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