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