test: tests for expanding noweb references
[org-mode/org-jambu.git] / lisp / org-agenda.el
blob5f01848678320ebebe5ec11c3c3f9ba65a11b882
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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: 7.5
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))
37 (declare-function diary-add-to-list "diary-lib"
38 (date string specifier &optional marker globcolor literal))
39 (declare-function calendar-absolute-from-iso "cal-iso" (date))
40 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
41 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
42 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
43 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
44 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
45 (declare-function calendar-french-date-string "cal-french" (&optional date))
46 (declare-function calendar-goto-date "cal-move" (date))
47 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
48 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
49 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
50 (declare-function calendar-iso-from-absolute "cal-iso" (date))
51 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
52 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
53 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
54 (declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
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 &optional moment))
64 (defvar calendar-mode-map)
65 (defvar org-clock-current-task) ; defined in org-clock.el
66 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
67 (defvar org-habit-show-habits)
68 (defvar org-habit-show-habits-only-for-today)
70 ;; Defined somewhere in this file, but used before definition.
71 (defvar org-agenda-buffer-name)
72 (defvar org-agenda-overriding-header)
73 (defvar org-agenda-title-append nil)
74 (defvar entry)
75 (defvar date)
76 (defvar org-agenda-undo-list)
77 (defvar org-agenda-pending-undo-list)
78 (defvar original-date) ; dynamically scoped, calendar.el does scope this
80 (defcustom org-agenda-confirm-kill 1
81 "When set, remote killing from the agenda buffer needs confirmation.
82 When t, a confirmation is always needed. When a number N, confirmation is
83 only needed when the text to be killed contains more than N non-white lines."
84 :group 'org-agenda
85 :type '(choice
86 (const :tag "Never" nil)
87 (const :tag "Always" t)
88 (integer :tag "When more than N lines")))
90 (defcustom org-agenda-compact-blocks nil
91 "Non-nil means make the block agenda more compact.
92 This is done by leaving out unnecessary lines."
93 :group 'org-agenda
94 :type 'boolean)
96 (defcustom org-agenda-block-separator ?=
97 "The separator between blocks in the agenda.
98 If this is a string, it will be used as the separator, with a newline added.
99 If it is a character, it will be repeated to fill the window width."
100 :group 'org-agenda
101 :type '(choice
102 (character)
103 (string)))
105 (defgroup org-agenda-export nil
106 "Options concerning exporting agenda views in Org-mode."
107 :tag "Org Agenda Export"
108 :group 'org-agenda)
110 (defcustom org-agenda-with-colors t
111 "Non-nil means use colors in agenda views."
112 :group 'org-agenda-export
113 :type 'boolean)
115 (defcustom org-agenda-exporter-settings nil
116 "Alist of variable/value pairs that should be active during agenda export.
117 This is a good place to set options for ps-print and for htmlize.
118 Note that the way this is implemented, the values will be evaluated
119 before assigned to the variables. So make sure to quote values you do
120 *not* want evaluated, for example
122 (setq org-agenda-exporter-settings
123 '((ps-print-color-p 'black-white)))"
124 :group 'org-agenda-export
125 :type '(repeat
126 (list
127 (variable)
128 (sexp :tag "Value"))))
130 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
131 "Hook run in temporary buffer before writing it to an export file.
132 A useful function is `org-agenda-add-entry-text'."
133 :group 'org-agenda-export
134 :type 'hook
135 :options '(org-agenda-add-entry-text))
137 (defcustom org-agenda-add-entry-text-maxlines 0
138 "Maximum number of entry text lines to be added to agenda.
139 This is only relevant when `org-agenda-add-entry-text' is part of
140 `org-agenda-before-write-hook', which it is by default.
141 When this is 0, nothing will happen. When it is greater than 0, it
142 specifies the maximum number of lines that will be added for each entry
143 that is listed in the agenda view.
145 Note that this variable is not used during display, only when exporting
146 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
147 and `org-agenda-entry-text-maxlines'."
148 :group 'org-agenda
149 :type 'integer)
151 (defcustom org-agenda-add-entry-text-descriptive-links t
152 "Non-nil means export org-links as descriptive links in agenda added text.
153 This variable applies to the text added to the agenda when
154 `org-agenda-add-entry-text-maxlines' is larger than 0.
155 When this variable nil, the URL will (also) be shown."
156 :group 'org-agenda
157 :type 'boolean)
159 (defcustom org-agenda-export-html-style ""
160 "The style specification for exported HTML Agenda files.
161 If this variable contains a string, it will replace the default <style>
162 section as produced by `htmlize'.
163 Since there are different ways of setting style information, this variable
164 needs to contain the full HTML structure to provide a style, including the
165 surrounding HTML tags. The style specifications should include definitions
166 the fonts used by the agenda, here is an example:
168 <style type=\"text/css\">
169 p { font-weight: normal; color: gray; }
170 .org-agenda-structure {
171 font-size: 110%;
172 color: #003399;
173 font-weight: 600;
175 .org-todo {
176 color: #cc6666;
177 font-weight: bold;
179 .org-agenda-done {
180 color: #339933;
182 .org-done {
183 color: #339933;
185 .title { text-align: center; }
186 .todo, .deadline { color: red; }
187 .done { color: green; }
188 </style>
190 or, if you want to keep the style in a file,
192 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
194 As the value of this option simply gets inserted into the HTML <head> header,
195 you can \"misuse\" it to also add other text to the header. However,
196 <style>...</style> is required, if not present the variable will be ignored."
197 :group 'org-agenda-export
198 :group 'org-export-html
199 :type 'string)
201 (defcustom org-agenda-persistent-filter nil
202 "When set, keep filters from one agenda view to the next."
203 :group 'org-agenda
204 :type 'boolean)
206 (defgroup org-agenda-custom-commands nil
207 "Options concerning agenda views in Org-mode."
208 :tag "Org Agenda Custom Commands"
209 :group 'org-agenda)
211 (defconst org-sorting-choice
212 '(choice
213 (const time-up) (const time-down)
214 (const category-keep) (const category-up) (const category-down)
215 (const tag-down) (const tag-up)
216 (const priority-up) (const priority-down)
217 (const todo-state-up) (const todo-state-down)
218 (const effort-up) (const effort-down)
219 (const habit-up) (const habit-down)
220 (const alpha-up) (const alpha-down)
221 (const user-defined-up) (const user-defined-down))
222 "Sorting choices.")
224 (defconst org-agenda-custom-commands-local-options
225 `(repeat :tag "Local settings for this command. Remember to quote values"
226 (choice :tag "Setting"
227 (list :tag "Heading for this block"
228 (const org-agenda-overriding-header)
229 (string :tag "Headline"))
230 (list :tag "Files to be searched"
231 (const org-agenda-files)
232 (list
233 (const :format "" quote)
234 (repeat (file))))
235 (list :tag "Sorting strategy"
236 (const org-agenda-sorting-strategy)
237 (list
238 (const :format "" quote)
239 (repeat
240 ,org-sorting-choice)))
241 (list :tag "Prefix format"
242 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
243 (string))
244 (list :tag "Number of days in agenda"
245 (const org-agenda-span)
246 (choice (const :tag "Day" 'day)
247 (const :tag "Week" 'week)
248 (const :tag "Month" 'month)
249 (const :tag "Year" 'year)
250 (integer :tag "Custom")))
251 (list :tag "Fixed starting date"
252 (const org-agenda-start-day)
253 (string :value "2007-11-01"))
254 (list :tag "Start on day of week"
255 (const org-agenda-start-on-weekday)
256 (choice :value 1
257 (const :tag "Today" nil)
258 (integer :tag "Weekday No.")))
259 (list :tag "Include data from diary"
260 (const org-agenda-include-diary)
261 (boolean))
262 (list :tag "Deadline Warning days"
263 (const org-deadline-warning-days)
264 (integer :value 1))
265 (list :tag "Tags filter preset"
266 (const org-agenda-filter-preset)
267 (list
268 (const :format "" quote)
269 (repeat
270 (string :tag "+tag or -tag"))))
271 (list :tag "Set daily/weekly entry types"
272 (const org-agenda-entry-types)
273 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
274 (const :deadline)
275 (const :scheduled)
276 (const :timestamp)
277 (const :sexp)))
278 (list :tag "Standard skipping condition"
279 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
280 (const org-agenda-skip-function)
281 (list
282 (const :format "" quote)
283 (list
284 (choice
285 :tag "Skipping range"
286 (const :tag "Skip entry" org-agenda-skip-entry-if)
287 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
288 (repeat :inline t :tag "Conditions for skipping"
289 (choice
290 :tag "Condition type"
291 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
292 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
293 (list :tag "TODO state is" :inline t
294 (const 'todo)
295 (choice
296 (const :tag "any not-done state" 'todo)
297 (const :tag "any done state" 'done)
298 (const :tag "any state" 'any)
299 (list :tag "Keyword list"
300 (const :format "" quote)
301 (repeat (string :tag "Keyword")))))
302 (list :tag "TODO state is not" :inline t
303 (const 'nottodo)
304 (choice
305 (const :tag "any not-done state" 'todo)
306 (const :tag "any done state" 'done)
307 (const :tag "any state" 'any)
308 (list :tag "Keyword list"
309 (const :format "" quote)
310 (repeat (string :tag "Keyword")))))
311 (const :tag "scheduled" 'scheduled)
312 (const :tag "not scheduled" 'notscheduled)
313 (const :tag "deadline" 'deadline)
314 (const :tag "no deadline" 'notdeadline)
315 (const :tag "timestamp" 'timestamp)
316 (const :tag "no timestamp" 'nottimestamp))))))
317 (list :tag "Non-standard skipping condition"
318 :value (org-agenda-skip-function)
319 (const org-agenda-skip-function)
320 (sexp :tag "Function or form (quoted!)"))
321 (list :tag "Any variable"
322 (variable :tag "Variable")
323 (sexp :tag "Value (sexp)"))))
324 "Selection of examples for agenda command settings.
325 This will be spliced into the custom type of
326 `org-agenda-custom-commands'.")
329 (defcustom org-agenda-custom-commands nil
330 "Custom commands for the agenda.
331 These commands will be offered on the splash screen displayed by the
332 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
334 (key desc type match settings files)
336 key The key (one or more characters as a string) to be associated
337 with the command.
338 desc A description of the command, when omitted or nil, a default
339 description is built using MATCH.
340 type The command type, any of the following symbols:
341 agenda The daily/weekly agenda.
342 todo Entries with a specific TODO keyword, in all agenda files.
343 search Entries containing search words entry or headline.
344 tags Tags/Property/TODO match in all agenda files.
345 tags-todo Tags/P/T match in all agenda files, TODO entries only.
346 todo-tree Sparse tree of specific TODO keyword in *current* file.
347 tags-tree Sparse tree with all tags matches in *current* file.
348 occur-tree Occur sparse tree for *current* file.
349 ... A user-defined function.
350 match What to search for:
351 - a single keyword for TODO keyword searches
352 - a tags match expression for tags searches
353 - a word search expression for text searches.
354 - a regular expression for occur searches
355 For all other commands, this should be the empty string.
356 settings A list of option settings, similar to that in a let form, so like
357 this: ((opt1 val1) (opt2 val2) ...). The values will be
358 evaluated at the moment of execution, so quote them when needed.
359 files A list of files file to write the produced agenda buffer to
360 with the command `org-store-agenda-views'.
361 If a file name ends in \".html\", an HTML version of the buffer
362 is written out. If it ends in \".ps\", a postscript version is
363 produced. Otherwise, only the plain text is written to the file.
365 You can also define a set of commands, to create a composite agenda buffer.
366 In this case, an entry looks like this:
368 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
370 where
372 desc A description string to be displayed in the dispatcher menu.
373 cmd An agenda command, similar to the above. However, tree commands
374 are no allowed, but instead you can get agenda and global todo list.
375 So valid commands for a set are:
376 (agenda \"\" settings)
377 (alltodo \"\" settings)
378 (stuck \"\" settings)
379 (todo \"match\" settings files)
380 (search \"match\" settings files)
381 (tags \"match\" settings files)
382 (tags-todo \"match\" settings files)
384 Each command can carry a list of options, and another set of options can be
385 given for the whole set of commands. Individual command options take
386 precedence over the general options.
388 When using several characters as key to a command, the first characters
389 are prefix commands. For the dispatcher to display useful information, you
390 should provide a description for the prefix, like
392 (setq org-agenda-custom-commands
393 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
394 (\"hl\" tags \"+HOME+Lisa\")
395 (\"hp\" tags \"+HOME+Peter\")
396 (\"hk\" tags \"+HOME+Kim\")))"
397 :group 'org-agenda-custom-commands
398 :type `(repeat
399 (choice :value ("x" "Describe command here" tags "" nil)
400 (list :tag "Single command"
401 (string :tag "Access Key(s) ")
402 (option (string :tag "Description"))
403 (choice
404 (const :tag "Agenda" agenda)
405 (const :tag "TODO list" alltodo)
406 (const :tag "Search words" search)
407 (const :tag "Stuck projects" stuck)
408 (const :tag "Tags/Property match (all agenda files)" tags)
409 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
410 (const :tag "TODO keyword search (all agenda files)" todo)
411 (const :tag "Tags sparse tree (current buffer)" tags-tree)
412 (const :tag "TODO keyword tree (current buffer)" todo-tree)
413 (const :tag "Occur tree (current buffer)" occur-tree)
414 (sexp :tag "Other, user-defined function"))
415 (string :tag "Match (only for some commands)")
416 ,org-agenda-custom-commands-local-options
417 (option (repeat :tag "Export" (file :tag "Export to"))))
418 (list :tag "Command series, all agenda files"
419 (string :tag "Access Key(s)")
420 (string :tag "Description ")
421 (repeat :tag "Component"
422 (choice
423 (list :tag "Agenda"
424 (const :format "" agenda)
425 (const :tag "" :format "" "")
426 ,org-agenda-custom-commands-local-options)
427 (list :tag "TODO list (all keywords)"
428 (const :format "" alltodo)
429 (const :tag "" :format "" "")
430 ,org-agenda-custom-commands-local-options)
431 (list :tag "Search words"
432 (const :format "" search)
433 (string :tag "Match")
434 ,org-agenda-custom-commands-local-options)
435 (list :tag "Stuck projects"
436 (const :format "" stuck)
437 (const :tag "" :format "" "")
438 ,org-agenda-custom-commands-local-options)
439 (list :tag "Tags search"
440 (const :format "" tags)
441 (string :tag "Match")
442 ,org-agenda-custom-commands-local-options)
443 (list :tag "Tags search, TODO entries only"
444 (const :format "" tags-todo)
445 (string :tag "Match")
446 ,org-agenda-custom-commands-local-options)
447 (list :tag "TODO keyword search"
448 (const :format "" todo)
449 (string :tag "Match")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "Other, user-defined function"
452 (symbol :tag "function")
453 (string :tag "Match")
454 ,org-agenda-custom-commands-local-options)))
456 (repeat :tag "Settings for entire command set"
457 (list (variable :tag "Any variable")
458 (sexp :tag "Value")))
459 (option (repeat :tag "Export" (file :tag "Export to"))))
460 (cons :tag "Prefix key documentation"
461 (string :tag "Access Key(s)")
462 (string :tag "Description ")))))
464 (defcustom org-agenda-query-register ?o
465 "The register holding the current query string.
466 The purpose of this is that if you construct a query string interactively,
467 you can then use it to define a custom command."
468 :group 'org-agenda-custom-commands
469 :type 'character)
471 (defcustom org-stuck-projects
472 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
473 "How to identify stuck projects.
474 This is a list of four items:
475 1. A tags/todo/property matcher string that is used to identify a project.
476 See the manual for a description of tag and property searches.
477 The entire tree below a headline matched by this is considered one project.
478 2. A list of TODO keywords identifying non-stuck projects.
479 If the project subtree contains any headline with one of these todo
480 keywords, the project is considered to be not stuck. If you specify
481 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
482 3. A list of tags identifying non-stuck projects.
483 If the project subtree contains any headline with one of these tags,
484 the project is considered to be not stuck. If you specify \"*\" as
485 a tag, any tag will mark the project unstuck. Note that this is about
486 the explicit presence of a tag somewhere in the subtree, inherited
487 tags to not count here. If inherited tags make a project not stuck,
488 use \"-TAG\" in the tags part of the matcher under (1.) above.
489 4. An arbitrary regular expression matching non-stuck projects.
491 If the project turns out to be not stuck, search continues also in the
492 subtree to see if any of the subtasks have project status.
494 See also the variable `org-tags-match-list-sublevels' which applies
495 to projects matched by this search as well.
497 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
498 or `C-c a #' to produce the list."
499 :group 'org-agenda-custom-commands
500 :type '(list
501 (string :tag "Tags/TODO match to identify a project")
502 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
503 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
504 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
506 (defcustom org-agenda-filter-effort-default-operator "<"
507 "The default operator for effort estimate filtering.
508 If you select an effort estimate limit without first pressing an operator,
509 this one will be used."
510 :group 'org-agenda-custom-commands
511 :type '(choice (const :tag "less or equal" "<")
512 (const :tag "greater or equal"">")
513 (const :tag "equal" "=")))
515 (defgroup org-agenda-skip nil
516 "Options concerning skipping parts of agenda files."
517 :tag "Org Agenda Skip"
518 :group 'org-agenda)
520 (defcustom org-agenda-skip-function-global nil
521 "Function to be called at each match during agenda construction.
522 If this function returns nil, the current match should not be skipped.
523 If the function decided to skip an agenda match, is must return the
524 buffer position from which the search should be continued.
525 This may also be a Lisp form, which will be evaluated.
527 This variable will be applied to every agenda match, including
528 tags/property searches and TODO lists. So try to make the test function
529 do its checking as efficiently as possible. To implement a skipping
530 condition just for specific agenda commands, use the variable
531 `org-agenda-skip-function' which can be set in the options section
532 of custom agenda commands."
533 :group 'org-agenda-skip
534 :type 'sexp)
536 (defgroup org-agenda-daily/weekly nil
537 "Options concerning the daily/weekly agenda."
538 :tag "Org Agenda Daily/Weekly"
539 :group 'org-agenda)
540 (defgroup org-agenda-todo-list nil
541 "Options concerning the global todo list agenda view."
542 :tag "Org Agenda Todo List"
543 :group 'org-agenda)
544 (defgroup org-agenda-match-view nil
545 "Options concerning the general tags/property/todo match agenda view."
546 :tag "Org Agenda Match View"
547 :group 'org-agenda)
548 (defgroup org-agenda-search-view nil
549 "Options concerning the general tags/property/todo match agenda view."
550 :tag "Org Agenda Match View"
551 :group 'org-agenda)
553 (defvar org-agenda-archives-mode nil
554 "Non-nil means the agenda will include archived items.
555 If this is the symbol `trees', trees in the selected agenda scope
556 that are marked with the ARCHIVE tag will be included anyway. When this is
557 t, also all archive files associated with the current selection of agenda
558 files will be included.")
560 (defcustom org-agenda-skip-comment-trees t
561 "Non-nil means skip trees that start with the COMMENT keyword.
562 When nil, these trees are also scanned by agenda commands."
563 :group 'org-agenda-skip
564 :type 'boolean)
566 (defcustom org-agenda-todo-list-sublevels t
567 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
568 When nil, the sublevels of a TODO entry are not checked, resulting in
569 potentially much shorter TODO lists."
570 :group 'org-agenda-skip
571 :group 'org-agenda-todo-list
572 :type 'boolean)
574 (defcustom org-agenda-todo-ignore-with-date nil
575 "Non-nil means don't show entries with a date in the global todo list.
576 You can use this if you prefer to mark mere appointments with a TODO keyword,
577 but don't want them to show up in the TODO list.
578 When this is set, it also covers deadlines and scheduled items, the settings
579 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
580 will be ignored.
581 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
582 :group 'org-agenda-skip
583 :group 'org-agenda-todo-list
584 :type 'boolean)
586 (defcustom org-agenda-todo-ignore-timestamp nil
587 "Non-nil means don't show entries with a timestamp.
588 This applies when creating the global todo list.
589 Valid values are:
591 past Don't show entries for today or in the past.
593 future Don't show entries with a timestamp in the future.
594 The idea behind this is that if it has a future
595 timestamp, you don't want to think about it until the
596 date.
598 all Don't show any entries with a timestamp in the global todo list.
599 The idea behind this is that by setting a timestamp, you
600 have already \"taken care\" of this item.
602 This variable can also have an integer as a value. If positive (N),
603 todos with a timestamp N or more days in the future will be ignored. If
604 negative (-N), todos with a timestamp N or more days in the past will be
605 ignored. If 0, todos with a timestamp either today or in the future will
606 be ignored. For example, a value of -1 will exclude todos with a
607 timestamp in the past (yesterday or earlier), while a value of 7 will
608 exclude todos with a timestamp a week or more in the future.
610 See also `org-agenda-todo-ignore-with-date'.
611 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
612 to make his option also apply to the tags-todo list."
613 :group 'org-agenda-skip
614 :group 'org-agenda-todo-list
615 :type '(choice
616 (const :tag "Ignore future timestamp todos" future)
617 (const :tag "Ignore past or present timestamp todos" past)
618 (const :tag "Ignore all timestamp todos" all)
619 (const :tag "Show timestamp todos" nil)
620 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
622 (defcustom org-agenda-todo-ignore-scheduled nil
623 "Non-nil means, ignore some scheduled TODO items when making TODO list.
624 This applies when creating the global todo list.
625 Valid values are:
627 past Don't show entries scheduled today or in the past.
629 future Don't show entries scheduled in the future.
630 The idea behind this is that by scheduling it, you don't want to
631 think about it until the scheduled date.
633 all Don't show any scheduled entries in the global todo list.
634 The idea behind this is that by scheduling it, you have already
635 \"taken care\" of this item.
637 t Same as `all', for backward compatibility.
639 This variable can also have an integer as a value. See
640 `org-agenda-todo-ignore-timestamp' for more details.
642 See also `org-agenda-todo-ignore-with-date'.
643 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
644 to make his option also apply to the tags-todo list."
645 :group 'org-agenda-skip
646 :group 'org-agenda-todo-list
647 :type '(choice
648 (const :tag "Ignore future-scheduled todos" future)
649 (const :tag "Ignore past- or present-scheduled todos" past)
650 (const :tag "Ignore all scheduled todos" all)
651 (const :tag "Ignore all scheduled todos (compatibility)" t)
652 (const :tag "Show scheduled todos" nil)
653 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
655 (defcustom org-agenda-todo-ignore-deadlines nil
656 "Non-nil means ignore some deadlined TODO items when making TODO list.
657 There are different motivations for using different values, please think
658 carefully when configuring this variable.
660 This applies when creating the global todo list.
661 Valid values are:
663 near Don't show near deadline entries. A deadline is near when it is
664 closer than `org-deadline-warning-days' days. The idea behind this
665 is that such items will appear in the agenda anyway.
667 far Don't show TODO entries where a deadline has been defined, but
668 the deadline is not near. This is useful if you don't want to
669 use the todo list to figure out what to do now.
671 past Don't show entries with a deadline timestamp for today or in the past.
673 future Don't show entries with a deadline timestamp in the future, not even
674 when they become `near' ones. Use it with caution.
676 all Ignore all TODO entries that do have a deadline.
678 t Same as `near', for backward compatibility.
680 This variable can also have an integer as a value. See
681 `org-agenda-todo-ignore-timestamp' for more details.
683 See also `org-agenda-todo-ignore-with-date'.
684 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
685 to make his option also apply to the tags-todo list."
686 :group 'org-agenda-skip
687 :group 'org-agenda-todo-list
688 :type '(choice
689 (const :tag "Ignore near deadlines" near)
690 (const :tag "Ignore near deadlines (compatibility)" t)
691 (const :tag "Ignore far deadlines" far)
692 (const :tag "Ignore all TODOs with a deadlines" all)
693 (const :tag "Show all TODOs, even if they have a deadline" nil)
694 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
696 (defcustom org-agenda-tags-todo-honor-ignore-options nil
697 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
698 The variables
699 `org-agenda-todo-ignore-with-date',
700 `org-agenda-todo-ignore-timestamp',
701 `org-agenda-todo-ignore-scheduled',
702 `org-agenda-todo-ignore-deadlines'
703 make the global TODO list skip entries that have time stamps of certain
704 kinds. If this option is set, the same options will also apply for the
705 tags-todo search, which is the general tags/property matcher
706 restricted to unfinished TODO entries only."
707 :group 'org-agenda-skip
708 :group 'org-agenda-todo-list
709 :group 'org-agenda-match-view
710 :type 'boolean)
712 (defcustom org-agenda-skip-scheduled-if-done nil
713 "Non-nil means don't show scheduled items in agenda when they are done.
714 This is relevant for the daily/weekly agenda, not for the TODO list. And
715 it applies only to the actual date of the scheduling. Warnings about
716 an item with a past scheduling dates are always turned off when the item
717 is DONE."
718 :group 'org-agenda-skip
719 :group 'org-agenda-daily/weekly
720 :type 'boolean)
722 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
723 "Non-nil means skip scheduling line if same entry shows because of deadline.
724 In the agenda of today, an entry can show up multiple times because
725 it is both scheduled and has a nearby deadline, and maybe a plain time
726 stamp as well.
727 When this variable is t, then only the deadline is shown and the fact that
728 the entry is scheduled today or was scheduled previously is not shown.
729 When this variable is nil, the entry will be shown several times. When
730 the variable is the symbol `not-today', then skip scheduled previously,
731 but not scheduled today."
732 :group 'org-agenda-skip
733 :group 'org-agenda-daily/weekly
734 :type '(choice
735 (const :tag "Never" nil)
736 (const :tag "Always" t)
737 (const :tag "Not when scheduled today" not-today)))
739 (defcustom org-agenda-skip-deadline-if-done nil
740 "Non-nil means don't show deadlines when the corresponding item is done.
741 When nil, the deadline is still shown and should give you a happy feeling.
742 This is relevant for the daily/weekly agenda. And it applied only to the
743 actually date of the deadline. Warnings about approaching and past-due
744 deadlines are always turned off when the item is DONE."
745 :group 'org-agenda-skip
746 :group 'org-agenda-daily/weekly
747 :type 'boolean)
749 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
750 "Non-nil means skip deadline prewarning when entry is also scheduled.
751 This will apply on all days where a prewarning for the deadline would
752 be shown, but not at the day when the entry is actually due. On that day,
753 the deadline will be shown anyway.
754 This variable may be set to nil, t, or a number which will then give
755 the number of days before the actual deadline when the prewarnings
756 should resume.
757 This can be used in a workflow where the first showing of the deadline will
758 trigger you to schedule it, and then you don't want to be reminded of it
759 because you will take care of it on the day when scheduled."
760 :group 'org-agenda-skip
761 :group 'org-agenda-daily/weekly
762 :type '(choice
763 (const :tag "Alwas show prewarning" nil)
764 (const :tag "Remove prewarning if entry is scheduled" t)
765 (integer :tag "Restart prewarning N days before deadline")))
767 (defcustom org-agenda-skip-additional-timestamps-same-entry t
768 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
769 When non-nil, after the search for timestamps has matched once in an
770 entry, the rest of the entry will not be searched."
771 :group 'org-agenda-skip
772 :type 'boolean)
774 (defcustom org-agenda-skip-timestamp-if-done nil
775 "Non-nil means don't select item by timestamp or -range if it is DONE."
776 :group 'org-agenda-skip
777 :group 'org-agenda-daily/weekly
778 :type 'boolean)
780 (defcustom org-agenda-dim-blocked-tasks t
781 "Non-nil means dim blocked tasks in the agenda display.
782 This causes some overhead during agenda construction, but if you
783 have turned on `org-enforce-todo-dependencies',
784 `org-enforce-todo-checkbox-dependencies', or any other blocking
785 mechanism, this will create useful feedback in the agenda.
787 Instead of t, this variable can also have the value `invisible'.
788 Then blocked tasks will be invisible and only become visible when
789 they become unblocked. An exemption to this behavior is when a task is
790 blocked because of unchecked checkboxes below it. Since checkboxes do
791 not show up in the agenda views, making this task invisible you remove any
792 trace from agenda views that there is something to do. Therefore, a task
793 that is blocked because of checkboxes will never be made invisible, it
794 will only be dimmed."
795 :group 'org-agenda-daily/weekly
796 :group 'org-agenda-todo-list
797 :type '(choice
798 (const :tag "Do not dim" nil)
799 (const :tag "Dim to a grey face" t)
800 (const :tag "Make invisible" invisible)))
802 (defcustom org-timeline-show-empty-dates 3
803 "Non-nil means `org-timeline' also shows dates without an entry.
804 When nil, only the days which actually have entries are shown.
805 When t, all days between the first and the last date are shown.
806 When an integer, show also empty dates, but if there is a gap of more than
807 N days, just insert a special line indicating the size of the gap."
808 :group 'org-agenda-skip
809 :type '(choice
810 (const :tag "None" nil)
811 (const :tag "All" t)
812 (integer :tag "at most")))
814 (defgroup org-agenda-startup nil
815 "Options concerning initial settings in the Agenda in Org Mode."
816 :tag "Org Agenda Startup"
817 :group 'org-agenda)
819 (defcustom org-agenda-menu-show-matcher t
820 "Non-nil means show the match string in the agenda dispatcher menu.
821 When nil, the matcher string is not shown, but is put into the help-echo
822 property so than moving the mouse over the command shows it.
823 Setting it to nil is good if matcher strings are very long and/or if
824 you want to use two-column display (see `org-agenda-menu-two-column')."
825 :group 'org-agenda
826 :type 'boolean)
828 (defcustom org-agenda-menu-two-column nil
829 "Non-nil means, use two columns to show custom commands in the dispatcher.
830 If you use this, you probably want to set `org-agenda-menu-show-matcher'
831 to nil."
832 :group 'org-agenda
833 :type 'boolean)
835 (defcustom org-finalize-agenda-hook nil
836 "Hook run just before displaying an agenda buffer."
837 :group 'org-agenda-startup
838 :type 'hook)
840 (defcustom org-agenda-mouse-1-follows-link nil
841 "Non-nil means mouse-1 on a link will follow the link in the agenda.
842 A longer mouse click will still set point. Does not work on XEmacs.
843 Needs to be set before org.el is loaded."
844 :group 'org-agenda-startup
845 :type 'boolean)
847 (defcustom org-agenda-start-with-follow-mode nil
848 "The initial value of follow mode in a newly created agenda window."
849 :group 'org-agenda-startup
850 :type 'boolean)
852 (defcustom org-agenda-show-outline-path t
853 "Non-nil means show outline path in echo area after line motion."
854 :group 'org-agenda-startup
855 :type 'boolean)
857 (defcustom org-agenda-start-with-entry-text-mode nil
858 "The initial value of entry-text-mode in a newly created agenda window."
859 :group 'org-agenda-startup
860 :type 'boolean)
862 (defcustom org-agenda-entry-text-maxlines 5
863 "Number of text lines to be added when `E' is pressed in the agenda.
865 Note that this variable only used during agenda display. Add add entry text
866 when exporting the agenda, configure the variable
867 `org-agenda-add-entry-ext-maxlines'."
868 :group 'org-agenda
869 :type 'integer)
871 (defcustom org-agenda-entry-text-exclude-regexps nil
872 "List of regular expressions to clean up entry text.
873 The complete matches of all regular expressions in this list will be
874 removed from entry text before it is shown in the agenda."
875 :group 'org-agenda
876 :type '(repeat (regexp)))
878 (defvar org-agenda-entry-text-cleanup-hook nil
879 "Hook that is run after basic cleanup of entry text to be shown in agenda.
880 This cleanup is done in a temporary buffer, so the function may inspect and
881 change the entire buffer.
882 Some default stuff like drawers and scheduling/deadline dates will already
883 have been removed when this is called, as will any matches for regular
884 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
886 (defvar org-agenda-include-inactive-timestamps nil
887 "Non-nil means include inactive time stamps in agenda and timeline.")
889 (defgroup org-agenda-windows nil
890 "Options concerning the windows used by the Agenda in Org Mode."
891 :tag "Org Agenda Windows"
892 :group 'org-agenda)
894 (defcustom org-agenda-window-setup 'reorganize-frame
895 "How the agenda buffer should be displayed.
896 Possible values for this option are:
898 current-window Show agenda in the current window, keeping all other windows.
899 other-window Use `switch-to-buffer-other-window' to display agenda.
900 reorganize-frame Show only two windows on the current frame, the current
901 window and the agenda.
902 other-frame Use `switch-to-buffer-other-frame' to display agenda.
903 Also, when exiting the agenda, kill that frame.
904 See also the variable `org-agenda-restore-windows-after-quit'."
905 :group 'org-agenda-windows
906 :type '(choice
907 (const current-window)
908 (const other-frame)
909 (const other-window)
910 (const reorganize-frame)))
912 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
913 "The min and max height of the agenda window as a fraction of frame height.
914 The value of the variable is a cons cell with two numbers between 0 and 1.
915 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
916 :group 'org-agenda-windows
917 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
919 (defcustom org-agenda-restore-windows-after-quit nil
920 "Non-nil means restore window configuration upon exiting agenda.
921 Before the window configuration is changed for displaying the agenda,
922 the current status is recorded. When the agenda is exited with
923 `q' or `x' and this option is set, the old state is restored. If
924 `org-agenda-window-setup' is `other-frame', the value of this
925 option will be ignored."
926 :group 'org-agenda-windows
927 :type 'boolean)
929 (defcustom org-agenda-ndays nil
930 "Number of days to include in overview display.
931 Should be 1 or 7.
932 Obsolete, see `org-agenda-span'."
933 :group 'org-agenda-daily/weekly
934 :type 'integer)
936 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
938 (defcustom org-agenda-span 'week
939 "Number of days to include in overview display.
940 Can be day, week, month, year, or any number of days.
941 Custom commands can set this variable in the options section."
942 :group 'org-agenda-daily/weekly
943 :type '(choice (const :tag "Day" day)
944 (const :tag "Week" week)
945 (const :tag "Month" month)
946 (const :tag "Year" year)
947 (integer :tag "Custom")))
949 (defcustom org-agenda-start-on-weekday 1
950 "Non-nil means start the overview always on the specified weekday.
951 0 denotes Sunday, 1 denotes Monday etc.
952 When nil, always start on the current day.
953 Custom commands can set this variable in the options section."
954 :group 'org-agenda-daily/weekly
955 :type '(choice (const :tag "Today" nil)
956 (integer :tag "Weekday No.")))
958 (defcustom org-agenda-show-all-dates t
959 "Non-nil means `org-agenda' shows every day in the selected range.
960 When nil, only the days which actually have entries are shown."
961 :group 'org-agenda-daily/weekly
962 :type 'boolean)
964 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
965 "Format string for displaying dates in the agenda.
966 Used by the daily/weekly agenda and by the timeline. This should be
967 a format string understood by `format-time-string', or a function returning
968 the formatted date as a string. The function must take a single argument,
969 a calendar-style date list like (month day year)."
970 :group 'org-agenda-daily/weekly
971 :type '(choice
972 (string :tag "Format string")
973 (function :tag "Function")))
975 (defun org-agenda-format-date-aligned (date)
976 "Format a date string for display in the daily/weekly agenda, or timeline.
977 This function makes sure that dates are aligned for easy reading."
978 (require 'cal-iso)
979 (let* ((dayname (calendar-day-name date))
980 (day (cadr date))
981 (day-of-week (calendar-day-of-week date))
982 (month (car date))
983 (monthname (calendar-month-name month))
984 (year (nth 2 date))
985 (iso-week (org-days-to-iso-week
986 (calendar-absolute-from-gregorian date)))
987 (weekyear (cond ((and (= month 1) (>= iso-week 52))
988 (1- year))
989 ((and (= month 12) (<= iso-week 1))
990 (1+ year))
991 (t year)))
992 (weekstring (if (= day-of-week 1)
993 (format " W%02d" iso-week)
994 "")))
995 (format "%-10s %2d %s %4d%s"
996 dayname day monthname year weekstring)))
998 (defcustom org-agenda-time-leading-zero nil
999 "Non-nil means use leading zero for military times in agenda.
1000 For example, 9:30am would become 09:30 rather than 9:30."
1001 :group 'org-agenda-daily/weekly
1002 :type 'boolean)
1004 (defcustom org-agenda-timegrid-use-ampm nil
1005 "When set, show AM/PM style timestamps on the timegrid."
1006 :group 'org-agenda
1007 :type 'boolean)
1009 (defun org-agenda-time-of-day-to-ampm (time)
1010 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1011 (let* ((hour-number (string-to-number (substring time 0 -3)))
1012 (minute (substring time -2))
1013 (ampm "am"))
1014 (cond
1015 ((equal hour-number 12)
1016 (setq ampm "pm"))
1017 ((> hour-number 12)
1018 (setq ampm "pm")
1019 (setq hour-number (- hour-number 12))))
1020 (concat
1021 (if org-agenda-time-leading-zero
1022 (format "%02d" hour-number)
1023 (format "%02s" (number-to-string hour-number)))
1024 ":" minute ampm)))
1026 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1027 "Conditionally convert TIME to AM/PM format
1028 based on `org-agenda-timegrid-use-ampm'"
1029 (if org-agenda-timegrid-use-ampm
1030 (org-agenda-time-of-day-to-ampm time)
1031 time))
1033 (defcustom org-agenda-weekend-days '(6 0)
1034 "Which days are weekend?
1035 These days get the special face `org-agenda-date-weekend' in the agenda
1036 and timeline buffers."
1037 :group 'org-agenda-daily/weekly
1038 :type '(set :greedy t
1039 (const :tag "Monday" 1)
1040 (const :tag "Tuesday" 2)
1041 (const :tag "Wednesday" 3)
1042 (const :tag "Thursday" 4)
1043 (const :tag "Friday" 5)
1044 (const :tag "Saturday" 6)
1045 (const :tag "Sunday" 0)))
1047 (defcustom org-agenda-include-diary nil
1048 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1049 Custom commands can set this variable in the options section."
1050 :group 'org-agenda-daily/weekly
1051 :type 'boolean)
1053 (defcustom org-agenda-include-deadlines t
1054 "If non-nil, include entries within their deadline warning period.
1055 Custom commands can set this variable in the options section."
1056 :group 'org-agenda-daily/weekly
1057 :type 'boolean)
1059 (defcustom org-agenda-include-all-todo nil
1060 "Set means weekly/daily agenda will always contain all TODO entries.
1061 The TODO entries will be listed at the top of the agenda, before
1062 the entries for specific days.
1063 This option is deprecated, it is better to define a block agenda instead."
1064 :group 'org-agenda-daily/weekly
1065 :type 'boolean)
1067 (defcustom org-agenda-repeating-timestamp-show-all t
1068 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1069 When set to a list of strings, only show occurrences of repeating
1070 stamps for these TODO keywords. When nil, only one occurrence is
1071 shown, either today or the nearest into the future."
1072 :group 'org-agenda-daily/weekly
1073 :type '(choice
1074 (const :tag "Show repeating stamps" t)
1075 (repeat :tag "Show repeating stamps for these TODO keywords"
1076 (string :tag "TODO Keyword"))
1077 (const :tag "Don't show repeating stamps" nil)))
1079 (defcustom org-scheduled-past-days 10000
1080 "No. of days to continue listing scheduled items that are not marked DONE.
1081 When an item is scheduled on a date, it shows up in the agenda on this
1082 day and will be listed until it is marked done for the number of days
1083 given here."
1084 :group 'org-agenda-daily/weekly
1085 :type 'integer)
1087 (defcustom org-agenda-log-mode-items '(closed clock)
1088 "List of items that should be shown in agenda log mode.
1089 This list may contain the following symbols:
1091 closed Show entries that have been closed on that day.
1092 clock Show entries that have received clocked time on that day.
1093 state Show all logged state changes.
1094 Note that instead of changing this variable, you can also press `C-u l' in
1095 the agenda to display all available LOG items temporarily."
1096 :group 'org-agenda-daily/weekly
1097 :type '(set :greedy t (const closed) (const clock) (const state)))
1099 (defcustom org-agenda-clock-consistency-checks
1100 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1101 :gap-ok-around ("4:00")
1102 :default-face ((:background "DarkRed") (:foreground "white"))
1103 :overlap-face nil :gap-face nil :no-end-time-face nil
1104 :long-face nil :short-face nil)
1105 "This is a property list, with the following keys:
1107 :max-duration Mark clocking chunks that are longer than this time.
1108 This is a time string like \"HH:MM\", or the number
1109 of minutes as an integer.
1111 :min-duration Mark clocking chunks that are shorter that this.
1112 This is a time string like \"HH:MM\", or the number
1113 of minutes as an integer.
1115 :max-gap Mark gaps between clocking chunks that are longer than
1116 this duration. A number of minutes, or a string
1117 like \"HH:MM\".
1119 :gap-ok-around List of times during the day which are usually not working
1120 times. When a gap is detected, but the gap contains any
1121 of these times, the gap is *not* reported. For example,
1122 if this is (\"4:00\" \"13:00\") then gaps that contain
1123 4:00 in the morning (i.e. the night) and 13:00
1124 (i.e. a typical lunch time) do not cause a warning.
1125 You should have at least one time during the night in this
1126 list, or otherwise the first task each morning will trigger
1127 a warning because it follows a long gap.
1129 Furthermore, the following properties can be used to define faces for
1130 issue display.
1132 :default-face the default face, if the specific face is undefined
1133 :overlap-face face for overlapping clocks
1134 :gap-face face for gaps between clocks
1135 :no-end-time-face face for incomplete clocks
1136 :long-face face for clock intervals that are too long
1137 :short-face face for clock intervals that are too short"
1138 :group 'org-agenda-daily/weekly
1139 :group 'org-clock
1140 :type 'plist)
1142 (defcustom org-agenda-log-mode-add-notes t
1143 "Non-nil means add first line of notes to log entries in agenda views.
1144 If a log item like a state change or a clock entry is associated with
1145 notes, the first line of these notes will be added to the entry in the
1146 agenda display."
1147 :group 'org-agenda-daily/weekly
1148 :type 'boolean)
1150 (defcustom org-agenda-start-with-log-mode nil
1151 "The initial value of log-mode in a newly created agenda window."
1152 :group 'org-agenda-startup
1153 :group 'org-agenda-daily/weekly
1154 :type 'boolean)
1156 (defcustom org-agenda-start-with-clockreport-mode nil
1157 "The initial value of clockreport-mode in a newly created agenda window."
1158 :group 'org-agenda-startup
1159 :group 'org-agenda-daily/weekly
1160 :type 'boolean)
1162 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1163 "Property list with parameters for the clocktable in clockreport mode.
1164 This is the display mode that shows a clock table in the daily/weekly
1165 agenda, the properties for this dynamic block can be set here.
1166 The usual clocktable parameters are allowed here, but you cannot set
1167 the properties :name, :tstart, :tend, :block, and :scope - these will
1168 be overwritten to make sure the content accurately reflects the
1169 current display in the agenda."
1170 :group 'org-agenda-daily/weekly
1171 :type 'plist)
1173 (defcustom org-agenda-search-view-always-boolean nil
1174 "Non-nil means the search string is interpreted as individual parts.
1176 The search string for search view can either be interpreted as a phrase,
1177 or as a list of snippets that define a boolean search for a number of
1178 strings.
1180 When this is non-nil, the string will be split on whitespace, and each
1181 snippet will be searched individually, and all must match in order to
1182 select an entry. A snippet is then a single string of non-white
1183 characters, or a string in double quotes, or a regexp in {} braces.
1184 If a snippet is preceded by \"-\", the snippet must *not* match.
1185 \"+\" is syntactic sugar for positive selection. Each snippet may
1186 be found as a full word or a partial word, but see the variable
1187 `org-agenda-search-view-force-full-words'.
1189 When this is nil, search will look for the entire search phrase as one,
1190 with each space character matching any amount of whitespace, including
1191 line breaks.
1193 Even when this is nil, you can still switch to Boolean search dynamically
1194 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1195 is a regexp marked with braces like \"{abc}\", this will also switch to
1196 boolean search."
1197 :group 'org-agenda-search-view
1198 :type 'boolean)
1200 (if (fboundp 'defvaralias)
1201 (defvaralias 'org-agenda-search-view-search-words-only
1202 'org-agenda-search-view-always-boolean))
1204 (defcustom org-agenda-search-view-force-full-words nil
1205 "Non-nil means, search words must be matches as complete words.
1206 When nil, they may also match part of a word."
1207 :group 'org-agenda-search-view
1208 :type 'boolean)
1210 (defgroup org-agenda-time-grid nil
1211 "Options concerning the time grid in the Org-mode Agenda."
1212 :tag "Org Agenda Time Grid"
1213 :group 'org-agenda)
1215 (defcustom org-agenda-search-headline-for-time t
1216 "Non-nil means search headline for a time-of-day.
1217 If the headline contains a time-of-day in one format or another, it will
1218 be used to sort the entry into the time sequence of items for a day.
1219 Some people have time stamps in the headline that refer to the creation
1220 time or so, and then this produces an unwanted side effect. If this is
1221 the case for your, use this variable to turn off searching the headline
1222 for a time."
1223 :group 'org-agenda-time-grid
1224 :type 'boolean)
1226 (defcustom org-agenda-use-time-grid t
1227 "Non-nil means show a time grid in the agenda schedule.
1228 A time grid is a set of lines for specific times (like every two hours between
1229 8:00 and 20:00). The items scheduled for a day at specific times are
1230 sorted in between these lines.
1231 For details about when the grid will be shown, and what it will look like, see
1232 the variable `org-agenda-time-grid'."
1233 :group 'org-agenda-time-grid
1234 :type 'boolean)
1236 (defcustom org-agenda-time-grid
1237 '((daily today require-timed)
1238 "----------------"
1239 (800 1000 1200 1400 1600 1800 2000))
1241 "The settings for time grid for agenda display.
1242 This is a list of three items. The first item is again a list. It contains
1243 symbols specifying conditions when the grid should be displayed:
1245 daily if the agenda shows a single day
1246 weekly if the agenda shows an entire week
1247 today show grid on current date, independent of daily/weekly display
1248 require-timed show grid only if at least one item has a time specification
1250 The second item is a string which will be placed behind the grid time.
1252 The third item is a list of integers, indicating the times that should have
1253 a grid line."
1254 :group 'org-agenda-time-grid
1255 :type
1256 '(list
1257 (set :greedy t :tag "Grid Display Options"
1258 (const :tag "Show grid in single day agenda display" daily)
1259 (const :tag "Show grid in weekly agenda display" weekly)
1260 (const :tag "Always show grid for today" today)
1261 (const :tag "Show grid only if any timed entries are present"
1262 require-timed)
1263 (const :tag "Skip grid times already present in an entry"
1264 remove-match))
1265 (string :tag "Grid String")
1266 (repeat :tag "Grid Times" (integer :tag "Time"))))
1268 (defcustom org-agenda-show-current-time-in-grid t
1269 "Non-nil means show the current time in the time grid."
1270 :group 'org-agenda-time-grid
1271 :type 'boolean)
1273 (defcustom org-agenda-current-time-string
1274 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1275 "The string for the current time marker in the agenda."
1276 :group 'org-agenda-time-grid
1277 :type 'string)
1279 (defgroup org-agenda-sorting nil
1280 "Options concerning sorting in the Org-mode Agenda."
1281 :tag "Org Agenda Sorting"
1282 :group 'org-agenda)
1284 (defcustom org-agenda-sorting-strategy
1285 '((agenda habit-down time-up priority-down category-keep)
1286 (todo priority-down category-keep)
1287 (tags priority-down category-keep)
1288 (search category-keep))
1289 "Sorting structure for the agenda items of a single day.
1290 This is a list of symbols which will be used in sequence to determine
1291 if an entry should be listed before another entry. The following
1292 symbols are recognized:
1294 time-up Put entries with time-of-day indications first, early first
1295 time-down Put entries with time-of-day indications first, late first
1296 category-keep Keep the default order of categories, corresponding to the
1297 sequence in `org-agenda-files'.
1298 category-up Sort alphabetically by category, A-Z.
1299 category-down Sort alphabetically by category, Z-A.
1300 tag-up Sort alphabetically by last tag, A-Z.
1301 tag-down Sort alphabetically by last tag, Z-A.
1302 priority-up Sort numerically by priority, high priority last.
1303 priority-down Sort numerically by priority, high priority first.
1304 todo-state-up Sort by todo state, tasks that are done last.
1305 todo-state-down Sort by todo state, tasks that are done first.
1306 effort-up Sort numerically by estimated effort, high effort last.
1307 effort-down Sort numerically by estimated effort, high effort first.
1308 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1309 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1310 habit-up Put entries that are habits first
1311 habit-down Put entries that are habits last
1312 alpha-up Sort headlines alphabetically
1313 alpha-down Sort headlines alphabetically, reversed
1315 The different possibilities will be tried in sequence, and testing stops
1316 if one comparison returns a \"not-equal\". For example, the default
1317 '(time-up category-keep priority-down)
1318 means: Pull out all entries having a specified time of day and sort them,
1319 in order to make a time schedule for the current day the first thing in the
1320 agenda listing for the day. Of the entries without a time indication, keep
1321 the grouped in categories, don't sort the categories, but keep them in
1322 the sequence given in `org-agenda-files'. Within each category sort by
1323 priority.
1325 Leaving out `category-keep' would mean that items will be sorted across
1326 categories by priority.
1328 Instead of a single list, this can also be a set of list for specific
1329 contents, with a context symbol in the car of the list, any of
1330 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1332 Custom commands can bind this variable in the options section."
1333 :group 'org-agenda-sorting
1334 :type `(choice
1335 (repeat :tag "General" ,org-sorting-choice)
1336 (list :tag "Individually"
1337 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1338 (repeat ,org-sorting-choice))
1339 (cons (const :tag "Strategy for TODO lists" todo)
1340 (repeat ,org-sorting-choice))
1341 (cons (const :tag "Strategy for Tags matches" tags)
1342 (repeat ,org-sorting-choice))
1343 (cons (const :tag "Strategy for search matches" search)
1344 (repeat ,org-sorting-choice)))))
1346 (defcustom org-agenda-cmp-user-defined nil
1347 "A function to define the comparison `user-defined'.
1348 This function must receive two arguments, agenda entry a and b.
1349 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1350 the user comparison, return nil.
1351 When this is defined, you can make `user-defined-up' and `user-defined-down'
1352 part of an agenda sorting strategy."
1353 :group 'org-agenda-sorting
1354 :type 'symbol)
1356 (defcustom org-sort-agenda-notime-is-late t
1357 "Non-nil means items without time are considered late.
1358 This is only relevant for sorting. When t, items which have no explicit
1359 time like 15:30 will be considered as 99:01, i.e. later than any items which
1360 do have a time. When nil, the default time is before 0:00. You can use this
1361 option to decide if the schedule for today should come before or after timeless
1362 agenda entries."
1363 :group 'org-agenda-sorting
1364 :type 'boolean)
1366 (defcustom org-sort-agenda-noeffort-is-high t
1367 "Non-nil means items without effort estimate are sorted as high effort.
1368 This also applies when filtering an agenda view with respect to the
1369 < or > effort operator. Then, tasks with no effort defined will be treated
1370 as tasks with high effort.
1371 When nil, such items are sorted as 0 minutes effort."
1372 :group 'org-agenda-sorting
1373 :type 'boolean)
1375 (defgroup org-agenda-line-format nil
1376 "Options concerning the entry prefix in the Org-mode agenda display."
1377 :tag "Org Agenda Line Format"
1378 :group 'org-agenda)
1380 (defcustom org-agenda-prefix-format
1381 '((agenda . " %i %-12:c%?-12t% s")
1382 (timeline . " % s")
1383 (todo . " %i %-12:c")
1384 (tags . " %i %-12:c")
1385 (search . " %i %-12:c"))
1386 "Format specifications for the prefix of items in the agenda views.
1387 An alist with four entries, for the different agenda types. The keys to the
1388 sublists are `agenda', `timeline', `todo', `search' and `tags'. The values
1389 are format strings.
1390 This format works similar to a printf format, with the following meaning:
1392 %c the category of the item, \"Diary\" for entries from the diary, or
1393 as given by the CATEGORY keyword or derived from the file name.
1394 %i the icon category of the item, as give in
1395 `org-agenda-category-icon-alist'.
1396 %T the *last* tag of the item. Last because inherited tags come
1397 first in the list.
1398 %t the time-of-day specification if one applies to the entry, in the
1399 format HH:MM
1400 %s Scheduling/Deadline information, a short string
1401 %(expression) Eval EXPRESSION and replace the control string
1402 by the result
1404 All specifiers work basically like the standard `%s' of printf, but may
1405 contain two additional characters: A question mark just after the `%' and
1406 a whitespace/punctuation character just before the final letter.
1408 If the first character after `%' is a question mark, the entire field
1409 will only be included if the corresponding value applies to the
1410 current entry. This is useful for fields which should have fixed
1411 width when present, but zero width when absent. For example,
1412 \"%?-12t\" will result in a 12 character time field if a time of the
1413 day is specified, but will completely disappear in entries which do
1414 not contain a time.
1416 If there is punctuation or whitespace character just before the final
1417 format letter, this character will be appended to the field value if
1418 the value is not empty. For example, the format \"%-12:c\" leads to
1419 \"Diary: \" if the category is \"Diary\". If the category were be
1420 empty, no additional colon would be inserted.
1422 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1423 - Indent the line with two space characters
1424 - Give the category in a 12 chars wide field, padded with whitespace on
1425 the right (because of `-'). Append a colon if there is a category
1426 (because of `:').
1427 - If there is a time-of-day, put it into a 12 chars wide field. If no
1428 time, don't put in an empty field, just skip it (because of '?').
1429 - Finally, put the scheduling information and append a whitespace.
1431 As another example, if you don't want the time-of-day of entries in
1432 the prefix, you could use:
1434 (setq org-agenda-prefix-format \" %-11:c% s\")
1436 See also the variables `org-agenda-remove-times-when-in-prefix' and
1437 `org-agenda-remove-tags'.
1439 Custom commands can set this variable in the options section."
1440 :type '(choice
1441 (string :tag "General format")
1442 (list :greedy t :tag "View dependent"
1443 (cons (const agenda) (string :tag "Format"))
1444 (cons (const timeline) (string :tag "Format"))
1445 (cons (const todo) (string :tag "Format"))
1446 (cons (const tags) (string :tag "Format"))
1447 (cons (const search) (string :tag "Format"))))
1448 :group 'org-agenda-line-format)
1450 (defvar org-prefix-format-compiled nil
1451 "The compiled version of the most recently used prefix format.
1452 See the variable `org-agenda-prefix-format'.")
1454 (defcustom org-agenda-todo-keyword-format "%-1s"
1455 "Format for the TODO keyword in agenda lines.
1456 Set this to something like \"%-12s\" if you want all TODO keywords
1457 to occupy a fixed space in the agenda display."
1458 :group 'org-agenda-line-format
1459 :type 'string)
1461 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1462 "Text preceding timerange entries in the agenda view.
1463 This is a list with two strings. The first applies when the range
1464 is entirely on one day. The second applies if the range spans several days.
1465 The strings may have two \"%d\" format specifiers which will be filled
1466 with the sequence number of the days, and the total number of days in the
1467 range, respectively."
1468 :group 'org-agenda-line-format
1469 :type '(list
1470 (string :tag "Deadline today ")
1471 (choice :tag "Deadline relative"
1472 (string :tag "Format string")
1473 (function))))
1475 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1476 "Text preceding scheduled items in the agenda view.
1477 This is a list with two strings. The first applies when the item is
1478 scheduled on the current day. The second applies when it has been scheduled
1479 previously, it may contain a %d indicating that this is the nth time that
1480 this item is scheduled, due to automatic rescheduling of unfinished items
1481 for the following day. So this number is one larger than the number of days
1482 that passed since this item was scheduled first."
1483 :group 'org-agenda-line-format
1484 :type '(list
1485 (string :tag "Scheduled today ")
1486 (string :tag "Scheduled previously")))
1488 (defcustom org-agenda-inactive-leader "["
1489 "Text preceding item pulled into the agenda by inactive time stamps.
1490 These entries are added to the agenda when pressing \"[\"."
1491 :group 'org-agenda-line-format
1492 :type '(list
1493 (string :tag "Scheduled today ")
1494 (string :tag "Scheduled previously")))
1496 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1497 "Text preceding deadline items in the agenda view.
1498 This is a list with two strings. The first applies when the item has its
1499 deadline on the current day. The second applies when it is in the past or
1500 in the future, it may contain %d to capture how many days away the deadline
1501 is (was)."
1502 :group 'org-agenda-line-format
1503 :type '(list
1504 (string :tag "Deadline today ")
1505 (choice :tag "Deadline relative"
1506 (string :tag "Format string")
1507 (function))))
1509 (defcustom org-agenda-remove-times-when-in-prefix t
1510 "Non-nil means remove duplicate time specifications in agenda items.
1511 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1512 time-of-day specification in a headline or diary entry is extracted and
1513 placed into the prefix. If this option is non-nil, the original specification
1514 \(a timestamp or -range, or just a plain time(range) specification like
1515 11:30-4pm) will be removed for agenda display. This makes the agenda less
1516 cluttered.
1517 The option can be t or nil. It may also be the symbol `beg', indicating
1518 that the time should only be removed when it is located at the beginning of
1519 the headline/diary entry."
1520 :group 'org-agenda-line-format
1521 :type '(choice
1522 (const :tag "Always" t)
1523 (const :tag "Never" nil)
1524 (const :tag "When at beginning of entry" beg)))
1526 (defcustom org-agenda-remove-timeranges-from-blocks nil
1527 "Non-nil means remove time ranges specifications in agenda
1528 items that span on several days."
1529 :group 'org-agenda-line-format
1530 :type 'boolean)
1532 (defcustom org-agenda-default-appointment-duration nil
1533 "Default duration for appointments that only have a starting time.
1534 When nil, no duration is specified in such cases.
1535 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1536 :group 'org-agenda-line-format
1537 :type '(choice
1538 (integer :tag "Minutes")
1539 (const :tag "No default duration")))
1541 (defcustom org-agenda-show-inherited-tags t
1542 "Non-nil means show inherited tags in each agenda line."
1543 :group 'org-agenda-line-format
1544 :type 'boolean)
1546 (defcustom org-agenda-hide-tags-regexp nil
1547 "Regular expression used to filter away specific tags in agenda views.
1548 This means that these tags will be present, but not be shown in the agenda
1549 line. Secondary filtering will still work on the hidden tags.
1550 Nil means don't hide any tags."
1551 :group 'org-agenda-line-format
1552 :type '(choice
1553 (const :tag "Hide none" nil)
1554 (string :tag "Regexp ")))
1556 (defcustom org-agenda-remove-tags nil
1557 "Non-nil means remove the tags from the headline copy in the agenda.
1558 When this is the symbol `prefix', only remove tags when
1559 `org-agenda-prefix-format' contains a `%T' specifier."
1560 :group 'org-agenda-line-format
1561 :type '(choice
1562 (const :tag "Always" t)
1563 (const :tag "Never" nil)
1564 (const :tag "When prefix format contains %T" prefix)))
1566 (if (fboundp 'defvaralias)
1567 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1568 'org-agenda-remove-tags))
1570 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1571 "Shift tags in agenda items to this column.
1572 If this number is positive, it specifies the column. If it is negative,
1573 it means that the tags should be flushright to that column. For example,
1574 -80 works well for a normal 80 character screen."
1575 :group 'org-agenda-line-format
1576 :type 'integer)
1578 (if (fboundp 'defvaralias)
1579 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1581 (defcustom org-agenda-fontify-priorities 'cookies
1582 "Non-nil means highlight low and high priorities in agenda.
1583 When t, the highest priority entries are bold, lowest priority italic.
1584 However, settings in `org-priority-faces' will overrule these faces.
1585 When this variable is the symbol `cookies', only fontify the
1586 cookies, not the entire task.
1587 This may also be an association list of priority faces, whose
1588 keys are the character values of `org-highest-priority',
1589 `org-default-priority', and `org-lowest-priority' (the default values
1590 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1591 color as a string, or a list like `(:background \"Red\")'.
1592 If it is a color, the variable `org-faces-easy-properties'
1593 determines if it is a foreground or a background color."
1594 :group 'org-agenda-line-format
1595 :type '(choice
1596 (const :tag "Never" nil)
1597 (const :tag "Defaults" t)
1598 (const :tag "Cookies only" cookies)
1599 (repeat :tag "Specify"
1600 (list (character :tag "Priority" :value ?A)
1601 (choice :tag "Face "
1602 (string :tag "Color")
1603 (sexp :tag "Face"))))))
1605 (defcustom org-agenda-day-face-function nil
1606 "Function called to determine what face should be used to display a day.
1607 The only argument passed to that function is the day. It should
1608 returns a face, or nil if does not want to specify a face and let
1609 the normal rules apply."
1610 :group 'org-agenda-line-format
1611 :type 'function)
1613 (defcustom org-agenda-category-icon-alist nil
1614 "Alist of category icon to be displayed in agenda views.
1616 Each entry should have the following format:
1618 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1620 Where CATEGORY-REGEXP is a regexp matching the categories where
1621 the icon should be displayed.
1622 FILE-OR-DATA either a file path or a string containing image data.
1624 The other fields can be omited safely if not needed:
1625 TYPE indicates the image type.
1626 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1627 image data.
1628 PROPS are additional image attributes to assign to the image,
1629 like, e.g. `:ascent center'.
1631 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1633 If you want to set the display properties yourself, just put a
1634 list as second element:
1636 (CATEGORY-REGEXP (MY PROPERTY LIST))
1638 For example, to display a 16px horizontal space for Emacs
1639 category, you can use:
1641 (\"Emacs\" '(space . (:width (16))))"
1642 :group 'org-agenda-line-format
1643 :type '(alist :key-type (string :tag "Regexp matching category")
1644 :value-type (choice (list :tag "Icon"
1645 (string :tag "File or data")
1646 (symbol :tag "Type")
1647 (boolean :tag "Data?")
1648 (repeat :tag "Extra image properties" :inline t symbol))
1649 (list :tag "Display properties" sexp))))
1651 (defgroup org-agenda-column-view nil
1652 "Options concerning column view in the agenda."
1653 :tag "Org Agenda Column View"
1654 :group 'org-agenda)
1656 (defcustom org-agenda-columns-show-summaries t
1657 "Non-nil means show summaries for columns displayed in the agenda view."
1658 :group 'org-agenda-column-view
1659 :type 'boolean)
1661 (defcustom org-agenda-columns-compute-summary-properties t
1662 "Non-nil means recompute all summary properties before column view.
1663 When column view in the agenda is listing properties that have a summary
1664 operator, it can go to all relevant buffers and recompute the summaries
1665 there. This can mean overhead for the agenda column view, but is necessary
1666 to have thing up to date.
1667 As a special case, a CLOCKSUM property also makes sure that the clock
1668 computations are current."
1669 :group 'org-agenda-column-view
1670 :type 'boolean)
1672 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1673 "Non-nil means the duration of an appointment will add to day effort.
1674 The property to which appointment durations will be added is the one given
1675 in the option `org-effort-property'. If an appointment does not have
1676 an end time, `org-agenda-default-appointment-duration' will be used. If that
1677 is not set, an appointment without end time will not contribute to the time
1678 estimate."
1679 :group 'org-agenda-column-view
1680 :type 'boolean)
1682 (defcustom org-agenda-auto-exclude-function nil
1683 "A function called with a tag to decide if it is filtered on '/ RET'.
1684 The sole argument to the function, which is called once for each
1685 possible tag, is a string giving the name of the tag. The
1686 function should return either nil if the tag should be included
1687 as normal, or \"-<TAG>\" to exclude the tag.
1688 Note that for the purpose of tag filtering, only the lower-case version of
1689 all tags will be considered, so that this function will only ever see
1690 the lower-case version of all tags."
1691 :group 'org-agenda
1692 :type 'function)
1694 (eval-when-compile
1695 (require 'cl))
1696 (require 'org)
1698 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1699 "Execute BODY with point at location given by `org-hd-marker' property.
1700 If STRING is non-nil, the text property will be fetched from position 0
1701 in that string. If STRING is nil, it will be fetched from the beginning
1702 of the current line."
1703 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1704 'org-hd-marker string)))
1705 (with-current-buffer (marker-buffer marker)
1706 (save-excursion
1707 (goto-char marker)
1708 ,@body))))
1710 (defun org-add-agenda-custom-command (entry)
1711 "Replace or add a command in `org-agenda-custom-commands'.
1712 This is mostly for hacking and trying a new command - once the command
1713 works you probably want to add it to `org-agenda-custom-commands' for good."
1714 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1715 (if ass
1716 (setcdr ass (cdr entry))
1717 (push entry org-agenda-custom-commands))))
1719 ;;; Define the Org-agenda-mode
1721 (defvar org-agenda-mode-map (make-sparse-keymap)
1722 "Keymap for `org-agenda-mode'.")
1723 (if (fboundp 'defvaralias)
1724 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1726 (defvar org-agenda-menu) ; defined later in this file.
1727 (defvar org-agenda-restrict) ; defined later in this file.
1728 (defvar org-agenda-follow-mode nil)
1729 (defvar org-agenda-entry-text-mode nil)
1730 (defvar org-agenda-clockreport-mode nil)
1731 (defvar org-agenda-show-log nil)
1732 (defvar org-agenda-redo-command nil)
1733 (defvar org-agenda-query-string nil)
1734 (defvar org-agenda-mode-hook nil
1735 "Hook for `org-agenda-mode', run after the mode is turned on.")
1736 (defvar org-agenda-type nil)
1737 (defvar org-agenda-force-single-file nil)
1738 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1740 (defun org-agenda-mode ()
1741 "Mode for time-sorted view on action items in Org-mode files.
1743 The following commands are available:
1745 \\{org-agenda-mode-map}"
1746 (interactive)
1747 (kill-all-local-variables)
1748 (setq org-agenda-undo-list nil
1749 org-agenda-pending-undo-list nil
1750 org-agenda-bulk-marked-entries nil)
1751 (setq major-mode 'org-agenda-mode)
1752 ;; Keep global-font-lock-mode from turning on font-lock-mode
1753 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1754 (setq mode-name "Org-Agenda")
1755 (use-local-map org-agenda-mode-map)
1756 (easy-menu-add org-agenda-menu)
1757 (if org-startup-truncated (setq truncate-lines t))
1758 (org-set-local 'line-move-visual nil)
1759 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1760 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1761 ;; Make sure properties are removed when copying text
1762 (when (boundp 'buffer-substring-filters)
1763 (org-set-local 'buffer-substring-filters
1764 (cons (lambda (x)
1765 (set-text-properties 0 (length x) nil x) x)
1766 buffer-substring-filters)))
1767 (unless org-agenda-keep-modes
1768 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1769 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1770 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1771 org-agenda-show-log org-agenda-start-with-log-mode))
1773 (easy-menu-change
1774 '("Agenda") "Agenda Files"
1775 (append
1776 (list
1777 (vector
1778 (if (get 'org-agenda-files 'org-restrict)
1779 "Restricted to single file"
1780 "Edit File List")
1781 '(org-edit-agenda-file-list)
1782 (not (get 'org-agenda-files 'org-restrict)))
1783 "--")
1784 (mapcar 'org-file-menu-entry (org-agenda-files))))
1785 (org-agenda-set-mode-name)
1786 (apply
1787 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1788 (list 'org-agenda-mode-hook)))
1790 (substitute-key-definition 'undo 'org-agenda-undo
1791 org-agenda-mode-map global-map)
1792 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1793 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1794 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1795 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1796 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1797 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1798 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1799 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1800 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1801 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1802 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1803 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1804 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1805 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1806 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1807 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1808 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1809 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1810 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1811 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1812 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1813 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1814 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1815 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1816 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1817 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1818 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1819 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1820 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1821 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1822 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1823 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1824 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1825 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1826 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1827 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1828 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1829 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1830 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1831 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1832 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1833 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1834 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1835 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1836 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1838 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1839 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1840 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1841 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1842 (while l (org-defkey org-agenda-mode-map
1843 (int-to-string (pop l)) 'digit-argument)))
1845 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1846 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1847 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1848 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1849 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1850 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1851 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1852 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1853 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1854 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1855 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1856 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1857 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1858 'org-clock-modify-effort-estimate)
1859 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1860 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1861 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1862 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1863 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1864 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1865 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1866 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1867 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1868 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1869 (substitute-key-definition 'next-line 'org-agenda-next-line
1870 org-agenda-mode-map global-map)
1871 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1872 org-agenda-mode-map global-map)
1873 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1874 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1875 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1876 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1877 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1878 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1879 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1880 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1881 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1882 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1883 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1884 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1885 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1886 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1887 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1888 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1889 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1890 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1891 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1892 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1893 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1894 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1895 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1896 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1897 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1898 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1899 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1900 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1901 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1902 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1904 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1905 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1906 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1907 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1908 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1909 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1910 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1911 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1912 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1913 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1915 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1916 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1917 (when org-agenda-mouse-1-follows-link
1918 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1919 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1920 '("Agenda"
1921 ("Agenda Files")
1922 "--"
1923 ("Agenda Dates"
1924 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1925 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1926 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1927 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1928 "--"
1929 ("View"
1930 ["Day View" org-agenda-day-view
1931 :active (org-agenda-check-type nil 'agenda)
1932 :style radio :selected (eq org-agenda-current-span 'day)
1933 :keys "v d (or just d)"]
1934 ["Week View" org-agenda-week-view
1935 :active (org-agenda-check-type nil 'agenda)
1936 :style radio :selected (eq org-agenda-current-span 'week)
1937 :keys "v w (or just w)"]
1938 ["Month View" org-agenda-month-view
1939 :active (org-agenda-check-type nil 'agenda)
1940 :style radio :selected (eq org-agenda-current-span 'month)
1941 :keys "v m"]
1942 ["Year View" org-agenda-year-view
1943 :active (org-agenda-check-type nil 'agenda)
1944 :style radio :selected (eq org-agenda-current-span 'year)
1945 :keys "v y"]
1946 "--"
1947 ["Include Diary" org-agenda-toggle-diary
1948 :style toggle :selected org-agenda-include-diary
1949 :active (org-agenda-check-type nil 'agenda)]
1950 ["Include Deadlines" org-agenda-toggle-deadlines
1951 :style toggle :selected org-agenda-include-deadlines
1952 :active (org-agenda-check-type nil 'agenda)]
1953 ["Use Time Grid" org-agenda-toggle-time-grid
1954 :style toggle :selected org-agenda-use-time-grid
1955 :active (org-agenda-check-type nil 'agenda)]
1956 "--"
1957 ["Show clock report" org-agenda-clockreport-mode
1958 :style toggle :selected org-agenda-clockreport-mode
1959 :active (org-agenda-check-type nil 'agenda)]
1960 ["Show some entry text" org-agenda-entry-text-mode
1961 :style toggle :selected org-agenda-entry-text-mode
1962 :active t]
1963 "--"
1964 ["Show Logbook entries" org-agenda-log-mode
1965 :style toggle :selected org-agenda-show-log
1966 :active (org-agenda-check-type nil 'agenda 'timeline)
1967 :keys "v l (or just l)"]
1968 ["Include archived trees" org-agenda-archives-mode
1969 :style toggle :selected org-agenda-archives-mode :active t
1970 :keys "v a"]
1971 ["Include archive files" (org-agenda-archives-mode t)
1972 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1973 :keys "v A"]
1974 "--"
1975 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1976 ["Write view to file" org-write-agenda t]
1977 ["Rebuild buffer" org-agenda-redo t]
1978 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1979 "--"
1980 ["Show original entry" org-agenda-show t]
1981 ["Go To (other window)" org-agenda-goto t]
1982 ["Go To (this window)" org-agenda-switch-to t]
1983 ["Follow Mode" org-agenda-follow-mode
1984 :style toggle :selected org-agenda-follow-mode :active t]
1985 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1986 "--"
1987 ("TODO"
1988 ["Cycle TODO" org-agenda-todo t]
1989 ["Next TODO set" org-agenda-todo-nextset t]
1990 ["Previous TODO set" org-agenda-todo-previousset t]
1991 ["Add note" org-agenda-add-note t])
1992 ("Archive/Refile/Delete"
1993 ["Archive default" org-agenda-archive-default t]
1994 ["Archive default" org-agenda-archive-default-with-confirmation t]
1995 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1996 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1997 ["Archive subtree" org-agenda-archive t]
1998 "--"
1999 ["Refile" org-agenda-refile t]
2000 "--"
2001 ["Delete subtree" org-agenda-kill t])
2002 ("Bulk action"
2003 ["Mark entry" org-agenda-bulk-mark t]
2004 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2005 ["Unmark entry" org-agenda-bulk-unmark t]
2006 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2007 ["Act on all marked" org-agenda-bulk-action t]
2008 "--"
2009 ("Tags and Properties"
2010 ["Show all Tags" org-agenda-show-tags t]
2011 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2012 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2013 "--"
2014 ["Column View" org-columns t])
2015 ("Deadline/Schedule"
2016 ["Schedule" org-agenda-schedule t]
2017 ["Set Deadline" org-agenda-deadline t]
2018 "--"
2019 ["Mark item" org-agenda-action :active t :keys "k m"]
2020 ["Show mark item" org-agenda-action :active t :keys "k v"]
2021 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2022 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2023 "--"
2024 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2025 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2026 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2027 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2028 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2029 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2030 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2031 ("Clock and Effort"
2032 ["Clock in" org-agenda-clock-in t]
2033 ["Clock out" org-agenda-clock-out t]
2034 ["Clock cancel" org-agenda-clock-cancel t]
2035 ["Goto running clock" org-clock-goto t]
2036 "--"
2037 ["Set Effort" org-agenda-set-effort t]
2038 ["Change clocked effort" org-clock-modify-effort-estimate
2039 (org-clock-is-active)])
2040 ("Priority"
2041 ["Set Priority" org-agenda-priority t]
2042 ["Increase Priority" org-agenda-priority-up t]
2043 ["Decrease Priority" org-agenda-priority-down t]
2044 ["Show Priority" org-agenda-show-priority t])
2045 ("Calendar/Diary"
2046 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2047 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2048 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2049 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2050 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2051 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2052 "--"
2053 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2054 "--"
2055 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2056 "--"
2057 ("MobileOrg"
2058 ["Push Files and Views" org-mobile-push t]
2059 ["Get Captured and Flagged" org-mobile-pull t]
2060 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2061 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2062 "--"
2063 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2064 "--"
2065 ["Quit" org-agenda-quit t]
2066 ["Exit and Release Buffers" org-agenda-exit t]
2069 ;;; Agenda undo
2071 (defvar org-agenda-allow-remote-undo t
2072 "Non-nil means allow remote undo from the agenda buffer.")
2073 (defvar org-agenda-undo-list nil
2074 "List of undoable operations in the agenda since last refresh.")
2075 (defvar org-agenda-undo-has-started-in nil
2076 "Buffers that have already seen `undo-start' in the current undo sequence.")
2077 (defvar org-agenda-pending-undo-list nil
2078 "In a series of undo commands, this is the list of remaining undo items.")
2080 (defun org-agenda-undo ()
2081 "Undo a remote editing step in the agenda.
2082 This undoes changes both in the agenda buffer and in the remote buffer
2083 that have been changed along."
2084 (interactive)
2085 (or org-agenda-allow-remote-undo
2086 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2087 (if (not (eq this-command last-command))
2088 (setq org-agenda-undo-has-started-in nil
2089 org-agenda-pending-undo-list org-agenda-undo-list))
2090 (if (not org-agenda-pending-undo-list)
2091 (error "No further undo information"))
2092 (let* ((entry (pop org-agenda-pending-undo-list))
2093 buf line cmd rembuf)
2094 (setq cmd (pop entry) line (pop entry))
2095 (setq rembuf (nth 2 entry))
2096 (org-with-remote-undo rembuf
2097 (while (bufferp (setq buf (pop entry)))
2098 (if (pop entry)
2099 (with-current-buffer buf
2100 (let ((last-undo-buffer buf)
2101 (inhibit-read-only t))
2102 (unless (memq buf org-agenda-undo-has-started-in)
2103 (push buf org-agenda-undo-has-started-in)
2104 (make-local-variable 'pending-undo-list)
2105 (undo-start))
2106 (while (and pending-undo-list
2107 (listp pending-undo-list)
2108 (not (car pending-undo-list)))
2109 (pop pending-undo-list))
2110 (undo-more 1))))))
2111 (org-goto-line line)
2112 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2114 (defun org-verify-change-for-undo (l1 l2)
2115 "Verify that a real change occurred between the undo lists L1 and L2."
2116 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2117 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2118 (not (eq l1 l2)))
2120 ;;; Agenda dispatch
2122 (defvar org-agenda-restrict nil)
2123 (defvar org-agenda-restrict-begin (make-marker))
2124 (defvar org-agenda-restrict-end (make-marker))
2125 (defvar org-agenda-last-dispatch-buffer nil)
2126 (defvar org-agenda-overriding-restriction nil)
2128 ;;;###autoload
2129 (defun org-agenda (&optional arg keys restriction)
2130 "Dispatch agenda commands to collect entries to the agenda buffer.
2131 Prompts for a command to execute. Any prefix arg will be passed
2132 on to the selected command. The default selections are:
2134 a Call `org-agenda-list' to display the agenda for current day or week.
2135 t Call `org-todo-list' to display the global todo list.
2136 T Call `org-todo-list' to display the global todo list, select only
2137 entries with a specific TODO keyword (the user gets a prompt).
2138 m Call `org-tags-view' to display headlines with tags matching
2139 a condition (the user is prompted for the condition).
2140 M Like `m', but select only TODO entries, no ordinary headlines.
2141 L Create a timeline for the current buffer.
2142 e Export views to associated files.
2143 s Search entries for keywords.
2144 / Multi occur across all agenda files and also files listed
2145 in `org-agenda-text-search-extra-files'.
2146 < Restrict agenda commands to buffer, subtree, or region.
2147 Press several times to get the desired effect.
2148 > Remove a previous restriction.
2149 # List \"stuck\" projects.
2150 ! Configure what \"stuck\" means.
2151 C Configure custom agenda commands.
2153 More commands can be added by configuring the variable
2154 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2155 searches can be pre-defined in this way.
2157 If the current buffer is in Org-mode and visiting a file, you can also
2158 first press `<' once to indicate that the agenda should be temporarily
2159 \(until the next use of \\[org-agenda]) restricted to the current file.
2160 Pressing `<' twice means to restrict to the current subtree or region
2161 \(if active)."
2162 (interactive "P")
2163 (catch 'exit
2164 (let* ((prefix-descriptions nil)
2165 (org-agenda-window-setup (if (equal (buffer-name)
2166 org-agenda-buffer-name)
2167 'current-window
2168 org-agenda-window-setup))
2169 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2170 (org-agenda-custom-commands
2171 ;; normalize different versions
2172 (delq nil
2173 (mapcar
2174 (lambda (x)
2175 (cond ((stringp (cdr x))
2176 (push x prefix-descriptions)
2177 nil)
2178 ((stringp (nth 1 x)) x)
2179 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2180 (t (cons (car x) (cons "" (cdr x))))))
2181 org-agenda-custom-commands)))
2182 (buf (current-buffer))
2183 (bfn (buffer-file-name (buffer-base-buffer)))
2184 entry key type match lprops ans)
2185 ;; Turn off restriction unless there is an overriding one,
2186 (unless org-agenda-overriding-restriction
2187 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2188 ;; There is a request to keep the file list in place
2189 (put 'org-agenda-files 'org-restrict nil))
2190 (setq org-agenda-restrict nil)
2191 (move-marker org-agenda-restrict-begin nil)
2192 (move-marker org-agenda-restrict-end nil))
2193 ;; Delete old local properties
2194 (put 'org-agenda-redo-command 'org-lprops nil)
2195 ;; Delete previously set last-arguments
2196 (put 'org-agenda-redo-command 'last-args nil)
2197 ;; Remember where this call originated
2198 (setq org-agenda-last-dispatch-buffer (current-buffer))
2199 (unless keys
2200 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2201 keys (car ans)
2202 restriction (cdr ans)))
2203 ;; Establish the restriction, if any
2204 (when (and (not org-agenda-overriding-restriction) restriction)
2205 (put 'org-agenda-files 'org-restrict (list bfn))
2206 (cond
2207 ((eq restriction 'region)
2208 (setq org-agenda-restrict t)
2209 (move-marker org-agenda-restrict-begin (region-beginning))
2210 (move-marker org-agenda-restrict-end (region-end)))
2211 ((eq restriction 'subtree)
2212 (save-excursion
2213 (setq org-agenda-restrict t)
2214 (org-back-to-heading t)
2215 (move-marker org-agenda-restrict-begin (point))
2216 (move-marker org-agenda-restrict-end
2217 (progn (org-end-of-subtree t)))))))
2219 ;; For example the todo list should not need it (but does...)
2220 (cond
2221 ((setq entry (assoc keys org-agenda-custom-commands))
2222 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2223 (progn
2224 (setq type (nth 2 entry) match (eval (nth 3 entry))
2225 lprops (nth 4 entry))
2226 (put 'org-agenda-redo-command 'org-lprops lprops)
2227 (cond
2228 ((eq type 'agenda)
2229 (org-let lprops '(org-agenda-list current-prefix-arg)))
2230 ((eq type 'alltodo)
2231 (org-let lprops '(org-todo-list current-prefix-arg)))
2232 ((eq type 'search)
2233 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2234 ((eq type 'stuck)
2235 (org-let lprops '(org-agenda-list-stuck-projects
2236 current-prefix-arg)))
2237 ((eq type 'tags)
2238 (org-let lprops '(org-tags-view current-prefix-arg match)))
2239 ((eq type 'tags-todo)
2240 (org-let lprops '(org-tags-view '(4) match)))
2241 ((eq type 'todo)
2242 (org-let lprops '(org-todo-list match)))
2243 ((eq type 'tags-tree)
2244 (org-check-for-org-mode)
2245 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2246 ((eq type 'todo-tree)
2247 (org-check-for-org-mode)
2248 (org-let lprops
2249 '(org-occur (concat "^" outline-regexp "[ \t]*"
2250 (regexp-quote match) "\\>"))))
2251 ((eq type 'occur-tree)
2252 (org-check-for-org-mode)
2253 (org-let lprops '(org-occur match)))
2254 ((functionp type)
2255 (org-let lprops '(funcall type match)))
2256 ((fboundp type)
2257 (org-let lprops '(funcall type match)))
2258 (t (error "Invalid custom agenda command type %s" type))))
2259 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2260 ((equal keys "C")
2261 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2262 (customize-variable 'org-agenda-custom-commands))
2263 ((equal keys "a") (call-interactively 'org-agenda-list))
2264 ((equal keys "s") (call-interactively 'org-search-view))
2265 ((equal keys "t") (call-interactively 'org-todo-list))
2266 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2267 ((equal keys "m") (call-interactively 'org-tags-view))
2268 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2269 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2270 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2271 (org-add-hook
2272 'post-command-hook
2273 (lambda ()
2274 (unless (current-message)
2275 (let* ((m (org-agenda-get-any-marker))
2276 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2277 (when note
2278 (message (concat
2279 "FLAGGING-NOTE ([?] for more info): "
2280 (org-add-props
2281 (replace-regexp-in-string
2282 "\\\\n" "//"
2283 (copy-sequence note))
2284 nil 'face 'org-warning)))))))
2285 t t))
2286 ((equal keys "L")
2287 (unless (org-mode-p)
2288 (error "This is not an Org-mode file"))
2289 (unless restriction
2290 (put 'org-agenda-files 'org-restrict (list bfn))
2291 (org-call-with-arg 'org-timeline arg)))
2292 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2293 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2294 ((equal keys "!") (customize-variable 'org-stuck-projects))
2295 (t (error "Invalid agenda key"))))))
2297 (defun org-agenda-append-agenda ()
2298 "Append another agenda view to the current one.
2299 This function allows interactive building of block agendas.
2300 Agenda views are separated by `org-agenda-block-separator'."
2301 (interactive)
2302 (unless (string= (buffer-name) org-agenda-buffer-name)
2303 (error "Can only append from within agenda buffer"))
2304 (let ((org-agenda-multi t))
2305 (org-agenda)
2306 (widen)))
2308 (defun org-agenda-normalize-custom-commands (cmds)
2309 (delq nil
2310 (mapcar
2311 (lambda (x)
2312 (cond ((stringp (cdr x)) nil)
2313 ((stringp (nth 1 x)) x)
2314 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2315 (t (cons (car x) (cons "" (cdr x))))))
2316 cmds)))
2318 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2319 "The user interface for selecting an agenda command."
2320 (catch 'exit
2321 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2322 (restrict-ok (and bfn (org-mode-p)))
2323 (region-p (org-region-active-p))
2324 (custom org-agenda-custom-commands)
2325 (selstring "")
2326 restriction second-time
2327 c entry key type match prefixes rmheader header-end custom1 desc
2328 line lines left right n n1)
2329 (save-window-excursion
2330 (delete-other-windows)
2331 (org-switch-to-buffer-other-window " *Agenda Commands*")
2332 (erase-buffer)
2333 (insert (eval-when-compile
2334 (let ((header
2336 Press key for an agenda command: < Buffer, subtree/region restriction
2337 -------------------------------- > Remove restriction
2338 a Agenda for current week or day e Export agenda views
2339 t List of all TODO entries T Entries with special TODO kwd
2340 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2341 L Timeline for current buffer # List stuck projects (!=configure)
2342 s Search for keywords C Configure custom agenda commands
2343 / Multi-occur ? Find :FLAGGED: entries
2345 (start 0))
2346 (while (string-match
2347 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2348 header start)
2349 (setq start (match-end 0))
2350 (add-text-properties (match-beginning 2) (match-end 2)
2351 '(face bold) header))
2352 header)))
2353 (setq header-end (move-marker (make-marker) (point)))
2354 (while t
2355 (setq custom1 custom)
2356 (when (eq rmheader t)
2357 (org-goto-line 1)
2358 (re-search-forward ":" nil t)
2359 (delete-region (match-end 0) (point-at-eol))
2360 (forward-char 1)
2361 (looking-at "-+")
2362 (delete-region (match-end 0) (point-at-eol))
2363 (move-marker header-end (match-end 0)))
2364 (goto-char header-end)
2365 (delete-region (point) (point-max))
2367 ;; Produce all the lines that describe custom commands and prefixes
2368 (setq lines nil)
2369 (while (setq entry (pop custom1))
2370 (setq key (car entry) desc (nth 1 entry)
2371 type (nth 2 entry)
2372 match (nth 3 entry))
2373 (if (> (length key) 1)
2374 (add-to-list 'prefixes (string-to-char key))
2375 (setq line
2376 (format
2377 "%-4s%-14s"
2378 (org-add-props (copy-sequence key)
2379 '(face bold))
2380 (cond
2381 ((string-match "\\S-" desc) desc)
2382 ((eq type 'agenda) "Agenda for current week or day")
2383 ((eq type 'alltodo) "List of all TODO entries")
2384 ((eq type 'search) "Word search")
2385 ((eq type 'stuck) "List of stuck projects")
2386 ((eq type 'todo) "TODO keyword")
2387 ((eq type 'tags) "Tags query")
2388 ((eq type 'tags-todo) "Tags (TODO)")
2389 ((eq type 'tags-tree) "Tags tree")
2390 ((eq type 'todo-tree) "TODO kwd tree")
2391 ((eq type 'occur-tree) "Occur tree")
2392 ((functionp type) (if (symbolp type)
2393 (symbol-name type)
2394 "Lambda expression"))
2395 (t "???"))))
2396 (if org-agenda-menu-show-matcher
2397 (setq line
2398 (concat line ": "
2399 (cond
2400 ((stringp match)
2401 (setq match (copy-sequence match))
2402 (org-add-props match nil 'face 'org-warning))
2403 (match
2404 (format "set of %d commands" (length match)))
2405 (t ""))))
2406 (if (org-string-nw-p match)
2407 (add-text-properties
2408 0 (length line) (list 'help-echo
2409 (concat "Matcher: "match)) line)))
2410 (push line lines)))
2411 (setq lines (nreverse lines))
2412 (when prefixes
2413 (mapc (lambda (x)
2414 (push
2415 (format "%s %s"
2416 (org-add-props (char-to-string x)
2417 nil 'face 'bold)
2418 (or (cdr (assoc (concat selstring
2419 (char-to-string x))
2420 prefix-descriptions))
2421 "Prefix key"))
2422 lines))
2423 prefixes))
2425 ;; Check if we should display in two columns
2426 (if org-agenda-menu-two-column
2427 (progn
2428 (setq n (length lines)
2429 n1 (+ (/ n 2) (mod n 2))
2430 right (nthcdr n1 lines)
2431 left (copy-sequence lines))
2432 (setcdr (nthcdr (1- n1) left) nil))
2433 (setq left lines right nil))
2434 (while left
2435 (insert "\n" (pop left))
2436 (when right
2437 (if (< (current-column) 40)
2438 (move-to-column 40 t)
2439 (insert " "))
2440 (insert (pop right))))
2442 ;; Make the window the right size
2443 (goto-char (point-min))
2444 (if second-time
2445 (if (not (pos-visible-in-window-p (point-max)))
2446 (org-fit-window-to-buffer))
2447 (setq second-time t)
2448 (org-fit-window-to-buffer))
2450 ;; Ask for selection
2451 (message "Press key for agenda command%s:"
2452 (if (or restrict-ok org-agenda-overriding-restriction)
2453 (if org-agenda-overriding-restriction
2454 " (restriction lock active)"
2455 (if restriction
2456 (format " (restricted to %s)" restriction)
2457 " (unrestricted)"))
2458 ""))
2459 (setq c (read-char-exclusive))
2460 (message "")
2461 (cond
2462 ((assoc (char-to-string c) custom)
2463 (setq selstring (concat selstring (char-to-string c)))
2464 (throw 'exit (cons selstring restriction)))
2465 ((memq c prefixes)
2466 (setq selstring (concat selstring (char-to-string c))
2467 prefixes nil
2468 rmheader (or rmheader t)
2469 custom (delq nil (mapcar
2470 (lambda (x)
2471 (if (or (= (length (car x)) 1)
2472 (/= (string-to-char (car x)) c))
2474 (cons (substring (car x) 1) (cdr x))))
2475 custom))))
2476 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2477 (message "Restriction is only possible in Org-mode buffers")
2478 (ding) (sit-for 1))
2479 ((eq c ?1)
2480 (org-agenda-remove-restriction-lock 'noupdate)
2481 (setq restriction 'buffer))
2482 ((eq c ?0)
2483 (org-agenda-remove-restriction-lock 'noupdate)
2484 (setq restriction (if region-p 'region 'subtree)))
2485 ((eq c ?<)
2486 (org-agenda-remove-restriction-lock 'noupdate)
2487 (setq restriction
2488 (cond
2489 ((eq restriction 'buffer)
2490 (if region-p 'region 'subtree))
2491 ((memq restriction '(subtree region))
2492 nil)
2493 (t 'buffer))))
2494 ((eq c ?>)
2495 (org-agenda-remove-restriction-lock 'noupdate)
2496 (setq restriction nil))
2497 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2498 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2499 ((and (> (length selstring) 0) (eq c ?\d))
2500 (delete-window)
2501 (org-agenda-get-restriction-and-command prefix-descriptions))
2503 ((equal c ?q) (error "Abort"))
2504 (t (error "Invalid key %c" c))))))))
2506 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2507 (defvar org-agenda-last-arguments nil
2508 "The arguments of the previous call to `org-agenda'.")
2509 (defun org-agenda-run-series (name series)
2510 (org-let (nth 1 series) '(org-prepare-agenda name))
2511 (let* ((org-agenda-multi t)
2512 (redo (list 'org-agenda-run-series name (list 'quote series)))
2513 (org-agenda-overriding-arguments
2514 (or org-agenda-overriding-arguments
2515 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2516 (get 'org-agenda-redo-command 'last-args))))
2517 (cmds (car series))
2518 (gprops (nth 1 series))
2519 match ;; The byte compiler incorrectly complains about this. Keep it!
2520 cmd type lprops)
2521 (while (setq cmd (pop cmds))
2522 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2523 (cond
2524 ((eq type 'agenda)
2525 (org-let2 gprops lprops
2526 '(call-interactively 'org-agenda-list)))
2527 ((eq type 'alltodo)
2528 (org-let2 gprops lprops
2529 '(call-interactively 'org-todo-list)))
2530 ((eq type 'search)
2531 (org-let2 gprops lprops
2532 '(org-search-view current-prefix-arg match nil)))
2533 ((eq type 'stuck)
2534 (org-let2 gprops lprops
2535 '(call-interactively 'org-agenda-list-stuck-projects)))
2536 ((eq type 'tags)
2537 (org-let2 gprops lprops
2538 '(org-tags-view current-prefix-arg match)))
2539 ((eq type 'tags-todo)
2540 (org-let2 gprops lprops
2541 '(org-tags-view '(4) match)))
2542 ((eq type 'todo)
2543 (org-let2 gprops lprops
2544 '(org-todo-list match)))
2545 ((fboundp type)
2546 (org-let2 gprops lprops
2547 '(funcall type match)))
2548 (t (error "Invalid type in command series"))))
2549 (widen)
2550 (setq org-agenda-redo-command redo)
2551 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2552 (goto-char (point-min)))
2553 (org-fit-agenda-window)
2554 (org-let (nth 1 series) '(org-finalize-agenda)))
2556 ;;;###autoload
2557 (defmacro org-batch-agenda (cmd-key &rest parameters)
2558 "Run an agenda command in batch mode and send the result to STDOUT.
2559 If CMD-KEY is a string of length 1, it is used as a key in
2560 `org-agenda-custom-commands' and triggers this command. If it is a
2561 longer string it is used as a tags/todo match string.
2562 Parameters are alternating variable names and values that will be bound
2563 before running the agenda command."
2564 (let (pars)
2565 (while parameters
2566 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2567 (if (> (length cmd-key) 2)
2568 (eval (list 'let (nreverse pars)
2569 (list 'org-tags-view nil cmd-key)))
2570 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2571 (set-buffer org-agenda-buffer-name)
2572 (princ (org-encode-for-stdout (buffer-string)))))
2574 ;(defun org-encode-for-stdout (string)
2575 ; (if (fboundp 'encode-coding-string)
2576 ; (encode-coding-string string buffer-file-coding-system)
2577 ; string))
2579 (defun org-encode-for-stdout (string)
2580 string)
2582 (defvar org-agenda-info nil)
2584 ;;;###autoload
2585 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2586 "Run an agenda command in batch mode and send the result to STDOUT.
2587 If CMD-KEY is a string of length 1, it is used as a key in
2588 `org-agenda-custom-commands' and triggers this command. If it is a
2589 longer string it is used as a tags/todo match string.
2590 Parameters are alternating variable names and values that will be bound
2591 before running the agenda command.
2593 The output gives a line for each selected agenda item. Each
2594 item is a list of comma-separated values, like this:
2596 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2598 category The category of the item
2599 head The headline, without TODO kwd, TAGS and PRIORITY
2600 type The type of the agenda entry, can be
2601 todo selected in TODO match
2602 tagsmatch selected in tags match
2603 diary imported from diary
2604 deadline a deadline on given date
2605 scheduled scheduled on given date
2606 timestamp entry has timestamp on given date
2607 closed entry was closed on given date
2608 upcoming-deadline warning about deadline
2609 past-scheduled forwarded scheduled item
2610 block entry has date block including g. date
2611 todo The todo keyword, if any
2612 tags All tags including inherited ones, separated by colons
2613 date The relevant date, like 2007-2-14
2614 time The time, like 15:00-16:50
2615 extra Sting with extra planning info
2616 priority-l The priority letter if any was given
2617 priority-n The computed numerical priority
2618 agenda-day The day in the agenda where this is listed"
2620 (let (pars)
2621 (while parameters
2622 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2623 (push (list 'org-agenda-remove-tags t) pars)
2624 (if (> (length cmd-key) 2)
2625 (eval (list 'let (nreverse pars)
2626 (list 'org-tags-view nil cmd-key)))
2627 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2628 (set-buffer org-agenda-buffer-name)
2629 (let* ((lines (org-split-string (buffer-string) "\n"))
2630 line)
2631 (while (setq line (pop lines))
2632 (catch 'next
2633 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2634 (setq org-agenda-info
2635 (org-fix-agenda-info (text-properties-at 0 line)))
2636 (princ
2637 (org-encode-for-stdout
2638 (mapconcat 'org-agenda-export-csv-mapper
2639 '(org-category txt type todo tags date time extra
2640 priority-letter priority agenda-day)
2641 ",")))
2642 (princ "\n"))))))
2644 (defun org-fix-agenda-info (props)
2645 "Make sure all properties on an agenda item have a canonical form.
2646 This ensures the export commands can easily use it."
2647 (let (tmp re)
2648 (when (setq tmp (plist-get props 'tags))
2649 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2650 (when (setq tmp (plist-get props 'date))
2651 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2652 (let ((calendar-date-display-form '(year "-" month "-" day)))
2653 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2655 (setq tmp (calendar-date-string tmp)))
2656 (setq props (plist-put props 'date tmp)))
2657 (when (setq tmp (plist-get props 'day))
2658 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2659 (let ((calendar-date-display-form '(year "-" month "-" day)))
2660 (setq tmp (calendar-date-string tmp)))
2661 (setq props (plist-put props 'day tmp))
2662 (setq props (plist-put props 'agenda-day tmp)))
2663 (when (setq tmp (plist-get props 'txt))
2664 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2665 (plist-put props 'priority-letter (match-string 1 tmp))
2666 (setq tmp (replace-match "" t t tmp)))
2667 (when (and (setq re (plist-get props 'org-todo-regexp))
2668 (setq re (concat "\\`\\.*" re " ?"))
2669 (string-match re tmp))
2670 (plist-put props 'todo (match-string 1 tmp))
2671 (setq tmp (replace-match "" t t tmp)))
2672 (plist-put props 'txt tmp)))
2673 props)
2675 (defun org-agenda-export-csv-mapper (prop)
2676 (let ((res (plist-get org-agenda-info prop)))
2677 (setq res
2678 (cond
2679 ((not res) "")
2680 ((stringp res) res)
2681 (t (prin1-to-string res))))
2682 (while (string-match "," res)
2683 (setq res (replace-match ";" t t res)))
2684 (org-trim res)))
2687 ;;;###autoload
2688 (defun org-store-agenda-views (&rest parameters)
2689 (interactive)
2690 (eval (list 'org-batch-store-agenda-views)))
2692 ;; FIXME, why is this a macro?????
2693 ;;;###autoload
2694 (defmacro org-batch-store-agenda-views (&rest parameters)
2695 "Run all custom agenda commands that have a file argument."
2696 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2697 (pop-up-frames nil)
2698 (dir default-directory)
2699 pars cmd thiscmdkey files opts cmd-or-set)
2700 (while parameters
2701 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2702 (setq pars (reverse pars))
2703 (save-window-excursion
2704 (while cmds
2705 (setq cmd (pop cmds)
2706 thiscmdkey (car cmd)
2707 cmd-or-set (nth 2 cmd)
2708 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2709 files (nth (if (listp cmd-or-set) 4 5) cmd))
2710 (if (stringp files) (setq files (list files)))
2711 (when files
2712 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2713 (list 'org-agenda nil thiscmdkey)))
2714 (set-buffer org-agenda-buffer-name)
2715 (while files
2716 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2717 (list 'org-write-agenda
2718 (expand-file-name (pop files) dir) nil t))))
2719 (and (get-buffer org-agenda-buffer-name)
2720 (kill-buffer org-agenda-buffer-name)))))))
2722 (defun org-agenda-mark-header-line (pos)
2723 "Mark the line at POS as an agenda structure header."
2724 (save-excursion
2725 (goto-char pos)
2726 (put-text-property (point-at-bol) (point-at-eol)
2727 'org-agenda-structural-header t)
2728 (when org-agenda-title-append
2729 (put-text-property (point-at-bol) (point-at-eol)
2730 'org-agenda-title-append org-agenda-title-append))))
2732 (defvar org-mobile-creating-agendas)
2733 (defun org-write-agenda (file &optional open nosettings)
2734 "Write the current buffer (an agenda view) as a file.
2735 Depending on the extension of the file name, plain text (.txt),
2736 HTML (.html or .htm) or Postscript (.ps) is produced.
2737 If the extension is .ics, run icalendar export over all files used
2738 to construct the agenda and limit the export to entries listed in the
2739 agenda now.
2740 With prefix argument OPEN, open the new file immediately.
2741 If NOSETTINGS is given, do not scope the settings of
2742 `org-agenda-exporter-settings' into the export commands. This is used when
2743 the settings have already been scoped and we do not wish to overrule other,
2744 higher priority settings."
2745 (interactive "FWrite agenda to file: \nP")
2746 (if (not (file-writable-p file))
2747 (error "Cannot write agenda to file %s" file))
2748 (org-let (if nosettings nil org-agenda-exporter-settings)
2749 '(save-excursion
2750 (save-window-excursion
2751 (org-agenda-mark-filtered-text)
2752 (let ((bs (copy-sequence (buffer-string))) beg)
2753 (org-agenda-unmark-filtered-text)
2754 (with-temp-buffer
2755 (rename-buffer "Agenda View" t)
2756 (set-buffer-modified-p nil)
2757 (insert bs)
2758 (org-agenda-remove-marked-text 'org-filtered)
2759 (while (setq beg (text-property-any (point-min) (point-max)
2760 'org-filtered t))
2761 (delete-region
2762 beg (or (next-single-property-change beg 'org-filtered)
2763 (point-max))))
2764 (run-hooks 'org-agenda-before-write-hook)
2765 (cond
2766 ((org-bound-and-true-p org-mobile-creating-agendas)
2767 (org-mobile-write-agenda-for-mobile file))
2768 ((string-match "\\.html?\\'" file)
2769 (require 'htmlize)
2770 (set-buffer (htmlize-buffer (current-buffer)))
2772 (when (and org-agenda-export-html-style
2773 (string-match "<style>" org-agenda-export-html-style))
2774 ;; replace <style> section with org-agenda-export-html-style
2775 (goto-char (point-min))
2776 (kill-region (- (search-forward "<style") 6)
2777 (search-forward "</style>"))
2778 (insert org-agenda-export-html-style))
2779 (write-file file)
2780 (kill-buffer (current-buffer))
2781 (message "HTML written to %s" file))
2782 ((string-match "\\.ps\\'" file)
2783 (require 'ps-print)
2784 (ps-print-buffer-with-faces file)
2785 (message "Postscript written to %s" file))
2786 ((string-match "\\.pdf\\'" file)
2787 (require 'ps-print)
2788 (ps-print-buffer-with-faces
2789 (concat (file-name-sans-extension file) ".ps"))
2790 (call-process "ps2pdf" nil nil nil
2791 (expand-file-name
2792 (concat (file-name-sans-extension file) ".ps"))
2793 (expand-file-name file))
2794 (delete-file (concat (file-name-sans-extension file) ".ps"))
2795 (message "PDF written to %s" file))
2796 ((string-match "\\.ics\\'" file)
2797 (require 'org-icalendar)
2798 (let ((org-agenda-marker-table
2799 (org-create-marker-find-array
2800 (org-agenda-collect-markers)))
2801 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2802 (org-combined-agenda-icalendar-file file))
2803 (apply 'org-export-icalendar 'combine
2804 (org-agenda-files nil 'ifmode))))
2806 (let ((bs (buffer-string)))
2807 (find-file file)
2808 (erase-buffer)
2809 (insert bs)
2810 (save-buffer 0)
2811 (kill-buffer (current-buffer))
2812 (message "Plain text written to %s" file))))))))
2813 (set-buffer org-agenda-buffer-name))
2814 (when open (org-open-file file)))
2816 (defvar org-agenda-filter-overlays nil)
2818 (defun org-agenda-mark-filtered-text ()
2819 "Mark all text hidden by filtering with a text property."
2820 (let ((inhibit-read-only t))
2821 (mapc
2822 (lambda (o)
2823 (when (equal (overlay-buffer o) (current-buffer))
2824 (put-text-property
2825 (overlay-start o) (overlay-end o)
2826 'org-filtered t)))
2827 org-agenda-filter-overlays)))
2829 (defun org-agenda-unmark-filtered-text ()
2830 "Remove the filtering text property."
2831 (let ((inhibit-read-only t))
2832 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2834 (defun org-agenda-remove-marked-text (property &optional value)
2835 "Delete all text marked with VALUE of PROPERTY.
2836 VALUE defaults to t."
2837 (let (beg)
2838 (setq value (or value t))
2839 (while (setq beg (text-property-any (point-min) (point-max)
2840 property value))
2841 (delete-region
2842 beg (or (next-single-property-change beg 'org-filtered)
2843 (point-max))))))
2845 (defun org-agenda-add-entry-text ()
2846 "Add entry text to agenda lines.
2847 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2848 entry text following headings shown in the agenda.
2849 Drawers will be excluded, also the line with scheduling/deadline info."
2850 (when (and (> org-agenda-add-entry-text-maxlines 0)
2851 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2852 (let (m txt)
2853 (goto-char (point-min))
2854 (while (not (eobp))
2855 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2856 (beginning-of-line 2)
2857 (setq txt (org-agenda-get-some-entry-text
2858 m org-agenda-add-entry-text-maxlines " > "))
2859 (end-of-line 1)
2860 (if (string-match "\\S-" txt)
2861 (insert "\n" txt)
2862 (or (eobp) (forward-char 1))))))))
2864 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2865 &rest keep)
2866 "Extract entry text from MARKER, at most N-LINES lines.
2867 This will ignore drawers etc, just get the text.
2868 If INDENT is given, prefix every line with this string. If KEEP is
2869 given, it is a list of symbols, defining stuff that should not be
2870 removed from the entry content. Currently only `planning' is allowed here."
2871 (let (txt drawer-re kwd-time-re ind)
2872 (save-excursion
2873 (with-current-buffer (marker-buffer marker)
2874 (if (not (org-mode-p))
2875 (setq txt "")
2876 (save-excursion
2877 (save-restriction
2878 (widen)
2879 (goto-char marker)
2880 (end-of-line 1)
2881 (setq txt (buffer-substring
2882 (min (1+ (point)) (point-max))
2883 (progn (outline-next-heading) (point)))
2884 drawer-re org-drawer-regexp
2885 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2886 ".*\n?"))
2887 (with-temp-buffer
2888 (insert txt)
2889 (when org-agenda-add-entry-text-descriptive-links
2890 (goto-char (point-min))
2891 (while (org-activate-bracket-links (point-max))
2892 (add-text-properties (match-beginning 0) (match-end 0)
2893 '(face org-link))))
2894 (goto-char (point-min))
2895 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2896 (set-text-properties (match-beginning 0) (match-end 0)
2897 nil))
2898 (goto-char (point-min))
2899 (while (re-search-forward drawer-re nil t)
2900 (delete-region
2901 (match-beginning 0)
2902 (progn (re-search-forward
2903 "^[ \t]*:END:.*\n?" nil 'move)
2904 (point))))
2905 (unless (member 'planning keep)
2906 (goto-char (point-min))
2907 (while (re-search-forward kwd-time-re nil t)
2908 (replace-match "")))
2909 (goto-char (point-min))
2910 (when org-agenda-entry-text-exclude-regexps
2911 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2912 (while (setq re (pop re-list))
2913 (goto-char (point-min))
2914 (while (re-search-forward re nil t)
2915 (replace-match "")))))
2916 (goto-char (point-max))
2917 (skip-chars-backward " \t\n")
2918 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2920 ;; find and remove min common indentation
2921 (goto-char (point-min))
2922 (untabify (point-min) (point-max))
2923 (setq ind (org-get-indentation))
2924 (while (not (eobp))
2925 (unless (looking-at "[ \t]*$")
2926 (setq ind (min ind (org-get-indentation))))
2927 (beginning-of-line 2))
2928 (goto-char (point-min))
2929 (while (not (eobp))
2930 (unless (looking-at "[ \t]*$")
2931 (move-to-column ind)
2932 (delete-region (point-at-bol) (point)))
2933 (beginning-of-line 2))
2935 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2937 (goto-char (point-min))
2938 (when indent
2939 (while (and (not (eobp)) (re-search-forward "^" nil t))
2940 (replace-match indent t t)))
2941 (goto-char (point-min))
2942 (while (looking-at "[ \t]*\n") (replace-match ""))
2943 (goto-char (point-max))
2944 (when (> (org-current-line)
2945 n-lines)
2946 (org-goto-line (1+ n-lines))
2947 (backward-char 1))
2948 (setq txt (buffer-substring (point-min) (point)))))))))
2949 txt))
2951 (defun org-agenda-collect-markers ()
2952 "Collect the markers pointing to entries in the agenda buffer."
2953 (let (m markers)
2954 (save-excursion
2955 (goto-char (point-min))
2956 (while (not (eobp))
2957 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2958 (org-get-at-bol 'org-marker)))
2959 (push m markers))
2960 (beginning-of-line 2)))
2961 (nreverse markers)))
2963 (defun org-create-marker-find-array (marker-list)
2964 "Create a alist of files names with all marker positions in that file."
2965 (let (f tbl m a p)
2966 (while (setq m (pop marker-list))
2967 (setq p (marker-position m)
2968 f (buffer-file-name (or (buffer-base-buffer
2969 (marker-buffer m))
2970 (marker-buffer m))))
2971 (if (setq a (assoc f tbl))
2972 (push (marker-position m) (cdr a))
2973 (push (list f p) tbl)))
2974 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2975 tbl)))
2977 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2978 (defun org-check-agenda-marker-table ()
2979 "Check of the current entry is on the marker list."
2980 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2982 (and (setq a (assoc file org-agenda-marker-table))
2983 (save-match-data
2984 (save-excursion
2985 (org-back-to-heading t)
2986 (member (point) (cdr a)))))))
2988 (defun org-check-for-org-mode ()
2989 "Make sure current buffer is in org-mode. Error if not."
2990 (or (org-mode-p)
2991 (error "Cannot execute org-mode agenda command on buffer in %s"
2992 major-mode)))
2994 (defun org-fit-agenda-window ()
2995 "Fit the window to the buffer size."
2996 (and (memq org-agenda-window-setup '(reorganize-frame))
2997 (fboundp 'fit-window-to-buffer)
2998 (org-fit-window-to-buffer
3000 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3001 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3003 ;;; Agenda prepare and finalize
3005 (defvar org-agenda-multi nil) ; dynamically scoped
3006 (defvar org-agenda-buffer-name "*Org Agenda*")
3007 (defvar org-pre-agenda-window-conf nil)
3008 (defvar org-agenda-columns-active nil)
3009 (defvar org-agenda-name nil)
3010 (defvar org-agenda-filter nil)
3011 (defvar org-agenda-filter-while-redo nil)
3012 (defvar org-agenda-filter-preset nil
3013 "A preset of the tags filter used for secondary agenda filtering.
3014 This must be a list of strings, each string must be a single tag preceded
3015 by \"+\" or \"-\".
3016 This variable should not be set directly, but agenda custom commands can
3017 bind it in the options section. The preset filter is a global property of
3018 the entire agenda view. In a block agenda, it will not work reliably to
3019 define a filter for one of the individual blocks. You need to set it in
3020 the global options and expect it to be applied to the entire view.")
3022 (defun org-prepare-agenda (&optional name)
3023 (setq org-todo-keywords-for-agenda nil)
3024 (setq org-done-keywords-for-agenda nil)
3025 (setq org-drawers-for-agenda nil)
3026 (unless org-agenda-persistent-filter
3027 (setq org-agenda-filter nil))
3028 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3029 (if org-agenda-multi
3030 (progn
3031 (setq buffer-read-only nil)
3032 (goto-char (point-max))
3033 (unless (or (bobp) org-agenda-compact-blocks)
3034 (insert "\n"
3035 (if (stringp org-agenda-block-separator)
3036 org-agenda-block-separator
3037 (make-string (window-width) org-agenda-block-separator))
3038 "\n"))
3039 (narrow-to-region (point) (point-max)))
3040 (org-agenda-reset-markers)
3041 (setq org-agenda-contributing-files nil)
3042 (setq org-agenda-columns-active nil)
3043 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3044 (setq org-todo-keywords-for-agenda
3045 (org-uniquify org-todo-keywords-for-agenda))
3046 (setq org-done-keywords-for-agenda
3047 (org-uniquify org-done-keywords-for-agenda))
3048 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3049 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3050 (awin (get-buffer-window abuf)))
3051 (cond
3052 ((equal (current-buffer) abuf) nil)
3053 (awin (select-window awin))
3054 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3055 ((equal org-agenda-window-setup 'current-window)
3056 (switch-to-buffer abuf))
3057 ((equal org-agenda-window-setup 'other-window)
3058 (org-switch-to-buffer-other-window abuf))
3059 ((equal org-agenda-window-setup 'other-frame)
3060 (switch-to-buffer-other-frame abuf))
3061 ((equal org-agenda-window-setup 'reorganize-frame)
3062 (delete-other-windows)
3063 (org-switch-to-buffer-other-window abuf)))
3064 ;; additional test in case agenda is invoked from within agenda
3065 ;; buffer via elisp link
3066 (unless (equal (current-buffer) abuf)
3067 (switch-to-buffer abuf)))
3068 (setq buffer-read-only nil)
3069 (let ((inhibit-read-only t)) (erase-buffer))
3070 (org-agenda-mode)
3071 (and name (not org-agenda-name)
3072 (org-set-local 'org-agenda-name name)))
3073 (setq buffer-read-only nil))
3075 (defun org-finalize-agenda ()
3076 "Finishing touch for the agenda buffer, called just before displaying it."
3077 (unless org-agenda-multi
3078 (save-excursion
3079 (let ((inhibit-read-only t))
3080 (goto-char (point-min))
3081 (while (org-activate-bracket-links (point-max))
3082 (add-text-properties (match-beginning 0) (match-end 0)
3083 '(face org-link)))
3084 (org-agenda-align-tags)
3085 (unless org-agenda-with-colors
3086 (remove-text-properties (point-min) (point-max) '(face nil))))
3087 (if (and (boundp 'org-agenda-overriding-columns-format)
3088 org-agenda-overriding-columns-format)
3089 (org-set-local 'org-agenda-overriding-columns-format
3090 org-agenda-overriding-columns-format))
3091 (if (and (boundp 'org-agenda-view-columns-initially)
3092 org-agenda-view-columns-initially)
3093 (org-agenda-columns))
3094 (when org-agenda-fontify-priorities
3095 (org-agenda-fontify-priorities))
3096 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3097 (org-agenda-dim-blocked-tasks))
3098 (org-agenda-mark-clocking-task)
3099 (when org-agenda-entry-text-mode
3100 (org-agenda-entry-text-hide)
3101 (org-agenda-entry-text-show))
3102 (if (functionp 'org-habit-insert-consistency-graphs)
3103 (org-habit-insert-consistency-graphs))
3104 (run-hooks 'org-finalize-agenda-hook)
3105 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3106 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3107 (org-agenda-filter-apply org-agenda-filter))
3110 (defun org-agenda-mark-clocking-task ()
3111 "Mark the current clock entry in the agenda if it is present."
3112 (mapc (lambda (o)
3113 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3114 (delete-overlay o)))
3115 (overlays-in (point-min) (point-max)))
3116 (when (marker-buffer org-clock-hd-marker)
3117 (save-excursion
3118 (goto-char (point-min))
3119 (let (s ov)
3120 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3121 (goto-char s)
3122 (when (equal (org-get-at-bol 'org-hd-marker)
3123 org-clock-hd-marker)
3124 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3125 (overlay-put ov 'type 'org-agenda-clocking)
3126 (overlay-put ov 'face 'org-agenda-clocking)
3127 (overlay-put ov 'help-echo
3128 "The clock is running in this item")))))))
3130 (defun org-agenda-fontify-priorities ()
3131 "Make highest priority lines bold, and lowest italic."
3132 (interactive)
3133 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3134 (delete-overlay o)))
3135 (overlays-in (point-min) (point-max)))
3136 (save-excursion
3137 (let ((inhibit-read-only t)
3138 b e p ov h l)
3139 (goto-char (point-min))
3140 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3141 (setq h (or (get-char-property (point) 'org-highest-priority)
3142 org-highest-priority)
3143 l (or (get-char-property (point) 'org-lowest-priority)
3144 org-lowest-priority)
3145 p (string-to-char (match-string 1))
3146 b (match-beginning 0)
3147 e (if (eq org-agenda-fontify-priorities 'cookies)
3148 (match-end 0)
3149 (point-at-eol))
3150 ov (make-overlay b e))
3151 (overlay-put
3152 ov 'face
3153 (cond ((org-face-from-face-or-color
3154 'priority nil
3155 (cdr (assoc p org-priority-faces))))
3156 ((and (listp org-agenda-fontify-priorities)
3157 (org-face-from-face-or-color
3158 'priority nil
3159 (cdr (assoc p org-agenda-fontify-priorities)))))
3160 ((equal p l) 'italic)
3161 ((equal p h) 'bold)))
3162 (overlay-put ov 'org-type 'org-priority)))))
3164 (defun org-agenda-dim-blocked-tasks ()
3165 "Dim currently blocked TODO's in the agenda display."
3166 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3167 (delete-overlay o)))
3168 (overlays-in (point-min) (point-max)))
3169 (save-excursion
3170 (let ((inhibit-read-only t)
3171 (org-depend-tag-blocked nil)
3172 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3173 org-blocked-by-checkboxes
3174 invis1 b e p ov h l)
3175 (goto-char (point-min))
3176 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3177 (and pos (goto-char (1+ pos))))
3178 (setq org-blocked-by-checkboxes nil invis1 invis)
3179 (let ((marker (org-get-at-bol 'org-hd-marker)))
3180 (when (and marker
3181 (not (with-current-buffer (marker-buffer marker)
3182 (save-excursion
3183 (goto-char marker)
3184 (if (org-entry-get nil "NOBLOCKING")
3185 t ;; Never block this entry
3186 (run-hook-with-args-until-failure
3187 'org-blocker-hook
3188 (list :type 'todo-state-change
3189 :position marker
3190 :from 'todo
3191 :to 'done)))))))
3192 (if org-blocked-by-checkboxes (setq invis1 nil))
3193 (setq b (if invis1
3194 (max (point-min) (1- (point-at-bol)))
3195 (point-at-bol))
3196 e (point-at-eol)
3197 ov (make-overlay b e))
3198 (if invis1
3199 (overlay-put ov 'invisible t)
3200 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3201 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3203 (defvar org-agenda-skip-function nil
3204 "Function to be called at each match during agenda construction.
3205 If this function returns nil, the current match should not be skipped.
3206 Otherwise, the function must return a position from where the search
3207 should be continued.
3208 This may also be a Lisp form, it will be evaluated.
3209 Never set this variable using `setq' or so, because then it will apply
3210 to all future agenda commands. If you do want a global skipping condition,
3211 use the option `org-agenda-skip-function-global' instead.
3212 The correct usage for `org-agenda-skip-function' is to bind it with
3213 `let' to scope it dynamically into the agenda-constructing command.
3214 A good way to set it is through options in `org-agenda-custom-commands'.")
3216 (defun org-agenda-skip ()
3217 "Throw to `:skip' in places that should be skipped.
3218 Also moves point to the end of the skipped region, so that search can
3219 continue from there."
3220 (let ((p (point-at-bol)) to)
3221 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3222 (get-text-property p :org-archived)
3223 (org-end-of-subtree t)
3224 (throw :skip t))
3225 (and org-agenda-skip-comment-trees
3226 (get-text-property p :org-comment)
3227 (org-end-of-subtree t)
3228 (throw :skip t))
3229 (if (equal (char-after p) ?#) (throw :skip t))
3230 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3231 (org-agenda-skip-eval org-agenda-skip-function)))
3232 (goto-char to)
3233 (throw :skip t))))
3235 (defun org-agenda-skip-eval (form)
3236 "If FORM is a function or a list, call (or eval) is and return result.
3237 `save-excursion' and `save-match-data' are wrapped around the call, so point
3238 and match data are returned to the previous state no matter what these
3239 functions do."
3240 (let (fp)
3241 (and form
3242 (or (setq fp (functionp form))
3243 (consp form))
3244 (save-excursion
3245 (save-match-data
3246 (if fp
3247 (funcall form)
3248 (eval form)))))))
3250 (defvar org-agenda-markers nil
3251 "List of all currently active markers created by `org-agenda'.")
3252 (defvar org-agenda-last-marker-time (org-float-time)
3253 "Creation time of the last agenda marker.")
3255 (defun org-agenda-new-marker (&optional pos)
3256 "Return a new agenda marker.
3257 Org-mode keeps a list of these markers and resets them when they are
3258 no longer in use."
3259 (let ((m (copy-marker (or pos (point)))))
3260 (setq org-agenda-last-marker-time (org-float-time))
3261 (push m org-agenda-markers)
3264 (defun org-agenda-reset-markers ()
3265 "Reset markers created by `org-agenda'."
3266 (while org-agenda-markers
3267 (move-marker (pop org-agenda-markers) nil)))
3269 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3270 "Save relative positions of markers in region."
3271 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3272 org-agenda-markers))
3274 ;;; Entry text mode
3276 (defun org-agenda-entry-text-show-here ()
3277 "Add some text from the entry as context to the current line."
3278 (let (m txt o)
3279 (setq m (org-get-at-bol 'org-hd-marker))
3280 (unless (marker-buffer m)
3281 (error "No marker points to an entry here"))
3282 (setq txt (concat "\n" (org-no-properties
3283 (org-agenda-get-some-entry-text
3284 m org-agenda-entry-text-maxlines " > "))))
3285 (when (string-match "\\S-" txt)
3286 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3287 (overlay-put o 'evaporate t)
3288 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3289 (overlay-put o 'after-string txt))))
3291 (defun org-agenda-entry-text-show ()
3292 "Add entry context for all agenda lines."
3293 (interactive)
3294 (save-excursion
3295 (goto-char (point-max))
3296 (beginning-of-line 1)
3297 (while (not (bobp))
3298 (when (org-get-at-bol 'org-hd-marker)
3299 (org-agenda-entry-text-show-here))
3300 (beginning-of-line 0))))
3302 (defun org-agenda-entry-text-hide ()
3303 "Remove any shown entry context."
3304 (delq nil
3305 (mapcar (lambda (o)
3306 (if (eq (overlay-get o 'org-overlay-type)
3307 'agenda-entry-content)
3308 (progn (delete-overlay o) t)))
3309 (overlays-in (point-min) (point-max)))))
3311 (defun org-agenda-get-day-face (date)
3312 "Return the face DATE should be displayed with."
3313 (or (and (functionp org-agenda-day-face-function)
3314 (funcall org-agenda-day-face-function date))
3315 (cond ((org-agenda-todayp date)
3316 'org-agenda-date-today)
3317 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3318 'org-agenda-date-weekend)
3319 (t 'org-agenda-date))))
3321 ;;; Agenda timeline
3323 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3325 (defun org-timeline (&optional include-all)
3326 "Show a time-sorted view of the entries in the current org file.
3327 Only entries with a time stamp of today or later will be listed. With
3328 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3329 under the current date.
3330 If the buffer contains an active region, only check the region for
3331 dates."
3332 (interactive "P")
3333 (org-compile-prefix-format 'timeline)
3334 (org-set-sorting-strategy 'timeline)
3335 (let* ((dopast t)
3336 (dotodo include-all)
3337 (doclosed org-agenda-show-log)
3338 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3339 (current-buffer))))
3340 (date (calendar-current-date))
3341 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3342 (end (if (org-region-active-p) (region-end) (point-max)))
3343 (day-numbers (org-get-all-dates beg end 'no-ranges
3344 t doclosed ; always include today
3345 org-timeline-show-empty-dates))
3346 (org-deadline-warning-days 0)
3347 (org-agenda-only-exact-dates t)
3348 (today (org-today))
3349 (past t)
3350 args
3351 s e rtn d emptyp)
3352 (setq org-agenda-redo-command
3353 (list 'progn
3354 (list 'org-switch-to-buffer-other-window (current-buffer))
3355 (list 'org-timeline (list 'quote include-all))))
3356 (if (not dopast)
3357 ;; Remove past dates from the list of dates.
3358 (setq day-numbers (delq nil (mapcar (lambda(x)
3359 (if (>= x today) x nil))
3360 day-numbers))))
3361 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3362 (if doclosed (push :closed args))
3363 (push :timestamp args)
3364 (push :deadline args)
3365 (push :scheduled args)
3366 (push :sexp args)
3367 (if dotodo (push :todo args))
3368 (insert "Timeline of file " entry "\n")
3369 (add-text-properties (point-min) (point)
3370 (list 'face 'org-agenda-structure))
3371 (org-agenda-mark-header-line (point-min))
3372 (while (setq d (pop day-numbers))
3373 (if (and (listp d) (eq (car d) :omitted))
3374 (progn
3375 (setq s (point))
3376 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3377 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3378 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3379 (if (and (>= d today)
3380 dopast
3381 past)
3382 (progn
3383 (setq past nil)
3384 (insert (make-string 79 ?-) "\n")))
3385 (setq date (calendar-gregorian-from-absolute d))
3386 (setq s (point))
3387 (setq rtn (and (not emptyp)
3388 (apply 'org-agenda-get-day-entries entry
3389 date args)))
3390 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3391 (progn
3392 (insert
3393 (if (stringp org-agenda-format-date)
3394 (format-time-string org-agenda-format-date
3395 (org-time-from-absolute date))
3396 (funcall org-agenda-format-date date))
3397 "\n")
3398 (put-text-property s (1- (point)) 'face
3399 (org-agenda-get-day-face date))
3400 (put-text-property s (1- (point)) 'org-date-line t)
3401 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3402 (if (equal d today)
3403 (put-text-property s (1- (point)) 'org-today t))
3404 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3405 (put-text-property s (1- (point)) 'day d)))))
3406 (goto-char (point-min))
3407 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3408 (point-min)))
3409 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3410 (org-finalize-agenda)
3411 (setq buffer-read-only t)))
3413 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3414 "Return a list of all relevant day numbers from BEG to END buffer positions.
3415 If NO-RANGES is non-nil, include only the start and end dates of a range,
3416 not every single day in the range. If FORCE-TODAY is non-nil, make
3417 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3418 inactive time stamps (those in square brackets) are included.
3419 When EMPTY is non-nil, also include days without any entries."
3420 (let ((re (concat
3421 (if pre-re pre-re "")
3422 (if inactive org-ts-regexp-both org-ts-regexp)))
3423 dates dates1 date day day1 day2 ts1 ts2)
3424 (if force-today
3425 (setq dates (list (org-today))))
3426 (save-excursion
3427 (goto-char beg)
3428 (while (re-search-forward re end t)
3429 (setq day (time-to-days (org-time-string-to-time
3430 (substring (match-string 1) 0 10))))
3431 (or (memq day dates) (push day dates)))
3432 (unless no-ranges
3433 (goto-char beg)
3434 (while (re-search-forward org-tr-regexp end t)
3435 (setq ts1 (substring (match-string 1) 0 10)
3436 ts2 (substring (match-string 2) 0 10)
3437 day1 (time-to-days (org-time-string-to-time ts1))
3438 day2 (time-to-days (org-time-string-to-time ts2)))
3439 (while (< (setq day1 (1+ day1)) day2)
3440 (or (memq day1 dates) (push day1 dates)))))
3441 (setq dates (sort dates '<))
3442 (when empty
3443 (while (setq day (pop dates))
3444 (setq day2 (car dates))
3445 (push day dates1)
3446 (when (and day2 empty)
3447 (if (or (eq empty t)
3448 (and (numberp empty) (<= (- day2 day) empty)))
3449 (while (< (setq day (1+ day)) day2)
3450 (push (list day) dates1))
3451 (push (cons :omitted (- day2 day)) dates1))))
3452 (setq dates (nreverse dates1)))
3453 dates)))
3455 ;;; Agenda Daily/Weekly
3457 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3458 "Start day for the agenda view.
3459 Custom commands can set this variable in the options section.")
3460 (defvar org-starting-day nil) ; local variable in the agenda buffer
3461 (defvar org-agenda-current-span nil
3462 "The current span used in the agenda view.") ; local variable in the agenda buffer
3463 (defvar org-include-all-loc nil) ; local variable
3465 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3466 "List of types searched for when creating the daily/weekly agenda.
3467 This variable is a list of symbols that controls the types of
3468 items that appear in the daily/weekly agenda. Allowed symbols in this
3469 list are are
3471 :timestamp List items containing a date stamp or date range matching
3472 the selected date. This includes sexp entries in
3473 angular brackets.
3475 :sexp List entries resulting from plain diary-like sexps.
3477 :deadline List deadline due on that date. When the date is today,
3478 also list any deadlines past due, or due within
3479 `org-deadline-warning-days'. `:deadline' must appear before
3480 `:scheduled' if the setting of
3481 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3482 any effect.
3484 :scheduled List all items which are scheduled for the given date.
3485 The diary for *today* also contains items which were
3486 scheduled earlier and are not yet marked DONE.
3488 By default, all four types are turned on.
3490 Never set this variable globally using `setq', because then it
3491 will apply to all future agenda commands. Instead, bind it with
3492 `let' to scope it dynamically into the agenda-constructing
3493 command. A good way to set it is through options in
3494 `org-agenda-custom-commands'. For a more flexible (though
3495 somewhat less efficient) way of determining what is included in
3496 the daily/weekly agenda, see `org-agenda-skip-function'.")
3498 ;;;###autoload
3499 (defun org-agenda-list (&optional include-all start-day span)
3500 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3501 The view will be for the current day or week, but from the overview buffer
3502 you will be able to go to other days/weeks.
3504 With a numeric prefix argument in an interactive call, the agenda will
3505 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3506 the number of days. SPAN defaults to `org-agenda-span'.
3508 START-DAY defaults to TODAY, or to the most recent match for the weekday
3509 given in `org-agenda-start-on-weekday'."
3510 (interactive "P")
3511 (if (and (integerp include-all) (> include-all 0))
3512 (setq span include-all include-all nil))
3513 (setq start-day (or start-day org-agenda-start-day))
3514 (if org-agenda-overriding-arguments
3515 (setq include-all (car org-agenda-overriding-arguments)
3516 start-day (nth 1 org-agenda-overriding-arguments)
3517 span (nth 2 org-agenda-overriding-arguments)))
3518 (if (stringp start-day)
3519 ;; Convert to an absolute day number
3520 (setq start-day (time-to-days (org-read-date nil t start-day))))
3521 (setq org-agenda-last-arguments (list include-all start-day span))
3522 (org-compile-prefix-format 'agenda)
3523 (org-set-sorting-strategy 'agenda)
3524 (let* ((span (org-agenda-ndays-to-span
3525 (or span org-agenda-ndays org-agenda-span)))
3526 (today (org-today))
3527 (sd (or start-day today))
3528 (ndays (org-agenda-span-to-ndays span sd))
3529 (org-agenda-start-on-weekday
3530 (if (eq ndays 7)
3531 org-agenda-start-on-weekday))
3532 (thefiles (org-agenda-files nil 'ifmode))
3533 (files thefiles)
3534 (start (if (or (null org-agenda-start-on-weekday)
3535 (< ndays 7))
3537 (let* ((nt (calendar-day-of-week
3538 (calendar-gregorian-from-absolute sd)))
3539 (n1 org-agenda-start-on-weekday)
3540 (d (- nt n1)))
3541 (- sd (+ (if (< d 0) 7 0) d)))))
3542 (day-numbers (list start))
3543 (day-cnt 0)
3544 (inhibit-redisplay (not debug-on-error))
3545 s e rtn rtnall file date d start-pos end-pos todayp
3546 clocktable-start clocktable-end filter)
3547 (setq org-agenda-redo-command
3548 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3549 (dotimes (n (1- ndays))
3550 (push (1+ (car day-numbers)) day-numbers))
3551 (setq day-numbers (nreverse day-numbers))
3552 (setq clocktable-start (car day-numbers)
3553 clocktable-end (1+ (or (org-last day-numbers) 0)))
3554 (org-prepare-agenda "Day/Week")
3555 (org-set-local 'org-starting-day (car day-numbers))
3556 (org-set-local 'org-include-all-loc include-all)
3557 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3558 (unless org-agenda-compact-blocks
3559 (let* ((d1 (car day-numbers))
3560 (d2 (org-last day-numbers))
3561 (w1 (org-days-to-iso-week d1))
3562 (w2 (org-days-to-iso-week d2)))
3563 (setq s (point))
3564 (if org-agenda-overriding-header
3565 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3566 nil 'face 'org-agenda-structure) "\n")
3567 (insert (org-agenda-span-name span)
3568 "-agenda"
3569 (if (< (- d2 d1) 350)
3570 (if (= w1 w2)
3571 (format " (W%02d)" w1)
3572 (format " (W%02d-W%02d)" w1 w2))
3574 ":\n")))
3575 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3576 'org-date-line t))
3577 (org-agenda-mark-header-line s))
3578 (while (setq d (pop day-numbers))
3579 (setq date (calendar-gregorian-from-absolute d)
3580 s (point))
3581 (if (or (setq todayp (= d today))
3582 (and (not start-pos) (= d sd)))
3583 (setq start-pos (point))
3584 (if (and start-pos (not end-pos))
3585 (setq end-pos (point))))
3586 (setq files thefiles
3587 rtnall nil)
3588 (while (setq file (pop files))
3589 (catch 'nextfile
3590 (org-check-agenda-file file)
3591 (let ((org-agenda-entry-types org-agenda-entry-types))
3592 (unless org-agenda-include-deadlines
3593 (setq org-agenda-entry-types
3594 (delq :deadline org-agenda-entry-types)))
3595 (cond
3596 ((memq org-agenda-show-log '(only clockcheck))
3597 (setq rtn (org-agenda-get-day-entries
3598 file date :closed)))
3599 (org-agenda-show-log
3600 (setq rtn (apply 'org-agenda-get-day-entries
3601 file date
3602 (append '(:closed) org-agenda-entry-types))))
3604 (setq rtn (apply 'org-agenda-get-day-entries
3605 file date
3606 org-agenda-entry-types)))))
3607 (setq rtnall (append rtnall rtn))))
3608 (if org-agenda-include-diary
3609 (let ((org-agenda-search-headline-for-time t))
3610 (require 'diary-lib)
3611 (setq rtn (org-get-entries-from-diary date))
3612 (setq rtnall (append rtnall rtn))))
3613 (if (or rtnall org-agenda-show-all-dates)
3614 (progn
3615 (setq day-cnt (1+ day-cnt))
3616 (insert
3617 (if (stringp org-agenda-format-date)
3618 (format-time-string org-agenda-format-date
3619 (org-time-from-absolute date))
3620 (funcall org-agenda-format-date date))
3621 "\n")
3622 (put-text-property s (1- (point)) 'face
3623 (org-agenda-get-day-face date))
3624 (put-text-property s (1- (point)) 'org-date-line t)
3625 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3626 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3627 (when todayp
3628 (put-text-property s (1- (point)) 'org-today t))
3629 (if rtnall (insert
3630 (org-finalize-agenda-entries
3631 (org-agenda-add-time-grid-maybe
3632 rtnall ndays todayp))
3633 "\n"))
3634 (put-text-property s (1- (point)) 'day d)
3635 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3636 (when (and org-agenda-clockreport-mode clocktable-start)
3637 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3638 ;; the above line is to ensure the restricted range!
3639 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3640 tbl)
3641 (setq p (org-plist-delete p :block))
3642 (setq p (plist-put p :tstart clocktable-start))
3643 (setq p (plist-put p :tend clocktable-end))
3644 (setq p (plist-put p :scope 'agenda))
3645 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3646 (setq filter (or org-agenda-filter-while-redo
3647 (get 'org-agenda-filter :preset-filter))))
3648 (setq p (plist-put p :tags (mapconcat (lambda (x)
3649 (if (string-match "[<>=]" x)
3652 filter ""))))
3653 (setq tbl (apply 'org-get-clocktable p))
3654 (insert tbl)))
3655 (goto-char (point-min))
3656 (or org-agenda-multi (org-fit-agenda-window))
3657 (unless (and (pos-visible-in-window-p (point-min))
3658 (pos-visible-in-window-p (point-max)))
3659 (goto-char (1- (point-max)))
3660 (recenter -1)
3661 (if (not (pos-visible-in-window-p (or start-pos 1)))
3662 (progn
3663 (goto-char (or start-pos 1))
3664 (recenter 1))))
3665 (goto-char (or start-pos 1))
3666 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3667 (if (eq org-agenda-show-log 'clockcheck)
3668 (org-agenda-show-clocking-issues))
3669 (org-finalize-agenda)
3670 (setq buffer-read-only t)
3671 (message "")))
3673 (defun org-agenda-ndays-to-span (n)
3674 "Return a span symbol for a span of N days, or N if none matches."
3675 (cond ((symbolp n) n)
3676 ((= n 1) 'day)
3677 ((= n 7) 'week)
3678 (t n)))
3680 (defun org-agenda-span-to-ndays (span start-day)
3681 "Return ndays from SPAN starting at START-DAY."
3682 (cond ((numberp span) span)
3683 ((eq span 'day) 1)
3684 ((eq span 'week) 7)
3685 ((eq span 'month)
3686 (let ((date (calendar-gregorian-from-absolute start-day)))
3687 (calendar-last-day-of-month (car date) (caddr date))))
3688 ((eq span 'year)
3689 (let ((date (calendar-gregorian-from-absolute start-day)))
3690 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3692 (defun org-agenda-span-name (span)
3693 "Return a SPAN name."
3694 (if (null span)
3696 (if (symbolp span)
3697 (capitalize (symbol-name span))
3698 (format "%d days" span))))
3700 ;;; Agenda word search
3702 (defvar org-agenda-search-history nil)
3703 (defvar org-todo-only nil)
3705 (defvar org-search-syntax-table nil
3706 "Special syntax table for org-mode search.
3707 In this table, we have single quotes not as word constituents, to
3708 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3710 (defun org-search-syntax-table ()
3711 (unless org-search-syntax-table
3712 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3713 (modify-syntax-entry ?' "." org-search-syntax-table)
3714 (modify-syntax-entry ?` "." org-search-syntax-table))
3715 org-search-syntax-table)
3717 (defvar org-agenda-last-search-view-search-was-boolean nil)
3719 ;;;###autoload
3720 (defun org-search-view (&optional todo-only string edit-at)
3721 "Show all entries that contain a phrase or words or regular expressions.
3723 With optional prefix argument TODO-ONLY, only consider entries that are
3724 TODO entries. The argument STRING can be used to pass a default search
3725 string into this function. If EDIT-AT is non-nil, it means that the
3726 user should get a chance to edit this string, with cursor at position
3727 EDIT-AT.
3729 The search string can be viewed either as a phrase that should be found as
3730 is, or it can be broken into a number of snippets, each of which must match
3731 in a Boolean way to select an entry. The default depends on the variable
3732 `org-agenda-search-view-always-boolean'.
3733 Even if this is turned off (the default) you can always switch to
3734 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3736 The default is a direct search of the whole phrase, where each space in
3737 the search string can expand to an arbitrary amount of whitespace,
3738 including newlines.
3740 If using a Boolean search, the search string is split on whitespace and
3741 each snippet is searched separately, with logical AND to select an entry.
3742 Words prefixed with a minus must *not* occur in the entry. Words without
3743 a prefix or prefixed with a plus must occur in the entry. Matching is
3744 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3745 match whole words, not parts of a word) if
3746 `org-agenda-search-view-force-full-words' is set (default is nil).
3748 Boolean search snippets enclosed by curly braces are interpreted as
3749 regular expressions that must or (when preceded with \"-\") must not
3750 match in the entry. Snippets enclosed into double quotes will be taken
3751 as a whole, to include whitespace.
3753 - If the search string starts with an asterisk, search only in headlines.
3754 - If (possibly after the leading star) the search string starts with an
3755 exclamation mark, this also means to look at TODO entries only, an effect
3756 that can also be achieved with a prefix argument.
3757 - If (possibly after star and exclamation mark) the search string starts
3758 with a colon, this will mean that the (non-regexp) snippets of the
3759 Boolean search must match as full words.
3761 This command searches the agenda files, and in addition the files listed
3762 in `org-agenda-text-search-extra-files'."
3763 (interactive "P")
3764 (org-compile-prefix-format 'search)
3765 (org-set-sorting-strategy 'search)
3766 (org-prepare-agenda "SEARCH")
3767 (let* ((props (list 'face nil
3768 'done-face 'org-agenda-done
3769 'org-not-done-regexp org-not-done-regexp
3770 'org-todo-regexp org-todo-regexp
3771 'org-complex-heading-regexp org-complex-heading-regexp
3772 'mouse-face 'highlight
3773 'help-echo (format "mouse-2 or RET jump to location")))
3774 (full-words org-agenda-search-view-force-full-words)
3775 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3776 regexp rtn rtnall files file pos
3777 marker category tags c neg re boolean
3778 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3779 (unless (and (not edit-at)
3780 (stringp string)
3781 (string-match "\\S-" string))
3782 (setq string (read-string
3783 (if org-agenda-search-view-always-boolean
3784 "[+-]Word/{Regexp} ...: "
3785 "Phrase, or [+-]Word/{Regexp} ...: ")
3786 (cond
3787 ((integerp edit-at) (cons string edit-at))
3788 (edit-at string))
3789 'org-agenda-search-history)))
3790 (org-set-local 'org-todo-only todo-only)
3791 (setq org-agenda-redo-command
3792 (list 'org-search-view (if todo-only t nil) string
3793 '(if current-prefix-arg 1 nil)))
3794 (setq org-agenda-query-string string)
3796 (if (equal (string-to-char string) ?*)
3797 (setq hdl-only t
3798 words (substring string 1))
3799 (setq words string))
3800 (when (equal (string-to-char words) ?!)
3801 (setq todo-only t
3802 words (substring words 1)))
3803 (when (equal (string-to-char words) ?:)
3804 (setq full-words t
3805 words (substring words 1)))
3806 (if (or org-agenda-search-view-always-boolean
3807 (member (string-to-char words) '(?- ?+ ?\{)))
3808 (setq boolean t))
3809 (setq words (org-split-string words))
3810 (let (www w)
3811 (while (setq w (pop words))
3812 (while (and (string-match "\\\\\\'" w) words)
3813 (setq w (concat (substring w 0 -1) " " (pop words))))
3814 (push w www))
3815 (setq words (nreverse www) www nil)
3816 (while (setq w (pop words))
3817 (when (and (string-match "\\`[-+]?{" w)
3818 (not (string-match "}\\'" w)))
3819 (while (and words (not (string-match "}\\'" (car words))))
3820 (setq w (concat w " " (pop words))))
3821 (setq w (concat w " " (pop words))))
3822 (push w www))
3823 (setq words (nreverse www)))
3824 (setq org-agenda-last-search-view-search-was-boolean boolean)
3825 (when boolean
3826 (let (wds w)
3827 (while (setq w (pop words))
3828 (if (or (equal (substring w 0 1) "\"")
3829 (and (> (length w) 1)
3830 (member (substring w 0 1) '("+" "-"))
3831 (equal (substring w 1 2) "\"")))
3832 (while (and words (not (equal (substring w -1) "\"")))
3833 (setq w (concat w " " (pop words)))))
3834 (and (string-match "\\`\\([-+]?\\)\"" w)
3835 (setq w (replace-match "\\1" nil nil w)))
3836 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3837 (push w wds))
3838 (setq words (nreverse wds))))
3839 (if boolean
3840 (mapc (lambda (w)
3841 (setq c (string-to-char w))
3842 (if (equal c ?-)
3843 (setq neg t w (substring w 1))
3844 (if (equal c ?+)
3845 (setq neg nil w (substring w 1))
3846 (setq neg nil)))
3847 (if (string-match "\\`{.*}\\'" w)
3848 (setq re (substring w 1 -1))
3849 (if full-words
3850 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3851 (setq re (regexp-quote (downcase w)))))
3852 (if neg (push re regexps-) (push re regexps+)))
3853 words)
3854 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3855 regexps+))
3856 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3857 (if (not regexps+)
3858 (setq regexp (concat "^" org-outline-regexp))
3859 (setq regexp (pop regexps+))
3860 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3861 regexp))))
3862 (setq files (org-agenda-files nil 'ifmode))
3863 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3864 (pop org-agenda-text-search-extra-files)
3865 (setq files (org-add-archive-files files)))
3866 (setq files (append files org-agenda-text-search-extra-files)
3867 rtnall nil)
3868 (while (setq file (pop files))
3869 (setq ee nil)
3870 (catch 'nextfile
3871 (org-check-agenda-file file)
3872 (setq buffer (if (file-exists-p file)
3873 (org-get-agenda-file-buffer file)
3874 (error "No such file %s" file)))
3875 (if (not buffer)
3876 ;; If file does not exist, make sure an error message is sent
3877 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3878 file))))
3879 (with-current-buffer buffer
3880 (with-syntax-table (org-search-syntax-table)
3881 (unless (org-mode-p)
3882 (error "Agenda file %s is not in `org-mode'" file))
3883 (let ((case-fold-search t))
3884 (save-excursion
3885 (save-restriction
3886 (if org-agenda-restrict
3887 (narrow-to-region org-agenda-restrict-begin
3888 org-agenda-restrict-end)
3889 (widen))
3890 (goto-char (point-min))
3891 (unless (or (org-on-heading-p)
3892 (outline-next-heading))
3893 (throw 'nextfile t))
3894 (goto-char (max (point-min) (1- (point))))
3895 (while (re-search-forward regexp nil t)
3896 (org-back-to-heading t)
3897 (skip-chars-forward "* ")
3898 (setq beg (point-at-bol)
3899 beg1 (point)
3900 end (progn (outline-next-heading) (point)))
3901 (catch :skip
3902 (goto-char beg)
3903 (org-agenda-skip)
3904 (setq str (buffer-substring-no-properties
3905 (point-at-bol)
3906 (if hdl-only (point-at-eol) end)))
3907 (mapc (lambda (wr) (when (string-match wr str)
3908 (goto-char (1- end))
3909 (throw :skip t)))
3910 regexps-)
3911 (mapc (lambda (wr) (unless (string-match wr str)
3912 (goto-char (1- end))
3913 (throw :skip t)))
3914 (if todo-only
3915 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3916 regexps+)
3917 regexps+))
3918 (goto-char beg)
3919 (setq marker (org-agenda-new-marker (point))
3920 category (org-get-category)
3921 tags (org-get-tags-at (point))
3922 txt (org-format-agenda-item
3924 (buffer-substring-no-properties
3925 beg1 (point-at-eol))
3926 category tags))
3927 (org-add-props txt props
3928 'org-marker marker 'org-hd-marker marker
3929 'org-todo-regexp org-todo-regexp
3930 'org-complex-heading-regexp org-complex-heading-regexp
3931 'priority 1000 'org-category category
3932 'type "search")
3933 (push txt ee)
3934 (goto-char (1- end))))))))))
3935 (setq rtn (nreverse ee))
3936 (setq rtnall (append rtnall rtn)))
3937 (if org-agenda-overriding-header
3938 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3939 nil 'face 'org-agenda-structure) "\n")
3940 (insert "Search words: ")
3941 (add-text-properties (point-min) (1- (point))
3942 (list 'face 'org-agenda-structure))
3943 (setq pos (point))
3944 (insert string "\n")
3945 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3946 (setq pos (point))
3947 (unless org-agenda-multi
3948 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3949 (add-text-properties pos (1- (point))
3950 (list 'face 'org-agenda-structure))))
3951 (org-agenda-mark-header-line (point-min))
3952 (when rtnall
3953 (insert (org-finalize-agenda-entries rtnall) "\n"))
3954 (goto-char (point-min))
3955 (or org-agenda-multi (org-fit-agenda-window))
3956 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3957 (org-finalize-agenda)
3958 (setq buffer-read-only t)))
3960 ;;; Agenda TODO list
3962 (defvar org-select-this-todo-keyword nil)
3963 (defvar org-last-arg nil)
3965 ;;;###autoload
3966 (defun org-todo-list (arg)
3967 "Show all (not done) TODO entries from all agenda file in a single list.
3968 The prefix arg can be used to select a specific TODO keyword and limit
3969 the list to these. When using \\[universal-argument], you will be prompted
3970 for a keyword. A numeric prefix directly selects the Nth keyword in
3971 `org-todo-keywords-1'."
3972 (interactive "P")
3973 (org-compile-prefix-format 'todo)
3974 (org-set-sorting-strategy 'todo)
3975 (org-prepare-agenda "TODO")
3976 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3977 (let* ((today (org-today))
3978 (date (calendar-gregorian-from-absolute today))
3979 (kwds org-todo-keywords-for-agenda)
3980 (completion-ignore-case t)
3981 (org-select-this-todo-keyword
3982 (if (stringp arg) arg
3983 (and arg (integerp arg) (> arg 0)
3984 (nth (1- arg) kwds))))
3985 rtn rtnall files file pos)
3986 (when (equal arg '(4))
3987 (setq org-select-this-todo-keyword
3988 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3989 (mapcar 'list kwds) nil nil)))
3990 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3991 (org-set-local 'org-last-arg arg)
3992 (setq org-agenda-redo-command
3993 '(org-todo-list (or current-prefix-arg org-last-arg)))
3994 (setq files (org-agenda-files nil 'ifmode)
3995 rtnall nil)
3996 (while (setq file (pop files))
3997 (catch 'nextfile
3998 (org-check-agenda-file file)
3999 (setq rtn (org-agenda-get-day-entries file date :todo))
4000 (setq rtnall (append rtnall rtn))))
4001 (if org-agenda-overriding-header
4002 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4003 nil 'face 'org-agenda-structure) "\n")
4004 (insert "Global list of TODO items of type: ")
4005 (add-text-properties (point-min) (1- (point))
4006 (list 'face 'org-agenda-structure
4007 'short-heading
4008 (concat "ToDo: "
4009 (or org-select-this-todo-keyword "ALL"))))
4010 (org-agenda-mark-header-line (point-min))
4011 (setq pos (point))
4012 (insert (or org-select-this-todo-keyword "ALL") "\n")
4013 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4014 (setq pos (point))
4015 (unless org-agenda-multi
4016 (insert "Available with `N r': (0)ALL")
4017 (let ((n 0) s)
4018 (mapc (lambda (x)
4019 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4020 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4021 (insert "\n "))
4022 (insert " " s))
4023 kwds))
4024 (insert "\n"))
4025 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4026 (org-agenda-mark-header-line (point-min))
4027 (when rtnall
4028 (insert (org-finalize-agenda-entries rtnall) "\n"))
4029 (goto-char (point-min))
4030 (or org-agenda-multi (org-fit-agenda-window))
4031 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4032 (org-finalize-agenda)
4033 (setq buffer-read-only t)))
4035 ;;; Agenda tags match
4037 ;;;###autoload
4038 (defun org-tags-view (&optional todo-only match)
4039 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4040 The prefix arg TODO-ONLY limits the search to TODO entries."
4041 (interactive "P")
4042 (org-compile-prefix-format 'tags)
4043 (org-set-sorting-strategy 'tags)
4044 (let* ((org-tags-match-list-sublevels
4045 org-tags-match-list-sublevels)
4046 (completion-ignore-case t)
4047 rtn rtnall files file pos matcher
4048 buffer)
4049 (when (and (stringp match) (not (string-match "\\S-" match)))
4050 (setq match nil))
4051 (setq matcher (org-make-tags-matcher match)
4052 match (car matcher) matcher (cdr matcher))
4053 (org-prepare-agenda (concat "TAGS " match))
4054 (setq org-agenda-query-string match)
4055 (setq org-agenda-redo-command
4056 (list 'org-tags-view (list 'quote todo-only)
4057 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4058 (setq files (org-agenda-files nil 'ifmode)
4059 rtnall nil)
4060 (while (setq file (pop files))
4061 (catch 'nextfile
4062 (org-check-agenda-file file)
4063 (setq buffer (if (file-exists-p file)
4064 (org-get-agenda-file-buffer file)
4065 (error "No such file %s" file)))
4066 (if (not buffer)
4067 ;; If file does not exist, error message to agenda
4068 (setq rtn (list
4069 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4070 rtnall (append rtnall rtn))
4071 (with-current-buffer buffer
4072 (unless (org-mode-p)
4073 (error "Agenda file %s is not in `org-mode'" file))
4074 (save-excursion
4075 (save-restriction
4076 (if org-agenda-restrict
4077 (narrow-to-region org-agenda-restrict-begin
4078 org-agenda-restrict-end)
4079 (widen))
4080 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4081 (setq rtnall (append rtnall rtn))))))))
4082 (if org-agenda-overriding-header
4083 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4084 nil 'face 'org-agenda-structure) "\n")
4085 (insert "Headlines with TAGS match: ")
4086 (add-text-properties (point-min) (1- (point))
4087 (list 'face 'org-agenda-structure
4088 'short-heading
4089 (concat "Match: " match)))
4090 (setq pos (point))
4091 (insert match "\n")
4092 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4093 (setq pos (point))
4094 (unless org-agenda-multi
4095 (insert "Press `C-u r' to search again with new search string\n"))
4096 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4097 (org-agenda-mark-header-line (point-min))
4098 (when rtnall
4099 (insert (org-finalize-agenda-entries rtnall) "\n"))
4100 (goto-char (point-min))
4101 (or org-agenda-multi (org-fit-agenda-window))
4102 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4103 (org-finalize-agenda)
4104 (setq buffer-read-only t)))
4106 ;;; Agenda Finding stuck projects
4108 (defvar org-agenda-skip-regexp nil
4109 "Regular expression used in skipping subtrees for the agenda.
4110 This is basically a temporary global variable that can be set and then
4111 used by user-defined selections using `org-agenda-skip-function'.")
4113 (defvar org-agenda-overriding-header nil
4114 "When this is set during todo and tags searches, will replace header.
4115 This variable should not be set directly, but custom commands can bind it
4116 in the options section.")
4118 (defun org-agenda-skip-entry-when-regexp-matches ()
4119 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4120 If yes, it returns the end position of this entry, causing agenda commands
4121 to skip the entry but continuing the search in the subtree. This is a
4122 function that can be put into `org-agenda-skip-function' for the duration
4123 of a command."
4124 (let ((end (save-excursion (org-end-of-subtree t)))
4125 skip)
4126 (save-excursion
4127 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4128 (and skip end)))
4130 (defun org-agenda-skip-subtree-when-regexp-matches ()
4131 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4132 If yes, it returns the end position of this tree, causing agenda commands
4133 to skip this subtree. This is a function that can be put into
4134 `org-agenda-skip-function' for the duration of a command."
4135 (let ((end (save-excursion (org-end-of-subtree t)))
4136 skip)
4137 (save-excursion
4138 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4139 (and skip end)))
4141 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4142 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4143 If yes, it returns the end position of the current entry (NOT the tree),
4144 causing agenda commands to skip the entry but continuing the search in
4145 the subtree. This is a function that can be put into
4146 `org-agenda-skip-function' for the duration of a command. An important
4147 use of this function is for the stuck project list."
4148 (let ((end (save-excursion (org-end-of-subtree t)))
4149 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4150 skip)
4151 (save-excursion
4152 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4153 (and skip entry-end)))
4155 (defun org-agenda-skip-entry-if (&rest conditions)
4156 "Skip entry if any of CONDITIONS is true.
4157 See `org-agenda-skip-if' for details."
4158 (org-agenda-skip-if nil conditions))
4160 (defun org-agenda-skip-subtree-if (&rest conditions)
4161 "Skip entry if any of CONDITIONS is true.
4162 See `org-agenda-skip-if' for details."
4163 (org-agenda-skip-if t conditions))
4165 (defun org-agenda-skip-if (subtree conditions)
4166 "Checks current entity for CONDITIONS.
4167 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4168 the entry, i.e. the text before the next heading is checked.
4170 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4171 from different tests. Valid conditions are:
4173 scheduled Check if there is a scheduled cookie
4174 notscheduled Check if there is no scheduled cookie
4175 deadline Check if there is a deadline
4176 notdeadline Check if there is no deadline
4177 timestamp Check if there is a timestamp (also deadline or scheduled)
4178 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4179 regexp Check if regexp matches
4180 notregexp Check if regexp does not match.
4181 todo Check if TODO keyword matches
4182 nottodo Check if TODO keyword does not match
4184 The regexp is taken from the conditions list, it must come right after
4185 the `regexp' or `notregexp' element.
4187 `todo' and `nottodo' accept as an argument a list of todo
4188 keywords, which may include \"*\" to match any todo keyword.
4190 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4192 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4194 Instead of a list a keyword class may be given
4196 (org-agenda-skip-entry-if 'nottodo 'done)
4198 would skip entries that haven't been marked with any of \"DONE\"
4199 keywords. Possible classes are: `todo', `done', `any'.
4201 If any of these conditions is met, this function returns the end point of
4202 the entity, causing the search to continue from there. This is a function
4203 that can be put into `org-agenda-skip-function' for the duration of a command."
4204 (let (beg end m)
4205 (org-back-to-heading t)
4206 (setq beg (point)
4207 end (if subtree
4208 (progn (org-end-of-subtree t) (point))
4209 (progn (outline-next-heading) (1- (point)))))
4210 (goto-char beg)
4211 (and
4213 (and (memq 'scheduled conditions)
4214 (re-search-forward org-scheduled-time-regexp end t))
4215 (and (memq 'notscheduled conditions)
4216 (not (re-search-forward org-scheduled-time-regexp end t)))
4217 (and (memq 'deadline conditions)
4218 (re-search-forward org-deadline-time-regexp end t))
4219 (and (memq 'notdeadline conditions)
4220 (not (re-search-forward org-deadline-time-regexp end t)))
4221 (and (memq 'timestamp conditions)
4222 (re-search-forward org-ts-regexp end t))
4223 (and (memq 'nottimestamp conditions)
4224 (not (re-search-forward org-ts-regexp end t)))
4225 (and (setq m (memq 'regexp conditions))
4226 (stringp (nth 1 m))
4227 (re-search-forward (nth 1 m) end t))
4228 (and (setq m (memq 'notregexp conditions))
4229 (stringp (nth 1 m))
4230 (not (re-search-forward (nth 1 m) end t)))
4231 (and (or
4232 (setq m (memq 'todo conditions))
4233 (setq m (memq 'nottodo conditions)))
4234 (org-agenda-skip-if-todo m end)))
4235 end)))
4237 (defun org-agenda-skip-if-todo (args end)
4238 "Helper function for `org-agenda-skip-if', do not use it directly.
4239 ARGS is a list with first element either `todo' or `nottodo'.
4240 The remainder is either a list of TODO keywords, or a state symbol
4241 `todo' or `done' or `any'."
4242 (let ((kw (car args))
4243 (arg (cadr args))
4244 todo-wds todo-re)
4245 (setq todo-wds
4246 (org-uniquify
4247 (cond
4248 ((listp arg) ;; list of keywords
4249 (if (member "*" arg)
4250 (mapcar 'substring-no-properties org-todo-keywords-1)
4251 arg))
4252 ((symbolp arg) ;; keyword class name
4253 (cond
4254 ((eq arg 'todo)
4255 (org-delete-all org-done-keywords
4256 (mapcar 'substring-no-properties
4257 org-todo-keywords-1)))
4258 ((eq arg 'done) org-done-keywords)
4259 ((eq arg 'any)
4260 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4261 (setq todo-re
4262 (concat "^\\*+[ \t]+\\<\\("
4263 (mapconcat 'identity todo-wds "\\|")
4264 "\\)\\>"))
4265 (if (eq kw 'todo)
4266 (re-search-forward todo-re end t)
4267 (not (re-search-forward todo-re end t)))))
4269 ;;;###autoload
4270 (defun org-agenda-list-stuck-projects (&rest ignore)
4271 "Create agenda view for projects that are stuck.
4272 Stuck projects are project that have no next actions. For the definitions
4273 of what a project is and how to check if it stuck, customize the variable
4274 `org-stuck-projects'."
4275 (interactive)
4276 (let* ((org-agenda-skip-function
4277 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4278 ;; We could have used org-agenda-skip-if here.
4279 (org-agenda-overriding-header
4280 (or org-agenda-overriding-header "List of stuck projects: "))
4281 (matcher (nth 0 org-stuck-projects))
4282 (todo (nth 1 org-stuck-projects))
4283 (todo-wds (if (member "*" todo)
4284 (progn
4285 (org-prepare-agenda-buffers (org-agenda-files
4286 nil 'ifmode))
4287 (org-delete-all
4288 org-done-keywords-for-agenda
4289 (copy-sequence org-todo-keywords-for-agenda)))
4290 todo))
4291 (todo-re (concat "^\\*+[ \t]+\\("
4292 (mapconcat 'identity todo-wds "\\|")
4293 "\\)\\>"))
4294 (tags (nth 2 org-stuck-projects))
4295 (tags-re (if (member "*" tags)
4296 (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
4297 (if tags
4298 (concat "^\\*+ .*:\\("
4299 (mapconcat 'identity tags "\\|")
4300 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4301 (gen-re (nth 3 org-stuck-projects))
4302 (re-list
4303 (delq nil
4304 (list
4305 (if todo todo-re)
4306 (if tags tags-re)
4307 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4308 gen-re)))))
4309 (setq org-agenda-skip-regexp
4310 (if re-list
4311 (mapconcat 'identity re-list "\\|")
4312 (error "No information how to identify unstuck projects")))
4313 (org-tags-view nil matcher)
4314 (with-current-buffer org-agenda-buffer-name
4315 (setq org-agenda-redo-command
4316 '(org-agenda-list-stuck-projects
4317 (or current-prefix-arg org-last-arg))))))
4319 ;;; Diary integration
4321 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4322 (defvar list-diary-entries-hook)
4323 (defvar diary-time-regexp)
4324 (defun org-get-entries-from-diary (date)
4325 "Get the (Emacs Calendar) diary entries for DATE."
4326 (require 'diary-lib)
4327 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4328 (diary-display-hook '(fancy-diary-display))
4329 (diary-display-function 'fancy-diary-display)
4330 (pop-up-frames nil)
4331 (list-diary-entries-hook
4332 (cons 'org-diary-default-entry list-diary-entries-hook))
4333 (diary-file-name-prefix-function nil) ; turn this feature off
4334 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4335 entries
4336 (org-disable-agenda-to-diary t))
4337 (save-excursion
4338 (save-window-excursion
4339 (funcall (if (fboundp 'diary-list-entries)
4340 'diary-list-entries 'list-diary-entries)
4341 date 1)))
4342 (if (not (get-buffer diary-fancy-buffer))
4343 (setq entries nil)
4344 (with-current-buffer diary-fancy-buffer
4345 (setq buffer-read-only nil)
4346 (if (zerop (buffer-size))
4347 ;; No entries
4348 (setq entries nil)
4349 ;; Omit the date and other unnecessary stuff
4350 (org-agenda-cleanup-fancy-diary)
4351 ;; Add prefix to each line and extend the text properties
4352 (if (zerop (buffer-size))
4353 (setq entries nil)
4354 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4355 (setq entries
4356 (with-temp-buffer
4357 (insert entries) (goto-char (point-min))
4358 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4359 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4360 (replace-match (concat "; " (match-string 1)))))
4361 (buffer-string)))))
4362 (set-buffer-modified-p nil)
4363 (kill-buffer diary-fancy-buffer)))
4364 (when entries
4365 (setq entries (org-split-string entries "\n"))
4366 (setq entries
4367 (mapcar
4368 (lambda (x)
4369 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4370 ;; Extend the text properties to the beginning of the line
4371 (org-add-props x (text-properties-at (1- (length x)) x)
4372 'type "diary" 'date date 'face 'org-agenda-diary))
4373 entries)))))
4375 (defvar org-agenda-cleanup-fancy-diary-hook nil
4376 "Hook run when the fancy diary buffer is cleaned up.")
4378 (defun org-agenda-cleanup-fancy-diary ()
4379 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4380 This gets rid of the date, the underline under the date, and
4381 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4382 date. It also removes lines that contain only whitespace."
4383 (goto-char (point-min))
4384 (if (looking-at ".*?:[ \t]*")
4385 (progn
4386 (replace-match "")
4387 (re-search-forward "\n=+$" nil t)
4388 (replace-match "")
4389 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4390 (re-search-forward "\n=+$" nil t)
4391 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4392 (goto-char (point-min))
4393 (while (re-search-forward "^ +\n" nil t)
4394 (replace-match ""))
4395 (goto-char (point-min))
4396 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4397 (replace-match ""))
4398 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4400 ;; Make sure entries from the diary have the right text properties.
4401 (eval-after-load "diary-lib"
4402 '(if (boundp 'diary-modify-entry-list-string-function)
4403 ;; We can rely on the hook, nothing to do
4405 ;; Hook not available, must use advice to make this work
4406 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4407 "Make the position visible."
4408 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4409 (stringp string)
4410 buffer-file-name)
4411 (setq string (org-modify-diary-entry-string string))))))
4413 (defun org-modify-diary-entry-string (string)
4414 "Add text properties to string, allowing org-mode to act on it."
4415 (org-add-props string nil
4416 'mouse-face 'highlight
4417 'help-echo (if buffer-file-name
4418 (format "mouse-2 or RET jump to diary file %s"
4419 (abbreviate-file-name buffer-file-name))
4421 'org-agenda-diary-link t
4422 'org-marker (org-agenda-new-marker (point-at-bol))))
4424 (defun org-diary-default-entry ()
4425 "Add a dummy entry to the diary.
4426 Needed to avoid empty dates which mess up holiday display."
4427 ;; Catch the error if dealing with the new add-to-diary-alist
4428 (when org-disable-agenda-to-diary
4429 (condition-case nil
4430 (org-add-to-diary-list original-date "Org-mode dummy" "")
4431 (error
4432 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4434 (defun org-add-to-diary-list (&rest args)
4435 (if (fboundp 'diary-add-to-list)
4436 (apply 'diary-add-to-list args)
4437 (apply 'add-to-diary-list args)))
4439 (defvar org-diary-last-run-time nil)
4441 ;;;###autoload
4442 (defun org-diary (&rest args)
4443 "Return diary information from org-files.
4444 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4445 It accesses org files and extracts information from those files to be
4446 listed in the diary. The function accepts arguments specifying what
4447 items should be listed. For a list of arguments allowed here, see the
4448 variable `org-agenda-entry-types'.
4450 The call in the diary file should look like this:
4452 &%%(org-diary) ~/path/to/some/orgfile.org
4454 Use a separate line for each org file to check. Or, if you omit the file name,
4455 all files listed in `org-agenda-files' will be checked automatically:
4457 &%%(org-diary)
4459 If you don't give any arguments (as in the example above), the default
4460 arguments (:deadline :scheduled :timestamp :sexp) are used.
4461 So the example above may also be written as
4463 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4465 The function expects the lisp variables `entry' and `date' to be provided
4466 by the caller, because this is how the calendar works. Don't use this
4467 function from a program - use `org-agenda-get-day-entries' instead."
4468 (when (> (- (org-float-time)
4469 org-agenda-last-marker-time)
4471 (org-agenda-reset-markers))
4472 (org-compile-prefix-format 'agenda)
4473 (org-set-sorting-strategy 'agenda)
4474 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4475 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4476 (list entry)
4477 (org-agenda-files t)))
4478 (time (org-float-time))
4479 file rtn results)
4480 (when (or (not org-diary-last-run-time)
4481 (> (- time
4482 org-diary-last-run-time)
4484 (org-prepare-agenda-buffers files))
4485 (setq org-diary-last-run-time time)
4486 ;; If this is called during org-agenda, don't return any entries to
4487 ;; the calendar. Org Agenda will list these entries itself.
4488 (if org-disable-agenda-to-diary (setq files nil))
4489 (while (setq file (pop files))
4490 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4491 (setq results (append results rtn)))
4492 (if results
4493 (concat (org-finalize-agenda-entries results) "\n"))))
4495 ;;; Agenda entry finders
4497 (defun org-agenda-get-day-entries (file date &rest args)
4498 "Does the work for `org-diary' and `org-agenda'.
4499 FILE is the path to a file to be checked for entries. DATE is date like
4500 the one returned by `calendar-current-date'. ARGS are symbols indicating
4501 which kind of entries should be extracted. For details about these, see
4502 the documentation of `org-diary'."
4503 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4504 (let* ((org-startup-folded nil)
4505 (org-startup-align-all-tables nil)
4506 (buffer (if (file-exists-p file)
4507 (org-get-agenda-file-buffer file)
4508 (error "No such file %s" file)))
4509 arg results rtn deadline-results)
4510 (if (not buffer)
4511 ;; If file does not exist, make sure an error message ends up in diary
4512 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4513 (with-current-buffer buffer
4514 (unless (org-mode-p)
4515 (error "Agenda file %s is not in `org-mode'" file))
4516 (let ((case-fold-search nil))
4517 (save-excursion
4518 (save-restriction
4519 (if org-agenda-restrict
4520 (narrow-to-region org-agenda-restrict-begin
4521 org-agenda-restrict-end)
4522 (widen))
4523 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4524 (while (setq arg (pop args))
4525 (cond
4526 ((and (eq arg :todo)
4527 (equal date (calendar-gregorian-from-absolute
4528 (org-today))))
4529 (setq rtn (org-agenda-get-todos))
4530 (setq results (append results rtn)))
4531 ((eq arg :timestamp)
4532 (setq rtn (org-agenda-get-blocks))
4533 (setq results (append results rtn))
4534 (setq rtn (org-agenda-get-timestamps))
4535 (setq results (append results rtn)))
4536 ((eq arg :sexp)
4537 (setq rtn (org-agenda-get-sexps))
4538 (setq results (append results rtn)))
4539 ((eq arg :scheduled)
4540 (setq rtn (org-agenda-get-scheduled deadline-results))
4541 (setq results (append results rtn)))
4542 ((eq arg :closed)
4543 (setq rtn (org-agenda-get-progress))
4544 (setq results (append results rtn)))
4545 ((eq arg :deadline)
4546 (setq rtn (org-agenda-get-deadlines))
4547 (setq deadline-results (copy-sequence rtn))
4548 (setq results (append results rtn))))))))
4549 results))))
4551 (defun org-agenda-get-todos ()
4552 "Return the TODO information for agenda display."
4553 (let* ((props (list 'face nil
4554 'done-face 'org-agenda-done
4555 'org-not-done-regexp org-not-done-regexp
4556 'org-todo-regexp org-todo-regexp
4557 'org-complex-heading-regexp org-complex-heading-regexp
4558 'mouse-face 'highlight
4559 'help-echo
4560 (format "mouse-2 or RET jump to org file %s"
4561 (abbreviate-file-name buffer-file-name))))
4562 (regexp (concat "^\\*+[ \t]+\\("
4563 (if org-select-this-todo-keyword
4564 (if (equal org-select-this-todo-keyword "*")
4565 org-todo-regexp
4566 (concat "\\<\\("
4567 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4568 "\\)\\>"))
4569 org-not-done-regexp)
4570 "[^\n\r]*\\)"))
4571 marker priority category tags todo-state
4572 ee txt beg end)
4573 (goto-char (point-min))
4574 (while (re-search-forward regexp nil t)
4575 (catch :skip
4576 (save-match-data
4577 (beginning-of-line)
4578 (org-agenda-skip)
4579 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4580 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4581 (goto-char (1+ beg))
4582 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4583 (throw :skip nil)))
4584 (goto-char (match-beginning 1))
4585 (setq marker (org-agenda-new-marker (match-beginning 0))
4586 category (org-get-category)
4587 txt (match-string 1)
4588 tags (org-get-tags-at (point))
4589 txt (org-format-agenda-item "" txt category tags)
4590 priority (1+ (org-get-priority txt))
4591 todo-state (org-get-todo-state))
4592 (org-add-props txt props
4593 'org-marker marker 'org-hd-marker marker
4594 'priority priority 'org-category category
4595 'type "todo" 'todo-state todo-state)
4596 (push txt ee)
4597 (if org-agenda-todo-list-sublevels
4598 (goto-char (match-end 1))
4599 (org-end-of-subtree 'invisible))))
4600 (nreverse ee)))
4602 (defun org-agenda-todo-custom-ignore-p (time n)
4603 "Check whether timestamp is farther away then n number of days.
4604 This function is invoked if `org-agenda-todo-ignore-deadlines',
4605 `org-agenda-todo-ignore-scheduled' or
4606 `org-agenda-todo-ignore-timestamp' is set to an integer."
4607 (let ((days (org-days-to-time time)))
4608 (if (>= n 0)
4609 (>= days n)
4610 (<= days n))))
4612 ;;;###autoload
4613 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4614 (&optional end)
4615 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4616 (when (or org-agenda-todo-ignore-with-date
4617 org-agenda-todo-ignore-scheduled
4618 org-agenda-todo-ignore-deadlines
4619 org-agenda-todo-ignore-timestamp)
4620 (setq end (or end (save-excursion (outline-next-heading) (point))))
4621 (save-excursion
4622 (or (and org-agenda-todo-ignore-with-date
4623 (re-search-forward org-ts-regexp end t))
4624 (and org-agenda-todo-ignore-scheduled
4625 (re-search-forward org-scheduled-time-regexp end t)
4626 (cond
4627 ((eq org-agenda-todo-ignore-scheduled 'future)
4628 (> (org-days-to-time (match-string 1)) 0))
4629 ((eq org-agenda-todo-ignore-scheduled 'past)
4630 (<= (org-days-to-time (match-string 1)) 0))
4631 ((numberp org-agenda-todo-ignore-scheduled)
4632 (org-agenda-todo-custom-ignore-p
4633 (match-string 1) org-agenda-todo-ignore-scheduled))
4634 (t)))
4635 (and org-agenda-todo-ignore-deadlines
4636 (re-search-forward org-deadline-time-regexp end t)
4637 (cond
4638 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4639 ((eq org-agenda-todo-ignore-deadlines 'far)
4640 (not (org-deadline-close (match-string 1))))
4641 ((eq org-agenda-todo-ignore-deadlines 'future)
4642 (> (org-days-to-time (match-string 1)) 0))
4643 ((eq org-agenda-todo-ignore-deadlines 'past)
4644 (<= (org-days-to-time (match-string 1)) 0))
4645 ((numberp org-agenda-todo-ignore-deadlines)
4646 (org-agenda-todo-custom-ignore-p
4647 (match-string 1) org-agenda-todo-ignore-deadlines))
4648 (t (org-deadline-close (match-string 1)))))
4649 (and org-agenda-todo-ignore-timestamp
4650 (let ((buffer (current-buffer))
4651 (regexp
4652 (concat
4653 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4654 (start (point)))
4655 ;; Copy current buffer into a temporary one
4656 (with-temp-buffer
4657 (insert-buffer-substring buffer start end)
4658 (goto-char (point-min))
4659 ;; Delete SCHEDULED and DEADLINE items
4660 (while (re-search-forward regexp end t)
4661 (delete-region (match-beginning 0) (match-end 0)))
4662 (goto-char (point-min))
4663 ;; No search for timestamp left
4664 (when (re-search-forward org-ts-regexp nil t)
4665 (cond
4666 ((eq org-agenda-todo-ignore-timestamp 'future)
4667 (> (org-days-to-time (match-string 1)) 0))
4668 ((eq org-agenda-todo-ignore-timestamp 'past)
4669 (<= (org-days-to-time (match-string 1)) 0))
4670 ((numberp org-agenda-todo-ignore-timestamp)
4671 (org-agenda-todo-custom-ignore-p
4672 (match-string 1) org-agenda-todo-ignore-timestamp))
4673 (t))))))))))
4675 (defconst org-agenda-no-heading-message
4676 "No heading for this item in buffer or region.")
4678 (defun org-agenda-get-timestamps ()
4679 "Return the date stamp information for agenda display."
4680 (let* ((props (list 'face nil
4681 'org-not-done-regexp org-not-done-regexp
4682 'org-todo-regexp org-todo-regexp
4683 'org-complex-heading-regexp org-complex-heading-regexp
4684 'mouse-face 'highlight
4685 'help-echo
4686 (format "mouse-2 or RET jump to org file %s"
4687 (abbreviate-file-name buffer-file-name))))
4688 (d1 (calendar-absolute-from-gregorian date))
4689 (remove-re
4690 (concat
4691 (regexp-quote
4692 (format-time-string
4693 "<%Y-%m-%d"
4694 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4695 ".*?>"))
4696 (regexp
4697 (concat
4698 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4699 (regexp-quote
4700 (substring
4701 (format-time-string
4702 (car org-time-stamp-formats)
4703 (apply 'encode-time ; DATE bound by calendar
4704 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4705 1 11))
4706 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4707 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4708 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4709 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4710 todo-state end-of-match show-all)
4711 (goto-char (point-min))
4712 (while (setq end-of-match (re-search-forward regexp nil t))
4713 (setq b0 (match-beginning 0)
4714 b3 (match-beginning 3) e3 (match-end 3)
4715 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4716 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4717 (member todo-state
4718 org-agenda-repeating-timestamp-show-all)))
4719 (catch :skip
4720 (and (org-at-date-range-p) (throw :skip nil))
4721 (org-agenda-skip)
4722 (if (and (match-end 1)
4723 (not (= d1 (org-time-string-to-absolute
4724 (match-string 1) d1 nil show-all))))
4725 (throw :skip nil))
4726 (if (and e3
4727 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4728 (throw :skip nil))
4729 (setq tmp (buffer-substring (max (point-min)
4730 (- b0 org-ds-keyword-length))
4732 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4733 inactivep (= (char-after b0) ?\[)
4734 deadlinep (string-match org-deadline-regexp tmp)
4735 scheduledp (string-match org-scheduled-regexp tmp)
4736 closedp (and org-agenda-include-inactive-timestamps
4737 (string-match org-closed-string tmp))
4738 clockp (and org-agenda-include-inactive-timestamps
4739 (or (string-match org-clock-string tmp)
4740 (string-match "]-+\\'" tmp)))
4741 donep (member todo-state org-done-keywords))
4742 (if (or scheduledp deadlinep closedp clockp
4743 (and donep org-agenda-skip-timestamp-if-done))
4744 (throw :skip t))
4745 (if (string-match ">" timestr)
4746 ;; substring should only run to end of time stamp
4747 (setq timestr (substring timestr 0 (match-end 0))))
4748 (setq marker (org-agenda-new-marker b0)
4749 category (org-get-category b0))
4750 (save-excursion
4751 (if (not (re-search-backward "^\\*+ " nil t))
4752 (setq txt org-agenda-no-heading-message)
4753 (goto-char (match-beginning 0))
4754 (setq hdmarker (org-agenda-new-marker)
4755 tags (org-get-tags-at))
4756 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4757 (setq head (or (match-string 1) ""))
4758 (setq txt (org-format-agenda-item
4759 (if inactivep org-agenda-inactive-leader nil)
4760 head category tags timestr
4761 remove-re)))
4762 (setq priority (org-get-priority txt))
4763 (org-add-props txt props
4764 'org-marker marker 'org-hd-marker hdmarker)
4765 (org-add-props txt nil 'priority priority
4766 'org-category category 'date date
4767 'todo-state todo-state
4768 'type "timestamp")
4769 (push txt ee))
4770 (if org-agenda-skip-additional-timestamps-same-entry
4771 (outline-next-heading)
4772 (goto-char end-of-match))))
4773 (nreverse ee)))
4775 (defun org-agenda-get-sexps ()
4776 "Return the sexp information for agenda display."
4777 (require 'diary-lib)
4778 (let* ((props (list 'mouse-face 'highlight
4779 'help-echo
4780 (format "mouse-2 or RET jump to org file %s"
4781 (abbreviate-file-name buffer-file-name))))
4782 (regexp "^&?%%(")
4783 marker category ee txt tags entry result beg b sexp sexp-entry
4784 todo-state)
4785 (goto-char (point-min))
4786 (while (re-search-forward regexp nil t)
4787 (catch :skip
4788 (org-agenda-skip)
4789 (setq beg (match-beginning 0))
4790 (goto-char (1- (match-end 0)))
4791 (setq b (point))
4792 (forward-sexp 1)
4793 (setq sexp (buffer-substring b (point)))
4794 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4795 (org-trim (match-string 1))
4796 ""))
4797 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4798 (when result
4799 (setq marker (org-agenda-new-marker beg)
4800 category (org-get-category beg)
4801 todo-state (org-get-todo-state))
4803 (dolist (r (if (stringp result)
4804 (list result)
4805 result)) ;; we expect a list here
4806 (if (string-match "\\S-" r)
4807 (setq txt r)
4808 (setq txt "SEXP entry returned empty string"))
4810 (setq txt (org-format-agenda-item
4811 "" txt category tags 'time))
4812 (org-add-props txt props 'org-marker marker)
4813 (org-add-props txt nil
4814 'org-category category 'date date 'todo-state todo-state
4815 'type "sexp")
4816 (push txt ee)))))
4817 (nreverse ee)))
4819 ;; Calendar sanity: define some functions that are independent of
4820 ;; `calendar-date-style'.
4821 ;; Normally I would like to use ISO format when calling the diary functions,
4822 ;; but to make sure we still have Emacs 22 compatibility we bind
4823 ;; also `european-calendar-style' and use european format
4824 (defun org-anniversary (year month day &optional mark)
4825 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4826 (org-no-warnings
4827 (let ((calendar-date-style 'european) (european-calendar-style t))
4828 (diary-anniversary day month year mark))))
4829 (defun org-cyclic (N year month day &optional mark)
4830 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4831 (org-no-warnings
4832 (let ((calendar-date-style 'european) (european-calendar-style t))
4833 (diary-cyclic N day month year mark))))
4834 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4835 "Like `diary-block', but with fixed (ISO) order of arguments."
4836 (org-no-warnings
4837 (let ((calendar-date-style 'european) (european-calendar-style t))
4838 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4839 (defun org-date (year month day &optional mark)
4840 "Like `diary-date', but with fixed (ISO) order of arguments."
4841 (org-no-warnings
4842 (let ((calendar-date-style 'european) (european-calendar-style t))
4843 (diary-date day month year mark))))
4844 (defalias 'org-float 'diary-float)
4846 ;; Define the` org-class' function
4847 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4848 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4849 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4850 is any number of ISO weeks in the block period for which the item should
4851 be skipped."
4852 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4853 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4854 (d (calendar-absolute-from-gregorian date)))
4855 (and
4856 (<= date1 d)
4857 (<= d date2)
4858 (= (calendar-day-of-week date) dayname)
4859 (or (not skip-weeks)
4860 (progn
4861 (require 'cal-iso)
4862 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4863 entry)))
4865 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4866 "Like `org-class', but honor `calendar-date-style'.
4867 The order of the first 2 times 3 arguments depends on the variable
4868 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4869 So for American calendars, give this as MONTH DAY YEAR, for European as
4870 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4871 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4872 is any number of ISO weeks in the block period for which the item should
4873 be skipped.
4875 This function is here only for backward compatibility and it is deprecated,
4876 please use `org-class' instead."
4877 (let* ((date1 (calendar-absolute-from-gregorian
4878 (org-order-calendar-date-args m1 d1 y1)))
4879 (date2 (calendar-absolute-from-gregorian
4880 (org-order-calendar-date-args m2 d2 y2)))
4881 (d (calendar-absolute-from-gregorian date)))
4882 (org-class
4883 (nth 2 date1) (car date1) (nth 1 date1)
4884 (nth 2 date2) (car date2) (nth 1 date2)
4885 dayname skip-weeks)))
4887 (defalias 'org-get-closed 'org-agenda-get-progress)
4888 (defun org-agenda-get-progress ()
4889 "Return the logged TODO entries for agenda display."
4890 (let* ((props (list 'mouse-face 'highlight
4891 'org-not-done-regexp org-not-done-regexp
4892 'org-todo-regexp org-todo-regexp
4893 'org-complex-heading-regexp org-complex-heading-regexp
4894 'help-echo
4895 (format "mouse-2 or RET jump to org file %s"
4896 (abbreviate-file-name buffer-file-name))))
4897 (items (if (consp org-agenda-show-log)
4898 org-agenda-show-log
4899 (if (eq org-agenda-show-log 'clockcheck)
4900 '(clock)
4901 org-agenda-log-mode-items)))
4902 (parts
4903 (delq nil
4904 (list
4905 (if (memq 'closed items) (concat "\\<" org-closed-string))
4906 (if (memq 'clock items) (concat "\\<" org-clock-string))
4907 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4908 (parts-re (if parts (mapconcat 'identity parts "\\|")
4909 (error "`org-agenda-log-mode-items' is empty")))
4910 (regexp (concat
4911 "\\(" parts-re "\\)"
4912 " *\\["
4913 (regexp-quote
4914 (substring
4915 (format-time-string
4916 (car org-time-stamp-formats)
4917 (apply 'encode-time ; DATE bound by calendar
4918 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4919 1 11))))
4920 (org-agenda-search-headline-for-time nil)
4921 marker hdmarker priority category tags closedp statep clockp state
4922 ee txt extra timestr rest clocked)
4923 (goto-char (point-min))
4924 (while (re-search-forward regexp nil t)
4925 (catch :skip
4926 (org-agenda-skip)
4927 (setq marker (org-agenda-new-marker (match-beginning 0))
4928 closedp (equal (match-string 1) org-closed-string)
4929 statep (equal (string-to-char (match-string 1)) ?-)
4930 clockp (not (or closedp statep))
4931 state (and statep (match-string 2))
4932 category (org-get-category (match-beginning 0))
4933 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4935 (when (string-match "\\]" timestr)
4936 ;; substring should only run to end of time stamp
4937 (setq rest (substring timestr (match-end 0))
4938 timestr (substring timestr 0 (match-end 0)))
4939 (if (and (not closedp) (not statep)
4940 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4941 (progn (setq timestr (concat (substring timestr 0 -1)
4942 "-" (match-string 1 rest) "]"))
4943 (setq clocked (match-string 2 rest)))
4944 (setq clocked "-")))
4945 (save-excursion
4946 (setq extra
4947 (cond
4948 ((not org-agenda-log-mode-add-notes) nil)
4949 (statep
4950 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4951 (match-string 1)))
4952 (clockp
4953 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4954 (match-string 1)))))
4955 (if (not (re-search-backward "^\\*+ " nil t))
4956 (setq txt org-agenda-no-heading-message)
4957 (goto-char (match-beginning 0))
4958 (setq hdmarker (org-agenda-new-marker)
4959 tags (org-get-tags-at))
4960 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4961 (setq txt (match-string 1))
4962 (when extra
4963 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4964 (setq txt (concat (substring txt 0 (match-beginning 1))
4965 " - " extra " " (match-string 2 txt)))
4966 (setq txt (concat txt " - " extra))))
4967 (setq txt (org-format-agenda-item
4968 (cond
4969 (closedp "Closed: ")
4970 (statep (concat "State: (" state ")"))
4971 (t (concat "Clocked: (" clocked ")")))
4972 txt category tags timestr)))
4973 (setq priority 100000)
4974 (org-add-props txt props
4975 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4976 'priority priority 'org-category category
4977 'type "closed" 'date date
4978 'undone-face 'org-warning 'done-face 'org-agenda-done)
4979 (push txt ee))
4980 (goto-char (point-at-eol))))
4981 (nreverse ee)))
4983 (defun org-agenda-show-clocking-issues ()
4984 "Add overlays, showing issues with clocking.
4985 See also the user option `org-agenda-clock-consistency-checks'."
4986 (interactive)
4987 (let* ((pl org-agenda-clock-consistency-checks)
4988 (re (concat "^[ \t]*"
4989 org-clock-string
4990 "[ \t]+"
4991 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
4992 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
4993 (tlstart 0.)
4994 (tlend 0.)
4995 (maxtime (org-hh:mm-string-to-minutes
4996 (or (plist-get pl :max-duration) "24:00")))
4997 (mintime (org-hh:mm-string-to-minutes
4998 (or (plist-get pl :min-duration) 0)))
4999 (maxgap (org-hh:mm-string-to-minutes
5000 ;; default 30:00 means never complain
5001 (or (plist-get pl :max-gap) "30:00")))
5002 (gapok (mapcar 'org-hh:mm-string-to-minutes
5003 (plist-get pl :gap-ok-around)))
5004 (def-face (or (plist-get pl :default-face)
5005 '((:background "DarkRed") (:foreground "white"))))
5006 issue face m te ts dt ov)
5007 (goto-char (point-min))
5008 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5009 (setq issue nil face def-face)
5010 (catch 'next
5011 (setq m (org-get-at-bol 'org-marker)
5012 te nil ts nil)
5013 (unless (and m (markerp m))
5014 (setq issue "No valid clock line") (throw 'next t))
5015 (org-with-point-at m
5016 (save-excursion
5017 (goto-char (point-at-bol))
5018 (unless (looking-at re)
5019 (error "No valid Clock line")
5020 (throw 'next t))
5021 (unless (match-end 3)
5022 (setq issue "No end time"
5023 face (or (plist-get pl :no-end-time-face) face))
5024 (throw 'next t))
5025 (setq ts (match-string 1)
5026 te (match-string 3)
5027 ts (org-float-time
5028 (apply 'encode-time (org-parse-time-string ts)))
5029 te (org-float-time
5030 (apply 'encode-time (org-parse-time-string te)))
5031 dt (- te ts))))
5032 (cond
5033 ((> dt (* 60 maxtime))
5034 ;; a very long clocking chunk
5035 (setq issue (format "Clocking interval is very long: %s"
5036 (org-minutes-to-hh:mm-string
5037 (floor (/ (float dt) 60.))))
5038 face (or (plist-get pl :long-face) face)))
5039 ((< dt (* 60 mintime))
5040 ;; a very short clocking chunk
5041 (setq issue (format "Clocking interval is very short: %s"
5042 (org-minutes-to-hh:mm-string
5043 (floor (/ (float dt) 60.))))
5044 face (or (plist-get pl :short-face) face)))
5045 ((and (> tlend 0) (< ts tlend))
5046 ;; Two clock entries are overlapping
5047 (setq issue (format "Clocking overlap: %d minutes"
5048 (/ (- tlend ts) 60))
5049 face (or (plist-get pl :overlap-face) face)))
5050 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5051 ;; There is a gap, lets see if we need to report it
5052 (unless (org-agenda-check-clock-gap tlend ts gapok)
5053 (setq issue (format "Clocking gap: %d minutes"
5054 (/ (- ts tlend) 60))
5055 face (or (plist-get pl :gap-face) face))))
5056 (t nil)))
5057 (setq tlend (or te tlend) tlstart (or ts tlstart))
5058 (when issue
5059 ;; OK, there was some issue, add an overlay to show the issue
5060 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5061 (overlay-put ov 'before-string
5062 (concat
5063 (org-add-props
5064 (format "%-43s" (concat " " issue))
5066 'face face)
5067 "\n"))
5068 (overlay-put ov 'evaporate t)))))
5070 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5071 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5072 (catch 'exit
5073 (unless ok-list
5074 ;; there are no OK times for gaps...
5075 (throw 'exit nil))
5076 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5077 ;; This is more than 24 hours, so it is OK.
5078 ;; because we have at least one OK time, that must be in the
5079 ;; 24 hour interval.
5080 (throw 'exit t))
5081 ;; We have a shorter gap.
5082 ;; Now we have to get the minute of the day when these times are
5083 (let* ((t1dec (decode-time (seconds-to-time t1)))
5084 (t2dec (decode-time (seconds-to-time t2)))
5085 ;; compute the minute on the day
5086 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5087 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5088 (when (< min2 min1)
5089 ;; if min2 is smaller than min1, this means it is on the next day.
5090 ;; Wrap it to after midnight.
5091 (setq min2 (+ min2 1440)))
5092 ;; Now check if any of the OK times is in the gap
5093 (mapc (lambda (x)
5094 ;; Wrap the time to after midnight if necessary
5095 (if (< x min1) (setq x (+ x 1440)))
5096 ;; Check if in interval
5097 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5098 ok-list)
5099 ;; Nope, this gap is not OK
5100 nil)))
5102 (defun org-agenda-get-deadlines ()
5103 "Return the deadline information for agenda display."
5104 (let* ((props (list 'mouse-face 'highlight
5105 'org-not-done-regexp org-not-done-regexp
5106 'org-todo-regexp org-todo-regexp
5107 'org-complex-heading-regexp org-complex-heading-regexp
5108 'help-echo
5109 (format "mouse-2 or RET jump to org file %s"
5110 (abbreviate-file-name buffer-file-name))))
5111 (regexp org-deadline-time-regexp)
5112 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5113 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5114 d2 diff dfrac wdays pos pos1 category tags
5115 suppress-prewarning
5116 ee txt head face s todo-state show-all upcomingp donep timestr)
5117 (goto-char (point-min))
5118 (while (re-search-forward regexp nil t)
5119 (setq suppress-prewarning nil)
5120 (catch :skip
5121 (org-agenda-skip)
5122 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5123 (save-match-data
5124 (string-match org-scheduled-time-regexp
5125 (buffer-substring (point-at-bol)
5126 (point-at-eol)))))
5127 (setq suppress-prewarning
5128 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5129 org-agenda-skip-deadline-prewarning-if-scheduled
5130 0)))
5131 (setq s (match-string 1)
5132 txt nil
5133 pos (1- (match-beginning 1))
5134 todo-state (save-match-data (org-get-todo-state))
5135 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5136 (member todo-state
5137 org-agenda-repeating-timestamp-show-all))
5138 d2 (org-time-string-to-absolute
5139 (match-string 1) d1 'past show-all)
5140 diff (- d2 d1)
5141 wdays (if suppress-prewarning
5142 (let ((org-deadline-warning-days suppress-prewarning))
5143 (org-get-wdays s))
5144 (org-get-wdays s))
5145 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5146 upcomingp (and todayp (> diff 0)))
5147 ;; When to show a deadline in the calendar:
5148 ;; If the expiration is within wdays warning time.
5149 ;; Past-due deadlines are only shown on the current date
5150 (if (and (or (and (<= diff wdays)
5151 (and todayp (not org-agenda-only-exact-dates)))
5152 (= diff 0)))
5153 (save-excursion
5154 ;; (setq todo-state (org-get-todo-state))
5155 (setq donep (member todo-state org-done-keywords))
5156 (if (and donep
5157 (or org-agenda-skip-deadline-if-done
5158 (not (= diff 0))))
5159 (setq txt nil)
5160 (setq category (org-get-category))
5161 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5162 (setq txt org-agenda-no-heading-message)
5163 (goto-char (match-end 0))
5164 (setq pos1 (match-beginning 0))
5165 (setq tags (org-get-tags-at pos1))
5166 (setq head (buffer-substring-no-properties
5167 (point)
5168 (progn (skip-chars-forward "^\r\n")
5169 (point))))
5170 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5171 (setq timestr
5172 (concat (substring s (match-beginning 1)) " "))
5173 (setq timestr 'time))
5174 (setq txt (org-format-agenda-item
5175 (if (= diff 0)
5176 (car org-agenda-deadline-leaders)
5177 (if (functionp
5178 (nth 1 org-agenda-deadline-leaders))
5179 (funcall
5180 (nth 1 org-agenda-deadline-leaders)
5181 diff date)
5182 (format (nth 1 org-agenda-deadline-leaders)
5183 diff)))
5184 head category tags
5185 (if (not (= diff 0)) nil timestr)))))
5186 (when txt
5187 (setq face (org-agenda-deadline-face dfrac wdays))
5188 (org-add-props txt props
5189 'org-marker (org-agenda-new-marker pos)
5190 'org-hd-marker (org-agenda-new-marker pos1)
5191 'priority (+ (- diff)
5192 (org-get-priority txt))
5193 'org-category category
5194 'todo-state todo-state
5195 'type (if upcomingp "upcoming-deadline" "deadline")
5196 'date (if upcomingp date d2)
5197 'face (if donep 'org-agenda-done face)
5198 'undone-face face 'done-face 'org-agenda-done)
5199 (push txt ee))))))
5200 (nreverse ee)))
5202 (defun org-agenda-deadline-face (fraction &optional wdays)
5203 "Return the face to displaying a deadline item.
5204 FRACTION is what fraction of the head-warning time has passed."
5205 (if (equal wdays 0) (setq fraction 1.))
5206 (let ((faces org-agenda-deadline-faces) f)
5207 (catch 'exit
5208 (while (setq f (pop faces))
5209 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5211 (defun org-agenda-get-scheduled (&optional deadline-results)
5212 "Return the scheduled information for agenda display."
5213 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5214 'org-todo-regexp org-todo-regexp
5215 'org-complex-heading-regexp org-complex-heading-regexp
5216 'done-face 'org-agenda-done
5217 'mouse-face 'highlight
5218 'help-echo
5219 (format "mouse-2 or RET jump to org file %s"
5220 (abbreviate-file-name buffer-file-name))))
5221 (regexp org-scheduled-time-regexp)
5222 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5223 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5225 (deadline-position-alist
5226 (mapcar (lambda (a) (and (setq mm (get-text-property
5227 0 'org-hd-marker a))
5228 (cons (marker-position mm) a)))
5229 deadline-results))
5230 d2 diff pos pos1 category tags donep
5231 ee txt head pastschedp todo-state face timestr s habitp show-all)
5232 (goto-char (point-min))
5233 (while (re-search-forward regexp nil t)
5234 (catch :skip
5235 (org-agenda-skip)
5236 (setq s (match-string 1)
5237 txt nil
5238 pos (1- (match-beginning 1))
5239 todo-state (save-match-data (org-get-todo-state))
5240 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5241 (member todo-state
5242 org-agenda-repeating-timestamp-show-all))
5243 d2 (org-time-string-to-absolute
5244 (match-string 1) d1 'past show-all)
5245 diff (- d2 d1))
5246 (setq pastschedp (and todayp (< diff 0)))
5247 ;; When to show a scheduled item in the calendar:
5248 ;; If it is on or past the date.
5249 (when (or (and (< diff 0)
5250 (< (abs diff) org-scheduled-past-days)
5251 (and todayp (not org-agenda-only-exact-dates)))
5252 (= diff 0))
5253 (save-excursion
5254 (setq donep (member todo-state org-done-keywords))
5255 (if (and donep
5256 (or org-agenda-skip-scheduled-if-done
5257 (not (= diff 0))
5258 (and (functionp 'org-is-habit-p)
5259 (org-is-habit-p))))
5260 (setq txt nil)
5261 (setq habitp (and (functionp 'org-is-habit-p)
5262 (org-is-habit-p)))
5263 (setq category (org-get-category))
5264 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5265 (setq txt org-agenda-no-heading-message)
5266 (goto-char (match-end 0))
5267 (setq pos1 (match-beginning 0))
5268 (if habitp
5269 (if (or (not org-habit-show-habits)
5270 (and (not todayp)
5271 org-habit-show-habits-only-for-today))
5272 (throw :skip nil))
5273 (if (and
5274 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5275 (and org-agenda-skip-scheduled-if-deadline-is-shown
5276 pastschedp))
5277 (setq mm (assoc pos1 deadline-position-alist)))
5278 (throw :skip nil)))
5279 (setq tags (org-get-tags-at))
5280 (setq head (buffer-substring-no-properties
5281 (point)
5282 (progn (skip-chars-forward "^\r\n") (point))))
5283 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5284 (setq timestr
5285 (concat (substring s (match-beginning 1)) " "))
5286 (setq timestr 'time))
5287 (setq txt (org-format-agenda-item
5288 (if (= diff 0)
5289 (car org-agenda-scheduled-leaders)
5290 (format (nth 1 org-agenda-scheduled-leaders)
5291 (- 1 diff)))
5292 head category tags
5293 (if (not (= diff 0)) nil timestr)
5294 nil habitp))))
5295 (when txt
5296 (setq face
5297 (cond
5298 ((and (not habitp) pastschedp)
5299 'org-scheduled-previously)
5300 (todayp 'org-scheduled-today)
5301 (t 'org-scheduled))
5302 habitp (and habitp (org-habit-parse-todo)))
5303 (org-add-props txt props
5304 'undone-face face
5305 'face (if donep 'org-agenda-done face)
5306 'org-marker (org-agenda-new-marker pos)
5307 'org-hd-marker (org-agenda-new-marker pos1)
5308 'type (if pastschedp "past-scheduled" "scheduled")
5309 'date (if pastschedp d2 date)
5310 'priority (if habitp
5311 (org-habit-get-priority habitp)
5312 (+ 94 (- 5 diff) (org-get-priority txt)))
5313 'org-category category
5314 'org-habit-p habitp
5315 'todo-state todo-state)
5316 (push txt ee))))))
5317 (nreverse ee)))
5319 (defun org-agenda-get-blocks ()
5320 "Return the date-range information for agenda display."
5321 (let* ((props (list 'face nil
5322 'org-not-done-regexp org-not-done-regexp
5323 'org-todo-regexp org-todo-regexp
5324 'org-complex-heading-regexp org-complex-heading-regexp
5325 'mouse-face 'highlight
5326 'help-echo
5327 (format "mouse-2 or RET jump to org file %s"
5328 (abbreviate-file-name buffer-file-name))))
5329 (regexp org-tr-regexp)
5330 (d0 (calendar-absolute-from-gregorian date))
5331 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5332 head donep)
5333 (goto-char (point-min))
5334 (while (re-search-forward regexp nil t)
5335 (catch :skip
5336 (org-agenda-skip)
5337 (setq pos (point))
5338 (let ((start-time (match-string 1))
5339 (end-time (match-string 2)))
5340 (setq s1 (match-string 1)
5341 s2 (match-string 2)
5342 d1 (time-to-days (org-time-string-to-time s1))
5343 d2 (time-to-days (org-time-string-to-time s2)))
5344 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5345 ;; Only allow days between the limits, because the normal
5346 ;; date stamps will catch the limits.
5347 (save-excursion
5348 (setq todo-state (org-get-todo-state))
5349 (setq donep (member todo-state org-done-keywords))
5350 (if (and donep org-agenda-skip-timestamp-if-done)
5351 (throw :skip t))
5352 (setq marker (org-agenda-new-marker (point)))
5353 (setq category (org-get-category))
5354 (if (not (re-search-backward "^\\*+ " nil t))
5355 (setq txt org-agenda-no-heading-message)
5356 (goto-char (match-beginning 0))
5357 (setq hdmarker (org-agenda-new-marker (point)))
5358 (setq tags (org-get-tags-at))
5359 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5360 (setq head (match-string 1))
5361 (let ((remove-re
5362 (if org-agenda-remove-timeranges-from-blocks
5363 (concat
5364 "<" (regexp-quote s1) ".*?>"
5365 "--"
5366 "<" (regexp-quote s2) ".*?>")
5367 nil)))
5368 (setq txt (org-format-agenda-item
5369 (format
5370 (nth (if (= d1 d2) 0 1)
5371 org-agenda-timerange-leaders)
5372 (1+ (- d0 d1)) (1+ (- d2 d1)))
5373 head category tags
5374 (cond ((= d1 d0)
5375 (concat "<" start-time ">"))
5376 ((= d2 d0)
5377 (concat "<" end-time ">"))
5378 (t nil))
5379 remove-re))))
5380 (org-add-props txt props
5381 'org-marker marker 'org-hd-marker hdmarker
5382 'type "block" 'date date
5383 'todo-state todo-state
5384 'priority (org-get-priority txt) 'org-category category)
5385 (push txt ee))))
5386 (goto-char pos)))
5387 ;; Sort the entries by expiration date.
5388 (nreverse ee)))
5390 ;;; Agenda presentation and sorting
5392 (defvar org-prefix-has-time nil
5393 "A flag, set by `org-compile-prefix-format'.
5394 The flag is set if the currently compiled format contains a `%t'.")
5395 (defvar org-prefix-has-tag nil
5396 "A flag, set by `org-compile-prefix-format'.
5397 The flag is set if the currently compiled format contains a `%T'.")
5398 (defvar org-prefix-has-effort nil
5399 "A flag, set by `org-compile-prefix-format'.
5400 The flag is set if the currently compiled format contains a `%e'.")
5401 (defvar org-prefix-category-length nil
5402 "Used by `org-compile-prefix-format' to remember the category field width.")
5403 (defvar org-prefix-category-max-length nil
5404 "Used by `org-compile-prefix-format' to remember the category field width.")
5406 (defun org-agenda-get-category-icon (category)
5407 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5408 (dolist (entry org-agenda-category-icon-alist)
5409 (when (org-string-match-p (car entry) category)
5410 (if (listp (cadr entry))
5411 (return (cadr entry))
5412 (return (apply 'create-image (cdr entry)))))))
5414 (defun org-format-agenda-item (extra txt &optional category tags dotime
5415 remove-re habitp)
5416 "Format TXT to be inserted into the agenda buffer.
5417 In particular, it adds the prefix and corresponding text properties. EXTRA
5418 must be a string and replaces the `%s' specifier in the prefix format.
5419 CATEGORY (string, symbol or nil) may be used to overrule the default
5420 category taken from local variable or file name. It will replace the `%c'
5421 specifier in the format. DOTIME, when non-nil, indicates that a
5422 time-of-day should be extracted from TXT for sorting of this entry, and for
5423 the `%t' specifier in the format. When DOTIME is a string, this string is
5424 searched for a time before TXT is. TAGS can be the tags of the headline.
5425 Any match of REMOVE-RE will be removed from TXT."
5426 (save-match-data
5427 ;; Diary entries sometimes have extra whitespace at the beginning
5428 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5430 ;; Fix the tags part in txt
5431 (setq txt (org-agenda-fix-displayed-tags
5432 txt tags
5433 org-agenda-show-inherited-tags
5434 org-agenda-hide-tags-regexp))
5435 (let* ((category (or category
5436 (if (stringp org-category)
5437 org-category
5438 (and org-category (symbol-name org-category)))
5439 (if buffer-file-name
5440 (file-name-sans-extension
5441 (file-name-nondirectory buffer-file-name))
5442 "")))
5443 (category-icon (org-agenda-get-category-icon category))
5444 (category-icon (if category-icon
5445 (propertize " " 'display category-icon)
5446 ""))
5447 ;; time, tag, effort are needed for the eval of the prefix format
5448 (tag (if tags (nth (1- (length tags)) tags) ""))
5449 time effort neffort
5450 (ts (if dotime (concat
5451 (if (stringp dotime) dotime "")
5452 (and org-agenda-search-headline-for-time txt))))
5453 (time-of-day (and dotime (org-get-time-of-day ts)))
5454 stamp plain s0 s1 s2 rtn srp l
5455 duration thecategory)
5456 (and (org-mode-p) buffer-file-name
5457 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5458 (when (and dotime time-of-day)
5459 ;; Extract starting and ending time and move them to prefix
5460 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5461 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5462 (setq s0 (match-string 0 ts)
5463 srp (and stamp (match-end 3))
5464 s1 (match-string (if plain 1 2) ts)
5465 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5467 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5468 ;; them, we might want to remove them there to avoid duplication.
5469 ;; The user can turn this off with a variable.
5470 (if (and org-prefix-has-time
5471 org-agenda-remove-times-when-in-prefix (or stamp plain)
5472 (string-match (concat (regexp-quote s0) " *") txt)
5473 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5474 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5475 (= (match-beginning 0) 0)
5477 (setq txt (replace-match "" nil nil txt))))
5478 ;; Normalize the time(s) to 24 hour
5479 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5480 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5482 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5483 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5484 (setq s2
5485 (org-minutes-to-hh:mm-string
5486 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5488 ;; Compute the duration
5489 (when s2
5490 (setq duration (- (org-hh:mm-string-to-minutes s2)
5491 (org-hh:mm-string-to-minutes s1)))))
5493 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5494 txt)
5495 ;; Tags are in the string
5496 (if (or (eq org-agenda-remove-tags t)
5497 (and org-agenda-remove-tags
5498 org-prefix-has-tag))
5499 (setq txt (replace-match "" t t txt))
5500 (setq txt (replace-match
5501 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5502 (match-string 2 txt))
5503 t t txt))))
5504 (when (org-mode-p)
5505 (setq effort
5506 (condition-case nil
5507 (org-get-effort
5508 (or (get-text-property 0 'org-hd-marker txt)
5509 (get-text-property 0 'org-marker txt)))
5510 (error nil)))
5511 (when effort
5512 (setq neffort (org-duration-string-to-minutes effort)
5513 effort (setq effort (concat "[" effort "]" )))))
5515 (when remove-re
5516 (while (string-match remove-re txt)
5517 (setq txt (replace-match "" t t txt))))
5519 ;; Set org-heading property on `txt' to mark the start of the
5520 ;; heading.
5521 (add-text-properties 0 (length txt) '(org-heading t) txt)
5523 ;; Prepare the variables needed in the eval of the compiled format
5524 (setq time (cond (s2 (concat
5525 (org-agenda-time-of-day-to-ampm-maybe s1)
5526 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5527 (if org-agenda-timegrid-use-ampm " ")))
5528 (s1 (concat
5529 (org-agenda-time-of-day-to-ampm-maybe s1)
5530 (if org-agenda-timegrid-use-ampm
5531 "........ "
5532 "......")))
5533 (t ""))
5534 extra (or (and (not habitp) extra) "")
5535 category (if (symbolp category) (symbol-name category) category)
5536 thecategory (copy-sequence category))
5537 (if (string-match org-bracket-link-regexp category)
5538 (progn
5539 (setq l (if (match-end 3)
5540 (- (match-end 3) (match-beginning 3))
5541 (- (match-end 1) (match-beginning 1))))
5542 (when (< l (or org-prefix-category-length 0))
5543 (setq category (copy-sequence category))
5544 (org-add-props category nil
5545 'extra-space (make-string
5546 (- org-prefix-category-length l 1) ?\ ))))
5547 (if (and org-prefix-category-max-length
5548 (>= (length category) org-prefix-category-max-length))
5549 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5550 ;; Evaluate the compiled format
5551 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5553 ;; And finally add the text properties
5554 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5555 (org-add-props rtn nil
5556 'org-category (if thecategory (downcase thecategory) category)
5557 'tags (mapcar 'org-downcase-keep-props tags)
5558 'org-highest-priority org-highest-priority
5559 'org-lowest-priority org-lowest-priority
5560 'time-of-day time-of-day
5561 'duration duration
5562 'effort effort
5563 'effort-minutes neffort
5564 'txt txt
5565 'time time
5566 'extra extra
5567 'dotime dotime))))
5569 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5570 "Remove tags string from TXT, and add a modified list of tags.
5571 The modified list may contain inherited tags, and tags matched by
5572 `org-agenda-hide-tags-regexp' will be removed."
5573 (when (or add-inherited hide-re)
5574 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5575 (setq txt (substring txt 0 (match-beginning 0))))
5576 (setq tags
5577 (delq nil
5578 (mapcar (lambda (tg)
5579 (if (or (and hide-re (string-match hide-re tg))
5580 (and (not add-inherited)
5581 (get-text-property 0 'inherited tg)))
5583 tg))
5584 tags)))
5585 (when tags
5586 (let ((have-i (get-text-property 0 'inherited (car tags)))
5588 (setq txt (concat txt " :"
5589 (mapconcat
5590 (lambda (x)
5591 (setq i (get-text-property 0 'inherited x))
5592 (if (and have-i (not i))
5593 (progn
5594 (setq have-i nil)
5595 (concat ":" x))
5597 tags ":")
5598 (if have-i "::" ":"))))))
5599 txt)
5601 (defun org-downcase-keep-props (s)
5602 (let ((props (text-properties-at 0 s)))
5603 (setq s (downcase s))
5604 (add-text-properties 0 (length s) props s)
5607 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5608 (defvar org-agenda-sorting-strategy-selected nil)
5610 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5611 (catch 'exit
5612 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5613 ((and todayp (member 'today (car org-agenda-time-grid))))
5614 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5615 ((member 'weekly (car org-agenda-time-grid)))
5616 (t (throw 'exit list)))
5617 (let* ((have (delq nil (mapcar
5618 (lambda (x) (get-text-property 1 'time-of-day x))
5619 list)))
5620 (string (nth 1 org-agenda-time-grid))
5621 (gridtimes (nth 2 org-agenda-time-grid))
5622 (req (car org-agenda-time-grid))
5623 (remove (member 'remove-match req))
5624 new time)
5625 (if (and (member 'require-timed req) (not have))
5626 ;; don't show empty grid
5627 (throw 'exit list))
5628 (while (setq time (pop gridtimes))
5629 (unless (and remove (member time have))
5630 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5631 (push (org-format-agenda-item
5632 nil string "" nil
5633 (concat (substring time 0 -2) ":" (substring time -2)))
5634 new)
5635 (put-text-property
5636 2 (length (car new)) 'face 'org-time-grid (car new))))
5637 (when (and todayp org-agenda-show-current-time-in-grid)
5638 (push (org-format-agenda-item
5640 org-agenda-current-time-string
5641 "" nil
5642 (format-time-string "%H:%M "))
5643 new)
5644 (put-text-property
5645 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5647 (if (member 'time-up org-agenda-sorting-strategy-selected)
5648 (append new list)
5649 (append list new)))))
5651 (defun org-compile-prefix-format (key)
5652 "Compile the prefix format into a Lisp form that can be evaluated.
5653 The resulting form is returned and stored in the variable
5654 `org-prefix-format-compiled'."
5655 (setq org-prefix-has-time nil org-prefix-has-tag nil
5656 org-prefix-category-length nil org-prefix-has-effort nil)
5657 (let ((s (cond
5658 ((stringp org-agenda-prefix-format)
5659 org-agenda-prefix-format)
5660 ((assq key org-agenda-prefix-format)
5661 (cdr (assq key org-agenda-prefix-format)))
5662 (t " %-12:c%?-12t% s")))
5663 (start 0)
5664 varform vars var e c f opt)
5665 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5666 s start)
5667 (setq var (or (cdr (assoc (match-string 4 s)
5668 '(("c" . category) ("t" . time) ("s" . extra)
5669 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5670 'eval)
5671 c (or (match-string 3 s) "")
5672 opt (match-beginning 1)
5673 start (1+ (match-beginning 0)))
5674 (if (equal var 'time) (setq org-prefix-has-time t))
5675 (if (equal var 'tag) (setq org-prefix-has-tag t))
5676 (if (equal var 'effort) (setq org-prefix-has-effort t))
5677 (setq f (concat "%" (match-string 2 s) "s"))
5678 (when (equal var 'category)
5679 (setq org-prefix-category-length
5680 (floor (abs (string-to-number (match-string 2 s)))))
5681 (setq org-prefix-category-max-length
5682 (let ((x (match-string 2 s)))
5683 (save-match-data
5684 (if (string-match "\\.[0-9]+" x)
5685 (string-to-number (substring (match-string 0 x) 1)))))))
5686 (if (eq var 'eval)
5687 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5688 (if opt
5689 (setq varform
5690 `(if (equal "" ,var)
5692 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5693 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5694 (setq s (replace-match "%s" t nil s))
5695 (push varform vars))
5696 (setq vars (nreverse vars))
5697 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5699 (defun org-set-sorting-strategy (key)
5700 (if (symbolp (car org-agenda-sorting-strategy))
5701 ;; the old format
5702 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5703 (setq org-agenda-sorting-strategy-selected
5704 (or (cdr (assq key org-agenda-sorting-strategy))
5705 (cdr (assq 'agenda org-agenda-sorting-strategy))
5706 '(time-up category-keep priority-down)))))
5708 (defun org-get-time-of-day (s &optional string mod24)
5709 "Check string S for a time of day.
5710 If found, return it as a military time number between 0 and 2400.
5711 If not found, return nil.
5712 The optional STRING argument forces conversion into a 5 character wide string
5713 HH:MM."
5714 (save-match-data
5715 (when
5716 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5717 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5718 (let* ((h (string-to-number (match-string 1 s)))
5719 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5720 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5721 (am-p (equal ampm "am"))
5722 (h1 (cond ((not ampm) h)
5723 ((= h 12) (if am-p 0 12))
5724 (t (+ h (if am-p 0 12)))))
5725 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5726 (mod h1 24) h1))
5727 (t0 (+ (* 100 h2) m))
5728 (t1 (concat (if (>= h1 24) "+" " ")
5729 (if (and org-agenda-time-leading-zero
5730 (< t0 1000)) "0" "")
5731 (if (< t0 100) "0" "")
5732 (if (< t0 10) "0" "")
5733 (int-to-string t0))))
5734 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5736 (defvar org-agenda-before-sorting-filter-function nil
5737 "Function to be applied to agenda items prior to sorting.
5738 Prior to sorting also means just before they are inserted into the agenda.
5740 To aid sorting, you may revisit the original entries and add more text
5741 properties which will later be used by the sorting functions.
5743 The function should take a string argument, an agenda line.
5744 It has access to the text properties in that line, which contain among
5745 other things, the property `org-hd-marker' that points to the entry
5746 where the line comes from. Note that not all lines going into the agenda
5747 have this property, only most.
5749 The function should return the modified string. It is probably best
5750 to ONLY change text properties.
5752 You can also use this function as a filter, by returning nil for lines
5753 you don't want to have in the agenda at all. For this application, you
5754 could bind the variable in the options section of a custom command.")
5756 (defun org-finalize-agenda-entries (list &optional nosort)
5757 "Sort and concatenate the agenda items."
5758 (setq list (mapcar 'org-agenda-highlight-todo list))
5759 (if nosort
5760 list
5761 (when org-agenda-before-sorting-filter-function
5762 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5763 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5765 (defun org-agenda-highlight-todo (x)
5766 (let ((org-done-keywords org-done-keywords-for-agenda)
5767 (case-fold-search nil)
5769 (if (eq x 'line)
5770 (save-excursion
5771 (beginning-of-line 1)
5772 (setq re (org-get-at-bol 'org-todo-regexp))
5773 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5774 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5775 (add-text-properties (match-beginning 0) (match-end 1)
5776 (list 'face (org-get-todo-face 1)))
5777 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5778 (delete-region (match-beginning 1) (1- (match-end 0)))
5779 (goto-char (match-beginning 1))
5780 (insert (format org-agenda-todo-keyword-format s)))))
5781 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5782 (setq re (get-text-property 0 'org-todo-regexp x))
5783 (when (and re
5784 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5785 x (or pl 0)) pl))
5786 (add-text-properties
5787 (or (match-end 1) (match-end 0)) (match-end 0)
5788 (list 'face (org-get-todo-face (match-string 2 x)))
5790 (when (match-end 1)
5791 (setq x (concat (substring x 0 (match-end 1))
5792 (format org-agenda-todo-keyword-format
5793 (match-string 2 x))
5794 (org-add-props " " (text-properties-at 0 x))
5795 (substring x (match-end 3)))))))
5796 x)))
5798 (defsubst org-cmp-priority (a b)
5799 "Compare the priorities of string A and B."
5800 (let ((pa (or (get-text-property 1 'priority a) 0))
5801 (pb (or (get-text-property 1 'priority b) 0)))
5802 (cond ((> pa pb) +1)
5803 ((< pa pb) -1)
5804 (t nil))))
5806 (defsubst org-cmp-effort (a b)
5807 "Compare the priorities of string A and B."
5808 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5809 (ea (or (get-text-property 1 'effort-minutes a) def))
5810 (eb (or (get-text-property 1 'effort-minutes b) def)))
5811 (cond ((> ea eb) +1)
5812 ((< ea eb) -1)
5813 (t nil))))
5815 (defsubst org-cmp-category (a b)
5816 "Compare the string values of categories of strings A and B."
5817 (let ((ca (or (get-text-property 1 'org-category a) ""))
5818 (cb (or (get-text-property 1 'org-category b) "")))
5819 (cond ((string-lessp ca cb) -1)
5820 ((string-lessp cb ca) +1)
5821 (t nil))))
5823 (defsubst org-cmp-todo-state (a b)
5824 "Compare the todo states of strings A and B."
5825 (let* ((ma (or (get-text-property 1 'org-marker a)
5826 (get-text-property 1 'org-hd-marker a)))
5827 (mb (or (get-text-property 1 'org-marker b)
5828 (get-text-property 1 'org-hd-marker b)))
5829 (fa (and ma (marker-buffer ma)))
5830 (fb (and mb (marker-buffer mb)))
5831 (todo-kwds
5832 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5833 (and fb (with-current-buffer fb org-todo-keywords-1))))
5834 (ta (or (get-text-property 1 'todo-state a) ""))
5835 (tb (or (get-text-property 1 'todo-state b) ""))
5836 (la (- (length (member ta todo-kwds))))
5837 (lb (- (length (member tb todo-kwds))))
5838 (donepa (member ta org-done-keywords-for-agenda))
5839 (donepb (member tb org-done-keywords-for-agenda)))
5840 (cond ((and donepa (not donepb)) -1)
5841 ((and (not donepa) donepb) +1)
5842 ((< la lb) -1)
5843 ((< lb la) +1)
5844 (t nil))))
5846 (defsubst org-cmp-alpha (a b)
5847 "Compare the headlines, alphabetically."
5848 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5849 (plb (text-property-any 0 (length b) 'org-heading t b))
5850 (ta (and pla (substring a pla)))
5851 (tb (and plb (substring b plb))))
5852 (when pla
5853 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5854 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5855 (setq ta (substring ta (match-end 0))))
5856 (setq ta (downcase ta)))
5857 (when plb
5858 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5859 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5860 (setq tb (substring tb (match-end 0))))
5861 (setq tb (downcase tb)))
5862 (cond ((not ta) +1)
5863 ((not tb) -1)
5864 ((string-lessp ta tb) -1)
5865 ((string-lessp tb ta) +1)
5866 (t nil))))
5868 (defsubst org-cmp-tag (a b)
5869 "Compare the string values of the first tags of A and B."
5870 (let ((ta (car (last (get-text-property 1 'tags a))))
5871 (tb (car (last (get-text-property 1 'tags b)))))
5872 (cond ((not ta) +1)
5873 ((not tb) -1)
5874 ((string-lessp ta tb) -1)
5875 ((string-lessp tb ta) +1)
5876 (t nil))))
5878 (defsubst org-cmp-time (a b)
5879 "Compare the time-of-day values of strings A and B."
5880 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5881 (ta (or (get-text-property 1 'time-of-day a) def))
5882 (tb (or (get-text-property 1 'time-of-day b) def)))
5883 (cond ((< ta tb) -1)
5884 ((< tb ta) +1)
5885 (t nil))))
5887 (defsubst org-cmp-habit-p (a b)
5888 "Compare the todo states of strings A and B."
5889 (let ((ha (get-text-property 1 'org-habit-p a))
5890 (hb (get-text-property 1 'org-habit-p b)))
5891 (cond ((and ha (not hb)) -1)
5892 ((and (not ha) hb) +1)
5893 (t nil))))
5895 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5897 (defun org-entries-lessp (a b)
5898 "Predicate for sorting agenda entries."
5899 ;; The following variables will be used when the form is evaluated.
5900 ;; So even though the compiler complains, keep them.
5901 (let* ((ss org-agenda-sorting-strategy-selected)
5902 (time-up (and (org-em 'time-up 'time-down ss)
5903 (org-cmp-time a b)))
5904 (time-down (if time-up (- time-up) nil))
5905 (priority-up (and (org-em 'priority-up 'priority-down ss)
5906 (org-cmp-priority a b)))
5907 (priority-down (if priority-up (- priority-up) nil))
5908 (effort-up (and (org-em 'effort-up 'effort-down ss)
5909 (org-cmp-effort a b)))
5910 (effort-down (if effort-up (- effort-up) nil))
5911 (category-up (and (or (org-em 'category-up 'category-down ss)
5912 (memq 'category-keep ss))
5913 (org-cmp-category a b)))
5914 (category-down (if category-up (- category-up) nil))
5915 (category-keep (if category-up +1 nil))
5916 (tag-up (and (org-em 'tag-up 'tag-down ss)
5917 (org-cmp-tag a b)))
5918 (tag-down (if tag-up (- tag-up) nil))
5919 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5920 (org-cmp-todo-state a b)))
5921 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5922 (habit-up (and (org-em 'habit-up 'habit-down ss)
5923 (org-cmp-habit-p a b)))
5924 (habit-down (if habit-up (- habit-up) nil))
5925 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5926 (org-cmp-alpha a b)))
5927 (alpha-down (if alpha-up (- alpha-up) nil))
5928 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5929 user-defined-up user-defined-down)
5930 (if (and need-user-cmp org-agenda-cmp-user-defined
5931 (functionp org-agenda-cmp-user-defined))
5932 (setq user-defined-up
5933 (funcall org-agenda-cmp-user-defined a b)
5934 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5935 (cdr (assoc
5936 (eval (cons 'or org-agenda-sorting-strategy-selected))
5937 '((-1 . t) (1 . nil) (nil . nil))))))
5939 ;;; Agenda restriction lock
5941 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5942 "Overlay to mark the headline to which agenda commands are restricted.")
5943 (overlay-put org-agenda-restriction-lock-overlay
5944 'face 'org-agenda-restriction-lock)
5945 (overlay-put org-agenda-restriction-lock-overlay
5946 'help-echo "Agendas are currently limited to this subtree.")
5947 (org-detach-overlay org-agenda-restriction-lock-overlay)
5949 (defun org-agenda-set-restriction-lock (&optional type)
5950 "Set restriction lock for agenda, to current subtree or file.
5951 Restriction will be the file if TYPE is `file', or if type is the
5952 universal prefix '(4), or if the cursor is before the first headline
5953 in the file. Otherwise, restriction will be to the current subtree."
5954 (interactive "P")
5955 (and (equal type '(4)) (setq type 'file))
5956 (setq type (cond
5957 (type type)
5958 ((org-at-heading-p) 'subtree)
5959 ((condition-case nil (org-back-to-heading t) (error nil))
5960 'subtree)
5961 (t 'file)))
5962 (if (eq type 'subtree)
5963 (progn
5964 (setq org-agenda-restrict t)
5965 (setq org-agenda-overriding-restriction 'subtree)
5966 (put 'org-agenda-files 'org-restrict
5967 (list (buffer-file-name (buffer-base-buffer))))
5968 (org-back-to-heading t)
5969 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5970 (move-marker org-agenda-restrict-begin (point))
5971 (move-marker org-agenda-restrict-end
5972 (save-excursion (org-end-of-subtree t)))
5973 (message "Locking agenda restriction to subtree"))
5974 (put 'org-agenda-files 'org-restrict
5975 (list (buffer-file-name (buffer-base-buffer))))
5976 (setq org-agenda-restrict nil)
5977 (setq org-agenda-overriding-restriction 'file)
5978 (move-marker org-agenda-restrict-begin nil)
5979 (move-marker org-agenda-restrict-end nil)
5980 (message "Locking agenda restriction to file"))
5981 (setq current-prefix-arg nil)
5982 (org-agenda-maybe-redo))
5984 (defun org-agenda-remove-restriction-lock (&optional noupdate)
5985 "Remove the agenda restriction lock."
5986 (interactive "P")
5987 (org-detach-overlay org-agenda-restriction-lock-overlay)
5988 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5989 (setq org-agenda-overriding-restriction nil)
5990 (setq org-agenda-restrict nil)
5991 (put 'org-agenda-files 'org-restrict nil)
5992 (move-marker org-agenda-restrict-begin nil)
5993 (move-marker org-agenda-restrict-end nil)
5994 (setq current-prefix-arg nil)
5995 (message "Agenda restriction lock removed")
5996 (or noupdate (org-agenda-maybe-redo)))
5998 (defun org-agenda-maybe-redo ()
5999 "If there is any window showing the agenda view, update it."
6000 (let ((w (get-buffer-window org-agenda-buffer-name t))
6001 (w0 (selected-window)))
6002 (when w
6003 (select-window w)
6004 (org-agenda-redo)
6005 (select-window w0)
6006 (if org-agenda-overriding-restriction
6007 (message "Agenda view shifted to new %s restriction"
6008 org-agenda-overriding-restriction)
6009 (message "Agenda restriction lock removed")))))
6011 ;;; Agenda commands
6013 (defun org-agenda-check-type (error &rest types)
6014 "Check if agenda buffer is of allowed type.
6015 If ERROR is non-nil, throw an error, otherwise just return nil."
6016 (if (memq org-agenda-type types)
6018 (if error
6019 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6020 nil)))
6022 (defun org-agenda-quit ()
6023 "Exit agenda by removing the window or the buffer."
6024 (interactive)
6025 (if org-agenda-columns-active
6026 (org-columns-quit)
6027 (let ((buf (current-buffer)))
6028 (if (eq org-agenda-window-setup 'other-frame)
6029 (progn
6030 (kill-buffer buf)
6031 (org-agenda-reset-markers)
6032 (org-columns-remove-overlays)
6033 (setq org-agenda-archives-mode nil)
6034 (delete-frame))
6035 (and (not (eq org-agenda-window-setup 'current-window))
6036 (not (one-window-p))
6037 (delete-window))
6038 (kill-buffer buf)
6039 (org-agenda-reset-markers)
6040 (org-columns-remove-overlays)
6041 (setq org-agenda-archives-mode nil)))
6042 ;; Maybe restore the pre-agenda window configuration.
6043 (and org-agenda-restore-windows-after-quit
6044 (not (eq org-agenda-window-setup 'other-frame))
6045 org-pre-agenda-window-conf
6046 (set-window-configuration org-pre-agenda-window-conf))))
6048 (defun org-agenda-exit ()
6049 "Exit agenda by removing the window or the buffer.
6050 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6051 Org-mode buffers visited directly by the user will not be touched."
6052 (interactive)
6053 (org-release-buffers org-agenda-new-buffers)
6054 (setq org-agenda-new-buffers nil)
6055 (org-agenda-quit))
6057 (defun org-agenda-execute (arg)
6058 "Execute another agenda command, keeping same window.
6059 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6060 in the agenda."
6061 (interactive "P")
6062 (let ((org-agenda-window-setup 'current-window))
6063 (org-agenda arg)))
6065 (defun org-agenda-redo ()
6066 "Rebuild Agenda.
6067 When this is the global TODO list, a prefix argument will be interpreted."
6068 (interactive)
6069 (let* ((org-agenda-keep-modes t)
6070 (filter org-agenda-filter)
6071 (preset (get 'org-agenda-filter :preset-filter))
6072 (org-agenda-filter-while-redo (or filter preset))
6073 (cols org-agenda-columns-active)
6074 (line (org-current-line))
6075 (window-line (- line (org-current-line (window-start))))
6076 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6077 (put 'org-agenda-filter :preset-filter nil)
6078 (and cols (org-columns-quit))
6079 (message "Rebuilding agenda buffer...")
6080 (org-let lprops '(eval org-agenda-redo-command))
6081 (setq org-agenda-undo-list nil
6082 org-agenda-pending-undo-list nil)
6083 (message "Rebuilding agenda buffer...done")
6084 (put 'org-agenda-filter :preset-filter preset)
6085 (and (or filter preset) (org-agenda-filter-apply filter))
6086 (and cols (interactive-p) (org-agenda-columns))
6087 (org-goto-line line)
6088 (recenter window-line)))
6091 (defvar org-global-tags-completion-table nil)
6092 (defvar org-agenda-filter-form nil)
6093 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6094 "Keep only those lines in the agenda buffer that have a specific tag.
6095 The tag is selected with its fast selection letter, as configured.
6096 With prefix argument STRIP, remove all lines that do have the tag.
6097 A lisp caller can specify CHAR. NARROW means that the new tag should be
6098 used to narrow the search - the interactive user can also press `-' or `+'
6099 to switch to narrowing."
6100 (interactive "P")
6101 (let* ((alist org-tag-alist-for-agenda)
6102 (tag-chars (mapconcat
6103 (lambda (x) (if (and (not (symbolp (car x)))
6104 (cdr x))
6105 (char-to-string (cdr x))
6106 ""))
6107 alist ""))
6108 (efforts (org-split-string
6109 (or (cdr (assoc (concat org-effort-property "_ALL")
6110 org-global-properties))
6111 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6112 "")))
6113 (effort-op org-agenda-filter-effort-default-operator)
6114 (effort-prompt "")
6115 (inhibit-read-only t)
6116 (current org-agenda-filter)
6117 maybe-refresh a n tag)
6118 (unless char
6119 (message
6120 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6121 (if narrow "Narrow" "Filter") tag-chars
6122 (if org-agenda-auto-exclude-function "[RET], " ""))
6123 (setq char (read-char)))
6124 (when (member char '(?+ ?-))
6125 ;; Narrowing down
6126 (cond ((equal char ?-) (setq strip t narrow t))
6127 ((equal char ?+) (setq strip nil narrow t)))
6128 (message
6129 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6130 (setq char (read-char)))
6131 (when (member char '(?< ?> ?= ??))
6132 ;; An effort operator
6133 (setq effort-op (char-to-string char))
6134 (setq alist nil) ; to make sure it will be interpreted as effort.
6135 (unless (equal char ??)
6136 (loop for i from 0 to 9 do
6137 (setq effort-prompt
6138 (concat
6139 effort-prompt " ["
6140 (if (= i 9) "0" (int-to-string (1+ i)))
6141 "]" (nth i efforts))))
6142 (message "Effort%s: %s " effort-op effort-prompt)
6143 (setq char (read-char))
6144 (when (or (< char ?0) (> char ?9))
6145 (error "Need 1-9,0 to select effort" ))))
6146 (when (equal char ?\t)
6147 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6148 (org-set-local 'org-global-tags-completion-table
6149 (org-global-tags-completion-table)))
6150 (let ((completion-ignore-case t))
6151 (setq tag (org-icompleting-read
6152 "Tag: " org-global-tags-completion-table))))
6153 (cond
6154 ((equal char ?\r)
6155 (org-agenda-filter-by-tag-show-all)
6156 (when org-agenda-auto-exclude-function
6157 (setq org-agenda-filter '())
6158 (dolist (tag (org-agenda-get-represented-tags))
6159 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6160 (if modifier
6161 (push modifier org-agenda-filter))))
6162 (if (not (null org-agenda-filter))
6163 (org-agenda-filter-apply org-agenda-filter)))
6164 (setq maybe-refresh t))
6165 ((equal char ?/)
6166 (org-agenda-filter-by-tag-show-all)
6167 (when (get 'org-agenda-filter :preset-filter)
6168 (org-agenda-filter-apply org-agenda-filter))
6169 (setq maybe-refresh t))
6170 ((or (equal char ?\ )
6171 (setq a (rassoc char alist))
6172 (and (>= char ?0) (<= char ?9)
6173 (setq n (if (= char ?0) 9 (- char ?0 1))
6174 tag (concat effort-op (nth n efforts))
6175 a (cons tag nil)))
6176 (and (= char ??)
6177 (setq tag "?eff")
6178 a (cons tag nil))
6179 (and tag (setq a (cons tag nil))))
6180 (org-agenda-filter-by-tag-show-all)
6181 (setq tag (car a))
6182 (setq org-agenda-filter
6183 (cons (concat (if strip "-" "+") tag)
6184 (if narrow current nil)))
6185 (org-agenda-filter-apply org-agenda-filter)
6186 (setq maybe-refresh t))
6187 (t (error "Invalid tag selection character %c" char)))
6188 (when (and maybe-refresh
6189 (eq org-agenda-clockreport-mode 'with-filter))
6190 (org-agenda-redo))))
6192 (defun org-agenda-get-represented-tags ()
6193 "Get a list of all tags currently represented in the agenda."
6194 (let (p tags)
6195 (save-excursion
6196 (goto-char (point-min))
6197 (while (setq p (next-single-property-change (point) 'tags))
6198 (goto-char p)
6199 (mapc (lambda (x) (add-to-list 'tags x))
6200 (get-text-property (point) 'tags))))
6201 tags))
6203 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6204 "Refine the current filter. See `org-agenda-filter-by-tag."
6205 (interactive "P")
6206 (org-agenda-filter-by-tag strip char 'refine))
6208 (defun org-agenda-filter-make-matcher ()
6209 "Create the form that tests a line for the agenda filter."
6210 (let (f f1)
6211 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6212 org-agenda-filter))
6213 (if (member x '("-" "+"))
6214 (setq f1 (if (equal x "-") 'tags '(not tags)))
6215 (if (string-match "[<=>?]" x)
6216 (setq f1 (org-agenda-filter-effort-form x))
6217 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6218 (if (equal (string-to-char x) ?-)
6219 (setq f1 (list 'not f1))))
6220 (push f1 f))
6221 (cons 'and (nreverse f))))
6223 (defun org-agenda-filter-effort-form (e)
6224 "Return the form to compare the effort of the current line with what E says.
6225 E looks like \"+<2:25\"."
6226 (let (op)
6227 (setq e (substring e 1))
6228 (setq op (string-to-char e) e (substring e 1))
6229 (setq op (cond ((equal op ?<) '<=)
6230 ((equal op ?>) '>=)
6231 ((equal op ??) op)
6232 (t '=)))
6233 (list 'org-agenda-compare-effort (list 'quote op)
6234 (org-duration-string-to-minutes e))))
6236 (defun org-agenda-compare-effort (op value)
6237 "Compare the effort of the current line with VALUE, using OP.
6238 If the line does not have an effort defined, return nil."
6239 (let ((eff (org-get-at-bol 'effort-minutes)))
6240 (if (equal op ??)
6241 (not eff)
6242 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6243 value))))
6245 (defun org-agenda-filter-apply (filter)
6246 "Set FILTER as the new agenda filter and apply it."
6247 (let (tags)
6248 (setq org-agenda-filter filter
6249 org-agenda-filter-form (org-agenda-filter-make-matcher))
6250 (org-agenda-set-mode-name)
6251 (save-excursion
6252 (goto-char (point-min))
6253 (while (not (eobp))
6254 (if (org-get-at-bol 'org-marker)
6255 (progn
6256 (setq tags (org-get-at-bol 'tags)) ; used in eval
6257 (if (not (eval org-agenda-filter-form))
6258 (org-agenda-filter-by-tag-hide-line))
6259 (beginning-of-line 2))
6260 (beginning-of-line 2))))
6261 (if (get-char-property (point) 'invisible)
6262 (org-agenda-previous-line))))
6264 (defun org-agenda-filter-by-tag-hide-line ()
6265 (let (ov)
6266 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6267 (point-at-eol)))
6268 (overlay-put ov 'invisible t)
6269 (overlay-put ov 'type 'tags-filter)
6270 (push ov org-agenda-filter-overlays)))
6272 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6273 (setq pos (or pos (point)))
6274 (save-excursion
6275 (dolist (ov (overlays-at pos))
6276 (when (and (overlay-get ov 'invisible)
6277 (eq (overlay-get ov 'type) 'tags-filter))
6278 (goto-char pos)
6279 (if (< (overlay-start ov) (point-at-eol))
6280 (move-overlay ov (point-at-eol)
6281 (overlay-end ov)))))))
6283 (defun org-agenda-filter-by-tag-show-all ()
6284 (mapc 'delete-overlay org-agenda-filter-overlays)
6285 (setq org-agenda-filter-overlays nil)
6286 (setq org-agenda-filter nil)
6287 (setq org-agenda-filter-form nil)
6288 (org-agenda-set-mode-name))
6290 (defun org-agenda-manipulate-query-add ()
6291 "Manipulate the query by adding a search term with positive selection.
6292 Positive selection means the term must be matched for selection of an entry."
6293 (interactive)
6294 (org-agenda-manipulate-query ?\[))
6295 (defun org-agenda-manipulate-query-subtract ()
6296 "Manipulate the query by adding a search term with negative selection.
6297 Negative selection means term must not be matched for selection of an entry."
6298 (interactive)
6299 (org-agenda-manipulate-query ?\]))
6300 (defun org-agenda-manipulate-query-add-re ()
6301 "Manipulate the query by adding a search regexp with positive selection.
6302 Positive selection means the regexp must match for selection of an entry."
6303 (interactive)
6304 (org-agenda-manipulate-query ?\{))
6305 (defun org-agenda-manipulate-query-subtract-re ()
6306 "Manipulate the query by adding a search regexp with negative selection.
6307 Negative selection means regexp must not match for selection of an entry."
6308 (interactive)
6309 (org-agenda-manipulate-query ?\}))
6310 (defun org-agenda-manipulate-query (char)
6311 (cond
6312 ((memq org-agenda-type '(timeline agenda))
6313 (let ((org-agenda-include-inactive-timestamps t))
6314 (org-agenda-redo))
6315 (message "Display now includes inactive timestamps as well"))
6316 ((eq org-agenda-type 'search)
6317 (org-add-to-string
6318 'org-agenda-query-string
6319 (if org-agenda-last-search-view-search-was-boolean
6320 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6321 (?\{ . " +{}") (?\} . " -{}"))))
6322 " "))
6323 (setq org-agenda-redo-command
6324 (list 'org-search-view
6325 org-todo-only
6326 org-agenda-query-string
6327 (+ (length org-agenda-query-string)
6328 (if (member char '(?\{ ?\})) 0 1))))
6329 (set-register org-agenda-query-register org-agenda-query-string)
6330 (org-agenda-redo))
6331 (t (error "Cannot manipulate query for %s-type agenda buffers"
6332 org-agenda-type))))
6334 (defun org-add-to-string (var string)
6335 (set var (concat (symbol-value var) string)))
6337 (defun org-agenda-goto-date (date)
6338 "Jump to DATE in agenda."
6339 (interactive (list (let ((org-read-date-prefer-future
6340 (eval org-agenda-jump-prefer-future)))
6341 (org-read-date))))
6342 (org-agenda-list nil date))
6344 (defun org-agenda-goto-today ()
6345 "Go to today."
6346 (interactive)
6347 (org-agenda-check-type t 'timeline 'agenda)
6348 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6349 (cond
6350 (tdpos (goto-char tdpos))
6351 ((eq org-agenda-type 'agenda)
6352 (let* ((sd (org-agenda-compute-starting-span
6353 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6354 (org-agenda-overriding-arguments org-agenda-last-arguments))
6355 (setf (nth 1 org-agenda-overriding-arguments) sd)
6356 (org-agenda-redo)
6357 (org-agenda-find-same-or-today-or-agenda)))
6358 (t (error "Cannot find today")))))
6360 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6361 (goto-char
6362 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6363 (text-property-any (point-min) (point-max) 'org-today t)
6364 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6365 (point-min))))
6367 (defun org-agenda-later (arg)
6368 "Go forward in time by thee current span.
6369 With prefix ARG, go forward that many times the current span."
6370 (interactive "p")
6371 (org-agenda-check-type t 'agenda)
6372 (let* ((span org-agenda-current-span)
6373 (sd org-starting-day)
6374 (greg (calendar-gregorian-from-absolute sd))
6375 (cnt (org-get-at-bol 'org-day-cnt))
6376 greg2)
6377 (cond
6378 ((eq span 'day)
6379 (setq sd (+ arg sd)))
6380 ((eq span 'week)
6381 (setq sd (+ (* 7 arg) sd)))
6382 ((eq span 'month)
6383 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6384 sd (calendar-absolute-from-gregorian greg2))
6385 (setcar greg2 (1+ (car greg2))))
6386 ((eq span 'year)
6387 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6388 sd (calendar-absolute-from-gregorian greg2))
6389 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6391 (setq sd (+ (* span arg) sd))))
6392 (let ((org-agenda-overriding-arguments
6393 (list (car org-agenda-last-arguments) sd span t)))
6394 (org-agenda-redo)
6395 (org-agenda-find-same-or-today-or-agenda cnt))))
6397 (defun org-agenda-earlier (arg)
6398 "Go backward in time by the current span.
6399 With prefix ARG, go backward that many times the current span."
6400 (interactive "p")
6401 (org-agenda-later (- arg)))
6403 (defun org-agenda-view-mode-dispatch ()
6404 "Call one of the view mode commands."
6405 (interactive)
6406 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6407 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6408 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6409 [E]ntryText")
6410 (let ((a (read-char-exclusive)))
6411 (case a
6412 (?\ (call-interactively 'org-agenda-reset-view))
6413 (?d (call-interactively 'org-agenda-day-view))
6414 (?w (call-interactively 'org-agenda-week-view))
6415 (?m (call-interactively 'org-agenda-month-view))
6416 (?y (call-interactively 'org-agenda-year-view))
6417 (?l (call-interactively 'org-agenda-log-mode))
6418 (?L (org-agenda-log-mode '(4)))
6419 (?c (org-agenda-log-mode 'clockcheck))
6420 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6421 (?a (call-interactively 'org-agenda-archives-mode))
6422 (?A (org-agenda-archives-mode 'files))
6423 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6424 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6425 (?G (call-interactively 'org-agenda-toggle-time-grid))
6426 (?D (call-interactively 'org-agenda-toggle-diary))
6427 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6428 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6429 (org-agenda-check-type t 'timeline 'agenda)
6430 (org-agenda-redo))
6431 (message "Display now includes inactive timestamps as well"))
6432 (?q (message "Abort"))
6433 (otherwise (error "Invalid key" )))))
6435 (defun org-agenda-reset-view ()
6436 "Switch to default view for agenda."
6437 (interactive)
6438 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6439 (defun org-agenda-day-view (&optional day-of-year)
6440 "Switch to daily view for agenda.
6441 With argument DAY-OF-YEAR, switch to that day of the year."
6442 (interactive "P")
6443 (org-agenda-change-time-span 'day day-of-year))
6444 (defun org-agenda-week-view (&optional iso-week)
6445 "Switch to daily view for agenda.
6446 With argument ISO-WEEK, switch to the corresponding ISO week.
6447 If ISO-WEEK has more then 2 digits, only the last two encode the
6448 week. Any digits before this encode a year. So 200712 means
6449 week 12 of year 2007. Years in the range 1938-2037 can also be
6450 written as 2-digit years."
6451 (interactive "P")
6452 (org-agenda-change-time-span 'week iso-week))
6453 (defun org-agenda-month-view (&optional month)
6454 "Switch to monthly view for agenda.
6455 With argument MONTH, switch to that month."
6456 (interactive "P")
6457 (org-agenda-change-time-span 'month month))
6458 (defun org-agenda-year-view (&optional year)
6459 "Switch to yearly view for agenda.
6460 With argument YEAR, switch to that year.
6461 If MONTH has more then 2 digits, only the last two encode the
6462 month. Any digits before this encode a year. So 200712 means
6463 December year 2007. Years in the range 1938-2037 can also be
6464 written as 2-digit years."
6465 (interactive "P")
6466 (when year
6467 (setq year (org-small-year-to-year year)))
6468 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6469 (org-agenda-change-time-span 'year year)
6470 (error "Abort")))
6472 (defun org-agenda-change-time-span (span &optional n)
6473 "Change the agenda view to SPAN.
6474 SPAN may be `day', `week', `month', `year'."
6475 (org-agenda-check-type t 'agenda)
6476 (if (and (not n) (equal org-agenda-current-span span))
6477 (error "Viewing span is already \"%s\"" span))
6478 (let* ((sd (or (org-get-at-bol 'day)
6479 org-starting-day))
6480 (sd (org-agenda-compute-starting-span sd span n))
6481 (org-agenda-overriding-arguments
6482 (or org-agenda-overriding-arguments
6483 (list (car org-agenda-last-arguments) sd span t))))
6484 (org-agenda-redo)
6485 (org-agenda-find-same-or-today-or-agenda))
6486 (org-agenda-set-mode-name)
6487 (message "Switched to %s view" span))
6489 (defun org-agenda-compute-starting-span (sd span &optional n)
6490 "Compute starting date for agenda.
6491 SPAN may be `day', `week', `month', `year'. The return value
6492 is a cons cell with the starting date and the number of days,
6493 so that the date SD will be in that range."
6494 (let* ((greg (calendar-gregorian-from-absolute sd))
6495 (dg (nth 1 greg))
6496 (mg (car greg))
6497 (yg (nth 2 greg)))
6498 (cond
6499 ((eq span 'day)
6500 (when n
6501 (setq sd (+ (calendar-absolute-from-gregorian
6502 (list mg 1 yg))
6503 n -1))))
6504 ((eq span 'week)
6505 (let* ((nt (calendar-day-of-week
6506 (calendar-gregorian-from-absolute sd)))
6507 (d (if org-agenda-start-on-weekday
6508 (- nt org-agenda-start-on-weekday)
6511 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6512 (when n
6513 (require 'cal-iso)
6514 (when (> n 99)
6515 (setq y1 (org-small-year-to-year (/ n 100))
6516 n (mod n 100)))
6517 (setq sd
6518 (calendar-absolute-from-iso
6519 (list n 1
6520 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6521 ((eq span 'month)
6522 (let (y1)
6523 (when (and n (> n 99))
6524 (setq y1 (org-small-year-to-year (/ n 100))
6525 n (mod n 100)))
6526 (setq sd (calendar-absolute-from-gregorian
6527 (list (or n mg) 1 (or y1 yg))))))
6528 ((eq span 'year)
6529 (setq sd (calendar-absolute-from-gregorian
6530 (list 1 1 (or n yg))))))
6531 sd))
6533 (defun org-agenda-next-date-line (&optional arg)
6534 "Jump to the next line indicating a date in agenda buffer."
6535 (interactive "p")
6536 (org-agenda-check-type t 'agenda 'timeline)
6537 (beginning-of-line 1)
6538 ;; This does not work if user makes date format that starts with a blank
6539 (if (looking-at "^\\S-") (forward-char 1))
6540 (if (not (re-search-forward "^\\S-" nil t arg))
6541 (progn
6542 (backward-char 1)
6543 (error "No next date after this line in this buffer")))
6544 (goto-char (match-beginning 0)))
6546 (defun org-agenda-previous-date-line (&optional arg)
6547 "Jump to the previous line indicating a date in agenda buffer."
6548 (interactive "p")
6549 (org-agenda-check-type t 'agenda 'timeline)
6550 (beginning-of-line 1)
6551 (if (not (re-search-backward "^\\S-" nil t arg))
6552 (error "No previous date before this line in this buffer")))
6554 ;; Initialize the highlight
6555 (defvar org-hl (make-overlay 1 1))
6556 (overlay-put org-hl 'face 'highlight)
6558 (defun org-highlight (begin end &optional buffer)
6559 "Highlight a region with overlay."
6560 (move-overlay org-hl begin end (or buffer (current-buffer))))
6562 (defun org-unhighlight ()
6563 "Detach overlay INDEX."
6564 (org-detach-overlay org-hl))
6566 ;; FIXME this is currently not used.
6567 (defun org-highlight-until-next-command (beg end &optional buffer)
6568 "Move the highlight overlay to BEG/END, remove it before the next command."
6569 (org-highlight beg end buffer)
6570 (add-hook 'pre-command-hook 'org-unhighlight-once))
6571 (defun org-unhighlight-once ()
6572 "Remove the highlight from its position, and this function from the hook."
6573 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6574 (org-unhighlight))
6576 (defun org-agenda-follow-mode ()
6577 "Toggle follow mode in an agenda buffer."
6578 (interactive)
6579 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6580 (org-agenda-set-mode-name)
6581 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6582 (org-agenda-show))
6583 (message "Follow mode is %s"
6584 (if org-agenda-follow-mode "on" "off")))
6586 (defun org-agenda-entry-text-mode (&optional arg)
6587 "Toggle entry text mode in an agenda buffer."
6588 (interactive "P")
6589 (setq org-agenda-entry-text-mode (or (integerp arg)
6590 (not org-agenda-entry-text-mode)))
6591 (org-agenda-entry-text-hide)
6592 (and org-agenda-entry-text-mode
6593 (let ((org-agenda-entry-text-maxlines
6594 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6595 (org-agenda-entry-text-show)))
6596 (org-agenda-set-mode-name)
6597 (message "Entry text mode is %s. Maximum number of lines is %d"
6598 (if org-agenda-entry-text-mode "on" "off")
6599 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6601 (defun org-agenda-clockreport-mode (&optional with-filter)
6602 "Toggle clocktable mode in an agenda buffer.
6603 With prefix arg WITH-FILTER, make the clocktable respect the current
6604 agenda filter."
6605 (interactive "P")
6606 (org-agenda-check-type t 'agenda)
6607 (if with-filter
6608 (setq org-agenda-clockreport-mode 'with-filter)
6609 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6610 (org-agenda-set-mode-name)
6611 (org-agenda-redo)
6612 (message "Clocktable mode is %s"
6613 (if org-agenda-clockreport-mode "on" "off")))
6615 (defun org-agenda-log-mode (&optional special)
6616 "Toggle log mode in an agenda buffer.
6617 With argument SPECIAL, show all possible log items, not only the ones
6618 configured in `org-agenda-log-mode-items'.
6619 With a double `C-u' prefix arg, show *only* log items, nothing else."
6620 (interactive "P")
6621 (org-agenda-check-type t 'agenda 'timeline)
6622 (setq org-agenda-show-log
6623 (cond
6624 ((equal special '(16)) 'only)
6625 ((eq special 'clockcheck)
6626 (if (eq org-agenda-show-log 'clockcheck)
6627 nil 'clockcheck))
6628 (special '(closed clock state))
6629 (t (not org-agenda-show-log))))
6630 (org-agenda-set-mode-name)
6631 (org-agenda-redo)
6632 (message "Log mode is %s"
6633 (if org-agenda-show-log "on" "off")))
6635 (defun org-agenda-archives-mode (&optional with-files)
6636 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6637 When called with a prefix argument, include all archive files as well."
6638 (interactive "P")
6639 (setq org-agenda-archives-mode
6640 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6641 (org-agenda-set-mode-name)
6642 (org-agenda-redo)
6643 (message
6644 "%s"
6645 (cond
6646 ((eq org-agenda-archives-mode nil)
6647 "No archives are included")
6648 ((eq org-agenda-archives-mode 'trees)
6649 (format "Trees with :%s: tag are included" org-archive-tag))
6650 ((eq org-agenda-archives-mode t)
6651 (format "Trees with :%s: tag and all active archive files are included"
6652 org-archive-tag)))))
6654 (defun org-agenda-toggle-diary ()
6655 "Toggle diary inclusion in an agenda buffer."
6656 (interactive)
6657 (org-agenda-check-type t 'agenda)
6658 (setq org-agenda-include-diary (not org-agenda-include-diary))
6659 (org-agenda-redo)
6660 (org-agenda-set-mode-name)
6661 (message "Diary inclusion turned %s"
6662 (if org-agenda-include-diary "on" "off")))
6664 (defun org-agenda-toggle-deadlines ()
6665 "Toggle inclusion of entries with a deadline in an agenda buffer."
6666 (interactive)
6667 (org-agenda-check-type t 'agenda)
6668 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6669 (org-agenda-redo)
6670 (org-agenda-set-mode-name)
6671 (message "Deadlines inclusion turned %s"
6672 (if org-agenda-include-deadlines "on" "off")))
6674 (defun org-agenda-toggle-time-grid ()
6675 "Toggle time grid in an agenda buffer."
6676 (interactive)
6677 (org-agenda-check-type t 'agenda)
6678 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6679 (org-agenda-redo)
6680 (org-agenda-set-mode-name)
6681 (message "Time-grid turned %s"
6682 (if org-agenda-use-time-grid "on" "off")))
6684 (defun org-agenda-set-mode-name ()
6685 "Set the mode name to indicate all the small mode settings."
6686 (setq mode-name
6687 (list "Org-Agenda"
6688 (if (get 'org-agenda-files 'org-restrict) " []" "")
6690 '(:eval (org-agenda-span-name org-agenda-current-span))
6691 (if org-agenda-follow-mode " Follow" "")
6692 (if org-agenda-entry-text-mode " ETxt" "")
6693 (if org-agenda-include-diary " Diary" "")
6694 (if org-agenda-include-deadlines " Ddl" "")
6695 (if org-agenda-use-time-grid " Grid" "")
6696 (if (and (boundp 'org-habit-show-habits)
6697 org-habit-show-habits) " Habit" "")
6698 (cond
6699 ((consp org-agenda-show-log) " LogAll")
6700 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6701 (org-agenda-show-log " Log")
6702 (t ""))
6703 (if (or org-agenda-filter (get 'org-agenda-filter
6704 :preset-filter))
6705 (concat " {" (mapconcat
6706 'identity
6707 (append (get 'org-agenda-filter
6708 :preset-filter)
6709 org-agenda-filter) "") "}")
6711 (if org-agenda-archives-mode
6712 (if (eq org-agenda-archives-mode t)
6713 " Archives"
6714 (format " :%s:" org-archive-tag))
6716 (if org-agenda-clockreport-mode
6717 (if (eq org-agenda-clockreport-mode 'with-filter)
6718 " Clock{}" " Clock")
6719 "")))
6720 (force-mode-line-update))
6722 (defun org-agenda-post-command-hook ()
6723 (setq org-agenda-type
6724 (or (get-text-property (point) 'org-agenda-type)
6725 (get-text-property (max (point-min) (1- (point)))
6726 'org-agenda-type))))
6728 (defun org-agenda-next-line ()
6729 "Move cursor to the next line, and show if follow mode is active."
6730 (interactive)
6731 (call-interactively 'next-line)
6732 (org-agenda-do-context-action))
6734 (defun org-agenda-previous-line ()
6735 "Move cursor to the previous line, and show if follow-mode is active."
6736 (interactive)
6737 (call-interactively 'previous-line)
6738 (org-agenda-do-context-action))
6740 (defun org-agenda-do-context-action ()
6741 "Show outline path and, maybe, follow mode window."
6742 (let ((m (org-get-at-bol 'org-marker)))
6743 (if (and org-agenda-follow-mode m)
6744 (org-agenda-show))
6745 (if (and m org-agenda-show-outline-path)
6746 (org-with-point-at m
6747 (org-display-outline-path t)))))
6749 (defun org-agenda-show-priority ()
6750 "Show the priority of the current item.
6751 This priority is composed of the main priority given with the [#A] cookies,
6752 and by additional input from the age of a schedules or deadline entry."
6753 (interactive)
6754 (let* ((pri (org-get-at-bol 'priority)))
6755 (message "Priority is %d" (if pri pri -1000))))
6757 (defun org-agenda-show-tags ()
6758 "Show the tags applicable to the current item."
6759 (interactive)
6760 (let* ((tags (org-get-at-bol 'tags)))
6761 (if tags
6762 (message "Tags are :%s:"
6763 (org-no-properties (mapconcat 'identity tags ":")))
6764 (message "No tags associated with this line"))))
6766 (defun org-agenda-goto (&optional highlight)
6767 "Go to the Org-mode file which contains the item at point."
6768 (interactive)
6769 (let* ((marker (or (org-get-at-bol 'org-marker)
6770 (org-agenda-error)))
6771 (buffer (marker-buffer marker))
6772 (pos (marker-position marker)))
6773 (switch-to-buffer-other-window buffer)
6774 (widen)
6775 (push-mark)
6776 (goto-char pos)
6777 (when (org-mode-p)
6778 (org-show-context 'agenda)
6779 (save-excursion
6780 (and (outline-next-heading)
6781 (org-flag-heading nil)))) ; show the next heading
6782 (when (outline-invisible-p)
6783 (show-entry)) ; display invisible text
6784 (recenter (/ (window-height) 2))
6785 (run-hooks 'org-agenda-after-show-hook)
6786 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6788 (defvar org-agenda-after-show-hook nil
6789 "Normal hook run after an item has been shown from the agenda.
6790 Point is in the buffer where the item originated.")
6792 (defun org-agenda-kill ()
6793 "Kill the entry or subtree belonging to the current agenda entry."
6794 (interactive)
6795 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6796 (let* ((marker (or (org-get-at-bol 'org-marker)
6797 (org-agenda-error)))
6798 (buffer (marker-buffer marker))
6799 (pos (marker-position marker))
6800 (type (org-get-at-bol 'type))
6801 dbeg dend (n 0) conf)
6802 (org-with-remote-undo buffer
6803 (with-current-buffer buffer
6804 (save-excursion
6805 (goto-char pos)
6806 (if (and (org-mode-p) (not (member type '("sexp"))))
6807 (setq dbeg (progn (org-back-to-heading t) (point))
6808 dend (org-end-of-subtree t t))
6809 (setq dbeg (point-at-bol)
6810 dend (min (point-max) (1+ (point-at-eol)))))
6811 (goto-char dbeg)
6812 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6813 (setq conf (or (eq t org-agenda-confirm-kill)
6814 (and (numberp org-agenda-confirm-kill)
6815 (> n org-agenda-confirm-kill))))
6816 (and conf
6817 (not (y-or-n-p
6818 (format "Delete entry with %d lines in buffer \"%s\"? "
6819 n (buffer-name buffer))))
6820 (error "Abort"))
6821 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6822 (with-current-buffer buffer (delete-region dbeg dend))
6823 (message "Agenda item and source killed"))))
6825 (defvar org-archive-default-command)
6826 (defun org-agenda-archive-default ()
6827 "Archive the entry or subtree belonging to the current agenda entry."
6828 (interactive)
6829 (require 'org-archive)
6830 (org-agenda-archive-with org-archive-default-command))
6832 (defun org-agenda-archive-default-with-confirmation ()
6833 "Archive the entry or subtree belonging to the current agenda entry."
6834 (interactive)
6835 (require 'org-archive)
6836 (org-agenda-archive-with org-archive-default-command 'confirm))
6838 (defun org-agenda-archive ()
6839 "Archive the entry or subtree belonging to the current agenda entry."
6840 (interactive)
6841 (org-agenda-archive-with 'org-archive-subtree))
6843 (defun org-agenda-archive-to-archive-sibling ()
6844 "Move the entry to the archive sibling."
6845 (interactive)
6846 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6848 (defun org-agenda-archive-with (cmd &optional confirm)
6849 "Move the entry to the archive sibling."
6850 (interactive)
6851 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6852 (let* ((marker (or (org-get-at-bol 'org-marker)
6853 (org-agenda-error)))
6854 (buffer (marker-buffer marker))
6855 (pos (marker-position marker)))
6856 (org-with-remote-undo buffer
6857 (with-current-buffer buffer
6858 (if (org-mode-p)
6859 (if (and confirm
6860 (not (y-or-n-p "Archive this subtree or entry? ")))
6861 (error "Abort")
6862 (save-excursion
6863 (goto-char pos)
6864 (org-remove-subtree-entries-from-agenda)
6865 (org-back-to-heading t)
6866 (funcall cmd)))
6867 (error "Archiving works only in Org-mode files"))))))
6869 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6870 "Remove all lines in the agenda that correspond to a given subtree.
6871 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6872 If this information is not given, the function uses the tree at point."
6873 (let ((buf (or buf (current-buffer))) m p)
6874 (save-excursion
6875 (unless (and beg end)
6876 (org-back-to-heading t)
6877 (setq beg (point))
6878 (org-end-of-subtree t)
6879 (setq end (point)))
6880 (set-buffer (get-buffer org-agenda-buffer-name))
6881 (save-excursion
6882 (goto-char (point-max))
6883 (beginning-of-line 1)
6884 (while (not (bobp))
6885 (when (and (setq m (org-get-at-bol 'org-marker))
6886 (equal buf (marker-buffer m))
6887 (setq p (marker-position m))
6888 (>= p beg)
6889 (< p end))
6890 (let ((inhibit-read-only t))
6891 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6892 (beginning-of-line 0))))))
6894 (defun org-agenda-refile (&optional goto rfloc no-update)
6895 "Refile the item at point."
6896 (interactive "P")
6897 (if (equal goto '(16))
6898 (org-refile-goto-last-stored)
6899 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6900 (org-agenda-error)))
6901 (buffer (marker-buffer marker))
6902 (pos (marker-position marker))
6903 (rfloc (or rfloc
6904 (org-refile-get-location
6905 (if goto "Goto" "Refile to") buffer
6906 org-refile-allow-creating-parent-nodes))))
6907 (with-current-buffer buffer
6908 (save-excursion
6909 (save-restriction
6910 (widen)
6911 (goto-char marker)
6912 (org-remove-subtree-entries-from-agenda)
6913 (org-refile goto buffer rfloc)))))
6914 (unless no-update (org-agenda-redo))))
6916 (defun org-agenda-open-link (&optional arg)
6917 "Follow the link in the current line, if any.
6918 This looks for a link in the displayed line in the agenda. It also looks
6919 at the text of the entry itself."
6920 (interactive "P")
6921 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6922 (org-get-at-bol 'org-marker)))
6923 (buffer (and marker (marker-buffer marker)))
6924 (prefix (buffer-substring
6925 (point-at-bol) (point-at-eol))))
6926 (cond
6927 (buffer
6928 (with-current-buffer buffer
6929 (save-excursion
6930 (save-restriction
6931 (widen)
6932 (goto-char marker)
6933 (org-offer-links-in-entry arg prefix)))))
6934 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6935 (save-excursion
6936 (beginning-of-line 1)
6937 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6938 (org-open-link-from-string (match-string 1)))
6939 (t (error "No link to open here")))))
6941 (defun org-agenda-copy-local-variable (var)
6942 "Get a variable from a referenced buffer and install it here."
6943 (let ((m (org-get-at-bol 'org-marker)))
6944 (when (and m (buffer-live-p (marker-buffer m)))
6945 (org-set-local var (with-current-buffer (marker-buffer m)
6946 (symbol-value var))))))
6948 (defun org-agenda-switch-to (&optional delete-other-windows)
6949 "Go to the Org-mode file which contains the item at point."
6950 (interactive)
6951 (if (and org-return-follows-link
6952 (not (org-get-at-bol 'org-marker))
6953 (org-in-regexp org-bracket-link-regexp))
6954 (org-open-link-from-string (match-string 0))
6955 (let* ((marker (or (org-get-at-bol 'org-marker)
6956 (org-agenda-error)))
6957 (buffer (marker-buffer marker))
6958 (pos (marker-position marker)))
6959 (switch-to-buffer buffer)
6960 (and delete-other-windows (delete-other-windows))
6961 (widen)
6962 (goto-char pos)
6963 (when (org-mode-p)
6964 (org-show-context 'agenda)
6965 (save-excursion
6966 (and (outline-next-heading)
6967 (org-flag-heading nil))) ; show the next heading
6968 (when (outline-invisible-p)
6969 (show-entry)))))) ; display invisible text
6971 (defun org-agenda-goto-mouse (ev)
6972 "Go to the Org-mode file which contains the item at the mouse click."
6973 (interactive "e")
6974 (mouse-set-point ev)
6975 (org-agenda-goto))
6977 (defun org-agenda-show (&optional full-entry)
6978 "Display the Org-mode file which contains the item at point.
6979 With prefix argument FULL-ENTRY, make the entire entry visible
6980 if it was hidden in the outline."
6981 (interactive "P")
6982 (let ((win (selected-window)))
6983 (if full-entry
6984 (let ((org-show-entry-below t))
6985 (org-agenda-goto t))
6986 (org-agenda-goto t))
6987 (select-window win)))
6989 (defvar org-agenda-show-window nil)
6990 (defun org-agenda-show-and-scroll-up ()
6991 "Display the Org-mode file which contains the item at point.
6992 When called repeatedly, scroll the window that is displaying the buffer."
6993 (interactive)
6994 (let ((win (selected-window)))
6995 (if (and (window-live-p org-agenda-show-window)
6996 (eq this-command last-command))
6997 (progn
6998 (select-window org-agenda-show-window)
6999 (ignore-errors (scroll-up)))
7000 (org-agenda-goto t)
7001 (show-subtree)
7002 (setq org-agenda-show-window (selected-window)))
7003 (select-window win)))
7005 (defun org-agenda-show-scroll-down ()
7006 "Scroll down the window showing the agenda."
7007 (interactive)
7008 (let ((win (selected-window)))
7009 (when (window-live-p org-agenda-show-window)
7010 (select-window org-agenda-show-window)
7011 (ignore-errors (scroll-down))
7012 (select-window win))))
7014 (defun org-agenda-show-1 (&optional more)
7015 "Display the Org-mode file which contains the item at point.
7016 The prefix arg selects the amount of information to display:
7018 0 hide the subtree
7019 1 just show the entry according to defaults.
7020 2 show the children view
7021 3 show the subtree view
7022 4 show the entire subtree and any LOGBOOK drawers
7023 5 show the entire subtree and any drawers
7024 With prefix argument FULL-ENTRY, make the entire entry visible
7025 if it was hidden in the outline."
7026 (interactive "p")
7027 (let ((win (selected-window)))
7028 (org-agenda-goto t)
7029 (org-recenter-heading 1)
7030 (cond
7031 ((= more 0)
7032 (hide-subtree)
7033 (save-excursion
7034 (org-back-to-heading)
7035 (run-hook-with-args 'org-cycle-hook 'folded))
7036 (message "Remote: FOLDED"))
7037 ((and (interactive-p) (= more 1))
7038 (message "Remote: show with default settings"))
7039 ((= more 2)
7040 (show-entry)
7041 (show-children)
7042 (save-excursion
7043 (org-back-to-heading)
7044 (run-hook-with-args 'org-cycle-hook 'children))
7045 (message "Remote: CHILDREN"))
7046 ((= more 3)
7047 (show-subtree)
7048 (save-excursion
7049 (org-back-to-heading)
7050 (run-hook-with-args 'org-cycle-hook 'subtree))
7051 (message "Remote: SUBTREE"))
7052 ((= more 4)
7053 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7054 (org-drawer-regexp
7055 (concat "^[ \t]*:\\("
7056 (mapconcat 'regexp-quote org-drawers "\\|")
7057 "\\):[ \t]*$")))
7058 (show-subtree)
7059 (save-excursion
7060 (org-back-to-heading)
7061 (org-cycle-hide-drawers 'subtree)))
7062 (message "Remote: SUBTREE AND LOGBOOK"))
7063 ((> more 4)
7064 (show-subtree)
7065 (message "Remote: SUBTREE AND ALL DRAWERS")))
7066 (select-window win)))
7068 (defun org-recenter-heading (n)
7069 (save-excursion
7070 (org-back-to-heading)
7071 (recenter n)))
7073 (defvar org-agenda-cycle-counter nil)
7074 (defun org-agenda-cycle-show (&optional n)
7075 "Show the current entry in another window, with default settings.
7076 Default settings are taken from `org-show-hierarchy-above' and siblings.
7077 When use repeatedly in immediate succession, the remote entry will cycle
7078 through visibility
7080 children -> subtree -> folded
7082 When called with a numeric prefix arg, that arg will be passed through to
7083 `org-agenda-show-1'. For the interpretation of that argument, see the
7084 docstring of `org-agenda-show-1'."
7085 (interactive "P")
7086 (if (integerp n)
7087 (setq org-agenda-cycle-counter n)
7088 (if (not (eq last-command this-command))
7089 (setq org-agenda-cycle-counter 1)
7090 (if (equal org-agenda-cycle-counter 0)
7091 (setq org-agenda-cycle-counter 2)
7092 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7093 (if (> org-agenda-cycle-counter 3)
7094 (setq org-agenda-cycle-counter 0)))))
7095 (org-agenda-show-1 org-agenda-cycle-counter))
7097 (defun org-agenda-recenter (arg)
7098 "Display the Org-mode file which contains the item at point and recenter."
7099 (interactive "P")
7100 (let ((win (selected-window)))
7101 (org-agenda-goto t)
7102 (recenter arg)
7103 (select-window win)))
7105 (defun org-agenda-show-mouse (ev)
7106 "Display the Org-mode file which contains the item at the mouse click."
7107 (interactive "e")
7108 (mouse-set-point ev)
7109 (org-agenda-show))
7111 (defun org-agenda-check-no-diary ()
7112 "Check if the entry is a diary link and abort if yes."
7113 (if (org-get-at-bol 'org-agenda-diary-link)
7114 (org-agenda-error)))
7116 (defun org-agenda-error ()
7117 (error "Command not allowed in this line"))
7119 (defun org-agenda-tree-to-indirect-buffer ()
7120 "Show the subtree corresponding to the current entry in an indirect buffer.
7121 This calls the command `org-tree-to-indirect-buffer' from the original
7122 Org-mode buffer.
7123 With numerical prefix arg ARG, go up to this level and then take that tree.
7124 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7125 use the dedicated frame)."
7126 (interactive)
7127 (org-agenda-check-no-diary)
7128 (let* ((marker (or (org-get-at-bol 'org-marker)
7129 (org-agenda-error)))
7130 (buffer (marker-buffer marker))
7131 (pos (marker-position marker)))
7132 (with-current-buffer buffer
7133 (save-excursion
7134 (goto-char pos)
7135 (call-interactively 'org-tree-to-indirect-buffer)))))
7137 (defvar org-last-heading-marker (make-marker)
7138 "Marker pointing to the headline that last changed its TODO state
7139 by a remote command from the agenda.")
7141 (defun org-agenda-todo-nextset ()
7142 "Switch TODO entry to next sequence."
7143 (interactive)
7144 (org-agenda-todo 'nextset))
7146 (defun org-agenda-todo-previousset ()
7147 "Switch TODO entry to previous sequence."
7148 (interactive)
7149 (org-agenda-todo 'previousset))
7151 (defun org-agenda-todo (&optional arg)
7152 "Cycle TODO state of line at point, also in Org-mode file.
7153 This changes the line at point, all other lines in the agenda referring to
7154 the same tree node, and the headline of the tree node in the Org-mode file."
7155 (interactive "P")
7156 (org-agenda-check-no-diary)
7157 (let* ((col (current-column))
7158 (marker (or (org-get-at-bol 'org-marker)
7159 (org-agenda-error)))
7160 (buffer (marker-buffer marker))
7161 (pos (marker-position marker))
7162 (hdmarker (org-get-at-bol 'org-hd-marker))
7163 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7164 (inhibit-read-only t)
7165 org-agenda-headline-snapshot-before-repeat newhead just-one)
7166 (org-with-remote-undo buffer
7167 (with-current-buffer buffer
7168 (widen)
7169 (goto-char pos)
7170 (org-show-context 'agenda)
7171 (save-excursion
7172 (and (outline-next-heading)
7173 (org-flag-heading nil))) ; show the next heading
7174 (let ((current-prefix-arg arg))
7175 (call-interactively 'org-todo))
7176 (and (bolp) (forward-char 1))
7177 (setq newhead (org-get-heading))
7178 (when (and (org-bound-and-true-p
7179 org-agenda-headline-snapshot-before-repeat)
7180 (not (equal org-agenda-headline-snapshot-before-repeat
7181 newhead))
7182 todayp)
7183 (setq newhead org-agenda-headline-snapshot-before-repeat
7184 just-one t))
7185 (save-excursion
7186 (org-back-to-heading)
7187 (move-marker org-last-heading-marker (point))))
7188 (beginning-of-line 1)
7189 (save-excursion
7190 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7191 (org-move-to-column col))))
7193 (defun org-agenda-add-note (&optional arg)
7194 "Add a time-stamped note to the entry at point."
7195 (interactive "P")
7196 (org-agenda-check-no-diary)
7197 (let* ((marker (or (org-get-at-bol 'org-marker)
7198 (org-agenda-error)))
7199 (buffer (marker-buffer marker))
7200 (pos (marker-position marker))
7201 (hdmarker (org-get-at-bol 'org-hd-marker))
7202 (inhibit-read-only t))
7203 (with-current-buffer buffer
7204 (widen)
7205 (goto-char pos)
7206 (org-show-context 'agenda)
7207 (save-excursion
7208 (and (outline-next-heading)
7209 (org-flag-heading nil))) ; show the next heading
7210 (org-add-note))))
7212 (defun org-agenda-change-all-lines (newhead hdmarker
7213 &optional fixface just-this)
7214 "Change all lines in the agenda buffer which match HDMARKER.
7215 The new content of the line will be NEWHEAD (as modified by
7216 `org-format-agenda-item'). HDMARKER is checked with
7217 `equal' against all `org-hd-marker' text properties in the file.
7218 If FIXFACE is non-nil, the face of each item is modified according to
7219 the new TODO state.
7220 If JUST-THIS is non-nil, change just the current line, not all.
7221 If FORCE-TAGS is non nil, the car of it returns the new tags."
7222 (let* ((inhibit-read-only t)
7223 (line (org-current-line))
7224 (thetags (with-current-buffer (marker-buffer hdmarker)
7225 (save-excursion (save-restriction (widen)
7226 (goto-char hdmarker)
7227 (org-get-tags-at)))))
7228 props m pl undone-face done-face finish new dotime cat tags)
7229 (save-excursion
7230 (goto-char (point-max))
7231 (beginning-of-line 1)
7232 (while (not finish)
7233 (setq finish (bobp))
7234 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7235 (or (not just-this) (= (org-current-line) line))
7236 (equal m hdmarker))
7237 (setq props (text-properties-at (point))
7238 dotime (org-get-at-bol 'dotime)
7239 cat (org-get-at-bol 'org-category)
7240 tags thetags
7241 new (org-format-agenda-item (org-get-at-bol 'extra)
7242 newhead cat tags dotime)
7243 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7244 undone-face (org-get-at-bol 'undone-face)
7245 done-face (org-get-at-bol 'done-face))
7246 (beginning-of-line 1)
7247 (cond
7248 ((equal new "")
7249 (and (looking-at ".*\n?") (replace-match "")))
7250 ((looking-at ".*")
7251 (replace-match new t t)
7252 (beginning-of-line 1)
7253 (add-text-properties (point-at-bol) (point-at-eol) props)
7254 (when fixface
7255 (add-text-properties
7256 (point-at-bol) (point-at-eol)
7257 (list 'face
7258 (if org-last-todo-state-is-todo
7259 undone-face done-face))))
7260 (org-agenda-highlight-todo 'line)
7261 (beginning-of-line 1))
7262 (t (error "Line update did not work"))))
7263 (beginning-of-line 0)))
7264 (org-finalize-agenda)))
7266 (defun org-agenda-align-tags (&optional line)
7267 "Align all tags in agenda items to `org-agenda-tags-column'."
7268 (let ((inhibit-read-only t) l c)
7269 (save-excursion
7270 (goto-char (if line (point-at-bol) (point-min)))
7271 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7272 (if line (point-at-eol) nil) t)
7273 (add-text-properties
7274 (match-beginning 2) (match-end 2)
7275 (list 'face (delq nil (let ((prop (get-text-property
7276 (match-beginning 2) 'face)))
7277 (or (listp prop) (setq prop (list prop)))
7278 (if (memq 'org-tag prop)
7279 prop
7280 (cons 'org-tag prop))))))
7281 (setq l (- (match-end 2) (match-beginning 2))
7282 c (if (< org-agenda-tags-column 0)
7283 (- (abs org-agenda-tags-column) l)
7284 org-agenda-tags-column))
7285 (delete-region (match-beginning 1) (match-end 1))
7286 (goto-char (match-beginning 1))
7287 (insert (org-add-props
7288 (make-string (max 1 (- c (current-column))) ?\ )
7289 (plist-put (copy-sequence (text-properties-at (point)))
7290 'face nil))))
7291 (goto-char (point-min))
7292 (org-font-lock-add-tag-faces (point-max)))))
7294 (defun org-agenda-priority-up ()
7295 "Increase the priority of line at point, also in Org-mode file."
7296 (interactive)
7297 (org-agenda-priority 'up))
7299 (defun org-agenda-priority-down ()
7300 "Decrease the priority of line at point, also in Org-mode file."
7301 (interactive)
7302 (org-agenda-priority 'down))
7304 (defun org-agenda-priority (&optional force-direction)
7305 "Set the priority of line at point, also in Org-mode file.
7306 This changes the line at point, all other lines in the agenda referring to
7307 the same tree node, and the headline of the tree node in the Org-mode file."
7308 (interactive)
7309 (unless org-enable-priority-commands
7310 (error "Priority commands are disabled"))
7311 (org-agenda-check-no-diary)
7312 (let* ((marker (or (org-get-at-bol 'org-marker)
7313 (org-agenda-error)))
7314 (hdmarker (org-get-at-bol 'org-hd-marker))
7315 (buffer (marker-buffer hdmarker))
7316 (pos (marker-position hdmarker))
7317 (inhibit-read-only t)
7318 newhead)
7319 (org-with-remote-undo buffer
7320 (with-current-buffer buffer
7321 (widen)
7322 (goto-char pos)
7323 (org-show-context 'agenda)
7324 (save-excursion
7325 (and (outline-next-heading)
7326 (org-flag-heading nil))) ; show the next heading
7327 (funcall 'org-priority force-direction)
7328 (end-of-line 1)
7329 (setq newhead (org-get-heading)))
7330 (org-agenda-change-all-lines newhead hdmarker)
7331 (beginning-of-line 1))))
7333 ;; FIXME: should fix the tags property of the agenda line.
7334 (defun org-agenda-set-tags (&optional tag onoff)
7335 "Set tags for the current headline."
7336 (interactive)
7337 (org-agenda-check-no-diary)
7338 (if (and (org-region-active-p) (interactive-p))
7339 (call-interactively 'org-change-tag-in-region)
7340 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7341 (org-agenda-error)))
7342 (buffer (marker-buffer hdmarker))
7343 (pos (marker-position hdmarker))
7344 (inhibit-read-only t)
7345 newhead)
7346 (org-with-remote-undo buffer
7347 (with-current-buffer buffer
7348 (widen)
7349 (goto-char pos)
7350 (save-excursion
7351 (org-show-context 'agenda))
7352 (save-excursion
7353 (and (outline-next-heading)
7354 (org-flag-heading nil))) ; show the next heading
7355 (goto-char pos)
7356 (if tag
7357 (org-toggle-tag tag onoff)
7358 (call-interactively 'org-set-tags))
7359 (end-of-line 1)
7360 (setq newhead (org-get-heading)))
7361 (org-agenda-change-all-lines newhead hdmarker)
7362 (beginning-of-line 1)))))
7364 (defun org-agenda-set-property ()
7365 "Set a property for the current headline."
7366 (interactive)
7367 (org-agenda-check-no-diary)
7368 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7369 (org-agenda-error)))
7370 (buffer (marker-buffer hdmarker))
7371 (pos (marker-position hdmarker))
7372 (inhibit-read-only t)
7373 newhead)
7374 (org-with-remote-undo buffer
7375 (with-current-buffer buffer
7376 (widen)
7377 (goto-char pos)
7378 (save-excursion
7379 (org-show-context 'agenda))
7380 (save-excursion
7381 (and (outline-next-heading)
7382 (org-flag-heading nil))) ; show the next heading
7383 (goto-char pos)
7384 (call-interactively 'org-set-property)))))
7386 (defun org-agenda-set-effort ()
7387 "Set the effort property for the current headline."
7388 (interactive)
7389 (org-agenda-check-no-diary)
7390 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7391 (org-agenda-error)))
7392 (buffer (marker-buffer hdmarker))
7393 (pos (marker-position hdmarker))
7394 (inhibit-read-only t)
7395 newhead)
7396 (org-with-remote-undo buffer
7397 (with-current-buffer buffer
7398 (widen)
7399 (goto-char pos)
7400 (save-excursion
7401 (org-show-context 'agenda))
7402 (save-excursion
7403 (and (outline-next-heading)
7404 (org-flag-heading nil))) ; show the next heading
7405 (goto-char pos)
7406 (call-interactively 'org-set-effort)
7407 (end-of-line 1)))))
7409 (defun org-agenda-toggle-archive-tag ()
7410 "Toggle the archive tag for the current entry."
7411 (interactive)
7412 (org-agenda-check-no-diary)
7413 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7414 (org-agenda-error)))
7415 (buffer (marker-buffer hdmarker))
7416 (pos (marker-position hdmarker))
7417 (inhibit-read-only t)
7418 newhead)
7419 (org-with-remote-undo buffer
7420 (with-current-buffer buffer
7421 (widen)
7422 (goto-char pos)
7423 (org-show-context 'agenda)
7424 (save-excursion
7425 (and (outline-next-heading)
7426 (org-flag-heading nil))) ; show the next heading
7427 (call-interactively 'org-toggle-archive-tag)
7428 (end-of-line 1)
7429 (setq newhead (org-get-heading)))
7430 (org-agenda-change-all-lines newhead hdmarker)
7431 (beginning-of-line 1))))
7433 (defun org-agenda-do-date-later (arg)
7434 (interactive "P")
7435 (cond
7436 ((or (equal arg '(16))
7437 (memq last-command
7438 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7439 (setq this-command 'org-agenda-date-later-minutes)
7440 (org-agenda-date-later-minutes 1))
7441 ((or (equal arg '(4))
7442 (memq last-command
7443 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7444 (setq this-command 'org-agenda-date-later-hours)
7445 (org-agenda-date-later-hours 1))
7447 (org-agenda-date-later (prefix-numeric-value arg)))))
7449 (defun org-agenda-do-date-earlier (arg)
7450 (interactive "P")
7451 (cond
7452 ((or (equal arg '(16))
7453 (memq last-command
7454 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7455 (setq this-command 'org-agenda-date-earlier-minutes)
7456 (org-agenda-date-earlier-minutes 1))
7457 ((or (equal arg '(4))
7458 (memq last-command
7459 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7460 (setq this-command 'org-agenda-date-earlier-hours)
7461 (org-agenda-date-earlier-hours 1))
7463 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7465 (defun org-agenda-date-later (arg &optional what)
7466 "Change the date of this item to ARG day(s) later."
7467 (interactive "p")
7468 (org-agenda-check-type t 'agenda 'timeline)
7469 (org-agenda-check-no-diary)
7470 (let* ((marker (or (org-get-at-bol 'org-marker)
7471 (org-agenda-error)))
7472 (buffer (marker-buffer marker))
7473 (pos (marker-position marker)))
7474 (org-with-remote-undo buffer
7475 (with-current-buffer buffer
7476 (widen)
7477 (goto-char pos)
7478 (if (not (org-at-timestamp-p))
7479 (error "Cannot find time stamp"))
7480 (org-timestamp-change arg (or what 'day)))
7481 (org-agenda-show-new-time marker org-last-changed-timestamp))
7482 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7484 (defun org-agenda-date-earlier (arg &optional what)
7485 "Change the date of this item to ARG day(s) earlier."
7486 (interactive "p")
7487 (org-agenda-date-later (- arg) what))
7489 (defun org-agenda-date-later-minutes (arg)
7490 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7491 (interactive "p")
7492 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7493 (org-agenda-date-later arg 'minute))
7495 (defun org-agenda-date-earlier-minutes (arg)
7496 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7497 (interactive "p")
7498 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7499 (org-agenda-date-earlier arg 'minute))
7501 (defun org-agenda-date-later-hours (arg)
7502 "Change the time of this item, in hour steps."
7503 (interactive "p")
7504 (org-agenda-date-later arg 'hour))
7506 (defun org-agenda-date-earlier-hours (arg)
7507 "Change the time of this item, in hour steps."
7508 (interactive "p")
7509 (org-agenda-date-earlier arg 'hour))
7511 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7512 "Show new date stamp via text properties."
7513 ;; We use text properties to make this undoable
7514 (let ((inhibit-read-only t)
7515 (buffer-invisibility-spec))
7516 (setq stamp (concat " " prefix " => " stamp))
7517 (save-excursion
7518 (goto-char (point-max))
7519 (while (not (bobp))
7520 (when (equal marker (org-get-at-bol 'org-marker))
7521 (org-move-to-column (- (window-width) (length stamp)) t)
7522 (org-agenda-fix-tags-filter-overlays-at (point))
7523 (if (featurep 'xemacs)
7524 ;; Use `duplicable' property to trigger undo recording
7525 (let ((ex (make-extent nil nil))
7526 (gl (make-glyph stamp)))
7527 (set-glyph-face gl 'secondary-selection)
7528 (set-extent-properties
7529 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7530 (insert-extent ex (1- (point)) (point-at-eol)))
7531 (add-text-properties
7532 (1- (point)) (point-at-eol)
7533 (list 'display (org-add-props stamp nil
7534 'face 'secondary-selection))))
7535 (beginning-of-line 1))
7536 (beginning-of-line 0)))))
7538 (defun org-agenda-date-prompt (arg)
7539 "Change the date of this item. Date is prompted for, with default today.
7540 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7541 be used to request time specification in the time stamp."
7542 (interactive "P")
7543 (org-agenda-check-type t 'agenda 'timeline)
7544 (org-agenda-check-no-diary)
7545 (let* ((marker (or (org-get-at-bol 'org-marker)
7546 (org-agenda-error)))
7547 (buffer (marker-buffer marker))
7548 (pos (marker-position marker)))
7549 (org-with-remote-undo buffer
7550 (with-current-buffer buffer
7551 (widen)
7552 (goto-char pos)
7553 (if (not (org-at-timestamp-p t))
7554 (error "Cannot find time stamp"))
7555 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7556 (org-agenda-show-new-time marker org-last-changed-timestamp))
7557 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7559 (defun org-agenda-schedule (arg &optional time)
7560 "Schedule the item at point.
7561 Arg is passed through to `org-schedule'."
7562 (interactive "P")
7563 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7564 (org-agenda-check-no-diary)
7565 (let* ((marker (or (org-get-at-bol 'org-marker)
7566 (org-agenda-error)))
7567 (type (marker-insertion-type marker))
7568 (buffer (marker-buffer marker))
7569 (pos (marker-position marker))
7570 (org-insert-labeled-timestamps-at-point nil)
7572 (set-marker-insertion-type marker t)
7573 (org-with-remote-undo buffer
7574 (with-current-buffer buffer
7575 (widen)
7576 (goto-char pos)
7577 (setq ts (org-schedule arg time)))
7578 (org-agenda-show-new-time marker ts "S"))
7579 (message "Item scheduled for %s" ts)))
7581 (defun org-agenda-deadline (arg &optional time)
7582 "Schedule the item at point.
7583 Arg is passed through to `org-deadline'."
7584 (interactive "P")
7585 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7586 (org-agenda-check-no-diary)
7587 (let* ((marker (or (org-get-at-bol 'org-marker)
7588 (org-agenda-error)))
7589 (buffer (marker-buffer marker))
7590 (pos (marker-position marker))
7591 (org-insert-labeled-timestamps-at-point nil)
7593 (org-with-remote-undo buffer
7594 (with-current-buffer buffer
7595 (widen)
7596 (goto-char pos)
7597 (setq ts (org-deadline arg time)))
7598 (org-agenda-show-new-time marker ts "D"))
7599 (message "Deadline for this item set to %s" ts)))
7601 (defun org-agenda-action ()
7602 "Select entry for agenda action, or execute an agenda action.
7603 This command prompts for another letter. Valid inputs are:
7605 m Mark the entry at point for an agenda action
7606 s Schedule the marked entry to the date at the cursor
7607 d Set the deadline of the marked entry to the date at the cursor
7608 r Call `org-remember' with cursor date as the default date
7609 c Call `org-capture' with cursor date as the default date
7610 SPC Show marked entry in other window
7611 TAB Visit marked entry in other window
7613 The cursor may be at a date in the calendar, or in the Org agenda."
7614 (interactive)
7615 (let (ans)
7616 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7617 (setq ans (read-char-exclusive))
7618 (cond
7619 ((equal ans ?m)
7620 ;; Mark this entry
7621 (if (eq major-mode 'org-agenda-mode)
7622 (let ((m (or (org-get-at-bol 'org-hd-marker)
7623 (org-get-at-bol 'org-marker))))
7624 (if m
7625 (progn
7626 (move-marker org-agenda-action-marker
7627 (marker-position m) (marker-buffer m))
7628 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7629 (error "Don't know which entry to mark")))
7630 (error "This command works only in the agenda")))
7631 ((equal ans ?s)
7632 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7633 ((equal ans ?d)
7634 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7635 ((equal ans ?r)
7636 (org-agenda-do-action '(org-remember) t))
7637 ((equal ans ?c)
7638 (org-agenda-do-action '(org-capture) t))
7639 ((equal ans ?\ )
7640 (let ((cw (selected-window)))
7641 (org-switch-to-buffer-other-window
7642 (marker-buffer org-agenda-action-marker))
7643 (goto-char org-agenda-action-marker)
7644 (org-show-context 'agenda)
7645 (select-window cw)))
7646 ((equal ans ?\C-i)
7647 (org-switch-to-buffer-other-window
7648 (marker-buffer org-agenda-action-marker))
7649 (goto-char org-agenda-action-marker)
7650 (org-show-context 'agenda))
7651 (t (error "Invalid agenda action %c" ans)))))
7653 (defun org-agenda-do-action (form &optional current-buffer)
7654 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7655 (let ((org-overriding-default-time (org-get-cursor-date)))
7656 (if current-buffer
7657 (eval form)
7658 (if (not (marker-buffer org-agenda-action-marker))
7659 (error "No entry has been selected for agenda action")
7660 (with-current-buffer (marker-buffer org-agenda-action-marker)
7661 (save-excursion
7662 (save-restriction
7663 (widen)
7664 (goto-char org-agenda-action-marker)
7665 (eval form))))))))
7667 (defun org-agenda-clock-in (&optional arg)
7668 "Start the clock on the currently selected item."
7669 (interactive "P")
7670 (org-agenda-check-no-diary)
7671 (if (equal arg '(4))
7672 (org-clock-in arg)
7673 (let* ((marker (or (org-get-at-bol 'org-marker)
7674 (org-agenda-error)))
7675 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7676 marker))
7677 (pos (marker-position marker))
7678 newhead)
7679 (org-with-remote-undo (marker-buffer marker)
7680 (with-current-buffer (marker-buffer marker)
7681 (widen)
7682 (goto-char pos)
7683 (org-show-context 'agenda)
7684 (org-show-entry)
7685 (org-cycle-hide-drawers 'children)
7686 (org-clock-in arg)
7687 (setq newhead (org-get-heading)))
7688 (org-agenda-change-all-lines newhead hdmarker)))))
7690 (defun org-agenda-clock-out ()
7691 "Stop the currently running clock."
7692 (interactive)
7693 (unless (marker-buffer org-clock-marker)
7694 (error "No running clock"))
7695 (let ((marker (make-marker)) newhead)
7696 (org-with-remote-undo (marker-buffer org-clock-marker)
7697 (with-current-buffer (marker-buffer org-clock-marker)
7698 (save-excursion
7699 (save-restriction
7700 (widen)
7701 (goto-char org-clock-marker)
7702 (org-back-to-heading t)
7703 (move-marker marker (point))
7704 (org-clock-out)
7705 (setq newhead (org-get-heading))))))
7706 (org-agenda-change-all-lines newhead marker)
7707 (move-marker marker nil)))
7709 (defun org-agenda-clock-cancel (&optional arg)
7710 "Cancel the currently running clock."
7711 (interactive "P")
7712 (unless (marker-buffer org-clock-marker)
7713 (error "No running clock"))
7714 (org-with-remote-undo (marker-buffer org-clock-marker)
7715 (org-clock-cancel)))
7717 (defun org-agenda-clock-goto ()
7718 "Jump to the currently clocked in task within the agenda.
7719 If the currently clocked in task is not listed in the agenda
7720 buffer, display it in another window."
7721 (interactive)
7722 (let (pos)
7723 (mapc (lambda (o)
7724 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7725 (setq pos (overlay-start o))))
7726 (overlays-in (point-min) (point-max)))
7727 (cond (pos (goto-char pos))
7728 ;; If the currently clocked entry is not in the agenda
7729 ;; buffer, we visit it in another window:
7730 (org-clock-current-task
7731 (org-switch-to-buffer-other-window (org-clock-goto)))
7732 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7734 (defun org-agenda-diary-entry-in-org-file ()
7735 "Make a diary entry in the file `org-agenda-diary-file'."
7736 (let (d1 d2 char (text "") dp1 dp2)
7737 (if (equal (buffer-name) "*Calendar*")
7738 (setq d1 (calendar-cursor-to-date t)
7739 d2 (car calendar-mark-ring))
7740 (setq dp1 (get-text-property (point-at-bol) 'day))
7741 (unless dp1 (error "No date defined in current line"))
7742 (setq d1 (calendar-gregorian-from-absolute dp1)
7743 d2 (and (ignore-errors (mark))
7744 (save-excursion
7745 (goto-char (mark))
7746 (setq dp2 (get-text-property (point-at-bol) 'day)))
7747 (calendar-gregorian-from-absolute dp2))))
7748 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7749 (setq char (read-char-exclusive))
7750 (cond
7751 ((equal char ?d)
7752 (setq text (read-string "Day entry: "))
7753 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7754 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7755 ((equal char ?a)
7756 (setq d1 (list (car d1) (nth 1 d1)
7757 (read-number (format "Reference year [%d]: " (nth 2 d1))
7758 (nth 2 d1))))
7759 (setq text (read-string "Anniversary (use %d to show years): "))
7760 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7761 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7762 ((equal char ?b)
7763 (setq text (read-string "Block entry: "))
7764 (unless (and d1 d2 (not (equal d1 d2)))
7765 (error "No block of days selected"))
7766 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7767 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7768 ((equal char ?j)
7769 (org-switch-to-buffer-other-window
7770 (find-file-noselect org-agenda-diary-file))
7771 (require 'org-datetree)
7772 (org-datetree-find-date-create d1)
7773 (org-reveal t))
7774 (t (error "Invalid selection character `%c'" char)))))
7776 (defcustom org-agenda-insert-diary-strategy 'date-tree
7777 "Where in `org-agenda-diary-file' should new entries be added?
7778 Valid values:
7780 date-tree in the date tree, as child of the date
7781 top-level as top-level entries at the end of the file."
7782 :group 'org-agenda
7783 :type '(choice
7784 (const :tag "in a date tree" date-tree)
7785 (const :tag "as top level at end of file" top-level)))
7787 (defcustom org-agenda-insert-diary-extract-time nil
7788 "Non-nil means extract any time specification from the diary entry."
7789 :group 'org-agenda
7790 :type 'boolean)
7792 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7793 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7794 If TEXT is not empty, it will become the headline of the new entry, and
7795 the resulting entry will not be shown. When TEXT is empty, switch to
7796 `org-agenda-diary-file' and let the user finish the entry there."
7797 (let ((cw (current-window-configuration)))
7798 (org-switch-to-buffer-other-window
7799 (find-file-noselect org-agenda-diary-file))
7800 (widen)
7801 (goto-char (point-min))
7802 (cond
7803 ((eq type 'anniversary)
7804 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7805 (progn
7806 (or (org-on-heading-p t)
7807 (progn
7808 (outline-next-heading)
7809 (insert "* Anniversaries\n\n")
7810 (beginning-of-line -1)))))
7811 (outline-next-heading)
7812 (org-back-over-empty-lines)
7813 (backward-char 1)
7814 (insert "\n")
7815 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7816 (nth 2 d1) (car d1) (nth 1 d1) text)))
7817 ((eq type 'day)
7818 (let ((org-prefix-has-time t)
7819 (org-agenda-time-leading-zero t)
7820 fmt time time2)
7821 (if org-agenda-insert-diary-extract-time
7822 ;; Use org-format-agenda-item to parse text for a time-range and
7823 ;; remove it. FIXME: This is a hack, we should refactor
7824 ;; that function to make time extraction available separately
7825 (setq fmt (org-format-agenda-item nil text nil nil t)
7826 time (get-text-property 0 'time fmt)
7827 time2 (if (> (length time) 0)
7828 ;; split-string removes trailing ...... if
7829 ;; no end time given. First space
7830 ;; separates time from date.
7831 (concat " " (car (split-string time "\\.")))
7832 nil)
7833 text (get-text-property 0 'txt fmt)))
7834 (if (eq org-agenda-insert-diary-strategy 'top-level)
7835 (org-agenda-insert-diary-as-top-level text)
7836 (require 'org-datetree)
7837 (org-datetree-find-date-create d1)
7838 (org-agenda-insert-diary-make-new-entry text))
7839 (org-insert-time-stamp (org-time-from-absolute
7840 (calendar-absolute-from-gregorian d1))
7841 nil nil nil nil time2))
7842 (end-of-line 0))
7843 ((eq type 'block)
7844 (if (> (calendar-absolute-from-gregorian d1)
7845 (calendar-absolute-from-gregorian d2))
7846 (setq d1 (prog1 d2 (setq d2 d1))))
7847 (if (eq org-agenda-insert-diary-strategy 'top-level)
7848 (org-agenda-insert-diary-as-top-level text)
7849 (require 'org-datetree)
7850 (org-datetree-find-date-create d1)
7851 (org-agenda-insert-diary-make-new-entry text))
7852 (org-insert-time-stamp (org-time-from-absolute
7853 (calendar-absolute-from-gregorian d1)))
7854 (insert "--")
7855 (org-insert-time-stamp (org-time-from-absolute
7856 (calendar-absolute-from-gregorian d2)))
7857 (end-of-line 0)))
7858 (if (string-match "\\S-" text)
7859 (progn
7860 (set-window-configuration cw)
7861 (message "%s entry added to %s"
7862 (capitalize (symbol-name type))
7863 (abbreviate-file-name org-agenda-diary-file)))
7864 (org-reveal t)
7865 (message "Please finish entry here"))))
7867 (defun org-agenda-insert-diary-as-top-level (text)
7868 "Make new entry as a top-level entry at the end of the file.
7869 Add TEXT as headline, and position the cursor in the second line so that
7870 a timestamp can be added there."
7871 (widen)
7872 (goto-char (point-max))
7873 (or (bolp) (insert "\n"))
7874 (insert "* " text "\n")
7875 (if org-adapt-indentation (org-indent-to-column 2)))
7877 (defun org-agenda-insert-diary-make-new-entry (text)
7878 "Make new entry as last child of current entry.
7879 Add TEXT as headline, and position the cursor in the second line so that
7880 a timestamp can be added there."
7881 (let ((org-show-following-heading t)
7882 (org-show-siblings t)
7883 (org-show-hierarchy-above t)
7884 (org-show-entry-below t)
7885 col)
7886 (outline-next-heading)
7887 (org-back-over-empty-lines)
7888 (or (looking-at "[ \t]*$")
7889 (progn (insert "\n") (backward-char 1)))
7890 (org-insert-heading nil t)
7891 (org-do-demote)
7892 (setq col (current-column))
7893 (insert text "\n")
7894 (if org-adapt-indentation (org-indent-to-column col))
7895 (let ((org-show-following-heading t)
7896 (org-show-siblings t)
7897 (org-show-hierarchy-above t)
7898 (org-show-entry-below t))
7899 (org-show-context))))
7901 (defun org-agenda-diary-entry ()
7902 "Make a diary entry, like the `i' command from the calendar.
7903 All the standard commands work: block, weekly etc.
7904 When `org-agenda-diary-file' points to a file,
7905 `org-agenda-diary-entry-in-org-file' is called instead to create
7906 entries in that Org-mode file."
7907 (interactive)
7908 (org-agenda-check-type t 'agenda 'timeline)
7909 (if (not (eq org-agenda-diary-file 'diary-file))
7910 (org-agenda-diary-entry-in-org-file)
7911 (require 'diary-lib)
7912 (let* ((char (progn
7913 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7914 (read-char-exclusive)))
7915 (cmd (cdr (assoc char
7916 '((?d . insert-diary-entry)
7917 (?w . insert-weekly-diary-entry)
7918 (?m . insert-monthly-diary-entry)
7919 (?y . insert-yearly-diary-entry)
7920 (?a . insert-anniversary-diary-entry)
7921 (?b . insert-block-diary-entry)
7922 (?c . insert-cyclic-diary-entry)))))
7923 (oldf (symbol-function 'calendar-cursor-to-date))
7924 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7925 (point (point))
7926 (mark (or (mark t) (point))))
7927 (unless cmd
7928 (error "No command associated with <%c>" char))
7929 (unless (and (get-text-property point 'day)
7930 (or (not (equal ?b char))
7931 (get-text-property mark 'day)))
7932 (error "Don't know which date to use for diary entry"))
7933 ;; We implement this by hacking the `calendar-cursor-to-date' function
7934 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7935 (let ((calendar-mark-ring
7936 (list (calendar-gregorian-from-absolute
7937 (or (get-text-property mark 'day)
7938 (get-text-property point 'day))))))
7939 (unwind-protect
7940 (progn
7941 (fset 'calendar-cursor-to-date
7942 (lambda (&optional error dummy)
7943 (calendar-gregorian-from-absolute
7944 (get-text-property point 'day))))
7945 (call-interactively cmd))
7946 (fset 'calendar-cursor-to-date oldf))))))
7948 (defun org-agenda-execute-calendar-command (cmd)
7949 "Execute a calendar command from the agenda, with the date associated to
7950 the cursor position."
7951 (org-agenda-check-type t 'agenda 'timeline)
7952 (require 'diary-lib)
7953 (unless (get-text-property (point) 'day)
7954 (error "Don't know which date to use for calendar command"))
7955 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7956 (point (point))
7957 (date (calendar-gregorian-from-absolute
7958 (get-text-property point 'day)))
7959 ;; the following 2 vars are needed in the calendar
7960 (displayed-month (car date))
7961 (displayed-year (nth 2 date)))
7962 (unwind-protect
7963 (progn
7964 (fset 'calendar-cursor-to-date
7965 (lambda (&optional error dummy)
7966 (calendar-gregorian-from-absolute
7967 (get-text-property point 'day))))
7968 (call-interactively cmd))
7969 (fset 'calendar-cursor-to-date oldf))))
7971 (defun org-agenda-phases-of-moon ()
7972 "Display the phases of the moon for the 3 months around the cursor date."
7973 (interactive)
7974 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7976 (defun org-agenda-holidays ()
7977 "Display the holidays for the 3 months around the cursor date."
7978 (interactive)
7979 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7981 (defvar calendar-longitude)
7982 (defvar calendar-latitude)
7983 (defvar calendar-location-name)
7985 (defun org-agenda-sunrise-sunset (arg)
7986 "Display sunrise and sunset for the cursor date.
7987 Latitude and longitude can be specified with the variables
7988 `calendar-latitude' and `calendar-longitude'. When called with prefix
7989 argument, latitude and longitude will be prompted for."
7990 (interactive "P")
7991 (require 'solar)
7992 (let ((calendar-longitude (if arg nil calendar-longitude))
7993 (calendar-latitude (if arg nil calendar-latitude))
7994 (calendar-location-name
7995 (if arg "the given coordinates" calendar-location-name)))
7996 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7998 (defun org-agenda-goto-calendar ()
7999 "Open the Emacs calendar with the date at the cursor."
8000 (interactive)
8001 (org-agenda-check-type t 'agenda 'timeline)
8002 (let* ((day (or (get-text-property (point) 'day)
8003 (error "Don't know which date to open in calendar")))
8004 (date (calendar-gregorian-from-absolute day))
8005 (calendar-move-hook nil)
8006 (calendar-view-holidays-initially-flag nil)
8007 (calendar-view-diary-initially-flag nil))
8008 (calendar)
8009 (calendar-goto-date date)))
8011 ;;;###autoload
8012 (defun org-calendar-goto-agenda ()
8013 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8014 This is a command that has to be installed in `calendar-mode-map'."
8015 (interactive)
8016 (org-agenda-list nil (calendar-absolute-from-gregorian
8017 (calendar-cursor-to-date))
8018 nil))
8020 (defun org-agenda-convert-date ()
8021 (interactive)
8022 (org-agenda-check-type t 'agenda 'timeline)
8023 (let ((day (get-text-property (point) 'day))
8024 date s)
8025 (unless day
8026 (error "Don't know which date to convert"))
8027 (setq date (calendar-gregorian-from-absolute day))
8028 (setq s (concat
8029 "Gregorian: " (calendar-date-string date) "\n"
8030 "ISO: " (calendar-iso-date-string date) "\n"
8031 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8032 "Julian: " (calendar-julian-date-string date) "\n"
8033 "Astron. JD: " (calendar-astro-date-string date)
8034 " (Julian date number at noon UTC)\n"
8035 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8036 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8037 "French: " (calendar-french-date-string date) "\n"
8038 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8039 "Mayan: " (calendar-mayan-date-string date) "\n"
8040 "Coptic: " (calendar-coptic-date-string date) "\n"
8041 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8042 "Persian: " (calendar-persian-date-string date) "\n"
8043 "Chinese: " (calendar-chinese-date-string date) "\n"))
8044 (with-output-to-temp-buffer "*Dates*"
8045 (princ s))
8046 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8048 ;;; Bulk commands
8050 (defvar org-agenda-bulk-marked-entries nil
8051 "List of markers that refer to marked entries in the agenda.")
8053 (defun org-agenda-bulk-marked-p ()
8054 (eq (get-char-property (point-at-bol) 'type)
8055 'org-marked-entry-overlay))
8057 (defun org-agenda-bulk-mark (&optional arg)
8058 "Mark the entry at point for future bulk action."
8059 (interactive "p")
8060 (dotimes (i (max arg 1))
8061 (unless (org-get-at-bol 'org-agenda-diary-link)
8062 (let* ((m (org-get-at-bol 'org-hd-marker))
8064 (unless (org-agenda-bulk-marked-p)
8065 (unless m (error "Nothing to mark at point"))
8066 (push m org-agenda-bulk-marked-entries)
8067 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8068 (org-overlay-display ov "> "
8069 (org-get-todo-face "TODO")
8070 'evaporate)
8071 (overlay-put ov 'type 'org-marked-entry-overlay))
8072 (beginning-of-line 2)
8073 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8074 (beginning-of-line 2))
8075 (message "%d entries marked for bulk action"
8076 (length org-agenda-bulk-marked-entries))))))
8078 (defun org-agenda-bulk-mark-regexp (regexp)
8079 "Mark entries match REGEXP."
8080 (interactive "sMark entries matching regexp: ")
8081 (let (entries-marked)
8082 (save-excursion
8083 (goto-char (point-min))
8084 (goto-char (next-single-property-change (point) 'txt))
8085 (while (re-search-forward regexp nil t)
8086 (when (string-match regexp (get-text-property (point) 'txt))
8087 (setq entries-marked (+ entries-marked 1))
8088 (call-interactively 'org-agenda-bulk-mark))))
8089 (if (not entries-marked)
8090 (message "No entry matching this regexp."))))
8092 (defun org-agenda-bulk-unmark ()
8093 "Unmark the entry at point for future bulk action."
8094 (interactive)
8095 (when (org-agenda-bulk-marked-p)
8096 (org-agenda-bulk-remove-overlays
8097 (point-at-bol) (+ 2 (point-at-bol)))
8098 (setq org-agenda-bulk-marked-entries
8099 (delete (org-get-at-bol 'org-hd-marker)
8100 org-agenda-bulk-marked-entries)))
8101 (beginning-of-line 2)
8102 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8103 (beginning-of-line 2))
8104 (message "%d entries marked for bulk action"
8105 (length org-agenda-bulk-marked-entries)))
8107 (defun org-agenda-bulk-toggle ()
8108 "Toggle marking the entry at point for bulk action."
8109 (interactive)
8110 (if (org-agenda-bulk-marked-p)
8111 (org-agenda-bulk-unmark)
8112 (org-agenda-bulk-mark)))
8114 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8115 "Remove the mark overlays between BEG and END in the agenda buffer.
8116 BEG and END default to the buffer limits.
8118 This only removes the overlays, it does not remove the markers
8119 from the list in `org-agenda-bulk-marked-entries'."
8120 (interactive)
8121 (mapc (lambda (ov)
8122 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8123 (delete-overlay ov)))
8124 (overlays-in (or beg (point-min)) (or end (point-max)))))
8126 (defun org-agenda-bulk-remove-all-marks ()
8127 "Remove all marks in the agenda buffer.
8128 This will remove the markers, and the overlays."
8129 (interactive)
8130 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8131 (setq org-agenda-bulk-marked-entries nil)
8132 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8134 (defun org-agenda-bulk-action (&optional arg)
8135 "Execute an remote-editing action on all marked entries.
8136 The prefix arg is passed through to the command if possible."
8137 (interactive "P")
8138 ;; Make sure we have markers, and only valid ones
8139 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8140 (mapc
8141 (lambda (m)
8142 (unless (and (markerp m)
8143 (marker-buffer m)
8144 (buffer-live-p (marker-buffer m))
8145 (marker-position m))
8146 (error "Marker %s for bulk command is invalid" m)))
8147 org-agenda-bulk-marked-entries)
8149 ;; Prompt for the bulk command
8150 (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline [f]unction")
8151 (let* ((action (read-char-exclusive))
8152 (org-log-refile (if org-log-refile 'time nil))
8153 (entries (reverse org-agenda-bulk-marked-entries))
8154 redo-at-end
8155 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8156 (cond
8157 ((equal action ?$)
8158 (setq cmd '(org-agenda-archive)))
8160 ((equal action ?A)
8161 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8163 ((member action '(?r ?w))
8164 (setq rfloc (org-refile-get-location
8165 "Refile to"
8166 (marker-buffer (car org-agenda-bulk-marked-entries))
8167 org-refile-allow-creating-parent-nodes))
8168 (if (nth 3 rfloc)
8169 (setcar (nthcdr 3 rfloc)
8170 (move-marker (make-marker) (nth 3 rfloc)
8171 (or (get-file-buffer (nth 1 rfloc))
8172 (find-buffer-visiting (nth 1 rfloc))
8173 (error "This should not happen")))))
8175 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8176 redo-at-end t))
8178 ((equal action ?t)
8179 (setq state (org-icompleting-read
8180 "Todo state: "
8181 (with-current-buffer (marker-buffer (car entries))
8182 (mapcar 'list org-todo-keywords-1))))
8183 (setq cmd `(let ((org-inhibit-blocking t)
8184 (org-inhibit-logging 'note))
8185 (org-agenda-todo ,state))))
8187 ((memq action '(?- ?+))
8188 (setq tag (org-icompleting-read
8189 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8190 (with-current-buffer (marker-buffer (car entries))
8191 (delq nil
8192 (mapcar (lambda (x)
8193 (if (stringp (car x)) x)) org-tag-alist)))))
8194 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8196 ((memq action '(?s ?d))
8197 (let* ((date (unless arg
8198 (org-read-date
8199 nil nil nil
8200 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8201 (ans (if arg nil org-read-date-final-answer))
8202 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8203 (setq cmd `(let* ((bound (fboundp 'read-string))
8204 (old (and bound (symbol-function 'read-string))))
8205 (unwind-protect
8206 (progn
8207 (fset 'read-string (lambda (&rest ignore) ,ans))
8208 (eval '(,c1 arg)))
8209 (if bound
8210 (fset 'read-string old)
8211 (fmakunbound 'read-string)))))))
8213 ((equal action ?S)
8214 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8215 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8216 (let ((days (read-number
8217 (format "Scatter tasks across how many %sdays: "
8218 (if arg "week" "")) 7)))
8219 (setq cmd
8220 `(let ((distance (1+ (random ,days))))
8221 (if arg
8222 (let ((dist distance)
8223 (day-of-week
8224 (calendar-day-of-week
8225 (calendar-gregorian-from-absolute (org-today)))))
8226 (dotimes (i (1+ dist))
8227 (while (member day-of-week org-agenda-weekend-days)
8228 (incf distance)
8229 (incf day-of-week)
8230 (if (= day-of-week 7)
8231 (setq day-of-week 0)))
8232 (incf day-of-week)
8233 (if (= day-of-week 7)
8234 (setq day-of-week 0)))))
8235 ;; silently fail when try to replan a sexp entry
8236 (condition-case nil
8237 (let* ((date (calendar-gregorian-from-absolute
8238 (+ (org-today) distance)))
8239 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8240 (nth 2 date))))
8241 (org-agenda-schedule nil time))
8242 (error nil)))))))
8244 ((equal action ?f)
8245 (setq cmd (list (intern
8246 (org-icompleting-read "Function: "
8247 obarray 'fboundp t nil nil)))))
8249 (t (error "Invalid bulk action")))
8251 ;; Sort the markers, to make sure that parents are handled before children
8252 (setq entries (sort entries
8253 (lambda (a b)
8254 (cond
8255 ((equal (marker-buffer a) (marker-buffer b))
8256 (< (marker-position a) (marker-position b)))
8258 (string< (buffer-name (marker-buffer a))
8259 (buffer-name (marker-buffer b))))))))
8261 ;; Now loop over all markers and apply cmd
8262 (while (setq e (pop entries))
8263 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8264 (if (not pos)
8265 (progn (message "Skipping removed entry at %s" e)
8266 (setq cntskip (1+ cntskip)))
8267 (goto-char pos)
8268 (eval cmd)
8269 (setq org-agenda-bulk-marked-entries
8270 (delete e org-agenda-bulk-marked-entries))
8271 (setq cnt (1+ cnt))))
8272 (setq org-agenda-bulk-marked-entries nil)
8273 (org-agenda-bulk-remove-all-marks)
8274 (when redo-at-end (org-agenda-redo))
8275 (message "Acted on %d entries%s"
8277 (if (= cntskip 0)
8279 (format ", skipped %d (disappeared before their turn)"
8280 cntskip)))))
8282 ;;; Flagging notes
8284 (defun org-agenda-show-the-flagging-note ()
8285 "Display the flagging note in the other window.
8286 When called a second time in direct sequence, offer to remove the FLAGGING
8287 tag and (if present) the flagging note."
8288 (interactive)
8289 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8290 (win (selected-window))
8291 note heading newhead)
8292 (unless hdmarker
8293 (error "No linked entry at point"))
8294 (if (and (eq this-command last-command)
8295 (y-or-n-p "Unflag and remove any flagging note? "))
8296 (progn
8297 (org-agenda-remove-flag hdmarker)
8298 (let ((win (get-buffer-window "*Flagging Note*")))
8299 (and win (delete-window win)))
8300 (message "Entry unflaged"))
8301 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8302 (unless note
8303 (error "No flagging note"))
8304 (org-kill-new note)
8305 (org-switch-to-buffer-other-window "*Flagging Note*")
8306 (erase-buffer)
8307 (insert note)
8308 (goto-char (point-min))
8309 (while (re-search-forward "\\\\n" nil t)
8310 (replace-match "\n" t t))
8311 (goto-char (point-min))
8312 (select-window win)
8313 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8315 (defun org-agenda-remove-flag (marker)
8316 "Remove the FLAGGED tag and any flagging note in the entry."
8317 (let (newhead)
8318 (org-with-point-at marker
8319 (org-toggle-tag "FLAGGED" 'off)
8320 (org-entry-delete nil "THEFLAGGINGNOTE")
8321 (setq newhead (org-get-heading)))
8322 (org-agenda-change-all-lines newhead marker)
8323 (message "Entry unflaged")))
8325 (defun org-agenda-get-any-marker (&optional pos)
8326 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8327 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8329 ;;; Appointment reminders
8331 (defvar appt-time-msg-list)
8333 ;;;###autoload
8334 (defun org-agenda-to-appt (&optional refresh filter)
8335 "Activate appointments found in `org-agenda-files'.
8336 With a \\[universal-argument] prefix, refresh the list of
8337 appointments.
8339 If FILTER is t, interactively prompt the user for a regular
8340 expression, and filter out entries that don't match it.
8342 If FILTER is a string, use this string as a regular expression
8343 for filtering entries out.
8345 FILTER can also be an alist with the car of each cell being
8346 either 'headline or 'category. For example:
8348 '((headline \"IMPORTANT\")
8349 (category \"Work\"))
8351 will only add headlines containing IMPORTANT or headlines
8352 belonging to the \"Work\" category."
8353 (interactive "P")
8354 (if refresh (setq appt-time-msg-list nil))
8355 (if (eq filter t)
8356 (setq filter (read-from-minibuffer "Regexp filter: ")))
8357 (let* ((cnt 0) ; count added events
8358 (org-agenda-new-buffers nil)
8359 (org-deadline-warning-days 0)
8360 ;; Do not use `org-today' here because appt only takes
8361 ;; time and without date as argument, so it may pass wrong
8362 ;; information otherwise
8363 (today (org-date-to-gregorian
8364 (time-to-days (current-time))))
8365 (org-agenda-restrict nil)
8366 (files (org-agenda-files 'unrestricted)) entries file)
8367 ;; Get all entries which may contain an appt
8368 (org-prepare-agenda-buffers files)
8369 (while (setq file (pop files))
8370 (setq entries
8371 (append entries
8372 (org-agenda-get-day-entries
8373 file today :timestamp :scheduled :deadline))))
8374 (setq entries (delq nil entries))
8375 ;; Map thru entries and find if we should filter them out
8376 (mapc
8377 (lambda(x)
8378 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8379 (cat (get-text-property 1 'org-category x))
8380 (tod (get-text-property 1 'time-of-day x))
8381 (ok (or (null filter)
8382 (and (stringp filter) (string-match filter evt))
8383 (and (listp filter)
8384 (or (string-match
8385 (cadr (assoc 'category filter)) cat)
8386 (string-match
8387 (cadr (assoc 'headline filter)) evt))))))
8388 ;; FIXME: Shall we remove text-properties for the appt text?
8389 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8390 (when (and ok tod)
8391 (setq tod (concat "00" (number-to-string tod))
8392 tod (when (string-match
8393 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8394 (concat (match-string 1 tod) ":"
8395 (match-string 2 tod))))
8396 (appt-add tod evt)
8397 (setq cnt (1+ cnt))))) entries)
8398 (org-release-buffers org-agenda-new-buffers)
8399 (if (eq cnt 0)
8400 (message "No event to add")
8401 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8403 (defun org-agenda-todayp (date)
8404 "Does DATE mean today, when considering `org-extend-today-until'?"
8405 (let ((today (org-today))
8406 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8407 date)))
8408 (eq date today)))
8410 (provide 'org-agenda)
8412 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
8414 ;;; org-agenda.el ends here