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