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