Release 6.33b
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-agenda.el
blob32196ccc4080db6716088321b53ab5bcb76a28a4
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.33b
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
44 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
45 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-french-date-string "cal-french" (&optional date))
47 (declare-function calendar-goto-date "cal-move" (date))
48 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
49 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
50 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
51 (declare-function calendar-iso-from-absolute "cal-iso" (date))
52 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
53 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
54 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
55 (declare-function org-datetree-find-date-create "org-datetree" (date))
56 (declare-function org-columns-quit "org-colview" ())
57 (declare-function diary-date-display-form "diary-lib" (&optional type))
58 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
59 (declare-function org-habit-insert-consistency-graphs
60 "org-habit" (&optional line))
61 (declare-function org-is-habit-p "org-habit" (&optional pom))
62 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
63 (declare-function org-habit-get-priority "org-habit" (habit))
64 (defvar calendar-mode-map)
65 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
66 (defvar org-habit-show-habits)
67 (defvar org-habit-show-habits-only-for-today)
69 ;; Defined somewhere in this file, but used before definition.
70 (defvar org-agenda-buffer-name)
71 (defvar org-agenda-overriding-header)
72 (defvar org-agenda-title-append nil)
73 (defvar entry)
74 (defvar date)
75 (defvar org-agenda-undo-list)
76 (defvar org-agenda-pending-undo-list)
77 (defvar original-date) ; dynamically scoped, calendar.el does scope this
79 (defcustom org-agenda-confirm-kill 1
80 "When set, remote killing from the agenda buffer needs confirmation.
81 When t, a confirmation is always needed. When a number N, confirmation is
82 only needed when the text to be killed contains more than N non-white lines."
83 :group 'org-agenda
84 :type '(choice
85 (const :tag "Never" nil)
86 (const :tag "Always" t)
87 (integer :tag "When more than N lines")))
89 (defcustom org-agenda-compact-blocks nil
90 "Non-nil means, make the block agenda more compact.
91 This is done by leaving out unnecessary lines."
92 :group 'org-agenda
93 :type 'boolean)
95 (defcustom org-agenda-block-separator ?=
96 "The separator between blocks in the agenda.
97 If this is a string, it will be used as the separator, with a newline added.
98 If it is a character, it will be repeated to fill the window width."
99 :group 'org-agenda
100 :type '(choice
101 (character)
102 (string)))
104 (defgroup org-agenda-export nil
105 "Options concerning exporting agenda views in Org-mode."
106 :tag "Org Agenda Export"
107 :group 'org-agenda)
109 (defcustom org-agenda-with-colors t
110 "Non-nil means, use colors in agenda views."
111 :group 'org-agenda-export
112 :type 'boolean)
114 (defcustom org-agenda-exporter-settings nil
115 "Alist of variable/value pairs that should be active during agenda export.
116 This is a good place to set options for ps-print and for htmlize.
117 Note that the way this is implemented, the values will be evaluated
118 before assigned to the variables. So make sure to quote values you do
119 *not* want evaluated, for example
121 (setq org-agenda-exporter-settings
122 '((ps-print-color-p 'black-white)))"
123 :group 'org-agenda-export
124 :type '(repeat
125 (list
126 (variable)
127 (sexp :tag "Value"))))
129 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
130 "Hook run in temporary buffer before writing it to an export file.
131 A useful function is `org-agenda-add-entry-text'."
132 :group 'org-agenda-export
133 :type 'hook
134 :options '(org-agenda-add-entry-text))
136 (defcustom org-agenda-add-entry-text-maxlines 0
137 "Maximum number of entry text lines to be added to agenda.
138 This is only relevant when `org-agenda-add-entry-text' is part of
139 `org-agenda-before-write-hook', which it is by default.
140 When this is 0, nothing will happen. When it is greater than 0, it
141 specifies the maximum number of lines that will be added for each entry
142 that is listed in the agenda view.
144 Note that this variable is not used during display, only when exporting
145 the agenda. For agenda display, see org-agenda-entry-text-mode and the
146 variable `org-agenda-entry-text-maxlines'."
147 :group 'org-agenda
148 :type 'integer)
150 (defcustom org-agenda-add-entry-text-descriptive-links t
151 "Non-nil means, export org-links as descriptive links in agenda added text.
152 This variable applies to the text added to the agenda when
153 `org-agenda-add-entry-text-maxlines' is larger than 0.
154 When this variable nil, the URL will (also) be shown."
155 :group 'org-agenda
156 :type 'boolean)
158 (defcustom org-agenda-export-html-style ""
159 "The style specification for exported HTML Agenda files.
160 If this variable contains a string, it will replace the default <style>
161 section as produced by `htmlize'.
162 Since there are different ways of setting style information, this variable
163 needs to contain the full HTML structure to provide a style, including the
164 surrounding HTML tags. The style specifications should include definitions
165 the fonts used by the agenda, here is an example:
167 <style type=\"text/css\">
168 p { font-weight: normal; color: gray; }
169 .org-agenda-structure {
170 font-size: 110%;
171 color: #003399;
172 font-weight: 600;
174 .org-todo {
175 color: #cc6666;
176 font-weight: bold;
178 .org-agenda-done {
179 color: #339933;
181 .org-done {
182 color: #339933;
184 .title { text-align: center; }
185 .todo, .deadline { color: red; }
186 .done { color: green; }
187 </style>
189 or, if you want to keep the style in a file,
191 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
193 As the value of this option simply gets inserted into the HTML <head> header,
194 you can \"misuse\" it to also add other text to the header. However,
195 <style>...</style> is required, if not present the variable will be ignored."
196 :group 'org-agenda-export
197 :group 'org-export-html
198 :type 'string)
200 (defgroup org-agenda-custom-commands nil
201 "Options concerning agenda views in Org-mode."
202 :tag "Org Agenda Custom Commands"
203 :group 'org-agenda)
205 (defconst org-sorting-choice
206 '(choice
207 (const time-up) (const time-down)
208 (const category-keep) (const category-up) (const category-down)
209 (const tag-down) (const tag-up)
210 (const priority-up) (const priority-down)
211 (const todo-state-up) (const todo-state-down)
212 (const effort-up) (const effort-down)
213 (const habit-up) (const habit-down)
214 (const user-defined-up) (const user-defined-down))
215 "Sorting choices.")
217 (defconst org-agenda-custom-commands-local-options
218 `(repeat :tag "Local settings for this command. Remember to quote values"
219 (choice :tag "Setting"
220 (list :tag "Heading for this block"
221 (const org-agenda-overriding-header)
222 (string :tag "Headline"))
223 (list :tag "Files to be searched"
224 (const org-agenda-files)
225 (list
226 (const :format "" quote)
227 (repeat (file))))
228 (list :tag "Sorting strategy"
229 (const org-agenda-sorting-strategy)
230 (list
231 (const :format "" quote)
232 (repeat
233 ,org-sorting-choice)))
234 (list :tag "Prefix format"
235 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
236 (string))
237 (list :tag "Number of days in agenda"
238 (const org-agenda-ndays)
239 (integer :value 1))
240 (list :tag "Fixed starting date"
241 (const org-agenda-start-day)
242 (string :value "2007-11-01"))
243 (list :tag "Start on day of week"
244 (const org-agenda-start-on-weekday)
245 (choice :value 1
246 (const :tag "Today" nil)
247 (integer :tag "Weekday No.")))
248 (list :tag "Include data from diary"
249 (const org-agenda-include-diary)
250 (boolean))
251 (list :tag "Deadline Warning days"
252 (const org-deadline-warning-days)
253 (integer :value 1))
254 (list :tag "Tags filter preset"
255 (const org-agenda-filter-preset)
256 (list
257 (const :format "" quote)
258 (repeat
259 (string :tag "+tag or -tag"))))
260 (list :tag "Standard skipping condition"
261 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
262 (const org-agenda-skip-function)
263 (list
264 (const :format "" quote)
265 (list
266 (choice
267 :tag "Skipping range"
268 (const :tag "Skip entry" org-agenda-skip-entry-if)
269 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
270 (repeat :inline t :tag "Conditions for skipping"
271 (choice
272 :tag "Condition type"
273 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
274 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
275 (const :tag "scheduled" 'scheduled)
276 (const :tag "not scheduled" 'notscheduled)
277 (const :tag "deadline" 'deadline)
278 (const :tag "no deadline" 'notdeadline)
279 (const :tag "timestamp" 'timestamp)
280 (const :tag "no timestamp" 'nottimestamp))))))
281 (list :tag "Non-standard skipping condition"
282 :value (org-agenda-skip-function)
283 (const org-agenda-skip-function)
284 (sexp :tag "Function or form (quoted!)"))
285 (list :tag "Any variable"
286 (variable :tag "Variable")
287 (sexp :tag "Value (sexp)"))))
288 "Selection of examples for agenda command settings.
289 This will be spliced into the custom type of
290 `org-agenda-custom-commands'.")
293 (defcustom org-agenda-custom-commands nil
294 "Custom commands for the agenda.
295 These commands will be offered on the splash screen displayed by the
296 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
298 (key desc type match settings files)
300 key The key (one or more characters as a string) to be associated
301 with the command.
302 desc A description of the command, when omitted or nil, a default
303 description is built using MATCH.
304 type The command type, any of the following symbols:
305 agenda The daily/weekly agenda.
306 todo Entries with a specific TODO keyword, in all agenda files.
307 search Entries containing search words entry or headline.
308 tags Tags/Property/TODO match in all agenda files.
309 tags-todo Tags/P/T match in all agenda files, TODO entries only.
310 todo-tree Sparse tree of specific TODO keyword in *current* file.
311 tags-tree Sparse tree with all tags matches in *current* file.
312 occur-tree Occur sparse tree for *current* file.
313 ... A user-defined function.
314 match What to search for:
315 - a single keyword for TODO keyword searches
316 - a tags match expression for tags searches
317 - a word search expression for text searches.
318 - a regular expression for occur searches
319 For all other commands, this should be the empty string.
320 settings A list of option settings, similar to that in a let form, so like
321 this: ((opt1 val1) (opt2 val2) ...). The values will be
322 evaluated at the moment of execution, so quote them when needed.
323 files A list of files file to write the produced agenda buffer to
324 with the command `org-store-agenda-views'.
325 If a file name ends in \".html\", an HTML version of the buffer
326 is written out. If it ends in \".ps\", a postscript version is
327 produced. Otherwise, only the plain text is written to the file.
329 You can also define a set of commands, to create a composite agenda buffer.
330 In this case, an entry looks like this:
332 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
334 where
336 desc A description string to be displayed in the dispatcher menu.
337 cmd An agenda command, similar to the above. However, tree commands
338 are no allowed, but instead you can get agenda and global todo list.
339 So valid commands for a set are:
340 (agenda \"\" settings)
341 (alltodo \"\" settings)
342 (stuck \"\" settings)
343 (todo \"match\" settings files)
344 (search \"match\" settings files)
345 (tags \"match\" settings files)
346 (tags-todo \"match\" settings files)
348 Each command can carry a list of options, and another set of options can be
349 given for the whole set of commands. Individual command options take
350 precedence over the general options.
352 When using several characters as key to a command, the first characters
353 are prefix commands. For the dispatcher to display useful information, you
354 should provide a description for the prefix, like
356 (setq org-agenda-custom-commands
357 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
358 (\"hl\" tags \"+HOME+Lisa\")
359 (\"hp\" tags \"+HOME+Peter\")
360 (\"hk\" tags \"+HOME+Kim\")))"
361 :group 'org-agenda-custom-commands
362 :type `(repeat
363 (choice :value ("x" "Describe command here" tags "" nil)
364 (list :tag "Single command"
365 (string :tag "Access Key(s) ")
366 (option (string :tag "Description"))
367 (choice
368 (const :tag "Agenda" agenda)
369 (const :tag "TODO list" alltodo)
370 (const :tag "Search words" search)
371 (const :tag "Stuck projects" stuck)
372 (const :tag "Tags/Property match (all agenda files)" tags)
373 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
374 (const :tag "TODO keyword search (all agenda files)" todo)
375 (const :tag "Tags sparse tree (current buffer)" tags-tree)
376 (const :tag "TODO keyword tree (current buffer)" todo-tree)
377 (const :tag "Occur tree (current buffer)" occur-tree)
378 (sexp :tag "Other, user-defined function"))
379 (string :tag "Match (only for some commands)")
380 ,org-agenda-custom-commands-local-options
381 (option (repeat :tag "Export" (file :tag "Export to"))))
382 (list :tag "Command series, all agenda files"
383 (string :tag "Access Key(s)")
384 (string :tag "Description ")
385 (repeat :tag "Component"
386 (choice
387 (list :tag "Agenda"
388 (const :format "" agenda)
389 (const :tag "" :format "" "")
390 ,org-agenda-custom-commands-local-options)
391 (list :tag "TODO list (all keywords)"
392 (const :format "" alltodo)
393 (const :tag "" :format "" "")
394 ,org-agenda-custom-commands-local-options)
395 (list :tag "Search words"
396 (const :format "" search)
397 (string :tag "Match")
398 ,org-agenda-custom-commands-local-options)
399 (list :tag "Stuck projects"
400 (const :format "" stuck)
401 (const :tag "" :format "" "")
402 ,org-agenda-custom-commands-local-options)
403 (list :tag "Tags search"
404 (const :format "" tags)
405 (string :tag "Match")
406 ,org-agenda-custom-commands-local-options)
407 (list :tag "Tags search, TODO entries only"
408 (const :format "" tags-todo)
409 (string :tag "Match")
410 ,org-agenda-custom-commands-local-options)
411 (list :tag "TODO keyword search"
412 (const :format "" todo)
413 (string :tag "Match")
414 ,org-agenda-custom-commands-local-options)
415 (list :tag "Other, user-defined function"
416 (symbol :tag "function")
417 (string :tag "Match")
418 ,org-agenda-custom-commands-local-options)))
420 (repeat :tag "Settings for entire command set"
421 (list (variable :tag "Any variable")
422 (sexp :tag "Value")))
423 (option (repeat :tag "Export" (file :tag "Export to"))))
424 (cons :tag "Prefix key documentation"
425 (string :tag "Access Key(s)")
426 (string :tag "Description ")))))
428 (defcustom org-agenda-query-register ?o
429 "The register holding the current query string.
430 The purpose of this is that if you construct a query string interactively,
431 you can then use it to define a custom command."
432 :group 'org-agenda-custom-commands
433 :type 'character)
435 (defcustom org-stuck-projects
436 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
437 "How to identify stuck projects.
438 This is a list of four items:
439 1. A tags/todo/property matcher string that is used to identify a project.
440 See the manual for a description of tag and property searches.
441 The entire tree below a headline matched by this is considered one project.
442 2. A list of TODO keywords identifying non-stuck projects.
443 If the project subtree contains any headline with one of these todo
444 keywords, the project is considered to be not stuck. If you specify
445 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
446 3. A list of tags identifying non-stuck projects.
447 If the project subtree contains any headline with one of these tags,
448 the project is considered to be not stuck. If you specify \"*\" as
449 a tag, any tag will mark the project unstuck. Note that this is about
450 the explicit presence of a tag somewhere in the subtree, inherited
451 tags to not count here. If inherited tags make a project not stuck,
452 use \"-TAG\" in the tags part of the matcher under (1.) above.
453 4. An arbitrary regular expression matching non-stuck projects.
455 If the project turns out to be not stuck, search continues also in the
456 subtree to see if any of the subtasks have project status.
458 See also the variable `org-tags-match-list-sublevels' which applies
459 to projects matched by this search as well.
461 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
462 or `C-c a #' to produce the list."
463 :group 'org-agenda-custom-commands
464 :type '(list
465 (string :tag "Tags/TODO match to identify a project")
466 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
467 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
468 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
470 (defcustom org-agenda-filter-effort-default-operator "<"
471 "The default operator for effort estimate filtering.
472 If you select an effort estimate limit without first pressing an operator,
473 this one will be used."
474 :group 'org-agenda-custom-commands
475 :type '(choice (const :tag "less or equal" "<")
476 (const :tag "greater or equal"">")
477 (const :tag "equal" "=")))
479 (defgroup org-agenda-skip nil
480 "Options concerning skipping parts of agenda files."
481 :tag "Org Agenda Skip"
482 :group 'org-agenda)
483 (defgroup org-agenda-daily/weekly nil
484 "Options concerning the daily/weekly agenda."
485 :tag "Org Agenda Daily/Weekly"
486 :group 'org-agenda)
487 (defgroup org-agenda-todo-list nil
488 "Options concerning the global todo list agenda view."
489 :tag "Org Agenda Todo List"
490 :group 'org-agenda)
491 (defgroup org-agenda-match-view nil
492 "Options concerning the general tags/property/todo match agenda view."
493 :tag "Org Agenda Match View"
494 :group 'org-agenda)
495 (defgroup org-agenda-search-view nil
496 "Options concerning the general tags/property/todo match agenda view."
497 :tag "Org Agenda Match View"
498 :group 'org-agenda)
500 (defvar org-agenda-archives-mode nil
501 "Non-nil means, the agenda will include archived items.
502 If this is the symbol `trees', trees in the selected agenda scope
503 that are marked with the ARCHIVE tag will be included anyway. When this is
504 t, also all archive files associated with the current selection of agenda
505 files will be included.")
507 (defcustom org-agenda-skip-comment-trees t
508 "Non-nil means, skip trees that start with the COMMENT keyword.
509 When nil, these trees are also scanned by agenda commands."
510 :group 'org-agenda-skip
511 :type 'boolean)
513 (defcustom org-agenda-todo-list-sublevels t
514 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
515 When nil, the sublevels of a TODO entry are not checked, resulting in
516 potentially much shorter TODO lists."
517 :group 'org-agenda-skip
518 :group 'org-agenda-todo-list
519 :type 'boolean)
521 (defcustom org-agenda-todo-ignore-with-date nil
522 "Non-nil means, don't show entries with a date in the global todo list.
523 You can use this if you prefer to mark mere appointments with a TODO keyword,
524 but don't want them to show up in the TODO list.
525 When this is set, it also covers deadlines and scheduled items, the settings
526 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
527 will be ignored.
528 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
529 :group 'org-agenda-skip
530 :group 'org-agenda-todo-list
531 :type 'boolean)
533 (defcustom org-agenda-todo-ignore-scheduled nil
534 "Non-nil means, don't show scheduled entries in the global todo list.
535 The idea behind this is that by scheduling it, you have already taken care
536 of this item.
537 See also `org-agenda-todo-ignore-with-date'.
538 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
539 :group 'org-agenda-skip
540 :group 'org-agenda-todo-list
541 :type 'boolean)
543 (defcustom org-agenda-todo-ignore-deadlines nil
544 "Non-nil means, don't show near deadline entries in the global todo list.
545 Near means closer than `org-deadline-warning-days' days.
546 The idea behind this is that such items will appear in the agenda anyway.
547 See also `org-agenda-todo-ignore-with-date'.
548 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
549 :group 'org-agenda-skip
550 :group 'org-agenda-todo-list
551 :type 'boolean)
553 (defcustom org-agenda-tags-todo-honor-ignore-options nil
554 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
555 The variables
556 `org-agenda-todo-ignore-with-date',
557 `org-agenda-todo-ignore-scheduled'
558 `org-agenda-todo-ignore-deadlines'
559 make the global TODO list skip entries that have time stamps of certain
560 kinds. If this option is set, the same options will also apply for the
561 tags-todo search, which is the general tags/property matcher
562 restricted to unfinished TODO entries only."
563 :group 'org-agenda-skip
564 :group 'org-agenda-todo-list
565 :group 'org-agenda-match-view
566 :type 'boolean)
568 (defcustom org-agenda-skip-scheduled-if-done nil
569 "Non-nil means don't show scheduled items in agenda when they are done.
570 This is relevant for the daily/weekly agenda, not for the TODO list. And
571 it applies only to the actual date of the scheduling. Warnings about
572 an item with a past scheduling dates are always turned off when the item
573 is DONE."
574 :group 'org-agenda-skip
575 :group 'org-agenda-daily/weekly
576 :type 'boolean)
578 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
579 "Non-nil means skip scheduling line if same entry shows because of deadline.
580 In the agenda of today, an entry can show up multiple times because
581 it is both scheduled and has a nearby deadline, and maybe a plain time
582 stamp as well.
583 When this variable is t, then only the deadline is shown and the fact that
584 the entry is scheduled today or was scheduled previously is not shown.
585 When this variable is nil, the entry will be shown several times. When
586 the variable is the symbol `not-today', then skip scheduled previously,
587 but not scheduled today."
588 :group 'org-agenda-skip
589 :group 'org-agenda-daily/weekly
590 :type '(choice
591 (const :tag "Never" nil)
592 (const :tag "Always" t)
593 (const :tag "Not when scheduled today" not-today)))
595 (defcustom org-agenda-skip-deadline-if-done nil
596 "Non-nil means don't show deadlines when the corresponding item is done.
597 When nil, the deadline is still shown and should give you a happy feeling.
598 This is relevant for the daily/weekly agenda. And it applied only to the
599 actually date of the deadline. Warnings about approaching and past-due
600 deadlines are always turned off when the item is DONE."
601 :group 'org-agenda-skip
602 :group 'org-agenda-daily/weekly
603 :type 'boolean)
605 (defcustom org-agenda-skip-additional-timestamps-same-entry t
606 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
607 When non-nil, after the search for timestamps has matched once in an
608 entry, the rest of the entry will not be searched."
609 :group 'org-agenda-skip
610 :type 'boolean)
612 (defcustom org-agenda-skip-timestamp-if-done nil
613 "Non-nil means don't select item by timestamp or -range if it is DONE."
614 :group 'org-agenda-skip
615 :group 'org-agenda-daily/weekly
616 :type 'boolean)
618 (defcustom org-agenda-dim-blocked-tasks t
619 "Non-nil means, dim blocked tasks in the agenda display.
620 This causes some overhead during agenda construction, but if you
621 have turned on `org-enforce-todo-dependencies',
622 `org-enforce-todo-checkbox-dependencies', or any other blocking
623 mechanism, this will create useful feedback in the agenda.
625 Instead of t, this variable can also have the value `invisible'.
626 Then blocked tasks will be invisible and only become visible when
627 they become unblocked. An exemption to this behavior is when a task is
628 blocked because of unchecked checkboxes below it. Since checkboxes do
629 not show up in the agenda views, making this task invisible you remove any
630 trace from agenda views that there is something to do. Therefore, a task
631 that is blocked because of checkboxes will never be made invisible, it
632 will only be dimmed."
633 :group 'org-agenda-daily/weekly
634 :group 'org-agenda-todo-list
635 :type '(choice
636 (const :tag "Do not dim" nil)
637 (const :tag "Dim to a grey face" t)
638 (const :tag "Make invisible" invisible)))
640 (defcustom org-timeline-show-empty-dates 3
641 "Non-nil means, `org-timeline' also shows dates without an entry.
642 When nil, only the days which actually have entries are shown.
643 When t, all days between the first and the last date are shown.
644 When an integer, show also empty dates, but if there is a gap of more than
645 N days, just insert a special line indicating the size of the gap."
646 :group 'org-agenda-skip
647 :type '(choice
648 (const :tag "None" nil)
649 (const :tag "All" t)
650 (integer :tag "at most")))
652 (defgroup org-agenda-startup nil
653 "Options concerning initial settings in the Agenda in Org Mode."
654 :tag "Org Agenda Startup"
655 :group 'org-agenda)
657 (defcustom org-finalize-agenda-hook nil
658 "Hook run just before displaying an agenda buffer."
659 :group 'org-agenda-startup
660 :type 'hook)
662 (defcustom org-agenda-mouse-1-follows-link nil
663 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
664 A longer mouse click will still set point. Does not work on XEmacs.
665 Needs to be set before org.el is loaded."
666 :group 'org-agenda-startup
667 :type 'boolean)
669 (defcustom org-agenda-start-with-follow-mode nil
670 "The initial value of follow-mode in a newly created agenda window."
671 :group 'org-agenda-startup
672 :type 'boolean)
674 (defcustom org-agenda-show-outline-path t
675 "Non-il means, show outline path in echo area after line motion."
676 :group 'org-agenda-startup
677 :type 'boolean)
679 (defcustom org-agenda-start-with-entry-text-mode nil
680 "The initial value of entry-text-mode in a newly created agenda window."
681 :group 'org-agenda-startup
682 :type 'boolean)
684 (defcustom org-agenda-entry-text-maxlines 5
685 "Number of text lines to be added when `E' is pressed in the agenda.
687 Note that this variable only used during agenda display. Add add entry text
688 when exporting the agenda, configure the variable
689 `org-agenda-add-entry-ext-maxlines'."
690 :group 'org-agenda
691 :type 'integer)
693 (defcustom org-agenda-entry-text-exclude-regexps nil
694 "List of regular expressions to clean up entry text.
695 The complete matches of all regular expressions in this list will be
696 removed from entry text before it is shown in the agenda."
697 :group 'org-agenda
698 :type '(repeat (regexp)))
700 (defvar org-agenda-entry-text-cleanup-hook nil
701 "Hook that is run after basic cleanup of entry text to be shown in agenda.
702 This cleanup is done in a temporary buffer, so the function may inspect and
703 change the entire buffer.
704 Some default stuff like drawers and scheduling/deadline dates will already
705 have been removed when this is called, as will any matches for regular
706 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
708 (defvar org-agenda-include-inactive-timestamps nil
709 "Non-nil means, include inactive time stamps in agenda and timeline.")
711 (defgroup org-agenda-windows nil
712 "Options concerning the windows used by the Agenda in Org Mode."
713 :tag "Org Agenda Windows"
714 :group 'org-agenda)
716 (defcustom org-agenda-window-setup 'reorganize-frame
717 "How the agenda buffer should be displayed.
718 Possible values for this option are:
720 current-window Show agenda in the current window, keeping all other windows.
721 other-window Use `switch-to-buffer-other-window' to display agenda.
722 reorganize-frame Show only two windows on the current frame, the current
723 window and the agenda.
724 other-frame Use `switch-to-buffer-other-frame' to display agenda.
725 Also, when exiting the agenda, kill that frame.
726 See also the variable `org-agenda-restore-windows-after-quit'."
727 :group 'org-agenda-windows
728 :type '(choice
729 (const current-window)
730 (const other-frame)
731 (const other-window)
732 (const reorganize-frame)))
734 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
735 "The min and max height of the agenda window as a fraction of frame height.
736 The value of the variable is a cons cell with two numbers between 0 and 1.
737 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
738 :group 'org-agenda-windows
739 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
741 (defcustom org-agenda-restore-windows-after-quit nil
742 "Non-nil means, restore window configuration open exiting agenda.
743 Before the window configuration is changed for displaying the agenda,
744 the current status is recorded. When the agenda is exited with
745 `q' or `x' and this option is set, the old state is restored. If
746 `org-agenda-window-setup' is `other-frame', the value of this
747 option will be ignored."
748 :group 'org-agenda-windows
749 :type 'boolean)
751 (defcustom org-agenda-ndays 7
752 "Number of days to include in overview display.
753 Should be 1 or 7.
754 Custom commands can set this variable in the options section."
755 :group 'org-agenda-daily/weekly
756 :type 'integer)
758 (defcustom org-agenda-start-on-weekday 1
759 "Non-nil means, start the overview always on the specified weekday.
760 0 denotes Sunday, 1 denotes Monday etc.
761 When nil, always start on the current day.
762 Custom commands can set this variable in the options section."
763 :group 'org-agenda-daily/weekly
764 :type '(choice (const :tag "Today" nil)
765 (integer :tag "Weekday No.")))
767 (defcustom org-agenda-show-all-dates t
768 "Non-nil means, `org-agenda' shows every day in the selected range.
769 When nil, only the days which actually have entries are shown."
770 :group 'org-agenda-daily/weekly
771 :type 'boolean)
773 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
774 "Format string for displaying dates in the agenda.
775 Used by the daily/weekly agenda and by the timeline. This should be
776 a format string understood by `format-time-string', or a function returning
777 the formatted date as a string. The function must take a single argument,
778 a calendar-style date list like (month day year)."
779 :group 'org-agenda-daily/weekly
780 :type '(choice
781 (string :tag "Format string")
782 (function :tag "Function")))
784 (defun org-agenda-format-date-aligned (date)
785 "Format a date string for display in the daily/weekly agenda, or timeline.
786 This function makes sure that dates are aligned for easy reading."
787 (require 'cal-iso)
788 (let* ((dayname (calendar-day-name date))
789 (day (cadr date))
790 (day-of-week (calendar-day-of-week date))
791 (month (car date))
792 (monthname (calendar-month-name month))
793 (year (nth 2 date))
794 (iso-week (org-days-to-iso-week
795 (calendar-absolute-from-gregorian date)))
796 (weekyear (cond ((and (= month 1) (>= iso-week 52))
797 (1- year))
798 ((and (= month 12) (<= iso-week 1))
799 (1+ year))
800 (t year)))
801 (weekstring (if (= day-of-week 1)
802 (format " W%02d" iso-week)
803 "")))
804 (format "%-10s %2d %s %4d%s"
805 dayname day monthname year weekstring)))
807 (defcustom org-agenda-weekend-days '(6 0)
808 "Which days are weekend?
809 These days get the special face `org-agenda-date-weekend' in the agenda
810 and timeline buffers."
811 :group 'org-agenda-daily/weekly
812 :type '(set :greedy t
813 (const :tag "Monday" 1)
814 (const :tag "Tuesday" 2)
815 (const :tag "Wednesday" 3)
816 (const :tag "Thursday" 4)
817 (const :tag "Friday" 5)
818 (const :tag "Saturday" 6)
819 (const :tag "Sunday" 0)))
821 (defcustom org-agenda-include-diary nil
822 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
823 Custom commands can set this variable in the options section."
824 :group 'org-agenda-daily/weekly
825 :type 'boolean)
827 (defcustom org-agenda-include-all-todo nil
828 "Set means weekly/daily agenda will always contain all TODO entries.
829 The TODO entries will be listed at the top of the agenda, before
830 the entries for specific days.
831 This option is deprecated, it is better to define a block agenda instead."
832 :group 'org-agenda-daily/weekly
833 :type 'boolean)
835 (defcustom org-agenda-repeating-timestamp-show-all t
836 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
837 When nil, only one occurrence is shown, either today or the
838 nearest into the future."
839 :group 'org-agenda-daily/weekly
840 :type 'boolean)
842 (defcustom org-scheduled-past-days 10000
843 "No. of days to continue listing scheduled items that are not marked DONE.
844 When an item is scheduled on a date, it shows up in the agenda on this
845 day and will be listed until it is marked done for the number of days
846 given here."
847 :group 'org-agenda-daily/weekly
848 :type 'integer)
850 (defcustom org-agenda-log-mode-items '(closed clock)
851 "List of items that should be shown in agenda log mode.
852 This list may contain the following symbols:
854 closed Show entries that have been closed on that day.
855 clock Show entries that have received clocked time on that day.
856 state Show all logged state changes.
857 Note that instead of changing this variable, you can also press `C-u l' in
858 the agenda to display all available LOG items temporarily."
859 :group 'org-agenda-daily/weekly
860 :type '(set :greedy t (const closed) (const clock) (const state)))
862 (defcustom org-agenda-log-mode-add-notes t
863 "Non-nil means, add first line of notes to log entries in agenda views.
864 If a log item like a state change or a clock entry is associated with
865 notes, the first line of these notes will be added to the entry in the
866 agenda display."
867 :group 'org-agenda-daily/weekly
868 :type 'boolean)
870 (defcustom org-agenda-start-with-log-mode nil
871 "The initial value of log-mode in a newly created agenda window."
872 :group 'org-agenda-startup
873 :group 'org-agenda-daily/weekly
874 :type 'boolean)
876 (defcustom org-agenda-start-with-clockreport-mode nil
877 "The initial value of clockreport-mode in a newly created agenda window."
878 :group 'org-agenda-startup
879 :group 'org-agenda-daily/weekly
880 :type 'boolean)
882 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
883 "Property list with parameters for the clocktable in clockreport mode.
884 This is the display mode that shows a clock table in the daily/weekly
885 agenda, the properties for this dynamic block can be set here.
886 The usual clocktable parameters are allowed here, but you cannot set
887 the properties :name, :tstart, :tend, :block, and :scope - these will
888 be overwritten to make sure the content accurately reflects the
889 current display in the agenda."
890 :group 'org-agenda-daily/weekly
891 :type 'plist)
893 (defcustom org-agenda-search-view-search-words-only nil
894 "Non-nil means, the search string is interpreted as individual words
895 The search then looks for each word separately in each entry and
896 selects entries that have matches for all words.
897 When nil, matching as loose words will only take place if the first
898 word is preceded by + or -. If that is not the case, the search
899 string will just be matched as a substring in the entry, but with
900 each space character allowing for any whitespace, including newlines."
901 :group 'org-agenda-search-view
902 :type 'boolean)
904 (defgroup org-agenda-time-grid nil
905 "Options concerning the time grid in the Org-mode Agenda."
906 :tag "Org Agenda Time Grid"
907 :group 'org-agenda)
909 (defcustom org-agenda-search-headline-for-time t
910 "Non-nil means, search headline for a time-of-day.
911 If the headline contains a time-of-day in one format or another, it will
912 be used to sort the entry into the time sequence of items for a day.
913 Some people have time stamps in the headline that refer to the creation
914 time or so, and then this produces an unwanted side effect. If this is
915 the case for your, use this variable to turn off searching the headline
916 for a time."
917 :group 'org-agenda-time-grid
918 :type 'boolean)
920 (defcustom org-agenda-use-time-grid t
921 "Non-nil means, show a time grid in the agenda schedule.
922 A time grid is a set of lines for specific times (like every two hours between
923 8:00 and 20:00). The items scheduled for a day at specific times are
924 sorted in between these lines.
925 For details about when the grid will be shown, and what it will look like, see
926 the variable `org-agenda-time-grid'."
927 :group 'org-agenda-time-grid
928 :type 'boolean)
930 (defcustom org-agenda-time-grid
931 '((daily today require-timed)
932 "----------------"
933 (800 1000 1200 1400 1600 1800 2000))
935 "The settings for time grid for agenda display.
936 This is a list of three items. The first item is again a list. It contains
937 symbols specifying conditions when the grid should be displayed:
939 daily if the agenda shows a single day
940 weekly if the agenda shows an entire week
941 today show grid on current date, independent of daily/weekly display
942 require-timed show grid only if at least one item has a time specification
944 The second item is a string which will be placed behind the grid time.
946 The third item is a list of integers, indicating the times that should have
947 a grid line."
948 :group 'org-agenda-time-grid
949 :type
950 '(list
951 (set :greedy t :tag "Grid Display Options"
952 (const :tag "Show grid in single day agenda display" daily)
953 (const :tag "Show grid in weekly agenda display" weekly)
954 (const :tag "Always show grid for today" today)
955 (const :tag "Show grid only if any timed entries are present"
956 require-timed)
957 (const :tag "Skip grid times already present in an entry"
958 remove-match))
959 (string :tag "Grid String")
960 (repeat :tag "Grid Times" (integer :tag "Time"))))
962 (defgroup org-agenda-sorting nil
963 "Options concerning sorting in the Org-mode Agenda."
964 :tag "Org Agenda Sorting"
965 :group 'org-agenda)
967 (defcustom org-agenda-sorting-strategy
968 '((agenda habit-down time-up priority-down category-keep)
969 (todo priority-down category-keep)
970 (tags priority-down category-keep)
971 (search category-keep))
972 "Sorting structure for the agenda items of a single day.
973 This is a list of symbols which will be used in sequence to determine
974 if an entry should be listed before another entry. The following
975 symbols are recognized:
977 time-up Put entries with time-of-day indications first, early first
978 time-down Put entries with time-of-day indications first, late first
979 category-keep Keep the default order of categories, corresponding to the
980 sequence in `org-agenda-files'.
981 category-up Sort alphabetically by category, A-Z.
982 category-down Sort alphabetically by category, Z-A.
983 tag-up Sort alphabetically by last tag, A-Z.
984 tag-down Sort alphabetically by last tag, Z-A.
985 priority-up Sort numerically by priority, high priority last.
986 priority-down Sort numerically by priority, high priority first.
987 todo-state-up Sort by todo state, tasks that are done last.
988 todo-state-down Sort by todo state, tasks that are done first.
989 effort-up Sort numerically by estimated effort, high effort last.
990 effort-down Sort numerically by estimated effort, high effort first.
991 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
992 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
993 habit-up Put entries that are habits first
994 habit-down Put entries that are habits last
996 The different possibilities will be tried in sequence, and testing stops
997 if one comparison returns a \"not-equal\". For example, the default
998 '(time-up category-keep priority-down)
999 means: Pull out all entries having a specified time of day and sort them,
1000 in order to make a time schedule for the current day the first thing in the
1001 agenda listing for the day. Of the entries without a time indication, keep
1002 the grouped in categories, don't sort the categories, but keep them in
1003 the sequence given in `org-agenda-files'. Within each category sort by
1004 priority.
1006 Leaving out `category-keep' would mean that items will be sorted across
1007 categories by priority.
1009 Instead of a single list, this can also be a set of list for specific
1010 contents, with a context symbol in the car of the list, any of
1011 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1013 Custom commands can bind this variable in the options section."
1014 :group 'org-agenda-sorting
1015 :type `(choice
1016 (repeat :tag "General" ,org-sorting-choice)
1017 (list :tag "Individually"
1018 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1019 (repeat ,org-sorting-choice))
1020 (cons (const :tag "Strategy for TODO lists" todo)
1021 (repeat ,org-sorting-choice))
1022 (cons (const :tag "Strategy for Tags matches" tags)
1023 (repeat ,org-sorting-choice))
1024 (cons (const :tag "Strategy for search matches" search)
1025 (repeat ,org-sorting-choice)))))
1027 (defcustom org-agenda-cmp-user-defined nil
1028 "A function to define the comparison `user-defined'.
1029 This function must receive two arguments, agenda entry a and b.
1030 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1031 the user comparison, return nil.
1032 When this is defined, you can make `user-defined-up' and `user-defined-down'
1033 part of an agenda sorting strategy."
1034 :group 'org-agenda-sorting
1035 :type 'symbol)
1037 (defcustom org-sort-agenda-notime-is-late t
1038 "Non-nil means, items without time are considered late.
1039 This is only relevant for sorting. When t, items which have no explicit
1040 time like 15:30 will be considered as 99:01, i.e. later than any items which
1041 do have a time. When nil, the default time is before 0:00. You can use this
1042 option to decide if the schedule for today should come before or after timeless
1043 agenda entries."
1044 :group 'org-agenda-sorting
1045 :type 'boolean)
1047 (defcustom org-sort-agenda-noeffort-is-high t
1048 "Non-nil means, items without effort estimate are sorted as high effort.
1049 This also applies when filtering an agenda view with respect to the
1050 < or > effort operator. Then, tasks with no effort defined will be treated
1051 as tasks with high effort.
1052 When nil, such items are sorted as 0 minutes effort."
1053 :group 'org-agenda-sorting
1054 :type 'boolean)
1056 (defgroup org-agenda-line-format nil
1057 "Options concerning the entry prefix in the Org-mode agenda display."
1058 :tag "Org Agenda Line Format"
1059 :group 'org-agenda)
1061 (defcustom org-agenda-prefix-format
1062 '((agenda . " %-12:c%?-12t% s")
1063 (timeline . " % s")
1064 (todo . " %-12:c")
1065 (tags . " %-12:c")
1066 (search . " %-12:c"))
1067 "Format specifications for the prefix of items in the agenda views.
1068 An alist with four entries, for the different agenda types. The keys to the
1069 sublists are `agenda', `timeline', `todo', and `tags'. The values
1070 are format strings.
1071 This format works similar to a printf format, with the following meaning:
1073 %c the category of the item, \"Diary\" for entries from the diary, or
1074 as given by the CATEGORY keyword or derived from the file name.
1075 %T the *last* tag of the item. Last because inherited tags come
1076 first in the list.
1077 %t the time-of-day specification if one applies to the entry, in the
1078 format HH:MM
1079 %s Scheduling/Deadline information, a short string
1081 All specifiers work basically like the standard `%s' of printf, but may
1082 contain two additional characters: A question mark just after the `%' and
1083 a whitespace/punctuation character just before the final letter.
1085 If the first character after `%' is a question mark, the entire field
1086 will only be included if the corresponding value applies to the
1087 current entry. This is useful for fields which should have fixed
1088 width when present, but zero width when absent. For example,
1089 \"%?-12t\" will result in a 12 character time field if a time of the
1090 day is specified, but will completely disappear in entries which do
1091 not contain a time.
1093 If there is punctuation or whitespace character just before the final
1094 format letter, this character will be appended to the field value if
1095 the value is not empty. For example, the format \"%-12:c\" leads to
1096 \"Diary: \" if the category is \"Diary\". If the category were be
1097 empty, no additional colon would be inserted.
1099 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1100 - Indent the line with two space characters
1101 - Give the category in a 12 chars wide field, padded with whitespace on
1102 the right (because of `-'). Append a colon if there is a category
1103 (because of `:').
1104 - If there is a time-of-day, put it into a 12 chars wide field. If no
1105 time, don't put in an empty field, just skip it (because of '?').
1106 - Finally, put the scheduling information and append a whitespace.
1108 As another example, if you don't want the time-of-day of entries in
1109 the prefix, you could use:
1111 (setq org-agenda-prefix-format \" %-11:c% s\")
1113 See also the variables `org-agenda-remove-times-when-in-prefix' and
1114 `org-agenda-remove-tags'.
1116 Custom commands can set this variable in the options section."
1117 :type '(choice
1118 (string :tag "General format")
1119 (list :greedy t :tag "View dependent"
1120 (cons (const agenda) (string :tag "Format"))
1121 (cons (const timeline) (string :tag "Format"))
1122 (cons (const todo) (string :tag "Format"))
1123 (cons (const tags) (string :tag "Format"))
1124 (cons (const search) (string :tag "Format"))))
1125 :group 'org-agenda-line-format)
1127 (defvar org-prefix-format-compiled nil
1128 "The compiled version of the most recently used prefix format.
1129 See the variable `org-agenda-prefix-format'.")
1131 (defcustom org-agenda-todo-keyword-format "%-1s"
1132 "Format for the TODO keyword in agenda lines.
1133 Set this to something like \"%-12s\" if you want all TODO keywords
1134 to occupy a fixed space in the agenda display."
1135 :group 'org-agenda-line-format
1136 :type 'string)
1138 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1139 "Text preceding timerange entries in the agenda view.
1140 This is a list with two strings. The first applies when the range
1141 is entirely on one day. The second applies if the range spans several days.
1142 The strings may have two \"%d\" format specifiers which will be filled
1143 with the sequence number of the days, and the total number of days in the
1144 range, respectively."
1145 :group 'org-agenda-line-format
1146 :type '(list
1147 (string :tag "Deadline today ")
1148 (choice :tag "Deadline relative"
1149 (string :tag "Format string")
1150 (function))))
1152 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1153 "Text preceeding scheduled items in the agenda view.
1154 This is a list with two strings. The first applies when the item is
1155 scheduled on the current day. The second applies when it has been scheduled
1156 previously, it may contain a %d indicating that this is the nth time that
1157 this item is scheduled, due to automatic rescheduling of unfinished items
1158 for the following day. So this number is one larger than the number of days
1159 that passed since this item was scheduled first."
1160 :group 'org-agenda-line-format
1161 :type '(list
1162 (string :tag "Scheduled today ")
1163 (string :tag "Scheduled previously")))
1165 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1166 "Text preceeding deadline items in the agenda view.
1167 This is a list with two strings. The first applies when the item has its
1168 deadline on the current day. The second applies when it is in the past or
1169 in the future, it may contain %d to capture how many days away the deadline
1170 is (was)."
1171 :group 'org-agenda-line-format
1172 :type '(list
1173 (string :tag "Deadline today ")
1174 (choice :tag "Deadline relative"
1175 (string :tag "Format string")
1176 (function))))
1178 (defcustom org-agenda-remove-times-when-in-prefix t
1179 "Non-nil means, remove duplicate time specifications in agenda items.
1180 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1181 time-of-day specification in a headline or diary entry is extracted and
1182 placed into the prefix. If this option is non-nil, the original specification
1183 \(a timestamp or -range, or just a plain time(range) specification like
1184 11:30-4pm) will be removed for agenda display. This makes the agenda less
1185 cluttered.
1186 The option can be t or nil. It may also be the symbol `beg', indicating
1187 that the time should only be removed what it is located at the beginning of
1188 the headline/diary entry."
1189 :group 'org-agenda-line-format
1190 :type '(choice
1191 (const :tag "Always" t)
1192 (const :tag "Never" nil)
1193 (const :tag "When at beginning of entry" beg)))
1196 (defcustom org-agenda-default-appointment-duration nil
1197 "Default duration for appointments that only have a starting time.
1198 When nil, no duration is specified in such cases.
1199 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1200 :group 'org-agenda-line-format
1201 :type '(choice
1202 (integer :tag "Minutes")
1203 (const :tag "No default duration")))
1205 (defcustom org-agenda-show-inherited-tags t
1206 "Non-nil means, show inherited tags in each agenda line."
1207 :group 'org-agenda-line-format
1208 :type 'boolean)
1210 (defcustom org-agenda-remove-tags nil
1211 "Non-nil means, remove the tags from the headline copy in the agenda.
1212 When this is the symbol `prefix', only remove tags when
1213 `org-agenda-prefix-format' contains a `%T' specifier."
1214 :group 'org-agenda-line-format
1215 :type '(choice
1216 (const :tag "Always" t)
1217 (const :tag "Never" nil)
1218 (const :tag "When prefix format contains %T" prefix)))
1220 (if (fboundp 'defvaralias)
1221 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1222 'org-agenda-remove-tags))
1224 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1225 "Shift tags in agenda items to this column.
1226 If this number is positive, it specifies the column. If it is negative,
1227 it means that the tags should be flushright to that column. For example,
1228 -80 works well for a normal 80 character screen."
1229 :group 'org-agenda-line-format
1230 :type 'integer)
1232 (if (fboundp 'defvaralias)
1233 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1235 (defcustom org-agenda-fontify-priorities 'cookies
1236 "Non-nil means, highlight low and high priorities in agenda.
1237 When t, the highest priority entries are bold, lowest priority italic.
1238 However, settings in org-priority-faces will overrule these faces.
1239 When this variable is the symbol `cookies', only fontify the
1240 cookies, not the entire task.
1241 This may also be an association list of priority faces, whose
1242 keys are the character values of `org-highest-priority',
1243 `org-default-priority', and `org-lowest-priority' (the default values
1244 are ?A, ?B, and ?C, respectively). The face may be a named face,
1245 or a list like `(:background \"Red\")'."
1246 :group 'org-agenda-line-format
1247 :type '(choice
1248 (const :tag "Never" nil)
1249 (const :tag "Defaults" t)
1250 (const :tag "Cookies only" cookies)
1251 (repeat :tag "Specify"
1252 (list (character :tag "Priority" :value ?A)
1253 (sexp :tag "face")))))
1255 (defgroup org-agenda-column-view nil
1256 "Options concerning column view in the agenda."
1257 :tag "Org Agenda Column View"
1258 :group 'org-agenda)
1260 (defcustom org-agenda-columns-show-summaries t
1261 "Non-nil means, show summaries for columns displayed in the agenda view."
1262 :group 'org-agenda-column-view
1263 :type 'boolean)
1265 (defcustom org-agenda-columns-remove-prefix-from-item t
1266 "Non-nil means, remove the prefix from a headline for agenda column view.
1267 The special ITEM field in the columns format contains the current line, with
1268 all information shown in other columns (like the TODO state or a tag).
1269 When this variable is non-nil, also the agenda prefix will be removed from
1270 the content of the ITEM field, to make sure as much as possible of the
1271 headline can be shown in the limited width of the field."
1272 :group 'org-agenda
1273 :type 'boolean)
1275 (defcustom org-agenda-columns-compute-summary-properties t
1276 "Non-nil means, recompute all summary properties before column view.
1277 When column view in the agenda is listing properties that have a summary
1278 operator, it can go to all relevant buffers and recompute the summaries
1279 there. This can mean overhead for the agenda column view, but is necessary
1280 to have thing up to date.
1281 As a special case, a CLOCKSUM property also makes sure that the clock
1282 computations are current."
1283 :group 'org-agenda-column-view
1284 :type 'boolean)
1286 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1287 "Non-nil means, the duration of an appointment will add to day effort.
1288 The property to which appointment durations will be added is the one given
1289 in the option `org-effort-property'. If an appointment does not have
1290 an end time, `org-agenda-default-appointment-duration' will be used. If that
1291 is not set, an appointment without end time will not contribute to the time
1292 estimate."
1293 :group 'org-agenda-column-view
1294 :type 'boolean)
1296 (defcustom org-agenda-auto-exclude-function nil
1297 "A function called with a tag to decide if it is filtered on '/ RET'.
1298 The sole argument to the function, which is called once for each
1299 possible tag, is a string giving the name of the tag. The
1300 function should return either nil if the tag should be included
1301 as normal, or \"-<TAG>\" to exclude the tag."
1302 :group 'org-agenda
1303 :type 'function)
1305 (eval-when-compile
1306 (require 'cl))
1307 (require 'org)
1309 (defun org-add-agenda-custom-command (entry)
1310 "Replace or add a command in `org-agenda-custom-commands'.
1311 This is mostly for hacking and trying a new command - once the command
1312 works you probably want to add it to `org-agenda-custom-commands' for good."
1313 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1314 (if ass
1315 (setcdr ass (cdr entry))
1316 (push entry org-agenda-custom-commands))))
1318 ;;; Define the Org-agenda-mode
1320 (defvar org-agenda-mode-map (make-sparse-keymap)
1321 "Keymap for `org-agenda-mode'.")
1322 (if (fboundp 'defvaralias)
1323 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1325 (defvar org-agenda-menu) ; defined later in this file.
1326 (defvar org-agenda-restrict) ; defined later in this file.
1327 (defvar org-agenda-follow-mode nil)
1328 (defvar org-agenda-entry-text-mode nil)
1329 (defvar org-agenda-clockreport-mode nil)
1330 (defvar org-agenda-show-log nil)
1331 (defvar org-agenda-redo-command nil)
1332 (defvar org-agenda-query-string nil)
1333 (defvar org-agenda-mode-hook nil
1334 "Hook for org-agenda-mode, run after the mode is turned on.")
1335 (defvar org-agenda-type nil)
1336 (defvar org-agenda-force-single-file nil)
1337 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1339 (defun org-agenda-mode ()
1340 "Mode for time-sorted view on action items in Org-mode files.
1342 The following commands are available:
1344 \\{org-agenda-mode-map}"
1345 (interactive)
1346 (kill-all-local-variables)
1347 (setq org-agenda-undo-list nil
1348 org-agenda-pending-undo-list nil
1349 org-agenda-bulk-marked-entries nil)
1350 (setq major-mode 'org-agenda-mode)
1351 ;; Keep global-font-lock-mode from turning on font-lock-mode
1352 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1353 (setq mode-name "Org-Agenda")
1354 (use-local-map org-agenda-mode-map)
1355 (easy-menu-add org-agenda-menu)
1356 (if org-startup-truncated (setq truncate-lines t))
1357 (org-set-local 'line-move-visual nil)
1358 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1359 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1360 ;; Make sure properties are removed when copying text
1361 (when (boundp 'buffer-substring-filters)
1362 (org-set-local 'buffer-substring-filters
1363 (cons (lambda (x)
1364 (set-text-properties 0 (length x) nil x) x)
1365 buffer-substring-filters)))
1366 (unless org-agenda-keep-modes
1367 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1368 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1369 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1370 org-agenda-show-log org-agenda-start-with-log-mode))
1372 (easy-menu-change
1373 '("Agenda") "Agenda Files"
1374 (append
1375 (list
1376 (vector
1377 (if (get 'org-agenda-files 'org-restrict)
1378 "Restricted to single file"
1379 "Edit File List")
1380 '(org-edit-agenda-file-list)
1381 (not (get 'org-agenda-files 'org-restrict)))
1382 "--")
1383 (mapcar 'org-file-menu-entry (org-agenda-files))))
1384 (org-agenda-set-mode-name)
1385 (apply
1386 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1387 (list 'org-agenda-mode-hook)))
1389 (substitute-key-definition 'undo 'org-agenda-undo
1390 org-agenda-mode-map global-map)
1391 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1392 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1393 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1394 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1395 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1396 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1397 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1398 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1399 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1400 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1401 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1402 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1403 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1404 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1405 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1406 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1407 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1408 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1409 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1410 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1411 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1412 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1413 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1414 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1415 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1416 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1417 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1418 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1419 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1420 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1421 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1422 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1423 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1424 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1425 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1426 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1427 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1428 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1429 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1430 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1431 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1432 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1433 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1435 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1436 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1437 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1438 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1439 (while l (org-defkey org-agenda-mode-map
1440 (int-to-string (pop l)) 'digit-argument)))
1442 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1443 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1444 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1445 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1446 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1447 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1448 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1449 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1450 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1451 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1452 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1453 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1454 'org-clock-modify-effort-estimate)
1455 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1456 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1457 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1458 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1459 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1460 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1461 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1462 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1463 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1464 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1465 (substitute-key-definition 'next-line 'org-agenda-next-line
1466 org-agenda-mode-map global-map)
1467 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1468 org-agenda-mode-map global-map)
1469 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1470 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1471 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1472 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1473 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1474 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1475 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1476 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1477 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1478 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1479 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1480 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1481 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1482 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1483 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1484 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1485 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1486 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1487 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1488 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1489 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1490 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1491 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1492 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1493 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1494 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1495 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1496 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1497 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1498 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1500 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1501 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1502 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1503 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1504 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1505 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1506 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1507 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1508 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1509 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1511 (org-defkey org-agenda-mode-map
1512 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1513 (org-defkey org-agenda-mode-map
1514 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1515 (when org-agenda-mouse-1-follows-link
1516 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1517 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1518 '("Agenda"
1519 ("Agenda Files")
1520 "--"
1521 ("Agenda Dates"
1522 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1523 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1524 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1525 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1526 "--"
1527 ("View"
1528 ["Day View" org-agenda-day-view
1529 :active (org-agenda-check-type nil 'agenda)
1530 :style radio :selected (equal org-agenda-ndays 1)
1531 :keys "v d (or just d)"]
1532 ["Week View" org-agenda-week-view
1533 :active (org-agenda-check-type nil 'agenda)
1534 :style radio :selected (equal org-agenda-ndays 7)
1535 :keys "v w (or just w)"]
1536 ["Month View" org-agenda-month-view
1537 :active (org-agenda-check-type nil 'agenda)
1538 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1539 :keys "v m"]
1540 ["Year View" org-agenda-year-view
1541 :active (org-agenda-check-type nil 'agenda)
1542 :style radio :selected (member org-agenda-ndays '(365 366))
1543 :keys "v y"]
1544 "--"
1545 ["Include Diary" org-agenda-toggle-diary
1546 :style toggle :selected org-agenda-include-diary
1547 :active (org-agenda-check-type nil 'agenda)]
1548 ["Use Time Grid" org-agenda-toggle-time-grid
1549 :style toggle :selected org-agenda-use-time-grid
1550 :active (org-agenda-check-type nil 'agenda)]
1551 "--"
1552 ["Show clock report" org-agenda-clockreport-mode
1553 :style toggle :selected org-agenda-clockreport-mode
1554 :active (org-agenda-check-type nil 'agenda)]
1555 ["Show some entry text" org-agenda-entry-text-mode
1556 :style toggle :selected org-agenda-entry-text-mode
1557 :active t]
1558 "--"
1559 ["Show Logbook entries" org-agenda-log-mode
1560 :style toggle :selected org-agenda-show-log
1561 :active (org-agenda-check-type nil 'agenda 'timeline)
1562 :keys "v l (or just l)"]
1563 ["Include archived trees" org-agenda-archives-mode
1564 :style toggle :selected org-agenda-archives-mode :active t
1565 :keys "v a"]
1566 ["Include archive files" (org-agenda-archives-mode t)
1567 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1568 :keys "v A"]
1569 "--"
1570 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1571 ["Write view to file" org-write-agenda t]
1572 ["Rebuild buffer" org-agenda-redo t]
1573 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1574 "--"
1575 ["Show original entry" org-agenda-show t]
1576 ["Go To (other window)" org-agenda-goto t]
1577 ["Go To (this window)" org-agenda-switch-to t]
1578 ["Follow Mode" org-agenda-follow-mode
1579 :style toggle :selected org-agenda-follow-mode :active t]
1580 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1581 "--"
1582 ("TODO"
1583 ["Cycle TODO" org-agenda-todo t]
1584 ["Next TODO set" org-agenda-todo-nextset t]
1585 ["Previous TODO set" org-agenda-todo-previousset t]
1586 ["Add note" org-agenda-add-note t])
1587 ("Archive/Refile/Delete"
1588 ["Archive default" org-agenda-archive-default t]
1589 ["Archive default" org-agenda-archive-default-with-confirmation t]
1590 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1591 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1592 ["Archive subtree" org-agenda-archive t]
1593 "--"
1594 ["Refile" org-agenda-refile t]
1595 "--"
1596 ["Delete subtree" org-agenda-kill t])
1597 ("Bulk action"
1598 ["Mark entry" org-agenda-bulk-mark t]
1599 ["Unmark entry" org-agenda-bulk-unmark t]
1600 ["Act on all marked" org-agenda-bulk-action t]
1601 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1602 "--"
1603 ("Tags and Properties"
1604 ["Show all Tags" org-agenda-show-tags t]
1605 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1606 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1607 "--"
1608 ["Column View" org-columns t])
1609 ("Deadline/Schedule"
1610 ["Schedule" org-agenda-schedule t]
1611 ["Set Deadline" org-agenda-deadline t]
1612 "--"
1613 ["Mark item" org-agenda-action :active t :keys "k m"]
1614 ["Show mark item" org-agenda-action :active t :keys "k v"]
1615 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1616 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1617 "--"
1618 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1619 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1620 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1621 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1622 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1623 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1624 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1625 ("Clock and Effort"
1626 ["Clock in" org-agenda-clock-in t]
1627 ["Clock out" org-agenda-clock-out t]
1628 ["Clock cancel" org-agenda-clock-cancel t]
1629 ["Goto running clock" org-clock-goto t]
1630 "--"
1631 ["Set Effort" org-agenda-set-effort t]
1632 ["Change clocked effort" org-clock-modify-effort-estimate
1633 (org-clock-is-active)])
1634 ("Priority"
1635 ["Set Priority" org-agenda-priority t]
1636 ["Increase Priority" org-agenda-priority-up t]
1637 ["Decrease Priority" org-agenda-priority-down t]
1638 ["Show Priority" org-agenda-show-priority t])
1639 ("Calendar/Diary"
1640 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1641 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1642 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1643 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1644 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1645 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1646 "--"
1647 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
1648 "--"
1649 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1650 "--"
1651 ("MobileOrg"
1652 ["Push Files and Views" org-mobile-push t]
1653 ["Get Captured and Flagged" org-mobile-pull t]
1654 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1655 ["Show note / unflag" org-agenda-show-the-flagging-note t]
1656 "--"
1657 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
1658 "--"
1659 ["Quit" org-agenda-quit t]
1660 ["Exit and Release Buffers" org-agenda-exit t]
1663 ;;; Agenda undo
1665 (defvar org-agenda-allow-remote-undo t
1666 "Non-nil means, allow remote undo from the agenda buffer.")
1667 (defvar org-agenda-undo-list nil
1668 "List of undoable operations in the agenda since last refresh.")
1669 (defvar org-agenda-undo-has-started-in nil
1670 "Buffers that have already seen `undo-start' in the current undo sequence.")
1671 (defvar org-agenda-pending-undo-list nil
1672 "In a series of undo commands, this is the list of remaining undo items.")
1675 (defun org-agenda-undo ()
1676 "Undo a remote editing step in the agenda.
1677 This undoes changes both in the agenda buffer and in the remote buffer
1678 that have been changed along."
1679 (interactive)
1680 (or org-agenda-allow-remote-undo
1681 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
1682 (if (not (eq this-command last-command))
1683 (setq org-agenda-undo-has-started-in nil
1684 org-agenda-pending-undo-list org-agenda-undo-list))
1685 (if (not org-agenda-pending-undo-list)
1686 (error "No further undo information"))
1687 (let* ((entry (pop org-agenda-pending-undo-list))
1688 buf line cmd rembuf)
1689 (setq cmd (pop entry) line (pop entry))
1690 (setq rembuf (nth 2 entry))
1691 (org-with-remote-undo rembuf
1692 (while (bufferp (setq buf (pop entry)))
1693 (if (pop entry)
1694 (with-current-buffer buf
1695 (let ((last-undo-buffer buf)
1696 (inhibit-read-only t))
1697 (unless (memq buf org-agenda-undo-has-started-in)
1698 (push buf org-agenda-undo-has-started-in)
1699 (make-local-variable 'pending-undo-list)
1700 (undo-start))
1701 (while (and pending-undo-list
1702 (listp pending-undo-list)
1703 (not (car pending-undo-list)))
1704 (pop pending-undo-list))
1705 (undo-more 1))))))
1706 (org-goto-line line)
1707 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1709 (defun org-verify-change-for-undo (l1 l2)
1710 "Verify that a real change occurred between the undo lists L1 and L2."
1711 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1712 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1713 (not (eq l1 l2)))
1715 ;;; Agenda dispatch
1717 (defvar org-agenda-restrict nil)
1718 (defvar org-agenda-restrict-begin (make-marker))
1719 (defvar org-agenda-restrict-end (make-marker))
1720 (defvar org-agenda-last-dispatch-buffer nil)
1721 (defvar org-agenda-overriding-restriction nil)
1723 ;;;###autoload
1724 (defun org-agenda (&optional arg keys restriction)
1725 "Dispatch agenda commands to collect entries to the agenda buffer.
1726 Prompts for a command to execute. Any prefix arg will be passed
1727 on to the selected command. The default selections are:
1729 a Call `org-agenda-list' to display the agenda for current day or week.
1730 t Call `org-todo-list' to display the global todo list.
1731 T Call `org-todo-list' to display the global todo list, select only
1732 entries with a specific TODO keyword (the user gets a prompt).
1733 m Call `org-tags-view' to display headlines with tags matching
1734 a condition (the user is prompted for the condition).
1735 M Like `m', but select only TODO entries, no ordinary headlines.
1736 L Create a timeline for the current buffer.
1737 e Export views to associated files.
1738 s Search entries for keywords.
1739 / Multi occur across all agenda files and also files listed
1740 in `org-agenda-text-search-extra-files'.
1741 < Restrict agenda commands to buffer, subtree, or region.
1742 Press several times to get the desired effect.
1743 > Remove a previous restriction.
1744 # List \"stuck\" projects.
1745 ! Configure what \"stuck\" means.
1746 C Configure custom agenda commands.
1748 More commands can be added by configuring the variable
1749 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1750 searches can be pre-defined in this way.
1752 If the current buffer is in Org-mode and visiting a file, you can also
1753 first press `<' once to indicate that the agenda should be temporarily
1754 \(until the next use of \\[org-agenda]) restricted to the current file.
1755 Pressing `<' twice means to restrict to the current subtree or region
1756 \(if active)."
1757 (interactive "P")
1758 (catch 'exit
1759 (let* ((prefix-descriptions nil)
1760 (org-agenda-window-setup (if (equal (buffer-name)
1761 org-agenda-buffer-name)
1762 'current-window
1763 org-agenda-window-setup))
1764 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1765 (org-agenda-custom-commands
1766 ;; normalize different versions
1767 (delq nil
1768 (mapcar
1769 (lambda (x)
1770 (cond ((stringp (cdr x))
1771 (push x prefix-descriptions)
1772 nil)
1773 ((stringp (nth 1 x)) x)
1774 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1775 (t (cons (car x) (cons "" (cdr x))))))
1776 org-agenda-custom-commands)))
1777 (buf (current-buffer))
1778 (bfn (buffer-file-name (buffer-base-buffer)))
1779 entry key type match lprops ans)
1780 ;; Turn off restriction unless there is an overriding one,
1781 (unless org-agenda-overriding-restriction
1782 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
1783 ;; There is a request to keep the file list in place
1784 (put 'org-agenda-files 'org-restrict nil))
1785 (setq org-agenda-restrict nil)
1786 (move-marker org-agenda-restrict-begin nil)
1787 (move-marker org-agenda-restrict-end nil))
1788 ;; Delete old local properties
1789 (put 'org-agenda-redo-command 'org-lprops nil)
1790 ;; Remember where this call originated
1791 (setq org-agenda-last-dispatch-buffer (current-buffer))
1792 (unless keys
1793 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1794 keys (car ans)
1795 restriction (cdr ans)))
1796 ;; Establish the restriction, if any
1797 (when (and (not org-agenda-overriding-restriction) restriction)
1798 (put 'org-agenda-files 'org-restrict (list bfn))
1799 (cond
1800 ((eq restriction 'region)
1801 (setq org-agenda-restrict t)
1802 (move-marker org-agenda-restrict-begin (region-beginning))
1803 (move-marker org-agenda-restrict-end (region-end)))
1804 ((eq restriction 'subtree)
1805 (save-excursion
1806 (setq org-agenda-restrict t)
1807 (org-back-to-heading t)
1808 (move-marker org-agenda-restrict-begin (point))
1809 (move-marker org-agenda-restrict-end
1810 (progn (org-end-of-subtree t)))))))
1812 (require 'calendar) ; FIXME: can we avoid this for some commands?
1813 ;; For example the todo list should not need it (but does...)
1814 (cond
1815 ((setq entry (assoc keys org-agenda-custom-commands))
1816 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1817 (progn
1818 (setq type (nth 2 entry) match (eval (nth 3 entry))
1819 lprops (nth 4 entry))
1820 (put 'org-agenda-redo-command 'org-lprops lprops)
1821 (cond
1822 ((eq type 'agenda)
1823 (org-let lprops '(org-agenda-list current-prefix-arg)))
1824 ((eq type 'alltodo)
1825 (org-let lprops '(org-todo-list current-prefix-arg)))
1826 ((eq type 'search)
1827 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1828 ((eq type 'stuck)
1829 (org-let lprops '(org-agenda-list-stuck-projects
1830 current-prefix-arg)))
1831 ((eq type 'tags)
1832 (org-let lprops '(org-tags-view current-prefix-arg match)))
1833 ((eq type 'tags-todo)
1834 (org-let lprops '(org-tags-view '(4) match)))
1835 ((eq type 'todo)
1836 (org-let lprops '(org-todo-list match)))
1837 ((eq type 'tags-tree)
1838 (org-check-for-org-mode)
1839 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1840 ((eq type 'todo-tree)
1841 (org-check-for-org-mode)
1842 (org-let lprops
1843 '(org-occur (concat "^" outline-regexp "[ \t]*"
1844 (regexp-quote match) "\\>"))))
1845 ((eq type 'occur-tree)
1846 (org-check-for-org-mode)
1847 (org-let lprops '(org-occur match)))
1848 ((functionp type)
1849 (org-let lprops '(funcall type match)))
1850 ((fboundp type)
1851 (org-let lprops '(funcall type match)))
1852 (t (error "Invalid custom agenda command type %s" type))))
1853 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1854 ((equal keys "C")
1855 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1856 (customize-variable 'org-agenda-custom-commands))
1857 ((equal keys "a") (call-interactively 'org-agenda-list))
1858 ((equal keys "s") (call-interactively 'org-search-view))
1859 ((equal keys "t") (call-interactively 'org-todo-list))
1860 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1861 ((equal keys "m") (call-interactively 'org-tags-view))
1862 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1863 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1864 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1865 (org-add-hook
1866 'post-command-hook
1867 (lambda ()
1868 (unless (current-message)
1869 (let* ((m (org-agenda-get-any-marker))
1870 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1871 (when note
1872 (message (concat
1873 "FLAGGING-NOTE ([?] for more info): "
1874 (org-add-props
1875 (replace-regexp-in-string
1876 "\\\\n" "//"
1877 (copy-sequence note))
1878 nil 'face 'org-warning)))))))
1879 t t))
1880 ((equal keys "L")
1881 (unless (org-mode-p)
1882 (error "This is not an Org-mode file"))
1883 (unless restriction
1884 (put 'org-agenda-files 'org-restrict (list bfn))
1885 (org-call-with-arg 'org-timeline arg)))
1886 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1887 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1888 ((equal keys "!") (customize-variable 'org-stuck-projects))
1889 (t (error "Invalid agenda key"))))))
1891 (defun org-agenda-normalize-custom-commands (cmds)
1892 (delq nil
1893 (mapcar
1894 (lambda (x)
1895 (cond ((stringp (cdr x)) nil)
1896 ((stringp (nth 1 x)) x)
1897 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1898 (t (cons (car x) (cons "" (cdr x))))))
1899 cmds)))
1901 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1902 "The user interface for selecting an agenda command."
1903 (catch 'exit
1904 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1905 (restrict-ok (and bfn (org-mode-p)))
1906 (region-p (org-region-active-p))
1907 (custom org-agenda-custom-commands)
1908 (selstring "")
1909 restriction second-time
1910 c entry key type match prefixes rmheader header-end custom1 desc)
1911 (save-window-excursion
1912 (delete-other-windows)
1913 (org-switch-to-buffer-other-window " *Agenda Commands*")
1914 (erase-buffer)
1915 (insert (eval-when-compile
1916 (let ((header
1918 Press key for an agenda command: < Buffer, subtree/region restriction
1919 -------------------------------- > Remove restriction
1920 a Agenda for current week or day e Export agenda views
1921 t List of all TODO entries T Entries with special TODO kwd
1922 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1923 L Timeline for current buffer # List stuck projects (!=configure)
1924 s Search for keywords C Configure custom agenda commands
1925 / Multi-occur ? Find :FLAGGED: entries
1927 (start 0))
1928 (while (string-match
1929 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1930 header start)
1931 (setq start (match-end 0))
1932 (add-text-properties (match-beginning 2) (match-end 2)
1933 '(face bold) header))
1934 header)))
1935 (setq header-end (move-marker (make-marker) (point)))
1936 (while t
1937 (setq custom1 custom)
1938 (when (eq rmheader t)
1939 (org-goto-line 1)
1940 (re-search-forward ":" nil t)
1941 (delete-region (match-end 0) (point-at-eol))
1942 (forward-char 1)
1943 (looking-at "-+")
1944 (delete-region (match-end 0) (point-at-eol))
1945 (move-marker header-end (match-end 0)))
1946 (goto-char header-end)
1947 (delete-region (point) (point-max))
1948 (while (setq entry (pop custom1))
1949 (setq key (car entry) desc (nth 1 entry)
1950 type (nth 2 entry)
1951 match (nth 3 entry))
1952 (if (> (length key) 1)
1953 (add-to-list 'prefixes (string-to-char key))
1954 (insert
1955 (format
1956 "\n%-4s%-14s: %s"
1957 (org-add-props (copy-sequence key)
1958 '(face bold))
1959 (cond
1960 ((string-match "\\S-" desc) desc)
1961 ((eq type 'agenda) "Agenda for current week or day")
1962 ((eq type 'alltodo) "List of all TODO entries")
1963 ((eq type 'search) "Word search")
1964 ((eq type 'stuck) "List of stuck projects")
1965 ((eq type 'todo) "TODO keyword")
1966 ((eq type 'tags) "Tags query")
1967 ((eq type 'tags-todo) "Tags (TODO)")
1968 ((eq type 'tags-tree) "Tags tree")
1969 ((eq type 'todo-tree) "TODO kwd tree")
1970 ((eq type 'occur-tree) "Occur tree")
1971 ((functionp type) (if (symbolp type)
1972 (symbol-name type)
1973 "Lambda expression"))
1974 (t "???"))
1975 (cond
1976 ((stringp match)
1977 (setq match (copy-sequence match))
1978 (org-add-props match nil 'face 'org-warning))
1979 (match
1980 (format "set of %d commands" (length match)))
1981 (t ""))))))
1982 (when prefixes
1983 (mapc (lambda (x)
1984 (insert
1985 (format "\n%s %s"
1986 (org-add-props (char-to-string x)
1987 nil 'face 'bold)
1988 (or (cdr (assoc (concat selstring (char-to-string x))
1989 prefix-descriptions))
1990 "Prefix key"))))
1991 prefixes))
1992 (goto-char (point-min))
1993 (if second-time
1994 (if (not (pos-visible-in-window-p (point-max)))
1995 (org-fit-window-to-buffer))
1996 (setq second-time t)
1997 (org-fit-window-to-buffer))
1998 (message "Press key for agenda command%s:"
1999 (if (or restrict-ok org-agenda-overriding-restriction)
2000 (if org-agenda-overriding-restriction
2001 " (restriction lock active)"
2002 (if restriction
2003 (format " (restricted to %s)" restriction)
2004 " (unrestricted)"))
2005 ""))
2006 (setq c (read-char-exclusive))
2007 (message "")
2008 (cond
2009 ((assoc (char-to-string c) custom)
2010 (setq selstring (concat selstring (char-to-string c)))
2011 (throw 'exit (cons selstring restriction)))
2012 ((memq c prefixes)
2013 (setq selstring (concat selstring (char-to-string c))
2014 prefixes nil
2015 rmheader (or rmheader t)
2016 custom (delq nil (mapcar
2017 (lambda (x)
2018 (if (or (= (length (car x)) 1)
2019 (/= (string-to-char (car x)) c))
2021 (cons (substring (car x) 1) (cdr x))))
2022 custom))))
2023 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2024 (message "Restriction is only possible in Org-mode buffers")
2025 (ding) (sit-for 1))
2026 ((eq c ?1)
2027 (org-agenda-remove-restriction-lock 'noupdate)
2028 (setq restriction 'buffer))
2029 ((eq c ?0)
2030 (org-agenda-remove-restriction-lock 'noupdate)
2031 (setq restriction (if region-p 'region 'subtree)))
2032 ((eq c ?<)
2033 (org-agenda-remove-restriction-lock 'noupdate)
2034 (setq restriction
2035 (cond
2036 ((eq restriction 'buffer)
2037 (if region-p 'region 'subtree))
2038 ((memq restriction '(subtree region))
2039 nil)
2040 (t 'buffer))))
2041 ((eq c ?>)
2042 (org-agenda-remove-restriction-lock 'noupdate)
2043 (setq restriction nil))
2044 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2045 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2046 ((and (> (length selstring) 0) (eq c ?\d))
2047 (delete-window)
2048 (org-agenda-get-restriction-and-command prefix-descriptions))
2050 ((equal c ?q) (error "Abort"))
2051 (t (error "Invalid key %c" c))))))))
2053 (defun org-run-agenda-series (name series)
2054 (org-let (nth 1 series) '(org-prepare-agenda name))
2055 (let* ((org-agenda-multi t)
2056 (redo (list 'org-run-agenda-series name (list 'quote series)))
2057 (cmds (car series))
2058 (gprops (nth 1 series))
2059 match ;; The byte compiler incorrectly complains about this. Keep it!
2060 cmd type lprops)
2061 (while (setq cmd (pop cmds))
2062 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2063 (cond
2064 ((eq type 'agenda)
2065 (org-let2 gprops lprops
2066 '(call-interactively 'org-agenda-list)))
2067 ((eq type 'alltodo)
2068 (org-let2 gprops lprops
2069 '(call-interactively 'org-todo-list)))
2070 ((eq type 'search)
2071 (org-let2 gprops lprops
2072 '(org-search-view current-prefix-arg match nil)))
2073 ((eq type 'stuck)
2074 (org-let2 gprops lprops
2075 '(call-interactively 'org-agenda-list-stuck-projects)))
2076 ((eq type 'tags)
2077 (org-let2 gprops lprops
2078 '(org-tags-view current-prefix-arg match)))
2079 ((eq type 'tags-todo)
2080 (org-let2 gprops lprops
2081 '(org-tags-view '(4) match)))
2082 ((eq type 'todo)
2083 (org-let2 gprops lprops
2084 '(org-todo-list match)))
2085 ((fboundp type)
2086 (org-let2 gprops lprops
2087 '(funcall type match)))
2088 (t (error "Invalid type in command series"))))
2089 (widen)
2090 (setq org-agenda-redo-command redo)
2091 (goto-char (point-min)))
2092 (org-fit-agenda-window)
2093 (org-let (nth 1 series) '(org-finalize-agenda)))
2095 ;;;###autoload
2096 (defmacro org-batch-agenda (cmd-key &rest parameters)
2097 "Run an agenda command in batch mode and send the result to STDOUT.
2098 If CMD-KEY is a string of length 1, it is used as a key in
2099 `org-agenda-custom-commands' and triggers this command. If it is a
2100 longer string it is used as a tags/todo match string.
2101 Paramters are alternating variable names and values that will be bound
2102 before running the agenda command."
2103 (let (pars)
2104 (while parameters
2105 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2106 (if (> (length cmd-key) 2)
2107 (eval (list 'let (nreverse pars)
2108 (list 'org-tags-view nil cmd-key)))
2109 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2110 (set-buffer org-agenda-buffer-name)
2111 (princ (org-encode-for-stdout (buffer-string)))))
2113 ;(defun org-encode-for-stdout (string)
2114 ; (if (fboundp 'encode-coding-string)
2115 ; (encode-coding-string string buffer-file-coding-system)
2116 ; string))
2118 (defun org-encode-for-stdout (string)
2119 string)
2121 (defvar org-agenda-info nil)
2123 ;;;###autoload
2124 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2125 "Run an agenda command in batch mode and send the result to STDOUT.
2126 If CMD-KEY is a string of length 1, it is used as a key in
2127 `org-agenda-custom-commands' and triggers this command. If it is a
2128 longer string it is used as a tags/todo match string.
2129 Paramters are alternating variable names and values that will be bound
2130 before running the agenda command.
2132 The output gives a line for each selected agenda item. Each
2133 item is a list of comma-separated values, like this:
2135 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2137 category The category of the item
2138 head The headline, without TODO kwd, TAGS and PRIORITY
2139 type The type of the agenda entry, can be
2140 todo selected in TODO match
2141 tagsmatch selected in tags match
2142 diary imported from diary
2143 deadline a deadline on given date
2144 scheduled scheduled on given date
2145 timestamp entry has timestamp on given date
2146 closed entry was closed on given date
2147 upcoming-deadline warning about deadline
2148 past-scheduled forwarded scheduled item
2149 block entry has date block including g. date
2150 todo The todo keyword, if any
2151 tags All tags including inherited ones, separated by colons
2152 date The relevant date, like 2007-2-14
2153 time The time, like 15:00-16:50
2154 extra Sting with extra planning info
2155 priority-l The priority letter if any was given
2156 priority-n The computed numerical priority
2157 agenda-day The day in the agenda where this is listed"
2159 (let (pars)
2160 (while parameters
2161 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2162 (push (list 'org-agenda-remove-tags t) pars)
2163 (if (> (length cmd-key) 2)
2164 (eval (list 'let (nreverse pars)
2165 (list 'org-tags-view nil cmd-key)))
2166 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2167 (set-buffer org-agenda-buffer-name)
2168 (let* ((lines (org-split-string (buffer-string) "\n"))
2169 line)
2170 (while (setq line (pop lines))
2171 (catch 'next
2172 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2173 (setq org-agenda-info
2174 (org-fix-agenda-info (text-properties-at 0 line)))
2175 (princ
2176 (org-encode-for-stdout
2177 (mapconcat 'org-agenda-export-csv-mapper
2178 '(org-category txt type todo tags date time-of-day extra
2179 priority-letter priority agenda-day)
2180 ",")))
2181 (princ "\n"))))))
2183 (defun org-fix-agenda-info (props)
2184 "Make sure all properties on an agenda item have a canonical form,
2185 so the export commands can easily use it."
2186 (let (tmp re)
2187 (when (setq tmp (plist-get props 'tags))
2188 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2189 (when (setq tmp (plist-get props 'date))
2190 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2191 (let ((calendar-date-display-form '(year "-" month "-" day)))
2192 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2194 (setq tmp (calendar-date-string tmp)))
2195 (setq props (plist-put props 'date tmp)))
2196 (when (setq tmp (plist-get props 'day))
2197 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2198 (let ((calendar-date-display-form '(year "-" month "-" day)))
2199 (setq tmp (calendar-date-string tmp)))
2200 (setq props (plist-put props 'day tmp))
2201 (setq props (plist-put props 'agenda-day tmp)))
2202 (when (setq tmp (plist-get props 'txt))
2203 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2204 (plist-put props 'priority-letter (match-string 1 tmp))
2205 (setq tmp (replace-match "" t t tmp)))
2206 (when (and (setq re (plist-get props 'org-todo-regexp))
2207 (setq re (concat "\\`\\.*" re " ?"))
2208 (string-match re tmp))
2209 (plist-put props 'todo (match-string 1 tmp))
2210 (setq tmp (replace-match "" t t tmp)))
2211 (plist-put props 'txt tmp)))
2212 props)
2214 (defun org-agenda-export-csv-mapper (prop)
2215 (let ((res (plist-get org-agenda-info prop)))
2216 (setq res
2217 (cond
2218 ((not res) "")
2219 ((stringp res) res)
2220 (t (prin1-to-string res))))
2221 (while (string-match "," res)
2222 (setq res (replace-match ";" t t res)))
2223 (org-trim res)))
2226 ;;;###autoload
2227 (defun org-store-agenda-views (&rest parameters)
2228 (interactive)
2229 (eval (list 'org-batch-store-agenda-views)))
2231 ;; FIXME, why is this a macro?????
2232 ;;;###autoload
2233 (defmacro org-batch-store-agenda-views (&rest parameters)
2234 "Run all custom agenda commands that have a file argument."
2235 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2236 (pop-up-frames nil)
2237 (dir default-directory)
2238 pars cmd thiscmdkey files opts cmd-or-set)
2239 (while parameters
2240 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2241 (setq pars (reverse pars))
2242 (save-window-excursion
2243 (while cmds
2244 (setq cmd (pop cmds)
2245 thiscmdkey (car cmd)
2246 cmd-or-set (nth 2 cmd)
2247 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2248 files (nth (if (listp cmd-or-set) 4 5) cmd))
2249 (if (stringp files) (setq files (list files)))
2250 (when files
2251 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2252 (list 'org-agenda nil thiscmdkey)))
2253 (set-buffer org-agenda-buffer-name)
2254 (while files
2255 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2256 (list 'org-write-agenda
2257 (expand-file-name (pop files) dir) nil t))))
2258 (and (get-buffer org-agenda-buffer-name)
2259 (kill-buffer org-agenda-buffer-name)))))))
2261 (defun org-agenda-mark-header-line (pos)
2262 "Mark the line at POS as an agenda structure header."
2263 (save-excursion
2264 (goto-char pos)
2265 (put-text-property (point-at-bol) (point-at-eol)
2266 'org-agenda-structural-header t)
2267 (when org-agenda-title-append
2268 (put-text-property (point-at-bol) (point-at-eol)
2269 'org-agenda-title-append org-agenda-title-append))))
2271 (defvar org-mobile-creating-agendas)
2272 (defun org-write-agenda (file &optional open nosettings)
2273 "Write the current buffer (an agenda view) as a file.
2274 Depending on the extension of the file name, plain text (.txt),
2275 HTML (.html or .htm) or Postscript (.ps) is produced.
2276 If the extension is .ics, run icalendar export over all files used
2277 to construct the agenda and limit the export to entries listed in the
2278 agenda now.
2279 With prefix argument OPEN, open the new file immediately.
2280 If NOSETTINGS is given, do not scope the settings of
2281 `org-agenda-exporter-settings' into the export commands. This is used when
2282 the settings have already been scoped and we do not wish to overrule other,
2283 higher priority settings."
2284 (interactive "FWrite agenda to file: \nP")
2285 (if (not (file-writable-p file))
2286 (error "Cannot write agenda to file %s" file))
2287 (cond
2288 ((string-match "\\.html?\\'" file) (require 'htmlize))
2289 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2290 (org-let (if nosettings nil org-agenda-exporter-settings)
2291 '(save-excursion
2292 (save-window-excursion
2293 (org-agenda-mark-filtered-text)
2294 (let ((bs (copy-sequence (buffer-string))) beg)
2295 (org-agenda-unmark-filtered-text)
2296 (with-temp-buffer
2297 (insert bs)
2298 (org-agenda-remove-marked-text 'org-filtered)
2299 (while (setq beg (text-property-any (point-min) (point-max)
2300 'org-filtered t))
2301 (delete-region
2302 beg (or (next-single-property-change beg 'org-filtered)
2303 (point-max))))
2304 (run-hooks 'org-agenda-before-write-hook)
2305 (cond
2306 ((org-bound-and-true-p org-mobile-creating-agendas)
2307 (org-mobile-write-agenda-for-mobile file))
2308 ((string-match "\\.html?\\'" file)
2309 (set-buffer (htmlize-buffer (current-buffer)))
2311 (when (and org-agenda-export-html-style
2312 (string-match "<style>" org-agenda-export-html-style))
2313 ;; replace <style> section with org-agenda-export-html-style
2314 (goto-char (point-min))
2315 (kill-region (- (search-forward "<style") 6)
2316 (search-forward "</style>"))
2317 (insert org-agenda-export-html-style))
2318 (write-file file)
2319 (kill-buffer (current-buffer))
2320 (message "HTML written to %s" file))
2321 ((string-match "\\.ps\\'" file)
2322 (require 'ps-print)
2323 (flet ((ps-get-buffer-name () "Agenda View"))
2324 (ps-print-buffer-with-faces file))
2325 (message "Postscript written to %s" file))
2326 ((string-match "\\.pdf\\'" file)
2327 (require 'ps-print)
2328 (flet ((ps-get-buffer-name () "Agenda View"))
2329 (ps-print-buffer-with-faces
2330 (concat (file-name-sans-extension file) ".ps")))
2331 (call-process "ps2pdf" nil nil nil
2332 (expand-file-name
2333 (concat (file-name-sans-extension file) ".ps"))
2334 (expand-file-name file))
2335 (message "PDF written to %s" file))
2336 ((string-match "\\.ics\\'" file)
2337 (require 'org-icalendar)
2338 (let ((org-agenda-marker-table
2339 (org-create-marker-find-array
2340 (org-agenda-collect-markers)))
2341 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2342 (org-combined-agenda-icalendar-file file))
2343 (apply 'org-export-icalendar 'combine
2344 (org-agenda-files nil 'ifmode))))
2346 (let ((bs (buffer-string)))
2347 (find-file file)
2348 (erase-buffer)
2349 (insert bs)
2350 (save-buffer 0)
2351 (kill-buffer (current-buffer))
2352 (message "Plain text written to %s" file))))))))
2353 (set-buffer org-agenda-buffer-name))
2354 (when open (org-open-file file)))
2356 (defvar org-agenda-filter-overlays nil)
2358 (defun org-agenda-mark-filtered-text ()
2359 "Mark all text hidden by filtering with a text property."
2360 (let ((inhibit-read-only t))
2361 (mapc
2362 (lambda (o)
2363 (when (equal (org-overlay-buffer o) (current-buffer))
2364 (put-text-property
2365 (org-overlay-start o) (org-overlay-end o)
2366 'org-filtered t)))
2367 org-agenda-filter-overlays)))
2369 (defun org-agenda-unmark-filtered-text ()
2370 "Remove the filtering text property."
2371 (let ((inhibit-read-only t))
2372 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2374 (defun org-agenda-remove-marked-text (property &optional value)
2375 "Delete all text marked with VALUE of PROPERTY.
2376 VALUE defaults to t."
2377 (let (beg)
2378 (setq value (or value t))
2379 (while (setq beg (text-property-any (point-min) (point-max)
2380 property value))
2381 (delete-region
2382 beg (or (next-single-property-change beg 'org-filtered)
2383 (point-max))))))
2385 (defun org-agenda-add-entry-text ()
2386 "Add entry text to agenda lines.
2387 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2388 entry text following headings shown in the agenda.
2389 Drawers will be excluded, also the line with scheduling/deadline info."
2390 (when (and (> org-agenda-add-entry-text-maxlines 0)
2391 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2392 (let (m txt)
2393 (goto-char (point-min))
2394 (while (not (eobp))
2395 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2396 (beginning-of-line 2)
2397 (setq txt (org-agenda-get-some-entry-text
2398 m org-agenda-add-entry-text-maxlines " > "))
2399 (end-of-line 1)
2400 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2402 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2403 &rest keep)
2404 "Extract entry text from MARKER, at most N-LINES lines.
2405 This will ignore drawers etc, just get the text.
2406 If INDENT is given, prefix every line with this string. If KEEP is
2407 given, it is a list of symbols, defining stuff that should not be
2408 removed from the entry content. Currently only `planning' is allowed here."
2409 (let (txt drawer-re kwd-time-re ind)
2410 (save-excursion
2411 (with-current-buffer (marker-buffer marker)
2412 (if (not (org-mode-p))
2413 (setq txt "")
2414 (save-excursion
2415 (save-restriction
2416 (widen)
2417 (goto-char marker)
2418 (end-of-line 1)
2419 (setq txt (buffer-substring
2420 (min (1+ (point)) (point-max))
2421 (progn (outline-next-heading) (point)))
2422 drawer-re org-drawer-regexp
2423 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2424 ".*\n?"))
2425 (with-temp-buffer
2426 (insert txt)
2427 (when org-agenda-add-entry-text-descriptive-links
2428 (goto-char (point-min))
2429 (while (org-activate-bracket-links (point-max))
2430 (add-text-properties (match-beginning 0) (match-end 0)
2431 '(face org-link))))
2432 (goto-char (point-min))
2433 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2434 (set-text-properties (match-beginning 0) (match-end 0)
2435 nil))
2436 (goto-char (point-min))
2437 (while (re-search-forward drawer-re nil t)
2438 (delete-region
2439 (match-beginning 0)
2440 (progn (re-search-forward
2441 "^[ \t]*:END:.*\n?" nil 'move)
2442 (point))))
2443 (unless (member 'planning keep)
2444 (goto-char (point-min))
2445 (while (re-search-forward kwd-time-re nil t)
2446 (replace-match "")))
2447 (goto-char (point-min))
2448 (when org-agenda-entry-text-exclude-regexps
2449 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2450 (while (setq re (pop re-list))
2451 (goto-char (point-min))
2452 (while (re-search-forward re nil t)
2453 (replace-match "")))))
2454 (goto-char (point-max))
2455 (skip-chars-backward " \t\n")
2456 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2458 ;; find and remove min common indentation
2459 (goto-char (point-min))
2460 (untabify (point-min) (point-max))
2461 (setq ind (org-get-indentation))
2462 (while (not (eobp))
2463 (unless (looking-at "[ \t]*$")
2464 (setq ind (min ind (org-get-indentation))))
2465 (beginning-of-line 2))
2466 (goto-char (point-min))
2467 (while (not (eobp))
2468 (unless (looking-at "[ \t]*$")
2469 (move-to-column ind)
2470 (delete-region (point-at-bol) (point)))
2471 (beginning-of-line 2))
2473 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2475 (goto-char (point-min))
2476 (when indent
2477 (while (and (not (eobp)) (re-search-forward "^" nil t))
2478 (replace-match indent t t)))
2479 (goto-char (point-min))
2480 (while (looking-at "[ \t]*\n") (replace-match ""))
2481 (goto-char (point-max))
2482 (when (> (org-current-line)
2483 n-lines)
2484 (org-goto-line (1+ n-lines))
2485 (backward-char 1))
2486 (setq txt (buffer-substring (point-min) (point)))))))))
2487 txt))
2489 (defun org-agenda-collect-markers ()
2490 "Collect the markers pointing to entries in the agenda buffer."
2491 (let (m markers)
2492 (save-excursion
2493 (goto-char (point-min))
2494 (while (not (eobp))
2495 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2496 (org-get-at-bol 'org-marker)))
2497 (push m markers))
2498 (beginning-of-line 2)))
2499 (nreverse markers)))
2501 (defun org-create-marker-find-array (marker-list)
2502 "Create a alist of files names with all marker positions in that file."
2503 (let (f tbl m a p)
2504 (while (setq m (pop marker-list))
2505 (setq p (marker-position m)
2506 f (buffer-file-name (or (buffer-base-buffer
2507 (marker-buffer m))
2508 (marker-buffer m))))
2509 (if (setq a (assoc f tbl))
2510 (push (marker-position m) (cdr a))
2511 (push (list f p) tbl)))
2512 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2513 tbl)))
2515 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2516 (defun org-check-agenda-marker-table ()
2517 "Check of the current entry is on the marker list."
2518 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2520 (and (setq a (assoc file org-agenda-marker-table))
2521 (save-match-data
2522 (save-excursion
2523 (org-back-to-heading t)
2524 (member (point) (cdr a)))))))
2526 (defun org-check-for-org-mode ()
2527 "Make sure current buffer is in org-mode. Error if not."
2528 (or (org-mode-p)
2529 (error "Cannot execute org-mode agenda command on buffer in %s"
2530 major-mode)))
2532 (defun org-fit-agenda-window ()
2533 "Fit the window to the buffer size."
2534 (and (memq org-agenda-window-setup '(reorganize-frame))
2535 (fboundp 'fit-window-to-buffer)
2536 (org-fit-window-to-buffer
2538 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2539 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2541 ;;; Agenda prepare and finalize
2543 (defvar org-agenda-multi nil) ; dynamically scoped
2544 (defvar org-agenda-buffer-name "*Org Agenda*")
2545 (defvar org-pre-agenda-window-conf nil)
2546 (defvar org-agenda-columns-active nil)
2547 (defvar org-agenda-name nil)
2548 (defvar org-agenda-filter nil)
2549 (defvar org-agenda-filter-preset nil
2550 "A preset of the tags filter used for secondary agenda filtering.
2551 This must be a list of strings, each string must be a single tag preceeded
2552 by \"+\" or \"-\".
2553 This variable should not be set directly, but agenda custom commands can
2554 bind it in the options section.")
2556 (defun org-prepare-agenda (&optional name)
2557 (setq org-todo-keywords-for-agenda nil)
2558 (setq org-done-keywords-for-agenda nil)
2559 (setq org-drawers-for-agenda nil)
2560 (setq org-agenda-filter nil)
2561 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2562 (if org-agenda-multi
2563 (progn
2564 (setq buffer-read-only nil)
2565 (goto-char (point-max))
2566 (unless (or (bobp) org-agenda-compact-blocks)
2567 (insert "\n"
2568 (if (stringp org-agenda-block-separator)
2569 org-agenda-block-separator
2570 (make-string (window-width) org-agenda-block-separator))
2571 "\n"))
2572 (narrow-to-region (point) (point-max)))
2573 (org-agenda-reset-markers)
2574 (setq org-agenda-contributing-files nil)
2575 (setq org-agenda-columns-active nil)
2576 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2577 (setq org-todo-keywords-for-agenda
2578 (org-uniquify org-todo-keywords-for-agenda))
2579 (setq org-done-keywords-for-agenda
2580 (org-uniquify org-done-keywords-for-agenda))
2581 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
2582 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2583 (awin (get-buffer-window abuf)))
2584 (cond
2585 ((equal (current-buffer) abuf) nil)
2586 (awin (select-window awin))
2587 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2588 ((equal org-agenda-window-setup 'current-window)
2589 (switch-to-buffer abuf))
2590 ((equal org-agenda-window-setup 'other-window)
2591 (org-switch-to-buffer-other-window abuf))
2592 ((equal org-agenda-window-setup 'other-frame)
2593 (switch-to-buffer-other-frame abuf))
2594 ((equal org-agenda-window-setup 'reorganize-frame)
2595 (delete-other-windows)
2596 (org-switch-to-buffer-other-window abuf))))
2597 (setq buffer-read-only nil)
2598 (let ((inhibit-read-only t)) (erase-buffer))
2599 (org-agenda-mode)
2600 (and name (not org-agenda-name)
2601 (org-set-local 'org-agenda-name name)))
2602 (setq buffer-read-only nil))
2604 (defun org-finalize-agenda ()
2605 "Finishing touch for the agenda buffer, called just before displaying it."
2606 (unless org-agenda-multi
2607 (save-excursion
2608 (let ((inhibit-read-only t))
2609 (goto-char (point-min))
2610 (while (org-activate-bracket-links (point-max))
2611 (add-text-properties (match-beginning 0) (match-end 0)
2612 '(face org-link)))
2613 (org-agenda-align-tags)
2614 (unless org-agenda-with-colors
2615 (remove-text-properties (point-min) (point-max) '(face nil))))
2616 (if (and (boundp 'org-agenda-overriding-columns-format)
2617 org-agenda-overriding-columns-format)
2618 (org-set-local 'org-agenda-overriding-columns-format
2619 org-agenda-overriding-columns-format))
2620 (if (and (boundp 'org-agenda-view-columns-initially)
2621 org-agenda-view-columns-initially)
2622 (org-agenda-columns))
2623 (when org-agenda-fontify-priorities
2624 (org-agenda-fontify-priorities))
2625 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2626 (org-agenda-dim-blocked-tasks))
2627 (org-agenda-mark-clocking-task)
2628 (when org-agenda-entry-text-mode
2629 (org-agenda-entry-text-hide)
2630 (org-agenda-entry-text-show))
2631 (if (functionp 'org-habit-insert-consistency-graphs)
2632 (org-habit-insert-consistency-graphs))
2633 (run-hooks 'org-finalize-agenda-hook)
2634 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
2635 (when (get 'org-agenda-filter :preset-filter)
2636 (org-agenda-filter-apply org-agenda-filter))
2639 (defun org-agenda-mark-clocking-task ()
2640 "Mark the current clock entry in the agenda if it is present."
2641 (mapc (lambda (o)
2642 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2643 (org-delete-overlay o)))
2644 (org-overlays-in (point-min) (point-max)))
2645 (when (marker-buffer org-clock-hd-marker)
2646 (save-excursion
2647 (goto-char (point-min))
2648 (let (s ov)
2649 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2650 (goto-char s)
2651 (when (equal (org-get-at-bol 'org-hd-marker)
2652 org-clock-hd-marker)
2653 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2654 (org-overlay-put ov 'type 'org-agenda-clocking)
2655 (org-overlay-put ov 'face 'org-agenda-clocking)
2656 (org-overlay-put ov 'help-echo
2657 "The clock is running in this item")))))))
2659 (defun org-agenda-fontify-priorities ()
2660 "Make highest priority lines bold, and lowest italic."
2661 (interactive)
2662 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2663 (org-delete-overlay o)))
2664 (org-overlays-in (point-min) (point-max)))
2665 (save-excursion
2666 (let ((inhibit-read-only t)
2667 b e p ov h l)
2668 (goto-char (point-min))
2669 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2670 (setq h (or (get-char-property (point) 'org-highest-priority)
2671 org-highest-priority)
2672 l (or (get-char-property (point) 'org-lowest-priority)
2673 org-lowest-priority)
2674 p (string-to-char (match-string 1))
2675 b (match-beginning 0)
2676 e (if (eq org-agenda-fontify-priorities 'cookies)
2677 (match-end 0)
2678 (point-at-eol))
2679 ov (org-make-overlay b e))
2680 (org-overlay-put
2681 ov 'face
2682 (cond ((cdr (assoc p org-priority-faces)))
2683 ((and (listp org-agenda-fontify-priorities)
2684 (cdr (assoc p org-agenda-fontify-priorities))))
2685 ((equal p l) 'italic)
2686 ((equal p h) 'bold)))
2687 (org-overlay-put ov 'org-type 'org-priority)))))
2689 (defun org-agenda-dim-blocked-tasks ()
2690 "Dim currently blocked TODO's in the agenda display."
2691 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2692 (org-delete-overlay o)))
2693 (org-overlays-in (point-min) (point-max)))
2694 (save-excursion
2695 (let ((inhibit-read-only t)
2696 (org-depend-tag-blocked nil)
2697 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2698 org-blocked-by-checkboxes
2699 invis1 b e p ov h l)
2700 (goto-char (point-min))
2701 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2702 (and pos (goto-char (1+ pos))))
2703 (setq org-blocked-by-checkboxes nil invis1 invis)
2704 (let ((marker (org-get-at-bol 'org-hd-marker)))
2705 (when (and marker
2706 (not (with-current-buffer (marker-buffer marker)
2707 (save-excursion
2708 (goto-char marker)
2709 (if (org-entry-get nil "NOBLOCKING")
2710 t ;; Never block this entry
2711 (run-hook-with-args-until-failure
2712 'org-blocker-hook
2713 (list :type 'todo-state-change
2714 :position marker
2715 :from 'todo
2716 :to 'done)))))))
2717 (if org-blocked-by-checkboxes (setq invis1 nil))
2718 (setq b (if invis1
2719 (max (point-min) (1- (point-at-bol)))
2720 (point-at-bol))
2721 e (point-at-eol)
2722 ov (org-make-overlay b e))
2723 (if invis1
2724 (org-overlay-put ov 'invisible t)
2725 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2726 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2728 (defvar org-agenda-skip-function nil
2729 "Function to be called at each match during agenda construction.
2730 If this function returns nil, the current match should not be skipped.
2731 Otherwise, the function must return a position from where the search
2732 should be continued.
2733 This may also be a Lisp form, it will be evaluated.
2734 Never set this variable using `setq' or so, because then it will apply
2735 to all future agenda commands. Instead, bind it with `let' to scope
2736 it dynamically into the agenda-constructing command. A good way to set
2737 it is through options in org-agenda-custom-commands.")
2739 (defun org-agenda-skip ()
2740 "Throw to `:skip' in places that should be skipped.
2741 Also moves point to the end of the skipped region, so that search can
2742 continue from there."
2743 (let ((p (point-at-bol)) to fp)
2744 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2745 (get-text-property p :org-archived)
2746 (org-end-of-subtree t)
2747 (throw :skip t))
2748 (and org-agenda-skip-comment-trees
2749 (get-text-property p :org-comment)
2750 (org-end-of-subtree t)
2751 (throw :skip t))
2752 (if (equal (char-after p) ?#) (throw :skip t))
2753 (when (and (or (setq fp (functionp org-agenda-skip-function))
2754 (consp org-agenda-skip-function))
2755 (setq to (save-excursion
2756 (save-match-data
2757 (if fp
2758 (funcall org-agenda-skip-function)
2759 (eval org-agenda-skip-function))))))
2760 (goto-char to)
2761 (throw :skip t))))
2763 (defvar org-agenda-markers nil
2764 "List of all currently active markers created by `org-agenda'.")
2765 (defvar org-agenda-last-marker-time (org-float-time)
2766 "Creation time of the last agenda marker.")
2768 (defun org-agenda-new-marker (&optional pos)
2769 "Return a new agenda marker.
2770 Org-mode keeps a list of these markers and resets them when they are
2771 no longer in use."
2772 (let ((m (copy-marker (or pos (point)))))
2773 (setq org-agenda-last-marker-time (org-float-time))
2774 (push m org-agenda-markers)
2777 (defun org-agenda-reset-markers ()
2778 "Reset markers created by `org-agenda'."
2779 (while org-agenda-markers
2780 (move-marker (pop org-agenda-markers) nil)))
2782 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2783 "Save relative positions of markers in region."
2784 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2785 org-agenda-markers))
2787 ;;; Entry text mode
2789 (defun org-agenda-entry-text-show-here ()
2790 "Add some text from the entry as context to the current line."
2791 (let (m txt o)
2792 (setq m (org-get-at-bol 'org-hd-marker))
2793 (unless (marker-buffer m)
2794 (error "No marker points to an entry here"))
2795 (setq txt (concat "\n" (org-no-properties
2796 (org-agenda-get-some-entry-text
2797 m org-agenda-entry-text-maxlines " > "))))
2798 (when (string-match "\\S-" txt)
2799 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2800 (org-overlay-put o 'evaporate t)
2801 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2802 (org-overlay-put o 'after-string txt))))
2804 (defun org-agenda-entry-text-show ()
2805 "Add entry context for all agenda lines."
2806 (interactive)
2807 (save-excursion
2808 (goto-char (point-max))
2809 (beginning-of-line 1)
2810 (while (not (bobp))
2811 (when (org-get-at-bol 'org-hd-marker)
2812 (org-agenda-entry-text-show-here))
2813 (beginning-of-line 0))))
2815 (defun org-agenda-entry-text-hide ()
2816 "Remove any shown entry context."
2817 (delq nil
2818 (mapcar (lambda (o)
2819 (if (eq (org-overlay-get o 'org-overlay-type)
2820 'agenda-entry-content)
2821 (progn (org-delete-overlay o) t)))
2822 (org-overlays-in (point-min) (point-max)))))
2824 ;;; Agenda timeline
2826 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2828 (defun org-timeline (&optional include-all)
2829 "Show a time-sorted view of the entries in the current org file.
2830 Only entries with a time stamp of today or later will be listed. With
2831 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2832 under the current date.
2833 If the buffer contains an active region, only check the region for
2834 dates."
2835 (interactive "P")
2836 (require 'calendar)
2837 (org-compile-prefix-format 'timeline)
2838 (org-set-sorting-strategy 'timeline)
2839 (let* ((dopast t)
2840 (dotodo include-all)
2841 (doclosed org-agenda-show-log)
2842 (entry buffer-file-name)
2843 (date (calendar-current-date))
2844 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2845 (end (if (org-region-active-p) (region-end) (point-max)))
2846 (day-numbers (org-get-all-dates beg end 'no-ranges
2847 t doclosed ; always include today
2848 org-timeline-show-empty-dates))
2849 (org-deadline-warning-days 0)
2850 (org-agenda-only-exact-dates t)
2851 (today (time-to-days (current-time)))
2852 (past t)
2853 args
2854 s e rtn d emptyp wd)
2855 (setq org-agenda-redo-command
2856 (list 'progn
2857 (list 'org-switch-to-buffer-other-window (current-buffer))
2858 (list 'org-timeline (list 'quote include-all))))
2859 (if (not dopast)
2860 ;; Remove past dates from the list of dates.
2861 (setq day-numbers (delq nil (mapcar (lambda(x)
2862 (if (>= x today) x nil))
2863 day-numbers))))
2864 (org-prepare-agenda (concat "Timeline "
2865 (file-name-nondirectory buffer-file-name)))
2866 (if doclosed (push :closed args))
2867 (push :timestamp args)
2868 (push :deadline args)
2869 (push :scheduled args)
2870 (push :sexp args)
2871 (if dotodo (push :todo args))
2872 (insert "Timeline of file " entry "\n")
2873 (add-text-properties (point-min) (point)
2874 (list 'face 'org-agenda-structure))
2875 (org-agenda-mark-header-line (point-min))
2876 (while (setq d (pop day-numbers))
2877 (if (and (listp d) (eq (car d) :omitted))
2878 (progn
2879 (setq s (point))
2880 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2881 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2882 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2883 (if (and (>= d today)
2884 dopast
2885 past)
2886 (progn
2887 (setq past nil)
2888 (insert (make-string 79 ?-) "\n")))
2889 (setq date (calendar-gregorian-from-absolute d)
2890 wd (calendar-day-of-week date))
2891 (setq s (point))
2892 (setq rtn (and (not emptyp)
2893 (apply 'org-agenda-get-day-entries entry
2894 date args)))
2895 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2896 (progn
2897 (insert
2898 (if (stringp org-agenda-format-date)
2899 (format-time-string org-agenda-format-date
2900 (org-time-from-absolute date))
2901 (funcall org-agenda-format-date date))
2902 "\n")
2903 (put-text-property s (1- (point)) 'face
2904 (if (member wd org-agenda-weekend-days)
2905 'org-agenda-date-weekend
2906 'org-agenda-date))
2907 (put-text-property s (1- (point)) 'org-date-line t)
2908 (put-text-property s (1- (point)) 'org-agenda-date-header t)
2909 (if (equal d today)
2910 (put-text-property s (1- (point)) 'org-today t))
2911 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2912 (put-text-property s (1- (point)) 'day d)))))
2913 (goto-char (point-min))
2914 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2915 (point-min)))
2916 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2917 (org-finalize-agenda)
2918 (setq buffer-read-only t)))
2920 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2921 "Return a list of all relevant day numbers from BEG to END buffer positions.
2922 If NO-RANGES is non-nil, include only the start and end dates of a range,
2923 not every single day in the range. If FORCE-TODAY is non-nil, make
2924 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2925 inactive time stamps (those in square brackets) are included.
2926 When EMPTY is non-nil, also include days without any entries."
2927 (let ((re (concat
2928 (if pre-re pre-re "")
2929 (if inactive org-ts-regexp-both org-ts-regexp)))
2930 dates dates1 date day day1 day2 ts1 ts2)
2931 (if force-today
2932 (setq dates (list (time-to-days (current-time)))))
2933 (save-excursion
2934 (goto-char beg)
2935 (while (re-search-forward re end t)
2936 (setq day (time-to-days (org-time-string-to-time
2937 (substring (match-string 1) 0 10))))
2938 (or (memq day dates) (push day dates)))
2939 (unless no-ranges
2940 (goto-char beg)
2941 (while (re-search-forward org-tr-regexp end t)
2942 (setq ts1 (substring (match-string 1) 0 10)
2943 ts2 (substring (match-string 2) 0 10)
2944 day1 (time-to-days (org-time-string-to-time ts1))
2945 day2 (time-to-days (org-time-string-to-time ts2)))
2946 (while (< (setq day1 (1+ day1)) day2)
2947 (or (memq day1 dates) (push day1 dates)))))
2948 (setq dates (sort dates '<))
2949 (when empty
2950 (while (setq day (pop dates))
2951 (setq day2 (car dates))
2952 (push day dates1)
2953 (when (and day2 empty)
2954 (if (or (eq empty t)
2955 (and (numberp empty) (<= (- day2 day) empty)))
2956 (while (< (setq day (1+ day)) day2)
2957 (push (list day) dates1))
2958 (push (cons :omitted (- day2 day)) dates1))))
2959 (setq dates (nreverse dates1)))
2960 dates)))
2962 ;;; Agenda Daily/Weekly
2964 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2965 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2966 "Custom commands can set this variable in the options section.")
2967 (defvar org-agenda-last-arguments nil
2968 "The arguments of the previous call to org-agenda")
2969 (defvar org-starting-day nil) ; local variable in the agenda buffer
2970 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2971 (defvar org-include-all-loc nil) ; local variable
2972 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2974 ;;;###autoload
2975 (defun org-agenda-list (&optional include-all start-day ndays)
2976 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2977 The view will be for the current day or week, but from the overview buffer
2978 you will be able to go to other days/weeks.
2980 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2981 all unfinished TODO items will also be shown, before the agenda.
2982 This feature is considered obsolete, please use the TODO list or a block
2983 agenda instead.
2985 With a numeric prefix argument in an interactive call, the agenda will
2986 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2987 the number of days. NDAYS defaults to `org-agenda-ndays'.
2989 START-DAY defaults to TODAY, or to the most recent match for the weekday
2990 given in `org-agenda-start-on-weekday'."
2991 (interactive "P")
2992 (if (and (integerp include-all) (> include-all 0))
2993 (setq ndays include-all include-all nil))
2994 (setq ndays (or ndays org-agenda-ndays)
2995 start-day (or start-day org-agenda-start-day))
2996 (if org-agenda-overriding-arguments
2997 (setq include-all (car org-agenda-overriding-arguments)
2998 start-day (nth 1 org-agenda-overriding-arguments)
2999 ndays (nth 2 org-agenda-overriding-arguments)))
3000 (if (stringp start-day)
3001 ;; Convert to an absolute day number
3002 (setq start-day (time-to-days (org-read-date nil t start-day))))
3003 (setq org-agenda-last-arguments (list include-all start-day ndays))
3004 (org-compile-prefix-format 'agenda)
3005 (org-set-sorting-strategy 'agenda)
3006 (require 'calendar)
3007 (let* ((org-agenda-start-on-weekday
3008 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
3009 org-agenda-start-on-weekday nil))
3010 (thefiles (org-agenda-files nil 'ifmode))
3011 (files thefiles)
3012 (today (time-to-days
3013 (time-subtract (current-time)
3014 (list 0 (* 3600 org-extend-today-until) 0))))
3015 (sd (or start-day today))
3016 (start (if (or (null org-agenda-start-on-weekday)
3017 (< org-agenda-ndays 7))
3019 (let* ((nt (calendar-day-of-week
3020 (calendar-gregorian-from-absolute sd)))
3021 (n1 org-agenda-start-on-weekday)
3022 (d (- nt n1)))
3023 (- sd (+ (if (< d 0) 7 0) d)))))
3024 (day-numbers (list start))
3025 (day-cnt 0)
3026 (inhibit-redisplay (not debug-on-error))
3027 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3028 clocktable-start clocktable-end)
3029 (setq org-agenda-redo-command
3030 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3031 ;; Make the list of days
3032 (setq ndays (or ndays org-agenda-ndays)
3033 nd ndays)
3034 (while (> ndays 1)
3035 (push (1+ (car day-numbers)) day-numbers)
3036 (setq ndays (1- ndays)))
3037 (setq day-numbers (nreverse day-numbers))
3038 (setq clocktable-start (car day-numbers)
3039 clocktable-end (1+ (or (org-last day-numbers) 0)))
3040 (org-prepare-agenda "Day/Week")
3041 (org-set-local 'org-starting-day (car day-numbers))
3042 (org-set-local 'org-include-all-loc include-all)
3043 (org-set-local 'org-agenda-span
3044 (org-agenda-ndays-to-span nd))
3045 (when (and (or include-all org-agenda-include-all-todo)
3046 (member today day-numbers))
3047 (setq files thefiles
3048 rtnall nil)
3049 (while (setq file (pop files))
3050 (catch 'nextfile
3051 (org-check-agenda-file file)
3052 (setq date (calendar-gregorian-from-absolute today)
3053 rtn (org-agenda-get-day-entries
3054 file date :todo))
3055 (setq rtnall (append rtnall rtn))))
3056 (when rtnall
3057 (insert "All currently open TODO items:\n")
3058 (add-text-properties (point-min) (1- (point))
3059 (list 'face 'org-agenda-structure
3060 'short-heading "All TODO items"))
3061 (org-agenda-mark-header-line (point-min))
3062 (insert (org-finalize-agenda-entries rtnall) "\n")))
3063 (unless org-agenda-compact-blocks
3064 (let* ((d1 (car day-numbers))
3065 (d2 (org-last day-numbers))
3066 (w1 (org-days-to-iso-week d1))
3067 (w2 (org-days-to-iso-week d2)))
3068 (setq s (point))
3069 (if org-agenda-overriding-header
3070 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3071 nil 'face 'org-agenda-structure) "\n")
3072 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3073 "-agenda"
3074 (if (< (- d2 d1) 350)
3075 (if (= w1 w2)
3076 (format " (W%02d)" w1)
3077 (format " (W%02d-W%02d)" w1 w2))
3079 ":\n")))
3080 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3081 'org-date-line t))
3082 (org-agenda-mark-header-line s))
3083 (while (setq d (pop day-numbers))
3084 (setq date (calendar-gregorian-from-absolute d)
3085 wd (calendar-day-of-week date)
3086 s (point))
3087 (if (or (setq todayp (= d today))
3088 (and (not start-pos) (= d sd)))
3089 (setq start-pos (point))
3090 (if (and start-pos (not end-pos))
3091 (setq end-pos (point))))
3092 (setq files thefiles
3093 rtnall nil)
3094 (while (setq file (pop files))
3095 (catch 'nextfile
3096 (org-check-agenda-file file)
3097 (cond
3098 ((eq org-agenda-show-log 'only)
3099 (setq rtn (org-agenda-get-day-entries
3100 file date :closed)))
3101 (org-agenda-show-log
3102 (setq rtn (org-agenda-get-day-entries
3103 file date
3104 :deadline :scheduled :timestamp :sexp :closed)))
3106 (setq rtn (org-agenda-get-day-entries
3107 file date
3108 :deadline :scheduled :sexp :timestamp))))
3109 (setq rtnall (append rtnall rtn))))
3110 (if org-agenda-include-diary
3111 (let ((org-agenda-search-headline-for-time t))
3112 (require 'diary-lib)
3113 (setq rtn (org-get-entries-from-diary date))
3114 (setq rtnall (append rtnall rtn))))
3115 (if (or rtnall org-agenda-show-all-dates)
3116 (progn
3117 (setq day-cnt (1+ day-cnt))
3118 (insert
3119 (if (stringp org-agenda-format-date)
3120 (format-time-string org-agenda-format-date
3121 (org-time-from-absolute date))
3122 (funcall org-agenda-format-date date))
3123 "\n")
3124 (put-text-property s (1- (point)) 'face
3125 (if (member wd org-agenda-weekend-days)
3126 'org-agenda-date-weekend
3127 'org-agenda-date))
3128 (put-text-property s (1- (point)) 'org-date-line t)
3129 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3130 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3131 (when todayp
3132 (put-text-property s (1- (point)) 'org-today t)
3133 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
3134 (if rtnall (insert
3135 (org-finalize-agenda-entries
3136 (org-agenda-add-time-grid-maybe
3137 rtnall nd todayp))
3138 "\n"))
3139 (put-text-property s (1- (point)) 'day d)
3140 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3141 (when (and org-agenda-clockreport-mode clocktable-start)
3142 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3143 ;; the above line is to ensure the restricted range!
3144 (p org-agenda-clockreport-parameter-plist)
3145 tbl)
3146 (setq p (org-plist-delete p :block))
3147 (setq p (plist-put p :tstart clocktable-start))
3148 (setq p (plist-put p :tend clocktable-end))
3149 (setq p (plist-put p :scope 'agenda))
3150 (setq tbl (apply 'org-get-clocktable p))
3151 (insert tbl)))
3152 (goto-char (point-min))
3153 (or org-agenda-multi (org-fit-agenda-window))
3154 (unless (and (pos-visible-in-window-p (point-min))
3155 (pos-visible-in-window-p (point-max)))
3156 (goto-char (1- (point-max)))
3157 (recenter -1)
3158 (if (not (pos-visible-in-window-p (or start-pos 1)))
3159 (progn
3160 (goto-char (or start-pos 1))
3161 (recenter 1))))
3162 (goto-char (or start-pos 1))
3163 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3164 (org-finalize-agenda)
3165 (setq buffer-read-only t)
3166 (message "")))
3168 (defun org-agenda-ndays-to-span (n)
3169 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3171 ;;; Agenda word search
3173 (defvar org-agenda-search-history nil)
3174 (defvar org-todo-only nil)
3176 (defvar org-search-syntax-table nil
3177 "Special syntax table for org-mode search.
3178 In this table, we have single quotes not as word constituents, to
3179 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3181 (defun org-search-syntax-table ()
3182 (unless org-search-syntax-table
3183 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3184 (modify-syntax-entry ?' "." org-search-syntax-table)
3185 (modify-syntax-entry ?` "." org-search-syntax-table))
3186 org-search-syntax-table)
3188 ;;;###autoload
3189 (defun org-search-view (&optional todo-only string edit-at)
3190 "Show all entries that contain words or regular expressions.
3191 If the first character of the search string is an asterisks,
3192 search only the headlines.
3194 With optional prefix argument TODO-ONLY, only consider entries that are
3195 TODO entries. The argument STRING can be used to pass a default search
3196 string into this function. If EDIT-AT is non-nil, it means that the
3197 user should get a chance to edit this string, with cursor at position
3198 EDIT-AT.
3200 The search string is broken into \"words\" by splitting at whitespace.
3201 Depending on the variable `org-agenda-search-view-search-words-only'
3202 and on whether the first character in the search string is \"+\" or \"-\",
3203 The string is then interpreted either as a substring with variable amounts
3204 of whitespace, or as a list or individual words that should be matched.
3206 The default is a substring match, where each space in the search string
3207 can expand to an arbitrary amount of whitespace, including newlines.
3209 If matching individual words, these words are then interpreted as a
3210 boolean expression with logical AND. Words prefixed with a minus must
3211 not occur in the entry. Words without a prefix or prefixed with a plus
3212 must occur in the entry. Matching is case-insensitive and the words
3213 are enclosed by word delimiters.
3215 Words enclosed by curly braces are interpreted as regular expressions
3216 that must or must not match in the entry.
3218 If the search string starts with an asterisk, search only in headlines.
3219 If (possibly after the leading star) the search string starts with an
3220 exclamation mark, this also means to look at TODO entries only, an effect
3221 that can also be achieved with a prefix argument.
3223 This command searches the agenda files, and in addition the files listed
3224 in `org-agenda-text-search-extra-files'."
3225 (interactive "P")
3226 (org-compile-prefix-format 'search)
3227 (org-set-sorting-strategy 'search)
3228 (org-prepare-agenda "SEARCH")
3229 (let* ((props (list 'face nil
3230 'done-face 'org-agenda-done
3231 'org-not-done-regexp org-not-done-regexp
3232 'org-todo-regexp org-todo-regexp
3233 'org-complex-heading-regexp org-complex-heading-regexp
3234 'mouse-face 'highlight
3235 'help-echo (format "mouse-2 or RET jump to location")))
3236 regexp rtn rtnall files file pos
3237 marker category tags c neg re as-words
3238 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3239 (unless (and (not edit-at)
3240 (stringp string)
3241 (string-match "\\S-" string))
3242 (setq string (read-string "[+-]Word/{Regexp} ...: "
3243 (cond
3244 ((integerp edit-at) (cons string edit-at))
3245 (edit-at string))
3246 'org-agenda-search-history)))
3247 (org-set-local 'org-todo-only todo-only)
3248 (setq org-agenda-redo-command
3249 (list 'org-search-view (if todo-only t nil) string
3250 '(if current-prefix-arg 1 nil)))
3251 (setq org-agenda-query-string string)
3253 (if (equal (string-to-char string) ?*)
3254 (setq hdl-only t
3255 words (substring string 1))
3256 (setq words string))
3257 (when (equal (string-to-char words) ?!)
3258 (setq todo-only t
3259 words (substring words 1)))
3260 (if (or org-agenda-search-view-search-words-only
3261 (member (string-to-char string) '(?- ?+)))
3262 (setq as-words t))
3263 (setq words (org-split-string words))
3264 (if as-words
3265 (mapc (lambda (w)
3266 (setq c (string-to-char w))
3267 (if (equal c ?-)
3268 (setq neg t w (substring w 1))
3269 (if (equal c ?+)
3270 (setq neg nil w (substring w 1))
3271 (setq neg nil)))
3272 (if (string-match "\\`{.*}\\'" w)
3273 (setq re (substring w 1 -1))
3274 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
3275 (if neg (push re regexps-) (push re regexps+)))
3276 words)
3277 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3278 regexps+))
3279 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3280 (if (not regexps+)
3281 (setq regexp (concat "^" org-outline-regexp))
3282 (setq regexp (pop regexps+))
3283 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3284 regexp))))
3285 (setq files (org-agenda-files nil 'ifmode))
3286 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3287 (pop org-agenda-text-search-extra-files)
3288 (setq files (org-add-archive-files files)))
3289 (setq files (append files org-agenda-text-search-extra-files)
3290 rtnall nil)
3291 (while (setq file (pop files))
3292 (setq ee nil)
3293 (catch 'nextfile
3294 (org-check-agenda-file file)
3295 (setq buffer (if (file-exists-p file)
3296 (org-get-agenda-file-buffer file)
3297 (error "No such file %s" file)))
3298 (if (not buffer)
3299 ;; If file does not exist, make sure an error message is sent
3300 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3301 file))))
3302 (with-current-buffer buffer
3303 (with-syntax-table (org-search-syntax-table)
3304 (unless (org-mode-p)
3305 (error "Agenda file %s is not in `org-mode'" file))
3306 (let ((case-fold-search t))
3307 (save-excursion
3308 (save-restriction
3309 (if org-agenda-restrict
3310 (narrow-to-region org-agenda-restrict-begin
3311 org-agenda-restrict-end)
3312 (widen))
3313 (goto-char (point-min))
3314 (unless (or (org-on-heading-p)
3315 (outline-next-heading))
3316 (throw 'nextfile t))
3317 (goto-char (max (point-min) (1- (point))))
3318 (while (re-search-forward regexp nil t)
3319 (org-back-to-heading t)
3320 (skip-chars-forward "* ")
3321 (setq beg (point-at-bol)
3322 beg1 (point)
3323 end (progn (outline-next-heading) (point)))
3324 (catch :skip
3325 (goto-char beg)
3326 (org-agenda-skip)
3327 (setq str (buffer-substring-no-properties
3328 (point-at-bol)
3329 (if hdl-only (point-at-eol) end)))
3330 (mapc (lambda (wr) (when (string-match wr str)
3331 (goto-char (1- end))
3332 (throw :skip t)))
3333 regexps-)
3334 (mapc (lambda (wr) (unless (string-match wr str)
3335 (goto-char (1- end))
3336 (throw :skip t)))
3337 (if todo-only
3338 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3339 regexps+)
3340 regexps+))
3341 (goto-char beg)
3342 (setq marker (org-agenda-new-marker (point))
3343 category (org-get-category)
3344 tags (org-get-tags-at (point))
3345 txt (org-format-agenda-item
3347 (buffer-substring-no-properties
3348 beg1 (point-at-eol))
3349 category tags))
3350 (org-add-props txt props
3351 'org-marker marker 'org-hd-marker marker
3352 'org-todo-regexp org-todo-regexp
3353 'org-complex-heading-regexp org-complex-heading-regexp
3354 'priority 1000 'org-category category
3355 'type "search")
3356 (push txt ee)
3357 (goto-char (1- end))))))))))
3358 (setq rtn (nreverse ee))
3359 (setq rtnall (append rtnall rtn)))
3360 (if org-agenda-overriding-header
3361 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3362 nil 'face 'org-agenda-structure) "\n")
3363 (insert "Search words: ")
3364 (add-text-properties (point-min) (1- (point))
3365 (list 'face 'org-agenda-structure))
3366 (setq pos (point))
3367 (insert string "\n")
3368 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3369 (setq pos (point))
3370 (unless org-agenda-multi
3371 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3372 (add-text-properties pos (1- (point))
3373 (list 'face 'org-agenda-structure))))
3374 (org-agenda-mark-header-line (point-min))
3375 (when rtnall
3376 (insert (org-finalize-agenda-entries rtnall) "\n"))
3377 (goto-char (point-min))
3378 (or org-agenda-multi (org-fit-agenda-window))
3379 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3380 (org-finalize-agenda)
3381 (setq buffer-read-only t)))
3383 ;;; Agenda TODO list
3385 (defvar org-select-this-todo-keyword nil)
3386 (defvar org-last-arg nil)
3388 ;;;###autoload
3389 (defun org-todo-list (arg)
3390 "Show all TODO entries from all agenda file in a single list.
3391 The prefix arg can be used to select a specific TODO keyword and limit
3392 the list to these. When using \\[universal-argument], you will be prompted
3393 for a keyword. A numeric prefix directly selects the Nth keyword in
3394 `org-todo-keywords-1'."
3395 (interactive "P")
3396 (require 'calendar)
3397 (org-compile-prefix-format 'todo)
3398 (org-set-sorting-strategy 'todo)
3399 (org-prepare-agenda "TODO")
3400 (let* ((today (time-to-days (current-time)))
3401 (date (calendar-gregorian-from-absolute today))
3402 (kwds org-todo-keywords-for-agenda)
3403 (completion-ignore-case t)
3404 (org-select-this-todo-keyword
3405 (if (stringp arg) arg
3406 (and arg (integerp arg) (> arg 0)
3407 (nth (1- arg) kwds))))
3408 rtn rtnall files file pos)
3409 (when (equal arg '(4))
3410 (setq org-select-this-todo-keyword
3411 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3412 (mapcar 'list kwds) nil nil)))
3413 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3414 (org-set-local 'org-last-arg arg)
3415 (setq org-agenda-redo-command
3416 '(org-todo-list (or current-prefix-arg org-last-arg)))
3417 (setq files (org-agenda-files nil 'ifmode)
3418 rtnall nil)
3419 (while (setq file (pop files))
3420 (catch 'nextfile
3421 (org-check-agenda-file file)
3422 (setq rtn (org-agenda-get-day-entries file date :todo))
3423 (setq rtnall (append rtnall rtn))))
3424 (if org-agenda-overriding-header
3425 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3426 nil 'face 'org-agenda-structure) "\n")
3427 (insert "Global list of TODO items of type: ")
3428 (add-text-properties (point-min) (1- (point))
3429 (list 'face 'org-agenda-structure
3430 'short-heading
3431 (concat "ToDo: "
3432 (or org-select-this-todo-keyword "ALL"))))
3433 (org-agenda-mark-header-line (point-min))
3434 (setq pos (point))
3435 (insert (or org-select-this-todo-keyword "ALL") "\n")
3436 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3437 (setq pos (point))
3438 (unless org-agenda-multi
3439 (insert "Available with `N r': (0)ALL")
3440 (let ((n 0) s)
3441 (mapc (lambda (x)
3442 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3443 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3444 (insert "\n "))
3445 (insert " " s))
3446 kwds))
3447 (insert "\n"))
3448 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3449 (org-agenda-mark-header-line (point-min))
3450 (when rtnall
3451 (insert (org-finalize-agenda-entries rtnall) "\n"))
3452 (goto-char (point-min))
3453 (or org-agenda-multi (org-fit-agenda-window))
3454 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3455 (org-finalize-agenda)
3456 (setq buffer-read-only t)))
3458 ;;; Agenda tags match
3460 ;;;###autoload
3461 (defun org-tags-view (&optional todo-only match)
3462 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3463 The prefix arg TODO-ONLY limits the search to TODO entries."
3464 (interactive "P")
3465 (org-compile-prefix-format 'tags)
3466 (org-set-sorting-strategy 'tags)
3467 (let* ((org-tags-match-list-sublevels
3468 ;?????? (if todo-only t org-tags-match-list-sublevels))
3469 org-tags-match-list-sublevels)
3470 (completion-ignore-case t)
3471 rtn rtnall files file pos matcher
3472 buffer)
3473 (setq matcher (org-make-tags-matcher match)
3474 match (car matcher) matcher (cdr matcher))
3475 (org-prepare-agenda (concat "TAGS " match))
3476 (setq org-agenda-query-string match)
3477 (setq org-agenda-redo-command
3478 (list 'org-tags-view (list 'quote todo-only)
3479 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3480 (setq files (org-agenda-files nil 'ifmode)
3481 rtnall nil)
3482 (while (setq file (pop files))
3483 (catch 'nextfile
3484 (org-check-agenda-file file)
3485 (setq buffer (if (file-exists-p file)
3486 (org-get-agenda-file-buffer file)
3487 (error "No such file %s" file)))
3488 (if (not buffer)
3489 ;; If file does not exist, error message to agenda
3490 (setq rtn (list
3491 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3492 rtnall (append rtnall rtn))
3493 (with-current-buffer buffer
3494 (unless (org-mode-p)
3495 (error "Agenda file %s is not in `org-mode'" file))
3496 (save-excursion
3497 (save-restriction
3498 (if org-agenda-restrict
3499 (narrow-to-region org-agenda-restrict-begin
3500 org-agenda-restrict-end)
3501 (widen))
3502 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3503 (setq rtnall (append rtnall rtn))))))))
3504 (if org-agenda-overriding-header
3505 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3506 nil 'face 'org-agenda-structure) "\n")
3507 (insert "Headlines with TAGS match: ")
3508 (add-text-properties (point-min) (1- (point))
3509 (list 'face 'org-agenda-structure
3510 'short-heading
3511 (concat "Match: " match)))
3512 (setq pos (point))
3513 (insert match "\n")
3514 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3515 (setq pos (point))
3516 (unless org-agenda-multi
3517 (insert "Press `C-u r' to search again with new search string\n"))
3518 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3519 (org-agenda-mark-header-line (point-min))
3520 (when rtnall
3521 (insert (org-finalize-agenda-entries rtnall) "\n"))
3522 (goto-char (point-min))
3523 (or org-agenda-multi (org-fit-agenda-window))
3524 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3525 (org-finalize-agenda)
3526 (setq buffer-read-only t)))
3528 ;;; Agenda Finding stuck projects
3530 (defvar org-agenda-skip-regexp nil
3531 "Regular expression used in skipping subtrees for the agenda.
3532 This is basically a temporary global variable that can be set and then
3533 used by user-defined selections using `org-agenda-skip-function'.")
3535 (defvar org-agenda-overriding-header nil
3536 "When this is set during todo and tags searches, will replace header.
3537 This variable should not be set directly, but custom commands can bind it
3538 in the options section.")
3540 (defun org-agenda-skip-entry-when-regexp-matches ()
3541 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3542 If yes, it returns the end position of this entry, causing agenda commands
3543 to skip the entry but continuing the search in the subtree. This is a
3544 function that can be put into `org-agenda-skip-function' for the duration
3545 of a command."
3546 (let ((end (save-excursion (org-end-of-subtree t)))
3547 skip)
3548 (save-excursion
3549 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3550 (and skip end)))
3552 (defun org-agenda-skip-subtree-when-regexp-matches ()
3553 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3554 If yes, it returns the end position of this tree, causing agenda commands
3555 to skip this subtree. This is a function that can be put into
3556 `org-agenda-skip-function' for the duration of a command."
3557 (let ((end (save-excursion (org-end-of-subtree t)))
3558 skip)
3559 (save-excursion
3560 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3561 (and skip end)))
3563 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3564 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3565 If yes, it returns the end position of the current entry (NOT the tree),
3566 causing agenda commands to skip the entry but continuing the search in
3567 the subtree. This is a function that can be put into
3568 `org-agenda-skip-function' for the duration of a command. An important
3569 use of this function is for the stuck project list."
3570 (let ((end (save-excursion (org-end-of-subtree t)))
3571 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3572 skip)
3573 (save-excursion
3574 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3575 (and skip entry-end)))
3577 (defun org-agenda-skip-entry-if (&rest conditions)
3578 "Skip entry if any of CONDITIONS is true.
3579 See `org-agenda-skip-if' for details."
3580 (org-agenda-skip-if nil conditions))
3582 (defun org-agenda-skip-subtree-if (&rest conditions)
3583 "Skip entry if any of CONDITIONS is true.
3584 See `org-agenda-skip-if' for details."
3585 (org-agenda-skip-if t conditions))
3587 (defun org-agenda-skip-if (subtree conditions)
3588 "Checks current entity for CONDITIONS.
3589 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3590 the entry, i.e. the text before the next heading is checked.
3592 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3593 from different tests. Valid conditions are:
3595 scheduled Check if there is a scheduled cookie
3596 notscheduled Check if there is no scheduled cookie
3597 deadline Check if there is a deadline
3598 notdeadline Check if there is no deadline
3599 timestamp Check if there is a timestamp (also deadline or scheduled)
3600 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3601 regexp Check if regexp matches
3602 notregexp Check if regexp does not match.
3604 The regexp is taken from the conditions list, it must come right after
3605 the `regexp' or `notregexp' element.
3607 If any of these conditions is met, this function returns the end point of
3608 the entity, causing the search to continue from there. This is a function
3609 that can be put into `org-agenda-skip-function' for the duration of a command."
3610 (let (beg end m)
3611 (org-back-to-heading t)
3612 (setq beg (point)
3613 end (if subtree
3614 (progn (org-end-of-subtree t) (point))
3615 (progn (outline-next-heading) (1- (point)))))
3616 (goto-char beg)
3617 (and
3619 (and (memq 'scheduled conditions)
3620 (re-search-forward org-scheduled-time-regexp end t))
3621 (and (memq 'notscheduled conditions)
3622 (not (re-search-forward org-scheduled-time-regexp end t)))
3623 (and (memq 'deadline conditions)
3624 (re-search-forward org-deadline-time-regexp end t))
3625 (and (memq 'notdeadline conditions)
3626 (not (re-search-forward org-deadline-time-regexp end t)))
3627 (and (memq 'timestamp conditions)
3628 (re-search-forward org-ts-regexp end t))
3629 (and (memq 'nottimestamp conditions)
3630 (not (re-search-forward org-ts-regexp end t)))
3631 (and (setq m (memq 'regexp conditions))
3632 (stringp (nth 1 m))
3633 (re-search-forward (nth 1 m) end t))
3634 (and (setq m (memq 'notregexp conditions))
3635 (stringp (nth 1 m))
3636 (not (re-search-forward (nth 1 m) end t))))
3637 end)))
3639 ;;;###autoload
3640 (defun org-agenda-list-stuck-projects (&rest ignore)
3641 "Create agenda view for projects that are stuck.
3642 Stuck projects are project that have no next actions. For the definitions
3643 of what a project is and how to check if it stuck, customize the variable
3644 `org-stuck-projects'.
3645 MATCH is being ignored."
3646 (interactive)
3647 (let* ((org-agenda-skip-function
3648 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3649 ;; We could have used org-agenda-skip-if here.
3650 (org-agenda-overriding-header
3651 (or org-agenda-overriding-header "List of stuck projects: "))
3652 (matcher (nth 0 org-stuck-projects))
3653 (todo (nth 1 org-stuck-projects))
3654 (todo-wds (if (member "*" todo)
3655 (progn
3656 (org-prepare-agenda-buffers (org-agenda-files
3657 nil 'ifmode))
3658 (org-delete-all
3659 org-done-keywords-for-agenda
3660 (copy-sequence org-todo-keywords-for-agenda)))
3661 todo))
3662 (todo-re (concat "^\\*+[ \t]+\\("
3663 (mapconcat 'identity todo-wds "\\|")
3664 "\\)\\>"))
3665 (tags (nth 2 org-stuck-projects))
3666 (tags-re (if (member "*" tags)
3667 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3668 (if tags
3669 (concat "^\\*+ .*:\\("
3670 (mapconcat 'identity tags "\\|")
3671 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3672 (gen-re (nth 3 org-stuck-projects))
3673 (re-list
3674 (delq nil
3675 (list
3676 (if todo todo-re)
3677 (if tags tags-re)
3678 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3679 gen-re)))))
3680 (setq org-agenda-skip-regexp
3681 (if re-list
3682 (mapconcat 'identity re-list "\\|")
3683 (error "No information how to identify unstuck projects")))
3684 (org-tags-view nil matcher)
3685 (with-current-buffer org-agenda-buffer-name
3686 (setq org-agenda-redo-command
3687 '(org-agenda-list-stuck-projects
3688 (or current-prefix-arg org-last-arg))))))
3690 ;;; Diary integration
3692 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3693 (defvar list-diary-entries-hook)
3695 (defun org-get-entries-from-diary (date)
3696 "Get the (Emacs Calendar) diary entries for DATE."
3697 (require 'diary-lib)
3698 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3699 (fancy-diary-buffer diary-fancy-buffer)
3700 (diary-display-hook '(fancy-diary-display))
3701 (diary-display-function 'fancy-diary-display)
3702 (pop-up-frames nil)
3703 (list-diary-entries-hook
3704 (cons 'org-diary-default-entry list-diary-entries-hook))
3705 (diary-file-name-prefix-function nil) ; turn this feature off
3706 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3707 entries
3708 (org-disable-agenda-to-diary t))
3709 (save-excursion
3710 (save-window-excursion
3711 (funcall (if (fboundp 'diary-list-entries)
3712 'diary-list-entries 'list-diary-entries)
3713 date 1)))
3714 (if (not (get-buffer diary-fancy-buffer))
3715 (setq entries nil)
3716 (with-current-buffer diary-fancy-buffer
3717 (setq buffer-read-only nil)
3718 (if (zerop (buffer-size))
3719 ;; No entries
3720 (setq entries nil)
3721 ;; Omit the date and other unnecessary stuff
3722 (org-agenda-cleanup-fancy-diary)
3723 ;; Add prefix to each line and extend the text properties
3724 (if (zerop (buffer-size))
3725 (setq entries nil)
3726 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3727 (set-buffer-modified-p nil)
3728 (kill-buffer diary-fancy-buffer)))
3729 (when entries
3730 (setq entries (org-split-string entries "\n"))
3731 (setq entries
3732 (mapcar
3733 (lambda (x)
3734 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3735 ;; Extend the text properties to the beginning of the line
3736 (org-add-props x (text-properties-at (1- (length x)) x)
3737 'type "diary" 'date date))
3738 entries)))))
3740 (defvar org-agenda-cleanup-fancy-diary-hook nil
3741 "Hook run when the fancy diary buffer is cleaned up.")
3743 (defun org-agenda-cleanup-fancy-diary ()
3744 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3745 This gets rid of the date, the underline under the date, and
3746 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3747 date. It also removes lines that contain only whitespace."
3748 (goto-char (point-min))
3749 (if (looking-at ".*?:[ \t]*")
3750 (progn
3751 (replace-match "")
3752 (re-search-forward "\n=+$" nil t)
3753 (replace-match "")
3754 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3755 (re-search-forward "\n=+$" nil t)
3756 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3757 (goto-char (point-min))
3758 (while (re-search-forward "^ +\n" nil t)
3759 (replace-match ""))
3760 (goto-char (point-min))
3761 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3762 (replace-match ""))
3763 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3765 ;; Make sure entries from the diary have the right text properties.
3766 (eval-after-load "diary-lib"
3767 '(if (boundp 'diary-modify-entry-list-string-function)
3768 ;; We can rely on the hook, nothing to do
3770 ;; Hook not available, must use advice to make this work
3771 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3772 "Make the position visible."
3773 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3774 (stringp string)
3775 buffer-file-name)
3776 (setq string (org-modify-diary-entry-string string))))))
3778 (defun org-modify-diary-entry-string (string)
3779 "Add text properties to string, allowing org-mode to act on it."
3780 (org-add-props string nil
3781 'mouse-face 'highlight
3782 'help-echo (if buffer-file-name
3783 (format "mouse-2 or RET jump to diary file %s"
3784 (abbreviate-file-name buffer-file-name))
3786 'org-agenda-diary-link t
3787 'org-marker (org-agenda-new-marker (point-at-bol))))
3789 (defun org-diary-default-entry ()
3790 "Add a dummy entry to the diary.
3791 Needed to avoid empty dates which mess up holiday display."
3792 ;; Catch the error if dealing with the new add-to-diary-alist
3793 (when org-disable-agenda-to-diary
3794 (condition-case nil
3795 (org-add-to-diary-list original-date "Org-mode dummy" "")
3796 (error
3797 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3799 (defun org-add-to-diary-list (&rest args)
3800 (if (fboundp 'diary-add-to-list)
3801 (apply 'diary-add-to-list args)
3802 (apply 'add-to-diary-list args)))
3804 ;;;###autoload
3805 (defun org-diary (&rest args)
3806 "Return diary information from org-files.
3807 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3808 It accesses org files and extracts information from those files to be
3809 listed in the diary. The function accepts arguments specifying what
3810 items should be listed. The following arguments are allowed:
3812 :timestamp List the headlines of items containing a date stamp or
3813 date range matching the selected date. Deadlines will
3814 also be listed, on the expiration day.
3816 :sexp List entries resulting from diary-like sexps.
3818 :deadline List any deadlines past due, or due within
3819 `org-deadline-warning-days'. The listing occurs only
3820 in the diary for *today*, not at any other date. If
3821 an entry is marked DONE, it is no longer listed.
3823 :scheduled List all items which are scheduled for the given date.
3824 The diary for *today* also contains items which were
3825 scheduled earlier and are not yet marked DONE.
3827 :todo List all TODO items from the org-file. This may be a
3828 long list - so this is not turned on by default.
3829 Like deadlines, these entries only show up in the
3830 diary for *today*, not at any other date.
3832 The call in the diary file should look like this:
3834 &%%(org-diary) ~/path/to/some/orgfile.org
3836 Use a separate line for each org file to check. Or, if you omit the file name,
3837 all files listed in `org-agenda-files' will be checked automatically:
3839 &%%(org-diary)
3841 If you don't give any arguments (as in the example above), the default
3842 arguments (:deadline :scheduled :timestamp :sexp) are used.
3843 So the example above may also be written as
3845 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3847 The function expects the lisp variables `entry' and `date' to be provided
3848 by the caller, because this is how the calendar works. Don't use this
3849 function from a program - use `org-agenda-get-day-entries' instead."
3850 (when (> (- (org-float-time)
3851 org-agenda-last-marker-time)
3853 (org-agenda-reset-markers))
3854 (org-compile-prefix-format 'agenda)
3855 (org-set-sorting-strategy 'agenda)
3856 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3857 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3858 (list entry)
3859 (org-agenda-files t)))
3860 file rtn results)
3861 (org-prepare-agenda-buffers files)
3862 ;; If this is called during org-agenda, don't return any entries to
3863 ;; the calendar. Org Agenda will list these entries itself.
3864 (if org-disable-agenda-to-diary (setq files nil))
3865 (while (setq file (pop files))
3866 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3867 (setq results (append results rtn)))
3868 (if results
3869 (concat (org-finalize-agenda-entries results) "\n"))))
3871 ;;; Agenda entry finders
3873 (defun org-agenda-get-day-entries (file date &rest args)
3874 "Does the work for `org-diary' and `org-agenda'.
3875 FILE is the path to a file to be checked for entries. DATE is date like
3876 the one returned by `calendar-current-date'. ARGS are symbols indicating
3877 which kind of entries should be extracted. For details about these, see
3878 the documentation of `org-diary'."
3879 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3880 (let* ((org-startup-folded nil)
3881 (org-startup-align-all-tables nil)
3882 (buffer (if (file-exists-p file)
3883 (org-get-agenda-file-buffer file)
3884 (error "No such file %s" file)))
3885 arg results rtn deadline-results)
3886 (if (not buffer)
3887 ;; If file does not exist, make sure an error message ends up in diary
3888 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3889 (with-current-buffer buffer
3890 (unless (org-mode-p)
3891 (error "Agenda file %s is not in `org-mode'" file))
3892 (let ((case-fold-search nil))
3893 (save-excursion
3894 (save-restriction
3895 (if org-agenda-restrict
3896 (narrow-to-region org-agenda-restrict-begin
3897 org-agenda-restrict-end)
3898 (widen))
3899 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3900 (while (setq arg (pop args))
3901 (cond
3902 ((and (eq arg :todo)
3903 (equal date (calendar-current-date)))
3904 (setq rtn (org-agenda-get-todos))
3905 (setq results (append results rtn)))
3906 ((eq arg :timestamp)
3907 (setq rtn (org-agenda-get-blocks))
3908 (setq results (append results rtn))
3909 (setq rtn (org-agenda-get-timestamps))
3910 (setq results (append results rtn)))
3911 ((eq arg :sexp)
3912 (setq rtn (org-agenda-get-sexps))
3913 (setq results (append results rtn)))
3914 ((eq arg :scheduled)
3915 (setq rtn (org-agenda-get-scheduled deadline-results))
3916 (setq results (append results rtn)))
3917 ((eq arg :closed)
3918 (setq rtn (org-agenda-get-progress))
3919 (setq results (append results rtn)))
3920 ((eq arg :deadline)
3921 (setq rtn (org-agenda-get-deadlines))
3922 (setq deadline-results (copy-sequence rtn))
3923 (setq results (append results rtn))))))))
3924 results))))
3926 (defun org-agenda-get-todos ()
3927 "Return the TODO information for agenda display."
3928 (let* ((props (list 'face nil
3929 'done-face 'org-agenda-done
3930 'org-not-done-regexp org-not-done-regexp
3931 'org-todo-regexp org-todo-regexp
3932 'org-complex-heading-regexp org-complex-heading-regexp
3933 'mouse-face 'highlight
3934 'help-echo
3935 (format "mouse-2 or RET jump to org file %s"
3936 (abbreviate-file-name buffer-file-name))))
3937 (regexp (concat "^\\*+[ \t]+\\("
3938 (if org-select-this-todo-keyword
3939 (if (equal org-select-this-todo-keyword "*")
3940 org-todo-regexp
3941 (concat "\\<\\("
3942 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3943 "\\)\\>"))
3944 org-not-done-regexp)
3945 "[^\n\r]*\\)"))
3946 marker priority category tags todo-state
3947 ee txt beg end)
3948 (goto-char (point-min))
3949 (while (re-search-forward regexp nil t)
3950 (catch :skip
3951 (save-match-data
3952 (beginning-of-line)
3953 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3954 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3955 (goto-char (1+ beg))
3956 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3957 (throw :skip nil)))
3958 (goto-char beg)
3959 (org-agenda-skip)
3960 (goto-char (match-beginning 1))
3961 (setq marker (org-agenda-new-marker (match-beginning 0))
3962 category (org-get-category)
3963 txt (match-string 1)
3964 tags (org-get-tags-at (point))
3965 txt (org-format-agenda-item "" txt category tags)
3966 priority (1+ (org-get-priority txt))
3967 todo-state (org-get-todo-state))
3968 (org-add-props txt props
3969 'org-marker marker 'org-hd-marker marker
3970 'priority priority 'org-category category
3971 'type "todo" 'todo-state todo-state)
3972 (push txt ee)
3973 (if org-agenda-todo-list-sublevels
3974 (goto-char (match-end 1))
3975 (org-end-of-subtree 'invisible))))
3976 (nreverse ee)))
3978 ;;;###autoload
3979 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3980 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3981 (when (or org-agenda-todo-ignore-with-date
3982 org-agenda-todo-ignore-scheduled
3983 org-agenda-todo-ignore-deadlines)
3984 (setq end (or end (save-excursion (outline-next-heading) (point))))
3985 (save-excursion
3986 (or (and org-agenda-todo-ignore-with-date
3987 (re-search-forward org-ts-regexp end t))
3988 (and org-agenda-todo-ignore-scheduled
3989 (re-search-forward org-scheduled-time-regexp end t))
3990 (and org-agenda-todo-ignore-deadlines
3991 (re-search-forward org-deadline-time-regexp end t)
3992 (org-deadline-close (match-string 1)))))))
3994 (defconst org-agenda-no-heading-message
3995 "No heading for this item in buffer or region.")
3997 (defun org-agenda-get-timestamps ()
3998 "Return the date stamp information for agenda display."
3999 (let* ((props (list 'face nil
4000 'org-not-done-regexp org-not-done-regexp
4001 'org-todo-regexp org-todo-regexp
4002 'org-complex-heading-regexp org-complex-heading-regexp
4003 'mouse-face 'highlight
4004 'help-echo
4005 (format "mouse-2 or RET jump to org file %s"
4006 (abbreviate-file-name buffer-file-name))))
4007 (d1 (calendar-absolute-from-gregorian date))
4008 (remove-re
4009 (concat
4010 (regexp-quote
4011 (format-time-string
4012 "<%Y-%m-%d"
4013 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4014 ".*?>"))
4015 (regexp
4016 (concat
4017 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4018 (regexp-quote
4019 (substring
4020 (format-time-string
4021 (car org-time-stamp-formats)
4022 (apply 'encode-time ; DATE bound by calendar
4023 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4024 1 11))
4025 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4026 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4027 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4028 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4029 todo-state end-of-match)
4030 (goto-char (point-min))
4031 (while (setq end-of-match (re-search-forward regexp nil t))
4032 (setq b0 (match-beginning 0)
4033 b3 (match-beginning 3) e3 (match-end 3))
4034 (catch :skip
4035 (and (org-at-date-range-p) (throw :skip nil))
4036 (org-agenda-skip)
4037 (if (and (match-end 1)
4038 (not (= d1 (org-time-string-to-absolute
4039 (match-string 1) d1 nil
4040 org-agenda-repeating-timestamp-show-all))))
4041 (throw :skip nil))
4042 (if (and e3
4043 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4044 (throw :skip nil))
4045 (setq tmp (buffer-substring (max (point-min)
4046 (- b0 org-ds-keyword-length))
4048 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4049 inactivep (= (char-after b0) ?\[)
4050 deadlinep (string-match org-deadline-regexp tmp)
4051 scheduledp (string-match org-scheduled-regexp tmp)
4052 closedp (and org-agenda-include-inactive-timestamps
4053 (string-match org-closed-string tmp))
4054 clockp (and org-agenda-include-inactive-timestamps
4055 (or (string-match org-clock-string tmp)
4056 (string-match "]-+\\'" tmp)))
4057 todo-state (org-get-todo-state)
4058 donep (member todo-state org-done-keywords))
4059 (if (or scheduledp deadlinep closedp clockp
4060 (and donep org-agenda-skip-timestamp-if-done))
4061 (throw :skip t))
4062 (if (string-match ">" timestr)
4063 ;; substring should only run to end of time stamp
4064 (setq timestr (substring timestr 0 (match-end 0))))
4065 (setq marker (org-agenda-new-marker b0)
4066 category (org-get-category b0))
4067 (save-excursion
4068 (if (not (re-search-backward "^\\*+ " nil t))
4069 (setq txt org-agenda-no-heading-message)
4070 (goto-char (match-beginning 0))
4071 (setq hdmarker (org-agenda-new-marker)
4072 tags (org-get-tags-at))
4073 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4074 (setq head (match-string 1))
4075 (setq txt (org-format-agenda-item
4076 (if inactivep "[" nil)
4077 head category tags timestr nil
4078 remove-re)))
4079 (setq priority (org-get-priority txt))
4080 (org-add-props txt props
4081 'org-marker marker 'org-hd-marker hdmarker)
4082 (org-add-props txt nil 'priority priority
4083 'org-category category 'date date
4084 'todo-state todo-state
4085 'type "timestamp")
4086 (push txt ee))
4087 (if org-agenda-skip-additional-timestamps-same-entry
4088 (outline-next-heading)
4089 (goto-char end-of-match))))
4090 (nreverse ee)))
4092 (defun org-agenda-get-sexps ()
4093 "Return the sexp information for agenda display."
4094 (require 'diary-lib)
4095 (let* ((props (list 'face nil
4096 'mouse-face 'highlight
4097 'help-echo
4098 (format "mouse-2 or RET jump to org file %s"
4099 (abbreviate-file-name buffer-file-name))))
4100 (regexp "^&?%%(")
4101 marker category ee txt tags entry result beg b sexp sexp-entry
4102 todo-state)
4103 (goto-char (point-min))
4104 (while (re-search-forward regexp nil t)
4105 (catch :skip
4106 (org-agenda-skip)
4107 (setq beg (match-beginning 0))
4108 (goto-char (1- (match-end 0)))
4109 (setq b (point))
4110 (forward-sexp 1)
4111 (setq sexp (buffer-substring b (point)))
4112 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4113 (org-trim (match-string 1))
4114 ""))
4115 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4116 (when result
4117 (setq marker (org-agenda-new-marker beg)
4118 category (org-get-category beg)
4119 todo-state (org-get-todo-state))
4121 (if (string-match "\\S-" result)
4122 (setq txt result)
4123 (setq txt "SEXP entry returned empty string"))
4125 (setq txt (org-format-agenda-item
4126 "" txt category tags 'time))
4127 (org-add-props txt props 'org-marker marker)
4128 (org-add-props txt nil
4129 'org-category category 'date date 'todo-state todo-state
4130 'type "sexp")
4131 (push txt ee))))
4132 (nreverse ee)))
4134 (defalias 'org-get-closed 'org-agenda-get-progress)
4135 (defun org-agenda-get-progress ()
4136 "Return the logged TODO entries for agenda display."
4137 (let* ((props (list 'mouse-face 'highlight
4138 'org-not-done-regexp org-not-done-regexp
4139 'org-todo-regexp org-todo-regexp
4140 'org-complex-heading-regexp org-complex-heading-regexp
4141 'help-echo
4142 (format "mouse-2 or RET jump to org file %s"
4143 (abbreviate-file-name buffer-file-name))))
4144 (items (if (consp org-agenda-show-log)
4145 org-agenda-show-log
4146 org-agenda-log-mode-items))
4147 (parts
4148 (delq nil
4149 (list
4150 (if (memq 'closed items) (concat "\\<" org-closed-string))
4151 (if (memq 'clock items) (concat "\\<" org-clock-string))
4152 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4153 (parts-re (if parts (mapconcat 'identity parts "\\|")
4154 (error "`org-agenda-log-mode-items' is empty")))
4155 (regexp (concat
4156 "\\(" parts-re "\\)"
4157 " *\\["
4158 (regexp-quote
4159 (substring
4160 (format-time-string
4161 (car org-time-stamp-formats)
4162 (apply 'encode-time ; DATE bound by calendar
4163 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4164 1 11))))
4165 (org-agenda-search-headline-for-time nil)
4166 marker hdmarker priority category tags closedp statep clockp state
4167 ee txt extra timestr rest clocked)
4168 (goto-char (point-min))
4169 (while (re-search-forward regexp nil t)
4170 (catch :skip
4171 (org-agenda-skip)
4172 (setq marker (org-agenda-new-marker (match-beginning 0))
4173 closedp (equal (match-string 1) org-closed-string)
4174 statep (equal (string-to-char (match-string 1)) ?-)
4175 clockp (not (or closedp statep))
4176 state (and statep (match-string 2))
4177 category (org-get-category (match-beginning 0))
4178 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4180 (when (string-match "\\]" timestr)
4181 ;; substring should only run to end of time stamp
4182 (setq rest (substring timestr (match-end 0))
4183 timestr (substring timestr 0 (match-end 0)))
4184 (if (and (not closedp) (not statep)
4185 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4186 (progn (setq timestr (concat (substring timestr 0 -1)
4187 "-" (match-string 1 rest) "]"))
4188 (setq clocked (match-string 2 rest)))
4189 (setq clocked "-")))
4190 (save-excursion
4191 (cond
4192 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4193 (statep
4194 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4195 (setq extra (match-string 1))))
4196 (clockp
4197 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4198 (setq extra (match-string 1))))
4199 (t (setq extra nil)))
4200 (if (not (re-search-backward "^\\*+ " nil t))
4201 (setq txt org-agenda-no-heading-message)
4202 (goto-char (match-beginning 0))
4203 (setq hdmarker (org-agenda-new-marker)
4204 tags (org-get-tags-at))
4205 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4206 (setq txt (match-string 1))
4207 (when extra
4208 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4209 (setq txt (concat (substring txt 0 (match-beginning 1))
4210 " - " extra " " (match-string 2 txt)))
4211 (setq txt (concat txt " - " extra))))
4212 (setq txt (org-format-agenda-item
4213 (cond
4214 (closedp "Closed: ")
4215 (statep (concat "State: (" state ")"))
4216 (t (concat "Clocked: (" clocked ")")))
4217 txt category tags timestr)))
4218 (setq priority 100000)
4219 (org-add-props txt props
4220 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4221 'priority priority 'org-category category
4222 'type "closed" 'date date
4223 'undone-face 'org-warning 'done-face 'org-agenda-done)
4224 (push txt ee))
4225 (goto-char (point-at-eol))))
4226 (nreverse ee)))
4228 (defun org-agenda-get-deadlines ()
4229 "Return the deadline information for agenda display."
4230 (let* ((props (list 'mouse-face 'highlight
4231 'org-not-done-regexp org-not-done-regexp
4232 'org-todo-regexp org-todo-regexp
4233 'org-complex-heading-regexp org-complex-heading-regexp
4234 'help-echo
4235 (format "mouse-2 or RET jump to org file %s"
4236 (abbreviate-file-name buffer-file-name))))
4237 (regexp org-deadline-time-regexp)
4238 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4239 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4240 d2 diff dfrac wdays pos pos1 category tags
4241 ee txt head face s todo-state upcomingp donep timestr)
4242 (goto-char (point-min))
4243 (while (re-search-forward regexp nil t)
4244 (catch :skip
4245 (org-agenda-skip)
4246 (setq s (match-string 1)
4247 txt nil
4248 pos (1- (match-beginning 1))
4249 d2 (org-time-string-to-absolute
4250 (match-string 1) d1 'past
4251 org-agenda-repeating-timestamp-show-all)
4252 diff (- d2 d1)
4253 wdays (org-get-wdays s)
4254 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4255 upcomingp (and todayp (> diff 0)))
4256 ;; When to show a deadline in the calendar:
4257 ;; If the expiration is within wdays warning time.
4258 ;; Past-due deadlines are only shown on the current date
4259 (if (and (or (and (<= diff wdays)
4260 (and todayp (not org-agenda-only-exact-dates)))
4261 (= diff 0)))
4262 (save-excursion
4263 (setq todo-state (org-get-todo-state))
4264 (setq donep (member todo-state org-done-keywords))
4265 (if (and donep
4266 (or org-agenda-skip-deadline-if-done
4267 (not (= diff 0))))
4268 (setq txt nil)
4269 (setq category (org-get-category))
4270 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4271 (setq txt org-agenda-no-heading-message)
4272 (goto-char (match-end 0))
4273 (setq pos1 (match-beginning 0))
4274 (setq tags (org-get-tags-at pos1))
4275 (setq head (buffer-substring-no-properties
4276 (point)
4277 (progn (skip-chars-forward "^\r\n")
4278 (point))))
4279 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4280 (setq timestr
4281 (concat (substring s (match-beginning 1)) " "))
4282 (setq timestr 'time))
4283 (setq txt (org-format-agenda-item
4284 (if (= diff 0)
4285 (car org-agenda-deadline-leaders)
4286 (if (functionp
4287 (nth 1 org-agenda-deadline-leaders))
4288 (funcall
4289 (nth 1 org-agenda-deadline-leaders)
4290 diff date)
4291 (format (nth 1 org-agenda-deadline-leaders)
4292 diff)))
4293 head category tags
4294 (if (not (= diff 0)) nil timestr)))))
4295 (when txt
4296 (setq face (org-agenda-deadline-face dfrac wdays))
4297 (org-add-props txt props
4298 'org-marker (org-agenda-new-marker pos)
4299 'org-hd-marker (org-agenda-new-marker pos1)
4300 'priority (+ (- diff)
4301 (org-get-priority txt))
4302 'org-category category
4303 'todo-state todo-state
4304 'type (if upcomingp "upcoming-deadline" "deadline")
4305 'date (if upcomingp date d2)
4306 'face (if donep 'org-agenda-done face)
4307 'undone-face face 'done-face 'org-agenda-done)
4308 (push txt ee))))))
4309 (nreverse ee)))
4311 (defun org-agenda-deadline-face (fraction &optional wdays)
4312 "Return the face to displaying a deadline item.
4313 FRACTION is what fraction of the head-warning time has passed."
4314 (if (equal wdays 0) (setq fraction 1.))
4315 (let ((faces org-agenda-deadline-faces) f)
4316 (catch 'exit
4317 (while (setq f (pop faces))
4318 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4320 (defun org-agenda-get-scheduled (&optional deadline-results)
4321 "Return the scheduled information for agenda display."
4322 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4323 'org-todo-regexp org-todo-regexp
4324 'org-complex-heading-regexp org-complex-heading-regexp
4325 'done-face 'org-agenda-done
4326 'mouse-face 'highlight
4327 'help-echo
4328 (format "mouse-2 or RET jump to org file %s"
4329 (abbreviate-file-name buffer-file-name))))
4330 (regexp org-scheduled-time-regexp)
4331 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4332 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4334 (deadline-position-alist
4335 (mapcar (lambda (a) (and (setq mm (get-text-property
4336 0 'org-hd-marker a))
4337 (cons (marker-position mm) a)))
4338 deadline-results))
4339 d2 diff pos pos1 category tags donep
4340 ee txt head pastschedp todo-state face timestr s habitp)
4341 (goto-char (point-min))
4342 (while (re-search-forward regexp nil t)
4343 (catch :skip
4344 (org-agenda-skip)
4345 (setq s (match-string 1)
4346 txt nil
4347 pos (1- (match-beginning 1))
4348 d2 (org-time-string-to-absolute
4349 (match-string 1) d1 'past
4350 org-agenda-repeating-timestamp-show-all)
4351 diff (- d2 d1))
4352 (setq pastschedp (and todayp (< diff 0)))
4353 ;; When to show a scheduled item in the calendar:
4354 ;; If it is on or past the date.
4355 (when (or (and (< diff 0)
4356 (< (abs diff) org-scheduled-past-days)
4357 (and todayp (not org-agenda-only-exact-dates)))
4358 (= diff 0))
4359 (save-excursion
4360 (setq todo-state (org-get-todo-state))
4361 (setq donep (member todo-state org-done-keywords))
4362 (setq habitp (and (functionp 'org-is-habit-p)
4363 (org-is-habit-p)))
4364 (if (and donep
4365 (or habitp org-agenda-skip-scheduled-if-done
4366 (not (= diff 0))))
4367 (setq txt nil)
4368 (setq category (org-get-category))
4369 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4370 (setq txt org-agenda-no-heading-message)
4371 (goto-char (match-end 0))
4372 (setq pos1 (match-beginning 0))
4373 (if habitp
4374 (if (or (not org-habit-show-habits)
4375 (and (not todayp)
4376 org-habit-show-habits-only-for-today))
4377 (throw :skip nil))
4378 (if (and
4379 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4380 (and org-agenda-skip-scheduled-if-deadline-is-shown
4381 pastschedp))
4382 (setq mm (assoc pos1 deadline-position-alist)))
4383 (throw :skip nil)))
4384 (setq tags (org-get-tags-at))
4385 (setq head (buffer-substring-no-properties
4386 (point)
4387 (progn (skip-chars-forward "^\r\n") (point))))
4388 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4389 (setq timestr
4390 (concat (substring s (match-beginning 1)) " "))
4391 (setq timestr 'time))
4392 (setq txt (org-format-agenda-item
4393 (if (= diff 0)
4394 (car org-agenda-scheduled-leaders)
4395 (format (nth 1 org-agenda-scheduled-leaders)
4396 (- 1 diff)))
4397 head category tags
4398 (if (not (= diff 0)) nil timestr)
4399 nil nil habitp))))
4400 (when txt
4401 (setq face
4402 (cond
4403 ((and (not habitp) pastschedp)
4404 'org-scheduled-previously)
4405 (todayp 'org-scheduled-today)
4406 (t 'org-scheduled))
4407 habitp (and habitp (org-habit-parse-todo)))
4408 (org-add-props txt props
4409 'undone-face face
4410 'face (if donep 'org-agenda-done face)
4411 'org-marker (org-agenda-new-marker pos)
4412 'org-hd-marker (org-agenda-new-marker pos1)
4413 'type (if pastschedp "past-scheduled" "scheduled")
4414 'date (if pastschedp d2 date)
4415 'priority (if habitp
4416 (org-habit-get-priority habitp)
4417 (+ 94 (- 5 diff) (org-get-priority txt)))
4418 'org-category category
4419 'org-habit-p habitp
4420 'todo-state todo-state)
4421 (push txt ee))))))
4422 (nreverse ee)))
4424 (defun org-agenda-get-blocks ()
4425 "Return the date-range information for agenda display."
4426 (let* ((props (list 'face nil
4427 'org-not-done-regexp org-not-done-regexp
4428 'org-todo-regexp org-todo-regexp
4429 'org-complex-heading-regexp org-complex-heading-regexp
4430 'mouse-face 'highlight
4431 'help-echo
4432 (format "mouse-2 or RET jump to org file %s"
4433 (abbreviate-file-name buffer-file-name))))
4434 (regexp org-tr-regexp)
4435 (d0 (calendar-absolute-from-gregorian date))
4436 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4437 head donep)
4438 (goto-char (point-min))
4439 (while (re-search-forward regexp nil t)
4440 (catch :skip
4441 (org-agenda-skip)
4442 (setq pos (point))
4443 (setq timestr (match-string 0)
4444 s1 (match-string 1)
4445 s2 (match-string 2)
4446 d1 (time-to-days (org-time-string-to-time s1))
4447 d2 (time-to-days (org-time-string-to-time s2)))
4448 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4449 ;; Only allow days between the limits, because the normal
4450 ;; date stamps will catch the limits.
4451 (save-excursion
4452 (setq todo-state (org-get-todo-state))
4453 (setq donep (member todo-state org-done-keywords))
4454 (if (and donep org-agenda-skip-timestamp-if-done)
4455 (throw :skip t))
4456 (setq marker (org-agenda-new-marker (point)))
4457 (setq category (org-get-category))
4458 (if (not (re-search-backward "^\\*+ " nil t))
4459 (setq txt org-agenda-no-heading-message)
4460 (goto-char (match-beginning 0))
4461 (setq hdmarker (org-agenda-new-marker (point)))
4462 (setq tags (org-get-tags-at))
4463 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4464 (setq head (match-string 1))
4465 (setq txt (org-format-agenda-item
4466 (format
4467 (nth (if (= d1 d2) 0 1)
4468 org-agenda-timerange-leaders)
4469 (1+ (- d0 d1)) (1+ (- d2 d1)))
4470 head category tags
4471 (if (= d0 d1) timestr))))
4472 (org-add-props txt props
4473 'org-marker marker 'org-hd-marker hdmarker
4474 'type "block" 'date date
4475 'todo-state todo-state
4476 'priority (org-get-priority txt) 'org-category category)
4477 (push txt ee)))
4478 (goto-char pos)))
4479 ;; Sort the entries by expiration date.
4480 (nreverse ee)))
4482 ;;; Agenda presentation and sorting
4484 (defvar org-prefix-has-time nil
4485 "A flag, set by `org-compile-prefix-format'.
4486 The flag is set if the currently compiled format contains a `%t'.")
4487 (defvar org-prefix-has-tag nil
4488 "A flag, set by `org-compile-prefix-format'.
4489 The flag is set if the currently compiled format contains a `%T'.")
4490 (defvar org-prefix-has-effort nil
4491 "A flag, set by `org-compile-prefix-format'.
4492 The flag is set if the currently compiled format contains a `%e'.")
4493 (defvar org-prefix-category-length nil
4494 "Used by `org-compile-prefix-format' to remember the category field widh.")
4495 (defvar org-prefix-category-max-length nil
4496 "Used by `org-compile-prefix-format' to remember the category field widh.")
4498 (defun org-format-agenda-item (extra txt &optional category tags dotime
4499 noprefix remove-re habitp)
4500 "Format TXT to be inserted into the agenda buffer.
4501 In particular, it adds the prefix and corresponding text properties. EXTRA
4502 must be a string and replaces the `%s' specifier in the prefix format.
4503 CATEGORY (string, symbol or nil) may be used to overrule the default
4504 category taken from local variable or file name. It will replace the `%c'
4505 specifier in the format. DOTIME, when non-nil, indicates that a
4506 time-of-day should be extracted from TXT for sorting of this entry, and for
4507 the `%t' specifier in the format. When DOTIME is a string, this string is
4508 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4509 only the correctly processes TXT should be returned - this is used by
4510 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4511 Any match of REMOVE-RE will be removed from TXT."
4512 (save-match-data
4513 ;; Diary entries sometimes have extra whitespace at the beginning
4514 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4515 (when org-agenda-show-inherited-tags
4516 ;; Fix the tags part in txt
4517 (setq txt (org-agenda-add-inherited-tags txt tags)))
4518 (let* ((category (or category
4519 org-category
4520 (if buffer-file-name
4521 (file-name-sans-extension
4522 (file-name-nondirectory buffer-file-name))
4523 "")))
4524 ;; time, tag, effort are needed for the eval of the prefix format
4525 (tag (if tags (nth (1- (length tags)) tags) ""))
4526 time effort neffort
4527 (ts (if dotime (concat
4528 (if (stringp dotime) dotime "")
4529 (and org-agenda-search-headline-for-time txt))))
4530 (time-of-day (and dotime (org-get-time-of-day ts)))
4531 stamp plain s0 s1 s2 t1 t2 rtn srp l
4532 duration thecategory)
4533 (and (org-mode-p) buffer-file-name
4534 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4535 (when (and dotime time-of-day)
4536 ;; Extract starting and ending time and move them to prefix
4537 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4538 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4539 (setq s0 (match-string 0 ts)
4540 srp (and stamp (match-end 3))
4541 s1 (match-string (if plain 1 2) ts)
4542 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4544 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4545 ;; them, we might want to remove them there to avoid duplication.
4546 ;; The user can turn this off with a variable.
4547 (if (and org-prefix-has-time
4548 org-agenda-remove-times-when-in-prefix (or stamp plain)
4549 (string-match (concat (regexp-quote s0) " *") txt)
4550 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4551 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4552 (= (match-beginning 0) 0)
4554 (setq txt (replace-match "" nil nil txt))))
4555 ;; Normalize the time(s) to 24 hour
4556 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4557 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4558 ;; Compute the duration
4559 (when s1
4560 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4561 (string-to-number (substring s1 3)))
4562 t2 (cond
4563 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4564 (string-to-number (substring s2 3))))
4565 (org-agenda-default-appointment-duration
4566 (+ t1 org-agenda-default-appointment-duration))
4567 (t nil)))
4568 (setq duration (if t2 (- t2 t1)))))
4570 (when (and s1 (not s2) org-agenda-default-appointment-duration
4571 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4572 (let ((m (+ (string-to-number (match-string 2 s1))
4573 (* 60 (string-to-number (match-string 1 s1)))
4574 org-agenda-default-appointment-duration))
4576 (setq h (/ m 60) m (- m (* h 60)))
4577 (setq s2 (format "%02d:%02d" h m))))
4579 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4580 txt)
4581 ;; Tags are in the string
4582 (if (or (eq org-agenda-remove-tags t)
4583 (and org-agenda-remove-tags
4584 org-prefix-has-tag))
4585 (setq txt (replace-match "" t t txt))
4586 (setq txt (replace-match
4587 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4588 (match-string 2 txt))
4589 t t txt))))
4590 (when (org-mode-p)
4591 (setq effort
4592 (condition-case nil
4593 (org-get-effort
4594 (or (get-text-property 0 'org-hd-marker txt)
4595 (get-text-property 0 'org-marker txt)))
4596 (error nil)))
4597 (when effort
4598 (setq neffort (org-hh:mm-string-to-minutes effort)
4599 effort (setq effort (concat "[" effort "]" )))))
4601 (when remove-re
4602 (while (string-match remove-re txt)
4603 (setq txt (replace-match "" t t txt))))
4605 ;; Create the final string
4606 (if noprefix
4607 (setq rtn txt)
4608 ;; Prepare the variables needed in the eval of the compiled format
4609 (setq time (cond (s2 (concat s1 "-" s2))
4610 (s1 (concat s1 "......"))
4611 (t ""))
4612 extra (or (and (not habitp) extra) "")
4613 category (if (symbolp category) (symbol-name category) category)
4614 thecategory (copy-sequence category))
4615 (if (string-match org-bracket-link-regexp category)
4616 (progn
4617 (setq l (if (match-end 3)
4618 (- (match-end 3) (match-beginning 3))
4619 (- (match-end 1) (match-beginning 1))))
4620 (when (< l (or org-prefix-category-length 0))
4621 (setq category (copy-sequence category))
4622 (org-add-props category nil
4623 'extra-space (make-string
4624 (- org-prefix-category-length l 1) ?\ ))))
4625 (if (and org-prefix-category-max-length
4626 (>= (length category) org-prefix-category-max-length))
4627 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
4628 ;; Evaluate the compiled format
4629 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4631 ;; And finally add the text properties
4632 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
4633 (org-add-props rtn nil
4634 'org-category (if thecategory (downcase thecategory) category)
4635 'tags (mapcar 'org-downcase-keep-props tags)
4636 'org-highest-priority org-highest-priority
4637 'org-lowest-priority org-lowest-priority
4638 'prefix-length (- (length rtn) (length txt))
4639 'time-of-day time-of-day
4640 'duration duration
4641 'effort effort
4642 'effort-minutes neffort
4643 'txt txt
4644 'time time
4645 'extra extra
4646 'dotime dotime))))
4648 (defun org-agenda-add-inherited-tags (txt tags)
4649 "Remove tags string from TXT, and add complete list of tags.
4650 The new list includes inherited tags. If any inherited tags are present,
4651 a double colon separates inherited tags from local tags."
4652 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4653 (setq txt (substring txt 0 (match-beginning 0))))
4654 (when tags
4655 (let ((have-i (get-text-property 0 'inherited (car tags)))
4657 (setq txt (concat txt " :"
4658 (mapconcat
4659 (lambda (x)
4660 (setq i (get-text-property 0 'inherited x))
4661 (if (and have-i (not i))
4662 (progn
4663 (setq have-i nil)
4664 (concat ":" x))
4666 tags ":")
4667 (if have-i "::" ":")))))
4668 txt)
4670 (defun org-downcase-keep-props (s)
4671 (let ((props (text-properties-at 0 s)))
4672 (setq s (downcase s))
4673 (add-text-properties 0 (length s) props s)
4676 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4677 (defvar org-agenda-sorting-strategy-selected nil)
4679 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4680 (catch 'exit
4681 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4682 ((and todayp (member 'today (car org-agenda-time-grid))))
4683 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4684 ((member 'weekly (car org-agenda-time-grid)))
4685 (t (throw 'exit list)))
4686 (let* ((have (delq nil (mapcar
4687 (lambda (x) (get-text-property 1 'time-of-day x))
4688 list)))
4689 (string (nth 1 org-agenda-time-grid))
4690 (gridtimes (nth 2 org-agenda-time-grid))
4691 (req (car org-agenda-time-grid))
4692 (remove (member 'remove-match req))
4693 new time)
4694 (if (and (member 'require-timed req) (not have))
4695 ;; don't show empty grid
4696 (throw 'exit list))
4697 (while (setq time (pop gridtimes))
4698 (unless (and remove (member time have))
4699 (setq time (int-to-string time))
4700 (push (org-format-agenda-item
4701 nil string "" nil
4702 (concat (substring time 0 -2) ":" (substring time -2)))
4703 new)
4704 (put-text-property
4705 1 (length (car new)) 'face 'org-time-grid (car new))))
4706 (if (member 'time-up org-agenda-sorting-strategy-selected)
4707 (append new list)
4708 (append list new)))))
4710 (defun org-compile-prefix-format (key)
4711 "Compile the prefix format into a Lisp form that can be evaluated.
4712 The resulting form is returned and stored in the variable
4713 `org-prefix-format-compiled'."
4714 (setq org-prefix-has-time nil org-prefix-has-tag nil
4715 org-prefix-category-length nil org-prefix-has-effort nil)
4716 (let ((s (cond
4717 ((stringp org-agenda-prefix-format)
4718 org-agenda-prefix-format)
4719 ((assq key org-agenda-prefix-format)
4720 (cdr (assq key org-agenda-prefix-format)))
4721 (t " %-12:c%?-12t% s")))
4722 (start 0)
4723 varform vars var e c f opt)
4724 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4725 s start)
4726 (setq var (cdr (assoc (match-string 4 s)
4727 '(("c" . category) ("t" . time) ("s" . extra)
4728 ("T" . tag) ("e" . effort))))
4729 c (or (match-string 3 s) "")
4730 opt (match-beginning 1)
4731 start (1+ (match-beginning 0)))
4732 (if (equal var 'time) (setq org-prefix-has-time t))
4733 (if (equal var 'tag) (setq org-prefix-has-tag t))
4734 (if (equal var 'effort) (setq org-prefix-has-effort t))
4735 (setq f (concat "%" (match-string 2 s) "s"))
4736 (when (equal var 'category)
4737 (setq org-prefix-category-length
4738 (floor (abs (string-to-number (match-string 2 s)))))
4739 (setq org-prefix-category-max-length
4740 (let ((x (match-string 2 s)))
4741 (save-match-data
4742 (if (string-match "\\.[0-9]+" x)
4743 (string-to-number (substring (match-string 0 x) 1)))))))
4744 (if opt
4745 (setq varform
4746 `(if (equal "" ,var)
4748 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4749 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
4750 (setq s (replace-match "%s" t nil s))
4751 (push varform vars))
4752 (setq vars (nreverse vars))
4753 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4755 (defun org-set-sorting-strategy (key)
4756 (if (symbolp (car org-agenda-sorting-strategy))
4757 ;; the old format
4758 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4759 (setq org-agenda-sorting-strategy-selected
4760 (or (cdr (assq key org-agenda-sorting-strategy))
4761 (cdr (assq 'agenda org-agenda-sorting-strategy))
4762 '(time-up category-keep priority-down)))))
4764 (defun org-get-time-of-day (s &optional string mod24)
4765 "Check string S for a time of day.
4766 If found, return it as a military time number between 0 and 2400.
4767 If not found, return nil.
4768 The optional STRING argument forces conversion into a 5 character wide string
4769 HH:MM."
4770 (save-match-data
4771 (when
4772 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4773 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4774 (let* ((h (string-to-number (match-string 1 s)))
4775 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4776 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4777 (am-p (equal ampm "am"))
4778 (h1 (cond ((not ampm) h)
4779 ((= h 12) (if am-p 0 12))
4780 (t (+ h (if am-p 0 12)))))
4781 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4782 (mod h1 24) h1))
4783 (t0 (+ (* 100 h2) m))
4784 (t1 (concat (if (>= h1 24) "+" " ")
4785 (if (< t0 100) "0" "")
4786 (if (< t0 10) "0" "")
4787 (int-to-string t0))))
4788 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4790 (defun org-finalize-agenda-entries (list &optional nosort)
4791 "Sort and concatenate the agenda items."
4792 (setq list (mapcar 'org-agenda-highlight-todo list))
4793 (if nosort
4794 list
4795 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4797 (defun org-agenda-highlight-todo (x)
4798 (let ((org-done-keywords org-done-keywords-for-agenda)
4799 re pl)
4800 (if (eq x 'line)
4801 (save-excursion
4802 (beginning-of-line 1)
4803 (setq re (org-get-at-bol 'org-todo-regexp))
4804 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
4805 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4806 (add-text-properties (match-beginning 0) (match-end 1)
4807 (list 'face (org-get-todo-face 1)))
4808 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4809 (delete-region (match-beginning 1) (1- (match-end 0)))
4810 (goto-char (match-beginning 1))
4811 (insert (format org-agenda-todo-keyword-format s)))))
4812 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4813 pl (get-text-property 0 'prefix-length x))
4814 (when (and re
4815 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4816 x (or pl 0)) pl))
4817 (add-text-properties
4818 (or (match-end 1) (match-end 0)) (match-end 0)
4819 (list 'face (org-get-todo-face (match-string 2 x)))
4821 (setq x (concat (substring x 0 (match-end 1))
4822 (format org-agenda-todo-keyword-format
4823 (match-string 2 x))
4824 (org-add-props " " (text-properties-at 0 x))
4825 (substring x (match-end 3)))))
4826 x)))
4828 (defsubst org-cmp-priority (a b)
4829 "Compare the priorities of string A and B."
4830 (let ((pa (or (get-text-property 1 'priority a) 0))
4831 (pb (or (get-text-property 1 'priority b) 0)))
4832 (cond ((> pa pb) +1)
4833 ((< pa pb) -1)
4834 (t nil))))
4836 (defsubst org-cmp-effort (a b)
4837 "Compare the priorities of string A and B."
4838 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4839 (ea (or (get-text-property 1 'effort-minutes a) def))
4840 (eb (or (get-text-property 1 'effort-minutes b) def)))
4841 (cond ((> ea eb) +1)
4842 ((< ea eb) -1)
4843 (t nil))))
4845 (defsubst org-cmp-category (a b)
4846 "Compare the string values of categories of strings A and B."
4847 (let ((ca (or (get-text-property 1 'org-category a) ""))
4848 (cb (or (get-text-property 1 'org-category b) "")))
4849 (cond ((string-lessp ca cb) -1)
4850 ((string-lessp cb ca) +1)
4851 (t nil))))
4853 (defsubst org-cmp-todo-state (a b)
4854 "Compare the todo states of strings A and B."
4855 (let* ((ma (or (get-text-property 1 'org-marker a)
4856 (get-text-property 1 'org-hd-marker a)))
4857 (mb (or (get-text-property 1 'org-marker b)
4858 (get-text-property 1 'org-hd-marker b)))
4859 (fa (and ma (marker-buffer ma)))
4860 (fb (and mb (marker-buffer mb)))
4861 (todo-kwds
4862 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4863 (and fb (with-current-buffer fb org-todo-keywords-1))))
4864 (ta (or (get-text-property 1 'todo-state a) ""))
4865 (tb (or (get-text-property 1 'todo-state b) ""))
4866 (la (- (length (member ta todo-kwds))))
4867 (lb (- (length (member tb todo-kwds))))
4868 (donepa (member ta org-done-keywords-for-agenda))
4869 (donepb (member tb org-done-keywords-for-agenda)))
4870 (cond ((and donepa (not donepb)) -1)
4871 ((and (not donepa) donepb) +1)
4872 ((< la lb) -1)
4873 ((< lb la) +1)
4874 (t nil))))
4876 (defsubst org-cmp-tag (a b)
4877 "Compare the string values of the first tags of A and B."
4878 (let ((ta (car (last (get-text-property 1 'tags a))))
4879 (tb (car (last (get-text-property 1 'tags b)))))
4880 (cond ((not ta) +1)
4881 ((not tb) -1)
4882 ((string-lessp ta tb) -1)
4883 ((string-lessp tb ta) +1)
4884 (t nil))))
4886 (defsubst org-cmp-time (a b)
4887 "Compare the time-of-day values of strings A and B."
4888 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4889 (ta (or (get-text-property 1 'time-of-day a) def))
4890 (tb (or (get-text-property 1 'time-of-day b) def)))
4891 (cond ((< ta tb) -1)
4892 ((< tb ta) +1)
4893 (t nil))))
4895 (defsubst org-cmp-habit-p (a b)
4896 "Compare the todo states of strings A and B."
4897 (let ((ha (get-text-property 1 'org-habit-p a))
4898 (hb (get-text-property 1 'org-habit-p b)))
4899 (cond ((and ha (not hb)) -1)
4900 ((and (not ha) hb) +1)
4901 (t nil))))
4903 (defun org-entries-lessp (a b)
4904 "Predicate for sorting agenda entries."
4905 ;; The following variables will be used when the form is evaluated.
4906 ;; So even though the compiler complains, keep them.
4907 (let* ((time-up (org-cmp-time a b))
4908 (time-down (if time-up (- time-up) nil))
4909 (priority-up (org-cmp-priority a b))
4910 (priority-down (if priority-up (- priority-up) nil))
4911 (effort-up (org-cmp-effort a b))
4912 (effort-down (if effort-up (- effort-up) nil))
4913 (category-up (org-cmp-category a b))
4914 (category-down (if category-up (- category-up) nil))
4915 (category-keep (if category-up +1 nil))
4916 (tag-up (org-cmp-tag a b))
4917 (tag-down (if tag-up (- tag-up) nil))
4918 (todo-state-up (org-cmp-todo-state a b))
4919 (todo-state-down (if todo-state-up (- todo-state-up) nil))
4920 (habit-up (org-cmp-habit-p a b))
4921 (habit-down (if habit-up (- habit-up) nil))
4922 user-defined-up user-defined-down)
4923 (if (and org-agenda-cmp-user-defined
4924 (functionp org-agenda-cmp-user-defined))
4925 (setq user-defined-up
4926 (funcall org-agenda-cmp-user-defined a b)
4927 user-defined-down (if user-defined-up (- user-defined-up) nil)))
4928 (cdr (assoc
4929 (eval (cons 'or org-agenda-sorting-strategy-selected))
4930 '((-1 . t) (1 . nil) (nil . nil))))))
4932 ;;; Agenda restriction lock
4934 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4935 "Overlay to mark the headline to which agenda commands are restricted.")
4936 (org-overlay-put org-agenda-restriction-lock-overlay
4937 'face 'org-agenda-restriction-lock)
4938 (org-overlay-put org-agenda-restriction-lock-overlay
4939 'help-echo "Agendas are currently limited to this subtree.")
4940 (org-detach-overlay org-agenda-restriction-lock-overlay)
4942 (defun org-agenda-set-restriction-lock (&optional type)
4943 "Set restriction lock for agenda, to current subtree or file.
4944 Restriction will be the file if TYPE is `file', or if type is the
4945 universal prefix '(4), or if the cursor is before the first headline
4946 in the file. Otherwise, restriction will be to the current subtree."
4947 (interactive "P")
4948 (and (equal type '(4)) (setq type 'file))
4949 (setq type (cond
4950 (type type)
4951 ((org-at-heading-p) 'subtree)
4952 ((condition-case nil (org-back-to-heading t) (error nil))
4953 'subtree)
4954 (t 'file)))
4955 (if (eq type 'subtree)
4956 (progn
4957 (setq org-agenda-restrict t)
4958 (setq org-agenda-overriding-restriction 'subtree)
4959 (put 'org-agenda-files 'org-restrict
4960 (list (buffer-file-name (buffer-base-buffer))))
4961 (org-back-to-heading t)
4962 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4963 (move-marker org-agenda-restrict-begin (point))
4964 (move-marker org-agenda-restrict-end
4965 (save-excursion (org-end-of-subtree t)))
4966 (message "Locking agenda restriction to subtree"))
4967 (put 'org-agenda-files 'org-restrict
4968 (list (buffer-file-name (buffer-base-buffer))))
4969 (setq org-agenda-restrict nil)
4970 (setq org-agenda-overriding-restriction 'file)
4971 (move-marker org-agenda-restrict-begin nil)
4972 (move-marker org-agenda-restrict-end nil)
4973 (message "Locking agenda restriction to file"))
4974 (setq current-prefix-arg nil)
4975 (org-agenda-maybe-redo))
4977 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4978 "Remove the agenda restriction lock."
4979 (interactive "P")
4980 (org-detach-overlay org-agenda-restriction-lock-overlay)
4981 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4982 (setq org-agenda-overriding-restriction nil)
4983 (setq org-agenda-restrict nil)
4984 (put 'org-agenda-files 'org-restrict nil)
4985 (move-marker org-agenda-restrict-begin nil)
4986 (move-marker org-agenda-restrict-end nil)
4987 (setq current-prefix-arg nil)
4988 (message "Agenda restriction lock removed")
4989 (or noupdate (org-agenda-maybe-redo)))
4991 (defun org-agenda-maybe-redo ()
4992 "If there is any window showing the agenda view, update it."
4993 (let ((w (get-buffer-window org-agenda-buffer-name t))
4994 (w0 (selected-window)))
4995 (when w
4996 (select-window w)
4997 (org-agenda-redo)
4998 (select-window w0)
4999 (if org-agenda-overriding-restriction
5000 (message "Agenda view shifted to new %s restriction"
5001 org-agenda-overriding-restriction)
5002 (message "Agenda restriction lock removed")))))
5004 ;;; Agenda commands
5006 (defun org-agenda-check-type (error &rest types)
5007 "Check if agenda buffer is of allowed type.
5008 If ERROR is non-nil, throw an error, otherwise just return nil."
5009 (if (memq org-agenda-type types)
5011 (if error
5012 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5013 nil)))
5015 (defun org-agenda-quit ()
5016 "Exit agenda by removing the window or the buffer."
5017 (interactive)
5018 (if org-agenda-columns-active
5019 (org-columns-quit)
5020 (let ((buf (current-buffer)))
5021 (if (eq org-agenda-window-setup 'other-frame)
5022 (progn
5023 (kill-buffer buf)
5024 (org-agenda-reset-markers)
5025 (org-columns-remove-overlays)
5026 (setq org-agenda-archives-mode nil)
5027 (delete-frame))
5028 (and (not (eq org-agenda-window-setup 'current-window))
5029 (not (one-window-p))
5030 (delete-window))
5031 (kill-buffer buf)
5032 (org-agenda-reset-markers)
5033 (org-columns-remove-overlays)
5034 (setq org-agenda-archives-mode nil)))
5035 ;; Maybe restore the pre-agenda window configuration.
5036 (and org-agenda-restore-windows-after-quit
5037 (not (eq org-agenda-window-setup 'other-frame))
5038 org-pre-agenda-window-conf
5039 (set-window-configuration org-pre-agenda-window-conf))))
5041 (defun org-agenda-exit ()
5042 "Exit agenda by removing the window or the buffer.
5043 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5044 Org-mode buffers visited directly by the user will not be touched."
5045 (interactive)
5046 (org-release-buffers org-agenda-new-buffers)
5047 (setq org-agenda-new-buffers nil)
5048 (org-agenda-quit))
5050 (defun org-agenda-execute (arg)
5051 "Execute another agenda command, keeping same window.\\<global-map>
5052 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
5053 (interactive "P")
5054 (let ((org-agenda-window-setup 'current-window))
5055 (org-agenda arg)))
5057 (defun org-agenda-redo ()
5058 "Rebuild Agenda.
5059 When this is the global TODO list, a prefix argument will be interpreted."
5060 (interactive)
5061 (let* ((org-agenda-keep-modes t)
5062 (filter org-agenda-filter)
5063 (preset (get 'org-agenda-filter :preset-filter))
5064 (cols org-agenda-columns-active)
5065 (line (org-current-line))
5066 (window-line (- line (org-current-line (window-start))))
5067 (lprops (get 'org-agenda-redo-command 'org-lprops)))
5068 (put 'org-agenda-filter :preset-filter nil)
5069 (and cols (org-columns-quit))
5070 (message "Rebuilding agenda buffer...")
5071 (org-let lprops '(eval org-agenda-redo-command))
5072 (setq org-agenda-undo-list nil
5073 org-agenda-pending-undo-list nil)
5074 (message "Rebuilding agenda buffer...done")
5075 (put 'org-agenda-filter :preset-filter preset)
5076 (and (or filter preset) (org-agenda-filter-apply filter))
5077 (and cols (interactive-p) (org-agenda-columns))
5078 (org-goto-line line)
5079 (recenter window-line)))
5082 (defvar org-global-tags-completion-table nil)
5083 (defvar org-agenda-filter-form nil)
5084 (defun org-agenda-filter-by-tag (strip &optional char narrow)
5085 "Keep only those lines in the agenda buffer that have a specific tag.
5086 The tag is selected with its fast selection letter, as configured.
5087 With prefix argument STRIP, remove all lines that do have the tag.
5088 A lisp caller can specify CHAR. NARROW means that the new tag should be
5089 used to narrow the search - the interactive user can also press `-' or `+'
5090 to switch to narrowing."
5091 (interactive "P")
5092 (let* ((alist org-tag-alist-for-agenda)
5093 (tag-chars (mapconcat
5094 (lambda (x) (if (and (not (symbolp (car x)))
5095 (cdr x))
5096 (char-to-string (cdr x))
5097 ""))
5098 alist ""))
5099 (efforts (org-split-string
5100 (or (cdr (assoc (concat org-effort-property "_ALL")
5101 org-global-properties))
5102 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5103 (effort-op org-agenda-filter-effort-default-operator)
5104 (effort-prompt "")
5105 (inhibit-read-only t)
5106 (current org-agenda-filter)
5107 char a n tag)
5108 (unless char
5109 (message
5110 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5111 (if narrow "Narrow" "Filter") tag-chars
5112 (if org-agenda-auto-exclude-function "[RET], " ""))
5113 (setq char (read-char)))
5114 (when (member char '(?+ ?-))
5115 ;; Narrowing down
5116 (cond ((equal char ?-) (setq strip t narrow t))
5117 ((equal char ?+) (setq strip nil narrow t)))
5118 (message
5119 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5120 (setq char (read-char)))
5121 (when (member char '(?< ?> ?= ??))
5122 ;; An effort operator
5123 (setq effort-op (char-to-string char))
5124 (setq alist nil) ; to make sure it will be interpreted as effort.
5125 (unless (equal char ??)
5126 (loop for i from 0 to 9 do
5127 (setq effort-prompt
5128 (concat
5129 effort-prompt " ["
5130 (if (= i 9) "0" (int-to-string (1+ i)))
5131 "]" (nth i efforts))))
5132 (message "Effort%s: %s " effort-op effort-prompt)
5133 (setq char (read-char))
5134 (when (or (< char ?0) (> char ?9))
5135 (error "Need 1-9,0 to select effort" ))))
5136 (when (equal char ?\t)
5137 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5138 (org-set-local 'org-global-tags-completion-table
5139 (org-global-tags-completion-table)))
5140 (let ((completion-ignore-case t))
5141 (setq tag (org-icompleting-read
5142 "Tag: " org-global-tags-completion-table))))
5143 (cond
5144 ((equal char ?\r)
5145 (org-agenda-filter-by-tag-show-all)
5146 (when org-agenda-auto-exclude-function
5147 (setq org-agenda-filter '())
5148 (dolist (tag org-tag-alist-for-agenda)
5149 (let ((modifier (funcall org-agenda-auto-exclude-function
5150 (car tag))))
5151 (if modifier
5152 (push modifier org-agenda-filter))))
5153 (if (not (null org-agenda-filter))
5154 (org-agenda-filter-apply org-agenda-filter))))
5155 ((equal char ?/)
5156 (org-agenda-filter-by-tag-show-all)
5157 (when (get 'org-agenda-filter :preset-filter)
5158 (org-agenda-filter-apply org-agenda-filter)))
5159 ((or (equal char ?\ )
5160 (setq a (rassoc char alist))
5161 (and (>= char ?0) (<= char ?9)
5162 (setq n (if (= char ?0) 9 (- char ?0 1))
5163 tag (concat effort-op (nth n efforts))
5164 a (cons tag nil)))
5165 (and (= char ??)
5166 (setq tag "?eff")
5167 a (cons tag nil))
5168 (and tag (setq a (cons tag nil))))
5169 (org-agenda-filter-by-tag-show-all)
5170 (setq tag (car a))
5171 (setq org-agenda-filter
5172 (cons (concat (if strip "-" "+") tag)
5173 (if narrow current nil)))
5174 (org-agenda-filter-apply org-agenda-filter))
5175 (t (error "Invalid tag selection character %c" char)))))
5177 (defun org-agenda-filter-by-tag-refine (strip &optional char)
5178 "Refine the current filter. See `org-agenda-filter-by-tag."
5179 (interactive "P")
5180 (org-agenda-filter-by-tag strip char 'refine))
5182 (defun org-agenda-filter-make-matcher ()
5183 "Create the form that tests a line for the agenda filter."
5184 (let (f f1)
5185 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5186 org-agenda-filter))
5187 (if (member x '("-" "+"))
5188 (setq f1 (if (equal x "-") 'tags '(not tags)))
5189 (if (string-match "[<=>?]" x)
5190 (setq f1 (org-agenda-filter-effort-form x))
5191 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5192 (if (equal (string-to-char x) ?-)
5193 (setq f1 (list 'not f1))))
5194 (push f1 f))
5195 (cons 'and (nreverse f))))
5197 (defun org-agenda-filter-effort-form (e)
5198 "Return the form to compare the effort of the current line with what E says.
5199 E looks line \"+<2:25\"."
5200 (let (op)
5201 (setq e (substring e 1))
5202 (setq op (string-to-char e) e (substring e 1))
5203 (setq op (cond ((equal op ?<) '<=)
5204 ((equal op ?>) '>=)
5205 ((equal op ??) op)
5206 (t '=)))
5207 (list 'org-agenda-compare-effort (list 'quote op)
5208 (org-hh:mm-string-to-minutes e))))
5210 (defun org-agenda-compare-effort (op value)
5211 "Compare the effort of the current line with VALUE, using OP.
5212 If the line does not have an effort defined, return nil."
5213 (let ((eff (org-get-at-bol 'effort-minutes)))
5214 (if (equal op ??)
5215 (not eff)
5216 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5217 value))))
5219 (defun org-agenda-filter-apply (filter)
5220 "Set FILTER as the new agenda filter and apply it."
5221 (let (tags)
5222 (setq org-agenda-filter filter
5223 org-agenda-filter-form (org-agenda-filter-make-matcher))
5224 (org-agenda-set-mode-name)
5225 (save-excursion
5226 (goto-char (point-min))
5227 (while (not (eobp))
5228 (if (org-get-at-bol 'org-marker)
5229 (progn
5230 (setq tags (org-get-at-bol 'tags)) ; used in eval
5231 (if (not (eval org-agenda-filter-form))
5232 (org-agenda-filter-by-tag-hide-line))
5233 (beginning-of-line 2))
5234 (beginning-of-line 2))))))
5236 (defun org-agenda-filter-by-tag-hide-line ()
5237 (let (ov)
5238 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5239 (point-at-eol)))
5240 (org-overlay-put ov 'invisible t)
5241 (org-overlay-put ov 'type 'tags-filter)
5242 (push ov org-agenda-filter-overlays)))
5244 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5245 (setq pos (or pos (point)))
5246 (save-excursion
5247 (dolist (ov (org-overlays-at pos))
5248 (when (and (org-overlay-get ov 'invisible)
5249 (eq (org-overlay-get ov 'type) 'tags-filter))
5250 (goto-char pos)
5251 (if (< (org-overlay-start ov) (point-at-eol))
5252 (org-move-overlay ov (point-at-eol)
5253 (org-overlay-end ov)))))))
5255 (defun org-agenda-filter-by-tag-show-all ()
5256 (mapc 'org-delete-overlay org-agenda-filter-overlays)
5257 (setq org-agenda-filter-overlays nil)
5258 (setq org-agenda-filter nil)
5259 (setq org-agenda-filter-form nil)
5260 (org-agenda-set-mode-name))
5262 (defun org-agenda-manipulate-query-add ()
5263 "Manipulate the query by adding a search term with positive selection.
5264 Positive selection means, the term must be matched for selection of an entry."
5265 (interactive)
5266 (org-agenda-manipulate-query ?\[))
5267 (defun org-agenda-manipulate-query-subtract ()
5268 "Manipulate the query by adding a search term with negative selection.
5269 Negative selection means, term must not be matched for selection of an entry."
5270 (interactive)
5271 (org-agenda-manipulate-query ?\]))
5272 (defun org-agenda-manipulate-query-add-re ()
5273 "Manipulate the query by adding a search regexp with positive selection.
5274 Positive selection means, the regexp must match for selection of an entry."
5275 (interactive)
5276 (org-agenda-manipulate-query ?\{))
5277 (defun org-agenda-manipulate-query-subtract-re ()
5278 "Manipulate the query by adding a search regexp with negative selection.
5279 Negative selection means, regexp must not match for selection of an entry."
5280 (interactive)
5281 (org-agenda-manipulate-query ?\}))
5282 (defun org-agenda-manipulate-query (char)
5283 (cond
5284 ((memq org-agenda-type '(timeline agenda))
5285 (let ((org-agenda-include-inactive-timestamps t))
5286 (org-agenda-redo))
5287 (message "Display now includes inactive timestamps as well"))
5288 ((eq org-agenda-type 'search)
5289 (org-add-to-string
5290 'org-agenda-query-string
5291 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5292 (?\{ . " +{}") (?\} . " -{}")))))
5293 (setq org-agenda-redo-command
5294 (list 'org-search-view
5295 org-todo-only
5296 org-agenda-query-string
5297 (+ (length org-agenda-query-string)
5298 (if (member char '(?\{ ?\})) 0 1))))
5299 (set-register org-agenda-query-register org-agenda-query-string)
5300 (org-agenda-redo))
5301 (t (error "Cannot manipulate query for %s-type agenda buffers"
5302 org-agenda-type))))
5304 (defun org-add-to-string (var string)
5305 (set var (concat (symbol-value var) string)))
5307 (defun org-agenda-goto-date (date)
5308 "Jump to DATE in agenda."
5309 (interactive (list (org-read-date)))
5310 (org-agenda-list nil date))
5312 (defun org-agenda-goto-today ()
5313 "Go to today."
5314 (interactive)
5315 (org-agenda-check-type t 'timeline 'agenda)
5316 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5317 (cond
5318 (tdpos (goto-char tdpos))
5319 ((eq org-agenda-type 'agenda)
5320 (let* ((sd (time-to-days
5321 (time-subtract (current-time)
5322 (list 0 (* 3600 org-extend-today-until) 0))))
5323 (comp (org-agenda-compute-time-span sd org-agenda-span))
5324 (org-agenda-overriding-arguments org-agenda-last-arguments))
5325 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5326 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5327 (org-agenda-redo)
5328 (org-agenda-find-same-or-today-or-agenda)))
5329 (t (error "Cannot find today")))))
5331 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5332 (goto-char
5333 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5334 (text-property-any (point-min) (point-max) 'org-today t)
5335 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5336 (point-min))))
5338 (defun org-agenda-later (arg)
5339 "Go forward in time by thee current span.
5340 With prefix ARG, go forward that many times the current span."
5341 (interactive "p")
5342 (org-agenda-check-type t 'agenda)
5343 (let* ((span org-agenda-span)
5344 (sd org-starting-day)
5345 (greg (calendar-gregorian-from-absolute sd))
5346 (cnt (org-get-at-bol 'org-day-cnt))
5347 greg2 nd)
5348 (cond
5349 ((eq span 'day)
5350 (setq sd (+ arg sd) nd 1))
5351 ((eq span 'week)
5352 (setq sd (+ (* 7 arg) sd) nd 7))
5353 ((eq span 'month)
5354 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5355 sd (calendar-absolute-from-gregorian greg2))
5356 (setcar greg2 (1+ (car greg2)))
5357 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5358 ((eq span 'year)
5359 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5360 sd (calendar-absolute-from-gregorian greg2))
5361 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5362 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5363 (let ((org-agenda-overriding-arguments
5364 (list (car org-agenda-last-arguments) sd nd t)))
5365 (org-agenda-redo)
5366 (org-agenda-find-same-or-today-or-agenda cnt))))
5368 (defun org-agenda-earlier (arg)
5369 "Go backward in time by the current span.
5370 With prefix ARG, go backward that many times the current span."
5371 (interactive "p")
5372 (org-agenda-later (- arg)))
5374 (defun org-agenda-view-mode-dispatch ()
5375 "Call one of the view mode commands."
5376 (interactive)
5377 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5378 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
5379 (let ((a (read-char-exclusive)))
5380 (case a
5381 (?d (call-interactively 'org-agenda-day-view))
5382 (?w (call-interactively 'org-agenda-week-view))
5383 (?m (call-interactively 'org-agenda-month-view))
5384 (?y (call-interactively 'org-agenda-year-view))
5385 (?l (call-interactively 'org-agenda-log-mode))
5386 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
5387 (?a (call-interactively 'org-agenda-archives-mode))
5388 (?A (org-agenda-archives-mode 'files))
5389 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5390 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
5391 (?G (call-interactively 'org-agenda-toggle-time-grid))
5392 (?D (call-interactively 'org-agenda-toggle-diary))
5393 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5394 (org-agenda-check-type t 'timeline 'agenda)
5395 (org-agenda-redo))
5396 (message "Display now includes inactive timestamps as well"))
5397 (?q (message "Abort"))
5398 (otherwise (error "Invalid key" )))))
5400 (defun org-agenda-day-view (&optional day-of-year)
5401 "Switch to daily view for agenda.
5402 With argument DAY-OF-YEAR, switch to that day of the year."
5403 (interactive "P")
5404 (setq org-agenda-ndays 1)
5405 (org-agenda-change-time-span 'day day-of-year))
5406 (defun org-agenda-week-view (&optional iso-week)
5407 "Switch to daily view for agenda.
5408 With argument ISO-WEEK, switch to the corresponding ISO week.
5409 If ISO-WEEK has more then 2 digits, only the last two encode the
5410 week. Any digits before this encode a year. So 200712 means
5411 week 12 of year 2007. Years in the range 1938-2037 can also be
5412 written as 2-digit years."
5413 (interactive "P")
5414 (setq org-agenda-ndays 7)
5415 (org-agenda-change-time-span 'week iso-week))
5416 (defun org-agenda-month-view (&optional month)
5417 "Switch to monthly view for agenda.
5418 With argument MONTH, switch to that month."
5419 (interactive "P")
5420 (org-agenda-change-time-span 'month month))
5421 (defun org-agenda-year-view (&optional year)
5422 "Switch to yearly view for agenda.
5423 With argument YEAR, switch to that year.
5424 If MONTH has more then 2 digits, only the last two encode the
5425 month. Any digits before this encode a year. So 200712 means
5426 December year 2007. Years in the range 1938-2037 can also be
5427 written as 2-digit years."
5428 (interactive "P")
5429 (when year
5430 (setq year (org-small-year-to-year year)))
5431 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5432 (org-agenda-change-time-span 'year year)
5433 (error "Abort")))
5435 (defun org-agenda-change-time-span (span &optional n)
5436 "Change the agenda view to SPAN.
5437 SPAN may be `day', `week', `month', `year'."
5438 (org-agenda-check-type t 'agenda)
5439 (if (and (not n) (equal org-agenda-span span))
5440 (error "Viewing span is already \"%s\"" span))
5441 (let* ((sd (or (org-get-at-bol 'day)
5442 org-starting-day))
5443 (computed (org-agenda-compute-time-span sd span n))
5444 (org-agenda-overriding-arguments
5445 (list (car org-agenda-last-arguments)
5446 (car computed) (cdr computed) t)))
5447 (org-agenda-redo)
5448 (org-agenda-find-same-or-today-or-agenda))
5449 (org-agenda-set-mode-name)
5450 (message "Switched to %s view" span))
5452 (defun org-agenda-compute-time-span (sd span &optional n)
5453 "Compute starting date and number of days for agenda.
5454 SPAN may be `day', `week', `month', `year'. The return value
5455 is a cons cell with the starting date and the number of days,
5456 so that the date SD will be in that range."
5457 (let* ((greg (calendar-gregorian-from-absolute sd))
5458 (dg (nth 1 greg))
5459 (mg (car greg))
5460 (yg (nth 2 greg))
5461 nd w1 y1 m1 thisweek)
5462 (cond
5463 ((eq span 'day)
5464 (when n
5465 (setq sd (+ (calendar-absolute-from-gregorian
5466 (list mg 1 yg))
5467 n -1)))
5468 (setq nd 1))
5469 ((eq span 'week)
5470 (let* ((nt (calendar-day-of-week
5471 (calendar-gregorian-from-absolute sd)))
5472 (d (if org-agenda-start-on-weekday
5473 (- nt org-agenda-start-on-weekday)
5474 0)))
5475 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5476 (when n
5477 (require 'cal-iso)
5478 (setq thisweek (car (calendar-iso-from-absolute sd)))
5479 (when (> n 99)
5480 (setq y1 (org-small-year-to-year (/ n 100))
5481 n (mod n 100)))
5482 (setq sd
5483 (calendar-absolute-from-iso
5484 (list n 1
5485 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5486 (setq nd 7)))
5487 ((eq span 'month)
5488 (when (and n (> n 99))
5489 (setq y1 (org-small-year-to-year (/ n 100))
5490 n (mod n 100)))
5491 (setq sd (calendar-absolute-from-gregorian
5492 (list (or n mg) 1 (or y1 yg)))
5493 nd (- (calendar-absolute-from-gregorian
5494 (list (1+ (or n mg)) 1 (or y1 yg)))
5495 sd)))
5496 ((eq span 'year)
5497 (setq sd (calendar-absolute-from-gregorian
5498 (list 1 1 (or n yg)))
5499 nd (- (calendar-absolute-from-gregorian
5500 (list 1 1 (1+ (or n yg))))
5501 sd))))
5502 (cons sd nd)))
5504 (defun org-agenda-next-date-line (&optional arg)
5505 "Jump to the next line indicating a date in agenda buffer."
5506 (interactive "p")
5507 (org-agenda-check-type t 'agenda 'timeline)
5508 (beginning-of-line 1)
5509 ;; This does not work if user makes date format that starts with a blank
5510 (if (looking-at "^\\S-") (forward-char 1))
5511 (if (not (re-search-forward "^\\S-" nil t arg))
5512 (progn
5513 (backward-char 1)
5514 (error "No next date after this line in this buffer")))
5515 (goto-char (match-beginning 0)))
5517 (defun org-agenda-previous-date-line (&optional arg)
5518 "Jump to the previous line indicating a date in agenda buffer."
5519 (interactive "p")
5520 (org-agenda-check-type t 'agenda 'timeline)
5521 (beginning-of-line 1)
5522 (if (not (re-search-backward "^\\S-" nil t arg))
5523 (error "No previous date before this line in this buffer")))
5525 ;; Initialize the highlight
5526 (defvar org-hl (org-make-overlay 1 1))
5527 (org-overlay-put org-hl 'face 'highlight)
5529 (defun org-highlight (begin end &optional buffer)
5530 "Highlight a region with overlay."
5531 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5532 org-hl begin end (or buffer (current-buffer))))
5534 (defun org-unhighlight ()
5535 "Detach overlay INDEX."
5536 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5538 ;; FIXME this is currently not used.
5539 (defun org-highlight-until-next-command (beg end &optional buffer)
5540 "Move the highlight overlay to BEG/END, remove it before the next command."
5541 (org-highlight beg end buffer)
5542 (add-hook 'pre-command-hook 'org-unhighlight-once))
5543 (defun org-unhighlight-once ()
5544 "Remove the highlight from its position, and this function from the hook."
5545 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5546 (org-unhighlight))
5548 (defun org-agenda-follow-mode ()
5549 "Toggle follow mode in an agenda buffer."
5550 (interactive)
5551 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5552 (org-agenda-set-mode-name)
5553 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5554 (org-agenda-show))
5555 (message "Follow mode is %s"
5556 (if org-agenda-follow-mode "on" "off")))
5558 (defun org-agenda-entry-text-mode (&optional arg)
5559 "Toggle entry text mode in an agenda buffer."
5560 (interactive "P")
5561 (if (integerp arg)
5562 (setq org-agenda-entry-text-mode t)
5563 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5564 (org-agenda-entry-text-hide)
5565 (and org-agenda-entry-text-mode
5566 (let ((org-agenda-entry-text-maxlines
5567 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5568 (org-agenda-entry-text-show)))
5569 (org-agenda-set-mode-name)
5570 (message "Entry text mode is %s. Maximum number of lines is %d"
5571 (if org-agenda-entry-text-mode "on" "off")
5572 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5574 (defun org-agenda-clockreport-mode ()
5575 "Toggle clocktable mode in an agenda buffer."
5576 (interactive)
5577 (org-agenda-check-type t 'agenda)
5578 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5579 (org-agenda-set-mode-name)
5580 (org-agenda-redo)
5581 (message "Clocktable mode is %s"
5582 (if org-agenda-clockreport-mode "on" "off")))
5584 (defun org-agenda-log-mode (&optional special)
5585 "Toggle log mode in an agenda buffer.
5586 With argument SPECIAL, show all possible log items, not only the ones
5587 configured in `org-agenda-log-mode-items'.
5588 With a double `C-u' prefix arg, show *only* log items, nothing else."
5589 (interactive "P")
5590 (org-agenda-check-type t 'agenda 'timeline)
5591 (setq org-agenda-show-log
5592 (if (equal special '(16))
5593 'only
5594 (if special '(closed clock state)
5595 (not org-agenda-show-log))))
5596 (org-agenda-set-mode-name)
5597 (org-agenda-redo)
5598 (message "Log mode is %s"
5599 (if org-agenda-show-log "on" "off")))
5601 (defun org-agenda-archives-mode (&optional with-files)
5602 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5603 When called with a prefix argument, include all archive files as well."
5604 (interactive "P")
5605 (setq org-agenda-archives-mode
5606 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5607 (org-agenda-set-mode-name)
5608 (org-agenda-redo)
5609 (message
5610 "%s"
5611 (cond
5612 ((eq org-agenda-archives-mode nil)
5613 "No archives are included")
5614 ((eq org-agenda-archives-mode 'trees)
5615 (format "Trees with :%s: tag are included" org-archive-tag))
5616 ((eq org-agenda-archives-mode t)
5617 (format "Trees with :%s: tag and all active archive files are included"
5618 org-archive-tag)))))
5620 (defun org-agenda-toggle-diary ()
5621 "Toggle diary inclusion in an agenda buffer."
5622 (interactive)
5623 (org-agenda-check-type t 'agenda)
5624 (setq org-agenda-include-diary (not org-agenda-include-diary))
5625 (org-agenda-redo)
5626 (org-agenda-set-mode-name)
5627 (message "Diary inclusion turned %s"
5628 (if org-agenda-include-diary "on" "off")))
5630 (defun org-agenda-toggle-time-grid ()
5631 "Toggle time grid in an agenda buffer."
5632 (interactive)
5633 (org-agenda-check-type t 'agenda)
5634 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5635 (org-agenda-redo)
5636 (org-agenda-set-mode-name)
5637 (message "Time-grid turned %s"
5638 (if org-agenda-use-time-grid "on" "off")))
5640 (defun org-agenda-set-mode-name ()
5641 "Set the mode name to indicate all the small mode settings."
5642 (setq mode-name
5643 (concat "Org-Agenda"
5644 (if (equal org-agenda-ndays 1) " Day" "")
5645 (if (equal org-agenda-ndays 7) " Week" "")
5646 (if org-agenda-follow-mode " Follow" "")
5647 (if org-agenda-entry-text-mode " ETxt" "")
5648 (if org-agenda-include-diary " Diary" "")
5649 (if org-agenda-use-time-grid " Grid" "")
5650 (if (and (boundp 'org-habit-show-habits)
5651 org-habit-show-habits) " Habit" "")
5652 (if (consp org-agenda-show-log) " LogAll"
5653 (if org-agenda-show-log " Log" ""))
5654 (if (or org-agenda-filter (get 'org-agenda-filter
5655 :preset-filter))
5656 (concat " {" (mapconcat
5657 'identity
5658 (append (get 'org-agenda-filter
5659 :preset-filter)
5660 org-agenda-filter) "") "}")
5662 (if org-agenda-archives-mode
5663 (if (eq org-agenda-archives-mode t)
5664 " Archives"
5665 (format " :%s:" org-archive-tag))
5667 (if org-agenda-clockreport-mode " Clock" "")))
5668 (force-mode-line-update))
5670 (defun org-agenda-post-command-hook ()
5671 (setq org-agenda-type
5672 (or (get-text-property (point) 'org-agenda-type)
5673 (get-text-property (max (point-min) (1- (point)))
5674 'org-agenda-type))))
5676 (defun org-agenda-next-line ()
5677 "Move cursor to the next line, and show if follow-mode is active."
5678 (interactive)
5679 (call-interactively 'next-line)
5680 (org-agenda-do-context-action))
5682 (defun org-agenda-previous-line ()
5683 "Move cursor to the previous line, and show if follow-mode is active."
5685 (interactive)
5686 (call-interactively 'previous-line)
5687 (org-agenda-do-context-action))
5689 (defun org-agenda-do-context-action ()
5690 "Show outline path and, maybe, follow-mode window."
5691 (let ((m (org-get-at-bol 'org-marker)))
5692 (if (and org-agenda-follow-mode m)
5693 (org-agenda-show))
5694 (if (and m org-agenda-show-outline-path)
5695 (message (org-with-point-at m
5696 (org-display-outline-path t))))))
5698 (defun org-agenda-show-priority ()
5699 "Show the priority of the current item.
5700 This priority is composed of the main priority given with the [#A] cookies,
5701 and by additional input from the age of a schedules or deadline entry."
5702 (interactive)
5703 (let* ((pri (org-get-at-bol 'priority)))
5704 (message "Priority is %d" (if pri pri -1000))))
5706 (defun org-agenda-show-tags ()
5707 "Show the tags applicable to the current item."
5708 (interactive)
5709 (let* ((tags (org-get-at-bol 'tags)))
5710 (if tags
5711 (message "Tags are :%s:"
5712 (org-no-properties (mapconcat 'identity tags ":")))
5713 (message "No tags associated with this line"))))
5715 (defun org-agenda-goto (&optional highlight)
5716 "Go to the Org-mode file which contains the item at point."
5717 (interactive)
5718 (let* ((marker (or (org-get-at-bol 'org-marker)
5719 (org-agenda-error)))
5720 (buffer (marker-buffer marker))
5721 (pos (marker-position marker)))
5722 (switch-to-buffer-other-window buffer)
5723 (widen)
5724 (goto-char pos)
5725 (when (org-mode-p)
5726 (org-show-context 'agenda)
5727 (save-excursion
5728 (and (outline-next-heading)
5729 (org-flag-heading nil)))) ; show the next heading
5730 (recenter (/ (window-height) 2))
5731 (run-hooks 'org-agenda-after-show-hook)
5732 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5734 (defvar org-agenda-after-show-hook nil
5735 "Normal hook run after an item has been shown from the agenda.
5736 Point is in the buffer where the item originated.")
5738 (defun org-agenda-kill ()
5739 "Kill the entry or subtree belonging to the current agenda entry."
5740 (interactive)
5741 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5742 (let* ((marker (or (org-get-at-bol 'org-marker)
5743 (org-agenda-error)))
5744 (buffer (marker-buffer marker))
5745 (pos (marker-position marker))
5746 (type (org-get-at-bol 'type))
5747 dbeg dend (n 0) conf)
5748 (org-with-remote-undo buffer
5749 (with-current-buffer buffer
5750 (save-excursion
5751 (goto-char pos)
5752 (if (and (org-mode-p) (not (member type '("sexp"))))
5753 (setq dbeg (progn (org-back-to-heading t) (point))
5754 dend (org-end-of-subtree t t))
5755 (setq dbeg (point-at-bol)
5756 dend (min (point-max) (1+ (point-at-eol)))))
5757 (goto-char dbeg)
5758 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5759 (setq conf (or (eq t org-agenda-confirm-kill)
5760 (and (numberp org-agenda-confirm-kill)
5761 (> n org-agenda-confirm-kill))))
5762 (and conf
5763 (not (y-or-n-p
5764 (format "Delete entry with %d lines in buffer \"%s\"? "
5765 n (buffer-name buffer))))
5766 (error "Abort"))
5767 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5768 (with-current-buffer buffer (delete-region dbeg dend))
5769 (message "Agenda item and source killed"))))
5771 (defvar org-archive-default-command)
5772 (defun org-agenda-archive-default ()
5773 "Archive the entry or subtree belonging to the current agenda entry."
5774 (interactive)
5775 (require 'org-archive)
5776 (org-agenda-archive-with org-archive-default-command))
5778 (defun org-agenda-archive-default-with-confirmation ()
5779 "Archive the entry or subtree belonging to the current agenda entry."
5780 (interactive)
5781 (require 'org-archive)
5782 (org-agenda-archive-with org-archive-default-command 'confirm))
5784 (defun org-agenda-archive ()
5785 "Archive the entry or subtree belonging to the current agenda entry."
5786 (interactive)
5787 (org-agenda-archive-with 'org-archive-subtree))
5789 (defun org-agenda-archive-to-archive-sibling ()
5790 "Move the entry to the archive sibling."
5791 (interactive)
5792 (org-agenda-archive-with 'org-archive-to-archive-sibling))
5794 (defun org-agenda-archive-with (cmd &optional confirm)
5795 "Move the entry to the archive sibling."
5796 (interactive)
5797 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5798 (let* ((marker (or (org-get-at-bol 'org-marker)
5799 (org-agenda-error)))
5800 (buffer (marker-buffer marker))
5801 (pos (marker-position marker)))
5802 (org-with-remote-undo buffer
5803 (with-current-buffer buffer
5804 (if (org-mode-p)
5805 (if (and confirm
5806 (not (y-or-n-p "Archive this subtree or entry? ")))
5807 (error "Abort")
5808 (save-excursion
5809 (goto-char pos)
5810 (org-remove-subtree-entries-from-agenda)
5811 (org-back-to-heading t)
5812 (funcall cmd)))
5813 (error "Archiving works only in Org-mode files"))))))
5815 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5816 "Remove all lines in the agenda that correspond to a given subtree.
5817 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5818 If this information is not given, the function uses the tree at point."
5819 (let ((buf (or buf (current-buffer))) m p)
5820 (save-excursion
5821 (unless (and beg end)
5822 (org-back-to-heading t)
5823 (setq beg (point))
5824 (org-end-of-subtree t)
5825 (setq end (point)))
5826 (set-buffer (get-buffer org-agenda-buffer-name))
5827 (save-excursion
5828 (goto-char (point-max))
5829 (beginning-of-line 1)
5830 (while (not (bobp))
5831 (when (and (setq m (org-get-at-bol 'org-marker))
5832 (equal buf (marker-buffer m))
5833 (setq p (marker-position m))
5834 (>= p beg)
5835 (< p end))
5836 (let ((inhibit-read-only t))
5837 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5838 (beginning-of-line 0))))))
5840 (defun org-agenda-refile (&optional goto rfloc)
5841 "Refile the item at point."
5842 (interactive "P")
5843 (if (equal goto '(16))
5844 (org-refile-goto-last-stored)
5845 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5846 (org-agenda-error)))
5847 (buffer (marker-buffer marker))
5848 (pos (marker-position marker))
5849 (rfloc (or rfloc
5850 (org-refile-get-location
5851 (if goto "Goto: " "Refile to: ") buffer
5852 org-refile-allow-creating-parent-nodes))))
5853 (with-current-buffer buffer
5854 (save-excursion
5855 (save-restriction
5856 (widen)
5857 (goto-char marker)
5858 (org-remove-subtree-entries-from-agenda)
5859 (org-refile goto buffer rfloc)))))))
5861 (defun org-agenda-open-link (&optional arg)
5862 "Follow the link in the current line, if any.
5863 This looks for a link in the displayed line in the agenda. It also looks
5864 at the text of the entry itself."
5865 (interactive "P")
5866 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5867 (org-get-at-bol 'org-marker)))
5868 (buffer (and marker (marker-buffer marker)))
5869 (prefix (buffer-substring
5870 (point-at-bol)
5871 (+ (point-at-bol)
5872 (or (org-get-at-bol 'prefix-length) 0)))))
5873 (cond
5874 (buffer
5875 (with-current-buffer buffer
5876 (save-excursion
5877 (save-restriction
5878 (widen)
5879 (goto-char marker)
5880 (org-offer-links-in-entry arg prefix)))))
5881 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
5882 (save-excursion
5883 (beginning-of-line 1)
5884 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
5885 (org-open-link-from-string (match-string 1)))
5886 (t (error "No link to open here")))))
5888 (defun org-agenda-copy-local-variable (var)
5889 "Get a variable from a referenced buffer and install it here."
5890 (let ((m (org-get-at-bol 'org-marker)))
5891 (when (and m (buffer-live-p (marker-buffer m)))
5892 (org-set-local var (with-current-buffer (marker-buffer m)
5893 (symbol-value var))))))
5895 (defun org-agenda-switch-to (&optional delete-other-windows)
5896 "Go to the Org-mode file which contains the item at point."
5897 (interactive)
5898 (if (and org-return-follows-link
5899 (not (org-get-at-bol 'org-marker))
5900 (org-in-regexp org-bracket-link-regexp))
5901 (org-open-link-from-string (match-string 0))
5902 (let* ((marker (or (org-get-at-bol 'org-marker)
5903 (org-agenda-error)))
5904 (buffer (marker-buffer marker))
5905 (pos (marker-position marker)))
5906 (switch-to-buffer buffer)
5907 (and delete-other-windows (delete-other-windows))
5908 (widen)
5909 (goto-char pos)
5910 (when (org-mode-p)
5911 (org-show-context 'agenda)
5912 (save-excursion
5913 (and (outline-next-heading)
5914 (org-flag-heading nil))))))) ; show the next heading
5916 (defun org-agenda-goto-mouse (ev)
5917 "Go to the Org-mode file which contains the item at the mouse click."
5918 (interactive "e")
5919 (mouse-set-point ev)
5920 (org-agenda-goto))
5922 (defun org-agenda-show (&optional full-entry)
5923 "Display the Org-mode file which contains the item at point.
5924 With prefix argument FULL-ENTRY, make the entire entry visible
5925 if it was hidden in the outline."
5926 (interactive "P")
5927 (let ((win (selected-window)))
5928 (if full-entry
5929 (let ((org-show-entry-below t))
5930 (org-agenda-goto t))
5931 (org-agenda-goto t))
5932 (select-window win)))
5934 (defvar org-agenda-show-window nil)
5935 (defun org-agenda-show-and-scroll-up ()
5936 "Display the Org-mode file which contains the item at point.
5937 When called repeatedly, scroll the window that is displaying the buffer."
5938 (interactive)
5939 (let ((win (selected-window)))
5940 (if (and (window-live-p org-agenda-show-window)
5941 (eq this-command last-command))
5942 (progn
5943 (select-window org-agenda-show-window)
5944 (ignore-errors (scroll-up)))
5945 (org-agenda-goto t)
5946 (show-subtree)
5947 (setq org-agenda-show-window (selected-window)))
5948 (select-window win)))
5950 (defun org-agenda-show-scroll-down ()
5951 "Scroll down the window showing the agenda."
5952 (interactive)
5953 (let ((win (selected-window)))
5954 (when (window-live-p org-agenda-show-window)
5955 (select-window org-agenda-show-window)
5956 (ignore-errors (scroll-down))
5957 (select-window win))))
5959 (defun org-agenda-show-1 (&optional more)
5960 "Display the Org-mode file which contains the item at point.
5961 The prefix arg selects the amount of information to display:
5963 0 hide the subtree
5964 1 just show the entry according to defaults.
5965 2 show the children view
5966 3 show the subtree view
5967 4 show the entire subtree and any LOGBOOK drawers
5968 5 show the entire subtree and any drawers
5969 With prefix argument FULL-ENTRY, make the entire entry visible
5970 if it was hidden in the outline."
5971 (interactive "p")
5972 (let ((win (selected-window)))
5973 (org-agenda-goto t)
5974 (org-recenter-heading 1)
5975 (cond
5976 ((= more 0)
5977 (hide-subtree)
5978 (save-excursion
5979 (org-back-to-heading)
5980 (run-hook-with-args 'org-cycle-hook 'folded))
5981 (message "Remote: FOLDED"))
5982 ((and (interactive-p) (= more 1))
5983 (message "Remote: show with default settings"))
5984 ((= more 2)
5985 (show-entry)
5986 (show-children)
5987 (save-excursion
5988 (org-back-to-heading)
5989 (run-hook-with-args 'org-cycle-hook 'children))
5990 (message "Remote: CHILDREN"))
5991 ((= more 3)
5992 (show-subtree)
5993 (save-excursion
5994 (org-back-to-heading)
5995 (run-hook-with-args 'org-cycle-hook 'subtree))
5996 (message "Remote: SUBTREE"))
5997 ((= more 4)
5998 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5999 (org-drawer-regexp
6000 (concat "^[ \t]*:\\("
6001 (mapconcat 'regexp-quote org-drawers "\\|")
6002 "\\):[ \t]*$")))
6003 (show-subtree)
6004 (save-excursion
6005 (org-back-to-heading)
6006 (org-cycle-hide-drawers 'subtree)))
6007 (message "Remote: SUBTREE AND LOGBOOK"))
6008 ((> more 4)
6009 (show-subtree)
6010 (message "Remote: SUBTREE AND ALL DRAWERS")))
6011 (select-window win)))
6013 (defun org-recenter-heading (n)
6014 (save-excursion
6015 (org-back-to-heading)
6016 (recenter n)))
6018 (defvar org-agenda-cycle-counter nil)
6019 (defun org-agenda-cycle-show (&optional n)
6020 "Show the current entry in another window, with default settings.
6021 Default settings are taken from `org-show-hierarchy-above' and siblings.
6022 When use repeatedly in immediate succession, the remote entry will cycle
6023 through visibility
6025 children -> subtree -> folded
6027 When called with a numeric prefix arg, that arg will be passed through to
6028 `org-agenda-show-1'. For the interpretation of that argument, see the
6029 docstring of `org-agenda-show-1'."
6030 (interactive "P")
6031 (if (integerp n)
6032 (setq org-agenda-cycle-counter n)
6033 (if (not (eq last-command this-command))
6034 (setq org-agenda-cycle-counter 1)
6035 (if (equal org-agenda-cycle-counter 0)
6036 (setq org-agenda-cycle-counter 2)
6037 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6038 (if (> org-agenda-cycle-counter 3)
6039 (setq org-agenda-cycle-counter 0)))))
6040 (org-agenda-show-1 org-agenda-cycle-counter))
6042 (defun org-agenda-recenter (arg)
6043 "Display the Org-mode file which contains the item at point and recenter."
6044 (interactive "P")
6045 (let ((win (selected-window)))
6046 (org-agenda-goto t)
6047 (recenter arg)
6048 (select-window win)))
6050 (defun org-agenda-show-mouse (ev)
6051 "Display the Org-mode file which contains the item at the mouse click."
6052 (interactive "e")
6053 (mouse-set-point ev)
6054 (org-agenda-show))
6056 (defun org-agenda-check-no-diary ()
6057 "Check if the entry is a diary link and abort if yes."
6058 (if (org-get-at-bol 'org-agenda-diary-link)
6059 (org-agenda-error)))
6061 (defun org-agenda-error ()
6062 (error "Command not allowed in this line"))
6064 (defun org-agenda-tree-to-indirect-buffer ()
6065 "Show the subtree corresponding to the current entry in an indirect buffer.
6066 This calls the command `org-tree-to-indirect-buffer' from the original
6067 Org-mode buffer.
6068 With numerical prefix arg ARG, go up to this level and then take that tree.
6069 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
6070 dedicated frame)."
6071 (interactive)
6072 (org-agenda-check-no-diary)
6073 (let* ((marker (or (org-get-at-bol 'org-marker)
6074 (org-agenda-error)))
6075 (buffer (marker-buffer marker))
6076 (pos (marker-position marker)))
6077 (with-current-buffer buffer
6078 (save-excursion
6079 (goto-char pos)
6080 (call-interactively 'org-tree-to-indirect-buffer)))))
6082 (defvar org-last-heading-marker (make-marker)
6083 "Marker pointing to the headline that last changed its TODO state
6084 by a remote command from the agenda.")
6086 (defun org-agenda-todo-nextset ()
6087 "Switch TODO entry to next sequence."
6088 (interactive)
6089 (org-agenda-todo 'nextset))
6091 (defun org-agenda-todo-previousset ()
6092 "Switch TODO entry to previous sequence."
6093 (interactive)
6094 (org-agenda-todo 'previousset))
6096 (defun org-agenda-todo (&optional arg)
6097 "Cycle TODO state of line at point, also in Org-mode file.
6098 This changes the line at point, all other lines in the agenda referring to
6099 the same tree node, and the headline of the tree node in the Org-mode file."
6100 (interactive "P")
6101 (org-agenda-check-no-diary)
6102 (let* ((col (current-column))
6103 (marker (or (org-get-at-bol 'org-marker)
6104 (org-agenda-error)))
6105 (buffer (marker-buffer marker))
6106 (pos (marker-position marker))
6107 (hdmarker (org-get-at-bol 'org-hd-marker))
6108 (todayp (equal (org-get-at-bol 'day)
6109 (time-to-days (current-time))))
6110 (inhibit-read-only t)
6111 org-agenda-headline-snapshot-before-repeat newhead just-one)
6112 (org-with-remote-undo buffer
6113 (with-current-buffer buffer
6114 (widen)
6115 (goto-char pos)
6116 (org-show-context 'agenda)
6117 (save-excursion
6118 (and (outline-next-heading)
6119 (org-flag-heading nil))) ; show the next heading
6120 (let ((current-prefix-arg arg))
6121 (call-interactively 'org-todo))
6122 (and (bolp) (forward-char 1))
6123 (setq newhead (org-get-heading))
6124 (when (and (org-bound-and-true-p
6125 org-agenda-headline-snapshot-before-repeat)
6126 (not (equal org-agenda-headline-snapshot-before-repeat
6127 newhead))
6128 todayp)
6129 (setq newhead org-agenda-headline-snapshot-before-repeat
6130 just-one t))
6131 (save-excursion
6132 (org-back-to-heading)
6133 (move-marker org-last-heading-marker (point))))
6134 (beginning-of-line 1)
6135 (save-excursion
6136 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
6137 (org-move-to-column col))))
6139 (defun org-agenda-add-note (&optional arg)
6140 "Add a time-stamped note to the entry at point."
6141 (interactive "P")
6142 (org-agenda-check-no-diary)
6143 (let* ((marker (or (org-get-at-bol 'org-marker)
6144 (org-agenda-error)))
6145 (buffer (marker-buffer marker))
6146 (pos (marker-position marker))
6147 (hdmarker (org-get-at-bol 'org-hd-marker))
6148 (inhibit-read-only t))
6149 (with-current-buffer buffer
6150 (widen)
6151 (goto-char pos)
6152 (org-show-context 'agenda)
6153 (save-excursion
6154 (and (outline-next-heading)
6155 (org-flag-heading nil))) ; show the next heading
6156 (org-add-note))))
6158 (defun org-agenda-change-all-lines (newhead hdmarker
6159 &optional fixface just-this)
6160 "Change all lines in the agenda buffer which match HDMARKER.
6161 The new content of the line will be NEWHEAD (as modified by
6162 `org-format-agenda-item'). HDMARKER is checked with
6163 `equal' against all `org-hd-marker' text properties in the file.
6164 If FIXFACE is non-nil, the face of each item is modified according to
6165 the new TODO state.
6166 If JUST-THIS is non-nil, change just the current line, not all.
6167 If FORCE-TAGS is non nil, the car of it returns the new tags."
6168 (let* ((inhibit-read-only t)
6169 (line (org-current-line))
6170 (thetags (with-current-buffer (marker-buffer hdmarker)
6171 (save-excursion (save-restriction (widen)
6172 (goto-char hdmarker)
6173 (org-get-tags-at)))))
6174 props m pl undone-face done-face finish new dotime cat tags)
6175 (save-excursion
6176 (goto-char (point-max))
6177 (beginning-of-line 1)
6178 (while (not finish)
6179 (setq finish (bobp))
6180 (when (and (setq m (org-get-at-bol 'org-hd-marker))
6181 (or (not just-this) (= (org-current-line) line))
6182 (equal m hdmarker))
6183 (setq props (text-properties-at (point))
6184 dotime (org-get-at-bol 'dotime)
6185 cat (org-get-at-bol 'org-category)
6186 tags thetags
6187 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6188 pl (org-get-at-bol 'prefix-length)
6189 undone-face (org-get-at-bol 'undone-face)
6190 done-face (org-get-at-bol 'done-face))
6191 (goto-char (+ (point) pl))
6192 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
6193 (cond
6194 ((equal new "")
6195 (beginning-of-line 1)
6196 (and (looking-at ".*\n?") (replace-match "")))
6197 ((looking-at ".*")
6198 (replace-match new t t)
6199 (beginning-of-line 1)
6200 (add-text-properties (point-at-bol) (point-at-eol) props)
6201 (when fixface
6202 (add-text-properties
6203 (point-at-bol) (point-at-eol)
6204 (list 'face
6205 (if org-last-todo-state-is-todo
6206 undone-face done-face))))
6207 (org-agenda-highlight-todo 'line)
6208 (beginning-of-line 1))
6209 (t (error "Line update did not work"))))
6210 (beginning-of-line 0)))
6211 (org-finalize-agenda)))
6213 (defun org-agenda-align-tags (&optional line)
6214 "Align all tags in agenda items to `org-agenda-tags-column'."
6215 (let ((inhibit-read-only t) l c)
6216 (save-excursion
6217 (goto-char (if line (point-at-bol) (point-min)))
6218 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6219 (if line (point-at-eol) nil) t)
6220 (add-text-properties
6221 (match-beginning 2) (match-end 2)
6222 (list 'face (delq nil (let ((prop (get-text-property
6223 (match-beginning 2) 'face)))
6224 (or (listp prop) (setq prop (list prop)))
6225 (if (memq 'org-tag prop)
6226 prop
6227 (cons 'org-tag prop))))))
6228 (setq l (- (match-end 2) (match-beginning 2))
6229 c (if (< org-agenda-tags-column 0)
6230 (- (abs org-agenda-tags-column) l)
6231 org-agenda-tags-column))
6232 (delete-region (match-beginning 1) (match-end 1))
6233 (goto-char (match-beginning 1))
6234 (insert (org-add-props
6235 (make-string (max 1 (- c (current-column))) ?\ )
6236 (text-properties-at (point)))))
6237 (goto-char (point-min))
6238 (org-font-lock-add-tag-faces (point-max)))))
6240 (defun org-agenda-priority-up ()
6241 "Increase the priority of line at point, also in Org-mode file."
6242 (interactive)
6243 (org-agenda-priority 'up))
6245 (defun org-agenda-priority-down ()
6246 "Decrease the priority of line at point, also in Org-mode file."
6247 (interactive)
6248 (org-agenda-priority 'down))
6250 (defun org-agenda-priority (&optional force-direction)
6251 "Set the priority of line at point, also in Org-mode file.
6252 This changes the line at point, all other lines in the agenda referring to
6253 the same tree node, and the headline of the tree node in the Org-mode file."
6254 (interactive)
6255 (unless org-enable-priority-commands
6256 (error "Priority commands are disabled"))
6257 (org-agenda-check-no-diary)
6258 (let* ((marker (or (org-get-at-bol 'org-marker)
6259 (org-agenda-error)))
6260 (hdmarker (org-get-at-bol 'org-hd-marker))
6261 (buffer (marker-buffer hdmarker))
6262 (pos (marker-position hdmarker))
6263 (inhibit-read-only t)
6264 newhead)
6265 (org-with-remote-undo buffer
6266 (with-current-buffer buffer
6267 (widen)
6268 (goto-char pos)
6269 (org-show-context 'agenda)
6270 (save-excursion
6271 (and (outline-next-heading)
6272 (org-flag-heading nil))) ; show the next heading
6273 (funcall 'org-priority force-direction)
6274 (end-of-line 1)
6275 (setq newhead (org-get-heading)))
6276 (org-agenda-change-all-lines newhead hdmarker)
6277 (beginning-of-line 1))))
6279 ;; FIXME: should fix the tags property of the agenda line.
6280 (defun org-agenda-set-tags (&optional tag onoff)
6281 "Set tags for the current headline."
6282 (interactive)
6283 (org-agenda-check-no-diary)
6284 (if (and (org-region-active-p) (interactive-p))
6285 (call-interactively 'org-change-tag-in-region)
6286 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6287 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6288 (org-agenda-error)))
6289 (buffer (marker-buffer hdmarker))
6290 (pos (marker-position hdmarker))
6291 (inhibit-read-only t)
6292 newhead)
6293 (org-with-remote-undo buffer
6294 (with-current-buffer buffer
6295 (widen)
6296 (goto-char pos)
6297 (save-excursion
6298 (org-show-context 'agenda))
6299 (save-excursion
6300 (and (outline-next-heading)
6301 (org-flag-heading nil))) ; show the next heading
6302 (goto-char pos)
6303 (if tag
6304 (org-toggle-tag tag onoff)
6305 (call-interactively 'org-set-tags))
6306 (end-of-line 1)
6307 (setq newhead (org-get-heading)))
6308 (org-agenda-change-all-lines newhead hdmarker)
6309 (beginning-of-line 1)))))
6311 (defun org-agenda-set-property ()
6312 "Set a property for the current headline."
6313 (interactive)
6314 (org-agenda-check-no-diary)
6315 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6316 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6317 (org-agenda-error)))
6318 (buffer (marker-buffer hdmarker))
6319 (pos (marker-position hdmarker))
6320 (inhibit-read-only t)
6321 newhead)
6322 (org-with-remote-undo buffer
6323 (with-current-buffer buffer
6324 (widen)
6325 (goto-char pos)
6326 (save-excursion
6327 (org-show-context 'agenda))
6328 (save-excursion
6329 (and (outline-next-heading)
6330 (org-flag-heading nil))) ; show the next heading
6331 (goto-char pos)
6332 (call-interactively 'org-set-property)))))
6334 (defun org-agenda-set-effort ()
6335 "Set the effort property for the current headline."
6336 (interactive)
6337 (org-agenda-check-no-diary)
6338 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6339 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6340 (org-agenda-error)))
6341 (buffer (marker-buffer hdmarker))
6342 (pos (marker-position hdmarker))
6343 (inhibit-read-only t)
6344 newhead)
6345 (org-with-remote-undo buffer
6346 (with-current-buffer buffer
6347 (widen)
6348 (goto-char pos)
6349 (save-excursion
6350 (org-show-context 'agenda))
6351 (save-excursion
6352 (and (outline-next-heading)
6353 (org-flag-heading nil))) ; show the next heading
6354 (goto-char pos)
6355 (call-interactively 'org-set-effort)
6356 (end-of-line 1)))))
6358 (defun org-agenda-toggle-archive-tag ()
6359 "Toggle the archive tag for the current entry."
6360 (interactive)
6361 (org-agenda-check-no-diary)
6362 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6363 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6364 (org-agenda-error)))
6365 (buffer (marker-buffer hdmarker))
6366 (pos (marker-position hdmarker))
6367 (inhibit-read-only t)
6368 newhead)
6369 (org-with-remote-undo buffer
6370 (with-current-buffer buffer
6371 (widen)
6372 (goto-char pos)
6373 (org-show-context 'agenda)
6374 (save-excursion
6375 (and (outline-next-heading)
6376 (org-flag-heading nil))) ; show the next heading
6377 (call-interactively 'org-toggle-archive-tag)
6378 (end-of-line 1)
6379 (setq newhead (org-get-heading)))
6380 (org-agenda-change-all-lines newhead hdmarker)
6381 (beginning-of-line 1))))
6383 (defun org-agenda-do-date-later (arg)
6384 (interactive "P")
6385 (cond
6386 ((or (equal arg '(16))
6387 (memq last-command
6388 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6389 (setq this-command 'org-agenda-date-later-minutes)
6390 (org-agenda-date-later-minutes 1))
6391 ((or (equal arg '(4))
6392 (memq last-command
6393 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6394 (setq this-command 'org-agenda-date-later-hours)
6395 (org-agenda-date-later-hours 1))
6397 (org-agenda-date-later (prefix-numeric-value arg)))))
6399 (defun org-agenda-do-date-earlier (arg)
6400 (interactive "P")
6401 (cond
6402 ((or (equal arg '(16))
6403 (memq last-command
6404 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6405 (setq this-command 'org-agenda-date-earlier-minutes)
6406 (org-agenda-date-earlier-minutes 1))
6407 ((or (equal arg '(4))
6408 (memq last-command
6409 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6410 (setq this-command 'org-agenda-date-earlier-hours)
6411 (org-agenda-date-earlier-hours 1))
6413 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6415 (defun org-agenda-date-later (arg &optional what)
6416 "Change the date of this item to one day later."
6417 (interactive "p")
6418 (org-agenda-check-type t 'agenda 'timeline)
6419 (org-agenda-check-no-diary)
6420 (let* ((marker (or (org-get-at-bol 'org-marker)
6421 (org-agenda-error)))
6422 (buffer (marker-buffer marker))
6423 (pos (marker-position marker)))
6424 (org-with-remote-undo buffer
6425 (with-current-buffer buffer
6426 (widen)
6427 (goto-char pos)
6428 (if (not (org-at-timestamp-p))
6429 (error "Cannot find time stamp"))
6430 (org-timestamp-change arg (or what 'day)))
6431 (org-agenda-show-new-time marker org-last-changed-timestamp))
6432 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6434 (defun org-agenda-date-earlier (arg &optional what)
6435 "Change the date of this item to one day earlier."
6436 (interactive "p")
6437 (org-agenda-date-later (- arg) what))
6439 (defun org-agenda-date-later-minutes (arg)
6440 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6441 (interactive "p")
6442 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6443 (org-agenda-date-later arg 'minute))
6445 (defun org-agenda-date-earlier-minutes (arg)
6446 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6447 (interactive "p")
6448 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6449 (org-agenda-date-earlier arg 'minute))
6451 (defun org-agenda-date-later-hours (arg)
6452 "Change the time of this item, in hour steps."
6453 (interactive "p")
6454 (org-agenda-date-later arg 'hour))
6456 (defun org-agenda-date-earlier-hours (arg)
6457 "Change the time of this item, in hour steps."
6458 (interactive "p")
6459 (org-agenda-date-earlier arg 'hour))
6461 (defun org-agenda-show-new-time (marker stamp &optional prefix)
6462 "Show new date stamp via text properties."
6463 ;; We use text properties to make this undoable
6464 (let ((inhibit-read-only t)
6465 (buffer-invisibility-spec))
6466 (setq stamp (concat " " prefix " => " stamp))
6467 (save-excursion
6468 (goto-char (point-max))
6469 (while (not (bobp))
6470 (when (equal marker (org-get-at-bol 'org-marker))
6471 (org-move-to-column (- (window-width) (length stamp)) t)
6472 (org-agenda-fix-tags-filter-overlays-at (point))
6473 (if (featurep 'xemacs)
6474 ;; Use `duplicable' property to trigger undo recording
6475 (let ((ex (make-extent nil nil))
6476 (gl (make-glyph stamp)))
6477 (set-glyph-face gl 'secondary-selection)
6478 (set-extent-properties
6479 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6480 (insert-extent ex (1- (point)) (point-at-eol)))
6481 (add-text-properties
6482 (1- (point)) (point-at-eol)
6483 (list 'display (org-add-props stamp nil
6484 'face 'secondary-selection))))
6485 (beginning-of-line 1))
6486 (beginning-of-line 0)))))
6488 (defun org-agenda-date-prompt (arg)
6489 "Change the date of this item. Date is prompted for, with default today.
6490 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6491 be used to request time specification in the time stamp."
6492 (interactive "P")
6493 (org-agenda-check-type t 'agenda 'timeline)
6494 (org-agenda-check-no-diary)
6495 (let* ((marker (or (org-get-at-bol 'org-marker)
6496 (org-agenda-error)))
6497 (buffer (marker-buffer marker))
6498 (pos (marker-position marker)))
6499 (org-with-remote-undo buffer
6500 (with-current-buffer buffer
6501 (widen)
6502 (goto-char pos)
6503 (if (not (org-at-timestamp-p))
6504 (error "Cannot find time stamp"))
6505 (org-time-stamp arg))
6506 (org-agenda-show-new-time marker org-last-changed-timestamp))
6507 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6509 (defun org-agenda-schedule (arg)
6510 "Schedule the item at point."
6511 (interactive "P")
6512 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6513 (org-agenda-check-no-diary)
6514 (let* ((marker (or (org-get-at-bol 'org-marker)
6515 (org-agenda-error)))
6516 (type (marker-insertion-type marker))
6517 (buffer (marker-buffer marker))
6518 (pos (marker-position marker))
6519 (org-insert-labeled-timestamps-at-point nil)
6521 (set-marker-insertion-type marker t)
6522 (org-with-remote-undo buffer
6523 (with-current-buffer buffer
6524 (widen)
6525 (goto-char pos)
6526 (setq ts (org-schedule arg)))
6527 (org-agenda-show-new-time marker ts "S"))
6528 (message "Item scheduled for %s" ts)))
6530 (defun org-agenda-deadline (arg)
6531 "Schedule the item at point."
6532 (interactive "P")
6533 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6534 (org-agenda-check-no-diary)
6535 (let* ((marker (or (org-get-at-bol 'org-marker)
6536 (org-agenda-error)))
6537 (buffer (marker-buffer marker))
6538 (pos (marker-position marker))
6539 (org-insert-labeled-timestamps-at-point nil)
6541 (org-with-remote-undo buffer
6542 (with-current-buffer buffer
6543 (widen)
6544 (goto-char pos)
6545 (setq ts (org-deadline arg)))
6546 (org-agenda-show-new-time marker ts "D"))
6547 (message "Deadline for this item set to %s" ts)))
6549 (defun org-agenda-action ()
6550 "Select entry for agenda action, or execute an agenda action.
6551 This command prompts for another letter. Valid inputs are:
6553 m Mark the entry at point for an agenda action
6554 s Schedule the marked entry to the date at the cursor
6555 d Set the deadline of the marked entry to the date at the cursor
6556 r Call `org-remember' with cursor date as the default date
6557 SPC Show marked entry in other window
6558 TAB Visit marked entry in other window
6560 The cursor may be at a date in the calendar, or in the Org agenda."
6561 (interactive)
6562 (let (ans)
6563 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6564 (setq ans (read-char-exclusive))
6565 (cond
6566 ((equal ans ?m)
6567 ;; Mark this entry
6568 (if (eq major-mode 'org-agenda-mode)
6569 (let ((m (or (org-get-at-bol 'org-hd-marker)
6570 (org-get-at-bol 'org-marker))))
6571 (if m
6572 (progn
6573 (move-marker org-agenda-action-marker
6574 (marker-position m) (marker-buffer m))
6575 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6576 (error "Don't know which entry to mark")))
6577 (error "This command works only in the agenda")))
6578 ((equal ans ?s)
6579 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6580 ((equal ans ?d)
6581 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6582 ((equal ans ?r)
6583 (org-agenda-do-action '(org-remember) t))
6584 ((equal ans ?\ )
6585 (let ((cw (selected-window)))
6586 (org-switch-to-buffer-other-window
6587 (marker-buffer org-agenda-action-marker))
6588 (goto-char org-agenda-action-marker)
6589 (org-show-context 'agenda)
6590 (select-window cw)))
6591 ((equal ans ?\C-i)
6592 (org-switch-to-buffer-other-window
6593 (marker-buffer org-agenda-action-marker))
6594 (goto-char org-agenda-action-marker)
6595 (org-show-context 'agenda))
6596 (t (error "Invalid agenda action %c" ans)))))
6598 (defun org-agenda-do-action (form &optional current-buffer)
6599 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6600 (let ((org-overriding-default-time (org-get-cursor-date)))
6601 (if current-buffer
6602 (eval form)
6603 (if (not (marker-buffer org-agenda-action-marker))
6604 (error "No entry has been selected for agenda action")
6605 (with-current-buffer (marker-buffer org-agenda-action-marker)
6606 (save-excursion
6607 (save-restriction
6608 (widen)
6609 (goto-char org-agenda-action-marker)
6610 (eval form))))))))
6612 (defun org-agenda-clock-in (&optional arg)
6613 "Start the clock on the currently selected item."
6614 (interactive "P")
6615 (org-agenda-check-no-diary)
6616 (if (equal arg '(4))
6617 (org-clock-in arg)
6618 (let* ((marker (or (org-get-at-bol 'org-marker)
6619 (org-agenda-error)))
6620 (hdmarker (or (org-get-at-bol 'org-hd-marker)
6621 marker))
6622 (pos (marker-position marker))
6623 newhead)
6624 (org-with-remote-undo (marker-buffer marker)
6625 (with-current-buffer (marker-buffer marker)
6626 (widen)
6627 (goto-char pos)
6628 (org-show-context 'agenda)
6629 (org-show-entry)
6630 (org-cycle-hide-drawers 'children)
6631 (org-clock-in arg)
6632 (setq newhead (org-get-heading)))
6633 (org-agenda-change-all-lines newhead hdmarker)))))
6635 (defun org-agenda-clock-out (&optional arg)
6636 "Stop the currently running clock."
6637 (interactive "P")
6638 (unless (marker-buffer org-clock-marker)
6639 (error "No running clock"))
6640 (let ((marker (make-marker)) newhead)
6641 (org-with-remote-undo (marker-buffer org-clock-marker)
6642 (with-current-buffer (marker-buffer org-clock-marker)
6643 (save-excursion
6644 (save-restriction
6645 (widen)
6646 (goto-char org-clock-marker)
6647 (org-back-to-heading t)
6648 (move-marker marker (point))
6649 (org-clock-out)
6650 (setq newhead (org-get-heading))))))
6651 (org-agenda-change-all-lines newhead marker)
6652 (move-marker marker nil)))
6654 (defun org-agenda-clock-cancel (&optional arg)
6655 "Cancel the currently running clock."
6656 (interactive "P")
6657 (unless (marker-buffer org-clock-marker)
6658 (error "No running clock"))
6659 (org-with-remote-undo (marker-buffer org-clock-marker)
6660 (org-clock-cancel)))
6662 (defun org-agenda-diary-entry-in-org-file ()
6663 "Make a diary entry in the file `org-agenda-diary-file'."
6664 (let (d1 d2 char (text ""))
6665 (if (equal (buffer-name) "*Calendar*")
6666 (setq d1 (calendar-cursor-to-date t)
6667 d2 (car calendar-mark-ring))
6668 (setq d1 (calendar-gregorian-from-absolute
6669 (get-text-property (point) 'day))
6670 d2 (and (mark) (get-text-property (mark) 'day)
6671 (calendar-gregorian-from-absolute
6672 (get-text-property (mark) 'day)))))
6673 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
6674 (setq char (read-char-exclusive))
6675 (cond
6676 ((equal char ?d)
6677 (setq text (read-string "Day entry: "))
6678 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1))
6679 ((equal char ?a)
6680 (setq d1 (list (car d1) (nth 1 d1)
6681 (read-number (format "Reference year [%d]: " (nth 2 d1))
6682 (nth 2 d1))))
6683 (setq text (read-string "Anniversary (use %d to show years): "))
6684 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1))
6685 ((equal char ?b)
6686 (setq text (read-string "Block entry: "))
6687 (unless (and d1 d2 (not (equal d1 d2)))
6688 (error "No block of days selected"))
6689 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2))
6690 ((equal char ?j)
6691 (org-switch-to-buffer-other-window
6692 (find-file-noselect org-agenda-diary-file))
6693 (org-datetree-find-date-create d1)
6694 (org-reveal t))
6695 (t (error "Invalid selection character `%c'" char)))))
6697 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
6698 "Add a diary entry with TYPE to `org-agenda-diary-file'.
6699 If TEXT is not empty, it will become the headline of the new entry, and
6700 the resulting entry will not be shown. When TEXT is empty, switch to
6701 `org-agenda-diary-file' and let the user finish the entry there."
6702 (let ((cw (current-window-configuration)))
6703 (org-switch-to-buffer-other-window
6704 (find-file-noselect org-agenda-diary-file))
6705 (widen)
6706 (goto-char (point-min))
6707 (cond
6708 ((eq type 'anniversary)
6709 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
6710 (progn
6711 (or (org-on-heading-p t)
6712 (progn
6713 (outline-next-heading)
6714 (insert "* Anniversaries\n\n")
6715 (beginning-of-line -1)))))
6716 (outline-next-heading)
6717 (org-back-over-empty-lines)
6718 (backward-char 1)
6719 (insert "\n")
6720 (require 'diary-lib)
6721 (let ((calendar-date-display-form
6722 (if (if (boundp 'calendar-date-style)
6723 (eq calendar-date-style 'european)
6724 european-calendar-style) ; Emacs 22
6725 '(day " " month " " year)
6726 '(month " " day " " year))))
6728 (insert (format "%%%%(diary-anniversary %s) %s"
6729 (calendar-date-string d1 nil t) text))))
6730 ((eq type 'day)
6731 (require 'org-datetree)
6732 (org-datetree-find-date-create d1)
6733 (org-agenda-insert-diary-make-new-entry text)
6734 (org-insert-time-stamp (org-time-from-absolute
6735 (calendar-absolute-from-gregorian d1)))
6736 (end-of-line 0))
6737 ((eq type 'block)
6738 (if (> (calendar-absolute-from-gregorian d1)
6739 (calendar-absolute-from-gregorian d2))
6740 (setq d1 (prog1 d2 (setq d2 d1))))
6741 (require 'org-datetree)
6742 (org-datetree-find-date-create d1)
6743 (org-agenda-insert-diary-make-new-entry text)
6744 (org-insert-time-stamp (org-time-from-absolute
6745 (calendar-absolute-from-gregorian d1)))
6746 (insert "--")
6747 (org-insert-time-stamp (org-time-from-absolute
6748 (calendar-absolute-from-gregorian d2)))
6749 (end-of-line 0)))
6750 (if (string-match "\\S-" text)
6751 (progn
6752 (set-window-configuration cw)
6753 (message "%s entry added to %s"
6754 (capitalize (symbol-name type))
6755 (abbreviate-file-name org-agenda-diary-file)))
6756 (org-reveal t)
6757 (message "Please finish entry here"))))
6759 (defun org-agenda-insert-diary-make-new-entry (text)
6760 "Make new entry as last child of current entry.
6761 Add TEXT as headline, and position the cursor in the second line so that
6762 a timestamp can be added there."
6763 (let ((org-show-following-heading t)
6764 (org-show-siblings t)
6765 (org-show-hierarchy-above t)
6766 (org-show-entry-below t)
6767 col)
6768 (outline-next-heading)
6769 (org-back-over-empty-lines)
6770 (or (looking-at "[ \t]*$")
6771 (progn (insert "\n") (backward-char 1)))
6772 (org-insert-heading)
6773 (org-do-demote)
6774 (setq col (current-column))
6775 (insert text "\n")
6776 (if org-adapt-indentation (org-indent-to-column col))
6777 (let ((org-show-following-heading t)
6778 (org-show-siblings t)
6779 (org-show-hierarchy-above t)
6780 (org-show-entry-below t))
6781 (org-show-context))))
6783 (defun org-agenda-diary-entry ()
6784 "Make a diary entry, like the `i' command from the calendar.
6785 All the standard commands work: block, weekly etc.
6786 When `org-agenda-diary-file' points to a file,
6787 `org-agenda-diary-entry-in-org-file' is called instead to create
6788 entries in that Org-mode file."
6789 (interactive)
6790 (org-agenda-check-type t 'agenda 'timeline)
6791 (if (not (eq org-agenda-diary-file 'diary-file))
6792 (org-agenda-diary-entry-in-org-file)
6793 (require 'diary-lib)
6794 (let* ((char (progn
6795 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6796 (read-char-exclusive)))
6797 (cmd (cdr (assoc char
6798 '((?d . insert-diary-entry)
6799 (?w . insert-weekly-diary-entry)
6800 (?m . insert-monthly-diary-entry)
6801 (?y . insert-yearly-diary-entry)
6802 (?a . insert-anniversary-diary-entry)
6803 (?b . insert-block-diary-entry)
6804 (?c . insert-cyclic-diary-entry)))))
6805 (oldf (symbol-function 'calendar-cursor-to-date))
6806 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6807 (point (point))
6808 (mark (or (mark t) (point))))
6809 (unless cmd
6810 (error "No command associated with <%c>" char))
6811 (unless (and (get-text-property point 'day)
6812 (or (not (equal ?b char))
6813 (get-text-property mark 'day)))
6814 (error "Don't know which date to use for diary entry"))
6815 ;; We implement this by hacking the `calendar-cursor-to-date' function
6816 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6817 (let ((calendar-mark-ring
6818 (list (calendar-gregorian-from-absolute
6819 (or (get-text-property mark 'day)
6820 (get-text-property point 'day))))))
6821 (unwind-protect
6822 (progn
6823 (fset 'calendar-cursor-to-date
6824 (lambda (&optional error dummy)
6825 (calendar-gregorian-from-absolute
6826 (get-text-property point 'day))))
6827 (call-interactively cmd))
6828 (fset 'calendar-cursor-to-date oldf))))))
6830 (defun org-agenda-execute-calendar-command (cmd)
6831 "Execute a calendar command from the agenda, with the date associated to
6832 the cursor position."
6833 (org-agenda-check-type t 'agenda 'timeline)
6834 (require 'diary-lib)
6835 (unless (get-text-property (point) 'day)
6836 (error "Don't know which date to use for calendar command"))
6837 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6838 (point (point))
6839 (date (calendar-gregorian-from-absolute
6840 (get-text-property point 'day)))
6841 ;; the following 2 vars are needed in the calendar
6842 (displayed-month (car date))
6843 (displayed-year (nth 2 date)))
6844 (unwind-protect
6845 (progn
6846 (fset 'calendar-cursor-to-date
6847 (lambda (&optional error dummy)
6848 (calendar-gregorian-from-absolute
6849 (get-text-property point 'day))))
6850 (call-interactively cmd))
6851 (fset 'calendar-cursor-to-date oldf))))
6853 (defun org-agenda-phases-of-moon ()
6854 "Display the phases of the moon for the 3 months around the cursor date."
6855 (interactive)
6856 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6858 (defun org-agenda-holidays ()
6859 "Display the holidays for the 3 months around the cursor date."
6860 (interactive)
6861 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6863 (defvar calendar-longitude)
6864 (defvar calendar-latitude)
6865 (defvar calendar-location-name)
6867 (defun org-agenda-sunrise-sunset (arg)
6868 "Display sunrise and sunset for the cursor date.
6869 Latitude and longitude can be specified with the variables
6870 `calendar-latitude' and `calendar-longitude'. When called with prefix
6871 argument, latitude and longitude will be prompted for."
6872 (interactive "P")
6873 (require 'solar)
6874 (let ((calendar-longitude (if arg nil calendar-longitude))
6875 (calendar-latitude (if arg nil calendar-latitude))
6876 (calendar-location-name
6877 (if arg "the given coordinates" calendar-location-name)))
6878 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6880 (defun org-agenda-goto-calendar ()
6881 "Open the Emacs calendar with the date at the cursor."
6882 (interactive)
6883 (org-agenda-check-type t 'agenda 'timeline)
6884 (let* ((day (or (get-text-property (point) 'day)
6885 (error "Don't know which date to open in calendar")))
6886 (date (calendar-gregorian-from-absolute day))
6887 (calendar-move-hook nil)
6888 (calendar-view-holidays-initially-flag nil)
6889 (calendar-view-diary-initially-flag nil)
6890 (view-calendar-holidays-initially nil)
6891 (view-diary-entries-initially nil))
6892 (calendar)
6893 (calendar-goto-date date)))
6895 ;;;###autoload
6896 (defun org-calendar-goto-agenda ()
6897 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6898 This is a command that has to be installed in `calendar-mode-map'."
6899 (interactive)
6900 (org-agenda-list nil (calendar-absolute-from-gregorian
6901 (calendar-cursor-to-date))
6902 nil))
6904 (defun org-agenda-convert-date ()
6905 (interactive)
6906 (org-agenda-check-type t 'agenda 'timeline)
6907 (let ((day (get-text-property (point) 'day))
6908 date s)
6909 (unless day
6910 (error "Don't know which date to convert"))
6911 (setq date (calendar-gregorian-from-absolute day))
6912 (setq s (concat
6913 "Gregorian: " (calendar-date-string date) "\n"
6914 "ISO: " (calendar-iso-date-string date) "\n"
6915 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6916 "Julian: " (calendar-julian-date-string date) "\n"
6917 "Astron. JD: " (calendar-astro-date-string date)
6918 " (Julian date number at noon UTC)\n"
6919 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6920 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6921 "French: " (calendar-french-date-string date) "\n"
6922 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6923 "Mayan: " (calendar-mayan-date-string date) "\n"
6924 "Coptic: " (calendar-coptic-date-string date) "\n"
6925 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6926 "Persian: " (calendar-persian-date-string date) "\n"
6927 "Chinese: " (calendar-chinese-date-string date) "\n"))
6928 (with-output-to-temp-buffer "*Dates*"
6929 (princ s))
6930 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6932 ;;; Bulk commands
6934 (defvar org-agenda-bulk-marked-entries nil
6935 "List of markers that refer to marked entries in the agenda.")
6937 (defun org-agenda-bulk-marked-p ()
6938 (eq (get-char-property (point-at-bol) 'type)
6939 'org-marked-entry-overlay))
6941 (defun org-agenda-bulk-mark ()
6942 "Mark the entry at point for future bulk action."
6943 (interactive)
6944 (org-agenda-check-no-diary)
6945 (let* ((m (org-get-at-bol 'org-hd-marker))
6947 (unless (org-agenda-bulk-marked-p)
6948 (unless m (error "Nothing to mark at point"))
6949 (push m org-agenda-bulk-marked-entries)
6950 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
6951 (org-overlay-display ov "> "
6952 (org-get-todo-face "TODO")
6953 'evaporate)
6954 (org-overlay-put ov 'type 'org-marked-entry-overlay))
6955 (beginning-of-line 2)
6956 (message "%d entries marked for bulk action"
6957 (length org-agenda-bulk-marked-entries))))
6959 (defun org-agenda-bulk-unmark ()
6960 "Unmark the entry at point for future bulk action."
6961 (interactive)
6962 (when (org-agenda-bulk-marked-p)
6963 (org-agenda-bulk-remove-overlays
6964 (point-at-bol) (+ 2 (point-at-bol)))
6965 (setq org-agenda-bulk-marked-entries
6966 (delete (org-get-at-bol 'org-hd-marker)
6967 org-agenda-bulk-marked-entries)))
6968 (beginning-of-line 2)
6969 (message "%d entries marked for bulk action"
6970 (length org-agenda-bulk-marked-entries)))
6972 (defun org-agenda-bulk-toggle ()
6973 "Toggle marking the entry at point for bulk action."
6974 (interactive)
6975 (if (org-agenda-bulk-marked-p)
6976 (org-agenda-bulk-unmark)
6977 (org-agenda-bulk-mark)))
6979 (defun org-agenda-bulk-remove-overlays (&optional beg end)
6980 "Remove the mark overlays between BEG and END in the agenda buffer.
6981 BEG and END default to the buffer limits.
6983 This only removes the overlays, it does not remove the markers
6984 from the list in `org-agenda-bulk-marked-entries'."
6985 (interactive)
6986 (mapc (lambda (ov)
6987 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
6988 (org-delete-overlay ov)))
6989 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
6991 (defun org-agenda-bulk-remove-all-marks ()
6992 "Remove all marks in the agenda buffer.
6993 This will remove the markers, and the overlays."
6994 (interactive)
6995 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
6996 (setq org-agenda-bulk-marked-entries nil)
6997 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
6999 (defun org-agenda-bulk-action ()
7000 "Execute an remote-editing action on all marked entries."
7001 (interactive)
7002 (unless org-agenda-bulk-marked-entries
7003 (error "No entries are marked"))
7004 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
7005 (let* ((action (read-char-exclusive))
7006 (entries (reverse org-agenda-bulk-marked-entries))
7007 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
7008 (cond
7009 ((equal action ?$)
7010 (setq cmd '(org-agenda-archive)))
7012 ((equal action ?A)
7013 (setq cmd '(org-agenda-archive-to-archive-sibling)))
7015 ((member action '(?r ?w))
7016 (setq rfloc (org-refile-get-location
7017 "Refile to: "
7018 (marker-buffer (car org-agenda-bulk-marked-entries))
7019 org-refile-allow-creating-parent-nodes))
7020 (setcar (nthcdr 3 rfloc)
7021 (move-marker (make-marker) (nth 3 rfloc)
7022 (or (get-file-buffer (nth 1 rfloc))
7023 (find-buffer-visiting (nth 1 rfloc))
7024 (error "This should not happen"))))
7026 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
7028 ((equal action ?t)
7029 (setq state (org-icompleting-read
7030 "Todo state: "
7031 (with-current-buffer (marker-buffer (car entries))
7032 (mapcar 'list org-todo-keywords-1))))
7033 (setq cmd `(let ((org-inhibit-blocking t)
7034 (org-inhibit-logging 'note))
7035 (org-agenda-todo ,state))))
7037 ((memq action '(?- ?+))
7038 (setq tag (org-icompleting-read
7039 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7040 (with-current-buffer (marker-buffer (car entries))
7041 (delq nil
7042 (mapcar (lambda (x)
7043 (if (stringp (car x)) x)) org-tag-alist)))))
7044 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7046 ((memq action '(?s ?d))
7047 (let* ((date (org-read-date
7048 nil nil nil
7049 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
7050 (ans org-read-date-final-answer)
7051 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7052 (setq cmd `(let* ((bound (fboundp 'read-string))
7053 (old (and bound (symbol-function 'read-string))))
7054 (unwind-protect
7055 (progn
7056 (fset 'read-string (lambda (&rest ignore) ,ans))
7057 (call-interactively ',c1))
7058 (if bound
7059 (fset 'read-string old)
7060 (fmakunbound 'read-string)))))))
7061 (t (error "Invalid bulk action")))
7063 ;; Sort the markers, to make sure that parents are handled before children
7064 (setq entries (sort entries
7065 (lambda (a b)
7066 (cond
7067 ((equal (marker-buffer a) (marker-buffer b))
7068 (< (marker-position a) (marker-position b)))
7070 (string< (buffer-name (marker-buffer a))
7071 (buffer-name (marker-buffer b))))))))
7073 ;; Now loop over all markers and apply cmd
7074 (while (setq e (pop entries))
7075 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7076 (if (not pos)
7077 (progn (message "Skipping removed entry at %s" e)
7078 (setq cntskip (1+ cntskip)))
7079 (goto-char pos)
7080 (eval cmd)
7081 (setq org-agenda-bulk-marked-entries
7082 (delete e org-agenda-bulk-marked-entries))
7083 (setq cnt (1+ cnt))))
7084 (setq org-agenda-bulk-marked-entries nil)
7085 (org-agenda-bulk-remove-all-marks)
7086 (message "Acted on %d entries%s"
7088 (if (= cntskip 0)
7090 (format ", skipped %d (disappeared before their turn)"
7091 cntskip)))))
7093 ;;; Flagging notes
7095 (defun org-agenda-show-the-flagging-note ()
7096 "Display the flagging note in the other window.
7097 When called a second time in direct sequence, offer to remove the FLAGGING
7098 tag and (if present) the flagging note."
7099 (interactive)
7100 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7101 (win (selected-window))
7102 note heading newhead)
7103 (unless hdmarker
7104 (error "No linked entry at point"))
7105 (if (and (eq this-command last-command)
7106 (y-or-n-p "Unflag and remove any flagging note? "))
7107 (progn
7108 (org-agenda-remove-flag hdmarker)
7109 (let ((win (get-buffer-window "*Flagging Note*")))
7110 (and win (delete-window win)))
7111 (message "Entry unflaged"))
7112 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7113 (unless note
7114 (error "No flagging note"))
7115 (org-kill-new note)
7116 (org-switch-to-buffer-other-window "*Flagging Note*")
7117 (erase-buffer)
7118 (insert note)
7119 (goto-char (point-min))
7120 (while (re-search-forward "\\\\n" nil t)
7121 (replace-match "\n" t t))
7122 (goto-char (point-min))
7123 (select-window win)
7124 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7126 (defun org-agenda-remove-flag (marker)
7127 "Remove the FLAGGED tag and any flagging note in the entry."
7128 (let (newhead)
7129 (org-with-point-at marker
7130 (org-toggle-tag "FLAGGED" 'off)
7131 (org-entry-delete nil "THEFLAGGINGNOTE")
7132 (setq newhead (org-get-heading)))
7133 (org-agenda-change-all-lines newhead marker)
7134 (message "Entry unflaged")))
7136 (defun org-agenda-get-any-marker (&optional pos)
7137 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7138 (get-text-property (or pos (point-at-bol)) 'org-marker)))
7140 ;;; Appointment reminders
7142 (defvar appt-time-msg-list)
7144 ;;;###autoload
7145 (defun org-agenda-to-appt (&optional refresh filter)
7146 "Activate appointments found in `org-agenda-files'.
7147 With a \\[universal-argument] prefix, refresh the list of
7148 appointments.
7150 If FILTER is t, interactively prompt the user for a regular
7151 expression, and filter out entries that don't match it.
7153 If FILTER is a string, use this string as a regular expression
7154 for filtering entries out.
7156 FILTER can also be an alist with the car of each cell being
7157 either 'headline or 'category. For example:
7159 '((headline \"IMPORTANT\")
7160 (category \"Work\"))
7162 will only add headlines containing IMPORTANT or headlines
7163 belonging to the \"Work\" category."
7164 (interactive "P")
7165 (require 'calendar)
7166 (if refresh (setq appt-time-msg-list nil))
7167 (if (eq filter t)
7168 (setq filter (read-from-minibuffer "Regexp filter: ")))
7169 (let* ((cnt 0) ; count added events
7170 (org-agenda-new-buffers nil)
7171 (org-deadline-warning-days 0)
7172 (today (org-date-to-gregorian
7173 (time-to-days (current-time))))
7174 (org-agenda-restrict nil)
7175 (files (org-agenda-files 'unrestricted)) entries file)
7176 ;; Get all entries which may contain an appt
7177 (org-prepare-agenda-buffers files)
7178 (while (setq file (pop files))
7179 (setq entries
7180 (append entries
7181 (org-agenda-get-day-entries
7182 file today :timestamp :scheduled :deadline))))
7183 (setq entries (delq nil entries))
7184 ;; Map thru entries and find if we should filter them out
7185 (mapc
7186 (lambda(x)
7187 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
7188 (cat (get-text-property 1 'org-category x))
7189 (tod (get-text-property 1 'time-of-day x))
7190 (ok (or (null filter)
7191 (and (stringp filter) (string-match filter evt))
7192 (and (listp filter)
7193 (or (string-match
7194 (cadr (assoc 'category filter)) cat)
7195 (string-match
7196 (cadr (assoc 'headline filter)) evt))))))
7197 ;; FIXME: Shall we remove text-properties for the appt text?
7198 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
7199 (when (and ok tod)
7200 (setq tod (concat "00" (number-to-string tod))
7201 tod (when (string-match
7202 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
7203 (concat (match-string 1 tod) ":"
7204 (match-string 2 tod))))
7205 (appt-add tod evt)
7206 (setq cnt (1+ cnt))))) entries)
7207 (org-release-buffers org-agenda-new-buffers)
7208 (if (eq cnt 0)
7209 (message "No event to add")
7210 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
7212 (defun org-agenda-todayp (date)
7213 "Does DATE mean today, when considering `org-extend-today-until'?"
7214 (let (today h)
7215 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
7216 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
7217 (setq h (nth 2 (decode-time (current-time))))
7218 (or (and (>= h org-extend-today-until)
7219 (= date today))
7220 (and (< h org-extend-today-until)
7221 (= date (1- today))))))
7223 (provide 'org-agenda)
7225 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
7227 ;;; org-agenda.el ends here