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