Fix bulk scattering command
[org-mode.git] / lisp / org-agenda.el
blobb519ff2b5e18fffdd61e429035e73582712ed1aa
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.5
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl))
37 (declare-function diary-add-to-list "diary-lib"
38 (date string specifier &optional marker globcolor literal))
39 (declare-function calendar-absolute-from-iso "cal-iso" (date))
40 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
41 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
42 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
43 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
44 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
45 (declare-function calendar-french-date-string "cal-french" (&optional date))
46 (declare-function calendar-goto-date "cal-move" (date))
47 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
48 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
49 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
50 (declare-function calendar-iso-from-absolute "cal-iso" (date))
51 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
52 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
53 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
54 (declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
56 (declare-function org-columns-quit "org-colview" ())
57 (declare-function diary-date-display-form "diary-lib" (&optional type))
58 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
59 (declare-function org-habit-insert-consistency-graphs
60 "org-habit" (&optional line))
61 (declare-function org-is-habit-p "org-habit" (&optional pom))
62 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
63 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
64 (defvar calendar-mode-map)
65 (defvar org-clock-current-task) ; defined in org-clock.el
66 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
67 (defvar org-habit-show-habits)
68 (defvar org-habit-show-habits-only-for-today)
70 ;; Defined somewhere in this file, but used before definition.
71 (defvar org-agenda-buffer-name)
72 (defvar org-agenda-overriding-header)
73 (defvar org-agenda-title-append nil)
74 (defvar entry)
75 (defvar date)
76 (defvar org-agenda-undo-list)
77 (defvar org-agenda-pending-undo-list)
78 (defvar original-date) ; dynamically scoped, calendar.el does scope this
80 (defcustom org-agenda-confirm-kill 1
81 "When set, remote killing from the agenda buffer needs confirmation.
82 When t, a confirmation is always needed. When a number N, confirmation is
83 only needed when the text to be killed contains more than N non-white lines."
84 :group 'org-agenda
85 :type '(choice
86 (const :tag "Never" nil)
87 (const :tag "Always" t)
88 (integer :tag "When more than N lines")))
90 (defcustom org-agenda-compact-blocks nil
91 "Non-nil means make the block agenda more compact.
92 This is done by leaving out unnecessary lines."
93 :group 'org-agenda
94 :type 'boolean)
96 (defcustom org-agenda-block-separator ?=
97 "The separator between blocks in the agenda.
98 If this is a string, it will be used as the separator, with a newline added.
99 If it is a character, it will be repeated to fill the window width."
100 :group 'org-agenda
101 :type '(choice
102 (character)
103 (string)))
105 (defgroup org-agenda-export nil
106 "Options concerning exporting agenda views in Org-mode."
107 :tag "Org Agenda Export"
108 :group 'org-agenda)
110 (defcustom org-agenda-with-colors t
111 "Non-nil means use colors in agenda views."
112 :group 'org-agenda-export
113 :type 'boolean)
115 (defcustom org-agenda-exporter-settings nil
116 "Alist of variable/value pairs that should be active during agenda export.
117 This is a good place to set options for ps-print and for htmlize.
118 Note that the way this is implemented, the values will be evaluated
119 before assigned to the variables. So make sure to quote values you do
120 *not* want evaluated, for example
122 (setq org-agenda-exporter-settings
123 '((ps-print-color-p 'black-white)))"
124 :group 'org-agenda-export
125 :type '(repeat
126 (list
127 (variable)
128 (sexp :tag "Value"))))
130 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
131 "Hook run in temporary buffer before writing it to an export file.
132 A useful function is `org-agenda-add-entry-text'."
133 :group 'org-agenda-export
134 :type 'hook
135 :options '(org-agenda-add-entry-text))
137 (defcustom org-agenda-add-entry-text-maxlines 0
138 "Maximum number of entry text lines to be added to agenda.
139 This is only relevant when `org-agenda-add-entry-text' is part of
140 `org-agenda-before-write-hook', which it is by default.
141 When this is 0, nothing will happen. When it is greater than 0, it
142 specifies the maximum number of lines that will be added for each entry
143 that is listed in the agenda view.
145 Note that this variable is not used during display, only when exporting
146 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
147 and `org-agenda-entry-text-maxlines'."
148 :group 'org-agenda
149 :type 'integer)
151 (defcustom org-agenda-add-entry-text-descriptive-links t
152 "Non-nil means export org-links as descriptive links in agenda added text.
153 This variable applies to the text added to the agenda when
154 `org-agenda-add-entry-text-maxlines' is larger than 0.
155 When this variable nil, the URL will (also) be shown."
156 :group 'org-agenda
157 :type 'boolean)
159 (defcustom org-agenda-export-html-style ""
160 "The style specification for exported HTML Agenda files.
161 If this variable contains a string, it will replace the default <style>
162 section as produced by `htmlize'.
163 Since there are different ways of setting style information, this variable
164 needs to contain the full HTML structure to provide a style, including the
165 surrounding HTML tags. The style specifications should include definitions
166 the fonts used by the agenda, here is an example:
168 <style type=\"text/css\">
169 p { font-weight: normal; color: gray; }
170 .org-agenda-structure {
171 font-size: 110%;
172 color: #003399;
173 font-weight: 600;
175 .org-todo {
176 color: #cc6666;
177 font-weight: bold;
179 .org-agenda-done {
180 color: #339933;
182 .org-done {
183 color: #339933;
185 .title { text-align: center; }
186 .todo, .deadline { color: red; }
187 .done { color: green; }
188 </style>
190 or, if you want to keep the style in a file,
192 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
194 As the value of this option simply gets inserted into the HTML <head> header,
195 you can \"misuse\" it to also add other text to the header. However,
196 <style>...</style> is required, if not present the variable will be ignored."
197 :group 'org-agenda-export
198 :group 'org-export-html
199 :type 'string)
201 (defcustom org-agenda-persistent-filter nil
202 "When set, keep filters from one agenda view to the next."
203 :group 'org-agenda
204 :type 'boolean)
206 (defgroup org-agenda-custom-commands nil
207 "Options concerning agenda views in Org-mode."
208 :tag "Org Agenda Custom Commands"
209 :group 'org-agenda)
211 (defconst org-sorting-choice
212 '(choice
213 (const time-up) (const time-down)
214 (const category-keep) (const category-up) (const category-down)
215 (const tag-down) (const tag-up)
216 (const priority-up) (const priority-down)
217 (const todo-state-up) (const todo-state-down)
218 (const effort-up) (const effort-down)
219 (const habit-up) (const habit-down)
220 (const alpha-up) (const alpha-down)
221 (const user-defined-up) (const user-defined-down))
222 "Sorting choices.")
224 (defconst org-agenda-custom-commands-local-options
225 `(repeat :tag "Local settings for this command. Remember to quote values"
226 (choice :tag "Setting"
227 (list :tag "Heading for this block"
228 (const org-agenda-overriding-header)
229 (string :tag "Headline"))
230 (list :tag "Files to be searched"
231 (const org-agenda-files)
232 (list
233 (const :format "" quote)
234 (repeat (file))))
235 (list :tag "Sorting strategy"
236 (const org-agenda-sorting-strategy)
237 (list
238 (const :format "" quote)
239 (repeat
240 ,org-sorting-choice)))
241 (list :tag "Prefix format"
242 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
243 (string))
244 (list :tag "Number of days in agenda"
245 (const org-agenda-span)
246 (choice (const :tag "Day" 'day)
247 (const :tag "Week" 'week)
248 (const :tag "Month" 'month)
249 (const :tag "Year" 'year)
250 (integer :tag "Custom")))
251 (list :tag "Fixed starting date"
252 (const org-agenda-start-day)
253 (string :value "2007-11-01"))
254 (list :tag "Start on day of week"
255 (const org-agenda-start-on-weekday)
256 (choice :value 1
257 (const :tag "Today" nil)
258 (integer :tag "Weekday No.")))
259 (list :tag "Include data from diary"
260 (const org-agenda-include-diary)
261 (boolean))
262 (list :tag "Deadline Warning days"
263 (const org-deadline-warning-days)
264 (integer :value 1))
265 (list :tag "Tags filter preset"
266 (const org-agenda-filter-preset)
267 (list
268 (const :format "" quote)
269 (repeat
270 (string :tag "+tag or -tag"))))
271 (list :tag "Set daily/weekly entry types"
272 (const org-agenda-entry-types)
273 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
274 (const :deadline)
275 (const :scheduled)
276 (const :timestamp)
277 (const :sexp)))
278 (list :tag "Standard skipping condition"
279 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
280 (const org-agenda-skip-function)
281 (list
282 (const :format "" quote)
283 (list
284 (choice
285 :tag "Skipping range"
286 (const :tag "Skip entry" org-agenda-skip-entry-if)
287 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
288 (repeat :inline t :tag "Conditions for skipping"
289 (choice
290 :tag "Condition type"
291 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
292 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
293 (list :tag "TODO state is" :inline t
294 (const 'todo)
295 (choice
296 (const :tag "any not-done state" 'todo)
297 (const :tag "any done state" 'done)
298 (const :tag "any state" 'any)
299 (list :tag "Keyword list"
300 (const :format "" quote)
301 (repeat (string :tag "Keyword")))))
302 (list :tag "TODO state is not" :inline t
303 (const 'nottodo)
304 (choice
305 (const :tag "any not-done state" 'todo)
306 (const :tag "any done state" 'done)
307 (const :tag "any state" 'any)
308 (list :tag "Keyword list"
309 (const :format "" quote)
310 (repeat (string :tag "Keyword")))))
311 (const :tag "scheduled" 'scheduled)
312 (const :tag "not scheduled" 'notscheduled)
313 (const :tag "deadline" 'deadline)
314 (const :tag "no deadline" 'notdeadline)
315 (const :tag "timestamp" 'timestamp)
316 (const :tag "no timestamp" 'nottimestamp))))))
317 (list :tag "Non-standard skipping condition"
318 :value (org-agenda-skip-function)
319 (const org-agenda-skip-function)
320 (sexp :tag "Function or form (quoted!)"))
321 (list :tag "Any variable"
322 (variable :tag "Variable")
323 (sexp :tag "Value (sexp)"))))
324 "Selection of examples for agenda command settings.
325 This will be spliced into the custom type of
326 `org-agenda-custom-commands'.")
329 (defcustom org-agenda-custom-commands nil
330 "Custom commands for the agenda.
331 These commands will be offered on the splash screen displayed by the
332 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
334 (key desc type match settings files)
336 key The key (one or more characters as a string) to be associated
337 with the command.
338 desc A description of the command, when omitted or nil, a default
339 description is built using MATCH.
340 type The command type, any of the following symbols:
341 agenda The daily/weekly agenda.
342 todo Entries with a specific TODO keyword, in all agenda files.
343 search Entries containing search words entry or headline.
344 tags Tags/Property/TODO match in all agenda files.
345 tags-todo Tags/P/T match in all agenda files, TODO entries only.
346 todo-tree Sparse tree of specific TODO keyword in *current* file.
347 tags-tree Sparse tree with all tags matches in *current* file.
348 occur-tree Occur sparse tree for *current* file.
349 ... A user-defined function.
350 match What to search for:
351 - a single keyword for TODO keyword searches
352 - a tags match expression for tags searches
353 - a word search expression for text searches.
354 - a regular expression for occur searches
355 For all other commands, this should be the empty string.
356 settings A list of option settings, similar to that in a let form, so like
357 this: ((opt1 val1) (opt2 val2) ...). The values will be
358 evaluated at the moment of execution, so quote them when needed.
359 files A list of files file to write the produced agenda buffer to
360 with the command `org-store-agenda-views'.
361 If a file name ends in \".html\", an HTML version of the buffer
362 is written out. If it ends in \".ps\", a postscript version is
363 produced. Otherwise, only the plain text is written to the file.
365 You can also define a set of commands, to create a composite agenda buffer.
366 In this case, an entry looks like this:
368 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
370 where
372 desc A description string to be displayed in the dispatcher menu.
373 cmd An agenda command, similar to the above. However, tree commands
374 are no allowed, but instead you can get agenda and global todo list.
375 So valid commands for a set are:
376 (agenda \"\" settings)
377 (alltodo \"\" settings)
378 (stuck \"\" settings)
379 (todo \"match\" settings files)
380 (search \"match\" settings files)
381 (tags \"match\" settings files)
382 (tags-todo \"match\" settings files)
384 Each command can carry a list of options, and another set of options can be
385 given for the whole set of commands. Individual command options take
386 precedence over the general options.
388 When using several characters as key to a command, the first characters
389 are prefix commands. For the dispatcher to display useful information, you
390 should provide a description for the prefix, like
392 (setq org-agenda-custom-commands
393 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
394 (\"hl\" tags \"+HOME+Lisa\")
395 (\"hp\" tags \"+HOME+Peter\")
396 (\"hk\" tags \"+HOME+Kim\")))"
397 :group 'org-agenda-custom-commands
398 :type `(repeat
399 (choice :value ("x" "Describe command here" tags "" nil)
400 (list :tag "Single command"
401 (string :tag "Access Key(s) ")
402 (option (string :tag "Description"))
403 (choice
404 (const :tag "Agenda" agenda)
405 (const :tag "TODO list" alltodo)
406 (const :tag "Search words" search)
407 (const :tag "Stuck projects" stuck)
408 (const :tag "Tags/Property match (all agenda files)" tags)
409 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
410 (const :tag "TODO keyword search (all agenda files)" todo)
411 (const :tag "Tags sparse tree (current buffer)" tags-tree)
412 (const :tag "TODO keyword tree (current buffer)" todo-tree)
413 (const :tag "Occur tree (current buffer)" occur-tree)
414 (sexp :tag "Other, user-defined function"))
415 (string :tag "Match (only for some commands)")
416 ,org-agenda-custom-commands-local-options
417 (option (repeat :tag "Export" (file :tag "Export to"))))
418 (list :tag "Command series, all agenda files"
419 (string :tag "Access Key(s)")
420 (string :tag "Description ")
421 (repeat :tag "Component"
422 (choice
423 (list :tag "Agenda"
424 (const :format "" agenda)
425 (const :tag "" :format "" "")
426 ,org-agenda-custom-commands-local-options)
427 (list :tag "TODO list (all keywords)"
428 (const :format "" alltodo)
429 (const :tag "" :format "" "")
430 ,org-agenda-custom-commands-local-options)
431 (list :tag "Search words"
432 (const :format "" search)
433 (string :tag "Match")
434 ,org-agenda-custom-commands-local-options)
435 (list :tag "Stuck projects"
436 (const :format "" stuck)
437 (const :tag "" :format "" "")
438 ,org-agenda-custom-commands-local-options)
439 (list :tag "Tags search"
440 (const :format "" tags)
441 (string :tag "Match")
442 ,org-agenda-custom-commands-local-options)
443 (list :tag "Tags search, TODO entries only"
444 (const :format "" tags-todo)
445 (string :tag "Match")
446 ,org-agenda-custom-commands-local-options)
447 (list :tag "TODO keyword search"
448 (const :format "" todo)
449 (string :tag "Match")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "Other, user-defined function"
452 (symbol :tag "function")
453 (string :tag "Match")
454 ,org-agenda-custom-commands-local-options)))
456 (repeat :tag "Settings for entire command set"
457 (list (variable :tag "Any variable")
458 (sexp :tag "Value")))
459 (option (repeat :tag "Export" (file :tag "Export to"))))
460 (cons :tag "Prefix key documentation"
461 (string :tag "Access Key(s)")
462 (string :tag "Description ")))))
464 (defcustom org-agenda-query-register ?o
465 "The register holding the current query string.
466 The purpose of this is that if you construct a query string interactively,
467 you can then use it to define a custom command."
468 :group 'org-agenda-custom-commands
469 :type 'character)
471 (defcustom org-stuck-projects
472 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
473 "How to identify stuck projects.
474 This is a list of four items:
475 1. A tags/todo/property matcher string that is used to identify a project.
476 See the manual for a description of tag and property searches.
477 The entire tree below a headline matched by this is considered one project.
478 2. A list of TODO keywords identifying non-stuck projects.
479 If the project subtree contains any headline with one of these todo
480 keywords, the project is considered to be not stuck. If you specify
481 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
482 3. A list of tags identifying non-stuck projects.
483 If the project subtree contains any headline with one of these tags,
484 the project is considered to be not stuck. If you specify \"*\" as
485 a tag, any tag will mark the project unstuck. Note that this is about
486 the explicit presence of a tag somewhere in the subtree, inherited
487 tags to not count here. If inherited tags make a project not stuck,
488 use \"-TAG\" in the tags part of the matcher under (1.) above.
489 4. An arbitrary regular expression matching non-stuck projects.
491 If the project turns out to be not stuck, search continues also in the
492 subtree to see if any of the subtasks have project status.
494 See also the variable `org-tags-match-list-sublevels' which applies
495 to projects matched by this search as well.
497 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
498 or `C-c a #' to produce the list."
499 :group 'org-agenda-custom-commands
500 :type '(list
501 (string :tag "Tags/TODO match to identify a project")
502 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
503 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
504 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
506 (defcustom org-agenda-filter-effort-default-operator "<"
507 "The default operator for effort estimate filtering.
508 If you select an effort estimate limit without first pressing an operator,
509 this one will be used."
510 :group 'org-agenda-custom-commands
511 :type '(choice (const :tag "less or equal" "<")
512 (const :tag "greater or equal"">")
513 (const :tag "equal" "=")))
515 (defgroup org-agenda-skip nil
516 "Options concerning skipping parts of agenda files."
517 :tag "Org Agenda Skip"
518 :group 'org-agenda)
520 (defcustom org-agenda-skip-function-global nil
521 "Function to be called at each match during agenda construction.
522 If this function returns nil, the current match should not be skipped.
523 If the function decided to skip an agenda match, is must return the
524 buffer position from which the search should be continued.
525 This may also be a Lisp form, which will be evaluated.
527 This variable will be applied to every agenda match, including
528 tags/property searches and TODO lists. So try to make the test function
529 do its checking as efficiently as possible. To implement a skipping
530 condition just for specific agenda commands, use the variable
531 `org-agenda-skip-function' which can be set in the options section
532 of custom agenda commands."
533 :group 'org-agenda-skip
534 :type 'sexp)
536 (defgroup org-agenda-daily/weekly nil
537 "Options concerning the daily/weekly agenda."
538 :tag "Org Agenda Daily/Weekly"
539 :group 'org-agenda)
540 (defgroup org-agenda-todo-list nil
541 "Options concerning the global todo list agenda view."
542 :tag "Org Agenda Todo List"
543 :group 'org-agenda)
544 (defgroup org-agenda-match-view nil
545 "Options concerning the general tags/property/todo match agenda view."
546 :tag "Org Agenda Match View"
547 :group 'org-agenda)
548 (defgroup org-agenda-search-view nil
549 "Options concerning the general tags/property/todo match agenda view."
550 :tag "Org Agenda Match View"
551 :group 'org-agenda)
553 (defvar org-agenda-archives-mode nil
554 "Non-nil means the agenda will include archived items.
555 If this is the symbol `trees', trees in the selected agenda scope
556 that are marked with the ARCHIVE tag will be included anyway. When this is
557 t, also all archive files associated with the current selection of agenda
558 files will be included.")
560 (defcustom org-agenda-skip-comment-trees t
561 "Non-nil means skip trees that start with the COMMENT keyword.
562 When nil, these trees are also scanned by agenda commands."
563 :group 'org-agenda-skip
564 :type 'boolean)
566 (defcustom org-agenda-todo-list-sublevels t
567 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
568 When nil, the sublevels of a TODO entry are not checked, resulting in
569 potentially much shorter TODO lists."
570 :group 'org-agenda-skip
571 :group 'org-agenda-todo-list
572 :type 'boolean)
574 (defcustom org-agenda-todo-ignore-with-date nil
575 "Non-nil means don't show entries with a date in the global todo list.
576 You can use this if you prefer to mark mere appointments with a TODO keyword,
577 but don't want them to show up in the TODO list.
578 When this is set, it also covers deadlines and scheduled items, the settings
579 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
580 will be ignored.
581 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
582 :group 'org-agenda-skip
583 :group 'org-agenda-todo-list
584 :type 'boolean)
586 (defcustom org-agenda-todo-ignore-timestamp nil
587 "Non-nil means don't show entries with a timestamp.
588 This applies when creating the global todo list.
589 Valid values are:
591 past Don't show entries for today or in the past.
593 future Don't show entries with a timestamp in the future.
594 The idea behind this is that if it has a future
595 timestamp, you don't want to think about it until the
596 date.
598 all Don't show any entries with a timestamp in the global todo list.
599 The idea behind this is that by setting a timestamp, you
600 have already \"taken care\" of this item.
602 This variable can also have an integer as a value. If positive (N),
603 todos with a timestamp N or more days in the future will be ignored. If
604 negative (-N), todos with a timestamp N or more days in the past will be
605 ignored. If 0, todos with a timestamp either today or in the future will
606 be ignored. For example, a value of -1 will exclude todos with a
607 timestamp in the past (yesterday or earlier), while a value of 7 will
608 exclude todos with a timestamp a week or more in the future.
610 See also `org-agenda-todo-ignore-with-date'.
611 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
612 to make his option also apply to the tags-todo list."
613 :group 'org-agenda-skip
614 :group 'org-agenda-todo-list
615 :type '(choice
616 (const :tag "Ignore future timestamp todos" future)
617 (const :tag "Ignore past or present timestamp todos" past)
618 (const :tag "Ignore all timestamp todos" all)
619 (const :tag "Show timestamp todos" nil)
620 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
622 (defcustom org-agenda-todo-ignore-scheduled nil
623 "Non-nil means, ignore some scheduled TODO items when making TODO list.
624 This applies when creating the global todo list.
625 Valid values are:
627 past Don't show entries scheduled today or in the past.
629 future Don't show entries scheduled in the future.
630 The idea behind this is that by scheduling it, you don't want to
631 think about it until the scheduled date.
633 all Don't show any scheduled entries in the global todo list.
634 The idea behind this is that by scheduling it, you have already
635 \"taken care\" of this item.
637 t Same as `all', for backward compatibility.
639 This variable can also have an integer as a value. See
640 `org-agenda-todo-ignore-timestamp' for more details.
642 See also `org-agenda-todo-ignore-with-date'.
643 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
644 to make his option also apply to the tags-todo list."
645 :group 'org-agenda-skip
646 :group 'org-agenda-todo-list
647 :type '(choice
648 (const :tag "Ignore future-scheduled todos" future)
649 (const :tag "Ignore past- or present-scheduled todos" past)
650 (const :tag "Ignore all scheduled todos" all)
651 (const :tag "Ignore all scheduled todos (compatibility)" t)
652 (const :tag "Show scheduled todos" nil)
653 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
655 (defcustom org-agenda-todo-ignore-deadlines nil
656 "Non-nil means ignore some deadlined TODO items when making TODO list.
657 There are different motivations for using different values, please think
658 carefully when configuring this variable.
660 This applies when creating the global todo list.
661 Valid values are:
663 near Don't show near deadline entries. A deadline is near when it is
664 closer than `org-deadline-warning-days' days. The idea behind this
665 is that such items will appear in the agenda anyway.
667 far Don't show TODO entries where a deadline has been defined, but
668 the deadline is not near. This is useful if you don't want to
669 use the todo list to figure out what to do now.
671 past Don't show entries with a deadline timestamp for today or in the past.
673 future Don't show entries with a deadline timestamp in the future, not even
674 when they become `near' ones. Use it with caution.
676 all Ignore all TODO entries that do have a deadline.
678 t Same as `near', for backward compatibility.
680 This variable can also have an integer as a value. See
681 `org-agenda-todo-ignore-timestamp' for more details.
683 See also `org-agenda-todo-ignore-with-date'.
684 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
685 to make his option also apply to the tags-todo list."
686 :group 'org-agenda-skip
687 :group 'org-agenda-todo-list
688 :type '(choice
689 (const :tag "Ignore near deadlines" near)
690 (const :tag "Ignore near deadlines (compatibility)" t)
691 (const :tag "Ignore far deadlines" far)
692 (const :tag "Ignore all TODOs with a deadlines" all)
693 (const :tag "Show all TODOs, even if they have a deadline" nil)
694 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
696 (defcustom org-agenda-tags-todo-honor-ignore-options nil
697 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
698 The variables
699 `org-agenda-todo-ignore-with-date',
700 `org-agenda-todo-ignore-timestamp',
701 `org-agenda-todo-ignore-scheduled',
702 `org-agenda-todo-ignore-deadlines'
703 make the global TODO list skip entries that have time stamps of certain
704 kinds. If this option is set, the same options will also apply for the
705 tags-todo search, which is the general tags/property matcher
706 restricted to unfinished TODO entries only."
707 :group 'org-agenda-skip
708 :group 'org-agenda-todo-list
709 :group 'org-agenda-match-view
710 :type 'boolean)
712 (defcustom org-agenda-skip-scheduled-if-done nil
713 "Non-nil means don't show scheduled items in agenda when they are done.
714 This is relevant for the daily/weekly agenda, not for the TODO list. And
715 it applies only to the actual date of the scheduling. Warnings about
716 an item with a past scheduling dates are always turned off when the item
717 is DONE."
718 :group 'org-agenda-skip
719 :group 'org-agenda-daily/weekly
720 :type 'boolean)
722 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
723 "Non-nil means skip scheduling line if same entry shows because of deadline.
724 In the agenda of today, an entry can show up multiple times because
725 it is both scheduled and has a nearby deadline, and maybe a plain time
726 stamp as well.
727 When this variable is t, then only the deadline is shown and the fact that
728 the entry is scheduled today or was scheduled previously is not shown.
729 When this variable is nil, the entry will be shown several times. When
730 the variable is the symbol `not-today', then skip scheduled previously,
731 but not scheduled today."
732 :group 'org-agenda-skip
733 :group 'org-agenda-daily/weekly
734 :type '(choice
735 (const :tag "Never" nil)
736 (const :tag "Always" t)
737 (const :tag "Not when scheduled today" not-today)))
739 (defcustom org-agenda-skip-deadline-if-done nil
740 "Non-nil means don't show deadlines when the corresponding item is done.
741 When nil, the deadline is still shown and should give you a happy feeling.
742 This is relevant for the daily/weekly agenda. And it applied only to the
743 actually date of the deadline. Warnings about approaching and past-due
744 deadlines are always turned off when the item is DONE."
745 :group 'org-agenda-skip
746 :group 'org-agenda-daily/weekly
747 :type 'boolean)
749 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
750 "Non-nil means skip deadline prewarning when entry is also scheduled.
751 This will apply on all days where a prewarning for the deadline would
752 be shown, but not at the day when the entry is actually due. On that day,
753 the deadline will be shown anyway.
754 This variable may be set to nil, t, or a number which will then give
755 the number of days before the actual deadline when the prewarnings
756 should resume.
757 This can be used in a workflow where the first showing of the deadline will
758 trigger you to schedule it, and then you don't want to be reminded of it
759 because you will take care of it on the day when scheduled."
760 :group 'org-agenda-skip
761 :group 'org-agenda-daily/weekly
762 :type '(choice
763 (const :tag "Alwas show prewarning" nil)
764 (const :tag "Remove prewarning if entry is scheduled" t)
765 (integer :tag "Restart prewarning N days before deadline")))
767 (defcustom org-agenda-skip-additional-timestamps-same-entry t
768 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
769 When non-nil, after the search for timestamps has matched once in an
770 entry, the rest of the entry will not be searched."
771 :group 'org-agenda-skip
772 :type 'boolean)
774 (defcustom org-agenda-skip-timestamp-if-done nil
775 "Non-nil means don't select item by timestamp or -range if it is DONE."
776 :group 'org-agenda-skip
777 :group 'org-agenda-daily/weekly
778 :type 'boolean)
780 (defcustom org-agenda-dim-blocked-tasks t
781 "Non-nil means dim blocked tasks in the agenda display.
782 This causes some overhead during agenda construction, but if you
783 have turned on `org-enforce-todo-dependencies',
784 `org-enforce-todo-checkbox-dependencies', or any other blocking
785 mechanism, this will create useful feedback in the agenda.
787 Instead of t, this variable can also have the value `invisible'.
788 Then blocked tasks will be invisible and only become visible when
789 they become unblocked. An exemption to this behavior is when a task is
790 blocked because of unchecked checkboxes below it. Since checkboxes do
791 not show up in the agenda views, making this task invisible you remove any
792 trace from agenda views that there is something to do. Therefore, a task
793 that is blocked because of checkboxes will never be made invisible, it
794 will only be dimmed."
795 :group 'org-agenda-daily/weekly
796 :group 'org-agenda-todo-list
797 :type '(choice
798 (const :tag "Do not dim" nil)
799 (const :tag "Dim to a grey face" t)
800 (const :tag "Make invisible" invisible)))
802 (defcustom org-timeline-show-empty-dates 3
803 "Non-nil means `org-timeline' also shows dates without an entry.
804 When nil, only the days which actually have entries are shown.
805 When t, all days between the first and the last date are shown.
806 When an integer, show also empty dates, but if there is a gap of more than
807 N days, just insert a special line indicating the size of the gap."
808 :group 'org-agenda-skip
809 :type '(choice
810 (const :tag "None" nil)
811 (const :tag "All" t)
812 (integer :tag "at most")))
814 (defgroup org-agenda-startup nil
815 "Options concerning initial settings in the Agenda in Org Mode."
816 :tag "Org Agenda Startup"
817 :group 'org-agenda)
819 (defcustom org-agenda-menu-show-matcher t
820 "Non-nil means show the match string in the agenda dispatcher menu.
821 When nil, the matcher string is not shown, but is put into the help-echo
822 property so than moving the mouse over the command shows it.
823 Setting it to nil is good if matcher strings are very long and/or if
824 you want to use two-column display (see `org-agenda-menu-two-column')."
825 :group 'org-agenda
826 :type 'boolean)
828 (defcustom org-agenda-menu-two-column nil
829 "Non-nil means, use two columns to show custom commands in the dispatcher.
830 If you use this, you probably want to set `org-agenda-menu-show-matcher'
831 to nil."
832 :group 'org-agenda
833 :type 'boolean)
835 (defcustom org-finalize-agenda-hook nil
836 "Hook run just before displaying an agenda buffer."
837 :group 'org-agenda-startup
838 :type 'hook)
840 (defcustom org-agenda-mouse-1-follows-link nil
841 "Non-nil means mouse-1 on a link will follow the link in the agenda.
842 A longer mouse click will still set point. Does not work on XEmacs.
843 Needs to be set before org.el is loaded."
844 :group 'org-agenda-startup
845 :type 'boolean)
847 (defcustom org-agenda-start-with-follow-mode nil
848 "The initial value of follow mode in a newly created agenda window."
849 :group 'org-agenda-startup
850 :type 'boolean)
852 (defcustom org-agenda-show-outline-path t
853 "Non-nil means show outline path in echo area after line motion."
854 :group 'org-agenda-startup
855 :type 'boolean)
857 (defcustom org-agenda-start-with-entry-text-mode nil
858 "The initial value of entry-text-mode in a newly created agenda window."
859 :group 'org-agenda-startup
860 :type 'boolean)
862 (defcustom org-agenda-entry-text-maxlines 5
863 "Number of text lines to be added when `E' is pressed in the agenda.
865 Note that this variable only used during agenda display. Add add entry text
866 when exporting the agenda, configure the variable
867 `org-agenda-add-entry-ext-maxlines'."
868 :group 'org-agenda
869 :type 'integer)
871 (defcustom org-agenda-entry-text-exclude-regexps nil
872 "List of regular expressions to clean up entry text.
873 The complete matches of all regular expressions in this list will be
874 removed from entry text before it is shown in the agenda."
875 :group 'org-agenda
876 :type '(repeat (regexp)))
878 (defvar org-agenda-entry-text-cleanup-hook nil
879 "Hook that is run after basic cleanup of entry text to be shown in agenda.
880 This cleanup is done in a temporary buffer, so the function may inspect and
881 change the entire buffer.
882 Some default stuff like drawers and scheduling/deadline dates will already
883 have been removed when this is called, as will any matches for regular
884 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
886 (defvar org-agenda-include-inactive-timestamps nil
887 "Non-nil means include inactive time stamps in agenda and timeline.")
889 (defgroup org-agenda-windows nil
890 "Options concerning the windows used by the Agenda in Org Mode."
891 :tag "Org Agenda Windows"
892 :group 'org-agenda)
894 (defcustom org-agenda-window-setup 'reorganize-frame
895 "How the agenda buffer should be displayed.
896 Possible values for this option are:
898 current-window Show agenda in the current window, keeping all other windows.
899 other-window Use `switch-to-buffer-other-window' to display agenda.
900 reorganize-frame Show only two windows on the current frame, the current
901 window and the agenda.
902 other-frame Use `switch-to-buffer-other-frame' to display agenda.
903 Also, when exiting the agenda, kill that frame.
904 See also the variable `org-agenda-restore-windows-after-quit'."
905 :group 'org-agenda-windows
906 :type '(choice
907 (const current-window)
908 (const other-frame)
909 (const other-window)
910 (const reorganize-frame)))
912 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
913 "The min and max height of the agenda window as a fraction of frame height.
914 The value of the variable is a cons cell with two numbers between 0 and 1.
915 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
916 :group 'org-agenda-windows
917 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
919 (defcustom org-agenda-restore-windows-after-quit nil
920 "Non-nil means restore window configuration upon exiting agenda.
921 Before the window configuration is changed for displaying the agenda,
922 the current status is recorded. When the agenda is exited with
923 `q' or `x' and this option is set, the old state is restored. If
924 `org-agenda-window-setup' is `other-frame', the value of this
925 option will be ignored."
926 :group 'org-agenda-windows
927 :type 'boolean)
929 (defcustom org-agenda-ndays nil
930 "Number of days to include in overview display.
931 Should be 1 or 7.
932 Obsolete, see `org-agenda-span'."
933 :group 'org-agenda-daily/weekly
934 :type 'integer)
936 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
938 (defcustom org-agenda-span 'week
939 "Number of days to include in overview display.
940 Can be day, week, month, year, or any number of days.
941 Custom commands can set this variable in the options section."
942 :group 'org-agenda-daily/weekly
943 :type '(choice (const :tag "Day" day)
944 (const :tag "Week" week)
945 (const :tag "Month" month)
946 (const :tag "Year" year)
947 (integer :tag "Custom")))
949 (defcustom org-agenda-start-on-weekday 1
950 "Non-nil means start the overview always on the specified weekday.
951 0 denotes Sunday, 1 denotes Monday etc.
952 When nil, always start on the current day.
953 Custom commands can set this variable in the options section."
954 :group 'org-agenda-daily/weekly
955 :type '(choice (const :tag "Today" nil)
956 (integer :tag "Weekday No.")))
958 (defcustom org-agenda-show-all-dates t
959 "Non-nil means `org-agenda' shows every day in the selected range.
960 When nil, only the days which actually have entries are shown."
961 :group 'org-agenda-daily/weekly
962 :type 'boolean)
964 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
965 "Format string for displaying dates in the agenda.
966 Used by the daily/weekly agenda and by the timeline. This should be
967 a format string understood by `format-time-string', or a function returning
968 the formatted date as a string. The function must take a single argument,
969 a calendar-style date list like (month day year)."
970 :group 'org-agenda-daily/weekly
971 :type '(choice
972 (string :tag "Format string")
973 (function :tag "Function")))
975 (defun org-agenda-format-date-aligned (date)
976 "Format a date string for display in the daily/weekly agenda, or timeline.
977 This function makes sure that dates are aligned for easy reading."
978 (require 'cal-iso)
979 (let* ((dayname (calendar-day-name date))
980 (day (cadr date))
981 (day-of-week (calendar-day-of-week date))
982 (month (car date))
983 (monthname (calendar-month-name month))
984 (year (nth 2 date))
985 (iso-week (org-days-to-iso-week
986 (calendar-absolute-from-gregorian date)))
987 (weekyear (cond ((and (= month 1) (>= iso-week 52))
988 (1- year))
989 ((and (= month 12) (<= iso-week 1))
990 (1+ year))
991 (t year)))
992 (weekstring (if (= day-of-week 1)
993 (format " W%02d" iso-week)
994 "")))
995 (format "%-10s %2d %s %4d%s"
996 dayname day monthname year weekstring)))
998 (defcustom org-agenda-time-leading-zero nil
999 "Non-nil means use leading zero for military times in agenda.
1000 For example, 9:30am would become 09:30 rather than 9:30."
1001 :group 'org-agenda-daily/weekly
1002 :type 'boolean)
1004 (defcustom org-agenda-timegrid-use-ampm nil
1005 "When set, show AM/PM style timestamps on the timegrid."
1006 :group 'org-agenda
1007 :type 'boolean)
1009 (defun org-agenda-time-of-day-to-ampm (time)
1010 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1011 (let* ((hour-number (string-to-number (substring time 0 -3)))
1012 (minute (substring time -2))
1013 (ampm "am"))
1014 (cond
1015 ((equal hour-number 12)
1016 (setq ampm "pm"))
1017 ((> hour-number 12)
1018 (setq ampm "pm")
1019 (setq hour-number (- hour-number 12))))
1020 (concat
1021 (if org-agenda-time-leading-zero
1022 (format "%02d" hour-number)
1023 (format "%02s" (number-to-string hour-number)))
1024 ":" minute ampm)))
1026 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1027 "Conditionally convert TIME to AM/PM format
1028 based on `org-agenda-timegrid-use-ampm'"
1029 (if org-agenda-timegrid-use-ampm
1030 (org-agenda-time-of-day-to-ampm time)
1031 time))
1033 (defcustom org-agenda-weekend-days '(6 0)
1034 "Which days are weekend?
1035 These days get the special face `org-agenda-date-weekend' in the agenda
1036 and timeline buffers."
1037 :group 'org-agenda-daily/weekly
1038 :type '(set :greedy t
1039 (const :tag "Monday" 1)
1040 (const :tag "Tuesday" 2)
1041 (const :tag "Wednesday" 3)
1042 (const :tag "Thursday" 4)
1043 (const :tag "Friday" 5)
1044 (const :tag "Saturday" 6)
1045 (const :tag "Sunday" 0)))
1047 (defcustom org-agenda-include-diary nil
1048 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1049 Custom commands can set this variable in the options section."
1050 :group 'org-agenda-daily/weekly
1051 :type 'boolean)
1053 (defcustom org-agenda-include-deadlines t
1054 "If non-nil, include entries within their deadline warning period.
1055 Custom commands can set this variable in the options section."
1056 :group 'org-agenda-daily/weekly
1057 :type 'boolean)
1059 (defcustom org-agenda-include-all-todo nil
1060 "Set means weekly/daily agenda will always contain all TODO entries.
1061 The TODO entries will be listed at the top of the agenda, before
1062 the entries for specific days.
1063 This option is deprecated, it is better to define a block agenda instead."
1064 :group 'org-agenda-daily/weekly
1065 :type 'boolean)
1067 (defcustom org-agenda-repeating-timestamp-show-all t
1068 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1069 When set to a list of strings, only show occurrences of repeating
1070 stamps for these TODO keywords. When nil, only one occurrence is
1071 shown, either today or the nearest into the future."
1072 :group 'org-agenda-daily/weekly
1073 :type '(choice
1074 (const :tag "Show repeating stamps" t)
1075 (repeat :tag "Show repeating stamps for these TODO keywords"
1076 (string :tag "TODO Keyword"))
1077 (const :tag "Don't show repeating stamps" nil)))
1079 (defcustom org-scheduled-past-days 10000
1080 "No. of days to continue listing scheduled items that are not marked DONE.
1081 When an item is scheduled on a date, it shows up in the agenda on this
1082 day and will be listed until it is marked done for the number of days
1083 given here."
1084 :group 'org-agenda-daily/weekly
1085 :type 'integer)
1087 (defcustom org-agenda-log-mode-items '(closed clock)
1088 "List of items that should be shown in agenda log mode.
1089 This list may contain the following symbols:
1091 closed Show entries that have been closed on that day.
1092 clock Show entries that have received clocked time on that day.
1093 state Show all logged state changes.
1094 Note that instead of changing this variable, you can also press `C-u l' in
1095 the agenda to display all available LOG items temporarily."
1096 :group 'org-agenda-daily/weekly
1097 :type '(set :greedy t (const closed) (const clock) (const state)))
1099 (defcustom org-agenda-clock-consistency-checks
1100 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1101 :gap-ok-around ("4:00")
1102 :default-face ((:background "DarkRed") (:foreground "white"))
1103 :overlap-face nil :gap-face nil :no-end-time-face nil
1104 :long-face nil :short-face nil)
1105 "This is a property list, with the following keys:
1107 :max-duration Mark clocking chunks that are longer than this time.
1108 This is a time string like \"HH:MM\", or the number
1109 of minutes as an integer.
1111 :min-duration Mark clocking chunks that are shorter that this.
1112 This is a time string like \"HH:MM\", or the number
1113 of minutes as an integer.
1115 :max-gap Mark gaps between clocking chunks that are longer than
1116 this duration. A number of minutes, or a string
1117 like \"HH:MM\".
1119 :gap-ok-around List of times during the day which are usually not working
1120 times. When a gap is detected, but the gap contains any
1121 of these times, the gap is *not* reported. For example,
1122 if this is (\"4:00\" \"13:00\") then gaps that contain
1123 4:00 in the morning (i.e. the night) and 13:00
1124 (i.e. a typical lunch time) do not cause a warning.
1125 You should have at least one time during the night in this
1126 list, or otherwise the first task each morning will trigger
1127 a warning because it follows a long gap.
1129 Furthermore, the following properties can be used to define faces for
1130 issue display.
1132 :default-face the default face, if the specific face is undefined
1133 :overlap-face face for overlapping clocks
1134 :gap-face face for gaps between clocks
1135 :no-end-time-face face for incomplete clocks
1136 :long-face face for clock intervals that are too long
1137 :short-face face for clock intervals that are too short"
1138 :group 'org-agenda-daily/weekly
1139 :group 'org-clock
1140 :type 'plist)
1142 (defcustom org-agenda-log-mode-add-notes t
1143 "Non-nil means add first line of notes to log entries in agenda views.
1144 If a log item like a state change or a clock entry is associated with
1145 notes, the first line of these notes will be added to the entry in the
1146 agenda display."
1147 :group 'org-agenda-daily/weekly
1148 :type 'boolean)
1150 (defcustom org-agenda-start-with-log-mode nil
1151 "The initial value of log-mode in a newly created agenda window."
1152 :group 'org-agenda-startup
1153 :group 'org-agenda-daily/weekly
1154 :type 'boolean)
1156 (defcustom org-agenda-start-with-clockreport-mode nil
1157 "The initial value of clockreport-mode in a newly created agenda window."
1158 :group 'org-agenda-startup
1159 :group 'org-agenda-daily/weekly
1160 :type 'boolean)
1162 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1163 "Property list with parameters for the clocktable in clockreport mode.
1164 This is the display mode that shows a clock table in the daily/weekly
1165 agenda, the properties for this dynamic block can be set here.
1166 The usual clocktable parameters are allowed here, but you cannot set
1167 the properties :name, :tstart, :tend, :block, and :scope - these will
1168 be overwritten to make sure the content accurately reflects the
1169 current display in the agenda."
1170 :group 'org-agenda-daily/weekly
1171 :type 'plist)
1173 (defcustom org-agenda-search-view-always-boolean nil
1174 "Non-nil means the search string is interpreted as individual parts.
1176 The search string for search view can either be interpreted as a phrase,
1177 or as a list of snippets that define a boolean search for a number of
1178 strings.
1180 When this is non-nil, the string will be split on whitespace, and each
1181 snippet will be searched individually, and all must match in order to
1182 select an entry. A snippet is then a single string of non-white
1183 characters, or a string in double quotes, or a regexp in {} braces.
1184 If a snippet is preceded by \"-\", the snippet must *not* match.
1185 \"+\" is syntactic sugar for positive selection. Each snippet may
1186 be found as a full word or a partial word, but see the variable
1187 `org-agenda-search-view-force-full-words'.
1189 When this is nil, search will look for the entire search phrase as one,
1190 with each space character matching any amount of whitespace, including
1191 line breaks.
1193 Even when this is nil, you can still switch to Boolean search dynamically
1194 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1195 is a regexp marked with braces like \"{abc}\", this will also switch to
1196 boolean search."
1197 :group 'org-agenda-search-view
1198 :type 'boolean)
1200 (if (fboundp 'defvaralias)
1201 (defvaralias 'org-agenda-search-view-search-words-only
1202 'org-agenda-search-view-always-boolean))
1204 (defcustom org-agenda-search-view-force-full-words nil
1205 "Non-nil means, search words must be matches as complete words.
1206 When nil, they may also match part of a word."
1207 :group 'org-agenda-search-view
1208 :type 'boolean)
1210 (defgroup org-agenda-time-grid nil
1211 "Options concerning the time grid in the Org-mode Agenda."
1212 :tag "Org Agenda Time Grid"
1213 :group 'org-agenda)
1215 (defcustom org-agenda-search-headline-for-time t
1216 "Non-nil means search headline for a time-of-day.
1217 If the headline contains a time-of-day in one format or another, it will
1218 be used to sort the entry into the time sequence of items for a day.
1219 Some people have time stamps in the headline that refer to the creation
1220 time or so, and then this produces an unwanted side effect. If this is
1221 the case for your, use this variable to turn off searching the headline
1222 for a time."
1223 :group 'org-agenda-time-grid
1224 :type 'boolean)
1226 (defcustom org-agenda-use-time-grid t
1227 "Non-nil means show a time grid in the agenda schedule.
1228 A time grid is a set of lines for specific times (like every two hours between
1229 8:00 and 20:00). The items scheduled for a day at specific times are
1230 sorted in between these lines.
1231 For details about when the grid will be shown, and what it will look like, see
1232 the variable `org-agenda-time-grid'."
1233 :group 'org-agenda-time-grid
1234 :type 'boolean)
1236 (defcustom org-agenda-time-grid
1237 '((daily today require-timed)
1238 "----------------"
1239 (800 1000 1200 1400 1600 1800 2000))
1241 "The settings for time grid for agenda display.
1242 This is a list of three items. The first item is again a list. It contains
1243 symbols specifying conditions when the grid should be displayed:
1245 daily if the agenda shows a single day
1246 weekly if the agenda shows an entire week
1247 today show grid on current date, independent of daily/weekly display
1248 require-timed show grid only if at least one item has a time specification
1250 The second item is a string which will be placed behind the grid time.
1252 The third item is a list of integers, indicating the times that should have
1253 a grid line."
1254 :group 'org-agenda-time-grid
1255 :type
1256 '(list
1257 (set :greedy t :tag "Grid Display Options"
1258 (const :tag "Show grid in single day agenda display" daily)
1259 (const :tag "Show grid in weekly agenda display" weekly)
1260 (const :tag "Always show grid for today" today)
1261 (const :tag "Show grid only if any timed entries are present"
1262 require-timed)
1263 (const :tag "Skip grid times already present in an entry"
1264 remove-match))
1265 (string :tag "Grid String")
1266 (repeat :tag "Grid Times" (integer :tag "Time"))))
1268 (defcustom org-agenda-show-current-time-in-grid t
1269 "Non-nil means show the current time in the time grid."
1270 :group 'org-agenda-time-grid
1271 :type 'boolean)
1273 (defcustom org-agenda-current-time-string
1274 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1275 "The string for the current time marker in the agenda."
1276 :group 'org-agenda-time-grid
1277 :type 'string)
1279 (defgroup org-agenda-sorting nil
1280 "Options concerning sorting in the Org-mode Agenda."
1281 :tag "Org Agenda Sorting"
1282 :group 'org-agenda)
1284 (defcustom org-agenda-sorting-strategy
1285 '((agenda habit-down time-up priority-down category-keep)
1286 (todo priority-down category-keep)
1287 (tags priority-down category-keep)
1288 (search category-keep))
1289 "Sorting structure for the agenda items of a single day.
1290 This is a list of symbols which will be used in sequence to determine
1291 if an entry should be listed before another entry. The following
1292 symbols are recognized:
1294 time-up Put entries with time-of-day indications first, early first
1295 time-down Put entries with time-of-day indications first, late first
1296 category-keep Keep the default order of categories, corresponding to the
1297 sequence in `org-agenda-files'.
1298 category-up Sort alphabetically by category, A-Z.
1299 category-down Sort alphabetically by category, Z-A.
1300 tag-up Sort alphabetically by last tag, A-Z.
1301 tag-down Sort alphabetically by last tag, Z-A.
1302 priority-up Sort numerically by priority, high priority last.
1303 priority-down Sort numerically by priority, high priority first.
1304 todo-state-up Sort by todo state, tasks that are done last.
1305 todo-state-down Sort by todo state, tasks that are done first.
1306 effort-up Sort numerically by estimated effort, high effort last.
1307 effort-down Sort numerically by estimated effort, high effort first.
1308 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1309 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1310 habit-up Put entries that are habits first
1311 habit-down Put entries that are habits last
1312 alpha-up Sort headlines alphabetically
1313 alpha-down Sort headlines alphabetically, reversed
1315 The different possibilities will be tried in sequence, and testing stops
1316 if one comparison returns a \"not-equal\". For example, the default
1317 '(time-up category-keep priority-down)
1318 means: Pull out all entries having a specified time of day and sort them,
1319 in order to make a time schedule for the current day the first thing in the
1320 agenda listing for the day. Of the entries without a time indication, keep
1321 the grouped in categories, don't sort the categories, but keep them in
1322 the sequence given in `org-agenda-files'. Within each category sort by
1323 priority.
1325 Leaving out `category-keep' would mean that items will be sorted across
1326 categories by priority.
1328 Instead of a single list, this can also be a set of list for specific
1329 contents, with a context symbol in the car of the list, any of
1330 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1332 Custom commands can bind this variable in the options section."
1333 :group 'org-agenda-sorting
1334 :type `(choice
1335 (repeat :tag "General" ,org-sorting-choice)
1336 (list :tag "Individually"
1337 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1338 (repeat ,org-sorting-choice))
1339 (cons (const :tag "Strategy for TODO lists" todo)
1340 (repeat ,org-sorting-choice))
1341 (cons (const :tag "Strategy for Tags matches" tags)
1342 (repeat ,org-sorting-choice))
1343 (cons (const :tag "Strategy for search matches" search)
1344 (repeat ,org-sorting-choice)))))
1346 (defcustom org-agenda-cmp-user-defined nil
1347 "A function to define the comparison `user-defined'.
1348 This function must receive two arguments, agenda entry a and b.
1349 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1350 the user comparison, return nil.
1351 When this is defined, you can make `user-defined-up' and `user-defined-down'
1352 part of an agenda sorting strategy."
1353 :group 'org-agenda-sorting
1354 :type 'symbol)
1356 (defcustom org-sort-agenda-notime-is-late t
1357 "Non-nil means items without time are considered late.
1358 This is only relevant for sorting. When t, items which have no explicit
1359 time like 15:30 will be considered as 99:01, i.e. later than any items which
1360 do have a time. When nil, the default time is before 0:00. You can use this
1361 option to decide if the schedule for today should come before or after timeless
1362 agenda entries."
1363 :group 'org-agenda-sorting
1364 :type 'boolean)
1366 (defcustom org-sort-agenda-noeffort-is-high t
1367 "Non-nil means items without effort estimate are sorted as high effort.
1368 This also applies when filtering an agenda view with respect to the
1369 < or > effort operator. Then, tasks with no effort defined will be treated
1370 as tasks with high effort.
1371 When nil, such items are sorted as 0 minutes effort."
1372 :group 'org-agenda-sorting
1373 :type 'boolean)
1375 (defgroup org-agenda-line-format nil
1376 "Options concerning the entry prefix in the Org-mode agenda display."
1377 :tag "Org Agenda Line Format"
1378 :group 'org-agenda)
1380 (defcustom org-agenda-prefix-format
1381 '((agenda . " %i %-12:c%?-12t% s")
1382 (timeline . " % s")
1383 (todo . " %i %-12:c")
1384 (tags . " %i %-12:c")
1385 (search . " %i %-12:c"))
1386 "Format specifications for the prefix of items in the agenda views.
1387 An alist with four entries, for the different agenda types. The keys to the
1388 sublists are `agenda', `timeline', `todo', `search' and `tags'. The values
1389 are format strings.
1390 This format works similar to a printf format, with the following meaning:
1392 %c the category of the item, \"Diary\" for entries from the diary, or
1393 as given by the CATEGORY keyword or derived from the file name.
1394 %i the icon category of the item, as give in
1395 `org-agenda-category-icon-alist'.
1396 %T the *last* tag of the item. Last because inherited tags come
1397 first in the list.
1398 %t the time-of-day specification if one applies to the entry, in the
1399 format HH:MM
1400 %s Scheduling/Deadline information, a short string
1401 %(expression) Eval EXPRESSION and replace the control string
1402 by the result
1404 All specifiers work basically like the standard `%s' of printf, but may
1405 contain two additional characters: A question mark just after the `%' and
1406 a whitespace/punctuation character just before the final letter.
1408 If the first character after `%' is a question mark, the entire field
1409 will only be included if the corresponding value applies to the
1410 current entry. This is useful for fields which should have fixed
1411 width when present, but zero width when absent. For example,
1412 \"%?-12t\" will result in a 12 character time field if a time of the
1413 day is specified, but will completely disappear in entries which do
1414 not contain a time.
1416 If there is punctuation or whitespace character just before the final
1417 format letter, this character will be appended to the field value if
1418 the value is not empty. For example, the format \"%-12:c\" leads to
1419 \"Diary: \" if the category is \"Diary\". If the category were be
1420 empty, no additional colon would be inserted.
1422 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1423 - Indent the line with two space characters
1424 - Give the category in a 12 chars wide field, padded with whitespace on
1425 the right (because of `-'). Append a colon if there is a category
1426 (because of `:').
1427 - If there is a time-of-day, put it into a 12 chars wide field. If no
1428 time, don't put in an empty field, just skip it (because of '?').
1429 - Finally, put the scheduling information and append a whitespace.
1431 As another example, if you don't want the time-of-day of entries in
1432 the prefix, you could use:
1434 (setq org-agenda-prefix-format \" %-11:c% s\")
1436 See also the variables `org-agenda-remove-times-when-in-prefix' and
1437 `org-agenda-remove-tags'.
1439 Custom commands can set this variable in the options section."
1440 :type '(choice
1441 (string :tag "General format")
1442 (list :greedy t :tag "View dependent"
1443 (cons (const agenda) (string :tag "Format"))
1444 (cons (const timeline) (string :tag "Format"))
1445 (cons (const todo) (string :tag "Format"))
1446 (cons (const tags) (string :tag "Format"))
1447 (cons (const search) (string :tag "Format"))))
1448 :group 'org-agenda-line-format)
1450 (defvar org-prefix-format-compiled nil
1451 "The compiled version of the most recently used prefix format.
1452 See the variable `org-agenda-prefix-format'.")
1454 (defcustom org-agenda-todo-keyword-format "%-1s"
1455 "Format for the TODO keyword in agenda lines.
1456 Set this to something like \"%-12s\" if you want all TODO keywords
1457 to occupy a fixed space in the agenda display."
1458 :group 'org-agenda-line-format
1459 :type 'string)
1461 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1462 "Text preceding timerange entries in the agenda view.
1463 This is a list with two strings. The first applies when the range
1464 is entirely on one day. The second applies if the range spans several days.
1465 The strings may have two \"%d\" format specifiers which will be filled
1466 with the sequence number of the days, and the total number of days in the
1467 range, respectively."
1468 :group 'org-agenda-line-format
1469 :type '(list
1470 (string :tag "Deadline today ")
1471 (choice :tag "Deadline relative"
1472 (string :tag "Format string")
1473 (function))))
1475 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1476 "Text preceding scheduled items in the agenda view.
1477 This is a list with two strings. The first applies when the item is
1478 scheduled on the current day. The second applies when it has been scheduled
1479 previously, it may contain a %d indicating that this is the nth time that
1480 this item is scheduled, due to automatic rescheduling of unfinished items
1481 for the following day. So this number is one larger than the number of days
1482 that passed since this item was scheduled first."
1483 :group 'org-agenda-line-format
1484 :type '(list
1485 (string :tag "Scheduled today ")
1486 (string :tag "Scheduled previously")))
1488 (defcustom org-agenda-inactive-leader "["
1489 "Text preceding item pulled into the agenda by inactive time stamps.
1490 These entries are added to the agenda when pressing \"[\"."
1491 :group 'org-agenda-line-format
1492 :type '(list
1493 (string :tag "Scheduled today ")
1494 (string :tag "Scheduled previously")))
1496 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1497 "Text preceding deadline items in the agenda view.
1498 This is a list with two strings. The first applies when the item has its
1499 deadline on the current day. The second applies when it is in the past or
1500 in the future, it may contain %d to capture how many days away the deadline
1501 is (was)."
1502 :group 'org-agenda-line-format
1503 :type '(list
1504 (string :tag "Deadline today ")
1505 (choice :tag "Deadline relative"
1506 (string :tag "Format string")
1507 (function))))
1509 (defcustom org-agenda-remove-times-when-in-prefix t
1510 "Non-nil means remove duplicate time specifications in agenda items.
1511 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1512 time-of-day specification in a headline or diary entry is extracted and
1513 placed into the prefix. If this option is non-nil, the original specification
1514 \(a timestamp or -range, or just a plain time(range) specification like
1515 11:30-4pm) will be removed for agenda display. This makes the agenda less
1516 cluttered.
1517 The option can be t or nil. It may also be the symbol `beg', indicating
1518 that the time should only be removed when it is located at the beginning of
1519 the headline/diary entry."
1520 :group 'org-agenda-line-format
1521 :type '(choice
1522 (const :tag "Always" t)
1523 (const :tag "Never" nil)
1524 (const :tag "When at beginning of entry" beg)))
1526 (defcustom org-agenda-remove-timeranges-from-blocks nil
1527 "Non-nil means remove time ranges specifications in agenda
1528 items that span on several days."
1529 :group 'org-agenda-line-format
1530 :type 'boolean)
1532 (defcustom org-agenda-default-appointment-duration nil
1533 "Default duration for appointments that only have a starting time.
1534 When nil, no duration is specified in such cases.
1535 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1536 :group 'org-agenda-line-format
1537 :type '(choice
1538 (integer :tag "Minutes")
1539 (const :tag "No default duration")))
1541 (defcustom org-agenda-show-inherited-tags t
1542 "Non-nil means show inherited tags in each agenda line."
1543 :group 'org-agenda-line-format
1544 :type 'boolean)
1546 (defcustom org-agenda-hide-tags-regexp nil
1547 "Regular expression used to filter away specific tags in agenda views.
1548 This means that these tags will be present, but not be shown in the agenda
1549 line. Secondary filtering will still work on the hidden tags.
1550 Nil means don't hide any tags."
1551 :group 'org-agenda-line-format
1552 :type '(choice
1553 (const :tag "Hide none" nil)
1554 (string :tag "Regexp ")))
1556 (defcustom org-agenda-remove-tags nil
1557 "Non-nil means remove the tags from the headline copy in the agenda.
1558 When this is the symbol `prefix', only remove tags when
1559 `org-agenda-prefix-format' contains a `%T' specifier."
1560 :group 'org-agenda-line-format
1561 :type '(choice
1562 (const :tag "Always" t)
1563 (const :tag "Never" nil)
1564 (const :tag "When prefix format contains %T" prefix)))
1566 (if (fboundp 'defvaralias)
1567 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1568 'org-agenda-remove-tags))
1570 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1571 "Shift tags in agenda items to this column.
1572 If this number is positive, it specifies the column. If it is negative,
1573 it means that the tags should be flushright to that column. For example,
1574 -80 works well for a normal 80 character screen."
1575 :group 'org-agenda-line-format
1576 :type 'integer)
1578 (if (fboundp 'defvaralias)
1579 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1581 (defcustom org-agenda-fontify-priorities 'cookies
1582 "Non-nil means highlight low and high priorities in agenda.
1583 When t, the highest priority entries are bold, lowest priority italic.
1584 However, settings in `org-priority-faces' will overrule these faces.
1585 When this variable is the symbol `cookies', only fontify the
1586 cookies, not the entire task.
1587 This may also be an association list of priority faces, whose
1588 keys are the character values of `org-highest-priority',
1589 `org-default-priority', and `org-lowest-priority' (the default values
1590 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1591 color as a string, or a list like `(:background \"Red\")'.
1592 If it is a color, the variable `org-faces-easy-properties'
1593 determines if it is a foreground or a background color."
1594 :group 'org-agenda-line-format
1595 :type '(choice
1596 (const :tag "Never" nil)
1597 (const :tag "Defaults" t)
1598 (const :tag "Cookies only" cookies)
1599 (repeat :tag "Specify"
1600 (list (character :tag "Priority" :value ?A)
1601 (choice :tag "Face "
1602 (string :tag "Color")
1603 (sexp :tag "Face"))))))
1605 (defcustom org-agenda-day-face-function nil
1606 "Function called to determine what face should be used to display a day.
1607 The only argument passed to that function is the day. It should
1608 returns a face, or nil if does not want to specify a face and let
1609 the normal rules apply."
1610 :group 'org-agenda-line-format
1611 :type 'function)
1613 (defcustom org-agenda-category-icon-alist nil
1614 "Alist of category icon to be displayed in agenda views.
1616 Each entry should have the following format:
1618 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1620 Where CATEGORY-REGEXP is a regexp matching the categories where
1621 the icon should be displayed.
1622 FILE-OR-DATA either a file path or a string containing image data.
1624 The other fields can be omited safely if not needed:
1625 TYPE indicates the image type.
1626 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1627 image data.
1628 PROPS are additional image attributes to assign to the image,
1629 like, e.g. `:ascent center'.
1631 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1633 If you want to set the display properties yourself, just put a
1634 list as second element:
1636 (CATEGORY-REGEXP (MY PROPERTY LIST))
1638 For example, to display a 16px horizontal space for Emacs
1639 category, you can use:
1641 (\"Emacs\" '(space . (:width (16))))"
1642 :group 'org-agenda-line-format
1643 :type '(alist :key-type (string :tag "Regexp matching category")
1644 :value-type (choice (list :tag "Icon"
1645 (string :tag "File or data")
1646 (symbol :tag "Type")
1647 (boolean :tag "Data?")
1648 (repeat :tag "Extra image properties" :inline t symbol))
1649 (list :tag "Display properties" sexp))))
1651 (defgroup org-agenda-column-view nil
1652 "Options concerning column view in the agenda."
1653 :tag "Org Agenda Column View"
1654 :group 'org-agenda)
1656 (defcustom org-agenda-columns-show-summaries t
1657 "Non-nil means show summaries for columns displayed in the agenda view."
1658 :group 'org-agenda-column-view
1659 :type 'boolean)
1661 (defcustom org-agenda-columns-compute-summary-properties t
1662 "Non-nil means recompute all summary properties before column view.
1663 When column view in the agenda is listing properties that have a summary
1664 operator, it can go to all relevant buffers and recompute the summaries
1665 there. This can mean overhead for the agenda column view, but is necessary
1666 to have thing up to date.
1667 As a special case, a CLOCKSUM property also makes sure that the clock
1668 computations are current."
1669 :group 'org-agenda-column-view
1670 :type 'boolean)
1672 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1673 "Non-nil means the duration of an appointment will add to day effort.
1674 The property to which appointment durations will be added is the one given
1675 in the option `org-effort-property'. If an appointment does not have
1676 an end time, `org-agenda-default-appointment-duration' will be used. If that
1677 is not set, an appointment without end time will not contribute to the time
1678 estimate."
1679 :group 'org-agenda-column-view
1680 :type 'boolean)
1682 (defcustom org-agenda-auto-exclude-function nil
1683 "A function called with a tag to decide if it is filtered on '/ RET'.
1684 The sole argument to the function, which is called once for each
1685 possible tag, is a string giving the name of the tag. The
1686 function should return either nil if the tag should be included
1687 as normal, or \"-<TAG>\" to exclude the tag.
1688 Note that for the purpose of tag filtering, only the lower-case version of
1689 all tags will be considered, so that this function will only ever see
1690 the lower-case version of all tags."
1691 :group 'org-agenda
1692 :type 'function)
1694 (eval-when-compile
1695 (require 'cl))
1696 (require 'org)
1698 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1699 "Execute BODY with point at location given by `org-hd-marker' property.
1700 If STRING is non-nil, the text property will be fetched from position 0
1701 in that string. If STRING is nil, it will be fetched from the beginning
1702 of the current line."
1703 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1704 'org-hd-marker string)))
1705 (with-current-buffer (marker-buffer marker)
1706 (save-excursion
1707 (goto-char marker)
1708 ,@body))))
1710 (defun org-add-agenda-custom-command (entry)
1711 "Replace or add a command in `org-agenda-custom-commands'.
1712 This is mostly for hacking and trying a new command - once the command
1713 works you probably want to add it to `org-agenda-custom-commands' for good."
1714 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1715 (if ass
1716 (setcdr ass (cdr entry))
1717 (push entry org-agenda-custom-commands))))
1719 ;;; Define the Org-agenda-mode
1721 (defvar org-agenda-mode-map (make-sparse-keymap)
1722 "Keymap for `org-agenda-mode'.")
1723 (if (fboundp 'defvaralias)
1724 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1726 (defvar org-agenda-menu) ; defined later in this file.
1727 (defvar org-agenda-restrict) ; defined later in this file.
1728 (defvar org-agenda-follow-mode nil)
1729 (defvar org-agenda-entry-text-mode nil)
1730 (defvar org-agenda-clockreport-mode nil)
1731 (defvar org-agenda-show-log nil)
1732 (defvar org-agenda-redo-command nil)
1733 (defvar org-agenda-query-string nil)
1734 (defvar org-agenda-mode-hook nil
1735 "Hook for `org-agenda-mode', run after the mode is turned on.")
1736 (defvar org-agenda-type nil)
1737 (defvar org-agenda-force-single-file nil)
1738 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1740 (defun org-agenda-mode ()
1741 "Mode for time-sorted view on action items in Org-mode files.
1743 The following commands are available:
1745 \\{org-agenda-mode-map}"
1746 (interactive)
1747 (kill-all-local-variables)
1748 (setq org-agenda-undo-list nil
1749 org-agenda-pending-undo-list nil
1750 org-agenda-bulk-marked-entries nil)
1751 (setq major-mode 'org-agenda-mode)
1752 ;; Keep global-font-lock-mode from turning on font-lock-mode
1753 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1754 (setq mode-name "Org-Agenda")
1755 (use-local-map org-agenda-mode-map)
1756 (easy-menu-add org-agenda-menu)
1757 (if org-startup-truncated (setq truncate-lines t))
1758 (org-set-local 'line-move-visual nil)
1759 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1760 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1761 ;; Make sure properties are removed when copying text
1762 (when (boundp 'buffer-substring-filters)
1763 (org-set-local 'buffer-substring-filters
1764 (cons (lambda (x)
1765 (set-text-properties 0 (length x) nil x) x)
1766 buffer-substring-filters)))
1767 (unless org-agenda-keep-modes
1768 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1769 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1770 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1771 org-agenda-show-log org-agenda-start-with-log-mode))
1773 (easy-menu-change
1774 '("Agenda") "Agenda Files"
1775 (append
1776 (list
1777 (vector
1778 (if (get 'org-agenda-files 'org-restrict)
1779 "Restricted to single file"
1780 "Edit File List")
1781 '(org-edit-agenda-file-list)
1782 (not (get 'org-agenda-files 'org-restrict)))
1783 "--")
1784 (mapcar 'org-file-menu-entry (org-agenda-files))))
1785 (org-agenda-set-mode-name)
1786 (apply
1787 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1788 (list 'org-agenda-mode-hook)))
1790 (substitute-key-definition 'undo 'org-agenda-undo
1791 org-agenda-mode-map global-map)
1792 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1793 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1794 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1795 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1796 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1797 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1798 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1799 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1800 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1801 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1802 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1803 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1804 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1805 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1806 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1807 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1808 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1809 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1810 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1811 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1812 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1813 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1814 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1815 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1816 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1817 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1818 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1819 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1820 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1821 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1822 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1823 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1824 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1825 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1826 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1827 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1828 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1829 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1830 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1831 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1832 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1833 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1834 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1835 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1836 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1838 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1839 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1840 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1841 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1842 (while l (org-defkey org-agenda-mode-map
1843 (int-to-string (pop l)) 'digit-argument)))
1845 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1846 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1847 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1848 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1849 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1850 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1851 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1852 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1853 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1854 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1855 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1856 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1857 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1858 'org-clock-modify-effort-estimate)
1859 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1860 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1861 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1862 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1863 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1864 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1865 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1866 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1867 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1868 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1869 (substitute-key-definition 'next-line 'org-agenda-next-line
1870 org-agenda-mode-map global-map)
1871 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1872 org-agenda-mode-map global-map)
1873 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1874 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1875 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1876 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1877 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1878 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1879 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1880 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1881 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1882 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1883 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1884 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1885 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1886 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1887 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1888 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1889 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1890 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1891 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1892 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1893 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1894 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1895 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1896 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1897 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1898 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1899 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1900 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1901 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1902 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1904 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1905 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1906 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1907 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1908 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1909 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1910 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1911 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1912 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1913 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1915 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1916 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1917 (when org-agenda-mouse-1-follows-link
1918 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1919 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1920 '("Agenda"
1921 ("Agenda Files")
1922 "--"
1923 ("Agenda Dates"
1924 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1925 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1926 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1927 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1928 "--"
1929 ("View"
1930 ["Day View" org-agenda-day-view
1931 :active (org-agenda-check-type nil 'agenda)
1932 :style radio :selected (eq org-agenda-current-span 'day)
1933 :keys "v d (or just d)"]
1934 ["Week View" org-agenda-week-view
1935 :active (org-agenda-check-type nil 'agenda)
1936 :style radio :selected (eq org-agenda-current-span 'week)
1937 :keys "v w (or just w)"]
1938 ["Month View" org-agenda-month-view
1939 :active (org-agenda-check-type nil 'agenda)
1940 :style radio :selected (eq org-agenda-current-span 'month)
1941 :keys "v m"]
1942 ["Year View" org-agenda-year-view
1943 :active (org-agenda-check-type nil 'agenda)
1944 :style radio :selected (eq org-agenda-current-span 'year)
1945 :keys "v y"]
1946 "--"
1947 ["Include Diary" org-agenda-toggle-diary
1948 :style toggle :selected org-agenda-include-diary
1949 :active (org-agenda-check-type nil 'agenda)]
1950 ["Include Deadlines" org-agenda-toggle-deadlines
1951 :style toggle :selected org-agenda-include-deadlines
1952 :active (org-agenda-check-type nil 'agenda)]
1953 ["Use Time Grid" org-agenda-toggle-time-grid
1954 :style toggle :selected org-agenda-use-time-grid
1955 :active (org-agenda-check-type nil 'agenda)]
1956 "--"
1957 ["Show clock report" org-agenda-clockreport-mode
1958 :style toggle :selected org-agenda-clockreport-mode
1959 :active (org-agenda-check-type nil 'agenda)]
1960 ["Show some entry text" org-agenda-entry-text-mode
1961 :style toggle :selected org-agenda-entry-text-mode
1962 :active t]
1963 "--"
1964 ["Show Logbook entries" org-agenda-log-mode
1965 :style toggle :selected org-agenda-show-log
1966 :active (org-agenda-check-type nil 'agenda 'timeline)
1967 :keys "v l (or just l)"]
1968 ["Include archived trees" org-agenda-archives-mode
1969 :style toggle :selected org-agenda-archives-mode :active t
1970 :keys "v a"]
1971 ["Include archive files" (org-agenda-archives-mode t)
1972 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1973 :keys "v A"]
1974 "--"
1975 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1976 ["Write view to file" org-write-agenda t]
1977 ["Rebuild buffer" org-agenda-redo t]
1978 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1979 "--"
1980 ["Show original entry" org-agenda-show t]
1981 ["Go To (other window)" org-agenda-goto t]
1982 ["Go To (this window)" org-agenda-switch-to t]
1983 ["Follow Mode" org-agenda-follow-mode
1984 :style toggle :selected org-agenda-follow-mode :active t]
1985 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1986 "--"
1987 ("TODO"
1988 ["Cycle TODO" org-agenda-todo t]
1989 ["Next TODO set" org-agenda-todo-nextset t]
1990 ["Previous TODO set" org-agenda-todo-previousset t]
1991 ["Add note" org-agenda-add-note t])
1992 ("Archive/Refile/Delete"
1993 ["Archive default" org-agenda-archive-default t]
1994 ["Archive default" org-agenda-archive-default-with-confirmation t]
1995 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1996 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1997 ["Archive subtree" org-agenda-archive t]
1998 "--"
1999 ["Refile" org-agenda-refile t]
2000 "--"
2001 ["Delete subtree" org-agenda-kill t])
2002 ("Bulk action"
2003 ["Mark entry" org-agenda-bulk-mark t]
2004 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2005 ["Unmark entry" org-agenda-bulk-unmark t]
2006 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2007 ["Act on all marked" org-agenda-bulk-action t]
2008 "--"
2009 ("Tags and Properties"
2010 ["Show all Tags" org-agenda-show-tags t]
2011 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2012 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2013 "--"
2014 ["Column View" org-columns t])
2015 ("Deadline/Schedule"
2016 ["Schedule" org-agenda-schedule t]
2017 ["Set Deadline" org-agenda-deadline t]
2018 "--"
2019 ["Mark item" org-agenda-action :active t :keys "k m"]
2020 ["Show mark item" org-agenda-action :active t :keys "k v"]
2021 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2022 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2023 "--"
2024 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2025 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2026 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2027 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2028 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2029 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2030 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2031 ("Clock and Effort"
2032 ["Clock in" org-agenda-clock-in t]
2033 ["Clock out" org-agenda-clock-out t]
2034 ["Clock cancel" org-agenda-clock-cancel t]
2035 ["Goto running clock" org-clock-goto t]
2036 "--"
2037 ["Set Effort" org-agenda-set-effort t]
2038 ["Change clocked effort" org-clock-modify-effort-estimate
2039 (org-clock-is-active)])
2040 ("Priority"
2041 ["Set Priority" org-agenda-priority t]
2042 ["Increase Priority" org-agenda-priority-up t]
2043 ["Decrease Priority" org-agenda-priority-down t]
2044 ["Show Priority" org-agenda-show-priority t])
2045 ("Calendar/Diary"
2046 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2047 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2048 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2049 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2050 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2051 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2052 "--"
2053 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2054 "--"
2055 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2056 "--"
2057 ("MobileOrg"
2058 ["Push Files and Views" org-mobile-push t]
2059 ["Get Captured and Flagged" org-mobile-pull t]
2060 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2061 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2062 "--"
2063 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2064 "--"
2065 ["Quit" org-agenda-quit t]
2066 ["Exit and Release Buffers" org-agenda-exit t]
2069 ;;; Agenda undo
2071 (defvar org-agenda-allow-remote-undo t
2072 "Non-nil means allow remote undo from the agenda buffer.")
2073 (defvar org-agenda-undo-list nil
2074 "List of undoable operations in the agenda since last refresh.")
2075 (defvar org-agenda-undo-has-started-in nil
2076 "Buffers that have already seen `undo-start' in the current undo sequence.")
2077 (defvar org-agenda-pending-undo-list nil
2078 "In a series of undo commands, this is the list of remaining undo items.")
2080 (defun org-agenda-undo ()
2081 "Undo a remote editing step in the agenda.
2082 This undoes changes both in the agenda buffer and in the remote buffer
2083 that have been changed along."
2084 (interactive)
2085 (or org-agenda-allow-remote-undo
2086 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2087 (if (not (eq this-command last-command))
2088 (setq org-agenda-undo-has-started-in nil
2089 org-agenda-pending-undo-list org-agenda-undo-list))
2090 (if (not org-agenda-pending-undo-list)
2091 (error "No further undo information"))
2092 (let* ((entry (pop org-agenda-pending-undo-list))
2093 buf line cmd rembuf)
2094 (setq cmd (pop entry) line (pop entry))
2095 (setq rembuf (nth 2 entry))
2096 (org-with-remote-undo rembuf
2097 (while (bufferp (setq buf (pop entry)))
2098 (if (pop entry)
2099 (with-current-buffer buf
2100 (let ((last-undo-buffer buf)
2101 (inhibit-read-only t))
2102 (unless (memq buf org-agenda-undo-has-started-in)
2103 (push buf org-agenda-undo-has-started-in)
2104 (make-local-variable 'pending-undo-list)
2105 (undo-start))
2106 (while (and pending-undo-list
2107 (listp pending-undo-list)
2108 (not (car pending-undo-list)))
2109 (pop pending-undo-list))
2110 (undo-more 1))))))
2111 (org-goto-line line)
2112 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2114 (defun org-verify-change-for-undo (l1 l2)
2115 "Verify that a real change occurred between the undo lists L1 and L2."
2116 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2117 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2118 (not (eq l1 l2)))
2120 ;;; Agenda dispatch
2122 (defvar org-agenda-restrict nil)
2123 (defvar org-agenda-restrict-begin (make-marker))
2124 (defvar org-agenda-restrict-end (make-marker))
2125 (defvar org-agenda-last-dispatch-buffer nil)
2126 (defvar org-agenda-overriding-restriction nil)
2128 ;;;###autoload
2129 (defun org-agenda (&optional arg keys restriction)
2130 "Dispatch agenda commands to collect entries to the agenda buffer.
2131 Prompts for a command to execute. Any prefix arg will be passed
2132 on to the selected command. The default selections are:
2134 a Call `org-agenda-list' to display the agenda for current day or week.
2135 t Call `org-todo-list' to display the global todo list.
2136 T Call `org-todo-list' to display the global todo list, select only
2137 entries with a specific TODO keyword (the user gets a prompt).
2138 m Call `org-tags-view' to display headlines with tags matching
2139 a condition (the user is prompted for the condition).
2140 M Like `m', but select only TODO entries, no ordinary headlines.
2141 L Create a timeline for the current buffer.
2142 e Export views to associated files.
2143 s Search entries for keywords.
2144 / Multi occur across all agenda files and also files listed
2145 in `org-agenda-text-search-extra-files'.
2146 < Restrict agenda commands to buffer, subtree, or region.
2147 Press several times to get the desired effect.
2148 > Remove a previous restriction.
2149 # List \"stuck\" projects.
2150 ! Configure what \"stuck\" means.
2151 C Configure custom agenda commands.
2153 More commands can be added by configuring the variable
2154 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2155 searches can be pre-defined in this way.
2157 If the current buffer is in Org-mode and visiting a file, you can also
2158 first press `<' once to indicate that the agenda should be temporarily
2159 \(until the next use of \\[org-agenda]) restricted to the current file.
2160 Pressing `<' twice means to restrict to the current subtree or region
2161 \(if active)."
2162 (interactive "P")
2163 (catch 'exit
2164 (let* ((prefix-descriptions nil)
2165 (org-agenda-window-setup (if (equal (buffer-name)
2166 org-agenda-buffer-name)
2167 'current-window
2168 org-agenda-window-setup))
2169 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2170 (org-agenda-custom-commands
2171 ;; normalize different versions
2172 (delq nil
2173 (mapcar
2174 (lambda (x)
2175 (cond ((stringp (cdr x))
2176 (push x prefix-descriptions)
2177 nil)
2178 ((stringp (nth 1 x)) x)
2179 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2180 (t (cons (car x) (cons "" (cdr x))))))
2181 org-agenda-custom-commands)))
2182 (buf (current-buffer))
2183 (bfn (buffer-file-name (buffer-base-buffer)))
2184 entry key type match lprops ans)
2185 ;; Turn off restriction unless there is an overriding one,
2186 (unless org-agenda-overriding-restriction
2187 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2188 ;; There is a request to keep the file list in place
2189 (put 'org-agenda-files 'org-restrict nil))
2190 (setq org-agenda-restrict nil)
2191 (move-marker org-agenda-restrict-begin nil)
2192 (move-marker org-agenda-restrict-end nil))
2193 ;; Delete old local properties
2194 (put 'org-agenda-redo-command 'org-lprops nil)
2195 ;; Delete previously set last-arguments
2196 (put 'org-agenda-redo-command 'last-args nil)
2197 ;; Remember where this call originated
2198 (setq org-agenda-last-dispatch-buffer (current-buffer))
2199 (unless keys
2200 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2201 keys (car ans)
2202 restriction (cdr ans)))
2203 ;; Establish the restriction, if any
2204 (when (and (not org-agenda-overriding-restriction) restriction)
2205 (put 'org-agenda-files 'org-restrict (list bfn))
2206 (cond
2207 ((eq restriction 'region)
2208 (setq org-agenda-restrict t)
2209 (move-marker org-agenda-restrict-begin (region-beginning))
2210 (move-marker org-agenda-restrict-end (region-end)))
2211 ((eq restriction 'subtree)
2212 (save-excursion
2213 (setq org-agenda-restrict t)
2214 (org-back-to-heading t)
2215 (move-marker org-agenda-restrict-begin (point))
2216 (move-marker org-agenda-restrict-end
2217 (progn (org-end-of-subtree t)))))))
2219 ;; For example the todo list should not need it (but does...)
2220 (cond
2221 ((setq entry (assoc keys org-agenda-custom-commands))
2222 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2223 (progn
2224 (setq type (nth 2 entry) match (eval (nth 3 entry))
2225 lprops (nth 4 entry))
2226 (put 'org-agenda-redo-command 'org-lprops lprops)
2227 (cond
2228 ((eq type 'agenda)
2229 (org-let lprops '(org-agenda-list current-prefix-arg)))
2230 ((eq type 'alltodo)
2231 (org-let lprops '(org-todo-list current-prefix-arg)))
2232 ((eq type 'search)
2233 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2234 ((eq type 'stuck)
2235 (org-let lprops '(org-agenda-list-stuck-projects
2236 current-prefix-arg)))
2237 ((eq type 'tags)
2238 (org-let lprops '(org-tags-view current-prefix-arg match)))
2239 ((eq type 'tags-todo)
2240 (org-let lprops '(org-tags-view '(4) match)))
2241 ((eq type 'todo)
2242 (org-let lprops '(org-todo-list match)))
2243 ((eq type 'tags-tree)
2244 (org-check-for-org-mode)
2245 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2246 ((eq type 'todo-tree)
2247 (org-check-for-org-mode)
2248 (org-let lprops
2249 '(org-occur (concat "^" outline-regexp "[ \t]*"
2250 (regexp-quote match) "\\>"))))
2251 ((eq type 'occur-tree)
2252 (org-check-for-org-mode)
2253 (org-let lprops '(org-occur match)))
2254 ((functionp type)
2255 (org-let lprops '(funcall type match)))
2256 ((fboundp type)
2257 (org-let lprops '(funcall type match)))
2258 (t (error "Invalid custom agenda command type %s" type))))
2259 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2260 ((equal keys "C")
2261 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2262 (customize-variable 'org-agenda-custom-commands))
2263 ((equal keys "a") (call-interactively 'org-agenda-list))
2264 ((equal keys "s") (call-interactively 'org-search-view))
2265 ((equal keys "t") (call-interactively 'org-todo-list))
2266 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2267 ((equal keys "m") (call-interactively 'org-tags-view))
2268 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2269 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2270 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2271 (org-add-hook
2272 'post-command-hook
2273 (lambda ()
2274 (unless (current-message)
2275 (let* ((m (org-agenda-get-any-marker))
2276 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2277 (when note
2278 (message (concat
2279 "FLAGGING-NOTE ([?] for more info): "
2280 (org-add-props
2281 (replace-regexp-in-string
2282 "\\\\n" "//"
2283 (copy-sequence note))
2284 nil 'face 'org-warning)))))))
2285 t t))
2286 ((equal keys "L")
2287 (unless (org-mode-p)
2288 (error "This is not an Org-mode file"))
2289 (unless restriction
2290 (put 'org-agenda-files 'org-restrict (list bfn))
2291 (org-call-with-arg 'org-timeline arg)))
2292 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2293 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2294 ((equal keys "!") (customize-variable 'org-stuck-projects))
2295 (t (error "Invalid agenda key"))))))
2297 (defun org-agenda-append-agenda ()
2298 "Append another agenda view to the current one.
2299 This function allows interactive building of block agendas.
2300 Agenda views are separated by `org-agenda-block-separator'."
2301 (interactive)
2302 (unless (string= (buffer-name) org-agenda-buffer-name)
2303 (error "Can only append from within agenda buffer"))
2304 (let ((org-agenda-multi t))
2305 (org-agenda)
2306 (widen)))
2308 (defun org-agenda-normalize-custom-commands (cmds)
2309 (delq nil
2310 (mapcar
2311 (lambda (x)
2312 (cond ((stringp (cdr x)) nil)
2313 ((stringp (nth 1 x)) x)
2314 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2315 (t (cons (car x) (cons "" (cdr x))))))
2316 cmds)))
2318 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2319 "The user interface for selecting an agenda command."
2320 (catch 'exit
2321 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2322 (restrict-ok (and bfn (org-mode-p)))
2323 (region-p (org-region-active-p))
2324 (custom org-agenda-custom-commands)
2325 (selstring "")
2326 restriction second-time
2327 c entry key type match prefixes rmheader header-end custom1 desc
2328 line lines left right n n1)
2329 (save-window-excursion
2330 (delete-other-windows)
2331 (org-switch-to-buffer-other-window " *Agenda Commands*")
2332 (erase-buffer)
2333 (insert (eval-when-compile
2334 (let ((header
2336 Press key for an agenda command: < Buffer, subtree/region restriction
2337 -------------------------------- > Remove restriction
2338 a Agenda for current week or day e Export agenda views
2339 t List of all TODO entries T Entries with special TODO kwd
2340 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2341 L Timeline for current buffer # List stuck projects (!=configure)
2342 s Search for keywords C Configure custom agenda commands
2343 / Multi-occur ? Find :FLAGGED: entries
2345 (start 0))
2346 (while (string-match
2347 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2348 header start)
2349 (setq start (match-end 0))
2350 (add-text-properties (match-beginning 2) (match-end 2)
2351 '(face bold) header))
2352 header)))
2353 (setq header-end (move-marker (make-marker) (point)))
2354 (while t
2355 (setq custom1 custom)
2356 (when (eq rmheader t)
2357 (org-goto-line 1)
2358 (re-search-forward ":" nil t)
2359 (delete-region (match-end 0) (point-at-eol))
2360 (forward-char 1)
2361 (looking-at "-+")
2362 (delete-region (match-end 0) (point-at-eol))
2363 (move-marker header-end (match-end 0)))
2364 (goto-char header-end)
2365 (delete-region (point) (point-max))
2367 ;; Produce all the lines that describe custom commands and prefixes
2368 (setq lines nil)
2369 (while (setq entry (pop custom1))
2370 (setq key (car entry) desc (nth 1 entry)
2371 type (nth 2 entry)
2372 match (nth 3 entry))
2373 (if (> (length key) 1)
2374 (add-to-list 'prefixes (string-to-char key))
2375 (setq line
2376 (format
2377 "%-4s%-14s"
2378 (org-add-props (copy-sequence key)
2379 '(face bold))
2380 (cond
2381 ((string-match "\\S-" desc) desc)
2382 ((eq type 'agenda) "Agenda for current week or day")
2383 ((eq type 'alltodo) "List of all TODO entries")
2384 ((eq type 'search) "Word search")
2385 ((eq type 'stuck) "List of stuck projects")
2386 ((eq type 'todo) "TODO keyword")
2387 ((eq type 'tags) "Tags query")
2388 ((eq type 'tags-todo) "Tags (TODO)")
2389 ((eq type 'tags-tree) "Tags tree")
2390 ((eq type 'todo-tree) "TODO kwd tree")
2391 ((eq type 'occur-tree) "Occur tree")
2392 ((functionp type) (if (symbolp type)
2393 (symbol-name type)
2394 "Lambda expression"))
2395 (t "???"))))
2396 (if org-agenda-menu-show-matcher
2397 (setq line
2398 (concat line ": "
2399 (cond
2400 ((stringp match)
2401 (setq match (copy-sequence match))
2402 (org-add-props match nil 'face 'org-warning))
2403 (match
2404 (format "set of %d commands" (length match)))
2405 (t ""))))
2406 (if (org-string-nw-p match)
2407 (add-text-properties
2408 0 (length line) (list 'help-echo
2409 (concat "Matcher: "match)) line)))
2410 (push line lines)))
2411 (setq lines (nreverse lines))
2412 (when prefixes
2413 (mapc (lambda (x)
2414 (push
2415 (format "%s %s"
2416 (org-add-props (char-to-string x)
2417 nil 'face 'bold)
2418 (or (cdr (assoc (concat selstring
2419 (char-to-string x))
2420 prefix-descriptions))
2421 "Prefix key"))
2422 lines))
2423 prefixes))
2425 ;; Check if we should display in two columns
2426 (if org-agenda-menu-two-column
2427 (progn
2428 (setq n (length lines)
2429 n1 (+ (/ n 2) (mod n 2))
2430 right (nthcdr n1 lines)
2431 left (copy-sequence lines))
2432 (setcdr (nthcdr (1- n1) left) nil))
2433 (setq left lines right nil))
2434 (while left
2435 (insert "\n" (pop left))
2436 (when right
2437 (if (< (current-column) 40)
2438 (move-to-column 40 t)
2439 (insert " "))
2440 (insert (pop right))))
2442 ;; Make the window the right size
2443 (goto-char (point-min))
2444 (if second-time
2445 (if (not (pos-visible-in-window-p (point-max)))
2446 (org-fit-window-to-buffer))
2447 (setq second-time t)
2448 (org-fit-window-to-buffer))
2450 ;; Ask for selection
2451 (message "Press key for agenda command%s:"
2452 (if (or restrict-ok org-agenda-overriding-restriction)
2453 (if org-agenda-overriding-restriction
2454 " (restriction lock active)"
2455 (if restriction
2456 (format " (restricted to %s)" restriction)
2457 " (unrestricted)"))
2458 ""))
2459 (setq c (read-char-exclusive))
2460 (message "")
2461 (cond
2462 ((assoc (char-to-string c) custom)
2463 (setq selstring (concat selstring (char-to-string c)))
2464 (throw 'exit (cons selstring restriction)))
2465 ((memq c prefixes)
2466 (setq selstring (concat selstring (char-to-string c))
2467 prefixes nil
2468 rmheader (or rmheader t)
2469 custom (delq nil (mapcar
2470 (lambda (x)
2471 (if (or (= (length (car x)) 1)
2472 (/= (string-to-char (car x)) c))
2474 (cons (substring (car x) 1) (cdr x))))
2475 custom))))
2476 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2477 (message "Restriction is only possible in Org-mode buffers")
2478 (ding) (sit-for 1))
2479 ((eq c ?1)
2480 (org-agenda-remove-restriction-lock 'noupdate)
2481 (setq restriction 'buffer))
2482 ((eq c ?0)
2483 (org-agenda-remove-restriction-lock 'noupdate)
2484 (setq restriction (if region-p 'region 'subtree)))
2485 ((eq c ?<)
2486 (org-agenda-remove-restriction-lock 'noupdate)
2487 (setq restriction
2488 (cond
2489 ((eq restriction 'buffer)
2490 (if region-p 'region 'subtree))
2491 ((memq restriction '(subtree region))
2492 nil)
2493 (t 'buffer))))
2494 ((eq c ?>)
2495 (org-agenda-remove-restriction-lock 'noupdate)
2496 (setq restriction nil))
2497 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2498 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2499 ((and (> (length selstring) 0) (eq c ?\d))
2500 (delete-window)
2501 (org-agenda-get-restriction-and-command prefix-descriptions))
2503 ((equal c ?q) (error "Abort"))
2504 (t (error "Invalid key %c" c))))))))
2506 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2507 (defvar org-agenda-last-arguments nil
2508 "The arguments of the previous call to `org-agenda'.")
2509 (defun org-agenda-run-series (name series)
2510 (org-let (nth 1 series) '(org-prepare-agenda name))
2511 (let* ((org-agenda-multi t)
2512 (redo (list 'org-agenda-run-series name (list 'quote series)))
2513 (org-agenda-overriding-arguments
2514 (or org-agenda-overriding-arguments
2515 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2516 (get 'org-agenda-redo-command 'last-args))))
2517 (cmds (car series))
2518 (gprops (nth 1 series))
2519 match ;; The byte compiler incorrectly complains about this. Keep it!
2520 cmd type lprops)
2521 (while (setq cmd (pop cmds))
2522 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2523 (cond
2524 ((eq type 'agenda)
2525 (org-let2 gprops lprops
2526 '(call-interactively 'org-agenda-list)))
2527 ((eq type 'alltodo)
2528 (org-let2 gprops lprops
2529 '(call-interactively 'org-todo-list)))
2530 ((eq type 'search)
2531 (org-let2 gprops lprops
2532 '(org-search-view current-prefix-arg match nil)))
2533 ((eq type 'stuck)
2534 (org-let2 gprops lprops
2535 '(call-interactively 'org-agenda-list-stuck-projects)))
2536 ((eq type 'tags)
2537 (org-let2 gprops lprops
2538 '(org-tags-view current-prefix-arg match)))
2539 ((eq type 'tags-todo)
2540 (org-let2 gprops lprops
2541 '(org-tags-view '(4) match)))
2542 ((eq type 'todo)
2543 (org-let2 gprops lprops
2544 '(org-todo-list match)))
2545 ((fboundp type)
2546 (org-let2 gprops lprops
2547 '(funcall type match)))
2548 (t (error "Invalid type in command series"))))
2549 (widen)
2550 (setq org-agenda-redo-command redo)
2551 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2552 (goto-char (point-min)))
2553 (org-fit-agenda-window)
2554 (org-let (nth 1 series) '(org-finalize-agenda)))
2556 ;;;###autoload
2557 (defmacro org-batch-agenda (cmd-key &rest parameters)
2558 "Run an agenda command in batch mode and send the result to STDOUT.
2559 If CMD-KEY is a string of length 1, it is used as a key in
2560 `org-agenda-custom-commands' and triggers this command. If it is a
2561 longer string it is used as a tags/todo match string.
2562 Parameters are alternating variable names and values that will be bound
2563 before running the agenda command."
2564 (let (pars)
2565 (while parameters
2566 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2567 (if (> (length cmd-key) 2)
2568 (eval (list 'let (nreverse pars)
2569 (list 'org-tags-view nil cmd-key)))
2570 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2571 (set-buffer org-agenda-buffer-name)
2572 (princ (org-encode-for-stdout (buffer-string)))))
2574 ;(defun org-encode-for-stdout (string)
2575 ; (if (fboundp 'encode-coding-string)
2576 ; (encode-coding-string string buffer-file-coding-system)
2577 ; string))
2579 (defun org-encode-for-stdout (string)
2580 string)
2582 (defvar org-agenda-info nil)
2584 ;;;###autoload
2585 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2586 "Run an agenda command in batch mode and send the result to STDOUT.
2587 If CMD-KEY is a string of length 1, it is used as a key in
2588 `org-agenda-custom-commands' and triggers this command. If it is a
2589 longer string it is used as a tags/todo match string.
2590 Parameters are alternating variable names and values that will be bound
2591 before running the agenda command.
2593 The output gives a line for each selected agenda item. Each
2594 item is a list of comma-separated values, like this:
2596 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2598 category The category of the item
2599 head The headline, without TODO kwd, TAGS and PRIORITY
2600 type The type of the agenda entry, can be
2601 todo selected in TODO match
2602 tagsmatch selected in tags match
2603 diary imported from diary
2604 deadline a deadline on given date
2605 scheduled scheduled on given date
2606 timestamp entry has timestamp on given date
2607 closed entry was closed on given date
2608 upcoming-deadline warning about deadline
2609 past-scheduled forwarded scheduled item
2610 block entry has date block including g. date
2611 todo The todo keyword, if any
2612 tags All tags including inherited ones, separated by colons
2613 date The relevant date, like 2007-2-14
2614 time The time, like 15:00-16:50
2615 extra Sting with extra planning info
2616 priority-l The priority letter if any was given
2617 priority-n The computed numerical priority
2618 agenda-day The day in the agenda where this is listed"
2620 (let (pars)
2621 (while parameters
2622 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2623 (push (list 'org-agenda-remove-tags t) pars)
2624 (if (> (length cmd-key) 2)
2625 (eval (list 'let (nreverse pars)
2626 (list 'org-tags-view nil cmd-key)))
2627 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2628 (set-buffer org-agenda-buffer-name)
2629 (let* ((lines (org-split-string (buffer-string) "\n"))
2630 line)
2631 (while (setq line (pop lines))
2632 (catch 'next
2633 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2634 (setq org-agenda-info
2635 (org-fix-agenda-info (text-properties-at 0 line)))
2636 (princ
2637 (org-encode-for-stdout
2638 (mapconcat 'org-agenda-export-csv-mapper
2639 '(org-category txt type todo tags date time extra
2640 priority-letter priority agenda-day)
2641 ",")))
2642 (princ "\n"))))))
2644 (defun org-fix-agenda-info (props)
2645 "Make sure all properties on an agenda item have a canonical form.
2646 This ensures the export commands can easily use it."
2647 (let (tmp re)
2648 (when (setq tmp (plist-get props 'tags))
2649 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2650 (when (setq tmp (plist-get props 'date))
2651 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2652 (let ((calendar-date-display-form '(year "-" month "-" day)))
2653 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2655 (setq tmp (calendar-date-string tmp)))
2656 (setq props (plist-put props 'date tmp)))
2657 (when (setq tmp (plist-get props 'day))
2658 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2659 (let ((calendar-date-display-form '(year "-" month "-" day)))
2660 (setq tmp (calendar-date-string tmp)))
2661 (setq props (plist-put props 'day tmp))
2662 (setq props (plist-put props 'agenda-day tmp)))
2663 (when (setq tmp (plist-get props 'txt))
2664 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2665 (plist-put props 'priority-letter (match-string 1 tmp))
2666 (setq tmp (replace-match "" t t tmp)))
2667 (when (and (setq re (plist-get props 'org-todo-regexp))
2668 (setq re (concat "\\`\\.*" re " ?"))
2669 (string-match re tmp))
2670 (plist-put props 'todo (match-string 1 tmp))
2671 (setq tmp (replace-match "" t t tmp)))
2672 (plist-put props 'txt tmp)))
2673 props)
2675 (defun org-agenda-export-csv-mapper (prop)
2676 (let ((res (plist-get org-agenda-info prop)))
2677 (setq res
2678 (cond
2679 ((not res) "")
2680 ((stringp res) res)
2681 (t (prin1-to-string res))))
2682 (while (string-match "," res)
2683 (setq res (replace-match ";" t t res)))
2684 (org-trim res)))
2687 ;;;###autoload
2688 (defun org-store-agenda-views (&rest parameters)
2689 (interactive)
2690 (eval (list 'org-batch-store-agenda-views)))
2692 ;; FIXME, why is this a macro?????
2693 ;;;###autoload
2694 (defmacro org-batch-store-agenda-views (&rest parameters)
2695 "Run all custom agenda commands that have a file argument."
2696 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2697 (pop-up-frames nil)
2698 (dir default-directory)
2699 pars cmd thiscmdkey files opts cmd-or-set)
2700 (while parameters
2701 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2702 (setq pars (reverse pars))
2703 (save-window-excursion
2704 (while cmds
2705 (setq cmd (pop cmds)
2706 thiscmdkey (car cmd)
2707 cmd-or-set (nth 2 cmd)
2708 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2709 files (nth (if (listp cmd-or-set) 4 5) cmd))
2710 (if (stringp files) (setq files (list files)))
2711 (when files
2712 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2713 (list 'org-agenda nil thiscmdkey)))
2714 (set-buffer org-agenda-buffer-name)
2715 (while files
2716 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2717 (list 'org-write-agenda
2718 (expand-file-name (pop files) dir) nil t))))
2719 (and (get-buffer org-agenda-buffer-name)
2720 (kill-buffer org-agenda-buffer-name)))))))
2722 (defun org-agenda-mark-header-line (pos)
2723 "Mark the line at POS as an agenda structure header."
2724 (save-excursion
2725 (goto-char pos)
2726 (put-text-property (point-at-bol) (point-at-eol)
2727 'org-agenda-structural-header t)
2728 (when org-agenda-title-append
2729 (put-text-property (point-at-bol) (point-at-eol)
2730 'org-agenda-title-append org-agenda-title-append))))
2732 (defvar org-mobile-creating-agendas)
2733 (defun org-write-agenda (file &optional open nosettings)
2734 "Write the current buffer (an agenda view) as a file.
2735 Depending on the extension of the file name, plain text (.txt),
2736 HTML (.html or .htm) or Postscript (.ps) is produced.
2737 If the extension is .ics, run icalendar export over all files used
2738 to construct the agenda and limit the export to entries listed in the
2739 agenda now.
2740 With prefix argument OPEN, open the new file immediately.
2741 If NOSETTINGS is given, do not scope the settings of
2742 `org-agenda-exporter-settings' into the export commands. This is used when
2743 the settings have already been scoped and we do not wish to overrule other,
2744 higher priority settings."
2745 (interactive "FWrite agenda to file: \nP")
2746 (if (not (file-writable-p file))
2747 (error "Cannot write agenda to file %s" file))
2748 (org-let (if nosettings nil org-agenda-exporter-settings)
2749 '(save-excursion
2750 (save-window-excursion
2751 (org-agenda-mark-filtered-text)
2752 (let ((bs (copy-sequence (buffer-string))) beg)
2753 (org-agenda-unmark-filtered-text)
2754 (with-temp-buffer
2755 (rename-buffer "Agenda View" t)
2756 (set-buffer-modified-p nil)
2757 (insert bs)
2758 (org-agenda-remove-marked-text 'org-filtered)
2759 (while (setq beg (text-property-any (point-min) (point-max)
2760 'org-filtered t))
2761 (delete-region
2762 beg (or (next-single-property-change beg 'org-filtered)
2763 (point-max))))
2764 (run-hooks 'org-agenda-before-write-hook)
2765 (cond
2766 ((org-bound-and-true-p org-mobile-creating-agendas)
2767 (org-mobile-write-agenda-for-mobile file))
2768 ((string-match "\\.html?\\'" file)
2769 (require 'htmlize)
2770 (set-buffer (htmlize-buffer (current-buffer)))
2772 (when (and org-agenda-export-html-style
2773 (string-match "<style>" org-agenda-export-html-style))
2774 ;; replace <style> section with org-agenda-export-html-style
2775 (goto-char (point-min))
2776 (kill-region (- (search-forward "<style") 6)
2777 (search-forward "</style>"))
2778 (insert org-agenda-export-html-style))
2779 (write-file file)
2780 (kill-buffer (current-buffer))
2781 (message "HTML written to %s" file))
2782 ((string-match "\\.ps\\'" file)
2783 (require 'ps-print)
2784 (ps-print-buffer-with-faces file)
2785 (message "Postscript written to %s" file))
2786 ((string-match "\\.pdf\\'" file)
2787 (require 'ps-print)
2788 (ps-print-buffer-with-faces
2789 (concat (file-name-sans-extension file) ".ps"))
2790 (call-process "ps2pdf" nil nil nil
2791 (expand-file-name
2792 (concat (file-name-sans-extension file) ".ps"))
2793 (expand-file-name file))
2794 (delete-file (concat (file-name-sans-extension file) ".ps"))
2795 (message "PDF written to %s" file))
2796 ((string-match "\\.ics\\'" file)
2797 (require 'org-icalendar)
2798 (let ((org-agenda-marker-table
2799 (org-create-marker-find-array
2800 (org-agenda-collect-markers)))
2801 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2802 (org-combined-agenda-icalendar-file file))
2803 (apply 'org-export-icalendar 'combine
2804 (org-agenda-files nil 'ifmode))))
2806 (let ((bs (buffer-string)))
2807 (find-file file)
2808 (erase-buffer)
2809 (insert bs)
2810 (save-buffer 0)
2811 (kill-buffer (current-buffer))
2812 (message "Plain text written to %s" file))))))))
2813 (set-buffer org-agenda-buffer-name))
2814 (when open (org-open-file file)))
2816 (defvar org-agenda-filter-overlays nil)
2818 (defun org-agenda-mark-filtered-text ()
2819 "Mark all text hidden by filtering with a text property."
2820 (let ((inhibit-read-only t))
2821 (mapc
2822 (lambda (o)
2823 (when (equal (overlay-buffer o) (current-buffer))
2824 (put-text-property
2825 (overlay-start o) (overlay-end o)
2826 'org-filtered t)))
2827 org-agenda-filter-overlays)))
2829 (defun org-agenda-unmark-filtered-text ()
2830 "Remove the filtering text property."
2831 (let ((inhibit-read-only t))
2832 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2834 (defun org-agenda-remove-marked-text (property &optional value)
2835 "Delete all text marked with VALUE of PROPERTY.
2836 VALUE defaults to t."
2837 (let (beg)
2838 (setq value (or value t))
2839 (while (setq beg (text-property-any (point-min) (point-max)
2840 property value))
2841 (delete-region
2842 beg (or (next-single-property-change beg 'org-filtered)
2843 (point-max))))))
2845 (defun org-agenda-add-entry-text ()
2846 "Add entry text to agenda lines.
2847 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2848 entry text following headings shown in the agenda.
2849 Drawers will be excluded, also the line with scheduling/deadline info."
2850 (when (and (> org-agenda-add-entry-text-maxlines 0)
2851 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2852 (let (m txt)
2853 (goto-char (point-min))
2854 (while (not (eobp))
2855 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2856 (beginning-of-line 2)
2857 (setq txt (org-agenda-get-some-entry-text
2858 m org-agenda-add-entry-text-maxlines " > "))
2859 (end-of-line 1)
2860 (if (string-match "\\S-" txt)
2861 (insert "\n" txt)
2862 (or (eobp) (forward-char 1))))))))
2864 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2865 &rest keep)
2866 "Extract entry text from MARKER, at most N-LINES lines.
2867 This will ignore drawers etc, just get the text.
2868 If INDENT is given, prefix every line with this string. If KEEP is
2869 given, it is a list of symbols, defining stuff that should not be
2870 removed from the entry content. Currently only `planning' is allowed here."
2871 (let (txt drawer-re kwd-time-re ind)
2872 (save-excursion
2873 (with-current-buffer (marker-buffer marker)
2874 (if (not (org-mode-p))
2875 (setq txt "")
2876 (save-excursion
2877 (save-restriction
2878 (widen)
2879 (goto-char marker)
2880 (end-of-line 1)
2881 (setq txt (buffer-substring
2882 (min (1+ (point)) (point-max))
2883 (progn (outline-next-heading) (point)))
2884 drawer-re org-drawer-regexp
2885 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2886 ".*\n?"))
2887 (with-temp-buffer
2888 (insert txt)
2889 (when org-agenda-add-entry-text-descriptive-links
2890 (goto-char (point-min))
2891 (while (org-activate-bracket-links (point-max))
2892 (add-text-properties (match-beginning 0) (match-end 0)
2893 '(face org-link))))
2894 (goto-char (point-min))
2895 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2896 (set-text-properties (match-beginning 0) (match-end 0)
2897 nil))
2898 (goto-char (point-min))
2899 (while (re-search-forward drawer-re nil t)
2900 (delete-region
2901 (match-beginning 0)
2902 (progn (re-search-forward
2903 "^[ \t]*:END:.*\n?" nil 'move)
2904 (point))))
2905 (unless (member 'planning keep)
2906 (goto-char (point-min))
2907 (while (re-search-forward kwd-time-re nil t)
2908 (replace-match "")))
2909 (goto-char (point-min))
2910 (when org-agenda-entry-text-exclude-regexps
2911 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2912 (while (setq re (pop re-list))
2913 (goto-char (point-min))
2914 (while (re-search-forward re nil t)
2915 (replace-match "")))))
2916 (goto-char (point-max))
2917 (skip-chars-backward " \t\n")
2918 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2920 ;; find and remove min common indentation
2921 (goto-char (point-min))
2922 (untabify (point-min) (point-max))
2923 (setq ind (org-get-indentation))
2924 (while (not (eobp))
2925 (unless (looking-at "[ \t]*$")
2926 (setq ind (min ind (org-get-indentation))))
2927 (beginning-of-line 2))
2928 (goto-char (point-min))
2929 (while (not (eobp))
2930 (unless (looking-at "[ \t]*$")
2931 (move-to-column ind)
2932 (delete-region (point-at-bol) (point)))
2933 (beginning-of-line 2))
2935 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2937 (goto-char (point-min))
2938 (when indent
2939 (while (and (not (eobp)) (re-search-forward "^" nil t))
2940 (replace-match indent t t)))
2941 (goto-char (point-min))
2942 (while (looking-at "[ \t]*\n") (replace-match ""))
2943 (goto-char (point-max))
2944 (when (> (org-current-line)
2945 n-lines)
2946 (org-goto-line (1+ n-lines))
2947 (backward-char 1))
2948 (setq txt (buffer-substring (point-min) (point)))))))))
2949 txt))
2951 (defun org-agenda-collect-markers ()
2952 "Collect the markers pointing to entries in the agenda buffer."
2953 (let (m markers)
2954 (save-excursion
2955 (goto-char (point-min))
2956 (while (not (eobp))
2957 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2958 (org-get-at-bol 'org-marker)))
2959 (push m markers))
2960 (beginning-of-line 2)))
2961 (nreverse markers)))
2963 (defun org-create-marker-find-array (marker-list)
2964 "Create a alist of files names with all marker positions in that file."
2965 (let (f tbl m a p)
2966 (while (setq m (pop marker-list))
2967 (setq p (marker-position m)
2968 f (buffer-file-name (or (buffer-base-buffer
2969 (marker-buffer m))
2970 (marker-buffer m))))
2971 (if (setq a (assoc f tbl))
2972 (push (marker-position m) (cdr a))
2973 (push (list f p) tbl)))
2974 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2975 tbl)))
2977 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2978 (defun org-check-agenda-marker-table ()
2979 "Check of the current entry is on the marker list."
2980 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2982 (and (setq a (assoc file org-agenda-marker-table))
2983 (save-match-data
2984 (save-excursion
2985 (org-back-to-heading t)
2986 (member (point) (cdr a)))))))
2988 (defun org-check-for-org-mode ()
2989 "Make sure current buffer is in org-mode. Error if not."
2990 (or (org-mode-p)
2991 (error "Cannot execute org-mode agenda command on buffer in %s"
2992 major-mode)))
2994 (defun org-fit-agenda-window ()
2995 "Fit the window to the buffer size."
2996 (and (memq org-agenda-window-setup '(reorganize-frame))
2997 (fboundp 'fit-window-to-buffer)
2998 (org-fit-window-to-buffer
3000 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3001 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3003 ;;; Agenda prepare and finalize
3005 (defvar org-agenda-multi nil) ; dynamically scoped
3006 (defvar org-agenda-buffer-name "*Org Agenda*")
3007 (defvar org-pre-agenda-window-conf nil)
3008 (defvar org-agenda-columns-active nil)
3009 (defvar org-agenda-name nil)
3010 (defvar org-agenda-filter nil)
3011 (defvar org-agenda-filter-while-redo nil)
3012 (defvar org-agenda-filter-preset nil
3013 "A preset of the tags filter used for secondary agenda filtering.
3014 This must be a list of strings, each string must be a single tag preceded
3015 by \"+\" or \"-\".
3016 This variable should not be set directly, but agenda custom commands can
3017 bind it in the options section. The preset filter is a global property of
3018 the entire agenda view. In a block agenda, it will not work reliably to
3019 define a filter for one of the individual blocks. You need to set it in
3020 the global options and expect it to be applied to the entire view.")
3022 (defun org-prepare-agenda (&optional name)
3023 (setq org-todo-keywords-for-agenda nil)
3024 (setq org-done-keywords-for-agenda nil)
3025 (setq org-drawers-for-agenda nil)
3026 (unless org-agenda-persistent-filter
3027 (setq org-agenda-filter nil))
3028 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3029 (if org-agenda-multi
3030 (progn
3031 (setq buffer-read-only nil)
3032 (goto-char (point-max))
3033 (unless (or (bobp) org-agenda-compact-blocks)
3034 (insert "\n"
3035 (if (stringp org-agenda-block-separator)
3036 org-agenda-block-separator
3037 (make-string (window-width) org-agenda-block-separator))
3038 "\n"))
3039 (narrow-to-region (point) (point-max)))
3040 (org-agenda-reset-markers)
3041 (setq org-agenda-contributing-files nil)
3042 (setq org-agenda-columns-active nil)
3043 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3044 (setq org-todo-keywords-for-agenda
3045 (org-uniquify org-todo-keywords-for-agenda))
3046 (setq org-done-keywords-for-agenda
3047 (org-uniquify org-done-keywords-for-agenda))
3048 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3049 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3050 (awin (get-buffer-window abuf)))
3051 (cond
3052 ((equal (current-buffer) abuf) nil)
3053 (awin (select-window awin))
3054 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3055 ((equal org-agenda-window-setup 'current-window)
3056 (switch-to-buffer abuf))
3057 ((equal org-agenda-window-setup 'other-window)
3058 (org-switch-to-buffer-other-window abuf))
3059 ((equal org-agenda-window-setup 'other-frame)
3060 (switch-to-buffer-other-frame abuf))
3061 ((equal org-agenda-window-setup 'reorganize-frame)
3062 (delete-other-windows)
3063 (org-switch-to-buffer-other-window abuf)))
3064 ;; additional test in case agenda is invoked from within agenda
3065 ;; buffer via elisp link
3066 (unless (equal (current-buffer) abuf)
3067 (switch-to-buffer abuf)))
3068 (setq buffer-read-only nil)
3069 (let ((inhibit-read-only t)) (erase-buffer))
3070 (org-agenda-mode)
3071 (and name (not org-agenda-name)
3072 (org-set-local 'org-agenda-name name)))
3073 (setq buffer-read-only nil))
3075 (defun org-finalize-agenda ()
3076 "Finishing touch for the agenda buffer, called just before displaying it."
3077 (unless org-agenda-multi
3078 (save-excursion
3079 (let ((inhibit-read-only t))
3080 (goto-char (point-min))
3081 (while (org-activate-bracket-links (point-max))
3082 (add-text-properties (match-beginning 0) (match-end 0)
3083 '(face org-link)))
3084 (org-agenda-align-tags)
3085 (unless org-agenda-with-colors
3086 (remove-text-properties (point-min) (point-max) '(face nil))))
3087 (if (and (boundp 'org-agenda-overriding-columns-format)
3088 org-agenda-overriding-columns-format)
3089 (org-set-local 'org-agenda-overriding-columns-format
3090 org-agenda-overriding-columns-format))
3091 (if (and (boundp 'org-agenda-view-columns-initially)
3092 org-agenda-view-columns-initially)
3093 (org-agenda-columns))
3094 (when org-agenda-fontify-priorities
3095 (org-agenda-fontify-priorities))
3096 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3097 (org-agenda-dim-blocked-tasks))
3098 (org-agenda-mark-clocking-task)
3099 (when org-agenda-entry-text-mode
3100 (org-agenda-entry-text-hide)
3101 (org-agenda-entry-text-show))
3102 (if (functionp 'org-habit-insert-consistency-graphs)
3103 (org-habit-insert-consistency-graphs))
3104 (run-hooks 'org-finalize-agenda-hook)
3105 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3106 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3107 (org-agenda-filter-apply org-agenda-filter))
3110 (defun org-agenda-mark-clocking-task ()
3111 "Mark the current clock entry in the agenda if it is present."
3112 (mapc (lambda (o)
3113 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3114 (delete-overlay o)))
3115 (overlays-in (point-min) (point-max)))
3116 (when (marker-buffer org-clock-hd-marker)
3117 (save-excursion
3118 (goto-char (point-min))
3119 (let (s ov)
3120 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3121 (goto-char s)
3122 (when (equal (org-get-at-bol 'org-hd-marker)
3123 org-clock-hd-marker)
3124 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3125 (overlay-put ov 'type 'org-agenda-clocking)
3126 (overlay-put ov 'face 'org-agenda-clocking)
3127 (overlay-put ov 'help-echo
3128 "The clock is running in this item")))))))
3130 (defun org-agenda-fontify-priorities ()
3131 "Make highest priority lines bold, and lowest italic."
3132 (interactive)
3133 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3134 (delete-overlay o)))
3135 (overlays-in (point-min) (point-max)))
3136 (save-excursion
3137 (let ((inhibit-read-only t)
3138 b e p ov h l)
3139 (goto-char (point-min))
3140 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3141 (setq h (or (get-char-property (point) 'org-highest-priority)
3142 org-highest-priority)
3143 l (or (get-char-property (point) 'org-lowest-priority)
3144 org-lowest-priority)
3145 p (string-to-char (match-string 1))
3146 b (match-beginning 0)
3147 e (if (eq org-agenda-fontify-priorities 'cookies)
3148 (match-end 0)
3149 (point-at-eol))
3150 ov (make-overlay b e))
3151 (overlay-put
3152 ov 'face
3153 (cond ((org-face-from-face-or-color
3154 'priority nil
3155 (cdr (assoc p org-priority-faces))))
3156 ((and (listp org-agenda-fontify-priorities)
3157 (org-face-from-face-or-color
3158 'priority nil
3159 (cdr (assoc p org-agenda-fontify-priorities)))))
3160 ((equal p l) 'italic)
3161 ((equal p h) 'bold)))
3162 (overlay-put ov 'org-type 'org-priority)))))
3164 (defun org-agenda-dim-blocked-tasks ()
3165 "Dim currently blocked TODO's in the agenda display."
3166 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3167 (delete-overlay o)))
3168 (overlays-in (point-min) (point-max)))
3169 (save-excursion
3170 (let ((inhibit-read-only t)
3171 (org-depend-tag-blocked nil)
3172 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3173 org-blocked-by-checkboxes
3174 invis1 b e p ov h l)
3175 (goto-char (point-min))
3176 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3177 (and pos (goto-char (1+ pos))))
3178 (setq org-blocked-by-checkboxes nil invis1 invis)
3179 (let ((marker (org-get-at-bol 'org-hd-marker)))
3180 (when (and marker
3181 (not (with-current-buffer (marker-buffer marker)
3182 (save-excursion
3183 (goto-char marker)
3184 (if (org-entry-get nil "NOBLOCKING")
3185 t ;; Never block this entry
3186 (run-hook-with-args-until-failure
3187 'org-blocker-hook
3188 (list :type 'todo-state-change
3189 :position marker
3190 :from 'todo
3191 :to 'done)))))))
3192 (if org-blocked-by-checkboxes (setq invis1 nil))
3193 (setq b (if invis1
3194 (max (point-min) (1- (point-at-bol)))
3195 (point-at-bol))
3196 e (point-at-eol)
3197 ov (make-overlay b e))
3198 (if invis1
3199 (overlay-put ov 'invisible t)
3200 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3201 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3203 (defvar org-agenda-skip-function nil
3204 "Function to be called at each match during agenda construction.
3205 If this function returns nil, the current match should not be skipped.
3206 Otherwise, the function must return a position from where the search
3207 should be continued.
3208 This may also be a Lisp form, it will be evaluated.
3209 Never set this variable using `setq' or so, because then it will apply
3210 to all future agenda commands. If you do want a global skipping condition,
3211 use the option `org-agenda-skip-function-global' instead.
3212 The correct usage for `org-agenda-skip-function' is to bind it with
3213 `let' to scope it dynamically into the agenda-constructing command.
3214 A good way to set it is through options in `org-agenda-custom-commands'.")
3216 (defun org-agenda-skip ()
3217 "Throw to `:skip' in places that should be skipped.
3218 Also moves point to the end of the skipped region, so that search can
3219 continue from there."
3220 (let ((p (point-at-bol)) to)
3221 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3222 (get-text-property p :org-archived)
3223 (org-end-of-subtree t)
3224 (throw :skip t))
3225 (and org-agenda-skip-comment-trees
3226 (get-text-property p :org-comment)
3227 (org-end-of-subtree t)
3228 (throw :skip t))
3229 (if (equal (char-after p) ?#) (throw :skip t))
3230 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3231 (org-agenda-skip-eval org-agenda-skip-function)))
3232 (goto-char to)
3233 (throw :skip t))))
3235 (defun org-agenda-skip-eval (form)
3236 "If FORM is a function or a list, call (or eval) is and return result.
3237 `save-excursion' and `save-match-data' are wrapped around the call, so point
3238 and match data are returned to the previous state no matter what these
3239 functions do."
3240 (let (fp)
3241 (and form
3242 (or (setq fp (functionp form))
3243 (consp form))
3244 (save-excursion
3245 (save-match-data
3246 (if fp
3247 (funcall form)
3248 (eval form)))))))
3250 (defvar org-agenda-markers nil
3251 "List of all currently active markers created by `org-agenda'.")
3252 (defvar org-agenda-last-marker-time (org-float-time)
3253 "Creation time of the last agenda marker.")
3255 (defun org-agenda-new-marker (&optional pos)
3256 "Return a new agenda marker.
3257 Org-mode keeps a list of these markers and resets them when they are
3258 no longer in use."
3259 (let ((m (copy-marker (or pos (point)))))
3260 (setq org-agenda-last-marker-time (org-float-time))
3261 (push m org-agenda-markers)
3264 (defun org-agenda-reset-markers ()
3265 "Reset markers created by `org-agenda'."
3266 (while org-agenda-markers
3267 (move-marker (pop org-agenda-markers) nil)))
3269 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3270 "Save relative positions of markers in region."
3271 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3272 org-agenda-markers))
3274 ;;; Entry text mode
3276 (defun org-agenda-entry-text-show-here ()
3277 "Add some text from the entry as context to the current line."
3278 (let (m txt o)
3279 (setq m (org-get-at-bol 'org-hd-marker))
3280 (unless (marker-buffer m)
3281 (error "No marker points to an entry here"))
3282 (setq txt (concat "\n" (org-no-properties
3283 (org-agenda-get-some-entry-text
3284 m org-agenda-entry-text-maxlines " > "))))
3285 (when (string-match "\\S-" txt)
3286 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3287 (overlay-put o 'evaporate t)
3288 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3289 (overlay-put o 'after-string txt))))
3291 (defun org-agenda-entry-text-show ()
3292 "Add entry context for all agenda lines."
3293 (interactive)
3294 (save-excursion
3295 (goto-char (point-max))
3296 (beginning-of-line 1)
3297 (while (not (bobp))
3298 (when (org-get-at-bol 'org-hd-marker)
3299 (org-agenda-entry-text-show-here))
3300 (beginning-of-line 0))))
3302 (defun org-agenda-entry-text-hide ()
3303 "Remove any shown entry context."
3304 (delq nil
3305 (mapcar (lambda (o)
3306 (if (eq (overlay-get o 'org-overlay-type)
3307 'agenda-entry-content)
3308 (progn (delete-overlay o) t)))
3309 (overlays-in (point-min) (point-max)))))
3311 (defun org-agenda-get-day-face (date)
3312 "Return the face DATE should be displayed with."
3313 (or (and (functionp org-agenda-day-face-function)
3314 (funcall org-agenda-day-face-function date))
3315 (cond ((org-agenda-todayp date)
3316 'org-agenda-date-today)
3317 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3318 'org-agenda-date-weekend)
3319 (t 'org-agenda-date))))
3321 ;;; Agenda timeline
3323 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3325 (defun org-timeline (&optional include-all)
3326 "Show a time-sorted view of the entries in the current org file.
3327 Only entries with a time stamp of today or later will be listed. With
3328 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3329 under the current date.
3330 If the buffer contains an active region, only check the region for
3331 dates."
3332 (interactive "P")
3333 (org-compile-prefix-format 'timeline)
3334 (org-set-sorting-strategy 'timeline)
3335 (let* ((dopast t)
3336 (dotodo include-all)
3337 (doclosed org-agenda-show-log)
3338 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3339 (current-buffer))))
3340 (date (calendar-current-date))
3341 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3342 (end (if (org-region-active-p) (region-end) (point-max)))
3343 (day-numbers (org-get-all-dates beg end 'no-ranges
3344 t doclosed ; always include today
3345 org-timeline-show-empty-dates))
3346 (org-deadline-warning-days 0)
3347 (org-agenda-only-exact-dates t)
3348 (today (org-today))
3349 (past t)
3350 args
3351 s e rtn d emptyp)
3352 (setq org-agenda-redo-command
3353 (list 'progn
3354 (list 'org-switch-to-buffer-other-window (current-buffer))
3355 (list 'org-timeline (list 'quote include-all))))
3356 (if (not dopast)
3357 ;; Remove past dates from the list of dates.
3358 (setq day-numbers (delq nil (mapcar (lambda(x)
3359 (if (>= x today) x nil))
3360 day-numbers))))
3361 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3362 (if doclosed (push :closed args))
3363 (push :timestamp args)
3364 (push :deadline args)
3365 (push :scheduled args)
3366 (push :sexp args)
3367 (if dotodo (push :todo args))
3368 (insert "Timeline of file " entry "\n")
3369 (add-text-properties (point-min) (point)
3370 (list 'face 'org-agenda-structure))
3371 (org-agenda-mark-header-line (point-min))
3372 (while (setq d (pop day-numbers))
3373 (if (and (listp d) (eq (car d) :omitted))
3374 (progn
3375 (setq s (point))
3376 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3377 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3378 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3379 (if (and (>= d today)
3380 dopast
3381 past)
3382 (progn
3383 (setq past nil)
3384 (insert (make-string 79 ?-) "\n")))
3385 (setq date (calendar-gregorian-from-absolute d))
3386 (setq s (point))
3387 (setq rtn (and (not emptyp)
3388 (apply 'org-agenda-get-day-entries entry
3389 date args)))
3390 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3391 (progn
3392 (insert
3393 (if (stringp org-agenda-format-date)
3394 (format-time-string org-agenda-format-date
3395 (org-time-from-absolute date))
3396 (funcall org-agenda-format-date date))
3397 "\n")
3398 (put-text-property s (1- (point)) 'face
3399 (org-agenda-get-day-face date))
3400 (put-text-property s (1- (point)) 'org-date-line t)
3401 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3402 (if (equal d today)
3403 (put-text-property s (1- (point)) 'org-today t))
3404 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3405 (put-text-property s (1- (point)) 'day d)))))
3406 (goto-char (point-min))
3407 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3408 (point-min)))
3409 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3410 (org-finalize-agenda)
3411 (setq buffer-read-only t)))
3413 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3414 "Return a list of all relevant day numbers from BEG to END buffer positions.
3415 If NO-RANGES is non-nil, include only the start and end dates of a range,
3416 not every single day in the range. If FORCE-TODAY is non-nil, make
3417 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3418 inactive time stamps (those in square brackets) are included.
3419 When EMPTY is non-nil, also include days without any entries."
3420 (let ((re (concat
3421 (if pre-re pre-re "")
3422 (if inactive org-ts-regexp-both org-ts-regexp)))
3423 dates dates1 date day day1 day2 ts1 ts2)
3424 (if force-today
3425 (setq dates (list (org-today))))
3426 (save-excursion
3427 (goto-char beg)
3428 (while (re-search-forward re end t)
3429 (setq day (time-to-days (org-time-string-to-time
3430 (substring (match-string 1) 0 10))))
3431 (or (memq day dates) (push day dates)))
3432 (unless no-ranges
3433 (goto-char beg)
3434 (while (re-search-forward org-tr-regexp end t)
3435 (setq ts1 (substring (match-string 1) 0 10)
3436 ts2 (substring (match-string 2) 0 10)
3437 day1 (time-to-days (org-time-string-to-time ts1))
3438 day2 (time-to-days (org-time-string-to-time ts2)))
3439 (while (< (setq day1 (1+ day1)) day2)
3440 (or (memq day1 dates) (push day1 dates)))))
3441 (setq dates (sort dates '<))
3442 (when empty
3443 (while (setq day (pop dates))
3444 (setq day2 (car dates))
3445 (push day dates1)
3446 (when (and day2 empty)
3447 (if (or (eq empty t)
3448 (and (numberp empty) (<= (- day2 day) empty)))
3449 (while (< (setq day (1+ day)) day2)
3450 (push (list day) dates1))
3451 (push (cons :omitted (- day2 day)) dates1))))
3452 (setq dates (nreverse dates1)))
3453 dates)))
3455 ;;; Agenda Daily/Weekly
3457 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3458 "Start day for the agenda view.
3459 Custom commands can set this variable in the options section.")
3460 (defvar org-starting-day nil) ; local variable in the agenda buffer
3461 (defvar org-agenda-current-span nil
3462 "The current span used in the agenda view.") ; local variable in the agenda buffer
3463 (defvar org-include-all-loc nil) ; local variable
3465 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3466 "List of types searched for when creating the daily/weekly agenda.
3467 This variable is a list of symbols that controls the types of
3468 items that appear in the daily/weekly agenda. Allowed symbols in this
3469 list are are
3471 :timestamp List items containing a date stamp or date range matching
3472 the selected date. This includes sexp entries in
3473 angular brackets.
3475 :sexp List entries resulting from plain diary-like sexps.
3477 :deadline List deadline due on that date. When the date is today,
3478 also list any deadlines past due, or due within
3479 `org-deadline-warning-days'. `:deadline' must appear before
3480 `:scheduled' if the setting of
3481 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3482 any effect.
3484 :scheduled List all items which are scheduled for the given date.
3485 The diary for *today* also contains items which were
3486 scheduled earlier and are not yet marked DONE.
3488 By default, all four types are turned on.
3490 Never set this variable globally using `setq', because then it
3491 will apply to all future agenda commands. Instead, bind it with
3492 `let' to scope it dynamically into the the agenda-constructing
3493 command. A good way to set it is through options in
3494 `org-agenda-custom-commands'. For a more flexible (though
3495 somewhat less efficient) way of determining what is included in
3496 the daily/weekly agenda, see `org-agenda-skip-function'.")
3498 ;;;###autoload
3499 (defun org-agenda-list (&optional include-all start-day span)
3500 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3501 The view will be for the current day or week, but from the overview buffer
3502 you will be able to go to other days/weeks.
3504 With a numeric prefix argument in an interactive call, the agenda will
3505 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3506 the number of days. SPAN defaults to `org-agenda-span'.
3508 START-DAY defaults to TODAY, or to the most recent match for the weekday
3509 given in `org-agenda-start-on-weekday'."
3510 (interactive "P")
3511 (if (and (integerp include-all) (> include-all 0))
3512 (setq span include-all include-all nil))
3513 (setq start-day (or start-day org-agenda-start-day))
3514 (if org-agenda-overriding-arguments
3515 (setq include-all (car org-agenda-overriding-arguments)
3516 start-day (nth 1 org-agenda-overriding-arguments)
3517 span (nth 2 org-agenda-overriding-arguments)))
3518 (if (stringp start-day)
3519 ;; Convert to an absolute day number
3520 (setq start-day (time-to-days (org-read-date nil t start-day))))
3521 (setq org-agenda-last-arguments (list include-all start-day span))
3522 (org-compile-prefix-format 'agenda)
3523 (org-set-sorting-strategy 'agenda)
3524 (let* ((span (org-agenda-ndays-to-span
3525 (or span org-agenda-ndays org-agenda-span)))
3526 (today (org-today))
3527 (sd (or start-day today))
3528 (ndays (org-agenda-span-to-ndays span sd))
3529 (org-agenda-start-on-weekday
3530 (if (eq ndays 7)
3531 org-agenda-start-on-weekday))
3532 (thefiles (org-agenda-files nil 'ifmode))
3533 (files thefiles)
3534 (start (if (or (null org-agenda-start-on-weekday)
3535 (< ndays 7))
3537 (let* ((nt (calendar-day-of-week
3538 (calendar-gregorian-from-absolute sd)))
3539 (n1 org-agenda-start-on-weekday)
3540 (d (- nt n1)))
3541 (- sd (+ (if (< d 0) 7 0) d)))))
3542 (day-numbers (list start))
3543 (day-cnt 0)
3544 (inhibit-redisplay (not debug-on-error))
3545 s e rtn rtnall file date d start-pos end-pos todayp
3546 clocktable-start clocktable-end filter)
3547 (setq org-agenda-redo-command
3548 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3549 (dotimes (n (1- ndays))
3550 (push (1+ (car day-numbers)) day-numbers))
3551 (setq day-numbers (nreverse day-numbers))
3552 (setq clocktable-start (car day-numbers)
3553 clocktable-end (1+ (or (org-last day-numbers) 0)))
3554 (org-prepare-agenda "Day/Week")
3555 (org-set-local 'org-starting-day (car day-numbers))
3556 (org-set-local 'org-include-all-loc include-all)
3557 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3558 (unless org-agenda-compact-blocks
3559 (let* ((d1 (car day-numbers))
3560 (d2 (org-last day-numbers))
3561 (w1 (org-days-to-iso-week d1))
3562 (w2 (org-days-to-iso-week d2)))
3563 (setq s (point))
3564 (if org-agenda-overriding-header
3565 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3566 nil 'face 'org-agenda-structure) "\n")
3567 (insert (org-agenda-span-name span)
3568 "-agenda"
3569 (if (< (- d2 d1) 350)
3570 (if (= w1 w2)
3571 (format " (W%02d)" w1)
3572 (format " (W%02d-W%02d)" w1 w2))
3574 ":\n")))
3575 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3576 'org-date-line t))
3577 (org-agenda-mark-header-line s))
3578 (while (setq d (pop day-numbers))
3579 (setq date (calendar-gregorian-from-absolute d)
3580 s (point))
3581 (if (or (setq todayp (= d today))
3582 (and (not start-pos) (= d sd)))
3583 (setq start-pos (point))
3584 (if (and start-pos (not end-pos))
3585 (setq end-pos (point))))
3586 (setq files thefiles
3587 rtnall nil)
3588 (while (setq file (pop files))
3589 (catch 'nextfile
3590 (org-check-agenda-file file)
3591 (let ((org-agenda-entry-types org-agenda-entry-types))
3592 (unless org-agenda-include-deadlines
3593 (setq org-agenda-entry-types
3594 (delq :deadline org-agenda-entry-types)))
3595 (cond
3596 ((memq org-agenda-show-log '(only clockcheck))
3597 (setq rtn (org-agenda-get-day-entries
3598 file date :closed)))
3599 (org-agenda-show-log
3600 (setq rtn (apply 'org-agenda-get-day-entries
3601 file date
3602 (append '(:closed) org-agenda-entry-types))))
3604 (setq rtn (apply 'org-agenda-get-day-entries
3605 file date
3606 org-agenda-entry-types)))))
3607 (setq rtnall (append rtnall rtn))))
3608 (if org-agenda-include-diary
3609 (let ((org-agenda-search-headline-for-time t))
3610 (require 'diary-lib)
3611 (setq rtn (org-get-entries-from-diary date))
3612 (setq rtnall (append rtnall rtn))))
3613 (if (or rtnall org-agenda-show-all-dates)
3614 (progn
3615 (setq day-cnt (1+ day-cnt))
3616 (insert
3617 (if (stringp org-agenda-format-date)
3618 (format-time-string org-agenda-format-date
3619 (org-time-from-absolute date))
3620 (funcall org-agenda-format-date date))
3621 "\n")
3622 (put-text-property s (1- (point)) 'face
3623 (org-agenda-get-day-face date))
3624 (put-text-property s (1- (point)) 'org-date-line t)
3625 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3626 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3627 (when todayp
3628 (put-text-property s (1- (point)) 'org-today t))
3629 (if rtnall (insert
3630 (org-finalize-agenda-entries
3631 (org-agenda-add-time-grid-maybe
3632 rtnall ndays todayp))
3633 "\n"))
3634 (put-text-property s (1- (point)) 'day d)
3635 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3636 (when (and org-agenda-clockreport-mode clocktable-start)
3637 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3638 ;; the above line is to ensure the restricted range!
3639 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3640 tbl)
3641 (setq p (org-plist-delete p :block))
3642 (setq p (plist-put p :tstart clocktable-start))
3643 (setq p (plist-put p :tend clocktable-end))
3644 (setq p (plist-put p :scope 'agenda))
3645 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3646 (setq filter (or org-agenda-filter-while-redo
3647 (get 'org-agenda-filter :preset-filter))))
3648 (setq p (plist-put p :tags (mapconcat (lambda (x)
3649 (if (string-match "[<>=]" x)
3652 filter ""))))
3653 (setq tbl (apply 'org-get-clocktable p))
3654 (insert tbl)))
3655 (goto-char (point-min))
3656 (or org-agenda-multi (org-fit-agenda-window))
3657 (unless (and (pos-visible-in-window-p (point-min))
3658 (pos-visible-in-window-p (point-max)))
3659 (goto-char (1- (point-max)))
3660 (recenter -1)
3661 (if (not (pos-visible-in-window-p (or start-pos 1)))
3662 (progn
3663 (goto-char (or start-pos 1))
3664 (recenter 1))))
3665 (goto-char (or start-pos 1))
3666 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3667 (if (eq org-agenda-show-log 'clockcheck)
3668 (org-agenda-show-clocking-issues))
3669 (org-finalize-agenda)
3670 (setq buffer-read-only t)
3671 (message "")))
3673 (defun org-agenda-ndays-to-span (n)
3674 "Return a span symbol for a span of N days, or N if none matches."
3675 (cond ((symbolp n) n)
3676 ((= n 1) 'day)
3677 ((= n 7) 'week)
3678 (t n)))
3680 (defun org-agenda-span-to-ndays (span start-day)
3681 "Return ndays from SPAN starting at START-DAY."
3682 (cond ((numberp span) span)
3683 ((eq span 'day) 1)
3684 ((eq span 'week) 7)
3685 ((eq span 'month)
3686 (let ((date (calendar-gregorian-from-absolute start-day)))
3687 (calendar-last-day-of-month (car date) (caddr date))))
3688 ((eq span 'year)
3689 (let ((date (calendar-gregorian-from-absolute start-day)))
3690 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3692 (defun org-agenda-span-name (span)
3693 "Return a SPAN name."
3694 (if (null span)
3696 (if (symbolp span)
3697 (capitalize (symbol-name span))
3698 (format "%d days" span))))
3700 ;;; Agenda word search
3702 (defvar org-agenda-search-history nil)
3703 (defvar org-todo-only nil)
3705 (defvar org-search-syntax-table nil
3706 "Special syntax table for org-mode search.
3707 In this table, we have single quotes not as word constituents, to
3708 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3710 (defun org-search-syntax-table ()
3711 (unless org-search-syntax-table
3712 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3713 (modify-syntax-entry ?' "." org-search-syntax-table)
3714 (modify-syntax-entry ?` "." org-search-syntax-table))
3715 org-search-syntax-table)
3717 (defvar org-agenda-last-search-view-search-was-boolean nil)
3719 ;;;###autoload
3720 (defun org-search-view (&optional todo-only string edit-at)
3721 "Show all entries that contain a phrase or words or regular expressions.
3723 With optional prefix argument TODO-ONLY, only consider entries that are
3724 TODO entries. The argument STRING can be used to pass a default search
3725 string into this function. If EDIT-AT is non-nil, it means that the
3726 user should get a chance to edit this string, with cursor at position
3727 EDIT-AT.
3729 The search string can be viewed either as a phrase that should be found as
3730 is, or it can be broken into a number of snippets, each of which must match
3731 in a Boolean way to select an entry. The default depends on the variable
3732 `org-agenda-search-view-always-boolean'.
3733 Even if this is turned off (the default) you can always switch to
3734 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3736 The default is a direct search of the whole phrase, where each space in
3737 the search string can expand to an arbitrary amount of whitespace,
3738 including newlines.
3740 If using a Boolean search, the search string is split on whitespace and
3741 each snippet is searched separately, with logical AND to select an entry.
3742 Words prefixed with a minus must *not* occur in the entry. Words without
3743 a prefix or prefixed with a plus must occur in the entry. Matching is
3744 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3745 match whole words, not parts of a word) if
3746 `org-agenda-search-view-force-full-words' is set (default is nil).
3748 Boolean search snippets enclosed by curly braces are interpreted as
3749 regular expressions that must or (when preceded with \"-\") must not
3750 match in the entry. Snippets enclosed into double quotes will be taken
3751 as a whole, to include whitespace.
3753 - If the search string starts with an asterisk, search only in headlines.
3754 - If (possibly after the leading star) the search string starts with an
3755 exclamation mark, this also means to look at TODO entries only, an effect
3756 that can also be achieved with a prefix argument.
3757 - If (possibly after star and exclamation mark) the search string starts
3758 with a colon, this will mean that the (non-regexp) snippets of the
3759 Boolean search must match as full words.
3761 This command searches the agenda files, and in addition the files listed
3762 in `org-agenda-text-search-extra-files'."
3763 (interactive "P")
3764 (org-compile-prefix-format 'search)
3765 (org-set-sorting-strategy 'search)
3766 (org-prepare-agenda "SEARCH")
3767 (let* ((props (list 'face nil
3768 'done-face 'org-agenda-done
3769 'org-not-done-regexp org-not-done-regexp
3770 'org-todo-regexp org-todo-regexp
3771 'org-complex-heading-regexp org-complex-heading-regexp
3772 'mouse-face 'highlight
3773 'help-echo (format "mouse-2 or RET jump to location")))
3774 (full-words org-agenda-search-view-force-full-words)
3775 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3776 regexp rtn rtnall files file pos
3777 marker category tags c neg re boolean
3778 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3779 (unless (and (not edit-at)
3780 (stringp string)
3781 (string-match "\\S-" string))
3782 (setq string (read-string
3783 (if org-agenda-search-view-always-boolean
3784 "[+-]Word/{Regexp} ...: "
3785 "Phrase, or [+-]Word/{Regexp} ...: ")
3786 (cond
3787 ((integerp edit-at) (cons string edit-at))
3788 (edit-at string))
3789 'org-agenda-search-history)))
3790 (org-set-local 'org-todo-only todo-only)
3791 (setq org-agenda-redo-command
3792 (list 'org-search-view (if todo-only t nil) string
3793 '(if current-prefix-arg 1 nil)))
3794 (setq org-agenda-query-string string)
3796 (if (equal (string-to-char string) ?*)
3797 (setq hdl-only t
3798 words (substring string 1))
3799 (setq words string))
3800 (when (equal (string-to-char words) ?!)
3801 (setq todo-only t
3802 words (substring words 1)))
3803 (when (equal (string-to-char words) ?:)
3804 (setq full-words t
3805 words (substring words 1)))
3806 (if (or org-agenda-search-view-always-boolean
3807 (member (string-to-char words) '(?- ?+ ?\{)))
3808 (setq boolean t))
3809 (setq words (org-split-string words))
3810 (let (www w)
3811 (while (setq w (pop words))
3812 (while (and (string-match "\\\\\\'" w) words)
3813 (setq w (concat (substring w 0 -1) " " (pop words))))
3814 (push w www))
3815 (setq words (nreverse www) www nil)
3816 (while (setq w (pop words))
3817 (when (and (string-match "\\`[-+]?{" w)
3818 (not (string-match "}\\'" w)))
3819 (while (and words (not (string-match "}\\'" (car words))))
3820 (setq w (concat w " " (pop words))))
3821 (setq w (concat w " " (pop words))))
3822 (push w www))
3823 (setq words (nreverse www)))
3824 (setq org-agenda-last-search-view-search-was-boolean boolean)
3825 (when boolean
3826 (let (wds w)
3827 (while (setq w (pop words))
3828 (if (or (equal (substring w 0 1) "\"")
3829 (and (> (length w) 1)
3830 (member (substring w 0 1) '("+" "-"))
3831 (equal (substring w 1 2) "\"")))
3832 (while (and words (not (equal (substring w -1) "\"")))
3833 (setq w (concat w " " (pop words)))))
3834 (and (string-match "\\`\\([-+]?\\)\"" w)
3835 (setq w (replace-match "\\1" nil nil w)))
3836 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3837 (push w wds))
3838 (setq words (nreverse wds))))
3839 (if boolean
3840 (mapc (lambda (w)
3841 (setq c (string-to-char w))
3842 (if (equal c ?-)
3843 (setq neg t w (substring w 1))
3844 (if (equal c ?+)
3845 (setq neg nil w (substring w 1))
3846 (setq neg nil)))
3847 (if (string-match "\\`{.*}\\'" w)
3848 (setq re (substring w 1 -1))
3849 (if full-words
3850 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3851 (setq re (regexp-quote (downcase w)))))
3852 (if neg (push re regexps-) (push re regexps+)))
3853 words)
3854 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3855 regexps+))
3856 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3857 (if (not regexps+)
3858 (setq regexp (concat "^" org-outline-regexp))
3859 (setq regexp (pop regexps+))
3860 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3861 regexp))))
3862 (setq files (org-agenda-files nil 'ifmode))
3863 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3864 (pop org-agenda-text-search-extra-files)
3865 (setq files (org-add-archive-files files)))
3866 (setq files (append files org-agenda-text-search-extra-files)
3867 rtnall nil)
3868 (while (setq file (pop files))
3869 (setq ee nil)
3870 (catch 'nextfile
3871 (org-check-agenda-file file)
3872 (setq buffer (if (file-exists-p file)
3873 (org-get-agenda-file-buffer file)
3874 (error "No such file %s" file)))
3875 (if (not buffer)
3876 ;; If file does not exist, make sure an error message is sent
3877 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3878 file))))
3879 (with-current-buffer buffer
3880 (with-syntax-table (org-search-syntax-table)
3881 (unless (org-mode-p)
3882 (error "Agenda file %s is not in `org-mode'" file))
3883 (let ((case-fold-search t))
3884 (save-excursion
3885 (save-restriction
3886 (if org-agenda-restrict
3887 (narrow-to-region org-agenda-restrict-begin
3888 org-agenda-restrict-end)
3889 (widen))
3890 (goto-char (point-min))
3891 (unless (or (org-on-heading-p)
3892 (outline-next-heading))
3893 (throw 'nextfile t))
3894 (goto-char (max (point-min) (1- (point))))
3895 (while (re-search-forward regexp nil t)
3896 (org-back-to-heading t)
3897 (skip-chars-forward "* ")
3898 (setq beg (point-at-bol)
3899 beg1 (point)
3900 end (progn (outline-next-heading) (point)))
3901 (catch :skip
3902 (goto-char beg)
3903 (org-agenda-skip)
3904 (setq str (buffer-substring-no-properties
3905 (point-at-bol)
3906 (if hdl-only (point-at-eol) end)))
3907 (mapc (lambda (wr) (when (string-match wr str)
3908 (goto-char (1- end))
3909 (throw :skip t)))
3910 regexps-)
3911 (mapc (lambda (wr) (unless (string-match wr str)
3912 (goto-char (1- end))
3913 (throw :skip t)))
3914 (if todo-only
3915 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3916 regexps+)
3917 regexps+))
3918 (goto-char beg)
3919 (setq marker (org-agenda-new-marker (point))
3920 category (org-get-category)
3921 tags (org-get-tags-at (point))
3922 txt (org-format-agenda-item
3924 (buffer-substring-no-properties
3925 beg1 (point-at-eol))
3926 category tags))
3927 (org-add-props txt props
3928 'org-marker marker 'org-hd-marker marker
3929 'org-todo-regexp org-todo-regexp
3930 'org-complex-heading-regexp org-complex-heading-regexp
3931 'priority 1000 'org-category category
3932 'type "search")
3933 (push txt ee)
3934 (goto-char (1- end))))))))))
3935 (setq rtn (nreverse ee))
3936 (setq rtnall (append rtnall rtn)))
3937 (if org-agenda-overriding-header
3938 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3939 nil 'face 'org-agenda-structure) "\n")
3940 (insert "Search words: ")
3941 (add-text-properties (point-min) (1- (point))
3942 (list 'face 'org-agenda-structure))
3943 (setq pos (point))
3944 (insert string "\n")
3945 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3946 (setq pos (point))
3947 (unless org-agenda-multi
3948 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3949 (add-text-properties pos (1- (point))
3950 (list 'face 'org-agenda-structure))))
3951 (org-agenda-mark-header-line (point-min))
3952 (when rtnall
3953 (insert (org-finalize-agenda-entries rtnall) "\n"))
3954 (goto-char (point-min))
3955 (or org-agenda-multi (org-fit-agenda-window))
3956 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3957 (org-finalize-agenda)
3958 (setq buffer-read-only t)))
3960 ;;; Agenda TODO list
3962 (defvar org-select-this-todo-keyword nil)
3963 (defvar org-last-arg nil)
3965 ;;;###autoload
3966 (defun org-todo-list (arg)
3967 "Show all (not done) TODO entries from all agenda file in a single list.
3968 The prefix arg can be used to select a specific TODO keyword and limit
3969 the list to these. When using \\[universal-argument], you will be prompted
3970 for a keyword. A numeric prefix directly selects the Nth keyword in
3971 `org-todo-keywords-1'."
3972 (interactive "P")
3973 (org-compile-prefix-format 'todo)
3974 (org-set-sorting-strategy 'todo)
3975 (org-prepare-agenda "TODO")
3976 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3977 (let* ((today (org-today))
3978 (date (calendar-gregorian-from-absolute today))
3979 (kwds org-todo-keywords-for-agenda)
3980 (completion-ignore-case t)
3981 (org-select-this-todo-keyword
3982 (if (stringp arg) arg
3983 (and arg (integerp arg) (> arg 0)
3984 (nth (1- arg) kwds))))
3985 rtn rtnall files file pos)
3986 (when (equal arg '(4))
3987 (setq org-select-this-todo-keyword
3988 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3989 (mapcar 'list kwds) nil nil)))
3990 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3991 (org-set-local 'org-last-arg arg)
3992 (setq org-agenda-redo-command
3993 '(org-todo-list (or current-prefix-arg org-last-arg)))
3994 (setq files (org-agenda-files nil 'ifmode)
3995 rtnall nil)
3996 (while (setq file (pop files))
3997 (catch 'nextfile
3998 (org-check-agenda-file file)
3999 (setq rtn (org-agenda-get-day-entries file date :todo))
4000 (setq rtnall (append rtnall rtn))))
4001 (if org-agenda-overriding-header
4002 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4003 nil 'face 'org-agenda-structure) "\n")
4004 (insert "Global list of TODO items of type: ")
4005 (add-text-properties (point-min) (1- (point))
4006 (list 'face 'org-agenda-structure
4007 'short-heading
4008 (concat "ToDo: "
4009 (or org-select-this-todo-keyword "ALL"))))
4010 (org-agenda-mark-header-line (point-min))
4011 (setq pos (point))
4012 (insert (or org-select-this-todo-keyword "ALL") "\n")
4013 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4014 (setq pos (point))
4015 (unless org-agenda-multi
4016 (insert "Available with `N r': (0)ALL")
4017 (let ((n 0) s)
4018 (mapc (lambda (x)
4019 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4020 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4021 (insert "\n "))
4022 (insert " " s))
4023 kwds))
4024 (insert "\n"))
4025 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4026 (org-agenda-mark-header-line (point-min))
4027 (when rtnall
4028 (insert (org-finalize-agenda-entries rtnall) "\n"))
4029 (goto-char (point-min))
4030 (or org-agenda-multi (org-fit-agenda-window))
4031 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4032 (org-finalize-agenda)
4033 (setq buffer-read-only t)))
4035 ;;; Agenda tags match
4037 ;;;###autoload
4038 (defun org-tags-view (&optional todo-only match)
4039 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4040 The prefix arg TODO-ONLY limits the search to TODO entries."
4041 (interactive "P")
4042 (org-compile-prefix-format 'tags)
4043 (org-set-sorting-strategy 'tags)
4044 (let* ((org-tags-match-list-sublevels
4045 org-tags-match-list-sublevels)
4046 (completion-ignore-case t)
4047 rtn rtnall files file pos matcher
4048 buffer)
4049 (when (and (stringp match) (not (string-match "\\S-" match)))
4050 (setq match nil))
4051 (setq matcher (org-make-tags-matcher match)
4052 match (car matcher) matcher (cdr matcher))
4053 (org-prepare-agenda (concat "TAGS " match))
4054 (setq org-agenda-query-string match)
4055 (setq org-agenda-redo-command
4056 (list 'org-tags-view (list 'quote todo-only)
4057 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4058 (setq files (org-agenda-files nil 'ifmode)
4059 rtnall nil)
4060 (while (setq file (pop files))
4061 (catch 'nextfile
4062 (org-check-agenda-file file)
4063 (setq buffer (if (file-exists-p file)
4064 (org-get-agenda-file-buffer file)
4065 (error "No such file %s" file)))
4066 (if (not buffer)
4067 ;; If file does not exist, error message to agenda
4068 (setq rtn (list
4069 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4070 rtnall (append rtnall rtn))
4071 (with-current-buffer buffer
4072 (unless (org-mode-p)
4073 (error "Agenda file %s is not in `org-mode'" file))
4074 (save-excursion
4075 (save-restriction
4076 (if org-agenda-restrict
4077 (narrow-to-region org-agenda-restrict-begin
4078 org-agenda-restrict-end)
4079 (widen))
4080 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4081 (setq rtnall (append rtnall rtn))))))))
4082 (if org-agenda-overriding-header
4083 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4084 nil 'face 'org-agenda-structure) "\n")
4085 (insert "Headlines with TAGS match: ")
4086 (add-text-properties (point-min) (1- (point))
4087 (list 'face 'org-agenda-structure
4088 'short-heading
4089 (concat "Match: " match)))
4090 (setq pos (point))
4091 (insert match "\n")
4092 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4093 (setq pos (point))
4094 (unless org-agenda-multi
4095 (insert "Press `C-u r' to search again with new search string\n"))
4096 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4097 (org-agenda-mark-header-line (point-min))
4098 (when rtnall
4099 (insert (org-finalize-agenda-entries rtnall) "\n"))
4100 (goto-char (point-min))
4101 (or org-agenda-multi (org-fit-agenda-window))
4102 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4103 (org-finalize-agenda)
4104 (setq buffer-read-only t)))
4106 ;;; Agenda Finding stuck projects
4108 (defvar org-agenda-skip-regexp nil
4109 "Regular expression used in skipping subtrees for the agenda.
4110 This is basically a temporary global variable that can be set and then
4111 used by user-defined selections using `org-agenda-skip-function'.")
4113 (defvar org-agenda-overriding-header nil
4114 "When this is set during todo and tags searches, will replace header.
4115 This variable should not be set directly, but custom commands can bind it
4116 in the options section.")
4118 (defun org-agenda-skip-entry-when-regexp-matches ()
4119 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4120 If yes, it returns the end position of this entry, causing agenda commands
4121 to skip the entry but continuing the search in the subtree. This is a
4122 function that can be put into `org-agenda-skip-function' for the duration
4123 of a command."
4124 (let ((end (save-excursion (org-end-of-subtree t)))
4125 skip)
4126 (save-excursion
4127 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4128 (and skip end)))
4130 (defun org-agenda-skip-subtree-when-regexp-matches ()
4131 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4132 If yes, it returns the end position of this tree, causing agenda commands
4133 to skip this subtree. This is a function that can be put into
4134 `org-agenda-skip-function' for the duration of a command."
4135 (let ((end (save-excursion (org-end-of-subtree t)))
4136 skip)
4137 (save-excursion
4138 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4139 (and skip end)))
4141 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4142 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4143 If yes, it returns the end position of the current entry (NOT the tree),
4144 causing agenda commands to skip the entry but continuing the search in
4145 the subtree. This is a function that can be put into
4146 `org-agenda-skip-function' for the duration of a command. An important
4147 use of this function is for the stuck project list."
4148 (let ((end (save-excursion (org-end-of-subtree t)))
4149 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4150 skip)
4151 (save-excursion
4152 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4153 (and skip entry-end)))
4155 (defun org-agenda-skip-entry-if (&rest conditions)
4156 "Skip entry if any of CONDITIONS is true.
4157 See `org-agenda-skip-if' for details."
4158 (org-agenda-skip-if nil conditions))
4160 (defun org-agenda-skip-subtree-if (&rest conditions)
4161 "Skip entry if any of CONDITIONS is true.
4162 See `org-agenda-skip-if' for details."
4163 (org-agenda-skip-if t conditions))
4165 (defun org-agenda-skip-if (subtree conditions)
4166 "Checks current entity for CONDITIONS.
4167 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4168 the entry, i.e. the text before the next heading is checked.
4170 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4171 from different tests. Valid conditions are:
4173 scheduled Check if there is a scheduled cookie
4174 notscheduled Check if there is no scheduled cookie
4175 deadline Check if there is a deadline
4176 notdeadline Check if there is no deadline
4177 timestamp Check if there is a timestamp (also deadline or scheduled)
4178 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4179 regexp Check if regexp matches
4180 notregexp Check if regexp does not match.
4181 todo Check if TODO keyword matches
4182 nottodo Check if TODO keyword does not match
4184 The regexp is taken from the conditions list, it must come right after
4185 the `regexp' or `notregexp' element.
4187 `todo' and `nottodo' accept as an argument a list of todo
4188 keywords, which may include \"*\" to match any todo keyword.
4190 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4192 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4194 Instead of a list a keyword class may be given
4196 (org-agenda-skip-entry-if 'nottodo 'done)
4198 would skip entries that haven't been marked with any of \"DONE\"
4199 keywords. Possible classes are: `todo', `done', `any'.
4201 If any of these conditions is met, this function returns the end point of
4202 the entity, causing the search to continue from there. This is a function
4203 that can be put into `org-agenda-skip-function' for the duration of a command."
4204 (let (beg end m)
4205 (org-back-to-heading t)
4206 (setq beg (point)
4207 end (if subtree
4208 (progn (org-end-of-subtree t) (point))
4209 (progn (outline-next-heading) (1- (point)))))
4210 (goto-char beg)
4211 (and
4213 (and (memq 'scheduled conditions)
4214 (re-search-forward org-scheduled-time-regexp end t))
4215 (and (memq 'notscheduled conditions)
4216 (not (re-search-forward org-scheduled-time-regexp end t)))
4217 (and (memq 'deadline conditions)
4218 (re-search-forward org-deadline-time-regexp end t))
4219 (and (memq 'notdeadline conditions)
4220 (not (re-search-forward org-deadline-time-regexp end t)))
4221 (and (memq 'timestamp conditions)
4222 (re-search-forward org-ts-regexp end t))
4223 (and (memq 'nottimestamp conditions)
4224 (not (re-search-forward org-ts-regexp end t)))
4225 (and (setq m (memq 'regexp conditions))
4226 (stringp (nth 1 m))
4227 (re-search-forward (nth 1 m) end t))
4228 (and (setq m (memq 'notregexp conditions))
4229 (stringp (nth 1 m))
4230 (not (re-search-forward (nth 1 m) end t)))
4231 (and (or
4232 (setq m (memq 'todo conditions))
4233 (setq m (memq 'nottodo conditions)))
4234 (org-agenda-skip-if-todo m end)))
4235 end)))
4237 (defun org-agenda-skip-if-todo (args end)
4238 "Helper function for `org-agenda-skip-if', do not use it directly.
4239 ARGS is a list with first element either `todo' or `nottodo'.
4240 The remainder is either a list of TODO keywords, or a state symbol
4241 `todo' or `done' or `any'."
4242 (let ((kw (car args))
4243 (arg (cadr args))
4244 todo-wds todo-re)
4245 (setq todo-wds
4246 (org-uniquify
4247 (cond
4248 ((listp arg) ;; list of keywords
4249 (if (member "*" arg)
4250 (mapcar 'substring-no-properties org-todo-keywords-1)
4251 arg))
4252 ((symbolp arg) ;; keyword class name
4253 (cond
4254 ((eq arg 'todo)
4255 (org-delete-all org-done-keywords
4256 (mapcar 'substring-no-properties
4257 org-todo-keywords-1)))
4258 ((eq arg 'done) org-done-keywords)
4259 ((eq arg 'any)
4260 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4261 (setq todo-re
4262 (concat "^\\*+[ \t]+\\<\\("
4263 (mapconcat 'identity todo-wds "\\|")
4264 "\\)\\>"))
4265 (if (eq kw 'todo)
4266 (re-search-forward todo-re end t)
4267 (not (re-search-forward todo-re end t)))))
4269 ;;;###autoload
4270 (defun org-agenda-list-stuck-projects (&rest ignore)
4271 "Create agenda view for projects that are stuck.
4272 Stuck projects are project that have no next actions. For the definitions
4273 of what a project is and how to check if it stuck, customize the variable
4274 `org-stuck-projects'."
4275 (interactive)
4276 (let* ((org-agenda-skip-function
4277 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4278 ;; We could have used org-agenda-skip-if here.
4279 (org-agenda-overriding-header
4280 (or org-agenda-overriding-header "List of stuck projects: "))
4281 (matcher (nth 0 org-stuck-projects))
4282 (todo (nth 1 org-stuck-projects))
4283 (todo-wds (if (member "*" todo)
4284 (progn
4285 (org-prepare-agenda-buffers (org-agenda-files
4286 nil 'ifmode))
4287 (org-delete-all
4288 org-done-keywords-for-agenda
4289 (copy-sequence org-todo-keywords-for-agenda)))
4290 todo))
4291 (todo-re (concat "^\\*+[ \t]+\\("
4292 (mapconcat 'identity todo-wds "\\|")
4293 "\\)\\>"))
4294 (tags (nth 2 org-stuck-projects))
4295 (tags-re (if (member "*" tags)
4296 (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
4297 (if tags
4298 (concat "^\\*+ .*:\\("
4299 (mapconcat 'identity tags "\\|")
4300 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4301 (gen-re (nth 3 org-stuck-projects))
4302 (re-list
4303 (delq nil
4304 (list
4305 (if todo todo-re)
4306 (if tags tags-re)
4307 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4308 gen-re)))))
4309 (setq org-agenda-skip-regexp
4310 (if re-list
4311 (mapconcat 'identity re-list "\\|")
4312 (error "No information how to identify unstuck projects")))
4313 (org-tags-view nil matcher)
4314 (with-current-buffer org-agenda-buffer-name
4315 (setq org-agenda-redo-command
4316 '(org-agenda-list-stuck-projects
4317 (or current-prefix-arg org-last-arg))))))
4319 ;;; Diary integration
4321 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4322 (defvar list-diary-entries-hook)
4323 (defvar diary-time-regexp)
4324 (defun org-get-entries-from-diary (date)
4325 "Get the (Emacs Calendar) diary entries for DATE."
4326 (require 'diary-lib)
4327 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4328 (diary-display-hook '(fancy-diary-display))
4329 (diary-display-function 'fancy-diary-display)
4330 (pop-up-frames nil)
4331 (list-diary-entries-hook
4332 (cons 'org-diary-default-entry list-diary-entries-hook))
4333 (diary-file-name-prefix-function nil) ; turn this feature off
4334 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4335 entries
4336 (org-disable-agenda-to-diary t))
4337 (save-excursion
4338 (save-window-excursion
4339 (funcall (if (fboundp 'diary-list-entries)
4340 'diary-list-entries 'list-diary-entries)
4341 date 1)))
4342 (if (not (get-buffer diary-fancy-buffer))
4343 (setq entries nil)
4344 (with-current-buffer diary-fancy-buffer
4345 (setq buffer-read-only nil)
4346 (if (zerop (buffer-size))
4347 ;; No entries
4348 (setq entries nil)
4349 ;; Omit the date and other unnecessary stuff
4350 (org-agenda-cleanup-fancy-diary)
4351 ;; Add prefix to each line and extend the text properties
4352 (if (zerop (buffer-size))
4353 (setq entries nil)
4354 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4355 (setq entries
4356 (with-temp-buffer
4357 (insert entries) (goto-char (point-min))
4358 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4359 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4360 (replace-match (concat "; " (match-string 1)))))
4361 (buffer-string)))))
4362 (set-buffer-modified-p nil)
4363 (kill-buffer diary-fancy-buffer)))
4364 (when entries
4365 (setq entries (org-split-string entries "\n"))
4366 (setq entries
4367 (mapcar
4368 (lambda (x)
4369 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4370 ;; Extend the text properties to the beginning of the line
4371 (org-add-props x (text-properties-at (1- (length x)) x)
4372 'type "diary" 'date date 'face 'org-agenda-diary))
4373 entries)))))
4375 (defvar org-agenda-cleanup-fancy-diary-hook nil
4376 "Hook run when the fancy diary buffer is cleaned up.")
4378 (defun org-agenda-cleanup-fancy-diary ()
4379 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4380 This gets rid of the date, the underline under the date, and
4381 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4382 date. It also removes lines that contain only whitespace."
4383 (goto-char (point-min))
4384 (if (looking-at ".*?:[ \t]*")
4385 (progn
4386 (replace-match "")
4387 (re-search-forward "\n=+$" nil t)
4388 (replace-match "")
4389 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4390 (re-search-forward "\n=+$" nil t)
4391 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4392 (goto-char (point-min))
4393 (while (re-search-forward "^ +\n" nil t)
4394 (replace-match ""))
4395 (goto-char (point-min))
4396 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4397 (replace-match ""))
4398 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4400 ;; Make sure entries from the diary have the right text properties.
4401 (eval-after-load "diary-lib"
4402 '(if (boundp 'diary-modify-entry-list-string-function)
4403 ;; We can rely on the hook, nothing to do
4405 ;; Hook not available, must use advice to make this work
4406 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4407 "Make the position visible."
4408 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4409 (stringp string)
4410 buffer-file-name)
4411 (setq string (org-modify-diary-entry-string string))))))
4413 (defun org-modify-diary-entry-string (string)
4414 "Add text properties to string, allowing org-mode to act on it."
4415 (org-add-props string nil
4416 'mouse-face 'highlight
4417 'help-echo (if buffer-file-name
4418 (format "mouse-2 or RET jump to diary file %s"
4419 (abbreviate-file-name buffer-file-name))
4421 'org-agenda-diary-link t
4422 'org-marker (org-agenda-new-marker (point-at-bol))))
4424 (defun org-diary-default-entry ()
4425 "Add a dummy entry to the diary.
4426 Needed to avoid empty dates which mess up holiday display."
4427 ;; Catch the error if dealing with the new add-to-diary-alist
4428 (when org-disable-agenda-to-diary
4429 (condition-case nil
4430 (org-add-to-diary-list original-date "Org-mode dummy" "")
4431 (error
4432 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4434 (defun org-add-to-diary-list (&rest args)
4435 (if (fboundp 'diary-add-to-list)
4436 (apply 'diary-add-to-list args)
4437 (apply 'add-to-diary-list args)))
4439 (defvar org-diary-last-run-time nil)
4441 ;;;###autoload
4442 (defun org-diary (&rest args)
4443 "Return diary information from org-files.
4444 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4445 It accesses org files and extracts information from those files to be
4446 listed in the diary. The function accepts arguments specifying what
4447 items should be listed. For a list of arguments allowed here, see the
4448 variable `org-agenda-entry-types'.
4450 The call in the diary file should look like this:
4452 &%%(org-diary) ~/path/to/some/orgfile.org
4454 Use a separate line for each org file to check. Or, if you omit the file name,
4455 all files listed in `org-agenda-files' will be checked automatically:
4457 &%%(org-diary)
4459 If you don't give any arguments (as in the example above), the default
4460 arguments (:deadline :scheduled :timestamp :sexp) are used.
4461 So the example above may also be written as
4463 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4465 The function expects the lisp variables `entry' and `date' to be provided
4466 by the caller, because this is how the calendar works. Don't use this
4467 function from a program - use `org-agenda-get-day-entries' instead."
4468 (when (> (- (org-float-time)
4469 org-agenda-last-marker-time)
4471 (org-agenda-reset-markers))
4472 (org-compile-prefix-format 'agenda)
4473 (org-set-sorting-strategy 'agenda)
4474 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4475 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4476 (list entry)
4477 (org-agenda-files t)))
4478 (time (org-float-time))
4479 file rtn results)
4480 (when (or (not org-diary-last-run-time)
4481 (> (- time
4482 org-diary-last-run-time)
4484 (org-prepare-agenda-buffers files))
4485 (setq org-diary-last-run-time time)
4486 ;; If this is called during org-agenda, don't return any entries to
4487 ;; the calendar. Org Agenda will list these entries itself.
4488 (if org-disable-agenda-to-diary (setq files nil))
4489 (while (setq file (pop files))
4490 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4491 (setq results (append results rtn)))
4492 (if results
4493 (concat (org-finalize-agenda-entries results) "\n"))))
4495 ;;; Agenda entry finders
4497 (defun org-agenda-get-day-entries (file date &rest args)
4498 "Does the work for `org-diary' and `org-agenda'.
4499 FILE is the path to a file to be checked for entries. DATE is date like
4500 the one returned by `calendar-current-date'. ARGS are symbols indicating
4501 which kind of entries should be extracted. For details about these, see
4502 the documentation of `org-diary'."
4503 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4504 (let* ((org-startup-folded nil)
4505 (org-startup-align-all-tables nil)
4506 (buffer (if (file-exists-p file)
4507 (org-get-agenda-file-buffer file)
4508 (error "No such file %s" file)))
4509 arg results rtn deadline-results)
4510 (if (not buffer)
4511 ;; If file does not exist, make sure an error message ends up in diary
4512 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4513 (with-current-buffer buffer
4514 (unless (org-mode-p)
4515 (error "Agenda file %s is not in `org-mode'" file))
4516 (let ((case-fold-search nil))
4517 (save-excursion
4518 (save-restriction
4519 (if org-agenda-restrict
4520 (narrow-to-region org-agenda-restrict-begin
4521 org-agenda-restrict-end)
4522 (widen))
4523 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4524 (while (setq arg (pop args))
4525 (cond
4526 ((and (eq arg :todo)
4527 (equal date (calendar-gregorian-from-absolute
4528 (org-today))))
4529 (setq rtn (org-agenda-get-todos))
4530 (setq results (append results rtn)))
4531 ((eq arg :timestamp)
4532 (setq rtn (org-agenda-get-blocks))
4533 (setq results (append results rtn))
4534 (setq rtn (org-agenda-get-timestamps))
4535 (setq results (append results rtn)))
4536 ((eq arg :sexp)
4537 (setq rtn (org-agenda-get-sexps))
4538 (setq results (append results rtn)))
4539 ((eq arg :scheduled)
4540 (setq rtn (org-agenda-get-scheduled deadline-results))
4541 (setq results (append results rtn)))
4542 ((eq arg :closed)
4543 (setq rtn (org-agenda-get-progress))
4544 (setq results (append results rtn)))
4545 ((eq arg :deadline)
4546 (setq rtn (org-agenda-get-deadlines))
4547 (setq deadline-results (copy-sequence rtn))
4548 (setq results (append results rtn))))))))
4549 results))))
4551 (defun org-agenda-get-todos ()
4552 "Return the TODO information for agenda display."
4553 (let* ((props (list 'face nil
4554 'done-face 'org-agenda-done
4555 'org-not-done-regexp org-not-done-regexp
4556 'org-todo-regexp org-todo-regexp
4557 'org-complex-heading-regexp org-complex-heading-regexp
4558 'mouse-face 'highlight
4559 'help-echo
4560 (format "mouse-2 or RET jump to org file %s"
4561 (abbreviate-file-name buffer-file-name))))
4562 (regexp (concat "^\\*+[ \t]+\\("
4563 (if org-select-this-todo-keyword
4564 (if (equal org-select-this-todo-keyword "*")
4565 org-todo-regexp
4566 (concat "\\<\\("
4567 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4568 "\\)\\>"))
4569 org-not-done-regexp)
4570 "[^\n\r]*\\)"))
4571 marker priority category tags todo-state
4572 ee txt beg end)
4573 (goto-char (point-min))
4574 (while (re-search-forward regexp nil t)
4575 (catch :skip
4576 (save-match-data
4577 (beginning-of-line)
4578 (org-agenda-skip)
4579 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4580 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4581 (goto-char (1+ beg))
4582 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4583 (throw :skip nil)))
4584 (goto-char (match-beginning 1))
4585 (setq marker (org-agenda-new-marker (match-beginning 0))
4586 category (org-get-category)
4587 txt (match-string 1)
4588 tags (org-get-tags-at (point))
4589 txt (org-format-agenda-item "" txt category tags)
4590 priority (1+ (org-get-priority txt))
4591 todo-state (org-get-todo-state))
4592 (org-add-props txt props
4593 'org-marker marker 'org-hd-marker marker
4594 'priority priority 'org-category category
4595 'type "todo" 'todo-state todo-state)
4596 (push txt ee)
4597 (if org-agenda-todo-list-sublevels
4598 (goto-char (match-end 1))
4599 (org-end-of-subtree 'invisible))))
4600 (nreverse ee)))
4602 (defun org-agenda-todo-custom-ignore-p (time n)
4603 "Check whether timestamp is farther away then n number of days.
4604 This function is invoked if `org-agenda-todo-ignore-deadlines',
4605 `org-agenda-todo-ignore-scheduled' or
4606 `org-agenda-todo-ignore-timestamp' is set to an integer."
4607 (let ((days (org-days-to-time time)))
4608 (if (>= n 0)
4609 (>= days n)
4610 (<= days n))))
4612 ;;;###autoload
4613 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4614 (&optional end)
4615 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4616 (when (or org-agenda-todo-ignore-with-date
4617 org-agenda-todo-ignore-scheduled
4618 org-agenda-todo-ignore-deadlines
4619 org-agenda-todo-ignore-timestamp)
4620 (setq end (or end (save-excursion (outline-next-heading) (point))))
4621 (save-excursion
4622 (or (and org-agenda-todo-ignore-with-date
4623 (re-search-forward org-ts-regexp end t))
4624 (and org-agenda-todo-ignore-scheduled
4625 (re-search-forward org-scheduled-time-regexp end t)
4626 (cond
4627 ((eq org-agenda-todo-ignore-scheduled 'future)
4628 (> (org-days-to-time (match-string 1)) 0))
4629 ((eq org-agenda-todo-ignore-scheduled 'past)
4630 (<= (org-days-to-time (match-string 1)) 0))
4631 ((numberp org-agenda-todo-ignore-scheduled)
4632 (org-agenda-todo-custom-ignore-p
4633 (match-string 1) org-agenda-todo-ignore-scheduled))
4634 (t)))
4635 (and org-agenda-todo-ignore-deadlines
4636 (re-search-forward org-deadline-time-regexp end t)
4637 (cond
4638 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4639 ((eq org-agenda-todo-ignore-deadlines 'far)
4640 (not (org-deadline-close (match-string 1))))
4641 ((eq org-agenda-todo-ignore-deadlines 'future)
4642 (> (org-days-to-time (match-string 1)) 0))
4643 ((eq org-agenda-todo-ignore-deadlines 'past)
4644 (<= (org-days-to-time (match-string 1)) 0))
4645 ((numberp org-agenda-todo-ignore-deadlines)
4646 (org-agenda-todo-custom-ignore-p
4647 (match-string 1) org-agenda-todo-ignore-deadlines))
4648 (t (org-deadline-close (match-string 1)))))
4649 (and org-agenda-todo-ignore-timestamp
4650 (let ((buffer (current-buffer))
4651 (regexp
4652 (concat
4653 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4654 (start (point)))
4655 ;; Copy current buffer into a temporary one
4656 (with-temp-buffer
4657 (insert-buffer-substring buffer start end)
4658 (goto-char (point-min))
4659 ;; Delete SCHEDULED and DEADLINE items
4660 (while (re-search-forward regexp end t)
4661 (delete-region (match-beginning 0) (match-end 0)))
4662 (goto-char (point-min))
4663 ;; No search for timestamp left
4664 (when (re-search-forward org-ts-regexp nil t)
4665 (cond
4666 ((eq org-agenda-todo-ignore-timestamp 'future)
4667 (> (org-days-to-time (match-string 1)) 0))
4668 ((eq org-agenda-todo-ignore-timestamp 'past)
4669 (<= (org-days-to-time (match-string 1)) 0))
4670 ((numberp org-agenda-todo-ignore-timestamp)
4671 (org-agenda-todo-custom-ignore-p
4672 (match-string 1) org-agenda-todo-ignore-timestamp))
4673 (t))))))))))
4675 (defconst org-agenda-no-heading-message
4676 "No heading for this item in buffer or region.")
4678 (defun org-agenda-get-timestamps ()
4679 "Return the date stamp information for agenda display."
4680 (let* ((props (list 'face nil
4681 'org-not-done-regexp org-not-done-regexp
4682 'org-todo-regexp org-todo-regexp
4683 'org-complex-heading-regexp org-complex-heading-regexp
4684 'mouse-face 'highlight
4685 'help-echo
4686 (format "mouse-2 or RET jump to org file %s"
4687 (abbreviate-file-name buffer-file-name))))
4688 (d1 (calendar-absolute-from-gregorian date))
4689 (remove-re
4690 (concat
4691 (regexp-quote
4692 (format-time-string
4693 "<%Y-%m-%d"
4694 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4695 ".*?>"))
4696 (regexp
4697 (concat
4698 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4699 (regexp-quote
4700 (substring
4701 (format-time-string
4702 (car org-time-stamp-formats)
4703 (apply 'encode-time ; DATE bound by calendar
4704 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4705 1 11))
4706 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4707 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4708 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4709 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4710 todo-state end-of-match show-all)
4711 (goto-char (point-min))
4712 (while (setq end-of-match (re-search-forward regexp nil t))
4713 (setq b0 (match-beginning 0)
4714 b3 (match-beginning 3) e3 (match-end 3)
4715 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4716 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4717 (member todo-state
4718 org-agenda-repeating-timestamp-show-all)))
4719 (catch :skip
4720 (and (org-at-date-range-p) (throw :skip nil))
4721 (org-agenda-skip)
4722 (if (and (match-end 1)
4723 (not (= d1 (org-time-string-to-absolute
4724 (match-string 1) d1 nil show-all))))
4725 (throw :skip nil))
4726 (if (and e3
4727 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4728 (throw :skip nil))
4729 (setq tmp (buffer-substring (max (point-min)
4730 (- b0 org-ds-keyword-length))
4732 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4733 inactivep (= (char-after b0) ?\[)
4734 deadlinep (string-match org-deadline-regexp tmp)
4735 scheduledp (string-match org-scheduled-regexp tmp)
4736 closedp (and org-agenda-include-inactive-timestamps
4737 (string-match org-closed-string tmp))
4738 clockp (and org-agenda-include-inactive-timestamps
4739 (or (string-match org-clock-string tmp)
4740 (string-match "]-+\\'" tmp)))
4741 donep (member todo-state org-done-keywords))
4742 (if (or scheduledp deadlinep closedp clockp
4743 (and donep org-agenda-skip-timestamp-if-done))
4744 (throw :skip t))
4745 (if (string-match ">" timestr)
4746 ;; substring should only run to end of time stamp
4747 (setq timestr (substring timestr 0 (match-end 0))))
4748 (setq marker (org-agenda-new-marker b0)
4749 category (org-get-category b0))
4750 (save-excursion
4751 (if (not (re-search-backward "^\\*+ " nil t))
4752 (setq txt org-agenda-no-heading-message)
4753 (goto-char (match-beginning 0))
4754 (setq hdmarker (org-agenda-new-marker)
4755 tags (org-get-tags-at))
4756 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4757 (setq head (or (match-string 1) ""))
4758 (setq txt (org-format-agenda-item
4759 (if inactivep org-agenda-inactive-leader nil)
4760 head category tags timestr
4761 remove-re)))
4762 (setq priority (org-get-priority txt))
4763 (org-add-props txt props
4764 'org-marker marker 'org-hd-marker hdmarker)
4765 (org-add-props txt nil 'priority priority
4766 'org-category category 'date date
4767 'todo-state todo-state
4768 'type "timestamp")
4769 (push txt ee))
4770 (if org-agenda-skip-additional-timestamps-same-entry
4771 (outline-next-heading)
4772 (goto-char end-of-match))))
4773 (nreverse ee)))
4775 (defun org-agenda-get-sexps ()
4776 "Return the sexp information for agenda display."
4777 (require 'diary-lib)
4778 (let* ((props (list 'mouse-face 'highlight
4779 'help-echo
4780 (format "mouse-2 or RET jump to org file %s"
4781 (abbreviate-file-name buffer-file-name))))
4782 (regexp "^&?%%(")
4783 marker category ee txt tags entry result beg b sexp sexp-entry
4784 todo-state)
4785 (goto-char (point-min))
4786 (while (re-search-forward regexp nil t)
4787 (catch :skip
4788 (org-agenda-skip)
4789 (setq beg (match-beginning 0))
4790 (goto-char (1- (match-end 0)))
4791 (setq b (point))
4792 (forward-sexp 1)
4793 (setq sexp (buffer-substring b (point)))
4794 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4795 (org-trim (match-string 1))
4796 ""))
4797 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4798 (when result
4799 (setq marker (org-agenda-new-marker beg)
4800 category (org-get-category beg)
4801 todo-state (org-get-todo-state))
4803 (dolist (r (if (stringp result)
4804 (list result)
4805 result)) ;; we expect a list here
4806 (if (string-match "\\S-" r)
4807 (setq txt r)
4808 (setq txt "SEXP entry returned empty string"))
4810 (setq txt (org-format-agenda-item
4811 "" txt category tags 'time))
4812 (org-add-props txt props 'org-marker marker)
4813 (org-add-props txt nil
4814 'org-category category 'date date 'todo-state todo-state
4815 'type "sexp")
4816 (push txt ee)))))
4817 (nreverse ee)))
4819 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4820 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4821 The order of the first 2 times 3 arguments depends on the variable
4822 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4823 So for American calendars, give this as MONTH DAY YEAR, for European as
4824 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4825 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4826 is any number of ISO weeks in the block period for which the item should
4827 be skipped."
4828 (let* ((date1 (calendar-absolute-from-gregorian
4829 (org-order-calendar-date-args m1 d1 y1)))
4830 (date2 (calendar-absolute-from-gregorian
4831 (org-order-calendar-date-args m2 d2 y2)))
4832 (d (calendar-absolute-from-gregorian date)))
4833 (and
4834 (<= date1 d)
4835 (<= d date2)
4836 (= (calendar-day-of-week date) dayname)
4837 (or (not skip-weeks)
4838 (progn
4839 (require 'cal-iso)
4840 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4841 entry)))
4843 (defalias 'org-get-closed 'org-agenda-get-progress)
4844 (defun org-agenda-get-progress ()
4845 "Return the logged TODO entries for agenda display."
4846 (let* ((props (list 'mouse-face 'highlight
4847 'org-not-done-regexp org-not-done-regexp
4848 'org-todo-regexp org-todo-regexp
4849 'org-complex-heading-regexp org-complex-heading-regexp
4850 'help-echo
4851 (format "mouse-2 or RET jump to org file %s"
4852 (abbreviate-file-name buffer-file-name))))
4853 (items (if (consp org-agenda-show-log)
4854 org-agenda-show-log
4855 (if (eq org-agenda-show-log 'clockcheck)
4856 '(clock)
4857 org-agenda-log-mode-items)))
4858 (parts
4859 (delq nil
4860 (list
4861 (if (memq 'closed items) (concat "\\<" org-closed-string))
4862 (if (memq 'clock items) (concat "\\<" org-clock-string))
4863 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4864 (parts-re (if parts (mapconcat 'identity parts "\\|")
4865 (error "`org-agenda-log-mode-items' is empty")))
4866 (regexp (concat
4867 "\\(" parts-re "\\)"
4868 " *\\["
4869 (regexp-quote
4870 (substring
4871 (format-time-string
4872 (car org-time-stamp-formats)
4873 (apply 'encode-time ; DATE bound by calendar
4874 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4875 1 11))))
4876 (org-agenda-search-headline-for-time nil)
4877 marker hdmarker priority category tags closedp statep clockp state
4878 ee txt extra timestr rest clocked)
4879 (goto-char (point-min))
4880 (while (re-search-forward regexp nil t)
4881 (catch :skip
4882 (org-agenda-skip)
4883 (setq marker (org-agenda-new-marker (match-beginning 0))
4884 closedp (equal (match-string 1) org-closed-string)
4885 statep (equal (string-to-char (match-string 1)) ?-)
4886 clockp (not (or closedp statep))
4887 state (and statep (match-string 2))
4888 category (org-get-category (match-beginning 0))
4889 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4891 (when (string-match "\\]" timestr)
4892 ;; substring should only run to end of time stamp
4893 (setq rest (substring timestr (match-end 0))
4894 timestr (substring timestr 0 (match-end 0)))
4895 (if (and (not closedp) (not statep)
4896 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4897 (progn (setq timestr (concat (substring timestr 0 -1)
4898 "-" (match-string 1 rest) "]"))
4899 (setq clocked (match-string 2 rest)))
4900 (setq clocked "-")))
4901 (save-excursion
4902 (setq extra
4903 (cond
4904 ((not org-agenda-log-mode-add-notes) nil)
4905 (statep
4906 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4907 (match-string 1)))
4908 (clockp
4909 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4910 (match-string 1)))))
4911 (if (not (re-search-backward "^\\*+ " nil t))
4912 (setq txt org-agenda-no-heading-message)
4913 (goto-char (match-beginning 0))
4914 (setq hdmarker (org-agenda-new-marker)
4915 tags (org-get-tags-at))
4916 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4917 (setq txt (match-string 1))
4918 (when extra
4919 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4920 (setq txt (concat (substring txt 0 (match-beginning 1))
4921 " - " extra " " (match-string 2 txt)))
4922 (setq txt (concat txt " - " extra))))
4923 (setq txt (org-format-agenda-item
4924 (cond
4925 (closedp "Closed: ")
4926 (statep (concat "State: (" state ")"))
4927 (t (concat "Clocked: (" clocked ")")))
4928 txt category tags timestr)))
4929 (setq priority 100000)
4930 (org-add-props txt props
4931 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4932 'priority priority 'org-category category
4933 'type "closed" 'date date
4934 'undone-face 'org-warning 'done-face 'org-agenda-done)
4935 (push txt ee))
4936 (goto-char (point-at-eol))))
4937 (nreverse ee)))
4939 (defun org-agenda-show-clocking-issues ()
4940 "Add overlays, showing issues with clocking.
4941 See also the user option `org-agenda-clock-consistency-checks'."
4942 (interactive)
4943 (let* ((pl org-agenda-clock-consistency-checks)
4944 (re (concat "^[ \t]*"
4945 org-clock-string
4946 "[ \t]+"
4947 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
4948 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
4949 (tlstart 0.)
4950 (tlend 0.)
4951 (maxtime (org-hh:mm-string-to-minutes
4952 (or (plist-get pl :max-duration) "24:00")))
4953 (mintime (org-hh:mm-string-to-minutes
4954 (or (plist-get pl :min-duration) 0)))
4955 (maxgap (org-hh:mm-string-to-minutes
4956 ;; default 30:00 means never complain
4957 (or (plist-get pl :max-gap) "30:00")))
4958 (gapok (mapcar 'org-hh:mm-string-to-minutes
4959 (plist-get pl :gap-ok-around)))
4960 (def-face (or (plist-get pl :default-face)
4961 '((:background "DarkRed") (:foreground "white"))))
4962 issue face m te ts dt ov)
4963 (goto-char (point-min))
4964 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
4965 (setq issue nil face def-face)
4966 (catch 'next
4967 (setq m (org-get-at-bol 'org-marker)
4968 te nil ts nil)
4969 (unless (and m (markerp m))
4970 (setq issue "No valid clock line") (throw 'next t))
4971 (org-with-point-at m
4972 (save-excursion
4973 (goto-char (point-at-bol))
4974 (unless (looking-at re)
4975 (error "No valid Clock line")
4976 (throw 'next t))
4977 (unless (match-end 3)
4978 (setq issue "No end time"
4979 face (or (plist-get pl :no-end-time-face) face))
4980 (throw 'next t))
4981 (setq ts (match-string 1)
4982 te (match-string 3)
4983 ts (org-float-time
4984 (apply 'encode-time (org-parse-time-string ts)))
4985 te (org-float-time
4986 (apply 'encode-time (org-parse-time-string te)))
4987 dt (- te ts))))
4988 (cond
4989 ((> dt (* 60 maxtime))
4990 ;; a very long clocking chunk
4991 (setq issue (format "Clocking interval is very long: %s"
4992 (org-minutes-to-hh:mm-string
4993 (floor (/ (float dt) 60.))))
4994 face (or (plist-get pl :long-face) face)))
4995 ((< dt (* 60 mintime))
4996 ;; a very short clocking chunk
4997 (setq issue (format "Clocking interval is very short: %s"
4998 (org-minutes-to-hh:mm-string
4999 (floor (/ (float dt) 60.))))
5000 face (or (plist-get pl :short-face) face)))
5001 ((and (> tlend 0) (< ts tlend))
5002 ;; Two clock entries are overlapping
5003 (setq issue (format "Clocking overlap: %d minutes"
5004 (/ (- tlend ts) 60))
5005 face (or (plist-get pl :overlap-face) face)))
5006 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5007 ;; There is a gap, lets see if we need to report it
5008 (unless (org-agenda-check-clock-gap tlend ts gapok)
5009 (setq issue (format "Clocking gap: %d minutes"
5010 (/ (- ts tlend) 60))
5011 face (or (plist-get pl :gap-face) face))))
5012 (t nil)))
5013 (setq tlend (or te tlend) tlstart (or ts tlstart))
5014 (when issue
5015 ;; OK, there was some issue, add an overlay to show the issue
5016 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5017 (overlay-put ov 'before-string
5018 (concat
5019 (org-add-props
5020 (format "%-43s" (concat " " issue))
5022 'face face)
5023 "\n"))
5024 (overlay-put ov 'evaporate t)))))
5026 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5027 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5028 (catch 'exit
5029 (unless ok-list
5030 ;; there are no OK times for gaps...
5031 (throw 'exit nil))
5032 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5033 ;; This is more than 24 hours, so it is OK.
5034 ;; because we have at least one OK time, that must be in the
5035 ;; 24 hour interval.
5036 (throw 'exit t))
5037 ;; We have a shorter gap.
5038 ;; Now we have to get the minute of the day when these times are
5039 (let* ((t1dec (decode-time (seconds-to-time t1)))
5040 (t2dec (decode-time (seconds-to-time t2)))
5041 ;; compute the minute on the day
5042 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5043 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5044 (when (< min2 min1)
5045 ;; if min2 is smaller than min1, this means it is on the next day.
5046 ;; Wrap it to after midnight.
5047 (setq min2 (+ min2 1440)))
5048 ;; Now check if any of the OK times is in the gap
5049 (mapc (lambda (x)
5050 ;; Wrap the time to after midnight if necessary
5051 (if (< x min1) (setq x (+ x 1440)))
5052 ;; Check if in interval
5053 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5054 ok-list)
5055 ;; Nope, this gap is not OK
5056 nil)))
5058 (defun org-agenda-get-deadlines ()
5059 "Return the deadline information for agenda display."
5060 (let* ((props (list 'mouse-face 'highlight
5061 'org-not-done-regexp org-not-done-regexp
5062 'org-todo-regexp org-todo-regexp
5063 'org-complex-heading-regexp org-complex-heading-regexp
5064 'help-echo
5065 (format "mouse-2 or RET jump to org file %s"
5066 (abbreviate-file-name buffer-file-name))))
5067 (regexp org-deadline-time-regexp)
5068 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5069 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5070 d2 diff dfrac wdays pos pos1 category tags
5071 suppress-prewarning
5072 ee txt head face s todo-state show-all upcomingp donep timestr)
5073 (goto-char (point-min))
5074 (while (re-search-forward regexp nil t)
5075 (setq suppress-prewarning nil)
5076 (catch :skip
5077 (org-agenda-skip)
5078 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5079 (save-match-data
5080 (string-match org-scheduled-time-regexp
5081 (buffer-substring (point-at-bol)
5082 (point-at-eol)))))
5083 (setq suppress-prewarning
5084 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5085 org-agenda-skip-deadline-prewarning-if-scheduled
5086 0)))
5087 (setq s (match-string 1)
5088 txt nil
5089 pos (1- (match-beginning 1))
5090 todo-state (save-match-data (org-get-todo-state))
5091 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5092 (member todo-state
5093 org-agenda-repeating-timestamp-show-all))
5094 d2 (org-time-string-to-absolute
5095 (match-string 1) d1 'past show-all)
5096 diff (- d2 d1)
5097 wdays (if suppress-prewarning
5098 (let ((org-deadline-warning-days suppress-prewarning))
5099 (org-get-wdays s))
5100 (org-get-wdays s))
5101 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5102 upcomingp (and todayp (> diff 0)))
5103 ;; When to show a deadline in the calendar:
5104 ;; If the expiration is within wdays warning time.
5105 ;; Past-due deadlines are only shown on the current date
5106 (if (and (or (and (<= diff wdays)
5107 (and todayp (not org-agenda-only-exact-dates)))
5108 (= diff 0)))
5109 (save-excursion
5110 ;; (setq todo-state (org-get-todo-state))
5111 (setq donep (member todo-state org-done-keywords))
5112 (if (and donep
5113 (or org-agenda-skip-deadline-if-done
5114 (not (= diff 0))))
5115 (setq txt nil)
5116 (setq category (org-get-category))
5117 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5118 (setq txt org-agenda-no-heading-message)
5119 (goto-char (match-end 0))
5120 (setq pos1 (match-beginning 0))
5121 (setq tags (org-get-tags-at pos1))
5122 (setq head (buffer-substring-no-properties
5123 (point)
5124 (progn (skip-chars-forward "^\r\n")
5125 (point))))
5126 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5127 (setq timestr
5128 (concat (substring s (match-beginning 1)) " "))
5129 (setq timestr 'time))
5130 (setq txt (org-format-agenda-item
5131 (if (= diff 0)
5132 (car org-agenda-deadline-leaders)
5133 (if (functionp
5134 (nth 1 org-agenda-deadline-leaders))
5135 (funcall
5136 (nth 1 org-agenda-deadline-leaders)
5137 diff date)
5138 (format (nth 1 org-agenda-deadline-leaders)
5139 diff)))
5140 head category tags
5141 (if (not (= diff 0)) nil timestr)))))
5142 (when txt
5143 (setq face (org-agenda-deadline-face dfrac wdays))
5144 (org-add-props txt props
5145 'org-marker (org-agenda-new-marker pos)
5146 'org-hd-marker (org-agenda-new-marker pos1)
5147 'priority (+ (- diff)
5148 (org-get-priority txt))
5149 'org-category category
5150 'todo-state todo-state
5151 'type (if upcomingp "upcoming-deadline" "deadline")
5152 'date (if upcomingp date d2)
5153 'face (if donep 'org-agenda-done face)
5154 'undone-face face 'done-face 'org-agenda-done)
5155 (push txt ee))))))
5156 (nreverse ee)))
5158 (defun org-agenda-deadline-face (fraction &optional wdays)
5159 "Return the face to displaying a deadline item.
5160 FRACTION is what fraction of the head-warning time has passed."
5161 (if (equal wdays 0) (setq fraction 1.))
5162 (let ((faces org-agenda-deadline-faces) f)
5163 (catch 'exit
5164 (while (setq f (pop faces))
5165 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5167 (defun org-agenda-get-scheduled (&optional deadline-results)
5168 "Return the scheduled information for agenda display."
5169 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5170 'org-todo-regexp org-todo-regexp
5171 'org-complex-heading-regexp org-complex-heading-regexp
5172 'done-face 'org-agenda-done
5173 'mouse-face 'highlight
5174 'help-echo
5175 (format "mouse-2 or RET jump to org file %s"
5176 (abbreviate-file-name buffer-file-name))))
5177 (regexp org-scheduled-time-regexp)
5178 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5179 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5181 (deadline-position-alist
5182 (mapcar (lambda (a) (and (setq mm (get-text-property
5183 0 'org-hd-marker a))
5184 (cons (marker-position mm) a)))
5185 deadline-results))
5186 d2 diff pos pos1 category tags donep
5187 ee txt head pastschedp todo-state face timestr s habitp show-all)
5188 (goto-char (point-min))
5189 (while (re-search-forward regexp nil t)
5190 (catch :skip
5191 (org-agenda-skip)
5192 (setq s (match-string 1)
5193 txt nil
5194 pos (1- (match-beginning 1))
5195 todo-state (save-match-data (org-get-todo-state))
5196 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5197 (member todo-state
5198 org-agenda-repeating-timestamp-show-all))
5199 d2 (org-time-string-to-absolute
5200 (match-string 1) d1 'past show-all)
5201 diff (- d2 d1))
5202 (setq pastschedp (and todayp (< diff 0)))
5203 ;; When to show a scheduled item in the calendar:
5204 ;; If it is on or past the date.
5205 (when (or (and (< diff 0)
5206 (< (abs diff) org-scheduled-past-days)
5207 (and todayp (not org-agenda-only-exact-dates)))
5208 (= diff 0))
5209 (save-excursion
5210 (setq donep (member todo-state org-done-keywords))
5211 (if (and donep
5212 (or org-agenda-skip-scheduled-if-done
5213 (not (= diff 0))
5214 (and (functionp 'org-is-habit-p)
5215 (org-is-habit-p))))
5216 (setq txt nil)
5217 (setq habitp (and (functionp 'org-is-habit-p)
5218 (org-is-habit-p)))
5219 (setq category (org-get-category))
5220 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5221 (setq txt org-agenda-no-heading-message)
5222 (goto-char (match-end 0))
5223 (setq pos1 (match-beginning 0))
5224 (if habitp
5225 (if (or (not org-habit-show-habits)
5226 (and (not todayp)
5227 org-habit-show-habits-only-for-today))
5228 (throw :skip nil))
5229 (if (and
5230 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5231 (and org-agenda-skip-scheduled-if-deadline-is-shown
5232 pastschedp))
5233 (setq mm (assoc pos1 deadline-position-alist)))
5234 (throw :skip nil)))
5235 (setq tags (org-get-tags-at))
5236 (setq head (buffer-substring-no-properties
5237 (point)
5238 (progn (skip-chars-forward "^\r\n") (point))))
5239 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5240 (setq timestr
5241 (concat (substring s (match-beginning 1)) " "))
5242 (setq timestr 'time))
5243 (setq txt (org-format-agenda-item
5244 (if (= diff 0)
5245 (car org-agenda-scheduled-leaders)
5246 (format (nth 1 org-agenda-scheduled-leaders)
5247 (- 1 diff)))
5248 head category tags
5249 (if (not (= diff 0)) nil timestr)
5250 nil habitp))))
5251 (when txt
5252 (setq face
5253 (cond
5254 ((and (not habitp) pastschedp)
5255 'org-scheduled-previously)
5256 (todayp 'org-scheduled-today)
5257 (t 'org-scheduled))
5258 habitp (and habitp (org-habit-parse-todo)))
5259 (org-add-props txt props
5260 'undone-face face
5261 'face (if donep 'org-agenda-done face)
5262 'org-marker (org-agenda-new-marker pos)
5263 'org-hd-marker (org-agenda-new-marker pos1)
5264 'type (if pastschedp "past-scheduled" "scheduled")
5265 'date (if pastschedp d2 date)
5266 'priority (if habitp
5267 (org-habit-get-priority habitp)
5268 (+ 94 (- 5 diff) (org-get-priority txt)))
5269 'org-category category
5270 'org-habit-p habitp
5271 'todo-state todo-state)
5272 (push txt ee))))))
5273 (nreverse ee)))
5275 (defun org-agenda-get-blocks ()
5276 "Return the date-range information for agenda display."
5277 (let* ((props (list 'face nil
5278 'org-not-done-regexp org-not-done-regexp
5279 'org-todo-regexp org-todo-regexp
5280 'org-complex-heading-regexp org-complex-heading-regexp
5281 'mouse-face 'highlight
5282 'help-echo
5283 (format "mouse-2 or RET jump to org file %s"
5284 (abbreviate-file-name buffer-file-name))))
5285 (regexp org-tr-regexp)
5286 (d0 (calendar-absolute-from-gregorian date))
5287 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5288 head donep)
5289 (goto-char (point-min))
5290 (while (re-search-forward regexp nil t)
5291 (catch :skip
5292 (org-agenda-skip)
5293 (setq pos (point))
5294 (let ((start-time (match-string 1))
5295 (end-time (match-string 2)))
5296 (setq s1 (match-string 1)
5297 s2 (match-string 2)
5298 d1 (time-to-days (org-time-string-to-time s1))
5299 d2 (time-to-days (org-time-string-to-time s2)))
5300 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5301 ;; Only allow days between the limits, because the normal
5302 ;; date stamps will catch the limits.
5303 (save-excursion
5304 (setq todo-state (org-get-todo-state))
5305 (setq donep (member todo-state org-done-keywords))
5306 (if (and donep org-agenda-skip-timestamp-if-done)
5307 (throw :skip t))
5308 (setq marker (org-agenda-new-marker (point)))
5309 (setq category (org-get-category))
5310 (if (not (re-search-backward "^\\*+ " nil t))
5311 (setq txt org-agenda-no-heading-message)
5312 (goto-char (match-beginning 0))
5313 (setq hdmarker (org-agenda-new-marker (point)))
5314 (setq tags (org-get-tags-at))
5315 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5316 (setq head (match-string 1))
5317 (let ((remove-re
5318 (if org-agenda-remove-timeranges-from-blocks
5319 (concat
5320 "<" (regexp-quote s1) ".*?>"
5321 "--"
5322 "<" (regexp-quote s2) ".*?>")
5323 nil)))
5324 (setq txt (org-format-agenda-item
5325 (format
5326 (nth (if (= d1 d2) 0 1)
5327 org-agenda-timerange-leaders)
5328 (1+ (- d0 d1)) (1+ (- d2 d1)))
5329 head category tags
5330 (cond ((= d1 d0)
5331 (concat "<" start-time ">"))
5332 ((= d2 d0)
5333 (concat "<" end-time ">"))
5334 (t nil))
5335 remove-re))))
5336 (org-add-props txt props
5337 'org-marker marker 'org-hd-marker hdmarker
5338 'type "block" 'date date
5339 'todo-state todo-state
5340 'priority (org-get-priority txt) 'org-category category)
5341 (push txt ee))))
5342 (goto-char pos)))
5343 ;; Sort the entries by expiration date.
5344 (nreverse ee)))
5346 ;;; Agenda presentation and sorting
5348 (defvar org-prefix-has-time nil
5349 "A flag, set by `org-compile-prefix-format'.
5350 The flag is set if the currently compiled format contains a `%t'.")
5351 (defvar org-prefix-has-tag nil
5352 "A flag, set by `org-compile-prefix-format'.
5353 The flag is set if the currently compiled format contains a `%T'.")
5354 (defvar org-prefix-has-effort nil
5355 "A flag, set by `org-compile-prefix-format'.
5356 The flag is set if the currently compiled format contains a `%e'.")
5357 (defvar org-prefix-category-length nil
5358 "Used by `org-compile-prefix-format' to remember the category field width.")
5359 (defvar org-prefix-category-max-length nil
5360 "Used by `org-compile-prefix-format' to remember the category field width.")
5362 (defun org-agenda-get-category-icon (category)
5363 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5364 (dolist (entry org-agenda-category-icon-alist)
5365 (when (org-string-match-p (car entry) category)
5366 (if (listp (cadr entry))
5367 (return (cadr entry))
5368 (return (apply 'create-image (cdr entry)))))))
5370 (defun org-format-agenda-item (extra txt &optional category tags dotime
5371 remove-re habitp)
5372 "Format TXT to be inserted into the agenda buffer.
5373 In particular, it adds the prefix and corresponding text properties. EXTRA
5374 must be a string and replaces the `%s' specifier in the prefix format.
5375 CATEGORY (string, symbol or nil) may be used to overrule the default
5376 category taken from local variable or file name. It will replace the `%c'
5377 specifier in the format. DOTIME, when non-nil, indicates that a
5378 time-of-day should be extracted from TXT for sorting of this entry, and for
5379 the `%t' specifier in the format. When DOTIME is a string, this string is
5380 searched for a time before TXT is. TAGS can be the tags of the headline.
5381 Any match of REMOVE-RE will be removed from TXT."
5382 (save-match-data
5383 ;; Diary entries sometimes have extra whitespace at the beginning
5384 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5386 ;; Fix the tags part in txt
5387 (setq txt (org-agenda-fix-displayed-tags
5388 txt tags
5389 org-agenda-show-inherited-tags
5390 org-agenda-hide-tags-regexp))
5391 (let* ((category (or category
5392 (if (stringp org-category)
5393 org-category
5394 (and org-category (symbol-name org-category)))
5395 (if buffer-file-name
5396 (file-name-sans-extension
5397 (file-name-nondirectory buffer-file-name))
5398 "")))
5399 (category-icon (org-agenda-get-category-icon category))
5400 (category-icon (if category-icon
5401 (propertize " " 'display category-icon)
5402 ""))
5403 ;; time, tag, effort are needed for the eval of the prefix format
5404 (tag (if tags (nth (1- (length tags)) tags) ""))
5405 time effort neffort
5406 (ts (if dotime (concat
5407 (if (stringp dotime) dotime "")
5408 (and org-agenda-search-headline-for-time txt))))
5409 (time-of-day (and dotime (org-get-time-of-day ts)))
5410 stamp plain s0 s1 s2 rtn srp l
5411 duration thecategory)
5412 (and (org-mode-p) buffer-file-name
5413 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5414 (when (and dotime time-of-day)
5415 ;; Extract starting and ending time and move them to prefix
5416 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5417 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5418 (setq s0 (match-string 0 ts)
5419 srp (and stamp (match-end 3))
5420 s1 (match-string (if plain 1 2) ts)
5421 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5423 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5424 ;; them, we might want to remove them there to avoid duplication.
5425 ;; The user can turn this off with a variable.
5426 (if (and org-prefix-has-time
5427 org-agenda-remove-times-when-in-prefix (or stamp plain)
5428 (string-match (concat (regexp-quote s0) " *") txt)
5429 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5430 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5431 (= (match-beginning 0) 0)
5433 (setq txt (replace-match "" nil nil txt))))
5434 ;; Normalize the time(s) to 24 hour
5435 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5436 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5438 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5439 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5440 (setq s2
5441 (org-minutes-to-hh:mm-string
5442 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5444 ;; Compute the duration
5445 (when s2
5446 (setq duration (- (org-hh:mm-string-to-minutes s2)
5447 (org-hh:mm-string-to-minutes s1)))))
5449 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5450 txt)
5451 ;; Tags are in the string
5452 (if (or (eq org-agenda-remove-tags t)
5453 (and org-agenda-remove-tags
5454 org-prefix-has-tag))
5455 (setq txt (replace-match "" t t txt))
5456 (setq txt (replace-match
5457 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5458 (match-string 2 txt))
5459 t t txt))))
5460 (when (org-mode-p)
5461 (setq effort
5462 (condition-case nil
5463 (org-get-effort
5464 (or (get-text-property 0 'org-hd-marker txt)
5465 (get-text-property 0 'org-marker txt)))
5466 (error nil)))
5467 (when effort
5468 (setq neffort (org-duration-string-to-minutes effort)
5469 effort (setq effort (concat "[" effort "]" )))))
5471 (when remove-re
5472 (while (string-match remove-re txt)
5473 (setq txt (replace-match "" t t txt))))
5475 ;; Set org-heading property on `txt' to mark the start of the
5476 ;; heading.
5477 (add-text-properties 0 (length txt) '(org-heading t) txt)
5479 ;; Prepare the variables needed in the eval of the compiled format
5480 (setq time (cond (s2 (concat
5481 (org-agenda-time-of-day-to-ampm-maybe s1)
5482 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5483 (if org-agenda-timegrid-use-ampm " ")))
5484 (s1 (concat
5485 (org-agenda-time-of-day-to-ampm-maybe s1)
5486 (if org-agenda-timegrid-use-ampm
5487 "........ "
5488 "......")))
5489 (t ""))
5490 extra (or (and (not habitp) extra) "")
5491 category (if (symbolp category) (symbol-name category) category)
5492 thecategory (copy-sequence category))
5493 (if (string-match org-bracket-link-regexp category)
5494 (progn
5495 (setq l (if (match-end 3)
5496 (- (match-end 3) (match-beginning 3))
5497 (- (match-end 1) (match-beginning 1))))
5498 (when (< l (or org-prefix-category-length 0))
5499 (setq category (copy-sequence category))
5500 (org-add-props category nil
5501 'extra-space (make-string
5502 (- org-prefix-category-length l 1) ?\ ))))
5503 (if (and org-prefix-category-max-length
5504 (>= (length category) org-prefix-category-max-length))
5505 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5506 ;; Evaluate the compiled format
5507 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5509 ;; And finally add the text properties
5510 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5511 (org-add-props rtn nil
5512 'org-category (if thecategory (downcase thecategory) category)
5513 'tags (mapcar 'org-downcase-keep-props tags)
5514 'org-highest-priority org-highest-priority
5515 'org-lowest-priority org-lowest-priority
5516 'time-of-day time-of-day
5517 'duration duration
5518 'effort effort
5519 'effort-minutes neffort
5520 'txt txt
5521 'time time
5522 'extra extra
5523 'dotime dotime))))
5525 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5526 "Remove tags string from TXT, and add a modified list of tags.
5527 The modified list may contain inherited tags, and tags matched by
5528 `org-agenda-hide-tags-regexp' will be removed."
5529 (when (or add-inherited hide-re)
5530 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5531 (setq txt (substring txt 0 (match-beginning 0))))
5532 (setq tags
5533 (delq nil
5534 (mapcar (lambda (tg)
5535 (if (or (and hide-re (string-match hide-re tg))
5536 (and (not add-inherited)
5537 (get-text-property 0 'inherited tg)))
5539 tg))
5540 tags)))
5541 (when tags
5542 (let ((have-i (get-text-property 0 'inherited (car tags)))
5544 (setq txt (concat txt " :"
5545 (mapconcat
5546 (lambda (x)
5547 (setq i (get-text-property 0 'inherited x))
5548 (if (and have-i (not i))
5549 (progn
5550 (setq have-i nil)
5551 (concat ":" x))
5553 tags ":")
5554 (if have-i "::" ":"))))))
5555 txt)
5557 (defun org-downcase-keep-props (s)
5558 (let ((props (text-properties-at 0 s)))
5559 (setq s (downcase s))
5560 (add-text-properties 0 (length s) props s)
5563 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5564 (defvar org-agenda-sorting-strategy-selected nil)
5566 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5567 (catch 'exit
5568 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5569 ((and todayp (member 'today (car org-agenda-time-grid))))
5570 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5571 ((member 'weekly (car org-agenda-time-grid)))
5572 (t (throw 'exit list)))
5573 (let* ((have (delq nil (mapcar
5574 (lambda (x) (get-text-property 1 'time-of-day x))
5575 list)))
5576 (string (nth 1 org-agenda-time-grid))
5577 (gridtimes (nth 2 org-agenda-time-grid))
5578 (req (car org-agenda-time-grid))
5579 (remove (member 'remove-match req))
5580 new time)
5581 (if (and (member 'require-timed req) (not have))
5582 ;; don't show empty grid
5583 (throw 'exit list))
5584 (while (setq time (pop gridtimes))
5585 (unless (and remove (member time have))
5586 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5587 (push (org-format-agenda-item
5588 nil string "" nil
5589 (concat (substring time 0 -2) ":" (substring time -2)))
5590 new)
5591 (put-text-property
5592 2 (length (car new)) 'face 'org-time-grid (car new))))
5593 (when (and todayp org-agenda-show-current-time-in-grid)
5594 (push (org-format-agenda-item
5596 org-agenda-current-time-string
5597 "" nil
5598 (format-time-string "%H:%M "))
5599 new)
5600 (put-text-property
5601 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5603 (if (member 'time-up org-agenda-sorting-strategy-selected)
5604 (append new list)
5605 (append list new)))))
5607 (defun org-compile-prefix-format (key)
5608 "Compile the prefix format into a Lisp form that can be evaluated.
5609 The resulting form is returned and stored in the variable
5610 `org-prefix-format-compiled'."
5611 (setq org-prefix-has-time nil org-prefix-has-tag nil
5612 org-prefix-category-length nil org-prefix-has-effort nil)
5613 (let ((s (cond
5614 ((stringp org-agenda-prefix-format)
5615 org-agenda-prefix-format)
5616 ((assq key org-agenda-prefix-format)
5617 (cdr (assq key org-agenda-prefix-format)))
5618 (t " %-12:c%?-12t% s")))
5619 (start 0)
5620 varform vars var e c f opt)
5621 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5622 s start)
5623 (setq var (or (cdr (assoc (match-string 4 s)
5624 '(("c" . category) ("t" . time) ("s" . extra)
5625 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5626 'eval)
5627 c (or (match-string 3 s) "")
5628 opt (match-beginning 1)
5629 start (1+ (match-beginning 0)))
5630 (if (equal var 'time) (setq org-prefix-has-time t))
5631 (if (equal var 'tag) (setq org-prefix-has-tag t))
5632 (if (equal var 'effort) (setq org-prefix-has-effort t))
5633 (setq f (concat "%" (match-string 2 s) "s"))
5634 (when (equal var 'category)
5635 (setq org-prefix-category-length
5636 (floor (abs (string-to-number (match-string 2 s)))))
5637 (setq org-prefix-category-max-length
5638 (let ((x (match-string 2 s)))
5639 (save-match-data
5640 (if (string-match "\\.[0-9]+" x)
5641 (string-to-number (substring (match-string 0 x) 1)))))))
5642 (if (eq var 'eval)
5643 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5644 (if opt
5645 (setq varform
5646 `(if (equal "" ,var)
5648 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5649 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5650 (setq s (replace-match "%s" t nil s))
5651 (push varform vars))
5652 (setq vars (nreverse vars))
5653 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5655 (defun org-set-sorting-strategy (key)
5656 (if (symbolp (car org-agenda-sorting-strategy))
5657 ;; the old format
5658 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5659 (setq org-agenda-sorting-strategy-selected
5660 (or (cdr (assq key org-agenda-sorting-strategy))
5661 (cdr (assq 'agenda org-agenda-sorting-strategy))
5662 '(time-up category-keep priority-down)))))
5664 (defun org-get-time-of-day (s &optional string mod24)
5665 "Check string S for a time of day.
5666 If found, return it as a military time number between 0 and 2400.
5667 If not found, return nil.
5668 The optional STRING argument forces conversion into a 5 character wide string
5669 HH:MM."
5670 (save-match-data
5671 (when
5672 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5673 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5674 (let* ((h (string-to-number (match-string 1 s)))
5675 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5676 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5677 (am-p (equal ampm "am"))
5678 (h1 (cond ((not ampm) h)
5679 ((= h 12) (if am-p 0 12))
5680 (t (+ h (if am-p 0 12)))))
5681 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5682 (mod h1 24) h1))
5683 (t0 (+ (* 100 h2) m))
5684 (t1 (concat (if (>= h1 24) "+" " ")
5685 (if (and org-agenda-time-leading-zero
5686 (< t0 1000)) "0" "")
5687 (if (< t0 100) "0" "")
5688 (if (< t0 10) "0" "")
5689 (int-to-string t0))))
5690 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5692 (defvar org-agenda-before-sorting-filter-function nil
5693 "Function to be applied to agenda items prior to sorting.
5694 Prior to sorting also means just before they are inserted into the agenda.
5696 To aid sorting, you may revisit the original entries and add more text
5697 properties which will later be used by the sorting functions.
5699 The function should take a string argument, an agenda line.
5700 It has access to the text properties in that line, which contain among
5701 other things, the property `org-hd-marker' that points to the entry
5702 where the line comes from. Note that not all lines going into the agenda
5703 have this property, only most.
5705 The function should return the modified string. It is probably best
5706 to ONLY change text properties.
5708 You can also use this function as a filter, by returning nil for lines
5709 you don't want to have in the agenda at all. For this application, you
5710 could bind the variable in the options section of a custom command.")
5712 (defun org-finalize-agenda-entries (list &optional nosort)
5713 "Sort and concatenate the agenda items."
5714 (setq list (mapcar 'org-agenda-highlight-todo list))
5715 (if nosort
5716 list
5717 (when org-agenda-before-sorting-filter-function
5718 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5719 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5721 (defun org-agenda-highlight-todo (x)
5722 (let ((org-done-keywords org-done-keywords-for-agenda)
5723 (case-fold-search nil)
5725 (if (eq x 'line)
5726 (save-excursion
5727 (beginning-of-line 1)
5728 (setq re (org-get-at-bol 'org-todo-regexp))
5729 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5730 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5731 (add-text-properties (match-beginning 0) (match-end 1)
5732 (list 'face (org-get-todo-face 1)))
5733 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5734 (delete-region (match-beginning 1) (1- (match-end 0)))
5735 (goto-char (match-beginning 1))
5736 (insert (format org-agenda-todo-keyword-format s)))))
5737 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5738 (setq re (get-text-property 0 'org-todo-regexp x))
5739 (when (and re
5740 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5741 x (or pl 0)) pl))
5742 (add-text-properties
5743 (or (match-end 1) (match-end 0)) (match-end 0)
5744 (list 'face (org-get-todo-face (match-string 2 x)))
5746 (when (match-end 1)
5747 (setq x (concat (substring x 0 (match-end 1))
5748 (format org-agenda-todo-keyword-format
5749 (match-string 2 x))
5750 (org-add-props " " (text-properties-at 0 x))
5751 (substring x (match-end 3)))))))
5752 x)))
5754 (defsubst org-cmp-priority (a b)
5755 "Compare the priorities of string A and B."
5756 (let ((pa (or (get-text-property 1 'priority a) 0))
5757 (pb (or (get-text-property 1 'priority b) 0)))
5758 (cond ((> pa pb) +1)
5759 ((< pa pb) -1)
5760 (t nil))))
5762 (defsubst org-cmp-effort (a b)
5763 "Compare the priorities of string A and B."
5764 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5765 (ea (or (get-text-property 1 'effort-minutes a) def))
5766 (eb (or (get-text-property 1 'effort-minutes b) def)))
5767 (cond ((> ea eb) +1)
5768 ((< ea eb) -1)
5769 (t nil))))
5771 (defsubst org-cmp-category (a b)
5772 "Compare the string values of categories of strings A and B."
5773 (let ((ca (or (get-text-property 1 'org-category a) ""))
5774 (cb (or (get-text-property 1 'org-category b) "")))
5775 (cond ((string-lessp ca cb) -1)
5776 ((string-lessp cb ca) +1)
5777 (t nil))))
5779 (defsubst org-cmp-todo-state (a b)
5780 "Compare the todo states of strings A and B."
5781 (let* ((ma (or (get-text-property 1 'org-marker a)
5782 (get-text-property 1 'org-hd-marker a)))
5783 (mb (or (get-text-property 1 'org-marker b)
5784 (get-text-property 1 'org-hd-marker b)))
5785 (fa (and ma (marker-buffer ma)))
5786 (fb (and mb (marker-buffer mb)))
5787 (todo-kwds
5788 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5789 (and fb (with-current-buffer fb org-todo-keywords-1))))
5790 (ta (or (get-text-property 1 'todo-state a) ""))
5791 (tb (or (get-text-property 1 'todo-state b) ""))
5792 (la (- (length (member ta todo-kwds))))
5793 (lb (- (length (member tb todo-kwds))))
5794 (donepa (member ta org-done-keywords-for-agenda))
5795 (donepb (member tb org-done-keywords-for-agenda)))
5796 (cond ((and donepa (not donepb)) -1)
5797 ((and (not donepa) donepb) +1)
5798 ((< la lb) -1)
5799 ((< lb la) +1)
5800 (t nil))))
5802 (defsubst org-cmp-alpha (a b)
5803 "Compare the headlines, alphabetically."
5804 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5805 (plb (text-property-any 0 (length b) 'org-heading t b))
5806 (ta (and pla (substring a pla)))
5807 (tb (and plb (substring b plb))))
5808 (when pla
5809 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5810 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5811 (setq ta (substring ta (match-end 0))))
5812 (setq ta (downcase ta)))
5813 (when plb
5814 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5815 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5816 (setq tb (substring tb (match-end 0))))
5817 (setq tb (downcase tb)))
5818 (cond ((not ta) +1)
5819 ((not tb) -1)
5820 ((string-lessp ta tb) -1)
5821 ((string-lessp tb ta) +1)
5822 (t nil))))
5824 (defsubst org-cmp-tag (a b)
5825 "Compare the string values of the first tags of A and B."
5826 (let ((ta (car (last (get-text-property 1 'tags a))))
5827 (tb (car (last (get-text-property 1 'tags b)))))
5828 (cond ((not ta) +1)
5829 ((not tb) -1)
5830 ((string-lessp ta tb) -1)
5831 ((string-lessp tb ta) +1)
5832 (t nil))))
5834 (defsubst org-cmp-time (a b)
5835 "Compare the time-of-day values of strings A and B."
5836 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5837 (ta (or (get-text-property 1 'time-of-day a) def))
5838 (tb (or (get-text-property 1 'time-of-day b) def)))
5839 (cond ((< ta tb) -1)
5840 ((< tb ta) +1)
5841 (t nil))))
5843 (defsubst org-cmp-habit-p (a b)
5844 "Compare the todo states of strings A and B."
5845 (let ((ha (get-text-property 1 'org-habit-p a))
5846 (hb (get-text-property 1 'org-habit-p b)))
5847 (cond ((and ha (not hb)) -1)
5848 ((and (not ha) hb) +1)
5849 (t nil))))
5851 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5853 (defun org-entries-lessp (a b)
5854 "Predicate for sorting agenda entries."
5855 ;; The following variables will be used when the form is evaluated.
5856 ;; So even though the compiler complains, keep them.
5857 (let* ((ss org-agenda-sorting-strategy-selected)
5858 (time-up (and (org-em 'time-up 'time-down ss)
5859 (org-cmp-time a b)))
5860 (time-down (if time-up (- time-up) nil))
5861 (priority-up (and (org-em 'priority-up 'priority-down ss)
5862 (org-cmp-priority a b)))
5863 (priority-down (if priority-up (- priority-up) nil))
5864 (effort-up (and (org-em 'effort-up 'effort-down ss)
5865 (org-cmp-effort a b)))
5866 (effort-down (if effort-up (- effort-up) nil))
5867 (category-up (and (or (org-em 'category-up 'category-down ss)
5868 (memq 'category-keep ss))
5869 (org-cmp-category a b)))
5870 (category-down (if category-up (- category-up) nil))
5871 (category-keep (if category-up +1 nil))
5872 (tag-up (and (org-em 'tag-up 'tag-down ss)
5873 (org-cmp-tag a b)))
5874 (tag-down (if tag-up (- tag-up) nil))
5875 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5876 (org-cmp-todo-state a b)))
5877 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5878 (habit-up (and (org-em 'habit-up 'habit-down ss)
5879 (org-cmp-habit-p a b)))
5880 (habit-down (if habit-up (- habit-up) nil))
5881 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5882 (org-cmp-alpha a b)))
5883 (alpha-down (if alpha-up (- alpha-up) nil))
5884 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5885 user-defined-up user-defined-down)
5886 (if (and need-user-cmp org-agenda-cmp-user-defined
5887 (functionp org-agenda-cmp-user-defined))
5888 (setq user-defined-up
5889 (funcall org-agenda-cmp-user-defined a b)
5890 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5891 (cdr (assoc
5892 (eval (cons 'or org-agenda-sorting-strategy-selected))
5893 '((-1 . t) (1 . nil) (nil . nil))))))
5895 ;;; Agenda restriction lock
5897 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5898 "Overlay to mark the headline to which agenda commands are restricted.")
5899 (overlay-put org-agenda-restriction-lock-overlay
5900 'face 'org-agenda-restriction-lock)
5901 (overlay-put org-agenda-restriction-lock-overlay
5902 'help-echo "Agendas are currently limited to this subtree.")
5903 (org-detach-overlay org-agenda-restriction-lock-overlay)
5905 (defun org-agenda-set-restriction-lock (&optional type)
5906 "Set restriction lock for agenda, to current subtree or file.
5907 Restriction will be the file if TYPE is `file', or if type is the
5908 universal prefix '(4), or if the cursor is before the first headline
5909 in the file. Otherwise, restriction will be to the current subtree."
5910 (interactive "P")
5911 (and (equal type '(4)) (setq type 'file))
5912 (setq type (cond
5913 (type type)
5914 ((org-at-heading-p) 'subtree)
5915 ((condition-case nil (org-back-to-heading t) (error nil))
5916 'subtree)
5917 (t 'file)))
5918 (if (eq type 'subtree)
5919 (progn
5920 (setq org-agenda-restrict t)
5921 (setq org-agenda-overriding-restriction 'subtree)
5922 (put 'org-agenda-files 'org-restrict
5923 (list (buffer-file-name (buffer-base-buffer))))
5924 (org-back-to-heading t)
5925 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5926 (move-marker org-agenda-restrict-begin (point))
5927 (move-marker org-agenda-restrict-end
5928 (save-excursion (org-end-of-subtree t)))
5929 (message "Locking agenda restriction to subtree"))
5930 (put 'org-agenda-files 'org-restrict
5931 (list (buffer-file-name (buffer-base-buffer))))
5932 (setq org-agenda-restrict nil)
5933 (setq org-agenda-overriding-restriction 'file)
5934 (move-marker org-agenda-restrict-begin nil)
5935 (move-marker org-agenda-restrict-end nil)
5936 (message "Locking agenda restriction to file"))
5937 (setq current-prefix-arg nil)
5938 (org-agenda-maybe-redo))
5940 (defun org-agenda-remove-restriction-lock (&optional noupdate)
5941 "Remove the agenda restriction lock."
5942 (interactive "P")
5943 (org-detach-overlay org-agenda-restriction-lock-overlay)
5944 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5945 (setq org-agenda-overriding-restriction nil)
5946 (setq org-agenda-restrict nil)
5947 (put 'org-agenda-files 'org-restrict nil)
5948 (move-marker org-agenda-restrict-begin nil)
5949 (move-marker org-agenda-restrict-end nil)
5950 (setq current-prefix-arg nil)
5951 (message "Agenda restriction lock removed")
5952 (or noupdate (org-agenda-maybe-redo)))
5954 (defun org-agenda-maybe-redo ()
5955 "If there is any window showing the agenda view, update it."
5956 (let ((w (get-buffer-window org-agenda-buffer-name t))
5957 (w0 (selected-window)))
5958 (when w
5959 (select-window w)
5960 (org-agenda-redo)
5961 (select-window w0)
5962 (if org-agenda-overriding-restriction
5963 (message "Agenda view shifted to new %s restriction"
5964 org-agenda-overriding-restriction)
5965 (message "Agenda restriction lock removed")))))
5967 ;;; Agenda commands
5969 (defun org-agenda-check-type (error &rest types)
5970 "Check if agenda buffer is of allowed type.
5971 If ERROR is non-nil, throw an error, otherwise just return nil."
5972 (if (memq org-agenda-type types)
5974 (if error
5975 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5976 nil)))
5978 (defun org-agenda-quit ()
5979 "Exit agenda by removing the window or the buffer."
5980 (interactive)
5981 (if org-agenda-columns-active
5982 (org-columns-quit)
5983 (let ((buf (current-buffer)))
5984 (if (eq org-agenda-window-setup 'other-frame)
5985 (progn
5986 (kill-buffer buf)
5987 (org-agenda-reset-markers)
5988 (org-columns-remove-overlays)
5989 (setq org-agenda-archives-mode nil)
5990 (delete-frame))
5991 (and (not (eq org-agenda-window-setup 'current-window))
5992 (not (one-window-p))
5993 (delete-window))
5994 (kill-buffer buf)
5995 (org-agenda-reset-markers)
5996 (org-columns-remove-overlays)
5997 (setq org-agenda-archives-mode nil)))
5998 ;; Maybe restore the pre-agenda window configuration.
5999 (and org-agenda-restore-windows-after-quit
6000 (not (eq org-agenda-window-setup 'other-frame))
6001 org-pre-agenda-window-conf
6002 (set-window-configuration org-pre-agenda-window-conf))))
6004 (defun org-agenda-exit ()
6005 "Exit agenda by removing the window or the buffer.
6006 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6007 Org-mode buffers visited directly by the user will not be touched."
6008 (interactive)
6009 (org-release-buffers org-agenda-new-buffers)
6010 (setq org-agenda-new-buffers nil)
6011 (org-agenda-quit))
6013 (defun org-agenda-execute (arg)
6014 "Execute another agenda command, keeping same window.
6015 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6016 in the agenda."
6017 (interactive "P")
6018 (let ((org-agenda-window-setup 'current-window))
6019 (org-agenda arg)))
6021 (defun org-agenda-redo ()
6022 "Rebuild Agenda.
6023 When this is the global TODO list, a prefix argument will be interpreted."
6024 (interactive)
6025 (let* ((org-agenda-keep-modes t)
6026 (filter org-agenda-filter)
6027 (preset (get 'org-agenda-filter :preset-filter))
6028 (org-agenda-filter-while-redo (or filter preset))
6029 (cols org-agenda-columns-active)
6030 (line (org-current-line))
6031 (window-line (- line (org-current-line (window-start))))
6032 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6033 (put 'org-agenda-filter :preset-filter nil)
6034 (and cols (org-columns-quit))
6035 (message "Rebuilding agenda buffer...")
6036 (org-let lprops '(eval org-agenda-redo-command))
6037 (setq org-agenda-undo-list nil
6038 org-agenda-pending-undo-list nil)
6039 (message "Rebuilding agenda buffer...done")
6040 (put 'org-agenda-filter :preset-filter preset)
6041 (and (or filter preset) (org-agenda-filter-apply filter))
6042 (and cols (interactive-p) (org-agenda-columns))
6043 (org-goto-line line)
6044 (recenter window-line)))
6047 (defvar org-global-tags-completion-table nil)
6048 (defvar org-agenda-filter-form nil)
6049 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6050 "Keep only those lines in the agenda buffer that have a specific tag.
6051 The tag is selected with its fast selection letter, as configured.
6052 With prefix argument STRIP, remove all lines that do have the tag.
6053 A lisp caller can specify CHAR. NARROW means that the new tag should be
6054 used to narrow the search - the interactive user can also press `-' or `+'
6055 to switch to narrowing."
6056 (interactive "P")
6057 (let* ((alist org-tag-alist-for-agenda)
6058 (tag-chars (mapconcat
6059 (lambda (x) (if (and (not (symbolp (car x)))
6060 (cdr x))
6061 (char-to-string (cdr x))
6062 ""))
6063 alist ""))
6064 (efforts (org-split-string
6065 (or (cdr (assoc (concat org-effort-property "_ALL")
6066 org-global-properties))
6067 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6068 "")))
6069 (effort-op org-agenda-filter-effort-default-operator)
6070 (effort-prompt "")
6071 (inhibit-read-only t)
6072 (current org-agenda-filter)
6073 maybe-refresh a n tag)
6074 (unless char
6075 (message
6076 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6077 (if narrow "Narrow" "Filter") tag-chars
6078 (if org-agenda-auto-exclude-function "[RET], " ""))
6079 (setq char (read-char)))
6080 (when (member char '(?+ ?-))
6081 ;; Narrowing down
6082 (cond ((equal char ?-) (setq strip t narrow t))
6083 ((equal char ?+) (setq strip nil narrow t)))
6084 (message
6085 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6086 (setq char (read-char)))
6087 (when (member char '(?< ?> ?= ??))
6088 ;; An effort operator
6089 (setq effort-op (char-to-string char))
6090 (setq alist nil) ; to make sure it will be interpreted as effort.
6091 (unless (equal char ??)
6092 (loop for i from 0 to 9 do
6093 (setq effort-prompt
6094 (concat
6095 effort-prompt " ["
6096 (if (= i 9) "0" (int-to-string (1+ i)))
6097 "]" (nth i efforts))))
6098 (message "Effort%s: %s " effort-op effort-prompt)
6099 (setq char (read-char))
6100 (when (or (< char ?0) (> char ?9))
6101 (error "Need 1-9,0 to select effort" ))))
6102 (when (equal char ?\t)
6103 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6104 (org-set-local 'org-global-tags-completion-table
6105 (org-global-tags-completion-table)))
6106 (let ((completion-ignore-case t))
6107 (setq tag (org-icompleting-read
6108 "Tag: " org-global-tags-completion-table))))
6109 (cond
6110 ((equal char ?\r)
6111 (org-agenda-filter-by-tag-show-all)
6112 (when org-agenda-auto-exclude-function
6113 (setq org-agenda-filter '())
6114 (dolist (tag (org-agenda-get-represented-tags))
6115 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6116 (if modifier
6117 (push modifier org-agenda-filter))))
6118 (if (not (null org-agenda-filter))
6119 (org-agenda-filter-apply org-agenda-filter)))
6120 (setq maybe-refresh t))
6121 ((equal char ?/)
6122 (org-agenda-filter-by-tag-show-all)
6123 (when (get 'org-agenda-filter :preset-filter)
6124 (org-agenda-filter-apply org-agenda-filter))
6125 (setq maybe-refresh t))
6126 ((or (equal char ?\ )
6127 (setq a (rassoc char alist))
6128 (and (>= char ?0) (<= char ?9)
6129 (setq n (if (= char ?0) 9 (- char ?0 1))
6130 tag (concat effort-op (nth n efforts))
6131 a (cons tag nil)))
6132 (and (= char ??)
6133 (setq tag "?eff")
6134 a (cons tag nil))
6135 (and tag (setq a (cons tag nil))))
6136 (org-agenda-filter-by-tag-show-all)
6137 (setq tag (car a))
6138 (setq org-agenda-filter
6139 (cons (concat (if strip "-" "+") tag)
6140 (if narrow current nil)))
6141 (org-agenda-filter-apply org-agenda-filter)
6142 (setq maybe-refresh t))
6143 (t (error "Invalid tag selection character %c" char)))
6144 (when (and maybe-refresh
6145 (eq org-agenda-clockreport-mode 'with-filter))
6146 (org-agenda-redo))))
6148 (defun org-agenda-get-represented-tags ()
6149 "Get a list of all tags currently represented in the agenda."
6150 (let (p tags)
6151 (save-excursion
6152 (goto-char (point-min))
6153 (while (setq p (next-single-property-change (point) 'tags))
6154 (goto-char p)
6155 (mapc (lambda (x) (add-to-list 'tags x))
6156 (get-text-property (point) 'tags))))
6157 tags))
6159 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6160 "Refine the current filter. See `org-agenda-filter-by-tag."
6161 (interactive "P")
6162 (org-agenda-filter-by-tag strip char 'refine))
6164 (defun org-agenda-filter-make-matcher ()
6165 "Create the form that tests a line for the agenda filter."
6166 (let (f f1)
6167 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6168 org-agenda-filter))
6169 (if (member x '("-" "+"))
6170 (setq f1 (if (equal x "-") 'tags '(not tags)))
6171 (if (string-match "[<=>?]" x)
6172 (setq f1 (org-agenda-filter-effort-form x))
6173 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6174 (if (equal (string-to-char x) ?-)
6175 (setq f1 (list 'not f1))))
6176 (push f1 f))
6177 (cons 'and (nreverse f))))
6179 (defun org-agenda-filter-effort-form (e)
6180 "Return the form to compare the effort of the current line with what E says.
6181 E looks like \"+<2:25\"."
6182 (let (op)
6183 (setq e (substring e 1))
6184 (setq op (string-to-char e) e (substring e 1))
6185 (setq op (cond ((equal op ?<) '<=)
6186 ((equal op ?>) '>=)
6187 ((equal op ??) op)
6188 (t '=)))
6189 (list 'org-agenda-compare-effort (list 'quote op)
6190 (org-duration-string-to-minutes e))))
6192 (defun org-agenda-compare-effort (op value)
6193 "Compare the effort of the current line with VALUE, using OP.
6194 If the line does not have an effort defined, return nil."
6195 (let ((eff (org-get-at-bol 'effort-minutes)))
6196 (if (equal op ??)
6197 (not eff)
6198 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6199 value))))
6201 (defun org-agenda-filter-apply (filter)
6202 "Set FILTER as the new agenda filter and apply it."
6203 (let (tags)
6204 (setq org-agenda-filter filter
6205 org-agenda-filter-form (org-agenda-filter-make-matcher))
6206 (org-agenda-set-mode-name)
6207 (save-excursion
6208 (goto-char (point-min))
6209 (while (not (eobp))
6210 (if (org-get-at-bol 'org-marker)
6211 (progn
6212 (setq tags (org-get-at-bol 'tags)) ; used in eval
6213 (if (not (eval org-agenda-filter-form))
6214 (org-agenda-filter-by-tag-hide-line))
6215 (beginning-of-line 2))
6216 (beginning-of-line 2))))
6217 (if (get-char-property (point) 'invisible)
6218 (org-agenda-previous-line))))
6220 (defun org-agenda-filter-by-tag-hide-line ()
6221 (let (ov)
6222 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6223 (point-at-eol)))
6224 (overlay-put ov 'invisible t)
6225 (overlay-put ov 'type 'tags-filter)
6226 (push ov org-agenda-filter-overlays)))
6228 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6229 (setq pos (or pos (point)))
6230 (save-excursion
6231 (dolist (ov (overlays-at pos))
6232 (when (and (overlay-get ov 'invisible)
6233 (eq (overlay-get ov 'type) 'tags-filter))
6234 (goto-char pos)
6235 (if (< (overlay-start ov) (point-at-eol))
6236 (move-overlay ov (point-at-eol)
6237 (overlay-end ov)))))))
6239 (defun org-agenda-filter-by-tag-show-all ()
6240 (mapc 'delete-overlay org-agenda-filter-overlays)
6241 (setq org-agenda-filter-overlays nil)
6242 (setq org-agenda-filter nil)
6243 (setq org-agenda-filter-form nil)
6244 (org-agenda-set-mode-name))
6246 (defun org-agenda-manipulate-query-add ()
6247 "Manipulate the query by adding a search term with positive selection.
6248 Positive selection means the term must be matched for selection of an entry."
6249 (interactive)
6250 (org-agenda-manipulate-query ?\[))
6251 (defun org-agenda-manipulate-query-subtract ()
6252 "Manipulate the query by adding a search term with negative selection.
6253 Negative selection means term must not be matched for selection of an entry."
6254 (interactive)
6255 (org-agenda-manipulate-query ?\]))
6256 (defun org-agenda-manipulate-query-add-re ()
6257 "Manipulate the query by adding a search regexp with positive selection.
6258 Positive selection means the regexp must match for selection of an entry."
6259 (interactive)
6260 (org-agenda-manipulate-query ?\{))
6261 (defun org-agenda-manipulate-query-subtract-re ()
6262 "Manipulate the query by adding a search regexp with negative selection.
6263 Negative selection means regexp must not match for selection of an entry."
6264 (interactive)
6265 (org-agenda-manipulate-query ?\}))
6266 (defun org-agenda-manipulate-query (char)
6267 (cond
6268 ((memq org-agenda-type '(timeline agenda))
6269 (let ((org-agenda-include-inactive-timestamps t))
6270 (org-agenda-redo))
6271 (message "Display now includes inactive timestamps as well"))
6272 ((eq org-agenda-type 'search)
6273 (org-add-to-string
6274 'org-agenda-query-string
6275 (if org-agenda-last-search-view-search-was-boolean
6276 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6277 (?\{ . " +{}") (?\} . " -{}"))))
6278 " "))
6279 (setq org-agenda-redo-command
6280 (list 'org-search-view
6281 org-todo-only
6282 org-agenda-query-string
6283 (+ (length org-agenda-query-string)
6284 (if (member char '(?\{ ?\})) 0 1))))
6285 (set-register org-agenda-query-register org-agenda-query-string)
6286 (org-agenda-redo))
6287 (t (error "Cannot manipulate query for %s-type agenda buffers"
6288 org-agenda-type))))
6290 (defun org-add-to-string (var string)
6291 (set var (concat (symbol-value var) string)))
6293 (defun org-agenda-goto-date (date)
6294 "Jump to DATE in agenda."
6295 (interactive (list (let ((org-read-date-prefer-future
6296 (eval org-agenda-jump-prefer-future)))
6297 (org-read-date))))
6298 (org-agenda-list nil date))
6300 (defun org-agenda-goto-today ()
6301 "Go to today."
6302 (interactive)
6303 (org-agenda-check-type t 'timeline 'agenda)
6304 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6305 (cond
6306 (tdpos (goto-char tdpos))
6307 ((eq org-agenda-type 'agenda)
6308 (let* ((sd (org-agenda-compute-starting-span
6309 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6310 (org-agenda-overriding-arguments org-agenda-last-arguments))
6311 (setf (nth 1 org-agenda-overriding-arguments) sd)
6312 (org-agenda-redo)
6313 (org-agenda-find-same-or-today-or-agenda)))
6314 (t (error "Cannot find today")))))
6316 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6317 (goto-char
6318 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6319 (text-property-any (point-min) (point-max) 'org-today t)
6320 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6321 (point-min))))
6323 (defun org-agenda-later (arg)
6324 "Go forward in time by thee current span.
6325 With prefix ARG, go forward that many times the current span."
6326 (interactive "p")
6327 (org-agenda-check-type t 'agenda)
6328 (let* ((span org-agenda-current-span)
6329 (sd org-starting-day)
6330 (greg (calendar-gregorian-from-absolute sd))
6331 (cnt (org-get-at-bol 'org-day-cnt))
6332 greg2)
6333 (cond
6334 ((eq span 'day)
6335 (setq sd (+ arg sd)))
6336 ((eq span 'week)
6337 (setq sd (+ (* 7 arg) sd)))
6338 ((eq span 'month)
6339 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6340 sd (calendar-absolute-from-gregorian greg2))
6341 (setcar greg2 (1+ (car greg2))))
6342 ((eq span 'year)
6343 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6344 sd (calendar-absolute-from-gregorian greg2))
6345 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6347 (setq sd (+ (* span arg) sd))))
6348 (let ((org-agenda-overriding-arguments
6349 (list (car org-agenda-last-arguments) sd span t)))
6350 (org-agenda-redo)
6351 (org-agenda-find-same-or-today-or-agenda cnt))))
6353 (defun org-agenda-earlier (arg)
6354 "Go backward in time by the current span.
6355 With prefix ARG, go backward that many times the current span."
6356 (interactive "p")
6357 (org-agenda-later (- arg)))
6359 (defun org-agenda-view-mode-dispatch ()
6360 "Call one of the view mode commands."
6361 (interactive)
6362 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6363 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6364 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6365 [E]ntryText")
6366 (let ((a (read-char-exclusive)))
6367 (case a
6368 (?\ (call-interactively 'org-agenda-reset-view))
6369 (?d (call-interactively 'org-agenda-day-view))
6370 (?w (call-interactively 'org-agenda-week-view))
6371 (?m (call-interactively 'org-agenda-month-view))
6372 (?y (call-interactively 'org-agenda-year-view))
6373 (?l (call-interactively 'org-agenda-log-mode))
6374 (?L (org-agenda-log-mode '(4)))
6375 (?c (org-agenda-log-mode 'clockcheck))
6376 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6377 (?a (call-interactively 'org-agenda-archives-mode))
6378 (?A (org-agenda-archives-mode 'files))
6379 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6380 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6381 (?G (call-interactively 'org-agenda-toggle-time-grid))
6382 (?D (call-interactively 'org-agenda-toggle-diary))
6383 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6384 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6385 (org-agenda-check-type t 'timeline 'agenda)
6386 (org-agenda-redo))
6387 (message "Display now includes inactive timestamps as well"))
6388 (?q (message "Abort"))
6389 (otherwise (error "Invalid key" )))))
6391 (defun org-agenda-reset-view ()
6392 "Switch to default view for agenda."
6393 (interactive)
6394 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6395 (defun org-agenda-day-view (&optional day-of-year)
6396 "Switch to daily view for agenda.
6397 With argument DAY-OF-YEAR, switch to that day of the year."
6398 (interactive "P")
6399 (org-agenda-change-time-span 'day day-of-year))
6400 (defun org-agenda-week-view (&optional iso-week)
6401 "Switch to daily view for agenda.
6402 With argument ISO-WEEK, switch to the corresponding ISO week.
6403 If ISO-WEEK has more then 2 digits, only the last two encode the
6404 week. Any digits before this encode a year. So 200712 means
6405 week 12 of year 2007. Years in the range 1938-2037 can also be
6406 written as 2-digit years."
6407 (interactive "P")
6408 (org-agenda-change-time-span 'week iso-week))
6409 (defun org-agenda-month-view (&optional month)
6410 "Switch to monthly view for agenda.
6411 With argument MONTH, switch to that month."
6412 (interactive "P")
6413 (org-agenda-change-time-span 'month month))
6414 (defun org-agenda-year-view (&optional year)
6415 "Switch to yearly view for agenda.
6416 With argument YEAR, switch to that year.
6417 If MONTH has more then 2 digits, only the last two encode the
6418 month. Any digits before this encode a year. So 200712 means
6419 December year 2007. Years in the range 1938-2037 can also be
6420 written as 2-digit years."
6421 (interactive "P")
6422 (when year
6423 (setq year (org-small-year-to-year year)))
6424 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6425 (org-agenda-change-time-span 'year year)
6426 (error "Abort")))
6428 (defun org-agenda-change-time-span (span &optional n)
6429 "Change the agenda view to SPAN.
6430 SPAN may be `day', `week', `month', `year'."
6431 (org-agenda-check-type t 'agenda)
6432 (if (and (not n) (equal org-agenda-current-span span))
6433 (error "Viewing span is already \"%s\"" span))
6434 (let* ((sd (or (org-get-at-bol 'day)
6435 org-starting-day))
6436 (sd (org-agenda-compute-starting-span sd span n))
6437 (org-agenda-overriding-arguments
6438 (or org-agenda-overriding-arguments
6439 (list (car org-agenda-last-arguments) sd span t))))
6440 (org-agenda-redo)
6441 (org-agenda-find-same-or-today-or-agenda))
6442 (org-agenda-set-mode-name)
6443 (message "Switched to %s view" span))
6445 (defun org-agenda-compute-starting-span (sd span &optional n)
6446 "Compute starting date for agenda.
6447 SPAN may be `day', `week', `month', `year'. The return value
6448 is a cons cell with the starting date and the number of days,
6449 so that the date SD will be in that range."
6450 (let* ((greg (calendar-gregorian-from-absolute sd))
6451 (dg (nth 1 greg))
6452 (mg (car greg))
6453 (yg (nth 2 greg)))
6454 (cond
6455 ((eq span 'day)
6456 (when n
6457 (setq sd (+ (calendar-absolute-from-gregorian
6458 (list mg 1 yg))
6459 n -1))))
6460 ((eq span 'week)
6461 (let* ((nt (calendar-day-of-week
6462 (calendar-gregorian-from-absolute sd)))
6463 (d (if org-agenda-start-on-weekday
6464 (- nt org-agenda-start-on-weekday)
6467 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6468 (when n
6469 (require 'cal-iso)
6470 (when (> n 99)
6471 (setq y1 (org-small-year-to-year (/ n 100))
6472 n (mod n 100)))
6473 (setq sd
6474 (calendar-absolute-from-iso
6475 (list n 1
6476 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6477 ((eq span 'month)
6478 (let (y1)
6479 (when (and n (> n 99))
6480 (setq y1 (org-small-year-to-year (/ n 100))
6481 n (mod n 100)))
6482 (setq sd (calendar-absolute-from-gregorian
6483 (list (or n mg) 1 (or y1 yg))))))
6484 ((eq span 'year)
6485 (setq sd (calendar-absolute-from-gregorian
6486 (list 1 1 (or n yg))))))
6487 sd))
6489 (defun org-agenda-next-date-line (&optional arg)
6490 "Jump to the next line indicating a date in agenda buffer."
6491 (interactive "p")
6492 (org-agenda-check-type t 'agenda 'timeline)
6493 (beginning-of-line 1)
6494 ;; This does not work if user makes date format that starts with a blank
6495 (if (looking-at "^\\S-") (forward-char 1))
6496 (if (not (re-search-forward "^\\S-" nil t arg))
6497 (progn
6498 (backward-char 1)
6499 (error "No next date after this line in this buffer")))
6500 (goto-char (match-beginning 0)))
6502 (defun org-agenda-previous-date-line (&optional arg)
6503 "Jump to the previous line indicating a date in agenda buffer."
6504 (interactive "p")
6505 (org-agenda-check-type t 'agenda 'timeline)
6506 (beginning-of-line 1)
6507 (if (not (re-search-backward "^\\S-" nil t arg))
6508 (error "No previous date before this line in this buffer")))
6510 ;; Initialize the highlight
6511 (defvar org-hl (make-overlay 1 1))
6512 (overlay-put org-hl 'face 'highlight)
6514 (defun org-highlight (begin end &optional buffer)
6515 "Highlight a region with overlay."
6516 (move-overlay org-hl begin end (or buffer (current-buffer))))
6518 (defun org-unhighlight ()
6519 "Detach overlay INDEX."
6520 (org-detach-overlay org-hl))
6522 ;; FIXME this is currently not used.
6523 (defun org-highlight-until-next-command (beg end &optional buffer)
6524 "Move the highlight overlay to BEG/END, remove it before the next command."
6525 (org-highlight beg end buffer)
6526 (add-hook 'pre-command-hook 'org-unhighlight-once))
6527 (defun org-unhighlight-once ()
6528 "Remove the highlight from its position, and this function from the hook."
6529 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6530 (org-unhighlight))
6532 (defun org-agenda-follow-mode ()
6533 "Toggle follow mode in an agenda buffer."
6534 (interactive)
6535 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6536 (org-agenda-set-mode-name)
6537 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6538 (org-agenda-show))
6539 (message "Follow mode is %s"
6540 (if org-agenda-follow-mode "on" "off")))
6542 (defun org-agenda-entry-text-mode (&optional arg)
6543 "Toggle entry text mode in an agenda buffer."
6544 (interactive "P")
6545 (setq org-agenda-entry-text-mode (or (integerp arg)
6546 (not org-agenda-entry-text-mode)))
6547 (org-agenda-entry-text-hide)
6548 (and org-agenda-entry-text-mode
6549 (let ((org-agenda-entry-text-maxlines
6550 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6551 (org-agenda-entry-text-show)))
6552 (org-agenda-set-mode-name)
6553 (message "Entry text mode is %s. Maximum number of lines is %d"
6554 (if org-agenda-entry-text-mode "on" "off")
6555 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6557 (defun org-agenda-clockreport-mode (&optional with-filter)
6558 "Toggle clocktable mode in an agenda buffer.
6559 With prefix arg WITH-FILTER, make the clocktable respect the current
6560 agenda filter."
6561 (interactive "P")
6562 (org-agenda-check-type t 'agenda)
6563 (if with-filter
6564 (setq org-agenda-clockreport-mode 'with-filter)
6565 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6566 (org-agenda-set-mode-name)
6567 (org-agenda-redo)
6568 (message "Clocktable mode is %s"
6569 (if org-agenda-clockreport-mode "on" "off")))
6571 (defun org-agenda-log-mode (&optional special)
6572 "Toggle log mode in an agenda buffer.
6573 With argument SPECIAL, show all possible log items, not only the ones
6574 configured in `org-agenda-log-mode-items'.
6575 With a double `C-u' prefix arg, show *only* log items, nothing else."
6576 (interactive "P")
6577 (org-agenda-check-type t 'agenda 'timeline)
6578 (setq org-agenda-show-log
6579 (cond
6580 ((equal special '(16)) 'only)
6581 ((eq special 'clockcheck)
6582 (if (eq org-agenda-show-log 'clockcheck)
6583 nil 'clockcheck))
6584 (special '(closed clock state))
6585 (t (not org-agenda-show-log))))
6586 (org-agenda-set-mode-name)
6587 (org-agenda-redo)
6588 (message "Log mode is %s"
6589 (if org-agenda-show-log "on" "off")))
6591 (defun org-agenda-archives-mode (&optional with-files)
6592 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6593 When called with a prefix argument, include all archive files as well."
6594 (interactive "P")
6595 (setq org-agenda-archives-mode
6596 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6597 (org-agenda-set-mode-name)
6598 (org-agenda-redo)
6599 (message
6600 "%s"
6601 (cond
6602 ((eq org-agenda-archives-mode nil)
6603 "No archives are included")
6604 ((eq org-agenda-archives-mode 'trees)
6605 (format "Trees with :%s: tag are included" org-archive-tag))
6606 ((eq org-agenda-archives-mode t)
6607 (format "Trees with :%s: tag and all active archive files are included"
6608 org-archive-tag)))))
6610 (defun org-agenda-toggle-diary ()
6611 "Toggle diary inclusion in an agenda buffer."
6612 (interactive)
6613 (org-agenda-check-type t 'agenda)
6614 (setq org-agenda-include-diary (not org-agenda-include-diary))
6615 (org-agenda-redo)
6616 (org-agenda-set-mode-name)
6617 (message "Diary inclusion turned %s"
6618 (if org-agenda-include-diary "on" "off")))
6620 (defun org-agenda-toggle-deadlines ()
6621 "Toggle inclusion of entries with a deadline in an agenda buffer."
6622 (interactive)
6623 (org-agenda-check-type t 'agenda)
6624 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6625 (org-agenda-redo)
6626 (org-agenda-set-mode-name)
6627 (message "Deadlines inclusion turned %s"
6628 (if org-agenda-include-deadlines "on" "off")))
6630 (defun org-agenda-toggle-time-grid ()
6631 "Toggle time grid in an agenda buffer."
6632 (interactive)
6633 (org-agenda-check-type t 'agenda)
6634 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6635 (org-agenda-redo)
6636 (org-agenda-set-mode-name)
6637 (message "Time-grid turned %s"
6638 (if org-agenda-use-time-grid "on" "off")))
6640 (defun org-agenda-set-mode-name ()
6641 "Set the mode name to indicate all the small mode settings."
6642 (setq mode-name
6643 (list "Org-Agenda"
6644 (if (get 'org-agenda-files 'org-restrict) " []" "")
6646 '(:eval (org-agenda-span-name org-agenda-current-span))
6647 (if org-agenda-follow-mode " Follow" "")
6648 (if org-agenda-entry-text-mode " ETxt" "")
6649 (if org-agenda-include-diary " Diary" "")
6650 (if org-agenda-include-deadlines " Ddl" "")
6651 (if org-agenda-use-time-grid " Grid" "")
6652 (if (and (boundp 'org-habit-show-habits)
6653 org-habit-show-habits) " Habit" "")
6654 (cond
6655 ((consp org-agenda-show-log) " LogAll")
6656 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6657 (org-agenda-show-log " Log")
6658 (t ""))
6659 (if (or org-agenda-filter (get 'org-agenda-filter
6660 :preset-filter))
6661 (concat " {" (mapconcat
6662 'identity
6663 (append (get 'org-agenda-filter
6664 :preset-filter)
6665 org-agenda-filter) "") "}")
6667 (if org-agenda-archives-mode
6668 (if (eq org-agenda-archives-mode t)
6669 " Archives"
6670 (format " :%s:" org-archive-tag))
6672 (if org-agenda-clockreport-mode
6673 (if (eq org-agenda-clockreport-mode 'with-filter)
6674 " Clock{}" " Clock")
6675 "")))
6676 (force-mode-line-update))
6678 (defun org-agenda-post-command-hook ()
6679 (setq org-agenda-type
6680 (or (get-text-property (point) 'org-agenda-type)
6681 (get-text-property (max (point-min) (1- (point)))
6682 'org-agenda-type))))
6684 (defun org-agenda-next-line ()
6685 "Move cursor to the next line, and show if follow mode is active."
6686 (interactive)
6687 (call-interactively 'next-line)
6688 (org-agenda-do-context-action))
6690 (defun org-agenda-previous-line ()
6691 "Move cursor to the previous line, and show if follow-mode is active."
6692 (interactive)
6693 (call-interactively 'previous-line)
6694 (org-agenda-do-context-action))
6696 (defun org-agenda-do-context-action ()
6697 "Show outline path and, maybe, follow mode window."
6698 (let ((m (org-get-at-bol 'org-marker)))
6699 (if (and org-agenda-follow-mode m)
6700 (org-agenda-show))
6701 (if (and m org-agenda-show-outline-path)
6702 (org-with-point-at m
6703 (org-display-outline-path t)))))
6705 (defun org-agenda-show-priority ()
6706 "Show the priority of the current item.
6707 This priority is composed of the main priority given with the [#A] cookies,
6708 and by additional input from the age of a schedules or deadline entry."
6709 (interactive)
6710 (let* ((pri (org-get-at-bol 'priority)))
6711 (message "Priority is %d" (if pri pri -1000))))
6713 (defun org-agenda-show-tags ()
6714 "Show the tags applicable to the current item."
6715 (interactive)
6716 (let* ((tags (org-get-at-bol 'tags)))
6717 (if tags
6718 (message "Tags are :%s:"
6719 (org-no-properties (mapconcat 'identity tags ":")))
6720 (message "No tags associated with this line"))))
6722 (defun org-agenda-goto (&optional highlight)
6723 "Go to the Org-mode file which contains the item at point."
6724 (interactive)
6725 (let* ((marker (or (org-get-at-bol 'org-marker)
6726 (org-agenda-error)))
6727 (buffer (marker-buffer marker))
6728 (pos (marker-position marker)))
6729 (switch-to-buffer-other-window buffer)
6730 (widen)
6731 (push-mark)
6732 (goto-char pos)
6733 (when (org-mode-p)
6734 (org-show-context 'agenda)
6735 (save-excursion
6736 (and (outline-next-heading)
6737 (org-flag-heading nil)))) ; show the next heading
6738 (when (outline-invisible-p)
6739 (show-entry)) ; display invisible text
6740 (recenter (/ (window-height) 2))
6741 (run-hooks 'org-agenda-after-show-hook)
6742 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6744 (defvar org-agenda-after-show-hook nil
6745 "Normal hook run after an item has been shown from the agenda.
6746 Point is in the buffer where the item originated.")
6748 (defun org-agenda-kill ()
6749 "Kill the entry or subtree belonging to the current agenda entry."
6750 (interactive)
6751 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6752 (let* ((marker (or (org-get-at-bol 'org-marker)
6753 (org-agenda-error)))
6754 (buffer (marker-buffer marker))
6755 (pos (marker-position marker))
6756 (type (org-get-at-bol 'type))
6757 dbeg dend (n 0) conf)
6758 (org-with-remote-undo buffer
6759 (with-current-buffer buffer
6760 (save-excursion
6761 (goto-char pos)
6762 (if (and (org-mode-p) (not (member type '("sexp"))))
6763 (setq dbeg (progn (org-back-to-heading t) (point))
6764 dend (org-end-of-subtree t t))
6765 (setq dbeg (point-at-bol)
6766 dend (min (point-max) (1+ (point-at-eol)))))
6767 (goto-char dbeg)
6768 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6769 (setq conf (or (eq t org-agenda-confirm-kill)
6770 (and (numberp org-agenda-confirm-kill)
6771 (> n org-agenda-confirm-kill))))
6772 (and conf
6773 (not (y-or-n-p
6774 (format "Delete entry with %d lines in buffer \"%s\"? "
6775 n (buffer-name buffer))))
6776 (error "Abort"))
6777 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6778 (with-current-buffer buffer (delete-region dbeg dend))
6779 (message "Agenda item and source killed"))))
6781 (defvar org-archive-default-command)
6782 (defun org-agenda-archive-default ()
6783 "Archive the entry or subtree belonging to the current agenda entry."
6784 (interactive)
6785 (require 'org-archive)
6786 (org-agenda-archive-with org-archive-default-command))
6788 (defun org-agenda-archive-default-with-confirmation ()
6789 "Archive the entry or subtree belonging to the current agenda entry."
6790 (interactive)
6791 (require 'org-archive)
6792 (org-agenda-archive-with org-archive-default-command 'confirm))
6794 (defun org-agenda-archive ()
6795 "Archive the entry or subtree belonging to the current agenda entry."
6796 (interactive)
6797 (org-agenda-archive-with 'org-archive-subtree))
6799 (defun org-agenda-archive-to-archive-sibling ()
6800 "Move the entry to the archive sibling."
6801 (interactive)
6802 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6804 (defun org-agenda-archive-with (cmd &optional confirm)
6805 "Move the entry to the archive sibling."
6806 (interactive)
6807 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6808 (let* ((marker (or (org-get-at-bol 'org-marker)
6809 (org-agenda-error)))
6810 (buffer (marker-buffer marker))
6811 (pos (marker-position marker)))
6812 (org-with-remote-undo buffer
6813 (with-current-buffer buffer
6814 (if (org-mode-p)
6815 (if (and confirm
6816 (not (y-or-n-p "Archive this subtree or entry? ")))
6817 (error "Abort")
6818 (save-excursion
6819 (goto-char pos)
6820 (org-remove-subtree-entries-from-agenda)
6821 (org-back-to-heading t)
6822 (funcall cmd)))
6823 (error "Archiving works only in Org-mode files"))))))
6825 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6826 "Remove all lines in the agenda that correspond to a given subtree.
6827 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6828 If this information is not given, the function uses the tree at point."
6829 (let ((buf (or buf (current-buffer))) m p)
6830 (save-excursion
6831 (unless (and beg end)
6832 (org-back-to-heading t)
6833 (setq beg (point))
6834 (org-end-of-subtree t)
6835 (setq end (point)))
6836 (set-buffer (get-buffer org-agenda-buffer-name))
6837 (save-excursion
6838 (goto-char (point-max))
6839 (beginning-of-line 1)
6840 (while (not (bobp))
6841 (when (and (setq m (org-get-at-bol 'org-marker))
6842 (equal buf (marker-buffer m))
6843 (setq p (marker-position m))
6844 (>= p beg)
6845 (< p end))
6846 (let ((inhibit-read-only t))
6847 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6848 (beginning-of-line 0))))))
6850 (defun org-agenda-refile (&optional goto rfloc no-update)
6851 "Refile the item at point."
6852 (interactive "P")
6853 (if (equal goto '(16))
6854 (org-refile-goto-last-stored)
6855 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6856 (org-agenda-error)))
6857 (buffer (marker-buffer marker))
6858 (pos (marker-position marker))
6859 (rfloc (or rfloc
6860 (org-refile-get-location
6861 (if goto "Goto" "Refile to") buffer
6862 org-refile-allow-creating-parent-nodes))))
6863 (with-current-buffer buffer
6864 (save-excursion
6865 (save-restriction
6866 (widen)
6867 (goto-char marker)
6868 (org-remove-subtree-entries-from-agenda)
6869 (org-refile goto buffer rfloc)))))
6870 (unless no-update (org-agenda-redo))))
6872 (defun org-agenda-open-link (&optional arg)
6873 "Follow the link in the current line, if any.
6874 This looks for a link in the displayed line in the agenda. It also looks
6875 at the text of the entry itself."
6876 (interactive "P")
6877 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6878 (org-get-at-bol 'org-marker)))
6879 (buffer (and marker (marker-buffer marker)))
6880 (prefix (buffer-substring
6881 (point-at-bol) (point-at-eol))))
6882 (cond
6883 (buffer
6884 (with-current-buffer buffer
6885 (save-excursion
6886 (save-restriction
6887 (widen)
6888 (goto-char marker)
6889 (org-offer-links-in-entry arg prefix)))))
6890 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6891 (save-excursion
6892 (beginning-of-line 1)
6893 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6894 (org-open-link-from-string (match-string 1)))
6895 (t (error "No link to open here")))))
6897 (defun org-agenda-copy-local-variable (var)
6898 "Get a variable from a referenced buffer and install it here."
6899 (let ((m (org-get-at-bol 'org-marker)))
6900 (when (and m (buffer-live-p (marker-buffer m)))
6901 (org-set-local var (with-current-buffer (marker-buffer m)
6902 (symbol-value var))))))
6904 (defun org-agenda-switch-to (&optional delete-other-windows)
6905 "Go to the Org-mode file which contains the item at point."
6906 (interactive)
6907 (if (and org-return-follows-link
6908 (not (org-get-at-bol 'org-marker))
6909 (org-in-regexp org-bracket-link-regexp))
6910 (org-open-link-from-string (match-string 0))
6911 (let* ((marker (or (org-get-at-bol 'org-marker)
6912 (org-agenda-error)))
6913 (buffer (marker-buffer marker))
6914 (pos (marker-position marker)))
6915 (switch-to-buffer buffer)
6916 (and delete-other-windows (delete-other-windows))
6917 (widen)
6918 (goto-char pos)
6919 (when (org-mode-p)
6920 (org-show-context 'agenda)
6921 (save-excursion
6922 (and (outline-next-heading)
6923 (org-flag-heading nil))) ; show the next heading
6924 (when (outline-invisible-p)
6925 (show-entry)))))) ; display invisible text
6927 (defun org-agenda-goto-mouse (ev)
6928 "Go to the Org-mode file which contains the item at the mouse click."
6929 (interactive "e")
6930 (mouse-set-point ev)
6931 (org-agenda-goto))
6933 (defun org-agenda-show (&optional full-entry)
6934 "Display the Org-mode file which contains the item at point.
6935 With prefix argument FULL-ENTRY, make the entire entry visible
6936 if it was hidden in the outline."
6937 (interactive "P")
6938 (let ((win (selected-window)))
6939 (if full-entry
6940 (let ((org-show-entry-below t))
6941 (org-agenda-goto t))
6942 (org-agenda-goto t))
6943 (select-window win)))
6945 (defvar org-agenda-show-window nil)
6946 (defun org-agenda-show-and-scroll-up ()
6947 "Display the Org-mode file which contains the item at point.
6948 When called repeatedly, scroll the window that is displaying the buffer."
6949 (interactive)
6950 (let ((win (selected-window)))
6951 (if (and (window-live-p org-agenda-show-window)
6952 (eq this-command last-command))
6953 (progn
6954 (select-window org-agenda-show-window)
6955 (ignore-errors (scroll-up)))
6956 (org-agenda-goto t)
6957 (show-subtree)
6958 (setq org-agenda-show-window (selected-window)))
6959 (select-window win)))
6961 (defun org-agenda-show-scroll-down ()
6962 "Scroll down the window showing the agenda."
6963 (interactive)
6964 (let ((win (selected-window)))
6965 (when (window-live-p org-agenda-show-window)
6966 (select-window org-agenda-show-window)
6967 (ignore-errors (scroll-down))
6968 (select-window win))))
6970 (defun org-agenda-show-1 (&optional more)
6971 "Display the Org-mode file which contains the item at point.
6972 The prefix arg selects the amount of information to display:
6974 0 hide the subtree
6975 1 just show the entry according to defaults.
6976 2 show the children view
6977 3 show the subtree view
6978 4 show the entire subtree and any LOGBOOK drawers
6979 5 show the entire subtree and any drawers
6980 With prefix argument FULL-ENTRY, make the entire entry visible
6981 if it was hidden in the outline."
6982 (interactive "p")
6983 (let ((win (selected-window)))
6984 (org-agenda-goto t)
6985 (org-recenter-heading 1)
6986 (cond
6987 ((= more 0)
6988 (hide-subtree)
6989 (save-excursion
6990 (org-back-to-heading)
6991 (run-hook-with-args 'org-cycle-hook 'folded))
6992 (message "Remote: FOLDED"))
6993 ((and (interactive-p) (= more 1))
6994 (message "Remote: show with default settings"))
6995 ((= more 2)
6996 (show-entry)
6997 (show-children)
6998 (save-excursion
6999 (org-back-to-heading)
7000 (run-hook-with-args 'org-cycle-hook 'children))
7001 (message "Remote: CHILDREN"))
7002 ((= more 3)
7003 (show-subtree)
7004 (save-excursion
7005 (org-back-to-heading)
7006 (run-hook-with-args 'org-cycle-hook 'subtree))
7007 (message "Remote: SUBTREE"))
7008 ((= more 4)
7009 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7010 (org-drawer-regexp
7011 (concat "^[ \t]*:\\("
7012 (mapconcat 'regexp-quote org-drawers "\\|")
7013 "\\):[ \t]*$")))
7014 (show-subtree)
7015 (save-excursion
7016 (org-back-to-heading)
7017 (org-cycle-hide-drawers 'subtree)))
7018 (message "Remote: SUBTREE AND LOGBOOK"))
7019 ((> more 4)
7020 (show-subtree)
7021 (message "Remote: SUBTREE AND ALL DRAWERS")))
7022 (select-window win)))
7024 (defun org-recenter-heading (n)
7025 (save-excursion
7026 (org-back-to-heading)
7027 (recenter n)))
7029 (defvar org-agenda-cycle-counter nil)
7030 (defun org-agenda-cycle-show (&optional n)
7031 "Show the current entry in another window, with default settings.
7032 Default settings are taken from `org-show-hierarchy-above' and siblings.
7033 When use repeatedly in immediate succession, the remote entry will cycle
7034 through visibility
7036 children -> subtree -> folded
7038 When called with a numeric prefix arg, that arg will be passed through to
7039 `org-agenda-show-1'. For the interpretation of that argument, see the
7040 docstring of `org-agenda-show-1'."
7041 (interactive "P")
7042 (if (integerp n)
7043 (setq org-agenda-cycle-counter n)
7044 (if (not (eq last-command this-command))
7045 (setq org-agenda-cycle-counter 1)
7046 (if (equal org-agenda-cycle-counter 0)
7047 (setq org-agenda-cycle-counter 2)
7048 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7049 (if (> org-agenda-cycle-counter 3)
7050 (setq org-agenda-cycle-counter 0)))))
7051 (org-agenda-show-1 org-agenda-cycle-counter))
7053 (defun org-agenda-recenter (arg)
7054 "Display the Org-mode file which contains the item at point and recenter."
7055 (interactive "P")
7056 (let ((win (selected-window)))
7057 (org-agenda-goto t)
7058 (recenter arg)
7059 (select-window win)))
7061 (defun org-agenda-show-mouse (ev)
7062 "Display the Org-mode file which contains the item at the mouse click."
7063 (interactive "e")
7064 (mouse-set-point ev)
7065 (org-agenda-show))
7067 (defun org-agenda-check-no-diary ()
7068 "Check if the entry is a diary link and abort if yes."
7069 (if (org-get-at-bol 'org-agenda-diary-link)
7070 (org-agenda-error)))
7072 (defun org-agenda-error ()
7073 (error "Command not allowed in this line"))
7075 (defun org-agenda-tree-to-indirect-buffer ()
7076 "Show the subtree corresponding to the current entry in an indirect buffer.
7077 This calls the command `org-tree-to-indirect-buffer' from the original
7078 Org-mode buffer.
7079 With numerical prefix arg ARG, go up to this level and then take that tree.
7080 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7081 use the dedicated frame)."
7082 (interactive)
7083 (org-agenda-check-no-diary)
7084 (let* ((marker (or (org-get-at-bol 'org-marker)
7085 (org-agenda-error)))
7086 (buffer (marker-buffer marker))
7087 (pos (marker-position marker)))
7088 (with-current-buffer buffer
7089 (save-excursion
7090 (goto-char pos)
7091 (call-interactively 'org-tree-to-indirect-buffer)))))
7093 (defvar org-last-heading-marker (make-marker)
7094 "Marker pointing to the headline that last changed its TODO state
7095 by a remote command from the agenda.")
7097 (defun org-agenda-todo-nextset ()
7098 "Switch TODO entry to next sequence."
7099 (interactive)
7100 (org-agenda-todo 'nextset))
7102 (defun org-agenda-todo-previousset ()
7103 "Switch TODO entry to previous sequence."
7104 (interactive)
7105 (org-agenda-todo 'previousset))
7107 (defun org-agenda-todo (&optional arg)
7108 "Cycle TODO state of line at point, also in Org-mode file.
7109 This changes the line at point, all other lines in the agenda referring to
7110 the same tree node, and the headline of the tree node in the Org-mode file."
7111 (interactive "P")
7112 (org-agenda-check-no-diary)
7113 (let* ((col (current-column))
7114 (marker (or (org-get-at-bol 'org-marker)
7115 (org-agenda-error)))
7116 (buffer (marker-buffer marker))
7117 (pos (marker-position marker))
7118 (hdmarker (org-get-at-bol 'org-hd-marker))
7119 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7120 (inhibit-read-only t)
7121 org-agenda-headline-snapshot-before-repeat newhead just-one)
7122 (org-with-remote-undo buffer
7123 (with-current-buffer buffer
7124 (widen)
7125 (goto-char pos)
7126 (org-show-context 'agenda)
7127 (save-excursion
7128 (and (outline-next-heading)
7129 (org-flag-heading nil))) ; show the next heading
7130 (let ((current-prefix-arg arg))
7131 (call-interactively 'org-todo))
7132 (and (bolp) (forward-char 1))
7133 (setq newhead (org-get-heading))
7134 (when (and (org-bound-and-true-p
7135 org-agenda-headline-snapshot-before-repeat)
7136 (not (equal org-agenda-headline-snapshot-before-repeat
7137 newhead))
7138 todayp)
7139 (setq newhead org-agenda-headline-snapshot-before-repeat
7140 just-one t))
7141 (save-excursion
7142 (org-back-to-heading)
7143 (move-marker org-last-heading-marker (point))))
7144 (beginning-of-line 1)
7145 (save-excursion
7146 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7147 (org-move-to-column col))))
7149 (defun org-agenda-add-note (&optional arg)
7150 "Add a time-stamped note to the entry at point."
7151 (interactive "P")
7152 (org-agenda-check-no-diary)
7153 (let* ((marker (or (org-get-at-bol 'org-marker)
7154 (org-agenda-error)))
7155 (buffer (marker-buffer marker))
7156 (pos (marker-position marker))
7157 (hdmarker (org-get-at-bol 'org-hd-marker))
7158 (inhibit-read-only t))
7159 (with-current-buffer buffer
7160 (widen)
7161 (goto-char pos)
7162 (org-show-context 'agenda)
7163 (save-excursion
7164 (and (outline-next-heading)
7165 (org-flag-heading nil))) ; show the next heading
7166 (org-add-note))))
7168 (defun org-agenda-change-all-lines (newhead hdmarker
7169 &optional fixface just-this)
7170 "Change all lines in the agenda buffer which match HDMARKER.
7171 The new content of the line will be NEWHEAD (as modified by
7172 `org-format-agenda-item'). HDMARKER is checked with
7173 `equal' against all `org-hd-marker' text properties in the file.
7174 If FIXFACE is non-nil, the face of each item is modified according to
7175 the new TODO state.
7176 If JUST-THIS is non-nil, change just the current line, not all.
7177 If FORCE-TAGS is non nil, the car of it returns the new tags."
7178 (let* ((inhibit-read-only t)
7179 (line (org-current-line))
7180 (thetags (with-current-buffer (marker-buffer hdmarker)
7181 (save-excursion (save-restriction (widen)
7182 (goto-char hdmarker)
7183 (org-get-tags-at)))))
7184 props m pl undone-face done-face finish new dotime cat tags)
7185 (save-excursion
7186 (goto-char (point-max))
7187 (beginning-of-line 1)
7188 (while (not finish)
7189 (setq finish (bobp))
7190 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7191 (or (not just-this) (= (org-current-line) line))
7192 (equal m hdmarker))
7193 (setq props (text-properties-at (point))
7194 dotime (org-get-at-bol 'dotime)
7195 cat (org-get-at-bol 'org-category)
7196 tags thetags
7197 new (org-format-agenda-item (org-get-at-bol 'extra)
7198 newhead cat tags dotime)
7199 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7200 undone-face (org-get-at-bol 'undone-face)
7201 done-face (org-get-at-bol 'done-face))
7202 (beginning-of-line 1)
7203 (cond
7204 ((equal new "")
7205 (and (looking-at ".*\n?") (replace-match "")))
7206 ((looking-at ".*")
7207 (replace-match new t t)
7208 (beginning-of-line 1)
7209 (add-text-properties (point-at-bol) (point-at-eol) props)
7210 (when fixface
7211 (add-text-properties
7212 (point-at-bol) (point-at-eol)
7213 (list 'face
7214 (if org-last-todo-state-is-todo
7215 undone-face done-face))))
7216 (org-agenda-highlight-todo 'line)
7217 (beginning-of-line 1))
7218 (t (error "Line update did not work"))))
7219 (beginning-of-line 0)))
7220 (org-finalize-agenda)))
7222 (defun org-agenda-align-tags (&optional line)
7223 "Align all tags in agenda items to `org-agenda-tags-column'."
7224 (let ((inhibit-read-only t) l c)
7225 (save-excursion
7226 (goto-char (if line (point-at-bol) (point-min)))
7227 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7228 (if line (point-at-eol) nil) t)
7229 (add-text-properties
7230 (match-beginning 2) (match-end 2)
7231 (list 'face (delq nil (let ((prop (get-text-property
7232 (match-beginning 2) 'face)))
7233 (or (listp prop) (setq prop (list prop)))
7234 (if (memq 'org-tag prop)
7235 prop
7236 (cons 'org-tag prop))))))
7237 (setq l (- (match-end 2) (match-beginning 2))
7238 c (if (< org-agenda-tags-column 0)
7239 (- (abs org-agenda-tags-column) l)
7240 org-agenda-tags-column))
7241 (delete-region (match-beginning 1) (match-end 1))
7242 (goto-char (match-beginning 1))
7243 (insert (org-add-props
7244 (make-string (max 1 (- c (current-column))) ?\ )
7245 (plist-put (copy-sequence (text-properties-at (point)))
7246 'face nil))))
7247 (goto-char (point-min))
7248 (org-font-lock-add-tag-faces (point-max)))))
7250 (defun org-agenda-priority-up ()
7251 "Increase the priority of line at point, also in Org-mode file."
7252 (interactive)
7253 (org-agenda-priority 'up))
7255 (defun org-agenda-priority-down ()
7256 "Decrease the priority of line at point, also in Org-mode file."
7257 (interactive)
7258 (org-agenda-priority 'down))
7260 (defun org-agenda-priority (&optional force-direction)
7261 "Set the priority of line at point, also in Org-mode file.
7262 This changes the line at point, all other lines in the agenda referring to
7263 the same tree node, and the headline of the tree node in the Org-mode file."
7264 (interactive)
7265 (unless org-enable-priority-commands
7266 (error "Priority commands are disabled"))
7267 (org-agenda-check-no-diary)
7268 (let* ((marker (or (org-get-at-bol 'org-marker)
7269 (org-agenda-error)))
7270 (hdmarker (org-get-at-bol 'org-hd-marker))
7271 (buffer (marker-buffer hdmarker))
7272 (pos (marker-position hdmarker))
7273 (inhibit-read-only t)
7274 newhead)
7275 (org-with-remote-undo buffer
7276 (with-current-buffer buffer
7277 (widen)
7278 (goto-char pos)
7279 (org-show-context 'agenda)
7280 (save-excursion
7281 (and (outline-next-heading)
7282 (org-flag-heading nil))) ; show the next heading
7283 (funcall 'org-priority force-direction)
7284 (end-of-line 1)
7285 (setq newhead (org-get-heading)))
7286 (org-agenda-change-all-lines newhead hdmarker)
7287 (beginning-of-line 1))))
7289 ;; FIXME: should fix the tags property of the agenda line.
7290 (defun org-agenda-set-tags (&optional tag onoff)
7291 "Set tags for the current headline."
7292 (interactive)
7293 (org-agenda-check-no-diary)
7294 (if (and (org-region-active-p) (interactive-p))
7295 (call-interactively 'org-change-tag-in-region)
7296 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7297 (org-agenda-error)))
7298 (buffer (marker-buffer hdmarker))
7299 (pos (marker-position hdmarker))
7300 (inhibit-read-only t)
7301 newhead)
7302 (org-with-remote-undo buffer
7303 (with-current-buffer buffer
7304 (widen)
7305 (goto-char pos)
7306 (save-excursion
7307 (org-show-context 'agenda))
7308 (save-excursion
7309 (and (outline-next-heading)
7310 (org-flag-heading nil))) ; show the next heading
7311 (goto-char pos)
7312 (if tag
7313 (org-toggle-tag tag onoff)
7314 (call-interactively 'org-set-tags))
7315 (end-of-line 1)
7316 (setq newhead (org-get-heading)))
7317 (org-agenda-change-all-lines newhead hdmarker)
7318 (beginning-of-line 1)))))
7320 (defun org-agenda-set-property ()
7321 "Set a property for the current headline."
7322 (interactive)
7323 (org-agenda-check-no-diary)
7324 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7325 (org-agenda-error)))
7326 (buffer (marker-buffer hdmarker))
7327 (pos (marker-position hdmarker))
7328 (inhibit-read-only t)
7329 newhead)
7330 (org-with-remote-undo buffer
7331 (with-current-buffer buffer
7332 (widen)
7333 (goto-char pos)
7334 (save-excursion
7335 (org-show-context 'agenda))
7336 (save-excursion
7337 (and (outline-next-heading)
7338 (org-flag-heading nil))) ; show the next heading
7339 (goto-char pos)
7340 (call-interactively 'org-set-property)))))
7342 (defun org-agenda-set-effort ()
7343 "Set the effort property for the current headline."
7344 (interactive)
7345 (org-agenda-check-no-diary)
7346 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7347 (org-agenda-error)))
7348 (buffer (marker-buffer hdmarker))
7349 (pos (marker-position hdmarker))
7350 (inhibit-read-only t)
7351 newhead)
7352 (org-with-remote-undo buffer
7353 (with-current-buffer buffer
7354 (widen)
7355 (goto-char pos)
7356 (save-excursion
7357 (org-show-context 'agenda))
7358 (save-excursion
7359 (and (outline-next-heading)
7360 (org-flag-heading nil))) ; show the next heading
7361 (goto-char pos)
7362 (call-interactively 'org-set-effort)
7363 (end-of-line 1)))))
7365 (defun org-agenda-toggle-archive-tag ()
7366 "Toggle the archive tag for the current entry."
7367 (interactive)
7368 (org-agenda-check-no-diary)
7369 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7370 (org-agenda-error)))
7371 (buffer (marker-buffer hdmarker))
7372 (pos (marker-position hdmarker))
7373 (inhibit-read-only t)
7374 newhead)
7375 (org-with-remote-undo buffer
7376 (with-current-buffer buffer
7377 (widen)
7378 (goto-char pos)
7379 (org-show-context 'agenda)
7380 (save-excursion
7381 (and (outline-next-heading)
7382 (org-flag-heading nil))) ; show the next heading
7383 (call-interactively 'org-toggle-archive-tag)
7384 (end-of-line 1)
7385 (setq newhead (org-get-heading)))
7386 (org-agenda-change-all-lines newhead hdmarker)
7387 (beginning-of-line 1))))
7389 (defun org-agenda-do-date-later (arg)
7390 (interactive "P")
7391 (cond
7392 ((or (equal arg '(16))
7393 (memq last-command
7394 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7395 (setq this-command 'org-agenda-date-later-minutes)
7396 (org-agenda-date-later-minutes 1))
7397 ((or (equal arg '(4))
7398 (memq last-command
7399 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7400 (setq this-command 'org-agenda-date-later-hours)
7401 (org-agenda-date-later-hours 1))
7403 (org-agenda-date-later (prefix-numeric-value arg)))))
7405 (defun org-agenda-do-date-earlier (arg)
7406 (interactive "P")
7407 (cond
7408 ((or (equal arg '(16))
7409 (memq last-command
7410 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7411 (setq this-command 'org-agenda-date-earlier-minutes)
7412 (org-agenda-date-earlier-minutes 1))
7413 ((or (equal arg '(4))
7414 (memq last-command
7415 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7416 (setq this-command 'org-agenda-date-earlier-hours)
7417 (org-agenda-date-earlier-hours 1))
7419 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7421 (defun org-agenda-date-later (arg &optional what)
7422 "Change the date of this item to ARG day(s) later."
7423 (interactive "p")
7424 (org-agenda-check-type t 'agenda 'timeline)
7425 (org-agenda-check-no-diary)
7426 (let* ((marker (or (org-get-at-bol 'org-marker)
7427 (org-agenda-error)))
7428 (buffer (marker-buffer marker))
7429 (pos (marker-position marker)))
7430 (org-with-remote-undo buffer
7431 (with-current-buffer buffer
7432 (widen)
7433 (goto-char pos)
7434 (if (not (org-at-timestamp-p))
7435 (error "Cannot find time stamp"))
7436 (org-timestamp-change arg (or what 'day)))
7437 (org-agenda-show-new-time marker org-last-changed-timestamp))
7438 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7440 (defun org-agenda-date-earlier (arg &optional what)
7441 "Change the date of this item to ARG day(s) earlier."
7442 (interactive "p")
7443 (org-agenda-date-later (- arg) what))
7445 (defun org-agenda-date-later-minutes (arg)
7446 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7447 (interactive "p")
7448 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7449 (org-agenda-date-later arg 'minute))
7451 (defun org-agenda-date-earlier-minutes (arg)
7452 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7453 (interactive "p")
7454 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7455 (org-agenda-date-earlier arg 'minute))
7457 (defun org-agenda-date-later-hours (arg)
7458 "Change the time of this item, in hour steps."
7459 (interactive "p")
7460 (org-agenda-date-later arg 'hour))
7462 (defun org-agenda-date-earlier-hours (arg)
7463 "Change the time of this item, in hour steps."
7464 (interactive "p")
7465 (org-agenda-date-earlier arg 'hour))
7467 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7468 "Show new date stamp via text properties."
7469 ;; We use text properties to make this undoable
7470 (let ((inhibit-read-only t)
7471 (buffer-invisibility-spec))
7472 (setq stamp (concat " " prefix " => " stamp))
7473 (save-excursion
7474 (goto-char (point-max))
7475 (while (not (bobp))
7476 (when (equal marker (org-get-at-bol 'org-marker))
7477 (org-move-to-column (- (window-width) (length stamp)) t)
7478 (org-agenda-fix-tags-filter-overlays-at (point))
7479 (if (featurep 'xemacs)
7480 ;; Use `duplicable' property to trigger undo recording
7481 (let ((ex (make-extent nil nil))
7482 (gl (make-glyph stamp)))
7483 (set-glyph-face gl 'secondary-selection)
7484 (set-extent-properties
7485 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7486 (insert-extent ex (1- (point)) (point-at-eol)))
7487 (add-text-properties
7488 (1- (point)) (point-at-eol)
7489 (list 'display (org-add-props stamp nil
7490 'face 'secondary-selection))))
7491 (beginning-of-line 1))
7492 (beginning-of-line 0)))))
7494 (defun org-agenda-date-prompt (arg)
7495 "Change the date of this item. Date is prompted for, with default today.
7496 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7497 be used to request time specification in the time stamp."
7498 (interactive "P")
7499 (org-agenda-check-type t 'agenda 'timeline)
7500 (org-agenda-check-no-diary)
7501 (let* ((marker (or (org-get-at-bol 'org-marker)
7502 (org-agenda-error)))
7503 (buffer (marker-buffer marker))
7504 (pos (marker-position marker)))
7505 (org-with-remote-undo buffer
7506 (with-current-buffer buffer
7507 (widen)
7508 (goto-char pos)
7509 (if (not (org-at-timestamp-p t))
7510 (error "Cannot find time stamp"))
7511 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7512 (org-agenda-show-new-time marker org-last-changed-timestamp))
7513 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7515 (defun org-agenda-schedule (arg &optional time)
7516 "Schedule the item at point.
7517 Arg is passed through to `org-schedule'."
7518 (interactive "P")
7519 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7520 (org-agenda-check-no-diary)
7521 (let* ((marker (or (org-get-at-bol 'org-marker)
7522 (org-agenda-error)))
7523 (type (marker-insertion-type marker))
7524 (buffer (marker-buffer marker))
7525 (pos (marker-position marker))
7526 (org-insert-labeled-timestamps-at-point nil)
7528 (set-marker-insertion-type marker t)
7529 (org-with-remote-undo buffer
7530 (with-current-buffer buffer
7531 (widen)
7532 (goto-char pos)
7533 (setq ts (org-schedule arg time)))
7534 (org-agenda-show-new-time marker ts "S"))
7535 (message "Item scheduled for %s" ts)))
7537 (defun org-agenda-deadline (arg &optional time)
7538 "Schedule the item at point.
7539 Arg is passed through to `org-deadline'."
7540 (interactive "P")
7541 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7542 (org-agenda-check-no-diary)
7543 (let* ((marker (or (org-get-at-bol 'org-marker)
7544 (org-agenda-error)))
7545 (buffer (marker-buffer marker))
7546 (pos (marker-position marker))
7547 (org-insert-labeled-timestamps-at-point nil)
7549 (org-with-remote-undo buffer
7550 (with-current-buffer buffer
7551 (widen)
7552 (goto-char pos)
7553 (setq ts (org-deadline arg time)))
7554 (org-agenda-show-new-time marker ts "D"))
7555 (message "Deadline for this item set to %s" ts)))
7557 (defun org-agenda-action ()
7558 "Select entry for agenda action, or execute an agenda action.
7559 This command prompts for another letter. Valid inputs are:
7561 m Mark the entry at point for an agenda action
7562 s Schedule the marked entry to the date at the cursor
7563 d Set the deadline of the marked entry to the date at the cursor
7564 r Call `org-remember' with cursor date as the default date
7565 c Call `org-capture' with cursor date as the default date
7566 SPC Show marked entry in other window
7567 TAB Visit marked entry in other window
7569 The cursor may be at a date in the calendar, or in the Org agenda."
7570 (interactive)
7571 (let (ans)
7572 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7573 (setq ans (read-char-exclusive))
7574 (cond
7575 ((equal ans ?m)
7576 ;; Mark this entry
7577 (if (eq major-mode 'org-agenda-mode)
7578 (let ((m (or (org-get-at-bol 'org-hd-marker)
7579 (org-get-at-bol 'org-marker))))
7580 (if m
7581 (progn
7582 (move-marker org-agenda-action-marker
7583 (marker-position m) (marker-buffer m))
7584 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7585 (error "Don't know which entry to mark")))
7586 (error "This command works only in the agenda")))
7587 ((equal ans ?s)
7588 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7589 ((equal ans ?d)
7590 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7591 ((equal ans ?r)
7592 (org-agenda-do-action '(org-remember) t))
7593 ((equal ans ?c)
7594 (org-agenda-do-action '(org-capture) t))
7595 ((equal ans ?\ )
7596 (let ((cw (selected-window)))
7597 (org-switch-to-buffer-other-window
7598 (marker-buffer org-agenda-action-marker))
7599 (goto-char org-agenda-action-marker)
7600 (org-show-context 'agenda)
7601 (select-window cw)))
7602 ((equal ans ?\C-i)
7603 (org-switch-to-buffer-other-window
7604 (marker-buffer org-agenda-action-marker))
7605 (goto-char org-agenda-action-marker)
7606 (org-show-context 'agenda))
7607 (t (error "Invalid agenda action %c" ans)))))
7609 (defun org-agenda-do-action (form &optional current-buffer)
7610 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7611 (let ((org-overriding-default-time (org-get-cursor-date)))
7612 (if current-buffer
7613 (eval form)
7614 (if (not (marker-buffer org-agenda-action-marker))
7615 (error "No entry has been selected for agenda action")
7616 (with-current-buffer (marker-buffer org-agenda-action-marker)
7617 (save-excursion
7618 (save-restriction
7619 (widen)
7620 (goto-char org-agenda-action-marker)
7621 (eval form))))))))
7623 (defun org-agenda-clock-in (&optional arg)
7624 "Start the clock on the currently selected item."
7625 (interactive "P")
7626 (org-agenda-check-no-diary)
7627 (if (equal arg '(4))
7628 (org-clock-in arg)
7629 (let* ((marker (or (org-get-at-bol 'org-marker)
7630 (org-agenda-error)))
7631 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7632 marker))
7633 (pos (marker-position marker))
7634 newhead)
7635 (org-with-remote-undo (marker-buffer marker)
7636 (with-current-buffer (marker-buffer marker)
7637 (widen)
7638 (goto-char pos)
7639 (org-show-context 'agenda)
7640 (org-show-entry)
7641 (org-cycle-hide-drawers 'children)
7642 (org-clock-in arg)
7643 (setq newhead (org-get-heading)))
7644 (org-agenda-change-all-lines newhead hdmarker)))))
7646 (defun org-agenda-clock-out ()
7647 "Stop the currently running clock."
7648 (interactive)
7649 (unless (marker-buffer org-clock-marker)
7650 (error "No running clock"))
7651 (let ((marker (make-marker)) newhead)
7652 (org-with-remote-undo (marker-buffer org-clock-marker)
7653 (with-current-buffer (marker-buffer org-clock-marker)
7654 (save-excursion
7655 (save-restriction
7656 (widen)
7657 (goto-char org-clock-marker)
7658 (org-back-to-heading t)
7659 (move-marker marker (point))
7660 (org-clock-out)
7661 (setq newhead (org-get-heading))))))
7662 (org-agenda-change-all-lines newhead marker)
7663 (move-marker marker nil)))
7665 (defun org-agenda-clock-cancel (&optional arg)
7666 "Cancel the currently running clock."
7667 (interactive "P")
7668 (unless (marker-buffer org-clock-marker)
7669 (error "No running clock"))
7670 (org-with-remote-undo (marker-buffer org-clock-marker)
7671 (org-clock-cancel)))
7673 (defun org-agenda-clock-goto ()
7674 "Jump to the currently clocked in task within the agenda.
7675 If the currently clocked in task is not listed in the agenda
7676 buffer, display it in another window."
7677 (interactive)
7678 (let (pos)
7679 (mapc (lambda (o)
7680 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7681 (setq pos (overlay-start o))))
7682 (overlays-in (point-min) (point-max)))
7683 (cond (pos (goto-char pos))
7684 ;; If the currently clocked entry is not in the agenda
7685 ;; buffer, we visit it in another window:
7686 (org-clock-current-task
7687 (org-switch-to-buffer-other-window (org-clock-goto)))
7688 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7690 (defun org-agenda-diary-entry-in-org-file ()
7691 "Make a diary entry in the file `org-agenda-diary-file'."
7692 (let (d1 d2 char (text "") dp1 dp2)
7693 (if (equal (buffer-name) "*Calendar*")
7694 (setq d1 (calendar-cursor-to-date t)
7695 d2 (car calendar-mark-ring))
7696 (setq dp1 (get-text-property (point-at-bol) 'day))
7697 (unless dp1 (error "No date defined in current line"))
7698 (setq d1 (calendar-gregorian-from-absolute dp1)
7699 d2 (and (ignore-errors (mark))
7700 (save-excursion
7701 (goto-char (mark))
7702 (setq dp2 (get-text-property (point-at-bol) 'day)))
7703 (calendar-gregorian-from-absolute dp2))))
7704 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7705 (setq char (read-char-exclusive))
7706 (cond
7707 ((equal char ?d)
7708 (setq text (read-string "Day entry: "))
7709 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7710 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7711 ((equal char ?a)
7712 (setq d1 (list (car d1) (nth 1 d1)
7713 (read-number (format "Reference year [%d]: " (nth 2 d1))
7714 (nth 2 d1))))
7715 (setq text (read-string "Anniversary (use %d to show years): "))
7716 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7717 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7718 ((equal char ?b)
7719 (setq text (read-string "Block entry: "))
7720 (unless (and d1 d2 (not (equal d1 d2)))
7721 (error "No block of days selected"))
7722 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7723 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7724 ((equal char ?j)
7725 (org-switch-to-buffer-other-window
7726 (find-file-noselect org-agenda-diary-file))
7727 (require 'org-datetree)
7728 (org-datetree-find-date-create d1)
7729 (org-reveal t))
7730 (t (error "Invalid selection character `%c'" char)))))
7732 (defcustom org-agenda-insert-diary-strategy 'date-tree
7733 "Where in `org-agenda-diary-file' should new entries be added?
7734 Valid values:
7736 date-tree in the date tree, as child of the date
7737 top-level as top-level entries at the end of the file."
7738 :group 'org-agenda
7739 :type '(choice
7740 (const :tag "in a date tree" date-tree)
7741 (const :tag "as top level at end of file" top-level)))
7743 (defcustom org-agenda-insert-diary-extract-time nil
7744 "Non-nil means extract any time specification from the diary entry."
7745 :group 'org-agenda
7746 :type 'boolean)
7748 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7749 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7750 If TEXT is not empty, it will become the headline of the new entry, and
7751 the resulting entry will not be shown. When TEXT is empty, switch to
7752 `org-agenda-diary-file' and let the user finish the entry there."
7753 (let ((cw (current-window-configuration)))
7754 (org-switch-to-buffer-other-window
7755 (find-file-noselect org-agenda-diary-file))
7756 (widen)
7757 (goto-char (point-min))
7758 (cond
7759 ((eq type 'anniversary)
7760 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7761 (progn
7762 (or (org-on-heading-p t)
7763 (progn
7764 (outline-next-heading)
7765 (insert "* Anniversaries\n\n")
7766 (beginning-of-line -1)))))
7767 (outline-next-heading)
7768 (org-back-over-empty-lines)
7769 (backward-char 1)
7770 (insert "\n")
7771 (require 'diary-lib)
7772 (let ((calendar-date-display-form
7773 (if (if (boundp 'calendar-date-style)
7774 (eq calendar-date-style 'european)
7775 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
7776 (org-bound-and-true-p european-calendar-style))) ; Emacs 22
7777 '(day " " month " " year)
7778 '(month " " day " " year))))
7780 (insert (format "%%%%(diary-anniversary %s) %s"
7781 (calendar-date-string d1 nil t) text))))
7782 ((eq type 'day)
7783 (let ((org-prefix-has-time t)
7784 (org-agenda-time-leading-zero t)
7785 fmt time time2)
7786 (if org-agenda-insert-diary-extract-time
7787 ;; Use org-format-agenda-item to parse text for a time-range and
7788 ;; remove it. FIXME: This is a hack, we should refactor
7789 ;; that function to make time extraction available separately
7790 (setq fmt (org-format-agenda-item nil text nil nil t)
7791 time (get-text-property 0 'time fmt)
7792 time2 (if (> (length time) 0)
7793 ;; split-string removes trailing ...... if
7794 ;; no end time given. First space
7795 ;; separates time from date.
7796 (concat " " (car (split-string time "\\.")))
7797 nil)
7798 text (get-text-property 0 'txt fmt)))
7799 (if (eq org-agenda-insert-diary-strategy 'top-level)
7800 (org-agenda-insert-diary-as-top-level text)
7801 (require 'org-datetree)
7802 (org-datetree-find-date-create d1)
7803 (org-agenda-insert-diary-make-new-entry text))
7804 (org-insert-time-stamp (org-time-from-absolute
7805 (calendar-absolute-from-gregorian d1))
7806 nil nil nil nil time2))
7807 (end-of-line 0))
7808 ((eq type 'block)
7809 (if (> (calendar-absolute-from-gregorian d1)
7810 (calendar-absolute-from-gregorian d2))
7811 (setq d1 (prog1 d2 (setq d2 d1))))
7812 (if (eq org-agenda-insert-diary-strategy 'top-level)
7813 (org-agenda-insert-diary-as-top-level text)
7814 (require 'org-datetree)
7815 (org-datetree-find-date-create d1)
7816 (org-agenda-insert-diary-make-new-entry text))
7817 (org-insert-time-stamp (org-time-from-absolute
7818 (calendar-absolute-from-gregorian d1)))
7819 (insert "--")
7820 (org-insert-time-stamp (org-time-from-absolute
7821 (calendar-absolute-from-gregorian d2)))
7822 (end-of-line 0)))
7823 (if (string-match "\\S-" text)
7824 (progn
7825 (set-window-configuration cw)
7826 (message "%s entry added to %s"
7827 (capitalize (symbol-name type))
7828 (abbreviate-file-name org-agenda-diary-file)))
7829 (org-reveal t)
7830 (message "Please finish entry here"))))
7832 (defun org-agenda-insert-diary-as-top-level (text)
7833 "Make new entry as a top-level entry at the end of the file.
7834 Add TEXT as headline, and position the cursor in the second line so that
7835 a timestamp can be added there."
7836 (widen)
7837 (goto-char (point-max))
7838 (or (bolp) (insert "\n"))
7839 (insert "* " text "\n")
7840 (if org-adapt-indentation (org-indent-to-column 2)))
7842 (defun org-agenda-insert-diary-make-new-entry (text)
7843 "Make new entry as last child of current entry.
7844 Add TEXT as headline, and position the cursor in the second line so that
7845 a timestamp can be added there."
7846 (let ((org-show-following-heading t)
7847 (org-show-siblings t)
7848 (org-show-hierarchy-above t)
7849 (org-show-entry-below t)
7850 col)
7851 (outline-next-heading)
7852 (org-back-over-empty-lines)
7853 (or (looking-at "[ \t]*$")
7854 (progn (insert "\n") (backward-char 1)))
7855 (org-insert-heading nil t)
7856 (org-do-demote)
7857 (setq col (current-column))
7858 (insert text "\n")
7859 (if org-adapt-indentation (org-indent-to-column col))
7860 (let ((org-show-following-heading t)
7861 (org-show-siblings t)
7862 (org-show-hierarchy-above t)
7863 (org-show-entry-below t))
7864 (org-show-context))))
7866 (defun org-agenda-diary-entry ()
7867 "Make a diary entry, like the `i' command from the calendar.
7868 All the standard commands work: block, weekly etc.
7869 When `org-agenda-diary-file' points to a file,
7870 `org-agenda-diary-entry-in-org-file' is called instead to create
7871 entries in that Org-mode file."
7872 (interactive)
7873 (org-agenda-check-type t 'agenda 'timeline)
7874 (if (not (eq org-agenda-diary-file 'diary-file))
7875 (org-agenda-diary-entry-in-org-file)
7876 (require 'diary-lib)
7877 (let* ((char (progn
7878 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7879 (read-char-exclusive)))
7880 (cmd (cdr (assoc char
7881 '((?d . insert-diary-entry)
7882 (?w . insert-weekly-diary-entry)
7883 (?m . insert-monthly-diary-entry)
7884 (?y . insert-yearly-diary-entry)
7885 (?a . insert-anniversary-diary-entry)
7886 (?b . insert-block-diary-entry)
7887 (?c . insert-cyclic-diary-entry)))))
7888 (oldf (symbol-function 'calendar-cursor-to-date))
7889 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7890 (point (point))
7891 (mark (or (mark t) (point))))
7892 (unless cmd
7893 (error "No command associated with <%c>" char))
7894 (unless (and (get-text-property point 'day)
7895 (or (not (equal ?b char))
7896 (get-text-property mark 'day)))
7897 (error "Don't know which date to use for diary entry"))
7898 ;; We implement this by hacking the `calendar-cursor-to-date' function
7899 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7900 (let ((calendar-mark-ring
7901 (list (calendar-gregorian-from-absolute
7902 (or (get-text-property mark 'day)
7903 (get-text-property point 'day))))))
7904 (unwind-protect
7905 (progn
7906 (fset 'calendar-cursor-to-date
7907 (lambda (&optional error dummy)
7908 (calendar-gregorian-from-absolute
7909 (get-text-property point 'day))))
7910 (call-interactively cmd))
7911 (fset 'calendar-cursor-to-date oldf))))))
7913 (defun org-agenda-execute-calendar-command (cmd)
7914 "Execute a calendar command from the agenda, with the date associated to
7915 the cursor position."
7916 (org-agenda-check-type t 'agenda 'timeline)
7917 (require 'diary-lib)
7918 (unless (get-text-property (point) 'day)
7919 (error "Don't know which date to use for calendar command"))
7920 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7921 (point (point))
7922 (date (calendar-gregorian-from-absolute
7923 (get-text-property point 'day)))
7924 ;; the following 2 vars are needed in the calendar
7925 (displayed-month (car date))
7926 (displayed-year (nth 2 date)))
7927 (unwind-protect
7928 (progn
7929 (fset 'calendar-cursor-to-date
7930 (lambda (&optional error dummy)
7931 (calendar-gregorian-from-absolute
7932 (get-text-property point 'day))))
7933 (call-interactively cmd))
7934 (fset 'calendar-cursor-to-date oldf))))
7936 (defun org-agenda-phases-of-moon ()
7937 "Display the phases of the moon for the 3 months around the cursor date."
7938 (interactive)
7939 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7941 (defun org-agenda-holidays ()
7942 "Display the holidays for the 3 months around the cursor date."
7943 (interactive)
7944 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7946 (defvar calendar-longitude)
7947 (defvar calendar-latitude)
7948 (defvar calendar-location-name)
7950 (defun org-agenda-sunrise-sunset (arg)
7951 "Display sunrise and sunset for the cursor date.
7952 Latitude and longitude can be specified with the variables
7953 `calendar-latitude' and `calendar-longitude'. When called with prefix
7954 argument, latitude and longitude will be prompted for."
7955 (interactive "P")
7956 (require 'solar)
7957 (let ((calendar-longitude (if arg nil calendar-longitude))
7958 (calendar-latitude (if arg nil calendar-latitude))
7959 (calendar-location-name
7960 (if arg "the given coordinates" calendar-location-name)))
7961 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7963 (defun org-agenda-goto-calendar ()
7964 "Open the Emacs calendar with the date at the cursor."
7965 (interactive)
7966 (org-agenda-check-type t 'agenda 'timeline)
7967 (let* ((day (or (get-text-property (point) 'day)
7968 (error "Don't know which date to open in calendar")))
7969 (date (calendar-gregorian-from-absolute day))
7970 (calendar-move-hook nil)
7971 (calendar-view-holidays-initially-flag nil)
7972 (calendar-view-diary-initially-flag nil))
7973 (calendar)
7974 (calendar-goto-date date)))
7976 ;;;###autoload
7977 (defun org-calendar-goto-agenda ()
7978 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
7979 This is a command that has to be installed in `calendar-mode-map'."
7980 (interactive)
7981 (org-agenda-list nil (calendar-absolute-from-gregorian
7982 (calendar-cursor-to-date))
7983 nil))
7985 (defun org-agenda-convert-date ()
7986 (interactive)
7987 (org-agenda-check-type t 'agenda 'timeline)
7988 (let ((day (get-text-property (point) 'day))
7989 date s)
7990 (unless day
7991 (error "Don't know which date to convert"))
7992 (setq date (calendar-gregorian-from-absolute day))
7993 (setq s (concat
7994 "Gregorian: " (calendar-date-string date) "\n"
7995 "ISO: " (calendar-iso-date-string date) "\n"
7996 "Day of Yr: " (calendar-day-of-year-string date) "\n"
7997 "Julian: " (calendar-julian-date-string date) "\n"
7998 "Astron. JD: " (calendar-astro-date-string date)
7999 " (Julian date number at noon UTC)\n"
8000 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8001 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8002 "French: " (calendar-french-date-string date) "\n"
8003 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8004 "Mayan: " (calendar-mayan-date-string date) "\n"
8005 "Coptic: " (calendar-coptic-date-string date) "\n"
8006 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8007 "Persian: " (calendar-persian-date-string date) "\n"
8008 "Chinese: " (calendar-chinese-date-string date) "\n"))
8009 (with-output-to-temp-buffer "*Dates*"
8010 (princ s))
8011 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8013 ;;; Bulk commands
8015 (defvar org-agenda-bulk-marked-entries nil
8016 "List of markers that refer to marked entries in the agenda.")
8018 (defun org-agenda-bulk-marked-p ()
8019 (eq (get-char-property (point-at-bol) 'type)
8020 'org-marked-entry-overlay))
8022 (defun org-agenda-bulk-mark (&optional arg)
8023 "Mark the entry at point for future bulk action."
8024 (interactive "p")
8025 (dotimes (i (max arg 1))
8026 (unless (org-get-at-bol 'org-agenda-diary-link)
8027 (let* ((m (org-get-at-bol 'org-hd-marker))
8029 (unless (org-agenda-bulk-marked-p)
8030 (unless m (error "Nothing to mark at point"))
8031 (push m org-agenda-bulk-marked-entries)
8032 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8033 (org-overlay-display ov "> "
8034 (org-get-todo-face "TODO")
8035 'evaporate)
8036 (overlay-put ov 'type 'org-marked-entry-overlay))
8037 (beginning-of-line 2)
8038 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8039 (beginning-of-line 2))
8040 (message "%d entries marked for bulk action"
8041 (length org-agenda-bulk-marked-entries))))))
8043 (defun org-agenda-bulk-mark-regexp (regexp)
8044 "Mark entries match REGEXP."
8045 (interactive "sMark entries matching regexp: ")
8046 (let (entries-marked)
8047 (save-excursion
8048 (goto-char (point-min))
8049 (goto-char (next-single-property-change (point) 'txt))
8050 (while (re-search-forward regexp nil t)
8051 (when (string-match regexp (get-text-property (point) 'txt))
8052 (setq entries-marked (+ entries-marked 1))
8053 (call-interactively 'org-agenda-bulk-mark))))
8054 (if (not entries-marked)
8055 (message "No entry matching this regexp."))))
8057 (defun org-agenda-bulk-unmark ()
8058 "Unmark the entry at point for future bulk action."
8059 (interactive)
8060 (when (org-agenda-bulk-marked-p)
8061 (org-agenda-bulk-remove-overlays
8062 (point-at-bol) (+ 2 (point-at-bol)))
8063 (setq org-agenda-bulk-marked-entries
8064 (delete (org-get-at-bol 'org-hd-marker)
8065 org-agenda-bulk-marked-entries)))
8066 (beginning-of-line 2)
8067 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8068 (beginning-of-line 2))
8069 (message "%d entries marked for bulk action"
8070 (length org-agenda-bulk-marked-entries)))
8072 (defun org-agenda-bulk-toggle ()
8073 "Toggle marking the entry at point for bulk action."
8074 (interactive)
8075 (if (org-agenda-bulk-marked-p)
8076 (org-agenda-bulk-unmark)
8077 (org-agenda-bulk-mark)))
8079 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8080 "Remove the mark overlays between BEG and END in the agenda buffer.
8081 BEG and END default to the buffer limits.
8083 This only removes the overlays, it does not remove the markers
8084 from the list in `org-agenda-bulk-marked-entries'."
8085 (interactive)
8086 (mapc (lambda (ov)
8087 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8088 (delete-overlay ov)))
8089 (overlays-in (or beg (point-min)) (or end (point-max)))))
8091 (defun org-agenda-bulk-remove-all-marks ()
8092 "Remove all marks in the agenda buffer.
8093 This will remove the markers, and the overlays."
8094 (interactive)
8095 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8096 (setq org-agenda-bulk-marked-entries nil)
8097 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8099 (defun org-agenda-bulk-action (&optional arg)
8100 "Execute an remote-editing action on all marked entries.
8101 The prefix arg is passed through to the command if possible."
8102 (interactive "P")
8103 ;; Make sure we have markers, and only valid ones
8104 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8105 (mapc
8106 (lambda (m)
8107 (unless (and (markerp m)
8108 (marker-buffer m)
8109 (buffer-live-p (marker-buffer m))
8110 (marker-position m))
8111 (error "Marker %s for bulk command is invalid" m)))
8112 org-agenda-bulk-marked-entries)
8114 ;; Prompt for the bulk command
8115 (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline [f]unction")
8116 (let* ((action (read-char-exclusive))
8117 (org-log-refile (if org-log-refile 'time nil))
8118 (entries (reverse org-agenda-bulk-marked-entries))
8119 redo-at-end
8120 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8121 (cond
8122 ((equal action ?$)
8123 (setq cmd '(org-agenda-archive)))
8125 ((equal action ?A)
8126 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8128 ((member action '(?r ?w))
8129 (setq rfloc (org-refile-get-location
8130 "Refile to"
8131 (marker-buffer (car org-agenda-bulk-marked-entries))
8132 org-refile-allow-creating-parent-nodes))
8133 (if (nth 3 rfloc)
8134 (setcar (nthcdr 3 rfloc)
8135 (move-marker (make-marker) (nth 3 rfloc)
8136 (or (get-file-buffer (nth 1 rfloc))
8137 (find-buffer-visiting (nth 1 rfloc))
8138 (error "This should not happen")))))
8140 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8141 redo-at-end t))
8143 ((equal action ?t)
8144 (setq state (org-icompleting-read
8145 "Todo state: "
8146 (with-current-buffer (marker-buffer (car entries))
8147 (mapcar 'list org-todo-keywords-1))))
8148 (setq cmd `(let ((org-inhibit-blocking t)
8149 (org-inhibit-logging 'note))
8150 (org-agenda-todo ,state))))
8152 ((memq action '(?- ?+))
8153 (setq tag (org-icompleting-read
8154 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8155 (with-current-buffer (marker-buffer (car entries))
8156 (delq nil
8157 (mapcar (lambda (x)
8158 (if (stringp (car x)) x)) org-tag-alist)))))
8159 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8161 ((memq action '(?s ?d))
8162 (let* ((date (unless arg
8163 (org-read-date
8164 nil nil nil
8165 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8166 (ans (if arg nil org-read-date-final-answer))
8167 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8168 (setq cmd `(let* ((bound (fboundp 'read-string))
8169 (old (and bound (symbol-function 'read-string))))
8170 (unwind-protect
8171 (progn
8172 (fset 'read-string (lambda (&rest ignore) ,ans))
8173 (eval '(,c1 arg)))
8174 (if bound
8175 (fset 'read-string old)
8176 (fmakunbound 'read-string)))))))
8178 ((equal action ?S)
8179 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8180 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8181 (let ((days (read-number
8182 (format "Scatter tasks across how many %sdays: "
8183 (if arg "week" "")) 7)))
8184 (setq cmd
8185 `(let ((distance (1+ (random ,days))))
8186 (if arg
8187 (let ((dist distance)
8188 (day-of-week
8189 (calendar-day-of-week
8190 (calendar-gregorian-from-absolute (org-today)))))
8191 (dotimes (i (1+ dist))
8192 (while (member day-of-week org-agenda-weekend-days)
8193 (incf distance)
8194 (incf day-of-week)
8195 (if (= day-of-week 7)
8196 (setq day-of-week 0)))
8197 (incf day-of-week)
8198 (if (= day-of-week 7)
8199 (setq day-of-week 0)))))
8200 ;; silently fail when try to replan a sexp entry
8201 (condition-case nil
8202 (let* ((date (calendar-gregorian-from-absolute
8203 (+ (org-today) distance)))
8204 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8205 (nth 2 date))))
8206 (org-agenda-schedule nil time))
8207 (error nil)))))))
8209 ((equal action ?f)
8210 (setq cmd (list (intern
8211 (org-icompleting-read "Function: "
8212 obarray 'fboundp t nil nil)))))
8214 (t (error "Invalid bulk action")))
8216 ;; Sort the markers, to make sure that parents are handled before children
8217 (setq entries (sort entries
8218 (lambda (a b)
8219 (cond
8220 ((equal (marker-buffer a) (marker-buffer b))
8221 (< (marker-position a) (marker-position b)))
8223 (string< (buffer-name (marker-buffer a))
8224 (buffer-name (marker-buffer b))))))))
8226 ;; Now loop over all markers and apply cmd
8227 (while (setq e (pop entries))
8228 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8229 (if (not pos)
8230 (progn (message "Skipping removed entry at %s" e)
8231 (setq cntskip (1+ cntskip)))
8232 (goto-char pos)
8233 (eval cmd)
8234 (setq org-agenda-bulk-marked-entries
8235 (delete e org-agenda-bulk-marked-entries))
8236 (setq cnt (1+ cnt))))
8237 (setq org-agenda-bulk-marked-entries nil)
8238 (org-agenda-bulk-remove-all-marks)
8239 (when redo-at-end (org-agenda-redo))
8240 (message "Acted on %d entries%s"
8242 (if (= cntskip 0)
8244 (format ", skipped %d (disappeared before their turn)"
8245 cntskip)))))
8247 ;;; Flagging notes
8249 (defun org-agenda-show-the-flagging-note ()
8250 "Display the flagging note in the other window.
8251 When called a second time in direct sequence, offer to remove the FLAGGING
8252 tag and (if present) the flagging note."
8253 (interactive)
8254 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8255 (win (selected-window))
8256 note heading newhead)
8257 (unless hdmarker
8258 (error "No linked entry at point"))
8259 (if (and (eq this-command last-command)
8260 (y-or-n-p "Unflag and remove any flagging note? "))
8261 (progn
8262 (org-agenda-remove-flag hdmarker)
8263 (let ((win (get-buffer-window "*Flagging Note*")))
8264 (and win (delete-window win)))
8265 (message "Entry unflaged"))
8266 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8267 (unless note
8268 (error "No flagging note"))
8269 (org-kill-new note)
8270 (org-switch-to-buffer-other-window "*Flagging Note*")
8271 (erase-buffer)
8272 (insert note)
8273 (goto-char (point-min))
8274 (while (re-search-forward "\\\\n" nil t)
8275 (replace-match "\n" t t))
8276 (goto-char (point-min))
8277 (select-window win)
8278 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8280 (defun org-agenda-remove-flag (marker)
8281 "Remove the FLAGGED tag and any flagging note in the entry."
8282 (let (newhead)
8283 (org-with-point-at marker
8284 (org-toggle-tag "FLAGGED" 'off)
8285 (org-entry-delete nil "THEFLAGGINGNOTE")
8286 (setq newhead (org-get-heading)))
8287 (org-agenda-change-all-lines newhead marker)
8288 (message "Entry unflaged")))
8290 (defun org-agenda-get-any-marker (&optional pos)
8291 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8292 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8294 ;;; Appointment reminders
8296 (defvar appt-time-msg-list)
8298 ;;;###autoload
8299 (defun org-agenda-to-appt (&optional refresh filter)
8300 "Activate appointments found in `org-agenda-files'.
8301 With a \\[universal-argument] prefix, refresh the list of
8302 appointments.
8304 If FILTER is t, interactively prompt the user for a regular
8305 expression, and filter out entries that don't match it.
8307 If FILTER is a string, use this string as a regular expression
8308 for filtering entries out.
8310 FILTER can also be an alist with the car of each cell being
8311 either 'headline or 'category. For example:
8313 '((headline \"IMPORTANT\")
8314 (category \"Work\"))
8316 will only add headlines containing IMPORTANT or headlines
8317 belonging to the \"Work\" category."
8318 (interactive "P")
8319 (if refresh (setq appt-time-msg-list nil))
8320 (if (eq filter t)
8321 (setq filter (read-from-minibuffer "Regexp filter: ")))
8322 (let* ((cnt 0) ; count added events
8323 (org-agenda-new-buffers nil)
8324 (org-deadline-warning-days 0)
8325 ;; Do not use `org-today' here because appt only takes
8326 ;; time and without date as argument, so it may pass wrong
8327 ;; information otherwise
8328 (today (org-date-to-gregorian
8329 (time-to-days (current-time))))
8330 (org-agenda-restrict nil)
8331 (files (org-agenda-files 'unrestricted)) entries file)
8332 ;; Get all entries which may contain an appt
8333 (org-prepare-agenda-buffers files)
8334 (while (setq file (pop files))
8335 (setq entries
8336 (append entries
8337 (org-agenda-get-day-entries
8338 file today :timestamp :scheduled :deadline))))
8339 (setq entries (delq nil entries))
8340 ;; Map thru entries and find if we should filter them out
8341 (mapc
8342 (lambda(x)
8343 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8344 (cat (get-text-property 1 'org-category x))
8345 (tod (get-text-property 1 'time-of-day x))
8346 (ok (or (null filter)
8347 (and (stringp filter) (string-match filter evt))
8348 (and (listp filter)
8349 (or (string-match
8350 (cadr (assoc 'category filter)) cat)
8351 (string-match
8352 (cadr (assoc 'headline filter)) evt))))))
8353 ;; FIXME: Shall we remove text-properties for the appt text?
8354 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8355 (when (and ok tod)
8356 (setq tod (concat "00" (number-to-string tod))
8357 tod (when (string-match
8358 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8359 (concat (match-string 1 tod) ":"
8360 (match-string 2 tod))))
8361 (appt-add tod evt)
8362 (setq cnt (1+ cnt))))) entries)
8363 (org-release-buffers org-agenda-new-buffers)
8364 (if (eq cnt 0)
8365 (message "No event to add")
8366 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8368 (defun org-agenda-todayp (date)
8369 "Does DATE mean today, when considering `org-extend-today-until'?"
8370 (let ((today (org-today))
8371 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8372 date)))
8373 (eq date today)))
8375 (provide 'org-agenda)
8377 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
8379 ;;; org-agenda.el ends here