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