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