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