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