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