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