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