Revert "Silence byte-compiler with a new alias: filter-buffer-substring-functions."
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-agenda.el
blob934bdbc6fbb1908069cd623410c1886b9c998ca4
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 globally by leaving out lines like the agenda span
93 name and week number or the separator lines."
94 :group 'org-agenda
95 :type 'boolean)
97 (defcustom org-agenda-block-separator ?=
98 "The separator between blocks in the agenda.
99 If this is a string, it will be used as the separator, with a newline added.
100 If it is a character, it will be repeated to fill the window width.
101 If nil the separator is disabled. In `org-agenda-custom-commands' this
102 addresses the separator between the current and the previous block."
103 :group 'org-agenda
104 :type '(choice
105 (const :tag "Disabled" nil)
106 (character)
107 (string)))
109 (defgroup org-agenda-export nil
110 "Options concerning exporting agenda views in Org-mode."
111 :tag "Org Agenda Export"
112 :group 'org-agenda)
114 (defcustom org-agenda-with-colors t
115 "Non-nil means use colors in agenda views."
116 :group 'org-agenda-export
117 :type 'boolean)
119 (defcustom org-agenda-exporter-settings nil
120 "Alist of variable/value pairs that should be active during agenda export.
121 This is a good place to set options for ps-print and for htmlize.
122 Note that the way this is implemented, the values will be evaluated
123 before assigned to the variables. So make sure to quote values you do
124 *not* want evaluated, for example
126 (setq org-agenda-exporter-settings
127 '((ps-print-color-p 'black-white)))"
128 :group 'org-agenda-export
129 :type '(repeat
130 (list
131 (variable)
132 (sexp :tag "Value"))))
134 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
135 "Hook run in temporary buffer before writing it to an export file.
136 A useful function is `org-agenda-add-entry-text'."
137 :group 'org-agenda-export
138 :type 'hook
139 :options '(org-agenda-add-entry-text))
141 (defcustom org-agenda-add-entry-text-maxlines 0
142 "Maximum number of entry text lines to be added to agenda.
143 This is only relevant when `org-agenda-add-entry-text' is part of
144 `org-agenda-before-write-hook', which it is by default.
145 When this is 0, nothing will happen. When it is greater than 0, it
146 specifies the maximum number of lines that will be added for each entry
147 that is listed in the agenda view.
149 Note that this variable is not used during display, only when exporting
150 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
151 and `org-agenda-entry-text-maxlines'."
152 :group 'org-agenda
153 :type 'integer)
155 (defcustom org-agenda-add-entry-text-descriptive-links t
156 "Non-nil means export org-links as descriptive links in agenda added text.
157 This variable applies to the text added to the agenda when
158 `org-agenda-add-entry-text-maxlines' is larger than 0.
159 When this variable nil, the URL will (also) be shown."
160 :group 'org-agenda
161 :type 'boolean)
163 (defcustom org-agenda-export-html-style ""
164 "The style specification for exported HTML Agenda files.
165 If this variable contains a string, it will replace the default <style>
166 section as produced by `htmlize'.
167 Since there are different ways of setting style information, this variable
168 needs to contain the full HTML structure to provide a style, including the
169 surrounding HTML tags. The style specifications should include definitions
170 the fonts used by the agenda, here is an example:
172 <style type=\"text/css\">
173 p { font-weight: normal; color: gray; }
174 .org-agenda-structure {
175 font-size: 110%;
176 color: #003399;
177 font-weight: 600;
179 .org-todo {
180 color: #cc6666;
181 font-weight: bold;
183 .org-agenda-done {
184 color: #339933;
186 .org-done {
187 color: #339933;
189 .title { text-align: center; }
190 .todo, .deadline { color: red; }
191 .done { color: green; }
192 </style>
194 or, if you want to keep the style in a file,
196 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
198 As the value of this option simply gets inserted into the HTML <head> header,
199 you can \"misuse\" it to also add other text to the header. However,
200 <style>...</style> is required, if not present the variable will be ignored."
201 :group 'org-agenda-export
202 :group 'org-export-html
203 :type 'string)
205 (defcustom org-agenda-persistent-filter nil
206 "When set, keep filters from one agenda view to the next."
207 :group 'org-agenda
208 :type 'boolean)
210 (defgroup org-agenda-custom-commands nil
211 "Options concerning agenda views in Org-mode."
212 :tag "Org Agenda Custom Commands"
213 :group 'org-agenda)
215 (defconst org-sorting-choice
216 '(choice
217 (const time-up) (const time-down)
218 (const category-keep) (const category-up) (const category-down)
219 (const tag-down) (const tag-up)
220 (const priority-up) (const priority-down)
221 (const todo-state-up) (const todo-state-down)
222 (const effort-up) (const effort-down)
223 (const habit-up) (const habit-down)
224 (const alpha-up) (const alpha-down)
225 (const user-defined-up) (const user-defined-down))
226 "Sorting choices.")
228 (defconst org-agenda-custom-commands-local-options
229 `(repeat :tag "Local settings for this command. Remember to quote values"
230 (choice :tag "Setting"
231 (list :tag "Heading for this block"
232 (const org-agenda-overriding-header)
233 (string :tag "Headline"))
234 (list :tag "Files to be searched"
235 (const org-agenda-files)
236 (list
237 (const :format "" quote)
238 (repeat (file))))
239 (list :tag "Sorting strategy"
240 (const org-agenda-sorting-strategy)
241 (list
242 (const :format "" quote)
243 (repeat
244 ,org-sorting-choice)))
245 (list :tag "Prefix format"
246 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
247 (string))
248 (list :tag "Number of days in agenda"
249 (const org-agenda-span)
250 (choice (const :tag "Day" 'day)
251 (const :tag "Week" 'week)
252 (const :tag "Month" 'month)
253 (const :tag "Year" 'year)
254 (integer :tag "Custom")))
255 (list :tag "Fixed starting date"
256 (const org-agenda-start-day)
257 (string :value "2007-11-01"))
258 (list :tag "Start on day of week"
259 (const org-agenda-start-on-weekday)
260 (choice :value 1
261 (const :tag "Today" nil)
262 (integer :tag "Weekday No.")))
263 (list :tag "Include data from diary"
264 (const org-agenda-include-diary)
265 (boolean))
266 (list :tag "Deadline Warning days"
267 (const org-deadline-warning-days)
268 (integer :value 1))
269 (list :tag "Tags filter preset"
270 (const org-agenda-filter-preset)
271 (list
272 (const :format "" quote)
273 (repeat
274 (string :tag "+tag or -tag"))))
275 (list :tag "Set daily/weekly entry types"
276 (const org-agenda-entry-types)
277 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
278 (const :deadline)
279 (const :scheduled)
280 (const :timestamp)
281 (const :sexp)))
282 (list :tag "Standard skipping condition"
283 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
284 (const org-agenda-skip-function)
285 (list
286 (const :format "" quote)
287 (list
288 (choice
289 :tag "Skipping range"
290 (const :tag "Skip entry" org-agenda-skip-entry-if)
291 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
292 (repeat :inline t :tag "Conditions for skipping"
293 (choice
294 :tag "Condition type"
295 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
296 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
297 (list :tag "TODO state is" :inline t
298 (const 'todo)
299 (choice
300 (const :tag "any not-done state" 'todo)
301 (const :tag "any done state" 'done)
302 (const :tag "any state" 'any)
303 (list :tag "Keyword list"
304 (const :format "" quote)
305 (repeat (string :tag "Keyword")))))
306 (list :tag "TODO state is not" :inline t
307 (const 'nottodo)
308 (choice
309 (const :tag "any not-done state" 'todo)
310 (const :tag "any done state" 'done)
311 (const :tag "any state" 'any)
312 (list :tag "Keyword list"
313 (const :format "" quote)
314 (repeat (string :tag "Keyword")))))
315 (const :tag "scheduled" 'scheduled)
316 (const :tag "not scheduled" 'notscheduled)
317 (const :tag "deadline" 'deadline)
318 (const :tag "no deadline" 'notdeadline)
319 (const :tag "timestamp" 'timestamp)
320 (const :tag "no timestamp" 'nottimestamp))))))
321 (list :tag "Non-standard skipping condition"
322 :value (org-agenda-skip-function)
323 (const org-agenda-skip-function)
324 (sexp :tag "Function or form (quoted!)"))
325 (list :tag "Any variable"
326 (variable :tag "Variable")
327 (sexp :tag "Value (sexp)"))))
328 "Selection of examples for agenda command settings.
329 This will be spliced into the custom type of
330 `org-agenda-custom-commands'.")
333 (defcustom org-agenda-custom-commands nil
334 "Custom commands for the agenda.
335 These commands will be offered on the splash screen displayed by the
336 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
338 (key desc type match settings files)
340 key The key (one or more characters as a string) to be associated
341 with the command.
342 desc A description of the command, when omitted or nil, a default
343 description is built using MATCH.
344 type The command type, any of the following symbols:
345 agenda The daily/weekly agenda.
346 todo Entries with a specific TODO keyword, in all agenda files.
347 search Entries containing search words entry or headline.
348 tags Tags/Property/TODO match in all agenda files.
349 tags-todo Tags/P/T match in all agenda files, TODO entries only.
350 todo-tree Sparse tree of specific TODO keyword in *current* file.
351 tags-tree Sparse tree with all tags matches in *current* file.
352 occur-tree Occur sparse tree for *current* file.
353 ... A user-defined function.
354 match What to search for:
355 - a single keyword for TODO keyword searches
356 - a tags match expression for tags searches
357 - a word search expression for text searches.
358 - a regular expression for occur searches
359 For all other commands, this should be the empty string.
360 settings A list of option settings, similar to that in a let form, so like
361 this: ((opt1 val1) (opt2 val2) ...). The values will be
362 evaluated at the moment of execution, so quote them when needed.
363 files A list of files file to write the produced agenda buffer to
364 with the command `org-store-agenda-views'.
365 If a file name ends in \".html\", an HTML version of the buffer
366 is written out. If it ends in \".ps\", a postscript version is
367 produced. Otherwise, only the plain text is written to the file.
369 You can also define a set of commands, to create a composite agenda buffer.
370 In this case, an entry looks like this:
372 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
374 where
376 desc A description string to be displayed in the dispatcher menu.
377 cmd An agenda command, similar to the above. However, tree commands
378 are no allowed, but instead you can get agenda and global todo list.
379 So valid commands for a set are:
380 (agenda \"\" settings)
381 (alltodo \"\" settings)
382 (stuck \"\" settings)
383 (todo \"match\" settings files)
384 (search \"match\" settings files)
385 (tags \"match\" settings files)
386 (tags-todo \"match\" settings files)
388 Each command can carry a list of options, and another set of options can be
389 given for the whole set of commands. Individual command options take
390 precedence over the general options.
392 When using several characters as key to a command, the first characters
393 are prefix commands. For the dispatcher to display useful information, you
394 should provide a description for the prefix, like
396 (setq org-agenda-custom-commands
397 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
398 (\"hl\" tags \"+HOME+Lisa\")
399 (\"hp\" tags \"+HOME+Peter\")
400 (\"hk\" tags \"+HOME+Kim\")))"
401 :group 'org-agenda-custom-commands
402 :type `(repeat
403 (choice :value ("x" "Describe command here" tags "" nil)
404 (list :tag "Single command"
405 (string :tag "Access Key(s) ")
406 (option (string :tag "Description"))
407 (choice
408 (const :tag "Agenda" agenda)
409 (const :tag "TODO list" alltodo)
410 (const :tag "Search words" search)
411 (const :tag "Stuck projects" stuck)
412 (const :tag "Tags/Property match (all agenda files)" tags)
413 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
414 (const :tag "TODO keyword search (all agenda files)" todo)
415 (const :tag "Tags sparse tree (current buffer)" tags-tree)
416 (const :tag "TODO keyword tree (current buffer)" todo-tree)
417 (const :tag "Occur tree (current buffer)" occur-tree)
418 (sexp :tag "Other, user-defined function"))
419 (string :tag "Match (only for some commands)")
420 ,org-agenda-custom-commands-local-options
421 (option (repeat :tag "Export" (file :tag "Export to"))))
422 (list :tag "Command series, all agenda files"
423 (string :tag "Access Key(s)")
424 (string :tag "Description ")
425 (repeat :tag "Component"
426 (choice
427 (list :tag "Agenda"
428 (const :format "" agenda)
429 (const :tag "" :format "" "")
430 ,org-agenda-custom-commands-local-options)
431 (list :tag "TODO list (all keywords)"
432 (const :format "" alltodo)
433 (const :tag "" :format "" "")
434 ,org-agenda-custom-commands-local-options)
435 (list :tag "Search words"
436 (const :format "" search)
437 (string :tag "Match")
438 ,org-agenda-custom-commands-local-options)
439 (list :tag "Stuck projects"
440 (const :format "" stuck)
441 (const :tag "" :format "" "")
442 ,org-agenda-custom-commands-local-options)
443 (list :tag "Tags search"
444 (const :format "" tags)
445 (string :tag "Match")
446 ,org-agenda-custom-commands-local-options)
447 (list :tag "Tags search, TODO entries only"
448 (const :format "" tags-todo)
449 (string :tag "Match")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "TODO keyword search"
452 (const :format "" todo)
453 (string :tag "Match")
454 ,org-agenda-custom-commands-local-options)
455 (list :tag "Other, user-defined function"
456 (symbol :tag "function")
457 (string :tag "Match")
458 ,org-agenda-custom-commands-local-options)))
460 (repeat :tag "Settings for entire command set"
461 (list (variable :tag "Any variable")
462 (sexp :tag "Value")))
463 (option (repeat :tag "Export" (file :tag "Export to"))))
464 (cons :tag "Prefix key documentation"
465 (string :tag "Access Key(s)")
466 (string :tag "Description ")))))
468 (defcustom org-agenda-query-register ?o
469 "The register holding the current query string.
470 The purpose of this is that if you construct a query string interactively,
471 you can then use it to define a custom command."
472 :group 'org-agenda-custom-commands
473 :type 'character)
475 (defcustom org-stuck-projects
476 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
477 "How to identify stuck projects.
478 This is a list of four items:
479 1. A tags/todo/property matcher string that is used to identify a project.
480 See the manual for a description of tag and property searches.
481 The entire tree below a headline matched by this is considered one project.
482 2. A list of TODO keywords identifying non-stuck projects.
483 If the project subtree contains any headline with one of these todo
484 keywords, the project is considered to be not stuck. If you specify
485 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
486 3. A list of tags identifying non-stuck projects.
487 If the project subtree contains any headline with one of these tags,
488 the project is considered to be not stuck. If you specify \"*\" as
489 a tag, any tag will mark the project unstuck. Note that this is about
490 the explicit presence of a tag somewhere in the subtree, inherited
491 tags to not count here. If inherited tags make a project not stuck,
492 use \"-TAG\" in the tags part of the matcher under (1.) above.
493 4. An arbitrary regular expression matching non-stuck projects.
495 If the project turns out to be not stuck, search continues also in the
496 subtree to see if any of the subtasks have project status.
498 See also the variable `org-tags-match-list-sublevels' which applies
499 to projects matched by this search as well.
501 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
502 or `C-c a #' to produce the list."
503 :group 'org-agenda-custom-commands
504 :type '(list
505 (string :tag "Tags/TODO match to identify a project")
506 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
507 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
508 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
510 (defcustom org-agenda-filter-effort-default-operator "<"
511 "The default operator for effort estimate filtering.
512 If you select an effort estimate limit without first pressing an operator,
513 this one will be used."
514 :group 'org-agenda-custom-commands
515 :type '(choice (const :tag "less or equal" "<")
516 (const :tag "greater or equal"">")
517 (const :tag "equal" "=")))
519 (defgroup org-agenda-skip nil
520 "Options concerning skipping parts of agenda files."
521 :tag "Org Agenda Skip"
522 :group 'org-agenda)
524 (defcustom org-agenda-skip-function-global nil
525 "Function to be called at each match during agenda construction.
526 If this function returns nil, the current match should not be skipped.
527 If the function decided to skip an agenda match, is must return the
528 buffer position from which the search should be continued.
529 This may also be a Lisp form, which will be evaluated.
531 This variable will be applied to every agenda match, including
532 tags/property searches and TODO lists. So try to make the test function
533 do its checking as efficiently as possible. To implement a skipping
534 condition just for specific agenda commands, use the variable
535 `org-agenda-skip-function' which can be set in the options section
536 of custom agenda commands."
537 :group 'org-agenda-skip
538 :type 'sexp)
540 (defgroup org-agenda-daily/weekly nil
541 "Options concerning the daily/weekly agenda."
542 :tag "Org Agenda Daily/Weekly"
543 :group 'org-agenda)
544 (defgroup org-agenda-todo-list nil
545 "Options concerning the global todo list agenda view."
546 :tag "Org Agenda Todo List"
547 :group 'org-agenda)
548 (defgroup org-agenda-match-view nil
549 "Options concerning the general tags/property/todo match agenda view."
550 :tag "Org Agenda Match View"
551 :group 'org-agenda)
552 (defgroup org-agenda-search-view nil
553 "Options concerning the general tags/property/todo match agenda view."
554 :tag "Org Agenda Match View"
555 :group 'org-agenda)
557 (defvar org-agenda-archives-mode nil
558 "Non-nil means the agenda will include archived items.
559 If this is the symbol `trees', trees in the selected agenda scope
560 that are marked with the ARCHIVE tag will be included anyway. When this is
561 t, also all archive files associated with the current selection of agenda
562 files will be included.")
564 (defcustom org-agenda-skip-comment-trees t
565 "Non-nil means skip trees that start with the COMMENT keyword.
566 When nil, these trees are also scanned by agenda commands."
567 :group 'org-agenda-skip
568 :type 'boolean)
570 (defcustom org-agenda-todo-list-sublevels t
571 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
572 When nil, the sublevels of a TODO entry are not checked, resulting in
573 potentially much shorter TODO lists."
574 :group 'org-agenda-skip
575 :group 'org-agenda-todo-list
576 :type 'boolean)
578 (defcustom org-agenda-todo-ignore-with-date nil
579 "Non-nil means don't show entries with a date in the global todo list.
580 You can use this if you prefer to mark mere appointments with a TODO keyword,
581 but don't want them to show up in the TODO list.
582 When this is set, it also covers deadlines and scheduled items, the settings
583 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
584 will be ignored.
585 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
586 :group 'org-agenda-skip
587 :group 'org-agenda-todo-list
588 :type 'boolean)
590 (defcustom org-agenda-todo-ignore-timestamp nil
591 "Non-nil means don't show entries with a timestamp.
592 This applies when creating the global todo list.
593 Valid values are:
595 past Don't show entries for today or in the past.
597 future Don't show entries with a timestamp in the future.
598 The idea behind this is that if it has a future
599 timestamp, you don't want to think about it until the
600 date.
602 all Don't show any entries with a timestamp in the global todo list.
603 The idea behind this is that by setting a timestamp, you
604 have already \"taken care\" of this item.
606 This variable can also have an integer as a value. If positive (N),
607 todos with a timestamp N or more days in the future will be ignored. If
608 negative (-N), todos with a timestamp N or more days in the past will be
609 ignored. If 0, todos with a timestamp either today or in the future will
610 be ignored. For example, a value of -1 will exclude todos with a
611 timestamp in the past (yesterday or earlier), while a value of 7 will
612 exclude todos with a timestamp a week or more in the future.
614 See also `org-agenda-todo-ignore-with-date'.
615 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
616 to make his option also apply to the tags-todo list."
617 :group 'org-agenda-skip
618 :group 'org-agenda-todo-list
619 :type '(choice
620 (const :tag "Ignore future timestamp todos" future)
621 (const :tag "Ignore past or present timestamp todos" past)
622 (const :tag "Ignore all timestamp todos" all)
623 (const :tag "Show timestamp todos" nil)
624 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
626 (defcustom org-agenda-todo-ignore-scheduled nil
627 "Non-nil means, ignore some scheduled TODO items when making TODO list.
628 This applies when creating the global todo list.
629 Valid values are:
631 past Don't show entries scheduled today or in the past.
633 future Don't show entries scheduled in the future.
634 The idea behind this is that by scheduling it, you don't want to
635 think about it until the scheduled date.
637 all Don't show any scheduled entries in the global todo list.
638 The idea behind this is that by scheduling it, you have already
639 \"taken care\" of this item.
641 t Same as `all', for backward compatibility.
643 This variable can also have an integer as a value. See
644 `org-agenda-todo-ignore-timestamp' for more details.
646 See also `org-agenda-todo-ignore-with-date'.
647 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
648 to make his option also apply to the tags-todo list."
649 :group 'org-agenda-skip
650 :group 'org-agenda-todo-list
651 :type '(choice
652 (const :tag "Ignore future-scheduled todos" future)
653 (const :tag "Ignore past- or present-scheduled todos" past)
654 (const :tag "Ignore all scheduled todos" all)
655 (const :tag "Ignore all scheduled todos (compatibility)" t)
656 (const :tag "Show scheduled todos" nil)
657 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
659 (defcustom org-agenda-todo-ignore-deadlines nil
660 "Non-nil means ignore some deadlined TODO items when making TODO list.
661 There are different motivations for using different values, please think
662 carefully when configuring this variable.
664 This applies when creating the global todo list.
665 Valid values are:
667 near Don't show near deadline entries. A deadline is near when it is
668 closer than `org-deadline-warning-days' days. The idea behind this
669 is that such items will appear in the agenda anyway.
671 far Don't show TODO entries where a deadline has been defined, but
672 the deadline is not near. This is useful if you don't want to
673 use the todo list to figure out what to do now.
675 past Don't show entries with a deadline timestamp for today or in the past.
677 future Don't show entries with a deadline timestamp in the future, not even
678 when they become `near' ones. Use it with caution.
680 all Ignore all TODO entries that do have a deadline.
682 t Same as `near', for backward compatibility.
684 This variable can also have an integer as a value. See
685 `org-agenda-todo-ignore-timestamp' for more details.
687 See also `org-agenda-todo-ignore-with-date'.
688 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
689 to make his option also apply to the tags-todo list."
690 :group 'org-agenda-skip
691 :group 'org-agenda-todo-list
692 :type '(choice
693 (const :tag "Ignore near deadlines" near)
694 (const :tag "Ignore near deadlines (compatibility)" t)
695 (const :tag "Ignore far deadlines" far)
696 (const :tag "Ignore all TODOs with a deadlines" all)
697 (const :tag "Show all TODOs, even if they have a deadline" nil)
698 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
700 (defcustom org-agenda-tags-todo-honor-ignore-options nil
701 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
702 The variables
703 `org-agenda-todo-ignore-with-date',
704 `org-agenda-todo-ignore-timestamp',
705 `org-agenda-todo-ignore-scheduled',
706 `org-agenda-todo-ignore-deadlines'
707 make the global TODO list skip entries that have time stamps of certain
708 kinds. If this option is set, the same options will also apply for the
709 tags-todo search, which is the general tags/property matcher
710 restricted to unfinished TODO entries only."
711 :group 'org-agenda-skip
712 :group 'org-agenda-todo-list
713 :group 'org-agenda-match-view
714 :type 'boolean)
716 (defcustom org-agenda-skip-scheduled-if-done nil
717 "Non-nil means don't show scheduled items in agenda when they are done.
718 This is relevant for the daily/weekly agenda, not for the TODO list. And
719 it applies only to the actual date of the scheduling. Warnings about
720 an item with a past scheduling dates are always turned off when the item
721 is DONE."
722 :group 'org-agenda-skip
723 :group 'org-agenda-daily/weekly
724 :type 'boolean)
726 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
727 "Non-nil means skip scheduling line if same entry shows because of deadline.
728 In the agenda of today, an entry can show up multiple times because
729 it is both scheduled and has a nearby deadline, and maybe a plain time
730 stamp as well.
731 When this variable is t, then only the deadline is shown and the fact that
732 the entry is scheduled today or was scheduled previously is not shown.
733 When this variable is nil, the entry will be shown several times. When
734 the variable is the symbol `not-today', then skip scheduled previously,
735 but not scheduled today."
736 :group 'org-agenda-skip
737 :group 'org-agenda-daily/weekly
738 :type '(choice
739 (const :tag "Never" nil)
740 (const :tag "Always" t)
741 (const :tag "Not when scheduled today" not-today)))
743 (defcustom org-agenda-skip-deadline-if-done nil
744 "Non-nil means don't show deadlines when the corresponding item is done.
745 When nil, the deadline is still shown and should give you a happy feeling.
746 This is relevant for the daily/weekly agenda. And it applied only to the
747 actually date of the deadline. Warnings about approaching and past-due
748 deadlines are always turned off when the item is DONE."
749 :group 'org-agenda-skip
750 :group 'org-agenda-daily/weekly
751 :type 'boolean)
753 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
754 "Non-nil means skip deadline prewarning when entry is also scheduled.
755 This will apply on all days where a prewarning for the deadline would
756 be shown, but not at the day when the entry is actually due. On that day,
757 the deadline will be shown anyway.
758 This variable may be set to nil, t, or a number which will then give
759 the number of days before the actual deadline when the prewarnings
760 should resume.
761 This can be used in a workflow where the first showing of the deadline will
762 trigger you to schedule it, and then you don't want to be reminded of it
763 because you will take care of it on the day when scheduled."
764 :group 'org-agenda-skip
765 :group 'org-agenda-daily/weekly
766 :type '(choice
767 (const :tag "Alwas show prewarning" nil)
768 (const :tag "Remove prewarning if entry is scheduled" t)
769 (integer :tag "Restart prewarning N days before deadline")))
771 (defcustom org-agenda-skip-additional-timestamps-same-entry t
772 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
773 When non-nil, after the search for timestamps has matched once in an
774 entry, the rest of the entry will not be searched."
775 :group 'org-agenda-skip
776 :type 'boolean)
778 (defcustom org-agenda-skip-timestamp-if-done nil
779 "Non-nil means don't select item by timestamp or -range if it is DONE."
780 :group 'org-agenda-skip
781 :group 'org-agenda-daily/weekly
782 :type 'boolean)
784 (defcustom org-agenda-dim-blocked-tasks t
785 "Non-nil means dim blocked tasks in the agenda display.
786 This causes some overhead during agenda construction, but if you
787 have turned on `org-enforce-todo-dependencies',
788 `org-enforce-todo-checkbox-dependencies', or any other blocking
789 mechanism, this will create useful feedback in the agenda.
791 Instead of t, this variable can also have the value `invisible'.
792 Then blocked tasks will be invisible and only become visible when
793 they become unblocked. An exemption to this behavior is when a task is
794 blocked because of unchecked checkboxes below it. Since checkboxes do
795 not show up in the agenda views, making this task invisible you remove any
796 trace from agenda views that there is something to do. Therefore, a task
797 that is blocked because of checkboxes will never be made invisible, it
798 will only be dimmed."
799 :group 'org-agenda-daily/weekly
800 :group 'org-agenda-todo-list
801 :type '(choice
802 (const :tag "Do not dim" nil)
803 (const :tag "Dim to a grey face" t)
804 (const :tag "Make invisible" invisible)))
806 (defcustom org-timeline-show-empty-dates 3
807 "Non-nil means `org-timeline' also shows dates without an entry.
808 When nil, only the days which actually have entries are shown.
809 When t, all days between the first and the last date are shown.
810 When an integer, show also empty dates, but if there is a gap of more than
811 N days, just insert a special line indicating the size of the gap."
812 :group 'org-agenda-skip
813 :type '(choice
814 (const :tag "None" nil)
815 (const :tag "All" t)
816 (integer :tag "at most")))
818 (defgroup org-agenda-startup nil
819 "Options concerning initial settings in the Agenda in Org Mode."
820 :tag "Org Agenda Startup"
821 :group 'org-agenda)
823 (defcustom org-agenda-menu-show-matcher t
824 "Non-nil means show the match string in the agenda dispatcher menu.
825 When nil, the matcher string is not shown, but is put into the help-echo
826 property so than moving the mouse over the command shows it.
827 Setting it to nil is good if matcher strings are very long and/or if
828 you want to use two-column display (see `org-agenda-menu-two-column')."
829 :group 'org-agenda
830 :type 'boolean)
832 (defcustom org-agenda-menu-two-column nil
833 "Non-nil means, use two columns to show custom commands in the dispatcher.
834 If you use this, you probably want to set `org-agenda-menu-show-matcher'
835 to nil."
836 :group 'org-agenda
837 :type 'boolean)
839 (defcustom org-finalize-agenda-hook nil
840 "Hook run just before displaying an agenda buffer."
841 :group 'org-agenda-startup
842 :type 'hook)
844 (defcustom org-agenda-mouse-1-follows-link nil
845 "Non-nil means mouse-1 on a link will follow the link in the agenda.
846 A longer mouse click will still set point. Does not work on XEmacs.
847 Needs to be set before org.el is loaded."
848 :group 'org-agenda-startup
849 :type 'boolean)
851 (defcustom org-agenda-start-with-follow-mode nil
852 "The initial value of follow mode in a newly created agenda window."
853 :group 'org-agenda-startup
854 :type 'boolean)
856 (defcustom org-agenda-show-outline-path t
857 "Non-nil means show outline path in echo area after line motion."
858 :group 'org-agenda-startup
859 :type 'boolean)
861 (defcustom org-agenda-start-with-entry-text-mode nil
862 "The initial value of entry-text-mode in a newly created agenda window."
863 :group 'org-agenda-startup
864 :type 'boolean)
866 (defcustom org-agenda-entry-text-maxlines 5
867 "Number of text lines to be added when `E' is pressed in the agenda.
869 Note that this variable only used during agenda display. Add add entry text
870 when exporting the agenda, configure the variable
871 `org-agenda-add-entry-ext-maxlines'."
872 :group 'org-agenda
873 :type 'integer)
875 (defcustom org-agenda-entry-text-exclude-regexps nil
876 "List of regular expressions to clean up entry text.
877 The complete matches of all regular expressions in this list will be
878 removed from entry text before it is shown in the agenda."
879 :group 'org-agenda
880 :type '(repeat (regexp)))
882 (defvar org-agenda-entry-text-cleanup-hook nil
883 "Hook that is run after basic cleanup of entry text to be shown in agenda.
884 This cleanup is done in a temporary buffer, so the function may inspect and
885 change the entire buffer.
886 Some default stuff like drawers and scheduling/deadline dates will already
887 have been removed when this is called, as will any matches for regular
888 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
890 (defvar org-agenda-include-inactive-timestamps nil
891 "Non-nil means include inactive time stamps in agenda and timeline.")
893 (defgroup org-agenda-windows nil
894 "Options concerning the windows used by the Agenda in Org Mode."
895 :tag "Org Agenda Windows"
896 :group 'org-agenda)
898 (defcustom org-agenda-window-setup 'reorganize-frame
899 "How the agenda buffer should be displayed.
900 Possible values for this option are:
902 current-window Show agenda in the current window, keeping all other windows.
903 other-window Use `switch-to-buffer-other-window' to display agenda.
904 reorganize-frame Show only two windows on the current frame, the current
905 window and the agenda.
906 other-frame Use `switch-to-buffer-other-frame' to display agenda.
907 Also, when exiting the agenda, kill that frame.
908 See also the variable `org-agenda-restore-windows-after-quit'."
909 :group 'org-agenda-windows
910 :type '(choice
911 (const current-window)
912 (const other-frame)
913 (const other-window)
914 (const reorganize-frame)))
916 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
917 "The min and max height of the agenda window as a fraction of frame height.
918 The value of the variable is a cons cell with two numbers between 0 and 1.
919 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
920 :group 'org-agenda-windows
921 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
923 (defcustom org-agenda-restore-windows-after-quit nil
924 "Non-nil means restore window configuration upon exiting agenda.
925 Before the window configuration is changed for displaying the agenda,
926 the current status is recorded. When the agenda is exited with
927 `q' or `x' and this option is set, the old state is restored. If
928 `org-agenda-window-setup' is `other-frame', the value of this
929 option will be ignored."
930 :group 'org-agenda-windows
931 :type 'boolean)
933 (defcustom org-agenda-ndays nil
934 "Number of days to include in overview display.
935 Should be 1 or 7.
936 Obsolete, see `org-agenda-span'."
937 :group 'org-agenda-daily/weekly
938 :type 'integer)
940 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
942 (defcustom org-agenda-span 'week
943 "Number of days to include in overview display.
944 Can be day, week, month, year, or any number of days.
945 Custom commands can set this variable in the options section."
946 :group 'org-agenda-daily/weekly
947 :type '(choice (const :tag "Day" day)
948 (const :tag "Week" week)
949 (const :tag "Month" month)
950 (const :tag "Year" year)
951 (integer :tag "Custom")))
953 (defcustom org-agenda-start-on-weekday 1
954 "Non-nil means start the overview always on the specified weekday.
955 0 denotes Sunday, 1 denotes Monday etc.
956 When nil, always start on the current day.
957 Custom commands can set this variable in the options section."
958 :group 'org-agenda-daily/weekly
959 :type '(choice (const :tag "Today" nil)
960 (integer :tag "Weekday No.")))
962 (defcustom org-agenda-show-all-dates t
963 "Non-nil means `org-agenda' shows every day in the selected range.
964 When nil, only the days which actually have entries are shown."
965 :group 'org-agenda-daily/weekly
966 :type 'boolean)
968 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
969 "Format string for displaying dates in the agenda.
970 Used by the daily/weekly agenda and by the timeline. This should be
971 a format string understood by `format-time-string', or a function returning
972 the formatted date as a string. The function must take a single argument,
973 a calendar-style date list like (month day year)."
974 :group 'org-agenda-daily/weekly
975 :type '(choice
976 (string :tag "Format string")
977 (function :tag "Function")))
979 (defun org-agenda-format-date-aligned (date)
980 "Format a date string for display in the daily/weekly agenda, or timeline.
981 This function makes sure that dates are aligned for easy reading."
982 (require 'cal-iso)
983 (let* ((dayname (calendar-day-name date))
984 (day (cadr date))
985 (day-of-week (calendar-day-of-week date))
986 (month (car date))
987 (monthname (calendar-month-name month))
988 (year (nth 2 date))
989 (iso-week (org-days-to-iso-week
990 (calendar-absolute-from-gregorian date)))
991 (weekyear (cond ((and (= month 1) (>= iso-week 52))
992 (1- year))
993 ((and (= month 12) (<= iso-week 1))
994 (1+ year))
995 (t year)))
996 (weekstring (if (= day-of-week 1)
997 (format " W%02d" iso-week)
998 "")))
999 (format "%-10s %2d %s %4d%s"
1000 dayname day monthname year weekstring)))
1002 (defcustom org-agenda-time-leading-zero nil
1003 "Non-nil means use leading zero for military times in agenda.
1004 For example, 9:30am would become 09:30 rather than 9:30."
1005 :group 'org-agenda-daily/weekly
1006 :type 'boolean)
1008 (defcustom org-agenda-timegrid-use-ampm nil
1009 "When set, show AM/PM style timestamps on the timegrid."
1010 :group 'org-agenda
1011 :type 'boolean)
1013 (defun org-agenda-time-of-day-to-ampm (time)
1014 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1015 (let* ((hour-number (string-to-number (substring time 0 -3)))
1016 (minute (substring time -2))
1017 (ampm "am"))
1018 (cond
1019 ((equal hour-number 12)
1020 (setq ampm "pm"))
1021 ((> hour-number 12)
1022 (setq ampm "pm")
1023 (setq hour-number (- hour-number 12))))
1024 (concat
1025 (if org-agenda-time-leading-zero
1026 (format "%02d" hour-number)
1027 (format "%02s" (number-to-string hour-number)))
1028 ":" minute ampm)))
1030 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1031 "Conditionally convert TIME to AM/PM format
1032 based on `org-agenda-timegrid-use-ampm'"
1033 (if org-agenda-timegrid-use-ampm
1034 (org-agenda-time-of-day-to-ampm time)
1035 time))
1037 (defcustom org-agenda-weekend-days '(6 0)
1038 "Which days are weekend?
1039 These days get the special face `org-agenda-date-weekend' in the agenda
1040 and timeline buffers."
1041 :group 'org-agenda-daily/weekly
1042 :type '(set :greedy t
1043 (const :tag "Monday" 1)
1044 (const :tag "Tuesday" 2)
1045 (const :tag "Wednesday" 3)
1046 (const :tag "Thursday" 4)
1047 (const :tag "Friday" 5)
1048 (const :tag "Saturday" 6)
1049 (const :tag "Sunday" 0)))
1051 (defcustom org-agenda-include-diary nil
1052 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1053 Custom commands can set this variable in the options section."
1054 :group 'org-agenda-daily/weekly
1055 :type 'boolean)
1057 (defcustom org-agenda-include-deadlines t
1058 "If non-nil, include entries within their deadline warning period.
1059 Custom commands can set this variable in the options section."
1060 :group 'org-agenda-daily/weekly
1061 :type 'boolean)
1063 (defcustom org-agenda-include-all-todo nil
1064 "Set means weekly/daily agenda will always contain all TODO entries.
1065 The TODO entries will be listed at the top of the agenda, before
1066 the entries for specific days.
1067 This option is deprecated, it is better to define a block agenda instead."
1068 :group 'org-agenda-daily/weekly
1069 :type 'boolean)
1071 (defcustom org-agenda-repeating-timestamp-show-all t
1072 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1073 When set to a list of strings, only show occurrences of repeating
1074 stamps for these TODO keywords. When nil, only one occurrence is
1075 shown, either today or the nearest into the future."
1076 :group 'org-agenda-daily/weekly
1077 :type '(choice
1078 (const :tag "Show repeating stamps" t)
1079 (repeat :tag "Show repeating stamps for these TODO keywords"
1080 (string :tag "TODO Keyword"))
1081 (const :tag "Don't show repeating stamps" nil)))
1083 (defcustom org-scheduled-past-days 10000
1084 "No. of days to continue listing scheduled items that are not marked DONE.
1085 When an item is scheduled on a date, it shows up in the agenda on this
1086 day and will be listed until it is marked done for the number of days
1087 given here."
1088 :group 'org-agenda-daily/weekly
1089 :type 'integer)
1091 (defcustom org-agenda-log-mode-items '(closed clock)
1092 "List of items that should be shown in agenda log mode.
1093 This list may contain the following symbols:
1095 closed Show entries that have been closed on that day.
1096 clock Show entries that have received clocked time on that day.
1097 state Show all logged state changes.
1098 Note that instead of changing this variable, you can also press `C-u l' in
1099 the agenda to display all available LOG items temporarily."
1100 :group 'org-agenda-daily/weekly
1101 :type '(set :greedy t (const closed) (const clock) (const state)))
1103 (defcustom org-agenda-clock-consistency-checks
1104 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1105 :gap-ok-around ("4:00")
1106 :default-face ((:background "DarkRed") (:foreground "white"))
1107 :overlap-face nil :gap-face nil :no-end-time-face nil
1108 :long-face nil :short-face nil)
1109 "This is a property list, with the following keys:
1111 :max-duration Mark clocking chunks that are longer than this time.
1112 This is a time string like \"HH:MM\", or the number
1113 of minutes as an integer.
1115 :min-duration Mark clocking chunks that are shorter that this.
1116 This is a time string like \"HH:MM\", or the number
1117 of minutes as an integer.
1119 :max-gap Mark gaps between clocking chunks that are longer than
1120 this duration. A number of minutes, or a string
1121 like \"HH:MM\".
1123 :gap-ok-around List of times during the day which are usually not working
1124 times. When a gap is detected, but the gap contains any
1125 of these times, the gap is *not* reported. For example,
1126 if this is (\"4:00\" \"13:00\") then gaps that contain
1127 4:00 in the morning (i.e. the night) and 13:00
1128 (i.e. a typical lunch time) do not cause a warning.
1129 You should have at least one time during the night in this
1130 list, or otherwise the first task each morning will trigger
1131 a warning because it follows a long gap.
1133 Furthermore, the following properties can be used to define faces for
1134 issue display.
1136 :default-face the default face, if the specific face is undefined
1137 :overlap-face face for overlapping clocks
1138 :gap-face face for gaps between clocks
1139 :no-end-time-face face for incomplete clocks
1140 :long-face face for clock intervals that are too long
1141 :short-face face for clock intervals that are too short"
1142 :group 'org-agenda-daily/weekly
1143 :group 'org-clock
1144 :type 'plist)
1146 (defcustom org-agenda-log-mode-add-notes t
1147 "Non-nil means add first line of notes to log entries in agenda views.
1148 If a log item like a state change or a clock entry is associated with
1149 notes, the first line of these notes will be added to the entry in the
1150 agenda display."
1151 :group 'org-agenda-daily/weekly
1152 :type 'boolean)
1154 (defcustom org-agenda-start-with-log-mode nil
1155 "The initial value of log-mode in a newly created agenda window."
1156 :group 'org-agenda-startup
1157 :group 'org-agenda-daily/weekly
1158 :type 'boolean)
1160 (defcustom org-agenda-start-with-clockreport-mode nil
1161 "The initial value of clockreport-mode in a newly created agenda window."
1162 :group 'org-agenda-startup
1163 :group 'org-agenda-daily/weekly
1164 :type 'boolean)
1166 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1167 "Property list with parameters for the clocktable in clockreport mode.
1168 This is the display mode that shows a clock table in the daily/weekly
1169 agenda, the properties for this dynamic block can be set here.
1170 The usual clocktable parameters are allowed here, but you cannot set
1171 the properties :name, :tstart, :tend, :block, and :scope - these will
1172 be overwritten to make sure the content accurately reflects the
1173 current display in the agenda."
1174 :group 'org-agenda-daily/weekly
1175 :type 'plist)
1177 (defcustom org-agenda-search-view-always-boolean nil
1178 "Non-nil means the search string is interpreted as individual parts.
1180 The search string for search view can either be interpreted as a phrase,
1181 or as a list of snippets that define a boolean search for a number of
1182 strings.
1184 When this is non-nil, the string will be split on whitespace, and each
1185 snippet will be searched individually, and all must match in order to
1186 select an entry. A snippet is then a single string of non-white
1187 characters, or a string in double quotes, or a regexp in {} braces.
1188 If a snippet is preceded by \"-\", the snippet must *not* match.
1189 \"+\" is syntactic sugar for positive selection. Each snippet may
1190 be found as a full word or a partial word, but see the variable
1191 `org-agenda-search-view-force-full-words'.
1193 When this is nil, search will look for the entire search phrase as one,
1194 with each space character matching any amount of whitespace, including
1195 line breaks.
1197 Even when this is nil, you can still switch to Boolean search dynamically
1198 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1199 is a regexp marked with braces like \"{abc}\", this will also switch to
1200 boolean search."
1201 :group 'org-agenda-search-view
1202 :type 'boolean)
1204 (if (fboundp 'defvaralias)
1205 (defvaralias 'org-agenda-search-view-search-words-only
1206 'org-agenda-search-view-always-boolean))
1208 (defcustom org-agenda-search-view-force-full-words nil
1209 "Non-nil means, search words must be matches as complete words.
1210 When nil, they may also match part of a word."
1211 :group 'org-agenda-search-view
1212 :type 'boolean)
1214 (defgroup org-agenda-time-grid nil
1215 "Options concerning the time grid in the Org-mode Agenda."
1216 :tag "Org Agenda Time Grid"
1217 :group 'org-agenda)
1219 (defcustom org-agenda-search-headline-for-time t
1220 "Non-nil means search headline for a time-of-day.
1221 If the headline contains a time-of-day in one format or another, it will
1222 be used to sort the entry into the time sequence of items for a day.
1223 Some people have time stamps in the headline that refer to the creation
1224 time or so, and then this produces an unwanted side effect. If this is
1225 the case for your, use this variable to turn off searching the headline
1226 for a time."
1227 :group 'org-agenda-time-grid
1228 :type 'boolean)
1230 (defcustom org-agenda-use-time-grid t
1231 "Non-nil means show a time grid in the agenda schedule.
1232 A time grid is a set of lines for specific times (like every two hours between
1233 8:00 and 20:00). The items scheduled for a day at specific times are
1234 sorted in between these lines.
1235 For details about when the grid will be shown, and what it will look like, see
1236 the variable `org-agenda-time-grid'."
1237 :group 'org-agenda-time-grid
1238 :type 'boolean)
1240 (defcustom org-agenda-time-grid
1241 '((daily today require-timed)
1242 "----------------"
1243 (800 1000 1200 1400 1600 1800 2000))
1245 "The settings for time grid for agenda display.
1246 This is a list of three items. The first item is again a list. It contains
1247 symbols specifying conditions when the grid should be displayed:
1249 daily if the agenda shows a single day
1250 weekly if the agenda shows an entire week
1251 today show grid on current date, independent of daily/weekly display
1252 require-timed show grid only if at least one item has a time specification
1254 The second item is a string which will be placed behind the grid time.
1256 The third item is a list of integers, indicating the times that should have
1257 a grid line."
1258 :group 'org-agenda-time-grid
1259 :type
1260 '(list
1261 (set :greedy t :tag "Grid Display Options"
1262 (const :tag "Show grid in single day agenda display" daily)
1263 (const :tag "Show grid in weekly agenda display" weekly)
1264 (const :tag "Always show grid for today" today)
1265 (const :tag "Show grid only if any timed entries are present"
1266 require-timed)
1267 (const :tag "Skip grid times already present in an entry"
1268 remove-match))
1269 (string :tag "Grid String")
1270 (repeat :tag "Grid Times" (integer :tag "Time"))))
1272 (defcustom org-agenda-show-current-time-in-grid t
1273 "Non-nil means show the current time in the time grid."
1274 :group 'org-agenda-time-grid
1275 :type 'boolean)
1277 (defcustom org-agenda-current-time-string
1278 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1279 "The string for the current time marker in the agenda."
1280 :group 'org-agenda-time-grid
1281 :type 'string)
1283 (defgroup org-agenda-sorting nil
1284 "Options concerning sorting in the Org-mode Agenda."
1285 :tag "Org Agenda Sorting"
1286 :group 'org-agenda)
1288 (defcustom org-agenda-sorting-strategy
1289 '((agenda habit-down time-up priority-down category-keep)
1290 (todo priority-down category-keep)
1291 (tags priority-down category-keep)
1292 (search category-keep))
1293 "Sorting structure for the agenda items of a single day.
1294 This is a list of symbols which will be used in sequence to determine
1295 if an entry should be listed before another entry. The following
1296 symbols are recognized:
1298 time-up Put entries with time-of-day indications first, early first
1299 time-down Put entries with time-of-day indications first, late first
1300 category-keep Keep the default order of categories, corresponding to the
1301 sequence in `org-agenda-files'.
1302 category-up Sort alphabetically by category, A-Z.
1303 category-down Sort alphabetically by category, Z-A.
1304 tag-up Sort alphabetically by last tag, A-Z.
1305 tag-down Sort alphabetically by last tag, Z-A.
1306 priority-up Sort numerically by priority, high priority last.
1307 priority-down Sort numerically by priority, high priority first.
1308 todo-state-up Sort by todo state, tasks that are done last.
1309 todo-state-down Sort by todo state, tasks that are done first.
1310 effort-up Sort numerically by estimated effort, high effort last.
1311 effort-down Sort numerically by estimated effort, high effort first.
1312 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1313 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1314 habit-up Put entries that are habits first
1315 habit-down Put entries that are habits last
1316 alpha-up Sort headlines alphabetically
1317 alpha-down Sort headlines alphabetically, reversed
1319 The different possibilities will be tried in sequence, and testing stops
1320 if one comparison returns a \"not-equal\". For example, the default
1321 '(time-up category-keep priority-down)
1322 means: Pull out all entries having a specified time of day and sort them,
1323 in order to make a time schedule for the current day the first thing in the
1324 agenda listing for the day. Of the entries without a time indication, keep
1325 the grouped in categories, don't sort the categories, but keep them in
1326 the sequence given in `org-agenda-files'. Within each category sort by
1327 priority.
1329 Leaving out `category-keep' would mean that items will be sorted across
1330 categories by priority.
1332 Instead of a single list, this can also be a set of list for specific
1333 contents, with a context symbol in the car of the list, any of
1334 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1336 Custom commands can bind this variable in the options section."
1337 :group 'org-agenda-sorting
1338 :type `(choice
1339 (repeat :tag "General" ,org-sorting-choice)
1340 (list :tag "Individually"
1341 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1342 (repeat ,org-sorting-choice))
1343 (cons (const :tag "Strategy for TODO lists" todo)
1344 (repeat ,org-sorting-choice))
1345 (cons (const :tag "Strategy for Tags matches" tags)
1346 (repeat ,org-sorting-choice))
1347 (cons (const :tag "Strategy for search matches" search)
1348 (repeat ,org-sorting-choice)))))
1350 (defcustom org-agenda-cmp-user-defined nil
1351 "A function to define the comparison `user-defined'.
1352 This function must receive two arguments, agenda entry a and b.
1353 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1354 the user comparison, return nil.
1355 When this is defined, you can make `user-defined-up' and `user-defined-down'
1356 part of an agenda sorting strategy."
1357 :group 'org-agenda-sorting
1358 :type 'symbol)
1360 (defcustom org-sort-agenda-notime-is-late t
1361 "Non-nil means items without time are considered late.
1362 This is only relevant for sorting. When t, items which have no explicit
1363 time like 15:30 will be considered as 99:01, i.e. later than any items which
1364 do have a time. When nil, the default time is before 0:00. You can use this
1365 option to decide if the schedule for today should come before or after timeless
1366 agenda entries."
1367 :group 'org-agenda-sorting
1368 :type 'boolean)
1370 (defcustom org-sort-agenda-noeffort-is-high t
1371 "Non-nil means items without effort estimate are sorted as high effort.
1372 This also applies when filtering an agenda view with respect to the
1373 < or > effort operator. Then, tasks with no effort defined will be treated
1374 as tasks with high effort.
1375 When nil, such items are sorted as 0 minutes effort."
1376 :group 'org-agenda-sorting
1377 :type 'boolean)
1379 (defgroup org-agenda-line-format nil
1380 "Options concerning the entry prefix in the Org-mode agenda display."
1381 :tag "Org Agenda Line Format"
1382 :group 'org-agenda)
1384 (defcustom org-agenda-prefix-format
1385 '((agenda . " %i %-12:c%?-12t% s")
1386 (timeline . " % s")
1387 (todo . " %i %-12:c")
1388 (tags . " %i %-12:c")
1389 (search . " %i %-12:c"))
1390 "Format specifications for the prefix of items in the agenda views.
1391 An alist with four entries, for the different agenda types. The keys to the
1392 sublists are `agenda', `timeline', `todo', `search' and `tags'. The values
1393 are format strings.
1394 This format works similar to a printf format, with the following meaning:
1396 %c the category of the item, \"Diary\" for entries from the diary, or
1397 as given by the CATEGORY keyword or derived from the file name.
1398 %i the icon category of the item, as give in
1399 `org-agenda-category-icon-alist'.
1400 %T the *last* tag of the item. Last because inherited tags come
1401 first in the list.
1402 %t the time-of-day specification if one applies to the entry, in the
1403 format HH:MM
1404 %s Scheduling/Deadline information, a short string
1405 %(expression) Eval EXPRESSION and replace the control string
1406 by the result
1408 All specifiers work basically like the standard `%s' of printf, but may
1409 contain two additional characters: A question mark just after the `%' and
1410 a whitespace/punctuation character just before the final letter.
1412 If the first character after `%' is a question mark, the entire field
1413 will only be included if the corresponding value applies to the
1414 current entry. This is useful for fields which should have fixed
1415 width when present, but zero width when absent. For example,
1416 \"%?-12t\" will result in a 12 character time field if a time of the
1417 day is specified, but will completely disappear in entries which do
1418 not contain a time.
1420 If there is punctuation or whitespace character just before the final
1421 format letter, this character will be appended to the field value if
1422 the value is not empty. For example, the format \"%-12:c\" leads to
1423 \"Diary: \" if the category is \"Diary\". If the category were be
1424 empty, no additional colon would be inserted.
1426 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1427 - Indent the line with two space characters
1428 - Give the category in a 12 chars wide field, padded with whitespace on
1429 the right (because of `-'). Append a colon if there is a category
1430 (because of `:').
1431 - If there is a time-of-day, put it into a 12 chars wide field. If no
1432 time, don't put in an empty field, just skip it (because of '?').
1433 - Finally, put the scheduling information and append a whitespace.
1435 As another example, if you don't want the time-of-day of entries in
1436 the prefix, you could use:
1438 (setq org-agenda-prefix-format \" %-11:c% s\")
1440 See also the variables `org-agenda-remove-times-when-in-prefix' and
1441 `org-agenda-remove-tags'.
1443 Custom commands can set this variable in the options section."
1444 :type '(choice
1445 (string :tag "General format")
1446 (list :greedy t :tag "View dependent"
1447 (cons (const agenda) (string :tag "Format"))
1448 (cons (const timeline) (string :tag "Format"))
1449 (cons (const todo) (string :tag "Format"))
1450 (cons (const tags) (string :tag "Format"))
1451 (cons (const search) (string :tag "Format"))))
1452 :group 'org-agenda-line-format)
1454 (defvar org-prefix-format-compiled nil
1455 "The compiled version of the most recently used prefix format.
1456 See the variable `org-agenda-prefix-format'.")
1458 (defcustom org-agenda-todo-keyword-format "%-1s"
1459 "Format for the TODO keyword in agenda lines.
1460 Set this to something like \"%-12s\" if you want all TODO keywords
1461 to occupy a fixed space in the agenda display."
1462 :group 'org-agenda-line-format
1463 :type 'string)
1465 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1466 "Text preceding timerange entries in the agenda view.
1467 This is a list with two strings. The first applies when the range
1468 is entirely on one day. The second applies if the range spans several days.
1469 The strings may have two \"%d\" format specifiers which will be filled
1470 with the sequence number of the days, and the total number of days in the
1471 range, respectively."
1472 :group 'org-agenda-line-format
1473 :type '(list
1474 (string :tag "Deadline today ")
1475 (choice :tag "Deadline relative"
1476 (string :tag "Format string")
1477 (function))))
1479 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1480 "Text preceding scheduled items in the agenda view.
1481 This is a list with two strings. The first applies when the item is
1482 scheduled on the current day. The second applies when it has been scheduled
1483 previously, it may contain a %d indicating that this is the nth time that
1484 this item is scheduled, due to automatic rescheduling of unfinished items
1485 for the following day. So this number is one larger than the number of days
1486 that passed since this item was scheduled first."
1487 :group 'org-agenda-line-format
1488 :type '(list
1489 (string :tag "Scheduled today ")
1490 (string :tag "Scheduled previously")))
1492 (defcustom org-agenda-inactive-leader "["
1493 "Text preceding item pulled into the agenda by inactive time stamps.
1494 These entries are added to the agenda when pressing \"[\"."
1495 :group 'org-agenda-line-format
1496 :type '(list
1497 (string :tag "Scheduled today ")
1498 (string :tag "Scheduled previously")))
1500 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1501 "Text preceding deadline items in the agenda view.
1502 This is a list with two strings. The first applies when the item has its
1503 deadline on the current day. The second applies when it is in the past or
1504 in the future, it may contain %d to capture how many days away the deadline
1505 is (was)."
1506 :group 'org-agenda-line-format
1507 :type '(list
1508 (string :tag "Deadline today ")
1509 (choice :tag "Deadline relative"
1510 (string :tag "Format string")
1511 (function))))
1513 (defcustom org-agenda-remove-times-when-in-prefix t
1514 "Non-nil means remove duplicate time specifications in agenda items.
1515 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1516 time-of-day specification in a headline or diary entry is extracted and
1517 placed into the prefix. If this option is non-nil, the original specification
1518 \(a timestamp or -range, or just a plain time(range) specification like
1519 11:30-4pm) will be removed for agenda display. This makes the agenda less
1520 cluttered.
1521 The option can be t or nil. It may also be the symbol `beg', indicating
1522 that the time should only be removed when it is located at the beginning of
1523 the headline/diary entry."
1524 :group 'org-agenda-line-format
1525 :type '(choice
1526 (const :tag "Always" t)
1527 (const :tag "Never" nil)
1528 (const :tag "When at beginning of entry" beg)))
1530 (defcustom org-agenda-remove-timeranges-from-blocks nil
1531 "Non-nil means remove time ranges specifications in agenda
1532 items that span on several days."
1533 :group 'org-agenda-line-format
1534 :type 'boolean)
1536 (defcustom org-agenda-default-appointment-duration nil
1537 "Default duration for appointments that only have a starting time.
1538 When nil, no duration is specified in such cases.
1539 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1540 :group 'org-agenda-line-format
1541 :type '(choice
1542 (integer :tag "Minutes")
1543 (const :tag "No default duration")))
1545 (defcustom org-agenda-show-inherited-tags t
1546 "Non-nil means show inherited tags in each agenda line."
1547 :group 'org-agenda-line-format
1548 :type 'boolean)
1550 (defcustom org-agenda-hide-tags-regexp nil
1551 "Regular expression used to filter away specific tags in agenda views.
1552 This means that these tags will be present, but not be shown in the agenda
1553 line. Secondary filtering will still work on the hidden tags.
1554 Nil means don't hide any tags."
1555 :group 'org-agenda-line-format
1556 :type '(choice
1557 (const :tag "Hide none" nil)
1558 (string :tag "Regexp ")))
1560 (defcustom org-agenda-remove-tags nil
1561 "Non-nil means remove the tags from the headline copy in the agenda.
1562 When this is the symbol `prefix', only remove tags when
1563 `org-agenda-prefix-format' contains a `%T' specifier."
1564 :group 'org-agenda-line-format
1565 :type '(choice
1566 (const :tag "Always" t)
1567 (const :tag "Never" nil)
1568 (const :tag "When prefix format contains %T" prefix)))
1570 (if (fboundp 'defvaralias)
1571 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1572 'org-agenda-remove-tags))
1574 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1575 "Shift tags in agenda items to this column.
1576 If this number is positive, it specifies the column. If it is negative,
1577 it means that the tags should be flushright to that column. For example,
1578 -80 works well for a normal 80 character screen."
1579 :group 'org-agenda-line-format
1580 :type 'integer)
1582 (if (fboundp 'defvaralias)
1583 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1585 (defcustom org-agenda-fontify-priorities 'cookies
1586 "Non-nil means highlight low and high priorities in agenda.
1587 When t, the highest priority entries are bold, lowest priority italic.
1588 However, settings in `org-priority-faces' will overrule these faces.
1589 When this variable is the symbol `cookies', only fontify the
1590 cookies, not the entire task.
1591 This may also be an association list of priority faces, whose
1592 keys are the character values of `org-highest-priority',
1593 `org-default-priority', and `org-lowest-priority' (the default values
1594 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1595 color as a string, or a list like `(:background \"Red\")'.
1596 If it is a color, the variable `org-faces-easy-properties'
1597 determines if it is a foreground or a background color."
1598 :group 'org-agenda-line-format
1599 :type '(choice
1600 (const :tag "Never" nil)
1601 (const :tag "Defaults" t)
1602 (const :tag "Cookies only" cookies)
1603 (repeat :tag "Specify"
1604 (list (character :tag "Priority" :value ?A)
1605 (choice :tag "Face "
1606 (string :tag "Color")
1607 (sexp :tag "Face"))))))
1609 (defcustom org-agenda-day-face-function nil
1610 "Function called to determine what face should be used to display a day.
1611 The only argument passed to that function is the day. It should
1612 returns a face, or nil if does not want to specify a face and let
1613 the normal rules apply."
1614 :group 'org-agenda-line-format
1615 :type 'function)
1617 (defcustom org-agenda-category-icon-alist nil
1618 "Alist of category icon to be displayed in agenda views.
1620 Each entry should have the following format:
1622 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1624 Where CATEGORY-REGEXP is a regexp matching the categories where
1625 the icon should be displayed.
1626 FILE-OR-DATA either a file path or a string containing image data.
1628 The other fields can be omited safely if not needed:
1629 TYPE indicates the image type.
1630 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1631 image data.
1632 PROPS are additional image attributes to assign to the image,
1633 like, e.g. `:ascent center'.
1635 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1637 If you want to set the display properties yourself, just put a
1638 list as second element:
1640 (CATEGORY-REGEXP (MY PROPERTY LIST))
1642 For example, to display a 16px horizontal space for Emacs
1643 category, you can use:
1645 (\"Emacs\" '(space . (:width (16))))"
1646 :group 'org-agenda-line-format
1647 :type '(alist :key-type (string :tag "Regexp matching category")
1648 :value-type (choice (list :tag "Icon"
1649 (string :tag "File or data")
1650 (symbol :tag "Type")
1651 (boolean :tag "Data?")
1652 (repeat :tag "Extra image properties" :inline t symbol))
1653 (list :tag "Display properties" sexp))))
1655 (defgroup org-agenda-column-view nil
1656 "Options concerning column view in the agenda."
1657 :tag "Org Agenda Column View"
1658 :group 'org-agenda)
1660 (defcustom org-agenda-columns-show-summaries t
1661 "Non-nil means show summaries for columns displayed in the agenda view."
1662 :group 'org-agenda-column-view
1663 :type 'boolean)
1665 (defcustom org-agenda-columns-compute-summary-properties t
1666 "Non-nil means recompute all summary properties before column view.
1667 When column view in the agenda is listing properties that have a summary
1668 operator, it can go to all relevant buffers and recompute the summaries
1669 there. This can mean overhead for the agenda column view, but is necessary
1670 to have thing up to date.
1671 As a special case, a CLOCKSUM property also makes sure that the clock
1672 computations are current."
1673 :group 'org-agenda-column-view
1674 :type 'boolean)
1676 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1677 "Non-nil means the duration of an appointment will add to day effort.
1678 The property to which appointment durations will be added is the one given
1679 in the option `org-effort-property'. If an appointment does not have
1680 an end time, `org-agenda-default-appointment-duration' will be used. If that
1681 is not set, an appointment without end time will not contribute to the time
1682 estimate."
1683 :group 'org-agenda-column-view
1684 :type 'boolean)
1686 (defcustom org-agenda-auto-exclude-function nil
1687 "A function called with a tag to decide if it is filtered on '/ RET'.
1688 The sole argument to the function, which is called once for each
1689 possible tag, is a string giving the name of the tag. The
1690 function should return either nil if the tag should be included
1691 as normal, or \"-<TAG>\" to exclude the tag.
1692 Note that for the purpose of tag filtering, only the lower-case version of
1693 all tags will be considered, so that this function will only ever see
1694 the lower-case version of all tags."
1695 :group 'org-agenda
1696 :type 'function)
1698 (eval-when-compile
1699 (require 'cl))
1700 (require 'org)
1702 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1703 "Execute BODY with point at location given by `org-hd-marker' property.
1704 If STRING is non-nil, the text property will be fetched from position 0
1705 in that string. If STRING is nil, it will be fetched from the beginning
1706 of the current line."
1707 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1708 'org-hd-marker string)))
1709 (with-current-buffer (marker-buffer marker)
1710 (save-excursion
1711 (goto-char marker)
1712 ,@body))))
1714 (defun org-add-agenda-custom-command (entry)
1715 "Replace or add a command in `org-agenda-custom-commands'.
1716 This is mostly for hacking and trying a new command - once the command
1717 works you probably want to add it to `org-agenda-custom-commands' for good."
1718 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1719 (if ass
1720 (setcdr ass (cdr entry))
1721 (push entry org-agenda-custom-commands))))
1723 ;;; Define the Org-agenda-mode
1725 (defvar org-agenda-mode-map (make-sparse-keymap)
1726 "Keymap for `org-agenda-mode'.")
1727 (if (fboundp 'defvaralias)
1728 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1730 (defvar org-agenda-menu) ; defined later in this file.
1731 (defvar org-agenda-restrict) ; defined later in this file.
1732 (defvar org-agenda-follow-mode nil)
1733 (defvar org-agenda-entry-text-mode nil)
1734 (defvar org-agenda-clockreport-mode nil)
1735 (defvar org-agenda-show-log nil)
1736 (defvar org-agenda-redo-command nil)
1737 (defvar org-agenda-query-string nil)
1738 (defvar org-agenda-mode-hook nil
1739 "Hook for `org-agenda-mode', run after the mode is turned on.")
1740 (defvar org-agenda-type nil)
1741 (defvar org-agenda-force-single-file nil)
1742 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1744 (defun org-agenda-mode ()
1745 "Mode for time-sorted view on action items in Org-mode files.
1747 The following commands are available:
1749 \\{org-agenda-mode-map}"
1750 (interactive)
1751 (kill-all-local-variables)
1752 (setq org-agenda-undo-list nil
1753 org-agenda-pending-undo-list nil
1754 org-agenda-bulk-marked-entries nil)
1755 (setq major-mode 'org-agenda-mode)
1756 ;; Keep global-font-lock-mode from turning on font-lock-mode
1757 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1758 (setq mode-name "Org-Agenda")
1759 (use-local-map org-agenda-mode-map)
1760 (easy-menu-add org-agenda-menu)
1761 (if org-startup-truncated (setq truncate-lines t))
1762 (org-set-local 'line-move-visual nil)
1763 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1764 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1765 ;; Make sure properties are removed when copying text
1766 (when (boundp 'buffer-substring-filters)
1767 (org-set-local 'buffer-substring-filters
1768 (cons (lambda (x)
1769 (set-text-properties 0 (length x) nil x) x)
1770 buffer-substring-filters)))
1771 (unless org-agenda-keep-modes
1772 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1773 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1774 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1775 org-agenda-show-log org-agenda-start-with-log-mode))
1777 (easy-menu-change
1778 '("Agenda") "Agenda Files"
1779 (append
1780 (list
1781 (vector
1782 (if (get 'org-agenda-files 'org-restrict)
1783 "Restricted to single file"
1784 "Edit File List")
1785 '(org-edit-agenda-file-list)
1786 (not (get 'org-agenda-files 'org-restrict)))
1787 "--")
1788 (mapcar 'org-file-menu-entry (org-agenda-files))))
1789 (org-agenda-set-mode-name)
1790 (apply
1791 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1792 (list 'org-agenda-mode-hook)))
1794 (substitute-key-definition 'undo 'org-agenda-undo
1795 org-agenda-mode-map global-map)
1796 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1797 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1798 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1799 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1800 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1801 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1802 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1803 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1804 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1805 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1806 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1807 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1808 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1809 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1810 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1811 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1812 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1813 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1814 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1815 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1816 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1817 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1818 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1819 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1820 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1821 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1822 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1823 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1824 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1825 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1826 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1827 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1828 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1829 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1830 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1831 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1832 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1833 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1834 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1835 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1836 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1837 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1838 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1839 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1840 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1842 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1843 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1844 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1845 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1846 (while l (org-defkey org-agenda-mode-map
1847 (int-to-string (pop l)) 'digit-argument)))
1849 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1850 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1851 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1852 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1853 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1854 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1855 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1856 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1857 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1858 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1859 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1860 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1861 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1862 'org-clock-modify-effort-estimate)
1863 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1864 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1865 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1866 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1867 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1868 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1869 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1870 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1871 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1872 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1873 (substitute-key-definition 'next-line 'org-agenda-next-line
1874 org-agenda-mode-map global-map)
1875 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1876 org-agenda-mode-map global-map)
1877 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1878 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1879 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1880 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1881 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1882 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1883 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1884 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1885 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1886 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1887 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1888 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1889 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1890 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1891 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1892 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1893 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1894 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1895 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1896 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1897 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1898 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1899 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1900 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1901 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1902 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1903 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1904 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1905 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1906 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1908 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1909 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1910 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1911 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1912 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1913 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1914 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1915 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1916 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1917 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1919 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1920 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1921 (when org-agenda-mouse-1-follows-link
1922 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1923 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1924 '("Agenda"
1925 ("Agenda Files")
1926 "--"
1927 ("Agenda Dates"
1928 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1929 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1930 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1931 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1932 "--"
1933 ("View"
1934 ["Day View" org-agenda-day-view
1935 :active (org-agenda-check-type nil 'agenda)
1936 :style radio :selected (eq org-agenda-current-span 'day)
1937 :keys "v d (or just d)"]
1938 ["Week View" org-agenda-week-view
1939 :active (org-agenda-check-type nil 'agenda)
1940 :style radio :selected (eq org-agenda-current-span 'week)
1941 :keys "v w (or just w)"]
1942 ["Month View" org-agenda-month-view
1943 :active (org-agenda-check-type nil 'agenda)
1944 :style radio :selected (eq org-agenda-current-span 'month)
1945 :keys "v m"]
1946 ["Year View" org-agenda-year-view
1947 :active (org-agenda-check-type nil 'agenda)
1948 :style radio :selected (eq org-agenda-current-span 'year)
1949 :keys "v y"]
1950 "--"
1951 ["Include Diary" org-agenda-toggle-diary
1952 :style toggle :selected org-agenda-include-diary
1953 :active (org-agenda-check-type nil 'agenda)]
1954 ["Include Deadlines" org-agenda-toggle-deadlines
1955 :style toggle :selected org-agenda-include-deadlines
1956 :active (org-agenda-check-type nil 'agenda)]
1957 ["Use Time Grid" org-agenda-toggle-time-grid
1958 :style toggle :selected org-agenda-use-time-grid
1959 :active (org-agenda-check-type nil 'agenda)]
1960 "--"
1961 ["Show clock report" org-agenda-clockreport-mode
1962 :style toggle :selected org-agenda-clockreport-mode
1963 :active (org-agenda-check-type nil 'agenda)]
1964 ["Show some entry text" org-agenda-entry-text-mode
1965 :style toggle :selected org-agenda-entry-text-mode
1966 :active t]
1967 "--"
1968 ["Show Logbook entries" org-agenda-log-mode
1969 :style toggle :selected org-agenda-show-log
1970 :active (org-agenda-check-type nil 'agenda 'timeline)
1971 :keys "v l (or just l)"]
1972 ["Include archived trees" org-agenda-archives-mode
1973 :style toggle :selected org-agenda-archives-mode :active t
1974 :keys "v a"]
1975 ["Include archive files" (org-agenda-archives-mode t)
1976 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1977 :keys "v A"]
1978 "--"
1979 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1980 ["Write view to file" org-write-agenda t]
1981 ["Rebuild buffer" org-agenda-redo t]
1982 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1983 "--"
1984 ["Show original entry" org-agenda-show t]
1985 ["Go To (other window)" org-agenda-goto t]
1986 ["Go To (this window)" org-agenda-switch-to t]
1987 ["Follow Mode" org-agenda-follow-mode
1988 :style toggle :selected org-agenda-follow-mode :active t]
1989 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1990 "--"
1991 ("TODO"
1992 ["Cycle TODO" org-agenda-todo t]
1993 ["Next TODO set" org-agenda-todo-nextset t]
1994 ["Previous TODO set" org-agenda-todo-previousset t]
1995 ["Add note" org-agenda-add-note t])
1996 ("Archive/Refile/Delete"
1997 ["Archive default" org-agenda-archive-default t]
1998 ["Archive default" org-agenda-archive-default-with-confirmation t]
1999 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2000 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2001 ["Archive subtree" org-agenda-archive t]
2002 "--"
2003 ["Refile" org-agenda-refile t]
2004 "--"
2005 ["Delete subtree" org-agenda-kill t])
2006 ("Bulk action"
2007 ["Mark entry" org-agenda-bulk-mark t]
2008 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2009 ["Unmark entry" org-agenda-bulk-unmark t]
2010 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2011 ["Act on all marked" org-agenda-bulk-action t]
2012 "--"
2013 ("Tags and Properties"
2014 ["Show all Tags" org-agenda-show-tags t]
2015 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2016 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2017 "--"
2018 ["Column View" org-columns t])
2019 ("Deadline/Schedule"
2020 ["Schedule" org-agenda-schedule t]
2021 ["Set Deadline" org-agenda-deadline t]
2022 "--"
2023 ["Mark item" org-agenda-action :active t :keys "k m"]
2024 ["Show mark item" org-agenda-action :active t :keys "k v"]
2025 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2026 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2027 "--"
2028 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2029 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2030 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2031 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2032 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2033 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2034 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2035 ("Clock and Effort"
2036 ["Clock in" org-agenda-clock-in t]
2037 ["Clock out" org-agenda-clock-out t]
2038 ["Clock cancel" org-agenda-clock-cancel t]
2039 ["Goto running clock" org-clock-goto t]
2040 "--"
2041 ["Set Effort" org-agenda-set-effort t]
2042 ["Change clocked effort" org-clock-modify-effort-estimate
2043 (org-clock-is-active)])
2044 ("Priority"
2045 ["Set Priority" org-agenda-priority t]
2046 ["Increase Priority" org-agenda-priority-up t]
2047 ["Decrease Priority" org-agenda-priority-down t]
2048 ["Show Priority" org-agenda-show-priority t])
2049 ("Calendar/Diary"
2050 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2051 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2052 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2053 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2054 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2055 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2056 "--"
2057 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2058 "--"
2059 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2060 "--"
2061 ("MobileOrg"
2062 ["Push Files and Views" org-mobile-push t]
2063 ["Get Captured and Flagged" org-mobile-pull t]
2064 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2065 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2066 "--"
2067 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2068 "--"
2069 ["Quit" org-agenda-quit t]
2070 ["Exit and Release Buffers" org-agenda-exit t]
2073 ;;; Agenda undo
2075 (defvar org-agenda-allow-remote-undo t
2076 "Non-nil means allow remote undo from the agenda buffer.")
2077 (defvar org-agenda-undo-list nil
2078 "List of undoable operations in the agenda since last refresh.")
2079 (defvar org-agenda-undo-has-started-in nil
2080 "Buffers that have already seen `undo-start' in the current undo sequence.")
2081 (defvar org-agenda-pending-undo-list nil
2082 "In a series of undo commands, this is the list of remaining undo items.")
2084 (defun org-agenda-undo ()
2085 "Undo a remote editing step in the agenda.
2086 This undoes changes both in the agenda buffer and in the remote buffer
2087 that have been changed along."
2088 (interactive)
2089 (or org-agenda-allow-remote-undo
2090 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2091 (if (not (eq this-command last-command))
2092 (setq org-agenda-undo-has-started-in nil
2093 org-agenda-pending-undo-list org-agenda-undo-list))
2094 (if (not org-agenda-pending-undo-list)
2095 (error "No further undo information"))
2096 (let* ((entry (pop org-agenda-pending-undo-list))
2097 buf line cmd rembuf)
2098 (setq cmd (pop entry) line (pop entry))
2099 (setq rembuf (nth 2 entry))
2100 (org-with-remote-undo rembuf
2101 (while (bufferp (setq buf (pop entry)))
2102 (if (pop entry)
2103 (with-current-buffer buf
2104 (let ((last-undo-buffer buf)
2105 (inhibit-read-only t))
2106 (unless (memq buf org-agenda-undo-has-started-in)
2107 (push buf org-agenda-undo-has-started-in)
2108 (make-local-variable 'pending-undo-list)
2109 (undo-start))
2110 (while (and pending-undo-list
2111 (listp pending-undo-list)
2112 (not (car pending-undo-list)))
2113 (pop pending-undo-list))
2114 (undo-more 1))))))
2115 (org-goto-line line)
2116 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2118 (defun org-verify-change-for-undo (l1 l2)
2119 "Verify that a real change occurred between the undo lists L1 and L2."
2120 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2121 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2122 (not (eq l1 l2)))
2124 ;;; Agenda dispatch
2126 (defvar org-agenda-restrict nil)
2127 (defvar org-agenda-restrict-begin (make-marker))
2128 (defvar org-agenda-restrict-end (make-marker))
2129 (defvar org-agenda-last-dispatch-buffer nil)
2130 (defvar org-agenda-overriding-restriction nil)
2132 ;;;###autoload
2133 (defun org-agenda (&optional arg keys restriction)
2134 "Dispatch agenda commands to collect entries to the agenda buffer.
2135 Prompts for a command to execute. Any prefix arg will be passed
2136 on to the selected command. The default selections are:
2138 a Call `org-agenda-list' to display the agenda for current day or week.
2139 t Call `org-todo-list' to display the global todo list.
2140 T Call `org-todo-list' to display the global todo list, select only
2141 entries with a specific TODO keyword (the user gets a prompt).
2142 m Call `org-tags-view' to display headlines with tags matching
2143 a condition (the user is prompted for the condition).
2144 M Like `m', but select only TODO entries, no ordinary headlines.
2145 L Create a timeline for the current buffer.
2146 e Export views to associated files.
2147 s Search entries for keywords.
2148 / Multi occur across all agenda files and also files listed
2149 in `org-agenda-text-search-extra-files'.
2150 < Restrict agenda commands to buffer, subtree, or region.
2151 Press several times to get the desired effect.
2152 > Remove a previous restriction.
2153 # List \"stuck\" projects.
2154 ! Configure what \"stuck\" means.
2155 C Configure custom agenda commands.
2157 More commands can be added by configuring the variable
2158 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2159 searches can be pre-defined in this way.
2161 If the current buffer is in Org-mode and visiting a file, you can also
2162 first press `<' once to indicate that the agenda should be temporarily
2163 \(until the next use of \\[org-agenda]) restricted to the current file.
2164 Pressing `<' twice means to restrict to the current subtree or region
2165 \(if active)."
2166 (interactive "P")
2167 (catch 'exit
2168 (let* ((prefix-descriptions nil)
2169 (org-agenda-window-setup (if (equal (buffer-name)
2170 org-agenda-buffer-name)
2171 'current-window
2172 org-agenda-window-setup))
2173 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2174 (org-agenda-custom-commands
2175 ;; normalize different versions
2176 (delq nil
2177 (mapcar
2178 (lambda (x)
2179 (cond ((stringp (cdr x))
2180 (push x prefix-descriptions)
2181 nil)
2182 ((stringp (nth 1 x)) x)
2183 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2184 (t (cons (car x) (cons "" (cdr x))))))
2185 org-agenda-custom-commands)))
2186 (buf (current-buffer))
2187 (bfn (buffer-file-name (buffer-base-buffer)))
2188 entry key type match lprops ans)
2189 ;; Turn off restriction unless there is an overriding one,
2190 (unless org-agenda-overriding-restriction
2191 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2192 ;; There is a request to keep the file list in place
2193 (put 'org-agenda-files 'org-restrict nil))
2194 (setq org-agenda-restrict nil)
2195 (move-marker org-agenda-restrict-begin nil)
2196 (move-marker org-agenda-restrict-end nil))
2197 ;; Delete old local properties
2198 (put 'org-agenda-redo-command 'org-lprops nil)
2199 ;; Delete previously set last-arguments
2200 (put 'org-agenda-redo-command 'last-args nil)
2201 ;; Remember where this call originated
2202 (setq org-agenda-last-dispatch-buffer (current-buffer))
2203 (unless keys
2204 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2205 keys (car ans)
2206 restriction (cdr ans)))
2207 ;; Establish the restriction, if any
2208 (when (and (not org-agenda-overriding-restriction) restriction)
2209 (put 'org-agenda-files 'org-restrict (list bfn))
2210 (cond
2211 ((eq restriction 'region)
2212 (setq org-agenda-restrict t)
2213 (move-marker org-agenda-restrict-begin (region-beginning))
2214 (move-marker org-agenda-restrict-end (region-end)))
2215 ((eq restriction 'subtree)
2216 (save-excursion
2217 (setq org-agenda-restrict t)
2218 (org-back-to-heading t)
2219 (move-marker org-agenda-restrict-begin (point))
2220 (move-marker org-agenda-restrict-end
2221 (progn (org-end-of-subtree t)))))))
2223 ;; For example the todo list should not need it (but does...)
2224 (cond
2225 ((setq entry (assoc keys org-agenda-custom-commands))
2226 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2227 (progn
2228 (setq type (nth 2 entry) match (eval (nth 3 entry))
2229 lprops (nth 4 entry))
2230 (put 'org-agenda-redo-command 'org-lprops lprops)
2231 (cond
2232 ((eq type 'agenda)
2233 (org-let lprops '(org-agenda-list current-prefix-arg)))
2234 ((eq type 'alltodo)
2235 (org-let lprops '(org-todo-list current-prefix-arg)))
2236 ((eq type 'search)
2237 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2238 ((eq type 'stuck)
2239 (org-let lprops '(org-agenda-list-stuck-projects
2240 current-prefix-arg)))
2241 ((eq type 'tags)
2242 (org-let lprops '(org-tags-view current-prefix-arg match)))
2243 ((eq type 'tags-todo)
2244 (org-let lprops '(org-tags-view '(4) match)))
2245 ((eq type 'todo)
2246 (org-let lprops '(org-todo-list match)))
2247 ((eq type 'tags-tree)
2248 (org-check-for-org-mode)
2249 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2250 ((eq type 'todo-tree)
2251 (org-check-for-org-mode)
2252 (org-let lprops
2253 '(org-occur (concat "^" outline-regexp "[ \t]*"
2254 (regexp-quote match) "\\>"))))
2255 ((eq type 'occur-tree)
2256 (org-check-for-org-mode)
2257 (org-let lprops '(org-occur match)))
2258 ((functionp type)
2259 (org-let lprops '(funcall type match)))
2260 ((fboundp type)
2261 (org-let lprops '(funcall type match)))
2262 (t (error "Invalid custom agenda command type %s" type))))
2263 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2264 ((equal keys "C")
2265 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2266 (customize-variable 'org-agenda-custom-commands))
2267 ((equal keys "a") (call-interactively 'org-agenda-list))
2268 ((equal keys "s") (call-interactively 'org-search-view))
2269 ((equal keys "t") (call-interactively 'org-todo-list))
2270 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2271 ((equal keys "m") (call-interactively 'org-tags-view))
2272 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2273 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2274 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2275 (org-add-hook
2276 'post-command-hook
2277 (lambda ()
2278 (unless (current-message)
2279 (let* ((m (org-agenda-get-any-marker))
2280 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2281 (when note
2282 (message (concat
2283 "FLAGGING-NOTE ([?] for more info): "
2284 (org-add-props
2285 (replace-regexp-in-string
2286 "\\\\n" "//"
2287 (copy-sequence note))
2288 nil 'face 'org-warning)))))))
2289 t t))
2290 ((equal keys "L")
2291 (unless (org-mode-p)
2292 (error "This is not an Org-mode file"))
2293 (unless restriction
2294 (put 'org-agenda-files 'org-restrict (list bfn))
2295 (org-call-with-arg 'org-timeline arg)))
2296 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2297 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2298 ((equal keys "!") (customize-variable 'org-stuck-projects))
2299 (t (error "Invalid agenda key"))))))
2301 (defun org-agenda-append-agenda ()
2302 "Append another agenda view to the current one.
2303 This function allows interactive building of block agendas.
2304 Agenda views are separated by `org-agenda-block-separator'."
2305 (interactive)
2306 (unless (string= (buffer-name) org-agenda-buffer-name)
2307 (error "Can only append from within agenda buffer"))
2308 (let ((org-agenda-multi t))
2309 (org-agenda)
2310 (widen)))
2312 (defun org-agenda-normalize-custom-commands (cmds)
2313 (delq nil
2314 (mapcar
2315 (lambda (x)
2316 (cond ((stringp (cdr x)) nil)
2317 ((stringp (nth 1 x)) x)
2318 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2319 (t (cons (car x) (cons "" (cdr x))))))
2320 cmds)))
2322 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2323 "The user interface for selecting an agenda command."
2324 (catch 'exit
2325 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2326 (restrict-ok (and bfn (org-mode-p)))
2327 (region-p (org-region-active-p))
2328 (custom org-agenda-custom-commands)
2329 (selstring "")
2330 restriction second-time
2331 c entry key type match prefixes rmheader header-end custom1 desc
2332 line lines left right n n1)
2333 (save-window-excursion
2334 (delete-other-windows)
2335 (org-switch-to-buffer-other-window " *Agenda Commands*")
2336 (erase-buffer)
2337 (insert (eval-when-compile
2338 (let ((header
2340 Press key for an agenda command: < Buffer, subtree/region restriction
2341 -------------------------------- > Remove restriction
2342 a Agenda for current week or day e Export agenda views
2343 t List of all TODO entries T Entries with special TODO kwd
2344 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2345 L Timeline for current buffer # List stuck projects (!=configure)
2346 s Search for keywords C Configure custom agenda commands
2347 / Multi-occur ? Find :FLAGGED: entries
2349 (start 0))
2350 (while (string-match
2351 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2352 header start)
2353 (setq start (match-end 0))
2354 (add-text-properties (match-beginning 2) (match-end 2)
2355 '(face bold) header))
2356 header)))
2357 (setq header-end (move-marker (make-marker) (point)))
2358 (while t
2359 (setq custom1 custom)
2360 (when (eq rmheader t)
2361 (org-goto-line 1)
2362 (re-search-forward ":" nil t)
2363 (delete-region (match-end 0) (point-at-eol))
2364 (forward-char 1)
2365 (looking-at "-+")
2366 (delete-region (match-end 0) (point-at-eol))
2367 (move-marker header-end (match-end 0)))
2368 (goto-char header-end)
2369 (delete-region (point) (point-max))
2371 ;; Produce all the lines that describe custom commands and prefixes
2372 (setq lines nil)
2373 (while (setq entry (pop custom1))
2374 (setq key (car entry) desc (nth 1 entry)
2375 type (nth 2 entry)
2376 match (nth 3 entry))
2377 (if (> (length key) 1)
2378 (add-to-list 'prefixes (string-to-char key))
2379 (setq line
2380 (format
2381 "%-4s%-14s"
2382 (org-add-props (copy-sequence key)
2383 '(face bold))
2384 (cond
2385 ((string-match "\\S-" desc) desc)
2386 ((eq type 'agenda) "Agenda for current week or day")
2387 ((eq type 'alltodo) "List of all TODO entries")
2388 ((eq type 'search) "Word search")
2389 ((eq type 'stuck) "List of stuck projects")
2390 ((eq type 'todo) "TODO keyword")
2391 ((eq type 'tags) "Tags query")
2392 ((eq type 'tags-todo) "Tags (TODO)")
2393 ((eq type 'tags-tree) "Tags tree")
2394 ((eq type 'todo-tree) "TODO kwd tree")
2395 ((eq type 'occur-tree) "Occur tree")
2396 ((functionp type) (if (symbolp type)
2397 (symbol-name type)
2398 "Lambda expression"))
2399 (t "???"))))
2400 (if org-agenda-menu-show-matcher
2401 (setq line
2402 (concat line ": "
2403 (cond
2404 ((stringp match)
2405 (setq match (copy-sequence match))
2406 (org-add-props match nil 'face 'org-warning))
2407 (match
2408 (format "set of %d commands" (length match)))
2409 (t ""))))
2410 (if (org-string-nw-p match)
2411 (add-text-properties
2412 0 (length line) (list 'help-echo
2413 (concat "Matcher: "match)) line)))
2414 (push line lines)))
2415 (setq lines (nreverse lines))
2416 (when prefixes
2417 (mapc (lambda (x)
2418 (push
2419 (format "%s %s"
2420 (org-add-props (char-to-string x)
2421 nil 'face 'bold)
2422 (or (cdr (assoc (concat selstring
2423 (char-to-string x))
2424 prefix-descriptions))
2425 "Prefix key"))
2426 lines))
2427 prefixes))
2429 ;; Check if we should display in two columns
2430 (if org-agenda-menu-two-column
2431 (progn
2432 (setq n (length lines)
2433 n1 (+ (/ n 2) (mod n 2))
2434 right (nthcdr n1 lines)
2435 left (copy-sequence lines))
2436 (setcdr (nthcdr (1- n1) left) nil))
2437 (setq left lines right nil))
2438 (while left
2439 (insert "\n" (pop left))
2440 (when right
2441 (if (< (current-column) 40)
2442 (move-to-column 40 t)
2443 (insert " "))
2444 (insert (pop right))))
2446 ;; Make the window the right size
2447 (goto-char (point-min))
2448 (if second-time
2449 (if (not (pos-visible-in-window-p (point-max)))
2450 (org-fit-window-to-buffer))
2451 (setq second-time t)
2452 (org-fit-window-to-buffer))
2454 ;; Ask for selection
2455 (message "Press key for agenda command%s:"
2456 (if (or restrict-ok org-agenda-overriding-restriction)
2457 (if org-agenda-overriding-restriction
2458 " (restriction lock active)"
2459 (if restriction
2460 (format " (restricted to %s)" restriction)
2461 " (unrestricted)"))
2462 ""))
2463 (setq c (read-char-exclusive))
2464 (message "")
2465 (cond
2466 ((assoc (char-to-string c) custom)
2467 (setq selstring (concat selstring (char-to-string c)))
2468 (throw 'exit (cons selstring restriction)))
2469 ((memq c prefixes)
2470 (setq selstring (concat selstring (char-to-string c))
2471 prefixes nil
2472 rmheader (or rmheader t)
2473 custom (delq nil (mapcar
2474 (lambda (x)
2475 (if (or (= (length (car x)) 1)
2476 (/= (string-to-char (car x)) c))
2478 (cons (substring (car x) 1) (cdr x))))
2479 custom))))
2480 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2481 (message "Restriction is only possible in Org-mode buffers")
2482 (ding) (sit-for 1))
2483 ((eq c ?1)
2484 (org-agenda-remove-restriction-lock 'noupdate)
2485 (setq restriction 'buffer))
2486 ((eq c ?0)
2487 (org-agenda-remove-restriction-lock 'noupdate)
2488 (setq restriction (if region-p 'region 'subtree)))
2489 ((eq c ?<)
2490 (org-agenda-remove-restriction-lock 'noupdate)
2491 (setq restriction
2492 (cond
2493 ((eq restriction 'buffer)
2494 (if region-p 'region 'subtree))
2495 ((memq restriction '(subtree region))
2496 nil)
2497 (t 'buffer))))
2498 ((eq c ?>)
2499 (org-agenda-remove-restriction-lock 'noupdate)
2500 (setq restriction nil))
2501 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2502 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2503 ((and (> (length selstring) 0) (eq c ?\d))
2504 (delete-window)
2505 (org-agenda-get-restriction-and-command prefix-descriptions))
2507 ((equal c ?q) (error "Abort"))
2508 (t (error "Invalid key %c" c))))))))
2510 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2511 (defvar org-agenda-last-arguments nil
2512 "The arguments of the previous call to `org-agenda'.")
2513 (defun org-agenda-run-series (name series)
2514 (org-let (nth 1 series) '(org-prepare-agenda name))
2515 (let* ((org-agenda-multi t)
2516 (redo (list 'org-agenda-run-series name (list 'quote series)))
2517 (org-agenda-overriding-arguments
2518 (or org-agenda-overriding-arguments
2519 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2520 (get 'org-agenda-redo-command 'last-args))))
2521 (cmds (car series))
2522 (gprops (nth 1 series))
2523 match ;; The byte compiler incorrectly complains about this. Keep it!
2524 cmd type lprops)
2525 (while (setq cmd (pop cmds))
2526 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2527 (cond
2528 ((eq type 'agenda)
2529 (org-let2 gprops lprops
2530 '(call-interactively 'org-agenda-list)))
2531 ((eq type 'alltodo)
2532 (org-let2 gprops lprops
2533 '(call-interactively 'org-todo-list)))
2534 ((eq type 'search)
2535 (org-let2 gprops lprops
2536 '(org-search-view current-prefix-arg match nil)))
2537 ((eq type 'stuck)
2538 (org-let2 gprops lprops
2539 '(call-interactively 'org-agenda-list-stuck-projects)))
2540 ((eq type 'tags)
2541 (org-let2 gprops lprops
2542 '(org-tags-view current-prefix-arg match)))
2543 ((eq type 'tags-todo)
2544 (org-let2 gprops lprops
2545 '(org-tags-view '(4) match)))
2546 ((eq type 'todo)
2547 (org-let2 gprops lprops
2548 '(org-todo-list match)))
2549 ((fboundp type)
2550 (org-let2 gprops lprops
2551 '(funcall type match)))
2552 (t (error "Invalid type in command series"))))
2553 (widen)
2554 (setq org-agenda-redo-command redo)
2555 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2556 (goto-char (point-min)))
2557 (org-fit-agenda-window)
2558 (org-let (nth 1 series) '(org-finalize-agenda)))
2560 ;;;###autoload
2561 (defmacro org-batch-agenda (cmd-key &rest parameters)
2562 "Run an agenda command in batch mode and send the result to STDOUT.
2563 If CMD-KEY is a string of length 1, it is used as a key in
2564 `org-agenda-custom-commands' and triggers this command. If it is a
2565 longer string it is used as a tags/todo match string.
2566 Parameters are alternating variable names and values that will be bound
2567 before running the agenda command."
2568 (let (pars)
2569 (while parameters
2570 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2571 (if (> (length cmd-key) 2)
2572 (eval (list 'let (nreverse pars)
2573 (list 'org-tags-view nil cmd-key)))
2574 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2575 (set-buffer org-agenda-buffer-name)
2576 (princ (org-encode-for-stdout (buffer-string)))))
2578 ;(defun org-encode-for-stdout (string)
2579 ; (if (fboundp 'encode-coding-string)
2580 ; (encode-coding-string string buffer-file-coding-system)
2581 ; string))
2583 (defun org-encode-for-stdout (string)
2584 string)
2586 (defvar org-agenda-info nil)
2588 ;;;###autoload
2589 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2590 "Run an agenda command in batch mode and send the result to STDOUT.
2591 If CMD-KEY is a string of length 1, it is used as a key in
2592 `org-agenda-custom-commands' and triggers this command. If it is a
2593 longer string it is used as a tags/todo match string.
2594 Parameters are alternating variable names and values that will be bound
2595 before running the agenda command.
2597 The output gives a line for each selected agenda item. Each
2598 item is a list of comma-separated values, like this:
2600 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2602 category The category of the item
2603 head The headline, without TODO kwd, TAGS and PRIORITY
2604 type The type of the agenda entry, can be
2605 todo selected in TODO match
2606 tagsmatch selected in tags match
2607 diary imported from diary
2608 deadline a deadline on given date
2609 scheduled scheduled on given date
2610 timestamp entry has timestamp on given date
2611 closed entry was closed on given date
2612 upcoming-deadline warning about deadline
2613 past-scheduled forwarded scheduled item
2614 block entry has date block including g. date
2615 todo The todo keyword, if any
2616 tags All tags including inherited ones, separated by colons
2617 date The relevant date, like 2007-2-14
2618 time The time, like 15:00-16:50
2619 extra Sting with extra planning info
2620 priority-l The priority letter if any was given
2621 priority-n The computed numerical priority
2622 agenda-day The day in the agenda where this is listed"
2624 (let (pars)
2625 (while parameters
2626 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2627 (push (list 'org-agenda-remove-tags t) pars)
2628 (if (> (length cmd-key) 2)
2629 (eval (list 'let (nreverse pars)
2630 (list 'org-tags-view nil cmd-key)))
2631 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2632 (set-buffer org-agenda-buffer-name)
2633 (let* ((lines (org-split-string (buffer-string) "\n"))
2634 line)
2635 (while (setq line (pop lines))
2636 (catch 'next
2637 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2638 (setq org-agenda-info
2639 (org-fix-agenda-info (text-properties-at 0 line)))
2640 (princ
2641 (org-encode-for-stdout
2642 (mapconcat 'org-agenda-export-csv-mapper
2643 '(org-category txt type todo tags date time extra
2644 priority-letter priority agenda-day)
2645 ",")))
2646 (princ "\n"))))))
2648 (defun org-fix-agenda-info (props)
2649 "Make sure all properties on an agenda item have a canonical form.
2650 This ensures the export commands can easily use it."
2651 (let (tmp re)
2652 (when (setq tmp (plist-get props 'tags))
2653 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2654 (when (setq tmp (plist-get props 'date))
2655 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2656 (let ((calendar-date-display-form '(year "-" month "-" day)))
2657 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2659 (setq tmp (calendar-date-string tmp)))
2660 (setq props (plist-put props 'date tmp)))
2661 (when (setq tmp (plist-get props 'day))
2662 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2663 (let ((calendar-date-display-form '(year "-" month "-" day)))
2664 (setq tmp (calendar-date-string tmp)))
2665 (setq props (plist-put props 'day tmp))
2666 (setq props (plist-put props 'agenda-day tmp)))
2667 (when (setq tmp (plist-get props 'txt))
2668 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2669 (plist-put props 'priority-letter (match-string 1 tmp))
2670 (setq tmp (replace-match "" t t tmp)))
2671 (when (and (setq re (plist-get props 'org-todo-regexp))
2672 (setq re (concat "\\`\\.*" re " ?"))
2673 (string-match re tmp))
2674 (plist-put props 'todo (match-string 1 tmp))
2675 (setq tmp (replace-match "" t t tmp)))
2676 (plist-put props 'txt tmp)))
2677 props)
2679 (defun org-agenda-export-csv-mapper (prop)
2680 (let ((res (plist-get org-agenda-info prop)))
2681 (setq res
2682 (cond
2683 ((not res) "")
2684 ((stringp res) res)
2685 (t (prin1-to-string res))))
2686 (while (string-match "," res)
2687 (setq res (replace-match ";" t t res)))
2688 (org-trim res)))
2691 ;;;###autoload
2692 (defun org-store-agenda-views (&rest parameters)
2693 (interactive)
2694 (eval (list 'org-batch-store-agenda-views)))
2696 ;; FIXME, why is this a macro?????
2697 ;;;###autoload
2698 (defmacro org-batch-store-agenda-views (&rest parameters)
2699 "Run all custom agenda commands that have a file argument."
2700 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2701 (pop-up-frames nil)
2702 (dir default-directory)
2703 pars cmd thiscmdkey files opts cmd-or-set)
2704 (while parameters
2705 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2706 (setq pars (reverse pars))
2707 (save-window-excursion
2708 (while cmds
2709 (setq cmd (pop cmds)
2710 thiscmdkey (car cmd)
2711 cmd-or-set (nth 2 cmd)
2712 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2713 files (nth (if (listp cmd-or-set) 4 5) cmd))
2714 (if (stringp files) (setq files (list files)))
2715 (when files
2716 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2717 (list 'org-agenda nil thiscmdkey)))
2718 (set-buffer org-agenda-buffer-name)
2719 (while files
2720 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2721 (list 'org-write-agenda
2722 (expand-file-name (pop files) dir) nil t))))
2723 (and (get-buffer org-agenda-buffer-name)
2724 (kill-buffer org-agenda-buffer-name)))))))
2726 (defun org-agenda-mark-header-line (pos)
2727 "Mark the line at POS as an agenda structure header."
2728 (save-excursion
2729 (goto-char pos)
2730 (put-text-property (point-at-bol) (point-at-eol)
2731 'org-agenda-structural-header t)
2732 (when org-agenda-title-append
2733 (put-text-property (point-at-bol) (point-at-eol)
2734 'org-agenda-title-append org-agenda-title-append))))
2736 (defvar org-mobile-creating-agendas)
2737 (defun org-write-agenda (file &optional open nosettings)
2738 "Write the current buffer (an agenda view) as a file.
2739 Depending on the extension of the file name, plain text (.txt),
2740 HTML (.html or .htm) or Postscript (.ps) is produced.
2741 If the extension is .ics, run icalendar export over all files used
2742 to construct the agenda and limit the export to entries listed in the
2743 agenda now.
2744 With prefix argument OPEN, open the new file immediately.
2745 If NOSETTINGS is given, do not scope the settings of
2746 `org-agenda-exporter-settings' into the export commands. This is used when
2747 the settings have already been scoped and we do not wish to overrule other,
2748 higher priority settings."
2749 (interactive "FWrite agenda to file: \nP")
2750 (if (not (file-writable-p file))
2751 (error "Cannot write agenda to file %s" file))
2752 (org-let (if nosettings nil org-agenda-exporter-settings)
2753 '(save-excursion
2754 (save-window-excursion
2755 (org-agenda-mark-filtered-text)
2756 (let ((bs (copy-sequence (buffer-string))) beg)
2757 (org-agenda-unmark-filtered-text)
2758 (with-temp-buffer
2759 (rename-buffer "Agenda View" t)
2760 (set-buffer-modified-p nil)
2761 (insert bs)
2762 (org-agenda-remove-marked-text 'org-filtered)
2763 (while (setq beg (text-property-any (point-min) (point-max)
2764 'org-filtered t))
2765 (delete-region
2766 beg (or (next-single-property-change beg 'org-filtered)
2767 (point-max))))
2768 (run-hooks 'org-agenda-before-write-hook)
2769 (cond
2770 ((org-bound-and-true-p org-mobile-creating-agendas)
2771 (org-mobile-write-agenda-for-mobile file))
2772 ((string-match "\\.html?\\'" file)
2773 (require 'htmlize)
2774 (set-buffer (htmlize-buffer (current-buffer)))
2776 (when (and org-agenda-export-html-style
2777 (string-match "<style>" org-agenda-export-html-style))
2778 ;; replace <style> section with org-agenda-export-html-style
2779 (goto-char (point-min))
2780 (kill-region (- (search-forward "<style") 6)
2781 (search-forward "</style>"))
2782 (insert org-agenda-export-html-style))
2783 (write-file file)
2784 (kill-buffer (current-buffer))
2785 (message "HTML written to %s" file))
2786 ((string-match "\\.ps\\'" file)
2787 (require 'ps-print)
2788 (ps-print-buffer-with-faces file)
2789 (message "Postscript written to %s" file))
2790 ((string-match "\\.pdf\\'" file)
2791 (require 'ps-print)
2792 (ps-print-buffer-with-faces
2793 (concat (file-name-sans-extension file) ".ps"))
2794 (call-process "ps2pdf" nil nil nil
2795 (expand-file-name
2796 (concat (file-name-sans-extension file) ".ps"))
2797 (expand-file-name file))
2798 (delete-file (concat (file-name-sans-extension file) ".ps"))
2799 (message "PDF written to %s" file))
2800 ((string-match "\\.ics\\'" file)
2801 (require 'org-icalendar)
2802 (let ((org-agenda-marker-table
2803 (org-create-marker-find-array
2804 (org-agenda-collect-markers)))
2805 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2806 (org-combined-agenda-icalendar-file file))
2807 (apply 'org-export-icalendar 'combine
2808 (org-agenda-files nil 'ifmode))))
2810 (let ((bs (buffer-string)))
2811 (find-file file)
2812 (erase-buffer)
2813 (insert bs)
2814 (save-buffer 0)
2815 (kill-buffer (current-buffer))
2816 (message "Plain text written to %s" file))))))))
2817 (set-buffer org-agenda-buffer-name))
2818 (when open (org-open-file file)))
2820 (defvar org-agenda-filter-overlays nil)
2822 (defun org-agenda-mark-filtered-text ()
2823 "Mark all text hidden by filtering with a text property."
2824 (let ((inhibit-read-only t))
2825 (mapc
2826 (lambda (o)
2827 (when (equal (overlay-buffer o) (current-buffer))
2828 (put-text-property
2829 (overlay-start o) (overlay-end o)
2830 'org-filtered t)))
2831 org-agenda-filter-overlays)))
2833 (defun org-agenda-unmark-filtered-text ()
2834 "Remove the filtering text property."
2835 (let ((inhibit-read-only t))
2836 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2838 (defun org-agenda-remove-marked-text (property &optional value)
2839 "Delete all text marked with VALUE of PROPERTY.
2840 VALUE defaults to t."
2841 (let (beg)
2842 (setq value (or value t))
2843 (while (setq beg (text-property-any (point-min) (point-max)
2844 property value))
2845 (delete-region
2846 beg (or (next-single-property-change beg 'org-filtered)
2847 (point-max))))))
2849 (defun org-agenda-add-entry-text ()
2850 "Add entry text to agenda lines.
2851 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2852 entry text following headings shown in the agenda.
2853 Drawers will be excluded, also the line with scheduling/deadline info."
2854 (when (and (> org-agenda-add-entry-text-maxlines 0)
2855 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2856 (let (m txt)
2857 (goto-char (point-min))
2858 (while (not (eobp))
2859 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2860 (beginning-of-line 2)
2861 (setq txt (org-agenda-get-some-entry-text
2862 m org-agenda-add-entry-text-maxlines " > "))
2863 (end-of-line 1)
2864 (if (string-match "\\S-" txt)
2865 (insert "\n" txt)
2866 (or (eobp) (forward-char 1))))))))
2868 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2869 &rest keep)
2870 "Extract entry text from MARKER, at most N-LINES lines.
2871 This will ignore drawers etc, just get the text.
2872 If INDENT is given, prefix every line with this string. If KEEP is
2873 given, it is a list of symbols, defining stuff that should not be
2874 removed from the entry content. Currently only `planning' is allowed here."
2875 (let (txt drawer-re kwd-time-re ind)
2876 (save-excursion
2877 (with-current-buffer (marker-buffer marker)
2878 (if (not (org-mode-p))
2879 (setq txt "")
2880 (save-excursion
2881 (save-restriction
2882 (widen)
2883 (goto-char marker)
2884 (end-of-line 1)
2885 (setq txt (buffer-substring
2886 (min (1+ (point)) (point-max))
2887 (progn (outline-next-heading) (point)))
2888 drawer-re org-drawer-regexp
2889 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2890 ".*\n?"))
2891 (with-temp-buffer
2892 (insert txt)
2893 (when org-agenda-add-entry-text-descriptive-links
2894 (goto-char (point-min))
2895 (while (org-activate-bracket-links (point-max))
2896 (add-text-properties (match-beginning 0) (match-end 0)
2897 '(face org-link))))
2898 (goto-char (point-min))
2899 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2900 (set-text-properties (match-beginning 0) (match-end 0)
2901 nil))
2902 (goto-char (point-min))
2903 (while (re-search-forward drawer-re nil t)
2904 (delete-region
2905 (match-beginning 0)
2906 (progn (re-search-forward
2907 "^[ \t]*:END:.*\n?" nil 'move)
2908 (point))))
2909 (unless (member 'planning keep)
2910 (goto-char (point-min))
2911 (while (re-search-forward kwd-time-re nil t)
2912 (replace-match "")))
2913 (goto-char (point-min))
2914 (when org-agenda-entry-text-exclude-regexps
2915 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2916 (while (setq re (pop re-list))
2917 (goto-char (point-min))
2918 (while (re-search-forward re nil t)
2919 (replace-match "")))))
2920 (goto-char (point-max))
2921 (skip-chars-backward " \t\n")
2922 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2924 ;; find and remove min common indentation
2925 (goto-char (point-min))
2926 (untabify (point-min) (point-max))
2927 (setq ind (org-get-indentation))
2928 (while (not (eobp))
2929 (unless (looking-at "[ \t]*$")
2930 (setq ind (min ind (org-get-indentation))))
2931 (beginning-of-line 2))
2932 (goto-char (point-min))
2933 (while (not (eobp))
2934 (unless (looking-at "[ \t]*$")
2935 (move-to-column ind)
2936 (delete-region (point-at-bol) (point)))
2937 (beginning-of-line 2))
2939 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2941 (goto-char (point-min))
2942 (when indent
2943 (while (and (not (eobp)) (re-search-forward "^" nil t))
2944 (replace-match indent t t)))
2945 (goto-char (point-min))
2946 (while (looking-at "[ \t]*\n") (replace-match ""))
2947 (goto-char (point-max))
2948 (when (> (org-current-line)
2949 n-lines)
2950 (org-goto-line (1+ n-lines))
2951 (backward-char 1))
2952 (setq txt (buffer-substring (point-min) (point)))))))))
2953 txt))
2955 (defun org-agenda-collect-markers ()
2956 "Collect the markers pointing to entries in the agenda buffer."
2957 (let (m markers)
2958 (save-excursion
2959 (goto-char (point-min))
2960 (while (not (eobp))
2961 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2962 (org-get-at-bol 'org-marker)))
2963 (push m markers))
2964 (beginning-of-line 2)))
2965 (nreverse markers)))
2967 (defun org-create-marker-find-array (marker-list)
2968 "Create a alist of files names with all marker positions in that file."
2969 (let (f tbl m a p)
2970 (while (setq m (pop marker-list))
2971 (setq p (marker-position m)
2972 f (buffer-file-name (or (buffer-base-buffer
2973 (marker-buffer m))
2974 (marker-buffer m))))
2975 (if (setq a (assoc f tbl))
2976 (push (marker-position m) (cdr a))
2977 (push (list f p) tbl)))
2978 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2979 tbl)))
2981 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2982 (defun org-check-agenda-marker-table ()
2983 "Check of the current entry is on the marker list."
2984 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2986 (and (setq a (assoc file org-agenda-marker-table))
2987 (save-match-data
2988 (save-excursion
2989 (org-back-to-heading t)
2990 (member (point) (cdr a)))))))
2992 (defun org-check-for-org-mode ()
2993 "Make sure current buffer is in org-mode. Error if not."
2994 (or (org-mode-p)
2995 (error "Cannot execute org-mode agenda command on buffer in %s"
2996 major-mode)))
2998 (defun org-fit-agenda-window ()
2999 "Fit the window to the buffer size."
3000 (and (memq org-agenda-window-setup '(reorganize-frame))
3001 (fboundp 'fit-window-to-buffer)
3002 (org-fit-window-to-buffer
3004 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3005 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3007 ;;; Agenda prepare and finalize
3009 (defvar org-agenda-multi nil) ; dynamically scoped
3010 (defvar org-agenda-buffer-name "*Org Agenda*")
3011 (defvar org-pre-agenda-window-conf nil)
3012 (defvar org-agenda-columns-active nil)
3013 (defvar org-agenda-name nil)
3014 (defvar org-agenda-filter nil)
3015 (defvar org-agenda-filter-while-redo nil)
3016 (defvar org-agenda-filter-preset nil
3017 "A preset of the tags filter used for secondary agenda filtering.
3018 This must be a list of strings, each string must be a single tag preceded
3019 by \"+\" or \"-\".
3020 This variable should not be set directly, but agenda custom commands can
3021 bind it in the options section. The preset filter is a global property of
3022 the entire agenda view. In a block agenda, it will not work reliably to
3023 define a filter for one of the individual blocks. You need to set it in
3024 the global options and expect it to be applied to the entire view.")
3026 (defun org-prepare-agenda (&optional name)
3027 (setq org-todo-keywords-for-agenda nil)
3028 (setq org-done-keywords-for-agenda nil)
3029 (setq org-drawers-for-agenda nil)
3030 (unless org-agenda-persistent-filter
3031 (setq org-agenda-filter nil))
3032 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3033 (if org-agenda-multi
3034 (progn
3035 (setq buffer-read-only nil)
3036 (goto-char (point-max))
3037 (unless (or (bobp) org-agenda-compact-blocks
3038 (not org-agenda-block-separator))
3039 (insert "\n"
3040 (if (stringp org-agenda-block-separator)
3041 org-agenda-block-separator
3042 (make-string (window-width) org-agenda-block-separator))
3043 "\n"))
3044 (narrow-to-region (point) (point-max)))
3045 (org-agenda-reset-markers)
3046 (setq org-agenda-contributing-files nil)
3047 (setq org-agenda-columns-active nil)
3048 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3049 (setq org-todo-keywords-for-agenda
3050 (org-uniquify org-todo-keywords-for-agenda))
3051 (setq org-done-keywords-for-agenda
3052 (org-uniquify org-done-keywords-for-agenda))
3053 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3054 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3055 (awin (get-buffer-window abuf)))
3056 (cond
3057 ((equal (current-buffer) abuf) nil)
3058 (awin (select-window awin))
3059 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3060 ((equal org-agenda-window-setup 'current-window)
3061 (switch-to-buffer abuf))
3062 ((equal org-agenda-window-setup 'other-window)
3063 (org-switch-to-buffer-other-window abuf))
3064 ((equal org-agenda-window-setup 'other-frame)
3065 (switch-to-buffer-other-frame abuf))
3066 ((equal org-agenda-window-setup 'reorganize-frame)
3067 (delete-other-windows)
3068 (org-switch-to-buffer-other-window abuf)))
3069 ;; additional test in case agenda is invoked from within agenda
3070 ;; buffer via elisp link
3071 (unless (equal (current-buffer) abuf)
3072 (switch-to-buffer abuf)))
3073 (setq buffer-read-only nil)
3074 (let ((inhibit-read-only t)) (erase-buffer))
3075 (org-agenda-mode)
3076 (and name (not org-agenda-name)
3077 (org-set-local 'org-agenda-name name)))
3078 (setq buffer-read-only nil))
3080 (defun org-finalize-agenda ()
3081 "Finishing touch for the agenda buffer, called just before displaying it."
3082 (unless org-agenda-multi
3083 (save-excursion
3084 (let ((inhibit-read-only t))
3085 (goto-char (point-min))
3086 (while (org-activate-bracket-links (point-max))
3087 (add-text-properties (match-beginning 0) (match-end 0)
3088 '(face org-link)))
3089 (org-agenda-align-tags)
3090 (unless org-agenda-with-colors
3091 (remove-text-properties (point-min) (point-max) '(face nil))))
3092 (if (and (boundp 'org-agenda-overriding-columns-format)
3093 org-agenda-overriding-columns-format)
3094 (org-set-local 'org-agenda-overriding-columns-format
3095 org-agenda-overriding-columns-format))
3096 (if (and (boundp 'org-agenda-view-columns-initially)
3097 org-agenda-view-columns-initially)
3098 (org-agenda-columns))
3099 (when org-agenda-fontify-priorities
3100 (org-agenda-fontify-priorities))
3101 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3102 (org-agenda-dim-blocked-tasks))
3103 (org-agenda-mark-clocking-task)
3104 (when org-agenda-entry-text-mode
3105 (org-agenda-entry-text-hide)
3106 (org-agenda-entry-text-show))
3107 (if (functionp 'org-habit-insert-consistency-graphs)
3108 (org-habit-insert-consistency-graphs))
3109 (run-hooks 'org-finalize-agenda-hook)
3110 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3111 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3112 (org-agenda-filter-apply org-agenda-filter))
3115 (defun org-agenda-mark-clocking-task ()
3116 "Mark the current clock entry in the agenda if it is present."
3117 (mapc (lambda (o)
3118 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3119 (delete-overlay o)))
3120 (overlays-in (point-min) (point-max)))
3121 (when (marker-buffer org-clock-hd-marker)
3122 (save-excursion
3123 (goto-char (point-min))
3124 (let (s ov)
3125 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3126 (goto-char s)
3127 (when (equal (org-get-at-bol 'org-hd-marker)
3128 org-clock-hd-marker)
3129 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3130 (overlay-put ov 'type 'org-agenda-clocking)
3131 (overlay-put ov 'face 'org-agenda-clocking)
3132 (overlay-put ov 'help-echo
3133 "The clock is running in this item")))))))
3135 (defun org-agenda-fontify-priorities ()
3136 "Make highest priority lines bold, and lowest italic."
3137 (interactive)
3138 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3139 (delete-overlay o)))
3140 (overlays-in (point-min) (point-max)))
3141 (save-excursion
3142 (let ((inhibit-read-only t)
3143 b e p ov h l)
3144 (goto-char (point-min))
3145 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3146 (setq h (or (get-char-property (point) 'org-highest-priority)
3147 org-highest-priority)
3148 l (or (get-char-property (point) 'org-lowest-priority)
3149 org-lowest-priority)
3150 p (string-to-char (match-string 1))
3151 b (match-beginning 0)
3152 e (if (eq org-agenda-fontify-priorities 'cookies)
3153 (match-end 0)
3154 (point-at-eol))
3155 ov (make-overlay b e))
3156 (overlay-put
3157 ov 'face
3158 (cond ((org-face-from-face-or-color
3159 'priority nil
3160 (cdr (assoc p org-priority-faces))))
3161 ((and (listp org-agenda-fontify-priorities)
3162 (org-face-from-face-or-color
3163 'priority nil
3164 (cdr (assoc p org-agenda-fontify-priorities)))))
3165 ((equal p l) 'italic)
3166 ((equal p h) 'bold)))
3167 (overlay-put ov 'org-type 'org-priority)))))
3169 (defun org-agenda-dim-blocked-tasks ()
3170 "Dim currently blocked TODO's in the agenda display."
3171 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3172 (delete-overlay o)))
3173 (overlays-in (point-min) (point-max)))
3174 (save-excursion
3175 (let ((inhibit-read-only t)
3176 (org-depend-tag-blocked nil)
3177 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3178 org-blocked-by-checkboxes
3179 invis1 b e p ov h l)
3180 (goto-char (point-min))
3181 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3182 (and pos (goto-char (1+ pos))))
3183 (setq org-blocked-by-checkboxes nil invis1 invis)
3184 (let ((marker (org-get-at-bol 'org-hd-marker)))
3185 (when (and marker
3186 (not (with-current-buffer (marker-buffer marker)
3187 (save-excursion
3188 (goto-char marker)
3189 (if (org-entry-get nil "NOBLOCKING")
3190 t ;; Never block this entry
3191 (run-hook-with-args-until-failure
3192 'org-blocker-hook
3193 (list :type 'todo-state-change
3194 :position marker
3195 :from 'todo
3196 :to 'done)))))))
3197 (if org-blocked-by-checkboxes (setq invis1 nil))
3198 (setq b (if invis1
3199 (max (point-min) (1- (point-at-bol)))
3200 (point-at-bol))
3201 e (point-at-eol)
3202 ov (make-overlay b e))
3203 (if invis1
3204 (overlay-put ov 'invisible t)
3205 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3206 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3208 (defvar org-agenda-skip-function nil
3209 "Function to be called at each match during agenda construction.
3210 If this function returns nil, the current match should not be skipped.
3211 Otherwise, the function must return a position from where the search
3212 should be continued.
3213 This may also be a Lisp form, it will be evaluated.
3214 Never set this variable using `setq' or so, because then it will apply
3215 to all future agenda commands. If you do want a global skipping condition,
3216 use the option `org-agenda-skip-function-global' instead.
3217 The correct usage for `org-agenda-skip-function' is to bind it with
3218 `let' to scope it dynamically into the agenda-constructing command.
3219 A good way to set it is through options in `org-agenda-custom-commands'.")
3221 (defun org-agenda-skip ()
3222 "Throw to `:skip' in places that should be skipped.
3223 Also moves point to the end of the skipped region, so that search can
3224 continue from there."
3225 (let ((p (point-at-bol)) to)
3226 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3227 (get-text-property p :org-archived)
3228 (org-end-of-subtree t)
3229 (throw :skip t))
3230 (and org-agenda-skip-comment-trees
3231 (get-text-property p :org-comment)
3232 (org-end-of-subtree t)
3233 (throw :skip t))
3234 (if (equal (char-after p) ?#) (throw :skip t))
3235 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3236 (org-agenda-skip-eval org-agenda-skip-function)))
3237 (goto-char to)
3238 (throw :skip t))))
3240 (defun org-agenda-skip-eval (form)
3241 "If FORM is a function or a list, call (or eval) is and return result.
3242 `save-excursion' and `save-match-data' are wrapped around the call, so point
3243 and match data are returned to the previous state no matter what these
3244 functions do."
3245 (let (fp)
3246 (and form
3247 (or (setq fp (functionp form))
3248 (consp form))
3249 (save-excursion
3250 (save-match-data
3251 (if fp
3252 (funcall form)
3253 (eval form)))))))
3255 (defvar org-agenda-markers nil
3256 "List of all currently active markers created by `org-agenda'.")
3257 (defvar org-agenda-last-marker-time (org-float-time)
3258 "Creation time of the last agenda marker.")
3260 (defun org-agenda-new-marker (&optional pos)
3261 "Return a new agenda marker.
3262 Org-mode keeps a list of these markers and resets them when they are
3263 no longer in use."
3264 (let ((m (copy-marker (or pos (point)))))
3265 (setq org-agenda-last-marker-time (org-float-time))
3266 (push m org-agenda-markers)
3269 (defun org-agenda-reset-markers ()
3270 "Reset markers created by `org-agenda'."
3271 (while org-agenda-markers
3272 (move-marker (pop org-agenda-markers) nil)))
3274 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3275 "Save relative positions of markers in region."
3276 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3277 org-agenda-markers))
3279 ;;; Entry text mode
3281 (defun org-agenda-entry-text-show-here ()
3282 "Add some text from the entry as context to the current line."
3283 (let (m txt o)
3284 (setq m (org-get-at-bol 'org-hd-marker))
3285 (unless (marker-buffer m)
3286 (error "No marker points to an entry here"))
3287 (setq txt (concat "\n" (org-no-properties
3288 (org-agenda-get-some-entry-text
3289 m org-agenda-entry-text-maxlines " > "))))
3290 (when (string-match "\\S-" txt)
3291 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3292 (overlay-put o 'evaporate t)
3293 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3294 (overlay-put o 'after-string txt))))
3296 (defun org-agenda-entry-text-show ()
3297 "Add entry context for all agenda lines."
3298 (interactive)
3299 (save-excursion
3300 (goto-char (point-max))
3301 (beginning-of-line 1)
3302 (while (not (bobp))
3303 (when (org-get-at-bol 'org-hd-marker)
3304 (org-agenda-entry-text-show-here))
3305 (beginning-of-line 0))))
3307 (defun org-agenda-entry-text-hide ()
3308 "Remove any shown entry context."
3309 (delq nil
3310 (mapcar (lambda (o)
3311 (if (eq (overlay-get o 'org-overlay-type)
3312 'agenda-entry-content)
3313 (progn (delete-overlay o) t)))
3314 (overlays-in (point-min) (point-max)))))
3316 (defun org-agenda-get-day-face (date)
3317 "Return the face DATE should be displayed with."
3318 (or (and (functionp org-agenda-day-face-function)
3319 (funcall org-agenda-day-face-function date))
3320 (cond ((org-agenda-todayp date)
3321 'org-agenda-date-today)
3322 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3323 'org-agenda-date-weekend)
3324 (t 'org-agenda-date))))
3326 ;;; Agenda timeline
3328 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3330 (defun org-timeline (&optional include-all)
3331 "Show a time-sorted view of the entries in the current org file.
3332 Only entries with a time stamp of today or later will be listed. With
3333 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3334 under the current date.
3335 If the buffer contains an active region, only check the region for
3336 dates."
3337 (interactive "P")
3338 (org-compile-prefix-format 'timeline)
3339 (org-set-sorting-strategy 'timeline)
3340 (let* ((dopast t)
3341 (dotodo include-all)
3342 (doclosed org-agenda-show-log)
3343 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3344 (current-buffer))))
3345 (date (calendar-current-date))
3346 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3347 (end (if (org-region-active-p) (region-end) (point-max)))
3348 (day-numbers (org-get-all-dates beg end 'no-ranges
3349 t doclosed ; always include today
3350 org-timeline-show-empty-dates))
3351 (org-deadline-warning-days 0)
3352 (org-agenda-only-exact-dates t)
3353 (today (org-today))
3354 (past t)
3355 args
3356 s e rtn d emptyp)
3357 (setq org-agenda-redo-command
3358 (list 'progn
3359 (list 'org-switch-to-buffer-other-window (current-buffer))
3360 (list 'org-timeline (list 'quote include-all))))
3361 (if (not dopast)
3362 ;; Remove past dates from the list of dates.
3363 (setq day-numbers (delq nil (mapcar (lambda(x)
3364 (if (>= x today) x nil))
3365 day-numbers))))
3366 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3367 (if doclosed (push :closed args))
3368 (push :timestamp args)
3369 (push :deadline args)
3370 (push :scheduled args)
3371 (push :sexp args)
3372 (if dotodo (push :todo args))
3373 (insert "Timeline of file " entry "\n")
3374 (add-text-properties (point-min) (point)
3375 (list 'face 'org-agenda-structure))
3376 (org-agenda-mark-header-line (point-min))
3377 (while (setq d (pop day-numbers))
3378 (if (and (listp d) (eq (car d) :omitted))
3379 (progn
3380 (setq s (point))
3381 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3382 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3383 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3384 (if (and (>= d today)
3385 dopast
3386 past)
3387 (progn
3388 (setq past nil)
3389 (insert (make-string 79 ?-) "\n")))
3390 (setq date (calendar-gregorian-from-absolute d))
3391 (setq s (point))
3392 (setq rtn (and (not emptyp)
3393 (apply 'org-agenda-get-day-entries entry
3394 date args)))
3395 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3396 (progn
3397 (insert
3398 (if (stringp org-agenda-format-date)
3399 (format-time-string org-agenda-format-date
3400 (org-time-from-absolute date))
3401 (funcall org-agenda-format-date date))
3402 "\n")
3403 (put-text-property s (1- (point)) 'face
3404 (org-agenda-get-day-face date))
3405 (put-text-property s (1- (point)) 'org-date-line t)
3406 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3407 (if (equal d today)
3408 (put-text-property s (1- (point)) 'org-today t))
3409 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3410 (put-text-property s (1- (point)) 'day d)))))
3411 (goto-char (point-min))
3412 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3413 (point-min)))
3414 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3415 (org-finalize-agenda)
3416 (setq buffer-read-only t)))
3418 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3419 "Return a list of all relevant day numbers from BEG to END buffer positions.
3420 If NO-RANGES is non-nil, include only the start and end dates of a range,
3421 not every single day in the range. If FORCE-TODAY is non-nil, make
3422 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3423 inactive time stamps (those in square brackets) are included.
3424 When EMPTY is non-nil, also include days without any entries."
3425 (let ((re (concat
3426 (if pre-re pre-re "")
3427 (if inactive org-ts-regexp-both org-ts-regexp)))
3428 dates dates1 date day day1 day2 ts1 ts2)
3429 (if force-today
3430 (setq dates (list (org-today))))
3431 (save-excursion
3432 (goto-char beg)
3433 (while (re-search-forward re end t)
3434 (setq day (time-to-days (org-time-string-to-time
3435 (substring (match-string 1) 0 10))))
3436 (or (memq day dates) (push day dates)))
3437 (unless no-ranges
3438 (goto-char beg)
3439 (while (re-search-forward org-tr-regexp end t)
3440 (setq ts1 (substring (match-string 1) 0 10)
3441 ts2 (substring (match-string 2) 0 10)
3442 day1 (time-to-days (org-time-string-to-time ts1))
3443 day2 (time-to-days (org-time-string-to-time ts2)))
3444 (while (< (setq day1 (1+ day1)) day2)
3445 (or (memq day1 dates) (push day1 dates)))))
3446 (setq dates (sort dates '<))
3447 (when empty
3448 (while (setq day (pop dates))
3449 (setq day2 (car dates))
3450 (push day dates1)
3451 (when (and day2 empty)
3452 (if (or (eq empty t)
3453 (and (numberp empty) (<= (- day2 day) empty)))
3454 (while (< (setq day (1+ day)) day2)
3455 (push (list day) dates1))
3456 (push (cons :omitted (- day2 day)) dates1))))
3457 (setq dates (nreverse dates1)))
3458 dates)))
3460 ;;; Agenda Daily/Weekly
3462 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3463 "Start day for the agenda view.
3464 Custom commands can set this variable in the options section.")
3465 (defvar org-starting-day nil) ; local variable in the agenda buffer
3466 (defvar org-agenda-current-span nil
3467 "The current span used in the agenda view.") ; local variable in the agenda buffer
3468 (defvar org-include-all-loc nil) ; local variable
3470 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3471 "List of types searched for when creating the daily/weekly agenda.
3472 This variable is a list of symbols that controls the types of
3473 items that appear in the daily/weekly agenda. Allowed symbols in this
3474 list are are
3476 :timestamp List items containing a date stamp or date range matching
3477 the selected date. This includes sexp entries in
3478 angular brackets.
3480 :sexp List entries resulting from plain diary-like sexps.
3482 :deadline List deadline due on that date. When the date is today,
3483 also list any deadlines past due, or due within
3484 `org-deadline-warning-days'. `:deadline' must appear before
3485 `:scheduled' if the setting of
3486 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3487 any effect.
3489 :scheduled List all items which are scheduled for the given date.
3490 The diary for *today* also contains items which were
3491 scheduled earlier and are not yet marked DONE.
3493 By default, all four types are turned on.
3495 Never set this variable globally using `setq', because then it
3496 will apply to all future agenda commands. Instead, bind it with
3497 `let' to scope it dynamically into the agenda-constructing
3498 command. A good way to set it is through options in
3499 `org-agenda-custom-commands'. For a more flexible (though
3500 somewhat less efficient) way of determining what is included in
3501 the daily/weekly agenda, see `org-agenda-skip-function'.")
3503 ;;;###autoload
3504 (defun org-agenda-list (&optional include-all start-day span)
3505 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3506 The view will be for the current day or week, but from the overview buffer
3507 you will be able to go to other days/weeks.
3509 With a numeric prefix argument in an interactive call, the agenda will
3510 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3511 the number of days. SPAN defaults to `org-agenda-span'.
3513 START-DAY defaults to TODAY, or to the most recent match for the weekday
3514 given in `org-agenda-start-on-weekday'."
3515 (interactive "P")
3516 (if (and (integerp include-all) (> include-all 0))
3517 (setq span include-all include-all nil))
3518 (setq start-day (or start-day org-agenda-start-day))
3519 (if org-agenda-overriding-arguments
3520 (setq include-all (car org-agenda-overriding-arguments)
3521 start-day (nth 1 org-agenda-overriding-arguments)
3522 span (nth 2 org-agenda-overriding-arguments)))
3523 (if (stringp start-day)
3524 ;; Convert to an absolute day number
3525 (setq start-day (time-to-days (org-read-date nil t start-day))))
3526 (setq org-agenda-last-arguments (list include-all start-day span))
3527 (org-compile-prefix-format 'agenda)
3528 (org-set-sorting-strategy 'agenda)
3529 (let* ((span (org-agenda-ndays-to-span
3530 (or span org-agenda-ndays org-agenda-span)))
3531 (today (org-today))
3532 (sd (or start-day today))
3533 (ndays (org-agenda-span-to-ndays span sd))
3534 (org-agenda-start-on-weekday
3535 (if (eq ndays 7)
3536 org-agenda-start-on-weekday))
3537 (thefiles (org-agenda-files nil 'ifmode))
3538 (files thefiles)
3539 (start (if (or (null org-agenda-start-on-weekday)
3540 (< ndays 7))
3542 (let* ((nt (calendar-day-of-week
3543 (calendar-gregorian-from-absolute sd)))
3544 (n1 org-agenda-start-on-weekday)
3545 (d (- nt n1)))
3546 (- sd (+ (if (< d 0) 7 0) d)))))
3547 (day-numbers (list start))
3548 (day-cnt 0)
3549 (inhibit-redisplay (not debug-on-error))
3550 s e rtn rtnall file date d start-pos end-pos todayp
3551 clocktable-start clocktable-end filter)
3552 (setq org-agenda-redo-command
3553 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3554 (dotimes (n (1- ndays))
3555 (push (1+ (car day-numbers)) day-numbers))
3556 (setq day-numbers (nreverse day-numbers))
3557 (setq clocktable-start (car day-numbers)
3558 clocktable-end (1+ (or (org-last day-numbers) 0)))
3559 (org-prepare-agenda "Day/Week")
3560 (org-set-local 'org-starting-day (car day-numbers))
3561 (org-set-local 'org-include-all-loc include-all)
3562 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3563 (unless org-agenda-compact-blocks
3564 (let* ((d1 (car day-numbers))
3565 (d2 (org-last day-numbers))
3566 (w1 (org-days-to-iso-week d1))
3567 (w2 (org-days-to-iso-week d2)))
3568 (setq s (point))
3569 (if org-agenda-overriding-header
3570 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3571 nil 'face 'org-agenda-structure) "\n")
3572 (insert (org-agenda-span-name span)
3573 "-agenda"
3574 (if (< (- d2 d1) 350)
3575 (if (= w1 w2)
3576 (format " (W%02d)" w1)
3577 (format " (W%02d-W%02d)" w1 w2))
3579 ":\n")))
3580 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3581 'org-date-line t))
3582 (org-agenda-mark-header-line s))
3583 (while (setq d (pop day-numbers))
3584 (setq date (calendar-gregorian-from-absolute d)
3585 s (point))
3586 (if (or (setq todayp (= d today))
3587 (and (not start-pos) (= d sd)))
3588 (setq start-pos (point))
3589 (if (and start-pos (not end-pos))
3590 (setq end-pos (point))))
3591 (setq files thefiles
3592 rtnall nil)
3593 (while (setq file (pop files))
3594 (catch 'nextfile
3595 (org-check-agenda-file file)
3596 (let ((org-agenda-entry-types org-agenda-entry-types))
3597 (unless org-agenda-include-deadlines
3598 (setq org-agenda-entry-types
3599 (delq :deadline org-agenda-entry-types)))
3600 (cond
3601 ((memq org-agenda-show-log '(only clockcheck))
3602 (setq rtn (org-agenda-get-day-entries
3603 file date :closed)))
3604 (org-agenda-show-log
3605 (setq rtn (apply 'org-agenda-get-day-entries
3606 file date
3607 (append '(:closed) org-agenda-entry-types))))
3609 (setq rtn (apply 'org-agenda-get-day-entries
3610 file date
3611 org-agenda-entry-types)))))
3612 (setq rtnall (append rtnall rtn))))
3613 (if org-agenda-include-diary
3614 (let ((org-agenda-search-headline-for-time t))
3615 (require 'diary-lib)
3616 (setq rtn (org-get-entries-from-diary date))
3617 (setq rtnall (append rtnall rtn))))
3618 (if (or rtnall org-agenda-show-all-dates)
3619 (progn
3620 (setq day-cnt (1+ day-cnt))
3621 (insert
3622 (if (stringp org-agenda-format-date)
3623 (format-time-string org-agenda-format-date
3624 (org-time-from-absolute date))
3625 (funcall org-agenda-format-date date))
3626 "\n")
3627 (put-text-property s (1- (point)) 'face
3628 (org-agenda-get-day-face date))
3629 (put-text-property s (1- (point)) 'org-date-line t)
3630 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3631 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3632 (when todayp
3633 (put-text-property s (1- (point)) 'org-today t))
3634 (if rtnall (insert
3635 (org-finalize-agenda-entries
3636 (org-agenda-add-time-grid-maybe
3637 rtnall ndays todayp))
3638 "\n"))
3639 (put-text-property s (1- (point)) 'day d)
3640 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3641 (when (and org-agenda-clockreport-mode clocktable-start)
3642 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3643 ;; the above line is to ensure the restricted range!
3644 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3645 tbl)
3646 (setq p (org-plist-delete p :block))
3647 (setq p (plist-put p :tstart clocktable-start))
3648 (setq p (plist-put p :tend clocktable-end))
3649 (setq p (plist-put p :scope 'agenda))
3650 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3651 (setq filter (or org-agenda-filter-while-redo
3652 (get 'org-agenda-filter :preset-filter))))
3653 (setq p (plist-put p :tags (mapconcat (lambda (x)
3654 (if (string-match "[<>=]" x)
3657 filter ""))))
3658 (setq tbl (apply 'org-get-clocktable p))
3659 (insert tbl)))
3660 (goto-char (point-min))
3661 (or org-agenda-multi (org-fit-agenda-window))
3662 (unless (and (pos-visible-in-window-p (point-min))
3663 (pos-visible-in-window-p (point-max)))
3664 (goto-char (1- (point-max)))
3665 (recenter -1)
3666 (if (not (pos-visible-in-window-p (or start-pos 1)))
3667 (progn
3668 (goto-char (or start-pos 1))
3669 (recenter 1))))
3670 (goto-char (or start-pos 1))
3671 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3672 (if (eq org-agenda-show-log 'clockcheck)
3673 (org-agenda-show-clocking-issues))
3674 (org-finalize-agenda)
3675 (setq buffer-read-only t)
3676 (message "")))
3678 (defun org-agenda-ndays-to-span (n)
3679 "Return a span symbol for a span of N days, or N if none matches."
3680 (cond ((symbolp n) n)
3681 ((= n 1) 'day)
3682 ((= n 7) 'week)
3683 (t n)))
3685 (defun org-agenda-span-to-ndays (span start-day)
3686 "Return ndays from SPAN starting at START-DAY."
3687 (cond ((numberp span) span)
3688 ((eq span 'day) 1)
3689 ((eq span 'week) 7)
3690 ((eq span 'month)
3691 (let ((date (calendar-gregorian-from-absolute start-day)))
3692 (calendar-last-day-of-month (car date) (caddr date))))
3693 ((eq span 'year)
3694 (let ((date (calendar-gregorian-from-absolute start-day)))
3695 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3697 (defun org-agenda-span-name (span)
3698 "Return a SPAN name."
3699 (if (null span)
3701 (if (symbolp span)
3702 (capitalize (symbol-name span))
3703 (format "%d days" span))))
3705 ;;; Agenda word search
3707 (defvar org-agenda-search-history nil)
3708 (defvar org-todo-only nil)
3710 (defvar org-search-syntax-table nil
3711 "Special syntax table for org-mode search.
3712 In this table, we have single quotes not as word constituents, to
3713 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3715 (defun org-search-syntax-table ()
3716 (unless org-search-syntax-table
3717 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3718 (modify-syntax-entry ?' "." org-search-syntax-table)
3719 (modify-syntax-entry ?` "." org-search-syntax-table))
3720 org-search-syntax-table)
3722 (defvar org-agenda-last-search-view-search-was-boolean nil)
3724 ;;;###autoload
3725 (defun org-search-view (&optional todo-only string edit-at)
3726 "Show all entries that contain a phrase or words or regular expressions.
3728 With optional prefix argument TODO-ONLY, only consider entries that are
3729 TODO entries. The argument STRING can be used to pass a default search
3730 string into this function. If EDIT-AT is non-nil, it means that the
3731 user should get a chance to edit this string, with cursor at position
3732 EDIT-AT.
3734 The search string can be viewed either as a phrase that should be found as
3735 is, or it can be broken into a number of snippets, each of which must match
3736 in a Boolean way to select an entry. The default depends on the variable
3737 `org-agenda-search-view-always-boolean'.
3738 Even if this is turned off (the default) you can always switch to
3739 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3741 The default is a direct search of the whole phrase, where each space in
3742 the search string can expand to an arbitrary amount of whitespace,
3743 including newlines.
3745 If using a Boolean search, the search string is split on whitespace and
3746 each snippet is searched separately, with logical AND to select an entry.
3747 Words prefixed with a minus must *not* occur in the entry. Words without
3748 a prefix or prefixed with a plus must occur in the entry. Matching is
3749 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3750 match whole words, not parts of a word) if
3751 `org-agenda-search-view-force-full-words' is set (default is nil).
3753 Boolean search snippets enclosed by curly braces are interpreted as
3754 regular expressions that must or (when preceded with \"-\") must not
3755 match in the entry. Snippets enclosed into double quotes will be taken
3756 as a whole, to include whitespace.
3758 - If the search string starts with an asterisk, search only in headlines.
3759 - If (possibly after the leading star) the search string starts with an
3760 exclamation mark, this also means to look at TODO entries only, an effect
3761 that can also be achieved with a prefix argument.
3762 - If (possibly after star and exclamation mark) the search string starts
3763 with a colon, this will mean that the (non-regexp) snippets of the
3764 Boolean search must match as full words.
3766 This command searches the agenda files, and in addition the files listed
3767 in `org-agenda-text-search-extra-files'."
3768 (interactive "P")
3769 (org-compile-prefix-format 'search)
3770 (org-set-sorting-strategy 'search)
3771 (org-prepare-agenda "SEARCH")
3772 (let* ((props (list 'face nil
3773 'done-face 'org-agenda-done
3774 'org-not-done-regexp org-not-done-regexp
3775 'org-todo-regexp org-todo-regexp
3776 'org-complex-heading-regexp org-complex-heading-regexp
3777 'mouse-face 'highlight
3778 'help-echo (format "mouse-2 or RET jump to location")))
3779 (full-words org-agenda-search-view-force-full-words)
3780 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3781 regexp rtn rtnall files file pos
3782 marker category tags c neg re boolean
3783 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3784 (unless (and (not edit-at)
3785 (stringp string)
3786 (string-match "\\S-" string))
3787 (setq string (read-string
3788 (if org-agenda-search-view-always-boolean
3789 "[+-]Word/{Regexp} ...: "
3790 "Phrase, or [+-]Word/{Regexp} ...: ")
3791 (cond
3792 ((integerp edit-at) (cons string edit-at))
3793 (edit-at string))
3794 'org-agenda-search-history)))
3795 (org-set-local 'org-todo-only todo-only)
3796 (setq org-agenda-redo-command
3797 (list 'org-search-view (if todo-only t nil) string
3798 '(if current-prefix-arg 1 nil)))
3799 (setq org-agenda-query-string string)
3801 (if (equal (string-to-char string) ?*)
3802 (setq hdl-only t
3803 words (substring string 1))
3804 (setq words string))
3805 (when (equal (string-to-char words) ?!)
3806 (setq todo-only t
3807 words (substring words 1)))
3808 (when (equal (string-to-char words) ?:)
3809 (setq full-words t
3810 words (substring words 1)))
3811 (if (or org-agenda-search-view-always-boolean
3812 (member (string-to-char words) '(?- ?+ ?\{)))
3813 (setq boolean t))
3814 (setq words (org-split-string words))
3815 (let (www w)
3816 (while (setq w (pop words))
3817 (while (and (string-match "\\\\\\'" w) words)
3818 (setq w (concat (substring w 0 -1) " " (pop words))))
3819 (push w www))
3820 (setq words (nreverse www) www nil)
3821 (while (setq w (pop words))
3822 (when (and (string-match "\\`[-+]?{" w)
3823 (not (string-match "}\\'" w)))
3824 (while (and words (not (string-match "}\\'" (car words))))
3825 (setq w (concat w " " (pop words))))
3826 (setq w (concat w " " (pop words))))
3827 (push w www))
3828 (setq words (nreverse www)))
3829 (setq org-agenda-last-search-view-search-was-boolean boolean)
3830 (when boolean
3831 (let (wds w)
3832 (while (setq w (pop words))
3833 (if (or (equal (substring w 0 1) "\"")
3834 (and (> (length w) 1)
3835 (member (substring w 0 1) '("+" "-"))
3836 (equal (substring w 1 2) "\"")))
3837 (while (and words (not (equal (substring w -1) "\"")))
3838 (setq w (concat w " " (pop words)))))
3839 (and (string-match "\\`\\([-+]?\\)\"" w)
3840 (setq w (replace-match "\\1" nil nil w)))
3841 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3842 (push w wds))
3843 (setq words (nreverse wds))))
3844 (if boolean
3845 (mapc (lambda (w)
3846 (setq c (string-to-char w))
3847 (if (equal c ?-)
3848 (setq neg t w (substring w 1))
3849 (if (equal c ?+)
3850 (setq neg nil w (substring w 1))
3851 (setq neg nil)))
3852 (if (string-match "\\`{.*}\\'" w)
3853 (setq re (substring w 1 -1))
3854 (if full-words
3855 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3856 (setq re (regexp-quote (downcase w)))))
3857 (if neg (push re regexps-) (push re regexps+)))
3858 words)
3859 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3860 regexps+))
3861 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3862 (if (not regexps+)
3863 (setq regexp (concat "^" org-outline-regexp))
3864 (setq regexp (pop regexps+))
3865 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3866 regexp))))
3867 (setq files (org-agenda-files nil 'ifmode))
3868 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3869 (pop org-agenda-text-search-extra-files)
3870 (setq files (org-add-archive-files files)))
3871 (setq files (append files org-agenda-text-search-extra-files)
3872 rtnall nil)
3873 (while (setq file (pop files))
3874 (setq ee nil)
3875 (catch 'nextfile
3876 (org-check-agenda-file file)
3877 (setq buffer (if (file-exists-p file)
3878 (org-get-agenda-file-buffer file)
3879 (error "No such file %s" file)))
3880 (if (not buffer)
3881 ;; If file does not exist, make sure an error message is sent
3882 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3883 file))))
3884 (with-current-buffer buffer
3885 (with-syntax-table (org-search-syntax-table)
3886 (unless (org-mode-p)
3887 (error "Agenda file %s is not in `org-mode'" file))
3888 (let ((case-fold-search t))
3889 (save-excursion
3890 (save-restriction
3891 (if org-agenda-restrict
3892 (narrow-to-region org-agenda-restrict-begin
3893 org-agenda-restrict-end)
3894 (widen))
3895 (goto-char (point-min))
3896 (unless (or (org-on-heading-p)
3897 (outline-next-heading))
3898 (throw 'nextfile t))
3899 (goto-char (max (point-min) (1- (point))))
3900 (while (re-search-forward regexp nil t)
3901 (org-back-to-heading t)
3902 (skip-chars-forward "* ")
3903 (setq beg (point-at-bol)
3904 beg1 (point)
3905 end (progn (outline-next-heading) (point)))
3906 (catch :skip
3907 (goto-char beg)
3908 (org-agenda-skip)
3909 (setq str (buffer-substring-no-properties
3910 (point-at-bol)
3911 (if hdl-only (point-at-eol) end)))
3912 (mapc (lambda (wr) (when (string-match wr str)
3913 (goto-char (1- end))
3914 (throw :skip t)))
3915 regexps-)
3916 (mapc (lambda (wr) (unless (string-match wr str)
3917 (goto-char (1- end))
3918 (throw :skip t)))
3919 (if todo-only
3920 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3921 regexps+)
3922 regexps+))
3923 (goto-char beg)
3924 (setq marker (org-agenda-new-marker (point))
3925 category (org-get-category)
3926 tags (org-get-tags-at (point))
3927 txt (org-format-agenda-item
3929 (buffer-substring-no-properties
3930 beg1 (point-at-eol))
3931 category tags))
3932 (org-add-props txt props
3933 'org-marker marker 'org-hd-marker marker
3934 'org-todo-regexp org-todo-regexp
3935 'org-complex-heading-regexp org-complex-heading-regexp
3936 'priority 1000 'org-category category
3937 'type "search")
3938 (push txt ee)
3939 (goto-char (1- end))))))))))
3940 (setq rtn (nreverse ee))
3941 (setq rtnall (append rtnall rtn)))
3942 (if org-agenda-overriding-header
3943 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3944 nil 'face 'org-agenda-structure) "\n")
3945 (insert "Search words: ")
3946 (add-text-properties (point-min) (1- (point))
3947 (list 'face 'org-agenda-structure))
3948 (setq pos (point))
3949 (insert string "\n")
3950 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3951 (setq pos (point))
3952 (unless org-agenda-multi
3953 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3954 (add-text-properties pos (1- (point))
3955 (list 'face 'org-agenda-structure))))
3956 (org-agenda-mark-header-line (point-min))
3957 (when rtnall
3958 (insert (org-finalize-agenda-entries rtnall) "\n"))
3959 (goto-char (point-min))
3960 (or org-agenda-multi (org-fit-agenda-window))
3961 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3962 (org-finalize-agenda)
3963 (setq buffer-read-only t)))
3965 ;;; Agenda TODO list
3967 (defvar org-select-this-todo-keyword nil)
3968 (defvar org-last-arg nil)
3970 ;;;###autoload
3971 (defun org-todo-list (arg)
3972 "Show all (not done) TODO entries from all agenda file in a single list.
3973 The prefix arg can be used to select a specific TODO keyword and limit
3974 the list to these. When using \\[universal-argument], you will be prompted
3975 for a keyword. A numeric prefix directly selects the Nth keyword in
3976 `org-todo-keywords-1'."
3977 (interactive "P")
3978 (org-compile-prefix-format 'todo)
3979 (org-set-sorting-strategy 'todo)
3980 (org-prepare-agenda "TODO")
3981 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3982 (let* ((today (org-today))
3983 (date (calendar-gregorian-from-absolute today))
3984 (kwds org-todo-keywords-for-agenda)
3985 (completion-ignore-case t)
3986 (org-select-this-todo-keyword
3987 (if (stringp arg) arg
3988 (and arg (integerp arg) (> arg 0)
3989 (nth (1- arg) kwds))))
3990 rtn rtnall files file pos)
3991 (when (equal arg '(4))
3992 (setq org-select-this-todo-keyword
3993 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3994 (mapcar 'list kwds) nil nil)))
3995 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3996 (org-set-local 'org-last-arg arg)
3997 (setq org-agenda-redo-command
3998 '(org-todo-list (or current-prefix-arg org-last-arg)))
3999 (setq files (org-agenda-files nil 'ifmode)
4000 rtnall nil)
4001 (while (setq file (pop files))
4002 (catch 'nextfile
4003 (org-check-agenda-file file)
4004 (setq rtn (org-agenda-get-day-entries file date :todo))
4005 (setq rtnall (append rtnall rtn))))
4006 (if org-agenda-overriding-header
4007 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4008 nil 'face 'org-agenda-structure) "\n")
4009 (insert "Global list of TODO items of type: ")
4010 (add-text-properties (point-min) (1- (point))
4011 (list 'face 'org-agenda-structure
4012 'short-heading
4013 (concat "ToDo: "
4014 (or org-select-this-todo-keyword "ALL"))))
4015 (org-agenda-mark-header-line (point-min))
4016 (setq pos (point))
4017 (insert (or org-select-this-todo-keyword "ALL") "\n")
4018 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4019 (setq pos (point))
4020 (unless org-agenda-multi
4021 (insert "Available with `N r': (0)ALL")
4022 (let ((n 0) s)
4023 (mapc (lambda (x)
4024 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4025 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4026 (insert "\n "))
4027 (insert " " s))
4028 kwds))
4029 (insert "\n"))
4030 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4031 (org-agenda-mark-header-line (point-min))
4032 (when rtnall
4033 (insert (org-finalize-agenda-entries rtnall) "\n"))
4034 (goto-char (point-min))
4035 (or org-agenda-multi (org-fit-agenda-window))
4036 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4037 (org-finalize-agenda)
4038 (setq buffer-read-only t)))
4040 ;;; Agenda tags match
4042 ;;;###autoload
4043 (defun org-tags-view (&optional todo-only match)
4044 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4045 The prefix arg TODO-ONLY limits the search to TODO entries."
4046 (interactive "P")
4047 (org-compile-prefix-format 'tags)
4048 (org-set-sorting-strategy 'tags)
4049 (let* ((org-tags-match-list-sublevels
4050 org-tags-match-list-sublevels)
4051 (completion-ignore-case t)
4052 rtn rtnall files file pos matcher
4053 buffer)
4054 (when (and (stringp match) (not (string-match "\\S-" match)))
4055 (setq match nil))
4056 (setq matcher (org-make-tags-matcher match)
4057 match (car matcher) matcher (cdr matcher))
4058 (org-prepare-agenda (concat "TAGS " match))
4059 (setq org-agenda-query-string match)
4060 (setq org-agenda-redo-command
4061 (list 'org-tags-view (list 'quote todo-only)
4062 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4063 (setq files (org-agenda-files nil 'ifmode)
4064 rtnall nil)
4065 (while (setq file (pop files))
4066 (catch 'nextfile
4067 (org-check-agenda-file file)
4068 (setq buffer (if (file-exists-p file)
4069 (org-get-agenda-file-buffer file)
4070 (error "No such file %s" file)))
4071 (if (not buffer)
4072 ;; If file does not exist, error message to agenda
4073 (setq rtn (list
4074 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4075 rtnall (append rtnall rtn))
4076 (with-current-buffer buffer
4077 (unless (org-mode-p)
4078 (error "Agenda file %s is not in `org-mode'" file))
4079 (save-excursion
4080 (save-restriction
4081 (if org-agenda-restrict
4082 (narrow-to-region org-agenda-restrict-begin
4083 org-agenda-restrict-end)
4084 (widen))
4085 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4086 (setq rtnall (append rtnall rtn))))))))
4087 (if org-agenda-overriding-header
4088 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4089 nil 'face 'org-agenda-structure) "\n")
4090 (insert "Headlines with TAGS match: ")
4091 (add-text-properties (point-min) (1- (point))
4092 (list 'face 'org-agenda-structure
4093 'short-heading
4094 (concat "Match: " match)))
4095 (setq pos (point))
4096 (insert match "\n")
4097 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4098 (setq pos (point))
4099 (unless org-agenda-multi
4100 (insert "Press `C-u r' to search again with new search string\n"))
4101 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4102 (org-agenda-mark-header-line (point-min))
4103 (when rtnall
4104 (insert (org-finalize-agenda-entries rtnall) "\n"))
4105 (goto-char (point-min))
4106 (or org-agenda-multi (org-fit-agenda-window))
4107 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4108 (org-finalize-agenda)
4109 (setq buffer-read-only t)))
4111 ;;; Agenda Finding stuck projects
4113 (defvar org-agenda-skip-regexp nil
4114 "Regular expression used in skipping subtrees for the agenda.
4115 This is basically a temporary global variable that can be set and then
4116 used by user-defined selections using `org-agenda-skip-function'.")
4118 (defvar org-agenda-overriding-header nil
4119 "When set during agenda, todo and tags searches it replaces the header.
4120 This variable should not be set directly, but custom commands can bind it
4121 in the options section.")
4123 (defun org-agenda-skip-entry-when-regexp-matches ()
4124 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4125 If yes, it returns the end position of this entry, causing agenda commands
4126 to skip the entry but continuing the search in the subtree. This is a
4127 function that can be put into `org-agenda-skip-function' for the duration
4128 of a command."
4129 (let ((end (save-excursion (org-end-of-subtree t)))
4130 skip)
4131 (save-excursion
4132 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4133 (and skip end)))
4135 (defun org-agenda-skip-subtree-when-regexp-matches ()
4136 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4137 If yes, it returns the end position of this tree, causing agenda commands
4138 to skip this subtree. This is a function that can be put into
4139 `org-agenda-skip-function' for the duration of a command."
4140 (let ((end (save-excursion (org-end-of-subtree t)))
4141 skip)
4142 (save-excursion
4143 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4144 (and skip end)))
4146 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4147 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4148 If yes, it returns the end position of the current entry (NOT the tree),
4149 causing agenda commands to skip the entry but continuing the search in
4150 the subtree. This is a function that can be put into
4151 `org-agenda-skip-function' for the duration of a command. An important
4152 use of this function is for the stuck project list."
4153 (let ((end (save-excursion (org-end-of-subtree t)))
4154 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4155 skip)
4156 (save-excursion
4157 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4158 (and skip entry-end)))
4160 (defun org-agenda-skip-entry-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 nil conditions))
4165 (defun org-agenda-skip-subtree-if (&rest conditions)
4166 "Skip entry if any of CONDITIONS is true.
4167 See `org-agenda-skip-if' for details."
4168 (org-agenda-skip-if t conditions))
4170 (defun org-agenda-skip-if (subtree conditions)
4171 "Checks current entity for CONDITIONS.
4172 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4173 the entry, i.e. the text before the next heading is checked.
4175 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4176 from different tests. Valid conditions are:
4178 scheduled Check if there is a scheduled cookie
4179 notscheduled Check if there is no scheduled cookie
4180 deadline Check if there is a deadline
4181 notdeadline Check if there is no deadline
4182 timestamp Check if there is a timestamp (also deadline or scheduled)
4183 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4184 regexp Check if regexp matches
4185 notregexp Check if regexp does not match.
4186 todo Check if TODO keyword matches
4187 nottodo Check if TODO keyword does not match
4189 The regexp is taken from the conditions list, it must come right after
4190 the `regexp' or `notregexp' element.
4192 `todo' and `nottodo' accept as an argument a list of todo
4193 keywords, which may include \"*\" to match any todo keyword.
4195 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4197 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4199 Instead of a list a keyword class may be given
4201 (org-agenda-skip-entry-if 'nottodo 'done)
4203 would skip entries that haven't been marked with any of \"DONE\"
4204 keywords. Possible classes are: `todo', `done', `any'.
4206 If any of these conditions is met, this function returns the end point of
4207 the entity, causing the search to continue from there. This is a function
4208 that can be put into `org-agenda-skip-function' for the duration of a command."
4209 (let (beg end m)
4210 (org-back-to-heading t)
4211 (setq beg (point)
4212 end (if subtree
4213 (progn (org-end-of-subtree t) (point))
4214 (progn (outline-next-heading) (1- (point)))))
4215 (goto-char beg)
4216 (and
4218 (and (memq 'scheduled conditions)
4219 (re-search-forward org-scheduled-time-regexp end t))
4220 (and (memq 'notscheduled conditions)
4221 (not (re-search-forward org-scheduled-time-regexp end t)))
4222 (and (memq 'deadline conditions)
4223 (re-search-forward org-deadline-time-regexp end t))
4224 (and (memq 'notdeadline conditions)
4225 (not (re-search-forward org-deadline-time-regexp end t)))
4226 (and (memq 'timestamp conditions)
4227 (re-search-forward org-ts-regexp end t))
4228 (and (memq 'nottimestamp conditions)
4229 (not (re-search-forward org-ts-regexp end t)))
4230 (and (setq m (memq 'regexp conditions))
4231 (stringp (nth 1 m))
4232 (re-search-forward (nth 1 m) end t))
4233 (and (setq m (memq 'notregexp conditions))
4234 (stringp (nth 1 m))
4235 (not (re-search-forward (nth 1 m) end t)))
4236 (and (or
4237 (setq m (memq 'todo conditions))
4238 (setq m (memq 'nottodo conditions)))
4239 (org-agenda-skip-if-todo m end)))
4240 end)))
4242 (defun org-agenda-skip-if-todo (args end)
4243 "Helper function for `org-agenda-skip-if', do not use it directly.
4244 ARGS is a list with first element either `todo' or `nottodo'.
4245 The remainder is either a list of TODO keywords, or a state symbol
4246 `todo' or `done' or `any'."
4247 (let ((kw (car args))
4248 (arg (cadr args))
4249 todo-wds todo-re)
4250 (setq todo-wds
4251 (org-uniquify
4252 (cond
4253 ((listp arg) ;; list of keywords
4254 (if (member "*" arg)
4255 (mapcar 'substring-no-properties org-todo-keywords-1)
4256 arg))
4257 ((symbolp arg) ;; keyword class name
4258 (cond
4259 ((eq arg 'todo)
4260 (org-delete-all org-done-keywords
4261 (mapcar 'substring-no-properties
4262 org-todo-keywords-1)))
4263 ((eq arg 'done) org-done-keywords)
4264 ((eq arg 'any)
4265 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4266 (setq todo-re
4267 (concat "^\\*+[ \t]+\\<\\("
4268 (mapconcat 'identity todo-wds "\\|")
4269 "\\)\\>"))
4270 (if (eq kw 'todo)
4271 (re-search-forward todo-re end t)
4272 (not (re-search-forward todo-re end t)))))
4274 ;;;###autoload
4275 (defun org-agenda-list-stuck-projects (&rest ignore)
4276 "Create agenda view for projects that are stuck.
4277 Stuck projects are project that have no next actions. For the definitions
4278 of what a project is and how to check if it stuck, customize the variable
4279 `org-stuck-projects'."
4280 (interactive)
4281 (let* ((org-agenda-skip-function
4282 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4283 ;; We could have used org-agenda-skip-if here.
4284 (org-agenda-overriding-header
4285 (or org-agenda-overriding-header "List of stuck projects: "))
4286 (matcher (nth 0 org-stuck-projects))
4287 (todo (nth 1 org-stuck-projects))
4288 (todo-wds (if (member "*" todo)
4289 (progn
4290 (org-prepare-agenda-buffers (org-agenda-files
4291 nil 'ifmode))
4292 (org-delete-all
4293 org-done-keywords-for-agenda
4294 (copy-sequence org-todo-keywords-for-agenda)))
4295 todo))
4296 (todo-re (concat "^\\*+[ \t]+\\("
4297 (mapconcat 'identity todo-wds "\\|")
4298 "\\)\\>"))
4299 (tags (nth 2 org-stuck-projects))
4300 (tags-re (if (member "*" tags)
4301 (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
4302 (if tags
4303 (concat "^\\*+ .*:\\("
4304 (mapconcat 'identity tags "\\|")
4305 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4306 (gen-re (nth 3 org-stuck-projects))
4307 (re-list
4308 (delq nil
4309 (list
4310 (if todo todo-re)
4311 (if tags tags-re)
4312 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4313 gen-re)))))
4314 (setq org-agenda-skip-regexp
4315 (if re-list
4316 (mapconcat 'identity re-list "\\|")
4317 (error "No information how to identify unstuck projects")))
4318 (org-tags-view nil matcher)
4319 (with-current-buffer org-agenda-buffer-name
4320 (setq org-agenda-redo-command
4321 '(org-agenda-list-stuck-projects
4322 (or current-prefix-arg org-last-arg))))))
4324 ;;; Diary integration
4326 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4327 (defvar list-diary-entries-hook)
4328 (defvar diary-time-regexp)
4329 (defun org-get-entries-from-diary (date)
4330 "Get the (Emacs Calendar) diary entries for DATE."
4331 (require 'diary-lib)
4332 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4333 (diary-display-hook '(fancy-diary-display))
4334 (diary-display-function 'fancy-diary-display)
4335 (pop-up-frames nil)
4336 (list-diary-entries-hook
4337 (cons 'org-diary-default-entry list-diary-entries-hook))
4338 (diary-file-name-prefix-function nil) ; turn this feature off
4339 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4340 entries
4341 (org-disable-agenda-to-diary t))
4342 (save-excursion
4343 (save-window-excursion
4344 (funcall (if (fboundp 'diary-list-entries)
4345 'diary-list-entries 'list-diary-entries)
4346 date 1)))
4347 (if (not (get-buffer diary-fancy-buffer))
4348 (setq entries nil)
4349 (with-current-buffer diary-fancy-buffer
4350 (setq buffer-read-only nil)
4351 (if (zerop (buffer-size))
4352 ;; No entries
4353 (setq entries nil)
4354 ;; Omit the date and other unnecessary stuff
4355 (org-agenda-cleanup-fancy-diary)
4356 ;; Add prefix to each line and extend the text properties
4357 (if (zerop (buffer-size))
4358 (setq entries nil)
4359 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4360 (setq entries
4361 (with-temp-buffer
4362 (insert entries) (goto-char (point-min))
4363 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4364 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4365 (replace-match (concat "; " (match-string 1)))))
4366 (buffer-string)))))
4367 (set-buffer-modified-p nil)
4368 (kill-buffer diary-fancy-buffer)))
4369 (when entries
4370 (setq entries (org-split-string entries "\n"))
4371 (setq entries
4372 (mapcar
4373 (lambda (x)
4374 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4375 ;; Extend the text properties to the beginning of the line
4376 (org-add-props x (text-properties-at (1- (length x)) x)
4377 'type "diary" 'date date 'face 'org-agenda-diary))
4378 entries)))))
4380 (defvar org-agenda-cleanup-fancy-diary-hook nil
4381 "Hook run when the fancy diary buffer is cleaned up.")
4383 (defun org-agenda-cleanup-fancy-diary ()
4384 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4385 This gets rid of the date, the underline under the date, and
4386 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4387 date. It also removes lines that contain only whitespace."
4388 (goto-char (point-min))
4389 (if (looking-at ".*?:[ \t]*")
4390 (progn
4391 (replace-match "")
4392 (re-search-forward "\n=+$" nil t)
4393 (replace-match "")
4394 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4395 (re-search-forward "\n=+$" nil t)
4396 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4397 (goto-char (point-min))
4398 (while (re-search-forward "^ +\n" nil t)
4399 (replace-match ""))
4400 (goto-char (point-min))
4401 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4402 (replace-match ""))
4403 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4405 ;; Make sure entries from the diary have the right text properties.
4406 (eval-after-load "diary-lib"
4407 '(if (boundp 'diary-modify-entry-list-string-function)
4408 ;; We can rely on the hook, nothing to do
4410 ;; Hook not available, must use advice to make this work
4411 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4412 "Make the position visible."
4413 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4414 (stringp string)
4415 buffer-file-name)
4416 (setq string (org-modify-diary-entry-string string))))))
4418 (defun org-modify-diary-entry-string (string)
4419 "Add text properties to string, allowing org-mode to act on it."
4420 (org-add-props string nil
4421 'mouse-face 'highlight
4422 'help-echo (if buffer-file-name
4423 (format "mouse-2 or RET jump to diary file %s"
4424 (abbreviate-file-name buffer-file-name))
4426 'org-agenda-diary-link t
4427 'org-marker (org-agenda-new-marker (point-at-bol))))
4429 (defun org-diary-default-entry ()
4430 "Add a dummy entry to the diary.
4431 Needed to avoid empty dates which mess up holiday display."
4432 ;; Catch the error if dealing with the new add-to-diary-alist
4433 (when org-disable-agenda-to-diary
4434 (condition-case nil
4435 (org-add-to-diary-list original-date "Org-mode dummy" "")
4436 (error
4437 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4439 (defun org-add-to-diary-list (&rest args)
4440 (if (fboundp 'diary-add-to-list)
4441 (apply 'diary-add-to-list args)
4442 (apply 'add-to-diary-list args)))
4444 (defvar org-diary-last-run-time nil)
4446 ;;;###autoload
4447 (defun org-diary (&rest args)
4448 "Return diary information from org-files.
4449 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4450 It accesses org files and extracts information from those files to be
4451 listed in the diary. The function accepts arguments specifying what
4452 items should be listed. For a list of arguments allowed here, see the
4453 variable `org-agenda-entry-types'.
4455 The call in the diary file should look like this:
4457 &%%(org-diary) ~/path/to/some/orgfile.org
4459 Use a separate line for each org file to check. Or, if you omit the file name,
4460 all files listed in `org-agenda-files' will be checked automatically:
4462 &%%(org-diary)
4464 If you don't give any arguments (as in the example above), the default
4465 arguments (:deadline :scheduled :timestamp :sexp) are used.
4466 So the example above may also be written as
4468 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4470 The function expects the lisp variables `entry' and `date' to be provided
4471 by the caller, because this is how the calendar works. Don't use this
4472 function from a program - use `org-agenda-get-day-entries' instead."
4473 (when (> (- (org-float-time)
4474 org-agenda-last-marker-time)
4476 (org-agenda-reset-markers))
4477 (org-compile-prefix-format 'agenda)
4478 (org-set-sorting-strategy 'agenda)
4479 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4480 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4481 (list entry)
4482 (org-agenda-files t)))
4483 (time (org-float-time))
4484 file rtn results)
4485 (when (or (not org-diary-last-run-time)
4486 (> (- time
4487 org-diary-last-run-time)
4489 (org-prepare-agenda-buffers files))
4490 (setq org-diary-last-run-time time)
4491 ;; If this is called during org-agenda, don't return any entries to
4492 ;; the calendar. Org Agenda will list these entries itself.
4493 (if org-disable-agenda-to-diary (setq files nil))
4494 (while (setq file (pop files))
4495 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4496 (setq results (append results rtn)))
4497 (if results
4498 (concat (org-finalize-agenda-entries results) "\n"))))
4500 ;;; Agenda entry finders
4502 (defun org-agenda-get-day-entries (file date &rest args)
4503 "Does the work for `org-diary' and `org-agenda'.
4504 FILE is the path to a file to be checked for entries. DATE is date like
4505 the one returned by `calendar-current-date'. ARGS are symbols indicating
4506 which kind of entries should be extracted. For details about these, see
4507 the documentation of `org-diary'."
4508 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4509 (let* ((org-startup-folded nil)
4510 (org-startup-align-all-tables nil)
4511 (buffer (if (file-exists-p file)
4512 (org-get-agenda-file-buffer file)
4513 (error "No such file %s" file)))
4514 arg results rtn deadline-results)
4515 (if (not buffer)
4516 ;; If file does not exist, make sure an error message ends up in diary
4517 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4518 (with-current-buffer buffer
4519 (unless (org-mode-p)
4520 (error "Agenda file %s is not in `org-mode'" file))
4521 (let ((case-fold-search nil))
4522 (save-excursion
4523 (save-restriction
4524 (if org-agenda-restrict
4525 (narrow-to-region org-agenda-restrict-begin
4526 org-agenda-restrict-end)
4527 (widen))
4528 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4529 (while (setq arg (pop args))
4530 (cond
4531 ((and (eq arg :todo)
4532 (equal date (calendar-gregorian-from-absolute
4533 (org-today))))
4534 (setq rtn (org-agenda-get-todos))
4535 (setq results (append results rtn)))
4536 ((eq arg :timestamp)
4537 (setq rtn (org-agenda-get-blocks))
4538 (setq results (append results rtn))
4539 (setq rtn (org-agenda-get-timestamps))
4540 (setq results (append results rtn)))
4541 ((eq arg :sexp)
4542 (setq rtn (org-agenda-get-sexps))
4543 (setq results (append results rtn)))
4544 ((eq arg :scheduled)
4545 (setq rtn (org-agenda-get-scheduled deadline-results))
4546 (setq results (append results rtn)))
4547 ((eq arg :closed)
4548 (setq rtn (org-agenda-get-progress))
4549 (setq results (append results rtn)))
4550 ((eq arg :deadline)
4551 (setq rtn (org-agenda-get-deadlines))
4552 (setq deadline-results (copy-sequence rtn))
4553 (setq results (append results rtn))))))))
4554 results))))
4556 (defun org-agenda-get-todos ()
4557 "Return the TODO information for agenda display."
4558 (let* ((props (list 'face nil
4559 'done-face 'org-agenda-done
4560 'org-not-done-regexp org-not-done-regexp
4561 'org-todo-regexp org-todo-regexp
4562 'org-complex-heading-regexp org-complex-heading-regexp
4563 'mouse-face 'highlight
4564 'help-echo
4565 (format "mouse-2 or RET jump to org file %s"
4566 (abbreviate-file-name buffer-file-name))))
4567 (regexp (concat "^\\*+[ \t]+\\("
4568 (if org-select-this-todo-keyword
4569 (if (equal org-select-this-todo-keyword "*")
4570 org-todo-regexp
4571 (concat "\\<\\("
4572 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4573 "\\)\\>"))
4574 org-not-done-regexp)
4575 "[^\n\r]*\\)"))
4576 marker priority category tags todo-state
4577 ee txt beg end)
4578 (goto-char (point-min))
4579 (while (re-search-forward regexp nil t)
4580 (catch :skip
4581 (save-match-data
4582 (beginning-of-line)
4583 (org-agenda-skip)
4584 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4585 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4586 (goto-char (1+ beg))
4587 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4588 (throw :skip nil)))
4589 (goto-char (match-beginning 1))
4590 (setq marker (org-agenda-new-marker (match-beginning 0))
4591 category (org-get-category)
4592 txt (match-string 1)
4593 tags (org-get-tags-at (point))
4594 txt (org-format-agenda-item "" txt category tags)
4595 priority (1+ (org-get-priority txt))
4596 todo-state (org-get-todo-state))
4597 (org-add-props txt props
4598 'org-marker marker 'org-hd-marker marker
4599 'priority priority 'org-category category
4600 'type "todo" 'todo-state todo-state)
4601 (push txt ee)
4602 (if org-agenda-todo-list-sublevels
4603 (goto-char (match-end 1))
4604 (org-end-of-subtree 'invisible))))
4605 (nreverse ee)))
4607 (defun org-agenda-todo-custom-ignore-p (time n)
4608 "Check whether timestamp is farther away then n number of days.
4609 This function is invoked if `org-agenda-todo-ignore-deadlines',
4610 `org-agenda-todo-ignore-scheduled' or
4611 `org-agenda-todo-ignore-timestamp' is set to an integer."
4612 (let ((days (org-days-to-time time)))
4613 (if (>= n 0)
4614 (>= days n)
4615 (<= days n))))
4617 ;;;###autoload
4618 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4619 (&optional end)
4620 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4621 (when (or org-agenda-todo-ignore-with-date
4622 org-agenda-todo-ignore-scheduled
4623 org-agenda-todo-ignore-deadlines
4624 org-agenda-todo-ignore-timestamp)
4625 (setq end (or end (save-excursion (outline-next-heading) (point))))
4626 (save-excursion
4627 (or (and org-agenda-todo-ignore-with-date
4628 (re-search-forward org-ts-regexp end t))
4629 (and org-agenda-todo-ignore-scheduled
4630 (re-search-forward org-scheduled-time-regexp end t)
4631 (cond
4632 ((eq org-agenda-todo-ignore-scheduled 'future)
4633 (> (org-days-to-time (match-string 1)) 0))
4634 ((eq org-agenda-todo-ignore-scheduled 'past)
4635 (<= (org-days-to-time (match-string 1)) 0))
4636 ((numberp org-agenda-todo-ignore-scheduled)
4637 (org-agenda-todo-custom-ignore-p
4638 (match-string 1) org-agenda-todo-ignore-scheduled))
4639 (t)))
4640 (and org-agenda-todo-ignore-deadlines
4641 (re-search-forward org-deadline-time-regexp end t)
4642 (cond
4643 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4644 ((eq org-agenda-todo-ignore-deadlines 'far)
4645 (not (org-deadline-close (match-string 1))))
4646 ((eq org-agenda-todo-ignore-deadlines 'future)
4647 (> (org-days-to-time (match-string 1)) 0))
4648 ((eq org-agenda-todo-ignore-deadlines 'past)
4649 (<= (org-days-to-time (match-string 1)) 0))
4650 ((numberp org-agenda-todo-ignore-deadlines)
4651 (org-agenda-todo-custom-ignore-p
4652 (match-string 1) org-agenda-todo-ignore-deadlines))
4653 (t (org-deadline-close (match-string 1)))))
4654 (and org-agenda-todo-ignore-timestamp
4655 (let ((buffer (current-buffer))
4656 (regexp
4657 (concat
4658 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4659 (start (point)))
4660 ;; Copy current buffer into a temporary one
4661 (with-temp-buffer
4662 (insert-buffer-substring buffer start end)
4663 (goto-char (point-min))
4664 ;; Delete SCHEDULED and DEADLINE items
4665 (while (re-search-forward regexp end t)
4666 (delete-region (match-beginning 0) (match-end 0)))
4667 (goto-char (point-min))
4668 ;; No search for timestamp left
4669 (when (re-search-forward org-ts-regexp nil t)
4670 (cond
4671 ((eq org-agenda-todo-ignore-timestamp 'future)
4672 (> (org-days-to-time (match-string 1)) 0))
4673 ((eq org-agenda-todo-ignore-timestamp 'past)
4674 (<= (org-days-to-time (match-string 1)) 0))
4675 ((numberp org-agenda-todo-ignore-timestamp)
4676 (org-agenda-todo-custom-ignore-p
4677 (match-string 1) org-agenda-todo-ignore-timestamp))
4678 (t))))))))))
4680 (defconst org-agenda-no-heading-message
4681 "No heading for this item in buffer or region.")
4683 (defun org-agenda-get-timestamps ()
4684 "Return the date stamp information for agenda display."
4685 (let* ((props (list 'face nil
4686 'org-not-done-regexp org-not-done-regexp
4687 'org-todo-regexp org-todo-regexp
4688 'org-complex-heading-regexp org-complex-heading-regexp
4689 'mouse-face 'highlight
4690 'help-echo
4691 (format "mouse-2 or RET jump to org file %s"
4692 (abbreviate-file-name buffer-file-name))))
4693 (d1 (calendar-absolute-from-gregorian date))
4694 (remove-re
4695 (concat
4696 (regexp-quote
4697 (format-time-string
4698 "<%Y-%m-%d"
4699 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4700 ".*?>"))
4701 (regexp
4702 (concat
4703 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4704 (regexp-quote
4705 (substring
4706 (format-time-string
4707 (car org-time-stamp-formats)
4708 (apply 'encode-time ; DATE bound by calendar
4709 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4710 1 11))
4711 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4712 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4713 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4714 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4715 todo-state end-of-match show-all)
4716 (goto-char (point-min))
4717 (while (setq end-of-match (re-search-forward regexp nil t))
4718 (setq b0 (match-beginning 0)
4719 b3 (match-beginning 3) e3 (match-end 3)
4720 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4721 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4722 (member todo-state
4723 org-agenda-repeating-timestamp-show-all)))
4724 (catch :skip
4725 (and (org-at-date-range-p) (throw :skip nil))
4726 (org-agenda-skip)
4727 (if (and (match-end 1)
4728 (not (= d1 (org-time-string-to-absolute
4729 (match-string 1) d1 nil show-all))))
4730 (throw :skip nil))
4731 (if (and e3
4732 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4733 (throw :skip nil))
4734 (setq tmp (buffer-substring (max (point-min)
4735 (- b0 org-ds-keyword-length))
4737 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4738 inactivep (= (char-after b0) ?\[)
4739 deadlinep (string-match org-deadline-regexp tmp)
4740 scheduledp (string-match org-scheduled-regexp tmp)
4741 closedp (and org-agenda-include-inactive-timestamps
4742 (string-match org-closed-string tmp))
4743 clockp (and org-agenda-include-inactive-timestamps
4744 (or (string-match org-clock-string tmp)
4745 (string-match "]-+\\'" tmp)))
4746 donep (member todo-state org-done-keywords))
4747 (if (or scheduledp deadlinep closedp clockp
4748 (and donep org-agenda-skip-timestamp-if-done))
4749 (throw :skip t))
4750 (if (string-match ">" timestr)
4751 ;; substring should only run to end of time stamp
4752 (setq timestr (substring timestr 0 (match-end 0))))
4753 (setq marker (org-agenda-new-marker b0)
4754 category (org-get-category b0))
4755 (save-excursion
4756 (if (not (re-search-backward "^\\*+ " nil t))
4757 (setq txt org-agenda-no-heading-message)
4758 (goto-char (match-beginning 0))
4759 (setq hdmarker (org-agenda-new-marker)
4760 tags (org-get-tags-at))
4761 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4762 (setq head (or (match-string 1) ""))
4763 (setq txt (org-format-agenda-item
4764 (if inactivep org-agenda-inactive-leader nil)
4765 head category tags timestr
4766 remove-re)))
4767 (setq priority (org-get-priority txt))
4768 (org-add-props txt props
4769 'org-marker marker 'org-hd-marker hdmarker)
4770 (org-add-props txt nil 'priority priority
4771 'org-category category 'date date
4772 'todo-state todo-state
4773 'type "timestamp")
4774 (push txt ee))
4775 (if org-agenda-skip-additional-timestamps-same-entry
4776 (outline-next-heading)
4777 (goto-char end-of-match))))
4778 (nreverse ee)))
4780 (defun org-agenda-get-sexps ()
4781 "Return the sexp information for agenda display."
4782 (require 'diary-lib)
4783 (let* ((props (list 'mouse-face 'highlight
4784 'help-echo
4785 (format "mouse-2 or RET jump to org file %s"
4786 (abbreviate-file-name buffer-file-name))))
4787 (regexp "^&?%%(")
4788 marker category ee txt tags entry result beg b sexp sexp-entry
4789 todo-state)
4790 (goto-char (point-min))
4791 (while (re-search-forward regexp nil t)
4792 (catch :skip
4793 (org-agenda-skip)
4794 (setq beg (match-beginning 0))
4795 (goto-char (1- (match-end 0)))
4796 (setq b (point))
4797 (forward-sexp 1)
4798 (setq sexp (buffer-substring b (point)))
4799 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4800 (org-trim (match-string 1))
4801 ""))
4802 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4803 (when result
4804 (setq marker (org-agenda-new-marker beg)
4805 category (org-get-category beg)
4806 todo-state (org-get-todo-state))
4808 (dolist (r (if (stringp result)
4809 (list result)
4810 result)) ;; we expect a list here
4811 (if (string-match "\\S-" r)
4812 (setq txt r)
4813 (setq txt "SEXP entry returned empty string"))
4815 (setq txt (org-format-agenda-item
4816 "" txt category tags 'time))
4817 (org-add-props txt props 'org-marker marker)
4818 (org-add-props txt nil
4819 'org-category category 'date date 'todo-state todo-state
4820 'type "sexp")
4821 (push txt ee)))))
4822 (nreverse ee)))
4824 ;; Calendar sanity: define some functions that are independent of
4825 ;; `calendar-date-style'.
4826 ;; Normally I would like to use ISO format when calling the diary functions,
4827 ;; but to make sure we still have Emacs 22 compatibility we bind
4828 ;; also `european-calendar-style' and use european format
4829 (defun org-anniversary (year month day &optional mark)
4830 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4831 (org-no-warnings
4832 (let ((calendar-date-style 'european) (european-calendar-style t))
4833 (diary-anniversary day month year mark))))
4834 (defun org-cyclic (N year month day &optional mark)
4835 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4836 (org-no-warnings
4837 (let ((calendar-date-style 'european) (european-calendar-style t))
4838 (diary-cyclic N day month year mark))))
4839 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4840 "Like `diary-block', but with fixed (ISO) order of arguments."
4841 (org-no-warnings
4842 (let ((calendar-date-style 'european) (european-calendar-style t))
4843 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4844 (defun org-date (year month day &optional mark)
4845 "Like `diary-date', but with fixed (ISO) order of arguments."
4846 (org-no-warnings
4847 (let ((calendar-date-style 'european) (european-calendar-style t))
4848 (diary-date day month year mark))))
4849 (defalias 'org-float 'diary-float)
4851 ;; Define the` org-class' function
4852 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4853 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4854 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4855 is any number of ISO weeks in the block period for which the item should
4856 be skipped."
4857 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4858 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4859 (d (calendar-absolute-from-gregorian date)))
4860 (and
4861 (<= date1 d)
4862 (<= d date2)
4863 (= (calendar-day-of-week date) dayname)
4864 (or (not skip-weeks)
4865 (progn
4866 (require 'cal-iso)
4867 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4868 entry)))
4870 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4871 "Like `org-class', but honor `calendar-date-style'.
4872 The order of the first 2 times 3 arguments depends on the variable
4873 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4874 So for American calendars, give this as MONTH DAY YEAR, for European as
4875 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4876 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4877 is any number of ISO weeks in the block period for which the item should
4878 be skipped.
4880 This function is here only for backward compatibility and it is deprecated,
4881 please use `org-class' instead."
4882 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4883 (date2 (org-order-calendar-date-args m2 d2 y2)))
4884 (org-class
4885 (nth 2 date1) (car date1) (nth 1 date1)
4886 (nth 2 date2) (car date2) (nth 1 date2)
4887 dayname skip-weeks)))
4889 (defalias 'org-get-closed 'org-agenda-get-progress)
4890 (defun org-agenda-get-progress ()
4891 "Return the logged TODO entries for agenda display."
4892 (let* ((props (list 'mouse-face 'highlight
4893 'org-not-done-regexp org-not-done-regexp
4894 'org-todo-regexp org-todo-regexp
4895 'org-complex-heading-regexp org-complex-heading-regexp
4896 'help-echo
4897 (format "mouse-2 or RET jump to org file %s"
4898 (abbreviate-file-name buffer-file-name))))
4899 (items (if (consp org-agenda-show-log)
4900 org-agenda-show-log
4901 (if (eq org-agenda-show-log 'clockcheck)
4902 '(clock)
4903 org-agenda-log-mode-items)))
4904 (parts
4905 (delq nil
4906 (list
4907 (if (memq 'closed items) (concat "\\<" org-closed-string))
4908 (if (memq 'clock items) (concat "\\<" org-clock-string))
4909 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4910 (parts-re (if parts (mapconcat 'identity parts "\\|")
4911 (error "`org-agenda-log-mode-items' is empty")))
4912 (regexp (concat
4913 "\\(" parts-re "\\)"
4914 " *\\["
4915 (regexp-quote
4916 (substring
4917 (format-time-string
4918 (car org-time-stamp-formats)
4919 (apply 'encode-time ; DATE bound by calendar
4920 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4921 1 11))))
4922 (org-agenda-search-headline-for-time nil)
4923 marker hdmarker priority category tags closedp statep clockp state
4924 ee txt extra timestr rest clocked)
4925 (goto-char (point-min))
4926 (while (re-search-forward regexp nil t)
4927 (catch :skip
4928 (org-agenda-skip)
4929 (setq marker (org-agenda-new-marker (match-beginning 0))
4930 closedp (equal (match-string 1) org-closed-string)
4931 statep (equal (string-to-char (match-string 1)) ?-)
4932 clockp (not (or closedp statep))
4933 state (and statep (match-string 2))
4934 category (org-get-category (match-beginning 0))
4935 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4937 (when (string-match "\\]" timestr)
4938 ;; substring should only run to end of time stamp
4939 (setq rest (substring timestr (match-end 0))
4940 timestr (substring timestr 0 (match-end 0)))
4941 (if (and (not closedp) (not statep)
4942 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4943 (progn (setq timestr (concat (substring timestr 0 -1)
4944 "-" (match-string 1 rest) "]"))
4945 (setq clocked (match-string 2 rest)))
4946 (setq clocked "-")))
4947 (save-excursion
4948 (setq extra
4949 (cond
4950 ((not org-agenda-log-mode-add-notes) nil)
4951 (statep
4952 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4953 (match-string 1)))
4954 (clockp
4955 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4956 (match-string 1)))))
4957 (if (not (re-search-backward "^\\*+ " nil t))
4958 (setq txt org-agenda-no-heading-message)
4959 (goto-char (match-beginning 0))
4960 (setq hdmarker (org-agenda-new-marker)
4961 tags (org-get-tags-at))
4962 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4963 (setq txt (match-string 1))
4964 (when extra
4965 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4966 (setq txt (concat (substring txt 0 (match-beginning 1))
4967 " - " extra " " (match-string 2 txt)))
4968 (setq txt (concat txt " - " extra))))
4969 (setq txt (org-format-agenda-item
4970 (cond
4971 (closedp "Closed: ")
4972 (statep (concat "State: (" state ")"))
4973 (t (concat "Clocked: (" clocked ")")))
4974 txt category tags timestr)))
4975 (setq priority 100000)
4976 (org-add-props txt props
4977 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4978 'priority priority 'org-category category
4979 'type "closed" 'date date
4980 'undone-face 'org-warning 'done-face 'org-agenda-done)
4981 (push txt ee))
4982 (goto-char (point-at-eol))))
4983 (nreverse ee)))
4985 (defun org-agenda-show-clocking-issues ()
4986 "Add overlays, showing issues with clocking.
4987 See also the user option `org-agenda-clock-consistency-checks'."
4988 (interactive)
4989 (let* ((pl org-agenda-clock-consistency-checks)
4990 (re (concat "^[ \t]*"
4991 org-clock-string
4992 "[ \t]+"
4993 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
4994 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
4995 (tlstart 0.)
4996 (tlend 0.)
4997 (maxtime (org-hh:mm-string-to-minutes
4998 (or (plist-get pl :max-duration) "24:00")))
4999 (mintime (org-hh:mm-string-to-minutes
5000 (or (plist-get pl :min-duration) 0)))
5001 (maxgap (org-hh:mm-string-to-minutes
5002 ;; default 30:00 means never complain
5003 (or (plist-get pl :max-gap) "30:00")))
5004 (gapok (mapcar 'org-hh:mm-string-to-minutes
5005 (plist-get pl :gap-ok-around)))
5006 (def-face (or (plist-get pl :default-face)
5007 '((:background "DarkRed") (:foreground "white"))))
5008 issue face m te ts dt ov)
5009 (goto-char (point-min))
5010 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5011 (setq issue nil face def-face)
5012 (catch 'next
5013 (setq m (org-get-at-bol 'org-marker)
5014 te nil ts nil)
5015 (unless (and m (markerp m))
5016 (setq issue "No valid clock line") (throw 'next t))
5017 (org-with-point-at m
5018 (save-excursion
5019 (goto-char (point-at-bol))
5020 (unless (looking-at re)
5021 (error "No valid Clock line")
5022 (throw 'next t))
5023 (unless (match-end 3)
5024 (setq issue "No end time"
5025 face (or (plist-get pl :no-end-time-face) face))
5026 (throw 'next t))
5027 (setq ts (match-string 1)
5028 te (match-string 3)
5029 ts (org-float-time
5030 (apply 'encode-time (org-parse-time-string ts)))
5031 te (org-float-time
5032 (apply 'encode-time (org-parse-time-string te)))
5033 dt (- te ts))))
5034 (cond
5035 ((> dt (* 60 maxtime))
5036 ;; a very long clocking chunk
5037 (setq issue (format "Clocking interval is very long: %s"
5038 (org-minutes-to-hh:mm-string
5039 (floor (/ (float dt) 60.))))
5040 face (or (plist-get pl :long-face) face)))
5041 ((< dt (* 60 mintime))
5042 ;; a very short clocking chunk
5043 (setq issue (format "Clocking interval is very short: %s"
5044 (org-minutes-to-hh:mm-string
5045 (floor (/ (float dt) 60.))))
5046 face (or (plist-get pl :short-face) face)))
5047 ((and (> tlend 0) (< ts tlend))
5048 ;; Two clock entries are overlapping
5049 (setq issue (format "Clocking overlap: %d minutes"
5050 (/ (- tlend ts) 60))
5051 face (or (plist-get pl :overlap-face) face)))
5052 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5053 ;; There is a gap, lets see if we need to report it
5054 (unless (org-agenda-check-clock-gap tlend ts gapok)
5055 (setq issue (format "Clocking gap: %d minutes"
5056 (/ (- ts tlend) 60))
5057 face (or (plist-get pl :gap-face) face))))
5058 (t nil)))
5059 (setq tlend (or te tlend) tlstart (or ts tlstart))
5060 (when issue
5061 ;; OK, there was some issue, add an overlay to show the issue
5062 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5063 (overlay-put ov 'before-string
5064 (concat
5065 (org-add-props
5066 (format "%-43s" (concat " " issue))
5068 'face face)
5069 "\n"))
5070 (overlay-put ov 'evaporate t)))))
5072 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5073 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5074 (catch 'exit
5075 (unless ok-list
5076 ;; there are no OK times for gaps...
5077 (throw 'exit nil))
5078 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5079 ;; This is more than 24 hours, so it is OK.
5080 ;; because we have at least one OK time, that must be in the
5081 ;; 24 hour interval.
5082 (throw 'exit t))
5083 ;; We have a shorter gap.
5084 ;; Now we have to get the minute of the day when these times are
5085 (let* ((t1dec (decode-time (seconds-to-time t1)))
5086 (t2dec (decode-time (seconds-to-time t2)))
5087 ;; compute the minute on the day
5088 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5089 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5090 (when (< min2 min1)
5091 ;; if min2 is smaller than min1, this means it is on the next day.
5092 ;; Wrap it to after midnight.
5093 (setq min2 (+ min2 1440)))
5094 ;; Now check if any of the OK times is in the gap
5095 (mapc (lambda (x)
5096 ;; Wrap the time to after midnight if necessary
5097 (if (< x min1) (setq x (+ x 1440)))
5098 ;; Check if in interval
5099 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5100 ok-list)
5101 ;; Nope, this gap is not OK
5102 nil)))
5104 (defun org-agenda-get-deadlines ()
5105 "Return the deadline information for agenda display."
5106 (let* ((props (list 'mouse-face 'highlight
5107 'org-not-done-regexp org-not-done-regexp
5108 'org-todo-regexp org-todo-regexp
5109 'org-complex-heading-regexp org-complex-heading-regexp
5110 'help-echo
5111 (format "mouse-2 or RET jump to org file %s"
5112 (abbreviate-file-name buffer-file-name))))
5113 (regexp org-deadline-time-regexp)
5114 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5115 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5116 d2 diff dfrac wdays pos pos1 category tags
5117 suppress-prewarning
5118 ee txt head face s todo-state show-all upcomingp donep timestr)
5119 (goto-char (point-min))
5120 (while (re-search-forward regexp nil t)
5121 (setq suppress-prewarning nil)
5122 (catch :skip
5123 (org-agenda-skip)
5124 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5125 (save-match-data
5126 (string-match org-scheduled-time-regexp
5127 (buffer-substring (point-at-bol)
5128 (point-at-eol)))))
5129 (setq suppress-prewarning
5130 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5131 org-agenda-skip-deadline-prewarning-if-scheduled
5132 0)))
5133 (setq s (match-string 1)
5134 txt nil
5135 pos (1- (match-beginning 1))
5136 todo-state (save-match-data (org-get-todo-state))
5137 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5138 (member todo-state
5139 org-agenda-repeating-timestamp-show-all))
5140 d2 (org-time-string-to-absolute
5141 (match-string 1) d1 'past show-all)
5142 diff (- d2 d1)
5143 wdays (if suppress-prewarning
5144 (let ((org-deadline-warning-days suppress-prewarning))
5145 (org-get-wdays s))
5146 (org-get-wdays s))
5147 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5148 upcomingp (and todayp (> diff 0)))
5149 ;; When to show a deadline in the calendar:
5150 ;; If the expiration is within wdays warning time.
5151 ;; Past-due deadlines are only shown on the current date
5152 (if (and (or (and (<= diff wdays)
5153 (and todayp (not org-agenda-only-exact-dates)))
5154 (= diff 0)))
5155 (save-excursion
5156 ;; (setq todo-state (org-get-todo-state))
5157 (setq donep (member todo-state org-done-keywords))
5158 (if (and donep
5159 (or org-agenda-skip-deadline-if-done
5160 (not (= diff 0))))
5161 (setq txt nil)
5162 (setq category (org-get-category))
5163 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5164 (setq txt org-agenda-no-heading-message)
5165 (goto-char (match-end 0))
5166 (setq pos1 (match-beginning 0))
5167 (setq tags (org-get-tags-at pos1))
5168 (setq head (buffer-substring-no-properties
5169 (point)
5170 (progn (skip-chars-forward "^\r\n")
5171 (point))))
5172 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5173 (setq timestr
5174 (concat (substring s (match-beginning 1)) " "))
5175 (setq timestr 'time))
5176 (setq txt (org-format-agenda-item
5177 (if (= diff 0)
5178 (car org-agenda-deadline-leaders)
5179 (if (functionp
5180 (nth 1 org-agenda-deadline-leaders))
5181 (funcall
5182 (nth 1 org-agenda-deadline-leaders)
5183 diff date)
5184 (format (nth 1 org-agenda-deadline-leaders)
5185 diff)))
5186 head category tags
5187 (if (not (= diff 0)) nil timestr)))))
5188 (when txt
5189 (setq face (org-agenda-deadline-face dfrac wdays))
5190 (org-add-props txt props
5191 'org-marker (org-agenda-new-marker pos)
5192 'org-hd-marker (org-agenda-new-marker pos1)
5193 'priority (+ (- diff)
5194 (org-get-priority txt))
5195 'org-category category
5196 'todo-state todo-state
5197 'type (if upcomingp "upcoming-deadline" "deadline")
5198 'date (if upcomingp date d2)
5199 'face (if donep 'org-agenda-done face)
5200 'undone-face face 'done-face 'org-agenda-done)
5201 (push txt ee))))))
5202 (nreverse ee)))
5204 (defun org-agenda-deadline-face (fraction &optional wdays)
5205 "Return the face to displaying a deadline item.
5206 FRACTION is what fraction of the head-warning time has passed."
5207 (if (equal wdays 0) (setq fraction 1.))
5208 (let ((faces org-agenda-deadline-faces) f)
5209 (catch 'exit
5210 (while (setq f (pop faces))
5211 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5213 (defun org-agenda-get-scheduled (&optional deadline-results)
5214 "Return the scheduled information for agenda display."
5215 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5216 'org-todo-regexp org-todo-regexp
5217 'org-complex-heading-regexp org-complex-heading-regexp
5218 'done-face 'org-agenda-done
5219 'mouse-face 'highlight
5220 'help-echo
5221 (format "mouse-2 or RET jump to org file %s"
5222 (abbreviate-file-name buffer-file-name))))
5223 (regexp org-scheduled-time-regexp)
5224 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5225 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5227 (deadline-position-alist
5228 (mapcar (lambda (a) (and (setq mm (get-text-property
5229 0 'org-hd-marker a))
5230 (cons (marker-position mm) a)))
5231 deadline-results))
5232 d2 diff pos pos1 category tags donep
5233 ee txt head pastschedp todo-state face timestr s habitp show-all)
5234 (goto-char (point-min))
5235 (while (re-search-forward regexp nil t)
5236 (catch :skip
5237 (org-agenda-skip)
5238 (setq s (match-string 1)
5239 txt nil
5240 pos (1- (match-beginning 1))
5241 todo-state (save-match-data (org-get-todo-state))
5242 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5243 (member todo-state
5244 org-agenda-repeating-timestamp-show-all))
5245 d2 (org-time-string-to-absolute
5246 (match-string 1) d1 'past show-all)
5247 diff (- d2 d1))
5248 (setq pastschedp (and todayp (< diff 0)))
5249 ;; When to show a scheduled item in the calendar:
5250 ;; If it is on or past the date.
5251 (when (or (and (< diff 0)
5252 (< (abs diff) org-scheduled-past-days)
5253 (and todayp (not org-agenda-only-exact-dates)))
5254 (= diff 0))
5255 (save-excursion
5256 (setq donep (member todo-state org-done-keywords))
5257 (if (and donep
5258 (or org-agenda-skip-scheduled-if-done
5259 (not (= diff 0))
5260 (and (functionp 'org-is-habit-p)
5261 (org-is-habit-p))))
5262 (setq txt nil)
5263 (setq habitp (and (functionp 'org-is-habit-p)
5264 (org-is-habit-p)))
5265 (setq category (org-get-category))
5266 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5267 (setq txt org-agenda-no-heading-message)
5268 (goto-char (match-end 0))
5269 (setq pos1 (match-beginning 0))
5270 (if habitp
5271 (if (or (not org-habit-show-habits)
5272 (and (not todayp)
5273 org-habit-show-habits-only-for-today))
5274 (throw :skip nil))
5275 (if (and
5276 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5277 (and org-agenda-skip-scheduled-if-deadline-is-shown
5278 pastschedp))
5279 (setq mm (assoc pos1 deadline-position-alist)))
5280 (throw :skip nil)))
5281 (setq tags (org-get-tags-at))
5282 (setq head (buffer-substring-no-properties
5283 (point)
5284 (progn (skip-chars-forward "^\r\n") (point))))
5285 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5286 (setq timestr
5287 (concat (substring s (match-beginning 1)) " "))
5288 (setq timestr 'time))
5289 (setq txt (org-format-agenda-item
5290 (if (= diff 0)
5291 (car org-agenda-scheduled-leaders)
5292 (format (nth 1 org-agenda-scheduled-leaders)
5293 (- 1 diff)))
5294 head category tags
5295 (if (not (= diff 0)) nil timestr)
5296 nil habitp))))
5297 (when txt
5298 (setq face
5299 (cond
5300 ((and (not habitp) pastschedp)
5301 'org-scheduled-previously)
5302 (todayp 'org-scheduled-today)
5303 (t 'org-scheduled))
5304 habitp (and habitp (org-habit-parse-todo)))
5305 (org-add-props txt props
5306 'undone-face face
5307 'face (if donep 'org-agenda-done face)
5308 'org-marker (org-agenda-new-marker pos)
5309 'org-hd-marker (org-agenda-new-marker pos1)
5310 'type (if pastschedp "past-scheduled" "scheduled")
5311 'date (if pastschedp d2 date)
5312 'priority (if habitp
5313 (org-habit-get-priority habitp)
5314 (+ 94 (- 5 diff) (org-get-priority txt)))
5315 'org-category category
5316 'org-habit-p habitp
5317 'todo-state todo-state)
5318 (push txt ee))))))
5319 (nreverse ee)))
5321 (defun org-agenda-get-blocks ()
5322 "Return the date-range information for agenda display."
5323 (let* ((props (list 'face nil
5324 'org-not-done-regexp org-not-done-regexp
5325 'org-todo-regexp org-todo-regexp
5326 'org-complex-heading-regexp org-complex-heading-regexp
5327 'mouse-face 'highlight
5328 'help-echo
5329 (format "mouse-2 or RET jump to org file %s"
5330 (abbreviate-file-name buffer-file-name))))
5331 (regexp org-tr-regexp)
5332 (d0 (calendar-absolute-from-gregorian date))
5333 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5334 head donep)
5335 (goto-char (point-min))
5336 (while (re-search-forward regexp nil t)
5337 (catch :skip
5338 (org-agenda-skip)
5339 (setq pos (point))
5340 (let ((start-time (match-string 1))
5341 (end-time (match-string 2)))
5342 (setq s1 (match-string 1)
5343 s2 (match-string 2)
5344 d1 (time-to-days (org-time-string-to-time s1))
5345 d2 (time-to-days (org-time-string-to-time s2)))
5346 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5347 ;; Only allow days between the limits, because the normal
5348 ;; date stamps will catch the limits.
5349 (save-excursion
5350 (setq todo-state (org-get-todo-state))
5351 (setq donep (member todo-state org-done-keywords))
5352 (if (and donep org-agenda-skip-timestamp-if-done)
5353 (throw :skip t))
5354 (setq marker (org-agenda-new-marker (point)))
5355 (setq category (org-get-category))
5356 (if (not (re-search-backward "^\\*+ " nil t))
5357 (setq txt org-agenda-no-heading-message)
5358 (goto-char (match-beginning 0))
5359 (setq hdmarker (org-agenda-new-marker (point)))
5360 (setq tags (org-get-tags-at))
5361 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5362 (setq head (match-string 1))
5363 (let ((remove-re
5364 (if org-agenda-remove-timeranges-from-blocks
5365 (concat
5366 "<" (regexp-quote s1) ".*?>"
5367 "--"
5368 "<" (regexp-quote s2) ".*?>")
5369 nil)))
5370 (setq txt (org-format-agenda-item
5371 (format
5372 (nth (if (= d1 d2) 0 1)
5373 org-agenda-timerange-leaders)
5374 (1+ (- d0 d1)) (1+ (- d2 d1)))
5375 head category tags
5376 (cond ((= d1 d0)
5377 (concat "<" start-time ">"))
5378 ((= d2 d0)
5379 (concat "<" end-time ">"))
5380 (t nil))
5381 remove-re))))
5382 (org-add-props txt props
5383 'org-marker marker 'org-hd-marker hdmarker
5384 'type "block" 'date date
5385 'todo-state todo-state
5386 'priority (org-get-priority txt) 'org-category category)
5387 (push txt ee))))
5388 (goto-char pos)))
5389 ;; Sort the entries by expiration date.
5390 (nreverse ee)))
5392 ;;; Agenda presentation and sorting
5394 (defvar org-prefix-has-time nil
5395 "A flag, set by `org-compile-prefix-format'.
5396 The flag is set if the currently compiled format contains a `%t'.")
5397 (defvar org-prefix-has-tag nil
5398 "A flag, set by `org-compile-prefix-format'.
5399 The flag is set if the currently compiled format contains a `%T'.")
5400 (defvar org-prefix-has-effort nil
5401 "A flag, set by `org-compile-prefix-format'.
5402 The flag is set if the currently compiled format contains a `%e'.")
5403 (defvar org-prefix-category-length nil
5404 "Used by `org-compile-prefix-format' to remember the category field width.")
5405 (defvar org-prefix-category-max-length nil
5406 "Used by `org-compile-prefix-format' to remember the category field width.")
5408 (defun org-agenda-get-category-icon (category)
5409 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5410 (dolist (entry org-agenda-category-icon-alist)
5411 (when (org-string-match-p (car entry) category)
5412 (if (listp (cadr entry))
5413 (return (cadr entry))
5414 (return (apply 'create-image (cdr entry)))))))
5416 (defun org-format-agenda-item (extra txt &optional category tags dotime
5417 remove-re habitp)
5418 "Format TXT to be inserted into the agenda buffer.
5419 In particular, it adds the prefix and corresponding text properties. EXTRA
5420 must be a string and replaces the `%s' specifier in the prefix format.
5421 CATEGORY (string, symbol or nil) may be used to overrule the default
5422 category taken from local variable or file name. It will replace the `%c'
5423 specifier in the format. DOTIME, when non-nil, indicates that a
5424 time-of-day should be extracted from TXT for sorting of this entry, and for
5425 the `%t' specifier in the format. When DOTIME is a string, this string is
5426 searched for a time before TXT is. TAGS can be the tags of the headline.
5427 Any match of REMOVE-RE will be removed from TXT."
5428 (save-match-data
5429 ;; Diary entries sometimes have extra whitespace at the beginning
5430 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5432 ;; Fix the tags part in txt
5433 (setq txt (org-agenda-fix-displayed-tags
5434 txt tags
5435 org-agenda-show-inherited-tags
5436 org-agenda-hide-tags-regexp))
5437 (let* ((category (or category
5438 (if (stringp org-category)
5439 org-category
5440 (and org-category (symbol-name org-category)))
5441 (if buffer-file-name
5442 (file-name-sans-extension
5443 (file-name-nondirectory buffer-file-name))
5444 "")))
5445 (category-icon (org-agenda-get-category-icon category))
5446 (category-icon (if category-icon
5447 (propertize " " 'display category-icon)
5448 ""))
5449 ;; time, tag, effort are needed for the eval of the prefix format
5450 (tag (if tags (nth (1- (length tags)) tags) ""))
5451 time effort neffort
5452 (ts (if dotime (concat
5453 (if (stringp dotime) dotime "")
5454 (and org-agenda-search-headline-for-time txt))))
5455 (time-of-day (and dotime (org-get-time-of-day ts)))
5456 stamp plain s0 s1 s2 rtn srp l
5457 duration thecategory)
5458 (and (org-mode-p) buffer-file-name
5459 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5460 (when (and dotime time-of-day)
5461 ;; Extract starting and ending time and move them to prefix
5462 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5463 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5464 (setq s0 (match-string 0 ts)
5465 srp (and stamp (match-end 3))
5466 s1 (match-string (if plain 1 2) ts)
5467 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5469 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5470 ;; them, we might want to remove them there to avoid duplication.
5471 ;; The user can turn this off with a variable.
5472 (if (and org-prefix-has-time
5473 org-agenda-remove-times-when-in-prefix (or stamp plain)
5474 (string-match (concat (regexp-quote s0) " *") txt)
5475 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5476 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5477 (= (match-beginning 0) 0)
5479 (setq txt (replace-match "" nil nil txt))))
5480 ;; Normalize the time(s) to 24 hour
5481 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5482 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5484 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5485 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5486 (setq s2
5487 (org-minutes-to-hh:mm-string
5488 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5490 ;; Compute the duration
5491 (when s2
5492 (setq duration (- (org-hh:mm-string-to-minutes s2)
5493 (org-hh:mm-string-to-minutes s1)))))
5495 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5496 txt)
5497 ;; Tags are in the string
5498 (if (or (eq org-agenda-remove-tags t)
5499 (and org-agenda-remove-tags
5500 org-prefix-has-tag))
5501 (setq txt (replace-match "" t t txt))
5502 (setq txt (replace-match
5503 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5504 (match-string 2 txt))
5505 t t txt))))
5506 (when (org-mode-p)
5507 (setq effort
5508 (condition-case nil
5509 (org-get-effort
5510 (or (get-text-property 0 'org-hd-marker txt)
5511 (get-text-property 0 'org-marker txt)))
5512 (error nil)))
5513 (when effort
5514 (setq neffort (org-duration-string-to-minutes effort)
5515 effort (setq effort (concat "[" effort "]" )))))
5517 (when remove-re
5518 (while (string-match remove-re txt)
5519 (setq txt (replace-match "" t t txt))))
5521 ;; Set org-heading property on `txt' to mark the start of the
5522 ;; heading.
5523 (add-text-properties 0 (length txt) '(org-heading t) txt)
5525 ;; Prepare the variables needed in the eval of the compiled format
5526 (setq time (cond (s2 (concat
5527 (org-agenda-time-of-day-to-ampm-maybe s1)
5528 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5529 (if org-agenda-timegrid-use-ampm " ")))
5530 (s1 (concat
5531 (org-agenda-time-of-day-to-ampm-maybe s1)
5532 (if org-agenda-timegrid-use-ampm
5533 "........ "
5534 "......")))
5535 (t ""))
5536 extra (or (and (not habitp) extra) "")
5537 category (if (symbolp category) (symbol-name category) category)
5538 thecategory (copy-sequence category))
5539 (if (string-match org-bracket-link-regexp category)
5540 (progn
5541 (setq l (if (match-end 3)
5542 (- (match-end 3) (match-beginning 3))
5543 (- (match-end 1) (match-beginning 1))))
5544 (when (< l (or org-prefix-category-length 0))
5545 (setq category (copy-sequence category))
5546 (org-add-props category nil
5547 'extra-space (make-string
5548 (- org-prefix-category-length l 1) ?\ ))))
5549 (if (and org-prefix-category-max-length
5550 (>= (length category) org-prefix-category-max-length))
5551 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5552 ;; Evaluate the compiled format
5553 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5555 ;; And finally add the text properties
5556 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5557 (org-add-props rtn nil
5558 'org-category (if thecategory (downcase thecategory) category)
5559 'tags (mapcar 'org-downcase-keep-props tags)
5560 'org-highest-priority org-highest-priority
5561 'org-lowest-priority org-lowest-priority
5562 'time-of-day time-of-day
5563 'duration duration
5564 'effort effort
5565 'effort-minutes neffort
5566 'txt txt
5567 'time time
5568 'extra extra
5569 'dotime dotime))))
5571 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5572 "Remove tags string from TXT, and add a modified list of tags.
5573 The modified list may contain inherited tags, and tags matched by
5574 `org-agenda-hide-tags-regexp' will be removed."
5575 (when (or add-inherited hide-re)
5576 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5577 (setq txt (substring txt 0 (match-beginning 0))))
5578 (setq tags
5579 (delq nil
5580 (mapcar (lambda (tg)
5581 (if (or (and hide-re (string-match hide-re tg))
5582 (and (not add-inherited)
5583 (get-text-property 0 'inherited tg)))
5585 tg))
5586 tags)))
5587 (when tags
5588 (let ((have-i (get-text-property 0 'inherited (car tags)))
5590 (setq txt (concat txt " :"
5591 (mapconcat
5592 (lambda (x)
5593 (setq i (get-text-property 0 'inherited x))
5594 (if (and have-i (not i))
5595 (progn
5596 (setq have-i nil)
5597 (concat ":" x))
5599 tags ":")
5600 (if have-i "::" ":"))))))
5601 txt)
5603 (defun org-downcase-keep-props (s)
5604 (let ((props (text-properties-at 0 s)))
5605 (setq s (downcase s))
5606 (add-text-properties 0 (length s) props s)
5609 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5610 (defvar org-agenda-sorting-strategy-selected nil)
5612 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5613 (catch 'exit
5614 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5615 ((and todayp (member 'today (car org-agenda-time-grid))))
5616 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5617 ((member 'weekly (car org-agenda-time-grid)))
5618 (t (throw 'exit list)))
5619 (let* ((have (delq nil (mapcar
5620 (lambda (x) (get-text-property 1 'time-of-day x))
5621 list)))
5622 (string (nth 1 org-agenda-time-grid))
5623 (gridtimes (nth 2 org-agenda-time-grid))
5624 (req (car org-agenda-time-grid))
5625 (remove (member 'remove-match req))
5626 new time)
5627 (if (and (member 'require-timed req) (not have))
5628 ;; don't show empty grid
5629 (throw 'exit list))
5630 (while (setq time (pop gridtimes))
5631 (unless (and remove (member time have))
5632 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5633 (push (org-format-agenda-item
5634 nil string "" nil
5635 (concat (substring time 0 -2) ":" (substring time -2)))
5636 new)
5637 (put-text-property
5638 2 (length (car new)) 'face 'org-time-grid (car new))))
5639 (when (and todayp org-agenda-show-current-time-in-grid)
5640 (push (org-format-agenda-item
5642 org-agenda-current-time-string
5643 "" nil
5644 (format-time-string "%H:%M "))
5645 new)
5646 (put-text-property
5647 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5649 (if (member 'time-up org-agenda-sorting-strategy-selected)
5650 (append new list)
5651 (append list new)))))
5653 (defun org-compile-prefix-format (key)
5654 "Compile the prefix format into a Lisp form that can be evaluated.
5655 The resulting form is returned and stored in the variable
5656 `org-prefix-format-compiled'."
5657 (setq org-prefix-has-time nil org-prefix-has-tag nil
5658 org-prefix-category-length nil org-prefix-has-effort nil)
5659 (let ((s (cond
5660 ((stringp org-agenda-prefix-format)
5661 org-agenda-prefix-format)
5662 ((assq key org-agenda-prefix-format)
5663 (cdr (assq key org-agenda-prefix-format)))
5664 (t " %-12:c%?-12t% s")))
5665 (start 0)
5666 varform vars var e c f opt)
5667 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5668 s start)
5669 (setq var (or (cdr (assoc (match-string 4 s)
5670 '(("c" . category) ("t" . time) ("s" . extra)
5671 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5672 'eval)
5673 c (or (match-string 3 s) "")
5674 opt (match-beginning 1)
5675 start (1+ (match-beginning 0)))
5676 (if (equal var 'time) (setq org-prefix-has-time t))
5677 (if (equal var 'tag) (setq org-prefix-has-tag t))
5678 (if (equal var 'effort) (setq org-prefix-has-effort t))
5679 (setq f (concat "%" (match-string 2 s) "s"))
5680 (when (equal var 'category)
5681 (setq org-prefix-category-length
5682 (floor (abs (string-to-number (match-string 2 s)))))
5683 (setq org-prefix-category-max-length
5684 (let ((x (match-string 2 s)))
5685 (save-match-data
5686 (if (string-match "\\.[0-9]+" x)
5687 (string-to-number (substring (match-string 0 x) 1)))))))
5688 (if (eq var 'eval)
5689 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5690 (if opt
5691 (setq varform
5692 `(if (equal "" ,var)
5694 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5695 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5696 (setq s (replace-match "%s" t nil s))
5697 (push varform vars))
5698 (setq vars (nreverse vars))
5699 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5701 (defun org-set-sorting-strategy (key)
5702 (if (symbolp (car org-agenda-sorting-strategy))
5703 ;; the old format
5704 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5705 (setq org-agenda-sorting-strategy-selected
5706 (or (cdr (assq key org-agenda-sorting-strategy))
5707 (cdr (assq 'agenda org-agenda-sorting-strategy))
5708 '(time-up category-keep priority-down)))))
5710 (defun org-get-time-of-day (s &optional string mod24)
5711 "Check string S for a time of day.
5712 If found, return it as a military time number between 0 and 2400.
5713 If not found, return nil.
5714 The optional STRING argument forces conversion into a 5 character wide string
5715 HH:MM."
5716 (save-match-data
5717 (when
5718 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5719 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5720 (let* ((h (string-to-number (match-string 1 s)))
5721 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5722 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5723 (am-p (equal ampm "am"))
5724 (h1 (cond ((not ampm) h)
5725 ((= h 12) (if am-p 0 12))
5726 (t (+ h (if am-p 0 12)))))
5727 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5728 (mod h1 24) h1))
5729 (t0 (+ (* 100 h2) m))
5730 (t1 (concat (if (>= h1 24) "+" " ")
5731 (if (and org-agenda-time-leading-zero
5732 (< t0 1000)) "0" "")
5733 (if (< t0 100) "0" "")
5734 (if (< t0 10) "0" "")
5735 (int-to-string t0))))
5736 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5738 (defvar org-agenda-before-sorting-filter-function nil
5739 "Function to be applied to agenda items prior to sorting.
5740 Prior to sorting also means just before they are inserted into the agenda.
5742 To aid sorting, you may revisit the original entries and add more text
5743 properties which will later be used by the sorting functions.
5745 The function should take a string argument, an agenda line.
5746 It has access to the text properties in that line, which contain among
5747 other things, the property `org-hd-marker' that points to the entry
5748 where the line comes from. Note that not all lines going into the agenda
5749 have this property, only most.
5751 The function should return the modified string. It is probably best
5752 to ONLY change text properties.
5754 You can also use this function as a filter, by returning nil for lines
5755 you don't want to have in the agenda at all. For this application, you
5756 could bind the variable in the options section of a custom command.")
5758 (defun org-finalize-agenda-entries (list &optional nosort)
5759 "Sort and concatenate the agenda items."
5760 (setq list (mapcar 'org-agenda-highlight-todo list))
5761 (if nosort
5762 list
5763 (when org-agenda-before-sorting-filter-function
5764 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5765 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5767 (defun org-agenda-highlight-todo (x)
5768 (let ((org-done-keywords org-done-keywords-for-agenda)
5769 (case-fold-search nil)
5771 (if (eq x 'line)
5772 (save-excursion
5773 (beginning-of-line 1)
5774 (setq re (org-get-at-bol 'org-todo-regexp))
5775 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5776 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5777 (add-text-properties (match-beginning 0) (match-end 1)
5778 (list 'face (org-get-todo-face 1)))
5779 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5780 (delete-region (match-beginning 1) (1- (match-end 0)))
5781 (goto-char (match-beginning 1))
5782 (insert (format org-agenda-todo-keyword-format s)))))
5783 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5784 (setq re (get-text-property 0 'org-todo-regexp x))
5785 (when (and re
5786 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5787 x (or pl 0)) pl))
5788 (add-text-properties
5789 (or (match-end 1) (match-end 0)) (match-end 0)
5790 (list 'face (org-get-todo-face (match-string 2 x)))
5792 (when (match-end 1)
5793 (setq x (concat (substring x 0 (match-end 1))
5794 (format org-agenda-todo-keyword-format
5795 (match-string 2 x))
5796 (org-add-props " " (text-properties-at 0 x))
5797 (substring x (match-end 3)))))))
5798 x)))
5800 (defsubst org-cmp-priority (a b)
5801 "Compare the priorities of string A and B."
5802 (let ((pa (or (get-text-property 1 'priority a) 0))
5803 (pb (or (get-text-property 1 'priority b) 0)))
5804 (cond ((> pa pb) +1)
5805 ((< pa pb) -1)
5806 (t nil))))
5808 (defsubst org-cmp-effort (a b)
5809 "Compare the priorities of string A and B."
5810 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5811 (ea (or (get-text-property 1 'effort-minutes a) def))
5812 (eb (or (get-text-property 1 'effort-minutes b) def)))
5813 (cond ((> ea eb) +1)
5814 ((< ea eb) -1)
5815 (t nil))))
5817 (defsubst org-cmp-category (a b)
5818 "Compare the string values of categories of strings A and B."
5819 (let ((ca (or (get-text-property 1 'org-category a) ""))
5820 (cb (or (get-text-property 1 'org-category b) "")))
5821 (cond ((string-lessp ca cb) -1)
5822 ((string-lessp cb ca) +1)
5823 (t nil))))
5825 (defsubst org-cmp-todo-state (a b)
5826 "Compare the todo states of strings A and B."
5827 (let* ((ma (or (get-text-property 1 'org-marker a)
5828 (get-text-property 1 'org-hd-marker a)))
5829 (mb (or (get-text-property 1 'org-marker b)
5830 (get-text-property 1 'org-hd-marker b)))
5831 (fa (and ma (marker-buffer ma)))
5832 (fb (and mb (marker-buffer mb)))
5833 (todo-kwds
5834 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5835 (and fb (with-current-buffer fb org-todo-keywords-1))))
5836 (ta (or (get-text-property 1 'todo-state a) ""))
5837 (tb (or (get-text-property 1 'todo-state b) ""))
5838 (la (- (length (member ta todo-kwds))))
5839 (lb (- (length (member tb todo-kwds))))
5840 (donepa (member ta org-done-keywords-for-agenda))
5841 (donepb (member tb org-done-keywords-for-agenda)))
5842 (cond ((and donepa (not donepb)) -1)
5843 ((and (not donepa) donepb) +1)
5844 ((< la lb) -1)
5845 ((< lb la) +1)
5846 (t nil))))
5848 (defsubst org-cmp-alpha (a b)
5849 "Compare the headlines, alphabetically."
5850 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5851 (plb (text-property-any 0 (length b) 'org-heading t b))
5852 (ta (and pla (substring a pla)))
5853 (tb (and plb (substring b plb))))
5854 (when pla
5855 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5856 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5857 (setq ta (substring ta (match-end 0))))
5858 (setq ta (downcase ta)))
5859 (when plb
5860 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5861 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5862 (setq tb (substring tb (match-end 0))))
5863 (setq tb (downcase tb)))
5864 (cond ((not ta) +1)
5865 ((not tb) -1)
5866 ((string-lessp ta tb) -1)
5867 ((string-lessp tb ta) +1)
5868 (t nil))))
5870 (defsubst org-cmp-tag (a b)
5871 "Compare the string values of the first tags of A and B."
5872 (let ((ta (car (last (get-text-property 1 'tags a))))
5873 (tb (car (last (get-text-property 1 'tags b)))))
5874 (cond ((not ta) +1)
5875 ((not tb) -1)
5876 ((string-lessp ta tb) -1)
5877 ((string-lessp tb ta) +1)
5878 (t nil))))
5880 (defsubst org-cmp-time (a b)
5881 "Compare the time-of-day values of strings A and B."
5882 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5883 (ta (or (get-text-property 1 'time-of-day a) def))
5884 (tb (or (get-text-property 1 'time-of-day b) def)))
5885 (cond ((< ta tb) -1)
5886 ((< tb ta) +1)
5887 (t nil))))
5889 (defsubst org-cmp-habit-p (a b)
5890 "Compare the todo states of strings A and B."
5891 (let ((ha (get-text-property 1 'org-habit-p a))
5892 (hb (get-text-property 1 'org-habit-p b)))
5893 (cond ((and ha (not hb)) -1)
5894 ((and (not ha) hb) +1)
5895 (t nil))))
5897 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5899 (defun org-entries-lessp (a b)
5900 "Predicate for sorting agenda entries."
5901 ;; The following variables will be used when the form is evaluated.
5902 ;; So even though the compiler complains, keep them.
5903 (let* ((ss org-agenda-sorting-strategy-selected)
5904 (time-up (and (org-em 'time-up 'time-down ss)
5905 (org-cmp-time a b)))
5906 (time-down (if time-up (- time-up) nil))
5907 (priority-up (and (org-em 'priority-up 'priority-down ss)
5908 (org-cmp-priority a b)))
5909 (priority-down (if priority-up (- priority-up) nil))
5910 (effort-up (and (org-em 'effort-up 'effort-down ss)
5911 (org-cmp-effort a b)))
5912 (effort-down (if effort-up (- effort-up) nil))
5913 (category-up (and (or (org-em 'category-up 'category-down ss)
5914 (memq 'category-keep ss))
5915 (org-cmp-category a b)))
5916 (category-down (if category-up (- category-up) nil))
5917 (category-keep (if category-up +1 nil))
5918 (tag-up (and (org-em 'tag-up 'tag-down ss)
5919 (org-cmp-tag a b)))
5920 (tag-down (if tag-up (- tag-up) nil))
5921 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5922 (org-cmp-todo-state a b)))
5923 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5924 (habit-up (and (org-em 'habit-up 'habit-down ss)
5925 (org-cmp-habit-p a b)))
5926 (habit-down (if habit-up (- habit-up) nil))
5927 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5928 (org-cmp-alpha a b)))
5929 (alpha-down (if alpha-up (- alpha-up) nil))
5930 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5931 user-defined-up user-defined-down)
5932 (if (and need-user-cmp org-agenda-cmp-user-defined
5933 (functionp org-agenda-cmp-user-defined))
5934 (setq user-defined-up
5935 (funcall org-agenda-cmp-user-defined a b)
5936 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5937 (cdr (assoc
5938 (eval (cons 'or org-agenda-sorting-strategy-selected))
5939 '((-1 . t) (1 . nil) (nil . nil))))))
5941 ;;; Agenda restriction lock
5943 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5944 "Overlay to mark the headline to which agenda commands are restricted.")
5945 (overlay-put org-agenda-restriction-lock-overlay
5946 'face 'org-agenda-restriction-lock)
5947 (overlay-put org-agenda-restriction-lock-overlay
5948 'help-echo "Agendas are currently limited to this subtree.")
5949 (org-detach-overlay org-agenda-restriction-lock-overlay)
5951 (defun org-agenda-set-restriction-lock (&optional type)
5952 "Set restriction lock for agenda, to current subtree or file.
5953 Restriction will be the file if TYPE is `file', or if type is the
5954 universal prefix '(4), or if the cursor is before the first headline
5955 in the file. Otherwise, restriction will be to the current subtree."
5956 (interactive "P")
5957 (and (equal type '(4)) (setq type 'file))
5958 (setq type (cond
5959 (type type)
5960 ((org-at-heading-p) 'subtree)
5961 ((condition-case nil (org-back-to-heading t) (error nil))
5962 'subtree)
5963 (t 'file)))
5964 (if (eq type 'subtree)
5965 (progn
5966 (setq org-agenda-restrict t)
5967 (setq org-agenda-overriding-restriction 'subtree)
5968 (put 'org-agenda-files 'org-restrict
5969 (list (buffer-file-name (buffer-base-buffer))))
5970 (org-back-to-heading t)
5971 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5972 (move-marker org-agenda-restrict-begin (point))
5973 (move-marker org-agenda-restrict-end
5974 (save-excursion (org-end-of-subtree t)))
5975 (message "Locking agenda restriction to subtree"))
5976 (put 'org-agenda-files 'org-restrict
5977 (list (buffer-file-name (buffer-base-buffer))))
5978 (setq org-agenda-restrict nil)
5979 (setq org-agenda-overriding-restriction 'file)
5980 (move-marker org-agenda-restrict-begin nil)
5981 (move-marker org-agenda-restrict-end nil)
5982 (message "Locking agenda restriction to file"))
5983 (setq current-prefix-arg nil)
5984 (org-agenda-maybe-redo))
5986 (defun org-agenda-remove-restriction-lock (&optional noupdate)
5987 "Remove the agenda restriction lock."
5988 (interactive "P")
5989 (org-detach-overlay org-agenda-restriction-lock-overlay)
5990 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5991 (setq org-agenda-overriding-restriction nil)
5992 (setq org-agenda-restrict nil)
5993 (put 'org-agenda-files 'org-restrict nil)
5994 (move-marker org-agenda-restrict-begin nil)
5995 (move-marker org-agenda-restrict-end nil)
5996 (setq current-prefix-arg nil)
5997 (message "Agenda restriction lock removed")
5998 (or noupdate (org-agenda-maybe-redo)))
6000 (defun org-agenda-maybe-redo ()
6001 "If there is any window showing the agenda view, update it."
6002 (let ((w (get-buffer-window org-agenda-buffer-name t))
6003 (w0 (selected-window)))
6004 (when w
6005 (select-window w)
6006 (org-agenda-redo)
6007 (select-window w0)
6008 (if org-agenda-overriding-restriction
6009 (message "Agenda view shifted to new %s restriction"
6010 org-agenda-overriding-restriction)
6011 (message "Agenda restriction lock removed")))))
6013 ;;; Agenda commands
6015 (defun org-agenda-check-type (error &rest types)
6016 "Check if agenda buffer is of allowed type.
6017 If ERROR is non-nil, throw an error, otherwise just return nil."
6018 (if (memq org-agenda-type types)
6020 (if error
6021 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6022 nil)))
6024 (defun org-agenda-quit ()
6025 "Exit agenda by removing the window or the buffer."
6026 (interactive)
6027 (if org-agenda-columns-active
6028 (org-columns-quit)
6029 (let ((buf (current-buffer)))
6030 (if (eq org-agenda-window-setup 'other-frame)
6031 (progn
6032 (kill-buffer buf)
6033 (org-agenda-reset-markers)
6034 (org-columns-remove-overlays)
6035 (setq org-agenda-archives-mode nil)
6036 (delete-frame))
6037 (and (not (eq org-agenda-window-setup 'current-window))
6038 (not (one-window-p))
6039 (delete-window))
6040 (kill-buffer buf)
6041 (org-agenda-reset-markers)
6042 (org-columns-remove-overlays)
6043 (setq org-agenda-archives-mode nil)))
6044 ;; Maybe restore the pre-agenda window configuration.
6045 (and org-agenda-restore-windows-after-quit
6046 (not (eq org-agenda-window-setup 'other-frame))
6047 org-pre-agenda-window-conf
6048 (set-window-configuration org-pre-agenda-window-conf))))
6050 (defun org-agenda-exit ()
6051 "Exit agenda by removing the window or the buffer.
6052 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6053 Org-mode buffers visited directly by the user will not be touched."
6054 (interactive)
6055 (org-release-buffers org-agenda-new-buffers)
6056 (setq org-agenda-new-buffers nil)
6057 (org-agenda-quit))
6059 (defun org-agenda-execute (arg)
6060 "Execute another agenda command, keeping same window.
6061 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6062 in the agenda."
6063 (interactive "P")
6064 (let ((org-agenda-window-setup 'current-window))
6065 (org-agenda arg)))
6067 (defun org-agenda-redo ()
6068 "Rebuild Agenda.
6069 When this is the global TODO list, a prefix argument will be interpreted."
6070 (interactive)
6071 (let* ((org-agenda-keep-modes t)
6072 (filter org-agenda-filter)
6073 (preset (get 'org-agenda-filter :preset-filter))
6074 (org-agenda-filter-while-redo (or filter preset))
6075 (cols org-agenda-columns-active)
6076 (line (org-current-line))
6077 (window-line (- line (org-current-line (window-start))))
6078 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6079 (put 'org-agenda-filter :preset-filter nil)
6080 (and cols (org-columns-quit))
6081 (message "Rebuilding agenda buffer...")
6082 (org-let lprops '(eval org-agenda-redo-command))
6083 (setq org-agenda-undo-list nil
6084 org-agenda-pending-undo-list nil)
6085 (message "Rebuilding agenda buffer...done")
6086 (put 'org-agenda-filter :preset-filter preset)
6087 (and (or filter preset) (org-agenda-filter-apply filter))
6088 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6089 (org-goto-line line)
6090 (recenter window-line)))
6093 (defvar org-global-tags-completion-table nil)
6094 (defvar org-agenda-filter-form nil)
6095 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6096 "Keep only those lines in the agenda buffer that have a specific tag.
6097 The tag is selected with its fast selection letter, as configured.
6098 With prefix argument STRIP, remove all lines that do have the tag.
6099 A lisp caller can specify CHAR. NARROW means that the new tag should be
6100 used to narrow the search - the interactive user can also press `-' or `+'
6101 to switch to narrowing."
6102 (interactive "P")
6103 (let* ((alist org-tag-alist-for-agenda)
6104 (tag-chars (mapconcat
6105 (lambda (x) (if (and (not (symbolp (car x)))
6106 (cdr x))
6107 (char-to-string (cdr x))
6108 ""))
6109 alist ""))
6110 (efforts (org-split-string
6111 (or (cdr (assoc (concat org-effort-property "_ALL")
6112 org-global-properties))
6113 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6114 "")))
6115 (effort-op org-agenda-filter-effort-default-operator)
6116 (effort-prompt "")
6117 (inhibit-read-only t)
6118 (current org-agenda-filter)
6119 maybe-refresh a n tag)
6120 (unless char
6121 (message
6122 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6123 (if narrow "Narrow" "Filter") tag-chars
6124 (if org-agenda-auto-exclude-function "[RET], " ""))
6125 (setq char (read-char)))
6126 (when (member char '(?+ ?-))
6127 ;; Narrowing down
6128 (cond ((equal char ?-) (setq strip t narrow t))
6129 ((equal char ?+) (setq strip nil narrow t)))
6130 (message
6131 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6132 (setq char (read-char)))
6133 (when (member char '(?< ?> ?= ??))
6134 ;; An effort operator
6135 (setq effort-op (char-to-string char))
6136 (setq alist nil) ; to make sure it will be interpreted as effort.
6137 (unless (equal char ??)
6138 (loop for i from 0 to 9 do
6139 (setq effort-prompt
6140 (concat
6141 effort-prompt " ["
6142 (if (= i 9) "0" (int-to-string (1+ i)))
6143 "]" (nth i efforts))))
6144 (message "Effort%s: %s " effort-op effort-prompt)
6145 (setq char (read-char))
6146 (when (or (< char ?0) (> char ?9))
6147 (error "Need 1-9,0 to select effort" ))))
6148 (when (equal char ?\t)
6149 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6150 (org-set-local 'org-global-tags-completion-table
6151 (org-global-tags-completion-table)))
6152 (let ((completion-ignore-case t))
6153 (setq tag (org-icompleting-read
6154 "Tag: " org-global-tags-completion-table))))
6155 (cond
6156 ((equal char ?\r)
6157 (org-agenda-filter-by-tag-show-all)
6158 (when org-agenda-auto-exclude-function
6159 (setq org-agenda-filter '())
6160 (dolist (tag (org-agenda-get-represented-tags))
6161 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6162 (if modifier
6163 (push modifier org-agenda-filter))))
6164 (if (not (null org-agenda-filter))
6165 (org-agenda-filter-apply org-agenda-filter)))
6166 (setq maybe-refresh t))
6167 ((equal char ?/)
6168 (org-agenda-filter-by-tag-show-all)
6169 (when (get 'org-agenda-filter :preset-filter)
6170 (org-agenda-filter-apply org-agenda-filter))
6171 (setq maybe-refresh t))
6172 ((or (equal char ?\ )
6173 (setq a (rassoc char alist))
6174 (and (>= char ?0) (<= char ?9)
6175 (setq n (if (= char ?0) 9 (- char ?0 1))
6176 tag (concat effort-op (nth n efforts))
6177 a (cons tag nil)))
6178 (and (= char ??)
6179 (setq tag "?eff")
6180 a (cons tag nil))
6181 (and tag (setq a (cons tag nil))))
6182 (org-agenda-filter-by-tag-show-all)
6183 (setq tag (car a))
6184 (setq org-agenda-filter
6185 (cons (concat (if strip "-" "+") tag)
6186 (if narrow current nil)))
6187 (org-agenda-filter-apply org-agenda-filter)
6188 (setq maybe-refresh t))
6189 (t (error "Invalid tag selection character %c" char)))
6190 (when (and maybe-refresh
6191 (eq org-agenda-clockreport-mode 'with-filter))
6192 (org-agenda-redo))))
6194 (defun org-agenda-get-represented-tags ()
6195 "Get a list of all tags currently represented in the agenda."
6196 (let (p tags)
6197 (save-excursion
6198 (goto-char (point-min))
6199 (while (setq p (next-single-property-change (point) 'tags))
6200 (goto-char p)
6201 (mapc (lambda (x) (add-to-list 'tags x))
6202 (get-text-property (point) 'tags))))
6203 tags))
6205 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6206 "Refine the current filter. See `org-agenda-filter-by-tag."
6207 (interactive "P")
6208 (org-agenda-filter-by-tag strip char 'refine))
6210 (defun org-agenda-filter-make-matcher ()
6211 "Create the form that tests a line for the agenda filter."
6212 (let (f f1)
6213 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6214 org-agenda-filter))
6215 (if (member x '("-" "+"))
6216 (setq f1 (if (equal x "-") 'tags '(not tags)))
6217 (if (string-match "[<=>?]" x)
6218 (setq f1 (org-agenda-filter-effort-form x))
6219 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6220 (if (equal (string-to-char x) ?-)
6221 (setq f1 (list 'not f1))))
6222 (push f1 f))
6223 (cons 'and (nreverse f))))
6225 (defun org-agenda-filter-effort-form (e)
6226 "Return the form to compare the effort of the current line with what E says.
6227 E looks like \"+<2:25\"."
6228 (let (op)
6229 (setq e (substring e 1))
6230 (setq op (string-to-char e) e (substring e 1))
6231 (setq op (cond ((equal op ?<) '<=)
6232 ((equal op ?>) '>=)
6233 ((equal op ??) op)
6234 (t '=)))
6235 (list 'org-agenda-compare-effort (list 'quote op)
6236 (org-duration-string-to-minutes e))))
6238 (defun org-agenda-compare-effort (op value)
6239 "Compare the effort of the current line with VALUE, using OP.
6240 If the line does not have an effort defined, return nil."
6241 (let ((eff (org-get-at-bol 'effort-minutes)))
6242 (if (equal op ??)
6243 (not eff)
6244 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6245 value))))
6247 (defun org-agenda-filter-apply (filter)
6248 "Set FILTER as the new agenda filter and apply it."
6249 (let (tags)
6250 (setq org-agenda-filter filter
6251 org-agenda-filter-form (org-agenda-filter-make-matcher))
6252 (org-agenda-set-mode-name)
6253 (save-excursion
6254 (goto-char (point-min))
6255 (while (not (eobp))
6256 (if (org-get-at-bol 'org-marker)
6257 (progn
6258 (setq tags (org-get-at-bol 'tags)) ; used in eval
6259 (if (not (eval org-agenda-filter-form))
6260 (org-agenda-filter-by-tag-hide-line))
6261 (beginning-of-line 2))
6262 (beginning-of-line 2))))
6263 (if (get-char-property (point) 'invisible)
6264 (org-agenda-previous-line))))
6266 (defun org-agenda-filter-by-tag-hide-line ()
6267 (let (ov)
6268 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6269 (point-at-eol)))
6270 (overlay-put ov 'invisible t)
6271 (overlay-put ov 'type 'tags-filter)
6272 (push ov org-agenda-filter-overlays)))
6274 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6275 (setq pos (or pos (point)))
6276 (save-excursion
6277 (dolist (ov (overlays-at pos))
6278 (when (and (overlay-get ov 'invisible)
6279 (eq (overlay-get ov 'type) 'tags-filter))
6280 (goto-char pos)
6281 (if (< (overlay-start ov) (point-at-eol))
6282 (move-overlay ov (point-at-eol)
6283 (overlay-end ov)))))))
6285 (defun org-agenda-filter-by-tag-show-all ()
6286 (mapc 'delete-overlay org-agenda-filter-overlays)
6287 (setq org-agenda-filter-overlays nil)
6288 (setq org-agenda-filter nil)
6289 (setq org-agenda-filter-form nil)
6290 (org-agenda-set-mode-name))
6292 (defun org-agenda-manipulate-query-add ()
6293 "Manipulate the query by adding a search term with positive selection.
6294 Positive selection means the term must be matched for selection of an entry."
6295 (interactive)
6296 (org-agenda-manipulate-query ?\[))
6297 (defun org-agenda-manipulate-query-subtract ()
6298 "Manipulate the query by adding a search term with negative selection.
6299 Negative selection means term must not be matched for selection of an entry."
6300 (interactive)
6301 (org-agenda-manipulate-query ?\]))
6302 (defun org-agenda-manipulate-query-add-re ()
6303 "Manipulate the query by adding a search regexp with positive selection.
6304 Positive selection means the regexp must match for selection of an entry."
6305 (interactive)
6306 (org-agenda-manipulate-query ?\{))
6307 (defun org-agenda-manipulate-query-subtract-re ()
6308 "Manipulate the query by adding a search regexp with negative selection.
6309 Negative selection means regexp must not match for selection of an entry."
6310 (interactive)
6311 (org-agenda-manipulate-query ?\}))
6312 (defun org-agenda-manipulate-query (char)
6313 (cond
6314 ((memq org-agenda-type '(timeline agenda))
6315 (let ((org-agenda-include-inactive-timestamps t))
6316 (org-agenda-redo))
6317 (message "Display now includes inactive timestamps as well"))
6318 ((eq org-agenda-type 'search)
6319 (org-add-to-string
6320 'org-agenda-query-string
6321 (if org-agenda-last-search-view-search-was-boolean
6322 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6323 (?\{ . " +{}") (?\} . " -{}"))))
6324 " "))
6325 (setq org-agenda-redo-command
6326 (list 'org-search-view
6327 org-todo-only
6328 org-agenda-query-string
6329 (+ (length org-agenda-query-string)
6330 (if (member char '(?\{ ?\})) 0 1))))
6331 (set-register org-agenda-query-register org-agenda-query-string)
6332 (org-agenda-redo))
6333 (t (error "Cannot manipulate query for %s-type agenda buffers"
6334 org-agenda-type))))
6336 (defun org-add-to-string (var string)
6337 (set var (concat (symbol-value var) string)))
6339 (defun org-agenda-goto-date (date)
6340 "Jump to DATE in agenda."
6341 (interactive (list (let ((org-read-date-prefer-future
6342 (eval org-agenda-jump-prefer-future)))
6343 (org-read-date))))
6344 (org-agenda-list nil date))
6346 (defun org-agenda-goto-today ()
6347 "Go to today."
6348 (interactive)
6349 (org-agenda-check-type t 'timeline 'agenda)
6350 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6351 (cond
6352 (tdpos (goto-char tdpos))
6353 ((eq org-agenda-type 'agenda)
6354 (let* ((sd (org-agenda-compute-starting-span
6355 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6356 (org-agenda-overriding-arguments org-agenda-last-arguments))
6357 (setf (nth 1 org-agenda-overriding-arguments) sd)
6358 (org-agenda-redo)
6359 (org-agenda-find-same-or-today-or-agenda)))
6360 (t (error "Cannot find today")))))
6362 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6363 (goto-char
6364 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6365 (text-property-any (point-min) (point-max) 'org-today t)
6366 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6367 (point-min))))
6369 (defun org-agenda-later (arg)
6370 "Go forward in time by thee current span.
6371 With prefix ARG, go forward that many times the current span."
6372 (interactive "p")
6373 (org-agenda-check-type t 'agenda)
6374 (let* ((span org-agenda-current-span)
6375 (sd org-starting-day)
6376 (greg (calendar-gregorian-from-absolute sd))
6377 (cnt (org-get-at-bol 'org-day-cnt))
6378 greg2)
6379 (cond
6380 ((eq span 'day)
6381 (setq sd (+ arg sd)))
6382 ((eq span 'week)
6383 (setq sd (+ (* 7 arg) sd)))
6384 ((eq span 'month)
6385 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6386 sd (calendar-absolute-from-gregorian greg2))
6387 (setcar greg2 (1+ (car greg2))))
6388 ((eq span 'year)
6389 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6390 sd (calendar-absolute-from-gregorian greg2))
6391 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6393 (setq sd (+ (* span arg) sd))))
6394 (let ((org-agenda-overriding-arguments
6395 (list (car org-agenda-last-arguments) sd span t)))
6396 (org-agenda-redo)
6397 (org-agenda-find-same-or-today-or-agenda cnt))))
6399 (defun org-agenda-earlier (arg)
6400 "Go backward in time by the current span.
6401 With prefix ARG, go backward that many times the current span."
6402 (interactive "p")
6403 (org-agenda-later (- arg)))
6405 (defun org-agenda-view-mode-dispatch ()
6406 "Call one of the view mode commands."
6407 (interactive)
6408 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6409 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6410 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6411 [E]ntryText")
6412 (let ((a (read-char-exclusive)))
6413 (case a
6414 (?\ (call-interactively 'org-agenda-reset-view))
6415 (?d (call-interactively 'org-agenda-day-view))
6416 (?w (call-interactively 'org-agenda-week-view))
6417 (?m (call-interactively 'org-agenda-month-view))
6418 (?y (call-interactively 'org-agenda-year-view))
6419 (?l (call-interactively 'org-agenda-log-mode))
6420 (?L (org-agenda-log-mode '(4)))
6421 (?c (org-agenda-log-mode 'clockcheck))
6422 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6423 (?a (call-interactively 'org-agenda-archives-mode))
6424 (?A (org-agenda-archives-mode 'files))
6425 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6426 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6427 (?G (call-interactively 'org-agenda-toggle-time-grid))
6428 (?D (call-interactively 'org-agenda-toggle-diary))
6429 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6430 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6431 (org-agenda-check-type t 'timeline 'agenda)
6432 (org-agenda-redo))
6433 (message "Display now includes inactive timestamps as well"))
6434 (?q (message "Abort"))
6435 (otherwise (error "Invalid key" )))))
6437 (defun org-agenda-reset-view ()
6438 "Switch to default view for agenda."
6439 (interactive)
6440 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6441 (defun org-agenda-day-view (&optional day-of-year)
6442 "Switch to daily view for agenda.
6443 With argument DAY-OF-YEAR, switch to that day of the year."
6444 (interactive "P")
6445 (org-agenda-change-time-span 'day day-of-year))
6446 (defun org-agenda-week-view (&optional iso-week)
6447 "Switch to daily view for agenda.
6448 With argument ISO-WEEK, switch to the corresponding ISO week.
6449 If ISO-WEEK has more then 2 digits, only the last two encode the
6450 week. Any digits before this encode a year. So 200712 means
6451 week 12 of year 2007. Years in the range 1938-2037 can also be
6452 written as 2-digit years."
6453 (interactive "P")
6454 (org-agenda-change-time-span 'week iso-week))
6455 (defun org-agenda-month-view (&optional month)
6456 "Switch to monthly view for agenda.
6457 With argument MONTH, switch to that month."
6458 (interactive "P")
6459 (org-agenda-change-time-span 'month month))
6460 (defun org-agenda-year-view (&optional year)
6461 "Switch to yearly view for agenda.
6462 With argument YEAR, switch to that year.
6463 If MONTH has more then 2 digits, only the last two encode the
6464 month. Any digits before this encode a year. So 200712 means
6465 December year 2007. Years in the range 1938-2037 can also be
6466 written as 2-digit years."
6467 (interactive "P")
6468 (when year
6469 (setq year (org-small-year-to-year year)))
6470 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6471 (org-agenda-change-time-span 'year year)
6472 (error "Abort")))
6474 (defun org-agenda-change-time-span (span &optional n)
6475 "Change the agenda view to SPAN.
6476 SPAN may be `day', `week', `month', `year'."
6477 (org-agenda-check-type t 'agenda)
6478 (if (and (not n) (equal org-agenda-current-span span))
6479 (error "Viewing span is already \"%s\"" span))
6480 (let* ((sd (or (org-get-at-bol 'day)
6481 org-starting-day))
6482 (sd (org-agenda-compute-starting-span sd span n))
6483 (org-agenda-overriding-arguments
6484 (or org-agenda-overriding-arguments
6485 (list (car org-agenda-last-arguments) sd span t))))
6486 (org-agenda-redo)
6487 (org-agenda-find-same-or-today-or-agenda))
6488 (org-agenda-set-mode-name)
6489 (message "Switched to %s view" span))
6491 (defun org-agenda-compute-starting-span (sd span &optional n)
6492 "Compute starting date for agenda.
6493 SPAN may be `day', `week', `month', `year'. The return value
6494 is a cons cell with the starting date and the number of days,
6495 so that the date SD will be in that range."
6496 (let* ((greg (calendar-gregorian-from-absolute sd))
6497 (dg (nth 1 greg))
6498 (mg (car greg))
6499 (yg (nth 2 greg)))
6500 (cond
6501 ((eq span 'day)
6502 (when n
6503 (setq sd (+ (calendar-absolute-from-gregorian
6504 (list mg 1 yg))
6505 n -1))))
6506 ((eq span 'week)
6507 (let* ((nt (calendar-day-of-week
6508 (calendar-gregorian-from-absolute sd)))
6509 (d (if org-agenda-start-on-weekday
6510 (- nt org-agenda-start-on-weekday)
6513 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6514 (when n
6515 (require 'cal-iso)
6516 (when (> n 99)
6517 (setq y1 (org-small-year-to-year (/ n 100))
6518 n (mod n 100)))
6519 (setq sd
6520 (calendar-absolute-from-iso
6521 (list n 1
6522 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6523 ((eq span 'month)
6524 (let (y1)
6525 (when (and n (> n 99))
6526 (setq y1 (org-small-year-to-year (/ n 100))
6527 n (mod n 100)))
6528 (setq sd (calendar-absolute-from-gregorian
6529 (list (or n mg) 1 (or y1 yg))))))
6530 ((eq span 'year)
6531 (setq sd (calendar-absolute-from-gregorian
6532 (list 1 1 (or n yg))))))
6533 sd))
6535 (defun org-agenda-next-date-line (&optional arg)
6536 "Jump to the next line indicating a date in agenda buffer."
6537 (interactive "p")
6538 (org-agenda-check-type t 'agenda 'timeline)
6539 (beginning-of-line 1)
6540 ;; This does not work if user makes date format that starts with a blank
6541 (if (looking-at "^\\S-") (forward-char 1))
6542 (if (not (re-search-forward "^\\S-" nil t arg))
6543 (progn
6544 (backward-char 1)
6545 (error "No next date after this line in this buffer")))
6546 (goto-char (match-beginning 0)))
6548 (defun org-agenda-previous-date-line (&optional arg)
6549 "Jump to the previous line indicating a date in agenda buffer."
6550 (interactive "p")
6551 (org-agenda-check-type t 'agenda 'timeline)
6552 (beginning-of-line 1)
6553 (if (not (re-search-backward "^\\S-" nil t arg))
6554 (error "No previous date before this line in this buffer")))
6556 ;; Initialize the highlight
6557 (defvar org-hl (make-overlay 1 1))
6558 (overlay-put org-hl 'face 'highlight)
6560 (defun org-highlight (begin end &optional buffer)
6561 "Highlight a region with overlay."
6562 (move-overlay org-hl begin end (or buffer (current-buffer))))
6564 (defun org-unhighlight ()
6565 "Detach overlay INDEX."
6566 (org-detach-overlay org-hl))
6568 ;; FIXME this is currently not used.
6569 (defun org-highlight-until-next-command (beg end &optional buffer)
6570 "Move the highlight overlay to BEG/END, remove it before the next command."
6571 (org-highlight beg end buffer)
6572 (add-hook 'pre-command-hook 'org-unhighlight-once))
6573 (defun org-unhighlight-once ()
6574 "Remove the highlight from its position, and this function from the hook."
6575 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6576 (org-unhighlight))
6578 (defun org-agenda-follow-mode ()
6579 "Toggle follow mode in an agenda buffer."
6580 (interactive)
6581 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6582 (org-agenda-set-mode-name)
6583 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6584 (org-agenda-show))
6585 (message "Follow mode is %s"
6586 (if org-agenda-follow-mode "on" "off")))
6588 (defun org-agenda-entry-text-mode (&optional arg)
6589 "Toggle entry text mode in an agenda buffer."
6590 (interactive "P")
6591 (setq org-agenda-entry-text-mode (or (integerp arg)
6592 (not org-agenda-entry-text-mode)))
6593 (org-agenda-entry-text-hide)
6594 (and org-agenda-entry-text-mode
6595 (let ((org-agenda-entry-text-maxlines
6596 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6597 (org-agenda-entry-text-show)))
6598 (org-agenda-set-mode-name)
6599 (message "Entry text mode is %s. Maximum number of lines is %d"
6600 (if org-agenda-entry-text-mode "on" "off")
6601 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6603 (defun org-agenda-clockreport-mode (&optional with-filter)
6604 "Toggle clocktable mode in an agenda buffer.
6605 With prefix arg WITH-FILTER, make the clocktable respect the current
6606 agenda filter."
6607 (interactive "P")
6608 (org-agenda-check-type t 'agenda)
6609 (if with-filter
6610 (setq org-agenda-clockreport-mode 'with-filter)
6611 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6612 (org-agenda-set-mode-name)
6613 (org-agenda-redo)
6614 (message "Clocktable mode is %s"
6615 (if org-agenda-clockreport-mode "on" "off")))
6617 (defun org-agenda-log-mode (&optional special)
6618 "Toggle log mode in an agenda buffer.
6619 With argument SPECIAL, show all possible log items, not only the ones
6620 configured in `org-agenda-log-mode-items'.
6621 With a double `C-u' prefix arg, show *only* log items, nothing else."
6622 (interactive "P")
6623 (org-agenda-check-type t 'agenda 'timeline)
6624 (setq org-agenda-show-log
6625 (cond
6626 ((equal special '(16)) 'only)
6627 ((eq special 'clockcheck)
6628 (if (eq org-agenda-show-log 'clockcheck)
6629 nil 'clockcheck))
6630 (special '(closed clock state))
6631 (t (not org-agenda-show-log))))
6632 (org-agenda-set-mode-name)
6633 (org-agenda-redo)
6634 (message "Log mode is %s"
6635 (if org-agenda-show-log "on" "off")))
6637 (defun org-agenda-archives-mode (&optional with-files)
6638 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6639 When called with a prefix argument, include all archive files as well."
6640 (interactive "P")
6641 (setq org-agenda-archives-mode
6642 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6643 (org-agenda-set-mode-name)
6644 (org-agenda-redo)
6645 (message
6646 "%s"
6647 (cond
6648 ((eq org-agenda-archives-mode nil)
6649 "No archives are included")
6650 ((eq org-agenda-archives-mode 'trees)
6651 (format "Trees with :%s: tag are included" org-archive-tag))
6652 ((eq org-agenda-archives-mode t)
6653 (format "Trees with :%s: tag and all active archive files are included"
6654 org-archive-tag)))))
6656 (defun org-agenda-toggle-diary ()
6657 "Toggle diary inclusion in an agenda buffer."
6658 (interactive)
6659 (org-agenda-check-type t 'agenda)
6660 (setq org-agenda-include-diary (not org-agenda-include-diary))
6661 (org-agenda-redo)
6662 (org-agenda-set-mode-name)
6663 (message "Diary inclusion turned %s"
6664 (if org-agenda-include-diary "on" "off")))
6666 (defun org-agenda-toggle-deadlines ()
6667 "Toggle inclusion of entries with a deadline in an agenda buffer."
6668 (interactive)
6669 (org-agenda-check-type t 'agenda)
6670 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6671 (org-agenda-redo)
6672 (org-agenda-set-mode-name)
6673 (message "Deadlines inclusion turned %s"
6674 (if org-agenda-include-deadlines "on" "off")))
6676 (defun org-agenda-toggle-time-grid ()
6677 "Toggle time grid in an agenda buffer."
6678 (interactive)
6679 (org-agenda-check-type t 'agenda)
6680 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6681 (org-agenda-redo)
6682 (org-agenda-set-mode-name)
6683 (message "Time-grid turned %s"
6684 (if org-agenda-use-time-grid "on" "off")))
6686 (defun org-agenda-set-mode-name ()
6687 "Set the mode name to indicate all the small mode settings."
6688 (setq mode-name
6689 (list "Org-Agenda"
6690 (if (get 'org-agenda-files 'org-restrict) " []" "")
6692 '(:eval (org-agenda-span-name org-agenda-current-span))
6693 (if org-agenda-follow-mode " Follow" "")
6694 (if org-agenda-entry-text-mode " ETxt" "")
6695 (if org-agenda-include-diary " Diary" "")
6696 (if org-agenda-include-deadlines " Ddl" "")
6697 (if org-agenda-use-time-grid " Grid" "")
6698 (if (and (boundp 'org-habit-show-habits)
6699 org-habit-show-habits) " Habit" "")
6700 (cond
6701 ((consp org-agenda-show-log) " LogAll")
6702 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6703 (org-agenda-show-log " Log")
6704 (t ""))
6705 (if (or org-agenda-filter (get 'org-agenda-filter
6706 :preset-filter))
6707 (concat " {" (mapconcat
6708 'identity
6709 (append (get 'org-agenda-filter
6710 :preset-filter)
6711 org-agenda-filter) "") "}")
6713 (if org-agenda-archives-mode
6714 (if (eq org-agenda-archives-mode t)
6715 " Archives"
6716 (format " :%s:" org-archive-tag))
6718 (if org-agenda-clockreport-mode
6719 (if (eq org-agenda-clockreport-mode 'with-filter)
6720 " Clock{}" " Clock")
6721 "")))
6722 (force-mode-line-update))
6724 (defun org-agenda-post-command-hook ()
6725 (setq org-agenda-type
6726 (or (get-text-property (point) 'org-agenda-type)
6727 (get-text-property (max (point-min) (1- (point)))
6728 'org-agenda-type))))
6730 (defun org-agenda-next-line ()
6731 "Move cursor to the next line, and show if follow mode is active."
6732 (interactive)
6733 (call-interactively 'next-line)
6734 (org-agenda-do-context-action))
6736 (defun org-agenda-previous-line ()
6737 "Move cursor to the previous line, and show if follow-mode is active."
6738 (interactive)
6739 (call-interactively 'previous-line)
6740 (org-agenda-do-context-action))
6742 (defun org-agenda-do-context-action ()
6743 "Show outline path and, maybe, follow mode window."
6744 (let ((m (org-get-at-bol 'org-marker)))
6745 (if (and org-agenda-follow-mode m)
6746 (org-agenda-show))
6747 (if (and m org-agenda-show-outline-path)
6748 (org-with-point-at m
6749 (org-display-outline-path t)))))
6751 (defun org-agenda-show-priority ()
6752 "Show the priority of the current item.
6753 This priority is composed of the main priority given with the [#A] cookies,
6754 and by additional input from the age of a schedules or deadline entry."
6755 (interactive)
6756 (let* ((pri (org-get-at-bol 'priority)))
6757 (message "Priority is %d" (if pri pri -1000))))
6759 (defun org-agenda-show-tags ()
6760 "Show the tags applicable to the current item."
6761 (interactive)
6762 (let* ((tags (org-get-at-bol 'tags)))
6763 (if tags
6764 (message "Tags are :%s:"
6765 (org-no-properties (mapconcat 'identity tags ":")))
6766 (message "No tags associated with this line"))))
6768 (defun org-agenda-goto (&optional highlight)
6769 "Go to the Org-mode file which contains the item at point."
6770 (interactive)
6771 (let* ((marker (or (org-get-at-bol 'org-marker)
6772 (org-agenda-error)))
6773 (buffer (marker-buffer marker))
6774 (pos (marker-position marker)))
6775 (switch-to-buffer-other-window buffer)
6776 (widen)
6777 (push-mark)
6778 (goto-char pos)
6779 (when (org-mode-p)
6780 (org-show-context 'agenda)
6781 (save-excursion
6782 (and (outline-next-heading)
6783 (org-flag-heading nil)))) ; show the next heading
6784 (when (outline-invisible-p)
6785 (show-entry)) ; display invisible text
6786 (recenter (/ (window-height) 2))
6787 (run-hooks 'org-agenda-after-show-hook)
6788 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6790 (defvar org-agenda-after-show-hook nil
6791 "Normal hook run after an item has been shown from the agenda.
6792 Point is in the buffer where the item originated.")
6794 (defun org-agenda-kill ()
6795 "Kill the entry or subtree belonging to the current agenda entry."
6796 (interactive)
6797 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6798 (let* ((marker (or (org-get-at-bol 'org-marker)
6799 (org-agenda-error)))
6800 (buffer (marker-buffer marker))
6801 (pos (marker-position marker))
6802 (type (org-get-at-bol 'type))
6803 dbeg dend (n 0) conf)
6804 (org-with-remote-undo buffer
6805 (with-current-buffer buffer
6806 (save-excursion
6807 (goto-char pos)
6808 (if (and (org-mode-p) (not (member type '("sexp"))))
6809 (setq dbeg (progn (org-back-to-heading t) (point))
6810 dend (org-end-of-subtree t t))
6811 (setq dbeg (point-at-bol)
6812 dend (min (point-max) (1+ (point-at-eol)))))
6813 (goto-char dbeg)
6814 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6815 (setq conf (or (eq t org-agenda-confirm-kill)
6816 (and (numberp org-agenda-confirm-kill)
6817 (> n org-agenda-confirm-kill))))
6818 (and conf
6819 (not (y-or-n-p
6820 (format "Delete entry with %d lines in buffer \"%s\"? "
6821 n (buffer-name buffer))))
6822 (error "Abort"))
6823 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6824 (with-current-buffer buffer (delete-region dbeg dend))
6825 (message "Agenda item and source killed"))))
6827 (defvar org-archive-default-command)
6828 (defun org-agenda-archive-default ()
6829 "Archive the entry or subtree belonging to the current agenda entry."
6830 (interactive)
6831 (require 'org-archive)
6832 (org-agenda-archive-with org-archive-default-command))
6834 (defun org-agenda-archive-default-with-confirmation ()
6835 "Archive the entry or subtree belonging to the current agenda entry."
6836 (interactive)
6837 (require 'org-archive)
6838 (org-agenda-archive-with org-archive-default-command 'confirm))
6840 (defun org-agenda-archive ()
6841 "Archive the entry or subtree belonging to the current agenda entry."
6842 (interactive)
6843 (org-agenda-archive-with 'org-archive-subtree))
6845 (defun org-agenda-archive-to-archive-sibling ()
6846 "Move the entry to the archive sibling."
6847 (interactive)
6848 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6850 (defun org-agenda-archive-with (cmd &optional confirm)
6851 "Move the entry to the archive sibling."
6852 (interactive)
6853 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6854 (let* ((marker (or (org-get-at-bol 'org-marker)
6855 (org-agenda-error)))
6856 (buffer (marker-buffer marker))
6857 (pos (marker-position marker)))
6858 (org-with-remote-undo buffer
6859 (with-current-buffer buffer
6860 (if (org-mode-p)
6861 (if (and confirm
6862 (not (y-or-n-p "Archive this subtree or entry? ")))
6863 (error "Abort")
6864 (save-excursion
6865 (goto-char pos)
6866 (org-remove-subtree-entries-from-agenda)
6867 (org-back-to-heading t)
6868 (funcall cmd)))
6869 (error "Archiving works only in Org-mode files"))))))
6871 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6872 "Remove all lines in the agenda that correspond to a given subtree.
6873 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6874 If this information is not given, the function uses the tree at point."
6875 (let ((buf (or buf (current-buffer))) m p)
6876 (save-excursion
6877 (unless (and beg end)
6878 (org-back-to-heading t)
6879 (setq beg (point))
6880 (org-end-of-subtree t)
6881 (setq end (point)))
6882 (set-buffer (get-buffer org-agenda-buffer-name))
6883 (save-excursion
6884 (goto-char (point-max))
6885 (beginning-of-line 1)
6886 (while (not (bobp))
6887 (when (and (setq m (org-get-at-bol 'org-marker))
6888 (equal buf (marker-buffer m))
6889 (setq p (marker-position m))
6890 (>= p beg)
6891 (< p end))
6892 (let ((inhibit-read-only t))
6893 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6894 (beginning-of-line 0))))))
6896 (defun org-agenda-refile (&optional goto rfloc no-update)
6897 "Refile the item at point."
6898 (interactive "P")
6899 (if (equal goto '(16))
6900 (org-refile-goto-last-stored)
6901 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6902 (org-agenda-error)))
6903 (buffer (marker-buffer marker))
6904 (pos (marker-position marker))
6905 (rfloc (or rfloc
6906 (org-refile-get-location
6907 (if goto "Goto" "Refile to") buffer
6908 org-refile-allow-creating-parent-nodes))))
6909 (with-current-buffer buffer
6910 (save-excursion
6911 (save-restriction
6912 (widen)
6913 (goto-char marker)
6914 (org-remove-subtree-entries-from-agenda)
6915 (org-refile goto buffer rfloc)))))
6916 (unless no-update (org-agenda-redo))))
6918 (defun org-agenda-open-link (&optional arg)
6919 "Follow the link in the current line, if any.
6920 This looks for a link in the displayed line in the agenda. It also looks
6921 at the text of the entry itself."
6922 (interactive "P")
6923 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6924 (org-get-at-bol 'org-marker)))
6925 (buffer (and marker (marker-buffer marker)))
6926 (prefix (buffer-substring
6927 (point-at-bol) (point-at-eol))))
6928 (cond
6929 (buffer
6930 (with-current-buffer buffer
6931 (save-excursion
6932 (save-restriction
6933 (widen)
6934 (goto-char marker)
6935 (org-offer-links-in-entry arg prefix)))))
6936 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6937 (save-excursion
6938 (beginning-of-line 1)
6939 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6940 (org-open-link-from-string (match-string 1)))
6941 (t (error "No link to open here")))))
6943 (defun org-agenda-copy-local-variable (var)
6944 "Get a variable from a referenced buffer and install it here."
6945 (let ((m (org-get-at-bol 'org-marker)))
6946 (when (and m (buffer-live-p (marker-buffer m)))
6947 (org-set-local var (with-current-buffer (marker-buffer m)
6948 (symbol-value var))))))
6950 (defun org-agenda-switch-to (&optional delete-other-windows)
6951 "Go to the Org-mode file which contains the item at point."
6952 (interactive)
6953 (if (and org-return-follows-link
6954 (not (org-get-at-bol 'org-marker))
6955 (org-in-regexp org-bracket-link-regexp))
6956 (org-open-link-from-string (match-string 0))
6957 (let* ((marker (or (org-get-at-bol 'org-marker)
6958 (org-agenda-error)))
6959 (buffer (marker-buffer marker))
6960 (pos (marker-position marker)))
6961 (switch-to-buffer buffer)
6962 (and delete-other-windows (delete-other-windows))
6963 (widen)
6964 (goto-char pos)
6965 (when (org-mode-p)
6966 (org-show-context 'agenda)
6967 (save-excursion
6968 (and (outline-next-heading)
6969 (org-flag-heading nil))) ; show the next heading
6970 (when (outline-invisible-p)
6971 (show-entry)))))) ; display invisible text
6973 (defun org-agenda-goto-mouse (ev)
6974 "Go to the Org-mode file which contains the item at the mouse click."
6975 (interactive "e")
6976 (mouse-set-point ev)
6977 (org-agenda-goto))
6979 (defun org-agenda-show (&optional full-entry)
6980 "Display the Org-mode file which contains the item at point.
6981 With prefix argument FULL-ENTRY, make the entire entry visible
6982 if it was hidden in the outline."
6983 (interactive "P")
6984 (let ((win (selected-window)))
6985 (if full-entry
6986 (let ((org-show-entry-below t))
6987 (org-agenda-goto t))
6988 (org-agenda-goto t))
6989 (select-window win)))
6991 (defvar org-agenda-show-window nil)
6992 (defun org-agenda-show-and-scroll-up ()
6993 "Display the Org-mode file which contains the item at point.
6994 When called repeatedly, scroll the window that is displaying the buffer."
6995 (interactive)
6996 (let ((win (selected-window)))
6997 (if (and (window-live-p org-agenda-show-window)
6998 (eq this-command last-command))
6999 (progn
7000 (select-window org-agenda-show-window)
7001 (ignore-errors (scroll-up)))
7002 (org-agenda-goto t)
7003 (show-subtree)
7004 (setq org-agenda-show-window (selected-window)))
7005 (select-window win)))
7007 (defun org-agenda-show-scroll-down ()
7008 "Scroll down the window showing the agenda."
7009 (interactive)
7010 (let ((win (selected-window)))
7011 (when (window-live-p org-agenda-show-window)
7012 (select-window org-agenda-show-window)
7013 (ignore-errors (scroll-down))
7014 (select-window win))))
7016 (defun org-agenda-show-1 (&optional more)
7017 "Display the Org-mode file which contains the item at point.
7018 The prefix arg selects the amount of information to display:
7020 0 hide the subtree
7021 1 just show the entry according to defaults.
7022 2 show the children view
7023 3 show the subtree view
7024 4 show the entire subtree and any LOGBOOK drawers
7025 5 show the entire subtree and any drawers
7026 With prefix argument FULL-ENTRY, make the entire entry visible
7027 if it was hidden in the outline."
7028 (interactive "p")
7029 (let ((win (selected-window)))
7030 (org-agenda-goto t)
7031 (org-recenter-heading 1)
7032 (cond
7033 ((= more 0)
7034 (hide-subtree)
7035 (save-excursion
7036 (org-back-to-heading)
7037 (run-hook-with-args 'org-cycle-hook 'folded))
7038 (message "Remote: FOLDED"))
7039 ((and (org-called-interactively-p 'any) (= more 1))
7040 (message "Remote: show with default settings"))
7041 ((= more 2)
7042 (show-entry)
7043 (show-children)
7044 (save-excursion
7045 (org-back-to-heading)
7046 (run-hook-with-args 'org-cycle-hook 'children))
7047 (message "Remote: CHILDREN"))
7048 ((= more 3)
7049 (show-subtree)
7050 (save-excursion
7051 (org-back-to-heading)
7052 (run-hook-with-args 'org-cycle-hook 'subtree))
7053 (message "Remote: SUBTREE"))
7054 ((= more 4)
7055 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7056 (org-drawer-regexp
7057 (concat "^[ \t]*:\\("
7058 (mapconcat 'regexp-quote org-drawers "\\|")
7059 "\\):[ \t]*$")))
7060 (show-subtree)
7061 (save-excursion
7062 (org-back-to-heading)
7063 (org-cycle-hide-drawers 'subtree)))
7064 (message "Remote: SUBTREE AND LOGBOOK"))
7065 ((> more 4)
7066 (show-subtree)
7067 (message "Remote: SUBTREE AND ALL DRAWERS")))
7068 (select-window win)))
7070 (defun org-recenter-heading (n)
7071 (save-excursion
7072 (org-back-to-heading)
7073 (recenter n)))
7075 (defvar org-agenda-cycle-counter nil)
7076 (defun org-agenda-cycle-show (&optional n)
7077 "Show the current entry in another window, with default settings.
7078 Default settings are taken from `org-show-hierarchy-above' and siblings.
7079 When use repeatedly in immediate succession, the remote entry will cycle
7080 through visibility
7082 children -> subtree -> folded
7084 When called with a numeric prefix arg, that arg will be passed through to
7085 `org-agenda-show-1'. For the interpretation of that argument, see the
7086 docstring of `org-agenda-show-1'."
7087 (interactive "P")
7088 (if (integerp n)
7089 (setq org-agenda-cycle-counter n)
7090 (if (not (eq last-command this-command))
7091 (setq org-agenda-cycle-counter 1)
7092 (if (equal org-agenda-cycle-counter 0)
7093 (setq org-agenda-cycle-counter 2)
7094 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7095 (if (> org-agenda-cycle-counter 3)
7096 (setq org-agenda-cycle-counter 0)))))
7097 (org-agenda-show-1 org-agenda-cycle-counter))
7099 (defun org-agenda-recenter (arg)
7100 "Display the Org-mode file which contains the item at point and recenter."
7101 (interactive "P")
7102 (let ((win (selected-window)))
7103 (org-agenda-goto t)
7104 (recenter arg)
7105 (select-window win)))
7107 (defun org-agenda-show-mouse (ev)
7108 "Display the Org-mode file which contains the item at the mouse click."
7109 (interactive "e")
7110 (mouse-set-point ev)
7111 (org-agenda-show))
7113 (defun org-agenda-check-no-diary ()
7114 "Check if the entry is a diary link and abort if yes."
7115 (if (org-get-at-bol 'org-agenda-diary-link)
7116 (org-agenda-error)))
7118 (defun org-agenda-error ()
7119 (error "Command not allowed in this line"))
7121 (defun org-agenda-tree-to-indirect-buffer ()
7122 "Show the subtree corresponding to the current entry in an indirect buffer.
7123 This calls the command `org-tree-to-indirect-buffer' from the original
7124 Org-mode buffer.
7125 With numerical prefix arg ARG, go up to this level and then take that tree.
7126 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7127 use the dedicated frame)."
7128 (interactive)
7129 (org-agenda-check-no-diary)
7130 (let* ((marker (or (org-get-at-bol 'org-marker)
7131 (org-agenda-error)))
7132 (buffer (marker-buffer marker))
7133 (pos (marker-position marker)))
7134 (with-current-buffer buffer
7135 (save-excursion
7136 (goto-char pos)
7137 (call-interactively 'org-tree-to-indirect-buffer)))))
7139 (defvar org-last-heading-marker (make-marker)
7140 "Marker pointing to the headline that last changed its TODO state
7141 by a remote command from the agenda.")
7143 (defun org-agenda-todo-nextset ()
7144 "Switch TODO entry to next sequence."
7145 (interactive)
7146 (org-agenda-todo 'nextset))
7148 (defun org-agenda-todo-previousset ()
7149 "Switch TODO entry to previous sequence."
7150 (interactive)
7151 (org-agenda-todo 'previousset))
7153 (defun org-agenda-todo (&optional arg)
7154 "Cycle TODO state of line at point, also in Org-mode file.
7155 This changes the line at point, all other lines in the agenda referring to
7156 the same tree node, and the headline of the tree node in the Org-mode file."
7157 (interactive "P")
7158 (org-agenda-check-no-diary)
7159 (let* ((col (current-column))
7160 (marker (or (org-get-at-bol 'org-marker)
7161 (org-agenda-error)))
7162 (buffer (marker-buffer marker))
7163 (pos (marker-position marker))
7164 (hdmarker (org-get-at-bol 'org-hd-marker))
7165 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7166 (inhibit-read-only t)
7167 org-agenda-headline-snapshot-before-repeat newhead just-one)
7168 (org-with-remote-undo buffer
7169 (with-current-buffer buffer
7170 (widen)
7171 (goto-char pos)
7172 (org-show-context 'agenda)
7173 (save-excursion
7174 (and (outline-next-heading)
7175 (org-flag-heading nil))) ; show the next heading
7176 (let ((current-prefix-arg arg))
7177 (call-interactively 'org-todo))
7178 (and (bolp) (forward-char 1))
7179 (setq newhead (org-get-heading))
7180 (when (and (org-bound-and-true-p
7181 org-agenda-headline-snapshot-before-repeat)
7182 (not (equal org-agenda-headline-snapshot-before-repeat
7183 newhead))
7184 todayp)
7185 (setq newhead org-agenda-headline-snapshot-before-repeat
7186 just-one t))
7187 (save-excursion
7188 (org-back-to-heading)
7189 (move-marker org-last-heading-marker (point))))
7190 (beginning-of-line 1)
7191 (save-excursion
7192 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7193 (org-move-to-column col))))
7195 (defun org-agenda-add-note (&optional arg)
7196 "Add a time-stamped note to the entry at point."
7197 (interactive "P")
7198 (org-agenda-check-no-diary)
7199 (let* ((marker (or (org-get-at-bol 'org-marker)
7200 (org-agenda-error)))
7201 (buffer (marker-buffer marker))
7202 (pos (marker-position marker))
7203 (hdmarker (org-get-at-bol 'org-hd-marker))
7204 (inhibit-read-only t))
7205 (with-current-buffer buffer
7206 (widen)
7207 (goto-char pos)
7208 (org-show-context 'agenda)
7209 (save-excursion
7210 (and (outline-next-heading)
7211 (org-flag-heading nil))) ; show the next heading
7212 (org-add-note))))
7214 (defun org-agenda-change-all-lines (newhead hdmarker
7215 &optional fixface just-this)
7216 "Change all lines in the agenda buffer which match HDMARKER.
7217 The new content of the line will be NEWHEAD (as modified by
7218 `org-format-agenda-item'). HDMARKER is checked with
7219 `equal' against all `org-hd-marker' text properties in the file.
7220 If FIXFACE is non-nil, the face of each item is modified according to
7221 the new TODO state.
7222 If JUST-THIS is non-nil, change just the current line, not all.
7223 If FORCE-TAGS is non nil, the car of it returns the new tags."
7224 (let* ((inhibit-read-only t)
7225 (line (org-current-line))
7226 (thetags (with-current-buffer (marker-buffer hdmarker)
7227 (save-excursion (save-restriction (widen)
7228 (goto-char hdmarker)
7229 (org-get-tags-at)))))
7230 props m pl undone-face done-face finish new dotime cat tags)
7231 (save-excursion
7232 (goto-char (point-max))
7233 (beginning-of-line 1)
7234 (while (not finish)
7235 (setq finish (bobp))
7236 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7237 (or (not just-this) (= (org-current-line) line))
7238 (equal m hdmarker))
7239 (setq props (text-properties-at (point))
7240 dotime (org-get-at-bol 'dotime)
7241 cat (org-get-at-bol 'org-category)
7242 tags thetags
7243 new (org-format-agenda-item (org-get-at-bol 'extra)
7244 newhead cat tags dotime)
7245 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7246 undone-face (org-get-at-bol 'undone-face)
7247 done-face (org-get-at-bol 'done-face))
7248 (beginning-of-line 1)
7249 (cond
7250 ((equal new "")
7251 (and (looking-at ".*\n?") (replace-match "")))
7252 ((looking-at ".*")
7253 (replace-match new t t)
7254 (beginning-of-line 1)
7255 (add-text-properties (point-at-bol) (point-at-eol) props)
7256 (when fixface
7257 (add-text-properties
7258 (point-at-bol) (point-at-eol)
7259 (list 'face
7260 (if org-last-todo-state-is-todo
7261 undone-face done-face))))
7262 (org-agenda-highlight-todo 'line)
7263 (beginning-of-line 1))
7264 (t (error "Line update did not work"))))
7265 (beginning-of-line 0)))
7266 (org-finalize-agenda)))
7268 (defun org-agenda-align-tags (&optional line)
7269 "Align all tags in agenda items to `org-agenda-tags-column'."
7270 (let ((inhibit-read-only t) l c)
7271 (save-excursion
7272 (goto-char (if line (point-at-bol) (point-min)))
7273 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7274 (if line (point-at-eol) nil) t)
7275 (add-text-properties
7276 (match-beginning 2) (match-end 2)
7277 (list 'face (delq nil (let ((prop (get-text-property
7278 (match-beginning 2) 'face)))
7279 (or (listp prop) (setq prop (list prop)))
7280 (if (memq 'org-tag prop)
7281 prop
7282 (cons 'org-tag prop))))))
7283 (setq l (- (match-end 2) (match-beginning 2))
7284 c (if (< org-agenda-tags-column 0)
7285 (- (abs org-agenda-tags-column) l)
7286 org-agenda-tags-column))
7287 (delete-region (match-beginning 1) (match-end 1))
7288 (goto-char (match-beginning 1))
7289 (insert (org-add-props
7290 (make-string (max 1 (- c (current-column))) ?\ )
7291 (plist-put (copy-sequence (text-properties-at (point)))
7292 'face nil))))
7293 (goto-char (point-min))
7294 (org-font-lock-add-tag-faces (point-max)))))
7296 (defun org-agenda-priority-up ()
7297 "Increase the priority of line at point, also in Org-mode file."
7298 (interactive)
7299 (org-agenda-priority 'up))
7301 (defun org-agenda-priority-down ()
7302 "Decrease the priority of line at point, also in Org-mode file."
7303 (interactive)
7304 (org-agenda-priority 'down))
7306 (defun org-agenda-priority (&optional force-direction)
7307 "Set the priority of line at point, also in Org-mode file.
7308 This changes the line at point, all other lines in the agenda referring to
7309 the same tree node, and the headline of the tree node in the Org-mode file."
7310 (interactive)
7311 (unless org-enable-priority-commands
7312 (error "Priority commands are disabled"))
7313 (org-agenda-check-no-diary)
7314 (let* ((marker (or (org-get-at-bol 'org-marker)
7315 (org-agenda-error)))
7316 (hdmarker (org-get-at-bol 'org-hd-marker))
7317 (buffer (marker-buffer hdmarker))
7318 (pos (marker-position hdmarker))
7319 (inhibit-read-only t)
7320 newhead)
7321 (org-with-remote-undo buffer
7322 (with-current-buffer buffer
7323 (widen)
7324 (goto-char pos)
7325 (org-show-context 'agenda)
7326 (save-excursion
7327 (and (outline-next-heading)
7328 (org-flag-heading nil))) ; show the next heading
7329 (funcall 'org-priority force-direction)
7330 (end-of-line 1)
7331 (setq newhead (org-get-heading)))
7332 (org-agenda-change-all-lines newhead hdmarker)
7333 (beginning-of-line 1))))
7335 ;; FIXME: should fix the tags property of the agenda line.
7336 (defun org-agenda-set-tags (&optional tag onoff)
7337 "Set tags for the current headline."
7338 (interactive)
7339 (org-agenda-check-no-diary)
7340 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7341 (call-interactively 'org-change-tag-in-region)
7342 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7343 (org-agenda-error)))
7344 (buffer (marker-buffer hdmarker))
7345 (pos (marker-position hdmarker))
7346 (inhibit-read-only t)
7347 newhead)
7348 (org-with-remote-undo buffer
7349 (with-current-buffer buffer
7350 (widen)
7351 (goto-char pos)
7352 (save-excursion
7353 (org-show-context 'agenda))
7354 (save-excursion
7355 (and (outline-next-heading)
7356 (org-flag-heading nil))) ; show the next heading
7357 (goto-char pos)
7358 (if tag
7359 (org-toggle-tag tag onoff)
7360 (call-interactively 'org-set-tags))
7361 (end-of-line 1)
7362 (setq newhead (org-get-heading)))
7363 (org-agenda-change-all-lines newhead hdmarker)
7364 (beginning-of-line 1)))))
7366 (defun org-agenda-set-property ()
7367 "Set a property for the current headline."
7368 (interactive)
7369 (org-agenda-check-no-diary)
7370 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7371 (org-agenda-error)))
7372 (buffer (marker-buffer hdmarker))
7373 (pos (marker-position hdmarker))
7374 (inhibit-read-only t)
7375 newhead)
7376 (org-with-remote-undo buffer
7377 (with-current-buffer buffer
7378 (widen)
7379 (goto-char pos)
7380 (save-excursion
7381 (org-show-context 'agenda))
7382 (save-excursion
7383 (and (outline-next-heading)
7384 (org-flag-heading nil))) ; show the next heading
7385 (goto-char pos)
7386 (call-interactively 'org-set-property)))))
7388 (defun org-agenda-set-effort ()
7389 "Set the effort property for the current headline."
7390 (interactive)
7391 (org-agenda-check-no-diary)
7392 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7393 (org-agenda-error)))
7394 (buffer (marker-buffer hdmarker))
7395 (pos (marker-position hdmarker))
7396 (inhibit-read-only t)
7397 newhead)
7398 (org-with-remote-undo buffer
7399 (with-current-buffer buffer
7400 (widen)
7401 (goto-char pos)
7402 (save-excursion
7403 (org-show-context 'agenda))
7404 (save-excursion
7405 (and (outline-next-heading)
7406 (org-flag-heading nil))) ; show the next heading
7407 (goto-char pos)
7408 (call-interactively 'org-set-effort)
7409 (end-of-line 1)))))
7411 (defun org-agenda-toggle-archive-tag ()
7412 "Toggle the archive tag for the current entry."
7413 (interactive)
7414 (org-agenda-check-no-diary)
7415 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7416 (org-agenda-error)))
7417 (buffer (marker-buffer hdmarker))
7418 (pos (marker-position hdmarker))
7419 (inhibit-read-only t)
7420 newhead)
7421 (org-with-remote-undo buffer
7422 (with-current-buffer buffer
7423 (widen)
7424 (goto-char pos)
7425 (org-show-context 'agenda)
7426 (save-excursion
7427 (and (outline-next-heading)
7428 (org-flag-heading nil))) ; show the next heading
7429 (call-interactively 'org-toggle-archive-tag)
7430 (end-of-line 1)
7431 (setq newhead (org-get-heading)))
7432 (org-agenda-change-all-lines newhead hdmarker)
7433 (beginning-of-line 1))))
7435 (defun org-agenda-do-date-later (arg)
7436 (interactive "P")
7437 (cond
7438 ((or (equal arg '(16))
7439 (memq last-command
7440 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7441 (setq this-command 'org-agenda-date-later-minutes)
7442 (org-agenda-date-later-minutes 1))
7443 ((or (equal arg '(4))
7444 (memq last-command
7445 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7446 (setq this-command 'org-agenda-date-later-hours)
7447 (org-agenda-date-later-hours 1))
7449 (org-agenda-date-later (prefix-numeric-value arg)))))
7451 (defun org-agenda-do-date-earlier (arg)
7452 (interactive "P")
7453 (cond
7454 ((or (equal arg '(16))
7455 (memq last-command
7456 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7457 (setq this-command 'org-agenda-date-earlier-minutes)
7458 (org-agenda-date-earlier-minutes 1))
7459 ((or (equal arg '(4))
7460 (memq last-command
7461 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7462 (setq this-command 'org-agenda-date-earlier-hours)
7463 (org-agenda-date-earlier-hours 1))
7465 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7467 (defun org-agenda-date-later (arg &optional what)
7468 "Change the date of this item to ARG day(s) later."
7469 (interactive "p")
7470 (org-agenda-check-type t 'agenda 'timeline)
7471 (org-agenda-check-no-diary)
7472 (let* ((marker (or (org-get-at-bol 'org-marker)
7473 (org-agenda-error)))
7474 (buffer (marker-buffer marker))
7475 (pos (marker-position marker)))
7476 (org-with-remote-undo buffer
7477 (with-current-buffer buffer
7478 (widen)
7479 (goto-char pos)
7480 (if (not (org-at-timestamp-p))
7481 (error "Cannot find time stamp"))
7482 (org-timestamp-change arg (or what 'day)))
7483 (org-agenda-show-new-time marker org-last-changed-timestamp))
7484 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7486 (defun org-agenda-date-earlier (arg &optional what)
7487 "Change the date of this item to ARG day(s) earlier."
7488 (interactive "p")
7489 (org-agenda-date-later (- arg) what))
7491 (defun org-agenda-date-later-minutes (arg)
7492 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7493 (interactive "p")
7494 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7495 (org-agenda-date-later arg 'minute))
7497 (defun org-agenda-date-earlier-minutes (arg)
7498 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7499 (interactive "p")
7500 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7501 (org-agenda-date-earlier arg 'minute))
7503 (defun org-agenda-date-later-hours (arg)
7504 "Change the time of this item, in hour steps."
7505 (interactive "p")
7506 (org-agenda-date-later arg 'hour))
7508 (defun org-agenda-date-earlier-hours (arg)
7509 "Change the time of this item, in hour steps."
7510 (interactive "p")
7511 (org-agenda-date-earlier arg 'hour))
7513 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7514 "Show new date stamp via text properties."
7515 ;; We use text properties to make this undoable
7516 (let ((inhibit-read-only t)
7517 (buffer-invisibility-spec))
7518 (setq stamp (concat " " prefix " => " stamp))
7519 (save-excursion
7520 (goto-char (point-max))
7521 (while (not (bobp))
7522 (when (equal marker (org-get-at-bol 'org-marker))
7523 (org-move-to-column (- (window-width) (length stamp)) t)
7524 (org-agenda-fix-tags-filter-overlays-at (point))
7525 (if (featurep 'xemacs)
7526 ;; Use `duplicable' property to trigger undo recording
7527 (let ((ex (make-extent nil nil))
7528 (gl (make-glyph stamp)))
7529 (set-glyph-face gl 'secondary-selection)
7530 (set-extent-properties
7531 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7532 (insert-extent ex (1- (point)) (point-at-eol)))
7533 (add-text-properties
7534 (1- (point)) (point-at-eol)
7535 (list 'display (org-add-props stamp nil
7536 'face 'secondary-selection))))
7537 (beginning-of-line 1))
7538 (beginning-of-line 0)))))
7540 (defun org-agenda-date-prompt (arg)
7541 "Change the date of this item. Date is prompted for, with default today.
7542 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7543 be used to request time specification in the time stamp."
7544 (interactive "P")
7545 (org-agenda-check-type t 'agenda 'timeline)
7546 (org-agenda-check-no-diary)
7547 (let* ((marker (or (org-get-at-bol 'org-marker)
7548 (org-agenda-error)))
7549 (buffer (marker-buffer marker))
7550 (pos (marker-position marker)))
7551 (org-with-remote-undo buffer
7552 (with-current-buffer buffer
7553 (widen)
7554 (goto-char pos)
7555 (if (not (org-at-timestamp-p t))
7556 (error "Cannot find time stamp"))
7557 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7558 (org-agenda-show-new-time marker org-last-changed-timestamp))
7559 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7561 (defun org-agenda-schedule (arg &optional time)
7562 "Schedule the item at point.
7563 Arg is passed through to `org-schedule'."
7564 (interactive "P")
7565 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7566 (org-agenda-check-no-diary)
7567 (let* ((marker (or (org-get-at-bol 'org-marker)
7568 (org-agenda-error)))
7569 (type (marker-insertion-type marker))
7570 (buffer (marker-buffer marker))
7571 (pos (marker-position marker))
7572 (org-insert-labeled-timestamps-at-point nil)
7574 (set-marker-insertion-type marker t)
7575 (org-with-remote-undo buffer
7576 (with-current-buffer buffer
7577 (widen)
7578 (goto-char pos)
7579 (setq ts (org-schedule arg time)))
7580 (org-agenda-show-new-time marker ts "S"))
7581 (message "Item scheduled for %s" ts)))
7583 (defun org-agenda-deadline (arg &optional time)
7584 "Schedule the item at point.
7585 Arg is passed through to `org-deadline'."
7586 (interactive "P")
7587 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7588 (org-agenda-check-no-diary)
7589 (let* ((marker (or (org-get-at-bol 'org-marker)
7590 (org-agenda-error)))
7591 (buffer (marker-buffer marker))
7592 (pos (marker-position marker))
7593 (org-insert-labeled-timestamps-at-point nil)
7595 (org-with-remote-undo buffer
7596 (with-current-buffer buffer
7597 (widen)
7598 (goto-char pos)
7599 (setq ts (org-deadline arg time)))
7600 (org-agenda-show-new-time marker ts "D"))
7601 (message "Deadline for this item set to %s" ts)))
7603 (defun org-agenda-action ()
7604 "Select entry for agenda action, or execute an agenda action.
7605 This command prompts for another letter. Valid inputs are:
7607 m Mark the entry at point for an agenda action
7608 s Schedule the marked entry to the date at the cursor
7609 d Set the deadline of the marked entry to the date at the cursor
7610 r Call `org-remember' with cursor date as the default date
7611 c Call `org-capture' with cursor date as the default date
7612 SPC Show marked entry in other window
7613 TAB Visit marked entry in other window
7615 The cursor may be at a date in the calendar, or in the Org agenda."
7616 (interactive)
7617 (let (ans)
7618 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7619 (setq ans (read-char-exclusive))
7620 (cond
7621 ((equal ans ?m)
7622 ;; Mark this entry
7623 (if (eq major-mode 'org-agenda-mode)
7624 (let ((m (or (org-get-at-bol 'org-hd-marker)
7625 (org-get-at-bol 'org-marker))))
7626 (if m
7627 (progn
7628 (move-marker org-agenda-action-marker
7629 (marker-position m) (marker-buffer m))
7630 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7631 (error "Don't know which entry to mark")))
7632 (error "This command works only in the agenda")))
7633 ((equal ans ?s)
7634 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7635 ((equal ans ?d)
7636 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7637 ((equal ans ?r)
7638 (org-agenda-do-action '(org-remember) t))
7639 ((equal ans ?c)
7640 (org-agenda-do-action '(org-capture) t))
7641 ((equal ans ?\ )
7642 (let ((cw (selected-window)))
7643 (org-switch-to-buffer-other-window
7644 (marker-buffer org-agenda-action-marker))
7645 (goto-char org-agenda-action-marker)
7646 (org-show-context 'agenda)
7647 (select-window cw)))
7648 ((equal ans ?\C-i)
7649 (org-switch-to-buffer-other-window
7650 (marker-buffer org-agenda-action-marker))
7651 (goto-char org-agenda-action-marker)
7652 (org-show-context 'agenda))
7653 (t (error "Invalid agenda action %c" ans)))))
7655 (defun org-agenda-do-action (form &optional current-buffer)
7656 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7657 (let ((org-overriding-default-time (org-get-cursor-date)))
7658 (if current-buffer
7659 (eval form)
7660 (if (not (marker-buffer org-agenda-action-marker))
7661 (error "No entry has been selected for agenda action")
7662 (with-current-buffer (marker-buffer org-agenda-action-marker)
7663 (save-excursion
7664 (save-restriction
7665 (widen)
7666 (goto-char org-agenda-action-marker)
7667 (eval form))))))))
7669 (defun org-agenda-clock-in (&optional arg)
7670 "Start the clock on the currently selected item."
7671 (interactive "P")
7672 (org-agenda-check-no-diary)
7673 (if (equal arg '(4))
7674 (org-clock-in arg)
7675 (let* ((marker (or (org-get-at-bol 'org-marker)
7676 (org-agenda-error)))
7677 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7678 marker))
7679 (pos (marker-position marker))
7680 newhead)
7681 (org-with-remote-undo (marker-buffer marker)
7682 (with-current-buffer (marker-buffer marker)
7683 (widen)
7684 (goto-char pos)
7685 (org-show-context 'agenda)
7686 (org-show-entry)
7687 (org-cycle-hide-drawers 'children)
7688 (org-clock-in arg)
7689 (setq newhead (org-get-heading)))
7690 (org-agenda-change-all-lines newhead hdmarker)))))
7692 (defun org-agenda-clock-out ()
7693 "Stop the currently running clock."
7694 (interactive)
7695 (unless (marker-buffer org-clock-marker)
7696 (error "No running clock"))
7697 (let ((marker (make-marker)) newhead)
7698 (org-with-remote-undo (marker-buffer org-clock-marker)
7699 (with-current-buffer (marker-buffer org-clock-marker)
7700 (save-excursion
7701 (save-restriction
7702 (widen)
7703 (goto-char org-clock-marker)
7704 (org-back-to-heading t)
7705 (move-marker marker (point))
7706 (org-clock-out)
7707 (setq newhead (org-get-heading))))))
7708 (org-agenda-change-all-lines newhead marker)
7709 (move-marker marker nil)))
7711 (defun org-agenda-clock-cancel (&optional arg)
7712 "Cancel the currently running clock."
7713 (interactive "P")
7714 (unless (marker-buffer org-clock-marker)
7715 (error "No running clock"))
7716 (org-with-remote-undo (marker-buffer org-clock-marker)
7717 (org-clock-cancel)))
7719 (defun org-agenda-clock-goto ()
7720 "Jump to the currently clocked in task within the agenda.
7721 If the currently clocked in task is not listed in the agenda
7722 buffer, display it in another window."
7723 (interactive)
7724 (let (pos)
7725 (mapc (lambda (o)
7726 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7727 (setq pos (overlay-start o))))
7728 (overlays-in (point-min) (point-max)))
7729 (cond (pos (goto-char pos))
7730 ;; If the currently clocked entry is not in the agenda
7731 ;; buffer, we visit it in another window:
7732 (org-clock-current-task
7733 (org-switch-to-buffer-other-window (org-clock-goto)))
7734 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7736 (defun org-agenda-diary-entry-in-org-file ()
7737 "Make a diary entry in the file `org-agenda-diary-file'."
7738 (let (d1 d2 char (text "") dp1 dp2)
7739 (if (equal (buffer-name) "*Calendar*")
7740 (setq d1 (calendar-cursor-to-date t)
7741 d2 (car calendar-mark-ring))
7742 (setq dp1 (get-text-property (point-at-bol) 'day))
7743 (unless dp1 (error "No date defined in current line"))
7744 (setq d1 (calendar-gregorian-from-absolute dp1)
7745 d2 (and (ignore-errors (mark))
7746 (save-excursion
7747 (goto-char (mark))
7748 (setq dp2 (get-text-property (point-at-bol) 'day)))
7749 (calendar-gregorian-from-absolute dp2))))
7750 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7751 (setq char (read-char-exclusive))
7752 (cond
7753 ((equal char ?d)
7754 (setq text (read-string "Day entry: "))
7755 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7756 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7757 ((equal char ?a)
7758 (setq d1 (list (car d1) (nth 1 d1)
7759 (read-number (format "Reference year [%d]: " (nth 2 d1))
7760 (nth 2 d1))))
7761 (setq text (read-string "Anniversary (use %d to show years): "))
7762 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7763 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7764 ((equal char ?b)
7765 (setq text (read-string "Block entry: "))
7766 (unless (and d1 d2 (not (equal d1 d2)))
7767 (error "No block of days selected"))
7768 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7769 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7770 ((equal char ?j)
7771 (org-switch-to-buffer-other-window
7772 (find-file-noselect org-agenda-diary-file))
7773 (require 'org-datetree)
7774 (org-datetree-find-date-create d1)
7775 (org-reveal t))
7776 (t (error "Invalid selection character `%c'" char)))))
7778 (defcustom org-agenda-insert-diary-strategy 'date-tree
7779 "Where in `org-agenda-diary-file' should new entries be added?
7780 Valid values:
7782 date-tree in the date tree, as child of the date
7783 top-level as top-level entries at the end of the file."
7784 :group 'org-agenda
7785 :type '(choice
7786 (const :tag "in a date tree" date-tree)
7787 (const :tag "as top level at end of file" top-level)))
7789 (defcustom org-agenda-insert-diary-extract-time nil
7790 "Non-nil means extract any time specification from the diary entry."
7791 :group 'org-agenda
7792 :type 'boolean)
7794 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7795 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7796 If TEXT is not empty, it will become the headline of the new entry, and
7797 the resulting entry will not be shown. When TEXT is empty, switch to
7798 `org-agenda-diary-file' and let the user finish the entry there."
7799 (let ((cw (current-window-configuration)))
7800 (org-switch-to-buffer-other-window
7801 (find-file-noselect org-agenda-diary-file))
7802 (widen)
7803 (goto-char (point-min))
7804 (cond
7805 ((eq type 'anniversary)
7806 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7807 (progn
7808 (or (org-on-heading-p t)
7809 (progn
7810 (outline-next-heading)
7811 (insert "* Anniversaries\n\n")
7812 (beginning-of-line -1)))))
7813 (outline-next-heading)
7814 (org-back-over-empty-lines)
7815 (backward-char 1)
7816 (insert "\n")
7817 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7818 (nth 2 d1) (car d1) (nth 1 d1) text)))
7819 ((eq type 'day)
7820 (let ((org-prefix-has-time t)
7821 (org-agenda-time-leading-zero t)
7822 fmt time time2)
7823 (if org-agenda-insert-diary-extract-time
7824 ;; Use org-format-agenda-item to parse text for a time-range and
7825 ;; remove it. FIXME: This is a hack, we should refactor
7826 ;; that function to make time extraction available separately
7827 (setq fmt (org-format-agenda-item nil text nil nil t)
7828 time (get-text-property 0 'time fmt)
7829 time2 (if (> (length time) 0)
7830 ;; split-string removes trailing ...... if
7831 ;; no end time given. First space
7832 ;; separates time from date.
7833 (concat " " (car (split-string time "\\.")))
7834 nil)
7835 text (get-text-property 0 'txt fmt)))
7836 (if (eq org-agenda-insert-diary-strategy 'top-level)
7837 (org-agenda-insert-diary-as-top-level text)
7838 (require 'org-datetree)
7839 (org-datetree-find-date-create d1)
7840 (org-agenda-insert-diary-make-new-entry text))
7841 (org-insert-time-stamp (org-time-from-absolute
7842 (calendar-absolute-from-gregorian d1))
7843 nil nil nil nil time2))
7844 (end-of-line 0))
7845 ((eq type 'block)
7846 (if (> (calendar-absolute-from-gregorian d1)
7847 (calendar-absolute-from-gregorian d2))
7848 (setq d1 (prog1 d2 (setq d2 d1))))
7849 (if (eq org-agenda-insert-diary-strategy 'top-level)
7850 (org-agenda-insert-diary-as-top-level text)
7851 (require 'org-datetree)
7852 (org-datetree-find-date-create d1)
7853 (org-agenda-insert-diary-make-new-entry text))
7854 (org-insert-time-stamp (org-time-from-absolute
7855 (calendar-absolute-from-gregorian d1)))
7856 (insert "--")
7857 (org-insert-time-stamp (org-time-from-absolute
7858 (calendar-absolute-from-gregorian d2)))
7859 (end-of-line 0)))
7860 (if (string-match "\\S-" text)
7861 (progn
7862 (set-window-configuration cw)
7863 (message "%s entry added to %s"
7864 (capitalize (symbol-name type))
7865 (abbreviate-file-name org-agenda-diary-file)))
7866 (org-reveal t)
7867 (message "Please finish entry here"))))
7869 (defun org-agenda-insert-diary-as-top-level (text)
7870 "Make new entry as a top-level entry at the end of the file.
7871 Add TEXT as headline, and position the cursor in the second line so that
7872 a timestamp can be added there."
7873 (widen)
7874 (goto-char (point-max))
7875 (or (bolp) (insert "\n"))
7876 (insert "* " text "\n")
7877 (if org-adapt-indentation (org-indent-to-column 2)))
7879 (defun org-agenda-insert-diary-make-new-entry (text)
7880 "Make new entry as last child of current entry.
7881 Add TEXT as headline, and position the cursor in the second line so that
7882 a timestamp can be added there."
7883 (let ((org-show-following-heading t)
7884 (org-show-siblings t)
7885 (org-show-hierarchy-above t)
7886 (org-show-entry-below t)
7887 col)
7888 (outline-next-heading)
7889 (org-back-over-empty-lines)
7890 (or (looking-at "[ \t]*$")
7891 (progn (insert "\n") (backward-char 1)))
7892 (org-insert-heading nil t)
7893 (org-do-demote)
7894 (setq col (current-column))
7895 (insert text "\n")
7896 (if org-adapt-indentation (org-indent-to-column col))
7897 (let ((org-show-following-heading t)
7898 (org-show-siblings t)
7899 (org-show-hierarchy-above t)
7900 (org-show-entry-below t))
7901 (org-show-context))))
7903 (defun org-agenda-diary-entry ()
7904 "Make a diary entry, like the `i' command from the calendar.
7905 All the standard commands work: block, weekly etc.
7906 When `org-agenda-diary-file' points to a file,
7907 `org-agenda-diary-entry-in-org-file' is called instead to create
7908 entries in that Org-mode file."
7909 (interactive)
7910 (org-agenda-check-type t 'agenda 'timeline)
7911 (if (not (eq org-agenda-diary-file 'diary-file))
7912 (org-agenda-diary-entry-in-org-file)
7913 (require 'diary-lib)
7914 (let* ((char (progn
7915 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7916 (read-char-exclusive)))
7917 (cmd (cdr (assoc char
7918 '((?d . insert-diary-entry)
7919 (?w . insert-weekly-diary-entry)
7920 (?m . insert-monthly-diary-entry)
7921 (?y . insert-yearly-diary-entry)
7922 (?a . insert-anniversary-diary-entry)
7923 (?b . insert-block-diary-entry)
7924 (?c . insert-cyclic-diary-entry)))))
7925 (oldf (symbol-function 'calendar-cursor-to-date))
7926 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7927 (point (point))
7928 (mark (or (mark t) (point))))
7929 (unless cmd
7930 (error "No command associated with <%c>" char))
7931 (unless (and (get-text-property point 'day)
7932 (or (not (equal ?b char))
7933 (get-text-property mark 'day)))
7934 (error "Don't know which date to use for diary entry"))
7935 ;; We implement this by hacking the `calendar-cursor-to-date' function
7936 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7937 (let ((calendar-mark-ring
7938 (list (calendar-gregorian-from-absolute
7939 (or (get-text-property mark 'day)
7940 (get-text-property point 'day))))))
7941 (unwind-protect
7942 (progn
7943 (fset 'calendar-cursor-to-date
7944 (lambda (&optional error dummy)
7945 (calendar-gregorian-from-absolute
7946 (get-text-property point 'day))))
7947 (call-interactively cmd))
7948 (fset 'calendar-cursor-to-date oldf))))))
7950 (defun org-agenda-execute-calendar-command (cmd)
7951 "Execute a calendar command from the agenda, with the date associated to
7952 the cursor position."
7953 (org-agenda-check-type t 'agenda 'timeline)
7954 (require 'diary-lib)
7955 (unless (get-text-property (point) 'day)
7956 (error "Don't know which date to use for calendar command"))
7957 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7958 (point (point))
7959 (date (calendar-gregorian-from-absolute
7960 (get-text-property point 'day)))
7961 ;; the following 2 vars are needed in the calendar
7962 (displayed-month (car date))
7963 (displayed-year (nth 2 date)))
7964 (unwind-protect
7965 (progn
7966 (fset 'calendar-cursor-to-date
7967 (lambda (&optional error dummy)
7968 (calendar-gregorian-from-absolute
7969 (get-text-property point 'day))))
7970 (call-interactively cmd))
7971 (fset 'calendar-cursor-to-date oldf))))
7973 (defun org-agenda-phases-of-moon ()
7974 "Display the phases of the moon for the 3 months around the cursor date."
7975 (interactive)
7976 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7978 (defun org-agenda-holidays ()
7979 "Display the holidays for the 3 months around the cursor date."
7980 (interactive)
7981 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7983 (defvar calendar-longitude)
7984 (defvar calendar-latitude)
7985 (defvar calendar-location-name)
7987 (defun org-agenda-sunrise-sunset (arg)
7988 "Display sunrise and sunset for the cursor date.
7989 Latitude and longitude can be specified with the variables
7990 `calendar-latitude' and `calendar-longitude'. When called with prefix
7991 argument, latitude and longitude will be prompted for."
7992 (interactive "P")
7993 (require 'solar)
7994 (let ((calendar-longitude (if arg nil calendar-longitude))
7995 (calendar-latitude (if arg nil calendar-latitude))
7996 (calendar-location-name
7997 (if arg "the given coordinates" calendar-location-name)))
7998 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8000 (defun org-agenda-goto-calendar ()
8001 "Open the Emacs calendar with the date at the cursor."
8002 (interactive)
8003 (org-agenda-check-type t 'agenda 'timeline)
8004 (let* ((day (or (get-text-property (point) 'day)
8005 (error "Don't know which date to open in calendar")))
8006 (date (calendar-gregorian-from-absolute day))
8007 (calendar-move-hook nil)
8008 (calendar-view-holidays-initially-flag nil)
8009 (calendar-view-diary-initially-flag nil))
8010 (calendar)
8011 (calendar-goto-date date)))
8013 ;;;###autoload
8014 (defun org-calendar-goto-agenda ()
8015 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8016 This is a command that has to be installed in `calendar-mode-map'."
8017 (interactive)
8018 (org-agenda-list nil (calendar-absolute-from-gregorian
8019 (calendar-cursor-to-date))
8020 nil))
8022 (defun org-agenda-convert-date ()
8023 (interactive)
8024 (org-agenda-check-type t 'agenda 'timeline)
8025 (let ((day (get-text-property (point) 'day))
8026 date s)
8027 (unless day
8028 (error "Don't know which date to convert"))
8029 (setq date (calendar-gregorian-from-absolute day))
8030 (setq s (concat
8031 "Gregorian: " (calendar-date-string date) "\n"
8032 "ISO: " (calendar-iso-date-string date) "\n"
8033 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8034 "Julian: " (calendar-julian-date-string date) "\n"
8035 "Astron. JD: " (calendar-astro-date-string date)
8036 " (Julian date number at noon UTC)\n"
8037 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8038 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8039 "French: " (calendar-french-date-string date) "\n"
8040 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8041 "Mayan: " (calendar-mayan-date-string date) "\n"
8042 "Coptic: " (calendar-coptic-date-string date) "\n"
8043 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8044 "Persian: " (calendar-persian-date-string date) "\n"
8045 "Chinese: " (calendar-chinese-date-string date) "\n"))
8046 (with-output-to-temp-buffer "*Dates*"
8047 (princ s))
8048 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8050 ;;; Bulk commands
8052 (defvar org-agenda-bulk-marked-entries nil
8053 "List of markers that refer to marked entries in the agenda.")
8055 (defun org-agenda-bulk-marked-p ()
8056 (eq (get-char-property (point-at-bol) 'type)
8057 'org-marked-entry-overlay))
8059 (defun org-agenda-bulk-mark (&optional arg)
8060 "Mark the entry at point for future bulk action."
8061 (interactive "p")
8062 (dotimes (i (max arg 1))
8063 (unless (org-get-at-bol 'org-agenda-diary-link)
8064 (let* ((m (org-get-at-bol 'org-hd-marker))
8066 (unless (org-agenda-bulk-marked-p)
8067 (unless m (error "Nothing to mark at point"))
8068 (push m org-agenda-bulk-marked-entries)
8069 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8070 (org-overlay-display ov "> "
8071 (org-get-todo-face "TODO")
8072 'evaporate)
8073 (overlay-put ov 'type 'org-marked-entry-overlay))
8074 (beginning-of-line 2)
8075 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8076 (beginning-of-line 2))
8077 (message "%d entries marked for bulk action"
8078 (length org-agenda-bulk-marked-entries))))))
8080 (defun org-agenda-bulk-mark-regexp (regexp)
8081 "Mark entries match REGEXP."
8082 (interactive "sMark entries matching regexp: ")
8083 (let (entries-marked)
8084 (save-excursion
8085 (goto-char (point-min))
8086 (goto-char (next-single-property-change (point) 'txt))
8087 (while (re-search-forward regexp nil t)
8088 (when (string-match regexp (get-text-property (point) 'txt))
8089 (setq entries-marked (+ entries-marked 1))
8090 (call-interactively 'org-agenda-bulk-mark))))
8091 (if (not entries-marked)
8092 (message "No entry matching this regexp."))))
8094 (defun org-agenda-bulk-unmark ()
8095 "Unmark the entry at point for future bulk action."
8096 (interactive)
8097 (when (org-agenda-bulk-marked-p)
8098 (org-agenda-bulk-remove-overlays
8099 (point-at-bol) (+ 2 (point-at-bol)))
8100 (setq org-agenda-bulk-marked-entries
8101 (delete (org-get-at-bol 'org-hd-marker)
8102 org-agenda-bulk-marked-entries)))
8103 (beginning-of-line 2)
8104 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8105 (beginning-of-line 2))
8106 (message "%d entries marked for bulk action"
8107 (length org-agenda-bulk-marked-entries)))
8109 (defun org-agenda-bulk-toggle ()
8110 "Toggle marking the entry at point for bulk action."
8111 (interactive)
8112 (if (org-agenda-bulk-marked-p)
8113 (org-agenda-bulk-unmark)
8114 (org-agenda-bulk-mark)))
8116 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8117 "Remove the mark overlays between BEG and END in the agenda buffer.
8118 BEG and END default to the buffer limits.
8120 This only removes the overlays, it does not remove the markers
8121 from the list in `org-agenda-bulk-marked-entries'."
8122 (interactive)
8123 (mapc (lambda (ov)
8124 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8125 (delete-overlay ov)))
8126 (overlays-in (or beg (point-min)) (or end (point-max)))))
8128 (defun org-agenda-bulk-remove-all-marks ()
8129 "Remove all marks in the agenda buffer.
8130 This will remove the markers, and the overlays."
8131 (interactive)
8132 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8133 (setq org-agenda-bulk-marked-entries nil)
8134 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8136 (defun org-agenda-bulk-action (&optional arg)
8137 "Execute an remote-editing action on all marked entries.
8138 The prefix arg is passed through to the command if possible."
8139 (interactive "P")
8140 ;; Make sure we have markers, and only valid ones
8141 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8142 (mapc
8143 (lambda (m)
8144 (unless (and (markerp m)
8145 (marker-buffer m)
8146 (buffer-live-p (marker-buffer m))
8147 (marker-position m))
8148 (error "Marker %s for bulk command is invalid" m)))
8149 org-agenda-bulk-marked-entries)
8151 ;; Prompt for the bulk command
8152 (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline [f]unction")
8153 (let* ((action (read-char-exclusive))
8154 (org-log-refile (if org-log-refile 'time nil))
8155 (entries (reverse org-agenda-bulk-marked-entries))
8156 redo-at-end
8157 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8158 (cond
8159 ((equal action ?$)
8160 (setq cmd '(org-agenda-archive)))
8162 ((equal action ?A)
8163 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8165 ((member action '(?r ?w))
8166 (setq rfloc (org-refile-get-location
8167 "Refile to"
8168 (marker-buffer (car org-agenda-bulk-marked-entries))
8169 org-refile-allow-creating-parent-nodes))
8170 (if (nth 3 rfloc)
8171 (setcar (nthcdr 3 rfloc)
8172 (move-marker (make-marker) (nth 3 rfloc)
8173 (or (get-file-buffer (nth 1 rfloc))
8174 (find-buffer-visiting (nth 1 rfloc))
8175 (error "This should not happen")))))
8177 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8178 redo-at-end t))
8180 ((equal action ?t)
8181 (setq state (org-icompleting-read
8182 "Todo state: "
8183 (with-current-buffer (marker-buffer (car entries))
8184 (mapcar 'list org-todo-keywords-1))))
8185 (setq cmd `(let ((org-inhibit-blocking t)
8186 (org-inhibit-logging 'note))
8187 (org-agenda-todo ,state))))
8189 ((memq action '(?- ?+))
8190 (setq tag (org-icompleting-read
8191 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8192 (with-current-buffer (marker-buffer (car entries))
8193 (delq nil
8194 (mapcar (lambda (x)
8195 (if (stringp (car x)) x)) org-tag-alist)))))
8196 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8198 ((memq action '(?s ?d))
8199 (let* ((date (unless arg
8200 (org-read-date
8201 nil nil nil
8202 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8203 (ans (if arg nil org-read-date-final-answer))
8204 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8205 (setq cmd `(let* ((bound (fboundp 'read-string))
8206 (old (and bound (symbol-function 'read-string))))
8207 (unwind-protect
8208 (progn
8209 (fset 'read-string (lambda (&rest ignore) ,ans))
8210 (eval '(,c1 arg)))
8211 (if bound
8212 (fset 'read-string old)
8213 (fmakunbound 'read-string)))))))
8215 ((equal action ?S)
8216 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8217 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8218 (let ((days (read-number
8219 (format "Scatter tasks across how many %sdays: "
8220 (if arg "week" "")) 7)))
8221 (setq cmd
8222 `(let ((distance (1+ (random ,days))))
8223 (if arg
8224 (let ((dist distance)
8225 (day-of-week
8226 (calendar-day-of-week
8227 (calendar-gregorian-from-absolute (org-today)))))
8228 (dotimes (i (1+ dist))
8229 (while (member day-of-week org-agenda-weekend-days)
8230 (incf distance)
8231 (incf day-of-week)
8232 (if (= day-of-week 7)
8233 (setq day-of-week 0)))
8234 (incf day-of-week)
8235 (if (= day-of-week 7)
8236 (setq day-of-week 0)))))
8237 ;; silently fail when try to replan a sexp entry
8238 (condition-case nil
8239 (let* ((date (calendar-gregorian-from-absolute
8240 (+ (org-today) distance)))
8241 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8242 (nth 2 date))))
8243 (org-agenda-schedule nil time))
8244 (error nil)))))))
8246 ((equal action ?f)
8247 (setq cmd (list (intern
8248 (org-icompleting-read "Function: "
8249 obarray 'fboundp t nil nil)))))
8251 (t (error "Invalid bulk action")))
8253 ;; Sort the markers, to make sure that parents are handled before children
8254 (setq entries (sort entries
8255 (lambda (a b)
8256 (cond
8257 ((equal (marker-buffer a) (marker-buffer b))
8258 (< (marker-position a) (marker-position b)))
8260 (string< (buffer-name (marker-buffer a))
8261 (buffer-name (marker-buffer b))))))))
8263 ;; Now loop over all markers and apply cmd
8264 (while (setq e (pop entries))
8265 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8266 (if (not pos)
8267 (progn (message "Skipping removed entry at %s" e)
8268 (setq cntskip (1+ cntskip)))
8269 (goto-char pos)
8270 (eval cmd)
8271 (setq org-agenda-bulk-marked-entries
8272 (delete e org-agenda-bulk-marked-entries))
8273 (setq cnt (1+ cnt))))
8274 (setq org-agenda-bulk-marked-entries nil)
8275 (org-agenda-bulk-remove-all-marks)
8276 (when redo-at-end (org-agenda-redo))
8277 (message "Acted on %d entries%s"
8279 (if (= cntskip 0)
8281 (format ", skipped %d (disappeared before their turn)"
8282 cntskip)))))
8284 ;;; Flagging notes
8286 (defun org-agenda-show-the-flagging-note ()
8287 "Display the flagging note in the other window.
8288 When called a second time in direct sequence, offer to remove the FLAGGING
8289 tag and (if present) the flagging note."
8290 (interactive)
8291 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8292 (win (selected-window))
8293 note heading newhead)
8294 (unless hdmarker
8295 (error "No linked entry at point"))
8296 (if (and (eq this-command last-command)
8297 (y-or-n-p "Unflag and remove any flagging note? "))
8298 (progn
8299 (org-agenda-remove-flag hdmarker)
8300 (let ((win (get-buffer-window "*Flagging Note*")))
8301 (and win (delete-window win)))
8302 (message "Entry unflaged"))
8303 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8304 (unless note
8305 (error "No flagging note"))
8306 (org-kill-new note)
8307 (org-switch-to-buffer-other-window "*Flagging Note*")
8308 (erase-buffer)
8309 (insert note)
8310 (goto-char (point-min))
8311 (while (re-search-forward "\\\\n" nil t)
8312 (replace-match "\n" t t))
8313 (goto-char (point-min))
8314 (select-window win)
8315 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8317 (defun org-agenda-remove-flag (marker)
8318 "Remove the FLAGGED tag and any flagging note in the entry."
8319 (let (newhead)
8320 (org-with-point-at marker
8321 (org-toggle-tag "FLAGGED" 'off)
8322 (org-entry-delete nil "THEFLAGGINGNOTE")
8323 (setq newhead (org-get-heading)))
8324 (org-agenda-change-all-lines newhead marker)
8325 (message "Entry unflaged")))
8327 (defun org-agenda-get-any-marker (&optional pos)
8328 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8329 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8331 ;;; Appointment reminders
8333 (defvar appt-time-msg-list)
8335 ;;;###autoload
8336 (defun org-agenda-to-appt (&optional refresh filter)
8337 "Activate appointments found in `org-agenda-files'.
8338 With a \\[universal-argument] prefix, refresh the list of
8339 appointments.
8341 If FILTER is t, interactively prompt the user for a regular
8342 expression, and filter out entries that don't match it.
8344 If FILTER is a string, use this string as a regular expression
8345 for filtering entries out.
8347 FILTER can also be an alist with the car of each cell being
8348 either 'headline or 'category. For example:
8350 '((headline \"IMPORTANT\")
8351 (category \"Work\"))
8353 will only add headlines containing IMPORTANT or headlines
8354 belonging to the \"Work\" category."
8355 (interactive "P")
8356 (if refresh (setq appt-time-msg-list nil))
8357 (if (eq filter t)
8358 (setq filter (read-from-minibuffer "Regexp filter: ")))
8359 (let* ((cnt 0) ; count added events
8360 (org-agenda-new-buffers nil)
8361 (org-deadline-warning-days 0)
8362 ;; Do not use `org-today' here because appt only takes
8363 ;; time and without date as argument, so it may pass wrong
8364 ;; information otherwise
8365 (today (org-date-to-gregorian
8366 (time-to-days (current-time))))
8367 (org-agenda-restrict nil)
8368 (files (org-agenda-files 'unrestricted)) entries file)
8369 ;; Get all entries which may contain an appt
8370 (org-prepare-agenda-buffers files)
8371 (while (setq file (pop files))
8372 (setq entries
8373 (append entries
8374 (org-agenda-get-day-entries
8375 file today :timestamp :scheduled :deadline))))
8376 (setq entries (delq nil entries))
8377 ;; Map thru entries and find if we should filter them out
8378 (mapc
8379 (lambda(x)
8380 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8381 (cat (get-text-property 1 'org-category x))
8382 (tod (get-text-property 1 'time-of-day x))
8383 (ok (or (null filter)
8384 (and (stringp filter) (string-match filter evt))
8385 (and (listp filter)
8386 (or (string-match
8387 (cadr (assoc 'category filter)) cat)
8388 (string-match
8389 (cadr (assoc 'headline filter)) evt))))))
8390 ;; FIXME: Shall we remove text-properties for the appt text?
8391 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8392 (when (and ok tod)
8393 (setq tod (concat "00" (number-to-string tod))
8394 tod (when (string-match
8395 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8396 (concat (match-string 1 tod) ":"
8397 (match-string 2 tod))))
8398 (appt-add tod evt)
8399 (setq cnt (1+ cnt))))) entries)
8400 (org-release-buffers org-agenda-new-buffers)
8401 (if (eq cnt 0)
8402 (message "No event to add")
8403 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8405 (defun org-agenda-todayp (date)
8406 "Does DATE mean today, when considering `org-extend-today-until'?"
8407 (let ((today (org-today))
8408 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8409 date)))
8410 (eq date today)))
8412 (provide 'org-agenda)
8414 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
8416 ;;; org-agenda.el ends here