Make `i' key in agenda and calendar add new entries to an org file
[org-mode.git] / lisp / org-agenda.el
blob92febb745470b99bcf40845b3c04a745bc7169de
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.32trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
44 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
45 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-french-date-string "cal-french" (&optional date))
47 (declare-function calendar-goto-date "cal-move" (date))
48 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
49 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
50 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
51 (declare-function calendar-iso-from-absolute "cal-iso" (date))
52 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
53 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
54 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
55 (declare-function org-datetree-find-date-create "org-datetree" (date))
56 (declare-function org-columns-quit "org-colview" ())
57 (declare-function diary-date-display-form "diary-lib" (&optional type))
58 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
59 (declare-function org-habit-insert-consistency-graphs
60 "org-habit" (&optional line))
61 (declare-function org-is-habit-p "org-habit" (&optional pom))
62 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
63 (declare-function org-habit-get-priority "org-habit" (habit))
64 (defvar calendar-mode-map)
65 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
66 (defvar org-habit-show-habits)
67 (defvar org-habit-show-habits-only-for-today)
69 ;; Defined somewhere in this file, but used before definition.
70 (defvar org-agenda-buffer-name)
71 (defvar org-agenda-overriding-header)
72 (defvar org-agenda-title-append nil)
73 (defvar entry)
74 (defvar date)
75 (defvar org-agenda-undo-list)
76 (defvar org-agenda-pending-undo-list)
77 (defvar original-date) ; dynamically scoped, calendar.el does scope this
79 (defcustom org-agenda-confirm-kill 1
80 "When set, remote killing from the agenda buffer needs confirmation.
81 When t, a confirmation is always needed. When a number N, confirmation is
82 only needed when the text to be killed contains more than N non-white lines."
83 :group 'org-agenda
84 :type '(choice
85 (const :tag "Never" nil)
86 (const :tag "Always" t)
87 (integer :tag "When more than N lines")))
89 (defcustom org-agenda-compact-blocks nil
90 "Non-nil means, make the block agenda more compact.
91 This is done by leaving out unnecessary lines."
92 :group 'org-agenda
93 :type 'boolean)
95 (defcustom org-agenda-block-separator ?=
96 "The separator between blocks in the agenda.
97 If this is a string, it will be used as the separator, with a newline added.
98 If it is a character, it will be repeated to fill the window width."
99 :group 'org-agenda
100 :type '(choice
101 (character)
102 (string)))
104 (defgroup org-agenda-export nil
105 "Options concerning exporting agenda views in Org-mode."
106 :tag "Org Agenda Export"
107 :group 'org-agenda)
109 (defcustom org-agenda-with-colors t
110 "Non-nil means, use colors in agenda views."
111 :group 'org-agenda-export
112 :type 'boolean)
114 (defcustom org-agenda-exporter-settings nil
115 "Alist of variable/value pairs that should be active during agenda export.
116 This is a good place to set options for ps-print and for htmlize.
117 Note that the way this is implemented, the values will be evaluated
118 before assigned to the variables. So make sure to quote values you do
119 *not* want evaluated, for example
121 (setq org-agenda-exporter-settings
122 '((ps-print-color-p 'black-white)))"
123 :group 'org-agenda-export
124 :type '(repeat
125 (list
126 (variable)
127 (sexp :tag "Value"))))
129 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
130 "Hook run in temporary buffer before writing it to an export file.
131 A useful function is `org-agenda-add-entry-text'."
132 :group 'org-agenda-export
133 :type 'hook
134 :options '(org-agenda-add-entry-text))
136 (defcustom org-agenda-add-entry-text-maxlines 0
137 "Maximum number of entry text lines to be added to agenda.
138 This is only relevant when `org-agenda-add-entry-text' is part of
139 `org-agenda-before-write-hook', which it is by default.
140 When this is 0, nothing will happen. When it is greater than 0, it
141 specifies the maximum number of lines that will be added for each entry
142 that is listed in the agenda view.
144 Note that this variable is not used during display, only when exporting
145 the agenda. For agenda display, see org-agenda-entry-text-mode and the
146 variable `org-agenda-entry-text-maxlines'."
147 :group 'org-agenda
148 :type 'integer)
150 (defcustom org-agenda-add-entry-text-descriptive-links t
151 "Non-nil means, export org-links as descriptive links in agenda added text.
152 This variable applies to the text added to the agenda when
153 `org-agenda-add-entry-text-maxlines' is larger than 0.
154 When this variable nil, the URL will (also) be shown."
155 :group 'org-agenda
156 :type 'boolean)
158 (defcustom org-agenda-export-html-style ""
159 "The style specification for exported HTML Agenda files.
160 If this variable contains a string, it will replace the default <style>
161 section as produced by `htmlize'.
162 Since there are different ways of setting style information, this variable
163 needs to contain the full HTML structure to provide a style, including the
164 surrounding HTML tags. The style specifications should include definitions
165 the fonts used by the agenda, here is an example:
167 <style type=\"text/css\">
168 p { font-weight: normal; color: gray; }
169 .org-agenda-structure {
170 font-size: 110%;
171 color: #003399;
172 font-weight: 600;
174 .org-todo {
175 color: #cc6666;
176 font-weight: bold;
178 .org-agenda-done {
179 color: #339933;
181 .org-done {
182 color: #339933;
184 .title { text-align: center; }
185 .todo, .deadline { color: red; }
186 .done { color: green; }
187 </style>
189 or, if you want to keep the style in a file,
191 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
193 As the value of this option simply gets inserted into the HTML <head> header,
194 you can \"misuse\" it to also add other text to the header. However,
195 <style>...</style> is required, if not present the variable will be ignored."
196 :group 'org-agenda-export
197 :group 'org-export-html
198 :type 'string)
200 (defgroup org-agenda-custom-commands nil
201 "Options concerning agenda views in Org-mode."
202 :tag "Org Agenda Custom Commands"
203 :group 'org-agenda)
205 (defconst org-sorting-choice
206 '(choice
207 (const time-up) (const time-down)
208 (const category-keep) (const category-up) (const category-down)
209 (const tag-down) (const tag-up)
210 (const priority-up) (const priority-down)
211 (const todo-state-up) (const todo-state-down)
212 (const effort-up) (const effort-down)
213 (const habit-up) (const habit-down)
214 (const user-defined-up) (const user-defined-down))
215 "Sorting choices.")
217 (defconst org-agenda-custom-commands-local-options
218 `(repeat :tag "Local settings for this command. Remember to quote values"
219 (choice :tag "Setting"
220 (list :tag "Heading for this block"
221 (const org-agenda-overriding-header)
222 (string :tag "Headline"))
223 (list :tag "Files to be searched"
224 (const org-agenda-files)
225 (list
226 (const :format "" quote)
227 (repeat (file))))
228 (list :tag "Sorting strategy"
229 (const org-agenda-sorting-strategy)
230 (list
231 (const :format "" quote)
232 (repeat
233 ,org-sorting-choice)))
234 (list :tag "Prefix format"
235 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
236 (string))
237 (list :tag "Number of days in agenda"
238 (const org-agenda-ndays)
239 (integer :value 1))
240 (list :tag "Fixed starting date"
241 (const org-agenda-start-day)
242 (string :value "2007-11-01"))
243 (list :tag "Start on day of week"
244 (const org-agenda-start-on-weekday)
245 (choice :value 1
246 (const :tag "Today" nil)
247 (integer :tag "Weekday No.")))
248 (list :tag "Include data from diary"
249 (const org-agenda-include-diary)
250 (boolean))
251 (list :tag "Deadline Warning days"
252 (const org-deadline-warning-days)
253 (integer :value 1))
254 (list :tag "Tags filter preset"
255 (const org-agenda-filter-preset)
256 (list
257 (const :format "" quote)
258 (repeat
259 (string :tag "+tag or -tag"))))
260 (list :tag "Standard skipping condition"
261 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
262 (const org-agenda-skip-function)
263 (list
264 (const :format "" quote)
265 (list
266 (choice
267 :tag "Skipping range"
268 (const :tag "Skip entry" org-agenda-skip-entry-if)
269 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
270 (repeat :inline t :tag "Conditions for skipping"
271 (choice
272 :tag "Condition type"
273 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
274 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
275 (const :tag "scheduled" 'scheduled)
276 (const :tag "not scheduled" 'notscheduled)
277 (const :tag "deadline" 'deadline)
278 (const :tag "no deadline" 'notdeadline)
279 (const :tag "timestamp" 'timestamp)
280 (const :tag "no timestamp" 'nottimestamp))))))
281 (list :tag "Non-standard skipping condition"
282 :value (org-agenda-skip-function)
283 (const org-agenda-skip-function)
284 (sexp :tag "Function or form (quoted!)"))
285 (list :tag "Any variable"
286 (variable :tag "Variable")
287 (sexp :tag "Value (sexp)"))))
288 "Selection of examples for agenda command settings.
289 This will be spliced into the custom type of
290 `org-agenda-custom-commands'.")
293 (defcustom org-agenda-custom-commands nil
294 "Custom commands for the agenda.
295 These commands will be offered on the splash screen displayed by the
296 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
298 (key desc type match settings files)
300 key The key (one or more characters as a string) to be associated
301 with the command.
302 desc A description of the command, when omitted or nil, a default
303 description is built using MATCH.
304 type The command type, any of the following symbols:
305 agenda The daily/weekly agenda.
306 todo Entries with a specific TODO keyword, in all agenda files.
307 search Entries containing search words entry or headline.
308 tags Tags/Property/TODO match in all agenda files.
309 tags-todo Tags/P/T match in all agenda files, TODO entries only.
310 todo-tree Sparse tree of specific TODO keyword in *current* file.
311 tags-tree Sparse tree with all tags matches in *current* file.
312 occur-tree Occur sparse tree for *current* file.
313 ... A user-defined function.
314 match What to search for:
315 - a single keyword for TODO keyword searches
316 - a tags match expression for tags searches
317 - a word search expression for text searches.
318 - a regular expression for occur searches
319 For all other commands, this should be the empty string.
320 settings A list of option settings, similar to that in a let form, so like
321 this: ((opt1 val1) (opt2 val2) ...). The values will be
322 evaluated at the moment of execution, so quote them when needed.
323 files A list of files file to write the produced agenda buffer to
324 with the command `org-store-agenda-views'.
325 If a file name ends in \".html\", an HTML version of the buffer
326 is written out. If it ends in \".ps\", a postscript version is
327 produced. Otherwise, only the plain text is written to the file.
329 You can also define a set of commands, to create a composite agenda buffer.
330 In this case, an entry looks like this:
332 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
334 where
336 desc A description string to be displayed in the dispatcher menu.
337 cmd An agenda command, similar to the above. However, tree commands
338 are no allowed, but instead you can get agenda and global todo list.
339 So valid commands for a set are:
340 (agenda \"\" settings)
341 (alltodo \"\" settings)
342 (stuck \"\" settings)
343 (todo \"match\" settings files)
344 (search \"match\" settings files)
345 (tags \"match\" settings files)
346 (tags-todo \"match\" settings files)
348 Each command can carry a list of options, and another set of options can be
349 given for the whole set of commands. Individual command options take
350 precedence over the general options.
352 When using several characters as key to a command, the first characters
353 are prefix commands. For the dispatcher to display useful information, you
354 should provide a description for the prefix, like
356 (setq org-agenda-custom-commands
357 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
358 (\"hl\" tags \"+HOME+Lisa\")
359 (\"hp\" tags \"+HOME+Peter\")
360 (\"hk\" tags \"+HOME+Kim\")))"
361 :group 'org-agenda-custom-commands
362 :type `(repeat
363 (choice :value ("x" "Describe command here" tags "" nil)
364 (list :tag "Single command"
365 (string :tag "Access Key(s) ")
366 (option (string :tag "Description"))
367 (choice
368 (const :tag "Agenda" agenda)
369 (const :tag "TODO list" alltodo)
370 (const :tag "Search words" search)
371 (const :tag "Stuck projects" stuck)
372 (const :tag "Tags/Property match (all agenda files)" tags)
373 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
374 (const :tag "TODO keyword search (all agenda files)" todo)
375 (const :tag "Tags sparse tree (current buffer)" tags-tree)
376 (const :tag "TODO keyword tree (current buffer)" todo-tree)
377 (const :tag "Occur tree (current buffer)" occur-tree)
378 (sexp :tag "Other, user-defined function"))
379 (string :tag "Match (only for some commands)")
380 ,org-agenda-custom-commands-local-options
381 (option (repeat :tag "Export" (file :tag "Export to"))))
382 (list :tag "Command series, all agenda files"
383 (string :tag "Access Key(s)")
384 (string :tag "Description ")
385 (repeat :tag "Component"
386 (choice
387 (list :tag "Agenda"
388 (const :format "" agenda)
389 (const :tag "" :format "" "")
390 ,org-agenda-custom-commands-local-options)
391 (list :tag "TODO list (all keywords)"
392 (const :format "" alltodo)
393 (const :tag "" :format "" "")
394 ,org-agenda-custom-commands-local-options)
395 (list :tag "Search words"
396 (const :format "" search)
397 (string :tag "Match")
398 ,org-agenda-custom-commands-local-options)
399 (list :tag "Stuck projects"
400 (const :format "" stuck)
401 (const :tag "" :format "" "")
402 ,org-agenda-custom-commands-local-options)
403 (list :tag "Tags search"
404 (const :format "" tags)
405 (string :tag "Match")
406 ,org-agenda-custom-commands-local-options)
407 (list :tag "Tags search, TODO entries only"
408 (const :format "" tags-todo)
409 (string :tag "Match")
410 ,org-agenda-custom-commands-local-options)
411 (list :tag "TODO keyword search"
412 (const :format "" todo)
413 (string :tag "Match")
414 ,org-agenda-custom-commands-local-options)
415 (list :tag "Other, user-defined function"
416 (symbol :tag "function")
417 (string :tag "Match")
418 ,org-agenda-custom-commands-local-options)))
420 (repeat :tag "Settings for entire command set"
421 (list (variable :tag "Any variable")
422 (sexp :tag "Value")))
423 (option (repeat :tag "Export" (file :tag "Export to"))))
424 (cons :tag "Prefix key documentation"
425 (string :tag "Access Key(s)")
426 (string :tag "Description ")))))
428 (defcustom org-agenda-query-register ?o
429 "The register holding the current query string.
430 The purpose of this is that if you construct a query string interactively,
431 you can then use it to define a custom command."
432 :group 'org-agenda-custom-commands
433 :type 'character)
435 (defcustom org-stuck-projects
436 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
437 "How to identify stuck projects.
438 This is a list of four items:
439 1. A tags/todo/property matcher string that is used to identify a project.
440 See the manual for a description of tag and property searches.
441 The entire tree below a headline matched by this is considered one project.
442 2. A list of TODO keywords identifying non-stuck projects.
443 If the project subtree contains any headline with one of these todo
444 keywords, the project is considered to be not stuck. If you specify
445 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
446 3. A list of tags identifying non-stuck projects.
447 If the project subtree contains any headline with one of these tags,
448 the project is considered to be not stuck. If you specify \"*\" as
449 a tag, any tag will mark the project unstuck. Note that this is about
450 the explicit presence of a tag somewhere in the subtree, inherited
451 tags to not count here. If inherited tags make a project not stuck,
452 use \"-TAG\" in the tags part of the matcher under (1.) above.
453 4. An arbitrary regular expression matching non-stuck projects.
455 If the project turns out to be not stuck, search continues also in the
456 subtree to see if any of the subtasks have project status.
458 See also the variable `org-tags-match-list-sublevels' which applies
459 to projects matched by this search as well.
461 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
462 or `C-c a #' to produce the list."
463 :group 'org-agenda-custom-commands
464 :type '(list
465 (string :tag "Tags/TODO match to identify a project")
466 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
467 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
468 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
470 (defcustom org-agenda-filter-effort-default-operator "<"
471 "The default operator for effort estimate filtering.
472 If you select an effort estimate limit without first pressing an operator,
473 this one will be used."
474 :group 'org-agenda-custom-commands
475 :type '(choice (const :tag "less or equal" "<")
476 (const :tag "greater or equal"">")
477 (const :tag "equal" "=")))
479 (defgroup org-agenda-skip nil
480 "Options concerning skipping parts of agenda files."
481 :tag "Org Agenda Skip"
482 :group 'org-agenda)
483 (defgroup org-agenda-daily/weekly nil
484 "Options concerning the daily/weekly agenda."
485 :tag "Org Agenda Daily/Weekly"
486 :group 'org-agenda)
487 (defgroup org-agenda-todo-list nil
488 "Options concerning the global todo list agenda view."
489 :tag "Org Agenda Todo List"
490 :group 'org-agenda)
491 (defgroup org-agenda-match-view nil
492 "Options concerning the general tags/property/todo match agenda view."
493 :tag "Org Agenda Match View"
494 :group 'org-agenda)
495 (defgroup org-agenda-search-view nil
496 "Options concerning the general tags/property/todo match agenda view."
497 :tag "Org Agenda Match View"
498 :group 'org-agenda)
500 (defvar org-agenda-archives-mode nil
501 "Non-nil means, the agenda will include archived items.
502 If this is the symbol `trees', trees in the selected agenda scope
503 that are marked with the ARCHIVE tag will be included anyway. When this is
504 t, also all archive files associated with the current selection of agenda
505 files will be included.")
507 (defcustom org-agenda-skip-comment-trees t
508 "Non-nil means, skip trees that start with the COMMENT keyword.
509 When nil, these trees are also scanned by agenda commands."
510 :group 'org-agenda-skip
511 :type 'boolean)
513 (defcustom org-agenda-todo-list-sublevels t
514 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
515 When nil, the sublevels of a TODO entry are not checked, resulting in
516 potentially much shorter TODO lists."
517 :group 'org-agenda-skip
518 :group 'org-agenda-todo-list
519 :type 'boolean)
521 (defcustom org-agenda-todo-ignore-with-date nil
522 "Non-nil means, don't show entries with a date in the global todo list.
523 You can use this if you prefer to mark mere appointments with a TODO keyword,
524 but don't want them to show up in the TODO list.
525 When this is set, it also covers deadlines and scheduled items, the settings
526 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
527 will be ignored.
528 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
529 :group 'org-agenda-skip
530 :group 'org-agenda-todo-list
531 :type 'boolean)
533 (defcustom org-agenda-todo-ignore-scheduled nil
534 "Non-nil means, don't show scheduled entries in the global todo list.
535 The idea behind this is that by scheduling it, you have already taken care
536 of this item.
537 See also `org-agenda-todo-ignore-with-date'.
538 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
539 :group 'org-agenda-skip
540 :group 'org-agenda-todo-list
541 :type 'boolean)
543 (defcustom org-agenda-todo-ignore-deadlines nil
544 "Non-nil means, don't show near deadline entries in the global todo list.
545 Near means closer than `org-deadline-warning-days' days.
546 The idea behind this is that such items will appear in the agenda anyway.
547 See also `org-agenda-todo-ignore-with-date'.
548 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
549 :group 'org-agenda-skip
550 :group 'org-agenda-todo-list
551 :type 'boolean)
553 (defcustom org-agenda-tags-todo-honor-ignore-options nil
554 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
555 The variables
556 `org-agenda-todo-ignore-with-date',
557 `org-agenda-todo-ignore-scheduled'
558 `org-agenda-todo-ignore-deadlines'
559 make the global TODO list skip entries that have time stamps of certain
560 kinds. If this option is set, the same options will also apply for the
561 tags-todo search, which is the general tags/property matcher
562 restricted to unfinished TODO entries only."
563 :group 'org-agenda-skip
564 :group 'org-agenda-todo-list
565 :group 'org-agenda-match-view
566 :type 'boolean)
568 (defcustom org-agenda-skip-scheduled-if-done nil
569 "Non-nil means don't show scheduled items in agenda when they are done.
570 This is relevant for the daily/weekly agenda, not for the TODO list. And
571 it applies only to the actual date of the scheduling. Warnings about
572 an item with a past scheduling dates are always turned off when the item
573 is DONE."
574 :group 'org-agenda-skip
575 :group 'org-agenda-daily/weekly
576 :type 'boolean)
578 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
579 "Non-nil means skip scheduling line if same entry shows because of deadline.
580 In the agenda of today, an entry can show up multiple times because
581 it is both scheduled and has a nearby deadline, and maybe a plain time
582 stamp as well.
583 When this variable is t, then only the deadline is shown and the fact that
584 the entry is scheduled today or was scheduled previously is not shown.
585 When this variable is nil, the entry will be shown several times. When
586 the variable is the symbol `not-today', then skip scheduled previously,
587 but not scheduled today."
588 :group 'org-agenda-skip
589 :group 'org-agenda-daily/weekly
590 :type '(choice
591 (const :tag "Never" nil)
592 (const :tag "Always" t)
593 (const :tag "Not when scheduled today" not-today)))
595 (defcustom org-agenda-skip-deadline-if-done nil
596 "Non-nil means don't show deadlines when the corresponding item is done.
597 When nil, the deadline is still shown and should give you a happy feeling.
598 This is relevant for the daily/weekly agenda. And it applied only to the
599 actually date of the deadline. Warnings about approaching and past-due
600 deadlines are always turned off when the item is DONE."
601 :group 'org-agenda-skip
602 :group 'org-agenda-daily/weekly
603 :type 'boolean)
605 (defcustom org-agenda-skip-additional-timestamps-same-entry t
606 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
607 When non-nil, after the search for timestamps has matched once in an
608 entry, the rest of the entry will not be searched."
609 :group 'org-agenda-skip
610 :type 'boolean)
612 (defcustom org-agenda-skip-timestamp-if-done nil
613 "Non-nil means don't select item by timestamp or -range if it is DONE."
614 :group 'org-agenda-skip
615 :group 'org-agenda-daily/weekly
616 :type 'boolean)
618 (defcustom org-agenda-dim-blocked-tasks t
619 "Non-nil means, dim blocked tasks in the agenda display.
620 This causes some overhead during agenda construction, but if you
621 have turned on `org-enforce-todo-dependencies',
622 `org-enforce-todo-checkbox-dependencies', or any other blocking
623 mechanism, this will create useful feedback in the agenda.
625 Instead ot t, this variable can also have the value `invisible'.
626 Then blocked tasks will be invisible and only become visible when
627 they become unblocked. An exemption to this behavior is when a task is
628 blocked because of unchecked checkboxes below it. Since checkboxes do
629 not show up in the agenda views, making this task invisible you remove any
630 trace from agenda views that there is something to do. Therefore, a task
631 that is blocked because of checkboxes will never be made invisible, it
632 will only be dimmed."
633 :group 'org-agenda-daily/weekly
634 :group 'org-agenda-todo-list
635 :type '(choice
636 (const :tag "Do not dim" nil)
637 (const :tag "Dim to a grey face" t)
638 (const :tag "Make invisibe" invisible)))
640 (defcustom org-timeline-show-empty-dates 3
641 "Non-nil means, `org-timeline' also shows dates without an entry.
642 When nil, only the days which actually have entries are shown.
643 When t, all days between the first and the last date are shown.
644 When an integer, show also empty dates, but if there is a gap of more than
645 N days, just insert a special line indicating the size of the gap."
646 :group 'org-agenda-skip
647 :type '(choice
648 (const :tag "None" nil)
649 (const :tag "All" t)
650 (integer :tag "at most")))
652 (defgroup org-agenda-startup nil
653 "Options concerning initial settings in the Agenda in Org Mode."
654 :tag "Org Agenda Startup"
655 :group 'org-agenda)
657 (defcustom org-finalize-agenda-hook nil
658 "Hook run just before displaying an agenda buffer."
659 :group 'org-agenda-startup
660 :type 'hook)
662 (defcustom org-agenda-mouse-1-follows-link nil
663 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
664 A longer mouse click will still set point. Does not work on XEmacs.
665 Needs to be set before org.el is loaded."
666 :group 'org-agenda-startup
667 :type 'boolean)
669 (defcustom org-agenda-start-with-follow-mode nil
670 "The initial value of follow-mode in a newly created agenda window."
671 :group 'org-agenda-startup
672 :type 'boolean)
674 (defcustom org-agenda-start-with-entry-text-mode nil
675 "The initial value of entry-text-mode in a newly created agenda window."
676 :group 'org-agenda-startup
677 :type 'boolean)
679 (defcustom org-agenda-entry-text-maxlines 5
680 "Number of text lines to be added when `E' is presed in the agenda.
682 Note that this variable only used during agenda display. Add add entry text
683 when exporting the agenda, configure the variable
684 `org-agenda-add-entry-ext-maxlines'."
685 :group 'org-agenda
686 :type 'integer)
688 (defcustom org-agenda-entry-text-exclude-regexps nil
689 "List of regular expressions to clean up entry text.
690 The complete matches of all regular expressions in this list will be
691 removed from entry text before it is shown in the agenda."
692 :group 'org-agenda
693 :type '(repeat (regexp)))
695 (defvar org-agenda-entry-text-cleanup-hook nil
696 "Hook that is run after basic cleanup of entry text to be shown in agenda.
697 This cleanup is done in a temporary buffer, so the function may inspect and
698 change the entire buffer.
699 Some default stuff like drawers and scheduling/deadline dates will already
700 have been removed when this is called, as will any matches for regular
701 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
703 (defvar org-agenda-include-inactive-timestamps nil
704 "Non-nil means, include inactive time stamps in agenda and timeline.")
706 (defgroup org-agenda-windows nil
707 "Options concerning the windows used by the Agenda in Org Mode."
708 :tag "Org Agenda Windows"
709 :group 'org-agenda)
711 (defcustom org-agenda-window-setup 'reorganize-frame
712 "How the agenda buffer should be displayed.
713 Possible values for this option are:
715 current-window Show agenda in the current window, keeping all other windows.
716 other-window Use `switch-to-buffer-other-window' to display agenda.
717 reorganize-frame Show only two windows on the current frame, the current
718 window and the agenda.
719 other-frame Use `switch-to-buffer-other-frame' to display agenda.
720 Also, when exiting the agenda, kill that frame.
721 See also the variable `org-agenda-restore-windows-after-quit'."
722 :group 'org-agenda-windows
723 :type '(choice
724 (const current-window)
725 (const other-frame)
726 (const other-window)
727 (const reorganize-frame)))
729 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
730 "The min and max height of the agenda window as a fraction of frame height.
731 The value of the variable is a cons cell with two numbers between 0 and 1.
732 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
733 :group 'org-agenda-windows
734 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
736 (defcustom org-agenda-restore-windows-after-quit nil
737 "Non-nil means, restore window configuration open exiting agenda.
738 Before the window configuration is changed for displaying the agenda,
739 the current status is recorded. When the agenda is exited with
740 `q' or `x' and this option is set, the old state is restored. If
741 `org-agenda-window-setup' is `other-frame', the value of this
742 option will be ignored."
743 :group 'org-agenda-windows
744 :type 'boolean)
746 (defcustom org-agenda-ndays 7
747 "Number of days to include in overview display.
748 Should be 1 or 7.
749 Custom commands can set this variable in the options section."
750 :group 'org-agenda-daily/weekly
751 :type 'integer)
753 (defcustom org-agenda-start-on-weekday 1
754 "Non-nil means, start the overview always on the specified weekday.
755 0 denotes Sunday, 1 denotes Monday etc.
756 When nil, always start on the current day.
757 Custom commands can set this variable in the options section."
758 :group 'org-agenda-daily/weekly
759 :type '(choice (const :tag "Today" nil)
760 (integer :tag "Weekday No.")))
762 (defcustom org-agenda-show-all-dates t
763 "Non-nil means, `org-agenda' shows every day in the selected range.
764 When nil, only the days which actually have entries are shown."
765 :group 'org-agenda-daily/weekly
766 :type 'boolean)
768 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
769 "Format string for displaying dates in the agenda.
770 Used by the daily/weekly agenda and by the timeline. This should be
771 a format string understood by `format-time-string', or a function returning
772 the formatted date as a string. The function must take a single argument,
773 a calendar-style date list like (month day year)."
774 :group 'org-agenda-daily/weekly
775 :type '(choice
776 (string :tag "Format string")
777 (function :tag "Function")))
779 (defun org-agenda-format-date-aligned (date)
780 "Format a date string for display in the daily/weekly agenda, or timeline.
781 This function makes sure that dates are aligned for easy reading."
782 (require 'cal-iso)
783 (let* ((dayname (calendar-day-name date))
784 (day (cadr date))
785 (day-of-week (calendar-day-of-week date))
786 (month (car date))
787 (monthname (calendar-month-name month))
788 (year (nth 2 date))
789 (iso-week (org-days-to-iso-week
790 (calendar-absolute-from-gregorian date)))
791 (weekyear (cond ((and (= month 1) (>= iso-week 52))
792 (1- year))
793 ((and (= month 12) (<= iso-week 1))
794 (1+ year))
795 (t year)))
796 (weekstring (if (= day-of-week 1)
797 (format " W%02d" iso-week)
798 "")))
799 (format "%-10s %2d %s %4d%s"
800 dayname day monthname year weekstring)))
802 (defcustom org-agenda-weekend-days '(6 0)
803 "Which days are weekend?
804 These days get the special face `org-agenda-date-weekend' in the agenda
805 and timeline buffers."
806 :group 'org-agenda-daily/weekly
807 :type '(set :greedy t
808 (const :tag "Monday" 1)
809 (const :tag "Tuesday" 2)
810 (const :tag "Wednesday" 3)
811 (const :tag "Thursday" 4)
812 (const :tag "Friday" 5)
813 (const :tag "Saturday" 6)
814 (const :tag "Sunday" 0)))
816 (defcustom org-agenda-include-diary nil
817 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
818 Custom commands can set this variable in the options section."
819 :group 'org-agenda-daily/weekly
820 :type 'boolean)
822 (defcustom org-agenda-include-all-todo nil
823 "Set means weekly/daily agenda will always contain all TODO entries.
824 The TODO entries will be listed at the top of the agenda, before
825 the entries for specific days.
826 This option is deprecated, it is better to define a block agenda instead."
827 :group 'org-agenda-daily/weekly
828 :type 'boolean)
830 (defcustom org-agenda-repeating-timestamp-show-all t
831 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
832 When nil, only one occurrence is shown, either today or the
833 nearest into the future."
834 :group 'org-agenda-daily/weekly
835 :type 'boolean)
837 (defcustom org-scheduled-past-days 10000
838 "No. of days to continue listing scheduled items that are not marked DONE.
839 When an item is scheduled on a date, it shows up in the agenda on this
840 day and will be listed until it is marked done for the number of days
841 given here."
842 :group 'org-agenda-daily/weekly
843 :type 'integer)
845 (defcustom org-agenda-log-mode-items '(closed clock)
846 "List of items that should be shown in agenda log mode.
847 This list may contain the following symbols:
849 closed Show entries that have been closed on that day.
850 clock Show entries that have received clocked time on that day.
851 state Show all logged state changes.
852 Note that instead of changing this variable, you can also press `C-u l' in
853 the agenda to display all available LOG items temporarily."
854 :group 'org-agenda-daily/weekly
855 :type '(set :greedy t (const closed) (const clock) (const state)))
857 (defcustom org-agenda-log-mode-add-notes t
858 "Non-nil means, add first line of notes to log entries in agenda views.
859 If a log item like a state change or a clock entry is associated with
860 notes, the first line of these notes will be added to the entry in the
861 agenda display."
862 :group 'org-agenda-daily/weekly
863 :type 'boolean)
865 (defcustom org-agenda-start-with-log-mode nil
866 "The initial value of log-mode in a newly created agenda window."
867 :group 'org-agenda-startup
868 :group 'org-agenda-daily/weekly
869 :type 'boolean)
871 (defcustom org-agenda-start-with-clockreport-mode nil
872 "The initial value of clockreport-mode in a newly created agenda window."
873 :group 'org-agenda-startup
874 :group 'org-agenda-daily/weekly
875 :type 'boolean)
877 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
878 "Property list with parameters for the clocktable in clockreport mode.
879 This is the display mode that shows a clock table in the daily/weekly
880 agenda, the properties for this dynamic block can be set here.
881 The usual clocktable parameters are allowed here, but you cannot set
882 the properties :name, :tstart, :tend, :block, and :scope - these will
883 be overwritten to make sure the content accurately reflects the
884 current display in the agenda."
885 :group 'org-agenda-daily/weekly
886 :type 'plist)
888 (defcustom org-agenda-search-view-search-words-only nil
889 "Non-nil means, the search string is interpreted as individual words
890 The search then looks for each word separately in each entry and
891 selects entries that have matches for all words.
892 When nil, matching as loose words will only take place if the first
893 word is preceded by + or -. If that is not the case, the search
894 string will just be matched as a substring in the entry, but with
895 each space character allowing for any whitespace, including newlines."
896 :group 'org-agenda-search-view
897 :type 'boolean)
899 (defgroup org-agenda-time-grid nil
900 "Options concerning the time grid in the Org-mode Agenda."
901 :tag "Org Agenda Time Grid"
902 :group 'org-agenda)
904 (defcustom org-agenda-search-headline-for-time t
905 "Non-nil means, search headline for a time-of-day.
906 If the headline contains a time-of-day in one format or another, it will
907 be used to sort the entry into the time sequence of items for a day.
908 Some people have time stamps in the headline that refer to the creation
909 time or so, and then this produces an unwanted side effect. If this is
910 the case for your, use this variable to turn off searching the headline
911 for a time."
912 :group 'org-agenda-time-grid
913 :type 'boolean)
915 (defcustom org-agenda-use-time-grid t
916 "Non-nil means, show a time grid in the agenda schedule.
917 A time grid is a set of lines for specific times (like every two hours between
918 8:00 and 20:00). The items scheduled for a day at specific times are
919 sorted in between these lines.
920 For details about when the grid will be shown, and what it will look like, see
921 the variable `org-agenda-time-grid'."
922 :group 'org-agenda-time-grid
923 :type 'boolean)
925 (defcustom org-agenda-time-grid
926 '((daily today require-timed)
927 "----------------"
928 (800 1000 1200 1400 1600 1800 2000))
930 "The settings for time grid for agenda display.
931 This is a list of three items. The first item is again a list. It contains
932 symbols specifying conditions when the grid should be displayed:
934 daily if the agenda shows a single day
935 weekly if the agenda shows an entire week
936 today show grid on current date, independent of daily/weekly display
937 require-timed show grid only if at least one item has a time specification
939 The second item is a string which will be placed behind the grid time.
941 The third item is a list of integers, indicating the times that should have
942 a grid line."
943 :group 'org-agenda-time-grid
944 :type
945 '(list
946 (set :greedy t :tag "Grid Display Options"
947 (const :tag "Show grid in single day agenda display" daily)
948 (const :tag "Show grid in weekly agenda display" weekly)
949 (const :tag "Always show grid for today" today)
950 (const :tag "Show grid only if any timed entries are present"
951 require-timed)
952 (const :tag "Skip grid times already present in an entry"
953 remove-match))
954 (string :tag "Grid String")
955 (repeat :tag "Grid Times" (integer :tag "Time"))))
957 (defgroup org-agenda-sorting nil
958 "Options concerning sorting in the Org-mode Agenda."
959 :tag "Org Agenda Sorting"
960 :group 'org-agenda)
962 (defcustom org-agenda-sorting-strategy
963 '((agenda habit-down time-up priority-down category-keep)
964 (todo priority-down category-keep)
965 (tags priority-down category-keep)
966 (search category-keep))
967 "Sorting structure for the agenda items of a single day.
968 This is a list of symbols which will be used in sequence to determine
969 if an entry should be listed before another entry. The following
970 symbols are recognized:
972 time-up Put entries with time-of-day indications first, early first
973 time-down Put entries with time-of-day indications first, late first
974 category-keep Keep the default order of categories, corresponding to the
975 sequence in `org-agenda-files'.
976 category-up Sort alphabetically by category, A-Z.
977 category-down Sort alphabetically by category, Z-A.
978 tag-up Sort alphabetically by last tag, A-Z.
979 tag-down Sort alphabetically by last tag, Z-A.
980 priority-up Sort numerically by priority, high priority last.
981 priority-down Sort numerically by priority, high priority first.
982 todo-state-up Sort by todo state, tasks that are done last.
983 todo-state-down Sort by todo state, tasks that are done first.
984 effort-up Sort numerically by estimated effort, high effort last.
985 effort-down Sort numerically by estimated effort, high effort first.
986 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
987 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
988 habit-up Put entries that are habits first
989 habit-down Put entries that are habits last
991 The different possibilities will be tried in sequence, and testing stops
992 if one comparison returns a \"not-equal\". For example, the default
993 '(time-up category-keep priority-down)
994 means: Pull out all entries having a specified time of day and sort them,
995 in order to make a time schedule for the current day the first thing in the
996 agenda listing for the day. Of the entries without a time indication, keep
997 the grouped in categories, don't sort the categories, but keep them in
998 the sequence given in `org-agenda-files'. Within each category sort by
999 priority.
1001 Leaving out `category-keep' would mean that items will be sorted across
1002 categories by priority.
1004 Instead of a single list, this can also be a set of list for specific
1005 contents, with a context symbol in the car of the list, any of
1006 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1008 Custom commands can bind this variable in the options section."
1009 :group 'org-agenda-sorting
1010 :type `(choice
1011 (repeat :tag "General" ,org-sorting-choice)
1012 (list :tag "Individually"
1013 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1014 (repeat ,org-sorting-choice))
1015 (cons (const :tag "Strategy for TODO lists" todo)
1016 (repeat ,org-sorting-choice))
1017 (cons (const :tag "Strategy for Tags matches" tags)
1018 (repeat ,org-sorting-choice))
1019 (cons (const :tag "Strategy for search matches" search)
1020 (repeat ,org-sorting-choice)))))
1022 (defcustom org-agenda-cmp-user-defined nil
1023 "A function to define the comparison `user-defined'.
1024 This function must receive two arguments, agenda entry a and b.
1025 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1026 the user comparison, return nil.
1027 When this is defined, you can make `user-defined-up' and `user-defined-down'
1028 part of an agenda sorting strategy."
1029 :group 'org-agenda-sorting
1030 :type 'symbol)
1032 (defcustom org-sort-agenda-notime-is-late t
1033 "Non-nil means, items without time are considered late.
1034 This is only relevant for sorting. When t, items which have no explicit
1035 time like 15:30 will be considered as 99:01, i.e. later than any items which
1036 do have a time. When nil, the default time is before 0:00. You can use this
1037 option to decide if the schedule for today should come before or after timeless
1038 agenda entries."
1039 :group 'org-agenda-sorting
1040 :type 'boolean)
1042 (defcustom org-sort-agenda-noeffort-is-high t
1043 "Non-nil means, items without effort estimate are sorted as high effort.
1044 This also applies when filtering an agenda view with respect to the
1045 < or > effort operator. Then, tasks with no effort defined will be treated
1046 as tasks with high effort.
1047 When nil, such items are sorted as 0 minutes effort."
1048 :group 'org-agenda-sorting
1049 :type 'boolean)
1051 (defgroup org-agenda-line-format nil
1052 "Options concerning the entry prefix in the Org-mode agenda display."
1053 :tag "Org Agenda Line Format"
1054 :group 'org-agenda)
1056 (defcustom org-agenda-prefix-format
1057 '((agenda . " %-12:c%?-12t% s")
1058 (timeline . " % s")
1059 (todo . " %-12:c")
1060 (tags . " %-12:c")
1061 (search . " %-12:c"))
1062 "Format specifications for the prefix of items in the agenda views.
1063 An alist with four entries, for the different agenda types. The keys to the
1064 sublists are `agenda', `timeline', `todo', and `tags'. The values
1065 are format strings.
1066 This format works similar to a printf format, with the following meaning:
1068 %c the category of the item, \"Diary\" for entries from the diary, or
1069 as given by the CATEGORY keyword or derived from the file name.
1070 %T the *last* tag of the item. Last because inherited tags come
1071 first in the list.
1072 %t the time-of-day specification if one applies to the entry, in the
1073 format HH:MM
1074 %s Scheduling/Deadline information, a short string
1076 All specifiers work basically like the standard `%s' of printf, but may
1077 contain two additional characters: A question mark just after the `%' and
1078 a whitespace/punctuation character just before the final letter.
1080 If the first character after `%' is a question mark, the entire field
1081 will only be included if the corresponding value applies to the
1082 current entry. This is useful for fields which should have fixed
1083 width when present, but zero width when absent. For example,
1084 \"%?-12t\" will result in a 12 character time field if a time of the
1085 day is specified, but will completely disappear in entries which do
1086 not contain a time.
1088 If there is punctuation or whitespace character just before the final
1089 format letter, this character will be appended to the field value if
1090 the value is not empty. For example, the format \"%-12:c\" leads to
1091 \"Diary: \" if the category is \"Diary\". If the category were be
1092 empty, no additional colon would be interted.
1094 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1095 - Indent the line with two space characters
1096 - Give the category in a 12 chars wide field, padded with whitespace on
1097 the right (because of `-'). Append a colon if there is a category
1098 (because of `:').
1099 - If there is a time-of-day, put it into a 12 chars wide field. If no
1100 time, don't put in an empty field, just skip it (because of '?').
1101 - Finally, put the scheduling information and append a whitespace.
1103 As another example, if you don't want the time-of-day of entries in
1104 the prefix, you could use:
1106 (setq org-agenda-prefix-format \" %-11:c% s\")
1108 See also the variables `org-agenda-remove-times-when-in-prefix' and
1109 `org-agenda-remove-tags'.
1111 Custom commands can set this variable in the options section."
1112 :type '(choice
1113 (string :tag "General format")
1114 (list :greedy t :tag "View dependent"
1115 (cons (const agenda) (string :tag "Format"))
1116 (cons (const timeline) (string :tag "Format"))
1117 (cons (const todo) (string :tag "Format"))
1118 (cons (const tags) (string :tag "Format"))
1119 (cons (const search) (string :tag "Format"))))
1120 :group 'org-agenda-line-format)
1122 (defvar org-prefix-format-compiled nil
1123 "The compiled version of the most recently used prefix format.
1124 See the variable `org-agenda-prefix-format'.")
1126 (defcustom org-agenda-todo-keyword-format "%-1s"
1127 "Format for the TODO keyword in agenda lines.
1128 Set this to something like \"%-12s\" if you want all TODO keywords
1129 to occupy a fixed space in the agenda display."
1130 :group 'org-agenda-line-format
1131 :type 'string)
1133 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1134 "Text preceding timerange entries in the agenda view.
1135 This is a list with two strings. The first applies when the range
1136 is entirely on one day. The second applies if the range spans several days.
1137 The strings may have two \"%d\" format specifiers which will be filled
1138 with the sequence number of the days, and the total number of days in the
1139 range, respectively."
1140 :group 'org-agenda-line-format
1141 :type '(list
1142 (string :tag "Deadline today ")
1143 (choice :tag "Deadline relative"
1144 (string :tag "Format string")
1145 (function))))
1147 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1148 "Text preceeding scheduled items in the agenda view.
1149 This is a list with two strings. The first applies when the item is
1150 scheduled on the current day. The second applies when it has been scheduled
1151 previously, it may contain a %d indicating that this is the nth time that
1152 this item is scheduled, due to automatic rescheduling of unfinished items
1153 for the following day. So this number is one larger than the number of days
1154 that passed since this item was scheduled first."
1155 :group 'org-agenda-line-format
1156 :type '(list
1157 (string :tag "Scheduled today ")
1158 (string :tag "Scheduled previously")))
1160 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1161 "Text preceeding deadline items in the agenda view.
1162 This is a list with two strings. The first applies when the item has its
1163 deadline on the current day. The second applies when it is in the past or
1164 in the future, it may contain %d to capture how many days away the deadline
1165 is (was)."
1166 :group 'org-agenda-line-format
1167 :type '(list
1168 (string :tag "Deadline today ")
1169 (choice :tag "Deadline relative"
1170 (string :tag "Format string")
1171 (function))))
1173 (defcustom org-agenda-remove-times-when-in-prefix t
1174 "Non-nil means, remove duplicate time specifications in agenda items.
1175 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1176 time-of-day specification in a headline or diary entry is extracted and
1177 placed into the prefix. If this option is non-nil, the original specification
1178 \(a timestamp or -range, or just a plain time(range) specification like
1179 11:30-4pm) will be removed for agenda display. This makes the agenda less
1180 cluttered.
1181 The option can be t or nil. It may also be the symbol `beg', indicating
1182 that the time should only be removed what it is located at the beginning of
1183 the headline/diary entry."
1184 :group 'org-agenda-line-format
1185 :type '(choice
1186 (const :tag "Always" t)
1187 (const :tag "Never" nil)
1188 (const :tag "When at beginning of entry" beg)))
1191 (defcustom org-agenda-default-appointment-duration nil
1192 "Default duration for appointments that only have a starting time.
1193 When nil, no duration is specified in such cases.
1194 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1195 :group 'org-agenda-line-format
1196 :type '(choice
1197 (integer :tag "Minutes")
1198 (const :tag "No default duration")))
1200 (defcustom org-agenda-show-inherited-tags t
1201 "Non-nil means, show inherited tags in each agenda line."
1202 :group 'org-agenda-line-format
1203 :type 'boolean)
1205 (defcustom org-agenda-remove-tags nil
1206 "Non-nil means, remove the tags from the headline copy in the agenda.
1207 When this is the symbol `prefix', only remove tags when
1208 `org-agenda-prefix-format' contains a `%T' specifier."
1209 :group 'org-agenda-line-format
1210 :type '(choice
1211 (const :tag "Always" t)
1212 (const :tag "Never" nil)
1213 (const :tag "When prefix format contains %T" prefix)))
1215 (if (fboundp 'defvaralias)
1216 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1217 'org-agenda-remove-tags))
1219 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1220 "Shift tags in agenda items to this column.
1221 If this number is positive, it specifies the column. If it is negative,
1222 it means that the tags should be flushright to that column. For example,
1223 -80 works well for a normal 80 character screen."
1224 :group 'org-agenda-line-format
1225 :type 'integer)
1227 (if (fboundp 'defvaralias)
1228 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1230 (defcustom org-agenda-fontify-priorities 'cookies
1231 "Non-nil means, highlight low and high priorities in agenda.
1232 When t, the highest priority entries are bold, lowest priority italic.
1233 However, settings in org-priority-faces will overrule these faces.
1234 When this variable is the symbol `cookies', only fontify the
1235 cookies, not the entire task.
1236 This may also be an association list of priority faces, whose
1237 keys are the character values of `org-highest-priority',
1238 `org-default-priority', and `org-lowest-priority' (the default values
1239 are ?A, ?B, and ?C, respectively). The face may be a named face,
1240 or a list like `(:background \"Red\")'."
1241 :group 'org-agenda-line-format
1242 :type '(choice
1243 (const :tag "Never" nil)
1244 (const :tag "Defaults" t)
1245 (const :tag "Cookies only" cookies)
1246 (repeat :tag "Specify"
1247 (list (character :tag "Priority" :value ?A)
1248 (sexp :tag "face")))))
1250 (defgroup org-agenda-column-view nil
1251 "Options concerning column view in the agenda."
1252 :tag "Org Agenda Column View"
1253 :group 'org-agenda)
1255 (defcustom org-agenda-columns-show-summaries t
1256 "Non-nil means, show summaries for columns displayed in the agenda view."
1257 :group 'org-agenda-column-view
1258 :type 'boolean)
1260 (defcustom org-agenda-columns-remove-prefix-from-item t
1261 "Non-nil means, remove the prefix from a headline for agenda column view.
1262 The special ITEM field in the columns format contains the current line, with
1263 all information shown in other columns (like the TODO state or a tag).
1264 When this variable is non-nil, also the agenda prefix will be removed from
1265 the content of the ITEM field, to make sure as much as possible of the
1266 headline can be shown in the limited width of the field."
1267 :group 'org-agenda
1268 :type 'boolean)
1270 (defcustom org-agenda-columns-compute-summary-properties t
1271 "Non-nil means, recompute all summary properties before column view.
1272 When column view in the agenda is listing properties that have a summary
1273 operator, it can go to all relevant buffers and recompute the summaries
1274 there. This can mean overhead for the agenda column view, but is necessary
1275 to have thing up to date.
1276 As a special case, a CLOCKSUM property also makes sure that the clock
1277 computations are current."
1278 :group 'org-agenda-column-view
1279 :type 'boolean)
1281 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1282 "Non-nil means, the duration of an appointment will add to day effort.
1283 The property to which appointment durations will be added is the one given
1284 in the option `org-effort-property'. If an appointment does not have
1285 an end time, `org-agenda-default-appointment-duration' will be used. If that
1286 is not set, an appointment without end time will not contribute to the time
1287 estimate."
1288 :group 'org-agenda-column-view
1289 :type 'boolean)
1291 (defcustom org-agenda-auto-exclude-function nil
1292 "A function called with a tag to decide if it is filtered on '/ RET'.
1293 The sole argument to the function, which is called once for each
1294 possible tag, is a string giving the name of the tag. The
1295 function should return either nil if the tag should be included
1296 as normal, or \"-<TAG>\" to exclude the tag."
1297 :group 'org-agenda
1298 :type 'function)
1300 (eval-when-compile
1301 (require 'cl))
1302 (require 'org)
1304 (defun org-add-agenda-custom-command (entry)
1305 "Replace or add a command in `org-agenda-custom-commands'.
1306 This is mostly for hacking and trying a new command - once the command
1307 works you probably want to add it to `org-agenda-custom-commands' for good."
1308 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1309 (if ass
1310 (setcdr ass (cdr entry))
1311 (push entry org-agenda-custom-commands))))
1313 ;;; Define the Org-agenda-mode
1315 (defvar org-agenda-mode-map (make-sparse-keymap)
1316 "Keymap for `org-agenda-mode'.")
1317 (if (fboundp 'defvaralias)
1318 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1320 (defvar org-agenda-menu) ; defined later in this file.
1321 (defvar org-agenda-restrict) ; defined later in this file.
1322 (defvar org-agenda-follow-mode nil)
1323 (defvar org-agenda-entry-text-mode nil)
1324 (defvar org-agenda-clockreport-mode nil)
1325 (defvar org-agenda-show-log nil)
1326 (defvar org-agenda-redo-command nil)
1327 (defvar org-agenda-query-string nil)
1328 (defvar org-agenda-mode-hook nil
1329 "Hook for org-agenda-mode, run after the mode is turned on.")
1330 (defvar org-agenda-type nil)
1331 (defvar org-agenda-force-single-file nil)
1332 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1334 (defun org-agenda-mode ()
1335 "Mode for time-sorted view on action items in Org-mode files.
1337 The following commands are available:
1339 \\{org-agenda-mode-map}"
1340 (interactive)
1341 (kill-all-local-variables)
1342 (setq org-agenda-undo-list nil
1343 org-agenda-pending-undo-list nil
1344 org-agenda-bulk-marked-entries nil)
1345 (setq major-mode 'org-agenda-mode)
1346 ;; Keep global-font-lock-mode from turning on font-lock-mode
1347 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1348 (setq mode-name "Org-Agenda")
1349 (use-local-map org-agenda-mode-map)
1350 (easy-menu-add org-agenda-menu)
1351 (if org-startup-truncated (setq truncate-lines t))
1352 (org-set-local 'line-move-visual nil)
1353 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1354 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1355 ;; Make sure properties are removed when copying text
1356 (when (boundp 'buffer-substring-filters)
1357 (org-set-local 'buffer-substring-filters
1358 (cons (lambda (x)
1359 (set-text-properties 0 (length x) nil x) x)
1360 buffer-substring-filters)))
1361 (unless org-agenda-keep-modes
1362 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1363 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1364 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1365 org-agenda-show-log org-agenda-start-with-log-mode))
1367 (easy-menu-change
1368 '("Agenda") "Agenda Files"
1369 (append
1370 (list
1371 (vector
1372 (if (get 'org-agenda-files 'org-restrict)
1373 "Restricted to single file"
1374 "Edit File List")
1375 '(org-edit-agenda-file-list)
1376 (not (get 'org-agenda-files 'org-restrict)))
1377 "--")
1378 (mapcar 'org-file-menu-entry (org-agenda-files))))
1379 (org-agenda-set-mode-name)
1380 (apply
1381 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1382 (list 'org-agenda-mode-hook)))
1384 (substitute-key-definition 'undo 'org-agenda-undo
1385 org-agenda-mode-map global-map)
1386 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1387 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1388 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1389 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1390 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1391 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1392 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1393 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1394 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1395 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1396 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1397 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1398 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1399 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1400 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1401 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1402 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1403 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1404 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1405 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1406 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1407 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1408 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1409 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1410 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1411 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1412 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1413 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1414 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1415 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1416 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1417 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1418 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1419 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1420 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1421 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1422 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1423 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1424 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1425 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1426 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1427 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1428 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1430 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1431 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1432 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1433 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1434 (while l (org-defkey org-agenda-mode-map
1435 (int-to-string (pop l)) 'digit-argument)))
1437 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1438 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1439 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1440 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1441 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1442 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1443 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1444 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1445 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1446 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1447 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1448 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1449 'org-clock-modify-effort-estimate)
1450 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1451 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1452 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1453 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1454 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1455 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1456 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1457 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1458 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1459 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1460 (substitute-key-definition 'next-line 'org-agenda-next-line
1461 org-agenda-mode-map global-map)
1462 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1463 org-agenda-mode-map global-map)
1464 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1465 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1466 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1467 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1468 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1469 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1470 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1471 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1472 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1473 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1474 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1475 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1476 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1477 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1478 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1479 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1480 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1481 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1482 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1483 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1484 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1485 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1486 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1487 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1488 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1489 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1490 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1491 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1492 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1493 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1495 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1496 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1497 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1498 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1499 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1500 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1501 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1502 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1503 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1504 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1506 (org-defkey org-agenda-mode-map
1507 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1508 (org-defkey org-agenda-mode-map
1509 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1510 (when org-agenda-mouse-1-follows-link
1511 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1512 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1513 '("Agenda"
1514 ("Agenda Files")
1515 "--"
1516 ("Agenda Dates"
1517 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1518 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1519 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1520 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1521 "--"
1522 ("View"
1523 ["Day View" org-agenda-day-view
1524 :active (org-agenda-check-type nil 'agenda)
1525 :style radio :selected (equal org-agenda-ndays 1)
1526 :keys "v d (or just d)"]
1527 ["Week View" org-agenda-week-view
1528 :active (org-agenda-check-type nil 'agenda)
1529 :style radio :selected (equal org-agenda-ndays 7)
1530 :keys "v w (or just w)"]
1531 ["Month View" org-agenda-month-view
1532 :active (org-agenda-check-type nil 'agenda)
1533 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1534 :keys "v m"]
1535 ["Year View" org-agenda-year-view
1536 :active (org-agenda-check-type nil 'agenda)
1537 :style radio :selected (member org-agenda-ndays '(365 366))
1538 :keys "v y"]
1539 "--"
1540 ["Include Diary" org-agenda-toggle-diary
1541 :style toggle :selected org-agenda-include-diary
1542 :active (org-agenda-check-type nil 'agenda)]
1543 ["Use Time Grid" org-agenda-toggle-time-grid
1544 :style toggle :selected org-agenda-use-time-grid
1545 :active (org-agenda-check-type nil 'agenda)]
1546 "--"
1547 ["Show clock report" org-agenda-clockreport-mode
1548 :style toggle :selected org-agenda-clockreport-mode
1549 :active (org-agenda-check-type nil 'agenda)]
1550 ["Show some entry text" org-agenda-entry-text-mode
1551 :style toggle :selected org-agenda-entry-text-mode
1552 :active t]
1553 "--"
1554 ["Show Logbook entries" org-agenda-log-mode
1555 :style toggle :selected org-agenda-show-log
1556 :active (org-agenda-check-type nil 'agenda 'timeline)
1557 :keys "v l (or just l)"]
1558 ["Include archived trees" org-agenda-archives-mode
1559 :style toggle :selected org-agenda-archives-mode :active t
1560 :keys "v a"]
1561 ["Include archive files" (org-agenda-archives-mode t)
1562 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1563 :keys "v A"]
1564 "--"
1565 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1566 ["Write view to file" org-write-agenda t]
1567 ["Rebuild buffer" org-agenda-redo t]
1568 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1569 "--"
1570 ["Show original entry" org-agenda-show t]
1571 ["Go To (other window)" org-agenda-goto t]
1572 ["Go To (this window)" org-agenda-switch-to t]
1573 ["Follow Mode" org-agenda-follow-mode
1574 :style toggle :selected org-agenda-follow-mode :active t]
1575 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1576 "--"
1577 ("TODO"
1578 ["Cycle TODO" org-agenda-todo t]
1579 ["Next TODO set" org-agenda-todo-nextset t]
1580 ["Previous TODO set" org-agenda-todo-previousset t]
1581 ["Add note" org-agenda-add-note t])
1582 ("Archive/Refile/Delete"
1583 ["Archive default" org-agenda-archive-default t]
1584 ["Archive default" org-agenda-archive-default-with-confirmation t]
1585 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1586 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1587 ["Archive subtree" org-agenda-archive t]
1588 "--"
1589 ["Refile" org-agenda-refile t]
1590 "--"
1591 ["Delete subtree" org-agenda-kill t])
1592 ("Bulk action"
1593 ["Mark entry" org-agenda-bulk-mark t]
1594 ["Unmark entry" org-agenda-bulk-unmark t]
1595 ["Act on all marked" org-agenda-bulk-action t]
1596 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1597 "--"
1598 ("Tags and Properties"
1599 ["Show all Tags" org-agenda-show-tags t]
1600 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1601 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1602 "--"
1603 ["Column View" org-columns t])
1604 ("Deadline/Schedule"
1605 ["Schedule" org-agenda-schedule t]
1606 ["Set Deadline" org-agenda-deadline t]
1607 "--"
1608 ["Mark item" org-agenda-action :active t :keys "k m"]
1609 ["Show mark item" org-agenda-action :active t :keys "k v"]
1610 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1611 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1612 "--"
1613 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1614 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1615 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1616 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1617 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1618 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1619 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1620 ("Clock and Effort"
1621 ["Clock in" org-agenda-clock-in t]
1622 ["Clock out" org-agenda-clock-out t]
1623 ["Clock cancel" org-agenda-clock-cancel t]
1624 ["Goto running clock" org-clock-goto t]
1625 "--"
1626 ["Set Effort" org-agenda-set-effort t]
1627 ["Change clocked effort" org-clock-modify-effort-estimate
1628 (org-clock-is-active)])
1629 ("Priority"
1630 ["Set Priority" org-agenda-priority t]
1631 ["Increase Priority" org-agenda-priority-up t]
1632 ["Decrease Priority" org-agenda-priority-down t]
1633 ["Show Priority" org-agenda-show-priority t])
1634 ("Calendar/Diary"
1635 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1636 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1637 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1638 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1639 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1640 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1641 "--"
1642 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
1643 "--"
1644 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1645 "--"
1646 ("MobileOrg"
1647 ["Push Files and Views" org-mobile-push t]
1648 ["Get Captured and Flagged" org-mobile-pull t]
1649 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1650 ["Show note / unflag" org-agenda-show-the-flagging-note t]
1651 "--"
1652 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
1653 "--"
1654 ["Quit" org-agenda-quit t]
1655 ["Exit and Release Buffers" org-agenda-exit t]
1658 ;;; Agenda undo
1660 (defvar org-agenda-allow-remote-undo t
1661 "Non-nil means, allow remote undo from the agenda buffer.")
1662 (defvar org-agenda-undo-list nil
1663 "List of undoable operations in the agenda since last refresh.")
1664 (defvar org-agenda-undo-has-started-in nil
1665 "Buffers that have already seen `undo-start' in the current undo sequence.")
1666 (defvar org-agenda-pending-undo-list nil
1667 "In a series of undo commands, this is the list of remaining undo items.")
1670 (defun org-agenda-undo ()
1671 "Undo a remote editing step in the agenda.
1672 This undoes changes both in the agenda buffer and in the remote buffer
1673 that have been changed along."
1674 (interactive)
1675 (or org-agenda-allow-remote-undo
1676 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
1677 (if (not (eq this-command last-command))
1678 (setq org-agenda-undo-has-started-in nil
1679 org-agenda-pending-undo-list org-agenda-undo-list))
1680 (if (not org-agenda-pending-undo-list)
1681 (error "No further undo information"))
1682 (let* ((entry (pop org-agenda-pending-undo-list))
1683 buf line cmd rembuf)
1684 (setq cmd (pop entry) line (pop entry))
1685 (setq rembuf (nth 2 entry))
1686 (org-with-remote-undo rembuf
1687 (while (bufferp (setq buf (pop entry)))
1688 (if (pop entry)
1689 (with-current-buffer buf
1690 (let ((last-undo-buffer buf)
1691 (inhibit-read-only t))
1692 (unless (memq buf org-agenda-undo-has-started-in)
1693 (push buf org-agenda-undo-has-started-in)
1694 (make-local-variable 'pending-undo-list)
1695 (undo-start))
1696 (while (and pending-undo-list
1697 (listp pending-undo-list)
1698 (not (car pending-undo-list)))
1699 (pop pending-undo-list))
1700 (undo-more 1))))))
1701 (org-goto-line line)
1702 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1704 (defun org-verify-change-for-undo (l1 l2)
1705 "Verify that a real change occurred between the undo lists L1 and L2."
1706 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1707 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1708 (not (eq l1 l2)))
1710 ;;; Agenda dispatch
1712 (defvar org-agenda-restrict nil)
1713 (defvar org-agenda-restrict-begin (make-marker))
1714 (defvar org-agenda-restrict-end (make-marker))
1715 (defvar org-agenda-last-dispatch-buffer nil)
1716 (defvar org-agenda-overriding-restriction nil)
1718 ;;;###autoload
1719 (defun org-agenda (&optional arg keys restriction)
1720 "Dispatch agenda commands to collect entries to the agenda buffer.
1721 Prompts for a command to execute. Any prefix arg will be passed
1722 on to the selected command. The default selections are:
1724 a Call `org-agenda-list' to display the agenda for current day or week.
1725 t Call `org-todo-list' to display the global todo list.
1726 T Call `org-todo-list' to display the global todo list, select only
1727 entries with a specific TODO keyword (the user gets a prompt).
1728 m Call `org-tags-view' to display headlines with tags matching
1729 a condition (the user is prompted for the condition).
1730 M Like `m', but select only TODO entries, no ordinary headlines.
1731 L Create a timeline for the current buffer.
1732 e Export views to associated files.
1733 s Search entries for keywords.
1734 / Multi occur across all agenda files and also files listed
1735 in `org-agenda-text-search-extra-files'.
1736 < Restrict agenda commands to buffer, subtree, or region.
1737 Press several times to get the desired effect.
1738 > Remove a previous restriction.
1739 # List \"stuck\" projects.
1740 ! Configure what \"stuck\" means.
1741 C Configure custom agenda commands.
1743 More commands can be added by configuring the variable
1744 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1745 searches can be pre-defined in this way.
1747 If the current buffer is in Org-mode and visiting a file, you can also
1748 first press `<' once to indicate that the agenda should be temporarily
1749 \(until the next use of \\[org-agenda]) restricted to the current file.
1750 Pressing `<' twice means to restrict to the current subtree or region
1751 \(if active)."
1752 (interactive "P")
1753 (catch 'exit
1754 (let* ((prefix-descriptions nil)
1755 (org-agenda-window-setup (if (equal (buffer-name)
1756 org-agenda-buffer-name)
1757 'current-window
1758 org-agenda-window-setup))
1759 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1760 (org-agenda-custom-commands
1761 ;; normalize different versions
1762 (delq nil
1763 (mapcar
1764 (lambda (x)
1765 (cond ((stringp (cdr x))
1766 (push x prefix-descriptions)
1767 nil)
1768 ((stringp (nth 1 x)) x)
1769 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1770 (t (cons (car x) (cons "" (cdr x))))))
1771 org-agenda-custom-commands)))
1772 (buf (current-buffer))
1773 (bfn (buffer-file-name (buffer-base-buffer)))
1774 entry key type match lprops ans)
1775 ;; Turn off restriction unless there is an overriding one,
1776 (unless org-agenda-overriding-restriction
1777 (unless (org-bound-and-true-p org-agenda-keep-restriced-file-list)
1778 ;; There is a request to keep the file list in place
1779 (put 'org-agenda-files 'org-restrict nil))
1780 (setq org-agenda-restrict nil)
1781 (move-marker org-agenda-restrict-begin nil)
1782 (move-marker org-agenda-restrict-end nil))
1783 ;; Delete old local properties
1784 (put 'org-agenda-redo-command 'org-lprops nil)
1785 ;; Remember where this call originated
1786 (setq org-agenda-last-dispatch-buffer (current-buffer))
1787 (unless keys
1788 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1789 keys (car ans)
1790 restriction (cdr ans)))
1791 ;; Estabish the restriction, if any
1792 (when (and (not org-agenda-overriding-restriction) restriction)
1793 (put 'org-agenda-files 'org-restrict (list bfn))
1794 (cond
1795 ((eq restriction 'region)
1796 (setq org-agenda-restrict t)
1797 (move-marker org-agenda-restrict-begin (region-beginning))
1798 (move-marker org-agenda-restrict-end (region-end)))
1799 ((eq restriction 'subtree)
1800 (save-excursion
1801 (setq org-agenda-restrict t)
1802 (org-back-to-heading t)
1803 (move-marker org-agenda-restrict-begin (point))
1804 (move-marker org-agenda-restrict-end
1805 (progn (org-end-of-subtree t)))))))
1807 (require 'calendar) ; FIXME: can we avoid this for some commands?
1808 ;; For example the todo list should not need it (but does...)
1809 (cond
1810 ((setq entry (assoc keys org-agenda-custom-commands))
1811 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1812 (progn
1813 (setq type (nth 2 entry) match (eval (nth 3 entry))
1814 lprops (nth 4 entry))
1815 (put 'org-agenda-redo-command 'org-lprops lprops)
1816 (cond
1817 ((eq type 'agenda)
1818 (org-let lprops '(org-agenda-list current-prefix-arg)))
1819 ((eq type 'alltodo)
1820 (org-let lprops '(org-todo-list current-prefix-arg)))
1821 ((eq type 'search)
1822 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1823 ((eq type 'stuck)
1824 (org-let lprops '(org-agenda-list-stuck-projects
1825 current-prefix-arg)))
1826 ((eq type 'tags)
1827 (org-let lprops '(org-tags-view current-prefix-arg match)))
1828 ((eq type 'tags-todo)
1829 (org-let lprops '(org-tags-view '(4) match)))
1830 ((eq type 'todo)
1831 (org-let lprops '(org-todo-list match)))
1832 ((eq type 'tags-tree)
1833 (org-check-for-org-mode)
1834 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1835 ((eq type 'todo-tree)
1836 (org-check-for-org-mode)
1837 (org-let lprops
1838 '(org-occur (concat "^" outline-regexp "[ \t]*"
1839 (regexp-quote match) "\\>"))))
1840 ((eq type 'occur-tree)
1841 (org-check-for-org-mode)
1842 (org-let lprops '(org-occur match)))
1843 ((functionp type)
1844 (org-let lprops '(funcall type match)))
1845 ((fboundp type)
1846 (org-let lprops '(funcall type match)))
1847 (t (error "Invalid custom agenda command type %s" type))))
1848 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1849 ((equal keys "C")
1850 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1851 (customize-variable 'org-agenda-custom-commands))
1852 ((equal keys "a") (call-interactively 'org-agenda-list))
1853 ((equal keys "s") (call-interactively 'org-search-view))
1854 ((equal keys "t") (call-interactively 'org-todo-list))
1855 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1856 ((equal keys "m") (call-interactively 'org-tags-view))
1857 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1858 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1859 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1860 (org-add-hook
1861 'post-command-hook
1862 (lambda ()
1863 (unless (current-message)
1864 (let* ((m (org-agenda-get-any-marker))
1865 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1866 (when note
1867 (message (concat
1868 "FLAGGING-NOTE ([?] for more info): "
1869 (org-add-props
1870 (replace-regexp-in-string
1871 "\\\\n" "//"
1872 (copy-sequence note))
1873 nil 'face 'org-warning)))))))
1874 t t))
1875 ((equal keys "L")
1876 (unless (org-mode-p)
1877 (error "This is not an Org-mode file"))
1878 (unless restriction
1879 (put 'org-agenda-files 'org-restrict (list bfn))
1880 (org-call-with-arg 'org-timeline arg)))
1881 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1882 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1883 ((equal keys "!") (customize-variable 'org-stuck-projects))
1884 (t (error "Invalid agenda key"))))))
1886 (defun org-agenda-normalize-custom-commands (cmds)
1887 (delq nil
1888 (mapcar
1889 (lambda (x)
1890 (cond ((stringp (cdr x)) nil)
1891 ((stringp (nth 1 x)) x)
1892 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1893 (t (cons (car x) (cons "" (cdr x))))))
1894 cmds)))
1896 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1897 "The user interface for selecting an agenda command."
1898 (catch 'exit
1899 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1900 (restrict-ok (and bfn (org-mode-p)))
1901 (region-p (org-region-active-p))
1902 (custom org-agenda-custom-commands)
1903 (selstring "")
1904 restriction second-time
1905 c entry key type match prefixes rmheader header-end custom1 desc)
1906 (save-window-excursion
1907 (delete-other-windows)
1908 (org-switch-to-buffer-other-window " *Agenda Commands*")
1909 (erase-buffer)
1910 (insert (eval-when-compile
1911 (let ((header
1913 Press key for an agenda command: < Buffer, subtree/region restriction
1914 -------------------------------- > Remove restriction
1915 a Agenda for current week or day e Export agenda views
1916 t List of all TODO entries T Entries with special TODO kwd
1917 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1918 L Timeline for current buffer # List stuck projects (!=configure)
1919 s Search for keywords C Configure custom agenda commands
1920 / Multi-occur ? Find :FLAGGED: entries
1922 (start 0))
1923 (while (string-match
1924 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1925 header start)
1926 (setq start (match-end 0))
1927 (add-text-properties (match-beginning 2) (match-end 2)
1928 '(face bold) header))
1929 header)))
1930 (setq header-end (move-marker (make-marker) (point)))
1931 (while t
1932 (setq custom1 custom)
1933 (when (eq rmheader t)
1934 (org-goto-line 1)
1935 (re-search-forward ":" nil t)
1936 (delete-region (match-end 0) (point-at-eol))
1937 (forward-char 1)
1938 (looking-at "-+")
1939 (delete-region (match-end 0) (point-at-eol))
1940 (move-marker header-end (match-end 0)))
1941 (goto-char header-end)
1942 (delete-region (point) (point-max))
1943 (while (setq entry (pop custom1))
1944 (setq key (car entry) desc (nth 1 entry)
1945 type (nth 2 entry)
1946 match (nth 3 entry))
1947 (if (> (length key) 1)
1948 (add-to-list 'prefixes (string-to-char key))
1949 (insert
1950 (format
1951 "\n%-4s%-14s: %s"
1952 (org-add-props (copy-sequence key)
1953 '(face bold))
1954 (cond
1955 ((string-match "\\S-" desc) desc)
1956 ((eq type 'agenda) "Agenda for current week or day")
1957 ((eq type 'alltodo) "List of all TODO entries")
1958 ((eq type 'search) "Word search")
1959 ((eq type 'stuck) "List of stuck projects")
1960 ((eq type 'todo) "TODO keyword")
1961 ((eq type 'tags) "Tags query")
1962 ((eq type 'tags-todo) "Tags (TODO)")
1963 ((eq type 'tags-tree) "Tags tree")
1964 ((eq type 'todo-tree) "TODO kwd tree")
1965 ((eq type 'occur-tree) "Occur tree")
1966 ((functionp type) (if (symbolp type)
1967 (symbol-name type)
1968 "Lambda expression"))
1969 (t "???"))
1970 (cond
1971 ((stringp match)
1972 (setq match (copy-sequence match))
1973 (org-add-props match nil 'face 'org-warning))
1974 (match
1975 (format "set of %d commands" (length match)))
1976 (t ""))))))
1977 (when prefixes
1978 (mapc (lambda (x)
1979 (insert
1980 (format "\n%s %s"
1981 (org-add-props (char-to-string x)
1982 nil 'face 'bold)
1983 (or (cdr (assoc (concat selstring (char-to-string x))
1984 prefix-descriptions))
1985 "Prefix key"))))
1986 prefixes))
1987 (goto-char (point-min))
1988 (if second-time
1989 (if (not (pos-visible-in-window-p (point-max)))
1990 (org-fit-window-to-buffer))
1991 (setq second-time t)
1992 (org-fit-window-to-buffer))
1993 (message "Press key for agenda command%s:"
1994 (if (or restrict-ok org-agenda-overriding-restriction)
1995 (if org-agenda-overriding-restriction
1996 " (restriction lock active)"
1997 (if restriction
1998 (format " (restricted to %s)" restriction)
1999 " (unrestricted)"))
2000 ""))
2001 (setq c (read-char-exclusive))
2002 (message "")
2003 (cond
2004 ((assoc (char-to-string c) custom)
2005 (setq selstring (concat selstring (char-to-string c)))
2006 (throw 'exit (cons selstring restriction)))
2007 ((memq c prefixes)
2008 (setq selstring (concat selstring (char-to-string c))
2009 prefixes nil
2010 rmheader (or rmheader t)
2011 custom (delq nil (mapcar
2012 (lambda (x)
2013 (if (or (= (length (car x)) 1)
2014 (/= (string-to-char (car x)) c))
2016 (cons (substring (car x) 1) (cdr x))))
2017 custom))))
2018 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2019 (message "Restriction is only possible in Org-mode buffers")
2020 (ding) (sit-for 1))
2021 ((eq c ?1)
2022 (org-agenda-remove-restriction-lock 'noupdate)
2023 (setq restriction 'buffer))
2024 ((eq c ?0)
2025 (org-agenda-remove-restriction-lock 'noupdate)
2026 (setq restriction (if region-p 'region 'subtree)))
2027 ((eq c ?<)
2028 (org-agenda-remove-restriction-lock 'noupdate)
2029 (setq restriction
2030 (cond
2031 ((eq restriction 'buffer)
2032 (if region-p 'region 'subtree))
2033 ((memq restriction '(subtree region))
2034 nil)
2035 (t 'buffer))))
2036 ((eq c ?>)
2037 (org-agenda-remove-restriction-lock 'noupdate)
2038 (setq restriction nil))
2039 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2040 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2041 ((and (> (length selstring) 0) (eq c ?\d))
2042 (delete-window)
2043 (org-agenda-get-restriction-and-command prefix-descriptions))
2045 ((equal c ?q) (error "Abort"))
2046 (t (error "Invalid key %c" c))))))))
2048 (defun org-run-agenda-series (name series)
2049 (org-let (nth 1 series) '(org-prepare-agenda name))
2050 (let* ((org-agenda-multi t)
2051 (redo (list 'org-run-agenda-series name (list 'quote series)))
2052 (cmds (car series))
2053 (gprops (nth 1 series))
2054 match ;; The byte compiler incorrectly complains about this. Keep it!
2055 cmd type lprops)
2056 (while (setq cmd (pop cmds))
2057 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2058 (cond
2059 ((eq type 'agenda)
2060 (org-let2 gprops lprops
2061 '(call-interactively 'org-agenda-list)))
2062 ((eq type 'alltodo)
2063 (org-let2 gprops lprops
2064 '(call-interactively 'org-todo-list)))
2065 ((eq type 'search)
2066 (org-let2 gprops lprops
2067 '(org-search-view current-prefix-arg match nil)))
2068 ((eq type 'stuck)
2069 (org-let2 gprops lprops
2070 '(call-interactively 'org-agenda-list-stuck-projects)))
2071 ((eq type 'tags)
2072 (org-let2 gprops lprops
2073 '(org-tags-view current-prefix-arg match)))
2074 ((eq type 'tags-todo)
2075 (org-let2 gprops lprops
2076 '(org-tags-view '(4) match)))
2077 ((eq type 'todo)
2078 (org-let2 gprops lprops
2079 '(org-todo-list match)))
2080 ((fboundp type)
2081 (org-let2 gprops lprops
2082 '(funcall type match)))
2083 (t (error "Invalid type in command series"))))
2084 (widen)
2085 (setq org-agenda-redo-command redo)
2086 (goto-char (point-min)))
2087 (org-fit-agenda-window)
2088 (org-let (nth 1 series) '(org-finalize-agenda)))
2090 ;;;###autoload
2091 (defmacro org-batch-agenda (cmd-key &rest parameters)
2092 "Run an agenda command in batch mode and send the result to STDOUT.
2093 If CMD-KEY is a string of length 1, it is used as a key in
2094 `org-agenda-custom-commands' and triggers this command. If it is a
2095 longer string it is used as a tags/todo match string.
2096 Paramters are alternating variable names and values that will be bound
2097 before running the agenda command."
2098 (let (pars)
2099 (while parameters
2100 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2101 (if (> (length cmd-key) 2)
2102 (eval (list 'let (nreverse pars)
2103 (list 'org-tags-view nil cmd-key)))
2104 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2105 (set-buffer org-agenda-buffer-name)
2106 (princ (org-encode-for-stdout (buffer-string)))))
2108 ;(defun org-encode-for-stdout (string)
2109 ; (if (fboundp 'encode-coding-string)
2110 ; (encode-coding-string string buffer-file-coding-system)
2111 ; string))
2113 (defun org-encode-for-stdout (string)
2114 string)
2116 (defvar org-agenda-info nil)
2118 ;;;###autoload
2119 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2120 "Run an agenda command in batch mode and send the result to STDOUT.
2121 If CMD-KEY is a string of length 1, it is used as a key in
2122 `org-agenda-custom-commands' and triggers this command. If it is a
2123 longer string it is used as a tags/todo match string.
2124 Paramters are alternating variable names and values that will be bound
2125 before running the agenda command.
2127 The output gives a line for each selected agenda item. Each
2128 item is a list of comma-separated values, like this:
2130 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2132 category The category of the item
2133 head The headline, without TODO kwd, TAGS and PRIORITY
2134 type The type of the agenda entry, can be
2135 todo selected in TODO match
2136 tagsmatch selected in tags match
2137 diary imported from diary
2138 deadline a deadline on given date
2139 scheduled scheduled on given date
2140 timestamp entry has timestamp on given date
2141 closed entry was closed on given date
2142 upcoming-deadline warning about deadline
2143 past-scheduled forwarded scheduled item
2144 block entry has date block including g. date
2145 todo The todo keyword, if any
2146 tags All tags including inherited ones, separated by colons
2147 date The relevant date, like 2007-2-14
2148 time The time, like 15:00-16:50
2149 extra Sting with extra planning info
2150 priority-l The priority letter if any was given
2151 priority-n The computed numerical priority
2152 agenda-day The day in the agenda where this is listed"
2154 (let (pars)
2155 (while parameters
2156 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2157 (push (list 'org-agenda-remove-tags t) pars)
2158 (if (> (length cmd-key) 2)
2159 (eval (list 'let (nreverse pars)
2160 (list 'org-tags-view nil cmd-key)))
2161 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2162 (set-buffer org-agenda-buffer-name)
2163 (let* ((lines (org-split-string (buffer-string) "\n"))
2164 line)
2165 (while (setq line (pop lines))
2166 (catch 'next
2167 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2168 (setq org-agenda-info
2169 (org-fix-agenda-info (text-properties-at 0 line)))
2170 (princ
2171 (org-encode-for-stdout
2172 (mapconcat 'org-agenda-export-csv-mapper
2173 '(org-category txt type todo tags date time-of-day extra
2174 priority-letter priority agenda-day)
2175 ",")))
2176 (princ "\n"))))))
2178 (defun org-fix-agenda-info (props)
2179 "Make sure all properties on an agenda item have a canonical form,
2180 so the export commands can easily use it."
2181 (let (tmp re)
2182 (when (setq tmp (plist-get props 'tags))
2183 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2184 (when (setq tmp (plist-get props 'date))
2185 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2186 (let ((calendar-date-display-form '(year "-" month "-" day)))
2187 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2189 (setq tmp (calendar-date-string tmp)))
2190 (setq props (plist-put props 'date tmp)))
2191 (when (setq tmp (plist-get props 'day))
2192 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2193 (let ((calendar-date-display-form '(year "-" month "-" day)))
2194 (setq tmp (calendar-date-string tmp)))
2195 (setq props (plist-put props 'day tmp))
2196 (setq props (plist-put props 'agenda-day tmp)))
2197 (when (setq tmp (plist-get props 'txt))
2198 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2199 (plist-put props 'priority-letter (match-string 1 tmp))
2200 (setq tmp (replace-match "" t t tmp)))
2201 (when (and (setq re (plist-get props 'org-todo-regexp))
2202 (setq re (concat "\\`\\.*" re " ?"))
2203 (string-match re tmp))
2204 (plist-put props 'todo (match-string 1 tmp))
2205 (setq tmp (replace-match "" t t tmp)))
2206 (plist-put props 'txt tmp)))
2207 props)
2209 (defun org-agenda-export-csv-mapper (prop)
2210 (let ((res (plist-get org-agenda-info prop)))
2211 (setq res
2212 (cond
2213 ((not res) "")
2214 ((stringp res) res)
2215 (t (prin1-to-string res))))
2216 (while (string-match "," res)
2217 (setq res (replace-match ";" t t res)))
2218 (org-trim res)))
2221 ;;;###autoload
2222 (defun org-store-agenda-views (&rest parameters)
2223 (interactive)
2224 (eval (list 'org-batch-store-agenda-views)))
2226 ;; FIXME, why is this a macro?????
2227 ;;;###autoload
2228 (defmacro org-batch-store-agenda-views (&rest parameters)
2229 "Run all custom agenda commands that have a file argument."
2230 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2231 (pop-up-frames nil)
2232 (dir default-directory)
2233 pars cmd thiscmdkey files opts cmd-or-set)
2234 (while parameters
2235 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2236 (setq pars (reverse pars))
2237 (save-window-excursion
2238 (while cmds
2239 (setq cmd (pop cmds)
2240 thiscmdkey (car cmd)
2241 cmd-or-set (nth 2 cmd)
2242 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2243 files (nth (if (listp cmd-or-set) 4 5) cmd))
2244 (if (stringp files) (setq files (list files)))
2245 (when files
2246 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2247 (list 'org-agenda nil thiscmdkey)))
2248 (set-buffer org-agenda-buffer-name)
2249 (while files
2250 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2251 (list 'org-write-agenda
2252 (expand-file-name (pop files) dir) nil t))))
2253 (and (get-buffer org-agenda-buffer-name)
2254 (kill-buffer org-agenda-buffer-name)))))))
2256 (defun org-agenda-mark-header-line (pos)
2257 "Mark the line at POS as an agenda structure header."
2258 (save-excursion
2259 (goto-char pos)
2260 (put-text-property (point-at-bol) (point-at-eol)
2261 'org-agenda-structural-header t)
2262 (when org-agenda-title-append
2263 (put-text-property (point-at-bol) (point-at-eol)
2264 'org-agenda-title-append org-agenda-title-append))))
2266 (defvar org-mobile-creating-agendas)
2267 (defun org-write-agenda (file &optional open nosettings)
2268 "Write the current buffer (an agenda view) as a file.
2269 Depending on the extension of the file name, plain text (.txt),
2270 HTML (.html or .htm) or Postscript (.ps) is produced.
2271 If the extension is .ics, run icalendar export over all files used
2272 to construct the agenda and limit the export to entries listed in the
2273 agenda now.
2274 With prefic argument OPEN, open the new file immediately.
2275 If NOSETTINGS is given, do not scope the settings of
2276 `org-agenda-exporter-settings' into the export commands. This is used when
2277 the settings have already been scoped and we do not wish to overrule other,
2278 higher priority settings."
2279 (interactive "FWrite agenda to file: \nP")
2280 (if (not (file-writable-p file))
2281 (error "Cannot write agenda to file %s" file))
2282 (cond
2283 ((string-match "\\.html?\\'" file) (require 'htmlize))
2284 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2285 (org-let (if nosettings nil org-agenda-exporter-settings)
2286 '(save-excursion
2287 (save-window-excursion
2288 (org-agenda-mark-filtered-text)
2289 (let ((bs (copy-sequence (buffer-string))) beg)
2290 (org-agenda-unmark-filtered-text)
2291 (with-temp-buffer
2292 (insert bs)
2293 (org-agenda-remove-marked-text 'org-filtered)
2294 (while (setq beg (text-property-any (point-min) (point-max)
2295 'org-filtered t))
2296 (delete-region
2297 beg (or (next-single-property-change beg 'org-filtered)
2298 (point-max))))
2299 (run-hooks 'org-agenda-before-write-hook)
2300 (cond
2301 ((org-bound-and-true-p org-mobile-creating-agendas)
2302 (org-mobile-write-agenda-for-mobile file))
2303 ((string-match "\\.html?\\'" file)
2304 (set-buffer (htmlize-buffer (current-buffer)))
2306 (when (and org-agenda-export-html-style
2307 (string-match "<style>" org-agenda-export-html-style))
2308 ;; replace <style> section with org-agenda-export-html-style
2309 (goto-char (point-min))
2310 (kill-region (- (search-forward "<style") 6)
2311 (search-forward "</style>"))
2312 (insert org-agenda-export-html-style))
2313 (write-file file)
2314 (kill-buffer (current-buffer))
2315 (message "HTML written to %s" file))
2316 ((string-match "\\.ps\\'" file)
2317 (require 'ps-print)
2318 (flet ((ps-get-buffer-name () "Agenda View"))
2319 (ps-print-buffer-with-faces file))
2320 (message "Postscript written to %s" file))
2321 ((string-match "\\.pdf\\'" file)
2322 (require 'ps-print)
2323 (flet ((ps-get-buffer-name () "Agenda View"))
2324 (ps-print-buffer-with-faces
2325 (concat (file-name-sans-extension file) ".ps")))
2326 (call-process "ps2pdf" nil nil nil
2327 (expand-file-name
2328 (concat (file-name-sans-extension file) ".ps"))
2329 (expand-file-name file))
2330 (message "PDF written to %s" file))
2331 ((string-match "\\.ics\\'" file)
2332 (require 'org-icalendar)
2333 (let ((org-agenda-marker-table
2334 (org-create-marker-find-array
2335 (org-agenda-collect-markers)))
2336 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2337 (org-combined-agenda-icalendar-file file))
2338 (apply 'org-export-icalendar 'combine
2339 (org-agenda-files nil 'ifmode))))
2341 (let ((bs (buffer-string)))
2342 (find-file file)
2343 (erase-buffer)
2344 (insert bs)
2345 (save-buffer 0)
2346 (kill-buffer (current-buffer))
2347 (message "Plain text written to %s" file))))))))
2348 (set-buffer org-agenda-buffer-name))
2349 (when open (org-open-file file)))
2351 (defvar org-agenda-filter-overlays nil)
2353 (defun org-agenda-mark-filtered-text ()
2354 "Mark all text hidden by filtering with a text property."
2355 (let ((inhibit-read-only t))
2356 (mapc
2357 (lambda (o)
2358 (when (equal (org-overlay-buffer o) (current-buffer))
2359 (put-text-property
2360 (org-overlay-start o) (org-overlay-end o)
2361 'org-filtered t)))
2362 org-agenda-filter-overlays)))
2364 (defun org-agenda-unmark-filtered-text ()
2365 "Remove the filtering text property."
2366 (let ((inhibit-read-only t))
2367 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2369 (defun org-agenda-remove-marked-text (property &optional value)
2370 "Delete all text marked with VALUE of PROPERTY.
2371 VALUE defaults to t."
2372 (let (beg)
2373 (setq value (or value t))
2374 (while (setq beg (text-property-any (point-min) (point-max)
2375 property value))
2376 (delete-region
2377 beg (or (next-single-property-change beg 'org-filtered)
2378 (point-max))))))
2380 (defun org-agenda-add-entry-text ()
2381 "Add entry text to agenda lines.
2382 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2383 entry text following headings shown in the agenda.
2384 Drawers will be excluded, also the line with scheduling/deadline info."
2385 (when (and (> org-agenda-add-entry-text-maxlines 0)
2386 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2387 (let (m txt)
2388 (goto-char (point-min))
2389 (while (not (eobp))
2390 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2391 (beginning-of-line 2)
2392 (setq txt (org-agenda-get-some-entry-text
2393 m org-agenda-add-entry-text-maxlines " > "))
2394 (end-of-line 1)
2395 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2397 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2398 &rest keep)
2399 "Extract entry text from MARKER, at most N-LINES lines.
2400 This will ignore drawers etc, just get the text.
2401 If INDENT is given, prefix every line with this string. If KEEP is
2402 given, it is a list of symbols, defining stuff that hould not be
2403 removed from the entry content. Currently only `planning' is allowed here."
2404 (let (txt drawer-re kwd-time-re ind)
2405 (save-excursion
2406 (with-current-buffer (marker-buffer marker)
2407 (if (not (org-mode-p))
2408 (setq txt "")
2409 (save-excursion
2410 (save-restriction
2411 (widen)
2412 (goto-char marker)
2413 (end-of-line 1)
2414 (setq txt (buffer-substring
2415 (min (1+ (point)) (point-max))
2416 (progn (outline-next-heading) (point)))
2417 drawer-re org-drawer-regexp
2418 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2419 ".*\n?"))
2420 (with-temp-buffer
2421 (insert txt)
2422 (when org-agenda-add-entry-text-descriptive-links
2423 (goto-char (point-min))
2424 (while (org-activate-bracket-links (point-max))
2425 (add-text-properties (match-beginning 0) (match-end 0)
2426 '(face org-link))))
2427 (goto-char (point-min))
2428 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2429 (set-text-properties (match-beginning 0) (match-end 0)
2430 nil))
2431 (goto-char (point-min))
2432 (while (re-search-forward drawer-re nil t)
2433 (delete-region
2434 (match-beginning 0)
2435 (progn (re-search-forward
2436 "^[ \t]*:END:.*\n?" nil 'move)
2437 (point))))
2438 (unless (member 'planning keep)
2439 (goto-char (point-min))
2440 (while (re-search-forward kwd-time-re nil t)
2441 (replace-match "")))
2442 (goto-char (point-min))
2443 (when org-agenda-entry-text-exclude-regexps
2444 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2445 (while (setq re (pop re-list))
2446 (goto-char (point-min))
2447 (while (re-search-forward re nil t)
2448 (replace-match "")))))
2449 (goto-char (point-max))
2450 (skip-chars-backward " \t\n")
2451 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2453 ;; find and remove min common indentation
2454 (goto-char (point-min))
2455 (untabify (point-min) (point-max))
2456 (setq ind (org-get-indentation))
2457 (while (not (eobp))
2458 (unless (looking-at "[ \t]*$")
2459 (setq ind (min ind (org-get-indentation))))
2460 (beginning-of-line 2))
2461 (goto-char (point-min))
2462 (while (not (eobp))
2463 (unless (looking-at "[ \t]*$")
2464 (move-to-column ind)
2465 (delete-region (point-at-bol) (point)))
2466 (beginning-of-line 2))
2468 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2470 (goto-char (point-min))
2471 (when indent
2472 (while (and (not (eobp)) (re-search-forward "^" nil t))
2473 (replace-match indent t t)))
2474 (goto-char (point-min))
2475 (while (looking-at "[ \t]*\n") (replace-match ""))
2476 (goto-char (point-max))
2477 (when (> (org-current-line)
2478 n-lines)
2479 (org-goto-line (1+ n-lines))
2480 (backward-char 1))
2481 (setq txt (buffer-substring (point-min) (point)))))))))
2482 txt))
2484 (defun org-agenda-collect-markers ()
2485 "Collect the markers pointing to entries in the agenda buffer."
2486 (let (m markers)
2487 (save-excursion
2488 (goto-char (point-min))
2489 (while (not (eobp))
2490 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2491 (org-get-at-bol 'org-marker)))
2492 (push m markers))
2493 (beginning-of-line 2)))
2494 (nreverse markers)))
2496 (defun org-create-marker-find-array (marker-list)
2497 "Create a alist of files names with all marker positions in that file."
2498 (let (f tbl m a p)
2499 (while (setq m (pop marker-list))
2500 (setq p (marker-position m)
2501 f (buffer-file-name (or (buffer-base-buffer
2502 (marker-buffer m))
2503 (marker-buffer m))))
2504 (if (setq a (assoc f tbl))
2505 (push (marker-position m) (cdr a))
2506 (push (list f p) tbl)))
2507 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2508 tbl)))
2510 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2511 (defun org-check-agenda-marker-table ()
2512 "Check of the current entry is on the marker list."
2513 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2515 (and (setq a (assoc file org-agenda-marker-table))
2516 (save-match-data
2517 (save-excursion
2518 (org-back-to-heading t)
2519 (member (point) (cdr a)))))))
2521 (defun org-check-for-org-mode ()
2522 "Make sure current buffer is in org-mode. Error if not."
2523 (or (org-mode-p)
2524 (error "Cannot execute org-mode agenda command on buffer in %s"
2525 major-mode)))
2527 (defun org-fit-agenda-window ()
2528 "Fit the window to the buffer size."
2529 (and (memq org-agenda-window-setup '(reorganize-frame))
2530 (fboundp 'fit-window-to-buffer)
2531 (org-fit-window-to-buffer
2533 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2534 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2536 ;;; Agenda prepare and finalize
2538 (defvar org-agenda-multi nil) ; dynamically scoped
2539 (defvar org-agenda-buffer-name "*Org Agenda*")
2540 (defvar org-pre-agenda-window-conf nil)
2541 (defvar org-agenda-columns-active nil)
2542 (defvar org-agenda-name nil)
2543 (defvar org-agenda-filter nil)
2544 (defvar org-agenda-filter-preset nil
2545 "A preset of the tags filter used for secondary agenda filtering.
2546 This must be a list of strings, each string must be a single tag preceeded
2547 by \"+\" or \"-\".
2548 This variable should not be set directly, but agenda custom commands can
2549 bind it in the options section.")
2551 (defun org-prepare-agenda (&optional name)
2552 (setq org-todo-keywords-for-agenda nil)
2553 (setq org-done-keywords-for-agenda nil)
2554 (setq org-drawers-for-agenda nil)
2555 (setq org-agenda-filter nil)
2556 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2557 (if org-agenda-multi
2558 (progn
2559 (setq buffer-read-only nil)
2560 (goto-char (point-max))
2561 (unless (or (bobp) org-agenda-compact-blocks)
2562 (insert "\n"
2563 (if (stringp org-agenda-block-separator)
2564 org-agenda-block-separator
2565 (make-string (window-width) org-agenda-block-separator))
2566 "\n"))
2567 (narrow-to-region (point) (point-max)))
2568 (org-agenda-reset-markers)
2569 (setq org-agenda-contributing-files nil)
2570 (setq org-agenda-columns-active nil)
2571 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2572 (setq org-todo-keywords-for-agenda
2573 (org-uniquify org-todo-keywords-for-agenda))
2574 (setq org-done-keywords-for-agenda
2575 (org-uniquify org-done-keywords-for-agenda))
2576 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
2577 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2578 (awin (get-buffer-window abuf)))
2579 (cond
2580 ((equal (current-buffer) abuf) nil)
2581 (awin (select-window awin))
2582 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2583 ((equal org-agenda-window-setup 'current-window)
2584 (switch-to-buffer abuf))
2585 ((equal org-agenda-window-setup 'other-window)
2586 (org-switch-to-buffer-other-window abuf))
2587 ((equal org-agenda-window-setup 'other-frame)
2588 (switch-to-buffer-other-frame abuf))
2589 ((equal org-agenda-window-setup 'reorganize-frame)
2590 (delete-other-windows)
2591 (org-switch-to-buffer-other-window abuf))))
2592 (setq buffer-read-only nil)
2593 (let ((inhibit-read-only t)) (erase-buffer))
2594 (org-agenda-mode)
2595 (and name (not org-agenda-name)
2596 (org-set-local 'org-agenda-name name)))
2597 (setq buffer-read-only nil))
2599 (defun org-finalize-agenda ()
2600 "Finishing touch for the agenda buffer, called just before displaying it."
2601 (unless org-agenda-multi
2602 (save-excursion
2603 (let ((inhibit-read-only t))
2604 (goto-char (point-min))
2605 (while (org-activate-bracket-links (point-max))
2606 (add-text-properties (match-beginning 0) (match-end 0)
2607 '(face org-link)))
2608 (org-agenda-align-tags)
2609 (unless org-agenda-with-colors
2610 (remove-text-properties (point-min) (point-max) '(face nil))))
2611 (if (and (boundp 'org-agenda-overriding-columns-format)
2612 org-agenda-overriding-columns-format)
2613 (org-set-local 'org-agenda-overriding-columns-format
2614 org-agenda-overriding-columns-format))
2615 (if (and (boundp 'org-agenda-view-columns-initially)
2616 org-agenda-view-columns-initially)
2617 (org-agenda-columns))
2618 (when org-agenda-fontify-priorities
2619 (org-agenda-fontify-priorities))
2620 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2621 (org-agenda-dim-blocked-tasks))
2622 (org-agenda-mark-clocking-task)
2623 (when org-agenda-entry-text-mode
2624 (org-agenda-entry-text-hide)
2625 (org-agenda-entry-text-show))
2626 (if (functionp 'org-habit-insert-consistency-graphs)
2627 (org-habit-insert-consistency-graphs))
2628 (run-hooks 'org-finalize-agenda-hook)
2629 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
2630 (when (get 'org-agenda-filter :preset-filter)
2631 (org-agenda-filter-apply org-agenda-filter))
2634 (defun org-agenda-mark-clocking-task ()
2635 "Mark the current clock entry in the agenda if it is present."
2636 (mapc (lambda (o)
2637 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2638 (org-delete-overlay o)))
2639 (org-overlays-in (point-min) (point-max)))
2640 (when (marker-buffer org-clock-hd-marker)
2641 (save-excursion
2642 (goto-char (point-min))
2643 (let (s ov)
2644 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2645 (goto-char s)
2646 (when (equal (org-get-at-bol 'org-hd-marker)
2647 org-clock-hd-marker)
2648 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2649 (org-overlay-put ov 'type 'org-agenda-clocking)
2650 (org-overlay-put ov 'face 'org-agenda-clocking)
2651 (org-overlay-put ov 'help-echo
2652 "The clock is running in this item")))))))
2654 (defun org-agenda-fontify-priorities ()
2655 "Make highest priority lines bold, and lowest italic."
2656 (interactive)
2657 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2658 (org-delete-overlay o)))
2659 (org-overlays-in (point-min) (point-max)))
2660 (save-excursion
2661 (let ((inhibit-read-only t)
2662 b e p ov h l)
2663 (goto-char (point-min))
2664 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2665 (setq h (or (get-char-property (point) 'org-highest-priority)
2666 org-highest-priority)
2667 l (or (get-char-property (point) 'org-lowest-priority)
2668 org-lowest-priority)
2669 p (string-to-char (match-string 1))
2670 b (match-beginning 0)
2671 e (if (eq org-agenda-fontify-priorities 'cookies)
2672 (match-end 0)
2673 (point-at-eol))
2674 ov (org-make-overlay b e))
2675 (org-overlay-put
2676 ov 'face
2677 (cond ((cdr (assoc p org-priority-faces)))
2678 ((and (listp org-agenda-fontify-priorities)
2679 (cdr (assoc p org-agenda-fontify-priorities))))
2680 ((equal p l) 'italic)
2681 ((equal p h) 'bold)))
2682 (org-overlay-put ov 'org-type 'org-priority)))))
2684 (defun org-agenda-dim-blocked-tasks ()
2685 "Dim currently blocked TODO's in the agenda display."
2686 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2687 (org-delete-overlay o)))
2688 (org-overlays-in (point-min) (point-max)))
2689 (save-excursion
2690 (let ((inhibit-read-only t)
2691 (org-depend-tag-blocked nil)
2692 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2693 org-blocked-by-checkboxes
2694 invis1 b e p ov h l)
2695 (goto-char (point-min))
2696 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2697 (and pos (goto-char (1+ pos))))
2698 (setq org-blocked-by-checkboxes nil invis1 invis)
2699 (let ((marker (org-get-at-bol 'org-hd-marker)))
2700 (when (and marker
2701 (not (with-current-buffer (marker-buffer marker)
2702 (save-excursion
2703 (goto-char marker)
2704 (if (org-entry-get nil "NOBLOCKING")
2705 t ;; Never block this entry
2706 (run-hook-with-args-until-failure
2707 'org-blocker-hook
2708 (list :type 'todo-state-change
2709 :position marker
2710 :from 'todo
2711 :to 'done)))))))
2712 (if org-blocked-by-checkboxes (setq invis1 nil))
2713 (setq b (if invis1
2714 (max (point-min) (1- (point-at-bol)))
2715 (point-at-bol))
2716 e (point-at-eol)
2717 ov (org-make-overlay b e))
2718 (if invis1
2719 (org-overlay-put ov 'invisible t)
2720 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2721 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2723 (defvar org-agenda-skip-function nil
2724 "Function to be called at each match during agenda construction.
2725 If this function returns nil, the current match should not be skipped.
2726 Otherwise, the function must return a position from where the search
2727 should be continued.
2728 This may also be a Lisp form, it will be evaluated.
2729 Never set this variable using `setq' or so, because then it will apply
2730 to all future agenda commands. Instead, bind it with `let' to scope
2731 it dynamically into the agenda-constructing command. A good way to set
2732 it is through options in org-agenda-custom-commands.")
2734 (defun org-agenda-skip ()
2735 "Throw to `:skip' in places that should be skipped.
2736 Also moves point to the end of the skipped region, so that search can
2737 continue from there."
2738 (let ((p (point-at-bol)) to fp)
2739 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2740 (get-text-property p :org-archived)
2741 (org-end-of-subtree t)
2742 (throw :skip t))
2743 (and org-agenda-skip-comment-trees
2744 (get-text-property p :org-comment)
2745 (org-end-of-subtree t)
2746 (throw :skip t))
2747 (if (equal (char-after p) ?#) (throw :skip t))
2748 (when (and (or (setq fp (functionp org-agenda-skip-function))
2749 (consp org-agenda-skip-function))
2750 (setq to (save-excursion
2751 (save-match-data
2752 (if fp
2753 (funcall org-agenda-skip-function)
2754 (eval org-agenda-skip-function))))))
2755 (goto-char to)
2756 (throw :skip t))))
2758 (defvar org-agenda-markers nil
2759 "List of all currently active markers created by `org-agenda'.")
2760 (defvar org-agenda-last-marker-time (org-float-time)
2761 "Creation time of the last agenda marker.")
2763 (defun org-agenda-new-marker (&optional pos)
2764 "Return a new agenda marker.
2765 Org-mode keeps a list of these markers and resets them when they are
2766 no longer in use."
2767 (let ((m (copy-marker (or pos (point)))))
2768 (setq org-agenda-last-marker-time (org-float-time))
2769 (push m org-agenda-markers)
2772 (defun org-agenda-reset-markers ()
2773 "Reset markers created by `org-agenda'."
2774 (while org-agenda-markers
2775 (move-marker (pop org-agenda-markers) nil)))
2777 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2778 "Save relative positions of markers in region."
2779 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2780 org-agenda-markers))
2782 ;;; Entry text mode
2784 (defun org-agenda-entry-text-show-here ()
2785 "Add some text from te entry as context to the current line."
2786 (let (m txt o)
2787 (setq m (org-get-at-bol 'org-hd-marker))
2788 (unless (marker-buffer m)
2789 (error "No marker points to an entry here"))
2790 (setq txt (concat "\n" (org-no-properties
2791 (org-agenda-get-some-entry-text
2792 m org-agenda-entry-text-maxlines " > "))))
2793 (when (string-match "\\S-" txt)
2794 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2795 (org-overlay-put o 'evaporate t)
2796 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2797 (org-overlay-put o 'after-string txt))))
2799 (defun org-agenda-entry-text-show ()
2800 "Add entry context for all agenda lines."
2801 (interactive)
2802 (save-excursion
2803 (goto-char (point-max))
2804 (beginning-of-line 1)
2805 (while (not (bobp))
2806 (when (org-get-at-bol 'org-hd-marker)
2807 (org-agenda-entry-text-show-here))
2808 (beginning-of-line 0))))
2810 (defun org-agenda-entry-text-hide ()
2811 "Remove any shown entry context."
2812 (delq nil
2813 (mapcar (lambda (o)
2814 (if (eq (org-overlay-get o 'org-overlay-type)
2815 'agenda-entry-content)
2816 (progn (org-delete-overlay o) t)))
2817 (org-overlays-in (point-min) (point-max)))))
2819 ;;; Agenda timeline
2821 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2823 (defun org-timeline (&optional include-all)
2824 "Show a time-sorted view of the entries in the current org file.
2825 Only entries with a time stamp of today or later will be listed. With
2826 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2827 under the current date.
2828 If the buffer contains an active region, only check the region for
2829 dates."
2830 (interactive "P")
2831 (require 'calendar)
2832 (org-compile-prefix-format 'timeline)
2833 (org-set-sorting-strategy 'timeline)
2834 (let* ((dopast t)
2835 (dotodo include-all)
2836 (doclosed org-agenda-show-log)
2837 (entry buffer-file-name)
2838 (date (calendar-current-date))
2839 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2840 (end (if (org-region-active-p) (region-end) (point-max)))
2841 (day-numbers (org-get-all-dates beg end 'no-ranges
2842 t doclosed ; always include today
2843 org-timeline-show-empty-dates))
2844 (org-deadline-warning-days 0)
2845 (org-agenda-only-exact-dates t)
2846 (today (time-to-days (current-time)))
2847 (past t)
2848 args
2849 s e rtn d emptyp wd)
2850 (setq org-agenda-redo-command
2851 (list 'progn
2852 (list 'org-switch-to-buffer-other-window (current-buffer))
2853 (list 'org-timeline (list 'quote include-all))))
2854 (if (not dopast)
2855 ;; Remove past dates from the list of dates.
2856 (setq day-numbers (delq nil (mapcar (lambda(x)
2857 (if (>= x today) x nil))
2858 day-numbers))))
2859 (org-prepare-agenda (concat "Timeline "
2860 (file-name-nondirectory buffer-file-name)))
2861 (if doclosed (push :closed args))
2862 (push :timestamp args)
2863 (push :deadline args)
2864 (push :scheduled args)
2865 (push :sexp args)
2866 (if dotodo (push :todo args))
2867 (insert "Timeline of file " entry "\n")
2868 (add-text-properties (point-min) (point)
2869 (list 'face 'org-agenda-structure))
2870 (org-agenda-mark-header-line (point-min))
2871 (while (setq d (pop day-numbers))
2872 (if (and (listp d) (eq (car d) :omitted))
2873 (progn
2874 (setq s (point))
2875 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2876 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2877 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2878 (if (and (>= d today)
2879 dopast
2880 past)
2881 (progn
2882 (setq past nil)
2883 (insert (make-string 79 ?-) "\n")))
2884 (setq date (calendar-gregorian-from-absolute d)
2885 wd (calendar-day-of-week date))
2886 (setq s (point))
2887 (setq rtn (and (not emptyp)
2888 (apply 'org-agenda-get-day-entries entry
2889 date args)))
2890 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2891 (progn
2892 (insert
2893 (if (stringp org-agenda-format-date)
2894 (format-time-string org-agenda-format-date
2895 (org-time-from-absolute date))
2896 (funcall org-agenda-format-date date))
2897 "\n")
2898 (put-text-property s (1- (point)) 'face
2899 (if (member wd org-agenda-weekend-days)
2900 'org-agenda-date-weekend
2901 'org-agenda-date))
2902 (put-text-property s (1- (point)) 'org-date-line t)
2903 (put-text-property s (1- (point)) 'org-agenda-date-header t)
2904 (if (equal d today)
2905 (put-text-property s (1- (point)) 'org-today t))
2906 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2907 (put-text-property s (1- (point)) 'day d)))))
2908 (goto-char (point-min))
2909 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2910 (point-min)))
2911 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2912 (org-finalize-agenda)
2913 (setq buffer-read-only t)))
2915 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2916 "Return a list of all relevant day numbers from BEG to END buffer positions.
2917 If NO-RANGES is non-nil, include only the start and end dates of a range,
2918 not every single day in the range. If FORCE-TODAY is non-nil, make
2919 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2920 inactive time stamps (those in square brackets) are included.
2921 When EMPTY is non-nil, also include days without any entries."
2922 (let ((re (concat
2923 (if pre-re pre-re "")
2924 (if inactive org-ts-regexp-both org-ts-regexp)))
2925 dates dates1 date day day1 day2 ts1 ts2)
2926 (if force-today
2927 (setq dates (list (time-to-days (current-time)))))
2928 (save-excursion
2929 (goto-char beg)
2930 (while (re-search-forward re end t)
2931 (setq day (time-to-days (org-time-string-to-time
2932 (substring (match-string 1) 0 10))))
2933 (or (memq day dates) (push day dates)))
2934 (unless no-ranges
2935 (goto-char beg)
2936 (while (re-search-forward org-tr-regexp end t)
2937 (setq ts1 (substring (match-string 1) 0 10)
2938 ts2 (substring (match-string 2) 0 10)
2939 day1 (time-to-days (org-time-string-to-time ts1))
2940 day2 (time-to-days (org-time-string-to-time ts2)))
2941 (while (< (setq day1 (1+ day1)) day2)
2942 (or (memq day1 dates) (push day1 dates)))))
2943 (setq dates (sort dates '<))
2944 (when empty
2945 (while (setq day (pop dates))
2946 (setq day2 (car dates))
2947 (push day dates1)
2948 (when (and day2 empty)
2949 (if (or (eq empty t)
2950 (and (numberp empty) (<= (- day2 day) empty)))
2951 (while (< (setq day (1+ day)) day2)
2952 (push (list day) dates1))
2953 (push (cons :omitted (- day2 day)) dates1))))
2954 (setq dates (nreverse dates1)))
2955 dates)))
2957 ;;; Agenda Daily/Weekly
2959 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2960 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2961 "Custom commands can set this variable in the options section.")
2962 (defvar org-agenda-last-arguments nil
2963 "The arguments of the previous call to org-agenda")
2964 (defvar org-starting-day nil) ; local variable in the agenda buffer
2965 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2966 (defvar org-include-all-loc nil) ; local variable
2967 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2969 ;;;###autoload
2970 (defun org-agenda-list (&optional include-all start-day ndays)
2971 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2972 The view will be for the current day or week, but from the overview buffer
2973 you will be able to go to other days/weeks.
2975 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2976 all unfinished TODO items will also be shown, before the agenda.
2977 This feature is considered obsolete, please use the TODO list or a block
2978 agenda instead.
2980 With a numeric prefix argument in an interactive call, the agenda will
2981 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2982 the number of days. NDAYS defaults to `org-agenda-ndays'.
2984 START-DAY defaults to TODAY, or to the most recent match for the weekday
2985 given in `org-agenda-start-on-weekday'."
2986 (interactive "P")
2987 (if (and (integerp include-all) (> include-all 0))
2988 (setq ndays include-all include-all nil))
2989 (setq ndays (or ndays org-agenda-ndays)
2990 start-day (or start-day org-agenda-start-day))
2991 (if org-agenda-overriding-arguments
2992 (setq include-all (car org-agenda-overriding-arguments)
2993 start-day (nth 1 org-agenda-overriding-arguments)
2994 ndays (nth 2 org-agenda-overriding-arguments)))
2995 (if (stringp start-day)
2996 ;; Convert to an absolute day number
2997 (setq start-day (time-to-days (org-read-date nil t start-day))))
2998 (setq org-agenda-last-arguments (list include-all start-day ndays))
2999 (org-compile-prefix-format 'agenda)
3000 (org-set-sorting-strategy 'agenda)
3001 (require 'calendar)
3002 (let* ((org-agenda-start-on-weekday
3003 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
3004 org-agenda-start-on-weekday nil))
3005 (thefiles (org-agenda-files nil 'ifmode))
3006 (files thefiles)
3007 (today (time-to-days
3008 (time-subtract (current-time)
3009 (list 0 (* 3600 org-extend-today-until) 0))))
3010 (sd (or start-day today))
3011 (start (if (or (null org-agenda-start-on-weekday)
3012 (< org-agenda-ndays 7))
3014 (let* ((nt (calendar-day-of-week
3015 (calendar-gregorian-from-absolute sd)))
3016 (n1 org-agenda-start-on-weekday)
3017 (d (- nt n1)))
3018 (- sd (+ (if (< d 0) 7 0) d)))))
3019 (day-numbers (list start))
3020 (day-cnt 0)
3021 (inhibit-redisplay (not debug-on-error))
3022 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3023 clocktable-start clocktable-end)
3024 (setq org-agenda-redo-command
3025 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3026 ;; Make the list of days
3027 (setq ndays (or ndays org-agenda-ndays)
3028 nd ndays)
3029 (while (> ndays 1)
3030 (push (1+ (car day-numbers)) day-numbers)
3031 (setq ndays (1- ndays)))
3032 (setq day-numbers (nreverse day-numbers))
3033 (setq clocktable-start (car day-numbers)
3034 clocktable-end (1+ (or (org-last day-numbers) 0)))
3035 (org-prepare-agenda "Day/Week")
3036 (org-set-local 'org-starting-day (car day-numbers))
3037 (org-set-local 'org-include-all-loc include-all)
3038 (org-set-local 'org-agenda-span
3039 (org-agenda-ndays-to-span nd))
3040 (when (and (or include-all org-agenda-include-all-todo)
3041 (member today day-numbers))
3042 (setq files thefiles
3043 rtnall nil)
3044 (while (setq file (pop files))
3045 (catch 'nextfile
3046 (org-check-agenda-file file)
3047 (setq date (calendar-gregorian-from-absolute today)
3048 rtn (org-agenda-get-day-entries
3049 file date :todo))
3050 (setq rtnall (append rtnall rtn))))
3051 (when rtnall
3052 (insert "All currently open TODO items:\n")
3053 (add-text-properties (point-min) (1- (point))
3054 (list 'face 'org-agenda-structure
3055 'short-heading "All TODO items"))
3056 (org-agenda-mark-header-line (point-min))
3057 (insert (org-finalize-agenda-entries rtnall) "\n")))
3058 (unless org-agenda-compact-blocks
3059 (let* ((d1 (car day-numbers))
3060 (d2 (org-last day-numbers))
3061 (w1 (org-days-to-iso-week d1))
3062 (w2 (org-days-to-iso-week d2)))
3063 (setq s (point))
3064 (if org-agenda-overriding-header
3065 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3066 nil 'face 'org-agenda-structure) "\n")
3067 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3068 "-agenda"
3069 (if (< (- d2 d1) 350)
3070 (if (= w1 w2)
3071 (format " (W%02d)" w1)
3072 (format " (W%02d-W%02d)" w1 w2))
3074 ":\n")))
3075 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3076 'org-date-line t))
3077 (org-agenda-mark-header-line s))
3078 (while (setq d (pop day-numbers))
3079 (setq date (calendar-gregorian-from-absolute d)
3080 wd (calendar-day-of-week date)
3081 s (point))
3082 (if (or (setq todayp (= d today))
3083 (and (not start-pos) (= d sd)))
3084 (setq start-pos (point))
3085 (if (and start-pos (not end-pos))
3086 (setq end-pos (point))))
3087 (setq files thefiles
3088 rtnall nil)
3089 (while (setq file (pop files))
3090 (catch 'nextfile
3091 (org-check-agenda-file file)
3092 (cond
3093 ((eq org-agenda-show-log 'only)
3094 (setq rtn (org-agenda-get-day-entries
3095 file date :closed)))
3096 (org-agenda-show-log
3097 (setq rtn (org-agenda-get-day-entries
3098 file date
3099 :deadline :scheduled :timestamp :sexp :closed)))
3101 (setq rtn (org-agenda-get-day-entries
3102 file date
3103 :deadline :scheduled :sexp :timestamp))))
3104 (setq rtnall (append rtnall rtn))))
3105 (if org-agenda-include-diary
3106 (let ((org-agenda-search-headline-for-time t))
3107 (require 'diary-lib)
3108 (setq rtn (org-get-entries-from-diary date))
3109 (setq rtnall (append rtnall rtn))))
3110 (if (or rtnall org-agenda-show-all-dates)
3111 (progn
3112 (setq day-cnt (1+ day-cnt))
3113 (insert
3114 (if (stringp org-agenda-format-date)
3115 (format-time-string org-agenda-format-date
3116 (org-time-from-absolute date))
3117 (funcall org-agenda-format-date date))
3118 "\n")
3119 (put-text-property s (1- (point)) 'face
3120 (if (member wd org-agenda-weekend-days)
3121 'org-agenda-date-weekend
3122 'org-agenda-date))
3123 (put-text-property s (1- (point)) 'org-date-line t)
3124 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3125 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3126 (when todayp
3127 (put-text-property s (1- (point)) 'org-today t)
3128 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
3129 (if rtnall (insert
3130 (org-finalize-agenda-entries
3131 (org-agenda-add-time-grid-maybe
3132 rtnall nd todayp))
3133 "\n"))
3134 (put-text-property s (1- (point)) 'day d)
3135 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3136 (when (and org-agenda-clockreport-mode clocktable-start)
3137 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3138 ;; the above line is to ensure the restricted range!
3139 (p org-agenda-clockreport-parameter-plist)
3140 tbl)
3141 (setq p (org-plist-delete p :block))
3142 (setq p (plist-put p :tstart clocktable-start))
3143 (setq p (plist-put p :tend clocktable-end))
3144 (setq p (plist-put p :scope 'agenda))
3145 (setq tbl (apply 'org-get-clocktable p))
3146 (insert tbl)))
3147 (goto-char (point-min))
3148 (or org-agenda-multi (org-fit-agenda-window))
3149 (unless (and (pos-visible-in-window-p (point-min))
3150 (pos-visible-in-window-p (point-max)))
3151 (goto-char (1- (point-max)))
3152 (recenter -1)
3153 (if (not (pos-visible-in-window-p (or start-pos 1)))
3154 (progn
3155 (goto-char (or start-pos 1))
3156 (recenter 1))))
3157 (goto-char (or start-pos 1))
3158 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3159 (org-finalize-agenda)
3160 (setq buffer-read-only t)
3161 (message "")))
3163 (defun org-agenda-ndays-to-span (n)
3164 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3166 ;;; Agenda word search
3168 (defvar org-agenda-search-history nil)
3169 (defvar org-todo-only nil)
3171 (defvar org-search-syntax-table nil
3172 "Special syntax table for org-mode search.
3173 In this table, we have single quotes not as word constituents, to
3174 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3176 (defun org-search-syntax-table ()
3177 (unless org-search-syntax-table
3178 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3179 (modify-syntax-entry ?' "." org-search-syntax-table)
3180 (modify-syntax-entry ?` "." org-search-syntax-table))
3181 org-search-syntax-table)
3183 ;;;###autoload
3184 (defun org-search-view (&optional todo-only string edit-at)
3185 "Show all entries that contain words or regular expressions.
3186 If the first character of the search string is an asterisks,
3187 search only the headlines.
3189 With optional prefix argument TODO-ONLY, only consider entries that are
3190 TODO entries. The argument STRING can be used to pass a default search
3191 string into this function. If EDIT-AT is non-nil, it means that the
3192 user should get a chance to edit this string, with cursor at position
3193 EDIT-AT.
3195 The search string is broken into \"words\" by splitting at whitespace.
3196 Depending on the variable `org-agenda-search-view-search-words-only'
3197 and on wether the first character in the search string is \"+\" or \"-\",
3198 The string is then interpreted either as a substrig with variable amounts
3199 of whitespace, or as a list or individual words that should be matched.
3201 The default is a substring match, where each space in the search string
3202 can expand to an arbitrary amount of whitespace, including newlines.
3204 If matching individual words, these words are then interpreted as a
3205 boolean expression with logical AND. Words prefixed with a minus must
3206 not occur in the entry. Words without a prefix or prefixed with a plus
3207 must occur in the entry. Matching is case-insensitive and the words
3208 are enclosed by word delimiters.
3210 Words enclosed by curly braces are interpreted as regular expressions
3211 that must or must not match in the entry.
3213 If the search string starts with an asterisk, search only in headlines.
3214 If (possibly after the leading star) the search string starts with an
3215 exclamation mark, this also means to look at TODO entries only, an effect
3216 that can also be achieved with a prefix argument.
3218 This command searches the agenda files, and in addition the files listed
3219 in `org-agenda-text-search-extra-files'."
3220 (interactive "P")
3221 (org-compile-prefix-format 'search)
3222 (org-set-sorting-strategy 'search)
3223 (org-prepare-agenda "SEARCH")
3224 (let* ((props (list 'face nil
3225 'done-face 'org-agenda-done
3226 'org-not-done-regexp org-not-done-regexp
3227 'org-todo-regexp org-todo-regexp
3228 'org-complex-heading-regexp org-complex-heading-regexp
3229 'mouse-face 'highlight
3230 'help-echo (format "mouse-2 or RET jump to location")))
3231 regexp rtn rtnall files file pos
3232 marker category tags c neg re as-words
3233 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3234 (unless (and (not edit-at)
3235 (stringp string)
3236 (string-match "\\S-" string))
3237 (setq string (read-string "[+-]Word/{Regexp} ...: "
3238 (cond
3239 ((integerp edit-at) (cons string edit-at))
3240 (edit-at string))
3241 'org-agenda-search-history)))
3242 (org-set-local 'org-todo-only todo-only)
3243 (setq org-agenda-redo-command
3244 (list 'org-search-view (if todo-only t nil) string
3245 '(if current-prefix-arg 1 nil)))
3246 (setq org-agenda-query-string string)
3248 (if (equal (string-to-char string) ?*)
3249 (setq hdl-only t
3250 words (substring string 1))
3251 (setq words string))
3252 (when (equal (string-to-char words) ?!)
3253 (setq todo-only t
3254 words (substring words 1)))
3255 (if (or org-agenda-search-view-search-words-only
3256 (member (string-to-char string) '(?- ?+)))
3257 (setq as-words t))
3258 (setq words (org-split-string words))
3259 (if as-words
3260 (mapc (lambda (w)
3261 (setq c (string-to-char w))
3262 (if (equal c ?-)
3263 (setq neg t w (substring w 1))
3264 (if (equal c ?+)
3265 (setq neg nil w (substring w 1))
3266 (setq neg nil)))
3267 (if (string-match "\\`{.*}\\'" w)
3268 (setq re (substring w 1 -1))
3269 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
3270 (if neg (push re regexps-) (push re regexps+)))
3271 words)
3272 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3273 regexps+))
3274 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3275 (if (not regexps+)
3276 (setq regexp (concat "^" org-outline-regexp))
3277 (setq regexp (pop regexps+))
3278 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3279 regexp))))
3280 (setq files (org-agenda-files nil 'ifmode))
3281 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3282 (pop org-agenda-text-search-extra-files)
3283 (setq files (org-add-archive-files files)))
3284 (setq files (append files org-agenda-text-search-extra-files)
3285 rtnall nil)
3286 (while (setq file (pop files))
3287 (setq ee nil)
3288 (catch 'nextfile
3289 (org-check-agenda-file file)
3290 (setq buffer (if (file-exists-p file)
3291 (org-get-agenda-file-buffer file)
3292 (error "No such file %s" file)))
3293 (if (not buffer)
3294 ;; If file does not exist, make sure an error message is sent
3295 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3296 file))))
3297 (with-current-buffer buffer
3298 (with-syntax-table (org-search-syntax-table)
3299 (unless (org-mode-p)
3300 (error "Agenda file %s is not in `org-mode'" file))
3301 (let ((case-fold-search t))
3302 (save-excursion
3303 (save-restriction
3304 (if org-agenda-restrict
3305 (narrow-to-region org-agenda-restrict-begin
3306 org-agenda-restrict-end)
3307 (widen))
3308 (goto-char (point-min))
3309 (unless (or (org-on-heading-p)
3310 (outline-next-heading))
3311 (throw 'nextfile t))
3312 (goto-char (max (point-min) (1- (point))))
3313 (while (re-search-forward regexp nil t)
3314 (org-back-to-heading t)
3315 (skip-chars-forward "* ")
3316 (setq beg (point-at-bol)
3317 beg1 (point)
3318 end (progn (outline-next-heading) (point)))
3319 (catch :skip
3320 (goto-char beg)
3321 (org-agenda-skip)
3322 (setq str (buffer-substring-no-properties
3323 (point-at-bol)
3324 (if hdl-only (point-at-eol) end)))
3325 (mapc (lambda (wr) (when (string-match wr str)
3326 (goto-char (1- end))
3327 (throw :skip t)))
3328 regexps-)
3329 (mapc (lambda (wr) (unless (string-match wr str)
3330 (goto-char (1- end))
3331 (throw :skip t)))
3332 (if todo-only
3333 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3334 regexps+)
3335 regexps+))
3336 (goto-char beg)
3337 (setq marker (org-agenda-new-marker (point))
3338 category (org-get-category)
3339 tags (org-get-tags-at (point))
3340 txt (org-format-agenda-item
3342 (buffer-substring-no-properties
3343 beg1 (point-at-eol))
3344 category tags))
3345 (org-add-props txt props
3346 'org-marker marker 'org-hd-marker marker
3347 'org-todo-regexp org-todo-regexp
3348 'org-complex-heading-regexp org-complex-heading-regexp
3349 'priority 1000 'org-category category
3350 'type "search")
3351 (push txt ee)
3352 (goto-char (1- end))))))))))
3353 (setq rtn (nreverse ee))
3354 (setq rtnall (append rtnall rtn)))
3355 (if org-agenda-overriding-header
3356 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3357 nil 'face 'org-agenda-structure) "\n")
3358 (insert "Search words: ")
3359 (add-text-properties (point-min) (1- (point))
3360 (list 'face 'org-agenda-structure))
3361 (setq pos (point))
3362 (insert string "\n")
3363 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3364 (setq pos (point))
3365 (unless org-agenda-multi
3366 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3367 (add-text-properties pos (1- (point))
3368 (list 'face 'org-agenda-structure))))
3369 (org-agenda-mark-header-line (point-min))
3370 (when rtnall
3371 (insert (org-finalize-agenda-entries rtnall) "\n"))
3372 (goto-char (point-min))
3373 (or org-agenda-multi (org-fit-agenda-window))
3374 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3375 (org-finalize-agenda)
3376 (setq buffer-read-only t)))
3378 ;;; Agenda TODO list
3380 (defvar org-select-this-todo-keyword nil)
3381 (defvar org-last-arg nil)
3383 ;;;###autoload
3384 (defun org-todo-list (arg)
3385 "Show all TODO entries from all agenda file in a single list.
3386 The prefix arg can be used to select a specific TODO keyword and limit
3387 the list to these. When using \\[universal-argument], you will be prompted
3388 for a keyword. A numeric prefix directly selects the Nth keyword in
3389 `org-todo-keywords-1'."
3390 (interactive "P")
3391 (require 'calendar)
3392 (org-compile-prefix-format 'todo)
3393 (org-set-sorting-strategy 'todo)
3394 (org-prepare-agenda "TODO")
3395 (let* ((today (time-to-days (current-time)))
3396 (date (calendar-gregorian-from-absolute today))
3397 (kwds org-todo-keywords-for-agenda)
3398 (completion-ignore-case t)
3399 (org-select-this-todo-keyword
3400 (if (stringp arg) arg
3401 (and arg (integerp arg) (> arg 0)
3402 (nth (1- arg) kwds))))
3403 rtn rtnall files file pos)
3404 (when (equal arg '(4))
3405 (setq org-select-this-todo-keyword
3406 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3407 (mapcar 'list kwds) nil nil)))
3408 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3409 (org-set-local 'org-last-arg arg)
3410 (setq org-agenda-redo-command
3411 '(org-todo-list (or current-prefix-arg org-last-arg)))
3412 (setq files (org-agenda-files nil 'ifmode)
3413 rtnall nil)
3414 (while (setq file (pop files))
3415 (catch 'nextfile
3416 (org-check-agenda-file file)
3417 (setq rtn (org-agenda-get-day-entries file date :todo))
3418 (setq rtnall (append rtnall rtn))))
3419 (if org-agenda-overriding-header
3420 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3421 nil 'face 'org-agenda-structure) "\n")
3422 (insert "Global list of TODO items of type: ")
3423 (add-text-properties (point-min) (1- (point))
3424 (list 'face 'org-agenda-structure
3425 'short-heading
3426 (concat "ToDo: "
3427 (or org-select-this-todo-keyword "ALL"))))
3428 (org-agenda-mark-header-line (point-min))
3429 (setq pos (point))
3430 (insert (or org-select-this-todo-keyword "ALL") "\n")
3431 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3432 (setq pos (point))
3433 (unless org-agenda-multi
3434 (insert "Available with `N r': (0)ALL")
3435 (let ((n 0) s)
3436 (mapc (lambda (x)
3437 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3438 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3439 (insert "\n "))
3440 (insert " " s))
3441 kwds))
3442 (insert "\n"))
3443 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3444 (org-agenda-mark-header-line (point-min))
3445 (when rtnall
3446 (insert (org-finalize-agenda-entries rtnall) "\n"))
3447 (goto-char (point-min))
3448 (or org-agenda-multi (org-fit-agenda-window))
3449 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3450 (org-finalize-agenda)
3451 (setq buffer-read-only t)))
3453 ;;; Agenda tags match
3455 ;;;###autoload
3456 (defun org-tags-view (&optional todo-only match)
3457 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3458 The prefix arg TODO-ONLY limits the search to TODO entries."
3459 (interactive "P")
3460 (org-compile-prefix-format 'tags)
3461 (org-set-sorting-strategy 'tags)
3462 (let* ((org-tags-match-list-sublevels
3463 ;?????? (if todo-only t org-tags-match-list-sublevels))
3464 org-tags-match-list-sublevels)
3465 (completion-ignore-case t)
3466 rtn rtnall files file pos matcher
3467 buffer)
3468 (setq matcher (org-make-tags-matcher match)
3469 match (car matcher) matcher (cdr matcher))
3470 (org-prepare-agenda (concat "TAGS " match))
3471 (setq org-agenda-query-string match)
3472 (setq org-agenda-redo-command
3473 (list 'org-tags-view (list 'quote todo-only)
3474 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3475 (setq files (org-agenda-files nil 'ifmode)
3476 rtnall nil)
3477 (while (setq file (pop files))
3478 (catch 'nextfile
3479 (org-check-agenda-file file)
3480 (setq buffer (if (file-exists-p file)
3481 (org-get-agenda-file-buffer file)
3482 (error "No such file %s" file)))
3483 (if (not buffer)
3484 ;; If file does not exist, error message to agenda
3485 (setq rtn (list
3486 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3487 rtnall (append rtnall rtn))
3488 (with-current-buffer buffer
3489 (unless (org-mode-p)
3490 (error "Agenda file %s is not in `org-mode'" file))
3491 (save-excursion
3492 (save-restriction
3493 (if org-agenda-restrict
3494 (narrow-to-region org-agenda-restrict-begin
3495 org-agenda-restrict-end)
3496 (widen))
3497 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3498 (setq rtnall (append rtnall rtn))))))))
3499 (if org-agenda-overriding-header
3500 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3501 nil 'face 'org-agenda-structure) "\n")
3502 (insert "Headlines with TAGS match: ")
3503 (add-text-properties (point-min) (1- (point))
3504 (list 'face 'org-agenda-structure
3505 'short-heading
3506 (concat "Match: " match)))
3507 (setq pos (point))
3508 (insert match "\n")
3509 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3510 (setq pos (point))
3511 (unless org-agenda-multi
3512 (insert "Press `C-u r' to search again with new search string\n"))
3513 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3514 (org-agenda-mark-header-line (point-min))
3515 (when rtnall
3516 (insert (org-finalize-agenda-entries rtnall) "\n"))
3517 (goto-char (point-min))
3518 (or org-agenda-multi (org-fit-agenda-window))
3519 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3520 (org-finalize-agenda)
3521 (setq buffer-read-only t)))
3523 ;;; Agenda Finding stuck projects
3525 (defvar org-agenda-skip-regexp nil
3526 "Regular expression used in skipping subtrees for the agenda.
3527 This is basically a temporary global variable that can be set and then
3528 used by user-defined selections using `org-agenda-skip-function'.")
3530 (defvar org-agenda-overriding-header nil
3531 "When this is set during todo and tags searches, will replace header.
3532 This variable should not be set directly, but custom commands can bind it
3533 in the options section.")
3535 (defun org-agenda-skip-entry-when-regexp-matches ()
3536 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3537 If yes, it returns the end position of this entry, causing agenda commands
3538 to skip the entry but continuing the search in the subtree. This is a
3539 function that can be put into `org-agenda-skip-function' for the duration
3540 of a command."
3541 (let ((end (save-excursion (org-end-of-subtree t)))
3542 skip)
3543 (save-excursion
3544 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3545 (and skip end)))
3547 (defun org-agenda-skip-subtree-when-regexp-matches ()
3548 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3549 If yes, it returns the end position of this tree, causing agenda commands
3550 to skip this subtree. This is a function that can be put into
3551 `org-agenda-skip-function' for the duration of a command."
3552 (let ((end (save-excursion (org-end-of-subtree t)))
3553 skip)
3554 (save-excursion
3555 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3556 (and skip end)))
3558 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3559 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3560 If yes, it returns the end position of the current entry (NOT the tree),
3561 causing agenda commands to skip the entry but continuing the search in
3562 the subtree. This is a function that can be put into
3563 `org-agenda-skip-function' for the duration of a command. An important
3564 use of this function is for the stuck project list."
3565 (let ((end (save-excursion (org-end-of-subtree t)))
3566 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3567 skip)
3568 (save-excursion
3569 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3570 (and skip entry-end)))
3572 (defun org-agenda-skip-entry-if (&rest conditions)
3573 "Skip entry if any of CONDITIONS is true.
3574 See `org-agenda-skip-if' for details."
3575 (org-agenda-skip-if nil conditions))
3577 (defun org-agenda-skip-subtree-if (&rest conditions)
3578 "Skip entry if any of CONDITIONS is true.
3579 See `org-agenda-skip-if' for details."
3580 (org-agenda-skip-if t conditions))
3582 (defun org-agenda-skip-if (subtree conditions)
3583 "Checks current entity for CONDITIONS.
3584 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3585 the entry, i.e. the text before the next heading is checked.
3587 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3588 from different tests. Valid conditions are:
3590 scheduled Check if there is a scheduled cookie
3591 notscheduled Check if there is no scheduled cookie
3592 deadline Check if there is a deadline
3593 notdeadline Check if there is no deadline
3594 timestamp Check if there is a timestamp (also deadline or scheduled)
3595 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3596 regexp Check if regexp matches
3597 notregexp Check if regexp does not match.
3599 The regexp is taken from the conditions list, it must come right after
3600 the `regexp' or `notregexp' element.
3602 If any of these conditions is met, this function returns the end point of
3603 the entity, causing the search to continue from there. This is a function
3604 that can be put into `org-agenda-skip-function' for the duration of a command."
3605 (let (beg end m)
3606 (org-back-to-heading t)
3607 (setq beg (point)
3608 end (if subtree
3609 (progn (org-end-of-subtree t) (point))
3610 (progn (outline-next-heading) (1- (point)))))
3611 (goto-char beg)
3612 (and
3614 (and (memq 'scheduled conditions)
3615 (re-search-forward org-scheduled-time-regexp end t))
3616 (and (memq 'notscheduled conditions)
3617 (not (re-search-forward org-scheduled-time-regexp end t)))
3618 (and (memq 'deadline conditions)
3619 (re-search-forward org-deadline-time-regexp end t))
3620 (and (memq 'notdeadline conditions)
3621 (not (re-search-forward org-deadline-time-regexp end t)))
3622 (and (memq 'timestamp conditions)
3623 (re-search-forward org-ts-regexp end t))
3624 (and (memq 'nottimestamp conditions)
3625 (not (re-search-forward org-ts-regexp end t)))
3626 (and (setq m (memq 'regexp conditions))
3627 (stringp (nth 1 m))
3628 (re-search-forward (nth 1 m) end t))
3629 (and (setq m (memq 'notregexp conditions))
3630 (stringp (nth 1 m))
3631 (not (re-search-forward (nth 1 m) end t))))
3632 end)))
3634 ;;;###autoload
3635 (defun org-agenda-list-stuck-projects (&rest ignore)
3636 "Create agenda view for projects that are stuck.
3637 Stuck projects are project that have no next actions. For the definitions
3638 of what a project is and how to check if it stuck, customize the variable
3639 `org-stuck-projects'.
3640 MATCH is being ignored."
3641 (interactive)
3642 (let* ((org-agenda-skip-function
3643 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3644 ;; We could have used org-agenda-skip-if here.
3645 (org-agenda-overriding-header
3646 (or org-agenda-overriding-header "List of stuck projects: "))
3647 (matcher (nth 0 org-stuck-projects))
3648 (todo (nth 1 org-stuck-projects))
3649 (todo-wds (if (member "*" todo)
3650 (progn
3651 (org-prepare-agenda-buffers (org-agenda-files
3652 nil 'ifmode))
3653 (org-delete-all
3654 org-done-keywords-for-agenda
3655 (copy-sequence org-todo-keywords-for-agenda)))
3656 todo))
3657 (todo-re (concat "^\\*+[ \t]+\\("
3658 (mapconcat 'identity todo-wds "\\|")
3659 "\\)\\>"))
3660 (tags (nth 2 org-stuck-projects))
3661 (tags-re (if (member "*" tags)
3662 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3663 (if tags
3664 (concat "^\\*+ .*:\\("
3665 (mapconcat 'identity tags "\\|")
3666 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3667 (gen-re (nth 3 org-stuck-projects))
3668 (re-list
3669 (delq nil
3670 (list
3671 (if todo todo-re)
3672 (if tags tags-re)
3673 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3674 gen-re)))))
3675 (setq org-agenda-skip-regexp
3676 (if re-list
3677 (mapconcat 'identity re-list "\\|")
3678 (error "No information how to identify unstuck projects")))
3679 (org-tags-view nil matcher)
3680 (with-current-buffer org-agenda-buffer-name
3681 (setq org-agenda-redo-command
3682 '(org-agenda-list-stuck-projects
3683 (or current-prefix-arg org-last-arg))))))
3685 ;;; Diary integration
3687 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3688 (defvar list-diary-entries-hook)
3690 (defun org-get-entries-from-diary (date)
3691 "Get the (Emacs Calendar) diary entries for DATE."
3692 (require 'diary-lib)
3693 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3694 (fancy-diary-buffer diary-fancy-buffer)
3695 (diary-display-hook '(fancy-diary-display))
3696 (diary-display-function 'fancy-diary-display)
3697 (pop-up-frames nil)
3698 (list-diary-entries-hook
3699 (cons 'org-diary-default-entry list-diary-entries-hook))
3700 (diary-file-name-prefix-function nil) ; turn this feature off
3701 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3702 entries
3703 (org-disable-agenda-to-diary t))
3704 (save-excursion
3705 (save-window-excursion
3706 (funcall (if (fboundp 'diary-list-entries)
3707 'diary-list-entries 'list-diary-entries)
3708 date 1)))
3709 (if (not (get-buffer diary-fancy-buffer))
3710 (setq entries nil)
3711 (with-current-buffer diary-fancy-buffer
3712 (setq buffer-read-only nil)
3713 (if (zerop (buffer-size))
3714 ;; No entries
3715 (setq entries nil)
3716 ;; Omit the date and other unnecessary stuff
3717 (org-agenda-cleanup-fancy-diary)
3718 ;; Add prefix to each line and extend the text properties
3719 (if (zerop (buffer-size))
3720 (setq entries nil)
3721 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3722 (set-buffer-modified-p nil)
3723 (kill-buffer diary-fancy-buffer)))
3724 (when entries
3725 (setq entries (org-split-string entries "\n"))
3726 (setq entries
3727 (mapcar
3728 (lambda (x)
3729 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3730 ;; Extend the text properties to the beginning of the line
3731 (org-add-props x (text-properties-at (1- (length x)) x)
3732 'type "diary" 'date date))
3733 entries)))))
3735 (defvar org-agenda-cleanup-fancy-diary-hook nil
3736 "Hook run when the fancy diary buffer is cleaned up.")
3738 (defun org-agenda-cleanup-fancy-diary ()
3739 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3740 This gets rid of the date, the underline under the date, and
3741 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3742 date. It also removes lines that contain only whitespace."
3743 (goto-char (point-min))
3744 (if (looking-at ".*?:[ \t]*")
3745 (progn
3746 (replace-match "")
3747 (re-search-forward "\n=+$" nil t)
3748 (replace-match "")
3749 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3750 (re-search-forward "\n=+$" nil t)
3751 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3752 (goto-char (point-min))
3753 (while (re-search-forward "^ +\n" nil t)
3754 (replace-match ""))
3755 (goto-char (point-min))
3756 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3757 (replace-match ""))
3758 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3760 ;; Make sure entries from the diary have the right text properties.
3761 (eval-after-load "diary-lib"
3762 '(if (boundp 'diary-modify-entry-list-string-function)
3763 ;; We can rely on the hook, nothing to do
3765 ;; Hook not available, must use advice to make this work
3766 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3767 "Make the position visible."
3768 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3769 (stringp string)
3770 buffer-file-name)
3771 (setq string (org-modify-diary-entry-string string))))))
3773 (defun org-modify-diary-entry-string (string)
3774 "Add text properties to string, allowing org-mode to act on it."
3775 (org-add-props string nil
3776 'mouse-face 'highlight
3777 'help-echo (if buffer-file-name
3778 (format "mouse-2 or RET jump to diary file %s"
3779 (abbreviate-file-name buffer-file-name))
3781 'org-agenda-diary-link t
3782 'org-marker (org-agenda-new-marker (point-at-bol))))
3784 (defun org-diary-default-entry ()
3785 "Add a dummy entry to the diary.
3786 Needed to avoid empty dates which mess up holiday display."
3787 ;; Catch the error if dealing with the new add-to-diary-alist
3788 (when org-disable-agenda-to-diary
3789 (condition-case nil
3790 (org-add-to-diary-list original-date "Org-mode dummy" "")
3791 (error
3792 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3794 (defun org-add-to-diary-list (&rest args)
3795 (if (fboundp 'diary-add-to-list)
3796 (apply 'diary-add-to-list args)
3797 (apply 'add-to-diary-list args)))
3799 ;;;###autoload
3800 (defun org-diary (&rest args)
3801 "Return diary information from org-files.
3802 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3803 It accesses org files and extracts information from those files to be
3804 listed in the diary. The function accepts arguments specifying what
3805 items should be listed. The following arguments are allowed:
3807 :timestamp List the headlines of items containing a date stamp or
3808 date range matching the selected date. Deadlines will
3809 also be listed, on the expiration day.
3811 :sexp List entries resulting from diary-like sexps.
3813 :deadline List any deadlines past due, or due within
3814 `org-deadline-warning-days'. The listing occurs only
3815 in the diary for *today*, not at any other date. If
3816 an entry is marked DONE, it is no longer listed.
3818 :scheduled List all items which are scheduled for the given date.
3819 The diary for *today* also contains items which were
3820 scheduled earlier and are not yet marked DONE.
3822 :todo List all TODO items from the org-file. This may be a
3823 long list - so this is not turned on by default.
3824 Like deadlines, these entries only show up in the
3825 diary for *today*, not at any other date.
3827 The call in the diary file should look like this:
3829 &%%(org-diary) ~/path/to/some/orgfile.org
3831 Use a separate line for each org file to check. Or, if you omit the file name,
3832 all files listed in `org-agenda-files' will be checked automatically:
3834 &%%(org-diary)
3836 If you don't give any arguments (as in the example above), the default
3837 arguments (:deadline :scheduled :timestamp :sexp) are used.
3838 So the example above may also be written as
3840 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3842 The function expects the lisp variables `entry' and `date' to be provided
3843 by the caller, because this is how the calendar works. Don't use this
3844 function from a program - use `org-agenda-get-day-entries' instead."
3845 (when (> (- (org-float-time)
3846 org-agenda-last-marker-time)
3848 (org-agenda-reset-markers))
3849 (org-compile-prefix-format 'agenda)
3850 (org-set-sorting-strategy 'agenda)
3851 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3852 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3853 (list entry)
3854 (org-agenda-files t)))
3855 file rtn results)
3856 (org-prepare-agenda-buffers files)
3857 ;; If this is called during org-agenda, don't return any entries to
3858 ;; the calendar. Org Agenda will list these entries itself.
3859 (if org-disable-agenda-to-diary (setq files nil))
3860 (while (setq file (pop files))
3861 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3862 (setq results (append results rtn)))
3863 (if results
3864 (concat (org-finalize-agenda-entries results) "\n"))))
3866 ;;; Agenda entry finders
3868 (defun org-agenda-get-day-entries (file date &rest args)
3869 "Does the work for `org-diary' and `org-agenda'.
3870 FILE is the path to a file to be checked for entries. DATE is date like
3871 the one returned by `calendar-current-date'. ARGS are symbols indicating
3872 which kind of entries should be extracted. For details about these, see
3873 the documentation of `org-diary'."
3874 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3875 (let* ((org-startup-folded nil)
3876 (org-startup-align-all-tables nil)
3877 (buffer (if (file-exists-p file)
3878 (org-get-agenda-file-buffer file)
3879 (error "No such file %s" file)))
3880 arg results rtn deadline-results)
3881 (if (not buffer)
3882 ;; If file does not exist, make sure an error message ends up in diary
3883 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3884 (with-current-buffer buffer
3885 (unless (org-mode-p)
3886 (error "Agenda file %s is not in `org-mode'" file))
3887 (let ((case-fold-search nil))
3888 (save-excursion
3889 (save-restriction
3890 (if org-agenda-restrict
3891 (narrow-to-region org-agenda-restrict-begin
3892 org-agenda-restrict-end)
3893 (widen))
3894 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3895 (while (setq arg (pop args))
3896 (cond
3897 ((and (eq arg :todo)
3898 (equal date (calendar-current-date)))
3899 (setq rtn (org-agenda-get-todos))
3900 (setq results (append results rtn)))
3901 ((eq arg :timestamp)
3902 (setq rtn (org-agenda-get-blocks))
3903 (setq results (append results rtn))
3904 (setq rtn (org-agenda-get-timestamps))
3905 (setq results (append results rtn)))
3906 ((eq arg :sexp)
3907 (setq rtn (org-agenda-get-sexps))
3908 (setq results (append results rtn)))
3909 ((eq arg :scheduled)
3910 (setq rtn (org-agenda-get-scheduled deadline-results))
3911 (setq results (append results rtn)))
3912 ((eq arg :closed)
3913 (setq rtn (org-agenda-get-progress))
3914 (setq results (append results rtn)))
3915 ((eq arg :deadline)
3916 (setq rtn (org-agenda-get-deadlines))
3917 (setq deadline-results (copy-sequence rtn))
3918 (setq results (append results rtn))))))))
3919 results))))
3921 (defun org-agenda-get-todos ()
3922 "Return the TODO information for agenda display."
3923 (let* ((props (list 'face nil
3924 'done-face 'org-agenda-done
3925 'org-not-done-regexp org-not-done-regexp
3926 'org-todo-regexp org-todo-regexp
3927 'org-complex-heading-regexp org-complex-heading-regexp
3928 'mouse-face 'highlight
3929 'help-echo
3930 (format "mouse-2 or RET jump to org file %s"
3931 (abbreviate-file-name buffer-file-name))))
3932 (regexp (concat "^\\*+[ \t]+\\("
3933 (if org-select-this-todo-keyword
3934 (if (equal org-select-this-todo-keyword "*")
3935 org-todo-regexp
3936 (concat "\\<\\("
3937 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3938 "\\)\\>"))
3939 org-not-done-regexp)
3940 "[^\n\r]*\\)"))
3941 marker priority category tags todo-state
3942 ee txt beg end)
3943 (goto-char (point-min))
3944 (while (re-search-forward regexp nil t)
3945 (catch :skip
3946 (save-match-data
3947 (beginning-of-line)
3948 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3949 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3950 (goto-char (1+ beg))
3951 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3952 (throw :skip nil)))
3953 (goto-char beg)
3954 (org-agenda-skip)
3955 (goto-char (match-beginning 1))
3956 (setq marker (org-agenda-new-marker (match-beginning 0))
3957 category (org-get-category)
3958 txt (match-string 1)
3959 tags (org-get-tags-at (point))
3960 txt (org-format-agenda-item "" txt category tags)
3961 priority (1+ (org-get-priority txt))
3962 todo-state (org-get-todo-state))
3963 (org-add-props txt props
3964 'org-marker marker 'org-hd-marker marker
3965 'priority priority 'org-category category
3966 'type "todo" 'todo-state todo-state)
3967 (push txt ee)
3968 (if org-agenda-todo-list-sublevels
3969 (goto-char (match-end 1))
3970 (org-end-of-subtree 'invisible))))
3971 (nreverse ee)))
3973 ;;;###autoload
3974 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3975 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3976 (when (or org-agenda-todo-ignore-with-date
3977 org-agenda-todo-ignore-scheduled
3978 org-agenda-todo-ignore-deadlines)
3979 (setq end (or end (save-excursion (outline-next-heading) (point))))
3980 (save-excursion
3981 (or (and org-agenda-todo-ignore-with-date
3982 (re-search-forward org-ts-regexp end t))
3983 (and org-agenda-todo-ignore-scheduled
3984 (re-search-forward org-scheduled-time-regexp end t))
3985 (and org-agenda-todo-ignore-deadlines
3986 (re-search-forward org-deadline-time-regexp end t)
3987 (org-deadline-close (match-string 1)))))))
3989 (defconst org-agenda-no-heading-message
3990 "No heading for this item in buffer or region.")
3992 (defun org-agenda-get-timestamps ()
3993 "Return the date stamp information for agenda display."
3994 (let* ((props (list 'face nil
3995 'org-not-done-regexp org-not-done-regexp
3996 'org-todo-regexp org-todo-regexp
3997 'org-complex-heading-regexp org-complex-heading-regexp
3998 'mouse-face 'highlight
3999 'help-echo
4000 (format "mouse-2 or RET jump to org file %s"
4001 (abbreviate-file-name buffer-file-name))))
4002 (d1 (calendar-absolute-from-gregorian date))
4003 (remove-re
4004 (concat
4005 (regexp-quote
4006 (format-time-string
4007 "<%Y-%m-%d"
4008 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4009 ".*?>"))
4010 (regexp
4011 (concat
4012 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4013 (regexp-quote
4014 (substring
4015 (format-time-string
4016 (car org-time-stamp-formats)
4017 (apply 'encode-time ; DATE bound by calendar
4018 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4019 1 11))
4020 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4021 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4022 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4023 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4024 todo-state end-of-match)
4025 (goto-char (point-min))
4026 (while (setq end-of-match (re-search-forward regexp nil t))
4027 (setq b0 (match-beginning 0)
4028 b3 (match-beginning 3) e3 (match-end 3))
4029 (catch :skip
4030 (and (org-at-date-range-p) (throw :skip nil))
4031 (org-agenda-skip)
4032 (if (and (match-end 1)
4033 (not (= d1 (org-time-string-to-absolute
4034 (match-string 1) d1 nil
4035 org-agenda-repeating-timestamp-show-all))))
4036 (throw :skip nil))
4037 (if (and e3
4038 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4039 (throw :skip nil))
4040 (setq tmp (buffer-substring (max (point-min)
4041 (- b0 org-ds-keyword-length))
4043 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4044 inactivep (= (char-after b0) ?\[)
4045 deadlinep (string-match org-deadline-regexp tmp)
4046 scheduledp (string-match org-scheduled-regexp tmp)
4047 closedp (and org-agenda-include-inactive-timestamps
4048 (string-match org-closed-string tmp))
4049 clockp (and org-agenda-include-inactive-timestamps
4050 (or (string-match org-clock-string tmp)
4051 (string-match "]-+\\'" tmp)))
4052 todo-state (org-get-todo-state)
4053 donep (member todo-state org-done-keywords))
4054 (if (or scheduledp deadlinep closedp clockp
4055 (and donep org-agenda-skip-timestamp-if-done))
4056 (throw :skip t))
4057 (if (string-match ">" timestr)
4058 ;; substring should only run to end of time stamp
4059 (setq timestr (substring timestr 0 (match-end 0))))
4060 (setq marker (org-agenda-new-marker b0)
4061 category (org-get-category b0))
4062 (save-excursion
4063 (if (not (re-search-backward "^\\*+ " nil t))
4064 (setq txt org-agenda-no-heading-message)
4065 (goto-char (match-beginning 0))
4066 (setq hdmarker (org-agenda-new-marker)
4067 tags (org-get-tags-at))
4068 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4069 (setq head (match-string 1))
4070 (setq txt (org-format-agenda-item
4071 (if inactivep "[" nil)
4072 head category tags timestr nil
4073 remove-re)))
4074 (setq priority (org-get-priority txt))
4075 (org-add-props txt props
4076 'org-marker marker 'org-hd-marker hdmarker)
4077 (org-add-props txt nil 'priority priority
4078 'org-category category 'date date
4079 'todo-state todo-state
4080 'type "timestamp")
4081 (push txt ee))
4082 (if org-agenda-skip-additional-timestamps-same-entry
4083 (outline-next-heading)
4084 (goto-char end-of-match))))
4085 (nreverse ee)))
4087 (defun org-agenda-get-sexps ()
4088 "Return the sexp information for agenda display."
4089 (require 'diary-lib)
4090 (let* ((props (list 'face nil
4091 'mouse-face 'highlight
4092 'help-echo
4093 (format "mouse-2 or RET jump to org file %s"
4094 (abbreviate-file-name buffer-file-name))))
4095 (regexp "^&?%%(")
4096 marker category ee txt tags entry result beg b sexp sexp-entry
4097 todo-state)
4098 (goto-char (point-min))
4099 (while (re-search-forward regexp nil t)
4100 (catch :skip
4101 (org-agenda-skip)
4102 (setq beg (match-beginning 0))
4103 (goto-char (1- (match-end 0)))
4104 (setq b (point))
4105 (forward-sexp 1)
4106 (setq sexp (buffer-substring b (point)))
4107 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4108 (org-trim (match-string 1))
4109 ""))
4110 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4111 (when result
4112 (setq marker (org-agenda-new-marker beg)
4113 category (org-get-category beg)
4114 todo-state (org-get-todo-state))
4116 (if (string-match "\\S-" result)
4117 (setq txt result)
4118 (setq txt "SEXP entry returned empty string"))
4120 (setq txt (org-format-agenda-item
4121 "" txt category tags 'time))
4122 (org-add-props txt props 'org-marker marker)
4123 (org-add-props txt nil
4124 'org-category category 'date date 'todo-state todo-state
4125 'type "sexp")
4126 (push txt ee))))
4127 (nreverse ee)))
4129 (defalias 'org-get-closed 'org-agenda-get-progress)
4130 (defun org-agenda-get-progress ()
4131 "Return the logged TODO entries for agenda display."
4132 (let* ((props (list 'mouse-face 'highlight
4133 'org-not-done-regexp org-not-done-regexp
4134 'org-todo-regexp org-todo-regexp
4135 'org-complex-heading-regexp org-complex-heading-regexp
4136 'help-echo
4137 (format "mouse-2 or RET jump to org file %s"
4138 (abbreviate-file-name buffer-file-name))))
4139 (items (if (consp org-agenda-show-log)
4140 org-agenda-show-log
4141 org-agenda-log-mode-items))
4142 (parts
4143 (delq nil
4144 (list
4145 (if (memq 'closed items) (concat "\\<" org-closed-string))
4146 (if (memq 'clock items) (concat "\\<" org-clock-string))
4147 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4148 (parts-re (if parts (mapconcat 'identity parts "\\|")
4149 (error "`org-agenda-log-mode-items' is empty")))
4150 (regexp (concat
4151 "\\(" parts-re "\\)"
4152 " *\\["
4153 (regexp-quote
4154 (substring
4155 (format-time-string
4156 (car org-time-stamp-formats)
4157 (apply 'encode-time ; DATE bound by calendar
4158 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4159 1 11))))
4160 (org-agenda-search-headline-for-time nil)
4161 marker hdmarker priority category tags closedp statep clockp state
4162 ee txt extra timestr rest clocked)
4163 (goto-char (point-min))
4164 (while (re-search-forward regexp nil t)
4165 (catch :skip
4166 (org-agenda-skip)
4167 (setq marker (org-agenda-new-marker (match-beginning 0))
4168 closedp (equal (match-string 1) org-closed-string)
4169 statep (equal (string-to-char (match-string 1)) ?-)
4170 clockp (not (or closedp statep))
4171 state (and statep (match-string 2))
4172 category (org-get-category (match-beginning 0))
4173 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4175 (when (string-match "\\]" timestr)
4176 ;; substring should only run to end of time stamp
4177 (setq rest (substring timestr (match-end 0))
4178 timestr (substring timestr 0 (match-end 0)))
4179 (if (and (not closedp) (not statep)
4180 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4181 (progn (setq timestr (concat (substring timestr 0 -1)
4182 "-" (match-string 1 rest) "]"))
4183 (setq clocked (match-string 2 rest)))
4184 (setq clocked "-")))
4185 (save-excursion
4186 (cond
4187 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4188 (statep
4189 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4190 (setq extra (match-string 1))))
4191 (clockp
4192 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4193 (setq extra (match-string 1))))
4194 (t (setq extra nil)))
4195 (if (not (re-search-backward "^\\*+ " nil t))
4196 (setq txt org-agenda-no-heading-message)
4197 (goto-char (match-beginning 0))
4198 (setq hdmarker (org-agenda-new-marker)
4199 tags (org-get-tags-at))
4200 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4201 (setq txt (match-string 1))
4202 (when extra
4203 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4204 (setq txt (concat (substring txt 0 (match-beginning 1))
4205 " - " extra " " (match-string 2 txt)))
4206 (setq txt (concat txt " - " extra))))
4207 (setq txt (org-format-agenda-item
4208 (cond
4209 (closedp "Closed: ")
4210 (statep (concat "State: (" state ")"))
4211 (t (concat "Clocked: (" clocked ")")))
4212 txt category tags timestr)))
4213 (setq priority 100000)
4214 (org-add-props txt props
4215 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4216 'priority priority 'org-category category
4217 'type "closed" 'date date
4218 'undone-face 'org-warning 'done-face 'org-agenda-done)
4219 (push txt ee))
4220 (goto-char (point-at-eol))))
4221 (nreverse ee)))
4223 (defun org-agenda-get-deadlines ()
4224 "Return the deadline information for agenda display."
4225 (let* ((props (list 'mouse-face 'highlight
4226 'org-not-done-regexp org-not-done-regexp
4227 'org-todo-regexp org-todo-regexp
4228 'org-complex-heading-regexp org-complex-heading-regexp
4229 'help-echo
4230 (format "mouse-2 or RET jump to org file %s"
4231 (abbreviate-file-name buffer-file-name))))
4232 (regexp org-deadline-time-regexp)
4233 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4234 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4235 d2 diff dfrac wdays pos pos1 category tags
4236 ee txt head face s todo-state upcomingp donep timestr)
4237 (goto-char (point-min))
4238 (while (re-search-forward regexp nil t)
4239 (catch :skip
4240 (org-agenda-skip)
4241 (setq s (match-string 1)
4242 txt nil
4243 pos (1- (match-beginning 1))
4244 d2 (org-time-string-to-absolute
4245 (match-string 1) d1 'past
4246 org-agenda-repeating-timestamp-show-all)
4247 diff (- d2 d1)
4248 wdays (org-get-wdays s)
4249 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4250 upcomingp (and todayp (> diff 0)))
4251 ;; When to show a deadline in the calendar:
4252 ;; If the expiration is within wdays warning time.
4253 ;; Past-due deadlines are only shown on the current date
4254 (if (and (or (and (<= diff wdays)
4255 (and todayp (not org-agenda-only-exact-dates)))
4256 (= diff 0)))
4257 (save-excursion
4258 (setq todo-state (org-get-todo-state))
4259 (setq donep (member todo-state org-done-keywords))
4260 (if (and donep
4261 (or org-agenda-skip-deadline-if-done
4262 (not (= diff 0))))
4263 (setq txt nil)
4264 (setq category (org-get-category))
4265 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4266 (setq txt org-agenda-no-heading-message)
4267 (goto-char (match-end 0))
4268 (setq pos1 (match-beginning 0))
4269 (setq tags (org-get-tags-at pos1))
4270 (setq head (buffer-substring-no-properties
4271 (point)
4272 (progn (skip-chars-forward "^\r\n")
4273 (point))))
4274 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4275 (setq timestr
4276 (concat (substring s (match-beginning 1)) " "))
4277 (setq timestr 'time))
4278 (setq txt (org-format-agenda-item
4279 (if (= diff 0)
4280 (car org-agenda-deadline-leaders)
4281 (if (functionp
4282 (nth 1 org-agenda-deadline-leaders))
4283 (funcall
4284 (nth 1 org-agenda-deadline-leaders)
4285 diff date)
4286 (format (nth 1 org-agenda-deadline-leaders)
4287 diff)))
4288 head category tags
4289 (if (not (= diff 0)) nil timestr)))))
4290 (when txt
4291 (setq face (org-agenda-deadline-face dfrac wdays))
4292 (org-add-props txt props
4293 'org-marker (org-agenda-new-marker pos)
4294 'org-hd-marker (org-agenda-new-marker pos1)
4295 'priority (+ (- diff)
4296 (org-get-priority txt))
4297 'org-category category
4298 'todo-state todo-state
4299 'type (if upcomingp "upcoming-deadline" "deadline")
4300 'date (if upcomingp date d2)
4301 'face (if donep 'org-agenda-done face)
4302 'undone-face face 'done-face 'org-agenda-done)
4303 (push txt ee))))))
4304 (nreverse ee)))
4306 (defun org-agenda-deadline-face (fraction &optional wdays)
4307 "Return the face to displaying a deadline item.
4308 FRACTION is what fraction of the head-warning time has passed."
4309 (if (equal wdays 0) (setq fraction 1.))
4310 (let ((faces org-agenda-deadline-faces) f)
4311 (catch 'exit
4312 (while (setq f (pop faces))
4313 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4315 (defun org-agenda-get-scheduled (&optional deadline-results)
4316 "Return the scheduled information for agenda display."
4317 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4318 'org-todo-regexp org-todo-regexp
4319 'org-complex-heading-regexp org-complex-heading-regexp
4320 'done-face 'org-agenda-done
4321 'mouse-face 'highlight
4322 'help-echo
4323 (format "mouse-2 or RET jump to org file %s"
4324 (abbreviate-file-name buffer-file-name))))
4325 (regexp org-scheduled-time-regexp)
4326 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4327 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4329 (deadline-position-alist
4330 (mapcar (lambda (a) (and (setq mm (get-text-property
4331 0 'org-hd-marker a))
4332 (cons (marker-position mm) a)))
4333 deadline-results))
4334 d2 diff pos pos1 category tags donep
4335 ee txt head pastschedp todo-state face timestr s habitp)
4336 (goto-char (point-min))
4337 (while (re-search-forward regexp nil t)
4338 (catch :skip
4339 (org-agenda-skip)
4340 (setq s (match-string 1)
4341 txt nil
4342 pos (1- (match-beginning 1))
4343 d2 (org-time-string-to-absolute
4344 (match-string 1) d1 'past
4345 org-agenda-repeating-timestamp-show-all)
4346 diff (- d2 d1))
4347 (setq pastschedp (and todayp (< diff 0)))
4348 ;; When to show a scheduled item in the calendar:
4349 ;; If it is on or past the date.
4350 (when (or (and (< diff 0)
4351 (< (abs diff) org-scheduled-past-days)
4352 (and todayp (not org-agenda-only-exact-dates)))
4353 (= diff 0))
4354 (save-excursion
4355 (setq todo-state (org-get-todo-state))
4356 (setq donep (member todo-state org-done-keywords))
4357 (setq habitp (and (functionp 'org-is-habit-p)
4358 (org-is-habit-p)))
4359 (if (and donep
4360 (or habitp org-agenda-skip-scheduled-if-done
4361 (not (= diff 0))))
4362 (setq txt nil)
4363 (setq category (org-get-category))
4364 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4365 (setq txt org-agenda-no-heading-message)
4366 (goto-char (match-end 0))
4367 (setq pos1 (match-beginning 0))
4368 (if habitp
4369 (if (or (not org-habit-show-habits)
4370 (and (not todayp)
4371 org-habit-show-habits-only-for-today))
4372 (throw :skip nil))
4373 (if (and
4374 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4375 (and org-agenda-skip-scheduled-if-deadline-is-shown
4376 pastschedp))
4377 (setq mm (assoc pos1 deadline-position-alist)))
4378 (throw :skip nil)))
4379 (setq tags (org-get-tags-at))
4380 (setq head (buffer-substring-no-properties
4381 (point)
4382 (progn (skip-chars-forward "^\r\n") (point))))
4383 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4384 (setq timestr
4385 (concat (substring s (match-beginning 1)) " "))
4386 (setq timestr 'time))
4387 (setq txt (org-format-agenda-item
4388 (if (= diff 0)
4389 (car org-agenda-scheduled-leaders)
4390 (format (nth 1 org-agenda-scheduled-leaders)
4391 (- 1 diff)))
4392 head category tags
4393 (if (not (= diff 0)) nil timestr)
4394 nil nil habitp))))
4395 (when txt
4396 (setq face
4397 (cond
4398 ((and (not habitp) pastschedp)
4399 'org-scheduled-previously)
4400 (todayp 'org-scheduled-today)
4401 (t 'org-scheduled))
4402 habitp (and habitp (org-habit-parse-todo)))
4403 (org-add-props txt props
4404 'undone-face face
4405 'face (if donep 'org-agenda-done face)
4406 'org-marker (org-agenda-new-marker pos)
4407 'org-hd-marker (org-agenda-new-marker pos1)
4408 'type (if pastschedp "past-scheduled" "scheduled")
4409 'date (if pastschedp d2 date)
4410 'priority (if habitp
4411 (org-habit-get-priority habitp)
4412 (+ 94 (- 5 diff) (org-get-priority txt)))
4413 'org-category category
4414 'org-habit-p habitp
4415 'todo-state todo-state)
4416 (push txt ee))))))
4417 (nreverse ee)))
4419 (defun org-agenda-get-blocks ()
4420 "Return the date-range information for agenda display."
4421 (let* ((props (list 'face nil
4422 'org-not-done-regexp org-not-done-regexp
4423 'org-todo-regexp org-todo-regexp
4424 'org-complex-heading-regexp org-complex-heading-regexp
4425 'mouse-face 'highlight
4426 'help-echo
4427 (format "mouse-2 or RET jump to org file %s"
4428 (abbreviate-file-name buffer-file-name))))
4429 (regexp org-tr-regexp)
4430 (d0 (calendar-absolute-from-gregorian date))
4431 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4432 head donep)
4433 (goto-char (point-min))
4434 (while (re-search-forward regexp nil t)
4435 (catch :skip
4436 (org-agenda-skip)
4437 (setq pos (point))
4438 (setq timestr (match-string 0)
4439 s1 (match-string 1)
4440 s2 (match-string 2)
4441 d1 (time-to-days (org-time-string-to-time s1))
4442 d2 (time-to-days (org-time-string-to-time s2)))
4443 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4444 ;; Only allow days between the limits, because the normal
4445 ;; date stamps will catch the limits.
4446 (save-excursion
4447 (setq todo-state (org-get-todo-state))
4448 (setq donep (member todo-state org-done-keywords))
4449 (if (and donep org-agenda-skip-timestamp-if-done)
4450 (throw :skip t))
4451 (setq marker (org-agenda-new-marker (point)))
4452 (setq category (org-get-category))
4453 (if (not (re-search-backward "^\\*+ " nil t))
4454 (setq txt org-agenda-no-heading-message)
4455 (goto-char (match-beginning 0))
4456 (setq hdmarker (org-agenda-new-marker (point)))
4457 (setq tags (org-get-tags-at))
4458 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4459 (setq head (match-string 1))
4460 (setq txt (org-format-agenda-item
4461 (format
4462 (nth (if (= d1 d2) 0 1)
4463 org-agenda-timerange-leaders)
4464 (1+ (- d0 d1)) (1+ (- d2 d1)))
4465 head category tags
4466 (if (= d0 d1) timestr))))
4467 (org-add-props txt props
4468 'org-marker marker 'org-hd-marker hdmarker
4469 'type "block" 'date date
4470 'todo-state todo-state
4471 'priority (org-get-priority txt) 'org-category category)
4472 (push txt ee)))
4473 (goto-char pos)))
4474 ;; Sort the entries by expiration date.
4475 (nreverse ee)))
4477 ;;; Agenda presentation and sorting
4479 (defvar org-prefix-has-time nil
4480 "A flag, set by `org-compile-prefix-format'.
4481 The flag is set if the currently compiled format contains a `%t'.")
4482 (defvar org-prefix-has-tag nil
4483 "A flag, set by `org-compile-prefix-format'.
4484 The flag is set if the currently compiled format contains a `%T'.")
4485 (defvar org-prefix-has-effort nil
4486 "A flag, set by `org-compile-prefix-format'.
4487 The flag is set if the currently compiled format contains a `%e'.")
4488 (defvar org-prefix-category-length nil
4489 "Used by `org-compile-prefix-format' to remember the category field widh.")
4490 (defvar org-prefix-category-max-length nil
4491 "Used by `org-compile-prefix-format' to remember the category field widh.")
4493 (defun org-format-agenda-item (extra txt &optional category tags dotime
4494 noprefix remove-re habitp)
4495 "Format TXT to be inserted into the agenda buffer.
4496 In particular, it adds the prefix and corresponding text properties. EXTRA
4497 must be a string and replaces the `%s' specifier in the prefix format.
4498 CATEGORY (string, symbol or nil) may be used to overrule the default
4499 category taken from local variable or file name. It will replace the `%c'
4500 specifier in the format. DOTIME, when non-nil, indicates that a
4501 time-of-day should be extracted from TXT for sorting of this entry, and for
4502 the `%t' specifier in the format. When DOTIME is a string, this string is
4503 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4504 only the correctly processes TXT should be returned - this is used by
4505 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4506 Any match of REMOVE-RE will be removed from TXT."
4507 (save-match-data
4508 ;; Diary entries sometimes have extra whitespace at the beginning
4509 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4510 (when org-agenda-show-inherited-tags
4511 ;; Fix the tags part in txt
4512 (setq txt (org-agenda-add-inherited-tags txt tags)))
4513 (let* ((category (or category
4514 org-category
4515 (if buffer-file-name
4516 (file-name-sans-extension
4517 (file-name-nondirectory buffer-file-name))
4518 "")))
4519 ;; time, tag, effort are needed for the eval of the prefix format
4520 (tag (if tags (nth (1- (length tags)) tags) ""))
4521 time effort neffort
4522 (ts (if dotime (concat
4523 (if (stringp dotime) dotime "")
4524 (and org-agenda-search-headline-for-time txt))))
4525 (time-of-day (and dotime (org-get-time-of-day ts)))
4526 stamp plain s0 s1 s2 t1 t2 rtn srp l
4527 duration thecategory)
4528 (and (org-mode-p) buffer-file-name
4529 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4530 (when (and dotime time-of-day)
4531 ;; Extract starting and ending time and move them to prefix
4532 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4533 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4534 (setq s0 (match-string 0 ts)
4535 srp (and stamp (match-end 3))
4536 s1 (match-string (if plain 1 2) ts)
4537 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4539 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4540 ;; them, we might want to remove them there to avoid duplication.
4541 ;; The user can turn this off with a variable.
4542 (if (and org-prefix-has-time
4543 org-agenda-remove-times-when-in-prefix (or stamp plain)
4544 (string-match (concat (regexp-quote s0) " *") txt)
4545 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4546 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4547 (= (match-beginning 0) 0)
4549 (setq txt (replace-match "" nil nil txt))))
4550 ;; Normalize the time(s) to 24 hour
4551 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4552 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4553 ;; Compute the duration
4554 (when s1
4555 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4556 (string-to-number (substring s1 3)))
4557 t2 (cond
4558 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4559 (string-to-number (substring s2 3))))
4560 (org-agenda-default-appointment-duration
4561 (+ t1 org-agenda-default-appointment-duration))
4562 (t nil)))
4563 (setq duration (if t2 (- t2 t1)))))
4565 (when (and s1 (not s2) org-agenda-default-appointment-duration
4566 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4567 (let ((m (+ (string-to-number (match-string 2 s1))
4568 (* 60 (string-to-number (match-string 1 s1)))
4569 org-agenda-default-appointment-duration))
4571 (setq h (/ m 60) m (- m (* h 60)))
4572 (setq s2 (format "%02d:%02d" h m))))
4574 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4575 txt)
4576 ;; Tags are in the string
4577 (if (or (eq org-agenda-remove-tags t)
4578 (and org-agenda-remove-tags
4579 org-prefix-has-tag))
4580 (setq txt (replace-match "" t t txt))
4581 (setq txt (replace-match
4582 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4583 (match-string 2 txt))
4584 t t txt))))
4585 (when (org-mode-p)
4586 (setq effort
4587 (condition-case nil
4588 (org-get-effort
4589 (or (get-text-property 0 'org-hd-marker txt)
4590 (get-text-property 0 'org-marker txt)))
4591 (error nil)))
4592 (when effort
4593 (setq neffort (org-hh:mm-string-to-minutes effort)
4594 effort (setq effort (concat "[" effort "]" )))))
4596 (when remove-re
4597 (while (string-match remove-re txt)
4598 (setq txt (replace-match "" t t txt))))
4600 ;; Create the final string
4601 (if noprefix
4602 (setq rtn txt)
4603 ;; Prepare the variables needed in the eval of the compiled format
4604 (setq time (cond (s2 (concat s1 "-" s2))
4605 (s1 (concat s1 "......"))
4606 (t ""))
4607 extra (or (and (not habitp) extra) "")
4608 category (if (symbolp category) (symbol-name category) category)
4609 thecategory (copy-sequence category))
4610 (if (string-match org-bracket-link-regexp category)
4611 (progn
4612 (setq l (if (match-end 3)
4613 (- (match-end 3) (match-beginning 3))
4614 (- (match-end 1) (match-beginning 1))))
4615 (when (< l (or org-prefix-category-length 0))
4616 (setq category (copy-sequence category))
4617 (org-add-props category nil
4618 'extra-space (make-string
4619 (- org-prefix-category-length l 1) ?\ ))))
4620 (if (and org-prefix-category-max-length
4621 (>= (length category) org-prefix-category-max-length))
4622 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
4623 ;; Evaluate the compiled format
4624 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4626 ;; And finally add the text properties
4627 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
4628 (org-add-props rtn nil
4629 'org-category (if thecategory (downcase thecategory) category)
4630 'tags (mapcar 'org-downcase-keep-props tags)
4631 'org-highest-priority org-highest-priority
4632 'org-lowest-priority org-lowest-priority
4633 'prefix-length (- (length rtn) (length txt))
4634 'time-of-day time-of-day
4635 'duration duration
4636 'effort effort
4637 'effort-minutes neffort
4638 'txt txt
4639 'time time
4640 'extra extra
4641 'dotime dotime))))
4643 (defun org-agenda-add-inherited-tags (txt tags)
4644 "Remove tags string from TXT, and add complete list of tags.
4645 The new list includes inherited tags. If any inherited tags are present,
4646 a double colon separates inherited tags from local tags."
4647 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4648 (setq txt (substring txt 0 (match-beginning 0))))
4649 (when tags
4650 (let ((have-i (get-text-property 0 'inherited (car tags)))
4652 (setq txt (concat txt " :"
4653 (mapconcat
4654 (lambda (x)
4655 (setq i (get-text-property 0 'inherited x))
4656 (if (and have-i (not i))
4657 (progn
4658 (setq have-i nil)
4659 (concat ":" x))
4661 tags ":")
4662 (if have-i "::" ":")))))
4663 txt)
4665 (defun org-downcase-keep-props (s)
4666 (let ((props (text-properties-at 0 s)))
4667 (setq s (downcase s))
4668 (add-text-properties 0 (length s) props s)
4671 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4672 (defvar org-agenda-sorting-strategy-selected nil)
4674 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4675 (catch 'exit
4676 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4677 ((and todayp (member 'today (car org-agenda-time-grid))))
4678 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4679 ((member 'weekly (car org-agenda-time-grid)))
4680 (t (throw 'exit list)))
4681 (let* ((have (delq nil (mapcar
4682 (lambda (x) (get-text-property 1 'time-of-day x))
4683 list)))
4684 (string (nth 1 org-agenda-time-grid))
4685 (gridtimes (nth 2 org-agenda-time-grid))
4686 (req (car org-agenda-time-grid))
4687 (remove (member 'remove-match req))
4688 new time)
4689 (if (and (member 'require-timed req) (not have))
4690 ;; don't show empty grid
4691 (throw 'exit list))
4692 (while (setq time (pop gridtimes))
4693 (unless (and remove (member time have))
4694 (setq time (int-to-string time))
4695 (push (org-format-agenda-item
4696 nil string "" nil
4697 (concat (substring time 0 -2) ":" (substring time -2)))
4698 new)
4699 (put-text-property
4700 1 (length (car new)) 'face 'org-time-grid (car new))))
4701 (if (member 'time-up org-agenda-sorting-strategy-selected)
4702 (append new list)
4703 (append list new)))))
4705 (defun org-compile-prefix-format (key)
4706 "Compile the prefix format into a Lisp form that can be evaluated.
4707 The resulting form is returned and stored in the variable
4708 `org-prefix-format-compiled'."
4709 (setq org-prefix-has-time nil org-prefix-has-tag nil
4710 org-prefix-category-length nil org-prefix-has-effort nil)
4711 (let ((s (cond
4712 ((stringp org-agenda-prefix-format)
4713 org-agenda-prefix-format)
4714 ((assq key org-agenda-prefix-format)
4715 (cdr (assq key org-agenda-prefix-format)))
4716 (t " %-12:c%?-12t% s")))
4717 (start 0)
4718 varform vars var e c f opt)
4719 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4720 s start)
4721 (setq var (cdr (assoc (match-string 4 s)
4722 '(("c" . category) ("t" . time) ("s" . extra)
4723 ("T" . tag) ("e" . effort))))
4724 c (or (match-string 3 s) "")
4725 opt (match-beginning 1)
4726 start (1+ (match-beginning 0)))
4727 (if (equal var 'time) (setq org-prefix-has-time t))
4728 (if (equal var 'tag) (setq org-prefix-has-tag t))
4729 (if (equal var 'effort) (setq org-prefix-has-effort t))
4730 (setq f (concat "%" (match-string 2 s) "s"))
4731 (when (equal var 'category)
4732 (setq org-prefix-category-length
4733 (floor (abs (string-to-number (match-string 2 s)))))
4734 (setq org-prefix-category-max-length
4735 (let ((x (match-string 2 s)))
4736 (save-match-data
4737 (if (string-match "\\.[0-9]+" x)
4738 (string-to-number (substring (match-string 0 x) 1)))))))
4739 (if opt
4740 (setq varform
4741 `(if (equal "" ,var)
4743 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4744 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
4745 (setq s (replace-match "%s" t nil s))
4746 (push varform vars))
4747 (setq vars (nreverse vars))
4748 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4750 (defun org-set-sorting-strategy (key)
4751 (if (symbolp (car org-agenda-sorting-strategy))
4752 ;; the old format
4753 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4754 (setq org-agenda-sorting-strategy-selected
4755 (or (cdr (assq key org-agenda-sorting-strategy))
4756 (cdr (assq 'agenda org-agenda-sorting-strategy))
4757 '(time-up category-keep priority-down)))))
4759 (defun org-get-time-of-day (s &optional string mod24)
4760 "Check string S for a time of day.
4761 If found, return it as a military time number between 0 and 2400.
4762 If not found, return nil.
4763 The optional STRING argument forces conversion into a 5 character wide string
4764 HH:MM."
4765 (save-match-data
4766 (when
4767 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4768 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4769 (let* ((h (string-to-number (match-string 1 s)))
4770 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4771 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4772 (am-p (equal ampm "am"))
4773 (h1 (cond ((not ampm) h)
4774 ((= h 12) (if am-p 0 12))
4775 (t (+ h (if am-p 0 12)))))
4776 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4777 (mod h1 24) h1))
4778 (t0 (+ (* 100 h2) m))
4779 (t1 (concat (if (>= h1 24) "+" " ")
4780 (if (< t0 100) "0" "")
4781 (if (< t0 10) "0" "")
4782 (int-to-string t0))))
4783 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4785 (defun org-finalize-agenda-entries (list &optional nosort)
4786 "Sort and concatenate the agenda items."
4787 (setq list (mapcar 'org-agenda-highlight-todo list))
4788 (if nosort
4789 list
4790 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4792 (defun org-agenda-highlight-todo (x)
4793 (let ((org-done-keywords org-done-keywords-for-agenda)
4794 re pl)
4795 (if (eq x 'line)
4796 (save-excursion
4797 (beginning-of-line 1)
4798 (setq re (org-get-at-bol 'org-todo-regexp))
4799 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
4800 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4801 (add-text-properties (match-beginning 0) (match-end 1)
4802 (list 'face (org-get-todo-face 1)))
4803 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4804 (delete-region (match-beginning 1) (1- (match-end 0)))
4805 (goto-char (match-beginning 1))
4806 (insert (format org-agenda-todo-keyword-format s)))))
4807 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4808 pl (get-text-property 0 'prefix-length x))
4809 (when (and re
4810 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4811 x (or pl 0)) pl))
4812 (add-text-properties
4813 (or (match-end 1) (match-end 0)) (match-end 0)
4814 (list 'face (org-get-todo-face (match-string 2 x)))
4816 (setq x (concat (substring x 0 (match-end 1))
4817 (format org-agenda-todo-keyword-format
4818 (match-string 2 x))
4819 (org-add-props " " (text-properties-at 0 x))
4820 (substring x (match-end 3)))))
4821 x)))
4823 (defsubst org-cmp-priority (a b)
4824 "Compare the priorities of string A and B."
4825 (let ((pa (or (get-text-property 1 'priority a) 0))
4826 (pb (or (get-text-property 1 'priority b) 0)))
4827 (cond ((> pa pb) +1)
4828 ((< pa pb) -1)
4829 (t nil))))
4831 (defsubst org-cmp-effort (a b)
4832 "Compare the priorities of string A and B."
4833 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4834 (ea (or (get-text-property 1 'effort-minutes a) def))
4835 (eb (or (get-text-property 1 'effort-minutes b) def)))
4836 (cond ((> ea eb) +1)
4837 ((< ea eb) -1)
4838 (t nil))))
4840 (defsubst org-cmp-category (a b)
4841 "Compare the string values of categories of strings A and B."
4842 (let ((ca (or (get-text-property 1 'org-category a) ""))
4843 (cb (or (get-text-property 1 'org-category b) "")))
4844 (cond ((string-lessp ca cb) -1)
4845 ((string-lessp cb ca) +1)
4846 (t nil))))
4848 (defsubst org-cmp-todo-state (a b)
4849 "Compare the todo states of strings A and B."
4850 (let* ((ma (or (get-text-property 1 'org-marker a)
4851 (get-text-property 1 'org-hd-marker a)))
4852 (mb (or (get-text-property 1 'org-marker b)
4853 (get-text-property 1 'org-hd-marker b)))
4854 (fa (and ma (marker-buffer ma)))
4855 (fb (and mb (marker-buffer mb)))
4856 (todo-kwds
4857 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4858 (and fb (with-current-buffer fb org-todo-keywords-1))))
4859 (ta (or (get-text-property 1 'todo-state a) ""))
4860 (tb (or (get-text-property 1 'todo-state b) ""))
4861 (la (- (length (member ta todo-kwds))))
4862 (lb (- (length (member tb todo-kwds))))
4863 (donepa (member ta org-done-keywords-for-agenda))
4864 (donepb (member tb org-done-keywords-for-agenda)))
4865 (cond ((and donepa (not donepb)) -1)
4866 ((and (not donepa) donepb) +1)
4867 ((< la lb) -1)
4868 ((< lb la) +1)
4869 (t nil))))
4871 (defsubst org-cmp-tag (a b)
4872 "Compare the string values of the first tags of A and B."
4873 (let ((ta (car (last (get-text-property 1 'tags a))))
4874 (tb (car (last (get-text-property 1 'tags b)))))
4875 (cond ((not ta) +1)
4876 ((not tb) -1)
4877 ((string-lessp ta tb) -1)
4878 ((string-lessp tb ta) +1)
4879 (t nil))))
4881 (defsubst org-cmp-time (a b)
4882 "Compare the time-of-day values of strings A and B."
4883 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4884 (ta (or (get-text-property 1 'time-of-day a) def))
4885 (tb (or (get-text-property 1 'time-of-day b) def)))
4886 (cond ((< ta tb) -1)
4887 ((< tb ta) +1)
4888 (t nil))))
4890 (defsubst org-cmp-habit-p (a b)
4891 "Compare the todo states of strings A and B."
4892 (let ((ha (get-text-property 1 'org-habit-p a))
4893 (hb (get-text-property 1 'org-habit-p b)))
4894 (cond ((and ha (not hb)) -1)
4895 ((and (not ha) hb) +1)
4896 (t nil))))
4898 (defun org-entries-lessp (a b)
4899 "Predicate for sorting agenda entries."
4900 ;; The following variables will be used when the form is evaluated.
4901 ;; So even though the compiler complains, keep them.
4902 (let* ((time-up (org-cmp-time a b))
4903 (time-down (if time-up (- time-up) nil))
4904 (priority-up (org-cmp-priority a b))
4905 (priority-down (if priority-up (- priority-up) nil))
4906 (effort-up (org-cmp-effort a b))
4907 (effort-down (if effort-up (- effort-up) nil))
4908 (category-up (org-cmp-category a b))
4909 (category-down (if category-up (- category-up) nil))
4910 (category-keep (if category-up +1 nil))
4911 (tag-up (org-cmp-tag a b))
4912 (tag-down (if tag-up (- tag-up) nil))
4913 (todo-state-up (org-cmp-todo-state a b))
4914 (todo-state-down (if todo-state-up (- todo-state-up) nil))
4915 (habit-up (org-cmp-habit-p a b))
4916 (habit-down (if habit-up (- habit-up) nil))
4917 user-defined-up user-defined-down)
4918 (if (and org-agenda-cmp-user-defined
4919 (functionp org-agenda-cmp-user-defined))
4920 (setq user-defined-up
4921 (funcall org-agenda-cmp-user-defined a b)
4922 user-defined-down (if user-defined-up (- user-defined-up) nil)))
4923 (cdr (assoc
4924 (eval (cons 'or org-agenda-sorting-strategy-selected))
4925 '((-1 . t) (1 . nil) (nil . nil))))))
4927 ;;; Agenda restriction lock
4929 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4930 "Overlay to mark the headline to which arenda commands are restricted.")
4931 (org-overlay-put org-agenda-restriction-lock-overlay
4932 'face 'org-agenda-restriction-lock)
4933 (org-overlay-put org-agenda-restriction-lock-overlay
4934 'help-echo "Agendas are currently limited to this subtree.")
4935 (org-detach-overlay org-agenda-restriction-lock-overlay)
4937 (defun org-agenda-set-restriction-lock (&optional type)
4938 "Set restriction lock for agenda, to current subtree or file.
4939 Restriction will be the file if TYPE is `file', or if type is the
4940 universal prefix '(4), or if the cursor is before the first headline
4941 in the file. Otherwise, restriction will be to the current subtree."
4942 (interactive "P")
4943 (and (equal type '(4)) (setq type 'file))
4944 (setq type (cond
4945 (type type)
4946 ((org-at-heading-p) 'subtree)
4947 ((condition-case nil (org-back-to-heading t) (error nil))
4948 'subtree)
4949 (t 'file)))
4950 (if (eq type 'subtree)
4951 (progn
4952 (setq org-agenda-restrict t)
4953 (setq org-agenda-overriding-restriction 'subtree)
4954 (put 'org-agenda-files 'org-restrict
4955 (list (buffer-file-name (buffer-base-buffer))))
4956 (org-back-to-heading t)
4957 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4958 (move-marker org-agenda-restrict-begin (point))
4959 (move-marker org-agenda-restrict-end
4960 (save-excursion (org-end-of-subtree t)))
4961 (message "Locking agenda restriction to subtree"))
4962 (put 'org-agenda-files 'org-restrict
4963 (list (buffer-file-name (buffer-base-buffer))))
4964 (setq org-agenda-restrict nil)
4965 (setq org-agenda-overriding-restriction 'file)
4966 (move-marker org-agenda-restrict-begin nil)
4967 (move-marker org-agenda-restrict-end nil)
4968 (message "Locking agenda restriction to file"))
4969 (setq current-prefix-arg nil)
4970 (org-agenda-maybe-redo))
4972 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4973 "Remove the agenda restriction lock."
4974 (interactive "P")
4975 (org-detach-overlay org-agenda-restriction-lock-overlay)
4976 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4977 (setq org-agenda-overriding-restriction nil)
4978 (setq org-agenda-restrict nil)
4979 (put 'org-agenda-files 'org-restrict nil)
4980 (move-marker org-agenda-restrict-begin nil)
4981 (move-marker org-agenda-restrict-end nil)
4982 (setq current-prefix-arg nil)
4983 (message "Agenda restriction lock removed")
4984 (or noupdate (org-agenda-maybe-redo)))
4986 (defun org-agenda-maybe-redo ()
4987 "If there is any window showing the agenda view, update it."
4988 (let ((w (get-buffer-window org-agenda-buffer-name t))
4989 (w0 (selected-window)))
4990 (when w
4991 (select-window w)
4992 (org-agenda-redo)
4993 (select-window w0)
4994 (if org-agenda-overriding-restriction
4995 (message "Agenda view shifted to new %s restriction"
4996 org-agenda-overriding-restriction)
4997 (message "Agenda restriction lock removed")))))
4999 ;;; Agenda commands
5001 (defun org-agenda-check-type (error &rest types)
5002 "Check if agenda buffer is of allowed type.
5003 If ERROR is non-nil, throw an error, otherwise just return nil."
5004 (if (memq org-agenda-type types)
5006 (if error
5007 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5008 nil)))
5010 (defun org-agenda-quit ()
5011 "Exit agenda by removing the window or the buffer."
5012 (interactive)
5013 (if org-agenda-columns-active
5014 (org-columns-quit)
5015 (let ((buf (current-buffer)))
5016 (if (eq org-agenda-window-setup 'other-frame)
5017 (progn
5018 (kill-buffer buf)
5019 (org-agenda-reset-markers)
5020 (org-columns-remove-overlays)
5021 (setq org-agenda-archives-mode nil)
5022 (delete-frame))
5023 (and (not (eq org-agenda-window-setup 'current-window))
5024 (not (one-window-p))
5025 (delete-window))
5026 (kill-buffer buf)
5027 (org-agenda-reset-markers)
5028 (org-columns-remove-overlays)
5029 (setq org-agenda-archives-mode nil)))
5030 ;; Maybe restore the pre-agenda window configuration.
5031 (and org-agenda-restore-windows-after-quit
5032 (not (eq org-agenda-window-setup 'other-frame))
5033 org-pre-agenda-window-conf
5034 (set-window-configuration org-pre-agenda-window-conf))))
5036 (defun org-agenda-exit ()
5037 "Exit agenda by removing the window or the buffer.
5038 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5039 Org-mode buffers visited directly by the user will not be touched."
5040 (interactive)
5041 (org-release-buffers org-agenda-new-buffers)
5042 (setq org-agenda-new-buffers nil)
5043 (org-agenda-quit))
5045 (defun org-agenda-execute (arg)
5046 "Execute another agenda command, keeping same window.\\<global-map>
5047 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
5048 (interactive "P")
5049 (let ((org-agenda-window-setup 'current-window))
5050 (org-agenda arg)))
5052 (defun org-agenda-redo ()
5053 "Rebuild Agenda.
5054 When this is the global TODO list, a prefix argument will be interpreted."
5055 (interactive)
5056 (let* ((org-agenda-keep-modes t)
5057 (filter org-agenda-filter)
5058 (preset (get 'org-agenda-filter :preset-filter))
5059 (cols org-agenda-columns-active)
5060 (line (org-current-line))
5061 (window-line (- line (org-current-line (window-start))))
5062 (lprops (get 'org-agenda-redo-command 'org-lprops)))
5063 (put 'org-agenda-filter :preset-filter nil)
5064 (and cols (org-columns-quit))
5065 (message "Rebuilding agenda buffer...")
5066 (org-let lprops '(eval org-agenda-redo-command))
5067 (setq org-agenda-undo-list nil
5068 org-agenda-pending-undo-list nil)
5069 (message "Rebuilding agenda buffer...done")
5070 (put 'org-agenda-filter :preset-filter preset)
5071 (and (or filter preset) (org-agenda-filter-apply filter))
5072 (and cols (interactive-p) (org-agenda-columns))
5073 (org-goto-line line)
5074 (recenter window-line)))
5077 (defvar org-global-tags-completion-table nil)
5078 (defvar org-agenda-filter-form nil)
5079 (defun org-agenda-filter-by-tag (strip &optional char narrow)
5080 "Keep only those lines in the agenda buffer that have a specific tag.
5081 The tag is selected with its fast selection letter, as configured.
5082 With prefix argument STRIP, remove all lines that do have the tag.
5083 A lisp caller can specify CHAR. NARROW means that the new tag should be
5084 used to narrow the search - the interactive user can also press `-' or `+'
5085 to switch to narrowing."
5086 (interactive "P")
5087 (let* ((alist org-tag-alist-for-agenda)
5088 (tag-chars (mapconcat
5089 (lambda (x) (if (and (not (symbolp (car x)))
5090 (cdr x))
5091 (char-to-string (cdr x))
5092 ""))
5093 alist ""))
5094 (efforts (org-split-string
5095 (or (cdr (assoc (concat org-effort-property "_ALL")
5096 org-global-properties))
5097 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5098 (effort-op org-agenda-filter-effort-default-operator)
5099 (effort-prompt "")
5100 (inhibit-read-only t)
5101 (current org-agenda-filter)
5102 char a n tag)
5103 (unless char
5104 (message
5105 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5106 (if narrow "Narrow" "Filter") tag-chars
5107 (if org-agenda-auto-exclude-function "[RET], " ""))
5108 (setq char (read-char)))
5109 (when (member char '(?+ ?-))
5110 ;; Narrowing down
5111 (cond ((equal char ?-) (setq strip t narrow t))
5112 ((equal char ?+) (setq strip nil narrow t)))
5113 (message
5114 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5115 (setq char (read-char)))
5116 (when (member char '(?< ?> ?= ??))
5117 ;; An effort operator
5118 (setq effort-op (char-to-string char))
5119 (setq alist nil) ; to make sure it will be interpreted as effort.
5120 (unless (equal char ??)
5121 (loop for i from 0 to 9 do
5122 (setq effort-prompt
5123 (concat
5124 effort-prompt " ["
5125 (if (= i 9) "0" (int-to-string (1+ i)))
5126 "]" (nth i efforts))))
5127 (message "Effort%s: %s " effort-op effort-prompt)
5128 (setq char (read-char))
5129 (when (or (< char ?0) (> char ?9))
5130 (error "Need 1-9,0 to select effort" ))))
5131 (when (equal char ?\t)
5132 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5133 (org-set-local 'org-global-tags-completion-table
5134 (org-global-tags-completion-table)))
5135 (let ((completion-ignore-case t))
5136 (setq tag (org-icompleting-read
5137 "Tag: " org-global-tags-completion-table))))
5138 (cond
5139 ((equal char ?\r)
5140 (org-agenda-filter-by-tag-show-all)
5141 (when org-agenda-auto-exclude-function
5142 (setq org-agenda-filter '())
5143 (dolist (tag org-tag-alist-for-agenda)
5144 (let ((modifier (funcall org-agenda-auto-exclude-function
5145 (car tag))))
5146 (if modifier
5147 (push modifier org-agenda-filter))))
5148 (if (not (null org-agenda-filter))
5149 (org-agenda-filter-apply org-agenda-filter))))
5150 ((equal char ?/)
5151 (org-agenda-filter-by-tag-show-all)
5152 (when (get 'org-agenda-filter :preset-filter)
5153 (org-agenda-filter-apply org-agenda-filter)))
5154 ((or (equal char ?\ )
5155 (setq a (rassoc char alist))
5156 (and (>= char ?0) (<= char ?9)
5157 (setq n (if (= char ?0) 9 (- char ?0 1))
5158 tag (concat effort-op (nth n efforts))
5159 a (cons tag nil)))
5160 (and (= char ??)
5161 (setq tag "?eff")
5162 a (cons tag nil))
5163 (and tag (setq a (cons tag nil))))
5164 (org-agenda-filter-by-tag-show-all)
5165 (setq tag (car a))
5166 (setq org-agenda-filter
5167 (cons (concat (if strip "-" "+") tag)
5168 (if narrow current nil)))
5169 (org-agenda-filter-apply org-agenda-filter))
5170 (t (error "Invalid tag selection character %c" char)))))
5172 (defun org-agenda-filter-by-tag-refine (strip &optional char)
5173 "Refine the current filter. See `org-agenda-filter-by-tag."
5174 (interactive "P")
5175 (org-agenda-filter-by-tag strip char 'refine))
5177 (defun org-agenda-filter-make-matcher ()
5178 "Create the form that tests a line for the agenda filter."
5179 (let (f f1)
5180 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5181 org-agenda-filter))
5182 (if (member x '("-" "+"))
5183 (setq f1 (if (equal x "-") 'tags '(not tags)))
5184 (if (string-match "[<=>?]" x)
5185 (setq f1 (org-agenda-filter-effort-form x))
5186 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5187 (if (equal (string-to-char x) ?-)
5188 (setq f1 (list 'not f1))))
5189 (push f1 f))
5190 (cons 'and (nreverse f))))
5192 (defun org-agenda-filter-effort-form (e)
5193 "Return the form to compare the effort of the current line with what E says.
5194 E looks line \"+<2:25\"."
5195 (let (op)
5196 (setq e (substring e 1))
5197 (setq op (string-to-char e) e (substring e 1))
5198 (setq op (cond ((equal op ?<) '<=)
5199 ((equal op ?>) '>=)
5200 ((equal op ??) op)
5201 (t '=)))
5202 (list 'org-agenda-compare-effort (list 'quote op)
5203 (org-hh:mm-string-to-minutes e))))
5205 (defun org-agenda-compare-effort (op value)
5206 "Compare the effort of the current line with VALUE, using OP.
5207 If the line does not have an effort defined, return nil."
5208 (let ((eff (org-get-at-bol 'effort-minutes)))
5209 (if (equal op ??)
5210 (not eff)
5211 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5212 value))))
5214 (defun org-agenda-filter-apply (filter)
5215 "Set FILTER as the new agenda filter and apply it."
5216 (let (tags)
5217 (setq org-agenda-filter filter
5218 org-agenda-filter-form (org-agenda-filter-make-matcher))
5219 (org-agenda-set-mode-name)
5220 (save-excursion
5221 (goto-char (point-min))
5222 (while (not (eobp))
5223 (if (org-get-at-bol 'org-marker)
5224 (progn
5225 (setq tags (org-get-at-bol 'tags)) ; used in eval
5226 (if (not (eval org-agenda-filter-form))
5227 (org-agenda-filter-by-tag-hide-line))
5228 (beginning-of-line 2))
5229 (beginning-of-line 2))))))
5231 (defun org-agenda-filter-by-tag-hide-line ()
5232 (let (ov)
5233 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5234 (point-at-eol)))
5235 (org-overlay-put ov 'invisible t)
5236 (org-overlay-put ov 'type 'tags-filter)
5237 (push ov org-agenda-filter-overlays)))
5239 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5240 (setq pos (or pos (point)))
5241 (save-excursion
5242 (dolist (ov (org-overlays-at pos))
5243 (when (and (org-overlay-get ov 'invisible)
5244 (eq (org-overlay-get ov 'type) 'tags-filter))
5245 (goto-char pos)
5246 (if (< (org-overlay-start ov) (point-at-eol))
5247 (org-move-overlay ov (point-at-eol)
5248 (org-overlay-end ov)))))))
5250 (defun org-agenda-filter-by-tag-show-all ()
5251 (mapc 'org-delete-overlay org-agenda-filter-overlays)
5252 (setq org-agenda-filter-overlays nil)
5253 (setq org-agenda-filter nil)
5254 (setq org-agenda-filter-form nil)
5255 (org-agenda-set-mode-name))
5257 (defun org-agenda-manipulate-query-add ()
5258 "Manipulate the query by adding a search term with positive selection.
5259 Positive selection means, the term must be matched for selection of an entry."
5260 (interactive)
5261 (org-agenda-manipulate-query ?\[))
5262 (defun org-agenda-manipulate-query-subtract ()
5263 "Manipulate the query by adding a search term with negative selection.
5264 Negative selection means, term must not be matched for selection of an entry."
5265 (interactive)
5266 (org-agenda-manipulate-query ?\]))
5267 (defun org-agenda-manipulate-query-add-re ()
5268 "Manipulate the query by adding a search regexp with positive selection.
5269 Positive selection means, the regexp must match for selection of an entry."
5270 (interactive)
5271 (org-agenda-manipulate-query ?\{))
5272 (defun org-agenda-manipulate-query-subtract-re ()
5273 "Manipulate the query by adding a search regexp with negative selection.
5274 Negative selection means, regexp must not match for selection of an entry."
5275 (interactive)
5276 (org-agenda-manipulate-query ?\}))
5277 (defun org-agenda-manipulate-query (char)
5278 (cond
5279 ((memq org-agenda-type '(timeline agenda))
5280 (let ((org-agenda-include-inactive-timestamps t))
5281 (org-agenda-redo))
5282 (message "Display now includes inactive timestamps as well"))
5283 ((eq org-agenda-type 'search)
5284 (org-add-to-string
5285 'org-agenda-query-string
5286 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5287 (?\{ . " +{}") (?\} . " -{}")))))
5288 (setq org-agenda-redo-command
5289 (list 'org-search-view
5290 org-todo-only
5291 org-agenda-query-string
5292 (+ (length org-agenda-query-string)
5293 (if (member char '(?\{ ?\})) 0 1))))
5294 (set-register org-agenda-query-register org-agenda-query-string)
5295 (org-agenda-redo))
5296 (t (error "Cannot manipulate query for %s-type agenda buffers"
5297 org-agenda-type))))
5299 (defun org-add-to-string (var string)
5300 (set var (concat (symbol-value var) string)))
5302 (defun org-agenda-goto-date (date)
5303 "Jump to DATE in agenda."
5304 (interactive (list (org-read-date)))
5305 (org-agenda-list nil date))
5307 (defun org-agenda-goto-today ()
5308 "Go to today."
5309 (interactive)
5310 (org-agenda-check-type t 'timeline 'agenda)
5311 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5312 (cond
5313 (tdpos (goto-char tdpos))
5314 ((eq org-agenda-type 'agenda)
5315 (let* ((sd (time-to-days
5316 (time-subtract (current-time)
5317 (list 0 (* 3600 org-extend-today-until) 0))))
5318 (comp (org-agenda-compute-time-span sd org-agenda-span))
5319 (org-agenda-overriding-arguments org-agenda-last-arguments))
5320 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5321 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5322 (org-agenda-redo)
5323 (org-agenda-find-same-or-today-or-agenda)))
5324 (t (error "Cannot find today")))))
5326 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5327 (goto-char
5328 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5329 (text-property-any (point-min) (point-max) 'org-today t)
5330 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5331 (point-min))))
5333 (defun org-agenda-later (arg)
5334 "Go forward in time by thee current span.
5335 With prefix ARG, go forward that many times the current span."
5336 (interactive "p")
5337 (org-agenda-check-type t 'agenda)
5338 (let* ((span org-agenda-span)
5339 (sd org-starting-day)
5340 (greg (calendar-gregorian-from-absolute sd))
5341 (cnt (org-get-at-bol 'org-day-cnt))
5342 greg2 nd)
5343 (cond
5344 ((eq span 'day)
5345 (setq sd (+ arg sd) nd 1))
5346 ((eq span 'week)
5347 (setq sd (+ (* 7 arg) sd) nd 7))
5348 ((eq span 'month)
5349 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5350 sd (calendar-absolute-from-gregorian greg2))
5351 (setcar greg2 (1+ (car greg2)))
5352 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5353 ((eq span 'year)
5354 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5355 sd (calendar-absolute-from-gregorian greg2))
5356 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5357 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5358 (let ((org-agenda-overriding-arguments
5359 (list (car org-agenda-last-arguments) sd nd t)))
5360 (org-agenda-redo)
5361 (org-agenda-find-same-or-today-or-agenda cnt))))
5363 (defun org-agenda-earlier (arg)
5364 "Go backward in time by the current span.
5365 With prefix ARG, go backward that many times the current span."
5366 (interactive "p")
5367 (org-agenda-later (- arg)))
5369 (defun org-agenda-view-mode-dispatch ()
5370 "Call one of the view mode commands."
5371 (interactive)
5372 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5373 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
5374 (let ((a (read-char-exclusive)))
5375 (case a
5376 (?d (call-interactively 'org-agenda-day-view))
5377 (?w (call-interactively 'org-agenda-week-view))
5378 (?m (call-interactively 'org-agenda-month-view))
5379 (?y (call-interactively 'org-agenda-year-view))
5380 (?l (call-interactively 'org-agenda-log-mode))
5381 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
5382 (?a (call-interactively 'org-agenda-archives-mode))
5383 (?A (org-agenda-archives-mode 'files))
5384 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5385 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
5386 (?G (call-interactively 'org-agenda-toggle-time-grid))
5387 (?D (call-interactively 'org-agenda-toggle-diary))
5388 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5389 (org-agenda-check-type t 'timeline 'agenda)
5390 (org-agenda-redo))
5391 (message "Display now includes inactive timestamps as well"))
5392 (?q (message "Abort"))
5393 (otherwise (error "Invalid key" )))))
5395 (defun org-agenda-day-view (&optional day-of-year)
5396 "Switch to daily view for agenda.
5397 With argument DAY-OF-YEAR, switch to that day of the year."
5398 (interactive "P")
5399 (setq org-agenda-ndays 1)
5400 (org-agenda-change-time-span 'day day-of-year))
5401 (defun org-agenda-week-view (&optional iso-week)
5402 "Switch to daily view for agenda.
5403 With argument ISO-WEEK, switch to the corresponding ISO week.
5404 If ISO-WEEK has more then 2 digits, only the last two encode the
5405 week. Any digits before this encode a year. So 200712 means
5406 week 12 of year 2007. Years in the range 1938-2037 can also be
5407 written as 2-digit years."
5408 (interactive "P")
5409 (setq org-agenda-ndays 7)
5410 (org-agenda-change-time-span 'week iso-week))
5411 (defun org-agenda-month-view (&optional month)
5412 "Switch to monthly view for agenda.
5413 With argument MONTH, switch to that month."
5414 (interactive "P")
5415 (org-agenda-change-time-span 'month month))
5416 (defun org-agenda-year-view (&optional year)
5417 "Switch to yearly view for agenda.
5418 With argument YEAR, switch to that year.
5419 If MONTH has more then 2 digits, only the last two encode the
5420 month. Any digits before this encode a year. So 200712 means
5421 December year 2007. Years in the range 1938-2037 can also be
5422 written as 2-digit years."
5423 (interactive "P")
5424 (when year
5425 (setq year (org-small-year-to-year year)))
5426 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5427 (org-agenda-change-time-span 'year year)
5428 (error "Abort")))
5430 (defun org-agenda-change-time-span (span &optional n)
5431 "Change the agenda view to SPAN.
5432 SPAN may be `day', `week', `month', `year'."
5433 (org-agenda-check-type t 'agenda)
5434 (if (and (not n) (equal org-agenda-span span))
5435 (error "Viewing span is already \"%s\"" span))
5436 (let* ((sd (or (org-get-at-bol 'day)
5437 org-starting-day))
5438 (computed (org-agenda-compute-time-span sd span n))
5439 (org-agenda-overriding-arguments
5440 (list (car org-agenda-last-arguments)
5441 (car computed) (cdr computed) t)))
5442 (org-agenda-redo)
5443 (org-agenda-find-same-or-today-or-agenda))
5444 (org-agenda-set-mode-name)
5445 (message "Switched to %s view" span))
5447 (defun org-agenda-compute-time-span (sd span &optional n)
5448 "Compute starting date and number of days for agenda.
5449 SPAN may be `day', `week', `month', `year'. The return value
5450 is a cons cell with the starting date and the number of days,
5451 so that the date SD will be in that range."
5452 (let* ((greg (calendar-gregorian-from-absolute sd))
5453 (dg (nth 1 greg))
5454 (mg (car greg))
5455 (yg (nth 2 greg))
5456 nd w1 y1 m1 thisweek)
5457 (cond
5458 ((eq span 'day)
5459 (when n
5460 (setq sd (+ (calendar-absolute-from-gregorian
5461 (list mg 1 yg))
5462 n -1)))
5463 (setq nd 1))
5464 ((eq span 'week)
5465 (let* ((nt (calendar-day-of-week
5466 (calendar-gregorian-from-absolute sd)))
5467 (d (if org-agenda-start-on-weekday
5468 (- nt org-agenda-start-on-weekday)
5469 0)))
5470 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5471 (when n
5472 (require 'cal-iso)
5473 (setq thisweek (car (calendar-iso-from-absolute sd)))
5474 (when (> n 99)
5475 (setq y1 (org-small-year-to-year (/ n 100))
5476 n (mod n 100)))
5477 (setq sd
5478 (calendar-absolute-from-iso
5479 (list n 1
5480 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5481 (setq nd 7)))
5482 ((eq span 'month)
5483 (when (and n (> n 99))
5484 (setq y1 (org-small-year-to-year (/ n 100))
5485 n (mod n 100)))
5486 (setq sd (calendar-absolute-from-gregorian
5487 (list (or n mg) 1 (or y1 yg)))
5488 nd (- (calendar-absolute-from-gregorian
5489 (list (1+ (or n mg)) 1 (or y1 yg)))
5490 sd)))
5491 ((eq span 'year)
5492 (setq sd (calendar-absolute-from-gregorian
5493 (list 1 1 (or n yg)))
5494 nd (- (calendar-absolute-from-gregorian
5495 (list 1 1 (1+ (or n yg))))
5496 sd))))
5497 (cons sd nd)))
5499 (defun org-agenda-next-date-line (&optional arg)
5500 "Jump to the next line indicating a date in agenda buffer."
5501 (interactive "p")
5502 (org-agenda-check-type t 'agenda 'timeline)
5503 (beginning-of-line 1)
5504 ;; This does not work if user makes date format that starts with a blank
5505 (if (looking-at "^\\S-") (forward-char 1))
5506 (if (not (re-search-forward "^\\S-" nil t arg))
5507 (progn
5508 (backward-char 1)
5509 (error "No next date after this line in this buffer")))
5510 (goto-char (match-beginning 0)))
5512 (defun org-agenda-previous-date-line (&optional arg)
5513 "Jump to the previous line indicating a date in agenda buffer."
5514 (interactive "p")
5515 (org-agenda-check-type t 'agenda 'timeline)
5516 (beginning-of-line 1)
5517 (if (not (re-search-backward "^\\S-" nil t arg))
5518 (error "No previous date before this line in this buffer")))
5520 ;; Initialize the highlight
5521 (defvar org-hl (org-make-overlay 1 1))
5522 (org-overlay-put org-hl 'face 'highlight)
5524 (defun org-highlight (begin end &optional buffer)
5525 "Highlight a region with overlay."
5526 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5527 org-hl begin end (or buffer (current-buffer))))
5529 (defun org-unhighlight ()
5530 "Detach overlay INDEX."
5531 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5533 ;; FIXME this is currently not used.
5534 (defun org-highlight-until-next-command (beg end &optional buffer)
5535 "Move the highlight overlay to BEG/END, remove it before the next command."
5536 (org-highlight beg end buffer)
5537 (add-hook 'pre-command-hook 'org-unhighlight-once))
5538 (defun org-unhighlight-once ()
5539 "Remove the highlight from its position, and this function from the hook."
5540 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5541 (org-unhighlight))
5543 (defun org-agenda-follow-mode ()
5544 "Toggle follow mode in an agenda buffer."
5545 (interactive)
5546 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5547 (org-agenda-set-mode-name)
5548 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5549 (org-agenda-show))
5550 (message "Follow mode is %s"
5551 (if org-agenda-follow-mode "on" "off")))
5553 (defun org-agenda-entry-text-mode (&optional arg)
5554 "Toggle entry text mode in an agenda buffer."
5555 (interactive "P")
5556 (if (integerp arg)
5557 (setq org-agenda-entry-text-mode t)
5558 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5559 (org-agenda-entry-text-hide)
5560 (and org-agenda-entry-text-mode
5561 (let ((org-agenda-entry-text-maxlines
5562 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5563 (org-agenda-entry-text-show)))
5564 (org-agenda-set-mode-name)
5565 (message "Entry text mode is %s. Maximum number of lines is %d"
5566 (if org-agenda-entry-text-mode "on" "off")
5567 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5569 (defun org-agenda-clockreport-mode ()
5570 "Toggle clocktable mode in an agenda buffer."
5571 (interactive)
5572 (org-agenda-check-type t 'agenda)
5573 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5574 (org-agenda-set-mode-name)
5575 (org-agenda-redo)
5576 (message "Clocktable mode is %s"
5577 (if org-agenda-clockreport-mode "on" "off")))
5579 (defun org-agenda-log-mode (&optional special)
5580 "Toggle log mode in an agenda buffer.
5581 With argument SPECIAL, show all possible log items, not only the ones
5582 configured in `org-agenda-log-mode-items'.
5583 With a double `C-u' prefix arg, show *only* log items, nothing else."
5584 (interactive "P")
5585 (org-agenda-check-type t 'agenda 'timeline)
5586 (setq org-agenda-show-log
5587 (if (equal special '(16))
5588 'only
5589 (if special '(closed clock state)
5590 (not org-agenda-show-log))))
5591 (org-agenda-set-mode-name)
5592 (org-agenda-redo)
5593 (message "Log mode is %s"
5594 (if org-agenda-show-log "on" "off")))
5596 (defun org-agenda-archives-mode (&optional with-files)
5597 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5598 When called with a prefix argument, include all archive files as well."
5599 (interactive "P")
5600 (setq org-agenda-archives-mode
5601 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5602 (org-agenda-set-mode-name)
5603 (org-agenda-redo)
5604 (message
5605 "%s"
5606 (cond
5607 ((eq org-agenda-archives-mode nil)
5608 "No archives are included")
5609 ((eq org-agenda-archives-mode 'trees)
5610 (format "Trees with :%s: tag are included" org-archive-tag))
5611 ((eq org-agenda-archives-mode t)
5612 (format "Trees with :%s: tag and all active archive files are included"
5613 org-archive-tag)))))
5615 (defun org-agenda-toggle-diary ()
5616 "Toggle diary inclusion in an agenda buffer."
5617 (interactive)
5618 (org-agenda-check-type t 'agenda)
5619 (setq org-agenda-include-diary (not org-agenda-include-diary))
5620 (org-agenda-redo)
5621 (org-agenda-set-mode-name)
5622 (message "Diary inclusion turned %s"
5623 (if org-agenda-include-diary "on" "off")))
5625 (defun org-agenda-toggle-time-grid ()
5626 "Toggle time grid in an agenda buffer."
5627 (interactive)
5628 (org-agenda-check-type t 'agenda)
5629 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5630 (org-agenda-redo)
5631 (org-agenda-set-mode-name)
5632 (message "Time-grid turned %s"
5633 (if org-agenda-use-time-grid "on" "off")))
5635 (defun org-agenda-set-mode-name ()
5636 "Set the mode name to indicate all the small mode settings."
5637 (setq mode-name
5638 (concat "Org-Agenda"
5639 (if (equal org-agenda-ndays 1) " Day" "")
5640 (if (equal org-agenda-ndays 7) " Week" "")
5641 (if org-agenda-follow-mode " Follow" "")
5642 (if org-agenda-entry-text-mode " ETxt" "")
5643 (if org-agenda-include-diary " Diary" "")
5644 (if org-agenda-use-time-grid " Grid" "")
5645 (if (and (boundp 'org-habit-show-habits)
5646 org-habit-show-habits) " Habit" "")
5647 (if (consp org-agenda-show-log) " LogAll"
5648 (if org-agenda-show-log " Log" ""))
5649 (if (or org-agenda-filter (get 'org-agenda-filter
5650 :preset-filter))
5651 (concat " {" (mapconcat
5652 'identity
5653 (append (get 'org-agenda-filter
5654 :preset-filter)
5655 org-agenda-filter) "") "}")
5657 (if org-agenda-archives-mode
5658 (if (eq org-agenda-archives-mode t)
5659 " Archives"
5660 (format " :%s:" org-archive-tag))
5662 (if org-agenda-clockreport-mode " Clock" "")))
5663 (force-mode-line-update))
5665 (defun org-agenda-post-command-hook ()
5666 (setq org-agenda-type
5667 (or (get-text-property (point) 'org-agenda-type)
5668 (get-text-property (max (point-min) (1- (point)))
5669 'org-agenda-type))))
5671 (defun org-agenda-next-line ()
5672 "Move cursor to the next line, and show if follow-mode is active."
5673 (interactive)
5674 (call-interactively 'next-line)
5675 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5676 (org-agenda-show)))
5677 (defun org-agenda-previous-line ()
5678 "Move cursor to the previous line, and show if follow-mode is active."
5680 (interactive)
5681 (call-interactively 'previous-line)
5682 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5683 (org-agenda-show)))
5685 (defun org-agenda-show-priority ()
5686 "Show the priority of the current item.
5687 This priority is composed of the main priority given with the [#A] cookies,
5688 and by additional input from the age of a schedules or deadline entry."
5689 (interactive)
5690 (let* ((pri (org-get-at-bol 'priority)))
5691 (message "Priority is %d" (if pri pri -1000))))
5693 (defun org-agenda-show-tags ()
5694 "Show the tags applicable to the current item."
5695 (interactive)
5696 (let* ((tags (org-get-at-bol 'tags)))
5697 (if tags
5698 (message "Tags are :%s:"
5699 (org-no-properties (mapconcat 'identity tags ":")))
5700 (message "No tags associated with this line"))))
5702 (defun org-agenda-goto (&optional highlight)
5703 "Go to the Org-mode file which contains the item at point."
5704 (interactive)
5705 (let* ((marker (or (org-get-at-bol 'org-marker)
5706 (org-agenda-error)))
5707 (buffer (marker-buffer marker))
5708 (pos (marker-position marker)))
5709 (switch-to-buffer-other-window buffer)
5710 (widen)
5711 (goto-char pos)
5712 (when (org-mode-p)
5713 (org-show-context 'agenda)
5714 (save-excursion
5715 (and (outline-next-heading)
5716 (org-flag-heading nil)))) ; show the next heading
5717 (recenter (/ (window-height) 2))
5718 (run-hooks 'org-agenda-after-show-hook)
5719 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5721 (defvar org-agenda-after-show-hook nil
5722 "Normal hook run after an item has been shown from the agenda.
5723 Point is in the buffer where the item originated.")
5725 (defun org-agenda-kill ()
5726 "Kill the entry or subtree belonging to the current agenda entry."
5727 (interactive)
5728 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5729 (let* ((marker (or (org-get-at-bol 'org-marker)
5730 (org-agenda-error)))
5731 (buffer (marker-buffer marker))
5732 (pos (marker-position marker))
5733 (type (org-get-at-bol 'type))
5734 dbeg dend (n 0) conf)
5735 (org-with-remote-undo buffer
5736 (with-current-buffer buffer
5737 (save-excursion
5738 (goto-char pos)
5739 (if (and (org-mode-p) (not (member type '("sexp"))))
5740 (setq dbeg (progn (org-back-to-heading t) (point))
5741 dend (org-end-of-subtree t t))
5742 (setq dbeg (point-at-bol)
5743 dend (min (point-max) (1+ (point-at-eol)))))
5744 (goto-char dbeg)
5745 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5746 (setq conf (or (eq t org-agenda-confirm-kill)
5747 (and (numberp org-agenda-confirm-kill)
5748 (> n org-agenda-confirm-kill))))
5749 (and conf
5750 (not (y-or-n-p
5751 (format "Delete entry with %d lines in buffer \"%s\"? "
5752 n (buffer-name buffer))))
5753 (error "Abort"))
5754 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5755 (with-current-buffer buffer (delete-region dbeg dend))
5756 (message "Agenda item and source killed"))))
5758 (defvar org-archive-default-command)
5759 (defun org-agenda-archive-default ()
5760 "Archive the entry or subtree belonging to the current agenda entry."
5761 (interactive)
5762 (require 'org-archive)
5763 (org-agenda-archive-with org-archive-default-command))
5765 (defun org-agenda-archive-default-with-confirmation ()
5766 "Archive the entry or subtree belonging to the current agenda entry."
5767 (interactive)
5768 (require 'org-archive)
5769 (org-agenda-archive-with org-archive-default-command 'confirm))
5771 (defun org-agenda-archive ()
5772 "Archive the entry or subtree belonging to the current agenda entry."
5773 (interactive)
5774 (org-agenda-archive-with 'org-archive-subtree))
5776 (defun org-agenda-archive-to-archive-sibling ()
5777 "Move the entry to the archive sibling."
5778 (interactive)
5779 (org-agenda-archive-with 'org-archive-to-archive-sibling))
5781 (defun org-agenda-archive-with (cmd &optional confirm)
5782 "Move the entry to the archive sibling."
5783 (interactive)
5784 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5785 (let* ((marker (or (org-get-at-bol 'org-marker)
5786 (org-agenda-error)))
5787 (buffer (marker-buffer marker))
5788 (pos (marker-position marker)))
5789 (org-with-remote-undo buffer
5790 (with-current-buffer buffer
5791 (if (org-mode-p)
5792 (if (and confirm
5793 (not (y-or-n-p "Archive this subtree or entry? ")))
5794 (error "Abort")
5795 (save-excursion
5796 (goto-char pos)
5797 (org-remove-subtree-entries-from-agenda)
5798 (org-back-to-heading t)
5799 (funcall cmd)))
5800 (error "Archiving works only in Org-mode files"))))))
5802 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5803 "Remove all lines in the agenda that correspond to a given subtree.
5804 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5805 If this information is not given, the function uses the tree at point."
5806 (let ((buf (or buf (current-buffer))) m p)
5807 (save-excursion
5808 (unless (and beg end)
5809 (org-back-to-heading t)
5810 (setq beg (point))
5811 (org-end-of-subtree t)
5812 (setq end (point)))
5813 (set-buffer (get-buffer org-agenda-buffer-name))
5814 (save-excursion
5815 (goto-char (point-max))
5816 (beginning-of-line 1)
5817 (while (not (bobp))
5818 (when (and (setq m (org-get-at-bol 'org-marker))
5819 (equal buf (marker-buffer m))
5820 (setq p (marker-position m))
5821 (>= p beg)
5822 (< p end))
5823 (let ((inhibit-read-only t))
5824 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5825 (beginning-of-line 0))))))
5827 (defun org-agenda-refile (&optional goto rfloc)
5828 "Refile the item at point."
5829 (interactive "P")
5830 (if (equal goto '(16))
5831 (org-refile-goto-last-stored)
5832 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5833 (org-agenda-error)))
5834 (buffer (marker-buffer marker))
5835 (pos (marker-position marker))
5836 (rfloc (or rfloc
5837 (org-refile-get-location
5838 (if goto "Goto: " "Refile to: ") buffer
5839 org-refile-allow-creating-parent-nodes))))
5840 (with-current-buffer buffer
5841 (save-excursion
5842 (save-restriction
5843 (widen)
5844 (goto-char marker)
5845 (org-remove-subtree-entries-from-agenda)
5846 (org-refile goto buffer rfloc)))))))
5848 (defun org-agenda-open-link (&optional arg)
5849 "Follow the link in the current line, if any.
5850 This looks for a link in the displayed line in the agenda. It also looks
5851 at the text of the entry itself."
5852 (interactive "P")
5853 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5854 (org-get-at-bol 'org-marker)))
5855 (buffer (and marker (marker-buffer marker)))
5856 (prefix (buffer-substring
5857 (point-at-bol)
5858 (+ (point-at-bol)
5859 (or (org-get-at-bol 'prefix-length) 0)))))
5860 (cond
5861 (buffer
5862 (with-current-buffer buffer
5863 (save-excursion
5864 (save-restriction
5865 (widen)
5866 (goto-char marker)
5867 (org-offer-links-in-entry arg prefix)))))
5868 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
5869 (save-excursion
5870 (beginning-of-line 1)
5871 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
5872 (org-open-link-from-string (match-string 1)))
5873 (t (error "No link to open here")))))
5875 (defun org-agenda-copy-local-variable (var)
5876 "Get a variable from a referenced buffer and install it here."
5877 (let ((m (org-get-at-bol 'org-marker)))
5878 (when (and m (buffer-live-p (marker-buffer m)))
5879 (org-set-local var (with-current-buffer (marker-buffer m)
5880 (symbol-value var))))))
5882 (defun org-agenda-switch-to (&optional delete-other-windows)
5883 "Go to the Org-mode file which contains the item at point."
5884 (interactive)
5885 (if (and org-return-follows-link
5886 (not (org-get-at-bol 'org-marker))
5887 (org-in-regexp org-bracket-link-regexp))
5888 (org-open-link-from-string (match-string 0))
5889 (let* ((marker (or (org-get-at-bol 'org-marker)
5890 (org-agenda-error)))
5891 (buffer (marker-buffer marker))
5892 (pos (marker-position marker)))
5893 (switch-to-buffer buffer)
5894 (and delete-other-windows (delete-other-windows))
5895 (widen)
5896 (goto-char pos)
5897 (when (org-mode-p)
5898 (org-show-context 'agenda)
5899 (save-excursion
5900 (and (outline-next-heading)
5901 (org-flag-heading nil))))))) ; show the next heading
5903 (defun org-agenda-goto-mouse (ev)
5904 "Go to the Org-mode file which contains the item at the mouse click."
5905 (interactive "e")
5906 (mouse-set-point ev)
5907 (org-agenda-goto))
5909 (defun org-agenda-show (&optional full-entry)
5910 "Display the Org-mode file which contains the item at point.
5911 With prefix argument FULL-ENTRY, make the entire entry visible
5912 if it was hidden in the outline."
5913 (interactive "P")
5914 (let ((win (selected-window)))
5915 (if full-entry
5916 (let ((org-show-entry-below t))
5917 (org-agenda-goto t))
5918 (org-agenda-goto t))
5919 (select-window win)))
5921 (defvar org-agenda-show-window nil)
5922 (defun org-agenda-show-and-scroll-up ()
5923 "Display the Org-mode file which contains the item at point.
5924 When called repeatedly, scroll the window that is displaying the buffer."
5925 (interactive)
5926 (let ((win (selected-window)))
5927 (if (and (window-live-p org-agenda-show-window)
5928 (eq this-command last-command))
5929 (progn
5930 (select-window org-agenda-show-window)
5931 (ignore-errors (scroll-up)))
5932 (org-agenda-goto t)
5933 (show-subtree)
5934 (setq org-agenda-show-window (selected-window)))
5935 (select-window win)))
5937 (defun org-agenda-show-scroll-down ()
5938 "Scroll down the window showing the agenda."
5939 (interactive)
5940 (let ((win (selected-window)))
5941 (when (window-live-p org-agenda-show-window)
5942 (select-window org-agenda-show-window)
5943 (ignore-errors (scroll-down))
5944 (select-window win))))
5946 (defun org-agenda-show-1 (&optional more)
5947 "Display the Org-mode file which contains the item at point.
5948 The prefix arg causes further revieling:
5950 0 hide the subtree
5951 1 just show the entry according to defaults.
5952 2 show the children view
5953 3 show the subtree view
5954 4 show the entire subtree and any LOGBOOK drawers
5955 5 show the entire subtree and any drawers
5956 With prefix argument FULL-ENTRY, make the entire entry visible
5957 if it was hidden in the outline."
5958 (interactive "p")
5959 (let ((win (selected-window)))
5960 (org-agenda-goto t)
5961 (org-recenter-heading 1)
5962 (cond
5963 ((= more 0)
5964 (hide-subtree)
5965 (save-excursion
5966 (org-back-to-heading)
5967 (run-hook-with-args 'org-cycle-hook 'folded))
5968 (message "Remote: FOLDED"))
5969 ((and (interactive-p) (= more 1))
5970 (message "Remote: show with default settings"))
5971 ((= more 2)
5972 (show-entry)
5973 (show-children)
5974 (save-excursion
5975 (org-back-to-heading)
5976 (run-hook-with-args 'org-cycle-hook 'children))
5977 (message "Remote: CHILDREN"))
5978 ((= more 3)
5979 (show-subtree)
5980 (save-excursion
5981 (org-back-to-heading)
5982 (run-hook-with-args 'org-cycle-hook 'subtree))
5983 (message "Remote: SUBTREE"))
5984 ((= more 4)
5985 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5986 (org-drawer-regexp
5987 (concat "^[ \t]*:\\("
5988 (mapconcat 'regexp-quote org-drawers "\\|")
5989 "\\):[ \t]*$")))
5990 (show-subtree)
5991 (save-excursion
5992 (org-back-to-heading)
5993 (org-cycle-hide-drawers 'subtree)))
5994 (message "Remote: SUBTREE AND LOGBOOK"))
5995 ((> more 4)
5996 (show-subtree)
5997 (message "Remote: SUBTREE AND ALL DRAWERS")))
5998 (select-window win)))
6000 (defun org-recenter-heading (n)
6001 (save-excursion
6002 (org-back-to-heading)
6003 (recenter n)))
6005 (defvar org-agenda-cycle-counter nil)
6006 (defun org-agenda-cycle-show (&optional n)
6007 "Show the current entry in another window, with default settings.
6008 Default settings are taken from `org-show-hierarchy-above' and siblings.
6009 When use repeatedly in immediate succession, the remote entry will cycle
6010 through visibility
6012 children -> subtree -> folded
6014 When called with a numeric prefix arg, that arg will be passed through to
6015 `org-agenda-show-1'. For the interpretation of that argument, see the
6016 docstring of `org-agenda-show-1'."
6017 (interactive "P")
6018 (if (integerp n)
6019 (setq org-agenda-cycle-counter n)
6020 (if (not (eq last-command this-command))
6021 (setq org-agenda-cycle-counter 1)
6022 (if (equal org-agenda-cycle-counter 0)
6023 (setq org-agenda-cycle-counter 2)
6024 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6025 (if (> org-agenda-cycle-counter 3)
6026 (setq org-agenda-cycle-counter 0)))))
6027 (org-agenda-show-1 org-agenda-cycle-counter))
6029 (defun org-agenda-recenter (arg)
6030 "Display the Org-mode file which contains the item at point and recenter."
6031 (interactive "P")
6032 (let ((win (selected-window)))
6033 (org-agenda-goto t)
6034 (recenter arg)
6035 (select-window win)))
6037 (defun org-agenda-show-mouse (ev)
6038 "Display the Org-mode file which contains the item at the mouse click."
6039 (interactive "e")
6040 (mouse-set-point ev)
6041 (org-agenda-show))
6043 (defun org-agenda-check-no-diary ()
6044 "Check if the entry is a diary link and abort if yes."
6045 (if (org-get-at-bol 'org-agenda-diary-link)
6046 (org-agenda-error)))
6048 (defun org-agenda-error ()
6049 (error "Command not allowed in this line"))
6051 (defun org-agenda-tree-to-indirect-buffer ()
6052 "Show the subtree corresponding to the current entry in an indirect buffer.
6053 This calls the command `org-tree-to-indirect-buffer' from the original
6054 Org-mode buffer.
6055 With numerical prefix arg ARG, go up to this level and then take that tree.
6056 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
6057 dedicated frame)."
6058 (interactive)
6059 (org-agenda-check-no-diary)
6060 (let* ((marker (or (org-get-at-bol 'org-marker)
6061 (org-agenda-error)))
6062 (buffer (marker-buffer marker))
6063 (pos (marker-position marker)))
6064 (with-current-buffer buffer
6065 (save-excursion
6066 (goto-char pos)
6067 (call-interactively 'org-tree-to-indirect-buffer)))))
6069 (defvar org-last-heading-marker (make-marker)
6070 "Marker pointing to the headline that last changed its TODO state
6071 by a remote command from the agenda.")
6073 (defun org-agenda-todo-nextset ()
6074 "Switch TODO entry to next sequence."
6075 (interactive)
6076 (org-agenda-todo 'nextset))
6078 (defun org-agenda-todo-previousset ()
6079 "Switch TODO entry to previous sequence."
6080 (interactive)
6081 (org-agenda-todo 'previousset))
6083 (defun org-agenda-todo (&optional arg)
6084 "Cycle TODO state of line at point, also in Org-mode file.
6085 This changes the line at point, all other lines in the agenda referring to
6086 the same tree node, and the headline of the tree node in the Org-mode file."
6087 (interactive "P")
6088 (org-agenda-check-no-diary)
6089 (let* ((col (current-column))
6090 (marker (or (org-get-at-bol 'org-marker)
6091 (org-agenda-error)))
6092 (buffer (marker-buffer marker))
6093 (pos (marker-position marker))
6094 (hdmarker (org-get-at-bol 'org-hd-marker))
6095 (todayp (equal (org-get-at-bol 'day)
6096 (time-to-days (current-time))))
6097 (inhibit-read-only t)
6098 org-agenda-headline-snapshot-before-repeat newhead just-one)
6099 (org-with-remote-undo buffer
6100 (with-current-buffer buffer
6101 (widen)
6102 (goto-char pos)
6103 (org-show-context 'agenda)
6104 (save-excursion
6105 (and (outline-next-heading)
6106 (org-flag-heading nil))) ; show the next heading
6107 (let ((current-prefix-arg arg))
6108 (call-interactively 'org-todo))
6109 (and (bolp) (forward-char 1))
6110 (setq newhead (org-get-heading))
6111 (when (and (org-bound-and-true-p
6112 org-agenda-headline-snapshot-before-repeat)
6113 (not (equal org-agenda-headline-snapshot-before-repeat
6114 newhead))
6115 todayp)
6116 (setq newhead org-agenda-headline-snapshot-before-repeat
6117 just-one t))
6118 (save-excursion
6119 (org-back-to-heading)
6120 (move-marker org-last-heading-marker (point))))
6121 (beginning-of-line 1)
6122 (save-excursion
6123 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
6124 (org-move-to-column col))))
6126 (defun org-agenda-add-note (&optional arg)
6127 "Add a time-stamped note to the entry at point."
6128 (interactive "P")
6129 (org-agenda-check-no-diary)
6130 (let* ((marker (or (org-get-at-bol 'org-marker)
6131 (org-agenda-error)))
6132 (buffer (marker-buffer marker))
6133 (pos (marker-position marker))
6134 (hdmarker (org-get-at-bol 'org-hd-marker))
6135 (inhibit-read-only t))
6136 (with-current-buffer buffer
6137 (widen)
6138 (goto-char pos)
6139 (org-show-context 'agenda)
6140 (save-excursion
6141 (and (outline-next-heading)
6142 (org-flag-heading nil))) ; show the next heading
6143 (org-add-note))))
6145 (defun org-agenda-change-all-lines (newhead hdmarker
6146 &optional fixface just-this)
6147 "Change all lines in the agenda buffer which match HDMARKER.
6148 The new content of the line will be NEWHEAD (as modified by
6149 `org-format-agenda-item'). HDMARKER is checked with
6150 `equal' against all `org-hd-marker' text properties in the file.
6151 If FIXFACE is non-nil, the face of each item is modified according to
6152 the new TODO state.
6153 If JUST-THIS is non-nil, change just the current line, not all.
6154 If FORCE-TAGS is non nil, the car of it returns the new tags."
6155 (let* ((inhibit-read-only t)
6156 (line (org-current-line))
6157 (thetags (with-current-buffer (marker-buffer hdmarker)
6158 (save-excursion (save-restriction (widen)
6159 (goto-char hdmarker)
6160 (org-get-tags-at)))))
6161 props m pl undone-face done-face finish new dotime cat tags)
6162 (save-excursion
6163 (goto-char (point-max))
6164 (beginning-of-line 1)
6165 (while (not finish)
6166 (setq finish (bobp))
6167 (when (and (setq m (org-get-at-bol 'org-hd-marker))
6168 (or (not just-this) (= (org-current-line) line))
6169 (equal m hdmarker))
6170 (setq props (text-properties-at (point))
6171 dotime (org-get-at-bol 'dotime)
6172 cat (org-get-at-bol 'org-category)
6173 tags thetags
6174 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6175 pl (org-get-at-bol 'prefix-length)
6176 undone-face (org-get-at-bol 'undone-face)
6177 done-face (org-get-at-bol 'done-face))
6178 (goto-char (+ (point) pl))
6179 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
6180 (cond
6181 ((equal new "")
6182 (beginning-of-line 1)
6183 (and (looking-at ".*\n?") (replace-match "")))
6184 ((looking-at ".*")
6185 (replace-match new t t)
6186 (beginning-of-line 1)
6187 (add-text-properties (point-at-bol) (point-at-eol) props)
6188 (when fixface
6189 (add-text-properties
6190 (point-at-bol) (point-at-eol)
6191 (list 'face
6192 (if org-last-todo-state-is-todo
6193 undone-face done-face))))
6194 (org-agenda-highlight-todo 'line)
6195 (beginning-of-line 1))
6196 (t (error "Line update did not work"))))
6197 (beginning-of-line 0)))
6198 (org-finalize-agenda)))
6200 (defun org-agenda-align-tags (&optional line)
6201 "Align all tags in agenda items to `org-agenda-tags-column'."
6202 (let ((inhibit-read-only t) l c)
6203 (save-excursion
6204 (goto-char (if line (point-at-bol) (point-min)))
6205 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6206 (if line (point-at-eol) nil) t)
6207 (add-text-properties
6208 (match-beginning 2) (match-end 2)
6209 (list 'face (delq nil (let ((prop (get-text-property
6210 (match-beginning 2) 'face)))
6211 (or (listp prop) (setq prop (list prop)))
6212 (if (memq 'org-tag prop)
6213 prop
6214 (cons 'org-tag prop))))))
6215 (setq l (- (match-end 2) (match-beginning 2))
6216 c (if (< org-agenda-tags-column 0)
6217 (- (abs org-agenda-tags-column) l)
6218 org-agenda-tags-column))
6219 (delete-region (match-beginning 1) (match-end 1))
6220 (goto-char (match-beginning 1))
6221 (insert (org-add-props
6222 (make-string (max 1 (- c (current-column))) ?\ )
6223 (text-properties-at (point)))))
6224 (goto-char (point-min))
6225 (org-font-lock-add-tag-faces (point-max)))))
6227 (defun org-agenda-priority-up ()
6228 "Increase the priority of line at point, also in Org-mode file."
6229 (interactive)
6230 (org-agenda-priority 'up))
6232 (defun org-agenda-priority-down ()
6233 "Decrease the priority of line at point, also in Org-mode file."
6234 (interactive)
6235 (org-agenda-priority 'down))
6237 (defun org-agenda-priority (&optional force-direction)
6238 "Set the priority of line at point, also in Org-mode file.
6239 This changes the line at point, all other lines in the agenda referring to
6240 the same tree node, and the headline of the tree node in the Org-mode file."
6241 (interactive)
6242 (unless org-enable-priority-commands
6243 (error "Priority commands are disabled"))
6244 (org-agenda-check-no-diary)
6245 (let* ((marker (or (org-get-at-bol 'org-marker)
6246 (org-agenda-error)))
6247 (hdmarker (org-get-at-bol 'org-hd-marker))
6248 (buffer (marker-buffer hdmarker))
6249 (pos (marker-position hdmarker))
6250 (inhibit-read-only t)
6251 newhead)
6252 (org-with-remote-undo buffer
6253 (with-current-buffer buffer
6254 (widen)
6255 (goto-char pos)
6256 (org-show-context 'agenda)
6257 (save-excursion
6258 (and (outline-next-heading)
6259 (org-flag-heading nil))) ; show the next heading
6260 (funcall 'org-priority force-direction)
6261 (end-of-line 1)
6262 (setq newhead (org-get-heading)))
6263 (org-agenda-change-all-lines newhead hdmarker)
6264 (beginning-of-line 1))))
6266 ;; FIXME: should fix the tags property of the agenda line.
6267 (defun org-agenda-set-tags (&optional tag onoff)
6268 "Set tags for the current headline."
6269 (interactive)
6270 (org-agenda-check-no-diary)
6271 (if (and (org-region-active-p) (interactive-p))
6272 (call-interactively 'org-change-tag-in-region)
6273 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6274 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6275 (org-agenda-error)))
6276 (buffer (marker-buffer hdmarker))
6277 (pos (marker-position hdmarker))
6278 (inhibit-read-only t)
6279 newhead)
6280 (org-with-remote-undo buffer
6281 (with-current-buffer buffer
6282 (widen)
6283 (goto-char pos)
6284 (save-excursion
6285 (org-show-context 'agenda))
6286 (save-excursion
6287 (and (outline-next-heading)
6288 (org-flag-heading nil))) ; show the next heading
6289 (goto-char pos)
6290 (if tag
6291 (org-toggle-tag tag onoff)
6292 (call-interactively 'org-set-tags))
6293 (end-of-line 1)
6294 (setq newhead (org-get-heading)))
6295 (org-agenda-change-all-lines newhead hdmarker)
6296 (beginning-of-line 1)))))
6298 (defun org-agenda-set-property ()
6299 "Set a property for the current headline."
6300 (interactive)
6301 (org-agenda-check-no-diary)
6302 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6303 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6304 (org-agenda-error)))
6305 (buffer (marker-buffer hdmarker))
6306 (pos (marker-position hdmarker))
6307 (inhibit-read-only t)
6308 newhead)
6309 (org-with-remote-undo buffer
6310 (with-current-buffer buffer
6311 (widen)
6312 (goto-char pos)
6313 (save-excursion
6314 (org-show-context 'agenda))
6315 (save-excursion
6316 (and (outline-next-heading)
6317 (org-flag-heading nil))) ; show the next heading
6318 (goto-char pos)
6319 (call-interactively 'org-set-property)))))
6321 (defun org-agenda-set-effort ()
6322 "Set the effort property for the current headline."
6323 (interactive)
6324 (org-agenda-check-no-diary)
6325 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6326 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6327 (org-agenda-error)))
6328 (buffer (marker-buffer hdmarker))
6329 (pos (marker-position hdmarker))
6330 (inhibit-read-only t)
6331 newhead)
6332 (org-with-remote-undo buffer
6333 (with-current-buffer buffer
6334 (widen)
6335 (goto-char pos)
6336 (save-excursion
6337 (org-show-context 'agenda))
6338 (save-excursion
6339 (and (outline-next-heading)
6340 (org-flag-heading nil))) ; show the next heading
6341 (goto-char pos)
6342 (call-interactively 'org-set-effort)
6343 (end-of-line 1)))))
6345 (defun org-agenda-toggle-archive-tag ()
6346 "Toggle the archive tag for the current entry."
6347 (interactive)
6348 (org-agenda-check-no-diary)
6349 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6350 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
6351 (org-agenda-error)))
6352 (buffer (marker-buffer hdmarker))
6353 (pos (marker-position hdmarker))
6354 (inhibit-read-only t)
6355 newhead)
6356 (org-with-remote-undo buffer
6357 (with-current-buffer buffer
6358 (widen)
6359 (goto-char pos)
6360 (org-show-context 'agenda)
6361 (save-excursion
6362 (and (outline-next-heading)
6363 (org-flag-heading nil))) ; show the next heading
6364 (call-interactively 'org-toggle-archive-tag)
6365 (end-of-line 1)
6366 (setq newhead (org-get-heading)))
6367 (org-agenda-change-all-lines newhead hdmarker)
6368 (beginning-of-line 1))))
6370 (defun org-agenda-do-date-later (arg)
6371 (interactive "P")
6372 (cond
6373 ((or (equal arg '(16))
6374 (memq last-command
6375 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6376 (setq this-command 'org-agenda-date-later-minutes)
6377 (org-agenda-date-later-minutes 1))
6378 ((or (equal arg '(4))
6379 (memq last-command
6380 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6381 (setq this-command 'org-agenda-date-later-hours)
6382 (org-agenda-date-later-hours 1))
6384 (org-agenda-date-later (prefix-numeric-value arg)))))
6386 (defun org-agenda-do-date-earlier (arg)
6387 (interactive "P")
6388 (cond
6389 ((or (equal arg '(16))
6390 (memq last-command
6391 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6392 (setq this-command 'org-agenda-date-earlier-minutes)
6393 (org-agenda-date-earlier-minutes 1))
6394 ((or (equal arg '(4))
6395 (memq last-command
6396 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6397 (setq this-command 'org-agenda-date-earlier-hours)
6398 (org-agenda-date-earlier-hours 1))
6400 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6402 (defun org-agenda-date-later (arg &optional what)
6403 "Change the date of this item to one day later."
6404 (interactive "p")
6405 (org-agenda-check-type t 'agenda 'timeline)
6406 (org-agenda-check-no-diary)
6407 (let* ((marker (or (org-get-at-bol 'org-marker)
6408 (org-agenda-error)))
6409 (buffer (marker-buffer marker))
6410 (pos (marker-position marker)))
6411 (org-with-remote-undo buffer
6412 (with-current-buffer buffer
6413 (widen)
6414 (goto-char pos)
6415 (if (not (org-at-timestamp-p))
6416 (error "Cannot find time stamp"))
6417 (org-timestamp-change arg (or what 'day)))
6418 (org-agenda-show-new-time marker org-last-changed-timestamp))
6419 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6421 (defun org-agenda-date-earlier (arg &optional what)
6422 "Change the date of this item to one day earlier."
6423 (interactive "p")
6424 (org-agenda-date-later (- arg) what))
6426 (defun org-agenda-date-later-minutes (arg)
6427 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6428 (interactive "p")
6429 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6430 (org-agenda-date-later arg 'minute))
6432 (defun org-agenda-date-earlier-minutes (arg)
6433 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6434 (interactive "p")
6435 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6436 (org-agenda-date-earlier arg 'minute))
6438 (defun org-agenda-date-later-hours (arg)
6439 "Change the time of this item, in hour steps."
6440 (interactive "p")
6441 (org-agenda-date-later arg 'hour))
6443 (defun org-agenda-date-earlier-hours (arg)
6444 "Change the time of this item, in hour steps."
6445 (interactive "p")
6446 (org-agenda-date-earlier arg 'hour))
6448 (defun org-agenda-show-new-time (marker stamp &optional prefix)
6449 "Show new date stamp via text properties."
6450 ;; We use text properties to make this undoable
6451 (let ((inhibit-read-only t)
6452 (buffer-invisibility-spec))
6453 (setq stamp (concat " " prefix " => " stamp))
6454 (save-excursion
6455 (goto-char (point-max))
6456 (while (not (bobp))
6457 (when (equal marker (org-get-at-bol 'org-marker))
6458 (org-move-to-column (- (window-width) (length stamp)) t)
6459 (org-agenda-fix-tags-filter-overlays-at (point))
6460 (if (featurep 'xemacs)
6461 ;; Use `duplicable' property to trigger undo recording
6462 (let ((ex (make-extent nil nil))
6463 (gl (make-glyph stamp)))
6464 (set-glyph-face gl 'secondary-selection)
6465 (set-extent-properties
6466 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6467 (insert-extent ex (1- (point)) (point-at-eol)))
6468 (add-text-properties
6469 (1- (point)) (point-at-eol)
6470 (list 'display (org-add-props stamp nil
6471 'face 'secondary-selection))))
6472 (beginning-of-line 1))
6473 (beginning-of-line 0)))))
6475 (defun org-agenda-date-prompt (arg)
6476 "Change the date of this item. Date is prompted for, with default today.
6477 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6478 be used to request time specification in the time stamp."
6479 (interactive "P")
6480 (org-agenda-check-type t 'agenda 'timeline)
6481 (org-agenda-check-no-diary)
6482 (let* ((marker (or (org-get-at-bol 'org-marker)
6483 (org-agenda-error)))
6484 (buffer (marker-buffer marker))
6485 (pos (marker-position marker)))
6486 (org-with-remote-undo buffer
6487 (with-current-buffer buffer
6488 (widen)
6489 (goto-char pos)
6490 (if (not (org-at-timestamp-p))
6491 (error "Cannot find time stamp"))
6492 (org-time-stamp arg))
6493 (org-agenda-show-new-time marker org-last-changed-timestamp))
6494 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6496 (defun org-agenda-schedule (arg)
6497 "Schedule the item at point."
6498 (interactive "P")
6499 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6500 (org-agenda-check-no-diary)
6501 (let* ((marker (or (org-get-at-bol 'org-marker)
6502 (org-agenda-error)))
6503 (type (marker-insertion-type marker))
6504 (buffer (marker-buffer marker))
6505 (pos (marker-position marker))
6506 (org-insert-labeled-timestamps-at-point nil)
6508 (set-marker-insertion-type marker t)
6509 (org-with-remote-undo buffer
6510 (with-current-buffer buffer
6511 (widen)
6512 (goto-char pos)
6513 (setq ts (org-schedule arg)))
6514 (org-agenda-show-new-time marker ts "S"))
6515 (message "Item scheduled for %s" ts)))
6517 (defun org-agenda-deadline (arg)
6518 "Schedule the item at point."
6519 (interactive "P")
6520 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6521 (org-agenda-check-no-diary)
6522 (let* ((marker (or (org-get-at-bol 'org-marker)
6523 (org-agenda-error)))
6524 (buffer (marker-buffer marker))
6525 (pos (marker-position marker))
6526 (org-insert-labeled-timestamps-at-point nil)
6528 (org-with-remote-undo buffer
6529 (with-current-buffer buffer
6530 (widen)
6531 (goto-char pos)
6532 (setq ts (org-deadline arg)))
6533 (org-agenda-show-new-time marker ts "D"))
6534 (message "Deadline for this item set to %s" ts)))
6536 (defun org-agenda-action ()
6537 "Select entry for agenda action, or execute an agenda action.
6538 This command prompts for another letter. Valid inputs are:
6540 m Mark the entry at point for an agenda action
6541 s Schedule the marked entry to the date at the cursor
6542 d Set the deadline of the marked entry to the date at the cursor
6543 r Call `org-remember' with cursor date as the default date
6544 SPC Show marked entry in other window
6545 TAB Visit marked entry in other window
6547 The cursor may be at a date in the calendar, or in the Org agenda."
6548 (interactive)
6549 (let (ans)
6550 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6551 (setq ans (read-char-exclusive))
6552 (cond
6553 ((equal ans ?m)
6554 ;; Mark this entry
6555 (if (eq major-mode 'org-agenda-mode)
6556 (let ((m (or (org-get-at-bol 'org-hd-marker)
6557 (org-get-at-bol 'org-marker))))
6558 (if m
6559 (progn
6560 (move-marker org-agenda-action-marker
6561 (marker-position m) (marker-buffer m))
6562 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6563 (error "Don't know which entry to mark")))
6564 (error "This command works only in the agenda")))
6565 ((equal ans ?s)
6566 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6567 ((equal ans ?d)
6568 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6569 ((equal ans ?r)
6570 (org-agenda-do-action '(org-remember) t))
6571 ((equal ans ?\ )
6572 (let ((cw (selected-window)))
6573 (org-switch-to-buffer-other-window
6574 (marker-buffer org-agenda-action-marker))
6575 (goto-char org-agenda-action-marker)
6576 (org-show-context 'agenda)
6577 (select-window cw)))
6578 ((equal ans ?\C-i)
6579 (org-switch-to-buffer-other-window
6580 (marker-buffer org-agenda-action-marker))
6581 (goto-char org-agenda-action-marker)
6582 (org-show-context 'agenda))
6583 (t (error "Invalid agenda action %c" ans)))))
6585 (defun org-agenda-do-action (form &optional current-buffer)
6586 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6587 (let ((org-overriding-default-time (org-get-cursor-date)))
6588 (if current-buffer
6589 (eval form)
6590 (if (not (marker-buffer org-agenda-action-marker))
6591 (error "No entry has been selected for agenda action")
6592 (with-current-buffer (marker-buffer org-agenda-action-marker)
6593 (save-excursion
6594 (save-restriction
6595 (widen)
6596 (goto-char org-agenda-action-marker)
6597 (eval form))))))))
6599 (defun org-agenda-clock-in (&optional arg)
6600 "Start the clock on the currently selected item."
6601 (interactive "P")
6602 (org-agenda-check-no-diary)
6603 (if (equal arg '(4))
6604 (org-clock-in arg)
6605 (let* ((marker (or (org-get-at-bol 'org-marker)
6606 (org-agenda-error)))
6607 (hdmarker (or (org-get-at-bol 'org-hd-marker)
6608 marker))
6609 (pos (marker-position marker))
6610 newhead)
6611 (org-with-remote-undo (marker-buffer marker)
6612 (with-current-buffer (marker-buffer marker)
6613 (widen)
6614 (goto-char pos)
6615 (org-show-context 'agenda)
6616 (org-show-entry)
6617 (org-cycle-hide-drawers 'children)
6618 (org-clock-in arg)
6619 (setq newhead (org-get-heading)))
6620 (org-agenda-change-all-lines newhead hdmarker)))))
6622 (defun org-agenda-clock-out (&optional arg)
6623 "Stop the currently running clock."
6624 (interactive "P")
6625 (unless (marker-buffer org-clock-marker)
6626 (error "No running clock"))
6627 (let ((marker (make-marker)) newhead)
6628 (org-with-remote-undo (marker-buffer org-clock-marker)
6629 (with-current-buffer (marker-buffer org-clock-marker)
6630 (save-excursion
6631 (save-restriction
6632 (widen)
6633 (goto-char org-clock-marker)
6634 (org-back-to-heading t)
6635 (move-marker marker (point))
6636 (org-clock-out)
6637 (setq newhead (org-get-heading))))))
6638 (org-agenda-change-all-lines newhead marker)
6639 (move-marker marker nil)))
6641 (defun org-agenda-clock-cancel (&optional arg)
6642 "Cancel the currently running clock."
6643 (interactive "P")
6644 (unless (marker-buffer org-clock-marker)
6645 (error "No running clock"))
6646 (org-with-remote-undo (marker-buffer org-clock-marker)
6647 (org-clock-cancel)))
6649 (defun org-agenda-diary-entry-in-org-file ()
6650 "Make a diary entry in the file `org-agenda-diary-file'."
6651 (let (d1 d2 char (text ""))
6652 (if (equal (buffer-name) "*Calendar*")
6653 (setq d1 (calendar-cursor-to-date t)
6654 d2 (car calendar-mark-ring))
6655 (setq d1 (calendar-gregorian-from-absolute
6656 (get-text-property (point) 'day))
6657 d2 (and (mark) (get-text-property (mark) 'day)
6658 (calendar-gregorian-from-absolute
6659 (get-text-property (mark) 'day)))))
6660 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
6661 (setq char (read-char-exclusive))
6662 (cond
6663 ((equal char ?d)
6664 (setq text (read-string "Day entry: "))
6665 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1))
6666 ((equal char ?a)
6667 (setq d1 (list (car d1) (nth 1 d1)
6668 (read-number (format "Referece year [%d]: " (nth 2 d1))
6669 (nth 2 d1))))
6670 (setq text (read-string "Anniversary (use %d to show years): "))
6671 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1))
6672 ((equal char ?b)
6673 (setq text (read-string "Block entry: "))
6674 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2))
6675 ((equal char ?j)
6676 (org-switch-to-buffer-other-window
6677 (find-file-noselect org-agenda-diary-file))
6678 (org-datetree-find-date-create d1))
6679 (t (error "Invalid selection character `%c'" char)))))
6681 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
6682 "Add a diary entry with TYPE to `org-agenda-diary-file'.
6683 If TEXT is not empty, it will become the headling of the new entry, and
6684 the resulting entry will not be shown. When TEXT is empty, switch to
6685 `org-agenda-diary-file' and let the user finish the entry there."
6686 (let ((cw (current-window-configuration)))
6687 (org-switch-to-buffer-other-window
6688 (find-file-noselect org-agenda-diary-file))
6689 (widen)
6690 (goto-char (point-min))
6691 (cond
6692 ((eq type 'anniversary)
6693 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
6694 (progn
6695 (or (org-on-heading-p t)
6696 (progn
6697 (outline-next-heading)
6698 (insert "* Anniversaries\n\n")
6699 (beginning-of-line -1)))))
6700 (outline-next-heading)
6701 (org-back-over-empty-lines)
6702 (backward-char 1)
6703 (insert "\n")
6704 (let ((calendar-date-display-form (diary-date-display-form)))
6705 (insert (format "%%%%(diary-anniversary %s) %s"
6706 (calendar-date-string d1 nil t) text))))
6707 ((eq type 'day)
6708 (require 'org-datetree)
6709 (org-datetree-find-date-create d1)
6710 (org-agenda-insert-diary-make-new-entry text)
6711 (org-insert-time-stamp (org-time-from-absolute
6712 (calendar-absolute-from-gregorian d1)))
6713 (end-of-line 0))
6714 ((eq type 'block)
6715 (if (> (calendar-absolute-from-gregorian d1)
6716 (calendar-absolute-from-gregorian d2))
6717 (setq d1 (prog1 d2 (setq d2 d1))))
6718 (require 'org-datetree)
6719 (org-datetree-find-date-create d1)
6720 (org-agenda-insert-diary-make-new-entry text)
6721 (org-insert-time-stamp (org-time-from-absolute
6722 (calendar-absolute-from-gregorian d1)))
6723 (insert "--")
6724 (org-insert-time-stamp (org-time-from-absolute
6725 (calendar-absolute-from-gregorian d2)))
6726 (end-of-line 0)))
6727 (if (string-match "\\S-" text)
6728 (progn
6729 (set-window-configuration cw)
6730 (message "%s entry added to %s"
6731 (capitalize (symbol-name type))
6732 (abbreviate-file-name org-agenda-diary-file)))
6733 (message "Please finish entry here"))))
6735 (defun org-agenda-insert-diary-make-new-entry (text)
6736 "Make new entry as last child of current entry.
6737 Add TEXT as headline, and position the cursor in the second line so that
6738 a timestamp can be added there."
6739 (let ((org-show-following-heading t)
6740 (org-show-siblings t)
6741 (org-show-hierarchy-above t)
6742 (org-show-entry-below t)
6743 col)
6744 (outline-next-heading)
6745 (org-back-over-empty-lines)
6746 (or (looking-at "[ \t]*$")
6747 (progn (insert "\n") (backward-char 1)))
6748 (org-insert-heading)
6749 (org-do-demote)
6750 (setq col (current-column))
6751 (insert text "\n")
6752 (if org-adapt-indentation (org-indent-to-column col))
6753 (let ((org-show-following-heading t)
6754 (org-show-siblings t)
6755 (org-show-hierarchy-above t)
6756 (org-show-entry-below t))
6757 (org-show-context))))
6759 (defun org-agenda-diary-entry ()
6760 "Make a diary entry, like the `i' command from the calendar.
6761 All the standard commands work: block, weekly etc.
6762 When `org-agenda-diary-file' points to a file,
6763 `org-agenda-diary-entry-in-org-file' is called instead to create
6764 entries in that Org-mode file."
6765 (interactive)
6766 (org-agenda-check-type t 'agenda 'timeline)
6767 (if (not (eq org-agenda-diary-file 'diary-file))
6768 (org-agenda-diary-entry-in-org-file)
6769 (require 'diary-lib)
6770 (let* ((char (progn
6771 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6772 (read-char-exclusive)))
6773 (cmd (cdr (assoc char
6774 '((?d . insert-diary-entry)
6775 (?w . insert-weekly-diary-entry)
6776 (?m . insert-monthly-diary-entry)
6777 (?y . insert-yearly-diary-entry)
6778 (?a . insert-anniversary-diary-entry)
6779 (?b . insert-block-diary-entry)
6780 (?c . insert-cyclic-diary-entry)))))
6781 (oldf (symbol-function 'calendar-cursor-to-date))
6782 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6783 (point (point))
6784 (mark (or (mark t) (point))))
6785 (unless cmd
6786 (error "No command associated with <%c>" char))
6787 (unless (and (get-text-property point 'day)
6788 (or (not (equal ?b char))
6789 (get-text-property mark 'day)))
6790 (error "Don't know which date to use for diary entry"))
6791 ;; We implement this by hacking the `calendar-cursor-to-date' function
6792 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6793 (let ((calendar-mark-ring
6794 (list (calendar-gregorian-from-absolute
6795 (or (get-text-property mark 'day)
6796 (get-text-property point 'day))))))
6797 (unwind-protect
6798 (progn
6799 (fset 'calendar-cursor-to-date
6800 (lambda (&optional error dummy)
6801 (calendar-gregorian-from-absolute
6802 (get-text-property point 'day))))
6803 (call-interactively cmd))
6804 (fset 'calendar-cursor-to-date oldf))))))
6806 (defun org-agenda-execute-calendar-command (cmd)
6807 "Execute a calendar command from the agenda, with the date associated to
6808 the cursor position."
6809 (org-agenda-check-type t 'agenda 'timeline)
6810 (require 'diary-lib)
6811 (unless (get-text-property (point) 'day)
6812 (error "Don't know which date to use for calendar command"))
6813 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6814 (point (point))
6815 (date (calendar-gregorian-from-absolute
6816 (get-text-property point 'day)))
6817 ;; the following 2 vars are needed in the calendar
6818 (displayed-month (car date))
6819 (displayed-year (nth 2 date)))
6820 (unwind-protect
6821 (progn
6822 (fset 'calendar-cursor-to-date
6823 (lambda (&optional error dummy)
6824 (calendar-gregorian-from-absolute
6825 (get-text-property point 'day))))
6826 (call-interactively cmd))
6827 (fset 'calendar-cursor-to-date oldf))))
6829 (defun org-agenda-phases-of-moon ()
6830 "Display the phases of the moon for the 3 months around the cursor date."
6831 (interactive)
6832 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6834 (defun org-agenda-holidays ()
6835 "Display the holidays for the 3 months around the cursor date."
6836 (interactive)
6837 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6839 (defvar calendar-longitude)
6840 (defvar calendar-latitude)
6841 (defvar calendar-location-name)
6843 (defun org-agenda-sunrise-sunset (arg)
6844 "Display sunrise and sunset for the cursor date.
6845 Latitude and longitude can be specified with the variables
6846 `calendar-latitude' and `calendar-longitude'. When called with prefix
6847 argument, latitude and longitude will be prompted for."
6848 (interactive "P")
6849 (require 'solar)
6850 (let ((calendar-longitude (if arg nil calendar-longitude))
6851 (calendar-latitude (if arg nil calendar-latitude))
6852 (calendar-location-name
6853 (if arg "the given coordinates" calendar-location-name)))
6854 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6856 (defun org-agenda-goto-calendar ()
6857 "Open the Emacs calendar with the date at the cursor."
6858 (interactive)
6859 (org-agenda-check-type t 'agenda 'timeline)
6860 (let* ((day (or (get-text-property (point) 'day)
6861 (error "Don't know which date to open in calendar")))
6862 (date (calendar-gregorian-from-absolute day))
6863 (calendar-move-hook nil)
6864 (calendar-view-holidays-initially-flag nil)
6865 (calendar-view-diary-initially-flag nil)
6866 (view-calendar-holidays-initially nil)
6867 (view-diary-entries-initially nil))
6868 (calendar)
6869 (calendar-goto-date date)))
6871 ;;;###autoload
6872 (defun org-calendar-goto-agenda ()
6873 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6874 This is a command that has to be installed in `calendar-mode-map'."
6875 (interactive)
6876 (org-agenda-list nil (calendar-absolute-from-gregorian
6877 (calendar-cursor-to-date))
6878 nil))
6880 (defun org-agenda-convert-date ()
6881 (interactive)
6882 (org-agenda-check-type t 'agenda 'timeline)
6883 (let ((day (get-text-property (point) 'day))
6884 date s)
6885 (unless day
6886 (error "Don't know which date to convert"))
6887 (setq date (calendar-gregorian-from-absolute day))
6888 (setq s (concat
6889 "Gregorian: " (calendar-date-string date) "\n"
6890 "ISO: " (calendar-iso-date-string date) "\n"
6891 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6892 "Julian: " (calendar-julian-date-string date) "\n"
6893 "Astron. JD: " (calendar-astro-date-string date)
6894 " (Julian date number at noon UTC)\n"
6895 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6896 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6897 "French: " (calendar-french-date-string date) "\n"
6898 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6899 "Mayan: " (calendar-mayan-date-string date) "\n"
6900 "Coptic: " (calendar-coptic-date-string date) "\n"
6901 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6902 "Persian: " (calendar-persian-date-string date) "\n"
6903 "Chinese: " (calendar-chinese-date-string date) "\n"))
6904 (with-output-to-temp-buffer "*Dates*"
6905 (princ s))
6906 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6908 ;;; Bulk commands
6910 (defvar org-agenda-bulk-marked-entries nil
6911 "List of markers that refer to marked entries in the agenda.")
6913 (defun org-agenda-bulk-marked-p ()
6914 (eq (get-char-property (point-at-bol) 'type)
6915 'org-marked-entry-overlay))
6917 (defun org-agenda-bulk-mark ()
6918 "Mark the entry at point for future bulk action."
6919 (interactive)
6920 (org-agenda-check-no-diary)
6921 (let* ((m (org-get-at-bol 'org-hd-marker))
6923 (unless (org-agenda-bulk-marked-p)
6924 (unless m (error "Nothing to mark at point"))
6925 (push m org-agenda-bulk-marked-entries)
6926 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
6927 (org-overlay-display ov "> "
6928 (org-get-todo-face "TODO")
6929 'evaporate)
6930 (org-overlay-put ov 'type 'org-marked-entry-overlay))
6931 (beginning-of-line 2)
6932 (message "%d entries marked for bulk action"
6933 (length org-agenda-bulk-marked-entries))))
6935 (defun org-agenda-bulk-unmark ()
6936 "Unmark the entry at point for future bulk action."
6937 (interactive)
6938 (when (org-agenda-bulk-marked-p)
6939 (org-agenda-bulk-remove-overlays
6940 (point-at-bol) (+ 2 (point-at-bol)))
6941 (setq org-agenda-bulk-marked-entries
6942 (delete (org-get-at-bol 'org-hd-marker)
6943 org-agenda-bulk-marked-entries)))
6944 (beginning-of-line 2)
6945 (message "%d entries marked for bulk action"
6946 (length org-agenda-bulk-marked-entries)))
6948 (defun org-agenda-bulk-toggle ()
6949 "Toggle marking the entry at point for bulk action."
6950 (interactive)
6951 (if (org-agenda-bulk-marked-p)
6952 (org-agenda-bulk-unmark)
6953 (org-agenda-bulk-mark)))
6955 (defun org-agenda-bulk-remove-overlays (&optional beg end)
6956 "Remove the mark overlays between BEG and END in the agenda buffer.
6957 BEG and END default to the buffer limits.
6959 This only removes the overlays, it does not remove the markers
6960 from the list in `org-agenda-bulk-marked-entries'."
6961 (interactive)
6962 (mapc (lambda (ov)
6963 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
6964 (org-delete-overlay ov)))
6965 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
6967 (defun org-agenda-bulk-remove-all-marks ()
6968 "Remove all marks in the agenda buffer.
6969 This will remove the markers, and the overlays."
6970 (interactive)
6971 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
6972 (setq org-agenda-bulk-marked-entries nil)
6973 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
6975 (defun org-agenda-bulk-action ()
6976 "Execute an remote-editing action on all marked entries."
6977 (interactive)
6978 (unless org-agenda-bulk-marked-entries
6979 (error "No entries are marked"))
6980 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
6981 (let* ((action (read-char-exclusive))
6982 (entries (reverse org-agenda-bulk-marked-entries))
6983 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
6984 (cond
6985 ((equal action ?$)
6986 (setq cmd '(org-agenda-archive)))
6988 ((equal action ?A)
6989 (setq cmd '(org-agenda-archive-to-archive-sibling)))
6991 ((member action '(?r ?w))
6992 (setq rfloc (org-refile-get-location
6993 "Refile to: "
6994 (marker-buffer (car org-agenda-bulk-marked-entries))
6995 org-refile-allow-creating-parent-nodes))
6996 (setcar (nthcdr 3 rfloc)
6997 (move-marker (make-marker) (nth 3 rfloc)
6998 (or (get-file-buffer (nth 1 rfloc))
6999 (find-buffer-visiting (nth 1 rfloc))
7000 (error "This should not happen"))))
7002 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
7004 ((equal action ?t)
7005 (setq state (org-icompleting-read
7006 "Todo state: "
7007 (with-current-buffer (marker-buffer (car entries))
7008 (mapcar 'list org-todo-keywords-1))))
7009 (setq cmd `(let ((org-inhibit-blocking t)
7010 (org-inhibit-logging 'note))
7011 (org-agenda-todo ,state))))
7013 ((memq action '(?- ?+))
7014 (setq tag (org-icompleting-read
7015 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7016 (with-current-buffer (marker-buffer (car entries))
7017 (delq nil
7018 (mapcar (lambda (x)
7019 (if (stringp (car x)) x)) org-tag-alist)))))
7020 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7022 ((memq action '(?s ?d))
7023 (let* ((date (org-read-date
7024 nil nil nil
7025 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
7026 (ans org-read-date-final-answer)
7027 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7028 (setq cmd `(let* ((bound (fboundp 'read-string))
7029 (old (and bound (symbol-function 'read-string))))
7030 (unwind-protect
7031 (progn
7032 (fset 'read-string (lambda (&rest ignore) ,ans))
7033 (call-interactively ',c1))
7034 (if bound
7035 (fset 'read-string old)
7036 (fmakunbound 'read-string)))))))
7037 (t (error "Invalid bulk action")))
7039 ;; Sort the markers, to make sure that parents are handled before children
7040 (setq entries (sort entries
7041 (lambda (a b)
7042 (cond
7043 ((equal (marker-buffer a) (marker-buffer b))
7044 (< (marker-position a) (marker-position b)))
7046 (string< (buffer-name (marker-buffer a))
7047 (buffer-name (marker-buffer b))))))))
7049 ;; Now loop over all markers and apply cmd
7050 (while (setq e (pop entries))
7051 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7052 (if (not pos)
7053 (progn (message "Skipping removed entry at %s" e)
7054 (setq cntskip (1+ cntskip)))
7055 (goto-char pos)
7056 (eval cmd)
7057 (setq org-agenda-bulk-marked-entries
7058 (delete e org-agenda-bulk-marked-entries))
7059 (setq cnt (1+ cnt))))
7060 (setq org-agenda-bulk-marked-entries nil)
7061 (org-agenda-bulk-remove-all-marks)
7062 (message "Acted on %d entries%s"
7064 (if (= cntskip 0)
7066 (format ", skipped %d (disappeared before their turn)"
7067 cntskip)))))
7069 ;;; Flagging notes
7071 (defun org-agenda-show-the-flagging-note ()
7072 "Display the flagging note in the other window.
7073 When called a second time in direct sequence, offer to remove the FLAGGING
7074 tag and (if present) the flagging note."
7075 (interactive)
7076 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7077 (win (selected-window))
7078 note heading newhead)
7079 (unless hdmarker
7080 (error "No linked entry at point"))
7081 (if (and (eq this-command last-command)
7082 (y-or-n-p "Unflag and remove any flagging note? "))
7083 (progn
7084 (org-agenda-remove-flag hdmarker)
7085 (let ((win (get-buffer-window "*Flagging Note*")))
7086 (and win (delete-window win)))
7087 (message "Entry unflaged"))
7088 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7089 (unless note
7090 (error "No flagging note"))
7091 (org-kill-new note)
7092 (org-switch-to-buffer-other-window "*Flagging Note*")
7093 (erase-buffer)
7094 (insert note)
7095 (goto-char (point-min))
7096 (while (re-search-forward "\\\\n" nil t)
7097 (replace-match "\n" t t))
7098 (goto-char (point-min))
7099 (select-window win)
7100 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7102 (defun org-agenda-remove-flag (marker)
7103 "Remove the FLAGGED tag and any flaging note in the entry."
7104 (let (newhead)
7105 (org-with-point-at marker
7106 (org-toggle-tag "FLAGGED" 'off)
7107 (org-entry-delete nil "THEFLAGGINGNOTE")
7108 (setq newhead (org-get-heading)))
7109 (org-agenda-change-all-lines newhead marker)
7110 (message "Entry unflaged")))
7112 (defun org-agenda-get-any-marker (&optional pos)
7113 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7114 (get-text-property (or pos (point-at-bol)) 'org-marker)))
7116 ;;; Appointment reminders
7118 (defvar appt-time-msg-list)
7120 ;;;###autoload
7121 (defun org-agenda-to-appt (&optional refresh filter)
7122 "Activate appointments found in `org-agenda-files'.
7123 With a \\[universal-argument] prefix, refresh the list of
7124 appointments.
7126 If FILTER is t, interactively prompt the user for a regular
7127 expression, and filter out entries that don't match it.
7129 If FILTER is a string, use this string as a regular expression
7130 for filtering entries out.
7132 FILTER can also be an alist with the car of each cell being
7133 either 'headline or 'category. For example:
7135 '((headline \"IMPORTANT\")
7136 (category \"Work\"))
7138 will only add headlines containing IMPORTANT or headlines
7139 belonging to the \"Work\" category."
7140 (interactive "P")
7141 (require 'calendar)
7142 (if refresh (setq appt-time-msg-list nil))
7143 (if (eq filter t)
7144 (setq filter (read-from-minibuffer "Regexp filter: ")))
7145 (let* ((cnt 0) ; count added events
7146 (org-agenda-new-buffers nil)
7147 (org-deadline-warning-days 0)
7148 (today (org-date-to-gregorian
7149 (time-to-days (current-time))))
7150 (org-agenda-restrict nil)
7151 (files (org-agenda-files 'unrestricted)) entries file)
7152 ;; Get all entries which may contain an appt
7153 (org-prepare-agenda-buffers files)
7154 (while (setq file (pop files))
7155 (setq entries
7156 (append entries
7157 (org-agenda-get-day-entries
7158 file today :timestamp :scheduled :deadline))))
7159 (setq entries (delq nil entries))
7160 ;; Map thru entries and find if we should filter them out
7161 (mapc
7162 (lambda(x)
7163 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
7164 (cat (get-text-property 1 'org-category x))
7165 (tod (get-text-property 1 'time-of-day x))
7166 (ok (or (null filter)
7167 (and (stringp filter) (string-match filter evt))
7168 (and (listp filter)
7169 (or (string-match
7170 (cadr (assoc 'category filter)) cat)
7171 (string-match
7172 (cadr (assoc 'headline filter)) evt))))))
7173 ;; FIXME: Shall we remove text-properties for the appt text?
7174 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
7175 (when (and ok tod)
7176 (setq tod (concat "00" (number-to-string tod))
7177 tod (when (string-match
7178 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
7179 (concat (match-string 1 tod) ":"
7180 (match-string 2 tod))))
7181 (appt-add tod evt)
7182 (setq cnt (1+ cnt))))) entries)
7183 (org-release-buffers org-agenda-new-buffers)
7184 (if (eq cnt 0)
7185 (message "No event to add")
7186 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
7188 (defun org-agenda-todayp (date)
7189 "Does DATE mean today, when considering `org-extend-today-until'?"
7190 (let (today h)
7191 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
7192 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
7193 (setq h (nth 2 (decode-time (current-time))))
7194 (or (and (>= h org-extend-today-until)
7195 (= date today))
7196 (and (< h org-extend-today-until)
7197 (= date (1- today))))))
7199 (provide 'org-agenda)
7201 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
7203 ;;; org-agenda.el ends here