org-agenda.el: Fix bug when displaying the changed date
[org-mode/org-kjn.git] / lisp / org-agenda.el
blob6bff6937b49fa56134563e6b4707c34af9ec60c9
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
86 (declare-function org-capture "org-capture" (&optional goto keys))
88 (defvar calendar-mode-map) ; defined in calendar.el
89 (defvar org-clock-current-task nil) ; defined in org-clock.el
90 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91 (defvar org-habit-show-habits) ; defined in org-habit.el
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
95 ;; Defined somewhere in this file, but used before definition.
96 (defvar org-agenda-buffer-name "*Org Agenda*")
97 (defvar org-agenda-overriding-header nil)
98 (defvar org-agenda-title-append nil)
99 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
101 (defvar original-date) ; dynamically scoped, calendar.el does scope this
103 (defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105 (defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
108 (defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110 When t, a confirmation is always needed. When a number N, confirmation is
111 only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
116 (integer :tag "When more than N lines")))
118 (defcustom org-agenda-compact-blocks nil
119 "Non-nil means make the block agenda more compact.
120 This is done globally by leaving out lines like the agenda span
121 name and week number or the separator lines."
122 :group 'org-agenda
123 :type 'boolean)
125 (defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127 If this is a string, it will be used as the separator, with a newline added.
128 If it is a character, it will be repeated to fill the window width.
129 If nil the separator is disabled. In `org-agenda-custom-commands' this
130 addresses the separator between the current and the previous block."
131 :group 'org-agenda
132 :type '(choice
133 (const :tag "Disabled" nil)
134 (character)
135 (string)))
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
147 (defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
149 This is a good place to set options for ps-print and for htmlize.
150 Note that the way this is implemented, the values will be evaluated
151 before assigned to the variables. So make sure to quote values you do
152 *not* want evaluated, for example
154 (setq org-agenda-exporter-settings
155 '((ps-print-color-p 'black-white)))"
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
162 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164 A useful function for this hook is `org-agenda-add-entry-text'."
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
169 (defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171 This is only relevant when `org-agenda-add-entry-text' is part of
172 `org-agenda-before-write-hook', which is the default.
173 When this is 0, nothing will happen. When it is greater than 0, it
174 specifies the maximum number of lines that will be added for each entry
175 that is listed in the agenda view.
177 Note that this variable is not used during display, only when exporting
178 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179 and `org-agenda-entry-text-maxlines'."
180 :group 'org-agenda
181 :type 'integer)
183 (defcustom org-agenda-add-entry-text-descriptive-links t
184 "Non-nil means export org-links as descriptive links in agenda added text.
185 This variable applies to the text added to the agenda when
186 `org-agenda-add-entry-text-maxlines' is larger than 0.
187 When this variable nil, the URL will (also) be shown."
188 :group 'org-agenda
189 :type 'boolean)
191 (defcustom org-agenda-export-html-style nil
192 "The style specification for exported HTML Agenda files.
193 If this variable contains a string, it will replace the default <style>
194 section as produced by `htmlize'.
195 Since there are different ways of setting style information, this variable
196 needs to contain the full HTML structure to provide a style, including the
197 surrounding HTML tags. The style specifications should include definitions
198 the fonts used by the agenda, here is an example:
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
211 .org-agenda-done {
212 color: #339933;
214 .org-done {
215 color: #339933;
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
222 or, if you want to keep the style in a file,
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
226 As the value of this option simply gets inserted into the HTML <head> header,
227 you can \"misuse\" it to also add other text to the header."
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type '(choice
231 (const nil)
232 (string)))
234 (defcustom org-agenda-persistent-filter nil
235 "When set, keep filters from one agenda view to the next."
236 :group 'org-agenda
237 :type 'boolean)
239 (defgroup org-agenda-custom-commands nil
240 "Options concerning agenda views in Org-mode."
241 :tag "Org Agenda Custom Commands"
242 :group 'org-agenda)
244 (defconst org-sorting-choice
245 '(choice
246 (const time-up) (const time-down)
247 (const timestamp-up) (const timestamp-down)
248 (const scheduled-up) (const scheduled-down)
249 (const deadline-up) (const deadline-down)
250 (const ts-up) (const ts-down)
251 (const tsia-up) (const tsia-down)
252 (const category-keep) (const category-up) (const category-down)
253 (const tag-down) (const tag-up)
254 (const priority-up) (const priority-down)
255 (const todo-state-up) (const todo-state-down)
256 (const effort-up) (const effort-down)
257 (const habit-up) (const habit-down)
258 (const alpha-up) (const alpha-down)
259 (const user-defined-up) (const user-defined-down))
260 "Sorting choices.")
262 ;; Keep custom values for `org-agenda-filter-preset' compatible with
263 ;; the new variable `org-agenda-tag-filter-preset'.
264 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
267 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
268 "List of types searched for when creating the daily/weekly agenda.
269 This variable is a list of symbols that controls the types of
270 items that appear in the daily/weekly agenda. Allowed symbols in this
271 list are are
273 :timestamp List items containing a date stamp or date range matching
274 the selected date. This includes sexp entries in angular
275 brackets.
277 :sexp List entries resulting from plain diary-like sexps.
279 :deadline List deadline due on that date. When the date is today,
280 also list any deadlines past due, or due within
281 `org-deadline-warning-days'. `:deadline' must appear before
282 `:scheduled' if the setting of
283 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
284 any effect.
286 :deadline* Same as above, but only include the deadline if it has an
287 hour specification as [h]h:mm.
289 :scheduled List all items which are scheduled for the given date.
290 The diary for *today* also contains items which were
291 scheduled earlier and are not yet marked DONE.
293 :scheduled* Same as above, but only include the scheduled item if it
294 has an hour specification as [h]h:mm.
296 By default, all four non-starred types are turned on.
298 When :scheduled* or :deadline* are included, :schedule or :deadline
299 will be ignored.
301 Never set this variable globally using `setq', because then it
302 will apply to all future agenda commands. Instead, bind it with
303 `let' to scope it dynamically into the agenda-constructing
304 command. A good way to set it is through options in
305 `org-agenda-custom-commands'. For a more flexible (though
306 somewhat less efficient) way of determining what is included in
307 the daily/weekly agenda, see `org-agenda-skip-function'.")
309 (defconst org-agenda-custom-commands-local-options
310 `(repeat :tag "Local settings for this command. Remember to quote values"
311 (choice :tag "Setting"
312 (list :tag "Heading for this block"
313 (const org-agenda-overriding-header)
314 (string :tag "Headline"))
315 (list :tag "Files to be searched"
316 (const org-agenda-files)
317 (list
318 (const :format "" quote)
319 (repeat (file))))
320 (list :tag "Sorting strategy"
321 (const org-agenda-sorting-strategy)
322 (list
323 (const :format "" quote)
324 (repeat
325 ,org-sorting-choice)))
326 (list :tag "Prefix format"
327 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
328 (string))
329 (list :tag "Number of days in agenda"
330 (const org-agenda-span)
331 (choice (const :tag "Day" day)
332 (const :tag "Week" week)
333 (const :tag "Fortnight" fortnight)
334 (const :tag "Month" month)
335 (const :tag "Year" year)
336 (integer :tag "Custom")))
337 (list :tag "Fixed starting date"
338 (const org-agenda-start-day)
339 (string :value "2007-11-01"))
340 (list :tag "Start on day of week"
341 (const org-agenda-start-on-weekday)
342 (choice :value 1
343 (const :tag "Today" nil)
344 (integer :tag "Weekday No.")))
345 (list :tag "Include data from diary"
346 (const org-agenda-include-diary)
347 (boolean))
348 (list :tag "Deadline Warning days"
349 (const org-deadline-warning-days)
350 (integer :value 1))
351 (list :tag "Category filter preset"
352 (const org-agenda-category-filter-preset)
353 (list
354 (const :format "" quote)
355 (repeat
356 (string :tag "+category or -category"))))
357 (list :tag "Tags filter preset"
358 (const org-agenda-tag-filter-preset)
359 (list
360 (const :format "" quote)
361 (repeat
362 (string :tag "+tag or -tag"))))
363 (list :tag "Regexp filter preset"
364 (const org-agenda-regexp-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+regexp or -regexp"))))
369 (list :tag "Set daily/weekly entry types"
370 (const org-agenda-entry-types)
371 (list
372 (const :format "" quote)
373 (set :greedy t :value ,org-agenda-entry-types
374 (const :deadline)
375 (const :scheduled)
376 (const :deadline*)
377 (const :scheduled*)
378 (const :timestamp)
379 (const :sexp))))
380 (list :tag "Standard skipping condition"
381 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
382 (const org-agenda-skip-function)
383 (list
384 (const :format "" quote)
385 (list
386 (choice
387 :tag "Skipping range"
388 (const :tag "Skip entry" org-agenda-skip-entry-if)
389 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
390 (repeat :inline t :tag "Conditions for skipping"
391 (choice
392 :tag "Condition type"
393 (list :tag "Regexp matches" :inline t (const :format "" regexp) (regexp))
394 (list :tag "Regexp does not match" :inline t (const :format "" notregexp) (regexp))
395 (list :tag "TODO state is" :inline t
396 (const todo)
397 (choice
398 (const :tag "any not-done state" todo)
399 (const :tag "any done state" done)
400 (const :tag "any state" any)
401 (list :tag "Keyword list"
402 (const :format "" quote)
403 (repeat (string :tag "Keyword")))))
404 (list :tag "TODO state is not" :inline t
405 (const nottodo)
406 (choice
407 (const :tag "any not-done state" todo)
408 (const :tag "any done state" done)
409 (const :tag "any state" any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (const :tag "scheduled" scheduled)
414 (const :tag "not scheduled" notscheduled)
415 (const :tag "deadline" deadline)
416 (const :tag "no deadline" notdeadline)
417 (const :tag "timestamp" timestamp)
418 (const :tag "no timestamp" nottimestamp))))))
419 (list :tag "Non-standard skipping condition"
420 :value (org-agenda-skip-function)
421 (const org-agenda-skip-function)
422 (sexp :tag "Function or form (quoted!)"))
423 (list :tag "Any variable"
424 (variable :tag "Variable")
425 (sexp :tag "Value (sexp)"))))
426 "Selection of examples for agenda command settings.
427 This will be spliced into the custom type of
428 `org-agenda-custom-commands'.")
431 (defcustom org-agenda-custom-commands
432 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
433 "Custom commands for the agenda.
434 These commands will be offered on the splash screen displayed by the
435 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
437 (key desc type match settings files)
439 key The key (one or more characters as a string) to be associated
440 with the command.
441 desc A description of the command, when omitted or nil, a default
442 description is built using MATCH.
443 type The command type, any of the following symbols:
444 agenda The daily/weekly agenda.
445 todo Entries with a specific TODO keyword, in all agenda files.
446 search Entries containing search words entry or headline.
447 tags Tags/Property/TODO match in all agenda files.
448 tags-todo Tags/P/T match in all agenda files, TODO entries only.
449 todo-tree Sparse tree of specific TODO keyword in *current* file.
450 tags-tree Sparse tree with all tags matches in *current* file.
451 occur-tree Occur sparse tree for *current* file.
452 ... A user-defined function.
453 match What to search for:
454 - a single keyword for TODO keyword searches
455 - a tags match expression for tags searches
456 - a word search expression for text searches.
457 - a regular expression for occur searches
458 For all other commands, this should be the empty string.
459 settings A list of option settings, similar to that in a let form, so like
460 this: ((opt1 val1) (opt2 val2) ...). The values will be
461 evaluated at the moment of execution, so quote them when needed.
462 files A list of files file to write the produced agenda buffer to
463 with the command `org-store-agenda-views'.
464 If a file name ends in \".html\", an HTML version of the buffer
465 is written out. If it ends in \".ps\", a postscript version is
466 produced. Otherwise, only the plain text is written to the file.
468 You can also define a set of commands, to create a composite agenda buffer.
469 In this case, an entry looks like this:
471 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
473 where
475 desc A description string to be displayed in the dispatcher menu.
476 cmd An agenda command, similar to the above. However, tree commands
477 are not allowed, but instead you can get agenda and global todo list.
478 So valid commands for a set are:
479 (agenda \"\" settings)
480 (alltodo \"\" settings)
481 (stuck \"\" settings)
482 (todo \"match\" settings files)
483 (search \"match\" settings files)
484 (tags \"match\" settings files)
485 (tags-todo \"match\" settings files)
487 Each command can carry a list of options, and another set of options can be
488 given for the whole set of commands. Individual command options take
489 precedence over the general options.
491 When using several characters as key to a command, the first characters
492 are prefix commands. For the dispatcher to display useful information, you
493 should provide a description for the prefix, like
495 (setq org-agenda-custom-commands
496 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
497 (\"hl\" tags \"+HOME+Lisa\")
498 (\"hp\" tags \"+HOME+Peter\")
499 (\"hk\" tags \"+HOME+Kim\")))"
500 :group 'org-agenda-custom-commands
501 :type `(repeat
502 (choice :value ("x" "Describe command here" tags "" nil)
503 (list :tag "Single command"
504 (string :tag "Access Key(s) ")
505 (option (string :tag "Description"))
506 (choice
507 (const :tag "Agenda" agenda)
508 (const :tag "TODO list" alltodo)
509 (const :tag "Search words" search)
510 (const :tag "Stuck projects" stuck)
511 (const :tag "Tags/Property match (all agenda files)" tags)
512 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
513 (const :tag "TODO keyword search (all agenda files)" todo)
514 (const :tag "Tags sparse tree (current buffer)" tags-tree)
515 (const :tag "TODO keyword tree (current buffer)" todo-tree)
516 (const :tag "Occur tree (current buffer)" occur-tree)
517 (sexp :tag "Other, user-defined function"))
518 (string :tag "Match (only for some commands)")
519 ,org-agenda-custom-commands-local-options
520 (option (repeat :tag "Export" (file :tag "Export to"))))
521 (list :tag "Command series, all agenda files"
522 (string :tag "Access Key(s)")
523 (string :tag "Description ")
524 (repeat :tag "Component"
525 (choice
526 (list :tag "Agenda"
527 (const :format "" agenda)
528 (const :tag "" :format "" "")
529 ,org-agenda-custom-commands-local-options)
530 (list :tag "TODO list (all keywords)"
531 (const :format "" alltodo)
532 (const :tag "" :format "" "")
533 ,org-agenda-custom-commands-local-options)
534 (list :tag "Search words"
535 (const :format "" search)
536 (string :tag "Match")
537 ,org-agenda-custom-commands-local-options)
538 (list :tag "Stuck projects"
539 (const :format "" stuck)
540 (const :tag "" :format "" "")
541 ,org-agenda-custom-commands-local-options)
542 (list :tag "Tags search"
543 (const :format "" tags)
544 (string :tag "Match")
545 ,org-agenda-custom-commands-local-options)
546 (list :tag "Tags search, TODO entries only"
547 (const :format "" tags-todo)
548 (string :tag "Match")
549 ,org-agenda-custom-commands-local-options)
550 (list :tag "TODO keyword search"
551 (const :format "" todo)
552 (string :tag "Match")
553 ,org-agenda-custom-commands-local-options)
554 (list :tag "Other, user-defined function"
555 (symbol :tag "function")
556 (string :tag "Match")
557 ,org-agenda-custom-commands-local-options)))
559 (repeat :tag "Settings for entire command set"
560 (list (variable :tag "Any variable")
561 (sexp :tag "Value")))
562 (option (repeat :tag "Export" (file :tag "Export to"))))
563 (cons :tag "Prefix key documentation"
564 (string :tag "Access Key(s)")
565 (string :tag "Description ")))))
567 (defcustom org-agenda-query-register ?o
568 "The register holding the current query string.
569 The purpose of this is that if you construct a query string interactively,
570 you can then use it to define a custom command."
571 :group 'org-agenda-custom-commands
572 :type 'character)
574 (defcustom org-stuck-projects
575 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
576 "How to identify stuck projects.
577 This is a list of four items:
578 1. A tags/todo/property matcher string that is used to identify a project.
579 See the manual for a description of tag and property searches.
580 The entire tree below a headline matched by this is considered one project.
581 2. A list of TODO keywords identifying non-stuck projects.
582 If the project subtree contains any headline with one of these todo
583 keywords, the project is considered to be not stuck. If you specify
584 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
585 3. A list of tags identifying non-stuck projects.
586 If the project subtree contains any headline with one of these tags,
587 the project is considered to be not stuck. If you specify \"*\" as
588 a tag, any tag will mark the project unstuck. Note that this is about
589 the explicit presence of a tag somewhere in the subtree, inherited
590 tags do not count here. If inherited tags make a project not stuck,
591 use \"-TAG\" in the tags part of the matcher under (1.) above.
592 4. An arbitrary regular expression matching non-stuck projects.
594 If the project turns out to be not stuck, search continues also in the
595 subtree to see if any of the subtasks have project status.
597 See also the variable `org-tags-match-list-sublevels' which applies
598 to projects matched by this search as well.
600 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
601 or `C-c a #' to produce the list."
602 :group 'org-agenda-custom-commands
603 :type '(list
604 (string :tag "Tags/TODO match to identify a project")
605 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
606 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
607 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
609 (defcustom org-agenda-filter-effort-default-operator "<"
610 "The default operator for effort estimate filtering.
611 If you select an effort estimate limit without first pressing an operator,
612 this one will be used."
613 :group 'org-agenda-custom-commands
614 :type '(choice (const :tag "less or equal" "<")
615 (const :tag "greater or equal"">")
616 (const :tag "equal" "=")))
618 (defgroup org-agenda-skip nil
619 "Options concerning skipping parts of agenda files."
620 :tag "Org Agenda Skip"
621 :group 'org-agenda)
623 (defcustom org-agenda-skip-function-global nil
624 "Function to be called at each match during agenda construction.
625 If this function returns nil, the current match should not be skipped.
626 If the function decided to skip an agenda match, is must return the
627 buffer position from which the search should be continued.
628 This may also be a Lisp form, which will be evaluated.
630 This variable will be applied to every agenda match, including
631 tags/property searches and TODO lists. So try to make the test function
632 do its checking as efficiently as possible. To implement a skipping
633 condition just for specific agenda commands, use the variable
634 `org-agenda-skip-function' which can be set in the options section
635 of custom agenda commands."
636 :group 'org-agenda-skip
637 :type 'sexp)
639 (defgroup org-agenda-daily/weekly nil
640 "Options concerning the daily/weekly agenda."
641 :tag "Org Agenda Daily/Weekly"
642 :group 'org-agenda)
643 (defgroup org-agenda-todo-list nil
644 "Options concerning the global todo list agenda view."
645 :tag "Org Agenda Todo List"
646 :group 'org-agenda)
647 (defgroup org-agenda-match-view nil
648 "Options concerning the general tags/property/todo match agenda view."
649 :tag "Org Agenda Match View"
650 :group 'org-agenda)
651 (defgroup org-agenda-search-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
656 (defvar org-agenda-archives-mode nil
657 "Non-nil means the agenda will include archived items.
658 If this is the symbol `trees', trees in the selected agenda scope
659 that are marked with the ARCHIVE tag will be included anyway. When this is
660 t, also all archive files associated with the current selection of agenda
661 files will be included.")
663 (defcustom org-agenda-restriction-lock-highlight-subtree t
664 "Non-nil means highlight the whole subtree when restriction is active.
665 Otherwise only highlight the headline. Highlighting the whole subtree is
666 useful to ensure no edits happen beyond the restricted region."
667 :group 'org-agenda
668 :type 'boolean)
670 (defcustom org-agenda-skip-comment-trees t
671 "Non-nil means skip trees that start with the COMMENT keyword.
672 When nil, these trees are also scanned by agenda commands."
673 :group 'org-agenda-skip
674 :type 'boolean)
676 (defcustom org-agenda-todo-list-sublevels t
677 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
678 When nil, the sublevels of a TODO entry are not checked, resulting in
679 potentially much shorter TODO lists."
680 :group 'org-agenda-skip
681 :group 'org-agenda-todo-list
682 :type 'boolean)
684 (defcustom org-agenda-todo-ignore-with-date nil
685 "Non-nil means don't show entries with a date in the global todo list.
686 You can use this if you prefer to mark mere appointments with a TODO keyword,
687 but don't want them to show up in the TODO list.
688 When this is set, it also covers deadlines and scheduled items, the settings
689 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
690 will be ignored.
691 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
692 :group 'org-agenda-skip
693 :group 'org-agenda-todo-list
694 :type 'boolean)
696 (defcustom org-agenda-todo-ignore-timestamp nil
697 "Non-nil means don't show entries with a timestamp.
698 This applies when creating the global todo list.
699 Valid values are:
701 past Don't show entries for today or in the past.
703 future Don't show entries with a timestamp in the future.
704 The idea behind this is that if it has a future
705 timestamp, you don't want to think about it until the
706 date.
708 all Don't show any entries with a timestamp in the global todo list.
709 The idea behind this is that by setting a timestamp, you
710 have already \"taken care\" of this item.
712 This variable can also have an integer as a value. If positive (N),
713 todos with a timestamp N or more days in the future will be ignored. If
714 negative (-N), todos with a timestamp N or more days in the past will be
715 ignored. If 0, todos with a timestamp either today or in the future will
716 be ignored. For example, a value of -1 will exclude todos with a
717 timestamp in the past (yesterday or earlier), while a value of 7 will
718 exclude todos with a timestamp a week or more in the future.
720 See also `org-agenda-todo-ignore-with-date'.
721 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
722 to make his option also apply to the tags-todo list."
723 :group 'org-agenda-skip
724 :group 'org-agenda-todo-list
725 :version "24.1"
726 :type '(choice
727 (const :tag "Ignore future timestamp todos" future)
728 (const :tag "Ignore past or present timestamp todos" past)
729 (const :tag "Ignore all timestamp todos" all)
730 (const :tag "Show timestamp todos" nil)
731 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
733 (defcustom org-agenda-todo-ignore-scheduled nil
734 "Non-nil means, ignore some scheduled TODO items when making TODO list.
735 This applies when creating the global todo list.
736 Valid values are:
738 past Don't show entries scheduled today or in the past.
740 future Don't show entries scheduled in the future.
741 The idea behind this is that by scheduling it, you don't want to
742 think about it until the scheduled date.
744 all Don't show any scheduled entries in the global todo list.
745 The idea behind this is that by scheduling it, you have already
746 \"taken care\" of this item.
748 t Same as `all', for backward compatibility.
750 This variable can also have an integer as a value. See
751 `org-agenda-todo-ignore-timestamp' for more details.
753 See also `org-agenda-todo-ignore-with-date'.
754 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
755 to make his option also apply to the tags-todo list."
756 :group 'org-agenda-skip
757 :group 'org-agenda-todo-list
758 :type '(choice
759 (const :tag "Ignore future-scheduled todos" future)
760 (const :tag "Ignore past- or present-scheduled todos" past)
761 (const :tag "Ignore all scheduled todos" all)
762 (const :tag "Ignore all scheduled todos (compatibility)" t)
763 (const :tag "Show scheduled todos" nil)
764 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
766 (defcustom org-agenda-todo-ignore-deadlines nil
767 "Non-nil means ignore some deadline TODO items when making TODO list.
768 There are different motivations for using different values, please think
769 carefully when configuring this variable.
771 This applies when creating the global todo list.
772 Valid values are:
774 near Don't show near deadline entries. A deadline is near when it is
775 closer than `org-deadline-warning-days' days. The idea behind this
776 is that such items will appear in the agenda anyway.
778 far Don't show TODO entries where a deadline has been defined, but
779 the deadline is not near. This is useful if you don't want to
780 use the todo list to figure out what to do now.
782 past Don't show entries with a deadline timestamp for today or in the past.
784 future Don't show entries with a deadline timestamp in the future, not even
785 when they become `near' ones. Use it with caution.
787 all Ignore all TODO entries that do have a deadline.
789 t Same as `near', for backward compatibility.
791 This variable can also have an integer as a value. See
792 `org-agenda-todo-ignore-timestamp' for more details.
794 See also `org-agenda-todo-ignore-with-date'.
795 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
796 to make his option also apply to the tags-todo list."
797 :group 'org-agenda-skip
798 :group 'org-agenda-todo-list
799 :type '(choice
800 (const :tag "Ignore near deadlines" near)
801 (const :tag "Ignore near deadlines (compatibility)" t)
802 (const :tag "Ignore far deadlines" far)
803 (const :tag "Ignore all TODOs with a deadlines" all)
804 (const :tag "Show all TODOs, even if they have a deadline" nil)
805 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
807 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
808 "Time unit to use when possibly ignoring an agenda item.
810 See the docstring of various `org-agenda-todo-ignore-*' options.
811 The default is to compare time stamps using days. An item is thus
812 considered to be in the future if it is at least one day after today.
813 Non-nil means to compare time stamps using seconds. An item is then
814 considered future if it has a time value later than current time."
815 :group 'org-agenda-skip
816 :group 'org-agenda-todo-list
817 :version "24.4"
818 :package-version '(Org . "8.0")
819 :type '(choice
820 (const :tag "Compare time with days" nil)
821 (const :tag "Compare time with seconds" t)))
823 (defcustom org-agenda-tags-todo-honor-ignore-options nil
824 "Non-nil means honor todo-list ignores options also in tags-todo search.
825 The variables
826 `org-agenda-todo-ignore-with-date',
827 `org-agenda-todo-ignore-timestamp',
828 `org-agenda-todo-ignore-scheduled',
829 `org-agenda-todo-ignore-deadlines'
830 make the global TODO list skip entries that have time stamps of certain
831 kinds. If this option is set, the same options will also apply for the
832 tags-todo search, which is the general tags/property matcher
833 restricted to unfinished TODO entries only."
834 :group 'org-agenda-skip
835 :group 'org-agenda-todo-list
836 :group 'org-agenda-match-view
837 :type 'boolean)
839 (defcustom org-agenda-skip-scheduled-if-done nil
840 "Non-nil means don't show scheduled items in agenda when they are done.
841 This is relevant for the daily/weekly agenda, not for the TODO list. And
842 it applies only to the actual date of the scheduling. Warnings about
843 an item with a past scheduling dates are always turned off when the item
844 is DONE."
845 :group 'org-agenda-skip
846 :group 'org-agenda-daily/weekly
847 :type 'boolean)
849 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
850 "Non-nil means skip scheduling line if same entry shows because of deadline.
852 In the agenda of today, an entry can show up multiple times
853 because it is both scheduled and has a nearby deadline, and maybe
854 a plain time stamp as well.
856 When this variable is nil, the entry will be shown several times.
858 When set to t, then only the deadline is shown and the fact that
859 the entry is scheduled today or was scheduled previously is not
860 shown.
862 When set to the symbol `not-today', skip scheduled previously,
863 but not scheduled today.
865 When set to the symbol `repeated-after-deadline', skip scheduled
866 items if they are repeated beyond the current deadline."
867 :group 'org-agenda-skip
868 :group 'org-agenda-daily/weekly
869 :type '(choice
870 (const :tag "Never" nil)
871 (const :tag "Always" t)
872 (const :tag "Not when scheduled today" not-today)
873 (const :tag "When repeated past deadline" repeated-after-deadline)))
875 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
876 "Non-nil means skip timestamp line if same entry shows because of deadline.
877 In the agenda of today, an entry can show up multiple times
878 because it has both a plain timestamp and has a nearby deadline.
879 When this variable is t, then only the deadline is shown and the
880 fact that the entry has a timestamp for or including today is not
881 shown. When this variable is nil, the entry will be shown
882 several times."
883 :group 'org-agenda-skip
884 :group 'org-agenda-daily/weekly
885 :version "24.1"
886 :type '(choice
887 (const :tag "Never" nil)
888 (const :tag "Always" t)))
890 (defcustom org-agenda-skip-deadline-if-done nil
891 "Non-nil means don't show deadlines when the corresponding item is done.
892 When nil, the deadline is still shown and should give you a happy feeling.
893 This is relevant for the daily/weekly agenda. And it applied only to the
894 actually date of the deadline. Warnings about approaching and past-due
895 deadlines are always turned off when the item is DONE."
896 :group 'org-agenda-skip
897 :group 'org-agenda-daily/weekly
898 :type 'boolean)
900 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
901 "Non-nil means skip deadline prewarning when entry is also scheduled.
902 This will apply on all days where a prewarning for the deadline would
903 be shown, but not at the day when the entry is actually due. On that day,
904 the deadline will be shown anyway.
905 This variable may be set to nil, t, the symbol `pre-scheduled',
906 or a number which will then give the number of days before the actual
907 deadline when the prewarnings should resume. The symbol `pre-scheduled'
908 eliminates the deadline prewarning only prior to the scheduled date.
909 This can be used in a workflow where the first showing of the deadline will
910 trigger you to schedule it, and then you don't want to be reminded of it
911 because you will take care of it on the day when scheduled."
912 :group 'org-agenda-skip
913 :group 'org-agenda-daily/weekly
914 :version "24.1"
915 :type '(choice
916 (const :tag "Always show prewarning" nil)
917 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
918 (const :tag "Remove prewarning if entry is scheduled" t)
919 (integer :tag "Restart prewarning N days before deadline")))
921 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
922 "Non-nil means skip scheduled delay when entry also has a deadline.
923 This variable may be set to nil, t, the symbol `post-deadline',
924 or a number which will then give the number of days after the actual
925 scheduled date when the delay should expire. The symbol `post-deadline'
926 eliminates the schedule delay when the date is posterior to the deadline."
927 :group 'org-agenda-skip
928 :group 'org-agenda-daily/weekly
929 :version "24.4"
930 :package-version '(Org . "8.0")
931 :type '(choice
932 (const :tag "Always honor delay" nil)
933 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
934 (const :tag "Ignore delay if entry has a deadline" t)
935 (integer :tag "Honor delay up until N days after the scheduled date")))
937 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
938 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
939 When non-nil, after the search for timestamps has matched once in an
940 entry, the rest of the entry will not be searched."
941 :group 'org-agenda-skip
942 :type 'boolean)
944 (defcustom org-agenda-skip-timestamp-if-done nil
945 "Non-nil means don't select item by timestamp or -range if it is DONE."
946 :group 'org-agenda-skip
947 :group 'org-agenda-daily/weekly
948 :type 'boolean)
950 (defcustom org-agenda-dim-blocked-tasks t
951 "Non-nil means dim blocked tasks in the agenda display.
952 This causes some overhead during agenda construction, but if you
953 have turned on `org-enforce-todo-dependencies',
954 `org-enforce-todo-checkbox-dependencies', or any other blocking
955 mechanism, this will create useful feedback in the agenda.
957 Instead of t, this variable can also have the value `invisible'.
958 Then blocked tasks will be invisible and only become visible when
959 they become unblocked. An exemption to this behavior is when a task is
960 blocked because of unchecked checkboxes below it. Since checkboxes do
961 not show up in the agenda views, making this task invisible you remove any
962 trace from agenda views that there is something to do. Therefore, a task
963 that is blocked because of checkboxes will never be made invisible, it
964 will only be dimmed."
965 :group 'org-agenda-daily/weekly
966 :group 'org-agenda-todo-list
967 :version "24.3"
968 :type '(choice
969 (const :tag "Do not dim" nil)
970 (const :tag "Dim to a gray face" t)
971 (const :tag "Make invisible" invisible)))
973 (defcustom org-timeline-show-empty-dates 3
974 "Non-nil means `org-timeline' also shows dates without an entry.
975 When nil, only the days which actually have entries are shown.
976 When t, all days between the first and the last date are shown.
977 When an integer, show also empty dates, but if there is a gap of more than
978 N days, just insert a special line indicating the size of the gap."
979 :group 'org-agenda-skip
980 :type '(choice
981 (const :tag "None" nil)
982 (const :tag "All" t)
983 (integer :tag "at most")))
985 (defgroup org-agenda-startup nil
986 "Options concerning initial settings in the Agenda in Org Mode."
987 :tag "Org Agenda Startup"
988 :group 'org-agenda)
990 (defcustom org-agenda-menu-show-matcher t
991 "Non-nil means show the match string in the agenda dispatcher menu.
992 When nil, the matcher string is not shown, but is put into the help-echo
993 property so than moving the mouse over the command shows it.
994 Setting it to nil is good if matcher strings are very long and/or if
995 you want to use two-columns display (see `org-agenda-menu-two-columns')."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1002 (defcustom org-agenda-menu-two-columns nil
1003 "Non-nil means, use two columns to show custom commands in the dispatcher.
1004 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1005 to nil."
1006 :group 'org-agenda
1007 :version "24.1"
1008 :type 'boolean)
1010 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1011 (defcustom org-agenda-finalize-hook nil
1012 "Hook run just before displaying an agenda buffer.
1013 The buffer is still writable when the hook is called.
1015 You can modify some of the buffer substrings but you should be
1016 extra careful not to modify the text properties of the agenda
1017 headlines as the agenda display heavily relies on them."
1018 :group 'org-agenda-startup
1019 :type 'hook)
1021 (defcustom org-agenda-mouse-1-follows-link nil
1022 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1023 A longer mouse click will still set point. Does not work on XEmacs.
1024 Needs to be set before org.el is loaded."
1025 :group 'org-agenda-startup
1026 :type 'boolean)
1028 (defcustom org-agenda-start-with-follow-mode nil
1029 "The initial value of follow mode in a newly created agenda window."
1030 :group 'org-agenda-startup
1031 :type 'boolean)
1033 (defcustom org-agenda-follow-indirect nil
1034 "Non-nil means `org-agenda-follow-mode' displays only the
1035 current item's tree, in an indirect buffer."
1036 :group 'org-agenda
1037 :version "24.1"
1038 :type 'boolean)
1040 (defcustom org-agenda-show-outline-path t
1041 "Non-nil means show outline path in echo area after line motion."
1042 :group 'org-agenda-startup
1043 :type 'boolean)
1045 (defcustom org-agenda-start-with-entry-text-mode nil
1046 "The initial value of entry-text-mode in a newly created agenda window."
1047 :group 'org-agenda-startup
1048 :type 'boolean)
1050 (defcustom org-agenda-entry-text-maxlines 5
1051 "Number of text lines to be added when `E' is pressed in the agenda.
1053 Note that this variable only used during agenda display. Add add entry text
1054 when exporting the agenda, configure the variable
1055 `org-agenda-add-entry-ext-maxlines'."
1056 :group 'org-agenda
1057 :type 'integer)
1059 (defcustom org-agenda-entry-text-exclude-regexps nil
1060 "List of regular expressions to clean up entry text.
1061 The complete matches of all regular expressions in this list will be
1062 removed from entry text before it is shown in the agenda."
1063 :group 'org-agenda
1064 :type '(repeat (regexp)))
1066 (defcustom org-agenda-entry-text-leaders " > "
1067 "Text prepended to the entry text in agenda buffers."
1068 :version "24.4"
1069 :package-version '(Org . "8.0")
1070 :group 'org-agenda
1071 :type 'string)
1073 (defvar org-agenda-entry-text-cleanup-hook nil
1074 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1075 This cleanup is done in a temporary buffer, so the function may inspect and
1076 change the entire buffer.
1077 Some default stuff like drawers and scheduling/deadline dates will already
1078 have been removed when this is called, as will any matches for regular
1079 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1081 (defvar org-agenda-include-inactive-timestamps nil
1082 "Non-nil means include inactive time stamps in agenda and timeline.
1083 Dynamically scoped.")
1085 (defgroup org-agenda-windows nil
1086 "Options concerning the windows used by the Agenda in Org Mode."
1087 :tag "Org Agenda Windows"
1088 :group 'org-agenda)
1090 (defcustom org-agenda-window-setup 'reorganize-frame
1091 "How the agenda buffer should be displayed.
1092 Possible values for this option are:
1094 current-window Show agenda in the current window, keeping all other windows.
1095 other-window Use `switch-to-buffer-other-window' to display agenda.
1096 reorganize-frame Show only two windows on the current frame, the current
1097 window and the agenda.
1098 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1099 Also, when exiting the agenda, kill that frame.
1100 See also the variable `org-agenda-restore-windows-after-quit'."
1101 :group 'org-agenda-windows
1102 :type '(choice
1103 (const current-window)
1104 (const other-frame)
1105 (const other-window)
1106 (const reorganize-frame)))
1108 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1109 "The min and max height of the agenda window as a fraction of frame height.
1110 The value of the variable is a cons cell with two numbers between 0 and 1.
1111 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1112 :group 'org-agenda-windows
1113 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1115 (defcustom org-agenda-restore-windows-after-quit nil
1116 "Non-nil means restore window configuration upon exiting agenda.
1117 Before the window configuration is changed for displaying the agenda,
1118 the current status is recorded. When the agenda is exited with
1119 `q' or `x' and this option is set, the old state is restored. If
1120 `org-agenda-window-setup' is `other-frame', the value of this
1121 option will be ignored."
1122 :group 'org-agenda-windows
1123 :type 'boolean)
1125 (defcustom org-agenda-ndays nil
1126 "Number of days to include in overview display.
1127 Should be 1 or 7.
1128 Obsolete, see `org-agenda-span'."
1129 :group 'org-agenda-daily/weekly
1130 :type '(choice (const nil)
1131 (integer)))
1133 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1135 (defcustom org-agenda-span 'week
1136 "Number of days to include in overview display.
1137 Can be day, week, month, year, or any number of days.
1138 Custom commands can set this variable in the options section."
1139 :group 'org-agenda-daily/weekly
1140 :type '(choice (const :tag "Day" day)
1141 (const :tag "Week" week)
1142 (const :tag "Fortnight" fortnight)
1143 (const :tag "Month" month)
1144 (const :tag "Year" year)
1145 (integer :tag "Custom")))
1147 (defcustom org-agenda-start-on-weekday 1
1148 "Non-nil means start the overview always on the specified weekday.
1149 0 denotes Sunday, 1 denotes Monday, etc.
1150 When nil, always start on the current day.
1151 Custom commands can set this variable in the options section."
1152 :group 'org-agenda-daily/weekly
1153 :type '(choice (const :tag "Today" nil)
1154 (integer :tag "Weekday No.")))
1156 (defcustom org-agenda-show-all-dates t
1157 "Non-nil means `org-agenda' shows every day in the selected range.
1158 When nil, only the days which actually have entries are shown."
1159 :group 'org-agenda-daily/weekly
1160 :type 'boolean)
1162 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1163 "Format string for displaying dates in the agenda.
1164 Used by the daily/weekly agenda and by the timeline. This should be
1165 a format string understood by `format-time-string', or a function returning
1166 the formatted date as a string. The function must take a single argument,
1167 a calendar-style date list like (month day year)."
1168 :group 'org-agenda-daily/weekly
1169 :type '(choice
1170 (string :tag "Format string")
1171 (function :tag "Function")))
1173 (defun org-agenda-format-date-aligned (date)
1174 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1175 This function makes sure that dates are aligned for easy reading."
1176 (require 'cal-iso)
1177 (let* ((dayname (calendar-day-name date))
1178 (day (cadr date))
1179 (day-of-week (calendar-day-of-week date))
1180 (month (car date))
1181 (monthname (calendar-month-name month))
1182 (year (nth 2 date))
1183 (iso-week (org-days-to-iso-week
1184 (calendar-absolute-from-gregorian date)))
1185 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1186 (1- year))
1187 ((and (= month 12) (<= iso-week 1))
1188 (1+ year))
1189 (t year)))
1190 (weekstring (if (= day-of-week 1)
1191 (format " W%02d" iso-week)
1192 "")))
1193 (format "%-10s %2d %s %4d%s"
1194 dayname day monthname year weekstring)))
1196 (defcustom org-agenda-time-leading-zero nil
1197 "Non-nil means use leading zero for military times in agenda.
1198 For example, 9:30am would become 09:30 rather than 9:30."
1199 :group 'org-agenda-daily/weekly
1200 :version "24.1"
1201 :type 'boolean)
1203 (defcustom org-agenda-timegrid-use-ampm nil
1204 "When set, show AM/PM style timestamps on the timegrid."
1205 :group 'org-agenda
1206 :version "24.1"
1207 :type 'boolean)
1209 (defun org-agenda-time-of-day-to-ampm (time)
1210 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1211 (let* ((hour-number (string-to-number (substring time 0 -3)))
1212 (minute (substring time -2))
1213 (ampm "am"))
1214 (cond
1215 ((equal hour-number 12)
1216 (setq ampm "pm"))
1217 ((> hour-number 12)
1218 (setq ampm "pm")
1219 (setq hour-number (- hour-number 12))))
1220 (concat
1221 (if org-agenda-time-leading-zero
1222 (format "%02d" hour-number)
1223 (format "%02s" (number-to-string hour-number)))
1224 ":" minute ampm)))
1226 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1227 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1228 (if org-agenda-timegrid-use-ampm
1229 (org-agenda-time-of-day-to-ampm time)
1230 time))
1232 (defcustom org-agenda-weekend-days '(6 0)
1233 "Which days are weekend?
1234 These days get the special face `org-agenda-date-weekend' in the agenda
1235 and timeline buffers."
1236 :group 'org-agenda-daily/weekly
1237 :type '(set :greedy t
1238 (const :tag "Monday" 1)
1239 (const :tag "Tuesday" 2)
1240 (const :tag "Wednesday" 3)
1241 (const :tag "Thursday" 4)
1242 (const :tag "Friday" 5)
1243 (const :tag "Saturday" 6)
1244 (const :tag "Sunday" 0)))
1246 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1247 "Non-nil means jump to today when moving a past date forward in time.
1248 When using S-right in the agenda to move a a date forward, and the date
1249 stamp currently points to the past, the first key press will move it
1250 to today. WHen nil, just move one day forward even if the date stays
1251 in the past."
1252 :group 'org-agenda-daily/weekly
1253 :version "24.1"
1254 :type 'boolean)
1256 (defcustom org-agenda-include-diary nil
1257 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1258 Custom commands can set this variable in the options section."
1259 :group 'org-agenda-daily/weekly
1260 :type 'boolean)
1262 (defcustom org-agenda-include-deadlines t
1263 "If non-nil, include entries within their deadline warning period.
1264 Custom commands can set this variable in the options section."
1265 :group 'org-agenda-daily/weekly
1266 :version "24.1"
1267 :type 'boolean)
1269 (defcustom org-agenda-repeating-timestamp-show-all t
1270 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1271 When set to a list of strings, only show occurrences of repeating
1272 stamps for these TODO keywords. When nil, only one occurrence is
1273 shown, either today or the nearest into the future."
1274 :group 'org-agenda-daily/weekly
1275 :type '(choice
1276 (const :tag "Show repeating stamps" t)
1277 (repeat :tag "Show repeating stamps for these TODO keywords"
1278 (string :tag "TODO Keyword"))
1279 (const :tag "Don't show repeating stamps" nil)))
1281 (defcustom org-scheduled-past-days 10000
1282 "Number of days to continue listing scheduled items not marked DONE.
1283 When an item is scheduled on a date, it shows up in the agenda on this
1284 day and will be listed until it is marked done for the number of days
1285 given here."
1286 :group 'org-agenda-daily/weekly
1287 :type 'integer)
1289 (defcustom org-agenda-log-mode-items '(closed clock)
1290 "List of items that should be shown in agenda log mode.
1291 This list may contain the following symbols:
1293 closed Show entries that have been closed on that day.
1294 clock Show entries that have received clocked time on that day.
1295 state Show all logged state changes.
1296 Note that instead of changing this variable, you can also press `C-u l' in
1297 the agenda to display all available LOG items temporarily."
1298 :group 'org-agenda-daily/weekly
1299 :type '(set :greedy t (const closed) (const clock) (const state)))
1301 (defcustom org-agenda-clock-consistency-checks
1302 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1303 :gap-ok-around ("4:00")
1304 :default-face ((:background "DarkRed") (:foreground "white"))
1305 :overlap-face nil :gap-face nil :no-end-time-face nil
1306 :long-face nil :short-face nil)
1307 "This is a property list, with the following keys:
1309 :max-duration Mark clocking chunks that are longer than this time.
1310 This is a time string like \"HH:MM\", or the number
1311 of minutes as an integer.
1313 :min-duration Mark clocking chunks that are shorter that this.
1314 This is a time string like \"HH:MM\", or the number
1315 of minutes as an integer.
1317 :max-gap Mark gaps between clocking chunks that are longer than
1318 this duration. A number of minutes, or a string
1319 like \"HH:MM\".
1321 :gap-ok-around List of times during the day which are usually not working
1322 times. When a gap is detected, but the gap contains any
1323 of these times, the gap is *not* reported. For example,
1324 if this is (\"4:00\" \"13:00\") then gaps that contain
1325 4:00 in the morning (i.e. the night) and 13:00
1326 (i.e. a typical lunch time) do not cause a warning.
1327 You should have at least one time during the night in this
1328 list, or otherwise the first task each morning will trigger
1329 a warning because it follows a long gap.
1331 Furthermore, the following properties can be used to define faces for
1332 issue display.
1334 :default-face the default face, if the specific face is undefined
1335 :overlap-face face for overlapping clocks
1336 :gap-face face for gaps between clocks
1337 :no-end-time-face face for incomplete clocks
1338 :long-face face for clock intervals that are too long
1339 :short-face face for clock intervals that are too short"
1340 :group 'org-agenda-daily/weekly
1341 :group 'org-clock
1342 :version "24.1"
1343 :type 'plist)
1345 (defcustom org-agenda-log-mode-add-notes t
1346 "Non-nil means add first line of notes to log entries in agenda views.
1347 If a log item like a state change or a clock entry is associated with
1348 notes, the first line of these notes will be added to the entry in the
1349 agenda display."
1350 :group 'org-agenda-daily/weekly
1351 :type 'boolean)
1353 (defcustom org-agenda-start-with-log-mode nil
1354 "The initial value of log-mode in a newly created agenda window.
1355 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1356 explanations on the possible values."
1357 :group 'org-agenda-startup
1358 :group 'org-agenda-daily/weekly
1359 :type '(choice (const :tag "Don't show log items" nil)
1360 (const :tag "Show only log items" only)
1361 (const :tag "Show all possible log items" clockcheck)
1362 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1363 (choice (const :tag "Show closed log items" closed)
1364 (const :tag "Show clocked log items" clock)
1365 (const :tag "Show all logged state changes" state)))))
1367 (defcustom org-agenda-start-with-clockreport-mode nil
1368 "The initial value of clockreport-mode in a newly created agenda window."
1369 :group 'org-agenda-startup
1370 :group 'org-agenda-daily/weekly
1371 :type 'boolean)
1373 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1374 "Property list with parameters for the clocktable in clockreport mode.
1375 This is the display mode that shows a clock table in the daily/weekly
1376 agenda, the properties for this dynamic block can be set here.
1377 The usual clocktable parameters are allowed here, but you cannot set
1378 the properties :name, :tstart, :tend, :block, and :scope - these will
1379 be overwritten to make sure the content accurately reflects the
1380 current display in the agenda."
1381 :group 'org-agenda-daily/weekly
1382 :type 'plist)
1384 (defcustom org-agenda-search-view-always-boolean nil
1385 "Non-nil means the search string is interpreted as individual parts.
1387 The search string for search view can either be interpreted as a phrase,
1388 or as a list of snippets that define a boolean search for a number of
1389 strings.
1391 When this is non-nil, the string will be split on whitespace, and each
1392 snippet will be searched individually, and all must match in order to
1393 select an entry. A snippet is then a single string of non-white
1394 characters, or a string in double quotes, or a regexp in {} braces.
1395 If a snippet is preceded by \"-\", the snippet must *not* match.
1396 \"+\" is syntactic sugar for positive selection. Each snippet may
1397 be found as a full word or a partial word, but see the variable
1398 `org-agenda-search-view-force-full-words'.
1400 When this is nil, search will look for the entire search phrase as one,
1401 with each space character matching any amount of whitespace, including
1402 line breaks.
1404 Even when this is nil, you can still switch to Boolean search dynamically
1405 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1406 is a regexp marked with braces like \"{abc}\", this will also switch to
1407 boolean search."
1408 :group 'org-agenda-search-view
1409 :version "24.1"
1410 :type 'boolean)
1412 (org-defvaralias 'org-agenda-search-view-search-words-only
1413 'org-agenda-search-view-always-boolean)
1415 (defcustom org-agenda-search-view-force-full-words nil
1416 "Non-nil means, search words must be matches as complete words.
1417 When nil, they may also match part of a word."
1418 :group 'org-agenda-search-view
1419 :version "24.1"
1420 :type 'boolean)
1422 (defcustom org-agenda-search-view-max-outline-level 0
1423 "Maximum outline level to display in search view.
1424 E.g. when this is set to 1, the search view will only
1425 show headlines of level 1. When set to 0, the default
1426 value, don't limit agenda view by outline level."
1427 :group 'org-agenda-search-view
1428 :version "24.4"
1429 :package-version '(Org . "8.3")
1430 :type 'integer)
1432 (defgroup org-agenda-time-grid nil
1433 "Options concerning the time grid in the Org-mode Agenda."
1434 :tag "Org Agenda Time Grid"
1435 :group 'org-agenda)
1437 (defcustom org-agenda-search-headline-for-time t
1438 "Non-nil means search headline for a time-of-day.
1439 If the headline contains a time-of-day in one format or another, it will
1440 be used to sort the entry into the time sequence of items for a day.
1441 Some people have time stamps in the headline that refer to the creation
1442 time or so, and then this produces an unwanted side effect. If this is
1443 the case for your, use this variable to turn off searching the headline
1444 for a time."
1445 :group 'org-agenda-time-grid
1446 :type 'boolean)
1448 (defcustom org-agenda-use-time-grid t
1449 "Non-nil means show a time grid in the agenda schedule.
1450 A time grid is a set of lines for specific times (like every two hours between
1451 8:00 and 20:00). The items scheduled for a day at specific times are
1452 sorted in between these lines.
1453 For details about when the grid will be shown, and what it will look like, see
1454 the variable `org-agenda-time-grid'."
1455 :group 'org-agenda-time-grid
1456 :type 'boolean)
1458 (defcustom org-agenda-time-grid
1459 '((daily today require-timed)
1460 "----------------"
1461 (800 1000 1200 1400 1600 1800 2000))
1463 "The settings for time grid for agenda display.
1464 This is a list of three items. The first item is again a list. It contains
1465 symbols specifying conditions when the grid should be displayed:
1467 daily if the agenda shows a single day
1468 weekly if the agenda shows an entire week
1469 today show grid on current date, independent of daily/weekly display
1470 require-timed show grid only if at least one item has a time specification
1472 The second item is a string which will be placed behind the grid time.
1474 The third item is a list of integers, indicating the times that should have
1475 a grid line."
1476 :group 'org-agenda-time-grid
1477 :type
1478 '(list
1479 (set :greedy t :tag "Grid Display Options"
1480 (const :tag "Show grid in single day agenda display" daily)
1481 (const :tag "Show grid in weekly agenda display" weekly)
1482 (const :tag "Always show grid for today" today)
1483 (const :tag "Show grid only if any timed entries are present"
1484 require-timed)
1485 (const :tag "Skip grid times already present in an entry"
1486 remove-match))
1487 (string :tag "Grid String")
1488 (repeat :tag "Grid Times" (integer :tag "Time"))))
1490 (defcustom org-agenda-show-current-time-in-grid t
1491 "Non-nil means show the current time in the time grid."
1492 :group 'org-agenda-time-grid
1493 :version "24.1"
1494 :type 'boolean)
1496 (defcustom org-agenda-current-time-string
1497 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1498 "The string for the current time marker in the agenda."
1499 :group 'org-agenda-time-grid
1500 :version "24.1"
1501 :type 'string)
1503 (defgroup org-agenda-sorting nil
1504 "Options concerning sorting in the Org-mode Agenda."
1505 :tag "Org Agenda Sorting"
1506 :group 'org-agenda)
1508 (defcustom org-agenda-sorting-strategy
1509 '((agenda habit-down time-up priority-down category-keep)
1510 (todo priority-down category-keep)
1511 (tags priority-down category-keep)
1512 (search category-keep))
1513 "Sorting structure for the agenda items of a single day.
1514 This is a list of symbols which will be used in sequence to determine
1515 if an entry should be listed before another entry. The following
1516 symbols are recognized:
1518 time-up Put entries with time-of-day indications first, early first
1519 time-down Put entries with time-of-day indications first, late first
1520 timestamp-up Sort by any timestamp, early first
1521 timestamp-down Sort by any timestamp, late first
1522 scheduled-up Sort by scheduled timestamp, early first
1523 scheduled-down Sort by scheduled timestamp, late first
1524 deadline-up Sort by deadline timestamp, early first
1525 deadline-down Sort by deadline timestamp, late first
1526 ts-up Sort by active timestamp, early first
1527 ts-down Sort by active timestamp, late first
1528 tsia-up Sort by inactive timestamp, early first
1529 tsia-down Sort by inactive timestamp, late first
1530 category-keep Keep the default order of categories, corresponding to the
1531 sequence in `org-agenda-files'.
1532 category-up Sort alphabetically by category, A-Z.
1533 category-down Sort alphabetically by category, Z-A.
1534 tag-up Sort alphabetically by last tag, A-Z.
1535 tag-down Sort alphabetically by last tag, Z-A.
1536 priority-up Sort numerically by priority, high priority last.
1537 priority-down Sort numerically by priority, high priority first.
1538 todo-state-up Sort by todo state, tasks that are done last.
1539 todo-state-down Sort by todo state, tasks that are done first.
1540 effort-up Sort numerically by estimated effort, high effort last.
1541 effort-down Sort numerically by estimated effort, high effort first.
1542 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1543 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1544 habit-up Put entries that are habits first
1545 habit-down Put entries that are habits last
1546 alpha-up Sort headlines alphabetically
1547 alpha-down Sort headlines alphabetically, reversed
1549 The different possibilities will be tried in sequence, and testing stops
1550 if one comparison returns a \"not-equal\". For example, the default
1551 '(time-up category-keep priority-down)
1552 means: Pull out all entries having a specified time of day and sort them,
1553 in order to make a time schedule for the current day the first thing in the
1554 agenda listing for the day. Of the entries without a time indication, keep
1555 the grouped in categories, don't sort the categories, but keep them in
1556 the sequence given in `org-agenda-files'. Within each category sort by
1557 priority.
1559 Leaving out `category-keep' would mean that items will be sorted across
1560 categories by priority.
1562 Instead of a single list, this can also be a set of list for specific
1563 contents, with a context symbol in the car of the list, any of
1564 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1566 Custom commands can bind this variable in the options section."
1567 :group 'org-agenda-sorting
1568 :type `(choice
1569 (repeat :tag "General" ,org-sorting-choice)
1570 (list :tag "Individually"
1571 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for TODO lists" todo)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for Tags matches" tags)
1576 (repeat ,org-sorting-choice))
1577 (cons (const :tag "Strategy for search matches" search)
1578 (repeat ,org-sorting-choice)))))
1580 (defcustom org-agenda-cmp-user-defined nil
1581 "A function to define the comparison `user-defined'.
1582 This function must receive two arguments, agenda entry a and b.
1583 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1584 the user comparison, return nil.
1585 When this is defined, you can make `user-defined-up' and `user-defined-down'
1586 part of an agenda sorting strategy."
1587 :group 'org-agenda-sorting
1588 :type 'symbol)
1590 (defcustom org-sort-agenda-notime-is-late t
1591 "Non-nil means items without time are considered late.
1592 This is only relevant for sorting. When t, items which have no explicit
1593 time like 15:30 will be considered as 99:01, i.e. later than any items which
1594 do have a time. When nil, the default time is before 0:00. You can use this
1595 option to decide if the schedule for today should come before or after timeless
1596 agenda entries."
1597 :group 'org-agenda-sorting
1598 :type 'boolean)
1600 (defcustom org-sort-agenda-noeffort-is-high t
1601 "Non-nil means items without effort estimate are sorted as high effort.
1602 This also applies when filtering an agenda view with respect to the
1603 < or > effort operator. Then, tasks with no effort defined will be treated
1604 as tasks with high effort.
1605 When nil, such items are sorted as 0 minutes effort."
1606 :group 'org-agenda-sorting
1607 :type 'boolean)
1609 (defgroup org-agenda-line-format nil
1610 "Options concerning the entry prefix in the Org-mode agenda display."
1611 :tag "Org Agenda Line Format"
1612 :group 'org-agenda)
1614 (defcustom org-agenda-prefix-format
1615 '((agenda . " %i %-12:c%?-12t% s")
1616 (timeline . " % s")
1617 (todo . " %i %-12:c")
1618 (tags . " %i %-12:c")
1619 (search . " %i %-12:c"))
1620 "Format specifications for the prefix of items in the agenda views.
1621 An alist with five entries, each for the different agenda types. The
1622 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1623 The values are format strings.
1625 This format works similar to a printf format, with the following meaning:
1627 %c the category of the item, \"Diary\" for entries from the diary,
1628 or as given by the CATEGORY keyword or derived from the file name
1629 %e the effort required by the item
1630 %l the level of the item (insert X space(s) if item is of level X)
1631 %i the icon category of the item, see `org-agenda-category-icon-alist'
1632 %T the last tag of the item (ignore inherited tags, which come first)
1633 %t the HH:MM time-of-day specification if one applies to the entry
1634 %s Scheduling/Deadline information, a short string
1635 %b show breadcrumbs, i.e., the names of the higher levels
1636 %(expression) Eval EXPRESSION and replace the control string
1637 by the result
1639 All specifiers work basically like the standard `%s' of printf, but may
1640 contain two additional characters: a question mark just after the `%'
1641 and a whitespace/punctuation character just before the final letter.
1643 If the first character after `%' is a question mark, the entire field
1644 will only be included if the corresponding value applies to the current
1645 entry. This is useful for fields which should have fixed width when
1646 present, but zero width when absent. For example, \"%?-12t\" will
1647 result in a 12 character time field if a time of the day is specified,
1648 but will completely disappear in entries which do not contain a time.
1650 If there is punctuation or whitespace character just before the
1651 final format letter, this character will be appended to the field
1652 value if the value is not empty. For example, the format
1653 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1654 the category is empty, no additional colon is inserted.
1656 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1657 which means:
1659 - Indent the line with two space characters
1660 - Give the category a 12 chars wide field, padded with whitespace on
1661 the right (because of `-'). Append a colon if there is a category
1662 (because of `:').
1663 - If there is a time-of-day, put it into a 12 chars wide field. If no
1664 time, don't put in an empty field, just skip it (because of '?').
1665 - Finally, put the scheduling information.
1667 See also the variables `org-agenda-remove-times-when-in-prefix' and
1668 `org-agenda-remove-tags'.
1670 Custom commands can set this variable in the options section."
1671 :type '(choice
1672 (string :tag "General format")
1673 (list :greedy t :tag "View dependent"
1674 (cons (const agenda) (string :tag "Format"))
1675 (cons (const timeline) (string :tag "Format"))
1676 (cons (const todo) (string :tag "Format"))
1677 (cons (const tags) (string :tag "Format"))
1678 (cons (const search) (string :tag "Format"))))
1679 :group 'org-agenda-line-format)
1681 (defvar org-prefix-format-compiled nil
1682 "The compiled prefix format and associated variables.
1683 This is a list where first element is a list of variable bindings, and second
1684 element is the compiled format expression. See the variable
1685 `org-agenda-prefix-format'.")
1687 (defcustom org-agenda-todo-keyword-format "%-1s"
1688 "Format for the TODO keyword in agenda lines.
1689 Set this to something like \"%-12s\" if you want all TODO keywords
1690 to occupy a fixed space in the agenda display."
1691 :group 'org-agenda-line-format
1692 :type 'string)
1694 (defcustom org-agenda-diary-sexp-prefix nil
1695 "A regexp that matches part of a diary sexp entry
1696 which should be treated as scheduling/deadline information in
1697 `org-agenda'.
1699 For example, you can use this to extract the `diary-remind-message' from
1700 `diary-remind' entries."
1701 :group 'org-agenda-line-format
1702 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1704 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1705 "Text preceding timerange entries in the agenda view.
1706 This is a list with two strings. The first applies when the range
1707 is entirely on one day. The second applies if the range spans several days.
1708 The strings may have two \"%d\" format specifiers which will be filled
1709 with the sequence number of the days, and the total number of days in the
1710 range, respectively."
1711 :group 'org-agenda-line-format
1712 :type '(list
1713 (string :tag "Deadline today ")
1714 (choice :tag "Deadline relative"
1715 (string :tag "Format string")
1716 (function))))
1718 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1719 "Text preceding scheduled items in the agenda view.
1720 This is a list with two strings. The first applies when the item is
1721 scheduled on the current day. The second applies when it has been scheduled
1722 previously, it may contain a %d indicating that this is the nth time that
1723 this item is scheduled, due to automatic rescheduling of unfinished items
1724 for the following day. So this number is one larger than the number of days
1725 that passed since this item was scheduled first."
1726 :group 'org-agenda-line-format
1727 :version "24.4"
1728 :package-version '(Org . "8.0")
1729 :type '(list
1730 (string :tag "Scheduled today ")
1731 (string :tag "Scheduled previously")))
1733 (defcustom org-agenda-inactive-leader "["
1734 "Text preceding item pulled into the agenda by inactive time stamps.
1735 These entries are added to the agenda when pressing \"[\"."
1736 :group 'org-agenda-line-format
1737 :version "24.1"
1738 :type 'string)
1740 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1741 "Text preceding deadline items in the agenda view.
1742 This is a list with three strings. The first applies when the item has its
1743 deadline on the current day. The second applies when the deadline is in the
1744 future, the third one when it is in the past. The strings may contain %d
1745 to capture the number of days."
1746 :group 'org-agenda-line-format
1747 :version "24.4"
1748 :package-version '(Org . "8.0")
1749 :type '(list
1750 (string :tag "Deadline today ")
1751 (string :tag "Deadline in the future ")
1752 (string :tag "Deadline in the past ")))
1754 (defcustom org-agenda-remove-times-when-in-prefix t
1755 "Non-nil means remove duplicate time specifications in agenda items.
1756 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1757 time-of-day specification in a headline or diary entry is extracted and
1758 placed into the prefix. If this option is non-nil, the original specification
1759 \(a timestamp or -range, or just a plain time(range) specification like
1760 11:30-4pm) will be removed for agenda display. This makes the agenda less
1761 cluttered.
1762 The option can be t or nil. It may also be the symbol `beg', indicating
1763 that the time should only be removed when it is located at the beginning of
1764 the headline/diary entry."
1765 :group 'org-agenda-line-format
1766 :type '(choice
1767 (const :tag "Always" t)
1768 (const :tag "Never" nil)
1769 (const :tag "When at beginning of entry" beg)))
1771 (defcustom org-agenda-remove-timeranges-from-blocks nil
1772 "Non-nil means remove time ranges specifications in agenda
1773 items that span on several days."
1774 :group 'org-agenda-line-format
1775 :version "24.1"
1776 :type 'boolean)
1778 (defcustom org-agenda-default-appointment-duration nil
1779 "Default duration for appointments that only have a starting time.
1780 When nil, no duration is specified in such cases.
1781 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1782 :group 'org-agenda-line-format
1783 :type '(choice
1784 (integer :tag "Minutes")
1785 (const :tag "No default duration")))
1787 (defcustom org-agenda-show-inherited-tags t
1788 "Non-nil means show inherited tags in each agenda line.
1790 When this option is set to 'always, it take precedences over
1791 `org-agenda-use-tag-inheritance' and inherited tags are shown
1792 in every agenda.
1794 When this option is set to t (the default), inherited tags are
1795 shown when they are available, i.e. when the value of
1796 `org-agenda-use-tag-inheritance' has been taken into account.
1798 This can be set to a list of agenda types in which the agenda
1799 must display the inherited tags. Available types are 'todo,
1800 'agenda, 'search and 'timeline.
1802 When set to nil, never show inherited tags in agenda lines."
1803 :group 'org-agenda-line-format
1804 :group 'org-agenda
1805 :version "24.3"
1806 :type '(choice
1807 (const :tag "Show inherited tags when available" t)
1808 (const :tag "Always show inherited tags" always)
1809 (repeat :tag "Show inherited tags only in selected agenda types"
1810 (symbol :tag "Agenda type"))))
1812 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1813 "List of agenda view types where to use tag inheritance.
1815 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1816 controlled by `org-use-tag-inheritance'. In other agenda types,
1817 `org-use-tag-inheritance' is not used for the selection of the
1818 agenda entries. Still, you may want the agenda to be aware of
1819 the inherited tags anyway, e.g. for later tag filtering.
1821 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1823 This variable has no effect if `org-agenda-show-inherited-tags'
1824 is set to 'always. In that case, the agenda is aware of those
1825 tags.
1827 The default value sets tags in every agenda type. Setting this
1828 option to nil will speed up non-tags agenda view a lot."
1829 :group 'org-agenda
1830 :version "24.3"
1831 :type '(choice
1832 (const :tag "Use tag inheritance in all agenda types" t)
1833 (repeat :tag "Use tag inheritance in selected agenda types"
1834 (symbol :tag "Agenda type"))))
1836 (defcustom org-agenda-hide-tags-regexp nil
1837 "Regular expression used to filter away specific tags in agenda views.
1838 This means that these tags will be present, but not be shown in the agenda
1839 line. Secondary filtering will still work on the hidden tags.
1840 Nil means don't hide any tags."
1841 :group 'org-agenda-line-format
1842 :type '(choice
1843 (const :tag "Hide none" nil)
1844 (string :tag "Regexp ")))
1846 (defcustom org-agenda-remove-tags nil
1847 "Non-nil means remove the tags from the headline copy in the agenda.
1848 When this is the symbol `prefix', only remove tags when
1849 `org-agenda-prefix-format' contains a `%T' specifier."
1850 :group 'org-agenda-line-format
1851 :type '(choice
1852 (const :tag "Always" t)
1853 (const :tag "Never" nil)
1854 (const :tag "When prefix format contains %T" prefix)))
1856 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1857 'org-agenda-remove-tags)
1859 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1860 "Shift tags in agenda items to this column.
1861 If this number is positive, it specifies the column. If it is negative,
1862 it means that the tags should be flushright to that column. For example,
1863 -80 works well for a normal 80 character screen."
1864 :group 'org-agenda-line-format
1865 :type 'integer)
1867 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1869 (defcustom org-agenda-fontify-priorities 'cookies
1870 "Non-nil means highlight low and high priorities in agenda.
1871 When t, the highest priority entries are bold, lowest priority italic.
1872 However, settings in `org-priority-faces' will overrule these faces.
1873 When this variable is the symbol `cookies', only fontify the
1874 cookies, not the entire task.
1875 This may also be an association list of priority faces, whose
1876 keys are the character values of `org-highest-priority',
1877 `org-default-priority', and `org-lowest-priority' (the default values
1878 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1879 color as a string, or a list like `(:background \"Red\")'.
1880 If it is a color, the variable `org-faces-easy-properties'
1881 determines if it is a foreground or a background color."
1882 :group 'org-agenda-line-format
1883 :type '(choice
1884 (const :tag "Never" nil)
1885 (const :tag "Defaults" t)
1886 (const :tag "Cookies only" cookies)
1887 (repeat :tag "Specify"
1888 (list (character :tag "Priority" :value ?A)
1889 (choice :tag "Face "
1890 (string :tag "Color")
1891 (sexp :tag "Face"))))))
1893 (defcustom org-agenda-day-face-function nil
1894 "Function called to determine what face should be used to display a day.
1895 The only argument passed to that function is the day. It should
1896 returns a face, or nil if does not want to specify a face and let
1897 the normal rules apply."
1898 :group 'org-agenda-line-format
1899 :version "24.1"
1900 :type '(choice (const nil) (function)))
1902 (defcustom org-agenda-category-icon-alist nil
1903 "Alist of category icon to be displayed in agenda views.
1905 Each entry should have the following format:
1907 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1909 Where CATEGORY-REGEXP is a regexp matching the categories where
1910 the icon should be displayed.
1911 FILE-OR-DATA either a file path or a string containing image data.
1913 The other fields can be omitted safely if not needed:
1914 TYPE indicates the image type.
1915 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1916 image data.
1917 PROPS are additional image attributes to assign to the image,
1918 like, e.g. `:ascent center'.
1920 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1922 If you want to set the display properties yourself, just put a
1923 list as second element:
1925 (CATEGORY-REGEXP (MY PROPERTY LIST))
1927 For example, to display a 16px horizontal space for Emacs
1928 category, you can use:
1930 (\"Emacs\" '(space . (:width (16))))"
1931 :group 'org-agenda-line-format
1932 :version "24.1"
1933 :type '(alist :key-type (string :tag "Regexp matching category")
1934 :value-type (choice (list :tag "Icon"
1935 (string :tag "File or data")
1936 (symbol :tag "Type")
1937 (boolean :tag "Data?")
1938 (repeat :tag "Extra image properties" :inline t symbol))
1939 (list :tag "Display properties" sexp))))
1941 (defgroup org-agenda-column-view nil
1942 "Options concerning column view in the agenda."
1943 :tag "Org Agenda Column View"
1944 :group 'org-agenda)
1946 (defcustom org-agenda-columns-show-summaries t
1947 "Non-nil means show summaries for columns displayed in the agenda view."
1948 :group 'org-agenda-column-view
1949 :type 'boolean)
1951 (defcustom org-agenda-columns-compute-summary-properties t
1952 "Non-nil means recompute all summary properties before column view.
1953 When column view in the agenda is listing properties that have a summary
1954 operator, it can go to all relevant buffers and recompute the summaries
1955 there. This can mean overhead for the agenda column view, but is necessary
1956 to have thing up to date.
1957 As a special case, a CLOCKSUM property also makes sure that the clock
1958 computations are current."
1959 :group 'org-agenda-column-view
1960 :type 'boolean)
1962 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1963 "Non-nil means the duration of an appointment will add to day effort.
1964 The property to which appointment durations will be added is the one given
1965 in the option `org-effort-property'. If an appointment does not have
1966 an end time, `org-agenda-default-appointment-duration' will be used. If that
1967 is not set, an appointment without end time will not contribute to the time
1968 estimate."
1969 :group 'org-agenda-column-view
1970 :type 'boolean)
1972 (defcustom org-agenda-auto-exclude-function nil
1973 "A function called with a tag to decide if it is filtered on '/ RET'.
1974 The sole argument to the function, which is called once for each
1975 possible tag, is a string giving the name of the tag. The
1976 function should return either nil if the tag should be included
1977 as normal, or \"-<TAG>\" to exclude the tag.
1978 Note that for the purpose of tag filtering, only the lower-case version of
1979 all tags will be considered, so that this function will only ever see
1980 the lower-case version of all tags."
1981 :group 'org-agenda
1982 :type '(choice (const nil) (function)))
1984 (defcustom org-agenda-bulk-custom-functions nil
1985 "Alist of characters and custom functions for bulk actions.
1986 For example, this value makes those two functions available:
1988 '((?R set-category)
1989 (?C bulk-cut))
1991 With selected entries in an agenda buffer, `B R' will call
1992 the custom function `set-category' on the selected entries.
1993 Note that functions in this alist don't need to be quoted."
1994 :type 'alist
1995 :version "24.1"
1996 :group 'org-agenda)
1998 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1999 "Execute BODY with point at location given by `org-hd-marker' property.
2000 If STRING is non-nil, the text property will be fetched from position 0
2001 in that string. If STRING is nil, it will be fetched from the beginning
2002 of the current line."
2003 (org-with-gensyms (marker)
2004 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2005 'org-hd-marker ,string)))
2006 (with-current-buffer (marker-buffer ,marker)
2007 (save-excursion
2008 (goto-char ,marker)
2009 ,@body)))))
2010 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2012 (defun org-add-agenda-custom-command (entry)
2013 "Replace or add a command in `org-agenda-custom-commands'.
2014 This is mostly for hacking and trying a new command - once the command
2015 works you probably want to add it to `org-agenda-custom-commands' for good."
2016 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2017 (if ass
2018 (setcdr ass (cdr entry))
2019 (push entry org-agenda-custom-commands))))
2021 ;;; Define the org-agenda-mode
2023 (defvar org-agenda-mode-map (make-sparse-keymap)
2024 "Keymap for `org-agenda-mode'.")
2025 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2027 (defvar org-agenda-menu) ; defined later in this file.
2028 (defvar org-agenda-restrict nil) ; defined later in this file.
2029 (defvar org-agenda-follow-mode nil)
2030 (defvar org-agenda-entry-text-mode nil)
2031 (defvar org-agenda-clockreport-mode nil)
2032 (defvar org-agenda-show-log nil)
2033 (defvar org-agenda-redo-command nil)
2034 (defvar org-agenda-query-string nil)
2035 (defvar org-agenda-mode-hook nil
2036 "Hook run after `org-agenda-mode' is turned on.
2037 The buffer is still writable when this hook is called.")
2038 (defvar org-agenda-type nil)
2039 (defvar org-agenda-force-single-file nil)
2040 (defvar org-agenda-bulk-marked-entries nil
2041 "List of markers that refer to marked entries in the agenda.")
2043 ;;; Multiple agenda buffers support
2045 (defcustom org-agenda-sticky nil
2046 "Non-nil means agenda q key will bury agenda buffers.
2047 Agenda commands will then show existing buffer instead of generating new ones.
2048 When nil, `q' will kill the single agenda buffer."
2049 :group 'org-agenda
2050 :version "24.3"
2051 :type 'boolean)
2054 ;;;###autoload
2055 (defun org-toggle-sticky-agenda (&optional arg)
2056 "Toggle `org-agenda-sticky'."
2057 (interactive "P")
2058 (let ((new-value (if arg
2059 (> (prefix-numeric-value arg) 0)
2060 (not org-agenda-sticky))))
2061 (if (equal new-value org-agenda-sticky)
2062 (and (org-called-interactively-p 'interactive)
2063 (message "Sticky agenda was already %s"
2064 (if org-agenda-sticky "enabled" "disabled")))
2065 (setq org-agenda-sticky new-value)
2066 (org-agenda-kill-all-agenda-buffers)
2067 (and (org-called-interactively-p 'interactive)
2068 (message "Sticky agenda was %s"
2069 (if org-agenda-sticky "enabled" "disabled"))))))
2071 (defvar org-agenda-buffer nil
2072 "Agenda buffer currently being generated.")
2074 (defvar org-agenda-last-prefix-arg nil)
2075 (defvar org-agenda-this-buffer-name nil)
2076 (defvar org-agenda-doing-sticky-redo nil)
2077 (defvar org-agenda-this-buffer-is-sticky nil)
2079 (defconst org-agenda-local-vars
2080 '(org-agenda-this-buffer-name
2081 org-agenda-undo-list
2082 org-agenda-pending-undo-list
2083 org-agenda-follow-mode
2084 org-agenda-entry-text-mode
2085 org-agenda-clockreport-mode
2086 org-agenda-show-log
2087 org-agenda-redo-command
2088 org-agenda-query-string
2089 org-agenda-type
2090 org-agenda-bulk-marked-entries
2091 org-agenda-undo-has-started-in
2092 org-agenda-info
2093 org-agenda-pre-window-conf
2094 org-agenda-columns-active
2095 org-agenda-tag-filter-overlays
2096 org-agenda-tag-filter
2097 org-agenda-cat-filter-overlays
2098 org-agenda-category-filter
2099 org-agenda-re-filter-overlays
2100 org-agenda-regexp-filter
2101 org-agenda-markers
2102 org-agenda-last-search-view-search-was-boolean
2103 org-agenda-filtered-by-category
2104 org-agenda-filter-form
2105 org-agenda-cycle-counter
2106 org-agenda-last-prefix-arg)
2107 "Variables that must be local in agenda buffers to allow multiple buffers.")
2109 (defun org-agenda-mode ()
2110 "Mode for time-sorted view on action items in Org-mode files.
2112 The following commands are available:
2114 \\{org-agenda-mode-map}"
2115 (interactive)
2116 (cond (org-agenda-doing-sticky-redo
2117 ;; Refreshing sticky agenda-buffer
2119 ;; Preserve the value of `org-agenda-local-vars' variables,
2120 ;; while letting `kill-all-local-variables' kill the rest
2121 (let ((save (buffer-local-variables)))
2122 (kill-all-local-variables)
2123 (mapc 'make-local-variable org-agenda-local-vars)
2124 (dolist (elem save)
2125 (let ((var (car elem))
2126 (val (cdr elem)))
2127 (when (and val
2128 (member var org-agenda-local-vars))
2129 (set var val)))))
2130 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2131 (org-agenda-sticky
2132 ;; Creating a sticky Agenda buffer for the first time
2133 (kill-all-local-variables)
2134 (mapc 'make-local-variable org-agenda-local-vars)
2135 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2137 ;; Creating a non-sticky agenda buffer
2138 (kill-all-local-variables)
2139 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2140 (setq org-agenda-undo-list nil
2141 org-agenda-pending-undo-list nil
2142 org-agenda-bulk-marked-entries nil)
2143 (setq major-mode 'org-agenda-mode)
2144 ;; Keep global-font-lock-mode from turning on font-lock-mode
2145 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2146 (setq mode-name "Org-Agenda")
2147 (setq indent-tabs-mode nil)
2148 (use-local-map org-agenda-mode-map)
2149 (easy-menu-add org-agenda-menu)
2150 (if org-startup-truncated (setq truncate-lines t))
2151 (org-set-local 'line-move-visual nil)
2152 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2153 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2154 ;; Make sure properties are removed when copying text
2155 (org-add-hook 'filter-buffer-substring-functions
2156 (lambda (fun start end delete)
2157 (substring-no-properties (funcall fun start end delete)))
2158 nil t)
2159 (unless org-agenda-keep-modes
2160 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2161 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2162 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2163 org-agenda-show-log org-agenda-start-with-log-mode))
2165 (easy-menu-change
2166 '("Agenda") "Agenda Files"
2167 (append
2168 (list
2169 (vector
2170 (if (get 'org-agenda-files 'org-restrict)
2171 "Restricted to single file"
2172 "Edit File List")
2173 '(org-edit-agenda-file-list)
2174 (not (get 'org-agenda-files 'org-restrict)))
2175 "--")
2176 (mapcar 'org-file-menu-entry (org-agenda-files))))
2177 (org-agenda-set-mode-name)
2178 (apply
2179 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2180 (list 'org-agenda-mode-hook)))
2182 (substitute-key-definition 'undo 'org-agenda-undo
2183 org-agenda-mode-map global-map)
2184 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2185 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2186 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2187 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2188 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2189 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2190 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2191 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2192 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2193 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2194 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2195 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2196 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2197 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2198 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2199 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2200 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2201 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2202 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2203 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2204 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2205 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2206 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2207 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2208 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2209 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2210 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2211 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2212 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2213 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2214 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2215 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2216 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2217 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2218 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2219 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2220 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2221 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2222 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2223 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2224 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2225 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2226 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2227 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2228 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2229 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2230 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2231 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2232 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2233 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2235 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2236 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2237 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2238 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2239 (while l (org-defkey org-agenda-mode-map
2240 (int-to-string (pop l)) 'digit-argument)))
2242 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2243 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2244 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2245 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2246 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2247 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2248 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2249 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2250 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2251 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2252 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2253 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2254 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2255 'org-clock-modify-effort-estimate)
2256 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2257 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2258 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2259 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2260 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2261 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2262 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2263 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2264 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2265 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2266 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2267 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2268 (substitute-key-definition 'next-line 'org-agenda-next-line
2269 org-agenda-mode-map global-map)
2270 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2271 org-agenda-mode-map global-map)
2272 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2273 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2274 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2275 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2276 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2277 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2278 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2279 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2280 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2281 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2282 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2283 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2285 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2287 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2289 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2291 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2292 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2293 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2294 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2297 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2298 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2299 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2300 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2303 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2304 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2305 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2306 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2307 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2308 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2309 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2310 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2311 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2312 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2313 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2314 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2315 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2317 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2319 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2320 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2321 (when org-agenda-mouse-1-follows-link
2322 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2323 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2324 '("Agenda"
2325 ("Agenda Files")
2326 "--"
2327 ("Agenda Dates"
2328 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2329 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2330 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2331 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2332 "--"
2333 ("View"
2334 ["Day View" org-agenda-day-view
2335 :active (org-agenda-check-type nil 'agenda)
2336 :style radio :selected (eq org-agenda-current-span 'day)
2337 :keys "v d (or just d)"]
2338 ["Week View" org-agenda-week-view
2339 :active (org-agenda-check-type nil 'agenda)
2340 :style radio :selected (eq org-agenda-current-span 'week)
2341 :keys "v w"]
2342 ["Fortnight View" org-agenda-fortnight-view
2343 :active (org-agenda-check-type nil 'agenda)
2344 :style radio :selected (eq org-agenda-current-span 'fortnight)
2345 :keys "v f"]
2346 ["Month View" org-agenda-month-view
2347 :active (org-agenda-check-type nil 'agenda)
2348 :style radio :selected (eq org-agenda-current-span 'month)
2349 :keys "v m"]
2350 ["Year View" org-agenda-year-view
2351 :active (org-agenda-check-type nil 'agenda)
2352 :style radio :selected (eq org-agenda-current-span 'year)
2353 :keys "v y"]
2354 "--"
2355 ["Include Diary" org-agenda-toggle-diary
2356 :style toggle :selected org-agenda-include-diary
2357 :active (org-agenda-check-type nil 'agenda)]
2358 ["Include Deadlines" org-agenda-toggle-deadlines
2359 :style toggle :selected org-agenda-include-deadlines
2360 :active (org-agenda-check-type nil 'agenda)]
2361 ["Use Time Grid" org-agenda-toggle-time-grid
2362 :style toggle :selected org-agenda-use-time-grid
2363 :active (org-agenda-check-type nil 'agenda)]
2364 "--"
2365 ["Show clock report" org-agenda-clockreport-mode
2366 :style toggle :selected org-agenda-clockreport-mode
2367 :active (org-agenda-check-type nil 'agenda)]
2368 ["Show some entry text" org-agenda-entry-text-mode
2369 :style toggle :selected org-agenda-entry-text-mode
2370 :active t]
2371 "--"
2372 ["Show Logbook entries" org-agenda-log-mode
2373 :style toggle :selected org-agenda-show-log
2374 :active (org-agenda-check-type nil 'agenda 'timeline)
2375 :keys "v l (or just l)"]
2376 ["Include archived trees" org-agenda-archives-mode
2377 :style toggle :selected org-agenda-archives-mode :active t
2378 :keys "v a"]
2379 ["Include archive files" (org-agenda-archives-mode t)
2380 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2381 :keys "v A"]
2382 "--"
2383 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2384 ["Write view to file" org-agenda-write t]
2385 ["Rebuild buffer" org-agenda-redo t]
2386 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2387 "--"
2388 ["Show original entry" org-agenda-show t]
2389 ["Go To (other window)" org-agenda-goto t]
2390 ["Go To (this window)" org-agenda-switch-to t]
2391 ["Capture with cursor date" org-agenda-capture t]
2392 ["Follow Mode" org-agenda-follow-mode
2393 :style toggle :selected org-agenda-follow-mode :active t]
2394 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2395 "--"
2396 ("TODO"
2397 ["Cycle TODO" org-agenda-todo t]
2398 ["Next TODO set" org-agenda-todo-nextset t]
2399 ["Previous TODO set" org-agenda-todo-previousset t]
2400 ["Add note" org-agenda-add-note t])
2401 ("Archive/Refile/Delete"
2402 ["Archive default" org-agenda-archive-default t]
2403 ["Archive default" org-agenda-archive-default-with-confirmation t]
2404 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2405 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2406 ["Archive subtree" org-agenda-archive t]
2407 "--"
2408 ["Refile" org-agenda-refile t]
2409 "--"
2410 ["Delete subtree" org-agenda-kill t])
2411 ("Bulk action"
2412 ["Mark entry" org-agenda-bulk-mark t]
2413 ["Mark all" org-agenda-bulk-mark-all t]
2414 ["Unmark entry" org-agenda-bulk-unmark t]
2415 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2416 ["Toggle mark" org-agenda-bulk-toggle t]
2417 ["Toggle all" org-agenda-bulk-toggle-all t]
2418 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2419 ["Act on all marked" org-agenda-bulk-action t]
2420 "--"
2421 ("Tags and Properties"
2422 ["Show all Tags" org-agenda-show-tags t]
2423 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2424 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2425 "--"
2426 ["Column View" org-columns t])
2427 ("Deadline/Schedule"
2428 ["Schedule" org-agenda-schedule t]
2429 ["Set Deadline" org-agenda-deadline t]
2430 "--"
2431 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2432 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2433 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2434 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2435 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2436 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2437 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2438 ("Clock and Effort"
2439 ["Clock in" org-agenda-clock-in t]
2440 ["Clock out" org-agenda-clock-out t]
2441 ["Clock cancel" org-agenda-clock-cancel t]
2442 ["Goto running clock" org-clock-goto t]
2443 "--"
2444 ["Set Effort" org-agenda-set-effort t]
2445 ["Change clocked effort" org-clock-modify-effort-estimate
2446 (org-clock-is-active)])
2447 ("Priority"
2448 ["Set Priority" org-agenda-priority t]
2449 ["Increase Priority" org-agenda-priority-up t]
2450 ["Decrease Priority" org-agenda-priority-down t]
2451 ["Show Priority" org-show-priority t])
2452 ("Calendar/Diary"
2453 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2454 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2455 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2456 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2457 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2459 "--"
2460 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2461 "--"
2462 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2463 "--"
2464 ("MobileOrg"
2465 ["Push Files and Views" org-mobile-push t]
2466 ["Get Captured and Flagged" org-mobile-pull t]
2467 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2468 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2469 "--"
2470 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2471 "--"
2472 ["Quit" org-agenda-quit t]
2473 ["Exit and Release Buffers" org-agenda-exit t]
2476 ;;; Agenda undo
2478 (defvar org-agenda-allow-remote-undo t
2479 "Non-nil means allow remote undo from the agenda buffer.")
2480 (defvar org-agenda-undo-has-started-in nil
2481 "Buffers that have already seen `undo-start' in the current undo sequence.")
2483 (defun org-agenda-undo ()
2484 "Undo a remote editing step in the agenda.
2485 This undoes changes both in the agenda buffer and in the remote buffer
2486 that have been changed along."
2487 (interactive)
2488 (or org-agenda-allow-remote-undo
2489 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2490 (if (not (eq this-command last-command))
2491 (setq org-agenda-undo-has-started-in nil
2492 org-agenda-pending-undo-list org-agenda-undo-list))
2493 (if (not org-agenda-pending-undo-list)
2494 (user-error "No further undo information"))
2495 (let* ((entry (pop org-agenda-pending-undo-list))
2496 buf line cmd rembuf)
2497 (setq cmd (pop entry) line (pop entry))
2498 (setq rembuf (nth 2 entry))
2499 (org-with-remote-undo rembuf
2500 (while (bufferp (setq buf (pop entry)))
2501 (if (pop entry)
2502 (with-current-buffer buf
2503 (let ((last-undo-buffer buf)
2504 (inhibit-read-only t))
2505 (unless (memq buf org-agenda-undo-has-started-in)
2506 (push buf org-agenda-undo-has-started-in)
2507 (make-local-variable 'pending-undo-list)
2508 (undo-start))
2509 (while (and pending-undo-list
2510 (listp pending-undo-list)
2511 (not (car pending-undo-list)))
2512 (pop pending-undo-list))
2513 (undo-more 1))))))
2514 (org-goto-line line)
2515 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2517 (defun org-verify-change-for-undo (l1 l2)
2518 "Verify that a real change occurred between the undo lists L1 and L2."
2519 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2520 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2521 (not (eq l1 l2)))
2523 ;;; Agenda dispatch
2525 (defvar org-agenda-restrict-begin (make-marker))
2526 (defvar org-agenda-restrict-end (make-marker))
2527 (defvar org-agenda-last-dispatch-buffer nil)
2528 (defvar org-agenda-overriding-restriction nil)
2530 (defcustom org-agenda-custom-commands-contexts nil
2531 "Alist of custom agenda keys and contextual rules.
2533 For example, if you have a custom agenda command \"p\" and you
2534 want this command to be accessible only from plain text files,
2535 use this:
2537 '((\"p\" ((in-file . \"\\.txt\"))))
2539 Here are the available contexts definitions:
2541 in-file: command displayed only in matching files
2542 in-mode: command displayed only in matching modes
2543 not-in-file: command not displayed in matching files
2544 not-in-mode: command not displayed in matching modes
2545 in-buffer: command displayed only in matching buffers
2546 not-in-buffer: command not displayed in matching buffers
2547 [function]: a custom function taking no argument
2549 If you define several checks, the agenda command will be
2550 accessible if there is at least one valid check.
2552 You can also bind a key to another agenda custom command
2553 depending on contextual rules.
2555 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2557 Here it means: in .txt files, use \"p\" as the key for the
2558 agenda command otherwise associated with \"q\". (The command
2559 originally associated with \"q\" is not displayed to avoid
2560 duplicates.)"
2561 :version "24.3"
2562 :group 'org-agenda-custom-commands
2563 :type '(repeat (list :tag "Rule"
2564 (string :tag " Agenda key")
2565 (string :tag "Replace by command")
2566 (repeat :tag "Available when"
2567 (choice
2568 (cons :tag "Condition"
2569 (choice
2570 (const :tag "In file" in-file)
2571 (const :tag "Not in file" not-in-file)
2572 (const :tag "In buffer" in-buffer)
2573 (const :tag "Not in buffer" not-in-buffer)
2574 (const :tag "In mode" in-mode)
2575 (const :tag "Not in mode" not-in-mode))
2576 (regexp))
2577 (function :tag "Custom function"))))))
2579 (defcustom org-agenda-max-entries nil
2580 "Maximum number of entries to display in an agenda.
2581 This can be nil (no limit) or an integer or an alist of agenda
2582 types with an associated number of entries to display in this
2583 type."
2584 :version "24.4"
2585 :package-version '(Org . "8.0")
2586 :group 'org-agenda-custom-commands
2587 :type '(choice (symbol :tag "No limit" nil)
2588 (integer :tag "Max number of entries")
2589 (repeat
2590 (cons (choice :tag "Agenda type"
2591 (const agenda)
2592 (const todo)
2593 (const tags)
2594 (const search)
2595 (const timeline))
2596 (integer :tag "Max number of entries")))))
2598 (defcustom org-agenda-max-todos nil
2599 "Maximum number of TODOs to display in an agenda.
2600 This can be nil (no limit) or an integer or an alist of agenda
2601 types with an associated number of entries to display in this
2602 type."
2603 :version "24.4"
2604 :package-version '(Org . "8.0")
2605 :group 'org-agenda-custom-commands
2606 :type '(choice (symbol :tag "No limit" nil)
2607 (integer :tag "Max number of entries")
2608 (repeat
2609 (cons (choice :tag "Agenda type"
2610 (const agenda)
2611 (const todo)
2612 (const tags)
2613 (const search)
2614 (const timeline))
2615 (integer :tag "Max number of entries")))))
2617 (defcustom org-agenda-max-tags nil
2618 "Maximum number of tagged entries to display in an agenda.
2619 This can be nil (no limit) or an integer or an alist of agenda
2620 types with an associated number of entries to display in this
2621 type."
2622 :version "24.4"
2623 :package-version '(Org . "8.0")
2624 :group 'org-agenda-custom-commands
2625 :type '(choice (symbol :tag "No limit" nil)
2626 (integer :tag "Max number of entries")
2627 (repeat
2628 (cons (choice :tag "Agenda type"
2629 (const agenda)
2630 (const todo)
2631 (const tags)
2632 (const search)
2633 (const timeline))
2634 (integer :tag "Max number of entries")))))
2636 (defcustom org-agenda-max-effort nil
2637 "Maximum cumulated effort duration for the agenda.
2638 This can be nil (no limit) or a number of minutes (as an integer)
2639 or an alist of agenda types with an associated number of minutes
2640 to limit entries to in this type."
2641 :version "24.4"
2642 :package-version '(Org . "8.0")
2643 :group 'org-agenda-custom-commands
2644 :type '(choice (symbol :tag "No limit" nil)
2645 (integer :tag "Max number of entries")
2646 (repeat
2647 (cons (choice :tag "Agenda type"
2648 (const agenda)
2649 (const todo)
2650 (const tags)
2651 (const search)
2652 (const timeline))
2653 (integer :tag "Max number of entries")))))
2655 (defvar org-keys nil)
2656 (defvar org-match nil)
2657 ;;;###autoload
2658 (defun org-agenda (&optional arg org-keys restriction)
2659 "Dispatch agenda commands to collect entries to the agenda buffer.
2660 Prompts for a command to execute. Any prefix arg will be passed
2661 on to the selected command. The default selections are:
2663 a Call `org-agenda-list' to display the agenda for current day or week.
2664 t Call `org-todo-list' to display the global todo list.
2665 T Call `org-todo-list' to display the global todo list, select only
2666 entries with a specific TODO keyword (the user gets a prompt).
2667 m Call `org-tags-view' to display headlines with tags matching
2668 a condition (the user is prompted for the condition).
2669 M Like `m', but select only TODO entries, no ordinary headlines.
2670 L Create a timeline for the current buffer.
2671 e Export views to associated files.
2672 s Search entries for keywords.
2673 S Search entries for keywords, only with TODO keywords.
2674 / Multi occur across all agenda files and also files listed
2675 in `org-agenda-text-search-extra-files'.
2676 < Restrict agenda commands to buffer, subtree, or region.
2677 Press several times to get the desired effect.
2678 > Remove a previous restriction.
2679 # List \"stuck\" projects.
2680 ! Configure what \"stuck\" means.
2681 C Configure custom agenda commands.
2683 More commands can be added by configuring the variable
2684 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2685 searches can be pre-defined in this way.
2687 If the current buffer is in Org-mode and visiting a file, you can also
2688 first press `<' once to indicate that the agenda should be temporarily
2689 \(until the next use of \\[org-agenda]) restricted to the current file.
2690 Pressing `<' twice means to restrict to the current subtree or region
2691 \(if active)."
2692 (interactive "P")
2693 (catch 'exit
2694 (let* ((prefix-descriptions nil)
2695 (org-agenda-buffer-name org-agenda-buffer-name)
2696 (org-agenda-window-setup (if (equal (buffer-name)
2697 org-agenda-buffer-name)
2698 'current-window
2699 org-agenda-window-setup))
2700 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2701 (org-agenda-custom-commands
2702 ;; normalize different versions
2703 (delq nil
2704 (mapcar
2705 (lambda (x)
2706 (cond ((stringp (cdr x))
2707 (push x prefix-descriptions)
2708 nil)
2709 ((stringp (nth 1 x)) x)
2710 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2711 (t (cons (car x) (cons "" (cdr x))))))
2712 org-agenda-custom-commands)))
2713 (org-agenda-custom-commands
2714 (org-contextualize-keys
2715 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2716 (buf (current-buffer))
2717 (bfn (buffer-file-name (buffer-base-buffer)))
2718 entry key type org-match lprops ans)
2719 ;; Turn off restriction unless there is an overriding one,
2720 (unless org-agenda-overriding-restriction
2721 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2722 ;; There is a request to keep the file list in place
2723 (put 'org-agenda-files 'org-restrict nil))
2724 (setq org-agenda-restrict nil)
2725 (move-marker org-agenda-restrict-begin nil)
2726 (move-marker org-agenda-restrict-end nil))
2727 ;; Delete old local properties
2728 (put 'org-agenda-redo-command 'org-lprops nil)
2729 ;; Delete previously set last-arguments
2730 (put 'org-agenda-redo-command 'last-args nil)
2731 ;; Remember where this call originated
2732 (setq org-agenda-last-dispatch-buffer (current-buffer))
2733 (unless org-keys
2734 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2735 org-keys (car ans)
2736 restriction (cdr ans)))
2737 ;; If we have sticky agenda buffers, set a name for the buffer,
2738 ;; depending on the invoking keys. The user may still set this
2739 ;; as a command option, which will overwrite what we do here.
2740 (if org-agenda-sticky
2741 (setq org-agenda-buffer-name
2742 (format "*Org Agenda(%s)*" org-keys)))
2743 ;; Establish the restriction, if any
2744 (when (and (not org-agenda-overriding-restriction) restriction)
2745 (put 'org-agenda-files 'org-restrict (list bfn))
2746 (cond
2747 ((eq restriction 'region)
2748 (setq org-agenda-restrict (current-buffer))
2749 (move-marker org-agenda-restrict-begin (region-beginning))
2750 (move-marker org-agenda-restrict-end (region-end)))
2751 ((eq restriction 'subtree)
2752 (save-excursion
2753 (setq org-agenda-restrict (current-buffer))
2754 (org-back-to-heading t)
2755 (move-marker org-agenda-restrict-begin (point))
2756 (move-marker org-agenda-restrict-end
2757 (progn (org-end-of-subtree t)))))))
2759 ;; For example the todo list should not need it (but does...)
2760 (cond
2761 ((setq entry (assoc org-keys org-agenda-custom-commands))
2762 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2763 (progn
2764 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2765 lprops (nth 4 entry))
2766 (if org-agenda-sticky
2767 (setq org-agenda-buffer-name
2768 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2769 (format "*Org Agenda(%s)*" org-keys))))
2770 (put 'org-agenda-redo-command 'org-lprops lprops)
2771 (cond
2772 ((eq type 'agenda)
2773 (org-let lprops '(org-agenda-list current-prefix-arg)))
2774 ((eq type 'agenda*)
2775 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2776 ((eq type 'alltodo)
2777 (org-let lprops '(org-todo-list current-prefix-arg)))
2778 ((eq type 'search)
2779 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2780 ((eq type 'stuck)
2781 (org-let lprops '(org-agenda-list-stuck-projects
2782 current-prefix-arg)))
2783 ((eq type 'tags)
2784 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2785 ((eq type 'tags-todo)
2786 (org-let lprops '(org-tags-view '(4) org-match)))
2787 ((eq type 'todo)
2788 (org-let lprops '(org-todo-list org-match)))
2789 ((eq type 'tags-tree)
2790 (org-check-for-org-mode)
2791 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2792 ((eq type 'todo-tree)
2793 (org-check-for-org-mode)
2794 (org-let lprops
2795 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2796 (regexp-quote org-match) "\\>"))))
2797 ((eq type 'occur-tree)
2798 (org-check-for-org-mode)
2799 (org-let lprops '(org-occur org-match)))
2800 ((functionp type)
2801 (org-let lprops '(funcall type org-match)))
2802 ((fboundp type)
2803 (org-let lprops '(funcall type org-match)))
2804 (t (user-error "Invalid custom agenda command type %s" type))))
2805 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2806 ((equal org-keys "C")
2807 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2808 (customize-variable 'org-agenda-custom-commands))
2809 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2810 ((equal org-keys "s") (call-interactively 'org-search-view))
2811 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2812 ((equal org-keys "t") (call-interactively 'org-todo-list))
2813 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2814 ((equal org-keys "m") (call-interactively 'org-tags-view))
2815 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2816 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2817 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2818 (org-add-hook
2819 'post-command-hook
2820 (lambda ()
2821 (unless (current-message)
2822 (let* ((m (org-agenda-get-any-marker))
2823 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2824 (when note
2825 (message (concat
2826 "FLAGGING-NOTE ([?] for more info): "
2827 (org-add-props
2828 (replace-regexp-in-string
2829 "\\\\n" "//"
2830 (copy-sequence note))
2831 nil 'face 'org-warning)))))))
2832 t t))
2833 ((equal org-keys "L")
2834 (unless (derived-mode-p 'org-mode)
2835 (user-error "This is not an Org-mode file"))
2836 (unless restriction
2837 (put 'org-agenda-files 'org-restrict (list bfn))
2838 (org-call-with-arg 'org-timeline arg)))
2839 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2840 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2841 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2842 (t (user-error "Invalid agenda key"))))))
2844 (defvar org-agenda-multi)
2846 (defun org-agenda-append-agenda ()
2847 "Append another agenda view to the current one.
2848 This function allows interactive building of block agendas.
2849 Agenda views are separated by `org-agenda-block-separator'."
2850 (interactive)
2851 (unless (derived-mode-p 'org-agenda-mode)
2852 (user-error "Can only append from within agenda buffer"))
2853 (let ((org-agenda-multi t))
2854 (org-agenda)
2855 (widen)
2856 (org-agenda-finalize)
2857 (setq buffer-read-only t)
2858 (org-agenda-fit-window-to-buffer)))
2860 (defun org-agenda-normalize-custom-commands (cmds)
2861 "Normalize custom commands CMDS."
2862 (delq nil
2863 (mapcar
2864 (lambda (x)
2865 (cond ((stringp (cdr x)) nil)
2866 ((stringp (nth 1 x)) x)
2867 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2868 (t (cons (car x) (cons "" (cdr x))))))
2869 cmds)))
2871 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2872 "The user interface for selecting an agenda command."
2873 (catch 'exit
2874 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2875 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2876 (region-p (org-region-active-p))
2877 (custom org-agenda-custom-commands)
2878 (selstring "")
2879 restriction second-time
2880 c entry key type match prefixes rmheader header-end custom1 desc
2881 line lines left right n n1)
2882 (save-window-excursion
2883 (delete-other-windows)
2884 (org-switch-to-buffer-other-window " *Agenda Commands*")
2885 (erase-buffer)
2886 (insert (eval-when-compile
2887 (let ((header
2888 "Press key for an agenda command: < Buffer, subtree/region restriction
2889 -------------------------------- > Remove restriction
2890 a Agenda for current week or day e Export agenda views
2891 t List of all TODO entries T Entries with special TODO kwd
2892 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2893 s Search for keywords S Like s, but only TODO entries
2894 L Timeline for current buffer # List stuck projects (!=configure)
2895 / Multi-occur C Configure custom agenda commands
2896 ? Find :FLAGGED: entries * Toggle sticky agenda views
2898 (start 0))
2899 (while (string-match
2900 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2901 header start)
2902 (setq start (match-end 0))
2903 (add-text-properties (match-beginning 2) (match-end 2)
2904 '(face bold) header))
2905 header)))
2906 (setq header-end (point-marker))
2907 (while t
2908 (setq custom1 custom)
2909 (when (eq rmheader t)
2910 (org-goto-line 1)
2911 (re-search-forward ":" nil t)
2912 (delete-region (match-end 0) (point-at-eol))
2913 (forward-char 1)
2914 (looking-at "-+")
2915 (delete-region (match-end 0) (point-at-eol))
2916 (move-marker header-end (match-end 0)))
2917 (goto-char header-end)
2918 (delete-region (point) (point-max))
2920 ;; Produce all the lines that describe custom commands and prefixes
2921 (setq lines nil)
2922 (while (setq entry (pop custom1))
2923 (setq key (car entry) desc (nth 1 entry)
2924 type (nth 2 entry)
2925 match (nth 3 entry))
2926 (if (> (length key) 1)
2927 (add-to-list 'prefixes (string-to-char key))
2928 (setq line
2929 (format
2930 "%-4s%-14s"
2931 (org-add-props (copy-sequence key)
2932 '(face bold))
2933 (cond
2934 ((string-match "\\S-" desc) desc)
2935 ((eq type 'agenda) "Agenda for current week or day")
2936 ((eq type 'agenda*) "Appointments for current week or day")
2937 ((eq type 'alltodo) "List of all TODO entries")
2938 ((eq type 'search) "Word search")
2939 ((eq type 'stuck) "List of stuck projects")
2940 ((eq type 'todo) "TODO keyword")
2941 ((eq type 'tags) "Tags query")
2942 ((eq type 'tags-todo) "Tags (TODO)")
2943 ((eq type 'tags-tree) "Tags tree")
2944 ((eq type 'todo-tree) "TODO kwd tree")
2945 ((eq type 'occur-tree) "Occur tree")
2946 ((functionp type) (if (symbolp type)
2947 (symbol-name type)
2948 "Lambda expression"))
2949 (t "???"))))
2950 (if org-agenda-menu-show-matcher
2951 (setq line
2952 (concat line ": "
2953 (cond
2954 ((stringp match)
2955 (setq match (copy-sequence match))
2956 (org-add-props match nil 'face 'org-warning))
2957 ((listp type)
2958 (format "set of %d commands" (length type))))))
2959 (if (org-string-nw-p match)
2960 (add-text-properties
2961 0 (length line) (list 'help-echo
2962 (concat "Matcher: " match)) line)))
2963 (push line lines)))
2964 (setq lines (nreverse lines))
2965 (when prefixes
2966 (mapc (lambda (x)
2967 (push
2968 (format "%s %s"
2969 (org-add-props (char-to-string x)
2970 nil 'face 'bold)
2971 (or (cdr (assoc (concat selstring
2972 (char-to-string x))
2973 prefix-descriptions))
2974 "Prefix key"))
2975 lines))
2976 prefixes))
2978 ;; Check if we should display in two columns
2979 (if org-agenda-menu-two-columns
2980 (progn
2981 (setq n (length lines)
2982 n1 (+ (/ n 2) (mod n 2))
2983 right (nthcdr n1 lines)
2984 left (copy-sequence lines))
2985 (setcdr (nthcdr (1- n1) left) nil))
2986 (setq left lines right nil))
2987 (while left
2988 (insert "\n" (pop left))
2989 (when right
2990 (if (< (current-column) 40)
2991 (move-to-column 40 t)
2992 (insert " "))
2993 (insert (pop right))))
2995 ;; Make the window the right size
2996 (goto-char (point-min))
2997 (if second-time
2998 (if (not (pos-visible-in-window-p (point-max)))
2999 (org-fit-window-to-buffer))
3000 (setq second-time t)
3001 (org-fit-window-to-buffer))
3003 ;; Ask for selection
3004 (message "Press key for agenda command%s:"
3005 (if (or restrict-ok org-agenda-overriding-restriction)
3006 (if org-agenda-overriding-restriction
3007 " (restriction lock active)"
3008 (if restriction
3009 (format " (restricted to %s)" restriction)
3010 " (unrestricted)"))
3011 ""))
3012 (setq c (read-char-exclusive))
3013 (message "")
3014 (cond
3015 ((assoc (char-to-string c) custom)
3016 (setq selstring (concat selstring (char-to-string c)))
3017 (throw 'exit (cons selstring restriction)))
3018 ((memq c prefixes)
3019 (setq selstring (concat selstring (char-to-string c))
3020 prefixes nil
3021 rmheader (or rmheader t)
3022 custom (delq nil (mapcar
3023 (lambda (x)
3024 (if (or (= (length (car x)) 1)
3025 (/= (string-to-char (car x)) c))
3027 (cons (substring (car x) 1) (cdr x))))
3028 custom))))
3029 ((eq c ?*)
3030 (call-interactively 'org-toggle-sticky-agenda)
3031 (sit-for 2))
3032 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3033 (message "Restriction is only possible in Org-mode buffers")
3034 (ding) (sit-for 1))
3035 ((eq c ?1)
3036 (org-agenda-remove-restriction-lock 'noupdate)
3037 (setq restriction 'buffer))
3038 ((eq c ?0)
3039 (org-agenda-remove-restriction-lock 'noupdate)
3040 (setq restriction (if region-p 'region 'subtree)))
3041 ((eq c ?<)
3042 (org-agenda-remove-restriction-lock 'noupdate)
3043 (setq restriction
3044 (cond
3045 ((eq restriction 'buffer)
3046 (if region-p 'region 'subtree))
3047 ((memq restriction '(subtree region))
3048 nil)
3049 (t 'buffer))))
3050 ((eq c ?>)
3051 (org-agenda-remove-restriction-lock 'noupdate)
3052 (setq restriction nil))
3053 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3054 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3055 ((and (> (length selstring) 0) (eq c ?\d))
3056 (delete-window)
3057 (org-agenda-get-restriction-and-command prefix-descriptions))
3059 ((equal c ?q) (error "Abort"))
3060 (t (user-error "Invalid key %c" c))))))))
3062 (defun org-agenda-fit-window-to-buffer ()
3063 "Fit the window to the buffer size."
3064 (and (memq org-agenda-window-setup '(reorganize-frame))
3065 (fboundp 'fit-window-to-buffer)
3066 (org-fit-window-to-buffer
3068 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3069 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3071 (defvar org-cmd nil)
3072 (defvar org-agenda-overriding-cmd nil)
3073 (defvar org-agenda-overriding-arguments nil)
3074 (defvar org-agenda-overriding-cmd-arguments nil)
3075 (defun org-agenda-run-series (name series)
3076 "Run agenda NAME as a SERIES of agenda commands."
3077 (org-let (nth 1 series) '(org-agenda-prepare name))
3078 ;; We need to reset agenda markers here, because when constructing a
3079 ;; block agenda, the individual blocks do not do that.
3080 (org-agenda-reset-markers)
3081 (let* ((org-agenda-multi t)
3082 (redo (list 'org-agenda-run-series name (list 'quote series)))
3083 (cmds (car series))
3084 (gprops (nth 1 series))
3085 match ;; The byte compiler incorrectly complains about this. Keep it!
3086 org-cmd type lprops)
3087 (while (setq org-cmd (pop cmds))
3088 (setq type (car org-cmd)
3089 match (eval (nth 1 org-cmd))
3090 lprops (nth 2 org-cmd))
3091 (let ((org-agenda-overriding-arguments
3092 (if (eq org-agenda-overriding-cmd org-cmd)
3093 (or org-agenda-overriding-arguments
3094 org-agenda-overriding-cmd-arguments))))
3095 (cond
3096 ((eq type 'agenda)
3097 (org-let2 gprops lprops
3098 '(call-interactively 'org-agenda-list)))
3099 ((eq type 'agenda*)
3100 (org-let2 gprops lprops
3101 '(funcall 'org-agenda-list nil nil t)))
3102 ((eq type 'alltodo)
3103 (org-let2 gprops lprops
3104 '(call-interactively 'org-todo-list)))
3105 ((eq type 'search)
3106 (org-let2 gprops lprops
3107 '(org-search-view current-prefix-arg match nil)))
3108 ((eq type 'stuck)
3109 (org-let2 gprops lprops
3110 '(call-interactively 'org-agenda-list-stuck-projects)))
3111 ((eq type 'tags)
3112 (org-let2 gprops lprops
3113 '(org-tags-view current-prefix-arg match)))
3114 ((eq type 'tags-todo)
3115 (org-let2 gprops lprops
3116 '(org-tags-view '(4) match)))
3117 ((eq type 'todo)
3118 (org-let2 gprops lprops
3119 '(org-todo-list match)))
3120 ((fboundp type)
3121 (org-let2 gprops lprops
3122 '(funcall type match)))
3123 (t (error "Invalid type in command series")))))
3124 (widen)
3125 (let ((inhibit-read-only t))
3126 (add-text-properties (point-min) (point-max)
3127 `(org-series t org-series-redo-cmd ,redo)))
3128 (setq org-agenda-redo-command redo)
3129 (goto-char (point-min)))
3130 (org-agenda-fit-window-to-buffer)
3131 (org-let (nth 1 series) '(org-agenda-finalize)))
3133 ;;;###autoload
3134 (defmacro org-batch-agenda (cmd-key &rest parameters)
3135 "Run an agenda command in batch mode and send the result to STDOUT.
3136 If CMD-KEY is a string of length 1, it is used as a key in
3137 `org-agenda-custom-commands' and triggers this command. If it is a
3138 longer string it is used as a tags/todo match string.
3139 Parameters are alternating variable names and values that will be bound
3140 before running the agenda command."
3141 (org-eval-in-environment (org-make-parameter-alist parameters)
3142 (let (org-agenda-sticky)
3143 (if (> (length cmd-key) 2)
3144 (org-tags-view nil cmd-key)
3145 (org-agenda nil cmd-key))))
3146 (set-buffer org-agenda-buffer-name)
3147 (princ (buffer-string)))
3149 (defvar org-agenda-info nil)
3151 ;;;###autoload
3152 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3153 "Run an agenda command in batch mode and send the result to STDOUT.
3154 If CMD-KEY is a string of length 1, it is used as a key in
3155 `org-agenda-custom-commands' and triggers this command. If it is a
3156 longer string it is used as a tags/todo match string.
3157 Parameters are alternating variable names and values that will be bound
3158 before running the agenda command.
3160 The output gives a line for each selected agenda item. Each
3161 item is a list of comma-separated values, like this:
3163 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3165 category The category of the item
3166 head The headline, without TODO kwd, TAGS and PRIORITY
3167 type The type of the agenda entry, can be
3168 todo selected in TODO match
3169 tagsmatch selected in tags match
3170 diary imported from diary
3171 deadline a deadline on given date
3172 scheduled scheduled on given date
3173 timestamp entry has timestamp on given date
3174 closed entry was closed on given date
3175 upcoming-deadline warning about deadline
3176 past-scheduled forwarded scheduled item
3177 block entry has date block including g. date
3178 todo The todo keyword, if any
3179 tags All tags including inherited ones, separated by colons
3180 date The relevant date, like 2007-2-14
3181 time The time, like 15:00-16:50
3182 extra Sting with extra planning info
3183 priority-l The priority letter if any was given
3184 priority-n The computed numerical priority
3185 agenda-day The day in the agenda where this is listed"
3186 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3187 (org-make-parameter-alist parameters))
3188 (if (> (length cmd-key) 2)
3189 (org-tags-view nil cmd-key)
3190 (org-agenda nil cmd-key)))
3191 (set-buffer org-agenda-buffer-name)
3192 (let* ((lines (org-split-string (buffer-string) "\n"))
3193 line)
3194 (while (setq line (pop lines))
3195 (catch 'next
3196 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3197 (setq org-agenda-info
3198 (org-fix-agenda-info (text-properties-at 0 line)))
3199 (princ
3200 (mapconcat 'org-agenda-export-csv-mapper
3201 '(org-category txt type todo tags date time extra
3202 priority-letter priority agenda-day)
3203 ","))
3204 (princ "\n")))))
3206 (defun org-fix-agenda-info (props)
3207 "Make sure all properties on an agenda item have a canonical form.
3208 This ensures the export commands can easily use it."
3209 (let (tmp re)
3210 (when (setq tmp (plist-get props 'tags))
3211 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3212 (when (setq tmp (plist-get props 'date))
3213 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3214 (let ((calendar-date-display-form '(year "-" month "-" day)))
3215 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3217 (setq tmp (calendar-date-string tmp)))
3218 (setq props (plist-put props 'date tmp)))
3219 (when (setq tmp (plist-get props 'day))
3220 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3221 (let ((calendar-date-display-form '(year "-" month "-" day)))
3222 (setq tmp (calendar-date-string tmp)))
3223 (setq props (plist-put props 'day tmp))
3224 (setq props (plist-put props 'agenda-day tmp)))
3225 (when (setq tmp (plist-get props 'txt))
3226 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3227 (plist-put props 'priority-letter (match-string 1 tmp))
3228 (setq tmp (replace-match "" t t tmp)))
3229 (when (and (setq re (plist-get props 'org-todo-regexp))
3230 (setq re (concat "\\`\\.*" re " ?"))
3231 (string-match re tmp))
3232 (plist-put props 'todo (match-string 1 tmp))
3233 (setq tmp (replace-match "" t t tmp)))
3234 (plist-put props 'txt tmp)))
3235 props)
3237 (defun org-agenda-export-csv-mapper (prop)
3238 (let ((res (plist-get org-agenda-info prop)))
3239 (setq res
3240 (cond
3241 ((not res) "")
3242 ((stringp res) res)
3243 (t (prin1-to-string res))))
3244 (while (string-match "," res)
3245 (setq res (replace-match ";" t t res)))
3246 (org-trim res)))
3248 ;;;###autoload
3249 (defun org-store-agenda-views (&rest parameters)
3250 "Store agenda views."
3251 (interactive)
3252 (eval (list 'org-batch-store-agenda-views)))
3254 ;;;###autoload
3255 (defmacro org-batch-store-agenda-views (&rest parameters)
3256 "Run all custom agenda commands that have a file argument."
3257 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3258 (pop-up-frames nil)
3259 (dir default-directory)
3260 (pars (org-make-parameter-alist parameters))
3261 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3262 (save-window-excursion
3263 (while cmds
3264 (setq cmd (pop cmds)
3265 thiscmdkey (car cmd)
3266 thiscmdcmd (cdr cmd)
3267 match (nth 2 thiscmdcmd)
3268 bufname (if org-agenda-sticky
3269 (or (and (stringp match)
3270 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3271 (format "*Org Agenda(%s)*" thiscmdkey))
3272 org-agenda-buffer-name)
3273 cmd-or-set (nth 2 cmd)
3274 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3275 files (nth (if (listp cmd-or-set) 4 5) cmd))
3276 (if (stringp files) (setq files (list files)))
3277 (when files
3278 (org-eval-in-environment (append org-agenda-exporter-settings
3279 opts pars)
3280 (org-agenda nil thiscmdkey))
3281 (set-buffer bufname)
3282 (while files
3283 (org-eval-in-environment (append org-agenda-exporter-settings
3284 opts pars)
3285 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3286 (and (get-buffer bufname)
3287 (kill-buffer bufname)))))))
3289 (defvar org-agenda-current-span nil
3290 "The current span used in the agenda view.") ; local variable in the agenda buffer
3291 (defun org-agenda-mark-header-line (pos)
3292 "Mark the line at POS as an agenda structure header."
3293 (save-excursion
3294 (goto-char pos)
3295 (put-text-property (point-at-bol) (point-at-eol)
3296 'org-agenda-structural-header t)
3297 (when org-agenda-title-append
3298 (put-text-property (point-at-bol) (point-at-eol)
3299 'org-agenda-title-append org-agenda-title-append))))
3301 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3302 (defvar org-agenda-write-buffer-name "Agenda View")
3303 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3304 "Write the current buffer (an agenda view) as a file.
3305 Depending on the extension of the file name, plain text (.txt),
3306 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3307 If the extension is .ics, run icalendar export over all files used
3308 to construct the agenda and limit the export to entries listed in the
3309 agenda now.
3310 If the extension is .org, collect all subtrees corresponding to the
3311 agenda entries and add them in an .org file.
3312 With prefix argument OPEN, open the new file immediately.
3313 If NOSETTINGS is given, do not scope the settings of
3314 `org-agenda-exporter-settings' into the export commands. This is used when
3315 the settings have already been scoped and we do not wish to overrule other,
3316 higher priority settings.
3317 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3318 (interactive "FWrite agenda to file: \nP")
3319 (if (or (not (file-writable-p file))
3320 (and (file-exists-p file)
3321 (if (org-called-interactively-p 'any)
3322 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3323 (user-error "Cannot write agenda to file %s" file))
3324 (org-let (if nosettings nil org-agenda-exporter-settings)
3325 '(save-excursion
3326 (save-window-excursion
3327 (org-agenda-mark-filtered-text)
3328 (let ((bs (copy-sequence (buffer-string))) beg content)
3329 (org-agenda-unmark-filtered-text)
3330 (with-temp-buffer
3331 (rename-buffer org-agenda-write-buffer-name t)
3332 (set-buffer-modified-p nil)
3333 (insert bs)
3334 (org-agenda-remove-marked-text 'org-filtered)
3335 (while (setq beg (text-property-any (point-min) (point-max)
3336 'org-filtered t))
3337 (delete-region
3338 beg (or (next-single-property-change beg 'org-filtered)
3339 (point-max))))
3340 (run-hooks 'org-agenda-before-write-hook)
3341 (cond
3342 ((org-bound-and-true-p org-mobile-creating-agendas)
3343 (org-mobile-write-agenda-for-mobile file))
3344 ((string-match "\\.org\\'" file)
3345 (let (content p m message-log-max)
3346 (goto-char (point-min))
3347 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3348 (goto-char p)
3349 (setq m (get-text-property (point) 'org-hd-marker))
3350 (when m
3351 (push (save-excursion
3352 (set-buffer (marker-buffer m))
3353 (goto-char m)
3354 (org-copy-subtree 1 nil t t)
3355 org-subtree-clip)
3356 content)))
3357 (find-file file)
3358 (erase-buffer)
3359 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3360 (write-file file)
3361 (kill-buffer (current-buffer))
3362 (message "Org file written to %s" file)))
3363 ((string-match "\\.html?\\'" file)
3364 (require 'htmlize)
3365 (set-buffer (htmlize-buffer (current-buffer)))
3366 (when org-agenda-export-html-style
3367 ;; replace <style> section with org-agenda-export-html-style
3368 (goto-char (point-min))
3369 (kill-region (- (search-forward "<style") 6)
3370 (search-forward "</style>"))
3371 (insert org-agenda-export-html-style))
3372 (write-file file)
3373 (kill-buffer (current-buffer))
3374 (message "HTML written to %s" file))
3375 ((string-match "\\.ps\\'" file)
3376 (require 'ps-print)
3377 (ps-print-buffer-with-faces file)
3378 (message "Postscript written to %s" file))
3379 ((string-match "\\.pdf\\'" file)
3380 (require 'ps-print)
3381 (ps-print-buffer-with-faces
3382 (concat (file-name-sans-extension file) ".ps"))
3383 (call-process "ps2pdf" nil nil nil
3384 (expand-file-name
3385 (concat (file-name-sans-extension file) ".ps"))
3386 (expand-file-name file))
3387 (delete-file (concat (file-name-sans-extension file) ".ps"))
3388 (message "PDF written to %s" file))
3389 ((string-match "\\.ics\\'" file)
3390 (require 'ox-icalendar)
3391 (org-icalendar-export-current-agenda (expand-file-name file)))
3393 (let ((bs (buffer-string)))
3394 (find-file file)
3395 (erase-buffer)
3396 (insert bs)
3397 (save-buffer 0)
3398 (kill-buffer (current-buffer))
3399 (message "Plain text written to %s" file))))))))
3400 (set-buffer (or agenda-bufname
3401 (and (org-called-interactively-p 'any) (buffer-name))
3402 org-agenda-buffer-name)))
3403 (when open (org-open-file file)))
3405 (defvar org-agenda-tag-filter-overlays nil)
3406 (defvar org-agenda-cat-filter-overlays nil)
3407 (defvar org-agenda-re-filter-overlays nil)
3409 (defun org-agenda-mark-filtered-text ()
3410 "Mark all text hidden by filtering with a text property."
3411 (let ((inhibit-read-only t))
3412 (mapc
3413 (lambda (o)
3414 (when (equal (overlay-buffer o) (current-buffer))
3415 (put-text-property
3416 (overlay-start o) (overlay-end o)
3417 'org-filtered t)))
3418 (append org-agenda-tag-filter-overlays
3419 org-agenda-cat-filter-overlays
3420 org-agenda-re-filter-overlays))))
3422 (defun org-agenda-unmark-filtered-text ()
3423 "Remove the filtering text property."
3424 (let ((inhibit-read-only t))
3425 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3427 (defun org-agenda-remove-marked-text (property &optional value)
3428 "Delete all text marked with VALUE of PROPERTY.
3429 VALUE defaults to t."
3430 (let (beg)
3431 (setq value (or value t))
3432 (while (setq beg (text-property-any (point-min) (point-max)
3433 property value))
3434 (delete-region
3435 beg (or (next-single-property-change beg 'org-filtered)
3436 (point-max))))))
3438 (defun org-agenda-add-entry-text ()
3439 "Add entry text to agenda lines.
3440 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3441 entry text following headings shown in the agenda.
3442 Drawers will be excluded, also the line with scheduling/deadline info."
3443 (when (and (> org-agenda-add-entry-text-maxlines 0)
3444 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3445 (let (m txt)
3446 (goto-char (point-min))
3447 (while (not (eobp))
3448 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3449 (beginning-of-line 2)
3450 (setq txt (org-agenda-get-some-entry-text
3451 m org-agenda-add-entry-text-maxlines " > "))
3452 (end-of-line 1)
3453 (if (string-match "\\S-" txt)
3454 (insert "\n" txt)
3455 (or (eobp) (forward-char 1))))))))
3457 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3458 &rest keep)
3459 "Extract entry text from MARKER, at most N-LINES lines.
3460 This will ignore drawers etc, just get the text.
3461 If INDENT is given, prefix every line with this string. If KEEP is
3462 given, it is a list of symbols, defining stuff that should not be
3463 removed from the entry content. Currently only `planning' is allowed here."
3464 (let (txt drawer-re kwd-time-re ind)
3465 (save-excursion
3466 (with-current-buffer (marker-buffer marker)
3467 (if (not (derived-mode-p 'org-mode))
3468 (setq txt "")
3469 (save-excursion
3470 (save-restriction
3471 (widen)
3472 (goto-char marker)
3473 (end-of-line 1)
3474 (setq txt (buffer-substring
3475 (min (1+ (point)) (point-max))
3476 (progn (outline-next-heading) (point)))
3477 drawer-re org-drawer-regexp
3478 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3479 ".*\n?"))
3480 (with-temp-buffer
3481 (insert txt)
3482 (when org-agenda-add-entry-text-descriptive-links
3483 (goto-char (point-min))
3484 (while (org-activate-bracket-links (point-max))
3485 (add-text-properties (match-beginning 0) (match-end 0)
3486 '(face org-link))))
3487 (goto-char (point-min))
3488 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3489 (set-text-properties (match-beginning 0) (match-end 0)
3490 nil))
3491 (goto-char (point-min))
3492 (while (re-search-forward drawer-re nil t)
3493 (delete-region
3494 (match-beginning 0)
3495 (progn (re-search-forward
3496 "^[ \t]*:END:.*\n?" nil 'move)
3497 (point))))
3498 (unless (member 'planning keep)
3499 (goto-char (point-min))
3500 (while (re-search-forward kwd-time-re nil t)
3501 (replace-match "")))
3502 (goto-char (point-min))
3503 (when org-agenda-entry-text-exclude-regexps
3504 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3505 (while (setq re (pop re-list))
3506 (goto-char (point-min))
3507 (while (re-search-forward re nil t)
3508 (replace-match "")))))
3509 (goto-char (point-max))
3510 (skip-chars-backward " \t\n")
3511 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3513 ;; find and remove min common indentation
3514 (goto-char (point-min))
3515 (untabify (point-min) (point-max))
3516 (setq ind (org-get-indentation))
3517 (while (not (eobp))
3518 (unless (looking-at "[ \t]*$")
3519 (setq ind (min ind (org-get-indentation))))
3520 (beginning-of-line 2))
3521 (goto-char (point-min))
3522 (while (not (eobp))
3523 (unless (looking-at "[ \t]*$")
3524 (move-to-column ind)
3525 (delete-region (point-at-bol) (point)))
3526 (beginning-of-line 2))
3528 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3530 (goto-char (point-min))
3531 (when indent
3532 (while (and (not (eobp)) (re-search-forward "^" nil t))
3533 (replace-match indent t t)))
3534 (goto-char (point-min))
3535 (while (looking-at "[ \t]*\n") (replace-match ""))
3536 (goto-char (point-max))
3537 (when (> (org-current-line)
3538 n-lines)
3539 (org-goto-line (1+ n-lines))
3540 (backward-char 1))
3541 (setq txt (buffer-substring (point-min) (point)))))))))
3542 txt))
3544 (defun org-check-for-org-mode ()
3545 "Make sure current buffer is in org-mode. Error if not."
3546 (or (derived-mode-p 'org-mode)
3547 (error "Cannot execute org-mode agenda command on buffer in %s"
3548 major-mode)))
3550 ;;; Agenda prepare and finalize
3552 (defvar org-agenda-multi nil) ; dynamically scoped
3553 (defvar org-agenda-pre-window-conf nil)
3554 (defvar org-agenda-columns-active nil)
3555 (defvar org-agenda-name nil)
3556 (defvar org-agenda-tag-filter nil)
3557 (defvar org-agenda-category-filter nil)
3558 (defvar org-agenda-regexp-filter nil)
3559 (defvar org-agenda-top-headline-filter nil)
3560 (defvar org-agenda-tag-filter-while-redo nil)
3561 (defvar org-agenda-tag-filter-preset nil
3562 "A preset of the tags filter used for secondary agenda filtering.
3563 This must be a list of strings, each string must be a single tag preceded
3564 by \"+\" or \"-\".
3565 This variable should not be set directly, but agenda custom commands can
3566 bind it in the options section. The preset filter is a global property of
3567 the entire agenda view. In a block agenda, it will not work reliably to
3568 define a filter for one of the individual blocks. You need to set it in
3569 the global options and expect it to be applied to the entire view.")
3571 (defvar org-agenda-category-filter-preset nil
3572 "A preset of the category filter used for secondary agenda filtering.
3573 This must be a list of strings, each string must be a single category
3574 preceded by \"+\" or \"-\".
3575 This variable should not be set directly, but agenda custom commands can
3576 bind it in the options section. The preset filter is a global property of
3577 the entire agenda view. In a block agenda, it will not work reliably to
3578 define a filter for one of the individual blocks. You need to set it in
3579 the global options and expect it to be applied to the entire view.")
3581 (defvar org-agenda-regexp-filter-preset nil
3582 "A preset of the regexp filter used for secondary agenda filtering.
3583 This must be a list of strings, each string must be a single regexp
3584 preceded by \"+\" or \"-\".
3585 This variable should not be set directly, but agenda custom commands can
3586 bind it in the options section. The preset filter is a global property of
3587 the entire agenda view. In a block agenda, it will not work reliably to
3588 define a filter for one of the individual blocks. You need to set it in
3589 the global options and expect it to be applied to the entire view.")
3591 (defun org-agenda-use-sticky-p ()
3592 "Return non-nil if an agenda buffer named
3593 `org-agenda-buffer-name' exists and should be shown instead of
3594 generating a new one."
3595 (and
3596 ;; turned off by user
3597 org-agenda-sticky
3598 ;; For multi-agenda buffer already exists
3599 (not org-agenda-multi)
3600 ;; buffer found
3601 (get-buffer org-agenda-buffer-name)
3602 ;; C-u parameter is same as last call
3603 (with-current-buffer (get-buffer org-agenda-buffer-name)
3604 (and
3605 (equal current-prefix-arg
3606 org-agenda-last-prefix-arg)
3607 ;; In case user turned stickiness on, while having existing
3608 ;; Agenda buffer active, don't reuse that buffer, because it
3609 ;; does not have org variables local
3610 org-agenda-this-buffer-is-sticky))))
3612 (defun org-agenda-prepare-window (abuf)
3613 "Setup agenda buffer in the window."
3614 (let* ((awin (get-buffer-window abuf))
3615 wconf)
3616 (cond
3617 ((equal (current-buffer) abuf) nil)
3618 (awin (select-window awin))
3619 ((not (setq wconf (current-window-configuration))))
3620 ((equal org-agenda-window-setup 'current-window)
3621 (org-pop-to-buffer-same-window abuf))
3622 ((equal org-agenda-window-setup 'other-window)
3623 (org-switch-to-buffer-other-window abuf))
3624 ((equal org-agenda-window-setup 'other-frame)
3625 (switch-to-buffer-other-frame abuf))
3626 ((equal org-agenda-window-setup 'reorganize-frame)
3627 (delete-other-windows)
3628 (org-switch-to-buffer-other-window abuf)))
3629 ;; additional test in case agenda is invoked from within agenda
3630 ;; buffer via elisp link
3631 (unless (equal (current-buffer) abuf)
3632 (org-pop-to-buffer-same-window abuf))
3633 (setq org-agenda-pre-window-conf
3634 (or org-agenda-pre-window-conf wconf))))
3636 (defun org-agenda-prepare (&optional name)
3637 (if (org-agenda-use-sticky-p)
3638 (progn
3639 ;; Popup existing buffer
3640 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3641 (message "Sticky Agenda buffer, use `r' to refresh")
3642 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3643 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3644 (setq org-todo-keywords-for-agenda nil)
3645 (setq org-drawers-for-agenda nil)
3646 (unless org-agenda-persistent-filter
3647 (setq org-agenda-tag-filter nil
3648 org-agenda-category-filter nil
3649 org-agenda-regexp-filter nil))
3650 (put 'org-agenda-tag-filter :preset-filter
3651 org-agenda-tag-filter-preset)
3652 (put 'org-agenda-category-filter :preset-filter
3653 org-agenda-category-filter-preset)
3654 (put 'org-agenda-regexp-filter :preset-filter
3655 org-agenda-regexp-filter-preset)
3656 (if org-agenda-multi
3657 (progn
3658 (setq buffer-read-only nil)
3659 (goto-char (point-max))
3660 (unless (or (bobp) org-agenda-compact-blocks
3661 (not org-agenda-block-separator))
3662 (insert "\n"
3663 (if (stringp org-agenda-block-separator)
3664 org-agenda-block-separator
3665 (make-string (window-width) org-agenda-block-separator))
3666 "\n"))
3667 (narrow-to-region (point) (point-max)))
3668 (setq org-done-keywords-for-agenda nil)
3670 ;; Setting any org variables that are in org-agenda-local-vars
3671 ;; list need to be done after the prepare call
3672 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3673 (setq buffer-read-only nil)
3674 (org-agenda-reset-markers)
3675 (let ((inhibit-read-only t)) (erase-buffer))
3676 (org-agenda-mode)
3677 (setq org-agenda-buffer (current-buffer))
3678 (setq org-agenda-contributing-files nil)
3679 (setq org-agenda-columns-active nil)
3680 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3681 (setq org-todo-keywords-for-agenda
3682 (org-uniquify org-todo-keywords-for-agenda))
3683 (setq org-done-keywords-for-agenda
3684 (org-uniquify org-done-keywords-for-agenda))
3685 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3686 (setq org-agenda-last-prefix-arg current-prefix-arg)
3687 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3688 (and name (not org-agenda-name)
3689 (org-set-local 'org-agenda-name name)))
3690 (setq buffer-read-only nil)))
3692 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3693 (defun org-agenda-finalize ()
3694 "Finishing touch for the agenda buffer, called just before displaying it."
3695 (unless org-agenda-multi
3696 (save-excursion
3697 (let ((inhibit-read-only t))
3698 (goto-char (point-min))
3699 (save-excursion
3700 (while (org-activate-bracket-links (point-max))
3701 (add-text-properties (match-beginning 0) (match-end 0)
3702 '(face org-link))))
3703 (save-excursion
3704 (while (org-activate-plain-links (point-max))
3705 (add-text-properties (match-beginning 0) (match-end 0)
3706 '(face org-link))))
3707 (unless (eq org-agenda-remove-tags t)
3708 (org-agenda-align-tags))
3709 (unless org-agenda-with-colors
3710 (remove-text-properties (point-min) (point-max) '(face nil)))
3711 (if (and (boundp 'org-agenda-overriding-columns-format)
3712 org-agenda-overriding-columns-format)
3713 (org-set-local 'org-agenda-overriding-columns-format
3714 org-agenda-overriding-columns-format))
3715 (if (and (boundp 'org-agenda-view-columns-initially)
3716 org-agenda-view-columns-initially)
3717 (org-agenda-columns))
3718 (when org-agenda-fontify-priorities
3719 (org-agenda-fontify-priorities))
3720 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3721 (org-agenda-dim-blocked-tasks))
3722 (org-agenda-mark-clocking-task)
3723 (when org-agenda-entry-text-mode
3724 (org-agenda-entry-text-hide)
3725 (org-agenda-entry-text-show))
3726 (if (and (functionp 'org-habit-insert-consistency-graphs)
3727 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3728 (org-habit-insert-consistency-graphs))
3729 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3730 (unless (or (eq org-agenda-show-inherited-tags 'always)
3731 (and (listp org-agenda-show-inherited-tags)
3732 (memq org-agenda-type org-agenda-show-inherited-tags))
3733 (and (eq org-agenda-show-inherited-tags t)
3734 (or (eq org-agenda-use-tag-inheritance t)
3735 (and (listp org-agenda-use-tag-inheritance)
3736 (not (memq org-agenda-type
3737 org-agenda-use-tag-inheritance))))))
3738 (let (mrk)
3739 (save-excursion
3740 (goto-char (point-min))
3741 (while (equal (forward-line) 0)
3742 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3743 (get-text-property (point) 'org-hd-marker)))
3744 (put-text-property (point-at-bol) (point-at-eol)
3745 'tags (org-with-point-at mrk
3746 (delete-dups
3747 (mapcar 'downcase (org-get-tags-at))))))))))
3748 (run-hooks 'org-agenda-finalize-hook)
3749 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3750 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3751 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3752 (org-agenda-filter-apply org-agenda-category-filter 'category))
3753 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3754 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3755 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3757 (defun org-agenda-mark-clocking-task ()
3758 "Mark the current clock entry in the agenda if it is present."
3759 ;; We need to widen when `org-agenda-finalize' is called from
3760 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3761 (when org-clock-current-task
3762 (save-restriction
3763 (widen)
3764 (org-agenda-unmark-clocking-task)
3765 (when (marker-buffer org-clock-hd-marker)
3766 (save-excursion
3767 (goto-char (point-min))
3768 (let (s ov)
3769 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3770 (goto-char s)
3771 (when (equal (org-get-at-bol 'org-hd-marker)
3772 org-clock-hd-marker)
3773 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3774 (overlay-put ov 'type 'org-agenda-clocking)
3775 (overlay-put ov 'face 'org-agenda-clocking)
3776 (overlay-put ov 'help-echo
3777 "The clock is running in this item")))))))))
3779 (defun org-agenda-unmark-clocking-task ()
3780 "Unmark the current clocking task."
3781 (mapc (lambda (o)
3782 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3783 (delete-overlay o)))
3784 (overlays-in (point-min) (point-max))))
3786 (defun org-agenda-fontify-priorities ()
3787 "Make highest priority lines bold, and lowest italic."
3788 (interactive)
3789 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3790 (delete-overlay o)))
3791 (overlays-in (point-min) (point-max)))
3792 (save-excursion
3793 (let (b e p ov h l)
3794 (goto-char (point-min))
3795 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3796 (setq h (or (get-char-property (point) 'org-highest-priority)
3797 org-highest-priority)
3798 l (or (get-char-property (point) 'org-lowest-priority)
3799 org-lowest-priority)
3800 p (string-to-char (match-string 1))
3801 b (match-beginning 0)
3802 e (if (eq org-agenda-fontify-priorities 'cookies)
3803 (match-end 0)
3804 (point-at-eol))
3805 ov (make-overlay b e))
3806 (overlay-put
3807 ov 'face
3808 (cons (cond ((org-face-from-face-or-color
3809 'priority nil
3810 (cdr (assoc p org-priority-faces))))
3811 ((and (listp org-agenda-fontify-priorities)
3812 (org-face-from-face-or-color
3813 'priority nil
3814 (cdr (assoc p org-agenda-fontify-priorities)))))
3815 ((equal p l) 'italic)
3816 ((equal p h) 'bold))
3817 'org-priority))
3818 (overlay-put ov 'org-type 'org-priority)))))
3820 (defvar org-depend-tag-blocked)
3822 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3823 "Dim currently blocked TODO's in the agenda display.
3824 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3825 dimming them."
3826 (interactive "P")
3827 (when (org-called-interactively-p 'interactive)
3828 (message "Dim or hide blocked tasks..."))
3829 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3830 (delete-overlay o)))
3831 (overlays-in (point-min) (point-max)))
3832 (save-excursion
3833 (let ((inhibit-read-only t)
3834 (org-depend-tag-blocked nil)
3835 (invis (or (not (null invisible))
3836 (eq org-agenda-dim-blocked-tasks 'invisible)))
3837 org-blocked-by-checkboxes
3838 invis1 b e p ov h l)
3839 (goto-char (point-min))
3840 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3841 (and pos (goto-char (1+ pos))))
3842 (setq org-blocked-by-checkboxes nil invis1 invis)
3843 (let ((marker (org-get-at-bol 'org-hd-marker)))
3844 (when (and marker
3845 (with-current-buffer (marker-buffer marker)
3846 (save-excursion (goto-char marker)
3847 (org-entry-blocked-p))))
3848 (if org-blocked-by-checkboxes (setq invis1 nil))
3849 (setq b (if invis1
3850 (max (point-min) (1- (point-at-bol)))
3851 (point-at-bol))
3852 e (point-at-eol)
3853 ov (make-overlay b e))
3854 (if invis1
3855 (progn (overlay-put ov 'invisible t)
3856 (overlay-put ov 'intangible t))
3857 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3858 (overlay-put ov 'org-type 'org-blocked-todo))))))
3859 (when (org-called-interactively-p 'interactive)
3860 (message "Dim or hide blocked tasks...done")))
3862 (defvar org-agenda-skip-function nil
3863 "Function to be called at each match during agenda construction.
3864 If this function returns nil, the current match should not be skipped.
3865 Otherwise, the function must return a position from where the search
3866 should be continued.
3867 This may also be a Lisp form, it will be evaluated.
3868 Never set this variable using `setq' or so, because then it will apply
3869 to all future agenda commands. If you do want a global skipping condition,
3870 use the option `org-agenda-skip-function-global' instead.
3871 The correct usage for `org-agenda-skip-function' is to bind it with
3872 `let' to scope it dynamically into the agenda-constructing command.
3873 A good way to set it is through options in `org-agenda-custom-commands'.")
3875 (defun org-agenda-skip ()
3876 "Throw to `:skip' in places that should be skipped.
3877 Also moves point to the end of the skipped region, so that search can
3878 continue from there."
3879 (let ((p (point-at-bol)) to)
3880 (when (or
3881 (save-excursion (goto-char p) (looking-at comment-start-skip))
3882 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3883 (get-text-property p :org-archived)
3884 (org-end-of-subtree t))
3885 (and org-agenda-skip-comment-trees
3886 (get-text-property p :org-comment)
3887 (org-end-of-subtree t))
3888 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3889 (org-agenda-skip-eval org-agenda-skip-function)))
3890 (goto-char to))
3891 (org-in-src-block-p t))
3892 (throw :skip t))))
3894 (defun org-agenda-skip-eval (form)
3895 "If FORM is a function or a list, call (or eval) it and return the result.
3896 `save-excursion' and `save-match-data' are wrapped around the call, so point
3897 and match data are returned to the previous state no matter what these
3898 functions do."
3899 (let (fp)
3900 (and form
3901 (or (setq fp (functionp form))
3902 (consp form))
3903 (save-excursion
3904 (save-match-data
3905 (if fp
3906 (funcall form)
3907 (eval form)))))))
3909 (defvar org-agenda-markers nil
3910 "List of all currently active markers created by `org-agenda'.")
3911 (defvar org-agenda-last-marker-time (org-float-time)
3912 "Creation time of the last agenda marker.")
3914 (defun org-agenda-new-marker (&optional pos)
3915 "Return a new agenda marker.
3916 Org-mode keeps a list of these markers and resets them when they are
3917 no longer in use."
3918 (let ((m (copy-marker (or pos (point)))))
3919 (setq org-agenda-last-marker-time (org-float-time))
3920 (if org-agenda-buffer
3921 (with-current-buffer org-agenda-buffer
3922 (push m org-agenda-markers))
3923 (push m org-agenda-markers))
3926 (defun org-agenda-reset-markers ()
3927 "Reset markers created by `org-agenda'."
3928 (while org-agenda-markers
3929 (move-marker (pop org-agenda-markers) nil)))
3931 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3932 "Save relative positions of markers in region.
3933 This check for agenda markers in all agenda buffers currently active."
3934 (dolist (buf (buffer-list))
3935 (with-current-buffer buf
3936 (when (eq major-mode 'org-agenda-mode)
3937 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3938 org-agenda-markers)))))
3940 ;;; Entry text mode
3942 (defun org-agenda-entry-text-show-here ()
3943 "Add some text from the entry as context to the current line."
3944 (let (m txt o)
3945 (setq m (org-get-at-bol 'org-hd-marker))
3946 (unless (marker-buffer m)
3947 (error "No marker points to an entry here"))
3948 (setq txt (concat "\n" (org-no-properties
3949 (org-agenda-get-some-entry-text
3950 m org-agenda-entry-text-maxlines
3951 org-agenda-entry-text-leaders))))
3952 (when (string-match "\\S-" txt)
3953 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3954 (overlay-put o 'evaporate t)
3955 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3956 (overlay-put o 'after-string txt))))
3958 (defun org-agenda-entry-text-show ()
3959 "Add entry context for all agenda lines."
3960 (interactive)
3961 (save-excursion
3962 (goto-char (point-max))
3963 (beginning-of-line 1)
3964 (while (not (bobp))
3965 (when (org-get-at-bol 'org-hd-marker)
3966 (org-agenda-entry-text-show-here))
3967 (beginning-of-line 0))))
3969 (defun org-agenda-entry-text-hide ()
3970 "Remove any shown entry context."
3971 (delq nil
3972 (mapcar (lambda (o)
3973 (if (eq (overlay-get o 'org-overlay-type)
3974 'agenda-entry-content)
3975 (progn (delete-overlay o) t)))
3976 (overlays-in (point-min) (point-max)))))
3978 (defun org-agenda-get-day-face (date)
3979 "Return the face DATE should be displayed with."
3980 (or (and (functionp org-agenda-day-face-function)
3981 (funcall org-agenda-day-face-function date))
3982 (cond ((org-agenda-todayp date)
3983 'org-agenda-date-today)
3984 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3985 'org-agenda-date-weekend)
3986 (t 'org-agenda-date))))
3988 ;;; Agenda timeline
3990 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3991 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
3993 (defun org-timeline (&optional dotodo)
3994 "Show a time-sorted view of the entries in the current org file.
3995 Only entries with a time stamp of today or later will be listed. With
3996 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3997 under the current date.
3998 If the buffer contains an active region, only check the region for
3999 dates."
4000 (interactive "P")
4001 (let* ((dopast t)
4002 (org-agenda-show-log-scoped org-agenda-show-log)
4003 (org-agenda-show-log org-agenda-show-log-scoped)
4004 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4005 (current-buffer))))
4006 (date (calendar-current-date))
4007 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4008 (end (if (org-region-active-p) (region-end) (point-max)))
4009 (day-numbers (org-get-all-dates
4010 beg end 'no-ranges
4011 t org-agenda-show-log-scoped ; always include today
4012 org-timeline-show-empty-dates))
4013 (org-deadline-warning-days 0)
4014 (org-agenda-only-exact-dates t)
4015 (today (org-today))
4016 (past t)
4017 args
4018 s e rtn d emptyp)
4019 (setq org-agenda-redo-command
4020 (list 'let
4021 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4022 (list 'org-switch-to-buffer-other-window (current-buffer))
4023 (list 'org-timeline (list 'quote dotodo))))
4024 (put 'org-agenda-redo-command 'org-lprops nil)
4025 (if (not dopast)
4026 ;; Remove past dates from the list of dates.
4027 (setq day-numbers (delq nil (mapcar (lambda(x)
4028 (if (>= x today) x nil))
4029 day-numbers))))
4030 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4031 (org-compile-prefix-format 'timeline)
4032 (org-set-sorting-strategy 'timeline)
4033 (if org-agenda-show-log-scoped (push :closed args))
4034 (push :timestamp args)
4035 (push :deadline args)
4036 (push :scheduled args)
4037 (push :sexp args)
4038 (if dotodo (push :todo args))
4039 (insert "Timeline of file " entry "\n")
4040 (add-text-properties (point-min) (point)
4041 (list 'face 'org-agenda-structure))
4042 (org-agenda-mark-header-line (point-min))
4043 (while (setq d (pop day-numbers))
4044 (if (and (listp d) (eq (car d) :omitted))
4045 (progn
4046 (setq s (point))
4047 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4048 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4049 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4050 (if (and (>= d today)
4051 dopast
4052 past)
4053 (progn
4054 (setq past nil)
4055 (insert (make-string 79 ?-) "\n")))
4056 (setq date (calendar-gregorian-from-absolute d))
4057 (setq s (point))
4058 (setq rtn (and (not emptyp)
4059 (apply 'org-agenda-get-day-entries entry
4060 date args)))
4061 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4062 (progn
4063 (insert
4064 (if (stringp org-agenda-format-date)
4065 (format-time-string org-agenda-format-date
4066 (org-time-from-absolute date))
4067 (funcall org-agenda-format-date date))
4068 "\n")
4069 (put-text-property s (1- (point)) 'face
4070 (org-agenda-get-day-face date))
4071 (put-text-property s (1- (point)) 'org-date-line t)
4072 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4073 (if (equal d today)
4074 (put-text-property s (1- (point)) 'org-today t))
4075 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4076 (put-text-property s (1- (point)) 'day d)))))
4077 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4078 (point-min)))
4079 (add-text-properties
4080 (point-min) (point-max)
4081 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4082 (org-agenda-finalize)
4083 (setq buffer-read-only t)))
4085 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4086 "Return a list of all relevant day numbers from BEG to END buffer positions.
4087 If NO-RANGES is non-nil, include only the start and end dates of a range,
4088 not every single day in the range. If FORCE-TODAY is non-nil, make
4089 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4090 inactive time stamps (those in square brackets) are included.
4091 When EMPTY is non-nil, also include days without any entries."
4092 (let ((re (concat
4093 (if pre-re pre-re "")
4094 (if inactive org-ts-regexp-both org-ts-regexp)))
4095 dates dates1 date day day1 day2 ts1 ts2 pos)
4096 (if force-today
4097 (setq dates (list (org-today))))
4098 (save-excursion
4099 (goto-char beg)
4100 (while (re-search-forward re end t)
4101 (setq day (time-to-days (org-time-string-to-time
4102 (substring (match-string 1) 0 10)
4103 (current-buffer) (match-beginning 0))))
4104 (or (memq day dates) (push day dates)))
4105 (unless no-ranges
4106 (goto-char beg)
4107 (while (re-search-forward org-tr-regexp end t)
4108 (setq pos (match-beginning 0))
4109 (setq ts1 (substring (match-string 1) 0 10)
4110 ts2 (substring (match-string 2) 0 10)
4111 day1 (time-to-days (org-time-string-to-time
4112 ts1 (current-buffer) pos))
4113 day2 (time-to-days (org-time-string-to-time
4114 ts2 (current-buffer) pos)))
4115 (while (< (setq day1 (1+ day1)) day2)
4116 (or (memq day1 dates) (push day1 dates)))))
4117 (setq dates (sort dates '<))
4118 (when empty
4119 (while (setq day (pop dates))
4120 (setq day2 (car dates))
4121 (push day dates1)
4122 (when (and day2 empty)
4123 (if (or (eq empty t)
4124 (and (numberp empty) (<= (- day2 day) empty)))
4125 (while (< (setq day (1+ day)) day2)
4126 (push (list day) dates1))
4127 (push (cons :omitted (- day2 day)) dates1))))
4128 (setq dates (nreverse dates1)))
4129 dates)))
4131 ;;; Agenda Daily/Weekly
4133 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4134 "Start day for the agenda view.
4135 Custom commands can set this variable in the options section.
4136 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4137 input allowed when reading a date through the Org calendar.
4138 See the docstring of `org-read-date' for details.")
4139 (defvar org-starting-day nil) ; local variable in the agenda buffer
4140 (defvar org-arg-loc nil) ; local variable
4142 (defvar org-agenda-buffer-tmp-name nil)
4143 ;;;###autoload
4144 (defun org-agenda-list (&optional arg start-day span with-hour)
4145 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4146 The view will be for the current day or week, but from the overview buffer
4147 you will be able to go to other days/weeks.
4149 With a numeric prefix argument in an interactive call, the agenda will
4150 span ARG days. Lisp programs should instead specify SPAN to change
4151 the number of days. SPAN defaults to `org-agenda-span'.
4153 START-DAY defaults to TODAY, or to the most recent match for the weekday
4154 given in `org-agenda-start-on-weekday'.
4156 When WITH-HOUR is non-nil, only include scheduled and deadline
4157 items if they have an hour specification like [h]h:mm."
4158 (interactive "P")
4159 (if org-agenda-overriding-arguments
4160 (setq arg (car org-agenda-overriding-arguments)
4161 start-day (nth 1 org-agenda-overriding-arguments)
4162 span (nth 2 org-agenda-overriding-arguments)))
4163 (if (and (integerp arg) (> arg 0))
4164 (setq span arg arg nil))
4165 (catch 'exit
4166 (setq org-agenda-buffer-name
4167 (or org-agenda-buffer-tmp-name
4168 (if org-agenda-sticky
4169 (cond ((and org-keys (stringp org-match))
4170 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4171 (org-keys
4172 (format "*Org Agenda(%s)*" org-keys))
4173 (t "*Org Agenda(a)*")))
4174 org-agenda-buffer-name))
4175 (org-agenda-prepare "Day/Week")
4176 (setq start-day (or start-day org-agenda-start-day))
4177 (if (stringp start-day)
4178 ;; Convert to an absolute day number
4179 (setq start-day (time-to-days (org-read-date nil t start-day))))
4180 (org-compile-prefix-format 'agenda)
4181 (org-set-sorting-strategy 'agenda)
4182 (let* ((span (org-agenda-ndays-to-span
4183 (or span org-agenda-ndays org-agenda-span)))
4184 (today (org-today))
4185 (sd (or start-day today))
4186 (ndays (org-agenda-span-to-ndays span sd))
4187 (org-agenda-start-on-weekday
4188 (if (or (eq ndays 7) (eq ndays 14))
4189 org-agenda-start-on-weekday))
4190 (thefiles (org-agenda-files nil 'ifmode))
4191 (files thefiles)
4192 (start (if (or (null org-agenda-start-on-weekday)
4193 (< ndays 7))
4195 (let* ((nt (calendar-day-of-week
4196 (calendar-gregorian-from-absolute sd)))
4197 (n1 org-agenda-start-on-weekday)
4198 (d (- nt n1)))
4199 (- sd (+ (if (< d 0) 7 0) d)))))
4200 (day-numbers (list start))
4201 (day-cnt 0)
4202 (inhibit-redisplay (not debug-on-error))
4203 (org-agenda-show-log-scoped org-agenda-show-log)
4204 s e rtn rtnall file date d start-pos end-pos todayp
4205 clocktable-start clocktable-end filter)
4206 (setq org-agenda-redo-command
4207 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4208 (dotimes (n (1- ndays))
4209 (push (1+ (car day-numbers)) day-numbers))
4210 (setq day-numbers (nreverse day-numbers))
4211 (setq clocktable-start (car day-numbers)
4212 clocktable-end (1+ (or (org-last day-numbers) 0)))
4213 (org-set-local 'org-starting-day (car day-numbers))
4214 (org-set-local 'org-arg-loc arg)
4215 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4216 (unless org-agenda-compact-blocks
4217 (let* ((d1 (car day-numbers))
4218 (d2 (org-last day-numbers))
4219 (w1 (org-days-to-iso-week d1))
4220 (w2 (org-days-to-iso-week d2)))
4221 (setq s (point))
4222 (if org-agenda-overriding-header
4223 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4224 nil 'face 'org-agenda-structure) "\n")
4225 (insert (org-agenda-span-name span)
4226 "-agenda"
4227 (if (< (- d2 d1) 350)
4228 (if (= w1 w2)
4229 (format " (W%02d)" w1)
4230 (format " (W%02d-W%02d)" w1 w2))
4232 ":\n")))
4233 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4234 'org-date-line t))
4235 (org-agenda-mark-header-line s))
4236 (while (setq d (pop day-numbers))
4237 (setq date (calendar-gregorian-from-absolute d)
4238 s (point))
4239 (if (or (setq todayp (= d today))
4240 (and (not start-pos) (= d sd)))
4241 (setq start-pos (point))
4242 (if (and start-pos (not end-pos))
4243 (setq end-pos (point))))
4244 (setq files thefiles
4245 rtnall nil)
4246 (while (setq file (pop files))
4247 (catch 'nextfile
4248 (org-check-agenda-file file)
4249 (let ((org-agenda-entry-types org-agenda-entry-types))
4250 ;; Starred types override non-starred equivalents
4251 (when (member :deadline* org-agenda-entry-types)
4252 (setq org-agenda-entry-types
4253 (delq :deadline org-agenda-entry-types)))
4254 (when (member :scheduled* org-agenda-entry-types)
4255 (setq org-agenda-entry-types
4256 (delq :scheduled org-agenda-entry-types)))
4257 ;; Honor with-hour
4258 (when with-hour
4259 (when (member :deadline org-agenda-entry-types)
4260 (setq org-agenda-entry-types
4261 (delq :deadline org-agenda-entry-types))
4262 (push :deadline* org-agenda-entry-types))
4263 (when (member :scheduled org-agenda-entry-types)
4264 (setq org-agenda-entry-types
4265 (delq :scheduled org-agenda-entry-types))
4266 (push :scheduled* org-agenda-entry-types)))
4267 (unless org-agenda-include-deadlines
4268 (setq org-agenda-entry-types
4269 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4270 (cond
4271 ((memq org-agenda-show-log-scoped '(only clockcheck))
4272 (setq rtn (org-agenda-get-day-entries
4273 file date :closed)))
4274 (org-agenda-show-log-scoped
4275 (setq rtn (apply 'org-agenda-get-day-entries
4276 file date
4277 (append '(:closed) org-agenda-entry-types))))
4279 (setq rtn (apply 'org-agenda-get-day-entries
4280 file date
4281 org-agenda-entry-types)))))
4282 (setq rtnall (append rtnall rtn)))) ;; all entries
4283 (if org-agenda-include-diary
4284 (let ((org-agenda-search-headline-for-time t))
4285 (require 'diary-lib)
4286 (setq rtn (org-get-entries-from-diary date))
4287 (setq rtnall (append rtnall rtn))))
4288 (if (or rtnall org-agenda-show-all-dates)
4289 (progn
4290 (setq day-cnt (1+ day-cnt))
4291 (insert
4292 (if (stringp org-agenda-format-date)
4293 (format-time-string org-agenda-format-date
4294 (org-time-from-absolute date))
4295 (funcall org-agenda-format-date date))
4296 "\n")
4297 (put-text-property s (1- (point)) 'face
4298 (org-agenda-get-day-face date))
4299 (put-text-property s (1- (point)) 'org-date-line t)
4300 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4301 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4302 (when todayp
4303 (put-text-property s (1- (point)) 'org-today t))
4304 (setq rtnall
4305 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4306 (if rtnall (insert ;; all entries
4307 (org-agenda-finalize-entries rtnall 'agenda)
4308 "\n"))
4309 (put-text-property s (1- (point)) 'day d)
4310 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4311 (when (and org-agenda-clockreport-mode clocktable-start)
4312 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4313 ;; the above line is to ensure the restricted range!
4314 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4315 tbl)
4316 (setq p (org-plist-delete p :block))
4317 (setq p (plist-put p :tstart clocktable-start))
4318 (setq p (plist-put p :tend clocktable-end))
4319 (setq p (plist-put p :scope 'agenda))
4320 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4321 (setq filter (or org-agenda-tag-filter-while-redo
4322 (get 'org-agenda-tag-filter :preset-filter))))
4323 (setq p (plist-put p :tags (mapconcat (lambda (x)
4324 (if (string-match "[<>=]" x)
4327 filter ""))))
4328 (setq tbl (apply 'org-clock-get-clocktable p))
4329 (insert tbl)))
4330 (goto-char (point-min))
4331 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4332 (unless (and (pos-visible-in-window-p (point-min))
4333 (pos-visible-in-window-p (point-max)))
4334 (goto-char (1- (point-max)))
4335 (recenter -1)
4336 (if (not (pos-visible-in-window-p (or start-pos 1)))
4337 (progn
4338 (goto-char (or start-pos 1))
4339 (recenter 1))))
4340 (goto-char (or start-pos 1))
4341 (add-text-properties (point-min) (point-max)
4342 `(org-agenda-type agenda
4343 org-last-args (,arg ,start-day ,span)
4344 org-redo-cmd ,org-agenda-redo-command
4345 org-series-cmd ,org-cmd))
4346 (if (eq org-agenda-show-log-scoped 'clockcheck)
4347 (org-agenda-show-clocking-issues))
4348 (org-agenda-finalize)
4349 (setq buffer-read-only t)
4350 (message ""))))
4352 (defun org-agenda-ndays-to-span (n)
4353 "Return a span symbol for a span of N days, or N if none matches."
4354 (cond ((symbolp n) n)
4355 ((= n 1) 'day)
4356 ((= n 7) 'week)
4357 ((= n 14) 'fortnight)
4358 (t n)))
4360 (defun org-agenda-span-to-ndays (span &optional start-day)
4361 "Return ndays from SPAN, possibly starting at START-DAY.
4362 START-DAY is an absolute time value."
4363 (cond ((numberp span) span)
4364 ((eq span 'day) 1)
4365 ((eq span 'week) 7)
4366 ((eq span 'fortnight) 14)
4367 ((eq span 'month)
4368 (let ((date (calendar-gregorian-from-absolute start-day)))
4369 (calendar-last-day-of-month (car date) (caddr date))))
4370 ((eq span 'year)
4371 (let ((date (calendar-gregorian-from-absolute start-day)))
4372 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4374 (defun org-agenda-span-name (span)
4375 "Return a SPAN name."
4376 (if (null span)
4378 (if (symbolp span)
4379 (capitalize (symbol-name span))
4380 (format "%d days" span))))
4382 ;;; Agenda word search
4384 (defvar org-agenda-search-history nil)
4386 (defvar org-search-syntax-table nil
4387 "Special syntax table for org-mode search.
4388 In this table, we have single quotes not as word constituents, to
4389 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4391 (defvar org-mode-syntax-table) ; From org.el
4392 (defun org-search-syntax-table ()
4393 (unless org-search-syntax-table
4394 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4395 (modify-syntax-entry ?' "." org-search-syntax-table)
4396 (modify-syntax-entry ?` "." org-search-syntax-table))
4397 org-search-syntax-table)
4399 (defvar org-agenda-last-search-view-search-was-boolean nil)
4401 ;;;###autoload
4402 (defun org-search-view (&optional todo-only string edit-at)
4403 "Show all entries that contain a phrase or words or regular expressions.
4405 With optional prefix argument TODO-ONLY, only consider entries that are
4406 TODO entries. The argument STRING can be used to pass a default search
4407 string into this function. If EDIT-AT is non-nil, it means that the
4408 user should get a chance to edit this string, with cursor at position
4409 EDIT-AT.
4411 The search string can be viewed either as a phrase that should be found as
4412 is, or it can be broken into a number of snippets, each of which must match
4413 in a Boolean way to select an entry. The default depends on the variable
4414 `org-agenda-search-view-always-boolean'.
4415 Even if this is turned off (the default) you can always switch to
4416 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4418 The default is a direct search of the whole phrase, where each space in
4419 the search string can expand to an arbitrary amount of whitespace,
4420 including newlines.
4422 If using a Boolean search, the search string is split on whitespace and
4423 each snippet is searched separately, with logical AND to select an entry.
4424 Words prefixed with a minus must *not* occur in the entry. Words without
4425 a prefix or prefixed with a plus must occur in the entry. Matching is
4426 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4427 match whole words, not parts of a word) if
4428 `org-agenda-search-view-force-full-words' is set (default is nil).
4430 Boolean search snippets enclosed by curly braces are interpreted as
4431 regular expressions that must or (when preceded with \"-\") must not
4432 match in the entry. Snippets enclosed into double quotes will be taken
4433 as a whole, to include whitespace.
4435 - If the search string starts with an asterisk, search only in headlines.
4436 - If (possibly after the leading star) the search string starts with an
4437 exclamation mark, this also means to look at TODO entries only, an effect
4438 that can also be achieved with a prefix argument.
4439 - If (possibly after star and exclamation mark) the search string starts
4440 with a colon, this will mean that the (non-regexp) snippets of the
4441 Boolean search must match as full words.
4443 This command searches the agenda files, and in addition the files listed
4444 in `org-agenda-text-search-extra-files'."
4445 (interactive "P")
4446 (if org-agenda-overriding-arguments
4447 (setq todo-only (car org-agenda-overriding-arguments)
4448 string (nth 1 org-agenda-overriding-arguments)
4449 edit-at (nth 2 org-agenda-overriding-arguments)))
4450 (let* ((props (list 'face nil
4451 'done-face 'org-agenda-done
4452 'org-not-done-regexp org-not-done-regexp
4453 'org-todo-regexp org-todo-regexp
4454 'org-complex-heading-regexp org-complex-heading-regexp
4455 'mouse-face 'highlight
4456 'help-echo (format "mouse-2 or RET jump to location")))
4457 (full-words org-agenda-search-view-force-full-words)
4458 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4459 regexp rtn rtnall files file pos inherited-tags
4460 marker category category-pos level tags c neg re boolean
4461 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4462 (unless (and (not edit-at)
4463 (stringp string)
4464 (string-match "\\S-" string))
4465 (setq string (read-string
4466 (if org-agenda-search-view-always-boolean
4467 "[+-]Word/{Regexp} ...: "
4468 "Phrase or [+-]Word/{Regexp} ...: ")
4469 (cond
4470 ((integerp edit-at) (cons string edit-at))
4471 (edit-at string))
4472 'org-agenda-search-history)))
4473 (catch 'exit
4474 (if org-agenda-sticky
4475 (setq org-agenda-buffer-name
4476 (if (stringp string)
4477 (format "*Org Agenda(%s:%s)*"
4478 (or org-keys (or (and todo-only "S") "s")) string)
4479 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4480 (org-agenda-prepare "SEARCH")
4481 (org-compile-prefix-format 'search)
4482 (org-set-sorting-strategy 'search)
4483 (setq org-agenda-redo-command
4484 (list 'org-search-view (if todo-only t nil)
4485 (list 'if 'current-prefix-arg nil string)))
4486 (setq org-agenda-query-string string)
4487 (if (equal (string-to-char string) ?*)
4488 (setq hdl-only t
4489 words (substring string 1))
4490 (setq words string))
4491 (when (equal (string-to-char words) ?!)
4492 (setq todo-only t
4493 words (substring words 1)))
4494 (when (equal (string-to-char words) ?:)
4495 (setq full-words t
4496 words (substring words 1)))
4497 (if (or org-agenda-search-view-always-boolean
4498 (member (string-to-char words) '(?- ?+ ?\{)))
4499 (setq boolean t))
4500 (setq words (org-split-string words))
4501 (let (www w)
4502 (while (setq w (pop words))
4503 (while (and (string-match "\\\\\\'" w) words)
4504 (setq w (concat (substring w 0 -1) " " (pop words))))
4505 (push w www))
4506 (setq words (nreverse www) www nil)
4507 (while (setq w (pop words))
4508 (when (and (string-match "\\`[-+]?{" w)
4509 (not (string-match "}\\'" w)))
4510 (while (and words (not (string-match "}\\'" (car words))))
4511 (setq w (concat w " " (pop words))))
4512 (setq w (concat w " " (pop words))))
4513 (push w www))
4514 (setq words (nreverse www)))
4515 (setq org-agenda-last-search-view-search-was-boolean boolean)
4516 (when boolean
4517 (let (wds w)
4518 (while (setq w (pop words))
4519 (if (or (equal (substring w 0 1) "\"")
4520 (and (> (length w) 1)
4521 (member (substring w 0 1) '("+" "-"))
4522 (equal (substring w 1 2) "\"")))
4523 (while (and words (not (equal (substring w -1) "\"")))
4524 (setq w (concat w " " (pop words)))))
4525 (and (string-match "\\`\\([-+]?\\)\"" w)
4526 (setq w (replace-match "\\1" nil nil w)))
4527 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4528 (push w wds))
4529 (setq words (nreverse wds))))
4530 (if boolean
4531 (mapc (lambda (w)
4532 (setq c (string-to-char w))
4533 (if (equal c ?-)
4534 (setq neg t w (substring w 1))
4535 (if (equal c ?+)
4536 (setq neg nil w (substring w 1))
4537 (setq neg nil)))
4538 (if (string-match "\\`{.*}\\'" w)
4539 (setq re (substring w 1 -1))
4540 (if full-words
4541 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4542 (setq re (regexp-quote (downcase w)))))
4543 (if neg (push re regexps-) (push re regexps+)))
4544 words)
4545 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4546 regexps+))
4547 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4548 (if (not regexps+)
4549 (setq regexp org-outline-regexp-bol)
4550 (setq regexp (pop regexps+))
4551 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4552 regexp))))
4553 (setq files (org-agenda-files nil 'ifmode))
4554 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4555 (pop org-agenda-text-search-extra-files)
4556 (setq files (org-add-archive-files files)))
4557 (setq files (append files org-agenda-text-search-extra-files)
4558 rtnall nil)
4559 (while (setq file (pop files))
4560 (setq ee nil)
4561 (catch 'nextfile
4562 (org-check-agenda-file file)
4563 (setq buffer (if (file-exists-p file)
4564 (org-get-agenda-file-buffer file)
4565 (error "No such file %s" file)))
4566 (if (not buffer)
4567 ;; If file does not exist, make sure an error message is sent
4568 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4569 file))))
4570 (with-current-buffer buffer
4571 (with-syntax-table (org-search-syntax-table)
4572 (unless (derived-mode-p 'org-mode)
4573 (error "Agenda file %s is not in `org-mode'" file))
4574 (let ((case-fold-search t))
4575 (save-excursion
4576 (save-restriction
4577 (if (eq buffer org-agenda-restrict)
4578 (narrow-to-region org-agenda-restrict-begin
4579 org-agenda-restrict-end)
4580 (widen))
4581 (goto-char (point-min))
4582 (unless (or (org-at-heading-p)
4583 (outline-next-heading))
4584 (throw 'nextfile t))
4585 (goto-char (max (point-min) (1- (point))))
4586 (while (re-search-forward regexp nil t)
4587 (org-back-to-heading t)
4588 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4589 (> (org-reduced-level (org-outline-level))
4590 org-agenda-search-view-max-outline-level)
4591 (forward-line -1)
4592 (outline-back-to-heading t)))
4593 (skip-chars-forward "* ")
4594 (setq beg (point-at-bol)
4595 beg1 (point)
4596 end (progn
4597 (outline-next-heading)
4598 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4599 (> (org-reduced-level (org-outline-level))
4600 org-agenda-search-view-max-outline-level)
4601 (forward-line 1)
4602 (outline-next-heading)))
4603 (point)))
4605 (catch :skip
4606 (goto-char beg)
4607 (org-agenda-skip)
4608 (setq str (buffer-substring-no-properties
4609 (point-at-bol)
4610 (if hdl-only (point-at-eol) end)))
4611 (mapc (lambda (wr) (when (string-match wr str)
4612 (goto-char (1- end))
4613 (throw :skip t)))
4614 regexps-)
4615 (mapc (lambda (wr) (unless (string-match wr str)
4616 (goto-char (1- end))
4617 (throw :skip t)))
4618 (if todo-only
4619 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4620 regexps+)
4621 regexps+))
4622 (goto-char beg)
4623 (setq marker (org-agenda-new-marker (point))
4624 category (org-get-category)
4625 level (make-string (org-reduced-level (org-outline-level)) ? )
4626 category-pos (get-text-property (point) 'org-category-position)
4627 inherited-tags
4628 (or (eq org-agenda-show-inherited-tags 'always)
4629 (and (listp org-agenda-show-inherited-tags)
4630 (memq 'todo org-agenda-show-inherited-tags))
4631 (and (eq org-agenda-show-inherited-tags t)
4632 (or (eq org-agenda-use-tag-inheritance t)
4633 (memq 'todo org-agenda-use-tag-inheritance))))
4634 tags (org-get-tags-at nil (not inherited-tags))
4635 txt (org-agenda-format-item
4637 (buffer-substring-no-properties
4638 beg1 (point-at-eol))
4639 level category tags t))
4640 (org-add-props txt props
4641 'org-marker marker 'org-hd-marker marker
4642 'org-todo-regexp org-todo-regexp
4643 'level level
4644 'org-complex-heading-regexp org-complex-heading-regexp
4645 'priority 1000 'org-category category
4646 'org-category-position category-pos
4647 'type "search")
4648 (push txt ee)
4649 (goto-char (1- end))))))))))
4650 (setq rtn (nreverse ee))
4651 (setq rtnall (append rtnall rtn)))
4652 (if org-agenda-overriding-header
4653 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4654 nil 'face 'org-agenda-structure) "\n")
4655 (insert "Search words: ")
4656 (add-text-properties (point-min) (1- (point))
4657 (list 'face 'org-agenda-structure))
4658 (setq pos (point))
4659 (insert string "\n")
4660 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4661 (setq pos (point))
4662 (unless org-agenda-multi
4663 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4664 (add-text-properties pos (1- (point))
4665 (list 'face 'org-agenda-structure))))
4666 (org-agenda-mark-header-line (point-min))
4667 (when rtnall
4668 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4669 (goto-char (point-min))
4670 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4671 (add-text-properties (point-min) (point-max)
4672 `(org-agenda-type search
4673 org-last-args (,todo-only ,string ,edit-at)
4674 org-redo-cmd ,org-agenda-redo-command
4675 org-series-cmd ,org-cmd))
4676 (org-agenda-finalize)
4677 (setq buffer-read-only t))))
4679 ;;; Agenda TODO list
4681 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4682 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4683 (concat
4684 (if (or (equal keywords "ALL") (not keywords))
4685 (propertize "ALL" 'face 'warning)
4686 (mapconcat
4687 (lambda (kw)
4688 (propertize kw 'face (org-get-todo-face kw)))
4689 (org-split-string keywords "|")
4690 "|"))
4691 "\n"))
4693 (defvar org-select-this-todo-keyword nil)
4694 (defvar org-last-arg nil)
4696 ;;;###autoload
4697 (defun org-todo-list (&optional arg)
4698 "Show all (not done) TODO entries from all agenda file in a single list.
4699 The prefix arg can be used to select a specific TODO keyword and limit
4700 the list to these. When using \\[universal-argument], you will be prompted
4701 for a keyword. A numeric prefix directly selects the Nth keyword in
4702 `org-todo-keywords-1'."
4703 (interactive "P")
4704 (if org-agenda-overriding-arguments
4705 (setq arg org-agenda-overriding-arguments))
4706 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4707 (let* ((today (org-today))
4708 (date (calendar-gregorian-from-absolute today))
4709 (kwds org-todo-keywords-for-agenda)
4710 (completion-ignore-case t)
4711 (org-select-this-todo-keyword
4712 (if (stringp arg) arg
4713 (and arg (integerp arg) (> arg 0)
4714 (nth (1- arg) kwds))))
4715 rtn rtnall files file pos)
4716 (when (equal arg '(4))
4717 (setq org-select-this-todo-keyword
4718 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4719 (mapcar 'list kwds) nil nil)))
4720 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4721 (catch 'exit
4722 (if org-agenda-sticky
4723 (setq org-agenda-buffer-name
4724 (if (stringp org-select-this-todo-keyword)
4725 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4726 org-select-this-todo-keyword)
4727 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4728 (org-agenda-prepare "TODO")
4729 (org-compile-prefix-format 'todo)
4730 (org-set-sorting-strategy 'todo)
4731 (setq org-agenda-redo-command
4732 `(org-todo-list (or (and (numberp current-prefix-arg)
4733 current-prefix-arg)
4734 ,org-select-this-todo-keyword
4735 current-prefix-arg ,arg)))
4736 (setq files (org-agenda-files nil 'ifmode)
4737 rtnall nil)
4738 (while (setq file (pop files))
4739 (catch 'nextfile
4740 (org-check-agenda-file file)
4741 (setq rtn (org-agenda-get-day-entries file date :todo))
4742 (setq rtnall (append rtnall rtn))))
4743 (if org-agenda-overriding-header
4744 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4745 nil 'face 'org-agenda-structure) "\n")
4746 (insert "Global list of TODO items of type: ")
4747 (add-text-properties (point-min) (1- (point))
4748 (list 'face 'org-agenda-structure
4749 'short-heading
4750 (concat "ToDo: "
4751 (or org-select-this-todo-keyword "ALL"))))
4752 (org-agenda-mark-header-line (point-min))
4753 (insert (org-agenda-propertize-selected-todo-keywords
4754 org-select-this-todo-keyword))
4755 (setq pos (point))
4756 (unless org-agenda-multi
4757 (insert "Available with `N r': (0)[ALL]")
4758 (let ((n 0) s)
4759 (mapc (lambda (x)
4760 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4761 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4762 (insert "\n "))
4763 (insert " " s))
4764 kwds))
4765 (insert "\n"))
4766 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4767 (org-agenda-mark-header-line (point-min))
4768 (when rtnall
4769 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4770 (goto-char (point-min))
4771 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4772 (add-text-properties (point-min) (point-max)
4773 `(org-agenda-type todo
4774 org-last-args ,arg
4775 org-redo-cmd ,org-agenda-redo-command
4776 org-series-cmd ,org-cmd))
4777 (org-agenda-finalize)
4778 (setq buffer-read-only t))))
4780 ;;; Agenda tags match
4782 ;;;###autoload
4783 (defun org-tags-view (&optional todo-only match)
4784 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4785 The prefix arg TODO-ONLY limits the search to TODO entries."
4786 (interactive "P")
4787 (if org-agenda-overriding-arguments
4788 (setq todo-only (car org-agenda-overriding-arguments)
4789 match (nth 1 org-agenda-overriding-arguments)))
4790 (let* ((org-tags-match-list-sublevels
4791 org-tags-match-list-sublevels)
4792 (completion-ignore-case t)
4793 rtn rtnall files file pos matcher
4794 buffer)
4795 (when (and (stringp match) (not (string-match "\\S-" match)))
4796 (setq match nil))
4797 (catch 'exit
4798 (if org-agenda-sticky
4799 (setq org-agenda-buffer-name
4800 (if (stringp match)
4801 (format "*Org Agenda(%s:%s)*"
4802 (or org-keys (or (and todo-only "M") "m")) match)
4803 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4804 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4805 ;; expanding tags within `org-make-tags-matcher'
4806 (org-agenda-prepare (concat "TAGS " match))
4807 (setq matcher (org-make-tags-matcher match)
4808 match (car matcher) matcher (cdr matcher))
4809 (org-compile-prefix-format 'tags)
4810 (org-set-sorting-strategy 'tags)
4811 (setq org-agenda-query-string match)
4812 (setq org-agenda-redo-command
4813 (list 'org-tags-view `(quote ,todo-only)
4814 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4815 (setq files (org-agenda-files nil 'ifmode)
4816 rtnall nil)
4817 (while (setq file (pop files))
4818 (catch 'nextfile
4819 (org-check-agenda-file file)
4820 (setq buffer (if (file-exists-p file)
4821 (org-get-agenda-file-buffer file)
4822 (error "No such file %s" file)))
4823 (if (not buffer)
4824 ;; If file does not exist, error message to agenda
4825 (setq rtn (list
4826 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4827 rtnall (append rtnall rtn))
4828 (with-current-buffer buffer
4829 (unless (derived-mode-p 'org-mode)
4830 (error "Agenda file %s is not in `org-mode'" file))
4831 (save-excursion
4832 (save-restriction
4833 (if (eq buffer org-agenda-restrict)
4834 (narrow-to-region org-agenda-restrict-begin
4835 org-agenda-restrict-end)
4836 (widen))
4837 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4838 (setq rtnall (append rtnall rtn))))))))
4839 (if org-agenda-overriding-header
4840 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4841 nil 'face 'org-agenda-structure) "\n")
4842 (insert "Headlines with TAGS match: ")
4843 (add-text-properties (point-min) (1- (point))
4844 (list 'face 'org-agenda-structure
4845 'short-heading
4846 (concat "Match: " match)))
4847 (setq pos (point))
4848 (insert match "\n")
4849 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4850 (setq pos (point))
4851 (unless org-agenda-multi
4852 (insert "Press `C-u r' to search again with new search string\n"))
4853 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4854 (org-agenda-mark-header-line (point-min))
4855 (when rtnall
4856 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4857 (goto-char (point-min))
4858 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4859 (add-text-properties (point-min) (point-max)
4860 `(org-agenda-type tags
4861 org-last-args (,todo-only ,match)
4862 org-redo-cmd ,org-agenda-redo-command
4863 org-series-cmd ,org-cmd))
4864 (org-agenda-finalize)
4865 (setq buffer-read-only t))))
4867 ;;; Agenda Finding stuck projects
4869 (defvar org-agenda-skip-regexp nil
4870 "Regular expression used in skipping subtrees for the agenda.
4871 This is basically a temporary global variable that can be set and then
4872 used by user-defined selections using `org-agenda-skip-function'.")
4874 (defvar org-agenda-overriding-header nil
4875 "When set during agenda, todo and tags searches it replaces the header.
4876 This variable should not be set directly, but custom commands can bind it
4877 in the options section.")
4879 (defun org-agenda-skip-entry-when-regexp-matches ()
4880 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4881 If yes, it returns the end position of this entry, causing agenda commands
4882 to skip the entry but continuing the search in the subtree. This is a
4883 function that can be put into `org-agenda-skip-function' for the duration
4884 of a command."
4885 (let ((end (save-excursion (org-end-of-subtree t)))
4886 skip)
4887 (save-excursion
4888 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4889 (and skip end)))
4891 (defun org-agenda-skip-subtree-when-regexp-matches ()
4892 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4893 If yes, it returns the end position of this tree, causing agenda commands
4894 to skip this subtree. This is a function that can be put into
4895 `org-agenda-skip-function' for the duration of a command."
4896 (let ((end (save-excursion (org-end-of-subtree t)))
4897 skip)
4898 (save-excursion
4899 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4900 (and skip end)))
4902 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4903 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4904 If yes, it returns the end position of the current entry (NOT the tree),
4905 causing agenda commands to skip the entry but continuing the search in
4906 the subtree. This is a function that can be put into
4907 `org-agenda-skip-function' for the duration of a command. An important
4908 use of this function is for the stuck project list."
4909 (let ((end (save-excursion (org-end-of-subtree t)))
4910 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4911 skip)
4912 (save-excursion
4913 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4914 (and skip entry-end)))
4916 (defun org-agenda-skip-entry-if (&rest conditions)
4917 "Skip entry if any of CONDITIONS is true.
4918 See `org-agenda-skip-if' for details."
4919 (org-agenda-skip-if nil conditions))
4921 (defun org-agenda-skip-subtree-if (&rest conditions)
4922 "Skip subtree if any of CONDITIONS is true.
4923 See `org-agenda-skip-if' for details."
4924 (org-agenda-skip-if t conditions))
4926 (defun org-agenda-skip-if (subtree conditions)
4927 "Checks current entity for CONDITIONS.
4928 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4929 the entry (i.e. the text before the next heading) is checked.
4931 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4932 from different tests. Valid conditions are:
4934 scheduled Check if there is a scheduled cookie
4935 notscheduled Check if there is no scheduled cookie
4936 deadline Check if there is a deadline
4937 notdeadline Check if there is no deadline
4938 timestamp Check if there is a timestamp (also deadline or scheduled)
4939 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4940 regexp Check if regexp matches
4941 notregexp Check if regexp does not match.
4942 todo Check if TODO keyword matches
4943 nottodo Check if TODO keyword does not match
4945 The regexp is taken from the conditions list, it must come right after
4946 the `regexp' or `notregexp' element.
4948 `todo' and `nottodo' accept as an argument a list of todo
4949 keywords, which may include \"*\" to match any todo keyword.
4951 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4953 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4955 Instead of a list, a keyword class may be given. For example:
4957 (org-agenda-skip-entry-if 'nottodo 'done)
4959 would skip entries that haven't been marked with any of \"DONE\"
4960 keywords. Possible classes are: `todo', `done', `any'.
4962 If any of these conditions is met, this function returns the end point of
4963 the entity, causing the search to continue from there. This is a function
4964 that can be put into `org-agenda-skip-function' for the duration of a command."
4965 (let (beg end m)
4966 (org-back-to-heading t)
4967 (setq beg (point)
4968 end (if subtree
4969 (progn (org-end-of-subtree t) (point))
4970 (progn (outline-next-heading) (1- (point)))))
4971 (goto-char beg)
4972 (and
4974 (and (memq 'scheduled conditions)
4975 (re-search-forward org-scheduled-time-regexp end t))
4976 (and (memq 'notscheduled conditions)
4977 (not (re-search-forward org-scheduled-time-regexp end t)))
4978 (and (memq 'deadline conditions)
4979 (re-search-forward org-deadline-time-regexp end t))
4980 (and (memq 'notdeadline conditions)
4981 (not (re-search-forward org-deadline-time-regexp end t)))
4982 (and (memq 'timestamp conditions)
4983 (re-search-forward org-ts-regexp end t))
4984 (and (memq 'nottimestamp conditions)
4985 (not (re-search-forward org-ts-regexp end t)))
4986 (and (setq m (memq 'regexp conditions))
4987 (stringp (nth 1 m))
4988 (re-search-forward (nth 1 m) end t))
4989 (and (setq m (memq 'notregexp conditions))
4990 (stringp (nth 1 m))
4991 (not (re-search-forward (nth 1 m) end t)))
4992 (and (or
4993 (setq m (memq 'nottodo conditions))
4994 (setq m (memq 'todo-unblocked conditions))
4995 (setq m (memq 'nottodo-unblocked conditions))
4996 (setq m (memq 'todo conditions)))
4997 (org-agenda-skip-if-todo m end)))
4998 end)))
5000 (defun org-agenda-skip-if-todo (args end)
5001 "Helper function for `org-agenda-skip-if', do not use it directly.
5002 ARGS is a list with first element either `todo', `nottodo',
5003 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5004 a list of TODO keywords, or a state symbol `todo' or `done' or
5005 `any'."
5006 (let ((kw (car args))
5007 (arg (cadr args))
5008 todo-wds todo-re)
5009 (setq todo-wds
5010 (org-uniquify
5011 (cond
5012 ((listp arg) ;; list of keywords
5013 (if (member "*" arg)
5014 (mapcar 'substring-no-properties org-todo-keywords-1)
5015 arg))
5016 ((symbolp arg) ;; keyword class name
5017 (cond
5018 ((eq arg 'todo)
5019 (org-delete-all org-done-keywords
5020 (mapcar 'substring-no-properties
5021 org-todo-keywords-1)))
5022 ((eq arg 'done) org-done-keywords)
5023 ((eq arg 'any)
5024 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5025 (setq todo-re
5026 (concat "^\\*+[ \t]+\\<\\("
5027 (mapconcat 'identity todo-wds "\\|")
5028 "\\)\\>"))
5029 (cond
5030 ((eq kw 'todo) (re-search-forward todo-re end t))
5031 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5032 ((eq kw 'todo-unblocked)
5033 (catch 'unblocked
5034 (while (re-search-forward todo-re end t)
5035 (or (org-entry-blocked-p) (throw 'unblocked t)))
5036 nil))
5037 ((eq kw 'nottodo-unblocked)
5038 (catch 'unblocked
5039 (while (re-search-forward todo-re end t)
5040 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5044 ;;;###autoload
5045 (defun org-agenda-list-stuck-projects (&rest ignore)
5046 "Create agenda view for projects that are stuck.
5047 Stuck projects are project that have no next actions. For the definitions
5048 of what a project is and how to check if it stuck, customize the variable
5049 `org-stuck-projects'."
5050 (interactive)
5051 (let* ((org-agenda-skip-function
5052 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5053 ;; We could have used org-agenda-skip-if here.
5054 (org-agenda-overriding-header
5055 (or org-agenda-overriding-header "List of stuck projects: "))
5056 (matcher (nth 0 org-stuck-projects))
5057 (todo (nth 1 org-stuck-projects))
5058 (todo-wds (if (member "*" todo)
5059 (progn
5060 (org-agenda-prepare-buffers (org-agenda-files
5061 nil 'ifmode))
5062 (org-delete-all
5063 org-done-keywords-for-agenda
5064 (copy-sequence org-todo-keywords-for-agenda)))
5065 todo))
5066 (todo-re (concat "^\\*+[ \t]+\\("
5067 (mapconcat 'identity todo-wds "\\|")
5068 "\\)\\>"))
5069 (tags (nth 2 org-stuck-projects))
5070 (tags-re (if (member "*" tags)
5071 (concat org-outline-regexp-bol
5072 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5073 (if tags
5074 (concat org-outline-regexp-bol
5075 ".*:\\("
5076 (mapconcat 'identity tags "\\|")
5077 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5078 (gen-re (nth 3 org-stuck-projects))
5079 (re-list
5080 (delq nil
5081 (list
5082 (if todo todo-re)
5083 (if tags tags-re)
5084 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5085 gen-re)))))
5086 (setq org-agenda-skip-regexp
5087 (if re-list
5088 (mapconcat 'identity re-list "\\|")
5089 (error "No information how to identify unstuck projects")))
5090 (org-tags-view nil matcher)
5091 (setq org-agenda-buffer-name (buffer-name))
5092 (with-current-buffer org-agenda-buffer-name
5093 (setq org-agenda-redo-command
5094 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5096 ;;; Diary integration
5098 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5099 (defvar diary-list-entries-hook)
5100 (defvar diary-time-regexp)
5101 (defun org-get-entries-from-diary (date)
5102 "Get the (Emacs Calendar) diary entries for DATE."
5103 (require 'diary-lib)
5104 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5105 (diary-display-hook '(fancy-diary-display))
5106 (diary-display-function 'fancy-diary-display)
5107 (pop-up-frames nil)
5108 (diary-list-entries-hook
5109 (cons 'org-diary-default-entry diary-list-entries-hook))
5110 (diary-file-name-prefix nil) ; turn this feature off
5111 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5112 entries
5113 (org-disable-agenda-to-diary t))
5114 (save-excursion
5115 (save-window-excursion
5116 (funcall (if (fboundp 'diary-list-entries)
5117 'diary-list-entries 'list-diary-entries)
5118 date 1)))
5119 (if (not (get-buffer diary-fancy-buffer))
5120 (setq entries nil)
5121 (with-current-buffer diary-fancy-buffer
5122 (setq buffer-read-only nil)
5123 (if (zerop (buffer-size))
5124 ;; No entries
5125 (setq entries nil)
5126 ;; Omit the date and other unnecessary stuff
5127 (org-agenda-cleanup-fancy-diary)
5128 ;; Add prefix to each line and extend the text properties
5129 (if (zerop (buffer-size))
5130 (setq entries nil)
5131 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5132 (setq entries
5133 (with-temp-buffer
5134 (insert entries) (goto-char (point-min))
5135 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5136 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5137 (replace-match (concat "; " (match-string 1)))))
5138 (buffer-string)))))
5139 (set-buffer-modified-p nil)
5140 (kill-buffer diary-fancy-buffer)))
5141 (when entries
5142 (setq entries (org-split-string entries "\n"))
5143 (setq entries
5144 (mapcar
5145 (lambda (x)
5146 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5147 ;; Extend the text properties to the beginning of the line
5148 (org-add-props x (text-properties-at (1- (length x)) x)
5149 'type "diary" 'date date 'face 'org-agenda-diary))
5150 entries)))))
5152 (defvar org-agenda-cleanup-fancy-diary-hook nil
5153 "Hook run when the fancy diary buffer is cleaned up.")
5155 (defun org-agenda-cleanup-fancy-diary ()
5156 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5157 This gets rid of the date, the underline under the date, and
5158 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5159 date. It also removes lines that contain only whitespace."
5160 (goto-char (point-min))
5161 (if (looking-at ".*?:[ \t]*")
5162 (progn
5163 (replace-match "")
5164 (re-search-forward "\n=+$" nil t)
5165 (replace-match "")
5166 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5167 (re-search-forward "\n=+$" nil t)
5168 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5169 (goto-char (point-min))
5170 (while (re-search-forward "^ +\n" nil t)
5171 (replace-match ""))
5172 (goto-char (point-min))
5173 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5174 (replace-match ""))
5175 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5177 ;; Make sure entries from the diary have the right text properties.
5178 (eval-after-load "diary-lib"
5179 '(if (boundp 'diary-modify-entry-list-string-function)
5180 ;; We can rely on the hook, nothing to do
5182 ;; Hook not available, must use advice to make this work
5183 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5184 "Make the position visible."
5185 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5186 (stringp string)
5187 buffer-file-name)
5188 (setq string (org-modify-diary-entry-string string))))))
5190 (defun org-modify-diary-entry-string (string)
5191 "Add text properties to string, allowing org-mode to act on it."
5192 (org-add-props string nil
5193 'mouse-face 'highlight
5194 'help-echo (if buffer-file-name
5195 (format "mouse-2 or RET jump to diary file %s"
5196 (abbreviate-file-name buffer-file-name))
5198 'org-agenda-diary-link t
5199 'org-marker (org-agenda-new-marker (point-at-bol))))
5201 (defun org-diary-default-entry ()
5202 "Add a dummy entry to the diary.
5203 Needed to avoid empty dates which mess up holiday display."
5204 ;; Catch the error if dealing with the new add-to-diary-alist
5205 (when org-disable-agenda-to-diary
5206 (condition-case nil
5207 (org-add-to-diary-list original-date "Org-mode dummy" "")
5208 (error
5209 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5211 (defun org-add-to-diary-list (&rest args)
5212 (if (fboundp 'diary-add-to-list)
5213 (apply 'diary-add-to-list args)
5214 (apply 'add-to-diary-list args)))
5216 (defvar org-diary-last-run-time nil)
5218 ;;;###autoload
5219 (defun org-diary (&rest args)
5220 "Return diary information from org files.
5221 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5222 It accesses org files and extracts information from those files to be
5223 listed in the diary. The function accepts arguments specifying what
5224 items should be listed. For a list of arguments allowed here, see the
5225 variable `org-agenda-entry-types'.
5227 The call in the diary file should look like this:
5229 &%%(org-diary) ~/path/to/some/orgfile.org
5231 Use a separate line for each org file to check. Or, if you omit the file name,
5232 all files listed in `org-agenda-files' will be checked automatically:
5234 &%%(org-diary)
5236 If you don't give any arguments (as in the example above), the default value
5237 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5238 So the example above may also be written as
5240 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5242 The function expects the lisp variables `entry' and `date' to be provided
5243 by the caller, because this is how the calendar works. Don't use this
5244 function from a program - use `org-agenda-get-day-entries' instead."
5245 (when (> (- (org-float-time)
5246 org-agenda-last-marker-time)
5248 ;; I am not sure if this works with sticky agendas, because the marker
5249 ;; list is then no longer a global variable.
5250 (org-agenda-reset-markers))
5251 (org-compile-prefix-format 'agenda)
5252 (org-set-sorting-strategy 'agenda)
5253 (setq args (or args org-agenda-entry-types))
5254 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5255 (list entry)
5256 (org-agenda-files t)))
5257 (time (org-float-time))
5258 file rtn results)
5259 (when (or (not org-diary-last-run-time)
5260 (> (- time
5261 org-diary-last-run-time)
5263 (org-agenda-prepare-buffers files))
5264 (setq org-diary-last-run-time time)
5265 ;; If this is called during org-agenda, don't return any entries to
5266 ;; the calendar. Org Agenda will list these entries itself.
5267 (if org-disable-agenda-to-diary (setq files nil))
5268 (while (setq file (pop files))
5269 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5270 (setq results (append results rtn)))
5271 (when results
5272 (setq results
5273 (mapcar (lambda (i) (replace-regexp-in-string
5274 org-bracket-link-regexp "\\3" i)) results))
5275 (concat (org-agenda-finalize-entries results) "\n"))))
5277 ;;; Agenda entry finders
5279 (defun org-agenda-get-day-entries (file date &rest args)
5280 "Does the work for `org-diary' and `org-agenda'.
5281 FILE is the path to a file to be checked for entries. DATE is date like
5282 the one returned by `calendar-current-date'. ARGS are symbols indicating
5283 which kind of entries should be extracted. For details about these, see
5284 the documentation of `org-diary'."
5285 (setq args (or args org-agenda-entry-types))
5286 (let* ((org-startup-folded nil)
5287 (org-startup-align-all-tables nil)
5288 (buffer (if (file-exists-p file)
5289 (org-get-agenda-file-buffer file)
5290 (error "No such file %s" file)))
5291 arg results rtn deadline-results)
5292 (if (not buffer)
5293 ;; If file does not exist, make sure an error message ends up in diary
5294 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5295 (with-current-buffer buffer
5296 (unless (derived-mode-p 'org-mode)
5297 (error "Agenda file %s is not in `org-mode'" file))
5298 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5299 (let ((case-fold-search nil))
5300 (save-excursion
5301 (save-restriction
5302 (if (eq buffer org-agenda-restrict)
5303 (narrow-to-region org-agenda-restrict-begin
5304 org-agenda-restrict-end)
5305 (widen))
5306 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5307 (while (setq arg (pop args))
5308 (cond
5309 ((and (eq arg :todo)
5310 (equal date (calendar-gregorian-from-absolute
5311 (org-today))))
5312 (setq rtn (org-agenda-get-todos))
5313 (setq results (append results rtn)))
5314 ((eq arg :timestamp)
5315 (setq rtn (org-agenda-get-blocks))
5316 (setq results (append results rtn))
5317 (setq rtn (org-agenda-get-timestamps deadline-results))
5318 (setq results (append results rtn)))
5319 ((eq arg :sexp)
5320 (setq rtn (org-agenda-get-sexps))
5321 (setq results (append results rtn)))
5322 ((eq arg :scheduled)
5323 (setq rtn (org-agenda-get-scheduled deadline-results))
5324 (setq results (append results rtn)))
5325 ((eq arg :scheduled*)
5326 (setq rtn (org-agenda-get-scheduled deadline-results t))
5327 (setq results (append results rtn)))
5328 ((eq arg :closed)
5329 (setq rtn (org-agenda-get-progress))
5330 (setq results (append results rtn)))
5331 ((eq arg :deadline)
5332 (setq rtn (org-agenda-get-deadlines))
5333 (setq deadline-results (copy-sequence rtn))
5334 (setq results (append results rtn)))
5335 ((eq arg :deadline*)
5336 (setq rtn (org-agenda-get-deadlines t))
5337 (setq deadline-results (copy-sequence rtn))
5338 (setq results (append results rtn))))))))
5339 results))))
5341 (defsubst org-em (x y list)
5342 "Is X or Y a member of LIST?"
5343 (or (memq x list) (memq y list)))
5345 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5346 (defvar org-agenda-sorting-strategy-selected nil)
5348 (defun org-agenda-get-todos ()
5349 "Return the TODO information for agenda display."
5350 (let* ((props (list 'face nil
5351 'done-face 'org-agenda-done
5352 'org-not-done-regexp org-not-done-regexp
5353 'org-todo-regexp org-todo-regexp
5354 'org-complex-heading-regexp org-complex-heading-regexp
5355 'mouse-face 'highlight
5356 'help-echo
5357 (format "mouse-2 or RET jump to org file %s"
5358 (abbreviate-file-name buffer-file-name))))
5359 (regexp (format org-heading-keyword-regexp-format
5360 (cond
5361 ((and org-select-this-todo-keyword
5362 (equal org-select-this-todo-keyword "*"))
5363 org-todo-regexp)
5364 (org-select-this-todo-keyword
5365 (concat "\\("
5366 (mapconcat 'identity
5367 (org-split-string
5368 org-select-this-todo-keyword
5369 "|")
5370 "\\|") "\\)"))
5371 (t org-not-done-regexp))))
5372 marker priority category category-pos level tags todo-state ts-date ts-date-type
5373 ee txt beg end inherited-tags todo-state-end-pos)
5374 (goto-char (point-min))
5375 (while (re-search-forward regexp nil t)
5376 (catch :skip
5377 (save-match-data
5378 (beginning-of-line)
5379 (org-agenda-skip)
5380 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5381 (unless (and (setq todo-state (org-get-todo-state))
5382 (setq todo-state-end-pos (match-end 2)))
5383 (goto-char end)
5384 (throw :skip nil))
5385 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5386 (goto-char (1+ beg))
5387 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5388 (throw :skip nil)))
5389 (goto-char (match-beginning 2))
5390 (setq marker (org-agenda-new-marker (match-beginning 0))
5391 category (org-get-category)
5392 ts-date (let (ts)
5393 (save-match-data
5394 (cond ((org-em 'scheduled-up 'scheduled-down
5395 org-agenda-sorting-strategy-selected)
5396 (setq ts (org-entry-get (point) "SCHEDULED")
5397 ts-date-type " scheduled"))
5398 ((org-em 'deadline-up 'deadline-down
5399 org-agenda-sorting-strategy-selected)
5400 (setq ts (org-entry-get (point) "DEADLINE")
5401 ts-date-type " deadline"))
5402 ((org-em 'ts-up 'ts-down
5403 org-agenda-sorting-strategy-selected)
5404 (setq ts (org-entry-get (point) "TIMESTAMP")
5405 ts-date-type " timestamp"))
5406 ((org-em 'tsia-up 'tsia-down
5407 org-agenda-sorting-strategy-selected)
5408 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5409 ts-date-type " timestamp_ia"))
5410 ((org-em 'timestamp-up 'timestamp-down
5411 org-agenda-sorting-strategy-selected)
5412 (setq ts (or (org-entry-get (point) "SCHEDULED")
5413 (org-entry-get (point) "DEADLINE")
5414 (org-entry-get (point) "TIMESTAMP")
5415 (org-entry-get (point) "TIMESTAMP_IA"))
5416 ts-date-type ""))
5417 (t (setq ts-date-type "")))
5418 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5419 category-pos (get-text-property (point) 'org-category-position)
5420 txt (org-trim
5421 (buffer-substring (match-beginning 2) (match-end 0)))
5422 inherited-tags
5423 (or (eq org-agenda-show-inherited-tags 'always)
5424 (and (listp org-agenda-show-inherited-tags)
5425 (memq 'todo org-agenda-show-inherited-tags))
5426 (and (eq org-agenda-show-inherited-tags t)
5427 (or (eq org-agenda-use-tag-inheritance t)
5428 (memq 'todo org-agenda-use-tag-inheritance))))
5429 tags (org-get-tags-at nil (not inherited-tags))
5430 level (make-string (org-reduced-level (org-outline-level)) ? )
5431 txt (org-agenda-format-item "" txt level category tags t)
5432 priority (1+ (org-get-priority txt)))
5433 (org-add-props txt props
5434 'org-marker marker 'org-hd-marker marker
5435 'priority priority 'org-category category
5436 'level level
5437 'ts-date ts-date
5438 'org-category-position category-pos
5439 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5440 (push txt ee)
5441 (if org-agenda-todo-list-sublevels
5442 (goto-char todo-state-end-pos)
5443 (org-end-of-subtree 'invisible))))
5444 (nreverse ee)))
5446 (defun org-agenda-todo-custom-ignore-p (time n)
5447 "Check whether timestamp is farther away than n number of days.
5448 This function is invoked if `org-agenda-todo-ignore-deadlines',
5449 `org-agenda-todo-ignore-scheduled' or
5450 `org-agenda-todo-ignore-timestamp' is set to an integer."
5451 (let ((days (org-time-stamp-to-now
5452 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5453 (if (>= n 0)
5454 (>= days n)
5455 (<= days n))))
5457 ;;;###autoload
5458 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5459 (&optional end)
5460 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5461 (when (or org-agenda-todo-ignore-with-date
5462 org-agenda-todo-ignore-scheduled
5463 org-agenda-todo-ignore-deadlines
5464 org-agenda-todo-ignore-timestamp)
5465 (setq end (or end (save-excursion (outline-next-heading) (point))))
5466 (save-excursion
5467 (or (and org-agenda-todo-ignore-with-date
5468 (re-search-forward org-ts-regexp end t))
5469 (and org-agenda-todo-ignore-scheduled
5470 (re-search-forward org-scheduled-time-regexp end t)
5471 (cond
5472 ((eq org-agenda-todo-ignore-scheduled 'future)
5473 (> (org-time-stamp-to-now
5474 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5475 ((eq org-agenda-todo-ignore-scheduled 'past)
5476 (<= (org-time-stamp-to-now
5477 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5478 ((numberp org-agenda-todo-ignore-scheduled)
5479 (org-agenda-todo-custom-ignore-p
5480 (match-string 1) org-agenda-todo-ignore-scheduled))
5481 (t)))
5482 (and org-agenda-todo-ignore-deadlines
5483 (re-search-forward org-deadline-time-regexp end t)
5484 (cond
5485 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5486 ((eq org-agenda-todo-ignore-deadlines 'far)
5487 (not (org-deadline-close (match-string 1))))
5488 ((eq org-agenda-todo-ignore-deadlines 'future)
5489 (> (org-time-stamp-to-now
5490 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5491 ((eq org-agenda-todo-ignore-deadlines 'past)
5492 (<= (org-time-stamp-to-now
5493 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5494 ((numberp org-agenda-todo-ignore-deadlines)
5495 (org-agenda-todo-custom-ignore-p
5496 (match-string 1) org-agenda-todo-ignore-deadlines))
5497 (t (org-deadline-close (match-string 1)))))
5498 (and org-agenda-todo-ignore-timestamp
5499 (let ((buffer (current-buffer))
5500 (regexp
5501 (concat
5502 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5503 (start (point)))
5504 ;; Copy current buffer into a temporary one
5505 (with-temp-buffer
5506 (insert-buffer-substring buffer start end)
5507 (goto-char (point-min))
5508 ;; Delete SCHEDULED and DEADLINE items
5509 (while (re-search-forward regexp end t)
5510 (delete-region (match-beginning 0) (match-end 0)))
5511 (goto-char (point-min))
5512 ;; No search for timestamp left
5513 (when (re-search-forward org-ts-regexp nil t)
5514 (cond
5515 ((eq org-agenda-todo-ignore-timestamp 'future)
5516 (> (org-time-stamp-to-now
5517 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5518 ((eq org-agenda-todo-ignore-timestamp 'past)
5519 (<= (org-time-stamp-to-now
5520 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5521 ((numberp org-agenda-todo-ignore-timestamp)
5522 (org-agenda-todo-custom-ignore-p
5523 (match-string 1) org-agenda-todo-ignore-timestamp))
5524 (t))))))))))
5526 (defun org-agenda-get-timestamps (&optional deadline-results)
5527 "Return the date stamp information for agenda display."
5528 (let* ((props (list 'face 'org-agenda-calendar-event
5529 'org-not-done-regexp org-not-done-regexp
5530 'org-todo-regexp org-todo-regexp
5531 'org-complex-heading-regexp org-complex-heading-regexp
5532 'mouse-face 'highlight
5533 'help-echo
5534 (format "mouse-2 or RET jump to org file %s"
5535 (abbreviate-file-name buffer-file-name))))
5536 (d1 (calendar-absolute-from-gregorian date))
5538 (deadline-position-alist
5539 (mapcar (lambda (a) (and (setq mm (get-text-property
5540 0 'org-hd-marker a))
5541 (cons (marker-position mm) a)))
5542 deadline-results))
5543 (remove-re org-ts-regexp)
5544 (regexp
5545 (concat
5546 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5547 (regexp-quote
5548 (substring
5549 (format-time-string
5550 (car org-time-stamp-formats)
5551 (apply 'encode-time ; DATE bound by calendar
5552 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5553 1 11))
5554 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5555 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5556 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5557 donep tmp priority category category-pos level ee txt timestr tags
5558 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5559 inherited-tags ts-date)
5560 (goto-char (point-min))
5561 (while (setq end-of-match (re-search-forward regexp nil t))
5562 (setq b0 (match-beginning 0)
5563 b3 (match-beginning 3) e3 (match-end 3)
5564 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5565 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5566 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5567 (member todo-state
5568 org-agenda-repeating-timestamp-show-all)))
5569 (catch :skip
5570 (and (org-at-date-range-p) (throw :skip nil))
5571 (org-agenda-skip)
5572 (if (and (match-end 1)
5573 (not (= d1 (org-time-string-to-absolute
5574 (match-string 1) d1 nil show-all
5575 (current-buffer) b0))))
5576 (throw :skip nil))
5577 (if (and e3
5578 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5579 (throw :skip nil))
5580 (setq tmp (buffer-substring (max (point-min)
5581 (- b0 org-ds-keyword-length))
5583 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5584 inactivep (= (char-after b0) ?\[)
5585 deadlinep (string-match org-deadline-regexp tmp)
5586 scheduledp (string-match org-scheduled-regexp tmp)
5587 closedp (and org-agenda-include-inactive-timestamps
5588 (string-match org-closed-string tmp))
5589 clockp (and org-agenda-include-inactive-timestamps
5590 (or (string-match org-clock-string tmp)
5591 (string-match "]-+\\'" tmp)))
5592 warntime (get-text-property (point) 'org-appt-warntime)
5593 donep (member todo-state org-done-keywords))
5594 (if (or scheduledp deadlinep closedp clockp
5595 (and donep org-agenda-skip-timestamp-if-done))
5596 (throw :skip t))
5597 (if (string-match ">" timestr)
5598 ;; substring should only run to end of time stamp
5599 (setq timestr (substring timestr 0 (match-end 0))))
5600 (setq marker (org-agenda-new-marker b0)
5601 category (org-get-category b0)
5602 category-pos (get-text-property b0 'org-category-position))
5603 (save-excursion
5604 (if (not (re-search-backward org-outline-regexp-bol nil t))
5605 (throw :skip nil)
5606 (goto-char (match-beginning 0))
5607 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5608 (assoc (point) deadline-position-alist))
5609 (throw :skip nil))
5610 (setq hdmarker (org-agenda-new-marker)
5611 inherited-tags
5612 (or (eq org-agenda-show-inherited-tags 'always)
5613 (and (listp org-agenda-show-inherited-tags)
5614 (memq 'agenda org-agenda-show-inherited-tags))
5615 (and (eq org-agenda-show-inherited-tags t)
5616 (or (eq org-agenda-use-tag-inheritance t)
5617 (memq 'agenda org-agenda-use-tag-inheritance))))
5618 tags (org-get-tags-at nil (not inherited-tags))
5619 level (make-string (org-reduced-level (org-outline-level)) ? ))
5620 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5621 (setq head (or (match-string 1) ""))
5622 (setq txt (org-agenda-format-item
5623 (if inactivep org-agenda-inactive-leader nil)
5624 head level category tags timestr
5625 remove-re habitp)))
5626 (setq priority (org-get-priority txt))
5627 (org-add-props txt props 'priority priority
5628 'org-marker marker 'org-hd-marker hdmarker
5629 'org-category category 'date date
5630 'level level
5631 'ts-date
5632 (ignore-errors (org-time-string-to-absolute timestr))
5633 'org-category-position category-pos
5634 'todo-state todo-state
5635 'warntime warntime
5636 'type "timestamp")
5637 (push txt ee))
5638 (if org-agenda-skip-additional-timestamps-same-entry
5639 (outline-next-heading)
5640 (goto-char end-of-match))))
5641 (nreverse ee)))
5643 (defun org-agenda-get-sexps ()
5644 "Return the sexp information for agenda display."
5645 (require 'diary-lib)
5646 (let* ((props (list 'face 'org-agenda-calendar-sexp
5647 'mouse-face 'highlight
5648 'help-echo
5649 (format "mouse-2 or RET jump to org file %s"
5650 (abbreviate-file-name buffer-file-name))))
5651 (regexp "^&?%%(")
5652 marker category extra category-pos level ee txt tags entry
5653 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5654 (goto-char (point-min))
5655 (while (re-search-forward regexp nil t)
5656 (catch :skip
5657 (org-agenda-skip)
5658 (setq beg (match-beginning 0))
5659 (goto-char (1- (match-end 0)))
5660 (setq b (point))
5661 (forward-sexp 1)
5662 (setq sexp (buffer-substring b (point)))
5663 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5664 (org-trim (match-string 1))
5665 ""))
5666 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5667 (when result
5668 (setq marker (org-agenda-new-marker beg)
5669 level (make-string (org-reduced-level (org-outline-level)) ? )
5670 category (org-get-category beg)
5671 category-pos (get-text-property beg 'org-category-position)
5672 inherited-tags
5673 (or (eq org-agenda-show-inherited-tags 'always)
5674 (and (listp org-agenda-show-inherited-tags)
5675 (memq 'agenda org-agenda-show-inherited-tags))
5676 (and (eq org-agenda-show-inherited-tags t)
5677 (or (eq org-agenda-use-tag-inheritance t)
5678 (memq 'agenda org-agenda-use-tag-inheritance))))
5679 tags (org-get-tags-at nil (not inherited-tags))
5680 todo-state (org-get-todo-state)
5681 warntime (get-text-property (point) 'org-appt-warntime)
5682 extra nil)
5684 (dolist (r (if (stringp result)
5685 (list result)
5686 result)) ;; we expect a list here
5687 (when (and org-agenda-diary-sexp-prefix
5688 (string-match org-agenda-diary-sexp-prefix r))
5689 (setq extra (match-string 0 r)
5690 r (replace-match "" nil nil r)))
5691 (if (string-match "\\S-" r)
5692 (setq txt r)
5693 (setq txt "SEXP entry returned empty string"))
5694 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5695 (org-add-props txt props 'org-marker marker
5696 'org-category category 'date date 'todo-state todo-state
5697 'org-category-position category-pos 'tags tags
5698 'level level
5699 'type "sexp" 'warntime warntime)
5700 (push txt ee)))))
5701 (nreverse ee)))
5703 ;; Calendar sanity: define some functions that are independent of
5704 ;; `calendar-date-style'.
5705 ;; Normally I would like to use ISO format when calling the diary functions,
5706 ;; but to make sure we still have Emacs 22 compatibility we bind
5707 ;; also `european-calendar-style' and use european format
5708 (defun org-anniversary (year month day &optional mark)
5709 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5710 (org-no-warnings
5711 (let ((calendar-date-style 'european) (european-calendar-style t))
5712 (diary-anniversary day month year mark))))
5713 (defun org-cyclic (N year month day &optional mark)
5714 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5715 (org-no-warnings
5716 (let ((calendar-date-style 'european) (european-calendar-style t))
5717 (diary-cyclic N day month year mark))))
5718 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5719 "Like `diary-block', but with fixed (ISO) order of arguments."
5720 (org-no-warnings
5721 (let ((calendar-date-style 'european) (european-calendar-style t))
5722 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5723 (defun org-date (year month day &optional mark)
5724 "Like `diary-date', but with fixed (ISO) order of arguments."
5725 (org-no-warnings
5726 (let ((calendar-date-style 'european) (european-calendar-style t))
5727 (diary-date day month year mark))))
5729 ;; Define the` org-class' function
5730 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5731 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5732 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5733 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5734 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5735 `holidays', then any date that is known by the Emacs calendar to be a
5736 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5737 then those holidays will be skipped."
5738 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5739 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5740 (d (calendar-absolute-from-gregorian date))
5741 (h (when skip-weeks (calendar-check-holidays date))))
5742 (and
5743 (<= date1 d)
5744 (<= d date2)
5745 (= (calendar-day-of-week date) dayname)
5746 (or (not skip-weeks)
5747 (progn
5748 (require 'cal-iso)
5749 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5750 (not (or (and h (memq 'holidays skip-weeks))
5751 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5752 entry)))
5754 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5755 "Like `org-class', but honor `calendar-date-style'.
5756 The order of the first 2 times 3 arguments depends on the variable
5757 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5758 So for American calendars, give this as MONTH DAY YEAR, for European as
5759 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5760 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5761 is any number of ISO weeks in the block period for which the item should
5762 be skipped.
5764 This function is here only for backward compatibility and it is deprecated,
5765 please use `org-class' instead."
5766 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5767 (date2 (org-order-calendar-date-args m2 d2 y2)))
5768 (org-class
5769 (nth 2 date1) (car date1) (nth 1 date1)
5770 (nth 2 date2) (car date2) (nth 1 date2)
5771 dayname skip-weeks)))
5772 (make-obsolete 'org-diary-class 'org-class "")
5774 (defalias 'org-get-closed 'org-agenda-get-progress)
5775 (defun org-agenda-get-progress ()
5776 "Return the logged TODO entries for agenda display."
5777 (let* ((props (list 'mouse-face 'highlight
5778 'org-not-done-regexp org-not-done-regexp
5779 'org-todo-regexp org-todo-regexp
5780 'org-complex-heading-regexp org-complex-heading-regexp
5781 'help-echo
5782 (format "mouse-2 or RET jump to org file %s"
5783 (abbreviate-file-name buffer-file-name))))
5784 (items (if (consp org-agenda-show-log-scoped)
5785 org-agenda-show-log-scoped
5786 (if (eq org-agenda-show-log-scoped 'clockcheck)
5787 '(clock)
5788 org-agenda-log-mode-items)))
5789 (parts
5790 (delq nil
5791 (list
5792 (if (memq 'closed items) (concat "\\<" org-closed-string))
5793 (if (memq 'clock items) (concat "\\<" org-clock-string))
5794 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5795 (parts-re (if parts (mapconcat 'identity parts "\\|")
5796 (error "`org-agenda-log-mode-items' is empty")))
5797 (regexp (concat
5798 "\\(" parts-re "\\)"
5799 " *\\["
5800 (regexp-quote
5801 (substring
5802 (format-time-string
5803 (car org-time-stamp-formats)
5804 (apply 'encode-time ; DATE bound by calendar
5805 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5806 1 11))))
5807 (org-agenda-search-headline-for-time nil)
5808 marker hdmarker priority category category-pos level tags closedp
5809 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5810 (goto-char (point-min))
5811 (while (re-search-forward regexp nil t)
5812 (catch :skip
5813 (org-agenda-skip)
5814 (setq marker (org-agenda-new-marker (match-beginning 0))
5815 closedp (equal (match-string 1) org-closed-string)
5816 statep (equal (string-to-char (match-string 1)) ?-)
5817 clockp (not (or closedp statep))
5818 state (and statep (match-string 2))
5819 category (org-get-category (match-beginning 0))
5820 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5821 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5822 (when (string-match "\\]" timestr)
5823 ;; substring should only run to end of time stamp
5824 (setq rest (substring timestr (match-end 0))
5825 timestr (substring timestr 0 (match-end 0)))
5826 (if (and (not closedp) (not statep)
5827 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5828 rest))
5829 (progn (setq timestr (concat (substring timestr 0 -1)
5830 "-" (match-string 1 rest) "]"))
5831 (setq clocked (match-string 2 rest)))
5832 (setq clocked "-")))
5833 (save-excursion
5834 (setq extra
5835 (cond
5836 ((not org-agenda-log-mode-add-notes) nil)
5837 (statep
5838 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5839 (match-string 1)))
5840 (clockp
5841 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5842 (match-string 1)))))
5843 (if (not (re-search-backward org-outline-regexp-bol nil t))
5844 (throw :skip nil)
5845 (goto-char (match-beginning 0))
5846 (setq hdmarker (org-agenda-new-marker)
5847 inherited-tags
5848 (or (eq org-agenda-show-inherited-tags 'always)
5849 (and (listp org-agenda-show-inherited-tags)
5850 (memq 'todo org-agenda-show-inherited-tags))
5851 (and (eq org-agenda-show-inherited-tags t)
5852 (or (eq org-agenda-use-tag-inheritance t)
5853 (memq 'todo org-agenda-use-tag-inheritance))))
5854 tags (org-get-tags-at nil (not inherited-tags))
5855 level (make-string (org-reduced-level (org-outline-level)) ? ))
5856 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5857 (setq txt (match-string 1))
5858 (when extra
5859 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5860 (setq txt (concat (substring txt 0 (match-beginning 1))
5861 " - " extra " " (match-string 2 txt)))
5862 (setq txt (concat txt " - " extra))))
5863 (setq txt (org-agenda-format-item
5864 (cond
5865 (closedp "Closed: ")
5866 (statep (concat "State: (" state ")"))
5867 (t (concat "Clocked: (" clocked ")")))
5868 txt level category tags timestr)))
5869 (setq priority 100000)
5870 (org-add-props txt props
5871 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5872 'priority priority 'org-category category
5873 'org-category-position category-pos
5874 'level level
5875 'type "closed" 'date date
5876 'undone-face 'org-warning 'done-face 'org-agenda-done)
5877 (push txt ee))
5878 (goto-char (point-at-eol))))
5879 (nreverse ee)))
5881 (defun org-agenda-show-clocking-issues ()
5882 "Add overlays, showing issues with clocking.
5883 See also the user option `org-agenda-clock-consistency-checks'."
5884 (interactive)
5885 (let* ((org-time-clocksum-use-effort-durations nil)
5886 (pl org-agenda-clock-consistency-checks)
5887 (re (concat "^[ \t]*"
5888 org-clock-string
5889 "[ \t]+"
5890 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5891 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5892 (tlstart 0.)
5893 (tlend 0.)
5894 (maxtime (org-hh:mm-string-to-minutes
5895 (or (plist-get pl :max-duration) "24:00")))
5896 (mintime (org-hh:mm-string-to-minutes
5897 (or (plist-get pl :min-duration) 0)))
5898 (maxgap (org-hh:mm-string-to-minutes
5899 ;; default 30:00 means never complain
5900 (or (plist-get pl :max-gap) "30:00")))
5901 (gapok (mapcar 'org-hh:mm-string-to-minutes
5902 (plist-get pl :gap-ok-around)))
5903 (def-face (or (plist-get pl :default-face)
5904 '((:background "DarkRed") (:foreground "white"))))
5905 issue face m te ts dt ov)
5906 (goto-char (point-min))
5907 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5908 (setq issue nil face def-face)
5909 (catch 'next
5910 (setq m (org-get-at-bol 'org-marker)
5911 te nil ts nil)
5912 (unless (and m (markerp m))
5913 (setq issue "No valid clock line") (throw 'next t))
5914 (org-with-point-at m
5915 (save-excursion
5916 (goto-char (point-at-bol))
5917 (unless (looking-at re)
5918 (error "No valid Clock line")
5919 (throw 'next t))
5920 (unless (match-end 3)
5921 (setq issue "No end time"
5922 face (or (plist-get pl :no-end-time-face) face))
5923 (throw 'next t))
5924 (setq ts (match-string 1)
5925 te (match-string 3)
5926 ts (org-float-time
5927 (apply 'encode-time (org-parse-time-string ts)))
5928 te (org-float-time
5929 (apply 'encode-time (org-parse-time-string te)))
5930 dt (- te ts))))
5931 (cond
5932 ((> dt (* 60 maxtime))
5933 ;; a very long clocking chunk
5934 (setq issue (format "Clocking interval is very long: %s"
5935 (org-minutes-to-clocksum-string
5936 (floor (/ (float dt) 60.))))
5937 face (or (plist-get pl :long-face) face)))
5938 ((< dt (* 60 mintime))
5939 ;; a very short clocking chunk
5940 (setq issue (format "Clocking interval is very short: %s"
5941 (org-minutes-to-clocksum-string
5942 (floor (/ (float dt) 60.))))
5943 face (or (plist-get pl :short-face) face)))
5944 ((and (> tlend 0) (< ts tlend))
5945 ;; Two clock entries are overlapping
5946 (setq issue (format "Clocking overlap: %d minutes"
5947 (/ (- tlend ts) 60))
5948 face (or (plist-get pl :overlap-face) face)))
5949 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5950 ;; There is a gap, lets see if we need to report it
5951 (unless (org-agenda-check-clock-gap tlend ts gapok)
5952 (setq issue (format "Clocking gap: %d minutes"
5953 (/ (- ts tlend) 60))
5954 face (or (plist-get pl :gap-face) face))))
5955 (t nil)))
5956 (setq tlend (or te tlend) tlstart (or ts tlstart))
5957 (when issue
5958 ;; OK, there was some issue, add an overlay to show the issue
5959 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5960 (overlay-put ov 'before-string
5961 (concat
5962 (org-add-props
5963 (format "%-43s" (concat " " issue))
5965 'face face)
5966 "\n"))
5967 (overlay-put ov 'evaporate t)))))
5969 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5970 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5971 (catch 'exit
5972 (unless ok-list
5973 ;; there are no OK times for gaps...
5974 (throw 'exit nil))
5975 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5976 ;; This is more than 24 hours, so it is OK.
5977 ;; because we have at least one OK time, that must be in the
5978 ;; 24 hour interval.
5979 (throw 'exit t))
5980 ;; We have a shorter gap.
5981 ;; Now we have to get the minute of the day when these times are
5982 (let* ((t1dec (decode-time (seconds-to-time t1)))
5983 (t2dec (decode-time (seconds-to-time t2)))
5984 ;; compute the minute on the day
5985 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5986 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5987 (when (< min2 min1)
5988 ;; if min2 is smaller than min1, this means it is on the next day.
5989 ;; Wrap it to after midnight.
5990 (setq min2 (+ min2 1440)))
5991 ;; Now check if any of the OK times is in the gap
5992 (mapc (lambda (x)
5993 ;; Wrap the time to after midnight if necessary
5994 (if (< x min1) (setq x (+ x 1440)))
5995 ;; Check if in interval
5996 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5997 ok-list)
5998 ;; Nope, this gap is not OK
5999 nil)))
6001 (defun org-agenda-get-deadlines (&optional with-hour)
6002 "Return the deadline information for agenda display.
6003 When WITH-HOUR is non-nil, only return deadlines with an hour
6004 specification like [h]h:mm."
6005 (let* ((props (list 'mouse-face 'highlight
6006 'org-not-done-regexp org-not-done-regexp
6007 'org-todo-regexp org-todo-regexp
6008 'org-complex-heading-regexp org-complex-heading-regexp
6009 'help-echo
6010 (format "mouse-2 or RET jump to org file %s"
6011 (abbreviate-file-name buffer-file-name))))
6012 (regexp (if with-hour
6013 org-deadline-time-hour-regexp
6014 org-deadline-time-regexp))
6015 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6016 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6017 (dl0 (car org-agenda-deadline-leaders))
6018 (dl1 (nth 1 org-agenda-deadline-leaders))
6019 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6020 d2 diff dfrac wdays pos pos1 category category-pos level
6021 tags suppress-prewarning ee txt head face s todo-state
6022 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6023 (goto-char (point-min))
6024 (while (re-search-forward regexp nil t)
6025 (catch :skip
6026 (org-agenda-skip)
6027 (setq s (match-string 1)
6028 txt nil
6029 pos (1- (match-beginning 1))
6030 todo-state (save-match-data (org-get-todo-state))
6031 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6032 (member todo-state
6033 org-agenda-repeating-timestamp-show-all))
6034 d2 (org-time-string-to-absolute
6035 s d1 'past show-all (current-buffer) pos)
6036 diff (- d2 d1))
6037 (setq suppress-prewarning
6038 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6039 (let ((item (buffer-substring (point-at-bol)
6040 (point-at-eol))))
6041 (save-match-data
6042 (and (string-match
6043 org-scheduled-time-regexp item)
6044 (match-string 1 item)))))))
6045 (cond
6046 ((not ds) nil)
6047 ;; The current item has a scheduled date (in ds), so
6048 ;; evaluate its prewarning lead time.
6049 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6050 ;; Use global prewarning-restart lead time.
6051 org-agenda-skip-deadline-prewarning-if-scheduled)
6052 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6053 'pre-scheduled)
6054 ;; Set prewarning to no earlier than scheduled.
6055 (min (- d2 (org-time-string-to-absolute
6056 ds d1 'past show-all (current-buffer) pos))
6057 org-deadline-warning-days))
6058 ;; Set prewarning to deadline.
6059 (t 0))))
6060 (setq wdays (if suppress-prewarning
6061 (let ((org-deadline-warning-days suppress-prewarning))
6062 (org-get-wdays s))
6063 (org-get-wdays s))
6064 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6065 upcomingp (and todayp (> diff 0)))
6066 ;; When to show a deadline in the calendar:
6067 ;; If the expiration is within wdays warning time.
6068 ;; Past-due deadlines are only shown on the current date
6069 (if (and (or (and (<= diff wdays)
6070 (and todayp (not org-agenda-only-exact-dates)))
6071 (= diff 0)))
6072 (save-excursion
6073 ;; (setq todo-state (org-get-todo-state))
6074 (setq donep (member todo-state org-done-keywords))
6075 (if (and donep
6076 (or org-agenda-skip-deadline-if-done
6077 (not (= diff 0))))
6078 (setq txt nil)
6079 (setq category (org-get-category)
6080 warntime (get-text-property (point) 'org-appt-warntime)
6081 category-pos (get-text-property (point) 'org-category-position))
6082 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6083 (throw :skip nil)
6084 (goto-char (match-end 0))
6085 (setq pos1 (match-beginning 0))
6086 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6087 (setq inherited-tags
6088 (or (eq org-agenda-show-inherited-tags 'always)
6089 (and (listp org-agenda-show-inherited-tags)
6090 (memq 'agenda org-agenda-show-inherited-tags))
6091 (and (eq org-agenda-show-inherited-tags t)
6092 (or (eq org-agenda-use-tag-inheritance t)
6093 (memq 'agenda org-agenda-use-tag-inheritance))))
6094 tags (org-get-tags-at pos1 (not inherited-tags)))
6095 (setq head (buffer-substring
6096 (point)
6097 (progn (skip-chars-forward "^\r\n")
6098 (point))))
6099 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6100 (setq timestr
6101 (concat (substring s (match-beginning 1)) " "))
6102 (setq timestr 'time))
6103 (setq txt (org-agenda-format-item
6104 (cond ((= diff 0) dl0)
6105 ((> diff 0)
6106 (if (functionp dl1)
6107 (funcall dl1 diff date)
6108 (format dl1 diff)))
6110 (if (functionp dl2)
6111 (funcall dl2 diff date)
6112 (format dl2 (if (string= dl2 dl1)
6113 diff (abs diff))))))
6114 head level category tags
6115 (if (not (= diff 0)) nil timestr)))))
6116 (when txt
6117 (setq face (org-agenda-deadline-face dfrac))
6118 (org-add-props txt props
6119 'org-marker (org-agenda-new-marker pos)
6120 'warntime warntime
6121 'level level
6122 'ts-date d2
6123 'org-hd-marker (org-agenda-new-marker pos1)
6124 'priority (+ (- diff)
6125 (org-get-priority txt))
6126 'org-category category
6127 'org-category-position category-pos
6128 'todo-state todo-state
6129 'type (if upcomingp "upcoming-deadline" "deadline")
6130 'date (if upcomingp date d2)
6131 'face (if donep 'org-agenda-done face)
6132 'undone-face face 'done-face 'org-agenda-done)
6133 (push txt ee))))))
6134 (nreverse ee)))
6136 (defun org-agenda-deadline-face (fraction)
6137 "Return the face to displaying a deadline item.
6138 FRACTION is what fraction of the head-warning time has passed."
6139 (let ((faces org-agenda-deadline-faces) f)
6140 (catch 'exit
6141 (while (setq f (pop faces))
6142 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6144 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6145 "Return the scheduled information for agenda display.
6146 When WITH-HOUR is non-nil, only return scheduled items with
6147 an hour specification like [h]h:mm."
6148 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6149 'org-todo-regexp org-todo-regexp
6150 'org-complex-heading-regexp org-complex-heading-regexp
6151 'done-face 'org-agenda-done
6152 'mouse-face 'highlight
6153 'help-echo
6154 (format "mouse-2 or RET jump to org file %s"
6155 (abbreviate-file-name buffer-file-name))))
6156 (regexp (if with-hour
6157 org-scheduled-time-hour-regexp
6158 org-scheduled-time-regexp))
6159 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6160 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6162 (deadline-position-alist
6163 (mapcar (lambda (a) (and (setq mm (get-text-property
6164 0 'org-hd-marker a))
6165 (cons (marker-position mm) a)))
6166 deadline-results))
6167 d2 diff pos pos1 category category-pos level tags donep
6168 ee txt head pastschedp todo-state face timestr s habitp show-all
6169 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6170 ddays)
6171 (goto-char (point-min))
6172 (while (re-search-forward regexp nil t)
6173 (catch :skip
6174 (org-agenda-skip)
6175 (setq s (match-string 1)
6176 txt nil
6177 pos (1- (match-beginning 1))
6178 todo-state (save-match-data (org-get-todo-state))
6179 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6180 (member todo-state
6181 org-agenda-repeating-timestamp-show-all))
6182 d2 (org-time-string-to-absolute
6183 s d1 'past show-all (current-buffer) pos)
6184 diff (- d2 d1)
6185 warntime (get-text-property (point) 'org-appt-warntime))
6186 (setq pastschedp (and todayp (< diff 0)))
6187 (setq did-habit-check-p nil)
6188 (setq suppress-delay
6189 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6190 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6191 (save-match-data
6192 (and (string-match
6193 org-deadline-time-regexp item)
6194 (match-string 1 item)))))))
6195 (cond
6196 ((not ds) nil)
6197 ;; The current item has a deadline date (in ds), so
6198 ;; evaluate its delay time.
6199 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6200 ;; Use global delay time.
6201 (- org-agenda-skip-scheduled-delay-if-deadline))
6202 ((eq org-agenda-skip-scheduled-delay-if-deadline
6203 'post-deadline)
6204 ;; Set delay to no later than deadline.
6205 (min (- d2 (org-time-string-to-absolute
6206 ds d1 'past show-all (current-buffer) pos))
6207 org-scheduled-delay-days))
6208 (t 0))))
6209 (setq ddays (if suppress-delay
6210 (let ((org-scheduled-delay-days suppress-delay))
6211 (org-get-wdays s t t))
6212 (org-get-wdays s t)))
6213 ;; Use a delay of 0 when there is a repeater and the delay is
6214 ;; of the form --3d
6215 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6216 (< (org-time-string-to-absolute s)
6217 (org-time-string-to-absolute
6218 s d2 'past nil (current-buffer) pos)))
6219 (setq ddays 0))
6220 ;; When to show a scheduled item in the calendar:
6221 ;; If it is on or past the date.
6222 (when (or (and (> ddays 0) (= diff (- ddays)))
6223 (and (zerop ddays) (= diff 0))
6224 (and (< (+ diff ddays) 0)
6225 (< (abs diff) org-scheduled-past-days)
6226 (and todayp (not org-agenda-only-exact-dates)))
6227 ;; org-is-habit-p uses org-entry-get, which is expansive
6228 ;; so we go extra mile to only call it once
6229 (and todayp
6230 (boundp 'org-habit-show-all-today)
6231 org-habit-show-all-today
6232 (setq did-habit-check-p t)
6233 (setq habitp (and (functionp 'org-is-habit-p)
6234 (org-is-habit-p)))))
6235 (save-excursion
6236 (setq donep (member todo-state org-done-keywords))
6237 (if (and donep
6238 (or org-agenda-skip-scheduled-if-done
6239 (not (= diff 0))
6240 (and (functionp 'org-is-habit-p)
6241 (org-is-habit-p))))
6242 (setq txt nil)
6243 (setq habitp (if did-habit-check-p habitp
6244 (and (functionp 'org-is-habit-p)
6245 (org-is-habit-p))))
6246 (setq category (org-get-category)
6247 category-pos (get-text-property (point) 'org-category-position))
6248 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6249 'repeated-after-deadline)
6250 (org-get-deadline-time (point))
6251 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6252 (throw :skip nil))
6253 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6254 (throw :skip nil)
6255 (goto-char (match-end 0))
6256 (setq pos1 (match-beginning 0))
6257 (if habitp
6258 (if (or (not org-habit-show-habits)
6259 (and (not todayp)
6260 (boundp 'org-habit-show-habits-only-for-today)
6261 org-habit-show-habits-only-for-today))
6262 (throw :skip nil))
6263 (if (and
6264 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6265 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6266 pastschedp))
6267 (setq mm (assoc pos1 deadline-position-alist)))
6268 (throw :skip nil)))
6269 (setq inherited-tags
6270 (or (eq org-agenda-show-inherited-tags 'always)
6271 (and (listp org-agenda-show-inherited-tags)
6272 (memq 'agenda org-agenda-show-inherited-tags))
6273 (and (eq org-agenda-show-inherited-tags t)
6274 (or (eq org-agenda-use-tag-inheritance t)
6275 (memq 'agenda org-agenda-use-tag-inheritance))))
6277 tags (org-get-tags-at nil (not inherited-tags)))
6278 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6279 (setq head (buffer-substring
6280 (point)
6281 (progn (skip-chars-forward "^\r\n") (point))))
6282 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6283 (setq timestr
6284 (concat (substring s (match-beginning 1)) " "))
6285 (setq timestr 'time))
6286 (setq txt (org-agenda-format-item
6287 (if (= diff 0)
6288 (car org-agenda-scheduled-leaders)
6289 (format (nth 1 org-agenda-scheduled-leaders)
6290 (- 1 diff)))
6291 head level category tags
6292 (if (not (= diff 0)) nil timestr)
6293 nil habitp))))
6294 (when txt
6295 (setq face
6296 (cond
6297 ((and (not habitp) pastschedp)
6298 'org-scheduled-previously)
6299 (todayp 'org-scheduled-today)
6300 (t 'org-scheduled))
6301 habitp (and habitp (org-habit-parse-todo)))
6302 (org-add-props txt props
6303 'undone-face face
6304 'face (if donep 'org-agenda-done face)
6305 'org-marker (org-agenda-new-marker pos)
6306 'org-hd-marker (org-agenda-new-marker pos1)
6307 'type (if pastschedp "past-scheduled" "scheduled")
6308 'date (if pastschedp d2 date)
6309 'ts-date d2
6310 'warntime warntime
6311 'level level
6312 'priority (if habitp
6313 (org-habit-get-priority habitp)
6314 (+ 94 (- 5 diff) (org-get-priority txt)))
6315 'org-category category
6316 'category-position category-pos
6317 'org-habit-p habitp
6318 'todo-state todo-state)
6319 (push txt ee))))))
6320 (nreverse ee)))
6322 (defun org-agenda-get-blocks ()
6323 "Return the date-range information for agenda display."
6324 (let* ((props (list 'face nil
6325 'org-not-done-regexp org-not-done-regexp
6326 'org-todo-regexp org-todo-regexp
6327 'org-complex-heading-regexp org-complex-heading-regexp
6328 'mouse-face 'highlight
6329 'help-echo
6330 (format "mouse-2 or RET jump to org file %s"
6331 (abbreviate-file-name buffer-file-name))))
6332 (regexp org-tr-regexp)
6333 (d0 (calendar-absolute-from-gregorian date))
6334 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6335 level todo-state tags pos head donep inherited-tags)
6336 (goto-char (point-min))
6337 (while (re-search-forward regexp nil t)
6338 (catch :skip
6339 (org-agenda-skip)
6340 (setq pos (point))
6341 (let ((start-time (match-string 1))
6342 (end-time (match-string 2)))
6343 (setq s1 (match-string 1)
6344 s2 (match-string 2)
6345 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6346 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6347 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6348 ;; Only allow days between the limits, because the normal
6349 ;; date stamps will catch the limits.
6350 (save-excursion
6351 (setq todo-state (org-get-todo-state))
6352 (setq donep (member todo-state org-done-keywords))
6353 (if (and donep org-agenda-skip-timestamp-if-done)
6354 (throw :skip t))
6355 (setq marker (org-agenda-new-marker (point)))
6356 (setq category (org-get-category)
6357 category-pos (get-text-property (point) 'org-category-position))
6358 (if (not (re-search-backward org-outline-regexp-bol nil t))
6359 (throw :skip nil)
6360 (goto-char (match-beginning 0))
6361 (setq hdmarker (org-agenda-new-marker (point))
6362 inherited-tags
6363 (or (eq org-agenda-show-inherited-tags 'always)
6364 (and (listp org-agenda-show-inherited-tags)
6365 (memq 'agenda org-agenda-show-inherited-tags))
6366 (and (eq org-agenda-show-inherited-tags t)
6367 (or (eq org-agenda-use-tag-inheritance t)
6368 (memq 'agenda org-agenda-use-tag-inheritance))))
6370 tags (org-get-tags-at nil (not inherited-tags)))
6371 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6372 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6373 (setq head (match-string 1))
6374 (let ((remove-re
6375 (if org-agenda-remove-timeranges-from-blocks
6376 (concat
6377 "<" (regexp-quote s1) ".*?>"
6378 "--"
6379 "<" (regexp-quote s2) ".*?>")
6380 nil)))
6381 (setq txt (org-agenda-format-item
6382 (format
6383 (nth (if (= d1 d2) 0 1)
6384 org-agenda-timerange-leaders)
6385 (1+ (- d0 d1)) (1+ (- d2 d1)))
6386 head level category tags
6387 (cond ((and (= d1 d0) (= d2 d0))
6388 (concat "<" start-time ">--<" end-time ">"))
6389 ((= d1 d0)
6390 (concat "<" start-time ">"))
6391 ((= d2 d0)
6392 (concat "<" end-time ">")))
6393 remove-re))))
6394 (org-add-props txt props
6395 'org-marker marker 'org-hd-marker hdmarker
6396 'type "block" 'date date
6397 'level level
6398 'todo-state todo-state
6399 'priority (org-get-priority txt) 'org-category category
6400 'org-category-position category-pos)
6401 (push txt ee))))
6402 (goto-char pos)))
6403 ;; Sort the entries by expiration date.
6404 (nreverse ee)))
6406 ;;; Agenda presentation and sorting
6408 (defvar org-prefix-has-time nil
6409 "A flag, set by `org-compile-prefix-format'.
6410 The flag is set if the currently compiled format contains a `%t'.")
6411 (defvar org-prefix-has-tag nil
6412 "A flag, set by `org-compile-prefix-format'.
6413 The flag is set if the currently compiled format contains a `%T'.")
6414 (defvar org-prefix-has-effort nil
6415 "A flag, set by `org-compile-prefix-format'.
6416 The flag is set if the currently compiled format contains a `%e'.")
6417 (defvar org-prefix-has-breadcrumbs nil
6418 "A flag, set by `org-compile-prefix-format'.
6419 The flag is set if the currently compiled format contains a `%b'.")
6420 (defvar org-prefix-category-length nil
6421 "Used by `org-compile-prefix-format' to remember the category field width.")
6422 (defvar org-prefix-category-max-length nil
6423 "Used by `org-compile-prefix-format' to remember the category field width.")
6425 (defun org-agenda-get-category-icon (category)
6426 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6427 (dolist (entry org-agenda-category-icon-alist)
6428 (when (org-string-match-p (car entry) category)
6429 (if (listp (cadr entry))
6430 (return (cadr entry))
6431 (return (apply 'create-image (cdr entry)))))))
6433 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6434 remove-re habitp)
6435 "Format TXT to be inserted into the agenda buffer.
6436 In particular, add the prefix and corresponding text properties.
6438 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6439 LEVEL may be a string to replace the `%l' specifier.
6440 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6441 category taken from local variable or file name. It will replace the `%c'
6442 specifier in the format.
6443 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6444 TXT for sorting of this entry, and for the `%t' specifier in the format.
6445 When DOTIME is a string, this string is searched for a time before TXT is.
6446 TAGS can be the tags of the headline.
6447 Any match of REMOVE-RE will be removed from TXT."
6448 ;; We keep the org-prefix-* variable values along with a compiled
6449 ;; formatter, so that multiple agendas existing at the same time do
6450 ;; not step on each other toes.
6452 ;; It was inconvenient to make these variables buffer local in
6453 ;; Agenda buffers, because this function expects to be called with
6454 ;; the buffer where item comes from being current, and not agenda
6455 ;; buffer
6456 (let* ((bindings (car org-prefix-format-compiled))
6457 (formatter (cadr org-prefix-format-compiled)))
6458 (loop for (var value) in bindings
6459 do (set var value))
6460 (save-match-data
6461 ;; Diary entries sometimes have extra whitespace at the beginning
6462 (setq txt (org-trim txt))
6464 ;; Fix the tags part in txt
6465 (setq txt (org-agenda-fix-displayed-tags
6466 txt tags
6467 org-agenda-show-inherited-tags
6468 org-agenda-hide-tags-regexp))
6470 (let* ((category (or category
6471 (if (stringp org-category)
6472 org-category
6473 (and org-category (symbol-name org-category)))
6474 (if buffer-file-name
6475 (file-name-sans-extension
6476 (file-name-nondirectory buffer-file-name))
6477 "")))
6478 (category-icon (org-agenda-get-category-icon category))
6479 (category-icon (if category-icon
6480 (propertize " " 'display category-icon)
6481 ""))
6482 ;; time, tag, effort are needed for the eval of the prefix format
6483 (tag (if tags (nth (1- (length tags)) tags) ""))
6484 time effort neffort
6485 (ts (if dotime (concat
6486 (if (stringp dotime) dotime "")
6487 (and org-agenda-search-headline-for-time txt))))
6488 (time-of-day (and dotime (org-get-time-of-day ts)))
6489 stamp plain s0 s1 s2 rtn srp l
6490 duration thecategory breadcrumbs)
6491 (and (derived-mode-p 'org-mode) buffer-file-name
6492 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6493 (when (and dotime time-of-day)
6494 ;; Extract starting and ending time and move them to prefix
6495 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6496 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6497 (setq s0 (match-string 0 ts)
6498 srp (and stamp (match-end 3))
6499 s1 (match-string (if plain 1 2) ts)
6500 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6502 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6503 ;; them, we might want to remove them there to avoid duplication.
6504 ;; The user can turn this off with a variable.
6505 (if (and org-prefix-has-time
6506 org-agenda-remove-times-when-in-prefix (or stamp plain)
6507 (string-match (concat (regexp-quote s0) " *") txt)
6508 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6509 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6510 (= (match-beginning 0) 0)
6512 (setq txt (replace-match "" nil nil txt))))
6513 ;; Normalize the time(s) to 24 hour
6514 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6515 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6517 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6518 (let (org-time-clocksum-use-effort-durations)
6519 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6520 (setq s2
6521 (org-minutes-to-clocksum-string
6522 (+ (org-hh:mm-string-to-minutes s1)
6523 org-agenda-default-appointment-duration)))))
6525 ;; Compute the duration
6526 (when s2
6527 (setq duration (- (org-hh:mm-string-to-minutes s2)
6528 (org-hh:mm-string-to-minutes s1)))))
6530 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6531 txt)
6532 ;; Tags are in the string
6533 (if (or (eq org-agenda-remove-tags t)
6534 (and org-agenda-remove-tags
6535 org-prefix-has-tag))
6536 (setq txt (replace-match "" t t txt))
6537 (setq txt (replace-match
6538 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6539 (match-string 2 txt))
6540 t t txt))))
6541 (when (derived-mode-p 'org-mode)
6542 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6544 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6545 ;; current buffer, so move this check outside of above
6546 (if effort
6547 (setq neffort (org-duration-string-to-minutes effort)
6548 effort (setq effort (concat "[" effort "]")))
6549 ;; prevent erroring out with %e format when there is no effort
6550 (setq effort ""))
6552 (when remove-re
6553 (while (string-match remove-re txt)
6554 (setq txt (replace-match "" t t txt))))
6556 ;; Set org-heading property on `txt' to mark the start of the
6557 ;; heading.
6558 (add-text-properties 0 (length txt) '(org-heading t) txt)
6560 ;; Prepare the variables needed in the eval of the compiled format
6561 (if org-prefix-has-breadcrumbs
6562 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6563 (let ((s (org-display-outline-path nil nil "->" t)))
6564 (if (eq "" s) "" (concat s "->"))))))
6565 (setq time (cond (s2 (concat
6566 (org-agenda-time-of-day-to-ampm-maybe s1)
6567 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6568 (if org-agenda-timegrid-use-ampm " ")))
6569 (s1 (concat
6570 (org-agenda-time-of-day-to-ampm-maybe s1)
6571 (if org-agenda-timegrid-use-ampm
6572 "........ "
6573 "......")))
6574 (t ""))
6575 extra (or (and (not habitp) extra) "")
6576 category (if (symbolp category) (symbol-name category) category)
6577 thecategory (copy-sequence category)
6578 level (or level ""))
6579 (if (string-match org-bracket-link-regexp category)
6580 (progn
6581 (setq l (if (match-end 3)
6582 (- (match-end 3) (match-beginning 3))
6583 (- (match-end 1) (match-beginning 1))))
6584 (when (< l (or org-prefix-category-length 0))
6585 (setq category (copy-sequence category))
6586 (org-add-props category nil
6587 'extra-space (make-string
6588 (- org-prefix-category-length l 1) ?\ ))))
6589 (if (and org-prefix-category-max-length
6590 (>= (length category) org-prefix-category-max-length))
6591 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6592 ;; Evaluate the compiled format
6593 (setq rtn (concat (eval formatter) txt))
6595 ;; And finally add the text properties
6596 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6597 (org-add-props rtn nil
6598 'org-category (if thecategory (downcase thecategory) category)
6599 'tags (mapcar 'org-downcase-keep-props tags)
6600 'org-highest-priority org-highest-priority
6601 'org-lowest-priority org-lowest-priority
6602 'time-of-day time-of-day
6603 'duration duration
6604 'effort effort
6605 'effort-minutes neffort
6606 'breadcrumbs breadcrumbs
6607 'txt txt
6608 'level level
6609 'time time
6610 'extra extra
6611 'format org-prefix-format-compiled
6612 'dotime dotime)))))
6614 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6615 "Remove tags string from TXT, and add a modified list of tags.
6616 The modified list may contain inherited tags, and tags matched by
6617 `org-agenda-hide-tags-regexp' will be removed."
6618 (when (or add-inherited hide-re)
6619 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6620 (setq txt (substring txt 0 (match-beginning 0))))
6621 (setq tags
6622 (delq nil
6623 (mapcar (lambda (tg)
6624 (if (or (and hide-re (string-match hide-re tg))
6625 (and (not add-inherited)
6626 (get-text-property 0 'inherited tg)))
6628 tg))
6629 tags)))
6630 (when tags
6631 (let ((have-i (get-text-property 0 'inherited (car tags)))
6633 (setq txt (concat txt " :"
6634 (mapconcat
6635 (lambda (x)
6636 (setq i (get-text-property 0 'inherited x))
6637 (if (and have-i (not i))
6638 (progn
6639 (setq have-i nil)
6640 (concat ":" x))
6642 tags ":")
6643 (if have-i "::" ":"))))))
6644 txt)
6646 (defun org-downcase-keep-props (s)
6647 (let ((props (text-properties-at 0 s)))
6648 (setq s (downcase s))
6649 (add-text-properties 0 (length s) props s)
6652 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6654 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6655 "Add a time-grid for agenda items which need it.
6657 LIST is the list of agenda items formatted by `org-agenda-list'.
6658 NDAYS is the span of the current agenda view.
6659 TODAYP is `t' when the current agenda view is on today."
6660 (catch 'exit
6661 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6662 ((and todayp (member 'today (car org-agenda-time-grid))))
6663 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6664 ((member 'weekly (car org-agenda-time-grid)))
6665 (t (throw 'exit list)))
6666 (let* ((have (delq nil (mapcar
6667 (lambda (x) (get-text-property 1 'time-of-day x))
6668 list)))
6669 (string (nth 1 org-agenda-time-grid))
6670 (gridtimes (nth 2 org-agenda-time-grid))
6671 (req (car org-agenda-time-grid))
6672 (remove (member 'remove-match req))
6673 new time)
6674 (if (and (member 'require-timed req) (not have))
6675 ;; don't show empty grid
6676 (throw 'exit list))
6677 (while (setq time (pop gridtimes))
6678 (unless (and remove (member time have))
6679 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6680 (push (org-agenda-format-item
6681 nil string nil "" nil
6682 (concat (substring time 0 -2) ":" (substring time -2)))
6683 new)
6684 (put-text-property
6685 2 (length (car new)) 'face 'org-time-grid (car new))))
6686 (when (and todayp org-agenda-show-current-time-in-grid)
6687 (push (org-agenda-format-item
6688 nil org-agenda-current-time-string nil "" nil
6689 (format-time-string "%H:%M "))
6690 new)
6691 (put-text-property
6692 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6694 (if (member 'time-up org-agenda-sorting-strategy-selected)
6695 (append new list)
6696 (append list new)))))
6698 (defun org-compile-prefix-format (key)
6699 "Compile the prefix format into a Lisp form that can be evaluated.
6700 The resulting form and associated variable bindings is returned
6701 and stored in the variable `org-prefix-format-compiled'."
6702 (setq org-prefix-has-time nil
6703 org-prefix-has-tag nil
6704 org-prefix-category-length nil
6705 org-prefix-has-effort nil
6706 org-prefix-has-breadcrumbs nil)
6707 (let ((s (cond
6708 ((stringp org-agenda-prefix-format)
6709 org-agenda-prefix-format)
6710 ((assq key org-agenda-prefix-format)
6711 (cdr (assq key org-agenda-prefix-format)))
6712 (t " %-12:c%?-12t% s")))
6713 (start 0)
6714 varform vars var e c f opt)
6715 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6716 s start)
6717 (setq var (or (cdr (assoc (match-string 4 s)
6718 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6719 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6720 'eval)
6721 c (or (match-string 3 s) "")
6722 opt (match-beginning 1)
6723 start (1+ (match-beginning 0)))
6724 (if (equal var 'time) (setq org-prefix-has-time t))
6725 (if (equal var 'tag) (setq org-prefix-has-tag t))
6726 (if (equal var 'effort) (setq org-prefix-has-effort t))
6727 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6728 (setq f (concat "%" (match-string 2 s) "s"))
6729 (when (equal var 'category)
6730 (setq org-prefix-category-length
6731 (floor (abs (string-to-number (match-string 2 s)))))
6732 (setq org-prefix-category-max-length
6733 (let ((x (match-string 2 s)))
6734 (save-match-data
6735 (if (string-match "\\.[0-9]+" x)
6736 (string-to-number (substring (match-string 0 x) 1)))))))
6737 (if (eq var 'eval)
6738 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6739 (if opt
6740 (setq varform
6741 `(if (equal "" ,var)
6743 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6744 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6745 (setq s (replace-match "%s" t nil s))
6746 (push varform vars))
6747 (setq vars (nreverse vars))
6748 (with-current-buffer (or org-agenda-buffer (current-buffer))
6749 (setq org-prefix-format-compiled
6750 (list
6751 `((org-prefix-has-time ,org-prefix-has-time)
6752 (org-prefix-has-tag ,org-prefix-has-tag)
6753 (org-prefix-category-length ,org-prefix-category-length)
6754 (org-prefix-has-effort ,org-prefix-has-effort)
6755 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6756 `(format ,s ,@vars))))))
6758 (defun org-set-sorting-strategy (key)
6759 (if (symbolp (car org-agenda-sorting-strategy))
6760 ;; the old format
6761 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6762 (setq org-agenda-sorting-strategy-selected
6763 (or (cdr (assq key org-agenda-sorting-strategy))
6764 (cdr (assq 'agenda org-agenda-sorting-strategy))
6765 '(time-up category-keep priority-down)))))
6767 (defun org-get-time-of-day (s &optional string mod24)
6768 "Check string S for a time of day.
6769 If found, return it as a military time number between 0 and 2400.
6770 If not found, return nil.
6771 The optional STRING argument forces conversion into a 5 character wide string
6772 HH:MM."
6773 (save-match-data
6774 (when
6775 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6776 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6777 (let* ((h (string-to-number (match-string 1 s)))
6778 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6779 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6780 (am-p (equal ampm "am"))
6781 (h1 (cond ((not ampm) h)
6782 ((= h 12) (if am-p 0 12))
6783 (t (+ h (if am-p 0 12)))))
6784 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6785 (mod h1 24) h1))
6786 (t0 (+ (* 100 h2) m))
6787 (t1 (concat (if (>= h1 24) "+" " ")
6788 (if (and org-agenda-time-leading-zero
6789 (< t0 1000)) "0" "")
6790 (if (< t0 100) "0" "")
6791 (if (< t0 10) "0" "")
6792 (int-to-string t0))))
6793 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6795 (defvar org-agenda-before-sorting-filter-function nil
6796 "Function to be applied to agenda items prior to sorting.
6797 Prior to sorting also means just before they are inserted into the agenda.
6799 To aid sorting, you may revisit the original entries and add more text
6800 properties which will later be used by the sorting functions.
6802 The function should take a string argument, an agenda line.
6803 It has access to the text properties in that line, which contain among
6804 other things, the property `org-hd-marker' that points to the entry
6805 where the line comes from. Note that not all lines going into the agenda
6806 have this property, only most.
6808 The function should return the modified string. It is probably best
6809 to ONLY change text properties.
6811 You can also use this function as a filter, by returning nil for lines
6812 you don't want to have in the agenda at all. For this application, you
6813 could bind the variable in the options section of a custom command.")
6815 (defun org-agenda-finalize-entries (list &optional type)
6816 "Sort, limit and concatenate the LIST of agenda items.
6817 The optional argument TYPE tells the agenda type."
6818 (let ((max-effort (cond ((listp org-agenda-max-effort)
6819 (cdr (assoc type org-agenda-max-effort)))
6820 (t org-agenda-max-effort)))
6821 (max-todo (cond ((listp org-agenda-max-todos)
6822 (cdr (assoc type org-agenda-max-todos)))
6823 (t org-agenda-max-todos)))
6824 (max-tags (cond ((listp org-agenda-max-tags)
6825 (cdr (assoc type org-agenda-max-tags)))
6826 (t org-agenda-max-tags)))
6827 (max-entries (cond ((listp org-agenda-max-entries)
6828 (cdr (assoc type org-agenda-max-entries)))
6829 (t org-agenda-max-entries))) l)
6830 (when org-agenda-before-sorting-filter-function
6831 (setq list
6832 (delq nil
6833 (mapcar
6834 org-agenda-before-sorting-filter-function list))))
6835 (setq list (mapcar 'org-agenda-highlight-todo list)
6836 list (mapcar 'identity (sort list 'org-entries-lessp)))
6837 (when max-effort
6838 (setq list (org-agenda-limit-entries
6839 list 'effort-minutes max-effort 'identity)))
6840 (when max-todo
6841 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6842 (when max-tags
6843 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6844 (when max-entries
6845 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6846 (mapconcat 'identity list "\n")))
6848 (defun org-agenda-limit-entries (list prop limit &optional fn)
6849 "Limit the number of agenda entries."
6850 (let ((include (and limit (< limit 0))))
6851 (if limit
6852 (let ((fun (or fn (lambda (p) (if p 1))))
6853 (lim 0))
6854 (delq nil
6855 (mapcar
6856 (lambda (e)
6857 (let ((pval (funcall fun (get-text-property 1 prop e))))
6858 (if pval (setq lim (+ lim pval)))
6859 (cond ((and pval (<= lim (abs limit))) e)
6860 ((and include (not pval)) e))))
6861 list)))
6862 list)))
6864 (defun org-agenda-limit-interactively ()
6865 "In agenda, interactively limit entries to various maximums."
6866 (interactive)
6867 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6868 (num (string-to-number (read-from-minibuffer "How many? "))))
6869 (cond ((equal max ?e)
6870 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6871 ((equal max ?t)
6872 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6873 ((equal max ?T)
6874 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6875 ((equal max ?E)
6876 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6877 (org-agenda-fit-window-to-buffer))
6879 (defun org-agenda-highlight-todo (x)
6880 (let ((org-done-keywords org-done-keywords-for-agenda)
6881 (case-fold-search nil)
6883 (if (eq x 'line)
6884 (save-excursion
6885 (beginning-of-line 1)
6886 (setq re (org-get-at-bol 'org-todo-regexp))
6887 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6888 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6889 (add-text-properties (match-beginning 0) (match-end 1)
6890 (list 'face (org-get-todo-face 1)))
6891 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6892 (delete-region (match-beginning 1) (1- (match-end 0)))
6893 (goto-char (match-beginning 1))
6894 (insert (format org-agenda-todo-keyword-format s)))))
6895 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6896 (setq re (get-text-property 0 'org-todo-regexp x))
6897 (when (and re
6898 ;; Test `pl' because if there's no heading content,
6899 ;; there's no point matching to highlight. Note
6900 ;; that if we didn't test `pl' first, and there
6901 ;; happened to be no keyword from `org-todo-regexp'
6902 ;; on this heading line, then the `equal' comparison
6903 ;; afterwards would spuriously succeed in the case
6904 ;; where `pl' is nil -- causing an args-out-of-range
6905 ;; error when we try to add text properties to text
6906 ;; that isn't there.
6908 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6909 x pl) pl))
6910 (add-text-properties
6911 (or (match-end 1) (match-end 0)) (match-end 0)
6912 (list 'face (org-get-todo-face (match-string 2 x)))
6914 (when (match-end 1)
6915 (setq x (concat (substring x 0 (match-end 1))
6916 (format org-agenda-todo-keyword-format
6917 (match-string 2 x))
6918 (org-add-props " " (text-properties-at 0 x))
6919 (substring x (match-end 3)))))))
6920 x)))
6922 (defsubst org-cmp-priority (a b)
6923 "Compare the priorities of string A and B."
6924 (let ((pa (or (get-text-property 1 'priority a) 0))
6925 (pb (or (get-text-property 1 'priority b) 0)))
6926 (cond ((> pa pb) +1)
6927 ((< pa pb) -1))))
6929 (defsubst org-cmp-effort (a b)
6930 "Compare the effort values of string A and B."
6931 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6932 (ea (or (get-text-property 1 'effort-minutes a) def))
6933 (eb (or (get-text-property 1 'effort-minutes b) def)))
6934 (cond ((> ea eb) +1)
6935 ((< ea eb) -1))))
6937 (defsubst org-cmp-category (a b)
6938 "Compare the string values of categories of strings A and B."
6939 (let ((ca (or (get-text-property 1 'org-category a) ""))
6940 (cb (or (get-text-property 1 'org-category b) "")))
6941 (cond ((string-lessp ca cb) -1)
6942 ((string-lessp cb ca) +1))))
6944 (defsubst org-cmp-todo-state (a b)
6945 "Compare the todo states of strings A and B."
6946 (let* ((ma (or (get-text-property 1 'org-marker a)
6947 (get-text-property 1 'org-hd-marker a)))
6948 (mb (or (get-text-property 1 'org-marker b)
6949 (get-text-property 1 'org-hd-marker b)))
6950 (fa (and ma (marker-buffer ma)))
6951 (fb (and mb (marker-buffer mb)))
6952 (todo-kwds
6953 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6954 (and fb (with-current-buffer fb org-todo-keywords-1))))
6955 (ta (or (get-text-property 1 'todo-state a) ""))
6956 (tb (or (get-text-property 1 'todo-state b) ""))
6957 (la (- (length (member ta todo-kwds))))
6958 (lb (- (length (member tb todo-kwds))))
6959 (donepa (member ta org-done-keywords-for-agenda))
6960 (donepb (member tb org-done-keywords-for-agenda)))
6961 (cond ((and donepa (not donepb)) -1)
6962 ((and (not donepa) donepb) +1)
6963 ((< la lb) -1)
6964 ((< lb la) +1))))
6966 (defsubst org-cmp-alpha (a b)
6967 "Compare the headlines, alphabetically."
6968 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6969 (plb (text-property-any 0 (length b) 'org-heading t b))
6970 (ta (and pla (substring a pla)))
6971 (tb (and plb (substring b plb))))
6972 (when pla
6973 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6974 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6975 (setq ta (substring ta (match-end 0))))
6976 (setq ta (downcase ta)))
6977 (when plb
6978 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6979 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6980 (setq tb (substring tb (match-end 0))))
6981 (setq tb (downcase tb)))
6982 (cond ((not ta) +1)
6983 ((not tb) -1)
6984 ((string-lessp ta tb) -1)
6985 ((string-lessp tb ta) +1))))
6987 (defsubst org-cmp-tag (a b)
6988 "Compare the string values of the first tags of A and B."
6989 (let ((ta (car (last (get-text-property 1 'tags a))))
6990 (tb (car (last (get-text-property 1 'tags b)))))
6991 (cond ((not ta) +1)
6992 ((not tb) -1)
6993 ((string-lessp ta tb) -1)
6994 ((string-lessp tb ta) +1))))
6996 (defsubst org-cmp-time (a b)
6997 "Compare the time-of-day values of strings A and B."
6998 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6999 (ta (or (get-text-property 1 'time-of-day a) def))
7000 (tb (or (get-text-property 1 'time-of-day b) def)))
7001 (cond ((< ta tb) -1)
7002 ((< tb ta) +1))))
7004 (defsubst org-cmp-ts (a b &optional type)
7005 "Compare the timestamps values of entries A and B.
7006 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7007 \"timestamp_ia\", compare within each of these type. When TYPE
7008 is the empty string, compare all timestamps without respect of
7009 their type."
7010 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7011 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7012 (get-text-property 1 'ts-date a)) def))
7013 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7014 (get-text-property 1 'ts-date b)) def)))
7015 (cond ((< ta tb) -1)
7016 ((< tb ta) +1))))
7018 (defsubst org-cmp-habit-p (a b)
7019 "Compare the todo states of strings A and B."
7020 (let ((ha (get-text-property 1 'org-habit-p a))
7021 (hb (get-text-property 1 'org-habit-p b)))
7022 (cond ((and ha (not hb)) -1)
7023 ((and (not ha) hb) +1))))
7025 (defun org-entries-lessp (a b)
7026 "Predicate for sorting agenda entries."
7027 ;; The following variables will be used when the form is evaluated.
7028 ;; So even though the compiler complains, keep them.
7029 (let* ((ss org-agenda-sorting-strategy-selected)
7030 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7031 (org-cmp-ts a b "")))
7032 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7033 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7034 (org-cmp-ts a b "scheduled")))
7035 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7036 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7037 (org-cmp-ts a b "deadline")))
7038 (deadline-down (if deadline-up (- deadline-up) nil))
7039 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7040 (org-cmp-ts a b "iatimestamp_ia")))
7041 (tsia-down (if tsia-up (- tsia-up) nil))
7042 (ts-up (and (org-em 'ts-up 'ts-down ss)
7043 (org-cmp-ts a b "timestamp")))
7044 (ts-down (if ts-up (- ts-up) nil))
7045 (time-up (and (org-em 'time-up 'time-down ss)
7046 (org-cmp-time a b)))
7047 (time-down (if time-up (- time-up) nil))
7048 (priority-up (and (org-em 'priority-up 'priority-down ss)
7049 (org-cmp-priority a b)))
7050 (priority-down (if priority-up (- priority-up) nil))
7051 (effort-up (and (org-em 'effort-up 'effort-down ss)
7052 (org-cmp-effort a b)))
7053 (effort-down (if effort-up (- effort-up) nil))
7054 (category-up (and (or (org-em 'category-up 'category-down ss)
7055 (memq 'category-keep ss))
7056 (org-cmp-category a b)))
7057 (category-down (if category-up (- category-up) nil))
7058 (category-keep (if category-up +1 nil))
7059 (tag-up (and (org-em 'tag-up 'tag-down ss)
7060 (org-cmp-tag a b)))
7061 (tag-down (if tag-up (- tag-up) nil))
7062 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7063 (org-cmp-todo-state a b)))
7064 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7065 (habit-up (and (org-em 'habit-up 'habit-down ss)
7066 (org-cmp-habit-p a b)))
7067 (habit-down (if habit-up (- habit-up) nil))
7068 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7069 (org-cmp-alpha a b)))
7070 (alpha-down (if alpha-up (- alpha-up) nil))
7071 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7072 user-defined-up user-defined-down)
7073 (if (and need-user-cmp org-agenda-cmp-user-defined
7074 (functionp org-agenda-cmp-user-defined))
7075 (setq user-defined-up
7076 (funcall org-agenda-cmp-user-defined a b)
7077 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7078 (cdr (assoc
7079 (eval (cons 'or org-agenda-sorting-strategy-selected))
7080 '((-1 . t) (1 . nil) (nil . nil))))))
7082 ;;; Agenda restriction lock
7084 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7085 "Overlay to mark the headline to which agenda commands are restricted.")
7086 (overlay-put org-agenda-restriction-lock-overlay
7087 'face 'org-agenda-restriction-lock)
7088 (overlay-put org-agenda-restriction-lock-overlay
7089 'help-echo "Agendas are currently limited to this subtree.")
7090 (org-detach-overlay org-agenda-restriction-lock-overlay)
7092 ;;;###autoload
7093 (defun org-agenda-set-restriction-lock (&optional type)
7094 "Set restriction lock for agenda, to current subtree or file.
7095 Restriction will be the file if TYPE is `file', or if type is the
7096 universal prefix '(4), or if the cursor is before the first headline
7097 in the file. Otherwise, restriction will be to the current subtree."
7098 (interactive "P")
7099 (and (equal type '(4)) (setq type 'file))
7100 (setq type (cond
7101 (type type)
7102 ((org-at-heading-p) 'subtree)
7103 ((condition-case nil (org-back-to-heading t) (error nil))
7104 'subtree)
7105 (t 'file)))
7106 (if (eq type 'subtree)
7107 (progn
7108 (setq org-agenda-restrict (current-buffer))
7109 (setq org-agenda-overriding-restriction 'subtree)
7110 (put 'org-agenda-files 'org-restrict
7111 (list (buffer-file-name (buffer-base-buffer))))
7112 (org-back-to-heading t)
7113 (move-overlay org-agenda-restriction-lock-overlay
7114 (point)
7115 (if org-agenda-restriction-lock-highlight-subtree
7116 (save-excursion (org-end-of-subtree t t) (point))
7117 (point-at-eol)))
7118 (move-marker org-agenda-restrict-begin (point))
7119 (move-marker org-agenda-restrict-end
7120 (save-excursion (org-end-of-subtree t t)))
7121 (message "Locking agenda restriction to subtree"))
7122 (put 'org-agenda-files 'org-restrict
7123 (list (buffer-file-name (buffer-base-buffer))))
7124 (setq org-agenda-restrict nil)
7125 (setq org-agenda-overriding-restriction 'file)
7126 (move-marker org-agenda-restrict-begin nil)
7127 (move-marker org-agenda-restrict-end nil)
7128 (message "Locking agenda restriction to file"))
7129 (setq current-prefix-arg nil)
7130 (org-agenda-maybe-redo))
7132 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7133 "Remove the agenda restriction lock."
7134 (interactive "P")
7135 (org-detach-overlay org-agenda-restriction-lock-overlay)
7136 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7137 (setq org-agenda-overriding-restriction nil)
7138 (setq org-agenda-restrict nil)
7139 (put 'org-agenda-files 'org-restrict nil)
7140 (move-marker org-agenda-restrict-begin nil)
7141 (move-marker org-agenda-restrict-end nil)
7142 (setq current-prefix-arg nil)
7143 (message "Agenda restriction lock removed")
7144 (or noupdate (org-agenda-maybe-redo)))
7146 (defun org-agenda-maybe-redo ()
7147 "If there is any window showing the agenda view, update it."
7148 (let ((w (get-buffer-window org-agenda-buffer-name t))
7149 (w0 (selected-window)))
7150 (when w
7151 (select-window w)
7152 (org-agenda-redo)
7153 (select-window w0)
7154 (if org-agenda-overriding-restriction
7155 (message "Agenda view shifted to new %s restriction"
7156 org-agenda-overriding-restriction)
7157 (message "Agenda restriction lock removed")))))
7159 ;;; Agenda commands
7161 (defun org-agenda-check-type (error &rest types)
7162 "Check if agenda buffer is of allowed type.
7163 If ERROR is non-nil, throw an error, otherwise just return nil.
7164 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7165 (if (not org-agenda-type)
7166 (error "No Org agenda currently displayed")
7167 (if (memq org-agenda-type types)
7169 (if error
7170 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7171 nil))))
7173 (defun org-agenda-Quit ()
7174 "Exit the agenda and kill buffers loaded by `org-agenda'.
7175 Also restore the window configuration."
7176 (interactive)
7177 (if org-agenda-columns-active
7178 (org-columns-quit)
7179 (let ((buf (current-buffer)))
7180 (if (eq org-agenda-window-setup 'other-frame)
7181 (progn
7182 (org-agenda-reset-markers)
7183 (kill-buffer buf)
7184 (org-columns-remove-overlays)
7185 (setq org-agenda-archives-mode nil)
7186 (delete-frame))
7187 (and (not (eq org-agenda-window-setup 'current-window))
7188 (not (one-window-p))
7189 (delete-window))
7190 (org-agenda-reset-markers)
7191 (kill-buffer buf)
7192 (org-columns-remove-overlays)
7193 (setq org-agenda-archives-mode nil)))
7194 (setq org-agenda-buffer nil)
7195 ;; Maybe restore the pre-agenda window configuration.
7196 (and org-agenda-restore-windows-after-quit
7197 (not (eq org-agenda-window-setup 'other-frame))
7198 org-agenda-pre-window-conf
7199 (set-window-configuration org-agenda-pre-window-conf)
7200 (setq org-agenda-pre-window-conf nil))))
7202 (defun org-agenda-quit ()
7203 "Exit the agenda and restore the window configuration.
7204 When `org-agenda-sticky' is non-nil, only bury the agenda."
7205 (interactive)
7206 (if (and (eq org-indirect-buffer-display 'other-window)
7207 org-last-indirect-buffer)
7208 (let ((org-last-indirect-window
7209 (get-buffer-window org-last-indirect-buffer)))
7210 (if org-last-indirect-window
7211 (delete-window org-last-indirect-window))))
7212 (if org-agenda-columns-active
7213 (org-columns-quit)
7214 (if org-agenda-sticky
7215 (let ((buf (current-buffer)))
7216 (if (eq org-agenda-window-setup 'other-frame)
7217 (progn
7218 (delete-frame))
7219 (and (not (eq org-agenda-window-setup 'current-window))
7220 (not (one-window-p))
7221 (delete-window)))
7222 (with-current-buffer buf
7223 (bury-buffer)
7224 ;; Maybe restore the pre-agenda window configuration.
7225 (and org-agenda-restore-windows-after-quit
7226 (not (eq org-agenda-window-setup 'other-frame))
7227 org-agenda-pre-window-conf
7228 (set-window-configuration org-agenda-pre-window-conf)
7229 (setq org-agenda-pre-window-conf nil))))
7230 (org-agenda-Quit))))
7232 (defun org-agenda-exit ()
7233 "Exit the agenda and restore the window configuration.
7234 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7235 buffers visited directly by the user will not be touched."
7236 (interactive)
7237 (org-release-buffers org-agenda-new-buffers)
7238 (setq org-agenda-new-buffers nil)
7239 (org-agenda-Quit))
7241 (defun org-agenda-kill-all-agenda-buffers ()
7242 "Kill all buffers in `org-agenda-mode'.
7243 This is used when toggling sticky agendas.
7244 You can also explicitly invoke it with `C-c a C-k'."
7245 (interactive)
7246 (let (blist)
7247 (dolist (buf (buffer-list))
7248 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7249 (push buf blist)))
7250 (mapc 'kill-buffer blist)))
7252 (defun org-agenda-execute (arg)
7253 "Execute another agenda command, keeping same window.
7254 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7255 in the agenda."
7256 (interactive "P")
7257 (let ((org-agenda-window-setup 'current-window))
7258 (org-agenda arg)))
7260 (defun org-agenda-redo (&optional all)
7261 "Rebuild possibly ALL agenda view(s) in the current buffer."
7262 (interactive "P")
7263 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7264 (cpa (unless (eq all t) current-prefix-arg))
7265 (org-agenda-doing-sticky-redo org-agenda-sticky)
7266 (org-agenda-sticky nil)
7267 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7268 org-agenda-buffer-name))
7269 (org-agenda-keep-modes t)
7270 (tag-filter org-agenda-tag-filter)
7271 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7272 (top-hl-filter org-agenda-top-headline-filter)
7273 (cat-filter org-agenda-category-filter)
7274 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7275 (re-filter org-agenda-regexp-filter)
7276 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7277 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7278 (cols org-agenda-columns-active)
7279 (line (org-current-line))
7280 (window-line (- line (org-current-line (window-start))))
7281 (lprops (get 'org-agenda-redo-command 'org-lprops))
7282 (redo-cmd (get-text-property p 'org-redo-cmd))
7283 (last-args (get-text-property p 'org-last-args))
7284 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7285 (org-agenda-overriding-cmd-arguments
7286 (unless (eq all t)
7287 (cond ((listp last-args)
7288 (cons (or cpa (car last-args)) (cdr last-args)))
7289 ((stringp last-args)
7290 last-args))))
7291 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7292 (put 'org-agenda-tag-filter :preset-filter nil)
7293 (put 'org-agenda-category-filter :preset-filter nil)
7294 (put 'org-agenda-regexp-filter :preset-filter nil)
7295 (and cols (org-columns-quit))
7296 (message "Rebuilding agenda buffer...")
7297 (if series-redo-cmd
7298 (eval series-redo-cmd)
7299 (org-let lprops redo-cmd))
7300 (setq org-agenda-undo-list nil
7301 org-agenda-pending-undo-list nil
7302 org-agenda-tag-filter tag-filter
7303 org-agenda-category-filter cat-filter
7304 org-agenda-regexp-filter re-filter
7305 org-agenda-top-headline-filter top-hl-filter)
7306 (message "Rebuilding agenda buffer...done")
7307 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7308 (put 'org-agenda-category-filter :preset-filter cat-preset)
7309 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7310 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7311 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7312 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7313 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7314 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7315 (org-goto-line line)
7316 (recenter window-line)))
7318 (defvar org-global-tags-completion-table nil)
7319 (defvar org-agenda-filter-form nil)
7320 (defvar org-agenda-filtered-by-category nil)
7322 (defun org-agenda-filter-by-category (strip)
7323 "Keep only those lines in the agenda buffer that have a specific category.
7324 The category is that of the current line."
7325 (interactive "P")
7326 (if (and org-agenda-filtered-by-category
7327 org-agenda-category-filter)
7328 (org-agenda-filter-show-all-cat)
7329 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7330 (cond
7331 ((and cat strip)
7332 (org-agenda-filter-apply
7333 (push (concat "-" cat) org-agenda-category-filter) 'category))
7334 ((and cat)
7335 (org-agenda-filter-apply
7336 (setq org-agenda-category-filter
7337 (list (concat "+" cat))) 'category))
7338 ((error "No category at point"))))))
7340 (defun org-find-top-headline (&optional pos)
7341 "Find the topmost parent headline and return it."
7342 (save-excursion
7343 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7344 (if pos (goto-char pos))
7345 ;; Skip up to the topmost parent
7346 (while (ignore-errors (outline-up-heading 1) t))
7347 (ignore-errors
7348 (nth 4 (org-heading-components))))))
7350 (defvar org-agenda-filtered-by-top-headline nil)
7351 (defun org-agenda-filter-by-top-headline (strip)
7352 "Keep only those lines that are descendants from the same top headline.
7353 The top headline is that of the current line."
7354 (interactive "P")
7355 (if org-agenda-filtered-by-top-headline
7356 (progn
7357 (setq org-agenda-filtered-by-top-headline nil
7358 org-agenda-top-headline-filter nil)
7359 (org-agenda-filter-show-all-cat))
7360 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7361 (if cat (org-agenda-filter-top-headline-apply cat strip)
7362 (error "No top-level category at point")))))
7364 (defvar org-agenda-regexp-filter nil)
7365 (defun org-agenda-filter-by-regexp (strip)
7366 "Filter agenda entries by a regular expression.
7367 Regexp filters are cumulative.
7368 With no prefix argument, keep entries matching the regexp.
7369 With one prefix argument, filter out entries matching the regexp.
7370 With two prefix arguments, remove the regexp filters."
7371 (interactive "P")
7372 (if (not (equal strip '(16)))
7373 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7374 (read-from-minibuffer
7375 (if (equal strip '(4))
7376 "Filter out entries matching regexp: "
7377 "Narrow to entries matching regexp: ")))))
7378 (push flt org-agenda-regexp-filter)
7379 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7380 (org-agenda-filter-show-all-re)
7381 (message "Regexp filter removed")))
7383 (defun org-agenda-filter-remove-all ()
7384 "Remove all filters from the current agenda buffer."
7385 (interactive)
7386 (when org-agenda-tag-filter
7387 (org-agenda-filter-show-all-tag))
7388 (when org-agenda-category-filter
7389 (org-agenda-filter-show-all-cat))
7390 (when org-agenda-regexp-filter
7391 (org-agenda-filter-show-all-re)))
7393 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7394 "Keep only those lines in the agenda buffer that have a specific tag.
7395 The tag is selected with its fast selection letter, as configured.
7396 With prefix argument STRIP, remove all lines that do have the tag.
7397 A lisp caller can specify CHAR. NARROW means that the new tag should be
7398 used to narrow the search - the interactive user can also press `-' or `+'
7399 to switch to narrowing."
7400 (interactive "P")
7401 (let* ((alist org-tag-alist-for-agenda)
7402 (tag-chars (mapconcat
7403 (lambda (x) (if (and (not (symbolp (car x)))
7404 (cdr x))
7405 (char-to-string (cdr x))
7406 ""))
7407 alist ""))
7408 (efforts (org-split-string
7409 (or (cdr (assoc (concat org-effort-property "_ALL")
7410 org-global-properties))
7411 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7412 "")))
7413 (effort-op org-agenda-filter-effort-default-operator)
7414 (effort-prompt "")
7415 (inhibit-read-only t)
7416 (current org-agenda-tag-filter)
7417 maybe-refresh a n tag)
7418 (unless char
7419 (message
7420 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7421 (if narrow "Narrow" "Filter") tag-chars
7422 (if org-agenda-auto-exclude-function "[RET], " ""))
7423 (setq char (read-char-exclusive)))
7424 (when (member char '(?+ ?-))
7425 ;; Narrowing down
7426 (cond ((equal char ?-) (setq strip t narrow t))
7427 ((equal char ?+) (setq strip nil narrow t)))
7428 (message
7429 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7430 (setq char (read-char-exclusive)))
7431 (when (member char '(?< ?> ?= ??))
7432 ;; An effort operator
7433 (setq effort-op (char-to-string char))
7434 (setq alist nil) ; to make sure it will be interpreted as effort.
7435 (unless (equal char ??)
7436 (loop for i from 0 to 9 do
7437 (setq effort-prompt
7438 (concat
7439 effort-prompt " ["
7440 (if (= i 9) "0" (int-to-string (1+ i)))
7441 "]" (nth i efforts))))
7442 (message "Effort%s: %s " effort-op effort-prompt)
7443 (setq char (read-char-exclusive))
7444 (when (or (< char ?0) (> char ?9))
7445 (error "Need 1-9,0 to select effort"))))
7446 (when (equal char ?\t)
7447 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7448 (org-set-local 'org-global-tags-completion-table
7449 (org-global-tags-completion-table)))
7450 (let ((completion-ignore-case t))
7451 (setq tag (org-icompleting-read
7452 "Tag: " org-global-tags-completion-table))))
7453 (cond
7454 ((equal char ?\r)
7455 (org-agenda-filter-show-all-tag)
7456 (when org-agenda-auto-exclude-function
7457 (setq org-agenda-tag-filter nil)
7458 (dolist (tag (org-agenda-get-represented-tags))
7459 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7460 (if modifier
7461 (push modifier org-agenda-tag-filter))))
7462 (if (not (null org-agenda-tag-filter))
7463 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7464 (setq maybe-refresh t))
7465 ((equal char ?/)
7466 (org-agenda-filter-show-all-tag)
7467 (when (get 'org-agenda-tag-filter :preset-filter)
7468 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7469 (setq maybe-refresh t))
7470 ((equal char ?. )
7471 (setq org-agenda-tag-filter
7472 (mapcar (lambda(tag) (concat "+" tag))
7473 (org-get-at-bol 'tags)))
7474 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7475 (setq maybe-refresh t))
7476 ((or (equal char ?\ )
7477 (setq a (rassoc char alist))
7478 (and (>= char ?0) (<= char ?9)
7479 (setq n (if (= char ?0) 9 (- char ?0 1))
7480 tag (concat effort-op (nth n efforts))
7481 a (cons tag nil)))
7482 (and (= char ??)
7483 (setq tag "?eff")
7484 a (cons tag nil))
7485 (and tag (setq a (cons tag nil))))
7486 (org-agenda-filter-show-all-tag)
7487 (setq tag (car a))
7488 (setq org-agenda-tag-filter
7489 (cons (concat (if strip "-" "+") tag)
7490 (if narrow current nil)))
7491 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7492 (setq maybe-refresh t))
7493 (t (error "Invalid tag selection character %c" char)))
7494 (when (and maybe-refresh
7495 (eq org-agenda-clockreport-mode 'with-filter))
7496 (org-agenda-redo))))
7498 (defun org-agenda-get-represented-tags ()
7499 "Get a list of all tags currently represented in the agenda."
7500 (let (p tags)
7501 (save-excursion
7502 (goto-char (point-min))
7503 (while (setq p (next-single-property-change (point) 'tags))
7504 (goto-char p)
7505 (mapc (lambda (x) (add-to-list 'tags x))
7506 (get-text-property (point) 'tags))))
7507 tags))
7509 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7510 "Refine the current filter. See `org-agenda-filter-by-tag'."
7511 (interactive "P")
7512 (org-agenda-filter-by-tag strip char 'refine))
7514 (defun org-agenda-filter-make-matcher (filter type)
7515 "Create the form that tests a line for agenda filter."
7516 (let (f f1)
7517 (cond
7518 ;; Tag filter
7519 ((eq type 'tag)
7520 (setq filter
7521 (delete-dups
7522 (append (get 'org-agenda-tag-filter :preset-filter)
7523 filter)))
7524 (dolist (x filter)
7525 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7526 (ffunc
7527 (lambda (nf0 nf01 fltr notgroup op)
7528 (dolist (x fltr)
7529 (if (member x '("-" "+"))
7530 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7531 (if (string-match "[<=>?]" x)
7532 (setq nf01 (org-agenda-filter-effort-form x))
7533 (setq nf01 (list 'member (downcase (substring x 1))
7534 'tags)))
7535 (when (equal (string-to-char x) ?-)
7536 (setq nf01 (list 'not nf01))
7537 (when (not notgroup) (setq op 'and))))
7538 (push nf01 nf0))
7539 (if notgroup
7540 (push (cons 'and nf0) f)
7541 (push (cons (or op 'or) nf0) f)))))
7542 (cond ((equal filter '("+"))
7543 (setq f (list (list 'not 'tags))))
7544 ((equal nfilter filter)
7545 (funcall ffunc f1 f filter t nil))
7546 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7547 ;; Category filter
7548 ((eq type 'category)
7549 (setq filter
7550 (delete-dups
7551 (append (get 'org-agenda-category-filter :preset-filter)
7552 filter)))
7553 (dolist (x filter)
7554 (if (equal "-" (substring x 0 1))
7555 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7556 (setq f1 (list 'equal (substring x 1) 'cat)))
7557 (push f1 f)))
7558 ;; Regexp filter
7559 ((eq type 'regexp)
7560 (setq filter
7561 (delete-dups
7562 (append (get 'org-agenda-regexp-filter :preset-filter)
7563 filter)))
7564 (dolist (x filter)
7565 (if (equal "-" (substring x 0 1))
7566 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7567 (setq f1 (list 'string-match (substring x 1) 'txt)))
7568 (push f1 f))))
7569 (cons 'and (nreverse f))))
7571 (defun org-agenda-filter-effort-form (e)
7572 "Return the form to compare the effort of the current line with what E says.
7573 E looks like \"+<2:25\"."
7574 (let (op)
7575 (setq e (substring e 1))
7576 (setq op (string-to-char e) e (substring e 1))
7577 (setq op (cond ((equal op ?<) '<=)
7578 ((equal op ?>) '>=)
7579 ((equal op ??) op)
7580 (t '=)))
7581 (list 'org-agenda-compare-effort (list 'quote op)
7582 (org-duration-string-to-minutes e))))
7584 (defun org-agenda-compare-effort (op value)
7585 "Compare the effort of the current line with VALUE, using OP.
7586 If the line does not have an effort defined, return nil."
7587 (let ((eff (org-get-at-bol 'effort-minutes)))
7588 (if (equal op ??)
7589 (not eff)
7590 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7591 value))))
7593 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7594 "Expand group tags in FILTER for the agenda.
7595 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7596 (if org-group-tags
7597 (let ((case-fold-search t) rtn)
7598 (mapc
7599 (lambda (f)
7600 (let (f0 dir)
7601 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7602 (setq dir (match-string 1 f) f0 (match-string 2 f))
7603 (setq dir (if no-operator "" "+") f0 f))
7604 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7605 (org-tags-expand f0 t t))
7606 rtn))))
7607 filter)
7608 (reverse rtn))
7609 filter))
7611 (defun org-agenda-filter-apply (filter type)
7612 "Set FILTER as the new agenda filter and apply it."
7613 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7614 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7615 (let (tags cat txt)
7616 (setq org-agenda-filter-form
7617 (org-agenda-filter-make-matcher filter type))
7618 (if (and (eq type 'category)
7619 (not (equal (substring (car filter) 0 1) "-")))
7620 ;; Only set `org-agenda-filtered-by-category' to t
7621 ;; when a unique category is used as the filter
7622 (setq org-agenda-filtered-by-category t))
7623 (org-agenda-set-mode-name)
7624 (save-excursion
7625 (goto-char (point-min))
7626 (while (not (eobp))
7627 (if (org-get-at-bol 'org-marker)
7628 (progn
7629 (setq tags ; used in eval
7630 (apply 'append
7631 (mapcar (lambda (f)
7632 (org-agenda-filter-expand-tags (list f) t))
7633 (org-get-at-bol 'tags)))
7634 cat (get-text-property (point) 'org-category)
7635 txt (get-text-property (point) 'txt))
7636 (if (not (eval org-agenda-filter-form))
7637 (org-agenda-filter-hide-line type))
7638 (beginning-of-line 2))
7639 (beginning-of-line 2))))
7640 (if (get-char-property (point) 'invisible)
7641 (ignore-errors (org-agenda-previous-line)))))
7643 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7644 "Filter by top headline HL."
7645 (org-agenda-set-mode-name)
7646 (save-excursion
7647 (goto-char (point-min))
7648 (while (not (eobp))
7649 (let* ((pos (org-get-at-bol 'org-hd-marker))
7650 (tophl (and pos (org-find-top-headline pos))))
7651 (if (and tophl (funcall (if negative 'identity 'not)
7652 (string= hl tophl)))
7653 (org-agenda-filter-hide-line 'category)))
7654 (beginning-of-line 2)))
7655 (if (get-char-property (point) 'invisible)
7656 (org-agenda-previous-line))
7657 (setq org-agenda-top-headline-filter hl
7658 org-agenda-filtered-by-top-headline t))
7660 (defun org-agenda-filter-hide-line (type)
7661 "Hide lines with TYPE in the agenda buffer."
7662 (let* ((b (max (point-min) (1- (point-at-bol))))
7663 (e (point-at-eol))
7664 (ov (make-overlay b e)))
7665 (overlay-put ov 'invisible t)
7666 (overlay-put ov 'intangible t)
7667 (overlay-put ov 'type type)
7668 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7669 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7670 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7672 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7673 (setq pos (or pos (point)))
7674 (save-excursion
7675 (dolist (ov (overlays-at pos))
7676 (when (and (overlay-get ov 'invisible)
7677 (eq (overlay-get ov 'type) 'tag))
7678 (goto-char pos)
7679 (if (< (overlay-start ov) (point-at-eol))
7680 (move-overlay ov (point-at-eol)
7681 (overlay-end ov)))))))
7683 (defun org-agenda-filter-show-all-tag nil
7684 "Remove tag filter overlays from the agenda buffer."
7685 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7686 (setq org-agenda-tag-filter-overlays nil
7687 org-agenda-tag-filter nil
7688 org-agenda-filter-form nil)
7689 (org-agenda-set-mode-name))
7691 (defun org-agenda-filter-show-all-re nil
7692 "Remove regexp filter overlays from the agenda buffer."
7693 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7694 (setq org-agenda-re-filter-overlays nil
7695 org-agenda-regexp-filter nil
7696 org-agenda-filter-form nil)
7697 (org-agenda-set-mode-name))
7699 (defun org-agenda-filter-show-all-cat nil
7700 "Remove category filter overlays from the agenda buffer."
7701 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7702 (setq org-agenda-cat-filter-overlays nil
7703 org-agenda-filtered-by-category nil
7704 org-agenda-category-filter nil
7705 org-agenda-filter-form nil)
7706 (org-agenda-set-mode-name))
7708 (defun org-agenda-manipulate-query-add ()
7709 "Manipulate the query by adding a search term with positive selection.
7710 Positive selection means the term must be matched for selection of an entry."
7711 (interactive)
7712 (org-agenda-manipulate-query ?\[))
7713 (defun org-agenda-manipulate-query-subtract ()
7714 "Manipulate the query by adding a search term with negative selection.
7715 Negative selection means term must not be matched for selection of an entry."
7716 (interactive)
7717 (org-agenda-manipulate-query ?\]))
7718 (defun org-agenda-manipulate-query-add-re ()
7719 "Manipulate the query by adding a search regexp with positive selection.
7720 Positive selection means the regexp must match for selection of an entry."
7721 (interactive)
7722 (org-agenda-manipulate-query ?\{))
7723 (defun org-agenda-manipulate-query-subtract-re ()
7724 "Manipulate the query by adding a search regexp with negative selection.
7725 Negative selection means regexp must not match for selection of an entry."
7726 (interactive)
7727 (org-agenda-manipulate-query ?\}))
7728 (defun org-agenda-manipulate-query (char)
7729 (cond
7730 ((memq org-agenda-type '(timeline agenda))
7731 (let ((org-agenda-include-inactive-timestamps t))
7732 (org-agenda-redo))
7733 (message "Display now includes inactive timestamps as well"))
7734 ((eq org-agenda-type 'search)
7735 (org-add-to-string
7736 'org-agenda-query-string
7737 (if org-agenda-last-search-view-search-was-boolean
7738 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7739 (?\{ . " +{}") (?\} . " -{}"))))
7740 " "))
7741 (setq org-agenda-redo-command
7742 (list 'org-search-view
7743 (car (get-text-property (min (1- (point-max)) (point))
7744 'org-last-args))
7745 org-agenda-query-string
7746 (+ (length org-agenda-query-string)
7747 (if (member char '(?\{ ?\})) 0 1))))
7748 (set-register org-agenda-query-register org-agenda-query-string)
7749 (let ((org-agenda-overriding-arguments
7750 (cdr org-agenda-redo-command)))
7751 (org-agenda-redo)))
7752 (t (error "Cannot manipulate query for %s-type agenda buffers"
7753 org-agenda-type))))
7755 (defun org-add-to-string (var string)
7756 (set var (concat (symbol-value var) string)))
7758 (defun org-agenda-goto-date (span)
7759 "Jump to DATE in agenda."
7760 (interactive "P")
7761 (let* ((org-read-date-prefer-future
7762 (eval org-agenda-jump-prefer-future))
7763 (date (org-read-date))
7764 (day (time-to-days (org-time-string-to-time date)))
7765 (org-agenda-sticky-orig org-agenda-sticky)
7766 (org-agenda-buffer-tmp-name (buffer-name))
7767 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7768 (0-arg (or current-prefix-arg (car args)))
7769 (2-arg (nth 2 args))
7770 (with-hour-p (nth 4 org-agenda-redo-command))
7771 (newcmd (list 'org-agenda-list 0-arg date
7772 (org-agenda-span-to-ndays
7773 2-arg (org-time-string-to-absolute date))
7774 with-hour-p))
7775 (newargs (cdr newcmd))
7776 (inhibit-read-only t)
7777 org-agenda-sticky)
7778 (if (not (org-agenda-check-type t 'agenda))
7779 (error "Not available in non-agenda views")
7780 (add-text-properties (point-min) (point-max)
7781 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7782 (org-agenda-redo)
7783 (goto-char (point-min))
7784 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7785 (save-excursion (move-beginning-of-line 2) (eobp))))
7786 (move-beginning-of-line 2))
7787 (setq org-agenda-sticky org-agenda-sticky-orig
7788 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7790 (defun org-agenda-goto-today ()
7791 "Go to today."
7792 (interactive)
7793 (org-agenda-check-type t 'timeline 'agenda)
7794 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7795 (curspan (nth 2 args))
7796 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7797 (cond
7798 (tdpos (goto-char tdpos))
7799 ((eq org-agenda-type 'agenda)
7800 (let* ((sd (org-agenda-compute-starting-span
7801 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7802 (org-agenda-overriding-arguments args))
7803 (setf (nth 1 org-agenda-overriding-arguments) sd)
7804 (org-agenda-redo)
7805 (org-agenda-find-same-or-today-or-agenda)))
7806 (t (error "Cannot find today")))))
7808 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7809 (goto-char
7810 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7811 (text-property-any (point-min) (point-max) 'org-today t)
7812 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7813 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7814 (org-agenda-goto-block-beginning))
7815 (point-min))))
7817 (defun org-agenda-goto-block-beginning ()
7818 "Go the agenda block beginning."
7819 (interactive)
7820 (if (not (derived-mode-p 'org-agenda-mode))
7821 (error "Cannot execute this command outside of org-agenda-mode buffers")
7822 (let (dest)
7823 (save-excursion
7824 (unless (looking-at "\\'")
7825 (forward-char))
7826 (let* ((prop 'org-agenda-structural-header)
7827 (p (previous-single-property-change (point) prop))
7828 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7829 (1- (point))) prop)))
7830 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7831 (if (not dest)
7832 (error "Cannot find the beginning of the blog")
7833 (goto-char dest)
7834 (move-beginning-of-line 1)))))
7836 (defun org-agenda-later (arg)
7837 "Go forward in time by the current span.
7838 With prefix ARG, go forward that many times the current span."
7839 (interactive "p")
7840 (org-agenda-check-type t 'agenda)
7841 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7842 (span (or (nth 2 args) org-agenda-current-span))
7843 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7844 (greg (calendar-gregorian-from-absolute sd))
7845 (cnt (org-get-at-bol 'org-day-cnt))
7846 greg2)
7847 (cond
7848 ((numberp span)
7849 (setq sd (+ (* span arg) sd)))
7850 ((eq span 'day)
7851 (setq sd (+ arg sd)))
7852 ((eq span 'week)
7853 (setq sd (+ (* 7 arg) sd)))
7854 ((eq span 'fortnight)
7855 (setq sd (+ (* 14 arg) sd)))
7856 ((eq span 'month)
7857 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7858 sd (calendar-absolute-from-gregorian greg2))
7859 (setcar greg2 (1+ (car greg2))))
7860 ((eq span 'year)
7861 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7862 sd (calendar-absolute-from-gregorian greg2))
7863 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7865 (setq sd (+ (* span arg) sd))))
7866 (let ((org-agenda-overriding-cmd
7867 ;; `cmd' may have been set by `org-agenda-run-series' which
7868 ;; uses `org-agenda-overriding-cmd' to decide whether
7869 ;; overriding is allowed for `cmd'
7870 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7871 (org-agenda-overriding-arguments
7872 (list (car args) sd span)))
7873 (org-agenda-redo)
7874 (org-agenda-find-same-or-today-or-agenda cnt))))
7876 (defun org-agenda-earlier (arg)
7877 "Go backward in time by the current span.
7878 With prefix ARG, go backward that many times the current span."
7879 (interactive "p")
7880 (org-agenda-later (- arg)))
7882 (defun org-agenda-view-mode-dispatch ()
7883 "Call one of the view mode commands."
7884 (interactive)
7885 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7886 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7887 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7888 (let ((a (read-char-exclusive)))
7889 (case a
7890 (?\ (call-interactively 'org-agenda-reset-view))
7891 (?d (call-interactively 'org-agenda-day-view))
7892 (?w (call-interactively 'org-agenda-week-view))
7893 (?t (call-interactively 'org-agenda-fortnight-view))
7894 (?m (call-interactively 'org-agenda-month-view))
7895 (?y (call-interactively 'org-agenda-year-view))
7896 (?l (call-interactively 'org-agenda-log-mode))
7897 (?L (org-agenda-log-mode '(4)))
7898 (?c (org-agenda-log-mode 'clockcheck))
7899 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7900 (?a (call-interactively 'org-agenda-archives-mode))
7901 (?A (org-agenda-archives-mode 'files))
7902 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7903 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7904 (?G (call-interactively 'org-agenda-toggle-time-grid))
7905 (?D (call-interactively 'org-agenda-toggle-diary))
7906 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7907 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7908 (org-agenda-check-type t 'timeline 'agenda)
7909 (org-agenda-redo))
7910 (message "Display now includes inactive timestamps as well"))
7911 (?q (message "Abort"))
7912 (otherwise (error "Invalid key" )))))
7914 (defun org-agenda-reset-view ()
7915 "Switch to default view for agenda."
7916 (interactive)
7917 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7918 (defun org-agenda-day-view (&optional day-of-month)
7919 "Switch to daily view for agenda.
7920 With argument DAY-OF-MONTH, switch to that day of the month."
7921 (interactive "P")
7922 (org-agenda-change-time-span 'day day-of-month))
7923 (defun org-agenda-week-view (&optional iso-week)
7924 "Switch to daily view for agenda.
7925 With argument ISO-WEEK, switch to the corresponding ISO week.
7926 If ISO-WEEK has more then 2 digits, only the last two encode the
7927 week. Any digits before this encode a year. So 200712 means
7928 week 12 of year 2007. Years in the range 1938-2037 can also be
7929 written as 2-digit years."
7930 (interactive "P")
7931 (org-agenda-change-time-span 'week iso-week))
7932 (defun org-agenda-fortnight-view (&optional iso-week)
7933 "Switch to daily view for agenda.
7934 With argument ISO-WEEK, switch to the corresponding ISO week.
7935 If ISO-WEEK has more then 2 digits, only the last two encode the
7936 week. Any digits before this encode a year. So 200712 means
7937 week 12 of year 2007. Years in the range 1938-2037 can also be
7938 written as 2-digit years."
7939 (interactive "P")
7940 (org-agenda-change-time-span 'fortnight iso-week))
7941 (defun org-agenda-month-view (&optional month)
7942 "Switch to monthly view for agenda.
7943 With argument MONTH, switch to that month."
7944 (interactive "P")
7945 (org-agenda-change-time-span 'month month))
7946 (defun org-agenda-year-view (&optional year)
7947 "Switch to yearly view for agenda.
7948 With argument YEAR, switch to that year.
7949 If MONTH has more then 2 digits, only the last two encode the
7950 month. Any digits before this encode a year. So 200712 means
7951 December year 2007. Years in the range 1938-2037 can also be
7952 written as 2-digit years."
7953 (interactive "P")
7954 (when year
7955 (setq year (org-small-year-to-year year)))
7956 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7957 (org-agenda-change-time-span 'year year)
7958 (error "Abort")))
7960 (defun org-agenda-change-time-span (span &optional n)
7961 "Change the agenda view to SPAN.
7962 SPAN may be `day', `week', `fortnight', `month', `year'."
7963 (org-agenda-check-type t 'agenda)
7964 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7965 (curspan (nth 2 args)))
7966 (if (and (not n) (equal curspan span))
7967 (error "Viewing span is already \"%s\"" span))
7968 (let* ((sd (or (org-get-at-bol 'day)
7969 (nth 1 args)
7970 org-starting-day))
7971 (sd (org-agenda-compute-starting-span sd span n))
7972 (org-agenda-overriding-cmd
7973 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7974 (org-agenda-overriding-arguments
7975 (list (car args) sd span)))
7976 (org-agenda-redo)
7977 (org-agenda-find-same-or-today-or-agenda))
7978 (org-agenda-set-mode-name)
7979 (message "Switched to %s view" span)))
7981 (defun org-agenda-compute-starting-span (sd span &optional n)
7982 "Compute starting date for agenda.
7983 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7984 is a cons cell with the starting date and the number of days,
7985 so that the date SD will be in that range."
7986 (let* ((greg (calendar-gregorian-from-absolute sd))
7987 (dg (nth 1 greg))
7988 (mg (car greg))
7989 (yg (nth 2 greg)))
7990 (cond
7991 ((eq span 'day)
7992 (when n
7993 (setq sd (+ (calendar-absolute-from-gregorian
7994 (list mg 1 yg))
7995 n -1))))
7996 ((or (eq span 'week) (eq span 'fortnight))
7997 (let* ((nt (calendar-day-of-week
7998 (calendar-gregorian-from-absolute sd)))
7999 (d (if org-agenda-start-on-weekday
8000 (- nt org-agenda-start-on-weekday)
8003 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8004 (when n
8005 (require 'cal-iso)
8006 (when (> n 99)
8007 (setq y1 (org-small-year-to-year (/ n 100))
8008 n (mod n 100)))
8009 (setq sd
8010 (calendar-absolute-from-iso
8011 (list n 1
8012 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8013 ((eq span 'month)
8014 (let (y1)
8015 (when (and n (> n 99))
8016 (setq y1 (org-small-year-to-year (/ n 100))
8017 n (mod n 100)))
8018 (setq sd (calendar-absolute-from-gregorian
8019 (list (or n mg) 1 (or y1 yg))))))
8020 ((eq span 'year)
8021 (setq sd (calendar-absolute-from-gregorian
8022 (list 1 1 (or n yg))))))
8023 sd))
8025 (defun org-agenda-next-date-line (&optional arg)
8026 "Jump to the next line indicating a date in agenda buffer."
8027 (interactive "p")
8028 (org-agenda-check-type t 'agenda 'timeline)
8029 (beginning-of-line 1)
8030 ;; This does not work if user makes date format that starts with a blank
8031 (if (looking-at "^\\S-") (forward-char 1))
8032 (if (not (re-search-forward "^\\S-" nil t arg))
8033 (progn
8034 (backward-char 1)
8035 (error "No next date after this line in this buffer")))
8036 (goto-char (match-beginning 0)))
8038 (defun org-agenda-previous-date-line (&optional arg)
8039 "Jump to the previous line indicating a date in agenda buffer."
8040 (interactive "p")
8041 (org-agenda-check-type t 'agenda 'timeline)
8042 (beginning-of-line 1)
8043 (if (not (re-search-backward "^\\S-" nil t arg))
8044 (error "No previous date before this line in this buffer")))
8046 ;; Initialize the highlight
8047 (defvar org-hl (make-overlay 1 1))
8048 (overlay-put org-hl 'face 'highlight)
8050 (defun org-highlight (begin end &optional buffer)
8051 "Highlight a region with overlay."
8052 (move-overlay org-hl begin end (or buffer (current-buffer))))
8054 (defun org-unhighlight ()
8055 "Detach overlay INDEX."
8056 (org-detach-overlay org-hl))
8058 (defun org-unhighlight-once ()
8059 "Remove the highlight from its position, and this function from the hook."
8060 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8061 (org-unhighlight))
8063 (defvar org-agenda-pre-follow-window-conf nil)
8064 (defun org-agenda-follow-mode ()
8065 "Toggle follow mode in an agenda buffer."
8066 (interactive)
8067 (unless org-agenda-follow-mode
8068 (setq org-agenda-pre-follow-window-conf
8069 (current-window-configuration)))
8070 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8071 (unless org-agenda-follow-mode
8072 (set-window-configuration org-agenda-pre-follow-window-conf))
8073 (org-agenda-set-mode-name)
8074 (org-agenda-do-context-action)
8075 (message "Follow mode is %s"
8076 (if org-agenda-follow-mode "on" "off")))
8078 (defun org-agenda-entry-text-mode (&optional arg)
8079 "Toggle entry text mode in an agenda buffer."
8080 (interactive "P")
8081 (if (or org-agenda-tag-filter
8082 org-agenda-category-filter
8083 org-agenda-regexp-filter
8084 org-agenda-top-headline-filter)
8085 (user-error "Can't show entry text in filtered views")
8086 (setq org-agenda-entry-text-mode (or (integerp arg)
8087 (not org-agenda-entry-text-mode)))
8088 (org-agenda-entry-text-hide)
8089 (and org-agenda-entry-text-mode
8090 (let ((org-agenda-entry-text-maxlines
8091 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8092 (org-agenda-entry-text-show)))
8093 (org-agenda-set-mode-name)
8094 (message "Entry text mode is %s%s"
8095 (if org-agenda-entry-text-mode "on" "off")
8096 (if (not org-agenda-entry-text-mode) ""
8097 (format " (maximum number of lines is %d)"
8098 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8100 (defun org-agenda-clockreport-mode (&optional with-filter)
8101 "Toggle clocktable mode in an agenda buffer.
8102 With prefix arg WITH-FILTER, make the clocktable respect the current
8103 agenda filter."
8104 (interactive "P")
8105 (org-agenda-check-type t 'agenda)
8106 (if with-filter
8107 (setq org-agenda-clockreport-mode 'with-filter)
8108 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8109 (org-agenda-set-mode-name)
8110 (org-agenda-redo)
8111 (message "Clocktable mode is %s"
8112 (if org-agenda-clockreport-mode "on" "off")))
8114 (defun org-agenda-log-mode (&optional special)
8115 "Toggle log mode in an agenda buffer.
8116 With argument SPECIAL, show all possible log items, not only the ones
8117 configured in `org-agenda-log-mode-items'.
8118 With a double `C-u' prefix arg, show *only* log items, nothing else."
8119 (interactive "P")
8120 (org-agenda-check-type t 'agenda 'timeline)
8121 (setq org-agenda-show-log
8122 (cond
8123 ((equal special '(16)) 'only)
8124 ((eq special 'clockcheck)
8125 (if (eq org-agenda-show-log 'clockcheck)
8126 nil 'clockcheck))
8127 (special '(closed clock state))
8128 (t (not org-agenda-show-log))))
8129 (org-agenda-set-mode-name)
8130 (org-agenda-redo)
8131 (message "Log mode is %s"
8132 (if org-agenda-show-log "on" "off")))
8134 (defun org-agenda-archives-mode (&optional with-files)
8135 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8136 When called with a prefix argument, include all archive files as well."
8137 (interactive "P")
8138 (setq org-agenda-archives-mode
8139 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8140 (org-agenda-set-mode-name)
8141 (org-agenda-redo)
8142 (message
8143 "%s"
8144 (cond
8145 ((eq org-agenda-archives-mode nil)
8146 "No archives are included")
8147 ((eq org-agenda-archives-mode 'trees)
8148 (format "Trees with :%s: tag are included" org-archive-tag))
8149 ((eq org-agenda-archives-mode t)
8150 (format "Trees with :%s: tag and all active archive files are included"
8151 org-archive-tag)))))
8153 (defun org-agenda-toggle-diary ()
8154 "Toggle diary inclusion in an agenda buffer."
8155 (interactive)
8156 (org-agenda-check-type t 'agenda)
8157 (setq org-agenda-include-diary (not org-agenda-include-diary))
8158 (org-agenda-redo)
8159 (org-agenda-set-mode-name)
8160 (message "Diary inclusion turned %s"
8161 (if org-agenda-include-diary "on" "off")))
8163 (defun org-agenda-toggle-deadlines ()
8164 "Toggle inclusion of entries with a deadline in an agenda buffer."
8165 (interactive)
8166 (org-agenda-check-type t 'agenda)
8167 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8168 (org-agenda-redo)
8169 (org-agenda-set-mode-name)
8170 (message "Deadlines inclusion turned %s"
8171 (if org-agenda-include-deadlines "on" "off")))
8173 (defun org-agenda-toggle-time-grid ()
8174 "Toggle time grid in an agenda buffer."
8175 (interactive)
8176 (org-agenda-check-type t 'agenda)
8177 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8178 (org-agenda-redo)
8179 (org-agenda-set-mode-name)
8180 (message "Time-grid turned %s"
8181 (if org-agenda-use-time-grid "on" "off")))
8183 (defun org-agenda-set-mode-name ()
8184 "Set the mode name to indicate all the small mode settings."
8185 (setq mode-name
8186 (list "Org-Agenda"
8187 (if (get 'org-agenda-files 'org-restrict) " []" "")
8189 '(:eval (org-agenda-span-name org-agenda-current-span))
8190 (if org-agenda-follow-mode " Follow" "")
8191 (if org-agenda-entry-text-mode " ETxt" "")
8192 (if org-agenda-include-diary " Diary" "")
8193 (if org-agenda-include-deadlines " Ddl" "")
8194 (if org-agenda-use-time-grid " Grid" "")
8195 (if (and (boundp 'org-habit-show-habits)
8196 org-habit-show-habits) " Habit" "")
8197 (cond
8198 ((consp org-agenda-show-log) " LogAll")
8199 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8200 (org-agenda-show-log " Log")
8201 (t ""))
8202 (if (or org-agenda-category-filter
8203 (get 'org-agenda-category-filter :preset-filter))
8204 '(:eval (org-propertize
8205 (concat " <"
8206 (mapconcat
8207 'identity
8208 (append
8209 (get 'org-agenda-category-filter :preset-filter)
8210 org-agenda-category-filter)
8212 ">")
8213 'face 'org-agenda-filter-category
8214 'help-echo "Category used in filtering")) "")
8215 (if (or org-agenda-tag-filter
8216 (get 'org-agenda-tag-filter :preset-filter))
8217 '(:eval (org-propertize
8218 (concat " {"
8219 (mapconcat
8220 'identity
8221 (append
8222 (get 'org-agenda-tag-filter :preset-filter)
8223 org-agenda-tag-filter)
8225 "}")
8226 'face 'org-agenda-filter-tags
8227 'help-echo "Tags used in filtering")) "")
8228 (if (or org-agenda-regexp-filter
8229 (get 'org-agenda-regexp-filter :preset-filter))
8230 '(:eval (org-propertize
8231 (concat " ["
8232 (mapconcat
8233 'identity
8234 (append
8235 (get 'org-agenda-regexp-filter :preset-filter)
8236 org-agenda-regexp-filter)
8238 "]")
8239 'face 'org-agenda-filter-regexp
8240 'help-echo "Regexp used in filtering")) "")
8241 (if org-agenda-archives-mode
8242 (if (eq org-agenda-archives-mode t)
8243 " Archives"
8244 (format " :%s:" org-archive-tag))
8246 (if org-agenda-clockreport-mode
8247 (if (eq org-agenda-clockreport-mode 'with-filter)
8248 " Clock{}" " Clock")
8249 "")))
8250 (force-mode-line-update))
8252 (define-obsolete-function-alias
8253 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8255 (defun org-agenda-update-agenda-type ()
8256 "Update the agenda type after each command."
8257 (setq org-agenda-type
8258 (or (get-text-property (point) 'org-agenda-type)
8259 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8261 (defun org-agenda-next-line ()
8262 "Move cursor to the next line, and show if follow mode is active."
8263 (interactive)
8264 (call-interactively 'next-line)
8265 (org-agenda-do-context-action))
8267 (defun org-agenda-previous-line ()
8268 "Move cursor to the previous line, and show if follow-mode is active."
8269 (interactive)
8270 (call-interactively 'previous-line)
8271 (org-agenda-do-context-action))
8273 (defun org-agenda-next-item (n)
8274 "Move cursor to next agenda item."
8275 (interactive "p")
8276 (let ((col (current-column)))
8277 (dotimes (c n)
8278 (when (next-single-property-change (point-at-eol) 'org-marker)
8279 (move-end-of-line 1)
8280 (goto-char (next-single-property-change (point) 'org-marker))))
8281 (org-move-to-column col))
8282 (org-agenda-do-context-action))
8284 (defun org-agenda-previous-item (n)
8285 "Move cursor to next agenda item."
8286 (interactive "p")
8287 (dotimes (c n)
8288 (let ((col (current-column))
8289 (goto (save-excursion
8290 (move-end-of-line 0)
8291 (previous-single-property-change (point) 'org-marker))))
8292 (if goto (goto-char goto))
8293 (org-move-to-column col)))
8294 (org-agenda-do-context-action))
8296 (defun org-agenda-do-context-action ()
8297 "Show outline path and, maybe, follow mode window."
8298 (let ((m (org-get-at-bol 'org-marker)))
8299 (when (and (markerp m) (marker-buffer m))
8300 (and org-agenda-follow-mode
8301 (if org-agenda-follow-indirect
8302 (org-agenda-tree-to-indirect-buffer nil)
8303 (org-agenda-show)))
8304 (and org-agenda-show-outline-path
8305 (org-with-point-at m (org-display-outline-path t))))))
8307 (defun org-agenda-show-tags ()
8308 "Show the tags applicable to the current item."
8309 (interactive)
8310 (let* ((tags (org-get-at-bol 'tags)))
8311 (if tags
8312 (message "Tags are :%s:"
8313 (org-no-properties (mapconcat 'identity tags ":")))
8314 (message "No tags associated with this line"))))
8316 (defun org-agenda-goto (&optional highlight)
8317 "Go to the Org-mode file which contains the item at point."
8318 (interactive)
8319 (let* ((marker (or (org-get-at-bol 'org-marker)
8320 (org-agenda-error)))
8321 (buffer (marker-buffer marker))
8322 (pos (marker-position marker)))
8323 (switch-to-buffer-other-window buffer)
8324 (widen)
8325 (push-mark)
8326 (goto-char pos)
8327 (when (derived-mode-p 'org-mode)
8328 (org-show-context 'agenda)
8329 (save-excursion
8330 (and (outline-next-heading)
8331 (org-flag-heading nil)))) ; show the next heading
8332 (when (outline-invisible-p)
8333 (show-entry)) ; display invisible text
8334 (recenter (/ (window-height) 2))
8335 (run-hooks 'org-agenda-after-show-hook)
8336 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8338 (defvar org-agenda-after-show-hook nil
8339 "Normal hook run after an item has been shown from the agenda.
8340 Point is in the buffer where the item originated.")
8342 (defun org-agenda-kill ()
8343 "Kill the entry or subtree belonging to the current agenda entry."
8344 (interactive)
8345 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8346 (let* ((bufname-orig (buffer-name))
8347 (marker (or (org-get-at-bol 'org-marker)
8348 (org-agenda-error)))
8349 (buffer (marker-buffer marker))
8350 (pos (marker-position marker))
8351 (type (org-get-at-bol 'type))
8352 dbeg dend (n 0) conf)
8353 (org-with-remote-undo buffer
8354 (with-current-buffer buffer
8355 (save-excursion
8356 (goto-char pos)
8357 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8358 (setq dbeg (progn (org-back-to-heading t) (point))
8359 dend (org-end-of-subtree t t))
8360 (setq dbeg (point-at-bol)
8361 dend (min (point-max) (1+ (point-at-eol)))))
8362 (goto-char dbeg)
8363 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8364 (setq conf (or (eq t org-agenda-confirm-kill)
8365 (and (numberp org-agenda-confirm-kill)
8366 (> n org-agenda-confirm-kill))))
8367 (and conf
8368 (not (y-or-n-p
8369 (format "Delete entry with %d lines in buffer \"%s\"? "
8370 n (buffer-name buffer))))
8371 (error "Abort"))
8372 (let ((org-agenda-buffer-name bufname-orig))
8373 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8374 (with-current-buffer buffer (delete-region dbeg dend))
8375 (message "Agenda item and source killed"))))
8377 (defvar org-archive-default-command) ; defined in org-archive.el
8378 (defun org-agenda-archive-default ()
8379 "Archive the entry or subtree belonging to the current agenda entry."
8380 (interactive)
8381 (require 'org-archive)
8382 (org-agenda-archive-with org-archive-default-command))
8384 (defun org-agenda-archive-default-with-confirmation ()
8385 "Archive the entry or subtree belonging to the current agenda entry."
8386 (interactive)
8387 (require 'org-archive)
8388 (org-agenda-archive-with org-archive-default-command 'confirm))
8390 (defun org-agenda-archive ()
8391 "Archive the entry or subtree belonging to the current agenda entry."
8392 (interactive)
8393 (org-agenda-archive-with 'org-archive-subtree))
8395 (defun org-agenda-archive-to-archive-sibling ()
8396 "Move the entry to the archive sibling."
8397 (interactive)
8398 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8400 (defun org-agenda-archive-with (cmd &optional confirm)
8401 "Move the entry to the archive sibling."
8402 (interactive)
8403 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8404 (let* ((bufname-orig (buffer-name))
8405 (marker (or (org-get-at-bol 'org-marker)
8406 (org-agenda-error)))
8407 (buffer (marker-buffer marker))
8408 (pos (marker-position marker)))
8409 (org-with-remote-undo buffer
8410 (with-current-buffer buffer
8411 (if (derived-mode-p 'org-mode)
8412 (if (and confirm
8413 (not (y-or-n-p "Archive this subtree or entry? ")))
8414 (error "Abort")
8415 (save-window-excursion
8416 (goto-char pos)
8417 (let ((org-agenda-buffer-name bufname-orig))
8418 (org-remove-subtree-entries-from-agenda))
8419 (org-back-to-heading t)
8420 (funcall cmd)))
8421 (error "Archiving works only in Org-mode files"))))))
8423 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8424 "Remove all lines in the agenda that correspond to a given subtree.
8425 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8426 If this information is not given, the function uses the tree at point."
8427 (let ((buf (or buf (current-buffer))) m p)
8428 (save-excursion
8429 (unless (and beg end)
8430 (org-back-to-heading t)
8431 (setq beg (point))
8432 (org-end-of-subtree t)
8433 (setq end (point)))
8434 (set-buffer (get-buffer org-agenda-buffer-name))
8435 (save-excursion
8436 (goto-char (point-max))
8437 (beginning-of-line 1)
8438 (while (not (bobp))
8439 (when (and (setq m (org-get-at-bol 'org-marker))
8440 (equal buf (marker-buffer m))
8441 (setq p (marker-position m))
8442 (>= p beg)
8443 (< p end))
8444 (let ((inhibit-read-only t))
8445 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8446 (beginning-of-line 0))))))
8448 (defun org-agenda-refile (&optional goto rfloc no-update)
8449 "Refile the item at point.
8451 When GOTO is 0 or '(64), clear the refile cache.
8452 When GOTO is '(16), go to the location of the last refiled item.
8453 RFLOC can be a refile location obtained in a different way.
8454 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8455 (interactive "P")
8456 (cond
8457 ((member goto '(0 (64)))
8458 (org-refile-cache-clear))
8459 ((equal goto '(16))
8460 (org-refile-goto-last-stored))
8462 (let* ((buffer-orig (buffer-name))
8463 (marker (or (org-get-at-bol 'org-hd-marker)
8464 (org-agenda-error)))
8465 (buffer (marker-buffer marker))
8466 (pos (marker-position marker))
8467 (rfloc (or rfloc
8468 (org-refile-get-location
8469 (if goto "Goto" "Refile to") buffer
8470 org-refile-allow-creating-parent-nodes))))
8471 (with-current-buffer buffer
8472 (save-excursion
8473 (save-restriction
8474 (widen)
8475 (goto-char marker)
8476 (let ((org-agenda-buffer-name buffer-orig))
8477 (org-remove-subtree-entries-from-agenda))
8478 (org-refile goto buffer rfloc)))))
8479 (unless no-update (org-agenda-redo)))))
8481 (defun org-agenda-open-link (&optional arg)
8482 "Open the link(s) in the current entry, if any.
8483 This looks for a link in the displayed line in the agenda.
8484 It also looks at the text of the entry itself."
8485 (interactive "P")
8486 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8487 (org-get-at-bol 'org-marker)))
8488 (buffer (and marker (marker-buffer marker)))
8489 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8490 (lkall (and buffer (org-offer-links-in-entry
8491 buffer marker arg prefix)))
8492 (lk0 (car lkall))
8493 (lk (if (stringp lk0) (list lk0) lk0))
8494 (lkend (cdr lkall))
8495 trg)
8496 (cond
8497 ((and buffer lk)
8498 (mapcar (lambda(l)
8499 (with-current-buffer buffer
8500 (setq trg (and (string-match org-bracket-link-regexp l)
8501 (match-string 1 l)))
8502 (if (or (not trg) (string-match org-any-link-re trg))
8503 (save-excursion
8504 (save-restriction
8505 (widen)
8506 (goto-char marker)
8507 (when (search-forward l nil lkend)
8508 (goto-char (match-beginning 0))
8509 (org-open-at-point))))
8510 ;; This is an internal link, widen the buffer
8511 (switch-to-buffer-other-window buffer)
8512 (widen)
8513 (goto-char marker)
8514 (when (search-forward l nil lkend)
8515 (goto-char (match-beginning 0))
8516 (org-open-at-point)))))
8517 lk))
8518 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8519 (save-excursion
8520 (beginning-of-line 1)
8521 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8522 (org-open-link-from-string (match-string 1)))
8523 (t (message "No link to open here")))))
8525 (defun org-agenda-copy-local-variable (var)
8526 "Get a variable from a referenced buffer and install it here."
8527 (let ((m (org-get-at-bol 'org-marker)))
8528 (when (and m (buffer-live-p (marker-buffer m)))
8529 (org-set-local var (with-current-buffer (marker-buffer m)
8530 (symbol-value var))))))
8532 (defun org-agenda-switch-to (&optional delete-other-windows)
8533 "Go to the Org-mode file which contains the item at point."
8534 (interactive)
8535 (if (and org-return-follows-link
8536 (not (org-get-at-bol 'org-marker))
8537 (org-in-regexp org-bracket-link-regexp))
8538 (org-open-link-from-string (match-string 0))
8539 (let* ((marker (or (org-get-at-bol 'org-marker)
8540 (org-agenda-error)))
8541 (buffer (marker-buffer marker))
8542 (pos (marker-position marker)))
8543 (org-pop-to-buffer-same-window buffer)
8544 (and delete-other-windows (delete-other-windows))
8545 (widen)
8546 (goto-char pos)
8547 (when (derived-mode-p 'org-mode)
8548 (org-show-context 'agenda)
8549 (save-excursion
8550 (and (outline-next-heading)
8551 (org-flag-heading nil))) ; show the next heading
8552 (when (outline-invisible-p)
8553 (show-entry)) ; display invisible text
8554 (run-hooks 'org-agenda-after-show-hook)))))
8556 (defun org-agenda-goto-mouse (ev)
8557 "Go to the Org-mode file which contains the item at the mouse click."
8558 (interactive "e")
8559 (mouse-set-point ev)
8560 (org-agenda-goto))
8562 (defun org-agenda-show (&optional full-entry)
8563 "Display the Org-mode file which contains the item at point.
8564 With prefix argument FULL-ENTRY, make the entire entry visible
8565 if it was hidden in the outline."
8566 (interactive "P")
8567 (let ((win (selected-window)))
8568 (if full-entry
8569 (let ((org-show-entry-below t))
8570 (org-agenda-goto t))
8571 (org-agenda-goto t))
8572 (select-window win)))
8574 (defvar org-agenda-show-window nil)
8575 (defun org-agenda-show-and-scroll-up (&optional arg)
8576 "Display the Org-mode file which contains the item at point.
8577 When called repeatedly, scroll the window that is displaying the buffer.
8578 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8579 `show-subtree' to display the item, so that drawers and logbooks stay
8580 folded."
8581 (interactive "P")
8582 (let ((win (selected-window)))
8583 (if (and (window-live-p org-agenda-show-window)
8584 (eq this-command last-command))
8585 (progn
8586 (select-window org-agenda-show-window)
8587 (ignore-errors (scroll-up)))
8588 (org-agenda-goto t)
8589 (if arg (org-show-entry) (show-subtree))
8590 (setq org-agenda-show-window (selected-window)))
8591 (select-window win)))
8593 (defun org-agenda-show-scroll-down ()
8594 "Scroll down the window showing the agenda."
8595 (interactive)
8596 (let ((win (selected-window)))
8597 (when (window-live-p org-agenda-show-window)
8598 (select-window org-agenda-show-window)
8599 (ignore-errors (scroll-down))
8600 (select-window win))))
8602 (defun org-agenda-show-1 (&optional more)
8603 "Display the Org-mode file which contains the item at point.
8604 The prefix arg selects the amount of information to display:
8606 0 hide the subtree
8607 1 just show the entry according to defaults.
8608 2 show the children view
8609 3 show the subtree view
8610 4 show the entire subtree and any LOGBOOK drawers
8611 5 show the entire subtree and any drawers
8612 With prefix argument FULL-ENTRY, make the entire entry visible
8613 if it was hidden in the outline."
8614 (interactive "p")
8615 (let ((win (selected-window)))
8616 (org-agenda-goto t)
8617 (org-recenter-heading 1)
8618 (cond
8619 ((= more 0)
8620 (hide-subtree)
8621 (save-excursion
8622 (org-back-to-heading)
8623 (run-hook-with-args 'org-cycle-hook 'folded))
8624 (message "Remote: FOLDED"))
8625 ((and (org-called-interactively-p 'any) (= more 1))
8626 (message "Remote: show with default settings"))
8627 ((= more 2)
8628 (show-entry)
8629 (show-children)
8630 (save-excursion
8631 (org-back-to-heading)
8632 (run-hook-with-args 'org-cycle-hook 'children))
8633 (message "Remote: CHILDREN"))
8634 ((= more 3)
8635 (show-subtree)
8636 (save-excursion
8637 (org-back-to-heading)
8638 (run-hook-with-args 'org-cycle-hook 'subtree))
8639 (message "Remote: SUBTREE"))
8640 ((= more 4)
8641 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8642 (org-drawer-regexp
8643 (concat "^[ \t]*:\\("
8644 (mapconcat 'regexp-quote org-drawers "\\|")
8645 "\\):[ \t]*$")))
8646 (show-subtree)
8647 (save-excursion
8648 (org-back-to-heading)
8649 (org-cycle-hide-drawers 'subtree)))
8650 (message "Remote: SUBTREE AND LOGBOOK"))
8651 ((> more 4)
8652 (show-subtree)
8653 (message "Remote: SUBTREE AND ALL DRAWERS")))
8654 (select-window win)))
8656 (defun org-recenter-heading (n)
8657 (save-excursion
8658 (org-back-to-heading)
8659 (recenter n)))
8661 (defvar org-agenda-cycle-counter nil)
8662 (defun org-agenda-cycle-show (&optional n)
8663 "Show the current entry in another window, with default settings.
8664 Default settings are taken from `org-show-hierarchy-above' and siblings.
8665 When use repeatedly in immediate succession, the remote entry will cycle
8666 through visibility
8668 children -> subtree -> folded
8670 When called with a numeric prefix arg, that arg will be passed through to
8671 `org-agenda-show-1'. For the interpretation of that argument, see the
8672 docstring of `org-agenda-show-1'."
8673 (interactive "P")
8674 (if (integerp n)
8675 (setq org-agenda-cycle-counter n)
8676 (if (not (eq last-command this-command))
8677 (setq org-agenda-cycle-counter 1)
8678 (if (equal org-agenda-cycle-counter 0)
8679 (setq org-agenda-cycle-counter 2)
8680 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8681 (if (> org-agenda-cycle-counter 3)
8682 (setq org-agenda-cycle-counter 0)))))
8683 (org-agenda-show-1 org-agenda-cycle-counter))
8685 (defun org-agenda-recenter (arg)
8686 "Display the Org-mode file which contains the item at point and recenter."
8687 (interactive "P")
8688 (let ((win (selected-window)))
8689 (org-agenda-goto t)
8690 (recenter arg)
8691 (select-window win)))
8693 (defun org-agenda-show-mouse (ev)
8694 "Display the Org-mode file which contains the item at the mouse click."
8695 (interactive "e")
8696 (mouse-set-point ev)
8697 (org-agenda-show))
8699 (defun org-agenda-check-no-diary ()
8700 "Check if the entry is a diary link and abort if yes."
8701 (if (org-get-at-bol 'org-agenda-diary-link)
8702 (org-agenda-error)))
8704 (defun org-agenda-error ()
8705 (error "Command not allowed in this line"))
8707 (defun org-agenda-tree-to-indirect-buffer (arg)
8708 "Show the subtree corresponding to the current entry in an indirect buffer.
8709 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8711 With a numerical prefix ARG, go up to this level and then take that tree.
8712 With a negative numeric ARG, go up by this number of levels.
8713 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8714 use the dedicated frame)."
8715 (interactive "P")
8716 (if current-prefix-arg
8717 (org-agenda-do-tree-to-indirect-buffer arg)
8718 (let ((agenda-buffer (buffer-name))
8719 (agenda-window (selected-window))
8720 (indirect-window
8721 (and org-last-indirect-buffer
8722 (get-buffer-window org-last-indirect-buffer))))
8723 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8724 (unless (or (eq org-indirect-buffer-display 'new-frame)
8725 (eq org-indirect-buffer-display 'dedicated-frame))
8726 (unwind-protect
8727 (unless (and indirect-window (window-live-p indirect-window))
8728 (setq indirect-window (split-window agenda-window)))
8729 (and indirect-window (select-window indirect-window))
8730 (switch-to-buffer org-last-indirect-buffer :norecord)
8731 (fit-window-to-buffer indirect-window)))
8732 (select-window (get-buffer-window agenda-buffer)))))
8734 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8735 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8736 (org-agenda-check-no-diary)
8737 (let* ((marker (or (org-get-at-bol 'org-marker)
8738 (org-agenda-error)))
8739 (buffer (marker-buffer marker))
8740 (pos (marker-position marker)))
8741 (with-current-buffer buffer
8742 (save-excursion
8743 (goto-char pos)
8744 (funcall 'org-tree-to-indirect-buffer arg)))))
8746 (defvar org-last-heading-marker (make-marker)
8747 "Marker pointing to the headline that last changed its TODO state
8748 by a remote command from the agenda.")
8750 (defun org-agenda-todo-nextset ()
8751 "Switch TODO entry to next sequence."
8752 (interactive)
8753 (org-agenda-todo 'nextset))
8755 (defun org-agenda-todo-previousset ()
8756 "Switch TODO entry to previous sequence."
8757 (interactive)
8758 (org-agenda-todo 'previousset))
8760 (defun org-agenda-todo (&optional arg)
8761 "Cycle TODO state of line at point, also in Org-mode file.
8762 This changes the line at point, all other lines in the agenda referring to
8763 the same tree node, and the headline of the tree node in the Org-mode file."
8764 (interactive "P")
8765 (org-agenda-check-no-diary)
8766 (let* ((col (current-column))
8767 (marker (or (org-get-at-bol 'org-marker)
8768 (org-agenda-error)))
8769 (buffer (marker-buffer marker))
8770 (pos (marker-position marker))
8771 (hdmarker (org-get-at-bol 'org-hd-marker))
8772 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8773 (inhibit-read-only t)
8774 org-agenda-headline-snapshot-before-repeat newhead just-one)
8775 (org-with-remote-undo buffer
8776 (with-current-buffer buffer
8777 (widen)
8778 (goto-char pos)
8779 (org-show-context 'agenda)
8780 (save-excursion
8781 (and (outline-next-heading)
8782 (org-flag-heading nil))) ; show the next heading
8783 (let ((current-prefix-arg arg))
8784 (call-interactively 'org-todo))
8785 (and (bolp) (forward-char 1))
8786 (setq newhead (org-get-heading))
8787 (when (and (org-bound-and-true-p
8788 org-agenda-headline-snapshot-before-repeat)
8789 (not (equal org-agenda-headline-snapshot-before-repeat
8790 newhead))
8791 todayp)
8792 (setq newhead org-agenda-headline-snapshot-before-repeat
8793 just-one t))
8794 (save-excursion
8795 (org-back-to-heading)
8796 (move-marker org-last-heading-marker (point))))
8797 (beginning-of-line 1)
8798 (save-window-excursion
8799 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8800 (when (org-bound-and-true-p org-clock-out-when-done)
8801 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8802 newhead)
8803 (org-agenda-unmark-clocking-task))
8804 (org-move-to-column col))))
8806 (defun org-agenda-add-note (&optional arg)
8807 "Add a time-stamped note to the entry at point."
8808 (interactive "P")
8809 (org-agenda-check-no-diary)
8810 (let* ((marker (or (org-get-at-bol 'org-marker)
8811 (org-agenda-error)))
8812 (buffer (marker-buffer marker))
8813 (pos (marker-position marker))
8814 (hdmarker (org-get-at-bol 'org-hd-marker))
8815 (inhibit-read-only t))
8816 (with-current-buffer buffer
8817 (widen)
8818 (goto-char pos)
8819 (org-show-context 'agenda)
8820 (save-excursion
8821 (and (outline-next-heading)
8822 (org-flag-heading nil))) ; show the next heading
8823 (org-add-note))))
8825 (defun org-agenda-change-all-lines (newhead hdmarker
8826 &optional fixface just-this)
8827 "Change all lines in the agenda buffer which match HDMARKER.
8828 The new content of the line will be NEWHEAD (as modified by
8829 `org-agenda-format-item'). HDMARKER is checked with
8830 `equal' against all `org-hd-marker' text properties in the file.
8831 If FIXFACE is non-nil, the face of each item is modified according to
8832 the new TODO state.
8833 If JUST-THIS is non-nil, change just the current line, not all.
8834 If FORCE-TAGS is non nil, the car of it returns the new tags."
8835 (let* ((inhibit-read-only t)
8836 (line (org-current-line))
8837 (org-agenda-buffer (current-buffer))
8838 (thetags (with-current-buffer (marker-buffer hdmarker)
8839 (save-excursion (save-restriction (widen)
8840 (goto-char hdmarker)
8841 (org-get-tags-at)))))
8842 props m pl undone-face done-face finish new dotime level cat tags)
8843 (save-excursion
8844 (goto-char (point-max))
8845 (beginning-of-line 1)
8846 (while (not finish)
8847 (setq finish (bobp))
8848 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8849 (or (not just-this) (= (org-current-line) line))
8850 (equal m hdmarker))
8851 (setq props (text-properties-at (point))
8852 dotime (org-get-at-bol 'dotime)
8853 cat (org-get-at-bol 'org-category)
8854 level (org-get-at-bol 'level)
8855 tags thetags
8857 (let ((org-prefix-format-compiled
8858 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8859 org-prefix-format-compiled))
8860 (extra (org-get-at-bol 'extra)))
8861 (with-current-buffer (marker-buffer hdmarker)
8862 (save-excursion
8863 (save-restriction
8864 (widen)
8865 (org-agenda-format-item extra newhead level cat tags dotime)))))
8866 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8867 undone-face (org-get-at-bol 'undone-face)
8868 done-face (org-get-at-bol 'done-face))
8869 (beginning-of-line 1)
8870 (cond
8871 ((equal new "")
8872 (and (looking-at ".*\n?") (replace-match "")))
8873 ((looking-at ".*")
8874 (replace-match new t t)
8875 (beginning-of-line 1)
8876 (add-text-properties (point-at-bol) (point-at-eol) props)
8877 (when fixface
8878 (add-text-properties
8879 (point-at-bol) (point-at-eol)
8880 (list 'face
8881 (if org-last-todo-state-is-todo
8882 undone-face done-face))))
8883 (org-agenda-highlight-todo 'line)
8884 (beginning-of-line 1))
8885 (t (error "Line update did not work")))
8886 (save-restriction
8887 (narrow-to-region (point-at-bol) (point-at-eol))
8888 (org-agenda-finalize)))
8889 (beginning-of-line 0)))))
8891 (defun org-agenda-align-tags (&optional line)
8892 "Align all tags in agenda items to `org-agenda-tags-column'."
8893 (let ((inhibit-read-only t) l c)
8894 (save-excursion
8895 (goto-char (if line (point-at-bol) (point-min)))
8896 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8897 (if line (point-at-eol) nil) t)
8898 (add-text-properties
8899 (match-beginning 2) (match-end 2)
8900 (list 'face (delq nil (let ((prop (get-text-property
8901 (match-beginning 2) 'face)))
8902 (or (listp prop) (setq prop (list prop)))
8903 (if (memq 'org-tag prop)
8904 prop
8905 (cons 'org-tag prop))))))
8906 (setq l (- (match-end 2) (match-beginning 2))
8907 c (if (< org-agenda-tags-column 0)
8908 (- (abs org-agenda-tags-column) l)
8909 org-agenda-tags-column))
8910 (delete-region (match-beginning 1) (match-end 1))
8911 (goto-char (match-beginning 1))
8912 (insert (org-add-props
8913 (make-string (max 1 (- c (current-column))) ?\ )
8914 (plist-put (copy-sequence (text-properties-at (point)))
8915 'face nil))))
8916 (goto-char (point-min))
8917 (org-font-lock-add-tag-faces (point-max)))))
8919 (defun org-agenda-priority-up ()
8920 "Increase the priority of line at point, also in Org-mode file."
8921 (interactive)
8922 (org-agenda-priority 'up))
8924 (defun org-agenda-priority-down ()
8925 "Decrease the priority of line at point, also in Org-mode file."
8926 (interactive)
8927 (org-agenda-priority 'down))
8929 (defun org-agenda-priority (&optional force-direction)
8930 "Set the priority of line at point, also in Org-mode file.
8931 This changes the line at point, all other lines in the agenda referring to
8932 the same tree node, and the headline of the tree node in the Org-mode file.
8933 Called with a universal prefix arg, show the priority instead of setting it."
8934 (interactive "P")
8935 (if (equal force-direction '(4))
8936 (org-show-priority)
8937 (unless org-enable-priority-commands
8938 (error "Priority commands are disabled"))
8939 (org-agenda-check-no-diary)
8940 (let* ((col (current-column))
8941 (marker (or (org-get-at-bol 'org-marker)
8942 (org-agenda-error)))
8943 (hdmarker (org-get-at-bol 'org-hd-marker))
8944 (buffer (marker-buffer hdmarker))
8945 (pos (marker-position hdmarker))
8946 (inhibit-read-only t)
8947 newhead)
8948 (org-with-remote-undo buffer
8949 (with-current-buffer buffer
8950 (widen)
8951 (goto-char pos)
8952 (org-show-context 'agenda)
8953 (save-excursion
8954 (and (outline-next-heading)
8955 (org-flag-heading nil))) ; show the next heading
8956 (funcall 'org-priority force-direction)
8957 (end-of-line 1)
8958 (setq newhead (org-get-heading)))
8959 (org-agenda-change-all-lines newhead hdmarker)
8960 (org-move-to-column col)))))
8962 ;; FIXME: should fix the tags property of the agenda line.
8963 (defun org-agenda-set-tags (&optional tag onoff)
8964 "Set tags for the current headline."
8965 (interactive)
8966 (org-agenda-check-no-diary)
8967 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8968 (call-interactively 'org-change-tag-in-region)
8969 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8970 (org-agenda-error)))
8971 (buffer (marker-buffer hdmarker))
8972 (pos (marker-position hdmarker))
8973 (inhibit-read-only t)
8974 newhead)
8975 (org-with-remote-undo buffer
8976 (with-current-buffer buffer
8977 (widen)
8978 (goto-char pos)
8979 (save-excursion
8980 (org-show-context 'agenda))
8981 (save-excursion
8982 (and (outline-next-heading)
8983 (org-flag-heading nil))) ; show the next heading
8984 (goto-char pos)
8985 (if tag
8986 (org-toggle-tag tag onoff)
8987 (call-interactively 'org-set-tags))
8988 (end-of-line 1)
8989 (setq newhead (org-get-heading)))
8990 (org-agenda-change-all-lines newhead hdmarker)
8991 (beginning-of-line 1)))))
8993 (defun org-agenda-set-property ()
8994 "Set a property for the current headline."
8995 (interactive)
8996 (org-agenda-check-no-diary)
8997 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8998 (org-agenda-error)))
8999 (buffer (marker-buffer hdmarker))
9000 (pos (marker-position hdmarker))
9001 (inhibit-read-only t)
9002 newhead)
9003 (org-with-remote-undo buffer
9004 (with-current-buffer buffer
9005 (widen)
9006 (goto-char pos)
9007 (save-excursion
9008 (org-show-context 'agenda))
9009 (save-excursion
9010 (and (outline-next-heading)
9011 (org-flag-heading nil))) ; show the next heading
9012 (goto-char pos)
9013 (call-interactively 'org-set-property)))))
9015 (defun org-agenda-set-effort ()
9016 "Set the effort property for the current headline."
9017 (interactive)
9018 (org-agenda-check-no-diary)
9019 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9020 (org-agenda-error)))
9021 (buffer (marker-buffer hdmarker))
9022 (pos (marker-position hdmarker))
9023 (inhibit-read-only t)
9024 newhead)
9025 (org-with-remote-undo buffer
9026 (with-current-buffer buffer
9027 (widen)
9028 (goto-char pos)
9029 (save-excursion
9030 (org-show-context 'agenda))
9031 (save-excursion
9032 (and (outline-next-heading)
9033 (org-flag-heading nil))) ; show the next heading
9034 (goto-char pos)
9035 (call-interactively 'org-set-effort)
9036 (end-of-line 1)
9037 (setq newhead (org-get-heading)))
9038 (org-agenda-change-all-lines newhead hdmarker))))
9040 (defun org-agenda-toggle-archive-tag ()
9041 "Toggle the archive tag for the current entry."
9042 (interactive)
9043 (org-agenda-check-no-diary)
9044 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9045 (org-agenda-error)))
9046 (buffer (marker-buffer hdmarker))
9047 (pos (marker-position hdmarker))
9048 (inhibit-read-only t)
9049 newhead)
9050 (org-with-remote-undo buffer
9051 (with-current-buffer buffer
9052 (widen)
9053 (goto-char pos)
9054 (org-show-context 'agenda)
9055 (save-excursion
9056 (and (outline-next-heading)
9057 (org-flag-heading nil))) ; show the next heading
9058 (call-interactively 'org-toggle-archive-tag)
9059 (end-of-line 1)
9060 (setq newhead (org-get-heading)))
9061 (org-agenda-change-all-lines newhead hdmarker)
9062 (beginning-of-line 1))))
9064 (defun org-agenda-do-date-later (arg)
9065 (interactive "P")
9066 (cond
9067 ((or (equal arg '(16))
9068 (memq last-command
9069 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9070 (setq this-command 'org-agenda-date-later-minutes)
9071 (org-agenda-date-later-minutes 1))
9072 ((or (equal arg '(4))
9073 (memq last-command
9074 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9075 (setq this-command 'org-agenda-date-later-hours)
9076 (org-agenda-date-later-hours 1))
9078 (org-agenda-date-later (prefix-numeric-value arg)))))
9080 (defun org-agenda-do-date-earlier (arg)
9081 (interactive "P")
9082 (cond
9083 ((or (equal arg '(16))
9084 (memq last-command
9085 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9086 (setq this-command 'org-agenda-date-earlier-minutes)
9087 (org-agenda-date-earlier-minutes 1))
9088 ((or (equal arg '(4))
9089 (memq last-command
9090 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9091 (setq this-command 'org-agenda-date-earlier-hours)
9092 (org-agenda-date-earlier-hours 1))
9094 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9096 (defun org-agenda-date-later (arg &optional what)
9097 "Change the date of this item to ARG day(s) later."
9098 (interactive "p")
9099 (org-agenda-check-type t 'agenda 'timeline)
9100 (org-agenda-check-no-diary)
9101 (let* ((marker (or (org-get-at-bol 'org-marker)
9102 (org-agenda-error)))
9103 (buffer (marker-buffer marker))
9104 (pos (marker-position marker))
9105 cdate today)
9106 (org-with-remote-undo buffer
9107 (with-current-buffer buffer
9108 (widen)
9109 (goto-char pos)
9110 (if (not (org-at-timestamp-p))
9111 (error "Cannot find time stamp"))
9112 (when (and org-agenda-move-date-from-past-immediately-to-today
9113 (equal arg 1)
9114 (or (not what) (eq what 'day))
9115 (not (save-match-data (org-at-date-range-p))))
9116 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9117 cdate (calendar-absolute-from-gregorian
9118 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9119 today (org-today))
9120 (if (> today cdate)
9121 ;; immediately shift to today
9122 (setq arg (- today cdate))))
9123 (org-timestamp-change arg (or what 'day))
9124 (when (and (org-at-date-range-p)
9125 (re-search-backward org-tr-regexp-both (point-at-bol)))
9126 (let ((end org-last-changed-timestamp))
9127 (org-timestamp-change arg (or what 'day))
9128 (setq org-last-changed-timestamp
9129 (concat org-last-changed-timestamp "--" end)))))
9130 (org-agenda-show-new-time marker org-last-changed-timestamp))
9131 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9133 (defun org-agenda-date-earlier (arg &optional what)
9134 "Change the date of this item to ARG day(s) earlier."
9135 (interactive "p")
9136 (org-agenda-date-later (- arg) what))
9138 (defun org-agenda-date-later-minutes (arg)
9139 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9140 (interactive "p")
9141 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9142 (org-agenda-date-later arg 'minute))
9144 (defun org-agenda-date-earlier-minutes (arg)
9145 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9146 (interactive "p")
9147 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9148 (org-agenda-date-earlier arg 'minute))
9150 (defun org-agenda-date-later-hours (arg)
9151 "Change the time of this item, in hour steps."
9152 (interactive "p")
9153 (org-agenda-date-later arg 'hour))
9155 (defun org-agenda-date-earlier-hours (arg)
9156 "Change the time of this item, in hour steps."
9157 (interactive "p")
9158 (org-agenda-date-earlier arg 'hour))
9160 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9161 "Show new date stamp via text properties."
9162 ;; We use text properties to make this undoable
9163 (let ((inhibit-read-only t))
9164 (setq stamp (concat prefix " => " stamp " "))
9165 (save-excursion
9166 (goto-char (point-max))
9167 (while (not (bobp))
9168 (when (equal marker (org-get-at-bol 'org-marker))
9169 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9170 (org-move-to-column (- (window-width) (length stamp)) t)
9172 (org-agenda-fix-tags-filter-overlays-at (point))
9173 (if (featurep 'xemacs)
9174 ;; Use `duplicable' property to trigger undo recording
9175 (let ((ex (make-extent nil nil))
9176 (gl (make-glyph stamp)))
9177 (set-glyph-face gl 'secondary-selection)
9178 (set-extent-properties
9179 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9180 (insert-extent ex (1- (point)) (point-at-eol)))
9181 (add-text-properties
9182 (1- (point)) (point-at-eol)
9183 (list 'display (org-add-props stamp nil
9184 'face 'secondary-selection))))
9185 (beginning-of-line 1))
9186 (beginning-of-line 0)))))
9188 (defun org-agenda-date-prompt (arg)
9189 "Change the date of this item. Date is prompted for, with default today.
9190 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9191 be used to request time specification in the time stamp."
9192 (interactive "P")
9193 (org-agenda-check-type t 'agenda 'timeline)
9194 (org-agenda-check-no-diary)
9195 (let* ((marker (or (org-get-at-bol 'org-marker)
9196 (org-agenda-error)))
9197 (buffer (marker-buffer marker))
9198 (pos (marker-position marker)))
9199 (org-with-remote-undo buffer
9200 (with-current-buffer buffer
9201 (widen)
9202 (goto-char pos)
9203 (if (not (org-at-timestamp-p t))
9204 (error "Cannot find time stamp"))
9205 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9206 (org-agenda-show-new-time marker org-last-changed-timestamp))
9207 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9209 (defun org-agenda-schedule (arg &optional time)
9210 "Schedule the item at point.
9211 ARG is passed through to `org-schedule'."
9212 (interactive "P")
9213 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9214 (org-agenda-check-no-diary)
9215 (let* ((marker (or (org-get-at-bol 'org-marker)
9216 (org-agenda-error)))
9217 (type (marker-insertion-type marker))
9218 (buffer (marker-buffer marker))
9219 (pos (marker-position marker))
9220 (org-insert-labeled-timestamps-at-point nil)
9222 (set-marker-insertion-type marker t)
9223 (org-with-remote-undo buffer
9224 (with-current-buffer buffer
9225 (widen)
9226 (goto-char pos)
9227 (setq ts (org-schedule arg time)))
9228 (org-agenda-show-new-time marker ts " S"))
9229 (message "%s" ts)))
9231 (defun org-agenda-deadline (arg &optional time)
9232 "Schedule the item at point.
9233 ARG is passed through to `org-deadline'."
9234 (interactive "P")
9235 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9236 (org-agenda-check-no-diary)
9237 (let* ((marker (or (org-get-at-bol 'org-marker)
9238 (org-agenda-error)))
9239 (buffer (marker-buffer marker))
9240 (pos (marker-position marker))
9241 (org-insert-labeled-timestamps-at-point nil)
9243 (org-with-remote-undo buffer
9244 (with-current-buffer buffer
9245 (widen)
9246 (goto-char pos)
9247 (setq ts (org-deadline arg time)))
9248 (org-agenda-show-new-time marker ts " D"))
9249 (message "%s" ts)))
9251 (defun org-agenda-clock-in (&optional arg)
9252 "Start the clock on the currently selected item."
9253 (interactive "P")
9254 (org-agenda-check-no-diary)
9255 (if (equal arg '(4))
9256 (org-clock-in arg)
9257 (let* ((marker (or (org-get-at-bol 'org-marker)
9258 (org-agenda-error)))
9259 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9260 (pos (marker-position marker))
9261 (col (current-column))
9262 newhead)
9263 (org-with-remote-undo (marker-buffer marker)
9264 (with-current-buffer (marker-buffer marker)
9265 (widen)
9266 (goto-char pos)
9267 (org-show-context 'agenda)
9268 (org-show-entry)
9269 (org-cycle-hide-drawers 'children)
9270 (org-clock-in arg)
9271 (setq newhead (org-get-heading)))
9272 (org-agenda-change-all-lines newhead hdmarker))
9273 (org-move-to-column col))))
9275 (defun org-agenda-clock-out ()
9276 "Stop the currently running clock."
9277 (interactive)
9278 (unless (marker-buffer org-clock-marker)
9279 (error "No running clock"))
9280 (let ((marker (make-marker)) (col (current-column)) newhead)
9281 (org-with-remote-undo (marker-buffer org-clock-marker)
9282 (with-current-buffer (marker-buffer org-clock-marker)
9283 (save-excursion
9284 (save-restriction
9285 (widen)
9286 (goto-char org-clock-marker)
9287 (org-back-to-heading t)
9288 (move-marker marker (point))
9289 (org-clock-out)
9290 (setq newhead (org-get-heading))))))
9291 (org-agenda-change-all-lines newhead marker)
9292 (move-marker marker nil)
9293 (org-move-to-column col)
9294 (org-agenda-unmark-clocking-task)))
9296 (defun org-agenda-clock-cancel (&optional arg)
9297 "Cancel the currently running clock."
9298 (interactive "P")
9299 (unless (marker-buffer org-clock-marker)
9300 (user-error "No running clock"))
9301 (org-with-remote-undo (marker-buffer org-clock-marker)
9302 (org-clock-cancel)))
9304 (defun org-agenda-clock-goto ()
9305 "Jump to the currently clocked in task within the agenda.
9306 If the currently clocked in task is not listed in the agenda
9307 buffer, display it in another window."
9308 (interactive)
9309 (let (pos)
9310 (mapc (lambda (o)
9311 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9312 (setq pos (overlay-start o))))
9313 (overlays-in (point-min) (point-max)))
9314 (cond (pos (goto-char pos))
9315 ;; If the currently clocked entry is not in the agenda
9316 ;; buffer, we visit it in another window:
9317 (org-clock-current-task
9318 (org-switch-to-buffer-other-window (org-clock-goto)))
9319 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9321 (defun org-agenda-diary-entry-in-org-file ()
9322 "Make a diary entry in the file `org-agenda-diary-file'."
9323 (let (d1 d2 char (text "") dp1 dp2)
9324 (if (equal (buffer-name) "*Calendar*")
9325 (setq d1 (calendar-cursor-to-date t)
9326 d2 (car calendar-mark-ring))
9327 (setq dp1 (get-text-property (point-at-bol) 'day))
9328 (unless dp1 (user-error "No date defined in current line"))
9329 (setq d1 (calendar-gregorian-from-absolute dp1)
9330 d2 (and (ignore-errors (mark))
9331 (save-excursion
9332 (goto-char (mark))
9333 (setq dp2 (get-text-property (point-at-bol) 'day)))
9334 (calendar-gregorian-from-absolute dp2))))
9335 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9336 (setq char (read-char-exclusive))
9337 (cond
9338 ((equal char ?d)
9339 (setq text (read-string "Day entry: "))
9340 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9341 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9342 ((equal char ?a)
9343 (setq d1 (list (car d1) (nth 1 d1)
9344 (read-number (format "Reference year [%d]: " (nth 2 d1))
9345 (nth 2 d1))))
9346 (setq text (read-string "Anniversary (use %d to show years): "))
9347 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9348 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9349 ((equal char ?b)
9350 (setq text (read-string "Block entry: "))
9351 (unless (and d1 d2 (not (equal d1 d2)))
9352 (user-error "No block of days selected"))
9353 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9354 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9355 ((equal char ?j)
9356 (org-switch-to-buffer-other-window
9357 (find-file-noselect org-agenda-diary-file))
9358 (require 'org-datetree)
9359 (org-datetree-find-date-create d1)
9360 (org-reveal t))
9361 (t (user-error "Invalid selection character `%c'" char)))))
9363 (defcustom org-agenda-insert-diary-strategy 'date-tree
9364 "Where in `org-agenda-diary-file' should new entries be added?
9365 Valid values:
9367 date-tree in the date tree, as child of the date
9368 top-level as top-level entries at the end of the file."
9369 :group 'org-agenda
9370 :type '(choice
9371 (const :tag "in a date tree" date-tree)
9372 (const :tag "as top level at end of file" top-level)))
9374 (defcustom org-agenda-insert-diary-extract-time nil
9375 "Non-nil means extract any time specification from the diary entry."
9376 :group 'org-agenda
9377 :version "24.1"
9378 :type 'boolean)
9380 (defcustom org-agenda-bulk-mark-char ">"
9381 "A single-character string to be used as the bulk mark."
9382 :group 'org-agenda
9383 :version "24.1"
9384 :type 'string)
9386 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9387 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9388 If TEXT is not empty, it will become the headline of the new entry, and
9389 the resulting entry will not be shown. When TEXT is empty, switch to
9390 `org-agenda-diary-file' and let the user finish the entry there."
9391 (let ((cw (current-window-configuration)))
9392 (org-switch-to-buffer-other-window
9393 (find-file-noselect org-agenda-diary-file))
9394 (widen)
9395 (goto-char (point-min))
9396 (cond
9397 ((eq type 'anniversary)
9398 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9399 (progn
9400 (or (org-at-heading-p t)
9401 (progn
9402 (outline-next-heading)
9403 (insert "* Anniversaries\n\n")
9404 (beginning-of-line -1)))))
9405 (outline-next-heading)
9406 (org-back-over-empty-lines)
9407 (backward-char 1)
9408 (insert "\n")
9409 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9410 (nth 2 d1) (car d1) (nth 1 d1) text)))
9411 ((eq type 'day)
9412 (let ((org-prefix-has-time t)
9413 (org-agenda-time-leading-zero t)
9414 fmt time time2)
9415 (if org-agenda-insert-diary-extract-time
9416 ;; Use org-agenda-format-item to parse text for a time-range and
9417 ;; remove it. FIXME: This is a hack, we should refactor
9418 ;; that function to make time extraction available separately
9419 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9420 time (get-text-property 0 'time fmt)
9421 time2 (if (> (length time) 0)
9422 ;; split-string removes trailing ...... if
9423 ;; no end time given. First space
9424 ;; separates time from date.
9425 (concat " " (car (split-string time "\\.")))
9426 nil)
9427 text (get-text-property 0 'txt fmt)))
9428 (if (eq org-agenda-insert-diary-strategy 'top-level)
9429 (org-agenda-insert-diary-as-top-level text)
9430 (require 'org-datetree)
9431 (org-datetree-find-date-create d1)
9432 (org-agenda-insert-diary-make-new-entry text))
9433 (org-insert-time-stamp (org-time-from-absolute
9434 (calendar-absolute-from-gregorian d1))
9435 nil nil nil nil time2))
9436 (end-of-line 0))
9437 ((eq type 'block)
9438 (if (> (calendar-absolute-from-gregorian d1)
9439 (calendar-absolute-from-gregorian d2))
9440 (setq d1 (prog1 d2 (setq d2 d1))))
9441 (if (eq org-agenda-insert-diary-strategy 'top-level)
9442 (org-agenda-insert-diary-as-top-level text)
9443 (require 'org-datetree)
9444 (org-datetree-find-date-create d1)
9445 (org-agenda-insert-diary-make-new-entry text))
9446 (org-insert-time-stamp (org-time-from-absolute
9447 (calendar-absolute-from-gregorian d1)))
9448 (insert "--")
9449 (org-insert-time-stamp (org-time-from-absolute
9450 (calendar-absolute-from-gregorian d2)))
9451 (end-of-line 0)))
9452 (if (string-match "\\S-" text)
9453 (progn
9454 (set-window-configuration cw)
9455 (message "%s entry added to %s"
9456 (capitalize (symbol-name type))
9457 (abbreviate-file-name org-agenda-diary-file)))
9458 (org-reveal t)
9459 (message "Please finish entry here"))))
9461 (defun org-agenda-insert-diary-as-top-level (text)
9462 "Make new entry as a top-level entry at the end of the file.
9463 Add TEXT as headline, and position the cursor in the second line so that
9464 a timestamp can be added there."
9465 (widen)
9466 (goto-char (point-max))
9467 (or (bolp) (insert "\n"))
9468 (insert "* " text "\n")
9469 (if org-adapt-indentation (org-indent-to-column 2)))
9471 (defun org-agenda-insert-diary-make-new-entry (text)
9472 "Make new entry as last child of current entry.
9473 Add TEXT as headline, and position the cursor in the second line so that
9474 a timestamp can be added there."
9475 (let ((org-show-following-heading t)
9476 (org-show-siblings t)
9477 (org-show-hierarchy-above t)
9478 (org-show-entry-below t)
9479 col)
9480 (outline-next-heading)
9481 (org-back-over-empty-lines)
9482 (or (looking-at "[ \t]*$")
9483 (progn (insert "\n") (backward-char 1)))
9484 (org-insert-heading nil t)
9485 (org-do-demote)
9486 (setq col (current-column))
9487 (insert text "\n")
9488 (if org-adapt-indentation (org-indent-to-column col))
9489 (let ((org-show-following-heading t)
9490 (org-show-siblings t)
9491 (org-show-hierarchy-above t)
9492 (org-show-entry-below t))
9493 (org-show-context))))
9495 (defun org-agenda-diary-entry ()
9496 "Make a diary entry, like the `i' command from the calendar.
9497 All the standard commands work: block, weekly etc.
9498 When `org-agenda-diary-file' points to a file,
9499 `org-agenda-diary-entry-in-org-file' is called instead to create
9500 entries in that Org-mode file."
9501 (interactive)
9502 (if (not (eq org-agenda-diary-file 'diary-file))
9503 (org-agenda-diary-entry-in-org-file)
9504 (require 'diary-lib)
9505 (let* ((char (progn
9506 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9507 (read-char-exclusive)))
9508 (cmd (cdr (assoc char
9509 '((?d . insert-diary-entry)
9510 (?w . insert-weekly-diary-entry)
9511 (?m . insert-monthly-diary-entry)
9512 (?y . insert-yearly-diary-entry)
9513 (?a . insert-anniversary-diary-entry)
9514 (?b . insert-block-diary-entry)
9515 (?c . insert-cyclic-diary-entry)))))
9516 (oldf (symbol-function 'calendar-cursor-to-date))
9517 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9518 (point (point))
9519 (mark (or (mark t) (point))))
9520 (unless cmd
9521 (user-error "No command associated with <%c>" char))
9522 (unless (and (get-text-property point 'day)
9523 (or (not (equal ?b char))
9524 (get-text-property mark 'day)))
9525 (user-error "Don't know which date to use for diary entry"))
9526 ;; We implement this by hacking the `calendar-cursor-to-date' function
9527 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9528 (let ((calendar-mark-ring
9529 (list (calendar-gregorian-from-absolute
9530 (or (get-text-property mark 'day)
9531 (get-text-property point 'day))))))
9532 (unwind-protect
9533 (progn
9534 (fset 'calendar-cursor-to-date
9535 (lambda (&optional error dummy)
9536 (calendar-gregorian-from-absolute
9537 (get-text-property point 'day))))
9538 (call-interactively cmd))
9539 (fset 'calendar-cursor-to-date oldf))))))
9541 (defun org-agenda-execute-calendar-command (cmd)
9542 "Execute a calendar command from the agenda with date from cursor."
9543 (org-agenda-check-type t 'agenda 'timeline)
9544 (require 'diary-lib)
9545 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9546 (user-error "Don't know which date to use for the calendar command"))
9547 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9548 (point (point))
9549 (date (calendar-gregorian-from-absolute
9550 (get-text-property point 'day)))
9551 ;; the following 2 vars are needed in the calendar
9552 (displayed-month (car date))
9553 (displayed-year (nth 2 date)))
9554 (unwind-protect
9555 (progn
9556 (fset 'calendar-cursor-to-date
9557 (lambda (&optional error dummy)
9558 (calendar-gregorian-from-absolute
9559 (get-text-property point 'day))))
9560 (call-interactively cmd))
9561 (fset 'calendar-cursor-to-date oldf))))
9563 (defun org-agenda-phases-of-moon ()
9564 "Display the phases of the moon for the 3 months around the cursor date."
9565 (interactive)
9566 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9568 (defun org-agenda-holidays ()
9569 "Display the holidays for the 3 months around the cursor date."
9570 (interactive)
9571 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9573 (defvar calendar-longitude) ; defined in calendar.el
9574 (defvar calendar-latitude) ; defined in calendar.el
9575 (defvar calendar-location-name) ; defined in calendar.el
9577 (defun org-agenda-sunrise-sunset (arg)
9578 "Display sunrise and sunset for the cursor date.
9579 Latitude and longitude can be specified with the variables
9580 `calendar-latitude' and `calendar-longitude'. When called with prefix
9581 argument, latitude and longitude will be prompted for."
9582 (interactive "P")
9583 (require 'solar)
9584 (let ((calendar-longitude (if arg nil calendar-longitude))
9585 (calendar-latitude (if arg nil calendar-latitude))
9586 (calendar-location-name
9587 (if arg "the given coordinates" calendar-location-name)))
9588 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9590 (defun org-agenda-goto-calendar ()
9591 "Open the Emacs calendar with the date at the cursor."
9592 (interactive)
9593 (org-agenda-check-type t 'agenda 'timeline)
9594 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9595 (user-error "Don't know which date to open in calendar")))
9596 (date (calendar-gregorian-from-absolute day))
9597 (calendar-move-hook nil)
9598 (calendar-view-holidays-initially-flag nil)
9599 (calendar-view-diary-initially-flag nil))
9600 (calendar)
9601 (calendar-goto-date date)))
9603 ;;;###autoload
9604 (defun org-calendar-goto-agenda ()
9605 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9606 This is a command that has to be installed in `calendar-mode-map'."
9607 (interactive)
9608 (org-agenda-list nil (calendar-absolute-from-gregorian
9609 (calendar-cursor-to-date))
9610 nil))
9612 (defun org-agenda-convert-date ()
9613 (interactive)
9614 (org-agenda-check-type t 'agenda 'timeline)
9615 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9616 date s)
9617 (unless day
9618 (user-error "Don't know which date to convert"))
9619 (setq date (calendar-gregorian-from-absolute day))
9620 (setq s (concat
9621 "Gregorian: " (calendar-date-string date) "\n"
9622 "ISO: " (calendar-iso-date-string date) "\n"
9623 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9624 "Julian: " (calendar-julian-date-string date) "\n"
9625 "Astron. JD: " (calendar-astro-date-string date)
9626 " (Julian date number at noon UTC)\n"
9627 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9628 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9629 "French: " (calendar-french-date-string date) "\n"
9630 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9631 "Mayan: " (calendar-mayan-date-string date) "\n"
9632 "Coptic: " (calendar-coptic-date-string date) "\n"
9633 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9634 "Persian: " (calendar-persian-date-string date) "\n"
9635 "Chinese: " (calendar-chinese-date-string date) "\n"))
9636 (with-output-to-temp-buffer "*Dates*"
9637 (princ s))
9638 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9640 ;;; Bulk commands
9642 (defun org-agenda-bulk-marked-p ()
9643 (eq (get-char-property (point-at-bol) 'type)
9644 'org-marked-entry-overlay))
9646 (defun org-agenda-bulk-mark (&optional arg)
9647 "Mark the entry at point for future bulk action."
9648 (interactive "p")
9649 (dotimes (i (or arg 1))
9650 (unless (org-get-at-bol 'org-agenda-diary-link)
9651 (let* ((m (org-get-at-bol 'org-hd-marker))
9653 (unless (org-agenda-bulk-marked-p)
9654 (unless m (user-error "Nothing to mark at point"))
9655 (push m org-agenda-bulk-marked-entries)
9656 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9657 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9658 (org-get-todo-face "TODO")
9659 'evaporate)
9660 (overlay-put ov 'type 'org-marked-entry-overlay))
9661 (end-of-line 1)
9662 (or (ignore-errors
9663 (goto-char (next-single-property-change (point) 'txt)))
9664 (beginning-of-line 2))
9665 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9666 (beginning-of-line 2))
9667 (message "%d entries marked for bulk action"
9668 (length org-agenda-bulk-marked-entries))))))
9670 (defun org-agenda-bulk-mark-all ()
9671 "Mark all entries for future agenda bulk action."
9672 (interactive)
9673 (org-agenda-bulk-mark-regexp "."))
9675 (defun org-agenda-bulk-mark-regexp (regexp)
9676 "Mark entries matching REGEXP for future agenda bulk action."
9677 (interactive "sMark entries matching regexp: ")
9678 (let ((entries-marked 0) txt-at-point)
9679 (save-excursion
9680 (goto-char (point-min))
9681 (goto-char (next-single-property-change (point) 'txt))
9682 (while (and (re-search-forward regexp nil t)
9683 (setq txt-at-point (get-text-property (point) 'txt)))
9684 (when (string-match regexp txt-at-point)
9685 (setq entries-marked (1+ entries-marked))
9686 (call-interactively 'org-agenda-bulk-mark))))
9687 (if (not entries-marked)
9688 (message "No entry matching this regexp."))))
9690 (defun org-agenda-bulk-unmark (&optional arg)
9691 "Unmark the entry at point for future bulk action."
9692 (interactive "P")
9693 (if arg
9694 (org-agenda-bulk-unmark-all)
9695 (cond ((org-agenda-bulk-marked-p)
9696 (org-agenda-bulk-remove-overlays
9697 (point-at-bol) (+ 2 (point-at-bol)))
9698 (setq org-agenda-bulk-marked-entries
9699 (delete (org-get-at-bol 'org-hd-marker)
9700 org-agenda-bulk-marked-entries))
9701 (end-of-line 1)
9702 (or (ignore-errors
9703 (goto-char (next-single-property-change (point) 'txt)))
9704 (beginning-of-line 2))
9705 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9706 (beginning-of-line 2))
9707 (message "%d entries left marked for bulk action"
9708 (length org-agenda-bulk-marked-entries)))
9709 (t (message "No entry to unmark here")))))
9711 (defun org-agenda-bulk-toggle-all ()
9712 "Toggle all marks for bulk action."
9713 (interactive)
9714 (save-excursion
9715 (goto-char (point-min))
9716 (while (ignore-errors
9717 (goto-char (next-single-property-change (point) 'txt)))
9718 (org-agenda-bulk-toggle))))
9720 (defun org-agenda-bulk-toggle ()
9721 "Toggle the mark at point for bulk action."
9722 (interactive)
9723 (if (org-agenda-bulk-marked-p)
9724 (org-agenda-bulk-unmark)
9725 (org-agenda-bulk-mark)))
9727 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9728 "Remove the mark overlays between BEG and END in the agenda buffer.
9729 BEG and END default to the buffer limits.
9731 This only removes the overlays, it does not remove the markers
9732 from the list in `org-agenda-bulk-marked-entries'."
9733 (interactive)
9734 (mapc (lambda (ov)
9735 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9736 (delete-overlay ov)))
9737 (overlays-in (or beg (point-min)) (or end (point-max)))))
9739 (defun org-agenda-bulk-unmark-all ()
9740 "Remove all marks in the agenda buffer.
9741 This will remove the markers and the overlays."
9742 (interactive)
9743 (if (null org-agenda-bulk-marked-entries)
9744 (message "No entry to unmark")
9745 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9746 (setq org-agenda-bulk-marked-entries nil)
9747 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9749 (defcustom org-agenda-persistent-marks nil
9750 "Non-nil means marked items will stay marked after a bulk action.
9751 You can toggle this interactively by typing `p' when prompted for a
9752 bulk action."
9753 :group 'org-agenda
9754 :version "24.1"
9755 :type 'boolean)
9757 (defun org-agenda-bulk-action (&optional arg)
9758 "Execute an remote-editing action on all marked entries.
9759 The prefix arg is passed through to the command if possible."
9760 (interactive "P")
9761 ;; Make sure we have markers, and only valid ones
9762 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9763 (mapc
9764 (lambda (m)
9765 (unless (and (markerp m)
9766 (marker-buffer m)
9767 (buffer-live-p (marker-buffer m))
9768 (marker-position m))
9769 (user-error "Marker %s for bulk command is invalid" m)))
9770 org-agenda-bulk-marked-entries)
9772 ;; Prompt for the bulk command
9773 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9774 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9775 "[S]catter [f]unction "
9776 (when org-agenda-bulk-custom-functions
9777 (concat " Custom: ["
9778 (mapconcat (lambda(f) (char-to-string (car f)))
9779 org-agenda-bulk-custom-functions "")
9780 "]"))))
9781 (catch 'exit
9782 (let* ((action (read-char-exclusive))
9783 (org-log-refile (if org-log-refile 'time nil))
9784 (entries (reverse org-agenda-bulk-marked-entries))
9785 (org-overriding-default-time
9786 (if (get-text-property (point) 'org-agenda-date-header)
9787 (org-get-cursor-date)))
9788 redo-at-end
9789 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9790 (cond
9791 ((equal action ?p)
9792 (let ((org-agenda-persistent-marks
9793 (not org-agenda-persistent-marks)))
9794 (org-agenda-bulk-action)
9795 (throw 'exit nil)))
9797 ((equal action ?$)
9798 (setq cmd '(org-agenda-archive)))
9800 ((equal action ?A)
9801 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9803 ((member action '(?r ?w))
9804 (setq rfloc (org-refile-get-location
9805 "Refile to"
9806 (marker-buffer (car entries))
9807 org-refile-allow-creating-parent-nodes))
9808 (if (nth 3 rfloc)
9809 (setcar (nthcdr 3 rfloc)
9810 (move-marker (make-marker) (nth 3 rfloc)
9811 (or (get-file-buffer (nth 1 rfloc))
9812 (find-buffer-visiting (nth 1 rfloc))
9813 (error "This should not happen")))))
9815 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9816 redo-at-end t))
9818 ((equal action ?t)
9819 (setq state (org-icompleting-read
9820 "Todo state: "
9821 (with-current-buffer (marker-buffer (car entries))
9822 (mapcar 'list org-todo-keywords-1))))
9823 (setq cmd `(let ((org-inhibit-blocking t)
9824 (org-inhibit-logging 'note))
9825 (org-agenda-todo ,state))))
9827 ((memq action '(?- ?+))
9828 (setq tag (org-icompleting-read
9829 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9830 (with-current-buffer (marker-buffer (car entries))
9831 (delq nil
9832 (mapcar (lambda (x)
9833 (if (stringp (car x)) x)) org-tag-alist)))))
9834 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9836 ((memq action '(?s ?d))
9837 (let* ((time
9838 (unless arg
9839 (org-read-date
9840 nil nil nil
9841 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9842 org-overriding-default-time)))
9843 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9844 (setq cmd `(eval '(,c1 arg ,time)))))
9846 ((equal action ?S)
9847 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9848 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9849 (let ((days (read-number
9850 (format "Scatter tasks across how many %sdays: "
9851 (if arg "week" "")) 7)))
9852 (setq cmd
9853 `(let ((distance (1+ (random ,days))))
9854 (if arg
9855 (let ((dist distance)
9856 (day-of-week
9857 (calendar-day-of-week
9858 (calendar-gregorian-from-absolute (org-today)))))
9859 (dotimes (i (1+ dist))
9860 (while (member day-of-week org-agenda-weekend-days)
9861 (incf distance)
9862 (incf day-of-week)
9863 (if (= day-of-week 7)
9864 (setq day-of-week 0)))
9865 (incf day-of-week)
9866 (if (= day-of-week 7)
9867 (setq day-of-week 0)))))
9868 ;; silently fail when try to replan a sexp entry
9869 (condition-case nil
9870 (let* ((date (calendar-gregorian-from-absolute
9871 (+ (org-today) distance)))
9872 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9873 (nth 2 date))))
9874 (org-agenda-schedule nil time))
9875 (error nil)))))))
9877 ((assoc action org-agenda-bulk-custom-functions)
9878 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9879 redo-at-end t))
9881 ((equal action ?f)
9882 (setq cmd (list (intern
9883 (org-icompleting-read "Function: "
9884 obarray 'fboundp t nil nil)))))
9886 (t (user-error "Invalid bulk action")))
9888 ;; Sort the markers, to make sure that parents are handled before children
9889 (setq entries (sort entries
9890 (lambda (a b)
9891 (cond
9892 ((equal (marker-buffer a) (marker-buffer b))
9893 (< (marker-position a) (marker-position b)))
9895 (string< (buffer-name (marker-buffer a))
9896 (buffer-name (marker-buffer b))))))))
9898 ;; Now loop over all markers and apply cmd
9899 (while (setq e (pop entries))
9900 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9901 (if (not pos)
9902 (progn (message "Skipping removed entry at %s" e)
9903 (setq cntskip (1+ cntskip)))
9904 (goto-char pos)
9905 (let (org-loop-over-headlines-in-active-region)
9906 (eval cmd))
9907 (setq cnt (1+ cnt))))
9908 (when redo-at-end (org-agenda-redo))
9909 (unless org-agenda-persistent-marks
9910 (org-agenda-bulk-unmark-all))
9911 (message "Acted on %d entries%s%s"
9913 (if (= cntskip 0)
9915 (format ", skipped %d (disappeared before their turn)"
9916 cntskip))
9917 (if (not org-agenda-persistent-marks)
9918 "" " (kept marked)"))))))
9920 (defun org-agenda-capture (&optional with-time)
9921 "Call `org-capture' with the date at point.
9922 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9923 current HH:MM time."
9924 (interactive "P")
9925 (if (not (eq major-mode 'org-agenda-mode))
9926 (user-error "You cannot do this outside of agenda buffers")
9927 (let ((org-overriding-default-time
9928 (org-get-cursor-date (equal with-time 1))))
9929 (call-interactively 'org-capture))))
9931 ;;; Dragging agenda lines forward/backward
9933 (defun org-agenda-reapply-filters ()
9934 "Re-apply all agenda filters."
9935 (mapcar
9936 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9937 `((,org-agenda-tag-filter tag)
9938 (,org-agenda-category-filter category)
9939 (,org-agenda-regexp-filter regexp)
9940 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9941 (,(get 'org-agenda-category-filter :preset-filter) category)
9942 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9944 (defun org-agenda-drag-line-forward (arg &optional backward)
9945 "Drag an agenda line forward by ARG lines.
9946 When the optional argument `backward' is non-nil, move backward."
9947 (interactive "p")
9948 (let ((inhibit-read-only t) lst line)
9949 (if (or (not (get-text-property (point) 'txt))
9950 (save-excursion
9951 (dotimes (n arg)
9952 (move-beginning-of-line (if backward 0 2))
9953 (push (not (get-text-property (point) 'txt)) lst))
9954 (delq nil lst)))
9955 (message "Cannot move line forward")
9956 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9957 (move-beginning-of-line 1)
9958 (setq line (buffer-substring (point) end))
9959 (delete-region (point) end)
9960 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9961 (insert line)
9962 (org-agenda-reapply-filters)
9963 (org-agenda-mark-clocking-task)
9964 (move-beginning-of-line 0)))))
9966 (defun org-agenda-drag-line-backward (arg)
9967 "Drag an agenda line backward by ARG lines."
9968 (interactive "p")
9969 (org-agenda-drag-line-forward arg t))
9971 ;;; Flagging notes
9973 (defun org-agenda-show-the-flagging-note ()
9974 "Display the flagging note in the other window.
9975 When called a second time in direct sequence, offer to remove the FLAGGING
9976 tag and (if present) the flagging note."
9977 (interactive)
9978 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9979 (win (selected-window))
9980 note heading newhead)
9981 (unless hdmarker
9982 (user-error "No linked entry at point"))
9983 (if (and (eq this-command last-command)
9984 (y-or-n-p "Unflag and remove any flagging note? "))
9985 (progn
9986 (org-agenda-remove-flag hdmarker)
9987 (let ((win (get-buffer-window "*Flagging Note*")))
9988 (and win (delete-window win)))
9989 (message "Entry unflagged"))
9990 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9991 (unless note
9992 (user-error "No flagging note"))
9993 (org-kill-new note)
9994 (org-switch-to-buffer-other-window "*Flagging Note*")
9995 (erase-buffer)
9996 (insert note)
9997 (goto-char (point-min))
9998 (while (re-search-forward "\\\\n" nil t)
9999 (replace-match "\n" t t))
10000 (goto-char (point-min))
10001 (select-window win)
10002 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10004 (defun org-agenda-remove-flag (marker)
10005 "Remove the FLAGGED tag and any flagging note in the entry."
10006 (let (newhead)
10007 (org-with-point-at marker
10008 (org-toggle-tag "FLAGGED" 'off)
10009 (org-entry-delete nil "THEFLAGGINGNOTE")
10010 (setq newhead (org-get-heading)))
10011 (org-agenda-change-all-lines newhead marker)
10012 (message "Entry unflagged")))
10014 (defun org-agenda-get-any-marker (&optional pos)
10015 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10016 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10018 ;;; Appointment reminders
10020 (defvar appt-time-msg-list) ; defined in appt.el
10022 ;;;###autoload
10023 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10024 "Activate appointments found in `org-agenda-files'.
10025 With a \\[universal-argument] prefix, refresh the list of
10026 appointments.
10028 If FILTER is t, interactively prompt the user for a regular
10029 expression, and filter out entries that don't match it.
10031 If FILTER is a string, use this string as a regular expression
10032 for filtering entries out.
10034 If FILTER is a function, filter out entries against which
10035 calling the function returns nil. This function takes one
10036 argument: an entry from `org-agenda-get-day-entries'.
10038 FILTER can also be an alist with the car of each cell being
10039 either 'headline or 'category. For example:
10041 '((headline \"IMPORTANT\")
10042 (category \"Work\"))
10044 will only add headlines containing IMPORTANT or headlines
10045 belonging to the \"Work\" category.
10047 ARGS are symbols indicating what kind of entries to consider.
10048 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10049 \(i.e., deadlines and scheduled items with a hh:mm specification)
10050 and :timestamp entries. See the docstring of `org-diary' for
10051 details and examples.
10053 If an entry has a APPT_WARNTIME property, its value will be used
10054 to override `appt-message-warning-time'."
10055 (interactive "P")
10056 (if refresh (setq appt-time-msg-list nil))
10057 (if (eq filter t)
10058 (setq filter (read-from-minibuffer "Regexp filter: ")))
10059 (let* ((cnt 0) ; count added events
10060 (scope (or args '(:deadline* :scheduled* :timestamp)))
10061 (org-agenda-new-buffers nil)
10062 (org-deadline-warning-days 0)
10063 ;; Do not use `org-today' here because appt only takes
10064 ;; time and without date as argument, so it may pass wrong
10065 ;; information otherwise
10066 (today (org-date-to-gregorian
10067 (time-to-days (current-time))))
10068 (org-agenda-restrict nil)
10069 (files (org-agenda-files 'unrestricted)) entries file
10070 (org-agenda-buffer nil))
10071 ;; Get all entries which may contain an appt
10072 (org-agenda-prepare-buffers files)
10073 (while (setq file (pop files))
10074 (setq entries
10075 (delq nil
10076 (append entries
10077 (apply 'org-agenda-get-day-entries
10078 file today scope)))))
10079 ;; Map thru entries and find if we should filter them out
10080 (mapc
10081 (lambda(x)
10082 (let* ((evt (org-trim
10083 (replace-regexp-in-string
10084 org-bracket-link-regexp "\\3"
10085 (or (get-text-property 1 'txt x) ""))))
10086 (cat (get-text-property 1 'org-category x))
10087 (tod (get-text-property 1 'time-of-day x))
10088 (ok (or (null filter)
10089 (and (stringp filter) (string-match filter evt))
10090 (and (functionp filter) (funcall filter x))
10091 (and (listp filter)
10092 (let ((cat-filter (cadr (assoc 'category filter)))
10093 (evt-filter (cadr (assoc 'headline filter))))
10094 (or (and (stringp cat-filter)
10095 (string-match cat-filter cat))
10096 (and (stringp evt-filter)
10097 (string-match evt-filter evt)))))))
10098 (wrn (get-text-property 1 'warntime x)))
10099 ;; FIXME: Shall we remove text-properties for the appt text?
10100 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10101 (when (and ok tod)
10102 (setq tod (concat "00" (number-to-string tod))
10103 tod (when (string-match
10104 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10105 (concat (match-string 1 tod) ":"
10106 (match-string 2 tod))))
10107 (if (version< emacs-version "23.3")
10108 (appt-add tod evt)
10109 (appt-add tod evt wrn))
10110 (setq cnt (1+ cnt))))) entries)
10111 (org-release-buffers org-agenda-new-buffers)
10112 (if (eq cnt 0)
10113 (message "No event to add")
10114 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10116 (defun org-agenda-todayp (date)
10117 "Does DATE mean today, when considering `org-extend-today-until'?"
10118 (let ((today (org-today))
10119 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10120 date)))
10121 (eq date today)))
10123 (defun org-agenda-todo-yesterday (&optional arg)
10124 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10125 (interactive "P")
10126 (let* ((hour (third (decode-time
10127 (org-current-time))))
10128 (org-extend-today-until (1+ hour)))
10129 (org-agenda-todo arg)))
10131 (provide 'org-agenda)
10133 ;;; org-agenda.el ends here