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