Implement proper handling of agenda markers when using sticky agendas
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob09dc4f5e3e8cc3fa43ed2648fb90ab5c88a35411
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2012 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 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 ;; Keep custom values for `org-agenda-filter-preset' compatible with
249 ;; the new variable `org-agenda-tag-filter-preset'.
250 (if (fboundp 'defvaralias)
251 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
252 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
254 (defconst org-agenda-custom-commands-local-options
255 `(repeat :tag "Local settings for this command. Remember to quote values"
256 (choice :tag "Setting"
257 (list :tag "Heading for this block"
258 (const org-agenda-overriding-header)
259 (string :tag "Headline"))
260 (list :tag "Files to be searched"
261 (const org-agenda-files)
262 (list
263 (const :format "" quote)
264 (repeat (file))))
265 (list :tag "Sorting strategy"
266 (const org-agenda-sorting-strategy)
267 (list
268 (const :format "" quote)
269 (repeat
270 ,org-sorting-choice)))
271 (list :tag "Prefix format"
272 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
273 (string))
274 (list :tag "Number of days in agenda"
275 (const org-agenda-span)
276 (choice (const :tag "Day" 'day)
277 (const :tag "Week" 'week)
278 (const :tag "Month" 'month)
279 (const :tag "Year" 'year)
280 (integer :tag "Custom")))
281 (list :tag "Fixed starting date"
282 (const org-agenda-start-day)
283 (string :value "2007-11-01"))
284 (list :tag "Start on day of week"
285 (const org-agenda-start-on-weekday)
286 (choice :value 1
287 (const :tag "Today" nil)
288 (integer :tag "Weekday No.")))
289 (list :tag "Include data from diary"
290 (const org-agenda-include-diary)
291 (boolean))
292 (list :tag "Deadline Warning days"
293 (const org-deadline-warning-days)
294 (integer :value 1))
295 (list :tag "Category filter preset"
296 (const org-agenda-category-filter-preset)
297 (list
298 (const :format "" quote)
299 (repeat
300 (string :tag "+category or -category"))))
301 (list :tag "Tags filter preset"
302 (const org-agenda-tag-filter-preset)
303 (list
304 (const :format "" quote)
305 (repeat
306 (string :tag "+tag or -tag"))))
307 (list :tag "Set daily/weekly entry types"
308 (const org-agenda-entry-types)
309 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
310 (const :deadline)
311 (const :scheduled)
312 (const :timestamp)
313 (const :sexp)))
314 (list :tag "Standard skipping condition"
315 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
316 (const org-agenda-skip-function)
317 (list
318 (const :format "" quote)
319 (list
320 (choice
321 :tag "Skipping range"
322 (const :tag "Skip entry" org-agenda-skip-entry-if)
323 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
324 (repeat :inline t :tag "Conditions for skipping"
325 (choice
326 :tag "Condition type"
327 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
328 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
329 (list :tag "TODO state is" :inline t
330 (const 'todo)
331 (choice
332 (const :tag "any not-done state" 'todo)
333 (const :tag "any done state" 'done)
334 (const :tag "any state" 'any)
335 (list :tag "Keyword list"
336 (const :format "" quote)
337 (repeat (string :tag "Keyword")))))
338 (list :tag "TODO state is not" :inline t
339 (const 'nottodo)
340 (choice
341 (const :tag "any not-done state" 'todo)
342 (const :tag "any done state" 'done)
343 (const :tag "any state" 'any)
344 (list :tag "Keyword list"
345 (const :format "" quote)
346 (repeat (string :tag "Keyword")))))
347 (const :tag "scheduled" 'scheduled)
348 (const :tag "not scheduled" 'notscheduled)
349 (const :tag "deadline" 'deadline)
350 (const :tag "no deadline" 'notdeadline)
351 (const :tag "timestamp" 'timestamp)
352 (const :tag "no timestamp" 'nottimestamp))))))
353 (list :tag "Non-standard skipping condition"
354 :value (org-agenda-skip-function)
355 (const org-agenda-skip-function)
356 (sexp :tag "Function or form (quoted!)"))
357 (list :tag "Any variable"
358 (variable :tag "Variable")
359 (sexp :tag "Value (sexp)"))))
360 "Selection of examples for agenda command settings.
361 This will be spliced into the custom type of
362 `org-agenda-custom-commands'.")
365 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
366 ((agenda "") (alltodo))))
367 "Custom commands for the agenda.
368 These commands will be offered on the splash screen displayed by the
369 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
371 (key desc type match settings files)
373 key The key (one or more characters as a string) to be associated
374 with the command.
375 desc A description of the command, when omitted or nil, a default
376 description is built using MATCH.
377 type The command type, any of the following symbols:
378 agenda The daily/weekly agenda.
379 todo Entries with a specific TODO keyword, in all agenda files.
380 search Entries containing search words entry or headline.
381 tags Tags/Property/TODO match in all agenda files.
382 tags-todo Tags/P/T match in all agenda files, TODO entries only.
383 todo-tree Sparse tree of specific TODO keyword in *current* file.
384 tags-tree Sparse tree with all tags matches in *current* file.
385 occur-tree Occur sparse tree for *current* file.
386 ... A user-defined function.
387 match What to search for:
388 - a single keyword for TODO keyword searches
389 - a tags match expression for tags searches
390 - a word search expression for text searches.
391 - a regular expression for occur searches
392 For all other commands, this should be the empty string.
393 settings A list of option settings, similar to that in a let form, so like
394 this: ((opt1 val1) (opt2 val2) ...). The values will be
395 evaluated at the moment of execution, so quote them when needed.
396 files A list of files file to write the produced agenda buffer to
397 with the command `org-store-agenda-views'.
398 If a file name ends in \".html\", an HTML version of the buffer
399 is written out. If it ends in \".ps\", a postscript version is
400 produced. Otherwise, only the plain text is written to the file.
402 You can also define a set of commands, to create a composite agenda buffer.
403 In this case, an entry looks like this:
405 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
407 where
409 desc A description string to be displayed in the dispatcher menu.
410 cmd An agenda command, similar to the above. However, tree commands
411 are not allowed, but instead you can get agenda and global todo list.
412 So valid commands for a set are:
413 (agenda \"\" settings)
414 (alltodo \"\" settings)
415 (stuck \"\" settings)
416 (todo \"match\" settings files)
417 (search \"match\" settings files)
418 (tags \"match\" settings files)
419 (tags-todo \"match\" settings files)
421 Each command can carry a list of options, and another set of options can be
422 given for the whole set of commands. Individual command options take
423 precedence over the general options.
425 When using several characters as key to a command, the first characters
426 are prefix commands. For the dispatcher to display useful information, you
427 should provide a description for the prefix, like
429 (setq org-agenda-custom-commands
430 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
431 (\"hl\" tags \"+HOME+Lisa\")
432 (\"hp\" tags \"+HOME+Peter\")
433 (\"hk\" tags \"+HOME+Kim\")))"
434 :group 'org-agenda-custom-commands
435 :type `(repeat
436 (choice :value ("x" "Describe command here" tags "" nil)
437 (list :tag "Single command"
438 (string :tag "Access Key(s) ")
439 (option (string :tag "Description"))
440 (choice
441 (const :tag "Agenda" agenda)
442 (const :tag "TODO list" alltodo)
443 (const :tag "Search words" search)
444 (const :tag "Stuck projects" stuck)
445 (const :tag "Tags/Property match (all agenda files)" tags)
446 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
447 (const :tag "TODO keyword search (all agenda files)" todo)
448 (const :tag "Tags sparse tree (current buffer)" tags-tree)
449 (const :tag "TODO keyword tree (current buffer)" todo-tree)
450 (const :tag "Occur tree (current buffer)" occur-tree)
451 (sexp :tag "Other, user-defined function"))
452 (string :tag "Match (only for some commands)")
453 ,org-agenda-custom-commands-local-options
454 (option (repeat :tag "Export" (file :tag "Export to"))))
455 (list :tag "Command series, all agenda files"
456 (string :tag "Access Key(s)")
457 (string :tag "Description ")
458 (repeat :tag "Component"
459 (choice
460 (list :tag "Agenda"
461 (const :format "" agenda)
462 (const :tag "" :format "" "")
463 ,org-agenda-custom-commands-local-options)
464 (list :tag "TODO list (all keywords)"
465 (const :format "" alltodo)
466 (const :tag "" :format "" "")
467 ,org-agenda-custom-commands-local-options)
468 (list :tag "Search words"
469 (const :format "" search)
470 (string :tag "Match")
471 ,org-agenda-custom-commands-local-options)
472 (list :tag "Stuck projects"
473 (const :format "" stuck)
474 (const :tag "" :format "" "")
475 ,org-agenda-custom-commands-local-options)
476 (list :tag "Tags search"
477 (const :format "" tags)
478 (string :tag "Match")
479 ,org-agenda-custom-commands-local-options)
480 (list :tag "Tags search, TODO entries only"
481 (const :format "" tags-todo)
482 (string :tag "Match")
483 ,org-agenda-custom-commands-local-options)
484 (list :tag "TODO keyword search"
485 (const :format "" todo)
486 (string :tag "Match")
487 ,org-agenda-custom-commands-local-options)
488 (list :tag "Other, user-defined function"
489 (symbol :tag "function")
490 (string :tag "Match")
491 ,org-agenda-custom-commands-local-options)))
493 (repeat :tag "Settings for entire command set"
494 (list (variable :tag "Any variable")
495 (sexp :tag "Value")))
496 (option (repeat :tag "Export" (file :tag "Export to"))))
497 (cons :tag "Prefix key documentation"
498 (string :tag "Access Key(s)")
499 (string :tag "Description ")))))
501 (defcustom org-agenda-query-register ?o
502 "The register holding the current query string.
503 The purpose of this is that if you construct a query string interactively,
504 you can then use it to define a custom command."
505 :group 'org-agenda-custom-commands
506 :type 'character)
508 (defcustom org-stuck-projects
509 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
510 "How to identify stuck projects.
511 This is a list of four items:
512 1. A tags/todo/property matcher string that is used to identify a project.
513 See the manual for a description of tag and property searches.
514 The entire tree below a headline matched by this is considered one project.
515 2. A list of TODO keywords identifying non-stuck projects.
516 If the project subtree contains any headline with one of these todo
517 keywords, the project is considered to be not stuck. If you specify
518 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
519 3. A list of tags identifying non-stuck projects.
520 If the project subtree contains any headline with one of these tags,
521 the project is considered to be not stuck. If you specify \"*\" as
522 a tag, any tag will mark the project unstuck. Note that this is about
523 the explicit presence of a tag somewhere in the subtree, inherited
524 tags to not count here. If inherited tags make a project not stuck,
525 use \"-TAG\" in the tags part of the matcher under (1.) above.
526 4. An arbitrary regular expression matching non-stuck projects.
528 If the project turns out to be not stuck, search continues also in the
529 subtree to see if any of the subtasks have project status.
531 See also the variable `org-tags-match-list-sublevels' which applies
532 to projects matched by this search as well.
534 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
535 or `C-c a #' to produce the list."
536 :group 'org-agenda-custom-commands
537 :type '(list
538 (string :tag "Tags/TODO match to identify a project")
539 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
540 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
541 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
543 (defcustom org-agenda-filter-effort-default-operator "<"
544 "The default operator for effort estimate filtering.
545 If you select an effort estimate limit without first pressing an operator,
546 this one will be used."
547 :group 'org-agenda-custom-commands
548 :type '(choice (const :tag "less or equal" "<")
549 (const :tag "greater or equal"">")
550 (const :tag "equal" "=")))
552 (defgroup org-agenda-skip nil
553 "Options concerning skipping parts of agenda files."
554 :tag "Org Agenda Skip"
555 :group 'org-agenda)
557 (defcustom org-agenda-skip-function-global nil
558 "Function to be called at each match during agenda construction.
559 If this function returns nil, the current match should not be skipped.
560 If the function decided to skip an agenda match, is must return the
561 buffer position from which the search should be continued.
562 This may also be a Lisp form, which will be evaluated.
564 This variable will be applied to every agenda match, including
565 tags/property searches and TODO lists. So try to make the test function
566 do its checking as efficiently as possible. To implement a skipping
567 condition just for specific agenda commands, use the variable
568 `org-agenda-skip-function' which can be set in the options section
569 of custom agenda commands."
570 :group 'org-agenda-skip
571 :type 'sexp)
573 (defgroup org-agenda-daily/weekly nil
574 "Options concerning the daily/weekly agenda."
575 :tag "Org Agenda Daily/Weekly"
576 :group 'org-agenda)
577 (defgroup org-agenda-todo-list nil
578 "Options concerning the global todo list agenda view."
579 :tag "Org Agenda Todo List"
580 :group 'org-agenda)
581 (defgroup org-agenda-match-view nil
582 "Options concerning the general tags/property/todo match agenda view."
583 :tag "Org Agenda Match View"
584 :group 'org-agenda)
585 (defgroup org-agenda-search-view nil
586 "Options concerning the general tags/property/todo match agenda view."
587 :tag "Org Agenda Match View"
588 :group 'org-agenda)
590 (defvar org-agenda-archives-mode nil
591 "Non-nil means the agenda will include archived items.
592 If this is the symbol `trees', trees in the selected agenda scope
593 that are marked with the ARCHIVE tag will be included anyway. When this is
594 t, also all archive files associated with the current selection of agenda
595 files will be included.")
597 (defcustom org-agenda-skip-comment-trees t
598 "Non-nil means skip trees that start with the COMMENT keyword.
599 When nil, these trees are also scanned by agenda commands."
600 :group 'org-agenda-skip
601 :type 'boolean)
603 (defcustom org-agenda-todo-list-sublevels t
604 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
605 When nil, the sublevels of a TODO entry are not checked, resulting in
606 potentially much shorter TODO lists."
607 :group 'org-agenda-skip
608 :group 'org-agenda-todo-list
609 :type 'boolean)
611 (defcustom org-agenda-todo-ignore-with-date nil
612 "Non-nil means don't show entries with a date in the global todo list.
613 You can use this if you prefer to mark mere appointments with a TODO keyword,
614 but don't want them to show up in the TODO list.
615 When this is set, it also covers deadlines and scheduled items, the settings
616 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
617 will be ignored.
618 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
619 :group 'org-agenda-skip
620 :group 'org-agenda-todo-list
621 :type 'boolean)
623 (defcustom org-agenda-todo-ignore-timestamp nil
624 "Non-nil means don't show entries with a timestamp.
625 This applies when creating the global todo list.
626 Valid values are:
628 past Don't show entries for today or in the past.
630 future Don't show entries with a timestamp in the future.
631 The idea behind this is that if it has a future
632 timestamp, you don't want to think about it until the
633 date.
635 all Don't show any entries with a timestamp in the global todo list.
636 The idea behind this is that by setting a timestamp, you
637 have already \"taken care\" of this item.
639 This variable can also have an integer as a value. If positive (N),
640 todos with a timestamp N or more days in the future will be ignored. If
641 negative (-N), todos with a timestamp N or more days in the past will be
642 ignored. If 0, todos with a timestamp either today or in the future will
643 be ignored. For example, a value of -1 will exclude todos with a
644 timestamp in the past (yesterday or earlier), while a value of 7 will
645 exclude todos with a timestamp a week or more in the future.
647 See also `org-agenda-todo-ignore-with-date'.
648 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
649 to make his option also apply to the tags-todo list."
650 :group 'org-agenda-skip
651 :group 'org-agenda-todo-list
652 :version "24.1"
653 :type '(choice
654 (const :tag "Ignore future timestamp todos" future)
655 (const :tag "Ignore past or present timestamp todos" past)
656 (const :tag "Ignore all timestamp todos" all)
657 (const :tag "Show timestamp todos" nil)
658 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
660 (defcustom org-agenda-todo-ignore-scheduled nil
661 "Non-nil means, ignore some scheduled TODO items when making TODO list.
662 This applies when creating the global todo list.
663 Valid values are:
665 past Don't show entries scheduled today or in the past.
667 future Don't show entries scheduled in the future.
668 The idea behind this is that by scheduling it, you don't want to
669 think about it until the scheduled date.
671 all Don't show any scheduled entries in the global todo list.
672 The idea behind this is that by scheduling it, you have already
673 \"taken care\" of this item.
675 t Same as `all', for backward compatibility.
677 This variable can also have an integer as a value. See
678 `org-agenda-todo-ignore-timestamp' for more details.
680 See also `org-agenda-todo-ignore-with-date'.
681 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
682 to make his option also apply to the tags-todo list."
683 :group 'org-agenda-skip
684 :group 'org-agenda-todo-list
685 :type '(choice
686 (const :tag "Ignore future-scheduled todos" future)
687 (const :tag "Ignore past- or present-scheduled todos" past)
688 (const :tag "Ignore all scheduled todos" all)
689 (const :tag "Ignore all scheduled todos (compatibility)" t)
690 (const :tag "Show scheduled todos" nil)
691 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
693 (defcustom org-agenda-todo-ignore-deadlines nil
694 "Non-nil means ignore some deadlined TODO items when making TODO list.
695 There are different motivations for using different values, please think
696 carefully when configuring this variable.
698 This applies when creating the global todo list.
699 Valid values are:
701 near Don't show near deadline entries. A deadline is near when it is
702 closer than `org-deadline-warning-days' days. The idea behind this
703 is that such items will appear in the agenda anyway.
705 far Don't show TODO entries where a deadline has been defined, but
706 the deadline is not near. This is useful if you don't want to
707 use the todo list to figure out what to do now.
709 past Don't show entries with a deadline timestamp for today or in the past.
711 future Don't show entries with a deadline timestamp in the future, not even
712 when they become `near' ones. Use it with caution.
714 all Ignore all TODO entries that do have a deadline.
716 t Same as `near', for backward compatibility.
718 This variable can also have an integer as a value. See
719 `org-agenda-todo-ignore-timestamp' for more details.
721 See also `org-agenda-todo-ignore-with-date'.
722 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
723 to make his option also apply to the tags-todo list."
724 :group 'org-agenda-skip
725 :group 'org-agenda-todo-list
726 :type '(choice
727 (const :tag "Ignore near deadlines" near)
728 (const :tag "Ignore near deadlines (compatibility)" t)
729 (const :tag "Ignore far deadlines" far)
730 (const :tag "Ignore all TODOs with a deadlines" all)
731 (const :tag "Show all TODOs, even if they have a deadline" nil)
732 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
734 (defcustom org-agenda-tags-todo-honor-ignore-options nil
735 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
736 The variables
737 `org-agenda-todo-ignore-with-date',
738 `org-agenda-todo-ignore-timestamp',
739 `org-agenda-todo-ignore-scheduled',
740 `org-agenda-todo-ignore-deadlines'
741 make the global TODO list skip entries that have time stamps of certain
742 kinds. If this option is set, the same options will also apply for the
743 tags-todo search, which is the general tags/property matcher
744 restricted to unfinished TODO entries only."
745 :group 'org-agenda-skip
746 :group 'org-agenda-todo-list
747 :group 'org-agenda-match-view
748 :type 'boolean)
750 (defcustom org-agenda-skip-scheduled-if-done nil
751 "Non-nil means don't show scheduled items in agenda when they are done.
752 This is relevant for the daily/weekly agenda, not for the TODO list. And
753 it applies only to the actual date of the scheduling. Warnings about
754 an item with a past scheduling dates are always turned off when the item
755 is DONE."
756 :group 'org-agenda-skip
757 :group 'org-agenda-daily/weekly
758 :type 'boolean)
760 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
761 "Non-nil means skip scheduling line if same entry shows because of deadline.
762 In the agenda of today, an entry can show up multiple times because
763 it is both scheduled and has a nearby deadline, and maybe a plain time
764 stamp as well.
765 When this variable is t, then only the deadline is shown and the fact that
766 the entry is scheduled today or was scheduled previously is not shown.
767 When this variable is nil, the entry will be shown several times. When
768 the variable is the symbol `not-today', then skip scheduled previously,
769 but not scheduled today."
770 :group 'org-agenda-skip
771 :group 'org-agenda-daily/weekly
772 :type '(choice
773 (const :tag "Never" nil)
774 (const :tag "Always" t)
775 (const :tag "Not when scheduled today" not-today)))
777 (defcustom org-agenda-skip-deadline-if-done nil
778 "Non-nil means don't show deadlines when the corresponding item is done.
779 When nil, the deadline is still shown and should give you a happy feeling.
780 This is relevant for the daily/weekly agenda. And it applied only to the
781 actually date of the deadline. Warnings about approaching and past-due
782 deadlines are always turned off when the item is DONE."
783 :group 'org-agenda-skip
784 :group 'org-agenda-daily/weekly
785 :type 'boolean)
787 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
788 "Non-nil means skip deadline prewarning when entry is also scheduled.
789 This will apply on all days where a prewarning for the deadline would
790 be shown, but not at the day when the entry is actually due. On that day,
791 the deadline will be shown anyway.
792 This variable may be set to nil, t, or a number which will then give
793 the number of days before the actual deadline when the prewarnings
794 should resume.
795 This can be used in a workflow where the first showing of the deadline will
796 trigger you to schedule it, and then you don't want to be reminded of it
797 because you will take care of it on the day when scheduled."
798 :group 'org-agenda-skip
799 :group 'org-agenda-daily/weekly
800 :version "24.1"
801 :type '(choice
802 (const :tag "Alwas show prewarning" nil)
803 (const :tag "Remove prewarning if entry is scheduled" t)
804 (integer :tag "Restart prewarning N days before deadline")))
806 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
807 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
808 When non-nil, after the search for timestamps has matched once in an
809 entry, the rest of the entry will not be searched."
810 :group 'org-agenda-skip
811 :type 'boolean)
813 (defcustom org-agenda-skip-timestamp-if-done nil
814 "Non-nil means don't select item by timestamp or -range if it is DONE."
815 :group 'org-agenda-skip
816 :group 'org-agenda-daily/weekly
817 :type 'boolean)
819 (defcustom org-agenda-dim-blocked-tasks t
820 "Non-nil means dim blocked tasks in the agenda display.
821 This causes some overhead during agenda construction, but if you
822 have turned on `org-enforce-todo-dependencies',
823 `org-enforce-todo-checkbox-dependencies', or any other blocking
824 mechanism, this will create useful feedback in the agenda.
826 Instead of t, this variable can also have the value `invisible'.
827 Then blocked tasks will be invisible and only become visible when
828 they become unblocked. An exemption to this behavior is when a task is
829 blocked because of unchecked checkboxes below it. Since checkboxes do
830 not show up in the agenda views, making this task invisible you remove any
831 trace from agenda views that there is something to do. Therefore, a task
832 that is blocked because of checkboxes will never be made invisible, it
833 will only be dimmed."
834 :group 'org-agenda-daily/weekly
835 :group 'org-agenda-todo-list
836 :type '(choice
837 (const :tag "Do not dim" nil)
838 (const :tag "Dim to a gray face" t)
839 (const :tag "Make invisible" invisible)))
841 (defcustom org-timeline-show-empty-dates 3
842 "Non-nil means `org-timeline' also shows dates without an entry.
843 When nil, only the days which actually have entries are shown.
844 When t, all days between the first and the last date are shown.
845 When an integer, show also empty dates, but if there is a gap of more than
846 N days, just insert a special line indicating the size of the gap."
847 :group 'org-agenda-skip
848 :type '(choice
849 (const :tag "None" nil)
850 (const :tag "All" t)
851 (integer :tag "at most")))
853 (defgroup org-agenda-startup nil
854 "Options concerning initial settings in the Agenda in Org Mode."
855 :tag "Org Agenda Startup"
856 :group 'org-agenda)
858 (defcustom org-agenda-menu-show-matcher t
859 "Non-nil means show the match string in the agenda dispatcher menu.
860 When nil, the matcher string is not shown, but is put into the help-echo
861 property so than moving the mouse over the command shows it.
862 Setting it to nil is good if matcher strings are very long and/or if
863 you want to use two-column display (see `org-agenda-menu-two-column')."
864 :group 'org-agenda
865 :version "24.1"
866 :type 'boolean)
868 (defcustom org-agenda-menu-two-column nil
869 "Non-nil means, use two columns to show custom commands in the dispatcher.
870 If you use this, you probably want to set `org-agenda-menu-show-matcher'
871 to nil."
872 :group 'org-agenda
873 :version "24.1"
874 :type 'boolean)
876 (defcustom org-finalize-agenda-hook nil
877 "Hook run just before displaying an agenda buffer."
878 :group 'org-agenda-startup
879 :type 'hook)
881 (defcustom org-agenda-mouse-1-follows-link nil
882 "Non-nil means mouse-1 on a link will follow the link in the agenda.
883 A longer mouse click will still set point. Does not work on XEmacs.
884 Needs to be set before org.el is loaded."
885 :group 'org-agenda-startup
886 :type 'boolean)
888 (defcustom org-agenda-start-with-follow-mode nil
889 "The initial value of follow mode in a newly created agenda window."
890 :group 'org-agenda-startup
891 :type 'boolean)
893 (defcustom org-agenda-follow-indirect nil
894 "Non-nil means `org-agenda-follow-mode' displays only the
895 current item's tree, in an indirect buffer."
896 :group 'org-agenda
897 :version "24.1"
898 :type 'boolean)
900 (defcustom org-agenda-show-outline-path t
901 "Non-nil means show outline path in echo area after line motion."
902 :group 'org-agenda-startup
903 :type 'boolean)
905 (defcustom org-agenda-start-with-entry-text-mode nil
906 "The initial value of entry-text-mode in a newly created agenda window."
907 :group 'org-agenda-startup
908 :type 'boolean)
910 (defcustom org-agenda-entry-text-maxlines 5
911 "Number of text lines to be added when `E' is pressed in the agenda.
913 Note that this variable only used during agenda display. Add add entry text
914 when exporting the agenda, configure the variable
915 `org-agenda-add-entry-ext-maxlines'."
916 :group 'org-agenda
917 :type 'integer)
919 (defcustom org-agenda-entry-text-exclude-regexps nil
920 "List of regular expressions to clean up entry text.
921 The complete matches of all regular expressions in this list will be
922 removed from entry text before it is shown in the agenda."
923 :group 'org-agenda
924 :type '(repeat (regexp)))
926 (defvar org-agenda-entry-text-cleanup-hook nil
927 "Hook that is run after basic cleanup of entry text to be shown in agenda.
928 This cleanup is done in a temporary buffer, so the function may inspect and
929 change the entire buffer.
930 Some default stuff like drawers and scheduling/deadline dates will already
931 have been removed when this is called, as will any matches for regular
932 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
934 (defvar org-agenda-include-inactive-timestamps nil
935 "Non-nil means include inactive time stamps in agenda and timeline.")
937 (defgroup org-agenda-windows nil
938 "Options concerning the windows used by the Agenda in Org Mode."
939 :tag "Org Agenda Windows"
940 :group 'org-agenda)
942 (defcustom org-agenda-window-setup 'reorganize-frame
943 "How the agenda buffer should be displayed.
944 Possible values for this option are:
946 current-window Show agenda in the current window, keeping all other windows.
947 other-window Use `switch-to-buffer-other-window' to display agenda.
948 reorganize-frame Show only two windows on the current frame, the current
949 window and the agenda.
950 other-frame Use `switch-to-buffer-other-frame' to display agenda.
951 Also, when exiting the agenda, kill that frame.
952 See also the variable `org-agenda-restore-windows-after-quit'."
953 :group 'org-agenda-windows
954 :type '(choice
955 (const current-window)
956 (const other-frame)
957 (const other-window)
958 (const reorganize-frame)))
960 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
961 "The min and max height of the agenda window as a fraction of frame height.
962 The value of the variable is a cons cell with two numbers between 0 and 1.
963 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
964 :group 'org-agenda-windows
965 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
967 (defcustom org-agenda-restore-windows-after-quit nil
968 "Non-nil means restore window configuration upon exiting agenda.
969 Before the window configuration is changed for displaying the agenda,
970 the current status is recorded. When the agenda is exited with
971 `q' or `x' and this option is set, the old state is restored. If
972 `org-agenda-window-setup' is `other-frame', the value of this
973 option will be ignored."
974 :group 'org-agenda-windows
975 :type 'boolean)
977 (defcustom org-agenda-ndays nil
978 "Number of days to include in overview display.
979 Should be 1 or 7.
980 Obsolete, see `org-agenda-span'."
981 :group 'org-agenda-daily/weekly
982 :type 'integer)
984 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
986 (defcustom org-agenda-span 'week
987 "Number of days to include in overview display.
988 Can be day, week, month, year, or any number of days.
989 Custom commands can set this variable in the options section."
990 :group 'org-agenda-daily/weekly
991 :type '(choice (const :tag "Day" day)
992 (const :tag "Week" week)
993 (const :tag "Month" month)
994 (const :tag "Year" year)
995 (integer :tag "Custom")))
997 (defcustom org-agenda-start-on-weekday 1
998 "Non-nil means start the overview always on the specified weekday.
999 0 denotes Sunday, 1 denotes Monday etc.
1000 When nil, always start on the current day.
1001 Custom commands can set this variable in the options section."
1002 :group 'org-agenda-daily/weekly
1003 :type '(choice (const :tag "Today" nil)
1004 (integer :tag "Weekday No.")))
1006 (defcustom org-agenda-show-all-dates t
1007 "Non-nil means `org-agenda' shows every day in the selected range.
1008 When nil, only the days which actually have entries are shown."
1009 :group 'org-agenda-daily/weekly
1010 :type 'boolean)
1012 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1013 "Format string for displaying dates in the agenda.
1014 Used by the daily/weekly agenda and by the timeline. This should be
1015 a format string understood by `format-time-string', or a function returning
1016 the formatted date as a string. The function must take a single argument,
1017 a calendar-style date list like (month day year)."
1018 :group 'org-agenda-daily/weekly
1019 :type '(choice
1020 (string :tag "Format string")
1021 (function :tag "Function")))
1023 (defun org-agenda-format-date-aligned (date)
1024 "Format a date string for display in the daily/weekly agenda, or timeline.
1025 This function makes sure that dates are aligned for easy reading."
1026 (require 'cal-iso)
1027 (let* ((dayname (calendar-day-name date))
1028 (day (cadr date))
1029 (day-of-week (calendar-day-of-week date))
1030 (month (car date))
1031 (monthname (calendar-month-name month))
1032 (year (nth 2 date))
1033 (iso-week (org-days-to-iso-week
1034 (calendar-absolute-from-gregorian date)))
1035 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1036 (1- year))
1037 ((and (= month 12) (<= iso-week 1))
1038 (1+ year))
1039 (t year)))
1040 (weekstring (if (= day-of-week 1)
1041 (format " W%02d" iso-week)
1042 "")))
1043 (format "%-10s %2d %s %4d%s"
1044 dayname day monthname year weekstring)))
1046 (defcustom org-agenda-time-leading-zero nil
1047 "Non-nil means use leading zero for military times in agenda.
1048 For example, 9:30am would become 09:30 rather than 9:30."
1049 :group 'org-agenda-daily/weekly
1050 :version "24.1"
1051 :type 'boolean)
1053 (defcustom org-agenda-timegrid-use-ampm nil
1054 "When set, show AM/PM style timestamps on the timegrid."
1055 :group 'org-agenda
1056 :version "24.1"
1057 :type 'boolean)
1059 (defun org-agenda-time-of-day-to-ampm (time)
1060 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1061 (let* ((hour-number (string-to-number (substring time 0 -3)))
1062 (minute (substring time -2))
1063 (ampm "am"))
1064 (cond
1065 ((equal hour-number 12)
1066 (setq ampm "pm"))
1067 ((> hour-number 12)
1068 (setq ampm "pm")
1069 (setq hour-number (- hour-number 12))))
1070 (concat
1071 (if org-agenda-time-leading-zero
1072 (format "%02d" hour-number)
1073 (format "%02s" (number-to-string hour-number)))
1074 ":" minute ampm)))
1076 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1077 "Conditionally convert TIME to AM/PM format
1078 based on `org-agenda-timegrid-use-ampm'"
1079 (if org-agenda-timegrid-use-ampm
1080 (org-agenda-time-of-day-to-ampm time)
1081 time))
1083 (defcustom org-agenda-weekend-days '(6 0)
1084 "Which days are weekend?
1085 These days get the special face `org-agenda-date-weekend' in the agenda
1086 and timeline buffers."
1087 :group 'org-agenda-daily/weekly
1088 :type '(set :greedy t
1089 (const :tag "Monday" 1)
1090 (const :tag "Tuesday" 2)
1091 (const :tag "Wednesday" 3)
1092 (const :tag "Thursday" 4)
1093 (const :tag "Friday" 5)
1094 (const :tag "Saturday" 6)
1095 (const :tag "Sunday" 0)))
1097 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1098 "Non-nil means jump to today when moving a past date forward in time.
1099 When using S-right in the agenda to move a a date forward, and the date
1100 stamp currently points to the past, the first key press will move it
1101 to today. WHen nil, just move one day forward even if the date stays
1102 in the past."
1103 :group 'org-agenda-daily/weekly
1104 :version "24.1"
1105 :type 'boolean)
1107 (defcustom org-agenda-include-diary nil
1108 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1109 Custom commands can set this variable in the options section."
1110 :group 'org-agenda-daily/weekly
1111 :type 'boolean)
1113 (defcustom org-agenda-include-deadlines t
1114 "If non-nil, include entries within their deadline warning period.
1115 Custom commands can set this variable in the options section."
1116 :group 'org-agenda-daily/weekly
1117 :version "24.1"
1118 :type 'boolean)
1120 (defcustom org-agenda-repeating-timestamp-show-all t
1121 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1122 When set to a list of strings, only show occurrences of repeating
1123 stamps for these TODO keywords. When nil, only one occurrence is
1124 shown, either today or the nearest into the future."
1125 :group 'org-agenda-daily/weekly
1126 :type '(choice
1127 (const :tag "Show repeating stamps" t)
1128 (repeat :tag "Show repeating stamps for these TODO keywords"
1129 (string :tag "TODO Keyword"))
1130 (const :tag "Don't show repeating stamps" nil)))
1132 (defcustom org-scheduled-past-days 10000
1133 "No. of days to continue listing scheduled items that are not marked DONE.
1134 When an item is scheduled on a date, it shows up in the agenda on this
1135 day and will be listed until it is marked done for the number of days
1136 given here."
1137 :group 'org-agenda-daily/weekly
1138 :type 'integer)
1140 (defcustom org-agenda-log-mode-items '(closed clock)
1141 "List of items that should be shown in agenda log mode.
1142 This list may contain the following symbols:
1144 closed Show entries that have been closed on that day.
1145 clock Show entries that have received clocked time on that day.
1146 state Show all logged state changes.
1147 Note that instead of changing this variable, you can also press `C-u l' in
1148 the agenda to display all available LOG items temporarily."
1149 :group 'org-agenda-daily/weekly
1150 :type '(set :greedy t (const closed) (const clock) (const state)))
1152 (defcustom org-agenda-clock-consistency-checks
1153 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1154 :gap-ok-around ("4:00")
1155 :default-face ((:background "DarkRed") (:foreground "white"))
1156 :overlap-face nil :gap-face nil :no-end-time-face nil
1157 :long-face nil :short-face nil)
1158 "This is a property list, with the following keys:
1160 :max-duration Mark clocking chunks that are longer than this time.
1161 This is a time string like \"HH:MM\", or the number
1162 of minutes as an integer.
1164 :min-duration Mark clocking chunks that are shorter that this.
1165 This is a time string like \"HH:MM\", or the number
1166 of minutes as an integer.
1168 :max-gap Mark gaps between clocking chunks that are longer than
1169 this duration. A number of minutes, or a string
1170 like \"HH:MM\".
1172 :gap-ok-around List of times during the day which are usually not working
1173 times. When a gap is detected, but the gap contains any
1174 of these times, the gap is *not* reported. For example,
1175 if this is (\"4:00\" \"13:00\") then gaps that contain
1176 4:00 in the morning (i.e. the night) and 13:00
1177 (i.e. a typical lunch time) do not cause a warning.
1178 You should have at least one time during the night in this
1179 list, or otherwise the first task each morning will trigger
1180 a warning because it follows a long gap.
1182 Furthermore, the following properties can be used to define faces for
1183 issue display.
1185 :default-face the default face, if the specific face is undefined
1186 :overlap-face face for overlapping clocks
1187 :gap-face face for gaps between clocks
1188 :no-end-time-face face for incomplete clocks
1189 :long-face face for clock intervals that are too long
1190 :short-face face for clock intervals that are too short"
1191 :group 'org-agenda-daily/weekly
1192 :group 'org-clock
1193 :version "24.1"
1194 :type 'plist)
1196 (defcustom org-agenda-log-mode-add-notes t
1197 "Non-nil means add first line of notes to log entries in agenda views.
1198 If a log item like a state change or a clock entry is associated with
1199 notes, the first line of these notes will be added to the entry in the
1200 agenda display."
1201 :group 'org-agenda-daily/weekly
1202 :type 'boolean)
1204 (defcustom org-agenda-start-with-log-mode nil
1205 "The initial value of log-mode in a newly created agenda window."
1206 :group 'org-agenda-startup
1207 :group 'org-agenda-daily/weekly
1208 :type 'boolean)
1210 (defcustom org-agenda-start-with-clockreport-mode nil
1211 "The initial value of clockreport-mode in a newly created agenda window."
1212 :group 'org-agenda-startup
1213 :group 'org-agenda-daily/weekly
1214 :type 'boolean)
1216 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1217 "Property list with parameters for the clocktable in clockreport mode.
1218 This is the display mode that shows a clock table in the daily/weekly
1219 agenda, the properties for this dynamic block can be set here.
1220 The usual clocktable parameters are allowed here, but you cannot set
1221 the properties :name, :tstart, :tend, :block, and :scope - these will
1222 be overwritten to make sure the content accurately reflects the
1223 current display in the agenda."
1224 :group 'org-agenda-daily/weekly
1225 :type 'plist)
1227 (defcustom org-agenda-search-view-always-boolean nil
1228 "Non-nil means the search string is interpreted as individual parts.
1230 The search string for search view can either be interpreted as a phrase,
1231 or as a list of snippets that define a boolean search for a number of
1232 strings.
1234 When this is non-nil, the string will be split on whitespace, and each
1235 snippet will be searched individually, and all must match in order to
1236 select an entry. A snippet is then a single string of non-white
1237 characters, or a string in double quotes, or a regexp in {} braces.
1238 If a snippet is preceded by \"-\", the snippet must *not* match.
1239 \"+\" is syntactic sugar for positive selection. Each snippet may
1240 be found as a full word or a partial word, but see the variable
1241 `org-agenda-search-view-force-full-words'.
1243 When this is nil, search will look for the entire search phrase as one,
1244 with each space character matching any amount of whitespace, including
1245 line breaks.
1247 Even when this is nil, you can still switch to Boolean search dynamically
1248 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1249 is a regexp marked with braces like \"{abc}\", this will also switch to
1250 boolean search."
1251 :group 'org-agenda-search-view
1252 :version "24.1"
1253 :type 'boolean)
1255 (if (fboundp 'defvaralias)
1256 (defvaralias 'org-agenda-search-view-search-words-only
1257 'org-agenda-search-view-always-boolean))
1259 (defcustom org-agenda-search-view-force-full-words nil
1260 "Non-nil means, search words must be matches as complete words.
1261 When nil, they may also match part of a word."
1262 :group 'org-agenda-search-view
1263 :version "24.1"
1264 :type 'boolean)
1266 (defgroup org-agenda-time-grid nil
1267 "Options concerning the time grid in the Org-mode Agenda."
1268 :tag "Org Agenda Time Grid"
1269 :group 'org-agenda)
1271 (defcustom org-agenda-search-headline-for-time t
1272 "Non-nil means search headline for a time-of-day.
1273 If the headline contains a time-of-day in one format or another, it will
1274 be used to sort the entry into the time sequence of items for a day.
1275 Some people have time stamps in the headline that refer to the creation
1276 time or so, and then this produces an unwanted side effect. If this is
1277 the case for your, use this variable to turn off searching the headline
1278 for a time."
1279 :group 'org-agenda-time-grid
1280 :type 'boolean)
1282 (defcustom org-agenda-use-time-grid t
1283 "Non-nil means show a time grid in the agenda schedule.
1284 A time grid is a set of lines for specific times (like every two hours between
1285 8:00 and 20:00). The items scheduled for a day at specific times are
1286 sorted in between these lines.
1287 For details about when the grid will be shown, and what it will look like, see
1288 the variable `org-agenda-time-grid'."
1289 :group 'org-agenda-time-grid
1290 :type 'boolean)
1292 (defcustom org-agenda-time-grid
1293 '((daily today require-timed)
1294 "----------------"
1295 (800 1000 1200 1400 1600 1800 2000))
1297 "The settings for time grid for agenda display.
1298 This is a list of three items. The first item is again a list. It contains
1299 symbols specifying conditions when the grid should be displayed:
1301 daily if the agenda shows a single day
1302 weekly if the agenda shows an entire week
1303 today show grid on current date, independent of daily/weekly display
1304 require-timed show grid only if at least one item has a time specification
1306 The second item is a string which will be placed behind the grid time.
1308 The third item is a list of integers, indicating the times that should have
1309 a grid line."
1310 :group 'org-agenda-time-grid
1311 :type
1312 '(list
1313 (set :greedy t :tag "Grid Display Options"
1314 (const :tag "Show grid in single day agenda display" daily)
1315 (const :tag "Show grid in weekly agenda display" weekly)
1316 (const :tag "Always show grid for today" today)
1317 (const :tag "Show grid only if any timed entries are present"
1318 require-timed)
1319 (const :tag "Skip grid times already present in an entry"
1320 remove-match))
1321 (string :tag "Grid String")
1322 (repeat :tag "Grid Times" (integer :tag "Time"))))
1324 (defcustom org-agenda-show-current-time-in-grid t
1325 "Non-nil means show the current time in the time grid."
1326 :group 'org-agenda-time-grid
1327 :version "24.1"
1328 :type 'boolean)
1330 (defcustom org-agenda-current-time-string
1331 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1332 "The string for the current time marker in the agenda."
1333 :group 'org-agenda-time-grid
1334 :version "24.1"
1335 :type 'string)
1337 (defgroup org-agenda-sorting nil
1338 "Options concerning sorting in the Org-mode Agenda."
1339 :tag "Org Agenda Sorting"
1340 :group 'org-agenda)
1342 (defcustom org-agenda-sorting-strategy
1343 '((agenda habit-down time-up priority-down category-keep)
1344 (todo priority-down category-keep)
1345 (tags priority-down category-keep)
1346 (search category-keep))
1347 "Sorting structure for the agenda items of a single day.
1348 This is a list of symbols which will be used in sequence to determine
1349 if an entry should be listed before another entry. The following
1350 symbols are recognized:
1352 time-up Put entries with time-of-day indications first, early first
1353 time-down Put entries with time-of-day indications first, late first
1354 category-keep Keep the default order of categories, corresponding to the
1355 sequence in `org-agenda-files'.
1356 category-up Sort alphabetically by category, A-Z.
1357 category-down Sort alphabetically by category, Z-A.
1358 tag-up Sort alphabetically by last tag, A-Z.
1359 tag-down Sort alphabetically by last tag, Z-A.
1360 priority-up Sort numerically by priority, high priority last.
1361 priority-down Sort numerically by priority, high priority first.
1362 todo-state-up Sort by todo state, tasks that are done last.
1363 todo-state-down Sort by todo state, tasks that are done first.
1364 effort-up Sort numerically by estimated effort, high effort last.
1365 effort-down Sort numerically by estimated effort, high effort first.
1366 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1367 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1368 habit-up Put entries that are habits first
1369 habit-down Put entries that are habits last
1370 alpha-up Sort headlines alphabetically
1371 alpha-down Sort headlines alphabetically, reversed
1373 The different possibilities will be tried in sequence, and testing stops
1374 if one comparison returns a \"not-equal\". For example, the default
1375 '(time-up category-keep priority-down)
1376 means: Pull out all entries having a specified time of day and sort them,
1377 in order to make a time schedule for the current day the first thing in the
1378 agenda listing for the day. Of the entries without a time indication, keep
1379 the grouped in categories, don't sort the categories, but keep them in
1380 the sequence given in `org-agenda-files'. Within each category sort by
1381 priority.
1383 Leaving out `category-keep' would mean that items will be sorted across
1384 categories by priority.
1386 Instead of a single list, this can also be a set of list for specific
1387 contents, with a context symbol in the car of the list, any of
1388 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1390 Custom commands can bind this variable in the options section."
1391 :group 'org-agenda-sorting
1392 :type `(choice
1393 (repeat :tag "General" ,org-sorting-choice)
1394 (list :tag "Individually"
1395 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1396 (repeat ,org-sorting-choice))
1397 (cons (const :tag "Strategy for TODO lists" todo)
1398 (repeat ,org-sorting-choice))
1399 (cons (const :tag "Strategy for Tags matches" tags)
1400 (repeat ,org-sorting-choice))
1401 (cons (const :tag "Strategy for search matches" search)
1402 (repeat ,org-sorting-choice)))))
1404 (defcustom org-agenda-cmp-user-defined nil
1405 "A function to define the comparison `user-defined'.
1406 This function must receive two arguments, agenda entry a and b.
1407 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1408 the user comparison, return nil.
1409 When this is defined, you can make `user-defined-up' and `user-defined-down'
1410 part of an agenda sorting strategy."
1411 :group 'org-agenda-sorting
1412 :type 'symbol)
1414 (defcustom org-sort-agenda-notime-is-late t
1415 "Non-nil means items without time are considered late.
1416 This is only relevant for sorting. When t, items which have no explicit
1417 time like 15:30 will be considered as 99:01, i.e. later than any items which
1418 do have a time. When nil, the default time is before 0:00. You can use this
1419 option to decide if the schedule for today should come before or after timeless
1420 agenda entries."
1421 :group 'org-agenda-sorting
1422 :type 'boolean)
1424 (defcustom org-sort-agenda-noeffort-is-high t
1425 "Non-nil means items without effort estimate are sorted as high effort.
1426 This also applies when filtering an agenda view with respect to the
1427 < or > effort operator. Then, tasks with no effort defined will be treated
1428 as tasks with high effort.
1429 When nil, such items are sorted as 0 minutes effort."
1430 :group 'org-agenda-sorting
1431 :type 'boolean)
1433 (defgroup org-agenda-line-format nil
1434 "Options concerning the entry prefix in the Org-mode agenda display."
1435 :tag "Org Agenda Line Format"
1436 :group 'org-agenda)
1438 (defcustom org-agenda-prefix-format
1439 '((agenda . " %i %-12:c%?-12t% s")
1440 (timeline . " % s")
1441 (todo . " %i %-12:c")
1442 (tags . " %i %-12:c")
1443 (search . " %i %-12:c"))
1444 "Format specifications for the prefix of items in the agenda views.
1445 An alist with five entries, each for the different agenda types. The
1446 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1447 The values are format strings.
1449 This format works similar to a printf format, with the following meaning:
1451 %c the category of the item, \"Diary\" for entries from the diary,
1452 or as given by the CATEGORY keyword or derived from the file name
1453 %e the effort required by the item
1454 %i the icon category of the item, see `org-agenda-category-icon-alist'
1455 %T the last tag of the item (ignore inherited tags, which come first)
1456 %t the HH:MM time-of-day specification if one applies to the entry
1457 %s Scheduling/Deadline information, a short string
1458 %(expression) Eval EXPRESSION and replace the control string
1459 by the result
1461 All specifiers work basically like the standard `%s' of printf, but may
1462 contain two additional characters: a question mark just after the `%'
1463 and a whitespace/punctuation character just before the final letter.
1465 If the first character after `%' is a question mark, the entire field
1466 will only be included if the corresponding value applies to the current
1467 entry. This is useful for fields which should have fixed width when
1468 present, but zero width when absent. For example, \"%?-12t\" will
1469 result in a 12 character time field if a time of the day is specified,
1470 but will completely disappear in entries which do not contain a time.
1472 If there is punctuation or whitespace character just before the final
1473 format letter, this character will be appended to the field value if
1474 the value is not empty. For example, the format \"%-12:c\" leads to
1475 \"Diary: \" if the category is \"Diary\". If the category were be
1476 empty, no additional colon would be inserted.
1478 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1479 which means:
1481 - Indent the line with two space characters
1482 - Give the category a 12 chars wide field, padded with whitespace on
1483 the right (because of `-'). Append a colon if there is a category
1484 (because of `:').
1485 - If there is a time-of-day, put it into a 12 chars wide field. If no
1486 time, don't put in an empty field, just skip it (because of '?').
1487 - Finally, put the scheduling information.
1489 See also the variables `org-agenda-remove-times-when-in-prefix' and
1490 `org-agenda-remove-tags'.
1492 Custom commands can set this variable in the options section."
1493 :type '(choice
1494 (string :tag "General format")
1495 (list :greedy t :tag "View dependent"
1496 (cons (const agenda) (string :tag "Format"))
1497 (cons (const timeline) (string :tag "Format"))
1498 (cons (const todo) (string :tag "Format"))
1499 (cons (const tags) (string :tag "Format"))
1500 (cons (const search) (string :tag "Format"))))
1501 :group 'org-agenda-line-format)
1503 (defvar org-prefix-format-compiled nil
1504 "The compiled prefix format and associated variables.
1505 This is a list where first element is a list of variable bindings, and second
1506 element is the compiled format expression. See the variable
1507 `org-agenda-prefix-format'.")
1509 (defcustom org-agenda-todo-keyword-format "%-1s"
1510 "Format for the TODO keyword in agenda lines.
1511 Set this to something like \"%-12s\" if you want all TODO keywords
1512 to occupy a fixed space in the agenda display."
1513 :group 'org-agenda-line-format
1514 :type 'string)
1516 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1517 "Text preceding timerange entries in the agenda view.
1518 This is a list with two strings. The first applies when the range
1519 is entirely on one day. The second applies if the range spans several days.
1520 The strings may have two \"%d\" format specifiers which will be filled
1521 with the sequence number of the days, and the total number of days in the
1522 range, respectively."
1523 :group 'org-agenda-line-format
1524 :type '(list
1525 (string :tag "Deadline today ")
1526 (choice :tag "Deadline relative"
1527 (string :tag "Format string")
1528 (function))))
1530 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1531 "Text preceding scheduled items in the agenda view.
1532 This is a list with two strings. The first applies when the item is
1533 scheduled on the current day. The second applies when it has been scheduled
1534 previously, it may contain a %d indicating that this is the nth time that
1535 this item is scheduled, due to automatic rescheduling of unfinished items
1536 for the following day. So this number is one larger than the number of days
1537 that passed since this item was scheduled first."
1538 :group 'org-agenda-line-format
1539 :type '(list
1540 (string :tag "Scheduled today ")
1541 (string :tag "Scheduled previously")))
1543 (defcustom org-agenda-inactive-leader "["
1544 "Text preceding item pulled into the agenda by inactive time stamps.
1545 These entries are added to the agenda when pressing \"[\"."
1546 :group 'org-agenda-line-format
1547 :version "24.1"
1548 :type '(list
1549 (string :tag "Scheduled today ")
1550 (string :tag "Scheduled previously")))
1552 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1553 "Text preceding deadline items in the agenda view.
1554 This is a list with two strings. The first applies when the item has its
1555 deadline on the current day. The second applies when it is in the past or
1556 in the future, it may contain %d to capture how many days away the deadline
1557 is (was)."
1558 :group 'org-agenda-line-format
1559 :type '(list
1560 (string :tag "Deadline today ")
1561 (choice :tag "Deadline relative"
1562 (string :tag "Format string")
1563 (function))))
1565 (defcustom org-agenda-remove-times-when-in-prefix t
1566 "Non-nil means remove duplicate time specifications in agenda items.
1567 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1568 time-of-day specification in a headline or diary entry is extracted and
1569 placed into the prefix. If this option is non-nil, the original specification
1570 \(a timestamp or -range, or just a plain time(range) specification like
1571 11:30-4pm) will be removed for agenda display. This makes the agenda less
1572 cluttered.
1573 The option can be t or nil. It may also be the symbol `beg', indicating
1574 that the time should only be removed when it is located at the beginning of
1575 the headline/diary entry."
1576 :group 'org-agenda-line-format
1577 :type '(choice
1578 (const :tag "Always" t)
1579 (const :tag "Never" nil)
1580 (const :tag "When at beginning of entry" beg)))
1582 (defcustom org-agenda-remove-timeranges-from-blocks nil
1583 "Non-nil means remove time ranges specifications in agenda
1584 items that span on several days."
1585 :group 'org-agenda-line-format
1586 :version "24.1"
1587 :type 'boolean)
1589 (defcustom org-agenda-default-appointment-duration nil
1590 "Default duration for appointments that only have a starting time.
1591 When nil, no duration is specified in such cases.
1592 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1593 :group 'org-agenda-line-format
1594 :type '(choice
1595 (integer :tag "Minutes")
1596 (const :tag "No default duration")))
1598 (defcustom org-agenda-show-inherited-tags t
1599 "Non-nil means show inherited tags in each agenda line."
1600 :group 'org-agenda-line-format
1601 :type 'boolean)
1603 (defcustom org-agenda-hide-tags-regexp nil
1604 "Regular expression used to filter away specific tags in agenda views.
1605 This means that these tags will be present, but not be shown in the agenda
1606 line. Secondary filtering will still work on the hidden tags.
1607 Nil means don't hide any tags."
1608 :group 'org-agenda-line-format
1609 :type '(choice
1610 (const :tag "Hide none" nil)
1611 (string :tag "Regexp ")))
1613 (defcustom org-agenda-remove-tags nil
1614 "Non-nil means remove the tags from the headline copy in the agenda.
1615 When this is the symbol `prefix', only remove tags when
1616 `org-agenda-prefix-format' contains a `%T' specifier."
1617 :group 'org-agenda-line-format
1618 :type '(choice
1619 (const :tag "Always" t)
1620 (const :tag "Never" nil)
1621 (const :tag "When prefix format contains %T" prefix)))
1623 (if (fboundp 'defvaralias)
1624 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1625 'org-agenda-remove-tags))
1627 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1628 "Shift tags in agenda items to this column.
1629 If this number is positive, it specifies the column. If it is negative,
1630 it means that the tags should be flushright to that column. For example,
1631 -80 works well for a normal 80 character screen."
1632 :group 'org-agenda-line-format
1633 :type 'integer)
1635 (if (fboundp 'defvaralias)
1636 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1638 (defcustom org-agenda-fontify-priorities 'cookies
1639 "Non-nil means highlight low and high priorities in agenda.
1640 When t, the highest priority entries are bold, lowest priority italic.
1641 However, settings in `org-priority-faces' will overrule these faces.
1642 When this variable is the symbol `cookies', only fontify the
1643 cookies, not the entire task.
1644 This may also be an association list of priority faces, whose
1645 keys are the character values of `org-highest-priority',
1646 `org-default-priority', and `org-lowest-priority' (the default values
1647 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1648 color as a string, or a list like `(:background \"Red\")'.
1649 If it is a color, the variable `org-faces-easy-properties'
1650 determines if it is a foreground or a background color."
1651 :group 'org-agenda-line-format
1652 :type '(choice
1653 (const :tag "Never" nil)
1654 (const :tag "Defaults" t)
1655 (const :tag "Cookies only" cookies)
1656 (repeat :tag "Specify"
1657 (list (character :tag "Priority" :value ?A)
1658 (choice :tag "Face "
1659 (string :tag "Color")
1660 (sexp :tag "Face"))))))
1662 (defcustom org-agenda-day-face-function nil
1663 "Function called to determine what face should be used to display a day.
1664 The only argument passed to that function is the day. It should
1665 returns a face, or nil if does not want to specify a face and let
1666 the normal rules apply."
1667 :group 'org-agenda-line-format
1668 :version "24.1"
1669 :type 'function)
1671 (defcustom org-agenda-category-icon-alist nil
1672 "Alist of category icon to be displayed in agenda views.
1674 Each entry should have the following format:
1676 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1678 Where CATEGORY-REGEXP is a regexp matching the categories where
1679 the icon should be displayed.
1680 FILE-OR-DATA either a file path or a string containing image data.
1682 The other fields can be omitted safely if not needed:
1683 TYPE indicates the image type.
1684 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1685 image data.
1686 PROPS are additional image attributes to assign to the image,
1687 like, e.g. `:ascent center'.
1689 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1691 If you want to set the display properties yourself, just put a
1692 list as second element:
1694 (CATEGORY-REGEXP (MY PROPERTY LIST))
1696 For example, to display a 16px horizontal space for Emacs
1697 category, you can use:
1699 (\"Emacs\" '(space . (:width (16))))"
1700 :group 'org-agenda-line-format
1701 :version "24.1"
1702 :type '(alist :key-type (string :tag "Regexp matching category")
1703 :value-type (choice (list :tag "Icon"
1704 (string :tag "File or data")
1705 (symbol :tag "Type")
1706 (boolean :tag "Data?")
1707 (repeat :tag "Extra image properties" :inline t symbol))
1708 (list :tag "Display properties" sexp))))
1710 (defgroup org-agenda-column-view nil
1711 "Options concerning column view in the agenda."
1712 :tag "Org Agenda Column View"
1713 :group 'org-agenda)
1715 (defcustom org-agenda-columns-show-summaries t
1716 "Non-nil means show summaries for columns displayed in the agenda view."
1717 :group 'org-agenda-column-view
1718 :type 'boolean)
1720 (defcustom org-agenda-columns-compute-summary-properties t
1721 "Non-nil means recompute all summary properties before column view.
1722 When column view in the agenda is listing properties that have a summary
1723 operator, it can go to all relevant buffers and recompute the summaries
1724 there. This can mean overhead for the agenda column view, but is necessary
1725 to have thing up to date.
1726 As a special case, a CLOCKSUM property also makes sure that the clock
1727 computations are current."
1728 :group 'org-agenda-column-view
1729 :type 'boolean)
1731 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1732 "Non-nil means the duration of an appointment will add to day effort.
1733 The property to which appointment durations will be added is the one given
1734 in the option `org-effort-property'. If an appointment does not have
1735 an end time, `org-agenda-default-appointment-duration' will be used. If that
1736 is not set, an appointment without end time will not contribute to the time
1737 estimate."
1738 :group 'org-agenda-column-view
1739 :type 'boolean)
1741 (defcustom org-agenda-auto-exclude-function nil
1742 "A function called with a tag to decide if it is filtered on '/ RET'.
1743 The sole argument to the function, which is called once for each
1744 possible tag, is a string giving the name of the tag. The
1745 function should return either nil if the tag should be included
1746 as normal, or \"-<TAG>\" to exclude the tag.
1747 Note that for the purpose of tag filtering, only the lower-case version of
1748 all tags will be considered, so that this function will only ever see
1749 the lower-case version of all tags."
1750 :group 'org-agenda
1751 :type 'function)
1753 (defcustom org-agenda-bulk-custom-functions nil
1754 "Alist of characters and custom functions for bulk actions.
1755 For example, this value makes those two functions available:
1757 '((?R set-category)
1758 (?C bulk-cut))
1760 With selected entries in an agenda buffer, `B R' will call
1761 the custom function `set-category' on the selected entries.
1762 Note that functions in this alist don't need to be quoted."
1763 :type 'alist
1764 :version "24.1"
1765 :group 'org-agenda)
1767 (eval-when-compile
1768 (require 'cl))
1769 (require 'org)
1771 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1772 "Execute BODY with point at location given by `org-hd-marker' property.
1773 If STRING is non-nil, the text property will be fetched from position 0
1774 in that string. If STRING is nil, it will be fetched from the beginning
1775 of the current line."
1776 (org-with-gensyms (marker)
1777 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1778 'org-hd-marker ,string)))
1779 (with-current-buffer (marker-buffer ,marker)
1780 (save-excursion
1781 (goto-char ,marker)
1782 ,@body)))))
1783 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1785 (defun org-add-agenda-custom-command (entry)
1786 "Replace or add a command in `org-agenda-custom-commands'.
1787 This is mostly for hacking and trying a new command - once the command
1788 works you probably want to add it to `org-agenda-custom-commands' for good."
1789 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1790 (if ass
1791 (setcdr ass (cdr entry))
1792 (push entry org-agenda-custom-commands))))
1794 ;;; Define the org-agenda-mode
1796 (defvar org-agenda-mode-map (make-sparse-keymap)
1797 "Keymap for `org-agenda-mode'.")
1798 (if (fboundp 'defvaralias)
1799 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1801 (defvar org-agenda-menu) ; defined later in this file.
1802 (defvar org-agenda-restrict) ; defined later in this file.
1803 (defvar org-agenda-follow-mode nil)
1804 (defvar org-agenda-entry-text-mode nil)
1805 (defvar org-agenda-clockreport-mode nil)
1806 (defvar org-agenda-show-log nil)
1807 (defvar org-agenda-redo-command nil)
1808 (defvar org-agenda-query-string nil)
1809 (defvar org-agenda-mode-hook nil
1810 "Hook for `org-agenda-mode', run after the mode is turned on.")
1811 (defvar org-agenda-type nil)
1812 (defvar org-agenda-force-single-file nil)
1813 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1816 ;;; Multiple agenda buffers support
1818 (defcustom org-agenda-sticky nil
1819 "Non-nil means agenda q key will bury agenda buffers.
1820 Agenda commands will then show existing buffer instead of generating new ones.
1821 When nil, `q' will kill the single agenda buffer."
1822 :group 'org-agenda
1823 :type 'boolean)
1825 (defvar org-agenda-buffer nil
1826 "Agenda buffer currently being generated.")
1828 (defun org-toggle-sticky-agenda (&optional arg)
1829 "Toggle `org-agenda-sticky'."
1830 (interactive)
1831 (setq org-agenda-sticky (or arg (not org-agenda-sticky)))
1832 (org-agenda-kill-all-agenda-buffers)
1833 (message "Sticky agenda was %s" (if org-agenda-sticky "enabled" "disabled")))
1835 (defvar org-agenda-last-prefix-arg nil)
1836 (defvar org-agenda-this-buffer-name nil)
1837 (defvar org-agenda-doing-sticky-redo nil)
1838 (defvar org-agenda-this-buffer-is-sticky nil)
1840 ;; below list is generating by grepping org-agenda.el for defvar
1841 (defconst org-agenda-local-vars
1842 '(;; calendar-mode-map
1843 ; org-clock-current-task
1844 ; org-mobile-force-id-on-agenda-items
1845 ; org-habit-show-habits
1846 ; org-habit-show-habits-only-for-today
1847 org-agenda-this-buffer-name
1848 ; org-agenda-overriding-header
1849 ; org-agenda-title-append
1850 org-agenda-undo-list
1851 org-agenda-pending-undo-list
1852 org-agenda-archives-mode
1853 ; org-agenda-entry-text-cleanup-hook
1854 ; org-agenda-include-inactive-timestamps ?????????????
1855 ; org-prefix-format-compiled ???????????
1856 ; org-agenda-mode-map
1857 ; org-agenda-menu
1858 org-agenda-follow-mode
1859 org-agenda-entry-text-mode
1860 org-agenda-clockreport-mode
1861 org-agenda-show-log
1862 org-agenda-redo-command
1863 org-agenda-query-string
1864 org-agenda-type
1865 org-agenda-force-single-file
1866 org-agenda-bulk-marked-entries
1867 ; org-agenda-allow-remote-undo
1868 org-agenda-undo-has-started-in
1869 org-agenda-restrict
1870 org-agenda-restrict-begin
1871 org-agenda-restrict-end
1872 ; org-agenda-last-dispatch-buffer
1873 ;; org-agenda-overriding-restriction
1874 ;; org-agenda-overriding-arguments
1875 org-agenda-last-arguments
1876 org-agenda-info
1877 ; org-mobile-creating-agendas
1878 org-agenda-tag-filter-overlays
1879 org-agenda-cat-filter-overlays
1880 ; org-agenda-marker-table
1881 org-pre-agenda-window-conf
1882 org-agenda-columns-active
1883 org-agenda-name
1884 org-agenda-tag-filter
1885 org-agenda-category-filter
1886 org-agenda-tag-filter-while-redo
1887 org-agenda-tag-filter-preset
1888 org-agenda-category-filter-preset
1889 org-agenda-markers
1890 ; org-agenda-last-marker-time
1891 ; org-agenda-only-exact-dates
1892 ; org-agenda-start-day
1893 org-starting-day ; not necessary to include, but no harm
1894 org-agenda-current-span ; not necessary to include, but no harm
1895 org-arg-loc ; not necessary to include, but no harm
1896 ; org-agenda-entry-types
1897 ; org-agenda-search-history
1898 ; org-todo-only
1899 ; org-search-syntax-table
1900 org-agenda-last-search-view-search-was-boolean
1901 org-last-arg ; not necessary to include, but no harm
1902 ; org-agenda-skip-regexp
1903 ; org-agenda-overriding-header
1904 ; org-disable-agenda-to-diary
1905 ; diary-list-entries-hook
1906 ; diary-time-regexp
1907 ; org-agenda-cleanup-fancy-diary-hook
1908 ; org-diary-last-run-time
1909 ; org-heading-keyword-regexp-format
1910 ; org-agenda-sorting-strategy
1911 ; org-agenda-sorting-strategy-selected
1912 ; org-agenda-before-sorting-filter-function
1913 ; org-agenda-restriction-lock-overlay
1914 ; org-global-tags-completion-table
1915 org-agenda-filtered-by-category
1916 org-agenda-filter-form
1917 ; org-hl
1918 ; org-agenda-after-show-hook
1919 ; org-archive-default-command
1920 org-agenda-show-window ; not sure if needed, but probably OK
1921 org-agenda-cycle-counter ; not sure if needed, but probably OK
1922 ; org-last-heading-marker
1923 ;; calendar-longitude
1924 ;; calendar-latitude
1925 ;; calendar-location-name
1926 org-agenda-bulk-marked-entries
1927 ; appt-time-msg-list
1928 org-agenda-last-prefix-arg
1932 (defun org-agenda-mode ()
1933 "Mode for time-sorted view on action items in Org-mode files.
1935 The following commands are available:
1937 \\{org-agenda-mode-map}"
1938 (interactive)
1939 (cond (org-agenda-doing-sticky-redo
1940 ;; Refreshing sticky agenda-buffer
1942 ;; Preserve the value of `org-agenda-local-vars' variables,
1943 ;; while letting `kill-all-local-variables' kill the rest
1944 (let ((save (buffer-local-variables)))
1945 (kill-all-local-variables)
1946 (mapc 'make-local-variable org-agenda-local-vars)
1947 (dolist (elem save)
1948 (let ((var (car elem))
1949 (val (cdr elem)))
1950 (when (and val
1951 (member var org-agenda-local-vars))
1952 (set var val)))))
1953 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1954 (org-agenda-sticky
1955 ;; Creating a sticky Agenda buffer for the first time
1956 (kill-all-local-variables)
1957 (mapc 'make-local-variable org-agenda-local-vars)
1958 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1960 ;; Creating a non-sticky agenda buffer
1961 (kill-all-local-variables)
1962 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1963 (setq org-agenda-undo-list nil
1964 org-agenda-pending-undo-list nil
1965 org-agenda-bulk-marked-entries nil)
1966 (setq major-mode 'org-agenda-mode)
1967 ;; Keep global-font-lock-mode from turning on font-lock-mode
1968 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1969 (setq mode-name "Org-Agenda")
1970 (use-local-map org-agenda-mode-map)
1971 (easy-menu-add org-agenda-menu)
1972 (if org-startup-truncated (setq truncate-lines t))
1973 (org-set-local 'line-move-visual nil)
1974 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1975 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1976 ;; Make sure properties are removed when copying text
1977 (when (boundp 'buffer-substring-filters)
1978 (org-set-local 'buffer-substring-filters
1979 (cons (lambda (x)
1980 (set-text-properties 0 (length x) nil x) x)
1981 buffer-substring-filters)))
1982 (unless org-agenda-keep-modes
1983 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1984 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1985 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1986 org-agenda-show-log org-agenda-start-with-log-mode))
1988 (easy-menu-change
1989 '("Agenda") "Agenda Files"
1990 (append
1991 (list
1992 (vector
1993 (if (get 'org-agenda-files 'org-restrict)
1994 "Restricted to single file"
1995 "Edit File List")
1996 '(org-edit-agenda-file-list)
1997 (not (get 'org-agenda-files 'org-restrict)))
1998 "--")
1999 (mapcar 'org-file-menu-entry (org-agenda-files))))
2000 (org-agenda-set-mode-name)
2001 (apply
2002 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2003 (list 'org-agenda-mode-hook)))
2005 (substitute-key-definition 'undo 'org-agenda-undo
2006 org-agenda-mode-map global-map)
2007 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2008 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2009 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2010 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2011 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2012 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2013 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2014 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2015 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
2016 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2017 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2018 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2019 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2020 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2021 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2022 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2023 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2024 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2025 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2026 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2027 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2028 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2029 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2030 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2031 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2032 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2033 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2034 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2035 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2036 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2037 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2038 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2039 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2040 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2041 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2042 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2043 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2044 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2045 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2046 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
2047 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2048 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2049 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2050 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2051 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2053 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2054 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2055 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2056 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2057 (while l (org-defkey org-agenda-mode-map
2058 (int-to-string (pop l)) 'digit-argument)))
2060 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2061 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2062 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2063 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2064 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2065 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2066 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2067 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2068 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2069 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2070 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2071 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2072 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2073 'org-clock-modify-effort-estimate)
2074 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2075 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2076 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2077 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2078 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2079 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2080 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2081 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
2082 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2083 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2084 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2085 (substitute-key-definition 'next-line 'org-agenda-next-line
2086 org-agenda-mode-map global-map)
2087 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2088 org-agenda-mode-map global-map)
2089 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2090 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2091 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2092 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2093 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2094 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2095 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2096 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2097 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2098 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2099 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2100 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2101 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2102 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2103 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2104 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2105 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2106 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2107 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2108 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2109 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2110 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2111 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2112 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2113 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2114 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2115 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2116 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2117 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2118 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2120 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2121 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2122 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2123 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2124 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2125 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2126 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2127 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2128 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2129 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2130 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2132 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2133 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2134 (when org-agenda-mouse-1-follows-link
2135 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2136 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2137 '("Agenda"
2138 ("Agenda Files")
2139 "--"
2140 ("Agenda Dates"
2141 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2142 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2143 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2144 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2145 "--"
2146 ("View"
2147 ["Day View" org-agenda-day-view
2148 :active (org-agenda-check-type nil 'agenda)
2149 :style radio :selected (eq org-agenda-current-span 'day)
2150 :keys "v d (or just d)"]
2151 ["Week View" org-agenda-week-view
2152 :active (org-agenda-check-type nil 'agenda)
2153 :style radio :selected (eq org-agenda-current-span 'week)
2154 :keys "v w (or just w)"]
2155 ["Month View" org-agenda-month-view
2156 :active (org-agenda-check-type nil 'agenda)
2157 :style radio :selected (eq org-agenda-current-span 'month)
2158 :keys "v m"]
2159 ["Year View" org-agenda-year-view
2160 :active (org-agenda-check-type nil 'agenda)
2161 :style radio :selected (eq org-agenda-current-span 'year)
2162 :keys "v y"]
2163 "--"
2164 ["Include Diary" org-agenda-toggle-diary
2165 :style toggle :selected org-agenda-include-diary
2166 :active (org-agenda-check-type nil 'agenda)]
2167 ["Include Deadlines" org-agenda-toggle-deadlines
2168 :style toggle :selected org-agenda-include-deadlines
2169 :active (org-agenda-check-type nil 'agenda)]
2170 ["Use Time Grid" org-agenda-toggle-time-grid
2171 :style toggle :selected org-agenda-use-time-grid
2172 :active (org-agenda-check-type nil 'agenda)]
2173 "--"
2174 ["Show clock report" org-agenda-clockreport-mode
2175 :style toggle :selected org-agenda-clockreport-mode
2176 :active (org-agenda-check-type nil 'agenda)]
2177 ["Show some entry text" org-agenda-entry-text-mode
2178 :style toggle :selected org-agenda-entry-text-mode
2179 :active t]
2180 "--"
2181 ["Show Logbook entries" org-agenda-log-mode
2182 :style toggle :selected org-agenda-show-log
2183 :active (org-agenda-check-type nil 'agenda 'timeline)
2184 :keys "v l (or just l)"]
2185 ["Include archived trees" org-agenda-archives-mode
2186 :style toggle :selected org-agenda-archives-mode :active t
2187 :keys "v a"]
2188 ["Include archive files" (org-agenda-archives-mode t)
2189 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2190 :keys "v A"]
2191 "--"
2192 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2193 ["Write view to file" org-agenda-write t]
2194 ["Rebuild buffer" org-agenda-redo t]
2195 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2196 "--"
2197 ["Show original entry" org-agenda-show t]
2198 ["Go To (other window)" org-agenda-goto t]
2199 ["Go To (this window)" org-agenda-switch-to t]
2200 ["Follow Mode" org-agenda-follow-mode
2201 :style toggle :selected org-agenda-follow-mode :active t]
2202 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2203 "--"
2204 ("TODO"
2205 ["Cycle TODO" org-agenda-todo t]
2206 ["Next TODO set" org-agenda-todo-nextset t]
2207 ["Previous TODO set" org-agenda-todo-previousset t]
2208 ["Add note" org-agenda-add-note t])
2209 ("Archive/Refile/Delete"
2210 ["Archive default" org-agenda-archive-default t]
2211 ["Archive default" org-agenda-archive-default-with-confirmation t]
2212 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2213 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2214 ["Archive subtree" org-agenda-archive t]
2215 "--"
2216 ["Refile" org-agenda-refile t]
2217 "--"
2218 ["Delete subtree" org-agenda-kill t])
2219 ("Bulk action"
2220 ["Mark entry" org-agenda-bulk-mark t]
2221 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2222 ["Unmark entry" org-agenda-bulk-unmark t]
2223 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2224 ["Act on all marked" org-agenda-bulk-action t]
2225 "--"
2226 ("Tags and Properties"
2227 ["Show all Tags" org-agenda-show-tags t]
2228 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2229 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2230 "--"
2231 ["Column View" org-columns t])
2232 ("Deadline/Schedule"
2233 ["Schedule" org-agenda-schedule t]
2234 ["Set Deadline" org-agenda-deadline t]
2235 "--"
2236 ["Mark item" org-agenda-action :active t :keys "k m"]
2237 ["Show mark item" org-agenda-action :active t :keys "k v"]
2238 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2239 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2240 "--"
2241 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2242 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2243 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2244 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2245 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2246 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2247 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2248 ("Clock and Effort"
2249 ["Clock in" org-agenda-clock-in t]
2250 ["Clock out" org-agenda-clock-out t]
2251 ["Clock cancel" org-agenda-clock-cancel t]
2252 ["Goto running clock" org-clock-goto t]
2253 "--"
2254 ["Set Effort" org-agenda-set-effort t]
2255 ["Change clocked effort" org-clock-modify-effort-estimate
2256 (org-clock-is-active)])
2257 ("Priority"
2258 ["Set Priority" org-agenda-priority t]
2259 ["Increase Priority" org-agenda-priority-up t]
2260 ["Decrease Priority" org-agenda-priority-down t]
2261 ["Show Priority" org-agenda-show-priority t])
2262 ("Calendar/Diary"
2263 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2264 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2265 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2266 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2267 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2268 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2269 "--"
2270 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2271 "--"
2272 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2273 "--"
2274 ("MobileOrg"
2275 ["Push Files and Views" org-mobile-push t]
2276 ["Get Captured and Flagged" org-mobile-pull t]
2277 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2278 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2279 "--"
2280 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2281 "--"
2282 ["Quit" org-agenda-quit t]
2283 ["Exit and Release Buffers" org-agenda-exit t]
2286 ;;; Agenda undo
2288 (defvar org-agenda-allow-remote-undo t
2289 "Non-nil means allow remote undo from the agenda buffer.")
2290 (defvar org-agenda-undo-list nil
2291 "List of undoable operations in the agenda since last refresh.")
2292 (defvar org-agenda-undo-has-started-in nil
2293 "Buffers that have already seen `undo-start' in the current undo sequence.")
2294 (defvar org-agenda-pending-undo-list nil
2295 "In a series of undo commands, this is the list of remaining undo items.")
2297 (defun org-agenda-undo ()
2298 "Undo a remote editing step in the agenda.
2299 This undoes changes both in the agenda buffer and in the remote buffer
2300 that have been changed along."
2301 (interactive)
2302 (or org-agenda-allow-remote-undo
2303 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2304 (if (not (eq this-command last-command))
2305 (setq org-agenda-undo-has-started-in nil
2306 org-agenda-pending-undo-list org-agenda-undo-list))
2307 (if (not org-agenda-pending-undo-list)
2308 (error "No further undo information"))
2309 (let* ((entry (pop org-agenda-pending-undo-list))
2310 buf line cmd rembuf)
2311 (setq cmd (pop entry) line (pop entry))
2312 (setq rembuf (nth 2 entry))
2313 (org-with-remote-undo rembuf
2314 (while (bufferp (setq buf (pop entry)))
2315 (if (pop entry)
2316 (with-current-buffer buf
2317 (let ((last-undo-buffer buf)
2318 (inhibit-read-only t))
2319 (unless (memq buf org-agenda-undo-has-started-in)
2320 (push buf org-agenda-undo-has-started-in)
2321 (make-local-variable 'pending-undo-list)
2322 (undo-start))
2323 (while (and pending-undo-list
2324 (listp pending-undo-list)
2325 (not (car pending-undo-list)))
2326 (pop pending-undo-list))
2327 (undo-more 1))))))
2328 (org-goto-line line)
2329 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2331 (defun org-verify-change-for-undo (l1 l2)
2332 "Verify that a real change occurred between the undo lists L1 and L2."
2333 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2334 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2335 (not (eq l1 l2)))
2337 ;;; Agenda dispatch
2339 (defvar org-agenda-restrict nil)
2340 (defvar org-agenda-restrict-begin (make-marker))
2341 (defvar org-agenda-restrict-end (make-marker))
2342 (defvar org-agenda-last-dispatch-buffer nil)
2343 (defvar org-agenda-overriding-restriction nil)
2345 ;;;###autoload
2346 (defun org-agenda (&optional arg keys restriction)
2347 "Dispatch agenda commands to collect entries to the agenda buffer.
2348 Prompts for a command to execute. Any prefix arg will be passed
2349 on to the selected command. The default selections are:
2351 a Call `org-agenda-list' to display the agenda for current day or week.
2352 t Call `org-todo-list' to display the global todo list.
2353 T Call `org-todo-list' to display the global todo list, select only
2354 entries with a specific TODO keyword (the user gets a prompt).
2355 m Call `org-tags-view' to display headlines with tags matching
2356 a condition (the user is prompted for the condition).
2357 M Like `m', but select only TODO entries, no ordinary headlines.
2358 L Create a timeline for the current buffer.
2359 e Export views to associated files.
2360 s Search entries for keywords.
2361 / Multi occur across all agenda files and also files listed
2362 in `org-agenda-text-search-extra-files'.
2363 < Restrict agenda commands to buffer, subtree, or region.
2364 Press several times to get the desired effect.
2365 > Remove a previous restriction.
2366 # List \"stuck\" projects.
2367 ! Configure what \"stuck\" means.
2368 C Configure custom agenda commands.
2370 More commands can be added by configuring the variable
2371 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2372 searches can be pre-defined in this way.
2374 If the current buffer is in Org-mode and visiting a file, you can also
2375 first press `<' once to indicate that the agenda should be temporarily
2376 \(until the next use of \\[org-agenda]) restricted to the current file.
2377 Pressing `<' twice means to restrict to the current subtree or region
2378 \(if active)."
2379 (interactive "P")
2380 (catch 'exit
2381 (let* ((prefix-descriptions nil)
2382 (org-agenda-buffer-name org-agenda-buffer-name)
2383 (org-agenda-window-setup (if (equal (buffer-name)
2384 org-agenda-buffer-name)
2385 'current-window
2386 org-agenda-window-setup))
2387 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2388 (org-agenda-custom-commands
2389 ;; normalize different versions
2390 (delq nil
2391 (mapcar
2392 (lambda (x)
2393 (cond ((stringp (cdr x))
2394 (push x prefix-descriptions)
2395 nil)
2396 ((stringp (nth 1 x)) x)
2397 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2398 (t (cons (car x) (cons "" (cdr x))))))
2399 org-agenda-custom-commands)))
2400 (buf (current-buffer))
2401 (bfn (buffer-file-name (buffer-base-buffer)))
2402 entry key type match lprops ans)
2403 ;; Turn off restriction unless there is an overriding one,
2404 (unless org-agenda-overriding-restriction
2405 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2406 ;; There is a request to keep the file list in place
2407 (put 'org-agenda-files 'org-restrict nil))
2408 (setq org-agenda-restrict nil)
2409 (move-marker org-agenda-restrict-begin nil)
2410 (move-marker org-agenda-restrict-end nil))
2411 ;; Delete old local properties
2412 (put 'org-agenda-redo-command 'org-lprops nil)
2413 ;; Delete previously set last-arguments
2414 (put 'org-agenda-redo-command 'last-args nil)
2415 ;; Remember where this call originated
2416 (setq org-agenda-last-dispatch-buffer (current-buffer))
2417 (unless keys
2418 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2419 keys (car ans)
2420 restriction (cdr ans)))
2421 ;; If we have sticky agenda buffers, set a name for the buffer,
2422 ;; depending on the invoking keys. The user may still set this
2423 ;; as a command option, which will overwrite what we do here.
2424 (if org-agenda-sticky
2425 (setq org-agenda-buffer-name (format "*Org Agenda(%s)*" keys)))
2426 ;; Establish the restriction, if any
2427 (when (and (not org-agenda-overriding-restriction) restriction)
2428 (put 'org-agenda-files 'org-restrict (list bfn))
2429 (cond
2430 ((eq restriction 'region)
2431 (setq org-agenda-restrict t)
2432 (move-marker org-agenda-restrict-begin (region-beginning))
2433 (move-marker org-agenda-restrict-end (region-end)))
2434 ((eq restriction 'subtree)
2435 (save-excursion
2436 (setq org-agenda-restrict t)
2437 (org-back-to-heading t)
2438 (move-marker org-agenda-restrict-begin (point))
2439 (move-marker org-agenda-restrict-end
2440 (progn (org-end-of-subtree t)))))))
2442 ;; For example the todo list should not need it (but does...)
2443 (cond
2444 ((setq entry (assoc keys org-agenda-custom-commands))
2445 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2446 (progn
2447 (setq type (nth 2 entry) match (eval (nth 3 entry))
2448 lprops (nth 4 entry))
2449 (put 'org-agenda-redo-command 'org-lprops lprops)
2450 (cond
2451 ((eq type 'agenda)
2452 (org-let lprops '(org-agenda-list current-prefix-arg)))
2453 ((eq type 'alltodo)
2454 (org-let lprops '(org-todo-list current-prefix-arg)))
2455 ((eq type 'search)
2456 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2457 ((eq type 'stuck)
2458 (org-let lprops '(org-agenda-list-stuck-projects
2459 current-prefix-arg)))
2460 ((eq type 'tags)
2461 (org-let lprops '(org-tags-view current-prefix-arg match)))
2462 ((eq type 'tags-todo)
2463 (org-let lprops '(org-tags-view '(4) match)))
2464 ((eq type 'todo)
2465 (org-let lprops '(org-todo-list match)))
2466 ((eq type 'tags-tree)
2467 (org-check-for-org-mode)
2468 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2469 ((eq type 'todo-tree)
2470 (org-check-for-org-mode)
2471 (org-let lprops
2472 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2473 (regexp-quote match) "\\>"))))
2474 ((eq type 'occur-tree)
2475 (org-check-for-org-mode)
2476 (org-let lprops '(org-occur match)))
2477 ((functionp type)
2478 (org-let lprops '(funcall type match)))
2479 ((fboundp type)
2480 (org-let lprops '(funcall type match)))
2481 (t (error "Invalid custom agenda command type %s" type))))
2482 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2483 ((equal keys "C")
2484 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2485 (customize-variable 'org-agenda-custom-commands))
2486 ((equal keys "a") (call-interactively 'org-agenda-list))
2487 ((equal keys "s") (call-interactively 'org-search-view))
2488 ((equal keys "t") (call-interactively 'org-todo-list))
2489 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2490 ((equal keys "m") (call-interactively 'org-tags-view))
2491 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2492 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2493 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2494 (org-add-hook
2495 'post-command-hook
2496 (lambda ()
2497 (unless (current-message)
2498 (let* ((m (org-agenda-get-any-marker))
2499 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2500 (when note
2501 (message (concat
2502 "FLAGGING-NOTE ([?] for more info): "
2503 (org-add-props
2504 (replace-regexp-in-string
2505 "\\\\n" "//"
2506 (copy-sequence note))
2507 nil 'face 'org-warning)))))))
2508 t t))
2509 ((equal keys "L")
2510 (unless (eq major-mode 'org-mode)
2511 (error "This is not an Org-mode file"))
2512 (unless restriction
2513 (put 'org-agenda-files 'org-restrict (list bfn))
2514 (org-call-with-arg 'org-timeline arg)))
2515 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2516 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2517 ((equal keys "!") (customize-variable 'org-stuck-projects))
2518 (t (error "Invalid agenda key"))))))
2520 (defun org-agenda-append-agenda ()
2521 "Append another agenda view to the current one.
2522 This function allows interactive building of block agendas.
2523 Agenda views are separated by `org-agenda-block-separator'."
2524 (interactive)
2525 (unless (string= (buffer-name) org-agenda-buffer-name)
2526 (error "Can only append from within agenda buffer"))
2527 (let ((org-agenda-multi t))
2528 (org-agenda)
2529 (widen)))
2531 (defun org-agenda-normalize-custom-commands (cmds)
2532 (delq nil
2533 (mapcar
2534 (lambda (x)
2535 (cond ((stringp (cdr x)) nil)
2536 ((stringp (nth 1 x)) x)
2537 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2538 (t (cons (car x) (cons "" (cdr x))))))
2539 cmds)))
2541 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2542 "The user interface for selecting an agenda command."
2543 (catch 'exit
2544 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2545 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2546 (region-p (org-region-active-p))
2547 (custom org-agenda-custom-commands)
2548 (selstring "")
2549 restriction second-time
2550 c entry key type match prefixes rmheader header-end custom1 desc
2551 line lines left right n n1)
2552 (save-window-excursion
2553 (delete-other-windows)
2554 (org-switch-to-buffer-other-window " *Agenda Commands*")
2555 (erase-buffer)
2556 (insert (eval-when-compile
2557 (let ((header
2559 Press key for an agenda command: < Buffer, subtree/region restriction
2560 -------------------------------- > Remove restriction
2561 a Agenda for current week or day e Export agenda views
2562 t List of all TODO entries T Entries with special TODO kwd
2563 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2564 L Timeline for current buffer # List stuck projects (!=configure)
2565 s Search for keywords C Configure custom agenda commands
2566 / Multi-occur ? Find :FLAGGED: entries
2568 (start 0))
2569 (while (string-match
2570 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2571 header start)
2572 (setq start (match-end 0))
2573 (add-text-properties (match-beginning 2) (match-end 2)
2574 '(face bold) header))
2575 header)))
2576 (setq header-end (move-marker (make-marker) (point)))
2577 (while t
2578 (setq custom1 custom)
2579 (when (eq rmheader t)
2580 (org-goto-line 1)
2581 (re-search-forward ":" nil t)
2582 (delete-region (match-end 0) (point-at-eol))
2583 (forward-char 1)
2584 (looking-at "-+")
2585 (delete-region (match-end 0) (point-at-eol))
2586 (move-marker header-end (match-end 0)))
2587 (goto-char header-end)
2588 (delete-region (point) (point-max))
2590 ;; Produce all the lines that describe custom commands and prefixes
2591 (setq lines nil)
2592 (while (setq entry (pop custom1))
2593 (setq key (car entry) desc (nth 1 entry)
2594 type (nth 2 entry)
2595 match (nth 3 entry))
2596 (if (> (length key) 1)
2597 (add-to-list 'prefixes (string-to-char key))
2598 (setq line
2599 (format
2600 "%-4s%-14s"
2601 (org-add-props (copy-sequence key)
2602 '(face bold))
2603 (cond
2604 ((string-match "\\S-" desc) desc)
2605 ((eq type 'agenda) "Agenda for current week or day")
2606 ((eq type 'alltodo) "List of all TODO entries")
2607 ((eq type 'search) "Word search")
2608 ((eq type 'stuck) "List of stuck projects")
2609 ((eq type 'todo) "TODO keyword")
2610 ((eq type 'tags) "Tags query")
2611 ((eq type 'tags-todo) "Tags (TODO)")
2612 ((eq type 'tags-tree) "Tags tree")
2613 ((eq type 'todo-tree) "TODO kwd tree")
2614 ((eq type 'occur-tree) "Occur tree")
2615 ((functionp type) (if (symbolp type)
2616 (symbol-name type)
2617 "Lambda expression"))
2618 (t "???"))))
2619 (if org-agenda-menu-show-matcher
2620 (setq line
2621 (concat line ": "
2622 (cond
2623 ((stringp match)
2624 (setq match (copy-sequence match))
2625 (org-add-props match nil 'face 'org-warning))
2626 (match
2627 (format "set of %d commands" (length match)))
2628 (t ""))))
2629 (if (org-string-nw-p match)
2630 (add-text-properties
2631 0 (length line) (list 'help-echo
2632 (concat "Matcher: "match)) line)))
2633 (push line lines)))
2634 (setq lines (nreverse lines))
2635 (when prefixes
2636 (mapc (lambda (x)
2637 (push
2638 (format "%s %s"
2639 (org-add-props (char-to-string x)
2640 nil 'face 'bold)
2641 (or (cdr (assoc (concat selstring
2642 (char-to-string x))
2643 prefix-descriptions))
2644 "Prefix key"))
2645 lines))
2646 prefixes))
2648 ;; Check if we should display in two columns
2649 (if org-agenda-menu-two-column
2650 (progn
2651 (setq n (length lines)
2652 n1 (+ (/ n 2) (mod n 2))
2653 right (nthcdr n1 lines)
2654 left (copy-sequence lines))
2655 (setcdr (nthcdr (1- n1) left) nil))
2656 (setq left lines right nil))
2657 (while left
2658 (insert "\n" (pop left))
2659 (when right
2660 (if (< (current-column) 40)
2661 (move-to-column 40 t)
2662 (insert " "))
2663 (insert (pop right))))
2665 ;; Make the window the right size
2666 (goto-char (point-min))
2667 (if second-time
2668 (if (not (pos-visible-in-window-p (point-max)))
2669 (org-fit-window-to-buffer))
2670 (setq second-time t)
2671 (org-fit-window-to-buffer))
2673 ;; Ask for selection
2674 (message "Press key for agenda command%s:"
2675 (if (or restrict-ok org-agenda-overriding-restriction)
2676 (if org-agenda-overriding-restriction
2677 " (restriction lock active)"
2678 (if restriction
2679 (format " (restricted to %s)" restriction)
2680 " (unrestricted)"))
2681 ""))
2682 (setq c (read-char-exclusive))
2683 (message "")
2684 (cond
2685 ((assoc (char-to-string c) custom)
2686 (setq selstring (concat selstring (char-to-string c)))
2687 (throw 'exit (cons selstring restriction)))
2688 ((memq c prefixes)
2689 (setq selstring (concat selstring (char-to-string c))
2690 prefixes nil
2691 rmheader (or rmheader t)
2692 custom (delq nil (mapcar
2693 (lambda (x)
2694 (if (or (= (length (car x)) 1)
2695 (/= (string-to-char (car x)) c))
2697 (cons (substring (car x) 1) (cdr x))))
2698 custom))))
2699 ((eq c ?\C-k)
2700 (org-agenda-remove-restriction-lock 'noupdate)
2701 (org-agenda-kill-all-agenda-buffers)
2702 (message "All agenda buffers have been killed")
2703 (ding) (sit-for 2))
2704 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2705 (message "Restriction is only possible in Org-mode buffers")
2706 (ding) (sit-for 1))
2707 ((eq c ?1)
2708 (org-agenda-remove-restriction-lock 'noupdate)
2709 (setq restriction 'buffer))
2710 ((eq c ?0)
2711 (org-agenda-remove-restriction-lock 'noupdate)
2712 (setq restriction (if region-p 'region 'subtree)))
2713 ((eq c ?<)
2714 (org-agenda-remove-restriction-lock 'noupdate)
2715 (setq restriction
2716 (cond
2717 ((eq restriction 'buffer)
2718 (if region-p 'region 'subtree))
2719 ((memq restriction '(subtree region))
2720 nil)
2721 (t 'buffer))))
2722 ((eq c ?>)
2723 (org-agenda-remove-restriction-lock 'noupdate)
2724 (setq restriction nil))
2725 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2726 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2727 ((and (> (length selstring) 0) (eq c ?\d))
2728 (delete-window)
2729 (org-agenda-get-restriction-and-command prefix-descriptions))
2731 ((equal c ?q) (error "Abort"))
2732 (t (error "Invalid key %c" c))))))))
2734 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2735 (defvar org-agenda-last-arguments nil
2736 "The arguments of the previous call to `org-agenda'.")
2737 (defun org-agenda-run-series (name series)
2738 (org-let (nth 1 series) '(org-prepare-agenda name))
2739 ;; We need to reset agenda markers heree, because when constructing a
2740 ;; block agenda, the individual blocks do not do that.
2741 (org-agenda-reset-markers)
2742 (let* ((org-agenda-multi t)
2743 (redo (list 'org-agenda-run-series name (list 'quote series)))
2744 (org-agenda-overriding-arguments
2745 (or org-agenda-overriding-arguments
2746 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2747 (get 'org-agenda-redo-command 'last-args))))
2748 (cmds (car series))
2749 (gprops (nth 1 series))
2750 match ;; The byte compiler incorrectly complains about this. Keep it!
2751 cmd type lprops)
2752 (while (setq cmd (pop cmds))
2753 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2754 (cond
2755 ((eq type 'agenda)
2756 (org-let2 gprops lprops
2757 '(call-interactively 'org-agenda-list)))
2758 ((eq type 'alltodo)
2759 (org-let2 gprops lprops
2760 '(call-interactively 'org-todo-list)))
2761 ((eq type 'search)
2762 (org-let2 gprops lprops
2763 '(org-search-view current-prefix-arg match nil)))
2764 ((eq type 'stuck)
2765 (org-let2 gprops lprops
2766 '(call-interactively 'org-agenda-list-stuck-projects)))
2767 ((eq type 'tags)
2768 (org-let2 gprops lprops
2769 '(org-tags-view current-prefix-arg match)))
2770 ((eq type 'tags-todo)
2771 (org-let2 gprops lprops
2772 '(org-tags-view '(4) match)))
2773 ((eq type 'todo)
2774 (org-let2 gprops lprops
2775 '(org-todo-list match)))
2776 ((fboundp type)
2777 (org-let2 gprops lprops
2778 '(funcall type match)))
2779 (t (error "Invalid type in command series"))))
2780 (widen)
2781 (setq org-agenda-redo-command redo)
2782 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2783 (goto-char (point-min)))
2784 (org-fit-agenda-window)
2785 (org-let (nth 1 series) '(org-finalize-agenda)))
2787 ;;;###autoload
2788 (defmacro org-batch-agenda (cmd-key &rest parameters)
2789 "Run an agenda command in batch mode and send the result to STDOUT.
2790 If CMD-KEY is a string of length 1, it is used as a key in
2791 `org-agenda-custom-commands' and triggers this command. If it is a
2792 longer string it is used as a tags/todo match string.
2793 Parameters are alternating variable names and values that will be bound
2794 before running the agenda command."
2795 (org-eval-in-environment (org-make-parameter-alist parameters)
2796 (if (> (length cmd-key) 2)
2797 (org-tags-view nil cmd-key)
2798 (org-agenda nil cmd-key)))
2799 (set-buffer org-agenda-buffer-name)
2800 (princ (buffer-string)))
2801 (def-edebug-spec org-batch-agenda (form &rest sexp))
2803 (defvar org-agenda-info nil)
2805 ;;;###autoload
2806 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2807 "Run an agenda command in batch mode and send the result to STDOUT.
2808 If CMD-KEY is a string of length 1, it is used as a key in
2809 `org-agenda-custom-commands' and triggers this command. If it is a
2810 longer string it is used as a tags/todo match string.
2811 Parameters are alternating variable names and values that will be bound
2812 before running the agenda command.
2814 The output gives a line for each selected agenda item. Each
2815 item is a list of comma-separated values, like this:
2817 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2819 category The category of the item
2820 head The headline, without TODO kwd, TAGS and PRIORITY
2821 type The type of the agenda entry, can be
2822 todo selected in TODO match
2823 tagsmatch selected in tags match
2824 diary imported from diary
2825 deadline a deadline on given date
2826 scheduled scheduled on given date
2827 timestamp entry has timestamp on given date
2828 closed entry was closed on given date
2829 upcoming-deadline warning about deadline
2830 past-scheduled forwarded scheduled item
2831 block entry has date block including g. date
2832 todo The todo keyword, if any
2833 tags All tags including inherited ones, separated by colons
2834 date The relevant date, like 2007-2-14
2835 time The time, like 15:00-16:50
2836 extra Sting with extra planning info
2837 priority-l The priority letter if any was given
2838 priority-n The computed numerical priority
2839 agenda-day The day in the agenda where this is listed"
2840 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2841 (org-make-parameter-alist parameters))
2842 (if (> (length cmd-key) 2)
2843 (org-tags-view nil cmd-key)
2844 (org-agenda nil cmd-key)))
2845 (set-buffer org-agenda-buffer-name)
2846 (let* ((lines (org-split-string (buffer-string) "\n"))
2847 line)
2848 (while (setq line (pop lines))
2849 (catch 'next
2850 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2851 (setq org-agenda-info
2852 (org-fix-agenda-info (text-properties-at 0 line)))
2853 (princ
2854 (mapconcat 'org-agenda-export-csv-mapper
2855 '(org-category txt type todo tags date time extra
2856 priority-letter priority agenda-day)
2857 ","))
2858 (princ "\n")))))
2859 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2861 (defun org-fix-agenda-info (props)
2862 "Make sure all properties on an agenda item have a canonical form.
2863 This ensures the export commands can easily use it."
2864 (let (tmp re)
2865 (when (setq tmp (plist-get props 'tags))
2866 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2867 (when (setq tmp (plist-get props 'date))
2868 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2869 (let ((calendar-date-display-form '(year "-" month "-" day)))
2870 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2872 (setq tmp (calendar-date-string tmp)))
2873 (setq props (plist-put props 'date tmp)))
2874 (when (setq tmp (plist-get props 'day))
2875 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2876 (let ((calendar-date-display-form '(year "-" month "-" day)))
2877 (setq tmp (calendar-date-string tmp)))
2878 (setq props (plist-put props 'day tmp))
2879 (setq props (plist-put props 'agenda-day tmp)))
2880 (when (setq tmp (plist-get props 'txt))
2881 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2882 (plist-put props 'priority-letter (match-string 1 tmp))
2883 (setq tmp (replace-match "" t t tmp)))
2884 (when (and (setq re (plist-get props 'org-todo-regexp))
2885 (setq re (concat "\\`\\.*" re " ?"))
2886 (string-match re tmp))
2887 (plist-put props 'todo (match-string 1 tmp))
2888 (setq tmp (replace-match "" t t tmp)))
2889 (plist-put props 'txt tmp)))
2890 props)
2892 (defun org-agenda-export-csv-mapper (prop)
2893 (let ((res (plist-get org-agenda-info prop)))
2894 (setq res
2895 (cond
2896 ((not res) "")
2897 ((stringp res) res)
2898 (t (prin1-to-string res))))
2899 (while (string-match "," res)
2900 (setq res (replace-match ";" t t res))) ;
2901 (org-trim res)))
2904 ;;;###autoload
2905 (defun org-store-agenda-views (&rest parameters)
2906 (interactive)
2907 (eval (list 'org-batch-store-agenda-views)))
2909 ;;;###autoload
2910 (defmacro org-batch-store-agenda-views (&rest parameters)
2911 "Run all custom agenda commands that have a file argument."
2912 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2913 (pop-up-frames nil)
2914 (dir default-directory)
2915 (pars (org-make-parameter-alist parameters))
2916 cmd thiscmdkey files opts cmd-or-set)
2917 (save-window-excursion
2918 (while cmds
2919 (setq cmd (pop cmds)
2920 thiscmdkey (car cmd)
2921 cmd-or-set (nth 2 cmd)
2922 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2923 files (nth (if (listp cmd-or-set) 4 5) cmd))
2924 (if (stringp files) (setq files (list files)))
2925 (when files
2926 (org-eval-in-environment (append org-agenda-exporter-settings
2927 opts pars)
2928 (org-agenda nil thiscmdkey))
2929 (set-buffer org-agenda-buffer-name)
2930 (while files
2931 (org-eval-in-environment (append org-agenda-exporter-settings
2932 opts pars)
2933 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2934 (and (get-buffer org-agenda-buffer-name)
2935 (kill-buffer org-agenda-buffer-name)))))))
2936 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2938 (defun org-agenda-mark-header-line (pos)
2939 "Mark the line at POS as an agenda structure header."
2940 (save-excursion
2941 (goto-char pos)
2942 (put-text-property (point-at-bol) (point-at-eol)
2943 'org-agenda-structural-header t)
2944 (when org-agenda-title-append
2945 (put-text-property (point-at-bol) (point-at-eol)
2946 'org-agenda-title-append org-agenda-title-append))))
2948 (defvar org-mobile-creating-agendas)
2949 (defvar org-agenda-write-buffer-name "Agenda View")
2950 (defun org-agenda-write (file &optional open nosettings)
2951 "Write the current buffer (an agenda view) as a file.
2952 Depending on the extension of the file name, plain text (.txt),
2953 HTML (.html or .htm) or Postscript (.ps) is produced.
2954 If the extension is .ics, run icalendar export over all files used
2955 to construct the agenda and limit the export to entries listed in the
2956 agenda now.
2957 With prefix argument OPEN, open the new file immediately.
2958 If NOSETTINGS is given, do not scope the settings of
2959 `org-agenda-exporter-settings' into the export commands. This is used when
2960 the settings have already been scoped and we do not wish to overrule other,
2961 higher priority settings."
2962 (interactive "FWrite agenda to file: \nP")
2963 (if (not (file-writable-p file))
2964 (error "Cannot write agenda to file %s" file))
2965 (org-let (if nosettings nil org-agenda-exporter-settings)
2966 '(save-excursion
2967 (save-window-excursion
2968 (org-agenda-mark-filtered-text)
2969 (let ((bs (copy-sequence (buffer-string))) beg)
2970 (org-agenda-unmark-filtered-text)
2971 (with-temp-buffer
2972 (rename-buffer org-agenda-write-buffer-name t)
2973 (set-buffer-modified-p nil)
2974 (insert bs)
2975 (org-agenda-remove-marked-text 'org-filtered)
2976 (while (setq beg (text-property-any (point-min) (point-max)
2977 'org-filtered t))
2978 (delete-region
2979 beg (or (next-single-property-change beg 'org-filtered)
2980 (point-max))))
2981 (run-hooks 'org-agenda-before-write-hook)
2982 (cond
2983 ((org-bound-and-true-p org-mobile-creating-agendas)
2984 (org-mobile-write-agenda-for-mobile file))
2985 ((string-match "\\.html?\\'" file)
2986 (require 'htmlize)
2987 (set-buffer (htmlize-buffer (current-buffer)))
2989 (when (and org-agenda-export-html-style
2990 (string-match "<style>" org-agenda-export-html-style))
2991 ;; replace <style> section with org-agenda-export-html-style
2992 (goto-char (point-min))
2993 (kill-region (- (search-forward "<style") 6)
2994 (search-forward "</style>"))
2995 (insert org-agenda-export-html-style))
2996 (write-file file)
2997 (kill-buffer (current-buffer))
2998 (message "HTML written to %s" file))
2999 ((string-match "\\.ps\\'" file)
3000 (require 'ps-print)
3001 (ps-print-buffer-with-faces file)
3002 (message "Postscript written to %s" file))
3003 ((string-match "\\.pdf\\'" file)
3004 (require 'ps-print)
3005 (ps-print-buffer-with-faces
3006 (concat (file-name-sans-extension file) ".ps"))
3007 (call-process "ps2pdf" nil nil nil
3008 (expand-file-name
3009 (concat (file-name-sans-extension file) ".ps"))
3010 (expand-file-name file))
3011 (delete-file (concat (file-name-sans-extension file) ".ps"))
3012 (message "PDF written to %s" file))
3013 ((string-match "\\.ics\\'" file)
3014 (require 'org-icalendar)
3015 (let ((org-agenda-marker-table
3016 (org-create-marker-find-array
3017 (org-agenda-collect-markers)))
3018 (org-icalendar-verify-function 'org-check-agenda-marker-table)
3019 (org-combined-agenda-icalendar-file file))
3020 (apply 'org-export-icalendar 'combine
3021 (org-agenda-files nil 'ifmode))))
3023 (let ((bs (buffer-string)))
3024 (find-file file)
3025 (erase-buffer)
3026 (insert bs)
3027 (save-buffer 0)
3028 (kill-buffer (current-buffer))
3029 (message "Plain text written to %s" file))))))))
3030 (set-buffer org-agenda-buffer-name))
3031 (when open (org-open-file file)))
3033 (defvar org-agenda-tag-filter-overlays nil)
3034 (defvar org-agenda-cat-filter-overlays nil)
3036 (defun org-agenda-mark-filtered-text ()
3037 "Mark all text hidden by filtering with a text property."
3038 (let ((inhibit-read-only t))
3039 (mapc
3040 (lambda (o)
3041 (when (equal (overlay-buffer o) (current-buffer))
3042 (put-text-property
3043 (overlay-start o) (overlay-end o)
3044 'org-filtered t)))
3045 (append org-agenda-tag-filter-overlays
3046 org-agenda-cat-filter-overlays))))
3048 (defun org-agenda-unmark-filtered-text ()
3049 "Remove the filtering text property."
3050 (let ((inhibit-read-only t))
3051 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3053 (defun org-agenda-remove-marked-text (property &optional value)
3054 "Delete all text marked with VALUE of PROPERTY.
3055 VALUE defaults to t."
3056 (let (beg)
3057 (setq value (or value t))
3058 (while (setq beg (text-property-any (point-min) (point-max)
3059 property value))
3060 (delete-region
3061 beg (or (next-single-property-change beg 'org-filtered)
3062 (point-max))))))
3064 (defun org-agenda-add-entry-text ()
3065 "Add entry text to agenda lines.
3066 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3067 entry text following headings shown in the agenda.
3068 Drawers will be excluded, also the line with scheduling/deadline info."
3069 (when (and (> org-agenda-add-entry-text-maxlines 0)
3070 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3071 (let (m txt)
3072 (goto-char (point-min))
3073 (while (not (eobp))
3074 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3075 (beginning-of-line 2)
3076 (setq txt (org-agenda-get-some-entry-text
3077 m org-agenda-add-entry-text-maxlines " > "))
3078 (end-of-line 1)
3079 (if (string-match "\\S-" txt)
3080 (insert "\n" txt)
3081 (or (eobp) (forward-char 1))))))))
3083 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3084 &rest keep)
3085 "Extract entry text from MARKER, at most N-LINES lines.
3086 This will ignore drawers etc, just get the text.
3087 If INDENT is given, prefix every line with this string. If KEEP is
3088 given, it is a list of symbols, defining stuff that should not be
3089 removed from the entry content. Currently only `planning' is allowed here."
3090 (let (txt drawer-re kwd-time-re ind)
3091 (save-excursion
3092 (with-current-buffer (marker-buffer marker)
3093 (if (not (eq major-mode 'org-mode))
3094 (setq txt "")
3095 (save-excursion
3096 (save-restriction
3097 (widen)
3098 (goto-char marker)
3099 (end-of-line 1)
3100 (setq txt (buffer-substring
3101 (min (1+ (point)) (point-max))
3102 (progn (outline-next-heading) (point)))
3103 drawer-re org-drawer-regexp
3104 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3105 ".*\n?"))
3106 (with-temp-buffer
3107 (insert txt)
3108 (when org-agenda-add-entry-text-descriptive-links
3109 (goto-char (point-min))
3110 (while (org-activate-bracket-links (point-max))
3111 (add-text-properties (match-beginning 0) (match-end 0)
3112 '(face org-link))))
3113 (goto-char (point-min))
3114 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3115 (set-text-properties (match-beginning 0) (match-end 0)
3116 nil))
3117 (goto-char (point-min))
3118 (while (re-search-forward drawer-re nil t)
3119 (delete-region
3120 (match-beginning 0)
3121 (progn (re-search-forward
3122 "^[ \t]*:END:.*\n?" nil 'move)
3123 (point))))
3124 (unless (member 'planning keep)
3125 (goto-char (point-min))
3126 (while (re-search-forward kwd-time-re nil t)
3127 (replace-match "")))
3128 (goto-char (point-min))
3129 (when org-agenda-entry-text-exclude-regexps
3130 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3131 (while (setq re (pop re-list))
3132 (goto-char (point-min))
3133 (while (re-search-forward re nil t)
3134 (replace-match "")))))
3135 (goto-char (point-max))
3136 (skip-chars-backward " \t\n")
3137 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3139 ;; find and remove min common indentation
3140 (goto-char (point-min))
3141 (untabify (point-min) (point-max))
3142 (setq ind (org-get-indentation))
3143 (while (not (eobp))
3144 (unless (looking-at "[ \t]*$")
3145 (setq ind (min ind (org-get-indentation))))
3146 (beginning-of-line 2))
3147 (goto-char (point-min))
3148 (while (not (eobp))
3149 (unless (looking-at "[ \t]*$")
3150 (move-to-column ind)
3151 (delete-region (point-at-bol) (point)))
3152 (beginning-of-line 2))
3154 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3156 (goto-char (point-min))
3157 (when indent
3158 (while (and (not (eobp)) (re-search-forward "^" nil t))
3159 (replace-match indent t t)))
3160 (goto-char (point-min))
3161 (while (looking-at "[ \t]*\n") (replace-match ""))
3162 (goto-char (point-max))
3163 (when (> (org-current-line)
3164 n-lines)
3165 (org-goto-line (1+ n-lines))
3166 (backward-char 1))
3167 (setq txt (buffer-substring (point-min) (point)))))))))
3168 txt))
3170 (defun org-agenda-collect-markers ()
3171 "Collect the markers pointing to entries in the agenda buffer."
3172 (let (m markers)
3173 (save-excursion
3174 (goto-char (point-min))
3175 (while (not (eobp))
3176 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3177 (org-get-at-bol 'org-marker)))
3178 (push m markers))
3179 (beginning-of-line 2)))
3180 (nreverse markers)))
3182 (defun org-create-marker-find-array (marker-list)
3183 "Create a alist of files names with all marker positions in that file."
3184 (let (f tbl m a p)
3185 (while (setq m (pop marker-list))
3186 (setq p (marker-position m)
3187 f (buffer-file-name (or (buffer-base-buffer
3188 (marker-buffer m))
3189 (marker-buffer m))))
3190 (if (setq a (assoc f tbl))
3191 (push (marker-position m) (cdr a))
3192 (push (list f p) tbl)))
3193 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3194 tbl)))
3196 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3197 (defun org-check-agenda-marker-table ()
3198 "Check of the current entry is on the marker list."
3199 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3201 (and (setq a (assoc file org-agenda-marker-table))
3202 (save-match-data
3203 (save-excursion
3204 (org-back-to-heading t)
3205 (member (point) (cdr a)))))))
3207 (defun org-check-for-org-mode ()
3208 "Make sure current buffer is in org-mode. Error if not."
3209 (or (eq major-mode 'org-mode)
3210 (error "Cannot execute org-mode agenda command on buffer in %s"
3211 major-mode)))
3213 (defun org-fit-agenda-window ()
3214 "Fit the window to the buffer size."
3215 (and (memq org-agenda-window-setup '(reorganize-frame))
3216 (fboundp 'fit-window-to-buffer)
3217 (org-fit-window-to-buffer
3219 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3220 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3222 ;;; Agenda prepare and finalize
3224 (defvar org-agenda-multi nil) ; dynamically scoped
3225 (defvar org-agenda-buffer-name "*Org Agenda*")
3226 (defvar org-pre-agenda-window-conf nil)
3227 (defvar org-agenda-columns-active nil)
3228 (defvar org-agenda-name nil)
3229 (defvar org-agenda-tag-filter nil)
3230 (defvar org-agenda-category-filter nil)
3231 (defvar org-agenda-tag-filter-while-redo nil)
3232 (defvar org-agenda-tag-filter-preset nil
3233 "A preset of the tags filter used for secondary agenda filtering.
3234 This must be a list of strings, each string must be a single tag preceded
3235 by \"+\" or \"-\".
3236 This variable should not be set directly, but agenda custom commands can
3237 bind it in the options section. The preset filter is a global property of
3238 the entire agenda view. In a block agenda, it will not work reliably to
3239 define a filter for one of the individual blocks. You need to set it in
3240 the global options and expect it to be applied to the entire view.")
3242 (defvar org-agenda-category-filter-preset nil
3243 "A preset of the category filter used for secondary agenda filtering.
3244 This must be a list of strings, each string must be a single category
3245 preceded by \"+\" or \"-\".
3246 This variable should not be set directly, but agenda custom commands can
3247 bind it in the options section. The preset filter is a global property of
3248 the entire agenda view. In a block agenda, it will not work reliably to
3249 define a filter for one of the individual blocks. You need to set it in
3250 the global options and expect it to be applied to the entire view.")
3253 (defun org-agenda-use-sticky-p ()
3254 "Return non-NIL if existing agenda buffer named
3255 `org-agenda-buffer-name' exists, and should be shown instead of
3256 generating a new one"
3257 (and
3258 ;; turned off by user
3259 org-agenda-sticky
3260 ;; For multi-agenda buffer already exists
3261 (not org-agenda-multi)
3262 ;; buffer found
3263 (get-buffer org-agenda-buffer-name)
3264 ;; C-u parameter is same as last call
3265 (with-current-buffer (get-buffer org-agenda-buffer-name)
3266 (and
3267 (equal current-prefix-arg
3268 org-agenda-last-prefix-arg)
3269 ;; In case user turned stickiness on, while having existing
3270 ;; Agenda buffer active, don't reuse that buffer, because it
3271 ;; does not have org variables local
3272 org-agenda-this-buffer-is-sticky))))
3274 (defun org-prepare-agenda-window (abuf)
3275 "Setup agenda buffer in the window"
3276 (let* ((awin (get-buffer-window abuf)))
3277 (cond
3278 ((equal (current-buffer) abuf) nil)
3279 (awin (select-window awin))
3280 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3281 ((equal org-agenda-window-setup 'current-window)
3282 (org-pop-to-buffer-same-window abuf))
3283 ((equal org-agenda-window-setup 'other-window)
3284 (org-switch-to-buffer-other-window abuf))
3285 ((equal org-agenda-window-setup 'other-frame)
3286 (switch-to-buffer-other-frame abuf))
3287 ((equal org-agenda-window-setup 'reorganize-frame)
3288 (delete-other-windows)
3289 (org-switch-to-buffer-other-window abuf)))
3290 ;; additional test in case agenda is invoked from within agenda
3291 ;; buffer via elisp link
3292 (unless (equal (current-buffer) abuf)
3293 (org-pop-to-buffer-same-window abuf))))
3295 (defun org-prepare-agenda (&optional name)
3296 (if (org-agenda-use-sticky-p)
3297 (progn
3298 ;; Popup existing buffer
3299 (org-prepare-agenda-window (get-buffer org-agenda-buffer-name))
3300 (message "Sticky Agenda buffer, use `r' to refresh")
3301 (throw 'exit nil))
3302 (setq org-todo-keywords-for-agenda nil)
3303 (setq org-done-keywords-for-agenda nil)
3304 (setq org-drawers-for-agenda nil)
3305 (unless org-agenda-persistent-filter
3306 (setq org-agenda-tag-filter nil
3307 org-agenda-category-filter nil))
3308 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3309 (put 'org-agenda-category-filter :preset-filter
3310 org-agenda-category-filter-preset)
3311 (if org-agenda-multi
3312 (progn
3313 (setq buffer-read-only nil)
3314 (goto-char (point-max))
3315 (unless (or (bobp) org-agenda-compact-blocks
3316 (not org-agenda-block-separator))
3317 (insert "\n"
3318 (if (stringp org-agenda-block-separator)
3319 org-agenda-block-separator
3320 (make-string (window-width) org-agenda-block-separator))
3321 "\n"))
3322 (narrow-to-region (point) (point-max)))
3324 ;; any org variables need to be set after being in agenda buffer
3325 ;; since they are now buffer local
3326 (org-prepare-agenda-window (get-buffer-create org-agenda-buffer-name))
3327 (setq buffer-read-only nil)
3328 (org-agenda-reset-markers)
3329 (let ((inhibit-read-only t)) (erase-buffer))
3330 (org-agenda-mode)
3331 (setq org-agenda-buffer (current-buffer))
3332 (setq org-agenda-contributing-files nil)
3333 (setq org-agenda-columns-active nil)
3334 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3335 (setq org-todo-keywords-for-agenda
3336 (org-uniquify org-todo-keywords-for-agenda))
3337 (setq org-done-keywords-for-agenda
3338 (org-uniquify org-done-keywords-for-agenda))
3339 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3340 (setq org-agenda-last-prefix-arg current-prefix-arg)
3341 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3342 (and name (not org-agenda-name)
3343 (org-set-local 'org-agenda-name name)))
3344 (setq buffer-read-only nil)))
3346 (defun org-finalize-agenda ()
3347 "Finishing touch for the agenda buffer, called just before displaying it."
3348 (unless org-agenda-multi
3349 (save-excursion
3350 (let ((inhibit-read-only t))
3351 (goto-char (point-min))
3352 (while (org-activate-bracket-links (point-max))
3353 (add-text-properties (match-beginning 0) (match-end 0)
3354 '(face org-link)))
3355 (org-agenda-align-tags)
3356 (unless org-agenda-with-colors
3357 (remove-text-properties (point-min) (point-max) '(face nil))))
3358 (if (and (boundp 'org-agenda-overriding-columns-format)
3359 org-agenda-overriding-columns-format)
3360 (org-set-local 'org-agenda-overriding-columns-format
3361 org-agenda-overriding-columns-format))
3362 (if (and (boundp 'org-agenda-view-columns-initially)
3363 org-agenda-view-columns-initially)
3364 (org-agenda-columns))
3365 (when org-agenda-fontify-priorities
3366 (org-agenda-fontify-priorities))
3367 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3368 (org-agenda-dim-blocked-tasks))
3369 (org-agenda-mark-clocking-task)
3370 (when org-agenda-entry-text-mode
3371 (org-agenda-entry-text-hide)
3372 (org-agenda-entry-text-show))
3373 (if (functionp 'org-habit-insert-consistency-graphs)
3374 (org-habit-insert-consistency-graphs))
3375 (run-hooks 'org-finalize-agenda-hook)
3376 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3377 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3378 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3379 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3380 (org-agenda-filter-apply org-agenda-category-filter 'category))
3381 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3384 (defun org-agenda-mark-clocking-task ()
3385 "Mark the current clock entry in the agenda if it is present."
3386 (mapc (lambda (o)
3387 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3388 (delete-overlay o)))
3389 (overlays-in (point-min) (point-max)))
3390 (when (marker-buffer org-clock-hd-marker)
3391 (save-excursion
3392 (goto-char (point-min))
3393 (let (s ov)
3394 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3395 (goto-char s)
3396 (when (equal (org-get-at-bol 'org-hd-marker)
3397 org-clock-hd-marker)
3398 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3399 (overlay-put ov 'type 'org-agenda-clocking)
3400 (overlay-put ov 'face 'org-agenda-clocking)
3401 (overlay-put ov 'help-echo
3402 "The clock is running in this item")))))))
3404 (defun org-agenda-fontify-priorities ()
3405 "Make highest priority lines bold, and lowest italic."
3406 (interactive)
3407 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3408 (delete-overlay o)))
3409 (overlays-in (point-min) (point-max)))
3410 (save-excursion
3411 (let ((inhibit-read-only t)
3412 b e p ov h l)
3413 (goto-char (point-min))
3414 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3415 (setq h (or (get-char-property (point) 'org-highest-priority)
3416 org-highest-priority)
3417 l (or (get-char-property (point) 'org-lowest-priority)
3418 org-lowest-priority)
3419 p (string-to-char (match-string 1))
3420 b (match-beginning 0)
3421 e (if (eq org-agenda-fontify-priorities 'cookies)
3422 (match-end 0)
3423 (point-at-eol))
3424 ov (make-overlay b e))
3425 (overlay-put
3426 ov 'face
3427 (cond ((org-face-from-face-or-color
3428 'priority nil
3429 (cdr (assoc p org-priority-faces))))
3430 ((and (listp org-agenda-fontify-priorities)
3431 (org-face-from-face-or-color
3432 'priority nil
3433 (cdr (assoc p org-agenda-fontify-priorities)))))
3434 ((equal p l) 'italic)
3435 ((equal p h) 'bold)))
3436 (overlay-put ov 'org-type 'org-priority)))))
3438 (defun org-agenda-dim-blocked-tasks ()
3439 "Dim currently blocked TODO's in the agenda display."
3440 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3441 (delete-overlay o)))
3442 (overlays-in (point-min) (point-max)))
3443 (save-excursion
3444 (let ((inhibit-read-only t)
3445 (org-depend-tag-blocked nil)
3446 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3447 org-blocked-by-checkboxes
3448 invis1 b e p ov h l)
3449 (goto-char (point-min))
3450 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3451 (and pos (goto-char (1+ pos))))
3452 (setq org-blocked-by-checkboxes nil invis1 invis)
3453 (let ((marker (org-get-at-bol 'org-hd-marker)))
3454 (when (and marker
3455 (with-current-buffer (marker-buffer marker)
3456 (save-excursion (goto-char marker)
3457 (org-entry-blocked-p))))
3458 (if org-blocked-by-checkboxes (setq invis1 nil))
3459 (setq b (if invis1
3460 (max (point-min) (1- (point-at-bol)))
3461 (point-at-bol))
3462 e (point-at-eol)
3463 ov (make-overlay b e))
3464 (if invis1
3465 (overlay-put ov 'invisible t)
3466 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3467 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3469 (defvar org-agenda-skip-function nil
3470 "Function to be called at each match during agenda construction.
3471 If this function returns nil, the current match should not be skipped.
3472 Otherwise, the function must return a position from where the search
3473 should be continued.
3474 This may also be a Lisp form, it will be evaluated.
3475 Never set this variable using `setq' or so, because then it will apply
3476 to all future agenda commands. If you do want a global skipping condition,
3477 use the option `org-agenda-skip-function-global' instead.
3478 The correct usage for `org-agenda-skip-function' is to bind it with
3479 `let' to scope it dynamically into the agenda-constructing command.
3480 A good way to set it is through options in `org-agenda-custom-commands'.")
3482 (defun org-agenda-skip ()
3483 "Throw to `:skip' in places that should be skipped.
3484 Also moves point to the end of the skipped region, so that search can
3485 continue from there."
3486 (let ((p (point-at-bol)) to)
3487 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3488 (get-text-property p :org-archived)
3489 (org-end-of-subtree t)
3490 (throw :skip t))
3491 (and org-agenda-skip-comment-trees
3492 (get-text-property p :org-comment)
3493 (org-end-of-subtree t)
3494 (throw :skip t))
3495 (if (equal (char-after p) ?#) (throw :skip t))
3496 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3497 (org-agenda-skip-eval org-agenda-skip-function)))
3498 (goto-char to)
3499 (throw :skip t))))
3501 (defun org-agenda-skip-eval (form)
3502 "If FORM is a function or a list, call (or eval) is and return result.
3503 `save-excursion' and `save-match-data' are wrapped around the call, so point
3504 and match data are returned to the previous state no matter what these
3505 functions do."
3506 (let (fp)
3507 (and form
3508 (or (setq fp (functionp form))
3509 (consp form))
3510 (save-excursion
3511 (save-match-data
3512 (if fp
3513 (funcall form)
3514 (eval form)))))))
3516 (defvar org-agenda-markers nil
3517 "List of all currently active markers created by `org-agenda'.")
3518 (defvar org-agenda-last-marker-time (org-float-time)
3519 "Creation time of the last agenda marker.")
3521 (defun org-agenda-new-marker (&optional pos)
3522 "Return a new agenda marker.
3523 Org-mode keeps a list of these markers and resets them when they are
3524 no longer in use."
3525 (let ((m (copy-marker (or pos (point)))))
3526 (setq org-agenda-last-marker-time (org-float-time))
3527 (with-current-buffer org-agenda-buffer
3528 (push m org-agenda-markers))
3531 (defun org-agenda-reset-markers ()
3532 "Reset markers created by `org-agenda'."
3533 (while org-agenda-markers
3534 (move-marker (pop org-agenda-markers) nil)))
3536 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3537 "Save relative positions of markers in region.
3538 This check for agenda markers in all agenda buffers currently active."
3539 (dolist (buf (buffer-list))
3540 (with-current-buffer buf
3541 (when (eq major-mode 'org-agenda-mode)
3542 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3543 org-agenda-markers)))))
3545 ;;; Entry text mode
3547 (defun org-agenda-entry-text-show-here ()
3548 "Add some text from the entry as context to the current line."
3549 (let (m txt o)
3550 (setq m (org-get-at-bol 'org-hd-marker))
3551 (unless (marker-buffer m)
3552 (error "No marker points to an entry here"))
3553 (setq txt (concat "\n" (org-no-properties
3554 (org-agenda-get-some-entry-text
3555 m org-agenda-entry-text-maxlines " > "))))
3556 (when (string-match "\\S-" txt)
3557 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3558 (overlay-put o 'evaporate t)
3559 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3560 (overlay-put o 'after-string txt))))
3562 (defun org-agenda-entry-text-show ()
3563 "Add entry context for all agenda lines."
3564 (interactive)
3565 (save-excursion
3566 (goto-char (point-max))
3567 (beginning-of-line 1)
3568 (while (not (bobp))
3569 (when (org-get-at-bol 'org-hd-marker)
3570 (org-agenda-entry-text-show-here))
3571 (beginning-of-line 0))))
3573 (defun org-agenda-entry-text-hide ()
3574 "Remove any shown entry context."
3575 (delq nil
3576 (mapcar (lambda (o)
3577 (if (eq (overlay-get o 'org-overlay-type)
3578 'agenda-entry-content)
3579 (progn (delete-overlay o) t)))
3580 (overlays-in (point-min) (point-max)))))
3582 (defun org-agenda-get-day-face (date)
3583 "Return the face DATE should be displayed with."
3584 (or (and (functionp org-agenda-day-face-function)
3585 (funcall org-agenda-day-face-function date))
3586 (cond ((org-agenda-todayp date)
3587 'org-agenda-date-today)
3588 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3589 'org-agenda-date-weekend)
3590 (t 'org-agenda-date))))
3592 ;;; Agenda timeline
3594 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3596 (defun org-timeline (&optional dotodo)
3597 "Show a time-sorted view of the entries in the current org file.
3598 Only entries with a time stamp of today or later will be listed. With
3599 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3600 under the current date.
3601 If the buffer contains an active region, only check the region for
3602 dates."
3603 (interactive "P")
3604 (let* ((dopast t)
3605 (doclosed org-agenda-show-log)
3606 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3607 (current-buffer))))
3608 (date (calendar-current-date))
3609 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3610 (end (if (org-region-active-p) (region-end) (point-max)))
3611 (day-numbers (org-get-all-dates beg end 'no-ranges
3612 t doclosed ; always include today
3613 org-timeline-show-empty-dates))
3614 (org-deadline-warning-days 0)
3615 (org-agenda-only-exact-dates t)
3616 (today (org-today))
3617 (past t)
3618 args
3619 s e rtn d emptyp)
3620 (setq org-agenda-redo-command
3621 (list 'progn
3622 (list 'org-switch-to-buffer-other-window (current-buffer))
3623 (list 'org-timeline (list 'quote dotodo))))
3624 (if (not dopast)
3625 ;; Remove past dates from the list of dates.
3626 (setq day-numbers (delq nil (mapcar (lambda(x)
3627 (if (>= x today) x nil))
3628 day-numbers))))
3629 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3630 (org-compile-prefix-format 'timeline)
3631 (org-set-sorting-strategy 'timeline)
3632 (if doclosed (push :closed args))
3633 (push :timestamp args)
3634 (push :deadline args)
3635 (push :scheduled args)
3636 (push :sexp args)
3637 (if dotodo (push :todo args))
3638 (insert "Timeline of file " entry "\n")
3639 (add-text-properties (point-min) (point)
3640 (list 'face 'org-agenda-structure))
3641 (org-agenda-mark-header-line (point-min))
3642 (while (setq d (pop day-numbers))
3643 (if (and (listp d) (eq (car d) :omitted))
3644 (progn
3645 (setq s (point))
3646 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3647 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3648 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3649 (if (and (>= d today)
3650 dopast
3651 past)
3652 (progn
3653 (setq past nil)
3654 (insert (make-string 79 ?-) "\n")))
3655 (setq date (calendar-gregorian-from-absolute d))
3656 (setq s (point))
3657 (setq rtn (and (not emptyp)
3658 (apply 'org-agenda-get-day-entries entry
3659 date args)))
3660 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3661 (progn
3662 (insert
3663 (if (stringp org-agenda-format-date)
3664 (format-time-string org-agenda-format-date
3665 (org-time-from-absolute date))
3666 (funcall org-agenda-format-date date))
3667 "\n")
3668 (put-text-property s (1- (point)) 'face
3669 (org-agenda-get-day-face date))
3670 (put-text-property s (1- (point)) 'org-date-line t)
3671 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3672 (if (equal d today)
3673 (put-text-property s (1- (point)) 'org-today t))
3674 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3675 (put-text-property s (1- (point)) 'day d)))))
3676 (goto-char (point-min))
3677 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3678 (point-min)))
3679 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3680 (org-finalize-agenda)
3681 (setq buffer-read-only t)))
3683 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3684 "Return a list of all relevant day numbers from BEG to END buffer positions.
3685 If NO-RANGES is non-nil, include only the start and end dates of a range,
3686 not every single day in the range. If FORCE-TODAY is non-nil, make
3687 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3688 inactive time stamps (those in square brackets) are included.
3689 When EMPTY is non-nil, also include days without any entries."
3690 (let ((re (concat
3691 (if pre-re pre-re "")
3692 (if inactive org-ts-regexp-both org-ts-regexp)))
3693 dates dates1 date day day1 day2 ts1 ts2 pos)
3694 (if force-today
3695 (setq dates (list (org-today))))
3696 (save-excursion
3697 (goto-char beg)
3698 (while (re-search-forward re end t)
3699 (setq day (time-to-days (org-time-string-to-time
3700 (substring (match-string 1) 0 10)
3701 (current-buffer) (match-beginning 0))))
3702 (or (memq day dates) (push day dates)))
3703 (unless no-ranges
3704 (goto-char beg)
3705 (while (re-search-forward org-tr-regexp end t)
3706 (setq pos (match-beginning 0))
3707 (setq ts1 (substring (match-string 1) 0 10)
3708 ts2 (substring (match-string 2) 0 10)
3709 day1 (time-to-days (org-time-string-to-time
3710 ts1 (current-buffer) pos))
3711 day2 (time-to-days (org-time-string-to-time
3712 ts2 (current-buffer) pos)))
3713 (while (< (setq day1 (1+ day1)) day2)
3714 (or (memq day1 dates) (push day1 dates)))))
3715 (setq dates (sort dates '<))
3716 (when empty
3717 (while (setq day (pop dates))
3718 (setq day2 (car dates))
3719 (push day dates1)
3720 (when (and day2 empty)
3721 (if (or (eq empty t)
3722 (and (numberp empty) (<= (- day2 day) empty)))
3723 (while (< (setq day (1+ day)) day2)
3724 (push (list day) dates1))
3725 (push (cons :omitted (- day2 day)) dates1))))
3726 (setq dates (nreverse dates1)))
3727 dates)))
3729 ;;; Agenda Daily/Weekly
3731 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3732 "Start day for the agenda view.
3733 Custom commands can set this variable in the options section.")
3734 (defvar org-starting-day nil) ; local variable in the agenda buffer
3735 (defvar org-agenda-current-span nil
3736 "The current span used in the agenda view.") ; local variable in the agenda buffer
3737 (defvar org-arg-loc nil) ; local variable
3739 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3740 "List of types searched for when creating the daily/weekly agenda.
3741 This variable is a list of symbols that controls the types of
3742 items that appear in the daily/weekly agenda. Allowed symbols in this
3743 list are are
3745 :timestamp List items containing a date stamp or date range matching
3746 the selected date. This includes sexp entries in
3747 angular brackets.
3749 :sexp List entries resulting from plain diary-like sexps.
3751 :deadline List deadline due on that date. When the date is today,
3752 also list any deadlines past due, or due within
3753 `org-deadline-warning-days'. `:deadline' must appear before
3754 `:scheduled' if the setting of
3755 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3756 any effect.
3758 :scheduled List all items which are scheduled for the given date.
3759 The diary for *today* also contains items which were
3760 scheduled earlier and are not yet marked DONE.
3762 By default, all four types are turned on.
3764 Never set this variable globally using `setq', because then it
3765 will apply to all future agenda commands. Instead, bind it with
3766 `let' to scope it dynamically into the agenda-constructing
3767 command. A good way to set it is through options in
3768 `org-agenda-custom-commands'. For a more flexible (though
3769 somewhat less efficient) way of determining what is included in
3770 the daily/weekly agenda, see `org-agenda-skip-function'.")
3772 ;;;###autoload
3773 (defun org-agenda-list (&optional arg start-day span)
3774 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3775 The view will be for the current day or week, but from the overview buffer
3776 you will be able to go to other days/weeks.
3778 With a numeric prefix argument in an interactive call, the agenda will
3779 span ARG days. Lisp programs should instead specify SPAN to change
3780 the number of days. SPAN defaults to `org-agenda-span'.
3782 START-DAY defaults to TODAY, or to the most recent match for the weekday
3783 given in `org-agenda-start-on-weekday'."
3784 (interactive "P")
3785 (if (and (integerp arg) (> arg 0))
3786 (setq span arg arg nil))
3787 (org-prepare-agenda "Day/Week")
3788 (setq start-day (or start-day org-agenda-start-day))
3789 (if org-agenda-overriding-arguments
3790 (setq arg (car org-agenda-overriding-arguments)
3791 start-day (nth 1 org-agenda-overriding-arguments)
3792 span (nth 2 org-agenda-overriding-arguments)))
3793 (if (stringp start-day)
3794 ;; Convert to an absolute day number
3795 (setq start-day (time-to-days (org-read-date nil t start-day))))
3796 (setq org-agenda-last-arguments (list arg start-day span))
3797 (org-compile-prefix-format 'agenda)
3798 (org-set-sorting-strategy 'agenda)
3799 (let* ((span (org-agenda-ndays-to-span
3800 (or span org-agenda-ndays org-agenda-span)))
3801 (today (org-today))
3802 (sd (or start-day today))
3803 (ndays (org-agenda-span-to-ndays span sd))
3804 (org-agenda-start-on-weekday
3805 (if (eq ndays 7)
3806 org-agenda-start-on-weekday))
3807 (thefiles (org-agenda-files nil 'ifmode))
3808 (files thefiles)
3809 (start (if (or (null org-agenda-start-on-weekday)
3810 (< ndays 7))
3812 (let* ((nt (calendar-day-of-week
3813 (calendar-gregorian-from-absolute sd)))
3814 (n1 org-agenda-start-on-weekday)
3815 (d (- nt n1)))
3816 (- sd (+ (if (< d 0) 7 0) d)))))
3817 (day-numbers (list start))
3818 (day-cnt 0)
3819 (inhibit-redisplay (not debug-on-error))
3820 s e rtn rtnall file date d start-pos end-pos todayp
3821 clocktable-start clocktable-end filter)
3822 (setq org-agenda-redo-command
3823 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3824 (dotimes (n (1- ndays))
3825 (push (1+ (car day-numbers)) day-numbers))
3826 (setq day-numbers (nreverse day-numbers))
3827 (setq clocktable-start (car day-numbers)
3828 clocktable-end (1+ (or (org-last day-numbers) 0)))
3829 (org-set-local 'org-starting-day (car day-numbers))
3830 (org-set-local 'org-arg-loc arg)
3831 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3832 (unless org-agenda-compact-blocks
3833 (let* ((d1 (car day-numbers))
3834 (d2 (org-last day-numbers))
3835 (w1 (org-days-to-iso-week d1))
3836 (w2 (org-days-to-iso-week d2)))
3837 (setq s (point))
3838 (if org-agenda-overriding-header
3839 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3840 nil 'face 'org-agenda-structure) "\n")
3841 (insert (org-agenda-span-name span)
3842 "-agenda"
3843 (if (< (- d2 d1) 350)
3844 (if (= w1 w2)
3845 (format " (W%02d)" w1)
3846 (format " (W%02d-W%02d)" w1 w2))
3848 ":\n")))
3849 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3850 'org-date-line t))
3851 (org-agenda-mark-header-line s))
3852 (while (setq d (pop day-numbers))
3853 (setq date (calendar-gregorian-from-absolute d)
3854 s (point))
3855 (if (or (setq todayp (= d today))
3856 (and (not start-pos) (= d sd)))
3857 (setq start-pos (point))
3858 (if (and start-pos (not end-pos))
3859 (setq end-pos (point))))
3860 (setq files thefiles
3861 rtnall nil)
3862 (while (setq file (pop files))
3863 (catch 'nextfile
3864 (org-check-agenda-file file)
3865 (let ((org-agenda-entry-types org-agenda-entry-types))
3866 (unless org-agenda-include-deadlines
3867 (setq org-agenda-entry-types
3868 (delq :deadline org-agenda-entry-types)))
3869 (cond
3870 ((memq org-agenda-show-log '(only clockcheck))
3871 (setq rtn (org-agenda-get-day-entries
3872 file date :closed)))
3873 (org-agenda-show-log
3874 (setq rtn (apply 'org-agenda-get-day-entries
3875 file date
3876 (append '(:closed) org-agenda-entry-types))))
3878 (setq rtn (apply 'org-agenda-get-day-entries
3879 file date
3880 org-agenda-entry-types)))))
3881 (setq rtnall (append rtnall rtn)))) ;; all entries
3882 (if org-agenda-include-diary
3883 (let ((org-agenda-search-headline-for-time t))
3884 (require 'diary-lib)
3885 (setq rtn (org-get-entries-from-diary date))
3886 (setq rtnall (append rtnall rtn))))
3887 (if (or rtnall org-agenda-show-all-dates)
3888 (progn
3889 (setq day-cnt (1+ day-cnt))
3890 (insert
3891 (if (stringp org-agenda-format-date)
3892 (format-time-string org-agenda-format-date
3893 (org-time-from-absolute date))
3894 (funcall org-agenda-format-date date))
3895 "\n")
3896 (put-text-property s (1- (point)) 'face
3897 (org-agenda-get-day-face date))
3898 (put-text-property s (1- (point)) 'org-date-line t)
3899 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3900 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3901 (when todayp
3902 (put-text-property s (1- (point)) 'org-today t))
3903 (if rtnall (insert ;; all entries
3904 (org-finalize-agenda-entries
3905 (org-agenda-add-time-grid-maybe
3906 rtnall ndays todayp))
3907 "\n"))
3908 (put-text-property s (1- (point)) 'day d)
3909 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3910 (when (and org-agenda-clockreport-mode clocktable-start)
3911 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3912 ;; the above line is to ensure the restricted range!
3913 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3914 tbl)
3915 (setq p (org-plist-delete p :block))
3916 (setq p (plist-put p :tstart clocktable-start))
3917 (setq p (plist-put p :tend clocktable-end))
3918 (setq p (plist-put p :scope 'agenda))
3919 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3920 (setq filter (or org-agenda-tag-filter-while-redo
3921 (get 'org-agenda-tag-filter :preset-filter))))
3922 (setq p (plist-put p :tags (mapconcat (lambda (x)
3923 (if (string-match "[<>=]" x)
3926 filter ""))))
3927 (setq tbl (apply 'org-get-clocktable p))
3928 (insert tbl)))
3929 (goto-char (point-min))
3930 (or org-agenda-multi (org-fit-agenda-window))
3931 (unless (and (pos-visible-in-window-p (point-min))
3932 (pos-visible-in-window-p (point-max)))
3933 (goto-char (1- (point-max)))
3934 (recenter -1)
3935 (if (not (pos-visible-in-window-p (or start-pos 1)))
3936 (progn
3937 (goto-char (or start-pos 1))
3938 (recenter 1))))
3939 (goto-char (or start-pos 1))
3940 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3941 (if (eq org-agenda-show-log 'clockcheck)
3942 (org-agenda-show-clocking-issues))
3943 (org-finalize-agenda)
3944 (setq buffer-read-only t)
3945 (message "")))
3947 (defun org-agenda-ndays-to-span (n)
3948 "Return a span symbol for a span of N days, or N if none matches."
3949 (cond ((symbolp n) n)
3950 ((= n 1) 'day)
3951 ((= n 7) 'week)
3952 (t n)))
3954 (defun org-agenda-span-to-ndays (span start-day)
3955 "Return ndays from SPAN starting at START-DAY."
3956 (cond ((numberp span) span)
3957 ((eq span 'day) 1)
3958 ((eq span 'week) 7)
3959 ((eq span 'month)
3960 (let ((date (calendar-gregorian-from-absolute start-day)))
3961 (calendar-last-day-of-month (car date) (caddr date))))
3962 ((eq span 'year)
3963 (let ((date (calendar-gregorian-from-absolute start-day)))
3964 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3966 (defun org-agenda-span-name (span)
3967 "Return a SPAN name."
3968 (if (null span)
3970 (if (symbolp span)
3971 (capitalize (symbol-name span))
3972 (format "%d days" span))))
3974 ;;; Agenda word search
3976 (defvar org-agenda-search-history nil)
3977 (defvar org-todo-only nil)
3979 (defvar org-search-syntax-table nil
3980 "Special syntax table for org-mode search.
3981 In this table, we have single quotes not as word constituents, to
3982 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3984 (defun org-search-syntax-table ()
3985 (unless org-search-syntax-table
3986 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3987 (modify-syntax-entry ?' "." org-search-syntax-table)
3988 (modify-syntax-entry ?` "." org-search-syntax-table))
3989 org-search-syntax-table)
3991 (defvar org-agenda-last-search-view-search-was-boolean nil)
3993 ;;;###autoload
3994 (defun org-search-view (&optional todo-only string edit-at)
3995 "Show all entries that contain a phrase or words or regular expressions.
3997 With optional prefix argument TODO-ONLY, only consider entries that are
3998 TODO entries. The argument STRING can be used to pass a default search
3999 string into this function. If EDIT-AT is non-nil, it means that the
4000 user should get a chance to edit this string, with cursor at position
4001 EDIT-AT.
4003 The search string can be viewed either as a phrase that should be found as
4004 is, or it can be broken into a number of snippets, each of which must match
4005 in a Boolean way to select an entry. The default depends on the variable
4006 `org-agenda-search-view-always-boolean'.
4007 Even if this is turned off (the default) you can always switch to
4008 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4010 The default is a direct search of the whole phrase, where each space in
4011 the search string can expand to an arbitrary amount of whitespace,
4012 including newlines.
4014 If using a Boolean search, the search string is split on whitespace and
4015 each snippet is searched separately, with logical AND to select an entry.
4016 Words prefixed with a minus must *not* occur in the entry. Words without
4017 a prefix or prefixed with a plus must occur in the entry. Matching is
4018 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4019 match whole words, not parts of a word) if
4020 `org-agenda-search-view-force-full-words' is set (default is nil).
4022 Boolean search snippets enclosed by curly braces are interpreted as
4023 regular expressions that must or (when preceded with \"-\") must not
4024 match in the entry. Snippets enclosed into double quotes will be taken
4025 as a whole, to include whitespace.
4027 - If the search string starts with an asterisk, search only in headlines.
4028 - If (possibly after the leading star) the search string starts with an
4029 exclamation mark, this also means to look at TODO entries only, an effect
4030 that can also be achieved with a prefix argument.
4031 - If (possibly after star and exclamation mark) the search string starts
4032 with a colon, this will mean that the (non-regexp) snippets of the
4033 Boolean search must match as full words.
4035 This command searches the agenda files, and in addition the files listed
4036 in `org-agenda-text-search-extra-files'."
4037 (interactive "P")
4038 (org-prepare-agenda "SEARCH")
4039 (org-compile-prefix-format 'search)
4040 (org-set-sorting-strategy 'search)
4041 (let* ((props (list 'face nil
4042 'done-face 'org-agenda-done
4043 'org-not-done-regexp org-not-done-regexp
4044 'org-todo-regexp org-todo-regexp
4045 'org-complex-heading-regexp org-complex-heading-regexp
4046 'mouse-face 'highlight
4047 'help-echo (format "mouse-2 or RET jump to location")))
4048 (full-words org-agenda-search-view-force-full-words)
4049 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4050 regexp rtn rtnall files file pos
4051 marker category org-category-pos tags c neg re boolean
4052 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4053 (unless (and (not edit-at)
4054 (stringp string)
4055 (string-match "\\S-" string))
4056 (setq string (read-string
4057 (if org-agenda-search-view-always-boolean
4058 "[+-]Word/{Regexp} ...: "
4059 "Phrase, or [+-]Word/{Regexp} ...: ")
4060 (cond
4061 ((integerp edit-at) (cons string edit-at))
4062 (edit-at string))
4063 'org-agenda-search-history)))
4064 (org-set-local 'org-todo-only todo-only)
4065 (setq org-agenda-redo-command
4066 (list 'org-search-view (if todo-only t nil) string
4067 '(if current-prefix-arg 1 nil)))
4068 (setq org-agenda-query-string string)
4070 (if (equal (string-to-char string) ?*)
4071 (setq hdl-only t
4072 words (substring string 1))
4073 (setq words string))
4074 (when (equal (string-to-char words) ?!)
4075 (setq todo-only t
4076 words (substring words 1)))
4077 (when (equal (string-to-char words) ?:)
4078 (setq full-words t
4079 words (substring words 1)))
4080 (if (or org-agenda-search-view-always-boolean
4081 (member (string-to-char words) '(?- ?+ ?\{)))
4082 (setq boolean t))
4083 (setq words (org-split-string words))
4084 (let (www w)
4085 (while (setq w (pop words))
4086 (while (and (string-match "\\\\\\'" w) words)
4087 (setq w (concat (substring w 0 -1) " " (pop words))))
4088 (push w www))
4089 (setq words (nreverse www) www nil)
4090 (while (setq w (pop words))
4091 (when (and (string-match "\\`[-+]?{" w)
4092 (not (string-match "}\\'" w)))
4093 (while (and words (not (string-match "}\\'" (car words))))
4094 (setq w (concat w " " (pop words))))
4095 (setq w (concat w " " (pop words))))
4096 (push w www))
4097 (setq words (nreverse www)))
4098 (setq org-agenda-last-search-view-search-was-boolean boolean)
4099 (when boolean
4100 (let (wds w)
4101 (while (setq w (pop words))
4102 (if (or (equal (substring w 0 1) "\"")
4103 (and (> (length w) 1)
4104 (member (substring w 0 1) '("+" "-"))
4105 (equal (substring w 1 2) "\"")))
4106 (while (and words (not (equal (substring w -1) "\"")))
4107 (setq w (concat w " " (pop words)))))
4108 (and (string-match "\\`\\([-+]?\\)\"" w)
4109 (setq w (replace-match "\\1" nil nil w)))
4110 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4111 (push w wds))
4112 (setq words (nreverse wds))))
4113 (if boolean
4114 (mapc (lambda (w)
4115 (setq c (string-to-char w))
4116 (if (equal c ?-)
4117 (setq neg t w (substring w 1))
4118 (if (equal c ?+)
4119 (setq neg nil w (substring w 1))
4120 (setq neg nil)))
4121 (if (string-match "\\`{.*}\\'" w)
4122 (setq re (substring w 1 -1))
4123 (if full-words
4124 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4125 (setq re (regexp-quote (downcase w)))))
4126 (if neg (push re regexps-) (push re regexps+)))
4127 words)
4128 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4129 regexps+))
4130 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4131 (if (not regexps+)
4132 (setq regexp org-outline-regexp-bol)
4133 (setq regexp (pop regexps+))
4134 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4135 regexp))))
4136 (setq files (org-agenda-files nil 'ifmode))
4137 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4138 (pop org-agenda-text-search-extra-files)
4139 (setq files (org-add-archive-files files)))
4140 (setq files (append files org-agenda-text-search-extra-files)
4141 rtnall nil)
4142 (while (setq file (pop files))
4143 (setq ee nil)
4144 (catch 'nextfile
4145 (org-check-agenda-file file)
4146 (setq buffer (if (file-exists-p file)
4147 (org-get-agenda-file-buffer file)
4148 (error "No such file %s" file)))
4149 (if (not buffer)
4150 ;; If file does not exist, make sure an error message is sent
4151 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4152 file))))
4153 (with-current-buffer buffer
4154 (with-syntax-table (org-search-syntax-table)
4155 (unless (eq major-mode 'org-mode)
4156 (error "Agenda file %s is not in `org-mode'" file))
4157 (let ((case-fold-search t))
4158 (save-excursion
4159 (save-restriction
4160 (if org-agenda-restrict
4161 (narrow-to-region org-agenda-restrict-begin
4162 org-agenda-restrict-end)
4163 (widen))
4164 (goto-char (point-min))
4165 (unless (or (org-at-heading-p)
4166 (outline-next-heading))
4167 (throw 'nextfile t))
4168 (goto-char (max (point-min) (1- (point))))
4169 (while (re-search-forward regexp nil t)
4170 (org-back-to-heading t)
4171 (skip-chars-forward "* ")
4172 (setq beg (point-at-bol)
4173 beg1 (point)
4174 end (progn (outline-next-heading) (point)))
4175 (catch :skip
4176 (goto-char beg)
4177 (org-agenda-skip)
4178 (setq str (buffer-substring-no-properties
4179 (point-at-bol)
4180 (if hdl-only (point-at-eol) end)))
4181 (mapc (lambda (wr) (when (string-match wr str)
4182 (goto-char (1- end))
4183 (throw :skip t)))
4184 regexps-)
4185 (mapc (lambda (wr) (unless (string-match wr str)
4186 (goto-char (1- end))
4187 (throw :skip t)))
4188 (if todo-only
4189 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4190 regexps+)
4191 regexps+))
4192 (goto-char beg)
4193 (setq marker (org-agenda-new-marker (point))
4194 category (org-get-category)
4195 org-category-pos (get-text-property (point) 'org-category-position)
4196 tags (org-get-tags-at (point))
4197 txt (org-agenda-format-item
4199 (buffer-substring-no-properties
4200 beg1 (point-at-eol))
4201 category tags))
4202 (org-add-props txt props
4203 'org-marker marker 'org-hd-marker marker
4204 'org-todo-regexp org-todo-regexp
4205 'org-complex-heading-regexp org-complex-heading-regexp
4206 'priority 1000 'org-category category
4207 'org-category-position org-category-pos
4208 'type "search")
4209 (push txt ee)
4210 (goto-char (1- end))))))))))
4211 (setq rtn (nreverse ee))
4212 (setq rtnall (append rtnall rtn)))
4213 (if org-agenda-overriding-header
4214 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4215 nil 'face 'org-agenda-structure) "\n")
4216 (insert "Search words: ")
4217 (add-text-properties (point-min) (1- (point))
4218 (list 'face 'org-agenda-structure))
4219 (setq pos (point))
4220 (insert string "\n")
4221 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4222 (setq pos (point))
4223 (unless org-agenda-multi
4224 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4225 (add-text-properties pos (1- (point))
4226 (list 'face 'org-agenda-structure))))
4227 (org-agenda-mark-header-line (point-min))
4228 (when rtnall
4229 (insert (org-finalize-agenda-entries rtnall) "\n"))
4230 (goto-char (point-min))
4231 (or org-agenda-multi (org-fit-agenda-window))
4232 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4233 (org-finalize-agenda)
4234 (setq buffer-read-only t)))
4236 ;;; Agenda TODO list
4238 (defvar org-select-this-todo-keyword nil)
4239 (defvar org-last-arg nil)
4241 ;;;###autoload
4242 (defun org-todo-list (arg)
4243 "Show all (not done) TODO entries from all agenda file in a single list.
4244 The prefix arg can be used to select a specific TODO keyword and limit
4245 the list to these. When using \\[universal-argument], you will be prompted
4246 for a keyword. A numeric prefix directly selects the Nth keyword in
4247 `org-todo-keywords-1'."
4248 (interactive "P")
4249 (org-prepare-agenda "TODO")
4250 (org-compile-prefix-format 'todo)
4251 (org-set-sorting-strategy 'todo)
4252 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4253 (let* ((today (org-today))
4254 (date (calendar-gregorian-from-absolute today))
4255 (kwds org-todo-keywords-for-agenda)
4256 (completion-ignore-case t)
4257 (org-select-this-todo-keyword
4258 (if (stringp arg) arg
4259 (and arg (integerp arg) (> arg 0)
4260 (nth (1- arg) kwds))))
4261 rtn rtnall files file pos)
4262 (when (equal arg '(4))
4263 (setq org-select-this-todo-keyword
4264 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4265 (mapcar 'list kwds) nil nil)))
4266 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4267 (org-set-local 'org-last-arg arg)
4268 (setq org-agenda-redo-command
4269 '(org-todo-list (or current-prefix-arg org-last-arg)))
4270 (setq files (org-agenda-files nil 'ifmode)
4271 rtnall nil)
4272 (while (setq file (pop files))
4273 (catch 'nextfile
4274 (org-check-agenda-file file)
4275 (setq rtn (org-agenda-get-day-entries file date :todo))
4276 (setq rtnall (append rtnall rtn))))
4277 (if org-agenda-overriding-header
4278 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4279 nil 'face 'org-agenda-structure) "\n")
4280 (insert "Global list of TODO items of type: ")
4281 (add-text-properties (point-min) (1- (point))
4282 (list 'face 'org-agenda-structure
4283 'short-heading
4284 (concat "ToDo: "
4285 (or org-select-this-todo-keyword "ALL"))))
4286 (org-agenda-mark-header-line (point-min))
4287 (setq pos (point))
4288 (insert (or org-select-this-todo-keyword "ALL") "\n")
4289 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4290 (setq pos (point))
4291 (unless org-agenda-multi
4292 (insert "Available with `N r': (0)ALL")
4293 (let ((n 0) s)
4294 (mapc (lambda (x)
4295 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4296 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4297 (insert "\n "))
4298 (insert " " s))
4299 kwds))
4300 (insert "\n"))
4301 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4302 (org-agenda-mark-header-line (point-min))
4303 (when rtnall
4304 (insert (org-finalize-agenda-entries rtnall) "\n"))
4305 (goto-char (point-min))
4306 (or org-agenda-multi (org-fit-agenda-window))
4307 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4308 (org-finalize-agenda)
4309 (setq buffer-read-only t)))
4311 ;;; Agenda tags match
4313 ;;;###autoload
4314 (defun org-tags-view (&optional todo-only match)
4315 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4316 The prefix arg TODO-ONLY limits the search to TODO entries."
4317 (interactive "P")
4318 (let* ((org-tags-match-list-sublevels
4319 org-tags-match-list-sublevels)
4320 (completion-ignore-case t)
4321 rtn rtnall files file pos matcher
4322 buffer)
4323 (when (and (stringp match) (not (string-match "\\S-" match)))
4324 (setq match nil))
4325 (setq matcher (org-make-tags-matcher match)
4326 match (car matcher) matcher (cdr matcher))
4327 (org-prepare-agenda (concat "TAGS " match))
4328 (org-compile-prefix-format 'tags)
4329 (org-set-sorting-strategy 'tags)
4330 (setq org-agenda-query-string match)
4331 (setq org-agenda-redo-command
4332 (list 'org-tags-view (list 'quote todo-only)
4333 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4334 (setq files (org-agenda-files nil 'ifmode)
4335 rtnall nil)
4336 (while (setq file (pop files))
4337 (catch 'nextfile
4338 (org-check-agenda-file file)
4339 (setq buffer (if (file-exists-p file)
4340 (org-get-agenda-file-buffer file)
4341 (error "No such file %s" file)))
4342 (if (not buffer)
4343 ;; If file does not exist, error message to agenda
4344 (setq rtn (list
4345 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4346 rtnall (append rtnall rtn))
4347 (with-current-buffer buffer
4348 (unless (eq major-mode 'org-mode)
4349 (error "Agenda file %s is not in `org-mode'" file))
4350 (save-excursion
4351 (save-restriction
4352 (if org-agenda-restrict
4353 (narrow-to-region org-agenda-restrict-begin
4354 org-agenda-restrict-end)
4355 (widen))
4356 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4357 (setq rtnall (append rtnall rtn))))))))
4358 (if org-agenda-overriding-header
4359 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4360 nil 'face 'org-agenda-structure) "\n")
4361 (insert "Headlines with TAGS match: ")
4362 (add-text-properties (point-min) (1- (point))
4363 (list 'face 'org-agenda-structure
4364 'short-heading
4365 (concat "Match: " match)))
4366 (setq pos (point))
4367 (insert match "\n")
4368 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4369 (setq pos (point))
4370 (unless org-agenda-multi
4371 (insert "Press `C-u r' to search again with new search string\n"))
4372 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4373 (org-agenda-mark-header-line (point-min))
4374 (when rtnall
4375 (insert (org-finalize-agenda-entries rtnall) "\n"))
4376 (goto-char (point-min))
4377 (or org-agenda-multi (org-fit-agenda-window))
4378 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4379 (org-finalize-agenda)
4380 (setq buffer-read-only t)))
4382 ;;; Agenda Finding stuck projects
4384 (defvar org-agenda-skip-regexp nil
4385 "Regular expression used in skipping subtrees for the agenda.
4386 This is basically a temporary global variable that can be set and then
4387 used by user-defined selections using `org-agenda-skip-function'.")
4389 (defvar org-agenda-overriding-header nil
4390 "When set during agenda, todo and tags searches it replaces the header.
4391 This variable should not be set directly, but custom commands can bind it
4392 in the options section.")
4394 (defun org-agenda-skip-entry-when-regexp-matches ()
4395 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4396 If yes, it returns the end position of this entry, causing agenda commands
4397 to skip the entry but continuing the search in the subtree. This is a
4398 function that can be put into `org-agenda-skip-function' for the duration
4399 of a command."
4400 (let ((end (save-excursion (org-end-of-subtree t)))
4401 skip)
4402 (save-excursion
4403 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4404 (and skip end)))
4406 (defun org-agenda-skip-subtree-when-regexp-matches ()
4407 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4408 If yes, it returns the end position of this tree, causing agenda commands
4409 to skip this subtree. This is a function that can be put into
4410 `org-agenda-skip-function' for the duration of a command."
4411 (let ((end (save-excursion (org-end-of-subtree t)))
4412 skip)
4413 (save-excursion
4414 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4415 (and skip end)))
4417 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4418 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4419 If yes, it returns the end position of the current entry (NOT the tree),
4420 causing agenda commands to skip the entry but continuing the search in
4421 the subtree. This is a function that can be put into
4422 `org-agenda-skip-function' for the duration of a command. An important
4423 use of this function is for the stuck project list."
4424 (let ((end (save-excursion (org-end-of-subtree t)))
4425 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4426 skip)
4427 (save-excursion
4428 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4429 (and skip entry-end)))
4431 (defun org-agenda-skip-entry-if (&rest conditions)
4432 "Skip entry if any of CONDITIONS is true.
4433 See `org-agenda-skip-if' for details."
4434 (org-agenda-skip-if nil conditions))
4436 (defun org-agenda-skip-subtree-if (&rest conditions)
4437 "Skip entry if any of CONDITIONS is true.
4438 See `org-agenda-skip-if' for details."
4439 (org-agenda-skip-if t conditions))
4441 (defun org-agenda-skip-if (subtree conditions)
4442 "Checks current entity for CONDITIONS.
4443 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4444 the entry, i.e. the text before the next heading is checked.
4446 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4447 from different tests. Valid conditions are:
4449 scheduled Check if there is a scheduled cookie
4450 notscheduled Check if there is no scheduled cookie
4451 deadline Check if there is a deadline
4452 notdeadline Check if there is no deadline
4453 timestamp Check if there is a timestamp (also deadline or scheduled)
4454 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4455 regexp Check if regexp matches
4456 notregexp Check if regexp does not match.
4457 todo Check if TODO keyword matches
4458 nottodo Check if TODO keyword does not match
4460 The regexp is taken from the conditions list, it must come right after
4461 the `regexp' or `notregexp' element.
4463 `todo' and `nottodo' accept as an argument a list of todo
4464 keywords, which may include \"*\" to match any todo keyword.
4466 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4468 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4470 Instead of a list a keyword class may be given
4472 (org-agenda-skip-entry-if 'nottodo 'done)
4474 would skip entries that haven't been marked with any of \"DONE\"
4475 keywords. Possible classes are: `todo', `done', `any'.
4477 If any of these conditions is met, this function returns the end point of
4478 the entity, causing the search to continue from there. This is a function
4479 that can be put into `org-agenda-skip-function' for the duration of a command."
4480 (let (beg end m)
4481 (org-back-to-heading t)
4482 (setq beg (point)
4483 end (if subtree
4484 (progn (org-end-of-subtree t) (point))
4485 (progn (outline-next-heading) (1- (point)))))
4486 (goto-char beg)
4487 (and
4489 (and (memq 'scheduled conditions)
4490 (re-search-forward org-scheduled-time-regexp end t))
4491 (and (memq 'notscheduled conditions)
4492 (not (re-search-forward org-scheduled-time-regexp end t)))
4493 (and (memq 'deadline conditions)
4494 (re-search-forward org-deadline-time-regexp end t))
4495 (and (memq 'notdeadline conditions)
4496 (not (re-search-forward org-deadline-time-regexp end t)))
4497 (and (memq 'timestamp conditions)
4498 (re-search-forward org-ts-regexp end t))
4499 (and (memq 'nottimestamp conditions)
4500 (not (re-search-forward org-ts-regexp end t)))
4501 (and (setq m (memq 'regexp conditions))
4502 (stringp (nth 1 m))
4503 (re-search-forward (nth 1 m) end t))
4504 (and (setq m (memq 'notregexp conditions))
4505 (stringp (nth 1 m))
4506 (not (re-search-forward (nth 1 m) end t)))
4507 (and (or
4508 (setq m (memq 'todo conditions))
4509 (setq m (memq 'nottodo conditions)))
4510 (org-agenda-skip-if-todo m end)))
4511 end)))
4513 (defun org-agenda-skip-if-todo (args end)
4514 "Helper function for `org-agenda-skip-if', do not use it directly.
4515 ARGS is a list with first element either `todo' or `nottodo'.
4516 The remainder is either a list of TODO keywords, or a state symbol
4517 `todo' or `done' or `any'."
4518 (let ((kw (car args))
4519 (arg (cadr args))
4520 todo-wds todo-re)
4521 (setq todo-wds
4522 (org-uniquify
4523 (cond
4524 ((listp arg) ;; list of keywords
4525 (if (member "*" arg)
4526 (mapcar 'substring-no-properties org-todo-keywords-1)
4527 arg))
4528 ((symbolp arg) ;; keyword class name
4529 (cond
4530 ((eq arg 'todo)
4531 (org-delete-all org-done-keywords
4532 (mapcar 'substring-no-properties
4533 org-todo-keywords-1)))
4534 ((eq arg 'done) org-done-keywords)
4535 ((eq arg 'any)
4536 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4537 (setq todo-re
4538 (concat "^\\*+[ \t]+\\<\\("
4539 (mapconcat 'identity todo-wds "\\|")
4540 "\\)\\>"))
4541 (if (eq kw 'todo)
4542 (re-search-forward todo-re end t)
4543 (not (re-search-forward todo-re end t)))))
4545 ;;;###autoload
4546 (defun org-agenda-list-stuck-projects (&rest ignore)
4547 "Create agenda view for projects that are stuck.
4548 Stuck projects are project that have no next actions. For the definitions
4549 of what a project is and how to check if it stuck, customize the variable
4550 `org-stuck-projects'."
4551 (interactive)
4552 (let* ((org-agenda-skip-function
4553 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4554 ;; We could have used org-agenda-skip-if here.
4555 (org-agenda-overriding-header
4556 (or org-agenda-overriding-header "List of stuck projects: "))
4557 (matcher (nth 0 org-stuck-projects))
4558 (todo (nth 1 org-stuck-projects))
4559 (todo-wds (if (member "*" todo)
4560 (progn
4561 (org-prepare-agenda-buffers (org-agenda-files
4562 nil 'ifmode))
4563 (org-delete-all
4564 org-done-keywords-for-agenda
4565 (copy-sequence org-todo-keywords-for-agenda)))
4566 todo))
4567 (todo-re (concat "^\\*+[ \t]+\\("
4568 (mapconcat 'identity todo-wds "\\|")
4569 "\\)\\>"))
4570 (tags (nth 2 org-stuck-projects))
4571 (tags-re (if (member "*" tags)
4572 (concat org-outline-regexp-bol
4573 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4574 (if tags
4575 (concat org-outline-regexp-bol
4576 ".*:\\("
4577 (mapconcat 'identity tags "\\|")
4578 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4579 (gen-re (nth 3 org-stuck-projects))
4580 (re-list
4581 (delq nil
4582 (list
4583 (if todo todo-re)
4584 (if tags tags-re)
4585 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4586 gen-re)))))
4587 (setq org-agenda-skip-regexp
4588 (if re-list
4589 (mapconcat 'identity re-list "\\|")
4590 (error "No information how to identify unstuck projects")))
4591 (org-tags-view nil matcher)
4592 (with-current-buffer org-agenda-buffer-name
4593 (setq org-agenda-redo-command
4594 '(org-agenda-list-stuck-projects
4595 (or current-prefix-arg org-last-arg))))))
4597 ;;; Diary integration
4599 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4600 (defvar diary-list-entries-hook)
4601 (defvar diary-time-regexp)
4602 (defun org-get-entries-from-diary (date)
4603 "Get the (Emacs Calendar) diary entries for DATE."
4604 (require 'diary-lib)
4605 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4606 (diary-display-hook '(fancy-diary-display))
4607 (diary-display-function 'fancy-diary-display)
4608 (pop-up-frames nil)
4609 (diary-list-entries-hook
4610 (cons 'org-diary-default-entry diary-list-entries-hook))
4611 (diary-file-name-prefix-function nil) ; turn this feature off
4612 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4613 entries
4614 (org-disable-agenda-to-diary t))
4615 (save-excursion
4616 (save-window-excursion
4617 (funcall (if (fboundp 'diary-list-entries)
4618 'diary-list-entries 'list-diary-entries)
4619 date 1)))
4620 (if (not (get-buffer diary-fancy-buffer))
4621 (setq entries nil)
4622 (with-current-buffer diary-fancy-buffer
4623 (setq buffer-read-only nil)
4624 (if (zerop (buffer-size))
4625 ;; No entries
4626 (setq entries nil)
4627 ;; Omit the date and other unnecessary stuff
4628 (org-agenda-cleanup-fancy-diary)
4629 ;; Add prefix to each line and extend the text properties
4630 (if (zerop (buffer-size))
4631 (setq entries nil)
4632 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4633 (setq entries
4634 (with-temp-buffer
4635 (insert entries) (goto-char (point-min))
4636 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4637 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4638 (replace-match (concat "; " (match-string 1)))))
4639 (buffer-string)))))
4640 (set-buffer-modified-p nil)
4641 (kill-buffer diary-fancy-buffer)))
4642 (when entries
4643 (setq entries (org-split-string entries "\n"))
4644 (setq entries
4645 (mapcar
4646 (lambda (x)
4647 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4648 ;; Extend the text properties to the beginning of the line
4649 (org-add-props x (text-properties-at (1- (length x)) x)
4650 'type "diary" 'date date 'face 'org-agenda-diary))
4651 entries)))))
4653 (defvar org-agenda-cleanup-fancy-diary-hook nil
4654 "Hook run when the fancy diary buffer is cleaned up.")
4656 (defun org-agenda-cleanup-fancy-diary ()
4657 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4658 This gets rid of the date, the underline under the date, and
4659 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4660 date. It also removes lines that contain only whitespace."
4661 (goto-char (point-min))
4662 (if (looking-at ".*?:[ \t]*")
4663 (progn
4664 (replace-match "")
4665 (re-search-forward "\n=+$" nil t)
4666 (replace-match "")
4667 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4668 (re-search-forward "\n=+$" nil t)
4669 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4670 (goto-char (point-min))
4671 (while (re-search-forward "^ +\n" nil t)
4672 (replace-match ""))
4673 (goto-char (point-min))
4674 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4675 (replace-match ""))
4676 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4678 ;; Make sure entries from the diary have the right text properties.
4679 (eval-after-load "diary-lib"
4680 '(if (boundp 'diary-modify-entry-list-string-function)
4681 ;; We can rely on the hook, nothing to do
4683 ;; Hook not available, must use advice to make this work
4684 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4685 "Make the position visible."
4686 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4687 (stringp string)
4688 buffer-file-name)
4689 (setq string (org-modify-diary-entry-string string))))))
4691 (defun org-modify-diary-entry-string (string)
4692 "Add text properties to string, allowing org-mode to act on it."
4693 (org-add-props string nil
4694 'mouse-face 'highlight
4695 'help-echo (if buffer-file-name
4696 (format "mouse-2 or RET jump to diary file %s"
4697 (abbreviate-file-name buffer-file-name))
4699 'org-agenda-diary-link t
4700 'org-marker (org-agenda-new-marker (point-at-bol))))
4702 (defun org-diary-default-entry ()
4703 "Add a dummy entry to the diary.
4704 Needed to avoid empty dates which mess up holiday display."
4705 ;; Catch the error if dealing with the new add-to-diary-alist
4706 (when org-disable-agenda-to-diary
4707 (condition-case nil
4708 (org-add-to-diary-list original-date "Org-mode dummy" "")
4709 (error
4710 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4712 (defun org-add-to-diary-list (&rest args)
4713 (if (fboundp 'diary-add-to-list)
4714 (apply 'diary-add-to-list args)
4715 (apply 'add-to-diary-list args)))
4717 (defvar org-diary-last-run-time nil)
4719 ;;;###autoload
4720 (defun org-diary (&rest args)
4721 "Return diary information from org-files.
4722 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4723 It accesses org files and extracts information from those files to be
4724 listed in the diary. The function accepts arguments specifying what
4725 items should be listed. For a list of arguments allowed here, see the
4726 variable `org-agenda-entry-types'.
4728 The call in the diary file should look like this:
4730 &%%(org-diary) ~/path/to/some/orgfile.org
4732 Use a separate line for each org file to check. Or, if you omit the file name,
4733 all files listed in `org-agenda-files' will be checked automatically:
4735 &%%(org-diary)
4737 If you don't give any arguments (as in the example above), the default
4738 arguments (:deadline :scheduled :timestamp :sexp) are used.
4739 So the example above may also be written as
4741 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4743 The function expects the lisp variables `entry' and `date' to be provided
4744 by the caller, because this is how the calendar works. Don't use this
4745 function from a program - use `org-agenda-get-day-entries' instead."
4746 (when (> (- (org-float-time)
4747 org-agenda-last-marker-time)
4749 ;; I am not sure if this works with sticky agendas, because the marker
4750 ;; list is then no longer a global variable.
4751 (org-agenda-reset-markers))
4752 (org-compile-prefix-format 'agenda)
4753 (org-set-sorting-strategy 'agenda)
4754 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4755 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4756 (list entry)
4757 (org-agenda-files t)))
4758 (time (org-float-time))
4759 file rtn results)
4760 (when (or (not org-diary-last-run-time)
4761 (> (- time
4762 org-diary-last-run-time)
4764 (org-prepare-agenda-buffers files))
4765 (setq org-diary-last-run-time time)
4766 ;; If this is called during org-agenda, don't return any entries to
4767 ;; the calendar. Org Agenda will list these entries itself.
4768 (if org-disable-agenda-to-diary (setq files nil))
4769 (while (setq file (pop files))
4770 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4771 (setq results (append results rtn)))
4772 (if results
4773 (concat (org-finalize-agenda-entries results) "\n"))))
4775 ;;; Agenda entry finders
4777 (defun org-agenda-get-day-entries (file date &rest args)
4778 "Does the work for `org-diary' and `org-agenda'.
4779 FILE is the path to a file to be checked for entries. DATE is date like
4780 the one returned by `calendar-current-date'. ARGS are symbols indicating
4781 which kind of entries should be extracted. For details about these, see
4782 the documentation of `org-diary'."
4783 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4784 (let* ((org-startup-folded nil)
4785 (org-startup-align-all-tables nil)
4786 (buffer (if (file-exists-p file)
4787 (org-get-agenda-file-buffer file)
4788 (error "No such file %s" file)))
4789 arg results rtn deadline-results)
4790 (if (not buffer)
4791 ;; If file does not exist, make sure an error message ends up in diary
4792 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4793 (with-current-buffer buffer
4794 (unless (eq major-mode 'org-mode)
4795 (error "Agenda file %s is not in `org-mode'" file))
4796 (let ((case-fold-search nil))
4797 (save-excursion
4798 (save-restriction
4799 (if org-agenda-restrict
4800 (narrow-to-region org-agenda-restrict-begin
4801 org-agenda-restrict-end)
4802 (widen))
4803 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4804 (while (setq arg (pop args))
4805 (cond
4806 ((and (eq arg :todo)
4807 (equal date (calendar-gregorian-from-absolute
4808 (org-today))))
4809 (setq rtn (org-agenda-get-todos))
4810 (setq results (append results rtn)))
4811 ((eq arg :timestamp)
4812 (setq rtn (org-agenda-get-blocks))
4813 (setq results (append results rtn))
4814 (setq rtn (org-agenda-get-timestamps))
4815 (setq results (append results rtn)))
4816 ((eq arg :sexp)
4817 (setq rtn (org-agenda-get-sexps))
4818 (setq results (append results rtn)))
4819 ((eq arg :scheduled)
4820 (setq rtn (org-agenda-get-scheduled deadline-results))
4821 (setq results (append results rtn)))
4822 ((eq arg :closed)
4823 (setq rtn (org-agenda-get-progress))
4824 (setq results (append results rtn)))
4825 ((eq arg :deadline)
4826 (setq rtn (org-agenda-get-deadlines))
4827 (setq deadline-results (copy-sequence rtn))
4828 (setq results (append results rtn))))))))
4829 results))))
4831 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4832 (defun org-agenda-get-todos ()
4833 "Return the TODO information for agenda display."
4834 (let* ((props (list 'face nil
4835 'done-face 'org-agenda-done
4836 'org-not-done-regexp org-not-done-regexp
4837 'org-todo-regexp org-todo-regexp
4838 'org-complex-heading-regexp org-complex-heading-regexp
4839 'mouse-face 'highlight
4840 'help-echo
4841 (format "mouse-2 or RET jump to org file %s"
4842 (abbreviate-file-name buffer-file-name))))
4843 (regexp (format org-heading-keyword-regexp-format
4844 (cond
4845 ((and org-select-this-todo-keyword
4846 (equal org-select-this-todo-keyword "*"))
4847 org-todo-regexp)
4848 (org-select-this-todo-keyword
4849 (concat "\\("
4850 (mapconcat 'identity
4851 (org-split-string
4852 org-select-this-todo-keyword
4853 "|")
4854 "\\|") "\\)"))
4855 (t org-not-done-regexp))))
4856 marker priority category org-category-pos tags todo-state
4857 ee txt beg end)
4858 (goto-char (point-min))
4859 (while (re-search-forward regexp nil t)
4860 (catch :skip
4861 (save-match-data
4862 (beginning-of-line)
4863 (org-agenda-skip)
4864 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4865 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4866 (goto-char (1+ beg))
4867 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4868 (throw :skip nil)))
4869 (goto-char (match-beginning 2))
4870 (setq marker (org-agenda-new-marker (match-beginning 0))
4871 category (org-get-category)
4872 org-category-pos (get-text-property (point) 'org-category-position)
4873 txt (org-trim
4874 (buffer-substring (match-beginning 2) (match-end 0)))
4875 tags (org-get-tags-at (point))
4876 txt (org-agenda-format-item "" txt category tags)
4877 priority (1+ (org-get-priority txt))
4878 todo-state (org-get-todo-state))
4879 (org-add-props txt props
4880 'org-marker marker 'org-hd-marker marker
4881 'priority priority 'org-category category
4882 'org-category-position org-category-pos
4883 'type "todo" 'todo-state todo-state)
4884 (push txt ee)
4885 (if org-agenda-todo-list-sublevels
4886 (goto-char (match-end 2))
4887 (org-end-of-subtree 'invisible))))
4888 (nreverse ee)))
4890 (defun org-agenda-todo-custom-ignore-p (time n)
4891 "Check whether timestamp is farther away then n number of days.
4892 This function is invoked if `org-agenda-todo-ignore-deadlines',
4893 `org-agenda-todo-ignore-scheduled' or
4894 `org-agenda-todo-ignore-timestamp' is set to an integer."
4895 (let ((days (org-days-to-time time)))
4896 (if (>= n 0)
4897 (>= days n)
4898 (<= days n))))
4900 ;;;###autoload
4901 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4902 (&optional end)
4903 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4904 (when (or org-agenda-todo-ignore-with-date
4905 org-agenda-todo-ignore-scheduled
4906 org-agenda-todo-ignore-deadlines
4907 org-agenda-todo-ignore-timestamp)
4908 (setq end (or end (save-excursion (outline-next-heading) (point))))
4909 (save-excursion
4910 (or (and org-agenda-todo-ignore-with-date
4911 (re-search-forward org-ts-regexp end t))
4912 (and org-agenda-todo-ignore-scheduled
4913 (re-search-forward org-scheduled-time-regexp end t)
4914 (cond
4915 ((eq org-agenda-todo-ignore-scheduled 'future)
4916 (> (org-days-to-time (match-string 1)) 0))
4917 ((eq org-agenda-todo-ignore-scheduled 'past)
4918 (<= (org-days-to-time (match-string 1)) 0))
4919 ((numberp org-agenda-todo-ignore-scheduled)
4920 (org-agenda-todo-custom-ignore-p
4921 (match-string 1) org-agenda-todo-ignore-scheduled))
4922 (t)))
4923 (and org-agenda-todo-ignore-deadlines
4924 (re-search-forward org-deadline-time-regexp end t)
4925 (cond
4926 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4927 ((eq org-agenda-todo-ignore-deadlines 'far)
4928 (not (org-deadline-close (match-string 1))))
4929 ((eq org-agenda-todo-ignore-deadlines 'future)
4930 (> (org-days-to-time (match-string 1)) 0))
4931 ((eq org-agenda-todo-ignore-deadlines 'past)
4932 (<= (org-days-to-time (match-string 1)) 0))
4933 ((numberp org-agenda-todo-ignore-deadlines)
4934 (org-agenda-todo-custom-ignore-p
4935 (match-string 1) org-agenda-todo-ignore-deadlines))
4936 (t (org-deadline-close (match-string 1)))))
4937 (and org-agenda-todo-ignore-timestamp
4938 (let ((buffer (current-buffer))
4939 (regexp
4940 (concat
4941 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4942 (start (point)))
4943 ;; Copy current buffer into a temporary one
4944 (with-temp-buffer
4945 (insert-buffer-substring buffer start end)
4946 (goto-char (point-min))
4947 ;; Delete SCHEDULED and DEADLINE items
4948 (while (re-search-forward regexp end t)
4949 (delete-region (match-beginning 0) (match-end 0)))
4950 (goto-char (point-min))
4951 ;; No search for timestamp left
4952 (when (re-search-forward org-ts-regexp nil t)
4953 (cond
4954 ((eq org-agenda-todo-ignore-timestamp 'future)
4955 (> (org-days-to-time (match-string 1)) 0))
4956 ((eq org-agenda-todo-ignore-timestamp 'past)
4957 (<= (org-days-to-time (match-string 1)) 0))
4958 ((numberp org-agenda-todo-ignore-timestamp)
4959 (org-agenda-todo-custom-ignore-p
4960 (match-string 1) org-agenda-todo-ignore-timestamp))
4961 (t))))))))))
4963 (defconst org-agenda-no-heading-message
4964 "No heading for this item in buffer or region.")
4966 (defun org-agenda-get-timestamps ()
4967 "Return the date stamp information for agenda display."
4968 (let* ((props (list 'face 'org-agenda-calendar-event
4969 'org-not-done-regexp org-not-done-regexp
4970 'org-todo-regexp org-todo-regexp
4971 'org-complex-heading-regexp org-complex-heading-regexp
4972 'mouse-face 'highlight
4973 'help-echo
4974 (format "mouse-2 or RET jump to org file %s"
4975 (abbreviate-file-name buffer-file-name))))
4976 (d1 (calendar-absolute-from-gregorian date))
4977 (remove-re
4978 (concat
4979 (regexp-quote
4980 (format-time-string
4981 "<%Y-%m-%d"
4982 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4983 ".*?>"))
4984 (regexp
4985 (concat
4986 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4987 (regexp-quote
4988 (substring
4989 (format-time-string
4990 (car org-time-stamp-formats)
4991 (apply 'encode-time ; DATE bound by calendar
4992 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4993 1 11))
4994 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4995 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4996 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4997 donep tmp priority category org-category-pos ee txt timestr tags
4998 b0 b3 e3 head todo-state end-of-match show-all)
4999 (goto-char (point-min))
5000 (while (setq end-of-match (re-search-forward regexp nil t))
5001 (setq b0 (match-beginning 0)
5002 b3 (match-beginning 3) e3 (match-end 3)
5003 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5004 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5005 (member todo-state
5006 org-agenda-repeating-timestamp-show-all)))
5007 (catch :skip
5008 (and (org-at-date-range-p) (throw :skip nil))
5009 (org-agenda-skip)
5010 (if (and (match-end 1)
5011 (not (= d1 (org-time-string-to-absolute
5012 (match-string 1) d1 nil show-all
5013 (current-buffer) b0))))
5014 (throw :skip nil))
5015 (if (and e3
5016 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5017 (throw :skip nil))
5018 (setq tmp (buffer-substring (max (point-min)
5019 (- b0 org-ds-keyword-length))
5021 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5022 inactivep (= (char-after b0) ?\[)
5023 deadlinep (string-match org-deadline-regexp tmp)
5024 scheduledp (string-match org-scheduled-regexp tmp)
5025 closedp (and org-agenda-include-inactive-timestamps
5026 (string-match org-closed-string tmp))
5027 clockp (and org-agenda-include-inactive-timestamps
5028 (or (string-match org-clock-string tmp)
5029 (string-match "]-+\\'" tmp)))
5030 donep (member todo-state org-done-keywords))
5031 (if (or scheduledp deadlinep closedp clockp
5032 (and donep org-agenda-skip-timestamp-if-done))
5033 (throw :skip t))
5034 (if (string-match ">" timestr)
5035 ;; substring should only run to end of time stamp
5036 (setq timestr (substring timestr 0 (match-end 0))))
5037 (setq marker (org-agenda-new-marker b0)
5038 category (org-get-category b0)
5039 org-category-pos (get-text-property b0 'org-category-position))
5040 (save-excursion
5041 (if (not (re-search-backward org-outline-regexp-bol nil t))
5042 (setq txt org-agenda-no-heading-message)
5043 (goto-char (match-beginning 0))
5044 (setq hdmarker (org-agenda-new-marker)
5045 tags (org-get-tags-at))
5046 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5047 (setq head (or (match-string 1) ""))
5048 (setq txt (org-agenda-format-item
5049 (if inactivep org-agenda-inactive-leader nil)
5050 head category tags timestr
5051 remove-re)))
5052 (setq priority (org-get-priority txt))
5053 (org-add-props txt props
5054 'org-marker marker 'org-hd-marker hdmarker)
5055 (org-add-props txt nil 'priority priority
5056 'org-category category 'date date
5057 'org-category-position org-category-pos
5058 'todo-state todo-state
5059 'type "timestamp")
5060 (push txt ee))
5061 (if org-agenda-skip-additional-timestamps-same-entry
5062 (outline-next-heading)
5063 (goto-char end-of-match))))
5064 (nreverse ee)))
5066 (defun org-agenda-get-sexps ()
5067 "Return the sexp information for agenda display."
5068 (require 'diary-lib)
5069 (let* ((props (list 'face 'org-agenda-calendar-sexp
5070 'mouse-face 'highlight
5071 'help-echo
5072 (format "mouse-2 or RET jump to org file %s"
5073 (abbreviate-file-name buffer-file-name))))
5074 (regexp "^&?%%(")
5075 marker category org-category-pos ee txt tags entry
5076 result beg b sexp sexp-entry todo-state)
5077 (goto-char (point-min))
5078 (while (re-search-forward regexp nil t)
5079 (catch :skip
5080 (org-agenda-skip)
5081 (setq beg (match-beginning 0))
5082 (goto-char (1- (match-end 0)))
5083 (setq b (point))
5084 (forward-sexp 1)
5085 (setq sexp (buffer-substring b (point)))
5086 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5087 (org-trim (match-string 1))
5088 ""))
5089 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5090 (when result
5091 (setq marker (org-agenda-new-marker beg)
5092 category (org-get-category beg)
5093 org-category-pos (get-text-property beg 'org-category-position)
5094 todo-state (org-get-todo-state))
5096 (dolist (r (if (stringp result)
5097 (list result)
5098 result)) ;; we expect a list here
5099 (if (string-match "\\S-" r)
5100 (setq txt r)
5101 (setq txt "SEXP entry returned empty string"))
5103 (setq txt (org-agenda-format-item
5104 "" txt category tags 'time))
5105 (org-add-props txt props 'org-marker marker)
5106 (org-add-props txt nil
5107 'org-category category 'date date 'todo-state todo-state
5108 'org-category-position org-category-pos
5109 'type "sexp")
5110 (push txt ee)))))
5111 (nreverse ee)))
5113 ;; Calendar sanity: define some functions that are independent of
5114 ;; `calendar-date-style'.
5115 ;; Normally I would like to use ISO format when calling the diary functions,
5116 ;; but to make sure we still have Emacs 22 compatibility we bind
5117 ;; also `european-calendar-style' and use european format
5118 (defun org-anniversary (year month day &optional mark)
5119 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5120 (org-no-warnings
5121 (let ((calendar-date-style 'european) (european-calendar-style t))
5122 (diary-anniversary day month year mark))))
5123 (defun org-cyclic (N year month day &optional mark)
5124 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5125 (org-no-warnings
5126 (let ((calendar-date-style 'european) (european-calendar-style t))
5127 (diary-cyclic N day month year mark))))
5128 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5129 "Like `diary-block', but with fixed (ISO) order of arguments."
5130 (org-no-warnings
5131 (let ((calendar-date-style 'european) (european-calendar-style t))
5132 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5133 (defun org-date (year month day &optional mark)
5134 "Like `diary-date', but with fixed (ISO) order of arguments."
5135 (org-no-warnings
5136 (let ((calendar-date-style 'european) (european-calendar-style t))
5137 (diary-date day month year mark))))
5138 (defalias 'org-float 'diary-float)
5140 ;; Define the` org-class' function
5141 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5142 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5143 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5144 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5145 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5146 `holidays', then any date that is known by the Emacs calendar to be a
5147 holiday will also be skipped."
5148 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5149 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5150 (d (calendar-absolute-from-gregorian date)))
5151 (and
5152 (<= date1 d)
5153 (<= d date2)
5154 (= (calendar-day-of-week date) dayname)
5155 (or (not skip-weeks)
5156 (progn
5157 (require 'cal-iso)
5158 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5159 (not (and (memq 'holidays skip-weeks)
5160 (calendar-check-holidays date)))
5161 entry)))
5163 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5164 "Like `org-class', but honor `calendar-date-style'.
5165 The order of the first 2 times 3 arguments depends on the variable
5166 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5167 So for American calendars, give this as MONTH DAY YEAR, for European as
5168 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5169 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5170 is any number of ISO weeks in the block period for which the item should
5171 be skipped.
5173 This function is here only for backward compatibility and it is deprecated,
5174 please use `org-class' instead."
5175 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5176 (date2 (org-order-calendar-date-args m2 d2 y2)))
5177 (org-class
5178 (nth 2 date1) (car date1) (nth 1 date1)
5179 (nth 2 date2) (car date2) (nth 1 date2)
5180 dayname skip-weeks)))
5181 (make-obsolete 'org-diary-class 'org-class "")
5183 (defalias 'org-get-closed 'org-agenda-get-progress)
5184 (defun org-agenda-get-progress ()
5185 "Return the logged TODO entries for agenda display."
5186 (let* ((props (list 'mouse-face 'highlight
5187 'org-not-done-regexp org-not-done-regexp
5188 'org-todo-regexp org-todo-regexp
5189 'org-complex-heading-regexp org-complex-heading-regexp
5190 'help-echo
5191 (format "mouse-2 or RET jump to org file %s"
5192 (abbreviate-file-name buffer-file-name))))
5193 (items (if (consp org-agenda-show-log)
5194 org-agenda-show-log
5195 (if (eq org-agenda-show-log 'clockcheck)
5196 '(clock)
5197 org-agenda-log-mode-items)))
5198 (parts
5199 (delq nil
5200 (list
5201 (if (memq 'closed items) (concat "\\<" org-closed-string))
5202 (if (memq 'clock items) (concat "\\<" org-clock-string))
5203 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5204 (parts-re (if parts (mapconcat 'identity parts "\\|")
5205 (error "`org-agenda-log-mode-items' is empty")))
5206 (regexp (concat
5207 "\\(" parts-re "\\)"
5208 " *\\["
5209 (regexp-quote
5210 (substring
5211 (format-time-string
5212 (car org-time-stamp-formats)
5213 (apply 'encode-time ; DATE bound by calendar
5214 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5215 1 11))))
5216 (org-agenda-search-headline-for-time nil)
5217 marker hdmarker priority category org-category-pos tags closedp
5218 statep clockp state ee txt extra timestr rest clocked)
5219 (goto-char (point-min))
5220 (while (re-search-forward regexp nil t)
5221 (catch :skip
5222 (org-agenda-skip)
5223 (setq marker (org-agenda-new-marker (match-beginning 0))
5224 closedp (equal (match-string 1) org-closed-string)
5225 statep (equal (string-to-char (match-string 1)) ?-)
5226 clockp (not (or closedp statep))
5227 state (and statep (match-string 2))
5228 category (org-get-category (match-beginning 0))
5229 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5230 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5231 (when (string-match "\\]" timestr)
5232 ;; substring should only run to end of time stamp
5233 (setq rest (substring timestr (match-end 0))
5234 timestr (substring timestr 0 (match-end 0)))
5235 (if (and (not closedp) (not statep)
5236 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5237 rest))
5238 (progn (setq timestr (concat (substring timestr 0 -1)
5239 "-" (match-string 1 rest) "]"))
5240 (setq clocked (match-string 2 rest)))
5241 (setq clocked "-")))
5242 (save-excursion
5243 (setq extra
5244 (cond
5245 ((not org-agenda-log-mode-add-notes) nil)
5246 (statep
5247 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5248 (match-string 1)))
5249 (clockp
5250 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5251 (match-string 1)))))
5252 (if (not (re-search-backward org-outline-regexp-bol nil t))
5253 (setq txt org-agenda-no-heading-message)
5254 (goto-char (match-beginning 0))
5255 (setq hdmarker (org-agenda-new-marker)
5256 tags (org-get-tags-at))
5257 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5258 (setq txt (match-string 1))
5259 (when extra
5260 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5261 (setq txt (concat (substring txt 0 (match-beginning 1))
5262 " - " extra " " (match-string 2 txt)))
5263 (setq txt (concat txt " - " extra))))
5264 (setq txt (org-agenda-format-item
5265 (cond
5266 (closedp "Closed: ")
5267 (statep (concat "State: (" state ")"))
5268 (t (concat "Clocked: (" clocked ")")))
5269 txt category tags timestr)))
5270 (setq priority 100000)
5271 (org-add-props txt props
5272 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5273 'priority priority 'org-category category
5274 'org-category-position org-category-pos
5275 'type "closed" 'date date
5276 'undone-face 'org-warning 'done-face 'org-agenda-done)
5277 (push txt ee))
5278 (goto-char (point-at-eol))))
5279 (nreverse ee)))
5281 (defun org-agenda-show-clocking-issues ()
5282 "Add overlays, showing issues with clocking.
5283 See also the user option `org-agenda-clock-consistency-checks'."
5284 (interactive)
5285 (let* ((pl org-agenda-clock-consistency-checks)
5286 (re (concat "^[ \t]*"
5287 org-clock-string
5288 "[ \t]+"
5289 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5290 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5291 (tlstart 0.)
5292 (tlend 0.)
5293 (maxtime (org-hh:mm-string-to-minutes
5294 (or (plist-get pl :max-duration) "24:00")))
5295 (mintime (org-hh:mm-string-to-minutes
5296 (or (plist-get pl :min-duration) 0)))
5297 (maxgap (org-hh:mm-string-to-minutes
5298 ;; default 30:00 means never complain
5299 (or (plist-get pl :max-gap) "30:00")))
5300 (gapok (mapcar 'org-hh:mm-string-to-minutes
5301 (plist-get pl :gap-ok-around)))
5302 (def-face (or (plist-get pl :default-face)
5303 '((:background "DarkRed") (:foreground "white"))))
5304 issue face m te ts dt ov)
5305 (goto-char (point-min))
5306 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5307 (setq issue nil face def-face)
5308 (catch 'next
5309 (setq m (org-get-at-bol 'org-marker)
5310 te nil ts nil)
5311 (unless (and m (markerp m))
5312 (setq issue "No valid clock line") (throw 'next t))
5313 (org-with-point-at m
5314 (save-excursion
5315 (goto-char (point-at-bol))
5316 (unless (looking-at re)
5317 (error "No valid Clock line")
5318 (throw 'next t))
5319 (unless (match-end 3)
5320 (setq issue "No end time"
5321 face (or (plist-get pl :no-end-time-face) face))
5322 (throw 'next t))
5323 (setq ts (match-string 1)
5324 te (match-string 3)
5325 ts (org-float-time
5326 (apply 'encode-time (org-parse-time-string ts)))
5327 te (org-float-time
5328 (apply 'encode-time (org-parse-time-string te)))
5329 dt (- te ts))))
5330 (cond
5331 ((> dt (* 60 maxtime))
5332 ;; a very long clocking chunk
5333 (setq issue (format "Clocking interval is very long: %s"
5334 (org-minutes-to-hh:mm-string
5335 (floor (/ (float dt) 60.))))
5336 face (or (plist-get pl :long-face) face)))
5337 ((< dt (* 60 mintime))
5338 ;; a very short clocking chunk
5339 (setq issue (format "Clocking interval is very short: %s"
5340 (org-minutes-to-hh:mm-string
5341 (floor (/ (float dt) 60.))))
5342 face (or (plist-get pl :short-face) face)))
5343 ((and (> tlend 0) (< ts tlend))
5344 ;; Two clock entries are overlapping
5345 (setq issue (format "Clocking overlap: %d minutes"
5346 (/ (- tlend ts) 60))
5347 face (or (plist-get pl :overlap-face) face)))
5348 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5349 ;; There is a gap, lets see if we need to report it
5350 (unless (org-agenda-check-clock-gap tlend ts gapok)
5351 (setq issue (format "Clocking gap: %d minutes"
5352 (/ (- ts tlend) 60))
5353 face (or (plist-get pl :gap-face) face))))
5354 (t nil)))
5355 (setq tlend (or te tlend) tlstart (or ts tlstart))
5356 (when issue
5357 ;; OK, there was some issue, add an overlay to show the issue
5358 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5359 (overlay-put ov 'before-string
5360 (concat
5361 (org-add-props
5362 (format "%-43s" (concat " " issue))
5364 'face face)
5365 "\n"))
5366 (overlay-put ov 'evaporate t)))))
5368 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5369 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5370 (catch 'exit
5371 (unless ok-list
5372 ;; there are no OK times for gaps...
5373 (throw 'exit nil))
5374 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5375 ;; This is more than 24 hours, so it is OK.
5376 ;; because we have at least one OK time, that must be in the
5377 ;; 24 hour interval.
5378 (throw 'exit t))
5379 ;; We have a shorter gap.
5380 ;; Now we have to get the minute of the day when these times are
5381 (let* ((t1dec (decode-time (seconds-to-time t1)))
5382 (t2dec (decode-time (seconds-to-time t2)))
5383 ;; compute the minute on the day
5384 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5385 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5386 (when (< min2 min1)
5387 ;; if min2 is smaller than min1, this means it is on the next day.
5388 ;; Wrap it to after midnight.
5389 (setq min2 (+ min2 1440)))
5390 ;; Now check if any of the OK times is in the gap
5391 (mapc (lambda (x)
5392 ;; Wrap the time to after midnight if necessary
5393 (if (< x min1) (setq x (+ x 1440)))
5394 ;; Check if in interval
5395 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5396 ok-list)
5397 ;; Nope, this gap is not OK
5398 nil)))
5400 (defun org-agenda-get-deadlines ()
5401 "Return the deadline information for agenda display."
5402 (let* ((props (list 'mouse-face 'highlight
5403 'org-not-done-regexp org-not-done-regexp
5404 'org-todo-regexp org-todo-regexp
5405 'org-complex-heading-regexp org-complex-heading-regexp
5406 'help-echo
5407 (format "mouse-2 or RET jump to org file %s"
5408 (abbreviate-file-name buffer-file-name))))
5409 (regexp org-deadline-time-regexp)
5410 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5411 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5412 d2 diff dfrac wdays pos pos1 category org-category-pos
5413 tags suppress-prewarning ee txt head face s todo-state
5414 show-all upcomingp donep timestr)
5415 (goto-char (point-min))
5416 (while (re-search-forward regexp nil t)
5417 (setq suppress-prewarning nil)
5418 (catch :skip
5419 (org-agenda-skip)
5420 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5421 (save-match-data
5422 (string-match org-scheduled-time-regexp
5423 (buffer-substring (point-at-bol)
5424 (point-at-eol)))))
5425 (setq suppress-prewarning
5426 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5427 org-agenda-skip-deadline-prewarning-if-scheduled
5428 0)))
5429 (setq s (match-string 1)
5430 txt nil
5431 pos (1- (match-beginning 1))
5432 todo-state (save-match-data (org-get-todo-state))
5433 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5434 (member todo-state
5435 org-agenda-repeating-timestamp-show-all))
5436 d2 (org-time-string-to-absolute
5437 (match-string 1) d1 'past show-all
5438 (current-buffer) pos)
5439 diff (- d2 d1)
5440 wdays (if suppress-prewarning
5441 (let ((org-deadline-warning-days suppress-prewarning))
5442 (org-get-wdays s))
5443 (org-get-wdays s))
5444 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5445 upcomingp (and todayp (> diff 0)))
5446 ;; When to show a deadline in the calendar:
5447 ;; If the expiration is within wdays warning time.
5448 ;; Past-due deadlines are only shown on the current date
5449 (if (and (or (and (<= diff wdays)
5450 (and todayp (not org-agenda-only-exact-dates)))
5451 (= diff 0)))
5452 (save-excursion
5453 ;; (setq todo-state (org-get-todo-state))
5454 (setq donep (member todo-state org-done-keywords))
5455 (if (and donep
5456 (or org-agenda-skip-deadline-if-done
5457 (not (= diff 0))))
5458 (setq txt nil)
5459 (setq category (org-get-category)
5460 org-category-pos (get-text-property (point) 'org-category-position))
5461 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5462 (setq txt org-agenda-no-heading-message)
5463 (goto-char (match-end 0))
5464 (setq pos1 (match-beginning 0))
5465 (setq tags (org-get-tags-at pos1))
5466 (setq head (buffer-substring-no-properties
5467 (point)
5468 (progn (skip-chars-forward "^\r\n")
5469 (point))))
5470 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5471 (setq timestr
5472 (concat (substring s (match-beginning 1)) " "))
5473 (setq timestr 'time))
5474 (setq txt (org-agenda-format-item
5475 (if (= diff 0)
5476 (car org-agenda-deadline-leaders)
5477 (if (functionp
5478 (nth 1 org-agenda-deadline-leaders))
5479 (funcall
5480 (nth 1 org-agenda-deadline-leaders)
5481 diff date)
5482 (format (nth 1 org-agenda-deadline-leaders)
5483 diff)))
5484 head category tags
5485 (if (not (= diff 0)) nil timestr)))))
5486 (when txt
5487 (setq face (org-agenda-deadline-face dfrac))
5488 (org-add-props txt props
5489 'org-marker (org-agenda-new-marker pos)
5490 'org-hd-marker (org-agenda-new-marker pos1)
5491 'priority (+ (- diff)
5492 (org-get-priority txt))
5493 'org-category category
5494 'org-category-position org-category-pos
5495 'todo-state todo-state
5496 'type (if upcomingp "upcoming-deadline" "deadline")
5497 'date (if upcomingp date d2)
5498 'face (if donep 'org-agenda-done face)
5499 'undone-face face 'done-face 'org-agenda-done)
5500 (push txt ee))))))
5501 (nreverse ee)))
5503 (defun org-agenda-deadline-face (fraction)
5504 "Return the face to displaying a deadline item.
5505 FRACTION is what fraction of the head-warning time has passed."
5506 (let ((faces org-agenda-deadline-faces) f)
5507 (catch 'exit
5508 (while (setq f (pop faces))
5509 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5511 (defun org-agenda-get-scheduled (&optional deadline-results)
5512 "Return the scheduled information for agenda display."
5513 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5514 'org-todo-regexp org-todo-regexp
5515 'org-complex-heading-regexp org-complex-heading-regexp
5516 'done-face 'org-agenda-done
5517 'mouse-face 'highlight
5518 'help-echo
5519 (format "mouse-2 or RET jump to org file %s"
5520 (abbreviate-file-name buffer-file-name))))
5521 (regexp org-scheduled-time-regexp)
5522 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5523 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5525 (deadline-position-alist
5526 (mapcar (lambda (a) (and (setq mm (get-text-property
5527 0 'org-hd-marker a))
5528 (cons (marker-position mm) a)))
5529 deadline-results))
5530 d2 diff pos pos1 category org-category-pos tags donep
5531 ee txt head pastschedp todo-state face timestr s habitp show-all)
5532 (goto-char (point-min))
5533 (while (re-search-forward regexp nil t)
5534 (catch :skip
5535 (org-agenda-skip)
5536 (setq s (match-string 1)
5537 txt nil
5538 pos (1- (match-beginning 1))
5539 todo-state (save-match-data (org-get-todo-state))
5540 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5541 (member todo-state
5542 org-agenda-repeating-timestamp-show-all))
5543 d2 (org-time-string-to-absolute
5544 (match-string 1) d1 'past show-all
5545 (current-buffer) pos)
5546 diff (- d2 d1))
5547 (setq pastschedp (and todayp (< diff 0)))
5548 ;; When to show a scheduled item in the calendar:
5549 ;; If it is on or past the date.
5550 (when (or (and (< diff 0)
5551 (< (abs diff) org-scheduled-past-days)
5552 (and todayp (not org-agenda-only-exact-dates)))
5553 (= diff 0))
5554 (save-excursion
5555 (setq donep (member todo-state org-done-keywords))
5556 (if (and donep
5557 (or org-agenda-skip-scheduled-if-done
5558 (not (= diff 0))
5559 (and (functionp 'org-is-habit-p)
5560 (org-is-habit-p))))
5561 (setq txt nil)
5562 (setq habitp (and (functionp 'org-is-habit-p)
5563 (org-is-habit-p)))
5564 (setq category (org-get-category)
5565 org-category-pos (get-text-property (point) 'org-category-position))
5566 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5567 (setq txt org-agenda-no-heading-message)
5568 (goto-char (match-end 0))
5569 (setq pos1 (match-beginning 0))
5570 (if habitp
5571 (if (or (not org-habit-show-habits)
5572 (and (not todayp)
5573 org-habit-show-habits-only-for-today))
5574 (throw :skip nil))
5575 (if (and
5576 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5577 (and org-agenda-skip-scheduled-if-deadline-is-shown
5578 pastschedp))
5579 (setq mm (assoc pos1 deadline-position-alist)))
5580 (throw :skip nil)))
5581 (setq tags (org-get-tags-at))
5582 (setq head (buffer-substring-no-properties
5583 (point)
5584 (progn (skip-chars-forward "^\r\n") (point))))
5585 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5586 (setq timestr
5587 (concat (substring s (match-beginning 1)) " "))
5588 (setq timestr 'time))
5589 (setq txt (org-agenda-format-item
5590 (if (= diff 0)
5591 (car org-agenda-scheduled-leaders)
5592 (format (nth 1 org-agenda-scheduled-leaders)
5593 (- 1 diff)))
5594 head category tags
5595 (if (not (= diff 0)) nil timestr)
5596 nil habitp))))
5597 (when txt
5598 (setq face
5599 (cond
5600 ((and (not habitp) pastschedp)
5601 'org-scheduled-previously)
5602 (todayp 'org-scheduled-today)
5603 (t 'org-scheduled))
5604 habitp (and habitp (org-habit-parse-todo)))
5605 (org-add-props txt props
5606 'undone-face face
5607 'face (if donep 'org-agenda-done face)
5608 'org-marker (org-agenda-new-marker pos)
5609 'org-hd-marker (org-agenda-new-marker pos1)
5610 'type (if pastschedp "past-scheduled" "scheduled")
5611 'date (if pastschedp d2 date)
5612 'priority (if habitp
5613 (org-habit-get-priority habitp)
5614 (+ 94 (- 5 diff) (org-get-priority txt)))
5615 'org-category category
5616 'org-category-position org-category-pos
5617 'org-habit-p habitp
5618 'todo-state todo-state)
5619 (push txt ee))))))
5620 (nreverse ee)))
5622 (defun org-agenda-get-blocks ()
5623 "Return the date-range information for agenda display."
5624 (let* ((props (list 'face nil
5625 'org-not-done-regexp org-not-done-regexp
5626 'org-todo-regexp org-todo-regexp
5627 'org-complex-heading-regexp org-complex-heading-regexp
5628 'mouse-face 'highlight
5629 'help-echo
5630 (format "mouse-2 or RET jump to org file %s"
5631 (abbreviate-file-name buffer-file-name))))
5632 (regexp org-tr-regexp)
5633 (d0 (calendar-absolute-from-gregorian date))
5634 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5635 todo-state tags pos head donep)
5636 (goto-char (point-min))
5637 (while (re-search-forward regexp nil t)
5638 (catch :skip
5639 (org-agenda-skip)
5640 (setq pos (point))
5641 (let ((start-time (match-string 1))
5642 (end-time (match-string 2)))
5643 (setq s1 (match-string 1)
5644 s2 (match-string 2)
5645 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5646 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5647 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5648 ;; Only allow days between the limits, because the normal
5649 ;; date stamps will catch the limits.
5650 (save-excursion
5651 (setq todo-state (org-get-todo-state))
5652 (setq donep (member todo-state org-done-keywords))
5653 (if (and donep org-agenda-skip-timestamp-if-done)
5654 (throw :skip t))
5655 (setq marker (org-agenda-new-marker (point)))
5656 (setq category (org-get-category)
5657 org-category-pos (get-text-property (point) 'org-category-position))
5658 (if (not (re-search-backward org-outline-regexp-bol nil t))
5659 (setq txt org-agenda-no-heading-message)
5660 (goto-char (match-beginning 0))
5661 (setq hdmarker (org-agenda-new-marker (point)))
5662 (setq tags (org-get-tags-at))
5663 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5664 (setq head (match-string 1))
5665 (let ((remove-re
5666 (if org-agenda-remove-timeranges-from-blocks
5667 (concat
5668 "<" (regexp-quote s1) ".*?>"
5669 "--"
5670 "<" (regexp-quote s2) ".*?>")
5671 nil)))
5672 (setq txt (org-agenda-format-item
5673 (format
5674 (nth (if (= d1 d2) 0 1)
5675 org-agenda-timerange-leaders)
5676 (1+ (- d0 d1)) (1+ (- d2 d1)))
5677 head category tags
5678 (cond ((and (= d1 d0) (= d2 d0))
5679 (concat "<" start-time ">--<" end-time ">"))
5680 ((= d1 d0)
5681 (concat "<" start-time ">"))
5682 ((= d2 d0)
5683 (concat "<" end-time ">"))
5684 (t nil))
5685 remove-re))))
5686 (org-add-props txt props
5687 'org-marker marker 'org-hd-marker hdmarker
5688 'type "block" 'date date
5689 'todo-state todo-state
5690 'priority (org-get-priority txt) 'org-category category
5691 'org-category-position org-category-pos)
5692 (push txt ee))))
5693 (goto-char pos)))
5694 ;; Sort the entries by expiration date.
5695 (nreverse ee)))
5697 ;;; Agenda presentation and sorting
5699 (defvar org-prefix-has-time nil
5700 "A flag, set by `org-compile-prefix-format'.
5701 The flag is set if the currently compiled format contains a `%t'.")
5702 (defvar org-prefix-has-tag nil
5703 "A flag, set by `org-compile-prefix-format'.
5704 The flag is set if the currently compiled format contains a `%T'.")
5705 (defvar org-prefix-has-effort nil
5706 "A flag, set by `org-compile-prefix-format'.
5707 The flag is set if the currently compiled format contains a `%e'.")
5708 (defvar org-prefix-category-length nil
5709 "Used by `org-compile-prefix-format' to remember the category field width.")
5710 (defvar org-prefix-category-max-length nil
5711 "Used by `org-compile-prefix-format' to remember the category field width.")
5713 (defun org-agenda-get-category-icon (category)
5714 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5715 (dolist (entry org-agenda-category-icon-alist)
5716 (when (org-string-match-p (car entry) category)
5717 (if (listp (cadr entry))
5718 (return (cadr entry))
5719 (return (apply 'create-image (cdr entry)))))))
5721 (defun org-agenda-format-item (extra txt &optional category tags dotime
5722 remove-re habitp)
5723 "Format TXT to be inserted into the agenda buffer.
5724 In particular, it adds the prefix and corresponding text properties. EXTRA
5725 must be a string and replaces the `%s' specifier in the prefix format.
5726 CATEGORY (string, symbol or nil) may be used to overrule the default
5727 category taken from local variable or file name. It will replace the `%c'
5728 specifier in the format. DOTIME, when non-nil, indicates that a
5729 time-of-day should be extracted from TXT for sorting of this entry, and for
5730 the `%t' specifier in the format. When DOTIME is a string, this string is
5731 searched for a time before TXT is. TAGS can be the tags of the headline.
5732 Any match of REMOVE-RE will be removed from TXT."
5733 ;; We keep the org-prefix-* variable values along with a compiled
5734 ;; formatter, so that multiple agendas existing at the same time, do
5735 ;; not step on each other toes.
5737 ;; It was inconvenient to make these variables buffer local in
5738 ;; Agenda buffers, because this function expects to be called with
5739 ;; the buffer where item comes from being current, and not agenda
5740 ;; buffer
5741 (let* ((org-prefix-format-compiled
5742 (with-current-buffer org-agenda-buffer
5743 org-prefix-format-compiled))
5744 (bindings (car org-prefix-format-compiled))
5745 (formatter (cadr org-prefix-format-compiled)))
5746 (loop for (var value) in bindings
5747 do (set var value))
5748 (save-match-data
5749 ;; Diary entries sometimes have extra whitespace at the beginning
5750 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5752 ;; Fix the tags part in txt
5753 (setq txt (org-agenda-fix-displayed-tags
5754 txt tags
5755 org-agenda-show-inherited-tags
5756 org-agenda-hide-tags-regexp))
5757 (let* ((category (or category
5758 (if (stringp org-category)
5759 org-category
5760 (and org-category (symbol-name org-category)))
5761 (if buffer-file-name
5762 (file-name-sans-extension
5763 (file-name-nondirectory buffer-file-name))
5764 "")))
5765 (category-icon (org-agenda-get-category-icon category))
5766 (category-icon (if category-icon
5767 (propertize " " 'display category-icon)
5768 ""))
5769 ;; time, tag, effort are needed for the eval of the prefix format
5770 (tag (if tags (nth (1- (length tags)) tags) ""))
5771 time effort neffort
5772 (ts (if dotime (concat
5773 (if (stringp dotime) dotime "")
5774 (and org-agenda-search-headline-for-time txt))))
5775 (time-of-day (and dotime (org-get-time-of-day ts)))
5776 stamp plain s0 s1 s2 rtn srp l
5777 duration thecategory)
5778 (and (eq major-mode 'org-mode) buffer-file-name
5779 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5780 (when (and dotime time-of-day)
5781 ;; Extract starting and ending time and move them to prefix
5782 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5783 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5784 (setq s0 (match-string 0 ts)
5785 srp (and stamp (match-end 3))
5786 s1 (match-string (if plain 1 2) ts)
5787 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5789 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5790 ;; them, we might want to remove them there to avoid duplication.
5791 ;; The user can turn this off with a variable.
5792 (if (and org-prefix-has-time
5793 org-agenda-remove-times-when-in-prefix (or stamp plain)
5794 (string-match (concat (regexp-quote s0) " *") txt)
5795 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5796 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5797 (= (match-beginning 0) 0)
5799 (setq txt (replace-match "" nil nil txt))))
5800 ;; Normalize the time(s) to 24 hour
5801 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5802 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5804 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5805 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5806 (setq s2
5807 (org-minutes-to-hh:mm-string
5808 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5810 ;; Compute the duration
5811 (when s2
5812 (setq duration (- (org-hh:mm-string-to-minutes s2)
5813 (org-hh:mm-string-to-minutes s1)))))
5815 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5816 txt)
5817 ;; Tags are in the string
5818 (if (or (eq org-agenda-remove-tags t)
5819 (and org-agenda-remove-tags
5820 org-prefix-has-tag))
5821 (setq txt (replace-match "" t t txt))
5822 (setq txt (replace-match
5823 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5824 (match-string 2 txt))
5825 t t txt))))
5826 (when (eq major-mode 'org-mode)
5827 (setq effort
5828 (condition-case nil
5829 (org-get-effort
5830 (or (get-text-property 0 'org-hd-marker txt)
5831 (get-text-property 0 'org-marker txt)))
5832 (error nil)))
5833 (when effort
5834 (setq neffort (org-duration-string-to-minutes effort)
5835 effort (setq effort (concat "[" effort "]")))))
5836 ;; prevent erroring out with %e format when there is no effort
5837 (or effort (setq effort ""))
5839 (when remove-re
5840 (while (string-match remove-re txt)
5841 (setq txt (replace-match "" t t txt))))
5843 ;; Set org-heading property on `txt' to mark the start of the
5844 ;; heading.
5845 (add-text-properties 0 (length txt) '(org-heading t) txt)
5847 ;; Prepare the variables needed in the eval of the compiled format
5848 (setq time (cond (s2 (concat
5849 (org-agenda-time-of-day-to-ampm-maybe s1)
5850 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5851 (if org-agenda-timegrid-use-ampm " ")))
5852 (s1 (concat
5853 (org-agenda-time-of-day-to-ampm-maybe s1)
5854 (if org-agenda-timegrid-use-ampm
5855 "........ "
5856 "......")))
5857 (t ""))
5858 extra (or (and (not habitp) extra) "")
5859 category (if (symbolp category) (symbol-name category) category)
5860 thecategory (copy-sequence category))
5861 (if (string-match org-bracket-link-regexp category)
5862 (progn
5863 (setq l (if (match-end 3)
5864 (- (match-end 3) (match-beginning 3))
5865 (- (match-end 1) (match-beginning 1))))
5866 (when (< l (or org-prefix-category-length 0))
5867 (setq category (copy-sequence category))
5868 (org-add-props category nil
5869 'extra-space (make-string
5870 (- org-prefix-category-length l 1) ?\ ))))
5871 (if (and org-prefix-category-max-length
5872 (>= (length category) org-prefix-category-max-length))
5873 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5874 ;; Evaluate the compiled format
5875 (setq rtn (concat (eval formatter) txt))
5877 ;; And finally add the text properties
5878 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5879 (org-add-props rtn nil
5880 'org-category (if thecategory (downcase thecategory) category)
5881 'tags (mapcar 'org-downcase-keep-props tags)
5882 'org-highest-priority org-highest-priority
5883 'org-lowest-priority org-lowest-priority
5884 'time-of-day time-of-day
5885 'duration duration
5886 'effort effort
5887 'effort-minutes neffort
5888 'txt txt
5889 'time time
5890 'extra extra
5891 'format org-prefix-format-compiled
5892 'dotime dotime)))))
5894 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5895 "Remove tags string from TXT, and add a modified list of tags.
5896 The modified list may contain inherited tags, and tags matched by
5897 `org-agenda-hide-tags-regexp' will be removed."
5898 (when (or add-inherited hide-re)
5899 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5900 (setq txt (substring txt 0 (match-beginning 0))))
5901 (setq tags
5902 (delq nil
5903 (mapcar (lambda (tg)
5904 (if (or (and hide-re (string-match hide-re tg))
5905 (and (not add-inherited)
5906 (get-text-property 0 'inherited tg)))
5908 tg))
5909 tags)))
5910 (when tags
5911 (let ((have-i (get-text-property 0 'inherited (car tags)))
5913 (setq txt (concat txt " :"
5914 (mapconcat
5915 (lambda (x)
5916 (setq i (get-text-property 0 'inherited x))
5917 (if (and have-i (not i))
5918 (progn
5919 (setq have-i nil)
5920 (concat ":" x))
5922 tags ":")
5923 (if have-i "::" ":"))))))
5924 txt)
5926 (defun org-downcase-keep-props (s)
5927 (let ((props (text-properties-at 0 s)))
5928 (setq s (downcase s))
5929 (add-text-properties 0 (length s) props s)
5932 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5933 (defvar org-agenda-sorting-strategy-selected nil)
5935 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5936 (catch 'exit
5937 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5938 ((and todayp (member 'today (car org-agenda-time-grid))))
5939 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5940 ((member 'weekly (car org-agenda-time-grid)))
5941 (t (throw 'exit list)))
5942 (let* ((have (delq nil (mapcar
5943 (lambda (x) (get-text-property 1 'time-of-day x))
5944 list)))
5945 (string (nth 1 org-agenda-time-grid))
5946 (gridtimes (nth 2 org-agenda-time-grid))
5947 (req (car org-agenda-time-grid))
5948 (remove (member 'remove-match req))
5949 new time)
5950 (if (and (member 'require-timed req) (not have))
5951 ;; don't show empty grid
5952 (throw 'exit list))
5953 (while (setq time (pop gridtimes))
5954 (unless (and remove (member time have))
5955 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5956 (push (org-agenda-format-item
5957 nil string "" nil
5958 (concat (substring time 0 -2) ":" (substring time -2)))
5959 new)
5960 (put-text-property
5961 2 (length (car new)) 'face 'org-time-grid (car new))))
5962 (when (and todayp org-agenda-show-current-time-in-grid)
5963 (push (org-agenda-format-item
5965 org-agenda-current-time-string
5966 "" nil
5967 (format-time-string "%H:%M "))
5968 new)
5969 (put-text-property
5970 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5972 (if (member 'time-up org-agenda-sorting-strategy-selected)
5973 (append new list)
5974 (append list new)))))
5976 (defun org-compile-prefix-format (key)
5977 "Compile the prefix format into a Lisp form that can be evaluated.
5978 The resulting form and associated variable bindings is returned
5979 and stored in the variable `org-prefix-format-compiled'."
5980 (setq org-prefix-has-time nil org-prefix-has-tag nil
5981 org-prefix-category-length nil
5982 org-prefix-has-effort nil)
5983 (let ((s (cond
5984 ((stringp org-agenda-prefix-format)
5985 org-agenda-prefix-format)
5986 ((assq key org-agenda-prefix-format)
5987 (cdr (assq key org-agenda-prefix-format)))
5988 (t " %-12:c%?-12t% s")))
5989 (start 0)
5990 varform vars var e c f opt)
5991 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5992 s start)
5993 (setq var (or (cdr (assoc (match-string 4 s)
5994 '(("c" . category) ("t" . time) ("s" . extra)
5995 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5996 'eval)
5997 c (or (match-string 3 s) "")
5998 opt (match-beginning 1)
5999 start (1+ (match-beginning 0)))
6000 (if (equal var 'time) (setq org-prefix-has-time t))
6001 (if (equal var 'tag) (setq org-prefix-has-tag t))
6002 (if (equal var 'effort) (setq org-prefix-has-effort t))
6003 (setq f (concat "%" (match-string 2 s) "s"))
6004 (when (equal var 'category)
6005 (setq org-prefix-category-length
6006 (floor (abs (string-to-number (match-string 2 s)))))
6007 (setq org-prefix-category-max-length
6008 (let ((x (match-string 2 s)))
6009 (save-match-data
6010 (if (string-match "\\.[0-9]+" x)
6011 (string-to-number (substring (match-string 0 x) 1)))))))
6012 (if (eq var 'eval)
6013 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6014 (if opt
6015 (setq varform
6016 `(if (equal "" ,var)
6018 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6019 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6020 (setq s (replace-match "%s" t nil s))
6021 (push varform vars))
6022 (setq vars (nreverse vars))
6023 (with-current-buffer org-agenda-buffer
6024 (setq org-prefix-format-compiled
6025 (list
6026 `((org-prefix-has-time ,org-prefix-has-time)
6027 (org-prefix-has-tag ,org-prefix-has-tag)
6028 (org-prefix-category-length ,org-prefix-category-length)
6029 (org-prefix-has-effort ,org-prefix-has-effort))
6030 `(format ,s ,@vars))))))
6032 (defun org-set-sorting-strategy (key)
6033 (if (symbolp (car org-agenda-sorting-strategy))
6034 ;; the old format
6035 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6036 (setq org-agenda-sorting-strategy-selected
6037 (or (cdr (assq key org-agenda-sorting-strategy))
6038 (cdr (assq 'agenda org-agenda-sorting-strategy))
6039 '(time-up category-keep priority-down)))))
6041 (defun org-get-time-of-day (s &optional string mod24)
6042 "Check string S for a time of day.
6043 If found, return it as a military time number between 0 and 2400.
6044 If not found, return nil.
6045 The optional STRING argument forces conversion into a 5 character wide string
6046 HH:MM."
6047 (save-match-data
6048 (when
6049 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6050 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6051 (let* ((h (string-to-number (match-string 1 s)))
6052 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6053 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6054 (am-p (equal ampm "am"))
6055 (h1 (cond ((not ampm) h)
6056 ((= h 12) (if am-p 0 12))
6057 (t (+ h (if am-p 0 12)))))
6058 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6059 (mod h1 24) h1))
6060 (t0 (+ (* 100 h2) m))
6061 (t1 (concat (if (>= h1 24) "+" " ")
6062 (if (and org-agenda-time-leading-zero
6063 (< t0 1000)) "0" "")
6064 (if (< t0 100) "0" "")
6065 (if (< t0 10) "0" "")
6066 (int-to-string t0))))
6067 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6069 (defvar org-agenda-before-sorting-filter-function nil
6070 "Function to be applied to agenda items prior to sorting.
6071 Prior to sorting also means just before they are inserted into the agenda.
6073 To aid sorting, you may revisit the original entries and add more text
6074 properties which will later be used by the sorting functions.
6076 The function should take a string argument, an agenda line.
6077 It has access to the text properties in that line, which contain among
6078 other things, the property `org-hd-marker' that points to the entry
6079 where the line comes from. Note that not all lines going into the agenda
6080 have this property, only most.
6082 The function should return the modified string. It is probably best
6083 to ONLY change text properties.
6085 You can also use this function as a filter, by returning nil for lines
6086 you don't want to have in the agenda at all. For this application, you
6087 could bind the variable in the options section of a custom command.")
6089 (defun org-finalize-agenda-entries (list &optional nosort)
6090 "Sort and concatenate the agenda items."
6091 (setq list (mapcar 'org-agenda-highlight-todo list))
6092 (if nosort
6093 list
6094 (when org-agenda-before-sorting-filter-function
6095 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6096 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6098 (defun org-agenda-highlight-todo (x)
6099 (let ((org-done-keywords org-done-keywords-for-agenda)
6100 (case-fold-search nil)
6102 (if (eq x 'line)
6103 (save-excursion
6104 (beginning-of-line 1)
6105 (setq re (org-get-at-bol 'org-todo-regexp))
6106 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6107 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6108 (add-text-properties (match-beginning 0) (match-end 1)
6109 (list 'face (org-get-todo-face 1)))
6110 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6111 (delete-region (match-beginning 1) (1- (match-end 0)))
6112 (goto-char (match-beginning 1))
6113 (insert (format org-agenda-todo-keyword-format s)))))
6114 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6115 (setq re (get-text-property 0 'org-todo-regexp x))
6116 (when (and re
6117 ;; Test `pl' because if there's no heading content,
6118 ;; there's no point matching to highlight. Note
6119 ;; that if we didn't test `pl' first, and there
6120 ;; happened to be no keyword from `org-todo-regexp'
6121 ;; on this heading line, then the `equal' comparison
6122 ;; afterwards would spuriously succeed in the case
6123 ;; where `pl' is nil -- causing an args-out-of-range
6124 ;; error when we try to add text properties to text
6125 ;; that isn't there.
6127 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6128 x pl) pl))
6129 (add-text-properties
6130 (or (match-end 1) (match-end 0)) (match-end 0)
6131 (list 'face (org-get-todo-face (match-string 2 x)))
6133 (when (match-end 1)
6134 (setq x (concat (substring x 0 (match-end 1))
6135 (format org-agenda-todo-keyword-format
6136 (match-string 2 x))
6137 (org-add-props " " (text-properties-at 0 x))
6138 (substring x (match-end 3)))))))
6139 x)))
6141 (defsubst org-cmp-priority (a b)
6142 "Compare the priorities of string A and B."
6143 (let ((pa (or (get-text-property 1 'priority a) 0))
6144 (pb (or (get-text-property 1 'priority b) 0)))
6145 (cond ((> pa pb) +1)
6146 ((< pa pb) -1)
6147 (t nil))))
6149 (defsubst org-cmp-effort (a b)
6150 "Compare the effort values of string A and B."
6151 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6152 (ea (or (get-text-property 1 'effort-minutes a) def))
6153 (eb (or (get-text-property 1 'effort-minutes b) def)))
6154 (cond ((> ea eb) +1)
6155 ((< ea eb) -1)
6156 (t nil))))
6158 (defsubst org-cmp-category (a b)
6159 "Compare the string values of categories of strings A and B."
6160 (let ((ca (or (get-text-property 1 'org-category a) ""))
6161 (cb (or (get-text-property 1 'org-category b) "")))
6162 (cond ((string-lessp ca cb) -1)
6163 ((string-lessp cb ca) +1)
6164 (t nil))))
6166 (defsubst org-cmp-todo-state (a b)
6167 "Compare the todo states of strings A and B."
6168 (let* ((ma (or (get-text-property 1 'org-marker a)
6169 (get-text-property 1 'org-hd-marker a)))
6170 (mb (or (get-text-property 1 'org-marker b)
6171 (get-text-property 1 'org-hd-marker b)))
6172 (fa (and ma (marker-buffer ma)))
6173 (fb (and mb (marker-buffer mb)))
6174 (todo-kwds
6175 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6176 (and fb (with-current-buffer fb org-todo-keywords-1))))
6177 (ta (or (get-text-property 1 'todo-state a) ""))
6178 (tb (or (get-text-property 1 'todo-state b) ""))
6179 (la (- (length (member ta todo-kwds))))
6180 (lb (- (length (member tb todo-kwds))))
6181 (donepa (member ta org-done-keywords-for-agenda))
6182 (donepb (member tb org-done-keywords-for-agenda)))
6183 (cond ((and donepa (not donepb)) -1)
6184 ((and (not donepa) donepb) +1)
6185 ((< la lb) -1)
6186 ((< lb la) +1)
6187 (t nil))))
6189 (defsubst org-cmp-alpha (a b)
6190 "Compare the headlines, alphabetically."
6191 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6192 (plb (text-property-any 0 (length b) 'org-heading t b))
6193 (ta (and pla (substring a pla)))
6194 (tb (and plb (substring b plb))))
6195 (when pla
6196 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6197 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6198 (setq ta (substring ta (match-end 0))))
6199 (setq ta (downcase ta)))
6200 (when plb
6201 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6202 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6203 (setq tb (substring tb (match-end 0))))
6204 (setq tb (downcase tb)))
6205 (cond ((not ta) +1)
6206 ((not tb) -1)
6207 ((string-lessp ta tb) -1)
6208 ((string-lessp tb ta) +1)
6209 (t nil))))
6211 (defsubst org-cmp-tag (a b)
6212 "Compare the string values of the first tags of A and B."
6213 (let ((ta (car (last (get-text-property 1 'tags a))))
6214 (tb (car (last (get-text-property 1 'tags b)))))
6215 (cond ((not ta) +1)
6216 ((not tb) -1)
6217 ((string-lessp ta tb) -1)
6218 ((string-lessp tb ta) +1)
6219 (t nil))))
6221 (defsubst org-cmp-time (a b)
6222 "Compare the time-of-day values of strings A and B."
6223 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6224 (ta (or (get-text-property 1 'time-of-day a) def))
6225 (tb (or (get-text-property 1 'time-of-day b) def)))
6226 (cond ((< ta tb) -1)
6227 ((< tb ta) +1)
6228 (t nil))))
6230 (defsubst org-cmp-habit-p (a b)
6231 "Compare the todo states of strings A and B."
6232 (let ((ha (get-text-property 1 'org-habit-p a))
6233 (hb (get-text-property 1 'org-habit-p b)))
6234 (cond ((and ha (not hb)) -1)
6235 ((and (not ha) hb) +1)
6236 (t nil))))
6238 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6240 (defun org-entries-lessp (a b)
6241 "Predicate for sorting agenda entries."
6242 ;; The following variables will be used when the form is evaluated.
6243 ;; So even though the compiler complains, keep them.
6244 (let* ((ss org-agenda-sorting-strategy-selected)
6245 (time-up (and (org-em 'time-up 'time-down ss)
6246 (org-cmp-time a b)))
6247 (time-down (if time-up (- time-up) nil))
6248 (priority-up (and (org-em 'priority-up 'priority-down ss)
6249 (org-cmp-priority a b)))
6250 (priority-down (if priority-up (- priority-up) nil))
6251 (effort-up (and (org-em 'effort-up 'effort-down ss)
6252 (org-cmp-effort a b)))
6253 (effort-down (if effort-up (- effort-up) nil))
6254 (category-up (and (or (org-em 'category-up 'category-down ss)
6255 (memq 'category-keep ss))
6256 (org-cmp-category a b)))
6257 (category-down (if category-up (- category-up) nil))
6258 (category-keep (if category-up +1 nil))
6259 (tag-up (and (org-em 'tag-up 'tag-down ss)
6260 (org-cmp-tag a b)))
6261 (tag-down (if tag-up (- tag-up) nil))
6262 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6263 (org-cmp-todo-state a b)))
6264 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6265 (habit-up (and (org-em 'habit-up 'habit-down ss)
6266 (org-cmp-habit-p a b)))
6267 (habit-down (if habit-up (- habit-up) nil))
6268 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6269 (org-cmp-alpha a b)))
6270 (alpha-down (if alpha-up (- alpha-up) nil))
6271 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6272 user-defined-up user-defined-down)
6273 (if (and need-user-cmp org-agenda-cmp-user-defined
6274 (functionp org-agenda-cmp-user-defined))
6275 (setq user-defined-up
6276 (funcall org-agenda-cmp-user-defined a b)
6277 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6278 (cdr (assoc
6279 (eval (cons 'or org-agenda-sorting-strategy-selected))
6280 '((-1 . t) (1 . nil) (nil . nil))))))
6282 ;;; Agenda restriction lock
6284 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6285 "Overlay to mark the headline to which agenda commands are restricted.")
6286 (overlay-put org-agenda-restriction-lock-overlay
6287 'face 'org-agenda-restriction-lock)
6288 (overlay-put org-agenda-restriction-lock-overlay
6289 'help-echo "Agendas are currently limited to this subtree.")
6290 (org-detach-overlay org-agenda-restriction-lock-overlay)
6292 (defun org-agenda-set-restriction-lock (&optional type)
6293 "Set restriction lock for agenda, to current subtree or file.
6294 Restriction will be the file if TYPE is `file', or if type is the
6295 universal prefix '(4), or if the cursor is before the first headline
6296 in the file. Otherwise, restriction will be to the current subtree."
6297 (interactive "P")
6298 (and (equal type '(4)) (setq type 'file))
6299 (setq type (cond
6300 (type type)
6301 ((org-at-heading-p) 'subtree)
6302 ((condition-case nil (org-back-to-heading t) (error nil))
6303 'subtree)
6304 (t 'file)))
6305 (if (eq type 'subtree)
6306 (progn
6307 (setq org-agenda-restrict t)
6308 (setq org-agenda-overriding-restriction 'subtree)
6309 (put 'org-agenda-files 'org-restrict
6310 (list (buffer-file-name (buffer-base-buffer))))
6311 (org-back-to-heading t)
6312 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6313 (move-marker org-agenda-restrict-begin (point))
6314 (move-marker org-agenda-restrict-end
6315 (save-excursion (org-end-of-subtree t)))
6316 (message "Locking agenda restriction to subtree"))
6317 (put 'org-agenda-files 'org-restrict
6318 (list (buffer-file-name (buffer-base-buffer))))
6319 (setq org-agenda-restrict nil)
6320 (setq org-agenda-overriding-restriction 'file)
6321 (move-marker org-agenda-restrict-begin nil)
6322 (move-marker org-agenda-restrict-end nil)
6323 (message "Locking agenda restriction to file"))
6324 (setq current-prefix-arg nil)
6325 (org-agenda-maybe-redo))
6327 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6328 "Remove the agenda restriction lock."
6329 (interactive "P")
6330 (org-detach-overlay org-agenda-restriction-lock-overlay)
6331 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6332 (setq org-agenda-overriding-restriction nil)
6333 (setq org-agenda-restrict nil)
6334 (put 'org-agenda-files 'org-restrict nil)
6335 (move-marker org-agenda-restrict-begin nil)
6336 (move-marker org-agenda-restrict-end nil)
6337 (setq current-prefix-arg nil)
6338 (message "Agenda restriction lock removed")
6339 (or noupdate (org-agenda-maybe-redo)))
6341 (defun org-agenda-maybe-redo ()
6342 "If there is any window showing the agenda view, update it."
6343 (let ((w (get-buffer-window org-agenda-buffer-name t))
6344 (w0 (selected-window)))
6345 (when w
6346 (select-window w)
6347 (org-agenda-redo)
6348 (select-window w0)
6349 (if org-agenda-overriding-restriction
6350 (message "Agenda view shifted to new %s restriction"
6351 org-agenda-overriding-restriction)
6352 (message "Agenda restriction lock removed")))))
6354 ;;; Agenda commands
6356 (defun org-agenda-check-type (error &rest types)
6357 "Check if agenda buffer is of allowed type.
6358 If ERROR is non-nil, throw an error, otherwise just return nil."
6359 (if (memq org-agenda-type types)
6361 (if error
6362 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6363 nil)))
6365 (defun org-agenda-Quit (&optional arg)
6366 "Exit agenda by removing the window or the buffer"
6367 (interactive)
6368 (if org-agenda-columns-active
6369 (org-columns-quit)
6370 (let ((buf (current-buffer)))
6371 (if (eq org-agenda-window-setup 'other-frame)
6372 (progn
6373 (org-agenda-reset-markers)
6374 (kill-buffer buf)
6375 (org-columns-remove-overlays)
6376 (setq org-agenda-archives-mode nil)
6377 (delete-frame))
6378 (and (not (eq org-agenda-window-setup 'current-window))
6379 (not (one-window-p))
6380 (delete-window))
6381 (org-agenda-reset-markers)
6382 (kill-buffer buf)
6383 (org-columns-remove-overlays)
6384 (setq org-agenda-archives-mode nil)))
6385 ;; Maybe restore the pre-agenda window configuration.
6386 (and org-agenda-restore-windows-after-quit
6387 (not (eq org-agenda-window-setup 'other-frame))
6388 org-pre-agenda-window-conf
6389 (set-window-configuration org-pre-agenda-window-conf))))
6391 (defun org-agenda-quit ()
6392 "Exit agenda by killing agenda buffer or burying it when
6393 `org-agenda-sticky' is non-NIL"
6394 (interactive)
6395 (if org-agenda-columns-active
6396 (org-columns-quit)
6397 (if org-agenda-sticky
6398 (let ((buf (current-buffer)))
6399 (if (eq org-agenda-window-setup 'other-frame)
6400 (progn
6401 (delete-frame))
6402 (and (not (eq org-agenda-window-setup 'current-window))
6403 (not (one-window-p))
6404 (delete-window)))
6405 (with-current-buffer buf
6406 (bury-buffer)))
6407 (org-agenda-Quit))))
6409 (defun org-agenda-exit ()
6410 "Exit agenda by removing the window or the buffer.
6411 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6412 Org-mode buffers visited directly by the user will not be touched."
6413 (interactive)
6414 (org-release-buffers org-agenda-new-buffers)
6415 (setq org-agenda-new-buffers nil)
6416 (org-agenda-Quit))
6418 (defun org-agenda-kill-all-agenda-buffers ()
6419 "Kill all buffers in `org-agena-mode'.
6420 This is used when toggling sticky agendas. You can also explicitly invoke it
6421 with `C-c a C-k'."
6422 (interactive)
6423 (let (blist)
6424 (dolist (buf (buffer-list))
6425 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6426 (push buf blist)))
6427 (mapc 'kill-buffer blist)))
6429 (defun org-agenda-execute (arg)
6430 "Execute another agenda command, keeping same window.
6431 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6432 in the agenda."
6433 (interactive "P")
6434 (let ((org-agenda-window-setup 'current-window))
6435 (org-agenda arg)))
6437 (defun org-agenda-redo ()
6438 "Rebuild Agenda.
6439 When this is the global TODO list, a prefix argument will be interpreted."
6440 (interactive)
6441 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6442 (org-agenda-sticky nil)
6443 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6444 org-agenda-buffer-name))
6445 (org-agenda-keep-modes t)
6446 (tag-filter org-agenda-tag-filter)
6447 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6448 (cat-filter org-agenda-category-filter)
6449 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6450 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6451 (cols org-agenda-columns-active)
6452 (line (org-current-line))
6453 (window-line (- line (org-current-line (window-start))))
6454 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6455 (put 'org-agenda-tag-filter :preset-filter nil)
6456 (put 'org-agenda-category-filter :preset-filter nil)
6457 (and cols (org-columns-quit))
6458 (message "Rebuilding agenda buffer...")
6459 (org-let lprops '(eval org-agenda-redo-command))
6460 (setq org-agenda-undo-list nil
6461 org-agenda-pending-undo-list nil)
6462 (message "Rebuilding agenda buffer...done")
6463 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6464 (put 'org-agenda-category-filter :preset-filter cat-preset)
6465 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6466 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6467 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6468 (org-goto-line line)
6469 (recenter window-line)))
6471 (defvar org-global-tags-completion-table nil)
6472 (defvar org-agenda-filtered-by-category nil)
6473 (defvar org-agenda-filter-form nil)
6475 (defun org-agenda-filter-by-category (strip)
6476 "Keep only those lines in the agenda buffer that have a specific category.
6477 The category is that of the current line."
6478 (interactive "P")
6479 (if org-agenda-filtered-by-category
6480 (org-agenda-filter-show-all-cat)
6481 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6482 (if cat (org-agenda-filter-apply
6483 (list (concat (if strip "-" "+") cat)) 'category)
6484 (error "No category at point")))))
6486 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6487 "Keep only those lines in the agenda buffer that have a specific tag.
6488 The tag is selected with its fast selection letter, as configured.
6489 With prefix argument STRIP, remove all lines that do have the tag.
6490 A lisp caller can specify CHAR. NARROW means that the new tag should be
6491 used to narrow the search - the interactive user can also press `-' or `+'
6492 to switch to narrowing."
6493 (interactive "P")
6494 (let* ((alist org-tag-alist-for-agenda)
6495 (tag-chars (mapconcat
6496 (lambda (x) (if (and (not (symbolp (car x)))
6497 (cdr x))
6498 (char-to-string (cdr x))
6499 ""))
6500 alist ""))
6501 (efforts (org-split-string
6502 (or (cdr (assoc (concat org-effort-property "_ALL")
6503 org-global-properties))
6504 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6505 "")))
6506 (effort-op org-agenda-filter-effort-default-operator)
6507 (effort-prompt "")
6508 (inhibit-read-only t)
6509 (current org-agenda-tag-filter)
6510 maybe-refresh a n tag)
6511 (unless char
6512 (message
6513 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6514 (if narrow "Narrow" "Filter") tag-chars
6515 (if org-agenda-auto-exclude-function "[RET], " ""))
6516 (setq char (read-char-exclusive)))
6517 (when (member char '(?+ ?-))
6518 ;; Narrowing down
6519 (cond ((equal char ?-) (setq strip t narrow t))
6520 ((equal char ?+) (setq strip nil narrow t)))
6521 (message
6522 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6523 (setq char (read-char-exclusive)))
6524 (when (member char '(?< ?> ?= ??))
6525 ;; An effort operator
6526 (setq effort-op (char-to-string char))
6527 (setq alist nil) ; to make sure it will be interpreted as effort.
6528 (unless (equal char ??)
6529 (loop for i from 0 to 9 do
6530 (setq effort-prompt
6531 (concat
6532 effort-prompt " ["
6533 (if (= i 9) "0" (int-to-string (1+ i)))
6534 "]" (nth i efforts))))
6535 (message "Effort%s: %s " effort-op effort-prompt)
6536 (setq char (read-char-exclusive))
6537 (when (or (< char ?0) (> char ?9))
6538 (error "Need 1-9,0 to select effort"))))
6539 (when (equal char ?\t)
6540 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6541 (org-set-local 'org-global-tags-completion-table
6542 (org-global-tags-completion-table)))
6543 (let ((completion-ignore-case t))
6544 (setq tag (org-icompleting-read
6545 "Tag: " org-global-tags-completion-table))))
6546 (cond
6547 ((equal char ?\r)
6548 (org-agenda-filter-show-all-tag)
6549 (when org-agenda-auto-exclude-function
6550 (setq org-agenda-tag-filter '())
6551 (dolist (tag (org-agenda-get-represented-tags))
6552 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6553 (if modifier
6554 (push modifier org-agenda-tag-filter))))
6555 (if (not (null org-agenda-tag-filter))
6556 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6557 (setq maybe-refresh t))
6558 ((equal char ?/)
6559 (org-agenda-filter-show-all-tag)
6560 (when (get 'org-agenda-tag-filter :preset-filter)
6561 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6562 (setq maybe-refresh t))
6563 ((equal char ?. )
6564 (setq org-agenda-tag-filter
6565 (mapcar (lambda(tag) (concat "+" tag))
6566 (org-get-at-bol 'tags)))
6567 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6568 (setq maybe-refresh t))
6569 ((or (equal char ?\ )
6570 (setq a (rassoc char alist))
6571 (and (>= char ?0) (<= char ?9)
6572 (setq n (if (= char ?0) 9 (- char ?0 1))
6573 tag (concat effort-op (nth n efforts))
6574 a (cons tag nil)))
6575 (and (= char ??)
6576 (setq tag "?eff")
6577 a (cons tag nil))
6578 (and tag (setq a (cons tag nil))))
6579 (org-agenda-filter-show-all-tag)
6580 (setq tag (car a))
6581 (setq org-agenda-tag-filter
6582 (cons (concat (if strip "-" "+") tag)
6583 (if narrow current nil)))
6584 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6585 (setq maybe-refresh t))
6586 (t (error "Invalid tag selection character %c" char)))
6587 (when (and maybe-refresh
6588 (eq org-agenda-clockreport-mode 'with-filter))
6589 (org-agenda-redo))))
6591 (defun org-agenda-get-represented-tags ()
6592 "Get a list of all tags currently represented in the agenda."
6593 (let (p tags)
6594 (save-excursion
6595 (goto-char (point-min))
6596 (while (setq p (next-single-property-change (point) 'tags))
6597 (goto-char p)
6598 (mapc (lambda (x) (add-to-list 'tags x))
6599 (get-text-property (point) 'tags))))
6600 tags))
6602 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6603 "Refine the current filter. See `org-agenda-filter-by-tag'."
6604 (interactive "P")
6605 (org-agenda-filter-by-tag strip char 'refine))
6607 (defun org-agenda-filter-make-matcher ()
6608 "Create the form that tests a line for agenda filter."
6609 (let (f f1)
6610 ;; first compute the tag-filter matcher
6611 (dolist (x (delete-dups
6612 (append (get 'org-agenda-tag-filter
6613 :preset-filter) org-agenda-tag-filter)))
6614 (if (member x '("-" "+"))
6615 (setq f1 (if (equal x "-") 'tags '(not tags)))
6616 (if (string-match "[<=>?]" x)
6617 (setq f1 (org-agenda-filter-effort-form x))
6618 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6619 (if (equal (string-to-char x) ?-)
6620 (setq f1 (list 'not f1))))
6621 (push f1 f))
6622 ;; then compute the category-filter matcher
6623 (dolist (x (delete-dups
6624 (append (get 'org-agenda-category-filter
6625 :preset-filter) org-agenda-category-filter)))
6626 (setq f1 (list 'equal (substring x 1) 'cat))
6627 (push f1 f))
6628 (cons 'and (nreverse f))))
6630 (defun org-agenda-filter-effort-form (e)
6631 "Return the form to compare the effort of the current line with what E says.
6632 E looks like \"+<2:25\"."
6633 (let (op)
6634 (setq e (substring e 1))
6635 (setq op (string-to-char e) e (substring e 1))
6636 (setq op (cond ((equal op ?<) '<=)
6637 ((equal op ?>) '>=)
6638 ((equal op ??) op)
6639 (t '=)))
6640 (list 'org-agenda-compare-effort (list 'quote op)
6641 (org-duration-string-to-minutes e))))
6643 (defun org-agenda-compare-effort (op value)
6644 "Compare the effort of the current line with VALUE, using OP.
6645 If the line does not have an effort defined, return nil."
6646 (let ((eff (org-get-at-bol 'effort-minutes)))
6647 (if (equal op ??)
6648 (not eff)
6649 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6650 value))))
6652 (defun org-agenda-filter-apply (filter type)
6653 "Set FILTER as the new agenda filter and apply it."
6654 (let (tags cat)
6655 (if (eq type 'tag)
6656 (setq org-agenda-tag-filter filter)
6657 (setq org-agenda-category-filter filter
6658 org-agenda-filtered-by-category t))
6659 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6660 (org-agenda-set-mode-name)
6661 (save-excursion
6662 (goto-char (point-min))
6663 (while (not (eobp))
6664 (if (org-get-at-bol 'org-marker)
6665 (progn
6666 (setq tags (org-get-at-bol 'tags) ; used in eval
6667 cat (get-text-property (point) 'org-category))
6668 (if (not (eval org-agenda-filter-form))
6669 (org-agenda-filter-hide-line type))
6670 (beginning-of-line 2))
6671 (beginning-of-line 2))))
6672 (if (get-char-property (point) 'invisible)
6673 (org-agenda-previous-line))))
6675 (defun org-agenda-filter-hide-line (type)
6676 (let (ov)
6677 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6678 (point-at-eol)))
6679 (overlay-put ov 'invisible t)
6680 (overlay-put ov 'type type)
6681 (if (eq type 'tag)
6682 (push ov org-agenda-tag-filter-overlays)
6683 (push ov org-agenda-cat-filter-overlays))))
6685 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6686 (setq pos (or pos (point)))
6687 (save-excursion
6688 (dolist (ov (overlays-at pos))
6689 (when (and (overlay-get ov 'invisible)
6690 (eq (overlay-get ov 'type) 'tag))
6691 (goto-char pos)
6692 (if (< (overlay-start ov) (point-at-eol))
6693 (move-overlay ov (point-at-eol)
6694 (overlay-end ov)))))))
6696 (defun org-agenda-filter-show-all-tag nil
6697 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6698 (setq org-agenda-tag-filter-overlays nil
6699 org-agenda-tag-filter nil
6700 org-agenda-filter-form nil)
6701 (org-agenda-set-mode-name))
6703 (defun org-agenda-filter-show-all-cat nil
6704 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6705 (setq org-agenda-cat-filter-overlays nil
6706 org-agenda-filtered-by-category nil
6707 org-agenda-category-filter nil
6708 org-agenda-filter-form nil)
6709 (org-agenda-set-mode-name))
6711 (defun org-agenda-manipulate-query-add ()
6712 "Manipulate the query by adding a search term with positive selection.
6713 Positive selection means the term must be matched for selection of an entry."
6714 (interactive)
6715 (org-agenda-manipulate-query ?\[))
6716 (defun org-agenda-manipulate-query-subtract ()
6717 "Manipulate the query by adding a search term with negative selection.
6718 Negative selection means term must not be matched for selection of an entry."
6719 (interactive)
6720 (org-agenda-manipulate-query ?\]))
6721 (defun org-agenda-manipulate-query-add-re ()
6722 "Manipulate the query by adding a search regexp with positive selection.
6723 Positive selection means the regexp must match for selection of an entry."
6724 (interactive)
6725 (org-agenda-manipulate-query ?\{))
6726 (defun org-agenda-manipulate-query-subtract-re ()
6727 "Manipulate the query by adding a search regexp with negative selection.
6728 Negative selection means regexp must not match for selection of an entry."
6729 (interactive)
6730 (org-agenda-manipulate-query ?\}))
6731 (defun org-agenda-manipulate-query (char)
6732 (cond
6733 ((memq org-agenda-type '(timeline agenda))
6734 (let ((org-agenda-include-inactive-timestamps t))
6735 (org-agenda-redo))
6736 (message "Display now includes inactive timestamps as well"))
6737 ((eq org-agenda-type 'search)
6738 (org-add-to-string
6739 'org-agenda-query-string
6740 (if org-agenda-last-search-view-search-was-boolean
6741 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6742 (?\{ . " +{}") (?\} . " -{}"))))
6743 " "))
6744 (setq org-agenda-redo-command
6745 (list 'org-search-view
6746 org-todo-only
6747 org-agenda-query-string
6748 (+ (length org-agenda-query-string)
6749 (if (member char '(?\{ ?\})) 0 1))))
6750 (set-register org-agenda-query-register org-agenda-query-string)
6751 (org-agenda-redo))
6752 (t (error "Cannot manipulate query for %s-type agenda buffers"
6753 org-agenda-type))))
6755 (defun org-add-to-string (var string)
6756 (set var (concat (symbol-value var) string)))
6758 (defun org-agenda-goto-date (date)
6759 "Jump to DATE in agenda."
6760 (interactive (list (let ((org-read-date-prefer-future
6761 (eval org-agenda-jump-prefer-future)))
6762 (org-read-date))))
6763 (org-agenda-list nil date))
6765 (defun org-agenda-goto-today ()
6766 "Go to today."
6767 (interactive)
6768 (org-agenda-check-type t 'timeline 'agenda)
6769 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6770 (cond
6771 (tdpos (goto-char tdpos))
6772 ((eq org-agenda-type 'agenda)
6773 (let* ((sd (org-agenda-compute-starting-span
6774 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6775 (org-agenda-overriding-arguments org-agenda-last-arguments))
6776 (setf (nth 1 org-agenda-overriding-arguments) sd)
6777 (org-agenda-redo)
6778 (org-agenda-find-same-or-today-or-agenda)))
6779 (t (error "Cannot find today")))))
6781 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6782 (goto-char
6783 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6784 (text-property-any (point-min) (point-max) 'org-today t)
6785 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6786 (point-min))))
6788 (defun org-agenda-later (arg)
6789 "Go forward in time by thee current span.
6790 With prefix ARG, go forward that many times the current span."
6791 (interactive "p")
6792 (org-agenda-check-type t 'agenda)
6793 (let* ((span org-agenda-current-span)
6794 (sd org-starting-day)
6795 (greg (calendar-gregorian-from-absolute sd))
6796 (cnt (org-get-at-bol 'org-day-cnt))
6797 greg2)
6798 (cond
6799 ((eq span 'day)
6800 (setq sd (+ arg sd)))
6801 ((eq span 'week)
6802 (setq sd (+ (* 7 arg) sd)))
6803 ((eq span 'month)
6804 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6805 sd (calendar-absolute-from-gregorian greg2))
6806 (setcar greg2 (1+ (car greg2))))
6807 ((eq span 'year)
6808 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6809 sd (calendar-absolute-from-gregorian greg2))
6810 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6812 (setq sd (+ (* span arg) sd))))
6813 (let ((org-agenda-overriding-arguments
6814 (list (car org-agenda-last-arguments) sd span t)))
6815 (org-agenda-redo)
6816 (org-agenda-find-same-or-today-or-agenda cnt))))
6818 (defun org-agenda-earlier (arg)
6819 "Go backward in time by the current span.
6820 With prefix ARG, go backward that many times the current span."
6821 (interactive "p")
6822 (org-agenda-later (- arg)))
6824 (defun org-agenda-view-mode-dispatch ()
6825 "Call one of the view mode commands."
6826 (interactive)
6827 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6828 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6829 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6830 [E]ntryText")
6831 (let ((a (read-char-exclusive)))
6832 (case a
6833 (?\ (call-interactively 'org-agenda-reset-view))
6834 (?d (call-interactively 'org-agenda-day-view))
6835 (?w (call-interactively 'org-agenda-week-view))
6836 (?m (call-interactively 'org-agenda-month-view))
6837 (?y (call-interactively 'org-agenda-year-view))
6838 (?l (call-interactively 'org-agenda-log-mode))
6839 (?L (org-agenda-log-mode '(4)))
6840 (?c (org-agenda-log-mode 'clockcheck))
6841 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6842 (?a (call-interactively 'org-agenda-archives-mode))
6843 (?A (org-agenda-archives-mode 'files))
6844 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6845 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6846 (?G (call-interactively 'org-agenda-toggle-time-grid))
6847 (?D (call-interactively 'org-agenda-toggle-diary))
6848 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6849 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6850 (org-agenda-check-type t 'timeline 'agenda)
6851 (org-agenda-redo))
6852 (message "Display now includes inactive timestamps as well"))
6853 (?q (message "Abort"))
6854 (otherwise (error "Invalid key" )))))
6856 (defun org-agenda-reset-view ()
6857 "Switch to default view for agenda."
6858 (interactive)
6859 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6860 (defun org-agenda-day-view (&optional day-of-year)
6861 "Switch to daily view for agenda.
6862 With argument DAY-OF-YEAR, switch to that day of the year."
6863 (interactive "P")
6864 (org-agenda-change-time-span 'day day-of-year))
6865 (defun org-agenda-week-view (&optional iso-week)
6866 "Switch to daily view for agenda.
6867 With argument ISO-WEEK, switch to the corresponding ISO week.
6868 If ISO-WEEK has more then 2 digits, only the last two encode the
6869 week. Any digits before this encode a year. So 200712 means
6870 week 12 of year 2007. Years in the range 1938-2037 can also be
6871 written as 2-digit years."
6872 (interactive "P")
6873 (org-agenda-change-time-span 'week iso-week))
6874 (defun org-agenda-month-view (&optional month)
6875 "Switch to monthly view for agenda.
6876 With argument MONTH, switch to that month."
6877 (interactive "P")
6878 (org-agenda-change-time-span 'month month))
6879 (defun org-agenda-year-view (&optional year)
6880 "Switch to yearly view for agenda.
6881 With argument YEAR, switch to that year.
6882 If MONTH has more then 2 digits, only the last two encode the
6883 month. Any digits before this encode a year. So 200712 means
6884 December year 2007. Years in the range 1938-2037 can also be
6885 written as 2-digit years."
6886 (interactive "P")
6887 (when year
6888 (setq year (org-small-year-to-year year)))
6889 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6890 (org-agenda-change-time-span 'year year)
6891 (error "Abort")))
6893 (defun org-agenda-change-time-span (span &optional n)
6894 "Change the agenda view to SPAN.
6895 SPAN may be `day', `week', `month', `year'."
6896 (org-agenda-check-type t 'agenda)
6897 (if (and (not n) (equal org-agenda-current-span span))
6898 (error "Viewing span is already \"%s\"" span))
6899 (let* ((sd (or (org-get-at-bol 'day)
6900 org-starting-day))
6901 (sd (org-agenda-compute-starting-span sd span n))
6902 (org-agenda-overriding-arguments
6903 (or org-agenda-overriding-arguments
6904 (list (car org-agenda-last-arguments) sd span t))))
6905 (org-agenda-redo)
6906 (org-agenda-find-same-or-today-or-agenda))
6907 (org-agenda-set-mode-name)
6908 (message "Switched to %s view" span))
6910 (defun org-agenda-compute-starting-span (sd span &optional n)
6911 "Compute starting date for agenda.
6912 SPAN may be `day', `week', `month', `year'. The return value
6913 is a cons cell with the starting date and the number of days,
6914 so that the date SD will be in that range."
6915 (let* ((greg (calendar-gregorian-from-absolute sd))
6916 (dg (nth 1 greg))
6917 (mg (car greg))
6918 (yg (nth 2 greg)))
6919 (cond
6920 ((eq span 'day)
6921 (when n
6922 (setq sd (+ (calendar-absolute-from-gregorian
6923 (list mg 1 yg))
6924 n -1))))
6925 ((eq span 'week)
6926 (let* ((nt (calendar-day-of-week
6927 (calendar-gregorian-from-absolute sd)))
6928 (d (if org-agenda-start-on-weekday
6929 (- nt org-agenda-start-on-weekday)
6932 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6933 (when n
6934 (require 'cal-iso)
6935 (when (> n 99)
6936 (setq y1 (org-small-year-to-year (/ n 100))
6937 n (mod n 100)))
6938 (setq sd
6939 (calendar-absolute-from-iso
6940 (list n 1
6941 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6942 ((eq span 'month)
6943 (let (y1)
6944 (when (and n (> n 99))
6945 (setq y1 (org-small-year-to-year (/ n 100))
6946 n (mod n 100)))
6947 (setq sd (calendar-absolute-from-gregorian
6948 (list (or n mg) 1 (or y1 yg))))))
6949 ((eq span 'year)
6950 (setq sd (calendar-absolute-from-gregorian
6951 (list 1 1 (or n yg))))))
6952 sd))
6954 (defun org-agenda-next-date-line (&optional arg)
6955 "Jump to the next line indicating a date in agenda buffer."
6956 (interactive "p")
6957 (org-agenda-check-type t 'agenda 'timeline)
6958 (beginning-of-line 1)
6959 ;; This does not work if user makes date format that starts with a blank
6960 (if (looking-at "^\\S-") (forward-char 1))
6961 (if (not (re-search-forward "^\\S-" nil t arg))
6962 (progn
6963 (backward-char 1)
6964 (error "No next date after this line in this buffer")))
6965 (goto-char (match-beginning 0)))
6967 (defun org-agenda-previous-date-line (&optional arg)
6968 "Jump to the previous line indicating a date in agenda buffer."
6969 (interactive "p")
6970 (org-agenda-check-type t 'agenda 'timeline)
6971 (beginning-of-line 1)
6972 (if (not (re-search-backward "^\\S-" nil t arg))
6973 (error "No previous date before this line in this buffer")))
6975 ;; Initialize the highlight
6976 (defvar org-hl (make-overlay 1 1))
6977 (overlay-put org-hl 'face 'highlight)
6979 (defun org-highlight (begin end &optional buffer)
6980 "Highlight a region with overlay."
6981 (move-overlay org-hl begin end (or buffer (current-buffer))))
6983 (defun org-unhighlight ()
6984 "Detach overlay INDEX."
6985 (org-detach-overlay org-hl))
6987 ;; FIXME this is currently not used.
6988 (defun org-highlight-until-next-command (beg end &optional buffer)
6989 "Move the highlight overlay to BEG/END, remove it before the next command."
6990 (org-highlight beg end buffer)
6991 (add-hook 'pre-command-hook 'org-unhighlight-once))
6992 (defun org-unhighlight-once ()
6993 "Remove the highlight from its position, and this function from the hook."
6994 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6995 (org-unhighlight))
6997 (defun org-agenda-follow-mode ()
6998 "Toggle follow mode in an agenda buffer."
6999 (interactive)
7000 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7001 (org-agenda-set-mode-name)
7002 (org-agenda-do-context-action)
7003 (message "Follow mode is %s"
7004 (if org-agenda-follow-mode "on" "off")))
7006 (defun org-agenda-entry-text-mode (&optional arg)
7007 "Toggle entry text mode in an agenda buffer."
7008 (interactive "P")
7009 (setq org-agenda-entry-text-mode (or (integerp arg)
7010 (not org-agenda-entry-text-mode)))
7011 (org-agenda-entry-text-hide)
7012 (and org-agenda-entry-text-mode
7013 (let ((org-agenda-entry-text-maxlines
7014 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7015 (org-agenda-entry-text-show)))
7016 (org-agenda-set-mode-name)
7017 (message "Entry text mode is %s. Maximum number of lines is %d"
7018 (if org-agenda-entry-text-mode "on" "off")
7019 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7021 (defun org-agenda-clockreport-mode (&optional with-filter)
7022 "Toggle clocktable mode in an agenda buffer.
7023 With prefix arg WITH-FILTER, make the clocktable respect the current
7024 agenda filter."
7025 (interactive "P")
7026 (org-agenda-check-type t 'agenda)
7027 (if with-filter
7028 (setq org-agenda-clockreport-mode 'with-filter)
7029 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7030 (org-agenda-set-mode-name)
7031 (org-agenda-redo)
7032 (message "Clocktable mode is %s"
7033 (if org-agenda-clockreport-mode "on" "off")))
7035 (defun org-agenda-log-mode (&optional special)
7036 "Toggle log mode in an agenda buffer.
7037 With argument SPECIAL, show all possible log items, not only the ones
7038 configured in `org-agenda-log-mode-items'.
7039 With a double `C-u' prefix arg, show *only* log items, nothing else."
7040 (interactive "P")
7041 (org-agenda-check-type t 'agenda 'timeline)
7042 (setq org-agenda-show-log
7043 (cond
7044 ((equal special '(16)) 'only)
7045 ((eq special 'clockcheck)
7046 (if (eq org-agenda-show-log 'clockcheck)
7047 nil 'clockcheck))
7048 (special '(closed clock state))
7049 (t (not org-agenda-show-log))))
7050 (org-agenda-set-mode-name)
7051 (org-agenda-redo)
7052 (message "Log mode is %s"
7053 (if org-agenda-show-log "on" "off")))
7055 (defun org-agenda-archives-mode (&optional with-files)
7056 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7057 When called with a prefix argument, include all archive files as well."
7058 (interactive "P")
7059 (setq org-agenda-archives-mode
7060 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7061 (org-agenda-set-mode-name)
7062 (org-agenda-redo)
7063 (message
7064 "%s"
7065 (cond
7066 ((eq org-agenda-archives-mode nil)
7067 "No archives are included")
7068 ((eq org-agenda-archives-mode 'trees)
7069 (format "Trees with :%s: tag are included" org-archive-tag))
7070 ((eq org-agenda-archives-mode t)
7071 (format "Trees with :%s: tag and all active archive files are included"
7072 org-archive-tag)))))
7074 (defun org-agenda-toggle-diary ()
7075 "Toggle diary inclusion in an agenda buffer."
7076 (interactive)
7077 (org-agenda-check-type t 'agenda)
7078 (setq org-agenda-include-diary (not org-agenda-include-diary))
7079 (org-agenda-redo)
7080 (org-agenda-set-mode-name)
7081 (message "Diary inclusion turned %s"
7082 (if org-agenda-include-diary "on" "off")))
7084 (defun org-agenda-toggle-deadlines ()
7085 "Toggle inclusion of entries with a deadline in an agenda buffer."
7086 (interactive)
7087 (org-agenda-check-type t 'agenda)
7088 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7089 (org-agenda-redo)
7090 (org-agenda-set-mode-name)
7091 (message "Deadlines inclusion turned %s"
7092 (if org-agenda-include-deadlines "on" "off")))
7094 (defun org-agenda-toggle-time-grid ()
7095 "Toggle time grid in an agenda buffer."
7096 (interactive)
7097 (org-agenda-check-type t 'agenda)
7098 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7099 (org-agenda-redo)
7100 (org-agenda-set-mode-name)
7101 (message "Time-grid turned %s"
7102 (if org-agenda-use-time-grid "on" "off")))
7104 (defun org-agenda-set-mode-name ()
7105 "Set the mode name to indicate all the small mode settings."
7106 (setq mode-name
7107 (list "Org-Agenda"
7108 (if (get 'org-agenda-files 'org-restrict) " []" "")
7110 '(:eval (org-agenda-span-name org-agenda-current-span))
7111 (if org-agenda-follow-mode " Follow" "")
7112 (if org-agenda-entry-text-mode " ETxt" "")
7113 (if org-agenda-include-diary " Diary" "")
7114 (if org-agenda-include-deadlines " Ddl" "")
7115 (if org-agenda-use-time-grid " Grid" "")
7116 (if (and (boundp 'org-habit-show-habits)
7117 org-habit-show-habits) " Habit" "")
7118 (cond
7119 ((consp org-agenda-show-log) " LogAll")
7120 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7121 (org-agenda-show-log " Log")
7122 (t ""))
7123 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7124 :preset-filter))
7125 '(:eval (org-propertize
7126 (concat " <"
7127 (mapconcat
7128 'identity
7129 (append
7130 (get 'org-agenda-category-filter :preset-filter)
7131 org-agenda-category-filter)
7133 ">")
7134 'face 'org-agenda-filter-category
7135 'help-echo "Category used in filtering"))
7137 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7138 :preset-filter))
7139 '(:eval (org-propertize
7140 (concat " {"
7141 (mapconcat
7142 'identity
7143 (append
7144 (get 'org-agenda-tag-filter :preset-filter)
7145 org-agenda-tag-filter)
7147 "}")
7148 'face 'org-agenda-filter-tags
7149 'help-echo "Tags used in filtering"))
7151 (if org-agenda-archives-mode
7152 (if (eq org-agenda-archives-mode t)
7153 " Archives"
7154 (format " :%s:" org-archive-tag))
7156 (if org-agenda-clockreport-mode
7157 (if (eq org-agenda-clockreport-mode 'with-filter)
7158 " Clock{}" " Clock")
7159 "")))
7160 (force-mode-line-update))
7162 (defun org-agenda-post-command-hook ()
7163 (setq org-agenda-type
7164 (or (get-text-property (point) 'org-agenda-type)
7165 (get-text-property (max (point-min) (1- (point)))
7166 'org-agenda-type))))
7168 (defun org-agenda-next-line ()
7169 "Move cursor to the next line, and show if follow mode is active."
7170 (interactive)
7171 (call-interactively 'next-line)
7172 (org-agenda-do-context-action))
7174 (defun org-agenda-previous-line ()
7175 "Move cursor to the previous line, and show if follow-mode is active."
7176 (interactive)
7177 (call-interactively 'previous-line)
7178 (org-agenda-do-context-action))
7180 (defun org-agenda-do-context-action ()
7181 "Show outline path and, maybe, follow mode window."
7182 (let ((m (org-get-at-bol 'org-marker)))
7183 (when (and (markerp m) (marker-buffer m))
7184 (and org-agenda-follow-mode
7185 (if org-agenda-follow-indirect
7186 (org-agenda-tree-to-indirect-buffer)
7187 (org-agenda-show)))
7188 (and org-agenda-show-outline-path
7189 (org-with-point-at m (org-display-outline-path t))))))
7191 (defun org-agenda-show-priority ()
7192 "Show the priority of the current item.
7193 This priority is composed of the main priority given with the [#A] cookies,
7194 and by additional input from the age of a schedules or deadline entry."
7195 (interactive)
7196 (let* ((pri (org-get-at-bol 'priority)))
7197 (message "Priority is %d" (if pri pri -1000))))
7199 (defun org-agenda-show-tags ()
7200 "Show the tags applicable to the current item."
7201 (interactive)
7202 (let* ((tags (org-get-at-bol 'tags)))
7203 (if tags
7204 (message "Tags are :%s:"
7205 (org-no-properties (mapconcat 'identity tags ":")))
7206 (message "No tags associated with this line"))))
7208 (defun org-agenda-goto (&optional highlight)
7209 "Go to the Org-mode file which contains the item at point."
7210 (interactive)
7211 (let* ((marker (or (org-get-at-bol 'org-marker)
7212 (org-agenda-error)))
7213 (buffer (marker-buffer marker))
7214 (pos (marker-position marker)))
7215 (switch-to-buffer-other-window buffer)
7216 (widen)
7217 (push-mark)
7218 (goto-char pos)
7219 (when (eq major-mode 'org-mode)
7220 (org-show-context 'agenda)
7221 (save-excursion
7222 (and (outline-next-heading)
7223 (org-flag-heading nil)))) ; show the next heading
7224 (when (outline-invisible-p)
7225 (show-entry)) ; display invisible text
7226 (recenter (/ (window-height) 2))
7227 (run-hooks 'org-agenda-after-show-hook)
7228 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7230 (defvar org-agenda-after-show-hook nil
7231 "Normal hook run after an item has been shown from the agenda.
7232 Point is in the buffer where the item originated.")
7234 (defun org-agenda-kill ()
7235 "Kill the entry or subtree belonging to the current agenda entry."
7236 (interactive)
7237 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7238 (let* ((marker (or (org-get-at-bol 'org-marker)
7239 (org-agenda-error)))
7240 (buffer (marker-buffer marker))
7241 (pos (marker-position marker))
7242 (type (org-get-at-bol 'type))
7243 dbeg dend (n 0) conf)
7244 (org-with-remote-undo buffer
7245 (with-current-buffer buffer
7246 (save-excursion
7247 (goto-char pos)
7248 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
7249 (setq dbeg (progn (org-back-to-heading t) (point))
7250 dend (org-end-of-subtree t t))
7251 (setq dbeg (point-at-bol)
7252 dend (min (point-max) (1+ (point-at-eol)))))
7253 (goto-char dbeg)
7254 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7255 (setq conf (or (eq t org-agenda-confirm-kill)
7256 (and (numberp org-agenda-confirm-kill)
7257 (> n org-agenda-confirm-kill))))
7258 (and conf
7259 (not (y-or-n-p
7260 (format "Delete entry with %d lines in buffer \"%s\"? "
7261 n (buffer-name buffer))))
7262 (error "Abort"))
7263 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7264 (with-current-buffer buffer (delete-region dbeg dend))
7265 (message "Agenda item and source killed"))))
7267 (defvar org-archive-default-command)
7268 (defun org-agenda-archive-default ()
7269 "Archive the entry or subtree belonging to the current agenda entry."
7270 (interactive)
7271 (require 'org-archive)
7272 (org-agenda-archive-with org-archive-default-command))
7274 (defun org-agenda-archive-default-with-confirmation ()
7275 "Archive the entry or subtree belonging to the current agenda entry."
7276 (interactive)
7277 (require 'org-archive)
7278 (org-agenda-archive-with org-archive-default-command 'confirm))
7280 (defun org-agenda-archive ()
7281 "Archive the entry or subtree belonging to the current agenda entry."
7282 (interactive)
7283 (org-agenda-archive-with 'org-archive-subtree))
7285 (defun org-agenda-archive-to-archive-sibling ()
7286 "Move the entry to the archive sibling."
7287 (interactive)
7288 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7290 (defun org-agenda-archive-with (cmd &optional confirm)
7291 "Move the entry to the archive sibling."
7292 (interactive)
7293 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7294 (let* ((marker (or (org-get-at-bol 'org-marker)
7295 (org-agenda-error)))
7296 (buffer (marker-buffer marker))
7297 (pos (marker-position marker)))
7298 (org-with-remote-undo buffer
7299 (with-current-buffer buffer
7300 (if (eq major-mode 'org-mode)
7301 (if (and confirm
7302 (not (y-or-n-p "Archive this subtree or entry? ")))
7303 (error "Abort")
7304 (save-excursion
7305 (goto-char pos)
7306 (org-remove-subtree-entries-from-agenda)
7307 (org-back-to-heading t)
7308 (funcall cmd)))
7309 (error "Archiving works only in Org-mode files"))))))
7311 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7312 "Remove all lines in the agenda that correspond to a given subtree.
7313 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7314 If this information is not given, the function uses the tree at point."
7315 (let ((buf (or buf (current-buffer))) m p)
7316 (save-excursion
7317 (unless (and beg end)
7318 (org-back-to-heading t)
7319 (setq beg (point))
7320 (org-end-of-subtree t)
7321 (setq end (point)))
7322 (set-buffer (get-buffer org-agenda-buffer-name))
7323 (save-excursion
7324 (goto-char (point-max))
7325 (beginning-of-line 1)
7326 (while (not (bobp))
7327 (when (and (setq m (org-get-at-bol 'org-marker))
7328 (equal buf (marker-buffer m))
7329 (setq p (marker-position m))
7330 (>= p beg)
7331 (< p end))
7332 (let ((inhibit-read-only t))
7333 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7334 (beginning-of-line 0))))))
7336 (defun org-agenda-refile (&optional goto rfloc no-update)
7337 "Refile the item at point."
7338 (interactive "P")
7339 (if (equal goto '(16))
7340 (org-refile-goto-last-stored)
7341 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7342 (org-agenda-error)))
7343 (buffer (marker-buffer marker))
7344 (pos (marker-position marker))
7345 (rfloc (or rfloc
7346 (org-refile-get-location
7347 (if goto "Goto" "Refile to") buffer
7348 org-refile-allow-creating-parent-nodes))))
7349 (with-current-buffer buffer
7350 (save-excursion
7351 (save-restriction
7352 (widen)
7353 (goto-char marker)
7354 (org-remove-subtree-entries-from-agenda)
7355 (org-refile goto buffer rfloc)))))
7356 (unless no-update (org-agenda-redo))))
7358 (defun org-agenda-open-link (&optional arg)
7359 "Follow the link in the current line, if any.
7360 This looks for a link in the displayed line in the agenda. It also looks
7361 at the text of the entry itself."
7362 (interactive "P")
7363 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7364 (org-get-at-bol 'org-marker)))
7365 (buffer (and marker (marker-buffer marker)))
7366 (prefix (buffer-substring
7367 (point-at-bol) (point-at-eol))))
7368 (cond
7369 (buffer
7370 (with-current-buffer buffer
7371 (save-excursion
7372 (save-restriction
7373 (widen)
7374 (goto-char marker)
7375 (org-offer-links-in-entry arg prefix)))))
7376 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7377 (save-excursion
7378 (beginning-of-line 1)
7379 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7380 (org-open-link-from-string (match-string 1)))
7381 (t (error "No link to open here")))))
7383 (defun org-agenda-copy-local-variable (var)
7384 "Get a variable from a referenced buffer and install it here."
7385 (let ((m (org-get-at-bol 'org-marker)))
7386 (when (and m (buffer-live-p (marker-buffer m)))
7387 (org-set-local var (with-current-buffer (marker-buffer m)
7388 (symbol-value var))))))
7390 (defun org-agenda-switch-to (&optional delete-other-windows)
7391 "Go to the Org-mode file which contains the item at point."
7392 (interactive)
7393 (if (and org-return-follows-link
7394 (not (org-get-at-bol 'org-marker))
7395 (org-in-regexp org-bracket-link-regexp))
7396 (org-open-link-from-string (match-string 0))
7397 (let* ((marker (or (org-get-at-bol 'org-marker)
7398 (org-agenda-error)))
7399 (buffer (marker-buffer marker))
7400 (pos (marker-position marker)))
7401 (org-pop-to-buffer-same-window buffer)
7402 (and delete-other-windows (delete-other-windows))
7403 (widen)
7404 (goto-char pos)
7405 (when (eq major-mode 'org-mode)
7406 (org-show-context 'agenda)
7407 (save-excursion
7408 (and (outline-next-heading)
7409 (org-flag-heading nil))) ; show the next heading
7410 (when (outline-invisible-p)
7411 (show-entry)))))) ; display invisible text
7413 (defun org-agenda-goto-mouse (ev)
7414 "Go to the Org-mode file which contains the item at the mouse click."
7415 (interactive "e")
7416 (mouse-set-point ev)
7417 (org-agenda-goto))
7419 (defun org-agenda-show (&optional full-entry)
7420 "Display the Org-mode file which contains the item at point.
7421 With prefix argument FULL-ENTRY, make the entire entry visible
7422 if it was hidden in the outline."
7423 (interactive "P")
7424 (let ((win (selected-window)))
7425 (if full-entry
7426 (let ((org-show-entry-below t))
7427 (org-agenda-goto t))
7428 (org-agenda-goto t))
7429 (select-window win)))
7431 (defvar org-agenda-show-window nil)
7432 (defun org-agenda-show-and-scroll-up (&optional arg)
7433 "Display the Org-mode file which contains the item at point.
7434 When called repeatedly, scroll the window that is displaying the buffer.
7435 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7436 `show-subtree' to display the item, so that drawers and logbooks stay
7437 folded."
7438 (interactive "P")
7439 (let ((win (selected-window)))
7440 (if (and (window-live-p org-agenda-show-window)
7441 (eq this-command last-command))
7442 (progn
7443 (select-window org-agenda-show-window)
7444 (ignore-errors (scroll-up)))
7445 (org-agenda-goto t)
7446 (if arg (org-show-entry) (show-subtree))
7447 (setq org-agenda-show-window (selected-window)))
7448 (select-window win)))
7450 (defun org-agenda-show-scroll-down ()
7451 "Scroll down the window showing the agenda."
7452 (interactive)
7453 (let ((win (selected-window)))
7454 (when (window-live-p org-agenda-show-window)
7455 (select-window org-agenda-show-window)
7456 (ignore-errors (scroll-down))
7457 (select-window win))))
7459 (defun org-agenda-show-1 (&optional more)
7460 "Display the Org-mode file which contains the item at point.
7461 The prefix arg selects the amount of information to display:
7463 0 hide the subtree
7464 1 just show the entry according to defaults.
7465 2 show the children view
7466 3 show the subtree view
7467 4 show the entire subtree and any LOGBOOK drawers
7468 5 show the entire subtree and any drawers
7469 With prefix argument FULL-ENTRY, make the entire entry visible
7470 if it was hidden in the outline."
7471 (interactive "p")
7472 (let ((win (selected-window)))
7473 (org-agenda-goto t)
7474 (org-recenter-heading 1)
7475 (cond
7476 ((= more 0)
7477 (hide-subtree)
7478 (save-excursion
7479 (org-back-to-heading)
7480 (run-hook-with-args 'org-cycle-hook 'folded))
7481 (message "Remote: FOLDED"))
7482 ((and (org-called-interactively-p 'any) (= more 1))
7483 (message "Remote: show with default settings"))
7484 ((= more 2)
7485 (show-entry)
7486 (show-children)
7487 (save-excursion
7488 (org-back-to-heading)
7489 (run-hook-with-args 'org-cycle-hook 'children))
7490 (message "Remote: CHILDREN"))
7491 ((= more 3)
7492 (show-subtree)
7493 (save-excursion
7494 (org-back-to-heading)
7495 (run-hook-with-args 'org-cycle-hook 'subtree))
7496 (message "Remote: SUBTREE"))
7497 ((= more 4)
7498 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7499 (org-drawer-regexp
7500 (concat "^[ \t]*:\\("
7501 (mapconcat 'regexp-quote org-drawers "\\|")
7502 "\\):[ \t]*$")))
7503 (show-subtree)
7504 (save-excursion
7505 (org-back-to-heading)
7506 (org-cycle-hide-drawers 'subtree)))
7507 (message "Remote: SUBTREE AND LOGBOOK"))
7508 ((> more 4)
7509 (show-subtree)
7510 (message "Remote: SUBTREE AND ALL DRAWERS")))
7511 (select-window win)))
7513 (defun org-recenter-heading (n)
7514 (save-excursion
7515 (org-back-to-heading)
7516 (recenter n)))
7518 (defvar org-agenda-cycle-counter nil)
7519 (defun org-agenda-cycle-show (&optional n)
7520 "Show the current entry in another window, with default settings.
7521 Default settings are taken from `org-show-hierarchy-above' and siblings.
7522 When use repeatedly in immediate succession, the remote entry will cycle
7523 through visibility
7525 children -> subtree -> folded
7527 When called with a numeric prefix arg, that arg will be passed through to
7528 `org-agenda-show-1'. For the interpretation of that argument, see the
7529 docstring of `org-agenda-show-1'."
7530 (interactive "P")
7531 (if (integerp n)
7532 (setq org-agenda-cycle-counter n)
7533 (if (not (eq last-command this-command))
7534 (setq org-agenda-cycle-counter 1)
7535 (if (equal org-agenda-cycle-counter 0)
7536 (setq org-agenda-cycle-counter 2)
7537 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7538 (if (> org-agenda-cycle-counter 3)
7539 (setq org-agenda-cycle-counter 0)))))
7540 (org-agenda-show-1 org-agenda-cycle-counter))
7542 (defun org-agenda-recenter (arg)
7543 "Display the Org-mode file which contains the item at point and recenter."
7544 (interactive "P")
7545 (let ((win (selected-window)))
7546 (org-agenda-goto t)
7547 (recenter arg)
7548 (select-window win)))
7550 (defun org-agenda-show-mouse (ev)
7551 "Display the Org-mode file which contains the item at the mouse click."
7552 (interactive "e")
7553 (mouse-set-point ev)
7554 (org-agenda-show))
7556 (defun org-agenda-check-no-diary ()
7557 "Check if the entry is a diary link and abort if yes."
7558 (if (org-get-at-bol 'org-agenda-diary-link)
7559 (org-agenda-error)))
7561 (defun org-agenda-error ()
7562 (error "Command not allowed in this line"))
7564 (defun org-agenda-tree-to-indirect-buffer ()
7565 "Show the subtree corresponding to the current entry in an indirect buffer.
7566 This calls the command `org-tree-to-indirect-buffer' from the original
7567 Org-mode buffer.
7568 With numerical prefix arg ARG, go up to this level and then take that tree.
7569 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7570 use the dedicated frame)."
7571 (interactive)
7572 (if (and current-prefix-arg (listp current-prefix-arg))
7573 (org-agenda-do-tree-to-indirect-buffer)
7574 (let ((agenda-window (selected-window))
7575 (indirect-window (and org-last-indirect-buffer (get-buffer-window org-last-indirect-buffer))))
7576 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7577 (unwind-protect
7578 (progn
7579 (unless (and indirect-window (window-live-p indirect-window))
7580 (setq indirect-window (split-window agenda-window)))
7581 (select-window indirect-window)
7582 (switch-to-buffer org-last-indirect-buffer :norecord)
7583 (fit-window-to-buffer indirect-window))
7584 (select-window agenda-window)))))
7586 (defun org-agenda-do-tree-to-indirect-buffer ()
7587 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7588 (org-agenda-check-no-diary)
7589 (let* ((marker (or (org-get-at-bol 'org-marker)
7590 (org-agenda-error)))
7591 (buffer (marker-buffer marker))
7592 (pos (marker-position marker)))
7593 (with-current-buffer buffer
7594 (save-excursion
7595 (goto-char pos)
7596 (call-interactively 'org-tree-to-indirect-buffer)))))
7598 (defvar org-last-heading-marker (make-marker)
7599 "Marker pointing to the headline that last changed its TODO state
7600 by a remote command from the agenda.")
7602 (defun org-agenda-todo-nextset ()
7603 "Switch TODO entry to next sequence."
7604 (interactive)
7605 (org-agenda-todo 'nextset))
7607 (defun org-agenda-todo-previousset ()
7608 "Switch TODO entry to previous sequence."
7609 (interactive)
7610 (org-agenda-todo 'previousset))
7612 (defun org-agenda-todo (&optional arg)
7613 "Cycle TODO state of line at point, also in Org-mode file.
7614 This changes the line at point, all other lines in the agenda referring to
7615 the same tree node, and the headline of the tree node in the Org-mode file."
7616 (interactive "P")
7617 (org-agenda-check-no-diary)
7618 (let* ((col (current-column))
7619 (marker (or (org-get-at-bol 'org-marker)
7620 (org-agenda-error)))
7621 (buffer (marker-buffer marker))
7622 (pos (marker-position marker))
7623 (hdmarker (org-get-at-bol 'org-hd-marker))
7624 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7625 (inhibit-read-only t)
7626 org-agenda-headline-snapshot-before-repeat newhead just-one)
7627 (org-with-remote-undo buffer
7628 (with-current-buffer buffer
7629 (widen)
7630 (goto-char pos)
7631 (org-show-context 'agenda)
7632 (save-excursion
7633 (and (outline-next-heading)
7634 (org-flag-heading nil))) ; show the next heading
7635 (let ((current-prefix-arg arg))
7636 (call-interactively 'org-todo))
7637 (and (bolp) (forward-char 1))
7638 (setq newhead (org-get-heading))
7639 (when (and (org-bound-and-true-p
7640 org-agenda-headline-snapshot-before-repeat)
7641 (not (equal org-agenda-headline-snapshot-before-repeat
7642 newhead))
7643 todayp)
7644 (setq newhead org-agenda-headline-snapshot-before-repeat
7645 just-one t))
7646 (save-excursion
7647 (org-back-to-heading)
7648 (move-marker org-last-heading-marker (point))))
7649 (beginning-of-line 1)
7650 (save-excursion
7651 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7652 (org-move-to-column col))))
7654 (defun org-agenda-add-note (&optional arg)
7655 "Add a time-stamped note to the entry at point."
7656 (interactive "P")
7657 (org-agenda-check-no-diary)
7658 (let* ((marker (or (org-get-at-bol 'org-marker)
7659 (org-agenda-error)))
7660 (buffer (marker-buffer marker))
7661 (pos (marker-position marker))
7662 (hdmarker (org-get-at-bol 'org-hd-marker))
7663 (inhibit-read-only t))
7664 (with-current-buffer buffer
7665 (widen)
7666 (goto-char pos)
7667 (org-show-context 'agenda)
7668 (save-excursion
7669 (and (outline-next-heading)
7670 (org-flag-heading nil))) ; show the next heading
7671 (org-add-note))))
7673 (defun org-agenda-change-all-lines (newhead hdmarker
7674 &optional fixface just-this)
7675 "Change all lines in the agenda buffer which match HDMARKER.
7676 The new content of the line will be NEWHEAD (as modified by
7677 `org-agenda-format-item'). HDMARKER is checked with
7678 `equal' against all `org-hd-marker' text properties in the file.
7679 If FIXFACE is non-nil, the face of each item is modified according to
7680 the new TODO state.
7681 If JUST-THIS is non-nil, change just the current line, not all.
7682 If FORCE-TAGS is non nil, the car of it returns the new tags."
7683 (let* ((inhibit-read-only t)
7684 (line (org-current-line))
7685 (org-agenda-buffer (current-buffer))
7686 (thetags (with-current-buffer (marker-buffer hdmarker)
7687 (save-excursion (save-restriction (widen)
7688 (goto-char hdmarker)
7689 (org-get-tags-at)))))
7690 props m pl undone-face done-face finish new dotime cat tags)
7691 (save-excursion
7692 (goto-char (point-max))
7693 (beginning-of-line 1)
7694 (while (not finish)
7695 (setq finish (bobp))
7696 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7697 (or (not just-this) (= (org-current-line) line))
7698 (equal m hdmarker))
7699 (setq props (text-properties-at (point))
7700 dotime (org-get-at-bol 'dotime)
7701 cat (org-get-at-bol 'org-category)
7702 tags thetags
7704 (let ((org-prefix-format-compiled
7705 (or (get-text-property (point) 'format)
7706 org-prefix-format-compiled)))
7707 (with-current-buffer (marker-buffer hdmarker)
7708 (save-excursion
7709 (save-restriction
7710 (widen)
7711 (org-agenda-format-item (org-get-at-bol 'extra)
7712 newhead cat tags dotime)))))
7713 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7714 undone-face (org-get-at-bol 'undone-face)
7715 done-face (org-get-at-bol 'done-face))
7716 (beginning-of-line 1)
7717 (cond
7718 ((equal new "")
7719 (and (looking-at ".*\n?") (replace-match "")))
7720 ((looking-at ".*")
7721 (replace-match new t t)
7722 (beginning-of-line 1)
7723 (add-text-properties (point-at-bol) (point-at-eol) props)
7724 (when fixface
7725 (add-text-properties
7726 (point-at-bol) (point-at-eol)
7727 (list 'face
7728 (if org-last-todo-state-is-todo
7729 undone-face done-face))))
7730 (org-agenda-highlight-todo 'line)
7731 (beginning-of-line 1))
7732 (t (error "Line update did not work"))))
7733 (beginning-of-line 0)))
7734 (org-finalize-agenda)))
7736 (defun org-agenda-align-tags (&optional line)
7737 "Align all tags in agenda items to `org-agenda-tags-column'."
7738 (let ((inhibit-read-only t) l c)
7739 (save-excursion
7740 (goto-char (if line (point-at-bol) (point-min)))
7741 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7742 (if line (point-at-eol) nil) t)
7743 (add-text-properties
7744 (match-beginning 2) (match-end 2)
7745 (list 'face (delq nil (let ((prop (get-text-property
7746 (match-beginning 2) 'face)))
7747 (or (listp prop) (setq prop (list prop)))
7748 (if (memq 'org-tag prop)
7749 prop
7750 (cons 'org-tag prop))))))
7751 (setq l (- (match-end 2) (match-beginning 2))
7752 c (if (< org-agenda-tags-column 0)
7753 (- (abs org-agenda-tags-column) l)
7754 org-agenda-tags-column))
7755 (delete-region (match-beginning 1) (match-end 1))
7756 (goto-char (match-beginning 1))
7757 (insert (org-add-props
7758 (make-string (max 1 (- c (current-column))) ?\ )
7759 (plist-put (copy-sequence (text-properties-at (point)))
7760 'face nil))))
7761 (goto-char (point-min))
7762 (org-font-lock-add-tag-faces (point-max)))))
7764 (defun org-agenda-priority-up ()
7765 "Increase the priority of line at point, also in Org-mode file."
7766 (interactive)
7767 (org-agenda-priority 'up))
7769 (defun org-agenda-priority-down ()
7770 "Decrease the priority of line at point, also in Org-mode file."
7771 (interactive)
7772 (org-agenda-priority 'down))
7774 (defun org-agenda-priority (&optional force-direction)
7775 "Set the priority of line at point, also in Org-mode file.
7776 This changes the line at point, all other lines in the agenda referring to
7777 the same tree node, and the headline of the tree node in the Org-mode file."
7778 (interactive)
7779 (unless org-enable-priority-commands
7780 (error "Priority commands are disabled"))
7781 (org-agenda-check-no-diary)
7782 (let* ((marker (or (org-get-at-bol 'org-marker)
7783 (org-agenda-error)))
7784 (hdmarker (org-get-at-bol 'org-hd-marker))
7785 (buffer (marker-buffer hdmarker))
7786 (pos (marker-position hdmarker))
7787 (inhibit-read-only t)
7788 newhead)
7789 (org-with-remote-undo buffer
7790 (with-current-buffer buffer
7791 (widen)
7792 (goto-char pos)
7793 (org-show-context 'agenda)
7794 (save-excursion
7795 (and (outline-next-heading)
7796 (org-flag-heading nil))) ; show the next heading
7797 (funcall 'org-priority force-direction)
7798 (end-of-line 1)
7799 (setq newhead (org-get-heading)))
7800 (org-agenda-change-all-lines newhead hdmarker)
7801 (beginning-of-line 1))))
7803 ;; FIXME: should fix the tags property of the agenda line.
7804 (defun org-agenda-set-tags (&optional tag onoff)
7805 "Set tags for the current headline."
7806 (interactive)
7807 (org-agenda-check-no-diary)
7808 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7809 (call-interactively 'org-change-tag-in-region)
7810 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7811 (org-agenda-error)))
7812 (buffer (marker-buffer hdmarker))
7813 (pos (marker-position hdmarker))
7814 (inhibit-read-only t)
7815 newhead)
7816 (org-with-remote-undo buffer
7817 (with-current-buffer buffer
7818 (widen)
7819 (goto-char pos)
7820 (save-excursion
7821 (org-show-context 'agenda))
7822 (save-excursion
7823 (and (outline-next-heading)
7824 (org-flag-heading nil))) ; show the next heading
7825 (goto-char pos)
7826 (if tag
7827 (org-toggle-tag tag onoff)
7828 (call-interactively 'org-set-tags))
7829 (end-of-line 1)
7830 (setq newhead (org-get-heading)))
7831 (org-agenda-change-all-lines newhead hdmarker)
7832 (beginning-of-line 1)))))
7834 (defun org-agenda-set-property ()
7835 "Set a property for the current headline."
7836 (interactive)
7837 (org-agenda-check-no-diary)
7838 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7839 (org-agenda-error)))
7840 (buffer (marker-buffer hdmarker))
7841 (pos (marker-position hdmarker))
7842 (inhibit-read-only t)
7843 newhead)
7844 (org-with-remote-undo buffer
7845 (with-current-buffer buffer
7846 (widen)
7847 (goto-char pos)
7848 (save-excursion
7849 (org-show-context 'agenda))
7850 (save-excursion
7851 (and (outline-next-heading)
7852 (org-flag-heading nil))) ; show the next heading
7853 (goto-char pos)
7854 (call-interactively 'org-set-property)))))
7856 (defun org-agenda-set-effort ()
7857 "Set the effort property for the current headline."
7858 (interactive)
7859 (org-agenda-check-no-diary)
7860 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7861 (org-agenda-error)))
7862 (buffer (marker-buffer hdmarker))
7863 (pos (marker-position hdmarker))
7864 (inhibit-read-only t)
7865 newhead)
7866 (org-with-remote-undo buffer
7867 (with-current-buffer buffer
7868 (widen)
7869 (goto-char pos)
7870 (save-excursion
7871 (org-show-context 'agenda))
7872 (save-excursion
7873 (and (outline-next-heading)
7874 (org-flag-heading nil))) ; show the next heading
7875 (goto-char pos)
7876 (call-interactively 'org-set-effort)
7877 (end-of-line 1)
7878 (setq newhead (org-get-heading)))
7879 (org-agenda-change-all-lines newhead hdmarker))))
7881 (defun org-agenda-toggle-archive-tag ()
7882 "Toggle the archive tag for the current entry."
7883 (interactive)
7884 (org-agenda-check-no-diary)
7885 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7886 (org-agenda-error)))
7887 (buffer (marker-buffer hdmarker))
7888 (pos (marker-position hdmarker))
7889 (inhibit-read-only t)
7890 newhead)
7891 (org-with-remote-undo buffer
7892 (with-current-buffer buffer
7893 (widen)
7894 (goto-char pos)
7895 (org-show-context 'agenda)
7896 (save-excursion
7897 (and (outline-next-heading)
7898 (org-flag-heading nil))) ; show the next heading
7899 (call-interactively 'org-toggle-archive-tag)
7900 (end-of-line 1)
7901 (setq newhead (org-get-heading)))
7902 (org-agenda-change-all-lines newhead hdmarker)
7903 (beginning-of-line 1))))
7905 (defun org-agenda-do-date-later (arg)
7906 (interactive "P")
7907 (cond
7908 ((or (equal arg '(16))
7909 (memq last-command
7910 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7911 (setq this-command 'org-agenda-date-later-minutes)
7912 (org-agenda-date-later-minutes 1))
7913 ((or (equal arg '(4))
7914 (memq last-command
7915 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7916 (setq this-command 'org-agenda-date-later-hours)
7917 (org-agenda-date-later-hours 1))
7919 (org-agenda-date-later (prefix-numeric-value arg)))))
7921 (defun org-agenda-do-date-earlier (arg)
7922 (interactive "P")
7923 (cond
7924 ((or (equal arg '(16))
7925 (memq last-command
7926 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7927 (setq this-command 'org-agenda-date-earlier-minutes)
7928 (org-agenda-date-earlier-minutes 1))
7929 ((or (equal arg '(4))
7930 (memq last-command
7931 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7932 (setq this-command 'org-agenda-date-earlier-hours)
7933 (org-agenda-date-earlier-hours 1))
7935 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7937 (defun org-agenda-date-later (arg &optional what)
7938 "Change the date of this item to ARG day(s) later."
7939 (interactive "p")
7940 (org-agenda-check-type t 'agenda 'timeline)
7941 (org-agenda-check-no-diary)
7942 (let* ((marker (or (org-get-at-bol 'org-marker)
7943 (org-agenda-error)))
7944 (buffer (marker-buffer marker))
7945 (pos (marker-position marker))
7946 cdate today)
7947 (org-with-remote-undo buffer
7948 (with-current-buffer buffer
7949 (widen)
7950 (goto-char pos)
7951 (if (not (org-at-timestamp-p))
7952 (error "Cannot find time stamp"))
7953 (when (and org-agenda-move-date-from-past-immediately-to-today
7954 (equal arg 1)
7955 (or (not what) (eq what 'day))
7956 (not (save-match-data (org-at-date-range-p))))
7957 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7958 cdate (calendar-absolute-from-gregorian
7959 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7960 today (org-today))
7961 (if (> today cdate)
7962 ;; immediately shift to today
7963 (setq arg (- today cdate))))
7964 (org-timestamp-change arg (or what 'day))
7965 (when (and (org-at-date-range-p)
7966 (re-search-backward org-tr-regexp-both (point-at-bol)))
7967 (let ((end org-last-changed-timestamp))
7968 (org-timestamp-change arg (or what 'day))
7969 (setq org-last-changed-timestamp
7970 (concat org-last-changed-timestamp "--" end)))))
7971 (org-agenda-show-new-time marker org-last-changed-timestamp))
7972 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7974 (defun org-agenda-date-earlier (arg &optional what)
7975 "Change the date of this item to ARG day(s) earlier."
7976 (interactive "p")
7977 (org-agenda-date-later (- arg) what))
7979 (defun org-agenda-date-later-minutes (arg)
7980 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7981 (interactive "p")
7982 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7983 (org-agenda-date-later arg 'minute))
7985 (defun org-agenda-date-earlier-minutes (arg)
7986 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7987 (interactive "p")
7988 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7989 (org-agenda-date-earlier arg 'minute))
7991 (defun org-agenda-date-later-hours (arg)
7992 "Change the time of this item, in hour steps."
7993 (interactive "p")
7994 (org-agenda-date-later arg 'hour))
7996 (defun org-agenda-date-earlier-hours (arg)
7997 "Change the time of this item, in hour steps."
7998 (interactive "p")
7999 (org-agenda-date-earlier arg 'hour))
8001 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8002 "Show new date stamp via text properties."
8003 ;; We use text properties to make this undoable
8004 (let ((inhibit-read-only t)
8005 (buffer-invisibility-spec))
8006 (setq stamp (concat " " prefix " => " stamp))
8007 (save-excursion
8008 (goto-char (point-max))
8009 (while (not (bobp))
8010 (when (equal marker (org-get-at-bol 'org-marker))
8011 (org-move-to-column (- (window-width) (length stamp)) t)
8012 (org-agenda-fix-tags-filter-overlays-at (point))
8013 (if (featurep 'xemacs)
8014 ;; Use `duplicable' property to trigger undo recording
8015 (let ((ex (make-extent nil nil))
8016 (gl (make-glyph stamp)))
8017 (set-glyph-face gl 'secondary-selection)
8018 (set-extent-properties
8019 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8020 (insert-extent ex (1- (point)) (point-at-eol)))
8021 (add-text-properties
8022 (1- (point)) (point-at-eol)
8023 (list 'display (org-add-props stamp nil
8024 'face 'secondary-selection))))
8025 (beginning-of-line 1))
8026 (beginning-of-line 0)))))
8028 (defun org-agenda-date-prompt (arg)
8029 "Change the date of this item. Date is prompted for, with default today.
8030 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8031 be used to request time specification in the time stamp."
8032 (interactive "P")
8033 (org-agenda-check-type t 'agenda 'timeline)
8034 (org-agenda-check-no-diary)
8035 (let* ((marker (or (org-get-at-bol 'org-marker)
8036 (org-agenda-error)))
8037 (buffer (marker-buffer marker))
8038 (pos (marker-position marker)))
8039 (org-with-remote-undo buffer
8040 (with-current-buffer buffer
8041 (widen)
8042 (goto-char pos)
8043 (if (not (org-at-timestamp-p t))
8044 (error "Cannot find time stamp"))
8045 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8046 (org-agenda-show-new-time marker org-last-changed-timestamp))
8047 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8049 (defun org-agenda-schedule (arg &optional time)
8050 "Schedule the item at point.
8051 ARG is passed through to `org-schedule'."
8052 (interactive "P")
8053 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8054 (org-agenda-check-no-diary)
8055 (let* ((marker (or (org-get-at-bol 'org-marker)
8056 (org-agenda-error)))
8057 (type (marker-insertion-type marker))
8058 (buffer (marker-buffer marker))
8059 (pos (marker-position marker))
8060 (org-insert-labeled-timestamps-at-point nil)
8062 (set-marker-insertion-type marker t)
8063 (org-with-remote-undo buffer
8064 (with-current-buffer buffer
8065 (widen)
8066 (goto-char pos)
8067 (setq ts (org-schedule arg time)))
8068 (org-agenda-show-new-time marker ts "S"))
8069 (message "Item scheduled for %s" ts)))
8071 (defun org-agenda-deadline (arg &optional time)
8072 "Schedule the item at point.
8073 ARG is passed through to `org-deadline'."
8074 (interactive "P")
8075 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8076 (org-agenda-check-no-diary)
8077 (let* ((marker (or (org-get-at-bol 'org-marker)
8078 (org-agenda-error)))
8079 (buffer (marker-buffer marker))
8080 (pos (marker-position marker))
8081 (org-insert-labeled-timestamps-at-point nil)
8083 (org-with-remote-undo buffer
8084 (with-current-buffer buffer
8085 (widen)
8086 (goto-char pos)
8087 (setq ts (org-deadline arg time)))
8088 (org-agenda-show-new-time marker ts "D"))
8089 (message "Deadline for this item set to %s" ts)))
8091 (defun org-agenda-action ()
8092 "Select entry for agenda action, or execute an agenda action.
8093 This command prompts for another letter. Valid inputs are:
8095 m Mark the entry at point for an agenda action
8096 s Schedule the marked entry to the date at the cursor
8097 d Set the deadline of the marked entry to the date at the cursor
8098 r Call `org-remember' with cursor date as the default date
8099 c Call `org-capture' with cursor date as the default date
8100 SPC Show marked entry in other window
8101 TAB Visit marked entry in other window
8103 The cursor may be at a date in the calendar, or in the Org agenda."
8104 (interactive)
8105 (let (ans)
8106 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8107 (setq ans (read-char-exclusive))
8108 (cond
8109 ((equal ans ?m)
8110 ;; Mark this entry
8111 (if (eq major-mode 'org-agenda-mode)
8112 (let ((m (or (org-get-at-bol 'org-hd-marker)
8113 (org-get-at-bol 'org-marker))))
8114 (if m
8115 (progn
8116 (move-marker org-agenda-action-marker
8117 (marker-position m) (marker-buffer m))
8118 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8119 (error "Don't know which entry to mark")))
8120 (error "This command works only in the agenda")))
8121 ((equal ans ?s)
8122 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8123 ((equal ans ?d)
8124 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8125 ((equal ans ?r)
8126 (org-agenda-do-action '(org-remember) t))
8127 ((equal ans ?c)
8128 (org-agenda-do-action '(org-capture) t))
8129 ((equal ans ?\ )
8130 (let ((cw (selected-window)))
8131 (org-switch-to-buffer-other-window
8132 (marker-buffer org-agenda-action-marker))
8133 (goto-char org-agenda-action-marker)
8134 (org-show-context 'agenda)
8135 (select-window cw)))
8136 ((equal ans ?\C-i)
8137 (org-switch-to-buffer-other-window
8138 (marker-buffer org-agenda-action-marker))
8139 (goto-char org-agenda-action-marker)
8140 (org-show-context 'agenda))
8141 (t (error "Invalid agenda action %c" ans)))))
8143 (defun org-agenda-do-action (form &optional current-buffer)
8144 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8145 (let ((org-overriding-default-time (org-get-cursor-date)))
8146 (if current-buffer
8147 (eval form)
8148 (if (not (marker-buffer org-agenda-action-marker))
8149 (error "No entry has been selected for agenda action")
8150 (with-current-buffer (marker-buffer org-agenda-action-marker)
8151 (save-excursion
8152 (save-restriction
8153 (widen)
8154 (goto-char org-agenda-action-marker)
8155 (eval form))))))))
8157 (defun org-agenda-clock-in (&optional arg)
8158 "Start the clock on the currently selected item."
8159 (interactive "P")
8160 (org-agenda-check-no-diary)
8161 (if (equal arg '(4))
8162 (org-clock-in arg)
8163 (let* ((marker (or (org-get-at-bol 'org-marker)
8164 (org-agenda-error)))
8165 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8166 marker))
8167 (pos (marker-position marker))
8168 newhead)
8169 (org-with-remote-undo (marker-buffer marker)
8170 (with-current-buffer (marker-buffer marker)
8171 (widen)
8172 (goto-char pos)
8173 (org-show-context 'agenda)
8174 (org-show-entry)
8175 (org-cycle-hide-drawers 'children)
8176 (org-clock-in arg)
8177 (setq newhead (org-get-heading)))
8178 (org-agenda-change-all-lines newhead hdmarker)))))
8180 (defun org-agenda-clock-out ()
8181 "Stop the currently running clock."
8182 (interactive)
8183 (unless (marker-buffer org-clock-marker)
8184 (error "No running clock"))
8185 (let ((marker (make-marker)) newhead)
8186 (org-with-remote-undo (marker-buffer org-clock-marker)
8187 (with-current-buffer (marker-buffer org-clock-marker)
8188 (save-excursion
8189 (save-restriction
8190 (widen)
8191 (goto-char org-clock-marker)
8192 (org-back-to-heading t)
8193 (move-marker marker (point))
8194 (org-clock-out)
8195 (setq newhead (org-get-heading))))))
8196 (org-agenda-change-all-lines newhead marker)
8197 (move-marker marker nil)))
8199 (defun org-agenda-clock-cancel (&optional arg)
8200 "Cancel the currently running clock."
8201 (interactive "P")
8202 (unless (marker-buffer org-clock-marker)
8203 (error "No running clock"))
8204 (org-with-remote-undo (marker-buffer org-clock-marker)
8205 (org-clock-cancel)))
8207 (defun org-agenda-clock-goto ()
8208 "Jump to the currently clocked in task within the agenda.
8209 If the currently clocked in task is not listed in the agenda
8210 buffer, display it in another window."
8211 (interactive)
8212 (let (pos)
8213 (mapc (lambda (o)
8214 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8215 (setq pos (overlay-start o))))
8216 (overlays-in (point-min) (point-max)))
8217 (cond (pos (goto-char pos))
8218 ;; If the currently clocked entry is not in the agenda
8219 ;; buffer, we visit it in another window:
8220 (org-clock-current-task
8221 (org-switch-to-buffer-other-window (org-clock-goto)))
8222 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8224 (defun org-agenda-diary-entry-in-org-file ()
8225 "Make a diary entry in the file `org-agenda-diary-file'."
8226 (let (d1 d2 char (text "") dp1 dp2)
8227 (if (equal (buffer-name) "*Calendar*")
8228 (setq d1 (calendar-cursor-to-date t)
8229 d2 (car calendar-mark-ring))
8230 (setq dp1 (get-text-property (point-at-bol) 'day))
8231 (unless dp1 (error "No date defined in current line"))
8232 (setq d1 (calendar-gregorian-from-absolute dp1)
8233 d2 (and (ignore-errors (mark))
8234 (save-excursion
8235 (goto-char (mark))
8236 (setq dp2 (get-text-property (point-at-bol) 'day)))
8237 (calendar-gregorian-from-absolute dp2))))
8238 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8239 (setq char (read-char-exclusive))
8240 (cond
8241 ((equal char ?d)
8242 (setq text (read-string "Day entry: "))
8243 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8244 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8245 ((equal char ?a)
8246 (setq d1 (list (car d1) (nth 1 d1)
8247 (read-number (format "Reference year [%d]: " (nth 2 d1))
8248 (nth 2 d1))))
8249 (setq text (read-string "Anniversary (use %d to show years): "))
8250 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8251 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8252 ((equal char ?b)
8253 (setq text (read-string "Block entry: "))
8254 (unless (and d1 d2 (not (equal d1 d2)))
8255 (error "No block of days selected"))
8256 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8257 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8258 ((equal char ?j)
8259 (org-switch-to-buffer-other-window
8260 (find-file-noselect org-agenda-diary-file))
8261 (require 'org-datetree)
8262 (org-datetree-find-date-create d1)
8263 (org-reveal t))
8264 (t (error "Invalid selection character `%c'" char)))))
8266 (defcustom org-agenda-insert-diary-strategy 'date-tree
8267 "Where in `org-agenda-diary-file' should new entries be added?
8268 Valid values:
8270 date-tree in the date tree, as child of the date
8271 top-level as top-level entries at the end of the file."
8272 :group 'org-agenda
8273 :type '(choice
8274 (const :tag "in a date tree" date-tree)
8275 (const :tag "as top level at end of file" top-level)))
8277 (defcustom org-agenda-insert-diary-extract-time nil
8278 "Non-nil means extract any time specification from the diary entry."
8279 :group 'org-agenda
8280 :version "24.1"
8281 :type 'boolean)
8283 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8284 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8285 If TEXT is not empty, it will become the headline of the new entry, and
8286 the resulting entry will not be shown. When TEXT is empty, switch to
8287 `org-agenda-diary-file' and let the user finish the entry there."
8288 (let ((cw (current-window-configuration)))
8289 (org-switch-to-buffer-other-window
8290 (find-file-noselect org-agenda-diary-file))
8291 (widen)
8292 (goto-char (point-min))
8293 (cond
8294 ((eq type 'anniversary)
8295 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8296 (progn
8297 (or (org-at-heading-p t)
8298 (progn
8299 (outline-next-heading)
8300 (insert "* Anniversaries\n\n")
8301 (beginning-of-line -1)))))
8302 (outline-next-heading)
8303 (org-back-over-empty-lines)
8304 (backward-char 1)
8305 (insert "\n")
8306 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8307 (nth 2 d1) (car d1) (nth 1 d1) text)))
8308 ((eq type 'day)
8309 (let ((org-prefix-has-time t)
8310 (org-agenda-time-leading-zero t)
8311 fmt time time2)
8312 (if org-agenda-insert-diary-extract-time
8313 ;; Use org-agenda-format-item to parse text for a time-range and
8314 ;; remove it. FIXME: This is a hack, we should refactor
8315 ;; that function to make time extraction available separately
8316 (setq fmt (org-agenda-format-item nil text nil nil t)
8317 time (get-text-property 0 'time fmt)
8318 time2 (if (> (length time) 0)
8319 ;; split-string removes trailing ...... if
8320 ;; no end time given. First space
8321 ;; separates time from date.
8322 (concat " " (car (split-string time "\\.")))
8323 nil)
8324 text (get-text-property 0 'txt fmt)))
8325 (if (eq org-agenda-insert-diary-strategy 'top-level)
8326 (org-agenda-insert-diary-as-top-level text)
8327 (require 'org-datetree)
8328 (org-datetree-find-date-create d1)
8329 (org-agenda-insert-diary-make-new-entry text))
8330 (org-insert-time-stamp (org-time-from-absolute
8331 (calendar-absolute-from-gregorian d1))
8332 nil nil nil nil time2))
8333 (end-of-line 0))
8334 ((eq type 'block)
8335 (if (> (calendar-absolute-from-gregorian d1)
8336 (calendar-absolute-from-gregorian d2))
8337 (setq d1 (prog1 d2 (setq d2 d1))))
8338 (if (eq org-agenda-insert-diary-strategy 'top-level)
8339 (org-agenda-insert-diary-as-top-level text)
8340 (require 'org-datetree)
8341 (org-datetree-find-date-create d1)
8342 (org-agenda-insert-diary-make-new-entry text))
8343 (org-insert-time-stamp (org-time-from-absolute
8344 (calendar-absolute-from-gregorian d1)))
8345 (insert "--")
8346 (org-insert-time-stamp (org-time-from-absolute
8347 (calendar-absolute-from-gregorian d2)))
8348 (end-of-line 0)))
8349 (if (string-match "\\S-" text)
8350 (progn
8351 (set-window-configuration cw)
8352 (message "%s entry added to %s"
8353 (capitalize (symbol-name type))
8354 (abbreviate-file-name org-agenda-diary-file)))
8355 (org-reveal t)
8356 (message "Please finish entry here"))))
8358 (defun org-agenda-insert-diary-as-top-level (text)
8359 "Make new entry as a top-level entry at the end of the file.
8360 Add TEXT as headline, and position the cursor in the second line so that
8361 a timestamp can be added there."
8362 (widen)
8363 (goto-char (point-max))
8364 (or (bolp) (insert "\n"))
8365 (insert "* " text "\n")
8366 (if org-adapt-indentation (org-indent-to-column 2)))
8368 (defun org-agenda-insert-diary-make-new-entry (text)
8369 "Make new entry as last child of current entry.
8370 Add TEXT as headline, and position the cursor in the second line so that
8371 a timestamp can be added there."
8372 (let ((org-show-following-heading t)
8373 (org-show-siblings t)
8374 (org-show-hierarchy-above t)
8375 (org-show-entry-below t)
8376 col)
8377 (outline-next-heading)
8378 (org-back-over-empty-lines)
8379 (or (looking-at "[ \t]*$")
8380 (progn (insert "\n") (backward-char 1)))
8381 (org-insert-heading nil t)
8382 (org-do-demote)
8383 (setq col (current-column))
8384 (insert text "\n")
8385 (if org-adapt-indentation (org-indent-to-column col))
8386 (let ((org-show-following-heading t)
8387 (org-show-siblings t)
8388 (org-show-hierarchy-above t)
8389 (org-show-entry-below t))
8390 (org-show-context))))
8392 (defun org-agenda-diary-entry ()
8393 "Make a diary entry, like the `i' command from the calendar.
8394 All the standard commands work: block, weekly etc.
8395 When `org-agenda-diary-file' points to a file,
8396 `org-agenda-diary-entry-in-org-file' is called instead to create
8397 entries in that Org-mode file."
8398 (interactive)
8399 (org-agenda-check-type t 'agenda 'timeline)
8400 (if (not (eq org-agenda-diary-file 'diary-file))
8401 (org-agenda-diary-entry-in-org-file)
8402 (require 'diary-lib)
8403 (let* ((char (progn
8404 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8405 (read-char-exclusive)))
8406 (cmd (cdr (assoc char
8407 '((?d . insert-diary-entry)
8408 (?w . insert-weekly-diary-entry)
8409 (?m . insert-monthly-diary-entry)
8410 (?y . insert-yearly-diary-entry)
8411 (?a . insert-anniversary-diary-entry)
8412 (?b . insert-block-diary-entry)
8413 (?c . insert-cyclic-diary-entry)))))
8414 (oldf (symbol-function 'calendar-cursor-to-date))
8415 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8416 (point (point))
8417 (mark (or (mark t) (point))))
8418 (unless cmd
8419 (error "No command associated with <%c>" char))
8420 (unless (and (get-text-property point 'day)
8421 (or (not (equal ?b char))
8422 (get-text-property mark 'day)))
8423 (error "Don't know which date to use for diary entry"))
8424 ;; We implement this by hacking the `calendar-cursor-to-date' function
8425 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8426 (let ((calendar-mark-ring
8427 (list (calendar-gregorian-from-absolute
8428 (or (get-text-property mark 'day)
8429 (get-text-property point 'day))))))
8430 (unwind-protect
8431 (progn
8432 (fset 'calendar-cursor-to-date
8433 (lambda (&optional error dummy)
8434 (calendar-gregorian-from-absolute
8435 (get-text-property point 'day))))
8436 (call-interactively cmd))
8437 (fset 'calendar-cursor-to-date oldf))))))
8439 (defun org-agenda-execute-calendar-command (cmd)
8440 "Execute a calendar command from the agenda, with the date associated to
8441 the cursor position."
8442 (org-agenda-check-type t 'agenda 'timeline)
8443 (require 'diary-lib)
8444 (unless (get-text-property (point) 'day)
8445 (error "Don't know which date to use for calendar command"))
8446 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8447 (point (point))
8448 (date (calendar-gregorian-from-absolute
8449 (get-text-property point 'day)))
8450 ;; the following 2 vars are needed in the calendar
8451 (displayed-month (car date))
8452 (displayed-year (nth 2 date)))
8453 (unwind-protect
8454 (progn
8455 (fset 'calendar-cursor-to-date
8456 (lambda (&optional error dummy)
8457 (calendar-gregorian-from-absolute
8458 (get-text-property point 'day))))
8459 (call-interactively cmd))
8460 (fset 'calendar-cursor-to-date oldf))))
8462 (defun org-agenda-phases-of-moon ()
8463 "Display the phases of the moon for the 3 months around the cursor date."
8464 (interactive)
8465 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8467 (defun org-agenda-holidays ()
8468 "Display the holidays for the 3 months around the cursor date."
8469 (interactive)
8470 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8472 (defvar calendar-longitude)
8473 (defvar calendar-latitude)
8474 (defvar calendar-location-name)
8476 (defun org-agenda-sunrise-sunset (arg)
8477 "Display sunrise and sunset for the cursor date.
8478 Latitude and longitude can be specified with the variables
8479 `calendar-latitude' and `calendar-longitude'. When called with prefix
8480 argument, latitude and longitude will be prompted for."
8481 (interactive "P")
8482 (require 'solar)
8483 (let ((calendar-longitude (if arg nil calendar-longitude))
8484 (calendar-latitude (if arg nil calendar-latitude))
8485 (calendar-location-name
8486 (if arg "the given coordinates" calendar-location-name)))
8487 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8489 (defun org-agenda-goto-calendar ()
8490 "Open the Emacs calendar with the date at the cursor."
8491 (interactive)
8492 (org-agenda-check-type t 'agenda 'timeline)
8493 (let* ((day (or (get-text-property (point) 'day)
8494 (error "Don't know which date to open in calendar")))
8495 (date (calendar-gregorian-from-absolute day))
8496 (calendar-move-hook nil)
8497 (calendar-view-holidays-initially-flag nil)
8498 (calendar-view-diary-initially-flag nil))
8499 (calendar)
8500 (calendar-goto-date date)))
8502 ;;;###autoload
8503 (defun org-calendar-goto-agenda ()
8504 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8505 This is a command that has to be installed in `calendar-mode-map'."
8506 (interactive)
8507 (org-agenda-list nil (calendar-absolute-from-gregorian
8508 (calendar-cursor-to-date))
8509 nil))
8511 (defun org-agenda-convert-date ()
8512 (interactive)
8513 (org-agenda-check-type t 'agenda 'timeline)
8514 (let ((day (get-text-property (point) 'day))
8515 date s)
8516 (unless day
8517 (error "Don't know which date to convert"))
8518 (setq date (calendar-gregorian-from-absolute day))
8519 (setq s (concat
8520 "Gregorian: " (calendar-date-string date) "\n"
8521 "ISO: " (calendar-iso-date-string date) "\n"
8522 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8523 "Julian: " (calendar-julian-date-string date) "\n"
8524 "Astron. JD: " (calendar-astro-date-string date)
8525 " (Julian date number at noon UTC)\n"
8526 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8527 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8528 "French: " (calendar-french-date-string date) "\n"
8529 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8530 "Mayan: " (calendar-mayan-date-string date) "\n"
8531 "Coptic: " (calendar-coptic-date-string date) "\n"
8532 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8533 "Persian: " (calendar-persian-date-string date) "\n"
8534 "Chinese: " (calendar-chinese-date-string date) "\n"))
8535 (with-output-to-temp-buffer "*Dates*"
8536 (princ s))
8537 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8539 ;;; Bulk commands
8541 (defvar org-agenda-bulk-marked-entries nil
8542 "List of markers that refer to marked entries in the agenda.")
8544 (defun org-agenda-bulk-marked-p ()
8545 (eq (get-char-property (point-at-bol) 'type)
8546 'org-marked-entry-overlay))
8548 (defun org-agenda-bulk-mark (&optional arg)
8549 "Mark the entry at point for future bulk action."
8550 (interactive "p")
8551 (dotimes (i (max arg 1))
8552 (unless (org-get-at-bol 'org-agenda-diary-link)
8553 (let* ((m (org-get-at-bol 'org-hd-marker))
8555 (unless (org-agenda-bulk-marked-p)
8556 (unless m (error "Nothing to mark at point"))
8557 (push m org-agenda-bulk-marked-entries)
8558 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8559 (org-overlay-display ov "> "
8560 (org-get-todo-face "TODO")
8561 'evaporate)
8562 (overlay-put ov 'type 'org-marked-entry-overlay))
8563 (beginning-of-line 2)
8564 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8565 (beginning-of-line 2))
8566 (message "%d entries marked for bulk action"
8567 (length org-agenda-bulk-marked-entries))))))
8569 (defun org-agenda-bulk-mark-regexp (regexp)
8570 "Mark entries match REGEXP."
8571 (interactive "sMark entries matching regexp: ")
8572 (let (entries-marked)
8573 (save-excursion
8574 (goto-char (point-min))
8575 (goto-char (next-single-property-change (point) 'txt))
8576 (while (re-search-forward regexp nil t)
8577 (when (string-match regexp (get-text-property (point) 'txt))
8578 (setq entries-marked (+ entries-marked 1))
8579 (call-interactively 'org-agenda-bulk-mark))))
8580 (if (not entries-marked)
8581 (message "No entry matching this regexp."))))
8583 (defun org-agenda-bulk-unmark ()
8584 "Unmark the entry at point for future bulk action."
8585 (interactive)
8586 (when (org-agenda-bulk-marked-p)
8587 (org-agenda-bulk-remove-overlays
8588 (point-at-bol) (+ 2 (point-at-bol)))
8589 (setq org-agenda-bulk-marked-entries
8590 (delete (org-get-at-bol 'org-hd-marker)
8591 org-agenda-bulk-marked-entries)))
8592 (beginning-of-line 2)
8593 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8594 (beginning-of-line 2))
8595 (message "%d entries marked for bulk action"
8596 (length org-agenda-bulk-marked-entries)))
8598 (defun org-agenda-bulk-toggle ()
8599 "Toggle marking the entry at point for bulk action."
8600 (interactive)
8601 (if (org-agenda-bulk-marked-p)
8602 (org-agenda-bulk-unmark)
8603 (org-agenda-bulk-mark)))
8605 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8606 "Remove the mark overlays between BEG and END in the agenda buffer.
8607 BEG and END default to the buffer limits.
8609 This only removes the overlays, it does not remove the markers
8610 from the list in `org-agenda-bulk-marked-entries'."
8611 (interactive)
8612 (mapc (lambda (ov)
8613 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8614 (delete-overlay ov)))
8615 (overlays-in (or beg (point-min)) (or end (point-max)))))
8617 (defun org-agenda-bulk-remove-all-marks ()
8618 "Remove all marks in the agenda buffer.
8619 This will remove the markers, and the overlays."
8620 (interactive)
8621 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8622 (setq org-agenda-bulk-marked-entries nil)
8623 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8625 (defun org-agenda-bulk-action (&optional arg)
8626 "Execute an remote-editing action on all marked entries.
8627 The prefix arg is passed through to the command if possible."
8628 (interactive "P")
8629 ;; Make sure we have markers, and only valid ones
8630 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8631 (mapc
8632 (lambda (m)
8633 (unless (and (markerp m)
8634 (marker-buffer m)
8635 (buffer-live-p (marker-buffer m))
8636 (marker-position m))
8637 (error "Marker %s for bulk command is invalid" m)))
8638 org-agenda-bulk-marked-entries)
8640 ;; Prompt for the bulk command
8641 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8642 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8643 (when org-agenda-bulk-custom-functions
8644 (concat " Custom: ["
8645 (mapconcat (lambda(f) (char-to-string (car f)))
8646 org-agenda-bulk-custom-functions "")
8647 "]"))))
8648 (let* ((action (read-char-exclusive))
8649 (org-log-refile (if org-log-refile 'time nil))
8650 (entries (reverse org-agenda-bulk-marked-entries))
8651 redo-at-end
8652 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8653 (cond
8654 ((equal action ?$)
8655 (setq cmd '(org-agenda-archive)))
8657 ((equal action ?A)
8658 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8660 ((member action '(?r ?w))
8661 (setq rfloc (org-refile-get-location
8662 "Refile to"
8663 (marker-buffer (car org-agenda-bulk-marked-entries))
8664 org-refile-allow-creating-parent-nodes))
8665 (if (nth 3 rfloc)
8666 (setcar (nthcdr 3 rfloc)
8667 (move-marker (make-marker) (nth 3 rfloc)
8668 (or (get-file-buffer (nth 1 rfloc))
8669 (find-buffer-visiting (nth 1 rfloc))
8670 (error "This should not happen")))))
8672 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8673 redo-at-end t))
8675 ((equal action ?t)
8676 (setq state (org-icompleting-read
8677 "Todo state: "
8678 (with-current-buffer (marker-buffer (car entries))
8679 (mapcar 'list org-todo-keywords-1))))
8680 (setq cmd `(let ((org-inhibit-blocking t)
8681 (org-inhibit-logging 'note))
8682 (org-agenda-todo ,state))))
8684 ((memq action '(?- ?+))
8685 (setq tag (org-icompleting-read
8686 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8687 (with-current-buffer (marker-buffer (car entries))
8688 (delq nil
8689 (mapcar (lambda (x)
8690 (if (stringp (car x)) x)) org-tag-alist)))))
8691 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8693 ((memq action '(?s ?d))
8694 (let* ((date (unless arg
8695 (org-read-date
8696 nil nil nil
8697 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8698 (ans (if arg nil org-read-date-final-answer))
8699 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8700 (setq cmd `(let* ((bound (fboundp 'read-string))
8701 (old (and bound (symbol-function 'read-string))))
8702 (unwind-protect
8703 (progn
8704 (fset 'read-string (lambda (&rest ignore) ,ans))
8705 (eval '(,c1 arg)))
8706 (if bound
8707 (fset 'read-string old)
8708 (fmakunbound 'read-string)))))))
8710 ((equal action ?S)
8711 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8712 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8713 (let ((days (read-number
8714 (format "Scatter tasks across how many %sdays: "
8715 (if arg "week" "")) 7)))
8716 (setq cmd
8717 `(let ((distance (1+ (random ,days))))
8718 (if arg
8719 (let ((dist distance)
8720 (day-of-week
8721 (calendar-day-of-week
8722 (calendar-gregorian-from-absolute (org-today)))))
8723 (dotimes (i (1+ dist))
8724 (while (member day-of-week org-agenda-weekend-days)
8725 (incf distance)
8726 (incf day-of-week)
8727 (if (= day-of-week 7)
8728 (setq day-of-week 0)))
8729 (incf day-of-week)
8730 (if (= day-of-week 7)
8731 (setq day-of-week 0)))))
8732 ;; silently fail when try to replan a sexp entry
8733 (condition-case nil
8734 (let* ((date (calendar-gregorian-from-absolute
8735 (+ (org-today) distance)))
8736 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8737 (nth 2 date))))
8738 (org-agenda-schedule nil time))
8739 (error nil)))))))
8741 ((assoc action org-agenda-bulk-custom-functions)
8742 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8743 redo-at-end t))
8745 ((equal action ?f)
8746 (setq cmd (list (intern
8747 (org-icompleting-read "Function: "
8748 obarray 'fboundp t nil nil)))))
8750 (t (error "Invalid bulk action")))
8752 ;; Sort the markers, to make sure that parents are handled before children
8753 (setq entries (sort entries
8754 (lambda (a b)
8755 (cond
8756 ((equal (marker-buffer a) (marker-buffer b))
8757 (< (marker-position a) (marker-position b)))
8759 (string< (buffer-name (marker-buffer a))
8760 (buffer-name (marker-buffer b))))))))
8762 ;; Now loop over all markers and apply cmd
8763 (while (setq e (pop entries))
8764 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8765 (if (not pos)
8766 (progn (message "Skipping removed entry at %s" e)
8767 (setq cntskip (1+ cntskip)))
8768 (goto-char pos)
8769 (let (org-loop-over-headlines-in-active-region)
8770 (eval cmd))
8771 (setq org-agenda-bulk-marked-entries
8772 (delete e org-agenda-bulk-marked-entries))
8773 (setq cnt (1+ cnt))))
8774 (setq org-agenda-bulk-marked-entries nil)
8775 (org-agenda-bulk-remove-all-marks)
8776 (when redo-at-end (org-agenda-redo))
8777 (message "Acted on %d entries%s"
8779 (if (= cntskip 0)
8781 (format ", skipped %d (disappeared before their turn)"
8782 cntskip)))))
8784 ;;; Flagging notes
8786 (defun org-agenda-show-the-flagging-note ()
8787 "Display the flagging note in the other window.
8788 When called a second time in direct sequence, offer to remove the FLAGGING
8789 tag and (if present) the flagging note."
8790 (interactive)
8791 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8792 (win (selected-window))
8793 note heading newhead)
8794 (unless hdmarker
8795 (error "No linked entry at point"))
8796 (if (and (eq this-command last-command)
8797 (y-or-n-p "Unflag and remove any flagging note? "))
8798 (progn
8799 (org-agenda-remove-flag hdmarker)
8800 (let ((win (get-buffer-window "*Flagging Note*")))
8801 (and win (delete-window win)))
8802 (message "Entry unflagged"))
8803 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8804 (unless note
8805 (error "No flagging note"))
8806 (org-kill-new note)
8807 (org-switch-to-buffer-other-window "*Flagging Note*")
8808 (erase-buffer)
8809 (insert note)
8810 (goto-char (point-min))
8811 (while (re-search-forward "\\\\n" nil t)
8812 (replace-match "\n" t t))
8813 (goto-char (point-min))
8814 (select-window win)
8815 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8817 (defun org-agenda-remove-flag (marker)
8818 "Remove the FLAGGED tag and any flagging note in the entry."
8819 (let (newhead)
8820 (org-with-point-at marker
8821 (org-toggle-tag "FLAGGED" 'off)
8822 (org-entry-delete nil "THEFLAGGINGNOTE")
8823 (setq newhead (org-get-heading)))
8824 (org-agenda-change-all-lines newhead marker)
8825 (message "Entry unflagged")))
8827 (defun org-agenda-get-any-marker (&optional pos)
8828 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8829 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8831 ;;; Appointment reminders
8833 (defvar appt-time-msg-list)
8835 ;;;###autoload
8836 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8837 "Activate appointments found in `org-agenda-files'.
8838 With a \\[universal-argument] prefix, refresh the list of
8839 appointments.
8841 If FILTER is t, interactively prompt the user for a regular
8842 expression, and filter out entries that don't match it.
8844 If FILTER is a string, use this string as a regular expression
8845 for filtering entries out.
8847 If FILTER is a function, filter out entries against which
8848 calling the function returns nil. This function takes one
8849 argument: an entry from `org-agenda-get-day-entries'.
8851 FILTER can also be an alist with the car of each cell being
8852 either 'headline or 'category. For example:
8854 '((headline \"IMPORTANT\")
8855 (category \"Work\"))
8857 will only add headlines containing IMPORTANT or headlines
8858 belonging to the \"Work\" category.
8860 ARGS are symbols indicating what kind of entries to consider.
8861 By default `org-agenda-to-appt' will use :deadline, :scheduled
8862 and :timestamp entries. See the docstring of `org-diary' for
8863 details and examples."
8864 (interactive "P")
8865 (if refresh (setq appt-time-msg-list nil))
8866 (if (eq filter t)
8867 (setq filter (read-from-minibuffer "Regexp filter: ")))
8868 (let* ((cnt 0) ; count added events
8869 (scope (or args '(:deadline :scheduled :timestamp)))
8870 (org-agenda-new-buffers nil)
8871 (org-deadline-warning-days 0)
8872 ;; Do not use `org-today' here because appt only takes
8873 ;; time and without date as argument, so it may pass wrong
8874 ;; information otherwise
8875 (today (org-date-to-gregorian
8876 (time-to-days (current-time))))
8877 (org-agenda-restrict nil)
8878 (files (org-agenda-files 'unrestricted)) entries file)
8879 ;; Get all entries which may contain an appt
8880 (org-prepare-agenda-buffers files)
8881 (while (setq file (pop files))
8882 (setq entries
8883 (delq nil
8884 (append entries
8885 (apply 'org-agenda-get-day-entries
8886 file today scope)))))
8887 ;; Map thru entries and find if we should filter them out
8888 (mapc
8889 (lambda(x)
8890 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8891 (cat (get-text-property 1 'org-category x))
8892 (tod (get-text-property 1 'time-of-day x))
8893 (ok (or (null filter)
8894 (and (stringp filter) (string-match filter evt))
8895 (and (functionp filter) (funcall filter x))
8896 (and (listp filter)
8897 (let ((cat-filter (cadr (assoc 'category filter)))
8898 (evt-filter (cadr (assoc 'headline filter))))
8899 (or (and (stringp cat-filter)
8900 (string-match cat-filter cat))
8901 (and (stringp evt-filter)
8902 (string-match evt-filter evt))))))))
8903 ;; FIXME: Shall we remove text-properties for the appt text?
8904 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8905 (when (and ok tod)
8906 (setq tod (concat "00" (number-to-string tod))
8907 tod (when (string-match
8908 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8909 (concat (match-string 1 tod) ":"
8910 (match-string 2 tod))))
8911 (appt-add tod evt)
8912 (setq cnt (1+ cnt))))) entries)
8913 (org-release-buffers org-agenda-new-buffers)
8914 (if (eq cnt 0)
8915 (message "No event to add")
8916 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8918 (defun org-agenda-todayp (date)
8919 "Does DATE mean today, when considering `org-extend-today-until'?"
8920 (let ((today (org-today))
8921 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8922 date)))
8923 (eq date today)))
8925 (defun org-agenda-todo-yesterday (&optional arg)
8926 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8927 (interactive "P")
8928 (let* ((hour (third (decode-time
8929 (org-current-time))))
8930 (org-extend-today-until (1+ hour)))
8931 (org-agenda-todo arg)))
8933 (provide 'org-agenda)
8935 ;;; org-agenda.el ends here