Push version number to 6.11pre01.
[org-mode.git] / lisp / org-agenda.el
blobaabd45e53c3db9dd0c67f27a39555e32901709e3
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.11pre01
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-check-holidays "holidays" (date))
44 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
45 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
47 (declare-function calendar-french-date-string "cal-french" (&optional date))
48 (declare-function calendar-goto-date "cal-move" (date))
49 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
50 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
51 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
52 (declare-function calendar-iso-from-absolute "cal-iso" (date))
53 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
54 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
55 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
56 (declare-function org-columns-quit "org-colview" ())
57 (defvar calendar-mode-map)
59 ;; Defined somewhere in this file, but used before definition.
60 (defvar org-agenda-buffer-name)
61 (defvar org-agenda-overriding-header)
62 (defvar entry)
63 (defvar date)
64 (defvar org-agenda-undo-list)
65 (defvar org-agenda-pending-undo-list)
66 (defvar original-date) ; dynamically scoped, calendar.el does scope this
68 (defcustom org-agenda-confirm-kill 1
69 "When set, remote killing from the agenda buffer needs confirmation.
70 When t, a confirmation is always needed. When a number N, confirmation is
71 only needed when the text to be killed contains more than N non-white lines."
72 :group 'org-agenda
73 :type '(choice
74 (const :tag "Never" nil)
75 (const :tag "Always" t)
76 (number :tag "When more than N lines")))
78 (defcustom org-agenda-compact-blocks nil
79 "Non-nil means, make the block agenda more compact.
80 This is done by leaving out unnecessary lines."
81 :group 'org-agenda
82 :type 'boolean)
84 (defgroup org-agenda-export nil
85 "Options concerning exporting agenda views in Org-mode."
86 :tag "Org Agenda Export"
87 :group 'org-agenda)
89 (defcustom org-agenda-with-colors t
90 "Non-nil means, use colors in agenda views."
91 :group 'org-agenda-export
92 :type 'boolean)
94 (defcustom org-agenda-exporter-settings nil
95 "Alist of variable/value pairs that should be active during agenda export.
96 This is a good place to set uptions for ps-print and for htmlize."
97 :group 'org-agenda-export
98 :type '(repeat
99 (list
100 (variable)
101 (sexp :tag "Value"))))
103 (defcustom org-agenda-export-html-style ""
104 "The style specification for exported HTML Agenda files.
105 If this variable contains a string, it will replace the default <style>
106 section as produced by `htmlize'.
107 Since there are different ways of setting style information, this variable
108 needs to contain the full HTML structure to provide a style, including the
109 surrounding HTML tags. The style specifications should include definitions
110 the fonts used by the agenda, here is an example:
112 <style type=\"text/css\">
113 p { font-weight: normal; color: gray; }
114 .org-agenda-structure {
115 font-size: 110%;
116 color: #003399;
117 font-weight: 600;
119 .org-todo {
120 color: #cc6666;
121 font-weight: bold;
123 .org-done {
124 color: #339933;
126 .title { text-align: center; }
127 .todo, .deadline { color: red; }
128 .done { color: green; }
129 </style>
131 or, if you want to keep the style in a file,
133 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
135 As the value of this option simply gets inserted into the HTML <head> header,
136 you can \"misuse\" it to also add other text to the header. However,
137 <style>...</style> is required, if not present the variable will be ignored."
138 :group 'org-agenda-export
139 :group 'org-export-html
140 :type 'string)
142 (defgroup org-agenda-custom-commands nil
143 "Options concerning agenda views in Org-mode."
144 :tag "Org Agenda Custom Commands"
145 :group 'org-agenda)
147 (defconst org-sorting-choice
148 '(choice
149 (const time-up) (const time-down)
150 (const category-keep) (const category-up) (const category-down)
151 (const tag-down) (const tag-up)
152 (const priority-up) (const priority-down)
153 (const todo-state-up) (const todo-state-down)
154 (const effort-up) (const effort-down))
155 "Sorting choices.")
157 (defconst org-agenda-custom-commands-local-options
158 `(repeat :tag "Local settings for this command. Remember to quote values"
159 (choice :tag "Setting"
160 (list :tag "Any variable"
161 (variable :tag "Variable")
162 (sexp :tag "Value"))
163 (list :tag "Files to be searched"
164 (const org-agenda-files)
165 (list
166 (const :format "" quote)
167 (repeat
168 (file))))
169 (list :tag "Sorting strategy"
170 (const org-agenda-sorting-strategy)
171 (list
172 (const :format "" quote)
173 (repeat
174 ,org-sorting-choice)))
175 (list :tag "Prefix format"
176 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
177 (string))
178 (list :tag "Number of days in agenda"
179 (const org-agenda-ndays)
180 (integer :value 1))
181 (list :tag "Fixed starting date"
182 (const org-agenda-start-day)
183 (string :value "2007-11-01"))
184 (list :tag "Start on day of week"
185 (const org-agenda-start-on-weekday)
186 (choice :value 1
187 (const :tag "Today" nil)
188 (number :tag "Weekday No.")))
189 (list :tag "Include data from diary"
190 (const org-agenda-include-diary)
191 (boolean))
192 (list :tag "Deadline Warning days"
193 (const org-deadline-warning-days)
194 (integer :value 1))
195 (list :tag "Standard skipping condition"
196 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
197 (const org-agenda-skip-function)
198 (list
199 (const :format "" quote)
200 (list
201 (choice
202 :tag "Skiping range"
203 (const :tag "Skip entry" org-agenda-skip-entry-if)
204 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
205 (repeat :inline t :tag "Conditions for skipping"
206 (choice
207 :tag "Condition type"
208 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
209 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
210 (const :tag "scheduled" 'scheduled)
211 (const :tag "not scheduled" 'notscheduled)
212 (const :tag "deadline" 'deadline)
213 (const :tag "no deadline" 'notdeadline))))))
214 (list :tag "Non-standard skipping condition"
215 :value (org-agenda-skip-function)
216 (const org-agenda-skip-function)
217 (sexp :tag "Function or form (quoted!)"))))
218 "Selection of examples for agenda command settings.
219 This will be spliced into the custom type of
220 `org-agenda-custom-commands'.")
223 (defcustom org-agenda-custom-commands nil
224 "Custom commands for the agenda.
225 These commands will be offered on the splash screen displayed by the
226 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
228 (key desc type match settings files)
230 key The key (one or more characters as a string) to be associated
231 with the command.
232 desc A description of the command, when omitted or nil, a default
233 description is built using MATCH.
234 type The command type, any of the following symbols:
235 agenda The daily/weekly agenda.
236 todo Entries with a specific TODO keyword, in all agenda files.
237 search Entries containing search words entry or headline.
238 tags Tags/Property/TODO match in all agenda files.
239 tags-todo Tags/P/T match in all agenda files, TODO entries only.
240 todo-tree Sparse tree of specific TODO keyword in *current* file.
241 tags-tree Sparse tree with all tags matches in *current* file.
242 occur-tree Occur sparse tree for *current* file.
243 ... A user-defined function.
244 match What to search for:
245 - a single keyword for TODO keyword searches
246 - a tags match expression for tags searches
247 - a word search expression for text searches.
248 - a regular expression for occur searches
249 For all other commands, this should be the empty string.
250 settings A list of option settings, similar to that in a let form, so like
251 this: ((opt1 val1) (opt2 val2) ...). The values will be
252 evaluated at the moment of execution, so quote them when needed.
253 files A list of files file to write the produced agenda buffer to
254 with the command `org-store-agenda-views'.
255 If a file name ends in \".html\", an HTML version of the buffer
256 is written out. If it ends in \".ps\", a postscript version is
257 produced. Otherwide, only the plain text is written to the file.
259 You can also define a set of commands, to create a composite agenda buffer.
260 In this case, an entry looks like this:
262 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
264 where
266 desc A description string to be displayed in the dispatcher menu.
267 cmd An agenda command, similar to the above. However, tree commands
268 are no allowed, but instead you can get agenda and global todo list.
269 So valid commands for a set are:
270 (agenda \"\" settings)
271 (alltodo \"\" settings)
272 (stuck \"\" settings)
273 (todo \"match\" settings files)
274 (search \"match\" settings files)
275 (tags \"match\" settings files)
276 (tags-todo \"match\" settings files)
278 Each command can carry a list of options, and another set of options can be
279 given for the whole set of commands. Individual command options take
280 precedence over the general options.
282 When using several characters as key to a command, the first characters
283 are prefix commands. For the dispatcher to display useful information, you
284 should provide a description for the prefix, like
286 (setq org-agenda-custom-commands
287 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
288 (\"hl\" tags \"+HOME+Lisa\")
289 (\"hp\" tags \"+HOME+Peter\")
290 (\"hk\" tags \"+HOME+Kim\")))"
291 :group 'org-agenda-custom-commands
292 :type `(repeat
293 (choice :value ("x" "Describe command here" tags "" nil)
294 (list :tag "Single command"
295 (string :tag "Access Key(s) ")
296 (option (string :tag "Description"))
297 (choice
298 (const :tag "Agenda" agenda)
299 (const :tag "TODO list" alltodo)
300 (const :tag "Search words" search)
301 (const :tag "Stuck projects" stuck)
302 (const :tag "Tags search (all agenda files)" tags)
303 (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
304 (const :tag "TODO keyword search (all agenda files)" todo)
305 (const :tag "Tags sparse tree (current buffer)" tags-tree)
306 (const :tag "TODO keyword tree (current buffer)" todo-tree)
307 (const :tag "Occur tree (current buffer)" occur-tree)
308 (sexp :tag "Other, user-defined function"))
309 (string :tag "Match (only for some commands)")
310 ,org-agenda-custom-commands-local-options
311 (option (repeat :tag "Export" (file :tag "Export to"))))
312 (list :tag "Command series, all agenda files"
313 (string :tag "Access Key(s)")
314 (string :tag "Description ")
315 (repeat :tag "Component"
316 (choice
317 (list :tag "Agenda"
318 (const :format "" agenda)
319 (const :tag "" :format "" "")
320 ,org-agenda-custom-commands-local-options)
321 (list :tag "TODO list (all keywords)"
322 (const :format "" alltodo)
323 (const :tag "" :format "" "")
324 ,org-agenda-custom-commands-local-options)
325 (list :tag "Search words"
326 (const :format "" search)
327 (string :tag "Match")
328 ,org-agenda-custom-commands-local-options)
329 (list :tag "Stuck projects"
330 (const :format "" stuck)
331 (const :tag "" :format "" "")
332 ,org-agenda-custom-commands-local-options)
333 (list :tag "Tags search"
334 (const :format "" tags)
335 (string :tag "Match")
336 ,org-agenda-custom-commands-local-options)
337 (list :tag "Tags search, TODO entries only"
338 (const :format "" tags-todo)
339 (string :tag "Match")
340 ,org-agenda-custom-commands-local-options)
341 (list :tag "TODO keyword search"
342 (const :format "" todo)
343 (string :tag "Match")
344 ,org-agenda-custom-commands-local-options)
345 (list :tag "Other, user-defined function"
346 (symbol :tag "function")
347 (string :tag "Match")
348 ,org-agenda-custom-commands-local-options)))
350 (repeat :tag "Settings for entire command set"
351 (list (variable :tag "Any variable")
352 (sexp :tag "Value")))
353 (option (repeat :tag "Export" (file :tag "Export to"))))
354 (cons :tag "Prefix key documentation"
355 (string :tag "Access Key(s)")
356 (string :tag "Description ")))))
358 (defcustom org-agenda-query-register ?o
359 "The register holding the current query string.
360 The prupose of this is that if you construct a query string interactively,
361 you can then use it to define a custom command."
362 :group 'org-agenda-custom-commands
363 :type 'character)
365 (defcustom org-stuck-projects
366 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
367 "How to identify stuck projects.
368 This is a list of four items:
369 1. A tags/todo matcher string that is used to identify a project.
370 The entire tree below a headline matched by this is considered one project.
371 2. A list of TODO keywords identifying non-stuck projects.
372 If the project subtree contains any headline with one of these todo
373 keywords, the project is considered to be not stuck. If you specify
374 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
375 3. A list of tags identifying non-stuck projects.
376 If the project subtree contains any headline with one of these tags,
377 the project is considered to be not stuck. If you specify \"*\" as
378 a tag, any tag will mark the project unstuck.
379 4. An arbitrary regular expression matching non-stuck projects.
381 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
382 or `C-c a #' to produce the list."
383 :group 'org-agenda-custom-commands
384 :type '(list
385 (string :tag "Tags/TODO match to identify a project")
386 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
387 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
388 (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
390 (defcustom org-agenda-filter-effort-default-operator "<"
391 "The default operator for effort estimate filtering.
392 If you select an effort estimate limit with first pressing an operator,
393 this one will be used."
394 :group 'org-agenda-custom-commands
395 :type '(choice (const :tag "less or equal" "<")
396 (const :tag "greater or equal"">")
397 (const :tag "equal" "=")))
399 (defgroup org-agenda-skip nil
400 "Options concerning skipping parts of agenda files."
401 :tag "Org Agenda Skip"
402 :group 'org-agenda)
404 (defvar org-agenda-archives-mode nil
405 "Non-nil means, the agenda will include archived items.
406 If this is the symbol `trees', trees in the selected agenda scope
407 that are marked with the ARCHIVE tag will be included anyway. When this is
408 t, also all archive files associated with the current selection of agenda
409 files will be included.")
411 (defcustom org-agenda-skip-comment-trees t
412 "Non-nil means, skip trees that start with 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 (if org-agenda-show-log
2430 (setq rtn (org-agenda-get-day-entries
2431 file date
2432 :deadline :scheduled :timestamp :sexp :closed))
2433 (setq rtn (org-agenda-get-day-entries
2434 file date
2435 :deadline :scheduled :sexp :timestamp)))
2436 (setq rtnall (append rtnall rtn))))
2437 (if org-agenda-include-diary
2438 (progn
2439 (require 'diary-lib)
2440 (setq rtn (org-get-entries-from-diary date))
2441 (setq rtnall (append rtnall rtn))))
2442 (if (or rtnall org-agenda-show-all-dates)
2443 (progn
2444 (setq day-cnt (1+ day-cnt))
2445 (insert
2446 (if (stringp org-agenda-format-date)
2447 (format-time-string org-agenda-format-date
2448 (org-time-from-absolute date))
2449 (funcall org-agenda-format-date date))
2450 "\n")
2451 (put-text-property s (1- (point)) 'face
2452 (if (member wd org-agenda-weekend-days)
2453 'org-agenda-date-weekend
2454 'org-agenda-date))
2455 (put-text-property s (1- (point)) 'org-date-line t)
2456 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
2457 (if todayp (put-text-property s (1- (point)) 'org-today t))
2458 (if rtnall (insert
2459 (org-finalize-agenda-entries
2460 (org-agenda-add-time-grid-maybe
2461 rtnall nd todayp))
2462 "\n"))
2463 (put-text-property s (1- (point)) 'day d)
2464 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
2465 (when (and org-agenda-clockreport-mode clocktable-start)
2466 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
2467 ;; the above line is to ensure the restricted range!
2468 (p org-agenda-clockreport-parameter-plist)
2469 tbl)
2470 (setq p (org-plist-delete p :block))
2471 (setq p (plist-put p :tstart clocktable-start))
2472 (setq p (plist-put p :tend clocktable-end))
2473 (setq p (plist-put p :scope 'agenda))
2474 (setq tbl (apply 'org-get-clocktable p))
2475 (insert tbl)))
2476 (goto-char (point-min))
2477 (org-fit-agenda-window)
2478 (unless (and (pos-visible-in-window-p (point-min))
2479 (pos-visible-in-window-p (point-max)))
2480 (goto-char (1- (point-max)))
2481 (recenter -1)
2482 (if (not (pos-visible-in-window-p (or start-pos 1)))
2483 (progn
2484 (goto-char (or start-pos 1))
2485 (recenter 1))))
2486 (goto-char (or start-pos 1))
2487 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
2488 (org-finalize-agenda)
2489 (setq buffer-read-only t)
2490 (message "")))
2492 (defun org-agenda-ndays-to-span (n)
2493 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
2495 ;;; Agenda word search
2497 (defvar org-agenda-search-history nil)
2498 (defvar org-todo-only nil)
2500 (defvar org-search-syntax-table nil
2501 "Special syntax table for org-mode search.
2502 In this table, we have single quotes not as word constituents, to
2503 that when \"+Ameli\" is searchd as a work, it will also match \"Ameli's\"")
2505 (defun org-search-syntax-table ()
2506 (unless org-search-syntax-table
2507 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
2508 (modify-syntax-entry ?' "." org-search-syntax-table)
2509 (modify-syntax-entry ?` "." org-search-syntax-table))
2510 org-search-syntax-table)
2512 ;;;###autoload
2513 (defun org-search-view (&optional todo-only string edit-at)
2514 "Show all entries that contain words or regular expressions.
2515 If the first character of the search string is an asterisks,
2516 search only the headlines.
2518 With optional prefix argument TODO-ONLY, only consider entries that are
2519 TODO entries. The argument STRING can be used to pass a default search
2520 string into this function. If EDIT-AT is non-nil, it means that the
2521 user should get a chance to edit this string, with cursor at position
2522 EDIT-AT.
2524 The search string is broken into \"words\" by splitting at whitespace.
2525 The individual words are then interpreted as a boolean expression with
2526 logical AND. Words prefixed with a minus must not occur in the entry.
2527 Words without a prefix or prefixed with a plus must occur in the entry.
2528 Matching is case-insensitive and the words are enclosed by word delimiters.
2530 Words enclosed by curly braces are interpreted as regular expressions
2531 that must or must not match in the entry.
2533 If the search string starts with an asterisk, search only in headlines.
2534 If (possibly after the leading star) the search string starts with an
2535 exclamation mark, this also means to look at TODO entries only, an effect
2536 that can also be achieved with a prefix argument.
2538 This command searches the agenda files, and in addition the files listed
2539 in `org-agenda-text-search-extra-files'."
2540 (interactive "P")
2541 (org-compile-prefix-format 'search)
2542 (org-set-sorting-strategy 'search)
2543 (org-prepare-agenda "SEARCH")
2544 (let* ((props (list 'face nil
2545 'done-face 'org-done
2546 'org-not-done-regexp org-not-done-regexp
2547 'org-todo-regexp org-todo-regexp
2548 'org-complex-heading-regexp org-complex-heading-regexp
2549 'mouse-face 'highlight
2550 'keymap org-agenda-keymap
2551 'help-echo (format "mouse-2 or RET jump to location")))
2552 regexp rtn rtnall files file pos
2553 marker priority category tags c neg re
2554 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
2555 (unless (and (not edit-at)
2556 (stringp string)
2557 (string-match "\\S-" string))
2558 (setq string (read-string "[+-]Word/{Regexp} ...: "
2559 (cond
2560 ((integerp edit-at) (cons string edit-at))
2561 (edit-at string))
2562 'org-agenda-search-history)))
2563 (org-set-local 'org-todo-only todo-only)
2564 (setq org-agenda-redo-command
2565 (list 'org-search-view (if todo-only t nil) string
2566 '(if current-prefix-arg 1 nil)))
2567 (setq org-agenda-query-string string)
2569 (if (equal (string-to-char string) ?*)
2570 (setq hdl-only t
2571 words (substring string 1))
2572 (setq words string))
2573 (when (equal (string-to-char words) ?!)
2574 (setq todo-only t
2575 words (substring words 1)))
2576 (setq words (org-split-string words))
2577 (mapc (lambda (w)
2578 (setq c (string-to-char w))
2579 (if (equal c ?-)
2580 (setq neg t w (substring w 1))
2581 (if (equal c ?+)
2582 (setq neg nil w (substring w 1))
2583 (setq neg nil)))
2584 (if (string-match "\\`{.*}\\'" w)
2585 (setq re (substring w 1 -1))
2586 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
2587 (if neg (push re regexps-) (push re regexps+)))
2588 words)
2589 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
2590 (if (not regexps+)
2591 (setq regexp (concat "^" org-outline-regexp))
2592 (setq regexp (pop regexps+))
2593 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
2594 regexp))))
2595 (setq files (org-agenda-files nil 'ifmode))
2596 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
2597 (pop org-agenda-text-search-extra-files)
2598 (setq files (org-add-archive-files files)))
2599 (setq files (append files org-agenda-text-search-extra-files)
2600 rtnall nil)
2601 (while (setq file (pop files))
2602 (setq ee nil)
2603 (catch 'nextfile
2604 (org-check-agenda-file file)
2605 (setq buffer (if (file-exists-p file)
2606 (org-get-agenda-file-buffer file)
2607 (error "No such file %s" file)))
2608 (if (not buffer)
2609 ;; If file does not exist, make sure an error message is sent
2610 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
2611 file))))
2612 (with-current-buffer buffer
2613 (with-syntax-table (org-search-syntax-table)
2614 (unless (org-mode-p)
2615 (error "Agenda file %s is not in `org-mode'" file))
2616 (let ((case-fold-search t))
2617 (save-excursion
2618 (save-restriction
2619 (if org-agenda-restrict
2620 (narrow-to-region org-agenda-restrict-begin
2621 org-agenda-restrict-end)
2622 (widen))
2623 (goto-char (point-min))
2624 (unless (or (org-on-heading-p)
2625 (outline-next-heading))
2626 (throw 'nextfile t))
2627 (goto-char (max (point-min) (1- (point))))
2628 (while (re-search-forward regexp nil t)
2629 (org-back-to-heading t)
2630 (skip-chars-forward "* ")
2631 (setq beg (point-at-bol)
2632 beg1 (point)
2633 end (progn (outline-next-heading) (point)))
2634 (catch :skip
2635 (goto-char beg)
2636 (org-agenda-skip)
2637 (setq str (buffer-substring-no-properties
2638 (point-at-bol)
2639 (if hdl-only (point-at-eol) end)))
2640 (mapc (lambda (wr) (when (string-match wr str)
2641 (goto-char (1- end))
2642 (throw :skip t)))
2643 regexps-)
2644 (mapc (lambda (wr) (unless (string-match wr str)
2645 (goto-char (1- end))
2646 (throw :skip t)))
2647 (if todo-only
2648 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
2649 regexps+)
2650 regexps+))
2651 (goto-char beg)
2652 (setq marker (org-agenda-new-marker (point))
2653 category (org-get-category)
2654 tags (org-get-tags-at (point))
2655 txt (org-format-agenda-item
2657 (buffer-substring-no-properties
2658 beg1 (point-at-eol))
2659 category tags))
2660 (org-add-props txt props
2661 'org-marker marker 'org-hd-marker marker
2662 'org-todo-regexp org-todo-regexp
2663 'org-complex-heading-regexp org-complex-heading-regexp
2664 'priority 1000 'org-category category
2665 'type "search")
2666 (push txt ee)
2667 (goto-char (1- end))))))))))
2668 (setq rtn (nreverse ee))
2669 (setq rtnall (append rtnall rtn)))
2670 (if org-agenda-overriding-header
2671 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2672 nil 'face 'org-agenda-structure) "\n")
2673 (insert "Search words: ")
2674 (add-text-properties (point-min) (1- (point))
2675 (list 'face 'org-agenda-structure))
2676 (setq pos (point))
2677 (insert string "\n")
2678 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2679 (setq pos (point))
2680 (unless org-agenda-multi
2681 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
2682 (add-text-properties pos (1- (point))
2683 (list 'face 'org-agenda-structure))))
2684 (when rtnall
2685 (insert (org-finalize-agenda-entries rtnall) "\n"))
2686 (goto-char (point-min))
2687 (org-fit-agenda-window)
2688 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
2689 (org-finalize-agenda)
2690 (setq buffer-read-only t)))
2692 ;;; Agenda TODO list
2694 (defvar org-select-this-todo-keyword nil)
2695 (defvar org-last-arg nil)
2697 ;;;###autoload
2698 (defun org-todo-list (arg)
2699 "Show all TODO entries from all agenda file in a single list.
2700 The prefix arg can be used to select a specific TODO keyword and limit
2701 the list to these. When using \\[universal-argument], you will be prompted
2702 for a keyword. A numeric prefix directly selects the Nth keyword in
2703 `org-todo-keywords-1'."
2704 (interactive "P")
2705 (require 'calendar)
2706 (org-compile-prefix-format 'todo)
2707 (org-set-sorting-strategy 'todo)
2708 (org-prepare-agenda "TODO")
2709 (let* ((today (time-to-days (current-time)))
2710 (date (calendar-gregorian-from-absolute today))
2711 (kwds org-todo-keywords-for-agenda)
2712 (completion-ignore-case t)
2713 (org-select-this-todo-keyword
2714 (if (stringp arg) arg
2715 (and arg (integerp arg) (> arg 0)
2716 (nth (1- arg) kwds))))
2717 rtn rtnall files file pos)
2718 (when (equal arg '(4))
2719 (setq org-select-this-todo-keyword
2720 (completing-read "Keyword (or KWD1|K2D2|...): "
2721 (mapcar 'list kwds) nil nil)))
2722 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
2723 (org-set-local 'org-last-arg arg)
2724 (setq org-agenda-redo-command
2725 '(org-todo-list (or current-prefix-arg org-last-arg)))
2726 (setq files (org-agenda-files nil 'ifmode)
2727 rtnall nil)
2728 (while (setq file (pop files))
2729 (catch 'nextfile
2730 (org-check-agenda-file file)
2731 (setq rtn (org-agenda-get-day-entries file date :todo))
2732 (setq rtnall (append rtnall rtn))))
2733 (if org-agenda-overriding-header
2734 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2735 nil 'face 'org-agenda-structure) "\n")
2736 (insert "Global list of TODO items of type: ")
2737 (add-text-properties (point-min) (1- (point))
2738 (list 'face 'org-agenda-structure))
2739 (setq pos (point))
2740 (insert (or org-select-this-todo-keyword "ALL") "\n")
2741 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2742 (setq pos (point))
2743 (unless org-agenda-multi
2744 (insert "Available with `N r': (0)ALL")
2745 (let ((n 0) s)
2746 (mapc (lambda (x)
2747 (setq s (format "(%d)%s" (setq n (1+ n)) x))
2748 (if (> (+ (current-column) (string-width s) 1) (frame-width))
2749 (insert "\n "))
2750 (insert " " s))
2751 kwds))
2752 (insert "\n"))
2753 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
2754 (when rtnall
2755 (insert (org-finalize-agenda-entries rtnall) "\n"))
2756 (goto-char (point-min))
2757 (org-fit-agenda-window)
2758 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
2759 (org-finalize-agenda)
2760 (setq buffer-read-only t)))
2762 ;;; Agenda tags match
2764 ;;;###autoload
2765 (defun org-tags-view (&optional todo-only match)
2766 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
2767 The prefix arg TODO-ONLY limits the search to TODO entries."
2768 (interactive "P")
2769 (org-compile-prefix-format 'tags)
2770 (org-set-sorting-strategy 'tags)
2771 (let* ((org-tags-match-list-sublevels
2772 (if todo-only t org-tags-match-list-sublevels))
2773 (completion-ignore-case t)
2774 rtn rtnall files file pos matcher
2775 buffer)
2776 (setq matcher (org-make-tags-matcher match)
2777 match (car matcher) matcher (cdr matcher))
2778 (org-prepare-agenda (concat "TAGS " match))
2779 (setq org-agenda-query-string match)
2780 (setq org-agenda-redo-command
2781 (list 'org-tags-view (list 'quote todo-only)
2782 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2783 (setq files (org-agenda-files nil 'ifmode)
2784 rtnall nil)
2785 (while (setq file (pop files))
2786 (catch 'nextfile
2787 (org-check-agenda-file file)
2788 (setq buffer (if (file-exists-p file)
2789 (org-get-agenda-file-buffer file)
2790 (error "No such file %s" file)))
2791 (if (not buffer)
2792 ;; If file does not exist, merror message to agenda
2793 (setq rtn (list
2794 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
2795 rtnall (append rtnall rtn))
2796 (with-current-buffer buffer
2797 (unless (org-mode-p)
2798 (error "Agenda file %s is not in `org-mode'" file))
2799 (save-excursion
2800 (save-restriction
2801 (if org-agenda-restrict
2802 (narrow-to-region org-agenda-restrict-begin
2803 org-agenda-restrict-end)
2804 (widen))
2805 (setq rtn (org-scan-tags 'agenda matcher todo-only))
2806 (setq rtnall (append rtnall rtn))))))))
2807 (if org-agenda-overriding-header
2808 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2809 nil 'face 'org-agenda-structure) "\n")
2810 (insert "Headlines with TAGS match: ")
2811 (add-text-properties (point-min) (1- (point))
2812 (list 'face 'org-agenda-structure))
2813 (setq pos (point))
2814 (insert match "\n")
2815 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2816 (setq pos (point))
2817 (unless org-agenda-multi
2818 (insert "Press `C-u r' to search again with new search string\n"))
2819 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
2820 (when rtnall
2821 (insert (org-finalize-agenda-entries rtnall) "\n"))
2822 (goto-char (point-min))
2823 (org-fit-agenda-window)
2824 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
2825 (org-finalize-agenda)
2826 (setq buffer-read-only t)))
2828 ;;; Agenda Finding stuck projects
2830 (defvar org-agenda-skip-regexp nil
2831 "Regular expression used in skipping subtrees for the agenda.
2832 This is basically a temporary global variable that can be set and then
2833 used by user-defined selections using `org-agenda-skip-function'.")
2835 (defvar org-agenda-overriding-header nil
2836 "When this is set during todo and tags searches, will replace header.")
2838 (defun org-agenda-skip-subtree-when-regexp-matches ()
2839 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
2840 If yes, it returns the end position of this tree, causing agenda commands
2841 to skip this subtree. This is a function that can be put into
2842 `org-agenda-skip-function' for the duration of a command."
2843 (let ((end (save-excursion (org-end-of-subtree t)))
2844 skip)
2845 (save-excursion
2846 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
2847 (and skip end)))
2849 (defun org-agenda-skip-entry-if (&rest conditions)
2850 "Skip entry if any of CONDITIONS is true.
2851 See `org-agenda-skip-if' for details."
2852 (org-agenda-skip-if nil conditions))
2854 (defun org-agenda-skip-subtree-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 t conditions))
2859 (defun org-agenda-skip-if (subtree conditions)
2860 "Checks current entity for CONDITIONS.
2861 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
2862 the entry, i.e. the text before the next heading is checked.
2864 CONDITIONS is a list of symbols, boolean OR is used to combine the results
2865 from different tests. Valid conditions are:
2867 scheduled Check if there is a scheduled cookie
2868 notscheduled Check if there is no scheduled cookie
2869 deadline Check if there is a deadline
2870 notdeadline Check if there is no deadline
2871 regexp Check if regexp matches
2872 notregexp Check if regexp does not match.
2874 The regexp is taken from the conditions list, it must come right after
2875 the `regexp' or `notregexp' element.
2877 If any of these conditions is met, this function returns the end point of
2878 the entity, causing the search to continue from there. This is a function
2879 that can be put into `org-agenda-skip-function' for the duration of a command."
2880 (let (beg end m)
2881 (org-back-to-heading t)
2882 (setq beg (point)
2883 end (if subtree
2884 (progn (org-end-of-subtree t) (point))
2885 (progn (outline-next-heading) (1- (point)))))
2886 (goto-char beg)
2887 (and
2889 (and (memq 'scheduled conditions)
2890 (re-search-forward org-scheduled-time-regexp end t))
2891 (and (memq 'notscheduled conditions)
2892 (not (re-search-forward org-scheduled-time-regexp end t)))
2893 (and (memq 'deadline conditions)
2894 (re-search-forward org-deadline-time-regexp end t))
2895 (and (memq 'notdeadline conditions)
2896 (not (re-search-forward org-deadline-time-regexp end t)))
2897 (and (setq m (memq 'regexp conditions))
2898 (stringp (nth 1 m))
2899 (re-search-forward (nth 1 m) end t))
2900 (and (setq m (memq 'notregexp conditions))
2901 (stringp (nth 1 m))
2902 (not (re-search-forward (nth 1 m) end t))))
2903 end)))
2905 ;;;###autoload
2906 (defun org-agenda-list-stuck-projects (&rest ignore)
2907 "Create agenda view for projects that are stuck.
2908 Stuck projects are project that have no next actions. For the definitions
2909 of what a project is and how to check if it stuck, customize the variable
2910 `org-stuck-projects'.
2911 MATCH is being ignored."
2912 (interactive)
2913 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
2914 ;; We could have used org-agenda-skip-if here.
2915 (org-agenda-overriding-header "List of stuck projects: ")
2916 (matcher (nth 0 org-stuck-projects))
2917 (todo (nth 1 org-stuck-projects))
2918 (todo-wds (if (member "*" todo)
2919 (progn
2920 (org-prepare-agenda-buffers (org-agenda-files
2921 nil 'ifmode))
2922 (org-delete-all
2923 org-done-keywords-for-agenda
2924 (copy-sequence org-todo-keywords-for-agenda)))
2925 todo))
2926 (todo-re (concat "^\\*+[ \t]+\\("
2927 (mapconcat 'identity todo-wds "\\|")
2928 "\\)\\>"))
2929 (tags (nth 2 org-stuck-projects))
2930 (tags-re (if (member "*" tags)
2931 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
2932 (concat "^\\*+ .*:\\("
2933 (mapconcat 'identity tags "\\|")
2934 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
2935 (gen-re (nth 3 org-stuck-projects))
2936 (re-list
2937 (delq nil
2938 (list
2939 (if todo todo-re)
2940 (if tags tags-re)
2941 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
2942 gen-re)))))
2943 (setq org-agenda-skip-regexp
2944 (if re-list
2945 (mapconcat 'identity re-list "\\|")
2946 (error "No information how to identify unstuck projects")))
2947 (org-tags-view nil matcher)
2948 (with-current-buffer org-agenda-buffer-name
2949 (setq org-agenda-redo-command
2950 '(org-agenda-list-stuck-projects
2951 (or current-prefix-arg org-last-arg))))))
2953 ;;; Diary integration
2955 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
2956 (defvar list-diary-entries-hook)
2958 (defun org-get-entries-from-diary (date)
2959 "Get the (Emacs Calendar) diary entries for DATE."
2960 (require 'diary-lib)
2961 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
2962 (fancy-diary-buffer diary-fancy-buffer)
2963 (diary-display-hook '(fancy-diary-display))
2964 (diary-display-function 'fancy-diary-display)
2965 (pop-up-frames nil)
2966 (list-diary-entries-hook
2967 (cons 'org-diary-default-entry list-diary-entries-hook))
2968 (diary-file-name-prefix-function nil) ; turn this feature off
2969 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
2970 entries
2971 (org-disable-agenda-to-diary t))
2972 (save-excursion
2973 (save-window-excursion
2974 (funcall (if (fboundp 'diary-list-entries)
2975 'diary-list-entries 'list-diary-entries)
2976 date 1)))
2977 (if (not (get-buffer diary-fancy-buffer))
2978 (setq entries nil)
2979 (with-current-buffer diary-fancy-buffer
2980 (setq buffer-read-only nil)
2981 (if (zerop (buffer-size))
2982 ;; No entries
2983 (setq entries nil)
2984 ;; Omit the date and other unnecessary stuff
2985 (org-agenda-cleanup-fancy-diary)
2986 ;; Add prefix to each line and extend the text properties
2987 (if (zerop (buffer-size))
2988 (setq entries nil)
2989 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
2990 (set-buffer-modified-p nil)
2991 (kill-buffer diary-fancy-buffer)))
2992 (when entries
2993 (setq entries (org-split-string entries "\n"))
2994 (setq entries
2995 (mapcar
2996 (lambda (x)
2997 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
2998 ;; Extend the text properties to the beginning of the line
2999 (org-add-props x (text-properties-at (1- (length x)) x)
3000 'type "diary" 'date date))
3001 entries)))))
3003 (defun org-agenda-cleanup-fancy-diary ()
3004 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3005 This gets rid of the date, the underline under the date, and
3006 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3007 date. It also removes lines that contain only whitespace."
3008 (goto-char (point-min))
3009 (if (looking-at ".*?:[ \t]*")
3010 (progn
3011 (replace-match "")
3012 (re-search-forward "\n=+$" nil t)
3013 (replace-match "")
3014 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3015 (re-search-forward "\n=+$" nil t)
3016 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3017 (goto-char (point-min))
3018 (while (re-search-forward "^ +\n" nil t)
3019 (replace-match ""))
3020 (goto-char (point-min))
3021 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3022 (replace-match "")))
3024 ;; Make sure entries from the diary have the right text properties.
3025 (eval-after-load "diary-lib"
3026 '(if (boundp 'diary-modify-entry-list-string-function)
3027 ;; We can rely on the hook, nothing to do
3029 ;; Hook not avaiable, must use advice to make this work
3030 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3031 "Make the position visible."
3032 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3033 (stringp string)
3034 buffer-file-name)
3035 (setq string (org-modify-diary-entry-string string))))))
3037 (defun org-modify-diary-entry-string (string)
3038 "Add text properties to string, allowing org-mode to act on it."
3039 (org-add-props string nil
3040 'mouse-face 'highlight
3041 'keymap org-agenda-keymap
3042 'help-echo (if buffer-file-name
3043 (format "mouse-2 or RET jump to diary file %s"
3044 (abbreviate-file-name buffer-file-name))
3046 'org-agenda-diary-link t
3047 'org-marker (org-agenda-new-marker (point-at-bol))))
3049 (defun org-diary-default-entry ()
3050 "Add a dummy entry to the diary.
3051 Needed to avoid empty dates which mess up holiday display."
3052 ;; Catch the error if dealing with the new add-to-diary-alist
3053 (when org-disable-agenda-to-diary
3054 (condition-case nil
3055 (org-add-to-diary-list original-date "Org-mode dummy" "")
3056 (error
3057 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3059 (defun org-add-to-diary-list (&rest args)
3060 (if (fboundp 'diary-add-to-list)
3061 (apply 'diary-add-to-list args)
3062 (apply 'add-to-diary-list args)))
3064 ;;;###autoload
3065 (defun org-diary (&rest args)
3066 "Return diary information from org-files.
3067 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3068 It accesses org files and extracts information from those files to be
3069 listed in the diary. The function accepts arguments specifying what
3070 items should be listed. The following arguments are allowed:
3072 :timestamp List the headlines of items containing a date stamp or
3073 date range matching the selected date. Deadlines will
3074 also be listed, on the expiration day.
3076 :sexp List entries resulting from diary-like sexps.
3078 :deadline List any deadlines past due, or due within
3079 `org-deadline-warning-days'. The listing occurs only
3080 in the diary for *today*, not at any other date. If
3081 an entry is marked DONE, it is no longer listed.
3083 :scheduled List all items which are scheduled for the given date.
3084 The diary for *today* also contains items which were
3085 scheduled earlier and are not yet marked DONE.
3087 :todo List all TODO items from the org-file. This may be a
3088 long list - so this is not turned on by default.
3089 Like deadlines, these entries only show up in the
3090 diary for *today*, not at any other date.
3092 The call in the diary file should look like this:
3094 &%%(org-diary) ~/path/to/some/orgfile.org
3096 Use a separate line for each org file to check. Or, if you omit the file name,
3097 all files listed in `org-agenda-files' will be checked automatically:
3099 &%%(org-diary)
3101 If you don't give any arguments (as in the example above), the default
3102 arguments (:deadline :scheduled :timestamp :sexp) are used.
3103 So the example above may also be written as
3105 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3107 The function expects the lisp variables `entry' and `date' to be provided
3108 by the caller, because this is how the calendar works. Don't use this
3109 function from a program - use `org-agenda-get-day-entries' instead."
3110 (when (> (- (time-to-seconds (current-time))
3111 org-agenda-last-marker-time)
3113 (org-agenda-reset-markers))
3114 (org-compile-prefix-format 'agenda)
3115 (org-set-sorting-strategy 'agenda)
3116 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3117 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3118 (list entry)
3119 (org-agenda-files t)))
3120 file rtn results)
3121 (org-prepare-agenda-buffers files)
3122 ;; If this is called during org-agenda, don't return any entries to
3123 ;; the calendar. Org Agenda will list these entries itself.
3124 (if org-disable-agenda-to-diary (setq files nil))
3125 (while (setq file (pop files))
3126 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3127 (setq results (append results rtn)))
3128 (if results
3129 (concat (org-finalize-agenda-entries results) "\n"))))
3131 ;;; Agenda entry finders
3133 (defun org-agenda-get-day-entries (file date &rest args)
3134 "Does the work for `org-diary' and `org-agenda'.
3135 FILE is the path to a file to be checked for entries. DATE is date like
3136 the one returned by `calendar-current-date'. ARGS are symbols indicating
3137 which kind of entries should be extracted. For details about these, see
3138 the documentation of `org-diary'."
3139 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3140 (let* ((org-startup-folded nil)
3141 (org-startup-align-all-tables nil)
3142 (buffer (if (file-exists-p file)
3143 (org-get-agenda-file-buffer file)
3144 (error "No such file %s" file)))
3145 arg results rtn)
3146 (if (not buffer)
3147 ;; If file does not exist, make sure an error message ends up in diary
3148 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3149 (with-current-buffer buffer
3150 (unless (org-mode-p)
3151 (error "Agenda file %s is not in `org-mode'" file))
3152 (let ((case-fold-search nil))
3153 (save-excursion
3154 (save-restriction
3155 (if org-agenda-restrict
3156 (narrow-to-region org-agenda-restrict-begin
3157 org-agenda-restrict-end)
3158 (widen))
3159 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3160 (while (setq arg (pop args))
3161 (cond
3162 ((and (eq arg :todo)
3163 (equal date (calendar-current-date)))
3164 (setq rtn (org-agenda-get-todos))
3165 (setq results (append results rtn)))
3166 ((eq arg :timestamp)
3167 (setq rtn (org-agenda-get-blocks))
3168 (setq results (append results rtn))
3169 (setq rtn (org-agenda-get-timestamps))
3170 (setq results (append results rtn)))
3171 ((eq arg :sexp)
3172 (setq rtn (org-agenda-get-sexps))
3173 (setq results (append results rtn)))
3174 ((eq arg :scheduled)
3175 (setq rtn (org-agenda-get-scheduled))
3176 (setq results (append results rtn)))
3177 ((eq arg :closed)
3178 (setq rtn (org-agenda-get-progress))
3179 (setq results (append results rtn)))
3180 ((eq arg :deadline)
3181 (setq rtn (org-agenda-get-deadlines))
3182 (setq results (append results rtn))))))))
3183 results))))
3185 (defun org-agenda-get-todos ()
3186 "Return the TODO information for agenda display."
3187 (let* ((props (list 'face nil
3188 'done-face 'org-done
3189 'org-not-done-regexp org-not-done-regexp
3190 'org-todo-regexp org-todo-regexp
3191 'org-complex-heading-regexp org-complex-heading-regexp
3192 'mouse-face 'highlight
3193 'keymap org-agenda-keymap
3194 'help-echo
3195 (format "mouse-2 or RET jump to org file %s"
3196 (abbreviate-file-name buffer-file-name))))
3197 (regexp (concat "^\\*+[ \t]+\\("
3198 (if org-select-this-todo-keyword
3199 (if (equal org-select-this-todo-keyword "*")
3200 org-todo-regexp
3201 (concat "\\<\\("
3202 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3203 "\\)\\>"))
3204 org-not-done-regexp)
3205 "[^\n\r]*\\)"))
3206 marker priority category tags todo-state
3207 ee txt beg end)
3208 (goto-char (point-min))
3209 (while (re-search-forward regexp nil t)
3210 (catch :skip
3211 (save-match-data
3212 (beginning-of-line)
3213 (setq beg (point) end (progn (outline-next-heading) (point)))
3214 (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
3215 (re-search-forward org-ts-regexp end t))
3216 (and org-agenda-todo-ignore-scheduled (goto-char beg)
3217 (re-search-forward org-scheduled-time-regexp end t))
3218 (and org-agenda-todo-ignore-deadlines (goto-char beg)
3219 (re-search-forward org-deadline-time-regexp end t)
3220 (org-deadline-close (match-string 1))))
3221 (goto-char (1+ beg))
3222 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3223 (throw :skip nil)))
3224 (goto-char beg)
3225 (org-agenda-skip)
3226 (goto-char (match-beginning 1))
3227 (setq marker (org-agenda-new-marker (match-beginning 0))
3228 category (org-get-category)
3229 tags (org-get-tags-at (point))
3230 txt (org-format-agenda-item "" (match-string 1) category tags)
3231 priority (1+ (org-get-priority txt))
3232 todo-state (org-get-todo-state))
3233 (org-add-props txt props
3234 'org-marker marker 'org-hd-marker marker
3235 'priority priority 'org-category category
3236 'type "todo" 'todo-state todo-state)
3237 (push txt ee)
3238 (if org-agenda-todo-list-sublevels
3239 (goto-char (match-end 1))
3240 (org-end-of-subtree 'invisible))))
3241 (nreverse ee)))
3243 (defconst org-agenda-no-heading-message
3244 "No heading for this item in buffer or region.")
3246 (defun org-agenda-get-timestamps ()
3247 "Return the date stamp information for agenda display."
3248 (let* ((props (list 'face nil
3249 'org-not-done-regexp org-not-done-regexp
3250 'org-todo-regexp org-todo-regexp
3251 'org-complex-heading-regexp org-complex-heading-regexp
3252 'mouse-face 'highlight
3253 'keymap org-agenda-keymap
3254 'help-echo
3255 (format "mouse-2 or RET jump to org file %s"
3256 (abbreviate-file-name buffer-file-name))))
3257 (d1 (calendar-absolute-from-gregorian date))
3258 (remove-re
3259 (concat
3260 (regexp-quote
3261 (format-time-string
3262 "<%Y-%m-%d"
3263 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3264 ".*?>"))
3265 (regexp
3266 (concat
3267 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3268 (regexp-quote
3269 (substring
3270 (format-time-string
3271 (car org-time-stamp-formats)
3272 (apply 'encode-time ; DATE bound by calendar
3273 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3274 1 11))
3275 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3276 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
3277 marker hdmarker deadlinep scheduledp clockp closedp inactivep
3278 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3279 todo-state)
3280 (goto-char (point-min))
3281 (while (re-search-forward regexp nil t)
3282 (setq b0 (match-beginning 0)
3283 b3 (match-beginning 3) e3 (match-end 3))
3284 (catch :skip
3285 (and (org-at-date-range-p) (throw :skip nil))
3286 (org-agenda-skip)
3287 (if (and (match-end 1)
3288 (not (= d1 (org-time-string-to-absolute
3289 (match-string 1) d1 nil
3290 org-agenda-repeating-timestamp-show-all))))
3291 (throw :skip nil))
3292 (if (and e3
3293 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
3294 (throw :skip nil))
3295 (setq marker (org-agenda-new-marker b0)
3296 category (org-get-category b0)
3297 tmp (buffer-substring (max (point-min)
3298 (- b0 org-ds-keyword-length))
3300 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
3301 inactivep (= (char-after b0) ?\[)
3302 deadlinep (string-match org-deadline-regexp tmp)
3303 scheduledp (string-match org-scheduled-regexp tmp)
3304 closedp (and org-agenda-include-inactive-timestamps
3305 (string-match org-closed-string tmp))
3306 clockp (and org-agenda-include-inactive-timestamps
3307 (or (string-match org-clock-string tmp)
3308 (string-match "]-+\\'" tmp)))
3309 todo-state (org-get-todo-state)
3310 donep (member todo-state org-done-keywords))
3311 (if (or scheduledp deadlinep closedp clockp)
3312 (throw :skip t))
3313 (if (string-match ">" timestr)
3314 ;; substring should only run to end of time stamp
3315 (setq timestr (substring timestr 0 (match-end 0))))
3316 (save-excursion
3317 (if (re-search-backward "^\\*+ " nil t)
3318 (progn
3319 (goto-char (match-beginning 0))
3320 (setq hdmarker (org-agenda-new-marker)
3321 tags (org-get-tags-at))
3322 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3323 (setq head (match-string 1))
3324 (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
3325 (setq txt (org-format-agenda-item
3326 (if inactivep "[" nil)
3327 head category tags timestr nil
3328 remove-re)))
3329 (setq txt org-agenda-no-heading-message))
3330 (setq priority (org-get-priority txt))
3331 (org-add-props txt props
3332 'org-marker marker 'org-hd-marker hdmarker)
3333 (org-add-props txt nil 'priority priority
3334 'org-category category 'date date
3335 'todo-state todo-state
3336 'type "timestamp")
3337 (push txt ee))
3338 (outline-next-heading)))
3339 (nreverse ee)))
3341 (defun org-agenda-get-sexps ()
3342 "Return the sexp information for agenda display."
3343 (require 'diary-lib)
3344 (let* ((props (list 'face nil
3345 'mouse-face 'highlight
3346 'keymap org-agenda-keymap
3347 'help-echo
3348 (format "mouse-2 or RET jump to org file %s"
3349 (abbreviate-file-name buffer-file-name))))
3350 (regexp "^&?%%(")
3351 marker category ee txt tags entry result beg b sexp sexp-entry)
3352 (goto-char (point-min))
3353 (while (re-search-forward regexp nil t)
3354 (catch :skip
3355 (org-agenda-skip)
3356 (setq beg (match-beginning 0))
3357 (goto-char (1- (match-end 0)))
3358 (setq b (point))
3359 (forward-sexp 1)
3360 (setq sexp (buffer-substring b (point)))
3361 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
3362 (org-trim (match-string 1))
3363 ""))
3364 (setq result (org-diary-sexp-entry sexp sexp-entry date))
3365 (when result
3366 (setq marker (org-agenda-new-marker beg)
3367 category (org-get-category beg))
3369 (if (string-match "\\S-" result)
3370 (setq txt result)
3371 (setq txt "SEXP entry returned empty string"))
3373 (setq txt (org-format-agenda-item
3374 "" txt category tags 'time))
3375 (org-add-props txt props 'org-marker marker)
3376 (org-add-props txt nil
3377 'org-category category 'date date
3378 'type "sexp")
3379 (push txt ee))))
3380 (nreverse ee)))
3382 (defalias 'org-get-closed 'org-get-progress)
3383 (defun org-agenda-get-progress ()
3384 "Return the logged TODO entries for agenda display."
3385 (let* ((props (list 'mouse-face 'highlight
3386 'org-not-done-regexp org-not-done-regexp
3387 'org-todo-regexp org-todo-regexp
3388 'org-complex-heading-regexp org-complex-heading-regexp
3389 'keymap org-agenda-keymap
3390 'help-echo
3391 (format "mouse-2 or RET jump to org file %s"
3392 (abbreviate-file-name buffer-file-name))))
3393 (items (if (consp org-agenda-show-log)
3394 org-agenda-show-log
3395 org-agenda-log-mode-items))
3396 (parts
3397 (delq nil
3398 (list
3399 (if (memq 'closed items) (concat "\\<" org-closed-string))
3400 (if (memq 'clock items) (concat "\\<" org-clock-string))
3401 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\""))))
3402 (parts-re (if parts (mapconcat 'identity parts "\\|")
3403 (error "`org-agenda-log-mode-items' is empty")))
3404 (regexp (concat
3405 "\\(" parts-re "\\)"
3406 " *\\["
3407 (regexp-quote
3408 (substring
3409 (format-time-string
3410 (car org-time-stamp-formats)
3411 (apply 'encode-time ; DATE bound by calendar
3412 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3413 1 11))))
3414 marker hdmarker priority category tags closedp statep state
3415 ee txt timestr rest clocked)
3416 (goto-char (point-min))
3417 (while (re-search-forward regexp nil t)
3418 (catch :skip
3419 (org-agenda-skip)
3420 (setq marker (org-agenda-new-marker (match-beginning 0))
3421 closedp (equal (match-string 1) org-closed-string)
3422 statep (equal (string-to-char (match-string 1)) ?-)
3423 state (and statep (match-string 2))
3424 category (org-get-category (match-beginning 0))
3425 timestr (buffer-substring (match-beginning 0) (point-at-eol))
3426 ;; donep (org-entry-is-done-p)
3428 (when (string-match "\\]" timestr)
3429 ;; substring should only run to end of time stamp
3430 (setq rest (substring timestr (match-end 0))
3431 timestr (substring timestr 0 (match-end 0)))
3432 (if (and (not closedp) (not statep)
3433 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
3434 (progn (setq timestr (concat (substring timestr 0 -1)
3435 "-" (match-string 1 rest) "]"))
3436 (setq clocked (match-string 2 rest)))
3437 (setq clocked "-")))
3438 (save-excursion
3439 (if (re-search-backward "^\\*+ " nil t)
3440 (progn
3441 (goto-char (match-beginning 0))
3442 (setq hdmarker (org-agenda-new-marker)
3443 tags (org-get-tags-at))
3444 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3445 (setq txt (org-format-agenda-item
3446 (cond
3447 (closedp "Closed: ")
3448 (statep (concat "State: (" state ")"))
3449 (t (concat "Clocked: (" clocked ")")))
3450 (match-string 1) category tags timestr)))
3451 (setq txt org-agenda-no-heading-message))
3452 (setq priority 100000)
3453 (org-add-props txt props
3454 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
3455 'priority priority 'org-category category
3456 'type "closed" 'date date
3457 'undone-face 'org-warning 'done-face 'org-done)
3458 (push txt ee))
3459 (goto-char (point-at-eol))))
3460 (nreverse ee)))
3462 (defun org-agenda-get-deadlines ()
3463 "Return the deadline information for agenda display."
3464 (let* ((props (list 'mouse-face 'highlight
3465 'org-not-done-regexp org-not-done-regexp
3466 'org-todo-regexp org-todo-regexp
3467 'org-complex-heading-regexp org-complex-heading-regexp
3468 'keymap org-agenda-keymap
3469 'help-echo
3470 (format "mouse-2 or RET jump to org file %s"
3471 (abbreviate-file-name buffer-file-name))))
3472 (regexp org-deadline-time-regexp)
3473 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3474 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3475 d2 diff dfrac wdays pos pos1 category tags
3476 ee txt head face s todo-state upcomingp donep timestr)
3477 (goto-char (point-min))
3478 (while (re-search-forward regexp nil t)
3479 (catch :skip
3480 (org-agenda-skip)
3481 (setq s (match-string 1)
3482 pos (1- (match-beginning 1))
3483 d2 (org-time-string-to-absolute
3484 (match-string 1) d1 'past
3485 org-agenda-repeating-timestamp-show-all)
3486 diff (- d2 d1)
3487 wdays (org-get-wdays s)
3488 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
3489 upcomingp (and todayp (> diff 0)))
3490 ;; When to show a deadline in the calendar:
3491 ;; If the expiration is within wdays warning time.
3492 ;; Past-due deadlines are only shown on the current date
3493 (if (or (and (<= diff wdays)
3494 (and todayp (not org-agenda-only-exact-dates)))
3495 (= diff 0))
3496 (save-excursion
3497 (setq category (org-get-category))
3498 (setq todo-state (org-get-todo-state))
3499 (if (re-search-backward "^\\*+[ \t]+" nil t)
3500 (progn
3501 (goto-char (match-end 0))
3502 (setq pos1 (match-beginning 0))
3503 (setq tags (org-get-tags-at pos1))
3504 (setq head (buffer-substring-no-properties
3505 (point)
3506 (progn (skip-chars-forward "^\r\n")
3507 (point))))
3508 (setq donep (member todo-state org-done-keywords))
3509 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3510 (setq timestr
3511 (concat (substring s (match-beginning 1)) " "))
3512 (setq timestr 'time))
3513 (if (and donep
3514 (or org-agenda-skip-deadline-if-done
3515 (not (= diff 0))))
3516 (setq txt nil)
3517 (setq txt (org-format-agenda-item
3518 (if (= diff 0)
3519 (car org-agenda-deadline-leaders)
3520 (if (functionp (nth 1 org-agenda-deadline-leaders))
3521 (funcall (nth 1 org-agenda-deadline-leaders) diff date)
3522 (format (nth 1 org-agenda-deadline-leaders)
3523 diff)))
3524 head category tags
3525 (if (not (= diff 0)) nil timestr)))))
3526 (setq txt org-agenda-no-heading-message))
3527 (when txt
3528 (setq face (org-agenda-deadline-face dfrac wdays))
3529 (org-add-props txt props
3530 'org-marker (org-agenda-new-marker pos)
3531 'org-hd-marker (org-agenda-new-marker pos1)
3532 'priority (+ (- diff)
3533 (org-get-priority txt))
3534 'org-category category
3535 'todo-state todo-state
3536 'type (if upcomingp "upcoming-deadline" "deadline")
3537 'date (if upcomingp date d2)
3538 'face (if donep 'org-done face)
3539 'undone-face face 'done-face 'org-done)
3540 (push txt ee))))))
3541 (nreverse ee)))
3543 (defun org-agenda-deadline-face (fraction &optional wdays)
3544 "Return the face to displaying a deadline item.
3545 FRACTION is what fraction of the head-warning time has passed."
3546 (if (equal wdays 0) (setq fraction 1.))
3547 (let ((faces org-agenda-deadline-faces) f)
3548 (catch 'exit
3549 (while (setq f (pop faces))
3550 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
3552 (defun org-agenda-get-scheduled ()
3553 "Return the scheduled information for agenda display."
3554 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
3555 'org-todo-regexp org-todo-regexp
3556 'org-complex-heading-regexp org-complex-heading-regexp
3557 'done-face 'org-done
3558 'mouse-face 'highlight
3559 'keymap org-agenda-keymap
3560 'help-echo
3561 (format "mouse-2 or RET jump to org file %s"
3562 (abbreviate-file-name buffer-file-name))))
3563 (regexp org-scheduled-time-regexp)
3564 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3565 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3566 d2 diff pos pos1 category tags donep
3567 ee txt head pastschedp todo-state face timestr s)
3568 (goto-char (point-min))
3569 (while (re-search-forward regexp nil t)
3570 (catch :skip
3571 (org-agenda-skip)
3572 (setq s (match-string 1)
3573 pos (1- (match-beginning 1))
3574 d2 (org-time-string-to-absolute
3575 (match-string 1) d1 'past
3576 org-agenda-repeating-timestamp-show-all)
3577 diff (- d2 d1))
3578 (setq pastschedp (and todayp (< diff 0)))
3579 ;; When to show a scheduled item in the calendar:
3580 ;; If it is on or past the date.
3581 (if (or (and (< diff 0)
3582 (< (abs diff) org-scheduled-past-days)
3583 (and todayp (not org-agenda-only-exact-dates)))
3584 (= diff 0))
3585 (save-excursion
3586 (setq category (org-get-category))
3587 (setq todo-state (org-get-todo-state))
3588 (if (re-search-backward "^\\*+[ \t]+" nil t)
3589 (progn
3590 (goto-char (match-end 0))
3591 (setq pos1 (match-beginning 0))
3592 (setq tags (org-get-tags-at))
3593 (setq head (buffer-substring-no-properties
3594 (point)
3595 (progn (skip-chars-forward "^\r\n") (point))))
3596 (setq donep (member todo-state org-done-keywords))
3597 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3598 (setq timestr
3599 (concat (substring s (match-beginning 1)) " "))
3600 (setq timestr 'time))
3601 (if (and donep
3602 (or org-agenda-skip-scheduled-if-done
3603 (not (= diff 0))))
3604 (setq txt nil)
3605 (setq txt (org-format-agenda-item
3606 (if (= diff 0)
3607 (car org-agenda-scheduled-leaders)
3608 (format (nth 1 org-agenda-scheduled-leaders)
3609 (- 1 diff)))
3610 head category tags
3611 (if (not (= diff 0)) nil timestr)))))
3612 (setq txt org-agenda-no-heading-message))
3613 (when txt
3614 (setq face
3615 (cond
3616 (pastschedp 'org-scheduled-previously)
3617 (todayp 'org-scheduled-today)
3618 (t 'org-scheduled)))
3619 (org-add-props txt props
3620 'undone-face face
3621 'face (if donep 'org-done face)
3622 'org-marker (org-agenda-new-marker pos)
3623 'org-hd-marker (org-agenda-new-marker pos1)
3624 'type (if pastschedp "past-scheduled" "scheduled")
3625 'date (if pastschedp d2 date)
3626 'priority (+ 94 (- 5 diff) (org-get-priority txt))
3627 'org-category category
3628 'todo-state todo-state)
3629 (push txt ee))))))
3630 (nreverse ee)))
3632 (defun org-agenda-get-blocks ()
3633 "Return the date-range information for agenda display."
3634 (let* ((props (list 'face nil
3635 'org-not-done-regexp org-not-done-regexp
3636 'org-todo-regexp org-todo-regexp
3637 'org-complex-heading-regexp org-complex-heading-regexp
3638 'mouse-face 'highlight
3639 'keymap org-agenda-keymap
3640 'help-echo
3641 (format "mouse-2 or RET jump to org file %s"
3642 (abbreviate-file-name buffer-file-name))))
3643 (regexp org-tr-regexp)
3644 (d0 (calendar-absolute-from-gregorian date))
3645 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
3646 donep head)
3647 (goto-char (point-min))
3648 (while (re-search-forward regexp nil t)
3649 (catch :skip
3650 (org-agenda-skip)
3651 (setq pos (point))
3652 (setq timestr (match-string 0)
3653 s1 (match-string 1)
3654 s2 (match-string 2)
3655 d1 (time-to-days (org-time-string-to-time s1))
3656 d2 (time-to-days (org-time-string-to-time s2)))
3657 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
3658 ;; Only allow days between the limits, because the normal
3659 ;; date stamps will catch the limits.
3660 (save-excursion
3661 (setq marker (org-agenda-new-marker (point)))
3662 (setq category (org-get-category))
3663 (setq todo-state (org-get-todo-state))
3664 (if (re-search-backward "^\\*+ " nil t)
3665 (progn
3666 (goto-char (match-beginning 0))
3667 (setq hdmarker (org-agenda-new-marker (point)))
3668 (setq tags (org-get-tags-at))
3669 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3670 (setq head (match-string 1))
3671 (and org-agenda-skip-timestamp-if-done
3672 (org-entry-is-done-p)
3673 (throw :skip t))
3674 (setq txt (org-format-agenda-item
3675 (format (if (= d1 d2) "" "(%d/%d): ")
3676 (1+ (- d0 d1)) (1+ (- d2 d1)))
3677 head category tags
3678 (if (= d0 d1) timestr))))
3679 (setq txt org-agenda-no-heading-message))
3680 (org-add-props txt props
3681 'org-marker marker 'org-hd-marker hdmarker
3682 'type "block" 'date date
3683 'todo-state todo-state
3684 'priority (org-get-priority txt) 'org-category category)
3685 (push txt ee)))
3686 (goto-char pos)))
3687 ;; Sort the entries by expiration date.
3688 (nreverse ee)))
3690 ;;; Agenda presentation and sorting
3692 (defvar org-prefix-has-time nil
3693 "A flag, set by `org-compile-prefix-format'.
3694 The flag is set if the currently compiled format contains a `%t'.")
3695 (defvar org-prefix-has-tag nil
3696 "A flag, set by `org-compile-prefix-format'.
3697 The flag is set if the currently compiled format contains a `%T'.")
3698 (defvar org-prefix-has-effort nil
3699 "A flag, set by `org-compile-prefix-format'.
3700 The flag is set if the currently compiled format contains a `%e'.")
3702 (defun org-format-agenda-item (extra txt &optional category tags dotime
3703 noprefix remove-re)
3704 "Format TXT to be inserted into the agenda buffer.
3705 In particular, it adds the prefix and corresponding text properties. EXTRA
3706 must be a string and replaces the `%s' specifier in the prefix format.
3707 CATEGORY (string, symbol or nil) may be used to overrule the default
3708 category taken from local variable or file name. It will replace the `%c'
3709 specifier in the format. DOTIME, when non-nil, indicates that a
3710 time-of-day should be extracted from TXT for sorting of this entry, and for
3711 the `%t' specifier in the format. When DOTIME is a string, this string is
3712 searched for a time before TXT is. NOPREFIX is a flag and indicates that
3713 only the correctly processes TXT should be returned - this is used by
3714 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
3715 Any match of REMOVE-RE will be removed from TXT."
3716 (save-match-data
3717 ;; Diary entries sometimes have extra whitespace at the beginning
3718 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
3719 (let* ((category (or category
3720 org-category
3721 (if buffer-file-name
3722 (file-name-sans-extension
3723 (file-name-nondirectory buffer-file-name))
3724 "")))
3725 ;; time, tag, effort are needed for the eval of the prefix format
3726 (tag (if tags (nth (1- (length tags)) tags) ""))
3727 time effort neffort
3728 (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
3729 (time-of-day (and dotime (org-get-time-of-day ts)))
3730 stamp plain s0 s1 s2 t1 t2 rtn srp
3731 duration)
3732 (and (org-mode-p) buffer-file-name
3733 (add-to-list 'org-agenda-contributing-files buffer-file-name))
3734 (when (and dotime time-of-day)
3735 ;; Extract starting and ending time and move them to prefix
3736 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
3737 (setq plain (string-match org-plain-time-of-day-regexp ts)))
3738 (setq s0 (match-string 0 ts)
3739 srp (and stamp (match-end 3))
3740 s1 (match-string (if plain 1 2) ts)
3741 s2 (match-string (if plain 8 (if srp 4 6)) ts))
3743 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
3744 ;; them, we might want to remove them there to avoid duplication.
3745 ;; The user can turn this off with a variable.
3746 (if (and org-prefix-has-time
3747 org-agenda-remove-times-when-in-prefix (or stamp plain)
3748 (string-match (concat (regexp-quote s0) " *") txt)
3749 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
3750 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
3751 (= (match-beginning 0) 0)
3753 (setq txt (replace-match "" nil nil txt))))
3754 ;; Normalize the time(s) to 24 hour
3755 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
3756 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
3757 ;; Compute the duration
3758 (when s1
3759 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
3760 (string-to-number (substring s1 3)))
3761 t2 (cond
3762 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
3763 (string-to-number (substring s2 3))))
3764 (org-agenda-default-appointment-duration
3765 (+ t1 org-agenda-default-appointment-duration))
3766 (t nil)))
3767 (setq duration (if t2 (- t2 t1)))))
3769 (when (and s1 (not s2) org-agenda-default-appointment-duration
3770 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
3771 (let ((m (+ (string-to-number (match-string 2 s1))
3772 (* 60 (string-to-number (match-string 1 s1)))
3773 org-agenda-default-appointment-duration))
3775 (setq h (/ m 60) m (- m (* h 60)))
3776 (setq s2 (format "%02d:%02d" h m))))
3778 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
3779 txt)
3780 ;; Tags are in the string
3781 (if (or (eq org-agenda-remove-tags t)
3782 (and org-agenda-remove-tags
3783 org-prefix-has-tag))
3784 (setq txt (replace-match "" t t txt))
3785 (setq txt (replace-match
3786 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
3787 (match-string 2 txt))
3788 t t txt))))
3789 (when (org-mode-p)
3790 (setq effort
3791 (condition-case nil
3792 (org-get-effort
3793 (or (get-text-property 0 'org-hd-marker txt)
3794 (get-text-property 0 'org-marker txt)))
3795 (error nil)))
3796 (when effort
3797 (setq neffort (org-hh:mm-string-to-minutes effort)
3798 effort (setq effort (concat "[" effort"]" )))))
3800 (when remove-re
3801 (while (string-match remove-re txt)
3802 (setq txt (replace-match "" t t txt))))
3804 ;; Create the final string
3805 (if noprefix
3806 (setq rtn txt)
3807 ;; Prepare the variables needed in the eval of the compiled format
3808 (setq time (cond (s2 (concat s1 "-" s2))
3809 (s1 (concat s1 "......"))
3810 (t ""))
3811 extra (or extra "")
3812 category (if (symbolp category) (symbol-name category) category))
3813 ;; Evaluate the compiled format
3814 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
3816 ;; And finally add the text properties
3817 (org-add-props rtn nil
3818 'org-category (downcase category) 'tags (mapcar 'downcase tags)
3819 'org-highest-priority org-highest-priority
3820 'org-lowest-priority org-lowest-priority
3821 'prefix-length (- (length rtn) (length txt))
3822 'time-of-day time-of-day
3823 'duration duration
3824 'effort effort
3825 'effort-minutes neffort
3826 'txt txt
3827 'time time
3828 'extra extra
3829 'dotime dotime))))
3831 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
3832 (defvar org-agenda-sorting-strategy-selected nil)
3834 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
3835 (catch 'exit
3836 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
3837 ((and todayp (member 'today (car org-agenda-time-grid))))
3838 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
3839 ((member 'weekly (car org-agenda-time-grid)))
3840 (t (throw 'exit list)))
3841 (let* ((have (delq nil (mapcar
3842 (lambda (x) (get-text-property 1 'time-of-day x))
3843 list)))
3844 (string (nth 1 org-agenda-time-grid))
3845 (gridtimes (nth 2 org-agenda-time-grid))
3846 (req (car org-agenda-time-grid))
3847 (remove (member 'remove-match req))
3848 new time)
3849 (if (and (member 'require-timed req) (not have))
3850 ;; don't show empty grid
3851 (throw 'exit list))
3852 (while (setq time (pop gridtimes))
3853 (unless (and remove (member time have))
3854 (setq time (int-to-string time))
3855 (push (org-format-agenda-item
3856 nil string "" nil
3857 (concat (substring time 0 -2) ":" (substring time -2)))
3858 new)
3859 (put-text-property
3860 1 (length (car new)) 'face 'org-time-grid (car new))))
3861 (if (member 'time-up org-agenda-sorting-strategy-selected)
3862 (append new list)
3863 (append list new)))))
3865 (defun org-compile-prefix-format (key)
3866 "Compile the prefix format into a Lisp form that can be evaluated.
3867 The resulting form is returned and stored in the variable
3868 `org-prefix-format-compiled'."
3869 (setq org-prefix-has-time nil org-prefix-has-tag nil
3870 org-prefix-has-effort nil)
3871 (let ((s (cond
3872 ((stringp org-agenda-prefix-format)
3873 org-agenda-prefix-format)
3874 ((assq key org-agenda-prefix-format)
3875 (cdr (assq key org-agenda-prefix-format)))
3876 (t " %-12:c%?-12t% s")))
3877 (start 0)
3878 varform vars var e c f opt)
3879 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
3880 s start)
3881 (setq var (cdr (assoc (match-string 4 s)
3882 '(("c" . category) ("t" . time) ("s" . extra)
3883 ("T" . tag) ("e" . effort))))
3884 c (or (match-string 3 s) "")
3885 opt (match-beginning 1)
3886 start (1+ (match-beginning 0)))
3887 (if (equal var 'time) (setq org-prefix-has-time t))
3888 (if (equal var 'tag) (setq org-prefix-has-tag t))
3889 (if (equal var 'effort) (setq org-prefix-has-effort t))
3890 (setq f (concat "%" (match-string 2 s) "s"))
3891 (if opt
3892 (setq varform
3893 `(if (equal "" ,var)
3895 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
3896 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
3897 (setq s (replace-match "%s" t nil s))
3898 (push varform vars))
3899 (setq vars (nreverse vars))
3900 (setq org-prefix-format-compiled `(format ,s ,@vars))))
3902 (defun org-set-sorting-strategy (key)
3903 (if (symbolp (car org-agenda-sorting-strategy))
3904 ;; the old format
3905 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
3906 (setq org-agenda-sorting-strategy-selected
3907 (or (cdr (assq key org-agenda-sorting-strategy))
3908 (cdr (assq 'agenda org-agenda-sorting-strategy))
3909 '(time-up category-keep priority-down)))))
3911 (defun org-get-time-of-day (s &optional string mod24)
3912 "Check string S for a time of day.
3913 If found, return it as a military time number between 0 and 2400.
3914 If not found, return nil.
3915 The optional STRING argument forces conversion into a 5 character wide string
3916 HH:MM."
3917 (save-match-data
3918 (when
3919 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
3920 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
3921 (let* ((h (string-to-number (match-string 1 s)))
3922 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
3923 (ampm (if (match-end 4) (downcase (match-string 4 s))))
3924 (am-p (equal ampm "am"))
3925 (h1 (cond ((not ampm) h)
3926 ((= h 12) (if am-p 0 12))
3927 (t (+ h (if am-p 0 12)))))
3928 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
3929 (mod h1 24) h1))
3930 (t0 (+ (* 100 h2) m))
3931 (t1 (concat (if (>= h1 24) "+" " ")
3932 (if (< t0 100) "0" "")
3933 (if (< t0 10) "0" "")
3934 (int-to-string t0))))
3935 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
3937 (defun org-finalize-agenda-entries (list &optional nosort)
3938 "Sort and concatenate the agenda items."
3939 (setq list (mapcar 'org-agenda-highlight-todo list))
3940 (if nosort
3941 list
3942 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
3944 (defun org-agenda-highlight-todo (x)
3945 (let ((org-done-keywords org-done-keywords-for-agenda)
3946 re pl)
3947 (if (eq x 'line)
3948 (save-excursion
3949 (beginning-of-line 1)
3950 (setq re (get-text-property (point) 'org-todo-regexp))
3951 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
3952 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
3953 (add-text-properties (match-beginning 0) (match-end 0)
3954 (list 'face (org-get-todo-face 1)))
3955 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
3956 (delete-region (match-beginning 1) (1- (match-end 0)))
3957 (goto-char (match-beginning 1))
3958 (insert (format org-agenda-todo-keyword-format s)))))
3959 (setq re (concat (get-text-property 0 'org-todo-regexp x))
3960 pl (get-text-property 0 'prefix-length x))
3961 (when (and re
3962 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
3963 x (or pl 0)) pl))
3964 (add-text-properties
3965 (or (match-end 1) (match-end 0)) (match-end 0)
3966 (list 'face (org-get-todo-face (match-string 2 x)))
3968 (setq x (concat (substring x 0 (match-end 1))
3969 (format org-agenda-todo-keyword-format
3970 (match-string 2 x))
3972 (substring x (match-end 3)))))
3973 x)))
3975 (defsubst org-cmp-priority (a b)
3976 "Compare the priorities of string A and B."
3977 (let ((pa (or (get-text-property 1 'priority a) 0))
3978 (pb (or (get-text-property 1 'priority b) 0)))
3979 (cond ((> pa pb) +1)
3980 ((< pa pb) -1)
3981 (t nil))))
3983 (defsubst org-cmp-effort (a b)
3984 "Compare the priorities of string A and B."
3985 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
3986 (ea (or (get-text-property 1 'effort-minutes a) def))
3987 (eb (or (get-text-property 1 'effort-minutes b) def)))
3988 (cond ((> ea eb) +1)
3989 ((< ea eb) -1)
3990 (t nil))))
3992 (defsubst org-cmp-category (a b)
3993 "Compare the string values of categories of strings A and B."
3994 (let ((ca (or (get-text-property 1 'org-category a) ""))
3995 (cb (or (get-text-property 1 'org-category b) "")))
3996 (cond ((string-lessp ca cb) -1)
3997 ((string-lessp cb ca) +1)
3998 (t nil))))
4000 (defsubst org-cmp-todo-state (a b)
4001 "Compare the todo states of strings A and B."
4002 (let* ((ta (or (get-text-property 1 'todo-state a) ""))
4003 (tb (or (get-text-property 1 'todo-state b) ""))
4004 (la (- (length (member ta org-todo-keywords-for-agenda))))
4005 (lb (- (length (member tb org-todo-keywords-for-agenda))))
4006 (donepa (member ta org-done-keywords-for-agenda))
4007 (donepb (member tb org-done-keywords-for-agenda)))
4008 (cond ((and donepa (not donepb)) -1)
4009 ((and (not donepa) donepb) +1)
4010 ((< la lb) -1)
4011 ((< lb la) +1)
4012 (t nil))))
4014 (defsubst org-cmp-tag (a b)
4015 "Compare the string values of the first tags of A and B."
4016 (let ((ta (car (last (get-text-property 1 'tags a))))
4017 (tb (car (last (get-text-property 1 'tags b)))))
4018 (cond ((not ta) +1)
4019 ((not tb) -1)
4020 ((string-lessp ta tb) -1)
4021 ((string-lessp tb ta) +1)
4022 (t nil))))
4024 (defsubst org-cmp-time (a b)
4025 "Compare the time-of-day values of strings A and B."
4026 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4027 (ta (or (get-text-property 1 'time-of-day a) def))
4028 (tb (or (get-text-property 1 'time-of-day b) def)))
4029 (cond ((< ta tb) -1)
4030 ((< tb ta) +1)
4031 (t nil))))
4033 (defun org-entries-lessp (a b)
4034 "Predicate for sorting agenda entries."
4035 ;; The following variables will be used when the form is evaluated.
4036 ;; So even though the compiler complains, keep them.
4037 (let* ((time-up (org-cmp-time a b))
4038 (time-down (if time-up (- time-up) nil))
4039 (priority-up (org-cmp-priority a b))
4040 (priority-down (if priority-up (- priority-up) nil))
4041 (effort-up (org-cmp-effort a b))
4042 (effort-down (if effort-up (- effort-up) nil))
4043 (category-up (org-cmp-category a b))
4044 (category-down (if category-up (- category-up) nil))
4045 (category-keep (if category-up +1 nil))
4046 (tag-up (org-cmp-tag a b))
4047 (tag-down (if tag-up (- tag-up) nil))
4048 (todo-state-up (org-cmp-todo-state a b))
4049 (todo-state-down (if todo-state-up (- todo-state-up) nil)))
4050 (cdr (assoc
4051 (eval (cons 'or org-agenda-sorting-strategy-selected))
4052 '((-1 . t) (1 . nil) (nil . nil))))))
4054 ;;; Agenda restriction lock
4056 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4057 "Overlay to mark the headline to which arenda commands are restricted.")
4058 (org-overlay-put org-agenda-restriction-lock-overlay
4059 'face 'org-agenda-restriction-lock)
4060 (org-overlay-put org-agenda-restriction-lock-overlay
4061 'help-echo "Agendas are currently limited to this subtree.")
4062 (org-detach-overlay org-agenda-restriction-lock-overlay)
4064 (defun org-agenda-set-restriction-lock (&optional type)
4065 "Set restriction lock for agenda, to current subtree or file.
4066 Restriction will be the file if TYPE is `file', or if type is the
4067 universal prefix '(4), or if the cursor is before the first headline
4068 in the file. Otherwise, restriction will be to the current subtree."
4069 (interactive "P")
4070 (and (equal type '(4)) (setq type 'file))
4071 (setq type (cond
4072 (type type)
4073 ((org-at-heading-p) 'subtree)
4074 ((condition-case nil (org-back-to-heading t) (error nil))
4075 'subtree)
4076 (t 'file)))
4077 (if (eq type 'subtree)
4078 (progn
4079 (setq org-agenda-restrict t)
4080 (setq org-agenda-overriding-restriction 'subtree)
4081 (put 'org-agenda-files 'org-restrict
4082 (list (buffer-file-name (buffer-base-buffer))))
4083 (org-back-to-heading t)
4084 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4085 (move-marker org-agenda-restrict-begin (point))
4086 (move-marker org-agenda-restrict-end
4087 (save-excursion (org-end-of-subtree t)))
4088 (message "Locking agenda restriction to subtree"))
4089 (put 'org-agenda-files 'org-restrict
4090 (list (buffer-file-name (buffer-base-buffer))))
4091 (setq org-agenda-restrict nil)
4092 (setq org-agenda-overriding-restriction 'file)
4093 (move-marker org-agenda-restrict-begin nil)
4094 (move-marker org-agenda-restrict-end nil)
4095 (message "Locking agenda restriction to file"))
4096 (setq current-prefix-arg nil)
4097 (org-agenda-maybe-redo))
4099 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4100 "Remove the agenda restriction lock."
4101 (interactive "P")
4102 (org-detach-overlay org-agenda-restriction-lock-overlay)
4103 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4104 (setq org-agenda-overriding-restriction nil)
4105 (setq org-agenda-restrict nil)
4106 (put 'org-agenda-files 'org-restrict nil)
4107 (move-marker org-agenda-restrict-begin nil)
4108 (move-marker org-agenda-restrict-end nil)
4109 (setq current-prefix-arg nil)
4110 (message "Agenda restriction lock removed")
4111 (or noupdate (org-agenda-maybe-redo)))
4113 (defun org-agenda-maybe-redo ()
4114 "If there is any window showing the agenda view, update it."
4115 (let ((w (get-buffer-window org-agenda-buffer-name t))
4116 (w0 (selected-window)))
4117 (when w
4118 (select-window w)
4119 (org-agenda-redo)
4120 (select-window w0)
4121 (if org-agenda-overriding-restriction
4122 (message "Agenda view shifted to new %s restriction"
4123 org-agenda-overriding-restriction)
4124 (message "Agenda restriction lock removed")))))
4126 ;;; Agenda commands
4128 (defun org-agenda-check-type (error &rest types)
4129 "Check if agenda buffer is of allowed type.
4130 If ERROR is non-nil, throw an error, otherwise just return nil."
4131 (if (memq org-agenda-type types)
4133 (if error
4134 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
4135 nil)))
4137 (defun org-agenda-quit ()
4138 "Exit agenda by removing the window or the buffer."
4139 (interactive)
4140 (if org-agenda-columns-active
4141 (org-columns-quit)
4142 (let ((buf (current-buffer)))
4143 (if (not (one-window-p)) (delete-window))
4144 (kill-buffer buf)
4145 (org-agenda-reset-markers)
4146 (org-columns-remove-overlays)
4147 (setq org-agenda-archives-mode nil))
4148 ;; Maybe restore the pre-agenda window configuration.
4149 (and org-agenda-restore-windows-after-quit
4150 (not (eq org-agenda-window-setup 'other-frame))
4151 org-pre-agenda-window-conf
4152 (set-window-configuration org-pre-agenda-window-conf))))
4154 (defun org-agenda-exit ()
4155 "Exit agenda by removing the window or the buffer.
4156 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
4157 Org-mode buffers visited directly by the user will not be touched."
4158 (interactive)
4159 (org-release-buffers org-agenda-new-buffers)
4160 (setq org-agenda-new-buffers nil)
4161 (org-agenda-quit))
4163 (defun org-agenda-execute (arg)
4164 "Execute another agenda command, keeping same window.\\<global-map>
4165 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
4166 (interactive "P")
4167 (let ((org-agenda-window-setup 'current-window))
4168 (org-agenda arg)))
4170 (defun org-save-all-org-buffers ()
4171 "Save all Org-mode buffers without user confirmation."
4172 (interactive)
4173 (message "Saving all Org-mode buffers...")
4174 (save-some-buffers t 'org-mode-p)
4175 (message "Saving all Org-mode buffers... done"))
4177 (defun org-agenda-redo ()
4178 "Rebuild Agenda.
4179 When this is the global TODO list, a prefix argument will be interpreted."
4180 (interactive)
4181 (let* ((org-agenda-keep-modes t)
4182 (filter org-agenda-filter)
4183 (cols org-agenda-columns-active)
4184 (line (org-current-line))
4185 (window-line (- line (org-current-line (window-start))))
4186 (lprops (get 'org-agenda-redo-command 'org-lprops)))
4187 (and cols (org-columns-quit))
4188 (message "Rebuilding agenda buffer...")
4189 (org-let lprops '(eval org-agenda-redo-command))
4190 (setq org-agenda-undo-list nil
4191 org-agenda-pending-undo-list nil)
4192 (message "Rebuilding agenda buffer...done")
4193 (and filter (org-agenda-filter-apply filter))
4194 (and cols (interactive-p) (org-agenda-columns))
4195 (goto-line line)
4196 (recenter window-line)))
4199 (defvar org-global-tags-completion-table nil)
4200 (defvar org-agenda-filter-form nil)
4201 (defun org-agenda-filter-by-tag (strip &optional char narrow)
4202 "Keep only those lines in the agenda buffer that have a specific tag.
4203 The tag is selected with its fast selection letter, as configured.
4204 With prefix argument STRIP, remove all lines that do have the tag.
4205 A lisp caller can specify CHAR. NARROW means that the new tag should be
4206 used to narrow the search - the interactive user can also press `-' or `+'
4207 to switch to narrowing."
4208 (interactive "P")
4209 (let* ((alist org-tag-alist-for-agenda)
4210 (tag-chars (mapconcat
4211 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
4212 alist ""))
4213 (efforts (org-split-string
4214 (or (cdr (assoc (concat org-effort-property "_ALL")
4215 org-global-properties))
4216 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
4217 (effort-op org-agenda-filter-effort-default-operator)
4218 (effort-prompt "")
4219 (inhibit-read-only t)
4220 (current org-agenda-filter)
4221 char a n tag tags)
4222 (unless char
4223 (message
4224 "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<]:effort: "
4225 (if narrow "Narrow" "Filter") tag-chars)
4226 (setq char (read-char)))
4227 (when (member char '(?+ ?-))
4228 ;; Narrowing down
4229 (cond ((equal char ?-) (setq strip t narrow t))
4230 ((equal char ?+) (setq strip nil narrow t)))
4231 (message
4232 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
4233 (setq char (read-char)))
4234 (when (member char '(?< ?> ?=))
4235 ;; An effort operator
4236 (setq effort-op (char-to-string char))
4237 (loop for i from 0 to 9 do
4238 (setq effort-prompt
4239 (concat
4240 effort-prompt " ["
4241 (if (= i 9) "0" (int-to-string (1+ i)))
4242 "]" (nth i efforts))))
4243 (setq alist nil) ; to make sure it will be interpreted as effort.
4244 (message "Effort%s: %s " effort-op effort-prompt)
4245 (setq char (read-char))
4246 (when (or (< char ?0) (> char ?9))
4247 (error "Need 1-9,0 to select effort" )))
4248 (when (equal char ?\t)
4249 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
4250 (org-set-local 'org-global-tags-completion-table
4251 (org-global-tags-completion-table)))
4252 (let ((completion-ignore-case t))
4253 (setq tag (completing-read
4254 "Tag: " org-global-tags-completion-table))))
4255 (cond
4256 ((equal char ?/) (org-agenda-filter-by-tag-show-all))
4257 ((or (equal char ?\ )
4258 (setq a (rassoc char alist))
4259 (and (>= char ?0) (<= char ?9)
4260 (setq n (if (= char ?0) 9 (- char ?0 1))
4261 tag (concat effort-op (nth n efforts))
4262 a (cons tag nil)))
4263 (and tag (setq a (cons tag nil))))
4264 (org-agenda-filter-by-tag-show-all)
4265 (setq tag (car a))
4266 (setq org-agenda-filter
4267 (cons (concat (if strip "-" "+") tag)
4268 (if narrow current nil)))
4269 (org-agenda-filter-apply org-agenda-filter))
4270 (t (error "Invalid tag selection character %c" char)))))
4272 (defun org-agenda-filter-by-tag-refine (strip &optional char)
4273 "Refine the current filter. See `org-agenda-filter-by-tag."
4274 (interactive "P")
4275 (org-agenda-filter-by-tag strip char 'refine))
4277 (defun org-agenda-filter-make-matcher ()
4278 "Create the form that tests a line for the agenda filter."
4279 (let (f f1)
4280 (dolist (x org-agenda-filter)
4281 (if (member x '("-" "+"))
4282 (setq f1 '(not tags))
4283 (if (string-match "[<=>]" x)
4284 (setq f1 (org-agenda-filter-effort-form x))
4285 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
4286 (if (equal (string-to-char x) ?-)
4287 (setq f1 (list 'not f1))))
4288 (push f1 f))
4289 (cons 'and (nreverse f))))
4291 (defun org-agenda-filter-effort-form (e)
4292 "Return the form to compare the effort of the current line with what E says.
4293 E looks line \"+<2:25\"."
4294 (let (op)
4295 (setq e (substring e 1))
4296 (setq op (string-to-char e) e (substring e 1))
4297 (setq op (if (equal op ?<) '<= (if (equal op ?>) '>= '=)))
4298 (list 'org-agenda-compare-effort (list 'quote op)
4299 (org-hh:mm-string-to-minutes e))))
4301 (defun org-agenda-compare-effort (op value)
4302 "Compare the effort of the current line with VALUE, using OP.
4303 If the line does not have an effort defined, return nil."
4304 (let ((eff (get-text-property (point) 'effort-minutes)))
4305 (if (not eff)
4306 nil ; we don't have an effort defined
4307 (funcall op eff value))))
4309 (defun org-agenda-filter-apply (filter)
4310 "Set FILTER as the new agenda filter and apply it."
4311 (let (tags)
4312 (setq org-agenda-filter filter
4313 org-agenda-filter-form (org-agenda-filter-make-matcher))
4314 (org-agenda-set-mode-name)
4315 (save-excursion
4316 (goto-char (point-min))
4317 (while (not (eobp))
4318 (if (get-text-property (point) 'org-marker)
4319 (progn
4320 (setq tags (get-text-property (point) 'tags))
4321 (if (not (eval org-agenda-filter-form))
4322 (org-agenda-filter-by-tag-hide-line))
4323 (beginning-of-line 2))
4324 (beginning-of-line 2))))))
4326 (defun org-agenda-filter-by-tag-hide-line ()
4327 (let (ov)
4328 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
4329 (point-at-eol)))
4330 (org-overlay-put ov 'invisible t)
4331 (org-overlay-put ov 'type 'tags-filter)
4332 (push ov org-agenda-filter-overlays)))
4334 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
4335 (setq pos (or pos (point)))
4336 (save-excursion
4337 (dolist (ov (org-overlays-at pos))
4338 (when (and (org-overlay-get ov 'invisible)
4339 (eq (org-overlay-get ov 'type) 'tags-filter))
4340 (goto-char pos)
4341 (if (< (org-overlay-start ov) (point-at-eol))
4342 (org-move-overlay ov (point-at-eol)
4343 (org-overlay-end ov)))))))
4345 (defun org-agenda-filter-by-tag-show-all ()
4346 (mapc 'org-delete-overlay org-agenda-filter-overlays)
4347 (setq org-agenda-filter-overlays nil)
4348 (setq org-agenda-filter nil)
4349 (setq org-agenda-filter-form nil)
4350 (org-agenda-set-mode-name))
4352 (defun org-agenda-manipulate-query-add ()
4353 "Manipulate the query by adding a search term with positive selection.
4354 Positive selection means, the term must be matched for selection of an entry."
4355 (interactive)
4356 (org-agenda-manipulate-query ?\[))
4357 (defun org-agenda-manipulate-query-subtract ()
4358 "Manipulate the query by adding a search term with negative selection.
4359 Negative selection means, term must not be matched for selection of an entry."
4360 (interactive)
4361 (org-agenda-manipulate-query ?\]))
4362 (defun org-agenda-manipulate-query-add-re ()
4363 "Manipulate the query by adding a search regexp with positive selection.
4364 Positive selection means, the regexp must match for selection of an entry."
4365 (interactive)
4366 (org-agenda-manipulate-query ?\{))
4367 (defun org-agenda-manipulate-query-subtract-re ()
4368 "Manipulate the query by adding a search regexp with negative selection.
4369 Negative selection means, regexp must not match for selection of an entry."
4370 (interactive)
4371 (org-agenda-manipulate-query ?\}))
4372 (defun org-agenda-manipulate-query (char)
4373 (cond
4374 ((memq org-agenda-type '(timeline agenda))
4375 (if (y-or-n-p "Re-display with inactive time stamps included? ")
4376 (let ((org-agenda-include-inactive-timestamps t))
4377 (org-agenda-redo))
4378 (error "Abort")))
4379 ((eq org-agenda-type 'search)
4380 (org-add-to-string
4381 'org-agenda-query-string
4382 (cdr (assoc char '((?\[ . " +") (?\] . " -")
4383 (?\{ . " +{}") (?\} . " -{}")))))
4384 (setq org-agenda-redo-command
4385 (list 'org-search-view
4386 org-todo-only
4387 org-agenda-query-string
4388 (+ (length org-agenda-query-string)
4389 (if (member char '(?\{ ?\})) 0 1))))
4390 (set-register org-agenda-query-register org-agenda-query-string)
4391 (org-agenda-redo))
4392 (t (error "Cannot manipulate query for %s-type agenda buffers"
4393 org-agenda-type))))
4395 (defun org-add-to-string (var string)
4396 (set var (concat (symbol-value var) string)))
4398 (defun org-agenda-goto-date (date)
4399 "Jump to DATE in agenda."
4400 (interactive (list (org-read-date)))
4401 (org-agenda-list nil date))
4403 (defun org-agenda-goto-today ()
4404 "Go to today."
4405 (interactive)
4406 (org-agenda-check-type t 'timeline 'agenda)
4407 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
4408 (cond
4409 (tdpos (goto-char tdpos))
4410 ((eq org-agenda-type 'agenda)
4411 (let* ((sd (time-to-days
4412 (time-subtract (current-time)
4413 (list 0 (* 3600 org-extend-today-until) 0))))
4414 (comp (org-agenda-compute-time-span sd org-agenda-span))
4415 (org-agenda-overriding-arguments org-agenda-last-arguments))
4416 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
4417 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
4418 (org-agenda-redo)
4419 (org-agenda-find-same-or-today-or-agenda)))
4420 (t (error "Cannot find today")))))
4422 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
4423 (goto-char
4424 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
4425 (text-property-any (point-min) (point-max) 'org-today t)
4426 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
4427 (point-min))))
4429 (defun org-agenda-later (arg)
4430 "Go forward in time by thee current span.
4431 With prefix ARG, go forward that many times the current span."
4432 (interactive "p")
4433 (org-agenda-check-type t 'agenda)
4434 (let* ((span org-agenda-span)
4435 (sd org-starting-day)
4436 (greg (calendar-gregorian-from-absolute sd))
4437 (cnt (get-text-property (point) 'org-day-cnt))
4438 greg2 nd)
4439 (cond
4440 ((eq span 'day)
4441 (setq sd (+ arg sd) nd 1))
4442 ((eq span 'week)
4443 (setq sd (+ (* 7 arg) sd) nd 7))
4444 ((eq span 'month)
4445 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
4446 sd (calendar-absolute-from-gregorian greg2))
4447 (setcar greg2 (1+ (car greg2)))
4448 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
4449 ((eq span 'year)
4450 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
4451 sd (calendar-absolute-from-gregorian greg2))
4452 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
4453 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
4454 (let ((org-agenda-overriding-arguments
4455 (list (car org-agenda-last-arguments) sd nd t)))
4456 (org-agenda-redo)
4457 (org-agenda-find-same-or-today-or-agenda cnt))))
4459 (defun org-agenda-earlier (arg)
4460 "Go backward in time by the current span.
4461 With prefix ARG, go backward that many times the current span."
4462 (interactive "p")
4463 (org-agenda-later (- arg)))
4465 (defun org-agenda-day-view (&optional day-of-year)
4466 "Switch to daily view for agenda.
4467 With argument DAY-OF-YEAR, switch to that day of the year."
4468 (interactive "P")
4469 (setq org-agenda-ndays 1)
4470 (org-agenda-change-time-span 'day day-of-year))
4471 (defun org-agenda-week-view (&optional iso-week)
4472 "Switch to daily view for agenda.
4473 With argument ISO-WEEK, switch to the corresponding ISO week.
4474 If ISO-WEEK has more then 2 digits, only the last two encode the
4475 week. Any digits before this encode a year. So 200712 means
4476 week 12 of year 2007. Years in the range 1938-2037 can also be
4477 written as 2-digit years."
4478 (interactive "P")
4479 (setq org-agenda-ndays 7)
4480 (org-agenda-change-time-span 'week iso-week))
4481 (defun org-agenda-month-view (&optional month)
4482 "Switch to monthly view for agenda.
4483 With argument MONTH, switch to that month."
4484 (interactive "P")
4485 (org-agenda-change-time-span 'month month))
4486 (defun org-agenda-year-view (&optional year)
4487 "Switch to yearly view for agenda.
4488 With argument YEAR, switch to that year.
4489 If MONTH has more then 2 digits, only the last two encode the
4490 month. Any digits before this encode a year. So 200712 means
4491 December year 2007. Years in the range 1938-2037 can also be
4492 written as 2-digit years."
4493 (interactive "P")
4494 (when year
4495 (setq year (org-small-year-to-year year)))
4496 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
4497 (org-agenda-change-time-span 'year year)
4498 (error "Abort")))
4500 (defun org-agenda-change-time-span (span &optional n)
4501 "Change the agenda view to SPAN.
4502 SPAN may be `day', `week', `month', `year'."
4503 (org-agenda-check-type t 'agenda)
4504 (if (and (not n) (equal org-agenda-span span))
4505 (error "Viewing span is already \"%s\"" span))
4506 (let* ((sd (or (get-text-property (point) 'day)
4507 org-starting-day))
4508 (computed (org-agenda-compute-time-span sd span n))
4509 (org-agenda-overriding-arguments
4510 (list (car org-agenda-last-arguments)
4511 (car computed) (cdr computed) t)))
4512 (org-agenda-redo)
4513 (org-agenda-find-same-or-today-or-agenda))
4514 (org-agenda-set-mode-name)
4515 (message "Switched to %s view" span))
4517 (defun org-agenda-compute-time-span (sd span &optional n)
4518 "Compute starting date and number of days for agenda.
4519 SPAN may be `day', `week', `month', `year'. The return value
4520 is a cons cell with the starting date and the number of days,
4521 so that the date SD will be in that range."
4522 (let* ((greg (calendar-gregorian-from-absolute sd))
4523 (dg (nth 1 greg))
4524 (mg (car greg))
4525 (yg (nth 2 greg))
4526 nd w1 y1 m1 thisweek)
4527 (cond
4528 ((eq span 'day)
4529 (when n
4530 (setq sd (+ (calendar-absolute-from-gregorian
4531 (list mg 1 yg))
4532 n -1)))
4533 (setq nd 1))
4534 ((eq span 'week)
4535 (let* ((nt (calendar-day-of-week
4536 (calendar-gregorian-from-absolute sd)))
4537 (d (if org-agenda-start-on-weekday
4538 (- nt org-agenda-start-on-weekday)
4539 0)))
4540 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
4541 (when n
4542 (require 'cal-iso)
4543 (setq thisweek (car (calendar-iso-from-absolute sd)))
4544 (when (> n 99)
4545 (setq y1 (org-small-year-to-year (/ n 100))
4546 n (mod n 100)))
4547 (setq sd
4548 (calendar-absolute-from-iso
4549 (list n 1
4550 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
4551 (setq nd 7)))
4552 ((eq span 'month)
4553 (when (and n (> n 99))
4554 (setq y1 (org-small-year-to-year (/ n 100))
4555 n (mod n 100)))
4556 (setq sd (calendar-absolute-from-gregorian
4557 (list (or n mg) 1 (or y1 yg)))
4558 nd (- (calendar-absolute-from-gregorian
4559 (list (1+ (or n mg)) 1 (or y1 yg)))
4560 sd)))
4561 ((eq span 'year)
4562 (setq sd (calendar-absolute-from-gregorian
4563 (list 1 1 (or n yg)))
4564 nd (- (calendar-absolute-from-gregorian
4565 (list 1 1 (1+ (or n yg))))
4566 sd))))
4567 (cons sd nd)))
4569 (defun org-agenda-next-date-line (&optional arg)
4570 "Jump to the next line indicating a date in agenda buffer."
4571 (interactive "p")
4572 (org-agenda-check-type t 'agenda 'timeline)
4573 (beginning-of-line 1)
4574 ;; This does not work if user makes date format that starts with a blank
4575 (if (looking-at "^\\S-") (forward-char 1))
4576 (if (not (re-search-forward "^\\S-" nil t arg))
4577 (progn
4578 (backward-char 1)
4579 (error "No next date after this line in this buffer")))
4580 (goto-char (match-beginning 0)))
4582 (defun org-agenda-previous-date-line (&optional arg)
4583 "Jump to the previous line indicating a date in agenda buffer."
4584 (interactive "p")
4585 (org-agenda-check-type t 'agenda 'timeline)
4586 (beginning-of-line 1)
4587 (if (not (re-search-backward "^\\S-" nil t arg))
4588 (error "No previous date before this line in this buffer")))
4590 ;; Initialize the highlight
4591 (defvar org-hl (org-make-overlay 1 1))
4592 (org-overlay-put org-hl 'face 'highlight)
4594 (defun org-highlight (begin end &optional buffer)
4595 "Highlight a region with overlay."
4596 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
4597 org-hl begin end (or buffer (current-buffer))))
4599 (defun org-unhighlight ()
4600 "Detach overlay INDEX."
4601 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
4603 ;; FIXME this is currently not used.
4604 (defun org-highlight-until-next-command (beg end &optional buffer)
4605 "Move the highlight overlay to BEG/END, remove it before the next command."
4606 (org-highlight beg end buffer)
4607 (add-hook 'pre-command-hook 'org-unhighlight-once))
4608 (defun org-unhighlight-once ()
4609 "Remove the highlight from its position, and this function from the hook."
4610 (remove-hook 'pre-command-hook 'org-unhighlight-once)
4611 (org-unhighlight))
4613 (defun org-agenda-follow-mode ()
4614 "Toggle follow mode in an agenda buffer."
4615 (interactive)
4616 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
4617 (org-agenda-set-mode-name)
4618 (message "Follow mode is %s"
4619 (if org-agenda-follow-mode "on" "off")))
4621 (defun org-agenda-clockreport-mode ()
4622 "Toggle clocktable mode in an agenda buffer."
4623 (interactive)
4624 (org-agenda-check-type t 'agenda)
4625 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
4626 (org-agenda-set-mode-name)
4627 (org-agenda-redo)
4628 (message "Clocktable mode is %s"
4629 (if org-agenda-clockreport-mode "on" "off")))
4631 (defun org-agenda-log-mode (&optional with-states)
4632 "Toggle log mode in an agenda buffer."
4633 (interactive "P")
4634 (org-agenda-check-type t 'agenda 'timeline)
4635 (setq org-agenda-show-log
4636 (if with-states '(closed clock state)
4637 (not org-agenda-show-log)))
4638 (org-agenda-set-mode-name)
4639 (org-agenda-redo)
4640 (message "Log mode is %s"
4641 (if org-agenda-show-log "on" "off")))
4643 (defun org-agenda-archives-mode (&optional with-files)
4644 "Toggle log mode in an agenda buffer."
4645 (interactive "P")
4646 (setq org-agenda-archives-mode
4647 (if with-files t (if org-agenda-archives-mode nil 'trees)))
4648 (org-agenda-set-mode-name)
4649 (org-agenda-redo)
4650 (message
4651 "%s"
4652 (cond
4653 ((eq org-agenda-archives-mode nil)
4654 "No archives are included")
4655 ((eq org-agenda-archives-mode 'trees)
4656 (format "Trees with :%s: tag are included" org-archive-tag))
4657 ((eq org-agenda-archives-mode t)
4658 (format "Trees with :%s: tag and all active archive files are included"
4659 org-archive-tag)))))
4661 (defun org-agenda-toggle-diary ()
4662 "Toggle diary inclusion in an agenda buffer."
4663 (interactive)
4664 (org-agenda-check-type t 'agenda)
4665 (setq org-agenda-include-diary (not org-agenda-include-diary))
4666 (org-agenda-redo)
4667 (org-agenda-set-mode-name)
4668 (message "Diary inclusion turned %s"
4669 (if org-agenda-include-diary "on" "off")))
4671 (defun org-agenda-toggle-time-grid ()
4672 "Toggle time grid in an agenda buffer."
4673 (interactive)
4674 (org-agenda-check-type t 'agenda)
4675 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
4676 (org-agenda-redo)
4677 (org-agenda-set-mode-name)
4678 (message "Time-grid turned %s"
4679 (if org-agenda-use-time-grid "on" "off")))
4681 (defun org-agenda-set-mode-name ()
4682 "Set the mode name to indicate all the small mode settings."
4683 (setq mode-name
4684 (concat "Org-Agenda"
4685 (if (equal org-agenda-ndays 1) " Day" "")
4686 (if (equal org-agenda-ndays 7) " Week" "")
4687 (if org-agenda-follow-mode " Follow" "")
4688 (if org-agenda-include-diary " Diary" "")
4689 (if org-agenda-use-time-grid " Grid" "")
4690 (if (consp org-agenda-show-log) " LogAll"
4691 (if org-agenda-show-log " Log" ""))
4692 (if org-agenda-filter
4693 (concat " {" (mapconcat 'identity org-agenda-filter "") "}")
4695 (if org-agenda-archives-mode
4696 (if (eq org-agenda-archives-mode t)
4697 " Archives"
4698 (format " :%s:" org-archive-tag))
4700 (if org-agenda-clockreport-mode " Clock" "")))
4701 (force-mode-line-update))
4703 (defun org-agenda-post-command-hook ()
4704 (and (eolp) (not (bolp)) (backward-char 1))
4705 (setq org-agenda-type
4706 (or (get-text-property (point) 'org-agenda-type)
4707 (get-text-property (max (point-min) (1- (point)))
4708 'org-agenda-type)))
4709 (if (and org-agenda-follow-mode
4710 (get-text-property (point) 'org-marker))
4711 (org-agenda-show)))
4713 (defun org-agenda-show-priority ()
4714 "Show the priority of the current item.
4715 This priority is composed of the main priority given with the [#A] cookies,
4716 and by additional input from the age of a schedules or deadline entry."
4717 (interactive)
4718 (let* ((pri (get-text-property (point-at-bol) 'priority)))
4719 (message "Priority is %d" (if pri pri -1000))))
4721 (defun org-agenda-show-tags ()
4722 "Show the tags applicable to the current item."
4723 (interactive)
4724 (let* ((tags (get-text-property (point-at-bol) 'tags)))
4725 (if tags
4726 (message "Tags are :%s:"
4727 (org-no-properties (mapconcat 'identity tags ":")))
4728 (message "No tags associated with this line"))))
4730 (defun org-agenda-goto (&optional highlight)
4731 "Go to the Org-mode file which contains the item at point."
4732 (interactive)
4733 (let* ((marker (or (get-text-property (point) 'org-marker)
4734 (org-agenda-error)))
4735 (buffer (marker-buffer marker))
4736 (pos (marker-position marker)))
4737 (switch-to-buffer-other-window buffer)
4738 (widen)
4739 (goto-char pos)
4740 (when (org-mode-p)
4741 (org-show-context 'agenda)
4742 (save-excursion
4743 (and (outline-next-heading)
4744 (org-flag-heading nil)))) ; show the next heading
4745 (recenter (/ (window-height) 2))
4746 (run-hooks 'org-agenda-after-show-hook)
4747 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
4749 (defvar org-agenda-after-show-hook nil
4750 "Normal hook run after an item has been shown from the agenda.
4751 Point is in the buffer where the item originated.")
4753 (defun org-agenda-kill ()
4754 "Kill the entry or subtree belonging to the current agenda entry."
4755 (interactive)
4756 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4757 (let* ((marker (or (get-text-property (point) 'org-marker)
4758 (org-agenda-error)))
4759 (buffer (marker-buffer marker))
4760 (pos (marker-position marker))
4761 (type (get-text-property (point) 'type))
4762 dbeg dend (n 0) conf)
4763 (org-with-remote-undo buffer
4764 (with-current-buffer buffer
4765 (save-excursion
4766 (goto-char pos)
4767 (if (and (org-mode-p) (not (member type '("sexp"))))
4768 (setq dbeg (progn (org-back-to-heading t) (point))
4769 dend (org-end-of-subtree t t))
4770 (setq dbeg (point-at-bol)
4771 dend (min (point-max) (1+ (point-at-eol)))))
4772 (goto-char dbeg)
4773 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
4774 (setq conf (or (eq t org-agenda-confirm-kill)
4775 (and (numberp org-agenda-confirm-kill)
4776 (> n org-agenda-confirm-kill))))
4777 (and conf
4778 (not (y-or-n-p
4779 (format "Delete entry with %d lines in buffer \"%s\"? "
4780 n (buffer-name buffer))))
4781 (error "Abort"))
4782 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
4783 (with-current-buffer buffer (delete-region dbeg dend))
4784 (message "Agenda item and source killed"))))
4786 (defun org-agenda-archive ()
4787 "Archive the entry or subtree belonging to the current agenda entry."
4788 (interactive)
4789 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4790 (let* ((marker (or (get-text-property (point) 'org-marker)
4791 (org-agenda-error)))
4792 (buffer (marker-buffer marker))
4793 (pos (marker-position marker)))
4794 (org-with-remote-undo buffer
4795 (with-current-buffer buffer
4796 (if (org-mode-p)
4797 (save-excursion
4798 (goto-char pos)
4799 (org-remove-subtree-entries-from-agenda)
4800 (org-back-to-heading t)
4801 (org-archive-subtree))
4802 (error "Archiving works only in Org-mode files"))))))
4804 (defun org-agenda-archive-to-archive-sibling ()
4805 "Move the entry to the archive sibling."
4806 (interactive)
4807 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4808 (let* ((marker (or (get-text-property (point) 'org-marker)
4809 (org-agenda-error)))
4810 (buffer (marker-buffer marker))
4811 (pos (marker-position marker)))
4812 (org-with-remote-undo buffer
4813 (with-current-buffer buffer
4814 (if (org-mode-p)
4815 (save-excursion
4816 (goto-char pos)
4817 (org-remove-subtree-entries-from-agenda)
4818 (org-back-to-heading t)
4819 (org-archive-to-archive-sibling))
4820 (error "Archiving works only in Org-mode files"))))))
4822 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
4823 "Remove all lines in the agenda that correspond to a given subtree.
4824 The subtree is the one in buffer BUF, starting at BEG and ending at END.
4825 If this information is not given, the function uses the tree at point."
4826 (let ((buf (or buf (current-buffer))) m p)
4827 (save-excursion
4828 (unless (and beg end)
4829 (org-back-to-heading t)
4830 (setq beg (point))
4831 (org-end-of-subtree t)
4832 (setq end (point)))
4833 (set-buffer (get-buffer org-agenda-buffer-name))
4834 (save-excursion
4835 (goto-char (point-max))
4836 (beginning-of-line 1)
4837 (while (not (bobp))
4838 (when (and (setq m (get-text-property (point) 'org-marker))
4839 (equal buf (marker-buffer m))
4840 (setq p (marker-position m))
4841 (>= p beg)
4842 (<= p end))
4843 (let ((inhibit-read-only t))
4844 (delete-region (point-at-bol) (1+ (point-at-eol)))))
4845 (beginning-of-line 0))))))
4847 (defun org-agenda-open-link ()
4848 "Follow the link in the current line, if any."
4849 (interactive)
4850 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
4851 (save-excursion
4852 (save-restriction
4853 (narrow-to-region (point-at-bol) (point-at-eol))
4854 (org-open-at-point))))
4856 (defun org-agenda-copy-local-variable (var)
4857 "Get a variable from a referenced buffer and install it here."
4858 (let ((m (get-text-property (point) 'org-marker)))
4859 (when (and m (buffer-live-p (marker-buffer m)))
4860 (org-set-local var (with-current-buffer (marker-buffer m)
4861 (symbol-value var))))))
4863 (defun org-agenda-switch-to (&optional delete-other-windows)
4864 "Go to the Org-mode file which contains the item at point."
4865 (interactive)
4866 (let* ((marker (or (get-text-property (point) 'org-marker)
4867 (org-agenda-error)))
4868 (buffer (marker-buffer marker))
4869 (pos (marker-position marker)))
4870 (switch-to-buffer buffer)
4871 (and delete-other-windows (delete-other-windows))
4872 (widen)
4873 (goto-char pos)
4874 (when (org-mode-p)
4875 (org-show-context 'agenda)
4876 (save-excursion
4877 (and (outline-next-heading)
4878 (org-flag-heading nil)))))) ; show the next heading
4880 (defun org-agenda-goto-mouse (ev)
4881 "Go to the Org-mode file which contains the item at the mouse click."
4882 (interactive "e")
4883 (mouse-set-point ev)
4884 (org-agenda-goto))
4886 (defun org-agenda-show ()
4887 "Display the Org-mode file which contains the item at point."
4888 (interactive)
4889 (let ((win (selected-window)))
4890 (org-agenda-goto t)
4891 (select-window win)))
4893 (defun org-agenda-recenter (arg)
4894 "Display the Org-mode file which contains the item at point and recenter."
4895 (interactive "P")
4896 (let ((win (selected-window)))
4897 (org-agenda-goto t)
4898 (recenter arg)
4899 (select-window win)))
4901 (defun org-agenda-show-mouse (ev)
4902 "Display the Org-mode file which contains the item at the mouse click."
4903 (interactive "e")
4904 (mouse-set-point ev)
4905 (org-agenda-show))
4907 (defun org-agenda-check-no-diary ()
4908 "Check if the entry is a diary link and abort if yes."
4909 (if (get-text-property (point) 'org-agenda-diary-link)
4910 (org-agenda-error)))
4912 (defun org-agenda-error ()
4913 (error "Command not allowed in this line"))
4915 (defun org-agenda-tree-to-indirect-buffer ()
4916 "Show the subtree corresponding to the current entry in an indirect buffer.
4917 This calls the command `org-tree-to-indirect-buffer' from the original
4918 Org-mode buffer.
4919 With numerical prefix arg ARG, go up to this level and then take that tree.
4920 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
4921 dedicated frame)."
4922 (interactive)
4923 (org-agenda-check-no-diary)
4924 (let* ((marker (or (get-text-property (point) 'org-marker)
4925 (org-agenda-error)))
4926 (buffer (marker-buffer marker))
4927 (pos (marker-position marker)))
4928 (with-current-buffer buffer
4929 (save-excursion
4930 (goto-char pos)
4931 (call-interactively 'org-tree-to-indirect-buffer)))))
4933 (defvar org-last-heading-marker (make-marker)
4934 "Marker pointing to the headline that last changed its TODO state
4935 by a remote command from the agenda.")
4937 (defun org-agenda-todo-nextset ()
4938 "Switch TODO entry to next sequence."
4939 (interactive)
4940 (org-agenda-todo 'nextset))
4942 (defun org-agenda-todo-previousset ()
4943 "Switch TODO entry to previous sequence."
4944 (interactive)
4945 (org-agenda-todo 'previousset))
4947 (defun org-agenda-todo (&optional arg)
4948 "Cycle TODO state of line at point, also in Org-mode file.
4949 This changes the line at point, all other lines in the agenda referring to
4950 the same tree node, and the headline of the tree node in the Org-mode file."
4951 (interactive "P")
4952 (org-agenda-check-no-diary)
4953 (let* ((col (current-column))
4954 (marker (or (get-text-property (point) 'org-marker)
4955 (org-agenda-error)))
4956 (buffer (marker-buffer marker))
4957 (pos (marker-position marker))
4958 (hdmarker (get-text-property (point) 'org-hd-marker))
4959 (todayp (equal (get-text-property (point) 'day)
4960 (time-to-days (current-time))))
4961 (inhibit-read-only t)
4962 org-agenda-headline-snapshot-before-repeat newhead just-one)
4963 (org-with-remote-undo buffer
4964 (with-current-buffer buffer
4965 (widen)
4966 (goto-char pos)
4967 (org-show-context 'agenda)
4968 (save-excursion
4969 (and (outline-next-heading)
4970 (org-flag-heading nil))) ; show the next heading
4971 (org-todo arg)
4972 (and (bolp) (forward-char 1))
4973 (setq newhead (org-get-heading))
4974 (when (and (org-bound-and-true-p
4975 org-agenda-headline-snapshot-before-repeat)
4976 (not (equal org-agenda-headline-snapshot-before-repeat
4977 newhead))
4978 todayp)
4979 (setq newhead org-agenda-headline-snapshot-before-repeat
4980 just-one t))
4981 (save-excursion
4982 (org-back-to-heading)
4983 (move-marker org-last-heading-marker (point))))
4984 (beginning-of-line 1)
4985 (save-excursion
4986 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
4987 (org-move-to-column col))))
4989 (defun org-agenda-add-note (&optional arg)
4990 "Add a time-stamped note to the entry at point."
4991 (interactive "P")
4992 (org-agenda-check-no-diary)
4993 (let* ((marker (or (get-text-property (point) 'org-marker)
4994 (org-agenda-error)))
4995 (buffer (marker-buffer marker))
4996 (pos (marker-position marker))
4997 (hdmarker (get-text-property (point) 'org-hd-marker))
4998 (inhibit-read-only t))
4999 (with-current-buffer buffer
5000 (widen)
5001 (goto-char pos)
5002 (org-show-context 'agenda)
5003 (save-excursion
5004 (and (outline-next-heading)
5005 (org-flag-heading nil))) ; show the next heading
5006 (org-add-note))))
5008 (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface just-this)
5009 "Change all lines in the agenda buffer which match HDMARKER.
5010 The new content of the line will be NEWHEAD (as modified by
5011 `org-format-agenda-item'). HDMARKER is checked with
5012 `equal' against all `org-hd-marker' text properties in the file.
5013 If FIXFACE is non-nil, the face of each item is modified acording to
5014 the new TODO state."
5015 (let* ((inhibit-read-only t)
5016 (line (org-current-line))
5017 props m pl undone-face done-face finish new dotime cat tags)
5018 (save-excursion
5019 (goto-char (point-max))
5020 (beginning-of-line 1)
5021 (while (not finish)
5022 (setq finish (bobp))
5023 (when (and (setq m (get-text-property (point) 'org-hd-marker))
5024 (or (not just-this) (= (org-current-line) line))
5025 (equal m hdmarker))
5026 (setq props (text-properties-at (point))
5027 dotime (get-text-property (point) 'dotime)
5028 cat (get-text-property (point) 'org-category)
5029 tags (get-text-property (point) 'tags)
5030 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
5031 pl (get-text-property (point) 'prefix-length)
5032 undone-face (get-text-property (point) 'undone-face)
5033 done-face (get-text-property (point) 'done-face))
5034 (org-move-to-column pl)
5035 (cond
5036 ((equal new "")
5037 (beginning-of-line 1)
5038 (and (looking-at ".*\n?") (replace-match "")))
5039 ((looking-at ".*")
5040 (replace-match new t t)
5041 (beginning-of-line 1)
5042 (add-text-properties (point-at-bol) (point-at-eol) props)
5043 (when fixface
5044 (add-text-properties
5045 (point-at-bol) (point-at-eol)
5046 (list 'face
5047 (if org-last-todo-state-is-todo
5048 undone-face done-face))))
5049 (org-agenda-highlight-todo 'line)
5050 (beginning-of-line 1))
5051 (t (error "Line update did not work"))))
5052 (beginning-of-line 0)))
5053 (org-finalize-agenda)))
5055 (defun org-agenda-align-tags (&optional line)
5056 "Align all tags in agenda items to `org-agenda-tags-column'."
5057 (let ((inhibit-read-only t) l c)
5058 (save-excursion
5059 (goto-char (if line (point-at-bol) (point-min)))
5060 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
5061 (if line (point-at-eol) nil) t)
5062 (add-text-properties
5063 (match-beginning 2) (match-end 2)
5064 (list 'face (delq nil (adjoin 'org-tag
5065 (let ((prop (get-text-property
5066 (match-beginning 2) 'face)))
5067 (if (listp prop)
5068 prop
5069 (list prop)))))))
5070 (setq l (- (match-end 2) (match-beginning 2))
5071 c (if (< org-agenda-tags-column 0)
5072 (- (abs org-agenda-tags-column) l)
5073 org-agenda-tags-column))
5074 (delete-region (match-beginning 1) (match-end 1))
5075 (goto-char (match-beginning 1))
5076 (insert (org-add-props
5077 (make-string (max 1 (- c (current-column))) ?\ )
5078 (text-properties-at (point))))))))
5080 (defun org-agenda-priority-up ()
5081 "Increase the priority of line at point, also in Org-mode file."
5082 (interactive)
5083 (org-agenda-priority 'up))
5085 (defun org-agenda-priority-down ()
5086 "Decrease the priority of line at point, also in Org-mode file."
5087 (interactive)
5088 (org-agenda-priority 'down))
5090 (defun org-agenda-priority (&optional force-direction)
5091 "Set the priority of line at point, also in Org-mode file.
5092 This changes the line at point, all other lines in the agenda referring to
5093 the same tree node, and the headline of the tree node in the Org-mode file."
5094 (interactive)
5095 (org-agenda-check-no-diary)
5096 (let* ((marker (or (get-text-property (point) 'org-marker)
5097 (org-agenda-error)))
5098 (hdmarker (get-text-property (point) 'org-hd-marker))
5099 (buffer (marker-buffer hdmarker))
5100 (pos (marker-position hdmarker))
5101 (inhibit-read-only t)
5102 newhead)
5103 (org-with-remote-undo buffer
5104 (with-current-buffer buffer
5105 (widen)
5106 (goto-char pos)
5107 (org-show-context 'agenda)
5108 (save-excursion
5109 (and (outline-next-heading)
5110 (org-flag-heading nil))) ; show the next heading
5111 (funcall 'org-priority force-direction)
5112 (end-of-line 1)
5113 (setq newhead (org-get-heading)))
5114 (org-agenda-change-all-lines newhead hdmarker)
5115 (beginning-of-line 1))))
5117 ;; FIXME: should fix the tags property of the agenda line.
5118 (defun org-agenda-set-tags ()
5119 "Set tags for the current headline."
5120 (interactive)
5121 (org-agenda-check-no-diary)
5122 (if (and (org-region-active-p) (interactive-p))
5123 (call-interactively 'org-change-tag-in-region)
5124 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5125 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5126 (org-agenda-error)))
5127 (buffer (marker-buffer hdmarker))
5128 (pos (marker-position hdmarker))
5129 (inhibit-read-only t)
5130 newhead)
5131 (org-with-remote-undo buffer
5132 (with-current-buffer buffer
5133 (widen)
5134 (goto-char pos)
5135 (save-excursion
5136 (org-show-context 'agenda))
5137 (save-excursion
5138 (and (outline-next-heading)
5139 (org-flag-heading nil))) ; show the next heading
5140 (goto-char pos)
5141 (call-interactively 'org-set-tags)
5142 (end-of-line 1)
5143 (setq newhead (org-get-heading)))
5144 (org-agenda-change-all-lines newhead hdmarker)
5145 (beginning-of-line 1)))))
5147 (defun org-agenda-toggle-archive-tag ()
5148 "Toggle the archive tag for the current entry."
5149 (interactive)
5150 (org-agenda-check-no-diary)
5151 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5152 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5153 (org-agenda-error)))
5154 (buffer (marker-buffer hdmarker))
5155 (pos (marker-position hdmarker))
5156 (inhibit-read-only t)
5157 newhead)
5158 (org-with-remote-undo buffer
5159 (with-current-buffer buffer
5160 (widen)
5161 (goto-char pos)
5162 (org-show-context 'agenda)
5163 (save-excursion
5164 (and (outline-next-heading)
5165 (org-flag-heading nil))) ; show the next heading
5166 (call-interactively 'org-toggle-archive-tag)
5167 (end-of-line 1)
5168 (setq newhead (org-get-heading)))
5169 (org-agenda-change-all-lines newhead hdmarker)
5170 (beginning-of-line 1))))
5172 (defun org-agenda-date-later (arg &optional what)
5173 "Change the date of this item to one day later."
5174 (interactive "p")
5175 (org-agenda-check-type t 'agenda 'timeline)
5176 (org-agenda-check-no-diary)
5177 (let* ((marker (or (get-text-property (point) 'org-marker)
5178 (org-agenda-error)))
5179 (buffer (marker-buffer marker))
5180 (pos (marker-position marker)))
5181 (org-with-remote-undo buffer
5182 (with-current-buffer buffer
5183 (widen)
5184 (goto-char pos)
5185 (if (not (org-at-timestamp-p))
5186 (error "Cannot find time stamp"))
5187 (org-timestamp-change arg (or what 'day)))
5188 (org-agenda-show-new-time marker org-last-changed-timestamp))
5189 (message "Time stamp changed to %s" org-last-changed-timestamp)))
5191 (defun org-agenda-date-earlier (arg &optional what)
5192 "Change the date of this item to one day earlier."
5193 (interactive "p")
5194 (org-agenda-date-later (- arg) what))
5196 (defun org-agenda-show-new-time (marker stamp &optional prefix)
5197 "Show new date stamp via text properties."
5198 ;; We use text properties to make this undoable
5199 (let ((inhibit-read-only t)
5200 (buffer-invisibility-spec))
5201 (setq stamp (concat " " prefix " => " stamp))
5202 (save-excursion
5203 (goto-char (point-max))
5204 (while (not (bobp))
5205 (when (equal marker (get-text-property (point) 'org-marker))
5206 (org-move-to-column (- (window-width) (length stamp)) t)
5207 (org-agenda-fix-tags-filter-overlays-at (point))
5208 (if (featurep 'xemacs)
5209 ;; Use `duplicable' property to trigger undo recording
5210 (let ((ex (make-extent nil nil))
5211 (gl (make-glyph stamp)))
5212 (set-glyph-face gl 'secondary-selection)
5213 (set-extent-properties
5214 ex (list 'invisible t 'end-glyph gl 'duplicable t))
5215 (insert-extent ex (1- (point)) (point-at-eol)))
5216 (add-text-properties
5217 (1- (point)) (point-at-eol)
5218 (list 'display (org-add-props stamp nil
5219 'face 'secondary-selection))))
5220 (beginning-of-line 1))
5221 (beginning-of-line 0)))))
5223 (defun org-agenda-date-prompt (arg)
5224 "Change the date of this item. Date is prompted for, with default today.
5225 The prefix ARG is passed to the `org-time-stamp' command and can therefore
5226 be used to request time specification in the time stamp."
5227 (interactive "P")
5228 (org-agenda-check-type t 'agenda 'timeline)
5229 (org-agenda-check-no-diary)
5230 (let* ((marker (or (get-text-property (point) 'org-marker)
5231 (org-agenda-error)))
5232 (buffer (marker-buffer marker))
5233 (pos (marker-position marker)))
5234 (org-with-remote-undo buffer
5235 (with-current-buffer buffer
5236 (widen)
5237 (goto-char pos)
5238 (if (not (org-at-timestamp-p))
5239 (error "Cannot find time stamp"))
5240 (org-time-stamp arg)
5241 (message "Time stamp changed to %s" org-last-changed-timestamp)))))
5243 (defun org-agenda-schedule (arg)
5244 "Schedule the item at point."
5245 (interactive "P")
5246 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5247 (org-agenda-check-no-diary)
5248 (let* ((marker (or (get-text-property (point) 'org-marker)
5249 (org-agenda-error)))
5250 (type (marker-insertion-type marker))
5251 (buffer (marker-buffer marker))
5252 (pos (marker-position marker))
5253 (org-insert-labeled-timestamps-at-point nil)
5255 (when type (message "%s" type) (sit-for 3))
5256 (set-marker-insertion-type marker t)
5257 (org-with-remote-undo buffer
5258 (with-current-buffer buffer
5259 (widen)
5260 (goto-char pos)
5261 (setq ts (org-schedule arg)))
5262 (org-agenda-show-new-time marker ts "S"))
5263 (message "Item scheduled for %s" ts)))
5265 (defun org-agenda-deadline (arg)
5266 "Schedule the item at point."
5267 (interactive "P")
5268 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5269 (org-agenda-check-no-diary)
5270 (let* ((marker (or (get-text-property (point) 'org-marker)
5271 (org-agenda-error)))
5272 (buffer (marker-buffer marker))
5273 (pos (marker-position marker))
5274 (org-insert-labeled-timestamps-at-point nil)
5276 (org-with-remote-undo buffer
5277 (with-current-buffer buffer
5278 (widen)
5279 (goto-char pos)
5280 (setq ts (org-deadline arg)))
5281 (org-agenda-show-new-time marker ts "S"))
5282 (message "Deadline for this item set to %s" ts)))
5284 (defun org-agenda-action ()
5285 "Select entry for agenda action, or execute an agenda action.
5286 This command prompts for another letter. Valid inputs are:
5288 m Mark the entry at point for an agenda action
5289 s Schedule the marked entry to the date at the cursor
5290 d Set the deadline of the marked entry to the date at the cursor
5291 r Call `org-remember' with cursor date as the default date
5292 SPC Show marked entry in other window
5293 TAB Visit marked entry in other window
5295 The cursor may be at a date in the calendar, or in the Org agenda."
5296 (interactive)
5297 (let (pos ans)
5298 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
5299 (setq ans (read-char-exclusive))
5300 (cond
5301 ((equal ans ?m)
5302 ;; Mark this entry
5303 (if (eq major-mode 'org-agenda-mode)
5304 (let ((m (or (get-text-property (point) 'org-hd-marker)
5305 (get-text-property (point) 'org-marker))))
5306 (if m
5307 (progn
5308 (move-marker org-agenda-action-marker
5309 (marker-position m) (marker-buffer m))
5310 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
5311 (error "Don't know which entry to mark")))
5312 (error "This command works only in the agenda")))
5313 ((equal ans ?s)
5314 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
5315 ((equal ans ?d)
5316 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
5317 ((equal ans ?r)
5318 (org-agenda-do-action '(org-remember) t))
5319 ((equal ans ?\ )
5320 (let ((cw (selected-window)))
5321 (org-switch-to-buffer-other-window
5322 (marker-buffer org-agenda-action-marker))
5323 (goto-char org-agenda-action-marker)
5324 (org-show-context 'agenda)
5325 (select-window cw)))
5326 ((equal ans ?\C-i)
5327 (org-switch-to-buffer-other-window
5328 (marker-buffer org-agenda-action-marker))
5329 (goto-char org-agenda-action-marker)
5330 (org-show-context 'agenda))
5331 (t (error "Invalid agenda action %c" ans)))))
5333 (defun org-agenda-do-action (form &optional current-buffer)
5334 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
5335 (let ((org-overriding-default-time (org-get-cursor-date)))
5336 (if current-buffer
5337 (eval form)
5338 (if (not (marker-buffer org-agenda-action-marker))
5339 (error "No entry has bee selected for agenda action")
5340 (with-current-buffer (marker-buffer org-agenda-action-marker)
5341 (save-excursion
5342 (save-restriction
5343 (widen)
5344 (goto-char org-agenda-action-marker)
5345 (eval form))))))))
5347 (defun org-agenda-clock-in (&optional arg)
5348 "Start the clock on the currently selected item."
5349 (interactive "P")
5350 (org-agenda-check-no-diary)
5351 (if (equal arg '(4))
5352 (org-clock-in arg)
5353 (let* ((marker (or (get-text-property (point) 'org-marker)
5354 (org-agenda-error)))
5355 (hdmarker (or (get-text-property (point) 'org-hd-marker)
5356 marker))
5357 (pos (marker-position marker))
5358 newhead)
5359 (org-with-remote-undo (marker-buffer marker)
5360 (with-current-buffer (marker-buffer marker)
5361 (widen)
5362 (goto-char pos)
5363 (org-show-context 'agenda)
5364 (org-show-entry)
5365 (org-cycle-hide-drawers 'children)
5366 (org-clock-in arg)
5367 (setq newhead (org-get-heading)))
5368 (org-agenda-change-all-lines newhead hdmarker t)))))
5370 (defun org-agenda-clock-out (&optional arg)
5371 "Stop the currently running clock."
5372 (interactive "P")
5373 (unless (marker-buffer org-clock-marker)
5374 (error "No running clock"))
5375 (org-with-remote-undo (marker-buffer org-clock-marker)
5376 (org-clock-out)))
5378 (defun org-agenda-clock-cancel (&optional arg)
5379 "Cancel the currently running clock."
5380 (interactive "P")
5381 (unless (marker-buffer org-clock-marker)
5382 (error "No running clock"))
5383 (org-with-remote-undo (marker-buffer org-clock-marker)
5384 (org-clock-cancel)))
5386 (defun org-agenda-diary-entry ()
5387 "Make a diary entry, like the `i' command from the calendar.
5388 All the standard commands work: block, weekly etc."
5389 (interactive)
5390 (org-agenda-check-type t 'agenda 'timeline)
5391 (require 'diary-lib)
5392 (let* ((char (progn
5393 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
5394 (read-char-exclusive)))
5395 (cmd (cdr (assoc char
5396 '((?d . insert-diary-entry)
5397 (?w . insert-weekly-diary-entry)
5398 (?m . insert-monthly-diary-entry)
5399 (?y . insert-yearly-diary-entry)
5400 (?a . insert-anniversary-diary-entry)
5401 (?b . insert-block-diary-entry)
5402 (?c . insert-cyclic-diary-entry)))))
5403 (oldf (symbol-function 'calendar-cursor-to-date))
5404 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
5405 (point (point))
5406 (mark (or (mark t) (point))))
5407 (unless cmd
5408 (error "No command associated with <%c>" char))
5409 (unless (and (get-text-property point 'day)
5410 (or (not (equal ?b char))
5411 (get-text-property mark 'day)))
5412 (error "Don't know which date to use for diary entry"))
5413 ;; We implement this by hacking the `calendar-cursor-to-date' function
5414 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
5415 (let ((calendar-mark-ring
5416 (list (calendar-gregorian-from-absolute
5417 (or (get-text-property mark 'day)
5418 (get-text-property point 'day))))))
5419 (unwind-protect
5420 (progn
5421 (fset 'calendar-cursor-to-date
5422 (lambda (&optional error dummy)
5423 (calendar-gregorian-from-absolute
5424 (get-text-property point 'day))))
5425 (call-interactively cmd))
5426 (fset 'calendar-cursor-to-date oldf)))))
5428 (defun org-agenda-execute-calendar-command (cmd)
5429 "Execute a calendar command from the agenda, with the date associated to
5430 the cursor position."
5431 (org-agenda-check-type t 'agenda 'timeline)
5432 (require 'diary-lib)
5433 (unless (get-text-property (point) 'day)
5434 (error "Don't know which date to use for calendar command"))
5435 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
5436 (point (point))
5437 (date (calendar-gregorian-from-absolute
5438 (get-text-property point 'day)))
5439 ;; the following 2 vars are needed in the calendar
5440 (displayed-month (car date))
5441 (displayed-year (nth 2 date)))
5442 (unwind-protect
5443 (progn
5444 (fset 'calendar-cursor-to-date
5445 (lambda (&optional error dummy)
5446 (calendar-gregorian-from-absolute
5447 (get-text-property point 'day))))
5448 (call-interactively cmd))
5449 (fset 'calendar-cursor-to-date oldf))))
5451 (defun org-agenda-phases-of-moon ()
5452 "Display the phases of the moon for the 3 months around the cursor date."
5453 (interactive)
5454 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
5456 (defun org-agenda-holidays ()
5457 "Display the holidays for the 3 months around the cursor date."
5458 (interactive)
5459 (org-agenda-execute-calendar-command 'list-calendar-holidays))
5461 (defvar calendar-longitude)
5462 (defvar calendar-latitude)
5463 (defvar calendar-location-name)
5465 (defun org-agenda-sunrise-sunset (arg)
5466 "Display sunrise and sunset for the cursor date.
5467 Latitude and longitude can be specified with the variables
5468 `calendar-latitude' and `calendar-longitude'. When called with prefix
5469 argument, latitude and longitude will be prompted for."
5470 (interactive "P")
5471 (require 'solar)
5472 (let ((calendar-longitude (if arg nil calendar-longitude))
5473 (calendar-latitude (if arg nil calendar-latitude))
5474 (calendar-location-name
5475 (if arg "the given coordinates" calendar-location-name)))
5476 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
5478 (defun org-agenda-goto-calendar ()
5479 "Open the Emacs calendar with the date at the cursor."
5480 (interactive)
5481 (org-agenda-check-type t 'agenda 'timeline)
5482 (let* ((day (or (get-text-property (point) 'day)
5483 (error "Don't know which date to open in calendar")))
5484 (date (calendar-gregorian-from-absolute day))
5485 (calendar-move-hook nil)
5486 (calendar-view-holidays-initially-flag nil)
5487 (calendar-view-diary-initially-flag nil)
5488 (view-calendar-holidays-initially nil)
5489 (calendar-view-diary-initially-flag nil)
5490 (calendar-view-holidays-initially-flag nil)
5491 (view-diary-entries-initially nil))
5492 (calendar)
5493 (calendar-goto-date date)))
5495 ;;;###autoload
5496 (defun org-calendar-goto-agenda ()
5497 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
5498 This is a command that has to be installed in `calendar-mode-map'."
5499 (interactive)
5500 (org-agenda-list nil (calendar-absolute-from-gregorian
5501 (calendar-cursor-to-date))
5502 nil))
5504 (defun org-agenda-convert-date ()
5505 (interactive)
5506 (org-agenda-check-type t 'agenda 'timeline)
5507 (let ((day (get-text-property (point) 'day))
5508 date s)
5509 (unless day
5510 (error "Don't know which date to convert"))
5511 (setq date (calendar-gregorian-from-absolute day))
5512 (setq s (concat
5513 "Gregorian: " (calendar-date-string date) "\n"
5514 "ISO: " (calendar-iso-date-string date) "\n"
5515 "Day of Yr: " (calendar-day-of-year-string date) "\n"
5516 "Julian: " (calendar-julian-date-string date) "\n"
5517 "Astron. JD: " (calendar-astro-date-string date)
5518 " (Julian date number at noon UTC)\n"
5519 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
5520 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
5521 "French: " (calendar-french-date-string date) "\n"
5522 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
5523 "Mayan: " (calendar-mayan-date-string date) "\n"
5524 "Coptic: " (calendar-coptic-date-string date) "\n"
5525 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
5526 "Persian: " (calendar-persian-date-string date) "\n"
5527 "Chinese: " (calendar-chinese-date-string date) "\n"))
5528 (with-output-to-temp-buffer "*Dates*"
5529 (princ s))
5530 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
5532 ;;; Appointment reminders
5534 (defvar appt-time-msg-list)
5536 ;;;###autoload
5537 (defun org-agenda-to-appt (&optional refresh filter)
5538 "Activate appointments found in `org-agenda-files'.
5539 With a \\[universal-argument] prefix, refresh the list of
5540 appointements.
5542 If FILTER is t, interactively prompt the user for a regular
5543 expression, and filter out entries that don't match it.
5545 If FILTER is a string, use this string as a regular expression
5546 for filtering entries out.
5548 FILTER can also be an alist with the car of each cell being
5549 either 'headline or 'category. For example:
5551 '((headline \"IMPORTANT\")
5552 (category \"Work\"))
5554 will only add headlines containing IMPORTANT or headlines
5555 belonging to the \"Work\" category."
5556 (interactive "P")
5557 (require 'calendar)
5558 (if refresh (setq appt-time-msg-list nil))
5559 (if (eq filter t)
5560 (setq filter (read-from-minibuffer "Regexp filter: ")))
5561 (let* ((cnt 0) ; count added events
5562 (org-agenda-new-buffers nil)
5563 (org-deadline-warning-days 0)
5564 (today (org-date-to-gregorian
5565 (time-to-days (current-time))))
5566 (files (org-agenda-files 'unrestricted)) entries file)
5567 ;; Get all entries which may contain an appt
5568 (while (setq file (pop files))
5569 (setq entries
5570 (append entries
5571 (org-agenda-get-day-entries
5572 file today :timestamp :scheduled :deadline))))
5573 (setq entries (delq nil entries))
5574 ;; Map thru entries and find if we should filter them out
5575 (mapc
5576 (lambda(x)
5577 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
5578 (cat (get-text-property 1 'org-category x))
5579 (tod (get-text-property 1 'time-of-day x))
5580 (ok (or (null filter)
5581 (and (stringp filter) (string-match filter evt))
5582 (and (listp filter)
5583 (or (string-match
5584 (cadr (assoc 'category filter)) cat)
5585 (string-match
5586 (cadr (assoc 'headline filter)) evt))))))
5587 ;; FIXME: Shall we remove text-properties for the appt text?
5588 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
5589 (when (and ok tod)
5590 (setq tod (concat "00" (number-to-string tod))
5591 tod (when (string-match
5592 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
5593 (concat (match-string 1 tod) ":"
5594 (match-string 2 tod))))
5595 (appt-add tod evt)
5596 (setq cnt (1+ cnt))))) entries)
5597 (org-release-buffers org-agenda-new-buffers)
5598 (if (eq cnt 0)
5599 (message "No event to add")
5600 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
5602 (defun org-agenda-todayp (date)
5603 "Does DATE mean today, when considering `org-extend-today-until'?"
5604 (let (today h)
5605 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
5606 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
5607 (setq h (nth 2 (decode-time (current-time))))
5608 (or (and (>= h org-extend-today-until)
5609 (= date today))
5610 (and (< h org-extend-today-until)
5611 (= date (1- today))))))
5613 (provide 'org-agenda)
5615 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
5617 ;;; org-agenda.el ends here