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