Spelling fixes.
[emacs.git] / lisp / org / org-agenda.el
blob53a0c5587a24c82ee6e17367542c64be373dce49
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.7
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the code for creating and using the Agenda for Org-mode.
30 ;;; Code:
32 (require 'org)
33 (eval-when-compile
34 (require 'cl))
36 (declare-function diary-add-to-list "diary-lib"
37 (date string specifier &optional marker globcolor literal))
38 (declare-function calendar-absolute-from-iso "cal-iso" (date))
39 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
40 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
41 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
42 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
43 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
44 (declare-function calendar-french-date-string "cal-french" (&optional date))
45 (declare-function calendar-goto-date "cal-move" (date))
46 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
47 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
48 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
49 (declare-function calendar-iso-from-absolute "cal-iso" (date))
50 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
51 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
52 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
53 (declare-function org-datetree-find-date-create "org-datetree"
54 (date &optional keep-restriction))
55 (declare-function org-columns-quit "org-colview" ())
56 (declare-function diary-date-display-form "diary-lib" (&optional type))
57 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
58 (declare-function org-habit-insert-consistency-graphs
59 "org-habit" (&optional line))
60 (declare-function org-is-habit-p "org-habit" (&optional pom))
61 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
62 (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 "Always 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 five entries, each for the different agenda types. The
1392 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1393 The values are format strings.
1395 This format works similar to a printf format, with the following meaning:
1397 %c the category of the item, \"Diary\" for entries from the diary,
1398 or as given by the CATEGORY keyword or derived from the file name
1399 %i the icon category of the item, see `org-agenda-category-icon-alist'
1400 %T the last tag of the item (ignore inherited tags, which come first)
1401 %t the HH:MM time-of-day specification if one applies to the entry
1402 %s Scheduling/Deadline information, a short string
1403 %(expression) Eval EXPRESSION and replace the control string
1404 by the result
1406 All specifiers work basically like the standard `%s' of printf, but may
1407 contain two additional characters: a question mark just after the `%'
1408 and a whitespace/punctuation character just before the final letter.
1410 If the first character after `%' is a question mark, the entire field
1411 will only be included if the corresponding value applies to the current
1412 entry. This is useful for fields which should have fixed width when
1413 present, but zero width when absent. For example, \"%?-12t\" will
1414 result in a 12 character time field if a time of the day is specified,
1415 but will completely disappear in entries which do not contain a time.
1417 If there is punctuation or whitespace character just before the final
1418 format letter, this character will be appended to the field value if
1419 the value is not empty. For example, the format \"%-12:c\" leads to
1420 \"Diary: \" if the category is \"Diary\". If the category were be
1421 empty, no additional colon would be inserted.
1423 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1424 which means:
1426 - Indent the line with two space characters
1427 - Give the category a 12 chars wide field, padded with whitespace on
1428 the right (because of `-'). Append a colon if there is a category
1429 (because of `:').
1430 - If there is a time-of-day, put it into a 12 chars wide field. If no
1431 time, don't put in an empty field, just skip it (because of '?').
1432 - Finally, put the scheduling information.
1434 See also the variables `org-agenda-remove-times-when-in-prefix' and
1435 `org-agenda-remove-tags'.
1437 Custom commands can set this variable in the options section."
1438 :type '(choice
1439 (string :tag "General format")
1440 (list :greedy t :tag "View dependent"
1441 (cons (const agenda) (string :tag "Format"))
1442 (cons (const timeline) (string :tag "Format"))
1443 (cons (const todo) (string :tag "Format"))
1444 (cons (const tags) (string :tag "Format"))
1445 (cons (const search) (string :tag "Format"))))
1446 :group 'org-agenda-line-format)
1448 (defvar org-prefix-format-compiled nil
1449 "The compiled version of the most recently used prefix format.
1450 See the variable `org-agenda-prefix-format'.")
1452 (defcustom org-agenda-todo-keyword-format "%-1s"
1453 "Format for the TODO keyword in agenda lines.
1454 Set this to something like \"%-12s\" if you want all TODO keywords
1455 to occupy a fixed space in the agenda display."
1456 :group 'org-agenda-line-format
1457 :type 'string)
1459 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1460 "Text preceding timerange entries in the agenda view.
1461 This is a list with two strings. The first applies when the range
1462 is entirely on one day. The second applies if the range spans several days.
1463 The strings may have two \"%d\" format specifiers which will be filled
1464 with the sequence number of the days, and the total number of days in the
1465 range, respectively."
1466 :group 'org-agenda-line-format
1467 :type '(list
1468 (string :tag "Deadline today ")
1469 (choice :tag "Deadline relative"
1470 (string :tag "Format string")
1471 (function))))
1473 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1474 "Text preceding scheduled items in the agenda view.
1475 This is a list with two strings. The first applies when the item is
1476 scheduled on the current day. The second applies when it has been scheduled
1477 previously, it may contain a %d indicating that this is the nth time that
1478 this item is scheduled, due to automatic rescheduling of unfinished items
1479 for the following day. So this number is one larger than the number of days
1480 that passed since this item was scheduled first."
1481 :group 'org-agenda-line-format
1482 :type '(list
1483 (string :tag "Scheduled today ")
1484 (string :tag "Scheduled previously")))
1486 (defcustom org-agenda-inactive-leader "["
1487 "Text preceding item pulled into the agenda by inactive time stamps.
1488 These entries are added to the agenda when pressing \"[\"."
1489 :group 'org-agenda-line-format
1490 :type '(list
1491 (string :tag "Scheduled today ")
1492 (string :tag "Scheduled previously")))
1494 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1495 "Text preceding deadline items in the agenda view.
1496 This is a list with two strings. The first applies when the item has its
1497 deadline on the current day. The second applies when it is in the past or
1498 in the future, it may contain %d to capture how many days away the deadline
1499 is (was)."
1500 :group 'org-agenda-line-format
1501 :type '(list
1502 (string :tag "Deadline today ")
1503 (choice :tag "Deadline relative"
1504 (string :tag "Format string")
1505 (function))))
1507 (defcustom org-agenda-remove-times-when-in-prefix t
1508 "Non-nil means remove duplicate time specifications in agenda items.
1509 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1510 time-of-day specification in a headline or diary entry is extracted and
1511 placed into the prefix. If this option is non-nil, the original specification
1512 \(a timestamp or -range, or just a plain time(range) specification like
1513 11:30-4pm) will be removed for agenda display. This makes the agenda less
1514 cluttered.
1515 The option can be t or nil. It may also be the symbol `beg', indicating
1516 that the time should only be removed when it is located at the beginning of
1517 the headline/diary entry."
1518 :group 'org-agenda-line-format
1519 :type '(choice
1520 (const :tag "Always" t)
1521 (const :tag "Never" nil)
1522 (const :tag "When at beginning of entry" beg)))
1524 (defcustom org-agenda-remove-timeranges-from-blocks nil
1525 "Non-nil means remove time ranges specifications in agenda
1526 items that span on several days."
1527 :group 'org-agenda-line-format
1528 :type 'boolean)
1530 (defcustom org-agenda-default-appointment-duration nil
1531 "Default duration for appointments that only have a starting time.
1532 When nil, no duration is specified in such cases.
1533 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1534 :group 'org-agenda-line-format
1535 :type '(choice
1536 (integer :tag "Minutes")
1537 (const :tag "No default duration")))
1539 (defcustom org-agenda-show-inherited-tags t
1540 "Non-nil means show inherited tags in each agenda line."
1541 :group 'org-agenda-line-format
1542 :type 'boolean)
1544 (defcustom org-agenda-hide-tags-regexp nil
1545 "Regular expression used to filter away specific tags in agenda views.
1546 This means that these tags will be present, but not be shown in the agenda
1547 line. Secondary filtering will still work on the hidden tags.
1548 Nil means don't hide any tags."
1549 :group 'org-agenda-line-format
1550 :type '(choice
1551 (const :tag "Hide none" nil)
1552 (string :tag "Regexp ")))
1554 (defcustom org-agenda-remove-tags nil
1555 "Non-nil means remove the tags from the headline copy in the agenda.
1556 When this is the symbol `prefix', only remove tags when
1557 `org-agenda-prefix-format' contains a `%T' specifier."
1558 :group 'org-agenda-line-format
1559 :type '(choice
1560 (const :tag "Always" t)
1561 (const :tag "Never" nil)
1562 (const :tag "When prefix format contains %T" prefix)))
1564 (if (fboundp 'defvaralias)
1565 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1566 'org-agenda-remove-tags))
1568 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1569 "Shift tags in agenda items to this column.
1570 If this number is positive, it specifies the column. If it is negative,
1571 it means that the tags should be flushright to that column. For example,
1572 -80 works well for a normal 80 character screen."
1573 :group 'org-agenda-line-format
1574 :type 'integer)
1576 (if (fboundp 'defvaralias)
1577 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1579 (defcustom org-agenda-fontify-priorities 'cookies
1580 "Non-nil means highlight low and high priorities in agenda.
1581 When t, the highest priority entries are bold, lowest priority italic.
1582 However, settings in `org-priority-faces' will overrule these faces.
1583 When this variable is the symbol `cookies', only fontify the
1584 cookies, not the entire task.
1585 This may also be an association list of priority faces, whose
1586 keys are the character values of `org-highest-priority',
1587 `org-default-priority', and `org-lowest-priority' (the default values
1588 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1589 color as a string, or a list like `(:background \"Red\")'.
1590 If it is a color, the variable `org-faces-easy-properties'
1591 determines if it is a foreground or a background color."
1592 :group 'org-agenda-line-format
1593 :type '(choice
1594 (const :tag "Never" nil)
1595 (const :tag "Defaults" t)
1596 (const :tag "Cookies only" cookies)
1597 (repeat :tag "Specify"
1598 (list (character :tag "Priority" :value ?A)
1599 (choice :tag "Face "
1600 (string :tag "Color")
1601 (sexp :tag "Face"))))))
1603 (defcustom org-agenda-day-face-function nil
1604 "Function called to determine what face should be used to display a day.
1605 The only argument passed to that function is the day. It should
1606 returns a face, or nil if does not want to specify a face and let
1607 the normal rules apply."
1608 :group 'org-agenda-line-format
1609 :type 'function)
1611 (defcustom org-agenda-category-icon-alist nil
1612 "Alist of category icon to be displayed in agenda views.
1614 Each entry should have the following format:
1616 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1618 Where CATEGORY-REGEXP is a regexp matching the categories where
1619 the icon should be displayed.
1620 FILE-OR-DATA either a file path or a string containing image data.
1622 The other fields can be omited safely if not needed:
1623 TYPE indicates the image type.
1624 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1625 image data.
1626 PROPS are additional image attributes to assign to the image,
1627 like, e.g. `:ascent center'.
1629 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1631 If you want to set the display properties yourself, just put a
1632 list as second element:
1634 (CATEGORY-REGEXP (MY PROPERTY LIST))
1636 For example, to display a 16px horizontal space for Emacs
1637 category, you can use:
1639 (\"Emacs\" '(space . (:width (16))))"
1640 :group 'org-agenda-line-format
1641 :type '(alist :key-type (string :tag "Regexp matching category")
1642 :value-type (choice (list :tag "Icon"
1643 (string :tag "File or data")
1644 (symbol :tag "Type")
1645 (boolean :tag "Data?")
1646 (repeat :tag "Extra image properties" :inline t symbol))
1647 (list :tag "Display properties" sexp))))
1649 (defgroup org-agenda-column-view nil
1650 "Options concerning column view in the agenda."
1651 :tag "Org Agenda Column View"
1652 :group 'org-agenda)
1654 (defcustom org-agenda-columns-show-summaries t
1655 "Non-nil means show summaries for columns displayed in the agenda view."
1656 :group 'org-agenda-column-view
1657 :type 'boolean)
1659 (defcustom org-agenda-columns-compute-summary-properties t
1660 "Non-nil means recompute all summary properties before column view.
1661 When column view in the agenda is listing properties that have a summary
1662 operator, it can go to all relevant buffers and recompute the summaries
1663 there. This can mean overhead for the agenda column view, but is necessary
1664 to have thing up to date.
1665 As a special case, a CLOCKSUM property also makes sure that the clock
1666 computations are current."
1667 :group 'org-agenda-column-view
1668 :type 'boolean)
1670 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1671 "Non-nil means the duration of an appointment will add to day effort.
1672 The property to which appointment durations will be added is the one given
1673 in the option `org-effort-property'. If an appointment does not have
1674 an end time, `org-agenda-default-appointment-duration' will be used. If that
1675 is not set, an appointment without end time will not contribute to the time
1676 estimate."
1677 :group 'org-agenda-column-view
1678 :type 'boolean)
1680 (defcustom org-agenda-auto-exclude-function nil
1681 "A function called with a tag to decide if it is filtered on '/ RET'.
1682 The sole argument to the function, which is called once for each
1683 possible tag, is a string giving the name of the tag. The
1684 function should return either nil if the tag should be included
1685 as normal, or \"-<TAG>\" to exclude the tag.
1686 Note that for the purpose of tag filtering, only the lower-case version of
1687 all tags will be considered, so that this function will only ever see
1688 the lower-case version of all tags."
1689 :group 'org-agenda
1690 :type 'function)
1692 (defcustom org-agenda-bulk-custom-functions nil
1693 "Alist of characters and custom functions for bulk actions.
1694 For example, this value makes those two functions available:
1696 '((?R set-category)
1697 (?C bulk-cut))
1699 With selected entries in an agenda buffer, `B R' will call
1700 the custom function `set-category' on the selected entries.
1701 Note that functions in this alist don't need to be quoted."
1702 :type 'alist
1703 :group 'org-agenda)
1705 (eval-when-compile
1706 (require 'cl))
1707 (require 'org)
1709 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1710 "Execute BODY with point at location given by `org-hd-marker' property.
1711 If STRING is non-nil, the text property will be fetched from position 0
1712 in that string. If STRING is nil, it will be fetched from the beginning
1713 of the current line."
1714 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1715 'org-hd-marker string)))
1716 (with-current-buffer (marker-buffer marker)
1717 (save-excursion
1718 (goto-char marker)
1719 ,@body))))
1721 (defun org-add-agenda-custom-command (entry)
1722 "Replace or add a command in `org-agenda-custom-commands'.
1723 This is mostly for hacking and trying a new command - once the command
1724 works you probably want to add it to `org-agenda-custom-commands' for good."
1725 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1726 (if ass
1727 (setcdr ass (cdr entry))
1728 (push entry org-agenda-custom-commands))))
1730 ;;; Define the Org-agenda-mode
1732 (defvar org-agenda-mode-map (make-sparse-keymap)
1733 "Keymap for `org-agenda-mode'.")
1734 (if (fboundp 'defvaralias)
1735 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1737 (defvar org-agenda-menu) ; defined later in this file.
1738 (defvar org-agenda-restrict) ; defined later in this file.
1739 (defvar org-agenda-follow-mode nil)
1740 (defvar org-agenda-entry-text-mode nil)
1741 (defvar org-agenda-clockreport-mode nil)
1742 (defvar org-agenda-show-log nil)
1743 (defvar org-agenda-redo-command nil)
1744 (defvar org-agenda-query-string nil)
1745 (defvar org-agenda-mode-hook nil
1746 "Hook for `org-agenda-mode', run after the mode is turned on.")
1747 (defvar org-agenda-type nil)
1748 (defvar org-agenda-force-single-file nil)
1749 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1751 (defun org-agenda-mode ()
1752 "Mode for time-sorted view on action items in Org-mode files.
1754 The following commands are available:
1756 \\{org-agenda-mode-map}"
1757 (interactive)
1758 (kill-all-local-variables)
1759 (setq org-agenda-undo-list nil
1760 org-agenda-pending-undo-list nil
1761 org-agenda-bulk-marked-entries nil)
1762 (setq major-mode 'org-agenda-mode)
1763 ;; Keep global-font-lock-mode from turning on font-lock-mode
1764 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1765 (setq mode-name "Org-Agenda")
1766 (use-local-map org-agenda-mode-map)
1767 (easy-menu-add org-agenda-menu)
1768 (if org-startup-truncated (setq truncate-lines t))
1769 (org-set-local 'line-move-visual nil)
1770 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1771 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1772 ;; Make sure properties are removed when copying text
1773 (when (boundp 'buffer-substring-filters)
1774 (org-set-local 'buffer-substring-filters
1775 (cons (lambda (x)
1776 (set-text-properties 0 (length x) nil x) x)
1777 buffer-substring-filters)))
1778 (unless org-agenda-keep-modes
1779 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1780 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1781 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1782 org-agenda-show-log org-agenda-start-with-log-mode))
1784 (easy-menu-change
1785 '("Agenda") "Agenda Files"
1786 (append
1787 (list
1788 (vector
1789 (if (get 'org-agenda-files 'org-restrict)
1790 "Restricted to single file"
1791 "Edit File List")
1792 '(org-edit-agenda-file-list)
1793 (not (get 'org-agenda-files 'org-restrict)))
1794 "--")
1795 (mapcar 'org-file-menu-entry (org-agenda-files))))
1796 (org-agenda-set-mode-name)
1797 (apply
1798 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1799 (list 'org-agenda-mode-hook)))
1801 (substitute-key-definition 'undo 'org-agenda-undo
1802 org-agenda-mode-map global-map)
1803 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1804 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1805 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1806 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1807 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1808 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1809 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1810 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1811 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1812 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1813 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1814 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1815 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1816 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1817 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1818 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1819 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1820 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1821 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1822 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1823 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1824 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1825 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1826 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1827 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1828 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1829 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1830 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1831 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1832 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1833 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1834 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1835 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1836 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1837 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1838 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1839 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1840 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1841 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1842 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1843 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1844 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1845 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1846 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1847 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1849 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1850 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1851 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1852 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1853 (while l (org-defkey org-agenda-mode-map
1854 (int-to-string (pop l)) 'digit-argument)))
1856 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1857 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1858 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1859 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1860 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1861 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1862 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1863 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1864 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1865 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1866 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1867 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1868 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1869 'org-clock-modify-effort-estimate)
1870 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1871 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1872 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1873 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1874 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1875 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1876 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1877 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1878 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1879 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1880 (substitute-key-definition 'next-line 'org-agenda-next-line
1881 org-agenda-mode-map global-map)
1882 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1883 org-agenda-mode-map global-map)
1884 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1885 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1886 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1887 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1888 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1889 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1890 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1891 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1892 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1893 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1894 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1895 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1896 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1897 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1898 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1899 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1900 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1901 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1902 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1903 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1904 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1905 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1906 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1907 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1908 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1909 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1910 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1911 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1912 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1913 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1915 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1916 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1917 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1918 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1919 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1920 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1921 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1922 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1923 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1924 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1926 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1927 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1928 (when org-agenda-mouse-1-follows-link
1929 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1930 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1931 '("Agenda"
1932 ("Agenda Files")
1933 "--"
1934 ("Agenda Dates"
1935 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1936 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1937 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1938 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1939 "--"
1940 ("View"
1941 ["Day View" org-agenda-day-view
1942 :active (org-agenda-check-type nil 'agenda)
1943 :style radio :selected (eq org-agenda-current-span 'day)
1944 :keys "v d (or just d)"]
1945 ["Week View" org-agenda-week-view
1946 :active (org-agenda-check-type nil 'agenda)
1947 :style radio :selected (eq org-agenda-current-span 'week)
1948 :keys "v w (or just w)"]
1949 ["Month View" org-agenda-month-view
1950 :active (org-agenda-check-type nil 'agenda)
1951 :style radio :selected (eq org-agenda-current-span 'month)
1952 :keys "v m"]
1953 ["Year View" org-agenda-year-view
1954 :active (org-agenda-check-type nil 'agenda)
1955 :style radio :selected (eq org-agenda-current-span 'year)
1956 :keys "v y"]
1957 "--"
1958 ["Include Diary" org-agenda-toggle-diary
1959 :style toggle :selected org-agenda-include-diary
1960 :active (org-agenda-check-type nil 'agenda)]
1961 ["Include Deadlines" org-agenda-toggle-deadlines
1962 :style toggle :selected org-agenda-include-deadlines
1963 :active (org-agenda-check-type nil 'agenda)]
1964 ["Use Time Grid" org-agenda-toggle-time-grid
1965 :style toggle :selected org-agenda-use-time-grid
1966 :active (org-agenda-check-type nil 'agenda)]
1967 "--"
1968 ["Show clock report" org-agenda-clockreport-mode
1969 :style toggle :selected org-agenda-clockreport-mode
1970 :active (org-agenda-check-type nil 'agenda)]
1971 ["Show some entry text" org-agenda-entry-text-mode
1972 :style toggle :selected org-agenda-entry-text-mode
1973 :active t]
1974 "--"
1975 ["Show Logbook entries" org-agenda-log-mode
1976 :style toggle :selected org-agenda-show-log
1977 :active (org-agenda-check-type nil 'agenda 'timeline)
1978 :keys "v l (or just l)"]
1979 ["Include archived trees" org-agenda-archives-mode
1980 :style toggle :selected org-agenda-archives-mode :active t
1981 :keys "v a"]
1982 ["Include archive files" (org-agenda-archives-mode t)
1983 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1984 :keys "v A"]
1985 "--"
1986 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1987 ["Write view to file" org-write-agenda t]
1988 ["Rebuild buffer" org-agenda-redo t]
1989 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1990 "--"
1991 ["Show original entry" org-agenda-show t]
1992 ["Go To (other window)" org-agenda-goto t]
1993 ["Go To (this window)" org-agenda-switch-to t]
1994 ["Follow Mode" org-agenda-follow-mode
1995 :style toggle :selected org-agenda-follow-mode :active t]
1996 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1997 "--"
1998 ("TODO"
1999 ["Cycle TODO" org-agenda-todo t]
2000 ["Next TODO set" org-agenda-todo-nextset t]
2001 ["Previous TODO set" org-agenda-todo-previousset t]
2002 ["Add note" org-agenda-add-note t])
2003 ("Archive/Refile/Delete"
2004 ["Archive default" org-agenda-archive-default t]
2005 ["Archive default" org-agenda-archive-default-with-confirmation t]
2006 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2007 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2008 ["Archive subtree" org-agenda-archive t]
2009 "--"
2010 ["Refile" org-agenda-refile t]
2011 "--"
2012 ["Delete subtree" org-agenda-kill t])
2013 ("Bulk action"
2014 ["Mark entry" org-agenda-bulk-mark t]
2015 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2016 ["Unmark entry" org-agenda-bulk-unmark t]
2017 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2018 ["Act on all marked" org-agenda-bulk-action t]
2019 "--"
2020 ("Tags and Properties"
2021 ["Show all Tags" org-agenda-show-tags t]
2022 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2023 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2024 "--"
2025 ["Column View" org-columns t])
2026 ("Deadline/Schedule"
2027 ["Schedule" org-agenda-schedule t]
2028 ["Set Deadline" org-agenda-deadline t]
2029 "--"
2030 ["Mark item" org-agenda-action :active t :keys "k m"]
2031 ["Show mark item" org-agenda-action :active t :keys "k v"]
2032 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2033 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2034 "--"
2035 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2036 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2037 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2038 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2039 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2040 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2041 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2042 ("Clock and Effort"
2043 ["Clock in" org-agenda-clock-in t]
2044 ["Clock out" org-agenda-clock-out t]
2045 ["Clock cancel" org-agenda-clock-cancel t]
2046 ["Goto running clock" org-clock-goto t]
2047 "--"
2048 ["Set Effort" org-agenda-set-effort t]
2049 ["Change clocked effort" org-clock-modify-effort-estimate
2050 (org-clock-is-active)])
2051 ("Priority"
2052 ["Set Priority" org-agenda-priority t]
2053 ["Increase Priority" org-agenda-priority-up t]
2054 ["Decrease Priority" org-agenda-priority-down t]
2055 ["Show Priority" org-agenda-show-priority t])
2056 ("Calendar/Diary"
2057 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2058 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2059 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2060 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2061 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2062 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2063 "--"
2064 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2065 "--"
2066 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2067 "--"
2068 ("MobileOrg"
2069 ["Push Files and Views" org-mobile-push t]
2070 ["Get Captured and Flagged" org-mobile-pull t]
2071 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2072 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2073 "--"
2074 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2075 "--"
2076 ["Quit" org-agenda-quit t]
2077 ["Exit and Release Buffers" org-agenda-exit t]
2080 ;;; Agenda undo
2082 (defvar org-agenda-allow-remote-undo t
2083 "Non-nil means allow remote undo from the agenda buffer.")
2084 (defvar org-agenda-undo-list nil
2085 "List of undoable operations in the agenda since last refresh.")
2086 (defvar org-agenda-undo-has-started-in nil
2087 "Buffers that have already seen `undo-start' in the current undo sequence.")
2088 (defvar org-agenda-pending-undo-list nil
2089 "In a series of undo commands, this is the list of remaining undo items.")
2091 (defun org-agenda-undo ()
2092 "Undo a remote editing step in the agenda.
2093 This undoes changes both in the agenda buffer and in the remote buffer
2094 that have been changed along."
2095 (interactive)
2096 (or org-agenda-allow-remote-undo
2097 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2098 (if (not (eq this-command last-command))
2099 (setq org-agenda-undo-has-started-in nil
2100 org-agenda-pending-undo-list org-agenda-undo-list))
2101 (if (not org-agenda-pending-undo-list)
2102 (error "No further undo information"))
2103 (let* ((entry (pop org-agenda-pending-undo-list))
2104 buf line cmd rembuf)
2105 (setq cmd (pop entry) line (pop entry))
2106 (setq rembuf (nth 2 entry))
2107 (org-with-remote-undo rembuf
2108 (while (bufferp (setq buf (pop entry)))
2109 (if (pop entry)
2110 (with-current-buffer buf
2111 (let ((last-undo-buffer buf)
2112 (inhibit-read-only t))
2113 (unless (memq buf org-agenda-undo-has-started-in)
2114 (push buf org-agenda-undo-has-started-in)
2115 (make-local-variable 'pending-undo-list)
2116 (undo-start))
2117 (while (and pending-undo-list
2118 (listp pending-undo-list)
2119 (not (car pending-undo-list)))
2120 (pop pending-undo-list))
2121 (undo-more 1))))))
2122 (org-goto-line line)
2123 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2125 (defun org-verify-change-for-undo (l1 l2)
2126 "Verify that a real change occurred between the undo lists L1 and L2."
2127 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2128 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2129 (not (eq l1 l2)))
2131 ;;; Agenda dispatch
2133 (defvar org-agenda-restrict nil)
2134 (defvar org-agenda-restrict-begin (make-marker))
2135 (defvar org-agenda-restrict-end (make-marker))
2136 (defvar org-agenda-last-dispatch-buffer nil)
2137 (defvar org-agenda-overriding-restriction nil)
2139 ;;;###autoload
2140 (defun org-agenda (&optional arg keys restriction)
2141 "Dispatch agenda commands to collect entries to the agenda buffer.
2142 Prompts for a command to execute. Any prefix arg will be passed
2143 on to the selected command. The default selections are:
2145 a Call `org-agenda-list' to display the agenda for current day or week.
2146 t Call `org-todo-list' to display the global todo list.
2147 T Call `org-todo-list' to display the global todo list, select only
2148 entries with a specific TODO keyword (the user gets a prompt).
2149 m Call `org-tags-view' to display headlines with tags matching
2150 a condition (the user is prompted for the condition).
2151 M Like `m', but select only TODO entries, no ordinary headlines.
2152 L Create a timeline for the current buffer.
2153 e Export views to associated files.
2154 s Search entries for keywords.
2155 / Multi occur across all agenda files and also files listed
2156 in `org-agenda-text-search-extra-files'.
2157 < Restrict agenda commands to buffer, subtree, or region.
2158 Press several times to get the desired effect.
2159 > Remove a previous restriction.
2160 # List \"stuck\" projects.
2161 ! Configure what \"stuck\" means.
2162 C Configure custom agenda commands.
2164 More commands can be added by configuring the variable
2165 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2166 searches can be pre-defined in this way.
2168 If the current buffer is in Org-mode and visiting a file, you can also
2169 first press `<' once to indicate that the agenda should be temporarily
2170 \(until the next use of \\[org-agenda]) restricted to the current file.
2171 Pressing `<' twice means to restrict to the current subtree or region
2172 \(if active)."
2173 (interactive "P")
2174 (catch 'exit
2175 (let* ((prefix-descriptions nil)
2176 (org-agenda-window-setup (if (equal (buffer-name)
2177 org-agenda-buffer-name)
2178 'current-window
2179 org-agenda-window-setup))
2180 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2181 (org-agenda-custom-commands
2182 ;; normalize different versions
2183 (delq nil
2184 (mapcar
2185 (lambda (x)
2186 (cond ((stringp (cdr x))
2187 (push x prefix-descriptions)
2188 nil)
2189 ((stringp (nth 1 x)) x)
2190 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2191 (t (cons (car x) (cons "" (cdr x))))))
2192 org-agenda-custom-commands)))
2193 (buf (current-buffer))
2194 (bfn (buffer-file-name (buffer-base-buffer)))
2195 entry key type match lprops ans)
2196 ;; Turn off restriction unless there is an overriding one,
2197 (unless org-agenda-overriding-restriction
2198 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2199 ;; There is a request to keep the file list in place
2200 (put 'org-agenda-files 'org-restrict nil))
2201 (setq org-agenda-restrict nil)
2202 (move-marker org-agenda-restrict-begin nil)
2203 (move-marker org-agenda-restrict-end nil))
2204 ;; Delete old local properties
2205 (put 'org-agenda-redo-command 'org-lprops nil)
2206 ;; Delete previously set last-arguments
2207 (put 'org-agenda-redo-command 'last-args nil)
2208 ;; Remember where this call originated
2209 (setq org-agenda-last-dispatch-buffer (current-buffer))
2210 (unless keys
2211 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2212 keys (car ans)
2213 restriction (cdr ans)))
2214 ;; Establish the restriction, if any
2215 (when (and (not org-agenda-overriding-restriction) restriction)
2216 (put 'org-agenda-files 'org-restrict (list bfn))
2217 (cond
2218 ((eq restriction 'region)
2219 (setq org-agenda-restrict t)
2220 (move-marker org-agenda-restrict-begin (region-beginning))
2221 (move-marker org-agenda-restrict-end (region-end)))
2222 ((eq restriction 'subtree)
2223 (save-excursion
2224 (setq org-agenda-restrict t)
2225 (org-back-to-heading t)
2226 (move-marker org-agenda-restrict-begin (point))
2227 (move-marker org-agenda-restrict-end
2228 (progn (org-end-of-subtree t)))))))
2230 ;; For example the todo list should not need it (but does...)
2231 (cond
2232 ((setq entry (assoc keys org-agenda-custom-commands))
2233 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2234 (progn
2235 (setq type (nth 2 entry) match (eval (nth 3 entry))
2236 lprops (nth 4 entry))
2237 (put 'org-agenda-redo-command 'org-lprops lprops)
2238 (cond
2239 ((eq type 'agenda)
2240 (org-let lprops '(org-agenda-list current-prefix-arg)))
2241 ((eq type 'alltodo)
2242 (org-let lprops '(org-todo-list current-prefix-arg)))
2243 ((eq type 'search)
2244 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2245 ((eq type 'stuck)
2246 (org-let lprops '(org-agenda-list-stuck-projects
2247 current-prefix-arg)))
2248 ((eq type 'tags)
2249 (org-let lprops '(org-tags-view current-prefix-arg match)))
2250 ((eq type 'tags-todo)
2251 (org-let lprops '(org-tags-view '(4) match)))
2252 ((eq type 'todo)
2253 (org-let lprops '(org-todo-list match)))
2254 ((eq type 'tags-tree)
2255 (org-check-for-org-mode)
2256 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2257 ((eq type 'todo-tree)
2258 (org-check-for-org-mode)
2259 (org-let lprops
2260 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2261 (regexp-quote match) "\\>"))))
2262 ((eq type 'occur-tree)
2263 (org-check-for-org-mode)
2264 (org-let lprops '(org-occur match)))
2265 ((functionp type)
2266 (org-let lprops '(funcall type match)))
2267 ((fboundp type)
2268 (org-let lprops '(funcall type match)))
2269 (t (error "Invalid custom agenda command type %s" type))))
2270 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2271 ((equal keys "C")
2272 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2273 (customize-variable 'org-agenda-custom-commands))
2274 ((equal keys "a") (call-interactively 'org-agenda-list))
2275 ((equal keys "s") (call-interactively 'org-search-view))
2276 ((equal keys "t") (call-interactively 'org-todo-list))
2277 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2278 ((equal keys "m") (call-interactively 'org-tags-view))
2279 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2280 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2281 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2282 (org-add-hook
2283 'post-command-hook
2284 (lambda ()
2285 (unless (current-message)
2286 (let* ((m (org-agenda-get-any-marker))
2287 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2288 (when note
2289 (message (concat
2290 "FLAGGING-NOTE ([?] for more info): "
2291 (org-add-props
2292 (replace-regexp-in-string
2293 "\\\\n" "//"
2294 (copy-sequence note))
2295 nil 'face 'org-warning)))))))
2296 t t))
2297 ((equal keys "L")
2298 (unless (org-mode-p)
2299 (error "This is not an Org-mode file"))
2300 (unless restriction
2301 (put 'org-agenda-files 'org-restrict (list bfn))
2302 (org-call-with-arg 'org-timeline arg)))
2303 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2304 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2305 ((equal keys "!") (customize-variable 'org-stuck-projects))
2306 (t (error "Invalid agenda key"))))))
2308 (defun org-agenda-append-agenda ()
2309 "Append another agenda view to the current one.
2310 This function allows interactive building of block agendas.
2311 Agenda views are separated by `org-agenda-block-separator'."
2312 (interactive)
2313 (unless (string= (buffer-name) org-agenda-buffer-name)
2314 (error "Can only append from within agenda buffer"))
2315 (let ((org-agenda-multi t))
2316 (org-agenda)
2317 (widen)))
2319 (defun org-agenda-normalize-custom-commands (cmds)
2320 (delq nil
2321 (mapcar
2322 (lambda (x)
2323 (cond ((stringp (cdr x)) nil)
2324 ((stringp (nth 1 x)) x)
2325 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2326 (t (cons (car x) (cons "" (cdr x))))))
2327 cmds)))
2329 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2330 "The user interface for selecting an agenda command."
2331 (catch 'exit
2332 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2333 (restrict-ok (and bfn (org-mode-p)))
2334 (region-p (org-region-active-p))
2335 (custom org-agenda-custom-commands)
2336 (selstring "")
2337 restriction second-time
2338 c entry key type match prefixes rmheader header-end custom1 desc
2339 line lines left right n n1)
2340 (save-window-excursion
2341 (delete-other-windows)
2342 (org-switch-to-buffer-other-window " *Agenda Commands*")
2343 (erase-buffer)
2344 (insert (eval-when-compile
2345 (let ((header
2347 Press key for an agenda command: < Buffer, subtree/region restriction
2348 -------------------------------- > Remove restriction
2349 a Agenda for current week or day e Export agenda views
2350 t List of all TODO entries T Entries with special TODO kwd
2351 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2352 L Timeline for current buffer # List stuck projects (!=configure)
2353 s Search for keywords C Configure custom agenda commands
2354 / Multi-occur ? Find :FLAGGED: entries
2356 (start 0))
2357 (while (string-match
2358 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2359 header start)
2360 (setq start (match-end 0))
2361 (add-text-properties (match-beginning 2) (match-end 2)
2362 '(face bold) header))
2363 header)))
2364 (setq header-end (move-marker (make-marker) (point)))
2365 (while t
2366 (setq custom1 custom)
2367 (when (eq rmheader t)
2368 (org-goto-line 1)
2369 (re-search-forward ":" nil t)
2370 (delete-region (match-end 0) (point-at-eol))
2371 (forward-char 1)
2372 (looking-at "-+")
2373 (delete-region (match-end 0) (point-at-eol))
2374 (move-marker header-end (match-end 0)))
2375 (goto-char header-end)
2376 (delete-region (point) (point-max))
2378 ;; Produce all the lines that describe custom commands and prefixes
2379 (setq lines nil)
2380 (while (setq entry (pop custom1))
2381 (setq key (car entry) desc (nth 1 entry)
2382 type (nth 2 entry)
2383 match (nth 3 entry))
2384 (if (> (length key) 1)
2385 (add-to-list 'prefixes (string-to-char key))
2386 (setq line
2387 (format
2388 "%-4s%-14s"
2389 (org-add-props (copy-sequence key)
2390 '(face bold))
2391 (cond
2392 ((string-match "\\S-" desc) desc)
2393 ((eq type 'agenda) "Agenda for current week or day")
2394 ((eq type 'alltodo) "List of all TODO entries")
2395 ((eq type 'search) "Word search")
2396 ((eq type 'stuck) "List of stuck projects")
2397 ((eq type 'todo) "TODO keyword")
2398 ((eq type 'tags) "Tags query")
2399 ((eq type 'tags-todo) "Tags (TODO)")
2400 ((eq type 'tags-tree) "Tags tree")
2401 ((eq type 'todo-tree) "TODO kwd tree")
2402 ((eq type 'occur-tree) "Occur tree")
2403 ((functionp type) (if (symbolp type)
2404 (symbol-name type)
2405 "Lambda expression"))
2406 (t "???"))))
2407 (if org-agenda-menu-show-matcher
2408 (setq line
2409 (concat line ": "
2410 (cond
2411 ((stringp match)
2412 (setq match (copy-sequence match))
2413 (org-add-props match nil 'face 'org-warning))
2414 (match
2415 (format "set of %d commands" (length match)))
2416 (t ""))))
2417 (if (org-string-nw-p match)
2418 (add-text-properties
2419 0 (length line) (list 'help-echo
2420 (concat "Matcher: "match)) line)))
2421 (push line lines)))
2422 (setq lines (nreverse lines))
2423 (when prefixes
2424 (mapc (lambda (x)
2425 (push
2426 (format "%s %s"
2427 (org-add-props (char-to-string x)
2428 nil 'face 'bold)
2429 (or (cdr (assoc (concat selstring
2430 (char-to-string x))
2431 prefix-descriptions))
2432 "Prefix key"))
2433 lines))
2434 prefixes))
2436 ;; Check if we should display in two columns
2437 (if org-agenda-menu-two-column
2438 (progn
2439 (setq n (length lines)
2440 n1 (+ (/ n 2) (mod n 2))
2441 right (nthcdr n1 lines)
2442 left (copy-sequence lines))
2443 (setcdr (nthcdr (1- n1) left) nil))
2444 (setq left lines right nil))
2445 (while left
2446 (insert "\n" (pop left))
2447 (when right
2448 (if (< (current-column) 40)
2449 (move-to-column 40 t)
2450 (insert " "))
2451 (insert (pop right))))
2453 ;; Make the window the right size
2454 (goto-char (point-min))
2455 (if second-time
2456 (if (not (pos-visible-in-window-p (point-max)))
2457 (org-fit-window-to-buffer))
2458 (setq second-time t)
2459 (org-fit-window-to-buffer))
2461 ;; Ask for selection
2462 (message "Press key for agenda command%s:"
2463 (if (or restrict-ok org-agenda-overriding-restriction)
2464 (if org-agenda-overriding-restriction
2465 " (restriction lock active)"
2466 (if restriction
2467 (format " (restricted to %s)" restriction)
2468 " (unrestricted)"))
2469 ""))
2470 (setq c (read-char-exclusive))
2471 (message "")
2472 (cond
2473 ((assoc (char-to-string c) custom)
2474 (setq selstring (concat selstring (char-to-string c)))
2475 (throw 'exit (cons selstring restriction)))
2476 ((memq c prefixes)
2477 (setq selstring (concat selstring (char-to-string c))
2478 prefixes nil
2479 rmheader (or rmheader t)
2480 custom (delq nil (mapcar
2481 (lambda (x)
2482 (if (or (= (length (car x)) 1)
2483 (/= (string-to-char (car x)) c))
2485 (cons (substring (car x) 1) (cdr x))))
2486 custom))))
2487 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2488 (message "Restriction is only possible in Org-mode buffers")
2489 (ding) (sit-for 1))
2490 ((eq c ?1)
2491 (org-agenda-remove-restriction-lock 'noupdate)
2492 (setq restriction 'buffer))
2493 ((eq c ?0)
2494 (org-agenda-remove-restriction-lock 'noupdate)
2495 (setq restriction (if region-p 'region 'subtree)))
2496 ((eq c ?<)
2497 (org-agenda-remove-restriction-lock 'noupdate)
2498 (setq restriction
2499 (cond
2500 ((eq restriction 'buffer)
2501 (if region-p 'region 'subtree))
2502 ((memq restriction '(subtree region))
2503 nil)
2504 (t 'buffer))))
2505 ((eq c ?>)
2506 (org-agenda-remove-restriction-lock 'noupdate)
2507 (setq restriction nil))
2508 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2509 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2510 ((and (> (length selstring) 0) (eq c ?\d))
2511 (delete-window)
2512 (org-agenda-get-restriction-and-command prefix-descriptions))
2514 ((equal c ?q) (error "Abort"))
2515 (t (error "Invalid key %c" c))))))))
2517 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2518 (defvar org-agenda-last-arguments nil
2519 "The arguments of the previous call to `org-agenda'.")
2520 (defun org-agenda-run-series (name series)
2521 (org-let (nth 1 series) '(org-prepare-agenda name))
2522 (let* ((org-agenda-multi t)
2523 (redo (list 'org-agenda-run-series name (list 'quote series)))
2524 (org-agenda-overriding-arguments
2525 (or org-agenda-overriding-arguments
2526 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2527 (get 'org-agenda-redo-command 'last-args))))
2528 (cmds (car series))
2529 (gprops (nth 1 series))
2530 match ;; The byte compiler incorrectly complains about this. Keep it!
2531 cmd type lprops)
2532 (while (setq cmd (pop cmds))
2533 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2534 (cond
2535 ((eq type 'agenda)
2536 (org-let2 gprops lprops
2537 '(call-interactively 'org-agenda-list)))
2538 ((eq type 'alltodo)
2539 (org-let2 gprops lprops
2540 '(call-interactively 'org-todo-list)))
2541 ((eq type 'search)
2542 (org-let2 gprops lprops
2543 '(org-search-view current-prefix-arg match nil)))
2544 ((eq type 'stuck)
2545 (org-let2 gprops lprops
2546 '(call-interactively 'org-agenda-list-stuck-projects)))
2547 ((eq type 'tags)
2548 (org-let2 gprops lprops
2549 '(org-tags-view current-prefix-arg match)))
2550 ((eq type 'tags-todo)
2551 (org-let2 gprops lprops
2552 '(org-tags-view '(4) match)))
2553 ((eq type 'todo)
2554 (org-let2 gprops lprops
2555 '(org-todo-list match)))
2556 ((fboundp type)
2557 (org-let2 gprops lprops
2558 '(funcall type match)))
2559 (t (error "Invalid type in command series"))))
2560 (widen)
2561 (setq org-agenda-redo-command redo)
2562 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2563 (goto-char (point-min)))
2564 (org-fit-agenda-window)
2565 (org-let (nth 1 series) '(org-finalize-agenda)))
2567 ;;;###autoload
2568 (defmacro org-batch-agenda (cmd-key &rest parameters)
2569 "Run an agenda command in batch mode and send the result to STDOUT.
2570 If CMD-KEY is a string of length 1, it is used as a key in
2571 `org-agenda-custom-commands' and triggers this command. If it is a
2572 longer string it is used as a tags/todo match string.
2573 Parameters are alternating variable names and values that will be bound
2574 before running the agenda command."
2575 (let (pars)
2576 (while parameters
2577 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2578 (if (> (length cmd-key) 2)
2579 (eval (list 'let (nreverse pars)
2580 (list 'org-tags-view nil cmd-key)))
2581 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2582 (set-buffer org-agenda-buffer-name)
2583 (princ (org-encode-for-stdout (buffer-string)))))
2585 ;(defun org-encode-for-stdout (string)
2586 ; (if (fboundp 'encode-coding-string)
2587 ; (encode-coding-string string buffer-file-coding-system)
2588 ; string))
2590 (defun org-encode-for-stdout (string)
2591 string)
2593 (defvar org-agenda-info nil)
2595 ;;;###autoload
2596 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2597 "Run an agenda command in batch mode and send the result to STDOUT.
2598 If CMD-KEY is a string of length 1, it is used as a key in
2599 `org-agenda-custom-commands' and triggers this command. If it is a
2600 longer string it is used as a tags/todo match string.
2601 Parameters are alternating variable names and values that will be bound
2602 before running the agenda command.
2604 The output gives a line for each selected agenda item. Each
2605 item is a list of comma-separated values, like this:
2607 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2609 category The category of the item
2610 head The headline, without TODO kwd, TAGS and PRIORITY
2611 type The type of the agenda entry, can be
2612 todo selected in TODO match
2613 tagsmatch selected in tags match
2614 diary imported from diary
2615 deadline a deadline on given date
2616 scheduled scheduled on given date
2617 timestamp entry has timestamp on given date
2618 closed entry was closed on given date
2619 upcoming-deadline warning about deadline
2620 past-scheduled forwarded scheduled item
2621 block entry has date block including g. date
2622 todo The todo keyword, if any
2623 tags All tags including inherited ones, separated by colons
2624 date The relevant date, like 2007-2-14
2625 time The time, like 15:00-16:50
2626 extra Sting with extra planning info
2627 priority-l The priority letter if any was given
2628 priority-n The computed numerical priority
2629 agenda-day The day in the agenda where this is listed"
2631 (let (pars)
2632 (while parameters
2633 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2634 (push (list 'org-agenda-remove-tags t) pars)
2635 (if (> (length cmd-key) 2)
2636 (eval (list 'let (nreverse pars)
2637 (list 'org-tags-view nil cmd-key)))
2638 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2639 (set-buffer org-agenda-buffer-name)
2640 (let* ((lines (org-split-string (buffer-string) "\n"))
2641 line)
2642 (while (setq line (pop lines))
2643 (catch 'next
2644 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2645 (setq org-agenda-info
2646 (org-fix-agenda-info (text-properties-at 0 line)))
2647 (princ
2648 (org-encode-for-stdout
2649 (mapconcat 'org-agenda-export-csv-mapper
2650 '(org-category txt type todo tags date time extra
2651 priority-letter priority agenda-day)
2652 ",")))
2653 (princ "\n"))))))
2655 (defun org-fix-agenda-info (props)
2656 "Make sure all properties on an agenda item have a canonical form.
2657 This ensures the export commands can easily use it."
2658 (let (tmp re)
2659 (when (setq tmp (plist-get props 'tags))
2660 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2661 (when (setq tmp (plist-get props 'date))
2662 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2663 (let ((calendar-date-display-form '(year "-" month "-" day)))
2664 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2666 (setq tmp (calendar-date-string tmp)))
2667 (setq props (plist-put props 'date tmp)))
2668 (when (setq tmp (plist-get props 'day))
2669 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2670 (let ((calendar-date-display-form '(year "-" month "-" day)))
2671 (setq tmp (calendar-date-string tmp)))
2672 (setq props (plist-put props 'day tmp))
2673 (setq props (plist-put props 'agenda-day tmp)))
2674 (when (setq tmp (plist-get props 'txt))
2675 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2676 (plist-put props 'priority-letter (match-string 1 tmp))
2677 (setq tmp (replace-match "" t t tmp)))
2678 (when (and (setq re (plist-get props 'org-todo-regexp))
2679 (setq re (concat "\\`\\.*" re " ?"))
2680 (string-match re tmp))
2681 (plist-put props 'todo (match-string 1 tmp))
2682 (setq tmp (replace-match "" t t tmp)))
2683 (plist-put props 'txt tmp)))
2684 props)
2686 (defun org-agenda-export-csv-mapper (prop)
2687 (let ((res (plist-get org-agenda-info prop)))
2688 (setq res
2689 (cond
2690 ((not res) "")
2691 ((stringp res) res)
2692 (t (prin1-to-string res))))
2693 (while (string-match "," res)
2694 (setq res (replace-match ";" t t res)))
2695 (org-trim res)))
2698 ;;;###autoload
2699 (defun org-store-agenda-views (&rest parameters)
2700 (interactive)
2701 (eval (list 'org-batch-store-agenda-views)))
2703 ;; FIXME, why is this a macro?????
2704 ;;;###autoload
2705 (defmacro org-batch-store-agenda-views (&rest parameters)
2706 "Run all custom agenda commands that have a file argument."
2707 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2708 (pop-up-frames nil)
2709 (dir default-directory)
2710 pars cmd thiscmdkey files opts cmd-or-set)
2711 (while parameters
2712 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2713 (setq pars (reverse pars))
2714 (save-window-excursion
2715 (while cmds
2716 (setq cmd (pop cmds)
2717 thiscmdkey (car cmd)
2718 cmd-or-set (nth 2 cmd)
2719 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2720 files (nth (if (listp cmd-or-set) 4 5) cmd))
2721 (if (stringp files) (setq files (list files)))
2722 (when files
2723 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2724 (list 'org-agenda nil thiscmdkey)))
2725 (set-buffer org-agenda-buffer-name)
2726 (while files
2727 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2728 (list 'org-write-agenda
2729 (expand-file-name (pop files) dir) nil t))))
2730 (and (get-buffer org-agenda-buffer-name)
2731 (kill-buffer org-agenda-buffer-name)))))))
2733 (defun org-agenda-mark-header-line (pos)
2734 "Mark the line at POS as an agenda structure header."
2735 (save-excursion
2736 (goto-char pos)
2737 (put-text-property (point-at-bol) (point-at-eol)
2738 'org-agenda-structural-header t)
2739 (when org-agenda-title-append
2740 (put-text-property (point-at-bol) (point-at-eol)
2741 'org-agenda-title-append org-agenda-title-append))))
2743 (defvar org-mobile-creating-agendas)
2744 (defun org-write-agenda (file &optional open nosettings)
2745 "Write the current buffer (an agenda view) as a file.
2746 Depending on the extension of the file name, plain text (.txt),
2747 HTML (.html or .htm) or Postscript (.ps) is produced.
2748 If the extension is .ics, run icalendar export over all files used
2749 to construct the agenda and limit the export to entries listed in the
2750 agenda now.
2751 With prefix argument OPEN, open the new file immediately.
2752 If NOSETTINGS is given, do not scope the settings of
2753 `org-agenda-exporter-settings' into the export commands. This is used when
2754 the settings have already been scoped and we do not wish to overrule other,
2755 higher priority settings."
2756 (interactive "FWrite agenda to file: \nP")
2757 (if (not (file-writable-p file))
2758 (error "Cannot write agenda to file %s" file))
2759 (org-let (if nosettings nil org-agenda-exporter-settings)
2760 '(save-excursion
2761 (save-window-excursion
2762 (org-agenda-mark-filtered-text)
2763 (let ((bs (copy-sequence (buffer-string))) beg)
2764 (org-agenda-unmark-filtered-text)
2765 (with-temp-buffer
2766 (rename-buffer "Agenda View" t)
2767 (set-buffer-modified-p nil)
2768 (insert bs)
2769 (org-agenda-remove-marked-text 'org-filtered)
2770 (while (setq beg (text-property-any (point-min) (point-max)
2771 'org-filtered t))
2772 (delete-region
2773 beg (or (next-single-property-change beg 'org-filtered)
2774 (point-max))))
2775 (run-hooks 'org-agenda-before-write-hook)
2776 (cond
2777 ((org-bound-and-true-p org-mobile-creating-agendas)
2778 (org-mobile-write-agenda-for-mobile file))
2779 ((string-match "\\.html?\\'" file)
2780 (require 'htmlize)
2781 (set-buffer (htmlize-buffer (current-buffer)))
2783 (when (and org-agenda-export-html-style
2784 (string-match "<style>" org-agenda-export-html-style))
2785 ;; replace <style> section with org-agenda-export-html-style
2786 (goto-char (point-min))
2787 (kill-region (- (search-forward "<style") 6)
2788 (search-forward "</style>"))
2789 (insert org-agenda-export-html-style))
2790 (write-file file)
2791 (kill-buffer (current-buffer))
2792 (message "HTML written to %s" file))
2793 ((string-match "\\.ps\\'" file)
2794 (require 'ps-print)
2795 (ps-print-buffer-with-faces file)
2796 (message "Postscript written to %s" file))
2797 ((string-match "\\.pdf\\'" file)
2798 (require 'ps-print)
2799 (ps-print-buffer-with-faces
2800 (concat (file-name-sans-extension file) ".ps"))
2801 (call-process "ps2pdf" nil nil nil
2802 (expand-file-name
2803 (concat (file-name-sans-extension file) ".ps"))
2804 (expand-file-name file))
2805 (delete-file (concat (file-name-sans-extension file) ".ps"))
2806 (message "PDF written to %s" file))
2807 ((string-match "\\.ics\\'" file)
2808 (require 'org-icalendar)
2809 (let ((org-agenda-marker-table
2810 (org-create-marker-find-array
2811 (org-agenda-collect-markers)))
2812 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2813 (org-combined-agenda-icalendar-file file))
2814 (apply 'org-export-icalendar 'combine
2815 (org-agenda-files nil 'ifmode))))
2817 (let ((bs (buffer-string)))
2818 (find-file file)
2819 (erase-buffer)
2820 (insert bs)
2821 (save-buffer 0)
2822 (kill-buffer (current-buffer))
2823 (message "Plain text written to %s" file))))))))
2824 (set-buffer org-agenda-buffer-name))
2825 (when open (org-open-file file)))
2827 (defvar org-agenda-filter-overlays nil)
2829 (defun org-agenda-mark-filtered-text ()
2830 "Mark all text hidden by filtering with a text property."
2831 (let ((inhibit-read-only t))
2832 (mapc
2833 (lambda (o)
2834 (when (equal (overlay-buffer o) (current-buffer))
2835 (put-text-property
2836 (overlay-start o) (overlay-end o)
2837 'org-filtered t)))
2838 org-agenda-filter-overlays)))
2840 (defun org-agenda-unmark-filtered-text ()
2841 "Remove the filtering text property."
2842 (let ((inhibit-read-only t))
2843 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2845 (defun org-agenda-remove-marked-text (property &optional value)
2846 "Delete all text marked with VALUE of PROPERTY.
2847 VALUE defaults to t."
2848 (let (beg)
2849 (setq value (or value t))
2850 (while (setq beg (text-property-any (point-min) (point-max)
2851 property value))
2852 (delete-region
2853 beg (or (next-single-property-change beg 'org-filtered)
2854 (point-max))))))
2856 (defun org-agenda-add-entry-text ()
2857 "Add entry text to agenda lines.
2858 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2859 entry text following headings shown in the agenda.
2860 Drawers will be excluded, also the line with scheduling/deadline info."
2861 (when (and (> org-agenda-add-entry-text-maxlines 0)
2862 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2863 (let (m txt)
2864 (goto-char (point-min))
2865 (while (not (eobp))
2866 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2867 (beginning-of-line 2)
2868 (setq txt (org-agenda-get-some-entry-text
2869 m org-agenda-add-entry-text-maxlines " > "))
2870 (end-of-line 1)
2871 (if (string-match "\\S-" txt)
2872 (insert "\n" txt)
2873 (or (eobp) (forward-char 1))))))))
2875 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2876 &rest keep)
2877 "Extract entry text from MARKER, at most N-LINES lines.
2878 This will ignore drawers etc, just get the text.
2879 If INDENT is given, prefix every line with this string. If KEEP is
2880 given, it is a list of symbols, defining stuff that should not be
2881 removed from the entry content. Currently only `planning' is allowed here."
2882 (let (txt drawer-re kwd-time-re ind)
2883 (save-excursion
2884 (with-current-buffer (marker-buffer marker)
2885 (if (not (org-mode-p))
2886 (setq txt "")
2887 (save-excursion
2888 (save-restriction
2889 (widen)
2890 (goto-char marker)
2891 (end-of-line 1)
2892 (setq txt (buffer-substring
2893 (min (1+ (point)) (point-max))
2894 (progn (outline-next-heading) (point)))
2895 drawer-re org-drawer-regexp
2896 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2897 ".*\n?"))
2898 (with-temp-buffer
2899 (insert txt)
2900 (when org-agenda-add-entry-text-descriptive-links
2901 (goto-char (point-min))
2902 (while (org-activate-bracket-links (point-max))
2903 (add-text-properties (match-beginning 0) (match-end 0)
2904 '(face org-link))))
2905 (goto-char (point-min))
2906 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2907 (set-text-properties (match-beginning 0) (match-end 0)
2908 nil))
2909 (goto-char (point-min))
2910 (while (re-search-forward drawer-re nil t)
2911 (delete-region
2912 (match-beginning 0)
2913 (progn (re-search-forward
2914 "^[ \t]*:END:.*\n?" nil 'move)
2915 (point))))
2916 (unless (member 'planning keep)
2917 (goto-char (point-min))
2918 (while (re-search-forward kwd-time-re nil t)
2919 (replace-match "")))
2920 (goto-char (point-min))
2921 (when org-agenda-entry-text-exclude-regexps
2922 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2923 (while (setq re (pop re-list))
2924 (goto-char (point-min))
2925 (while (re-search-forward re nil t)
2926 (replace-match "")))))
2927 (goto-char (point-max))
2928 (skip-chars-backward " \t\n")
2929 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2931 ;; find and remove min common indentation
2932 (goto-char (point-min))
2933 (untabify (point-min) (point-max))
2934 (setq ind (org-get-indentation))
2935 (while (not (eobp))
2936 (unless (looking-at "[ \t]*$")
2937 (setq ind (min ind (org-get-indentation))))
2938 (beginning-of-line 2))
2939 (goto-char (point-min))
2940 (while (not (eobp))
2941 (unless (looking-at "[ \t]*$")
2942 (move-to-column ind)
2943 (delete-region (point-at-bol) (point)))
2944 (beginning-of-line 2))
2946 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2948 (goto-char (point-min))
2949 (when indent
2950 (while (and (not (eobp)) (re-search-forward "^" nil t))
2951 (replace-match indent t t)))
2952 (goto-char (point-min))
2953 (while (looking-at "[ \t]*\n") (replace-match ""))
2954 (goto-char (point-max))
2955 (when (> (org-current-line)
2956 n-lines)
2957 (org-goto-line (1+ n-lines))
2958 (backward-char 1))
2959 (setq txt (buffer-substring (point-min) (point)))))))))
2960 txt))
2962 (defun org-agenda-collect-markers ()
2963 "Collect the markers pointing to entries in the agenda buffer."
2964 (let (m markers)
2965 (save-excursion
2966 (goto-char (point-min))
2967 (while (not (eobp))
2968 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2969 (org-get-at-bol 'org-marker)))
2970 (push m markers))
2971 (beginning-of-line 2)))
2972 (nreverse markers)))
2974 (defun org-create-marker-find-array (marker-list)
2975 "Create a alist of files names with all marker positions in that file."
2976 (let (f tbl m a p)
2977 (while (setq m (pop marker-list))
2978 (setq p (marker-position m)
2979 f (buffer-file-name (or (buffer-base-buffer
2980 (marker-buffer m))
2981 (marker-buffer m))))
2982 (if (setq a (assoc f tbl))
2983 (push (marker-position m) (cdr a))
2984 (push (list f p) tbl)))
2985 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2986 tbl)))
2988 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2989 (defun org-check-agenda-marker-table ()
2990 "Check of the current entry is on the marker list."
2991 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2993 (and (setq a (assoc file org-agenda-marker-table))
2994 (save-match-data
2995 (save-excursion
2996 (org-back-to-heading t)
2997 (member (point) (cdr a)))))))
2999 (defun org-check-for-org-mode ()
3000 "Make sure current buffer is in org-mode. Error if not."
3001 (or (org-mode-p)
3002 (error "Cannot execute org-mode agenda command on buffer in %s"
3003 major-mode)))
3005 (defun org-fit-agenda-window ()
3006 "Fit the window to the buffer size."
3007 (and (memq org-agenda-window-setup '(reorganize-frame))
3008 (fboundp 'fit-window-to-buffer)
3009 (org-fit-window-to-buffer
3011 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3012 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3014 ;;; Agenda prepare and finalize
3016 (defvar org-agenda-multi nil) ; dynamically scoped
3017 (defvar org-agenda-buffer-name "*Org Agenda*")
3018 (defvar org-pre-agenda-window-conf nil)
3019 (defvar org-agenda-columns-active nil)
3020 (defvar org-agenda-name nil)
3021 (defvar org-agenda-filter nil)
3022 (defvar org-agenda-filter-while-redo nil)
3023 (defvar org-agenda-filter-preset nil
3024 "A preset of the tags filter used for secondary agenda filtering.
3025 This must be a list of strings, each string must be a single tag preceded
3026 by \"+\" or \"-\".
3027 This variable should not be set directly, but agenda custom commands can
3028 bind it in the options section. The preset filter is a global property of
3029 the entire agenda view. In a block agenda, it will not work reliably to
3030 define a filter for one of the individual blocks. You need to set it in
3031 the global options and expect it to be applied to the entire view.")
3033 (defun org-prepare-agenda (&optional name)
3034 (setq org-todo-keywords-for-agenda nil)
3035 (setq org-done-keywords-for-agenda nil)
3036 (setq org-drawers-for-agenda nil)
3037 (unless org-agenda-persistent-filter
3038 (setq org-agenda-filter nil))
3039 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3040 (if org-agenda-multi
3041 (progn
3042 (setq buffer-read-only nil)
3043 (goto-char (point-max))
3044 (unless (or (bobp) org-agenda-compact-blocks
3045 (not org-agenda-block-separator))
3046 (insert "\n"
3047 (if (stringp org-agenda-block-separator)
3048 org-agenda-block-separator
3049 (make-string (window-width) org-agenda-block-separator))
3050 "\n"))
3051 (narrow-to-region (point) (point-max)))
3052 (org-agenda-reset-markers)
3053 (setq org-agenda-contributing-files nil)
3054 (setq org-agenda-columns-active nil)
3055 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3056 (setq org-todo-keywords-for-agenda
3057 (org-uniquify org-todo-keywords-for-agenda))
3058 (setq org-done-keywords-for-agenda
3059 (org-uniquify org-done-keywords-for-agenda))
3060 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3061 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3062 (awin (get-buffer-window abuf)))
3063 (cond
3064 ((equal (current-buffer) abuf) nil)
3065 (awin (select-window awin))
3066 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3067 ((equal org-agenda-window-setup 'current-window)
3068 (switch-to-buffer abuf))
3069 ((equal org-agenda-window-setup 'other-window)
3070 (org-switch-to-buffer-other-window abuf))
3071 ((equal org-agenda-window-setup 'other-frame)
3072 (switch-to-buffer-other-frame abuf))
3073 ((equal org-agenda-window-setup 'reorganize-frame)
3074 (delete-other-windows)
3075 (org-switch-to-buffer-other-window abuf)))
3076 ;; additional test in case agenda is invoked from within agenda
3077 ;; buffer via elisp link
3078 (unless (equal (current-buffer) abuf)
3079 (switch-to-buffer abuf)))
3080 (setq buffer-read-only nil)
3081 (let ((inhibit-read-only t)) (erase-buffer))
3082 (org-agenda-mode)
3083 (and name (not org-agenda-name)
3084 (org-set-local 'org-agenda-name name)))
3085 (setq buffer-read-only nil))
3087 (defun org-finalize-agenda ()
3088 "Finishing touch for the agenda buffer, called just before displaying it."
3089 (unless org-agenda-multi
3090 (save-excursion
3091 (let ((inhibit-read-only t))
3092 (goto-char (point-min))
3093 (while (org-activate-bracket-links (point-max))
3094 (add-text-properties (match-beginning 0) (match-end 0)
3095 '(face org-link)))
3096 (org-agenda-align-tags)
3097 (unless org-agenda-with-colors
3098 (remove-text-properties (point-min) (point-max) '(face nil))))
3099 (if (and (boundp 'org-agenda-overriding-columns-format)
3100 org-agenda-overriding-columns-format)
3101 (org-set-local 'org-agenda-overriding-columns-format
3102 org-agenda-overriding-columns-format))
3103 (if (and (boundp 'org-agenda-view-columns-initially)
3104 org-agenda-view-columns-initially)
3105 (org-agenda-columns))
3106 (when org-agenda-fontify-priorities
3107 (org-agenda-fontify-priorities))
3108 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3109 (org-agenda-dim-blocked-tasks))
3110 (org-agenda-mark-clocking-task)
3111 (when org-agenda-entry-text-mode
3112 (org-agenda-entry-text-hide)
3113 (org-agenda-entry-text-show))
3114 (if (functionp 'org-habit-insert-consistency-graphs)
3115 (org-habit-insert-consistency-graphs))
3116 (run-hooks 'org-finalize-agenda-hook)
3117 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3118 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3119 (org-agenda-filter-apply org-agenda-filter))
3122 (defun org-agenda-mark-clocking-task ()
3123 "Mark the current clock entry in the agenda if it is present."
3124 (mapc (lambda (o)
3125 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3126 (delete-overlay o)))
3127 (overlays-in (point-min) (point-max)))
3128 (when (marker-buffer org-clock-hd-marker)
3129 (save-excursion
3130 (goto-char (point-min))
3131 (let (s ov)
3132 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3133 (goto-char s)
3134 (when (equal (org-get-at-bol 'org-hd-marker)
3135 org-clock-hd-marker)
3136 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3137 (overlay-put ov 'type 'org-agenda-clocking)
3138 (overlay-put ov 'face 'org-agenda-clocking)
3139 (overlay-put ov 'help-echo
3140 "The clock is running in this item")))))))
3142 (defun org-agenda-fontify-priorities ()
3143 "Make highest priority lines bold, and lowest italic."
3144 (interactive)
3145 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3146 (delete-overlay o)))
3147 (overlays-in (point-min) (point-max)))
3148 (save-excursion
3149 (let ((inhibit-read-only t)
3150 b e p ov h l)
3151 (goto-char (point-min))
3152 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3153 (setq h (or (get-char-property (point) 'org-highest-priority)
3154 org-highest-priority)
3155 l (or (get-char-property (point) 'org-lowest-priority)
3156 org-lowest-priority)
3157 p (string-to-char (match-string 1))
3158 b (match-beginning 0)
3159 e (if (eq org-agenda-fontify-priorities 'cookies)
3160 (match-end 0)
3161 (point-at-eol))
3162 ov (make-overlay b e))
3163 (overlay-put
3164 ov 'face
3165 (cond ((org-face-from-face-or-color
3166 'priority nil
3167 (cdr (assoc p org-priority-faces))))
3168 ((and (listp org-agenda-fontify-priorities)
3169 (org-face-from-face-or-color
3170 'priority nil
3171 (cdr (assoc p org-agenda-fontify-priorities)))))
3172 ((equal p l) 'italic)
3173 ((equal p h) 'bold)))
3174 (overlay-put ov 'org-type 'org-priority)))))
3176 (defun org-agenda-dim-blocked-tasks ()
3177 "Dim currently blocked TODO's in the agenda display."
3178 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3179 (delete-overlay o)))
3180 (overlays-in (point-min) (point-max)))
3181 (save-excursion
3182 (let ((inhibit-read-only t)
3183 (org-depend-tag-blocked nil)
3184 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3185 org-blocked-by-checkboxes
3186 invis1 b e p ov h l)
3187 (goto-char (point-min))
3188 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3189 (and pos (goto-char (1+ pos))))
3190 (setq org-blocked-by-checkboxes nil invis1 invis)
3191 (let ((marker (org-get-at-bol 'org-hd-marker)))
3192 (when (and marker
3193 (not (with-current-buffer (marker-buffer marker)
3194 (save-excursion
3195 (goto-char marker)
3196 (if (org-entry-get nil "NOBLOCKING")
3197 t ;; Never block this entry
3198 (run-hook-with-args-until-failure
3199 'org-blocker-hook
3200 (list :type 'todo-state-change
3201 :position marker
3202 :from 'todo
3203 :to 'done)))))))
3204 (if org-blocked-by-checkboxes (setq invis1 nil))
3205 (setq b (if invis1
3206 (max (point-min) (1- (point-at-bol)))
3207 (point-at-bol))
3208 e (point-at-eol)
3209 ov (make-overlay b e))
3210 (if invis1
3211 (overlay-put ov 'invisible t)
3212 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3213 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3215 (defvar org-agenda-skip-function nil
3216 "Function to be called at each match during agenda construction.
3217 If this function returns nil, the current match should not be skipped.
3218 Otherwise, the function must return a position from where the search
3219 should be continued.
3220 This may also be a Lisp form, it will be evaluated.
3221 Never set this variable using `setq' or so, because then it will apply
3222 to all future agenda commands. If you do want a global skipping condition,
3223 use the option `org-agenda-skip-function-global' instead.
3224 The correct usage for `org-agenda-skip-function' is to bind it with
3225 `let' to scope it dynamically into the agenda-constructing command.
3226 A good way to set it is through options in `org-agenda-custom-commands'.")
3228 (defun org-agenda-skip ()
3229 "Throw to `:skip' in places that should be skipped.
3230 Also moves point to the end of the skipped region, so that search can
3231 continue from there."
3232 (let ((p (point-at-bol)) to)
3233 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3234 (get-text-property p :org-archived)
3235 (org-end-of-subtree t)
3236 (throw :skip t))
3237 (and org-agenda-skip-comment-trees
3238 (get-text-property p :org-comment)
3239 (org-end-of-subtree t)
3240 (throw :skip t))
3241 (if (equal (char-after p) ?#) (throw :skip t))
3242 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3243 (org-agenda-skip-eval org-agenda-skip-function)))
3244 (goto-char to)
3245 (throw :skip t))))
3247 (defun org-agenda-skip-eval (form)
3248 "If FORM is a function or a list, call (or eval) is and return result.
3249 `save-excursion' and `save-match-data' are wrapped around the call, so point
3250 and match data are returned to the previous state no matter what these
3251 functions do."
3252 (let (fp)
3253 (and form
3254 (or (setq fp (functionp form))
3255 (consp form))
3256 (save-excursion
3257 (save-match-data
3258 (if fp
3259 (funcall form)
3260 (eval form)))))))
3262 (defvar org-agenda-markers nil
3263 "List of all currently active markers created by `org-agenda'.")
3264 (defvar org-agenda-last-marker-time (org-float-time)
3265 "Creation time of the last agenda marker.")
3267 (defun org-agenda-new-marker (&optional pos)
3268 "Return a new agenda marker.
3269 Org-mode keeps a list of these markers and resets them when they are
3270 no longer in use."
3271 (let ((m (copy-marker (or pos (point)))))
3272 (setq org-agenda-last-marker-time (org-float-time))
3273 (push m org-agenda-markers)
3276 (defun org-agenda-reset-markers ()
3277 "Reset markers created by `org-agenda'."
3278 (while org-agenda-markers
3279 (move-marker (pop org-agenda-markers) nil)))
3281 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3282 "Save relative positions of markers in region."
3283 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3284 org-agenda-markers))
3286 ;;; Entry text mode
3288 (defun org-agenda-entry-text-show-here ()
3289 "Add some text from the entry as context to the current line."
3290 (let (m txt o)
3291 (setq m (org-get-at-bol 'org-hd-marker))
3292 (unless (marker-buffer m)
3293 (error "No marker points to an entry here"))
3294 (setq txt (concat "\n" (org-no-properties
3295 (org-agenda-get-some-entry-text
3296 m org-agenda-entry-text-maxlines " > "))))
3297 (when (string-match "\\S-" txt)
3298 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3299 (overlay-put o 'evaporate t)
3300 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3301 (overlay-put o 'after-string txt))))
3303 (defun org-agenda-entry-text-show ()
3304 "Add entry context for all agenda lines."
3305 (interactive)
3306 (save-excursion
3307 (goto-char (point-max))
3308 (beginning-of-line 1)
3309 (while (not (bobp))
3310 (when (org-get-at-bol 'org-hd-marker)
3311 (org-agenda-entry-text-show-here))
3312 (beginning-of-line 0))))
3314 (defun org-agenda-entry-text-hide ()
3315 "Remove any shown entry context."
3316 (delq nil
3317 (mapcar (lambda (o)
3318 (if (eq (overlay-get o 'org-overlay-type)
3319 'agenda-entry-content)
3320 (progn (delete-overlay o) t)))
3321 (overlays-in (point-min) (point-max)))))
3323 (defun org-agenda-get-day-face (date)
3324 "Return the face DATE should be displayed with."
3325 (or (and (functionp org-agenda-day-face-function)
3326 (funcall org-agenda-day-face-function date))
3327 (cond ((org-agenda-todayp date)
3328 'org-agenda-date-today)
3329 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3330 'org-agenda-date-weekend)
3331 (t 'org-agenda-date))))
3333 ;;; Agenda timeline
3335 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3337 (defun org-timeline (&optional include-all)
3338 "Show a time-sorted view of the entries in the current org file.
3339 Only entries with a time stamp of today or later will be listed. With
3340 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3341 under the current date.
3342 If the buffer contains an active region, only check the region for
3343 dates."
3344 (interactive "P")
3345 (org-compile-prefix-format 'timeline)
3346 (org-set-sorting-strategy 'timeline)
3347 (let* ((dopast t)
3348 (dotodo include-all)
3349 (doclosed org-agenda-show-log)
3350 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3351 (current-buffer))))
3352 (date (calendar-current-date))
3353 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3354 (end (if (org-region-active-p) (region-end) (point-max)))
3355 (day-numbers (org-get-all-dates beg end 'no-ranges
3356 t doclosed ; always include today
3357 org-timeline-show-empty-dates))
3358 (org-deadline-warning-days 0)
3359 (org-agenda-only-exact-dates t)
3360 (today (org-today))
3361 (past t)
3362 args
3363 s e rtn d emptyp)
3364 (setq org-agenda-redo-command
3365 (list 'progn
3366 (list 'org-switch-to-buffer-other-window (current-buffer))
3367 (list 'org-timeline (list 'quote include-all))))
3368 (if (not dopast)
3369 ;; Remove past dates from the list of dates.
3370 (setq day-numbers (delq nil (mapcar (lambda(x)
3371 (if (>= x today) x nil))
3372 day-numbers))))
3373 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3374 (if doclosed (push :closed args))
3375 (push :timestamp args)
3376 (push :deadline args)
3377 (push :scheduled args)
3378 (push :sexp args)
3379 (if dotodo (push :todo args))
3380 (insert "Timeline of file " entry "\n")
3381 (add-text-properties (point-min) (point)
3382 (list 'face 'org-agenda-structure))
3383 (org-agenda-mark-header-line (point-min))
3384 (while (setq d (pop day-numbers))
3385 (if (and (listp d) (eq (car d) :omitted))
3386 (progn
3387 (setq s (point))
3388 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3389 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3390 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3391 (if (and (>= d today)
3392 dopast
3393 past)
3394 (progn
3395 (setq past nil)
3396 (insert (make-string 79 ?-) "\n")))
3397 (setq date (calendar-gregorian-from-absolute d))
3398 (setq s (point))
3399 (setq rtn (and (not emptyp)
3400 (apply 'org-agenda-get-day-entries entry
3401 date args)))
3402 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3403 (progn
3404 (insert
3405 (if (stringp org-agenda-format-date)
3406 (format-time-string org-agenda-format-date
3407 (org-time-from-absolute date))
3408 (funcall org-agenda-format-date date))
3409 "\n")
3410 (put-text-property s (1- (point)) 'face
3411 (org-agenda-get-day-face date))
3412 (put-text-property s (1- (point)) 'org-date-line t)
3413 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3414 (if (equal d today)
3415 (put-text-property s (1- (point)) 'org-today t))
3416 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3417 (put-text-property s (1- (point)) 'day d)))))
3418 (goto-char (point-min))
3419 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3420 (point-min)))
3421 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3422 (org-finalize-agenda)
3423 (setq buffer-read-only t)))
3425 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3426 "Return a list of all relevant day numbers from BEG to END buffer positions.
3427 If NO-RANGES is non-nil, include only the start and end dates of a range,
3428 not every single day in the range. If FORCE-TODAY is non-nil, make
3429 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3430 inactive time stamps (those in square brackets) are included.
3431 When EMPTY is non-nil, also include days without any entries."
3432 (let ((re (concat
3433 (if pre-re pre-re "")
3434 (if inactive org-ts-regexp-both org-ts-regexp)))
3435 dates dates1 date day day1 day2 ts1 ts2)
3436 (if force-today
3437 (setq dates (list (org-today))))
3438 (save-excursion
3439 (goto-char beg)
3440 (while (re-search-forward re end t)
3441 (setq day (time-to-days (org-time-string-to-time
3442 (substring (match-string 1) 0 10))))
3443 (or (memq day dates) (push day dates)))
3444 (unless no-ranges
3445 (goto-char beg)
3446 (while (re-search-forward org-tr-regexp end t)
3447 (setq ts1 (substring (match-string 1) 0 10)
3448 ts2 (substring (match-string 2) 0 10)
3449 day1 (time-to-days (org-time-string-to-time ts1))
3450 day2 (time-to-days (org-time-string-to-time ts2)))
3451 (while (< (setq day1 (1+ day1)) day2)
3452 (or (memq day1 dates) (push day1 dates)))))
3453 (setq dates (sort dates '<))
3454 (when empty
3455 (while (setq day (pop dates))
3456 (setq day2 (car dates))
3457 (push day dates1)
3458 (when (and day2 empty)
3459 (if (or (eq empty t)
3460 (and (numberp empty) (<= (- day2 day) empty)))
3461 (while (< (setq day (1+ day)) day2)
3462 (push (list day) dates1))
3463 (push (cons :omitted (- day2 day)) dates1))))
3464 (setq dates (nreverse dates1)))
3465 dates)))
3467 ;;; Agenda Daily/Weekly
3469 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3470 "Start day for the agenda view.
3471 Custom commands can set this variable in the options section.")
3472 (defvar org-starting-day nil) ; local variable in the agenda buffer
3473 (defvar org-agenda-current-span nil
3474 "The current span used in the agenda view.") ; local variable in the agenda buffer
3475 (defvar org-include-all-loc nil) ; local variable
3477 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3478 "List of types searched for when creating the daily/weekly agenda.
3479 This variable is a list of symbols that controls the types of
3480 items that appear in the daily/weekly agenda. Allowed symbols in this
3481 list are are
3483 :timestamp List items containing a date stamp or date range matching
3484 the selected date. This includes sexp entries in
3485 angular brackets.
3487 :sexp List entries resulting from plain diary-like sexps.
3489 :deadline List deadline due on that date. When the date is today,
3490 also list any deadlines past due, or due within
3491 `org-deadline-warning-days'. `:deadline' must appear before
3492 `:scheduled' if the setting of
3493 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3494 any effect.
3496 :scheduled List all items which are scheduled for the given date.
3497 The diary for *today* also contains items which were
3498 scheduled earlier and are not yet marked DONE.
3500 By default, all four types are turned on.
3502 Never set this variable globally using `setq', because then it
3503 will apply to all future agenda commands. Instead, bind it with
3504 `let' to scope it dynamically into the agenda-constructing
3505 command. A good way to set it is through options in
3506 `org-agenda-custom-commands'. For a more flexible (though
3507 somewhat less efficient) way of determining what is included in
3508 the daily/weekly agenda, see `org-agenda-skip-function'.")
3510 ;;;###autoload
3511 (defun org-agenda-list (&optional include-all start-day span)
3512 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3513 The view will be for the current day or week, but from the overview buffer
3514 you will be able to go to other days/weeks.
3516 With a numeric prefix argument in an interactive call, the agenda will
3517 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3518 the number of days. SPAN defaults to `org-agenda-span'.
3520 START-DAY defaults to TODAY, or to the most recent match for the weekday
3521 given in `org-agenda-start-on-weekday'."
3522 (interactive "P")
3523 (if (and (integerp include-all) (> include-all 0))
3524 (setq span include-all include-all nil))
3525 (setq start-day (or start-day org-agenda-start-day))
3526 (if org-agenda-overriding-arguments
3527 (setq include-all (car org-agenda-overriding-arguments)
3528 start-day (nth 1 org-agenda-overriding-arguments)
3529 span (nth 2 org-agenda-overriding-arguments)))
3530 (if (stringp start-day)
3531 ;; Convert to an absolute day number
3532 (setq start-day (time-to-days (org-read-date nil t start-day))))
3533 (setq org-agenda-last-arguments (list include-all start-day span))
3534 (org-compile-prefix-format 'agenda)
3535 (org-set-sorting-strategy 'agenda)
3536 (let* ((span (org-agenda-ndays-to-span
3537 (or span org-agenda-ndays org-agenda-span)))
3538 (today (org-today))
3539 (sd (or start-day today))
3540 (ndays (org-agenda-span-to-ndays span sd))
3541 (org-agenda-start-on-weekday
3542 (if (eq ndays 7)
3543 org-agenda-start-on-weekday))
3544 (thefiles (org-agenda-files nil 'ifmode))
3545 (files thefiles)
3546 (start (if (or (null org-agenda-start-on-weekday)
3547 (< ndays 7))
3549 (let* ((nt (calendar-day-of-week
3550 (calendar-gregorian-from-absolute sd)))
3551 (n1 org-agenda-start-on-weekday)
3552 (d (- nt n1)))
3553 (- sd (+ (if (< d 0) 7 0) d)))))
3554 (day-numbers (list start))
3555 (day-cnt 0)
3556 (inhibit-redisplay (not debug-on-error))
3557 s e rtn rtnall file date d start-pos end-pos todayp
3558 clocktable-start clocktable-end filter)
3559 (setq org-agenda-redo-command
3560 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3561 (dotimes (n (1- ndays))
3562 (push (1+ (car day-numbers)) day-numbers))
3563 (setq day-numbers (nreverse day-numbers))
3564 (setq clocktable-start (car day-numbers)
3565 clocktable-end (1+ (or (org-last day-numbers) 0)))
3566 (org-prepare-agenda "Day/Week")
3567 (org-set-local 'org-starting-day (car day-numbers))
3568 (org-set-local 'org-include-all-loc include-all)
3569 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3570 (unless org-agenda-compact-blocks
3571 (let* ((d1 (car day-numbers))
3572 (d2 (org-last day-numbers))
3573 (w1 (org-days-to-iso-week d1))
3574 (w2 (org-days-to-iso-week d2)))
3575 (setq s (point))
3576 (if org-agenda-overriding-header
3577 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3578 nil 'face 'org-agenda-structure) "\n")
3579 (insert (org-agenda-span-name span)
3580 "-agenda"
3581 (if (< (- d2 d1) 350)
3582 (if (= w1 w2)
3583 (format " (W%02d)" w1)
3584 (format " (W%02d-W%02d)" w1 w2))
3586 ":\n")))
3587 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3588 'org-date-line t))
3589 (org-agenda-mark-header-line s))
3590 (while (setq d (pop day-numbers))
3591 (setq date (calendar-gregorian-from-absolute d)
3592 s (point))
3593 (if (or (setq todayp (= d today))
3594 (and (not start-pos) (= d sd)))
3595 (setq start-pos (point))
3596 (if (and start-pos (not end-pos))
3597 (setq end-pos (point))))
3598 (setq files thefiles
3599 rtnall nil)
3600 (while (setq file (pop files))
3601 (catch 'nextfile
3602 (org-check-agenda-file file)
3603 (let ((org-agenda-entry-types org-agenda-entry-types))
3604 (unless org-agenda-include-deadlines
3605 (setq org-agenda-entry-types
3606 (delq :deadline org-agenda-entry-types)))
3607 (cond
3608 ((memq org-agenda-show-log '(only clockcheck))
3609 (setq rtn (org-agenda-get-day-entries
3610 file date :closed)))
3611 (org-agenda-show-log
3612 (setq rtn (apply 'org-agenda-get-day-entries
3613 file date
3614 (append '(:closed) org-agenda-entry-types))))
3616 (setq rtn (apply 'org-agenda-get-day-entries
3617 file date
3618 org-agenda-entry-types)))))
3619 (setq rtnall (append rtnall rtn))))
3620 (if org-agenda-include-diary
3621 (let ((org-agenda-search-headline-for-time t))
3622 (require 'diary-lib)
3623 (setq rtn (org-get-entries-from-diary date))
3624 (setq rtnall (append rtnall rtn))))
3625 (if (or rtnall org-agenda-show-all-dates)
3626 (progn
3627 (setq day-cnt (1+ day-cnt))
3628 (insert
3629 (if (stringp org-agenda-format-date)
3630 (format-time-string org-agenda-format-date
3631 (org-time-from-absolute date))
3632 (funcall org-agenda-format-date date))
3633 "\n")
3634 (put-text-property s (1- (point)) 'face
3635 (org-agenda-get-day-face date))
3636 (put-text-property s (1- (point)) 'org-date-line t)
3637 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3638 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3639 (when todayp
3640 (put-text-property s (1- (point)) 'org-today t))
3641 (if rtnall (insert
3642 (org-finalize-agenda-entries
3643 (org-agenda-add-time-grid-maybe
3644 rtnall ndays todayp))
3645 "\n"))
3646 (put-text-property s (1- (point)) 'day d)
3647 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3648 (when (and org-agenda-clockreport-mode clocktable-start)
3649 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3650 ;; the above line is to ensure the restricted range!
3651 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3652 tbl)
3653 (setq p (org-plist-delete p :block))
3654 (setq p (plist-put p :tstart clocktable-start))
3655 (setq p (plist-put p :tend clocktable-end))
3656 (setq p (plist-put p :scope 'agenda))
3657 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3658 (setq filter (or org-agenda-filter-while-redo
3659 (get 'org-agenda-filter :preset-filter))))
3660 (setq p (plist-put p :tags (mapconcat (lambda (x)
3661 (if (string-match "[<>=]" x)
3664 filter ""))))
3665 (setq tbl (apply 'org-get-clocktable p))
3666 (insert tbl)))
3667 (goto-char (point-min))
3668 (or org-agenda-multi (org-fit-agenda-window))
3669 (unless (and (pos-visible-in-window-p (point-min))
3670 (pos-visible-in-window-p (point-max)))
3671 (goto-char (1- (point-max)))
3672 (recenter -1)
3673 (if (not (pos-visible-in-window-p (or start-pos 1)))
3674 (progn
3675 (goto-char (or start-pos 1))
3676 (recenter 1))))
3677 (goto-char (or start-pos 1))
3678 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3679 (if (eq org-agenda-show-log 'clockcheck)
3680 (org-agenda-show-clocking-issues))
3681 (org-finalize-agenda)
3682 (setq buffer-read-only t)
3683 (message "")))
3685 (defun org-agenda-ndays-to-span (n)
3686 "Return a span symbol for a span of N days, or N if none matches."
3687 (cond ((symbolp n) n)
3688 ((= n 1) 'day)
3689 ((= n 7) 'week)
3690 (t n)))
3692 (defun org-agenda-span-to-ndays (span start-day)
3693 "Return ndays from SPAN starting at START-DAY."
3694 (cond ((numberp span) span)
3695 ((eq span 'day) 1)
3696 ((eq span 'week) 7)
3697 ((eq span 'month)
3698 (let ((date (calendar-gregorian-from-absolute start-day)))
3699 (calendar-last-day-of-month (car date) (caddr date))))
3700 ((eq span 'year)
3701 (let ((date (calendar-gregorian-from-absolute start-day)))
3702 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3704 (defun org-agenda-span-name (span)
3705 "Return a SPAN name."
3706 (if (null span)
3708 (if (symbolp span)
3709 (capitalize (symbol-name span))
3710 (format "%d days" span))))
3712 ;;; Agenda word search
3714 (defvar org-agenda-search-history nil)
3715 (defvar org-todo-only nil)
3717 (defvar org-search-syntax-table nil
3718 "Special syntax table for org-mode search.
3719 In this table, we have single quotes not as word constituents, to
3720 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3722 (defun org-search-syntax-table ()
3723 (unless org-search-syntax-table
3724 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3725 (modify-syntax-entry ?' "." org-search-syntax-table)
3726 (modify-syntax-entry ?` "." org-search-syntax-table))
3727 org-search-syntax-table)
3729 (defvar org-agenda-last-search-view-search-was-boolean nil)
3731 ;;;###autoload
3732 (defun org-search-view (&optional todo-only string edit-at)
3733 "Show all entries that contain a phrase or words or regular expressions.
3735 With optional prefix argument TODO-ONLY, only consider entries that are
3736 TODO entries. The argument STRING can be used to pass a default search
3737 string into this function. If EDIT-AT is non-nil, it means that the
3738 user should get a chance to edit this string, with cursor at position
3739 EDIT-AT.
3741 The search string can be viewed either as a phrase that should be found as
3742 is, or it can be broken into a number of snippets, each of which must match
3743 in a Boolean way to select an entry. The default depends on the variable
3744 `org-agenda-search-view-always-boolean'.
3745 Even if this is turned off (the default) you can always switch to
3746 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3748 The default is a direct search of the whole phrase, where each space in
3749 the search string can expand to an arbitrary amount of whitespace,
3750 including newlines.
3752 If using a Boolean search, the search string is split on whitespace and
3753 each snippet is searched separately, with logical AND to select an entry.
3754 Words prefixed with a minus must *not* occur in the entry. Words without
3755 a prefix or prefixed with a plus must occur in the entry. Matching is
3756 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3757 match whole words, not parts of a word) if
3758 `org-agenda-search-view-force-full-words' is set (default is nil).
3760 Boolean search snippets enclosed by curly braces are interpreted as
3761 regular expressions that must or (when preceded with \"-\") must not
3762 match in the entry. Snippets enclosed into double quotes will be taken
3763 as a whole, to include whitespace.
3765 - If the search string starts with an asterisk, search only in headlines.
3766 - If (possibly after the leading star) the search string starts with an
3767 exclamation mark, this also means to look at TODO entries only, an effect
3768 that can also be achieved with a prefix argument.
3769 - If (possibly after star and exclamation mark) the search string starts
3770 with a colon, this will mean that the (non-regexp) snippets of the
3771 Boolean search must match as full words.
3773 This command searches the agenda files, and in addition the files listed
3774 in `org-agenda-text-search-extra-files'."
3775 (interactive "P")
3776 (org-compile-prefix-format 'search)
3777 (org-set-sorting-strategy 'search)
3778 (org-prepare-agenda "SEARCH")
3779 (let* ((props (list 'face nil
3780 'done-face 'org-agenda-done
3781 'org-not-done-regexp org-not-done-regexp
3782 'org-todo-regexp org-todo-regexp
3783 'org-complex-heading-regexp org-complex-heading-regexp
3784 'mouse-face 'highlight
3785 'help-echo (format "mouse-2 or RET jump to location")))
3786 (full-words org-agenda-search-view-force-full-words)
3787 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3788 regexp rtn rtnall files file pos
3789 marker category tags c neg re boolean
3790 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3791 (unless (and (not edit-at)
3792 (stringp string)
3793 (string-match "\\S-" string))
3794 (setq string (read-string
3795 (if org-agenda-search-view-always-boolean
3796 "[+-]Word/{Regexp} ...: "
3797 "Phrase, or [+-]Word/{Regexp} ...: ")
3798 (cond
3799 ((integerp edit-at) (cons string edit-at))
3800 (edit-at string))
3801 'org-agenda-search-history)))
3802 (org-set-local 'org-todo-only todo-only)
3803 (setq org-agenda-redo-command
3804 (list 'org-search-view (if todo-only t nil) string
3805 '(if current-prefix-arg 1 nil)))
3806 (setq org-agenda-query-string string)
3808 (if (equal (string-to-char string) ?*)
3809 (setq hdl-only t
3810 words (substring string 1))
3811 (setq words string))
3812 (when (equal (string-to-char words) ?!)
3813 (setq todo-only t
3814 words (substring words 1)))
3815 (when (equal (string-to-char words) ?:)
3816 (setq full-words t
3817 words (substring words 1)))
3818 (if (or org-agenda-search-view-always-boolean
3819 (member (string-to-char words) '(?- ?+ ?\{)))
3820 (setq boolean t))
3821 (setq words (org-split-string words))
3822 (let (www w)
3823 (while (setq w (pop words))
3824 (while (and (string-match "\\\\\\'" w) words)
3825 (setq w (concat (substring w 0 -1) " " (pop words))))
3826 (push w www))
3827 (setq words (nreverse www) www nil)
3828 (while (setq w (pop words))
3829 (when (and (string-match "\\`[-+]?{" w)
3830 (not (string-match "}\\'" w)))
3831 (while (and words (not (string-match "}\\'" (car words))))
3832 (setq w (concat w " " (pop words))))
3833 (setq w (concat w " " (pop words))))
3834 (push w www))
3835 (setq words (nreverse www)))
3836 (setq org-agenda-last-search-view-search-was-boolean boolean)
3837 (when boolean
3838 (let (wds w)
3839 (while (setq w (pop words))
3840 (if (or (equal (substring w 0 1) "\"")
3841 (and (> (length w) 1)
3842 (member (substring w 0 1) '("+" "-"))
3843 (equal (substring w 1 2) "\"")))
3844 (while (and words (not (equal (substring w -1) "\"")))
3845 (setq w (concat w " " (pop words)))))
3846 (and (string-match "\\`\\([-+]?\\)\"" w)
3847 (setq w (replace-match "\\1" nil nil w)))
3848 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3849 (push w wds))
3850 (setq words (nreverse wds))))
3851 (if boolean
3852 (mapc (lambda (w)
3853 (setq c (string-to-char w))
3854 (if (equal c ?-)
3855 (setq neg t w (substring w 1))
3856 (if (equal c ?+)
3857 (setq neg nil w (substring w 1))
3858 (setq neg nil)))
3859 (if (string-match "\\`{.*}\\'" w)
3860 (setq re (substring w 1 -1))
3861 (if full-words
3862 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3863 (setq re (regexp-quote (downcase w)))))
3864 (if neg (push re regexps-) (push re regexps+)))
3865 words)
3866 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3867 regexps+))
3868 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3869 (if (not regexps+)
3870 (setq regexp org-outline-regexp-bol)
3871 (setq regexp (pop regexps+))
3872 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3873 regexp))))
3874 (setq files (org-agenda-files nil 'ifmode))
3875 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3876 (pop org-agenda-text-search-extra-files)
3877 (setq files (org-add-archive-files files)))
3878 (setq files (append files org-agenda-text-search-extra-files)
3879 rtnall nil)
3880 (while (setq file (pop files))
3881 (setq ee nil)
3882 (catch 'nextfile
3883 (org-check-agenda-file file)
3884 (setq buffer (if (file-exists-p file)
3885 (org-get-agenda-file-buffer file)
3886 (error "No such file %s" file)))
3887 (if (not buffer)
3888 ;; If file does not exist, make sure an error message is sent
3889 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3890 file))))
3891 (with-current-buffer buffer
3892 (with-syntax-table (org-search-syntax-table)
3893 (unless (org-mode-p)
3894 (error "Agenda file %s is not in `org-mode'" file))
3895 (let ((case-fold-search t))
3896 (save-excursion
3897 (save-restriction
3898 (if org-agenda-restrict
3899 (narrow-to-region org-agenda-restrict-begin
3900 org-agenda-restrict-end)
3901 (widen))
3902 (goto-char (point-min))
3903 (unless (or (org-on-heading-p)
3904 (outline-next-heading))
3905 (throw 'nextfile t))
3906 (goto-char (max (point-min) (1- (point))))
3907 (while (re-search-forward regexp nil t)
3908 (org-back-to-heading t)
3909 (skip-chars-forward "* ")
3910 (setq beg (point-at-bol)
3911 beg1 (point)
3912 end (progn (outline-next-heading) (point)))
3913 (catch :skip
3914 (goto-char beg)
3915 (org-agenda-skip)
3916 (setq str (buffer-substring-no-properties
3917 (point-at-bol)
3918 (if hdl-only (point-at-eol) end)))
3919 (mapc (lambda (wr) (when (string-match wr str)
3920 (goto-char (1- end))
3921 (throw :skip t)))
3922 regexps-)
3923 (mapc (lambda (wr) (unless (string-match wr str)
3924 (goto-char (1- end))
3925 (throw :skip t)))
3926 (if todo-only
3927 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3928 regexps+)
3929 regexps+))
3930 (goto-char beg)
3931 (setq marker (org-agenda-new-marker (point))
3932 category (org-get-category)
3933 tags (org-get-tags-at (point))
3934 txt (org-format-agenda-item
3936 (buffer-substring-no-properties
3937 beg1 (point-at-eol))
3938 category tags))
3939 (org-add-props txt props
3940 'org-marker marker 'org-hd-marker marker
3941 'org-todo-regexp org-todo-regexp
3942 'org-complex-heading-regexp org-complex-heading-regexp
3943 'priority 1000 'org-category category
3944 'type "search")
3945 (push txt ee)
3946 (goto-char (1- end))))))))))
3947 (setq rtn (nreverse ee))
3948 (setq rtnall (append rtnall rtn)))
3949 (if org-agenda-overriding-header
3950 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3951 nil 'face 'org-agenda-structure) "\n")
3952 (insert "Search words: ")
3953 (add-text-properties (point-min) (1- (point))
3954 (list 'face 'org-agenda-structure))
3955 (setq pos (point))
3956 (insert string "\n")
3957 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3958 (setq pos (point))
3959 (unless org-agenda-multi
3960 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3961 (add-text-properties pos (1- (point))
3962 (list 'face 'org-agenda-structure))))
3963 (org-agenda-mark-header-line (point-min))
3964 (when rtnall
3965 (insert (org-finalize-agenda-entries rtnall) "\n"))
3966 (goto-char (point-min))
3967 (or org-agenda-multi (org-fit-agenda-window))
3968 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3969 (org-finalize-agenda)
3970 (setq buffer-read-only t)))
3972 ;;; Agenda TODO list
3974 (defvar org-select-this-todo-keyword nil)
3975 (defvar org-last-arg nil)
3977 ;;;###autoload
3978 (defun org-todo-list (arg)
3979 "Show all (not done) TODO entries from all agenda file in a single list.
3980 The prefix arg can be used to select a specific TODO keyword and limit
3981 the list to these. When using \\[universal-argument], you will be prompted
3982 for a keyword. A numeric prefix directly selects the Nth keyword in
3983 `org-todo-keywords-1'."
3984 (interactive "P")
3985 (org-compile-prefix-format 'todo)
3986 (org-set-sorting-strategy 'todo)
3987 (org-prepare-agenda "TODO")
3988 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3989 (let* ((today (org-today))
3990 (date (calendar-gregorian-from-absolute today))
3991 (kwds org-todo-keywords-for-agenda)
3992 (completion-ignore-case t)
3993 (org-select-this-todo-keyword
3994 (if (stringp arg) arg
3995 (and arg (integerp arg) (> arg 0)
3996 (nth (1- arg) kwds))))
3997 rtn rtnall files file pos)
3998 (when (equal arg '(4))
3999 (setq org-select-this-todo-keyword
4000 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4001 (mapcar 'list kwds) nil nil)))
4002 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4003 (org-set-local 'org-last-arg arg)
4004 (setq org-agenda-redo-command
4005 '(org-todo-list (or current-prefix-arg org-last-arg)))
4006 (setq files (org-agenda-files nil 'ifmode)
4007 rtnall nil)
4008 (while (setq file (pop files))
4009 (catch 'nextfile
4010 (org-check-agenda-file file)
4011 (setq rtn (org-agenda-get-day-entries file date :todo))
4012 (setq rtnall (append rtnall rtn))))
4013 (if org-agenda-overriding-header
4014 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4015 nil 'face 'org-agenda-structure) "\n")
4016 (insert "Global list of TODO items of type: ")
4017 (add-text-properties (point-min) (1- (point))
4018 (list 'face 'org-agenda-structure
4019 'short-heading
4020 (concat "ToDo: "
4021 (or org-select-this-todo-keyword "ALL"))))
4022 (org-agenda-mark-header-line (point-min))
4023 (setq pos (point))
4024 (insert (or org-select-this-todo-keyword "ALL") "\n")
4025 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4026 (setq pos (point))
4027 (unless org-agenda-multi
4028 (insert "Available with `N r': (0)ALL")
4029 (let ((n 0) s)
4030 (mapc (lambda (x)
4031 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4032 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4033 (insert "\n "))
4034 (insert " " s))
4035 kwds))
4036 (insert "\n"))
4037 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4038 (org-agenda-mark-header-line (point-min))
4039 (when rtnall
4040 (insert (org-finalize-agenda-entries rtnall) "\n"))
4041 (goto-char (point-min))
4042 (or org-agenda-multi (org-fit-agenda-window))
4043 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4044 (org-finalize-agenda)
4045 (setq buffer-read-only t)))
4047 ;;; Agenda tags match
4049 ;;;###autoload
4050 (defun org-tags-view (&optional todo-only match)
4051 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4052 The prefix arg TODO-ONLY limits the search to TODO entries."
4053 (interactive "P")
4054 (org-compile-prefix-format 'tags)
4055 (org-set-sorting-strategy 'tags)
4056 (let* ((org-tags-match-list-sublevels
4057 org-tags-match-list-sublevels)
4058 (completion-ignore-case t)
4059 rtn rtnall files file pos matcher
4060 buffer)
4061 (when (and (stringp match) (not (string-match "\\S-" match)))
4062 (setq match nil))
4063 (setq matcher (org-make-tags-matcher match)
4064 match (car matcher) matcher (cdr matcher))
4065 (org-prepare-agenda (concat "TAGS " match))
4066 (setq org-agenda-query-string match)
4067 (setq org-agenda-redo-command
4068 (list 'org-tags-view (list 'quote todo-only)
4069 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4070 (setq files (org-agenda-files nil 'ifmode)
4071 rtnall nil)
4072 (while (setq file (pop files))
4073 (catch 'nextfile
4074 (org-check-agenda-file file)
4075 (setq buffer (if (file-exists-p file)
4076 (org-get-agenda-file-buffer file)
4077 (error "No such file %s" file)))
4078 (if (not buffer)
4079 ;; If file does not exist, error message to agenda
4080 (setq rtn (list
4081 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4082 rtnall (append rtnall rtn))
4083 (with-current-buffer buffer
4084 (unless (org-mode-p)
4085 (error "Agenda file %s is not in `org-mode'" file))
4086 (save-excursion
4087 (save-restriction
4088 (if org-agenda-restrict
4089 (narrow-to-region org-agenda-restrict-begin
4090 org-agenda-restrict-end)
4091 (widen))
4092 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4093 (setq rtnall (append rtnall rtn))))))))
4094 (if org-agenda-overriding-header
4095 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4096 nil 'face 'org-agenda-structure) "\n")
4097 (insert "Headlines with TAGS match: ")
4098 (add-text-properties (point-min) (1- (point))
4099 (list 'face 'org-agenda-structure
4100 'short-heading
4101 (concat "Match: " match)))
4102 (setq pos (point))
4103 (insert match "\n")
4104 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4105 (setq pos (point))
4106 (unless org-agenda-multi
4107 (insert "Press `C-u r' to search again with new search string\n"))
4108 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4109 (org-agenda-mark-header-line (point-min))
4110 (when rtnall
4111 (insert (org-finalize-agenda-entries rtnall) "\n"))
4112 (goto-char (point-min))
4113 (or org-agenda-multi (org-fit-agenda-window))
4114 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4115 (org-finalize-agenda)
4116 (setq buffer-read-only t)))
4118 ;;; Agenda Finding stuck projects
4120 (defvar org-agenda-skip-regexp nil
4121 "Regular expression used in skipping subtrees for the agenda.
4122 This is basically a temporary global variable that can be set and then
4123 used by user-defined selections using `org-agenda-skip-function'.")
4125 (defvar org-agenda-overriding-header nil
4126 "When set during agenda, todo and tags searches it replaces the header.
4127 This variable should not be set directly, but custom commands can bind it
4128 in the options section.")
4130 (defun org-agenda-skip-entry-when-regexp-matches ()
4131 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4132 If yes, it returns the end position of this entry, causing agenda commands
4133 to skip the entry but continuing the search in the subtree. This is a
4134 function that can be put into `org-agenda-skip-function' for the duration
4135 of a command."
4136 (let ((end (save-excursion (org-end-of-subtree t)))
4137 skip)
4138 (save-excursion
4139 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4140 (and skip end)))
4142 (defun org-agenda-skip-subtree-when-regexp-matches ()
4143 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4144 If yes, it returns the end position of this tree, causing agenda commands
4145 to skip this subtree. This is a function that can be put into
4146 `org-agenda-skip-function' for the duration of a command."
4147 (let ((end (save-excursion (org-end-of-subtree t)))
4148 skip)
4149 (save-excursion
4150 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4151 (and skip end)))
4153 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4154 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4155 If yes, it returns the end position of the current entry (NOT the tree),
4156 causing agenda commands to skip the entry but continuing the search in
4157 the subtree. This is a function that can be put into
4158 `org-agenda-skip-function' for the duration of a command. An important
4159 use of this function is for the stuck project list."
4160 (let ((end (save-excursion (org-end-of-subtree t)))
4161 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4162 skip)
4163 (save-excursion
4164 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4165 (and skip entry-end)))
4167 (defun org-agenda-skip-entry-if (&rest conditions)
4168 "Skip entry if any of CONDITIONS is true.
4169 See `org-agenda-skip-if' for details."
4170 (org-agenda-skip-if nil conditions))
4172 (defun org-agenda-skip-subtree-if (&rest conditions)
4173 "Skip entry if any of CONDITIONS is true.
4174 See `org-agenda-skip-if' for details."
4175 (org-agenda-skip-if t conditions))
4177 (defun org-agenda-skip-if (subtree conditions)
4178 "Checks current entity for CONDITIONS.
4179 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4180 the entry, i.e. the text before the next heading is checked.
4182 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4183 from different tests. Valid conditions are:
4185 scheduled Check if there is a scheduled cookie
4186 notscheduled Check if there is no scheduled cookie
4187 deadline Check if there is a deadline
4188 notdeadline Check if there is no deadline
4189 timestamp Check if there is a timestamp (also deadline or scheduled)
4190 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4191 regexp Check if regexp matches
4192 notregexp Check if regexp does not match.
4193 todo Check if TODO keyword matches
4194 nottodo Check if TODO keyword does not match
4196 The regexp is taken from the conditions list, it must come right after
4197 the `regexp' or `notregexp' element.
4199 `todo' and `nottodo' accept as an argument a list of todo
4200 keywords, which may include \"*\" to match any todo keyword.
4202 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4204 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4206 Instead of a list a keyword class may be given
4208 (org-agenda-skip-entry-if 'nottodo 'done)
4210 would skip entries that haven't been marked with any of \"DONE\"
4211 keywords. Possible classes are: `todo', `done', `any'.
4213 If any of these conditions is met, this function returns the end point of
4214 the entity, causing the search to continue from there. This is a function
4215 that can be put into `org-agenda-skip-function' for the duration of a command."
4216 (let (beg end m)
4217 (org-back-to-heading t)
4218 (setq beg (point)
4219 end (if subtree
4220 (progn (org-end-of-subtree t) (point))
4221 (progn (outline-next-heading) (1- (point)))))
4222 (goto-char beg)
4223 (and
4225 (and (memq 'scheduled conditions)
4226 (re-search-forward org-scheduled-time-regexp end t))
4227 (and (memq 'notscheduled conditions)
4228 (not (re-search-forward org-scheduled-time-regexp end t)))
4229 (and (memq 'deadline conditions)
4230 (re-search-forward org-deadline-time-regexp end t))
4231 (and (memq 'notdeadline conditions)
4232 (not (re-search-forward org-deadline-time-regexp end t)))
4233 (and (memq 'timestamp conditions)
4234 (re-search-forward org-ts-regexp end t))
4235 (and (memq 'nottimestamp conditions)
4236 (not (re-search-forward org-ts-regexp end t)))
4237 (and (setq m (memq 'regexp conditions))
4238 (stringp (nth 1 m))
4239 (re-search-forward (nth 1 m) end t))
4240 (and (setq m (memq 'notregexp conditions))
4241 (stringp (nth 1 m))
4242 (not (re-search-forward (nth 1 m) end t)))
4243 (and (or
4244 (setq m (memq 'todo conditions))
4245 (setq m (memq 'nottodo conditions)))
4246 (org-agenda-skip-if-todo m end)))
4247 end)))
4249 (defun org-agenda-skip-if-todo (args end)
4250 "Helper function for `org-agenda-skip-if', do not use it directly.
4251 ARGS is a list with first element either `todo' or `nottodo'.
4252 The remainder is either a list of TODO keywords, or a state symbol
4253 `todo' or `done' or `any'."
4254 (let ((kw (car args))
4255 (arg (cadr args))
4256 todo-wds todo-re)
4257 (setq todo-wds
4258 (org-uniquify
4259 (cond
4260 ((listp arg) ;; list of keywords
4261 (if (member "*" arg)
4262 (mapcar 'substring-no-properties org-todo-keywords-1)
4263 arg))
4264 ((symbolp arg) ;; keyword class name
4265 (cond
4266 ((eq arg 'todo)
4267 (org-delete-all org-done-keywords
4268 (mapcar 'substring-no-properties
4269 org-todo-keywords-1)))
4270 ((eq arg 'done) org-done-keywords)
4271 ((eq arg 'any)
4272 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4273 (setq todo-re
4274 (concat "^\\*+[ \t]+\\<\\("
4275 (mapconcat 'identity todo-wds "\\|")
4276 "\\)\\>"))
4277 (if (eq kw 'todo)
4278 (re-search-forward todo-re end t)
4279 (not (re-search-forward todo-re end t)))))
4281 ;;;###autoload
4282 (defun org-agenda-list-stuck-projects (&rest ignore)
4283 "Create agenda view for projects that are stuck.
4284 Stuck projects are project that have no next actions. For the definitions
4285 of what a project is and how to check if it stuck, customize the variable
4286 `org-stuck-projects'."
4287 (interactive)
4288 (let* ((org-agenda-skip-function
4289 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4290 ;; We could have used org-agenda-skip-if here.
4291 (org-agenda-overriding-header
4292 (or org-agenda-overriding-header "List of stuck projects: "))
4293 (matcher (nth 0 org-stuck-projects))
4294 (todo (nth 1 org-stuck-projects))
4295 (todo-wds (if (member "*" todo)
4296 (progn
4297 (org-prepare-agenda-buffers (org-agenda-files
4298 nil 'ifmode))
4299 (org-delete-all
4300 org-done-keywords-for-agenda
4301 (copy-sequence org-todo-keywords-for-agenda)))
4302 todo))
4303 (todo-re (concat "^\\*+[ \t]+\\("
4304 (mapconcat 'identity todo-wds "\\|")
4305 "\\)\\>"))
4306 (tags (nth 2 org-stuck-projects))
4307 (tags-re (if (member "*" tags)
4308 (org-re (concat org-outline-regexp-bol
4309 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4310 (if tags
4311 (concat org-outline-regexp-bol
4312 ".*:\\("
4313 (mapconcat 'identity tags "\\|")
4314 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4315 (gen-re (nth 3 org-stuck-projects))
4316 (re-list
4317 (delq nil
4318 (list
4319 (if todo todo-re)
4320 (if tags tags-re)
4321 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4322 gen-re)))))
4323 (setq org-agenda-skip-regexp
4324 (if re-list
4325 (mapconcat 'identity re-list "\\|")
4326 (error "No information how to identify unstuck projects")))
4327 (org-tags-view nil matcher)
4328 (with-current-buffer org-agenda-buffer-name
4329 (setq org-agenda-redo-command
4330 '(org-agenda-list-stuck-projects
4331 (or current-prefix-arg org-last-arg))))))
4333 ;;; Diary integration
4335 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4336 (defvar list-diary-entries-hook)
4337 (defvar diary-time-regexp)
4338 (defun org-get-entries-from-diary (date)
4339 "Get the (Emacs Calendar) diary entries for DATE."
4340 (require 'diary-lib)
4341 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4342 (diary-display-hook '(fancy-diary-display))
4343 (diary-display-function 'fancy-diary-display)
4344 (pop-up-frames nil)
4345 (list-diary-entries-hook
4346 (cons 'org-diary-default-entry list-diary-entries-hook))
4347 (diary-file-name-prefix-function nil) ; turn this feature off
4348 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4349 entries
4350 (org-disable-agenda-to-diary t))
4351 (save-excursion
4352 (save-window-excursion
4353 (funcall (if (fboundp 'diary-list-entries)
4354 'diary-list-entries 'list-diary-entries)
4355 date 1)))
4356 (if (not (get-buffer diary-fancy-buffer))
4357 (setq entries nil)
4358 (with-current-buffer diary-fancy-buffer
4359 (setq buffer-read-only nil)
4360 (if (zerop (buffer-size))
4361 ;; No entries
4362 (setq entries nil)
4363 ;; Omit the date and other unnecessary stuff
4364 (org-agenda-cleanup-fancy-diary)
4365 ;; Add prefix to each line and extend the text properties
4366 (if (zerop (buffer-size))
4367 (setq entries nil)
4368 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4369 (setq entries
4370 (with-temp-buffer
4371 (insert entries) (goto-char (point-min))
4372 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4373 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4374 (replace-match (concat "; " (match-string 1)))))
4375 (buffer-string)))))
4376 (set-buffer-modified-p nil)
4377 (kill-buffer diary-fancy-buffer)))
4378 (when entries
4379 (setq entries (org-split-string entries "\n"))
4380 (setq entries
4381 (mapcar
4382 (lambda (x)
4383 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4384 ;; Extend the text properties to the beginning of the line
4385 (org-add-props x (text-properties-at (1- (length x)) x)
4386 'type "diary" 'date date 'face 'org-agenda-diary))
4387 entries)))))
4389 (defvar org-agenda-cleanup-fancy-diary-hook nil
4390 "Hook run when the fancy diary buffer is cleaned up.")
4392 (defun org-agenda-cleanup-fancy-diary ()
4393 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4394 This gets rid of the date, the underline under the date, and
4395 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4396 date. It also removes lines that contain only whitespace."
4397 (goto-char (point-min))
4398 (if (looking-at ".*?:[ \t]*")
4399 (progn
4400 (replace-match "")
4401 (re-search-forward "\n=+$" nil t)
4402 (replace-match "")
4403 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4404 (re-search-forward "\n=+$" nil t)
4405 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4406 (goto-char (point-min))
4407 (while (re-search-forward "^ +\n" nil t)
4408 (replace-match ""))
4409 (goto-char (point-min))
4410 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4411 (replace-match ""))
4412 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4414 ;; Make sure entries from the diary have the right text properties.
4415 (eval-after-load "diary-lib"
4416 '(if (boundp 'diary-modify-entry-list-string-function)
4417 ;; We can rely on the hook, nothing to do
4419 ;; Hook not available, must use advice to make this work
4420 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4421 "Make the position visible."
4422 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4423 (stringp string)
4424 buffer-file-name)
4425 (setq string (org-modify-diary-entry-string string))))))
4427 (defun org-modify-diary-entry-string (string)
4428 "Add text properties to string, allowing org-mode to act on it."
4429 (org-add-props string nil
4430 'mouse-face 'highlight
4431 'help-echo (if buffer-file-name
4432 (format "mouse-2 or RET jump to diary file %s"
4433 (abbreviate-file-name buffer-file-name))
4435 'org-agenda-diary-link t
4436 'org-marker (org-agenda-new-marker (point-at-bol))))
4438 (defun org-diary-default-entry ()
4439 "Add a dummy entry to the diary.
4440 Needed to avoid empty dates which mess up holiday display."
4441 ;; Catch the error if dealing with the new add-to-diary-alist
4442 (when org-disable-agenda-to-diary
4443 (condition-case nil
4444 (org-add-to-diary-list original-date "Org-mode dummy" "")
4445 (error
4446 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4448 (defun org-add-to-diary-list (&rest args)
4449 (if (fboundp 'diary-add-to-list)
4450 (apply 'diary-add-to-list args)
4451 (apply 'add-to-diary-list args)))
4453 (defvar org-diary-last-run-time nil)
4455 ;;;###autoload
4456 (defun org-diary (&rest args)
4457 "Return diary information from org-files.
4458 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4459 It accesses org files and extracts information from those files to be
4460 listed in the diary. The function accepts arguments specifying what
4461 items should be listed. For a list of arguments allowed here, see the
4462 variable `org-agenda-entry-types'.
4464 The call in the diary file should look like this:
4466 &%%(org-diary) ~/path/to/some/orgfile.org
4468 Use a separate line for each org file to check. Or, if you omit the file name,
4469 all files listed in `org-agenda-files' will be checked automatically:
4471 &%%(org-diary)
4473 If you don't give any arguments (as in the example above), the default
4474 arguments (:deadline :scheduled :timestamp :sexp) are used.
4475 So the example above may also be written as
4477 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4479 The function expects the lisp variables `entry' and `date' to be provided
4480 by the caller, because this is how the calendar works. Don't use this
4481 function from a program - use `org-agenda-get-day-entries' instead."
4482 (when (> (- (org-float-time)
4483 org-agenda-last-marker-time)
4485 (org-agenda-reset-markers))
4486 (org-compile-prefix-format 'agenda)
4487 (org-set-sorting-strategy 'agenda)
4488 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4489 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4490 (list entry)
4491 (org-agenda-files t)))
4492 (time (org-float-time))
4493 file rtn results)
4494 (when (or (not org-diary-last-run-time)
4495 (> (- time
4496 org-diary-last-run-time)
4498 (org-prepare-agenda-buffers files))
4499 (setq org-diary-last-run-time time)
4500 ;; If this is called during org-agenda, don't return any entries to
4501 ;; the calendar. Org Agenda will list these entries itself.
4502 (if org-disable-agenda-to-diary (setq files nil))
4503 (while (setq file (pop files))
4504 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4505 (setq results (append results rtn)))
4506 (if results
4507 (concat (org-finalize-agenda-entries results) "\n"))))
4509 ;;; Agenda entry finders
4511 (defun org-agenda-get-day-entries (file date &rest args)
4512 "Does the work for `org-diary' and `org-agenda'.
4513 FILE is the path to a file to be checked for entries. DATE is date like
4514 the one returned by `calendar-current-date'. ARGS are symbols indicating
4515 which kind of entries should be extracted. For details about these, see
4516 the documentation of `org-diary'."
4517 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4518 (let* ((org-startup-folded nil)
4519 (org-startup-align-all-tables nil)
4520 (buffer (if (file-exists-p file)
4521 (org-get-agenda-file-buffer file)
4522 (error "No such file %s" file)))
4523 arg results rtn deadline-results)
4524 (if (not buffer)
4525 ;; If file does not exist, make sure an error message ends up in diary
4526 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4527 (with-current-buffer buffer
4528 (unless (org-mode-p)
4529 (error "Agenda file %s is not in `org-mode'" file))
4530 (let ((case-fold-search nil))
4531 (save-excursion
4532 (save-restriction
4533 (if org-agenda-restrict
4534 (narrow-to-region org-agenda-restrict-begin
4535 org-agenda-restrict-end)
4536 (widen))
4537 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4538 (while (setq arg (pop args))
4539 (cond
4540 ((and (eq arg :todo)
4541 (equal date (calendar-gregorian-from-absolute
4542 (org-today))))
4543 (setq rtn (org-agenda-get-todos))
4544 (setq results (append results rtn)))
4545 ((eq arg :timestamp)
4546 (setq rtn (org-agenda-get-blocks))
4547 (setq results (append results rtn))
4548 (setq rtn (org-agenda-get-timestamps))
4549 (setq results (append results rtn)))
4550 ((eq arg :sexp)
4551 (setq rtn (org-agenda-get-sexps))
4552 (setq results (append results rtn)))
4553 ((eq arg :scheduled)
4554 (setq rtn (org-agenda-get-scheduled deadline-results))
4555 (setq results (append results rtn)))
4556 ((eq arg :closed)
4557 (setq rtn (org-agenda-get-progress))
4558 (setq results (append results rtn)))
4559 ((eq arg :deadline)
4560 (setq rtn (org-agenda-get-deadlines))
4561 (setq deadline-results (copy-sequence rtn))
4562 (setq results (append results rtn))))))))
4563 results))))
4565 (defun org-agenda-get-todos ()
4566 "Return the TODO information for agenda display."
4567 (let* ((props (list 'face nil
4568 'done-face 'org-agenda-done
4569 'org-not-done-regexp org-not-done-regexp
4570 'org-todo-regexp org-todo-regexp
4571 'org-complex-heading-regexp org-complex-heading-regexp
4572 'mouse-face 'highlight
4573 'help-echo
4574 (format "mouse-2 or RET jump to org file %s"
4575 (abbreviate-file-name buffer-file-name))))
4576 (regexp (concat "^\\*+[ \t]+\\("
4577 (if org-select-this-todo-keyword
4578 (if (equal org-select-this-todo-keyword "*")
4579 org-todo-regexp
4580 (concat "\\<\\("
4581 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4582 "\\)\\>"))
4583 org-not-done-regexp)
4584 "[^\n\r]*\\)"))
4585 marker priority category tags todo-state
4586 ee txt beg end)
4587 (goto-char (point-min))
4588 (while (re-search-forward regexp nil t)
4589 (catch :skip
4590 (save-match-data
4591 (beginning-of-line)
4592 (org-agenda-skip)
4593 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4594 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4595 (goto-char (1+ beg))
4596 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4597 (throw :skip nil)))
4598 (goto-char (match-beginning 1))
4599 (setq marker (org-agenda-new-marker (match-beginning 0))
4600 category (org-get-category)
4601 txt (match-string 1)
4602 tags (org-get-tags-at (point))
4603 txt (org-format-agenda-item "" txt category tags)
4604 priority (1+ (org-get-priority txt))
4605 todo-state (org-get-todo-state))
4606 (org-add-props txt props
4607 'org-marker marker 'org-hd-marker marker
4608 'priority priority 'org-category category
4609 'type "todo" 'todo-state todo-state)
4610 (push txt ee)
4611 (if org-agenda-todo-list-sublevels
4612 (goto-char (match-end 1))
4613 (org-end-of-subtree 'invisible))))
4614 (nreverse ee)))
4616 (defun org-agenda-todo-custom-ignore-p (time n)
4617 "Check whether timestamp is farther away then n number of days.
4618 This function is invoked if `org-agenda-todo-ignore-deadlines',
4619 `org-agenda-todo-ignore-scheduled' or
4620 `org-agenda-todo-ignore-timestamp' is set to an integer."
4621 (let ((days (org-days-to-time time)))
4622 (if (>= n 0)
4623 (>= days n)
4624 (<= days n))))
4626 ;;;###autoload
4627 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4628 (&optional end)
4629 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4630 (when (or org-agenda-todo-ignore-with-date
4631 org-agenda-todo-ignore-scheduled
4632 org-agenda-todo-ignore-deadlines
4633 org-agenda-todo-ignore-timestamp)
4634 (setq end (or end (save-excursion (outline-next-heading) (point))))
4635 (save-excursion
4636 (or (and org-agenda-todo-ignore-with-date
4637 (re-search-forward org-ts-regexp end t))
4638 (and org-agenda-todo-ignore-scheduled
4639 (re-search-forward org-scheduled-time-regexp end t)
4640 (cond
4641 ((eq org-agenda-todo-ignore-scheduled 'future)
4642 (> (org-days-to-time (match-string 1)) 0))
4643 ((eq org-agenda-todo-ignore-scheduled 'past)
4644 (<= (org-days-to-time (match-string 1)) 0))
4645 ((numberp org-agenda-todo-ignore-scheduled)
4646 (org-agenda-todo-custom-ignore-p
4647 (match-string 1) org-agenda-todo-ignore-scheduled))
4648 (t)))
4649 (and org-agenda-todo-ignore-deadlines
4650 (re-search-forward org-deadline-time-regexp end t)
4651 (cond
4652 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4653 ((eq org-agenda-todo-ignore-deadlines 'far)
4654 (not (org-deadline-close (match-string 1))))
4655 ((eq org-agenda-todo-ignore-deadlines 'future)
4656 (> (org-days-to-time (match-string 1)) 0))
4657 ((eq org-agenda-todo-ignore-deadlines 'past)
4658 (<= (org-days-to-time (match-string 1)) 0))
4659 ((numberp org-agenda-todo-ignore-deadlines)
4660 (org-agenda-todo-custom-ignore-p
4661 (match-string 1) org-agenda-todo-ignore-deadlines))
4662 (t (org-deadline-close (match-string 1)))))
4663 (and org-agenda-todo-ignore-timestamp
4664 (let ((buffer (current-buffer))
4665 (regexp
4666 (concat
4667 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4668 (start (point)))
4669 ;; Copy current buffer into a temporary one
4670 (with-temp-buffer
4671 (insert-buffer-substring buffer start end)
4672 (goto-char (point-min))
4673 ;; Delete SCHEDULED and DEADLINE items
4674 (while (re-search-forward regexp end t)
4675 (delete-region (match-beginning 0) (match-end 0)))
4676 (goto-char (point-min))
4677 ;; No search for timestamp left
4678 (when (re-search-forward org-ts-regexp nil t)
4679 (cond
4680 ((eq org-agenda-todo-ignore-timestamp 'future)
4681 (> (org-days-to-time (match-string 1)) 0))
4682 ((eq org-agenda-todo-ignore-timestamp 'past)
4683 (<= (org-days-to-time (match-string 1)) 0))
4684 ((numberp org-agenda-todo-ignore-timestamp)
4685 (org-agenda-todo-custom-ignore-p
4686 (match-string 1) org-agenda-todo-ignore-timestamp))
4687 (t))))))))))
4689 (defconst org-agenda-no-heading-message
4690 "No heading for this item in buffer or region.")
4692 (defun org-agenda-get-timestamps ()
4693 "Return the date stamp information for agenda display."
4694 (let* ((props (list 'face nil
4695 'org-not-done-regexp org-not-done-regexp
4696 'org-todo-regexp org-todo-regexp
4697 'org-complex-heading-regexp org-complex-heading-regexp
4698 'mouse-face 'highlight
4699 'help-echo
4700 (format "mouse-2 or RET jump to org file %s"
4701 (abbreviate-file-name buffer-file-name))))
4702 (d1 (calendar-absolute-from-gregorian date))
4703 (remove-re
4704 (concat
4705 (regexp-quote
4706 (format-time-string
4707 "<%Y-%m-%d"
4708 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4709 ".*?>"))
4710 (regexp
4711 (concat
4712 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4713 (regexp-quote
4714 (substring
4715 (format-time-string
4716 (car org-time-stamp-formats)
4717 (apply 'encode-time ; DATE bound by calendar
4718 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4719 1 11))
4720 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4721 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4722 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4723 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4724 todo-state end-of-match show-all)
4725 (goto-char (point-min))
4726 (while (setq end-of-match (re-search-forward regexp nil t))
4727 (setq b0 (match-beginning 0)
4728 b3 (match-beginning 3) e3 (match-end 3)
4729 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4730 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4731 (member todo-state
4732 org-agenda-repeating-timestamp-show-all)))
4733 (catch :skip
4734 (and (org-at-date-range-p) (throw :skip nil))
4735 (org-agenda-skip)
4736 (if (and (match-end 1)
4737 (not (= d1 (org-time-string-to-absolute
4738 (match-string 1) d1 nil show-all))))
4739 (throw :skip nil))
4740 (if (and e3
4741 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4742 (throw :skip nil))
4743 (setq tmp (buffer-substring (max (point-min)
4744 (- b0 org-ds-keyword-length))
4746 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4747 inactivep (= (char-after b0) ?\[)
4748 deadlinep (string-match org-deadline-regexp tmp)
4749 scheduledp (string-match org-scheduled-regexp tmp)
4750 closedp (and org-agenda-include-inactive-timestamps
4751 (string-match org-closed-string tmp))
4752 clockp (and org-agenda-include-inactive-timestamps
4753 (or (string-match org-clock-string tmp)
4754 (string-match "]-+\\'" tmp)))
4755 donep (member todo-state org-done-keywords))
4756 (if (or scheduledp deadlinep closedp clockp
4757 (and donep org-agenda-skip-timestamp-if-done))
4758 (throw :skip t))
4759 (if (string-match ">" timestr)
4760 ;; substring should only run to end of time stamp
4761 (setq timestr (substring timestr 0 (match-end 0))))
4762 (setq marker (org-agenda-new-marker b0)
4763 category (org-get-category b0))
4764 (save-excursion
4765 (if (not (re-search-backward org-outline-regexp-bol nil t))
4766 (setq txt org-agenda-no-heading-message)
4767 (goto-char (match-beginning 0))
4768 (setq hdmarker (org-agenda-new-marker)
4769 tags (org-get-tags-at))
4770 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4771 (setq head (or (match-string 1) ""))
4772 (setq txt (org-format-agenda-item
4773 (if inactivep org-agenda-inactive-leader nil)
4774 head category tags timestr
4775 remove-re)))
4776 (setq priority (org-get-priority txt))
4777 (org-add-props txt props
4778 'org-marker marker 'org-hd-marker hdmarker)
4779 (org-add-props txt nil 'priority priority
4780 'org-category category 'date date
4781 'todo-state todo-state
4782 'type "timestamp")
4783 (push txt ee))
4784 (if org-agenda-skip-additional-timestamps-same-entry
4785 (outline-next-heading)
4786 (goto-char end-of-match))))
4787 (nreverse ee)))
4789 (defun org-agenda-get-sexps ()
4790 "Return the sexp information for agenda display."
4791 (require 'diary-lib)
4792 (let* ((props (list 'mouse-face 'highlight
4793 'help-echo
4794 (format "mouse-2 or RET jump to org file %s"
4795 (abbreviate-file-name buffer-file-name))))
4796 (regexp "^&?%%(")
4797 marker category ee txt tags entry result beg b sexp sexp-entry
4798 todo-state)
4799 (goto-char (point-min))
4800 (while (re-search-forward regexp nil t)
4801 (catch :skip
4802 (org-agenda-skip)
4803 (setq beg (match-beginning 0))
4804 (goto-char (1- (match-end 0)))
4805 (setq b (point))
4806 (forward-sexp 1)
4807 (setq sexp (buffer-substring b (point)))
4808 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4809 (org-trim (match-string 1))
4810 ""))
4811 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4812 (when result
4813 (setq marker (org-agenda-new-marker beg)
4814 category (org-get-category beg)
4815 todo-state (org-get-todo-state))
4817 (dolist (r (if (stringp result)
4818 (list result)
4819 result)) ;; we expect a list here
4820 (if (string-match "\\S-" r)
4821 (setq txt r)
4822 (setq txt "SEXP entry returned empty string"))
4824 (setq txt (org-format-agenda-item
4825 "" txt category tags 'time))
4826 (org-add-props txt props 'org-marker marker)
4827 (org-add-props txt nil
4828 'org-category category 'date date 'todo-state todo-state
4829 'type "sexp")
4830 (push txt ee)))))
4831 (nreverse ee)))
4833 ;; Calendar sanity: define some functions that are independent of
4834 ;; `calendar-date-style'.
4835 ;; Normally I would like to use ISO format when calling the diary functions,
4836 ;; but to make sure we still have Emacs 22 compatibility we bind
4837 ;; also `european-calendar-style' and use european format
4838 (defun org-anniversary (year month day &optional mark)
4839 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4840 (org-no-warnings
4841 (let ((calendar-date-style 'european) (european-calendar-style t))
4842 (diary-anniversary day month year mark))))
4843 (defun org-cyclic (N year month day &optional mark)
4844 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4845 (org-no-warnings
4846 (let ((calendar-date-style 'european) (european-calendar-style t))
4847 (diary-cyclic N day month year mark))))
4848 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4849 "Like `diary-block', but with fixed (ISO) order of arguments."
4850 (org-no-warnings
4851 (let ((calendar-date-style 'european) (european-calendar-style t))
4852 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4853 (defun org-date (year month day &optional mark)
4854 "Like `diary-date', but with fixed (ISO) order of arguments."
4855 (org-no-warnings
4856 (let ((calendar-date-style 'european) (european-calendar-style t))
4857 (diary-date day month year mark))))
4858 (defalias 'org-float 'diary-float)
4860 ;; Define the` org-class' function
4861 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4862 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4863 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4864 is any number of ISO weeks in the block period for which the item should
4865 be skipped."
4866 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4867 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4868 (d (calendar-absolute-from-gregorian date)))
4869 (and
4870 (<= date1 d)
4871 (<= d date2)
4872 (= (calendar-day-of-week date) dayname)
4873 (or (not skip-weeks)
4874 (progn
4875 (require 'cal-iso)
4876 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4877 entry)))
4879 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4880 "Like `org-class', but honor `calendar-date-style'.
4881 The order of the first 2 times 3 arguments depends on the variable
4882 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4883 So for American calendars, give this as MONTH DAY YEAR, for European as
4884 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4885 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4886 is any number of ISO weeks in the block period for which the item should
4887 be skipped.
4889 This function is here only for backward compatibility and it is deprecated,
4890 please use `org-class' instead."
4891 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4892 (date2 (org-order-calendar-date-args m2 d2 y2)))
4893 (org-class
4894 (nth 2 date1) (car date1) (nth 1 date1)
4895 (nth 2 date2) (car date2) (nth 1 date2)
4896 dayname skip-weeks)))
4898 (defalias 'org-get-closed 'org-agenda-get-progress)
4899 (defun org-agenda-get-progress ()
4900 "Return the logged TODO entries for agenda display."
4901 (let* ((props (list 'mouse-face 'highlight
4902 'org-not-done-regexp org-not-done-regexp
4903 'org-todo-regexp org-todo-regexp
4904 'org-complex-heading-regexp org-complex-heading-regexp
4905 'help-echo
4906 (format "mouse-2 or RET jump to org file %s"
4907 (abbreviate-file-name buffer-file-name))))
4908 (items (if (consp org-agenda-show-log)
4909 org-agenda-show-log
4910 (if (eq org-agenda-show-log 'clockcheck)
4911 '(clock)
4912 org-agenda-log-mode-items)))
4913 (parts
4914 (delq nil
4915 (list
4916 (if (memq 'closed items) (concat "\\<" org-closed-string))
4917 (if (memq 'clock items) (concat "\\<" org-clock-string))
4918 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4919 (parts-re (if parts (mapconcat 'identity parts "\\|")
4920 (error "`org-agenda-log-mode-items' is empty")))
4921 (regexp (concat
4922 "\\(" parts-re "\\)"
4923 " *\\["
4924 (regexp-quote
4925 (substring
4926 (format-time-string
4927 (car org-time-stamp-formats)
4928 (apply 'encode-time ; DATE bound by calendar
4929 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4930 1 11))))
4931 (org-agenda-search-headline-for-time nil)
4932 marker hdmarker priority category tags closedp statep clockp state
4933 ee txt extra timestr rest clocked)
4934 (goto-char (point-min))
4935 (while (re-search-forward regexp nil t)
4936 (catch :skip
4937 (org-agenda-skip)
4938 (setq marker (org-agenda-new-marker (match-beginning 0))
4939 closedp (equal (match-string 1) org-closed-string)
4940 statep (equal (string-to-char (match-string 1)) ?-)
4941 clockp (not (or closedp statep))
4942 state (and statep (match-string 2))
4943 category (org-get-category (match-beginning 0))
4944 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4946 (when (string-match "\\]" timestr)
4947 ;; substring should only run to end of time stamp
4948 (setq rest (substring timestr (match-end 0))
4949 timestr (substring timestr 0 (match-end 0)))
4950 (if (and (not closedp) (not statep)
4951 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4952 (progn (setq timestr (concat (substring timestr 0 -1)
4953 "-" (match-string 1 rest) "]"))
4954 (setq clocked (match-string 2 rest)))
4955 (setq clocked "-")))
4956 (save-excursion
4957 (setq extra
4958 (cond
4959 ((not org-agenda-log-mode-add-notes) nil)
4960 (statep
4961 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4962 (match-string 1)))
4963 (clockp
4964 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4965 (match-string 1)))))
4966 (if (not (re-search-backward org-outline-regexp-bol nil t))
4967 (setq txt org-agenda-no-heading-message)
4968 (goto-char (match-beginning 0))
4969 (setq hdmarker (org-agenda-new-marker)
4970 tags (org-get-tags-at))
4971 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4972 (setq txt (match-string 1))
4973 (when extra
4974 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4975 (setq txt (concat (substring txt 0 (match-beginning 1))
4976 " - " extra " " (match-string 2 txt)))
4977 (setq txt (concat txt " - " extra))))
4978 (setq txt (org-format-agenda-item
4979 (cond
4980 (closedp "Closed: ")
4981 (statep (concat "State: (" state ")"))
4982 (t (concat "Clocked: (" clocked ")")))
4983 txt category tags timestr)))
4984 (setq priority 100000)
4985 (org-add-props txt props
4986 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4987 'priority priority 'org-category category
4988 'type "closed" 'date date
4989 'undone-face 'org-warning 'done-face 'org-agenda-done)
4990 (push txt ee))
4991 (goto-char (point-at-eol))))
4992 (nreverse ee)))
4994 (defun org-agenda-show-clocking-issues ()
4995 "Add overlays, showing issues with clocking.
4996 See also the user option `org-agenda-clock-consistency-checks'."
4997 (interactive)
4998 (let* ((pl org-agenda-clock-consistency-checks)
4999 (re (concat "^[ \t]*"
5000 org-clock-string
5001 "[ \t]+"
5002 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5003 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5004 (tlstart 0.)
5005 (tlend 0.)
5006 (maxtime (org-hh:mm-string-to-minutes
5007 (or (plist-get pl :max-duration) "24:00")))
5008 (mintime (org-hh:mm-string-to-minutes
5009 (or (plist-get pl :min-duration) 0)))
5010 (maxgap (org-hh:mm-string-to-minutes
5011 ;; default 30:00 means never complain
5012 (or (plist-get pl :max-gap) "30:00")))
5013 (gapok (mapcar 'org-hh:mm-string-to-minutes
5014 (plist-get pl :gap-ok-around)))
5015 (def-face (or (plist-get pl :default-face)
5016 '((:background "DarkRed") (:foreground "white"))))
5017 issue face m te ts dt ov)
5018 (goto-char (point-min))
5019 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5020 (setq issue nil face def-face)
5021 (catch 'next
5022 (setq m (org-get-at-bol 'org-marker)
5023 te nil ts nil)
5024 (unless (and m (markerp m))
5025 (setq issue "No valid clock line") (throw 'next t))
5026 (org-with-point-at m
5027 (save-excursion
5028 (goto-char (point-at-bol))
5029 (unless (looking-at re)
5030 (error "No valid Clock line")
5031 (throw 'next t))
5032 (unless (match-end 3)
5033 (setq issue "No end time"
5034 face (or (plist-get pl :no-end-time-face) face))
5035 (throw 'next t))
5036 (setq ts (match-string 1)
5037 te (match-string 3)
5038 ts (org-float-time
5039 (apply 'encode-time (org-parse-time-string ts)))
5040 te (org-float-time
5041 (apply 'encode-time (org-parse-time-string te)))
5042 dt (- te ts))))
5043 (cond
5044 ((> dt (* 60 maxtime))
5045 ;; a very long clocking chunk
5046 (setq issue (format "Clocking interval is very long: %s"
5047 (org-minutes-to-hh:mm-string
5048 (floor (/ (float dt) 60.))))
5049 face (or (plist-get pl :long-face) face)))
5050 ((< dt (* 60 mintime))
5051 ;; a very short clocking chunk
5052 (setq issue (format "Clocking interval is very short: %s"
5053 (org-minutes-to-hh:mm-string
5054 (floor (/ (float dt) 60.))))
5055 face (or (plist-get pl :short-face) face)))
5056 ((and (> tlend 0) (< ts tlend))
5057 ;; Two clock entries are overlapping
5058 (setq issue (format "Clocking overlap: %d minutes"
5059 (/ (- tlend ts) 60))
5060 face (or (plist-get pl :overlap-face) face)))
5061 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5062 ;; There is a gap, lets see if we need to report it
5063 (unless (org-agenda-check-clock-gap tlend ts gapok)
5064 (setq issue (format "Clocking gap: %d minutes"
5065 (/ (- ts tlend) 60))
5066 face (or (plist-get pl :gap-face) face))))
5067 (t nil)))
5068 (setq tlend (or te tlend) tlstart (or ts tlstart))
5069 (when issue
5070 ;; OK, there was some issue, add an overlay to show the issue
5071 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5072 (overlay-put ov 'before-string
5073 (concat
5074 (org-add-props
5075 (format "%-43s" (concat " " issue))
5077 'face face)
5078 "\n"))
5079 (overlay-put ov 'evaporate t)))))
5081 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5082 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5083 (catch 'exit
5084 (unless ok-list
5085 ;; there are no OK times for gaps...
5086 (throw 'exit nil))
5087 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5088 ;; This is more than 24 hours, so it is OK.
5089 ;; because we have at least one OK time, that must be in the
5090 ;; 24 hour interval.
5091 (throw 'exit t))
5092 ;; We have a shorter gap.
5093 ;; Now we have to get the minute of the day when these times are
5094 (let* ((t1dec (decode-time (seconds-to-time t1)))
5095 (t2dec (decode-time (seconds-to-time t2)))
5096 ;; compute the minute on the day
5097 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5098 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5099 (when (< min2 min1)
5100 ;; if min2 is smaller than min1, this means it is on the next day.
5101 ;; Wrap it to after midnight.
5102 (setq min2 (+ min2 1440)))
5103 ;; Now check if any of the OK times is in the gap
5104 (mapc (lambda (x)
5105 ;; Wrap the time to after midnight if necessary
5106 (if (< x min1) (setq x (+ x 1440)))
5107 ;; Check if in interval
5108 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5109 ok-list)
5110 ;; Nope, this gap is not OK
5111 nil)))
5113 (defun org-agenda-get-deadlines ()
5114 "Return the deadline information for agenda display."
5115 (let* ((props (list 'mouse-face 'highlight
5116 'org-not-done-regexp org-not-done-regexp
5117 'org-todo-regexp org-todo-regexp
5118 'org-complex-heading-regexp org-complex-heading-regexp
5119 'help-echo
5120 (format "mouse-2 or RET jump to org file %s"
5121 (abbreviate-file-name buffer-file-name))))
5122 (regexp org-deadline-time-regexp)
5123 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5124 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5125 d2 diff dfrac wdays pos pos1 category tags
5126 suppress-prewarning
5127 ee txt head face s todo-state show-all upcomingp donep timestr)
5128 (goto-char (point-min))
5129 (while (re-search-forward regexp nil t)
5130 (setq suppress-prewarning nil)
5131 (catch :skip
5132 (org-agenda-skip)
5133 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5134 (save-match-data
5135 (string-match org-scheduled-time-regexp
5136 (buffer-substring (point-at-bol)
5137 (point-at-eol)))))
5138 (setq suppress-prewarning
5139 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5140 org-agenda-skip-deadline-prewarning-if-scheduled
5141 0)))
5142 (setq s (match-string 1)
5143 txt nil
5144 pos (1- (match-beginning 1))
5145 todo-state (save-match-data (org-get-todo-state))
5146 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5147 (member todo-state
5148 org-agenda-repeating-timestamp-show-all))
5149 d2 (org-time-string-to-absolute
5150 (match-string 1) d1 'past show-all)
5151 diff (- d2 d1)
5152 wdays (if suppress-prewarning
5153 (let ((org-deadline-warning-days suppress-prewarning))
5154 (org-get-wdays s))
5155 (org-get-wdays s))
5156 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5157 upcomingp (and todayp (> diff 0)))
5158 ;; When to show a deadline in the calendar:
5159 ;; If the expiration is within wdays warning time.
5160 ;; Past-due deadlines are only shown on the current date
5161 (if (and (or (and (<= diff wdays)
5162 (and todayp (not org-agenda-only-exact-dates)))
5163 (= diff 0)))
5164 (save-excursion
5165 ;; (setq todo-state (org-get-todo-state))
5166 (setq donep (member todo-state org-done-keywords))
5167 (if (and donep
5168 (or org-agenda-skip-deadline-if-done
5169 (not (= diff 0))))
5170 (setq txt nil)
5171 (setq category (org-get-category))
5172 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5173 (setq txt org-agenda-no-heading-message)
5174 (goto-char (match-end 0))
5175 (setq pos1 (match-beginning 0))
5176 (setq tags (org-get-tags-at pos1))
5177 (setq head (buffer-substring-no-properties
5178 (point)
5179 (progn (skip-chars-forward "^\r\n")
5180 (point))))
5181 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5182 (setq timestr
5183 (concat (substring s (match-beginning 1)) " "))
5184 (setq timestr 'time))
5185 (setq txt (org-format-agenda-item
5186 (if (= diff 0)
5187 (car org-agenda-deadline-leaders)
5188 (if (functionp
5189 (nth 1 org-agenda-deadline-leaders))
5190 (funcall
5191 (nth 1 org-agenda-deadline-leaders)
5192 diff date)
5193 (format (nth 1 org-agenda-deadline-leaders)
5194 diff)))
5195 head category tags
5196 (if (not (= diff 0)) nil timestr)))))
5197 (when txt
5198 (setq face (org-agenda-deadline-face dfrac wdays))
5199 (org-add-props txt props
5200 'org-marker (org-agenda-new-marker pos)
5201 'org-hd-marker (org-agenda-new-marker pos1)
5202 'priority (+ (- diff)
5203 (org-get-priority txt))
5204 'org-category category
5205 'todo-state todo-state
5206 'type (if upcomingp "upcoming-deadline" "deadline")
5207 'date (if upcomingp date d2)
5208 'face (if donep 'org-agenda-done face)
5209 'undone-face face 'done-face 'org-agenda-done)
5210 (push txt ee))))))
5211 (nreverse ee)))
5213 (defun org-agenda-deadline-face (fraction &optional wdays)
5214 "Return the face to displaying a deadline item.
5215 FRACTION is what fraction of the head-warning time has passed."
5216 (if (equal wdays 0) (setq fraction 1.))
5217 (let ((faces org-agenda-deadline-faces) f)
5218 (catch 'exit
5219 (while (setq f (pop faces))
5220 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5222 (defun org-agenda-get-scheduled (&optional deadline-results)
5223 "Return the scheduled information for agenda display."
5224 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5225 'org-todo-regexp org-todo-regexp
5226 'org-complex-heading-regexp org-complex-heading-regexp
5227 'done-face 'org-agenda-done
5228 'mouse-face 'highlight
5229 'help-echo
5230 (format "mouse-2 or RET jump to org file %s"
5231 (abbreviate-file-name buffer-file-name))))
5232 (regexp org-scheduled-time-regexp)
5233 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5234 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5236 (deadline-position-alist
5237 (mapcar (lambda (a) (and (setq mm (get-text-property
5238 0 'org-hd-marker a))
5239 (cons (marker-position mm) a)))
5240 deadline-results))
5241 d2 diff pos pos1 category tags donep
5242 ee txt head pastschedp todo-state face timestr s habitp show-all)
5243 (goto-char (point-min))
5244 (while (re-search-forward regexp nil t)
5245 (catch :skip
5246 (org-agenda-skip)
5247 (setq s (match-string 1)
5248 txt nil
5249 pos (1- (match-beginning 1))
5250 todo-state (save-match-data (org-get-todo-state))
5251 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5252 (member todo-state
5253 org-agenda-repeating-timestamp-show-all))
5254 d2 (org-time-string-to-absolute
5255 (match-string 1) d1 'past show-all)
5256 diff (- d2 d1))
5257 (setq pastschedp (and todayp (< diff 0)))
5258 ;; When to show a scheduled item in the calendar:
5259 ;; If it is on or past the date.
5260 (when (or (and (< diff 0)
5261 (< (abs diff) org-scheduled-past-days)
5262 (and todayp (not org-agenda-only-exact-dates)))
5263 (= diff 0))
5264 (save-excursion
5265 (setq donep (member todo-state org-done-keywords))
5266 (if (and donep
5267 (or org-agenda-skip-scheduled-if-done
5268 (not (= diff 0))
5269 (and (functionp 'org-is-habit-p)
5270 (org-is-habit-p))))
5271 (setq txt nil)
5272 (setq habitp (and (functionp 'org-is-habit-p)
5273 (org-is-habit-p)))
5274 (setq category (org-get-category))
5275 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5276 (setq txt org-agenda-no-heading-message)
5277 (goto-char (match-end 0))
5278 (setq pos1 (match-beginning 0))
5279 (if habitp
5280 (if (or (not org-habit-show-habits)
5281 (and (not todayp)
5282 org-habit-show-habits-only-for-today))
5283 (throw :skip nil))
5284 (if (and
5285 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5286 (and org-agenda-skip-scheduled-if-deadline-is-shown
5287 pastschedp))
5288 (setq mm (assoc pos1 deadline-position-alist)))
5289 (throw :skip nil)))
5290 (setq tags (org-get-tags-at))
5291 (setq head (buffer-substring-no-properties
5292 (point)
5293 (progn (skip-chars-forward "^\r\n") (point))))
5294 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5295 (setq timestr
5296 (concat (substring s (match-beginning 1)) " "))
5297 (setq timestr 'time))
5298 (setq txt (org-format-agenda-item
5299 (if (= diff 0)
5300 (car org-agenda-scheduled-leaders)
5301 (format (nth 1 org-agenda-scheduled-leaders)
5302 (- 1 diff)))
5303 head category tags
5304 (if (not (= diff 0)) nil timestr)
5305 nil habitp))))
5306 (when txt
5307 (setq face
5308 (cond
5309 ((and (not habitp) pastschedp)
5310 'org-scheduled-previously)
5311 (todayp 'org-scheduled-today)
5312 (t 'org-scheduled))
5313 habitp (and habitp (org-habit-parse-todo)))
5314 (org-add-props txt props
5315 'undone-face face
5316 'face (if donep 'org-agenda-done face)
5317 'org-marker (org-agenda-new-marker pos)
5318 'org-hd-marker (org-agenda-new-marker pos1)
5319 'type (if pastschedp "past-scheduled" "scheduled")
5320 'date (if pastschedp d2 date)
5321 'priority (if habitp
5322 (org-habit-get-priority habitp)
5323 (+ 94 (- 5 diff) (org-get-priority txt)))
5324 'org-category category
5325 'org-habit-p habitp
5326 'todo-state todo-state)
5327 (push txt ee))))))
5328 (nreverse ee)))
5330 (defun org-agenda-get-blocks ()
5331 "Return the date-range information for agenda display."
5332 (let* ((props (list 'face nil
5333 'org-not-done-regexp org-not-done-regexp
5334 'org-todo-regexp org-todo-regexp
5335 'org-complex-heading-regexp org-complex-heading-regexp
5336 'mouse-face 'highlight
5337 'help-echo
5338 (format "mouse-2 or RET jump to org file %s"
5339 (abbreviate-file-name buffer-file-name))))
5340 (regexp org-tr-regexp)
5341 (d0 (calendar-absolute-from-gregorian date))
5342 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5343 head donep)
5344 (goto-char (point-min))
5345 (while (re-search-forward regexp nil t)
5346 (catch :skip
5347 (org-agenda-skip)
5348 (setq pos (point))
5349 (let ((start-time (match-string 1))
5350 (end-time (match-string 2)))
5351 (setq s1 (match-string 1)
5352 s2 (match-string 2)
5353 d1 (time-to-days (org-time-string-to-time s1))
5354 d2 (time-to-days (org-time-string-to-time s2)))
5355 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5356 ;; Only allow days between the limits, because the normal
5357 ;; date stamps will catch the limits.
5358 (save-excursion
5359 (setq todo-state (org-get-todo-state))
5360 (setq donep (member todo-state org-done-keywords))
5361 (if (and donep org-agenda-skip-timestamp-if-done)
5362 (throw :skip t))
5363 (setq marker (org-agenda-new-marker (point)))
5364 (setq category (org-get-category))
5365 (if (not (re-search-backward org-outline-regexp-bol nil t))
5366 (setq txt org-agenda-no-heading-message)
5367 (goto-char (match-beginning 0))
5368 (setq hdmarker (org-agenda-new-marker (point)))
5369 (setq tags (org-get-tags-at))
5370 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5371 (setq head (match-string 1))
5372 (let ((remove-re
5373 (if org-agenda-remove-timeranges-from-blocks
5374 (concat
5375 "<" (regexp-quote s1) ".*?>"
5376 "--"
5377 "<" (regexp-quote s2) ".*?>")
5378 nil)))
5379 (setq txt (org-format-agenda-item
5380 (format
5381 (nth (if (= d1 d2) 0 1)
5382 org-agenda-timerange-leaders)
5383 (1+ (- d0 d1)) (1+ (- d2 d1)))
5384 head category tags
5385 (cond ((= d1 d0)
5386 (concat "<" start-time ">"))
5387 ((= d2 d0)
5388 (concat "<" end-time ">"))
5389 (t nil))
5390 remove-re))))
5391 (org-add-props txt props
5392 'org-marker marker 'org-hd-marker hdmarker
5393 'type "block" 'date date
5394 'todo-state todo-state
5395 'priority (org-get-priority txt) 'org-category category)
5396 (push txt ee))))
5397 (goto-char pos)))
5398 ;; Sort the entries by expiration date.
5399 (nreverse ee)))
5401 ;;; Agenda presentation and sorting
5403 (defvar org-prefix-has-time nil
5404 "A flag, set by `org-compile-prefix-format'.
5405 The flag is set if the currently compiled format contains a `%t'.")
5406 (defvar org-prefix-has-tag nil
5407 "A flag, set by `org-compile-prefix-format'.
5408 The flag is set if the currently compiled format contains a `%T'.")
5409 (defvar org-prefix-has-effort nil
5410 "A flag, set by `org-compile-prefix-format'.
5411 The flag is set if the currently compiled format contains a `%e'.")
5412 (defvar org-prefix-category-length nil
5413 "Used by `org-compile-prefix-format' to remember the category field width.")
5414 (defvar org-prefix-category-max-length nil
5415 "Used by `org-compile-prefix-format' to remember the category field width.")
5417 (defun org-agenda-get-category-icon (category)
5418 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5419 (dolist (entry org-agenda-category-icon-alist)
5420 (when (org-string-match-p (car entry) category)
5421 (if (listp (cadr entry))
5422 (return (cadr entry))
5423 (return (apply 'create-image (cdr entry)))))))
5425 (defun org-format-agenda-item (extra txt &optional category tags dotime
5426 remove-re habitp)
5427 "Format TXT to be inserted into the agenda buffer.
5428 In particular, it adds the prefix and corresponding text properties. EXTRA
5429 must be a string and replaces the `%s' specifier in the prefix format.
5430 CATEGORY (string, symbol or nil) may be used to overrule the default
5431 category taken from local variable or file name. It will replace the `%c'
5432 specifier in the format. DOTIME, when non-nil, indicates that a
5433 time-of-day should be extracted from TXT for sorting of this entry, and for
5434 the `%t' specifier in the format. When DOTIME is a string, this string is
5435 searched for a time before TXT is. TAGS can be the tags of the headline.
5436 Any match of REMOVE-RE will be removed from TXT."
5437 (save-match-data
5438 ;; Diary entries sometimes have extra whitespace at the beginning
5439 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5441 ;; Fix the tags part in txt
5442 (setq txt (org-agenda-fix-displayed-tags
5443 txt tags
5444 org-agenda-show-inherited-tags
5445 org-agenda-hide-tags-regexp))
5446 (let* ((category (or category
5447 (if (stringp org-category)
5448 org-category
5449 (and org-category (symbol-name org-category)))
5450 (if buffer-file-name
5451 (file-name-sans-extension
5452 (file-name-nondirectory buffer-file-name))
5453 "")))
5454 (category-icon (org-agenda-get-category-icon category))
5455 (category-icon (if category-icon
5456 (propertize " " 'display category-icon)
5457 ""))
5458 ;; time, tag, effort are needed for the eval of the prefix format
5459 (tag (if tags (nth (1- (length tags)) tags) ""))
5460 time effort neffort
5461 (ts (if dotime (concat
5462 (if (stringp dotime) dotime "")
5463 (and org-agenda-search-headline-for-time txt))))
5464 (time-of-day (and dotime (org-get-time-of-day ts)))
5465 stamp plain s0 s1 s2 rtn srp l
5466 duration thecategory)
5467 (and (org-mode-p) buffer-file-name
5468 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5469 (when (and dotime time-of-day)
5470 ;; Extract starting and ending time and move them to prefix
5471 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5472 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5473 (setq s0 (match-string 0 ts)
5474 srp (and stamp (match-end 3))
5475 s1 (match-string (if plain 1 2) ts)
5476 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5478 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5479 ;; them, we might want to remove them there to avoid duplication.
5480 ;; The user can turn this off with a variable.
5481 (if (and org-prefix-has-time
5482 org-agenda-remove-times-when-in-prefix (or stamp plain)
5483 (string-match (concat (regexp-quote s0) " *") txt)
5484 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5485 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5486 (= (match-beginning 0) 0)
5488 (setq txt (replace-match "" nil nil txt))))
5489 ;; Normalize the time(s) to 24 hour
5490 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5491 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5493 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5494 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5495 (setq s2
5496 (org-minutes-to-hh:mm-string
5497 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5499 ;; Compute the duration
5500 (when s2
5501 (setq duration (- (org-hh:mm-string-to-minutes s2)
5502 (org-hh:mm-string-to-minutes s1)))))
5504 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5505 txt)
5506 ;; Tags are in the string
5507 (if (or (eq org-agenda-remove-tags t)
5508 (and org-agenda-remove-tags
5509 org-prefix-has-tag))
5510 (setq txt (replace-match "" t t txt))
5511 (setq txt (replace-match
5512 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5513 (match-string 2 txt))
5514 t t txt))))
5515 (when (org-mode-p)
5516 (setq effort
5517 (condition-case nil
5518 (org-get-effort
5519 (or (get-text-property 0 'org-hd-marker txt)
5520 (get-text-property 0 'org-marker txt)))
5521 (error nil)))
5522 (when effort
5523 (setq neffort (org-duration-string-to-minutes effort)
5524 effort (setq effort (concat "[" effort "]")))))
5525 ;; prevent erroring out with %e format when there is no effort
5526 (or effort (setq effort ""))
5528 (when remove-re
5529 (while (string-match remove-re txt)
5530 (setq txt (replace-match "" t t txt))))
5532 ;; Set org-heading property on `txt' to mark the start of the
5533 ;; heading.
5534 (add-text-properties 0 (length txt) '(org-heading t) txt)
5536 ;; Prepare the variables needed in the eval of the compiled format
5537 (setq time (cond (s2 (concat
5538 (org-agenda-time-of-day-to-ampm-maybe s1)
5539 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5540 (if org-agenda-timegrid-use-ampm " ")))
5541 (s1 (concat
5542 (org-agenda-time-of-day-to-ampm-maybe s1)
5543 (if org-agenda-timegrid-use-ampm
5544 "........ "
5545 "......")))
5546 (t ""))
5547 extra (or (and (not habitp) extra) "")
5548 category (if (symbolp category) (symbol-name category) category)
5549 thecategory (copy-sequence category))
5550 (if (string-match org-bracket-link-regexp category)
5551 (progn
5552 (setq l (if (match-end 3)
5553 (- (match-end 3) (match-beginning 3))
5554 (- (match-end 1) (match-beginning 1))))
5555 (when (< l (or org-prefix-category-length 0))
5556 (setq category (copy-sequence category))
5557 (org-add-props category nil
5558 'extra-space (make-string
5559 (- org-prefix-category-length l 1) ?\ ))))
5560 (if (and org-prefix-category-max-length
5561 (>= (length category) org-prefix-category-max-length))
5562 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5563 ;; Evaluate the compiled format
5564 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5566 ;; And finally add the text properties
5567 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5568 (org-add-props rtn nil
5569 'org-category (if thecategory (downcase thecategory) category)
5570 'tags (mapcar 'org-downcase-keep-props tags)
5571 'org-highest-priority org-highest-priority
5572 'org-lowest-priority org-lowest-priority
5573 'time-of-day time-of-day
5574 'duration duration
5575 'effort effort
5576 'effort-minutes neffort
5577 'txt txt
5578 'time time
5579 'extra extra
5580 'format org-prefix-format-compiled
5581 'dotime dotime))))
5583 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5584 "Remove tags string from TXT, and add a modified list of tags.
5585 The modified list may contain inherited tags, and tags matched by
5586 `org-agenda-hide-tags-regexp' will be removed."
5587 (when (or add-inherited hide-re)
5588 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5589 (setq txt (substring txt 0 (match-beginning 0))))
5590 (setq tags
5591 (delq nil
5592 (mapcar (lambda (tg)
5593 (if (or (and hide-re (string-match hide-re tg))
5594 (and (not add-inherited)
5595 (get-text-property 0 'inherited tg)))
5597 tg))
5598 tags)))
5599 (when tags
5600 (let ((have-i (get-text-property 0 'inherited (car tags)))
5602 (setq txt (concat txt " :"
5603 (mapconcat
5604 (lambda (x)
5605 (setq i (get-text-property 0 'inherited x))
5606 (if (and have-i (not i))
5607 (progn
5608 (setq have-i nil)
5609 (concat ":" x))
5611 tags ":")
5612 (if have-i "::" ":"))))))
5613 txt)
5615 (defun org-downcase-keep-props (s)
5616 (let ((props (text-properties-at 0 s)))
5617 (setq s (downcase s))
5618 (add-text-properties 0 (length s) props s)
5621 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5622 (defvar org-agenda-sorting-strategy-selected nil)
5624 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5625 (catch 'exit
5626 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5627 ((and todayp (member 'today (car org-agenda-time-grid))))
5628 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5629 ((member 'weekly (car org-agenda-time-grid)))
5630 (t (throw 'exit list)))
5631 (let* ((have (delq nil (mapcar
5632 (lambda (x) (get-text-property 1 'time-of-day x))
5633 list)))
5634 (string (nth 1 org-agenda-time-grid))
5635 (gridtimes (nth 2 org-agenda-time-grid))
5636 (req (car org-agenda-time-grid))
5637 (remove (member 'remove-match req))
5638 new time)
5639 (if (and (member 'require-timed req) (not have))
5640 ;; don't show empty grid
5641 (throw 'exit list))
5642 (while (setq time (pop gridtimes))
5643 (unless (and remove (member time have))
5644 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5645 (push (org-format-agenda-item
5646 nil string "" nil
5647 (concat (substring time 0 -2) ":" (substring time -2)))
5648 new)
5649 (put-text-property
5650 2 (length (car new)) 'face 'org-time-grid (car new))))
5651 (when (and todayp org-agenda-show-current-time-in-grid)
5652 (push (org-format-agenda-item
5654 org-agenda-current-time-string
5655 "" nil
5656 (format-time-string "%H:%M "))
5657 new)
5658 (put-text-property
5659 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5661 (if (member 'time-up org-agenda-sorting-strategy-selected)
5662 (append new list)
5663 (append list new)))))
5665 (defun org-compile-prefix-format (key)
5666 "Compile the prefix format into a Lisp form that can be evaluated.
5667 The resulting form is returned and stored in the variable
5668 `org-prefix-format-compiled'."
5669 (setq org-prefix-has-time nil org-prefix-has-tag nil
5670 org-prefix-category-length nil org-prefix-has-effort nil)
5671 (let ((s (cond
5672 ((stringp org-agenda-prefix-format)
5673 org-agenda-prefix-format)
5674 ((assq key org-agenda-prefix-format)
5675 (cdr (assq key org-agenda-prefix-format)))
5676 (t " %-12:c%?-12t% s")))
5677 (start 0)
5678 varform vars var e c f opt)
5679 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5680 s start)
5681 (setq var (or (cdr (assoc (match-string 4 s)
5682 '(("c" . category) ("t" . time) ("s" . extra)
5683 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5684 'eval)
5685 c (or (match-string 3 s) "")
5686 opt (match-beginning 1)
5687 start (1+ (match-beginning 0)))
5688 (if (equal var 'time) (setq org-prefix-has-time t))
5689 (if (equal var 'tag) (setq org-prefix-has-tag t))
5690 (if (equal var 'effort) (setq org-prefix-has-effort t))
5691 (setq f (concat "%" (match-string 2 s) "s"))
5692 (when (equal var 'category)
5693 (setq org-prefix-category-length
5694 (floor (abs (string-to-number (match-string 2 s)))))
5695 (setq org-prefix-category-max-length
5696 (let ((x (match-string 2 s)))
5697 (save-match-data
5698 (if (string-match "\\.[0-9]+" x)
5699 (string-to-number (substring (match-string 0 x) 1)))))))
5700 (if (eq var 'eval)
5701 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5702 (if opt
5703 (setq varform
5704 `(if (equal "" ,var)
5706 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5707 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5708 (setq s (replace-match "%s" t nil s))
5709 (push varform vars))
5710 (setq vars (nreverse vars))
5711 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5713 (defun org-set-sorting-strategy (key)
5714 (if (symbolp (car org-agenda-sorting-strategy))
5715 ;; the old format
5716 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5717 (setq org-agenda-sorting-strategy-selected
5718 (or (cdr (assq key org-agenda-sorting-strategy))
5719 (cdr (assq 'agenda org-agenda-sorting-strategy))
5720 '(time-up category-keep priority-down)))))
5722 (defun org-get-time-of-day (s &optional string mod24)
5723 "Check string S for a time of day.
5724 If found, return it as a military time number between 0 and 2400.
5725 If not found, return nil.
5726 The optional STRING argument forces conversion into a 5 character wide string
5727 HH:MM."
5728 (save-match-data
5729 (when
5730 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5731 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5732 (let* ((h (string-to-number (match-string 1 s)))
5733 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5734 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5735 (am-p (equal ampm "am"))
5736 (h1 (cond ((not ampm) h)
5737 ((= h 12) (if am-p 0 12))
5738 (t (+ h (if am-p 0 12)))))
5739 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5740 (mod h1 24) h1))
5741 (t0 (+ (* 100 h2) m))
5742 (t1 (concat (if (>= h1 24) "+" " ")
5743 (if (and org-agenda-time-leading-zero
5744 (< t0 1000)) "0" "")
5745 (if (< t0 100) "0" "")
5746 (if (< t0 10) "0" "")
5747 (int-to-string t0))))
5748 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5750 (defvar org-agenda-before-sorting-filter-function nil
5751 "Function to be applied to agenda items prior to sorting.
5752 Prior to sorting also means just before they are inserted into the agenda.
5754 To aid sorting, you may revisit the original entries and add more text
5755 properties which will later be used by the sorting functions.
5757 The function should take a string argument, an agenda line.
5758 It has access to the text properties in that line, which contain among
5759 other things, the property `org-hd-marker' that points to the entry
5760 where the line comes from. Note that not all lines going into the agenda
5761 have this property, only most.
5763 The function should return the modified string. It is probably best
5764 to ONLY change text properties.
5766 You can also use this function as a filter, by returning nil for lines
5767 you don't want to have in the agenda at all. For this application, you
5768 could bind the variable in the options section of a custom command.")
5770 (defun org-finalize-agenda-entries (list &optional nosort)
5771 "Sort and concatenate the agenda items."
5772 (setq list (mapcar 'org-agenda-highlight-todo list))
5773 (if nosort
5774 list
5775 (when org-agenda-before-sorting-filter-function
5776 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5777 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5779 (defun org-agenda-highlight-todo (x)
5780 (let ((org-done-keywords org-done-keywords-for-agenda)
5781 (case-fold-search nil)
5783 (if (eq x 'line)
5784 (save-excursion
5785 (beginning-of-line 1)
5786 (setq re (org-get-at-bol 'org-todo-regexp))
5787 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5788 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5789 (add-text-properties (match-beginning 0) (match-end 1)
5790 (list 'face (org-get-todo-face 1)))
5791 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5792 (delete-region (match-beginning 1) (1- (match-end 0)))
5793 (goto-char (match-beginning 1))
5794 (insert (format org-agenda-todo-keyword-format s)))))
5795 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5796 (setq re (get-text-property 0 'org-todo-regexp x))
5797 (when (and re
5798 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5799 x (or pl 0)) pl))
5800 (add-text-properties
5801 (or (match-end 1) (match-end 0)) (match-end 0)
5802 (list 'face (org-get-todo-face (match-string 2 x)))
5804 (when (match-end 1)
5805 (setq x (concat (substring x 0 (match-end 1))
5806 (format org-agenda-todo-keyword-format
5807 (match-string 2 x))
5808 (org-add-props " " (text-properties-at 0 x))
5809 (substring x (match-end 3)))))))
5810 x)))
5812 (defsubst org-cmp-priority (a b)
5813 "Compare the priorities of string A and B."
5814 (let ((pa (or (get-text-property 1 'priority a) 0))
5815 (pb (or (get-text-property 1 'priority b) 0)))
5816 (cond ((> pa pb) +1)
5817 ((< pa pb) -1)
5818 (t nil))))
5820 (defsubst org-cmp-effort (a b)
5821 "Compare the priorities of string A and B."
5822 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5823 (ea (or (get-text-property 1 'effort-minutes a) def))
5824 (eb (or (get-text-property 1 'effort-minutes b) def)))
5825 (cond ((> ea eb) +1)
5826 ((< ea eb) -1)
5827 (t nil))))
5829 (defsubst org-cmp-category (a b)
5830 "Compare the string values of categories of strings A and B."
5831 (let ((ca (or (get-text-property 1 'org-category a) ""))
5832 (cb (or (get-text-property 1 'org-category b) "")))
5833 (cond ((string-lessp ca cb) -1)
5834 ((string-lessp cb ca) +1)
5835 (t nil))))
5837 (defsubst org-cmp-todo-state (a b)
5838 "Compare the todo states of strings A and B."
5839 (let* ((ma (or (get-text-property 1 'org-marker a)
5840 (get-text-property 1 'org-hd-marker a)))
5841 (mb (or (get-text-property 1 'org-marker b)
5842 (get-text-property 1 'org-hd-marker b)))
5843 (fa (and ma (marker-buffer ma)))
5844 (fb (and mb (marker-buffer mb)))
5845 (todo-kwds
5846 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5847 (and fb (with-current-buffer fb org-todo-keywords-1))))
5848 (ta (or (get-text-property 1 'todo-state a) ""))
5849 (tb (or (get-text-property 1 'todo-state b) ""))
5850 (la (- (length (member ta todo-kwds))))
5851 (lb (- (length (member tb todo-kwds))))
5852 (donepa (member ta org-done-keywords-for-agenda))
5853 (donepb (member tb org-done-keywords-for-agenda)))
5854 (cond ((and donepa (not donepb)) -1)
5855 ((and (not donepa) donepb) +1)
5856 ((< la lb) -1)
5857 ((< lb la) +1)
5858 (t nil))))
5860 (defsubst org-cmp-alpha (a b)
5861 "Compare the headlines, alphabetically."
5862 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5863 (plb (text-property-any 0 (length b) 'org-heading t b))
5864 (ta (and pla (substring a pla)))
5865 (tb (and plb (substring b plb))))
5866 (when pla
5867 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5868 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5869 (setq ta (substring ta (match-end 0))))
5870 (setq ta (downcase ta)))
5871 (when plb
5872 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5873 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5874 (setq tb (substring tb (match-end 0))))
5875 (setq tb (downcase tb)))
5876 (cond ((not ta) +1)
5877 ((not tb) -1)
5878 ((string-lessp ta tb) -1)
5879 ((string-lessp tb ta) +1)
5880 (t nil))))
5882 (defsubst org-cmp-tag (a b)
5883 "Compare the string values of the first tags of A and B."
5884 (let ((ta (car (last (get-text-property 1 'tags a))))
5885 (tb (car (last (get-text-property 1 'tags b)))))
5886 (cond ((not ta) +1)
5887 ((not tb) -1)
5888 ((string-lessp ta tb) -1)
5889 ((string-lessp tb ta) +1)
5890 (t nil))))
5892 (defsubst org-cmp-time (a b)
5893 "Compare the time-of-day values of strings A and B."
5894 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5895 (ta (or (get-text-property 1 'time-of-day a) def))
5896 (tb (or (get-text-property 1 'time-of-day b) def)))
5897 (cond ((< ta tb) -1)
5898 ((< tb ta) +1)
5899 (t nil))))
5901 (defsubst org-cmp-habit-p (a b)
5902 "Compare the todo states of strings A and B."
5903 (let ((ha (get-text-property 1 'org-habit-p a))
5904 (hb (get-text-property 1 'org-habit-p b)))
5905 (cond ((and ha (not hb)) -1)
5906 ((and (not ha) hb) +1)
5907 (t nil))))
5909 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5911 (defun org-entries-lessp (a b)
5912 "Predicate for sorting agenda entries."
5913 ;; The following variables will be used when the form is evaluated.
5914 ;; So even though the compiler complains, keep them.
5915 (let* ((ss org-agenda-sorting-strategy-selected)
5916 (time-up (and (org-em 'time-up 'time-down ss)
5917 (org-cmp-time a b)))
5918 (time-down (if time-up (- time-up) nil))
5919 (priority-up (and (org-em 'priority-up 'priority-down ss)
5920 (org-cmp-priority a b)))
5921 (priority-down (if priority-up (- priority-up) nil))
5922 (effort-up (and (org-em 'effort-up 'effort-down ss)
5923 (org-cmp-effort a b)))
5924 (effort-down (if effort-up (- effort-up) nil))
5925 (category-up (and (or (org-em 'category-up 'category-down ss)
5926 (memq 'category-keep ss))
5927 (org-cmp-category a b)))
5928 (category-down (if category-up (- category-up) nil))
5929 (category-keep (if category-up +1 nil))
5930 (tag-up (and (org-em 'tag-up 'tag-down ss)
5931 (org-cmp-tag a b)))
5932 (tag-down (if tag-up (- tag-up) nil))
5933 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5934 (org-cmp-todo-state a b)))
5935 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5936 (habit-up (and (org-em 'habit-up 'habit-down ss)
5937 (org-cmp-habit-p a b)))
5938 (habit-down (if habit-up (- habit-up) nil))
5939 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5940 (org-cmp-alpha a b)))
5941 (alpha-down (if alpha-up (- alpha-up) nil))
5942 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5943 user-defined-up user-defined-down)
5944 (if (and need-user-cmp org-agenda-cmp-user-defined
5945 (functionp org-agenda-cmp-user-defined))
5946 (setq user-defined-up
5947 (funcall org-agenda-cmp-user-defined a b)
5948 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5949 (cdr (assoc
5950 (eval (cons 'or org-agenda-sorting-strategy-selected))
5951 '((-1 . t) (1 . nil) (nil . nil))))))
5953 ;;; Agenda restriction lock
5955 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5956 "Overlay to mark the headline to which agenda commands are restricted.")
5957 (overlay-put org-agenda-restriction-lock-overlay
5958 'face 'org-agenda-restriction-lock)
5959 (overlay-put org-agenda-restriction-lock-overlay
5960 'help-echo "Agendas are currently limited to this subtree.")
5961 (org-detach-overlay org-agenda-restriction-lock-overlay)
5963 (defun org-agenda-set-restriction-lock (&optional type)
5964 "Set restriction lock for agenda, to current subtree or file.
5965 Restriction will be the file if TYPE is `file', or if type is the
5966 universal prefix '(4), or if the cursor is before the first headline
5967 in the file. Otherwise, restriction will be to the current subtree."
5968 (interactive "P")
5969 (and (equal type '(4)) (setq type 'file))
5970 (setq type (cond
5971 (type type)
5972 ((org-at-heading-p) 'subtree)
5973 ((condition-case nil (org-back-to-heading t) (error nil))
5974 'subtree)
5975 (t 'file)))
5976 (if (eq type 'subtree)
5977 (progn
5978 (setq org-agenda-restrict t)
5979 (setq org-agenda-overriding-restriction 'subtree)
5980 (put 'org-agenda-files 'org-restrict
5981 (list (buffer-file-name (buffer-base-buffer))))
5982 (org-back-to-heading t)
5983 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5984 (move-marker org-agenda-restrict-begin (point))
5985 (move-marker org-agenda-restrict-end
5986 (save-excursion (org-end-of-subtree t)))
5987 (message "Locking agenda restriction to subtree"))
5988 (put 'org-agenda-files 'org-restrict
5989 (list (buffer-file-name (buffer-base-buffer))))
5990 (setq org-agenda-restrict nil)
5991 (setq org-agenda-overriding-restriction 'file)
5992 (move-marker org-agenda-restrict-begin nil)
5993 (move-marker org-agenda-restrict-end nil)
5994 (message "Locking agenda restriction to file"))
5995 (setq current-prefix-arg nil)
5996 (org-agenda-maybe-redo))
5998 (defun org-agenda-remove-restriction-lock (&optional noupdate)
5999 "Remove the agenda restriction lock."
6000 (interactive "P")
6001 (org-detach-overlay org-agenda-restriction-lock-overlay)
6002 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6003 (setq org-agenda-overriding-restriction nil)
6004 (setq org-agenda-restrict nil)
6005 (put 'org-agenda-files 'org-restrict nil)
6006 (move-marker org-agenda-restrict-begin nil)
6007 (move-marker org-agenda-restrict-end nil)
6008 (setq current-prefix-arg nil)
6009 (message "Agenda restriction lock removed")
6010 (or noupdate (org-agenda-maybe-redo)))
6012 (defun org-agenda-maybe-redo ()
6013 "If there is any window showing the agenda view, update it."
6014 (let ((w (get-buffer-window org-agenda-buffer-name t))
6015 (w0 (selected-window)))
6016 (when w
6017 (select-window w)
6018 (org-agenda-redo)
6019 (select-window w0)
6020 (if org-agenda-overriding-restriction
6021 (message "Agenda view shifted to new %s restriction"
6022 org-agenda-overriding-restriction)
6023 (message "Agenda restriction lock removed")))))
6025 ;;; Agenda commands
6027 (defun org-agenda-check-type (error &rest types)
6028 "Check if agenda buffer is of allowed type.
6029 If ERROR is non-nil, throw an error, otherwise just return nil."
6030 (if (memq org-agenda-type types)
6032 (if error
6033 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6034 nil)))
6036 (defun org-agenda-quit ()
6037 "Exit agenda by removing the window or the buffer."
6038 (interactive)
6039 (if org-agenda-columns-active
6040 (org-columns-quit)
6041 (let ((buf (current-buffer)))
6042 (if (eq org-agenda-window-setup 'other-frame)
6043 (progn
6044 (kill-buffer buf)
6045 (org-agenda-reset-markers)
6046 (org-columns-remove-overlays)
6047 (setq org-agenda-archives-mode nil)
6048 (delete-frame))
6049 (and (not (eq org-agenda-window-setup 'current-window))
6050 (not (one-window-p))
6051 (delete-window))
6052 (kill-buffer buf)
6053 (org-agenda-reset-markers)
6054 (org-columns-remove-overlays)
6055 (setq org-agenda-archives-mode nil)))
6056 ;; Maybe restore the pre-agenda window configuration.
6057 (and org-agenda-restore-windows-after-quit
6058 (not (eq org-agenda-window-setup 'other-frame))
6059 org-pre-agenda-window-conf
6060 (set-window-configuration org-pre-agenda-window-conf))))
6062 (defun org-agenda-exit ()
6063 "Exit agenda by removing the window or the buffer.
6064 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6065 Org-mode buffers visited directly by the user will not be touched."
6066 (interactive)
6067 (org-release-buffers org-agenda-new-buffers)
6068 (setq org-agenda-new-buffers nil)
6069 (org-agenda-quit))
6071 (defun org-agenda-execute (arg)
6072 "Execute another agenda command, keeping same window.
6073 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6074 in the agenda."
6075 (interactive "P")
6076 (let ((org-agenda-window-setup 'current-window))
6077 (org-agenda arg)))
6079 (defun org-agenda-redo ()
6080 "Rebuild Agenda.
6081 When this is the global TODO list, a prefix argument will be interpreted."
6082 (interactive)
6083 (let* ((org-agenda-keep-modes t)
6084 (filter org-agenda-filter)
6085 (preset (get 'org-agenda-filter :preset-filter))
6086 (org-agenda-filter-while-redo (or filter preset))
6087 (cols org-agenda-columns-active)
6088 (line (org-current-line))
6089 (window-line (- line (org-current-line (window-start))))
6090 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6091 (put 'org-agenda-filter :preset-filter nil)
6092 (and cols (org-columns-quit))
6093 (message "Rebuilding agenda buffer...")
6094 (org-let lprops '(eval org-agenda-redo-command))
6095 (setq org-agenda-undo-list nil
6096 org-agenda-pending-undo-list nil)
6097 (message "Rebuilding agenda buffer...done")
6098 (put 'org-agenda-filter :preset-filter preset)
6099 (and (or filter preset) (org-agenda-filter-apply filter))
6100 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6101 (org-goto-line line)
6102 (recenter window-line)))
6105 (defvar org-global-tags-completion-table nil)
6106 (defvar org-agenda-filter-form nil)
6107 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6108 "Keep only those lines in the agenda buffer that have a specific tag.
6109 The tag is selected with its fast selection letter, as configured.
6110 With prefix argument STRIP, remove all lines that do have the tag.
6111 A lisp caller can specify CHAR. NARROW means that the new tag should be
6112 used to narrow the search - the interactive user can also press `-' or `+'
6113 to switch to narrowing."
6114 (interactive "P")
6115 (let* ((alist org-tag-alist-for-agenda)
6116 (tag-chars (mapconcat
6117 (lambda (x) (if (and (not (symbolp (car x)))
6118 (cdr x))
6119 (char-to-string (cdr x))
6120 ""))
6121 alist ""))
6122 (efforts (org-split-string
6123 (or (cdr (assoc (concat org-effort-property "_ALL")
6124 org-global-properties))
6125 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6126 "")))
6127 (effort-op org-agenda-filter-effort-default-operator)
6128 (effort-prompt "")
6129 (inhibit-read-only t)
6130 (current org-agenda-filter)
6131 maybe-refresh a n tag)
6132 (unless char
6133 (message
6134 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6135 (if narrow "Narrow" "Filter") tag-chars
6136 (if org-agenda-auto-exclude-function "[RET], " ""))
6137 (setq char (read-char)))
6138 (when (member char '(?+ ?-))
6139 ;; Narrowing down
6140 (cond ((equal char ?-) (setq strip t narrow t))
6141 ((equal char ?+) (setq strip nil narrow t)))
6142 (message
6143 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6144 (setq char (read-char)))
6145 (when (member char '(?< ?> ?= ??))
6146 ;; An effort operator
6147 (setq effort-op (char-to-string char))
6148 (setq alist nil) ; to make sure it will be interpreted as effort.
6149 (unless (equal char ??)
6150 (loop for i from 0 to 9 do
6151 (setq effort-prompt
6152 (concat
6153 effort-prompt " ["
6154 (if (= i 9) "0" (int-to-string (1+ i)))
6155 "]" (nth i efforts))))
6156 (message "Effort%s: %s " effort-op effort-prompt)
6157 (setq char (read-char))
6158 (when (or (< char ?0) (> char ?9))
6159 (error "Need 1-9,0 to select effort" ))))
6160 (when (equal char ?\t)
6161 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6162 (org-set-local 'org-global-tags-completion-table
6163 (org-global-tags-completion-table)))
6164 (let ((completion-ignore-case t))
6165 (setq tag (org-icompleting-read
6166 "Tag: " org-global-tags-completion-table))))
6167 (cond
6168 ((equal char ?\r)
6169 (org-agenda-filter-by-tag-show-all)
6170 (when org-agenda-auto-exclude-function
6171 (setq org-agenda-filter '())
6172 (dolist (tag (org-agenda-get-represented-tags))
6173 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6174 (if modifier
6175 (push modifier org-agenda-filter))))
6176 (if (not (null org-agenda-filter))
6177 (org-agenda-filter-apply org-agenda-filter)))
6178 (setq maybe-refresh t))
6179 ((equal char ?/)
6180 (org-agenda-filter-by-tag-show-all)
6181 (when (get 'org-agenda-filter :preset-filter)
6182 (org-agenda-filter-apply org-agenda-filter))
6183 (setq maybe-refresh t))
6184 ((or (equal char ?\ )
6185 (setq a (rassoc char alist))
6186 (and (>= char ?0) (<= char ?9)
6187 (setq n (if (= char ?0) 9 (- char ?0 1))
6188 tag (concat effort-op (nth n efforts))
6189 a (cons tag nil)))
6190 (and (= char ??)
6191 (setq tag "?eff")
6192 a (cons tag nil))
6193 (and tag (setq a (cons tag nil))))
6194 (org-agenda-filter-by-tag-show-all)
6195 (setq tag (car a))
6196 (setq org-agenda-filter
6197 (cons (concat (if strip "-" "+") tag)
6198 (if narrow current nil)))
6199 (org-agenda-filter-apply org-agenda-filter)
6200 (setq maybe-refresh t))
6201 (t (error "Invalid tag selection character %c" char)))
6202 (when (and maybe-refresh
6203 (eq org-agenda-clockreport-mode 'with-filter))
6204 (org-agenda-redo))))
6206 (defun org-agenda-get-represented-tags ()
6207 "Get a list of all tags currently represented in the agenda."
6208 (let (p tags)
6209 (save-excursion
6210 (goto-char (point-min))
6211 (while (setq p (next-single-property-change (point) 'tags))
6212 (goto-char p)
6213 (mapc (lambda (x) (add-to-list 'tags x))
6214 (get-text-property (point) 'tags))))
6215 tags))
6217 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6218 "Refine the current filter. See `org-agenda-filter-by-tag."
6219 (interactive "P")
6220 (org-agenda-filter-by-tag strip char 'refine))
6222 (defun org-agenda-filter-make-matcher ()
6223 "Create the form that tests a line for the agenda filter."
6224 (let (f f1)
6225 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6226 org-agenda-filter))
6227 (if (member x '("-" "+"))
6228 (setq f1 (if (equal x "-") 'tags '(not tags)))
6229 (if (string-match "[<=>?]" x)
6230 (setq f1 (org-agenda-filter-effort-form x))
6231 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6232 (if (equal (string-to-char x) ?-)
6233 (setq f1 (list 'not f1))))
6234 (push f1 f))
6235 (cons 'and (nreverse f))))
6237 (defun org-agenda-filter-effort-form (e)
6238 "Return the form to compare the effort of the current line with what E says.
6239 E looks like \"+<2:25\"."
6240 (let (op)
6241 (setq e (substring e 1))
6242 (setq op (string-to-char e) e (substring e 1))
6243 (setq op (cond ((equal op ?<) '<=)
6244 ((equal op ?>) '>=)
6245 ((equal op ??) op)
6246 (t '=)))
6247 (list 'org-agenda-compare-effort (list 'quote op)
6248 (org-duration-string-to-minutes e))))
6250 (defun org-agenda-compare-effort (op value)
6251 "Compare the effort of the current line with VALUE, using OP.
6252 If the line does not have an effort defined, return nil."
6253 (let ((eff (org-get-at-bol 'effort-minutes)))
6254 (if (equal op ??)
6255 (not eff)
6256 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6257 value))))
6259 (defun org-agenda-filter-apply (filter)
6260 "Set FILTER as the new agenda filter and apply it."
6261 (let (tags)
6262 (setq org-agenda-filter filter
6263 org-agenda-filter-form (org-agenda-filter-make-matcher))
6264 (org-agenda-set-mode-name)
6265 (save-excursion
6266 (goto-char (point-min))
6267 (while (not (eobp))
6268 (if (org-get-at-bol 'org-marker)
6269 (progn
6270 (setq tags (org-get-at-bol 'tags)) ; used in eval
6271 (if (not (eval org-agenda-filter-form))
6272 (org-agenda-filter-by-tag-hide-line))
6273 (beginning-of-line 2))
6274 (beginning-of-line 2))))
6275 (if (get-char-property (point) 'invisible)
6276 (org-agenda-previous-line))))
6278 (defun org-agenda-filter-by-tag-hide-line ()
6279 (let (ov)
6280 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6281 (point-at-eol)))
6282 (overlay-put ov 'invisible t)
6283 (overlay-put ov 'type 'tags-filter)
6284 (push ov org-agenda-filter-overlays)))
6286 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6287 (setq pos (or pos (point)))
6288 (save-excursion
6289 (dolist (ov (overlays-at pos))
6290 (when (and (overlay-get ov 'invisible)
6291 (eq (overlay-get ov 'type) 'tags-filter))
6292 (goto-char pos)
6293 (if (< (overlay-start ov) (point-at-eol))
6294 (move-overlay ov (point-at-eol)
6295 (overlay-end ov)))))))
6297 (defun org-agenda-filter-by-tag-show-all ()
6298 (mapc 'delete-overlay org-agenda-filter-overlays)
6299 (setq org-agenda-filter-overlays nil)
6300 (setq org-agenda-filter nil)
6301 (setq org-agenda-filter-form nil)
6302 (org-agenda-set-mode-name))
6304 (defun org-agenda-manipulate-query-add ()
6305 "Manipulate the query by adding a search term with positive selection.
6306 Positive selection means the term must be matched for selection of an entry."
6307 (interactive)
6308 (org-agenda-manipulate-query ?\[))
6309 (defun org-agenda-manipulate-query-subtract ()
6310 "Manipulate the query by adding a search term with negative selection.
6311 Negative selection means term must not be matched for selection of an entry."
6312 (interactive)
6313 (org-agenda-manipulate-query ?\]))
6314 (defun org-agenda-manipulate-query-add-re ()
6315 "Manipulate the query by adding a search regexp with positive selection.
6316 Positive selection means the regexp must match for selection of an entry."
6317 (interactive)
6318 (org-agenda-manipulate-query ?\{))
6319 (defun org-agenda-manipulate-query-subtract-re ()
6320 "Manipulate the query by adding a search regexp with negative selection.
6321 Negative selection means regexp must not match for selection of an entry."
6322 (interactive)
6323 (org-agenda-manipulate-query ?\}))
6324 (defun org-agenda-manipulate-query (char)
6325 (cond
6326 ((memq org-agenda-type '(timeline agenda))
6327 (let ((org-agenda-include-inactive-timestamps t))
6328 (org-agenda-redo))
6329 (message "Display now includes inactive timestamps as well"))
6330 ((eq org-agenda-type 'search)
6331 (org-add-to-string
6332 'org-agenda-query-string
6333 (if org-agenda-last-search-view-search-was-boolean
6334 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6335 (?\{ . " +{}") (?\} . " -{}"))))
6336 " "))
6337 (setq org-agenda-redo-command
6338 (list 'org-search-view
6339 org-todo-only
6340 org-agenda-query-string
6341 (+ (length org-agenda-query-string)
6342 (if (member char '(?\{ ?\})) 0 1))))
6343 (set-register org-agenda-query-register org-agenda-query-string)
6344 (org-agenda-redo))
6345 (t (error "Cannot manipulate query for %s-type agenda buffers"
6346 org-agenda-type))))
6348 (defun org-add-to-string (var string)
6349 (set var (concat (symbol-value var) string)))
6351 (defun org-agenda-goto-date (date)
6352 "Jump to DATE in agenda."
6353 (interactive (list (let ((org-read-date-prefer-future
6354 (eval org-agenda-jump-prefer-future)))
6355 (org-read-date))))
6356 (org-agenda-list nil date))
6358 (defun org-agenda-goto-today ()
6359 "Go to today."
6360 (interactive)
6361 (org-agenda-check-type t 'timeline 'agenda)
6362 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6363 (cond
6364 (tdpos (goto-char tdpos))
6365 ((eq org-agenda-type 'agenda)
6366 (let* ((sd (org-agenda-compute-starting-span
6367 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6368 (org-agenda-overriding-arguments org-agenda-last-arguments))
6369 (setf (nth 1 org-agenda-overriding-arguments) sd)
6370 (org-agenda-redo)
6371 (org-agenda-find-same-or-today-or-agenda)))
6372 (t (error "Cannot find today")))))
6374 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6375 (goto-char
6376 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6377 (text-property-any (point-min) (point-max) 'org-today t)
6378 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6379 (point-min))))
6381 (defun org-agenda-later (arg)
6382 "Go forward in time by thee current span.
6383 With prefix ARG, go forward that many times the current span."
6384 (interactive "p")
6385 (org-agenda-check-type t 'agenda)
6386 (let* ((span org-agenda-current-span)
6387 (sd org-starting-day)
6388 (greg (calendar-gregorian-from-absolute sd))
6389 (cnt (org-get-at-bol 'org-day-cnt))
6390 greg2)
6391 (cond
6392 ((eq span 'day)
6393 (setq sd (+ arg sd)))
6394 ((eq span 'week)
6395 (setq sd (+ (* 7 arg) sd)))
6396 ((eq span 'month)
6397 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6398 sd (calendar-absolute-from-gregorian greg2))
6399 (setcar greg2 (1+ (car greg2))))
6400 ((eq span 'year)
6401 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6402 sd (calendar-absolute-from-gregorian greg2))
6403 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6405 (setq sd (+ (* span arg) sd))))
6406 (let ((org-agenda-overriding-arguments
6407 (list (car org-agenda-last-arguments) sd span t)))
6408 (org-agenda-redo)
6409 (org-agenda-find-same-or-today-or-agenda cnt))))
6411 (defun org-agenda-earlier (arg)
6412 "Go backward in time by the current span.
6413 With prefix ARG, go backward that many times the current span."
6414 (interactive "p")
6415 (org-agenda-later (- arg)))
6417 (defun org-agenda-view-mode-dispatch ()
6418 "Call one of the view mode commands."
6419 (interactive)
6420 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6421 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6422 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6423 [E]ntryText")
6424 (let ((a (read-char-exclusive)))
6425 (case a
6426 (?\ (call-interactively 'org-agenda-reset-view))
6427 (?d (call-interactively 'org-agenda-day-view))
6428 (?w (call-interactively 'org-agenda-week-view))
6429 (?m (call-interactively 'org-agenda-month-view))
6430 (?y (call-interactively 'org-agenda-year-view))
6431 (?l (call-interactively 'org-agenda-log-mode))
6432 (?L (org-agenda-log-mode '(4)))
6433 (?c (org-agenda-log-mode 'clockcheck))
6434 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6435 (?a (call-interactively 'org-agenda-archives-mode))
6436 (?A (org-agenda-archives-mode 'files))
6437 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6438 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6439 (?G (call-interactively 'org-agenda-toggle-time-grid))
6440 (?D (call-interactively 'org-agenda-toggle-diary))
6441 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6442 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6443 (org-agenda-check-type t 'timeline 'agenda)
6444 (org-agenda-redo))
6445 (message "Display now includes inactive timestamps as well"))
6446 (?q (message "Abort"))
6447 (otherwise (error "Invalid key" )))))
6449 (defun org-agenda-reset-view ()
6450 "Switch to default view for agenda."
6451 (interactive)
6452 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6453 (defun org-agenda-day-view (&optional day-of-year)
6454 "Switch to daily view for agenda.
6455 With argument DAY-OF-YEAR, switch to that day of the year."
6456 (interactive "P")
6457 (org-agenda-change-time-span 'day day-of-year))
6458 (defun org-agenda-week-view (&optional iso-week)
6459 "Switch to daily view for agenda.
6460 With argument ISO-WEEK, switch to the corresponding ISO week.
6461 If ISO-WEEK has more then 2 digits, only the last two encode the
6462 week. Any digits before this encode a year. So 200712 means
6463 week 12 of year 2007. Years in the range 1938-2037 can also be
6464 written as 2-digit years."
6465 (interactive "P")
6466 (org-agenda-change-time-span 'week iso-week))
6467 (defun org-agenda-month-view (&optional month)
6468 "Switch to monthly view for agenda.
6469 With argument MONTH, switch to that month."
6470 (interactive "P")
6471 (org-agenda-change-time-span 'month month))
6472 (defun org-agenda-year-view (&optional year)
6473 "Switch to yearly view for agenda.
6474 With argument YEAR, switch to that year.
6475 If MONTH has more then 2 digits, only the last two encode the
6476 month. Any digits before this encode a year. So 200712 means
6477 December year 2007. Years in the range 1938-2037 can also be
6478 written as 2-digit years."
6479 (interactive "P")
6480 (when year
6481 (setq year (org-small-year-to-year year)))
6482 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6483 (org-agenda-change-time-span 'year year)
6484 (error "Abort")))
6486 (defun org-agenda-change-time-span (span &optional n)
6487 "Change the agenda view to SPAN.
6488 SPAN may be `day', `week', `month', `year'."
6489 (org-agenda-check-type t 'agenda)
6490 (if (and (not n) (equal org-agenda-current-span span))
6491 (error "Viewing span is already \"%s\"" span))
6492 (let* ((sd (or (org-get-at-bol 'day)
6493 org-starting-day))
6494 (sd (org-agenda-compute-starting-span sd span n))
6495 (org-agenda-overriding-arguments
6496 (or org-agenda-overriding-arguments
6497 (list (car org-agenda-last-arguments) sd span t))))
6498 (org-agenda-redo)
6499 (org-agenda-find-same-or-today-or-agenda))
6500 (org-agenda-set-mode-name)
6501 (message "Switched to %s view" span))
6503 (defun org-agenda-compute-starting-span (sd span &optional n)
6504 "Compute starting date for agenda.
6505 SPAN may be `day', `week', `month', `year'. The return value
6506 is a cons cell with the starting date and the number of days,
6507 so that the date SD will be in that range."
6508 (let* ((greg (calendar-gregorian-from-absolute sd))
6509 (dg (nth 1 greg))
6510 (mg (car greg))
6511 (yg (nth 2 greg)))
6512 (cond
6513 ((eq span 'day)
6514 (when n
6515 (setq sd (+ (calendar-absolute-from-gregorian
6516 (list mg 1 yg))
6517 n -1))))
6518 ((eq span 'week)
6519 (let* ((nt (calendar-day-of-week
6520 (calendar-gregorian-from-absolute sd)))
6521 (d (if org-agenda-start-on-weekday
6522 (- nt org-agenda-start-on-weekday)
6525 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6526 (when n
6527 (require 'cal-iso)
6528 (when (> n 99)
6529 (setq y1 (org-small-year-to-year (/ n 100))
6530 n (mod n 100)))
6531 (setq sd
6532 (calendar-absolute-from-iso
6533 (list n 1
6534 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6535 ((eq span 'month)
6536 (let (y1)
6537 (when (and n (> n 99))
6538 (setq y1 (org-small-year-to-year (/ n 100))
6539 n (mod n 100)))
6540 (setq sd (calendar-absolute-from-gregorian
6541 (list (or n mg) 1 (or y1 yg))))))
6542 ((eq span 'year)
6543 (setq sd (calendar-absolute-from-gregorian
6544 (list 1 1 (or n yg))))))
6545 sd))
6547 (defun org-agenda-next-date-line (&optional arg)
6548 "Jump to the next line indicating a date in agenda buffer."
6549 (interactive "p")
6550 (org-agenda-check-type t 'agenda 'timeline)
6551 (beginning-of-line 1)
6552 ;; This does not work if user makes date format that starts with a blank
6553 (if (looking-at "^\\S-") (forward-char 1))
6554 (if (not (re-search-forward "^\\S-" nil t arg))
6555 (progn
6556 (backward-char 1)
6557 (error "No next date after this line in this buffer")))
6558 (goto-char (match-beginning 0)))
6560 (defun org-agenda-previous-date-line (&optional arg)
6561 "Jump to the previous line indicating a date in agenda buffer."
6562 (interactive "p")
6563 (org-agenda-check-type t 'agenda 'timeline)
6564 (beginning-of-line 1)
6565 (if (not (re-search-backward "^\\S-" nil t arg))
6566 (error "No previous date before this line in this buffer")))
6568 ;; Initialize the highlight
6569 (defvar org-hl (make-overlay 1 1))
6570 (overlay-put org-hl 'face 'highlight)
6572 (defun org-highlight (begin end &optional buffer)
6573 "Highlight a region with overlay."
6574 (move-overlay org-hl begin end (or buffer (current-buffer))))
6576 (defun org-unhighlight ()
6577 "Detach overlay INDEX."
6578 (org-detach-overlay org-hl))
6580 ;; FIXME this is currently not used.
6581 (defun org-highlight-until-next-command (beg end &optional buffer)
6582 "Move the highlight overlay to BEG/END, remove it before the next command."
6583 (org-highlight beg end buffer)
6584 (add-hook 'pre-command-hook 'org-unhighlight-once))
6585 (defun org-unhighlight-once ()
6586 "Remove the highlight from its position, and this function from the hook."
6587 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6588 (org-unhighlight))
6590 (defun org-agenda-follow-mode ()
6591 "Toggle follow mode in an agenda buffer."
6592 (interactive)
6593 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6594 (org-agenda-set-mode-name)
6595 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6596 (org-agenda-show))
6597 (message "Follow mode is %s"
6598 (if org-agenda-follow-mode "on" "off")))
6600 (defun org-agenda-entry-text-mode (&optional arg)
6601 "Toggle entry text mode in an agenda buffer."
6602 (interactive "P")
6603 (setq org-agenda-entry-text-mode (or (integerp arg)
6604 (not org-agenda-entry-text-mode)))
6605 (org-agenda-entry-text-hide)
6606 (and org-agenda-entry-text-mode
6607 (let ((org-agenda-entry-text-maxlines
6608 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6609 (org-agenda-entry-text-show)))
6610 (org-agenda-set-mode-name)
6611 (message "Entry text mode is %s. Maximum number of lines is %d"
6612 (if org-agenda-entry-text-mode "on" "off")
6613 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6615 (defun org-agenda-clockreport-mode (&optional with-filter)
6616 "Toggle clocktable mode in an agenda buffer.
6617 With prefix arg WITH-FILTER, make the clocktable respect the current
6618 agenda filter."
6619 (interactive "P")
6620 (org-agenda-check-type t 'agenda)
6621 (if with-filter
6622 (setq org-agenda-clockreport-mode 'with-filter)
6623 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6624 (org-agenda-set-mode-name)
6625 (org-agenda-redo)
6626 (message "Clocktable mode is %s"
6627 (if org-agenda-clockreport-mode "on" "off")))
6629 (defun org-agenda-log-mode (&optional special)
6630 "Toggle log mode in an agenda buffer.
6631 With argument SPECIAL, show all possible log items, not only the ones
6632 configured in `org-agenda-log-mode-items'.
6633 With a double `C-u' prefix arg, show *only* log items, nothing else."
6634 (interactive "P")
6635 (org-agenda-check-type t 'agenda 'timeline)
6636 (setq org-agenda-show-log
6637 (cond
6638 ((equal special '(16)) 'only)
6639 ((eq special 'clockcheck)
6640 (if (eq org-agenda-show-log 'clockcheck)
6641 nil 'clockcheck))
6642 (special '(closed clock state))
6643 (t (not org-agenda-show-log))))
6644 (org-agenda-set-mode-name)
6645 (org-agenda-redo)
6646 (message "Log mode is %s"
6647 (if org-agenda-show-log "on" "off")))
6649 (defun org-agenda-archives-mode (&optional with-files)
6650 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6651 When called with a prefix argument, include all archive files as well."
6652 (interactive "P")
6653 (setq org-agenda-archives-mode
6654 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6655 (org-agenda-set-mode-name)
6656 (org-agenda-redo)
6657 (message
6658 "%s"
6659 (cond
6660 ((eq org-agenda-archives-mode nil)
6661 "No archives are included")
6662 ((eq org-agenda-archives-mode 'trees)
6663 (format "Trees with :%s: tag are included" org-archive-tag))
6664 ((eq org-agenda-archives-mode t)
6665 (format "Trees with :%s: tag and all active archive files are included"
6666 org-archive-tag)))))
6668 (defun org-agenda-toggle-diary ()
6669 "Toggle diary inclusion in an agenda buffer."
6670 (interactive)
6671 (org-agenda-check-type t 'agenda)
6672 (setq org-agenda-include-diary (not org-agenda-include-diary))
6673 (org-agenda-redo)
6674 (org-agenda-set-mode-name)
6675 (message "Diary inclusion turned %s"
6676 (if org-agenda-include-diary "on" "off")))
6678 (defun org-agenda-toggle-deadlines ()
6679 "Toggle inclusion of entries with a deadline in an agenda buffer."
6680 (interactive)
6681 (org-agenda-check-type t 'agenda)
6682 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6683 (org-agenda-redo)
6684 (org-agenda-set-mode-name)
6685 (message "Deadlines inclusion turned %s"
6686 (if org-agenda-include-deadlines "on" "off")))
6688 (defun org-agenda-toggle-time-grid ()
6689 "Toggle time grid in an agenda buffer."
6690 (interactive)
6691 (org-agenda-check-type t 'agenda)
6692 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6693 (org-agenda-redo)
6694 (org-agenda-set-mode-name)
6695 (message "Time-grid turned %s"
6696 (if org-agenda-use-time-grid "on" "off")))
6698 (defun org-agenda-set-mode-name ()
6699 "Set the mode name to indicate all the small mode settings."
6700 (setq mode-name
6701 (list "Org-Agenda"
6702 (if (get 'org-agenda-files 'org-restrict) " []" "")
6704 '(:eval (org-agenda-span-name org-agenda-current-span))
6705 (if org-agenda-follow-mode " Follow" "")
6706 (if org-agenda-entry-text-mode " ETxt" "")
6707 (if org-agenda-include-diary " Diary" "")
6708 (if org-agenda-include-deadlines " Ddl" "")
6709 (if org-agenda-use-time-grid " Grid" "")
6710 (if (and (boundp 'org-habit-show-habits)
6711 org-habit-show-habits) " Habit" "")
6712 (cond
6713 ((consp org-agenda-show-log) " LogAll")
6714 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6715 (org-agenda-show-log " Log")
6716 (t ""))
6717 (if (or org-agenda-filter (get 'org-agenda-filter
6718 :preset-filter))
6719 (concat " {" (mapconcat
6720 'identity
6721 (append (get 'org-agenda-filter
6722 :preset-filter)
6723 org-agenda-filter) "") "}")
6725 (if org-agenda-archives-mode
6726 (if (eq org-agenda-archives-mode t)
6727 " Archives"
6728 (format " :%s:" org-archive-tag))
6730 (if org-agenda-clockreport-mode
6731 (if (eq org-agenda-clockreport-mode 'with-filter)
6732 " Clock{}" " Clock")
6733 "")))
6734 (force-mode-line-update))
6736 (defun org-agenda-post-command-hook ()
6737 (setq org-agenda-type
6738 (or (get-text-property (point) 'org-agenda-type)
6739 (get-text-property (max (point-min) (1- (point)))
6740 'org-agenda-type))))
6742 (defun org-agenda-next-line ()
6743 "Move cursor to the next line, and show if follow mode is active."
6744 (interactive)
6745 (call-interactively 'next-line)
6746 (org-agenda-do-context-action))
6748 (defun org-agenda-previous-line ()
6749 "Move cursor to the previous line, and show if follow-mode is active."
6750 (interactive)
6751 (call-interactively 'previous-line)
6752 (org-agenda-do-context-action))
6754 (defun org-agenda-do-context-action ()
6755 "Show outline path and, maybe, follow mode window."
6756 (let ((m (org-get-at-bol 'org-marker)))
6757 (if (and org-agenda-follow-mode m)
6758 (org-agenda-show))
6759 (if (and m org-agenda-show-outline-path)
6760 (org-with-point-at m
6761 (org-display-outline-path t)))))
6763 (defun org-agenda-show-priority ()
6764 "Show the priority of the current item.
6765 This priority is composed of the main priority given with the [#A] cookies,
6766 and by additional input from the age of a schedules or deadline entry."
6767 (interactive)
6768 (let* ((pri (org-get-at-bol 'priority)))
6769 (message "Priority is %d" (if pri pri -1000))))
6771 (defun org-agenda-show-tags ()
6772 "Show the tags applicable to the current item."
6773 (interactive)
6774 (let* ((tags (org-get-at-bol 'tags)))
6775 (if tags
6776 (message "Tags are :%s:"
6777 (org-no-properties (mapconcat 'identity tags ":")))
6778 (message "No tags associated with this line"))))
6780 (defun org-agenda-goto (&optional highlight)
6781 "Go to the Org-mode file which contains the item at point."
6782 (interactive)
6783 (let* ((marker (or (org-get-at-bol 'org-marker)
6784 (org-agenda-error)))
6785 (buffer (marker-buffer marker))
6786 (pos (marker-position marker)))
6787 (switch-to-buffer-other-window buffer)
6788 (widen)
6789 (push-mark)
6790 (goto-char pos)
6791 (when (org-mode-p)
6792 (org-show-context 'agenda)
6793 (save-excursion
6794 (and (outline-next-heading)
6795 (org-flag-heading nil)))) ; show the next heading
6796 (when (outline-invisible-p)
6797 (show-entry)) ; display invisible text
6798 (recenter (/ (window-height) 2))
6799 (run-hooks 'org-agenda-after-show-hook)
6800 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6802 (defvar org-agenda-after-show-hook nil
6803 "Normal hook run after an item has been shown from the agenda.
6804 Point is in the buffer where the item originated.")
6806 (defun org-agenda-kill ()
6807 "Kill the entry or subtree belonging to the current agenda entry."
6808 (interactive)
6809 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6810 (let* ((marker (or (org-get-at-bol 'org-marker)
6811 (org-agenda-error)))
6812 (buffer (marker-buffer marker))
6813 (pos (marker-position marker))
6814 (type (org-get-at-bol 'type))
6815 dbeg dend (n 0) conf)
6816 (org-with-remote-undo buffer
6817 (with-current-buffer buffer
6818 (save-excursion
6819 (goto-char pos)
6820 (if (and (org-mode-p) (not (member type '("sexp"))))
6821 (setq dbeg (progn (org-back-to-heading t) (point))
6822 dend (org-end-of-subtree t t))
6823 (setq dbeg (point-at-bol)
6824 dend (min (point-max) (1+ (point-at-eol)))))
6825 (goto-char dbeg)
6826 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6827 (setq conf (or (eq t org-agenda-confirm-kill)
6828 (and (numberp org-agenda-confirm-kill)
6829 (> n org-agenda-confirm-kill))))
6830 (and conf
6831 (not (y-or-n-p
6832 (format "Delete entry with %d lines in buffer \"%s\"? "
6833 n (buffer-name buffer))))
6834 (error "Abort"))
6835 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6836 (with-current-buffer buffer (delete-region dbeg dend))
6837 (message "Agenda item and source killed"))))
6839 (defvar org-archive-default-command)
6840 (defun org-agenda-archive-default ()
6841 "Archive the entry or subtree belonging to the current agenda entry."
6842 (interactive)
6843 (require 'org-archive)
6844 (org-agenda-archive-with org-archive-default-command))
6846 (defun org-agenda-archive-default-with-confirmation ()
6847 "Archive the entry or subtree belonging to the current agenda entry."
6848 (interactive)
6849 (require 'org-archive)
6850 (org-agenda-archive-with org-archive-default-command 'confirm))
6852 (defun org-agenda-archive ()
6853 "Archive the entry or subtree belonging to the current agenda entry."
6854 (interactive)
6855 (org-agenda-archive-with 'org-archive-subtree))
6857 (defun org-agenda-archive-to-archive-sibling ()
6858 "Move the entry to the archive sibling."
6859 (interactive)
6860 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6862 (defun org-agenda-archive-with (cmd &optional confirm)
6863 "Move the entry to the archive sibling."
6864 (interactive)
6865 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6866 (let* ((marker (or (org-get-at-bol 'org-marker)
6867 (org-agenda-error)))
6868 (buffer (marker-buffer marker))
6869 (pos (marker-position marker)))
6870 (org-with-remote-undo buffer
6871 (with-current-buffer buffer
6872 (if (org-mode-p)
6873 (if (and confirm
6874 (not (y-or-n-p "Archive this subtree or entry? ")))
6875 (error "Abort")
6876 (save-excursion
6877 (goto-char pos)
6878 (org-remove-subtree-entries-from-agenda)
6879 (org-back-to-heading t)
6880 (funcall cmd)))
6881 (error "Archiving works only in Org-mode files"))))))
6883 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6884 "Remove all lines in the agenda that correspond to a given subtree.
6885 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6886 If this information is not given, the function uses the tree at point."
6887 (let ((buf (or buf (current-buffer))) m p)
6888 (save-excursion
6889 (unless (and beg end)
6890 (org-back-to-heading t)
6891 (setq beg (point))
6892 (org-end-of-subtree t)
6893 (setq end (point)))
6894 (set-buffer (get-buffer org-agenda-buffer-name))
6895 (save-excursion
6896 (goto-char (point-max))
6897 (beginning-of-line 1)
6898 (while (not (bobp))
6899 (when (and (setq m (org-get-at-bol 'org-marker))
6900 (equal buf (marker-buffer m))
6901 (setq p (marker-position m))
6902 (>= p beg)
6903 (< p end))
6904 (let ((inhibit-read-only t))
6905 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6906 (beginning-of-line 0))))))
6908 (defun org-agenda-refile (&optional goto rfloc no-update)
6909 "Refile the item at point."
6910 (interactive "P")
6911 (if (equal goto '(16))
6912 (org-refile-goto-last-stored)
6913 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6914 (org-agenda-error)))
6915 (buffer (marker-buffer marker))
6916 (pos (marker-position marker))
6917 (rfloc (or rfloc
6918 (org-refile-get-location
6919 (if goto "Goto" "Refile to") buffer
6920 org-refile-allow-creating-parent-nodes))))
6921 (with-current-buffer buffer
6922 (save-excursion
6923 (save-restriction
6924 (widen)
6925 (goto-char marker)
6926 (org-remove-subtree-entries-from-agenda)
6927 (org-refile goto buffer rfloc)))))
6928 (unless no-update (org-agenda-redo))))
6930 (defun org-agenda-open-link (&optional arg)
6931 "Follow the link in the current line, if any.
6932 This looks for a link in the displayed line in the agenda. It also looks
6933 at the text of the entry itself."
6934 (interactive "P")
6935 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6936 (org-get-at-bol 'org-marker)))
6937 (buffer (and marker (marker-buffer marker)))
6938 (prefix (buffer-substring
6939 (point-at-bol) (point-at-eol))))
6940 (cond
6941 (buffer
6942 (with-current-buffer buffer
6943 (save-excursion
6944 (save-restriction
6945 (widen)
6946 (goto-char marker)
6947 (org-offer-links-in-entry arg prefix)))))
6948 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6949 (save-excursion
6950 (beginning-of-line 1)
6951 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6952 (org-open-link-from-string (match-string 1)))
6953 (t (error "No link to open here")))))
6955 (defun org-agenda-copy-local-variable (var)
6956 "Get a variable from a referenced buffer and install it here."
6957 (let ((m (org-get-at-bol 'org-marker)))
6958 (when (and m (buffer-live-p (marker-buffer m)))
6959 (org-set-local var (with-current-buffer (marker-buffer m)
6960 (symbol-value var))))))
6962 (defun org-agenda-switch-to (&optional delete-other-windows)
6963 "Go to the Org-mode file which contains the item at point."
6964 (interactive)
6965 (if (and org-return-follows-link
6966 (not (org-get-at-bol 'org-marker))
6967 (org-in-regexp org-bracket-link-regexp))
6968 (org-open-link-from-string (match-string 0))
6969 (let* ((marker (or (org-get-at-bol 'org-marker)
6970 (org-agenda-error)))
6971 (buffer (marker-buffer marker))
6972 (pos (marker-position marker)))
6973 (switch-to-buffer buffer)
6974 (and delete-other-windows (delete-other-windows))
6975 (widen)
6976 (goto-char pos)
6977 (when (org-mode-p)
6978 (org-show-context 'agenda)
6979 (save-excursion
6980 (and (outline-next-heading)
6981 (org-flag-heading nil))) ; show the next heading
6982 (when (outline-invisible-p)
6983 (show-entry)))))) ; display invisible text
6985 (defun org-agenda-goto-mouse (ev)
6986 "Go to the Org-mode file which contains the item at the mouse click."
6987 (interactive "e")
6988 (mouse-set-point ev)
6989 (org-agenda-goto))
6991 (defun org-agenda-show (&optional full-entry)
6992 "Display the Org-mode file which contains the item at point.
6993 With prefix argument FULL-ENTRY, make the entire entry visible
6994 if it was hidden in the outline."
6995 (interactive "P")
6996 (let ((win (selected-window)))
6997 (if full-entry
6998 (let ((org-show-entry-below t))
6999 (org-agenda-goto t))
7000 (org-agenda-goto t))
7001 (select-window win)))
7003 (defvar org-agenda-show-window nil)
7004 (defun org-agenda-show-and-scroll-up ()
7005 "Display the Org-mode file which contains the item at point.
7006 When called repeatedly, scroll the window that is displaying the buffer."
7007 (interactive)
7008 (let ((win (selected-window)))
7009 (if (and (window-live-p org-agenda-show-window)
7010 (eq this-command last-command))
7011 (progn
7012 (select-window org-agenda-show-window)
7013 (ignore-errors (scroll-up)))
7014 (org-agenda-goto t)
7015 (show-subtree)
7016 (setq org-agenda-show-window (selected-window)))
7017 (select-window win)))
7019 (defun org-agenda-show-scroll-down ()
7020 "Scroll down the window showing the agenda."
7021 (interactive)
7022 (let ((win (selected-window)))
7023 (when (window-live-p org-agenda-show-window)
7024 (select-window org-agenda-show-window)
7025 (ignore-errors (scroll-down))
7026 (select-window win))))
7028 (defun org-agenda-show-1 (&optional more)
7029 "Display the Org-mode file which contains the item at point.
7030 The prefix arg selects the amount of information to display:
7032 0 hide the subtree
7033 1 just show the entry according to defaults.
7034 2 show the children view
7035 3 show the subtree view
7036 4 show the entire subtree and any LOGBOOK drawers
7037 5 show the entire subtree and any drawers
7038 With prefix argument FULL-ENTRY, make the entire entry visible
7039 if it was hidden in the outline."
7040 (interactive "p")
7041 (let ((win (selected-window)))
7042 (org-agenda-goto t)
7043 (org-recenter-heading 1)
7044 (cond
7045 ((= more 0)
7046 (hide-subtree)
7047 (save-excursion
7048 (org-back-to-heading)
7049 (run-hook-with-args 'org-cycle-hook 'folded))
7050 (message "Remote: FOLDED"))
7051 ((and (org-called-interactively-p 'any) (= more 1))
7052 (message "Remote: show with default settings"))
7053 ((= more 2)
7054 (show-entry)
7055 (show-children)
7056 (save-excursion
7057 (org-back-to-heading)
7058 (run-hook-with-args 'org-cycle-hook 'children))
7059 (message "Remote: CHILDREN"))
7060 ((= more 3)
7061 (show-subtree)
7062 (save-excursion
7063 (org-back-to-heading)
7064 (run-hook-with-args 'org-cycle-hook 'subtree))
7065 (message "Remote: SUBTREE"))
7066 ((= more 4)
7067 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7068 (org-drawer-regexp
7069 (concat "^[ \t]*:\\("
7070 (mapconcat 'regexp-quote org-drawers "\\|")
7071 "\\):[ \t]*$")))
7072 (show-subtree)
7073 (save-excursion
7074 (org-back-to-heading)
7075 (org-cycle-hide-drawers 'subtree)))
7076 (message "Remote: SUBTREE AND LOGBOOK"))
7077 ((> more 4)
7078 (show-subtree)
7079 (message "Remote: SUBTREE AND ALL DRAWERS")))
7080 (select-window win)))
7082 (defun org-recenter-heading (n)
7083 (save-excursion
7084 (org-back-to-heading)
7085 (recenter n)))
7087 (defvar org-agenda-cycle-counter nil)
7088 (defun org-agenda-cycle-show (&optional n)
7089 "Show the current entry in another window, with default settings.
7090 Default settings are taken from `org-show-hierarchy-above' and siblings.
7091 When use repeatedly in immediate succession, the remote entry will cycle
7092 through visibility
7094 children -> subtree -> folded
7096 When called with a numeric prefix arg, that arg will be passed through to
7097 `org-agenda-show-1'. For the interpretation of that argument, see the
7098 docstring of `org-agenda-show-1'."
7099 (interactive "P")
7100 (if (integerp n)
7101 (setq org-agenda-cycle-counter n)
7102 (if (not (eq last-command this-command))
7103 (setq org-agenda-cycle-counter 1)
7104 (if (equal org-agenda-cycle-counter 0)
7105 (setq org-agenda-cycle-counter 2)
7106 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7107 (if (> org-agenda-cycle-counter 3)
7108 (setq org-agenda-cycle-counter 0)))))
7109 (org-agenda-show-1 org-agenda-cycle-counter))
7111 (defun org-agenda-recenter (arg)
7112 "Display the Org-mode file which contains the item at point and recenter."
7113 (interactive "P")
7114 (let ((win (selected-window)))
7115 (org-agenda-goto t)
7116 (recenter arg)
7117 (select-window win)))
7119 (defun org-agenda-show-mouse (ev)
7120 "Display the Org-mode file which contains the item at the mouse click."
7121 (interactive "e")
7122 (mouse-set-point ev)
7123 (org-agenda-show))
7125 (defun org-agenda-check-no-diary ()
7126 "Check if the entry is a diary link and abort if yes."
7127 (if (org-get-at-bol 'org-agenda-diary-link)
7128 (org-agenda-error)))
7130 (defun org-agenda-error ()
7131 (error "Command not allowed in this line"))
7133 (defun org-agenda-tree-to-indirect-buffer ()
7134 "Show the subtree corresponding to the current entry in an indirect buffer.
7135 This calls the command `org-tree-to-indirect-buffer' from the original
7136 Org-mode buffer.
7137 With numerical prefix arg ARG, go up to this level and then take that tree.
7138 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7139 use the dedicated frame)."
7140 (interactive)
7141 (org-agenda-check-no-diary)
7142 (let* ((marker (or (org-get-at-bol 'org-marker)
7143 (org-agenda-error)))
7144 (buffer (marker-buffer marker))
7145 (pos (marker-position marker)))
7146 (with-current-buffer buffer
7147 (save-excursion
7148 (goto-char pos)
7149 (call-interactively 'org-tree-to-indirect-buffer)))))
7151 (defvar org-last-heading-marker (make-marker)
7152 "Marker pointing to the headline that last changed its TODO state
7153 by a remote command from the agenda.")
7155 (defun org-agenda-todo-nextset ()
7156 "Switch TODO entry to next sequence."
7157 (interactive)
7158 (org-agenda-todo 'nextset))
7160 (defun org-agenda-todo-previousset ()
7161 "Switch TODO entry to previous sequence."
7162 (interactive)
7163 (org-agenda-todo 'previousset))
7165 (defun org-agenda-todo (&optional arg)
7166 "Cycle TODO state of line at point, also in Org-mode file.
7167 This changes the line at point, all other lines in the agenda referring to
7168 the same tree node, and the headline of the tree node in the Org-mode file."
7169 (interactive "P")
7170 (org-agenda-check-no-diary)
7171 (let* ((col (current-column))
7172 (marker (or (org-get-at-bol 'org-marker)
7173 (org-agenda-error)))
7174 (buffer (marker-buffer marker))
7175 (pos (marker-position marker))
7176 (hdmarker (org-get-at-bol 'org-hd-marker))
7177 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7178 (inhibit-read-only t)
7179 org-agenda-headline-snapshot-before-repeat newhead just-one)
7180 (org-with-remote-undo buffer
7181 (with-current-buffer buffer
7182 (widen)
7183 (goto-char pos)
7184 (org-show-context 'agenda)
7185 (save-excursion
7186 (and (outline-next-heading)
7187 (org-flag-heading nil))) ; show the next heading
7188 (let ((current-prefix-arg arg))
7189 (call-interactively 'org-todo))
7190 (and (bolp) (forward-char 1))
7191 (setq newhead (org-get-heading))
7192 (when (and (org-bound-and-true-p
7193 org-agenda-headline-snapshot-before-repeat)
7194 (not (equal org-agenda-headline-snapshot-before-repeat
7195 newhead))
7196 todayp)
7197 (setq newhead org-agenda-headline-snapshot-before-repeat
7198 just-one t))
7199 (save-excursion
7200 (org-back-to-heading)
7201 (move-marker org-last-heading-marker (point))))
7202 (beginning-of-line 1)
7203 (save-excursion
7204 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7205 (org-move-to-column col))))
7207 (defun org-agenda-add-note (&optional arg)
7208 "Add a time-stamped note to the entry at point."
7209 (interactive "P")
7210 (org-agenda-check-no-diary)
7211 (let* ((marker (or (org-get-at-bol 'org-marker)
7212 (org-agenda-error)))
7213 (buffer (marker-buffer marker))
7214 (pos (marker-position marker))
7215 (hdmarker (org-get-at-bol 'org-hd-marker))
7216 (inhibit-read-only t))
7217 (with-current-buffer buffer
7218 (widen)
7219 (goto-char pos)
7220 (org-show-context 'agenda)
7221 (save-excursion
7222 (and (outline-next-heading)
7223 (org-flag-heading nil))) ; show the next heading
7224 (org-add-note))))
7226 (defun org-agenda-change-all-lines (newhead hdmarker
7227 &optional fixface just-this)
7228 "Change all lines in the agenda buffer which match HDMARKER.
7229 The new content of the line will be NEWHEAD (as modified by
7230 `org-format-agenda-item'). HDMARKER is checked with
7231 `equal' against all `org-hd-marker' text properties in the file.
7232 If FIXFACE is non-nil, the face of each item is modified according to
7233 the new TODO state.
7234 If JUST-THIS is non-nil, change just the current line, not all.
7235 If FORCE-TAGS is non nil, the car of it returns the new tags."
7236 (let* ((inhibit-read-only t)
7237 (line (org-current-line))
7238 (thetags (with-current-buffer (marker-buffer hdmarker)
7239 (save-excursion (save-restriction (widen)
7240 (goto-char hdmarker)
7241 (org-get-tags-at)))))
7242 props m pl undone-face done-face finish new dotime cat tags)
7243 (save-excursion
7244 (goto-char (point-max))
7245 (beginning-of-line 1)
7246 (while (not finish)
7247 (setq finish (bobp))
7248 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7249 (or (not just-this) (= (org-current-line) line))
7250 (equal m hdmarker))
7251 (setq props (text-properties-at (point))
7252 dotime (org-get-at-bol 'dotime)
7253 cat (org-get-at-bol 'org-category)
7254 tags thetags
7256 (let ((org-prefix-format-compiled
7257 (or (get-text-property (point) 'format)
7258 org-prefix-format-compiled)))
7259 (with-current-buffer (marker-buffer hdmarker)
7260 (save-excursion
7261 (save-restriction
7262 (widen)
7263 (org-format-agenda-item (org-get-at-bol 'extra)
7264 newhead cat tags dotime)))))
7265 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7266 undone-face (org-get-at-bol 'undone-face)
7267 done-face (org-get-at-bol 'done-face))
7268 (beginning-of-line 1)
7269 (cond
7270 ((equal new "")
7271 (and (looking-at ".*\n?") (replace-match "")))
7272 ((looking-at ".*")
7273 (replace-match new t t)
7274 (beginning-of-line 1)
7275 (add-text-properties (point-at-bol) (point-at-eol) props)
7276 (when fixface
7277 (add-text-properties
7278 (point-at-bol) (point-at-eol)
7279 (list 'face
7280 (if org-last-todo-state-is-todo
7281 undone-face done-face))))
7282 (org-agenda-highlight-todo 'line)
7283 (beginning-of-line 1))
7284 (t (error "Line update did not work"))))
7285 (beginning-of-line 0)))
7286 (org-finalize-agenda)))
7288 (defun org-agenda-align-tags (&optional line)
7289 "Align all tags in agenda items to `org-agenda-tags-column'."
7290 (let ((inhibit-read-only t) l c)
7291 (save-excursion
7292 (goto-char (if line (point-at-bol) (point-min)))
7293 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7294 (if line (point-at-eol) nil) t)
7295 (add-text-properties
7296 (match-beginning 2) (match-end 2)
7297 (list 'face (delq nil (let ((prop (get-text-property
7298 (match-beginning 2) 'face)))
7299 (or (listp prop) (setq prop (list prop)))
7300 (if (memq 'org-tag prop)
7301 prop
7302 (cons 'org-tag prop))))))
7303 (setq l (- (match-end 2) (match-beginning 2))
7304 c (if (< org-agenda-tags-column 0)
7305 (- (abs org-agenda-tags-column) l)
7306 org-agenda-tags-column))
7307 (delete-region (match-beginning 1) (match-end 1))
7308 (goto-char (match-beginning 1))
7309 (insert (org-add-props
7310 (make-string (max 1 (- c (current-column))) ?\ )
7311 (plist-put (copy-sequence (text-properties-at (point)))
7312 'face nil))))
7313 (goto-char (point-min))
7314 (org-font-lock-add-tag-faces (point-max)))))
7316 (defun org-agenda-priority-up ()
7317 "Increase the priority of line at point, also in Org-mode file."
7318 (interactive)
7319 (org-agenda-priority 'up))
7321 (defun org-agenda-priority-down ()
7322 "Decrease the priority of line at point, also in Org-mode file."
7323 (interactive)
7324 (org-agenda-priority 'down))
7326 (defun org-agenda-priority (&optional force-direction)
7327 "Set the priority of line at point, also in Org-mode file.
7328 This changes the line at point, all other lines in the agenda referring to
7329 the same tree node, and the headline of the tree node in the Org-mode file."
7330 (interactive)
7331 (unless org-enable-priority-commands
7332 (error "Priority commands are disabled"))
7333 (org-agenda-check-no-diary)
7334 (let* ((marker (or (org-get-at-bol 'org-marker)
7335 (org-agenda-error)))
7336 (hdmarker (org-get-at-bol 'org-hd-marker))
7337 (buffer (marker-buffer hdmarker))
7338 (pos (marker-position hdmarker))
7339 (inhibit-read-only t)
7340 newhead)
7341 (org-with-remote-undo buffer
7342 (with-current-buffer buffer
7343 (widen)
7344 (goto-char pos)
7345 (org-show-context 'agenda)
7346 (save-excursion
7347 (and (outline-next-heading)
7348 (org-flag-heading nil))) ; show the next heading
7349 (funcall 'org-priority force-direction)
7350 (end-of-line 1)
7351 (setq newhead (org-get-heading)))
7352 (org-agenda-change-all-lines newhead hdmarker)
7353 (beginning-of-line 1))))
7355 ;; FIXME: should fix the tags property of the agenda line.
7356 (defun org-agenda-set-tags (&optional tag onoff)
7357 "Set tags for the current headline."
7358 (interactive)
7359 (org-agenda-check-no-diary)
7360 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7361 (call-interactively 'org-change-tag-in-region)
7362 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7363 (org-agenda-error)))
7364 (buffer (marker-buffer hdmarker))
7365 (pos (marker-position hdmarker))
7366 (inhibit-read-only t)
7367 newhead)
7368 (org-with-remote-undo buffer
7369 (with-current-buffer buffer
7370 (widen)
7371 (goto-char pos)
7372 (save-excursion
7373 (org-show-context 'agenda))
7374 (save-excursion
7375 (and (outline-next-heading)
7376 (org-flag-heading nil))) ; show the next heading
7377 (goto-char pos)
7378 (if tag
7379 (org-toggle-tag tag onoff)
7380 (call-interactively 'org-set-tags))
7381 (end-of-line 1)
7382 (setq newhead (org-get-heading)))
7383 (org-agenda-change-all-lines newhead hdmarker)
7384 (beginning-of-line 1)))))
7386 (defun org-agenda-set-property ()
7387 "Set a property for the current headline."
7388 (interactive)
7389 (org-agenda-check-no-diary)
7390 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7391 (org-agenda-error)))
7392 (buffer (marker-buffer hdmarker))
7393 (pos (marker-position hdmarker))
7394 (inhibit-read-only t)
7395 newhead)
7396 (org-with-remote-undo buffer
7397 (with-current-buffer buffer
7398 (widen)
7399 (goto-char pos)
7400 (save-excursion
7401 (org-show-context 'agenda))
7402 (save-excursion
7403 (and (outline-next-heading)
7404 (org-flag-heading nil))) ; show the next heading
7405 (goto-char pos)
7406 (call-interactively 'org-set-property)))))
7408 (defun org-agenda-set-effort ()
7409 "Set the effort property for the current headline."
7410 (interactive)
7411 (org-agenda-check-no-diary)
7412 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7413 (org-agenda-error)))
7414 (buffer (marker-buffer hdmarker))
7415 (pos (marker-position hdmarker))
7416 (inhibit-read-only t)
7417 newhead)
7418 (org-with-remote-undo buffer
7419 (with-current-buffer buffer
7420 (widen)
7421 (goto-char pos)
7422 (save-excursion
7423 (org-show-context 'agenda))
7424 (save-excursion
7425 (and (outline-next-heading)
7426 (org-flag-heading nil))) ; show the next heading
7427 (goto-char pos)
7428 (call-interactively 'org-set-effort)
7429 (end-of-line 1)
7430 (setq newhead (org-get-heading)))
7431 (org-agenda-change-all-lines newhead hdmarker))))
7433 (defun org-agenda-toggle-archive-tag ()
7434 "Toggle the archive tag for the current entry."
7435 (interactive)
7436 (org-agenda-check-no-diary)
7437 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7438 (org-agenda-error)))
7439 (buffer (marker-buffer hdmarker))
7440 (pos (marker-position hdmarker))
7441 (inhibit-read-only t)
7442 newhead)
7443 (org-with-remote-undo buffer
7444 (with-current-buffer buffer
7445 (widen)
7446 (goto-char pos)
7447 (org-show-context 'agenda)
7448 (save-excursion
7449 (and (outline-next-heading)
7450 (org-flag-heading nil))) ; show the next heading
7451 (call-interactively 'org-toggle-archive-tag)
7452 (end-of-line 1)
7453 (setq newhead (org-get-heading)))
7454 (org-agenda-change-all-lines newhead hdmarker)
7455 (beginning-of-line 1))))
7457 (defun org-agenda-do-date-later (arg)
7458 (interactive "P")
7459 (cond
7460 ((or (equal arg '(16))
7461 (memq last-command
7462 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7463 (setq this-command 'org-agenda-date-later-minutes)
7464 (org-agenda-date-later-minutes 1))
7465 ((or (equal arg '(4))
7466 (memq last-command
7467 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7468 (setq this-command 'org-agenda-date-later-hours)
7469 (org-agenda-date-later-hours 1))
7471 (org-agenda-date-later (prefix-numeric-value arg)))))
7473 (defun org-agenda-do-date-earlier (arg)
7474 (interactive "P")
7475 (cond
7476 ((or (equal arg '(16))
7477 (memq last-command
7478 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7479 (setq this-command 'org-agenda-date-earlier-minutes)
7480 (org-agenda-date-earlier-minutes 1))
7481 ((or (equal arg '(4))
7482 (memq last-command
7483 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7484 (setq this-command 'org-agenda-date-earlier-hours)
7485 (org-agenda-date-earlier-hours 1))
7487 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7489 (defun org-agenda-date-later (arg &optional what)
7490 "Change the date of this item to ARG day(s) later."
7491 (interactive "p")
7492 (org-agenda-check-type t 'agenda 'timeline)
7493 (org-agenda-check-no-diary)
7494 (let* ((marker (or (org-get-at-bol 'org-marker)
7495 (org-agenda-error)))
7496 (buffer (marker-buffer marker))
7497 (pos (marker-position marker)))
7498 (org-with-remote-undo buffer
7499 (with-current-buffer buffer
7500 (widen)
7501 (goto-char pos)
7502 (if (not (org-at-timestamp-p))
7503 (error "Cannot find time stamp"))
7504 (org-timestamp-change arg (or what 'day)))
7505 (org-agenda-show-new-time marker org-last-changed-timestamp))
7506 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7508 (defun org-agenda-date-earlier (arg &optional what)
7509 "Change the date of this item to ARG day(s) earlier."
7510 (interactive "p")
7511 (org-agenda-date-later (- arg) what))
7513 (defun org-agenda-date-later-minutes (arg)
7514 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7515 (interactive "p")
7516 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7517 (org-agenda-date-later arg 'minute))
7519 (defun org-agenda-date-earlier-minutes (arg)
7520 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7521 (interactive "p")
7522 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7523 (org-agenda-date-earlier arg 'minute))
7525 (defun org-agenda-date-later-hours (arg)
7526 "Change the time of this item, in hour steps."
7527 (interactive "p")
7528 (org-agenda-date-later arg 'hour))
7530 (defun org-agenda-date-earlier-hours (arg)
7531 "Change the time of this item, in hour steps."
7532 (interactive "p")
7533 (org-agenda-date-earlier arg 'hour))
7535 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7536 "Show new date stamp via text properties."
7537 ;; We use text properties to make this undoable
7538 (let ((inhibit-read-only t)
7539 (buffer-invisibility-spec))
7540 (setq stamp (concat " " prefix " => " stamp))
7541 (save-excursion
7542 (goto-char (point-max))
7543 (while (not (bobp))
7544 (when (equal marker (org-get-at-bol 'org-marker))
7545 (org-move-to-column (- (window-width) (length stamp)) t)
7546 (org-agenda-fix-tags-filter-overlays-at (point))
7547 (if (featurep 'xemacs)
7548 ;; Use `duplicable' property to trigger undo recording
7549 (let ((ex (make-extent nil nil))
7550 (gl (make-glyph stamp)))
7551 (set-glyph-face gl 'secondary-selection)
7552 (set-extent-properties
7553 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7554 (insert-extent ex (1- (point)) (point-at-eol)))
7555 (add-text-properties
7556 (1- (point)) (point-at-eol)
7557 (list 'display (org-add-props stamp nil
7558 'face 'secondary-selection))))
7559 (beginning-of-line 1))
7560 (beginning-of-line 0)))))
7562 (defun org-agenda-date-prompt (arg)
7563 "Change the date of this item. Date is prompted for, with default today.
7564 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7565 be used to request time specification in the time stamp."
7566 (interactive "P")
7567 (org-agenda-check-type t 'agenda 'timeline)
7568 (org-agenda-check-no-diary)
7569 (let* ((marker (or (org-get-at-bol 'org-marker)
7570 (org-agenda-error)))
7571 (buffer (marker-buffer marker))
7572 (pos (marker-position marker)))
7573 (org-with-remote-undo buffer
7574 (with-current-buffer buffer
7575 (widen)
7576 (goto-char pos)
7577 (if (not (org-at-timestamp-p t))
7578 (error "Cannot find time stamp"))
7579 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7580 (org-agenda-show-new-time marker org-last-changed-timestamp))
7581 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7583 (defun org-agenda-schedule (arg &optional time)
7584 "Schedule the item at point.
7585 ARG is passed through to `org-schedule'."
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 (type (marker-insertion-type marker))
7592 (buffer (marker-buffer marker))
7593 (pos (marker-position marker))
7594 (org-insert-labeled-timestamps-at-point nil)
7596 (set-marker-insertion-type marker t)
7597 (org-with-remote-undo buffer
7598 (with-current-buffer buffer
7599 (widen)
7600 (goto-char pos)
7601 (setq ts (org-schedule arg time)))
7602 (org-agenda-show-new-time marker ts "S"))
7603 (message "Item scheduled for %s" ts)))
7605 (defun org-agenda-deadline (arg &optional time)
7606 "Schedule the item at point.
7607 ARG is passed through to `org-deadline'."
7608 (interactive "P")
7609 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7610 (org-agenda-check-no-diary)
7611 (let* ((marker (or (org-get-at-bol 'org-marker)
7612 (org-agenda-error)))
7613 (buffer (marker-buffer marker))
7614 (pos (marker-position marker))
7615 (org-insert-labeled-timestamps-at-point nil)
7617 (org-with-remote-undo buffer
7618 (with-current-buffer buffer
7619 (widen)
7620 (goto-char pos)
7621 (setq ts (org-deadline arg time)))
7622 (org-agenda-show-new-time marker ts "D"))
7623 (message "Deadline for this item set to %s" ts)))
7625 (defun org-agenda-action ()
7626 "Select entry for agenda action, or execute an agenda action.
7627 This command prompts for another letter. Valid inputs are:
7629 m Mark the entry at point for an agenda action
7630 s Schedule the marked entry to the date at the cursor
7631 d Set the deadline of the marked entry to the date at the cursor
7632 r Call `org-remember' with cursor date as the default date
7633 c Call `org-capture' with cursor date as the default date
7634 SPC Show marked entry in other window
7635 TAB Visit marked entry in other window
7637 The cursor may be at a date in the calendar, or in the Org agenda."
7638 (interactive)
7639 (let (ans)
7640 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7641 (setq ans (read-char-exclusive))
7642 (cond
7643 ((equal ans ?m)
7644 ;; Mark this entry
7645 (if (eq major-mode 'org-agenda-mode)
7646 (let ((m (or (org-get-at-bol 'org-hd-marker)
7647 (org-get-at-bol 'org-marker))))
7648 (if m
7649 (progn
7650 (move-marker org-agenda-action-marker
7651 (marker-position m) (marker-buffer m))
7652 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7653 (error "Don't know which entry to mark")))
7654 (error "This command works only in the agenda")))
7655 ((equal ans ?s)
7656 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7657 ((equal ans ?d)
7658 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7659 ((equal ans ?r)
7660 (org-agenda-do-action '(org-remember) t))
7661 ((equal ans ?c)
7662 (org-agenda-do-action '(org-capture) t))
7663 ((equal ans ?\ )
7664 (let ((cw (selected-window)))
7665 (org-switch-to-buffer-other-window
7666 (marker-buffer org-agenda-action-marker))
7667 (goto-char org-agenda-action-marker)
7668 (org-show-context 'agenda)
7669 (select-window cw)))
7670 ((equal ans ?\C-i)
7671 (org-switch-to-buffer-other-window
7672 (marker-buffer org-agenda-action-marker))
7673 (goto-char org-agenda-action-marker)
7674 (org-show-context 'agenda))
7675 (t (error "Invalid agenda action %c" ans)))))
7677 (defun org-agenda-do-action (form &optional current-buffer)
7678 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7679 (let ((org-overriding-default-time (org-get-cursor-date)))
7680 (if current-buffer
7681 (eval form)
7682 (if (not (marker-buffer org-agenda-action-marker))
7683 (error "No entry has been selected for agenda action")
7684 (with-current-buffer (marker-buffer org-agenda-action-marker)
7685 (save-excursion
7686 (save-restriction
7687 (widen)
7688 (goto-char org-agenda-action-marker)
7689 (eval form))))))))
7691 (defun org-agenda-clock-in (&optional arg)
7692 "Start the clock on the currently selected item."
7693 (interactive "P")
7694 (org-agenda-check-no-diary)
7695 (if (equal arg '(4))
7696 (org-clock-in arg)
7697 (let* ((marker (or (org-get-at-bol 'org-marker)
7698 (org-agenda-error)))
7699 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7700 marker))
7701 (pos (marker-position marker))
7702 newhead)
7703 (org-with-remote-undo (marker-buffer marker)
7704 (with-current-buffer (marker-buffer marker)
7705 (widen)
7706 (goto-char pos)
7707 (org-show-context 'agenda)
7708 (org-show-entry)
7709 (org-cycle-hide-drawers 'children)
7710 (org-clock-in arg)
7711 (setq newhead (org-get-heading)))
7712 (org-agenda-change-all-lines newhead hdmarker)))))
7714 (defun org-agenda-clock-out ()
7715 "Stop the currently running clock."
7716 (interactive)
7717 (unless (marker-buffer org-clock-marker)
7718 (error "No running clock"))
7719 (let ((marker (make-marker)) newhead)
7720 (org-with-remote-undo (marker-buffer org-clock-marker)
7721 (with-current-buffer (marker-buffer org-clock-marker)
7722 (save-excursion
7723 (save-restriction
7724 (widen)
7725 (goto-char org-clock-marker)
7726 (org-back-to-heading t)
7727 (move-marker marker (point))
7728 (org-clock-out)
7729 (setq newhead (org-get-heading))))))
7730 (org-agenda-change-all-lines newhead marker)
7731 (move-marker marker nil)))
7733 (defun org-agenda-clock-cancel (&optional arg)
7734 "Cancel the currently running clock."
7735 (interactive "P")
7736 (unless (marker-buffer org-clock-marker)
7737 (error "No running clock"))
7738 (org-with-remote-undo (marker-buffer org-clock-marker)
7739 (org-clock-cancel)))
7741 (defun org-agenda-clock-goto ()
7742 "Jump to the currently clocked in task within the agenda.
7743 If the currently clocked in task is not listed in the agenda
7744 buffer, display it in another window."
7745 (interactive)
7746 (let (pos)
7747 (mapc (lambda (o)
7748 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7749 (setq pos (overlay-start o))))
7750 (overlays-in (point-min) (point-max)))
7751 (cond (pos (goto-char pos))
7752 ;; If the currently clocked entry is not in the agenda
7753 ;; buffer, we visit it in another window:
7754 (org-clock-current-task
7755 (org-switch-to-buffer-other-window (org-clock-goto)))
7756 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7758 (defun org-agenda-diary-entry-in-org-file ()
7759 "Make a diary entry in the file `org-agenda-diary-file'."
7760 (let (d1 d2 char (text "") dp1 dp2)
7761 (if (equal (buffer-name) "*Calendar*")
7762 (setq d1 (calendar-cursor-to-date t)
7763 d2 (car calendar-mark-ring))
7764 (setq dp1 (get-text-property (point-at-bol) 'day))
7765 (unless dp1 (error "No date defined in current line"))
7766 (setq d1 (calendar-gregorian-from-absolute dp1)
7767 d2 (and (ignore-errors (mark))
7768 (save-excursion
7769 (goto-char (mark))
7770 (setq dp2 (get-text-property (point-at-bol) 'day)))
7771 (calendar-gregorian-from-absolute dp2))))
7772 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7773 (setq char (read-char-exclusive))
7774 (cond
7775 ((equal char ?d)
7776 (setq text (read-string "Day entry: "))
7777 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7778 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7779 ((equal char ?a)
7780 (setq d1 (list (car d1) (nth 1 d1)
7781 (read-number (format "Reference year [%d]: " (nth 2 d1))
7782 (nth 2 d1))))
7783 (setq text (read-string "Anniversary (use %d to show years): "))
7784 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7785 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7786 ((equal char ?b)
7787 (setq text (read-string "Block entry: "))
7788 (unless (and d1 d2 (not (equal d1 d2)))
7789 (error "No block of days selected"))
7790 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7791 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7792 ((equal char ?j)
7793 (org-switch-to-buffer-other-window
7794 (find-file-noselect org-agenda-diary-file))
7795 (require 'org-datetree)
7796 (org-datetree-find-date-create d1)
7797 (org-reveal t))
7798 (t (error "Invalid selection character `%c'" char)))))
7800 (defcustom org-agenda-insert-diary-strategy 'date-tree
7801 "Where in `org-agenda-diary-file' should new entries be added?
7802 Valid values:
7804 date-tree in the date tree, as child of the date
7805 top-level as top-level entries at the end of the file."
7806 :group 'org-agenda
7807 :type '(choice
7808 (const :tag "in a date tree" date-tree)
7809 (const :tag "as top level at end of file" top-level)))
7811 (defcustom org-agenda-insert-diary-extract-time nil
7812 "Non-nil means extract any time specification from the diary entry."
7813 :group 'org-agenda
7814 :type 'boolean)
7816 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7817 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7818 If TEXT is not empty, it will become the headline of the new entry, and
7819 the resulting entry will not be shown. When TEXT is empty, switch to
7820 `org-agenda-diary-file' and let the user finish the entry there."
7821 (let ((cw (current-window-configuration)))
7822 (org-switch-to-buffer-other-window
7823 (find-file-noselect org-agenda-diary-file))
7824 (widen)
7825 (goto-char (point-min))
7826 (cond
7827 ((eq type 'anniversary)
7828 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7829 (progn
7830 (or (org-on-heading-p t)
7831 (progn
7832 (outline-next-heading)
7833 (insert "* Anniversaries\n\n")
7834 (beginning-of-line -1)))))
7835 (outline-next-heading)
7836 (org-back-over-empty-lines)
7837 (backward-char 1)
7838 (insert "\n")
7839 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7840 (nth 2 d1) (car d1) (nth 1 d1) text)))
7841 ((eq type 'day)
7842 (let ((org-prefix-has-time t)
7843 (org-agenda-time-leading-zero t)
7844 fmt time time2)
7845 (if org-agenda-insert-diary-extract-time
7846 ;; Use org-format-agenda-item to parse text for a time-range and
7847 ;; remove it. FIXME: This is a hack, we should refactor
7848 ;; that function to make time extraction available separately
7849 (setq fmt (org-format-agenda-item nil text nil nil t)
7850 time (get-text-property 0 'time fmt)
7851 time2 (if (> (length time) 0)
7852 ;; split-string removes trailing ...... if
7853 ;; no end time given. First space
7854 ;; separates time from date.
7855 (concat " " (car (split-string time "\\.")))
7856 nil)
7857 text (get-text-property 0 'txt fmt)))
7858 (if (eq org-agenda-insert-diary-strategy 'top-level)
7859 (org-agenda-insert-diary-as-top-level text)
7860 (require 'org-datetree)
7861 (org-datetree-find-date-create d1)
7862 (org-agenda-insert-diary-make-new-entry text))
7863 (org-insert-time-stamp (org-time-from-absolute
7864 (calendar-absolute-from-gregorian d1))
7865 nil nil nil nil time2))
7866 (end-of-line 0))
7867 ((eq type 'block)
7868 (if (> (calendar-absolute-from-gregorian d1)
7869 (calendar-absolute-from-gregorian d2))
7870 (setq d1 (prog1 d2 (setq d2 d1))))
7871 (if (eq org-agenda-insert-diary-strategy 'top-level)
7872 (org-agenda-insert-diary-as-top-level text)
7873 (require 'org-datetree)
7874 (org-datetree-find-date-create d1)
7875 (org-agenda-insert-diary-make-new-entry text))
7876 (org-insert-time-stamp (org-time-from-absolute
7877 (calendar-absolute-from-gregorian d1)))
7878 (insert "--")
7879 (org-insert-time-stamp (org-time-from-absolute
7880 (calendar-absolute-from-gregorian d2)))
7881 (end-of-line 0)))
7882 (if (string-match "\\S-" text)
7883 (progn
7884 (set-window-configuration cw)
7885 (message "%s entry added to %s"
7886 (capitalize (symbol-name type))
7887 (abbreviate-file-name org-agenda-diary-file)))
7888 (org-reveal t)
7889 (message "Please finish entry here"))))
7891 (defun org-agenda-insert-diary-as-top-level (text)
7892 "Make new entry as a top-level entry at the end of the file.
7893 Add TEXT as headline, and position the cursor in the second line so that
7894 a timestamp can be added there."
7895 (widen)
7896 (goto-char (point-max))
7897 (or (bolp) (insert "\n"))
7898 (insert "* " text "\n")
7899 (if org-adapt-indentation (org-indent-to-column 2)))
7901 (defun org-agenda-insert-diary-make-new-entry (text)
7902 "Make new entry as last child of current entry.
7903 Add TEXT as headline, and position the cursor in the second line so that
7904 a timestamp can be added there."
7905 (let ((org-show-following-heading t)
7906 (org-show-siblings t)
7907 (org-show-hierarchy-above t)
7908 (org-show-entry-below t)
7909 col)
7910 (outline-next-heading)
7911 (org-back-over-empty-lines)
7912 (or (looking-at "[ \t]*$")
7913 (progn (insert "\n") (backward-char 1)))
7914 (org-insert-heading nil t)
7915 (org-do-demote)
7916 (setq col (current-column))
7917 (insert text "\n")
7918 (if org-adapt-indentation (org-indent-to-column col))
7919 (let ((org-show-following-heading t)
7920 (org-show-siblings t)
7921 (org-show-hierarchy-above t)
7922 (org-show-entry-below t))
7923 (org-show-context))))
7925 (defun org-agenda-diary-entry ()
7926 "Make a diary entry, like the `i' command from the calendar.
7927 All the standard commands work: block, weekly etc.
7928 When `org-agenda-diary-file' points to a file,
7929 `org-agenda-diary-entry-in-org-file' is called instead to create
7930 entries in that Org-mode file."
7931 (interactive)
7932 (org-agenda-check-type t 'agenda 'timeline)
7933 (if (not (eq org-agenda-diary-file 'diary-file))
7934 (org-agenda-diary-entry-in-org-file)
7935 (require 'diary-lib)
7936 (let* ((char (progn
7937 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7938 (read-char-exclusive)))
7939 (cmd (cdr (assoc char
7940 '((?d . insert-diary-entry)
7941 (?w . insert-weekly-diary-entry)
7942 (?m . insert-monthly-diary-entry)
7943 (?y . insert-yearly-diary-entry)
7944 (?a . insert-anniversary-diary-entry)
7945 (?b . insert-block-diary-entry)
7946 (?c . insert-cyclic-diary-entry)))))
7947 (oldf (symbol-function 'calendar-cursor-to-date))
7948 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7949 (point (point))
7950 (mark (or (mark t) (point))))
7951 (unless cmd
7952 (error "No command associated with <%c>" char))
7953 (unless (and (get-text-property point 'day)
7954 (or (not (equal ?b char))
7955 (get-text-property mark 'day)))
7956 (error "Don't know which date to use for diary entry"))
7957 ;; We implement this by hacking the `calendar-cursor-to-date' function
7958 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7959 (let ((calendar-mark-ring
7960 (list (calendar-gregorian-from-absolute
7961 (or (get-text-property mark 'day)
7962 (get-text-property point 'day))))))
7963 (unwind-protect
7964 (progn
7965 (fset 'calendar-cursor-to-date
7966 (lambda (&optional error dummy)
7967 (calendar-gregorian-from-absolute
7968 (get-text-property point 'day))))
7969 (call-interactively cmd))
7970 (fset 'calendar-cursor-to-date oldf))))))
7972 (defun org-agenda-execute-calendar-command (cmd)
7973 "Execute a calendar command from the agenda, with the date associated to
7974 the cursor position."
7975 (org-agenda-check-type t 'agenda 'timeline)
7976 (require 'diary-lib)
7977 (unless (get-text-property (point) 'day)
7978 (error "Don't know which date to use for calendar command"))
7979 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7980 (point (point))
7981 (date (calendar-gregorian-from-absolute
7982 (get-text-property point 'day)))
7983 ;; the following 2 vars are needed in the calendar
7984 (displayed-month (car date))
7985 (displayed-year (nth 2 date)))
7986 (unwind-protect
7987 (progn
7988 (fset 'calendar-cursor-to-date
7989 (lambda (&optional error dummy)
7990 (calendar-gregorian-from-absolute
7991 (get-text-property point 'day))))
7992 (call-interactively cmd))
7993 (fset 'calendar-cursor-to-date oldf))))
7995 (defun org-agenda-phases-of-moon ()
7996 "Display the phases of the moon for the 3 months around the cursor date."
7997 (interactive)
7998 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8000 (defun org-agenda-holidays ()
8001 "Display the holidays for the 3 months around the cursor date."
8002 (interactive)
8003 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8005 (defvar calendar-longitude)
8006 (defvar calendar-latitude)
8007 (defvar calendar-location-name)
8009 (defun org-agenda-sunrise-sunset (arg)
8010 "Display sunrise and sunset for the cursor date.
8011 Latitude and longitude can be specified with the variables
8012 `calendar-latitude' and `calendar-longitude'. When called with prefix
8013 argument, latitude and longitude will be prompted for."
8014 (interactive "P")
8015 (require 'solar)
8016 (let ((calendar-longitude (if arg nil calendar-longitude))
8017 (calendar-latitude (if arg nil calendar-latitude))
8018 (calendar-location-name
8019 (if arg "the given coordinates" calendar-location-name)))
8020 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8022 (defun org-agenda-goto-calendar ()
8023 "Open the Emacs calendar with the date at the cursor."
8024 (interactive)
8025 (org-agenda-check-type t 'agenda 'timeline)
8026 (let* ((day (or (get-text-property (point) 'day)
8027 (error "Don't know which date to open in calendar")))
8028 (date (calendar-gregorian-from-absolute day))
8029 (calendar-move-hook nil)
8030 (calendar-view-holidays-initially-flag nil)
8031 (calendar-view-diary-initially-flag nil))
8032 (calendar)
8033 (calendar-goto-date date)))
8035 ;;;###autoload
8036 (defun org-calendar-goto-agenda ()
8037 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8038 This is a command that has to be installed in `calendar-mode-map'."
8039 (interactive)
8040 (org-agenda-list nil (calendar-absolute-from-gregorian
8041 (calendar-cursor-to-date))
8042 nil))
8044 (defun org-agenda-convert-date ()
8045 (interactive)
8046 (org-agenda-check-type t 'agenda 'timeline)
8047 (let ((day (get-text-property (point) 'day))
8048 date s)
8049 (unless day
8050 (error "Don't know which date to convert"))
8051 (setq date (calendar-gregorian-from-absolute day))
8052 (setq s (concat
8053 "Gregorian: " (calendar-date-string date) "\n"
8054 "ISO: " (calendar-iso-date-string date) "\n"
8055 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8056 "Julian: " (calendar-julian-date-string date) "\n"
8057 "Astron. JD: " (calendar-astro-date-string date)
8058 " (Julian date number at noon UTC)\n"
8059 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8060 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8061 "French: " (calendar-french-date-string date) "\n"
8062 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8063 "Mayan: " (calendar-mayan-date-string date) "\n"
8064 "Coptic: " (calendar-coptic-date-string date) "\n"
8065 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8066 "Persian: " (calendar-persian-date-string date) "\n"
8067 "Chinese: " (calendar-chinese-date-string date) "\n"))
8068 (with-output-to-temp-buffer "*Dates*"
8069 (princ s))
8070 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8072 ;;; Bulk commands
8074 (defvar org-agenda-bulk-marked-entries nil
8075 "List of markers that refer to marked entries in the agenda.")
8077 (defun org-agenda-bulk-marked-p ()
8078 (eq (get-char-property (point-at-bol) 'type)
8079 'org-marked-entry-overlay))
8081 (defun org-agenda-bulk-mark (&optional arg)
8082 "Mark the entry at point for future bulk action."
8083 (interactive "p")
8084 (dotimes (i (max arg 1))
8085 (unless (org-get-at-bol 'org-agenda-diary-link)
8086 (let* ((m (org-get-at-bol 'org-hd-marker))
8088 (unless (org-agenda-bulk-marked-p)
8089 (unless m (error "Nothing to mark at point"))
8090 (push m org-agenda-bulk-marked-entries)
8091 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8092 (org-overlay-display ov "> "
8093 (org-get-todo-face "TODO")
8094 'evaporate)
8095 (overlay-put ov 'type 'org-marked-entry-overlay))
8096 (beginning-of-line 2)
8097 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8098 (beginning-of-line 2))
8099 (message "%d entries marked for bulk action"
8100 (length org-agenda-bulk-marked-entries))))))
8102 (defun org-agenda-bulk-mark-regexp (regexp)
8103 "Mark entries match REGEXP."
8104 (interactive "sMark entries matching regexp: ")
8105 (let (entries-marked)
8106 (save-excursion
8107 (goto-char (point-min))
8108 (goto-char (next-single-property-change (point) 'txt))
8109 (while (re-search-forward regexp nil t)
8110 (when (string-match regexp (get-text-property (point) 'txt))
8111 (setq entries-marked (+ entries-marked 1))
8112 (call-interactively 'org-agenda-bulk-mark))))
8113 (if (not entries-marked)
8114 (message "No entry matching this regexp."))))
8116 (defun org-agenda-bulk-unmark ()
8117 "Unmark the entry at point for future bulk action."
8118 (interactive)
8119 (when (org-agenda-bulk-marked-p)
8120 (org-agenda-bulk-remove-overlays
8121 (point-at-bol) (+ 2 (point-at-bol)))
8122 (setq org-agenda-bulk-marked-entries
8123 (delete (org-get-at-bol 'org-hd-marker)
8124 org-agenda-bulk-marked-entries)))
8125 (beginning-of-line 2)
8126 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8127 (beginning-of-line 2))
8128 (message "%d entries marked for bulk action"
8129 (length org-agenda-bulk-marked-entries)))
8131 (defun org-agenda-bulk-toggle ()
8132 "Toggle marking the entry at point for bulk action."
8133 (interactive)
8134 (if (org-agenda-bulk-marked-p)
8135 (org-agenda-bulk-unmark)
8136 (org-agenda-bulk-mark)))
8138 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8139 "Remove the mark overlays between BEG and END in the agenda buffer.
8140 BEG and END default to the buffer limits.
8142 This only removes the overlays, it does not remove the markers
8143 from the list in `org-agenda-bulk-marked-entries'."
8144 (interactive)
8145 (mapc (lambda (ov)
8146 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8147 (delete-overlay ov)))
8148 (overlays-in (or beg (point-min)) (or end (point-max)))))
8150 (defun org-agenda-bulk-remove-all-marks ()
8151 "Remove all marks in the agenda buffer.
8152 This will remove the markers, and the overlays."
8153 (interactive)
8154 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8155 (setq org-agenda-bulk-marked-entries nil)
8156 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8158 (defun org-agenda-bulk-action (&optional arg)
8159 "Execute an remote-editing action on all marked entries.
8160 The prefix arg is passed through to the command if possible."
8161 (interactive "P")
8162 ;; Make sure we have markers, and only valid ones
8163 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8164 (mapc
8165 (lambda (m)
8166 (unless (and (markerp m)
8167 (marker-buffer m)
8168 (buffer-live-p (marker-buffer m))
8169 (marker-position m))
8170 (error "Marker %s for bulk command is invalid" m)))
8171 org-agenda-bulk-marked-entries)
8173 ;; Prompt for the bulk command
8174 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8175 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8176 (when org-agenda-bulk-custom-functions
8177 (concat " Custom: ["
8178 (mapconcat (lambda(f) (char-to-string (car f)))
8179 org-agenda-bulk-custom-functions "")
8180 "]"))))
8181 (let* ((action (read-char-exclusive))
8182 (org-log-refile (if org-log-refile 'time nil))
8183 (entries (reverse org-agenda-bulk-marked-entries))
8184 redo-at-end
8185 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8186 (cond
8187 ((equal action ?$)
8188 (setq cmd '(org-agenda-archive)))
8190 ((equal action ?A)
8191 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8193 ((member action '(?r ?w))
8194 (setq rfloc (org-refile-get-location
8195 "Refile to"
8196 (marker-buffer (car org-agenda-bulk-marked-entries))
8197 org-refile-allow-creating-parent-nodes))
8198 (if (nth 3 rfloc)
8199 (setcar (nthcdr 3 rfloc)
8200 (move-marker (make-marker) (nth 3 rfloc)
8201 (or (get-file-buffer (nth 1 rfloc))
8202 (find-buffer-visiting (nth 1 rfloc))
8203 (error "This should not happen")))))
8205 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8206 redo-at-end t))
8208 ((equal action ?t)
8209 (setq state (org-icompleting-read
8210 "Todo state: "
8211 (with-current-buffer (marker-buffer (car entries))
8212 (mapcar 'list org-todo-keywords-1))))
8213 (setq cmd `(let ((org-inhibit-blocking t)
8214 (org-inhibit-logging 'note))
8215 (org-agenda-todo ,state))))
8217 ((memq action '(?- ?+))
8218 (setq tag (org-icompleting-read
8219 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8220 (with-current-buffer (marker-buffer (car entries))
8221 (delq nil
8222 (mapcar (lambda (x)
8223 (if (stringp (car x)) x)) org-tag-alist)))))
8224 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8226 ((memq action '(?s ?d))
8227 (let* ((date (unless arg
8228 (org-read-date
8229 nil nil nil
8230 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8231 (ans (if arg nil org-read-date-final-answer))
8232 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8233 (setq cmd `(let* ((bound (fboundp 'read-string))
8234 (old (and bound (symbol-function 'read-string))))
8235 (unwind-protect
8236 (progn
8237 (fset 'read-string (lambda (&rest ignore) ,ans))
8238 (eval '(,c1 arg)))
8239 (if bound
8240 (fset 'read-string old)
8241 (fmakunbound 'read-string)))))))
8243 ((equal action ?S)
8244 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8245 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8246 (let ((days (read-number
8247 (format "Scatter tasks across how many %sdays: "
8248 (if arg "week" "")) 7)))
8249 (setq cmd
8250 `(let ((distance (1+ (random ,days))))
8251 (if arg
8252 (let ((dist distance)
8253 (day-of-week
8254 (calendar-day-of-week
8255 (calendar-gregorian-from-absolute (org-today)))))
8256 (dotimes (i (1+ dist))
8257 (while (member day-of-week org-agenda-weekend-days)
8258 (incf distance)
8259 (incf day-of-week)
8260 (if (= day-of-week 7)
8261 (setq day-of-week 0)))
8262 (incf day-of-week)
8263 (if (= day-of-week 7)
8264 (setq day-of-week 0)))))
8265 ;; silently fail when try to replan a sexp entry
8266 (condition-case nil
8267 (let* ((date (calendar-gregorian-from-absolute
8268 (+ (org-today) distance)))
8269 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8270 (nth 2 date))))
8271 (org-agenda-schedule nil time))
8272 (error nil)))))))
8274 ((assoc action org-agenda-bulk-custom-functions)
8275 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8276 redo-at-end t))
8278 ((equal action ?f)
8279 (setq cmd (list (intern
8280 (org-icompleting-read "Function: "
8281 obarray 'fboundp t nil nil)))))
8283 (t (error "Invalid bulk action")))
8285 ;; Sort the markers, to make sure that parents are handled before children
8286 (setq entries (sort entries
8287 (lambda (a b)
8288 (cond
8289 ((equal (marker-buffer a) (marker-buffer b))
8290 (< (marker-position a) (marker-position b)))
8292 (string< (buffer-name (marker-buffer a))
8293 (buffer-name (marker-buffer b))))))))
8295 ;; Now loop over all markers and apply cmd
8296 (while (setq e (pop entries))
8297 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8298 (if (not pos)
8299 (progn (message "Skipping removed entry at %s" e)
8300 (setq cntskip (1+ cntskip)))
8301 (goto-char pos)
8302 (eval cmd)
8303 (setq org-agenda-bulk-marked-entries
8304 (delete e org-agenda-bulk-marked-entries))
8305 (setq cnt (1+ cnt))))
8306 (setq org-agenda-bulk-marked-entries nil)
8307 (org-agenda-bulk-remove-all-marks)
8308 (when redo-at-end (org-agenda-redo))
8309 (message "Acted on %d entries%s"
8311 (if (= cntskip 0)
8313 (format ", skipped %d (disappeared before their turn)"
8314 cntskip)))))
8316 ;;; Flagging notes
8318 (defun org-agenda-show-the-flagging-note ()
8319 "Display the flagging note in the other window.
8320 When called a second time in direct sequence, offer to remove the FLAGGING
8321 tag and (if present) the flagging note."
8322 (interactive)
8323 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8324 (win (selected-window))
8325 note heading newhead)
8326 (unless hdmarker
8327 (error "No linked entry at point"))
8328 (if (and (eq this-command last-command)
8329 (y-or-n-p "Unflag and remove any flagging note? "))
8330 (progn
8331 (org-agenda-remove-flag hdmarker)
8332 (let ((win (get-buffer-window "*Flagging Note*")))
8333 (and win (delete-window win)))
8334 (message "Entry unflaged"))
8335 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8336 (unless note
8337 (error "No flagging note"))
8338 (org-kill-new note)
8339 (org-switch-to-buffer-other-window "*Flagging Note*")
8340 (erase-buffer)
8341 (insert note)
8342 (goto-char (point-min))
8343 (while (re-search-forward "\\\\n" nil t)
8344 (replace-match "\n" t t))
8345 (goto-char (point-min))
8346 (select-window win)
8347 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8349 (defun org-agenda-remove-flag (marker)
8350 "Remove the FLAGGED tag and any flagging note in the entry."
8351 (let (newhead)
8352 (org-with-point-at marker
8353 (org-toggle-tag "FLAGGED" 'off)
8354 (org-entry-delete nil "THEFLAGGINGNOTE")
8355 (setq newhead (org-get-heading)))
8356 (org-agenda-change-all-lines newhead marker)
8357 (message "Entry unflaged")))
8359 (defun org-agenda-get-any-marker (&optional pos)
8360 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8361 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8363 ;;; Appointment reminders
8365 (defvar appt-time-msg-list)
8367 ;;;###autoload
8368 (defun org-agenda-to-appt (&optional refresh filter)
8369 "Activate appointments found in `org-agenda-files'.
8370 With a \\[universal-argument] prefix, refresh the list of
8371 appointments.
8373 If FILTER is t, interactively prompt the user for a regular
8374 expression, and filter out entries that don't match it.
8376 If FILTER is a string, use this string as a regular expression
8377 for filtering entries out.
8379 FILTER can also be an alist with the car of each cell being
8380 either 'headline or 'category. For example:
8382 '((headline \"IMPORTANT\")
8383 (category \"Work\"))
8385 will only add headlines containing IMPORTANT or headlines
8386 belonging to the \"Work\" category."
8387 (interactive "P")
8388 (if refresh (setq appt-time-msg-list nil))
8389 (if (eq filter t)
8390 (setq filter (read-from-minibuffer "Regexp filter: ")))
8391 (let* ((cnt 0) ; count added events
8392 (org-agenda-new-buffers nil)
8393 (org-deadline-warning-days 0)
8394 ;; Do not use `org-today' here because appt only takes
8395 ;; time and without date as argument, so it may pass wrong
8396 ;; information otherwise
8397 (today (org-date-to-gregorian
8398 (time-to-days (current-time))))
8399 (org-agenda-restrict nil)
8400 (files (org-agenda-files 'unrestricted)) entries file)
8401 ;; Get all entries which may contain an appt
8402 (org-prepare-agenda-buffers files)
8403 (while (setq file (pop files))
8404 (setq entries
8405 (append entries
8406 (org-agenda-get-day-entries
8407 file today :timestamp :scheduled :deadline))))
8408 (setq entries (delq nil entries))
8409 ;; Map thru entries and find if we should filter them out
8410 (mapc
8411 (lambda(x)
8412 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8413 (cat (get-text-property 1 'org-category x))
8414 (tod (get-text-property 1 'time-of-day x))
8415 (ok (or (null filter)
8416 (and (stringp filter) (string-match filter evt))
8417 (and (listp filter)
8418 (or (string-match
8419 (cadr (assoc 'category filter)) cat)
8420 (string-match
8421 (cadr (assoc 'headline filter)) evt))))))
8422 ;; FIXME: Shall we remove text-properties for the appt text?
8423 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8424 (when (and ok tod)
8425 (setq tod (concat "00" (number-to-string tod))
8426 tod (when (string-match
8427 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8428 (concat (match-string 1 tod) ":"
8429 (match-string 2 tod))))
8430 (appt-add tod evt)
8431 (setq cnt (1+ cnt))))) entries)
8432 (org-release-buffers org-agenda-new-buffers)
8433 (if (eq cnt 0)
8434 (message "No event to add")
8435 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8437 (defun org-agenda-todayp (date)
8438 "Does DATE mean today, when considering `org-extend-today-until'?"
8439 (let ((today (org-today))
8440 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8441 date)))
8442 (eq date today)))
8444 (provide 'org-agenda)
8448 ;;; org-agenda.el ends here