Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob50f520ce1281425480acc6480912ff39c2162caf
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2016 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-iso-to-absolute "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-pop-to-buffer-same-window "org-compat"
84 (&optional buffer-or-name norecord label))
85 (declare-function org-agenda-columns "org-colview" ())
86 (declare-function org-add-archive-files "org-archive" (files))
87 (declare-function org-capture "org-capture" (&optional goto keys))
89 (defvar calendar-mode-map)
90 (defvar org-clock-current-task)
91 (defvar org-current-tag-alist)
92 (defvar org-mobile-force-id-on-agenda-items)
93 (defvar org-habit-show-habits)
94 (defvar org-habit-show-habits-only-for-today)
95 (defvar org-habit-show-all-today)
97 ;; Defined somewhere in this file, but used before definition.
98 (defvar org-agenda-buffer-name "*Org Agenda*")
99 (defvar org-agenda-overriding-header nil)
100 (defvar org-agenda-title-append nil)
101 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
102 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
103 (defvar original-date) ; dynamically scoped, calendar.el does scope this
105 (defvar org-agenda-undo-list nil
106 "List of undoable operations in the agenda since last refresh.")
107 (defvar org-agenda-pending-undo-list nil
108 "In a series of undo commands, this is the list of remaining undo items.")
110 (defcustom org-agenda-confirm-kill 1
111 "When set, remote killing from the agenda buffer needs confirmation.
112 When t, a confirmation is always needed. When a number N, confirmation is
113 only needed when the text to be killed contains more than N non-white lines."
114 :group 'org-agenda
115 :type '(choice
116 (const :tag "Never" nil)
117 (const :tag "Always" t)
118 (integer :tag "When more than N lines")))
120 (defcustom org-agenda-compact-blocks nil
121 "Non-nil means make the block agenda more compact.
122 This is done globally by leaving out lines like the agenda span
123 name and week number or the separator lines."
124 :group 'org-agenda
125 :type 'boolean)
127 (defcustom org-agenda-block-separator ?=
128 "The separator between blocks in the agenda.
129 If this is a string, it will be used as the separator, with a newline added.
130 If it is a character, it will be repeated to fill the window width.
131 If nil the separator is disabled. In `org-agenda-custom-commands' this
132 addresses the separator between the current and the previous block."
133 :group 'org-agenda
134 :type '(choice
135 (const :tag "Disabled" nil)
136 (character)
137 (string)))
139 (defgroup org-agenda-export nil
140 "Options concerning exporting agenda views in Org-mode."
141 :tag "Org Agenda Export"
142 :group 'org-agenda)
144 (defcustom org-agenda-with-colors t
145 "Non-nil means use colors in agenda views."
146 :group 'org-agenda-export
147 :type 'boolean)
149 (defcustom org-agenda-exporter-settings nil
150 "Alist of variable/value pairs that should be active during agenda export.
151 This is a good place to set options for ps-print and for htmlize.
152 Note that the way this is implemented, the values will be evaluated
153 before assigned to the variables. So make sure to quote values you do
154 *not* want evaluated, for example
156 (setq org-agenda-exporter-settings
157 \\='((ps-print-color-p \\='black-white)))"
158 :group 'org-agenda-export
159 :type '(repeat
160 (list
161 (variable)
162 (sexp :tag "Value"))))
164 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
165 "Hook run in a temporary buffer before writing the agenda to an export file.
166 A useful function for this hook is `org-agenda-add-entry-text'."
167 :group 'org-agenda-export
168 :type 'hook
169 :options '(org-agenda-add-entry-text))
171 (defcustom org-agenda-add-entry-text-maxlines 0
172 "Maximum number of entry text lines to be added to agenda.
173 This is only relevant when `org-agenda-add-entry-text' is part of
174 `org-agenda-before-write-hook', which is the default.
175 When this is 0, nothing will happen. When it is greater than 0, it
176 specifies the maximum number of lines that will be added for each entry
177 that is listed in the agenda view.
179 Note that this variable is not used during display, only when exporting
180 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
181 and `org-agenda-entry-text-maxlines'."
182 :group 'org-agenda
183 :type 'integer)
185 (defcustom org-agenda-add-entry-text-descriptive-links t
186 "Non-nil means export org-links as descriptive links in agenda added text.
187 This variable applies to the text added to the agenda when
188 `org-agenda-add-entry-text-maxlines' is larger than 0.
189 When this variable nil, the URL will (also) be shown."
190 :group 'org-agenda
191 :type 'boolean)
193 (defcustom org-agenda-export-html-style nil
194 "The style specification for exported HTML Agenda files.
195 If this variable contains a string, it will replace the default <style>
196 section as produced by `htmlize'.
197 Since there are different ways of setting style information, this variable
198 needs to contain the full HTML structure to provide a style, including the
199 surrounding HTML tags. The style specifications should include definitions
200 the fonts used by the agenda, here is an example:
202 <style type=\"text/css\">
203 p { font-weight: normal; color: gray; }
204 .org-agenda-structure {
205 font-size: 110%;
206 color: #003399;
207 font-weight: 600;
209 .org-todo {
210 color: #cc6666;
211 font-weight: bold;
213 .org-agenda-done {
214 color: #339933;
216 .org-done {
217 color: #339933;
219 .title { text-align: center; }
220 .todo, .deadline { color: red; }
221 .done { color: green; }
222 </style>
224 or, if you want to keep the style in a file,
226 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
228 As the value of this option simply gets inserted into the HTML <head> header,
229 you can \"misuse\" it to also add other text to the header."
230 :group 'org-agenda-export
231 :group 'org-export-html
232 :type '(choice
233 (const nil)
234 (string)))
236 (defcustom org-agenda-persistent-filter nil
237 "When set, keep filters from one agenda view to the next."
238 :group 'org-agenda
239 :type 'boolean)
241 (defgroup org-agenda-custom-commands nil
242 "Options concerning agenda views in Org-mode."
243 :tag "Org Agenda Custom Commands"
244 :group 'org-agenda)
246 (defconst org-sorting-choice
247 '(choice
248 (const time-up) (const time-down)
249 (const timestamp-up) (const timestamp-down)
250 (const scheduled-up) (const scheduled-down)
251 (const deadline-up) (const deadline-down)
252 (const ts-up) (const ts-down)
253 (const tsia-up) (const tsia-down)
254 (const category-keep) (const category-up) (const category-down)
255 (const tag-down) (const tag-up)
256 (const priority-up) (const priority-down)
257 (const todo-state-up) (const todo-state-down)
258 (const effort-up) (const effort-down)
259 (const habit-up) (const habit-down)
260 (const alpha-up) (const alpha-down)
261 (const user-defined-up) (const user-defined-down))
262 "Sorting choices.")
264 ;; Keep custom values for `org-agenda-filter-preset' compatible with
265 ;; the new variable `org-agenda-tag-filter-preset'.
266 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
267 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
269 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
270 "List of types searched for when creating the daily/weekly agenda.
271 This variable is a list of symbols that controls the types of
272 items that appear in the daily/weekly agenda. Allowed symbols in this
273 list are are
275 :timestamp List items containing a date stamp or date range matching
276 the selected date. This includes sexp entries in angular
277 brackets.
279 :sexp List entries resulting from plain diary-like sexps.
281 :deadline List deadline due on that date. When the date is today,
282 also list any deadlines past due, or due within
283 `org-deadline-warning-days'. `:deadline' must appear before
284 `:scheduled' if the setting of
285 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
286 any effect.
288 :deadline* Same as above, but only include the deadline if it has an
289 hour specification as [h]h:mm.
291 :scheduled List all items which are scheduled for the given date.
292 The diary for *today* also contains items which were
293 scheduled earlier and are not yet marked DONE.
295 :scheduled* Same as above, but only include the scheduled item if it
296 has an hour specification as [h]h:mm.
298 By default, all four non-starred types are turned on.
300 When :scheduled* or :deadline* are included, :schedule or :deadline
301 will be ignored.
303 Never set this variable globally using `setq', because then it
304 will apply to all future agenda commands. Instead, bind it with
305 `let' to scope it dynamically into the agenda-constructing
306 command. A good way to set it is through options in
307 `org-agenda-custom-commands'. For a more flexible (though
308 somewhat less efficient) way of determining what is included in
309 the daily/weekly agenda, see `org-agenda-skip-function'.")
311 (defconst org-agenda-custom-commands-local-options
312 `(repeat :tag "Local settings for this command. Remember to quote values"
313 (choice :tag "Setting"
314 (list :tag "Heading for this block"
315 (const org-agenda-overriding-header)
316 (string :tag "Headline"))
317 (list :tag "Files to be searched"
318 (const org-agenda-files)
319 (list
320 (const :format "" quote)
321 (repeat (file))))
322 (list :tag "Sorting strategy"
323 (const org-agenda-sorting-strategy)
324 (list
325 (const :format "" quote)
326 (repeat
327 ,org-sorting-choice)))
328 (list :tag "Prefix format"
329 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
330 (string))
331 (list :tag "Number of days in agenda"
332 (const org-agenda-span)
333 (choice (const :tag "Day" day)
334 (const :tag "Week" week)
335 (const :tag "Fortnight" fortnight)
336 (const :tag "Month" month)
337 (const :tag "Year" year)
338 (integer :tag "Custom")))
339 (list :tag "Fixed starting date"
340 (const org-agenda-start-day)
341 (string :value "2007-11-01"))
342 (list :tag "Start on day of week"
343 (const org-agenda-start-on-weekday)
344 (choice :value 1
345 (const :tag "Today" nil)
346 (integer :tag "Weekday No.")))
347 (list :tag "Include data from diary"
348 (const org-agenda-include-diary)
349 (boolean))
350 (list :tag "Deadline Warning days"
351 (const org-deadline-warning-days)
352 (integer :value 1))
353 (list :tag "Category filter preset"
354 (const org-agenda-category-filter-preset)
355 (list
356 (const :format "" quote)
357 (repeat
358 (string :tag "+category or -category"))))
359 (list :tag "Tags filter preset"
360 (const org-agenda-tag-filter-preset)
361 (list
362 (const :format "" quote)
363 (repeat
364 (string :tag "+tag or -tag"))))
365 (list :tag "Effort filter preset"
366 (const org-agenda-effort-filter-preset)
367 (list
368 (const :format "" quote)
369 (repeat
370 (string :tag "+=10 or -=10 or +<10 or ->10"))))
371 (list :tag "Regexp filter preset"
372 (const org-agenda-regexp-filter-preset)
373 (list
374 (const :format "" quote)
375 (repeat
376 (string :tag "+regexp or -regexp"))))
377 (list :tag "Set daily/weekly entry types"
378 (const org-agenda-entry-types)
379 (list
380 (const :format "" quote)
381 (set :greedy t :value ,org-agenda-entry-types
382 (const :deadline)
383 (const :scheduled)
384 (const :deadline*)
385 (const :scheduled*)
386 (const :timestamp)
387 (const :sexp))))
388 (list :tag "Standard skipping condition"
389 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
390 (const org-agenda-skip-function)
391 (list
392 (const :format "" quote)
393 (list
394 (choice
395 :tag "Skipping range"
396 (const :tag "Skip entry" org-agenda-skip-entry-if)
397 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
398 (repeat :inline t :tag "Conditions for skipping"
399 (choice
400 :tag "Condition type"
401 (list :tag "Regexp matches" :inline t
402 (const :format "" 'regexp)
403 (regexp))
404 (list :tag "Regexp does not match" :inline t
405 (const :format "" 'notregexp)
406 (regexp))
407 (list :tag "TODO state is" :inline t
408 (const 'todo)
409 (choice
410 (const :tag "Any not-done state" 'todo)
411 (const :tag "Any done state" 'done)
412 (const :tag "Any state" 'any)
413 (list :tag "Keyword list"
414 (const :format "" quote)
415 (repeat (string :tag "Keyword")))))
416 (list :tag "TODO state is not" :inline t
417 (const 'nottodo)
418 (choice
419 (const :tag "Any not-done state" 'todo)
420 (const :tag "Any done state" 'done)
421 (const :tag "Any state" 'any)
422 (list :tag "Keyword list"
423 (const :format "" quote)
424 (repeat (string :tag "Keyword")))))
425 (const :tag "scheduled" 'scheduled)
426 (const :tag "not scheduled" 'notscheduled)
427 (const :tag "deadline" 'deadline)
428 (const :tag "no deadline" 'notdeadline)
429 (const :tag "timestamp" 'timestamp)
430 (const :tag "no timestamp" 'nottimestamp))))))
431 (list :tag "Non-standard skipping condition"
432 :value (org-agenda-skip-function)
433 (const org-agenda-skip-function)
434 (sexp :tag "Function or form (quoted!)"))
435 (list :tag "Any variable"
436 (variable :tag "Variable")
437 (sexp :tag "Value (sexp)"))))
438 "Selection of examples for agenda command settings.
439 This will be spliced into the custom type of
440 `org-agenda-custom-commands'.")
443 (defcustom org-agenda-custom-commands
444 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
445 "Custom commands for the agenda.
446 These commands will be offered on the splash screen displayed by the
447 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
449 (key desc type match settings files)
451 key The key (one or more characters as a string) to be associated
452 with the command.
453 desc A description of the command, when omitted or nil, a default
454 description is built using MATCH.
455 type The command type, any of the following symbols:
456 agenda The daily/weekly agenda.
457 todo Entries with a specific TODO keyword, in all agenda files.
458 search Entries containing search words entry or headline.
459 tags Tags/Property/TODO match in all agenda files.
460 tags-todo Tags/P/T match in all agenda files, TODO entries only.
461 todo-tree Sparse tree of specific TODO keyword in *current* file.
462 tags-tree Sparse tree with all tags matches in *current* file.
463 occur-tree Occur sparse tree for *current* file.
464 ... A user-defined function.
465 match What to search for:
466 - a single keyword for TODO keyword searches
467 - a tags match expression for tags searches
468 - a word search expression for text searches.
469 - a regular expression for occur searches
470 For all other commands, this should be the empty string.
471 settings A list of option settings, similar to that in a let form, so like
472 this: ((opt1 val1) (opt2 val2) ...). The values will be
473 evaluated at the moment of execution, so quote them when needed.
474 files A list of files file to write the produced agenda buffer to
475 with the command `org-store-agenda-views'.
476 If a file name ends in \".html\", an HTML version of the buffer
477 is written out. If it ends in \".ps\", a postscript version is
478 produced. Otherwise, only the plain text is written to the file.
480 You can also define a set of commands, to create a composite agenda buffer.
481 In this case, an entry looks like this:
483 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
485 where
487 desc A description string to be displayed in the dispatcher menu.
488 cmd An agenda command, similar to the above. However, tree commands
489 are not allowed, but instead you can get agenda and global todo list.
490 So valid commands for a set are:
491 (agenda \"\" settings)
492 (alltodo \"\" settings)
493 (stuck \"\" settings)
494 (todo \"match\" settings files)
495 (search \"match\" settings files)
496 (tags \"match\" settings files)
497 (tags-todo \"match\" settings files)
499 Each command can carry a list of options, and another set of options can be
500 given for the whole set of commands. Individual command options take
501 precedence over the general options.
503 When using several characters as key to a command, the first characters
504 are prefix commands. For the dispatcher to display useful information, you
505 should provide a description for the prefix, like
507 (setq org-agenda-custom-commands
508 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
509 (\"hl\" tags \"+HOME+Lisa\")
510 (\"hp\" tags \"+HOME+Peter\")
511 (\"hk\" tags \"+HOME+Kim\")))"
512 :group 'org-agenda-custom-commands
513 :type `(repeat
514 (choice :value ("x" "Describe command here" tags "" nil)
515 (list :tag "Single command"
516 (string :tag "Access Key(s) ")
517 (option (string :tag "Description"))
518 (choice
519 (const :tag "Agenda" agenda)
520 (const :tag "TODO list" alltodo)
521 (const :tag "Search words" search)
522 (const :tag "Stuck projects" stuck)
523 (const :tag "Tags/Property match (all agenda files)" tags)
524 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
525 (const :tag "TODO keyword search (all agenda files)" todo)
526 (const :tag "Tags sparse tree (current buffer)" tags-tree)
527 (const :tag "TODO keyword tree (current buffer)" todo-tree)
528 (const :tag "Occur tree (current buffer)" occur-tree)
529 (sexp :tag "Other, user-defined function"))
530 (string :tag "Match (only for some commands)")
531 ,org-agenda-custom-commands-local-options
532 (option (repeat :tag "Export" (file :tag "Export to"))))
533 (list :tag "Command series, all agenda files"
534 (string :tag "Access Key(s)")
535 (string :tag "Description ")
536 (repeat :tag "Component"
537 (choice
538 (list :tag "Agenda"
539 (const :format "" agenda)
540 (const :tag "" :format "" "")
541 ,org-agenda-custom-commands-local-options)
542 (list :tag "TODO list (all keywords)"
543 (const :format "" alltodo)
544 (const :tag "" :format "" "")
545 ,org-agenda-custom-commands-local-options)
546 (list :tag "Search words"
547 (const :format "" search)
548 (string :tag "Match")
549 ,org-agenda-custom-commands-local-options)
550 (list :tag "Stuck projects"
551 (const :format "" stuck)
552 (const :tag "" :format "" "")
553 ,org-agenda-custom-commands-local-options)
554 (list :tag "Tags search"
555 (const :format "" tags)
556 (string :tag "Match")
557 ,org-agenda-custom-commands-local-options)
558 (list :tag "Tags search, TODO entries only"
559 (const :format "" tags-todo)
560 (string :tag "Match")
561 ,org-agenda-custom-commands-local-options)
562 (list :tag "TODO keyword search"
563 (const :format "" todo)
564 (string :tag "Match")
565 ,org-agenda-custom-commands-local-options)
566 (list :tag "Other, user-defined function"
567 (symbol :tag "function")
568 (string :tag "Match")
569 ,org-agenda-custom-commands-local-options)))
571 (repeat :tag "Settings for entire command set"
572 (list (variable :tag "Any variable")
573 (sexp :tag "Value")))
574 (option (repeat :tag "Export" (file :tag "Export to"))))
575 (cons :tag "Prefix key documentation"
576 (string :tag "Access Key(s)")
577 (string :tag "Description ")))))
579 (defcustom org-agenda-query-register ?o
580 "The register holding the current query string.
581 The purpose of this is that if you construct a query string interactively,
582 you can then use it to define a custom command."
583 :group 'org-agenda-custom-commands
584 :type 'character)
586 (defcustom org-stuck-projects
587 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
588 "How to identify stuck projects.
589 This is a list of four items:
590 1. A tags/todo/property matcher string that is used to identify a project.
591 See the manual for a description of tag and property searches.
592 The entire tree below a headline matched by this is considered one project.
593 2. A list of TODO keywords identifying non-stuck projects.
594 If the project subtree contains any headline with one of these todo
595 keywords, the project is considered to be not stuck. If you specify
596 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
597 3. A list of tags identifying non-stuck projects.
598 If the project subtree contains any headline with one of these tags,
599 the project is considered to be not stuck. If you specify \"*\" as
600 a tag, any tag will mark the project unstuck. Note that this is about
601 the explicit presence of a tag somewhere in the subtree, inherited
602 tags do not count here. If inherited tags make a project not stuck,
603 use \"-TAG\" in the tags part of the matcher under (1.) above.
604 4. An arbitrary regular expression matching non-stuck projects.
606 If the project turns out to be not stuck, search continues also in the
607 subtree to see if any of the subtasks have project status.
609 See also the variable `org-tags-match-list-sublevels' which applies
610 to projects matched by this search as well.
612 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
613 or `C-c a #' to produce the list."
614 :group 'org-agenda-custom-commands
615 :type '(list
616 (string :tag "Tags/TODO match to identify a project")
617 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
618 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
619 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
621 (defgroup org-agenda-skip nil
622 "Options concerning skipping parts of agenda files."
623 :tag "Org Agenda Skip"
624 :group 'org-agenda)
626 (defcustom org-agenda-skip-function-global nil
627 "Function to be called at each match during agenda construction.
628 If this function returns nil, the current match should not be skipped.
629 If the function decided to skip an agenda match, is must return the
630 buffer position from which the search should be continued.
631 This may also be a Lisp form, which will be evaluated.
633 This variable will be applied to every agenda match, including
634 tags/property searches and TODO lists. So try to make the test function
635 do its checking as efficiently as possible. To implement a skipping
636 condition just for specific agenda commands, use the variable
637 `org-agenda-skip-function' which can be set in the options section
638 of custom agenda commands."
639 :group 'org-agenda-skip
640 :type 'sexp)
642 (defgroup org-agenda-daily/weekly nil
643 "Options concerning the daily/weekly agenda."
644 :tag "Org Agenda Daily/Weekly"
645 :group 'org-agenda)
646 (defgroup org-agenda-todo-list nil
647 "Options concerning the global todo list agenda view."
648 :tag "Org Agenda Todo List"
649 :group 'org-agenda)
650 (defgroup org-agenda-match-view nil
651 "Options concerning the general tags/property/todo match agenda view."
652 :tag "Org Agenda Match View"
653 :group 'org-agenda)
654 (defgroup org-agenda-search-view nil
655 "Options concerning the search agenda view."
656 :tag "Org Agenda Search View"
657 :group 'org-agenda)
659 (defvar org-agenda-archives-mode nil
660 "Non-nil means the agenda will include archived items.
661 If this is the symbol `trees', trees in the selected agenda scope
662 that are marked with the ARCHIVE tag will be included anyway. When this is
663 t, also all archive files associated with the current selection of agenda
664 files will be included.")
666 (defcustom org-agenda-restriction-lock-highlight-subtree t
667 "Non-nil means highlight the whole subtree when restriction is active.
668 Otherwise only highlight the headline. Highlighting the whole subtree is
669 useful to ensure no edits happen beyond the restricted region."
670 :group 'org-agenda
671 :type 'boolean)
673 (defcustom org-agenda-skip-comment-trees t
674 "Non-nil means skip trees that start with the COMMENT keyword.
675 When nil, these trees are also scanned by agenda commands."
676 :group 'org-agenda-skip
677 :type 'boolean)
679 (defcustom org-agenda-todo-list-sublevels t
680 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
681 When nil, the sublevels of a TODO entry are not checked, resulting in
682 potentially much shorter TODO lists."
683 :group 'org-agenda-skip
684 :group 'org-agenda-todo-list
685 :type 'boolean)
687 (defcustom org-agenda-todo-ignore-with-date nil
688 "Non-nil means don't show entries with a date in the global todo list.
689 You can use this if you prefer to mark mere appointments with a TODO keyword,
690 but don't want them to show up in the TODO list.
691 When this is set, it also covers deadlines and scheduled items, the settings
692 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
693 will be ignored.
694 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
695 :group 'org-agenda-skip
696 :group 'org-agenda-todo-list
697 :type 'boolean)
699 (defcustom org-agenda-todo-ignore-timestamp nil
700 "Non-nil means don't show entries with a timestamp.
701 This applies when creating the global todo list.
702 Valid values are:
704 past Don't show entries for today or in the past.
706 future Don't show entries with a timestamp in the future.
707 The idea behind this is that if it has a future
708 timestamp, you don't want to think about it until the
709 date.
711 all Don't show any entries with a timestamp in the global todo list.
712 The idea behind this is that by setting a timestamp, you
713 have already \"taken care\" of this item.
715 This variable can also have an integer as a value. If positive (N),
716 todos with a timestamp N or more days in the future will be ignored. If
717 negative (-N), todos with a timestamp N or more days in the past will be
718 ignored. If 0, todos with a timestamp either today or in the future will
719 be ignored. For example, a value of -1 will exclude todos with a
720 timestamp in the past (yesterday or earlier), while a value of 7 will
721 exclude todos with a timestamp a week or more in the future.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :version "24.1"
729 :type '(choice
730 (const :tag "Ignore future timestamp todos" future)
731 (const :tag "Ignore past or present timestamp todos" past)
732 (const :tag "Ignore all timestamp todos" all)
733 (const :tag "Show timestamp todos" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-todo-ignore-scheduled nil
737 "Non-nil means, ignore some scheduled TODO items when making TODO list.
738 This applies when creating the global todo list.
739 Valid values are:
741 past Don't show entries scheduled today or in the past.
743 future Don't show entries scheduled in the future.
744 The idea behind this is that by scheduling it, you don't want to
745 think about it until the scheduled date.
747 all Don't show any scheduled entries in the global todo list.
748 The idea behind this is that by scheduling it, you have already
749 \"taken care\" of this item.
751 t Same as `all', for backward compatibility.
753 This variable can also have an integer as a value. See
754 `org-agenda-todo-ignore-timestamp' for more details.
756 See also `org-agenda-todo-ignore-with-date'.
757 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
758 to make his option also apply to the tags-todo list."
759 :group 'org-agenda-skip
760 :group 'org-agenda-todo-list
761 :type '(choice
762 (const :tag "Ignore future-scheduled todos" future)
763 (const :tag "Ignore past- or present-scheduled todos" past)
764 (const :tag "Ignore all scheduled todos" all)
765 (const :tag "Ignore all scheduled todos (compatibility)" t)
766 (const :tag "Show scheduled todos" nil)
767 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
769 (defcustom org-agenda-todo-ignore-deadlines nil
770 "Non-nil means ignore some deadline TODO items when making TODO list.
771 There are different motivations for using different values, please think
772 carefully when configuring this variable.
774 This applies when creating the global todo list.
775 Valid values are:
777 near Don't show near deadline entries. A deadline is near when it is
778 closer than `org-deadline-warning-days' days. The idea behind this
779 is that such items will appear in the agenda anyway.
781 far Don't show TODO entries where a deadline has been defined, but
782 the deadline is not near. This is useful if you don't want to
783 use the todo list to figure out what to do now.
785 past Don't show entries with a deadline timestamp for today or in the past.
787 future Don't show entries with a deadline timestamp in the future, not even
788 when they become `near' ones. Use it with caution.
790 all Ignore all TODO entries that do have a deadline.
792 t Same as `near', for backward compatibility.
794 This variable can also have an integer as a value. See
795 `org-agenda-todo-ignore-timestamp' for more details.
797 See also `org-agenda-todo-ignore-with-date'.
798 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
799 to make his option also apply to the tags-todo list."
800 :group 'org-agenda-skip
801 :group 'org-agenda-todo-list
802 :type '(choice
803 (const :tag "Ignore near deadlines" near)
804 (const :tag "Ignore near deadlines (compatibility)" t)
805 (const :tag "Ignore far deadlines" far)
806 (const :tag "Ignore all TODOs with a deadlines" all)
807 (const :tag "Show all TODOs, even if they have a deadline" nil)
808 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
810 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
811 "Time unit to use when possibly ignoring an agenda item.
813 See the docstring of various `org-agenda-todo-ignore-*' options.
814 The default is to compare time stamps using days. An item is thus
815 considered to be in the future if it is at least one day after today.
816 Non-nil means to compare time stamps using seconds. An item is then
817 considered future if it has a time value later than current time."
818 :group 'org-agenda-skip
819 :group 'org-agenda-todo-list
820 :version "24.4"
821 :package-version '(Org . "8.0")
822 :type '(choice
823 (const :tag "Compare time with days" nil)
824 (const :tag "Compare time with seconds" t)))
826 (defcustom org-agenda-tags-todo-honor-ignore-options nil
827 "Non-nil means honor todo-list ignores options also in tags-todo search.
828 The variables
829 `org-agenda-todo-ignore-with-date',
830 `org-agenda-todo-ignore-timestamp',
831 `org-agenda-todo-ignore-scheduled',
832 `org-agenda-todo-ignore-deadlines'
833 make the global TODO list skip entries that have time stamps of certain
834 kinds. If this option is set, the same options will also apply for the
835 tags-todo search, which is the general tags/property matcher
836 restricted to unfinished TODO entries only."
837 :group 'org-agenda-skip
838 :group 'org-agenda-todo-list
839 :group 'org-agenda-match-view
840 :type 'boolean)
842 (defcustom org-agenda-skip-scheduled-if-done nil
843 "Non-nil means don't show scheduled items in agenda when they are done.
844 This is relevant for the daily/weekly agenda, not for the TODO list. And
845 it applies only to the actual date of the scheduling. Warnings about
846 an item with a past scheduling dates are always turned off when the item
847 is DONE."
848 :group 'org-agenda-skip
849 :group 'org-agenda-daily/weekly
850 :type 'boolean)
852 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
853 "Non-nil means skip scheduling line if same entry shows because of deadline.
855 In the agenda of today, an entry can show up multiple times
856 because it is both scheduled and has a nearby deadline, and maybe
857 a plain time stamp as well.
859 When this variable is nil, the entry will be shown several times.
861 When set to t, then only the deadline is shown and the fact that
862 the entry is scheduled today or was scheduled previously is not
863 shown.
865 When set to the symbol `not-today', skip scheduled previously,
866 but not scheduled today.
868 When set to the symbol `repeated-after-deadline', skip scheduled
869 items if they are repeated beyond the current deadline."
870 :group 'org-agenda-skip
871 :group 'org-agenda-daily/weekly
872 :type '(choice
873 (const :tag "Never" nil)
874 (const :tag "Always" t)
875 (const :tag "Not when scheduled today" not-today)
876 (const :tag "When repeated past deadline" repeated-after-deadline)))
878 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
879 "Non-nil means skip timestamp line if same entry shows because of deadline.
880 In the agenda of today, an entry can show up multiple times
881 because it has both a plain timestamp and has a nearby deadline.
882 When this variable is t, then only the deadline is shown and the
883 fact that the entry has a timestamp for or including today is not
884 shown. When this variable is nil, the entry will be shown
885 several times."
886 :group 'org-agenda-skip
887 :group 'org-agenda-daily/weekly
888 :version "24.1"
889 :type '(choice
890 (const :tag "Never" nil)
891 (const :tag "Always" t)))
893 (defcustom org-agenda-skip-deadline-if-done nil
894 "Non-nil means don't show deadlines when the corresponding item is done.
895 When nil, the deadline is still shown and should give you a happy feeling.
896 This is relevant for the daily/weekly agenda. And it applied only to the
897 actually date of the deadline. Warnings about approaching and past-due
898 deadlines are always turned off when the item is DONE."
899 :group 'org-agenda-skip
900 :group 'org-agenda-daily/weekly
901 :type 'boolean)
903 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
904 "Non-nil means skip deadline prewarning when entry is also scheduled.
905 This will apply on all days where a prewarning for the deadline would
906 be shown, but not at the day when the entry is actually due. On that day,
907 the deadline will be shown anyway.
908 This variable may be set to nil, t, the symbol `pre-scheduled',
909 or a number which will then give the number of days before the actual
910 deadline when the prewarnings should resume. The symbol `pre-scheduled'
911 eliminates the deadline prewarning only prior to the scheduled date.
912 This can be used in a workflow where the first showing of the deadline will
913 trigger you to schedule it, and then you don't want to be reminded of it
914 because you will take care of it on the day when scheduled."
915 :group 'org-agenda-skip
916 :group 'org-agenda-daily/weekly
917 :version "24.1"
918 :type '(choice
919 (const :tag "Always show prewarning" nil)
920 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
921 (const :tag "Remove prewarning if entry is scheduled" t)
922 (integer :tag "Restart prewarning N days before deadline")))
924 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
925 "Non-nil means skip scheduled delay when entry also has a deadline.
926 This variable may be set to nil, t, the symbol `post-deadline',
927 or a number which will then give the number of days after the actual
928 scheduled date when the delay should expire. The symbol `post-deadline'
929 eliminates the schedule delay when the date is posterior to the deadline."
930 :group 'org-agenda-skip
931 :group 'org-agenda-daily/weekly
932 :version "24.4"
933 :package-version '(Org . "8.0")
934 :type '(choice
935 (const :tag "Always honor delay" nil)
936 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
937 (const :tag "Ignore delay if entry has a deadline" t)
938 (integer :tag "Honor delay up until N days after the scheduled date")))
940 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
941 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
942 When non-nil, after the search for timestamps has matched once in an
943 entry, the rest of the entry will not be searched."
944 :group 'org-agenda-skip
945 :type 'boolean)
947 (defcustom org-agenda-skip-timestamp-if-done nil
948 "Non-nil means don't select item by timestamp or -range if it is DONE."
949 :group 'org-agenda-skip
950 :group 'org-agenda-daily/weekly
951 :type 'boolean)
953 (defcustom org-agenda-dim-blocked-tasks t
954 "Non-nil means dim blocked tasks in the agenda display.
955 This causes some overhead during agenda construction, but if you
956 have turned on `org-enforce-todo-dependencies',
957 `org-enforce-todo-checkbox-dependencies', or any other blocking
958 mechanism, this will create useful feedback in the agenda.
960 Instead of t, this variable can also have the value `invisible'.
961 Then blocked tasks will be invisible and only become visible when
962 they become unblocked. An exemption to this behavior is when a task is
963 blocked because of unchecked checkboxes below it. Since checkboxes do
964 not show up in the agenda views, making this task invisible you remove any
965 trace from agenda views that there is something to do. Therefore, a task
966 that is blocked because of checkboxes will never be made invisible, it
967 will only be dimmed."
968 :group 'org-agenda-daily/weekly
969 :group 'org-agenda-todo-list
970 :version "24.3"
971 :type '(choice
972 (const :tag "Do not dim" nil)
973 (const :tag "Dim to a gray face" t)
974 (const :tag "Make invisible" invisible)))
976 (defcustom org-timeline-show-empty-dates 3
977 "Non-nil means `org-timeline' also shows dates without an entry.
978 When nil, only the days which actually have entries are shown.
979 When t, all days between the first and the last date are shown.
980 When an integer, show also empty dates, but if there is a gap of more than
981 N days, just insert a special line indicating the size of the gap."
982 :group 'org-agenda-skip
983 :type '(choice
984 (const :tag "None" nil)
985 (const :tag "All" t)
986 (integer :tag "at most")))
988 (defgroup org-agenda-startup nil
989 "Options concerning initial settings in the Agenda in Org Mode."
990 :tag "Org Agenda Startup"
991 :group 'org-agenda)
993 (defcustom org-agenda-menu-show-matcher t
994 "Non-nil means show the match string in the agenda dispatcher menu.
995 When nil, the matcher string is not shown, but is put into the help-echo
996 property so than moving the mouse over the command shows it.
997 Setting it to nil is good if matcher strings are very long and/or if
998 you want to use two-columns display (see `org-agenda-menu-two-columns')."
999 :group 'org-agenda
1000 :version "24.1"
1001 :type 'boolean)
1003 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1005 (defcustom org-agenda-menu-two-columns nil
1006 "Non-nil means, use two columns to show custom commands in the dispatcher.
1007 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1008 to nil."
1009 :group 'org-agenda
1010 :version "24.1"
1011 :type 'boolean)
1013 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1014 (defcustom org-agenda-finalize-hook nil
1015 "Hook run just before displaying an agenda buffer.
1016 The buffer is still writable when the hook is called.
1018 You can modify some of the buffer substrings but you should be
1019 extra careful not to modify the text properties of the agenda
1020 headlines as the agenda display heavily relies on them."
1021 :group 'org-agenda-startup
1022 :type 'hook)
1024 (defcustom org-agenda-mouse-1-follows-link nil
1025 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1026 A longer mouse click will still set point. Needs to be set
1027 before org.el is loaded."
1028 :group 'org-agenda-startup
1029 :type 'boolean)
1031 (defcustom org-agenda-start-with-follow-mode nil
1032 "The initial value of follow mode in a newly created agenda window."
1033 :group 'org-agenda-startup
1034 :type 'boolean)
1036 (defcustom org-agenda-follow-indirect nil
1037 "Non-nil means `org-agenda-follow-mode' displays only the
1038 current item's tree, in an indirect buffer."
1039 :group 'org-agenda
1040 :version "24.1"
1041 :type 'boolean)
1043 (defcustom org-agenda-show-outline-path t
1044 "Non-nil means show outline path in echo area after line motion."
1045 :group 'org-agenda-startup
1046 :type 'boolean)
1048 (defcustom org-agenda-start-with-entry-text-mode nil
1049 "The initial value of entry-text-mode in a newly created agenda window."
1050 :group 'org-agenda-startup
1051 :type 'boolean)
1053 (defcustom org-agenda-entry-text-maxlines 5
1054 "Number of text lines to be added when `E' is pressed in the agenda.
1056 Note that this variable only used during agenda display. Add add entry text
1057 when exporting the agenda, configure the variable
1058 `org-agenda-add-entry-ext-maxlines'."
1059 :group 'org-agenda
1060 :type 'integer)
1062 (defcustom org-agenda-entry-text-exclude-regexps nil
1063 "List of regular expressions to clean up entry text.
1064 The complete matches of all regular expressions in this list will be
1065 removed from entry text before it is shown in the agenda."
1066 :group 'org-agenda
1067 :type '(repeat (regexp)))
1069 (defcustom org-agenda-entry-text-leaders " > "
1070 "Text prepended to the entry text in agenda buffers."
1071 :version "24.4"
1072 :package-version '(Org . "8.0")
1073 :group 'org-agenda
1074 :type 'string)
1076 (defvar org-agenda-entry-text-cleanup-hook nil
1077 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1078 This cleanup is done in a temporary buffer, so the function may inspect and
1079 change the entire buffer.
1080 Some default stuff like drawers and scheduling/deadline dates will already
1081 have been removed when this is called, as will any matches for regular
1082 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1084 (defvar org-agenda-include-inactive-timestamps nil
1085 "Non-nil means include inactive time stamps in agenda and timeline.
1086 Dynamically scoped.")
1088 (defgroup org-agenda-windows nil
1089 "Options concerning the windows used by the Agenda in Org Mode."
1090 :tag "Org Agenda Windows"
1091 :group 'org-agenda)
1093 (defcustom org-agenda-window-setup 'reorganize-frame
1094 "How the agenda buffer should be displayed.
1095 Possible values for this option are:
1097 current-window Show agenda in the current window, keeping all other windows.
1098 other-window Use `switch-to-buffer-other-window' to display agenda.
1099 only-window Show agenda, deleting all other windows.
1100 reorganize-frame Show only two windows on the current frame, the current
1101 window and the agenda.
1102 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1103 Also, when exiting the agenda, kill that frame.
1104 See also the variable `org-agenda-restore-windows-after-quit'."
1105 :group 'org-agenda-windows
1106 :type '(choice
1107 (const current-window)
1108 (const other-frame)
1109 (const other-window)
1110 (const only-window)
1111 (const reorganize-frame)))
1113 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1114 "The min and max height of the agenda window as a fraction of frame height.
1115 The value of the variable is a cons cell with two numbers between 0 and 1.
1116 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1117 :group 'org-agenda-windows
1118 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1120 (defcustom org-agenda-restore-windows-after-quit nil
1121 "Non-nil means restore window configuration upon exiting agenda.
1122 Before the window configuration is changed for displaying the agenda,
1123 the current status is recorded. When the agenda is exited with
1124 `q' or `x' and this option is set, the old state is restored. If
1125 `org-agenda-window-setup' is `other-frame', the value of this
1126 option will be ignored."
1127 :group 'org-agenda-windows
1128 :type 'boolean)
1130 (defcustom org-agenda-ndays nil
1131 "Number of days to include in overview display.
1132 Should be 1 or 7.
1133 Obsolete, see `org-agenda-span'."
1134 :group 'org-agenda-daily/weekly
1135 :type '(choice (const nil)
1136 (integer)))
1138 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1140 (defcustom org-agenda-span 'week
1141 "Number of days to include in overview display.
1142 Can be day, week, month, year, or any number of days.
1143 Custom commands can set this variable in the options section."
1144 :group 'org-agenda-daily/weekly
1145 :type '(choice (const :tag "Day" day)
1146 (const :tag "Week" week)
1147 (const :tag "Fortnight" fortnight)
1148 (const :tag "Month" month)
1149 (const :tag "Year" year)
1150 (integer :tag "Custom")))
1152 (defcustom org-agenda-start-on-weekday 1
1153 "Non-nil means start the overview always on the specified weekday.
1154 0 denotes Sunday, 1 denotes Monday, etc.
1155 When nil, always start on the current day.
1156 Custom commands can set this variable in the options section."
1157 :group 'org-agenda-daily/weekly
1158 :type '(choice (const :tag "Today" nil)
1159 (integer :tag "Weekday No.")))
1161 (defcustom org-agenda-show-all-dates t
1162 "Non-nil means `org-agenda' shows every day in the selected range.
1163 When nil, only the days which actually have entries are shown."
1164 :group 'org-agenda-daily/weekly
1165 :type 'boolean)
1167 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1168 "Format string for displaying dates in the agenda.
1169 Used by the daily/weekly agenda and by the timeline. This should be
1170 a format string understood by `format-time-string', or a function returning
1171 the formatted date as a string. The function must take a single argument,
1172 a calendar-style date list like (month day year)."
1173 :group 'org-agenda-daily/weekly
1174 :type '(choice
1175 (string :tag "Format string")
1176 (function :tag "Function")))
1178 (defun org-agenda-format-date-aligned (date)
1179 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1180 This function makes sure that dates are aligned for easy reading."
1181 (require 'cal-iso)
1182 (let* ((dayname (calendar-day-name date))
1183 (day (cadr date))
1184 (day-of-week (calendar-day-of-week date))
1185 (month (car date))
1186 (monthname (calendar-month-name month))
1187 (year (nth 2 date))
1188 (iso-week (org-days-to-iso-week
1189 (calendar-absolute-from-gregorian date)))
1190 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1191 (1- year))
1192 ((and (= month 12) (<= iso-week 1))
1193 (1+ year))
1194 (t year)))
1195 (weekstring (if (= day-of-week 1)
1196 (format " W%02d" iso-week)
1197 "")))
1198 (format "%-10s %2d %s %4d%s"
1199 dayname day monthname year weekstring)))
1201 (defcustom org-agenda-time-leading-zero nil
1202 "Non-nil means use leading zero for military times in agenda.
1203 For example, 9:30am would become 09:30 rather than 9:30."
1204 :group 'org-agenda-daily/weekly
1205 :version "24.1"
1206 :type 'boolean)
1208 (defcustom org-agenda-timegrid-use-ampm nil
1209 "When set, show AM/PM style timestamps on the timegrid."
1210 :group 'org-agenda
1211 :version "24.1"
1212 :type 'boolean)
1214 (defun org-agenda-time-of-day-to-ampm (time)
1215 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1216 (let* ((hour-number (string-to-number (substring time 0 -3)))
1217 (minute (substring time -2))
1218 (ampm "am"))
1219 (cond
1220 ((equal hour-number 12)
1221 (setq ampm "pm"))
1222 ((> hour-number 12)
1223 (setq ampm "pm")
1224 (setq hour-number (- hour-number 12))))
1225 (concat
1226 (if org-agenda-time-leading-zero
1227 (format "%02d" hour-number)
1228 (format "%02s" (number-to-string hour-number)))
1229 ":" minute ampm)))
1231 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1232 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1233 (if org-agenda-timegrid-use-ampm
1234 (org-agenda-time-of-day-to-ampm time)
1235 time))
1237 (defcustom org-agenda-weekend-days '(6 0)
1238 "Which days are weekend?
1239 These days get the special face `org-agenda-date-weekend' in the agenda
1240 and timeline buffers."
1241 :group 'org-agenda-daily/weekly
1242 :type '(set :greedy t
1243 (const :tag "Monday" 1)
1244 (const :tag "Tuesday" 2)
1245 (const :tag "Wednesday" 3)
1246 (const :tag "Thursday" 4)
1247 (const :tag "Friday" 5)
1248 (const :tag "Saturday" 6)
1249 (const :tag "Sunday" 0)))
1251 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1252 "Non-nil means jump to today when moving a past date forward in time.
1253 When using S-right in the agenda to move a a date forward, and the date
1254 stamp currently points to the past, the first key press will move it
1255 to today. WHen nil, just move one day forward even if the date stays
1256 in the past."
1257 :group 'org-agenda-daily/weekly
1258 :version "24.1"
1259 :type 'boolean)
1261 (defcustom org-agenda-include-diary nil
1262 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1263 Custom commands can set this variable in the options section."
1264 :group 'org-agenda-daily/weekly
1265 :type 'boolean)
1267 (defcustom org-agenda-include-deadlines t
1268 "If non-nil, include entries within their deadline warning period.
1269 Custom commands can set this variable in the options section."
1270 :group 'org-agenda-daily/weekly
1271 :version "24.1"
1272 :type 'boolean)
1274 (defcustom org-agenda-repeating-timestamp-show-all t
1275 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1276 When set to a list of strings, only show occurrences of repeating
1277 stamps for these TODO keywords. When nil, only one occurrence is
1278 shown, either today or the nearest into the future."
1279 :group 'org-agenda-daily/weekly
1280 :type '(choice
1281 (const :tag "Show repeating stamps" t)
1282 (repeat :tag "Show repeating stamps for these TODO keywords"
1283 (string :tag "TODO Keyword"))
1284 (const :tag "Don't show repeating stamps" nil)))
1286 (defcustom org-scheduled-past-days 10000
1287 "Number of days to continue listing scheduled items not marked DONE.
1288 When an item is scheduled on a date, it shows up in the agenda on
1289 this day and will be listed until it is marked done or for the
1290 number of days given here."
1291 :group 'org-agenda-daily/weekly
1292 :type 'integer)
1294 (defcustom org-agenda-log-mode-items '(closed clock)
1295 "List of items that should be shown in agenda log mode.
1296 \\<org-agenda-mode-map>\
1297 This list may contain the following symbols:
1299 closed Show entries that have been closed on that day.
1300 clock Show entries that have received clocked time on that day.
1301 state Show all logged state changes.
1302 Note that instead of changing this variable, you can also press \
1303 `\\[universal-argument] \\[org-agenda-log-mode]' in
1304 the agenda to display all available LOG items temporarily."
1305 :group 'org-agenda-daily/weekly
1306 :type '(set :greedy t (const closed) (const clock) (const state)))
1308 (defcustom org-agenda-clock-consistency-checks
1309 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1310 :gap-ok-around ("4:00")
1311 :default-face ((:background "DarkRed") (:foreground "white"))
1312 :overlap-face nil :gap-face nil :no-end-time-face nil
1313 :long-face nil :short-face nil)
1314 "This is a property list, with the following keys:
1316 :max-duration Mark clocking chunks that are longer than this time.
1317 This is a time string like \"HH:MM\", or the number
1318 of minutes as an integer.
1320 :min-duration Mark clocking chunks that are shorter that this.
1321 This is a time string like \"HH:MM\", or the number
1322 of minutes as an integer.
1324 :max-gap Mark gaps between clocking chunks that are longer than
1325 this duration. A number of minutes, or a string
1326 like \"HH:MM\".
1328 :gap-ok-around List of times during the day which are usually not working
1329 times. When a gap is detected, but the gap contains any
1330 of these times, the gap is *not* reported. For example,
1331 if this is (\"4:00\" \"13:00\") then gaps that contain
1332 4:00 in the morning (i.e. the night) and 13:00
1333 (i.e. a typical lunch time) do not cause a warning.
1334 You should have at least one time during the night in this
1335 list, or otherwise the first task each morning will trigger
1336 a warning because it follows a long gap.
1338 Furthermore, the following properties can be used to define faces for
1339 issue display.
1341 :default-face the default face, if the specific face is undefined
1342 :overlap-face face for overlapping clocks
1343 :gap-face face for gaps between clocks
1344 :no-end-time-face face for incomplete clocks
1345 :long-face face for clock intervals that are too long
1346 :short-face face for clock intervals that are too short"
1347 :group 'org-agenda-daily/weekly
1348 :group 'org-clock
1349 :version "24.1"
1350 :type 'plist)
1352 (defcustom org-agenda-log-mode-add-notes t
1353 "Non-nil means add first line of notes to log entries in agenda views.
1354 If a log item like a state change or a clock entry is associated with
1355 notes, the first line of these notes will be added to the entry in the
1356 agenda display."
1357 :group 'org-agenda-daily/weekly
1358 :type 'boolean)
1360 (defcustom org-agenda-start-with-log-mode nil
1361 "The initial value of log-mode in a newly created agenda window.
1362 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1363 explanations on the possible values."
1364 :group 'org-agenda-startup
1365 :group 'org-agenda-daily/weekly
1366 :type '(choice (const :tag "Don't show log items" nil)
1367 (const :tag "Show only log items" only)
1368 (const :tag "Show all possible log items" clockcheck)
1369 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1370 (choice (const :tag "Show closed log items" closed)
1371 (const :tag "Show clocked log items" clock)
1372 (const :tag "Show all logged state changes" state)))))
1374 (defcustom org-agenda-start-with-clockreport-mode nil
1375 "The initial value of clockreport-mode in a newly created agenda window."
1376 :group 'org-agenda-startup
1377 :group 'org-agenda-daily/weekly
1378 :type 'boolean)
1380 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1381 "Property list with parameters for the clocktable in clockreport mode.
1382 This is the display mode that shows a clock table in the daily/weekly
1383 agenda, the properties for this dynamic block can be set here.
1384 The usual clocktable parameters are allowed here, but you cannot set
1385 the properties :name, :tstart, :tend, :block, and :scope - these will
1386 be overwritten to make sure the content accurately reflects the
1387 current display in the agenda."
1388 :group 'org-agenda-daily/weekly
1389 :type 'plist)
1391 (defcustom org-agenda-search-view-always-boolean nil
1392 "Non-nil means the search string is interpreted as individual parts.
1394 The search string for search view can either be interpreted as a phrase,
1395 or as a list of snippets that define a boolean search for a number of
1396 strings.
1398 When this is non-nil, the string will be split on whitespace, and each
1399 snippet will be searched individually, and all must match in order to
1400 select an entry. A snippet is then a single string of non-white
1401 characters, or a string in double quotes, or a regexp in {} braces.
1402 If a snippet is preceded by \"-\", the snippet must *not* match.
1403 \"+\" is syntactic sugar for positive selection. Each snippet may
1404 be found as a full word or a partial word, but see the variable
1405 `org-agenda-search-view-force-full-words'.
1407 When this is nil, search will look for the entire search phrase as one,
1408 with each space character matching any amount of whitespace, including
1409 line breaks.
1411 Even when this is nil, you can still switch to Boolean search dynamically
1412 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1413 is a regexp marked with braces like \"{abc}\", this will also switch to
1414 boolean search."
1415 :group 'org-agenda-search-view
1416 :version "24.1"
1417 :type 'boolean)
1419 (org-defvaralias 'org-agenda-search-view-search-words-only
1420 'org-agenda-search-view-always-boolean)
1422 (defcustom org-agenda-search-view-force-full-words nil
1423 "Non-nil means, search words must be matches as complete words.
1424 When nil, they may also match part of a word."
1425 :group 'org-agenda-search-view
1426 :version "24.1"
1427 :type 'boolean)
1429 (defcustom org-agenda-search-view-max-outline-level 0
1430 "Maximum outline level to display in search view.
1431 E.g. when this is set to 1, the search view will only
1432 show headlines of level 1. When set to 0, the default
1433 value, don't limit agenda view by outline level."
1434 :group 'org-agenda-search-view
1435 :version "24.4"
1436 :package-version '(Org . "8.3")
1437 :type 'integer)
1439 (defgroup org-agenda-time-grid nil
1440 "Options concerning the time grid in the Org-mode Agenda."
1441 :tag "Org Agenda Time Grid"
1442 :group 'org-agenda)
1444 (defcustom org-agenda-search-headline-for-time t
1445 "Non-nil means search headline for a time-of-day.
1446 If the headline contains a time-of-day in one format or another, it will
1447 be used to sort the entry into the time sequence of items for a day.
1448 Some people have time stamps in the headline that refer to the creation
1449 time or so, and then this produces an unwanted side effect. If this is
1450 the case for your, use this variable to turn off searching the headline
1451 for a time."
1452 :group 'org-agenda-time-grid
1453 :type 'boolean)
1455 (defcustom org-agenda-use-time-grid t
1456 "Non-nil means show a time grid in the agenda schedule.
1457 A time grid is a set of lines for specific times (like every two hours between
1458 8:00 and 20:00). The items scheduled for a day at specific times are
1459 sorted in between these lines.
1460 For details about when the grid will be shown, and what it will look like, see
1461 the variable `org-agenda-time-grid'."
1462 :group 'org-agenda-time-grid
1463 :type 'boolean)
1465 (defcustom org-agenda-time-grid
1466 '((daily today require-timed)
1467 "----------------"
1468 (800 1000 1200 1400 1600 1800 2000))
1470 "The settings for time grid for agenda display.
1471 This is a list of three items. The first item is again a list. It contains
1472 symbols specifying conditions when the grid should be displayed:
1474 daily if the agenda shows a single day
1475 weekly if the agenda shows an entire week
1476 today show grid on current date, independent of daily/weekly display
1477 require-timed show grid only if at least one item has a time specification
1478 remove-match skip grid times already present in an entry
1480 The second item is a string which will be placed behind the grid time.
1482 The third item is a list of integers, indicating the times that should have
1483 a grid line."
1484 :group 'org-agenda-time-grid
1485 :type
1486 '(list
1487 (set :greedy t :tag "Grid Display Options"
1488 (const :tag "Show grid in single day agenda display" daily)
1489 (const :tag "Show grid in weekly agenda display" weekly)
1490 (const :tag "Always show grid for today" today)
1491 (const :tag "Show grid only if any timed entries are present"
1492 require-timed)
1493 (const :tag "Skip grid times already present in an entry"
1494 remove-match))
1495 (string :tag "Grid String")
1496 (repeat :tag "Grid Times" (integer :tag "Time"))))
1498 (defcustom org-agenda-show-current-time-in-grid t
1499 "Non-nil means show the current time in the time grid."
1500 :group 'org-agenda-time-grid
1501 :version "24.1"
1502 :type 'boolean)
1504 (defcustom org-agenda-current-time-string
1505 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1506 "The string for the current time marker in the agenda."
1507 :group 'org-agenda-time-grid
1508 :version "24.1"
1509 :type 'string)
1511 (defgroup org-agenda-sorting nil
1512 "Options concerning sorting in the Org-mode Agenda."
1513 :tag "Org Agenda Sorting"
1514 :group 'org-agenda)
1516 (defcustom org-agenda-sorting-strategy
1517 '((agenda habit-down time-up priority-down category-keep)
1518 (todo priority-down category-keep)
1519 (tags priority-down category-keep)
1520 (search category-keep))
1521 "Sorting structure for the agenda items of a single day.
1522 This is a list of symbols which will be used in sequence to determine
1523 if an entry should be listed before another entry. The following
1524 symbols are recognized:
1526 time-up Put entries with time-of-day indications first, early first
1527 time-down Put entries with time-of-day indications first, late first
1528 timestamp-up Sort by any timestamp, early first
1529 timestamp-down Sort by any timestamp, late first
1530 scheduled-up Sort by scheduled timestamp, early first
1531 scheduled-down Sort by scheduled timestamp, late first
1532 deadline-up Sort by deadline timestamp, early first
1533 deadline-down Sort by deadline timestamp, late first
1534 ts-up Sort by active timestamp, early first
1535 ts-down Sort by active timestamp, late first
1536 tsia-up Sort by inactive timestamp, early first
1537 tsia-down Sort by inactive timestamp, late first
1538 category-keep Keep the default order of categories, corresponding to the
1539 sequence in `org-agenda-files'.
1540 category-up Sort alphabetically by category, A-Z.
1541 category-down Sort alphabetically by category, Z-A.
1542 tag-up Sort alphabetically by last tag, A-Z.
1543 tag-down Sort alphabetically by last tag, Z-A.
1544 priority-up Sort numerically by priority, high priority last.
1545 priority-down Sort numerically by priority, high priority first.
1546 todo-state-up Sort by todo state, tasks that are done last.
1547 todo-state-down Sort by todo state, tasks that are done first.
1548 effort-up Sort numerically by estimated effort, high effort last.
1549 effort-down Sort numerically by estimated effort, high effort first.
1550 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1551 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1552 habit-up Put entries that are habits first
1553 habit-down Put entries that are habits last
1554 alpha-up Sort headlines alphabetically
1555 alpha-down Sort headlines alphabetically, reversed
1557 The different possibilities will be tried in sequence, and testing stops
1558 if one comparison returns a \"not-equal\". For example, the default
1559 '(time-up category-keep priority-down)
1560 means: Pull out all entries having a specified time of day and sort them,
1561 in order to make a time schedule for the current day the first thing in the
1562 agenda listing for the day. Of the entries without a time indication, keep
1563 the grouped in categories, don't sort the categories, but keep them in
1564 the sequence given in `org-agenda-files'. Within each category sort by
1565 priority.
1567 Leaving out `category-keep' would mean that items will be sorted across
1568 categories by priority.
1570 Instead of a single list, this can also be a set of list for specific
1571 contents, with a context symbol in the car of the list, any of
1572 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1574 Custom commands can bind this variable in the options section."
1575 :group 'org-agenda-sorting
1576 :type `(choice
1577 (repeat :tag "General" ,org-sorting-choice)
1578 (list :tag "Individually"
1579 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1580 (repeat ,org-sorting-choice))
1581 (cons (const :tag "Strategy for TODO lists" todo)
1582 (repeat ,org-sorting-choice))
1583 (cons (const :tag "Strategy for Tags matches" tags)
1584 (repeat ,org-sorting-choice))
1585 (cons (const :tag "Strategy for search matches" search)
1586 (repeat ,org-sorting-choice)))))
1588 (defcustom org-agenda-cmp-user-defined nil
1589 "A function to define the comparison `user-defined'.
1590 This function must receive two arguments, agenda entry a and b.
1591 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1592 the user comparison, return nil.
1593 When this is defined, you can make `user-defined-up' and `user-defined-down'
1594 part of an agenda sorting strategy."
1595 :group 'org-agenda-sorting
1596 :type 'symbol)
1598 (defcustom org-sort-agenda-notime-is-late t
1599 "Non-nil means items without time are considered late.
1600 This is only relevant for sorting. When t, items which have no explicit
1601 time like 15:30 will be considered as 99:01, i.e. later than any items which
1602 do have a time. When nil, the default time is before 0:00. You can use this
1603 option to decide if the schedule for today should come before or after timeless
1604 agenda entries."
1605 :group 'org-agenda-sorting
1606 :type 'boolean)
1608 (defcustom org-sort-agenda-noeffort-is-high t
1609 "Non-nil means items without effort estimate are sorted as high effort.
1610 This also applies when filtering an agenda view with respect to the
1611 < or > effort operator. Then, tasks with no effort defined will be treated
1612 as tasks with high effort.
1613 When nil, such items are sorted as 0 minutes effort."
1614 :group 'org-agenda-sorting
1615 :type 'boolean)
1617 (defgroup org-agenda-line-format nil
1618 "Options concerning the entry prefix in the Org-mode agenda display."
1619 :tag "Org Agenda Line Format"
1620 :group 'org-agenda)
1622 (defcustom org-agenda-prefix-format
1623 '((agenda . " %i %-12:c%?-12t% s")
1624 (timeline . " % s")
1625 (todo . " %i %-12:c")
1626 (tags . " %i %-12:c")
1627 (search . " %i %-12:c"))
1628 "Format specifications for the prefix of items in the agenda views.
1629 An alist with five entries, each for the different agenda types. The
1630 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1631 The values are format strings.
1633 This format works similar to a printf format, with the following meaning:
1635 %c the category of the item, \"Diary\" for entries from the diary,
1636 or as given by the CATEGORY keyword or derived from the file name
1637 %e the effort required by the item
1638 %l the level of the item (insert X space(s) if item is of level X)
1639 %i the icon category of the item, see `org-agenda-category-icon-alist'
1640 %T the last tag of the item (ignore inherited tags, which come first)
1641 %t the HH:MM time-of-day specification if one applies to the entry
1642 %s Scheduling/Deadline information, a short string
1643 %b show breadcrumbs, i.e., the names of the higher levels
1644 %(expression) Eval EXPRESSION and replace the control string
1645 by the result
1647 All specifiers work basically like the standard `%s' of printf, but may
1648 contain two additional characters: a question mark just after the `%'
1649 and a whitespace/punctuation character just before the final letter.
1651 If the first character after `%' is a question mark, the entire field
1652 will only be included if the corresponding value applies to the current
1653 entry. This is useful for fields which should have fixed width when
1654 present, but zero width when absent. For example, \"%?-12t\" will
1655 result in a 12 character time field if a time of the day is specified,
1656 but will completely disappear in entries which do not contain a time.
1658 If there is punctuation or whitespace character just before the
1659 final format letter, this character will be appended to the field
1660 value if the value is not empty. For example, the format
1661 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1662 the category is empty, no additional colon is inserted.
1664 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1665 which means:
1667 - Indent the line with two space characters
1668 - Give the category a 12 chars wide field, padded with whitespace on
1669 the right (because of `-'). Append a colon if there is a category
1670 (because of `:').
1671 - If there is a time-of-day, put it into a 12 chars wide field. If no
1672 time, don't put in an empty field, just skip it (because of '?').
1673 - Finally, put the scheduling information.
1675 See also the variables `org-agenda-remove-times-when-in-prefix' and
1676 `org-agenda-remove-tags'.
1678 Custom commands can set this variable in the options section."
1679 :type '(choice
1680 (string :tag "General format")
1681 (list :greedy t :tag "View dependent"
1682 (cons (const agenda) (string :tag "Format"))
1683 (cons (const timeline) (string :tag "Format"))
1684 (cons (const todo) (string :tag "Format"))
1685 (cons (const tags) (string :tag "Format"))
1686 (cons (const search) (string :tag "Format"))))
1687 :group 'org-agenda-line-format)
1689 (defvar org-prefix-format-compiled nil
1690 "The compiled prefix format and associated variables.
1691 This is a list where first element is a list of variable bindings, and second
1692 element is the compiled format expression. See the variable
1693 `org-agenda-prefix-format'.")
1695 (defcustom org-agenda-todo-keyword-format "%-1s"
1696 "Format for the TODO keyword in agenda lines.
1697 Set this to something like \"%-12s\" if you want all TODO keywords
1698 to occupy a fixed space in the agenda display."
1699 :group 'org-agenda-line-format
1700 :type 'string)
1702 (defcustom org-agenda-diary-sexp-prefix nil
1703 "A regexp that matches part of a diary sexp entry
1704 which should be treated as scheduling/deadline information in
1705 `org-agenda'.
1707 For example, you can use this to extract the `diary-remind-message' from
1708 `diary-remind' entries."
1709 :group 'org-agenda-line-format
1710 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1712 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1713 "Text preceding timerange entries in the agenda view.
1714 This is a list with two strings. The first applies when the range
1715 is entirely on one day. The second applies if the range spans several days.
1716 The strings may have two \"%d\" format specifiers which will be filled
1717 with the sequence number of the days, and the total number of days in the
1718 range, respectively."
1719 :group 'org-agenda-line-format
1720 :type '(list
1721 (string :tag "Deadline today ")
1722 (choice :tag "Deadline relative"
1723 (string :tag "Format string")
1724 (function))))
1726 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1727 "Text preceding scheduled items in the agenda view.
1728 This is a list with two strings. The first applies when the item is
1729 scheduled on the current day. The second applies when it has been scheduled
1730 previously, it may contain a %d indicating that this is the nth time that
1731 this item is scheduled, due to automatic rescheduling of unfinished items
1732 for the following day. So this number is one larger than the number of days
1733 that passed since this item was scheduled first."
1734 :group 'org-agenda-line-format
1735 :version "24.4"
1736 :package-version '(Org . "8.0")
1737 :type '(list
1738 (string :tag "Scheduled today ")
1739 (string :tag "Scheduled previously")))
1741 (defcustom org-agenda-inactive-leader "["
1742 "Text preceding item pulled into the agenda by inactive time stamps.
1743 These entries are added to the agenda when pressing \"[\"."
1744 :group 'org-agenda-line-format
1745 :version "24.1"
1746 :type 'string)
1748 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1749 "Text preceding deadline items in the agenda view.
1750 This is a list with three strings. The first applies when the item has its
1751 deadline on the current day. The second applies when the deadline is in the
1752 future, the third one when it is in the past. The strings may contain %d
1753 to capture the number of days."
1754 :group 'org-agenda-line-format
1755 :version "24.4"
1756 :package-version '(Org . "8.0")
1757 :type '(list
1758 (string :tag "Deadline today ")
1759 (string :tag "Deadline in the future ")
1760 (string :tag "Deadline in the past ")))
1762 (defcustom org-agenda-remove-times-when-in-prefix t
1763 "Non-nil means remove duplicate time specifications in agenda items.
1764 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1765 time-of-day specification in a headline or diary entry is extracted and
1766 placed into the prefix. If this option is non-nil, the original specification
1767 \(a timestamp or -range, or just a plain time(range) specification like
1768 11:30-4pm) will be removed for agenda display. This makes the agenda less
1769 cluttered.
1770 The option can be t or nil. It may also be the symbol `beg', indicating
1771 that the time should only be removed when it is located at the beginning of
1772 the headline/diary entry."
1773 :group 'org-agenda-line-format
1774 :type '(choice
1775 (const :tag "Always" t)
1776 (const :tag "Never" nil)
1777 (const :tag "When at beginning of entry" beg)))
1779 (defcustom org-agenda-remove-timeranges-from-blocks nil
1780 "Non-nil means remove time ranges specifications in agenda
1781 items that span on several days."
1782 :group 'org-agenda-line-format
1783 :version "24.1"
1784 :type 'boolean)
1786 (defcustom org-agenda-default-appointment-duration nil
1787 "Default duration for appointments that only have a starting time.
1788 When nil, no duration is specified in such cases.
1789 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1790 :group 'org-agenda-line-format
1791 :type '(choice
1792 (integer :tag "Minutes")
1793 (const :tag "No default duration")))
1795 (defcustom org-agenda-show-inherited-tags t
1796 "Non-nil means show inherited tags in each agenda line.
1798 When this option is set to `always', it take precedences over
1799 `org-agenda-use-tag-inheritance' and inherited tags are shown
1800 in every agenda.
1802 When this option is set to t (the default), inherited tags are
1803 shown when they are available, i.e. when the value of
1804 `org-agenda-use-tag-inheritance' has been taken into account.
1806 This can be set to a list of agenda types in which the agenda
1807 must display the inherited tags. Available types are `todo',
1808 `agenda', `search' and `timeline'.
1810 When set to nil, never show inherited tags in agenda lines."
1811 :group 'org-agenda-line-format
1812 :group 'org-agenda
1813 :version "24.3"
1814 :type '(choice
1815 (const :tag "Show inherited tags when available" t)
1816 (const :tag "Always show inherited tags" always)
1817 (repeat :tag "Show inherited tags only in selected agenda types"
1818 (symbol :tag "Agenda type"))))
1820 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1821 "List of agenda view types where to use tag inheritance.
1823 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1824 controlled by `org-use-tag-inheritance'. In other agenda types,
1825 `org-use-tag-inheritance' is not used for the selection of the
1826 agenda entries. Still, you may want the agenda to be aware of
1827 the inherited tags anyway, e.g. for later tag filtering.
1829 Allowed value are `todo', `search', `timeline' and `agenda'.
1831 This variable has no effect if `org-agenda-show-inherited-tags'
1832 is set to `always'. In that case, the agenda is aware of those
1833 tags.
1835 The default value sets tags in every agenda type. Setting this
1836 option to nil will speed up non-tags agenda view a lot."
1837 :group 'org-agenda
1838 :version "24.3"
1839 :type '(choice
1840 (const :tag "Use tag inheritance in all agenda types" t)
1841 (repeat :tag "Use tag inheritance in selected agenda types"
1842 (symbol :tag "Agenda type"))))
1844 (defcustom org-agenda-hide-tags-regexp nil
1845 "Regular expression used to filter away specific tags in agenda views.
1846 This means that these tags will be present, but not be shown in the agenda
1847 line. Secondary filtering will still work on the hidden tags.
1848 Nil means don't hide any tags."
1849 :group 'org-agenda-line-format
1850 :type '(choice
1851 (const :tag "Hide none" nil)
1852 (string :tag "Regexp ")))
1854 (defcustom org-agenda-remove-tags nil
1855 "Non-nil means remove the tags from the headline copy in the agenda.
1856 When this is the symbol `prefix', only remove tags when
1857 `org-agenda-prefix-format' contains a `%T' specifier."
1858 :group 'org-agenda-line-format
1859 :type '(choice
1860 (const :tag "Always" t)
1861 (const :tag "Never" nil)
1862 (const :tag "When prefix format contains %T" prefix)))
1864 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1865 'org-agenda-remove-tags)
1867 (defcustom org-agenda-tags-column -80
1868 "Shift tags in agenda items to this column.
1869 If this number is positive, it specifies the column. If it is negative,
1870 it means that the tags should be flushright to that column. For example,
1871 -80 works well for a normal 80 character screen."
1872 :group 'org-agenda-line-format
1873 :type 'integer)
1875 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1877 (defcustom org-agenda-fontify-priorities 'cookies
1878 "Non-nil means highlight low and high priorities in agenda.
1879 When t, the highest priority entries are bold, lowest priority italic.
1880 However, settings in `org-priority-faces' will overrule these faces.
1881 When this variable is the symbol `cookies', only fontify the
1882 cookies, not the entire task.
1883 This may also be an association list of priority faces, whose
1884 keys are the character values of `org-highest-priority',
1885 `org-default-priority', and `org-lowest-priority' (the default values
1886 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1887 color as a string, or a list like `(:background \"Red\")'.
1888 If it is a color, the variable `org-faces-easy-properties'
1889 determines if it is a foreground or a background color."
1890 :group 'org-agenda-line-format
1891 :type '(choice
1892 (const :tag "Never" nil)
1893 (const :tag "Defaults" t)
1894 (const :tag "Cookies only" cookies)
1895 (repeat :tag "Specify"
1896 (list (character :tag "Priority" :value ?A)
1897 (choice :tag "Face "
1898 (string :tag "Color")
1899 (sexp :tag "Face"))))))
1901 (defcustom org-agenda-day-face-function nil
1902 "Function called to determine what face should be used to display a day.
1903 The only argument passed to that function is the day. It should
1904 returns a face, or nil if does not want to specify a face and let
1905 the normal rules apply."
1906 :group 'org-agenda-line-format
1907 :version "24.1"
1908 :type '(choice (const nil) (function)))
1910 (defcustom org-agenda-category-icon-alist nil
1911 "Alist of category icon to be displayed in agenda views.
1913 Each entry should have the following format:
1915 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1917 Where CATEGORY-REGEXP is a regexp matching the categories where
1918 the icon should be displayed.
1919 FILE-OR-DATA either a file path or a string containing image data.
1921 The other fields can be omitted safely if not needed:
1922 TYPE indicates the image type.
1923 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1924 image data.
1925 PROPS are additional image attributes to assign to the image,
1926 like, e.g. `:ascent center'.
1928 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1930 If you want to set the display properties yourself, just put a
1931 list as second element:
1933 (CATEGORY-REGEXP (MY PROPERTY LIST))
1935 For example, to display a 16px horizontal space for Emacs
1936 category, you can use:
1938 (\"Emacs\" \\='(space . (:width (16))))"
1939 :group 'org-agenda-line-format
1940 :version "24.1"
1941 :type '(alist :key-type (string :tag "Regexp matching category")
1942 :value-type (choice (list :tag "Icon"
1943 (string :tag "File or data")
1944 (symbol :tag "Type")
1945 (boolean :tag "Data?")
1946 (repeat :tag "Extra image properties" :inline t symbol))
1947 (list :tag "Display properties" sexp))))
1949 (defgroup org-agenda-column-view nil
1950 "Options concerning column view in the agenda."
1951 :tag "Org Agenda Column View"
1952 :group 'org-agenda)
1954 (defcustom org-agenda-view-columns-initially nil
1955 "When non-nil, switch to columns view right after creating the agenda."
1956 :group 'org-agenda-column-view
1957 :type 'boolean
1958 :version "25.1"
1959 :package-version '(Org . "9.0")
1960 :safe #'booleanp)
1962 (defcustom org-agenda-columns-show-summaries t
1963 "Non-nil means show summaries for columns displayed in the agenda view."
1964 :group 'org-agenda-column-view
1965 :type 'boolean)
1967 (defcustom org-agenda-columns-compute-summary-properties t
1968 "Non-nil means recompute all summary properties before column view.
1969 When column view in the agenda is listing properties that have a summary
1970 operator, it can go to all relevant buffers and recompute the summaries
1971 there. This can mean overhead for the agenda column view, but is necessary
1972 to have thing up to date.
1973 As a special case, a CLOCKSUM property also makes sure that the clock
1974 computations are current."
1975 :group 'org-agenda-column-view
1976 :type 'boolean)
1978 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1979 "Non-nil means the duration of an appointment will add to day effort.
1980 The property to which appointment durations will be added is the one given
1981 in the option `org-effort-property'. If an appointment does not have
1982 an end time, `org-agenda-default-appointment-duration' will be used. If that
1983 is not set, an appointment without end time will not contribute to the time
1984 estimate."
1985 :group 'org-agenda-column-view
1986 :type 'boolean)
1988 (defcustom org-agenda-auto-exclude-function nil
1989 "A function called with a tag to decide if it is filtered on \
1990 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1991 The sole argument to the function, which is called once for each
1992 possible tag, is a string giving the name of the tag. The
1993 function should return either nil if the tag should be included
1994 as normal, or \"-<TAG>\" to exclude the tag.
1995 Note that for the purpose of tag filtering, only the lower-case version of
1996 all tags will be considered, so that this function will only ever see
1997 the lower-case version of all tags."
1998 :group 'org-agenda
1999 :type '(choice (const nil) (function)))
2001 (defcustom org-agenda-bulk-custom-functions nil
2002 "Alist of characters and custom functions for bulk actions.
2003 For example, this value makes those two functions available:
2005 \\='((?R set-category)
2006 (?C bulk-cut))
2008 With selected entries in an agenda buffer, `B R' will call
2009 the custom function `set-category' on the selected entries.
2010 Note that functions in this alist don't need to be quoted."
2011 :type '(alist :key-type character :value-type (group function))
2012 :version "24.1"
2013 :group 'org-agenda)
2015 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2016 "Execute BODY with point at location given by `org-hd-marker' property.
2017 If STRING is non-nil, the text property will be fetched from position 0
2018 in that string. If STRING is nil, it will be fetched from the beginning
2019 of the current line."
2020 (org-with-gensyms (marker)
2021 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2022 'org-hd-marker ,string)))
2023 (with-current-buffer (marker-buffer ,marker)
2024 (save-excursion
2025 (goto-char ,marker)
2026 ,@body)))))
2027 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2029 (defun org-add-agenda-custom-command (entry)
2030 "Replace or add a command in `org-agenda-custom-commands'.
2031 This is mostly for hacking and trying a new command - once the command
2032 works you probably want to add it to `org-agenda-custom-commands' for good."
2033 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2034 (if ass
2035 (setcdr ass (cdr entry))
2036 (push entry org-agenda-custom-commands))))
2038 ;;; Define the org-agenda-mode
2040 (defvar org-agenda-mode-map (make-sparse-keymap)
2041 "Keymap for `org-agenda-mode'.")
2042 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2044 (defvar org-agenda-menu) ; defined later in this file.
2045 (defvar org-agenda-restrict nil) ; defined later in this file.
2046 (defvar org-agenda-follow-mode nil)
2047 (defvar org-agenda-entry-text-mode nil)
2048 (defvar org-agenda-clockreport-mode nil)
2049 (defvar org-agenda-show-log nil)
2050 (defvar org-agenda-redo-command nil)
2051 (defvar org-agenda-query-string nil)
2052 (defvar org-agenda-mode-hook nil
2053 "Hook run after `org-agenda-mode' is turned on.
2054 The buffer is still writable when this hook is called.")
2055 (defvar org-agenda-type nil)
2056 (defvar org-agenda-force-single-file nil)
2057 (defvar org-agenda-bulk-marked-entries nil
2058 "List of markers that refer to marked entries in the agenda.")
2059 (defvar org-agenda-current-date nil
2060 "Active date when building the agenda.")
2062 ;;; Multiple agenda buffers support
2064 (defcustom org-agenda-sticky nil
2065 "Non-nil means agenda q key will bury agenda buffers.
2066 Agenda commands will then show existing buffer instead of generating new ones.
2067 When nil, `q' will kill the single agenda buffer."
2068 :group 'org-agenda
2069 :version "24.3"
2070 :type 'boolean)
2073 ;;;###autoload
2074 (defun org-toggle-sticky-agenda (&optional arg)
2075 "Toggle `org-agenda-sticky'."
2076 (interactive "P")
2077 (let ((new-value (if arg
2078 (> (prefix-numeric-value arg) 0)
2079 (not org-agenda-sticky))))
2080 (if (equal new-value org-agenda-sticky)
2081 (and (org-called-interactively-p 'interactive)
2082 (message "Sticky agenda was already %s"
2083 (if org-agenda-sticky "enabled" "disabled")))
2084 (setq org-agenda-sticky new-value)
2085 (org-agenda-kill-all-agenda-buffers)
2086 (and (org-called-interactively-p 'interactive)
2087 (message "Sticky agenda %s"
2088 (if org-agenda-sticky "enabled" "disabled"))))))
2090 (defvar org-agenda-buffer nil
2091 "Agenda buffer currently being generated.")
2093 (defvar org-agenda-last-prefix-arg nil)
2094 (defvar org-agenda-this-buffer-name nil)
2095 (defvar org-agenda-doing-sticky-redo nil)
2096 (defvar org-agenda-this-buffer-is-sticky nil)
2097 (defvar org-agenda-last-indirect-buffer nil
2098 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2100 (defconst org-agenda-local-vars
2101 '(org-agenda-this-buffer-name
2102 org-agenda-undo-list
2103 org-agenda-pending-undo-list
2104 org-agenda-follow-mode
2105 org-agenda-entry-text-mode
2106 org-agenda-clockreport-mode
2107 org-agenda-show-log
2108 org-agenda-redo-command
2109 org-agenda-query-string
2110 org-agenda-type
2111 org-agenda-bulk-marked-entries
2112 org-agenda-undo-has-started-in
2113 org-agenda-info
2114 org-agenda-pre-window-conf
2115 org-agenda-columns-active
2116 org-agenda-tag-filter
2117 org-agenda-category-filter
2118 org-agenda-top-headline-filter
2119 org-agenda-regexp-filter
2120 org-agenda-effort-filter
2121 org-agenda-markers
2122 org-agenda-last-search-view-search-was-boolean
2123 org-agenda-last-indirect-buffer
2124 org-agenda-filtered-by-category
2125 org-agenda-filter-form
2126 org-agenda-cycle-counter
2127 org-agenda-last-prefix-arg)
2128 "Variables that must be local in agenda buffers to allow multiple buffers.")
2130 (defun org-agenda-mode ()
2131 "Mode for time-sorted view on action items in Org-mode files.
2133 The following commands are available:
2135 \\{org-agenda-mode-map}"
2136 (interactive)
2137 (cond (org-agenda-doing-sticky-redo
2138 ;; Refreshing sticky agenda-buffer
2140 ;; Preserve the value of `org-agenda-local-vars' variables,
2141 ;; while letting `kill-all-local-variables' kill the rest
2142 (let ((save (buffer-local-variables)))
2143 (kill-all-local-variables)
2144 (mapc 'make-local-variable org-agenda-local-vars)
2145 (dolist (elem save)
2146 (let ((var (car elem))
2147 (val (cdr elem)))
2148 (when (and val
2149 (member var org-agenda-local-vars))
2150 (set var val)))))
2151 (setq-local org-agenda-this-buffer-is-sticky t))
2152 (org-agenda-sticky
2153 ;; Creating a sticky Agenda buffer for the first time
2154 (kill-all-local-variables)
2155 (mapc 'make-local-variable org-agenda-local-vars)
2156 (setq-local org-agenda-this-buffer-is-sticky t))
2158 ;; Creating a non-sticky agenda buffer
2159 (kill-all-local-variables)
2160 (setq-local org-agenda-this-buffer-is-sticky nil)))
2161 (setq org-agenda-undo-list nil
2162 org-agenda-pending-undo-list nil
2163 org-agenda-bulk-marked-entries nil)
2164 (setq major-mode 'org-agenda-mode)
2165 ;; Keep global-font-lock-mode from turning on font-lock-mode
2166 (setq-local font-lock-global-modes (list 'not major-mode))
2167 (setq mode-name "Org-Agenda")
2168 (setq indent-tabs-mode nil)
2169 (use-local-map org-agenda-mode-map)
2170 (easy-menu-add org-agenda-menu)
2171 (if org-startup-truncated (setq truncate-lines t))
2172 (setq-local line-move-visual nil)
2173 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2174 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2175 ;; Make sure properties are removed when copying text
2176 (add-hook 'filter-buffer-substring-functions
2177 (lambda (fun start end delete)
2178 (substring-no-properties (funcall fun start end delete)))
2179 nil t)
2180 (unless org-agenda-keep-modes
2181 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2182 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2183 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2184 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2185 (add-to-invisibility-spec '(org-filtered))
2186 (add-to-invisibility-spec '(org-link))
2187 (easy-menu-change
2188 '("Agenda") "Agenda Files"
2189 (append
2190 (list
2191 (vector
2192 (if (get 'org-agenda-files 'org-restrict)
2193 "Restricted to single file"
2194 "Edit File List")
2195 '(org-edit-agenda-file-list)
2196 (not (get 'org-agenda-files 'org-restrict)))
2197 "--")
2198 (mapcar 'org-file-menu-entry (org-agenda-files))))
2199 (org-agenda-set-mode-name)
2200 (apply
2201 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2202 (list 'org-agenda-mode-hook)))
2204 (substitute-key-definition 'undo 'org-agenda-undo
2205 org-agenda-mode-map global-map)
2206 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2207 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2208 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2209 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2210 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2211 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2212 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2213 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2214 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2215 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2216 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2217 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2218 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2219 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2220 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2221 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2222 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2223 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2224 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2225 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2226 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2227 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2228 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2229 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2230 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2231 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2232 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2233 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2234 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2235 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2236 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2237 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2238 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2239 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2240 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2241 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2242 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2243 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2244 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2245 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2246 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2247 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2248 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2249 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2250 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2251 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2252 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2253 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2254 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2255 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2257 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2258 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2259 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2260 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2261 (while l (org-defkey org-agenda-mode-map
2262 (int-to-string (pop l)) 'digit-argument)))
2264 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2265 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2266 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2267 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2268 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2269 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2270 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2271 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2272 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2273 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2274 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2275 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2276 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2277 'org-clock-modify-effort-estimate)
2278 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2279 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2280 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2281 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2282 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2283 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2284 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2285 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2286 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2287 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2288 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2289 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2290 (substitute-key-definition 'next-line 'org-agenda-next-line
2291 org-agenda-mode-map global-map)
2292 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2293 org-agenda-mode-map global-map)
2294 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2295 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2296 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2297 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2298 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2299 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2300 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2301 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2302 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2303 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2304 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2305 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2306 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2307 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2308 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2309 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2310 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2311 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2312 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2313 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2314 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2315 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2316 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2317 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2318 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2319 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2320 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2321 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2322 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2325 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2326 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2327 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2328 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2329 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2330 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2331 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2332 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2333 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2334 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2335 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2336 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2337 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2338 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2339 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2341 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2342 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2344 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2345 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2347 (when org-agenda-mouse-1-follows-link
2348 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2349 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2350 '("Agenda"
2351 ("Agenda Files")
2352 "--"
2353 ("Agenda Dates"
2354 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2355 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2356 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2357 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2358 "--"
2359 ("View"
2360 ["Day View" org-agenda-day-view
2361 :active (org-agenda-check-type nil 'agenda)
2362 :style radio :selected (eq org-agenda-current-span 'day)
2363 :keys "v d (or just d)"]
2364 ["Week View" org-agenda-week-view
2365 :active (org-agenda-check-type nil 'agenda)
2366 :style radio :selected (eq org-agenda-current-span 'week)
2367 :keys "v w"]
2368 ["Fortnight View" org-agenda-fortnight-view
2369 :active (org-agenda-check-type nil 'agenda)
2370 :style radio :selected (eq org-agenda-current-span 'fortnight)
2371 :keys "v f"]
2372 ["Month View" org-agenda-month-view
2373 :active (org-agenda-check-type nil 'agenda)
2374 :style radio :selected (eq org-agenda-current-span 'month)
2375 :keys "v m"]
2376 ["Year View" org-agenda-year-view
2377 :active (org-agenda-check-type nil 'agenda)
2378 :style radio :selected (eq org-agenda-current-span 'year)
2379 :keys "v y"]
2380 "--"
2381 ["Include Diary" org-agenda-toggle-diary
2382 :style toggle :selected org-agenda-include-diary
2383 :active (org-agenda-check-type nil 'agenda)]
2384 ["Include Deadlines" org-agenda-toggle-deadlines
2385 :style toggle :selected org-agenda-include-deadlines
2386 :active (org-agenda-check-type nil 'agenda)]
2387 ["Use Time Grid" org-agenda-toggle-time-grid
2388 :style toggle :selected org-agenda-use-time-grid
2389 :active (org-agenda-check-type nil 'agenda)]
2390 "--"
2391 ["Show clock report" org-agenda-clockreport-mode
2392 :style toggle :selected org-agenda-clockreport-mode
2393 :active (org-agenda-check-type nil 'agenda)]
2394 ["Show some entry text" org-agenda-entry-text-mode
2395 :style toggle :selected org-agenda-entry-text-mode
2396 :active t]
2397 "--"
2398 ["Show Logbook entries" org-agenda-log-mode
2399 :style toggle :selected org-agenda-show-log
2400 :active (org-agenda-check-type nil 'agenda 'timeline)
2401 :keys "v l (or just l)"]
2402 ["Include archived trees" org-agenda-archives-mode
2403 :style toggle :selected org-agenda-archives-mode :active t
2404 :keys "v a"]
2405 ["Include archive files" (org-agenda-archives-mode t)
2406 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2407 :keys "v A"]
2408 "--"
2409 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2410 ["Write view to file" org-agenda-write t]
2411 ["Rebuild buffer" org-agenda-redo t]
2412 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2413 "--"
2414 ["Show original entry" org-agenda-show t]
2415 ["Go To (other window)" org-agenda-goto t]
2416 ["Go To (this window)" org-agenda-switch-to t]
2417 ["Capture with cursor date" org-agenda-capture t]
2418 ["Follow Mode" org-agenda-follow-mode
2419 :style toggle :selected org-agenda-follow-mode :active t]
2420 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2421 "--"
2422 ("TODO"
2423 ["Cycle TODO" org-agenda-todo t]
2424 ["Next TODO set" org-agenda-todo-nextset t]
2425 ["Previous TODO set" org-agenda-todo-previousset t]
2426 ["Add note" org-agenda-add-note t])
2427 ("Archive/Refile/Delete"
2428 ["Archive default" org-agenda-archive-default t]
2429 ["Archive default" org-agenda-archive-default-with-confirmation t]
2430 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2431 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2432 ["Archive subtree" org-agenda-archive t]
2433 "--"
2434 ["Refile" org-agenda-refile t]
2435 "--"
2436 ["Delete subtree" org-agenda-kill t])
2437 ("Bulk action"
2438 ["Mark entry" org-agenda-bulk-mark t]
2439 ["Mark all" org-agenda-bulk-mark-all t]
2440 ["Unmark entry" org-agenda-bulk-unmark t]
2441 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2442 ["Toggle mark" org-agenda-bulk-toggle t]
2443 ["Toggle all" org-agenda-bulk-toggle-all t]
2444 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2445 ["Act on all marked" org-agenda-bulk-action t]
2446 "--"
2447 ("Tags and Properties"
2448 ["Show all Tags" org-agenda-show-tags t]
2449 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2450 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2451 "--"
2452 ["Column View" org-columns t])
2453 ("Deadline/Schedule"
2454 ["Schedule" org-agenda-schedule t]
2455 ["Set Deadline" org-agenda-deadline t]
2456 "--"
2457 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2460 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2461 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2462 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2463 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2464 ("Clock and Effort"
2465 ["Clock in" org-agenda-clock-in t]
2466 ["Clock out" org-agenda-clock-out t]
2467 ["Clock cancel" org-agenda-clock-cancel t]
2468 ["Goto running clock" org-clock-goto t]
2469 "--"
2470 ["Set Effort" org-agenda-set-effort t]
2471 ["Change clocked effort" org-clock-modify-effort-estimate
2472 (org-clock-is-active)])
2473 ("Priority"
2474 ["Set Priority" org-agenda-priority t]
2475 ["Increase Priority" org-agenda-priority-up t]
2476 ["Decrease Priority" org-agenda-priority-down t]
2477 ["Show Priority" org-show-priority t])
2478 ("Calendar/Diary"
2479 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2480 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2481 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2482 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2483 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2484 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2485 "--"
2486 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2487 "--"
2488 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2489 "--"
2490 ("MobileOrg"
2491 ["Push Files and Views" org-mobile-push t]
2492 ["Get Captured and Flagged" org-mobile-pull t]
2493 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2494 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2495 "--"
2496 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2497 "--"
2498 ["Quit" org-agenda-quit t]
2499 ["Exit and Release Buffers" org-agenda-exit t]
2502 ;;; Agenda undo
2504 (defvar org-agenda-allow-remote-undo t
2505 "Non-nil means allow remote undo from the agenda buffer.")
2506 (defvar org-agenda-undo-has-started-in nil
2507 "Buffers that have already seen `undo-start' in the current undo sequence.")
2509 (defun org-agenda-undo ()
2510 "Undo a remote editing step in the agenda.
2511 This undoes changes both in the agenda buffer and in the remote buffer
2512 that have been changed along."
2513 (interactive)
2514 (or org-agenda-allow-remote-undo
2515 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2516 (if (not (eq this-command last-command))
2517 (setq org-agenda-undo-has-started-in nil
2518 org-agenda-pending-undo-list org-agenda-undo-list))
2519 (if (not org-agenda-pending-undo-list)
2520 (user-error "No further undo information"))
2521 (let* ((entry (pop org-agenda-pending-undo-list))
2522 buf line cmd rembuf)
2523 (setq cmd (pop entry) line (pop entry))
2524 (setq rembuf (nth 2 entry))
2525 (org-with-remote-undo rembuf
2526 (while (bufferp (setq buf (pop entry)))
2527 (if (pop entry)
2528 (with-current-buffer buf
2529 (let ((last-undo-buffer buf)
2530 (inhibit-read-only t))
2531 (unless (memq buf org-agenda-undo-has-started-in)
2532 (push buf org-agenda-undo-has-started-in)
2533 (make-local-variable 'pending-undo-list)
2534 (undo-start))
2535 (while (and pending-undo-list
2536 (listp pending-undo-list)
2537 (not (car pending-undo-list)))
2538 (pop pending-undo-list))
2539 (undo-more 1))))))
2540 (org-goto-line line)
2541 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2543 (defun org-verify-change-for-undo (l1 l2)
2544 "Verify that a real change occurred between the undo lists L1 and L2."
2545 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2546 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2547 (not (eq l1 l2)))
2549 ;;; Agenda dispatch
2551 (defvar org-agenda-restrict-begin (make-marker))
2552 (defvar org-agenda-restrict-end (make-marker))
2553 (defvar org-agenda-last-dispatch-buffer nil)
2554 (defvar org-agenda-overriding-restriction nil)
2556 (defcustom org-agenda-custom-commands-contexts nil
2557 "Alist of custom agenda keys and contextual rules.
2559 For example, if you have a custom agenda command \"p\" and you
2560 want this command to be accessible only from plain text files,
2561 use this:
2563 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2565 Here are the available contexts definitions:
2567 in-file: command displayed only in matching files
2568 in-mode: command displayed only in matching modes
2569 not-in-file: command not displayed in matching files
2570 not-in-mode: command not displayed in matching modes
2571 in-buffer: command displayed only in matching buffers
2572 not-in-buffer: command not displayed in matching buffers
2573 [function]: a custom function taking no argument
2575 If you define several checks, the agenda command will be
2576 accessible if there is at least one valid check.
2578 You can also bind a key to another agenda custom command
2579 depending on contextual rules.
2581 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2583 Here it means: in .txt files, use \"p\" as the key for the
2584 agenda command otherwise associated with \"q\". (The command
2585 originally associated with \"q\" is not displayed to avoid
2586 duplicates.)"
2587 :version "24.3"
2588 :group 'org-agenda-custom-commands
2589 :type '(repeat (list :tag "Rule"
2590 (string :tag " Agenda key")
2591 (string :tag "Replace by command")
2592 (repeat :tag "Available when"
2593 (choice
2594 (cons :tag "Condition"
2595 (choice
2596 (const :tag "In file" in-file)
2597 (const :tag "Not in file" not-in-file)
2598 (const :tag "In buffer" in-buffer)
2599 (const :tag "Not in buffer" not-in-buffer)
2600 (const :tag "In mode" in-mode)
2601 (const :tag "Not in mode" not-in-mode))
2602 (regexp))
2603 (function :tag "Custom function"))))))
2605 (defcustom org-agenda-max-entries nil
2606 "Maximum number of entries to display in an agenda.
2607 This can be nil (no limit) or an integer or an alist of agenda
2608 types with an associated number of entries to display in this
2609 type."
2610 :version "24.4"
2611 :package-version '(Org . "8.0")
2612 :group 'org-agenda-custom-commands
2613 :type '(choice (symbol :tag "No limit" nil)
2614 (integer :tag "Max number of entries")
2615 (repeat
2616 (cons (choice :tag "Agenda type"
2617 (const agenda)
2618 (const todo)
2619 (const tags)
2620 (const search)
2621 (const timeline))
2622 (integer :tag "Max number of entries")))))
2624 (defcustom org-agenda-max-todos nil
2625 "Maximum number of TODOs to display in an agenda.
2626 This can be nil (no limit) or an integer or an alist of agenda
2627 types with an associated number of entries to display in this
2628 type."
2629 :version "24.4"
2630 :package-version '(Org . "8.0")
2631 :group 'org-agenda-custom-commands
2632 :type '(choice (symbol :tag "No limit" nil)
2633 (integer :tag "Max number of TODOs")
2634 (repeat
2635 (cons (choice :tag "Agenda type"
2636 (const agenda)
2637 (const todo)
2638 (const tags)
2639 (const search)
2640 (const timeline))
2641 (integer :tag "Max number of TODOs")))))
2643 (defcustom org-agenda-max-tags nil
2644 "Maximum number of tagged entries to display in an agenda.
2645 This can be nil (no limit) or an integer or an alist of agenda
2646 types with an associated number of entries to display in this
2647 type."
2648 :version "24.4"
2649 :package-version '(Org . "8.0")
2650 :group 'org-agenda-custom-commands
2651 :type '(choice (symbol :tag "No limit" nil)
2652 (integer :tag "Max number of tagged entries")
2653 (repeat
2654 (cons (choice :tag "Agenda type"
2655 (const agenda)
2656 (const todo)
2657 (const tags)
2658 (const search)
2659 (const timeline))
2660 (integer :tag "Max number of tagged entries")))))
2662 (defcustom org-agenda-max-effort nil
2663 "Maximum cumulated effort duration for the agenda.
2664 This can be nil (no limit) or a number of minutes (as an integer)
2665 or an alist of agenda types with an associated number of minutes
2666 to limit entries to in this type."
2667 :version "24.4"
2668 :package-version '(Org . "8.0")
2669 :group 'org-agenda-custom-commands
2670 :type '(choice (symbol :tag "No limit" nil)
2671 (integer :tag "Max number of minutes")
2672 (repeat
2673 (cons (choice :tag "Agenda type"
2674 (const agenda)
2675 (const todo)
2676 (const tags)
2677 (const search)
2678 (const timeline))
2679 (integer :tag "Max number of minutes")))))
2681 (defvar org-keys nil)
2682 (defvar org-match nil)
2683 ;;;###autoload
2684 (defun org-agenda (&optional arg org-keys restriction)
2685 "Dispatch agenda commands to collect entries to the agenda buffer.
2686 Prompts for a command to execute. Any prefix arg will be passed
2687 on to the selected command. The default selections are:
2689 a Call `org-agenda-list' to display the agenda for current day or week.
2690 t Call `org-todo-list' to display the global todo list.
2691 T Call `org-todo-list' to display the global todo list, select only
2692 entries with a specific TODO keyword (the user gets a prompt).
2693 m Call `org-tags-view' to display headlines with tags matching
2694 a condition (the user is prompted for the condition).
2695 M Like `m', but select only TODO entries, no ordinary headlines.
2696 L Create a timeline for the current buffer.
2697 e Export views to associated files.
2698 s Search entries for keywords.
2699 S Search entries for keywords, only with TODO keywords.
2700 / Multi occur across all agenda files and also files listed
2701 in `org-agenda-text-search-extra-files'.
2702 < Restrict agenda commands to buffer, subtree, or region.
2703 Press several times to get the desired effect.
2704 > Remove a previous restriction.
2705 # List \"stuck\" projects.
2706 ! Configure what \"stuck\" means.
2707 C Configure custom agenda commands.
2709 More commands can be added by configuring the variable
2710 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2711 searches can be pre-defined in this way.
2713 If the current buffer is in Org-mode and visiting a file, you can also
2714 first press `<' once to indicate that the agenda should be temporarily
2715 \(until the next use of \\[org-agenda]) restricted to the current file.
2716 Pressing `<' twice means to restrict to the current subtree or region
2717 \(if active)."
2718 (interactive "P")
2719 (catch 'exit
2720 (let* ((prefix-descriptions nil)
2721 (org-agenda-buffer-name org-agenda-buffer-name)
2722 (org-agenda-window-setup (if (equal (buffer-name)
2723 org-agenda-buffer-name)
2724 'current-window
2725 org-agenda-window-setup))
2726 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2727 (org-agenda-custom-commands
2728 ;; normalize different versions
2729 (delq nil
2730 (mapcar
2731 (lambda (x)
2732 (cond ((stringp (cdr x))
2733 (push x prefix-descriptions)
2734 nil)
2735 ((stringp (nth 1 x)) x)
2736 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2737 (t (cons (car x) (cons "" (cdr x))))))
2738 org-agenda-custom-commands)))
2739 (org-agenda-custom-commands
2740 (org-contextualize-keys
2741 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2742 (buf (current-buffer))
2743 (bfn (buffer-file-name (buffer-base-buffer)))
2744 entry key type org-match lprops ans)
2745 ;; Turn off restriction unless there is an overriding one,
2746 (unless org-agenda-overriding-restriction
2747 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2748 ;; There is a request to keep the file list in place
2749 (put 'org-agenda-files 'org-restrict nil))
2750 (setq org-agenda-restrict nil)
2751 (move-marker org-agenda-restrict-begin nil)
2752 (move-marker org-agenda-restrict-end nil))
2753 ;; Delete old local properties
2754 (put 'org-agenda-redo-command 'org-lprops nil)
2755 ;; Delete previously set last-arguments
2756 (put 'org-agenda-redo-command 'last-args nil)
2757 ;; Remember where this call originated
2758 (setq org-agenda-last-dispatch-buffer (current-buffer))
2759 (unless org-keys
2760 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2761 org-keys (car ans)
2762 restriction (cdr ans)))
2763 ;; If we have sticky agenda buffers, set a name for the buffer,
2764 ;; depending on the invoking keys. The user may still set this
2765 ;; as a command option, which will overwrite what we do here.
2766 (if org-agenda-sticky
2767 (setq org-agenda-buffer-name
2768 (format "*Org Agenda(%s)*" org-keys)))
2769 ;; Establish the restriction, if any
2770 (when (and (not org-agenda-overriding-restriction) restriction)
2771 (put 'org-agenda-files 'org-restrict (list bfn))
2772 (cond
2773 ((eq restriction 'region)
2774 (setq org-agenda-restrict (current-buffer))
2775 (move-marker org-agenda-restrict-begin (region-beginning))
2776 (move-marker org-agenda-restrict-end (region-end)))
2777 ((eq restriction 'subtree)
2778 (save-excursion
2779 (setq org-agenda-restrict (current-buffer))
2780 (org-back-to-heading t)
2781 (move-marker org-agenda-restrict-begin (point))
2782 (move-marker org-agenda-restrict-end
2783 (progn (org-end-of-subtree t)))))))
2785 ;; For example the todo list should not need it (but does...)
2786 (cond
2787 ((setq entry (assoc org-keys org-agenda-custom-commands))
2788 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2789 (progn
2790 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2791 lprops (nth 4 entry))
2792 (if org-agenda-sticky
2793 (setq org-agenda-buffer-name
2794 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2795 (format "*Org Agenda(%s)*" org-keys))))
2796 (put 'org-agenda-redo-command 'org-lprops lprops)
2797 (cond
2798 ((eq type 'agenda)
2799 (org-let lprops '(org-agenda-list current-prefix-arg)))
2800 ((eq type 'agenda*)
2801 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2802 ((eq type 'alltodo)
2803 (org-let lprops '(org-todo-list current-prefix-arg)))
2804 ((eq type 'search)
2805 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2806 ((eq type 'stuck)
2807 (org-let lprops '(org-agenda-list-stuck-projects
2808 current-prefix-arg)))
2809 ((eq type 'tags)
2810 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2811 ((eq type 'tags-todo)
2812 (org-let lprops '(org-tags-view '(4) org-match)))
2813 ((eq type 'todo)
2814 (org-let lprops '(org-todo-list org-match)))
2815 ((eq type 'tags-tree)
2816 (org-check-for-org-mode)
2817 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2818 ((eq type 'todo-tree)
2819 (org-check-for-org-mode)
2820 (org-let lprops
2821 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2822 (regexp-quote org-match) "\\>"))))
2823 ((eq type 'occur-tree)
2824 (org-check-for-org-mode)
2825 (org-let lprops '(org-occur org-match)))
2826 ((functionp type)
2827 (org-let lprops '(funcall type org-match)))
2828 ((fboundp type)
2829 (org-let lprops '(funcall type org-match)))
2830 (t (user-error "Invalid custom agenda command type %s" type))))
2831 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2832 ((equal org-keys "C")
2833 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2834 (customize-variable 'org-agenda-custom-commands))
2835 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2836 ((equal org-keys "s") (call-interactively 'org-search-view))
2837 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2838 ((equal org-keys "t") (call-interactively 'org-todo-list))
2839 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2840 ((equal org-keys "m") (call-interactively 'org-tags-view))
2841 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2842 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2843 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2844 (add-hook
2845 'post-command-hook
2846 (lambda ()
2847 (unless (current-message)
2848 (let* ((m (org-agenda-get-any-marker))
2849 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2850 (when note
2851 (message (concat
2852 "FLAGGING-NOTE ([?] for more info): "
2853 (org-add-props
2854 (replace-regexp-in-string
2855 "\\\\n" "//"
2856 (copy-sequence note))
2857 nil 'face 'org-warning)))))))
2858 t t))
2859 ((equal org-keys "L")
2860 (unless (derived-mode-p 'org-mode)
2861 (user-error "This is not an Org-mode file"))
2862 (unless restriction
2863 (put 'org-agenda-files 'org-restrict (list bfn))
2864 (org-call-with-arg 'org-timeline arg)))
2865 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2866 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2867 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2868 (t (user-error "Invalid agenda key"))))))
2870 (defvar org-agenda-multi)
2872 (defun org-agenda-append-agenda ()
2873 "Append another agenda view to the current one.
2874 This function allows interactive building of block agendas.
2875 Agenda views are separated by `org-agenda-block-separator'."
2876 (interactive)
2877 (unless (derived-mode-p 'org-agenda-mode)
2878 (user-error "Can only append from within agenda buffer"))
2879 (let ((org-agenda-multi t))
2880 (org-agenda)
2881 (widen)
2882 (org-agenda-finalize)
2883 (setq buffer-read-only t)
2884 (org-agenda-fit-window-to-buffer)))
2886 (defun org-agenda-normalize-custom-commands (cmds)
2887 "Normalize custom commands CMDS."
2888 (delq nil
2889 (mapcar
2890 (lambda (x)
2891 (cond ((stringp (cdr x)) nil)
2892 ((stringp (nth 1 x)) x)
2893 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2894 (t (cons (car x) (cons "" (cdr x))))))
2895 cmds)))
2897 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2898 "The user interface for selecting an agenda command."
2899 (catch 'exit
2900 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2901 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2902 (region-p (org-region-active-p))
2903 (custom org-agenda-custom-commands)
2904 (selstring "")
2905 restriction second-time
2906 c entry key type match prefixes rmheader header-end custom1 desc
2907 line lines left right n n1)
2908 (save-window-excursion
2909 (delete-other-windows)
2910 (org-switch-to-buffer-other-window " *Agenda Commands*")
2911 (erase-buffer)
2912 (insert (eval-when-compile
2913 (let ((header
2914 "Press key for an agenda command: < Buffer, subtree/region restriction
2915 -------------------------------- > Remove restriction
2916 a Agenda for current week or day e Export agenda views
2917 t List of all TODO entries T Entries with special TODO kwd
2918 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2919 s Search for keywords S Like s, but only TODO entries
2920 L Timeline for current buffer # List stuck projects (!=configure)
2921 / Multi-occur C Configure custom agenda commands
2922 ? Find :FLAGGED: entries * Toggle sticky agenda views
2924 (start 0))
2925 (while (string-match
2926 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2927 header start)
2928 (setq start (match-end 0))
2929 (add-text-properties (match-beginning 2) (match-end 2)
2930 '(face bold) header))
2931 header)))
2932 (setq header-end (point-marker))
2933 (while t
2934 (setq custom1 custom)
2935 (when (eq rmheader t)
2936 (org-goto-line 1)
2937 (re-search-forward ":" nil t)
2938 (delete-region (match-end 0) (point-at-eol))
2939 (forward-char 1)
2940 (looking-at "-+")
2941 (delete-region (match-end 0) (point-at-eol))
2942 (move-marker header-end (match-end 0)))
2943 (goto-char header-end)
2944 (delete-region (point) (point-max))
2946 ;; Produce all the lines that describe custom commands and prefixes
2947 (setq lines nil)
2948 (while (setq entry (pop custom1))
2949 (setq key (car entry) desc (nth 1 entry)
2950 type (nth 2 entry)
2951 match (nth 3 entry))
2952 (if (> (length key) 1)
2953 (add-to-list 'prefixes (string-to-char key))
2954 (setq line
2955 (format
2956 "%-4s%-14s"
2957 (org-add-props (copy-sequence key)
2958 '(face bold))
2959 (cond
2960 ((string-match "\\S-" desc) desc)
2961 ((eq type 'agenda) "Agenda for current week or day")
2962 ((eq type 'agenda*) "Appointments for current week or day")
2963 ((eq type 'alltodo) "List of all TODO entries")
2964 ((eq type 'search) "Word search")
2965 ((eq type 'stuck) "List of stuck projects")
2966 ((eq type 'todo) "TODO keyword")
2967 ((eq type 'tags) "Tags query")
2968 ((eq type 'tags-todo) "Tags (TODO)")
2969 ((eq type 'tags-tree) "Tags tree")
2970 ((eq type 'todo-tree) "TODO kwd tree")
2971 ((eq type 'occur-tree) "Occur tree")
2972 ((functionp type) (if (symbolp type)
2973 (symbol-name type)
2974 "Lambda expression"))
2975 (t "???"))))
2976 (if org-agenda-menu-show-matcher
2977 (setq line
2978 (concat line ": "
2979 (cond
2980 ((stringp match)
2981 (setq match (copy-sequence match))
2982 (org-add-props match nil 'face 'org-warning))
2983 ((listp type)
2984 (format "set of %d commands" (length type))))))
2985 (if (org-string-nw-p match)
2986 (add-text-properties
2987 0 (length line) (list 'help-echo
2988 (concat "Matcher: " match)) line)))
2989 (push line lines)))
2990 (setq lines (nreverse lines))
2991 (when prefixes
2992 (mapc (lambda (x)
2993 (push
2994 (format "%s %s"
2995 (org-add-props (char-to-string x)
2996 nil 'face 'bold)
2997 (or (cdr (assoc (concat selstring
2998 (char-to-string x))
2999 prefix-descriptions))
3000 "Prefix key"))
3001 lines))
3002 prefixes))
3004 ;; Check if we should display in two columns
3005 (if org-agenda-menu-two-columns
3006 (progn
3007 (setq n (length lines)
3008 n1 (+ (/ n 2) (mod n 2))
3009 right (nthcdr n1 lines)
3010 left (copy-sequence lines))
3011 (setcdr (nthcdr (1- n1) left) nil))
3012 (setq left lines right nil))
3013 (while left
3014 (insert "\n" (pop left))
3015 (when right
3016 (if (< (current-column) 40)
3017 (move-to-column 40 t)
3018 (insert " "))
3019 (insert (pop right))))
3021 ;; Make the window the right size
3022 (goto-char (point-min))
3023 (if second-time
3024 (if (not (pos-visible-in-window-p (point-max)))
3025 (org-fit-window-to-buffer))
3026 (setq second-time t)
3027 (org-fit-window-to-buffer))
3029 ;; Ask for selection
3030 (message "Press key for agenda command%s:"
3031 (if (or restrict-ok org-agenda-overriding-restriction)
3032 (if org-agenda-overriding-restriction
3033 " (restriction lock active)"
3034 (if restriction
3035 (format " (restricted to %s)" restriction)
3036 " (unrestricted)"))
3037 ""))
3038 (setq c (read-char-exclusive))
3039 (message "")
3040 (cond
3041 ((assoc (char-to-string c) custom)
3042 (setq selstring (concat selstring (char-to-string c)))
3043 (throw 'exit (cons selstring restriction)))
3044 ((memq c prefixes)
3045 (setq selstring (concat selstring (char-to-string c))
3046 prefixes nil
3047 rmheader (or rmheader t)
3048 custom (delq nil (mapcar
3049 (lambda (x)
3050 (if (or (= (length (car x)) 1)
3051 (/= (string-to-char (car x)) c))
3053 (cons (substring (car x) 1) (cdr x))))
3054 custom))))
3055 ((eq c ?*)
3056 (call-interactively 'org-toggle-sticky-agenda)
3057 (sit-for 2))
3058 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3059 (message "Restriction is only possible in Org-mode buffers")
3060 (ding) (sit-for 1))
3061 ((eq c ?1)
3062 (org-agenda-remove-restriction-lock 'noupdate)
3063 (setq restriction 'buffer))
3064 ((eq c ?0)
3065 (org-agenda-remove-restriction-lock 'noupdate)
3066 (setq restriction (if region-p 'region 'subtree)))
3067 ((eq c ?<)
3068 (org-agenda-remove-restriction-lock 'noupdate)
3069 (setq restriction
3070 (cond
3071 ((eq restriction 'buffer)
3072 (if region-p 'region 'subtree))
3073 ((memq restriction '(subtree region))
3074 nil)
3075 (t 'buffer))))
3076 ((eq c ?>)
3077 (org-agenda-remove-restriction-lock 'noupdate)
3078 (setq restriction nil))
3079 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3080 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3081 ((and (> (length selstring) 0) (eq c ?\d))
3082 (delete-window)
3083 (org-agenda-get-restriction-and-command prefix-descriptions))
3085 ((equal c ?q) (error "Abort"))
3086 (t (user-error "Invalid key %c" c))))))))
3088 (defun org-agenda-fit-window-to-buffer ()
3089 "Fit the window to the buffer size."
3090 (and (memq org-agenda-window-setup '(reorganize-frame))
3091 (fboundp 'fit-window-to-buffer)
3092 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3093 (= (car org-agenda-window-frame-fractions) 1.0))
3094 (delete-other-windows)
3095 (org-fit-window-to-buffer
3097 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3098 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3100 (defvar org-cmd nil)
3101 (defvar org-agenda-overriding-cmd nil)
3102 (defvar org-agenda-overriding-arguments nil)
3103 (defvar org-agenda-overriding-cmd-arguments nil)
3104 (defun org-agenda-run-series (name series)
3105 "Run agenda NAME as a SERIES of agenda commands."
3106 (org-let (nth 1 series) '(org-agenda-prepare name))
3107 ;; We need to reset agenda markers here, because when constructing a
3108 ;; block agenda, the individual blocks do not do that.
3109 (org-agenda-reset-markers)
3110 (let* ((org-agenda-multi t)
3111 (redo (list 'org-agenda-run-series name (list 'quote series)))
3112 (cmds (car series))
3113 (gprops (nth 1 series))
3114 match ;; The byte compiler incorrectly complains about this. Keep it!
3115 org-cmd type lprops)
3116 (while (setq org-cmd (pop cmds))
3117 (setq type (car org-cmd))
3118 (setq match (eval (nth 1 org-cmd)))
3119 (setq lprops (nth 2 org-cmd))
3120 (let ((org-agenda-overriding-arguments
3121 (if (eq org-agenda-overriding-cmd org-cmd)
3122 (or org-agenda-overriding-arguments
3123 org-agenda-overriding-cmd-arguments))))
3124 (cond
3125 ((eq type 'agenda)
3126 (org-let2 gprops lprops
3127 '(call-interactively 'org-agenda-list)))
3128 ((eq type 'agenda*)
3129 (org-let2 gprops lprops
3130 '(funcall 'org-agenda-list nil nil t)))
3131 ((eq type 'alltodo)
3132 (org-let2 gprops lprops
3133 '(call-interactively 'org-todo-list)))
3134 ((eq type 'search)
3135 (org-let2 gprops lprops
3136 '(org-search-view current-prefix-arg match nil)))
3137 ((eq type 'stuck)
3138 (org-let2 gprops lprops
3139 '(call-interactively 'org-agenda-list-stuck-projects)))
3140 ((eq type 'tags)
3141 (org-let2 gprops lprops
3142 '(org-tags-view current-prefix-arg match)))
3143 ((eq type 'tags-todo)
3144 (org-let2 gprops lprops
3145 '(org-tags-view '(4) match)))
3146 ((eq type 'todo)
3147 (org-let2 gprops lprops
3148 '(org-todo-list match)))
3149 ((fboundp type)
3150 (org-let2 gprops lprops
3151 '(funcall type match)))
3152 (t (error "Invalid type in command series")))))
3153 (widen)
3154 (let ((inhibit-read-only t))
3155 (add-text-properties (point-min) (point-max)
3156 `(org-series t org-series-redo-cmd ,redo)))
3157 (setq org-agenda-redo-command redo)
3158 (goto-char (point-min)))
3159 (org-agenda-fit-window-to-buffer)
3160 (org-let (nth 1 series) '(org-agenda-finalize)))
3162 ;;;###autoload
3163 (defmacro org-batch-agenda (cmd-key &rest parameters)
3164 "Run an agenda command in batch mode and send the result to STDOUT.
3165 If CMD-KEY is a string of length 1, it is used as a key in
3166 `org-agenda-custom-commands' and triggers this command. If it is a
3167 longer string it is used as a tags/todo match string.
3168 Parameters are alternating variable names and values that will be bound
3169 before running the agenda command."
3170 (org-eval-in-environment (org-make-parameter-alist parameters)
3171 (let (org-agenda-sticky)
3172 (if (> (length cmd-key) 1)
3173 (org-tags-view nil cmd-key)
3174 (org-agenda nil cmd-key))))
3175 (set-buffer org-agenda-buffer-name)
3176 (princ (buffer-string)))
3178 (defvar org-agenda-info nil)
3180 ;;;###autoload
3181 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3182 "Run an agenda command in batch mode and send the result to STDOUT.
3183 If CMD-KEY is a string of length 1, it is used as a key in
3184 `org-agenda-custom-commands' and triggers this command. If it is a
3185 longer string it is used as a tags/todo match string.
3186 Parameters are alternating variable names and values that will be bound
3187 before running the agenda command.
3189 The output gives a line for each selected agenda item. Each
3190 item is a list of comma-separated values, like this:
3192 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3194 category The category of the item
3195 head The headline, without TODO kwd, TAGS and PRIORITY
3196 type The type of the agenda entry, can be
3197 todo selected in TODO match
3198 tagsmatch selected in tags match
3199 diary imported from diary
3200 deadline a deadline on given date
3201 scheduled scheduled on given date
3202 timestamp entry has timestamp on given date
3203 closed entry was closed on given date
3204 upcoming-deadline warning about deadline
3205 past-scheduled forwarded scheduled item
3206 block entry has date block including g. date
3207 todo The todo keyword, if any
3208 tags All tags including inherited ones, separated by colons
3209 date The relevant date, like 2007-2-14
3210 time The time, like 15:00-16:50
3211 extra Sting with extra planning info
3212 priority-l The priority letter if any was given
3213 priority-n The computed numerical priority
3214 agenda-day The day in the agenda where this is listed"
3215 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3216 (org-make-parameter-alist parameters))
3217 (if (> (length cmd-key) 2)
3218 (org-tags-view nil cmd-key)
3219 (org-agenda nil cmd-key)))
3220 (set-buffer org-agenda-buffer-name)
3221 (let* ((lines (org-split-string (buffer-string) "\n"))
3222 line)
3223 (while (setq line (pop lines))
3224 (catch 'next
3225 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3226 (setq org-agenda-info
3227 (org-fix-agenda-info (text-properties-at 0 line)))
3228 (princ
3229 (mapconcat 'org-agenda-export-csv-mapper
3230 '(org-category txt type todo tags date time extra
3231 priority-letter priority agenda-day)
3232 ","))
3233 (princ "\n")))))
3235 (defun org-fix-agenda-info (props)
3236 "Make sure all properties on an agenda item have a canonical form.
3237 This ensures the export commands can easily use it."
3238 (let (tmp re)
3239 (when (setq tmp (plist-get props 'tags))
3240 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3241 (when (setq tmp (plist-get props 'date))
3242 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3243 (let ((calendar-date-display-form '(year "-" month "-" day)))
3244 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3246 (setq tmp (calendar-date-string tmp)))
3247 (setq props (plist-put props 'date tmp)))
3248 (when (setq tmp (plist-get props 'day))
3249 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3250 (let ((calendar-date-display-form '(year "-" month "-" day)))
3251 (setq tmp (calendar-date-string tmp)))
3252 (setq props (plist-put props 'day tmp))
3253 (setq props (plist-put props 'agenda-day tmp)))
3254 (when (setq tmp (plist-get props 'txt))
3255 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3256 (plist-put props 'priority-letter (match-string 1 tmp))
3257 (setq tmp (replace-match "" t t tmp)))
3258 (when (and (setq re (plist-get props 'org-todo-regexp))
3259 (setq re (concat "\\`\\.*" re " ?"))
3260 (string-match re tmp))
3261 (plist-put props 'todo (match-string 1 tmp))
3262 (setq tmp (replace-match "" t t tmp)))
3263 (plist-put props 'txt tmp)))
3264 props)
3266 (defun org-agenda-export-csv-mapper (prop)
3267 (let ((res (plist-get org-agenda-info prop)))
3268 (setq res
3269 (cond
3270 ((not res) "")
3271 ((stringp res) res)
3272 (t (prin1-to-string res))))
3273 (while (string-match "," res)
3274 (setq res (replace-match ";" t t res)))
3275 (org-trim res)))
3277 ;;;###autoload
3278 (defun org-store-agenda-views (&rest parameters)
3279 "Store agenda views."
3280 (interactive)
3281 (eval (list 'org-batch-store-agenda-views)))
3283 ;;;###autoload
3284 (defmacro org-batch-store-agenda-views (&rest parameters)
3285 "Run all custom agenda commands that have a file argument."
3286 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3287 (pop-up-frames nil)
3288 (dir default-directory)
3289 (pars (org-make-parameter-alist parameters))
3290 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3291 (save-window-excursion
3292 (while cmds
3293 (setq cmd (pop cmds)
3294 thiscmdkey (car cmd)
3295 thiscmdcmd (cdr cmd)
3296 match (nth 2 thiscmdcmd)
3297 bufname (if org-agenda-sticky
3298 (or (and (stringp match)
3299 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3300 (format "*Org Agenda(%s)*" thiscmdkey))
3301 org-agenda-buffer-name)
3302 cmd-or-set (nth 2 cmd)
3303 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3304 files (nth (if (listp cmd-or-set) 4 5) cmd))
3305 (if (stringp files) (setq files (list files)))
3306 (when files
3307 (org-eval-in-environment (append org-agenda-exporter-settings
3308 opts pars)
3309 (org-agenda nil thiscmdkey))
3310 (set-buffer bufname)
3311 (while files
3312 (org-eval-in-environment (append org-agenda-exporter-settings
3313 opts pars)
3314 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3315 (and (get-buffer bufname)
3316 (kill-buffer bufname)))))))
3318 (defvar org-agenda-current-span nil
3319 "The current span used in the agenda view.") ; local variable in the agenda buffer
3320 (defun org-agenda-mark-header-line (pos)
3321 "Mark the line at POS as an agenda structure header."
3322 (save-excursion
3323 (goto-char pos)
3324 (put-text-property (point-at-bol) (point-at-eol)
3325 'org-agenda-structural-header t)
3326 (when org-agenda-title-append
3327 (put-text-property (point-at-bol) (point-at-eol)
3328 'org-agenda-title-append org-agenda-title-append))))
3330 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3331 (defvar org-agenda-write-buffer-name "Agenda View")
3332 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3333 "Write the current buffer (an agenda view) as a file.
3335 Depending on the extension of the file name, plain text (.txt),
3336 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3337 If the extension is .ics, translate visible agenda into iCalendar
3338 format. If the extension is .org, collect all subtrees
3339 corresponding to the agenda entries and add them in an .org file.
3341 With prefix argument OPEN, open the new file immediately. If
3342 NOSETTINGS is given, do not scope the settings of
3343 `org-agenda-exporter-settings' into the export commands. This is
3344 used when the settings have already been scoped and we do not
3345 wish to overrule other, higher priority settings. If
3346 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3347 the agenda to write."
3348 (interactive "FWrite agenda to file: \nP")
3349 (if (or (not (file-writable-p file))
3350 (and (file-exists-p file)
3351 (if (org-called-interactively-p 'any)
3352 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3353 (user-error "Cannot write agenda to file %s" file))
3354 (org-let (if nosettings nil org-agenda-exporter-settings)
3355 '(save-excursion
3356 (save-window-excursion
3357 (let ((bs (copy-sequence (buffer-string))) beg content)
3358 (with-temp-buffer
3359 (rename-buffer org-agenda-write-buffer-name t)
3360 (set-buffer-modified-p nil)
3361 (insert bs)
3362 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3363 (run-hooks 'org-agenda-before-write-hook)
3364 (cond
3365 ((org-bound-and-true-p org-mobile-creating-agendas)
3366 (org-mobile-write-agenda-for-mobile file))
3367 ((string-match "\\.org\\'" file)
3368 (let (content p m message-log-max)
3369 (goto-char (point-min))
3370 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3371 (goto-char p)
3372 (setq m (get-text-property (point) 'org-hd-marker))
3373 (when m
3374 (push (save-excursion
3375 (set-buffer (marker-buffer m))
3376 (goto-char m)
3377 (org-copy-subtree 1 nil t t)
3378 org-subtree-clip)
3379 content)))
3380 (find-file file)
3381 (erase-buffer)
3382 (dolist (s content) (org-paste-subtree 1 s))
3383 (write-file file)
3384 (kill-buffer (current-buffer))
3385 (message "Org file written to %s" file)))
3386 ((string-match "\\.html?\\'" file)
3387 (require 'htmlize)
3388 (set-buffer (htmlize-buffer (current-buffer)))
3389 (when org-agenda-export-html-style
3390 ;; replace <style> section with org-agenda-export-html-style
3391 (goto-char (point-min))
3392 (kill-region (- (search-forward "<style") 6)
3393 (search-forward "</style>"))
3394 (insert org-agenda-export-html-style))
3395 (write-file file)
3396 (kill-buffer (current-buffer))
3397 (message "HTML written to %s" file))
3398 ((string-match "\\.ps\\'" file)
3399 (require 'ps-print)
3400 (ps-print-buffer-with-faces file)
3401 (message "Postscript written to %s" file))
3402 ((string-match "\\.pdf\\'" file)
3403 (require 'ps-print)
3404 (ps-print-buffer-with-faces
3405 (concat (file-name-sans-extension file) ".ps"))
3406 (call-process "ps2pdf" nil nil nil
3407 (expand-file-name
3408 (concat (file-name-sans-extension file) ".ps"))
3409 (expand-file-name file))
3410 (delete-file (concat (file-name-sans-extension file) ".ps"))
3411 (message "PDF written to %s" file))
3412 ((string-match "\\.ics\\'" file)
3413 (require 'ox-icalendar)
3414 (org-icalendar-export-current-agenda (expand-file-name file)))
3416 (let ((bs (buffer-string)))
3417 (find-file file)
3418 (erase-buffer)
3419 (insert bs)
3420 (save-buffer 0)
3421 (kill-buffer (current-buffer))
3422 (message "Plain text written to %s" file))))))))
3423 (set-buffer (or agenda-bufname
3424 (and (org-called-interactively-p 'any) (buffer-name))
3425 org-agenda-buffer-name)))
3426 (when open (org-open-file file)))
3428 (defun org-agenda-remove-marked-text (property &optional value)
3429 "Delete all text marked with VALUE of PROPERTY.
3430 VALUE defaults to t."
3431 (let (beg)
3432 (setq value (or value t))
3433 (while (setq beg (text-property-any (point-min) (point-max)
3434 property value))
3435 (delete-region
3436 beg (or (next-single-property-change beg property)
3437 (point-max))))))
3439 (defun org-agenda-add-entry-text ()
3440 "Add entry text to agenda lines.
3441 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3442 entry text following headings shown in the agenda.
3443 Drawers will be excluded, also the line with scheduling/deadline info."
3444 (when (and (> org-agenda-add-entry-text-maxlines 0)
3445 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3446 (let (m txt)
3447 (goto-char (point-min))
3448 (while (not (eobp))
3449 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3450 (beginning-of-line 2)
3451 (setq txt (org-agenda-get-some-entry-text
3452 m org-agenda-add-entry-text-maxlines " > "))
3453 (end-of-line 1)
3454 (if (string-match "\\S-" txt)
3455 (insert "\n" txt)
3456 (or (eobp) (forward-char 1))))))))
3458 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3459 &rest keep)
3460 "Extract entry text from MARKER, at most N-LINES lines.
3461 This will ignore drawers etc, just get the text.
3462 If INDENT is given, prefix every line with this string. If KEEP is
3463 given, it is a list of symbols, defining stuff that should not be
3464 removed from the entry content. Currently only `planning' is allowed here."
3465 (let (txt drawer-re kwd-time-re ind)
3466 (save-excursion
3467 (with-current-buffer (marker-buffer marker)
3468 (if (not (derived-mode-p 'org-mode))
3469 (setq txt "")
3470 (save-excursion
3471 (save-restriction
3472 (widen)
3473 (goto-char marker)
3474 (end-of-line 1)
3475 (setq txt (buffer-substring
3476 (min (1+ (point)) (point-max))
3477 (progn (outline-next-heading) (point)))
3478 drawer-re org-drawer-regexp
3479 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3480 ".*\n?"))
3481 (with-temp-buffer
3482 (insert txt)
3483 (when org-agenda-add-entry-text-descriptive-links
3484 (goto-char (point-min))
3485 (while (org-activate-bracket-links (point-max))
3486 (add-text-properties (match-beginning 0) (match-end 0)
3487 '(face org-link))))
3488 (goto-char (point-min))
3489 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3490 (set-text-properties (match-beginning 0) (match-end 0)
3491 nil))
3492 (goto-char (point-min))
3493 (while (re-search-forward drawer-re nil t)
3494 (delete-region
3495 (match-beginning 0)
3496 (progn (re-search-forward
3497 "^[ \t]*:END:.*\n?" nil 'move)
3498 (point))))
3499 (unless (member 'planning keep)
3500 (goto-char (point-min))
3501 (while (re-search-forward kwd-time-re nil t)
3502 (replace-match "")))
3503 (goto-char (point-min))
3504 (when org-agenda-entry-text-exclude-regexps
3505 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3506 (while (setq re (pop re-list))
3507 (goto-char (point-min))
3508 (while (re-search-forward re nil t)
3509 (replace-match "")))))
3510 (goto-char (point-max))
3511 (skip-chars-backward " \t\n")
3512 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3514 ;; find and remove min common indentation
3515 (goto-char (point-min))
3516 (untabify (point-min) (point-max))
3517 (setq ind (org-get-indentation))
3518 (while (not (eobp))
3519 (unless (looking-at "[ \t]*$")
3520 (setq ind (min ind (org-get-indentation))))
3521 (beginning-of-line 2))
3522 (goto-char (point-min))
3523 (while (not (eobp))
3524 (unless (looking-at "[ \t]*$")
3525 (move-to-column ind)
3526 (delete-region (point-at-bol) (point)))
3527 (beginning-of-line 2))
3529 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3531 (goto-char (point-min))
3532 (when indent
3533 (while (and (not (eobp)) (re-search-forward "^" nil t))
3534 (replace-match indent t t)))
3535 (goto-char (point-min))
3536 (while (looking-at "[ \t]*\n") (replace-match ""))
3537 (goto-char (point-max))
3538 (when (> (org-current-line)
3539 n-lines)
3540 (org-goto-line (1+ n-lines))
3541 (backward-char 1))
3542 (setq txt (buffer-substring (point-min) (point)))))))))
3543 txt))
3545 (defun org-check-for-org-mode ()
3546 "Make sure current buffer is in org-mode. Error if not."
3547 (or (derived-mode-p 'org-mode)
3548 (error "Cannot execute org-mode agenda command on buffer in %s"
3549 major-mode)))
3551 ;;; Agenda prepare and finalize
3553 (defvar org-agenda-multi nil) ; dynamically scoped
3554 (defvar org-agenda-pre-window-conf nil)
3555 (defvar org-agenda-columns-active nil)
3556 (defvar org-agenda-name nil)
3557 (defvar org-agenda-tag-filter nil)
3558 (defvar org-agenda-category-filter nil)
3559 (defvar org-agenda-regexp-filter nil)
3560 (defvar org-agenda-effort-filter nil)
3561 (defvar org-agenda-top-headline-filter nil)
3562 (defvar org-agenda-tag-filter-preset nil
3563 "A preset of the tags filter used for secondary agenda filtering.
3564 This must be a list of strings, each string must be a single tag preceded
3565 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-category-filter-preset nil
3573 "A preset of the category 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 (defvar org-agenda-regexp-filter-preset nil
3583 "A preset of the regexp filter used for secondary agenda filtering.
3584 This must be a list of strings, each string must be a single regexp
3585 preceded by \"+\" or \"-\".
3586 This variable should not be set directly, but agenda custom commands can
3587 bind it in the options section. The preset filter is a global property of
3588 the entire agenda view. In a block agenda, it will not work reliably to
3589 define a filter for one of the individual blocks. You need to set it in
3590 the global options and expect it to be applied to the entire view.")
3592 (defvar org-agenda-effort-filter-preset nil
3593 "A preset of the effort condition used for secondary agenda filtering.
3594 This must be a list of strings, each string must be a single regexp
3595 preceded by \"+\" or \"-\".
3596 This variable should not be set directly, but agenda custom commands can
3597 bind it in the options section. The preset filter is a global property of
3598 the entire agenda view. In a block agenda, it will not work reliably to
3599 define a filter for one of the individual blocks. You need to set it in
3600 the global options and expect it to be applied to the entire view.")
3602 (defun org-agenda-use-sticky-p ()
3603 "Return non-nil if an agenda buffer named
3604 `org-agenda-buffer-name' exists and should be shown instead of
3605 generating a new one."
3606 (and
3607 ;; turned off by user
3608 org-agenda-sticky
3609 ;; For multi-agenda buffer already exists
3610 (not org-agenda-multi)
3611 ;; buffer found
3612 (get-buffer org-agenda-buffer-name)
3613 ;; C-u parameter is same as last call
3614 (with-current-buffer (get-buffer org-agenda-buffer-name)
3615 (and
3616 (equal current-prefix-arg
3617 org-agenda-last-prefix-arg)
3618 ;; In case user turned stickiness on, while having existing
3619 ;; Agenda buffer active, don't reuse that buffer, because it
3620 ;; does not have org variables local
3621 org-agenda-this-buffer-is-sticky))))
3623 (defun org-agenda-prepare-window (abuf filter-alist)
3624 "Setup agenda buffer in the window.
3625 ABUF is the buffer for the agenda window.
3626 FILTER-ALIST is an alist of filters we need to apply when
3627 `org-agenda-persistent-filter' is non-nil."
3628 (let* ((awin (get-buffer-window abuf)) wconf)
3629 (cond
3630 ((equal (current-buffer) abuf) nil)
3631 (awin (select-window awin))
3632 ((not (setq wconf (current-window-configuration))))
3633 ((equal org-agenda-window-setup 'current-window)
3634 (org-pop-to-buffer-same-window abuf))
3635 ((equal org-agenda-window-setup 'other-window)
3636 (org-switch-to-buffer-other-window abuf))
3637 ((equal org-agenda-window-setup 'other-frame)
3638 (switch-to-buffer-other-frame abuf))
3639 ((eq org-agenda-window-setup 'only-window)
3640 (delete-other-windows)
3641 (org-pop-to-buffer-same-window abuf))
3642 ((equal org-agenda-window-setup 'reorganize-frame)
3643 (delete-other-windows)
3644 (org-switch-to-buffer-other-window abuf)))
3645 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3646 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3647 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3648 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3649 ;; Additional test in case agenda is invoked from within agenda
3650 ;; buffer via elisp link.
3651 (unless (equal (current-buffer) abuf)
3652 (org-pop-to-buffer-same-window abuf))
3653 (setq org-agenda-pre-window-conf
3654 (or wconf org-agenda-pre-window-conf))))
3656 (defun org-agenda-prepare (&optional name)
3657 (let ((filter-alist (if org-agenda-persistent-filter
3658 (with-current-buffer
3659 (get-buffer-create org-agenda-buffer-name)
3660 (list `(tag . ,org-agenda-tag-filter)
3661 `(re . ,org-agenda-regexp-filter)
3662 `(effort . ,org-agenda-effort-filter)
3663 `(cat . ,org-agenda-category-filter))))))
3664 (if (org-agenda-use-sticky-p)
3665 (progn
3666 (put 'org-agenda-tag-filter :preset-filter nil)
3667 (put 'org-agenda-category-filter :preset-filter nil)
3668 (put 'org-agenda-regexp-filter :preset-filter nil)
3669 ;; Popup existing buffer
3670 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3671 filter-alist)
3672 (message "Sticky Agenda buffer, use `r' to refresh")
3673 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3674 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3675 (setq org-todo-keywords-for-agenda nil)
3676 (put 'org-agenda-tag-filter :preset-filter
3677 org-agenda-tag-filter-preset)
3678 (put 'org-agenda-category-filter :preset-filter
3679 org-agenda-category-filter-preset)
3680 (put 'org-agenda-regexp-filter :preset-filter
3681 org-agenda-regexp-filter-preset)
3682 (put 'org-agenda-effort-filter :preset-filter
3683 org-agenda-effort-filter-preset)
3684 (if org-agenda-multi
3685 (progn
3686 (setq buffer-read-only nil)
3687 (goto-char (point-max))
3688 (unless (or (bobp) org-agenda-compact-blocks
3689 (not org-agenda-block-separator))
3690 (insert "\n"
3691 (if (stringp org-agenda-block-separator)
3692 org-agenda-block-separator
3693 (make-string (window-width) org-agenda-block-separator))
3694 "\n"))
3695 (narrow-to-region (point) (point-max)))
3696 (setq org-done-keywords-for-agenda nil)
3697 ;; Setting any org variables that are in org-agenda-local-vars
3698 ;; list need to be done after the prepare call
3699 (org-agenda-prepare-window
3700 (get-buffer-create org-agenda-buffer-name) filter-alist)
3701 (setq buffer-read-only nil)
3702 (org-agenda-reset-markers)
3703 (let ((inhibit-read-only t)) (erase-buffer))
3704 (org-agenda-mode)
3705 (setq org-agenda-buffer (current-buffer))
3706 (setq org-agenda-contributing-files nil)
3707 (setq org-agenda-columns-active nil)
3708 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3709 (setq org-todo-keywords-for-agenda
3710 (org-uniquify org-todo-keywords-for-agenda))
3711 (setq org-done-keywords-for-agenda
3712 (org-uniquify org-done-keywords-for-agenda))
3713 (setq org-agenda-last-prefix-arg current-prefix-arg)
3714 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3715 (and name (not org-agenda-name)
3716 (setq-local org-agenda-name name)))
3717 (setq buffer-read-only nil))))
3719 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3720 (defun org-agenda-finalize ()
3721 "Finishing touch for the agenda buffer, called just before displaying it."
3722 (unless org-agenda-multi
3723 (save-excursion
3724 (let ((inhibit-read-only t))
3725 (goto-char (point-min))
3726 (save-excursion
3727 (while (org-activate-bracket-links (point-max))
3728 (add-text-properties (match-beginning 0) (match-end 0)
3729 '(face org-link))))
3730 (save-excursion
3731 (while (org-activate-plain-links (point-max))
3732 (add-text-properties (match-beginning 0) (match-end 0)
3733 '(face org-link))))
3734 (unless (eq org-agenda-remove-tags t)
3735 (org-agenda-align-tags))
3736 (unless org-agenda-with-colors
3737 (remove-text-properties (point-min) (point-max) '(face nil)))
3738 (if (and (boundp 'org-agenda-overriding-columns-format)
3739 org-agenda-overriding-columns-format)
3740 (setq-local org-agenda-overriding-columns-format
3741 org-agenda-overriding-columns-format))
3742 (if (and (boundp 'org-agenda-view-columns-initially)
3743 org-agenda-view-columns-initially)
3744 (org-agenda-columns))
3745 (when org-agenda-fontify-priorities
3746 (org-agenda-fontify-priorities))
3747 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3748 (org-agenda-dim-blocked-tasks))
3749 (org-agenda-mark-clocking-task)
3750 (when org-agenda-entry-text-mode
3751 (org-agenda-entry-text-hide)
3752 (org-agenda-entry-text-show))
3753 (if (and (functionp 'org-habit-insert-consistency-graphs)
3754 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3755 (org-habit-insert-consistency-graphs))
3756 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3757 (unless (or (eq org-agenda-show-inherited-tags 'always)
3758 (and (listp org-agenda-show-inherited-tags)
3759 (memq org-agenda-type org-agenda-show-inherited-tags))
3760 (and (eq org-agenda-show-inherited-tags t)
3761 (or (eq org-agenda-use-tag-inheritance t)
3762 (and (listp org-agenda-use-tag-inheritance)
3763 (not (memq org-agenda-type
3764 org-agenda-use-tag-inheritance))))))
3765 (let (mrk)
3766 (save-excursion
3767 (goto-char (point-min))
3768 (while (equal (forward-line) 0)
3769 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3770 (put-text-property (point-at-bol) (point-at-eol)
3771 'tags (org-with-point-at mrk
3772 (delete-dups
3773 (mapcar 'downcase (org-get-tags-at))))))))))
3774 (run-hooks 'org-agenda-finalize-hook)
3775 (when org-agenda-top-headline-filter
3776 (org-agenda-filter-top-headline-apply
3777 org-agenda-top-headline-filter))
3778 (when org-agenda-tag-filter
3779 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3780 (when (get 'org-agenda-tag-filter :preset-filter)
3781 (org-agenda-filter-apply
3782 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3783 (when org-agenda-category-filter
3784 (org-agenda-filter-apply org-agenda-category-filter 'category))
3785 (when (get 'org-agenda-category-filter :preset-filter)
3786 (org-agenda-filter-apply
3787 (get 'org-agenda-category-filter :preset-filter) 'category))
3788 (when org-agenda-regexp-filter
3789 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3790 (when (get 'org-agenda-regexp-filter :preset-filter)
3791 (org-agenda-filter-apply
3792 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3793 (when org-agenda-effort-filter
3794 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3795 (when (get 'org-agenda-effort-filter :preset-filter)
3796 (org-agenda-filter-apply
3797 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3798 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3800 (defun org-agenda-mark-clocking-task ()
3801 "Mark the current clock entry in the agenda if it is present."
3802 ;; We need to widen when `org-agenda-finalize' is called from
3803 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3804 (when (bound-and-true-p org-clock-current-task)
3805 (save-restriction
3806 (widen)
3807 (org-agenda-unmark-clocking-task)
3808 (when (marker-buffer org-clock-hd-marker)
3809 (save-excursion
3810 (goto-char (point-min))
3811 (let (s ov)
3812 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3813 (goto-char s)
3814 (when (equal (org-get-at-bol 'org-hd-marker)
3815 org-clock-hd-marker)
3816 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3817 (overlay-put ov 'type 'org-agenda-clocking)
3818 (overlay-put ov 'face 'org-agenda-clocking)
3819 (overlay-put ov 'help-echo
3820 "The clock is running in this item")))))))))
3822 (defun org-agenda-unmark-clocking-task ()
3823 "Unmark the current clocking task."
3824 (mapc (lambda (o)
3825 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3826 (delete-overlay o)))
3827 (overlays-in (point-min) (point-max))))
3829 (defun org-agenda-fontify-priorities ()
3830 "Make highest priority lines bold, and lowest italic."
3831 (interactive)
3832 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3833 (delete-overlay o)))
3834 (overlays-in (point-min) (point-max)))
3835 (save-excursion
3836 (let (b e p ov h l)
3837 (goto-char (point-min))
3838 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3839 (setq h (or (get-char-property (point) 'org-highest-priority)
3840 org-highest-priority)
3841 l (or (get-char-property (point) 'org-lowest-priority)
3842 org-lowest-priority)
3843 p (string-to-char (match-string 1))
3844 b (match-beginning 0)
3845 e (if (eq org-agenda-fontify-priorities 'cookies)
3846 (match-end 0)
3847 (point-at-eol))
3848 ov (make-overlay b e))
3849 (overlay-put
3850 ov 'face
3851 (let ((special-face
3852 (cond ((org-face-from-face-or-color
3853 'priority nil
3854 (cdr (assoc p org-priority-faces))))
3855 ((and (listp org-agenda-fontify-priorities)
3856 (org-face-from-face-or-color
3857 'priority nil
3858 (cdr (assoc p org-agenda-fontify-priorities)))))
3859 ((equal p l) 'italic)
3860 ((equal p h) 'bold))))
3861 (if special-face (list special-face 'org-priority) 'org-priority)))
3862 (overlay-put ov 'org-type 'org-priority)))))
3864 (defvar org-depend-tag-blocked)
3866 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3867 "Dim currently blocked TODOs in the agenda display.
3868 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3869 dimming them."
3870 (interactive "P")
3871 (when (org-called-interactively-p 'interactive)
3872 (message "Dim or hide blocked tasks..."))
3873 (dolist (o (overlays-in (point-min) (point-max)))
3874 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3875 (delete-overlay o)))
3876 (save-excursion
3877 (let ((inhibit-read-only t)
3878 (org-depend-tag-blocked nil)
3879 org-blocked-by-checkboxes)
3880 (goto-char (point-min))
3881 (while (let ((pos (text-property-not-all
3882 (point) (point-max) 'todo-state nil)))
3883 (when pos (goto-char pos)))
3884 (setq org-blocked-by-checkboxes nil)
3885 (let ((marker (org-get-at-bol 'org-hd-marker)))
3886 (when (and (markerp marker)
3887 (with-current-buffer (marker-buffer marker)
3888 (save-excursion (goto-char marker)
3889 (org-entry-blocked-p))))
3890 ;; Entries blocked by checkboxes cannot be made invisible.
3891 ;; See `org-agenda-dim-blocked-tasks' for details.
3892 (let* ((really-invisible
3893 (and (not org-blocked-by-checkboxes)
3894 (or invisible (eq org-agenda-dim-blocked-tasks
3895 'invisible))))
3896 (ov (make-overlay (if really-invisible (line-end-position 0)
3897 (line-beginning-position))
3898 (line-end-position))))
3899 (if really-invisible (overlay-put ov 'invisible t)
3900 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3901 (overlay-put ov 'org-type 'org-blocked-todo))))
3902 (forward-line))))
3903 (when (org-called-interactively-p 'interactive)
3904 (message "Dim or hide blocked tasks...done")))
3906 (defvar org-agenda-skip-function nil
3907 "Function to be called at each match during agenda construction.
3908 If this function returns nil, the current match should not be skipped.
3909 Otherwise, the function must return a position from where the search
3910 should be continued.
3911 This may also be a Lisp form, it will be evaluated.
3912 Never set this variable using `setq' or so, because then it will apply
3913 to all future agenda commands. If you do want a global skipping condition,
3914 use the option `org-agenda-skip-function-global' instead.
3915 The correct usage for `org-agenda-skip-function' is to bind it with
3916 `let' to scope it dynamically into the agenda-constructing command.
3917 A good way to set it is through options in `org-agenda-custom-commands'.")
3919 (defun org-agenda-skip ()
3920 "Throw to `:skip' in places that should be skipped.
3921 Also moves point to the end of the skipped region, so that search can
3922 continue from there."
3923 (let ((p (point-at-bol)) to)
3924 (when (or
3925 (save-excursion (goto-char p) (looking-at comment-start-skip))
3926 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3927 (get-text-property p :org-archived)
3928 (org-end-of-subtree t))
3929 (and org-agenda-skip-comment-trees
3930 (get-text-property p :org-comment)
3931 (org-end-of-subtree t))
3932 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3933 (org-agenda-skip-eval org-agenda-skip-function)))
3934 (goto-char to))
3935 (org-in-src-block-p t))
3936 (throw :skip t))))
3938 (defun org-agenda-skip-eval (form)
3939 "If FORM is a function or a list, call (or eval) it and return the result.
3940 `save-excursion' and `save-match-data' are wrapped around the call, so point
3941 and match data are returned to the previous state no matter what these
3942 functions do."
3943 (let (fp)
3944 (and form
3945 (or (setq fp (functionp form))
3946 (consp form))
3947 (save-excursion
3948 (save-match-data
3949 (if fp
3950 (funcall form)
3951 (eval form)))))))
3953 (defvar org-agenda-markers nil
3954 "List of all currently active markers created by `org-agenda'.")
3955 (defvar org-agenda-last-marker-time (float-time)
3956 "Creation time of the last agenda marker.")
3958 (defun org-agenda-new-marker (&optional pos)
3959 "Return a new agenda marker.
3960 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3961 these markers and resets them when they are no longer in use."
3962 (let ((m (copy-marker (or pos (point)) t)))
3963 (setq org-agenda-last-marker-time (float-time))
3964 (if org-agenda-buffer
3965 (with-current-buffer org-agenda-buffer
3966 (push m org-agenda-markers))
3967 (push m org-agenda-markers))
3970 (defun org-agenda-reset-markers ()
3971 "Reset markers created by `org-agenda'."
3972 (while org-agenda-markers
3973 (move-marker (pop org-agenda-markers) nil)))
3975 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3976 "Save relative positions of markers in region.
3977 This check for agenda markers in all agenda buffers currently active."
3978 (dolist (buf (buffer-list))
3979 (with-current-buffer buf
3980 (when (eq major-mode 'org-agenda-mode)
3981 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3982 org-agenda-markers)))))
3984 ;;; Entry text mode
3986 (defun org-agenda-entry-text-show-here ()
3987 "Add some text from the entry as context to the current line."
3988 (let (m txt o)
3989 (setq m (org-get-at-bol 'org-hd-marker))
3990 (unless (marker-buffer m)
3991 (error "No marker points to an entry here"))
3992 (setq txt (concat "\n" (org-no-properties
3993 (org-agenda-get-some-entry-text
3994 m org-agenda-entry-text-maxlines
3995 org-agenda-entry-text-leaders))))
3996 (when (string-match "\\S-" txt)
3997 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3998 (overlay-put o 'evaporate t)
3999 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4000 (overlay-put o 'after-string txt))))
4002 (defun org-agenda-entry-text-show ()
4003 "Add entry context for all agenda lines."
4004 (interactive)
4005 (save-excursion
4006 (goto-char (point-max))
4007 (beginning-of-line 1)
4008 (while (not (bobp))
4009 (when (org-get-at-bol 'org-hd-marker)
4010 (org-agenda-entry-text-show-here))
4011 (beginning-of-line 0))))
4013 (defun org-agenda-entry-text-hide ()
4014 "Remove any shown entry context."
4015 (delq nil
4016 (mapcar (lambda (o)
4017 (if (eq (overlay-get o 'org-overlay-type)
4018 'agenda-entry-content)
4019 (progn (delete-overlay o) t)))
4020 (overlays-in (point-min) (point-max)))))
4022 (defun org-agenda-get-day-face (date)
4023 "Return the face DATE should be displayed with."
4024 (cond ((and (functionp org-agenda-day-face-function)
4025 (funcall org-agenda-day-face-function date)))
4026 ((org-agenda-today-p date) 'org-agenda-date-today)
4027 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4028 'org-agenda-date-weekend)
4029 (t 'org-agenda-date)))
4031 ;;; Agenda timeline
4033 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4034 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4036 (defun org-timeline (&optional dotodo)
4037 "Show a time-sorted view of the entries in the current org file.
4038 Only entries with a time stamp of today or later will be listed. With
4039 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4040 under the current date.
4041 If the buffer contains an active region, only check the region for
4042 dates."
4043 (interactive "P")
4044 (let* ((dopast t)
4045 (org-agenda-show-log-scoped org-agenda-show-log)
4046 (org-agenda-show-log org-agenda-show-log-scoped)
4047 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4048 (current-buffer))))
4049 (date (calendar-current-date))
4050 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4051 (end (if (org-region-active-p) (region-end) (point-max)))
4052 (day-numbers (org-get-all-dates
4053 beg end 'no-ranges
4054 t org-agenda-show-log-scoped ; always include today
4055 org-timeline-show-empty-dates))
4056 (org-deadline-warning-days 0)
4057 (org-agenda-only-exact-dates t)
4058 (today (org-today))
4059 (past t)
4060 args
4061 s e rtn d emptyp)
4062 (setq org-agenda-redo-command
4063 (list 'let
4064 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4065 (list 'org-switch-to-buffer-other-window (current-buffer))
4066 (list 'org-timeline (list 'quote dotodo))))
4067 (put 'org-agenda-redo-command 'org-lprops nil)
4068 (if (not dopast)
4069 ;; Remove past dates from the list of dates.
4070 (setq day-numbers (delq nil (mapcar (lambda(x)
4071 (if (>= x today) x nil))
4072 day-numbers))))
4073 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4074 (org-compile-prefix-format 'timeline)
4075 (org-set-sorting-strategy 'timeline)
4076 (if org-agenda-show-log-scoped (push :closed args))
4077 (push :timestamp args)
4078 (push :deadline args)
4079 (push :scheduled args)
4080 (push :sexp args)
4081 (if dotodo (push :todo args))
4082 (insert "Timeline of file " entry "\n")
4083 (add-text-properties (point-min) (point)
4084 (list 'face 'org-agenda-structure))
4085 (org-agenda-mark-header-line (point-min))
4086 (while (setq d (pop day-numbers))
4087 (if (and (listp d) (eq (car d) :omitted))
4088 (progn
4089 (setq s (point))
4090 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4091 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4092 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4093 (if (and (>= d today)
4094 dopast
4095 past)
4096 (progn
4097 (setq past nil)
4098 (insert (make-string 79 ?-) "\n")))
4099 (setq date (calendar-gregorian-from-absolute d))
4100 (setq s (point))
4101 (setq rtn (and (not emptyp)
4102 (apply 'org-agenda-get-day-entries entry
4103 date args)))
4104 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4105 (progn
4106 (insert
4107 (if (stringp org-agenda-format-date)
4108 (format-time-string org-agenda-format-date
4109 (org-time-from-absolute date))
4110 (funcall org-agenda-format-date date))
4111 "\n")
4112 (put-text-property s (1- (point)) 'face
4113 (org-agenda-get-day-face date))
4114 (put-text-property s (1- (point)) 'org-date-line t)
4115 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4116 (if (equal d today)
4117 (put-text-property s (1- (point)) 'org-today t))
4118 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4119 (put-text-property s (1- (point)) 'day d)))))
4120 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4121 (point-min)))
4122 (add-text-properties
4123 (point-min) (point-max)
4124 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4125 (org-agenda-finalize)
4126 (setq buffer-read-only t)))
4128 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4129 "Return a list of all relevant day numbers from BEG to END buffer positions.
4130 If NO-RANGES is non-nil, include only the start and end dates of a range,
4131 not every single day in the range. If FORCE-TODAY is non-nil, make
4132 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4133 inactive time stamps (those in square brackets) are included.
4134 When EMPTY is non-nil, also include days without any entries."
4135 (let ((re (concat
4136 (if pre-re pre-re "")
4137 (if inactive org-ts-regexp-both org-ts-regexp)))
4138 dates dates1 date day day1 day2 ts1 ts2 pos)
4139 (if force-today
4140 (setq dates (list (org-today))))
4141 (save-excursion
4142 (goto-char beg)
4143 (while (re-search-forward re end t)
4144 (setq day (time-to-days (org-time-string-to-time
4145 (substring (match-string 1) 0 10)
4146 (current-buffer) (match-beginning 0))))
4147 (or (memq day dates) (push day dates)))
4148 (unless no-ranges
4149 (goto-char beg)
4150 (while (re-search-forward org-tr-regexp end t)
4151 (setq pos (match-beginning 0))
4152 (setq ts1 (substring (match-string 1) 0 10)
4153 ts2 (substring (match-string 2) 0 10)
4154 day1 (time-to-days (org-time-string-to-time
4155 ts1 (current-buffer) pos))
4156 day2 (time-to-days (org-time-string-to-time
4157 ts2 (current-buffer) pos)))
4158 (while (< (setq day1 (1+ day1)) day2)
4159 (or (memq day1 dates) (push day1 dates)))))
4160 (setq dates (sort dates '<))
4161 (when empty
4162 (while (setq day (pop dates))
4163 (setq day2 (car dates))
4164 (push day dates1)
4165 (when (and day2 empty)
4166 (if (or (eq empty t)
4167 (and (numberp empty) (<= (- day2 day) empty)))
4168 (while (< (setq day (1+ day)) day2)
4169 (push (list day) dates1))
4170 (push (cons :omitted (- day2 day)) dates1))))
4171 (setq dates (nreverse dates1)))
4172 dates)))
4174 ;;; Agenda Daily/Weekly
4176 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4177 "Start day for the agenda view.
4178 Custom commands can set this variable in the options section.
4179 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4180 input allowed when reading a date through the Org calendar.
4181 See the docstring of `org-read-date' for details.")
4182 (defvar org-starting-day nil) ; local variable in the agenda buffer
4183 (defvar org-arg-loc nil) ; local variable
4185 (defvar org-agenda-buffer-tmp-name nil)
4186 ;;;###autoload
4187 (defun org-agenda-list (&optional arg start-day span with-hour)
4188 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4189 The view will be for the current day or week, but from the overview buffer
4190 you will be able to go to other days/weeks.
4192 With a numeric prefix argument in an interactive call, the agenda will
4193 span ARG days. Lisp programs should instead specify SPAN to change
4194 the number of days. SPAN defaults to `org-agenda-span'.
4196 START-DAY defaults to TODAY, or to the most recent match for the weekday
4197 given in `org-agenda-start-on-weekday'.
4199 When WITH-HOUR is non-nil, only include scheduled and deadline
4200 items if they have an hour specification like [h]h:mm."
4201 (interactive "P")
4202 (if org-agenda-overriding-arguments
4203 (setq arg (car org-agenda-overriding-arguments)
4204 start-day (nth 1 org-agenda-overriding-arguments)
4205 span (nth 2 org-agenda-overriding-arguments)))
4206 (if (and (integerp arg) (> arg 0))
4207 (setq span arg arg nil))
4208 (catch 'exit
4209 (setq org-agenda-buffer-name
4210 (or org-agenda-buffer-tmp-name
4211 (if org-agenda-sticky
4212 (cond ((and org-keys (stringp org-match))
4213 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4214 (org-keys
4215 (format "*Org Agenda(%s)*" org-keys))
4216 (t "*Org Agenda(a)*")))
4217 org-agenda-buffer-name))
4218 (org-agenda-prepare "Day/Week")
4219 (setq start-day (or start-day org-agenda-start-day))
4220 (if (stringp start-day)
4221 ;; Convert to an absolute day number
4222 (setq start-day (time-to-days (org-read-date nil t start-day))))
4223 (org-compile-prefix-format 'agenda)
4224 (org-set-sorting-strategy 'agenda)
4225 (let* ((span (org-agenda-ndays-to-span
4226 (or span org-agenda-ndays org-agenda-span)))
4227 (today (org-today))
4228 (sd (or start-day today))
4229 (ndays (org-agenda-span-to-ndays span sd))
4230 (org-agenda-start-on-weekday
4231 (if (or (eq ndays 7) (eq ndays 14))
4232 org-agenda-start-on-weekday))
4233 (thefiles (org-agenda-files nil 'ifmode))
4234 (files thefiles)
4235 (start (if (or (null org-agenda-start-on-weekday)
4236 (< ndays 7))
4238 (let* ((nt (calendar-day-of-week
4239 (calendar-gregorian-from-absolute sd)))
4240 (n1 org-agenda-start-on-weekday)
4241 (d (- nt n1)))
4242 (- sd (+ (if (< d 0) 7 0) d)))))
4243 (day-numbers (list start))
4244 (day-cnt 0)
4245 (inhibit-redisplay (not debug-on-error))
4246 (org-agenda-show-log-scoped org-agenda-show-log)
4247 s e rtn rtnall file date d start-pos end-pos todayp
4248 clocktable-start clocktable-end filter)
4249 (setq org-agenda-redo-command
4250 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4251 (dotimes (n (1- ndays))
4252 (push (1+ (car day-numbers)) day-numbers))
4253 (setq day-numbers (nreverse day-numbers))
4254 (setq clocktable-start (car day-numbers)
4255 clocktable-end (1+ (or (org-last day-numbers) 0)))
4256 (setq-local org-starting-day (car day-numbers))
4257 (setq-local org-arg-loc arg)
4258 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4259 (unless org-agenda-compact-blocks
4260 (let* ((d1 (car day-numbers))
4261 (d2 (org-last day-numbers))
4262 (w1 (org-days-to-iso-week d1))
4263 (w2 (org-days-to-iso-week d2)))
4264 (setq s (point))
4265 (if org-agenda-overriding-header
4266 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4267 nil 'face 'org-agenda-structure) "\n")
4268 (insert (org-agenda-span-name span)
4269 "-agenda"
4270 (if (< (- d2 d1) 350)
4271 (if (= w1 w2)
4272 (format " (W%02d)" w1)
4273 (format " (W%02d-W%02d)" w1 w2))
4275 ":\n")))
4276 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4277 'org-date-line t))
4278 (org-agenda-mark-header-line s))
4279 (while (setq d (pop day-numbers))
4280 (setq date (calendar-gregorian-from-absolute d)
4281 s (point))
4282 (if (or (setq todayp (= d today))
4283 (and (not start-pos) (= d sd)))
4284 (setq start-pos (point))
4285 (if (and start-pos (not end-pos))
4286 (setq end-pos (point))))
4287 (setq files thefiles
4288 rtnall nil)
4289 (while (setq file (pop files))
4290 (catch 'nextfile
4291 (org-check-agenda-file file)
4292 (let ((org-agenda-entry-types org-agenda-entry-types))
4293 ;; Starred types override non-starred equivalents
4294 (when (member :deadline* org-agenda-entry-types)
4295 (setq org-agenda-entry-types
4296 (delq :deadline org-agenda-entry-types)))
4297 (when (member :scheduled* org-agenda-entry-types)
4298 (setq org-agenda-entry-types
4299 (delq :scheduled org-agenda-entry-types)))
4300 ;; Honor with-hour
4301 (when with-hour
4302 (when (member :deadline org-agenda-entry-types)
4303 (setq org-agenda-entry-types
4304 (delq :deadline org-agenda-entry-types))
4305 (push :deadline* org-agenda-entry-types))
4306 (when (member :scheduled org-agenda-entry-types)
4307 (setq org-agenda-entry-types
4308 (delq :scheduled org-agenda-entry-types))
4309 (push :scheduled* org-agenda-entry-types)))
4310 (unless org-agenda-include-deadlines
4311 (setq org-agenda-entry-types
4312 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4313 (cond
4314 ((memq org-agenda-show-log-scoped '(only clockcheck))
4315 (setq rtn (org-agenda-get-day-entries
4316 file date :closed)))
4317 (org-agenda-show-log-scoped
4318 (setq rtn (apply 'org-agenda-get-day-entries
4319 file date
4320 (append '(:closed) org-agenda-entry-types))))
4322 (setq rtn (apply 'org-agenda-get-day-entries
4323 file date
4324 org-agenda-entry-types)))))
4325 (setq rtnall (append rtnall rtn)))) ;; all entries
4326 (if org-agenda-include-diary
4327 (let ((org-agenda-search-headline-for-time t))
4328 (require 'diary-lib)
4329 (setq rtn (org-get-entries-from-diary date))
4330 (setq rtnall (append rtnall rtn))))
4331 (if (or rtnall org-agenda-show-all-dates)
4332 (progn
4333 (setq day-cnt (1+ day-cnt))
4334 (insert
4335 (if (stringp org-agenda-format-date)
4336 (format-time-string org-agenda-format-date
4337 (org-time-from-absolute date))
4338 (funcall org-agenda-format-date date))
4339 "\n")
4340 (put-text-property s (1- (point)) 'face
4341 (org-agenda-get-day-face date))
4342 (put-text-property s (1- (point)) 'org-date-line t)
4343 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4344 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4345 (when todayp
4346 (put-text-property s (1- (point)) 'org-today t))
4347 (setq rtnall
4348 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4349 (if rtnall (insert ;; all entries
4350 (org-agenda-finalize-entries rtnall 'agenda)
4351 "\n"))
4352 (put-text-property s (1- (point)) 'day d)
4353 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4354 (when (and org-agenda-clockreport-mode clocktable-start)
4355 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4356 ;; the above line is to ensure the restricted range!
4357 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4358 tbl)
4359 (setq p (org-plist-delete p :block))
4360 (setq p (plist-put p :tstart clocktable-start))
4361 (setq p (plist-put p :tend clocktable-end))
4362 (setq p (plist-put p :scope 'agenda))
4363 (setq tbl (apply 'org-clock-get-clocktable p))
4364 (insert tbl)))
4365 (goto-char (point-min))
4366 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4367 (unless (and (pos-visible-in-window-p (point-min))
4368 (pos-visible-in-window-p (point-max)))
4369 (goto-char (1- (point-max)))
4370 (recenter -1)
4371 (if (not (pos-visible-in-window-p (or start-pos 1)))
4372 (progn
4373 (goto-char (or start-pos 1))
4374 (recenter 1))))
4375 (goto-char (or start-pos 1))
4376 (add-text-properties (point-min) (point-max)
4377 `(org-agenda-type agenda
4378 org-last-args (,arg ,start-day ,span)
4379 org-redo-cmd ,org-agenda-redo-command
4380 org-series-cmd ,org-cmd))
4381 (if (eq org-agenda-show-log-scoped 'clockcheck)
4382 (org-agenda-show-clocking-issues))
4383 (org-agenda-finalize)
4384 (setq buffer-read-only t)
4385 (message ""))))
4387 (defun org-agenda-ndays-to-span (n)
4388 "Return a span symbol for a span of N days, or N if none matches."
4389 (cond ((symbolp n) n)
4390 ((= n 1) 'day)
4391 ((= n 7) 'week)
4392 ((= n 14) 'fortnight)
4393 (t n)))
4395 (defun org-agenda-span-to-ndays (span &optional start-day)
4396 "Return ndays from SPAN, possibly starting at START-DAY.
4397 START-DAY is an absolute time value."
4398 (cond ((numberp span) span)
4399 ((eq span 'day) 1)
4400 ((eq span 'week) 7)
4401 ((eq span 'fortnight) 14)
4402 ((eq span 'month)
4403 (let ((date (calendar-gregorian-from-absolute start-day)))
4404 (calendar-last-day-of-month (car date) (caddr date))))
4405 ((eq span 'year)
4406 (let ((date (calendar-gregorian-from-absolute start-day)))
4407 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4409 (defun org-agenda-span-name (span)
4410 "Return a SPAN name."
4411 (if (null span)
4413 (if (symbolp span)
4414 (capitalize (symbol-name span))
4415 (format "%d days" span))))
4417 ;;; Agenda word search
4419 (defvar org-agenda-search-history nil)
4421 (defvar org-search-syntax-table nil
4422 "Special syntax table for org-mode search.
4423 In this table, we have single quotes not as word constituents, to
4424 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4426 (defvar org-mode-syntax-table) ; From org.el
4427 (defun org-search-syntax-table ()
4428 (unless org-search-syntax-table
4429 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4430 (modify-syntax-entry ?' "." org-search-syntax-table)
4431 (modify-syntax-entry ?` "." org-search-syntax-table))
4432 org-search-syntax-table)
4434 (defvar org-agenda-last-search-view-search-was-boolean nil)
4436 ;;;###autoload
4437 (defun org-search-view (&optional todo-only string edit-at)
4438 "Show all entries that contain a phrase or words or regular expressions.
4440 With optional prefix argument TODO-ONLY, only consider entries that are
4441 TODO entries. The argument STRING can be used to pass a default search
4442 string into this function. If EDIT-AT is non-nil, it means that the
4443 user should get a chance to edit this string, with cursor at position
4444 EDIT-AT.
4446 The search string can be viewed either as a phrase that should be found as
4447 is, or it can be broken into a number of snippets, each of which must match
4448 in a Boolean way to select an entry. The default depends on the variable
4449 `org-agenda-search-view-always-boolean'.
4450 Even if this is turned off (the default) you can always switch to
4451 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4453 The default is a direct search of the whole phrase, where each space in
4454 the search string can expand to an arbitrary amount of whitespace,
4455 including newlines.
4457 If using a Boolean search, the search string is split on whitespace and
4458 each snippet is searched separately, with logical AND to select an entry.
4459 Words prefixed with a minus must *not* occur in the entry. Words without
4460 a prefix or prefixed with a plus must occur in the entry. Matching is
4461 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4462 match whole words, not parts of a word) if
4463 `org-agenda-search-view-force-full-words' is set (default is nil).
4465 Boolean search snippets enclosed by curly braces are interpreted as
4466 regular expressions that must or (when preceded with \"-\") must not
4467 match in the entry. Snippets enclosed into double quotes will be taken
4468 as a whole, to include whitespace.
4470 - If the search string starts with an asterisk, search only in headlines.
4471 - If (possibly after the leading star) the search string starts with an
4472 exclamation mark, this also means to look at TODO entries only, an effect
4473 that can also be achieved with a prefix argument.
4474 - If (possibly after star and exclamation mark) the search string starts
4475 with a colon, this will mean that the (non-regexp) snippets of the
4476 Boolean search must match as full words.
4478 This command searches the agenda files, and in addition the files listed
4479 in `org-agenda-text-search-extra-files'."
4480 (interactive "P")
4481 (if org-agenda-overriding-arguments
4482 (setq todo-only (car org-agenda-overriding-arguments)
4483 string (nth 1 org-agenda-overriding-arguments)
4484 edit-at (nth 2 org-agenda-overriding-arguments)))
4485 (let* ((props (list 'face nil
4486 'done-face 'org-agenda-done
4487 'org-not-done-regexp org-not-done-regexp
4488 'org-todo-regexp org-todo-regexp
4489 'org-complex-heading-regexp org-complex-heading-regexp
4490 'mouse-face 'highlight
4491 'help-echo (format "mouse-2 or RET jump to location")))
4492 (full-words org-agenda-search-view-force-full-words)
4493 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4494 regexp rtn rtnall files file pos inherited-tags
4495 marker category level tags c neg re boolean
4496 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4497 (unless (and (not edit-at)
4498 (stringp string)
4499 (string-match "\\S-" string))
4500 (setq string (read-string
4501 (if org-agenda-search-view-always-boolean
4502 "[+-]Word/{Regexp} ...: "
4503 "Phrase or [+-]Word/{Regexp} ...: ")
4504 (cond
4505 ((integerp edit-at) (cons string edit-at))
4506 (edit-at string))
4507 'org-agenda-search-history)))
4508 (catch 'exit
4509 (if org-agenda-sticky
4510 (setq org-agenda-buffer-name
4511 (if (stringp string)
4512 (format "*Org Agenda(%s:%s)*"
4513 (or org-keys (or (and todo-only "S") "s")) string)
4514 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4515 (org-agenda-prepare "SEARCH")
4516 (org-compile-prefix-format 'search)
4517 (org-set-sorting-strategy 'search)
4518 (setq org-agenda-redo-command
4519 (list 'org-search-view (if todo-only t nil)
4520 (list 'if 'current-prefix-arg nil string)))
4521 (setq org-agenda-query-string string)
4522 (if (equal (string-to-char string) ?*)
4523 (setq hdl-only t
4524 words (substring string 1))
4525 (setq words string))
4526 (when (equal (string-to-char words) ?!)
4527 (setq todo-only t
4528 words (substring words 1)))
4529 (when (equal (string-to-char words) ?:)
4530 (setq full-words t
4531 words (substring words 1)))
4532 (if (or org-agenda-search-view-always-boolean
4533 (member (string-to-char words) '(?- ?+ ?\{)))
4534 (setq boolean t))
4535 (setq words (org-split-string words))
4536 (let (www w)
4537 (while (setq w (pop words))
4538 (while (and (string-match "\\\\\\'" w) words)
4539 (setq w (concat (substring w 0 -1) " " (pop words))))
4540 (push w www))
4541 (setq words (nreverse www) www nil)
4542 (while (setq w (pop words))
4543 (when (and (string-match "\\`[-+]?{" w)
4544 (not (string-match "}\\'" w)))
4545 (while (and words (not (string-match "}\\'" (car words))))
4546 (setq w (concat w " " (pop words))))
4547 (setq w (concat w " " (pop words))))
4548 (push w www))
4549 (setq words (nreverse www)))
4550 (setq org-agenda-last-search-view-search-was-boolean boolean)
4551 (when boolean
4552 (let (wds w)
4553 (while (setq w (pop words))
4554 (if (or (equal (substring w 0 1) "\"")
4555 (and (> (length w) 1)
4556 (member (substring w 0 1) '("+" "-"))
4557 (equal (substring w 1 2) "\"")))
4558 (while (and words (not (equal (substring w -1) "\"")))
4559 (setq w (concat w " " (pop words)))))
4560 (and (string-match "\\`\\([-+]?\\)\"" w)
4561 (setq w (replace-match "\\1" nil nil w)))
4562 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4563 (push w wds))
4564 (setq words (nreverse wds))))
4565 (if boolean
4566 (mapc (lambda (w)
4567 (setq c (string-to-char w))
4568 (if (equal c ?-)
4569 (setq neg t w (substring w 1))
4570 (if (equal c ?+)
4571 (setq neg nil w (substring w 1))
4572 (setq neg nil)))
4573 (if (string-match "\\`{.*}\\'" w)
4574 (setq re (substring w 1 -1))
4575 (if full-words
4576 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4577 (setq re (regexp-quote (downcase w)))))
4578 (if neg (push re regexps-) (push re regexps+)))
4579 words)
4580 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4581 regexps+))
4582 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4583 (if (not regexps+)
4584 (setq regexp org-outline-regexp-bol)
4585 (setq regexp (pop regexps+))
4586 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4587 regexp))))
4588 (setq files (org-agenda-files nil 'ifmode))
4589 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4590 (pop org-agenda-text-search-extra-files)
4591 (setq files (org-add-archive-files files)))
4592 (setq files (append files org-agenda-text-search-extra-files)
4593 rtnall nil)
4594 (while (setq file (pop files))
4595 (setq ee nil)
4596 (catch 'nextfile
4597 (org-check-agenda-file file)
4598 (setq buffer (if (file-exists-p file)
4599 (org-get-agenda-file-buffer file)
4600 (error "No such file %s" file)))
4601 (if (not buffer)
4602 ;; If file does not exist, make sure an error message is sent
4603 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4604 file))))
4605 (with-current-buffer buffer
4606 (with-syntax-table (org-search-syntax-table)
4607 (unless (derived-mode-p 'org-mode)
4608 (error "Agenda file %s is not in `org-mode'" file))
4609 (let ((case-fold-search t))
4610 (save-excursion
4611 (save-restriction
4612 (if (eq buffer org-agenda-restrict)
4613 (narrow-to-region org-agenda-restrict-begin
4614 org-agenda-restrict-end)
4615 (widen))
4616 (goto-char (point-min))
4617 (unless (or (org-at-heading-p)
4618 (outline-next-heading))
4619 (throw 'nextfile t))
4620 (goto-char (max (point-min) (1- (point))))
4621 (while (re-search-forward regexp nil t)
4622 (org-back-to-heading t)
4623 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4624 (> (org-reduced-level (org-outline-level))
4625 org-agenda-search-view-max-outline-level)
4626 (forward-line -1)
4627 (org-back-to-heading t)))
4628 (skip-chars-forward "* ")
4629 (setq beg (point-at-bol)
4630 beg1 (point)
4631 end (progn
4632 (outline-next-heading)
4633 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4634 (> (org-reduced-level (org-outline-level))
4635 org-agenda-search-view-max-outline-level)
4636 (forward-line 1)
4637 (outline-next-heading)))
4638 (point)))
4640 (catch :skip
4641 (goto-char beg)
4642 (org-agenda-skip)
4643 (setq str (buffer-substring-no-properties
4644 (point-at-bol)
4645 (if hdl-only (point-at-eol) end)))
4646 (mapc (lambda (wr) (when (string-match wr str)
4647 (goto-char (1- end))
4648 (throw :skip t)))
4649 regexps-)
4650 (mapc (lambda (wr) (unless (string-match wr str)
4651 (goto-char (1- end))
4652 (throw :skip t)))
4653 (if todo-only
4654 (cons (concat "^\\*+[ \t]+"
4655 org-not-done-regexp)
4656 regexps+)
4657 regexps+))
4658 (goto-char beg)
4659 (setq marker (org-agenda-new-marker (point))
4660 category (org-get-category)
4661 level (make-string (org-reduced-level (org-outline-level)) ? )
4662 inherited-tags
4663 (or (eq org-agenda-show-inherited-tags 'always)
4664 (and (listp org-agenda-show-inherited-tags)
4665 (memq 'todo org-agenda-show-inherited-tags))
4666 (and (eq org-agenda-show-inherited-tags t)
4667 (or (eq org-agenda-use-tag-inheritance t)
4668 (memq 'todo org-agenda-use-tag-inheritance))))
4669 tags (org-get-tags-at nil (not inherited-tags))
4670 txt (org-agenda-format-item
4672 (buffer-substring-no-properties
4673 beg1 (point-at-eol))
4674 level category tags t))
4675 (org-add-props txt props
4676 'org-marker marker 'org-hd-marker marker
4677 'org-todo-regexp org-todo-regexp
4678 'level level
4679 'org-complex-heading-regexp org-complex-heading-regexp
4680 'priority 1000
4681 'type "search")
4682 (push txt ee)
4683 (goto-char (1- end))))))))))
4684 (setq rtn (nreverse ee))
4685 (setq rtnall (append rtnall rtn)))
4686 (if org-agenda-overriding-header
4687 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4688 nil 'face 'org-agenda-structure) "\n")
4689 (insert "Search words: ")
4690 (add-text-properties (point-min) (1- (point))
4691 (list 'face 'org-agenda-structure))
4692 (setq pos (point))
4693 (insert string "\n")
4694 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4695 (setq pos (point))
4696 (unless org-agenda-multi
4697 (insert (substitute-command-keys "\
4698 Press `\\[org-agenda-manipulate-query-add]', \
4699 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4700 `\\[org-agenda-manipulate-query-add-re]', \
4701 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4702 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4703 (add-text-properties pos (1- (point))
4704 (list 'face 'org-agenda-structure))))
4705 (org-agenda-mark-header-line (point-min))
4706 (when rtnall
4707 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4708 (goto-char (point-min))
4709 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4710 (add-text-properties (point-min) (point-max)
4711 `(org-agenda-type search
4712 org-last-args (,todo-only ,string ,edit-at)
4713 org-redo-cmd ,org-agenda-redo-command
4714 org-series-cmd ,org-cmd))
4715 (org-agenda-finalize)
4716 (setq buffer-read-only t))))
4718 ;;; Agenda TODO list
4720 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4721 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4722 (concat
4723 (if (or (equal keywords "ALL") (not keywords))
4724 (propertize "ALL" 'face 'warning)
4725 (mapconcat
4726 (lambda (kw)
4727 (propertize kw 'face (org-get-todo-face kw)))
4728 (org-split-string keywords "|")
4729 "|"))
4730 "\n"))
4732 (defvar org-select-this-todo-keyword nil)
4733 (defvar org-last-arg nil)
4735 ;;;###autoload
4736 (defun org-todo-list (&optional arg)
4737 "Show all (not done) TODO entries from all agenda file in a single list.
4738 The prefix arg can be used to select a specific TODO keyword and limit
4739 the list to these. When using \\[universal-argument], you will be prompted
4740 for a keyword. A numeric prefix directly selects the Nth keyword in
4741 `org-todo-keywords-1'."
4742 (interactive "P")
4743 (if org-agenda-overriding-arguments
4744 (setq arg org-agenda-overriding-arguments))
4745 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4746 (let* ((today (org-today))
4747 (date (calendar-gregorian-from-absolute today))
4748 (kwds org-todo-keywords-for-agenda)
4749 (completion-ignore-case t)
4750 (org-select-this-todo-keyword
4751 (if (stringp arg) arg
4752 (and arg (integerp arg) (> arg 0)
4753 (nth (1- arg) kwds))))
4754 rtn rtnall files file pos)
4755 (when (equal arg '(4))
4756 (setq org-select-this-todo-keyword
4757 (completing-read "Keyword (or KWD1|K2D2|...): "
4758 (mapcar #'list kwds) nil nil)))
4759 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4760 (catch 'exit
4761 (if org-agenda-sticky
4762 (setq org-agenda-buffer-name
4763 (if (stringp org-select-this-todo-keyword)
4764 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4765 org-select-this-todo-keyword)
4766 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4767 (org-agenda-prepare "TODO")
4768 (org-compile-prefix-format 'todo)
4769 (org-set-sorting-strategy 'todo)
4770 (setq org-agenda-redo-command
4771 `(org-todo-list (or (and (numberp current-prefix-arg)
4772 current-prefix-arg)
4773 ,org-select-this-todo-keyword
4774 current-prefix-arg ,arg)))
4775 (setq files (org-agenda-files nil 'ifmode)
4776 rtnall nil)
4777 (while (setq file (pop files))
4778 (catch 'nextfile
4779 (org-check-agenda-file file)
4780 (setq rtn (org-agenda-get-day-entries file date :todo))
4781 (setq rtnall (append rtnall rtn))))
4782 (if org-agenda-overriding-header
4783 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4784 nil 'face 'org-agenda-structure) "\n")
4785 (insert "Global list of TODO items of type: ")
4786 (add-text-properties (point-min) (1- (point))
4787 (list 'face 'org-agenda-structure
4788 'short-heading
4789 (concat "ToDo: "
4790 (or org-select-this-todo-keyword "ALL"))))
4791 (org-agenda-mark-header-line (point-min))
4792 (insert (org-agenda-propertize-selected-todo-keywords
4793 org-select-this-todo-keyword))
4794 (setq pos (point))
4795 (unless org-agenda-multi
4796 (insert (substitute-command-keys "Available with \
4797 `N \\[org-agenda-redo]': (0)[ALL]"))
4798 (let ((n 0) s)
4799 (mapc (lambda (x)
4800 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4801 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4802 (insert "\n "))
4803 (insert " " s))
4804 kwds))
4805 (insert "\n"))
4806 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4807 (org-agenda-mark-header-line (point-min))
4808 (when rtnall
4809 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4810 (goto-char (point-min))
4811 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4812 (add-text-properties (point-min) (point-max)
4813 `(org-agenda-type todo
4814 org-last-args ,arg
4815 org-redo-cmd ,org-agenda-redo-command
4816 org-series-cmd ,org-cmd))
4817 (org-agenda-finalize)
4818 (setq buffer-read-only t))))
4820 ;;; Agenda tags match
4822 ;;;###autoload
4823 (defun org-tags-view (&optional todo-only match)
4824 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4825 The prefix arg TODO-ONLY limits the search to TODO entries."
4826 (interactive "P")
4827 (if org-agenda-overriding-arguments
4828 (setq todo-only (car org-agenda-overriding-arguments)
4829 match (nth 1 org-agenda-overriding-arguments)))
4830 (let* ((org-tags-match-list-sublevels
4831 org-tags-match-list-sublevels)
4832 (completion-ignore-case t)
4833 rtn rtnall files file pos matcher
4834 buffer)
4835 (when (and (stringp match) (not (string-match "\\S-" match)))
4836 (setq match nil))
4837 (catch 'exit
4838 (if org-agenda-sticky
4839 (setq org-agenda-buffer-name
4840 (if (stringp match)
4841 (format "*Org Agenda(%s:%s)*"
4842 (or org-keys (or (and todo-only "M") "m")) match)
4843 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4844 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4845 ;; expanding tags within `org-make-tags-matcher'
4846 (org-agenda-prepare (concat "TAGS " match))
4847 (setq org--matcher-tags-todo-only todo-only
4848 matcher (org-make-tags-matcher match)
4849 match (car matcher)
4850 matcher (cdr matcher))
4851 (org-compile-prefix-format 'tags)
4852 (org-set-sorting-strategy 'tags)
4853 (setq org-agenda-query-string match)
4854 (setq org-agenda-redo-command
4855 (list 'org-tags-view
4856 `(quote ,org--matcher-tags-todo-only)
4857 `(if current-prefix-arg nil ,org-agenda-query-string)))
4858 (setq files (org-agenda-files nil 'ifmode)
4859 rtnall nil)
4860 (while (setq file (pop files))
4861 (catch 'nextfile
4862 (org-check-agenda-file file)
4863 (setq buffer (if (file-exists-p file)
4864 (org-get-agenda-file-buffer file)
4865 (error "No such file %s" file)))
4866 (if (not buffer)
4867 ;; If file does not exist, error message to agenda
4868 (setq rtn (list
4869 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4870 rtnall (append rtnall rtn))
4871 (with-current-buffer buffer
4872 (unless (derived-mode-p 'org-mode)
4873 (error "Agenda file %s is not in `org-mode'" file))
4874 (save-excursion
4875 (save-restriction
4876 (if (eq buffer org-agenda-restrict)
4877 (narrow-to-region org-agenda-restrict-begin
4878 org-agenda-restrict-end)
4879 (widen))
4880 (setq rtn (org-scan-tags 'agenda
4881 matcher
4882 org--matcher-tags-todo-only))
4883 (setq rtnall (append rtnall rtn))))))))
4884 (if org-agenda-overriding-header
4885 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4886 nil 'face 'org-agenda-structure) "\n")
4887 (insert "Headlines with TAGS match: ")
4888 (add-text-properties (point-min) (1- (point))
4889 (list 'face 'org-agenda-structure
4890 'short-heading
4891 (concat "Match: " match)))
4892 (setq pos (point))
4893 (insert match "\n")
4894 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4895 (setq pos (point))
4896 (unless org-agenda-multi
4897 (insert (substitute-command-keys
4898 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4899 to search again with new search string\n")))
4900 (add-text-properties pos (1- (point))
4901 (list 'face 'org-agenda-structure)))
4902 (org-agenda-mark-header-line (point-min))
4903 (when rtnall
4904 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4905 (goto-char (point-min))
4906 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4907 (add-text-properties
4908 (point-min) (point-max)
4909 `(org-agenda-type tags
4910 org-last-args (,org--matcher-tags-todo-only ,match)
4911 org-redo-cmd ,org-agenda-redo-command
4912 org-series-cmd ,org-cmd))
4913 (org-agenda-finalize)
4914 (setq buffer-read-only t))))
4916 ;;; Agenda Finding stuck projects
4918 (defvar org-agenda-skip-regexp nil
4919 "Regular expression used in skipping subtrees for the agenda.
4920 This is basically a temporary global variable that can be set and then
4921 used by user-defined selections using `org-agenda-skip-function'.")
4923 (defvar org-agenda-overriding-header nil
4924 "When set during agenda, todo and tags searches it replaces the header.
4925 This variable should not be set directly, but custom commands can bind it
4926 in the options section.")
4928 (defun org-agenda-skip-entry-when-regexp-matches ()
4929 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4930 If yes, it returns the end position of this entry, causing agenda commands
4931 to skip the entry but continuing the search in the subtree. This is a
4932 function that can be put into `org-agenda-skip-function' for the duration
4933 of a command."
4934 (let ((end (save-excursion (org-end-of-subtree t)))
4935 skip)
4936 (save-excursion
4937 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4938 (and skip end)))
4940 (defun org-agenda-skip-subtree-when-regexp-matches ()
4941 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4942 If yes, it returns the end position of this tree, causing agenda commands
4943 to skip this subtree. This is a function that can be put into
4944 `org-agenda-skip-function' for the duration of a command."
4945 (let ((end (save-excursion (org-end-of-subtree t)))
4946 skip)
4947 (save-excursion
4948 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4949 (and skip end)))
4951 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4952 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4953 If yes, it returns the end position of the current entry (NOT the tree),
4954 causing agenda commands to skip the entry but continuing the search in
4955 the subtree. This is a function that can be put into
4956 `org-agenda-skip-function' for the duration of a command. An important
4957 use of this function is for the stuck project list."
4958 (let ((end (save-excursion (org-end-of-subtree t)))
4959 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4960 skip)
4961 (save-excursion
4962 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4963 (and skip entry-end)))
4965 (defun org-agenda-skip-entry-if (&rest conditions)
4966 "Skip entry if any of CONDITIONS is true.
4967 See `org-agenda-skip-if' for details."
4968 (org-agenda-skip-if nil conditions))
4970 (defun org-agenda-skip-subtree-if (&rest conditions)
4971 "Skip subtree if any of CONDITIONS is true.
4972 See `org-agenda-skip-if' for details."
4973 (org-agenda-skip-if t conditions))
4975 (defun org-agenda-skip-if (subtree conditions)
4976 "Checks current entity for CONDITIONS.
4977 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4978 the entry (i.e. the text before the next heading) is checked.
4980 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4981 from different tests. Valid conditions are:
4983 scheduled Check if there is a scheduled cookie
4984 notscheduled Check if there is no scheduled cookie
4985 deadline Check if there is a deadline
4986 notdeadline Check if there is no deadline
4987 timestamp Check if there is a timestamp (also deadline or scheduled)
4988 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4989 regexp Check if regexp matches
4990 notregexp Check if regexp does not match.
4991 todo Check if TODO keyword matches
4992 nottodo Check if TODO keyword does not match
4994 The regexp is taken from the conditions list, it must come right after
4995 the `regexp' or `notregexp' element.
4997 `todo' and `nottodo' accept as an argument a list of todo
4998 keywords, which may include \"*\" to match any todo keyword.
5000 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
5002 would skip all entries with \"TODO\" or \"WAITING\" keywords.
5004 Instead of a list, a keyword class may be given. For example:
5006 (org-agenda-skip-entry-if \\='nottodo \\='done)
5008 would skip entries that haven't been marked with any of \"DONE\"
5009 keywords. Possible classes are: `todo', `done', `any'.
5011 If any of these conditions is met, this function returns the end point of
5012 the entity, causing the search to continue from there. This is a function
5013 that can be put into `org-agenda-skip-function' for the duration of a command."
5014 (let (beg end m)
5015 (org-back-to-heading t)
5016 (setq beg (point)
5017 end (if subtree
5018 (progn (org-end-of-subtree t) (point))
5019 (progn (outline-next-heading) (1- (point)))))
5020 (goto-char beg)
5021 (and
5023 (and (memq 'scheduled conditions)
5024 (re-search-forward org-scheduled-time-regexp end t))
5025 (and (memq 'notscheduled conditions)
5026 (not (re-search-forward org-scheduled-time-regexp end t)))
5027 (and (memq 'deadline conditions)
5028 (re-search-forward org-deadline-time-regexp end t))
5029 (and (memq 'notdeadline conditions)
5030 (not (re-search-forward org-deadline-time-regexp end t)))
5031 (and (memq 'timestamp conditions)
5032 (re-search-forward org-ts-regexp end t))
5033 (and (memq 'nottimestamp conditions)
5034 (not (re-search-forward org-ts-regexp end t)))
5035 (and (setq m (memq 'regexp conditions))
5036 (stringp (nth 1 m))
5037 (re-search-forward (nth 1 m) end t))
5038 (and (setq m (memq 'notregexp conditions))
5039 (stringp (nth 1 m))
5040 (not (re-search-forward (nth 1 m) end t)))
5041 (and (or
5042 (setq m (memq 'nottodo conditions))
5043 (setq m (memq 'todo-unblocked conditions))
5044 (setq m (memq 'nottodo-unblocked conditions))
5045 (setq m (memq 'todo conditions)))
5046 (org-agenda-skip-if-todo m end)))
5047 end)))
5049 (defun org-agenda-skip-if-todo (args end)
5050 "Helper function for `org-agenda-skip-if', do not use it directly.
5051 ARGS is a list with first element either `todo', `nottodo',
5052 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5053 a list of TODO keywords, or a state symbol `todo' or `done' or
5054 `any'."
5055 (let ((kw (car args))
5056 (arg (cadr args))
5057 todo-wds todo-re)
5058 (setq todo-wds
5059 (org-uniquify
5060 (cond
5061 ((listp arg) ;; list of keywords
5062 (if (member "*" arg)
5063 (mapcar 'substring-no-properties org-todo-keywords-1)
5064 arg))
5065 ((symbolp arg) ;; keyword class name
5066 (cond
5067 ((eq arg 'todo)
5068 (org-delete-all org-done-keywords
5069 (mapcar 'substring-no-properties
5070 org-todo-keywords-1)))
5071 ((eq arg 'done) org-done-keywords)
5072 ((eq arg 'any)
5073 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5074 (setq todo-re
5075 (concat "^\\*+[ \t]+\\<\\("
5076 (mapconcat 'identity todo-wds "\\|")
5077 "\\)\\>"))
5078 (cond
5079 ((eq kw 'todo) (re-search-forward todo-re end t))
5080 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5081 ((eq kw 'todo-unblocked)
5082 (catch 'unblocked
5083 (while (re-search-forward todo-re end t)
5084 (or (org-entry-blocked-p) (throw 'unblocked t)))
5085 nil))
5086 ((eq kw 'nottodo-unblocked)
5087 (catch 'unblocked
5088 (while (re-search-forward todo-re end t)
5089 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5093 ;;;###autoload
5094 (defun org-agenda-list-stuck-projects (&rest ignore)
5095 "Create agenda view for projects that are stuck.
5096 Stuck projects are project that have no next actions. For the definitions
5097 of what a project is and how to check if it stuck, customize the variable
5098 `org-stuck-projects'."
5099 (interactive)
5100 (let* ((org-agenda-skip-function
5101 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5102 ;; We could have used org-agenda-skip-if here.
5103 (org-agenda-overriding-header
5104 (or org-agenda-overriding-header "List of stuck projects: "))
5105 (matcher (nth 0 org-stuck-projects))
5106 (todo (nth 1 org-stuck-projects))
5107 (todo-wds (if (member "*" todo)
5108 (progn
5109 (org-agenda-prepare-buffers (org-agenda-files
5110 nil 'ifmode))
5111 (org-delete-all
5112 org-done-keywords-for-agenda
5113 (copy-sequence org-todo-keywords-for-agenda)))
5114 todo))
5115 (todo-re (concat "^\\*+[ \t]+\\("
5116 (mapconcat 'identity todo-wds "\\|")
5117 "\\)\\>"))
5118 (tags (nth 2 org-stuck-projects))
5119 (tags-re (if (member "*" tags)
5120 (concat org-outline-regexp-bol
5121 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5122 (if tags
5123 (concat org-outline-regexp-bol
5124 ".*:\\("
5125 (mapconcat #'identity tags "\\|")
5126 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5127 (gen-re (nth 3 org-stuck-projects))
5128 (re-list
5129 (delq nil
5130 (list
5131 (if todo todo-re)
5132 (if tags tags-re)
5133 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5134 gen-re)))))
5135 (setq org-agenda-skip-regexp
5136 (if re-list
5137 (mapconcat 'identity re-list "\\|")
5138 (error "No information how to identify unstuck projects")))
5139 (org-tags-view nil matcher)
5140 (setq org-agenda-buffer-name (buffer-name))
5141 (with-current-buffer org-agenda-buffer-name
5142 (setq org-agenda-redo-command
5143 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5145 ;;; Diary integration
5147 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5148 (defvar diary-list-entries-hook)
5149 (defvar diary-time-regexp)
5150 (defun org-get-entries-from-diary (date)
5151 "Get the (Emacs Calendar) diary entries for DATE."
5152 (require 'diary-lib)
5153 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5154 (diary-display-function 'diary-fancy-display)
5155 (pop-up-frames nil)
5156 (diary-list-entries-hook
5157 (cons 'org-diary-default-entry diary-list-entries-hook))
5158 (diary-file-name-prefix nil) ; turn this feature off
5159 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5160 entries
5161 (org-disable-agenda-to-diary t))
5162 (save-excursion
5163 (save-window-excursion
5164 (funcall (if (fboundp 'diary-list-entries)
5165 'diary-list-entries 'list-diary-entries)
5166 date 1)))
5167 (if (not (get-buffer diary-fancy-buffer))
5168 (setq entries nil)
5169 (with-current-buffer diary-fancy-buffer
5170 (setq buffer-read-only nil)
5171 (if (zerop (buffer-size))
5172 ;; No entries
5173 (setq entries nil)
5174 ;; Omit the date and other unnecessary stuff
5175 (org-agenda-cleanup-fancy-diary)
5176 ;; Add prefix to each line and extend the text properties
5177 (if (zerop (buffer-size))
5178 (setq entries nil)
5179 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5180 (setq entries
5181 (with-temp-buffer
5182 (insert entries) (goto-char (point-min))
5183 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5184 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5185 (replace-match (concat "; " (match-string 1)))))
5186 (buffer-string)))))
5187 (set-buffer-modified-p nil)
5188 (kill-buffer diary-fancy-buffer)))
5189 (when entries
5190 (setq entries (org-split-string entries "\n"))
5191 (setq entries
5192 (mapcar
5193 (lambda (x)
5194 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5195 ;; Extend the text properties to the beginning of the line
5196 (org-add-props x (text-properties-at (1- (length x)) x)
5197 'type "diary" 'date date 'face 'org-agenda-diary))
5198 entries)))))
5200 (defvar org-agenda-cleanup-fancy-diary-hook nil
5201 "Hook run when the fancy diary buffer is cleaned up.")
5203 (defun org-agenda-cleanup-fancy-diary ()
5204 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5205 This gets rid of the date, the underline under the date, and
5206 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5207 date. It also removes lines that contain only whitespace."
5208 (goto-char (point-min))
5209 (if (looking-at ".*?:[ \t]*")
5210 (progn
5211 (replace-match "")
5212 (re-search-forward "\n=+$" nil t)
5213 (replace-match "")
5214 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5215 (re-search-forward "\n=+$" nil t)
5216 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5217 (goto-char (point-min))
5218 (while (re-search-forward "^ +\n" nil t)
5219 (replace-match ""))
5220 (goto-char (point-min))
5221 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5222 (replace-match ""))
5223 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5225 ;; Make sure entries from the diary have the right text properties.
5226 (eval-after-load "diary-lib"
5227 '(if (boundp 'diary-modify-entry-list-string-function)
5228 ;; We can rely on the hook, nothing to do
5230 ;; Hook not available, must use advice to make this work
5231 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5232 "Make the position visible."
5233 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5234 (stringp string)
5235 buffer-file-name)
5236 (setq string (org-modify-diary-entry-string string))))))
5238 (defun org-modify-diary-entry-string (string)
5239 "Add text properties to string, allowing org-mode to act on it."
5240 (org-add-props string nil
5241 'mouse-face 'highlight
5242 'help-echo (if buffer-file-name
5243 (format "mouse-2 or RET jump to diary file %s"
5244 (abbreviate-file-name buffer-file-name))
5246 'org-agenda-diary-link t
5247 'org-marker (org-agenda-new-marker (point-at-bol))))
5249 (defun org-diary-default-entry ()
5250 "Add a dummy entry to the diary.
5251 Needed to avoid empty dates which mess up holiday display."
5252 ;; Catch the error if dealing with the new add-to-diary-alist
5253 (when org-disable-agenda-to-diary
5254 (condition-case nil
5255 (org-add-to-diary-list original-date "Org-mode dummy" "")
5256 (error
5257 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5259 (defun org-add-to-diary-list (&rest args)
5260 (if (fboundp 'diary-add-to-list)
5261 (apply 'diary-add-to-list args)
5262 (apply 'add-to-diary-list args)))
5264 (defvar org-diary-last-run-time nil)
5266 ;;;###autoload
5267 (defun org-diary (&rest args)
5268 "Return diary information from org files.
5269 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5270 It accesses org files and extracts information from those files to be
5271 listed in the diary. The function accepts arguments specifying what
5272 items should be listed. For a list of arguments allowed here, see the
5273 variable `org-agenda-entry-types'.
5275 The call in the diary file should look like this:
5277 &%%(org-diary) ~/path/to/some/orgfile.org
5279 Use a separate line for each org file to check. Or, if you omit the file name,
5280 all files listed in `org-agenda-files' will be checked automatically:
5282 &%%(org-diary)
5284 If you don't give any arguments (as in the example above), the default value
5285 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5286 So the example above may also be written as
5288 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5290 The function expects the lisp variables `entry' and `date' to be provided
5291 by the caller, because this is how the calendar works. Don't use this
5292 function from a program - use `org-agenda-get-day-entries' instead."
5293 (when (> (- (float-time)
5294 org-agenda-last-marker-time)
5296 ;; I am not sure if this works with sticky agendas, because the marker
5297 ;; list is then no longer a global variable.
5298 (org-agenda-reset-markers))
5299 (org-compile-prefix-format 'agenda)
5300 (org-set-sorting-strategy 'agenda)
5301 (setq args (or args org-agenda-entry-types))
5302 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5303 (list entry)
5304 (org-agenda-files t)))
5305 (time (float-time))
5306 file rtn results)
5307 (when (or (not org-diary-last-run-time)
5308 (> (- time
5309 org-diary-last-run-time)
5311 (org-agenda-prepare-buffers files))
5312 (setq org-diary-last-run-time time)
5313 ;; If this is called during org-agenda, don't return any entries to
5314 ;; the calendar. Org Agenda will list these entries itself.
5315 (if org-disable-agenda-to-diary (setq files nil))
5316 (while (setq file (pop files))
5317 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5318 (setq results (append results rtn)))
5319 (when results
5320 (setq results
5321 (mapcar (lambda (i) (replace-regexp-in-string
5322 org-bracket-link-regexp "\\3" i)) results))
5323 (concat (org-agenda-finalize-entries results) "\n"))))
5325 ;;; Agenda entry finders
5327 (defun org-agenda--timestamp-to-absolute (&rest args)
5328 "Call `org-time-string-to-absolute' with ARGS.
5329 However, throw `:skip' whenever an error is raised."
5330 (condition-case e
5331 (apply #'org-time-string-to-absolute args)
5332 (org-diary-sexp-no-match (throw :skip nil))
5333 (error
5334 (message "%s; Skipping entry" (error-message-string e))
5335 (throw :skip nil))))
5337 (defun org-agenda-get-day-entries (file date &rest args)
5338 "Does the work for `org-diary' and `org-agenda'.
5339 FILE is the path to a file to be checked for entries. DATE is date like
5340 the one returned by `calendar-current-date'. ARGS are symbols indicating
5341 which kind of entries should be extracted. For details about these, see
5342 the documentation of `org-diary'."
5343 (let* ((org-startup-folded nil)
5344 (org-startup-align-all-tables nil)
5345 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5346 (error "No such file %s" file))))
5347 (if (not buffer)
5348 ;; If file does not exist, signal it in diary nonetheless.
5349 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5350 (with-current-buffer buffer
5351 (unless (derived-mode-p 'org-mode)
5352 (error "Agenda file %s is not in `org-mode'" file))
5353 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5354 (setf org-agenda-current-date date)
5355 (save-excursion
5356 (save-restriction
5357 (if (eq buffer org-agenda-restrict)
5358 (narrow-to-region org-agenda-restrict-begin
5359 org-agenda-restrict-end)
5360 (widen))
5361 ;; Rationalize ARGS. Also make sure `:deadline' comes
5362 ;; first in order to populate DEADLINES before passing it.
5364 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5365 ;; guarding us from modifying `org-agenda-entry-types'.
5366 (setf args (org-uniquify (or args org-agenda-entry-types)))
5367 (when (and (memq :scheduled args) (memq :scheduled* args))
5368 (setf args (delq :scheduled* args)))
5369 (cond
5370 ((memq :deadline args)
5371 (setf args (cons :deadline
5372 (delq :deadline (delq :deadline* args)))))
5373 ((memq :deadline* args)
5374 (setf args (cons :deadline* (delq :deadline* args)))))
5375 ;; Collect list of headlines. Return them flattened.
5376 (let ((case-fold-search nil) results deadlines)
5377 (dolist (arg args (apply #'nconc (nreverse results)))
5378 (pcase arg
5379 ((and :todo (guard (org-agenda-today-p date)))
5380 (push (org-agenda-get-todos) results))
5381 (:timestamp
5382 (push (org-agenda-get-blocks) results)
5383 (push (org-agenda-get-timestamps deadlines) results))
5384 (:sexp
5385 (push (org-agenda-get-sexps) results))
5386 (:scheduled
5387 (push (org-agenda-get-scheduled deadlines) results))
5388 (:scheduled*
5389 (push (org-agenda-get-scheduled deadlines t) results))
5390 (:closed
5391 (push (org-agenda-get-progress) results))
5392 (:deadline
5393 (setf deadlines (org-agenda-get-deadlines))
5394 (push deadlines results))
5395 (:deadline*
5396 (setf deadlines (org-agenda-get-deadlines t))
5397 (push deadlines results)))))))))))
5399 (defsubst org-em (x y list)
5400 "Is X or Y a member of LIST?"
5401 (or (memq x list) (memq y list)))
5403 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5404 (defvar org-agenda-sorting-strategy-selected nil)
5406 (defun org-agenda-entry-get-agenda-timestamp (pom)
5407 "Retrieve timestamp information for sorting agenda views.
5408 Given a point or marker POM, returns a cons cell of the timestamp
5409 and the timestamp type relevant for the sorting strategy in
5410 `org-agenda-sorting-strategy-selected'."
5411 (let (ts ts-date-type)
5412 (save-match-data
5413 (cond ((org-em 'scheduled-up 'scheduled-down
5414 org-agenda-sorting-strategy-selected)
5415 (setq ts (org-entry-get pom "SCHEDULED")
5416 ts-date-type " scheduled"))
5417 ((org-em 'deadline-up 'deadline-down
5418 org-agenda-sorting-strategy-selected)
5419 (setq ts (org-entry-get pom "DEADLINE")
5420 ts-date-type " deadline"))
5421 ((org-em 'ts-up 'ts-down
5422 org-agenda-sorting-strategy-selected)
5423 (setq ts (org-entry-get pom "TIMESTAMP")
5424 ts-date-type " timestamp"))
5425 ((org-em 'tsia-up 'tsia-down
5426 org-agenda-sorting-strategy-selected)
5427 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5428 ts-date-type " timestamp_ia"))
5429 ((org-em 'timestamp-up 'timestamp-down
5430 org-agenda-sorting-strategy-selected)
5431 (setq ts (or (org-entry-get pom "SCHEDULED")
5432 (org-entry-get pom "DEADLINE")
5433 (org-entry-get pom "TIMESTAMP")
5434 (org-entry-get pom "TIMESTAMP_IA"))
5435 ts-date-type ""))
5436 (t (setq ts-date-type "")))
5437 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5438 ts-date-type))))
5440 (defun org-agenda-get-todos ()
5441 "Return the TODO information for agenda display."
5442 (let* ((props (list 'face nil
5443 'done-face 'org-agenda-done
5444 'org-not-done-regexp org-not-done-regexp
5445 'org-todo-regexp org-todo-regexp
5446 'org-complex-heading-regexp org-complex-heading-regexp
5447 'mouse-face 'highlight
5448 'help-echo
5449 (format "mouse-2 or RET jump to org file %s"
5450 (abbreviate-file-name buffer-file-name))))
5451 (regexp (format org-heading-keyword-regexp-format
5452 (cond
5453 ((and org-select-this-todo-keyword
5454 (equal org-select-this-todo-keyword "*"))
5455 org-todo-regexp)
5456 (org-select-this-todo-keyword
5457 (concat "\\("
5458 (mapconcat 'identity
5459 (org-split-string
5460 org-select-this-todo-keyword
5461 "|")
5462 "\\|") "\\)"))
5463 (t org-not-done-regexp))))
5464 marker priority category level tags todo-state
5465 ts-date ts-date-type ts-date-pair
5466 ee txt beg end inherited-tags todo-state-end-pos)
5467 (goto-char (point-min))
5468 (while (re-search-forward regexp nil t)
5469 (catch :skip
5470 (save-match-data
5471 (beginning-of-line)
5472 (org-agenda-skip)
5473 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5474 (unless (and (setq todo-state (org-get-todo-state))
5475 (setq todo-state-end-pos (match-end 2)))
5476 (goto-char end)
5477 (throw :skip nil))
5478 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5479 (goto-char (1+ beg))
5480 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5481 (throw :skip nil)))
5482 (goto-char (match-beginning 2))
5483 (setq marker (org-agenda-new-marker (match-beginning 0))
5484 category (org-get-category)
5485 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5486 ts-date (car ts-date-pair)
5487 ts-date-type (cdr ts-date-pair)
5488 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5489 inherited-tags
5490 (or (eq org-agenda-show-inherited-tags 'always)
5491 (and (listp org-agenda-show-inherited-tags)
5492 (memq 'todo org-agenda-show-inherited-tags))
5493 (and (eq org-agenda-show-inherited-tags t)
5494 (or (eq org-agenda-use-tag-inheritance t)
5495 (memq 'todo org-agenda-use-tag-inheritance))))
5496 tags (org-get-tags-at nil (not inherited-tags))
5497 level (make-string (org-reduced-level (org-outline-level)) ? )
5498 txt (org-agenda-format-item "" txt level category tags t)
5499 priority (1+ (org-get-priority txt)))
5500 (org-add-props txt props
5501 'org-marker marker 'org-hd-marker marker
5502 'priority priority
5503 'level level
5504 'ts-date ts-date
5505 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5506 (push txt ee)
5507 (if org-agenda-todo-list-sublevels
5508 (goto-char todo-state-end-pos)
5509 (org-end-of-subtree 'invisible))))
5510 (nreverse ee)))
5512 (defun org-agenda-todo-custom-ignore-p (time n)
5513 "Check whether timestamp is farther away than n number of days.
5514 This function is invoked if `org-agenda-todo-ignore-deadlines',
5515 `org-agenda-todo-ignore-scheduled' or
5516 `org-agenda-todo-ignore-timestamp' is set to an integer."
5517 (let ((days (org-time-stamp-to-now
5518 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5519 (if (>= n 0)
5520 (>= days n)
5521 (<= days n))))
5523 ;;;###autoload
5524 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5525 (&optional end)
5526 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5527 (when (or org-agenda-todo-ignore-with-date
5528 org-agenda-todo-ignore-scheduled
5529 org-agenda-todo-ignore-deadlines
5530 org-agenda-todo-ignore-timestamp)
5531 (setq end (or end (save-excursion (outline-next-heading) (point))))
5532 (save-excursion
5533 (or (and org-agenda-todo-ignore-with-date
5534 (re-search-forward org-ts-regexp end t))
5535 (and org-agenda-todo-ignore-scheduled
5536 (re-search-forward org-scheduled-time-regexp end t)
5537 (cond
5538 ((eq org-agenda-todo-ignore-scheduled 'future)
5539 (> (org-time-stamp-to-now
5540 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5541 ((eq org-agenda-todo-ignore-scheduled 'past)
5542 (<= (org-time-stamp-to-now
5543 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5544 ((numberp org-agenda-todo-ignore-scheduled)
5545 (org-agenda-todo-custom-ignore-p
5546 (match-string 1) org-agenda-todo-ignore-scheduled))
5547 (t)))
5548 (and org-agenda-todo-ignore-deadlines
5549 (re-search-forward org-deadline-time-regexp end t)
5550 (cond
5551 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5552 ((eq org-agenda-todo-ignore-deadlines 'far)
5553 (not (org-deadline-close (match-string 1))))
5554 ((eq org-agenda-todo-ignore-deadlines 'future)
5555 (> (org-time-stamp-to-now
5556 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5557 ((eq org-agenda-todo-ignore-deadlines 'past)
5558 (<= (org-time-stamp-to-now
5559 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5560 ((numberp org-agenda-todo-ignore-deadlines)
5561 (org-agenda-todo-custom-ignore-p
5562 (match-string 1) org-agenda-todo-ignore-deadlines))
5563 (t (org-deadline-close (match-string 1)))))
5564 (and org-agenda-todo-ignore-timestamp
5565 (let ((buffer (current-buffer))
5566 (regexp
5567 (concat
5568 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5569 (start (point)))
5570 ;; Copy current buffer into a temporary one
5571 (with-temp-buffer
5572 (insert-buffer-substring buffer start end)
5573 (goto-char (point-min))
5574 ;; Delete SCHEDULED and DEADLINE items
5575 (while (re-search-forward regexp end t)
5576 (delete-region (match-beginning 0) (match-end 0)))
5577 (goto-char (point-min))
5578 ;; No search for timestamp left
5579 (when (re-search-forward org-ts-regexp nil t)
5580 (cond
5581 ((eq org-agenda-todo-ignore-timestamp 'future)
5582 (> (org-time-stamp-to-now
5583 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5584 ((eq org-agenda-todo-ignore-timestamp 'past)
5585 (<= (org-time-stamp-to-now
5586 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5587 ((numberp org-agenda-todo-ignore-timestamp)
5588 (org-agenda-todo-custom-ignore-p
5589 (match-string 1) org-agenda-todo-ignore-timestamp))
5590 (t))))))))))
5592 (defun org-agenda-get-timestamps (&optional deadline-results)
5593 "Return the date stamp information for agenda display."
5594 (let* ((props (list 'face 'org-agenda-calendar-event
5595 'org-not-done-regexp org-not-done-regexp
5596 'org-todo-regexp org-todo-regexp
5597 'org-complex-heading-regexp org-complex-heading-regexp
5598 'mouse-face 'highlight
5599 'help-echo
5600 (format "mouse-2 or RET jump to org file %s"
5601 (abbreviate-file-name buffer-file-name))))
5602 (d1 (calendar-absolute-from-gregorian date))
5604 (deadline-position-alist
5605 (mapcar (lambda (a) (and (setq mm (get-text-property
5606 0 'org-hd-marker a))
5607 (cons (marker-position mm) a)))
5608 deadline-results))
5609 (remove-re org-ts-regexp)
5610 (regexp
5611 (concat
5612 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5613 (regexp-quote
5614 (substring
5615 (format-time-string
5616 (car org-time-stamp-formats)
5617 (apply 'encode-time ; DATE bound by calendar
5618 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5619 1 11))
5620 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5621 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5622 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5623 donep tmp priority category level ee txt timestr tags
5624 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5625 inherited-tags ts-date)
5626 (goto-char (point-min))
5627 (while (setq end-of-match (re-search-forward regexp nil t))
5628 (setq b0 (match-beginning 0)
5629 b3 (match-beginning 3) e3 (match-end 3)
5630 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5631 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5632 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5633 (member todo-state
5634 org-agenda-repeating-timestamp-show-all)))
5635 (catch :skip
5636 (and (org-at-date-range-p) (throw :skip nil))
5637 (org-agenda-skip)
5638 (if (and (match-end 1)
5639 (not (= d1 (org-agenda--timestamp-to-absolute
5640 (match-string 1) d1 nil show-all
5641 (current-buffer) b0))))
5642 (throw :skip nil))
5643 (if (and e3
5644 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5645 (throw :skip nil))
5646 (setq tmp (buffer-substring (max (point-min)
5647 (- b0 org-ds-keyword-length))
5649 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5650 inactivep (= (char-after b0) ?\[)
5651 deadlinep (string-match org-deadline-regexp tmp)
5652 scheduledp (string-match org-scheduled-regexp tmp)
5653 closedp (and org-agenda-include-inactive-timestamps
5654 (string-match org-closed-string tmp))
5655 clockp (and org-agenda-include-inactive-timestamps
5656 (or (string-match org-clock-string tmp)
5657 (string-match "]-+\\'" tmp)))
5658 warntime (get-text-property (point) 'org-appt-warntime)
5659 donep (member todo-state org-done-keywords))
5660 (if (or scheduledp deadlinep closedp clockp
5661 (and donep org-agenda-skip-timestamp-if-done))
5662 (throw :skip t))
5663 (if (string-match ">" timestr)
5664 ;; substring should only run to end of time stamp
5665 (setq timestr (substring timestr 0 (match-end 0))))
5666 (setq marker (org-agenda-new-marker b0)
5667 category (org-get-category b0))
5668 (save-excursion
5669 (if (not (re-search-backward org-outline-regexp-bol nil t))
5670 (throw :skip nil)
5671 (goto-char (match-beginning 0))
5672 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5673 (assoc (point) deadline-position-alist))
5674 (throw :skip nil))
5675 (setq hdmarker (org-agenda-new-marker)
5676 inherited-tags
5677 (or (eq org-agenda-show-inherited-tags 'always)
5678 (and (listp org-agenda-show-inherited-tags)
5679 (memq 'agenda org-agenda-show-inherited-tags))
5680 (and (eq org-agenda-show-inherited-tags t)
5681 (or (eq org-agenda-use-tag-inheritance t)
5682 (memq 'agenda org-agenda-use-tag-inheritance))))
5683 tags (org-get-tags-at nil (not inherited-tags))
5684 level (make-string (org-reduced-level (org-outline-level)) ? ))
5685 (looking-at "\\*+[ \t]+\\(.*\\)")
5686 (setq head (match-string 1))
5687 (setq txt (org-agenda-format-item
5688 (if inactivep org-agenda-inactive-leader nil)
5689 head level category tags timestr
5690 remove-re habitp)))
5691 (setq priority (org-get-priority txt))
5692 (org-add-props txt props 'priority priority
5693 'org-marker marker 'org-hd-marker hdmarker
5694 'date date
5695 'level level
5696 'ts-date
5697 (ignore-errors (org-time-string-to-absolute timestr))
5698 'todo-state todo-state
5699 'warntime warntime
5700 'type "timestamp")
5701 (push txt ee))
5702 (if org-agenda-skip-additional-timestamps-same-entry
5703 (outline-next-heading)
5704 (goto-char end-of-match))))
5705 (nreverse ee)))
5707 (defun org-agenda-get-sexps ()
5708 "Return the sexp information for agenda display."
5709 (require 'diary-lib)
5710 (let* ((props (list 'face 'org-agenda-calendar-sexp
5711 'mouse-face 'highlight
5712 'help-echo
5713 (format "mouse-2 or RET jump to org file %s"
5714 (abbreviate-file-name buffer-file-name))))
5715 (regexp "^&?%%(")
5716 marker category extra level ee txt tags entry
5717 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5718 (goto-char (point-min))
5719 (while (re-search-forward regexp nil t)
5720 (catch :skip
5721 (org-agenda-skip)
5722 (setq beg (match-beginning 0))
5723 (goto-char (1- (match-end 0)))
5724 (setq b (point))
5725 (forward-sexp 1)
5726 (setq sexp (buffer-substring b (point)))
5727 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5728 (org-trim (match-string 1))
5729 ""))
5730 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5731 (when result
5732 (setq marker (org-agenda-new-marker beg)
5733 level (make-string (org-reduced-level (org-outline-level)) ? )
5734 category (org-get-category beg)
5735 inherited-tags
5736 (or (eq org-agenda-show-inherited-tags 'always)
5737 (and (listp org-agenda-show-inherited-tags)
5738 (memq 'agenda org-agenda-show-inherited-tags))
5739 (and (eq org-agenda-show-inherited-tags t)
5740 (or (eq org-agenda-use-tag-inheritance t)
5741 (memq 'agenda org-agenda-use-tag-inheritance))))
5742 tags (org-get-tags-at nil (not inherited-tags))
5743 todo-state (org-get-todo-state)
5744 warntime (get-text-property (point) 'org-appt-warntime)
5745 extra nil)
5747 (dolist (r (if (stringp result)
5748 (list result)
5749 result)) ;; we expect a list here
5750 (when (and org-agenda-diary-sexp-prefix
5751 (string-match org-agenda-diary-sexp-prefix r))
5752 (setq extra (match-string 0 r)
5753 r (replace-match "" nil nil r)))
5754 (if (string-match "\\S-" r)
5755 (setq txt r)
5756 (setq txt "SEXP entry returned empty string"))
5757 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5758 (org-add-props txt props 'org-marker marker
5759 'date date 'todo-state todo-state
5760 'level level 'type "sexp" 'warntime warntime)
5761 (push txt ee)))))
5762 (nreverse ee)))
5764 ;; Calendar sanity: define some functions that are independent of
5765 ;; `calendar-date-style'.
5766 ;; Normally I would like to use ISO format when calling the diary functions,
5767 ;; but to make sure we still have Emacs 22 compatibility we bind
5768 ;; also `european-calendar-style' and use european format
5769 (defun org-anniversary (year month day &optional mark)
5770 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5771 (org-no-warnings
5772 (let ((calendar-date-style 'european) (european-calendar-style t))
5773 (diary-anniversary day month year mark))))
5774 (defun org-cyclic (N year month day &optional mark)
5775 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5776 (org-no-warnings
5777 (let ((calendar-date-style 'european) (european-calendar-style t))
5778 (diary-cyclic N day month year mark))))
5779 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5780 "Like `diary-block', but with fixed (ISO) order of arguments."
5781 (org-no-warnings
5782 (let ((calendar-date-style 'european) (european-calendar-style t))
5783 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5784 (defun org-date (year month day &optional mark)
5785 "Like `diary-date', but with fixed (ISO) order of arguments."
5786 (org-no-warnings
5787 (let ((calendar-date-style 'european) (european-calendar-style t))
5788 (diary-date day month year mark))))
5790 ;; Define the `org-class' function
5791 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5792 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5793 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5794 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5795 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5796 `holidays', then any date that is known by the Emacs calendar to be a
5797 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5798 then those holidays will be skipped."
5799 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5800 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5801 (d (calendar-absolute-from-gregorian date))
5802 (h (when skip-weeks (calendar-check-holidays date))))
5803 (and
5804 (<= date1 d)
5805 (<= d date2)
5806 (= (calendar-day-of-week date) dayname)
5807 (or (not skip-weeks)
5808 (progn
5809 (require 'cal-iso)
5810 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5811 (not (or (and h (memq 'holidays skip-weeks))
5812 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5813 entry)))
5815 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5816 "Like `org-class', but honor `calendar-date-style'.
5817 The order of the first 2 times 3 arguments depends on the variable
5818 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5819 So for American calendars, give this as MONTH DAY YEAR, for European as
5820 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5821 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5822 is any number of ISO weeks in the block period for which the item should
5823 be skipped.
5825 This function is here only for backward compatibility and it is deprecated,
5826 please use `org-class' instead."
5827 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5828 (date2 (org-order-calendar-date-args m2 d2 y2)))
5829 (org-class
5830 (nth 2 date1) (car date1) (nth 1 date1)
5831 (nth 2 date2) (car date2) (nth 1 date2)
5832 dayname skip-weeks)))
5833 (make-obsolete 'org-diary-class 'org-class "")
5835 (defalias 'org-get-closed 'org-agenda-get-progress)
5836 (defun org-agenda-get-progress ()
5837 "Return the logged TODO entries for agenda display."
5838 (let* ((props (list 'mouse-face 'highlight
5839 'org-not-done-regexp org-not-done-regexp
5840 'org-todo-regexp org-todo-regexp
5841 'org-complex-heading-regexp org-complex-heading-regexp
5842 'help-echo
5843 (format "mouse-2 or RET jump to org file %s"
5844 (abbreviate-file-name buffer-file-name))))
5845 (items (if (consp org-agenda-show-log-scoped)
5846 org-agenda-show-log-scoped
5847 (if (eq org-agenda-show-log-scoped 'clockcheck)
5848 '(clock)
5849 org-agenda-log-mode-items)))
5850 (parts
5851 (delq nil
5852 (list
5853 (if (memq 'closed items) (concat "\\<" org-closed-string))
5854 (if (memq 'clock items) (concat "\\<" org-clock-string))
5855 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5856 (parts-re (if parts (mapconcat 'identity parts "\\|")
5857 (error "`org-agenda-log-mode-items' is empty")))
5858 (regexp (concat
5859 "\\(" parts-re "\\)"
5860 " *\\["
5861 (regexp-quote
5862 (substring
5863 (format-time-string
5864 (car org-time-stamp-formats)
5865 (apply 'encode-time ; DATE bound by calendar
5866 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5867 1 11))))
5868 (org-agenda-search-headline-for-time nil)
5869 marker hdmarker priority category level tags closedp
5870 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5871 (goto-char (point-min))
5872 (while (re-search-forward regexp nil t)
5873 (catch :skip
5874 (org-agenda-skip)
5875 (setq marker (org-agenda-new-marker (match-beginning 0))
5876 closedp (equal (match-string 1) org-closed-string)
5877 statep (equal (string-to-char (match-string 1)) ?-)
5878 clockp (not (or closedp statep))
5879 state (and statep (match-string 2))
5880 category (org-get-category (match-beginning 0))
5881 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5882 (when (string-match "\\]" timestr)
5883 ;; substring should only run to end of time stamp
5884 (setq rest (substring timestr (match-end 0))
5885 timestr (substring timestr 0 (match-end 0)))
5886 (if (and (not closedp) (not statep)
5887 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5888 rest))
5889 (progn (setq timestr (concat (substring timestr 0 -1)
5890 "-" (match-string 1 rest) "]"))
5891 (setq clocked (match-string 2 rest)))
5892 (setq clocked "-")))
5893 (save-excursion
5894 (setq extra
5895 (cond
5896 ((not org-agenda-log-mode-add-notes) nil)
5897 (statep
5898 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5899 (match-string 1)))
5900 (clockp
5901 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5902 (match-string 1)))))
5903 (if (not (re-search-backward org-outline-regexp-bol nil t))
5904 (throw :skip nil)
5905 (goto-char (match-beginning 0))
5906 (setq hdmarker (org-agenda-new-marker)
5907 inherited-tags
5908 (or (eq org-agenda-show-inherited-tags 'always)
5909 (and (listp org-agenda-show-inherited-tags)
5910 (memq 'todo org-agenda-show-inherited-tags))
5911 (and (eq org-agenda-show-inherited-tags t)
5912 (or (eq org-agenda-use-tag-inheritance t)
5913 (memq 'todo org-agenda-use-tag-inheritance))))
5914 tags (org-get-tags-at nil (not inherited-tags))
5915 level (make-string (org-reduced-level (org-outline-level)) ? ))
5916 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5917 (setq txt (match-string 1))
5918 (when extra
5919 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5920 (setq txt (concat (substring txt 0 (match-beginning 1))
5921 " - " extra " " (match-string 2 txt)))
5922 (setq txt (concat txt " - " extra))))
5923 (setq txt (org-agenda-format-item
5924 (cond
5925 (closedp "Closed: ")
5926 (statep (concat "State: (" state ")"))
5927 (t (concat "Clocked: (" clocked ")")))
5928 txt level category tags timestr)))
5929 (setq priority 100000)
5930 (org-add-props txt props
5931 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5932 'priority priority 'level level
5933 'type "closed" 'date date
5934 'undone-face 'org-warning 'done-face 'org-agenda-done)
5935 (push txt ee))
5936 (goto-char (point-at-eol))))
5937 (nreverse ee)))
5939 (defun org-agenda-show-clocking-issues ()
5940 "Add overlays, showing issues with clocking.
5941 See also the user option `org-agenda-clock-consistency-checks'."
5942 (interactive)
5943 (let* ((org-time-clocksum-use-effort-durations nil)
5944 (pl org-agenda-clock-consistency-checks)
5945 (re (concat "^[ \t]*"
5946 org-clock-string
5947 "[ \t]+"
5948 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5949 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5950 (tlstart 0.)
5951 (tlend 0.)
5952 (maxtime (org-hh:mm-string-to-minutes
5953 (or (plist-get pl :max-duration) "24:00")))
5954 (mintime (org-hh:mm-string-to-minutes
5955 (or (plist-get pl :min-duration) 0)))
5956 (maxgap (org-hh:mm-string-to-minutes
5957 ;; default 30:00 means never complain
5958 (or (plist-get pl :max-gap) "30:00")))
5959 (gapok (mapcar 'org-hh:mm-string-to-minutes
5960 (plist-get pl :gap-ok-around)))
5961 (def-face (or (plist-get pl :default-face)
5962 '((:background "DarkRed") (:foreground "white"))))
5963 issue face m te ts dt ov)
5964 (goto-char (point-min))
5965 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5966 (setq issue nil face def-face)
5967 (catch 'next
5968 (setq m (org-get-at-bol 'org-marker)
5969 te nil ts nil)
5970 (unless (and m (markerp m))
5971 (setq issue "No valid clock line") (throw 'next t))
5972 (org-with-point-at m
5973 (save-excursion
5974 (goto-char (point-at-bol))
5975 (unless (looking-at re)
5976 (error "No valid Clock line")
5977 (throw 'next t))
5978 (unless (match-end 3)
5979 (setq issue "No end time"
5980 face (or (plist-get pl :no-end-time-face) face))
5981 (throw 'next t))
5982 (setq ts (match-string 1)
5983 te (match-string 3)
5984 ts (float-time
5985 (apply #'encode-time (org-parse-time-string ts)))
5986 te (float-time
5987 (apply #'encode-time (org-parse-time-string te)))
5988 dt (- te ts))))
5989 (cond
5990 ((> dt (* 60 maxtime))
5991 ;; a very long clocking chunk
5992 (setq issue (format "Clocking interval is very long: %s"
5993 (org-minutes-to-clocksum-string
5994 (floor (/ (float dt) 60.))))
5995 face (or (plist-get pl :long-face) face)))
5996 ((< dt (* 60 mintime))
5997 ;; a very short clocking chunk
5998 (setq issue (format "Clocking interval is very short: %s"
5999 (org-minutes-to-clocksum-string
6000 (floor (/ (float dt) 60.))))
6001 face (or (plist-get pl :short-face) face)))
6002 ((and (> tlend 0) (< ts tlend))
6003 ;; Two clock entries are overlapping
6004 (setq issue (format "Clocking overlap: %d minutes"
6005 (/ (- tlend ts) 60))
6006 face (or (plist-get pl :overlap-face) face)))
6007 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
6008 ;; There is a gap, lets see if we need to report it
6009 (unless (org-agenda-check-clock-gap tlend ts gapok)
6010 (setq issue (format "Clocking gap: %d minutes"
6011 (/ (- ts tlend) 60))
6012 face (or (plist-get pl :gap-face) face))))
6013 (t nil)))
6014 (setq tlend (or te tlend) tlstart (or ts tlstart))
6015 (when issue
6016 ;; OK, there was some issue, add an overlay to show the issue
6017 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
6018 (overlay-put ov 'before-string
6019 (concat
6020 (org-add-props
6021 (format "%-43s" (concat " " issue))
6023 'face face)
6024 "\n"))
6025 (overlay-put ov 'evaporate t)))))
6027 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
6028 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
6029 (catch 'exit
6030 (unless ok-list
6031 ;; there are no OK times for gaps...
6032 (throw 'exit nil))
6033 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
6034 ;; This is more than 24 hours, so it is OK.
6035 ;; because we have at least one OK time, that must be in the
6036 ;; 24 hour interval.
6037 (throw 'exit t))
6038 ;; We have a shorter gap.
6039 ;; Now we have to get the minute of the day when these times are
6040 (let* ((t1dec (decode-time (seconds-to-time t1)))
6041 (t2dec (decode-time (seconds-to-time t2)))
6042 ;; compute the minute on the day
6043 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6044 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6045 (when (< min2 min1)
6046 ;; if min2 is smaller than min1, this means it is on the next day.
6047 ;; Wrap it to after midnight.
6048 (setq min2 (+ min2 1440)))
6049 ;; Now check if any of the OK times is in the gap
6050 (mapc (lambda (x)
6051 ;; Wrap the time to after midnight if necessary
6052 (if (< x min1) (setq x (+ x 1440)))
6053 ;; Check if in interval
6054 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6055 ok-list)
6056 ;; Nope, this gap is not OK
6057 nil)))
6059 (defun org-agenda-get-deadlines (&optional with-hour)
6060 "Return the deadline information for agenda display.
6061 When WITH-HOUR is non-nil, only return deadlines with an hour
6062 specification like [h]h:mm."
6063 (let* ((props (list 'mouse-face 'highlight
6064 'org-not-done-regexp org-not-done-regexp
6065 'org-todo-regexp org-todo-regexp
6066 'org-complex-heading-regexp org-complex-heading-regexp
6067 'help-echo
6068 (format "mouse-2 or RET jump to org file %s"
6069 (abbreviate-file-name buffer-file-name))))
6070 (regexp (if with-hour
6071 org-deadline-time-hour-regexp
6072 org-deadline-time-regexp))
6073 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6074 (current (calendar-absolute-from-gregorian date))
6075 deadline-items)
6076 (goto-char (point-min))
6077 (while (re-search-forward regexp nil t)
6078 (catch :skip
6079 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6080 (org-agenda-skip)
6081 (let* ((s (match-string 1))
6082 (pos (1- (match-beginning 1)))
6083 (todo-state (save-match-data (org-get-todo-state)))
6084 (donep (member todo-state org-done-keywords))
6085 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6086 (member todo-state
6087 org-agenda-repeating-timestamp-show-all)))
6088 ;; DEADLINE is the current scheduled date. When it
6089 ;; contains a repeater and SHOW-ALL is non-nil,
6090 ;; LAST-REPEAT is the repeat closest to CURRENT.
6091 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6092 (last-repeat (org-agenda--timestamp-to-absolute
6093 s current 'past show-all (current-buffer) pos))
6094 (deadline (org-agenda--timestamp-to-absolute s current))
6095 (diff (- last-repeat current))
6096 (suppress-prewarning
6097 (let ((scheduled
6098 (and org-agenda-skip-deadline-prewarning-if-scheduled
6099 (org-entry-get nil "SCHEDULED"))))
6100 (cond
6101 ((not scheduled) nil)
6102 ;; The current item has a scheduled date, so
6103 ;; evaluate its prewarning lead time.
6104 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6105 ;; Use global prewarning-restart lead time.
6106 org-agenda-skip-deadline-prewarning-if-scheduled)
6107 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6108 'pre-scheduled)
6109 ;; Set pre-warning to no earlier than SCHEDULED.
6110 (min (- last-repeat
6111 (org-agenda--timestamp-to-absolute
6112 scheduled current 'past show-all
6113 (current-buffer)
6114 (save-excursion
6115 (beginning-of-line)
6116 (1+ (search-forward org-deadline-string)))))
6117 org-deadline-warning-days))
6118 ;; Set pre-warning to deadline.
6119 (t 0))))
6120 (wdays (if suppress-prewarning
6121 (let ((org-deadline-warning-days suppress-prewarning))
6122 (org-get-wdays s))
6123 (org-get-wdays s))))
6124 ;; When to show a deadline in the calendar: if the
6125 ;; expiration is within WDAYS warning time. Past-due
6126 ;; deadlines are only shown on the current date
6127 (unless (or (and (<= diff wdays)
6128 (and todayp (not org-agenda-only-exact-dates)))
6129 (= diff 0))
6130 (throw :skip nil))
6131 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6132 ;; non-nil or if it isn't applicable to CURRENT deadline.
6133 (when (and donep
6134 (or org-agenda-skip-deadline-if-done
6135 (/= deadline current)))
6136 (throw :skip nil))
6137 (save-excursion
6138 (re-search-backward "^\\*+[ \t]+" nil t)
6139 (goto-char (match-end 0))
6140 (let* ((category (org-get-category))
6141 (level
6142 (make-string (org-reduced-level (org-outline-level)) ?\s))
6143 (head (buffer-substring (point) (line-end-position)))
6144 (inherited-tags
6145 (or (eq org-agenda-show-inherited-tags 'always)
6146 (and (listp org-agenda-show-inherited-tags)
6147 (memq 'agenda org-agenda-show-inherited-tags))
6148 (and (eq org-agenda-show-inherited-tags t)
6149 (or (eq org-agenda-use-tag-inheritance t)
6150 (memq 'agenda
6151 org-agenda-use-tag-inheritance)))))
6152 (tags (org-get-tags-at nil (not inherited-tags)))
6153 (timestr
6154 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6155 (concat (substring s (match-beginning 1)) " ")
6156 'time))
6157 (item
6158 (org-agenda-format-item
6159 ;; For past deadlines, make sure to report time
6160 ;; difference since date S, not since closest
6161 ;; repeater.
6162 (let ((diff (if (< (org-today) current) diff
6163 (- deadline current))))
6164 (if (= diff 0) (car org-agenda-deadline-leaders)
6165 (let ((future (nth 1 org-agenda-deadline-leaders))
6166 (past (nth 2 org-agenda-deadline-leaders)))
6167 (cond ((> diff 0) (format future diff))
6168 ((string= future past) (format past diff))
6169 (t (format past (abs diff)))))))
6170 head level category tags
6171 (and (= diff 0) timestr)))
6172 (face (org-agenda-deadline-face
6173 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6174 (upcomingp (and todayp (> diff 0)))
6175 (warntime (get-text-property (point) 'org-appt-warntime)))
6176 (org-add-props item props
6177 'org-marker (org-agenda-new-marker pos)
6178 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6179 'warntime warntime
6180 'level level
6181 'ts-date deadline
6182 'priority (- (org-get-priority item) diff)
6183 'todo-state todo-state
6184 'type (if upcomingp "upcoming-deadline" "deadline")
6185 'date (if upcomingp date deadline)
6186 'face (if donep 'org-agenda-done face)
6187 'undone-face face
6188 'done-face 'org-agenda-done)
6189 (push item deadline-items))))))
6190 (nreverse deadline-items)))
6192 (defun org-agenda-deadline-face (fraction)
6193 "Return the face to displaying a deadline item.
6194 FRACTION is what fraction of the head-warning time has passed."
6195 (let ((faces org-agenda-deadline-faces) f)
6196 (catch 'exit
6197 (while (setq f (pop faces))
6198 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6200 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6201 "Return the scheduled information for agenda display.
6202 Optional argument DEADLINES is a list of deadline items to be
6203 displayed in agenda view. When WITH-HOUR is non-nil, only return
6204 scheduled items with an hour specification like [h]h:mm."
6205 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6206 'org-todo-regexp org-todo-regexp
6207 'org-complex-heading-regexp org-complex-heading-regexp
6208 'done-face 'org-agenda-done
6209 'mouse-face 'highlight
6210 'help-echo
6211 (format "mouse-2 or RET jump to org file %s"
6212 (abbreviate-file-name buffer-file-name))))
6213 (regexp (if with-hour
6214 org-scheduled-time-hour-regexp
6215 org-scheduled-time-regexp))
6216 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6217 (current (calendar-absolute-from-gregorian date))
6218 (deadline-pos
6219 (mapcar (lambda (d)
6220 (let ((m (get-text-property 0 'org-hd-marker d)))
6221 (and m (marker-position m))))
6222 deadlines))
6223 scheduled-items)
6224 (goto-char (point-min))
6225 (while (re-search-forward regexp nil t)
6226 (catch :skip
6227 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6228 (org-agenda-skip)
6229 (let* ((s (match-string 1))
6230 (pos (1- (match-beginning 1)))
6231 (todo-state (save-match-data (org-get-todo-state)))
6232 (donep (member todo-state org-done-keywords))
6233 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6234 (member todo-state
6235 org-agenda-repeating-timestamp-show-all)))
6236 ;; SCHEDULE is the current scheduled date. When it
6237 ;; contains a repeater and SHOW-ALL is non-nil,
6238 ;; LAST-REPEAT is the repeat closest to CURRENT.
6239 ;; Otherwise, LAST-REPEAT is equal to SCHEDULE.
6240 (last-repeat (org-agenda--timestamp-to-absolute
6241 s current 'past show-all (current-buffer) pos))
6242 (schedule (org-agenda--timestamp-to-absolute s current))
6243 (diff (- last-repeat current))
6244 (warntime (get-text-property (point) 'org-appt-warntime))
6245 (pastschedp (< schedule (org-today)))
6246 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6247 (suppress-delay
6248 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6249 (org-entry-get nil "DEADLINE"))))
6250 (cond
6251 ((not deadline) nil)
6252 ;; The current item has a deadline date, so
6253 ;; evaluate its delay time.
6254 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6255 ;; Use global delay time.
6256 (- org-agenda-skip-scheduled-delay-if-deadline))
6257 ((eq org-agenda-skip-scheduled-delay-if-deadline
6258 'post-deadline)
6259 ;; Set delay to no later than DEADLINE. If
6260 ;; DEADLINE has a repeater, compare last schedule
6261 ;; repeat and last deadline repeat.
6262 (min (- last-repeat
6263 (org-agenda--timestamp-to-absolute
6264 deadline current 'past show-all
6265 (current-buffer)
6266 (save-excursion
6267 (beginning-of-line)
6268 (1+ (search-forward org-deadline-string)))))
6269 org-scheduled-delay-days))
6270 (t 0))))
6271 (ddays
6272 (cond
6273 ;; Nullify delay when a repeater triggered already
6274 ;; and the delay is of the form --Xd.
6275 ((and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6276 (/= schedule last-repeat))
6278 (suppress-delay
6279 (let ((org-scheduled-delay-days suppress-delay))
6280 (org-get-wdays s t t)))
6281 (t (org-get-wdays s t)))))
6282 ;; Only show a scheduled item in the calendar if it is on or
6283 ;; past the current date. Skip it if it has been displayed
6284 ;; for more than `org-scheduled-past-days'.
6285 (unless (or (and (>= ddays 0) (= diff (- ddays)))
6286 (and (< (+ diff ddays) 0)
6287 (< (abs diff) org-scheduled-past-days)
6288 (and todayp (not org-agenda-only-exact-dates)))
6289 (and todayp
6290 habitp
6291 (bound-and-true-p org-habit-show-all-today)))
6292 (throw :skip nil))
6293 ;; Skip done habits, or tasks if
6294 ;; `org-agenda-skip-deadline-if-done' is non-nil or if it
6295 ;; was scheduled in the past anyway.
6296 (when (and donep
6297 (or org-agenda-skip-scheduled-if-done
6298 (/= schedule current)
6299 habitp))
6300 (throw :skip nil))
6301 ;; Skip entry if it already appears as a deadline, per
6302 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6303 ;; doesn't apply to habits.
6304 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6305 ((guard
6306 (or (not (assq (line-beginning-position 0) deadline-pos))
6307 habitp))
6308 nil)
6309 (`repeated-after-deadline
6310 (>= last-repeat
6311 (time-to-days (org-get-deadline-time (point)))))
6312 (`not-today pastschedp)
6313 (`t t)
6314 (_ nil))
6315 (throw :skip nil))
6316 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6317 ;; only show them for today.
6318 (when (and habitp
6319 (or (not (bound-and-true-p org-habit-show-habits))
6320 (and (not todayp)
6321 (bound-and-true-p
6322 org-habit-show-habits-only-for-today))))
6323 (throw :skip nil))
6324 (save-excursion
6325 (re-search-backward "^\\*+[ \t]+" nil t)
6326 (goto-char (match-end 0))
6327 (let* ((category (org-get-category))
6328 (inherited-tags
6329 (or (eq org-agenda-show-inherited-tags 'always)
6330 (and (listp org-agenda-show-inherited-tags)
6331 (memq 'agenda org-agenda-show-inherited-tags))
6332 (and (eq org-agenda-show-inherited-tags t)
6333 (or (eq org-agenda-use-tag-inheritance t)
6334 (memq 'agenda
6335 org-agenda-use-tag-inheritance)))))
6336 (tags (org-get-tags-at nil (not inherited-tags)))
6337 (level
6338 (make-string (org-reduced-level (org-outline-level)) ?\s))
6339 (head (buffer-substring (point) (line-end-position)))
6340 (timestr
6341 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6342 (concat (substring s (match-beginning 1)) " ")
6343 'time))
6344 (item (org-agenda-format-item
6345 ;; For past scheduled dates, make sure to
6346 ;; report time difference since SCHEDULE,
6347 ;; not since closest repeater.
6348 (let ((diff (if (< (org-today) current) diff
6349 (- schedule current))))
6350 (if (= diff 0) (car org-agenda-scheduled-leaders)
6351 (format (nth 1 org-agenda-scheduled-leaders)
6352 (- 1 diff))))
6353 head level category tags
6354 (and (= diff 0) timestr)
6355 nil habitp))
6356 (face (cond ((and (not habitp) pastschedp)
6357 'org-scheduled-previously)
6358 (todayp 'org-scheduled-today)
6359 (t 'org-scheduled)))
6360 (habitp (and habitp (org-habit-parse-todo))))
6361 (org-add-props item props
6362 'undone-face face
6363 'face (if donep 'org-agenda-done face)
6364 'org-marker (org-agenda-new-marker pos)
6365 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6366 'type (if pastschedp "past-scheduled" "scheduled")
6367 'date (if pastschedp schedule date)
6368 'ts-date schedule
6369 'warntime warntime
6370 'level level
6371 'priority (if habitp (org-habit-get-priority habitp)
6372 (+ 94 (- 5 diff) (org-get-priority item)))
6373 'org-habit-p habitp
6374 'todo-state todo-state)
6375 (push item scheduled-items))))))
6376 (nreverse scheduled-items)))
6378 (defun org-agenda-get-blocks ()
6379 "Return the date-range information for agenda display."
6380 (let* ((props (list 'face nil
6381 'org-not-done-regexp org-not-done-regexp
6382 'org-todo-regexp org-todo-regexp
6383 'org-complex-heading-regexp org-complex-heading-regexp
6384 'mouse-face 'highlight
6385 'help-echo
6386 (format "mouse-2 or RET jump to org file %s"
6387 (abbreviate-file-name buffer-file-name))))
6388 (regexp org-tr-regexp)
6389 (d0 (calendar-absolute-from-gregorian date))
6390 marker hdmarker ee txt d1 d2 s1 s2 category
6391 level todo-state tags pos head donep inherited-tags)
6392 (goto-char (point-min))
6393 (while (re-search-forward regexp nil t)
6394 (catch :skip
6395 (org-agenda-skip)
6396 (setq pos (point))
6397 (let ((start-time (match-string 1))
6398 (end-time (match-string 2)))
6399 (setq s1 (match-string 1)
6400 s2 (match-string 2)
6401 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6402 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6403 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6404 ;; Only allow days between the limits, because the normal
6405 ;; date stamps will catch the limits.
6406 (save-excursion
6407 (setq todo-state (org-get-todo-state))
6408 (setq donep (member todo-state org-done-keywords))
6409 (if (and donep org-agenda-skip-timestamp-if-done)
6410 (throw :skip t))
6411 (setq marker (org-agenda-new-marker (point))
6412 category (org-get-category))
6413 (if (not (re-search-backward org-outline-regexp-bol nil t))
6414 (throw :skip nil)
6415 (goto-char (match-beginning 0))
6416 (setq hdmarker (org-agenda-new-marker (point))
6417 inherited-tags
6418 (or (eq org-agenda-show-inherited-tags 'always)
6419 (and (listp org-agenda-show-inherited-tags)
6420 (memq 'agenda org-agenda-show-inherited-tags))
6421 (and (eq org-agenda-show-inherited-tags t)
6422 (or (eq org-agenda-use-tag-inheritance t)
6423 (memq 'agenda org-agenda-use-tag-inheritance))))
6425 tags (org-get-tags-at nil (not inherited-tags)))
6426 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6427 (looking-at "\\*+[ \t]+\\(.*\\)")
6428 (setq head (match-string 1))
6429 (let ((remove-re
6430 (if org-agenda-remove-timeranges-from-blocks
6431 (concat
6432 "<" (regexp-quote s1) ".*?>"
6433 "--"
6434 "<" (regexp-quote s2) ".*?>")
6435 nil)))
6436 (setq txt (org-agenda-format-item
6437 (format
6438 (nth (if (= d1 d2) 0 1)
6439 org-agenda-timerange-leaders)
6440 (1+ (- d0 d1)) (1+ (- d2 d1)))
6441 head level category tags
6442 (cond ((and (= d1 d0) (= d2 d0))
6443 (concat "<" start-time ">--<" end-time ">"))
6444 ((= d1 d0)
6445 (concat "<" start-time ">"))
6446 ((= d2 d0)
6447 (concat "<" end-time ">")))
6448 remove-re))))
6449 (org-add-props txt props
6450 'org-marker marker 'org-hd-marker hdmarker
6451 'type "block" 'date date
6452 'level level
6453 'todo-state todo-state
6454 'priority (org-get-priority txt))
6455 (push txt ee))))
6456 (goto-char pos)))
6457 ;; Sort the entries by expiration date.
6458 (nreverse ee)))
6460 ;;; Agenda presentation and sorting
6462 (defvar org-prefix-has-time nil
6463 "A flag, set by `org-compile-prefix-format'.
6464 The flag is set if the currently compiled format contains a `%t'.")
6465 (defvar org-prefix-has-tag nil
6466 "A flag, set by `org-compile-prefix-format'.
6467 The flag is set if the currently compiled format contains a `%T'.")
6468 (defvar org-prefix-has-effort nil
6469 "A flag, set by `org-compile-prefix-format'.
6470 The flag is set if the currently compiled format contains a `%e'.")
6471 (defvar org-prefix-has-breadcrumbs nil
6472 "A flag, set by `org-compile-prefix-format'.
6473 The flag is set if the currently compiled format contains a `%b'.")
6474 (defvar org-prefix-category-length nil
6475 "Used by `org-compile-prefix-format' to remember the category field width.")
6476 (defvar org-prefix-category-max-length nil
6477 "Used by `org-compile-prefix-format' to remember the category field width.")
6479 (defun org-agenda-get-category-icon (category)
6480 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6481 (dolist (entry org-agenda-category-icon-alist)
6482 (when (org-string-match-p (car entry) category)
6483 (if (listp (cadr entry))
6484 (return (cadr entry))
6485 (return (apply 'create-image (cdr entry)))))))
6487 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6488 remove-re habitp)
6489 "Format TXT to be inserted into the agenda buffer.
6490 In particular, add the prefix and corresponding text properties.
6492 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6493 LEVEL may be a string to replace the `%l' specifier.
6494 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6495 category taken from local variable or file name. It will replace the `%c'
6496 specifier in the format.
6497 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6498 TXT for sorting of this entry, and for the `%t' specifier in the format.
6499 When DOTIME is a string, this string is searched for a time before TXT is.
6500 TAGS can be the tags of the headline.
6501 Any match of REMOVE-RE will be removed from TXT."
6502 ;; We keep the org-prefix-* variable values along with a compiled
6503 ;; formatter, so that multiple agendas existing at the same time do
6504 ;; not step on each other toes.
6506 ;; It was inconvenient to make these variables buffer local in
6507 ;; Agenda buffers, because this function expects to be called with
6508 ;; the buffer where item comes from being current, and not agenda
6509 ;; buffer
6510 (let* ((bindings (car org-prefix-format-compiled))
6511 (formatter (cadr org-prefix-format-compiled)))
6512 (loop for (var value) in bindings
6513 do (set var value))
6514 (save-match-data
6515 ;; Diary entries sometimes have extra whitespace at the beginning
6516 (setq txt (org-trim txt))
6518 ;; Fix the tags part in txt
6519 (setq txt (org-agenda-fix-displayed-tags
6520 txt tags
6521 org-agenda-show-inherited-tags
6522 org-agenda-hide-tags-regexp))
6524 (let* ((category (or category
6525 (if buffer-file-name
6526 (file-name-sans-extension
6527 (file-name-nondirectory buffer-file-name))
6528 "")))
6529 (category-icon (org-agenda-get-category-icon category))
6530 (category-icon (if category-icon
6531 (propertize " " 'display category-icon)
6532 ""))
6533 (effort (and (not (string= txt ""))
6534 (get-text-property 1 'effort txt)))
6535 ;; time, tag, effort are needed for the eval of the prefix format
6536 (tag (if tags (nth (1- (length tags)) tags) ""))
6537 time
6538 (ts (if dotime (concat
6539 (if (stringp dotime) dotime "")
6540 (and org-agenda-search-headline-for-time txt))))
6541 (time-of-day (and dotime (org-get-time-of-day ts)))
6542 stamp plain s0 s1 s2 rtn srp l
6543 duration breadcrumbs)
6544 (and (derived-mode-p 'org-mode) buffer-file-name
6545 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6546 (when (and dotime time-of-day)
6547 ;; Extract starting and ending time and move them to prefix
6548 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6549 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6550 (setq s0 (match-string 0 ts)
6551 srp (and stamp (match-end 3))
6552 s1 (match-string (if plain 1 2) ts)
6553 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6555 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6556 ;; them, we might want to remove them there to avoid duplication.
6557 ;; The user can turn this off with a variable.
6558 (if (and org-prefix-has-time
6559 org-agenda-remove-times-when-in-prefix (or stamp plain)
6560 (string-match (concat (regexp-quote s0) " *") txt)
6561 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6562 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6563 (= (match-beginning 0) 0)
6565 (setq txt (replace-match "" nil nil txt))))
6566 ;; Normalize the time(s) to 24 hour
6567 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6568 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6570 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6571 (let (org-time-clocksum-use-effort-durations)
6572 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6573 (setq s2
6574 (org-minutes-to-clocksum-string
6575 (+ (org-hh:mm-string-to-minutes s1)
6576 org-agenda-default-appointment-duration)))))
6578 ;; Compute the duration
6579 (when s2
6580 (setq duration (- (org-hh:mm-string-to-minutes s2)
6581 (org-hh:mm-string-to-minutes s1)))))
6583 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6584 ;; Tags are in the string
6585 (if (or (eq org-agenda-remove-tags t)
6586 (and org-agenda-remove-tags
6587 org-prefix-has-tag))
6588 (setq txt (replace-match "" t t txt))
6589 (setq txt (replace-match
6590 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6591 (match-string 2 txt))
6592 t t txt))))
6594 (when remove-re
6595 (while (string-match remove-re txt)
6596 (setq txt (replace-match "" t t txt))))
6598 ;; Set org-heading property on `txt' to mark the start of the
6599 ;; heading.
6600 (add-text-properties 0 (length txt) '(org-heading t) txt)
6602 ;; Prepare the variables needed in the eval of the compiled format
6603 (if org-prefix-has-breadcrumbs
6604 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6605 (let ((s (org-display-outline-path nil nil "->" t)))
6606 (if (eq "" s) "" (concat s "->"))))))
6607 (setq time (cond (s2 (concat
6608 (org-agenda-time-of-day-to-ampm-maybe s1)
6609 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6610 (if org-agenda-timegrid-use-ampm " ")))
6611 (s1 (concat
6612 (org-agenda-time-of-day-to-ampm-maybe s1)
6613 (if org-agenda-timegrid-use-ampm
6614 "........ "
6615 "......")))
6616 (t ""))
6617 extra (or (and (not habitp) extra) "")
6618 category (if (symbolp category) (symbol-name category) category)
6619 level (or level ""))
6620 (if (string-match org-bracket-link-regexp category)
6621 (progn
6622 (setq l (if (match-end 3)
6623 (- (match-end 3) (match-beginning 3))
6624 (- (match-end 1) (match-beginning 1))))
6625 (when (< l (or org-prefix-category-length 0))
6626 (setq category (copy-sequence category))
6627 (org-add-props category nil
6628 'extra-space (make-string
6629 (- org-prefix-category-length l 1) ?\ ))))
6630 (if (and org-prefix-category-max-length
6631 (>= (length category) org-prefix-category-max-length))
6632 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6633 ;; Evaluate the compiled format
6634 (setq rtn (concat (eval formatter) txt))
6636 ;; And finally add the text properties
6637 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6638 (org-add-props rtn nil
6639 'org-category category
6640 'tags (mapcar 'org-downcase-keep-props tags)
6641 'org-highest-priority org-highest-priority
6642 'org-lowest-priority org-lowest-priority
6643 'time-of-day time-of-day
6644 'duration duration
6645 'breadcrumbs breadcrumbs
6646 'txt txt
6647 'level level
6648 'time time
6649 'extra extra
6650 'format org-prefix-format-compiled
6651 'dotime dotime)))))
6653 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6654 "Remove tags string from TXT, and add a modified list of tags.
6655 The modified list may contain inherited tags, and tags matched by
6656 `org-agenda-hide-tags-regexp' will be removed."
6657 (when (or add-inherited hide-re)
6658 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6659 (setq txt (substring txt 0 (match-beginning 0))))
6660 (setq tags
6661 (delq nil
6662 (mapcar (lambda (tg)
6663 (if (or (and hide-re (string-match hide-re tg))
6664 (and (not add-inherited)
6665 (get-text-property 0 'inherited tg)))
6667 tg))
6668 tags)))
6669 (when tags
6670 (let ((have-i (get-text-property 0 'inherited (car tags)))
6672 (setq txt (concat txt " :"
6673 (mapconcat
6674 (lambda (x)
6675 (setq i (get-text-property 0 'inherited x))
6676 (if (and have-i (not i))
6677 (progn
6678 (setq have-i nil)
6679 (concat ":" x))
6681 tags ":")
6682 (if have-i "::" ":"))))))
6683 txt)
6685 (defun org-downcase-keep-props (s)
6686 (let ((props (text-properties-at 0 s)))
6687 (setq s (downcase s))
6688 (add-text-properties 0 (length s) props s)
6691 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6693 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6694 "Add a time-grid for agenda items which need it.
6696 LIST is the list of agenda items formatted by `org-agenda-list'.
6697 NDAYS is the span of the current agenda view.
6698 TODAYP is t when the current agenda view is on today."
6699 (catch 'exit
6700 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6701 ((and todayp (member 'today (car org-agenda-time-grid))))
6702 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6703 ((member 'weekly (car org-agenda-time-grid)))
6704 (t (throw 'exit list)))
6705 (let* ((have (delq nil (mapcar
6706 (lambda (x) (get-text-property 1 'time-of-day x))
6707 list)))
6708 (string (nth 1 org-agenda-time-grid))
6709 (gridtimes (nth 2 org-agenda-time-grid))
6710 (req (car org-agenda-time-grid))
6711 (remove (member 'remove-match req))
6712 new time)
6713 (if (and (member 'require-timed req) (not have))
6714 ;; don't show empty grid
6715 (throw 'exit list))
6716 (while (setq time (pop gridtimes))
6717 (unless (and remove (member time have))
6718 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6719 (push (org-agenda-format-item
6720 nil string nil "" nil
6721 (concat (substring time 0 -2) ":" (substring time -2)))
6722 new)
6723 (put-text-property
6724 2 (length (car new)) 'face 'org-time-grid (car new))))
6725 (when (and todayp org-agenda-show-current-time-in-grid)
6726 (push (org-agenda-format-item
6727 nil org-agenda-current-time-string nil "" nil
6728 (format-time-string "%H:%M "))
6729 new)
6730 (put-text-property
6731 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6733 (if (member 'time-up org-agenda-sorting-strategy-selected)
6734 (append new list)
6735 (append list new)))))
6737 (defun org-compile-prefix-format (key)
6738 "Compile the prefix format into a Lisp form that can be evaluated.
6739 The resulting form and associated variable bindings is returned
6740 and stored in the variable `org-prefix-format-compiled'."
6741 (setq org-prefix-has-time nil
6742 org-prefix-has-tag nil
6743 org-prefix-category-length nil
6744 org-prefix-has-effort nil
6745 org-prefix-has-breadcrumbs nil)
6746 (let ((s (cond
6747 ((stringp org-agenda-prefix-format)
6748 org-agenda-prefix-format)
6749 ((assq key org-agenda-prefix-format)
6750 (cdr (assq key org-agenda-prefix-format)))
6751 (t " %-12:c%?-12t% s")))
6752 (start 0)
6753 varform vars var e c f opt)
6754 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6755 s start)
6756 (setq var (or (cdr (assoc (match-string 4 s)
6757 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6758 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6759 'eval)
6760 c (or (match-string 3 s) "")
6761 opt (match-beginning 1)
6762 start (1+ (match-beginning 0)))
6763 (if (equal var 'time) (setq org-prefix-has-time t))
6764 (if (equal var 'tag) (setq org-prefix-has-tag t))
6765 (if (equal var 'effort) (setq org-prefix-has-effort t))
6766 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6767 (setq f (concat "%" (match-string 2 s) "s"))
6768 (when (equal var 'category)
6769 (setq org-prefix-category-length
6770 (floor (abs (string-to-number (match-string 2 s)))))
6771 (setq org-prefix-category-max-length
6772 (let ((x (match-string 2 s)))
6773 (save-match-data
6774 (if (string-match "\\.[0-9]+" x)
6775 (string-to-number (substring (match-string 0 x) 1)))))))
6776 (if (eq var 'eval)
6777 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6778 (if opt
6779 (setq varform
6780 `(if (or (equal "" ,var) (equal nil ,var))
6782 (format ,f (concat ,var ,c))))
6783 (setq varform
6784 `(format ,f (if (or (equal ,var "")
6785 (equal ,var nil)) ""
6786 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6787 (setq s (replace-match "%s" t nil s))
6788 (push varform vars))
6789 (setq vars (nreverse vars))
6790 (with-current-buffer (or org-agenda-buffer (current-buffer))
6791 (setq org-prefix-format-compiled
6792 (list
6793 `((org-prefix-has-time ,org-prefix-has-time)
6794 (org-prefix-has-tag ,org-prefix-has-tag)
6795 (org-prefix-category-length ,org-prefix-category-length)
6796 (org-prefix-has-effort ,org-prefix-has-effort)
6797 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6798 `(format ,s ,@vars))))))
6800 (defun org-set-sorting-strategy (key)
6801 (if (symbolp (car org-agenda-sorting-strategy))
6802 ;; the old format
6803 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6804 (setq org-agenda-sorting-strategy-selected
6805 (or (cdr (assq key org-agenda-sorting-strategy))
6806 (cdr (assq 'agenda org-agenda-sorting-strategy))
6807 '(time-up category-keep priority-down)))))
6809 (defun org-get-time-of-day (s &optional string mod24)
6810 "Check string S for a time of day.
6811 If found, return it as a military time number between 0 and 2400.
6812 If not found, return nil.
6813 The optional STRING argument forces conversion into a 5 character wide string
6814 HH:MM."
6815 (save-match-data
6816 (when
6817 (and
6818 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6819 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6820 (not (eq (get-text-property 1 'face s) 'org-link)))
6821 (let* ((h (string-to-number (match-string 1 s)))
6822 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6823 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6824 (am-p (equal ampm "am"))
6825 (h1 (cond ((not ampm) h)
6826 ((= h 12) (if am-p 0 12))
6827 (t (+ h (if am-p 0 12)))))
6828 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6829 (mod h1 24) h1))
6830 (t0 (+ (* 100 h2) m))
6831 (t1 (concat (if (>= h1 24) "+" " ")
6832 (if (and org-agenda-time-leading-zero
6833 (< t0 1000)) "0" "")
6834 (if (< t0 100) "0" "")
6835 (if (< t0 10) "0" "")
6836 (int-to-string t0))))
6837 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6839 (defvar org-agenda-before-sorting-filter-function nil
6840 "Function to be applied to agenda items prior to sorting.
6841 Prior to sorting also means just before they are inserted into the agenda.
6843 To aid sorting, you may revisit the original entries and add more text
6844 properties which will later be used by the sorting functions.
6846 The function should take a string argument, an agenda line.
6847 It has access to the text properties in that line, which contain among
6848 other things, the property `org-hd-marker' that points to the entry
6849 where the line comes from. Note that not all lines going into the agenda
6850 have this property, only most.
6852 The function should return the modified string. It is probably best
6853 to ONLY change text properties.
6855 You can also use this function as a filter, by returning nil for lines
6856 you don't want to have in the agenda at all. For this application, you
6857 could bind the variable in the options section of a custom command.")
6859 (defun org-agenda-finalize-entries (list &optional type)
6860 "Sort, limit and concatenate the LIST of agenda items.
6861 The optional argument TYPE tells the agenda type."
6862 (let ((max-effort (cond ((listp org-agenda-max-effort)
6863 (cdr (assoc type org-agenda-max-effort)))
6864 (t org-agenda-max-effort)))
6865 (max-todo (cond ((listp org-agenda-max-todos)
6866 (cdr (assoc type org-agenda-max-todos)))
6867 (t org-agenda-max-todos)))
6868 (max-tags (cond ((listp org-agenda-max-tags)
6869 (cdr (assoc type org-agenda-max-tags)))
6870 (t org-agenda-max-tags)))
6871 (max-entries (cond ((listp org-agenda-max-entries)
6872 (cdr (assoc type org-agenda-max-entries)))
6873 (t org-agenda-max-entries))))
6874 (when org-agenda-before-sorting-filter-function
6875 (setq list
6876 (delq nil
6877 (mapcar
6878 org-agenda-before-sorting-filter-function list))))
6879 (setq list (mapcar 'org-agenda-highlight-todo list)
6880 list (mapcar 'identity (sort list 'org-entries-lessp)))
6881 (when max-effort
6882 (setq list (org-agenda-limit-entries
6883 list 'effort-minutes max-effort
6884 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6885 32767 -1))))))
6886 (when max-todo
6887 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6888 (when max-tags
6889 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6890 (when max-entries
6891 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6892 (mapconcat 'identity list "\n")))
6894 (defun org-agenda-limit-entries (list prop limit &optional fn)
6895 "Limit the number of agenda entries."
6896 (let ((include (and limit (< limit 0))))
6897 (if limit
6898 (let ((fun (or fn (lambda (p) (if p 1))))
6899 (lim 0))
6900 (delq nil
6901 (mapcar
6902 (lambda (e)
6903 (let ((pval (funcall
6904 fun (get-text-property (1- (length e))
6905 prop e))))
6906 (if pval (setq lim (+ lim pval)))
6907 (cond ((and pval (<= lim (abs limit))) e)
6908 ((and include (not pval)) e))))
6909 list)))
6910 list)))
6912 (defun org-agenda-limit-interactively (remove)
6913 "In agenda, interactively limit entries to various maximums."
6914 (interactive "P")
6915 (if remove
6916 (progn (setq org-agenda-max-entries nil
6917 org-agenda-max-todos nil
6918 org-agenda-max-tags nil
6919 org-agenda-max-effort nil)
6920 (org-agenda-redo))
6921 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6922 (msg (cond ((= max ?E) "How many minutes? ")
6923 ((= max ?e) "How many entries? ")
6924 ((= max ?t) "How many TODO entries? ")
6925 ((= max ?T) "How many tagged entries? ")
6926 (t (user-error "Wrong input"))))
6927 (num (string-to-number (read-from-minibuffer msg))))
6928 (cond ((equal max ?e)
6929 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6930 ((equal max ?t)
6931 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6932 ((equal max ?T)
6933 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6934 ((equal max ?E)
6935 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6936 (org-agenda-fit-window-to-buffer))
6938 (defun org-agenda-highlight-todo (x)
6939 (let ((org-done-keywords org-done-keywords-for-agenda)
6940 (case-fold-search nil)
6942 (if (eq x 'line)
6943 (save-excursion
6944 (beginning-of-line 1)
6945 (setq re (org-get-at-bol 'org-todo-regexp))
6946 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6947 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6948 (add-text-properties (match-beginning 0) (match-end 1)
6949 (list 'face (org-get-todo-face 1)))
6950 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6951 (delete-region (match-beginning 1) (1- (match-end 0)))
6952 (goto-char (match-beginning 1))
6953 (insert (format org-agenda-todo-keyword-format s)))))
6954 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6955 (setq re (get-text-property 0 'org-todo-regexp x))
6956 (when (and re
6957 ;; Test `pl' because if there's no heading content,
6958 ;; there's no point matching to highlight. Note
6959 ;; that if we didn't test `pl' first, and there
6960 ;; happened to be no keyword from `org-todo-regexp'
6961 ;; on this heading line, then the `equal' comparison
6962 ;; afterwards would spuriously succeed in the case
6963 ;; where `pl' is nil -- causing an args-out-of-range
6964 ;; error when we try to add text properties to text
6965 ;; that isn't there.
6967 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6968 x pl) pl))
6969 (add-text-properties
6970 (or (match-end 1) (match-end 0)) (match-end 0)
6971 (list 'face (org-get-todo-face (match-string 2 x)))
6973 (when (match-end 1)
6974 (setq x (concat (substring x 0 (match-end 1))
6975 (format org-agenda-todo-keyword-format
6976 (match-string 2 x))
6977 (org-add-props " " (text-properties-at 0 x))
6978 (substring x (match-end 3)))))))
6979 x)))
6981 (defsubst org-cmp-values (a b property)
6982 "Compare the numeric value of text PROPERTY for string A and B."
6983 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6984 (pb (or (get-text-property (1- (length b)) property b) 0)))
6985 (cond ((> pa pb) +1)
6986 ((< pa pb) -1))))
6988 (defsubst org-cmp-effort (a b)
6989 "Compare the effort values of string A and B."
6990 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6991 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6992 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6993 (cond ((> ea eb) +1)
6994 ((< ea eb) -1))))
6996 (defsubst org-cmp-category (a b)
6997 "Compare the string values of categories of strings A and B."
6998 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6999 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
7000 (cond ((string-lessp ca cb) -1)
7001 ((string-lessp cb ca) +1))))
7003 (defsubst org-cmp-todo-state (a b)
7004 "Compare the todo states of strings A and B."
7005 (let* ((ma (or (get-text-property 1 'org-marker a)
7006 (get-text-property 1 'org-hd-marker a)))
7007 (mb (or (get-text-property 1 'org-marker b)
7008 (get-text-property 1 'org-hd-marker b)))
7009 (fa (and ma (marker-buffer ma)))
7010 (fb (and mb (marker-buffer mb)))
7011 (todo-kwds
7012 (or (and fa (with-current-buffer fa org-todo-keywords-1))
7013 (and fb (with-current-buffer fb org-todo-keywords-1))))
7014 (ta (or (get-text-property 1 'todo-state a) ""))
7015 (tb (or (get-text-property 1 'todo-state b) ""))
7016 (la (- (length (member ta todo-kwds))))
7017 (lb (- (length (member tb todo-kwds))))
7018 (donepa (member ta org-done-keywords-for-agenda))
7019 (donepb (member tb org-done-keywords-for-agenda)))
7020 (cond ((and donepa (not donepb)) -1)
7021 ((and (not donepa) donepb) +1)
7022 ((< la lb) -1)
7023 ((< lb la) +1))))
7025 (defsubst org-cmp-alpha (a b)
7026 "Compare the headlines, alphabetically."
7027 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7028 (plb (text-property-any 0 (length b) 'org-heading t b))
7029 (ta (and pla (substring a pla)))
7030 (tb (and plb (substring b plb))))
7031 (when pla
7032 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7033 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7034 (setq ta (substring ta (match-end 0))))
7035 (setq ta (downcase ta)))
7036 (when plb
7037 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7038 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7039 (setq tb (substring tb (match-end 0))))
7040 (setq tb (downcase tb)))
7041 (cond ((not ta) +1)
7042 ((not tb) -1)
7043 ((string-lessp ta tb) -1)
7044 ((string-lessp tb ta) +1))))
7046 (defsubst org-cmp-tag (a b)
7047 "Compare the string values of the first tags of A and B."
7048 (let ((ta (car (last (get-text-property 1 'tags a))))
7049 (tb (car (last (get-text-property 1 'tags b)))))
7050 (cond ((not ta) +1)
7051 ((not tb) -1)
7052 ((string-lessp ta tb) -1)
7053 ((string-lessp tb ta) +1))))
7055 (defsubst org-cmp-time (a b)
7056 "Compare the time-of-day values of strings A and B."
7057 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7058 (ta (or (get-text-property 1 'time-of-day a) def))
7059 (tb (or (get-text-property 1 'time-of-day b) def)))
7060 (cond ((< ta tb) -1)
7061 ((< tb ta) +1))))
7063 (defsubst org-cmp-ts (a b type)
7064 "Compare the timestamps values of entries A and B.
7065 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7066 \"timestamp_ia\", compare within each of these type. When TYPE
7067 is the empty string, compare all timestamps without respect of
7068 their type."
7069 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7070 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7071 (get-text-property 1 'ts-date a))
7072 def))
7073 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7074 (get-text-property 1 'ts-date b))
7075 def)))
7076 (cond ((< ta tb) -1)
7077 ((< tb ta) +1))))
7079 (defsubst org-cmp-habit-p (a b)
7080 "Compare the todo states of strings A and B."
7081 (let ((ha (get-text-property 1 'org-habit-p a))
7082 (hb (get-text-property 1 'org-habit-p b)))
7083 (cond ((and ha (not hb)) -1)
7084 ((and (not ha) hb) +1))))
7086 (defun org-entries-lessp (a b)
7087 "Predicate for sorting agenda entries."
7088 ;; The following variables will be used when the form is evaluated.
7089 ;; So even though the compiler complains, keep them.
7090 (let* ((ss org-agenda-sorting-strategy-selected)
7091 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7092 (org-cmp-ts a b "")))
7093 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7094 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7095 (org-cmp-ts a b "scheduled")))
7096 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7097 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7098 (org-cmp-ts a b "deadline")))
7099 (deadline-down (if deadline-up (- deadline-up) nil))
7100 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7101 (org-cmp-ts a b "timestamp_ia")))
7102 (tsia-down (if tsia-up (- tsia-up) nil))
7103 (ts-up (and (org-em 'ts-up 'ts-down ss)
7104 (org-cmp-ts a b "timestamp")))
7105 (ts-down (if ts-up (- ts-up) nil))
7106 (time-up (and (org-em 'time-up 'time-down ss)
7107 (org-cmp-time a b)))
7108 (time-down (if time-up (- time-up) nil))
7109 (stats-up (and (org-em 'stats-up 'stats-down ss)
7110 (org-cmp-values a b 'org-stats)))
7111 (stats-down (if stats-up (- stats-up) nil))
7112 (priority-up (and (org-em 'priority-up 'priority-down ss)
7113 (org-cmp-values a b 'priority)))
7114 (priority-down (if priority-up (- priority-up) nil))
7115 (effort-up (and (org-em 'effort-up 'effort-down ss)
7116 (org-cmp-effort a b)))
7117 (effort-down (if effort-up (- effort-up) nil))
7118 (category-up (and (or (org-em 'category-up 'category-down ss)
7119 (memq 'category-keep ss))
7120 (org-cmp-category a b)))
7121 (category-down (if category-up (- category-up) nil))
7122 (category-keep (if category-up +1 nil))
7123 (tag-up (and (org-em 'tag-up 'tag-down ss)
7124 (org-cmp-tag a b)))
7125 (tag-down (if tag-up (- tag-up) nil))
7126 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7127 (org-cmp-todo-state a b)))
7128 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7129 (habit-up (and (org-em 'habit-up 'habit-down ss)
7130 (org-cmp-habit-p a b)))
7131 (habit-down (if habit-up (- habit-up) nil))
7132 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7133 (org-cmp-alpha a b)))
7134 (alpha-down (if alpha-up (- alpha-up) nil))
7135 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7136 user-defined-up user-defined-down)
7137 (if (and need-user-cmp org-agenda-cmp-user-defined
7138 (functionp org-agenda-cmp-user-defined))
7139 (setq user-defined-up
7140 (funcall org-agenda-cmp-user-defined a b)
7141 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7142 (cdr (assoc
7143 (eval (cons 'or org-agenda-sorting-strategy-selected))
7144 '((-1 . t) (1 . nil) (nil . nil))))))
7146 ;;; Agenda restriction lock
7148 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7149 "Overlay to mark the headline to which agenda commands are restricted.")
7150 (overlay-put org-agenda-restriction-lock-overlay
7151 'face 'org-agenda-restriction-lock)
7152 (overlay-put org-agenda-restriction-lock-overlay
7153 'help-echo "Agendas are currently limited to this subtree.")
7154 (delete-overlay org-agenda-restriction-lock-overlay)
7156 ;;;###autoload
7157 (defun org-agenda-set-restriction-lock (&optional type)
7158 "Set restriction lock for agenda, to current subtree or file.
7159 Restriction will be the file if TYPE is `file', or if type is the
7160 universal prefix \\='(4), or if the cursor is before the first headline
7161 in the file. Otherwise, restriction will be to the current subtree."
7162 (interactive "P")
7163 (org-agenda-remove-restriction-lock 'noupdate)
7164 (and (equal type '(4)) (setq type 'file))
7165 (setq type (cond
7166 (type type)
7167 ((org-at-heading-p) 'subtree)
7168 ((condition-case nil (org-back-to-heading t) (error nil))
7169 'subtree)
7170 (t 'file)))
7171 (if (eq type 'subtree)
7172 (progn
7173 (setq org-agenda-restrict (current-buffer))
7174 (setq org-agenda-overriding-restriction 'subtree)
7175 (put 'org-agenda-files 'org-restrict
7176 (list (buffer-file-name (buffer-base-buffer))))
7177 (org-back-to-heading t)
7178 (move-overlay org-agenda-restriction-lock-overlay
7179 (point)
7180 (if org-agenda-restriction-lock-highlight-subtree
7181 (save-excursion (org-end-of-subtree t t) (point))
7182 (point-at-eol)))
7183 (move-marker org-agenda-restrict-begin (point))
7184 (move-marker org-agenda-restrict-end
7185 (save-excursion (org-end-of-subtree t t)))
7186 (message "Locking agenda restriction to subtree"))
7187 (put 'org-agenda-files 'org-restrict
7188 (list (buffer-file-name (buffer-base-buffer))))
7189 (setq org-agenda-restrict nil)
7190 (setq org-agenda-overriding-restriction 'file)
7191 (move-marker org-agenda-restrict-begin nil)
7192 (move-marker org-agenda-restrict-end nil)
7193 (message "Locking agenda restriction to file"))
7194 (setq current-prefix-arg nil)
7195 (org-agenda-maybe-redo))
7197 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7198 "Remove the agenda restriction lock."
7199 (interactive "P")
7200 (delete-overlay org-agenda-restriction-lock-overlay)
7201 (delete-overlay org-speedbar-restriction-lock-overlay)
7202 (setq org-agenda-overriding-restriction nil)
7203 (setq org-agenda-restrict nil)
7204 (put 'org-agenda-files 'org-restrict nil)
7205 (move-marker org-agenda-restrict-begin nil)
7206 (move-marker org-agenda-restrict-end nil)
7207 (setq current-prefix-arg nil)
7208 (message "Agenda restriction lock removed")
7209 (or noupdate (org-agenda-maybe-redo)))
7211 (defun org-agenda-maybe-redo ()
7212 "If there is any window showing the agenda view, update it."
7213 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7214 org-agenda-buffer-name)
7216 (w0 (selected-window)))
7217 (when w
7218 (select-window w)
7219 (org-agenda-redo)
7220 (select-window w0)
7221 (if org-agenda-overriding-restriction
7222 (message "Agenda view shifted to new %s restriction"
7223 org-agenda-overriding-restriction)
7224 (message "Agenda restriction lock removed")))))
7226 ;;; Agenda commands
7228 (defun org-agenda-check-type (error &rest types)
7229 "Check if agenda buffer is of allowed type.
7230 If ERROR is non-nil, throw an error, otherwise just return nil.
7231 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7232 (if (not org-agenda-type)
7233 (error "No Org agenda currently displayed")
7234 (if (memq org-agenda-type types)
7236 (if error
7237 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7238 nil))))
7240 (defun org-agenda-Quit ()
7241 "Exit the agenda, killing the agenda buffer.
7242 Like `org-agenda-quit', but kill the buffer even when
7243 `org-agenda-sticky' is non-nil."
7244 (interactive)
7245 (org-agenda--quit))
7247 (defun org-agenda-quit ()
7248 "Exit the agenda.
7250 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7251 instead of killing it.
7253 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7254 the pre-agenda window configuration.
7256 When column view is active, exit column view instead of the
7257 agenda."
7258 (interactive)
7259 (org-agenda--quit org-agenda-sticky))
7261 (defun org-agenda--quit (&optional bury)
7262 (if org-agenda-columns-active
7263 (org-columns-quit)
7264 (let ((wconf org-agenda-pre-window-conf)
7265 (buf (current-buffer))
7266 (org-agenda-last-indirect-window
7267 (and (eq org-indirect-buffer-display 'other-window)
7268 org-agenda-last-indirect-buffer
7269 (get-buffer-window org-agenda-last-indirect-buffer))))
7270 (cond
7271 ((eq org-agenda-window-setup 'other-frame)
7272 (delete-frame))
7273 ((and org-agenda-restore-windows-after-quit
7274 wconf)
7275 ;; Maybe restore the pre-agenda window configuration. Reset
7276 ;; `org-agenda-pre-window-conf' before running
7277 ;; `set-window-configuration', which loses the current buffer.
7278 (setq org-agenda-pre-window-conf nil)
7279 (set-window-configuration wconf))
7281 (when org-agenda-last-indirect-window
7282 (delete-window org-agenda-last-indirect-window))
7283 (and (not (eq org-agenda-window-setup 'current-window))
7284 (not (one-window-p))
7285 (delete-window))))
7286 (if bury
7287 ;; Set the agenda buffer as the current buffer instead of
7288 ;; passing it as an argument to `bury-buffer' so that
7289 ;; `bury-buffer' removes it from the window.
7290 (with-current-buffer buf
7291 (bury-buffer))
7292 (kill-buffer buf)
7293 (setq org-agenda-archives-mode nil
7294 org-agenda-buffer nil)))))
7296 (defun org-agenda-exit ()
7297 "Exit the agenda, killing Org buffers loaded by the agenda.
7298 Like `org-agenda-Quit', but kill any buffers that were created by
7299 the agenda. Org buffers visited directly by the user will not be
7300 touched. Also, exit the agenda even if it is in column view."
7301 (interactive)
7302 (when org-agenda-columns-active
7303 (org-columns-quit))
7304 (org-release-buffers org-agenda-new-buffers)
7305 (setq org-agenda-new-buffers nil)
7306 (org-agenda-Quit))
7308 (defun org-agenda-kill-all-agenda-buffers ()
7309 "Kill all buffers in `org-agenda-mode'.
7310 This is used when toggling sticky agendas."
7311 (interactive)
7312 (let (blist)
7313 (dolist (buf (buffer-list))
7314 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7315 (push buf blist)))
7316 (mapc 'kill-buffer blist)))
7318 (defun org-agenda-execute (arg)
7319 "Execute another agenda command, keeping same window.
7320 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7321 in the agenda."
7322 (interactive "P")
7323 (let ((org-agenda-window-setup 'current-window))
7324 (org-agenda arg)))
7326 (defun org-agenda-redo (&optional all)
7327 "Rebuild possibly ALL agenda view(s) in the current buffer."
7328 (interactive "P")
7329 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7330 (cpa (unless (eq all t) current-prefix-arg))
7331 (org-agenda-doing-sticky-redo org-agenda-sticky)
7332 (org-agenda-sticky nil)
7333 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7334 org-agenda-buffer-name))
7335 (org-agenda-keep-modes t)
7336 (tag-filter org-agenda-tag-filter)
7337 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7338 (top-hl-filter org-agenda-top-headline-filter)
7339 (cat-filter org-agenda-category-filter)
7340 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7341 (re-filter org-agenda-regexp-filter)
7342 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7343 (effort-filter org-agenda-effort-filter)
7344 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7345 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7346 (cols org-agenda-columns-active)
7347 (line (org-current-line))
7348 (window-line (- line (org-current-line (window-start))))
7349 (lprops (get 'org-agenda-redo-command 'org-lprops))
7350 (redo-cmd (get-text-property p 'org-redo-cmd))
7351 (last-args (get-text-property p 'org-last-args))
7352 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7353 (org-agenda-overriding-cmd-arguments
7354 (unless (eq all t)
7355 (cond ((listp last-args)
7356 (cons (or cpa (car last-args)) (cdr last-args)))
7357 ((stringp last-args)
7358 last-args))))
7359 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7360 (put 'org-agenda-tag-filter :preset-filter nil)
7361 (put 'org-agenda-category-filter :preset-filter nil)
7362 (put 'org-agenda-regexp-filter :preset-filter nil)
7363 (put 'org-agenda-effort-filter :preset-filter nil)
7364 (and cols (org-columns-quit))
7365 (message "Rebuilding agenda buffer...")
7366 (if series-redo-cmd
7367 (eval series-redo-cmd)
7368 (org-let lprops redo-cmd))
7369 (setq org-agenda-undo-list nil
7370 org-agenda-pending-undo-list nil
7371 org-agenda-tag-filter tag-filter
7372 org-agenda-category-filter cat-filter
7373 org-agenda-regexp-filter re-filter
7374 org-agenda-effort-filter effort-filter
7375 org-agenda-top-headline-filter top-hl-filter)
7376 (message "Rebuilding agenda buffer...done")
7377 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7378 (put 'org-agenda-category-filter :preset-filter cat-preset)
7379 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7380 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7381 (let ((tag (or tag-filter tag-preset))
7382 (cat (or cat-filter cat-preset))
7383 (effort (or effort-filter effort-preset))
7384 (re (or re-filter re-preset)))
7385 (when tag (org-agenda-filter-apply tag 'tag t))
7386 (when cat (org-agenda-filter-apply cat 'category))
7387 (when effort (org-agenda-filter-apply effort 'effort))
7388 (when re (org-agenda-filter-apply re 'regexp)))
7389 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7390 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7391 (org-goto-line line)
7392 (recenter window-line)))
7394 (defvar org-global-tags-completion-table nil)
7395 (defvar org-agenda-filter-form nil)
7396 (defvar org-agenda-filtered-by-category nil)
7398 (defun org-agenda-filter-by-category (strip)
7399 "Filter lines in the agenda buffer that have a specific category.
7400 The category is that of the current line.
7401 Without prefix argument, keep only the lines of that category.
7402 With a prefix argument, exclude the lines of that category.
7404 (interactive "P")
7405 (if (and org-agenda-filtered-by-category
7406 org-agenda-category-filter)
7407 (org-agenda-filter-show-all-cat)
7408 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7409 (cond
7410 ((and cat strip)
7411 (org-agenda-filter-apply
7412 (push (concat "-" cat) org-agenda-category-filter) 'category))
7413 (cat
7414 (org-agenda-filter-apply
7415 (setq org-agenda-category-filter
7416 (list (concat "+" cat))) 'category))
7417 (t (error "No category at point"))))))
7419 (defun org-find-top-headline (&optional pos)
7420 "Find the topmost parent headline and return it."
7421 (save-excursion
7422 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7423 (if pos (goto-char pos))
7424 ;; Skip up to the topmost parent
7425 (while (ignore-errors (outline-up-heading 1) t))
7426 (ignore-errors
7427 (nth 4 (org-heading-components))))))
7429 (defvar org-agenda-filtered-by-top-headline nil)
7430 (defun org-agenda-filter-by-top-headline (strip)
7431 "Keep only those lines that are descendants from the same top headline.
7432 The top headline is that of the current line."
7433 (interactive "P")
7434 (if org-agenda-filtered-by-top-headline
7435 (progn
7436 (setq org-agenda-filtered-by-top-headline nil
7437 org-agenda-top-headline-filter nil)
7438 (org-agenda-filter-show-all-top-filter))
7439 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7440 (if toph (org-agenda-filter-top-headline-apply toph strip)
7441 (error "No top-level headline at point")))))
7443 (defvar org-agenda-regexp-filter nil)
7444 (defun org-agenda-filter-by-regexp (strip)
7445 "Filter agenda entries by a regular expression.
7446 Regexp filters are cumulative.
7447 With no prefix argument, keep entries matching the regexp.
7448 With one prefix argument, filter out entries matching the regexp.
7449 With two prefix arguments, remove the regexp filters."
7450 (interactive "P")
7451 (if (not (equal strip '(16)))
7452 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7453 (read-from-minibuffer
7454 (if (equal strip '(4))
7455 "Filter out entries matching regexp: "
7456 "Narrow to entries matching regexp: ")))))
7457 (push flt org-agenda-regexp-filter)
7458 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7459 (org-agenda-filter-show-all-re)
7460 (message "Regexp filter removed")))
7462 (defvar org-agenda-effort-filter nil)
7463 (defun org-agenda-filter-by-effort (strip)
7464 "Filter agenda entries by effort.
7465 With no prefix argument, keep entries matching the effort condition.
7466 With one prefix argument, filter out entries matching the condition.
7467 With two prefix arguments, remove the effort filters."
7468 (interactive "P")
7469 (cond ((member strip '(nil 4))
7470 (let ((efforts (org-split-string
7471 (or (cdr (assoc (concat org-effort-property "_ALL")
7472 org-global-properties))
7473 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7474 "")))
7475 (eff -1)
7476 effort-prompt op)
7477 (while (not (member op '(?< ?> ?=)))
7478 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7479 (loop for i from 0 to 9 do
7480 (setq effort-prompt
7481 (concat
7482 effort-prompt " ["
7483 (if (= i 9) "0" (int-to-string (1+ i)))
7484 "]" (nth i efforts))))
7485 (message "Effort %s%s" (char-to-string op) effort-prompt)
7486 (while (or (< eff 0) (> eff 9))
7487 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7488 (setq org-agenda-effort-filter
7489 (list (concat (if strip "-" "+")
7490 (char-to-string op) (nth (1- eff) efforts))))
7491 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7492 (t (org-agenda-filter-show-all-effort)
7493 (message "Effort filter removed"))))
7495 (defun org-agenda-filter-remove-all ()
7496 "Remove all filters from the current agenda buffer."
7497 (interactive)
7498 (when org-agenda-tag-filter
7499 (org-agenda-filter-show-all-tag))
7500 (when org-agenda-category-filter
7501 (org-agenda-filter-show-all-cat))
7502 (when org-agenda-regexp-filter
7503 (org-agenda-filter-show-all-re))
7504 (when org-agenda-top-headline-filter
7505 (org-agenda-filter-show-all-top-filter))
7506 (when org-agenda-effort-filter
7507 (org-agenda-filter-show-all-effort))
7508 (org-agenda-finalize))
7510 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7511 "Keep only those lines in the agenda buffer that have a specific tag.
7512 The tag is selected with its fast selection letter, as
7513 configured. With a single \\[universal-argument] prefix ARG,
7514 exclude the agenda search. With a double \\[universal-argument]
7515 prefix ARG, filter the literal tag. I.e. don't filter on all its
7516 group members.
7518 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7519 used to exclude the search - the interactive user can also press `-' or `+'
7520 to switch between filtering and excluding."
7521 (interactive "P")
7522 (let* ((alist org-tag-alist-for-agenda)
7523 (tag-chars (mapconcat
7524 (lambda (x) (if (and (not (symbolp (car x)))
7525 (cdr x))
7526 (char-to-string (cdr x))
7527 ""))
7528 org-tag-alist-for-agenda ""))
7529 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7530 (string-to-list tag-chars)))
7531 (exclude (or exclude (equal arg '(4))))
7532 (expand (not (equal arg '(16))))
7533 (inhibit-read-only t)
7534 (current org-agenda-tag-filter)
7535 a n tag)
7536 (unless char
7537 (while (not (memq char valid-char-list))
7538 (message
7539 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7540 (if exclude "Exclude" "Filter") tag-chars
7541 (if org-agenda-auto-exclude-function "[RET], " "")
7542 (if expand "" ", no grouptag expand"))
7543 (setq char (read-char-exclusive))
7544 ;; Excluding or filtering down
7545 (cond ((eq char ?-) (setq exclude t))
7546 ((eq char ?+) (setq exclude nil)))))
7547 (when (eq char ?\t)
7548 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7549 (setq-local org-global-tags-completion-table
7550 (org-global-tags-completion-table)))
7551 (let ((completion-ignore-case t))
7552 (setq tag (completing-read
7553 "Tag: " org-global-tags-completion-table))))
7554 (cond
7555 ((eq char ?\r)
7556 (org-agenda-filter-show-all-tag)
7557 (when org-agenda-auto-exclude-function
7558 (setq org-agenda-tag-filter nil)
7559 (dolist (tag (org-agenda-get-represented-tags))
7560 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7561 (if modifier
7562 (push modifier org-agenda-tag-filter))))
7563 (if (not (null org-agenda-tag-filter))
7564 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7565 ((eq char ?/)
7566 (org-agenda-filter-show-all-tag)
7567 (when (get 'org-agenda-tag-filter :preset-filter)
7568 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7569 ((eq char ?.)
7570 (setq org-agenda-tag-filter
7571 (mapcar (lambda(tag) (concat "+" tag))
7572 (org-get-at-bol 'tags)))
7573 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7574 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7575 ((or (eq char ?\s)
7576 (setq a (rassoc char alist))
7577 (and tag (setq a (cons tag nil))))
7578 (org-agenda-filter-show-all-tag)
7579 (setq tag (car a))
7580 (setq org-agenda-tag-filter
7581 (cons (concat (if exclude "-" "+") tag)
7582 current))
7583 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7584 (t (error "Invalid tag selection character %c" char)))))
7586 (defun org-agenda-get-represented-tags ()
7587 "Get a list of all tags currently represented in the agenda."
7588 (let (p tags)
7589 (save-excursion
7590 (goto-char (point-min))
7591 (while (setq p (next-single-property-change (point) 'tags))
7592 (goto-char p)
7593 (mapc (lambda (x) (add-to-list 'tags x))
7594 (get-text-property (point) 'tags))))
7595 tags))
7598 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7599 "Create the form that tests a line for agenda filter. Optional
7600 argument EXPAND can be used for the TYPE tag and will expand the
7601 tags in the FILTER if any of the tags in FILTER are grouptags."
7602 (let (f f1)
7603 (cond
7604 ;; Tag filter
7605 ((eq type 'tag)
7606 (setq filter
7607 (delete-dups
7608 (append (get 'org-agenda-tag-filter :preset-filter)
7609 filter)))
7610 (dolist (x filter)
7611 (let ((op (string-to-char x)))
7612 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7613 (setq x (list x)))
7614 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7615 (push f1 f))))
7616 ;; Category filter
7617 ((eq type 'category)
7618 (setq filter
7619 (delete-dups
7620 (append (get 'org-agenda-category-filter :preset-filter)
7621 filter)))
7622 (dolist (x filter)
7623 (if (equal "-" (substring x 0 1))
7624 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7625 (setq f1 (list 'equal (substring x 1) 'cat)))
7626 (push f1 f)))
7627 ;; Regexp filter
7628 ((eq type 'regexp)
7629 (setq filter
7630 (delete-dups
7631 (append (get 'org-agenda-regexp-filter :preset-filter)
7632 filter)))
7633 (dolist (x filter)
7634 (if (equal "-" (substring x 0 1))
7635 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7636 (setq f1 (list 'string-match (substring x 1) 'txt)))
7637 (push f1 f)))
7638 ;; Effort filter
7639 ((eq type 'effort)
7640 (setq filter
7641 (delete-dups
7642 (append (get 'org-agenda-effort-filter :preset-filter)
7643 filter)))
7644 (dolist (x filter)
7645 (push (org-agenda-filter-effort-form x) f))))
7646 (cons 'and (nreverse f))))
7648 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7649 "Return a form associated to tag-expression TAGS.
7650 Build a form testing a line for agenda filter for
7651 tag-expressions. OP is an operator of type CHAR that allows the
7652 function to set the right switches in the returned form."
7653 (let (form)
7654 ;; Any of the expressions can match if OP is +, all must match if
7655 ;; the operator is -.
7656 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7657 (let* ((tag (substring x 1))
7658 (f (cond
7659 ((string= "" tag) '(not tags))
7660 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7661 ;; TAG is a regexp.
7662 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7663 (t (list 'member (downcase tag) 'tags)))))
7664 (push (if (eq op ?-) (list 'not f) f) form)))))
7666 (defun org-agenda-filter-effort-form (e)
7667 "Return the form to compare the effort of the current line with what E says.
7668 E looks like \"+<2:25\"."
7669 (let (op)
7670 (setq e (substring e 1))
7671 (setq op (string-to-char e) e (substring e 1))
7672 (setq op (cond ((equal op ?<) '<=)
7673 ((equal op ?>) '>=)
7674 ((equal op ??) op)
7675 (t '=)))
7676 (list 'org-agenda-compare-effort (list 'quote op)
7677 (org-duration-string-to-minutes e))))
7679 (defun org-agenda-compare-effort (op value)
7680 "Compare the effort of the current line with VALUE, using OP.
7681 If the line does not have an effort defined, return nil."
7682 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7683 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7684 value)))
7686 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7687 "Expand group tags in FILTER for the agenda.
7688 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7689 (if org-group-tags
7690 (let ((case-fold-search t) rtn)
7691 (mapc
7692 (lambda (f)
7693 (let (f0 dir)
7694 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7695 (setq dir (match-string 1 f) f0 (match-string 2 f))
7696 (setq dir (if no-operator "" "+") f0 f))
7697 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7698 (org-tags-expand f0 t t))
7699 rtn))))
7700 filter)
7701 (reverse rtn))
7702 filter))
7704 (defun org-agenda-filter-apply (filter type &optional expand)
7705 "Set FILTER as the new agenda filter and apply it. Optional
7706 argument EXPAND can be used for the TYPE tag and will expand the
7707 tags in the FILTER if any of the tags in FILTER are grouptags."
7708 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7709 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7710 (let (tags cat txt)
7711 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7712 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7713 ;; category is used as the filter:
7714 (setq org-agenda-filtered-by-category
7715 (and (eq type 'category)
7716 (not (equal (substring (car filter) 0 1) "-"))))
7717 (org-agenda-set-mode-name)
7718 (save-excursion
7719 (goto-char (point-min))
7720 (while (not (eobp))
7721 (if (org-get-at-bol 'org-marker)
7722 (progn
7723 (setq tags (org-get-at-bol 'tags)
7724 cat (org-get-at-eol 'org-category 1)
7725 txt (org-get-at-bol 'txt))
7726 (if (not (eval org-agenda-filter-form))
7727 (org-agenda-filter-hide-line type))
7728 (beginning-of-line 2))
7729 (beginning-of-line 2))))
7730 (if (get-char-property (point) 'invisible)
7731 (ignore-errors (org-agenda-previous-line)))))
7733 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7734 "Filter by top headline HL."
7735 (org-agenda-set-mode-name)
7736 (save-excursion
7737 (goto-char (point-min))
7738 (while (not (eobp))
7739 (let* ((pos (org-get-at-bol 'org-hd-marker))
7740 (tophl (and pos (org-find-top-headline pos))))
7741 (if (and tophl (funcall (if negative 'identity 'not)
7742 (string= hl tophl)))
7743 (org-agenda-filter-hide-line 'top-headline)))
7744 (beginning-of-line 2)))
7745 (if (get-char-property (point) 'invisible)
7746 (org-agenda-previous-line))
7747 (setq org-agenda-top-headline-filter hl
7748 org-agenda-filtered-by-top-headline t))
7750 (defun org-agenda-filter-hide-line (type)
7751 "Hide lines with TYPE in the agenda buffer."
7752 (let* ((b (max (point-min) (1- (point-at-bol))))
7753 (e (point-at-eol)))
7754 (let ((inhibit-read-only t))
7755 (add-text-properties
7756 b e `(invisible org-filtered org-filter-type ,type)))))
7758 (defun org-agenda-remove-filter (type)
7759 (interactive)
7760 "Remove filter of type TYPE from the agenda buffer."
7761 (save-excursion
7762 (goto-char (point-min))
7763 (let ((inhibit-read-only t) pos)
7764 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7765 (goto-char pos)
7766 (remove-text-properties
7767 (point) (next-single-property-change (point) 'org-filter-type)
7768 `(invisible org-filtered org-filter-type ,type))))
7769 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7770 (setq org-agenda-filter-form nil)
7771 (org-agenda-set-mode-name)
7772 (org-agenda-finalize)))
7774 (defun org-agenda-filter-show-all-tag nil
7775 (org-agenda-remove-filter 'tag))
7776 (defun org-agenda-filter-show-all-re nil
7777 (org-agenda-remove-filter 'regexp))
7778 (defun org-agenda-filter-show-all-effort nil
7779 (org-agenda-remove-filter 'effort))
7780 (defun org-agenda-filter-show-all-cat nil
7781 (org-agenda-remove-filter 'category))
7782 (defun org-agenda-filter-show-all-top-filter nil
7783 (org-agenda-remove-filter 'top-headline))
7785 (defun org-agenda-manipulate-query-add ()
7786 "Manipulate the query by adding a search term with positive selection.
7787 Positive selection means the term must be matched for selection of an entry."
7788 (interactive)
7789 (org-agenda-manipulate-query ?\[))
7790 (defun org-agenda-manipulate-query-subtract ()
7791 "Manipulate the query by adding a search term with negative selection.
7792 Negative selection means term must not be matched for selection of an entry."
7793 (interactive)
7794 (org-agenda-manipulate-query ?\]))
7795 (defun org-agenda-manipulate-query-add-re ()
7796 "Manipulate the query by adding a search regexp with positive selection.
7797 Positive selection means the regexp must match for selection of an entry."
7798 (interactive)
7799 (org-agenda-manipulate-query ?\{))
7800 (defun org-agenda-manipulate-query-subtract-re ()
7801 "Manipulate the query by adding a search regexp with negative selection.
7802 Negative selection means regexp must not match for selection of an entry."
7803 (interactive)
7804 (org-agenda-manipulate-query ?\}))
7805 (defun org-agenda-manipulate-query (char)
7806 (cond
7807 ((memq org-agenda-type '(timeline agenda))
7808 (let ((org-agenda-include-inactive-timestamps t))
7809 (org-agenda-redo))
7810 (message "Display now includes inactive timestamps as well"))
7811 ((eq org-agenda-type 'search)
7812 (org-add-to-string
7813 'org-agenda-query-string
7814 (if org-agenda-last-search-view-search-was-boolean
7815 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7816 (?\{ . " +{}") (?\} . " -{}"))))
7817 " "))
7818 (setq org-agenda-redo-command
7819 (list 'org-search-view
7820 (car (get-text-property (min (1- (point-max)) (point))
7821 'org-last-args))
7822 org-agenda-query-string
7823 (+ (length org-agenda-query-string)
7824 (if (member char '(?\{ ?\})) 0 1))))
7825 (set-register org-agenda-query-register org-agenda-query-string)
7826 (let ((org-agenda-overriding-arguments
7827 (cdr org-agenda-redo-command)))
7828 (org-agenda-redo)))
7829 (t (error "Cannot manipulate query for %s-type agenda buffers"
7830 org-agenda-type))))
7832 (defun org-add-to-string (var string)
7833 (set var (concat (symbol-value var) string)))
7835 (defun org-agenda-goto-date (span)
7836 "Jump to DATE in agenda."
7837 (interactive "P")
7838 (let* ((org-read-date-prefer-future
7839 (eval org-agenda-jump-prefer-future))
7840 (date (org-read-date))
7841 (day (time-to-days (org-time-string-to-time date)))
7842 (org-agenda-sticky-orig org-agenda-sticky)
7843 (org-agenda-buffer-tmp-name (buffer-name))
7844 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7845 (0-arg (or current-prefix-arg (car args)))
7846 (2-arg (nth 2 args))
7847 (with-hour-p (nth 4 org-agenda-redo-command))
7848 (newcmd (list 'org-agenda-list 0-arg date
7849 (org-agenda-span-to-ndays
7850 2-arg (org-time-string-to-absolute date))
7851 with-hour-p))
7852 (newargs (cdr newcmd))
7853 (inhibit-read-only t)
7854 org-agenda-sticky)
7855 (if (not (org-agenda-check-type t 'agenda))
7856 (error "Not available in non-agenda views")
7857 (add-text-properties (point-min) (point-max)
7858 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7859 (org-agenda-redo)
7860 (goto-char (point-min))
7861 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7862 (save-excursion (move-beginning-of-line 2) (eobp))))
7863 (move-beginning-of-line 2))
7864 (setq org-agenda-sticky org-agenda-sticky-orig
7865 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7867 (defun org-agenda-goto-today ()
7868 "Go to today."
7869 (interactive)
7870 (org-agenda-check-type t 'timeline 'agenda)
7871 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7872 (curspan (nth 2 args))
7873 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7874 (cond
7875 (tdpos (goto-char tdpos))
7876 ((eq org-agenda-type 'agenda)
7877 (let* ((sd (org-agenda-compute-starting-span
7878 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7879 (org-agenda-overriding-arguments args))
7880 (setf (nth 1 org-agenda-overriding-arguments) sd)
7881 (org-agenda-redo)
7882 (org-agenda-find-same-or-today-or-agenda)))
7883 (t (error "Cannot find today")))))
7885 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7886 (goto-char
7887 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7888 (text-property-any (point-min) (point-max) 'org-today t)
7889 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7890 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7891 (org-agenda-backward-block))
7892 (point-min))))
7894 (defun org-agenda-backward-block ()
7895 "Move backward by one agenda block."
7896 (interactive)
7897 (org-agenda-forward-block 'backward))
7899 (defun org-agenda-forward-block (&optional backward)
7900 "Move forward by one agenda block.
7901 When optional argument BACKWARD is set, go backward"
7902 (interactive)
7903 (cond ((not (derived-mode-p 'org-agenda-mode))
7904 (user-error
7905 "Cannot execute this command outside of org-agenda-mode buffers"))
7906 ((looking-at (if backward "\\`" "\\'"))
7907 (message "Already at the %s block" (if backward "first" "last")))
7908 (t (let ((pos (prog1 (point)
7909 (ignore-errors (if backward (backward-char 1)
7910 (move-end-of-line 1)))))
7911 (f (if backward
7912 'previous-single-property-change
7913 'next-single-property-change))
7914 moved dest)
7915 (while (and (setq dest (funcall
7916 f (point) 'org-agenda-structural-header))
7917 (not (get-text-property
7918 (point) 'org-agenda-structural-header)))
7919 (setq moved t)
7920 (goto-char dest))
7921 (if moved (move-beginning-of-line 1)
7922 (goto-char (if backward (point-min) (point-max)))
7923 (move-beginning-of-line 1)
7924 (message "No %s block" (if backward "previous" "further")))))))
7926 (defun org-agenda-later (arg)
7927 "Go forward in time by the current span.
7928 With prefix ARG, go forward that many times the current span."
7929 (interactive "p")
7930 (org-agenda-check-type t 'agenda)
7931 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7932 (span (or (nth 2 args) org-agenda-current-span))
7933 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7934 (greg (calendar-gregorian-from-absolute sd))
7935 (cnt (org-get-at-bol 'org-day-cnt))
7936 greg2)
7937 (cond
7938 ((numberp span)
7939 (setq sd (+ (* span arg) sd)))
7940 ((eq span 'day)
7941 (setq sd (+ arg sd)))
7942 ((eq span 'week)
7943 (setq sd (+ (* 7 arg) sd)))
7944 ((eq span 'fortnight)
7945 (setq sd (+ (* 14 arg) sd)))
7946 ((eq span 'month)
7947 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7948 sd (calendar-absolute-from-gregorian greg2))
7949 (setcar greg2 (1+ (car greg2))))
7950 ((eq span 'year)
7951 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7952 sd (calendar-absolute-from-gregorian greg2))
7953 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7955 (setq sd (+ (* span arg) sd))))
7956 (let ((org-agenda-overriding-cmd
7957 ;; `cmd' may have been set by `org-agenda-run-series' which
7958 ;; uses `org-agenda-overriding-cmd' to decide whether
7959 ;; overriding is allowed for `cmd'
7960 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7961 (org-agenda-overriding-arguments
7962 (list (car args) sd span)))
7963 (org-agenda-redo)
7964 (org-agenda-find-same-or-today-or-agenda cnt))))
7966 (defun org-agenda-earlier (arg)
7967 "Go backward in time by the current span.
7968 With prefix ARG, go backward that many times the current span."
7969 (interactive "p")
7970 (org-agenda-later (- arg)))
7972 (defun org-agenda-view-mode-dispatch ()
7973 "Call one of the view mode commands."
7974 (interactive)
7975 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7976 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7977 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7978 (let ((a (read-char-exclusive)))
7979 (case a
7980 (?\ (call-interactively 'org-agenda-reset-view))
7981 (?d (call-interactively 'org-agenda-day-view))
7982 (?w (call-interactively 'org-agenda-week-view))
7983 (?t (call-interactively 'org-agenda-fortnight-view))
7984 (?m (call-interactively 'org-agenda-month-view))
7985 (?y (call-interactively 'org-agenda-year-view))
7986 (?l (call-interactively 'org-agenda-log-mode))
7987 (?L (org-agenda-log-mode '(4)))
7988 (?c (org-agenda-log-mode 'clockcheck))
7989 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7990 (?a (call-interactively 'org-agenda-archives-mode))
7991 (?A (org-agenda-archives-mode 'files))
7992 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7993 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7994 (?G (call-interactively 'org-agenda-toggle-time-grid))
7995 (?D (call-interactively 'org-agenda-toggle-diary))
7996 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7997 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7998 (org-agenda-check-type t 'timeline 'agenda)
7999 (org-agenda-redo))
8000 (message "Display now includes inactive timestamps as well"))
8001 (?q (message "Abort"))
8002 (otherwise (error "Invalid key" )))))
8004 (defun org-agenda-reset-view ()
8005 "Switch to default view for agenda."
8006 (interactive)
8007 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
8008 (defun org-agenda-day-view (&optional day-of-month)
8009 "Switch to daily view for agenda.
8010 With argument DAY-OF-MONTH, switch to that day of the month."
8011 (interactive "P")
8012 (org-agenda-change-time-span 'day day-of-month))
8013 (defun org-agenda-week-view (&optional iso-week)
8014 "Switch to daily view for agenda.
8015 With argument ISO-WEEK, switch to the corresponding ISO week.
8016 If ISO-WEEK has more then 2 digits, only the last two encode the
8017 week. Any digits before this encode a year. So 200712 means
8018 week 12 of year 2007. Years in the range 1938-2037 can also be
8019 written as 2-digit years."
8020 (interactive "P")
8021 (org-agenda-change-time-span 'week iso-week))
8022 (defun org-agenda-fortnight-view (&optional iso-week)
8023 "Switch to daily view for agenda.
8024 With argument ISO-WEEK, switch to the corresponding ISO week.
8025 If ISO-WEEK has more then 2 digits, only the last two encode the
8026 week. Any digits before this encode a year. So 200712 means
8027 week 12 of year 2007. Years in the range 1938-2037 can also be
8028 written as 2-digit years."
8029 (interactive "P")
8030 (org-agenda-change-time-span 'fortnight iso-week))
8031 (defun org-agenda-month-view (&optional month)
8032 "Switch to monthly view for agenda.
8033 With argument MONTH, switch to that month."
8034 (interactive "P")
8035 (org-agenda-change-time-span 'month month))
8036 (defun org-agenda-year-view (&optional year)
8037 "Switch to yearly view for agenda.
8038 With argument YEAR, switch to that year.
8039 If MONTH has more then 2 digits, only the last two encode the
8040 month. Any digits before this encode a year. So 200712 means
8041 December year 2007. Years in the range 1938-2037 can also be
8042 written as 2-digit years."
8043 (interactive "P")
8044 (when year
8045 (setq year (org-small-year-to-year year)))
8046 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8047 (org-agenda-change-time-span 'year year)
8048 (error "Abort")))
8050 (defun org-agenda-change-time-span (span &optional n)
8051 "Change the agenda view to SPAN.
8052 SPAN may be `day', `week', `fortnight', `month', `year'."
8053 (org-agenda-check-type t 'agenda)
8054 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8055 (curspan (nth 2 args)))
8056 (if (and (not n) (equal curspan span))
8057 (error "Viewing span is already \"%s\"" span))
8058 (let* ((sd (or (org-get-at-bol 'day)
8059 (nth 1 args)
8060 org-starting-day))
8061 (sd (org-agenda-compute-starting-span sd span n))
8062 (org-agenda-overriding-cmd
8063 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8064 (org-agenda-overriding-arguments
8065 (list (car args) sd span)))
8066 (org-agenda-redo)
8067 (org-agenda-find-same-or-today-or-agenda))
8068 (org-agenda-set-mode-name)
8069 (message "Switched to %s view" span)))
8071 (defun org-agenda-compute-starting-span (sd span &optional n)
8072 "Compute starting date for agenda.
8073 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8074 is a cons cell with the starting date and the number of days,
8075 so that the date SD will be in that range."
8076 (let* ((greg (calendar-gregorian-from-absolute sd))
8077 (dg (nth 1 greg))
8078 (mg (car greg))
8079 (yg (nth 2 greg)))
8080 (cond
8081 ((eq span 'day)
8082 (when n
8083 (setq sd (+ (calendar-absolute-from-gregorian
8084 (list mg 1 yg))
8085 n -1))))
8086 ((or (eq span 'week) (eq span 'fortnight))
8087 (let* ((nt (calendar-day-of-week
8088 (calendar-gregorian-from-absolute sd)))
8089 (d (if org-agenda-start-on-weekday
8090 (- nt org-agenda-start-on-weekday)
8093 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8094 (when n
8095 (require 'cal-iso)
8096 (when (> n 99)
8097 (setq y1 (org-small-year-to-year (/ n 100))
8098 n (mod n 100)))
8099 (setq sd
8100 (calendar-iso-to-absolute
8101 (list n 1
8102 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8103 ((eq span 'month)
8104 (let (y1)
8105 (when (and n (> n 99))
8106 (setq y1 (org-small-year-to-year (/ n 100))
8107 n (mod n 100)))
8108 (setq sd (calendar-absolute-from-gregorian
8109 (list (or n mg) 1 (or y1 yg))))))
8110 ((eq span 'year)
8111 (setq sd (calendar-absolute-from-gregorian
8112 (list 1 1 (or n yg))))))
8113 sd))
8115 (defun org-agenda-next-date-line (&optional arg)
8116 "Jump to the next line indicating a date in agenda buffer."
8117 (interactive "p")
8118 (org-agenda-check-type t 'agenda 'timeline)
8119 (beginning-of-line 1)
8120 ;; This does not work if user makes date format that starts with a blank
8121 (if (looking-at "^\\S-") (forward-char 1))
8122 (if (not (re-search-forward "^\\S-" nil t arg))
8123 (progn
8124 (backward-char 1)
8125 (error "No next date after this line in this buffer")))
8126 (goto-char (match-beginning 0)))
8128 (defun org-agenda-previous-date-line (&optional arg)
8129 "Jump to the previous line indicating a date in agenda buffer."
8130 (interactive "p")
8131 (org-agenda-check-type t 'agenda 'timeline)
8132 (beginning-of-line 1)
8133 (if (not (re-search-backward "^\\S-" nil t arg))
8134 (error "No previous date before this line in this buffer")))
8136 ;; Initialize the highlight
8137 (defvar org-hl (make-overlay 1 1))
8138 (overlay-put org-hl 'face 'highlight)
8140 (defun org-highlight (begin end &optional buffer)
8141 "Highlight a region with overlay."
8142 (move-overlay org-hl begin end (or buffer (current-buffer))))
8144 (defun org-unhighlight ()
8145 "Detach overlay INDEX."
8146 (delete-overlay org-hl))
8148 (defun org-unhighlight-once ()
8149 "Remove the highlight from its position, and this function from the hook."
8150 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8151 (org-unhighlight))
8153 (defvar org-agenda-pre-follow-window-conf nil)
8154 (defun org-agenda-follow-mode ()
8155 "Toggle follow mode in an agenda buffer."
8156 (interactive)
8157 (unless org-agenda-follow-mode
8158 (setq org-agenda-pre-follow-window-conf
8159 (current-window-configuration)))
8160 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8161 (unless org-agenda-follow-mode
8162 (set-window-configuration org-agenda-pre-follow-window-conf))
8163 (org-agenda-set-mode-name)
8164 (org-agenda-do-context-action)
8165 (message "Follow mode is %s"
8166 (if org-agenda-follow-mode "on" "off")))
8168 (defun org-agenda-entry-text-mode (&optional arg)
8169 "Toggle entry text mode in an agenda buffer."
8170 (interactive "P")
8171 (if (or org-agenda-tag-filter
8172 org-agenda-category-filter
8173 org-agenda-regexp-filter
8174 org-agenda-top-headline-filter)
8175 (user-error "Can't show entry text in filtered views")
8176 (setq org-agenda-entry-text-mode (or (integerp arg)
8177 (not org-agenda-entry-text-mode)))
8178 (org-agenda-entry-text-hide)
8179 (and org-agenda-entry-text-mode
8180 (let ((org-agenda-entry-text-maxlines
8181 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8182 (org-agenda-entry-text-show)))
8183 (org-agenda-set-mode-name)
8184 (message "Entry text mode is %s%s"
8185 (if org-agenda-entry-text-mode "on" "off")
8186 (if (not org-agenda-entry-text-mode) ""
8187 (format " (maximum number of lines is %d)"
8188 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8190 (defun org-agenda-clockreport-mode ()
8191 "Toggle clocktable mode in an agenda buffer."
8192 (interactive)
8193 (org-agenda-check-type t 'agenda)
8194 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8195 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8196 (org-agenda-set-mode-name)
8197 (org-agenda-redo)
8198 (message "Clocktable mode is %s"
8199 (if org-agenda-clockreport-mode "on" "off")))
8201 (defun org-agenda-log-mode (&optional special)
8202 "Toggle log mode in an agenda buffer.
8203 With argument SPECIAL, show all possible log items, not only the ones
8204 configured in `org-agenda-log-mode-items'.
8205 With a double \\[universal-argument] prefix arg, show *only* \
8206 log items, nothing else."
8207 (interactive "P")
8208 (org-agenda-check-type t 'agenda 'timeline)
8209 (setq org-agenda-show-log
8210 (cond
8211 ((equal special '(16)) 'only)
8212 ((eq special 'clockcheck)
8213 (if (eq org-agenda-show-log 'clockcheck)
8214 nil 'clockcheck))
8215 (special '(closed clock state))
8216 (t (not org-agenda-show-log))))
8217 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8218 (org-agenda-set-mode-name)
8219 (org-agenda-redo)
8220 (message "Log mode is %s"
8221 (if org-agenda-show-log "on" "off")))
8223 (defun org-agenda-archives-mode (&optional with-files)
8224 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8225 When called with a prefix argument, include all archive files as well."
8226 (interactive "P")
8227 (setq org-agenda-archives-mode
8228 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8229 (org-agenda-set-mode-name)
8230 (org-agenda-redo)
8231 (message
8232 "%s"
8233 (cond
8234 ((eq org-agenda-archives-mode nil)
8235 "No archives are included")
8236 ((eq org-agenda-archives-mode 'trees)
8237 (format "Trees with :%s: tag are included" org-archive-tag))
8238 ((eq org-agenda-archives-mode t)
8239 (format "Trees with :%s: tag and all active archive files are included"
8240 org-archive-tag)))))
8242 (defun org-agenda-toggle-diary ()
8243 "Toggle diary inclusion in an agenda buffer."
8244 (interactive)
8245 (org-agenda-check-type t 'agenda)
8246 (setq org-agenda-include-diary (not org-agenda-include-diary))
8247 (org-agenda-redo)
8248 (org-agenda-set-mode-name)
8249 (message "Diary inclusion turned %s"
8250 (if org-agenda-include-diary "on" "off")))
8252 (defun org-agenda-toggle-deadlines ()
8253 "Toggle inclusion of entries with a deadline in an agenda buffer."
8254 (interactive)
8255 (org-agenda-check-type t 'agenda)
8256 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8257 (org-agenda-redo)
8258 (org-agenda-set-mode-name)
8259 (message "Deadlines inclusion turned %s"
8260 (if org-agenda-include-deadlines "on" "off")))
8262 (defun org-agenda-toggle-time-grid ()
8263 "Toggle time grid in an agenda buffer."
8264 (interactive)
8265 (org-agenda-check-type t 'agenda)
8266 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8267 (org-agenda-redo)
8268 (org-agenda-set-mode-name)
8269 (message "Time-grid turned %s"
8270 (if org-agenda-use-time-grid "on" "off")))
8272 (defun org-agenda-set-mode-name ()
8273 "Set the mode name to indicate all the small mode settings."
8274 (setq mode-name
8275 (list "Org-Agenda"
8276 (if (get 'org-agenda-files 'org-restrict) " []" "")
8278 '(:eval (org-agenda-span-name org-agenda-current-span))
8279 (if org-agenda-follow-mode " Follow" "")
8280 (if org-agenda-entry-text-mode " ETxt" "")
8281 (if org-agenda-include-diary " Diary" "")
8282 (if org-agenda-include-deadlines " Ddl" "")
8283 (if org-agenda-use-time-grid " Grid" "")
8284 (if (and (boundp 'org-habit-show-habits)
8285 org-habit-show-habits) " Habit" "")
8286 (cond
8287 ((consp org-agenda-show-log) " LogAll")
8288 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8289 (org-agenda-show-log " Log")
8290 (t ""))
8291 (if (or org-agenda-category-filter
8292 (get 'org-agenda-category-filter :preset-filter))
8293 '(:eval (propertize
8294 (concat " <"
8295 (mapconcat
8296 'identity
8297 (append
8298 (get 'org-agenda-category-filter :preset-filter)
8299 org-agenda-category-filter)
8301 ">")
8302 'face 'org-agenda-filter-category
8303 'help-echo "Category used in filtering")) "")
8304 (if (or org-agenda-tag-filter
8305 (get 'org-agenda-tag-filter :preset-filter))
8306 '(:eval (propertize
8307 (concat " {"
8308 (mapconcat
8309 'identity
8310 (append
8311 (get 'org-agenda-tag-filter :preset-filter)
8312 org-agenda-tag-filter)
8314 "}")
8315 'face 'org-agenda-filter-tags
8316 'help-echo "Tags used in filtering")) "")
8317 (if (or org-agenda-effort-filter
8318 (get 'org-agenda-effort-filter :preset-filter))
8319 '(:eval (propertize
8320 (concat " {"
8321 (mapconcat
8322 'identity
8323 (append
8324 (get 'org-agenda-effort-filter :preset-filter)
8325 org-agenda-effort-filter)
8327 "}")
8328 'face 'org-agenda-filter-effort
8329 'help-echo "Effort conditions used in filtering")) "")
8330 (if (or org-agenda-regexp-filter
8331 (get 'org-agenda-regexp-filter :preset-filter))
8332 '(:eval (propertize
8333 (concat " ["
8334 (mapconcat
8335 'identity
8336 (append
8337 (get 'org-agenda-regexp-filter :preset-filter)
8338 org-agenda-regexp-filter)
8340 "]")
8341 'face 'org-agenda-filter-regexp
8342 'help-echo "Regexp used in filtering")) "")
8343 (if org-agenda-archives-mode
8344 (if (eq org-agenda-archives-mode t)
8345 " Archives"
8346 (format " :%s:" org-archive-tag))
8348 (if org-agenda-clockreport-mode " Clock" "")))
8349 (force-mode-line-update))
8351 (define-obsolete-function-alias
8352 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8354 (defun org-agenda-update-agenda-type ()
8355 "Update the agenda type after each command."
8356 (setq org-agenda-type
8357 (or (get-text-property (point) 'org-agenda-type)
8358 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8360 (defun org-agenda-next-line ()
8361 "Move cursor to the next line, and show if follow mode is active."
8362 (interactive)
8363 (call-interactively 'next-line)
8364 (org-agenda-do-context-action))
8366 (defun org-agenda-previous-line ()
8367 "Move cursor to the previous line, and show if follow-mode is active."
8368 (interactive)
8369 (call-interactively 'previous-line)
8370 (org-agenda-do-context-action))
8372 (defun org-agenda-next-item (n)
8373 "Move cursor to next agenda item."
8374 (interactive "p")
8375 (let ((col (current-column)))
8376 (dotimes (c n)
8377 (when (next-single-property-change (point-at-eol) 'org-marker)
8378 (move-end-of-line 1)
8379 (goto-char (next-single-property-change (point) 'org-marker))))
8380 (org-move-to-column col))
8381 (org-agenda-do-context-action))
8383 (defun org-agenda-previous-item (n)
8384 "Move cursor to next agenda item."
8385 (interactive "p")
8386 (dotimes (c n)
8387 (let ((col (current-column))
8388 (goto (save-excursion
8389 (move-end-of-line 0)
8390 (previous-single-property-change (point) 'org-marker))))
8391 (if goto (goto-char goto))
8392 (org-move-to-column col)))
8393 (org-agenda-do-context-action))
8395 (defun org-agenda-do-context-action ()
8396 "Show outline path and, maybe, follow mode window."
8397 (let ((m (org-get-at-bol 'org-marker)))
8398 (when (and (markerp m) (marker-buffer m))
8399 (and org-agenda-follow-mode
8400 (if org-agenda-follow-indirect
8401 (org-agenda-tree-to-indirect-buffer nil)
8402 (org-agenda-show)))
8403 (and org-agenda-show-outline-path
8404 (org-with-point-at m (org-display-outline-path t))))))
8406 (defun org-agenda-show-tags ()
8407 "Show the tags applicable to the current item."
8408 (interactive)
8409 (let* ((tags (org-get-at-bol 'tags)))
8410 (if tags
8411 (message "Tags are :%s:"
8412 (org-no-properties (mapconcat 'identity tags ":")))
8413 (message "No tags associated with this line"))))
8415 (defun org-agenda-goto (&optional highlight)
8416 "Go to the entry at point in the corresponding Org-mode file."
8417 (interactive)
8418 (let* ((marker (or (org-get-at-bol 'org-marker)
8419 (org-agenda-error)))
8420 (buffer (marker-buffer marker))
8421 (pos (marker-position marker)))
8422 (switch-to-buffer-other-window buffer)
8423 (widen)
8424 (push-mark)
8425 (goto-char pos)
8426 (when (derived-mode-p 'org-mode)
8427 (org-show-context 'agenda)
8428 (recenter (/ (window-height) 2))
8429 (org-back-to-heading t)
8430 (if (re-search-forward org-complex-heading-regexp nil t)
8431 (goto-char (match-beginning 4))))
8432 (run-hooks 'org-agenda-after-show-hook)
8433 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8435 (defvar org-agenda-after-show-hook nil
8436 "Normal hook run after an item has been shown from the agenda.
8437 Point is in the buffer where the item originated.")
8439 (defun org-agenda-kill ()
8440 "Kill the entry or subtree belonging to the current agenda entry."
8441 (interactive)
8442 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8443 (let* ((bufname-orig (buffer-name))
8444 (marker (or (org-get-at-bol 'org-marker)
8445 (org-agenda-error)))
8446 (buffer (marker-buffer marker))
8447 (pos (marker-position marker))
8448 (type (org-get-at-bol 'type))
8449 dbeg dend (n 0) conf)
8450 (org-with-remote-undo buffer
8451 (with-current-buffer buffer
8452 (save-excursion
8453 (goto-char pos)
8454 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8455 (setq dbeg (progn (org-back-to-heading t) (point))
8456 dend (org-end-of-subtree t t))
8457 (setq dbeg (point-at-bol)
8458 dend (min (point-max) (1+ (point-at-eol)))))
8459 (goto-char dbeg)
8460 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8461 (setq conf (or (eq t org-agenda-confirm-kill)
8462 (and (numberp org-agenda-confirm-kill)
8463 (> n org-agenda-confirm-kill))))
8464 (and conf
8465 (not (y-or-n-p
8466 (format "Delete entry with %d lines in buffer \"%s\"? "
8467 n (buffer-name buffer))))
8468 (error "Abort"))
8469 (let ((org-agenda-buffer-name bufname-orig))
8470 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8471 (with-current-buffer buffer (delete-region dbeg dend))
8472 (message "Agenda item and source killed"))))
8474 (defvar org-archive-default-command) ; defined in org-archive.el
8475 (defun org-agenda-archive-default ()
8476 "Archive the entry or subtree belonging to the current agenda entry."
8477 (interactive)
8478 (require 'org-archive)
8479 (org-agenda-archive-with org-archive-default-command))
8481 (defun org-agenda-archive-default-with-confirmation ()
8482 "Archive the entry or subtree belonging to the current agenda entry."
8483 (interactive)
8484 (require 'org-archive)
8485 (org-agenda-archive-with org-archive-default-command 'confirm))
8487 (defun org-agenda-archive ()
8488 "Archive the entry or subtree belonging to the current agenda entry."
8489 (interactive)
8490 (org-agenda-archive-with 'org-archive-subtree))
8492 (defun org-agenda-archive-to-archive-sibling ()
8493 "Move the entry to the archive sibling."
8494 (interactive)
8495 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8497 (defun org-agenda-archive-with (cmd &optional confirm)
8498 "Move the entry to the archive sibling."
8499 (interactive)
8500 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8501 (let* ((bufname-orig (buffer-name))
8502 (marker (or (org-get-at-bol 'org-marker)
8503 (org-agenda-error)))
8504 (buffer (marker-buffer marker))
8505 (pos (marker-position marker)))
8506 (org-with-remote-undo buffer
8507 (with-current-buffer buffer
8508 (if (derived-mode-p 'org-mode)
8509 (if (and confirm
8510 (not (y-or-n-p "Archive this subtree or entry? ")))
8511 (error "Abort")
8512 (save-window-excursion
8513 (goto-char pos)
8514 (let ((org-agenda-buffer-name bufname-orig))
8515 (org-remove-subtree-entries-from-agenda))
8516 (org-back-to-heading t)
8517 (funcall cmd)))
8518 (error "Archiving works only in Org-mode files"))))))
8520 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8521 "Remove all lines in the agenda that correspond to a given subtree.
8522 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8523 If this information is not given, the function uses the tree at point."
8524 (let ((buf (or buf (current-buffer))) m p)
8525 (save-excursion
8526 (unless (and beg end)
8527 (org-back-to-heading t)
8528 (setq beg (point))
8529 (org-end-of-subtree t)
8530 (setq end (point)))
8531 (set-buffer (get-buffer org-agenda-buffer-name))
8532 (save-excursion
8533 (goto-char (point-max))
8534 (beginning-of-line 1)
8535 (while (not (bobp))
8536 (when (and (setq m (org-get-at-bol 'org-marker))
8537 (equal buf (marker-buffer m))
8538 (setq p (marker-position m))
8539 (>= p beg)
8540 (< p end))
8541 (let ((inhibit-read-only t))
8542 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8543 (beginning-of-line 0))))))
8545 (defun org-agenda-refile (&optional goto rfloc no-update)
8546 "Refile the item at point.
8548 When GOTO is 0 or \\='(64) or a triple \\[universal-argument] prefix argument,
8549 clear the refile cache.
8550 When GOTO is \\='(16) or a double \\[universal-argument] prefix argument,
8551 go to the location of the last refiled item.
8552 RFLOC can be a refile location obtained in a different way.
8553 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8554 (interactive "P")
8555 (cond
8556 ((member goto '(0 (64)))
8557 (org-refile-cache-clear))
8558 ((equal goto '(16))
8559 (org-refile-goto-last-stored))
8561 (let* ((buffer-orig (buffer-name))
8562 (marker (or (org-get-at-bol 'org-hd-marker)
8563 (org-agenda-error)))
8564 (buffer (marker-buffer marker))
8565 (pos (marker-position marker))
8566 (rfloc (or rfloc
8567 (org-refile-get-location
8568 (if goto "Goto" "Refile to") buffer
8569 org-refile-allow-creating-parent-nodes))))
8570 (with-current-buffer buffer
8571 (save-excursion
8572 (save-restriction
8573 (widen)
8574 (goto-char marker)
8575 (let ((org-agenda-buffer-name buffer-orig))
8576 (org-remove-subtree-entries-from-agenda))
8577 (org-refile goto buffer rfloc)))))
8578 (unless no-update (org-agenda-redo)))))
8580 (defun org-agenda-open-link (&optional arg)
8581 "Open the link(s) in the current entry, if any.
8582 This looks for a link in the displayed line in the agenda.
8583 It also looks at the text of the entry itself."
8584 (interactive "P")
8585 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8586 (org-get-at-bol 'org-marker)))
8587 (buffer (and marker (marker-buffer marker)))
8588 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8589 (lkall (and buffer (org-offer-links-in-entry
8590 buffer marker arg prefix)))
8591 (lk0 (car lkall))
8592 (lk (if (stringp lk0) (list lk0) lk0))
8593 (lkend (cdr lkall))
8594 trg)
8595 (cond
8596 ((and buffer lk)
8597 (mapcar (lambda(l)
8598 (with-current-buffer buffer
8599 (setq trg (and (string-match org-bracket-link-regexp l)
8600 (match-string 1 l)))
8601 (if (or (not trg) (string-match org-any-link-re trg))
8602 (save-excursion
8603 (save-restriction
8604 (widen)
8605 (goto-char marker)
8606 (when (search-forward l nil lkend)
8607 (goto-char (match-beginning 0))
8608 (org-open-at-point))))
8609 ;; This is an internal link, widen the buffer
8610 (switch-to-buffer-other-window buffer)
8611 (widen)
8612 (goto-char marker)
8613 (when (search-forward l nil lkend)
8614 (goto-char (match-beginning 0))
8615 (org-open-at-point)))))
8616 lk))
8617 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8618 (save-excursion
8619 (beginning-of-line 1)
8620 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8621 (org-open-link-from-string (match-string 1)))
8622 (t (message "No link to open here")))))
8624 (defun org-agenda-copy-local-variable (var)
8625 "Get a variable from a referenced buffer and install it here."
8626 (let ((m (org-get-at-bol 'org-marker)))
8627 (when (and m (buffer-live-p (marker-buffer m)))
8628 (set (make-local-variable var)
8629 (with-current-buffer (marker-buffer m)
8630 (symbol-value var))))))
8632 (defun org-agenda-switch-to (&optional delete-other-windows)
8633 "Go to the Org mode file which contains the item at point.
8634 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8635 displayed Org file fills the frame."
8636 (interactive)
8637 (if (and org-return-follows-link
8638 (not (org-get-at-bol 'org-marker))
8639 (org-in-regexp org-bracket-link-regexp))
8640 (org-open-link-from-string (match-string 0))
8641 (let* ((marker (or (org-get-at-bol 'org-marker)
8642 (org-agenda-error)))
8643 (buffer (marker-buffer marker))
8644 (pos (marker-position marker)))
8645 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8646 (org-pop-to-buffer-same-window buffer)
8647 (when delete-other-windows (delete-other-windows))
8648 (widen)
8649 (goto-char pos)
8650 (when (derived-mode-p 'org-mode)
8651 (org-show-context 'agenda)
8652 (run-hooks 'org-agenda-after-show-hook)))))
8654 (defun org-agenda-goto-mouse (ev)
8655 "Go to the Org-mode file which contains the item at the mouse click."
8656 (interactive "e")
8657 (mouse-set-point ev)
8658 (org-agenda-goto))
8660 (defun org-agenda-show (&optional full-entry)
8661 "Display the Org-mode file which contains the item at point.
8662 With prefix argument FULL-ENTRY, make the entire entry visible
8663 if it was hidden in the outline."
8664 (interactive "P")
8665 (let ((win (selected-window)))
8666 (org-agenda-goto t)
8667 (when full-entry (org-show-entry))
8668 (select-window win)))
8670 (defvar org-agenda-show-window nil)
8671 (defun org-agenda-show-and-scroll-up (&optional arg)
8672 "Display the Org-mode file which contains the item at point.
8673 When called repeatedly, scroll the window that is displaying the buffer.
8674 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8675 `show-subtree' to display the item, so that drawers and logbooks stay
8676 folded."
8677 (interactive "P")
8678 (let ((win (selected-window)))
8679 (if (and (window-live-p org-agenda-show-window)
8680 (eq this-command last-command))
8681 (progn
8682 (select-window org-agenda-show-window)
8683 (ignore-errors (scroll-up)))
8684 (org-agenda-goto t)
8685 (if arg (org-show-entry) (outline-show-subtree))
8686 (setq org-agenda-show-window (selected-window)))
8687 (select-window win)))
8689 (defun org-agenda-show-scroll-down ()
8690 "Scroll down the window showing the agenda."
8691 (interactive)
8692 (let ((win (selected-window)))
8693 (when (window-live-p org-agenda-show-window)
8694 (select-window org-agenda-show-window)
8695 (ignore-errors (scroll-down))
8696 (select-window win))))
8698 (defun org-agenda-show-1 (&optional more)
8699 "Display the Org-mode file which contains the item at point.
8700 The prefix arg selects the amount of information to display:
8702 0 hide the subtree
8703 1 just show the entry according to defaults.
8704 2 show the children view
8705 3 show the subtree view
8706 4 show the entire subtree and any LOGBOOK drawers
8707 5 show the entire subtree and any drawers
8708 With prefix argument FULL-ENTRY, make the entire entry visible
8709 if it was hidden in the outline."
8710 (interactive "p")
8711 (let ((win (selected-window)))
8712 (org-agenda-goto t)
8713 (org-back-to-heading)
8714 (set-window-start (selected-window) (point-at-bol))
8715 (cond
8716 ((= more 0)
8717 (outline-hide-subtree)
8718 (save-excursion
8719 (org-back-to-heading)
8720 (run-hook-with-args 'org-cycle-hook 'folded))
8721 (message "Remote: FOLDED"))
8722 ((and (org-called-interactively-p 'any) (= more 1))
8723 (message "Remote: show with default settings"))
8724 ((= more 2)
8725 (outline-show-entry)
8726 (org-show-children)
8727 (save-excursion
8728 (org-back-to-heading)
8729 (run-hook-with-args 'org-cycle-hook 'children))
8730 (message "Remote: CHILDREN"))
8731 ((= more 3)
8732 (outline-show-subtree)
8733 (save-excursion
8734 (org-back-to-heading)
8735 (run-hook-with-args 'org-cycle-hook 'subtree))
8736 (message "Remote: SUBTREE"))
8737 ((= more 4)
8738 (outline-show-subtree)
8739 (save-excursion
8740 (org-back-to-heading)
8741 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8742 (message "Remote: SUBTREE AND LOGBOOK"))
8743 ((> more 4)
8744 (outline-show-subtree)
8745 (message "Remote: SUBTREE AND ALL DRAWERS")))
8746 (select-window win)))
8748 (defvar org-agenda-cycle-counter nil)
8749 (defun org-agenda-cycle-show (&optional n)
8750 "Show the current entry in another window, with default settings.
8752 Default settings are taken from `org-show-context-detail'. When
8753 use repeatedly in immediate succession, the remote entry will
8754 cycle through visibility
8756 children -> subtree -> folded
8758 When called with a numeric prefix arg, that arg will be passed through to
8759 `org-agenda-show-1'. For the interpretation of that argument, see the
8760 docstring of `org-agenda-show-1'."
8761 (interactive "P")
8762 (if (integerp n)
8763 (setq org-agenda-cycle-counter n)
8764 (if (not (eq last-command this-command))
8765 (setq org-agenda-cycle-counter 1)
8766 (if (equal org-agenda-cycle-counter 0)
8767 (setq org-agenda-cycle-counter 2)
8768 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8769 (if (> org-agenda-cycle-counter 3)
8770 (setq org-agenda-cycle-counter 0)))))
8771 (org-agenda-show-1 org-agenda-cycle-counter))
8773 (defun org-agenda-recenter (arg)
8774 "Display the Org-mode file which contains the item at point and recenter."
8775 (interactive "P")
8776 (let ((win (selected-window)))
8777 (org-agenda-goto t)
8778 (recenter arg)
8779 (select-window win)))
8781 (defun org-agenda-show-mouse (ev)
8782 "Display the Org-mode file which contains the item at the mouse click."
8783 (interactive "e")
8784 (mouse-set-point ev)
8785 (org-agenda-show))
8787 (defun org-agenda-check-no-diary ()
8788 "Check if the entry is a diary link and abort if yes."
8789 (if (org-get-at-bol 'org-agenda-diary-link)
8790 (org-agenda-error)))
8792 (defun org-agenda-error ()
8793 "Throw an error when a command is not allowed in the agenda."
8794 (user-error "Command not allowed in this line"))
8796 (defun org-agenda-tree-to-indirect-buffer (arg)
8797 "Show the subtree corresponding to the current entry in an indirect buffer.
8798 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8800 With a numerical prefix ARG, go up to this level and then take that tree.
8801 With a negative numeric ARG, go up by this number of levels.
8802 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8803 use the dedicated frame)."
8804 (interactive "P")
8805 (if current-prefix-arg
8806 (org-agenda-do-tree-to-indirect-buffer arg)
8807 (let ((agenda-buffer (buffer-name))
8808 (agenda-window (selected-window))
8809 (indirect-window
8810 (and org-last-indirect-buffer
8811 (get-buffer-window org-last-indirect-buffer))))
8812 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8813 (unless (or (eq org-indirect-buffer-display 'new-frame)
8814 (eq org-indirect-buffer-display 'dedicated-frame))
8815 (unwind-protect
8816 (unless (and indirect-window (window-live-p indirect-window))
8817 (setq indirect-window (split-window agenda-window)))
8818 (and indirect-window (select-window indirect-window))
8819 (switch-to-buffer org-last-indirect-buffer :norecord)
8820 (fit-window-to-buffer indirect-window)))
8821 (select-window (get-buffer-window agenda-buffer))
8822 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8824 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8825 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8826 (org-agenda-check-no-diary)
8827 (let* ((marker (or (org-get-at-bol 'org-marker)
8828 (org-agenda-error)))
8829 (buffer (marker-buffer marker))
8830 (pos (marker-position marker)))
8831 (with-current-buffer buffer
8832 (save-excursion
8833 (goto-char pos)
8834 (funcall 'org-tree-to-indirect-buffer arg)))))
8836 (defvar org-last-heading-marker (make-marker)
8837 "Marker pointing to the headline that last changed its TODO state
8838 by a remote command from the agenda.")
8840 (defun org-agenda-todo-nextset ()
8841 "Switch TODO entry to next sequence."
8842 (interactive)
8843 (org-agenda-todo 'nextset))
8845 (defun org-agenda-todo-previousset ()
8846 "Switch TODO entry to previous sequence."
8847 (interactive)
8848 (org-agenda-todo 'previousset))
8850 (defun org-agenda-todo (&optional arg)
8851 "Cycle TODO state of line at point, also in Org-mode file.
8852 This changes the line at point, all other lines in the agenda referring to
8853 the same tree node, and the headline of the tree node in the Org-mode file."
8854 (interactive "P")
8855 (org-agenda-check-no-diary)
8856 (let* ((col (current-column))
8857 (marker (or (org-get-at-bol 'org-marker)
8858 (org-agenda-error)))
8859 (buffer (marker-buffer marker))
8860 (pos (marker-position marker))
8861 (hdmarker (org-get-at-bol 'org-hd-marker))
8862 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8863 (inhibit-read-only t)
8864 org-agenda-headline-snapshot-before-repeat newhead just-one)
8865 (org-with-remote-undo buffer
8866 (with-current-buffer buffer
8867 (widen)
8868 (goto-char pos)
8869 (org-show-context 'agenda)
8870 (let ((current-prefix-arg arg))
8871 (call-interactively 'org-todo))
8872 (and (bolp) (forward-char 1))
8873 (setq newhead (org-get-heading))
8874 (when (and (org-bound-and-true-p
8875 org-agenda-headline-snapshot-before-repeat)
8876 (not (equal org-agenda-headline-snapshot-before-repeat
8877 newhead))
8878 todayp)
8879 (setq newhead org-agenda-headline-snapshot-before-repeat
8880 just-one t))
8881 (save-excursion
8882 (org-back-to-heading)
8883 (move-marker org-last-heading-marker (point))))
8884 (beginning-of-line 1)
8885 (save-window-excursion
8886 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8887 (when (org-bound-and-true-p org-clock-out-when-done)
8888 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8889 newhead)
8890 (org-agenda-unmark-clocking-task))
8891 (org-move-to-column col)
8892 (org-agenda-mark-clocking-task))))
8894 (defun org-agenda-add-note (&optional arg)
8895 "Add a time-stamped note to the entry at point."
8896 (interactive "P")
8897 (org-agenda-check-no-diary)
8898 (let* ((marker (or (org-get-at-bol 'org-marker)
8899 (org-agenda-error)))
8900 (buffer (marker-buffer marker))
8901 (pos (marker-position marker))
8902 (hdmarker (org-get-at-bol 'org-hd-marker))
8903 (inhibit-read-only t))
8904 (with-current-buffer buffer
8905 (widen)
8906 (goto-char pos)
8907 (org-show-context 'agenda)
8908 (org-add-note))))
8910 (defun org-agenda-change-all-lines (newhead hdmarker
8911 &optional fixface just-this)
8912 "Change all lines in the agenda buffer which match HDMARKER.
8913 The new content of the line will be NEWHEAD (as modified by
8914 `org-agenda-format-item'). HDMARKER is checked with
8915 `equal' against all `org-hd-marker' text properties in the file.
8916 If FIXFACE is non-nil, the face of each item is modified according to
8917 the new TODO state.
8918 If JUST-THIS is non-nil, change just the current line, not all.
8919 If FORCE-TAGS is non nil, the car of it returns the new tags."
8920 (let* ((inhibit-read-only t)
8921 (line (org-current-line))
8922 (org-agenda-buffer (current-buffer))
8923 (thetags (with-current-buffer (marker-buffer hdmarker)
8924 (save-excursion (save-restriction (widen)
8925 (goto-char hdmarker)
8926 (org-get-tags-at)))))
8927 props m pl undone-face done-face finish new dotime level cat tags)
8928 (save-excursion
8929 (goto-char (point-max))
8930 (beginning-of-line 1)
8931 (while (not finish)
8932 (setq finish (bobp))
8933 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8934 (or (not just-this) (= (org-current-line) line))
8935 (equal m hdmarker))
8936 (setq props (text-properties-at (point))
8937 dotime (org-get-at-bol 'dotime)
8938 cat (org-get-at-eol 'org-category 1)
8939 level (org-get-at-bol 'level)
8940 tags thetags
8942 (let ((org-prefix-format-compiled
8943 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8944 org-prefix-format-compiled))
8945 (extra (org-get-at-bol 'extra)))
8946 (with-current-buffer (marker-buffer hdmarker)
8947 (save-excursion
8948 (save-restriction
8949 (widen)
8950 (org-agenda-format-item extra newhead level cat tags dotime)))))
8951 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8952 undone-face (org-get-at-bol 'undone-face)
8953 done-face (org-get-at-bol 'done-face))
8954 (beginning-of-line 1)
8955 (cond
8956 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8957 ((looking-at ".*")
8958 ;; When replacing the whole line, preserve bulk mark
8959 ;; overlay, if any.
8960 (let ((mark (catch :overlay
8961 (dolist (o (overlays-in (point) (+ 2 (point))))
8962 (when (eq (overlay-get o 'type)
8963 'org-marked-entry-overlay)
8964 (throw :overlay o))))))
8965 (replace-match new t t)
8966 (beginning-of-line)
8967 (when mark (move-overlay mark (point) (+ 2 (point)))))
8968 (add-text-properties (point-at-bol) (point-at-eol) props)
8969 (when fixface
8970 (add-text-properties
8971 (point-at-bol) (point-at-eol)
8972 (list 'face
8973 (if org-last-todo-state-is-todo
8974 undone-face done-face))))
8975 (org-agenda-highlight-todo 'line)
8976 (beginning-of-line 1))
8977 (t (error "Line update did not work")))
8978 (save-restriction
8979 (narrow-to-region (point-at-bol) (point-at-eol))
8980 (org-agenda-finalize)))
8981 (beginning-of-line 0)))))
8983 (defun org-agenda-align-tags (&optional line)
8984 "Align all tags in agenda items to `org-agenda-tags-column'."
8985 (let ((inhibit-read-only t) l c)
8986 (save-excursion
8987 (goto-char (if line (point-at-bol) (point-min)))
8988 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8989 (if line (point-at-eol) nil) t)
8990 (add-text-properties
8991 (match-beginning 2) (match-end 2)
8992 (list 'face (delq nil (let ((prop (get-text-property
8993 (match-beginning 2) 'face)))
8994 (or (listp prop) (setq prop (list prop)))
8995 (if (memq 'org-tag prop)
8996 prop
8997 (cons 'org-tag prop))))))
8998 (setq l (- (match-end 2) (match-beginning 2))
8999 c (if (< org-agenda-tags-column 0)
9000 (- (abs org-agenda-tags-column) l)
9001 org-agenda-tags-column))
9002 (delete-region (match-beginning 1) (match-end 1))
9003 (goto-char (match-beginning 1))
9004 (insert (org-add-props
9005 (make-string (max 1 (- c (current-column))) ?\ )
9006 (plist-put (copy-sequence (text-properties-at (point)))
9007 'face nil))))
9008 (goto-char (point-min))
9009 (org-font-lock-add-tag-faces (point-max)))))
9011 (defun org-agenda-priority-up ()
9012 "Increase the priority of line at point, also in Org-mode file."
9013 (interactive)
9014 (org-agenda-priority 'up))
9016 (defun org-agenda-priority-down ()
9017 "Decrease the priority of line at point, also in Org-mode file."
9018 (interactive)
9019 (org-agenda-priority 'down))
9021 (defun org-agenda-priority (&optional force-direction)
9022 "Set the priority of line at point, also in Org-mode file.
9023 This changes the line at point, all other lines in the agenda referring to
9024 the same tree node, and the headline of the tree node in the Org-mode file.
9025 Called with a universal prefix arg, show the priority instead of setting it."
9026 (interactive "P")
9027 (if (equal force-direction '(4))
9028 (org-show-priority)
9029 (unless org-enable-priority-commands
9030 (error "Priority commands are disabled"))
9031 (org-agenda-check-no-diary)
9032 (let* ((col (current-column))
9033 (marker (or (org-get-at-bol 'org-marker)
9034 (org-agenda-error)))
9035 (hdmarker (org-get-at-bol 'org-hd-marker))
9036 (buffer (marker-buffer hdmarker))
9037 (pos (marker-position hdmarker))
9038 (inhibit-read-only t)
9039 newhead)
9040 (org-with-remote-undo buffer
9041 (with-current-buffer buffer
9042 (widen)
9043 (goto-char pos)
9044 (org-show-context 'agenda)
9045 (funcall 'org-priority force-direction)
9046 (end-of-line 1)
9047 (setq newhead (org-get-heading)))
9048 (org-agenda-change-all-lines newhead hdmarker)
9049 (org-move-to-column col)))))
9051 ;; FIXME: should fix the tags property of the agenda line.
9052 (defun org-agenda-set-tags (&optional tag onoff)
9053 "Set tags for the current headline."
9054 (interactive)
9055 (org-agenda-check-no-diary)
9056 (if (and (org-region-active-p) (org-called-interactively-p 'any))
9057 (call-interactively 'org-change-tag-in-region)
9058 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9059 (org-agenda-error)))
9060 (buffer (marker-buffer hdmarker))
9061 (pos (marker-position hdmarker))
9062 (inhibit-read-only t)
9063 newhead)
9064 (org-with-remote-undo buffer
9065 (with-current-buffer buffer
9066 (widen)
9067 (goto-char pos)
9068 (org-show-context 'agenda)
9069 (if tag
9070 (org-toggle-tag tag onoff)
9071 (call-interactively 'org-set-tags))
9072 (end-of-line 1)
9073 (setq newhead (org-get-heading)))
9074 (org-agenda-change-all-lines newhead hdmarker)
9075 (beginning-of-line 1)))))
9077 (defun org-agenda-set-property ()
9078 "Set a property for the current headline."
9079 (interactive)
9080 (org-agenda-check-no-diary)
9081 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9082 (org-agenda-error)))
9083 (buffer (marker-buffer hdmarker))
9084 (pos (marker-position hdmarker))
9085 (inhibit-read-only t)
9086 newhead)
9087 (org-with-remote-undo buffer
9088 (with-current-buffer buffer
9089 (widen)
9090 (goto-char pos)
9091 (org-show-context 'agenda)
9092 (call-interactively 'org-set-property)))))
9094 (defun org-agenda-set-effort ()
9095 "Set the effort property for the current headline."
9096 (interactive)
9097 (org-agenda-check-no-diary)
9098 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9099 (org-agenda-error)))
9100 (buffer (marker-buffer hdmarker))
9101 (pos (marker-position hdmarker))
9102 (inhibit-read-only t)
9103 newhead)
9104 (org-with-remote-undo buffer
9105 (with-current-buffer buffer
9106 (widen)
9107 (goto-char pos)
9108 (org-show-context 'agenda)
9109 (call-interactively 'org-set-effort)
9110 (end-of-line 1)
9111 (setq newhead (org-get-heading)))
9112 (org-agenda-change-all-lines newhead hdmarker))))
9114 (defun org-agenda-toggle-archive-tag ()
9115 "Toggle the archive tag for the current entry."
9116 (interactive)
9117 (org-agenda-check-no-diary)
9118 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9119 (org-agenda-error)))
9120 (buffer (marker-buffer hdmarker))
9121 (pos (marker-position hdmarker))
9122 (inhibit-read-only t)
9123 newhead)
9124 (org-with-remote-undo buffer
9125 (with-current-buffer buffer
9126 (widen)
9127 (goto-char pos)
9128 (org-show-context 'agenda)
9129 (call-interactively 'org-toggle-archive-tag)
9130 (end-of-line 1)
9131 (setq newhead (org-get-heading)))
9132 (org-agenda-change-all-lines newhead hdmarker)
9133 (beginning-of-line 1))))
9135 (defun org-agenda-do-date-later (arg)
9136 (interactive "P")
9137 (cond
9138 ((or (equal arg '(16))
9139 (memq last-command
9140 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9141 (setq this-command 'org-agenda-date-later-minutes)
9142 (org-agenda-date-later-minutes 1))
9143 ((or (equal arg '(4))
9144 (memq last-command
9145 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9146 (setq this-command 'org-agenda-date-later-hours)
9147 (org-agenda-date-later-hours 1))
9149 (org-agenda-date-later (prefix-numeric-value arg)))))
9151 (defun org-agenda-do-date-earlier (arg)
9152 (interactive "P")
9153 (cond
9154 ((or (equal arg '(16))
9155 (memq last-command
9156 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9157 (setq this-command 'org-agenda-date-earlier-minutes)
9158 (org-agenda-date-earlier-minutes 1))
9159 ((or (equal arg '(4))
9160 (memq last-command
9161 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9162 (setq this-command 'org-agenda-date-earlier-hours)
9163 (org-agenda-date-earlier-hours 1))
9165 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9167 (defun org-agenda-date-later (arg &optional what)
9168 "Change the date of this item to ARG day(s) later."
9169 (interactive "p")
9170 (org-agenda-check-type t 'agenda 'timeline)
9171 (org-agenda-check-no-diary)
9172 (let* ((marker (or (org-get-at-bol 'org-marker)
9173 (org-agenda-error)))
9174 (buffer (marker-buffer marker))
9175 (pos (marker-position marker))
9176 cdate today)
9177 (org-with-remote-undo buffer
9178 (with-current-buffer buffer
9179 (widen)
9180 (goto-char pos)
9181 (if (not (org-at-timestamp-p))
9182 (error "Cannot find time stamp"))
9183 (when (and org-agenda-move-date-from-past-immediately-to-today
9184 (equal arg 1)
9185 (or (not what) (eq what 'day))
9186 (not (save-match-data (org-at-date-range-p))))
9187 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9188 cdate (calendar-absolute-from-gregorian
9189 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9190 today (org-today))
9191 (if (> today cdate)
9192 ;; immediately shift to today
9193 (setq arg (- today cdate))))
9194 (org-timestamp-change arg (or what 'day))
9195 (when (and (org-at-date-range-p)
9196 (re-search-backward org-tr-regexp-both (point-at-bol)))
9197 (let ((end org-last-changed-timestamp))
9198 (org-timestamp-change arg (or what 'day))
9199 (setq org-last-changed-timestamp
9200 (concat org-last-changed-timestamp "--" end)))))
9201 (org-agenda-show-new-time marker org-last-changed-timestamp))
9202 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9204 (defun org-agenda-date-earlier (arg &optional what)
9205 "Change the date of this item to ARG day(s) earlier."
9206 (interactive "p")
9207 (org-agenda-date-later (- arg) what))
9209 (defun org-agenda-date-later-minutes (arg)
9210 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9211 (interactive "p")
9212 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9213 (org-agenda-date-later arg 'minute))
9215 (defun org-agenda-date-earlier-minutes (arg)
9216 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9217 (interactive "p")
9218 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9219 (org-agenda-date-earlier arg 'minute))
9221 (defun org-agenda-date-later-hours (arg)
9222 "Change the time of this item, in hour steps."
9223 (interactive "p")
9224 (org-agenda-date-later arg 'hour))
9226 (defun org-agenda-date-earlier-hours (arg)
9227 "Change the time of this item, in hour steps."
9228 (interactive "p")
9229 (org-agenda-date-earlier arg 'hour))
9231 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9232 "Show new date stamp via text properties."
9233 ;; We use text properties to make this undoable
9234 (let ((inhibit-read-only t))
9235 (setq stamp (concat prefix " => " stamp " "))
9236 (save-excursion
9237 (goto-char (point-max))
9238 (while (not (bobp))
9239 (when (equal marker (org-get-at-bol 'org-marker))
9240 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9241 (org-move-to-column (- (window-width) (length stamp)) t)
9242 (add-text-properties
9243 (1- (point)) (point-at-eol)
9244 (list 'display (org-add-props stamp nil
9245 'face '(secondary-selection default))))
9246 (beginning-of-line 1))
9247 (beginning-of-line 0)))))
9249 (defun org-agenda-date-prompt (arg)
9250 "Change the date of this item. Date is prompted for, with default today.
9251 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9252 be used to request time specification in the time stamp."
9253 (interactive "P")
9254 (org-agenda-check-type t 'agenda 'timeline)
9255 (org-agenda-check-no-diary)
9256 (let* ((marker (or (org-get-at-bol 'org-marker)
9257 (org-agenda-error)))
9258 (buffer (marker-buffer marker))
9259 (pos (marker-position marker)))
9260 (org-with-remote-undo buffer
9261 (with-current-buffer buffer
9262 (widen)
9263 (goto-char pos)
9264 (if (not (org-at-timestamp-p t))
9265 (error "Cannot find time stamp"))
9266 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9267 (org-agenda-show-new-time marker org-last-changed-timestamp))
9268 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9270 (defun org-agenda-schedule (arg &optional time)
9271 "Schedule the item at point.
9272 ARG is passed through to `org-schedule'."
9273 (interactive "P")
9274 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9275 (org-agenda-check-no-diary)
9276 (let* ((marker (or (org-get-at-bol 'org-marker)
9277 (org-agenda-error)))
9278 (type (marker-insertion-type marker))
9279 (buffer (marker-buffer marker))
9280 (pos (marker-position marker))
9282 (set-marker-insertion-type marker t)
9283 (org-with-remote-undo buffer
9284 (with-current-buffer buffer
9285 (widen)
9286 (goto-char pos)
9287 (setq ts (org-schedule arg time)))
9288 (org-agenda-show-new-time marker ts " S"))
9289 (message "%s" ts)))
9291 (defun org-agenda-deadline (arg &optional time)
9292 "Schedule the item at point.
9293 ARG is passed through to `org-deadline'."
9294 (interactive "P")
9295 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9296 (org-agenda-check-no-diary)
9297 (let* ((marker (or (org-get-at-bol 'org-marker)
9298 (org-agenda-error)))
9299 (buffer (marker-buffer marker))
9300 (pos (marker-position marker))
9302 (org-with-remote-undo buffer
9303 (with-current-buffer buffer
9304 (widen)
9305 (goto-char pos)
9306 (setq ts (org-deadline arg time)))
9307 (org-agenda-show-new-time marker ts " D"))
9308 (message "%s" ts)))
9310 (defun org-agenda-clock-in (&optional arg)
9311 "Start the clock on the currently selected item."
9312 (interactive "P")
9313 (org-agenda-check-no-diary)
9314 (if (equal arg '(4))
9315 (org-clock-in arg)
9316 (let* ((marker (or (org-get-at-bol 'org-marker)
9317 (org-agenda-error)))
9318 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9319 (pos (marker-position marker))
9320 (col (current-column))
9321 newhead)
9322 (org-with-remote-undo (marker-buffer marker)
9323 (with-current-buffer (marker-buffer marker)
9324 (widen)
9325 (goto-char pos)
9326 (org-show-context 'agenda)
9327 (org-cycle-hide-drawers 'children)
9328 (org-clock-in arg)
9329 (setq newhead (org-get-heading)))
9330 (org-agenda-change-all-lines newhead hdmarker))
9331 (org-move-to-column col))))
9333 (defun org-agenda-clock-out ()
9334 "Stop the currently running clock."
9335 (interactive)
9336 (unless (marker-buffer org-clock-marker)
9337 (error "No running clock"))
9338 (let ((marker (make-marker)) (col (current-column)) newhead)
9339 (org-with-remote-undo (marker-buffer org-clock-marker)
9340 (with-current-buffer (marker-buffer org-clock-marker)
9341 (save-excursion
9342 (save-restriction
9343 (widen)
9344 (goto-char org-clock-marker)
9345 (org-back-to-heading t)
9346 (move-marker marker (point))
9347 (org-clock-out)
9348 (setq newhead (org-get-heading))))))
9349 (org-agenda-change-all-lines newhead marker)
9350 (move-marker marker nil)
9351 (org-move-to-column col)
9352 (org-agenda-unmark-clocking-task)))
9354 (defun org-agenda-clock-cancel (&optional arg)
9355 "Cancel the currently running clock."
9356 (interactive "P")
9357 (unless (marker-buffer org-clock-marker)
9358 (user-error "No running clock"))
9359 (org-with-remote-undo (marker-buffer org-clock-marker)
9360 (org-clock-cancel)))
9362 (defun org-agenda-clock-goto ()
9363 "Jump to the currently clocked in task within the agenda.
9364 If the currently clocked in task is not listed in the agenda
9365 buffer, display it in another window."
9366 (interactive)
9367 (let (pos)
9368 (mapc (lambda (o)
9369 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9370 (setq pos (overlay-start o))))
9371 (overlays-in (point-min) (point-max)))
9372 (cond (pos (goto-char pos))
9373 ;; If the currently clocked entry is not in the agenda
9374 ;; buffer, we visit it in another window:
9375 ((bound-and-true-p org-clock-current-task)
9376 (org-switch-to-buffer-other-window (org-clock-goto)))
9377 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9379 (defun org-agenda-diary-entry-in-org-file ()
9380 "Make a diary entry in the file `org-agenda-diary-file'."
9381 (let (d1 d2 char (text "") dp1 dp2)
9382 (if (equal (buffer-name) "*Calendar*")
9383 (setq d1 (calendar-cursor-to-date t)
9384 d2 (car calendar-mark-ring))
9385 (setq dp1 (get-text-property (point-at-bol) 'day))
9386 (unless dp1 (user-error "No date defined in current line"))
9387 (setq d1 (calendar-gregorian-from-absolute dp1)
9388 d2 (and (ignore-errors (mark))
9389 (save-excursion
9390 (goto-char (mark))
9391 (setq dp2 (get-text-property (point-at-bol) 'day)))
9392 (calendar-gregorian-from-absolute dp2))))
9393 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9394 (setq char (read-char-exclusive))
9395 (cond
9396 ((equal char ?d)
9397 (setq text (read-string "Day entry: "))
9398 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9399 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9400 ((equal char ?a)
9401 (setq d1 (list (car d1) (nth 1 d1)
9402 (read-number (format "Reference year [%d]: " (nth 2 d1))
9403 (nth 2 d1))))
9404 (setq text (read-string "Anniversary (use %d to show years): "))
9405 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9406 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9407 ((equal char ?b)
9408 (setq text (read-string "Block entry: "))
9409 (unless (and d1 d2 (not (equal d1 d2)))
9410 (user-error "No block of days selected"))
9411 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9412 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9413 ((equal char ?j)
9414 (org-switch-to-buffer-other-window
9415 (find-file-noselect org-agenda-diary-file))
9416 (require 'org-datetree)
9417 (org-datetree-find-date-create d1)
9418 (org-reveal t))
9419 (t (user-error "Invalid selection character `%c'" char)))))
9421 (defcustom org-agenda-insert-diary-strategy 'date-tree
9422 "Where in `org-agenda-diary-file' should new entries be added?
9423 Valid values:
9425 date-tree in the date tree, as first child of the date
9426 date-tree-last in the date tree, as last child of the date
9427 top-level as top-level entries at the end of the file."
9428 :group 'org-agenda
9429 :type '(choice
9430 (const :tag "first in a date tree" date-tree)
9431 (const :tag "last in a date tree" date-tree-last)
9432 (const :tag "as top level at end of file" top-level)))
9434 (defcustom org-agenda-insert-diary-extract-time nil
9435 "Non-nil means extract any time specification from the diary entry."
9436 :group 'org-agenda
9437 :version "24.1"
9438 :type 'boolean)
9440 (defcustom org-agenda-bulk-mark-char ">"
9441 "A single-character string to be used as the bulk mark."
9442 :group 'org-agenda
9443 :version "24.1"
9444 :type 'string)
9446 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9447 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9448 If TEXT is not empty, it will become the headline of the new entry, and
9449 the resulting entry will not be shown. When TEXT is empty, switch to
9450 `org-agenda-diary-file' and let the user finish the entry there."
9451 (let ((cw (current-window-configuration)))
9452 (org-switch-to-buffer-other-window
9453 (find-file-noselect org-agenda-diary-file))
9454 (widen)
9455 (goto-char (point-min))
9456 (cond
9457 ((eq type 'anniversary)
9458 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9459 (progn
9460 (or (org-at-heading-p t)
9461 (progn
9462 (outline-next-heading)
9463 (insert "* Anniversaries\n\n")
9464 (beginning-of-line -1)))))
9465 (outline-next-heading)
9466 (org-back-over-empty-lines)
9467 (backward-char 1)
9468 (insert "\n")
9469 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9470 (nth 2 d1) (car d1) (nth 1 d1) text)))
9471 ((eq type 'day)
9472 (let ((org-prefix-has-time t)
9473 (org-agenda-time-leading-zero t)
9474 fmt time time2)
9475 (if org-agenda-insert-diary-extract-time
9476 ;; Use org-agenda-format-item to parse text for a time-range and
9477 ;; remove it. FIXME: This is a hack, we should refactor
9478 ;; that function to make time extraction available separately
9479 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9480 time (get-text-property 0 'time fmt)
9481 time2 (if (> (length time) 0)
9482 ;; split-string removes trailing ...... if
9483 ;; no end time given. First space
9484 ;; separates time from date.
9485 (concat " " (car (split-string time "\\.")))
9486 nil)
9487 text (get-text-property 0 'txt fmt)))
9488 (if (eq org-agenda-insert-diary-strategy 'top-level)
9489 (org-agenda-insert-diary-as-top-level text)
9490 (require 'org-datetree)
9491 (org-datetree-find-date-create d1)
9492 (org-agenda-insert-diary-make-new-entry text))
9493 (org-insert-time-stamp (org-time-from-absolute
9494 (calendar-absolute-from-gregorian d1))
9495 nil nil nil nil time2))
9496 (end-of-line 0))
9497 ((eq type 'block)
9498 (if (> (calendar-absolute-from-gregorian d1)
9499 (calendar-absolute-from-gregorian d2))
9500 (setq d1 (prog1 d2 (setq d2 d1))))
9501 (if (eq org-agenda-insert-diary-strategy 'top-level)
9502 (org-agenda-insert-diary-as-top-level text)
9503 (require 'org-datetree)
9504 (org-datetree-find-date-create d1)
9505 (org-agenda-insert-diary-make-new-entry text))
9506 (org-insert-time-stamp (org-time-from-absolute
9507 (calendar-absolute-from-gregorian d1)))
9508 (insert "--")
9509 (org-insert-time-stamp (org-time-from-absolute
9510 (calendar-absolute-from-gregorian d2)))
9511 (end-of-line 0)))
9512 (if (string-match "\\S-" text)
9513 (progn
9514 (set-window-configuration cw)
9515 (message "%s entry added to %s"
9516 (capitalize (symbol-name type))
9517 (abbreviate-file-name org-agenda-diary-file)))
9518 (org-reveal t)
9519 (message "Please finish entry here"))))
9521 (defun org-agenda-insert-diary-as-top-level (text)
9522 "Make new entry as a top-level entry at the end of the file.
9523 Add TEXT as headline, and position the cursor in the second line so that
9524 a timestamp can be added there."
9525 (widen)
9526 (goto-char (point-max))
9527 (unless (bolp) (insert "\n"))
9528 (org-insert-heading nil t t)
9529 (insert text)
9530 (org-end-of-meta-data)
9531 (unless (bolp) (insert "\n"))
9532 (when org-adapt-indentation (indent-to-column 2)))
9534 (defun org-agenda-insert-diary-make-new-entry (text)
9535 "Make a new entry with TEXT as a child of the current subtree.
9536 Position the point in the heading's first body line so that
9537 a timestamp can be added there."
9538 (cond
9539 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9540 (end-of-line)
9541 (org-insert-heading '(4) t)
9542 (org-do-demote))
9544 (outline-next-heading)
9545 (org-back-over-empty-lines)
9546 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9547 (org-insert-heading nil t)
9548 (org-do-demote)))
9549 (let ((col (current-column)))
9550 (insert text)
9551 (org-end-of-meta-data)
9552 ;; Ensure point is left on a blank line, at proper indentation.
9553 (unless (bolp) (insert "\n"))
9554 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9555 (when org-adapt-indentation (indent-to-column col)))
9556 (org-show-set-visibility 'lineage))
9558 (defun org-agenda-diary-entry ()
9559 "Make a diary entry, like the `i' command from the calendar.
9560 All the standard commands work: block, weekly etc.
9561 When `org-agenda-diary-file' points to a file,
9562 `org-agenda-diary-entry-in-org-file' is called instead to create
9563 entries in that Org-mode file."
9564 (interactive)
9565 (if (not (eq org-agenda-diary-file 'diary-file))
9566 (org-agenda-diary-entry-in-org-file)
9567 (require 'diary-lib)
9568 (let* ((char (progn
9569 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9570 (read-char-exclusive)))
9571 (cmd (cdr (assoc char
9572 '((?d . diary-insert-entry)
9573 (?w . diary-insert-weekly-entry)
9574 (?m . diary-insert-monthly-entry)
9575 (?y . diary-insert-yearly-entry)
9576 (?a . diary-insert-anniversary-entry)
9577 (?b . diary-insert-block-entry)
9578 (?c . diary-insert-cyclic-entry)))))
9579 (oldf (symbol-function 'calendar-cursor-to-date))
9580 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9581 (point (point))
9582 (mark (or (mark t) (point))))
9583 (unless cmd
9584 (user-error "No command associated with <%c>" char))
9585 (unless (and (get-text-property point 'day)
9586 (or (not (equal ?b char))
9587 (get-text-property mark 'day)))
9588 (user-error "Don't know which date to use for diary entry"))
9589 ;; We implement this by hacking the `calendar-cursor-to-date' function
9590 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9591 (let ((calendar-mark-ring
9592 (list (calendar-gregorian-from-absolute
9593 (or (get-text-property mark 'day)
9594 (get-text-property point 'day))))))
9595 (unwind-protect
9596 (progn
9597 (fset 'calendar-cursor-to-date
9598 (lambda (&optional error dummy)
9599 (calendar-gregorian-from-absolute
9600 (get-text-property point 'day))))
9601 (call-interactively cmd))
9602 (fset 'calendar-cursor-to-date oldf))))))
9604 (defun org-agenda-execute-calendar-command (cmd)
9605 "Execute a calendar command from the agenda with date from cursor."
9606 (org-agenda-check-type t 'agenda 'timeline)
9607 (require 'diary-lib)
9608 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9609 (user-error "Don't know which date to use for the calendar command"))
9610 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9611 (point (point))
9612 (date (calendar-gregorian-from-absolute
9613 (get-text-property point 'day)))
9614 ;; the following 2 vars are needed in the calendar
9615 (displayed-month (car date))
9616 (displayed-year (nth 2 date)))
9617 (unwind-protect
9618 (progn
9619 (fset 'calendar-cursor-to-date
9620 (lambda (&optional error dummy)
9621 (calendar-gregorian-from-absolute
9622 (get-text-property point 'day))))
9623 (call-interactively cmd))
9624 (fset 'calendar-cursor-to-date oldf))))
9626 (defun org-agenda-phases-of-moon ()
9627 "Display the phases of the moon for the 3 months around the cursor date."
9628 (interactive)
9629 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9631 (defun org-agenda-holidays ()
9632 "Display the holidays for the 3 months around the cursor date."
9633 (interactive)
9634 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9636 (defvar calendar-longitude) ; defined in calendar.el
9637 (defvar calendar-latitude) ; defined in calendar.el
9638 (defvar calendar-location-name) ; defined in calendar.el
9640 (defun org-agenda-sunrise-sunset (arg)
9641 "Display sunrise and sunset for the cursor date.
9642 Latitude and longitude can be specified with the variables
9643 `calendar-latitude' and `calendar-longitude'. When called with prefix
9644 argument, latitude and longitude will be prompted for."
9645 (interactive "P")
9646 (require 'solar)
9647 (let ((calendar-longitude (if arg nil calendar-longitude))
9648 (calendar-latitude (if arg nil calendar-latitude))
9649 (calendar-location-name
9650 (if arg "the given coordinates" calendar-location-name)))
9651 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9653 (defun org-agenda-goto-calendar ()
9654 "Open the Emacs calendar with the date at the cursor."
9655 (interactive)
9656 (org-agenda-check-type t 'agenda 'timeline)
9657 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9658 (user-error "Don't know which date to open in calendar")))
9659 (date (calendar-gregorian-from-absolute day))
9660 (calendar-move-hook nil)
9661 (calendar-view-holidays-initially-flag nil)
9662 (calendar-view-diary-initially-flag nil))
9663 (calendar)
9664 (calendar-goto-date date)))
9666 ;;;###autoload
9667 (defun org-calendar-goto-agenda ()
9668 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9669 This is a command that has to be installed in `calendar-mode-map'."
9670 (interactive)
9671 ;; Temporarily disable sticky agenda since user clearly wants to
9672 ;; refresh view anyway.
9673 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9674 (org-agenda-sticky nil))
9675 (org-agenda-list nil (calendar-absolute-from-gregorian
9676 (calendar-cursor-to-date))
9677 nil)))
9679 (defun org-agenda-convert-date ()
9680 (interactive)
9681 (org-agenda-check-type t 'agenda 'timeline)
9682 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9683 date s)
9684 (unless day
9685 (user-error "Don't know which date to convert"))
9686 (setq date (calendar-gregorian-from-absolute day))
9687 (setq s (concat
9688 "Gregorian: " (calendar-date-string date) "\n"
9689 "ISO: " (calendar-iso-date-string date) "\n"
9690 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9691 "Julian: " (calendar-julian-date-string date) "\n"
9692 "Astron. JD: " (calendar-astro-date-string date)
9693 " (Julian date number at noon UTC)\n"
9694 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9695 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9696 "French: " (calendar-french-date-string date) "\n"
9697 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9698 "Mayan: " (calendar-mayan-date-string date) "\n"
9699 "Coptic: " (calendar-coptic-date-string date) "\n"
9700 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9701 "Persian: " (calendar-persian-date-string date) "\n"
9702 "Chinese: " (calendar-chinese-date-string date) "\n"))
9703 (with-output-to-temp-buffer "*Dates*"
9704 (princ s))
9705 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9707 ;;; Bulk commands
9709 (defun org-agenda-bulk-marked-p ()
9710 "Non-nil when current entry is marked for bulk action."
9711 (eq (get-char-property (point-at-bol) 'type)
9712 'org-marked-entry-overlay))
9714 (defun org-agenda-bulk-mark (&optional arg)
9715 "Mark the entry at point for future bulk action."
9716 (interactive "p")
9717 (dotimes (i (or arg 1))
9718 (unless (org-get-at-bol 'org-agenda-diary-link)
9719 (let* ((m (org-get-at-bol 'org-hd-marker))
9721 (unless (org-agenda-bulk-marked-p)
9722 (unless m (user-error "Nothing to mark at point"))
9723 (push m org-agenda-bulk-marked-entries)
9724 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9725 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9726 (org-get-todo-face "TODO")
9727 'evaporate)
9728 (overlay-put ov 'type 'org-marked-entry-overlay))
9729 (end-of-line 1)
9730 (or (ignore-errors
9731 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9732 (beginning-of-line 2))
9733 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9734 (beginning-of-line 2))
9735 (message "%d entries marked for bulk action"
9736 (length org-agenda-bulk-marked-entries))))))
9738 (defun org-agenda-bulk-mark-all ()
9739 "Mark all entries for future agenda bulk action."
9740 (interactive)
9741 (org-agenda-bulk-mark-regexp "."))
9743 (defun org-agenda-bulk-mark-regexp (regexp)
9744 "Mark entries matching REGEXP for future agenda bulk action."
9745 (interactive "sMark entries matching regexp: ")
9746 (let ((entries-marked 0) txt-at-point)
9747 (save-excursion
9748 (goto-char (point-min))
9749 (goto-char (next-single-property-change (point) 'org-hd-marker))
9750 (while (and (re-search-forward regexp nil t)
9751 (setq txt-at-point (get-text-property (point) 'txt)))
9752 (when (string-match regexp txt-at-point)
9753 (setq entries-marked (1+ entries-marked))
9754 (call-interactively 'org-agenda-bulk-mark))))
9755 (if (not entries-marked)
9756 (message "No entry matching this regexp."))))
9758 (defun org-agenda-bulk-unmark (&optional arg)
9759 "Unmark the entry at point for future bulk action."
9760 (interactive "P")
9761 (if arg
9762 (org-agenda-bulk-unmark-all)
9763 (cond ((org-agenda-bulk-marked-p)
9764 (org-agenda-bulk-remove-overlays
9765 (point-at-bol) (+ 2 (point-at-bol)))
9766 (setq org-agenda-bulk-marked-entries
9767 (delete (org-get-at-bol 'org-hd-marker)
9768 org-agenda-bulk-marked-entries))
9769 (end-of-line 1)
9770 (or (ignore-errors
9771 (goto-char (next-single-property-change (point) 'txt)))
9772 (beginning-of-line 2))
9773 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9774 (beginning-of-line 2))
9775 (message "%d entries left marked for bulk action"
9776 (length org-agenda-bulk-marked-entries)))
9777 (t (message "No entry to unmark here")))))
9779 (defun org-agenda-bulk-toggle-all ()
9780 "Toggle all marks for bulk action."
9781 (interactive)
9782 (save-excursion
9783 (goto-char (point-min))
9784 (while (ignore-errors
9785 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9786 (org-agenda-bulk-toggle))))
9788 (defun org-agenda-bulk-toggle ()
9789 "Toggle the mark at point for bulk action."
9790 (interactive)
9791 (if (org-agenda-bulk-marked-p)
9792 (org-agenda-bulk-unmark)
9793 (org-agenda-bulk-mark)))
9795 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9796 "Remove the mark overlays between BEG and END in the agenda buffer.
9797 BEG and END default to the buffer limits.
9799 This only removes the overlays, it does not remove the markers
9800 from the list in `org-agenda-bulk-marked-entries'."
9801 (interactive)
9802 (mapc (lambda (ov)
9803 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9804 (delete-overlay ov)))
9805 (overlays-in (or beg (point-min)) (or end (point-max)))))
9807 (defun org-agenda-bulk-unmark-all ()
9808 "Remove all marks in the agenda buffer.
9809 This will remove the markers and the overlays."
9810 (interactive)
9811 (if (null org-agenda-bulk-marked-entries)
9812 (message "No entry to unmark")
9813 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9814 (setq org-agenda-bulk-marked-entries nil)
9815 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9817 (defcustom org-agenda-persistent-marks nil
9818 "Non-nil means marked items will stay marked after a bulk action.
9819 You can toggle this interactively by typing `p' when prompted for a
9820 bulk action."
9821 :group 'org-agenda
9822 :version "24.1"
9823 :type 'boolean)
9825 (defun org-agenda-bulk-action (&optional arg)
9826 "Execute an remote-editing action on all marked entries.
9827 The prefix arg is passed through to the command if possible."
9828 (interactive "P")
9829 ;; Make sure we have markers, and only valid ones
9830 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9831 (mapc
9832 (lambda (m)
9833 (unless (and (markerp m)
9834 (marker-buffer m)
9835 (buffer-live-p (marker-buffer m))
9836 (marker-position m))
9837 (user-error "Marker %s for bulk command is invalid" m)))
9838 org-agenda-bulk-marked-entries)
9840 ;; Prompt for the bulk command
9841 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9842 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9843 "[S]catter [f]unction "
9844 (when org-agenda-bulk-custom-functions
9845 (concat " Custom: ["
9846 (mapconcat (lambda(f) (char-to-string (car f)))
9847 org-agenda-bulk-custom-functions "")
9848 "]"))))
9849 (catch 'exit
9850 (let* ((action (read-char-exclusive))
9851 (org-log-refile (if org-log-refile 'time nil))
9852 (entries (reverse org-agenda-bulk-marked-entries))
9853 (org-overriding-default-time
9854 (if (get-text-property (point) 'org-agenda-date-header)
9855 (org-get-cursor-date)))
9856 redo-at-end
9857 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9858 (cond
9859 ((equal action ?p)
9860 (let ((org-agenda-persistent-marks
9861 (not org-agenda-persistent-marks)))
9862 (org-agenda-bulk-action)
9863 (throw 'exit nil)))
9865 ((equal action ?$)
9866 (setq cmd '(org-agenda-archive)))
9868 ((equal action ?A)
9869 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9871 ((member action '(?r ?w))
9872 (setq rfloc (org-refile-get-location
9873 "Refile to"
9874 (marker-buffer (car entries))
9875 org-refile-allow-creating-parent-nodes))
9876 (if (nth 3 rfloc)
9877 (setcar (nthcdr 3 rfloc)
9878 (move-marker (make-marker) (nth 3 rfloc)
9879 (or (get-file-buffer (nth 1 rfloc))
9880 (find-buffer-visiting (nth 1 rfloc))
9881 (error "This should not happen")))))
9883 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9884 redo-at-end t))
9886 ((equal action ?t)
9887 (setq state (completing-read
9888 "Todo state: "
9889 (with-current-buffer (marker-buffer (car entries))
9890 (mapcar #'list org-todo-keywords-1))))
9891 (setq cmd `(let ((org-inhibit-blocking t)
9892 (org-inhibit-logging 'note))
9893 (org-agenda-todo ,state))))
9895 ((memq action '(?- ?+))
9896 (setq tag (completing-read
9897 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9898 (with-current-buffer (marker-buffer (car entries))
9899 (delq nil
9900 (mapcar (lambda (x) (and (stringp (car x)) x))
9901 org-current-tag-alist)))))
9902 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9904 ((memq action '(?s ?d))
9905 (let* ((time
9906 (unless arg
9907 (org-read-date
9908 nil nil nil
9909 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9910 org-overriding-default-time)))
9911 (c1 (if (eq action ?s) 'org-agenda-schedule
9912 'org-agenda-deadline)))
9913 ;; Make sure to not prompt for a note when bulk
9914 ;; rescheduling as Org cannot cope with simultaneous Org.
9915 ;; Besides, it could be annoying depending on the number
9916 ;; of items re-scheduled.
9917 (setq cmd `(eval '(let ((org-log-reschedule
9918 (and org-log-reschedule 'time)))
9919 (,c1 arg ,time))))))
9921 ((equal action ?S)
9922 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9923 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9924 (let ((days (read-number
9925 (format "Scatter tasks across how many %sdays: "
9926 (if arg "week" "")) 7)))
9927 (setq cmd
9928 `(let ((distance (1+ (random ,days))))
9929 (if arg
9930 (let ((dist distance)
9931 (day-of-week
9932 (calendar-day-of-week
9933 (calendar-gregorian-from-absolute (org-today)))))
9934 (dotimes (i (1+ dist))
9935 (while (member day-of-week org-agenda-weekend-days)
9936 (incf distance)
9937 (incf day-of-week)
9938 (if (= day-of-week 7)
9939 (setq day-of-week 0)))
9940 (incf day-of-week)
9941 (if (= day-of-week 7)
9942 (setq day-of-week 0)))))
9943 ;; silently fail when try to replan a sexp entry
9944 (condition-case nil
9945 (let* ((date (calendar-gregorian-from-absolute
9946 (+ (org-today) distance)))
9947 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9948 (nth 2 date))))
9949 (org-agenda-schedule nil time))
9950 (error nil)))))))
9952 ((assoc action org-agenda-bulk-custom-functions)
9953 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9954 redo-at-end t))
9956 ((equal action ?f)
9957 (setq cmd (list (intern
9958 (completing-read "Function: "
9959 obarray 'fboundp t nil nil)))))
9961 (t (user-error "Invalid bulk action")))
9963 ;; Sort the markers, to make sure that parents are handled before children
9964 (setq entries (sort entries
9965 (lambda (a b)
9966 (cond
9967 ((equal (marker-buffer a) (marker-buffer b))
9968 (< (marker-position a) (marker-position b)))
9970 (string< (buffer-name (marker-buffer a))
9971 (buffer-name (marker-buffer b))))))))
9973 ;; Now loop over all markers and apply cmd
9974 (while (setq e (pop entries))
9975 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9976 (if (not pos)
9977 (progn (message "Skipping removed entry at %s" e)
9978 (setq cntskip (1+ cntskip)))
9979 (goto-char pos)
9980 (let (org-loop-over-headlines-in-active-region)
9981 (eval cmd))
9982 ;; `post-command-hook' is not run yet. We make sure any
9983 ;; pending log note is processed.
9984 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9985 (memq 'org-add-log-note post-command-hook))
9986 (org-add-log-note))
9987 (setq cnt (1+ cnt))))
9988 (when redo-at-end (org-agenda-redo))
9989 (unless org-agenda-persistent-marks
9990 (org-agenda-bulk-unmark-all))
9991 (message "Acted on %d entries%s%s"
9993 (if (= cntskip 0)
9995 (format ", skipped %d (disappeared before their turn)"
9996 cntskip))
9997 (if (not org-agenda-persistent-marks)
9998 "" " (kept marked)"))))))
10000 (defun org-agenda-capture (&optional with-time)
10001 "Call `org-capture' with the date at point.
10002 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10003 current HH:MM time."
10004 (interactive "P")
10005 (if (not (eq major-mode 'org-agenda-mode))
10006 (user-error "You cannot do this outside of agenda buffers")
10007 (let ((org-overriding-default-time
10008 (org-get-cursor-date (equal with-time 1))))
10009 (call-interactively 'org-capture))))
10011 ;;; Dragging agenda lines forward/backward
10013 (defun org-agenda-reapply-filters ()
10014 "Re-apply all agenda filters."
10015 (mapcar
10016 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10017 `((,org-agenda-tag-filter tag)
10018 (,org-agenda-category-filter category)
10019 (,org-agenda-regexp-filter regexp)
10020 (,org-agenda-effort-filter effort)
10021 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10022 (,(get 'org-agenda-category-filter :preset-filter) category)
10023 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10024 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10026 (defun org-agenda-drag-line-forward (arg &optional backward)
10027 "Drag an agenda line forward by ARG lines.
10028 When the optional argument `backward' is non-nil, move backward."
10029 (interactive "p")
10030 (let ((inhibit-read-only t) lst line)
10031 (if (or (not (get-text-property (point) 'txt))
10032 (save-excursion
10033 (dotimes (n arg)
10034 (move-beginning-of-line (if backward 0 2))
10035 (push (not (get-text-property (point) 'txt)) lst))
10036 (delq nil lst)))
10037 (message "Cannot move line forward")
10038 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10039 (move-beginning-of-line 1)
10040 (setq line (buffer-substring (point) end))
10041 (delete-region (point) end)
10042 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10043 (insert line)
10044 (org-agenda-reapply-filters)
10045 (org-agenda-mark-clocking-task)
10046 (move-beginning-of-line 0)))))
10048 (defun org-agenda-drag-line-backward (arg)
10049 "Drag an agenda line backward by ARG lines."
10050 (interactive "p")
10051 (org-agenda-drag-line-forward arg t))
10053 ;;; Flagging notes
10055 (defun org-agenda-show-the-flagging-note ()
10056 "Display the flagging note in the other window.
10057 When called a second time in direct sequence, offer to remove the FLAGGING
10058 tag and (if present) the flagging note."
10059 (interactive)
10060 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10061 (win (selected-window))
10062 note heading newhead)
10063 (unless hdmarker
10064 (user-error "No linked entry at point"))
10065 (if (and (eq this-command last-command)
10066 (y-or-n-p "Unflag and remove any flagging note? "))
10067 (progn
10068 (org-agenda-remove-flag hdmarker)
10069 (let ((win (get-buffer-window "*Flagging Note*")))
10070 (and win (delete-window win)))
10071 (message "Entry unflagged"))
10072 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10073 (unless note
10074 (user-error "No flagging note"))
10075 (org-kill-new note)
10076 (org-switch-to-buffer-other-window "*Flagging Note*")
10077 (erase-buffer)
10078 (insert note)
10079 (goto-char (point-min))
10080 (while (re-search-forward "\\\\n" nil t)
10081 (replace-match "\n" t t))
10082 (goto-char (point-min))
10083 (select-window win)
10084 (message "%s" (substitute-command-keys "Flagging note pushed to \
10085 kill ring. Press \\[org-agenda-show-the-flagging-note] again to remove \
10086 tag and note")))))
10088 (defun org-agenda-remove-flag (marker)
10089 "Remove the FLAGGED tag and any flagging note in the entry."
10090 (let (newhead)
10091 (org-with-point-at marker
10092 (org-toggle-tag "FLAGGED" 'off)
10093 (org-entry-delete nil "THEFLAGGINGNOTE")
10094 (setq newhead (org-get-heading)))
10095 (org-agenda-change-all-lines newhead marker)
10096 (message "Entry unflagged")))
10098 (defun org-agenda-get-any-marker (&optional pos)
10099 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10100 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10102 ;;; Appointment reminders
10104 (defvar appt-time-msg-list) ; defined in appt.el
10106 ;;;###autoload
10107 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10108 "Activate appointments found in `org-agenda-files'.
10109 With a \\[universal-argument] prefix, refresh the list of
10110 appointments.
10112 If FILTER is t, interactively prompt the user for a regular
10113 expression, and filter out entries that don't match it.
10115 If FILTER is a string, use this string as a regular expression
10116 for filtering entries out.
10118 If FILTER is a function, filter out entries against which
10119 calling the function returns nil. This function takes one
10120 argument: an entry from `org-agenda-get-day-entries'.
10122 FILTER can also be an alist with the car of each cell being
10123 either `headline' or `category'. For example:
10125 \\='((headline \"IMPORTANT\")
10126 (category \"Work\"))
10128 will only add headlines containing IMPORTANT or headlines
10129 belonging to the \"Work\" category.
10131 ARGS are symbols indicating what kind of entries to consider.
10132 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10133 \(i.e., deadlines and scheduled items with a hh:mm specification)
10134 and :timestamp entries. See the docstring of `org-diary' for
10135 details and examples.
10137 If an entry has a APPT_WARNTIME property, its value will be used
10138 to override `appt-message-warning-time'."
10139 (interactive "P")
10140 (if refresh (setq appt-time-msg-list nil))
10141 (if (eq filter t)
10142 (setq filter (read-from-minibuffer "Regexp filter: ")))
10143 (let* ((cnt 0) ; count added events
10144 (scope (or args '(:deadline* :scheduled* :timestamp)))
10145 (org-agenda-new-buffers nil)
10146 (org-deadline-warning-days 0)
10147 ;; Do not use `org-today' here because appt only takes
10148 ;; time and without date as argument, so it may pass wrong
10149 ;; information otherwise
10150 (today (org-date-to-gregorian
10151 (time-to-days (current-time))))
10152 (org-agenda-restrict nil)
10153 (files (org-agenda-files 'unrestricted)) entries file
10154 (org-agenda-buffer nil))
10155 ;; Get all entries which may contain an appt
10156 (org-agenda-prepare-buffers files)
10157 (while (setq file (pop files))
10158 (setq entries
10159 (delq nil
10160 (append entries
10161 (apply 'org-agenda-get-day-entries
10162 file today scope)))))
10163 ;; Map thru entries and find if we should filter them out
10164 (mapc
10165 (lambda (x)
10166 (let* ((evt (org-trim
10167 (replace-regexp-in-string
10168 org-bracket-link-regexp "\\3"
10169 (or (get-text-property 1 'txt x) ""))))
10170 (cat (get-text-property (1- (length x)) 'org-category x))
10171 (tod (get-text-property 1 'time-of-day x))
10172 (ok (or (null filter)
10173 (and (stringp filter) (string-match filter evt))
10174 (and (functionp filter) (funcall filter x))
10175 (and (listp filter)
10176 (let ((cat-filter (cadr (assoc 'category filter)))
10177 (evt-filter (cadr (assoc 'headline filter))))
10178 (or (and (stringp cat-filter)
10179 (string-match cat-filter cat))
10180 (and (stringp evt-filter)
10181 (string-match evt-filter evt)))))))
10182 (wrn (get-text-property 1 'warntime x)))
10183 ;; FIXME: Shall we remove text-properties for the appt text?
10184 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10185 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10186 (setq tod (concat "00" (number-to-string tod)))
10187 (setq tod (when (string-match
10188 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10189 (concat (match-string 1 tod) ":"
10190 (match-string 2 tod))))
10191 (when (if (version< emacs-version "23.3")
10192 (appt-add tod evt)
10193 (appt-add tod evt wrn))
10194 (setq cnt (1+ cnt))))))
10195 entries)
10196 (org-release-buffers org-agenda-new-buffers)
10197 (if (eq cnt 0)
10198 (message "No event to add")
10199 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10201 (defun org-agenda-today-p (date)
10202 "Non nil when DATE means today.
10203 DATE is either a list of the form (month day year) or a number of
10204 days as returned by `calendar-absolute-from-gregorian' or
10205 `org-today'. This function considers `org-extend-today-until'
10206 when defining today."
10207 (eq (org-today)
10208 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10209 (define-obsolete-function-alias 'org-agenda-todayp 'org-agenda-today-p "25.1")
10211 (defun org-agenda-todo-yesterday (&optional arg)
10212 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10213 (interactive "P")
10214 (let* ((org-use-effective-time t)
10215 (hour (third (decode-time
10216 (org-current-time))))
10217 (org-extend-today-until (1+ hour)))
10218 (org-agenda-todo arg)))
10220 (provide 'org-agenda)
10222 ;;; org-agenda.el ends here