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