org-indent: also initialize background buffers, less aggressively
[org-mode.git] / lisp / org-agenda.el
blob07f3c1240654ffe413df40731ef6217895f0bf0d
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a conveniant way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function org-datetree-find-date-create "org-datetree"
70 (date &optional keep-restriction))
71 (declare-function org-columns-quit "org-colview" ())
72 (declare-function diary-date-display-form "diary-lib" (&optional type))
73 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
74 (declare-function org-habit-insert-consistency-graphs
75 "org-habit" (&optional line))
76 (declare-function org-is-habit-p "org-habit" (&optional pom))
77 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
78 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
79 (declare-function org-pop-to-buffer-same-window "org-compat"
80 (&optional buffer-or-name norecord label))
82 (defvar calendar-mode-map)
83 (defvar org-clock-current-task) ; defined in org-clock.el
84 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
85 (defvar org-habit-show-habits)
86 (defvar org-habit-show-habits-only-for-today)
88 ;; Defined somewhere in this file, but used before definition.
89 (defvar org-agenda-buffer-name)
90 (defvar org-agenda-overriding-header)
91 (defvar org-agenda-title-append nil)
92 (defvar entry)
93 (defvar date)
94 (defvar org-agenda-undo-list)
95 (defvar org-agenda-pending-undo-list)
96 (defvar original-date) ; dynamically scoped, calendar.el does scope this
98 (defcustom org-agenda-confirm-kill 1
99 "When set, remote killing from the agenda buffer needs confirmation.
100 When t, a confirmation is always needed. When a number N, confirmation is
101 only needed when the text to be killed contains more than N non-white lines."
102 :group 'org-agenda
103 :type '(choice
104 (const :tag "Never" nil)
105 (const :tag "Always" t)
106 (integer :tag "When more than N lines")))
108 (defcustom org-agenda-compact-blocks nil
109 "Non-nil means make the block agenda more compact.
110 This is done globally by leaving out lines like the agenda span
111 name and week number or the separator lines."
112 :group 'org-agenda
113 :type 'boolean)
115 (defcustom org-agenda-block-separator ?=
116 "The separator between blocks in the agenda.
117 If this is a string, it will be used as the separator, with a newline added.
118 If it is a character, it will be repeated to fill the window width.
119 If nil the separator is disabled. In `org-agenda-custom-commands' this
120 addresses the separator between the current and the previous block."
121 :group 'org-agenda
122 :type '(choice
123 (const :tag "Disabled" nil)
124 (character)
125 (string)))
127 (defgroup org-agenda-export nil
128 "Options concerning exporting agenda views in Org-mode."
129 :tag "Org Agenda Export"
130 :group 'org-agenda)
132 (defcustom org-agenda-with-colors t
133 "Non-nil means use colors in agenda views."
134 :group 'org-agenda-export
135 :type 'boolean)
137 (defcustom org-agenda-exporter-settings nil
138 "Alist of variable/value pairs that should be active during agenda export.
139 This is a good place to set options for ps-print and for htmlize.
140 Note that the way this is implemented, the values will be evaluated
141 before assigned to the variables. So make sure to quote values you do
142 *not* want evaluated, for example
144 (setq org-agenda-exporter-settings
145 '((ps-print-color-p 'black-white)))"
146 :group 'org-agenda-export
147 :type '(repeat
148 (list
149 (variable)
150 (sexp :tag "Value"))))
152 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
153 "Hook run in temporary buffer before writing it to an export file.
154 A useful function is `org-agenda-add-entry-text'."
155 :group 'org-agenda-export
156 :type 'hook
157 :options '(org-agenda-add-entry-text))
159 (defcustom org-agenda-add-entry-text-maxlines 0
160 "Maximum number of entry text lines to be added to agenda.
161 This is only relevant when `org-agenda-add-entry-text' is part of
162 `org-agenda-before-write-hook', which it is by default.
163 When this is 0, nothing will happen. When it is greater than 0, it
164 specifies the maximum number of lines that will be added for each entry
165 that is listed in the agenda view.
167 Note that this variable is not used during display, only when exporting
168 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
169 and `org-agenda-entry-text-maxlines'."
170 :group 'org-agenda
171 :type 'integer)
173 (defcustom org-agenda-add-entry-text-descriptive-links t
174 "Non-nil means export org-links as descriptive links in agenda added text.
175 This variable applies to the text added to the agenda when
176 `org-agenda-add-entry-text-maxlines' is larger than 0.
177 When this variable nil, the URL will (also) be shown."
178 :group 'org-agenda
179 :type 'boolean)
181 (defcustom org-agenda-export-html-style ""
182 "The style specification for exported HTML Agenda files.
183 If this variable contains a string, it will replace the default <style>
184 section as produced by `htmlize'.
185 Since there are different ways of setting style information, this variable
186 needs to contain the full HTML structure to provide a style, including the
187 surrounding HTML tags. The style specifications should include definitions
188 the fonts used by the agenda, here is an example:
190 <style type=\"text/css\">
191 p { font-weight: normal; color: gray; }
192 .org-agenda-structure {
193 font-size: 110%;
194 color: #003399;
195 font-weight: 600;
197 .org-todo {
198 color: #cc6666;
199 font-weight: bold;
201 .org-agenda-done {
202 color: #339933;
204 .org-done {
205 color: #339933;
207 .title { text-align: center; }
208 .todo, .deadline { color: red; }
209 .done { color: green; }
210 </style>
212 or, if you want to keep the style in a file,
214 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
216 As the value of this option simply gets inserted into the HTML <head> header,
217 you can \"misuse\" it to also add other text to the header. However,
218 <style>...</style> is required, if not present the variable will be ignored."
219 :group 'org-agenda-export
220 :group 'org-export-html
221 :type 'string)
223 (defcustom org-agenda-persistent-filter nil
224 "When set, keep filters from one agenda view to the next."
225 :group 'org-agenda
226 :type 'boolean)
228 (defgroup org-agenda-custom-commands nil
229 "Options concerning agenda views in Org-mode."
230 :tag "Org Agenda Custom Commands"
231 :group 'org-agenda)
233 (defconst org-sorting-choice
234 '(choice
235 (const time-up) (const time-down)
236 (const category-keep) (const category-up) (const category-down)
237 (const tag-down) (const tag-up)
238 (const priority-up) (const priority-down)
239 (const todo-state-up) (const todo-state-down)
240 (const effort-up) (const effort-down)
241 (const habit-up) (const habit-down)
242 (const alpha-up) (const alpha-down)
243 (const user-defined-up) (const user-defined-down))
244 "Sorting choices.")
246 (defconst org-agenda-custom-commands-local-options
247 `(repeat :tag "Local settings for this command. Remember to quote values"
248 (choice :tag "Setting"
249 (list :tag "Heading for this block"
250 (const org-agenda-overriding-header)
251 (string :tag "Headline"))
252 (list :tag "Files to be searched"
253 (const org-agenda-files)
254 (list
255 (const :format "" quote)
256 (repeat (file))))
257 (list :tag "Sorting strategy"
258 (const org-agenda-sorting-strategy)
259 (list
260 (const :format "" quote)
261 (repeat
262 ,org-sorting-choice)))
263 (list :tag "Prefix format"
264 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
265 (string))
266 (list :tag "Number of days in agenda"
267 (const org-agenda-span)
268 (choice (const :tag "Day" 'day)
269 (const :tag "Week" 'week)
270 (const :tag "Month" 'month)
271 (const :tag "Year" 'year)
272 (integer :tag "Custom")))
273 (list :tag "Fixed starting date"
274 (const org-agenda-start-day)
275 (string :value "2007-11-01"))
276 (list :tag "Start on day of week"
277 (const org-agenda-start-on-weekday)
278 (choice :value 1
279 (const :tag "Today" nil)
280 (integer :tag "Weekday No.")))
281 (list :tag "Include data from diary"
282 (const org-agenda-include-diary)
283 (boolean))
284 (list :tag "Deadline Warning days"
285 (const org-deadline-warning-days)
286 (integer :value 1))
287 (list :tag "Tags filter preset"
288 (const org-agenda-filter-preset)
289 (list
290 (const :format "" quote)
291 (repeat
292 (string :tag "+tag or -tag"))))
293 (list :tag "Set daily/weekly entry types"
294 (const org-agenda-entry-types)
295 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
296 (const :deadline)
297 (const :scheduled)
298 (const :timestamp)
299 (const :sexp)))
300 (list :tag "Standard skipping condition"
301 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
302 (const org-agenda-skip-function)
303 (list
304 (const :format "" quote)
305 (list
306 (choice
307 :tag "Skipping range"
308 (const :tag "Skip entry" org-agenda-skip-entry-if)
309 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
310 (repeat :inline t :tag "Conditions for skipping"
311 (choice
312 :tag "Condition type"
313 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
314 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
315 (list :tag "TODO state is" :inline t
316 (const 'todo)
317 (choice
318 (const :tag "any not-done state" 'todo)
319 (const :tag "any done state" 'done)
320 (const :tag "any state" 'any)
321 (list :tag "Keyword list"
322 (const :format "" quote)
323 (repeat (string :tag "Keyword")))))
324 (list :tag "TODO state is not" :inline t
325 (const 'nottodo)
326 (choice
327 (const :tag "any not-done state" 'todo)
328 (const :tag "any done state" 'done)
329 (const :tag "any state" 'any)
330 (list :tag "Keyword list"
331 (const :format "" quote)
332 (repeat (string :tag "Keyword")))))
333 (const :tag "scheduled" 'scheduled)
334 (const :tag "not scheduled" 'notscheduled)
335 (const :tag "deadline" 'deadline)
336 (const :tag "no deadline" 'notdeadline)
337 (const :tag "timestamp" 'timestamp)
338 (const :tag "no timestamp" 'nottimestamp))))))
339 (list :tag "Non-standard skipping condition"
340 :value (org-agenda-skip-function)
341 (const org-agenda-skip-function)
342 (sexp :tag "Function or form (quoted!)"))
343 (list :tag "Any variable"
344 (variable :tag "Variable")
345 (sexp :tag "Value (sexp)"))))
346 "Selection of examples for agenda command settings.
347 This will be spliced into the custom type of
348 `org-agenda-custom-commands'.")
351 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
352 ((agenda "") (alltodo))))
353 "Custom commands for the agenda.
354 These commands will be offered on the splash screen displayed by the
355 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
357 (key desc type match settings files)
359 key The key (one or more characters as a string) to be associated
360 with the command.
361 desc A description of the command, when omitted or nil, a default
362 description is built using MATCH.
363 type The command type, any of the following symbols:
364 agenda The daily/weekly agenda.
365 todo Entries with a specific TODO keyword, in all agenda files.
366 search Entries containing search words entry or headline.
367 tags Tags/Property/TODO match in all agenda files.
368 tags-todo Tags/P/T match in all agenda files, TODO entries only.
369 todo-tree Sparse tree of specific TODO keyword in *current* file.
370 tags-tree Sparse tree with all tags matches in *current* file.
371 occur-tree Occur sparse tree for *current* file.
372 ... A user-defined function.
373 match What to search for:
374 - a single keyword for TODO keyword searches
375 - a tags match expression for tags searches
376 - a word search expression for text searches.
377 - a regular expression for occur searches
378 For all other commands, this should be the empty string.
379 settings A list of option settings, similar to that in a let form, so like
380 this: ((opt1 val1) (opt2 val2) ...). The values will be
381 evaluated at the moment of execution, so quote them when needed.
382 files A list of files file to write the produced agenda buffer to
383 with the command `org-store-agenda-views'.
384 If a file name ends in \".html\", an HTML version of the buffer
385 is written out. If it ends in \".ps\", a postscript version is
386 produced. Otherwise, only the plain text is written to the file.
388 You can also define a set of commands, to create a composite agenda buffer.
389 In this case, an entry looks like this:
391 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
393 where
395 desc A description string to be displayed in the dispatcher menu.
396 cmd An agenda command, similar to the above. However, tree commands
397 are no allowed, but instead you can get agenda and global todo list.
398 So valid commands for a set are:
399 (agenda \"\" settings)
400 (alltodo \"\" settings)
401 (stuck \"\" settings)
402 (todo \"match\" settings files)
403 (search \"match\" settings files)
404 (tags \"match\" settings files)
405 (tags-todo \"match\" settings files)
407 Each command can carry a list of options, and another set of options can be
408 given for the whole set of commands. Individual command options take
409 precedence over the general options.
411 When using several characters as key to a command, the first characters
412 are prefix commands. For the dispatcher to display useful information, you
413 should provide a description for the prefix, like
415 (setq org-agenda-custom-commands
416 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
417 (\"hl\" tags \"+HOME+Lisa\")
418 (\"hp\" tags \"+HOME+Peter\")
419 (\"hk\" tags \"+HOME+Kim\")))"
420 :group 'org-agenda-custom-commands
421 :type `(repeat
422 (choice :value ("x" "Describe command here" tags "" nil)
423 (list :tag "Single command"
424 (string :tag "Access Key(s) ")
425 (option (string :tag "Description"))
426 (choice
427 (const :tag "Agenda" agenda)
428 (const :tag "TODO list" alltodo)
429 (const :tag "Search words" search)
430 (const :tag "Stuck projects" stuck)
431 (const :tag "Tags/Property match (all agenda files)" tags)
432 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
433 (const :tag "TODO keyword search (all agenda files)" todo)
434 (const :tag "Tags sparse tree (current buffer)" tags-tree)
435 (const :tag "TODO keyword tree (current buffer)" todo-tree)
436 (const :tag "Occur tree (current buffer)" occur-tree)
437 (sexp :tag "Other, user-defined function"))
438 (string :tag "Match (only for some commands)")
439 ,org-agenda-custom-commands-local-options
440 (option (repeat :tag "Export" (file :tag "Export to"))))
441 (list :tag "Command series, all agenda files"
442 (string :tag "Access Key(s)")
443 (string :tag "Description ")
444 (repeat :tag "Component"
445 (choice
446 (list :tag "Agenda"
447 (const :format "" agenda)
448 (const :tag "" :format "" "")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "TODO list (all keywords)"
451 (const :format "" alltodo)
452 (const :tag "" :format "" "")
453 ,org-agenda-custom-commands-local-options)
454 (list :tag "Search words"
455 (const :format "" search)
456 (string :tag "Match")
457 ,org-agenda-custom-commands-local-options)
458 (list :tag "Stuck projects"
459 (const :format "" stuck)
460 (const :tag "" :format "" "")
461 ,org-agenda-custom-commands-local-options)
462 (list :tag "Tags search"
463 (const :format "" tags)
464 (string :tag "Match")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "Tags search, TODO entries only"
467 (const :format "" tags-todo)
468 (string :tag "Match")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "TODO keyword search"
471 (const :format "" todo)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Other, user-defined function"
475 (symbol :tag "function")
476 (string :tag "Match")
477 ,org-agenda-custom-commands-local-options)))
479 (repeat :tag "Settings for entire command set"
480 (list (variable :tag "Any variable")
481 (sexp :tag "Value")))
482 (option (repeat :tag "Export" (file :tag "Export to"))))
483 (cons :tag "Prefix key documentation"
484 (string :tag "Access Key(s)")
485 (string :tag "Description ")))))
487 (defcustom org-agenda-query-register ?o
488 "The register holding the current query string.
489 The purpose of this is that if you construct a query string interactively,
490 you can then use it to define a custom command."
491 :group 'org-agenda-custom-commands
492 :type 'character)
494 (defcustom org-stuck-projects
495 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
496 "How to identify stuck projects.
497 This is a list of four items:
498 1. A tags/todo/property matcher string that is used to identify a project.
499 See the manual for a description of tag and property searches.
500 The entire tree below a headline matched by this is considered one project.
501 2. A list of TODO keywords identifying non-stuck projects.
502 If the project subtree contains any headline with one of these todo
503 keywords, the project is considered to be not stuck. If you specify
504 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
505 3. A list of tags identifying non-stuck projects.
506 If the project subtree contains any headline with one of these tags,
507 the project is considered to be not stuck. If you specify \"*\" as
508 a tag, any tag will mark the project unstuck. Note that this is about
509 the explicit presence of a tag somewhere in the subtree, inherited
510 tags to not count here. If inherited tags make a project not stuck,
511 use \"-TAG\" in the tags part of the matcher under (1.) above.
512 4. An arbitrary regular expression matching non-stuck projects.
514 If the project turns out to be not stuck, search continues also in the
515 subtree to see if any of the subtasks have project status.
517 See also the variable `org-tags-match-list-sublevels' which applies
518 to projects matched by this search as well.
520 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
521 or `C-c a #' to produce the list."
522 :group 'org-agenda-custom-commands
523 :type '(list
524 (string :tag "Tags/TODO match to identify a project")
525 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
526 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
527 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
529 (defcustom org-agenda-filter-effort-default-operator "<"
530 "The default operator for effort estimate filtering.
531 If you select an effort estimate limit without first pressing an operator,
532 this one will be used."
533 :group 'org-agenda-custom-commands
534 :type '(choice (const :tag "less or equal" "<")
535 (const :tag "greater or equal"">")
536 (const :tag "equal" "=")))
538 (defgroup org-agenda-skip nil
539 "Options concerning skipping parts of agenda files."
540 :tag "Org Agenda Skip"
541 :group 'org-agenda)
543 (defcustom org-agenda-skip-function-global nil
544 "Function to be called at each match during agenda construction.
545 If this function returns nil, the current match should not be skipped.
546 If the function decided to skip an agenda match, is must return the
547 buffer position from which the search should be continued.
548 This may also be a Lisp form, which will be evaluated.
550 This variable will be applied to every agenda match, including
551 tags/property searches and TODO lists. So try to make the test function
552 do its checking as efficiently as possible. To implement a skipping
553 condition just for specific agenda commands, use the variable
554 `org-agenda-skip-function' which can be set in the options section
555 of custom agenda commands."
556 :group 'org-agenda-skip
557 :type 'sexp)
559 (defgroup org-agenda-daily/weekly nil
560 "Options concerning the daily/weekly agenda."
561 :tag "Org Agenda Daily/Weekly"
562 :group 'org-agenda)
563 (defgroup org-agenda-todo-list nil
564 "Options concerning the global todo list agenda view."
565 :tag "Org Agenda Todo List"
566 :group 'org-agenda)
567 (defgroup org-agenda-match-view nil
568 "Options concerning the general tags/property/todo match agenda view."
569 :tag "Org Agenda Match View"
570 :group 'org-agenda)
571 (defgroup org-agenda-search-view nil
572 "Options concerning the general tags/property/todo match agenda view."
573 :tag "Org Agenda Match View"
574 :group 'org-agenda)
576 (defvar org-agenda-archives-mode nil
577 "Non-nil means the agenda will include archived items.
578 If this is the symbol `trees', trees in the selected agenda scope
579 that are marked with the ARCHIVE tag will be included anyway. When this is
580 t, also all archive files associated with the current selection of agenda
581 files will be included.")
583 (defcustom org-agenda-skip-comment-trees t
584 "Non-nil means skip trees that start with the COMMENT keyword.
585 When nil, these trees are also scanned by agenda commands."
586 :group 'org-agenda-skip
587 :type 'boolean)
589 (defcustom org-agenda-todo-list-sublevels t
590 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
591 When nil, the sublevels of a TODO entry are not checked, resulting in
592 potentially much shorter TODO lists."
593 :group 'org-agenda-skip
594 :group 'org-agenda-todo-list
595 :type 'boolean)
597 (defcustom org-agenda-todo-ignore-with-date nil
598 "Non-nil means don't show entries with a date in the global todo list.
599 You can use this if you prefer to mark mere appointments with a TODO keyword,
600 but don't want them to show up in the TODO list.
601 When this is set, it also covers deadlines and scheduled items, the settings
602 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
603 will be ignored.
604 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
605 :group 'org-agenda-skip
606 :group 'org-agenda-todo-list
607 :type 'boolean)
609 (defcustom org-agenda-todo-ignore-timestamp nil
610 "Non-nil means don't show entries with a timestamp.
611 This applies when creating the global todo list.
612 Valid values are:
614 past Don't show entries for today or in the past.
616 future Don't show entries with a timestamp in the future.
617 The idea behind this is that if it has a future
618 timestamp, you don't want to think about it until the
619 date.
621 all Don't show any entries with a timestamp in the global todo list.
622 The idea behind this is that by setting a timestamp, you
623 have already \"taken care\" of this item.
625 This variable can also have an integer as a value. If positive (N),
626 todos with a timestamp N or more days in the future will be ignored. If
627 negative (-N), todos with a timestamp N or more days in the past will be
628 ignored. If 0, todos with a timestamp either today or in the future will
629 be ignored. For example, a value of -1 will exclude todos with a
630 timestamp in the past (yesterday or earlier), while a value of 7 will
631 exclude todos with a timestamp a week or more in the future.
633 See also `org-agenda-todo-ignore-with-date'.
634 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
635 to make his option also apply to the tags-todo list."
636 :group 'org-agenda-skip
637 :group 'org-agenda-todo-list
638 :type '(choice
639 (const :tag "Ignore future timestamp todos" future)
640 (const :tag "Ignore past or present timestamp todos" past)
641 (const :tag "Ignore all timestamp todos" all)
642 (const :tag "Show timestamp todos" nil)
643 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
645 (defcustom org-agenda-todo-ignore-scheduled nil
646 "Non-nil means, ignore some scheduled TODO items when making TODO list.
647 This applies when creating the global todo list.
648 Valid values are:
650 past Don't show entries scheduled today or in the past.
652 future Don't show entries scheduled in the future.
653 The idea behind this is that by scheduling it, you don't want to
654 think about it until the scheduled date.
656 all Don't show any scheduled entries in the global todo list.
657 The idea behind this is that by scheduling it, you have already
658 \"taken care\" of this item.
660 t Same as `all', for backward compatibility.
662 This variable can also have an integer as a value. See
663 `org-agenda-todo-ignore-timestamp' for more details.
665 See also `org-agenda-todo-ignore-with-date'.
666 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
667 to make his option also apply to the tags-todo list."
668 :group 'org-agenda-skip
669 :group 'org-agenda-todo-list
670 :type '(choice
671 (const :tag "Ignore future-scheduled todos" future)
672 (const :tag "Ignore past- or present-scheduled todos" past)
673 (const :tag "Ignore all scheduled todos" all)
674 (const :tag "Ignore all scheduled todos (compatibility)" t)
675 (const :tag "Show scheduled todos" nil)
676 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
678 (defcustom org-agenda-todo-ignore-deadlines nil
679 "Non-nil means ignore some deadlined TODO items when making TODO list.
680 There are different motivations for using different values, please think
681 carefully when configuring this variable.
683 This applies when creating the global todo list.
684 Valid values are:
686 near Don't show near deadline entries. A deadline is near when it is
687 closer than `org-deadline-warning-days' days. The idea behind this
688 is that such items will appear in the agenda anyway.
690 far Don't show TODO entries where a deadline has been defined, but
691 the deadline is not near. This is useful if you don't want to
692 use the todo list to figure out what to do now.
694 past Don't show entries with a deadline timestamp for today or in the past.
696 future Don't show entries with a deadline timestamp in the future, not even
697 when they become `near' ones. Use it with caution.
699 all Ignore all TODO entries that do have a deadline.
701 t Same as `near', for backward compatibility.
703 This variable can also have an integer as a value. See
704 `org-agenda-todo-ignore-timestamp' for more details.
706 See also `org-agenda-todo-ignore-with-date'.
707 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
708 to make his option also apply to the tags-todo list."
709 :group 'org-agenda-skip
710 :group 'org-agenda-todo-list
711 :type '(choice
712 (const :tag "Ignore near deadlines" near)
713 (const :tag "Ignore near deadlines (compatibility)" t)
714 (const :tag "Ignore far deadlines" far)
715 (const :tag "Ignore all TODOs with a deadlines" all)
716 (const :tag "Show all TODOs, even if they have a deadline" nil)
717 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
719 (defcustom org-agenda-tags-todo-honor-ignore-options nil
720 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
721 The variables
722 `org-agenda-todo-ignore-with-date',
723 `org-agenda-todo-ignore-timestamp',
724 `org-agenda-todo-ignore-scheduled',
725 `org-agenda-todo-ignore-deadlines'
726 make the global TODO list skip entries that have time stamps of certain
727 kinds. If this option is set, the same options will also apply for the
728 tags-todo search, which is the general tags/property matcher
729 restricted to unfinished TODO entries only."
730 :group 'org-agenda-skip
731 :group 'org-agenda-todo-list
732 :group 'org-agenda-match-view
733 :type 'boolean)
735 (defcustom org-agenda-skip-scheduled-if-done nil
736 "Non-nil means don't show scheduled items in agenda when they are done.
737 This is relevant for the daily/weekly agenda, not for the TODO list. And
738 it applies only to the actual date of the scheduling. Warnings about
739 an item with a past scheduling dates are always turned off when the item
740 is DONE."
741 :group 'org-agenda-skip
742 :group 'org-agenda-daily/weekly
743 :type 'boolean)
745 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
746 "Non-nil means skip scheduling line if same entry shows because of deadline.
747 In the agenda of today, an entry can show up multiple times because
748 it is both scheduled and has a nearby deadline, and maybe a plain time
749 stamp as well.
750 When this variable is t, then only the deadline is shown and the fact that
751 the entry is scheduled today or was scheduled previously is not shown.
752 When this variable is nil, the entry will be shown several times. When
753 the variable is the symbol `not-today', then skip scheduled previously,
754 but not scheduled today."
755 :group 'org-agenda-skip
756 :group 'org-agenda-daily/weekly
757 :type '(choice
758 (const :tag "Never" nil)
759 (const :tag "Always" t)
760 (const :tag "Not when scheduled today" not-today)))
762 (defcustom org-agenda-skip-deadline-if-done nil
763 "Non-nil means don't show deadlines when the corresponding item is done.
764 When nil, the deadline is still shown and should give you a happy feeling.
765 This is relevant for the daily/weekly agenda. And it applied only to the
766 actually date of the deadline. Warnings about approaching and past-due
767 deadlines are always turned off when the item is DONE."
768 :group 'org-agenda-skip
769 :group 'org-agenda-daily/weekly
770 :type 'boolean)
772 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
773 "Non-nil means skip deadline prewarning when entry is also scheduled.
774 This will apply on all days where a prewarning for the deadline would
775 be shown, but not at the day when the entry is actually due. On that day,
776 the deadline will be shown anyway.
777 This variable may be set to nil, t, or a number which will then give
778 the number of days before the actual deadline when the prewarnings
779 should resume.
780 This can be used in a workflow where the first showing of the deadline will
781 trigger you to schedule it, and then you don't want to be reminded of it
782 because you will take care of it on the day when scheduled."
783 :group 'org-agenda-skip
784 :group 'org-agenda-daily/weekly
785 :type '(choice
786 (const :tag "Alwas show prewarning" nil)
787 (const :tag "Remove prewarning if entry is scheduled" t)
788 (integer :tag "Restart prewarning N days before deadline")))
790 (defcustom org-agenda-skip-additional-timestamps-same-entry t
791 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
792 When non-nil, after the search for timestamps has matched once in an
793 entry, the rest of the entry will not be searched."
794 :group 'org-agenda-skip
795 :type 'boolean)
797 (defcustom org-agenda-skip-timestamp-if-done nil
798 "Non-nil means don't select item by timestamp or -range if it is DONE."
799 :group 'org-agenda-skip
800 :group 'org-agenda-daily/weekly
801 :type 'boolean)
803 (defcustom org-agenda-dim-blocked-tasks t
804 "Non-nil means dim blocked tasks in the agenda display.
805 This causes some overhead during agenda construction, but if you
806 have turned on `org-enforce-todo-dependencies',
807 `org-enforce-todo-checkbox-dependencies', or any other blocking
808 mechanism, this will create useful feedback in the agenda.
810 Instead of t, this variable can also have the value `invisible'.
811 Then blocked tasks will be invisible and only become visible when
812 they become unblocked. An exemption to this behavior is when a task is
813 blocked because of unchecked checkboxes below it. Since checkboxes do
814 not show up in the agenda views, making this task invisible you remove any
815 trace from agenda views that there is something to do. Therefore, a task
816 that is blocked because of checkboxes will never be made invisible, it
817 will only be dimmed."
818 :group 'org-agenda-daily/weekly
819 :group 'org-agenda-todo-list
820 :type '(choice
821 (const :tag "Do not dim" nil)
822 (const :tag "Dim to a grey face" t)
823 (const :tag "Make invisible" invisible)))
825 (defcustom org-timeline-show-empty-dates 3
826 "Non-nil means `org-timeline' also shows dates without an entry.
827 When nil, only the days which actually have entries are shown.
828 When t, all days between the first and the last date are shown.
829 When an integer, show also empty dates, but if there is a gap of more than
830 N days, just insert a special line indicating the size of the gap."
831 :group 'org-agenda-skip
832 :type '(choice
833 (const :tag "None" nil)
834 (const :tag "All" t)
835 (integer :tag "at most")))
837 (defgroup org-agenda-startup nil
838 "Options concerning initial settings in the Agenda in Org Mode."
839 :tag "Org Agenda Startup"
840 :group 'org-agenda)
842 (defcustom org-agenda-menu-show-matcher t
843 "Non-nil means show the match string in the agenda dispatcher menu.
844 When nil, the matcher string is not shown, but is put into the help-echo
845 property so than moving the mouse over the command shows it.
846 Setting it to nil is good if matcher strings are very long and/or if
847 you want to use two-column display (see `org-agenda-menu-two-column')."
848 :group 'org-agenda
849 :type 'boolean)
851 (defcustom org-agenda-menu-two-column nil
852 "Non-nil means, use two columns to show custom commands in the dispatcher.
853 If you use this, you probably want to set `org-agenda-menu-show-matcher'
854 to nil."
855 :group 'org-agenda
856 :type 'boolean)
858 (defcustom org-finalize-agenda-hook nil
859 "Hook run just before displaying an agenda buffer."
860 :group 'org-agenda-startup
861 :type 'hook)
863 (defcustom org-agenda-mouse-1-follows-link nil
864 "Non-nil means mouse-1 on a link will follow the link in the agenda.
865 A longer mouse click will still set point. Does not work on XEmacs.
866 Needs to be set before org.el is loaded."
867 :group 'org-agenda-startup
868 :type 'boolean)
870 (defcustom org-agenda-start-with-follow-mode nil
871 "The initial value of follow mode in a newly created agenda window."
872 :group 'org-agenda-startup
873 :type 'boolean)
875 (defcustom org-agenda-show-outline-path t
876 "Non-nil means show outline path in echo area after line motion."
877 :group 'org-agenda-startup
878 :type 'boolean)
880 (defcustom org-agenda-start-with-entry-text-mode nil
881 "The initial value of entry-text-mode in a newly created agenda window."
882 :group 'org-agenda-startup
883 :type 'boolean)
885 (defcustom org-agenda-entry-text-maxlines 5
886 "Number of text lines to be added when `E' is pressed in the agenda.
888 Note that this variable only used during agenda display. Add add entry text
889 when exporting the agenda, configure the variable
890 `org-agenda-add-entry-ext-maxlines'."
891 :group 'org-agenda
892 :type 'integer)
894 (defcustom org-agenda-entry-text-exclude-regexps nil
895 "List of regular expressions to clean up entry text.
896 The complete matches of all regular expressions in this list will be
897 removed from entry text before it is shown in the agenda."
898 :group 'org-agenda
899 :type '(repeat (regexp)))
901 (defvar org-agenda-entry-text-cleanup-hook nil
902 "Hook that is run after basic cleanup of entry text to be shown in agenda.
903 This cleanup is done in a temporary buffer, so the function may inspect and
904 change the entire buffer.
905 Some default stuff like drawers and scheduling/deadline dates will already
906 have been removed when this is called, as will any matches for regular
907 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
909 (defvar org-agenda-include-inactive-timestamps nil
910 "Non-nil means include inactive time stamps in agenda and timeline.")
912 (defgroup org-agenda-windows nil
913 "Options concerning the windows used by the Agenda in Org Mode."
914 :tag "Org Agenda Windows"
915 :group 'org-agenda)
917 (defcustom org-agenda-window-setup 'reorganize-frame
918 "How the agenda buffer should be displayed.
919 Possible values for this option are:
921 current-window Show agenda in the current window, keeping all other windows.
922 other-window Use `switch-to-buffer-other-window' to display agenda.
923 reorganize-frame Show only two windows on the current frame, the current
924 window and the agenda.
925 other-frame Use `switch-to-buffer-other-frame' to display agenda.
926 Also, when exiting the agenda, kill that frame.
927 See also the variable `org-agenda-restore-windows-after-quit'."
928 :group 'org-agenda-windows
929 :type '(choice
930 (const current-window)
931 (const other-frame)
932 (const other-window)
933 (const reorganize-frame)))
935 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
936 "The min and max height of the agenda window as a fraction of frame height.
937 The value of the variable is a cons cell with two numbers between 0 and 1.
938 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
939 :group 'org-agenda-windows
940 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
942 (defcustom org-agenda-restore-windows-after-quit nil
943 "Non-nil means restore window configuration upon exiting agenda.
944 Before the window configuration is changed for displaying the agenda,
945 the current status is recorded. When the agenda is exited with
946 `q' or `x' and this option is set, the old state is restored. If
947 `org-agenda-window-setup' is `other-frame', the value of this
948 option will be ignored."
949 :group 'org-agenda-windows
950 :type 'boolean)
952 (defcustom org-agenda-ndays nil
953 "Number of days to include in overview display.
954 Should be 1 or 7.
955 Obsolete, see `org-agenda-span'."
956 :group 'org-agenda-daily/weekly
957 :type 'integer)
959 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
961 (defcustom org-agenda-span 'week
962 "Number of days to include in overview display.
963 Can be day, week, month, year, or any number of days.
964 Custom commands can set this variable in the options section."
965 :group 'org-agenda-daily/weekly
966 :type '(choice (const :tag "Day" day)
967 (const :tag "Week" week)
968 (const :tag "Month" month)
969 (const :tag "Year" year)
970 (integer :tag "Custom")))
972 (defcustom org-agenda-start-on-weekday 1
973 "Non-nil means start the overview always on the specified weekday.
974 0 denotes Sunday, 1 denotes Monday etc.
975 When nil, always start on the current day.
976 Custom commands can set this variable in the options section."
977 :group 'org-agenda-daily/weekly
978 :type '(choice (const :tag "Today" nil)
979 (integer :tag "Weekday No.")))
981 (defcustom org-agenda-show-all-dates t
982 "Non-nil means `org-agenda' shows every day in the selected range.
983 When nil, only the days which actually have entries are shown."
984 :group 'org-agenda-daily/weekly
985 :type 'boolean)
987 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
988 "Format string for displaying dates in the agenda.
989 Used by the daily/weekly agenda and by the timeline. This should be
990 a format string understood by `format-time-string', or a function returning
991 the formatted date as a string. The function must take a single argument,
992 a calendar-style date list like (month day year)."
993 :group 'org-agenda-daily/weekly
994 :type '(choice
995 (string :tag "Format string")
996 (function :tag "Function")))
998 (defun org-agenda-format-date-aligned (date)
999 "Format a date string for display in the daily/weekly agenda, or timeline.
1000 This function makes sure that dates are aligned for easy reading."
1001 (require 'cal-iso)
1002 (let* ((dayname (calendar-day-name date))
1003 (day (cadr date))
1004 (day-of-week (calendar-day-of-week date))
1005 (month (car date))
1006 (monthname (calendar-month-name month))
1007 (year (nth 2 date))
1008 (iso-week (org-days-to-iso-week
1009 (calendar-absolute-from-gregorian date)))
1010 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1011 (1- year))
1012 ((and (= month 12) (<= iso-week 1))
1013 (1+ year))
1014 (t year)))
1015 (weekstring (if (= day-of-week 1)
1016 (format " W%02d" iso-week)
1017 "")))
1018 (format "%-10s %2d %s %4d%s"
1019 dayname day monthname year weekstring)))
1021 (defcustom org-agenda-time-leading-zero nil
1022 "Non-nil means use leading zero for military times in agenda.
1023 For example, 9:30am would become 09:30 rather than 9:30."
1024 :group 'org-agenda-daily/weekly
1025 :type 'boolean)
1027 (defcustom org-agenda-timegrid-use-ampm nil
1028 "When set, show AM/PM style timestamps on the timegrid."
1029 :group 'org-agenda
1030 :type 'boolean)
1032 (defun org-agenda-time-of-day-to-ampm (time)
1033 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1034 (let* ((hour-number (string-to-number (substring time 0 -3)))
1035 (minute (substring time -2))
1036 (ampm "am"))
1037 (cond
1038 ((equal hour-number 12)
1039 (setq ampm "pm"))
1040 ((> hour-number 12)
1041 (setq ampm "pm")
1042 (setq hour-number (- hour-number 12))))
1043 (concat
1044 (if org-agenda-time-leading-zero
1045 (format "%02d" hour-number)
1046 (format "%02s" (number-to-string hour-number)))
1047 ":" minute ampm)))
1049 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1050 "Conditionally convert TIME to AM/PM format
1051 based on `org-agenda-timegrid-use-ampm'"
1052 (if org-agenda-timegrid-use-ampm
1053 (org-agenda-time-of-day-to-ampm time)
1054 time))
1056 (defcustom org-agenda-weekend-days '(6 0)
1057 "Which days are weekend?
1058 These days get the special face `org-agenda-date-weekend' in the agenda
1059 and timeline buffers."
1060 :group 'org-agenda-daily/weekly
1061 :type '(set :greedy t
1062 (const :tag "Monday" 1)
1063 (const :tag "Tuesday" 2)
1064 (const :tag "Wednesday" 3)
1065 (const :tag "Thursday" 4)
1066 (const :tag "Friday" 5)
1067 (const :tag "Saturday" 6)
1068 (const :tag "Sunday" 0)))
1070 (defcustom org-agenda-include-diary nil
1071 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1072 Custom commands can set this variable in the options section."
1073 :group 'org-agenda-daily/weekly
1074 :type 'boolean)
1076 (defcustom org-agenda-include-deadlines t
1077 "If non-nil, include entries within their deadline warning period.
1078 Custom commands can set this variable in the options section."
1079 :group 'org-agenda-daily/weekly
1080 :type 'boolean)
1082 (defcustom org-agenda-repeating-timestamp-show-all t
1083 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1084 When set to a list of strings, only show occurrences of repeating
1085 stamps for these TODO keywords. When nil, only one occurrence is
1086 shown, either today or the nearest into the future."
1087 :group 'org-agenda-daily/weekly
1088 :type '(choice
1089 (const :tag "Show repeating stamps" t)
1090 (repeat :tag "Show repeating stamps for these TODO keywords"
1091 (string :tag "TODO Keyword"))
1092 (const :tag "Don't show repeating stamps" nil)))
1094 (defcustom org-scheduled-past-days 10000
1095 "No. of days to continue listing scheduled items that are not marked DONE.
1096 When an item is scheduled on a date, it shows up in the agenda on this
1097 day and will be listed until it is marked done for the number of days
1098 given here."
1099 :group 'org-agenda-daily/weekly
1100 :type 'integer)
1102 (defcustom org-agenda-log-mode-items '(closed clock)
1103 "List of items that should be shown in agenda log mode.
1104 This list may contain the following symbols:
1106 closed Show entries that have been closed on that day.
1107 clock Show entries that have received clocked time on that day.
1108 state Show all logged state changes.
1109 Note that instead of changing this variable, you can also press `C-u l' in
1110 the agenda to display all available LOG items temporarily."
1111 :group 'org-agenda-daily/weekly
1112 :type '(set :greedy t (const closed) (const clock) (const state)))
1114 (defcustom org-agenda-clock-consistency-checks
1115 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1116 :gap-ok-around ("4:00")
1117 :default-face ((:background "DarkRed") (:foreground "white"))
1118 :overlap-face nil :gap-face nil :no-end-time-face nil
1119 :long-face nil :short-face nil)
1120 "This is a property list, with the following keys:
1122 :max-duration Mark clocking chunks that are longer than this time.
1123 This is a time string like \"HH:MM\", or the number
1124 of minutes as an integer.
1126 :min-duration Mark clocking chunks that are shorter that this.
1127 This is a time string like \"HH:MM\", or the number
1128 of minutes as an integer.
1130 :max-gap Mark gaps between clocking chunks that are longer than
1131 this duration. A number of minutes, or a string
1132 like \"HH:MM\".
1134 :gap-ok-around List of times during the day which are usually not working
1135 times. When a gap is detected, but the gap contains any
1136 of these times, the gap is *not* reported. For example,
1137 if this is (\"4:00\" \"13:00\") then gaps that contain
1138 4:00 in the morning (i.e. the night) and 13:00
1139 (i.e. a typical lunch time) do not cause a warning.
1140 You should have at least one time during the night in this
1141 list, or otherwise the first task each morning will trigger
1142 a warning because it follows a long gap.
1144 Furthermore, the following properties can be used to define faces for
1145 issue display.
1147 :default-face the default face, if the specific face is undefined
1148 :overlap-face face for overlapping clocks
1149 :gap-face face for gaps between clocks
1150 :no-end-time-face face for incomplete clocks
1151 :long-face face for clock intervals that are too long
1152 :short-face face for clock intervals that are too short"
1153 :group 'org-agenda-daily/weekly
1154 :group 'org-clock
1155 :type 'plist)
1157 (defcustom org-agenda-log-mode-add-notes t
1158 "Non-nil means add first line of notes to log entries in agenda views.
1159 If a log item like a state change or a clock entry is associated with
1160 notes, the first line of these notes will be added to the entry in the
1161 agenda display."
1162 :group 'org-agenda-daily/weekly
1163 :type 'boolean)
1165 (defcustom org-agenda-start-with-log-mode nil
1166 "The initial value of log-mode in a newly created agenda window."
1167 :group 'org-agenda-startup
1168 :group 'org-agenda-daily/weekly
1169 :type 'boolean)
1171 (defcustom org-agenda-start-with-clockreport-mode nil
1172 "The initial value of clockreport-mode in a newly created agenda window."
1173 :group 'org-agenda-startup
1174 :group 'org-agenda-daily/weekly
1175 :type 'boolean)
1177 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1178 "Property list with parameters for the clocktable in clockreport mode.
1179 This is the display mode that shows a clock table in the daily/weekly
1180 agenda, the properties for this dynamic block can be set here.
1181 The usual clocktable parameters are allowed here, but you cannot set
1182 the properties :name, :tstart, :tend, :block, and :scope - these will
1183 be overwritten to make sure the content accurately reflects the
1184 current display in the agenda."
1185 :group 'org-agenda-daily/weekly
1186 :type 'plist)
1188 (defcustom org-agenda-search-view-always-boolean nil
1189 "Non-nil means the search string is interpreted as individual parts.
1191 The search string for search view can either be interpreted as a phrase,
1192 or as a list of snippets that define a boolean search for a number of
1193 strings.
1195 When this is non-nil, the string will be split on whitespace, and each
1196 snippet will be searched individually, and all must match in order to
1197 select an entry. A snippet is then a single string of non-white
1198 characters, or a string in double quotes, or a regexp in {} braces.
1199 If a snippet is preceded by \"-\", the snippet must *not* match.
1200 \"+\" is syntactic sugar for positive selection. Each snippet may
1201 be found as a full word or a partial word, but see the variable
1202 `org-agenda-search-view-force-full-words'.
1204 When this is nil, search will look for the entire search phrase as one,
1205 with each space character matching any amount of whitespace, including
1206 line breaks.
1208 Even when this is nil, you can still switch to Boolean search dynamically
1209 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1210 is a regexp marked with braces like \"{abc}\", this will also switch to
1211 boolean search."
1212 :group 'org-agenda-search-view
1213 :type 'boolean)
1215 (if (fboundp 'defvaralias)
1216 (defvaralias 'org-agenda-search-view-search-words-only
1217 'org-agenda-search-view-always-boolean))
1219 (defcustom org-agenda-search-view-force-full-words nil
1220 "Non-nil means, search words must be matches as complete words.
1221 When nil, they may also match part of a word."
1222 :group 'org-agenda-search-view
1223 :type 'boolean)
1225 (defgroup org-agenda-time-grid nil
1226 "Options concerning the time grid in the Org-mode Agenda."
1227 :tag "Org Agenda Time Grid"
1228 :group 'org-agenda)
1230 (defcustom org-agenda-search-headline-for-time t
1231 "Non-nil means search headline for a time-of-day.
1232 If the headline contains a time-of-day in one format or another, it will
1233 be used to sort the entry into the time sequence of items for a day.
1234 Some people have time stamps in the headline that refer to the creation
1235 time or so, and then this produces an unwanted side effect. If this is
1236 the case for your, use this variable to turn off searching the headline
1237 for a time."
1238 :group 'org-agenda-time-grid
1239 :type 'boolean)
1241 (defcustom org-agenda-use-time-grid t
1242 "Non-nil means show a time grid in the agenda schedule.
1243 A time grid is a set of lines for specific times (like every two hours between
1244 8:00 and 20:00). The items scheduled for a day at specific times are
1245 sorted in between these lines.
1246 For details about when the grid will be shown, and what it will look like, see
1247 the variable `org-agenda-time-grid'."
1248 :group 'org-agenda-time-grid
1249 :type 'boolean)
1251 (defcustom org-agenda-time-grid
1252 '((daily today require-timed)
1253 "----------------"
1254 (800 1000 1200 1400 1600 1800 2000))
1256 "The settings for time grid for agenda display.
1257 This is a list of three items. The first item is again a list. It contains
1258 symbols specifying conditions when the grid should be displayed:
1260 daily if the agenda shows a single day
1261 weekly if the agenda shows an entire week
1262 today show grid on current date, independent of daily/weekly display
1263 require-timed show grid only if at least one item has a time specification
1265 The second item is a string which will be placed behind the grid time.
1267 The third item is a list of integers, indicating the times that should have
1268 a grid line."
1269 :group 'org-agenda-time-grid
1270 :type
1271 '(list
1272 (set :greedy t :tag "Grid Display Options"
1273 (const :tag "Show grid in single day agenda display" daily)
1274 (const :tag "Show grid in weekly agenda display" weekly)
1275 (const :tag "Always show grid for today" today)
1276 (const :tag "Show grid only if any timed entries are present"
1277 require-timed)
1278 (const :tag "Skip grid times already present in an entry"
1279 remove-match))
1280 (string :tag "Grid String")
1281 (repeat :tag "Grid Times" (integer :tag "Time"))))
1283 (defcustom org-agenda-show-current-time-in-grid t
1284 "Non-nil means show the current time in the time grid."
1285 :group 'org-agenda-time-grid
1286 :type 'boolean)
1288 (defcustom org-agenda-current-time-string
1289 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1290 "The string for the current time marker in the agenda."
1291 :group 'org-agenda-time-grid
1292 :type 'string)
1294 (defgroup org-agenda-sorting nil
1295 "Options concerning sorting in the Org-mode Agenda."
1296 :tag "Org Agenda Sorting"
1297 :group 'org-agenda)
1299 (defcustom org-agenda-sorting-strategy
1300 '((agenda habit-down time-up priority-down category-keep)
1301 (todo priority-down category-keep)
1302 (tags priority-down category-keep)
1303 (search category-keep))
1304 "Sorting structure for the agenda items of a single day.
1305 This is a list of symbols which will be used in sequence to determine
1306 if an entry should be listed before another entry. The following
1307 symbols are recognized:
1309 time-up Put entries with time-of-day indications first, early first
1310 time-down Put entries with time-of-day indications first, late first
1311 category-keep Keep the default order of categories, corresponding to the
1312 sequence in `org-agenda-files'.
1313 category-up Sort alphabetically by category, A-Z.
1314 category-down Sort alphabetically by category, Z-A.
1315 tag-up Sort alphabetically by last tag, A-Z.
1316 tag-down Sort alphabetically by last tag, Z-A.
1317 priority-up Sort numerically by priority, high priority last.
1318 priority-down Sort numerically by priority, high priority first.
1319 todo-state-up Sort by todo state, tasks that are done last.
1320 todo-state-down Sort by todo state, tasks that are done first.
1321 effort-up Sort numerically by estimated effort, high effort last.
1322 effort-down Sort numerically by estimated effort, high effort first.
1323 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1324 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1325 habit-up Put entries that are habits first
1326 habit-down Put entries that are habits last
1327 alpha-up Sort headlines alphabetically
1328 alpha-down Sort headlines alphabetically, reversed
1330 The different possibilities will be tried in sequence, and testing stops
1331 if one comparison returns a \"not-equal\". For example, the default
1332 '(time-up category-keep priority-down)
1333 means: Pull out all entries having a specified time of day and sort them,
1334 in order to make a time schedule for the current day the first thing in the
1335 agenda listing for the day. Of the entries without a time indication, keep
1336 the grouped in categories, don't sort the categories, but keep them in
1337 the sequence given in `org-agenda-files'. Within each category sort by
1338 priority.
1340 Leaving out `category-keep' would mean that items will be sorted across
1341 categories by priority.
1343 Instead of a single list, this can also be a set of list for specific
1344 contents, with a context symbol in the car of the list, any of
1345 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1347 Custom commands can bind this variable in the options section."
1348 :group 'org-agenda-sorting
1349 :type `(choice
1350 (repeat :tag "General" ,org-sorting-choice)
1351 (list :tag "Individually"
1352 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1353 (repeat ,org-sorting-choice))
1354 (cons (const :tag "Strategy for TODO lists" todo)
1355 (repeat ,org-sorting-choice))
1356 (cons (const :tag "Strategy for Tags matches" tags)
1357 (repeat ,org-sorting-choice))
1358 (cons (const :tag "Strategy for search matches" search)
1359 (repeat ,org-sorting-choice)))))
1361 (defcustom org-agenda-cmp-user-defined nil
1362 "A function to define the comparison `user-defined'.
1363 This function must receive two arguments, agenda entry a and b.
1364 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1365 the user comparison, return nil.
1366 When this is defined, you can make `user-defined-up' and `user-defined-down'
1367 part of an agenda sorting strategy."
1368 :group 'org-agenda-sorting
1369 :type 'symbol)
1371 (defcustom org-sort-agenda-notime-is-late t
1372 "Non-nil means items without time are considered late.
1373 This is only relevant for sorting. When t, items which have no explicit
1374 time like 15:30 will be considered as 99:01, i.e. later than any items which
1375 do have a time. When nil, the default time is before 0:00. You can use this
1376 option to decide if the schedule for today should come before or after timeless
1377 agenda entries."
1378 :group 'org-agenda-sorting
1379 :type 'boolean)
1381 (defcustom org-sort-agenda-noeffort-is-high t
1382 "Non-nil means items without effort estimate are sorted as high effort.
1383 This also applies when filtering an agenda view with respect to the
1384 < or > effort operator. Then, tasks with no effort defined will be treated
1385 as tasks with high effort.
1386 When nil, such items are sorted as 0 minutes effort."
1387 :group 'org-agenda-sorting
1388 :type 'boolean)
1390 (defgroup org-agenda-line-format nil
1391 "Options concerning the entry prefix in the Org-mode agenda display."
1392 :tag "Org Agenda Line Format"
1393 :group 'org-agenda)
1395 (defcustom org-agenda-prefix-format
1396 '((agenda . " %i %-12:c%?-12t% s")
1397 (timeline . " % s")
1398 (todo . " %i %-12:c")
1399 (tags . " %i %-12:c")
1400 (search . " %i %-12:c"))
1401 "Format specifications for the prefix of items in the agenda views.
1402 An alist with five entries, each for the different agenda types. The
1403 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1404 The values are format strings.
1406 This format works similar to a printf format, with the following meaning:
1408 %c the category of the item, \"Diary\" for entries from the diary,
1409 or as given by the CATEGORY keyword or derived from the file name
1410 %e the effort required by the item
1411 %i the icon category of the item, see `org-agenda-category-icon-alist'
1412 %T the last tag of the item (ignore inherited tags, which come first)
1413 %t the HH:MM time-of-day specification if one applies to the entry
1414 %s Scheduling/Deadline information, a short string
1415 %(expression) Eval EXPRESSION and replace the control string
1416 by the result
1418 All specifiers work basically like the standard `%s' of printf, but may
1419 contain two additional characters: a question mark just after the `%'
1420 and a whitespace/punctuation character just before the final letter.
1422 If the first character after `%' is a question mark, the entire field
1423 will only be included if the corresponding value applies to the current
1424 entry. This is useful for fields which should have fixed width when
1425 present, but zero width when absent. For example, \"%?-12t\" will
1426 result in a 12 character time field if a time of the day is specified,
1427 but will completely disappear in entries which do not contain a time.
1429 If there is punctuation or whitespace character just before the final
1430 format letter, this character will be appended to the field value if
1431 the value is not empty. For example, the format \"%-12:c\" leads to
1432 \"Diary: \" if the category is \"Diary\". If the category were be
1433 empty, no additional colon would be inserted.
1435 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1436 which means:
1438 - Indent the line with two space characters
1439 - Give the category a 12 chars wide field, padded with whitespace on
1440 the right (because of `-'). Append a colon if there is a category
1441 (because of `:').
1442 - If there is a time-of-day, put it into a 12 chars wide field. If no
1443 time, don't put in an empty field, just skip it (because of '?').
1444 - Finally, put the scheduling information.
1446 See also the variables `org-agenda-remove-times-when-in-prefix' and
1447 `org-agenda-remove-tags'.
1449 Custom commands can set this variable in the options section."
1450 :type '(choice
1451 (string :tag "General format")
1452 (list :greedy t :tag "View dependent"
1453 (cons (const agenda) (string :tag "Format"))
1454 (cons (const timeline) (string :tag "Format"))
1455 (cons (const todo) (string :tag "Format"))
1456 (cons (const tags) (string :tag "Format"))
1457 (cons (const search) (string :tag "Format"))))
1458 :group 'org-agenda-line-format)
1460 (defvar org-prefix-format-compiled nil
1461 "The compiled version of the most recently used prefix format.
1462 See the variable `org-agenda-prefix-format'.")
1464 (defcustom org-agenda-todo-keyword-format "%-1s"
1465 "Format for the TODO keyword in agenda lines.
1466 Set this to something like \"%-12s\" if you want all TODO keywords
1467 to occupy a fixed space in the agenda display."
1468 :group 'org-agenda-line-format
1469 :type 'string)
1471 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1472 "Text preceding timerange entries in the agenda view.
1473 This is a list with two strings. The first applies when the range
1474 is entirely on one day. The second applies if the range spans several days.
1475 The strings may have two \"%d\" format specifiers which will be filled
1476 with the sequence number of the days, and the total number of days in the
1477 range, respectively."
1478 :group 'org-agenda-line-format
1479 :type '(list
1480 (string :tag "Deadline today ")
1481 (choice :tag "Deadline relative"
1482 (string :tag "Format string")
1483 (function))))
1485 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1486 "Text preceding scheduled items in the agenda view.
1487 This is a list with two strings. The first applies when the item is
1488 scheduled on the current day. The second applies when it has been scheduled
1489 previously, it may contain a %d indicating that this is the nth time that
1490 this item is scheduled, due to automatic rescheduling of unfinished items
1491 for the following day. So this number is one larger than the number of days
1492 that passed since this item was scheduled first."
1493 :group 'org-agenda-line-format
1494 :type '(list
1495 (string :tag "Scheduled today ")
1496 (string :tag "Scheduled previously")))
1498 (defcustom org-agenda-inactive-leader "["
1499 "Text preceding item pulled into the agenda by inactive time stamps.
1500 These entries are added to the agenda when pressing \"[\"."
1501 :group 'org-agenda-line-format
1502 :type '(list
1503 (string :tag "Scheduled today ")
1504 (string :tag "Scheduled previously")))
1506 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1507 "Text preceding deadline items in the agenda view.
1508 This is a list with two strings. The first applies when the item has its
1509 deadline on the current day. The second applies when it is in the past or
1510 in the future, it may contain %d to capture how many days away the deadline
1511 is (was)."
1512 :group 'org-agenda-line-format
1513 :type '(list
1514 (string :tag "Deadline today ")
1515 (choice :tag "Deadline relative"
1516 (string :tag "Format string")
1517 (function))))
1519 (defcustom org-agenda-remove-times-when-in-prefix t
1520 "Non-nil means remove duplicate time specifications in agenda items.
1521 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1522 time-of-day specification in a headline or diary entry is extracted and
1523 placed into the prefix. If this option is non-nil, the original specification
1524 \(a timestamp or -range, or just a plain time(range) specification like
1525 11:30-4pm) will be removed for agenda display. This makes the agenda less
1526 cluttered.
1527 The option can be t or nil. It may also be the symbol `beg', indicating
1528 that the time should only be removed when it is located at the beginning of
1529 the headline/diary entry."
1530 :group 'org-agenda-line-format
1531 :type '(choice
1532 (const :tag "Always" t)
1533 (const :tag "Never" nil)
1534 (const :tag "When at beginning of entry" beg)))
1536 (defcustom org-agenda-remove-timeranges-from-blocks nil
1537 "Non-nil means remove time ranges specifications in agenda
1538 items that span on several days."
1539 :group 'org-agenda-line-format
1540 :type 'boolean)
1542 (defcustom org-agenda-default-appointment-duration nil
1543 "Default duration for appointments that only have a starting time.
1544 When nil, no duration is specified in such cases.
1545 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1546 :group 'org-agenda-line-format
1547 :type '(choice
1548 (integer :tag "Minutes")
1549 (const :tag "No default duration")))
1551 (defcustom org-agenda-show-inherited-tags t
1552 "Non-nil means show inherited tags in each agenda line."
1553 :group 'org-agenda-line-format
1554 :type 'boolean)
1556 (defcustom org-agenda-hide-tags-regexp nil
1557 "Regular expression used to filter away specific tags in agenda views.
1558 This means that these tags will be present, but not be shown in the agenda
1559 line. Secondary filtering will still work on the hidden tags.
1560 Nil means don't hide any tags."
1561 :group 'org-agenda-line-format
1562 :type '(choice
1563 (const :tag "Hide none" nil)
1564 (string :tag "Regexp ")))
1566 (defcustom org-agenda-remove-tags nil
1567 "Non-nil means remove the tags from the headline copy in the agenda.
1568 When this is the symbol `prefix', only remove tags when
1569 `org-agenda-prefix-format' contains a `%T' specifier."
1570 :group 'org-agenda-line-format
1571 :type '(choice
1572 (const :tag "Always" t)
1573 (const :tag "Never" nil)
1574 (const :tag "When prefix format contains %T" prefix)))
1576 (if (fboundp 'defvaralias)
1577 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1578 'org-agenda-remove-tags))
1580 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1581 "Shift tags in agenda items to this column.
1582 If this number is positive, it specifies the column. If it is negative,
1583 it means that the tags should be flushright to that column. For example,
1584 -80 works well for a normal 80 character screen."
1585 :group 'org-agenda-line-format
1586 :type 'integer)
1588 (if (fboundp 'defvaralias)
1589 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1591 (defcustom org-agenda-fontify-priorities 'cookies
1592 "Non-nil means highlight low and high priorities in agenda.
1593 When t, the highest priority entries are bold, lowest priority italic.
1594 However, settings in `org-priority-faces' will overrule these faces.
1595 When this variable is the symbol `cookies', only fontify the
1596 cookies, not the entire task.
1597 This may also be an association list of priority faces, whose
1598 keys are the character values of `org-highest-priority',
1599 `org-default-priority', and `org-lowest-priority' (the default values
1600 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1601 color as a string, or a list like `(:background \"Red\")'.
1602 If it is a color, the variable `org-faces-easy-properties'
1603 determines if it is a foreground or a background color."
1604 :group 'org-agenda-line-format
1605 :type '(choice
1606 (const :tag "Never" nil)
1607 (const :tag "Defaults" t)
1608 (const :tag "Cookies only" cookies)
1609 (repeat :tag "Specify"
1610 (list (character :tag "Priority" :value ?A)
1611 (choice :tag "Face "
1612 (string :tag "Color")
1613 (sexp :tag "Face"))))))
1615 (defcustom org-agenda-day-face-function nil
1616 "Function called to determine what face should be used to display a day.
1617 The only argument passed to that function is the day. It should
1618 returns a face, or nil if does not want to specify a face and let
1619 the normal rules apply."
1620 :group 'org-agenda-line-format
1621 :type 'function)
1623 (defcustom org-agenda-category-icon-alist nil
1624 "Alist of category icon to be displayed in agenda views.
1626 Each entry should have the following format:
1628 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1630 Where CATEGORY-REGEXP is a regexp matching the categories where
1631 the icon should be displayed.
1632 FILE-OR-DATA either a file path or a string containing image data.
1634 The other fields can be omited safely if not needed:
1635 TYPE indicates the image type.
1636 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1637 image data.
1638 PROPS are additional image attributes to assign to the image,
1639 like, e.g. `:ascent center'.
1641 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1643 If you want to set the display properties yourself, just put a
1644 list as second element:
1646 (CATEGORY-REGEXP (MY PROPERTY LIST))
1648 For example, to display a 16px horizontal space for Emacs
1649 category, you can use:
1651 (\"Emacs\" '(space . (:width (16))))"
1652 :group 'org-agenda-line-format
1653 :type '(alist :key-type (string :tag "Regexp matching category")
1654 :value-type (choice (list :tag "Icon"
1655 (string :tag "File or data")
1656 (symbol :tag "Type")
1657 (boolean :tag "Data?")
1658 (repeat :tag "Extra image properties" :inline t symbol))
1659 (list :tag "Display properties" sexp))))
1661 (defgroup org-agenda-column-view nil
1662 "Options concerning column view in the agenda."
1663 :tag "Org Agenda Column View"
1664 :group 'org-agenda)
1666 (defcustom org-agenda-columns-show-summaries t
1667 "Non-nil means show summaries for columns displayed in the agenda view."
1668 :group 'org-agenda-column-view
1669 :type 'boolean)
1671 (defcustom org-agenda-columns-compute-summary-properties t
1672 "Non-nil means recompute all summary properties before column view.
1673 When column view in the agenda is listing properties that have a summary
1674 operator, it can go to all relevant buffers and recompute the summaries
1675 there. This can mean overhead for the agenda column view, but is necessary
1676 to have thing up to date.
1677 As a special case, a CLOCKSUM property also makes sure that the clock
1678 computations are current."
1679 :group 'org-agenda-column-view
1680 :type 'boolean)
1682 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1683 "Non-nil means the duration of an appointment will add to day effort.
1684 The property to which appointment durations will be added is the one given
1685 in the option `org-effort-property'. If an appointment does not have
1686 an end time, `org-agenda-default-appointment-duration' will be used. If that
1687 is not set, an appointment without end time will not contribute to the time
1688 estimate."
1689 :group 'org-agenda-column-view
1690 :type 'boolean)
1692 (defcustom org-agenda-auto-exclude-function nil
1693 "A function called with a tag to decide if it is filtered on '/ RET'.
1694 The sole argument to the function, which is called once for each
1695 possible tag, is a string giving the name of the tag. The
1696 function should return either nil if the tag should be included
1697 as normal, or \"-<TAG>\" to exclude the tag.
1698 Note that for the purpose of tag filtering, only the lower-case version of
1699 all tags will be considered, so that this function will only ever see
1700 the lower-case version of all tags."
1701 :group 'org-agenda
1702 :type 'function)
1704 (defcustom org-agenda-bulk-custom-functions nil
1705 "Alist of characters and custom functions for bulk actions.
1706 For example, this value makes those two functions available:
1708 '((?R set-category)
1709 (?C bulk-cut))
1711 With selected entries in an agenda buffer, `B R' will call
1712 the custom function `set-category' on the selected entries.
1713 Note that functions in this alist don't need to be quoted."
1714 :type 'alist
1715 :group 'org-agenda)
1717 (eval-when-compile
1718 (require 'cl))
1719 (require 'org)
1721 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1722 "Execute BODY with point at location given by `org-hd-marker' property.
1723 If STRING is non-nil, the text property will be fetched from position 0
1724 in that string. If STRING is nil, it will be fetched from the beginning
1725 of the current line."
1726 (org-with-gensyms (marker)
1727 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1728 'org-hd-marker string)))
1729 (with-current-buffer (marker-buffer ,marker)
1730 (save-excursion
1731 (goto-char ,marker)
1732 ,@body)))))
1733 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1735 (defun org-add-agenda-custom-command (entry)
1736 "Replace or add a command in `org-agenda-custom-commands'.
1737 This is mostly for hacking and trying a new command - once the command
1738 works you probably want to add it to `org-agenda-custom-commands' for good."
1739 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1740 (if ass
1741 (setcdr ass (cdr entry))
1742 (push entry org-agenda-custom-commands))))
1744 ;;; Define the Org-agenda-mode
1746 (defvar org-agenda-mode-map (make-sparse-keymap)
1747 "Keymap for `org-agenda-mode'.")
1748 (if (fboundp 'defvaralias)
1749 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1751 (defvar org-agenda-menu) ; defined later in this file.
1752 (defvar org-agenda-restrict) ; defined later in this file.
1753 (defvar org-agenda-follow-mode nil)
1754 (defvar org-agenda-entry-text-mode nil)
1755 (defvar org-agenda-clockreport-mode nil)
1756 (defvar org-agenda-show-log nil)
1757 (defvar org-agenda-redo-command nil)
1758 (defvar org-agenda-query-string nil)
1759 (defvar org-agenda-mode-hook nil
1760 "Hook for `org-agenda-mode', run after the mode is turned on.")
1761 (defvar org-agenda-type nil)
1762 (defvar org-agenda-force-single-file nil)
1763 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1765 (defun org-agenda-mode ()
1766 "Mode for time-sorted view on action items in Org-mode files.
1768 The following commands are available:
1770 \\{org-agenda-mode-map}"
1771 (interactive)
1772 (kill-all-local-variables)
1773 (setq org-agenda-undo-list nil
1774 org-agenda-pending-undo-list nil
1775 org-agenda-bulk-marked-entries nil)
1776 (setq major-mode 'org-agenda-mode)
1777 ;; Keep global-font-lock-mode from turning on font-lock-mode
1778 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1779 (setq mode-name "Org-Agenda")
1780 (use-local-map org-agenda-mode-map)
1781 (easy-menu-add org-agenda-menu)
1782 (if org-startup-truncated (setq truncate-lines t))
1783 (org-set-local 'line-move-visual nil)
1784 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1785 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1786 ;; Make sure properties are removed when copying text
1787 (when (boundp 'buffer-substring-filters)
1788 (org-set-local 'buffer-substring-filters
1789 (cons (lambda (x)
1790 (set-text-properties 0 (length x) nil x) x)
1791 buffer-substring-filters)))
1792 (unless org-agenda-keep-modes
1793 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1794 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1795 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1796 org-agenda-show-log org-agenda-start-with-log-mode))
1798 (easy-menu-change
1799 '("Agenda") "Agenda Files"
1800 (append
1801 (list
1802 (vector
1803 (if (get 'org-agenda-files 'org-restrict)
1804 "Restricted to single file"
1805 "Edit File List")
1806 '(org-edit-agenda-file-list)
1807 (not (get 'org-agenda-files 'org-restrict)))
1808 "--")
1809 (mapcar 'org-file-menu-entry (org-agenda-files))))
1810 (org-agenda-set-mode-name)
1811 (apply
1812 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1813 (list 'org-agenda-mode-hook)))
1815 (substitute-key-definition 'undo 'org-agenda-undo
1816 org-agenda-mode-map global-map)
1817 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1818 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1819 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1820 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1821 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1822 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1823 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1824 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1825 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1826 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1827 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1828 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1829 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1830 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1831 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1832 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1833 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1834 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1835 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1836 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1837 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1838 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1839 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1840 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1841 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1842 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1843 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1844 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1845 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1846 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1847 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1848 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1849 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1850 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1851 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1852 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1853 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1854 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1855 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1856 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1857 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1858 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1859 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1860 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1861 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1863 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1864 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1865 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1866 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1867 (while l (org-defkey org-agenda-mode-map
1868 (int-to-string (pop l)) 'digit-argument)))
1870 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1871 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1872 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1873 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1874 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1875 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1876 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1877 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1878 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1879 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1880 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1881 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1882 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1883 'org-clock-modify-effort-estimate)
1884 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1885 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1886 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1887 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1888 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1889 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1890 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1891 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1892 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1893 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1894 (substitute-key-definition 'next-line 'org-agenda-next-line
1895 org-agenda-mode-map global-map)
1896 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1897 org-agenda-mode-map global-map)
1898 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1899 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1900 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1901 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1902 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1903 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1904 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1905 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1906 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1907 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1908 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1909 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1910 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1911 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1912 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1913 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1914 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1915 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1916 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1917 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1918 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1919 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1920 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1921 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1922 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1923 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1924 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1925 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1926 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1927 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1929 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1930 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1931 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1932 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1933 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1934 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1935 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1936 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1937 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1938 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1940 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1941 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1942 (when org-agenda-mouse-1-follows-link
1943 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1944 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1945 '("Agenda"
1946 ("Agenda Files")
1947 "--"
1948 ("Agenda Dates"
1949 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1950 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1951 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1952 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1953 "--"
1954 ("View"
1955 ["Day View" org-agenda-day-view
1956 :active (org-agenda-check-type nil 'agenda)
1957 :style radio :selected (eq org-agenda-current-span 'day)
1958 :keys "v d (or just d)"]
1959 ["Week View" org-agenda-week-view
1960 :active (org-agenda-check-type nil 'agenda)
1961 :style radio :selected (eq org-agenda-current-span 'week)
1962 :keys "v w (or just w)"]
1963 ["Month View" org-agenda-month-view
1964 :active (org-agenda-check-type nil 'agenda)
1965 :style radio :selected (eq org-agenda-current-span 'month)
1966 :keys "v m"]
1967 ["Year View" org-agenda-year-view
1968 :active (org-agenda-check-type nil 'agenda)
1969 :style radio :selected (eq org-agenda-current-span 'year)
1970 :keys "v y"]
1971 "--"
1972 ["Include Diary" org-agenda-toggle-diary
1973 :style toggle :selected org-agenda-include-diary
1974 :active (org-agenda-check-type nil 'agenda)]
1975 ["Include Deadlines" org-agenda-toggle-deadlines
1976 :style toggle :selected org-agenda-include-deadlines
1977 :active (org-agenda-check-type nil 'agenda)]
1978 ["Use Time Grid" org-agenda-toggle-time-grid
1979 :style toggle :selected org-agenda-use-time-grid
1980 :active (org-agenda-check-type nil 'agenda)]
1981 "--"
1982 ["Show clock report" org-agenda-clockreport-mode
1983 :style toggle :selected org-agenda-clockreport-mode
1984 :active (org-agenda-check-type nil 'agenda)]
1985 ["Show some entry text" org-agenda-entry-text-mode
1986 :style toggle :selected org-agenda-entry-text-mode
1987 :active t]
1988 "--"
1989 ["Show Logbook entries" org-agenda-log-mode
1990 :style toggle :selected org-agenda-show-log
1991 :active (org-agenda-check-type nil 'agenda 'timeline)
1992 :keys "v l (or just l)"]
1993 ["Include archived trees" org-agenda-archives-mode
1994 :style toggle :selected org-agenda-archives-mode :active t
1995 :keys "v a"]
1996 ["Include archive files" (org-agenda-archives-mode t)
1997 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1998 :keys "v A"]
1999 "--"
2000 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2001 ["Write view to file" org-write-agenda t]
2002 ["Rebuild buffer" org-agenda-redo t]
2003 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2004 "--"
2005 ["Show original entry" org-agenda-show t]
2006 ["Go To (other window)" org-agenda-goto t]
2007 ["Go To (this window)" org-agenda-switch-to t]
2008 ["Follow Mode" org-agenda-follow-mode
2009 :style toggle :selected org-agenda-follow-mode :active t]
2010 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2011 "--"
2012 ("TODO"
2013 ["Cycle TODO" org-agenda-todo t]
2014 ["Next TODO set" org-agenda-todo-nextset t]
2015 ["Previous TODO set" org-agenda-todo-previousset t]
2016 ["Add note" org-agenda-add-note t])
2017 ("Archive/Refile/Delete"
2018 ["Archive default" org-agenda-archive-default t]
2019 ["Archive default" org-agenda-archive-default-with-confirmation t]
2020 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2021 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2022 ["Archive subtree" org-agenda-archive t]
2023 "--"
2024 ["Refile" org-agenda-refile t]
2025 "--"
2026 ["Delete subtree" org-agenda-kill t])
2027 ("Bulk action"
2028 ["Mark entry" org-agenda-bulk-mark t]
2029 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2030 ["Unmark entry" org-agenda-bulk-unmark t]
2031 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2032 ["Act on all marked" org-agenda-bulk-action t]
2033 "--"
2034 ("Tags and Properties"
2035 ["Show all Tags" org-agenda-show-tags t]
2036 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2037 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2038 "--"
2039 ["Column View" org-columns t])
2040 ("Deadline/Schedule"
2041 ["Schedule" org-agenda-schedule t]
2042 ["Set Deadline" org-agenda-deadline t]
2043 "--"
2044 ["Mark item" org-agenda-action :active t :keys "k m"]
2045 ["Show mark item" org-agenda-action :active t :keys "k v"]
2046 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2047 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2048 "--"
2049 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2050 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2051 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2052 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2053 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2054 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2055 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2056 ("Clock and Effort"
2057 ["Clock in" org-agenda-clock-in t]
2058 ["Clock out" org-agenda-clock-out t]
2059 ["Clock cancel" org-agenda-clock-cancel t]
2060 ["Goto running clock" org-clock-goto t]
2061 "--"
2062 ["Set Effort" org-agenda-set-effort t]
2063 ["Change clocked effort" org-clock-modify-effort-estimate
2064 (org-clock-is-active)])
2065 ("Priority"
2066 ["Set Priority" org-agenda-priority t]
2067 ["Increase Priority" org-agenda-priority-up t]
2068 ["Decrease Priority" org-agenda-priority-down t]
2069 ["Show Priority" org-agenda-show-priority t])
2070 ("Calendar/Diary"
2071 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2072 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2073 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2074 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2075 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2076 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2077 "--"
2078 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2079 "--"
2080 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2081 "--"
2082 ("MobileOrg"
2083 ["Push Files and Views" org-mobile-push t]
2084 ["Get Captured and Flagged" org-mobile-pull t]
2085 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2086 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2087 "--"
2088 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2089 "--"
2090 ["Quit" org-agenda-quit t]
2091 ["Exit and Release Buffers" org-agenda-exit t]
2094 ;;; Agenda undo
2096 (defvar org-agenda-allow-remote-undo t
2097 "Non-nil means allow remote undo from the agenda buffer.")
2098 (defvar org-agenda-undo-list nil
2099 "List of undoable operations in the agenda since last refresh.")
2100 (defvar org-agenda-undo-has-started-in nil
2101 "Buffers that have already seen `undo-start' in the current undo sequence.")
2102 (defvar org-agenda-pending-undo-list nil
2103 "In a series of undo commands, this is the list of remaining undo items.")
2105 (defun org-agenda-undo ()
2106 "Undo a remote editing step in the agenda.
2107 This undoes changes both in the agenda buffer and in the remote buffer
2108 that have been changed along."
2109 (interactive)
2110 (or org-agenda-allow-remote-undo
2111 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2112 (if (not (eq this-command last-command))
2113 (setq org-agenda-undo-has-started-in nil
2114 org-agenda-pending-undo-list org-agenda-undo-list))
2115 (if (not org-agenda-pending-undo-list)
2116 (error "No further undo information"))
2117 (let* ((entry (pop org-agenda-pending-undo-list))
2118 buf line cmd rembuf)
2119 (setq cmd (pop entry) line (pop entry))
2120 (setq rembuf (nth 2 entry))
2121 (org-with-remote-undo rembuf
2122 (while (bufferp (setq buf (pop entry)))
2123 (if (pop entry)
2124 (with-current-buffer buf
2125 (let ((last-undo-buffer buf)
2126 (inhibit-read-only t))
2127 (unless (memq buf org-agenda-undo-has-started-in)
2128 (push buf org-agenda-undo-has-started-in)
2129 (make-local-variable 'pending-undo-list)
2130 (undo-start))
2131 (while (and pending-undo-list
2132 (listp pending-undo-list)
2133 (not (car pending-undo-list)))
2134 (pop pending-undo-list))
2135 (undo-more 1))))))
2136 (org-goto-line line)
2137 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2139 (defun org-verify-change-for-undo (l1 l2)
2140 "Verify that a real change occurred between the undo lists L1 and L2."
2141 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2142 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2143 (not (eq l1 l2)))
2145 ;;; Agenda dispatch
2147 (defvar org-agenda-restrict nil)
2148 (defvar org-agenda-restrict-begin (make-marker))
2149 (defvar org-agenda-restrict-end (make-marker))
2150 (defvar org-agenda-last-dispatch-buffer nil)
2151 (defvar org-agenda-overriding-restriction nil)
2153 ;;;###autoload
2154 (defun org-agenda (&optional arg keys restriction)
2155 "Dispatch agenda commands to collect entries to the agenda buffer.
2156 Prompts for a command to execute. Any prefix arg will be passed
2157 on to the selected command. The default selections are:
2159 a Call `org-agenda-list' to display the agenda for current day or week.
2160 t Call `org-todo-list' to display the global todo list.
2161 T Call `org-todo-list' to display the global todo list, select only
2162 entries with a specific TODO keyword (the user gets a prompt).
2163 m Call `org-tags-view' to display headlines with tags matching
2164 a condition (the user is prompted for the condition).
2165 M Like `m', but select only TODO entries, no ordinary headlines.
2166 L Create a timeline for the current buffer.
2167 e Export views to associated files.
2168 s Search entries for keywords.
2169 / Multi occur across all agenda files and also files listed
2170 in `org-agenda-text-search-extra-files'.
2171 < Restrict agenda commands to buffer, subtree, or region.
2172 Press several times to get the desired effect.
2173 > Remove a previous restriction.
2174 # List \"stuck\" projects.
2175 ! Configure what \"stuck\" means.
2176 C Configure custom agenda commands.
2178 More commands can be added by configuring the variable
2179 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2180 searches can be pre-defined in this way.
2182 If the current buffer is in Org-mode and visiting a file, you can also
2183 first press `<' once to indicate that the agenda should be temporarily
2184 \(until the next use of \\[org-agenda]) restricted to the current file.
2185 Pressing `<' twice means to restrict to the current subtree or region
2186 \(if active)."
2187 (interactive "P")
2188 (catch 'exit
2189 (let* ((prefix-descriptions nil)
2190 (org-agenda-window-setup (if (equal (buffer-name)
2191 org-agenda-buffer-name)
2192 'current-window
2193 org-agenda-window-setup))
2194 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2195 (org-agenda-custom-commands
2196 ;; normalize different versions
2197 (delq nil
2198 (mapcar
2199 (lambda (x)
2200 (cond ((stringp (cdr x))
2201 (push x prefix-descriptions)
2202 nil)
2203 ((stringp (nth 1 x)) x)
2204 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2205 (t (cons (car x) (cons "" (cdr x))))))
2206 org-agenda-custom-commands)))
2207 (buf (current-buffer))
2208 (bfn (buffer-file-name (buffer-base-buffer)))
2209 entry key type match lprops ans)
2210 ;; Turn off restriction unless there is an overriding one,
2211 (unless org-agenda-overriding-restriction
2212 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2213 ;; There is a request to keep the file list in place
2214 (put 'org-agenda-files 'org-restrict nil))
2215 (setq org-agenda-restrict nil)
2216 (move-marker org-agenda-restrict-begin nil)
2217 (move-marker org-agenda-restrict-end nil))
2218 ;; Delete old local properties
2219 (put 'org-agenda-redo-command 'org-lprops nil)
2220 ;; Delete previously set last-arguments
2221 (put 'org-agenda-redo-command 'last-args nil)
2222 ;; Remember where this call originated
2223 (setq org-agenda-last-dispatch-buffer (current-buffer))
2224 (unless keys
2225 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2226 keys (car ans)
2227 restriction (cdr ans)))
2228 ;; Establish the restriction, if any
2229 (when (and (not org-agenda-overriding-restriction) restriction)
2230 (put 'org-agenda-files 'org-restrict (list bfn))
2231 (cond
2232 ((eq restriction 'region)
2233 (setq org-agenda-restrict t)
2234 (move-marker org-agenda-restrict-begin (region-beginning))
2235 (move-marker org-agenda-restrict-end (region-end)))
2236 ((eq restriction 'subtree)
2237 (save-excursion
2238 (setq org-agenda-restrict t)
2239 (org-back-to-heading t)
2240 (move-marker org-agenda-restrict-begin (point))
2241 (move-marker org-agenda-restrict-end
2242 (progn (org-end-of-subtree t)))))))
2244 ;; For example the todo list should not need it (but does...)
2245 (cond
2246 ((setq entry (assoc keys org-agenda-custom-commands))
2247 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2248 (progn
2249 (setq type (nth 2 entry) match (eval (nth 3 entry))
2250 lprops (nth 4 entry))
2251 (put 'org-agenda-redo-command 'org-lprops lprops)
2252 (cond
2253 ((eq type 'agenda)
2254 (org-let lprops '(org-agenda-list current-prefix-arg)))
2255 ((eq type 'alltodo)
2256 (org-let lprops '(org-todo-list current-prefix-arg)))
2257 ((eq type 'search)
2258 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2259 ((eq type 'stuck)
2260 (org-let lprops '(org-agenda-list-stuck-projects
2261 current-prefix-arg)))
2262 ((eq type 'tags)
2263 (org-let lprops '(org-tags-view current-prefix-arg match)))
2264 ((eq type 'tags-todo)
2265 (org-let lprops '(org-tags-view '(4) match)))
2266 ((eq type 'todo)
2267 (org-let lprops '(org-todo-list match)))
2268 ((eq type 'tags-tree)
2269 (org-check-for-org-mode)
2270 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2271 ((eq type 'todo-tree)
2272 (org-check-for-org-mode)
2273 (org-let lprops
2274 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2275 (regexp-quote match) "\\>"))))
2276 ((eq type 'occur-tree)
2277 (org-check-for-org-mode)
2278 (org-let lprops '(org-occur match)))
2279 ((functionp type)
2280 (org-let lprops '(funcall type match)))
2281 ((fboundp type)
2282 (org-let lprops '(funcall type match)))
2283 (t (error "Invalid custom agenda command type %s" type))))
2284 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2285 ((equal keys "C")
2286 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2287 (customize-variable 'org-agenda-custom-commands))
2288 ((equal keys "a") (call-interactively 'org-agenda-list))
2289 ((equal keys "s") (call-interactively 'org-search-view))
2290 ((equal keys "t") (call-interactively 'org-todo-list))
2291 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2292 ((equal keys "m") (call-interactively 'org-tags-view))
2293 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2294 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2295 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2296 (org-add-hook
2297 'post-command-hook
2298 (lambda ()
2299 (unless (current-message)
2300 (let* ((m (org-agenda-get-any-marker))
2301 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2302 (when note
2303 (message (concat
2304 "FLAGGING-NOTE ([?] for more info): "
2305 (org-add-props
2306 (replace-regexp-in-string
2307 "\\\\n" "//"
2308 (copy-sequence note))
2309 nil 'face 'org-warning)))))))
2310 t t))
2311 ((equal keys "L")
2312 (unless (org-mode-p)
2313 (error "This is not an Org-mode file"))
2314 (unless restriction
2315 (put 'org-agenda-files 'org-restrict (list bfn))
2316 (org-call-with-arg 'org-timeline arg)))
2317 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2318 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2319 ((equal keys "!") (customize-variable 'org-stuck-projects))
2320 (t (error "Invalid agenda key"))))))
2322 (defun org-agenda-append-agenda ()
2323 "Append another agenda view to the current one.
2324 This function allows interactive building of block agendas.
2325 Agenda views are separated by `org-agenda-block-separator'."
2326 (interactive)
2327 (unless (string= (buffer-name) org-agenda-buffer-name)
2328 (error "Can only append from within agenda buffer"))
2329 (let ((org-agenda-multi t))
2330 (org-agenda)
2331 (widen)))
2333 (defun org-agenda-normalize-custom-commands (cmds)
2334 (delq nil
2335 (mapcar
2336 (lambda (x)
2337 (cond ((stringp (cdr x)) nil)
2338 ((stringp (nth 1 x)) x)
2339 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2340 (t (cons (car x) (cons "" (cdr x))))))
2341 cmds)))
2343 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2344 "The user interface for selecting an agenda command."
2345 (catch 'exit
2346 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2347 (restrict-ok (and bfn (org-mode-p)))
2348 (region-p (org-region-active-p))
2349 (custom org-agenda-custom-commands)
2350 (selstring "")
2351 restriction second-time
2352 c entry key type match prefixes rmheader header-end custom1 desc
2353 line lines left right n n1)
2354 (save-window-excursion
2355 (delete-other-windows)
2356 (org-switch-to-buffer-other-window " *Agenda Commands*")
2357 (erase-buffer)
2358 (insert (eval-when-compile
2359 (let ((header
2361 Press key for an agenda command: < Buffer, subtree/region restriction
2362 -------------------------------- > Remove restriction
2363 a Agenda for current week or day e Export agenda views
2364 t List of all TODO entries T Entries with special TODO kwd
2365 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2366 L Timeline for current buffer # List stuck projects (!=configure)
2367 s Search for keywords C Configure custom agenda commands
2368 / Multi-occur ? Find :FLAGGED: entries
2370 (start 0))
2371 (while (string-match
2372 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2373 header start)
2374 (setq start (match-end 0))
2375 (add-text-properties (match-beginning 2) (match-end 2)
2376 '(face bold) header))
2377 header)))
2378 (setq header-end (move-marker (make-marker) (point)))
2379 (while t
2380 (setq custom1 custom)
2381 (when (eq rmheader t)
2382 (org-goto-line 1)
2383 (re-search-forward ":" nil t)
2384 (delete-region (match-end 0) (point-at-eol))
2385 (forward-char 1)
2386 (looking-at "-+")
2387 (delete-region (match-end 0) (point-at-eol))
2388 (move-marker header-end (match-end 0)))
2389 (goto-char header-end)
2390 (delete-region (point) (point-max))
2392 ;; Produce all the lines that describe custom commands and prefixes
2393 (setq lines nil)
2394 (while (setq entry (pop custom1))
2395 (setq key (car entry) desc (nth 1 entry)
2396 type (nth 2 entry)
2397 match (nth 3 entry))
2398 (if (> (length key) 1)
2399 (add-to-list 'prefixes (string-to-char key))
2400 (setq line
2401 (format
2402 "%-4s%-14s"
2403 (org-add-props (copy-sequence key)
2404 '(face bold))
2405 (cond
2406 ((string-match "\\S-" desc) desc)
2407 ((eq type 'agenda) "Agenda for current week or day")
2408 ((eq type 'alltodo) "List of all TODO entries")
2409 ((eq type 'search) "Word search")
2410 ((eq type 'stuck) "List of stuck projects")
2411 ((eq type 'todo) "TODO keyword")
2412 ((eq type 'tags) "Tags query")
2413 ((eq type 'tags-todo) "Tags (TODO)")
2414 ((eq type 'tags-tree) "Tags tree")
2415 ((eq type 'todo-tree) "TODO kwd tree")
2416 ((eq type 'occur-tree) "Occur tree")
2417 ((functionp type) (if (symbolp type)
2418 (symbol-name type)
2419 "Lambda expression"))
2420 (t "???"))))
2421 (if org-agenda-menu-show-matcher
2422 (setq line
2423 (concat line ": "
2424 (cond
2425 ((stringp match)
2426 (setq match (copy-sequence match))
2427 (org-add-props match nil 'face 'org-warning))
2428 (match
2429 (format "set of %d commands" (length match)))
2430 (t ""))))
2431 (if (org-string-nw-p match)
2432 (add-text-properties
2433 0 (length line) (list 'help-echo
2434 (concat "Matcher: "match)) line)))
2435 (push line lines)))
2436 (setq lines (nreverse lines))
2437 (when prefixes
2438 (mapc (lambda (x)
2439 (push
2440 (format "%s %s"
2441 (org-add-props (char-to-string x)
2442 nil 'face 'bold)
2443 (or (cdr (assoc (concat selstring
2444 (char-to-string x))
2445 prefix-descriptions))
2446 "Prefix key"))
2447 lines))
2448 prefixes))
2450 ;; Check if we should display in two columns
2451 (if org-agenda-menu-two-column
2452 (progn
2453 (setq n (length lines)
2454 n1 (+ (/ n 2) (mod n 2))
2455 right (nthcdr n1 lines)
2456 left (copy-sequence lines))
2457 (setcdr (nthcdr (1- n1) left) nil))
2458 (setq left lines right nil))
2459 (while left
2460 (insert "\n" (pop left))
2461 (when right
2462 (if (< (current-column) 40)
2463 (move-to-column 40 t)
2464 (insert " "))
2465 (insert (pop right))))
2467 ;; Make the window the right size
2468 (goto-char (point-min))
2469 (if second-time
2470 (if (not (pos-visible-in-window-p (point-max)))
2471 (org-fit-window-to-buffer))
2472 (setq second-time t)
2473 (org-fit-window-to-buffer))
2475 ;; Ask for selection
2476 (message "Press key for agenda command%s:"
2477 (if (or restrict-ok org-agenda-overriding-restriction)
2478 (if org-agenda-overriding-restriction
2479 " (restriction lock active)"
2480 (if restriction
2481 (format " (restricted to %s)" restriction)
2482 " (unrestricted)"))
2483 ""))
2484 (setq c (read-char-exclusive))
2485 (message "")
2486 (cond
2487 ((assoc (char-to-string c) custom)
2488 (setq selstring (concat selstring (char-to-string c)))
2489 (throw 'exit (cons selstring restriction)))
2490 ((memq c prefixes)
2491 (setq selstring (concat selstring (char-to-string c))
2492 prefixes nil
2493 rmheader (or rmheader t)
2494 custom (delq nil (mapcar
2495 (lambda (x)
2496 (if (or (= (length (car x)) 1)
2497 (/= (string-to-char (car x)) c))
2499 (cons (substring (car x) 1) (cdr x))))
2500 custom))))
2501 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2502 (message "Restriction is only possible in Org-mode buffers")
2503 (ding) (sit-for 1))
2504 ((eq c ?1)
2505 (org-agenda-remove-restriction-lock 'noupdate)
2506 (setq restriction 'buffer))
2507 ((eq c ?0)
2508 (org-agenda-remove-restriction-lock 'noupdate)
2509 (setq restriction (if region-p 'region 'subtree)))
2510 ((eq c ?<)
2511 (org-agenda-remove-restriction-lock 'noupdate)
2512 (setq restriction
2513 (cond
2514 ((eq restriction 'buffer)
2515 (if region-p 'region 'subtree))
2516 ((memq restriction '(subtree region))
2517 nil)
2518 (t 'buffer))))
2519 ((eq c ?>)
2520 (org-agenda-remove-restriction-lock 'noupdate)
2521 (setq restriction nil))
2522 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2523 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2524 ((and (> (length selstring) 0) (eq c ?\d))
2525 (delete-window)
2526 (org-agenda-get-restriction-and-command prefix-descriptions))
2528 ((equal c ?q) (error "Abort"))
2529 (t (error "Invalid key %c" c))))))))
2531 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2532 (defvar org-agenda-last-arguments nil
2533 "The arguments of the previous call to `org-agenda'.")
2534 (defun org-agenda-run-series (name series)
2535 (org-let (nth 1 series) '(org-prepare-agenda name))
2536 (let* ((org-agenda-multi t)
2537 (redo (list 'org-agenda-run-series name (list 'quote series)))
2538 (org-agenda-overriding-arguments
2539 (or org-agenda-overriding-arguments
2540 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2541 (get 'org-agenda-redo-command 'last-args))))
2542 (cmds (car series))
2543 (gprops (nth 1 series))
2544 match ;; The byte compiler incorrectly complains about this. Keep it!
2545 cmd type lprops)
2546 (while (setq cmd (pop cmds))
2547 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2548 (cond
2549 ((eq type 'agenda)
2550 (org-let2 gprops lprops
2551 '(call-interactively 'org-agenda-list)))
2552 ((eq type 'alltodo)
2553 (org-let2 gprops lprops
2554 '(call-interactively 'org-todo-list)))
2555 ((eq type 'search)
2556 (org-let2 gprops lprops
2557 '(org-search-view current-prefix-arg match nil)))
2558 ((eq type 'stuck)
2559 (org-let2 gprops lprops
2560 '(call-interactively 'org-agenda-list-stuck-projects)))
2561 ((eq type 'tags)
2562 (org-let2 gprops lprops
2563 '(org-tags-view current-prefix-arg match)))
2564 ((eq type 'tags-todo)
2565 (org-let2 gprops lprops
2566 '(org-tags-view '(4) match)))
2567 ((eq type 'todo)
2568 (org-let2 gprops lprops
2569 '(org-todo-list match)))
2570 ((fboundp type)
2571 (org-let2 gprops lprops
2572 '(funcall type match)))
2573 (t (error "Invalid type in command series"))))
2574 (widen)
2575 (setq org-agenda-redo-command redo)
2576 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2577 (goto-char (point-min)))
2578 (org-fit-agenda-window)
2579 (org-let (nth 1 series) '(org-finalize-agenda)))
2581 ;;;###autoload
2582 (defmacro org-batch-agenda (cmd-key &rest parameters)
2583 "Run an agenda command in batch mode and send the result to STDOUT.
2584 If CMD-KEY is a string of length 1, it is used as a key in
2585 `org-agenda-custom-commands' and triggers this command. If it is a
2586 longer string it is used as a tags/todo match string.
2587 Parameters are alternating variable names and values that will be bound
2588 before running the agenda command."
2589 (org-eval-in-environment (org-make-parameter-alist parameters)
2590 (if (> (length cmd-key) 2)
2591 (org-tags-view nil cmd-key)
2592 (org-agenda nil cmd-key)))
2593 (set-buffer org-agenda-buffer-name)
2594 (princ (org-encode-for-stdout (buffer-string))))
2595 (def-edebug-spec org-batch-agenda (form &rest sexp))
2597 ;(defun org-encode-for-stdout (string)
2598 ; (if (fboundp 'encode-coding-string)
2599 ; (encode-coding-string string buffer-file-coding-system)
2600 ; string))
2602 (defun org-encode-for-stdout (string)
2603 string)
2605 (defvar org-agenda-info nil)
2607 ;;;###autoload
2608 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2609 "Run an agenda command in batch mode and send the result to STDOUT.
2610 If CMD-KEY is a string of length 1, it is used as a key in
2611 `org-agenda-custom-commands' and triggers this command. If it is a
2612 longer string it is used as a tags/todo match string.
2613 Parameters are alternating variable names and values that will be bound
2614 before running the agenda command.
2616 The output gives a line for each selected agenda item. Each
2617 item is a list of comma-separated values, like this:
2619 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2621 category The category of the item
2622 head The headline, without TODO kwd, TAGS and PRIORITY
2623 type The type of the agenda entry, can be
2624 todo selected in TODO match
2625 tagsmatch selected in tags match
2626 diary imported from diary
2627 deadline a deadline on given date
2628 scheduled scheduled on given date
2629 timestamp entry has timestamp on given date
2630 closed entry was closed on given date
2631 upcoming-deadline warning about deadline
2632 past-scheduled forwarded scheduled item
2633 block entry has date block including g. date
2634 todo The todo keyword, if any
2635 tags All tags including inherited ones, separated by colons
2636 date The relevant date, like 2007-2-14
2637 time The time, like 15:00-16:50
2638 extra Sting with extra planning info
2639 priority-l The priority letter if any was given
2640 priority-n The computed numerical priority
2641 agenda-day The day in the agenda where this is listed"
2642 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2643 (org-make-parameter-alist parameters))
2644 (if (> (length cmd-key) 2)
2645 (org-tags-view nil cmd-key)
2646 (org-agenda nil cmd-key)))
2647 (set-buffer org-agenda-buffer-name)
2648 (let* ((lines (org-split-string (buffer-string) "\n"))
2649 line)
2650 (while (setq line (pop lines))
2651 (catch 'next
2652 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2653 (setq org-agenda-info
2654 (org-fix-agenda-info (text-properties-at 0 line)))
2655 (princ
2656 (org-encode-for-stdout
2657 (mapconcat 'org-agenda-export-csv-mapper
2658 '(org-category txt type todo tags date time extra
2659 priority-letter priority agenda-day)
2660 ",")))
2661 (princ "\n")))))
2662 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2664 (defun org-fix-agenda-info (props)
2665 "Make sure all properties on an agenda item have a canonical form.
2666 This ensures the export commands can easily use it."
2667 (let (tmp re)
2668 (when (setq tmp (plist-get props 'tags))
2669 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2670 (when (setq tmp (plist-get props 'date))
2671 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2672 (let ((calendar-date-display-form '(year "-" month "-" day)))
2673 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2675 (setq tmp (calendar-date-string tmp)))
2676 (setq props (plist-put props 'date tmp)))
2677 (when (setq tmp (plist-get props 'day))
2678 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2679 (let ((calendar-date-display-form '(year "-" month "-" day)))
2680 (setq tmp (calendar-date-string tmp)))
2681 (setq props (plist-put props 'day tmp))
2682 (setq props (plist-put props 'agenda-day tmp)))
2683 (when (setq tmp (plist-get props 'txt))
2684 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2685 (plist-put props 'priority-letter (match-string 1 tmp))
2686 (setq tmp (replace-match "" t t tmp)))
2687 (when (and (setq re (plist-get props 'org-todo-regexp))
2688 (setq re (concat "\\`\\.*" re " ?"))
2689 (string-match re tmp))
2690 (plist-put props 'todo (match-string 1 tmp))
2691 (setq tmp (replace-match "" t t tmp)))
2692 (plist-put props 'txt tmp)))
2693 props)
2695 (defun org-agenda-export-csv-mapper (prop)
2696 (let ((res (plist-get org-agenda-info prop)))
2697 (setq res
2698 (cond
2699 ((not res) "")
2700 ((stringp res) res)
2701 (t (prin1-to-string res))))
2702 (while (string-match "," res)
2703 (setq res (replace-match ";" t t res)))
2704 (org-trim res)))
2707 ;;;###autoload
2708 (defun org-store-agenda-views (&rest parameters)
2709 (interactive)
2710 (eval (list 'org-batch-store-agenda-views)))
2712 ;;;###autoload
2713 (defmacro org-batch-store-agenda-views (&rest parameters)
2714 "Run all custom agenda commands that have a file argument."
2715 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2716 (pop-up-frames nil)
2717 (dir default-directory)
2718 (pars (org-make-parameter-alist parameters))
2719 cmd thiscmdkey files opts cmd-or-set)
2720 (save-window-excursion
2721 (while cmds
2722 (setq cmd (pop cmds)
2723 thiscmdkey (car cmd)
2724 cmd-or-set (nth 2 cmd)
2725 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2726 files (nth (if (listp cmd-or-set) 4 5) cmd))
2727 (if (stringp files) (setq files (list files)))
2728 (when files
2729 (org-eval-in-environment (append org-agenda-exporter-settings
2730 opts pars)
2731 (org-agenda nil thiscmdkey))
2732 (set-buffer org-agenda-buffer-name)
2733 (while files
2734 (org-eval-in-environment (append org-agenda-exporter-settings
2735 opts pars)
2736 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2737 (and (get-buffer org-agenda-buffer-name)
2738 (kill-buffer org-agenda-buffer-name)))))))
2739 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2741 (defun org-agenda-mark-header-line (pos)
2742 "Mark the line at POS as an agenda structure header."
2743 (save-excursion
2744 (goto-char pos)
2745 (put-text-property (point-at-bol) (point-at-eol)
2746 'org-agenda-structural-header t)
2747 (when org-agenda-title-append
2748 (put-text-property (point-at-bol) (point-at-eol)
2749 'org-agenda-title-append org-agenda-title-append))))
2751 (defvar org-mobile-creating-agendas)
2752 (defun org-write-agenda (file &optional open nosettings)
2753 "Write the current buffer (an agenda view) as a file.
2754 Depending on the extension of the file name, plain text (.txt),
2755 HTML (.html or .htm) or Postscript (.ps) is produced.
2756 If the extension is .ics, run icalendar export over all files used
2757 to construct the agenda and limit the export to entries listed in the
2758 agenda now.
2759 With prefix argument OPEN, open the new file immediately.
2760 If NOSETTINGS is given, do not scope the settings of
2761 `org-agenda-exporter-settings' into the export commands. This is used when
2762 the settings have already been scoped and we do not wish to overrule other,
2763 higher priority settings."
2764 (interactive "FWrite agenda to file: \nP")
2765 (if (not (file-writable-p file))
2766 (error "Cannot write agenda to file %s" file))
2767 (org-let (if nosettings nil org-agenda-exporter-settings)
2768 '(save-excursion
2769 (save-window-excursion
2770 (org-agenda-mark-filtered-text)
2771 (let ((bs (copy-sequence (buffer-string))) beg)
2772 (org-agenda-unmark-filtered-text)
2773 (with-temp-buffer
2774 (rename-buffer "Agenda View" t)
2775 (set-buffer-modified-p nil)
2776 (insert bs)
2777 (org-agenda-remove-marked-text 'org-filtered)
2778 (while (setq beg (text-property-any (point-min) (point-max)
2779 'org-filtered t))
2780 (delete-region
2781 beg (or (next-single-property-change beg 'org-filtered)
2782 (point-max))))
2783 (run-hooks 'org-agenda-before-write-hook)
2784 (cond
2785 ((org-bound-and-true-p org-mobile-creating-agendas)
2786 (org-mobile-write-agenda-for-mobile file))
2787 ((string-match "\\.html?\\'" file)
2788 (require 'htmlize)
2789 (set-buffer (htmlize-buffer (current-buffer)))
2791 (when (and org-agenda-export-html-style
2792 (string-match "<style>" org-agenda-export-html-style))
2793 ;; replace <style> section with org-agenda-export-html-style
2794 (goto-char (point-min))
2795 (kill-region (- (search-forward "<style") 6)
2796 (search-forward "</style>"))
2797 (insert org-agenda-export-html-style))
2798 (write-file file)
2799 (kill-buffer (current-buffer))
2800 (message "HTML written to %s" file))
2801 ((string-match "\\.ps\\'" file)
2802 (require 'ps-print)
2803 (ps-print-buffer-with-faces file)
2804 (message "Postscript written to %s" file))
2805 ((string-match "\\.pdf\\'" file)
2806 (require 'ps-print)
2807 (ps-print-buffer-with-faces
2808 (concat (file-name-sans-extension file) ".ps"))
2809 (call-process "ps2pdf" nil nil nil
2810 (expand-file-name
2811 (concat (file-name-sans-extension file) ".ps"))
2812 (expand-file-name file))
2813 (delete-file (concat (file-name-sans-extension file) ".ps"))
2814 (message "PDF written to %s" file))
2815 ((string-match "\\.ics\\'" file)
2816 (require 'org-icalendar)
2817 (let ((org-agenda-marker-table
2818 (org-create-marker-find-array
2819 (org-agenda-collect-markers)))
2820 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2821 (org-combined-agenda-icalendar-file file))
2822 (apply 'org-export-icalendar 'combine
2823 (org-agenda-files nil 'ifmode))))
2825 (let ((bs (buffer-string)))
2826 (find-file file)
2827 (erase-buffer)
2828 (insert bs)
2829 (save-buffer 0)
2830 (kill-buffer (current-buffer))
2831 (message "Plain text written to %s" file))))))))
2832 (set-buffer org-agenda-buffer-name))
2833 (when open (org-open-file file)))
2835 (defvar org-agenda-filter-overlays nil)
2837 (defun org-agenda-mark-filtered-text ()
2838 "Mark all text hidden by filtering with a text property."
2839 (let ((inhibit-read-only t))
2840 (mapc
2841 (lambda (o)
2842 (when (equal (overlay-buffer o) (current-buffer))
2843 (put-text-property
2844 (overlay-start o) (overlay-end o)
2845 'org-filtered t)))
2846 org-agenda-filter-overlays)))
2848 (defun org-agenda-unmark-filtered-text ()
2849 "Remove the filtering text property."
2850 (let ((inhibit-read-only t))
2851 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2853 (defun org-agenda-remove-marked-text (property &optional value)
2854 "Delete all text marked with VALUE of PROPERTY.
2855 VALUE defaults to t."
2856 (let (beg)
2857 (setq value (or value t))
2858 (while (setq beg (text-property-any (point-min) (point-max)
2859 property value))
2860 (delete-region
2861 beg (or (next-single-property-change beg 'org-filtered)
2862 (point-max))))))
2864 (defun org-agenda-add-entry-text ()
2865 "Add entry text to agenda lines.
2866 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2867 entry text following headings shown in the agenda.
2868 Drawers will be excluded, also the line with scheduling/deadline info."
2869 (when (and (> org-agenda-add-entry-text-maxlines 0)
2870 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2871 (let (m txt)
2872 (goto-char (point-min))
2873 (while (not (eobp))
2874 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2875 (beginning-of-line 2)
2876 (setq txt (org-agenda-get-some-entry-text
2877 m org-agenda-add-entry-text-maxlines " > "))
2878 (end-of-line 1)
2879 (if (string-match "\\S-" txt)
2880 (insert "\n" txt)
2881 (or (eobp) (forward-char 1))))))))
2883 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2884 &rest keep)
2885 "Extract entry text from MARKER, at most N-LINES lines.
2886 This will ignore drawers etc, just get the text.
2887 If INDENT is given, prefix every line with this string. If KEEP is
2888 given, it is a list of symbols, defining stuff that should not be
2889 removed from the entry content. Currently only `planning' is allowed here."
2890 (let (txt drawer-re kwd-time-re ind)
2891 (save-excursion
2892 (with-current-buffer (marker-buffer marker)
2893 (if (not (org-mode-p))
2894 (setq txt "")
2895 (save-excursion
2896 (save-restriction
2897 (widen)
2898 (goto-char marker)
2899 (end-of-line 1)
2900 (setq txt (buffer-substring
2901 (min (1+ (point)) (point-max))
2902 (progn (outline-next-heading) (point)))
2903 drawer-re org-drawer-regexp
2904 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2905 ".*\n?"))
2906 (with-temp-buffer
2907 (insert txt)
2908 (when org-agenda-add-entry-text-descriptive-links
2909 (goto-char (point-min))
2910 (while (org-activate-bracket-links (point-max))
2911 (add-text-properties (match-beginning 0) (match-end 0)
2912 '(face org-link))))
2913 (goto-char (point-min))
2914 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2915 (set-text-properties (match-beginning 0) (match-end 0)
2916 nil))
2917 (goto-char (point-min))
2918 (while (re-search-forward drawer-re nil t)
2919 (delete-region
2920 (match-beginning 0)
2921 (progn (re-search-forward
2922 "^[ \t]*:END:.*\n?" nil 'move)
2923 (point))))
2924 (unless (member 'planning keep)
2925 (goto-char (point-min))
2926 (while (re-search-forward kwd-time-re nil t)
2927 (replace-match "")))
2928 (goto-char (point-min))
2929 (when org-agenda-entry-text-exclude-regexps
2930 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2931 (while (setq re (pop re-list))
2932 (goto-char (point-min))
2933 (while (re-search-forward re nil t)
2934 (replace-match "")))))
2935 (goto-char (point-max))
2936 (skip-chars-backward " \t\n")
2937 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2939 ;; find and remove min common indentation
2940 (goto-char (point-min))
2941 (untabify (point-min) (point-max))
2942 (setq ind (org-get-indentation))
2943 (while (not (eobp))
2944 (unless (looking-at "[ \t]*$")
2945 (setq ind (min ind (org-get-indentation))))
2946 (beginning-of-line 2))
2947 (goto-char (point-min))
2948 (while (not (eobp))
2949 (unless (looking-at "[ \t]*$")
2950 (move-to-column ind)
2951 (delete-region (point-at-bol) (point)))
2952 (beginning-of-line 2))
2954 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2956 (goto-char (point-min))
2957 (when indent
2958 (while (and (not (eobp)) (re-search-forward "^" nil t))
2959 (replace-match indent t t)))
2960 (goto-char (point-min))
2961 (while (looking-at "[ \t]*\n") (replace-match ""))
2962 (goto-char (point-max))
2963 (when (> (org-current-line)
2964 n-lines)
2965 (org-goto-line (1+ n-lines))
2966 (backward-char 1))
2967 (setq txt (buffer-substring (point-min) (point)))))))))
2968 txt))
2970 (defun org-agenda-collect-markers ()
2971 "Collect the markers pointing to entries in the agenda buffer."
2972 (let (m markers)
2973 (save-excursion
2974 (goto-char (point-min))
2975 (while (not (eobp))
2976 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2977 (org-get-at-bol 'org-marker)))
2978 (push m markers))
2979 (beginning-of-line 2)))
2980 (nreverse markers)))
2982 (defun org-create-marker-find-array (marker-list)
2983 "Create a alist of files names with all marker positions in that file."
2984 (let (f tbl m a p)
2985 (while (setq m (pop marker-list))
2986 (setq p (marker-position m)
2987 f (buffer-file-name (or (buffer-base-buffer
2988 (marker-buffer m))
2989 (marker-buffer m))))
2990 (if (setq a (assoc f tbl))
2991 (push (marker-position m) (cdr a))
2992 (push (list f p) tbl)))
2993 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2994 tbl)))
2996 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2997 (defun org-check-agenda-marker-table ()
2998 "Check of the current entry is on the marker list."
2999 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3001 (and (setq a (assoc file org-agenda-marker-table))
3002 (save-match-data
3003 (save-excursion
3004 (org-back-to-heading t)
3005 (member (point) (cdr a)))))))
3007 (defun org-check-for-org-mode ()
3008 "Make sure current buffer is in org-mode. Error if not."
3009 (or (org-mode-p)
3010 (error "Cannot execute org-mode agenda command on buffer in %s"
3011 major-mode)))
3013 (defun org-fit-agenda-window ()
3014 "Fit the window to the buffer size."
3015 (and (memq org-agenda-window-setup '(reorganize-frame))
3016 (fboundp 'fit-window-to-buffer)
3017 (org-fit-window-to-buffer
3019 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3020 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3022 ;;; Agenda prepare and finalize
3024 (defvar org-agenda-multi nil) ; dynamically scoped
3025 (defvar org-agenda-buffer-name "*Org Agenda*")
3026 (defvar org-pre-agenda-window-conf nil)
3027 (defvar org-agenda-columns-active nil)
3028 (defvar org-agenda-name nil)
3029 (defvar org-agenda-filter nil)
3030 (defvar org-agenda-filter-while-redo nil)
3031 (defvar org-agenda-filter-preset nil
3032 "A preset of the tags filter used for secondary agenda filtering.
3033 This must be a list of strings, each string must be a single tag preceded
3034 by \"+\" or \"-\".
3035 This variable should not be set directly, but agenda custom commands can
3036 bind it in the options section. The preset filter is a global property of
3037 the entire agenda view. In a block agenda, it will not work reliably to
3038 define a filter for one of the individual blocks. You need to set it in
3039 the global options and expect it to be applied to the entire view.")
3041 (defun org-prepare-agenda (&optional name)
3042 (setq org-todo-keywords-for-agenda nil)
3043 (setq org-done-keywords-for-agenda nil)
3044 (setq org-drawers-for-agenda nil)
3045 (unless org-agenda-persistent-filter
3046 (setq org-agenda-filter nil))
3047 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3048 (if org-agenda-multi
3049 (progn
3050 (setq buffer-read-only nil)
3051 (goto-char (point-max))
3052 (unless (or (bobp) org-agenda-compact-blocks
3053 (not org-agenda-block-separator))
3054 (insert "\n"
3055 (if (stringp org-agenda-block-separator)
3056 org-agenda-block-separator
3057 (make-string (window-width) org-agenda-block-separator))
3058 "\n"))
3059 (narrow-to-region (point) (point-max)))
3060 (org-agenda-reset-markers)
3061 (setq org-agenda-contributing-files nil)
3062 (setq org-agenda-columns-active nil)
3063 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3064 (setq org-todo-keywords-for-agenda
3065 (org-uniquify org-todo-keywords-for-agenda))
3066 (setq org-done-keywords-for-agenda
3067 (org-uniquify org-done-keywords-for-agenda))
3068 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3069 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3070 (awin (get-buffer-window abuf)))
3071 (cond
3072 ((equal (current-buffer) abuf) nil)
3073 (awin (select-window awin))
3074 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3075 ((equal org-agenda-window-setup 'current-window)
3076 (org-pop-to-buffer-same-window abuf))
3077 ((equal org-agenda-window-setup 'other-window)
3078 (org-switch-to-buffer-other-window abuf))
3079 ((equal org-agenda-window-setup 'other-frame)
3080 (switch-to-buffer-other-frame abuf))
3081 ((equal org-agenda-window-setup 'reorganize-frame)
3082 (delete-other-windows)
3083 (org-switch-to-buffer-other-window abuf)))
3084 ;; additional test in case agenda is invoked from within agenda
3085 ;; buffer via elisp link
3086 (unless (equal (current-buffer) abuf)
3087 (org-pop-to-buffer-same-window abuf)))
3088 (setq buffer-read-only nil)
3089 (let ((inhibit-read-only t)) (erase-buffer))
3090 (org-agenda-mode)
3091 (and name (not org-agenda-name)
3092 (org-set-local 'org-agenda-name name)))
3093 (setq buffer-read-only nil))
3095 (defun org-finalize-agenda ()
3096 "Finishing touch for the agenda buffer, called just before displaying it."
3097 (unless org-agenda-multi
3098 (save-excursion
3099 (let ((inhibit-read-only t))
3100 (goto-char (point-min))
3101 (while (org-activate-bracket-links (point-max))
3102 (add-text-properties (match-beginning 0) (match-end 0)
3103 '(face org-link)))
3104 (org-agenda-align-tags)
3105 (unless org-agenda-with-colors
3106 (remove-text-properties (point-min) (point-max) '(face nil))))
3107 (if (and (boundp 'org-agenda-overriding-columns-format)
3108 org-agenda-overriding-columns-format)
3109 (org-set-local 'org-agenda-overriding-columns-format
3110 org-agenda-overriding-columns-format))
3111 (if (and (boundp 'org-agenda-view-columns-initially)
3112 org-agenda-view-columns-initially)
3113 (org-agenda-columns))
3114 (when org-agenda-fontify-priorities
3115 (org-agenda-fontify-priorities))
3116 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3117 (org-agenda-dim-blocked-tasks))
3118 (org-agenda-mark-clocking-task)
3119 (when org-agenda-entry-text-mode
3120 (org-agenda-entry-text-hide)
3121 (org-agenda-entry-text-show))
3122 (if (functionp 'org-habit-insert-consistency-graphs)
3123 (org-habit-insert-consistency-graphs))
3124 (run-hooks 'org-finalize-agenda-hook)
3125 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3126 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3127 (org-agenda-filter-apply org-agenda-filter))
3130 (defun org-agenda-mark-clocking-task ()
3131 "Mark the current clock entry in the agenda if it is present."
3132 (mapc (lambda (o)
3133 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3134 (delete-overlay o)))
3135 (overlays-in (point-min) (point-max)))
3136 (when (marker-buffer org-clock-hd-marker)
3137 (save-excursion
3138 (goto-char (point-min))
3139 (let (s ov)
3140 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3141 (goto-char s)
3142 (when (equal (org-get-at-bol 'org-hd-marker)
3143 org-clock-hd-marker)
3144 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3145 (overlay-put ov 'type 'org-agenda-clocking)
3146 (overlay-put ov 'face 'org-agenda-clocking)
3147 (overlay-put ov 'help-echo
3148 "The clock is running in this item")))))))
3150 (defun org-agenda-fontify-priorities ()
3151 "Make highest priority lines bold, and lowest italic."
3152 (interactive)
3153 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3154 (delete-overlay o)))
3155 (overlays-in (point-min) (point-max)))
3156 (save-excursion
3157 (let ((inhibit-read-only t)
3158 b e p ov h l)
3159 (goto-char (point-min))
3160 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3161 (setq h (or (get-char-property (point) 'org-highest-priority)
3162 org-highest-priority)
3163 l (or (get-char-property (point) 'org-lowest-priority)
3164 org-lowest-priority)
3165 p (string-to-char (match-string 1))
3166 b (match-beginning 0)
3167 e (if (eq org-agenda-fontify-priorities 'cookies)
3168 (match-end 0)
3169 (point-at-eol))
3170 ov (make-overlay b e))
3171 (overlay-put
3172 ov 'face
3173 (cond ((org-face-from-face-or-color
3174 'priority nil
3175 (cdr (assoc p org-priority-faces))))
3176 ((and (listp org-agenda-fontify-priorities)
3177 (org-face-from-face-or-color
3178 'priority nil
3179 (cdr (assoc p org-agenda-fontify-priorities)))))
3180 ((equal p l) 'italic)
3181 ((equal p h) 'bold)))
3182 (overlay-put ov 'org-type 'org-priority)))))
3184 (defun org-agenda-dim-blocked-tasks ()
3185 "Dim currently blocked TODO's in the agenda display."
3186 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3187 (delete-overlay o)))
3188 (overlays-in (point-min) (point-max)))
3189 (save-excursion
3190 (let ((inhibit-read-only t)
3191 (org-depend-tag-blocked nil)
3192 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3193 org-blocked-by-checkboxes
3194 invis1 b e p ov h l)
3195 (goto-char (point-min))
3196 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3197 (and pos (goto-char (1+ pos))))
3198 (setq org-blocked-by-checkboxes nil invis1 invis)
3199 (let ((marker (org-get-at-bol 'org-hd-marker)))
3200 (when (and marker
3201 (not (with-current-buffer (marker-buffer marker)
3202 (save-excursion
3203 (goto-char marker)
3204 (if (org-entry-get nil "NOBLOCKING")
3205 t ;; Never block this entry
3206 (run-hook-with-args-until-failure
3207 'org-blocker-hook
3208 (list :type 'todo-state-change
3209 :position marker
3210 :from 'todo
3211 :to 'done)))))))
3212 (if org-blocked-by-checkboxes (setq invis1 nil))
3213 (setq b (if invis1
3214 (max (point-min) (1- (point-at-bol)))
3215 (point-at-bol))
3216 e (point-at-eol)
3217 ov (make-overlay b e))
3218 (if invis1
3219 (overlay-put ov 'invisible t)
3220 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3221 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3223 (defvar org-agenda-skip-function nil
3224 "Function to be called at each match during agenda construction.
3225 If this function returns nil, the current match should not be skipped.
3226 Otherwise, the function must return a position from where the search
3227 should be continued.
3228 This may also be a Lisp form, it will be evaluated.
3229 Never set this variable using `setq' or so, because then it will apply
3230 to all future agenda commands. If you do want a global skipping condition,
3231 use the option `org-agenda-skip-function-global' instead.
3232 The correct usage for `org-agenda-skip-function' is to bind it with
3233 `let' to scope it dynamically into the agenda-constructing command.
3234 A good way to set it is through options in `org-agenda-custom-commands'.")
3236 (defun org-agenda-skip ()
3237 "Throw to `:skip' in places that should be skipped.
3238 Also moves point to the end of the skipped region, so that search can
3239 continue from there."
3240 (let ((p (point-at-bol)) to)
3241 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3242 (get-text-property p :org-archived)
3243 (org-end-of-subtree t)
3244 (throw :skip t))
3245 (and org-agenda-skip-comment-trees
3246 (get-text-property p :org-comment)
3247 (org-end-of-subtree t)
3248 (throw :skip t))
3249 (if (equal (char-after p) ?#) (throw :skip t))
3250 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3251 (org-agenda-skip-eval org-agenda-skip-function)))
3252 (goto-char to)
3253 (throw :skip t))))
3255 (defun org-agenda-skip-eval (form)
3256 "If FORM is a function or a list, call (or eval) is and return result.
3257 `save-excursion' and `save-match-data' are wrapped around the call, so point
3258 and match data are returned to the previous state no matter what these
3259 functions do."
3260 (let (fp)
3261 (and form
3262 (or (setq fp (functionp form))
3263 (consp form))
3264 (save-excursion
3265 (save-match-data
3266 (if fp
3267 (funcall form)
3268 (eval form)))))))
3270 (defvar org-agenda-markers nil
3271 "List of all currently active markers created by `org-agenda'.")
3272 (defvar org-agenda-last-marker-time (org-float-time)
3273 "Creation time of the last agenda marker.")
3275 (defun org-agenda-new-marker (&optional pos)
3276 "Return a new agenda marker.
3277 Org-mode keeps a list of these markers and resets them when they are
3278 no longer in use."
3279 (let ((m (copy-marker (or pos (point)))))
3280 (setq org-agenda-last-marker-time (org-float-time))
3281 (push m org-agenda-markers)
3284 (defun org-agenda-reset-markers ()
3285 "Reset markers created by `org-agenda'."
3286 (while org-agenda-markers
3287 (move-marker (pop org-agenda-markers) nil)))
3289 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3290 "Save relative positions of markers in region."
3291 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3292 org-agenda-markers))
3294 ;;; Entry text mode
3296 (defun org-agenda-entry-text-show-here ()
3297 "Add some text from the entry as context to the current line."
3298 (let (m txt o)
3299 (setq m (org-get-at-bol 'org-hd-marker))
3300 (unless (marker-buffer m)
3301 (error "No marker points to an entry here"))
3302 (setq txt (concat "\n" (org-no-properties
3303 (org-agenda-get-some-entry-text
3304 m org-agenda-entry-text-maxlines " > "))))
3305 (when (string-match "\\S-" txt)
3306 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3307 (overlay-put o 'evaporate t)
3308 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3309 (overlay-put o 'after-string txt))))
3311 (defun org-agenda-entry-text-show ()
3312 "Add entry context for all agenda lines."
3313 (interactive)
3314 (save-excursion
3315 (goto-char (point-max))
3316 (beginning-of-line 1)
3317 (while (not (bobp))
3318 (when (org-get-at-bol 'org-hd-marker)
3319 (org-agenda-entry-text-show-here))
3320 (beginning-of-line 0))))
3322 (defun org-agenda-entry-text-hide ()
3323 "Remove any shown entry context."
3324 (delq nil
3325 (mapcar (lambda (o)
3326 (if (eq (overlay-get o 'org-overlay-type)
3327 'agenda-entry-content)
3328 (progn (delete-overlay o) t)))
3329 (overlays-in (point-min) (point-max)))))
3331 (defun org-agenda-get-day-face (date)
3332 "Return the face DATE should be displayed with."
3333 (or (and (functionp org-agenda-day-face-function)
3334 (funcall org-agenda-day-face-function date))
3335 (cond ((org-agenda-todayp date)
3336 'org-agenda-date-today)
3337 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3338 'org-agenda-date-weekend)
3339 (t 'org-agenda-date))))
3341 ;;; Agenda timeline
3343 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3345 (defun org-timeline (&optional dotodo)
3346 "Show a time-sorted view of the entries in the current org file.
3347 Only entries with a time stamp of today or later will be listed. With
3348 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3349 under the current date.
3350 If the buffer contains an active region, only check the region for
3351 dates."
3352 (interactive "P")
3353 (org-compile-prefix-format 'timeline)
3354 (org-set-sorting-strategy 'timeline)
3355 (let* ((dopast t)
3356 (doclosed org-agenda-show-log)
3357 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3358 (current-buffer))))
3359 (date (calendar-current-date))
3360 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3361 (end (if (org-region-active-p) (region-end) (point-max)))
3362 (day-numbers (org-get-all-dates beg end 'no-ranges
3363 t doclosed ; always include today
3364 org-timeline-show-empty-dates))
3365 (org-deadline-warning-days 0)
3366 (org-agenda-only-exact-dates t)
3367 (today (org-today))
3368 (past t)
3369 args
3370 s e rtn d emptyp)
3371 (setq org-agenda-redo-command
3372 (list 'progn
3373 (list 'org-switch-to-buffer-other-window (current-buffer))
3374 (list 'org-timeline (list 'quote dotodo))))
3375 (if (not dopast)
3376 ;; Remove past dates from the list of dates.
3377 (setq day-numbers (delq nil (mapcar (lambda(x)
3378 (if (>= x today) x nil))
3379 day-numbers))))
3380 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3381 (if doclosed (push :closed args))
3382 (push :timestamp args)
3383 (push :deadline args)
3384 (push :scheduled args)
3385 (push :sexp args)
3386 (if dotodo (push :todo args))
3387 (insert "Timeline of file " entry "\n")
3388 (add-text-properties (point-min) (point)
3389 (list 'face 'org-agenda-structure))
3390 (org-agenda-mark-header-line (point-min))
3391 (while (setq d (pop day-numbers))
3392 (if (and (listp d) (eq (car d) :omitted))
3393 (progn
3394 (setq s (point))
3395 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3396 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3397 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3398 (if (and (>= d today)
3399 dopast
3400 past)
3401 (progn
3402 (setq past nil)
3403 (insert (make-string 79 ?-) "\n")))
3404 (setq date (calendar-gregorian-from-absolute d))
3405 (setq s (point))
3406 (setq rtn (and (not emptyp)
3407 (apply 'org-agenda-get-day-entries entry
3408 date args)))
3409 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3410 (progn
3411 (insert
3412 (if (stringp org-agenda-format-date)
3413 (format-time-string org-agenda-format-date
3414 (org-time-from-absolute date))
3415 (funcall org-agenda-format-date date))
3416 "\n")
3417 (put-text-property s (1- (point)) 'face
3418 (org-agenda-get-day-face date))
3419 (put-text-property s (1- (point)) 'org-date-line t)
3420 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3421 (if (equal d today)
3422 (put-text-property s (1- (point)) 'org-today t))
3423 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3424 (put-text-property s (1- (point)) 'day d)))))
3425 (goto-char (point-min))
3426 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3427 (point-min)))
3428 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3429 (org-finalize-agenda)
3430 (setq buffer-read-only t)))
3432 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3433 "Return a list of all relevant day numbers from BEG to END buffer positions.
3434 If NO-RANGES is non-nil, include only the start and end dates of a range,
3435 not every single day in the range. If FORCE-TODAY is non-nil, make
3436 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3437 inactive time stamps (those in square brackets) are included.
3438 When EMPTY is non-nil, also include days without any entries."
3439 (let ((re (concat
3440 (if pre-re pre-re "")
3441 (if inactive org-ts-regexp-both org-ts-regexp)))
3442 dates dates1 date day day1 day2 ts1 ts2)
3443 (if force-today
3444 (setq dates (list (org-today))))
3445 (save-excursion
3446 (goto-char beg)
3447 (while (re-search-forward re end t)
3448 (setq day (time-to-days (org-time-string-to-time
3449 (substring (match-string 1) 0 10))))
3450 (or (memq day dates) (push day dates)))
3451 (unless no-ranges
3452 (goto-char beg)
3453 (while (re-search-forward org-tr-regexp end t)
3454 (setq ts1 (substring (match-string 1) 0 10)
3455 ts2 (substring (match-string 2) 0 10)
3456 day1 (time-to-days (org-time-string-to-time ts1))
3457 day2 (time-to-days (org-time-string-to-time ts2)))
3458 (while (< (setq day1 (1+ day1)) day2)
3459 (or (memq day1 dates) (push day1 dates)))))
3460 (setq dates (sort dates '<))
3461 (when empty
3462 (while (setq day (pop dates))
3463 (setq day2 (car dates))
3464 (push day dates1)
3465 (when (and day2 empty)
3466 (if (or (eq empty t)
3467 (and (numberp empty) (<= (- day2 day) empty)))
3468 (while (< (setq day (1+ day)) day2)
3469 (push (list day) dates1))
3470 (push (cons :omitted (- day2 day)) dates1))))
3471 (setq dates (nreverse dates1)))
3472 dates)))
3474 ;;; Agenda Daily/Weekly
3476 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3477 "Start day for the agenda view.
3478 Custom commands can set this variable in the options section.")
3479 (defvar org-starting-day nil) ; local variable in the agenda buffer
3480 (defvar org-agenda-current-span nil
3481 "The current span used in the agenda view.") ; local variable in the agenda buffer
3482 (defvar org-arg-loc nil) ; local variable
3484 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3485 "List of types searched for when creating the daily/weekly agenda.
3486 This variable is a list of symbols that controls the types of
3487 items that appear in the daily/weekly agenda. Allowed symbols in this
3488 list are are
3490 :timestamp List items containing a date stamp or date range matching
3491 the selected date. This includes sexp entries in
3492 angular brackets.
3494 :sexp List entries resulting from plain diary-like sexps.
3496 :deadline List deadline due on that date. When the date is today,
3497 also list any deadlines past due, or due within
3498 `org-deadline-warning-days'. `:deadline' must appear before
3499 `:scheduled' if the setting of
3500 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3501 any effect.
3503 :scheduled List all items which are scheduled for the given date.
3504 The diary for *today* also contains items which were
3505 scheduled earlier and are not yet marked DONE.
3507 By default, all four types are turned on.
3509 Never set this variable globally using `setq', because then it
3510 will apply to all future agenda commands. Instead, bind it with
3511 `let' to scope it dynamically into the agenda-constructing
3512 command. A good way to set it is through options in
3513 `org-agenda-custom-commands'. For a more flexible (though
3514 somewhat less efficient) way of determining what is included in
3515 the daily/weekly agenda, see `org-agenda-skip-function'.")
3517 ;;;###autoload
3518 (defun org-agenda-list (&optional arg start-day span)
3519 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3520 The view will be for the current day or week, but from the overview buffer
3521 you will be able to go to other days/weeks.
3523 With a numeric prefix argument in an interactive call, the agenda will
3524 span ARG days. Lisp programs should instead specify SPAN to change
3525 the number of days. SPAN defaults to `org-agenda-span'.
3527 START-DAY defaults to TODAY, or to the most recent match for the weekday
3528 given in `org-agenda-start-on-weekday'."
3529 (interactive "P")
3530 (if (and (integerp arg) (> arg 0))
3531 (setq span arg arg nil))
3532 (setq start-day (or start-day org-agenda-start-day))
3533 (if org-agenda-overriding-arguments
3534 (setq arg (car org-agenda-overriding-arguments)
3535 start-day (nth 1 org-agenda-overriding-arguments)
3536 span (nth 2 org-agenda-overriding-arguments)))
3537 (if (stringp start-day)
3538 ;; Convert to an absolute day number
3539 (setq start-day (time-to-days (org-read-date nil t start-day))))
3540 (setq org-agenda-last-arguments (list arg start-day span))
3541 (org-compile-prefix-format 'agenda)
3542 (org-set-sorting-strategy 'agenda)
3543 (let* ((span (org-agenda-ndays-to-span
3544 (or span org-agenda-ndays org-agenda-span)))
3545 (today (org-today))
3546 (sd (or start-day today))
3547 (ndays (org-agenda-span-to-ndays span sd))
3548 (org-agenda-start-on-weekday
3549 (if (eq ndays 7)
3550 org-agenda-start-on-weekday))
3551 (thefiles (org-agenda-files nil 'ifmode))
3552 (files thefiles)
3553 (start (if (or (null org-agenda-start-on-weekday)
3554 (< ndays 7))
3556 (let* ((nt (calendar-day-of-week
3557 (calendar-gregorian-from-absolute sd)))
3558 (n1 org-agenda-start-on-weekday)
3559 (d (- nt n1)))
3560 (- sd (+ (if (< d 0) 7 0) d)))))
3561 (day-numbers (list start))
3562 (day-cnt 0)
3563 (inhibit-redisplay (not debug-on-error))
3564 s e rtn rtnall file date d start-pos end-pos todayp
3565 clocktable-start clocktable-end filter)
3566 (setq org-agenda-redo-command
3567 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3568 (dotimes (n (1- ndays))
3569 (push (1+ (car day-numbers)) day-numbers))
3570 (setq day-numbers (nreverse day-numbers))
3571 (setq clocktable-start (car day-numbers)
3572 clocktable-end (1+ (or (org-last day-numbers) 0)))
3573 (org-prepare-agenda "Day/Week")
3574 (org-set-local 'org-starting-day (car day-numbers))
3575 (org-set-local 'org-arg-loc arg)
3576 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3577 (unless org-agenda-compact-blocks
3578 (let* ((d1 (car day-numbers))
3579 (d2 (org-last day-numbers))
3580 (w1 (org-days-to-iso-week d1))
3581 (w2 (org-days-to-iso-week d2)))
3582 (setq s (point))
3583 (if org-agenda-overriding-header
3584 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3585 nil 'face 'org-agenda-structure) "\n")
3586 (insert (org-agenda-span-name span)
3587 "-agenda"
3588 (if (< (- d2 d1) 350)
3589 (if (= w1 w2)
3590 (format " (W%02d)" w1)
3591 (format " (W%02d-W%02d)" w1 w2))
3593 ":\n")))
3594 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3595 'org-date-line t))
3596 (org-agenda-mark-header-line s))
3597 (while (setq d (pop day-numbers))
3598 (setq date (calendar-gregorian-from-absolute d)
3599 s (point))
3600 (if (or (setq todayp (= d today))
3601 (and (not start-pos) (= d sd)))
3602 (setq start-pos (point))
3603 (if (and start-pos (not end-pos))
3604 (setq end-pos (point))))
3605 (setq files thefiles
3606 rtnall nil)
3607 (while (setq file (pop files))
3608 (catch 'nextfile
3609 (org-check-agenda-file file)
3610 (let ((org-agenda-entry-types org-agenda-entry-types))
3611 (unless org-agenda-include-deadlines
3612 (setq org-agenda-entry-types
3613 (delq :deadline org-agenda-entry-types)))
3614 (cond
3615 ((memq org-agenda-show-log '(only clockcheck))
3616 (setq rtn (org-agenda-get-day-entries
3617 file date :closed)))
3618 (org-agenda-show-log
3619 (setq rtn (apply 'org-agenda-get-day-entries
3620 file date
3621 (append '(:closed) org-agenda-entry-types))))
3623 (setq rtn (apply 'org-agenda-get-day-entries
3624 file date
3625 org-agenda-entry-types)))))
3626 (setq rtnall (append rtnall rtn))))
3627 (if org-agenda-include-diary
3628 (let ((org-agenda-search-headline-for-time t))
3629 (require 'diary-lib)
3630 (setq rtn (org-get-entries-from-diary date))
3631 (setq rtnall (append rtnall rtn))))
3632 (if (or rtnall org-agenda-show-all-dates)
3633 (progn
3634 (setq day-cnt (1+ day-cnt))
3635 (insert
3636 (if (stringp org-agenda-format-date)
3637 (format-time-string org-agenda-format-date
3638 (org-time-from-absolute date))
3639 (funcall org-agenda-format-date date))
3640 "\n")
3641 (put-text-property s (1- (point)) 'face
3642 (org-agenda-get-day-face date))
3643 (put-text-property s (1- (point)) 'org-date-line t)
3644 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3645 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3646 (when todayp
3647 (put-text-property s (1- (point)) 'org-today t))
3648 (if rtnall (insert
3649 (org-finalize-agenda-entries
3650 (org-agenda-add-time-grid-maybe
3651 rtnall ndays todayp))
3652 "\n"))
3653 (put-text-property s (1- (point)) 'day d)
3654 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3655 (when (and org-agenda-clockreport-mode clocktable-start)
3656 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3657 ;; the above line is to ensure the restricted range!
3658 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3659 tbl)
3660 (setq p (org-plist-delete p :block))
3661 (setq p (plist-put p :tstart clocktable-start))
3662 (setq p (plist-put p :tend clocktable-end))
3663 (setq p (plist-put p :scope 'agenda))
3664 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3665 (setq filter (or org-agenda-filter-while-redo
3666 (get 'org-agenda-filter :preset-filter))))
3667 (setq p (plist-put p :tags (mapconcat (lambda (x)
3668 (if (string-match "[<>=]" x)
3671 filter ""))))
3672 (setq tbl (apply 'org-get-clocktable p))
3673 (insert tbl)))
3674 (goto-char (point-min))
3675 (or org-agenda-multi (org-fit-agenda-window))
3676 (unless (and (pos-visible-in-window-p (point-min))
3677 (pos-visible-in-window-p (point-max)))
3678 (goto-char (1- (point-max)))
3679 (recenter -1)
3680 (if (not (pos-visible-in-window-p (or start-pos 1)))
3681 (progn
3682 (goto-char (or start-pos 1))
3683 (recenter 1))))
3684 (goto-char (or start-pos 1))
3685 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3686 (if (eq org-agenda-show-log 'clockcheck)
3687 (org-agenda-show-clocking-issues))
3688 (org-finalize-agenda)
3689 (setq buffer-read-only t)
3690 (message "")))
3692 (defun org-agenda-ndays-to-span (n)
3693 "Return a span symbol for a span of N days, or N if none matches."
3694 (cond ((symbolp n) n)
3695 ((= n 1) 'day)
3696 ((= n 7) 'week)
3697 (t n)))
3699 (defun org-agenda-span-to-ndays (span start-day)
3700 "Return ndays from SPAN starting at START-DAY."
3701 (cond ((numberp span) span)
3702 ((eq span 'day) 1)
3703 ((eq span 'week) 7)
3704 ((eq span 'month)
3705 (let ((date (calendar-gregorian-from-absolute start-day)))
3706 (calendar-last-day-of-month (car date) (caddr date))))
3707 ((eq span 'year)
3708 (let ((date (calendar-gregorian-from-absolute start-day)))
3709 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3711 (defun org-agenda-span-name (span)
3712 "Return a SPAN name."
3713 (if (null span)
3715 (if (symbolp span)
3716 (capitalize (symbol-name span))
3717 (format "%d days" span))))
3719 ;;; Agenda word search
3721 (defvar org-agenda-search-history nil)
3722 (defvar org-todo-only nil)
3724 (defvar org-search-syntax-table nil
3725 "Special syntax table for org-mode search.
3726 In this table, we have single quotes not as word constituents, to
3727 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3729 (defun org-search-syntax-table ()
3730 (unless org-search-syntax-table
3731 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3732 (modify-syntax-entry ?' "." org-search-syntax-table)
3733 (modify-syntax-entry ?` "." org-search-syntax-table))
3734 org-search-syntax-table)
3736 (defvar org-agenda-last-search-view-search-was-boolean nil)
3738 ;;;###autoload
3739 (defun org-search-view (&optional todo-only string edit-at)
3740 "Show all entries that contain a phrase or words or regular expressions.
3742 With optional prefix argument TODO-ONLY, only consider entries that are
3743 TODO entries. The argument STRING can be used to pass a default search
3744 string into this function. If EDIT-AT is non-nil, it means that the
3745 user should get a chance to edit this string, with cursor at position
3746 EDIT-AT.
3748 The search string can be viewed either as a phrase that should be found as
3749 is, or it can be broken into a number of snippets, each of which must match
3750 in a Boolean way to select an entry. The default depends on the variable
3751 `org-agenda-search-view-always-boolean'.
3752 Even if this is turned off (the default) you can always switch to
3753 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3755 The default is a direct search of the whole phrase, where each space in
3756 the search string can expand to an arbitrary amount of whitespace,
3757 including newlines.
3759 If using a Boolean search, the search string is split on whitespace and
3760 each snippet is searched separately, with logical AND to select an entry.
3761 Words prefixed with a minus must *not* occur in the entry. Words without
3762 a prefix or prefixed with a plus must occur in the entry. Matching is
3763 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3764 match whole words, not parts of a word) if
3765 `org-agenda-search-view-force-full-words' is set (default is nil).
3767 Boolean search snippets enclosed by curly braces are interpreted as
3768 regular expressions that must or (when preceded with \"-\") must not
3769 match in the entry. Snippets enclosed into double quotes will be taken
3770 as a whole, to include whitespace.
3772 - If the search string starts with an asterisk, search only in headlines.
3773 - If (possibly after the leading star) the search string starts with an
3774 exclamation mark, this also means to look at TODO entries only, an effect
3775 that can also be achieved with a prefix argument.
3776 - If (possibly after star and exclamation mark) the search string starts
3777 with a colon, this will mean that the (non-regexp) snippets of the
3778 Boolean search must match as full words.
3780 This command searches the agenda files, and in addition the files listed
3781 in `org-agenda-text-search-extra-files'."
3782 (interactive "P")
3783 (org-compile-prefix-format 'search)
3784 (org-set-sorting-strategy 'search)
3785 (org-prepare-agenda "SEARCH")
3786 (let* ((props (list 'face nil
3787 'done-face 'org-agenda-done
3788 'org-not-done-regexp org-not-done-regexp
3789 'org-todo-regexp org-todo-regexp
3790 'org-complex-heading-regexp org-complex-heading-regexp
3791 'mouse-face 'highlight
3792 'help-echo (format "mouse-2 or RET jump to location")))
3793 (full-words org-agenda-search-view-force-full-words)
3794 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3795 regexp rtn rtnall files file pos
3796 marker category tags c neg re boolean
3797 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3798 (unless (and (not edit-at)
3799 (stringp string)
3800 (string-match "\\S-" string))
3801 (setq string (read-string
3802 (if org-agenda-search-view-always-boolean
3803 "[+-]Word/{Regexp} ...: "
3804 "Phrase, or [+-]Word/{Regexp} ...: ")
3805 (cond
3806 ((integerp edit-at) (cons string edit-at))
3807 (edit-at string))
3808 'org-agenda-search-history)))
3809 (org-set-local 'org-todo-only todo-only)
3810 (setq org-agenda-redo-command
3811 (list 'org-search-view (if todo-only t nil) string
3812 '(if current-prefix-arg 1 nil)))
3813 (setq org-agenda-query-string string)
3815 (if (equal (string-to-char string) ?*)
3816 (setq hdl-only t
3817 words (substring string 1))
3818 (setq words string))
3819 (when (equal (string-to-char words) ?!)
3820 (setq todo-only t
3821 words (substring words 1)))
3822 (when (equal (string-to-char words) ?:)
3823 (setq full-words t
3824 words (substring words 1)))
3825 (if (or org-agenda-search-view-always-boolean
3826 (member (string-to-char words) '(?- ?+ ?\{)))
3827 (setq boolean t))
3828 (setq words (org-split-string words))
3829 (let (www w)
3830 (while (setq w (pop words))
3831 (while (and (string-match "\\\\\\'" w) words)
3832 (setq w (concat (substring w 0 -1) " " (pop words))))
3833 (push w www))
3834 (setq words (nreverse www) www nil)
3835 (while (setq w (pop words))
3836 (when (and (string-match "\\`[-+]?{" w)
3837 (not (string-match "}\\'" w)))
3838 (while (and words (not (string-match "}\\'" (car words))))
3839 (setq w (concat w " " (pop words))))
3840 (setq w (concat w " " (pop words))))
3841 (push w www))
3842 (setq words (nreverse www)))
3843 (setq org-agenda-last-search-view-search-was-boolean boolean)
3844 (when boolean
3845 (let (wds w)
3846 (while (setq w (pop words))
3847 (if (or (equal (substring w 0 1) "\"")
3848 (and (> (length w) 1)
3849 (member (substring w 0 1) '("+" "-"))
3850 (equal (substring w 1 2) "\"")))
3851 (while (and words (not (equal (substring w -1) "\"")))
3852 (setq w (concat w " " (pop words)))))
3853 (and (string-match "\\`\\([-+]?\\)\"" w)
3854 (setq w (replace-match "\\1" nil nil w)))
3855 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3856 (push w wds))
3857 (setq words (nreverse wds))))
3858 (if boolean
3859 (mapc (lambda (w)
3860 (setq c (string-to-char w))
3861 (if (equal c ?-)
3862 (setq neg t w (substring w 1))
3863 (if (equal c ?+)
3864 (setq neg nil w (substring w 1))
3865 (setq neg nil)))
3866 (if (string-match "\\`{.*}\\'" w)
3867 (setq re (substring w 1 -1))
3868 (if full-words
3869 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3870 (setq re (regexp-quote (downcase w)))))
3871 (if neg (push re regexps-) (push re regexps+)))
3872 words)
3873 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3874 regexps+))
3875 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3876 (if (not regexps+)
3877 (setq regexp org-outline-regexp-bol)
3878 (setq regexp (pop regexps+))
3879 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3880 regexp))))
3881 (setq files (org-agenda-files nil 'ifmode))
3882 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3883 (pop org-agenda-text-search-extra-files)
3884 (setq files (org-add-archive-files files)))
3885 (setq files (append files org-agenda-text-search-extra-files)
3886 rtnall nil)
3887 (while (setq file (pop files))
3888 (setq ee nil)
3889 (catch 'nextfile
3890 (org-check-agenda-file file)
3891 (setq buffer (if (file-exists-p file)
3892 (org-get-agenda-file-buffer file)
3893 (error "No such file %s" file)))
3894 (if (not buffer)
3895 ;; If file does not exist, make sure an error message is sent
3896 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3897 file))))
3898 (with-current-buffer buffer
3899 (with-syntax-table (org-search-syntax-table)
3900 (unless (org-mode-p)
3901 (error "Agenda file %s is not in `org-mode'" file))
3902 (let ((case-fold-search t))
3903 (save-excursion
3904 (save-restriction
3905 (if org-agenda-restrict
3906 (narrow-to-region org-agenda-restrict-begin
3907 org-agenda-restrict-end)
3908 (widen))
3909 (goto-char (point-min))
3910 (unless (or (org-on-heading-p)
3911 (outline-next-heading))
3912 (throw 'nextfile t))
3913 (goto-char (max (point-min) (1- (point))))
3914 (while (re-search-forward regexp nil t)
3915 (org-back-to-heading t)
3916 (skip-chars-forward "* ")
3917 (setq beg (point-at-bol)
3918 beg1 (point)
3919 end (progn (outline-next-heading) (point)))
3920 (catch :skip
3921 (goto-char beg)
3922 (org-agenda-skip)
3923 (setq str (buffer-substring-no-properties
3924 (point-at-bol)
3925 (if hdl-only (point-at-eol) end)))
3926 (mapc (lambda (wr) (when (string-match wr str)
3927 (goto-char (1- end))
3928 (throw :skip t)))
3929 regexps-)
3930 (mapc (lambda (wr) (unless (string-match wr str)
3931 (goto-char (1- end))
3932 (throw :skip t)))
3933 (if todo-only
3934 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3935 regexps+)
3936 regexps+))
3937 (goto-char beg)
3938 (setq marker (org-agenda-new-marker (point))
3939 category (org-get-category)
3940 tags (org-get-tags-at (point))
3941 txt (org-format-agenda-item
3943 (buffer-substring-no-properties
3944 beg1 (point-at-eol))
3945 category tags))
3946 (org-add-props txt props
3947 'org-marker marker 'org-hd-marker marker
3948 'org-todo-regexp org-todo-regexp
3949 'org-complex-heading-regexp org-complex-heading-regexp
3950 'priority 1000 'org-category category
3951 'type "search")
3952 (push txt ee)
3953 (goto-char (1- end))))))))))
3954 (setq rtn (nreverse ee))
3955 (setq rtnall (append rtnall rtn)))
3956 (if org-agenda-overriding-header
3957 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3958 nil 'face 'org-agenda-structure) "\n")
3959 (insert "Search words: ")
3960 (add-text-properties (point-min) (1- (point))
3961 (list 'face 'org-agenda-structure))
3962 (setq pos (point))
3963 (insert string "\n")
3964 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3965 (setq pos (point))
3966 (unless org-agenda-multi
3967 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3968 (add-text-properties pos (1- (point))
3969 (list 'face 'org-agenda-structure))))
3970 (org-agenda-mark-header-line (point-min))
3971 (when rtnall
3972 (insert (org-finalize-agenda-entries rtnall) "\n"))
3973 (goto-char (point-min))
3974 (or org-agenda-multi (org-fit-agenda-window))
3975 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3976 (org-finalize-agenda)
3977 (setq buffer-read-only t)))
3979 ;;; Agenda TODO list
3981 (defvar org-select-this-todo-keyword nil)
3982 (defvar org-last-arg nil)
3984 ;;;###autoload
3985 (defun org-todo-list (arg)
3986 "Show all (not done) TODO entries from all agenda file in a single list.
3987 The prefix arg can be used to select a specific TODO keyword and limit
3988 the list to these. When using \\[universal-argument], you will be prompted
3989 for a keyword. A numeric prefix directly selects the Nth keyword in
3990 `org-todo-keywords-1'."
3991 (interactive "P")
3992 (org-compile-prefix-format 'todo)
3993 (org-set-sorting-strategy 'todo)
3994 (org-prepare-agenda "TODO")
3995 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3996 (let* ((today (org-today))
3997 (date (calendar-gregorian-from-absolute today))
3998 (kwds org-todo-keywords-for-agenda)
3999 (completion-ignore-case t)
4000 (org-select-this-todo-keyword
4001 (if (stringp arg) arg
4002 (and arg (integerp arg) (> arg 0)
4003 (nth (1- arg) kwds))))
4004 rtn rtnall files file pos)
4005 (when (equal arg '(4))
4006 (setq org-select-this-todo-keyword
4007 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4008 (mapcar 'list kwds) nil nil)))
4009 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4010 (org-set-local 'org-last-arg arg)
4011 (setq org-agenda-redo-command
4012 '(org-todo-list (or current-prefix-arg org-last-arg)))
4013 (setq files (org-agenda-files nil 'ifmode)
4014 rtnall nil)
4015 (while (setq file (pop files))
4016 (catch 'nextfile
4017 (org-check-agenda-file file)
4018 (setq rtn (org-agenda-get-day-entries file date :todo))
4019 (setq rtnall (append rtnall rtn))))
4020 (if org-agenda-overriding-header
4021 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4022 nil 'face 'org-agenda-structure) "\n")
4023 (insert "Global list of TODO items of type: ")
4024 (add-text-properties (point-min) (1- (point))
4025 (list 'face 'org-agenda-structure
4026 'short-heading
4027 (concat "ToDo: "
4028 (or org-select-this-todo-keyword "ALL"))))
4029 (org-agenda-mark-header-line (point-min))
4030 (setq pos (point))
4031 (insert (or org-select-this-todo-keyword "ALL") "\n")
4032 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4033 (setq pos (point))
4034 (unless org-agenda-multi
4035 (insert "Available with `N r': (0)ALL")
4036 (let ((n 0) s)
4037 (mapc (lambda (x)
4038 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4039 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4040 (insert "\n "))
4041 (insert " " s))
4042 kwds))
4043 (insert "\n"))
4044 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4045 (org-agenda-mark-header-line (point-min))
4046 (when rtnall
4047 (insert (org-finalize-agenda-entries rtnall) "\n"))
4048 (goto-char (point-min))
4049 (or org-agenda-multi (org-fit-agenda-window))
4050 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4051 (org-finalize-agenda)
4052 (setq buffer-read-only t)))
4054 ;;; Agenda tags match
4056 ;;;###autoload
4057 (defun org-tags-view (&optional todo-only match)
4058 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4059 The prefix arg TODO-ONLY limits the search to TODO entries."
4060 (interactive "P")
4061 (org-compile-prefix-format 'tags)
4062 (org-set-sorting-strategy 'tags)
4063 (let* ((org-tags-match-list-sublevels
4064 org-tags-match-list-sublevels)
4065 (completion-ignore-case t)
4066 rtn rtnall files file pos matcher
4067 buffer)
4068 (when (and (stringp match) (not (string-match "\\S-" match)))
4069 (setq match nil))
4070 (setq matcher (org-make-tags-matcher match)
4071 match (car matcher) matcher (cdr matcher))
4072 (org-prepare-agenda (concat "TAGS " match))
4073 (setq org-agenda-query-string match)
4074 (setq org-agenda-redo-command
4075 (list 'org-tags-view (list 'quote todo-only)
4076 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4077 (setq files (org-agenda-files nil 'ifmode)
4078 rtnall nil)
4079 (while (setq file (pop files))
4080 (catch 'nextfile
4081 (org-check-agenda-file file)
4082 (setq buffer (if (file-exists-p file)
4083 (org-get-agenda-file-buffer file)
4084 (error "No such file %s" file)))
4085 (if (not buffer)
4086 ;; If file does not exist, error message to agenda
4087 (setq rtn (list
4088 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4089 rtnall (append rtnall rtn))
4090 (with-current-buffer buffer
4091 (unless (org-mode-p)
4092 (error "Agenda file %s is not in `org-mode'" file))
4093 (save-excursion
4094 (save-restriction
4095 (if org-agenda-restrict
4096 (narrow-to-region org-agenda-restrict-begin
4097 org-agenda-restrict-end)
4098 (widen))
4099 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4100 (setq rtnall (append rtnall rtn))))))))
4101 (if org-agenda-overriding-header
4102 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4103 nil 'face 'org-agenda-structure) "\n")
4104 (insert "Headlines with TAGS match: ")
4105 (add-text-properties (point-min) (1- (point))
4106 (list 'face 'org-agenda-structure
4107 'short-heading
4108 (concat "Match: " match)))
4109 (setq pos (point))
4110 (insert match "\n")
4111 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4112 (setq pos (point))
4113 (unless org-agenda-multi
4114 (insert "Press `C-u r' to search again with new search string\n"))
4115 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4116 (org-agenda-mark-header-line (point-min))
4117 (when rtnall
4118 (insert (org-finalize-agenda-entries rtnall) "\n"))
4119 (goto-char (point-min))
4120 (or org-agenda-multi (org-fit-agenda-window))
4121 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4122 (org-finalize-agenda)
4123 (setq buffer-read-only t)))
4125 ;;; Agenda Finding stuck projects
4127 (defvar org-agenda-skip-regexp nil
4128 "Regular expression used in skipping subtrees for the agenda.
4129 This is basically a temporary global variable that can be set and then
4130 used by user-defined selections using `org-agenda-skip-function'.")
4132 (defvar org-agenda-overriding-header nil
4133 "When set during agenda, todo and tags searches it replaces the header.
4134 This variable should not be set directly, but custom commands can bind it
4135 in the options section.")
4137 (defun org-agenda-skip-entry-when-regexp-matches ()
4138 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4139 If yes, it returns the end position of this entry, causing agenda commands
4140 to skip the entry but continuing the search in the subtree. This is a
4141 function that can be put into `org-agenda-skip-function' for the duration
4142 of a command."
4143 (let ((end (save-excursion (org-end-of-subtree t)))
4144 skip)
4145 (save-excursion
4146 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4147 (and skip end)))
4149 (defun org-agenda-skip-subtree-when-regexp-matches ()
4150 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4151 If yes, it returns the end position of this tree, causing agenda commands
4152 to skip this subtree. This is a function that can be put into
4153 `org-agenda-skip-function' for the duration of a command."
4154 (let ((end (save-excursion (org-end-of-subtree t)))
4155 skip)
4156 (save-excursion
4157 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4158 (and skip end)))
4160 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4161 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4162 If yes, it returns the end position of the current entry (NOT the tree),
4163 causing agenda commands to skip the entry but continuing the search in
4164 the subtree. This is a function that can be put into
4165 `org-agenda-skip-function' for the duration of a command. An important
4166 use of this function is for the stuck project list."
4167 (let ((end (save-excursion (org-end-of-subtree t)))
4168 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4169 skip)
4170 (save-excursion
4171 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4172 (and skip entry-end)))
4174 (defun org-agenda-skip-entry-if (&rest conditions)
4175 "Skip entry if any of CONDITIONS is true.
4176 See `org-agenda-skip-if' for details."
4177 (org-agenda-skip-if nil conditions))
4179 (defun org-agenda-skip-subtree-if (&rest conditions)
4180 "Skip entry if any of CONDITIONS is true.
4181 See `org-agenda-skip-if' for details."
4182 (org-agenda-skip-if t conditions))
4184 (defun org-agenda-skip-if (subtree conditions)
4185 "Checks current entity for CONDITIONS.
4186 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4187 the entry, i.e. the text before the next heading is checked.
4189 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4190 from different tests. Valid conditions are:
4192 scheduled Check if there is a scheduled cookie
4193 notscheduled Check if there is no scheduled cookie
4194 deadline Check if there is a deadline
4195 notdeadline Check if there is no deadline
4196 timestamp Check if there is a timestamp (also deadline or scheduled)
4197 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4198 regexp Check if regexp matches
4199 notregexp Check if regexp does not match.
4200 todo Check if TODO keyword matches
4201 nottodo Check if TODO keyword does not match
4203 The regexp is taken from the conditions list, it must come right after
4204 the `regexp' or `notregexp' element.
4206 `todo' and `nottodo' accept as an argument a list of todo
4207 keywords, which may include \"*\" to match any todo keyword.
4209 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4211 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4213 Instead of a list a keyword class may be given
4215 (org-agenda-skip-entry-if 'nottodo 'done)
4217 would skip entries that haven't been marked with any of \"DONE\"
4218 keywords. Possible classes are: `todo', `done', `any'.
4220 If any of these conditions is met, this function returns the end point of
4221 the entity, causing the search to continue from there. This is a function
4222 that can be put into `org-agenda-skip-function' for the duration of a command."
4223 (let (beg end m)
4224 (org-back-to-heading t)
4225 (setq beg (point)
4226 end (if subtree
4227 (progn (org-end-of-subtree t) (point))
4228 (progn (outline-next-heading) (1- (point)))))
4229 (goto-char beg)
4230 (and
4232 (and (memq 'scheduled conditions)
4233 (re-search-forward org-scheduled-time-regexp end t))
4234 (and (memq 'notscheduled conditions)
4235 (not (re-search-forward org-scheduled-time-regexp end t)))
4236 (and (memq 'deadline conditions)
4237 (re-search-forward org-deadline-time-regexp end t))
4238 (and (memq 'notdeadline conditions)
4239 (not (re-search-forward org-deadline-time-regexp end t)))
4240 (and (memq 'timestamp conditions)
4241 (re-search-forward org-ts-regexp end t))
4242 (and (memq 'nottimestamp conditions)
4243 (not (re-search-forward org-ts-regexp end t)))
4244 (and (setq m (memq 'regexp conditions))
4245 (stringp (nth 1 m))
4246 (re-search-forward (nth 1 m) end t))
4247 (and (setq m (memq 'notregexp conditions))
4248 (stringp (nth 1 m))
4249 (not (re-search-forward (nth 1 m) end t)))
4250 (and (or
4251 (setq m (memq 'todo conditions))
4252 (setq m (memq 'nottodo conditions)))
4253 (org-agenda-skip-if-todo m end)))
4254 end)))
4256 (defun org-agenda-skip-if-todo (args end)
4257 "Helper function for `org-agenda-skip-if', do not use it directly.
4258 ARGS is a list with first element either `todo' or `nottodo'.
4259 The remainder is either a list of TODO keywords, or a state symbol
4260 `todo' or `done' or `any'."
4261 (let ((kw (car args))
4262 (arg (cadr args))
4263 todo-wds todo-re)
4264 (setq todo-wds
4265 (org-uniquify
4266 (cond
4267 ((listp arg) ;; list of keywords
4268 (if (member "*" arg)
4269 (mapcar 'substring-no-properties org-todo-keywords-1)
4270 arg))
4271 ((symbolp arg) ;; keyword class name
4272 (cond
4273 ((eq arg 'todo)
4274 (org-delete-all org-done-keywords
4275 (mapcar 'substring-no-properties
4276 org-todo-keywords-1)))
4277 ((eq arg 'done) org-done-keywords)
4278 ((eq arg 'any)
4279 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4280 (setq todo-re
4281 (concat "^\\*+[ \t]+\\<\\("
4282 (mapconcat 'identity todo-wds "\\|")
4283 "\\)\\>"))
4284 (if (eq kw 'todo)
4285 (re-search-forward todo-re end t)
4286 (not (re-search-forward todo-re end t)))))
4288 ;;;###autoload
4289 (defun org-agenda-list-stuck-projects (&rest ignore)
4290 "Create agenda view for projects that are stuck.
4291 Stuck projects are project that have no next actions. For the definitions
4292 of what a project is and how to check if it stuck, customize the variable
4293 `org-stuck-projects'."
4294 (interactive)
4295 (let* ((org-agenda-skip-function
4296 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4297 ;; We could have used org-agenda-skip-if here.
4298 (org-agenda-overriding-header
4299 (or org-agenda-overriding-header "List of stuck projects: "))
4300 (matcher (nth 0 org-stuck-projects))
4301 (todo (nth 1 org-stuck-projects))
4302 (todo-wds (if (member "*" todo)
4303 (progn
4304 (org-prepare-agenda-buffers (org-agenda-files
4305 nil 'ifmode))
4306 (org-delete-all
4307 org-done-keywords-for-agenda
4308 (copy-sequence org-todo-keywords-for-agenda)))
4309 todo))
4310 (todo-re (concat "^\\*+[ \t]+\\("
4311 (mapconcat 'identity todo-wds "\\|")
4312 "\\)\\>"))
4313 (tags (nth 2 org-stuck-projects))
4314 (tags-re (if (member "*" tags)
4315 (org-re (concat org-outline-regexp-bol
4316 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4317 (if tags
4318 (concat org-outline-regexp-bol
4319 ".*:\\("
4320 (mapconcat 'identity tags "\\|")
4321 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4322 (gen-re (nth 3 org-stuck-projects))
4323 (re-list
4324 (delq nil
4325 (list
4326 (if todo todo-re)
4327 (if tags tags-re)
4328 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4329 gen-re)))))
4330 (setq org-agenda-skip-regexp
4331 (if re-list
4332 (mapconcat 'identity re-list "\\|")
4333 (error "No information how to identify unstuck projects")))
4334 (org-tags-view nil matcher)
4335 (with-current-buffer org-agenda-buffer-name
4336 (setq org-agenda-redo-command
4337 '(org-agenda-list-stuck-projects
4338 (or current-prefix-arg org-last-arg))))))
4340 ;;; Diary integration
4342 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4343 (defvar list-diary-entries-hook)
4344 (defvar diary-time-regexp)
4345 (defun org-get-entries-from-diary (date)
4346 "Get the (Emacs Calendar) diary entries for DATE."
4347 (require 'diary-lib)
4348 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4349 (diary-display-hook '(fancy-diary-display))
4350 (diary-display-function 'fancy-diary-display)
4351 (pop-up-frames nil)
4352 (list-diary-entries-hook
4353 (cons 'org-diary-default-entry list-diary-entries-hook))
4354 (diary-file-name-prefix-function nil) ; turn this feature off
4355 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4356 entries
4357 (org-disable-agenda-to-diary t))
4358 (save-excursion
4359 (save-window-excursion
4360 (funcall (if (fboundp 'diary-list-entries)
4361 'diary-list-entries 'list-diary-entries)
4362 date 1)))
4363 (if (not (get-buffer diary-fancy-buffer))
4364 (setq entries nil)
4365 (with-current-buffer diary-fancy-buffer
4366 (setq buffer-read-only nil)
4367 (if (zerop (buffer-size))
4368 ;; No entries
4369 (setq entries nil)
4370 ;; Omit the date and other unnecessary stuff
4371 (org-agenda-cleanup-fancy-diary)
4372 ;; Add prefix to each line and extend the text properties
4373 (if (zerop (buffer-size))
4374 (setq entries nil)
4375 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4376 (setq entries
4377 (with-temp-buffer
4378 (insert entries) (goto-char (point-min))
4379 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4380 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4381 (replace-match (concat "; " (match-string 1)))))
4382 (buffer-string)))))
4383 (set-buffer-modified-p nil)
4384 (kill-buffer diary-fancy-buffer)))
4385 (when entries
4386 (setq entries (org-split-string entries "\n"))
4387 (setq entries
4388 (mapcar
4389 (lambda (x)
4390 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4391 ;; Extend the text properties to the beginning of the line
4392 (org-add-props x (text-properties-at (1- (length x)) x)
4393 'type "diary" 'date date 'face 'org-agenda-diary))
4394 entries)))))
4396 (defvar org-agenda-cleanup-fancy-diary-hook nil
4397 "Hook run when the fancy diary buffer is cleaned up.")
4399 (defun org-agenda-cleanup-fancy-diary ()
4400 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4401 This gets rid of the date, the underline under the date, and
4402 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4403 date. It also removes lines that contain only whitespace."
4404 (goto-char (point-min))
4405 (if (looking-at ".*?:[ \t]*")
4406 (progn
4407 (replace-match "")
4408 (re-search-forward "\n=+$" nil t)
4409 (replace-match "")
4410 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4411 (re-search-forward "\n=+$" nil t)
4412 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4413 (goto-char (point-min))
4414 (while (re-search-forward "^ +\n" nil t)
4415 (replace-match ""))
4416 (goto-char (point-min))
4417 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4418 (replace-match ""))
4419 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4421 ;; Make sure entries from the diary have the right text properties.
4422 (eval-after-load "diary-lib"
4423 '(if (boundp 'diary-modify-entry-list-string-function)
4424 ;; We can rely on the hook, nothing to do
4426 ;; Hook not available, must use advice to make this work
4427 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4428 "Make the position visible."
4429 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4430 (stringp string)
4431 buffer-file-name)
4432 (setq string (org-modify-diary-entry-string string))))))
4434 (defun org-modify-diary-entry-string (string)
4435 "Add text properties to string, allowing org-mode to act on it."
4436 (org-add-props string nil
4437 'mouse-face 'highlight
4438 'help-echo (if buffer-file-name
4439 (format "mouse-2 or RET jump to diary file %s"
4440 (abbreviate-file-name buffer-file-name))
4442 'org-agenda-diary-link t
4443 'org-marker (org-agenda-new-marker (point-at-bol))))
4445 (defun org-diary-default-entry ()
4446 "Add a dummy entry to the diary.
4447 Needed to avoid empty dates which mess up holiday display."
4448 ;; Catch the error if dealing with the new add-to-diary-alist
4449 (when org-disable-agenda-to-diary
4450 (condition-case nil
4451 (org-add-to-diary-list original-date "Org-mode dummy" "")
4452 (error
4453 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4455 (defun org-add-to-diary-list (&rest args)
4456 (if (fboundp 'diary-add-to-list)
4457 (apply 'diary-add-to-list args)
4458 (apply 'add-to-diary-list args)))
4460 (defvar org-diary-last-run-time nil)
4462 ;;;###autoload
4463 (defun org-diary (&rest args)
4464 "Return diary information from org-files.
4465 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4466 It accesses org files and extracts information from those files to be
4467 listed in the diary. The function accepts arguments specifying what
4468 items should be listed. For a list of arguments allowed here, see the
4469 variable `org-agenda-entry-types'.
4471 The call in the diary file should look like this:
4473 &%%(org-diary) ~/path/to/some/orgfile.org
4475 Use a separate line for each org file to check. Or, if you omit the file name,
4476 all files listed in `org-agenda-files' will be checked automatically:
4478 &%%(org-diary)
4480 If you don't give any arguments (as in the example above), the default
4481 arguments (:deadline :scheduled :timestamp :sexp) are used.
4482 So the example above may also be written as
4484 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4486 The function expects the lisp variables `entry' and `date' to be provided
4487 by the caller, because this is how the calendar works. Don't use this
4488 function from a program - use `org-agenda-get-day-entries' instead."
4489 (when (> (- (org-float-time)
4490 org-agenda-last-marker-time)
4492 (org-agenda-reset-markers))
4493 (org-compile-prefix-format 'agenda)
4494 (org-set-sorting-strategy 'agenda)
4495 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4496 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4497 (list entry)
4498 (org-agenda-files t)))
4499 (time (org-float-time))
4500 file rtn results)
4501 (when (or (not org-diary-last-run-time)
4502 (> (- time
4503 org-diary-last-run-time)
4505 (org-prepare-agenda-buffers files))
4506 (setq org-diary-last-run-time time)
4507 ;; If this is called during org-agenda, don't return any entries to
4508 ;; the calendar. Org Agenda will list these entries itself.
4509 (if org-disable-agenda-to-diary (setq files nil))
4510 (while (setq file (pop files))
4511 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4512 (setq results (append results rtn)))
4513 (if results
4514 (concat (org-finalize-agenda-entries results) "\n"))))
4516 ;;; Agenda entry finders
4518 (defun org-agenda-get-day-entries (file date &rest args)
4519 "Does the work for `org-diary' and `org-agenda'.
4520 FILE is the path to a file to be checked for entries. DATE is date like
4521 the one returned by `calendar-current-date'. ARGS are symbols indicating
4522 which kind of entries should be extracted. For details about these, see
4523 the documentation of `org-diary'."
4524 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4525 (let* ((org-startup-folded nil)
4526 (org-startup-align-all-tables nil)
4527 (buffer (if (file-exists-p file)
4528 (org-get-agenda-file-buffer file)
4529 (error "No such file %s" file)))
4530 arg results rtn deadline-results)
4531 (if (not buffer)
4532 ;; If file does not exist, make sure an error message ends up in diary
4533 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4534 (with-current-buffer buffer
4535 (unless (org-mode-p)
4536 (error "Agenda file %s is not in `org-mode'" file))
4537 (let ((case-fold-search nil))
4538 (save-excursion
4539 (save-restriction
4540 (if org-agenda-restrict
4541 (narrow-to-region org-agenda-restrict-begin
4542 org-agenda-restrict-end)
4543 (widen))
4544 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4545 (while (setq arg (pop args))
4546 (cond
4547 ((and (eq arg :todo)
4548 (equal date (calendar-gregorian-from-absolute
4549 (org-today))))
4550 (setq rtn (org-agenda-get-todos))
4551 (setq results (append results rtn)))
4552 ((eq arg :timestamp)
4553 (setq rtn (org-agenda-get-blocks))
4554 (setq results (append results rtn))
4555 (setq rtn (org-agenda-get-timestamps))
4556 (setq results (append results rtn)))
4557 ((eq arg :sexp)
4558 (setq rtn (org-agenda-get-sexps))
4559 (setq results (append results rtn)))
4560 ((eq arg :scheduled)
4561 (setq rtn (org-agenda-get-scheduled deadline-results))
4562 (setq results (append results rtn)))
4563 ((eq arg :closed)
4564 (setq rtn (org-agenda-get-progress))
4565 (setq results (append results rtn)))
4566 ((eq arg :deadline)
4567 (setq rtn (org-agenda-get-deadlines))
4568 (setq deadline-results (copy-sequence rtn))
4569 (setq results (append results rtn))))))))
4570 results))))
4572 (defun org-agenda-get-todos ()
4573 "Return the TODO information for agenda display."
4574 (let* ((props (list 'face nil
4575 'done-face 'org-agenda-done
4576 'org-not-done-regexp org-not-done-regexp
4577 'org-todo-regexp org-todo-regexp
4578 'org-complex-heading-regexp org-complex-heading-regexp
4579 'mouse-face 'highlight
4580 'help-echo
4581 (format "mouse-2 or RET jump to org file %s"
4582 (abbreviate-file-name buffer-file-name))))
4583 (regexp (concat "^\\*+[ \t]+\\("
4584 (if org-select-this-todo-keyword
4585 (if (equal org-select-this-todo-keyword "*")
4586 org-todo-regexp
4587 (concat "\\<\\("
4588 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4589 "\\)\\>"))
4590 org-not-done-regexp)
4591 "[^\n\r]*\\)"))
4592 marker priority category tags todo-state
4593 ee txt beg end)
4594 (goto-char (point-min))
4595 (while (re-search-forward regexp nil t)
4596 (catch :skip
4597 (save-match-data
4598 (beginning-of-line)
4599 (org-agenda-skip)
4600 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4601 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4602 (goto-char (1+ beg))
4603 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4604 (throw :skip nil)))
4605 (goto-char (match-beginning 1))
4606 (setq marker (org-agenda-new-marker (match-beginning 0))
4607 category (org-get-category)
4608 txt (match-string 1)
4609 tags (org-get-tags-at (point))
4610 txt (org-format-agenda-item "" txt category tags)
4611 priority (1+ (org-get-priority txt))
4612 todo-state (org-get-todo-state))
4613 (org-add-props txt props
4614 'org-marker marker 'org-hd-marker marker
4615 'priority priority 'org-category category
4616 'type "todo" 'todo-state todo-state)
4617 (push txt ee)
4618 (if org-agenda-todo-list-sublevels
4619 (goto-char (match-end 1))
4620 (org-end-of-subtree 'invisible))))
4621 (nreverse ee)))
4623 (defun org-agenda-todo-custom-ignore-p (time n)
4624 "Check whether timestamp is farther away then n number of days.
4625 This function is invoked if `org-agenda-todo-ignore-deadlines',
4626 `org-agenda-todo-ignore-scheduled' or
4627 `org-agenda-todo-ignore-timestamp' is set to an integer."
4628 (let ((days (org-days-to-time time)))
4629 (if (>= n 0)
4630 (>= days n)
4631 (<= days n))))
4633 ;;;###autoload
4634 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4635 (&optional end)
4636 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4637 (when (or org-agenda-todo-ignore-with-date
4638 org-agenda-todo-ignore-scheduled
4639 org-agenda-todo-ignore-deadlines
4640 org-agenda-todo-ignore-timestamp)
4641 (setq end (or end (save-excursion (outline-next-heading) (point))))
4642 (save-excursion
4643 (or (and org-agenda-todo-ignore-with-date
4644 (re-search-forward org-ts-regexp end t))
4645 (and org-agenda-todo-ignore-scheduled
4646 (re-search-forward org-scheduled-time-regexp end t)
4647 (cond
4648 ((eq org-agenda-todo-ignore-scheduled 'future)
4649 (> (org-days-to-time (match-string 1)) 0))
4650 ((eq org-agenda-todo-ignore-scheduled 'past)
4651 (<= (org-days-to-time (match-string 1)) 0))
4652 ((numberp org-agenda-todo-ignore-scheduled)
4653 (org-agenda-todo-custom-ignore-p
4654 (match-string 1) org-agenda-todo-ignore-scheduled))
4655 (t)))
4656 (and org-agenda-todo-ignore-deadlines
4657 (re-search-forward org-deadline-time-regexp end t)
4658 (cond
4659 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4660 ((eq org-agenda-todo-ignore-deadlines 'far)
4661 (not (org-deadline-close (match-string 1))))
4662 ((eq org-agenda-todo-ignore-deadlines 'future)
4663 (> (org-days-to-time (match-string 1)) 0))
4664 ((eq org-agenda-todo-ignore-deadlines 'past)
4665 (<= (org-days-to-time (match-string 1)) 0))
4666 ((numberp org-agenda-todo-ignore-deadlines)
4667 (org-agenda-todo-custom-ignore-p
4668 (match-string 1) org-agenda-todo-ignore-deadlines))
4669 (t (org-deadline-close (match-string 1)))))
4670 (and org-agenda-todo-ignore-timestamp
4671 (let ((buffer (current-buffer))
4672 (regexp
4673 (concat
4674 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4675 (start (point)))
4676 ;; Copy current buffer into a temporary one
4677 (with-temp-buffer
4678 (insert-buffer-substring buffer start end)
4679 (goto-char (point-min))
4680 ;; Delete SCHEDULED and DEADLINE items
4681 (while (re-search-forward regexp end t)
4682 (delete-region (match-beginning 0) (match-end 0)))
4683 (goto-char (point-min))
4684 ;; No search for timestamp left
4685 (when (re-search-forward org-ts-regexp nil t)
4686 (cond
4687 ((eq org-agenda-todo-ignore-timestamp 'future)
4688 (> (org-days-to-time (match-string 1)) 0))
4689 ((eq org-agenda-todo-ignore-timestamp 'past)
4690 (<= (org-days-to-time (match-string 1)) 0))
4691 ((numberp org-agenda-todo-ignore-timestamp)
4692 (org-agenda-todo-custom-ignore-p
4693 (match-string 1) org-agenda-todo-ignore-timestamp))
4694 (t))))))))))
4696 (defconst org-agenda-no-heading-message
4697 "No heading for this item in buffer or region.")
4699 (defun org-agenda-get-timestamps ()
4700 "Return the date stamp information for agenda display."
4701 (let* ((props (list 'face nil
4702 'org-not-done-regexp org-not-done-regexp
4703 'org-todo-regexp org-todo-regexp
4704 'org-complex-heading-regexp org-complex-heading-regexp
4705 'mouse-face 'highlight
4706 'help-echo
4707 (format "mouse-2 or RET jump to org file %s"
4708 (abbreviate-file-name buffer-file-name))))
4709 (d1 (calendar-absolute-from-gregorian date))
4710 (remove-re
4711 (concat
4712 (regexp-quote
4713 (format-time-string
4714 "<%Y-%m-%d"
4715 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4716 ".*?>"))
4717 (regexp
4718 (concat
4719 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4720 (regexp-quote
4721 (substring
4722 (format-time-string
4723 (car org-time-stamp-formats)
4724 (apply 'encode-time ; DATE bound by calendar
4725 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4726 1 11))
4727 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4728 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4729 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4730 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4731 todo-state end-of-match show-all)
4732 (goto-char (point-min))
4733 (while (setq end-of-match (re-search-forward regexp nil t))
4734 (setq b0 (match-beginning 0)
4735 b3 (match-beginning 3) e3 (match-end 3)
4736 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4737 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4738 (member todo-state
4739 org-agenda-repeating-timestamp-show-all)))
4740 (catch :skip
4741 (and (org-at-date-range-p) (throw :skip nil))
4742 (org-agenda-skip)
4743 (if (and (match-end 1)
4744 (not (= d1 (org-time-string-to-absolute
4745 (match-string 1) d1 nil show-all))))
4746 (throw :skip nil))
4747 (if (and e3
4748 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4749 (throw :skip nil))
4750 (setq tmp (buffer-substring (max (point-min)
4751 (- b0 org-ds-keyword-length))
4753 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4754 inactivep (= (char-after b0) ?\[)
4755 deadlinep (string-match org-deadline-regexp tmp)
4756 scheduledp (string-match org-scheduled-regexp tmp)
4757 closedp (and org-agenda-include-inactive-timestamps
4758 (string-match org-closed-string tmp))
4759 clockp (and org-agenda-include-inactive-timestamps
4760 (or (string-match org-clock-string tmp)
4761 (string-match "]-+\\'" tmp)))
4762 donep (member todo-state org-done-keywords))
4763 (if (or scheduledp deadlinep closedp clockp
4764 (and donep org-agenda-skip-timestamp-if-done))
4765 (throw :skip t))
4766 (if (string-match ">" timestr)
4767 ;; substring should only run to end of time stamp
4768 (setq timestr (substring timestr 0 (match-end 0))))
4769 (setq marker (org-agenda-new-marker b0)
4770 category (org-get-category b0))
4771 (save-excursion
4772 (if (not (re-search-backward org-outline-regexp-bol nil t))
4773 (setq txt org-agenda-no-heading-message)
4774 (goto-char (match-beginning 0))
4775 (setq hdmarker (org-agenda-new-marker)
4776 tags (org-get-tags-at))
4777 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4778 (setq head (or (match-string 1) ""))
4779 (setq txt (org-format-agenda-item
4780 (if inactivep org-agenda-inactive-leader nil)
4781 head category tags timestr
4782 remove-re)))
4783 (setq priority (org-get-priority txt))
4784 (org-add-props txt props
4785 'org-marker marker 'org-hd-marker hdmarker)
4786 (org-add-props txt nil 'priority priority
4787 'org-category category 'date date
4788 'todo-state todo-state
4789 'type "timestamp")
4790 (push txt ee))
4791 (if org-agenda-skip-additional-timestamps-same-entry
4792 (outline-next-heading)
4793 (goto-char end-of-match))))
4794 (nreverse ee)))
4796 (defun org-agenda-get-sexps ()
4797 "Return the sexp information for agenda display."
4798 (require 'diary-lib)
4799 (let* ((props (list 'mouse-face 'highlight
4800 'help-echo
4801 (format "mouse-2 or RET jump to org file %s"
4802 (abbreviate-file-name buffer-file-name))))
4803 (regexp "^&?%%(")
4804 marker category ee txt tags entry result beg b sexp sexp-entry
4805 todo-state)
4806 (goto-char (point-min))
4807 (while (re-search-forward regexp nil t)
4808 (catch :skip
4809 (org-agenda-skip)
4810 (setq beg (match-beginning 0))
4811 (goto-char (1- (match-end 0)))
4812 (setq b (point))
4813 (forward-sexp 1)
4814 (setq sexp (buffer-substring b (point)))
4815 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4816 (org-trim (match-string 1))
4817 ""))
4818 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4819 (when result
4820 (setq marker (org-agenda-new-marker beg)
4821 category (org-get-category beg)
4822 todo-state (org-get-todo-state))
4824 (dolist (r (if (stringp result)
4825 (list result)
4826 result)) ;; we expect a list here
4827 (if (string-match "\\S-" r)
4828 (setq txt r)
4829 (setq txt "SEXP entry returned empty string"))
4831 (setq txt (org-format-agenda-item
4832 "" txt category tags 'time))
4833 (org-add-props txt props 'org-marker marker)
4834 (org-add-props txt nil
4835 'org-category category 'date date 'todo-state todo-state
4836 'type "sexp")
4837 (push txt ee)))))
4838 (nreverse ee)))
4840 ;; Calendar sanity: define some functions that are independent of
4841 ;; `calendar-date-style'.
4842 ;; Normally I would like to use ISO format when calling the diary functions,
4843 ;; but to make sure we still have Emacs 22 compatibility we bind
4844 ;; also `european-calendar-style' and use european format
4845 (defun org-anniversary (year month day &optional mark)
4846 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4847 (org-no-warnings
4848 (let ((calendar-date-style 'european) (european-calendar-style t))
4849 (diary-anniversary day month year mark))))
4850 (defun org-cyclic (N year month day &optional mark)
4851 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4852 (org-no-warnings
4853 (let ((calendar-date-style 'european) (european-calendar-style t))
4854 (diary-cyclic N day month year mark))))
4855 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4856 "Like `diary-block', but with fixed (ISO) order of arguments."
4857 (org-no-warnings
4858 (let ((calendar-date-style 'european) (european-calendar-style t))
4859 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4860 (defun org-date (year month day &optional mark)
4861 "Like `diary-date', but with fixed (ISO) order of arguments."
4862 (org-no-warnings
4863 (let ((calendar-date-style 'european) (european-calendar-style t))
4864 (diary-date day month year mark))))
4865 (defalias 'org-float 'diary-float)
4867 ;; Define the` org-class' function
4868 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4869 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4870 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4871 is any number of ISO weeks in the block period for which the item should
4872 be skipped."
4873 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4874 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4875 (d (calendar-absolute-from-gregorian date)))
4876 (and
4877 (<= date1 d)
4878 (<= d date2)
4879 (= (calendar-day-of-week date) dayname)
4880 (or (not skip-weeks)
4881 (progn
4882 (require 'cal-iso)
4883 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4884 entry)))
4886 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4887 "Like `org-class', but honor `calendar-date-style'.
4888 The order of the first 2 times 3 arguments depends on the variable
4889 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4890 So for American calendars, give this as MONTH DAY YEAR, for European as
4891 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4892 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4893 is any number of ISO weeks in the block period for which the item should
4894 be skipped.
4896 This function is here only for backward compatibility and it is deprecated,
4897 please use `org-class' instead."
4898 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4899 (date2 (org-order-calendar-date-args m2 d2 y2)))
4900 (org-class
4901 (nth 2 date1) (car date1) (nth 1 date1)
4902 (nth 2 date2) (car date2) (nth 1 date2)
4903 dayname skip-weeks)))
4905 (defalias 'org-get-closed 'org-agenda-get-progress)
4906 (defun org-agenda-get-progress ()
4907 "Return the logged TODO entries for agenda display."
4908 (let* ((props (list 'mouse-face 'highlight
4909 'org-not-done-regexp org-not-done-regexp
4910 'org-todo-regexp org-todo-regexp
4911 'org-complex-heading-regexp org-complex-heading-regexp
4912 'help-echo
4913 (format "mouse-2 or RET jump to org file %s"
4914 (abbreviate-file-name buffer-file-name))))
4915 (items (if (consp org-agenda-show-log)
4916 org-agenda-show-log
4917 (if (eq org-agenda-show-log 'clockcheck)
4918 '(clock)
4919 org-agenda-log-mode-items)))
4920 (parts
4921 (delq nil
4922 (list
4923 (if (memq 'closed items) (concat "\\<" org-closed-string))
4924 (if (memq 'clock items) (concat "\\<" org-clock-string))
4925 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4926 (parts-re (if parts (mapconcat 'identity parts "\\|")
4927 (error "`org-agenda-log-mode-items' is empty")))
4928 (regexp (concat
4929 "\\(" parts-re "\\)"
4930 " *\\["
4931 (regexp-quote
4932 (substring
4933 (format-time-string
4934 (car org-time-stamp-formats)
4935 (apply 'encode-time ; DATE bound by calendar
4936 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4937 1 11))))
4938 (org-agenda-search-headline-for-time nil)
4939 marker hdmarker priority category tags closedp statep clockp state
4940 ee txt extra timestr rest clocked)
4941 (goto-char (point-min))
4942 (while (re-search-forward regexp nil t)
4943 (catch :skip
4944 (org-agenda-skip)
4945 (setq marker (org-agenda-new-marker (match-beginning 0))
4946 closedp (equal (match-string 1) org-closed-string)
4947 statep (equal (string-to-char (match-string 1)) ?-)
4948 clockp (not (or closedp statep))
4949 state (and statep (match-string 2))
4950 category (org-get-category (match-beginning 0))
4951 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4953 (when (string-match "\\]" timestr)
4954 ;; substring should only run to end of time stamp
4955 (setq rest (substring timestr (match-end 0))
4956 timestr (substring timestr 0 (match-end 0)))
4957 (if (and (not closedp) (not statep)
4958 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4959 (progn (setq timestr (concat (substring timestr 0 -1)
4960 "-" (match-string 1 rest) "]"))
4961 (setq clocked (match-string 2 rest)))
4962 (setq clocked "-")))
4963 (save-excursion
4964 (setq extra
4965 (cond
4966 ((not org-agenda-log-mode-add-notes) nil)
4967 (statep
4968 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4969 (match-string 1)))
4970 (clockp
4971 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4972 (match-string 1)))))
4973 (if (not (re-search-backward org-outline-regexp-bol nil t))
4974 (setq txt org-agenda-no-heading-message)
4975 (goto-char (match-beginning 0))
4976 (setq hdmarker (org-agenda-new-marker)
4977 tags (org-get-tags-at))
4978 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4979 (setq txt (match-string 1))
4980 (when extra
4981 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4982 (setq txt (concat (substring txt 0 (match-beginning 1))
4983 " - " extra " " (match-string 2 txt)))
4984 (setq txt (concat txt " - " extra))))
4985 (setq txt (org-format-agenda-item
4986 (cond
4987 (closedp "Closed: ")
4988 (statep (concat "State: (" state ")"))
4989 (t (concat "Clocked: (" clocked ")")))
4990 txt category tags timestr)))
4991 (setq priority 100000)
4992 (org-add-props txt props
4993 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4994 'priority priority 'org-category category
4995 'type "closed" 'date date
4996 'undone-face 'org-warning 'done-face 'org-agenda-done)
4997 (push txt ee))
4998 (goto-char (point-at-eol))))
4999 (nreverse ee)))
5001 (defun org-agenda-show-clocking-issues ()
5002 "Add overlays, showing issues with clocking.
5003 See also the user option `org-agenda-clock-consistency-checks'."
5004 (interactive)
5005 (let* ((pl org-agenda-clock-consistency-checks)
5006 (re (concat "^[ \t]*"
5007 org-clock-string
5008 "[ \t]+"
5009 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5010 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5011 (tlstart 0.)
5012 (tlend 0.)
5013 (maxtime (org-hh:mm-string-to-minutes
5014 (or (plist-get pl :max-duration) "24:00")))
5015 (mintime (org-hh:mm-string-to-minutes
5016 (or (plist-get pl :min-duration) 0)))
5017 (maxgap (org-hh:mm-string-to-minutes
5018 ;; default 30:00 means never complain
5019 (or (plist-get pl :max-gap) "30:00")))
5020 (gapok (mapcar 'org-hh:mm-string-to-minutes
5021 (plist-get pl :gap-ok-around)))
5022 (def-face (or (plist-get pl :default-face)
5023 '((:background "DarkRed") (:foreground "white"))))
5024 issue face m te ts dt ov)
5025 (goto-char (point-min))
5026 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5027 (setq issue nil face def-face)
5028 (catch 'next
5029 (setq m (org-get-at-bol 'org-marker)
5030 te nil ts nil)
5031 (unless (and m (markerp m))
5032 (setq issue "No valid clock line") (throw 'next t))
5033 (org-with-point-at m
5034 (save-excursion
5035 (goto-char (point-at-bol))
5036 (unless (looking-at re)
5037 (error "No valid Clock line")
5038 (throw 'next t))
5039 (unless (match-end 3)
5040 (setq issue "No end time"
5041 face (or (plist-get pl :no-end-time-face) face))
5042 (throw 'next t))
5043 (setq ts (match-string 1)
5044 te (match-string 3)
5045 ts (org-float-time
5046 (apply 'encode-time (org-parse-time-string ts)))
5047 te (org-float-time
5048 (apply 'encode-time (org-parse-time-string te)))
5049 dt (- te ts))))
5050 (cond
5051 ((> dt (* 60 maxtime))
5052 ;; a very long clocking chunk
5053 (setq issue (format "Clocking interval is very long: %s"
5054 (org-minutes-to-hh:mm-string
5055 (floor (/ (float dt) 60.))))
5056 face (or (plist-get pl :long-face) face)))
5057 ((< dt (* 60 mintime))
5058 ;; a very short clocking chunk
5059 (setq issue (format "Clocking interval is very short: %s"
5060 (org-minutes-to-hh:mm-string
5061 (floor (/ (float dt) 60.))))
5062 face (or (plist-get pl :short-face) face)))
5063 ((and (> tlend 0) (< ts tlend))
5064 ;; Two clock entries are overlapping
5065 (setq issue (format "Clocking overlap: %d minutes"
5066 (/ (- tlend ts) 60))
5067 face (or (plist-get pl :overlap-face) face)))
5068 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5069 ;; There is a gap, lets see if we need to report it
5070 (unless (org-agenda-check-clock-gap tlend ts gapok)
5071 (setq issue (format "Clocking gap: %d minutes"
5072 (/ (- ts tlend) 60))
5073 face (or (plist-get pl :gap-face) face))))
5074 (t nil)))
5075 (setq tlend (or te tlend) tlstart (or ts tlstart))
5076 (when issue
5077 ;; OK, there was some issue, add an overlay to show the issue
5078 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5079 (overlay-put ov 'before-string
5080 (concat
5081 (org-add-props
5082 (format "%-43s" (concat " " issue))
5084 'face face)
5085 "\n"))
5086 (overlay-put ov 'evaporate t)))))
5088 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5089 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5090 (catch 'exit
5091 (unless ok-list
5092 ;; there are no OK times for gaps...
5093 (throw 'exit nil))
5094 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5095 ;; This is more than 24 hours, so it is OK.
5096 ;; because we have at least one OK time, that must be in the
5097 ;; 24 hour interval.
5098 (throw 'exit t))
5099 ;; We have a shorter gap.
5100 ;; Now we have to get the minute of the day when these times are
5101 (let* ((t1dec (decode-time (seconds-to-time t1)))
5102 (t2dec (decode-time (seconds-to-time t2)))
5103 ;; compute the minute on the day
5104 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5105 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5106 (when (< min2 min1)
5107 ;; if min2 is smaller than min1, this means it is on the next day.
5108 ;; Wrap it to after midnight.
5109 (setq min2 (+ min2 1440)))
5110 ;; Now check if any of the OK times is in the gap
5111 (mapc (lambda (x)
5112 ;; Wrap the time to after midnight if necessary
5113 (if (< x min1) (setq x (+ x 1440)))
5114 ;; Check if in interval
5115 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5116 ok-list)
5117 ;; Nope, this gap is not OK
5118 nil)))
5120 (defun org-agenda-get-deadlines ()
5121 "Return the deadline information for agenda display."
5122 (let* ((props (list 'mouse-face 'highlight
5123 'org-not-done-regexp org-not-done-regexp
5124 'org-todo-regexp org-todo-regexp
5125 'org-complex-heading-regexp org-complex-heading-regexp
5126 'help-echo
5127 (format "mouse-2 or RET jump to org file %s"
5128 (abbreviate-file-name buffer-file-name))))
5129 (regexp org-deadline-time-regexp)
5130 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5131 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5132 d2 diff dfrac wdays pos pos1 category tags
5133 suppress-prewarning
5134 ee txt head face s todo-state show-all upcomingp donep timestr)
5135 (goto-char (point-min))
5136 (while (re-search-forward regexp nil t)
5137 (setq suppress-prewarning nil)
5138 (catch :skip
5139 (org-agenda-skip)
5140 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5141 (save-match-data
5142 (string-match org-scheduled-time-regexp
5143 (buffer-substring (point-at-bol)
5144 (point-at-eol)))))
5145 (setq suppress-prewarning
5146 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5147 org-agenda-skip-deadline-prewarning-if-scheduled
5148 0)))
5149 (setq s (match-string 1)
5150 txt nil
5151 pos (1- (match-beginning 1))
5152 todo-state (save-match-data (org-get-todo-state))
5153 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5154 (member todo-state
5155 org-agenda-repeating-timestamp-show-all))
5156 d2 (org-time-string-to-absolute
5157 (match-string 1) d1 'past show-all)
5158 diff (- d2 d1)
5159 wdays (if suppress-prewarning
5160 (let ((org-deadline-warning-days suppress-prewarning))
5161 (org-get-wdays s))
5162 (org-get-wdays s))
5163 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5164 upcomingp (and todayp (> diff 0)))
5165 ;; When to show a deadline in the calendar:
5166 ;; If the expiration is within wdays warning time.
5167 ;; Past-due deadlines are only shown on the current date
5168 (if (and (or (and (<= diff wdays)
5169 (and todayp (not org-agenda-only-exact-dates)))
5170 (= diff 0)))
5171 (save-excursion
5172 ;; (setq todo-state (org-get-todo-state))
5173 (setq donep (member todo-state org-done-keywords))
5174 (if (and donep
5175 (or org-agenda-skip-deadline-if-done
5176 (not (= diff 0))))
5177 (setq txt nil)
5178 (setq category (org-get-category))
5179 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5180 (setq txt org-agenda-no-heading-message)
5181 (goto-char (match-end 0))
5182 (setq pos1 (match-beginning 0))
5183 (setq tags (org-get-tags-at pos1))
5184 (setq head (buffer-substring-no-properties
5185 (point)
5186 (progn (skip-chars-forward "^\r\n")
5187 (point))))
5188 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5189 (setq timestr
5190 (concat (substring s (match-beginning 1)) " "))
5191 (setq timestr 'time))
5192 (setq txt (org-format-agenda-item
5193 (if (= diff 0)
5194 (car org-agenda-deadline-leaders)
5195 (if (functionp
5196 (nth 1 org-agenda-deadline-leaders))
5197 (funcall
5198 (nth 1 org-agenda-deadline-leaders)
5199 diff date)
5200 (format (nth 1 org-agenda-deadline-leaders)
5201 diff)))
5202 head category tags
5203 (if (not (= diff 0)) nil timestr)))))
5204 (when txt
5205 (setq face (org-agenda-deadline-face dfrac))
5206 (org-add-props txt props
5207 'org-marker (org-agenda-new-marker pos)
5208 'org-hd-marker (org-agenda-new-marker pos1)
5209 'priority (+ (- diff)
5210 (org-get-priority txt))
5211 'org-category category
5212 'todo-state todo-state
5213 'type (if upcomingp "upcoming-deadline" "deadline")
5214 'date (if upcomingp date d2)
5215 'face (if donep 'org-agenda-done face)
5216 'undone-face face 'done-face 'org-agenda-done)
5217 (push txt ee))))))
5218 (nreverse ee)))
5220 (defun org-agenda-deadline-face (fraction)
5221 "Return the face to displaying a deadline item.
5222 FRACTION is what fraction of the head-warning time has passed."
5223 (let ((faces org-agenda-deadline-faces) f)
5224 (catch 'exit
5225 (while (setq f (pop faces))
5226 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5228 (defun org-agenda-get-scheduled (&optional deadline-results)
5229 "Return the scheduled information for agenda display."
5230 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5231 'org-todo-regexp org-todo-regexp
5232 'org-complex-heading-regexp org-complex-heading-regexp
5233 'done-face 'org-agenda-done
5234 'mouse-face 'highlight
5235 'help-echo
5236 (format "mouse-2 or RET jump to org file %s"
5237 (abbreviate-file-name buffer-file-name))))
5238 (regexp org-scheduled-time-regexp)
5239 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5240 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5242 (deadline-position-alist
5243 (mapcar (lambda (a) (and (setq mm (get-text-property
5244 0 'org-hd-marker a))
5245 (cons (marker-position mm) a)))
5246 deadline-results))
5247 d2 diff pos pos1 category tags donep
5248 ee txt head pastschedp todo-state face timestr s habitp show-all)
5249 (goto-char (point-min))
5250 (while (re-search-forward regexp nil t)
5251 (catch :skip
5252 (org-agenda-skip)
5253 (setq s (match-string 1)
5254 txt nil
5255 pos (1- (match-beginning 1))
5256 todo-state (save-match-data (org-get-todo-state))
5257 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5258 (member todo-state
5259 org-agenda-repeating-timestamp-show-all))
5260 d2 (org-time-string-to-absolute
5261 (match-string 1) d1 'past show-all)
5262 diff (- d2 d1))
5263 (setq pastschedp (and todayp (< diff 0)))
5264 ;; When to show a scheduled item in the calendar:
5265 ;; If it is on or past the date.
5266 (when (or (and (< diff 0)
5267 (< (abs diff) org-scheduled-past-days)
5268 (and todayp (not org-agenda-only-exact-dates)))
5269 (= diff 0))
5270 (save-excursion
5271 (setq donep (member todo-state org-done-keywords))
5272 (if (and donep
5273 (or org-agenda-skip-scheduled-if-done
5274 (not (= diff 0))
5275 (and (functionp 'org-is-habit-p)
5276 (org-is-habit-p))))
5277 (setq txt nil)
5278 (setq habitp (and (functionp 'org-is-habit-p)
5279 (org-is-habit-p)))
5280 (setq category (org-get-category))
5281 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5282 (setq txt org-agenda-no-heading-message)
5283 (goto-char (match-end 0))
5284 (setq pos1 (match-beginning 0))
5285 (if habitp
5286 (if (or (not org-habit-show-habits)
5287 (and (not todayp)
5288 org-habit-show-habits-only-for-today))
5289 (throw :skip nil))
5290 (if (and
5291 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5292 (and org-agenda-skip-scheduled-if-deadline-is-shown
5293 pastschedp))
5294 (setq mm (assoc pos1 deadline-position-alist)))
5295 (throw :skip nil)))
5296 (setq tags (org-get-tags-at))
5297 (setq head (buffer-substring-no-properties
5298 (point)
5299 (progn (skip-chars-forward "^\r\n") (point))))
5300 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5301 (setq timestr
5302 (concat (substring s (match-beginning 1)) " "))
5303 (setq timestr 'time))
5304 (setq txt (org-format-agenda-item
5305 (if (= diff 0)
5306 (car org-agenda-scheduled-leaders)
5307 (format (nth 1 org-agenda-scheduled-leaders)
5308 (- 1 diff)))
5309 head category tags
5310 (if (not (= diff 0)) nil timestr)
5311 nil habitp))))
5312 (when txt
5313 (setq face
5314 (cond
5315 ((and (not habitp) pastschedp)
5316 'org-scheduled-previously)
5317 (todayp 'org-scheduled-today)
5318 (t 'org-scheduled))
5319 habitp (and habitp (org-habit-parse-todo)))
5320 (org-add-props txt props
5321 'undone-face face
5322 'face (if donep 'org-agenda-done face)
5323 'org-marker (org-agenda-new-marker pos)
5324 'org-hd-marker (org-agenda-new-marker pos1)
5325 'type (if pastschedp "past-scheduled" "scheduled")
5326 'date (if pastschedp d2 date)
5327 'priority (if habitp
5328 (org-habit-get-priority habitp)
5329 (+ 94 (- 5 diff) (org-get-priority txt)))
5330 'org-category category
5331 'org-habit-p habitp
5332 'todo-state todo-state)
5333 (push txt ee))))))
5334 (nreverse ee)))
5336 (defun org-agenda-get-blocks ()
5337 "Return the date-range information for agenda display."
5338 (let* ((props (list 'face nil
5339 'org-not-done-regexp org-not-done-regexp
5340 'org-todo-regexp org-todo-regexp
5341 'org-complex-heading-regexp org-complex-heading-regexp
5342 'mouse-face 'highlight
5343 'help-echo
5344 (format "mouse-2 or RET jump to org file %s"
5345 (abbreviate-file-name buffer-file-name))))
5346 (regexp org-tr-regexp)
5347 (d0 (calendar-absolute-from-gregorian date))
5348 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5349 head donep)
5350 (goto-char (point-min))
5351 (while (re-search-forward regexp nil t)
5352 (catch :skip
5353 (org-agenda-skip)
5354 (setq pos (point))
5355 (let ((start-time (match-string 1))
5356 (end-time (match-string 2)))
5357 (setq s1 (match-string 1)
5358 s2 (match-string 2)
5359 d1 (time-to-days (org-time-string-to-time s1))
5360 d2 (time-to-days (org-time-string-to-time s2)))
5361 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5362 ;; Only allow days between the limits, because the normal
5363 ;; date stamps will catch the limits.
5364 (save-excursion
5365 (setq todo-state (org-get-todo-state))
5366 (setq donep (member todo-state org-done-keywords))
5367 (if (and donep org-agenda-skip-timestamp-if-done)
5368 (throw :skip t))
5369 (setq marker (org-agenda-new-marker (point)))
5370 (setq category (org-get-category))
5371 (if (not (re-search-backward org-outline-regexp-bol nil t))
5372 (setq txt org-agenda-no-heading-message)
5373 (goto-char (match-beginning 0))
5374 (setq hdmarker (org-agenda-new-marker (point)))
5375 (setq tags (org-get-tags-at))
5376 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5377 (setq head (match-string 1))
5378 (let ((remove-re
5379 (if org-agenda-remove-timeranges-from-blocks
5380 (concat
5381 "<" (regexp-quote s1) ".*?>"
5382 "--"
5383 "<" (regexp-quote s2) ".*?>")
5384 nil)))
5385 (setq txt (org-format-agenda-item
5386 (format
5387 (nth (if (= d1 d2) 0 1)
5388 org-agenda-timerange-leaders)
5389 (1+ (- d0 d1)) (1+ (- d2 d1)))
5390 head category tags
5391 (cond ((= d1 d0)
5392 (concat "<" start-time ">"))
5393 ((= d2 d0)
5394 (concat "<" end-time ">"))
5395 (t nil))
5396 remove-re))))
5397 (org-add-props txt props
5398 'org-marker marker 'org-hd-marker hdmarker
5399 'type "block" 'date date
5400 'todo-state todo-state
5401 'priority (org-get-priority txt) 'org-category category)
5402 (push txt ee))))
5403 (goto-char pos)))
5404 ;; Sort the entries by expiration date.
5405 (nreverse ee)))
5407 ;;; Agenda presentation and sorting
5409 (defvar org-prefix-has-time nil
5410 "A flag, set by `org-compile-prefix-format'.
5411 The flag is set if the currently compiled format contains a `%t'.")
5412 (defvar org-prefix-has-tag nil
5413 "A flag, set by `org-compile-prefix-format'.
5414 The flag is set if the currently compiled format contains a `%T'.")
5415 (defvar org-prefix-has-effort nil
5416 "A flag, set by `org-compile-prefix-format'.
5417 The flag is set if the currently compiled format contains a `%e'.")
5418 (defvar org-prefix-category-length nil
5419 "Used by `org-compile-prefix-format' to remember the category field width.")
5420 (defvar org-prefix-category-max-length nil
5421 "Used by `org-compile-prefix-format' to remember the category field width.")
5423 (defun org-agenda-get-category-icon (category)
5424 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5425 (dolist (entry org-agenda-category-icon-alist)
5426 (when (org-string-match-p (car entry) category)
5427 (if (listp (cadr entry))
5428 (return (cadr entry))
5429 (return (apply 'create-image (cdr entry)))))))
5431 (defun org-format-agenda-item (extra txt &optional category tags dotime
5432 remove-re habitp)
5433 "Format TXT to be inserted into the agenda buffer.
5434 In particular, it adds the prefix and corresponding text properties. EXTRA
5435 must be a string and replaces the `%s' specifier in the prefix format.
5436 CATEGORY (string, symbol or nil) may be used to overrule the default
5437 category taken from local variable or file name. It will replace the `%c'
5438 specifier in the format. DOTIME, when non-nil, indicates that a
5439 time-of-day should be extracted from TXT for sorting of this entry, and for
5440 the `%t' specifier in the format. When DOTIME is a string, this string is
5441 searched for a time before TXT is. TAGS can be the tags of the headline.
5442 Any match of REMOVE-RE will be removed from TXT."
5443 (save-match-data
5444 ;; Diary entries sometimes have extra whitespace at the beginning
5445 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5447 ;; Fix the tags part in txt
5448 (setq txt (org-agenda-fix-displayed-tags
5449 txt tags
5450 org-agenda-show-inherited-tags
5451 org-agenda-hide-tags-regexp))
5452 (let* ((category (or category
5453 (if (stringp org-category)
5454 org-category
5455 (and org-category (symbol-name org-category)))
5456 (if buffer-file-name
5457 (file-name-sans-extension
5458 (file-name-nondirectory buffer-file-name))
5459 "")))
5460 (category-icon (org-agenda-get-category-icon category))
5461 (category-icon (if category-icon
5462 (propertize " " 'display category-icon)
5463 ""))
5464 ;; time, tag, effort are needed for the eval of the prefix format
5465 (tag (if tags (nth (1- (length tags)) tags) ""))
5466 time effort neffort
5467 (ts (if dotime (concat
5468 (if (stringp dotime) dotime "")
5469 (and org-agenda-search-headline-for-time txt))))
5470 (time-of-day (and dotime (org-get-time-of-day ts)))
5471 stamp plain s0 s1 s2 rtn srp l
5472 duration thecategory)
5473 (and (org-mode-p) buffer-file-name
5474 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5475 (when (and dotime time-of-day)
5476 ;; Extract starting and ending time and move them to prefix
5477 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5478 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5479 (setq s0 (match-string 0 ts)
5480 srp (and stamp (match-end 3))
5481 s1 (match-string (if plain 1 2) ts)
5482 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5484 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5485 ;; them, we might want to remove them there to avoid duplication.
5486 ;; The user can turn this off with a variable.
5487 (if (and org-prefix-has-time
5488 org-agenda-remove-times-when-in-prefix (or stamp plain)
5489 (string-match (concat (regexp-quote s0) " *") txt)
5490 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5491 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5492 (= (match-beginning 0) 0)
5494 (setq txt (replace-match "" nil nil txt))))
5495 ;; Normalize the time(s) to 24 hour
5496 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5497 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5499 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5500 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5501 (setq s2
5502 (org-minutes-to-hh:mm-string
5503 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5505 ;; Compute the duration
5506 (when s2
5507 (setq duration (- (org-hh:mm-string-to-minutes s2)
5508 (org-hh:mm-string-to-minutes s1)))))
5510 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5511 txt)
5512 ;; Tags are in the string
5513 (if (or (eq org-agenda-remove-tags t)
5514 (and org-agenda-remove-tags
5515 org-prefix-has-tag))
5516 (setq txt (replace-match "" t t txt))
5517 (setq txt (replace-match
5518 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5519 (match-string 2 txt))
5520 t t txt))))
5521 (when (org-mode-p)
5522 (setq effort
5523 (condition-case nil
5524 (org-get-effort
5525 (or (get-text-property 0 'org-hd-marker txt)
5526 (get-text-property 0 'org-marker txt)))
5527 (error nil)))
5528 (when effort
5529 (setq neffort (org-duration-string-to-minutes effort)
5530 effort (setq effort (concat "[" effort "]")))))
5531 ;; prevent erroring out with %e format when there is no effort
5532 (or effort (setq effort ""))
5534 (when remove-re
5535 (while (string-match remove-re txt)
5536 (setq txt (replace-match "" t t txt))))
5538 ;; Set org-heading property on `txt' to mark the start of the
5539 ;; heading.
5540 (add-text-properties 0 (length txt) '(org-heading t) txt)
5542 ;; Prepare the variables needed in the eval of the compiled format
5543 (setq time (cond (s2 (concat
5544 (org-agenda-time-of-day-to-ampm-maybe s1)
5545 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5546 (if org-agenda-timegrid-use-ampm " ")))
5547 (s1 (concat
5548 (org-agenda-time-of-day-to-ampm-maybe s1)
5549 (if org-agenda-timegrid-use-ampm
5550 "........ "
5551 "......")))
5552 (t ""))
5553 extra (or (and (not habitp) extra) "")
5554 category (if (symbolp category) (symbol-name category) category)
5555 thecategory (copy-sequence category))
5556 (if (string-match org-bracket-link-regexp category)
5557 (progn
5558 (setq l (if (match-end 3)
5559 (- (match-end 3) (match-beginning 3))
5560 (- (match-end 1) (match-beginning 1))))
5561 (when (< l (or org-prefix-category-length 0))
5562 (setq category (copy-sequence category))
5563 (org-add-props category nil
5564 'extra-space (make-string
5565 (- org-prefix-category-length l 1) ?\ ))))
5566 (if (and org-prefix-category-max-length
5567 (>= (length category) org-prefix-category-max-length))
5568 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5569 ;; Evaluate the compiled format
5570 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5572 ;; And finally add the text properties
5573 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5574 (org-add-props rtn nil
5575 'org-category (if thecategory (downcase thecategory) category)
5576 'tags (mapcar 'org-downcase-keep-props tags)
5577 'org-highest-priority org-highest-priority
5578 'org-lowest-priority org-lowest-priority
5579 'time-of-day time-of-day
5580 'duration duration
5581 'effort effort
5582 'effort-minutes neffort
5583 'txt txt
5584 'time time
5585 'extra extra
5586 'format org-prefix-format-compiled
5587 'dotime dotime))))
5589 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5590 "Remove tags string from TXT, and add a modified list of tags.
5591 The modified list may contain inherited tags, and tags matched by
5592 `org-agenda-hide-tags-regexp' will be removed."
5593 (when (or add-inherited hide-re)
5594 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5595 (setq txt (substring txt 0 (match-beginning 0))))
5596 (setq tags
5597 (delq nil
5598 (mapcar (lambda (tg)
5599 (if (or (and hide-re (string-match hide-re tg))
5600 (and (not add-inherited)
5601 (get-text-property 0 'inherited tg)))
5603 tg))
5604 tags)))
5605 (when tags
5606 (let ((have-i (get-text-property 0 'inherited (car tags)))
5608 (setq txt (concat txt " :"
5609 (mapconcat
5610 (lambda (x)
5611 (setq i (get-text-property 0 'inherited x))
5612 (if (and have-i (not i))
5613 (progn
5614 (setq have-i nil)
5615 (concat ":" x))
5617 tags ":")
5618 (if have-i "::" ":"))))))
5619 txt)
5621 (defun org-downcase-keep-props (s)
5622 (let ((props (text-properties-at 0 s)))
5623 (setq s (downcase s))
5624 (add-text-properties 0 (length s) props s)
5627 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5628 (defvar org-agenda-sorting-strategy-selected nil)
5630 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5631 (catch 'exit
5632 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5633 ((and todayp (member 'today (car org-agenda-time-grid))))
5634 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5635 ((member 'weekly (car org-agenda-time-grid)))
5636 (t (throw 'exit list)))
5637 (let* ((have (delq nil (mapcar
5638 (lambda (x) (get-text-property 1 'time-of-day x))
5639 list)))
5640 (string (nth 1 org-agenda-time-grid))
5641 (gridtimes (nth 2 org-agenda-time-grid))
5642 (req (car org-agenda-time-grid))
5643 (remove (member 'remove-match req))
5644 new time)
5645 (if (and (member 'require-timed req) (not have))
5646 ;; don't show empty grid
5647 (throw 'exit list))
5648 (while (setq time (pop gridtimes))
5649 (unless (and remove (member time have))
5650 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5651 (push (org-format-agenda-item
5652 nil string "" nil
5653 (concat (substring time 0 -2) ":" (substring time -2)))
5654 new)
5655 (put-text-property
5656 2 (length (car new)) 'face 'org-time-grid (car new))))
5657 (when (and todayp org-agenda-show-current-time-in-grid)
5658 (push (org-format-agenda-item
5660 org-agenda-current-time-string
5661 "" nil
5662 (format-time-string "%H:%M "))
5663 new)
5664 (put-text-property
5665 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5667 (if (member 'time-up org-agenda-sorting-strategy-selected)
5668 (append new list)
5669 (append list new)))))
5671 (defun org-compile-prefix-format (key)
5672 "Compile the prefix format into a Lisp form that can be evaluated.
5673 The resulting form is returned and stored in the variable
5674 `org-prefix-format-compiled'."
5675 (setq org-prefix-has-time nil org-prefix-has-tag nil
5676 org-prefix-category-length nil
5677 org-prefix-has-effort nil)
5678 (let ((s (cond
5679 ((stringp org-agenda-prefix-format)
5680 org-agenda-prefix-format)
5681 ((assq key org-agenda-prefix-format)
5682 (cdr (assq key org-agenda-prefix-format)))
5683 (t " %-12:c%?-12t% s")))
5684 (start 0)
5685 varform vars var e c f opt)
5686 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5687 s start)
5688 (setq var (or (cdr (assoc (match-string 4 s)
5689 '(("c" . category) ("t" . time) ("s" . extra)
5690 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5691 'eval)
5692 c (or (match-string 3 s) "")
5693 opt (match-beginning 1)
5694 start (1+ (match-beginning 0)))
5695 (if (equal var 'time) (setq org-prefix-has-time t))
5696 (if (equal var 'tag) (setq org-prefix-has-tag t))
5697 (if (equal var 'effort) (setq org-prefix-has-effort t))
5698 (setq f (concat "%" (match-string 2 s) "s"))
5699 (when (equal var 'category)
5700 (setq org-prefix-category-length
5701 (floor (abs (string-to-number (match-string 2 s)))))
5702 (setq org-prefix-category-max-length
5703 (let ((x (match-string 2 s)))
5704 (save-match-data
5705 (if (string-match "\\.[0-9]+" x)
5706 (string-to-number (substring (match-string 0 x) 1)))))))
5707 (if (eq var 'eval)
5708 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5709 (if opt
5710 (setq varform
5711 `(if (equal "" ,var)
5713 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5714 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5715 (setq s (replace-match "%s" t nil s))
5716 (push varform vars))
5717 (setq vars (nreverse vars))
5718 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5720 (defun org-set-sorting-strategy (key)
5721 (if (symbolp (car org-agenda-sorting-strategy))
5722 ;; the old format
5723 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5724 (setq org-agenda-sorting-strategy-selected
5725 (or (cdr (assq key org-agenda-sorting-strategy))
5726 (cdr (assq 'agenda org-agenda-sorting-strategy))
5727 '(time-up category-keep priority-down)))))
5729 (defun org-get-time-of-day (s &optional string mod24)
5730 "Check string S for a time of day.
5731 If found, return it as a military time number between 0 and 2400.
5732 If not found, return nil.
5733 The optional STRING argument forces conversion into a 5 character wide string
5734 HH:MM."
5735 (save-match-data
5736 (when
5737 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5738 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5739 (let* ((h (string-to-number (match-string 1 s)))
5740 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5741 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5742 (am-p (equal ampm "am"))
5743 (h1 (cond ((not ampm) h)
5744 ((= h 12) (if am-p 0 12))
5745 (t (+ h (if am-p 0 12)))))
5746 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5747 (mod h1 24) h1))
5748 (t0 (+ (* 100 h2) m))
5749 (t1 (concat (if (>= h1 24) "+" " ")
5750 (if (and org-agenda-time-leading-zero
5751 (< t0 1000)) "0" "")
5752 (if (< t0 100) "0" "")
5753 (if (< t0 10) "0" "")
5754 (int-to-string t0))))
5755 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5757 (defvar org-agenda-before-sorting-filter-function nil
5758 "Function to be applied to agenda items prior to sorting.
5759 Prior to sorting also means just before they are inserted into the agenda.
5761 To aid sorting, you may revisit the original entries and add more text
5762 properties which will later be used by the sorting functions.
5764 The function should take a string argument, an agenda line.
5765 It has access to the text properties in that line, which contain among
5766 other things, the property `org-hd-marker' that points to the entry
5767 where the line comes from. Note that not all lines going into the agenda
5768 have this property, only most.
5770 The function should return the modified string. It is probably best
5771 to ONLY change text properties.
5773 You can also use this function as a filter, by returning nil for lines
5774 you don't want to have in the agenda at all. For this application, you
5775 could bind the variable in the options section of a custom command.")
5777 (defun org-finalize-agenda-entries (list &optional nosort)
5778 "Sort and concatenate the agenda items."
5779 (setq list (mapcar 'org-agenda-highlight-todo list))
5780 (if nosort
5781 list
5782 (when org-agenda-before-sorting-filter-function
5783 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5784 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5786 (defun org-agenda-highlight-todo (x)
5787 (let ((org-done-keywords org-done-keywords-for-agenda)
5788 (case-fold-search nil)
5790 (if (eq x 'line)
5791 (save-excursion
5792 (beginning-of-line 1)
5793 (setq re (org-get-at-bol 'org-todo-regexp))
5794 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5795 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5796 (add-text-properties (match-beginning 0) (match-end 1)
5797 (list 'face (org-get-todo-face 1)))
5798 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5799 (delete-region (match-beginning 1) (1- (match-end 0)))
5800 (goto-char (match-beginning 1))
5801 (insert (format org-agenda-todo-keyword-format s)))))
5802 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5803 (setq re (get-text-property 0 'org-todo-regexp x))
5804 (when (and re
5805 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5806 x (or pl 0)) pl))
5807 (add-text-properties
5808 (or (match-end 1) (match-end 0)) (match-end 0)
5809 (list 'face (org-get-todo-face (match-string 2 x)))
5811 (when (match-end 1)
5812 (setq x (concat (substring x 0 (match-end 1))
5813 (format org-agenda-todo-keyword-format
5814 (match-string 2 x))
5815 (org-add-props " " (text-properties-at 0 x))
5816 (substring x (match-end 3)))))))
5817 x)))
5819 (defsubst org-cmp-priority (a b)
5820 "Compare the priorities of string A and B."
5821 (let ((pa (or (get-text-property 1 'priority a) 0))
5822 (pb (or (get-text-property 1 'priority b) 0)))
5823 (cond ((> pa pb) +1)
5824 ((< pa pb) -1)
5825 (t nil))))
5827 (defsubst org-cmp-effort (a b)
5828 "Compare the effort values of string A and B."
5829 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5830 (ea (or (get-text-property 1 'effort-minutes a) def))
5831 (eb (or (get-text-property 1 'effort-minutes b) def)))
5832 (cond ((> ea eb) +1)
5833 ((< ea eb) -1)
5834 (t nil))))
5836 (defsubst org-cmp-category (a b)
5837 "Compare the string values of categories of strings A and B."
5838 (let ((ca (or (get-text-property 1 'org-category a) ""))
5839 (cb (or (get-text-property 1 'org-category b) "")))
5840 (cond ((string-lessp ca cb) -1)
5841 ((string-lessp cb ca) +1)
5842 (t nil))))
5844 (defsubst org-cmp-todo-state (a b)
5845 "Compare the todo states of strings A and B."
5846 (let* ((ma (or (get-text-property 1 'org-marker a)
5847 (get-text-property 1 'org-hd-marker a)))
5848 (mb (or (get-text-property 1 'org-marker b)
5849 (get-text-property 1 'org-hd-marker b)))
5850 (fa (and ma (marker-buffer ma)))
5851 (fb (and mb (marker-buffer mb)))
5852 (todo-kwds
5853 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5854 (and fb (with-current-buffer fb org-todo-keywords-1))))
5855 (ta (or (get-text-property 1 'todo-state a) ""))
5856 (tb (or (get-text-property 1 'todo-state b) ""))
5857 (la (- (length (member ta todo-kwds))))
5858 (lb (- (length (member tb todo-kwds))))
5859 (donepa (member ta org-done-keywords-for-agenda))
5860 (donepb (member tb org-done-keywords-for-agenda)))
5861 (cond ((and donepa (not donepb)) -1)
5862 ((and (not donepa) donepb) +1)
5863 ((< la lb) -1)
5864 ((< lb la) +1)
5865 (t nil))))
5867 (defsubst org-cmp-alpha (a b)
5868 "Compare the headlines, alphabetically."
5869 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5870 (plb (text-property-any 0 (length b) 'org-heading t b))
5871 (ta (and pla (substring a pla)))
5872 (tb (and plb (substring b plb))))
5873 (when pla
5874 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5875 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5876 (setq ta (substring ta (match-end 0))))
5877 (setq ta (downcase ta)))
5878 (when plb
5879 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5880 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5881 (setq tb (substring tb (match-end 0))))
5882 (setq tb (downcase tb)))
5883 (cond ((not ta) +1)
5884 ((not tb) -1)
5885 ((string-lessp ta tb) -1)
5886 ((string-lessp tb ta) +1)
5887 (t nil))))
5889 (defsubst org-cmp-tag (a b)
5890 "Compare the string values of the first tags of A and B."
5891 (let ((ta (car (last (get-text-property 1 'tags a))))
5892 (tb (car (last (get-text-property 1 'tags b)))))
5893 (cond ((not ta) +1)
5894 ((not tb) -1)
5895 ((string-lessp ta tb) -1)
5896 ((string-lessp tb ta) +1)
5897 (t nil))))
5899 (defsubst org-cmp-time (a b)
5900 "Compare the time-of-day values of strings A and B."
5901 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5902 (ta (or (get-text-property 1 'time-of-day a) def))
5903 (tb (or (get-text-property 1 'time-of-day b) def)))
5904 (cond ((< ta tb) -1)
5905 ((< tb ta) +1)
5906 (t nil))))
5908 (defsubst org-cmp-habit-p (a b)
5909 "Compare the todo states of strings A and B."
5910 (let ((ha (get-text-property 1 'org-habit-p a))
5911 (hb (get-text-property 1 'org-habit-p b)))
5912 (cond ((and ha (not hb)) -1)
5913 ((and (not ha) hb) +1)
5914 (t nil))))
5916 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5918 (defun org-entries-lessp (a b)
5919 "Predicate for sorting agenda entries."
5920 ;; The following variables will be used when the form is evaluated.
5921 ;; So even though the compiler complains, keep them.
5922 (let* ((ss org-agenda-sorting-strategy-selected)
5923 (time-up (and (org-em 'time-up 'time-down ss)
5924 (org-cmp-time a b)))
5925 (time-down (if time-up (- time-up) nil))
5926 (priority-up (and (org-em 'priority-up 'priority-down ss)
5927 (org-cmp-priority a b)))
5928 (priority-down (if priority-up (- priority-up) nil))
5929 (effort-up (and (org-em 'effort-up 'effort-down ss)
5930 (org-cmp-effort a b)))
5931 (effort-down (if effort-up (- effort-up) nil))
5932 (category-up (and (or (org-em 'category-up 'category-down ss)
5933 (memq 'category-keep ss))
5934 (org-cmp-category a b)))
5935 (category-down (if category-up (- category-up) nil))
5936 (category-keep (if category-up +1 nil))
5937 (tag-up (and (org-em 'tag-up 'tag-down ss)
5938 (org-cmp-tag a b)))
5939 (tag-down (if tag-up (- tag-up) nil))
5940 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5941 (org-cmp-todo-state a b)))
5942 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5943 (habit-up (and (org-em 'habit-up 'habit-down ss)
5944 (org-cmp-habit-p a b)))
5945 (habit-down (if habit-up (- habit-up) nil))
5946 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5947 (org-cmp-alpha a b)))
5948 (alpha-down (if alpha-up (- alpha-up) nil))
5949 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5950 user-defined-up user-defined-down)
5951 (if (and need-user-cmp org-agenda-cmp-user-defined
5952 (functionp org-agenda-cmp-user-defined))
5953 (setq user-defined-up
5954 (funcall org-agenda-cmp-user-defined a b)
5955 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5956 (cdr (assoc
5957 (eval (cons 'or org-agenda-sorting-strategy-selected))
5958 '((-1 . t) (1 . nil) (nil . nil))))))
5960 ;;; Agenda restriction lock
5962 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5963 "Overlay to mark the headline to which agenda commands are restricted.")
5964 (overlay-put org-agenda-restriction-lock-overlay
5965 'face 'org-agenda-restriction-lock)
5966 (overlay-put org-agenda-restriction-lock-overlay
5967 'help-echo "Agendas are currently limited to this subtree.")
5968 (org-detach-overlay org-agenda-restriction-lock-overlay)
5970 (defun org-agenda-set-restriction-lock (&optional type)
5971 "Set restriction lock for agenda, to current subtree or file.
5972 Restriction will be the file if TYPE is `file', or if type is the
5973 universal prefix '(4), or if the cursor is before the first headline
5974 in the file. Otherwise, restriction will be to the current subtree."
5975 (interactive "P")
5976 (and (equal type '(4)) (setq type 'file))
5977 (setq type (cond
5978 (type type)
5979 ((org-at-heading-p) 'subtree)
5980 ((condition-case nil (org-back-to-heading t) (error nil))
5981 'subtree)
5982 (t 'file)))
5983 (if (eq type 'subtree)
5984 (progn
5985 (setq org-agenda-restrict t)
5986 (setq org-agenda-overriding-restriction 'subtree)
5987 (put 'org-agenda-files 'org-restrict
5988 (list (buffer-file-name (buffer-base-buffer))))
5989 (org-back-to-heading t)
5990 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5991 (move-marker org-agenda-restrict-begin (point))
5992 (move-marker org-agenda-restrict-end
5993 (save-excursion (org-end-of-subtree t)))
5994 (message "Locking agenda restriction to subtree"))
5995 (put 'org-agenda-files 'org-restrict
5996 (list (buffer-file-name (buffer-base-buffer))))
5997 (setq org-agenda-restrict nil)
5998 (setq org-agenda-overriding-restriction 'file)
5999 (move-marker org-agenda-restrict-begin nil)
6000 (move-marker org-agenda-restrict-end nil)
6001 (message "Locking agenda restriction to file"))
6002 (setq current-prefix-arg nil)
6003 (org-agenda-maybe-redo))
6005 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6006 "Remove the agenda restriction lock."
6007 (interactive "P")
6008 (org-detach-overlay org-agenda-restriction-lock-overlay)
6009 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6010 (setq org-agenda-overriding-restriction nil)
6011 (setq org-agenda-restrict nil)
6012 (put 'org-agenda-files 'org-restrict nil)
6013 (move-marker org-agenda-restrict-begin nil)
6014 (move-marker org-agenda-restrict-end nil)
6015 (setq current-prefix-arg nil)
6016 (message "Agenda restriction lock removed")
6017 (or noupdate (org-agenda-maybe-redo)))
6019 (defun org-agenda-maybe-redo ()
6020 "If there is any window showing the agenda view, update it."
6021 (let ((w (get-buffer-window org-agenda-buffer-name t))
6022 (w0 (selected-window)))
6023 (when w
6024 (select-window w)
6025 (org-agenda-redo)
6026 (select-window w0)
6027 (if org-agenda-overriding-restriction
6028 (message "Agenda view shifted to new %s restriction"
6029 org-agenda-overriding-restriction)
6030 (message "Agenda restriction lock removed")))))
6032 ;;; Agenda commands
6034 (defun org-agenda-check-type (error &rest types)
6035 "Check if agenda buffer is of allowed type.
6036 If ERROR is non-nil, throw an error, otherwise just return nil."
6037 (if (memq org-agenda-type types)
6039 (if error
6040 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6041 nil)))
6043 (defun org-agenda-quit ()
6044 "Exit agenda by removing the window or the buffer."
6045 (interactive)
6046 (if org-agenda-columns-active
6047 (org-columns-quit)
6048 (let ((buf (current-buffer)))
6049 (if (eq org-agenda-window-setup 'other-frame)
6050 (progn
6051 (kill-buffer buf)
6052 (org-agenda-reset-markers)
6053 (org-columns-remove-overlays)
6054 (setq org-agenda-archives-mode nil)
6055 (delete-frame))
6056 (and (not (eq org-agenda-window-setup 'current-window))
6057 (not (one-window-p))
6058 (delete-window))
6059 (kill-buffer buf)
6060 (org-agenda-reset-markers)
6061 (org-columns-remove-overlays)
6062 (setq org-agenda-archives-mode nil)))
6063 ;; Maybe restore the pre-agenda window configuration.
6064 (and org-agenda-restore-windows-after-quit
6065 (not (eq org-agenda-window-setup 'other-frame))
6066 org-pre-agenda-window-conf
6067 (set-window-configuration org-pre-agenda-window-conf))))
6069 (defun org-agenda-exit ()
6070 "Exit agenda by removing the window or the buffer.
6071 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6072 Org-mode buffers visited directly by the user will not be touched."
6073 (interactive)
6074 (org-release-buffers org-agenda-new-buffers)
6075 (setq org-agenda-new-buffers nil)
6076 (org-agenda-quit))
6078 (defun org-agenda-execute (arg)
6079 "Execute another agenda command, keeping same window.
6080 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6081 in the agenda."
6082 (interactive "P")
6083 (let ((org-agenda-window-setup 'current-window))
6084 (org-agenda arg)))
6086 (defun org-agenda-redo ()
6087 "Rebuild Agenda.
6088 When this is the global TODO list, a prefix argument will be interpreted."
6089 (interactive)
6090 (let* ((org-agenda-keep-modes t)
6091 (filter org-agenda-filter)
6092 (preset (get 'org-agenda-filter :preset-filter))
6093 (org-agenda-filter-while-redo (or filter preset))
6094 (cols org-agenda-columns-active)
6095 (line (org-current-line))
6096 (window-line (- line (org-current-line (window-start))))
6097 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6098 (put 'org-agenda-filter :preset-filter nil)
6099 (and cols (org-columns-quit))
6100 (message "Rebuilding agenda buffer...")
6101 (org-let lprops '(eval org-agenda-redo-command))
6102 (setq org-agenda-undo-list nil
6103 org-agenda-pending-undo-list nil)
6104 (message "Rebuilding agenda buffer...done")
6105 (put 'org-agenda-filter :preset-filter preset)
6106 (and (or filter preset) (org-agenda-filter-apply filter))
6107 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6108 (org-goto-line line)
6109 (recenter window-line)))
6112 (defvar org-global-tags-completion-table nil)
6113 (defvar org-agenda-filter-form nil)
6114 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6115 "Keep only those lines in the agenda buffer that have a specific tag.
6116 The tag is selected with its fast selection letter, as configured.
6117 With prefix argument STRIP, remove all lines that do have the tag.
6118 A lisp caller can specify CHAR. NARROW means that the new tag should be
6119 used to narrow the search - the interactive user can also press `-' or `+'
6120 to switch to narrowing."
6121 (interactive "P")
6122 (let* ((alist org-tag-alist-for-agenda)
6123 (tag-chars (mapconcat
6124 (lambda (x) (if (and (not (symbolp (car x)))
6125 (cdr x))
6126 (char-to-string (cdr x))
6127 ""))
6128 alist ""))
6129 (efforts (org-split-string
6130 (or (cdr (assoc (concat org-effort-property "_ALL")
6131 org-global-properties))
6132 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6133 "")))
6134 (effort-op org-agenda-filter-effort-default-operator)
6135 (effort-prompt "")
6136 (inhibit-read-only t)
6137 (current org-agenda-filter)
6138 maybe-refresh a n tag)
6139 (unless char
6140 (message
6141 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6142 (if narrow "Narrow" "Filter") tag-chars
6143 (if org-agenda-auto-exclude-function "[RET], " ""))
6144 (setq char (read-char)))
6145 (when (member char '(?+ ?-))
6146 ;; Narrowing down
6147 (cond ((equal char ?-) (setq strip t narrow t))
6148 ((equal char ?+) (setq strip nil narrow t)))
6149 (message
6150 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6151 (setq char (read-char)))
6152 (when (member char '(?< ?> ?= ??))
6153 ;; An effort operator
6154 (setq effort-op (char-to-string char))
6155 (setq alist nil) ; to make sure it will be interpreted as effort.
6156 (unless (equal char ??)
6157 (loop for i from 0 to 9 do
6158 (setq effort-prompt
6159 (concat
6160 effort-prompt " ["
6161 (if (= i 9) "0" (int-to-string (1+ i)))
6162 "]" (nth i efforts))))
6163 (message "Effort%s: %s " effort-op effort-prompt)
6164 (setq char (read-char))
6165 (when (or (< char ?0) (> char ?9))
6166 (error "Need 1-9,0 to select effort" ))))
6167 (when (equal char ?\t)
6168 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6169 (org-set-local 'org-global-tags-completion-table
6170 (org-global-tags-completion-table)))
6171 (let ((completion-ignore-case t))
6172 (setq tag (org-icompleting-read
6173 "Tag: " org-global-tags-completion-table))))
6174 (cond
6175 ((equal char ?\r)
6176 (org-agenda-filter-by-tag-show-all)
6177 (when org-agenda-auto-exclude-function
6178 (setq org-agenda-filter '())
6179 (dolist (tag (org-agenda-get-represented-tags))
6180 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6181 (if modifier
6182 (push modifier org-agenda-filter))))
6183 (if (not (null org-agenda-filter))
6184 (org-agenda-filter-apply org-agenda-filter)))
6185 (setq maybe-refresh t))
6186 ((equal char ?/)
6187 (org-agenda-filter-by-tag-show-all)
6188 (when (get 'org-agenda-filter :preset-filter)
6189 (org-agenda-filter-apply org-agenda-filter))
6190 (setq maybe-refresh t))
6191 ((or (equal char ?\ )
6192 (setq a (rassoc char alist))
6193 (and (>= char ?0) (<= char ?9)
6194 (setq n (if (= char ?0) 9 (- char ?0 1))
6195 tag (concat effort-op (nth n efforts))
6196 a (cons tag nil)))
6197 (and (= char ??)
6198 (setq tag "?eff")
6199 a (cons tag nil))
6200 (and tag (setq a (cons tag nil))))
6201 (org-agenda-filter-by-tag-show-all)
6202 (setq tag (car a))
6203 (setq org-agenda-filter
6204 (cons (concat (if strip "-" "+") tag)
6205 (if narrow current nil)))
6206 (org-agenda-filter-apply org-agenda-filter)
6207 (setq maybe-refresh t))
6208 (t (error "Invalid tag selection character %c" char)))
6209 (when (and maybe-refresh
6210 (eq org-agenda-clockreport-mode 'with-filter))
6211 (org-agenda-redo))))
6213 (defun org-agenda-get-represented-tags ()
6214 "Get a list of all tags currently represented in the agenda."
6215 (let (p tags)
6216 (save-excursion
6217 (goto-char (point-min))
6218 (while (setq p (next-single-property-change (point) 'tags))
6219 (goto-char p)
6220 (mapc (lambda (x) (add-to-list 'tags x))
6221 (get-text-property (point) 'tags))))
6222 tags))
6224 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6225 "Refine the current filter. See `org-agenda-filter-by-tag."
6226 (interactive "P")
6227 (org-agenda-filter-by-tag strip char 'refine))
6229 (defun org-agenda-filter-make-matcher ()
6230 "Create the form that tests a line for the agenda filter."
6231 (let (f f1)
6232 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6233 org-agenda-filter))
6234 (if (member x '("-" "+"))
6235 (setq f1 (if (equal x "-") 'tags '(not tags)))
6236 (if (string-match "[<=>?]" x)
6237 (setq f1 (org-agenda-filter-effort-form x))
6238 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6239 (if (equal (string-to-char x) ?-)
6240 (setq f1 (list 'not f1))))
6241 (push f1 f))
6242 (cons 'and (nreverse f))))
6244 (defun org-agenda-filter-effort-form (e)
6245 "Return the form to compare the effort of the current line with what E says.
6246 E looks like \"+<2:25\"."
6247 (let (op)
6248 (setq e (substring e 1))
6249 (setq op (string-to-char e) e (substring e 1))
6250 (setq op (cond ((equal op ?<) '<=)
6251 ((equal op ?>) '>=)
6252 ((equal op ??) op)
6253 (t '=)))
6254 (list 'org-agenda-compare-effort (list 'quote op)
6255 (org-duration-string-to-minutes e))))
6257 (defun org-agenda-compare-effort (op value)
6258 "Compare the effort of the current line with VALUE, using OP.
6259 If the line does not have an effort defined, return nil."
6260 (let ((eff (org-get-at-bol 'effort-minutes)))
6261 (if (equal op ??)
6262 (not eff)
6263 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6264 value))))
6266 (defun org-agenda-filter-apply (filter)
6267 "Set FILTER as the new agenda filter and apply it."
6268 (let (tags)
6269 (setq org-agenda-filter filter
6270 org-agenda-filter-form (org-agenda-filter-make-matcher))
6271 (org-agenda-set-mode-name)
6272 (save-excursion
6273 (goto-char (point-min))
6274 (while (not (eobp))
6275 (if (org-get-at-bol 'org-marker)
6276 (progn
6277 (setq tags (org-get-at-bol 'tags)) ; used in eval
6278 (if (not (eval org-agenda-filter-form))
6279 (org-agenda-filter-by-tag-hide-line))
6280 (beginning-of-line 2))
6281 (beginning-of-line 2))))
6282 (if (get-char-property (point) 'invisible)
6283 (org-agenda-previous-line))))
6285 (defun org-agenda-filter-by-tag-hide-line ()
6286 (let (ov)
6287 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6288 (point-at-eol)))
6289 (overlay-put ov 'invisible t)
6290 (overlay-put ov 'type 'tags-filter)
6291 (push ov org-agenda-filter-overlays)))
6293 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6294 (setq pos (or pos (point)))
6295 (save-excursion
6296 (dolist (ov (overlays-at pos))
6297 (when (and (overlay-get ov 'invisible)
6298 (eq (overlay-get ov 'type) 'tags-filter))
6299 (goto-char pos)
6300 (if (< (overlay-start ov) (point-at-eol))
6301 (move-overlay ov (point-at-eol)
6302 (overlay-end ov)))))))
6304 (defun org-agenda-filter-by-tag-show-all ()
6305 (mapc 'delete-overlay org-agenda-filter-overlays)
6306 (setq org-agenda-filter-overlays nil)
6307 (setq org-agenda-filter nil)
6308 (setq org-agenda-filter-form nil)
6309 (org-agenda-set-mode-name))
6311 (defun org-agenda-manipulate-query-add ()
6312 "Manipulate the query by adding a search term with positive selection.
6313 Positive selection means the term must be matched for selection of an entry."
6314 (interactive)
6315 (org-agenda-manipulate-query ?\[))
6316 (defun org-agenda-manipulate-query-subtract ()
6317 "Manipulate the query by adding a search term with negative selection.
6318 Negative selection means term must not be matched for selection of an entry."
6319 (interactive)
6320 (org-agenda-manipulate-query ?\]))
6321 (defun org-agenda-manipulate-query-add-re ()
6322 "Manipulate the query by adding a search regexp with positive selection.
6323 Positive selection means the regexp must match for selection of an entry."
6324 (interactive)
6325 (org-agenda-manipulate-query ?\{))
6326 (defun org-agenda-manipulate-query-subtract-re ()
6327 "Manipulate the query by adding a search regexp with negative selection.
6328 Negative selection means regexp must not match for selection of an entry."
6329 (interactive)
6330 (org-agenda-manipulate-query ?\}))
6331 (defun org-agenda-manipulate-query (char)
6332 (cond
6333 ((memq org-agenda-type '(timeline agenda))
6334 (let ((org-agenda-include-inactive-timestamps t))
6335 (org-agenda-redo))
6336 (message "Display now includes inactive timestamps as well"))
6337 ((eq org-agenda-type 'search)
6338 (org-add-to-string
6339 'org-agenda-query-string
6340 (if org-agenda-last-search-view-search-was-boolean
6341 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6342 (?\{ . " +{}") (?\} . " -{}"))))
6343 " "))
6344 (setq org-agenda-redo-command
6345 (list 'org-search-view
6346 org-todo-only
6347 org-agenda-query-string
6348 (+ (length org-agenda-query-string)
6349 (if (member char '(?\{ ?\})) 0 1))))
6350 (set-register org-agenda-query-register org-agenda-query-string)
6351 (org-agenda-redo))
6352 (t (error "Cannot manipulate query for %s-type agenda buffers"
6353 org-agenda-type))))
6355 (defun org-add-to-string (var string)
6356 (set var (concat (symbol-value var) string)))
6358 (defun org-agenda-goto-date (date)
6359 "Jump to DATE in agenda."
6360 (interactive (list (let ((org-read-date-prefer-future
6361 (eval org-agenda-jump-prefer-future)))
6362 (org-read-date))))
6363 (org-agenda-list nil date))
6365 (defun org-agenda-goto-today ()
6366 "Go to today."
6367 (interactive)
6368 (org-agenda-check-type t 'timeline 'agenda)
6369 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6370 (cond
6371 (tdpos (goto-char tdpos))
6372 ((eq org-agenda-type 'agenda)
6373 (let* ((sd (org-agenda-compute-starting-span
6374 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6375 (org-agenda-overriding-arguments org-agenda-last-arguments))
6376 (setf (nth 1 org-agenda-overriding-arguments) sd)
6377 (org-agenda-redo)
6378 (org-agenda-find-same-or-today-or-agenda)))
6379 (t (error "Cannot find today")))))
6381 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6382 (goto-char
6383 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6384 (text-property-any (point-min) (point-max) 'org-today t)
6385 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6386 (point-min))))
6388 (defun org-agenda-later (arg)
6389 "Go forward in time by thee current span.
6390 With prefix ARG, go forward that many times the current span."
6391 (interactive "p")
6392 (org-agenda-check-type t 'agenda)
6393 (let* ((span org-agenda-current-span)
6394 (sd org-starting-day)
6395 (greg (calendar-gregorian-from-absolute sd))
6396 (cnt (org-get-at-bol 'org-day-cnt))
6397 greg2)
6398 (cond
6399 ((eq span 'day)
6400 (setq sd (+ arg sd)))
6401 ((eq span 'week)
6402 (setq sd (+ (* 7 arg) sd)))
6403 ((eq span 'month)
6404 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6405 sd (calendar-absolute-from-gregorian greg2))
6406 (setcar greg2 (1+ (car greg2))))
6407 ((eq span 'year)
6408 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6409 sd (calendar-absolute-from-gregorian greg2))
6410 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6412 (setq sd (+ (* span arg) sd))))
6413 (let ((org-agenda-overriding-arguments
6414 (list (car org-agenda-last-arguments) sd span t)))
6415 (org-agenda-redo)
6416 (org-agenda-find-same-or-today-or-agenda cnt))))
6418 (defun org-agenda-earlier (arg)
6419 "Go backward in time by the current span.
6420 With prefix ARG, go backward that many times the current span."
6421 (interactive "p")
6422 (org-agenda-later (- arg)))
6424 (defun org-agenda-view-mode-dispatch ()
6425 "Call one of the view mode commands."
6426 (interactive)
6427 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6428 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6429 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6430 [E]ntryText")
6431 (let ((a (read-char-exclusive)))
6432 (case a
6433 (?\ (call-interactively 'org-agenda-reset-view))
6434 (?d (call-interactively 'org-agenda-day-view))
6435 (?w (call-interactively 'org-agenda-week-view))
6436 (?m (call-interactively 'org-agenda-month-view))
6437 (?y (call-interactively 'org-agenda-year-view))
6438 (?l (call-interactively 'org-agenda-log-mode))
6439 (?L (org-agenda-log-mode '(4)))
6440 (?c (org-agenda-log-mode 'clockcheck))
6441 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6442 (?a (call-interactively 'org-agenda-archives-mode))
6443 (?A (org-agenda-archives-mode 'files))
6444 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6445 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6446 (?G (call-interactively 'org-agenda-toggle-time-grid))
6447 (?D (call-interactively 'org-agenda-toggle-diary))
6448 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6449 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6450 (org-agenda-check-type t 'timeline 'agenda)
6451 (org-agenda-redo))
6452 (message "Display now includes inactive timestamps as well"))
6453 (?q (message "Abort"))
6454 (otherwise (error "Invalid key" )))))
6456 (defun org-agenda-reset-view ()
6457 "Switch to default view for agenda."
6458 (interactive)
6459 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6460 (defun org-agenda-day-view (&optional day-of-year)
6461 "Switch to daily view for agenda.
6462 With argument DAY-OF-YEAR, switch to that day of the year."
6463 (interactive "P")
6464 (org-agenda-change-time-span 'day day-of-year))
6465 (defun org-agenda-week-view (&optional iso-week)
6466 "Switch to daily view for agenda.
6467 With argument ISO-WEEK, switch to the corresponding ISO week.
6468 If ISO-WEEK has more then 2 digits, only the last two encode the
6469 week. Any digits before this encode a year. So 200712 means
6470 week 12 of year 2007. Years in the range 1938-2037 can also be
6471 written as 2-digit years."
6472 (interactive "P")
6473 (org-agenda-change-time-span 'week iso-week))
6474 (defun org-agenda-month-view (&optional month)
6475 "Switch to monthly view for agenda.
6476 With argument MONTH, switch to that month."
6477 (interactive "P")
6478 (org-agenda-change-time-span 'month month))
6479 (defun org-agenda-year-view (&optional year)
6480 "Switch to yearly view for agenda.
6481 With argument YEAR, switch to that year.
6482 If MONTH has more then 2 digits, only the last two encode the
6483 month. Any digits before this encode a year. So 200712 means
6484 December year 2007. Years in the range 1938-2037 can also be
6485 written as 2-digit years."
6486 (interactive "P")
6487 (when year
6488 (setq year (org-small-year-to-year year)))
6489 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6490 (org-agenda-change-time-span 'year year)
6491 (error "Abort")))
6493 (defun org-agenda-change-time-span (span &optional n)
6494 "Change the agenda view to SPAN.
6495 SPAN may be `day', `week', `month', `year'."
6496 (org-agenda-check-type t 'agenda)
6497 (if (and (not n) (equal org-agenda-current-span span))
6498 (error "Viewing span is already \"%s\"" span))
6499 (let* ((sd (or (org-get-at-bol 'day)
6500 org-starting-day))
6501 (sd (org-agenda-compute-starting-span sd span n))
6502 (org-agenda-overriding-arguments
6503 (or org-agenda-overriding-arguments
6504 (list (car org-agenda-last-arguments) sd span t))))
6505 (org-agenda-redo)
6506 (org-agenda-find-same-or-today-or-agenda))
6507 (org-agenda-set-mode-name)
6508 (message "Switched to %s view" span))
6510 (defun org-agenda-compute-starting-span (sd span &optional n)
6511 "Compute starting date for agenda.
6512 SPAN may be `day', `week', `month', `year'. The return value
6513 is a cons cell with the starting date and the number of days,
6514 so that the date SD will be in that range."
6515 (let* ((greg (calendar-gregorian-from-absolute sd))
6516 (dg (nth 1 greg))
6517 (mg (car greg))
6518 (yg (nth 2 greg)))
6519 (cond
6520 ((eq span 'day)
6521 (when n
6522 (setq sd (+ (calendar-absolute-from-gregorian
6523 (list mg 1 yg))
6524 n -1))))
6525 ((eq span 'week)
6526 (let* ((nt (calendar-day-of-week
6527 (calendar-gregorian-from-absolute sd)))
6528 (d (if org-agenda-start-on-weekday
6529 (- nt org-agenda-start-on-weekday)
6532 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6533 (when n
6534 (require 'cal-iso)
6535 (when (> n 99)
6536 (setq y1 (org-small-year-to-year (/ n 100))
6537 n (mod n 100)))
6538 (setq sd
6539 (calendar-absolute-from-iso
6540 (list n 1
6541 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6542 ((eq span 'month)
6543 (let (y1)
6544 (when (and n (> n 99))
6545 (setq y1 (org-small-year-to-year (/ n 100))
6546 n (mod n 100)))
6547 (setq sd (calendar-absolute-from-gregorian
6548 (list (or n mg) 1 (or y1 yg))))))
6549 ((eq span 'year)
6550 (setq sd (calendar-absolute-from-gregorian
6551 (list 1 1 (or n yg))))))
6552 sd))
6554 (defun org-agenda-next-date-line (&optional arg)
6555 "Jump to the next line indicating a date in agenda buffer."
6556 (interactive "p")
6557 (org-agenda-check-type t 'agenda 'timeline)
6558 (beginning-of-line 1)
6559 ;; This does not work if user makes date format that starts with a blank
6560 (if (looking-at "^\\S-") (forward-char 1))
6561 (if (not (re-search-forward "^\\S-" nil t arg))
6562 (progn
6563 (backward-char 1)
6564 (error "No next date after this line in this buffer")))
6565 (goto-char (match-beginning 0)))
6567 (defun org-agenda-previous-date-line (&optional arg)
6568 "Jump to the previous line indicating a date in agenda buffer."
6569 (interactive "p")
6570 (org-agenda-check-type t 'agenda 'timeline)
6571 (beginning-of-line 1)
6572 (if (not (re-search-backward "^\\S-" nil t arg))
6573 (error "No previous date before this line in this buffer")))
6575 ;; Initialize the highlight
6576 (defvar org-hl (make-overlay 1 1))
6577 (overlay-put org-hl 'face 'highlight)
6579 (defun org-highlight (begin end &optional buffer)
6580 "Highlight a region with overlay."
6581 (move-overlay org-hl begin end (or buffer (current-buffer))))
6583 (defun org-unhighlight ()
6584 "Detach overlay INDEX."
6585 (org-detach-overlay org-hl))
6587 ;; FIXME this is currently not used.
6588 (defun org-highlight-until-next-command (beg end &optional buffer)
6589 "Move the highlight overlay to BEG/END, remove it before the next command."
6590 (org-highlight beg end buffer)
6591 (add-hook 'pre-command-hook 'org-unhighlight-once))
6592 (defun org-unhighlight-once ()
6593 "Remove the highlight from its position, and this function from the hook."
6594 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6595 (org-unhighlight))
6597 (defun org-agenda-follow-mode ()
6598 "Toggle follow mode in an agenda buffer."
6599 (interactive)
6600 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6601 (org-agenda-set-mode-name)
6602 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6603 (org-agenda-show))
6604 (message "Follow mode is %s"
6605 (if org-agenda-follow-mode "on" "off")))
6607 (defun org-agenda-entry-text-mode (&optional arg)
6608 "Toggle entry text mode in an agenda buffer."
6609 (interactive "P")
6610 (setq org-agenda-entry-text-mode (or (integerp arg)
6611 (not org-agenda-entry-text-mode)))
6612 (org-agenda-entry-text-hide)
6613 (and org-agenda-entry-text-mode
6614 (let ((org-agenda-entry-text-maxlines
6615 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6616 (org-agenda-entry-text-show)))
6617 (org-agenda-set-mode-name)
6618 (message "Entry text mode is %s. Maximum number of lines is %d"
6619 (if org-agenda-entry-text-mode "on" "off")
6620 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6622 (defun org-agenda-clockreport-mode (&optional with-filter)
6623 "Toggle clocktable mode in an agenda buffer.
6624 With prefix arg WITH-FILTER, make the clocktable respect the current
6625 agenda filter."
6626 (interactive "P")
6627 (org-agenda-check-type t 'agenda)
6628 (if with-filter
6629 (setq org-agenda-clockreport-mode 'with-filter)
6630 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6631 (org-agenda-set-mode-name)
6632 (org-agenda-redo)
6633 (message "Clocktable mode is %s"
6634 (if org-agenda-clockreport-mode "on" "off")))
6636 (defun org-agenda-log-mode (&optional special)
6637 "Toggle log mode in an agenda buffer.
6638 With argument SPECIAL, show all possible log items, not only the ones
6639 configured in `org-agenda-log-mode-items'.
6640 With a double `C-u' prefix arg, show *only* log items, nothing else."
6641 (interactive "P")
6642 (org-agenda-check-type t 'agenda 'timeline)
6643 (setq org-agenda-show-log
6644 (cond
6645 ((equal special '(16)) 'only)
6646 ((eq special 'clockcheck)
6647 (if (eq org-agenda-show-log 'clockcheck)
6648 nil 'clockcheck))
6649 (special '(closed clock state))
6650 (t (not org-agenda-show-log))))
6651 (org-agenda-set-mode-name)
6652 (org-agenda-redo)
6653 (message "Log mode is %s"
6654 (if org-agenda-show-log "on" "off")))
6656 (defun org-agenda-archives-mode (&optional with-files)
6657 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6658 When called with a prefix argument, include all archive files as well."
6659 (interactive "P")
6660 (setq org-agenda-archives-mode
6661 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6662 (org-agenda-set-mode-name)
6663 (org-agenda-redo)
6664 (message
6665 "%s"
6666 (cond
6667 ((eq org-agenda-archives-mode nil)
6668 "No archives are included")
6669 ((eq org-agenda-archives-mode 'trees)
6670 (format "Trees with :%s: tag are included" org-archive-tag))
6671 ((eq org-agenda-archives-mode t)
6672 (format "Trees with :%s: tag and all active archive files are included"
6673 org-archive-tag)))))
6675 (defun org-agenda-toggle-diary ()
6676 "Toggle diary inclusion in an agenda buffer."
6677 (interactive)
6678 (org-agenda-check-type t 'agenda)
6679 (setq org-agenda-include-diary (not org-agenda-include-diary))
6680 (org-agenda-redo)
6681 (org-agenda-set-mode-name)
6682 (message "Diary inclusion turned %s"
6683 (if org-agenda-include-diary "on" "off")))
6685 (defun org-agenda-toggle-deadlines ()
6686 "Toggle inclusion of entries with a deadline in an agenda buffer."
6687 (interactive)
6688 (org-agenda-check-type t 'agenda)
6689 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6690 (org-agenda-redo)
6691 (org-agenda-set-mode-name)
6692 (message "Deadlines inclusion turned %s"
6693 (if org-agenda-include-deadlines "on" "off")))
6695 (defun org-agenda-toggle-time-grid ()
6696 "Toggle time grid in an agenda buffer."
6697 (interactive)
6698 (org-agenda-check-type t 'agenda)
6699 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6700 (org-agenda-redo)
6701 (org-agenda-set-mode-name)
6702 (message "Time-grid turned %s"
6703 (if org-agenda-use-time-grid "on" "off")))
6705 (defun org-agenda-set-mode-name ()
6706 "Set the mode name to indicate all the small mode settings."
6707 (setq mode-name
6708 (list "Org-Agenda"
6709 (if (get 'org-agenda-files 'org-restrict) " []" "")
6711 '(:eval (org-agenda-span-name org-agenda-current-span))
6712 (if org-agenda-follow-mode " Follow" "")
6713 (if org-agenda-entry-text-mode " ETxt" "")
6714 (if org-agenda-include-diary " Diary" "")
6715 (if org-agenda-include-deadlines " Ddl" "")
6716 (if org-agenda-use-time-grid " Grid" "")
6717 (if (and (boundp 'org-habit-show-habits)
6718 org-habit-show-habits) " Habit" "")
6719 (cond
6720 ((consp org-agenda-show-log) " LogAll")
6721 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6722 (org-agenda-show-log " Log")
6723 (t ""))
6724 (if (or org-agenda-filter (get 'org-agenda-filter
6725 :preset-filter))
6726 (concat " {" (mapconcat
6727 'identity
6728 (append (get 'org-agenda-filter
6729 :preset-filter)
6730 org-agenda-filter) "") "}")
6732 (if org-agenda-archives-mode
6733 (if (eq org-agenda-archives-mode t)
6734 " Archives"
6735 (format " :%s:" org-archive-tag))
6737 (if org-agenda-clockreport-mode
6738 (if (eq org-agenda-clockreport-mode 'with-filter)
6739 " Clock{}" " Clock")
6740 "")))
6741 (force-mode-line-update))
6743 (defun org-agenda-post-command-hook ()
6744 (setq org-agenda-type
6745 (or (get-text-property (point) 'org-agenda-type)
6746 (get-text-property (max (point-min) (1- (point)))
6747 'org-agenda-type))))
6749 (defun org-agenda-next-line ()
6750 "Move cursor to the next line, and show if follow mode is active."
6751 (interactive)
6752 (call-interactively 'next-line)
6753 (org-agenda-do-context-action))
6755 (defun org-agenda-previous-line ()
6756 "Move cursor to the previous line, and show if follow-mode is active."
6757 (interactive)
6758 (call-interactively 'previous-line)
6759 (org-agenda-do-context-action))
6761 (defun org-agenda-do-context-action ()
6762 "Show outline path and, maybe, follow mode window."
6763 (let ((m (org-get-at-bol 'org-marker)))
6764 (if (and org-agenda-follow-mode m)
6765 (org-agenda-show))
6766 (if (and m org-agenda-show-outline-path)
6767 (org-with-point-at m
6768 (org-display-outline-path t)))))
6770 (defun org-agenda-show-priority ()
6771 "Show the priority of the current item.
6772 This priority is composed of the main priority given with the [#A] cookies,
6773 and by additional input from the age of a schedules or deadline entry."
6774 (interactive)
6775 (let* ((pri (org-get-at-bol 'priority)))
6776 (message "Priority is %d" (if pri pri -1000))))
6778 (defun org-agenda-show-tags ()
6779 "Show the tags applicable to the current item."
6780 (interactive)
6781 (let* ((tags (org-get-at-bol 'tags)))
6782 (if tags
6783 (message "Tags are :%s:"
6784 (org-no-properties (mapconcat 'identity tags ":")))
6785 (message "No tags associated with this line"))))
6787 (defun org-agenda-goto (&optional highlight)
6788 "Go to the Org-mode file which contains the item at point."
6789 (interactive)
6790 (let* ((marker (or (org-get-at-bol 'org-marker)
6791 (org-agenda-error)))
6792 (buffer (marker-buffer marker))
6793 (pos (marker-position marker)))
6794 (switch-to-buffer-other-window buffer)
6795 (widen)
6796 (push-mark)
6797 (goto-char pos)
6798 (when (org-mode-p)
6799 (org-show-context 'agenda)
6800 (save-excursion
6801 (and (outline-next-heading)
6802 (org-flag-heading nil)))) ; show the next heading
6803 (when (outline-invisible-p)
6804 (show-entry)) ; display invisible text
6805 (recenter (/ (window-height) 2))
6806 (run-hooks 'org-agenda-after-show-hook)
6807 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6809 (defvar org-agenda-after-show-hook nil
6810 "Normal hook run after an item has been shown from the agenda.
6811 Point is in the buffer where the item originated.")
6813 (defun org-agenda-kill ()
6814 "Kill the entry or subtree belonging to the current agenda entry."
6815 (interactive)
6816 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6817 (let* ((marker (or (org-get-at-bol 'org-marker)
6818 (org-agenda-error)))
6819 (buffer (marker-buffer marker))
6820 (pos (marker-position marker))
6821 (type (org-get-at-bol 'type))
6822 dbeg dend (n 0) conf)
6823 (org-with-remote-undo buffer
6824 (with-current-buffer buffer
6825 (save-excursion
6826 (goto-char pos)
6827 (if (and (org-mode-p) (not (member type '("sexp"))))
6828 (setq dbeg (progn (org-back-to-heading t) (point))
6829 dend (org-end-of-subtree t t))
6830 (setq dbeg (point-at-bol)
6831 dend (min (point-max) (1+ (point-at-eol)))))
6832 (goto-char dbeg)
6833 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6834 (setq conf (or (eq t org-agenda-confirm-kill)
6835 (and (numberp org-agenda-confirm-kill)
6836 (> n org-agenda-confirm-kill))))
6837 (and conf
6838 (not (y-or-n-p
6839 (format "Delete entry with %d lines in buffer \"%s\"? "
6840 n (buffer-name buffer))))
6841 (error "Abort"))
6842 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6843 (with-current-buffer buffer (delete-region dbeg dend))
6844 (message "Agenda item and source killed"))))
6846 (defvar org-archive-default-command)
6847 (defun org-agenda-archive-default ()
6848 "Archive the entry or subtree belonging to the current agenda entry."
6849 (interactive)
6850 (require 'org-archive)
6851 (org-agenda-archive-with org-archive-default-command))
6853 (defun org-agenda-archive-default-with-confirmation ()
6854 "Archive the entry or subtree belonging to the current agenda entry."
6855 (interactive)
6856 (require 'org-archive)
6857 (org-agenda-archive-with org-archive-default-command 'confirm))
6859 (defun org-agenda-archive ()
6860 "Archive the entry or subtree belonging to the current agenda entry."
6861 (interactive)
6862 (org-agenda-archive-with 'org-archive-subtree))
6864 (defun org-agenda-archive-to-archive-sibling ()
6865 "Move the entry to the archive sibling."
6866 (interactive)
6867 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6869 (defun org-agenda-archive-with (cmd &optional confirm)
6870 "Move the entry to the archive sibling."
6871 (interactive)
6872 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6873 (let* ((marker (or (org-get-at-bol 'org-marker)
6874 (org-agenda-error)))
6875 (buffer (marker-buffer marker))
6876 (pos (marker-position marker)))
6877 (org-with-remote-undo buffer
6878 (with-current-buffer buffer
6879 (if (org-mode-p)
6880 (if (and confirm
6881 (not (y-or-n-p "Archive this subtree or entry? ")))
6882 (error "Abort")
6883 (save-excursion
6884 (goto-char pos)
6885 (org-remove-subtree-entries-from-agenda)
6886 (org-back-to-heading t)
6887 (funcall cmd)))
6888 (error "Archiving works only in Org-mode files"))))))
6890 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6891 "Remove all lines in the agenda that correspond to a given subtree.
6892 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6893 If this information is not given, the function uses the tree at point."
6894 (let ((buf (or buf (current-buffer))) m p)
6895 (save-excursion
6896 (unless (and beg end)
6897 (org-back-to-heading t)
6898 (setq beg (point))
6899 (org-end-of-subtree t)
6900 (setq end (point)))
6901 (set-buffer (get-buffer org-agenda-buffer-name))
6902 (save-excursion
6903 (goto-char (point-max))
6904 (beginning-of-line 1)
6905 (while (not (bobp))
6906 (when (and (setq m (org-get-at-bol 'org-marker))
6907 (equal buf (marker-buffer m))
6908 (setq p (marker-position m))
6909 (>= p beg)
6910 (< p end))
6911 (let ((inhibit-read-only t))
6912 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6913 (beginning-of-line 0))))))
6915 (defun org-agenda-refile (&optional goto rfloc no-update)
6916 "Refile the item at point."
6917 (interactive "P")
6918 (if (equal goto '(16))
6919 (org-refile-goto-last-stored)
6920 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6921 (org-agenda-error)))
6922 (buffer (marker-buffer marker))
6923 (pos (marker-position marker))
6924 (rfloc (or rfloc
6925 (org-refile-get-location
6926 (if goto "Goto" "Refile to") buffer
6927 org-refile-allow-creating-parent-nodes))))
6928 (with-current-buffer buffer
6929 (save-excursion
6930 (save-restriction
6931 (widen)
6932 (goto-char marker)
6933 (org-remove-subtree-entries-from-agenda)
6934 (org-refile goto buffer rfloc)))))
6935 (unless no-update (org-agenda-redo))))
6937 (defun org-agenda-open-link (&optional arg)
6938 "Follow the link in the current line, if any.
6939 This looks for a link in the displayed line in the agenda. It also looks
6940 at the text of the entry itself."
6941 (interactive "P")
6942 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6943 (org-get-at-bol 'org-marker)))
6944 (buffer (and marker (marker-buffer marker)))
6945 (prefix (buffer-substring
6946 (point-at-bol) (point-at-eol))))
6947 (cond
6948 (buffer
6949 (with-current-buffer buffer
6950 (save-excursion
6951 (save-restriction
6952 (widen)
6953 (goto-char marker)
6954 (org-offer-links-in-entry arg prefix)))))
6955 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6956 (save-excursion
6957 (beginning-of-line 1)
6958 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6959 (org-open-link-from-string (match-string 1)))
6960 (t (error "No link to open here")))))
6962 (defun org-agenda-copy-local-variable (var)
6963 "Get a variable from a referenced buffer and install it here."
6964 (let ((m (org-get-at-bol 'org-marker)))
6965 (when (and m (buffer-live-p (marker-buffer m)))
6966 (org-set-local var (with-current-buffer (marker-buffer m)
6967 (symbol-value var))))))
6969 (defun org-agenda-switch-to (&optional delete-other-windows)
6970 "Go to the Org-mode file which contains the item at point."
6971 (interactive)
6972 (if (and org-return-follows-link
6973 (not (org-get-at-bol 'org-marker))
6974 (org-in-regexp org-bracket-link-regexp))
6975 (org-open-link-from-string (match-string 0))
6976 (let* ((marker (or (org-get-at-bol 'org-marker)
6977 (org-agenda-error)))
6978 (buffer (marker-buffer marker))
6979 (pos (marker-position marker)))
6980 (org-pop-to-buffer-same-window buffer)
6981 (and delete-other-windows (delete-other-windows))
6982 (widen)
6983 (goto-char pos)
6984 (when (org-mode-p)
6985 (org-show-context 'agenda)
6986 (save-excursion
6987 (and (outline-next-heading)
6988 (org-flag-heading nil))) ; show the next heading
6989 (when (outline-invisible-p)
6990 (show-entry)))))) ; display invisible text
6992 (defun org-agenda-goto-mouse (ev)
6993 "Go to the Org-mode file which contains the item at the mouse click."
6994 (interactive "e")
6995 (mouse-set-point ev)
6996 (org-agenda-goto))
6998 (defun org-agenda-show (&optional full-entry)
6999 "Display the Org-mode file which contains the item at point.
7000 With prefix argument FULL-ENTRY, make the entire entry visible
7001 if it was hidden in the outline."
7002 (interactive "P")
7003 (let ((win (selected-window)))
7004 (if full-entry
7005 (let ((org-show-entry-below t))
7006 (org-agenda-goto t))
7007 (org-agenda-goto t))
7008 (select-window win)))
7010 (defvar org-agenda-show-window nil)
7011 (defun org-agenda-show-and-scroll-up ()
7012 "Display the Org-mode file which contains the item at point.
7013 When called repeatedly, scroll the window that is displaying the buffer."
7014 (interactive)
7015 (let ((win (selected-window)))
7016 (if (and (window-live-p org-agenda-show-window)
7017 (eq this-command last-command))
7018 (progn
7019 (select-window org-agenda-show-window)
7020 (ignore-errors (scroll-up)))
7021 (org-agenda-goto t)
7022 (show-subtree)
7023 (setq org-agenda-show-window (selected-window)))
7024 (select-window win)))
7026 (defun org-agenda-show-scroll-down ()
7027 "Scroll down the window showing the agenda."
7028 (interactive)
7029 (let ((win (selected-window)))
7030 (when (window-live-p org-agenda-show-window)
7031 (select-window org-agenda-show-window)
7032 (ignore-errors (scroll-down))
7033 (select-window win))))
7035 (defun org-agenda-show-1 (&optional more)
7036 "Display the Org-mode file which contains the item at point.
7037 The prefix arg selects the amount of information to display:
7039 0 hide the subtree
7040 1 just show the entry according to defaults.
7041 2 show the children view
7042 3 show the subtree view
7043 4 show the entire subtree and any LOGBOOK drawers
7044 5 show the entire subtree and any drawers
7045 With prefix argument FULL-ENTRY, make the entire entry visible
7046 if it was hidden in the outline."
7047 (interactive "p")
7048 (let ((win (selected-window)))
7049 (org-agenda-goto t)
7050 (org-recenter-heading 1)
7051 (cond
7052 ((= more 0)
7053 (hide-subtree)
7054 (save-excursion
7055 (org-back-to-heading)
7056 (run-hook-with-args 'org-cycle-hook 'folded))
7057 (message "Remote: FOLDED"))
7058 ((and (org-called-interactively-p 'any) (= more 1))
7059 (message "Remote: show with default settings"))
7060 ((= more 2)
7061 (show-entry)
7062 (show-children)
7063 (save-excursion
7064 (org-back-to-heading)
7065 (run-hook-with-args 'org-cycle-hook 'children))
7066 (message "Remote: CHILDREN"))
7067 ((= more 3)
7068 (show-subtree)
7069 (save-excursion
7070 (org-back-to-heading)
7071 (run-hook-with-args 'org-cycle-hook 'subtree))
7072 (message "Remote: SUBTREE"))
7073 ((= more 4)
7074 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7075 (org-drawer-regexp
7076 (concat "^[ \t]*:\\("
7077 (mapconcat 'regexp-quote org-drawers "\\|")
7078 "\\):[ \t]*$")))
7079 (show-subtree)
7080 (save-excursion
7081 (org-back-to-heading)
7082 (org-cycle-hide-drawers 'subtree)))
7083 (message "Remote: SUBTREE AND LOGBOOK"))
7084 ((> more 4)
7085 (show-subtree)
7086 (message "Remote: SUBTREE AND ALL DRAWERS")))
7087 (select-window win)))
7089 (defun org-recenter-heading (n)
7090 (save-excursion
7091 (org-back-to-heading)
7092 (recenter n)))
7094 (defvar org-agenda-cycle-counter nil)
7095 (defun org-agenda-cycle-show (&optional n)
7096 "Show the current entry in another window, with default settings.
7097 Default settings are taken from `org-show-hierarchy-above' and siblings.
7098 When use repeatedly in immediate succession, the remote entry will cycle
7099 through visibility
7101 children -> subtree -> folded
7103 When called with a numeric prefix arg, that arg will be passed through to
7104 `org-agenda-show-1'. For the interpretation of that argument, see the
7105 docstring of `org-agenda-show-1'."
7106 (interactive "P")
7107 (if (integerp n)
7108 (setq org-agenda-cycle-counter n)
7109 (if (not (eq last-command this-command))
7110 (setq org-agenda-cycle-counter 1)
7111 (if (equal org-agenda-cycle-counter 0)
7112 (setq org-agenda-cycle-counter 2)
7113 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7114 (if (> org-agenda-cycle-counter 3)
7115 (setq org-agenda-cycle-counter 0)))))
7116 (org-agenda-show-1 org-agenda-cycle-counter))
7118 (defun org-agenda-recenter (arg)
7119 "Display the Org-mode file which contains the item at point and recenter."
7120 (interactive "P")
7121 (let ((win (selected-window)))
7122 (org-agenda-goto t)
7123 (recenter arg)
7124 (select-window win)))
7126 (defun org-agenda-show-mouse (ev)
7127 "Display the Org-mode file which contains the item at the mouse click."
7128 (interactive "e")
7129 (mouse-set-point ev)
7130 (org-agenda-show))
7132 (defun org-agenda-check-no-diary ()
7133 "Check if the entry is a diary link and abort if yes."
7134 (if (org-get-at-bol 'org-agenda-diary-link)
7135 (org-agenda-error)))
7137 (defun org-agenda-error ()
7138 (error "Command not allowed in this line"))
7140 (defun org-agenda-tree-to-indirect-buffer ()
7141 "Show the subtree corresponding to the current entry in an indirect buffer.
7142 This calls the command `org-tree-to-indirect-buffer' from the original
7143 Org-mode buffer.
7144 With numerical prefix arg ARG, go up to this level and then take that tree.
7145 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7146 use the dedicated frame)."
7147 (interactive)
7148 (org-agenda-check-no-diary)
7149 (let* ((marker (or (org-get-at-bol 'org-marker)
7150 (org-agenda-error)))
7151 (buffer (marker-buffer marker))
7152 (pos (marker-position marker)))
7153 (with-current-buffer buffer
7154 (save-excursion
7155 (goto-char pos)
7156 (call-interactively 'org-tree-to-indirect-buffer)))))
7158 (defvar org-last-heading-marker (make-marker)
7159 "Marker pointing to the headline that last changed its TODO state
7160 by a remote command from the agenda.")
7162 (defun org-agenda-todo-nextset ()
7163 "Switch TODO entry to next sequence."
7164 (interactive)
7165 (org-agenda-todo 'nextset))
7167 (defun org-agenda-todo-previousset ()
7168 "Switch TODO entry to previous sequence."
7169 (interactive)
7170 (org-agenda-todo 'previousset))
7172 (defun org-agenda-todo (&optional arg)
7173 "Cycle TODO state of line at point, also in Org-mode file.
7174 This changes the line at point, all other lines in the agenda referring to
7175 the same tree node, and the headline of the tree node in the Org-mode file."
7176 (interactive "P")
7177 (org-agenda-check-no-diary)
7178 (let* ((col (current-column))
7179 (marker (or (org-get-at-bol 'org-marker)
7180 (org-agenda-error)))
7181 (buffer (marker-buffer marker))
7182 (pos (marker-position marker))
7183 (hdmarker (org-get-at-bol 'org-hd-marker))
7184 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7185 (inhibit-read-only t)
7186 org-agenda-headline-snapshot-before-repeat newhead just-one)
7187 (org-with-remote-undo buffer
7188 (with-current-buffer buffer
7189 (widen)
7190 (goto-char pos)
7191 (org-show-context 'agenda)
7192 (save-excursion
7193 (and (outline-next-heading)
7194 (org-flag-heading nil))) ; show the next heading
7195 (let ((current-prefix-arg arg))
7196 (call-interactively 'org-todo))
7197 (and (bolp) (forward-char 1))
7198 (setq newhead (org-get-heading))
7199 (when (and (org-bound-and-true-p
7200 org-agenda-headline-snapshot-before-repeat)
7201 (not (equal org-agenda-headline-snapshot-before-repeat
7202 newhead))
7203 todayp)
7204 (setq newhead org-agenda-headline-snapshot-before-repeat
7205 just-one t))
7206 (save-excursion
7207 (org-back-to-heading)
7208 (move-marker org-last-heading-marker (point))))
7209 (beginning-of-line 1)
7210 (save-excursion
7211 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7212 (org-move-to-column col))))
7214 (defun org-agenda-add-note (&optional arg)
7215 "Add a time-stamped note to the entry at point."
7216 (interactive "P")
7217 (org-agenda-check-no-diary)
7218 (let* ((marker (or (org-get-at-bol 'org-marker)
7219 (org-agenda-error)))
7220 (buffer (marker-buffer marker))
7221 (pos (marker-position marker))
7222 (hdmarker (org-get-at-bol 'org-hd-marker))
7223 (inhibit-read-only t))
7224 (with-current-buffer buffer
7225 (widen)
7226 (goto-char pos)
7227 (org-show-context 'agenda)
7228 (save-excursion
7229 (and (outline-next-heading)
7230 (org-flag-heading nil))) ; show the next heading
7231 (org-add-note))))
7233 (defun org-agenda-change-all-lines (newhead hdmarker
7234 &optional fixface just-this)
7235 "Change all lines in the agenda buffer which match HDMARKER.
7236 The new content of the line will be NEWHEAD (as modified by
7237 `org-format-agenda-item'). HDMARKER is checked with
7238 `equal' against all `org-hd-marker' text properties in the file.
7239 If FIXFACE is non-nil, the face of each item is modified according to
7240 the new TODO state.
7241 If JUST-THIS is non-nil, change just the current line, not all.
7242 If FORCE-TAGS is non nil, the car of it returns the new tags."
7243 (let* ((inhibit-read-only t)
7244 (line (org-current-line))
7245 (thetags (with-current-buffer (marker-buffer hdmarker)
7246 (save-excursion (save-restriction (widen)
7247 (goto-char hdmarker)
7248 (org-get-tags-at)))))
7249 props m pl undone-face done-face finish new dotime cat tags)
7250 (save-excursion
7251 (goto-char (point-max))
7252 (beginning-of-line 1)
7253 (while (not finish)
7254 (setq finish (bobp))
7255 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7256 (or (not just-this) (= (org-current-line) line))
7257 (equal m hdmarker))
7258 (setq props (text-properties-at (point))
7259 dotime (org-get-at-bol 'dotime)
7260 cat (org-get-at-bol 'org-category)
7261 tags thetags
7263 (let ((org-prefix-format-compiled
7264 (or (get-text-property (point) 'format)
7265 org-prefix-format-compiled)))
7266 (with-current-buffer (marker-buffer hdmarker)
7267 (save-excursion
7268 (save-restriction
7269 (widen)
7270 (org-format-agenda-item (org-get-at-bol 'extra)
7271 newhead cat tags dotime)))))
7272 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7273 undone-face (org-get-at-bol 'undone-face)
7274 done-face (org-get-at-bol 'done-face))
7275 (beginning-of-line 1)
7276 (cond
7277 ((equal new "")
7278 (and (looking-at ".*\n?") (replace-match "")))
7279 ((looking-at ".*")
7280 (replace-match new t t)
7281 (beginning-of-line 1)
7282 (add-text-properties (point-at-bol) (point-at-eol) props)
7283 (when fixface
7284 (add-text-properties
7285 (point-at-bol) (point-at-eol)
7286 (list 'face
7287 (if org-last-todo-state-is-todo
7288 undone-face done-face))))
7289 (org-agenda-highlight-todo 'line)
7290 (beginning-of-line 1))
7291 (t (error "Line update did not work"))))
7292 (beginning-of-line 0)))
7293 (org-finalize-agenda)))
7295 (defun org-agenda-align-tags (&optional line)
7296 "Align all tags in agenda items to `org-agenda-tags-column'."
7297 (let ((inhibit-read-only t) l c)
7298 (save-excursion
7299 (goto-char (if line (point-at-bol) (point-min)))
7300 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7301 (if line (point-at-eol) nil) t)
7302 (add-text-properties
7303 (match-beginning 2) (match-end 2)
7304 (list 'face (delq nil (let ((prop (get-text-property
7305 (match-beginning 2) 'face)))
7306 (or (listp prop) (setq prop (list prop)))
7307 (if (memq 'org-tag prop)
7308 prop
7309 (cons 'org-tag prop))))))
7310 (setq l (- (match-end 2) (match-beginning 2))
7311 c (if (< org-agenda-tags-column 0)
7312 (- (abs org-agenda-tags-column) l)
7313 org-agenda-tags-column))
7314 (delete-region (match-beginning 1) (match-end 1))
7315 (goto-char (match-beginning 1))
7316 (insert (org-add-props
7317 (make-string (max 1 (- c (current-column))) ?\ )
7318 (plist-put (copy-sequence (text-properties-at (point)))
7319 'face nil))))
7320 (goto-char (point-min))
7321 (org-font-lock-add-tag-faces (point-max)))))
7323 (defun org-agenda-priority-up ()
7324 "Increase the priority of line at point, also in Org-mode file."
7325 (interactive)
7326 (org-agenda-priority 'up))
7328 (defun org-agenda-priority-down ()
7329 "Decrease the priority of line at point, also in Org-mode file."
7330 (interactive)
7331 (org-agenda-priority 'down))
7333 (defun org-agenda-priority (&optional force-direction)
7334 "Set the priority of line at point, also in Org-mode file.
7335 This changes the line at point, all other lines in the agenda referring to
7336 the same tree node, and the headline of the tree node in the Org-mode file."
7337 (interactive)
7338 (unless org-enable-priority-commands
7339 (error "Priority commands are disabled"))
7340 (org-agenda-check-no-diary)
7341 (let* ((marker (or (org-get-at-bol 'org-marker)
7342 (org-agenda-error)))
7343 (hdmarker (org-get-at-bol 'org-hd-marker))
7344 (buffer (marker-buffer hdmarker))
7345 (pos (marker-position hdmarker))
7346 (inhibit-read-only t)
7347 newhead)
7348 (org-with-remote-undo buffer
7349 (with-current-buffer buffer
7350 (widen)
7351 (goto-char pos)
7352 (org-show-context 'agenda)
7353 (save-excursion
7354 (and (outline-next-heading)
7355 (org-flag-heading nil))) ; show the next heading
7356 (funcall 'org-priority force-direction)
7357 (end-of-line 1)
7358 (setq newhead (org-get-heading)))
7359 (org-agenda-change-all-lines newhead hdmarker)
7360 (beginning-of-line 1))))
7362 ;; FIXME: should fix the tags property of the agenda line.
7363 (defun org-agenda-set-tags (&optional tag onoff)
7364 "Set tags for the current headline."
7365 (interactive)
7366 (org-agenda-check-no-diary)
7367 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7368 (call-interactively 'org-change-tag-in-region)
7369 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7370 (org-agenda-error)))
7371 (buffer (marker-buffer hdmarker))
7372 (pos (marker-position hdmarker))
7373 (inhibit-read-only t)
7374 newhead)
7375 (org-with-remote-undo buffer
7376 (with-current-buffer buffer
7377 (widen)
7378 (goto-char pos)
7379 (save-excursion
7380 (org-show-context 'agenda))
7381 (save-excursion
7382 (and (outline-next-heading)
7383 (org-flag-heading nil))) ; show the next heading
7384 (goto-char pos)
7385 (if tag
7386 (org-toggle-tag tag onoff)
7387 (call-interactively 'org-set-tags))
7388 (end-of-line 1)
7389 (setq newhead (org-get-heading)))
7390 (org-agenda-change-all-lines newhead hdmarker)
7391 (beginning-of-line 1)))))
7393 (defun org-agenda-set-property ()
7394 "Set a property for the current headline."
7395 (interactive)
7396 (org-agenda-check-no-diary)
7397 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7398 (org-agenda-error)))
7399 (buffer (marker-buffer hdmarker))
7400 (pos (marker-position hdmarker))
7401 (inhibit-read-only t)
7402 newhead)
7403 (org-with-remote-undo buffer
7404 (with-current-buffer buffer
7405 (widen)
7406 (goto-char pos)
7407 (save-excursion
7408 (org-show-context 'agenda))
7409 (save-excursion
7410 (and (outline-next-heading)
7411 (org-flag-heading nil))) ; show the next heading
7412 (goto-char pos)
7413 (call-interactively 'org-set-property)))))
7415 (defun org-agenda-set-effort ()
7416 "Set the effort property for the current headline."
7417 (interactive)
7418 (org-agenda-check-no-diary)
7419 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7420 (org-agenda-error)))
7421 (buffer (marker-buffer hdmarker))
7422 (pos (marker-position hdmarker))
7423 (inhibit-read-only t)
7424 newhead)
7425 (org-with-remote-undo buffer
7426 (with-current-buffer buffer
7427 (widen)
7428 (goto-char pos)
7429 (save-excursion
7430 (org-show-context 'agenda))
7431 (save-excursion
7432 (and (outline-next-heading)
7433 (org-flag-heading nil))) ; show the next heading
7434 (goto-char pos)
7435 (call-interactively 'org-set-effort)
7436 (end-of-line 1)
7437 (setq newhead (org-get-heading)))
7438 (org-agenda-change-all-lines newhead hdmarker))))
7440 (defun org-agenda-toggle-archive-tag ()
7441 "Toggle the archive tag for the current entry."
7442 (interactive)
7443 (org-agenda-check-no-diary)
7444 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7445 (org-agenda-error)))
7446 (buffer (marker-buffer hdmarker))
7447 (pos (marker-position hdmarker))
7448 (inhibit-read-only t)
7449 newhead)
7450 (org-with-remote-undo buffer
7451 (with-current-buffer buffer
7452 (widen)
7453 (goto-char pos)
7454 (org-show-context 'agenda)
7455 (save-excursion
7456 (and (outline-next-heading)
7457 (org-flag-heading nil))) ; show the next heading
7458 (call-interactively 'org-toggle-archive-tag)
7459 (end-of-line 1)
7460 (setq newhead (org-get-heading)))
7461 (org-agenda-change-all-lines newhead hdmarker)
7462 (beginning-of-line 1))))
7464 (defun org-agenda-do-date-later (arg)
7465 (interactive "P")
7466 (cond
7467 ((or (equal arg '(16))
7468 (memq last-command
7469 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7470 (setq this-command 'org-agenda-date-later-minutes)
7471 (org-agenda-date-later-minutes 1))
7472 ((or (equal arg '(4))
7473 (memq last-command
7474 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7475 (setq this-command 'org-agenda-date-later-hours)
7476 (org-agenda-date-later-hours 1))
7478 (org-agenda-date-later (prefix-numeric-value arg)))))
7480 (defun org-agenda-do-date-earlier (arg)
7481 (interactive "P")
7482 (cond
7483 ((or (equal arg '(16))
7484 (memq last-command
7485 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7486 (setq this-command 'org-agenda-date-earlier-minutes)
7487 (org-agenda-date-earlier-minutes 1))
7488 ((or (equal arg '(4))
7489 (memq last-command
7490 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7491 (setq this-command 'org-agenda-date-earlier-hours)
7492 (org-agenda-date-earlier-hours 1))
7494 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7496 (defun org-agenda-date-later (arg &optional what)
7497 "Change the date of this item to ARG day(s) later."
7498 (interactive "p")
7499 (org-agenda-check-type t 'agenda 'timeline)
7500 (org-agenda-check-no-diary)
7501 (let* ((marker (or (org-get-at-bol 'org-marker)
7502 (org-agenda-error)))
7503 (buffer (marker-buffer marker))
7504 (pos (marker-position marker)))
7505 (org-with-remote-undo buffer
7506 (with-current-buffer buffer
7507 (widen)
7508 (goto-char pos)
7509 (if (not (org-at-timestamp-p))
7510 (error "Cannot find time stamp"))
7511 (org-timestamp-change arg (or what 'day)))
7512 (org-agenda-show-new-time marker org-last-changed-timestamp))
7513 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7515 (defun org-agenda-date-earlier (arg &optional what)
7516 "Change the date of this item to ARG day(s) earlier."
7517 (interactive "p")
7518 (org-agenda-date-later (- arg) what))
7520 (defun org-agenda-date-later-minutes (arg)
7521 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7522 (interactive "p")
7523 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7524 (org-agenda-date-later arg 'minute))
7526 (defun org-agenda-date-earlier-minutes (arg)
7527 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7528 (interactive "p")
7529 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7530 (org-agenda-date-earlier arg 'minute))
7532 (defun org-agenda-date-later-hours (arg)
7533 "Change the time of this item, in hour steps."
7534 (interactive "p")
7535 (org-agenda-date-later arg 'hour))
7537 (defun org-agenda-date-earlier-hours (arg)
7538 "Change the time of this item, in hour steps."
7539 (interactive "p")
7540 (org-agenda-date-earlier arg 'hour))
7542 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7543 "Show new date stamp via text properties."
7544 ;; We use text properties to make this undoable
7545 (let ((inhibit-read-only t)
7546 (buffer-invisibility-spec))
7547 (setq stamp (concat " " prefix " => " stamp))
7548 (save-excursion
7549 (goto-char (point-max))
7550 (while (not (bobp))
7551 (when (equal marker (org-get-at-bol 'org-marker))
7552 (org-move-to-column (- (window-width) (length stamp)) t)
7553 (org-agenda-fix-tags-filter-overlays-at (point))
7554 (if (featurep 'xemacs)
7555 ;; Use `duplicable' property to trigger undo recording
7556 (let ((ex (make-extent nil nil))
7557 (gl (make-glyph stamp)))
7558 (set-glyph-face gl 'secondary-selection)
7559 (set-extent-properties
7560 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7561 (insert-extent ex (1- (point)) (point-at-eol)))
7562 (add-text-properties
7563 (1- (point)) (point-at-eol)
7564 (list 'display (org-add-props stamp nil
7565 'face 'secondary-selection))))
7566 (beginning-of-line 1))
7567 (beginning-of-line 0)))))
7569 (defun org-agenda-date-prompt (arg)
7570 "Change the date of this item. Date is prompted for, with default today.
7571 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7572 be used to request time specification in the time stamp."
7573 (interactive "P")
7574 (org-agenda-check-type t 'agenda 'timeline)
7575 (org-agenda-check-no-diary)
7576 (let* ((marker (or (org-get-at-bol 'org-marker)
7577 (org-agenda-error)))
7578 (buffer (marker-buffer marker))
7579 (pos (marker-position marker)))
7580 (org-with-remote-undo buffer
7581 (with-current-buffer buffer
7582 (widen)
7583 (goto-char pos)
7584 (if (not (org-at-timestamp-p t))
7585 (error "Cannot find time stamp"))
7586 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7587 (org-agenda-show-new-time marker org-last-changed-timestamp))
7588 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7590 (defun org-agenda-schedule (arg &optional time)
7591 "Schedule the item at point.
7592 ARG is passed through to `org-schedule'."
7593 (interactive "P")
7594 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7595 (org-agenda-check-no-diary)
7596 (let* ((marker (or (org-get-at-bol 'org-marker)
7597 (org-agenda-error)))
7598 (type (marker-insertion-type marker))
7599 (buffer (marker-buffer marker))
7600 (pos (marker-position marker))
7601 (org-insert-labeled-timestamps-at-point nil)
7603 (set-marker-insertion-type marker t)
7604 (org-with-remote-undo buffer
7605 (with-current-buffer buffer
7606 (widen)
7607 (goto-char pos)
7608 (setq ts (org-schedule arg time)))
7609 (org-agenda-show-new-time marker ts "S"))
7610 (message "Item scheduled for %s" ts)))
7612 (defun org-agenda-deadline (arg &optional time)
7613 "Schedule the item at point.
7614 ARG is passed through to `org-deadline'."
7615 (interactive "P")
7616 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7617 (org-agenda-check-no-diary)
7618 (let* ((marker (or (org-get-at-bol 'org-marker)
7619 (org-agenda-error)))
7620 (buffer (marker-buffer marker))
7621 (pos (marker-position marker))
7622 (org-insert-labeled-timestamps-at-point nil)
7624 (org-with-remote-undo buffer
7625 (with-current-buffer buffer
7626 (widen)
7627 (goto-char pos)
7628 (setq ts (org-deadline arg time)))
7629 (org-agenda-show-new-time marker ts "D"))
7630 (message "Deadline for this item set to %s" ts)))
7632 (defun org-agenda-action ()
7633 "Select entry for agenda action, or execute an agenda action.
7634 This command prompts for another letter. Valid inputs are:
7636 m Mark the entry at point for an agenda action
7637 s Schedule the marked entry to the date at the cursor
7638 d Set the deadline of the marked entry to the date at the cursor
7639 r Call `org-remember' with cursor date as the default date
7640 c Call `org-capture' with cursor date as the default date
7641 SPC Show marked entry in other window
7642 TAB Visit marked entry in other window
7644 The cursor may be at a date in the calendar, or in the Org agenda."
7645 (interactive)
7646 (let (ans)
7647 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7648 (setq ans (read-char-exclusive))
7649 (cond
7650 ((equal ans ?m)
7651 ;; Mark this entry
7652 (if (eq major-mode 'org-agenda-mode)
7653 (let ((m (or (org-get-at-bol 'org-hd-marker)
7654 (org-get-at-bol 'org-marker))))
7655 (if m
7656 (progn
7657 (move-marker org-agenda-action-marker
7658 (marker-position m) (marker-buffer m))
7659 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7660 (error "Don't know which entry to mark")))
7661 (error "This command works only in the agenda")))
7662 ((equal ans ?s)
7663 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7664 ((equal ans ?d)
7665 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7666 ((equal ans ?r)
7667 (org-agenda-do-action '(org-remember) t))
7668 ((equal ans ?c)
7669 (org-agenda-do-action '(org-capture) t))
7670 ((equal ans ?\ )
7671 (let ((cw (selected-window)))
7672 (org-switch-to-buffer-other-window
7673 (marker-buffer org-agenda-action-marker))
7674 (goto-char org-agenda-action-marker)
7675 (org-show-context 'agenda)
7676 (select-window cw)))
7677 ((equal ans ?\C-i)
7678 (org-switch-to-buffer-other-window
7679 (marker-buffer org-agenda-action-marker))
7680 (goto-char org-agenda-action-marker)
7681 (org-show-context 'agenda))
7682 (t (error "Invalid agenda action %c" ans)))))
7684 (defun org-agenda-do-action (form &optional current-buffer)
7685 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7686 (let ((org-overriding-default-time (org-get-cursor-date)))
7687 (if current-buffer
7688 (eval form)
7689 (if (not (marker-buffer org-agenda-action-marker))
7690 (error "No entry has been selected for agenda action")
7691 (with-current-buffer (marker-buffer org-agenda-action-marker)
7692 (save-excursion
7693 (save-restriction
7694 (widen)
7695 (goto-char org-agenda-action-marker)
7696 (eval form))))))))
7698 (defun org-agenda-clock-in (&optional arg)
7699 "Start the clock on the currently selected item."
7700 (interactive "P")
7701 (org-agenda-check-no-diary)
7702 (if (equal arg '(4))
7703 (org-clock-in arg)
7704 (let* ((marker (or (org-get-at-bol 'org-marker)
7705 (org-agenda-error)))
7706 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7707 marker))
7708 (pos (marker-position marker))
7709 newhead)
7710 (org-with-remote-undo (marker-buffer marker)
7711 (with-current-buffer (marker-buffer marker)
7712 (widen)
7713 (goto-char pos)
7714 (org-show-context 'agenda)
7715 (org-show-entry)
7716 (org-cycle-hide-drawers 'children)
7717 (org-clock-in arg)
7718 (setq newhead (org-get-heading)))
7719 (org-agenda-change-all-lines newhead hdmarker)))))
7721 (defun org-agenda-clock-out ()
7722 "Stop the currently running clock."
7723 (interactive)
7724 (unless (marker-buffer org-clock-marker)
7725 (error "No running clock"))
7726 (let ((marker (make-marker)) newhead)
7727 (org-with-remote-undo (marker-buffer org-clock-marker)
7728 (with-current-buffer (marker-buffer org-clock-marker)
7729 (save-excursion
7730 (save-restriction
7731 (widen)
7732 (goto-char org-clock-marker)
7733 (org-back-to-heading t)
7734 (move-marker marker (point))
7735 (org-clock-out)
7736 (setq newhead (org-get-heading))))))
7737 (org-agenda-change-all-lines newhead marker)
7738 (move-marker marker nil)))
7740 (defun org-agenda-clock-cancel (&optional arg)
7741 "Cancel the currently running clock."
7742 (interactive "P")
7743 (unless (marker-buffer org-clock-marker)
7744 (error "No running clock"))
7745 (org-with-remote-undo (marker-buffer org-clock-marker)
7746 (org-clock-cancel)))
7748 (defun org-agenda-clock-goto ()
7749 "Jump to the currently clocked in task within the agenda.
7750 If the currently clocked in task is not listed in the agenda
7751 buffer, display it in another window."
7752 (interactive)
7753 (let (pos)
7754 (mapc (lambda (o)
7755 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7756 (setq pos (overlay-start o))))
7757 (overlays-in (point-min) (point-max)))
7758 (cond (pos (goto-char pos))
7759 ;; If the currently clocked entry is not in the agenda
7760 ;; buffer, we visit it in another window:
7761 (org-clock-current-task
7762 (org-switch-to-buffer-other-window (org-clock-goto)))
7763 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7765 (defun org-agenda-diary-entry-in-org-file ()
7766 "Make a diary entry in the file `org-agenda-diary-file'."
7767 (let (d1 d2 char (text "") dp1 dp2)
7768 (if (equal (buffer-name) "*Calendar*")
7769 (setq d1 (calendar-cursor-to-date t)
7770 d2 (car calendar-mark-ring))
7771 (setq dp1 (get-text-property (point-at-bol) 'day))
7772 (unless dp1 (error "No date defined in current line"))
7773 (setq d1 (calendar-gregorian-from-absolute dp1)
7774 d2 (and (ignore-errors (mark))
7775 (save-excursion
7776 (goto-char (mark))
7777 (setq dp2 (get-text-property (point-at-bol) 'day)))
7778 (calendar-gregorian-from-absolute dp2))))
7779 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7780 (setq char (read-char-exclusive))
7781 (cond
7782 ((equal char ?d)
7783 (setq text (read-string "Day entry: "))
7784 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7785 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7786 ((equal char ?a)
7787 (setq d1 (list (car d1) (nth 1 d1)
7788 (read-number (format "Reference year [%d]: " (nth 2 d1))
7789 (nth 2 d1))))
7790 (setq text (read-string "Anniversary (use %d to show years): "))
7791 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7792 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7793 ((equal char ?b)
7794 (setq text (read-string "Block entry: "))
7795 (unless (and d1 d2 (not (equal d1 d2)))
7796 (error "No block of days selected"))
7797 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7798 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7799 ((equal char ?j)
7800 (org-switch-to-buffer-other-window
7801 (find-file-noselect org-agenda-diary-file))
7802 (require 'org-datetree)
7803 (org-datetree-find-date-create d1)
7804 (org-reveal t))
7805 (t (error "Invalid selection character `%c'" char)))))
7807 (defcustom org-agenda-insert-diary-strategy 'date-tree
7808 "Where in `org-agenda-diary-file' should new entries be added?
7809 Valid values:
7811 date-tree in the date tree, as child of the date
7812 top-level as top-level entries at the end of the file."
7813 :group 'org-agenda
7814 :type '(choice
7815 (const :tag "in a date tree" date-tree)
7816 (const :tag "as top level at end of file" top-level)))
7818 (defcustom org-agenda-insert-diary-extract-time nil
7819 "Non-nil means extract any time specification from the diary entry."
7820 :group 'org-agenda
7821 :type 'boolean)
7823 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7824 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7825 If TEXT is not empty, it will become the headline of the new entry, and
7826 the resulting entry will not be shown. When TEXT is empty, switch to
7827 `org-agenda-diary-file' and let the user finish the entry there."
7828 (let ((cw (current-window-configuration)))
7829 (org-switch-to-buffer-other-window
7830 (find-file-noselect org-agenda-diary-file))
7831 (widen)
7832 (goto-char (point-min))
7833 (cond
7834 ((eq type 'anniversary)
7835 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7836 (progn
7837 (or (org-on-heading-p t)
7838 (progn
7839 (outline-next-heading)
7840 (insert "* Anniversaries\n\n")
7841 (beginning-of-line -1)))))
7842 (outline-next-heading)
7843 (org-back-over-empty-lines)
7844 (backward-char 1)
7845 (insert "\n")
7846 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7847 (nth 2 d1) (car d1) (nth 1 d1) text)))
7848 ((eq type 'day)
7849 (let ((org-prefix-has-time t)
7850 (org-agenda-time-leading-zero t)
7851 fmt time time2)
7852 (if org-agenda-insert-diary-extract-time
7853 ;; Use org-format-agenda-item to parse text for a time-range and
7854 ;; remove it. FIXME: This is a hack, we should refactor
7855 ;; that function to make time extraction available separately
7856 (setq fmt (org-format-agenda-item nil text nil nil t)
7857 time (get-text-property 0 'time fmt)
7858 time2 (if (> (length time) 0)
7859 ;; split-string removes trailing ...... if
7860 ;; no end time given. First space
7861 ;; separates time from date.
7862 (concat " " (car (split-string time "\\.")))
7863 nil)
7864 text (get-text-property 0 'txt fmt)))
7865 (if (eq org-agenda-insert-diary-strategy 'top-level)
7866 (org-agenda-insert-diary-as-top-level text)
7867 (require 'org-datetree)
7868 (org-datetree-find-date-create d1)
7869 (org-agenda-insert-diary-make-new-entry text))
7870 (org-insert-time-stamp (org-time-from-absolute
7871 (calendar-absolute-from-gregorian d1))
7872 nil nil nil nil time2))
7873 (end-of-line 0))
7874 ((eq type 'block)
7875 (if (> (calendar-absolute-from-gregorian d1)
7876 (calendar-absolute-from-gregorian d2))
7877 (setq d1 (prog1 d2 (setq d2 d1))))
7878 (if (eq org-agenda-insert-diary-strategy 'top-level)
7879 (org-agenda-insert-diary-as-top-level text)
7880 (require 'org-datetree)
7881 (org-datetree-find-date-create d1)
7882 (org-agenda-insert-diary-make-new-entry text))
7883 (org-insert-time-stamp (org-time-from-absolute
7884 (calendar-absolute-from-gregorian d1)))
7885 (insert "--")
7886 (org-insert-time-stamp (org-time-from-absolute
7887 (calendar-absolute-from-gregorian d2)))
7888 (end-of-line 0)))
7889 (if (string-match "\\S-" text)
7890 (progn
7891 (set-window-configuration cw)
7892 (message "%s entry added to %s"
7893 (capitalize (symbol-name type))
7894 (abbreviate-file-name org-agenda-diary-file)))
7895 (org-reveal t)
7896 (message "Please finish entry here"))))
7898 (defun org-agenda-insert-diary-as-top-level (text)
7899 "Make new entry as a top-level entry at the end of the file.
7900 Add TEXT as headline, and position the cursor in the second line so that
7901 a timestamp can be added there."
7902 (widen)
7903 (goto-char (point-max))
7904 (or (bolp) (insert "\n"))
7905 (insert "* " text "\n")
7906 (if org-adapt-indentation (org-indent-to-column 2)))
7908 (defun org-agenda-insert-diary-make-new-entry (text)
7909 "Make new entry as last child of current entry.
7910 Add TEXT as headline, and position the cursor in the second line so that
7911 a timestamp can be added there."
7912 (let ((org-show-following-heading t)
7913 (org-show-siblings t)
7914 (org-show-hierarchy-above t)
7915 (org-show-entry-below t)
7916 col)
7917 (outline-next-heading)
7918 (org-back-over-empty-lines)
7919 (or (looking-at "[ \t]*$")
7920 (progn (insert "\n") (backward-char 1)))
7921 (org-insert-heading nil t)
7922 (org-do-demote)
7923 (setq col (current-column))
7924 (insert text "\n")
7925 (if org-adapt-indentation (org-indent-to-column col))
7926 (let ((org-show-following-heading t)
7927 (org-show-siblings t)
7928 (org-show-hierarchy-above t)
7929 (org-show-entry-below t))
7930 (org-show-context))))
7932 (defun org-agenda-diary-entry ()
7933 "Make a diary entry, like the `i' command from the calendar.
7934 All the standard commands work: block, weekly etc.
7935 When `org-agenda-diary-file' points to a file,
7936 `org-agenda-diary-entry-in-org-file' is called instead to create
7937 entries in that Org-mode file."
7938 (interactive)
7939 (org-agenda-check-type t 'agenda 'timeline)
7940 (if (not (eq org-agenda-diary-file 'diary-file))
7941 (org-agenda-diary-entry-in-org-file)
7942 (require 'diary-lib)
7943 (let* ((char (progn
7944 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7945 (read-char-exclusive)))
7946 (cmd (cdr (assoc char
7947 '((?d . insert-diary-entry)
7948 (?w . insert-weekly-diary-entry)
7949 (?m . insert-monthly-diary-entry)
7950 (?y . insert-yearly-diary-entry)
7951 (?a . insert-anniversary-diary-entry)
7952 (?b . insert-block-diary-entry)
7953 (?c . insert-cyclic-diary-entry)))))
7954 (oldf (symbol-function 'calendar-cursor-to-date))
7955 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7956 (point (point))
7957 (mark (or (mark t) (point))))
7958 (unless cmd
7959 (error "No command associated with <%c>" char))
7960 (unless (and (get-text-property point 'day)
7961 (or (not (equal ?b char))
7962 (get-text-property mark 'day)))
7963 (error "Don't know which date to use for diary entry"))
7964 ;; We implement this by hacking the `calendar-cursor-to-date' function
7965 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7966 (let ((calendar-mark-ring
7967 (list (calendar-gregorian-from-absolute
7968 (or (get-text-property mark 'day)
7969 (get-text-property point 'day))))))
7970 (unwind-protect
7971 (progn
7972 (fset 'calendar-cursor-to-date
7973 (lambda (&optional error dummy)
7974 (calendar-gregorian-from-absolute
7975 (get-text-property point 'day))))
7976 (call-interactively cmd))
7977 (fset 'calendar-cursor-to-date oldf))))))
7979 (defun org-agenda-execute-calendar-command (cmd)
7980 "Execute a calendar command from the agenda, with the date associated to
7981 the cursor position."
7982 (org-agenda-check-type t 'agenda 'timeline)
7983 (require 'diary-lib)
7984 (unless (get-text-property (point) 'day)
7985 (error "Don't know which date to use for calendar command"))
7986 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7987 (point (point))
7988 (date (calendar-gregorian-from-absolute
7989 (get-text-property point 'day)))
7990 ;; the following 2 vars are needed in the calendar
7991 (displayed-month (car date))
7992 (displayed-year (nth 2 date)))
7993 (unwind-protect
7994 (progn
7995 (fset 'calendar-cursor-to-date
7996 (lambda (&optional error dummy)
7997 (calendar-gregorian-from-absolute
7998 (get-text-property point 'day))))
7999 (call-interactively cmd))
8000 (fset 'calendar-cursor-to-date oldf))))
8002 (defun org-agenda-phases-of-moon ()
8003 "Display the phases of the moon for the 3 months around the cursor date."
8004 (interactive)
8005 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8007 (defun org-agenda-holidays ()
8008 "Display the holidays for the 3 months around the cursor date."
8009 (interactive)
8010 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8012 (defvar calendar-longitude)
8013 (defvar calendar-latitude)
8014 (defvar calendar-location-name)
8016 (defun org-agenda-sunrise-sunset (arg)
8017 "Display sunrise and sunset for the cursor date.
8018 Latitude and longitude can be specified with the variables
8019 `calendar-latitude' and `calendar-longitude'. When called with prefix
8020 argument, latitude and longitude will be prompted for."
8021 (interactive "P")
8022 (require 'solar)
8023 (let ((calendar-longitude (if arg nil calendar-longitude))
8024 (calendar-latitude (if arg nil calendar-latitude))
8025 (calendar-location-name
8026 (if arg "the given coordinates" calendar-location-name)))
8027 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8029 (defun org-agenda-goto-calendar ()
8030 "Open the Emacs calendar with the date at the cursor."
8031 (interactive)
8032 (org-agenda-check-type t 'agenda 'timeline)
8033 (let* ((day (or (get-text-property (point) 'day)
8034 (error "Don't know which date to open in calendar")))
8035 (date (calendar-gregorian-from-absolute day))
8036 (calendar-move-hook nil)
8037 (calendar-view-holidays-initially-flag nil)
8038 (calendar-view-diary-initially-flag nil))
8039 (calendar)
8040 (calendar-goto-date date)))
8042 ;;;###autoload
8043 (defun org-calendar-goto-agenda ()
8044 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8045 This is a command that has to be installed in `calendar-mode-map'."
8046 (interactive)
8047 (org-agenda-list nil (calendar-absolute-from-gregorian
8048 (calendar-cursor-to-date))
8049 nil))
8051 (defun org-agenda-convert-date ()
8052 (interactive)
8053 (org-agenda-check-type t 'agenda 'timeline)
8054 (let ((day (get-text-property (point) 'day))
8055 date s)
8056 (unless day
8057 (error "Don't know which date to convert"))
8058 (setq date (calendar-gregorian-from-absolute day))
8059 (setq s (concat
8060 "Gregorian: " (calendar-date-string date) "\n"
8061 "ISO: " (calendar-iso-date-string date) "\n"
8062 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8063 "Julian: " (calendar-julian-date-string date) "\n"
8064 "Astron. JD: " (calendar-astro-date-string date)
8065 " (Julian date number at noon UTC)\n"
8066 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8067 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8068 "French: " (calendar-french-date-string date) "\n"
8069 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8070 "Mayan: " (calendar-mayan-date-string date) "\n"
8071 "Coptic: " (calendar-coptic-date-string date) "\n"
8072 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8073 "Persian: " (calendar-persian-date-string date) "\n"
8074 "Chinese: " (calendar-chinese-date-string date) "\n"))
8075 (with-output-to-temp-buffer "*Dates*"
8076 (princ s))
8077 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8079 ;;; Bulk commands
8081 (defvar org-agenda-bulk-marked-entries nil
8082 "List of markers that refer to marked entries in the agenda.")
8084 (defun org-agenda-bulk-marked-p ()
8085 (eq (get-char-property (point-at-bol) 'type)
8086 'org-marked-entry-overlay))
8088 (defun org-agenda-bulk-mark (&optional arg)
8089 "Mark the entry at point for future bulk action."
8090 (interactive "p")
8091 (dotimes (i (max arg 1))
8092 (unless (org-get-at-bol 'org-agenda-diary-link)
8093 (let* ((m (org-get-at-bol 'org-hd-marker))
8095 (unless (org-agenda-bulk-marked-p)
8096 (unless m (error "Nothing to mark at point"))
8097 (push m org-agenda-bulk-marked-entries)
8098 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8099 (org-overlay-display ov "> "
8100 (org-get-todo-face "TODO")
8101 'evaporate)
8102 (overlay-put ov 'type 'org-marked-entry-overlay))
8103 (beginning-of-line 2)
8104 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8105 (beginning-of-line 2))
8106 (message "%d entries marked for bulk action"
8107 (length org-agenda-bulk-marked-entries))))))
8109 (defun org-agenda-bulk-mark-regexp (regexp)
8110 "Mark entries match REGEXP."
8111 (interactive "sMark entries matching regexp: ")
8112 (let (entries-marked)
8113 (save-excursion
8114 (goto-char (point-min))
8115 (goto-char (next-single-property-change (point) 'txt))
8116 (while (re-search-forward regexp nil t)
8117 (when (string-match regexp (get-text-property (point) 'txt))
8118 (setq entries-marked (+ entries-marked 1))
8119 (call-interactively 'org-agenda-bulk-mark))))
8120 (if (not entries-marked)
8121 (message "No entry matching this regexp."))))
8123 (defun org-agenda-bulk-unmark ()
8124 "Unmark the entry at point for future bulk action."
8125 (interactive)
8126 (when (org-agenda-bulk-marked-p)
8127 (org-agenda-bulk-remove-overlays
8128 (point-at-bol) (+ 2 (point-at-bol)))
8129 (setq org-agenda-bulk-marked-entries
8130 (delete (org-get-at-bol 'org-hd-marker)
8131 org-agenda-bulk-marked-entries)))
8132 (beginning-of-line 2)
8133 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8134 (beginning-of-line 2))
8135 (message "%d entries marked for bulk action"
8136 (length org-agenda-bulk-marked-entries)))
8138 (defun org-agenda-bulk-toggle ()
8139 "Toggle marking the entry at point for bulk action."
8140 (interactive)
8141 (if (org-agenda-bulk-marked-p)
8142 (org-agenda-bulk-unmark)
8143 (org-agenda-bulk-mark)))
8145 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8146 "Remove the mark overlays between BEG and END in the agenda buffer.
8147 BEG and END default to the buffer limits.
8149 This only removes the overlays, it does not remove the markers
8150 from the list in `org-agenda-bulk-marked-entries'."
8151 (interactive)
8152 (mapc (lambda (ov)
8153 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8154 (delete-overlay ov)))
8155 (overlays-in (or beg (point-min)) (or end (point-max)))))
8157 (defun org-agenda-bulk-remove-all-marks ()
8158 "Remove all marks in the agenda buffer.
8159 This will remove the markers, and the overlays."
8160 (interactive)
8161 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8162 (setq org-agenda-bulk-marked-entries nil)
8163 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8165 (defun org-agenda-bulk-action (&optional arg)
8166 "Execute an remote-editing action on all marked entries.
8167 The prefix arg is passed through to the command if possible."
8168 (interactive "P")
8169 ;; Make sure we have markers, and only valid ones
8170 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8171 (mapc
8172 (lambda (m)
8173 (unless (and (markerp m)
8174 (marker-buffer m)
8175 (buffer-live-p (marker-buffer m))
8176 (marker-position m))
8177 (error "Marker %s for bulk command is invalid" m)))
8178 org-agenda-bulk-marked-entries)
8180 ;; Prompt for the bulk command
8181 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8182 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8183 (when org-agenda-bulk-custom-functions
8184 (concat " Custom: ["
8185 (mapconcat (lambda(f) (char-to-string (car f)))
8186 org-agenda-bulk-custom-functions "")
8187 "]"))))
8188 (let* ((action (read-char-exclusive))
8189 (org-log-refile (if org-log-refile 'time nil))
8190 (entries (reverse org-agenda-bulk-marked-entries))
8191 redo-at-end
8192 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8193 (cond
8194 ((equal action ?$)
8195 (setq cmd '(org-agenda-archive)))
8197 ((equal action ?A)
8198 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8200 ((member action '(?r ?w))
8201 (setq rfloc (org-refile-get-location
8202 "Refile to"
8203 (marker-buffer (car org-agenda-bulk-marked-entries))
8204 org-refile-allow-creating-parent-nodes))
8205 (if (nth 3 rfloc)
8206 (setcar (nthcdr 3 rfloc)
8207 (move-marker (make-marker) (nth 3 rfloc)
8208 (or (get-file-buffer (nth 1 rfloc))
8209 (find-buffer-visiting (nth 1 rfloc))
8210 (error "This should not happen")))))
8212 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8213 redo-at-end t))
8215 ((equal action ?t)
8216 (setq state (org-icompleting-read
8217 "Todo state: "
8218 (with-current-buffer (marker-buffer (car entries))
8219 (mapcar 'list org-todo-keywords-1))))
8220 (setq cmd `(let ((org-inhibit-blocking t)
8221 (org-inhibit-logging 'note))
8222 (org-agenda-todo ,state))))
8224 ((memq action '(?- ?+))
8225 (setq tag (org-icompleting-read
8226 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8227 (with-current-buffer (marker-buffer (car entries))
8228 (delq nil
8229 (mapcar (lambda (x)
8230 (if (stringp (car x)) x)) org-tag-alist)))))
8231 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8233 ((memq action '(?s ?d))
8234 (let* ((date (unless arg
8235 (org-read-date
8236 nil nil nil
8237 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8238 (ans (if arg nil org-read-date-final-answer))
8239 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8240 (setq cmd `(let* ((bound (fboundp 'read-string))
8241 (old (and bound (symbol-function 'read-string))))
8242 (unwind-protect
8243 (progn
8244 (fset 'read-string (lambda (&rest ignore) ,ans))
8245 (eval '(,c1 arg)))
8246 (if bound
8247 (fset 'read-string old)
8248 (fmakunbound 'read-string)))))))
8250 ((equal action ?S)
8251 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8252 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8253 (let ((days (read-number
8254 (format "Scatter tasks across how many %sdays: "
8255 (if arg "week" "")) 7)))
8256 (setq cmd
8257 `(let ((distance (1+ (random ,days))))
8258 (if arg
8259 (let ((dist distance)
8260 (day-of-week
8261 (calendar-day-of-week
8262 (calendar-gregorian-from-absolute (org-today)))))
8263 (dotimes (i (1+ dist))
8264 (while (member day-of-week org-agenda-weekend-days)
8265 (incf distance)
8266 (incf day-of-week)
8267 (if (= day-of-week 7)
8268 (setq day-of-week 0)))
8269 (incf day-of-week)
8270 (if (= day-of-week 7)
8271 (setq day-of-week 0)))))
8272 ;; silently fail when try to replan a sexp entry
8273 (condition-case nil
8274 (let* ((date (calendar-gregorian-from-absolute
8275 (+ (org-today) distance)))
8276 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8277 (nth 2 date))))
8278 (org-agenda-schedule nil time))
8279 (error nil)))))))
8281 ((assoc action org-agenda-bulk-custom-functions)
8282 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8283 redo-at-end t))
8285 ((equal action ?f)
8286 (setq cmd (list (intern
8287 (org-icompleting-read "Function: "
8288 obarray 'fboundp t nil nil)))))
8290 (t (error "Invalid bulk action")))
8292 ;; Sort the markers, to make sure that parents are handled before children
8293 (setq entries (sort entries
8294 (lambda (a b)
8295 (cond
8296 ((equal (marker-buffer a) (marker-buffer b))
8297 (< (marker-position a) (marker-position b)))
8299 (string< (buffer-name (marker-buffer a))
8300 (buffer-name (marker-buffer b))))))))
8302 ;; Now loop over all markers and apply cmd
8303 (while (setq e (pop entries))
8304 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8305 (if (not pos)
8306 (progn (message "Skipping removed entry at %s" e)
8307 (setq cntskip (1+ cntskip)))
8308 (goto-char pos)
8309 (eval cmd)
8310 (setq org-agenda-bulk-marked-entries
8311 (delete e org-agenda-bulk-marked-entries))
8312 (setq cnt (1+ cnt))))
8313 (setq org-agenda-bulk-marked-entries nil)
8314 (org-agenda-bulk-remove-all-marks)
8315 (when redo-at-end (org-agenda-redo))
8316 (message "Acted on %d entries%s"
8318 (if (= cntskip 0)
8320 (format ", skipped %d (disappeared before their turn)"
8321 cntskip)))))
8323 ;;; Flagging notes
8325 (defun org-agenda-show-the-flagging-note ()
8326 "Display the flagging note in the other window.
8327 When called a second time in direct sequence, offer to remove the FLAGGING
8328 tag and (if present) the flagging note."
8329 (interactive)
8330 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8331 (win (selected-window))
8332 note heading newhead)
8333 (unless hdmarker
8334 (error "No linked entry at point"))
8335 (if (and (eq this-command last-command)
8336 (y-or-n-p "Unflag and remove any flagging note? "))
8337 (progn
8338 (org-agenda-remove-flag hdmarker)
8339 (let ((win (get-buffer-window "*Flagging Note*")))
8340 (and win (delete-window win)))
8341 (message "Entry unflaged"))
8342 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8343 (unless note
8344 (error "No flagging note"))
8345 (org-kill-new note)
8346 (org-switch-to-buffer-other-window "*Flagging Note*")
8347 (erase-buffer)
8348 (insert note)
8349 (goto-char (point-min))
8350 (while (re-search-forward "\\\\n" nil t)
8351 (replace-match "\n" t t))
8352 (goto-char (point-min))
8353 (select-window win)
8354 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8356 (defun org-agenda-remove-flag (marker)
8357 "Remove the FLAGGED tag and any flagging note in the entry."
8358 (let (newhead)
8359 (org-with-point-at marker
8360 (org-toggle-tag "FLAGGED" 'off)
8361 (org-entry-delete nil "THEFLAGGINGNOTE")
8362 (setq newhead (org-get-heading)))
8363 (org-agenda-change-all-lines newhead marker)
8364 (message "Entry unflaged")))
8366 (defun org-agenda-get-any-marker (&optional pos)
8367 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8368 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8370 ;;; Appointment reminders
8372 (defvar appt-time-msg-list)
8374 ;;;###autoload
8375 (defun org-agenda-to-appt (&optional refresh filter)
8376 "Activate appointments found in `org-agenda-files'.
8377 With a \\[universal-argument] prefix, refresh the list of
8378 appointments.
8380 If FILTER is t, interactively prompt the user for a regular
8381 expression, and filter out entries that don't match it.
8383 If FILTER is a string, use this string as a regular expression
8384 for filtering entries out.
8386 FILTER can also be an alist with the car of each cell being
8387 either 'headline or 'category. For example:
8389 '((headline \"IMPORTANT\")
8390 (category \"Work\"))
8392 will only add headlines containing IMPORTANT or headlines
8393 belonging to the \"Work\" category."
8394 (interactive "P")
8395 (if refresh (setq appt-time-msg-list nil))
8396 (if (eq filter t)
8397 (setq filter (read-from-minibuffer "Regexp filter: ")))
8398 (let* ((cnt 0) ; count added events
8399 (org-agenda-new-buffers nil)
8400 (org-deadline-warning-days 0)
8401 ;; Do not use `org-today' here because appt only takes
8402 ;; time and without date as argument, so it may pass wrong
8403 ;; information otherwise
8404 (today (org-date-to-gregorian
8405 (time-to-days (current-time))))
8406 (org-agenda-restrict nil)
8407 (files (org-agenda-files 'unrestricted)) entries file)
8408 ;; Get all entries which may contain an appt
8409 (org-prepare-agenda-buffers files)
8410 (while (setq file (pop files))
8411 (setq entries
8412 (append entries
8413 (org-agenda-get-day-entries
8414 file today :timestamp :scheduled :deadline))))
8415 (setq entries (delq nil entries))
8416 ;; Map thru entries and find if we should filter them out
8417 (mapc
8418 (lambda(x)
8419 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8420 (cat (get-text-property 1 'org-category x))
8421 (tod (get-text-property 1 'time-of-day x))
8422 (ok (or (null filter)
8423 (and (stringp filter) (string-match filter evt))
8424 (and (listp filter)
8425 (or (string-match
8426 (cadr (assoc 'category filter)) cat)
8427 (string-match
8428 (cadr (assoc 'headline filter)) evt))))))
8429 ;; FIXME: Shall we remove text-properties for the appt text?
8430 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8431 (when (and ok tod)
8432 (setq tod (concat "00" (number-to-string tod))
8433 tod (when (string-match
8434 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8435 (concat (match-string 1 tod) ":"
8436 (match-string 2 tod))))
8437 (appt-add tod evt)
8438 (setq cnt (1+ cnt))))) entries)
8439 (org-release-buffers org-agenda-new-buffers)
8440 (if (eq cnt 0)
8441 (message "No event to add")
8442 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8444 (defun org-agenda-todayp (date)
8445 "Does DATE mean today, when considering `org-extend-today-until'?"
8446 (let ((today (org-today))
8447 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8448 date)))
8449 (eq date today)))
8451 (defun org-agenda-todo-yesterday (&optional arg)
8452 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8453 (interactive "P")
8454 (let* ((hour (third (decode-time
8455 (org-current-time))))
8456 (org-extend-today-until (1+ hour)))
8457 (org-agenda-todo arg)))
8459 (provide 'org-agenda)
8461 ;;; org-agenda.el ends here