Agenda: Make sure diary items are searched for a time.
[org-mode.git] / lisp / org-agenda.el
blobd013e5c34c1628e605bc4dead541ed37465597ee
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.24trans
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-check-holidays "holidays" (date))
44 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
45 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
47 (declare-function calendar-french-date-string "cal-french" (&optional date))
48 (declare-function calendar-goto-date "cal-move" (date))
49 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
50 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
51 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
52 (declare-function calendar-iso-from-absolute "cal-iso" (date))
53 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
54 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
55 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
56 (declare-function org-columns-quit "org-colview" ())
57 (defvar calendar-mode-map)
59 ;; Defined somewhere in this file, but used before definition.
60 (defvar org-agenda-buffer-name)
61 (defvar org-agenda-overriding-header)
62 (defvar entry)
63 (defvar date)
64 (defvar org-agenda-undo-list)
65 (defvar org-agenda-pending-undo-list)
66 (defvar original-date) ; dynamically scoped, calendar.el does scope this
68 (defcustom org-agenda-confirm-kill 1
69 "When set, remote killing from the agenda buffer needs confirmation.
70 When t, a confirmation is always needed. When a number N, confirmation is
71 only needed when the text to be killed contains more than N non-white lines."
72 :group 'org-agenda
73 :type '(choice
74 (const :tag "Never" nil)
75 (const :tag "Always" t)
76 (number :tag "When more than N lines")))
78 (defcustom org-agenda-compact-blocks nil
79 "Non-nil means, make the block agenda more compact.
80 This is done by leaving out unnecessary lines."
81 :group 'org-agenda
82 :type 'boolean)
84 (defcustom org-agenda-block-separator ?=
85 "The separator between blocks in the agenda.
86 If this is a string, it will be used as the separator, with a newline added.
87 If it is a character, it will be repeated to fill the window width."
88 :group 'org-agenda
89 :type '(choice
90 (character)
91 (string)))
93 (defgroup org-agenda-export nil
94 "Options concerning exporting agenda views in Org-mode."
95 :tag "Org Agenda Export"
96 :group 'org-agenda)
98 (defcustom org-agenda-with-colors t
99 "Non-nil means, use colors in agenda views."
100 :group 'org-agenda-export
101 :type 'boolean)
103 (defcustom org-agenda-exporter-settings nil
104 "Alist of variable/value pairs that should be active during agenda export.
105 This is a good place to set options for ps-print and for htmlize."
106 :group 'org-agenda-export
107 :type '(repeat
108 (list
109 (variable)
110 (sexp :tag "Value"))))
112 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
113 "Hook run in temporary buffer before writing it to an export file.
114 A useful function is `org-agenda-add-entry-text'."
115 :group 'org-agenda-export
116 :type 'hook
117 :options '(org-agenda-add-entry-text))
119 (defcustom org-agenda-add-entry-text-maxlines 0
120 "Maximum number of entry text lines to be added to agenda.
121 This is only relevant when `org-agenda-add-entry-text' is part of
122 `org-agenda-before-write-hook', which it is by default.
123 When this is 0, nothing will happen. When it is greater than 0, it
124 specifies the maximum number of lines that will be added for each entry
125 that is listed in the agenda view."
126 :group 'org-agenda
127 :type 'integer)
129 (defcustom org-agenda-add-entry-text-descriptive-links t
130 "Non-nil means, export org-links as descriptive links in agenda added text.
131 This variable applies to the text added to the agenda when
132 `org-agenda-add-entry-text-maxlines' is larger than 0.
133 When this variable nil, the URL will (also) be shown."
134 :group 'org-agenda
135 :type 'boolean)
137 (defcustom org-agenda-export-html-style ""
138 "The style specification for exported HTML Agenda files.
139 If this variable contains a string, it will replace the default <style>
140 section as produced by `htmlize'.
141 Since there are different ways of setting style information, this variable
142 needs to contain the full HTML structure to provide a style, including the
143 surrounding HTML tags. The style specifications should include definitions
144 the fonts used by the agenda, here is an example:
146 <style type=\"text/css\">
147 p { font-weight: normal; color: gray; }
148 .org-agenda-structure {
149 font-size: 110%;
150 color: #003399;
151 font-weight: 600;
153 .org-todo {
154 color: #cc6666;
155 font-weight: bold;
157 .org-agenda-done {
158 color: #339933;
160 .org-done {
161 color: #339933;
163 .title { text-align: center; }
164 .todo, .deadline { color: red; }
165 .done { color: green; }
166 </style>
168 or, if you want to keep the style in a file,
170 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
172 As the value of this option simply gets inserted into the HTML <head> header,
173 you can \"misuse\" it to also add other text to the header. However,
174 <style>...</style> is required, if not present the variable will be ignored."
175 :group 'org-agenda-export
176 :group 'org-export-html
177 :type 'string)
179 (defgroup org-agenda-custom-commands nil
180 "Options concerning agenda views in Org-mode."
181 :tag "Org Agenda Custom Commands"
182 :group 'org-agenda)
184 (defconst org-sorting-choice
185 '(choice
186 (const time-up) (const time-down)
187 (const category-keep) (const category-up) (const category-down)
188 (const tag-down) (const tag-up)
189 (const priority-up) (const priority-down)
190 (const todo-state-up) (const todo-state-down)
191 (const effort-up) (const effort-down))
192 "Sorting choices.")
194 (defconst org-agenda-custom-commands-local-options
195 `(repeat :tag "Local settings for this command. Remember to quote values"
196 (choice :tag "Setting"
197 (list :tag "Heading for this block"
198 (const org-agenda-overriding-header)
199 (string :tag "Headline"))
200 (list :tag "Files to be searched"
201 (const org-agenda-files)
202 (list
203 (const :format "" quote)
204 (repeat (file))))
205 (list :tag "Sorting strategy"
206 (const org-agenda-sorting-strategy)
207 (list
208 (const :format "" quote)
209 (repeat
210 ,org-sorting-choice)))
211 (list :tag "Prefix format"
212 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
213 (string))
214 (list :tag "Number of days in agenda"
215 (const org-agenda-ndays)
216 (integer :value 1))
217 (list :tag "Fixed starting date"
218 (const org-agenda-start-day)
219 (string :value "2007-11-01"))
220 (list :tag "Start on day of week"
221 (const org-agenda-start-on-weekday)
222 (choice :value 1
223 (const :tag "Today" nil)
224 (number :tag "Weekday No.")))
225 (list :tag "Include data from diary"
226 (const org-agenda-include-diary)
227 (boolean))
228 (list :tag "Deadline Warning days"
229 (const org-deadline-warning-days)
230 (integer :value 1))
231 (list :tag "Tags filter preset"
232 (const org-agenda-filter-preset)
233 (list
234 (const :format "" quote)
235 (repeat
236 (string :tag "+tag or -tag"))))
237 (list :tag "Standard skipping condition"
238 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
239 (const org-agenda-skip-function)
240 (list
241 (const :format "" quote)
242 (list
243 (choice
244 :tag "Skipping range"
245 (const :tag "Skip entry" org-agenda-skip-entry-if)
246 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
247 (repeat :inline t :tag "Conditions for skipping"
248 (choice
249 :tag "Condition type"
250 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
251 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
252 (const :tag "scheduled" 'scheduled)
253 (const :tag "not scheduled" 'notscheduled)
254 (const :tag "deadline" 'deadline)
255 (const :tag "no deadline" 'notdeadline)
256 (const :tag "timestamp" 'timestamp)
257 (const :tag "no timestamp" 'nottimestamp))))))
258 (list :tag "Non-standard skipping condition"
259 :value (org-agenda-skip-function)
260 (const org-agenda-skip-function)
261 (sexp :tag "Function or form (quoted!)"))
262 (list :tag "Any variable"
263 (variable :tag "Variable")
264 (sexp :tag "Value (sexp)"))))
265 "Selection of examples for agenda command settings.
266 This will be spliced into the custom type of
267 `org-agenda-custom-commands'.")
270 (defcustom org-agenda-custom-commands nil
271 "Custom commands for the agenda.
272 These commands will be offered on the splash screen displayed by the
273 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
275 (key desc type match settings files)
277 key The key (one or more characters as a string) to be associated
278 with the command.
279 desc A description of the command, when omitted or nil, a default
280 description is built using MATCH.
281 type The command type, any of the following symbols:
282 agenda The daily/weekly agenda.
283 todo Entries with a specific TODO keyword, in all agenda files.
284 search Entries containing search words entry or headline.
285 tags Tags/Property/TODO match in all agenda files.
286 tags-todo Tags/P/T match in all agenda files, TODO entries only.
287 todo-tree Sparse tree of specific TODO keyword in *current* file.
288 tags-tree Sparse tree with all tags matches in *current* file.
289 occur-tree Occur sparse tree for *current* file.
290 ... A user-defined function.
291 match What to search for:
292 - a single keyword for TODO keyword searches
293 - a tags match expression for tags searches
294 - a word search expression for text searches.
295 - a regular expression for occur searches
296 For all other commands, this should be the empty string.
297 settings A list of option settings, similar to that in a let form, so like
298 this: ((opt1 val1) (opt2 val2) ...). The values will be
299 evaluated at the moment of execution, so quote them when needed.
300 files A list of files file to write the produced agenda buffer to
301 with the command `org-store-agenda-views'.
302 If a file name ends in \".html\", an HTML version of the buffer
303 is written out. If it ends in \".ps\", a postscript version is
304 produced. Otherwise, only the plain text is written to the file.
306 You can also define a set of commands, to create a composite agenda buffer.
307 In this case, an entry looks like this:
309 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
311 where
313 desc A description string to be displayed in the dispatcher menu.
314 cmd An agenda command, similar to the above. However, tree commands
315 are no allowed, but instead you can get agenda and global todo list.
316 So valid commands for a set are:
317 (agenda \"\" settings)
318 (alltodo \"\" settings)
319 (stuck \"\" settings)
320 (todo \"match\" settings files)
321 (search \"match\" settings files)
322 (tags \"match\" settings files)
323 (tags-todo \"match\" settings files)
325 Each command can carry a list of options, and another set of options can be
326 given for the whole set of commands. Individual command options take
327 precedence over the general options.
329 When using several characters as key to a command, the first characters
330 are prefix commands. For the dispatcher to display useful information, you
331 should provide a description for the prefix, like
333 (setq org-agenda-custom-commands
334 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
335 (\"hl\" tags \"+HOME+Lisa\")
336 (\"hp\" tags \"+HOME+Peter\")
337 (\"hk\" tags \"+HOME+Kim\")))"
338 :group 'org-agenda-custom-commands
339 :type `(repeat
340 (choice :value ("x" "Describe command here" tags "" nil)
341 (list :tag "Single command"
342 (string :tag "Access Key(s) ")
343 (option (string :tag "Description"))
344 (choice
345 (const :tag "Agenda" agenda)
346 (const :tag "TODO list" alltodo)
347 (const :tag "Search words" search)
348 (const :tag "Stuck projects" stuck)
349 (const :tag "Tags/Property match (all agenda files)" tags)
350 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
351 (const :tag "TODO keyword search (all agenda files)" todo)
352 (const :tag "Tags sparse tree (current buffer)" tags-tree)
353 (const :tag "TODO keyword tree (current buffer)" todo-tree)
354 (const :tag "Occur tree (current buffer)" occur-tree)
355 (sexp :tag "Other, user-defined function"))
356 (string :tag "Match (only for some commands)")
357 ,org-agenda-custom-commands-local-options
358 (option (repeat :tag "Export" (file :tag "Export to"))))
359 (list :tag "Command series, all agenda files"
360 (string :tag "Access Key(s)")
361 (string :tag "Description ")
362 (repeat :tag "Component"
363 (choice
364 (list :tag "Agenda"
365 (const :format "" agenda)
366 (const :tag "" :format "" "")
367 ,org-agenda-custom-commands-local-options)
368 (list :tag "TODO list (all keywords)"
369 (const :format "" alltodo)
370 (const :tag "" :format "" "")
371 ,org-agenda-custom-commands-local-options)
372 (list :tag "Search words"
373 (const :format "" search)
374 (string :tag "Match")
375 ,org-agenda-custom-commands-local-options)
376 (list :tag "Stuck projects"
377 (const :format "" stuck)
378 (const :tag "" :format "" "")
379 ,org-agenda-custom-commands-local-options)
380 (list :tag "Tags search"
381 (const :format "" tags)
382 (string :tag "Match")
383 ,org-agenda-custom-commands-local-options)
384 (list :tag "Tags search, TODO entries only"
385 (const :format "" tags-todo)
386 (string :tag "Match")
387 ,org-agenda-custom-commands-local-options)
388 (list :tag "TODO keyword search"
389 (const :format "" todo)
390 (string :tag "Match")
391 ,org-agenda-custom-commands-local-options)
392 (list :tag "Other, user-defined function"
393 (symbol :tag "function")
394 (string :tag "Match")
395 ,org-agenda-custom-commands-local-options)))
397 (repeat :tag "Settings for entire command set"
398 (list (variable :tag "Any variable")
399 (sexp :tag "Value")))
400 (option (repeat :tag "Export" (file :tag "Export to"))))
401 (cons :tag "Prefix key documentation"
402 (string :tag "Access Key(s)")
403 (string :tag "Description ")))))
405 (defcustom org-agenda-query-register ?o
406 "The register holding the current query string.
407 The purpose of this is that if you construct a query string interactively,
408 you can then use it to define a custom command."
409 :group 'org-agenda-custom-commands
410 :type 'character)
412 (defcustom org-stuck-projects
413 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
414 "How to identify stuck projects.
415 This is a list of four items:
416 1. A tags/todo/property matcher string that is used to identify a project.
417 See the manual for a description of tag and property searches.
418 The entire tree below a headline matched by this is considered one project.
419 2. A list of TODO keywords identifying non-stuck projects.
420 If the project subtree contains any headline with one of these todo
421 keywords, the project is considered to be not stuck. If you specify
422 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
423 3. A list of tags identifying non-stuck projects.
424 If the project subtree contains any headline with one of these tags,
425 the project is considered to be not stuck. If you specify \"*\" as
426 a tag, any tag will mark the project unstuck.
427 4. An arbitrary regular expression matching non-stuck projects.
429 If the project turns out to be not stuck, search continues also in the
430 subtree to see if any of the subtasks have project status.
432 See also the variable `org-tags-match-list-sublevels' which applies
433 to projects matched by this search as well.
435 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
436 or `C-c a #' to produce the list."
437 :group 'org-agenda-custom-commands
438 :type '(list
439 (string :tag "Tags/TODO match to identify a project")
440 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
441 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
442 (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
444 (defcustom org-agenda-filter-effort-default-operator "<"
445 "The default operator for effort estimate filtering.
446 If you select an effort estimate limit without first pressing an operator,
447 this one will be used."
448 :group 'org-agenda-custom-commands
449 :type '(choice (const :tag "less or equal" "<")
450 (const :tag "greater or equal"">")
451 (const :tag "equal" "=")))
453 (defgroup org-agenda-skip nil
454 "Options concerning skipping parts of agenda files."
455 :tag "Org Agenda Skip"
456 :group 'org-agenda)
457 (defgroup org-agenda-daily/weekly nil
458 "Options concerning the daily/weekly agenda."
459 :tag "Org Agenda Daily/Weekly"
460 :group 'org-agenda)
461 (defgroup org-agenda-todo-list nil
462 "Options concerning the global todo list agenda view."
463 :tag "Org Agenda Todo List"
464 :group 'org-agenda)
465 (defgroup org-agenda-match-view nil
466 "Options concerning the general tags/property/todo match agenda view."
467 :tag "Org Agenda Match View"
468 :group 'org-agenda)
470 (defvar org-agenda-archives-mode nil
471 "Non-nil means, the agenda will include archived items.
472 If this is the symbol `trees', trees in the selected agenda scope
473 that are marked with the ARCHIVE tag will be included anyway. When this is
474 t, also all archive files associated with the current selection of agenda
475 files will be included.")
477 (defcustom org-agenda-skip-comment-trees t
478 "Non-nil means, skip trees that start with the COMMENT keyword.
479 When nil, these trees are also scanned by agenda commands."
480 :group 'org-agenda-skip
481 :type 'boolean)
483 (defcustom org-agenda-todo-list-sublevels t
484 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
485 When nil, the sublevels of a TODO entry are not checked, resulting in
486 potentially much shorter TODO lists."
487 :group 'org-agenda-skip
488 :group 'org-agenda-todo-list
489 :type 'boolean)
491 (defcustom org-agenda-todo-ignore-with-date nil
492 "Non-nil means, don't show entries with a date in the global todo list.
493 You can use this if you prefer to mark mere appointments with a TODO keyword,
494 but don't want them to show up in the TODO list.
495 When this is set, it also covers deadlines and scheduled items, the settings
496 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
497 will be ignored.
498 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
499 :group 'org-agenda-skip
500 :group 'org-agenda-todo-list
501 :type 'boolean)
503 (defcustom org-agenda-todo-ignore-scheduled nil
504 "Non-nil means, don't show scheduled entries in the global todo list.
505 The idea behind this is that by scheduling it, you have already taken care
506 of this item.
507 See also `org-agenda-todo-ignore-with-date'.
508 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
509 :group 'org-agenda-skip
510 :group 'org-agenda-todo-list
511 :type 'boolean)
513 (defcustom org-agenda-todo-ignore-deadlines nil
514 "Non-nil means, don't show near deadline entries in the global todo list.
515 Near means closer than `org-deadline-warning-days' days.
516 The idea behind this is that such items will appear in the agenda anyway.
517 See also `org-agenda-todo-ignore-with-date'.
518 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
519 :group 'org-agenda-skip
520 :group 'org-agenda-todo-list
521 :type 'boolean)
523 (defcustom org-agenda-tags-todo-honor-ignore-options nil
524 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
525 The variables
526 `org-agenda-todo-ignore-with-date',
527 `org-agenda-todo-ignore-scheduled'
528 `org-agenda-todo-ignore-deadlines'
529 make the global TODO list skip entries that have time stamps of certain
530 kinds. If this option is set, the same options will also apply for the
531 tags-todo search, which is the general tags/property matcher
532 restricted to unfinished TODO entries only."
533 :group 'org-agenda-skip
534 :group 'org-agenda-todo-list
535 :group 'org-agenda-match-view
536 :type 'boolean)
538 (defcustom org-agenda-skip-scheduled-if-done nil
539 "Non-nil means don't show scheduled items in agenda when they are done.
540 This is relevant for the daily/weekly agenda, not for the TODO list. And
541 it applies only to the actual date of the scheduling. Warnings about
542 an item with a past scheduling dates are always turned off when the item
543 is DONE."
544 :group 'org-agenda-skip
545 :group 'org-agenda-daily/weekly
546 :type 'boolean)
548 (defcustom org-agenda-skip-deadline-if-done nil
549 "Non-nil means don't show deadlines when the corresponding item is done.
550 When nil, the deadline is still shown and should give you a happy feeling.
551 This is relevant for the daily/weekly agenda. And it applied only to the
552 actually date of the deadline. Warnings about approaching and past-due
553 deadlines are always turned off when the item is DONE."
554 :group 'org-agenda-skip
555 :group 'org-agenda-daily/weekly
556 :type 'boolean)
558 (defcustom org-agenda-skip-timestamp-if-done nil
559 "Non-nil means don't select item by timestamp or -range if it is DONE."
560 :group 'org-agenda-skip
561 :group 'org-agenda-daily/weekly
562 :type 'boolean)
564 (defcustom org-agenda-dim-blocked-tasks t
565 "Non-nil means, dim blocked tasks in the agenda display.
566 This causes some overhead during agenda construction, but if you
567 have turned on `org-enforce-todo-dependencies',
568 `org-enforce-todo-checkbox-dependencies', or any other blocking
569 mechanism, this will create useful feedback in the agenda.
571 Instead ot t, this variable can also have the value `invisible'.
572 Then blocked tasks will be invisible and only become visible when
573 they become unblocked. An exemption to this behavior is when a task is
574 blocked because of unchecked checkboxes below it. Since checkboxes do
575 not show up in the agenda views, making this task invisible you remove any
576 trace from agenda views that there is something to do. Therefore, a task
577 that is blocked because of checkboxes will never be made invisible, it
578 will only be dimmed."
579 :group 'org-agenda-daily/weekly
580 :group 'org-agenda-todo-list
581 :type '(choice
582 (const :tag "Do not dim" nil)
583 (const :tag "Dim to a grey face" t)
584 (const :tag "Make invisibe" invisible)))
586 (defcustom org-timeline-show-empty-dates 3
587 "Non-nil means, `org-timeline' also shows dates without an entry.
588 When nil, only the days which actually have entries are shown.
589 When t, all days between the first and the last date are shown.
590 When an integer, show also empty dates, but if there is a gap of more than
591 N days, just insert a special line indicating the size of the gap."
592 :group 'org-agenda-skip
593 :type '(choice
594 (const :tag "None" nil)
595 (const :tag "All" t)
596 (number :tag "at most")))
598 (defgroup org-agenda-startup nil
599 "Options concerning initial settings in the Agenda in Org Mode."
600 :tag "Org Agenda Startup"
601 :group 'org-agenda)
603 (defcustom org-finalize-agenda-hook nil
604 "Hook run just before displaying an agenda buffer."
605 :group 'org-agenda-startup
606 :type 'hook)
608 (defcustom org-agenda-mouse-1-follows-link nil
609 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
610 A longer mouse click will still set point. Does not work on XEmacs.
611 Needs to be set before org.el is loaded."
612 :group 'org-agenda-startup
613 :type 'boolean)
615 (defcustom org-agenda-start-with-follow-mode nil
616 "The initial value of follow-mode in a newly created agenda window."
617 :group 'org-agenda-startup
618 :type 'boolean)
620 (defvar org-agenda-include-inactive-timestamps nil
621 "Non-nil means, include inactive time stamps in agenda and timeline.")
623 (defgroup org-agenda-windows nil
624 "Options concerning the windows used by the Agenda in Org Mode."
625 :tag "Org Agenda Windows"
626 :group 'org-agenda)
628 (defcustom org-agenda-window-setup 'reorganize-frame
629 "How the agenda buffer should be displayed.
630 Possible values for this option are:
632 current-window Show agenda in the current window, keeping all other windows.
633 other-frame Use `switch-to-buffer-other-frame' to display agenda.
634 other-window Use `switch-to-buffer-other-window' to display agenda.
635 reorganize-frame Show only two windows on the current frame, the current
636 window and the agenda.
637 See also the variable `org-agenda-restore-windows-after-quit'."
638 :group 'org-agenda-windows
639 :type '(choice
640 (const current-window)
641 (const other-frame)
642 (const other-window)
643 (const reorganize-frame)))
645 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
646 "The min and max height of the agenda window as a fraction of frame height.
647 The value of the variable is a cons cell with two numbers between 0 and 1.
648 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
649 :group 'org-agenda-windows
650 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
652 (defcustom org-agenda-restore-windows-after-quit nil
653 "Non-nil means, restore window configuration open exiting agenda.
654 Before the window configuration is changed for displaying the agenda,
655 the current status is recorded. When the agenda is exited with
656 `q' or `x' and this option is set, the old state is restored. If
657 `org-agenda-window-setup' is `other-frame', the value of this
658 option will be ignored.."
659 :group 'org-agenda-windows
660 :type 'boolean)
662 (defcustom org-agenda-ndays 7
663 "Number of days to include in overview display.
664 Should be 1 or 7.
665 Custom commands can set this variable in the options section."
666 :group 'org-agenda-daily/weekly
667 :type 'number)
669 (defcustom org-agenda-start-on-weekday 1
670 "Non-nil means, start the overview always on the specified weekday.
671 0 denotes Sunday, 1 denotes Monday etc.
672 When nil, always start on the current day.
673 Custom commands can set this variable in the options section."
674 :group 'org-agenda-daily/weekly
675 :type '(choice (const :tag "Today" nil)
676 (number :tag "Weekday No.")))
678 (defcustom org-agenda-show-all-dates t
679 "Non-nil means, `org-agenda' shows every day in the selected range.
680 When nil, only the days which actually have entries are shown."
681 :group 'org-agenda-daily/weekly
682 :type 'boolean)
684 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
685 "Format string for displaying dates in the agenda.
686 Used by the daily/weekly agenda and by the timeline. This should be
687 a format string understood by `format-time-string', or a function returning
688 the formatted date as a string. The function must take a single argument,
689 a calendar-style date list like (month day year)."
690 :group 'org-agenda-daily/weekly
691 :type '(choice
692 (string :tag "Format string")
693 (function :tag "Function")))
695 (defun org-agenda-format-date-aligned (date)
696 "Format a date string for display in the daily/weekly agenda, or timeline.
697 This function makes sure that dates are aligned for easy reading."
698 (require 'cal-iso)
699 (let* ((dayname (calendar-day-name date))
700 (day (cadr date))
701 (day-of-week (calendar-day-of-week date))
702 (month (car date))
703 (monthname (calendar-month-name month))
704 (year (nth 2 date))
705 (iso-week (org-days-to-iso-week
706 (calendar-absolute-from-gregorian date)))
707 (weekyear (cond ((and (= month 1) (>= iso-week 52))
708 (1- year))
709 ((and (= month 12) (<= iso-week 1))
710 (1+ year))
711 (t year)))
712 (weekstring (if (= day-of-week 1)
713 (format " W%02d" iso-week)
714 "")))
715 (format "%-10s %2d %s %4d%s"
716 dayname day monthname year weekstring)))
718 (defcustom org-agenda-weekend-days '(6 0)
719 "Which days are weekend?
720 These days get the special face `org-agenda-date-weekend' in the agenda
721 and timeline buffers."
722 :group 'org-agenda-daily/weekly
723 :type '(set :greedy t
724 (const :tag "Monday" 1)
725 (const :tag "Tuesday" 2)
726 (const :tag "Wednesday" 3)
727 (const :tag "Thursday" 4)
728 (const :tag "Friday" 5)
729 (const :tag "Saturday" 6)
730 (const :tag "Sunday" 0)))
732 (defcustom org-agenda-include-diary nil
733 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
734 Custom commands can set this variable in the options section."
735 :group 'org-agenda-daily/weekly
736 :type 'boolean)
738 (defcustom org-agenda-include-all-todo nil
739 "Set means weekly/daily agenda will always contain all TODO entries.
740 The TODO entries will be listed at the top of the agenda, before
741 the entries for specific days.
742 This option is deprecated, it is better to define a block agenda instead."
743 :group 'org-agenda-daily/weekly
744 :type 'boolean)
746 (defcustom org-agenda-repeating-timestamp-show-all t
747 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
748 When nil, only one occurrence is shown, either today or the
749 nearest into the future."
750 :group 'org-agenda-daily/weekly
751 :type 'boolean)
753 (defcustom org-scheduled-past-days 10000
754 "No. of days to continue listing scheduled items that are not marked DONE.
755 When an item is scheduled on a date, it shows up in the agenda on this
756 day and will be listed until it is marked done for the number of days
757 given here."
758 :group 'org-agenda-daily/weekly
759 :type 'number)
761 (defcustom org-agenda-log-mode-items '(closed clock)
762 "List of items that should be shown in agenda log mode.
763 This list may contain the following symbols:
765 closed Show entries that have been closed on that day.
766 clock Show entries that have received clocked time on that day.
767 state Show all logged state changes.
768 Note that instead of changing this variable, you can also press `C-u l' in
769 the agenda to display all available LOG items temporarily."
770 :group 'org-agenda-daily/weekly
771 :type '(set :greedy t (const closed) (const clock) (const state)))
773 (defcustom org-agenda-start-with-clockreport-mode nil
774 "The initial value of clockreport-mode in a newly created agenda window."
775 :group 'org-agenda-startup
776 :group 'org-agenda-daily/weekly
777 :type 'boolean)
779 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
780 "Property list with parameters for the clocktable in clockreport mode.
781 This is the display mode that shows a clock table in the daily/weekly
782 agenda, the properties for this dynamic block can be set here.
783 The usual clocktable parameters are allowed here, but you cannot set
784 the properties :name, :tstart, :tend, :block, and :scope - these will
785 be overwritten to make sure the content accurately reflects the
786 current display in the agenda."
787 :group 'org-agenda-daily/weekly
788 :type 'plist)
791 (defgroup org-agenda-time-grid nil
792 "Options concerning the time grid in the Org-mode Agenda."
793 :tag "Org Agenda Time Grid"
794 :group 'org-agenda)
796 (defcustom org-agenda-search-headline-for-time t
797 "Non-nil means, search headline for a time-of-day.
798 If the headline contains a time-of-day in one format or another, it will
799 be used to sort the entry into the time sequence of items for a day.
800 Some people have time stamps in the headline that refer to the creation
801 time or so, and then this produces an unwanted side effect. If this is
802 the case for your, use this variable to turn off searching the headline
803 for a time."
804 :group 'org-agenda-time-grid
805 :type 'boolean)
807 (defcustom org-agenda-use-time-grid t
808 "Non-nil means, show a time grid in the agenda schedule.
809 A time grid is a set of lines for specific times (like every two hours between
810 8:00 and 20:00). The items scheduled for a day at specific times are
811 sorted in between these lines.
812 For details about when the grid will be shown, and what it will look like, see
813 the variable `org-agenda-time-grid'."
814 :group 'org-agenda-time-grid
815 :type 'boolean)
817 (defcustom org-agenda-time-grid
818 '((daily today require-timed)
819 "----------------"
820 (800 1000 1200 1400 1600 1800 2000))
822 "The settings for time grid for agenda display.
823 This is a list of three items. The first item is again a list. It contains
824 symbols specifying conditions when the grid should be displayed:
826 daily if the agenda shows a single day
827 weekly if the agenda shows an entire week
828 today show grid on current date, independent of daily/weekly display
829 require-timed show grid only if at least one item has a time specification
831 The second item is a string which will be placed behind the grid time.
833 The third item is a list of integers, indicating the times that should have
834 a grid line."
835 :group 'org-agenda-time-grid
836 :type
837 '(list
838 (set :greedy t :tag "Grid Display Options"
839 (const :tag "Show grid in single day agenda display" daily)
840 (const :tag "Show grid in weekly agenda display" weekly)
841 (const :tag "Always show grid for today" today)
842 (const :tag "Show grid only if any timed entries are present"
843 require-timed)
844 (const :tag "Skip grid times already present in an entry"
845 remove-match))
846 (string :tag "Grid String")
847 (repeat :tag "Grid Times" (integer :tag "Time"))))
849 (defgroup org-agenda-sorting nil
850 "Options concerning sorting in the Org-mode Agenda."
851 :tag "Org Agenda Sorting"
852 :group 'org-agenda)
854 (defcustom org-agenda-sorting-strategy
855 '((agenda time-up priority-down category-keep)
856 (todo priority-down category-keep)
857 (tags priority-down category-keep)
858 (search category-keep))
859 "Sorting structure for the agenda items of a single day.
860 This is a list of symbols which will be used in sequence to determine
861 if an entry should be listed before another entry. The following
862 symbols are recognized:
864 time-up Put entries with time-of-day indications first, early first
865 time-down Put entries with time-of-day indications first, late first
866 category-keep Keep the default order of categories, corresponding to the
867 sequence in `org-agenda-files'.
868 category-up Sort alphabetically by category, A-Z.
869 category-down Sort alphabetically by category, Z-A.
870 tag-up Sort alphabetically by last tag, A-Z.
871 tag-down Sort alphabetically by last tag, Z-A.
872 priority-up Sort numerically by priority, high priority last.
873 priority-down Sort numerically by priority, high priority first.
874 todo-state-up Sort by todo state, tasks that are done last.
875 todo-state-down Sort by todo state, tasks that are done first.
876 effort-up Sort numerically by estimated effort, high effort last.
877 effort-down Sort numerically by estimated effort, high effort first.
879 The different possibilities will be tried in sequence, and testing stops
880 if one comparison returns a \"not-equal\". For example, the default
881 '(time-up category-keep priority-down)
882 means: Pull out all entries having a specified time of day and sort them,
883 in order to make a time schedule for the current day the first thing in the
884 agenda listing for the day. Of the entries without a time indication, keep
885 the grouped in categories, don't sort the categories, but keep them in
886 the sequence given in `org-agenda-files'. Within each category sort by
887 priority.
889 Leaving out `category-keep' would mean that items will be sorted across
890 categories by priority.
892 Instead of a single list, this can also be a set of list for specific
893 contents, with a context symbol in the car of the list, any of
894 `agenda', `todo', `tags' for the corresponding agenda views.
896 Custom commands can bind this variable in the options section."
897 :group 'org-agenda-sorting
898 :type `(choice
899 (repeat :tag "General" ,org-sorting-choice)
900 (list :tag "Individually"
901 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
902 (repeat ,org-sorting-choice))
903 (cons (const :tag "Strategy for TODO lists" todo)
904 (repeat ,org-sorting-choice))
905 (cons (const :tag "Strategy for Tags matches" tags)
906 (repeat ,org-sorting-choice)))))
908 (defcustom org-sort-agenda-notime-is-late t
909 "Non-nil means, items without time are considered late.
910 This is only relevant for sorting. When t, items which have no explicit
911 time like 15:30 will be considered as 99:01, i.e. later than any items which
912 do have a time. When nil, the default time is before 0:00. You can use this
913 option to decide if the schedule for today should come before or after timeless
914 agenda entries."
915 :group 'org-agenda-sorting
916 :type 'boolean)
918 (defcustom org-sort-agenda-noeffort-is-high t
919 "Non-nil means, items without effort estimate are sorted as high effort.
920 When nil, such items are sorted as 0 minutes effort."
921 :group 'org-agenda-sorting
922 :type 'boolean)
924 (defgroup org-agenda-line-format nil
925 "Options concerning the entry prefix in the Org-mode agenda display."
926 :tag "Org Agenda Line Format"
927 :group 'org-agenda)
929 (defcustom org-agenda-prefix-format
930 '((agenda . " %-12:c%?-12t% s")
931 (timeline . " % s")
932 (todo . " %-12:c")
933 (tags . " %-12:c")
934 (search . " %-12:c"))
935 "Format specifications for the prefix of items in the agenda views.
936 An alist with four entries, for the different agenda types. The keys to the
937 sublists are `agenda', `timeline', `todo', and `tags'. The values
938 are format strings.
939 This format works similar to a printf format, with the following meaning:
941 %c the category of the item, \"Diary\" for entries from the diary, or
942 as given by the CATEGORY keyword or derived from the file name.
943 %T the *last* tag of the item. Last because inherited tags come
944 first in the list.
945 %t the time-of-day specification if one applies to the entry, in the
946 format HH:MM
947 %s Scheduling/Deadline information, a short string
949 All specifiers work basically like the standard `%s' of printf, but may
950 contain two additional characters: A question mark just after the `%' and
951 a whitespace/punctuation character just before the final letter.
953 If the first character after `%' is a question mark, the entire field
954 will only be included if the corresponding value applies to the
955 current entry. This is useful for fields which should have fixed
956 width when present, but zero width when absent. For example,
957 \"%?-12t\" will result in a 12 character time field if a time of the
958 day is specified, but will completely disappear in entries which do
959 not contain a time.
961 If there is punctuation or whitespace character just before the final
962 format letter, this character will be appended to the field value if
963 the value is not empty. For example, the format \"%-12:c\" leads to
964 \"Diary: \" if the category is \"Diary\". If the category were be
965 empty, no additional colon would be interted.
967 The default value of this option is \" %-12:c%?-12t% s\", meaning:
968 - Indent the line with two space characters
969 - Give the category in a 12 chars wide field, padded with whitespace on
970 the right (because of `-'). Append a colon if there is a category
971 (because of `:').
972 - If there is a time-of-day, put it into a 12 chars wide field. If no
973 time, don't put in an empty field, just skip it (because of '?').
974 - Finally, put the scheduling information and append a whitespace.
976 As another example, if you don't want the time-of-day of entries in
977 the prefix, you could use:
979 (setq org-agenda-prefix-format \" %-11:c% s\")
981 See also the variables `org-agenda-remove-times-when-in-prefix' and
982 `org-agenda-remove-tags'.
984 Custom commands can set this variable in the options section."
985 :type '(choice
986 (string :tag "General format")
987 (list :greedy t :tag "View dependent"
988 (cons (const agenda) (string :tag "Format"))
989 (cons (const timeline) (string :tag "Format"))
990 (cons (const todo) (string :tag "Format"))
991 (cons (const tags) (string :tag "Format"))
992 (cons (const search) (string :tag "Format"))))
993 :group 'org-agenda-line-format)
995 (defvar org-prefix-format-compiled nil
996 "The compiled version of the most recently used prefix format.
997 See the variable `org-agenda-prefix-format'.")
999 (defcustom org-agenda-todo-keyword-format "%-1s"
1000 "Format for the TODO keyword in agenda lines.
1001 Set this to something like \"%-12s\" if you want all TODO keywords
1002 to occupy a fixed space in the agenda display."
1003 :group 'org-agenda-line-format
1004 :type 'string)
1006 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1007 "Text preceding timerange entries in the agenda view.
1008 This is a list with two strings. The first applies when the range
1009 is entirely on one day. The second applies if the range spans several days.
1010 The strings may have two \"%d\" format specifiers which will be filled
1011 with the sequence number of the days, and the total number of days in the
1012 range, respectively."
1013 :group 'org-agenda-line-format
1014 :type '(list
1015 (string :tag "Deadline today ")
1016 (choice :tag "Deadline relative"
1017 (string :tag "Format string")
1018 (function))))
1020 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1021 "Text preceeding scheduled items in the agenda view.
1022 This is a list with two strings. The first applies when the item is
1023 scheduled on the current day. The second applies when it has been scheduled
1024 previously, it may contain a %d indicating that this is the nth time that
1025 this item is scheduled, due to automatic rescheduling of unfinished items
1026 for the following day. So this number is one larger than the number of days
1027 that passed since this item was scheduled first."
1028 :group 'org-agenda-line-format
1029 :type '(list
1030 (string :tag "Scheduled today ")
1031 (string :tag "Scheduled previously")))
1033 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1034 "Text preceeding deadline items in the agenda view.
1035 This is a list with two strings. The first applies when the item has its
1036 deadline on the current day. The second applies when it is in the past or
1037 in the future, it may contain %d to capture how many days away the deadline
1038 is (was)."
1039 :group 'org-agenda-line-format
1040 :type '(list
1041 (string :tag "Deadline today ")
1042 (choice :tag "Deadline relative"
1043 (string :tag "Format string")
1044 (function))))
1046 (defcustom org-agenda-remove-times-when-in-prefix t
1047 "Non-nil means, remove duplicate time specifications in agenda items.
1048 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1049 time-of-day specification in a headline or diary entry is extracted and
1050 placed into the prefix. If this option is non-nil, the original specification
1051 \(a timestamp or -range, or just a plain time(range) specification like
1052 11:30-4pm) will be removed for agenda display. This makes the agenda less
1053 cluttered.
1054 The option can be t or nil. It may also be the symbol `beg', indicating
1055 that the time should only be removed what it is located at the beginning of
1056 the headline/diary entry."
1057 :group 'org-agenda-line-format
1058 :type '(choice
1059 (const :tag "Always" t)
1060 (const :tag "Never" nil)
1061 (const :tag "When at beginning of entry" beg)))
1064 (defcustom org-agenda-default-appointment-duration nil
1065 "Default duration for appointments that only have a starting time.
1066 When nil, no duration is specified in such cases.
1067 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1068 :group 'org-agenda-line-format
1069 :type '(choice
1070 (integer :tag "Minutes")
1071 (const :tag "No default duration")))
1073 (defcustom org-agenda-show-inherited-tags t
1074 "Non-nil means, show inherited tags in each agenda line."
1075 :group 'org-agenda-line-format
1076 :type 'boolean)
1078 (defcustom org-agenda-remove-tags nil
1079 "Non-nil means, remove the tags from the headline copy in the agenda.
1080 When this is the symbol `prefix', only remove tags when
1081 `org-agenda-prefix-format' contains a `%T' specifier."
1082 :group 'org-agenda-line-format
1083 :type '(choice
1084 (const :tag "Always" t)
1085 (const :tag "Never" nil)
1086 (const :tag "When prefix format contains %T" prefix)))
1088 (if (fboundp 'defvaralias)
1089 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1090 'org-agenda-remove-tags))
1092 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1093 "Shift tags in agenda items to this column.
1094 If this number is positive, it specifies the column. If it is negative,
1095 it means that the tags should be flushright to that column. For example,
1096 -80 works well for a normal 80 character screen."
1097 :group 'org-agenda-line-format
1098 :type 'integer)
1100 (if (fboundp 'defvaralias)
1101 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1103 (defcustom org-agenda-fontify-priorities 'cookies
1104 "Non-nil means, highlight low and high priorities in agenda.
1105 When t, the highest priority entries are bold, lowest priority italic.
1106 However, settings in org-priority-faces will overrule these faces.
1107 When this variable is the symbol `cookies', only fontify the
1108 cookies, not the entire task.
1109 This may also be an association list of priority faces, whose
1110 keys are the character values of `org-highest-priority',
1111 `org-default-priority', and `org-lowest-priority' (the default values
1112 are ?A, ?B, and ?C, respectively). The face may be a named face,
1113 or a list like `(:background \"Red\")'."
1114 :group 'org-agenda-line-format
1115 :type '(choice
1116 (const :tag "Never" nil)
1117 (const :tag "Defaults" t)
1118 (const :tag "Cookies only" cookies)
1119 (repeat :tag "Specify"
1120 (list (character :tag "Priority" :value ?A)
1121 (sexp :tag "face")))))
1123 (defgroup org-agenda-column-view nil
1124 "Options concerning column view in the agenda."
1125 :tag "Org Agenda Column View"
1126 :group 'org-agenda)
1128 (defcustom org-agenda-columns-show-summaries t
1129 "Non-nil means, show summaries for columns displayed in the agenda view."
1130 :group 'org-agenda-column-view
1131 :type 'boolean)
1133 (defcustom org-agenda-columns-remove-prefix-from-item t
1134 "Non-nil means, remove the prefix from a headline for agenda column view.
1135 The special ITEM field in the columns format contains the current line, with
1136 all information shown in other columns (like the TODO state or a tag).
1137 When this variable is non-nil, also the agenda prefix will be removed from
1138 the content of the ITEM field, to make sure as much as possible of the
1139 headline can be shown in the limited width of the field."
1140 :group 'org-agenda
1141 :type 'boolean)
1143 (defcustom org-agenda-columns-compute-summary-properties t
1144 "Non-nil means, recompute all summary properties before column view.
1145 When column view in the agenda is listing properties that have a summary
1146 operator, it can go to all relevant buffers and recompute the summaries
1147 there. This can mean overhead for the agenda column view, but is necessary
1148 to have thing up to date.
1149 As a special case, a CLOCKSUM property also makes sure that the clock
1150 computations are current."
1151 :group 'org-agenda-column-view
1152 :type 'boolean)
1154 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1155 "Non-nil means, the duration of an appointment will add to day effort.
1156 The property to which appointment durations will be added is the one given
1157 in the option `org-effort-property'. If an appointment does not have
1158 an end time, `org-agenda-default-appointment-duration' will be used. If that
1159 is not set, an appointment without end time will not contribute to the time
1160 estimate."
1161 :group 'org-agenda-column-view
1162 :type 'boolean)
1164 (eval-when-compile
1165 (require 'cl))
1166 (require 'org)
1168 (defun org-add-agenda-custom-command (entry)
1169 "Replace or add a command in `org-agenda-custom-commands'.
1170 This is mostly for hacking and trying a new command - once the command
1171 works you probably want to add it to `org-agenda-custom-commands' for good."
1172 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1173 (if ass
1174 (setcdr ass (cdr entry))
1175 (push entry org-agenda-custom-commands))))
1177 ;;; Define the Org-agenda-mode
1179 (defvar org-agenda-mode-map (make-sparse-keymap)
1180 "Keymap for `org-agenda-mode'.")
1182 (defvar org-agenda-menu) ; defined later in this file.
1183 (defvar org-agenda-follow-mode nil)
1184 (defvar org-agenda-clockreport-mode nil)
1185 (defvar org-agenda-show-log nil)
1186 (defvar org-agenda-redo-command nil)
1187 (defvar org-agenda-query-string nil)
1188 (defvar org-agenda-mode-hook nil
1189 "Hook for org-agenda-mode, run after the mode is turned on.")
1190 (defvar org-agenda-type nil)
1191 (defvar org-agenda-force-single-file nil)
1193 (defun org-agenda-mode ()
1194 "Mode for time-sorted view on action items in Org-mode files.
1196 The following commands are available:
1198 \\{org-agenda-mode-map}"
1199 (interactive)
1200 (kill-all-local-variables)
1201 (setq org-agenda-undo-list nil
1202 org-agenda-pending-undo-list nil)
1203 (setq major-mode 'org-agenda-mode)
1204 ;; Keep global-font-lock-mode from turning on font-lock-mode
1205 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1206 (setq mode-name "Org-Agenda")
1207 (use-local-map org-agenda-mode-map)
1208 (easy-menu-add org-agenda-menu)
1209 (if org-startup-truncated (setq truncate-lines t))
1210 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1211 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1212 ;; Make sure properties are removed when copying text
1213 (when (boundp 'buffer-substring-filters)
1214 (org-set-local 'buffer-substring-filters
1215 (cons (lambda (x)
1216 (set-text-properties 0 (length x) nil x) x)
1217 buffer-substring-filters)))
1218 (unless org-agenda-keep-modes
1219 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1220 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1221 org-agenda-show-log nil))
1222 (easy-menu-change
1223 '("Agenda") "Agenda Files"
1224 (append
1225 (list
1226 (vector
1227 (if (get 'org-agenda-files 'org-restrict)
1228 "Restricted to single file"
1229 "Edit File List")
1230 '(org-edit-agenda-file-list)
1231 (not (get 'org-agenda-files 'org-restrict)))
1232 "--")
1233 (mapcar 'org-file-menu-entry (org-agenda-files))))
1234 (org-agenda-set-mode-name)
1235 (apply
1236 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1237 (list 'org-agenda-mode-hook)))
1239 (substitute-key-definition 'undo 'org-agenda-undo
1240 org-agenda-mode-map global-map)
1241 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1242 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1243 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1244 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1245 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1246 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1247 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1248 (org-defkey org-agenda-mode-map "A" 'org-agenda-archive-to-archive-sibling)
1249 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1250 (org-defkey org-agenda-mode-map " " 'org-agenda-show)
1251 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1252 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1253 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1254 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1255 (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
1256 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1257 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1258 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1259 (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
1260 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1261 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1262 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1263 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1264 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1265 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1266 (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
1267 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1268 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1269 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1270 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1271 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1272 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1273 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1274 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1275 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1277 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1278 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1279 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1280 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1281 (while l (org-defkey org-agenda-mode-map
1282 (int-to-string (pop l)) 'digit-argument)))
1284 (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
1285 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1286 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1287 (org-defkey org-agenda-mode-map "v" 'org-agenda-archives-mode)
1288 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1289 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1290 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1291 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1292 (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
1293 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1294 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1295 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1296 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1297 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1298 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1299 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1300 (org-defkey org-agenda-mode-map "n" 'next-line)
1301 (org-defkey org-agenda-mode-map "p" 'previous-line)
1302 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1303 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1304 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1305 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1306 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1307 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1308 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1309 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1310 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1311 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1312 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1313 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1314 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1315 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1317 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1318 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1319 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1320 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1321 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1322 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1323 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1324 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1325 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1326 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1327 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1328 (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
1329 (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
1330 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1332 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1333 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1334 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1335 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1336 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1337 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1339 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
1340 "Local keymap for agenda entries from Org-mode.")
1342 (org-defkey org-agenda-keymap
1343 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1344 (org-defkey org-agenda-keymap
1345 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1346 (when org-agenda-mouse-1-follows-link
1347 (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
1348 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1349 '("Agenda"
1350 ("Agenda Files")
1351 "--"
1352 ["Show" org-agenda-show t]
1353 ["Go To (other window)" org-agenda-goto t]
1354 ["Go To (this window)" org-agenda-switch-to t]
1355 ["Follow Mode" org-agenda-follow-mode
1356 :style toggle :selected org-agenda-follow-mode :active t]
1357 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1358 "--"
1359 ["Cycle TODO" org-agenda-todo t]
1360 ("Archive"
1361 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1362 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1363 ["Archive subtree" org-agenda-archive t])
1364 ["Delete subtree" org-agenda-kill t]
1365 ["Add note" org-agenda-add-note t]
1366 "--"
1367 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1368 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1369 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1370 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
1371 "--"
1372 ("Tags and Properties"
1373 ["Show all Tags" org-agenda-show-tags t]
1374 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1375 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1376 "--"
1377 ["Column View" org-columns t])
1378 ("Date/Schedule"
1379 ["Schedule" org-agenda-schedule t]
1380 ["Set Deadline" org-agenda-deadline t]
1381 "--"
1382 ["Mark item" org-agenda-action :active t :keys "k m"]
1383 ["Show mark item" org-agenda-action :active t :keys "k v"]
1384 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1385 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1386 "--"
1387 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1388 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1389 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1390 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1391 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1392 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1393 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1394 ("Clock"
1395 ["Clock in" org-agenda-clock-in t]
1396 ["Clock out" org-agenda-clock-out t]
1397 ["Clock cancel" org-agenda-clock-cancel t]
1398 ["Goto running clock" org-clock-goto t])
1399 ("Priority"
1400 ["Set Priority" org-agenda-priority t]
1401 ["Increase Priority" org-agenda-priority-up t]
1402 ["Decrease Priority" org-agenda-priority-down t]
1403 ["Show Priority" org-agenda-show-priority t])
1404 ("Calendar/Diary"
1405 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1406 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1407 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1408 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1409 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1410 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1411 "--"
1412 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1413 "--"
1414 ("View"
1415 ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
1416 :style radio :selected (equal org-agenda-ndays 1)]
1417 ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
1418 :style radio :selected (equal org-agenda-ndays 7)]
1419 ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
1420 :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
1421 ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
1422 :style radio :selected (member org-agenda-ndays '(365 366))]
1423 "--"
1424 ["Include Diary" org-agenda-toggle-diary
1425 :style toggle :selected org-agenda-include-diary
1426 :active (org-agenda-check-type nil 'agenda)]
1427 ["Use Time Grid" org-agenda-toggle-time-grid
1428 :style toggle :selected org-agenda-use-time-grid
1429 :active (org-agenda-check-type nil 'agenda)]
1430 "--"
1431 ["Show clock report" org-agenda-clockreport-mode
1432 :style toggle :selected org-agenda-clockreport-mode
1433 :active (org-agenda-check-type nil 'agenda)]
1434 "--"
1435 ["Show Logbook entries" org-agenda-log-mode
1436 :style toggle :selected org-agenda-show-log
1437 :active (org-agenda-check-type nil 'agenda 'timeline)]
1438 ["Include archived trees" org-agenda-archives-mode
1439 :style toggle :selected org-agenda-archives-mode :active t]
1440 ["Include archive files" (org-agenda-archives-mode t)
1441 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1442 :keys "C-u v"])
1443 ["Write view to file" org-write-agenda t]
1444 ["Rebuild buffer" org-agenda-redo t]
1445 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1446 "--"
1447 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1448 "--"
1449 ["Quit" org-agenda-quit t]
1450 ["Exit and Release Buffers" org-agenda-exit t]
1453 ;;; Agenda undo
1455 (defvar org-agenda-allow-remote-undo t
1456 "Non-nil means, allow remote undo from the agenda buffer.")
1457 (defvar org-agenda-undo-list nil
1458 "List of undoable operations in the agenda since last refresh.")
1459 (defvar org-agenda-undo-has-started-in nil
1460 "Buffers that have already seen `undo-start' in the current undo sequence.")
1461 (defvar org-agenda-pending-undo-list nil
1462 "In a series of undo commands, this is the list of remaining undo items.")
1465 (defun org-agenda-undo ()
1466 "Undo a remote editing step in the agenda.
1467 This undoes changes both in the agenda buffer and in the remote buffer
1468 that have been changed along."
1469 (interactive)
1470 (or org-agenda-allow-remote-undo
1471 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
1472 (if (not (eq this-command last-command))
1473 (setq org-agenda-undo-has-started-in nil
1474 org-agenda-pending-undo-list org-agenda-undo-list))
1475 (if (not org-agenda-pending-undo-list)
1476 (error "No further undo information"))
1477 (let* ((entry (pop org-agenda-pending-undo-list))
1478 buf line cmd rembuf)
1479 (setq cmd (pop entry) line (pop entry))
1480 (setq rembuf (nth 2 entry))
1481 (org-with-remote-undo rembuf
1482 (while (bufferp (setq buf (pop entry)))
1483 (if (pop entry)
1484 (with-current-buffer buf
1485 (let ((last-undo-buffer buf)
1486 (inhibit-read-only t))
1487 (unless (memq buf org-agenda-undo-has-started-in)
1488 (push buf org-agenda-undo-has-started-in)
1489 (make-local-variable 'pending-undo-list)
1490 (undo-start))
1491 (while (and pending-undo-list
1492 (listp pending-undo-list)
1493 (not (car pending-undo-list)))
1494 (pop pending-undo-list))
1495 (undo-more 1))))))
1496 (goto-line line)
1497 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1499 (defun org-verify-change-for-undo (l1 l2)
1500 "Verify that a real change occurred between the undo lists L1 and L2."
1501 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1502 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1503 (not (eq l1 l2)))
1505 ;;; Agenda dispatch
1507 (defvar org-agenda-restrict nil)
1508 (defvar org-agenda-restrict-begin (make-marker))
1509 (defvar org-agenda-restrict-end (make-marker))
1510 (defvar org-agenda-last-dispatch-buffer nil)
1511 (defvar org-agenda-overriding-restriction nil)
1513 ;;;###autoload
1514 (defun org-agenda (&optional arg keys restriction)
1515 "Dispatch agenda commands to collect entries to the agenda buffer.
1516 Prompts for a command to execute. Any prefix arg will be passed
1517 on to the selected command. The default selections are:
1519 a Call `org-agenda-list' to display the agenda for current day or week.
1520 t Call `org-todo-list' to display the global todo list.
1521 T Call `org-todo-list' to display the global todo list, select only
1522 entries with a specific TODO keyword (the user gets a prompt).
1523 m Call `org-tags-view' to display headlines with tags matching
1524 a condition (the user is prompted for the condition).
1525 M Like `m', but select only TODO entries, no ordinary headlines.
1526 L Create a timeline for the current buffer.
1527 e Export views to associated files.
1528 s Search entries for keywords.
1529 / Multi occur accros all agenda files and also files listed
1530 in `org-agenda-text-search-extra-files'.
1531 < Restrict agenda commands to buffer, subtree, or region.
1532 Press several times to get the desired effect.
1533 > Remove a previous restriction.
1534 # List \"stuck\" projects.
1535 ! Configure what \"stuck\" means.
1536 C Configure custom agenda commands.
1538 More commands can be added by configuring the variable
1539 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1540 searches can be pre-defined in this way.
1542 If the current buffer is in Org-mode and visiting a file, you can also
1543 first press `<' once to indicate that the agenda should be temporarily
1544 \(until the next use of \\[org-agenda]) restricted to the current file.
1545 Pressing `<' twice means to restrict to the current subtree or region
1546 \(if active)."
1547 (interactive "P")
1548 (catch 'exit
1549 (let* ((prefix-descriptions nil)
1550 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1551 (org-agenda-custom-commands
1552 ;; normalize different versions
1553 (delq nil
1554 (mapcar
1555 (lambda (x)
1556 (cond ((stringp (cdr x))
1557 (push x prefix-descriptions)
1558 nil)
1559 ((stringp (nth 1 x)) x)
1560 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1561 (t (cons (car x) (cons "" (cdr x))))))
1562 org-agenda-custom-commands)))
1563 (buf (current-buffer))
1564 (bfn (buffer-file-name (buffer-base-buffer)))
1565 entry key type match lprops ans)
1566 ;; Turn off restriction unless there is an overriding one
1567 (unless org-agenda-overriding-restriction
1568 (put 'org-agenda-files 'org-restrict nil)
1569 (setq org-agenda-restrict nil)
1570 (move-marker org-agenda-restrict-begin nil)
1571 (move-marker org-agenda-restrict-end nil))
1572 ;; Delete old local properties
1573 (put 'org-agenda-redo-command 'org-lprops nil)
1574 ;; Remember where this call originated
1575 (setq org-agenda-last-dispatch-buffer (current-buffer))
1576 (unless keys
1577 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1578 keys (car ans)
1579 restriction (cdr ans)))
1580 ;; Estabish the restriction, if any
1581 (when (and (not org-agenda-overriding-restriction) restriction)
1582 (put 'org-agenda-files 'org-restrict (list bfn))
1583 (cond
1584 ((eq restriction 'region)
1585 (setq org-agenda-restrict t)
1586 (move-marker org-agenda-restrict-begin (region-beginning))
1587 (move-marker org-agenda-restrict-end (region-end)))
1588 ((eq restriction 'subtree)
1589 (save-excursion
1590 (setq org-agenda-restrict t)
1591 (org-back-to-heading t)
1592 (move-marker org-agenda-restrict-begin (point))
1593 (move-marker org-agenda-restrict-end
1594 (progn (org-end-of-subtree t)))))))
1596 (require 'calendar) ; FIXME: can we avoid this for some commands?
1597 ;; For example the todo list should not need it (but does...)
1598 (cond
1599 ((setq entry (assoc keys org-agenda-custom-commands))
1600 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1601 (progn
1602 (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
1603 (put 'org-agenda-redo-command 'org-lprops lprops)
1604 (cond
1605 ((eq type 'agenda)
1606 (org-let lprops '(org-agenda-list current-prefix-arg)))
1607 ((eq type 'alltodo)
1608 (org-let lprops '(org-todo-list current-prefix-arg)))
1609 ((eq type 'search)
1610 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1611 ((eq type 'stuck)
1612 (org-let lprops '(org-agenda-list-stuck-projects
1613 current-prefix-arg)))
1614 ((eq type 'tags)
1615 (org-let lprops '(org-tags-view current-prefix-arg match)))
1616 ((eq type 'tags-todo)
1617 (org-let lprops '(org-tags-view '(4) match)))
1618 ((eq type 'todo)
1619 (org-let lprops '(org-todo-list match)))
1620 ((eq type 'tags-tree)
1621 (org-check-for-org-mode)
1622 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1623 ((eq type 'todo-tree)
1624 (org-check-for-org-mode)
1625 (org-let lprops
1626 '(org-occur (concat "^" outline-regexp "[ \t]*"
1627 (regexp-quote match) "\\>"))))
1628 ((eq type 'occur-tree)
1629 (org-check-for-org-mode)
1630 (org-let lprops '(org-occur match)))
1631 ((functionp type)
1632 (org-let lprops '(funcall type match)))
1633 ((fboundp type)
1634 (org-let lprops '(funcall type match)))
1635 (t (error "Invalid custom agenda command type %s" type))))
1636 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1637 ((equal keys "C")
1638 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1639 (customize-variable 'org-agenda-custom-commands))
1640 ((equal keys "a") (call-interactively 'org-agenda-list))
1641 ((equal keys "s") (call-interactively 'org-search-view))
1642 ((equal keys "t") (call-interactively 'org-todo-list))
1643 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1644 ((equal keys "m") (call-interactively 'org-tags-view))
1645 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1646 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1647 ((equal keys "L")
1648 (unless (org-mode-p)
1649 (error "This is not an Org-mode file"))
1650 (unless restriction
1651 (put 'org-agenda-files 'org-restrict (list bfn))
1652 (org-call-with-arg 'org-timeline arg)))
1653 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1654 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1655 ((equal keys "!") (customize-variable 'org-stuck-projects))
1656 (t (error "Invalid agenda key"))))))
1658 (defun org-agenda-normalize-custom-commands (cmds)
1659 (delq nil
1660 (mapcar
1661 (lambda (x)
1662 (cond ((stringp (cdr x)) nil)
1663 ((stringp (nth 1 x)) x)
1664 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1665 (t (cons (car x) (cons "" (cdr x))))))
1666 cmds)))
1668 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1669 "The user interface for selecting an agenda command."
1670 (catch 'exit
1671 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1672 (restrict-ok (and bfn (org-mode-p)))
1673 (region-p (org-region-active-p))
1674 (custom org-agenda-custom-commands)
1675 (selstring "")
1676 restriction second-time
1677 c entry key type match prefixes rmheader header-end custom1 desc)
1678 (save-window-excursion
1679 (delete-other-windows)
1680 (org-switch-to-buffer-other-window " *Agenda Commands*")
1681 (erase-buffer)
1682 (insert (eval-when-compile
1683 (let ((header
1685 Press key for an agenda command: < Buffer, subtree/region restriction
1686 -------------------------------- > Remove restriction
1687 a Agenda for current week or day e Export agenda views
1688 t List of all TODO entries T Entries with special TODO kwd
1689 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1690 L Timeline for current buffer # List stuck projects (!=configure)
1691 s Search for keywords C Configure custom agenda commands
1692 / Multi-occur
1694 (start 0))
1695 (while (string-match
1696 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1697 header start)
1698 (setq start (match-end 0))
1699 (add-text-properties (match-beginning 2) (match-end 2)
1700 '(face bold) header))
1701 header)))
1702 (setq header-end (move-marker (make-marker) (point)))
1703 (while t
1704 (setq custom1 custom)
1705 (when (eq rmheader t)
1706 (goto-line 1)
1707 (re-search-forward ":" nil t)
1708 (delete-region (match-end 0) (point-at-eol))
1709 (forward-char 1)
1710 (looking-at "-+")
1711 (delete-region (match-end 0) (point-at-eol))
1712 (move-marker header-end (match-end 0)))
1713 (goto-char header-end)
1714 (delete-region (point) (point-max))
1715 (while (setq entry (pop custom1))
1716 (setq key (car entry) desc (nth 1 entry)
1717 type (nth 2 entry) match (nth 3 entry))
1718 (if (> (length key) 1)
1719 (add-to-list 'prefixes (string-to-char key))
1720 (insert
1721 (format
1722 "\n%-4s%-14s: %s"
1723 (org-add-props (copy-sequence key)
1724 '(face bold))
1725 (cond
1726 ((string-match "\\S-" desc) desc)
1727 ((eq type 'agenda) "Agenda for current week or day")
1728 ((eq type 'alltodo) "List of all TODO entries")
1729 ((eq type 'search) "Word search")
1730 ((eq type 'stuck) "List of stuck projects")
1731 ((eq type 'todo) "TODO keyword")
1732 ((eq type 'tags) "Tags query")
1733 ((eq type 'tags-todo) "Tags (TODO)")
1734 ((eq type 'tags-tree) "Tags tree")
1735 ((eq type 'todo-tree) "TODO kwd tree")
1736 ((eq type 'occur-tree) "Occur tree")
1737 ((functionp type) (if (symbolp type)
1738 (symbol-name type)
1739 "Lambda expression"))
1740 (t "???"))
1741 (cond
1742 ((stringp match)
1743 (org-add-props match nil 'face 'org-warning))
1744 (match
1745 (format "set of %d commands" (length match)))
1746 (t ""))))))
1747 (when prefixes
1748 (mapc (lambda (x)
1749 (insert
1750 (format "\n%s %s"
1751 (org-add-props (char-to-string x)
1752 nil 'face 'bold)
1753 (or (cdr (assoc (concat selstring (char-to-string x))
1754 prefix-descriptions))
1755 "Prefix key"))))
1756 prefixes))
1757 (goto-char (point-min))
1758 (if second-time
1759 (if (not (pos-visible-in-window-p (point-max)))
1760 (org-fit-window-to-buffer))
1761 (setq second-time t)
1762 (org-fit-window-to-buffer))
1763 (message "Press key for agenda command%s:"
1764 (if (or restrict-ok org-agenda-overriding-restriction)
1765 (if org-agenda-overriding-restriction
1766 " (restriction lock active)"
1767 (if restriction
1768 (format " (restricted to %s)" restriction)
1769 " (unrestricted)"))
1770 ""))
1771 (setq c (read-char-exclusive))
1772 (message "")
1773 (cond
1774 ((assoc (char-to-string c) custom)
1775 (setq selstring (concat selstring (char-to-string c)))
1776 (throw 'exit (cons selstring restriction)))
1777 ((memq c prefixes)
1778 (setq selstring (concat selstring (char-to-string c))
1779 prefixes nil
1780 rmheader (or rmheader t)
1781 custom (delq nil (mapcar
1782 (lambda (x)
1783 (if (or (= (length (car x)) 1)
1784 (/= (string-to-char (car x)) c))
1786 (cons (substring (car x) 1) (cdr x))))
1787 custom))))
1788 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
1789 (message "Restriction is only possible in Org-mode buffers")
1790 (ding) (sit-for 1))
1791 ((eq c ?1)
1792 (org-agenda-remove-restriction-lock 'noupdate)
1793 (setq restriction 'buffer))
1794 ((eq c ?0)
1795 (org-agenda-remove-restriction-lock 'noupdate)
1796 (setq restriction (if region-p 'region 'subtree)))
1797 ((eq c ?<)
1798 (org-agenda-remove-restriction-lock 'noupdate)
1799 (setq restriction
1800 (cond
1801 ((eq restriction 'buffer)
1802 (if region-p 'region 'subtree))
1803 ((memq restriction '(subtree region))
1804 nil)
1805 (t 'buffer))))
1806 ((eq c ?>)
1807 (org-agenda-remove-restriction-lock 'noupdate)
1808 (setq restriction nil))
1809 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
1810 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
1811 ((and (> (length selstring) 0) (eq c ?\d))
1812 (delete-window)
1813 (org-agenda-get-restriction-and-command prefix-descriptions))
1815 ((equal c ?q) (error "Abort"))
1816 (t (error "Invalid key %c" c))))))))
1818 (defun org-run-agenda-series (name series)
1819 (org-prepare-agenda name)
1820 (let* ((org-agenda-multi t)
1821 (redo (list 'org-run-agenda-series name (list 'quote series)))
1822 (cmds (car series))
1823 (gprops (nth 1 series))
1824 match ;; The byte compiler incorrectly complains about this. Keep it!
1825 cmd type lprops)
1826 (while (setq cmd (pop cmds))
1827 (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
1828 (cond
1829 ((eq type 'agenda)
1830 (org-let2 gprops lprops
1831 '(call-interactively 'org-agenda-list)))
1832 ((eq type 'alltodo)
1833 (org-let2 gprops lprops
1834 '(call-interactively 'org-todo-list)))
1835 ((eq type 'search)
1836 (org-let2 gprops lprops
1837 '(org-search-view current-prefix-arg match nil)))
1838 ((eq type 'stuck)
1839 (org-let2 gprops lprops
1840 '(call-interactively 'org-agenda-list-stuck-projects)))
1841 ((eq type 'tags)
1842 (org-let2 gprops lprops
1843 '(org-tags-view current-prefix-arg match)))
1844 ((eq type 'tags-todo)
1845 (org-let2 gprops lprops
1846 '(org-tags-view '(4) match)))
1847 ((eq type 'todo)
1848 (org-let2 gprops lprops
1849 '(org-todo-list match)))
1850 ((fboundp type)
1851 (org-let2 gprops lprops
1852 '(funcall type match)))
1853 (t (error "Invalid type in command series"))))
1854 (widen)
1855 (setq org-agenda-redo-command redo)
1856 (goto-char (point-min)))
1857 (org-fit-agenda-window)
1858 (org-let (nth 1 series) '(org-finalize-agenda)))
1860 ;;;###autoload
1861 (defmacro org-batch-agenda (cmd-key &rest parameters)
1862 "Run an agenda command in batch mode and send the result to STDOUT.
1863 If CMD-KEY is a string of length 1, it is used as a key in
1864 `org-agenda-custom-commands' and triggers this command. If it is a
1865 longer string it is used as a tags/todo match string.
1866 Paramters are alternating variable names and values that will be bound
1867 before running the agenda command."
1868 (let (pars)
1869 (while parameters
1870 (push (list (pop parameters) (if parameters (pop parameters))) pars))
1871 (if (> (length cmd-key) 2)
1872 (eval (list 'let (nreverse pars)
1873 (list 'org-tags-view nil cmd-key)))
1874 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
1875 (set-buffer org-agenda-buffer-name)
1876 (princ (org-encode-for-stdout (buffer-string)))))
1878 (defun org-encode-for-stdout (string)
1879 (if (fboundp 'encode-coding-string)
1880 (encode-coding-string string buffer-file-coding-system)
1881 string))
1883 (defvar org-agenda-info nil)
1885 ;;;###autoload
1886 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
1887 "Run an agenda command in batch mode and send the result to STDOUT.
1888 If CMD-KEY is a string of length 1, it is used as a key in
1889 `org-agenda-custom-commands' and triggers this command. If it is a
1890 longer string it is used as a tags/todo match string.
1891 Paramters are alternating variable names and values that will be bound
1892 before running the agenda command.
1894 The output gives a line for each selected agenda item. Each
1895 item is a list of comma-separated values, like this:
1897 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
1899 category The category of the item
1900 head The headline, without TODO kwd, TAGS and PRIORITY
1901 type The type of the agenda entry, can be
1902 todo selected in TODO match
1903 tagsmatch selected in tags match
1904 diary imported from diary
1905 deadline a deadline on given date
1906 scheduled scheduled on given date
1907 timestamp entry has timestamp on given date
1908 closed entry was closed on given date
1909 upcoming-deadline warning about deadline
1910 past-scheduled forwarded scheduled item
1911 block entry has date block including g. date
1912 todo The todo keyword, if any
1913 tags All tags including inherited ones, separated by colons
1914 date The relevant date, like 2007-2-14
1915 time The time, like 15:00-16:50
1916 extra Sting with extra planning info
1917 priority-l The priority letter if any was given
1918 priority-n The computed numerical priority
1919 agenda-day The day in the agenda where this is listed"
1921 (let (pars)
1922 (while parameters
1923 (push (list (pop parameters) (if parameters (pop parameters))) pars))
1924 (push (list 'org-agenda-remove-tags t) pars)
1925 (if (> (length cmd-key) 2)
1926 (eval (list 'let (nreverse pars)
1927 (list 'org-tags-view nil cmd-key)))
1928 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
1929 (set-buffer org-agenda-buffer-name)
1930 (let* ((lines (org-split-string (buffer-string) "\n"))
1931 line)
1932 (while (setq line (pop lines))
1933 (catch 'next
1934 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
1935 (setq org-agenda-info
1936 (org-fix-agenda-info (text-properties-at 0 line)))
1937 (princ
1938 (org-encode-for-stdout
1939 (mapconcat 'org-agenda-export-csv-mapper
1940 '(org-category txt type todo tags date time-of-day extra
1941 priority-letter priority agenda-day)
1942 ",")))
1943 (princ "\n"))))))
1945 (defun org-fix-agenda-info (props)
1946 "Make sure all properties on an agenda item have a canonical form,
1947 so the export commands can easily use it."
1948 (let (tmp re)
1949 (when (setq tmp (plist-get props 'tags))
1950 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
1951 (when (setq tmp (plist-get props 'date))
1952 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
1953 (let ((calendar-date-display-form '(year "-" month "-" day)))
1954 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
1956 (setq tmp (calendar-date-string tmp)))
1957 (setq props (plist-put props 'date tmp)))
1958 (when (setq tmp (plist-get props 'day))
1959 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
1960 (let ((calendar-date-display-form '(year "-" month "-" day)))
1961 (setq tmp (calendar-date-string tmp)))
1962 (setq props (plist-put props 'day tmp))
1963 (setq props (plist-put props 'agenda-day tmp)))
1964 (when (setq tmp (plist-get props 'txt))
1965 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
1966 (plist-put props 'priority-letter (match-string 1 tmp))
1967 (setq tmp (replace-match "" t t tmp)))
1968 (when (and (setq re (plist-get props 'org-todo-regexp))
1969 (setq re (concat "\\`\\.*" re " ?"))
1970 (string-match re tmp))
1971 (plist-put props 'todo (match-string 1 tmp))
1972 (setq tmp (replace-match "" t t tmp)))
1973 (plist-put props 'txt tmp)))
1974 props)
1976 (defun org-agenda-export-csv-mapper (prop)
1977 (let ((res (plist-get org-agenda-info prop)))
1978 (setq res
1979 (cond
1980 ((not res) "")
1981 ((stringp res) res)
1982 (t (prin1-to-string res))))
1983 (while (string-match "," res)
1984 (setq res (replace-match ";" t t res)))
1985 (org-trim res)))
1988 ;;;###autoload
1989 (defun org-store-agenda-views (&rest parameters)
1990 (interactive)
1991 (eval (list 'org-batch-store-agenda-views)))
1993 ;; FIXME, why is this a macro?????
1994 ;;;###autoload
1995 (defmacro org-batch-store-agenda-views (&rest parameters)
1996 "Run all custom agenda commands that have a file argument."
1997 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
1998 (pop-up-frames nil)
1999 (dir default-directory)
2000 pars cmd thiscmdkey files opts cmd-or-set)
2001 (while parameters
2002 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2003 (setq pars (reverse pars))
2004 (save-window-excursion
2005 (while cmds
2006 (setq cmd (pop cmds)
2007 thiscmdkey (car cmd)
2008 cmd-or-set (nth 2 cmd)
2009 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2010 files (nth (if (listp cmd-or-set) 4 5) cmd))
2011 (if (stringp files) (setq files (list files)))
2012 (when files
2013 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2014 (list 'org-agenda nil thiscmdkey)))
2015 (set-buffer org-agenda-buffer-name)
2016 (while files
2017 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2018 (list 'org-write-agenda
2019 (expand-file-name (pop files) dir) nil t))))
2020 (and (get-buffer org-agenda-buffer-name)
2021 (kill-buffer org-agenda-buffer-name)))))))
2023 (defun org-write-agenda (file &optional open nosettings)
2024 "Write the current buffer (an agenda view) as a file.
2025 Depending on the extension of the file name, plain text (.txt),
2026 HTML (.html or .htm) or Postscript (.ps) is produced.
2027 If the extension is .ics, run icalendar export over all files used
2028 to construct the agenda and limit the export to entries listed in the
2029 agenda now.
2030 With prefic argument OPEN, open the new file immediately.
2031 If NOSETTINGS is given, do not scope the settings of
2032 `org-agenda-exporter-settings' into the export commands. This is used when
2033 the settings have already been scoped and we do not wish to overrule other,
2034 higher priority settings."
2035 (interactive "FWrite agenda to file: \nP")
2036 (if (not (file-writable-p file))
2037 (error "Cannot write agenda to file %s" file))
2038 (cond
2039 ((string-match "\\.html?\\'" file) (require 'htmlize))
2040 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2041 (org-let (if nosettings nil org-agenda-exporter-settings)
2042 '(save-excursion
2043 (save-window-excursion
2044 (org-agenda-mark-filtered-text)
2045 (let ((bs (copy-sequence (buffer-string))) beg)
2046 (org-agenda-unmark-filtered-text)
2047 (with-temp-buffer
2048 (insert bs)
2049 (org-agenda-remove-marked-text 'org-filtered)
2050 (while (setq beg (text-property-any (point-min) (point-max)
2051 'org-filtered t))
2052 (delete-region
2053 beg (or (next-single-property-change beg 'org-filtered)
2054 (point-max))))
2055 (run-hooks 'org-agenda-before-write-hook)
2056 (cond
2057 ((string-match "\\.html?\\'" file)
2058 (set-buffer (htmlize-buffer (current-buffer)))
2060 (when (and org-agenda-export-html-style
2061 (string-match "<style>" org-agenda-export-html-style))
2062 ;; replace <style> section with org-agenda-export-html-style
2063 (goto-char (point-min))
2064 (kill-region (- (search-forward "<style") 6)
2065 (search-forward "</style>"))
2066 (insert org-agenda-export-html-style))
2067 (write-file file)
2068 (kill-buffer (current-buffer))
2069 (message "HTML written to %s" file))
2070 ((string-match "\\.ps\\'" file)
2071 (require 'ps-print)
2072 (flet ((ps-get-buffer-name () "Agenda View"))
2073 (ps-print-buffer-with-faces file))
2074 (message "Postscript written to %s" file))
2075 ((string-match "\\.pdf\\'" file)
2076 (require 'ps-print)
2077 (flet ((ps-get-buffer-name () "Agenda View"))
2078 (ps-print-buffer-with-faces
2079 (concat (file-name-sans-extension file) ".ps")))
2080 (call-process "ps2pdf" nil nil nil
2081 (expand-file-name
2082 (concat (file-name-sans-extension file) ".ps"))
2083 (expand-file-name file))
2084 (message "PDF written to %s" file))
2085 ((string-match "\\.ics\\'" file)
2086 (let ((org-agenda-marker-table
2087 (org-create-marker-find-array
2088 (org-agenda-collect-markers)))
2089 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2090 (org-combined-agenda-icalendar-file file))
2091 (apply 'org-export-icalendar 'combine
2092 (org-agenda-files nil 'ifmode))))
2094 (let ((bs (buffer-string)))
2095 (find-file file)
2096 (erase-buffer)
2097 (insert bs)
2098 (save-buffer 0)
2099 (kill-buffer (current-buffer))
2100 (message "Plain text written to %s" file))))))))
2101 (set-buffer org-agenda-buffer-name))
2102 (when open (org-open-file file)))
2104 (defvar org-agenda-filter-overlays nil)
2106 (defun org-agenda-mark-filtered-text ()
2107 "Mark all text hidden by filtering with a text property."
2108 (let ((inhibit-read-only t))
2109 (mapc
2110 (lambda (o)
2111 (when (equal (org-overlay-buffer o) (current-buffer))
2112 (put-text-property
2113 (org-overlay-start o) (org-overlay-end o)
2114 'org-filtered t)))
2115 org-agenda-filter-overlays)))
2117 (defun org-agenda-unmark-filtered-text ()
2118 "Remove the filtering text property."
2119 (let ((inhibit-read-only t))
2120 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2122 (defun org-agenda-remove-marked-text (property &optional value)
2123 "Delete all text marked with VALUE of PROPERTY.
2124 VALUE defaults to t."
2125 (let (beg)
2126 (setq value (or value t))
2127 (while (setq beg (text-property-any (point-min) (point-max)
2128 property value))
2129 (delete-region
2130 beg (or (next-single-property-change beg 'org-filtered)
2131 (point-max))))))
2133 (defun org-agenda-add-entry-text ()
2134 "Add entry text to agenda lines.
2135 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2136 entry text following headings shown in the agenda.
2137 Drawers will be excluded, also the line with scheduling/deadline info."
2138 (when (> org-agenda-add-entry-text-maxlines 0)
2139 (let (m txt drawer-re kwd-time-re ind)
2140 (goto-char (point-min))
2141 (while (not (eobp))
2142 (if (not (setq m (get-text-property (point) 'org-hd-marker)))
2143 (beginning-of-line 2)
2144 (save-excursion
2145 (with-current-buffer (marker-buffer m)
2146 (if (not (org-mode-p))
2147 (setq txt "")
2148 (save-excursion
2149 (save-restriction
2150 (widen)
2151 (goto-char m)
2152 (beginning-of-line 2)
2153 (setq txt (buffer-substring
2154 (point)
2155 (progn (outline-next-heading) (point)))
2156 drawer-re org-drawer-regexp
2157 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2158 ".*\n?"))
2159 (with-temp-buffer
2160 (insert txt)
2161 (when org-agenda-add-entry-text-descriptive-links
2162 (goto-char (point-min))
2163 (while (org-activate-bracket-links (point-max))
2164 (add-text-properties (match-beginning 0) (match-end 0)
2165 '(face org-link))))
2166 (goto-char (point-min))
2167 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2168 (set-text-properties (match-beginning 0) (match-end 0)
2169 nil))
2170 (goto-char (point-min))
2171 (while (re-search-forward drawer-re nil t)
2172 (delete-region
2173 (match-beginning 0)
2174 (progn (re-search-forward
2175 "^[ \t]*:END:.*\n?" nil 'move)
2176 (point))))
2177 (goto-char (point-min))
2178 (while (re-search-forward kwd-time-re nil t)
2179 (replace-match ""))
2180 (if (re-search-forward "[ \t\n]+\\'" nil t)
2181 (replace-match ""))
2182 (goto-char (point-min))
2183 ;; find min indentation
2184 (goto-char (point-min))
2185 (untabify (point-min) (point-max))
2186 (setq ind (org-get-indentation))
2187 (while (not (eobp))
2188 (unless (looking-at "[ \t]*$")
2189 (setq ind (min ind (org-get-indentation))))
2190 (beginning-of-line 2))
2191 (goto-char (point-min))
2192 (while (not (eobp))
2193 (unless (looking-at "[ \t]*$")
2194 (move-to-column ind)
2195 (delete-region (point-at-bol) (point)))
2196 (beginning-of-line 2))
2197 (goto-char (point-min))
2198 (while (and (not (eobp)) (re-search-forward "^" nil t))
2199 (replace-match " > "))
2200 (goto-char (point-min))
2201 (while (looking-at "[ \t]*\n") (replace-match ""))
2202 (goto-char (point-max))
2203 (when (> (org-current-line)
2204 (1+ org-agenda-add-entry-text-maxlines))
2205 (goto-line (1+ org-agenda-add-entry-text-maxlines))
2206 (backward-char 1))
2207 (setq txt (buffer-substring (point-min) (point)))))))))
2208 (end-of-line 1)
2209 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2211 (defun org-agenda-collect-markers ()
2212 "Collect the markers pointing to entries in the agenda buffer."
2213 (let (m markers)
2214 (save-excursion
2215 (goto-char (point-min))
2216 (while (not (eobp))
2217 (when (setq m (or (get-text-property (point) 'org-hd-marker)
2218 (get-text-property (point) 'org-marker)))
2219 (push m markers))
2220 (beginning-of-line 2)))
2221 (nreverse markers)))
2223 (defun org-create-marker-find-array (marker-list)
2224 "Create a alist of files names with all marker positions in that file."
2225 (let (f tbl m a p)
2226 (while (setq m (pop marker-list))
2227 (setq p (marker-position m)
2228 f (buffer-file-name (or (buffer-base-buffer
2229 (marker-buffer m))
2230 (marker-buffer m))))
2231 (if (setq a (assoc f tbl))
2232 (push (marker-position m) (cdr a))
2233 (push (list f p) tbl)))
2234 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2235 tbl)))
2237 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2238 (defun org-check-agenda-marker-table ()
2239 "Check of the current entry is on the marker list."
2240 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2242 (and (setq a (assoc file org-agenda-marker-table))
2243 (save-match-data
2244 (save-excursion
2245 (org-back-to-heading t)
2246 (member (point) (cdr a)))))))
2248 (defun org-check-for-org-mode ()
2249 "Make sure current buffer is in org-mode. Error if not."
2250 (or (org-mode-p)
2251 (error "Cannot execute org-mode agenda command on buffer in %s."
2252 major-mode)))
2254 (defun org-fit-agenda-window ()
2255 "Fit the window to the buffer size."
2256 (and (memq org-agenda-window-setup '(reorganize-frame))
2257 (fboundp 'fit-window-to-buffer)
2258 (org-fit-window-to-buffer
2260 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2261 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2263 ;;; Agenda prepare and finalize
2265 (defvar org-agenda-multi nil) ; dynamically scoped
2266 (defvar org-agenda-buffer-name "*Org Agenda*")
2267 (defvar org-pre-agenda-window-conf nil)
2268 (defvar org-agenda-columns-active nil)
2269 (defvar org-agenda-name nil)
2270 (defvar org-agenda-filter nil)
2271 (defvar org-agenda-filter-preset nil
2272 "A preset of the tags filter used for secondary agenda filtering.
2273 This must be a list of strings, each string must be a single tag preceeded
2274 by \"+\" or \"-\".
2275 This variable should not be set directly, but agenda custom commands can
2276 bind it in the options section.")
2278 (defun org-prepare-agenda (&optional name)
2279 (setq org-todo-keywords-for-agenda nil)
2280 (setq org-done-keywords-for-agenda nil)
2281 (setq org-agenda-filter nil)
2282 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2283 (if org-agenda-multi
2284 (progn
2285 (setq buffer-read-only nil)
2286 (goto-char (point-max))
2287 (unless (or (bobp) org-agenda-compact-blocks)
2288 (insert "\n"
2289 (if (stringp org-agenda-block-separator)
2290 org-agenda-block-separator
2291 (make-string (window-width) org-agenda-block-separator))
2292 "\n"))
2293 (narrow-to-region (point) (point-max)))
2294 (org-agenda-reset-markers)
2295 (setq org-agenda-contributing-files nil)
2296 (setq org-agenda-columns-active nil)
2297 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2298 (setq org-todo-keywords-for-agenda
2299 (org-uniquify org-todo-keywords-for-agenda))
2300 (setq org-done-keywords-for-agenda
2301 (org-uniquify org-done-keywords-for-agenda))
2302 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2303 (awin (get-buffer-window abuf)))
2304 (cond
2305 ((equal (current-buffer) abuf) nil)
2306 (awin (select-window awin))
2307 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2308 ((equal org-agenda-window-setup 'current-window)
2309 (switch-to-buffer abuf))
2310 ((equal org-agenda-window-setup 'other-window)
2311 (org-switch-to-buffer-other-window abuf))
2312 ((equal org-agenda-window-setup 'other-frame)
2313 (switch-to-buffer-other-frame abuf))
2314 ((equal org-agenda-window-setup 'reorganize-frame)
2315 (delete-other-windows)
2316 (org-switch-to-buffer-other-window abuf))))
2317 (setq buffer-read-only nil)
2318 (let ((inhibit-read-only t)) (erase-buffer))
2319 (org-agenda-mode)
2320 (and name (not org-agenda-name)
2321 (org-set-local 'org-agenda-name name)))
2322 (setq buffer-read-only nil))
2324 (defun org-finalize-agenda ()
2325 "Finishing touch for the agenda buffer, called just before displaying it."
2326 (unless org-agenda-multi
2327 (save-excursion
2328 (let ((inhibit-read-only t))
2329 (goto-char (point-min))
2330 (while (org-activate-bracket-links (point-max))
2331 (add-text-properties (match-beginning 0) (match-end 0)
2332 '(face org-link)))
2333 (org-agenda-align-tags)
2334 (unless org-agenda-with-colors
2335 (remove-text-properties (point-min) (point-max) '(face nil))))
2336 (if (and (boundp 'org-agenda-overriding-columns-format)
2337 org-agenda-overriding-columns-format)
2338 (org-set-local 'org-agenda-overriding-columns-format
2339 org-agenda-overriding-columns-format))
2340 (if (and (boundp 'org-agenda-view-columns-initially)
2341 org-agenda-view-columns-initially)
2342 (org-agenda-columns))
2343 (when org-agenda-fontify-priorities
2344 (org-agenda-fontify-priorities))
2345 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2346 (org-agenda-dim-blocked-tasks))
2347 (run-hooks 'org-finalize-agenda-hook)
2348 (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
2349 (when (get 'org-agenda-filter :preset-filter)
2350 (org-agenda-filter-apply org-agenda-filter))
2353 (defun org-agenda-fontify-priorities ()
2354 "Make highest priority lines bold, and lowest italic."
2355 (interactive)
2356 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2357 (org-delete-overlay o)))
2358 (org-overlays-in (point-min) (point-max)))
2359 (save-excursion
2360 (let ((inhibit-read-only t)
2361 b e p ov h l)
2362 (goto-char (point-min))
2363 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2364 (setq h (or (get-char-property (point) 'org-highest-priority)
2365 org-highest-priority)
2366 l (or (get-char-property (point) 'org-lowest-priority)
2367 org-lowest-priority)
2368 p (string-to-char (match-string 1))
2369 b (match-beginning 0)
2370 e (if (eq org-agenda-fontify-priorities 'cookies)
2371 (match-end 0)
2372 (point-at-eol))
2373 ov (org-make-overlay b e))
2374 (org-overlay-put
2375 ov 'face
2376 (cond ((cdr (assoc p org-priority-faces)))
2377 ((and (listp org-agenda-fontify-priorities)
2378 (cdr (assoc p org-agenda-fontify-priorities))))
2379 ((equal p l) 'italic)
2380 ((equal p h) 'bold)))
2381 (org-overlay-put ov 'org-type 'org-priority)))))
2383 (defun org-agenda-dim-blocked-tasks ()
2384 "Dim currently blocked TODO's in the agenda display."
2385 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2386 (org-delete-overlay o)))
2387 (org-overlays-in (point-min) (point-max)))
2388 (save-excursion
2389 (let ((inhibit-read-only t)
2390 (org-depend-tag-blocked nil)
2391 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2392 org-blocked-by-checkboxes
2393 invis1 b e p ov h l)
2394 (goto-char (point-min))
2395 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2396 (and pos (goto-char (1+ pos))))
2397 (setq org-blocked-by-checkboxes nil invis1 invis)
2398 (let ((marker (get-text-property (point) 'org-hd-marker)))
2399 (when (and marker
2400 (not (with-current-buffer (marker-buffer marker)
2401 (save-excursion
2402 (goto-char marker)
2403 (run-hook-with-args-until-failure
2404 'org-blocker-hook
2405 (list :type 'todo-state-change
2406 :position marker
2407 :from 'todo
2408 :to 'done))))))
2409 (if org-blocked-by-checkboxes (setq invis1 nil))
2410 (setq b (if invis1 (max (point-min) (1- (point))) (point))
2411 e (point-at-eol)
2412 ov (org-make-overlay b e))
2413 (if invis1
2414 (org-overlay-put ov 'invisible t)
2415 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2416 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2418 (defvar org-agenda-skip-function nil
2419 "Function to be called at each match during agenda construction.
2420 If this function returns nil, the current match should not be skipped.
2421 Otherwise, the function must return a position from where the search
2422 should be continued.
2423 This may also be a Lisp form, it will be evaluated.
2424 Never set this variable using `setq' or so, because then it will apply
2425 to all future agenda commands. Instead, bind it with `let' to scope
2426 it dynamically into the agenda-constructing command. A good way to set
2427 it is through options in org-agenda-custom-commands.")
2429 (defun org-agenda-skip ()
2430 "Throw to `:skip' in places that should be skipped.
2431 Also moves point to the end of the skipped region, so that search can
2432 continue from there."
2433 (let ((p (point-at-bol)) to fp)
2434 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2435 (get-text-property p :org-archived)
2436 (org-end-of-subtree t)
2437 (throw :skip t))
2438 (and org-agenda-skip-comment-trees
2439 (get-text-property p :org-comment)
2440 (org-end-of-subtree t)
2441 (throw :skip t))
2442 (if (equal (char-after p) ?#) (throw :skip t))
2443 (when (and (or (setq fp (functionp org-agenda-skip-function))
2444 (consp org-agenda-skip-function))
2445 (setq to (save-excursion
2446 (save-match-data
2447 (if fp
2448 (funcall org-agenda-skip-function)
2449 (eval org-agenda-skip-function))))))
2450 (goto-char to)
2451 (throw :skip t))))
2453 (defvar org-agenda-markers nil
2454 "List of all currently active markers created by `org-agenda'.")
2455 (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
2456 "Creation time of the last agenda marker.")
2458 (defun org-agenda-new-marker (&optional pos)
2459 "Return a new agenda marker.
2460 Org-mode keeps a list of these markers and resets them when they are
2461 no longer in use."
2462 (let ((m (copy-marker (or pos (point)))))
2463 (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
2464 (push m org-agenda-markers)
2467 (defun org-agenda-reset-markers ()
2468 "Reset markers created by `org-agenda'."
2469 (while org-agenda-markers
2470 (move-marker (pop org-agenda-markers) nil)))
2472 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2473 "Save relative positions of markers in region."
2474 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2475 org-agenda-markers))
2477 ;;; Agenda timeline
2479 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2481 (defun org-timeline (&optional include-all)
2482 "Show a time-sorted view of the entries in the current org file.
2483 Only entries with a time stamp of today or later will be listed. With
2484 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2485 under the current date.
2486 If the buffer contains an active region, only check the region for
2487 dates."
2488 (interactive "P")
2489 (require 'calendar)
2490 (org-compile-prefix-format 'timeline)
2491 (org-set-sorting-strategy 'timeline)
2492 (let* ((dopast t)
2493 (dotodo include-all)
2494 (doclosed org-agenda-show-log)
2495 (entry buffer-file-name)
2496 (date (calendar-current-date))
2497 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2498 (end (if (org-region-active-p) (region-end) (point-max)))
2499 (day-numbers (org-get-all-dates beg end 'no-ranges
2500 t doclosed ; always include today
2501 org-timeline-show-empty-dates))
2502 (org-deadline-warning-days 0)
2503 (org-agenda-only-exact-dates t)
2504 (today (time-to-days (current-time)))
2505 (past t)
2506 args
2507 s e rtn d emptyp wd)
2508 (setq org-agenda-redo-command
2509 (list 'progn
2510 (list 'org-switch-to-buffer-other-window (current-buffer))
2511 (list 'org-timeline (list 'quote include-all))))
2512 (if (not dopast)
2513 ;; Remove past dates from the list of dates.
2514 (setq day-numbers (delq nil (mapcar (lambda(x)
2515 (if (>= x today) x nil))
2516 day-numbers))))
2517 (org-prepare-agenda (concat "Timeline "
2518 (file-name-nondirectory buffer-file-name)))
2519 (if doclosed (push :closed args))
2520 (push :timestamp args)
2521 (push :deadline args)
2522 (push :scheduled args)
2523 (push :sexp args)
2524 (if dotodo (push :todo args))
2525 (while (setq d (pop day-numbers))
2526 (if (and (listp d) (eq (car d) :omitted))
2527 (progn
2528 (setq s (point))
2529 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2530 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2531 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2532 (if (and (>= d today)
2533 dopast
2534 past)
2535 (progn
2536 (setq past nil)
2537 (insert (make-string 79 ?-) "\n")))
2538 (setq date (calendar-gregorian-from-absolute d)
2539 wd (calendar-day-of-week date))
2540 (setq s (point))
2541 (setq rtn (and (not emptyp)
2542 (apply 'org-agenda-get-day-entries entry
2543 date args)))
2544 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2545 (progn
2546 (insert
2547 (if (stringp org-agenda-format-date)
2548 (format-time-string org-agenda-format-date
2549 (org-time-from-absolute date))
2550 (funcall org-agenda-format-date date))
2551 "\n")
2552 (put-text-property s (1- (point)) 'face
2553 (if (member wd org-agenda-weekend-days)
2554 'org-agenda-date-weekend
2555 'org-agenda-date))
2556 (put-text-property s (1- (point)) 'org-date-line t)
2557 (if (equal d today)
2558 (put-text-property s (1- (point)) 'org-today t))
2559 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2560 (put-text-property s (1- (point)) 'day d)))))
2561 (goto-char (point-min))
2562 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2563 (point-min)))
2564 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2565 (org-finalize-agenda)
2566 (setq buffer-read-only t)))
2568 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2569 "Return a list of all relevant day numbers from BEG to END buffer positions.
2570 If NO-RANGES is non-nil, include only the start and end dates of a range,
2571 not every single day in the range. If FORCE-TODAY is non-nil, make
2572 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2573 inactive time stamps (those in square brackets) are included.
2574 When EMPTY is non-nil, also include days without any entries."
2575 (let ((re (concat
2576 (if pre-re pre-re "")
2577 (if inactive org-ts-regexp-both org-ts-regexp)))
2578 dates dates1 date day day1 day2 ts1 ts2)
2579 (if force-today
2580 (setq dates (list (time-to-days (current-time)))))
2581 (save-excursion
2582 (goto-char beg)
2583 (while (re-search-forward re end t)
2584 (setq day (time-to-days (org-time-string-to-time
2585 (substring (match-string 1) 0 10))))
2586 (or (memq day dates) (push day dates)))
2587 (unless no-ranges
2588 (goto-char beg)
2589 (while (re-search-forward org-tr-regexp end t)
2590 (setq ts1 (substring (match-string 1) 0 10)
2591 ts2 (substring (match-string 2) 0 10)
2592 day1 (time-to-days (org-time-string-to-time ts1))
2593 day2 (time-to-days (org-time-string-to-time ts2)))
2594 (while (< (setq day1 (1+ day1)) day2)
2595 (or (memq day1 dates) (push day1 dates)))))
2596 (setq dates (sort dates '<))
2597 (when empty
2598 (while (setq day (pop dates))
2599 (setq day2 (car dates))
2600 (push day dates1)
2601 (when (and day2 empty)
2602 (if (or (eq empty t)
2603 (and (numberp empty) (<= (- day2 day) empty)))
2604 (while (< (setq day (1+ day)) day2)
2605 (push (list day) dates1))
2606 (push (cons :omitted (- day2 day)) dates1))))
2607 (setq dates (nreverse dates1)))
2608 dates)))
2610 ;;; Agenda Daily/Weekly
2612 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2613 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2614 "Custom commands can set this variable in the options section.")
2615 (defvar org-agenda-last-arguments nil
2616 "The arguments of the previous call to org-agenda")
2617 (defvar org-starting-day nil) ; local variable in the agenda buffer
2618 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2619 (defvar org-include-all-loc nil) ; local variable
2620 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2622 ;;;###autoload
2623 (defun org-agenda-list (&optional include-all start-day ndays)
2624 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2625 The view will be for the current day or week, but from the overview buffer
2626 you will be able to go to other days/weeks.
2628 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2629 all unfinished TODO items will also be shown, before the agenda.
2630 This feature is considered obsolete, please use the TODO list or a block
2631 agenda instead.
2633 With a numeric prefix argument in an interactive call, the agenda will
2634 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2635 the number of days. NDAYS defaults to `org-agenda-ndays'.
2637 START-DAY defaults to TODAY, or to the most recent match for the weekday
2638 given in `org-agenda-start-on-weekday'."
2639 (interactive "P")
2640 (if (and (integerp include-all) (> include-all 0))
2641 (setq ndays include-all include-all nil))
2642 (setq ndays (or ndays org-agenda-ndays)
2643 start-day (or start-day org-agenda-start-day))
2644 (if org-agenda-overriding-arguments
2645 (setq include-all (car org-agenda-overriding-arguments)
2646 start-day (nth 1 org-agenda-overriding-arguments)
2647 ndays (nth 2 org-agenda-overriding-arguments)))
2648 (if (stringp start-day)
2649 ;; Convert to an absolute day number
2650 (setq start-day (time-to-days (org-read-date nil t start-day))))
2651 (setq org-agenda-last-arguments (list include-all start-day ndays))
2652 (org-compile-prefix-format 'agenda)
2653 (org-set-sorting-strategy 'agenda)
2654 (require 'calendar)
2655 (let* ((org-agenda-start-on-weekday
2656 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
2657 org-agenda-start-on-weekday nil))
2658 (thefiles (org-agenda-files nil 'ifmode))
2659 (files thefiles)
2660 (today (time-to-days
2661 (time-subtract (current-time)
2662 (list 0 (* 3600 org-extend-today-until) 0))))
2663 (sd (or start-day today))
2664 (start (if (or (null org-agenda-start-on-weekday)
2665 (< org-agenda-ndays 7))
2667 (let* ((nt (calendar-day-of-week
2668 (calendar-gregorian-from-absolute sd)))
2669 (n1 org-agenda-start-on-weekday)
2670 (d (- nt n1)))
2671 (- sd (+ (if (< d 0) 7 0) d)))))
2672 (day-numbers (list start))
2673 (day-cnt 0)
2674 (inhibit-redisplay (not debug-on-error))
2675 s e rtn rtnall file date d start-pos end-pos todayp nd wd
2676 clocktable-start clocktable-end)
2677 (setq org-agenda-redo-command
2678 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
2679 ;; Make the list of days
2680 (setq ndays (or ndays org-agenda-ndays)
2681 nd ndays)
2682 (while (> ndays 1)
2683 (push (1+ (car day-numbers)) day-numbers)
2684 (setq ndays (1- ndays)))
2685 (setq day-numbers (nreverse day-numbers))
2686 (setq clocktable-start (car day-numbers)
2687 clocktable-end (1+ (or (org-last day-numbers) 0)))
2688 (org-prepare-agenda "Day/Week")
2689 (org-set-local 'org-starting-day (car day-numbers))
2690 (org-set-local 'org-include-all-loc include-all)
2691 (org-set-local 'org-agenda-span
2692 (org-agenda-ndays-to-span nd))
2693 (when (and (or include-all org-agenda-include-all-todo)
2694 (member today day-numbers))
2695 (setq files thefiles
2696 rtnall nil)
2697 (while (setq file (pop files))
2698 (catch 'nextfile
2699 (org-check-agenda-file file)
2700 (setq date (calendar-gregorian-from-absolute today)
2701 rtn (org-agenda-get-day-entries
2702 file date :todo))
2703 (setq rtnall (append rtnall rtn))))
2704 (when rtnall
2705 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
2706 (add-text-properties (point-min) (1- (point))
2707 (list 'face 'org-agenda-structure))
2708 (insert (org-finalize-agenda-entries rtnall) "\n")))
2709 (unless org-agenda-compact-blocks
2710 (let* ((d1 (car day-numbers))
2711 (d2 (org-last day-numbers))
2712 (w1 (org-days-to-iso-week d1))
2713 (w2 (org-days-to-iso-week d2)))
2714 (setq s (point))
2715 (if org-agenda-overriding-header
2716 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2717 nil 'face 'org-agenda-structure) "\n")
2718 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
2719 "-agenda"
2720 (if (< (- d2 d1) 350)
2721 (if (= w1 w2)
2722 (format " (W%02d)" w1)
2723 (format " (W%02d-W%02d)" w1 w2))
2725 ":\n")))
2726 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
2727 'org-date-line t)))
2728 (while (setq d (pop day-numbers))
2729 (setq date (calendar-gregorian-from-absolute d)
2730 wd (calendar-day-of-week date)
2731 s (point))
2732 (if (or (setq todayp (= d today))
2733 (and (not start-pos) (= d sd)))
2734 (setq start-pos (point))
2735 (if (and start-pos (not end-pos))
2736 (setq end-pos (point))))
2737 (setq files thefiles
2738 rtnall nil)
2739 (while (setq file (pop files))
2740 (catch 'nextfile
2741 (org-check-agenda-file file)
2742 (cond
2743 ((eq org-agenda-show-log 'only)
2744 (setq rtn (org-agenda-get-day-entries
2745 file date :closed)))
2746 (org-agenda-show-log
2747 (setq rtn (org-agenda-get-day-entries
2748 file date
2749 :deadline :scheduled :timestamp :sexp :closed)))
2751 (setq rtn (org-agenda-get-day-entries
2752 file date
2753 :deadline :scheduled :sexp :timestamp))))
2754 (setq rtnall (append rtnall rtn))))
2755 (if org-agenda-include-diary
2756 (let ((org-agenda-search-headline-for-time t))
2757 (require 'diary-lib)
2758 (setq rtn (org-get-entries-from-diary date))
2759 (setq rtnall (append rtnall rtn))))
2760 (if (or rtnall org-agenda-show-all-dates)
2761 (progn
2762 (setq day-cnt (1+ day-cnt))
2763 (insert
2764 (if (stringp org-agenda-format-date)
2765 (format-time-string org-agenda-format-date
2766 (org-time-from-absolute date))
2767 (funcall org-agenda-format-date date))
2768 "\n")
2769 (put-text-property s (1- (point)) 'face
2770 (if (member wd org-agenda-weekend-days)
2771 'org-agenda-date-weekend
2772 'org-agenda-date))
2773 (put-text-property s (1- (point)) 'org-date-line t)
2774 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
2775 (if todayp (put-text-property s (1- (point)) 'org-today t))
2776 (if rtnall (insert
2777 (org-finalize-agenda-entries
2778 (org-agenda-add-time-grid-maybe
2779 rtnall nd todayp))
2780 "\n"))
2781 (put-text-property s (1- (point)) 'day d)
2782 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
2783 (when (and org-agenda-clockreport-mode clocktable-start)
2784 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
2785 ;; the above line is to ensure the restricted range!
2786 (p org-agenda-clockreport-parameter-plist)
2787 tbl)
2788 (setq p (org-plist-delete p :block))
2789 (setq p (plist-put p :tstart clocktable-start))
2790 (setq p (plist-put p :tend clocktable-end))
2791 (setq p (plist-put p :scope 'agenda))
2792 (setq tbl (apply 'org-get-clocktable p))
2793 (insert tbl)))
2794 (goto-char (point-min))
2795 (or org-agenda-multi (org-fit-agenda-window))
2796 (unless (and (pos-visible-in-window-p (point-min))
2797 (pos-visible-in-window-p (point-max)))
2798 (goto-char (1- (point-max)))
2799 (recenter -1)
2800 (if (not (pos-visible-in-window-p (or start-pos 1)))
2801 (progn
2802 (goto-char (or start-pos 1))
2803 (recenter 1))))
2804 (goto-char (or start-pos 1))
2805 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
2806 (org-finalize-agenda)
2807 (setq buffer-read-only t)
2808 (message "")))
2810 (defun org-agenda-ndays-to-span (n)
2811 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
2813 ;;; Agenda word search
2815 (defvar org-agenda-search-history nil)
2816 (defvar org-todo-only nil)
2818 (defvar org-search-syntax-table nil
2819 "Special syntax table for org-mode search.
2820 In this table, we have single quotes not as word constituents, to
2821 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
2823 (defun org-search-syntax-table ()
2824 (unless org-search-syntax-table
2825 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
2826 (modify-syntax-entry ?' "." org-search-syntax-table)
2827 (modify-syntax-entry ?` "." org-search-syntax-table))
2828 org-search-syntax-table)
2830 ;;;###autoload
2831 (defun org-search-view (&optional todo-only string edit-at)
2832 "Show all entries that contain words or regular expressions.
2833 If the first character of the search string is an asterisks,
2834 search only the headlines.
2836 With optional prefix argument TODO-ONLY, only consider entries that are
2837 TODO entries. The argument STRING can be used to pass a default search
2838 string into this function. If EDIT-AT is non-nil, it means that the
2839 user should get a chance to edit this string, with cursor at position
2840 EDIT-AT.
2842 The search string is broken into \"words\" by splitting at whitespace.
2843 The individual words are then interpreted as a boolean expression with
2844 logical AND. Words prefixed with a minus must not occur in the entry.
2845 Words without a prefix or prefixed with a plus must occur in the entry.
2846 Matching is case-insensitive and the words are enclosed by word delimiters.
2848 Words enclosed by curly braces are interpreted as regular expressions
2849 that must or must not match in the entry.
2851 If the search string starts with an asterisk, search only in headlines.
2852 If (possibly after the leading star) the search string starts with an
2853 exclamation mark, this also means to look at TODO entries only, an effect
2854 that can also be achieved with a prefix argument.
2856 This command searches the agenda files, and in addition the files listed
2857 in `org-agenda-text-search-extra-files'."
2858 (interactive "P")
2859 (org-compile-prefix-format 'search)
2860 (org-set-sorting-strategy 'search)
2861 (org-prepare-agenda "SEARCH")
2862 (let* ((props (list 'face nil
2863 'done-face 'org-agenda-done
2864 'org-not-done-regexp org-not-done-regexp
2865 'org-todo-regexp org-todo-regexp
2866 'org-complex-heading-regexp org-complex-heading-regexp
2867 'mouse-face 'highlight
2868 'keymap org-agenda-keymap
2869 'help-echo (format "mouse-2 or RET jump to location")))
2870 regexp rtn rtnall files file pos
2871 marker category tags c neg re
2872 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
2873 (unless (and (not edit-at)
2874 (stringp string)
2875 (string-match "\\S-" string))
2876 (setq string (read-string "[+-]Word/{Regexp} ...: "
2877 (cond
2878 ((integerp edit-at) (cons string edit-at))
2879 (edit-at string))
2880 'org-agenda-search-history)))
2881 (org-set-local 'org-todo-only todo-only)
2882 (setq org-agenda-redo-command
2883 (list 'org-search-view (if todo-only t nil) string
2884 '(if current-prefix-arg 1 nil)))
2885 (setq org-agenda-query-string string)
2887 (if (equal (string-to-char string) ?*)
2888 (setq hdl-only t
2889 words (substring string 1))
2890 (setq words string))
2891 (when (equal (string-to-char words) ?!)
2892 (setq todo-only t
2893 words (substring words 1)))
2894 (setq words (org-split-string words))
2895 (mapc (lambda (w)
2896 (setq c (string-to-char w))
2897 (if (equal c ?-)
2898 (setq neg t w (substring w 1))
2899 (if (equal c ?+)
2900 (setq neg nil w (substring w 1))
2901 (setq neg nil)))
2902 (if (string-match "\\`{.*}\\'" w)
2903 (setq re (substring w 1 -1))
2904 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
2905 (if neg (push re regexps-) (push re regexps+)))
2906 words)
2907 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
2908 (if (not regexps+)
2909 (setq regexp (concat "^" org-outline-regexp))
2910 (setq regexp (pop regexps+))
2911 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
2912 regexp))))
2913 (setq files (org-agenda-files nil 'ifmode))
2914 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
2915 (pop org-agenda-text-search-extra-files)
2916 (setq files (org-add-archive-files files)))
2917 (setq files (append files org-agenda-text-search-extra-files)
2918 rtnall nil)
2919 (while (setq file (pop files))
2920 (setq ee nil)
2921 (catch 'nextfile
2922 (org-check-agenda-file file)
2923 (setq buffer (if (file-exists-p file)
2924 (org-get-agenda-file-buffer file)
2925 (error "No such file %s" file)))
2926 (if (not buffer)
2927 ;; If file does not exist, make sure an error message is sent
2928 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
2929 file))))
2930 (with-current-buffer buffer
2931 (with-syntax-table (org-search-syntax-table)
2932 (unless (org-mode-p)
2933 (error "Agenda file %s is not in `org-mode'" file))
2934 (let ((case-fold-search t))
2935 (save-excursion
2936 (save-restriction
2937 (if org-agenda-restrict
2938 (narrow-to-region org-agenda-restrict-begin
2939 org-agenda-restrict-end)
2940 (widen))
2941 (goto-char (point-min))
2942 (unless (or (org-on-heading-p)
2943 (outline-next-heading))
2944 (throw 'nextfile t))
2945 (goto-char (max (point-min) (1- (point))))
2946 (while (re-search-forward regexp nil t)
2947 (org-back-to-heading t)
2948 (skip-chars-forward "* ")
2949 (setq beg (point-at-bol)
2950 beg1 (point)
2951 end (progn (outline-next-heading) (point)))
2952 (catch :skip
2953 (goto-char beg)
2954 (org-agenda-skip)
2955 (setq str (buffer-substring-no-properties
2956 (point-at-bol)
2957 (if hdl-only (point-at-eol) end)))
2958 (mapc (lambda (wr) (when (string-match wr str)
2959 (goto-char (1- end))
2960 (throw :skip t)))
2961 regexps-)
2962 (mapc (lambda (wr) (unless (string-match wr str)
2963 (goto-char (1- end))
2964 (throw :skip t)))
2965 (if todo-only
2966 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
2967 regexps+)
2968 regexps+))
2969 (goto-char beg)
2970 (setq marker (org-agenda-new-marker (point))
2971 category (org-get-category)
2972 tags (org-get-tags-at (point))
2973 txt (org-format-agenda-item
2975 (buffer-substring-no-properties
2976 beg1 (point-at-eol))
2977 category tags))
2978 (org-add-props txt props
2979 'org-marker marker 'org-hd-marker marker
2980 'org-todo-regexp org-todo-regexp
2981 'org-complex-heading-regexp org-complex-heading-regexp
2982 'priority 1000 'org-category category
2983 'type "search")
2984 (push txt ee)
2985 (goto-char (1- end))))))))))
2986 (setq rtn (nreverse ee))
2987 (setq rtnall (append rtnall rtn)))
2988 (if org-agenda-overriding-header
2989 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2990 nil 'face 'org-agenda-structure) "\n")
2991 (insert "Search words: ")
2992 (add-text-properties (point-min) (1- (point))
2993 (list 'face 'org-agenda-structure))
2994 (setq pos (point))
2995 (insert string "\n")
2996 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2997 (setq pos (point))
2998 (unless org-agenda-multi
2999 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3000 (add-text-properties pos (1- (point))
3001 (list 'face 'org-agenda-structure))))
3002 (when rtnall
3003 (insert (org-finalize-agenda-entries rtnall) "\n"))
3004 (goto-char (point-min))
3005 (or org-agenda-multi (org-fit-agenda-window))
3006 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3007 (org-finalize-agenda)
3008 (setq buffer-read-only t)))
3010 ;;; Agenda TODO list
3012 (defvar org-select-this-todo-keyword nil)
3013 (defvar org-last-arg nil)
3015 ;;;###autoload
3016 (defun org-todo-list (arg)
3017 "Show all TODO entries from all agenda file in a single list.
3018 The prefix arg can be used to select a specific TODO keyword and limit
3019 the list to these. When using \\[universal-argument], you will be prompted
3020 for a keyword. A numeric prefix directly selects the Nth keyword in
3021 `org-todo-keywords-1'."
3022 (interactive "P")
3023 (require 'calendar)
3024 (org-compile-prefix-format 'todo)
3025 (org-set-sorting-strategy 'todo)
3026 (org-prepare-agenda "TODO")
3027 (let* ((today (time-to-days (current-time)))
3028 (date (calendar-gregorian-from-absolute today))
3029 (kwds org-todo-keywords-for-agenda)
3030 (completion-ignore-case t)
3031 (org-select-this-todo-keyword
3032 (if (stringp arg) arg
3033 (and arg (integerp arg) (> arg 0)
3034 (nth (1- arg) kwds))))
3035 rtn rtnall files file pos)
3036 (when (equal arg '(4))
3037 (setq org-select-this-todo-keyword
3038 (org-ido-completing-read "Keyword (or KWD1|K2D2|...): "
3039 (mapcar 'list kwds) nil nil)))
3040 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3041 (org-set-local 'org-last-arg arg)
3042 (setq org-agenda-redo-command
3043 '(org-todo-list (or current-prefix-arg org-last-arg)))
3044 (setq files (org-agenda-files nil 'ifmode)
3045 rtnall nil)
3046 (while (setq file (pop files))
3047 (catch 'nextfile
3048 (org-check-agenda-file file)
3049 (setq rtn (org-agenda-get-day-entries file date :todo))
3050 (setq rtnall (append rtnall rtn))))
3051 (if org-agenda-overriding-header
3052 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3053 nil 'face 'org-agenda-structure) "\n")
3054 (insert "Global list of TODO items of type: ")
3055 (add-text-properties (point-min) (1- (point))
3056 (list 'face 'org-agenda-structure))
3057 (setq pos (point))
3058 (insert (or org-select-this-todo-keyword "ALL") "\n")
3059 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3060 (setq pos (point))
3061 (unless org-agenda-multi
3062 (insert "Available with `N r': (0)ALL")
3063 (let ((n 0) s)
3064 (mapc (lambda (x)
3065 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3066 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3067 (insert "\n "))
3068 (insert " " s))
3069 kwds))
3070 (insert "\n"))
3071 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3072 (when rtnall
3073 (insert (org-finalize-agenda-entries rtnall) "\n"))
3074 (goto-char (point-min))
3075 (or org-agenda-multi (org-fit-agenda-window))
3076 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3077 (org-finalize-agenda)
3078 (setq buffer-read-only t)))
3080 ;;; Agenda tags match
3082 ;;;###autoload
3083 (defun org-tags-view (&optional todo-only match)
3084 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3085 The prefix arg TODO-ONLY limits the search to TODO entries."
3086 (interactive "P")
3087 (org-compile-prefix-format 'tags)
3088 (org-set-sorting-strategy 'tags)
3089 (let* ((org-tags-match-list-sublevels
3090 (if todo-only t org-tags-match-list-sublevels))
3091 (completion-ignore-case t)
3092 rtn rtnall files file pos matcher
3093 buffer)
3094 (setq matcher (org-make-tags-matcher match)
3095 match (car matcher) matcher (cdr matcher))
3096 (org-prepare-agenda (concat "TAGS " match))
3097 (setq org-agenda-query-string match)
3098 (setq org-agenda-redo-command
3099 (list 'org-tags-view (list 'quote todo-only)
3100 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3101 (setq files (org-agenda-files nil 'ifmode)
3102 rtnall nil)
3103 (while (setq file (pop files))
3104 (catch 'nextfile
3105 (org-check-agenda-file file)
3106 (setq buffer (if (file-exists-p file)
3107 (org-get-agenda-file-buffer file)
3108 (error "No such file %s" file)))
3109 (if (not buffer)
3110 ;; If file does not exist, error message to agenda
3111 (setq rtn (list
3112 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3113 rtnall (append rtnall rtn))
3114 (with-current-buffer buffer
3115 (unless (org-mode-p)
3116 (error "Agenda file %s is not in `org-mode'" file))
3117 (save-excursion
3118 (save-restriction
3119 (if org-agenda-restrict
3120 (narrow-to-region org-agenda-restrict-begin
3121 org-agenda-restrict-end)
3122 (widen))
3123 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3124 (setq rtnall (append rtnall rtn))))))))
3125 (if org-agenda-overriding-header
3126 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3127 nil 'face 'org-agenda-structure) "\n")
3128 (insert "Headlines with TAGS match: ")
3129 (add-text-properties (point-min) (1- (point))
3130 (list 'face 'org-agenda-structure))
3131 (setq pos (point))
3132 (insert match "\n")
3133 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3134 (setq pos (point))
3135 (unless org-agenda-multi
3136 (insert "Press `C-u r' to search again with new search string\n"))
3137 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3138 (when rtnall
3139 (insert (org-finalize-agenda-entries rtnall) "\n"))
3140 (goto-char (point-min))
3141 (or org-agenda-multi (org-fit-agenda-window))
3142 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3143 (org-finalize-agenda)
3144 (setq buffer-read-only t)))
3146 ;;; Agenda Finding stuck projects
3148 (defvar org-agenda-skip-regexp nil
3149 "Regular expression used in skipping subtrees for the agenda.
3150 This is basically a temporary global variable that can be set and then
3151 used by user-defined selections using `org-agenda-skip-function'.")
3153 (defvar org-agenda-overriding-header nil
3154 "When this is set during todo and tags searches, will replace header.
3155 This variable should not be set directly, but custom commands can bind it
3156 in the options section.")
3158 (defun org-agenda-skip-entry-when-regexp-matches ()
3159 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3160 If yes, it returns the end position of this entry, causing agenda commands
3161 to skip the entry but continuing the search in the subtree. This is a
3162 function that can be put into `org-agenda-skip-function' for the duration
3163 of a command."
3164 (let ((end (save-excursion (org-end-of-subtree t)))
3165 skip)
3166 (save-excursion
3167 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3168 (and skip end)))
3170 (defun org-agenda-skip-subtree-when-regexp-matches ()
3171 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3172 If yes, it returns the end position of this tree, causing agenda commands
3173 to skip this subtree. This is a function that can be put into
3174 `org-agenda-skip-function' for the duration of a command."
3175 (let ((end (save-excursion (org-end-of-subtree t)))
3176 skip)
3177 (save-excursion
3178 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3179 (and skip end)))
3181 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3182 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3183 If yes, it returns the end position of the current entry (NOT the tree),
3184 causing agenda commands to skip the entry but continuing the search in
3185 the subtree. This is a function that can be put into
3186 `org-agenda-skip-function' for the duration of a command. An important
3187 use of this function is for the stuck project list."
3188 (let ((end (save-excursion (org-end-of-subtree t)))
3189 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3190 skip)
3191 (save-excursion
3192 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3193 (and skip entry-end)))
3195 (defun org-agenda-skip-entry-if (&rest conditions)
3196 "Skip entry if any of CONDITIONS is true.
3197 See `org-agenda-skip-if' for details."
3198 (org-agenda-skip-if nil conditions))
3200 (defun org-agenda-skip-subtree-if (&rest conditions)
3201 "Skip entry if any of CONDITIONS is true.
3202 See `org-agenda-skip-if' for details."
3203 (org-agenda-skip-if t conditions))
3205 (defun org-agenda-skip-if (subtree conditions)
3206 "Checks current entity for CONDITIONS.
3207 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3208 the entry, i.e. the text before the next heading is checked.
3210 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3211 from different tests. Valid conditions are:
3213 scheduled Check if there is a scheduled cookie
3214 notscheduled Check if there is no scheduled cookie
3215 deadline Check if there is a deadline
3216 notdeadline Check if there is no deadline
3217 timestamp Check if there is a timestamp (also deadline or scheduled)
3218 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3219 regexp Check if regexp matches
3220 notregexp Check if regexp does not match.
3222 The regexp is taken from the conditions list, it must come right after
3223 the `regexp' or `notregexp' element.
3225 If any of these conditions is met, this function returns the end point of
3226 the entity, causing the search to continue from there. This is a function
3227 that can be put into `org-agenda-skip-function' for the duration of a command."
3228 (let (beg end m)
3229 (org-back-to-heading t)
3230 (setq beg (point)
3231 end (if subtree
3232 (progn (org-end-of-subtree t) (point))
3233 (progn (outline-next-heading) (1- (point)))))
3234 (goto-char beg)
3235 (and
3237 (and (memq 'scheduled conditions)
3238 (re-search-forward org-scheduled-time-regexp end t))
3239 (and (memq 'notscheduled conditions)
3240 (not (re-search-forward org-scheduled-time-regexp end t)))
3241 (and (memq 'deadline conditions)
3242 (re-search-forward org-deadline-time-regexp end t))
3243 (and (memq 'notdeadline conditions)
3244 (not (re-search-forward org-deadline-time-regexp end t)))
3245 (and (memq 'timestamp conditions)
3246 (re-search-forward org-ts-regexp end t))
3247 (and (memq 'nottimestamp conditions)
3248 (not (re-search-forward org-ts-regexp end t)))
3249 (and (setq m (memq 'regexp conditions))
3250 (stringp (nth 1 m))
3251 (re-search-forward (nth 1 m) end t))
3252 (and (setq m (memq 'notregexp conditions))
3253 (stringp (nth 1 m))
3254 (not (re-search-forward (nth 1 m) end t))))
3255 end)))
3257 ;;;###autoload
3258 (defun org-agenda-list-stuck-projects (&rest ignore)
3259 "Create agenda view for projects that are stuck.
3260 Stuck projects are project that have no next actions. For the definitions
3261 of what a project is and how to check if it stuck, customize the variable
3262 `org-stuck-projects'.
3263 MATCH is being ignored."
3264 (interactive)
3265 (let* ((org-agenda-skip-function
3266 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3267 ;; We could have used org-agenda-skip-if here.
3268 (org-agenda-overriding-header "List of stuck projects: ")
3269 (matcher (nth 0 org-stuck-projects))
3270 (todo (nth 1 org-stuck-projects))
3271 (todo-wds (if (member "*" todo)
3272 (progn
3273 (org-prepare-agenda-buffers (org-agenda-files
3274 nil 'ifmode))
3275 (org-delete-all
3276 org-done-keywords-for-agenda
3277 (copy-sequence org-todo-keywords-for-agenda)))
3278 todo))
3279 (todo-re (concat "^\\*+[ \t]+\\("
3280 (mapconcat 'identity todo-wds "\\|")
3281 "\\)\\>"))
3282 (tags (nth 2 org-stuck-projects))
3283 (tags-re (if (member "*" tags)
3284 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3285 (if tags
3286 (concat "^\\*+ .*:\\("
3287 (mapconcat 'identity tags "\\|")
3288 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3289 (gen-re (nth 3 org-stuck-projects))
3290 (re-list
3291 (delq nil
3292 (list
3293 (if todo todo-re)
3294 (if tags tags-re)
3295 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3296 gen-re)))))
3297 (setq org-agenda-skip-regexp
3298 (if re-list
3299 (mapconcat 'identity re-list "\\|")
3300 (error "No information how to identify unstuck projects")))
3301 (org-tags-view nil matcher)
3302 (with-current-buffer org-agenda-buffer-name
3303 (setq org-agenda-redo-command
3304 '(org-agenda-list-stuck-projects
3305 (or current-prefix-arg org-last-arg))))))
3307 ;;; Diary integration
3309 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3310 (defvar list-diary-entries-hook)
3312 (defun org-get-entries-from-diary (date)
3313 "Get the (Emacs Calendar) diary entries for DATE."
3314 (require 'diary-lib)
3315 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3316 (fancy-diary-buffer diary-fancy-buffer)
3317 (diary-display-hook '(fancy-diary-display))
3318 (diary-display-function 'fancy-diary-display)
3319 (pop-up-frames nil)
3320 (list-diary-entries-hook
3321 (cons 'org-diary-default-entry list-diary-entries-hook))
3322 (diary-file-name-prefix-function nil) ; turn this feature off
3323 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3324 entries
3325 (org-disable-agenda-to-diary t))
3326 (save-excursion
3327 (save-window-excursion
3328 (funcall (if (fboundp 'diary-list-entries)
3329 'diary-list-entries 'list-diary-entries)
3330 date 1)))
3331 (if (not (get-buffer diary-fancy-buffer))
3332 (setq entries nil)
3333 (with-current-buffer diary-fancy-buffer
3334 (setq buffer-read-only nil)
3335 (if (zerop (buffer-size))
3336 ;; No entries
3337 (setq entries nil)
3338 ;; Omit the date and other unnecessary stuff
3339 (org-agenda-cleanup-fancy-diary)
3340 ;; Add prefix to each line and extend the text properties
3341 (if (zerop (buffer-size))
3342 (setq entries nil)
3343 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3344 (set-buffer-modified-p nil)
3345 (kill-buffer diary-fancy-buffer)))
3346 (when entries
3347 (setq entries (org-split-string entries "\n"))
3348 (setq entries
3349 (mapcar
3350 (lambda (x)
3351 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3352 ;; Extend the text properties to the beginning of the line
3353 (org-add-props x (text-properties-at (1- (length x)) x)
3354 'type "diary" 'date date))
3355 entries)))))
3357 (defun org-agenda-cleanup-fancy-diary ()
3358 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3359 This gets rid of the date, the underline under the date, and
3360 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3361 date. It also removes lines that contain only whitespace."
3362 (goto-char (point-min))
3363 (if (looking-at ".*?:[ \t]*")
3364 (progn
3365 (replace-match "")
3366 (re-search-forward "\n=+$" nil t)
3367 (replace-match "")
3368 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3369 (re-search-forward "\n=+$" nil t)
3370 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3371 (goto-char (point-min))
3372 (while (re-search-forward "^ +\n" nil t)
3373 (replace-match ""))
3374 (goto-char (point-min))
3375 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3376 (replace-match "")))
3378 ;; Make sure entries from the diary have the right text properties.
3379 (eval-after-load "diary-lib"
3380 '(if (boundp 'diary-modify-entry-list-string-function)
3381 ;; We can rely on the hook, nothing to do
3383 ;; Hook not available, must use advice to make this work
3384 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3385 "Make the position visible."
3386 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3387 (stringp string)
3388 buffer-file-name)
3389 (setq string (org-modify-diary-entry-string string))))))
3391 (defun org-modify-diary-entry-string (string)
3392 "Add text properties to string, allowing org-mode to act on it."
3393 (org-add-props string nil
3394 'mouse-face 'highlight
3395 'keymap org-agenda-keymap
3396 'help-echo (if buffer-file-name
3397 (format "mouse-2 or RET jump to diary file %s"
3398 (abbreviate-file-name buffer-file-name))
3400 'org-agenda-diary-link t
3401 'org-marker (org-agenda-new-marker (point-at-bol))))
3403 (defun org-diary-default-entry ()
3404 "Add a dummy entry to the diary.
3405 Needed to avoid empty dates which mess up holiday display."
3406 ;; Catch the error if dealing with the new add-to-diary-alist
3407 (when org-disable-agenda-to-diary
3408 (condition-case nil
3409 (org-add-to-diary-list original-date "Org-mode dummy" "")
3410 (error
3411 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3413 (defun org-add-to-diary-list (&rest args)
3414 (if (fboundp 'diary-add-to-list)
3415 (apply 'diary-add-to-list args)
3416 (apply 'add-to-diary-list args)))
3418 ;;;###autoload
3419 (defun org-diary (&rest args)
3420 "Return diary information from org-files.
3421 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3422 It accesses org files and extracts information from those files to be
3423 listed in the diary. The function accepts arguments specifying what
3424 items should be listed. The following arguments are allowed:
3426 :timestamp List the headlines of items containing a date stamp or
3427 date range matching the selected date. Deadlines will
3428 also be listed, on the expiration day.
3430 :sexp List entries resulting from diary-like sexps.
3432 :deadline List any deadlines past due, or due within
3433 `org-deadline-warning-days'. The listing occurs only
3434 in the diary for *today*, not at any other date. If
3435 an entry is marked DONE, it is no longer listed.
3437 :scheduled List all items which are scheduled for the given date.
3438 The diary for *today* also contains items which were
3439 scheduled earlier and are not yet marked DONE.
3441 :todo List all TODO items from the org-file. This may be a
3442 long list - so this is not turned on by default.
3443 Like deadlines, these entries only show up in the
3444 diary for *today*, not at any other date.
3446 The call in the diary file should look like this:
3448 &%%(org-diary) ~/path/to/some/orgfile.org
3450 Use a separate line for each org file to check. Or, if you omit the file name,
3451 all files listed in `org-agenda-files' will be checked automatically:
3453 &%%(org-diary)
3455 If you don't give any arguments (as in the example above), the default
3456 arguments (:deadline :scheduled :timestamp :sexp) are used.
3457 So the example above may also be written as
3459 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3461 The function expects the lisp variables `entry' and `date' to be provided
3462 by the caller, because this is how the calendar works. Don't use this
3463 function from a program - use `org-agenda-get-day-entries' instead."
3464 (when (> (- (time-to-seconds (current-time))
3465 org-agenda-last-marker-time)
3467 (org-agenda-reset-markers))
3468 (org-compile-prefix-format 'agenda)
3469 (org-set-sorting-strategy 'agenda)
3470 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3471 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3472 (list entry)
3473 (org-agenda-files t)))
3474 file rtn results)
3475 (org-prepare-agenda-buffers files)
3476 ;; If this is called during org-agenda, don't return any entries to
3477 ;; the calendar. Org Agenda will list these entries itself.
3478 (if org-disable-agenda-to-diary (setq files nil))
3479 (while (setq file (pop files))
3480 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3481 (setq results (append results rtn)))
3482 (if results
3483 (concat (org-finalize-agenda-entries results) "\n"))))
3485 ;;; Agenda entry finders
3487 (defun org-agenda-get-day-entries (file date &rest args)
3488 "Does the work for `org-diary' and `org-agenda'.
3489 FILE is the path to a file to be checked for entries. DATE is date like
3490 the one returned by `calendar-current-date'. ARGS are symbols indicating
3491 which kind of entries should be extracted. For details about these, see
3492 the documentation of `org-diary'."
3493 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3494 (let* ((org-startup-folded nil)
3495 (org-startup-align-all-tables nil)
3496 (buffer (if (file-exists-p file)
3497 (org-get-agenda-file-buffer file)
3498 (error "No such file %s" file)))
3499 arg results rtn)
3500 (if (not buffer)
3501 ;; If file does not exist, make sure an error message ends up in diary
3502 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3503 (with-current-buffer buffer
3504 (unless (org-mode-p)
3505 (error "Agenda file %s is not in `org-mode'" file))
3506 (let ((case-fold-search nil))
3507 (save-excursion
3508 (save-restriction
3509 (if org-agenda-restrict
3510 (narrow-to-region org-agenda-restrict-begin
3511 org-agenda-restrict-end)
3512 (widen))
3513 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3514 (while (setq arg (pop args))
3515 (cond
3516 ((and (eq arg :todo)
3517 (equal date (calendar-current-date)))
3518 (setq rtn (org-agenda-get-todos))
3519 (setq results (append results rtn)))
3520 ((eq arg :timestamp)
3521 (setq rtn (org-agenda-get-blocks))
3522 (setq results (append results rtn))
3523 (setq rtn (org-agenda-get-timestamps))
3524 (setq results (append results rtn)))
3525 ((eq arg :sexp)
3526 (setq rtn (org-agenda-get-sexps))
3527 (setq results (append results rtn)))
3528 ((eq arg :scheduled)
3529 (setq rtn (org-agenda-get-scheduled))
3530 (setq results (append results rtn)))
3531 ((eq arg :closed)
3532 (setq rtn (org-agenda-get-progress))
3533 (setq results (append results rtn)))
3534 ((eq arg :deadline)
3535 (setq rtn (org-agenda-get-deadlines))
3536 (setq results (append results rtn))))))))
3537 results))))
3539 (defun org-agenda-get-todos ()
3540 "Return the TODO information for agenda display."
3541 (let* ((props (list 'face nil
3542 'done-face 'org-agenda-done
3543 'org-not-done-regexp org-not-done-regexp
3544 'org-todo-regexp org-todo-regexp
3545 'org-complex-heading-regexp org-complex-heading-regexp
3546 'mouse-face 'highlight
3547 'keymap org-agenda-keymap
3548 'help-echo
3549 (format "mouse-2 or RET jump to org file %s"
3550 (abbreviate-file-name buffer-file-name))))
3551 (regexp (concat "^\\*+[ \t]+\\("
3552 (if org-select-this-todo-keyword
3553 (if (equal org-select-this-todo-keyword "*")
3554 org-todo-regexp
3555 (concat "\\<\\("
3556 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3557 "\\)\\>"))
3558 org-not-done-regexp)
3559 "[^\n\r]*\\)"))
3560 marker priority category tags todo-state
3561 ee txt beg end)
3562 (goto-char (point-min))
3563 (while (re-search-forward regexp nil t)
3564 (catch :skip
3565 (save-match-data
3566 (beginning-of-line)
3567 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3568 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3569 (goto-char (1+ beg))
3570 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3571 (throw :skip nil)))
3572 (goto-char beg)
3573 (org-agenda-skip)
3574 (goto-char (match-beginning 1))
3575 (setq marker (org-agenda-new-marker (match-beginning 0))
3576 category (org-get-category)
3577 txt (match-string 1)
3578 tags (org-get-tags-at (point))
3579 txt (org-format-agenda-item "" txt category tags)
3580 priority (1+ (org-get-priority txt))
3581 todo-state (org-get-todo-state))
3582 (org-add-props txt props
3583 'org-marker marker 'org-hd-marker marker
3584 'priority priority 'org-category category
3585 'type "todo" 'todo-state todo-state)
3586 (push txt ee)
3587 (if org-agenda-todo-list-sublevels
3588 (goto-char (match-end 1))
3589 (org-end-of-subtree 'invisible))))
3590 (nreverse ee)))
3592 ;;;###autoload
3593 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3594 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3595 (when (or org-agenda-todo-ignore-with-date
3596 org-agenda-todo-ignore-scheduled
3597 org-agenda-todo-ignore-deadlines)
3598 (setq end (or end (save-excursion (outline-next-heading) (point))))
3599 (save-excursion
3600 (or (and org-agenda-todo-ignore-with-date
3601 (re-search-forward org-ts-regexp end t))
3602 (and org-agenda-todo-ignore-scheduled
3603 (re-search-forward org-scheduled-time-regexp end t))
3604 (and org-agenda-todo-ignore-deadlines
3605 (re-search-forward org-deadline-time-regexp end t)
3606 (org-deadline-close (match-string 1)))))))
3608 (defconst org-agenda-no-heading-message
3609 "No heading for this item in buffer or region.")
3611 (defun org-agenda-get-timestamps ()
3612 "Return the date stamp information for agenda display."
3613 (let* ((props (list 'face nil
3614 'org-not-done-regexp org-not-done-regexp
3615 'org-todo-regexp org-todo-regexp
3616 'org-complex-heading-regexp org-complex-heading-regexp
3617 'mouse-face 'highlight
3618 'keymap org-agenda-keymap
3619 'help-echo
3620 (format "mouse-2 or RET jump to org file %s"
3621 (abbreviate-file-name buffer-file-name))))
3622 (d1 (calendar-absolute-from-gregorian date))
3623 (remove-re
3624 (concat
3625 (regexp-quote
3626 (format-time-string
3627 "<%Y-%m-%d"
3628 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3629 ".*?>"))
3630 (regexp
3631 (concat
3632 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3633 (regexp-quote
3634 (substring
3635 (format-time-string
3636 (car org-time-stamp-formats)
3637 (apply 'encode-time ; DATE bound by calendar
3638 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3639 1 11))
3640 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3641 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
3642 marker hdmarker deadlinep scheduledp clockp closedp inactivep
3643 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3644 todo-state)
3645 (goto-char (point-min))
3646 (while (re-search-forward regexp nil t)
3647 (setq b0 (match-beginning 0)
3648 b3 (match-beginning 3) e3 (match-end 3))
3649 (catch :skip
3650 (and (org-at-date-range-p) (throw :skip nil))
3651 (org-agenda-skip)
3652 (if (and (match-end 1)
3653 (not (= d1 (org-time-string-to-absolute
3654 (match-string 1) d1 nil
3655 org-agenda-repeating-timestamp-show-all))))
3656 (throw :skip nil))
3657 (if (and e3
3658 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
3659 (throw :skip nil))
3660 (setq marker (org-agenda-new-marker b0)
3661 category (org-get-category b0)
3662 tmp (buffer-substring (max (point-min)
3663 (- b0 org-ds-keyword-length))
3665 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
3666 inactivep (= (char-after b0) ?\[)
3667 deadlinep (string-match org-deadline-regexp tmp)
3668 scheduledp (string-match org-scheduled-regexp tmp)
3669 closedp (and org-agenda-include-inactive-timestamps
3670 (string-match org-closed-string tmp))
3671 clockp (and org-agenda-include-inactive-timestamps
3672 (or (string-match org-clock-string tmp)
3673 (string-match "]-+\\'" tmp)))
3674 todo-state (org-get-todo-state)
3675 donep (member todo-state org-done-keywords))
3676 (if (or scheduledp deadlinep closedp clockp)
3677 (throw :skip t))
3678 (if (string-match ">" timestr)
3679 ;; substring should only run to end of time stamp
3680 (setq timestr (substring timestr 0 (match-end 0))))
3681 (save-excursion
3682 (if (re-search-backward "^\\*+ " nil t)
3683 (progn
3684 (goto-char (match-beginning 0))
3685 (setq hdmarker (org-agenda-new-marker)
3686 tags (org-get-tags-at))
3687 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3688 (setq head (match-string 1))
3689 (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
3690 (setq txt (org-format-agenda-item
3691 (if inactivep "[" nil)
3692 head category tags timestr nil
3693 remove-re)))
3694 (setq txt org-agenda-no-heading-message))
3695 (setq priority (org-get-priority txt))
3696 (org-add-props txt props
3697 'org-marker marker 'org-hd-marker hdmarker)
3698 (org-add-props txt nil 'priority priority
3699 'org-category category 'date date
3700 'todo-state todo-state
3701 'type "timestamp")
3702 (push txt ee))
3703 (outline-next-heading)))
3704 (nreverse ee)))
3706 (defun org-agenda-get-sexps ()
3707 "Return the sexp information for agenda display."
3708 (require 'diary-lib)
3709 (let* ((props (list 'face nil
3710 'mouse-face 'highlight
3711 'keymap org-agenda-keymap
3712 'help-echo
3713 (format "mouse-2 or RET jump to org file %s"
3714 (abbreviate-file-name buffer-file-name))))
3715 (regexp "^&?%%(")
3716 marker category ee txt tags entry result beg b sexp sexp-entry
3717 todo-state)
3718 (goto-char (point-min))
3719 (while (re-search-forward regexp nil t)
3720 (catch :skip
3721 (org-agenda-skip)
3722 (setq beg (match-beginning 0))
3723 (goto-char (1- (match-end 0)))
3724 (setq b (point))
3725 (forward-sexp 1)
3726 (setq sexp (buffer-substring b (point)))
3727 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
3728 (org-trim (match-string 1))
3729 ""))
3730 (setq result (org-diary-sexp-entry sexp sexp-entry date))
3731 (when result
3732 (setq marker (org-agenda-new-marker beg)
3733 category (org-get-category beg)
3734 todo-state (org-get-todo-state))
3736 (if (string-match "\\S-" result)
3737 (setq txt result)
3738 (setq txt "SEXP entry returned empty string"))
3740 (setq txt (org-format-agenda-item
3741 "" txt category tags 'time))
3742 (org-add-props txt props 'org-marker marker)
3743 (org-add-props txt nil
3744 'org-category category 'date date 'todo-state todo-state
3745 'type "sexp")
3746 (push txt ee))))
3747 (nreverse ee)))
3749 (defalias 'org-get-closed 'org-agenda-get-progress)
3750 (defun org-agenda-get-progress ()
3751 "Return the logged TODO entries for agenda display."
3752 (let* ((props (list 'mouse-face 'highlight
3753 'org-not-done-regexp org-not-done-regexp
3754 'org-todo-regexp org-todo-regexp
3755 'org-complex-heading-regexp org-complex-heading-regexp
3756 'keymap org-agenda-keymap
3757 'help-echo
3758 (format "mouse-2 or RET jump to org file %s"
3759 (abbreviate-file-name buffer-file-name))))
3760 (items (if (consp org-agenda-show-log)
3761 org-agenda-show-log
3762 org-agenda-log-mode-items))
3763 (parts
3764 (delq nil
3765 (list
3766 (if (memq 'closed items) (concat "\\<" org-closed-string))
3767 (if (memq 'clock items) (concat "\\<" org-clock-string))
3768 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
3769 (parts-re (if parts (mapconcat 'identity parts "\\|")
3770 (error "`org-agenda-log-mode-items' is empty")))
3771 (regexp (concat
3772 "\\(" parts-re "\\)"
3773 " *\\["
3774 (regexp-quote
3775 (substring
3776 (format-time-string
3777 (car org-time-stamp-formats)
3778 (apply 'encode-time ; DATE bound by calendar
3779 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3780 1 11))))
3781 marker hdmarker priority category tags closedp statep state
3782 ee txt timestr rest clocked)
3783 (goto-char (point-min))
3784 (while (re-search-forward regexp nil t)
3785 (catch :skip
3786 (org-agenda-skip)
3787 (setq marker (org-agenda-new-marker (match-beginning 0))
3788 closedp (equal (match-string 1) org-closed-string)
3789 statep (equal (string-to-char (match-string 1)) ?-)
3790 state (and statep (match-string 2))
3791 category (org-get-category (match-beginning 0))
3792 timestr (buffer-substring (match-beginning 0) (point-at-eol))
3793 ;; donep (org-entry-is-done-p)
3795 (when (string-match "\\]" timestr)
3796 ;; substring should only run to end of time stamp
3797 (setq rest (substring timestr (match-end 0))
3798 timestr (substring timestr 0 (match-end 0)))
3799 (if (and (not closedp) (not statep)
3800 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
3801 (progn (setq timestr (concat (substring timestr 0 -1)
3802 "-" (match-string 1 rest) "]"))
3803 (setq clocked (match-string 2 rest)))
3804 (setq clocked "-")))
3805 (save-excursion
3806 (if (re-search-backward "^\\*+ " nil t)
3807 (progn
3808 (goto-char (match-beginning 0))
3809 (setq hdmarker (org-agenda-new-marker)
3810 tags (org-get-tags-at))
3811 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3812 (setq txt (org-format-agenda-item
3813 (cond
3814 (closedp "Closed: ")
3815 (statep (concat "State: (" state ")"))
3816 (t (concat "Clocked: (" clocked ")")))
3817 (match-string 1) category tags timestr)))
3818 (setq txt org-agenda-no-heading-message))
3819 (setq priority 100000)
3820 (org-add-props txt props
3821 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
3822 'priority priority 'org-category category
3823 'type "closed" 'date date
3824 'undone-face 'org-warning 'done-face 'org-agenda-done)
3825 (push txt ee))
3826 (goto-char (point-at-eol))))
3827 (nreverse ee)))
3829 (defun org-agenda-get-deadlines ()
3830 "Return the deadline information for agenda display."
3831 (let* ((props (list 'mouse-face 'highlight
3832 'org-not-done-regexp org-not-done-regexp
3833 'org-todo-regexp org-todo-regexp
3834 'org-complex-heading-regexp org-complex-heading-regexp
3835 'keymap org-agenda-keymap
3836 'help-echo
3837 (format "mouse-2 or RET jump to org file %s"
3838 (abbreviate-file-name buffer-file-name))))
3839 (regexp org-deadline-time-regexp)
3840 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3841 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3842 d2 diff dfrac wdays pos pos1 category tags
3843 ee txt head face s todo-state upcomingp donep timestr)
3844 (goto-char (point-min))
3845 (while (re-search-forward regexp nil t)
3846 (catch :skip
3847 (org-agenda-skip)
3848 (setq s (match-string 1)
3849 pos (1- (match-beginning 1))
3850 d2 (org-time-string-to-absolute
3851 (match-string 1) d1 'past
3852 org-agenda-repeating-timestamp-show-all)
3853 diff (- d2 d1)
3854 wdays (org-get-wdays s)
3855 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
3856 upcomingp (and todayp (> diff 0)))
3857 ;; When to show a deadline in the calendar:
3858 ;; If the expiration is within wdays warning time.
3859 ;; Past-due deadlines are only shown on the current date
3860 (if (or (and (<= diff wdays)
3861 (and todayp (not org-agenda-only-exact-dates)))
3862 (= diff 0))
3863 (save-excursion
3864 (setq category (org-get-category))
3865 (setq todo-state (org-get-todo-state))
3866 (if (re-search-backward "^\\*+[ \t]+" nil t)
3867 (progn
3868 (goto-char (match-end 0))
3869 (setq pos1 (match-beginning 0))
3870 (setq tags (org-get-tags-at pos1))
3871 (setq head (buffer-substring-no-properties
3872 (point)
3873 (progn (skip-chars-forward "^\r\n")
3874 (point))))
3875 (setq donep (member todo-state org-done-keywords))
3876 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3877 (setq timestr
3878 (concat (substring s (match-beginning 1)) " "))
3879 (setq timestr 'time))
3880 (if (and donep
3881 (or org-agenda-skip-deadline-if-done
3882 (not (= diff 0))))
3883 (setq txt nil)
3884 (setq txt (org-format-agenda-item
3885 (if (= diff 0)
3886 (car org-agenda-deadline-leaders)
3887 (if (functionp (nth 1 org-agenda-deadline-leaders))
3888 (funcall (nth 1 org-agenda-deadline-leaders) diff date)
3889 (format (nth 1 org-agenda-deadline-leaders)
3890 diff)))
3891 head category tags
3892 (if (not (= diff 0)) nil timestr)))))
3893 (setq txt org-agenda-no-heading-message))
3894 (when txt
3895 (setq face (org-agenda-deadline-face dfrac wdays))
3896 (org-add-props txt props
3897 'org-marker (org-agenda-new-marker pos)
3898 'org-hd-marker (org-agenda-new-marker pos1)
3899 'priority (+ (- diff)
3900 (org-get-priority txt))
3901 'org-category category
3902 'todo-state todo-state
3903 'type (if upcomingp "upcoming-deadline" "deadline")
3904 'date (if upcomingp date d2)
3905 'face (if donep 'org-agenda-done face)
3906 'undone-face face 'done-face 'org-agenda-done)
3907 (push txt ee))))))
3908 (nreverse ee)))
3910 (defun org-agenda-deadline-face (fraction &optional wdays)
3911 "Return the face to displaying a deadline item.
3912 FRACTION is what fraction of the head-warning time has passed."
3913 (if (equal wdays 0) (setq fraction 1.))
3914 (let ((faces org-agenda-deadline-faces) f)
3915 (catch 'exit
3916 (while (setq f (pop faces))
3917 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
3919 (defun org-agenda-get-scheduled ()
3920 "Return the scheduled information for agenda display."
3921 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
3922 'org-todo-regexp org-todo-regexp
3923 'org-complex-heading-regexp org-complex-heading-regexp
3924 'done-face 'org-agenda-done
3925 'mouse-face 'highlight
3926 'keymap org-agenda-keymap
3927 'help-echo
3928 (format "mouse-2 or RET jump to org file %s"
3929 (abbreviate-file-name buffer-file-name))))
3930 (regexp org-scheduled-time-regexp)
3931 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3932 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3933 d2 diff pos pos1 category tags donep
3934 ee txt head pastschedp todo-state face timestr s)
3935 (goto-char (point-min))
3936 (while (re-search-forward regexp nil t)
3937 (catch :skip
3938 (org-agenda-skip)
3939 (setq s (match-string 1)
3940 pos (1- (match-beginning 1))
3941 d2 (org-time-string-to-absolute
3942 (match-string 1) d1 'past
3943 org-agenda-repeating-timestamp-show-all)
3944 diff (- d2 d1))
3945 (setq pastschedp (and todayp (< diff 0)))
3946 ;; When to show a scheduled item in the calendar:
3947 ;; If it is on or past the date.
3948 (if (or (and (< diff 0)
3949 (< (abs diff) org-scheduled-past-days)
3950 (and todayp (not org-agenda-only-exact-dates)))
3951 (= diff 0))
3952 (save-excursion
3953 (setq category (org-get-category))
3954 (setq todo-state (org-get-todo-state))
3955 (if (re-search-backward "^\\*+[ \t]+" nil t)
3956 (progn
3957 (goto-char (match-end 0))
3958 (setq pos1 (match-beginning 0))
3959 (setq tags (org-get-tags-at))
3960 (setq head (buffer-substring-no-properties
3961 (point)
3962 (progn (skip-chars-forward "^\r\n") (point))))
3963 (setq donep (member todo-state org-done-keywords))
3964 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3965 (setq timestr
3966 (concat (substring s (match-beginning 1)) " "))
3967 (setq timestr 'time))
3968 (if (and donep
3969 (or org-agenda-skip-scheduled-if-done
3970 (not (= diff 0))))
3971 (setq txt nil)
3972 (setq txt (org-format-agenda-item
3973 (if (= diff 0)
3974 (car org-agenda-scheduled-leaders)
3975 (format (nth 1 org-agenda-scheduled-leaders)
3976 (- 1 diff)))
3977 head category tags
3978 (if (not (= diff 0)) nil timestr)))))
3979 (setq txt org-agenda-no-heading-message))
3980 (when txt
3981 (setq face
3982 (cond
3983 (pastschedp 'org-scheduled-previously)
3984 (todayp 'org-scheduled-today)
3985 (t 'org-scheduled)))
3986 (org-add-props txt props
3987 'undone-face face
3988 'face (if donep 'org-agenda-done face)
3989 'org-marker (org-agenda-new-marker pos)
3990 'org-hd-marker (org-agenda-new-marker pos1)
3991 'type (if pastschedp "past-scheduled" "scheduled")
3992 'date (if pastschedp d2 date)
3993 'priority (+ 94 (- 5 diff) (org-get-priority txt))
3994 'org-category category
3995 'todo-state todo-state)
3996 (push txt ee))))))
3997 (nreverse ee)))
3999 (defun org-agenda-get-blocks ()
4000 "Return the date-range information for agenda display."
4001 (let* ((props (list 'face nil
4002 'org-not-done-regexp org-not-done-regexp
4003 'org-todo-regexp org-todo-regexp
4004 'org-complex-heading-regexp org-complex-heading-regexp
4005 'mouse-face 'highlight
4006 'keymap org-agenda-keymap
4007 'help-echo
4008 (format "mouse-2 or RET jump to org file %s"
4009 (abbreviate-file-name buffer-file-name))))
4010 (regexp org-tr-regexp)
4011 (d0 (calendar-absolute-from-gregorian date))
4012 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4013 head)
4014 (goto-char (point-min))
4015 (while (re-search-forward regexp nil t)
4016 (catch :skip
4017 (org-agenda-skip)
4018 (setq pos (point))
4019 (setq timestr (match-string 0)
4020 s1 (match-string 1)
4021 s2 (match-string 2)
4022 d1 (time-to-days (org-time-string-to-time s1))
4023 d2 (time-to-days (org-time-string-to-time s2)))
4024 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4025 ;; Only allow days between the limits, because the normal
4026 ;; date stamps will catch the limits.
4027 (save-excursion
4028 (setq marker (org-agenda-new-marker (point)))
4029 (setq category (org-get-category))
4030 (setq todo-state (org-get-todo-state))
4031 (if (re-search-backward "^\\*+ " nil t)
4032 (progn
4033 (goto-char (match-beginning 0))
4034 (setq hdmarker (org-agenda-new-marker (point)))
4035 (setq tags (org-get-tags-at))
4036 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4037 (setq head (match-string 1))
4038 (and org-agenda-skip-timestamp-if-done
4039 (org-entry-is-done-p)
4040 (throw :skip t))
4041 (setq txt (org-format-agenda-item
4042 (format
4043 (nth (if (= d1 d2) 0 1)
4044 org-agenda-timerange-leaders)
4045 (1+ (- d0 d1)) (1+ (- d2 d1)))
4046 head category tags
4047 (if (= d0 d1) timestr))))
4048 (setq txt org-agenda-no-heading-message))
4049 (org-add-props txt props
4050 'org-marker marker 'org-hd-marker hdmarker
4051 'type "block" 'date date
4052 'todo-state todo-state
4053 'priority (org-get-priority txt) 'org-category category)
4054 (push txt ee)))
4055 (goto-char pos)))
4056 ;; Sort the entries by expiration date.
4057 (nreverse ee)))
4059 ;;; Agenda presentation and sorting
4061 (defvar org-prefix-has-time nil
4062 "A flag, set by `org-compile-prefix-format'.
4063 The flag is set if the currently compiled format contains a `%t'.")
4064 (defvar org-prefix-has-tag nil
4065 "A flag, set by `org-compile-prefix-format'.
4066 The flag is set if the currently compiled format contains a `%T'.")
4067 (defvar org-prefix-has-effort nil
4068 "A flag, set by `org-compile-prefix-format'.
4069 The flag is set if the currently compiled format contains a `%e'.")
4071 (defun org-format-agenda-item (extra txt &optional category tags dotime
4072 noprefix remove-re)
4073 "Format TXT to be inserted into the agenda buffer.
4074 In particular, it adds the prefix and corresponding text properties. EXTRA
4075 must be a string and replaces the `%s' specifier in the prefix format.
4076 CATEGORY (string, symbol or nil) may be used to overrule the default
4077 category taken from local variable or file name. It will replace the `%c'
4078 specifier in the format. DOTIME, when non-nil, indicates that a
4079 time-of-day should be extracted from TXT for sorting of this entry, and for
4080 the `%t' specifier in the format. When DOTIME is a string, this string is
4081 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4082 only the correctly processes TXT should be returned - this is used by
4083 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4084 Any match of REMOVE-RE will be removed from TXT."
4085 (save-match-data
4086 ;; Diary entries sometimes have extra whitespace at the beginning
4087 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4088 (when org-agenda-show-inherited-tags
4089 ;; Fix the tags part in txt
4090 (setq txt (org-agenda-add-inherited-tags txt tags)))
4091 (let* ((category (or category
4092 org-category
4093 (if buffer-file-name
4094 (file-name-sans-extension
4095 (file-name-nondirectory buffer-file-name))
4096 "")))
4097 ;; time, tag, effort are needed for the eval of the prefix format
4098 (tag (if tags (nth (1- (length tags)) tags) ""))
4099 time effort neffort
4100 (ts (if dotime (concat
4101 (if (stringp dotime) dotime "")
4102 (and org-agenda-search-headline-for-time txt))))
4103 (time-of-day (and dotime (org-get-time-of-day ts)))
4104 stamp plain s0 s1 s2 t1 t2 rtn srp
4105 duration)
4106 (and (org-mode-p) buffer-file-name
4107 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4108 (when (and dotime time-of-day)
4109 ;; Extract starting and ending time and move them to prefix
4110 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4111 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4112 (setq s0 (match-string 0 ts)
4113 srp (and stamp (match-end 3))
4114 s1 (match-string (if plain 1 2) ts)
4115 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4117 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4118 ;; them, we might want to remove them there to avoid duplication.
4119 ;; The user can turn this off with a variable.
4120 (if (and org-prefix-has-time
4121 org-agenda-remove-times-when-in-prefix (or stamp plain)
4122 (string-match (concat (regexp-quote s0) " *") txt)
4123 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4124 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4125 (= (match-beginning 0) 0)
4127 (setq txt (replace-match "" nil nil txt))))
4128 ;; Normalize the time(s) to 24 hour
4129 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4130 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4131 ;; Compute the duration
4132 (when s1
4133 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4134 (string-to-number (substring s1 3)))
4135 t2 (cond
4136 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4137 (string-to-number (substring s2 3))))
4138 (org-agenda-default-appointment-duration
4139 (+ t1 org-agenda-default-appointment-duration))
4140 (t nil)))
4141 (setq duration (if t2 (- t2 t1)))))
4143 (when (and s1 (not s2) org-agenda-default-appointment-duration
4144 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4145 (let ((m (+ (string-to-number (match-string 2 s1))
4146 (* 60 (string-to-number (match-string 1 s1)))
4147 org-agenda-default-appointment-duration))
4149 (setq h (/ m 60) m (- m (* h 60)))
4150 (setq s2 (format "%02d:%02d" h m))))
4152 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4153 txt)
4154 ;; Tags are in the string
4155 (if (or (eq org-agenda-remove-tags t)
4156 (and org-agenda-remove-tags
4157 org-prefix-has-tag))
4158 (setq txt (replace-match "" t t txt))
4159 (setq txt (replace-match
4160 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4161 (match-string 2 txt))
4162 t t txt))))
4163 (when (org-mode-p)
4164 (setq effort
4165 (condition-case nil
4166 (org-get-effort
4167 (or (get-text-property 0 'org-hd-marker txt)
4168 (get-text-property 0 'org-marker txt)))
4169 (error nil)))
4170 (when effort
4171 (setq neffort (org-hh:mm-string-to-minutes effort)
4172 effort (setq effort (concat "[" effort"]" )))))
4174 (when remove-re
4175 (while (string-match remove-re txt)
4176 (setq txt (replace-match "" t t txt))))
4178 ;; Create the final string
4179 (if noprefix
4180 (setq rtn txt)
4181 ;; Prepare the variables needed in the eval of the compiled format
4182 (setq time (cond (s2 (concat s1 "-" s2))
4183 (s1 (concat s1 "......"))
4184 (t ""))
4185 extra (or extra "")
4186 category (if (symbolp category) (symbol-name category) category))
4187 ;; Evaluate the compiled format
4188 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4190 ;; And finally add the text properties
4191 (org-add-props rtn nil
4192 'org-category (downcase category)
4193 'tags (mapcar 'org-downcase-keep-props tags)
4194 'org-highest-priority org-highest-priority
4195 'org-lowest-priority org-lowest-priority
4196 'prefix-length (- (length rtn) (length txt))
4197 'time-of-day time-of-day
4198 'duration duration
4199 'effort effort
4200 'effort-minutes neffort
4201 'txt txt
4202 'time time
4203 'extra extra
4204 'dotime dotime))))
4206 (defun org-agenda-add-inherited-tags (txt tags)
4207 "Remove tags string from TXT, and add complete list of tags.
4208 The new list includes inherited tags. If any inherited tags are present,
4209 a double colon separates inherited tags from local tags."
4210 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4211 (setq txt (substring txt 0 (match-beginning 0))))
4212 (when tags
4213 (let ((have-i (get-text-property 0 'inherited (car tags)))
4215 (setq txt (concat txt " :"
4216 (mapconcat
4217 (lambda (x)
4218 (setq i (get-text-property 0 'inherited x))
4219 (if (and have-i (not i))
4220 (progn
4221 (setq have-i nil)
4222 (concat ":" x))
4224 tags ":")
4225 (if have-i "::" ":")))))
4226 txt)
4228 (defun org-downcase-keep-props (s)
4229 (let ((props (text-properties-at 0 s)))
4230 (setq s (downcase s))
4231 (add-text-properties 0 (length s) props s)
4234 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4235 (defvar org-agenda-sorting-strategy-selected nil)
4237 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4238 (catch 'exit
4239 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4240 ((and todayp (member 'today (car org-agenda-time-grid))))
4241 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4242 ((member 'weekly (car org-agenda-time-grid)))
4243 (t (throw 'exit list)))
4244 (let* ((have (delq nil (mapcar
4245 (lambda (x) (get-text-property 1 'time-of-day x))
4246 list)))
4247 (string (nth 1 org-agenda-time-grid))
4248 (gridtimes (nth 2 org-agenda-time-grid))
4249 (req (car org-agenda-time-grid))
4250 (remove (member 'remove-match req))
4251 new time)
4252 (if (and (member 'require-timed req) (not have))
4253 ;; don't show empty grid
4254 (throw 'exit list))
4255 (while (setq time (pop gridtimes))
4256 (unless (and remove (member time have))
4257 (setq time (int-to-string time))
4258 (push (org-format-agenda-item
4259 nil string "" nil
4260 (concat (substring time 0 -2) ":" (substring time -2)))
4261 new)
4262 (put-text-property
4263 1 (length (car new)) 'face 'org-time-grid (car new))))
4264 (if (member 'time-up org-agenda-sorting-strategy-selected)
4265 (append new list)
4266 (append list new)))))
4268 (defun org-compile-prefix-format (key)
4269 "Compile the prefix format into a Lisp form that can be evaluated.
4270 The resulting form is returned and stored in the variable
4271 `org-prefix-format-compiled'."
4272 (setq org-prefix-has-time nil org-prefix-has-tag nil
4273 org-prefix-has-effort nil)
4274 (let ((s (cond
4275 ((stringp org-agenda-prefix-format)
4276 org-agenda-prefix-format)
4277 ((assq key org-agenda-prefix-format)
4278 (cdr (assq key org-agenda-prefix-format)))
4279 (t " %-12:c%?-12t% s")))
4280 (start 0)
4281 varform vars var e c f opt)
4282 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4283 s start)
4284 (setq var (cdr (assoc (match-string 4 s)
4285 '(("c" . category) ("t" . time) ("s" . extra)
4286 ("T" . tag) ("e" . effort))))
4287 c (or (match-string 3 s) "")
4288 opt (match-beginning 1)
4289 start (1+ (match-beginning 0)))
4290 (if (equal var 'time) (setq org-prefix-has-time t))
4291 (if (equal var 'tag) (setq org-prefix-has-tag t))
4292 (if (equal var 'effort) (setq org-prefix-has-effort t))
4293 (setq f (concat "%" (match-string 2 s) "s"))
4294 (if opt
4295 (setq varform
4296 `(if (equal "" ,var)
4298 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4299 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
4300 (setq s (replace-match "%s" t nil s))
4301 (push varform vars))
4302 (setq vars (nreverse vars))
4303 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4305 (defun org-set-sorting-strategy (key)
4306 (if (symbolp (car org-agenda-sorting-strategy))
4307 ;; the old format
4308 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4309 (setq org-agenda-sorting-strategy-selected
4310 (or (cdr (assq key org-agenda-sorting-strategy))
4311 (cdr (assq 'agenda org-agenda-sorting-strategy))
4312 '(time-up category-keep priority-down)))))
4314 (defun org-get-time-of-day (s &optional string mod24)
4315 "Check string S for a time of day.
4316 If found, return it as a military time number between 0 and 2400.
4317 If not found, return nil.
4318 The optional STRING argument forces conversion into a 5 character wide string
4319 HH:MM."
4320 (save-match-data
4321 (when
4322 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4323 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4324 (let* ((h (string-to-number (match-string 1 s)))
4325 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4326 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4327 (am-p (equal ampm "am"))
4328 (h1 (cond ((not ampm) h)
4329 ((= h 12) (if am-p 0 12))
4330 (t (+ h (if am-p 0 12)))))
4331 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4332 (mod h1 24) h1))
4333 (t0 (+ (* 100 h2) m))
4334 (t1 (concat (if (>= h1 24) "+" " ")
4335 (if (< t0 100) "0" "")
4336 (if (< t0 10) "0" "")
4337 (int-to-string t0))))
4338 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4340 (defun org-finalize-agenda-entries (list &optional nosort)
4341 "Sort and concatenate the agenda items."
4342 (setq list (mapcar 'org-agenda-highlight-todo list))
4343 (if nosort
4344 list
4345 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4347 (defun org-agenda-highlight-todo (x)
4348 (let ((org-done-keywords org-done-keywords-for-agenda)
4349 re pl)
4350 (if (eq x 'line)
4351 (save-excursion
4352 (beginning-of-line 1)
4353 (setq re (get-text-property (point) 'org-todo-regexp))
4354 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
4355 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4356 (add-text-properties (match-beginning 0) (match-end 0)
4357 (list 'face (org-get-todo-face 1)))
4358 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4359 (delete-region (match-beginning 1) (1- (match-end 0)))
4360 (goto-char (match-beginning 1))
4361 (insert (format org-agenda-todo-keyword-format s)))))
4362 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4363 pl (get-text-property 0 'prefix-length x))
4364 (when (and re
4365 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4366 x (or pl 0)) pl))
4367 (add-text-properties
4368 (or (match-end 1) (match-end 0)) (match-end 0)
4369 (list 'face (org-get-todo-face (match-string 2 x)))
4371 (setq x (concat (substring x 0 (match-end 1))
4372 (format org-agenda-todo-keyword-format
4373 (match-string 2 x))
4375 (substring x (match-end 3)))))
4376 x)))
4378 (defsubst org-cmp-priority (a b)
4379 "Compare the priorities of string A and B."
4380 (let ((pa (or (get-text-property 1 'priority a) 0))
4381 (pb (or (get-text-property 1 'priority b) 0)))
4382 (cond ((> pa pb) +1)
4383 ((< pa pb) -1)
4384 (t nil))))
4386 (defsubst org-cmp-effort (a b)
4387 "Compare the priorities of string A and B."
4388 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4389 (ea (or (get-text-property 1 'effort-minutes a) def))
4390 (eb (or (get-text-property 1 'effort-minutes b) def)))
4391 (cond ((> ea eb) +1)
4392 ((< ea eb) -1)
4393 (t nil))))
4395 (defsubst org-cmp-category (a b)
4396 "Compare the string values of categories of strings A and B."
4397 (let ((ca (or (get-text-property 1 'org-category a) ""))
4398 (cb (or (get-text-property 1 'org-category b) "")))
4399 (cond ((string-lessp ca cb) -1)
4400 ((string-lessp cb ca) +1)
4401 (t nil))))
4403 (defsubst org-cmp-todo-state (a b)
4404 "Compare the todo states of strings A and B."
4405 (let* ((ma (or (get-text-property 1 'org-marker a)
4406 (get-text-property 1 'org-hd-marker a)))
4407 (mb (or (get-text-property 1 'org-marker b)
4408 (get-text-property 1 'org-hd-marker b)))
4409 (fa (and ma (marker-buffer ma)))
4410 (fb (and mb (marker-buffer mb)))
4411 (todo-kwds
4412 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4413 (and fb (with-current-buffer fb org-todo-keywords-1))))
4414 (ta (or (get-text-property 1 'todo-state a) ""))
4415 (tb (or (get-text-property 1 'todo-state b) ""))
4416 (la (- (length (member ta todo-kwds))))
4417 (lb (- (length (member tb todo-kwds))))
4418 (donepa (member ta org-done-keywords-for-agenda))
4419 (donepb (member tb org-done-keywords-for-agenda)))
4420 (cond ((and donepa (not donepb)) -1)
4421 ((and (not donepa) donepb) +1)
4422 ((< la lb) -1)
4423 ((< lb la) +1)
4424 (t nil))))
4426 (defsubst org-cmp-tag (a b)
4427 "Compare the string values of the first tags of A and B."
4428 (let ((ta (car (last (get-text-property 1 'tags a))))
4429 (tb (car (last (get-text-property 1 'tags b)))))
4430 (cond ((not ta) +1)
4431 ((not tb) -1)
4432 ((string-lessp ta tb) -1)
4433 ((string-lessp tb ta) +1)
4434 (t nil))))
4436 (defsubst org-cmp-time (a b)
4437 "Compare the time-of-day values of strings A and B."
4438 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4439 (ta (or (get-text-property 1 'time-of-day a) def))
4440 (tb (or (get-text-property 1 'time-of-day b) def)))
4441 (cond ((< ta tb) -1)
4442 ((< tb ta) +1)
4443 (t nil))))
4445 (defun org-entries-lessp (a b)
4446 "Predicate for sorting agenda entries."
4447 ;; The following variables will be used when the form is evaluated.
4448 ;; So even though the compiler complains, keep them.
4449 (let* ((time-up (org-cmp-time a b))
4450 (time-down (if time-up (- time-up) nil))
4451 (priority-up (org-cmp-priority a b))
4452 (priority-down (if priority-up (- priority-up) nil))
4453 (effort-up (org-cmp-effort a b))
4454 (effort-down (if effort-up (- effort-up) nil))
4455 (category-up (org-cmp-category a b))
4456 (category-down (if category-up (- category-up) nil))
4457 (category-keep (if category-up +1 nil))
4458 (tag-up (org-cmp-tag a b))
4459 (tag-down (if tag-up (- tag-up) nil))
4460 (todo-state-up (org-cmp-todo-state a b))
4461 (todo-state-down (if todo-state-up (- todo-state-up) nil)))
4462 (cdr (assoc
4463 (eval (cons 'or org-agenda-sorting-strategy-selected))
4464 '((-1 . t) (1 . nil) (nil . nil))))))
4466 ;;; Agenda restriction lock
4468 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4469 "Overlay to mark the headline to which arenda commands are restricted.")
4470 (org-overlay-put org-agenda-restriction-lock-overlay
4471 'face 'org-agenda-restriction-lock)
4472 (org-overlay-put org-agenda-restriction-lock-overlay
4473 'help-echo "Agendas are currently limited to this subtree.")
4474 (org-detach-overlay org-agenda-restriction-lock-overlay)
4476 (defun org-agenda-set-restriction-lock (&optional type)
4477 "Set restriction lock for agenda, to current subtree or file.
4478 Restriction will be the file if TYPE is `file', or if type is the
4479 universal prefix '(4), or if the cursor is before the first headline
4480 in the file. Otherwise, restriction will be to the current subtree."
4481 (interactive "P")
4482 (and (equal type '(4)) (setq type 'file))
4483 (setq type (cond
4484 (type type)
4485 ((org-at-heading-p) 'subtree)
4486 ((condition-case nil (org-back-to-heading t) (error nil))
4487 'subtree)
4488 (t 'file)))
4489 (if (eq type 'subtree)
4490 (progn
4491 (setq org-agenda-restrict t)
4492 (setq org-agenda-overriding-restriction 'subtree)
4493 (put 'org-agenda-files 'org-restrict
4494 (list (buffer-file-name (buffer-base-buffer))))
4495 (org-back-to-heading t)
4496 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4497 (move-marker org-agenda-restrict-begin (point))
4498 (move-marker org-agenda-restrict-end
4499 (save-excursion (org-end-of-subtree t)))
4500 (message "Locking agenda restriction to subtree"))
4501 (put 'org-agenda-files 'org-restrict
4502 (list (buffer-file-name (buffer-base-buffer))))
4503 (setq org-agenda-restrict nil)
4504 (setq org-agenda-overriding-restriction 'file)
4505 (move-marker org-agenda-restrict-begin nil)
4506 (move-marker org-agenda-restrict-end nil)
4507 (message "Locking agenda restriction to file"))
4508 (setq current-prefix-arg nil)
4509 (org-agenda-maybe-redo))
4511 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4512 "Remove the agenda restriction lock."
4513 (interactive "P")
4514 (org-detach-overlay org-agenda-restriction-lock-overlay)
4515 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4516 (setq org-agenda-overriding-restriction nil)
4517 (setq org-agenda-restrict nil)
4518 (put 'org-agenda-files 'org-restrict nil)
4519 (move-marker org-agenda-restrict-begin nil)
4520 (move-marker org-agenda-restrict-end nil)
4521 (setq current-prefix-arg nil)
4522 (message "Agenda restriction lock removed")
4523 (or noupdate (org-agenda-maybe-redo)))
4525 (defun org-agenda-maybe-redo ()
4526 "If there is any window showing the agenda view, update it."
4527 (let ((w (get-buffer-window org-agenda-buffer-name t))
4528 (w0 (selected-window)))
4529 (when w
4530 (select-window w)
4531 (org-agenda-redo)
4532 (select-window w0)
4533 (if org-agenda-overriding-restriction
4534 (message "Agenda view shifted to new %s restriction"
4535 org-agenda-overriding-restriction)
4536 (message "Agenda restriction lock removed")))))
4538 ;;; Agenda commands
4540 (defun org-agenda-check-type (error &rest types)
4541 "Check if agenda buffer is of allowed type.
4542 If ERROR is non-nil, throw an error, otherwise just return nil."
4543 (if (memq org-agenda-type types)
4545 (if error
4546 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
4547 nil)))
4549 (defun org-agenda-quit ()
4550 "Exit agenda by removing the window or the buffer."
4551 (interactive)
4552 (if org-agenda-columns-active
4553 (org-columns-quit)
4554 (let ((buf (current-buffer)))
4555 (and (not (eq org-agenda-window-setup 'current-window))
4556 (not (one-window-p))
4557 (delete-window))
4558 (kill-buffer buf)
4559 (org-agenda-reset-markers)
4560 (org-columns-remove-overlays)
4561 (setq org-agenda-archives-mode nil))
4562 ;; Maybe restore the pre-agenda window configuration.
4563 (and org-agenda-restore-windows-after-quit
4564 (not (eq org-agenda-window-setup 'other-frame))
4565 org-pre-agenda-window-conf
4566 (set-window-configuration org-pre-agenda-window-conf))))
4568 (defun org-agenda-exit ()
4569 "Exit agenda by removing the window or the buffer.
4570 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
4571 Org-mode buffers visited directly by the user will not be touched."
4572 (interactive)
4573 (org-release-buffers org-agenda-new-buffers)
4574 (setq org-agenda-new-buffers nil)
4575 (org-agenda-quit))
4577 (defun org-agenda-execute (arg)
4578 "Execute another agenda command, keeping same window.\\<global-map>
4579 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
4580 (interactive "P")
4581 (let ((org-agenda-window-setup 'current-window))
4582 (org-agenda arg)))
4584 (defun org-agenda-redo ()
4585 "Rebuild Agenda.
4586 When this is the global TODO list, a prefix argument will be interpreted."
4587 (interactive)
4588 (let* ((org-agenda-keep-modes t)
4589 (filter org-agenda-filter)
4590 (preset (get 'org-agenda-filter :preset-filter))
4591 (cols org-agenda-columns-active)
4592 (line (org-current-line))
4593 (window-line (- line (org-current-line (window-start))))
4594 (lprops (get 'org-agenda-redo-command 'org-lprops)))
4595 (put 'org-agenda-filter :preset-filter nil)
4596 (and cols (org-columns-quit))
4597 (message "Rebuilding agenda buffer...")
4598 (org-let lprops '(eval org-agenda-redo-command))
4599 (setq org-agenda-undo-list nil
4600 org-agenda-pending-undo-list nil)
4601 (message "Rebuilding agenda buffer...done")
4602 (put 'org-agenda-filter :preset-filter preset)
4603 (and (or filter preset) (org-agenda-filter-apply filter))
4604 (and cols (interactive-p) (org-agenda-columns))
4605 (goto-line line)
4606 (recenter window-line)))
4609 (defvar org-global-tags-completion-table nil)
4610 (defvar org-agenda-filter-form nil)
4611 (defun org-agenda-filter-by-tag (strip &optional char narrow)
4612 "Keep only those lines in the agenda buffer that have a specific tag.
4613 The tag is selected with its fast selection letter, as configured.
4614 With prefix argument STRIP, remove all lines that do have the tag.
4615 A lisp caller can specify CHAR. NARROW means that the new tag should be
4616 used to narrow the search - the interactive user can also press `-' or `+'
4617 to switch to narrowing."
4618 (interactive "P")
4619 (let* ((alist org-tag-alist-for-agenda)
4620 (tag-chars (mapconcat
4621 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
4622 alist ""))
4623 (efforts (org-split-string
4624 (or (cdr (assoc (concat org-effort-property "_ALL")
4625 org-global-properties))
4626 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
4627 (effort-op org-agenda-filter-effort-default-operator)
4628 (effort-prompt "")
4629 (inhibit-read-only t)
4630 (current org-agenda-filter)
4631 char a n tag)
4632 (unless char
4633 (message
4634 "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<]:effort: "
4635 (if narrow "Narrow" "Filter") tag-chars)
4636 (setq char (read-char)))
4637 (when (member char '(?+ ?-))
4638 ;; Narrowing down
4639 (cond ((equal char ?-) (setq strip t narrow t))
4640 ((equal char ?+) (setq strip nil narrow t)))
4641 (message
4642 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
4643 (setq char (read-char)))
4644 (when (member char '(?< ?> ?=))
4645 ;; An effort operator
4646 (setq effort-op (char-to-string char))
4647 (loop for i from 0 to 9 do
4648 (setq effort-prompt
4649 (concat
4650 effort-prompt " ["
4651 (if (= i 9) "0" (int-to-string (1+ i)))
4652 "]" (nth i efforts))))
4653 (setq alist nil) ; to make sure it will be interpreted as effort.
4654 (message "Effort%s: %s " effort-op effort-prompt)
4655 (setq char (read-char))
4656 (when (or (< char ?0) (> char ?9))
4657 (error "Need 1-9,0 to select effort" )))
4658 (when (equal char ?\t)
4659 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
4660 (org-set-local 'org-global-tags-completion-table
4661 (org-global-tags-completion-table)))
4662 (let ((completion-ignore-case t))
4663 (setq tag (org-ido-completing-read
4664 "Tag: " org-global-tags-completion-table))))
4665 (cond
4666 ((equal char ?/)
4667 (org-agenda-filter-by-tag-show-all)
4668 (when (get 'org-agenda-filter :preset-filter)
4669 (org-agenda-filter-apply org-agenda-filter)))
4670 ((or (equal char ?\ )
4671 (setq a (rassoc char alist))
4672 (and (>= char ?0) (<= char ?9)
4673 (setq n (if (= char ?0) 9 (- char ?0 1))
4674 tag (concat effort-op (nth n efforts))
4675 a (cons tag nil)))
4676 (and tag (setq a (cons tag nil))))
4677 (org-agenda-filter-by-tag-show-all)
4678 (setq tag (car a))
4679 (setq org-agenda-filter
4680 (cons (concat (if strip "-" "+") tag)
4681 (if narrow current nil)))
4682 (org-agenda-filter-apply org-agenda-filter))
4683 (t (error "Invalid tag selection character %c" char)))))
4685 (defun org-agenda-filter-by-tag-refine (strip &optional char)
4686 "Refine the current filter. See `org-agenda-filter-by-tag."
4687 (interactive "P")
4688 (org-agenda-filter-by-tag strip char 'refine))
4690 (defun org-agenda-filter-make-matcher ()
4691 "Create the form that tests a line for the agenda filter."
4692 (let (f f1)
4693 (dolist (x (append (get 'org-agenda-filter :preset-filter)
4694 org-agenda-filter))
4695 (if (member x '("-" "+"))
4696 (setq f1 '(not tags))
4697 (if (string-match "[<=>]" x)
4698 (setq f1 (org-agenda-filter-effort-form x))
4699 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
4700 (if (equal (string-to-char x) ?-)
4701 (setq f1 (list 'not f1))))
4702 (push f1 f))
4703 (cons 'and (nreverse f))))
4705 (defun org-agenda-filter-effort-form (e)
4706 "Return the form to compare the effort of the current line with what E says.
4707 E looks line \"+<2:25\"."
4708 (let (op)
4709 (setq e (substring e 1))
4710 (setq op (string-to-char e) e (substring e 1))
4711 (setq op (if (equal op ?<) '<= (if (equal op ?>) '>= '=)))
4712 (list 'org-agenda-compare-effort (list 'quote op)
4713 (org-hh:mm-string-to-minutes e))))
4715 (defun org-agenda-compare-effort (op value)
4716 "Compare the effort of the current line with VALUE, using OP.
4717 If the line does not have an effort defined, return nil."
4718 (let ((eff (get-text-property (point) 'effort-minutes)))
4719 (if (not eff)
4720 0 ; we don't have an effort defined, treat as 0
4721 (funcall op eff value))))
4723 (defun org-agenda-filter-apply (filter)
4724 "Set FILTER as the new agenda filter and apply it."
4725 (let (tags)
4726 (setq org-agenda-filter filter
4727 org-agenda-filter-form (org-agenda-filter-make-matcher))
4728 (org-agenda-set-mode-name)
4729 (save-excursion
4730 (goto-char (point-min))
4731 (while (not (eobp))
4732 (if (get-text-property (point) 'org-marker)
4733 (progn
4734 (setq tags (get-text-property (point) 'tags)) ; used in eval
4735 (if (not (eval org-agenda-filter-form))
4736 (org-agenda-filter-by-tag-hide-line))
4737 (beginning-of-line 2))
4738 (beginning-of-line 2))))))
4740 (defun org-agenda-filter-by-tag-hide-line ()
4741 (let (ov)
4742 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
4743 (point-at-eol)))
4744 (org-overlay-put ov 'invisible t)
4745 (org-overlay-put ov 'type 'tags-filter)
4746 (push ov org-agenda-filter-overlays)))
4748 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
4749 (setq pos (or pos (point)))
4750 (save-excursion
4751 (dolist (ov (org-overlays-at pos))
4752 (when (and (org-overlay-get ov 'invisible)
4753 (eq (org-overlay-get ov 'type) 'tags-filter))
4754 (goto-char pos)
4755 (if (< (org-overlay-start ov) (point-at-eol))
4756 (org-move-overlay ov (point-at-eol)
4757 (org-overlay-end ov)))))))
4759 (defun org-agenda-filter-by-tag-show-all ()
4760 (mapc 'org-delete-overlay org-agenda-filter-overlays)
4761 (setq org-agenda-filter-overlays nil)
4762 (setq org-agenda-filter nil)
4763 (setq org-agenda-filter-form nil)
4764 (org-agenda-set-mode-name))
4766 (defun org-agenda-manipulate-query-add ()
4767 "Manipulate the query by adding a search term with positive selection.
4768 Positive selection means, the term must be matched for selection of an entry."
4769 (interactive)
4770 (org-agenda-manipulate-query ?\[))
4771 (defun org-agenda-manipulate-query-subtract ()
4772 "Manipulate the query by adding a search term with negative selection.
4773 Negative selection means, term must not be matched for selection of an entry."
4774 (interactive)
4775 (org-agenda-manipulate-query ?\]))
4776 (defun org-agenda-manipulate-query-add-re ()
4777 "Manipulate the query by adding a search regexp with positive selection.
4778 Positive selection means, the regexp must match for selection of an entry."
4779 (interactive)
4780 (org-agenda-manipulate-query ?\{))
4781 (defun org-agenda-manipulate-query-subtract-re ()
4782 "Manipulate the query by adding a search regexp with negative selection.
4783 Negative selection means, regexp must not match for selection of an entry."
4784 (interactive)
4785 (org-agenda-manipulate-query ?\}))
4786 (defun org-agenda-manipulate-query (char)
4787 (cond
4788 ((memq org-agenda-type '(timeline agenda))
4789 (if (y-or-n-p "Re-display with inactive time stamps included? ")
4790 (let ((org-agenda-include-inactive-timestamps t))
4791 (org-agenda-redo))
4792 (error "Abort")))
4793 ((eq org-agenda-type 'search)
4794 (org-add-to-string
4795 'org-agenda-query-string
4796 (cdr (assoc char '((?\[ . " +") (?\] . " -")
4797 (?\{ . " +{}") (?\} . " -{}")))))
4798 (setq org-agenda-redo-command
4799 (list 'org-search-view
4800 org-todo-only
4801 org-agenda-query-string
4802 (+ (length org-agenda-query-string)
4803 (if (member char '(?\{ ?\})) 0 1))))
4804 (set-register org-agenda-query-register org-agenda-query-string)
4805 (org-agenda-redo))
4806 (t (error "Cannot manipulate query for %s-type agenda buffers"
4807 org-agenda-type))))
4809 (defun org-add-to-string (var string)
4810 (set var (concat (symbol-value var) string)))
4812 (defun org-agenda-goto-date (date)
4813 "Jump to DATE in agenda."
4814 (interactive (list (org-read-date)))
4815 (org-agenda-list nil date))
4817 (defun org-agenda-goto-today ()
4818 "Go to today."
4819 (interactive)
4820 (org-agenda-check-type t 'timeline 'agenda)
4821 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
4822 (cond
4823 (tdpos (goto-char tdpos))
4824 ((eq org-agenda-type 'agenda)
4825 (let* ((sd (time-to-days
4826 (time-subtract (current-time)
4827 (list 0 (* 3600 org-extend-today-until) 0))))
4828 (comp (org-agenda-compute-time-span sd org-agenda-span))
4829 (org-agenda-overriding-arguments org-agenda-last-arguments))
4830 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
4831 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
4832 (org-agenda-redo)
4833 (org-agenda-find-same-or-today-or-agenda)))
4834 (t (error "Cannot find today")))))
4836 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
4837 (goto-char
4838 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
4839 (text-property-any (point-min) (point-max) 'org-today t)
4840 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
4841 (point-min))))
4843 (defun org-agenda-later (arg)
4844 "Go forward in time by thee current span.
4845 With prefix ARG, go forward that many times the current span."
4846 (interactive "p")
4847 (org-agenda-check-type t 'agenda)
4848 (let* ((span org-agenda-span)
4849 (sd org-starting-day)
4850 (greg (calendar-gregorian-from-absolute sd))
4851 (cnt (get-text-property (point) 'org-day-cnt))
4852 greg2 nd)
4853 (cond
4854 ((eq span 'day)
4855 (setq sd (+ arg sd) nd 1))
4856 ((eq span 'week)
4857 (setq sd (+ (* 7 arg) sd) nd 7))
4858 ((eq span 'month)
4859 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
4860 sd (calendar-absolute-from-gregorian greg2))
4861 (setcar greg2 (1+ (car greg2)))
4862 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
4863 ((eq span 'year)
4864 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
4865 sd (calendar-absolute-from-gregorian greg2))
4866 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
4867 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
4868 (let ((org-agenda-overriding-arguments
4869 (list (car org-agenda-last-arguments) sd nd t)))
4870 (org-agenda-redo)
4871 (org-agenda-find-same-or-today-or-agenda cnt))))
4873 (defun org-agenda-earlier (arg)
4874 "Go backward in time by the current span.
4875 With prefix ARG, go backward that many times the current span."
4876 (interactive "p")
4877 (org-agenda-later (- arg)))
4879 (defun org-agenda-day-view (&optional day-of-year)
4880 "Switch to daily view for agenda.
4881 With argument DAY-OF-YEAR, switch to that day of the year."
4882 (interactive "P")
4883 (setq org-agenda-ndays 1)
4884 (org-agenda-change-time-span 'day day-of-year))
4885 (defun org-agenda-week-view (&optional iso-week)
4886 "Switch to daily view for agenda.
4887 With argument ISO-WEEK, switch to the corresponding ISO week.
4888 If ISO-WEEK has more then 2 digits, only the last two encode the
4889 week. Any digits before this encode a year. So 200712 means
4890 week 12 of year 2007. Years in the range 1938-2037 can also be
4891 written as 2-digit years."
4892 (interactive "P")
4893 (setq org-agenda-ndays 7)
4894 (org-agenda-change-time-span 'week iso-week))
4895 (defun org-agenda-month-view (&optional month)
4896 "Switch to monthly view for agenda.
4897 With argument MONTH, switch to that month."
4898 (interactive "P")
4899 (org-agenda-change-time-span 'month month))
4900 (defun org-agenda-year-view (&optional year)
4901 "Switch to yearly view for agenda.
4902 With argument YEAR, switch to that year.
4903 If MONTH has more then 2 digits, only the last two encode the
4904 month. Any digits before this encode a year. So 200712 means
4905 December year 2007. Years in the range 1938-2037 can also be
4906 written as 2-digit years."
4907 (interactive "P")
4908 (when year
4909 (setq year (org-small-year-to-year year)))
4910 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
4911 (org-agenda-change-time-span 'year year)
4912 (error "Abort")))
4914 (defun org-agenda-change-time-span (span &optional n)
4915 "Change the agenda view to SPAN.
4916 SPAN may be `day', `week', `month', `year'."
4917 (org-agenda-check-type t 'agenda)
4918 (if (and (not n) (equal org-agenda-span span))
4919 (error "Viewing span is already \"%s\"" span))
4920 (let* ((sd (or (get-text-property (point) 'day)
4921 org-starting-day))
4922 (computed (org-agenda-compute-time-span sd span n))
4923 (org-agenda-overriding-arguments
4924 (list (car org-agenda-last-arguments)
4925 (car computed) (cdr computed) t)))
4926 (org-agenda-redo)
4927 (org-agenda-find-same-or-today-or-agenda))
4928 (org-agenda-set-mode-name)
4929 (message "Switched to %s view" span))
4931 (defun org-agenda-compute-time-span (sd span &optional n)
4932 "Compute starting date and number of days for agenda.
4933 SPAN may be `day', `week', `month', `year'. The return value
4934 is a cons cell with the starting date and the number of days,
4935 so that the date SD will be in that range."
4936 (let* ((greg (calendar-gregorian-from-absolute sd))
4937 (dg (nth 1 greg))
4938 (mg (car greg))
4939 (yg (nth 2 greg))
4940 nd w1 y1 m1 thisweek)
4941 (cond
4942 ((eq span 'day)
4943 (when n
4944 (setq sd (+ (calendar-absolute-from-gregorian
4945 (list mg 1 yg))
4946 n -1)))
4947 (setq nd 1))
4948 ((eq span 'week)
4949 (let* ((nt (calendar-day-of-week
4950 (calendar-gregorian-from-absolute sd)))
4951 (d (if org-agenda-start-on-weekday
4952 (- nt org-agenda-start-on-weekday)
4953 0)))
4954 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
4955 (when n
4956 (require 'cal-iso)
4957 (setq thisweek (car (calendar-iso-from-absolute sd)))
4958 (when (> n 99)
4959 (setq y1 (org-small-year-to-year (/ n 100))
4960 n (mod n 100)))
4961 (setq sd
4962 (calendar-absolute-from-iso
4963 (list n 1
4964 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
4965 (setq nd 7)))
4966 ((eq span 'month)
4967 (when (and n (> n 99))
4968 (setq y1 (org-small-year-to-year (/ n 100))
4969 n (mod n 100)))
4970 (setq sd (calendar-absolute-from-gregorian
4971 (list (or n mg) 1 (or y1 yg)))
4972 nd (- (calendar-absolute-from-gregorian
4973 (list (1+ (or n mg)) 1 (or y1 yg)))
4974 sd)))
4975 ((eq span 'year)
4976 (setq sd (calendar-absolute-from-gregorian
4977 (list 1 1 (or n yg)))
4978 nd (- (calendar-absolute-from-gregorian
4979 (list 1 1 (1+ (or n yg))))
4980 sd))))
4981 (cons sd nd)))
4983 (defun org-agenda-next-date-line (&optional arg)
4984 "Jump to the next line indicating a date in agenda buffer."
4985 (interactive "p")
4986 (org-agenda-check-type t 'agenda 'timeline)
4987 (beginning-of-line 1)
4988 ;; This does not work if user makes date format that starts with a blank
4989 (if (looking-at "^\\S-") (forward-char 1))
4990 (if (not (re-search-forward "^\\S-" nil t arg))
4991 (progn
4992 (backward-char 1)
4993 (error "No next date after this line in this buffer")))
4994 (goto-char (match-beginning 0)))
4996 (defun org-agenda-previous-date-line (&optional arg)
4997 "Jump to the previous line indicating a date in agenda buffer."
4998 (interactive "p")
4999 (org-agenda-check-type t 'agenda 'timeline)
5000 (beginning-of-line 1)
5001 (if (not (re-search-backward "^\\S-" nil t arg))
5002 (error "No previous date before this line in this buffer")))
5004 ;; Initialize the highlight
5005 (defvar org-hl (org-make-overlay 1 1))
5006 (org-overlay-put org-hl 'face 'highlight)
5008 (defun org-highlight (begin end &optional buffer)
5009 "Highlight a region with overlay."
5010 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5011 org-hl begin end (or buffer (current-buffer))))
5013 (defun org-unhighlight ()
5014 "Detach overlay INDEX."
5015 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5017 ;; FIXME this is currently not used.
5018 (defun org-highlight-until-next-command (beg end &optional buffer)
5019 "Move the highlight overlay to BEG/END, remove it before the next command."
5020 (org-highlight beg end buffer)
5021 (add-hook 'pre-command-hook 'org-unhighlight-once))
5022 (defun org-unhighlight-once ()
5023 "Remove the highlight from its position, and this function from the hook."
5024 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5025 (org-unhighlight))
5027 (defun org-agenda-follow-mode ()
5028 "Toggle follow mode in an agenda buffer."
5029 (interactive)
5030 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5031 (org-agenda-set-mode-name)
5032 (message "Follow mode is %s"
5033 (if org-agenda-follow-mode "on" "off")))
5035 (defun org-agenda-clockreport-mode ()
5036 "Toggle clocktable mode in an agenda buffer."
5037 (interactive)
5038 (org-agenda-check-type t 'agenda)
5039 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5040 (org-agenda-set-mode-name)
5041 (org-agenda-redo)
5042 (message "Clocktable mode is %s"
5043 (if org-agenda-clockreport-mode "on" "off")))
5045 (defun org-agenda-log-mode (&optional special)
5046 "Toggle log mode in an agenda buffer.
5047 With argument SPECIAL, show all possible log items, not only the ones
5048 configured in `org-agenda-log-mode-items'.
5049 With a double `C-u' prefix arg, show *only* log items, nothing else."
5050 (interactive "P")
5051 (org-agenda-check-type t 'agenda 'timeline)
5052 (setq org-agenda-show-log
5053 (if (equal special '(16))
5054 'only
5055 (if special '(closed clock state)
5056 (not org-agenda-show-log))))
5057 (org-agenda-set-mode-name)
5058 (org-agenda-redo)
5059 (message "Log mode is %s"
5060 (if org-agenda-show-log "on" "off")))
5062 (defun org-agenda-archives-mode (&optional with-files)
5063 "Toggle log mode in an agenda buffer."
5064 (interactive "P")
5065 (setq org-agenda-archives-mode
5066 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5067 (org-agenda-set-mode-name)
5068 (org-agenda-redo)
5069 (message
5070 "%s"
5071 (cond
5072 ((eq org-agenda-archives-mode nil)
5073 "No archives are included")
5074 ((eq org-agenda-archives-mode 'trees)
5075 (format "Trees with :%s: tag are included" org-archive-tag))
5076 ((eq org-agenda-archives-mode t)
5077 (format "Trees with :%s: tag and all active archive files are included"
5078 org-archive-tag)))))
5080 (defun org-agenda-toggle-diary ()
5081 "Toggle diary inclusion in an agenda buffer."
5082 (interactive)
5083 (org-agenda-check-type t 'agenda)
5084 (setq org-agenda-include-diary (not org-agenda-include-diary))
5085 (org-agenda-redo)
5086 (org-agenda-set-mode-name)
5087 (message "Diary inclusion turned %s"
5088 (if org-agenda-include-diary "on" "off")))
5090 (defun org-agenda-toggle-time-grid ()
5091 "Toggle time grid in an agenda buffer."
5092 (interactive)
5093 (org-agenda-check-type t 'agenda)
5094 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5095 (org-agenda-redo)
5096 (org-agenda-set-mode-name)
5097 (message "Time-grid turned %s"
5098 (if org-agenda-use-time-grid "on" "off")))
5100 (defun org-agenda-set-mode-name ()
5101 "Set the mode name to indicate all the small mode settings."
5102 (setq mode-name
5103 (concat "Org-Agenda"
5104 (if (equal org-agenda-ndays 1) " Day" "")
5105 (if (equal org-agenda-ndays 7) " Week" "")
5106 (if org-agenda-follow-mode " Follow" "")
5107 (if org-agenda-include-diary " Diary" "")
5108 (if org-agenda-use-time-grid " Grid" "")
5109 (if (consp org-agenda-show-log) " LogAll"
5110 (if org-agenda-show-log " Log" ""))
5111 (if (or org-agenda-filter (get 'org-agenda-filter
5112 :preset-filter))
5113 (concat " {" (mapconcat
5114 'identity
5115 (append (get 'org-agenda-filter
5116 :preset-filter)
5117 org-agenda-filter) "") "}")
5119 (if org-agenda-archives-mode
5120 (if (eq org-agenda-archives-mode t)
5121 " Archives"
5122 (format " :%s:" org-archive-tag))
5124 (if org-agenda-clockreport-mode " Clock" "")))
5125 (force-mode-line-update))
5127 (defun org-agenda-post-command-hook ()
5128 (and (eolp) (not (bolp)) (backward-char 1))
5129 (setq org-agenda-type
5130 (or (get-text-property (point) 'org-agenda-type)
5131 (get-text-property (max (point-min) (1- (point)))
5132 'org-agenda-type)))
5133 (if (and org-agenda-follow-mode
5134 (get-text-property (point) 'org-marker))
5135 (org-agenda-show)))
5137 (defun org-agenda-show-priority ()
5138 "Show the priority of the current item.
5139 This priority is composed of the main priority given with the [#A] cookies,
5140 and by additional input from the age of a schedules or deadline entry."
5141 (interactive)
5142 (let* ((pri (get-text-property (point-at-bol) 'priority)))
5143 (message "Priority is %d" (if pri pri -1000))))
5145 (defun org-agenda-show-tags ()
5146 "Show the tags applicable to the current item."
5147 (interactive)
5148 (let* ((tags (get-text-property (point-at-bol) 'tags)))
5149 (if tags
5150 (message "Tags are :%s:"
5151 (org-no-properties (mapconcat 'identity tags ":")))
5152 (message "No tags associated with this line"))))
5154 (defun org-agenda-goto (&optional highlight)
5155 "Go to the Org-mode file which contains the item at point."
5156 (interactive)
5157 (let* ((marker (or (get-text-property (point) 'org-marker)
5158 (org-agenda-error)))
5159 (buffer (marker-buffer marker))
5160 (pos (marker-position marker)))
5161 (switch-to-buffer-other-window buffer)
5162 (widen)
5163 (goto-char pos)
5164 (when (org-mode-p)
5165 (org-show-context 'agenda)
5166 (save-excursion
5167 (and (outline-next-heading)
5168 (org-flag-heading nil)))) ; show the next heading
5169 (recenter (/ (window-height) 2))
5170 (run-hooks 'org-agenda-after-show-hook)
5171 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5173 (defvar org-agenda-after-show-hook nil
5174 "Normal hook run after an item has been shown from the agenda.
5175 Point is in the buffer where the item originated.")
5177 (defun org-agenda-kill ()
5178 "Kill the entry or subtree belonging to the current agenda entry."
5179 (interactive)
5180 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5181 (let* ((marker (or (get-text-property (point) 'org-marker)
5182 (org-agenda-error)))
5183 (buffer (marker-buffer marker))
5184 (pos (marker-position marker))
5185 (type (get-text-property (point) 'type))
5186 dbeg dend (n 0) conf)
5187 (org-with-remote-undo buffer
5188 (with-current-buffer buffer
5189 (save-excursion
5190 (goto-char pos)
5191 (if (and (org-mode-p) (not (member type '("sexp"))))
5192 (setq dbeg (progn (org-back-to-heading t) (point))
5193 dend (org-end-of-subtree t t))
5194 (setq dbeg (point-at-bol)
5195 dend (min (point-max) (1+ (point-at-eol)))))
5196 (goto-char dbeg)
5197 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5198 (setq conf (or (eq t org-agenda-confirm-kill)
5199 (and (numberp org-agenda-confirm-kill)
5200 (> n org-agenda-confirm-kill))))
5201 (and conf
5202 (not (y-or-n-p
5203 (format "Delete entry with %d lines in buffer \"%s\"? "
5204 n (buffer-name buffer))))
5205 (error "Abort"))
5206 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5207 (with-current-buffer buffer (delete-region dbeg dend))
5208 (message "Agenda item and source killed"))))
5210 (defun org-agenda-archive ()
5211 "Archive the entry or subtree belonging to the current agenda entry."
5212 (interactive)
5213 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5214 (let* ((marker (or (get-text-property (point) 'org-marker)
5215 (org-agenda-error)))
5216 (buffer (marker-buffer marker))
5217 (pos (marker-position marker)))
5218 (org-with-remote-undo buffer
5219 (with-current-buffer buffer
5220 (if (org-mode-p)
5221 (save-excursion
5222 (goto-char pos)
5223 (org-remove-subtree-entries-from-agenda)
5224 (org-back-to-heading t)
5225 (org-archive-subtree))
5226 (error "Archiving works only in Org-mode files"))))))
5228 (defun org-agenda-archive-to-archive-sibling ()
5229 "Move the entry to the archive sibling."
5230 (interactive)
5231 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5232 (let* ((marker (or (get-text-property (point) 'org-marker)
5233 (org-agenda-error)))
5234 (buffer (marker-buffer marker))
5235 (pos (marker-position marker)))
5236 (org-with-remote-undo buffer
5237 (with-current-buffer buffer
5238 (if (org-mode-p)
5239 (save-excursion
5240 (goto-char pos)
5241 (org-remove-subtree-entries-from-agenda)
5242 (org-back-to-heading t)
5243 (org-archive-to-archive-sibling))
5244 (error "Archiving works only in Org-mode files"))))))
5246 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5247 "Remove all lines in the agenda that correspond to a given subtree.
5248 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5249 If this information is not given, the function uses the tree at point."
5250 (let ((buf (or buf (current-buffer))) m p)
5251 (save-excursion
5252 (unless (and beg end)
5253 (org-back-to-heading t)
5254 (setq beg (point))
5255 (org-end-of-subtree t)
5256 (setq end (point)))
5257 (set-buffer (get-buffer org-agenda-buffer-name))
5258 (save-excursion
5259 (goto-char (point-max))
5260 (beginning-of-line 1)
5261 (while (not (bobp))
5262 (when (and (setq m (get-text-property (point) 'org-marker))
5263 (equal buf (marker-buffer m))
5264 (setq p (marker-position m))
5265 (>= p beg)
5266 (<= p end))
5267 (let ((inhibit-read-only t))
5268 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5269 (beginning-of-line 0))))))
5271 (defun org-agenda-open-link ()
5272 "Follow the link in the current line, if any."
5273 (interactive)
5274 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
5275 (save-excursion
5276 (save-restriction
5277 (narrow-to-region (point-at-bol) (point-at-eol))
5278 (org-open-at-point))))
5280 (defun org-agenda-copy-local-variable (var)
5281 "Get a variable from a referenced buffer and install it here."
5282 (let ((m (get-text-property (point) 'org-marker)))
5283 (when (and m (buffer-live-p (marker-buffer m)))
5284 (org-set-local var (with-current-buffer (marker-buffer m)
5285 (symbol-value var))))))
5287 (defun org-agenda-switch-to (&optional delete-other-windows)
5288 "Go to the Org-mode file which contains the item at point."
5289 (interactive)
5290 (let* ((marker (or (get-text-property (point) 'org-marker)
5291 (org-agenda-error)))
5292 (buffer (marker-buffer marker))
5293 (pos (marker-position marker)))
5294 (switch-to-buffer buffer)
5295 (and delete-other-windows (delete-other-windows))
5296 (widen)
5297 (goto-char pos)
5298 (when (org-mode-p)
5299 (org-show-context 'agenda)
5300 (save-excursion
5301 (and (outline-next-heading)
5302 (org-flag-heading nil)))))) ; show the next heading
5304 (defun org-agenda-goto-mouse (ev)
5305 "Go to the Org-mode file which contains the item at the mouse click."
5306 (interactive "e")
5307 (mouse-set-point ev)
5308 (org-agenda-goto))
5310 (defun org-agenda-show (&optional full-entry)
5311 "Display the Org-mode file which contains the item at point.
5312 With prefix argument FULL-ENTRY, make the entire entry visible
5313 if it was hidden in the outline."
5314 (interactive "P")
5315 (let ((win (selected-window)))
5316 (if full-entry
5317 (let ((org-show-entry-below t))
5318 (org-agenda-goto t))
5319 (org-agenda-goto t))
5320 (select-window win)))
5322 (defun org-agenda-show-1 (&optional more)
5323 "Display the Org-mode file which contains the item at point.
5324 The prefix arg causes further revieling:
5326 0 hide the subtree
5327 1 just show the entry according to defaults.
5328 2 show the text below the heading
5329 3 show the entire subtree
5330 4 show the entire subtree and any LOGBOOK drawers
5331 5 show the entire subtree and any drawers
5332 With prefix argument FULL-ENTRY, make the entire entry visible
5333 if it was hidden in the outline."
5334 (interactive "p")
5335 (let ((win (selected-window)))
5336 (org-agenda-goto t)
5337 (org-recenter-heading 1)
5338 (cond
5339 ((= more 0)
5340 (hide-subtree)
5341 (message "Remote: hide subtree"))
5342 ((and (interactive-p) (= more 1))
5343 (message "Remote: show with default settings"))
5344 ((= more 2)
5345 (show-entry)
5346 (save-excursion
5347 (org-back-to-heading)
5348 (org-cycle-hide-drawers 'children))
5349 (message "Remote: show entry"))
5350 ((= more 3)
5351 (show-subtree)
5352 (save-excursion
5353 (org-back-to-heading)
5354 (org-cycle-hide-drawers 'subtree))
5355 (message "Remote: show subtree"))
5356 ((= more 4)
5357 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5358 (org-drawer-regexp
5359 (concat "^[ \t]*:\\("
5360 (mapconcat 'regexp-quote org-drawers "\\|")
5361 "\\):[ \t]*$")))
5362 (show-subtree)
5363 (save-excursion
5364 (org-back-to-heading)
5365 (org-cycle-hide-drawers 'subtree)))
5366 (message "Remote: show subtree and LOGBOOK"))
5367 ((> more 4)
5368 (show-subtree)
5369 (message "Remote: show subtree and LOGBOOK")))
5370 (select-window win)))
5372 (defun org-recenter-heading (n)
5373 (save-excursion
5374 (org-back-to-heading)
5375 (recenter n)))
5377 (defvar org-agenda-cycle-counter nil)
5378 (defun org-agenda-cycle-show (n)
5379 "Show the current entry in another window, with default settings.
5380 Default settings are taken from `org-show-hierarchy-above' and siblings.
5381 When use repeadedly in immediate succession, the remote entry will cycle
5382 through visibility
5384 entry -> subtree -> subtree with logbook"
5385 (interactive "p")
5386 (when (and (= n 1)
5387 (not (eq last-command this-command)))
5388 (setq org-agenda-cycle-counter 0))
5389 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
5390 (if (> org-agenda-cycle-counter 4)
5391 (setq org-agenda-cycle-counter 0))
5392 (org-agenda-show-1 org-agenda-cycle-counter))
5394 (defun org-agenda-recenter (arg)
5395 "Display the Org-mode file which contains the item at point and recenter."
5396 (interactive "P")
5397 (let ((win (selected-window)))
5398 (org-agenda-goto t)
5399 (recenter arg)
5400 (select-window win)))
5402 (defun org-agenda-show-mouse (ev)
5403 "Display the Org-mode file which contains the item at the mouse click."
5404 (interactive "e")
5405 (mouse-set-point ev)
5406 (org-agenda-show))
5408 (defun org-agenda-check-no-diary ()
5409 "Check if the entry is a diary link and abort if yes."
5410 (if (get-text-property (point) 'org-agenda-diary-link)
5411 (org-agenda-error)))
5413 (defun org-agenda-error ()
5414 (error "Command not allowed in this line"))
5416 (defun org-agenda-tree-to-indirect-buffer ()
5417 "Show the subtree corresponding to the current entry in an indirect buffer.
5418 This calls the command `org-tree-to-indirect-buffer' from the original
5419 Org-mode buffer.
5420 With numerical prefix arg ARG, go up to this level and then take that tree.
5421 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
5422 dedicated frame)."
5423 (interactive)
5424 (org-agenda-check-no-diary)
5425 (let* ((marker (or (get-text-property (point) 'org-marker)
5426 (org-agenda-error)))
5427 (buffer (marker-buffer marker))
5428 (pos (marker-position marker)))
5429 (with-current-buffer buffer
5430 (save-excursion
5431 (goto-char pos)
5432 (call-interactively 'org-tree-to-indirect-buffer)))))
5434 (defvar org-last-heading-marker (make-marker)
5435 "Marker pointing to the headline that last changed its TODO state
5436 by a remote command from the agenda.")
5438 (defun org-agenda-todo-nextset ()
5439 "Switch TODO entry to next sequence."
5440 (interactive)
5441 (org-agenda-todo 'nextset))
5443 (defun org-agenda-todo-previousset ()
5444 "Switch TODO entry to previous sequence."
5445 (interactive)
5446 (org-agenda-todo 'previousset))
5448 (defun org-agenda-todo (&optional arg)
5449 "Cycle TODO state of line at point, also in Org-mode file.
5450 This changes the line at point, all other lines in the agenda referring to
5451 the same tree node, and the headline of the tree node in the Org-mode file."
5452 (interactive "P")
5453 (org-agenda-check-no-diary)
5454 (let* ((col (current-column))
5455 (marker (or (get-text-property (point) 'org-marker)
5456 (org-agenda-error)))
5457 (buffer (marker-buffer marker))
5458 (pos (marker-position marker))
5459 (hdmarker (get-text-property (point) 'org-hd-marker))
5460 (todayp (equal (get-text-property (point) 'day)
5461 (time-to-days (current-time))))
5462 (inhibit-read-only t)
5463 org-agenda-headline-snapshot-before-repeat newhead just-one)
5464 (org-with-remote-undo buffer
5465 (with-current-buffer buffer
5466 (widen)
5467 (goto-char pos)
5468 (org-show-context 'agenda)
5469 (save-excursion
5470 (and (outline-next-heading)
5471 (org-flag-heading nil))) ; show the next heading
5472 (let ((current-prefix-arg arg))
5473 (call-interactively 'org-todo))
5474 (and (bolp) (forward-char 1))
5475 (setq newhead (org-get-heading))
5476 (when (and (org-bound-and-true-p
5477 org-agenda-headline-snapshot-before-repeat)
5478 (not (equal org-agenda-headline-snapshot-before-repeat
5479 newhead))
5480 todayp)
5481 (setq newhead org-agenda-headline-snapshot-before-repeat
5482 just-one t))
5483 (save-excursion
5484 (org-back-to-heading)
5485 (move-marker org-last-heading-marker (point))))
5486 (beginning-of-line 1)
5487 (save-excursion
5488 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
5489 (org-move-to-column col))))
5491 (defun org-agenda-add-note (&optional arg)
5492 "Add a time-stamped note to the entry at point."
5493 (interactive "P")
5494 (org-agenda-check-no-diary)
5495 (let* ((marker (or (get-text-property (point) 'org-marker)
5496 (org-agenda-error)))
5497 (buffer (marker-buffer marker))
5498 (pos (marker-position marker))
5499 (hdmarker (get-text-property (point) 'org-hd-marker))
5500 (inhibit-read-only t))
5501 (with-current-buffer buffer
5502 (widen)
5503 (goto-char pos)
5504 (org-show-context 'agenda)
5505 (save-excursion
5506 (and (outline-next-heading)
5507 (org-flag-heading nil))) ; show the next heading
5508 (org-add-note))))
5510 (defun org-agenda-change-all-lines (newhead hdmarker
5511 &optional fixface just-this)
5512 "Change all lines in the agenda buffer which match HDMARKER.
5513 The new content of the line will be NEWHEAD (as modified by
5514 `org-format-agenda-item'). HDMARKER is checked with
5515 `equal' against all `org-hd-marker' text properties in the file.
5516 If FIXFACE is non-nil, the face of each item is modified according to
5517 the new TODO state.
5518 If JUST-THIS is non-nil, change just the current line, not all.
5519 If FORCE-TAGS is non nil, the car of it returns the new tags."
5520 (let* ((inhibit-read-only t)
5521 (line (org-current-line))
5522 (thetags (with-current-buffer (marker-buffer hdmarker)
5523 (save-excursion (save-restriction (widen)
5524 (goto-char hdmarker)
5525 (org-get-tags-at)))))
5526 props m pl undone-face done-face finish new dotime cat tags)
5527 (save-excursion
5528 (goto-char (point-max))
5529 (beginning-of-line 1)
5530 (while (not finish)
5531 (setq finish (bobp))
5532 (when (and (setq m (get-text-property (point) 'org-hd-marker))
5533 (or (not just-this) (= (org-current-line) line))
5534 (equal m hdmarker))
5535 (setq props (text-properties-at (point))
5536 dotime (get-text-property (point) 'dotime)
5537 cat (get-text-property (point) 'org-category)
5538 tags thetags
5539 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
5540 pl (get-text-property (point) 'prefix-length)
5541 undone-face (get-text-property (point) 'undone-face)
5542 done-face (get-text-property (point) 'done-face))
5543 (org-move-to-column pl)
5544 (cond
5545 ((equal new "")
5546 (beginning-of-line 1)
5547 (and (looking-at ".*\n?") (replace-match "")))
5548 ((looking-at ".*")
5549 (replace-match new t t)
5550 (beginning-of-line 1)
5551 (add-text-properties (point-at-bol) (point-at-eol) props)
5552 (when fixface
5553 (add-text-properties
5554 (point-at-bol) (point-at-eol)
5555 (list 'face
5556 (if org-last-todo-state-is-todo
5557 undone-face done-face))))
5558 (org-agenda-highlight-todo 'line)
5559 (beginning-of-line 1))
5560 (t (error "Line update did not work"))))
5561 (beginning-of-line 0)))
5562 (org-finalize-agenda)))
5564 (defun org-agenda-align-tags (&optional line)
5565 "Align all tags in agenda items to `org-agenda-tags-column'."
5566 (let ((inhibit-read-only t) l c)
5567 (save-excursion
5568 (goto-char (if line (point-at-bol) (point-min)))
5569 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
5570 (if line (point-at-eol) nil) t)
5571 (add-text-properties
5572 (match-beginning 2) (match-end 2)
5573 (list 'face (delq nil (let ((prop (get-text-property
5574 (match-beginning 2) 'face)))
5575 (or (listp prop) (setq prop (list prop)))
5576 (if (memq 'org-tag prop)
5577 prop
5578 (cons 'org-tag prop))))))
5579 (setq l (- (match-end 2) (match-beginning 2))
5580 c (if (< org-agenda-tags-column 0)
5581 (- (abs org-agenda-tags-column) l)
5582 org-agenda-tags-column))
5583 (delete-region (match-beginning 1) (match-end 1))
5584 (goto-char (match-beginning 1))
5585 (insert (org-add-props
5586 (make-string (max 1 (- c (current-column))) ?\ )
5587 (text-properties-at (point)))))
5588 (goto-char (point-min))
5589 (org-font-lock-add-tag-faces (point-max)))))
5591 (defun org-agenda-priority-up ()
5592 "Increase the priority of line at point, also in Org-mode file."
5593 (interactive)
5594 (org-agenda-priority 'up))
5596 (defun org-agenda-priority-down ()
5597 "Decrease the priority of line at point, also in Org-mode file."
5598 (interactive)
5599 (org-agenda-priority 'down))
5601 (defun org-agenda-priority (&optional force-direction)
5602 "Set the priority of line at point, also in Org-mode file.
5603 This changes the line at point, all other lines in the agenda referring to
5604 the same tree node, and the headline of the tree node in the Org-mode file."
5605 (interactive)
5606 (org-agenda-check-no-diary)
5607 (let* ((marker (or (get-text-property (point) 'org-marker)
5608 (org-agenda-error)))
5609 (hdmarker (get-text-property (point) 'org-hd-marker))
5610 (buffer (marker-buffer hdmarker))
5611 (pos (marker-position hdmarker))
5612 (inhibit-read-only t)
5613 newhead)
5614 (org-with-remote-undo buffer
5615 (with-current-buffer buffer
5616 (widen)
5617 (goto-char pos)
5618 (org-show-context 'agenda)
5619 (save-excursion
5620 (and (outline-next-heading)
5621 (org-flag-heading nil))) ; show the next heading
5622 (funcall 'org-priority force-direction)
5623 (end-of-line 1)
5624 (setq newhead (org-get-heading)))
5625 (org-agenda-change-all-lines newhead hdmarker)
5626 (beginning-of-line 1))))
5628 ;; FIXME: should fix the tags property of the agenda line.
5629 (defun org-agenda-set-tags ()
5630 "Set tags for the current headline."
5631 (interactive)
5632 (org-agenda-check-no-diary)
5633 (if (and (org-region-active-p) (interactive-p))
5634 (call-interactively 'org-change-tag-in-region)
5635 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5636 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5637 (org-agenda-error)))
5638 (buffer (marker-buffer hdmarker))
5639 (pos (marker-position hdmarker))
5640 (inhibit-read-only t)
5641 newhead)
5642 (org-with-remote-undo buffer
5643 (with-current-buffer buffer
5644 (widen)
5645 (goto-char pos)
5646 (save-excursion
5647 (org-show-context 'agenda))
5648 (save-excursion
5649 (and (outline-next-heading)
5650 (org-flag-heading nil))) ; show the next heading
5651 (goto-char pos)
5652 (call-interactively 'org-set-tags)
5653 (end-of-line 1)
5654 (setq newhead (org-get-heading)))
5655 (org-agenda-change-all-lines newhead hdmarker)
5656 (beginning-of-line 1)))))
5658 (defun org-agenda-toggle-archive-tag ()
5659 "Toggle the archive tag for the current entry."
5660 (interactive)
5661 (org-agenda-check-no-diary)
5662 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5663 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5664 (org-agenda-error)))
5665 (buffer (marker-buffer hdmarker))
5666 (pos (marker-position hdmarker))
5667 (inhibit-read-only t)
5668 newhead)
5669 (org-with-remote-undo buffer
5670 (with-current-buffer buffer
5671 (widen)
5672 (goto-char pos)
5673 (org-show-context 'agenda)
5674 (save-excursion
5675 (and (outline-next-heading)
5676 (org-flag-heading nil))) ; show the next heading
5677 (call-interactively 'org-toggle-archive-tag)
5678 (end-of-line 1)
5679 (setq newhead (org-get-heading)))
5680 (org-agenda-change-all-lines newhead hdmarker)
5681 (beginning-of-line 1))))
5683 (defun org-agenda-do-date-later (arg)
5684 (interactive "P")
5685 (cond
5686 ((or (equal arg '(16))
5687 (memq last-command
5688 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
5689 (setq this-command 'org-agenda-date-later-minutes)
5690 (org-agenda-date-later-minutes 1))
5691 ((or (equal arg '(4))
5692 (memq last-command
5693 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
5694 (setq this-command 'org-agenda-date-later-hours)
5695 (org-agenda-date-later-hours 1))
5697 (org-agenda-date-later (prefix-numeric-value arg)))))
5699 (defun org-agenda-do-date-earlier (arg)
5700 (interactive "P")
5701 (cond
5702 ((or (equal arg '(16))
5703 (memq last-command
5704 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
5705 (setq this-command 'org-agenda-date-earlier-minutes)
5706 (org-agenda-date-earlier-minutes 1))
5707 ((or (equal arg '(4))
5708 (memq last-command
5709 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
5710 (setq this-command 'org-agenda-date-earlier-hours)
5711 (org-agenda-date-earlier-hours 1))
5713 (org-agenda-date-earlier (prefix-numeric-value arg)))))
5715 (defun org-agenda-date-later (arg &optional what)
5716 "Change the date of this item to one day later."
5717 (interactive "p")
5718 (org-agenda-check-type t 'agenda 'timeline)
5719 (org-agenda-check-no-diary)
5720 (let* ((marker (or (get-text-property (point) 'org-marker)
5721 (org-agenda-error)))
5722 (buffer (marker-buffer marker))
5723 (pos (marker-position marker)))
5724 (org-with-remote-undo buffer
5725 (with-current-buffer buffer
5726 (widen)
5727 (goto-char pos)
5728 (if (not (org-at-timestamp-p))
5729 (error "Cannot find time stamp"))
5730 (org-timestamp-change arg (or what 'day)))
5731 (org-agenda-show-new-time marker org-last-changed-timestamp))
5732 (message "Time stamp changed to %s" org-last-changed-timestamp)))
5734 (defun org-agenda-date-earlier (arg &optional what)
5735 "Change the date of this item to one day earlier."
5736 (interactive "p")
5737 (org-agenda-date-later (- arg) what))
5739 (defun org-agenda-date-later-minutes (arg)
5740 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
5741 (interactive "p")
5742 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
5743 (org-agenda-date-later arg 'minute))
5745 (defun org-agenda-date-earlier-minutes (arg)
5746 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
5747 (interactive "p")
5748 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
5749 (org-agenda-date-earlier arg 'minute))
5751 (defun org-agenda-date-later-hours (arg)
5752 "Change the time of this item, in hour steps."
5753 (interactive "p")
5754 (org-agenda-date-later arg 'hour))
5756 (defun org-agenda-date-earlier-hours (arg)
5757 "Change the time of this item, in hour steps."
5758 (interactive "p")
5759 (org-agenda-date-earlier arg 'hour))
5761 (defun org-agenda-show-new-time (marker stamp &optional prefix)
5762 "Show new date stamp via text properties."
5763 ;; We use text properties to make this undoable
5764 (let ((inhibit-read-only t)
5765 (buffer-invisibility-spec))
5766 (setq stamp (concat " " prefix " => " stamp))
5767 (save-excursion
5768 (goto-char (point-max))
5769 (while (not (bobp))
5770 (when (equal marker (get-text-property (point) 'org-marker))
5771 (org-move-to-column (- (window-width) (length stamp)) t)
5772 (org-agenda-fix-tags-filter-overlays-at (point))
5773 (if (featurep 'xemacs)
5774 ;; Use `duplicable' property to trigger undo recording
5775 (let ((ex (make-extent nil nil))
5776 (gl (make-glyph stamp)))
5777 (set-glyph-face gl 'secondary-selection)
5778 (set-extent-properties
5779 ex (list 'invisible t 'end-glyph gl 'duplicable t))
5780 (insert-extent ex (1- (point)) (point-at-eol)))
5781 (add-text-properties
5782 (1- (point)) (point-at-eol)
5783 (list 'display (org-add-props stamp nil
5784 'face 'secondary-selection))))
5785 (beginning-of-line 1))
5786 (beginning-of-line 0)))))
5788 (defun org-agenda-date-prompt (arg)
5789 "Change the date of this item. Date is prompted for, with default today.
5790 The prefix ARG is passed to the `org-time-stamp' command and can therefore
5791 be used to request time specification in the time stamp."
5792 (interactive "P")
5793 (org-agenda-check-type t 'agenda 'timeline)
5794 (org-agenda-check-no-diary)
5795 (let* ((marker (or (get-text-property (point) 'org-marker)
5796 (org-agenda-error)))
5797 (buffer (marker-buffer marker))
5798 (pos (marker-position marker)))
5799 (org-with-remote-undo buffer
5800 (with-current-buffer buffer
5801 (widen)
5802 (goto-char pos)
5803 (if (not (org-at-timestamp-p))
5804 (error "Cannot find time stamp"))
5805 (org-time-stamp arg)
5806 (message "Time stamp changed to %s" org-last-changed-timestamp)))))
5808 (defun org-agenda-schedule (arg)
5809 "Schedule the item at point."
5810 (interactive "P")
5811 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5812 (org-agenda-check-no-diary)
5813 (let* ((marker (or (get-text-property (point) 'org-marker)
5814 (org-agenda-error)))
5815 (type (marker-insertion-type marker))
5816 (buffer (marker-buffer marker))
5817 (pos (marker-position marker))
5818 (org-insert-labeled-timestamps-at-point nil)
5820 (set-marker-insertion-type marker t)
5821 (org-with-remote-undo buffer
5822 (with-current-buffer buffer
5823 (widen)
5824 (goto-char pos)
5825 (setq ts (org-schedule arg)))
5826 (org-agenda-show-new-time marker ts "S"))
5827 (message "Item scheduled for %s" ts)))
5829 (defun org-agenda-deadline (arg)
5830 "Schedule the item at point."
5831 (interactive "P")
5832 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5833 (org-agenda-check-no-diary)
5834 (let* ((marker (or (get-text-property (point) 'org-marker)
5835 (org-agenda-error)))
5836 (buffer (marker-buffer marker))
5837 (pos (marker-position marker))
5838 (org-insert-labeled-timestamps-at-point nil)
5840 (org-with-remote-undo buffer
5841 (with-current-buffer buffer
5842 (widen)
5843 (goto-char pos)
5844 (setq ts (org-deadline arg)))
5845 (org-agenda-show-new-time marker ts "S"))
5846 (message "Deadline for this item set to %s" ts)))
5848 (defun org-agenda-action ()
5849 "Select entry for agenda action, or execute an agenda action.
5850 This command prompts for another letter. Valid inputs are:
5852 m Mark the entry at point for an agenda action
5853 s Schedule the marked entry to the date at the cursor
5854 d Set the deadline of the marked entry to the date at the cursor
5855 r Call `org-remember' with cursor date as the default date
5856 SPC Show marked entry in other window
5857 TAB Visit marked entry in other window
5859 The cursor may be at a date in the calendar, or in the Org agenda."
5860 (interactive)
5861 (let (ans)
5862 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
5863 (setq ans (read-char-exclusive))
5864 (cond
5865 ((equal ans ?m)
5866 ;; Mark this entry
5867 (if (eq major-mode 'org-agenda-mode)
5868 (let ((m (or (get-text-property (point) 'org-hd-marker)
5869 (get-text-property (point) 'org-marker))))
5870 (if m
5871 (progn
5872 (move-marker org-agenda-action-marker
5873 (marker-position m) (marker-buffer m))
5874 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
5875 (error "Don't know which entry to mark")))
5876 (error "This command works only in the agenda")))
5877 ((equal ans ?s)
5878 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
5879 ((equal ans ?d)
5880 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
5881 ((equal ans ?r)
5882 (org-agenda-do-action '(org-remember) t))
5883 ((equal ans ?\ )
5884 (let ((cw (selected-window)))
5885 (org-switch-to-buffer-other-window
5886 (marker-buffer org-agenda-action-marker))
5887 (goto-char org-agenda-action-marker)
5888 (org-show-context 'agenda)
5889 (select-window cw)))
5890 ((equal ans ?\C-i)
5891 (org-switch-to-buffer-other-window
5892 (marker-buffer org-agenda-action-marker))
5893 (goto-char org-agenda-action-marker)
5894 (org-show-context 'agenda))
5895 (t (error "Invalid agenda action %c" ans)))))
5897 (defun org-agenda-do-action (form &optional current-buffer)
5898 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
5899 (let ((org-overriding-default-time (org-get-cursor-date)))
5900 (if current-buffer
5901 (eval form)
5902 (if (not (marker-buffer org-agenda-action-marker))
5903 (error "No entry has bee selected for agenda action")
5904 (with-current-buffer (marker-buffer org-agenda-action-marker)
5905 (save-excursion
5906 (save-restriction
5907 (widen)
5908 (goto-char org-agenda-action-marker)
5909 (eval form))))))))
5911 (defun org-agenda-clock-in (&optional arg)
5912 "Start the clock on the currently selected item."
5913 (interactive "P")
5914 (org-agenda-check-no-diary)
5915 (if (equal arg '(4))
5916 (org-clock-in arg)
5917 (let* ((marker (or (get-text-property (point) 'org-marker)
5918 (org-agenda-error)))
5919 (hdmarker (or (get-text-property (point) 'org-hd-marker)
5920 marker))
5921 (pos (marker-position marker))
5922 newhead)
5923 (org-with-remote-undo (marker-buffer marker)
5924 (with-current-buffer (marker-buffer marker)
5925 (widen)
5926 (goto-char pos)
5927 (org-show-context 'agenda)
5928 (org-show-entry)
5929 (org-cycle-hide-drawers 'children)
5930 (org-clock-in arg)
5931 (setq newhead (org-get-heading)))
5932 (org-agenda-change-all-lines newhead hdmarker t)))))
5934 (defun org-agenda-clock-out (&optional arg)
5935 "Stop the currently running clock."
5936 (interactive "P")
5937 (unless (marker-buffer org-clock-marker)
5938 (error "No running clock"))
5939 (org-with-remote-undo (marker-buffer org-clock-marker)
5940 (org-clock-out)))
5942 (defun org-agenda-clock-cancel (&optional arg)
5943 "Cancel the currently running clock."
5944 (interactive "P")
5945 (unless (marker-buffer org-clock-marker)
5946 (error "No running clock"))
5947 (org-with-remote-undo (marker-buffer org-clock-marker)
5948 (org-clock-cancel)))
5950 (defun org-agenda-diary-entry ()
5951 "Make a diary entry, like the `i' command from the calendar.
5952 All the standard commands work: block, weekly etc."
5953 (interactive)
5954 (org-agenda-check-type t 'agenda 'timeline)
5955 (require 'diary-lib)
5956 (let* ((char (progn
5957 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
5958 (read-char-exclusive)))
5959 (cmd (cdr (assoc char
5960 '((?d . insert-diary-entry)
5961 (?w . insert-weekly-diary-entry)
5962 (?m . insert-monthly-diary-entry)
5963 (?y . insert-yearly-diary-entry)
5964 (?a . insert-anniversary-diary-entry)
5965 (?b . insert-block-diary-entry)
5966 (?c . insert-cyclic-diary-entry)))))
5967 (oldf (symbol-function 'calendar-cursor-to-date))
5968 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
5969 (point (point))
5970 (mark (or (mark t) (point))))
5971 (unless cmd
5972 (error "No command associated with <%c>" char))
5973 (unless (and (get-text-property point 'day)
5974 (or (not (equal ?b char))
5975 (get-text-property mark 'day)))
5976 (error "Don't know which date to use for diary entry"))
5977 ;; We implement this by hacking the `calendar-cursor-to-date' function
5978 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
5979 (let ((calendar-mark-ring
5980 (list (calendar-gregorian-from-absolute
5981 (or (get-text-property mark 'day)
5982 (get-text-property point 'day))))))
5983 (unwind-protect
5984 (progn
5985 (fset 'calendar-cursor-to-date
5986 (lambda (&optional error dummy)
5987 (calendar-gregorian-from-absolute
5988 (get-text-property point 'day))))
5989 (call-interactively cmd))
5990 (fset 'calendar-cursor-to-date oldf)))))
5992 (defun org-agenda-execute-calendar-command (cmd)
5993 "Execute a calendar command from the agenda, with the date associated to
5994 the cursor position."
5995 (org-agenda-check-type t 'agenda 'timeline)
5996 (require 'diary-lib)
5997 (unless (get-text-property (point) 'day)
5998 (error "Don't know which date to use for calendar command"))
5999 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6000 (point (point))
6001 (date (calendar-gregorian-from-absolute
6002 (get-text-property point 'day)))
6003 ;; the following 2 vars are needed in the calendar
6004 (displayed-month (car date))
6005 (displayed-year (nth 2 date)))
6006 (unwind-protect
6007 (progn
6008 (fset 'calendar-cursor-to-date
6009 (lambda (&optional error dummy)
6010 (calendar-gregorian-from-absolute
6011 (get-text-property point 'day))))
6012 (call-interactively cmd))
6013 (fset 'calendar-cursor-to-date oldf))))
6015 (defun org-agenda-phases-of-moon ()
6016 "Display the phases of the moon for the 3 months around the cursor date."
6017 (interactive)
6018 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6020 (defun org-agenda-holidays ()
6021 "Display the holidays for the 3 months around the cursor date."
6022 (interactive)
6023 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6025 (defvar calendar-longitude)
6026 (defvar calendar-latitude)
6027 (defvar calendar-location-name)
6029 (defun org-agenda-sunrise-sunset (arg)
6030 "Display sunrise and sunset for the cursor date.
6031 Latitude and longitude can be specified with the variables
6032 `calendar-latitude' and `calendar-longitude'. When called with prefix
6033 argument, latitude and longitude will be prompted for."
6034 (interactive "P")
6035 (require 'solar)
6036 (let ((calendar-longitude (if arg nil calendar-longitude))
6037 (calendar-latitude (if arg nil calendar-latitude))
6038 (calendar-location-name
6039 (if arg "the given coordinates" calendar-location-name)))
6040 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6042 (defun org-agenda-goto-calendar ()
6043 "Open the Emacs calendar with the date at the cursor."
6044 (interactive)
6045 (org-agenda-check-type t 'agenda 'timeline)
6046 (let* ((day (or (get-text-property (point) 'day)
6047 (error "Don't know which date to open in calendar")))
6048 (date (calendar-gregorian-from-absolute day))
6049 (calendar-move-hook nil)
6050 (calendar-view-holidays-initially-flag nil)
6051 (calendar-view-diary-initially-flag nil)
6052 (view-calendar-holidays-initially nil)
6053 (view-diary-entries-initially nil))
6054 (calendar)
6055 (calendar-goto-date date)))
6057 ;;;###autoload
6058 (defun org-calendar-goto-agenda ()
6059 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6060 This is a command that has to be installed in `calendar-mode-map'."
6061 (interactive)
6062 (org-agenda-list nil (calendar-absolute-from-gregorian
6063 (calendar-cursor-to-date))
6064 nil))
6066 (defun org-agenda-convert-date ()
6067 (interactive)
6068 (org-agenda-check-type t 'agenda 'timeline)
6069 (let ((day (get-text-property (point) 'day))
6070 date s)
6071 (unless day
6072 (error "Don't know which date to convert"))
6073 (setq date (calendar-gregorian-from-absolute day))
6074 (setq s (concat
6075 "Gregorian: " (calendar-date-string date) "\n"
6076 "ISO: " (calendar-iso-date-string date) "\n"
6077 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6078 "Julian: " (calendar-julian-date-string date) "\n"
6079 "Astron. JD: " (calendar-astro-date-string date)
6080 " (Julian date number at noon UTC)\n"
6081 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6082 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6083 "French: " (calendar-french-date-string date) "\n"
6084 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6085 "Mayan: " (calendar-mayan-date-string date) "\n"
6086 "Coptic: " (calendar-coptic-date-string date) "\n"
6087 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6088 "Persian: " (calendar-persian-date-string date) "\n"
6089 "Chinese: " (calendar-chinese-date-string date) "\n"))
6090 (with-output-to-temp-buffer "*Dates*"
6091 (princ s))
6092 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6094 ;;; Appointment reminders
6096 (defvar appt-time-msg-list)
6098 ;;;###autoload
6099 (defun org-agenda-to-appt (&optional refresh filter)
6100 "Activate appointments found in `org-agenda-files'.
6101 With a \\[universal-argument] prefix, refresh the list of
6102 appointments.
6104 If FILTER is t, interactively prompt the user for a regular
6105 expression, and filter out entries that don't match it.
6107 If FILTER is a string, use this string as a regular expression
6108 for filtering entries out.
6110 FILTER can also be an alist with the car of each cell being
6111 either 'headline or 'category. For example:
6113 '((headline \"IMPORTANT\")
6114 (category \"Work\"))
6116 will only add headlines containing IMPORTANT or headlines
6117 belonging to the \"Work\" category."
6118 (interactive "P")
6119 (require 'calendar)
6120 (if refresh (setq appt-time-msg-list nil))
6121 (if (eq filter t)
6122 (setq filter (read-from-minibuffer "Regexp filter: ")))
6123 (let* ((cnt 0) ; count added events
6124 (org-agenda-new-buffers nil)
6125 (org-deadline-warning-days 0)
6126 (today (org-date-to-gregorian
6127 (time-to-days (current-time))))
6128 (files (org-agenda-files 'unrestricted)) entries file)
6129 ;; Get all entries which may contain an appt
6130 (org-prepare-agenda-buffers files)
6131 (while (setq file (pop files))
6132 (setq entries
6133 (append entries
6134 (org-agenda-get-day-entries
6135 file today :timestamp :scheduled :deadline))))
6136 (setq entries (delq nil entries))
6137 ;; Map thru entries and find if we should filter them out
6138 (mapc
6139 (lambda(x)
6140 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
6141 (cat (get-text-property 1 'org-category x))
6142 (tod (get-text-property 1 'time-of-day x))
6143 (ok (or (null filter)
6144 (and (stringp filter) (string-match filter evt))
6145 (and (listp filter)
6146 (or (string-match
6147 (cadr (assoc 'category filter)) cat)
6148 (string-match
6149 (cadr (assoc 'headline filter)) evt))))))
6150 ;; FIXME: Shall we remove text-properties for the appt text?
6151 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
6152 (when (and ok tod)
6153 (setq tod (concat "00" (number-to-string tod))
6154 tod (when (string-match
6155 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
6156 (concat (match-string 1 tod) ":"
6157 (match-string 2 tod))))
6158 (appt-add tod evt)
6159 (setq cnt (1+ cnt))))) entries)
6160 (org-release-buffers org-agenda-new-buffers)
6161 (if (eq cnt 0)
6162 (message "No event to add")
6163 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
6165 (defun org-agenda-todayp (date)
6166 "Does DATE mean today, when considering `org-extend-today-until'?"
6167 (let (today h)
6168 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
6169 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
6170 (setq h (nth 2 (decode-time (current-time))))
6171 (or (and (>= h org-extend-today-until)
6172 (= date today))
6173 (and (< h org-extend-today-until)
6174 (= date (1- today))))))
6176 (provide 'org-agenda)
6178 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
6180 ;;; org-agenda.el ends here