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