Introduced org-lparse-begin-collect and org-lparse-end-collect
[org-mode.git] / lisp / org-agenda.el
blobb1fa5f5075542850f6050698a6517ccb846c695b
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 (with-current-buffer (marker-buffer marker)
3202 (save-excursion (goto-char marker)
3203 (org-entry-blocked-p))))
3204 (if org-blocked-by-checkboxes (setq invis1 nil))
3205 (setq b (if invis1
3206 (max (point-min) (1- (point-at-bol)))
3207 (point-at-bol))
3208 e (point-at-eol)
3209 ov (make-overlay b e))
3210 (if invis1
3211 (overlay-put ov 'invisible t)
3212 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3213 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3215 (defvar org-agenda-skip-function nil
3216 "Function to be called at each match during agenda construction.
3217 If this function returns nil, the current match should not be skipped.
3218 Otherwise, the function must return a position from where the search
3219 should be continued.
3220 This may also be a Lisp form, it will be evaluated.
3221 Never set this variable using `setq' or so, because then it will apply
3222 to all future agenda commands. If you do want a global skipping condition,
3223 use the option `org-agenda-skip-function-global' instead.
3224 The correct usage for `org-agenda-skip-function' is to bind it with
3225 `let' to scope it dynamically into the agenda-constructing command.
3226 A good way to set it is through options in `org-agenda-custom-commands'.")
3228 (defun org-agenda-skip ()
3229 "Throw to `:skip' in places that should be skipped.
3230 Also moves point to the end of the skipped region, so that search can
3231 continue from there."
3232 (let ((p (point-at-bol)) to)
3233 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3234 (get-text-property p :org-archived)
3235 (org-end-of-subtree t)
3236 (throw :skip t))
3237 (and org-agenda-skip-comment-trees
3238 (get-text-property p :org-comment)
3239 (org-end-of-subtree t)
3240 (throw :skip t))
3241 (if (equal (char-after p) ?#) (throw :skip t))
3242 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3243 (org-agenda-skip-eval org-agenda-skip-function)))
3244 (goto-char to)
3245 (throw :skip t))))
3247 (defun org-agenda-skip-eval (form)
3248 "If FORM is a function or a list, call (or eval) is and return result.
3249 `save-excursion' and `save-match-data' are wrapped around the call, so point
3250 and match data are returned to the previous state no matter what these
3251 functions do."
3252 (let (fp)
3253 (and form
3254 (or (setq fp (functionp form))
3255 (consp form))
3256 (save-excursion
3257 (save-match-data
3258 (if fp
3259 (funcall form)
3260 (eval form)))))))
3262 (defvar org-agenda-markers nil
3263 "List of all currently active markers created by `org-agenda'.")
3264 (defvar org-agenda-last-marker-time (org-float-time)
3265 "Creation time of the last agenda marker.")
3267 (defun org-agenda-new-marker (&optional pos)
3268 "Return a new agenda marker.
3269 Org-mode keeps a list of these markers and resets them when they are
3270 no longer in use."
3271 (let ((m (copy-marker (or pos (point)))))
3272 (setq org-agenda-last-marker-time (org-float-time))
3273 (push m org-agenda-markers)
3276 (defun org-agenda-reset-markers ()
3277 "Reset markers created by `org-agenda'."
3278 (while org-agenda-markers
3279 (move-marker (pop org-agenda-markers) nil)))
3281 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3282 "Save relative positions of markers in region."
3283 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3284 org-agenda-markers))
3286 ;;; Entry text mode
3288 (defun org-agenda-entry-text-show-here ()
3289 "Add some text from the entry as context to the current line."
3290 (let (m txt o)
3291 (setq m (org-get-at-bol 'org-hd-marker))
3292 (unless (marker-buffer m)
3293 (error "No marker points to an entry here"))
3294 (setq txt (concat "\n" (org-no-properties
3295 (org-agenda-get-some-entry-text
3296 m org-agenda-entry-text-maxlines " > "))))
3297 (when (string-match "\\S-" txt)
3298 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3299 (overlay-put o 'evaporate t)
3300 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3301 (overlay-put o 'after-string txt))))
3303 (defun org-agenda-entry-text-show ()
3304 "Add entry context for all agenda lines."
3305 (interactive)
3306 (save-excursion
3307 (goto-char (point-max))
3308 (beginning-of-line 1)
3309 (while (not (bobp))
3310 (when (org-get-at-bol 'org-hd-marker)
3311 (org-agenda-entry-text-show-here))
3312 (beginning-of-line 0))))
3314 (defun org-agenda-entry-text-hide ()
3315 "Remove any shown entry context."
3316 (delq nil
3317 (mapcar (lambda (o)
3318 (if (eq (overlay-get o 'org-overlay-type)
3319 'agenda-entry-content)
3320 (progn (delete-overlay o) t)))
3321 (overlays-in (point-min) (point-max)))))
3323 (defun org-agenda-get-day-face (date)
3324 "Return the face DATE should be displayed with."
3325 (or (and (functionp org-agenda-day-face-function)
3326 (funcall org-agenda-day-face-function date))
3327 (cond ((org-agenda-todayp date)
3328 'org-agenda-date-today)
3329 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3330 'org-agenda-date-weekend)
3331 (t 'org-agenda-date))))
3333 ;;; Agenda timeline
3335 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3337 (defun org-timeline (&optional dotodo)
3338 "Show a time-sorted view of the entries in the current org file.
3339 Only entries with a time stamp of today or later will be listed. With
3340 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3341 under the current date.
3342 If the buffer contains an active region, only check the region for
3343 dates."
3344 (interactive "P")
3345 (org-compile-prefix-format 'timeline)
3346 (org-set-sorting-strategy 'timeline)
3347 (let* ((dopast t)
3348 (doclosed org-agenda-show-log)
3349 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3350 (current-buffer))))
3351 (date (calendar-current-date))
3352 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3353 (end (if (org-region-active-p) (region-end) (point-max)))
3354 (day-numbers (org-get-all-dates beg end 'no-ranges
3355 t doclosed ; always include today
3356 org-timeline-show-empty-dates))
3357 (org-deadline-warning-days 0)
3358 (org-agenda-only-exact-dates t)
3359 (today (org-today))
3360 (past t)
3361 args
3362 s e rtn d emptyp)
3363 (setq org-agenda-redo-command
3364 (list 'progn
3365 (list 'org-switch-to-buffer-other-window (current-buffer))
3366 (list 'org-timeline (list 'quote dotodo))))
3367 (if (not dopast)
3368 ;; Remove past dates from the list of dates.
3369 (setq day-numbers (delq nil (mapcar (lambda(x)
3370 (if (>= x today) x nil))
3371 day-numbers))))
3372 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3373 (if doclosed (push :closed args))
3374 (push :timestamp args)
3375 (push :deadline args)
3376 (push :scheduled args)
3377 (push :sexp args)
3378 (if dotodo (push :todo args))
3379 (insert "Timeline of file " entry "\n")
3380 (add-text-properties (point-min) (point)
3381 (list 'face 'org-agenda-structure))
3382 (org-agenda-mark-header-line (point-min))
3383 (while (setq d (pop day-numbers))
3384 (if (and (listp d) (eq (car d) :omitted))
3385 (progn
3386 (setq s (point))
3387 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3388 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3389 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3390 (if (and (>= d today)
3391 dopast
3392 past)
3393 (progn
3394 (setq past nil)
3395 (insert (make-string 79 ?-) "\n")))
3396 (setq date (calendar-gregorian-from-absolute d))
3397 (setq s (point))
3398 (setq rtn (and (not emptyp)
3399 (apply 'org-agenda-get-day-entries entry
3400 date args)))
3401 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3402 (progn
3403 (insert
3404 (if (stringp org-agenda-format-date)
3405 (format-time-string org-agenda-format-date
3406 (org-time-from-absolute date))
3407 (funcall org-agenda-format-date date))
3408 "\n")
3409 (put-text-property s (1- (point)) 'face
3410 (org-agenda-get-day-face date))
3411 (put-text-property s (1- (point)) 'org-date-line t)
3412 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3413 (if (equal d today)
3414 (put-text-property s (1- (point)) 'org-today t))
3415 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3416 (put-text-property s (1- (point)) 'day d)))))
3417 (goto-char (point-min))
3418 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3419 (point-min)))
3420 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3421 (org-finalize-agenda)
3422 (setq buffer-read-only t)))
3424 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3425 "Return a list of all relevant day numbers from BEG to END buffer positions.
3426 If NO-RANGES is non-nil, include only the start and end dates of a range,
3427 not every single day in the range. If FORCE-TODAY is non-nil, make
3428 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3429 inactive time stamps (those in square brackets) are included.
3430 When EMPTY is non-nil, also include days without any entries."
3431 (let ((re (concat
3432 (if pre-re pre-re "")
3433 (if inactive org-ts-regexp-both org-ts-regexp)))
3434 dates dates1 date day day1 day2 ts1 ts2)
3435 (if force-today
3436 (setq dates (list (org-today))))
3437 (save-excursion
3438 (goto-char beg)
3439 (while (re-search-forward re end t)
3440 (setq day (time-to-days (org-time-string-to-time
3441 (substring (match-string 1) 0 10))))
3442 (or (memq day dates) (push day dates)))
3443 (unless no-ranges
3444 (goto-char beg)
3445 (while (re-search-forward org-tr-regexp end t)
3446 (setq ts1 (substring (match-string 1) 0 10)
3447 ts2 (substring (match-string 2) 0 10)
3448 day1 (time-to-days (org-time-string-to-time ts1))
3449 day2 (time-to-days (org-time-string-to-time ts2)))
3450 (while (< (setq day1 (1+ day1)) day2)
3451 (or (memq day1 dates) (push day1 dates)))))
3452 (setq dates (sort dates '<))
3453 (when empty
3454 (while (setq day (pop dates))
3455 (setq day2 (car dates))
3456 (push day dates1)
3457 (when (and day2 empty)
3458 (if (or (eq empty t)
3459 (and (numberp empty) (<= (- day2 day) empty)))
3460 (while (< (setq day (1+ day)) day2)
3461 (push (list day) dates1))
3462 (push (cons :omitted (- day2 day)) dates1))))
3463 (setq dates (nreverse dates1)))
3464 dates)))
3466 ;;; Agenda Daily/Weekly
3468 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3469 "Start day for the agenda view.
3470 Custom commands can set this variable in the options section.")
3471 (defvar org-starting-day nil) ; local variable in the agenda buffer
3472 (defvar org-agenda-current-span nil
3473 "The current span used in the agenda view.") ; local variable in the agenda buffer
3474 (defvar org-arg-loc nil) ; local variable
3476 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3477 "List of types searched for when creating the daily/weekly agenda.
3478 This variable is a list of symbols that controls the types of
3479 items that appear in the daily/weekly agenda. Allowed symbols in this
3480 list are are
3482 :timestamp List items containing a date stamp or date range matching
3483 the selected date. This includes sexp entries in
3484 angular brackets.
3486 :sexp List entries resulting from plain diary-like sexps.
3488 :deadline List deadline due on that date. When the date is today,
3489 also list any deadlines past due, or due within
3490 `org-deadline-warning-days'. `:deadline' must appear before
3491 `:scheduled' if the setting of
3492 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3493 any effect.
3495 :scheduled List all items which are scheduled for the given date.
3496 The diary for *today* also contains items which were
3497 scheduled earlier and are not yet marked DONE.
3499 By default, all four types are turned on.
3501 Never set this variable globally using `setq', because then it
3502 will apply to all future agenda commands. Instead, bind it with
3503 `let' to scope it dynamically into the agenda-constructing
3504 command. A good way to set it is through options in
3505 `org-agenda-custom-commands'. For a more flexible (though
3506 somewhat less efficient) way of determining what is included in
3507 the daily/weekly agenda, see `org-agenda-skip-function'.")
3509 ;;;###autoload
3510 (defun org-agenda-list (&optional arg start-day span)
3511 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3512 The view will be for the current day or week, but from the overview buffer
3513 you will be able to go to other days/weeks.
3515 With a numeric prefix argument in an interactive call, the agenda will
3516 span ARG days. Lisp programs should instead specify SPAN to change
3517 the number of days. SPAN defaults to `org-agenda-span'.
3519 START-DAY defaults to TODAY, or to the most recent match for the weekday
3520 given in `org-agenda-start-on-weekday'."
3521 (interactive "P")
3522 (if (and (integerp arg) (> arg 0))
3523 (setq span arg arg nil))
3524 (setq start-day (or start-day org-agenda-start-day))
3525 (if org-agenda-overriding-arguments
3526 (setq arg (car org-agenda-overriding-arguments)
3527 start-day (nth 1 org-agenda-overriding-arguments)
3528 span (nth 2 org-agenda-overriding-arguments)))
3529 (if (stringp start-day)
3530 ;; Convert to an absolute day number
3531 (setq start-day (time-to-days (org-read-date nil t start-day))))
3532 (setq org-agenda-last-arguments (list arg start-day span))
3533 (org-compile-prefix-format 'agenda)
3534 (org-set-sorting-strategy 'agenda)
3535 (let* ((span (org-agenda-ndays-to-span
3536 (or span org-agenda-ndays org-agenda-span)))
3537 (today (org-today))
3538 (sd (or start-day today))
3539 (ndays (org-agenda-span-to-ndays span sd))
3540 (org-agenda-start-on-weekday
3541 (if (eq ndays 7)
3542 org-agenda-start-on-weekday))
3543 (thefiles (org-agenda-files nil 'ifmode))
3544 (files thefiles)
3545 (start (if (or (null org-agenda-start-on-weekday)
3546 (< ndays 7))
3548 (let* ((nt (calendar-day-of-week
3549 (calendar-gregorian-from-absolute sd)))
3550 (n1 org-agenda-start-on-weekday)
3551 (d (- nt n1)))
3552 (- sd (+ (if (< d 0) 7 0) d)))))
3553 (day-numbers (list start))
3554 (day-cnt 0)
3555 (inhibit-redisplay (not debug-on-error))
3556 s e rtn rtnall file date d start-pos end-pos todayp
3557 clocktable-start clocktable-end filter)
3558 (setq org-agenda-redo-command
3559 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3560 (dotimes (n (1- ndays))
3561 (push (1+ (car day-numbers)) day-numbers))
3562 (setq day-numbers (nreverse day-numbers))
3563 (setq clocktable-start (car day-numbers)
3564 clocktable-end (1+ (or (org-last day-numbers) 0)))
3565 (org-prepare-agenda "Day/Week")
3566 (org-set-local 'org-starting-day (car day-numbers))
3567 (org-set-local 'org-arg-loc arg)
3568 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3569 (unless org-agenda-compact-blocks
3570 (let* ((d1 (car day-numbers))
3571 (d2 (org-last day-numbers))
3572 (w1 (org-days-to-iso-week d1))
3573 (w2 (org-days-to-iso-week d2)))
3574 (setq s (point))
3575 (if org-agenda-overriding-header
3576 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3577 nil 'face 'org-agenda-structure) "\n")
3578 (insert (org-agenda-span-name span)
3579 "-agenda"
3580 (if (< (- d2 d1) 350)
3581 (if (= w1 w2)
3582 (format " (W%02d)" w1)
3583 (format " (W%02d-W%02d)" w1 w2))
3585 ":\n")))
3586 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3587 'org-date-line t))
3588 (org-agenda-mark-header-line s))
3589 (while (setq d (pop day-numbers))
3590 (setq date (calendar-gregorian-from-absolute d)
3591 s (point))
3592 (if (or (setq todayp (= d today))
3593 (and (not start-pos) (= d sd)))
3594 (setq start-pos (point))
3595 (if (and start-pos (not end-pos))
3596 (setq end-pos (point))))
3597 (setq files thefiles
3598 rtnall nil)
3599 (while (setq file (pop files))
3600 (catch 'nextfile
3601 (org-check-agenda-file file)
3602 (let ((org-agenda-entry-types org-agenda-entry-types))
3603 (unless org-agenda-include-deadlines
3604 (setq org-agenda-entry-types
3605 (delq :deadline org-agenda-entry-types)))
3606 (cond
3607 ((memq org-agenda-show-log '(only clockcheck))
3608 (setq rtn (org-agenda-get-day-entries
3609 file date :closed)))
3610 (org-agenda-show-log
3611 (setq rtn (apply 'org-agenda-get-day-entries
3612 file date
3613 (append '(:closed) org-agenda-entry-types))))
3615 (setq rtn (apply 'org-agenda-get-day-entries
3616 file date
3617 org-agenda-entry-types)))))
3618 (setq rtnall (append rtnall rtn))))
3619 (if org-agenda-include-diary
3620 (let ((org-agenda-search-headline-for-time t))
3621 (require 'diary-lib)
3622 (setq rtn (org-get-entries-from-diary date))
3623 (setq rtnall (append rtnall rtn))))
3624 (if (or rtnall org-agenda-show-all-dates)
3625 (progn
3626 (setq day-cnt (1+ day-cnt))
3627 (insert
3628 (if (stringp org-agenda-format-date)
3629 (format-time-string org-agenda-format-date
3630 (org-time-from-absolute date))
3631 (funcall org-agenda-format-date date))
3632 "\n")
3633 (put-text-property s (1- (point)) 'face
3634 (org-agenda-get-day-face date))
3635 (put-text-property s (1- (point)) 'org-date-line t)
3636 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3637 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3638 (when todayp
3639 (put-text-property s (1- (point)) 'org-today t))
3640 (if rtnall (insert
3641 (org-finalize-agenda-entries
3642 (org-agenda-add-time-grid-maybe
3643 rtnall ndays todayp))
3644 "\n"))
3645 (put-text-property s (1- (point)) 'day d)
3646 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3647 (when (and org-agenda-clockreport-mode clocktable-start)
3648 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3649 ;; the above line is to ensure the restricted range!
3650 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3651 tbl)
3652 (setq p (org-plist-delete p :block))
3653 (setq p (plist-put p :tstart clocktable-start))
3654 (setq p (plist-put p :tend clocktable-end))
3655 (setq p (plist-put p :scope 'agenda))
3656 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3657 (setq filter (or org-agenda-filter-while-redo
3658 (get 'org-agenda-filter :preset-filter))))
3659 (setq p (plist-put p :tags (mapconcat (lambda (x)
3660 (if (string-match "[<>=]" x)
3663 filter ""))))
3664 (setq tbl (apply 'org-get-clocktable p))
3665 (insert tbl)))
3666 (goto-char (point-min))
3667 (or org-agenda-multi (org-fit-agenda-window))
3668 (unless (and (pos-visible-in-window-p (point-min))
3669 (pos-visible-in-window-p (point-max)))
3670 (goto-char (1- (point-max)))
3671 (recenter -1)
3672 (if (not (pos-visible-in-window-p (or start-pos 1)))
3673 (progn
3674 (goto-char (or start-pos 1))
3675 (recenter 1))))
3676 (goto-char (or start-pos 1))
3677 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3678 (if (eq org-agenda-show-log 'clockcheck)
3679 (org-agenda-show-clocking-issues))
3680 (org-finalize-agenda)
3681 (setq buffer-read-only t)
3682 (message "")))
3684 (defun org-agenda-ndays-to-span (n)
3685 "Return a span symbol for a span of N days, or N if none matches."
3686 (cond ((symbolp n) n)
3687 ((= n 1) 'day)
3688 ((= n 7) 'week)
3689 (t n)))
3691 (defun org-agenda-span-to-ndays (span start-day)
3692 "Return ndays from SPAN starting at START-DAY."
3693 (cond ((numberp span) span)
3694 ((eq span 'day) 1)
3695 ((eq span 'week) 7)
3696 ((eq span 'month)
3697 (let ((date (calendar-gregorian-from-absolute start-day)))
3698 (calendar-last-day-of-month (car date) (caddr date))))
3699 ((eq span 'year)
3700 (let ((date (calendar-gregorian-from-absolute start-day)))
3701 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3703 (defun org-agenda-span-name (span)
3704 "Return a SPAN name."
3705 (if (null span)
3707 (if (symbolp span)
3708 (capitalize (symbol-name span))
3709 (format "%d days" span))))
3711 ;;; Agenda word search
3713 (defvar org-agenda-search-history nil)
3714 (defvar org-todo-only nil)
3716 (defvar org-search-syntax-table nil
3717 "Special syntax table for org-mode search.
3718 In this table, we have single quotes not as word constituents, to
3719 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3721 (defun org-search-syntax-table ()
3722 (unless org-search-syntax-table
3723 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3724 (modify-syntax-entry ?' "." org-search-syntax-table)
3725 (modify-syntax-entry ?` "." org-search-syntax-table))
3726 org-search-syntax-table)
3728 (defvar org-agenda-last-search-view-search-was-boolean nil)
3730 ;;;###autoload
3731 (defun org-search-view (&optional todo-only string edit-at)
3732 "Show all entries that contain a phrase or words or regular expressions.
3734 With optional prefix argument TODO-ONLY, only consider entries that are
3735 TODO entries. The argument STRING can be used to pass a default search
3736 string into this function. If EDIT-AT is non-nil, it means that the
3737 user should get a chance to edit this string, with cursor at position
3738 EDIT-AT.
3740 The search string can be viewed either as a phrase that should be found as
3741 is, or it can be broken into a number of snippets, each of which must match
3742 in a Boolean way to select an entry. The default depends on the variable
3743 `org-agenda-search-view-always-boolean'.
3744 Even if this is turned off (the default) you can always switch to
3745 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3747 The default is a direct search of the whole phrase, where each space in
3748 the search string can expand to an arbitrary amount of whitespace,
3749 including newlines.
3751 If using a Boolean search, the search string is split on whitespace and
3752 each snippet is searched separately, with logical AND to select an entry.
3753 Words prefixed with a minus must *not* occur in the entry. Words without
3754 a prefix or prefixed with a plus must occur in the entry. Matching is
3755 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3756 match whole words, not parts of a word) if
3757 `org-agenda-search-view-force-full-words' is set (default is nil).
3759 Boolean search snippets enclosed by curly braces are interpreted as
3760 regular expressions that must or (when preceded with \"-\") must not
3761 match in the entry. Snippets enclosed into double quotes will be taken
3762 as a whole, to include whitespace.
3764 - If the search string starts with an asterisk, search only in headlines.
3765 - If (possibly after the leading star) the search string starts with an
3766 exclamation mark, this also means to look at TODO entries only, an effect
3767 that can also be achieved with a prefix argument.
3768 - If (possibly after star and exclamation mark) the search string starts
3769 with a colon, this will mean that the (non-regexp) snippets of the
3770 Boolean search must match as full words.
3772 This command searches the agenda files, and in addition the files listed
3773 in `org-agenda-text-search-extra-files'."
3774 (interactive "P")
3775 (org-compile-prefix-format 'search)
3776 (org-set-sorting-strategy 'search)
3777 (org-prepare-agenda "SEARCH")
3778 (let* ((props (list 'face nil
3779 'done-face 'org-agenda-done
3780 'org-not-done-regexp org-not-done-regexp
3781 'org-todo-regexp org-todo-regexp
3782 'org-complex-heading-regexp org-complex-heading-regexp
3783 'mouse-face 'highlight
3784 'help-echo (format "mouse-2 or RET jump to location")))
3785 (full-words org-agenda-search-view-force-full-words)
3786 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3787 regexp rtn rtnall files file pos
3788 marker category category-pos tags c neg re boolean
3789 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3790 (unless (and (not edit-at)
3791 (stringp string)
3792 (string-match "\\S-" string))
3793 (setq string (read-string
3794 (if org-agenda-search-view-always-boolean
3795 "[+-]Word/{Regexp} ...: "
3796 "Phrase, or [+-]Word/{Regexp} ...: ")
3797 (cond
3798 ((integerp edit-at) (cons string edit-at))
3799 (edit-at string))
3800 'org-agenda-search-history)))
3801 (org-set-local 'org-todo-only todo-only)
3802 (setq org-agenda-redo-command
3803 (list 'org-search-view (if todo-only t nil) string
3804 '(if current-prefix-arg 1 nil)))
3805 (setq org-agenda-query-string string)
3807 (if (equal (string-to-char string) ?*)
3808 (setq hdl-only t
3809 words (substring string 1))
3810 (setq words string))
3811 (when (equal (string-to-char words) ?!)
3812 (setq todo-only t
3813 words (substring words 1)))
3814 (when (equal (string-to-char words) ?:)
3815 (setq full-words t
3816 words (substring words 1)))
3817 (if (or org-agenda-search-view-always-boolean
3818 (member (string-to-char words) '(?- ?+ ?\{)))
3819 (setq boolean t))
3820 (setq words (org-split-string words))
3821 (let (www w)
3822 (while (setq w (pop words))
3823 (while (and (string-match "\\\\\\'" w) words)
3824 (setq w (concat (substring w 0 -1) " " (pop words))))
3825 (push w www))
3826 (setq words (nreverse www) www nil)
3827 (while (setq w (pop words))
3828 (when (and (string-match "\\`[-+]?{" w)
3829 (not (string-match "}\\'" w)))
3830 (while (and words (not (string-match "}\\'" (car words))))
3831 (setq w (concat w " " (pop words))))
3832 (setq w (concat w " " (pop words))))
3833 (push w www))
3834 (setq words (nreverse www)))
3835 (setq org-agenda-last-search-view-search-was-boolean boolean)
3836 (when boolean
3837 (let (wds w)
3838 (while (setq w (pop words))
3839 (if (or (equal (substring w 0 1) "\"")
3840 (and (> (length w) 1)
3841 (member (substring w 0 1) '("+" "-"))
3842 (equal (substring w 1 2) "\"")))
3843 (while (and words (not (equal (substring w -1) "\"")))
3844 (setq w (concat w " " (pop words)))))
3845 (and (string-match "\\`\\([-+]?\\)\"" w)
3846 (setq w (replace-match "\\1" nil nil w)))
3847 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3848 (push w wds))
3849 (setq words (nreverse wds))))
3850 (if boolean
3851 (mapc (lambda (w)
3852 (setq c (string-to-char w))
3853 (if (equal c ?-)
3854 (setq neg t w (substring w 1))
3855 (if (equal c ?+)
3856 (setq neg nil w (substring w 1))
3857 (setq neg nil)))
3858 (if (string-match "\\`{.*}\\'" w)
3859 (setq re (substring w 1 -1))
3860 (if full-words
3861 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3862 (setq re (regexp-quote (downcase w)))))
3863 (if neg (push re regexps-) (push re regexps+)))
3864 words)
3865 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3866 regexps+))
3867 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3868 (if (not regexps+)
3869 (setq regexp org-outline-regexp-bol)
3870 (setq regexp (pop regexps+))
3871 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3872 regexp))))
3873 (setq files (org-agenda-files nil 'ifmode))
3874 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3875 (pop org-agenda-text-search-extra-files)
3876 (setq files (org-add-archive-files files)))
3877 (setq files (append files org-agenda-text-search-extra-files)
3878 rtnall nil)
3879 (while (setq file (pop files))
3880 (setq ee nil)
3881 (catch 'nextfile
3882 (org-check-agenda-file file)
3883 (setq buffer (if (file-exists-p file)
3884 (org-get-agenda-file-buffer file)
3885 (error "No such file %s" file)))
3886 (if (not buffer)
3887 ;; If file does not exist, make sure an error message is sent
3888 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3889 file))))
3890 (with-current-buffer buffer
3891 (with-syntax-table (org-search-syntax-table)
3892 (unless (org-mode-p)
3893 (error "Agenda file %s is not in `org-mode'" file))
3894 (let ((case-fold-search t))
3895 (save-excursion
3896 (save-restriction
3897 (if org-agenda-restrict
3898 (narrow-to-region org-agenda-restrict-begin
3899 org-agenda-restrict-end)
3900 (widen))
3901 (goto-char (point-min))
3902 (unless (or (org-on-heading-p)
3903 (outline-next-heading))
3904 (throw 'nextfile t))
3905 (goto-char (max (point-min) (1- (point))))
3906 (while (re-search-forward regexp nil t)
3907 (org-back-to-heading t)
3908 (skip-chars-forward "* ")
3909 (setq beg (point-at-bol)
3910 beg1 (point)
3911 end (progn (outline-next-heading) (point)))
3912 (catch :skip
3913 (goto-char beg)
3914 (org-agenda-skip)
3915 (setq str (buffer-substring-no-properties
3916 (point-at-bol)
3917 (if hdl-only (point-at-eol) end)))
3918 (mapc (lambda (wr) (when (string-match wr str)
3919 (goto-char (1- end))
3920 (throw :skip t)))
3921 regexps-)
3922 (mapc (lambda (wr) (unless (string-match wr str)
3923 (goto-char (1- end))
3924 (throw :skip t)))
3925 (if todo-only
3926 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3927 regexps+)
3928 regexps+))
3929 (goto-char beg)
3930 (setq marker (org-agenda-new-marker (point))
3931 category (org-get-category)
3932 category-pos (get-text-property (point) 'org-category-position)
3933 tags (org-get-tags-at (point))
3934 txt (org-format-agenda-item
3936 (buffer-substring-no-properties
3937 beg1 (point-at-eol))
3938 category tags))
3939 (org-add-props txt props
3940 'org-marker marker 'org-hd-marker marker
3941 'org-todo-regexp org-todo-regexp
3942 'org-complex-heading-regexp org-complex-heading-regexp
3943 'priority 1000 'org-category category
3944 'org-category-position category-pos
3945 'type "search")
3946 (push txt ee)
3947 (goto-char (1- end))))))))))
3948 (setq rtn (nreverse ee))
3949 (setq rtnall (append rtnall rtn)))
3950 (if org-agenda-overriding-header
3951 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3952 nil 'face 'org-agenda-structure) "\n")
3953 (insert "Search words: ")
3954 (add-text-properties (point-min) (1- (point))
3955 (list 'face 'org-agenda-structure))
3956 (setq pos (point))
3957 (insert string "\n")
3958 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3959 (setq pos (point))
3960 (unless org-agenda-multi
3961 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3962 (add-text-properties pos (1- (point))
3963 (list 'face 'org-agenda-structure))))
3964 (org-agenda-mark-header-line (point-min))
3965 (when rtnall
3966 (insert (org-finalize-agenda-entries rtnall) "\n"))
3967 (goto-char (point-min))
3968 (or org-agenda-multi (org-fit-agenda-window))
3969 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3970 (org-finalize-agenda)
3971 (setq buffer-read-only t)))
3973 ;;; Agenda TODO list
3975 (defvar org-select-this-todo-keyword nil)
3976 (defvar org-last-arg nil)
3978 ;;;###autoload
3979 (defun org-todo-list (arg)
3980 "Show all (not done) TODO entries from all agenda file in a single list.
3981 The prefix arg can be used to select a specific TODO keyword and limit
3982 the list to these. When using \\[universal-argument], you will be prompted
3983 for a keyword. A numeric prefix directly selects the Nth keyword in
3984 `org-todo-keywords-1'."
3985 (interactive "P")
3986 (org-compile-prefix-format 'todo)
3987 (org-set-sorting-strategy 'todo)
3988 (org-prepare-agenda "TODO")
3989 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3990 (let* ((today (org-today))
3991 (date (calendar-gregorian-from-absolute today))
3992 (kwds org-todo-keywords-for-agenda)
3993 (completion-ignore-case t)
3994 (org-select-this-todo-keyword
3995 (if (stringp arg) arg
3996 (and arg (integerp arg) (> arg 0)
3997 (nth (1- arg) kwds))))
3998 rtn rtnall files file pos)
3999 (when (equal arg '(4))
4000 (setq org-select-this-todo-keyword
4001 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4002 (mapcar 'list kwds) nil nil)))
4003 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4004 (org-set-local 'org-last-arg arg)
4005 (setq org-agenda-redo-command
4006 '(org-todo-list (or current-prefix-arg org-last-arg)))
4007 (setq files (org-agenda-files nil 'ifmode)
4008 rtnall nil)
4009 (while (setq file (pop files))
4010 (catch 'nextfile
4011 (org-check-agenda-file file)
4012 (setq rtn (org-agenda-get-day-entries file date :todo))
4013 (setq rtnall (append rtnall rtn))))
4014 (if org-agenda-overriding-header
4015 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4016 nil 'face 'org-agenda-structure) "\n")
4017 (insert "Global list of TODO items of type: ")
4018 (add-text-properties (point-min) (1- (point))
4019 (list 'face 'org-agenda-structure
4020 'short-heading
4021 (concat "ToDo: "
4022 (or org-select-this-todo-keyword "ALL"))))
4023 (org-agenda-mark-header-line (point-min))
4024 (setq pos (point))
4025 (insert (or org-select-this-todo-keyword "ALL") "\n")
4026 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4027 (setq pos (point))
4028 (unless org-agenda-multi
4029 (insert "Available with `N r': (0)ALL")
4030 (let ((n 0) s)
4031 (mapc (lambda (x)
4032 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4033 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4034 (insert "\n "))
4035 (insert " " s))
4036 kwds))
4037 (insert "\n"))
4038 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4039 (org-agenda-mark-header-line (point-min))
4040 (when rtnall
4041 (insert (org-finalize-agenda-entries rtnall) "\n"))
4042 (goto-char (point-min))
4043 (or org-agenda-multi (org-fit-agenda-window))
4044 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4045 (org-finalize-agenda)
4046 (setq buffer-read-only t)))
4048 ;;; Agenda tags match
4050 ;;;###autoload
4051 (defun org-tags-view (&optional todo-only match)
4052 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4053 The prefix arg TODO-ONLY limits the search to TODO entries."
4054 (interactive "P")
4055 (org-compile-prefix-format 'tags)
4056 (org-set-sorting-strategy 'tags)
4057 (let* ((org-tags-match-list-sublevels
4058 org-tags-match-list-sublevels)
4059 (completion-ignore-case t)
4060 rtn rtnall files file pos matcher
4061 buffer)
4062 (when (and (stringp match) (not (string-match "\\S-" match)))
4063 (setq match nil))
4064 (setq matcher (org-make-tags-matcher match)
4065 match (car matcher) matcher (cdr matcher))
4066 (org-prepare-agenda (concat "TAGS " match))
4067 (setq org-agenda-query-string match)
4068 (setq org-agenda-redo-command
4069 (list 'org-tags-view (list 'quote todo-only)
4070 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4071 (setq files (org-agenda-files nil 'ifmode)
4072 rtnall nil)
4073 (while (setq file (pop files))
4074 (catch 'nextfile
4075 (org-check-agenda-file file)
4076 (setq buffer (if (file-exists-p file)
4077 (org-get-agenda-file-buffer file)
4078 (error "No such file %s" file)))
4079 (if (not buffer)
4080 ;; If file does not exist, error message to agenda
4081 (setq rtn (list
4082 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4083 rtnall (append rtnall rtn))
4084 (with-current-buffer buffer
4085 (unless (org-mode-p)
4086 (error "Agenda file %s is not in `org-mode'" file))
4087 (save-excursion
4088 (save-restriction
4089 (if org-agenda-restrict
4090 (narrow-to-region org-agenda-restrict-begin
4091 org-agenda-restrict-end)
4092 (widen))
4093 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4094 (setq rtnall (append rtnall rtn))))))))
4095 (if org-agenda-overriding-header
4096 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4097 nil 'face 'org-agenda-structure) "\n")
4098 (insert "Headlines with TAGS match: ")
4099 (add-text-properties (point-min) (1- (point))
4100 (list 'face 'org-agenda-structure
4101 'short-heading
4102 (concat "Match: " match)))
4103 (setq pos (point))
4104 (insert match "\n")
4105 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4106 (setq pos (point))
4107 (unless org-agenda-multi
4108 (insert "Press `C-u r' to search again with new search string\n"))
4109 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4110 (org-agenda-mark-header-line (point-min))
4111 (when rtnall
4112 (insert (org-finalize-agenda-entries rtnall) "\n"))
4113 (goto-char (point-min))
4114 (or org-agenda-multi (org-fit-agenda-window))
4115 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4116 (org-finalize-agenda)
4117 (setq buffer-read-only t)))
4119 ;;; Agenda Finding stuck projects
4121 (defvar org-agenda-skip-regexp nil
4122 "Regular expression used in skipping subtrees for the agenda.
4123 This is basically a temporary global variable that can be set and then
4124 used by user-defined selections using `org-agenda-skip-function'.")
4126 (defvar org-agenda-overriding-header nil
4127 "When set during agenda, todo and tags searches it replaces the header.
4128 This variable should not be set directly, but custom commands can bind it
4129 in the options section.")
4131 (defun org-agenda-skip-entry-when-regexp-matches ()
4132 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4133 If yes, it returns the end position of this entry, causing agenda commands
4134 to skip the entry but continuing the search in the subtree. This is a
4135 function that can be put into `org-agenda-skip-function' for the duration
4136 of a command."
4137 (let ((end (save-excursion (org-end-of-subtree t)))
4138 skip)
4139 (save-excursion
4140 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4141 (and skip end)))
4143 (defun org-agenda-skip-subtree-when-regexp-matches ()
4144 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4145 If yes, it returns the end position of this tree, causing agenda commands
4146 to skip this subtree. This is a function that can be put into
4147 `org-agenda-skip-function' for the duration of a command."
4148 (let ((end (save-excursion (org-end-of-subtree t)))
4149 skip)
4150 (save-excursion
4151 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4152 (and skip end)))
4154 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4155 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4156 If yes, it returns the end position of the current entry (NOT the tree),
4157 causing agenda commands to skip the entry but continuing the search in
4158 the subtree. This is a function that can be put into
4159 `org-agenda-skip-function' for the duration of a command. An important
4160 use of this function is for the stuck project list."
4161 (let ((end (save-excursion (org-end-of-subtree t)))
4162 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4163 skip)
4164 (save-excursion
4165 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4166 (and skip entry-end)))
4168 (defun org-agenda-skip-entry-if (&rest conditions)
4169 "Skip entry if any of CONDITIONS is true.
4170 See `org-agenda-skip-if' for details."
4171 (org-agenda-skip-if nil conditions))
4173 (defun org-agenda-skip-subtree-if (&rest conditions)
4174 "Skip entry if any of CONDITIONS is true.
4175 See `org-agenda-skip-if' for details."
4176 (org-agenda-skip-if t conditions))
4178 (defun org-agenda-skip-if (subtree conditions)
4179 "Checks current entity for CONDITIONS.
4180 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4181 the entry, i.e. the text before the next heading is checked.
4183 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4184 from different tests. Valid conditions are:
4186 scheduled Check if there is a scheduled cookie
4187 notscheduled Check if there is no scheduled cookie
4188 deadline Check if there is a deadline
4189 notdeadline Check if there is no deadline
4190 timestamp Check if there is a timestamp (also deadline or scheduled)
4191 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4192 regexp Check if regexp matches
4193 notregexp Check if regexp does not match.
4194 todo Check if TODO keyword matches
4195 nottodo Check if TODO keyword does not match
4197 The regexp is taken from the conditions list, it must come right after
4198 the `regexp' or `notregexp' element.
4200 `todo' and `nottodo' accept as an argument a list of todo
4201 keywords, which may include \"*\" to match any todo keyword.
4203 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4205 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4207 Instead of a list a keyword class may be given
4209 (org-agenda-skip-entry-if 'nottodo 'done)
4211 would skip entries that haven't been marked with any of \"DONE\"
4212 keywords. Possible classes are: `todo', `done', `any'.
4214 If any of these conditions is met, this function returns the end point of
4215 the entity, causing the search to continue from there. This is a function
4216 that can be put into `org-agenda-skip-function' for the duration of a command."
4217 (let (beg end m)
4218 (org-back-to-heading t)
4219 (setq beg (point)
4220 end (if subtree
4221 (progn (org-end-of-subtree t) (point))
4222 (progn (outline-next-heading) (1- (point)))))
4223 (goto-char beg)
4224 (and
4226 (and (memq 'scheduled conditions)
4227 (re-search-forward org-scheduled-time-regexp end t))
4228 (and (memq 'notscheduled conditions)
4229 (not (re-search-forward org-scheduled-time-regexp end t)))
4230 (and (memq 'deadline conditions)
4231 (re-search-forward org-deadline-time-regexp end t))
4232 (and (memq 'notdeadline conditions)
4233 (not (re-search-forward org-deadline-time-regexp end t)))
4234 (and (memq 'timestamp conditions)
4235 (re-search-forward org-ts-regexp end t))
4236 (and (memq 'nottimestamp conditions)
4237 (not (re-search-forward org-ts-regexp end t)))
4238 (and (setq m (memq 'regexp conditions))
4239 (stringp (nth 1 m))
4240 (re-search-forward (nth 1 m) end t))
4241 (and (setq m (memq 'notregexp conditions))
4242 (stringp (nth 1 m))
4243 (not (re-search-forward (nth 1 m) end t)))
4244 (and (or
4245 (setq m (memq 'todo conditions))
4246 (setq m (memq 'nottodo conditions)))
4247 (org-agenda-skip-if-todo m end)))
4248 end)))
4250 (defun org-agenda-skip-if-todo (args end)
4251 "Helper function for `org-agenda-skip-if', do not use it directly.
4252 ARGS is a list with first element either `todo' or `nottodo'.
4253 The remainder is either a list of TODO keywords, or a state symbol
4254 `todo' or `done' or `any'."
4255 (let ((kw (car args))
4256 (arg (cadr args))
4257 todo-wds todo-re)
4258 (setq todo-wds
4259 (org-uniquify
4260 (cond
4261 ((listp arg) ;; list of keywords
4262 (if (member "*" arg)
4263 (mapcar 'substring-no-properties org-todo-keywords-1)
4264 arg))
4265 ((symbolp arg) ;; keyword class name
4266 (cond
4267 ((eq arg 'todo)
4268 (org-delete-all org-done-keywords
4269 (mapcar 'substring-no-properties
4270 org-todo-keywords-1)))
4271 ((eq arg 'done) org-done-keywords)
4272 ((eq arg 'any)
4273 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4274 (setq todo-re
4275 (concat "^\\*+[ \t]+\\<\\("
4276 (mapconcat 'identity todo-wds "\\|")
4277 "\\)\\>"))
4278 (if (eq kw 'todo)
4279 (re-search-forward todo-re end t)
4280 (not (re-search-forward todo-re end t)))))
4282 ;;;###autoload
4283 (defun org-agenda-list-stuck-projects (&rest ignore)
4284 "Create agenda view for projects that are stuck.
4285 Stuck projects are project that have no next actions. For the definitions
4286 of what a project is and how to check if it stuck, customize the variable
4287 `org-stuck-projects'."
4288 (interactive)
4289 (let* ((org-agenda-skip-function
4290 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4291 ;; We could have used org-agenda-skip-if here.
4292 (org-agenda-overriding-header
4293 (or org-agenda-overriding-header "List of stuck projects: "))
4294 (matcher (nth 0 org-stuck-projects))
4295 (todo (nth 1 org-stuck-projects))
4296 (todo-wds (if (member "*" todo)
4297 (progn
4298 (org-prepare-agenda-buffers (org-agenda-files
4299 nil 'ifmode))
4300 (org-delete-all
4301 org-done-keywords-for-agenda
4302 (copy-sequence org-todo-keywords-for-agenda)))
4303 todo))
4304 (todo-re (concat "^\\*+[ \t]+\\("
4305 (mapconcat 'identity todo-wds "\\|")
4306 "\\)\\>"))
4307 (tags (nth 2 org-stuck-projects))
4308 (tags-re (if (member "*" tags)
4309 (org-re (concat org-outline-regexp-bol
4310 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4311 (if tags
4312 (concat org-outline-regexp-bol
4313 ".*:\\("
4314 (mapconcat 'identity tags "\\|")
4315 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4316 (gen-re (nth 3 org-stuck-projects))
4317 (re-list
4318 (delq nil
4319 (list
4320 (if todo todo-re)
4321 (if tags tags-re)
4322 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4323 gen-re)))))
4324 (setq org-agenda-skip-regexp
4325 (if re-list
4326 (mapconcat 'identity re-list "\\|")
4327 (error "No information how to identify unstuck projects")))
4328 (org-tags-view nil matcher)
4329 (with-current-buffer org-agenda-buffer-name
4330 (setq org-agenda-redo-command
4331 '(org-agenda-list-stuck-projects
4332 (or current-prefix-arg org-last-arg))))))
4334 ;;; Diary integration
4336 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4337 (defvar list-diary-entries-hook)
4338 (defvar diary-time-regexp)
4339 (defun org-get-entries-from-diary (date)
4340 "Get the (Emacs Calendar) diary entries for DATE."
4341 (require 'diary-lib)
4342 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4343 (diary-display-hook '(fancy-diary-display))
4344 (diary-display-function 'fancy-diary-display)
4345 (pop-up-frames nil)
4346 (list-diary-entries-hook
4347 (cons 'org-diary-default-entry list-diary-entries-hook))
4348 (diary-file-name-prefix-function nil) ; turn this feature off
4349 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4350 entries
4351 (org-disable-agenda-to-diary t))
4352 (save-excursion
4353 (save-window-excursion
4354 (funcall (if (fboundp 'diary-list-entries)
4355 'diary-list-entries 'list-diary-entries)
4356 date 1)))
4357 (if (not (get-buffer diary-fancy-buffer))
4358 (setq entries nil)
4359 (with-current-buffer diary-fancy-buffer
4360 (setq buffer-read-only nil)
4361 (if (zerop (buffer-size))
4362 ;; No entries
4363 (setq entries nil)
4364 ;; Omit the date and other unnecessary stuff
4365 (org-agenda-cleanup-fancy-diary)
4366 ;; Add prefix to each line and extend the text properties
4367 (if (zerop (buffer-size))
4368 (setq entries nil)
4369 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4370 (setq entries
4371 (with-temp-buffer
4372 (insert entries) (goto-char (point-min))
4373 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4374 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4375 (replace-match (concat "; " (match-string 1)))))
4376 (buffer-string)))))
4377 (set-buffer-modified-p nil)
4378 (kill-buffer diary-fancy-buffer)))
4379 (when entries
4380 (setq entries (org-split-string entries "\n"))
4381 (setq entries
4382 (mapcar
4383 (lambda (x)
4384 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4385 ;; Extend the text properties to the beginning of the line
4386 (org-add-props x (text-properties-at (1- (length x)) x)
4387 'type "diary" 'date date 'face 'org-agenda-diary))
4388 entries)))))
4390 (defvar org-agenda-cleanup-fancy-diary-hook nil
4391 "Hook run when the fancy diary buffer is cleaned up.")
4393 (defun org-agenda-cleanup-fancy-diary ()
4394 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4395 This gets rid of the date, the underline under the date, and
4396 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4397 date. It also removes lines that contain only whitespace."
4398 (goto-char (point-min))
4399 (if (looking-at ".*?:[ \t]*")
4400 (progn
4401 (replace-match "")
4402 (re-search-forward "\n=+$" nil t)
4403 (replace-match "")
4404 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4405 (re-search-forward "\n=+$" nil t)
4406 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4407 (goto-char (point-min))
4408 (while (re-search-forward "^ +\n" nil t)
4409 (replace-match ""))
4410 (goto-char (point-min))
4411 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4412 (replace-match ""))
4413 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4415 ;; Make sure entries from the diary have the right text properties.
4416 (eval-after-load "diary-lib"
4417 '(if (boundp 'diary-modify-entry-list-string-function)
4418 ;; We can rely on the hook, nothing to do
4420 ;; Hook not available, must use advice to make this work
4421 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4422 "Make the position visible."
4423 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4424 (stringp string)
4425 buffer-file-name)
4426 (setq string (org-modify-diary-entry-string string))))))
4428 (defun org-modify-diary-entry-string (string)
4429 "Add text properties to string, allowing org-mode to act on it."
4430 (org-add-props string nil
4431 'mouse-face 'highlight
4432 'help-echo (if buffer-file-name
4433 (format "mouse-2 or RET jump to diary file %s"
4434 (abbreviate-file-name buffer-file-name))
4436 'org-agenda-diary-link t
4437 'org-marker (org-agenda-new-marker (point-at-bol))))
4439 (defun org-diary-default-entry ()
4440 "Add a dummy entry to the diary.
4441 Needed to avoid empty dates which mess up holiday display."
4442 ;; Catch the error if dealing with the new add-to-diary-alist
4443 (when org-disable-agenda-to-diary
4444 (condition-case nil
4445 (org-add-to-diary-list original-date "Org-mode dummy" "")
4446 (error
4447 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4449 (defun org-add-to-diary-list (&rest args)
4450 (if (fboundp 'diary-add-to-list)
4451 (apply 'diary-add-to-list args)
4452 (apply 'add-to-diary-list args)))
4454 (defvar org-diary-last-run-time nil)
4456 ;;;###autoload
4457 (defun org-diary (&rest args)
4458 "Return diary information from org-files.
4459 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4460 It accesses org files and extracts information from those files to be
4461 listed in the diary. The function accepts arguments specifying what
4462 items should be listed. For a list of arguments allowed here, see the
4463 variable `org-agenda-entry-types'.
4465 The call in the diary file should look like this:
4467 &%%(org-diary) ~/path/to/some/orgfile.org
4469 Use a separate line for each org file to check. Or, if you omit the file name,
4470 all files listed in `org-agenda-files' will be checked automatically:
4472 &%%(org-diary)
4474 If you don't give any arguments (as in the example above), the default
4475 arguments (:deadline :scheduled :timestamp :sexp) are used.
4476 So the example above may also be written as
4478 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4480 The function expects the lisp variables `entry' and `date' to be provided
4481 by the caller, because this is how the calendar works. Don't use this
4482 function from a program - use `org-agenda-get-day-entries' instead."
4483 (when (> (- (org-float-time)
4484 org-agenda-last-marker-time)
4486 (org-agenda-reset-markers))
4487 (org-compile-prefix-format 'agenda)
4488 (org-set-sorting-strategy 'agenda)
4489 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4490 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4491 (list entry)
4492 (org-agenda-files t)))
4493 (time (org-float-time))
4494 file rtn results)
4495 (when (or (not org-diary-last-run-time)
4496 (> (- time
4497 org-diary-last-run-time)
4499 (org-prepare-agenda-buffers files))
4500 (setq org-diary-last-run-time time)
4501 ;; If this is called during org-agenda, don't return any entries to
4502 ;; the calendar. Org Agenda will list these entries itself.
4503 (if org-disable-agenda-to-diary (setq files nil))
4504 (while (setq file (pop files))
4505 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4506 (setq results (append results rtn)))
4507 (if results
4508 (concat (org-finalize-agenda-entries results) "\n"))))
4510 ;;; Agenda entry finders
4512 (defun org-agenda-get-day-entries (file date &rest args)
4513 "Does the work for `org-diary' and `org-agenda'.
4514 FILE is the path to a file to be checked for entries. DATE is date like
4515 the one returned by `calendar-current-date'. ARGS are symbols indicating
4516 which kind of entries should be extracted. For details about these, see
4517 the documentation of `org-diary'."
4518 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4519 (let* ((org-startup-folded nil)
4520 (org-startup-align-all-tables nil)
4521 (buffer (if (file-exists-p file)
4522 (org-get-agenda-file-buffer file)
4523 (error "No such file %s" file)))
4524 arg results rtn deadline-results)
4525 (if (not buffer)
4526 ;; If file does not exist, make sure an error message ends up in diary
4527 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4528 (with-current-buffer buffer
4529 (unless (org-mode-p)
4530 (error "Agenda file %s is not in `org-mode'" file))
4531 (let ((case-fold-search nil))
4532 (save-excursion
4533 (save-restriction
4534 (if org-agenda-restrict
4535 (narrow-to-region org-agenda-restrict-begin
4536 org-agenda-restrict-end)
4537 (widen))
4538 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4539 (while (setq arg (pop args))
4540 (cond
4541 ((and (eq arg :todo)
4542 (equal date (calendar-gregorian-from-absolute
4543 (org-today))))
4544 (setq rtn (org-agenda-get-todos))
4545 (setq results (append results rtn)))
4546 ((eq arg :timestamp)
4547 (setq rtn (org-agenda-get-blocks))
4548 (setq results (append results rtn))
4549 (setq rtn (org-agenda-get-timestamps))
4550 (setq results (append results rtn)))
4551 ((eq arg :sexp)
4552 (setq rtn (org-agenda-get-sexps))
4553 (setq results (append results rtn)))
4554 ((eq arg :scheduled)
4555 (setq rtn (org-agenda-get-scheduled deadline-results))
4556 (setq results (append results rtn)))
4557 ((eq arg :closed)
4558 (setq rtn (org-agenda-get-progress))
4559 (setq results (append results rtn)))
4560 ((eq arg :deadline)
4561 (setq rtn (org-agenda-get-deadlines))
4562 (setq deadline-results (copy-sequence rtn))
4563 (setq results (append results rtn))))))))
4564 results))))
4566 (defun org-agenda-get-todos ()
4567 "Return the TODO information for agenda display."
4568 (let* ((props (list 'face nil
4569 'done-face 'org-agenda-done
4570 'org-not-done-regexp org-not-done-regexp
4571 'org-todo-regexp org-todo-regexp
4572 'org-complex-heading-regexp org-complex-heading-regexp
4573 'mouse-face 'highlight
4574 'help-echo
4575 (format "mouse-2 or RET jump to org file %s"
4576 (abbreviate-file-name buffer-file-name))))
4577 (regexp (concat "^\\*+[ \t]+\\("
4578 (if org-select-this-todo-keyword
4579 (if (equal org-select-this-todo-keyword "*")
4580 org-todo-regexp
4581 (concat "\\<\\("
4582 (mapconcat 'identity
4583 (org-split-string
4584 org-select-this-todo-keyword "|") "\\|")
4585 "\\)\\>"))
4586 org-not-done-regexp)
4587 "[^\n\r]*\\)"))
4588 marker priority category category-pos tags todo-state ee txt beg end)
4589 (goto-char (point-min))
4590 (while (re-search-forward regexp nil t)
4591 (catch :skip
4592 (save-match-data
4593 (beginning-of-line)
4594 (org-agenda-skip)
4595 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4596 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4597 (goto-char (1+ beg))
4598 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4599 (throw :skip nil)))
4600 (goto-char (match-beginning 1))
4601 (setq marker (org-agenda-new-marker (match-beginning 0))
4602 category (org-get-category)
4603 category-pos (get-text-property (point) 'org-category-position)
4604 txt (match-string 1)
4605 tags (org-get-tags-at (point))
4606 txt (org-format-agenda-item "" txt category tags)
4607 priority (1+ (org-get-priority txt))
4608 todo-state (org-get-todo-state))
4609 (org-add-props txt props
4610 'org-marker marker 'org-hd-marker marker
4611 'priority priority 'org-category category
4612 'org-category-position category-pos
4613 'type "todo" 'todo-state todo-state)
4614 (push txt ee)
4615 (if org-agenda-todo-list-sublevels
4616 (goto-char (match-end 1))
4617 (org-end-of-subtree 'invisible))))
4618 (nreverse ee)))
4620 (defun org-agenda-todo-custom-ignore-p (time n)
4621 "Check whether timestamp is farther away then n number of days.
4622 This function is invoked if `org-agenda-todo-ignore-deadlines',
4623 `org-agenda-todo-ignore-scheduled' or
4624 `org-agenda-todo-ignore-timestamp' is set to an integer."
4625 (let ((days (org-days-to-time time)))
4626 (if (>= n 0)
4627 (>= days n)
4628 (<= days n))))
4630 ;;;###autoload
4631 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4632 (&optional end)
4633 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4634 (when (or org-agenda-todo-ignore-with-date
4635 org-agenda-todo-ignore-scheduled
4636 org-agenda-todo-ignore-deadlines
4637 org-agenda-todo-ignore-timestamp)
4638 (setq end (or end (save-excursion (outline-next-heading) (point))))
4639 (save-excursion
4640 (or (and org-agenda-todo-ignore-with-date
4641 (re-search-forward org-ts-regexp end t))
4642 (and org-agenda-todo-ignore-scheduled
4643 (re-search-forward org-scheduled-time-regexp end t)
4644 (cond
4645 ((eq org-agenda-todo-ignore-scheduled 'future)
4646 (> (org-days-to-time (match-string 1)) 0))
4647 ((eq org-agenda-todo-ignore-scheduled 'past)
4648 (<= (org-days-to-time (match-string 1)) 0))
4649 ((numberp org-agenda-todo-ignore-scheduled)
4650 (org-agenda-todo-custom-ignore-p
4651 (match-string 1) org-agenda-todo-ignore-scheduled))
4652 (t)))
4653 (and org-agenda-todo-ignore-deadlines
4654 (re-search-forward org-deadline-time-regexp end t)
4655 (cond
4656 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4657 ((eq org-agenda-todo-ignore-deadlines 'far)
4658 (not (org-deadline-close (match-string 1))))
4659 ((eq org-agenda-todo-ignore-deadlines 'future)
4660 (> (org-days-to-time (match-string 1)) 0))
4661 ((eq org-agenda-todo-ignore-deadlines 'past)
4662 (<= (org-days-to-time (match-string 1)) 0))
4663 ((numberp org-agenda-todo-ignore-deadlines)
4664 (org-agenda-todo-custom-ignore-p
4665 (match-string 1) org-agenda-todo-ignore-deadlines))
4666 (t (org-deadline-close (match-string 1)))))
4667 (and org-agenda-todo-ignore-timestamp
4668 (let ((buffer (current-buffer))
4669 (regexp
4670 (concat
4671 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4672 (start (point)))
4673 ;; Copy current buffer into a temporary one
4674 (with-temp-buffer
4675 (insert-buffer-substring buffer start end)
4676 (goto-char (point-min))
4677 ;; Delete SCHEDULED and DEADLINE items
4678 (while (re-search-forward regexp end t)
4679 (delete-region (match-beginning 0) (match-end 0)))
4680 (goto-char (point-min))
4681 ;; No search for timestamp left
4682 (when (re-search-forward org-ts-regexp nil t)
4683 (cond
4684 ((eq org-agenda-todo-ignore-timestamp 'future)
4685 (> (org-days-to-time (match-string 1)) 0))
4686 ((eq org-agenda-todo-ignore-timestamp 'past)
4687 (<= (org-days-to-time (match-string 1)) 0))
4688 ((numberp org-agenda-todo-ignore-timestamp)
4689 (org-agenda-todo-custom-ignore-p
4690 (match-string 1) org-agenda-todo-ignore-timestamp))
4691 (t))))))))))
4693 (defconst org-agenda-no-heading-message
4694 "No heading for this item in buffer or region.")
4696 (defun org-agenda-get-timestamps ()
4697 "Return the date stamp information for agenda display."
4698 (let* ((props (list 'face nil
4699 'org-not-done-regexp org-not-done-regexp
4700 'org-todo-regexp org-todo-regexp
4701 'org-complex-heading-regexp org-complex-heading-regexp
4702 'mouse-face 'highlight
4703 'help-echo
4704 (format "mouse-2 or RET jump to org file %s"
4705 (abbreviate-file-name buffer-file-name))))
4706 (d1 (calendar-absolute-from-gregorian date))
4707 (remove-re
4708 (concat
4709 (regexp-quote
4710 (format-time-string
4711 "<%Y-%m-%d"
4712 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4713 ".*?>"))
4714 (regexp
4715 (concat
4716 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4717 (regexp-quote
4718 (substring
4719 (format-time-string
4720 (car org-time-stamp-formats)
4721 (apply 'encode-time ; DATE bound by calendar
4722 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4723 1 11))
4724 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4725 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4726 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4727 donep tmp priority category category-pos ee txt timestr tags
4728 b0 b3 e3 head todo-state end-of-match show-all)
4729 (goto-char (point-min))
4730 (while (setq end-of-match (re-search-forward regexp nil t))
4731 (setq b0 (match-beginning 0)
4732 b3 (match-beginning 3) e3 (match-end 3)
4733 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4734 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4735 (member todo-state
4736 org-agenda-repeating-timestamp-show-all)))
4737 (catch :skip
4738 (and (org-at-date-range-p) (throw :skip nil))
4739 (org-agenda-skip)
4740 (if (and (match-end 1)
4741 (not (= d1 (org-time-string-to-absolute
4742 (match-string 1) d1 nil show-all))))
4743 (throw :skip nil))
4744 (if (and e3
4745 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4746 (throw :skip nil))
4747 (setq tmp (buffer-substring (max (point-min)
4748 (- b0 org-ds-keyword-length))
4750 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4751 inactivep (= (char-after b0) ?\[)
4752 deadlinep (string-match org-deadline-regexp tmp)
4753 scheduledp (string-match org-scheduled-regexp tmp)
4754 closedp (and org-agenda-include-inactive-timestamps
4755 (string-match org-closed-string tmp))
4756 clockp (and org-agenda-include-inactive-timestamps
4757 (or (string-match org-clock-string tmp)
4758 (string-match "]-+\\'" tmp)))
4759 donep (member todo-state org-done-keywords))
4760 (if (or scheduledp deadlinep closedp clockp
4761 (and donep org-agenda-skip-timestamp-if-done))
4762 (throw :skip t))
4763 (if (string-match ">" timestr)
4764 ;; substring should only run to end of time stamp
4765 (setq timestr (substring timestr 0 (match-end 0))))
4766 (setq marker (org-agenda-new-marker b0)
4767 category (org-get-category b0)
4768 category-pos (get-text-property b0 'org-category-position))
4769 (save-excursion
4770 (if (not (re-search-backward org-outline-regexp-bol nil t))
4771 (setq txt org-agenda-no-heading-message)
4772 (goto-char (match-beginning 0))
4773 (setq hdmarker (org-agenda-new-marker)
4774 tags (org-get-tags-at))
4775 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4776 (setq head (or (match-string 1) ""))
4777 (setq txt (org-format-agenda-item
4778 (if inactivep org-agenda-inactive-leader nil)
4779 head category tags timestr
4780 remove-re)))
4781 (setq priority (org-get-priority txt))
4782 (org-add-props txt props
4783 'org-marker marker 'org-hd-marker hdmarker)
4784 (org-add-props txt nil 'priority priority
4785 'org-category category 'date date
4786 'org-category-position category-pos
4787 'todo-state todo-state
4788 'type "timestamp")
4789 (push txt ee))
4790 (if org-agenda-skip-additional-timestamps-same-entry
4791 (outline-next-heading)
4792 (goto-char end-of-match))))
4793 (nreverse ee)))
4795 (defun org-agenda-get-sexps ()
4796 "Return the sexp information for agenda display."
4797 (require 'diary-lib)
4798 (let* ((props (list 'mouse-face 'highlight
4799 'help-echo
4800 (format "mouse-2 or RET jump to org file %s"
4801 (abbreviate-file-name buffer-file-name))))
4802 (regexp "^&?%%(")
4803 marker category category-pos ee txt tags entry
4804 result beg b sexp sexp-entry todo-state)
4805 (goto-char (point-min))
4806 (while (re-search-forward regexp nil t)
4807 (catch :skip
4808 (org-agenda-skip)
4809 (setq beg (match-beginning 0))
4810 (goto-char (1- (match-end 0)))
4811 (setq b (point))
4812 (forward-sexp 1)
4813 (setq sexp (buffer-substring b (point)))
4814 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4815 (org-trim (match-string 1))
4816 ""))
4817 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4818 (when result
4819 (setq marker (org-agenda-new-marker beg)
4820 category (org-get-category beg)
4821 category-pos (get-text-property beg 'org-category-position)
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 'org-category-position category-pos
4837 'type "sexp")
4838 (push txt ee)))))
4839 (nreverse ee)))
4841 ;; Calendar sanity: define some functions that are independent of
4842 ;; `calendar-date-style'.
4843 ;; Normally I would like to use ISO format when calling the diary functions,
4844 ;; but to make sure we still have Emacs 22 compatibility we bind
4845 ;; also `european-calendar-style' and use european format
4846 (defun org-anniversary (year month day &optional mark)
4847 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4848 (org-no-warnings
4849 (let ((calendar-date-style 'european) (european-calendar-style t))
4850 (diary-anniversary day month year mark))))
4851 (defun org-cyclic (N year month day &optional mark)
4852 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4853 (org-no-warnings
4854 (let ((calendar-date-style 'european) (european-calendar-style t))
4855 (diary-cyclic N day month year mark))))
4856 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4857 "Like `diary-block', but with fixed (ISO) order of arguments."
4858 (org-no-warnings
4859 (let ((calendar-date-style 'european) (european-calendar-style t))
4860 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4861 (defun org-date (year month day &optional mark)
4862 "Like `diary-date', but with fixed (ISO) order of arguments."
4863 (org-no-warnings
4864 (let ((calendar-date-style 'european) (european-calendar-style t))
4865 (diary-date day month year mark))))
4866 (defalias 'org-float 'diary-float)
4868 ;; Define the` org-class' function
4869 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4870 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4871 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4872 is any number of ISO weeks in the block period for which the item should
4873 be skipped."
4874 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4875 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4876 (d (calendar-absolute-from-gregorian date)))
4877 (and
4878 (<= date1 d)
4879 (<= d date2)
4880 (= (calendar-day-of-week date) dayname)
4881 (or (not skip-weeks)
4882 (progn
4883 (require 'cal-iso)
4884 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4885 entry)))
4887 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4888 "Like `org-class', but honor `calendar-date-style'.
4889 The order of the first 2 times 3 arguments depends on the variable
4890 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4891 So for American calendars, give this as MONTH DAY YEAR, for European as
4892 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4893 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4894 is any number of ISO weeks in the block period for which the item should
4895 be skipped.
4897 This function is here only for backward compatibility and it is deprecated,
4898 please use `org-class' instead."
4899 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4900 (date2 (org-order-calendar-date-args m2 d2 y2)))
4901 (org-class
4902 (nth 2 date1) (car date1) (nth 1 date1)
4903 (nth 2 date2) (car date2) (nth 1 date2)
4904 dayname skip-weeks)))
4906 (defalias 'org-get-closed 'org-agenda-get-progress)
4907 (defun org-agenda-get-progress ()
4908 "Return the logged TODO entries for agenda display."
4909 (let* ((props (list 'mouse-face 'highlight
4910 'org-not-done-regexp org-not-done-regexp
4911 'org-todo-regexp org-todo-regexp
4912 'org-complex-heading-regexp org-complex-heading-regexp
4913 'help-echo
4914 (format "mouse-2 or RET jump to org file %s"
4915 (abbreviate-file-name buffer-file-name))))
4916 (items (if (consp org-agenda-show-log)
4917 org-agenda-show-log
4918 (if (eq org-agenda-show-log 'clockcheck)
4919 '(clock)
4920 org-agenda-log-mode-items)))
4921 (parts
4922 (delq nil
4923 (list
4924 (if (memq 'closed items) (concat "\\<" org-closed-string))
4925 (if (memq 'clock items) (concat "\\<" org-clock-string))
4926 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4927 (parts-re (if parts (mapconcat 'identity parts "\\|")
4928 (error "`org-agenda-log-mode-items' is empty")))
4929 (regexp (concat
4930 "\\(" parts-re "\\)"
4931 " *\\["
4932 (regexp-quote
4933 (substring
4934 (format-time-string
4935 (car org-time-stamp-formats)
4936 (apply 'encode-time ; DATE bound by calendar
4937 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4938 1 11))))
4939 (org-agenda-search-headline-for-time nil)
4940 marker hdmarker priority category category-pos tags closedp
4941 statep clockp state ee txt extra timestr rest clocked)
4942 (goto-char (point-min))
4943 (while (re-search-forward regexp nil t)
4944 (catch :skip
4945 (org-agenda-skip)
4946 (setq marker (org-agenda-new-marker (match-beginning 0))
4947 closedp (equal (match-string 1) org-closed-string)
4948 statep (equal (string-to-char (match-string 1)) ?-)
4949 clockp (not (or closedp statep))
4950 state (and statep (match-string 2))
4951 category (org-get-category (match-beginning 0))
4952 category-pos (get-text-property (match-beginning 0) 'org-category-position)
4953 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4954 (when (string-match "\\]" timestr)
4955 ;; substring should only run to end of time stamp
4956 (setq rest (substring timestr (match-end 0))
4957 timestr (substring timestr 0 (match-end 0)))
4958 (if (and (not closedp) (not statep)
4959 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4960 rest))
4961 (progn (setq timestr (concat (substring timestr 0 -1)
4962 "-" (match-string 1 rest) "]"))
4963 (setq clocked (match-string 2 rest)))
4964 (setq clocked "-")))
4965 (save-excursion
4966 (setq extra
4967 (cond
4968 ((not org-agenda-log-mode-add-notes) nil)
4969 (statep
4970 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4971 (match-string 1)))
4972 (clockp
4973 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4974 (match-string 1)))))
4975 (if (not (re-search-backward org-outline-regexp-bol nil t))
4976 (setq txt org-agenda-no-heading-message)
4977 (goto-char (match-beginning 0))
4978 (setq hdmarker (org-agenda-new-marker)
4979 tags (org-get-tags-at))
4980 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4981 (setq txt (match-string 1))
4982 (when extra
4983 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4984 (setq txt (concat (substring txt 0 (match-beginning 1))
4985 " - " extra " " (match-string 2 txt)))
4986 (setq txt (concat txt " - " extra))))
4987 (setq txt (org-format-agenda-item
4988 (cond
4989 (closedp "Closed: ")
4990 (statep (concat "State: (" state ")"))
4991 (t (concat "Clocked: (" clocked ")")))
4992 txt category tags timestr)))
4993 (setq priority 100000)
4994 (org-add-props txt props
4995 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4996 'priority priority 'org-category category
4997 'org-category-position category-pos
4998 'type "closed" 'date date
4999 'undone-face 'org-warning 'done-face 'org-agenda-done)
5000 (push txt ee))
5001 (goto-char (point-at-eol))))
5002 (nreverse ee)))
5004 (defun org-agenda-show-clocking-issues ()
5005 "Add overlays, showing issues with clocking.
5006 See also the user option `org-agenda-clock-consistency-checks'."
5007 (interactive)
5008 (let* ((pl org-agenda-clock-consistency-checks)
5009 (re (concat "^[ \t]*"
5010 org-clock-string
5011 "[ \t]+"
5012 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5013 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5014 (tlstart 0.)
5015 (tlend 0.)
5016 (maxtime (org-hh:mm-string-to-minutes
5017 (or (plist-get pl :max-duration) "24:00")))
5018 (mintime (org-hh:mm-string-to-minutes
5019 (or (plist-get pl :min-duration) 0)))
5020 (maxgap (org-hh:mm-string-to-minutes
5021 ;; default 30:00 means never complain
5022 (or (plist-get pl :max-gap) "30:00")))
5023 (gapok (mapcar 'org-hh:mm-string-to-minutes
5024 (plist-get pl :gap-ok-around)))
5025 (def-face (or (plist-get pl :default-face)
5026 '((:background "DarkRed") (:foreground "white"))))
5027 issue face m te ts dt ov)
5028 (goto-char (point-min))
5029 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5030 (setq issue nil face def-face)
5031 (catch 'next
5032 (setq m (org-get-at-bol 'org-marker)
5033 te nil ts nil)
5034 (unless (and m (markerp m))
5035 (setq issue "No valid clock line") (throw 'next t))
5036 (org-with-point-at m
5037 (save-excursion
5038 (goto-char (point-at-bol))
5039 (unless (looking-at re)
5040 (error "No valid Clock line")
5041 (throw 'next t))
5042 (unless (match-end 3)
5043 (setq issue "No end time"
5044 face (or (plist-get pl :no-end-time-face) face))
5045 (throw 'next t))
5046 (setq ts (match-string 1)
5047 te (match-string 3)
5048 ts (org-float-time
5049 (apply 'encode-time (org-parse-time-string ts)))
5050 te (org-float-time
5051 (apply 'encode-time (org-parse-time-string te)))
5052 dt (- te ts))))
5053 (cond
5054 ((> dt (* 60 maxtime))
5055 ;; a very long clocking chunk
5056 (setq issue (format "Clocking interval is very long: %s"
5057 (org-minutes-to-hh:mm-string
5058 (floor (/ (float dt) 60.))))
5059 face (or (plist-get pl :long-face) face)))
5060 ((< dt (* 60 mintime))
5061 ;; a very short clocking chunk
5062 (setq issue (format "Clocking interval is very short: %s"
5063 (org-minutes-to-hh:mm-string
5064 (floor (/ (float dt) 60.))))
5065 face (or (plist-get pl :short-face) face)))
5066 ((and (> tlend 0) (< ts tlend))
5067 ;; Two clock entries are overlapping
5068 (setq issue (format "Clocking overlap: %d minutes"
5069 (/ (- tlend ts) 60))
5070 face (or (plist-get pl :overlap-face) face)))
5071 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5072 ;; There is a gap, lets see if we need to report it
5073 (unless (org-agenda-check-clock-gap tlend ts gapok)
5074 (setq issue (format "Clocking gap: %d minutes"
5075 (/ (- ts tlend) 60))
5076 face (or (plist-get pl :gap-face) face))))
5077 (t nil)))
5078 (setq tlend (or te tlend) tlstart (or ts tlstart))
5079 (when issue
5080 ;; OK, there was some issue, add an overlay to show the issue
5081 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5082 (overlay-put ov 'before-string
5083 (concat
5084 (org-add-props
5085 (format "%-43s" (concat " " issue))
5087 'face face)
5088 "\n"))
5089 (overlay-put ov 'evaporate t)))))
5091 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5092 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5093 (catch 'exit
5094 (unless ok-list
5095 ;; there are no OK times for gaps...
5096 (throw 'exit nil))
5097 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5098 ;; This is more than 24 hours, so it is OK.
5099 ;; because we have at least one OK time, that must be in the
5100 ;; 24 hour interval.
5101 (throw 'exit t))
5102 ;; We have a shorter gap.
5103 ;; Now we have to get the minute of the day when these times are
5104 (let* ((t1dec (decode-time (seconds-to-time t1)))
5105 (t2dec (decode-time (seconds-to-time t2)))
5106 ;; compute the minute on the day
5107 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5108 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5109 (when (< min2 min1)
5110 ;; if min2 is smaller than min1, this means it is on the next day.
5111 ;; Wrap it to after midnight.
5112 (setq min2 (+ min2 1440)))
5113 ;; Now check if any of the OK times is in the gap
5114 (mapc (lambda (x)
5115 ;; Wrap the time to after midnight if necessary
5116 (if (< x min1) (setq x (+ x 1440)))
5117 ;; Check if in interval
5118 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5119 ok-list)
5120 ;; Nope, this gap is not OK
5121 nil)))
5123 (defun org-agenda-get-deadlines ()
5124 "Return the deadline information for agenda display."
5125 (let* ((props (list 'mouse-face 'highlight
5126 'org-not-done-regexp org-not-done-regexp
5127 'org-todo-regexp org-todo-regexp
5128 'org-complex-heading-regexp org-complex-heading-regexp
5129 'help-echo
5130 (format "mouse-2 or RET jump to org file %s"
5131 (abbreviate-file-name buffer-file-name))))
5132 (regexp org-deadline-time-regexp)
5133 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5134 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5135 d2 diff dfrac wdays pos pos1 category category-pos
5136 tags suppress-prewarning ee txt head face s todo-state
5137 show-all upcomingp donep timestr)
5138 (goto-char (point-min))
5139 (while (re-search-forward regexp nil t)
5140 (setq suppress-prewarning nil)
5141 (catch :skip
5142 (org-agenda-skip)
5143 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5144 (save-match-data
5145 (string-match org-scheduled-time-regexp
5146 (buffer-substring (point-at-bol)
5147 (point-at-eol)))))
5148 (setq suppress-prewarning
5149 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5150 org-agenda-skip-deadline-prewarning-if-scheduled
5151 0)))
5152 (setq s (match-string 1)
5153 txt nil
5154 pos (1- (match-beginning 1))
5155 todo-state (save-match-data (org-get-todo-state))
5156 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5157 (member todo-state
5158 org-agenda-repeating-timestamp-show-all))
5159 d2 (org-time-string-to-absolute
5160 (match-string 1) d1 'past show-all)
5161 diff (- d2 d1)
5162 wdays (if suppress-prewarning
5163 (let ((org-deadline-warning-days suppress-prewarning))
5164 (org-get-wdays s))
5165 (org-get-wdays s))
5166 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5167 upcomingp (and todayp (> diff 0)))
5168 ;; When to show a deadline in the calendar:
5169 ;; If the expiration is within wdays warning time.
5170 ;; Past-due deadlines are only shown on the current date
5171 (if (and (or (and (<= diff wdays)
5172 (and todayp (not org-agenda-only-exact-dates)))
5173 (= diff 0)))
5174 (save-excursion
5175 ;; (setq todo-state (org-get-todo-state))
5176 (setq donep (member todo-state org-done-keywords))
5177 (if (and donep
5178 (or org-agenda-skip-deadline-if-done
5179 (not (= diff 0))))
5180 (setq txt nil)
5181 (setq category (org-get-category)
5182 category-pos (get-text-property (point) 'org-category-position))
5183 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5184 (setq txt org-agenda-no-heading-message)
5185 (goto-char (match-end 0))
5186 (setq pos1 (match-beginning 0))
5187 (setq tags (org-get-tags-at pos1))
5188 (setq head (buffer-substring-no-properties
5189 (point)
5190 (progn (skip-chars-forward "^\r\n")
5191 (point))))
5192 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5193 (setq timestr
5194 (concat (substring s (match-beginning 1)) " "))
5195 (setq timestr 'time))
5196 (setq txt (org-format-agenda-item
5197 (if (= diff 0)
5198 (car org-agenda-deadline-leaders)
5199 (if (functionp
5200 (nth 1 org-agenda-deadline-leaders))
5201 (funcall
5202 (nth 1 org-agenda-deadline-leaders)
5203 diff date)
5204 (format (nth 1 org-agenda-deadline-leaders)
5205 diff)))
5206 head category tags
5207 (if (not (= diff 0)) nil timestr)))))
5208 (when txt
5209 (setq face (org-agenda-deadline-face dfrac))
5210 (org-add-props txt props
5211 'org-marker (org-agenda-new-marker pos)
5212 'org-hd-marker (org-agenda-new-marker pos1)
5213 'priority (+ (- diff)
5214 (org-get-priority txt))
5215 'org-category category
5216 'org-category-position category-pos
5217 'todo-state todo-state
5218 'type (if upcomingp "upcoming-deadline" "deadline")
5219 'date (if upcomingp date d2)
5220 'face (if donep 'org-agenda-done face)
5221 'undone-face face 'done-face 'org-agenda-done)
5222 (push txt ee))))))
5223 (nreverse ee)))
5225 (defun org-agenda-deadline-face (fraction)
5226 "Return the face to displaying a deadline item.
5227 FRACTION is what fraction of the head-warning time has passed."
5228 (let ((faces org-agenda-deadline-faces) f)
5229 (catch 'exit
5230 (while (setq f (pop faces))
5231 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5233 (defun org-agenda-get-scheduled (&optional deadline-results)
5234 "Return the scheduled information for agenda display."
5235 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5236 'org-todo-regexp org-todo-regexp
5237 'org-complex-heading-regexp org-complex-heading-regexp
5238 'done-face 'org-agenda-done
5239 'mouse-face 'highlight
5240 'help-echo
5241 (format "mouse-2 or RET jump to org file %s"
5242 (abbreviate-file-name buffer-file-name))))
5243 (regexp org-scheduled-time-regexp)
5244 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5245 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5247 (deadline-position-alist
5248 (mapcar (lambda (a) (and (setq mm (get-text-property
5249 0 'org-hd-marker a))
5250 (cons (marker-position mm) a)))
5251 deadline-results))
5252 d2 diff pos pos1 category category-pos tags donep
5253 ee txt head pastschedp todo-state face timestr s habitp show-all)
5254 (goto-char (point-min))
5255 (while (re-search-forward regexp nil t)
5256 (catch :skip
5257 (org-agenda-skip)
5258 (setq s (match-string 1)
5259 txt nil
5260 pos (1- (match-beginning 1))
5261 todo-state (save-match-data (org-get-todo-state))
5262 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5263 (member todo-state
5264 org-agenda-repeating-timestamp-show-all))
5265 d2 (org-time-string-to-absolute
5266 (match-string 1) d1 'past show-all)
5267 diff (- d2 d1))
5268 (setq pastschedp (and todayp (< diff 0)))
5269 ;; When to show a scheduled item in the calendar:
5270 ;; If it is on or past the date.
5271 (when (or (and (< diff 0)
5272 (< (abs diff) org-scheduled-past-days)
5273 (and todayp (not org-agenda-only-exact-dates)))
5274 (= diff 0))
5275 (save-excursion
5276 (setq donep (member todo-state org-done-keywords))
5277 (if (and donep
5278 (or org-agenda-skip-scheduled-if-done
5279 (not (= diff 0))
5280 (and (functionp 'org-is-habit-p)
5281 (org-is-habit-p))))
5282 (setq txt nil)
5283 (setq habitp (and (functionp 'org-is-habit-p)
5284 (org-is-habit-p)))
5285 (setq category (org-get-category)
5286 category-pos (get-text-property (point) 'org-category-position))
5287 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5288 (setq txt org-agenda-no-heading-message)
5289 (goto-char (match-end 0))
5290 (setq pos1 (match-beginning 0))
5291 (if habitp
5292 (if (or (not org-habit-show-habits)
5293 (and (not todayp)
5294 org-habit-show-habits-only-for-today))
5295 (throw :skip nil))
5296 (if (and
5297 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5298 (and org-agenda-skip-scheduled-if-deadline-is-shown
5299 pastschedp))
5300 (setq mm (assoc pos1 deadline-position-alist)))
5301 (throw :skip nil)))
5302 (setq tags (org-get-tags-at))
5303 (setq head (buffer-substring-no-properties
5304 (point)
5305 (progn (skip-chars-forward "^\r\n") (point))))
5306 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5307 (setq timestr
5308 (concat (substring s (match-beginning 1)) " "))
5309 (setq timestr 'time))
5310 (setq txt (org-format-agenda-item
5311 (if (= diff 0)
5312 (car org-agenda-scheduled-leaders)
5313 (format (nth 1 org-agenda-scheduled-leaders)
5314 (- 1 diff)))
5315 head category tags
5316 (if (not (= diff 0)) nil timestr)
5317 nil habitp))))
5318 (when txt
5319 (setq face
5320 (cond
5321 ((and (not habitp) pastschedp)
5322 'org-scheduled-previously)
5323 (todayp 'org-scheduled-today)
5324 (t 'org-scheduled))
5325 habitp (and habitp (org-habit-parse-todo)))
5326 (org-add-props txt props
5327 'undone-face face
5328 'face (if donep 'org-agenda-done face)
5329 'org-marker (org-agenda-new-marker pos)
5330 'org-hd-marker (org-agenda-new-marker pos1)
5331 'type (if pastschedp "past-scheduled" "scheduled")
5332 'date (if pastschedp d2 date)
5333 'priority (if habitp
5334 (org-habit-get-priority habitp)
5335 (+ 94 (- 5 diff) (org-get-priority txt)))
5336 'org-category category
5337 'org-category-position category-pos
5338 'org-habit-p habitp
5339 'todo-state todo-state)
5340 (push txt ee))))))
5341 (nreverse ee)))
5343 (defun org-agenda-get-blocks ()
5344 "Return the date-range information for agenda display."
5345 (let* ((props (list 'face nil
5346 'org-not-done-regexp org-not-done-regexp
5347 'org-todo-regexp org-todo-regexp
5348 'org-complex-heading-regexp org-complex-heading-regexp
5349 'mouse-face 'highlight
5350 'help-echo
5351 (format "mouse-2 or RET jump to org file %s"
5352 (abbreviate-file-name buffer-file-name))))
5353 (regexp org-tr-regexp)
5354 (d0 (calendar-absolute-from-gregorian date))
5355 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5356 head donep)
5357 (goto-char (point-min))
5358 (while (re-search-forward regexp nil t)
5359 (catch :skip
5360 (org-agenda-skip)
5361 (setq pos (point))
5362 (let ((start-time (match-string 1))
5363 (end-time (match-string 2)))
5364 (setq s1 (match-string 1)
5365 s2 (match-string 2)
5366 d1 (time-to-days (org-time-string-to-time s1))
5367 d2 (time-to-days (org-time-string-to-time s2)))
5368 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5369 ;; Only allow days between the limits, because the normal
5370 ;; date stamps will catch the limits.
5371 (save-excursion
5372 (setq todo-state (org-get-todo-state))
5373 (setq donep (member todo-state org-done-keywords))
5374 (if (and donep org-agenda-skip-timestamp-if-done)
5375 (throw :skip t))
5376 (setq marker (org-agenda-new-marker (point)))
5377 (setq category (org-get-category)
5378 category-pos (get-text-property (point) 'org-category-position))
5379 (if (not (re-search-backward org-outline-regexp-bol nil t))
5380 (setq txt org-agenda-no-heading-message)
5381 (goto-char (match-beginning 0))
5382 (setq hdmarker (org-agenda-new-marker (point)))
5383 (setq tags (org-get-tags-at))
5384 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5385 (setq head (match-string 1))
5386 (let ((remove-re
5387 (if org-agenda-remove-timeranges-from-blocks
5388 (concat
5389 "<" (regexp-quote s1) ".*?>"
5390 "--"
5391 "<" (regexp-quote s2) ".*?>")
5392 nil)))
5393 (setq txt (org-format-agenda-item
5394 (format
5395 (nth (if (= d1 d2) 0 1)
5396 org-agenda-timerange-leaders)
5397 (1+ (- d0 d1)) (1+ (- d2 d1)))
5398 head category tags
5399 (cond ((= d1 d0)
5400 (concat "<" start-time ">"))
5401 ((= d2 d0)
5402 (concat "<" end-time ">"))
5403 (t nil))
5404 remove-re))))
5405 (org-add-props txt props
5406 'org-marker marker 'org-hd-marker hdmarker
5407 'type "block" 'date date
5408 'todo-state todo-state
5409 'priority (org-get-priority txt) 'org-category category
5410 'org-category-position category-pos)
5411 (push txt ee))))
5412 (goto-char pos)))
5413 ;; Sort the entries by expiration date.
5414 (nreverse ee)))
5416 ;;; Agenda presentation and sorting
5418 (defvar org-prefix-has-time nil
5419 "A flag, set by `org-compile-prefix-format'.
5420 The flag is set if the currently compiled format contains a `%t'.")
5421 (defvar org-prefix-has-tag nil
5422 "A flag, set by `org-compile-prefix-format'.
5423 The flag is set if the currently compiled format contains a `%T'.")
5424 (defvar org-prefix-has-effort nil
5425 "A flag, set by `org-compile-prefix-format'.
5426 The flag is set if the currently compiled format contains a `%e'.")
5427 (defvar org-prefix-category-length nil
5428 "Used by `org-compile-prefix-format' to remember the category field width.")
5429 (defvar org-prefix-category-max-length nil
5430 "Used by `org-compile-prefix-format' to remember the category field width.")
5432 (defun org-agenda-get-category-icon (category)
5433 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5434 (dolist (entry org-agenda-category-icon-alist)
5435 (when (org-string-match-p (car entry) category)
5436 (if (listp (cadr entry))
5437 (return (cadr entry))
5438 (return (apply 'create-image (cdr entry)))))))
5440 (defun org-format-agenda-item (extra txt &optional category tags dotime
5441 remove-re habitp)
5442 "Format TXT to be inserted into the agenda buffer.
5443 In particular, it adds the prefix and corresponding text properties. EXTRA
5444 must be a string and replaces the `%s' specifier in the prefix format.
5445 CATEGORY (string, symbol or nil) may be used to overrule the default
5446 category taken from local variable or file name. It will replace the `%c'
5447 specifier in the format. DOTIME, when non-nil, indicates that a
5448 time-of-day should be extracted from TXT for sorting of this entry, and for
5449 the `%t' specifier in the format. When DOTIME is a string, this string is
5450 searched for a time before TXT is. TAGS can be the tags of the headline.
5451 Any match of REMOVE-RE will be removed from TXT."
5452 (save-match-data
5453 ;; Diary entries sometimes have extra whitespace at the beginning
5454 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5456 ;; Fix the tags part in txt
5457 (setq txt (org-agenda-fix-displayed-tags
5458 txt tags
5459 org-agenda-show-inherited-tags
5460 org-agenda-hide-tags-regexp))
5461 (let* ((category (or category
5462 (if (stringp org-category)
5463 org-category
5464 (and org-category (symbol-name org-category)))
5465 (if buffer-file-name
5466 (file-name-sans-extension
5467 (file-name-nondirectory buffer-file-name))
5468 "")))
5469 (category-icon (org-agenda-get-category-icon category))
5470 (category-icon (if category-icon
5471 (propertize " " 'display category-icon)
5472 ""))
5473 ;; time, tag, effort are needed for the eval of the prefix format
5474 (tag (if tags (nth (1- (length tags)) tags) ""))
5475 time effort neffort
5476 (ts (if dotime (concat
5477 (if (stringp dotime) dotime "")
5478 (and org-agenda-search-headline-for-time txt))))
5479 (time-of-day (and dotime (org-get-time-of-day ts)))
5480 stamp plain s0 s1 s2 rtn srp l
5481 duration thecategory)
5482 (and (org-mode-p) buffer-file-name
5483 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5484 (when (and dotime time-of-day)
5485 ;; Extract starting and ending time and move them to prefix
5486 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5487 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5488 (setq s0 (match-string 0 ts)
5489 srp (and stamp (match-end 3))
5490 s1 (match-string (if plain 1 2) ts)
5491 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5493 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5494 ;; them, we might want to remove them there to avoid duplication.
5495 ;; The user can turn this off with a variable.
5496 (if (and org-prefix-has-time
5497 org-agenda-remove-times-when-in-prefix (or stamp plain)
5498 (string-match (concat (regexp-quote s0) " *") txt)
5499 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5500 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5501 (= (match-beginning 0) 0)
5503 (setq txt (replace-match "" nil nil txt))))
5504 ;; Normalize the time(s) to 24 hour
5505 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5506 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5508 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5509 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5510 (setq s2
5511 (org-minutes-to-hh:mm-string
5512 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5514 ;; Compute the duration
5515 (when s2
5516 (setq duration (- (org-hh:mm-string-to-minutes s2)
5517 (org-hh:mm-string-to-minutes s1)))))
5519 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5520 txt)
5521 ;; Tags are in the string
5522 (if (or (eq org-agenda-remove-tags t)
5523 (and org-agenda-remove-tags
5524 org-prefix-has-tag))
5525 (setq txt (replace-match "" t t txt))
5526 (setq txt (replace-match
5527 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5528 (match-string 2 txt))
5529 t t txt))))
5530 (when (org-mode-p)
5531 (setq effort
5532 (condition-case nil
5533 (org-get-effort
5534 (or (get-text-property 0 'org-hd-marker txt)
5535 (get-text-property 0 'org-marker txt)))
5536 (error nil)))
5537 (when effort
5538 (setq neffort (org-duration-string-to-minutes effort)
5539 effort (setq effort (concat "[" effort "]")))))
5540 ;; prevent erroring out with %e format when there is no effort
5541 (or effort (setq effort ""))
5543 (when remove-re
5544 (while (string-match remove-re txt)
5545 (setq txt (replace-match "" t t txt))))
5547 ;; Set org-heading property on `txt' to mark the start of the
5548 ;; heading.
5549 (add-text-properties 0 (length txt) '(org-heading t) txt)
5551 ;; Prepare the variables needed in the eval of the compiled format
5552 (setq time (cond (s2 (concat
5553 (org-agenda-time-of-day-to-ampm-maybe s1)
5554 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5555 (if org-agenda-timegrid-use-ampm " ")))
5556 (s1 (concat
5557 (org-agenda-time-of-day-to-ampm-maybe s1)
5558 (if org-agenda-timegrid-use-ampm
5559 "........ "
5560 "......")))
5561 (t ""))
5562 extra (or (and (not habitp) extra) "")
5563 category (if (symbolp category) (symbol-name category) category)
5564 thecategory (copy-sequence category))
5565 (if (string-match org-bracket-link-regexp category)
5566 (progn
5567 (setq l (if (match-end 3)
5568 (- (match-end 3) (match-beginning 3))
5569 (- (match-end 1) (match-beginning 1))))
5570 (when (< l (or org-prefix-category-length 0))
5571 (setq category (copy-sequence category))
5572 (org-add-props category nil
5573 'extra-space (make-string
5574 (- org-prefix-category-length l 1) ?\ ))))
5575 (if (and org-prefix-category-max-length
5576 (>= (length category) org-prefix-category-max-length))
5577 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5578 ;; Evaluate the compiled format
5579 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5581 ;; And finally add the text properties
5582 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5583 (org-add-props rtn nil
5584 'org-category (if thecategory (downcase thecategory) category)
5585 'tags (mapcar 'org-downcase-keep-props tags)
5586 'org-highest-priority org-highest-priority
5587 'org-lowest-priority org-lowest-priority
5588 'time-of-day time-of-day
5589 'duration duration
5590 'effort effort
5591 'effort-minutes neffort
5592 'txt txt
5593 'time time
5594 'extra extra
5595 'format org-prefix-format-compiled
5596 'dotime dotime))))
5598 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5599 "Remove tags string from TXT, and add a modified list of tags.
5600 The modified list may contain inherited tags, and tags matched by
5601 `org-agenda-hide-tags-regexp' will be removed."
5602 (when (or add-inherited hide-re)
5603 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5604 (setq txt (substring txt 0 (match-beginning 0))))
5605 (setq tags
5606 (delq nil
5607 (mapcar (lambda (tg)
5608 (if (or (and hide-re (string-match hide-re tg))
5609 (and (not add-inherited)
5610 (get-text-property 0 'inherited tg)))
5612 tg))
5613 tags)))
5614 (when tags
5615 (let ((have-i (get-text-property 0 'inherited (car tags)))
5617 (setq txt (concat txt " :"
5618 (mapconcat
5619 (lambda (x)
5620 (setq i (get-text-property 0 'inherited x))
5621 (if (and have-i (not i))
5622 (progn
5623 (setq have-i nil)
5624 (concat ":" x))
5626 tags ":")
5627 (if have-i "::" ":"))))))
5628 txt)
5630 (defun org-downcase-keep-props (s)
5631 (let ((props (text-properties-at 0 s)))
5632 (setq s (downcase s))
5633 (add-text-properties 0 (length s) props s)
5636 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5637 (defvar org-agenda-sorting-strategy-selected nil)
5639 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5640 (catch 'exit
5641 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5642 ((and todayp (member 'today (car org-agenda-time-grid))))
5643 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5644 ((member 'weekly (car org-agenda-time-grid)))
5645 (t (throw 'exit list)))
5646 (let* ((have (delq nil (mapcar
5647 (lambda (x) (get-text-property 1 'time-of-day x))
5648 list)))
5649 (string (nth 1 org-agenda-time-grid))
5650 (gridtimes (nth 2 org-agenda-time-grid))
5651 (req (car org-agenda-time-grid))
5652 (remove (member 'remove-match req))
5653 new time)
5654 (if (and (member 'require-timed req) (not have))
5655 ;; don't show empty grid
5656 (throw 'exit list))
5657 (while (setq time (pop gridtimes))
5658 (unless (and remove (member time have))
5659 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5660 (push (org-format-agenda-item
5661 nil string "" nil
5662 (concat (substring time 0 -2) ":" (substring time -2)))
5663 new)
5664 (put-text-property
5665 2 (length (car new)) 'face 'org-time-grid (car new))))
5666 (when (and todayp org-agenda-show-current-time-in-grid)
5667 (push (org-format-agenda-item
5669 org-agenda-current-time-string
5670 "" nil
5671 (format-time-string "%H:%M "))
5672 new)
5673 (put-text-property
5674 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5676 (if (member 'time-up org-agenda-sorting-strategy-selected)
5677 (append new list)
5678 (append list new)))))
5680 (defun org-compile-prefix-format (key)
5681 "Compile the prefix format into a Lisp form that can be evaluated.
5682 The resulting form is returned and stored in the variable
5683 `org-prefix-format-compiled'."
5684 (setq org-prefix-has-time nil org-prefix-has-tag nil
5685 org-prefix-category-length nil
5686 org-prefix-has-effort nil)
5687 (let ((s (cond
5688 ((stringp org-agenda-prefix-format)
5689 org-agenda-prefix-format)
5690 ((assq key org-agenda-prefix-format)
5691 (cdr (assq key org-agenda-prefix-format)))
5692 (t " %-12:c%?-12t% s")))
5693 (start 0)
5694 varform vars var e c f opt)
5695 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5696 s start)
5697 (setq var (or (cdr (assoc (match-string 4 s)
5698 '(("c" . category) ("t" . time) ("s" . extra)
5699 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5700 'eval)
5701 c (or (match-string 3 s) "")
5702 opt (match-beginning 1)
5703 start (1+ (match-beginning 0)))
5704 (if (equal var 'time) (setq org-prefix-has-time t))
5705 (if (equal var 'tag) (setq org-prefix-has-tag t))
5706 (if (equal var 'effort) (setq org-prefix-has-effort t))
5707 (setq f (concat "%" (match-string 2 s) "s"))
5708 (when (equal var 'category)
5709 (setq org-prefix-category-length
5710 (floor (abs (string-to-number (match-string 2 s)))))
5711 (setq org-prefix-category-max-length
5712 (let ((x (match-string 2 s)))
5713 (save-match-data
5714 (if (string-match "\\.[0-9]+" x)
5715 (string-to-number (substring (match-string 0 x) 1)))))))
5716 (if (eq var 'eval)
5717 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5718 (if opt
5719 (setq varform
5720 `(if (equal "" ,var)
5722 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5723 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5724 (setq s (replace-match "%s" t nil s))
5725 (push varform vars))
5726 (setq vars (nreverse vars))
5727 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5729 (defun org-set-sorting-strategy (key)
5730 (if (symbolp (car org-agenda-sorting-strategy))
5731 ;; the old format
5732 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5733 (setq org-agenda-sorting-strategy-selected
5734 (or (cdr (assq key org-agenda-sorting-strategy))
5735 (cdr (assq 'agenda org-agenda-sorting-strategy))
5736 '(time-up category-keep priority-down)))))
5738 (defun org-get-time-of-day (s &optional string mod24)
5739 "Check string S for a time of day.
5740 If found, return it as a military time number between 0 and 2400.
5741 If not found, return nil.
5742 The optional STRING argument forces conversion into a 5 character wide string
5743 HH:MM."
5744 (save-match-data
5745 (when
5746 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5747 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5748 (let* ((h (string-to-number (match-string 1 s)))
5749 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5750 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5751 (am-p (equal ampm "am"))
5752 (h1 (cond ((not ampm) h)
5753 ((= h 12) (if am-p 0 12))
5754 (t (+ h (if am-p 0 12)))))
5755 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5756 (mod h1 24) h1))
5757 (t0 (+ (* 100 h2) m))
5758 (t1 (concat (if (>= h1 24) "+" " ")
5759 (if (and org-agenda-time-leading-zero
5760 (< t0 1000)) "0" "")
5761 (if (< t0 100) "0" "")
5762 (if (< t0 10) "0" "")
5763 (int-to-string t0))))
5764 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5766 (defvar org-agenda-before-sorting-filter-function nil
5767 "Function to be applied to agenda items prior to sorting.
5768 Prior to sorting also means just before they are inserted into the agenda.
5770 To aid sorting, you may revisit the original entries and add more text
5771 properties which will later be used by the sorting functions.
5773 The function should take a string argument, an agenda line.
5774 It has access to the text properties in that line, which contain among
5775 other things, the property `org-hd-marker' that points to the entry
5776 where the line comes from. Note that not all lines going into the agenda
5777 have this property, only most.
5779 The function should return the modified string. It is probably best
5780 to ONLY change text properties.
5782 You can also use this function as a filter, by returning nil for lines
5783 you don't want to have in the agenda at all. For this application, you
5784 could bind the variable in the options section of a custom command.")
5786 (defun org-finalize-agenda-entries (list &optional nosort)
5787 "Sort and concatenate the agenda items."
5788 (setq list (mapcar 'org-agenda-highlight-todo list))
5789 (if nosort
5790 list
5791 (when org-agenda-before-sorting-filter-function
5792 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5793 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5795 (defun org-agenda-highlight-todo (x)
5796 (let ((org-done-keywords org-done-keywords-for-agenda)
5797 (case-fold-search nil)
5799 (if (eq x 'line)
5800 (save-excursion
5801 (beginning-of-line 1)
5802 (setq re (org-get-at-bol 'org-todo-regexp))
5803 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5804 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5805 (add-text-properties (match-beginning 0) (match-end 1)
5806 (list 'face (org-get-todo-face 1)))
5807 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5808 (delete-region (match-beginning 1) (1- (match-end 0)))
5809 (goto-char (match-beginning 1))
5810 (insert (format org-agenda-todo-keyword-format s)))))
5811 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5812 (setq re (get-text-property 0 'org-todo-regexp x))
5813 (when (and re
5814 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5815 x (or pl 0)) pl))
5816 (add-text-properties
5817 (or (match-end 1) (match-end 0)) (match-end 0)
5818 (list 'face (org-get-todo-face (match-string 2 x)))
5820 (when (match-end 1)
5821 (setq x (concat (substring x 0 (match-end 1))
5822 (format org-agenda-todo-keyword-format
5823 (match-string 2 x))
5824 (org-add-props " " (text-properties-at 0 x))
5825 (substring x (match-end 3)))))))
5826 x)))
5828 (defsubst org-cmp-priority (a b)
5829 "Compare the priorities of string A and B."
5830 (let ((pa (or (get-text-property 1 'priority a) 0))
5831 (pb (or (get-text-property 1 'priority b) 0)))
5832 (cond ((> pa pb) +1)
5833 ((< pa pb) -1)
5834 (t nil))))
5836 (defsubst org-cmp-effort (a b)
5837 "Compare the effort values of string A and B."
5838 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5839 (ea (or (get-text-property 1 'effort-minutes a) def))
5840 (eb (or (get-text-property 1 'effort-minutes b) def)))
5841 (cond ((> ea eb) +1)
5842 ((< ea eb) -1)
5843 (t nil))))
5845 (defsubst org-cmp-category (a b)
5846 "Compare the string values of categories of strings A and B."
5847 (let ((ca (or (get-text-property 1 'org-category a) ""))
5848 (cb (or (get-text-property 1 'org-category b) "")))
5849 (cond ((string-lessp ca cb) -1)
5850 ((string-lessp cb ca) +1)
5851 (t nil))))
5853 (defsubst org-cmp-todo-state (a b)
5854 "Compare the todo states of strings A and B."
5855 (let* ((ma (or (get-text-property 1 'org-marker a)
5856 (get-text-property 1 'org-hd-marker a)))
5857 (mb (or (get-text-property 1 'org-marker b)
5858 (get-text-property 1 'org-hd-marker b)))
5859 (fa (and ma (marker-buffer ma)))
5860 (fb (and mb (marker-buffer mb)))
5861 (todo-kwds
5862 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5863 (and fb (with-current-buffer fb org-todo-keywords-1))))
5864 (ta (or (get-text-property 1 'todo-state a) ""))
5865 (tb (or (get-text-property 1 'todo-state b) ""))
5866 (la (- (length (member ta todo-kwds))))
5867 (lb (- (length (member tb todo-kwds))))
5868 (donepa (member ta org-done-keywords-for-agenda))
5869 (donepb (member tb org-done-keywords-for-agenda)))
5870 (cond ((and donepa (not donepb)) -1)
5871 ((and (not donepa) donepb) +1)
5872 ((< la lb) -1)
5873 ((< lb la) +1)
5874 (t nil))))
5876 (defsubst org-cmp-alpha (a b)
5877 "Compare the headlines, alphabetically."
5878 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5879 (plb (text-property-any 0 (length b) 'org-heading t b))
5880 (ta (and pla (substring a pla)))
5881 (tb (and plb (substring b plb))))
5882 (when pla
5883 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5884 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5885 (setq ta (substring ta (match-end 0))))
5886 (setq ta (downcase ta)))
5887 (when plb
5888 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5889 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5890 (setq tb (substring tb (match-end 0))))
5891 (setq tb (downcase tb)))
5892 (cond ((not ta) +1)
5893 ((not tb) -1)
5894 ((string-lessp ta tb) -1)
5895 ((string-lessp tb ta) +1)
5896 (t nil))))
5898 (defsubst org-cmp-tag (a b)
5899 "Compare the string values of the first tags of A and B."
5900 (let ((ta (car (last (get-text-property 1 'tags a))))
5901 (tb (car (last (get-text-property 1 'tags b)))))
5902 (cond ((not ta) +1)
5903 ((not tb) -1)
5904 ((string-lessp ta tb) -1)
5905 ((string-lessp tb ta) +1)
5906 (t nil))))
5908 (defsubst org-cmp-time (a b)
5909 "Compare the time-of-day values of strings A and B."
5910 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5911 (ta (or (get-text-property 1 'time-of-day a) def))
5912 (tb (or (get-text-property 1 'time-of-day b) def)))
5913 (cond ((< ta tb) -1)
5914 ((< tb ta) +1)
5915 (t nil))))
5917 (defsubst org-cmp-habit-p (a b)
5918 "Compare the todo states of strings A and B."
5919 (let ((ha (get-text-property 1 'org-habit-p a))
5920 (hb (get-text-property 1 'org-habit-p b)))
5921 (cond ((and ha (not hb)) -1)
5922 ((and (not ha) hb) +1)
5923 (t nil))))
5925 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5927 (defun org-entries-lessp (a b)
5928 "Predicate for sorting agenda entries."
5929 ;; The following variables will be used when the form is evaluated.
5930 ;; So even though the compiler complains, keep them.
5931 (let* ((ss org-agenda-sorting-strategy-selected)
5932 (time-up (and (org-em 'time-up 'time-down ss)
5933 (org-cmp-time a b)))
5934 (time-down (if time-up (- time-up) nil))
5935 (priority-up (and (org-em 'priority-up 'priority-down ss)
5936 (org-cmp-priority a b)))
5937 (priority-down (if priority-up (- priority-up) nil))
5938 (effort-up (and (org-em 'effort-up 'effort-down ss)
5939 (org-cmp-effort a b)))
5940 (effort-down (if effort-up (- effort-up) nil))
5941 (category-up (and (or (org-em 'category-up 'category-down ss)
5942 (memq 'category-keep ss))
5943 (org-cmp-category a b)))
5944 (category-down (if category-up (- category-up) nil))
5945 (category-keep (if category-up +1 nil))
5946 (tag-up (and (org-em 'tag-up 'tag-down ss)
5947 (org-cmp-tag a b)))
5948 (tag-down (if tag-up (- tag-up) nil))
5949 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5950 (org-cmp-todo-state a b)))
5951 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5952 (habit-up (and (org-em 'habit-up 'habit-down ss)
5953 (org-cmp-habit-p a b)))
5954 (habit-down (if habit-up (- habit-up) nil))
5955 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5956 (org-cmp-alpha a b)))
5957 (alpha-down (if alpha-up (- alpha-up) nil))
5958 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5959 user-defined-up user-defined-down)
5960 (if (and need-user-cmp org-agenda-cmp-user-defined
5961 (functionp org-agenda-cmp-user-defined))
5962 (setq user-defined-up
5963 (funcall org-agenda-cmp-user-defined a b)
5964 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5965 (cdr (assoc
5966 (eval (cons 'or org-agenda-sorting-strategy-selected))
5967 '((-1 . t) (1 . nil) (nil . nil))))))
5969 ;;; Agenda restriction lock
5971 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5972 "Overlay to mark the headline to which agenda commands are restricted.")
5973 (overlay-put org-agenda-restriction-lock-overlay
5974 'face 'org-agenda-restriction-lock)
5975 (overlay-put org-agenda-restriction-lock-overlay
5976 'help-echo "Agendas are currently limited to this subtree.")
5977 (org-detach-overlay org-agenda-restriction-lock-overlay)
5979 (defun org-agenda-set-restriction-lock (&optional type)
5980 "Set restriction lock for agenda, to current subtree or file.
5981 Restriction will be the file if TYPE is `file', or if type is the
5982 universal prefix '(4), or if the cursor is before the first headline
5983 in the file. Otherwise, restriction will be to the current subtree."
5984 (interactive "P")
5985 (and (equal type '(4)) (setq type 'file))
5986 (setq type (cond
5987 (type type)
5988 ((org-at-heading-p) 'subtree)
5989 ((condition-case nil (org-back-to-heading t) (error nil))
5990 'subtree)
5991 (t 'file)))
5992 (if (eq type 'subtree)
5993 (progn
5994 (setq org-agenda-restrict t)
5995 (setq org-agenda-overriding-restriction 'subtree)
5996 (put 'org-agenda-files 'org-restrict
5997 (list (buffer-file-name (buffer-base-buffer))))
5998 (org-back-to-heading t)
5999 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6000 (move-marker org-agenda-restrict-begin (point))
6001 (move-marker org-agenda-restrict-end
6002 (save-excursion (org-end-of-subtree t)))
6003 (message "Locking agenda restriction to subtree"))
6004 (put 'org-agenda-files 'org-restrict
6005 (list (buffer-file-name (buffer-base-buffer))))
6006 (setq org-agenda-restrict nil)
6007 (setq org-agenda-overriding-restriction 'file)
6008 (move-marker org-agenda-restrict-begin nil)
6009 (move-marker org-agenda-restrict-end nil)
6010 (message "Locking agenda restriction to file"))
6011 (setq current-prefix-arg nil)
6012 (org-agenda-maybe-redo))
6014 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6015 "Remove the agenda restriction lock."
6016 (interactive "P")
6017 (org-detach-overlay org-agenda-restriction-lock-overlay)
6018 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6019 (setq org-agenda-overriding-restriction nil)
6020 (setq org-agenda-restrict nil)
6021 (put 'org-agenda-files 'org-restrict nil)
6022 (move-marker org-agenda-restrict-begin nil)
6023 (move-marker org-agenda-restrict-end nil)
6024 (setq current-prefix-arg nil)
6025 (message "Agenda restriction lock removed")
6026 (or noupdate (org-agenda-maybe-redo)))
6028 (defun org-agenda-maybe-redo ()
6029 "If there is any window showing the agenda view, update it."
6030 (let ((w (get-buffer-window org-agenda-buffer-name t))
6031 (w0 (selected-window)))
6032 (when w
6033 (select-window w)
6034 (org-agenda-redo)
6035 (select-window w0)
6036 (if org-agenda-overriding-restriction
6037 (message "Agenda view shifted to new %s restriction"
6038 org-agenda-overriding-restriction)
6039 (message "Agenda restriction lock removed")))))
6041 ;;; Agenda commands
6043 (defun org-agenda-check-type (error &rest types)
6044 "Check if agenda buffer is of allowed type.
6045 If ERROR is non-nil, throw an error, otherwise just return nil."
6046 (if (memq org-agenda-type types)
6048 (if error
6049 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6050 nil)))
6052 (defun org-agenda-quit ()
6053 "Exit agenda by removing the window or the buffer."
6054 (interactive)
6055 (if org-agenda-columns-active
6056 (org-columns-quit)
6057 (let ((buf (current-buffer)))
6058 (if (eq org-agenda-window-setup 'other-frame)
6059 (progn
6060 (kill-buffer buf)
6061 (org-agenda-reset-markers)
6062 (org-columns-remove-overlays)
6063 (setq org-agenda-archives-mode nil)
6064 (delete-frame))
6065 (and (not (eq org-agenda-window-setup 'current-window))
6066 (not (one-window-p))
6067 (delete-window))
6068 (kill-buffer buf)
6069 (org-agenda-reset-markers)
6070 (org-columns-remove-overlays)
6071 (setq org-agenda-archives-mode nil)))
6072 ;; Maybe restore the pre-agenda window configuration.
6073 (and org-agenda-restore-windows-after-quit
6074 (not (eq org-agenda-window-setup 'other-frame))
6075 org-pre-agenda-window-conf
6076 (set-window-configuration org-pre-agenda-window-conf))))
6078 (defun org-agenda-exit ()
6079 "Exit agenda by removing the window or the buffer.
6080 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6081 Org-mode buffers visited directly by the user will not be touched."
6082 (interactive)
6083 (org-release-buffers org-agenda-new-buffers)
6084 (setq org-agenda-new-buffers nil)
6085 (org-agenda-quit))
6087 (defun org-agenda-execute (arg)
6088 "Execute another agenda command, keeping same window.
6089 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6090 in the agenda."
6091 (interactive "P")
6092 (let ((org-agenda-window-setup 'current-window))
6093 (org-agenda arg)))
6095 (defun org-agenda-redo ()
6096 "Rebuild Agenda.
6097 When this is the global TODO list, a prefix argument will be interpreted."
6098 (interactive)
6099 (let* ((org-agenda-keep-modes t)
6100 (filter org-agenda-filter)
6101 (preset (get 'org-agenda-filter :preset-filter))
6102 (org-agenda-filter-while-redo (or filter preset))
6103 (cols org-agenda-columns-active)
6104 (line (org-current-line))
6105 (window-line (- line (org-current-line (window-start))))
6106 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6107 (put 'org-agenda-filter :preset-filter nil)
6108 (and cols (org-columns-quit))
6109 (message "Rebuilding agenda buffer...")
6110 (org-let lprops '(eval org-agenda-redo-command))
6111 (setq org-agenda-undo-list nil
6112 org-agenda-pending-undo-list nil)
6113 (message "Rebuilding agenda buffer...done")
6114 (put 'org-agenda-filter :preset-filter preset)
6115 (and (or filter preset) (org-agenda-filter-apply filter))
6116 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6117 (org-goto-line line)
6118 (recenter window-line)))
6121 (defvar org-global-tags-completion-table nil)
6122 (defvar org-agenda-filter-form nil)
6123 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6124 "Keep only those lines in the agenda buffer that have a specific tag.
6125 The tag is selected with its fast selection letter, as configured.
6126 With prefix argument STRIP, remove all lines that do have the tag.
6127 A lisp caller can specify CHAR. NARROW means that the new tag should be
6128 used to narrow the search - the interactive user can also press `-' or `+'
6129 to switch to narrowing."
6130 (interactive "P")
6131 (let* ((alist org-tag-alist-for-agenda)
6132 (tag-chars (mapconcat
6133 (lambda (x) (if (and (not (symbolp (car x)))
6134 (cdr x))
6135 (char-to-string (cdr x))
6136 ""))
6137 alist ""))
6138 (efforts (org-split-string
6139 (or (cdr (assoc (concat org-effort-property "_ALL")
6140 org-global-properties))
6141 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6142 "")))
6143 (effort-op org-agenda-filter-effort-default-operator)
6144 (effort-prompt "")
6145 (inhibit-read-only t)
6146 (current org-agenda-filter)
6147 maybe-refresh a n tag)
6148 (unless char
6149 (message
6150 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6151 (if narrow "Narrow" "Filter") tag-chars
6152 (if org-agenda-auto-exclude-function "[RET], " ""))
6153 (setq char (read-char)))
6154 (when (member char '(?+ ?-))
6155 ;; Narrowing down
6156 (cond ((equal char ?-) (setq strip t narrow t))
6157 ((equal char ?+) (setq strip nil narrow t)))
6158 (message
6159 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6160 (setq char (read-char)))
6161 (when (member char '(?< ?> ?= ??))
6162 ;; An effort operator
6163 (setq effort-op (char-to-string char))
6164 (setq alist nil) ; to make sure it will be interpreted as effort.
6165 (unless (equal char ??)
6166 (loop for i from 0 to 9 do
6167 (setq effort-prompt
6168 (concat
6169 effort-prompt " ["
6170 (if (= i 9) "0" (int-to-string (1+ i)))
6171 "]" (nth i efforts))))
6172 (message "Effort%s: %s " effort-op effort-prompt)
6173 (setq char (read-char))
6174 (when (or (< char ?0) (> char ?9))
6175 (error "Need 1-9,0 to select effort" ))))
6176 (when (equal char ?\t)
6177 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6178 (org-set-local 'org-global-tags-completion-table
6179 (org-global-tags-completion-table)))
6180 (let ((completion-ignore-case t))
6181 (setq tag (org-icompleting-read
6182 "Tag: " org-global-tags-completion-table))))
6183 (cond
6184 ((equal char ?\r)
6185 (org-agenda-filter-by-tag-show-all)
6186 (when org-agenda-auto-exclude-function
6187 (setq org-agenda-filter '())
6188 (dolist (tag (org-agenda-get-represented-tags))
6189 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6190 (if modifier
6191 (push modifier org-agenda-filter))))
6192 (if (not (null org-agenda-filter))
6193 (org-agenda-filter-apply org-agenda-filter)))
6194 (setq maybe-refresh t))
6195 ((equal char ?/)
6196 (org-agenda-filter-by-tag-show-all)
6197 (when (get 'org-agenda-filter :preset-filter)
6198 (org-agenda-filter-apply org-agenda-filter))
6199 (setq maybe-refresh t))
6200 ((or (equal char ?\ )
6201 (setq a (rassoc char alist))
6202 (and (>= char ?0) (<= char ?9)
6203 (setq n (if (= char ?0) 9 (- char ?0 1))
6204 tag (concat effort-op (nth n efforts))
6205 a (cons tag nil)))
6206 (and (= char ??)
6207 (setq tag "?eff")
6208 a (cons tag nil))
6209 (and tag (setq a (cons tag nil))))
6210 (org-agenda-filter-by-tag-show-all)
6211 (setq tag (car a))
6212 (setq org-agenda-filter
6213 (cons (concat (if strip "-" "+") tag)
6214 (if narrow current nil)))
6215 (org-agenda-filter-apply org-agenda-filter)
6216 (setq maybe-refresh t))
6217 (t (error "Invalid tag selection character %c" char)))
6218 (when (and maybe-refresh
6219 (eq org-agenda-clockreport-mode 'with-filter))
6220 (org-agenda-redo))))
6222 (defun org-agenda-get-represented-tags ()
6223 "Get a list of all tags currently represented in the agenda."
6224 (let (p tags)
6225 (save-excursion
6226 (goto-char (point-min))
6227 (while (setq p (next-single-property-change (point) 'tags))
6228 (goto-char p)
6229 (mapc (lambda (x) (add-to-list 'tags x))
6230 (get-text-property (point) 'tags))))
6231 tags))
6233 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6234 "Refine the current filter. See `org-agenda-filter-by-tag'."
6235 (interactive "P")
6236 (org-agenda-filter-by-tag strip char 'refine))
6238 (defun org-agenda-filter-make-matcher ()
6239 "Create the form that tests a line for the agenda filter."
6240 (let (f f1)
6241 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6242 org-agenda-filter))
6243 (if (member x '("-" "+"))
6244 (setq f1 (if (equal x "-") 'tags '(not tags)))
6245 (if (string-match "[<=>?]" x)
6246 (setq f1 (org-agenda-filter-effort-form x))
6247 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6248 (if (equal (string-to-char x) ?-)
6249 (setq f1 (list 'not f1))))
6250 (push f1 f))
6251 (cons 'and (nreverse f))))
6253 (defun org-agenda-filter-effort-form (e)
6254 "Return the form to compare the effort of the current line with what E says.
6255 E looks like \"+<2:25\"."
6256 (let (op)
6257 (setq e (substring e 1))
6258 (setq op (string-to-char e) e (substring e 1))
6259 (setq op (cond ((equal op ?<) '<=)
6260 ((equal op ?>) '>=)
6261 ((equal op ??) op)
6262 (t '=)))
6263 (list 'org-agenda-compare-effort (list 'quote op)
6264 (org-duration-string-to-minutes e))))
6266 (defun org-agenda-compare-effort (op value)
6267 "Compare the effort of the current line with VALUE, using OP.
6268 If the line does not have an effort defined, return nil."
6269 (let ((eff (org-get-at-bol 'effort-minutes)))
6270 (if (equal op ??)
6271 (not eff)
6272 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6273 value))))
6275 (defun org-agenda-filter-apply (filter)
6276 "Set FILTER as the new agenda filter and apply it."
6277 (let (tags)
6278 (setq org-agenda-filter filter
6279 org-agenda-filter-form (org-agenda-filter-make-matcher))
6280 (org-agenda-set-mode-name)
6281 (save-excursion
6282 (goto-char (point-min))
6283 (while (not (eobp))
6284 (if (org-get-at-bol 'org-marker)
6285 (progn
6286 (setq tags (org-get-at-bol 'tags)) ; used in eval
6287 (if (not (eval org-agenda-filter-form))
6288 (org-agenda-filter-by-tag-hide-line))
6289 (beginning-of-line 2))
6290 (beginning-of-line 2))))
6291 (if (get-char-property (point) 'invisible)
6292 (org-agenda-previous-line))))
6294 (defun org-agenda-filter-by-tag-hide-line ()
6295 (let (ov)
6296 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6297 (point-at-eol)))
6298 (overlay-put ov 'invisible t)
6299 (overlay-put ov 'type 'tags-filter)
6300 (push ov org-agenda-filter-overlays)))
6302 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6303 (setq pos (or pos (point)))
6304 (save-excursion
6305 (dolist (ov (overlays-at pos))
6306 (when (and (overlay-get ov 'invisible)
6307 (eq (overlay-get ov 'type) 'tags-filter))
6308 (goto-char pos)
6309 (if (< (overlay-start ov) (point-at-eol))
6310 (move-overlay ov (point-at-eol)
6311 (overlay-end ov)))))))
6313 (defun org-agenda-filter-by-tag-show-all ()
6314 (mapc 'delete-overlay org-agenda-filter-overlays)
6315 (setq org-agenda-filter-overlays nil)
6316 (setq org-agenda-filter nil)
6317 (setq org-agenda-filter-form nil)
6318 (org-agenda-set-mode-name))
6320 (defun org-agenda-manipulate-query-add ()
6321 "Manipulate the query by adding a search term with positive selection.
6322 Positive selection means the term must be matched for selection of an entry."
6323 (interactive)
6324 (org-agenda-manipulate-query ?\[))
6325 (defun org-agenda-manipulate-query-subtract ()
6326 "Manipulate the query by adding a search term with negative selection.
6327 Negative selection means term must not be matched for selection of an entry."
6328 (interactive)
6329 (org-agenda-manipulate-query ?\]))
6330 (defun org-agenda-manipulate-query-add-re ()
6331 "Manipulate the query by adding a search regexp with positive selection.
6332 Positive selection means the regexp must match for selection of an entry."
6333 (interactive)
6334 (org-agenda-manipulate-query ?\{))
6335 (defun org-agenda-manipulate-query-subtract-re ()
6336 "Manipulate the query by adding a search regexp with negative selection.
6337 Negative selection means regexp must not match for selection of an entry."
6338 (interactive)
6339 (org-agenda-manipulate-query ?\}))
6340 (defun org-agenda-manipulate-query (char)
6341 (cond
6342 ((memq org-agenda-type '(timeline agenda))
6343 (let ((org-agenda-include-inactive-timestamps t))
6344 (org-agenda-redo))
6345 (message "Display now includes inactive timestamps as well"))
6346 ((eq org-agenda-type 'search)
6347 (org-add-to-string
6348 'org-agenda-query-string
6349 (if org-agenda-last-search-view-search-was-boolean
6350 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6351 (?\{ . " +{}") (?\} . " -{}"))))
6352 " "))
6353 (setq org-agenda-redo-command
6354 (list 'org-search-view
6355 org-todo-only
6356 org-agenda-query-string
6357 (+ (length org-agenda-query-string)
6358 (if (member char '(?\{ ?\})) 0 1))))
6359 (set-register org-agenda-query-register org-agenda-query-string)
6360 (org-agenda-redo))
6361 (t (error "Cannot manipulate query for %s-type agenda buffers"
6362 org-agenda-type))))
6364 (defun org-add-to-string (var string)
6365 (set var (concat (symbol-value var) string)))
6367 (defun org-agenda-goto-date (date)
6368 "Jump to DATE in agenda."
6369 (interactive (list (let ((org-read-date-prefer-future
6370 (eval org-agenda-jump-prefer-future)))
6371 (org-read-date))))
6372 (org-agenda-list nil date))
6374 (defun org-agenda-goto-today ()
6375 "Go to today."
6376 (interactive)
6377 (org-agenda-check-type t 'timeline 'agenda)
6378 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6379 (cond
6380 (tdpos (goto-char tdpos))
6381 ((eq org-agenda-type 'agenda)
6382 (let* ((sd (org-agenda-compute-starting-span
6383 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6384 (org-agenda-overriding-arguments org-agenda-last-arguments))
6385 (setf (nth 1 org-agenda-overriding-arguments) sd)
6386 (org-agenda-redo)
6387 (org-agenda-find-same-or-today-or-agenda)))
6388 (t (error "Cannot find today")))))
6390 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6391 (goto-char
6392 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6393 (text-property-any (point-min) (point-max) 'org-today t)
6394 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6395 (point-min))))
6397 (defun org-agenda-later (arg)
6398 "Go forward in time by thee current span.
6399 With prefix ARG, go forward that many times the current span."
6400 (interactive "p")
6401 (org-agenda-check-type t 'agenda)
6402 (let* ((span org-agenda-current-span)
6403 (sd org-starting-day)
6404 (greg (calendar-gregorian-from-absolute sd))
6405 (cnt (org-get-at-bol 'org-day-cnt))
6406 greg2)
6407 (cond
6408 ((eq span 'day)
6409 (setq sd (+ arg sd)))
6410 ((eq span 'week)
6411 (setq sd (+ (* 7 arg) sd)))
6412 ((eq span 'month)
6413 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6414 sd (calendar-absolute-from-gregorian greg2))
6415 (setcar greg2 (1+ (car greg2))))
6416 ((eq span 'year)
6417 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6418 sd (calendar-absolute-from-gregorian greg2))
6419 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6421 (setq sd (+ (* span arg) sd))))
6422 (let ((org-agenda-overriding-arguments
6423 (list (car org-agenda-last-arguments) sd span t)))
6424 (org-agenda-redo)
6425 (org-agenda-find-same-or-today-or-agenda cnt))))
6427 (defun org-agenda-earlier (arg)
6428 "Go backward in time by the current span.
6429 With prefix ARG, go backward that many times the current span."
6430 (interactive "p")
6431 (org-agenda-later (- arg)))
6433 (defun org-agenda-view-mode-dispatch ()
6434 "Call one of the view mode commands."
6435 (interactive)
6436 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6437 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6438 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6439 [E]ntryText")
6440 (let ((a (read-char-exclusive)))
6441 (case a
6442 (?\ (call-interactively 'org-agenda-reset-view))
6443 (?d (call-interactively 'org-agenda-day-view))
6444 (?w (call-interactively 'org-agenda-week-view))
6445 (?m (call-interactively 'org-agenda-month-view))
6446 (?y (call-interactively 'org-agenda-year-view))
6447 (?l (call-interactively 'org-agenda-log-mode))
6448 (?L (org-agenda-log-mode '(4)))
6449 (?c (org-agenda-log-mode 'clockcheck))
6450 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6451 (?a (call-interactively 'org-agenda-archives-mode))
6452 (?A (org-agenda-archives-mode 'files))
6453 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6454 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6455 (?G (call-interactively 'org-agenda-toggle-time-grid))
6456 (?D (call-interactively 'org-agenda-toggle-diary))
6457 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6458 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6459 (org-agenda-check-type t 'timeline 'agenda)
6460 (org-agenda-redo))
6461 (message "Display now includes inactive timestamps as well"))
6462 (?q (message "Abort"))
6463 (otherwise (error "Invalid key" )))))
6465 (defun org-agenda-reset-view ()
6466 "Switch to default view for agenda."
6467 (interactive)
6468 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6469 (defun org-agenda-day-view (&optional day-of-year)
6470 "Switch to daily view for agenda.
6471 With argument DAY-OF-YEAR, switch to that day of the year."
6472 (interactive "P")
6473 (org-agenda-change-time-span 'day day-of-year))
6474 (defun org-agenda-week-view (&optional iso-week)
6475 "Switch to daily view for agenda.
6476 With argument ISO-WEEK, switch to the corresponding ISO week.
6477 If ISO-WEEK has more then 2 digits, only the last two encode the
6478 week. Any digits before this encode a year. So 200712 means
6479 week 12 of year 2007. Years in the range 1938-2037 can also be
6480 written as 2-digit years."
6481 (interactive "P")
6482 (org-agenda-change-time-span 'week iso-week))
6483 (defun org-agenda-month-view (&optional month)
6484 "Switch to monthly view for agenda.
6485 With argument MONTH, switch to that month."
6486 (interactive "P")
6487 (org-agenda-change-time-span 'month month))
6488 (defun org-agenda-year-view (&optional year)
6489 "Switch to yearly view for agenda.
6490 With argument YEAR, switch to that year.
6491 If MONTH has more then 2 digits, only the last two encode the
6492 month. Any digits before this encode a year. So 200712 means
6493 December year 2007. Years in the range 1938-2037 can also be
6494 written as 2-digit years."
6495 (interactive "P")
6496 (when year
6497 (setq year (org-small-year-to-year year)))
6498 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6499 (org-agenda-change-time-span 'year year)
6500 (error "Abort")))
6502 (defun org-agenda-change-time-span (span &optional n)
6503 "Change the agenda view to SPAN.
6504 SPAN may be `day', `week', `month', `year'."
6505 (org-agenda-check-type t 'agenda)
6506 (if (and (not n) (equal org-agenda-current-span span))
6507 (error "Viewing span is already \"%s\"" span))
6508 (let* ((sd (or (org-get-at-bol 'day)
6509 org-starting-day))
6510 (sd (org-agenda-compute-starting-span sd span n))
6511 (org-agenda-overriding-arguments
6512 (or org-agenda-overriding-arguments
6513 (list (car org-agenda-last-arguments) sd span t))))
6514 (org-agenda-redo)
6515 (org-agenda-find-same-or-today-or-agenda))
6516 (org-agenda-set-mode-name)
6517 (message "Switched to %s view" span))
6519 (defun org-agenda-compute-starting-span (sd span &optional n)
6520 "Compute starting date for agenda.
6521 SPAN may be `day', `week', `month', `year'. The return value
6522 is a cons cell with the starting date and the number of days,
6523 so that the date SD will be in that range."
6524 (let* ((greg (calendar-gregorian-from-absolute sd))
6525 (dg (nth 1 greg))
6526 (mg (car greg))
6527 (yg (nth 2 greg)))
6528 (cond
6529 ((eq span 'day)
6530 (when n
6531 (setq sd (+ (calendar-absolute-from-gregorian
6532 (list mg 1 yg))
6533 n -1))))
6534 ((eq span 'week)
6535 (let* ((nt (calendar-day-of-week
6536 (calendar-gregorian-from-absolute sd)))
6537 (d (if org-agenda-start-on-weekday
6538 (- nt org-agenda-start-on-weekday)
6541 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6542 (when n
6543 (require 'cal-iso)
6544 (when (> n 99)
6545 (setq y1 (org-small-year-to-year (/ n 100))
6546 n (mod n 100)))
6547 (setq sd
6548 (calendar-absolute-from-iso
6549 (list n 1
6550 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6551 ((eq span 'month)
6552 (let (y1)
6553 (when (and n (> n 99))
6554 (setq y1 (org-small-year-to-year (/ n 100))
6555 n (mod n 100)))
6556 (setq sd (calendar-absolute-from-gregorian
6557 (list (or n mg) 1 (or y1 yg))))))
6558 ((eq span 'year)
6559 (setq sd (calendar-absolute-from-gregorian
6560 (list 1 1 (or n yg))))))
6561 sd))
6563 (defun org-agenda-next-date-line (&optional arg)
6564 "Jump to the next line indicating a date in agenda buffer."
6565 (interactive "p")
6566 (org-agenda-check-type t 'agenda 'timeline)
6567 (beginning-of-line 1)
6568 ;; This does not work if user makes date format that starts with a blank
6569 (if (looking-at "^\\S-") (forward-char 1))
6570 (if (not (re-search-forward "^\\S-" nil t arg))
6571 (progn
6572 (backward-char 1)
6573 (error "No next date after this line in this buffer")))
6574 (goto-char (match-beginning 0)))
6576 (defun org-agenda-previous-date-line (&optional arg)
6577 "Jump to the previous line indicating a date in agenda buffer."
6578 (interactive "p")
6579 (org-agenda-check-type t 'agenda 'timeline)
6580 (beginning-of-line 1)
6581 (if (not (re-search-backward "^\\S-" nil t arg))
6582 (error "No previous date before this line in this buffer")))
6584 ;; Initialize the highlight
6585 (defvar org-hl (make-overlay 1 1))
6586 (overlay-put org-hl 'face 'highlight)
6588 (defun org-highlight (begin end &optional buffer)
6589 "Highlight a region with overlay."
6590 (move-overlay org-hl begin end (or buffer (current-buffer))))
6592 (defun org-unhighlight ()
6593 "Detach overlay INDEX."
6594 (org-detach-overlay org-hl))
6596 ;; FIXME this is currently not used.
6597 (defun org-highlight-until-next-command (beg end &optional buffer)
6598 "Move the highlight overlay to BEG/END, remove it before the next command."
6599 (org-highlight beg end buffer)
6600 (add-hook 'pre-command-hook 'org-unhighlight-once))
6601 (defun org-unhighlight-once ()
6602 "Remove the highlight from its position, and this function from the hook."
6603 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6604 (org-unhighlight))
6606 (defun org-agenda-follow-mode ()
6607 "Toggle follow mode in an agenda buffer."
6608 (interactive)
6609 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6610 (org-agenda-set-mode-name)
6611 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6612 (org-agenda-show))
6613 (message "Follow mode is %s"
6614 (if org-agenda-follow-mode "on" "off")))
6616 (defun org-agenda-entry-text-mode (&optional arg)
6617 "Toggle entry text mode in an agenda buffer."
6618 (interactive "P")
6619 (setq org-agenda-entry-text-mode (or (integerp arg)
6620 (not org-agenda-entry-text-mode)))
6621 (org-agenda-entry-text-hide)
6622 (and org-agenda-entry-text-mode
6623 (let ((org-agenda-entry-text-maxlines
6624 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6625 (org-agenda-entry-text-show)))
6626 (org-agenda-set-mode-name)
6627 (message "Entry text mode is %s. Maximum number of lines is %d"
6628 (if org-agenda-entry-text-mode "on" "off")
6629 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6631 (defun org-agenda-clockreport-mode (&optional with-filter)
6632 "Toggle clocktable mode in an agenda buffer.
6633 With prefix arg WITH-FILTER, make the clocktable respect the current
6634 agenda filter."
6635 (interactive "P")
6636 (org-agenda-check-type t 'agenda)
6637 (if with-filter
6638 (setq org-agenda-clockreport-mode 'with-filter)
6639 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6640 (org-agenda-set-mode-name)
6641 (org-agenda-redo)
6642 (message "Clocktable mode is %s"
6643 (if org-agenda-clockreport-mode "on" "off")))
6645 (defun org-agenda-log-mode (&optional special)
6646 "Toggle log mode in an agenda buffer.
6647 With argument SPECIAL, show all possible log items, not only the ones
6648 configured in `org-agenda-log-mode-items'.
6649 With a double `C-u' prefix arg, show *only* log items, nothing else."
6650 (interactive "P")
6651 (org-agenda-check-type t 'agenda 'timeline)
6652 (setq org-agenda-show-log
6653 (cond
6654 ((equal special '(16)) 'only)
6655 ((eq special 'clockcheck)
6656 (if (eq org-agenda-show-log 'clockcheck)
6657 nil 'clockcheck))
6658 (special '(closed clock state))
6659 (t (not org-agenda-show-log))))
6660 (org-agenda-set-mode-name)
6661 (org-agenda-redo)
6662 (message "Log mode is %s"
6663 (if org-agenda-show-log "on" "off")))
6665 (defun org-agenda-archives-mode (&optional with-files)
6666 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6667 When called with a prefix argument, include all archive files as well."
6668 (interactive "P")
6669 (setq org-agenda-archives-mode
6670 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6671 (org-agenda-set-mode-name)
6672 (org-agenda-redo)
6673 (message
6674 "%s"
6675 (cond
6676 ((eq org-agenda-archives-mode nil)
6677 "No archives are included")
6678 ((eq org-agenda-archives-mode 'trees)
6679 (format "Trees with :%s: tag are included" org-archive-tag))
6680 ((eq org-agenda-archives-mode t)
6681 (format "Trees with :%s: tag and all active archive files are included"
6682 org-archive-tag)))))
6684 (defun org-agenda-toggle-diary ()
6685 "Toggle diary inclusion in an agenda buffer."
6686 (interactive)
6687 (org-agenda-check-type t 'agenda)
6688 (setq org-agenda-include-diary (not org-agenda-include-diary))
6689 (org-agenda-redo)
6690 (org-agenda-set-mode-name)
6691 (message "Diary inclusion turned %s"
6692 (if org-agenda-include-diary "on" "off")))
6694 (defun org-agenda-toggle-deadlines ()
6695 "Toggle inclusion of entries with a deadline in an agenda buffer."
6696 (interactive)
6697 (org-agenda-check-type t 'agenda)
6698 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6699 (org-agenda-redo)
6700 (org-agenda-set-mode-name)
6701 (message "Deadlines inclusion turned %s"
6702 (if org-agenda-include-deadlines "on" "off")))
6704 (defun org-agenda-toggle-time-grid ()
6705 "Toggle time grid in an agenda buffer."
6706 (interactive)
6707 (org-agenda-check-type t 'agenda)
6708 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6709 (org-agenda-redo)
6710 (org-agenda-set-mode-name)
6711 (message "Time-grid turned %s"
6712 (if org-agenda-use-time-grid "on" "off")))
6714 (defun org-agenda-set-mode-name ()
6715 "Set the mode name to indicate all the small mode settings."
6716 (setq mode-name
6717 (list "Org-Agenda"
6718 (if (get 'org-agenda-files 'org-restrict) " []" "")
6720 '(:eval (org-agenda-span-name org-agenda-current-span))
6721 (if org-agenda-follow-mode " Follow" "")
6722 (if org-agenda-entry-text-mode " ETxt" "")
6723 (if org-agenda-include-diary " Diary" "")
6724 (if org-agenda-include-deadlines " Ddl" "")
6725 (if org-agenda-use-time-grid " Grid" "")
6726 (if (and (boundp 'org-habit-show-habits)
6727 org-habit-show-habits) " Habit" "")
6728 (cond
6729 ((consp org-agenda-show-log) " LogAll")
6730 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6731 (org-agenda-show-log " Log")
6732 (t ""))
6733 (if (or org-agenda-filter (get 'org-agenda-filter
6734 :preset-filter))
6735 (concat " {" (mapconcat
6736 'identity
6737 (append (get 'org-agenda-filter
6738 :preset-filter)
6739 org-agenda-filter) "") "}")
6741 (if org-agenda-archives-mode
6742 (if (eq org-agenda-archives-mode t)
6743 " Archives"
6744 (format " :%s:" org-archive-tag))
6746 (if org-agenda-clockreport-mode
6747 (if (eq org-agenda-clockreport-mode 'with-filter)
6748 " Clock{}" " Clock")
6749 "")))
6750 (force-mode-line-update))
6752 (defun org-agenda-post-command-hook ()
6753 (setq org-agenda-type
6754 (or (get-text-property (point) 'org-agenda-type)
6755 (get-text-property (max (point-min) (1- (point)))
6756 'org-agenda-type))))
6758 (defun org-agenda-next-line ()
6759 "Move cursor to the next line, and show if follow mode is active."
6760 (interactive)
6761 (call-interactively 'next-line)
6762 (org-agenda-do-context-action))
6764 (defun org-agenda-previous-line ()
6765 "Move cursor to the previous line, and show if follow-mode is active."
6766 (interactive)
6767 (call-interactively 'previous-line)
6768 (org-agenda-do-context-action))
6770 (defun org-agenda-do-context-action ()
6771 "Show outline path and, maybe, follow mode window."
6772 (let ((m (org-get-at-bol 'org-marker)))
6773 (if (and org-agenda-follow-mode m)
6774 (org-agenda-show))
6775 (if (and m org-agenda-show-outline-path)
6776 (org-with-point-at m
6777 (org-display-outline-path t)))))
6779 (defun org-agenda-show-priority ()
6780 "Show the priority of the current item.
6781 This priority is composed of the main priority given with the [#A] cookies,
6782 and by additional input from the age of a schedules or deadline entry."
6783 (interactive)
6784 (let* ((pri (org-get-at-bol 'priority)))
6785 (message "Priority is %d" (if pri pri -1000))))
6787 (defun org-agenda-show-tags ()
6788 "Show the tags applicable to the current item."
6789 (interactive)
6790 (let* ((tags (org-get-at-bol 'tags)))
6791 (if tags
6792 (message "Tags are :%s:"
6793 (org-no-properties (mapconcat 'identity tags ":")))
6794 (message "No tags associated with this line"))))
6796 (defun org-agenda-goto (&optional highlight)
6797 "Go to the Org-mode file which contains the item at point."
6798 (interactive)
6799 (let* ((marker (or (org-get-at-bol 'org-marker)
6800 (org-agenda-error)))
6801 (buffer (marker-buffer marker))
6802 (pos (marker-position marker)))
6803 (switch-to-buffer-other-window buffer)
6804 (widen)
6805 (push-mark)
6806 (goto-char pos)
6807 (when (org-mode-p)
6808 (org-show-context 'agenda)
6809 (save-excursion
6810 (and (outline-next-heading)
6811 (org-flag-heading nil)))) ; show the next heading
6812 (when (outline-invisible-p)
6813 (show-entry)) ; display invisible text
6814 (recenter (/ (window-height) 2))
6815 (run-hooks 'org-agenda-after-show-hook)
6816 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6818 (defvar org-agenda-after-show-hook nil
6819 "Normal hook run after an item has been shown from the agenda.
6820 Point is in the buffer where the item originated.")
6822 (defun org-agenda-kill ()
6823 "Kill the entry or subtree belonging to the current agenda entry."
6824 (interactive)
6825 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6826 (let* ((marker (or (org-get-at-bol 'org-marker)
6827 (org-agenda-error)))
6828 (buffer (marker-buffer marker))
6829 (pos (marker-position marker))
6830 (type (org-get-at-bol 'type))
6831 dbeg dend (n 0) conf)
6832 (org-with-remote-undo buffer
6833 (with-current-buffer buffer
6834 (save-excursion
6835 (goto-char pos)
6836 (if (and (org-mode-p) (not (member type '("sexp"))))
6837 (setq dbeg (progn (org-back-to-heading t) (point))
6838 dend (org-end-of-subtree t t))
6839 (setq dbeg (point-at-bol)
6840 dend (min (point-max) (1+ (point-at-eol)))))
6841 (goto-char dbeg)
6842 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6843 (setq conf (or (eq t org-agenda-confirm-kill)
6844 (and (numberp org-agenda-confirm-kill)
6845 (> n org-agenda-confirm-kill))))
6846 (and conf
6847 (not (y-or-n-p
6848 (format "Delete entry with %d lines in buffer \"%s\"? "
6849 n (buffer-name buffer))))
6850 (error "Abort"))
6851 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6852 (with-current-buffer buffer (delete-region dbeg dend))
6853 (message "Agenda item and source killed"))))
6855 (defvar org-archive-default-command)
6856 (defun org-agenda-archive-default ()
6857 "Archive the entry or subtree belonging to the current agenda entry."
6858 (interactive)
6859 (require 'org-archive)
6860 (org-agenda-archive-with org-archive-default-command))
6862 (defun org-agenda-archive-default-with-confirmation ()
6863 "Archive the entry or subtree belonging to the current agenda entry."
6864 (interactive)
6865 (require 'org-archive)
6866 (org-agenda-archive-with org-archive-default-command 'confirm))
6868 (defun org-agenda-archive ()
6869 "Archive the entry or subtree belonging to the current agenda entry."
6870 (interactive)
6871 (org-agenda-archive-with 'org-archive-subtree))
6873 (defun org-agenda-archive-to-archive-sibling ()
6874 "Move the entry to the archive sibling."
6875 (interactive)
6876 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6878 (defun org-agenda-archive-with (cmd &optional confirm)
6879 "Move the entry to the archive sibling."
6880 (interactive)
6881 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6882 (let* ((marker (or (org-get-at-bol 'org-marker)
6883 (org-agenda-error)))
6884 (buffer (marker-buffer marker))
6885 (pos (marker-position marker)))
6886 (org-with-remote-undo buffer
6887 (with-current-buffer buffer
6888 (if (org-mode-p)
6889 (if (and confirm
6890 (not (y-or-n-p "Archive this subtree or entry? ")))
6891 (error "Abort")
6892 (save-excursion
6893 (goto-char pos)
6894 (org-remove-subtree-entries-from-agenda)
6895 (org-back-to-heading t)
6896 (funcall cmd)))
6897 (error "Archiving works only in Org-mode files"))))))
6899 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6900 "Remove all lines in the agenda that correspond to a given subtree.
6901 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6902 If this information is not given, the function uses the tree at point."
6903 (let ((buf (or buf (current-buffer))) m p)
6904 (save-excursion
6905 (unless (and beg end)
6906 (org-back-to-heading t)
6907 (setq beg (point))
6908 (org-end-of-subtree t)
6909 (setq end (point)))
6910 (set-buffer (get-buffer org-agenda-buffer-name))
6911 (save-excursion
6912 (goto-char (point-max))
6913 (beginning-of-line 1)
6914 (while (not (bobp))
6915 (when (and (setq m (org-get-at-bol 'org-marker))
6916 (equal buf (marker-buffer m))
6917 (setq p (marker-position m))
6918 (>= p beg)
6919 (< p end))
6920 (let ((inhibit-read-only t))
6921 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6922 (beginning-of-line 0))))))
6924 (defun org-agenda-refile (&optional goto rfloc no-update)
6925 "Refile the item at point."
6926 (interactive "P")
6927 (if (equal goto '(16))
6928 (org-refile-goto-last-stored)
6929 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6930 (org-agenda-error)))
6931 (buffer (marker-buffer marker))
6932 (pos (marker-position marker))
6933 (rfloc (or rfloc
6934 (org-refile-get-location
6935 (if goto "Goto" "Refile to") buffer
6936 org-refile-allow-creating-parent-nodes))))
6937 (with-current-buffer buffer
6938 (save-excursion
6939 (save-restriction
6940 (widen)
6941 (goto-char marker)
6942 (org-remove-subtree-entries-from-agenda)
6943 (org-refile goto buffer rfloc)))))
6944 (unless no-update (org-agenda-redo))))
6946 (defun org-agenda-open-link (&optional arg)
6947 "Follow the link in the current line, if any.
6948 This looks for a link in the displayed line in the agenda. It also looks
6949 at the text of the entry itself."
6950 (interactive "P")
6951 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6952 (org-get-at-bol 'org-marker)))
6953 (buffer (and marker (marker-buffer marker)))
6954 (prefix (buffer-substring
6955 (point-at-bol) (point-at-eol))))
6956 (cond
6957 (buffer
6958 (with-current-buffer buffer
6959 (save-excursion
6960 (save-restriction
6961 (widen)
6962 (goto-char marker)
6963 (org-offer-links-in-entry arg prefix)))))
6964 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6965 (save-excursion
6966 (beginning-of-line 1)
6967 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6968 (org-open-link-from-string (match-string 1)))
6969 (t (error "No link to open here")))))
6971 (defun org-agenda-copy-local-variable (var)
6972 "Get a variable from a referenced buffer and install it here."
6973 (let ((m (org-get-at-bol 'org-marker)))
6974 (when (and m (buffer-live-p (marker-buffer m)))
6975 (org-set-local var (with-current-buffer (marker-buffer m)
6976 (symbol-value var))))))
6978 (defun org-agenda-switch-to (&optional delete-other-windows)
6979 "Go to the Org-mode file which contains the item at point."
6980 (interactive)
6981 (if (and org-return-follows-link
6982 (not (org-get-at-bol 'org-marker))
6983 (org-in-regexp org-bracket-link-regexp))
6984 (org-open-link-from-string (match-string 0))
6985 (let* ((marker (or (org-get-at-bol 'org-marker)
6986 (org-agenda-error)))
6987 (buffer (marker-buffer marker))
6988 (pos (marker-position marker)))
6989 (org-pop-to-buffer-same-window buffer)
6990 (and delete-other-windows (delete-other-windows))
6991 (widen)
6992 (goto-char pos)
6993 (when (org-mode-p)
6994 (org-show-context 'agenda)
6995 (save-excursion
6996 (and (outline-next-heading)
6997 (org-flag-heading nil))) ; show the next heading
6998 (when (outline-invisible-p)
6999 (show-entry)))))) ; display invisible text
7001 (defun org-agenda-goto-mouse (ev)
7002 "Go to the Org-mode file which contains the item at the mouse click."
7003 (interactive "e")
7004 (mouse-set-point ev)
7005 (org-agenda-goto))
7007 (defun org-agenda-show (&optional full-entry)
7008 "Display the Org-mode file which contains the item at point.
7009 With prefix argument FULL-ENTRY, make the entire entry visible
7010 if it was hidden in the outline."
7011 (interactive "P")
7012 (let ((win (selected-window)))
7013 (if full-entry
7014 (let ((org-show-entry-below t))
7015 (org-agenda-goto t))
7016 (org-agenda-goto t))
7017 (select-window win)))
7019 (defvar org-agenda-show-window nil)
7020 (defun org-agenda-show-and-scroll-up ()
7021 "Display the Org-mode file which contains the item at point.
7022 When called repeatedly, scroll the window that is displaying the buffer."
7023 (interactive)
7024 (let ((win (selected-window)))
7025 (if (and (window-live-p org-agenda-show-window)
7026 (eq this-command last-command))
7027 (progn
7028 (select-window org-agenda-show-window)
7029 (ignore-errors (scroll-up)))
7030 (org-agenda-goto t)
7031 (show-subtree)
7032 (setq org-agenda-show-window (selected-window)))
7033 (select-window win)))
7035 (defun org-agenda-show-scroll-down ()
7036 "Scroll down the window showing the agenda."
7037 (interactive)
7038 (let ((win (selected-window)))
7039 (when (window-live-p org-agenda-show-window)
7040 (select-window org-agenda-show-window)
7041 (ignore-errors (scroll-down))
7042 (select-window win))))
7044 (defun org-agenda-show-1 (&optional more)
7045 "Display the Org-mode file which contains the item at point.
7046 The prefix arg selects the amount of information to display:
7048 0 hide the subtree
7049 1 just show the entry according to defaults.
7050 2 show the children view
7051 3 show the subtree view
7052 4 show the entire subtree and any LOGBOOK drawers
7053 5 show the entire subtree and any drawers
7054 With prefix argument FULL-ENTRY, make the entire entry visible
7055 if it was hidden in the outline."
7056 (interactive "p")
7057 (let ((win (selected-window)))
7058 (org-agenda-goto t)
7059 (org-recenter-heading 1)
7060 (cond
7061 ((= more 0)
7062 (hide-subtree)
7063 (save-excursion
7064 (org-back-to-heading)
7065 (run-hook-with-args 'org-cycle-hook 'folded))
7066 (message "Remote: FOLDED"))
7067 ((and (org-called-interactively-p 'any) (= more 1))
7068 (message "Remote: show with default settings"))
7069 ((= more 2)
7070 (show-entry)
7071 (show-children)
7072 (save-excursion
7073 (org-back-to-heading)
7074 (run-hook-with-args 'org-cycle-hook 'children))
7075 (message "Remote: CHILDREN"))
7076 ((= more 3)
7077 (show-subtree)
7078 (save-excursion
7079 (org-back-to-heading)
7080 (run-hook-with-args 'org-cycle-hook 'subtree))
7081 (message "Remote: SUBTREE"))
7082 ((= more 4)
7083 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7084 (org-drawer-regexp
7085 (concat "^[ \t]*:\\("
7086 (mapconcat 'regexp-quote org-drawers "\\|")
7087 "\\):[ \t]*$")))
7088 (show-subtree)
7089 (save-excursion
7090 (org-back-to-heading)
7091 (org-cycle-hide-drawers 'subtree)))
7092 (message "Remote: SUBTREE AND LOGBOOK"))
7093 ((> more 4)
7094 (show-subtree)
7095 (message "Remote: SUBTREE AND ALL DRAWERS")))
7096 (select-window win)))
7098 (defun org-recenter-heading (n)
7099 (save-excursion
7100 (org-back-to-heading)
7101 (recenter n)))
7103 (defvar org-agenda-cycle-counter nil)
7104 (defun org-agenda-cycle-show (&optional n)
7105 "Show the current entry in another window, with default settings.
7106 Default settings are taken from `org-show-hierarchy-above' and siblings.
7107 When use repeatedly in immediate succession, the remote entry will cycle
7108 through visibility
7110 children -> subtree -> folded
7112 When called with a numeric prefix arg, that arg will be passed through to
7113 `org-agenda-show-1'. For the interpretation of that argument, see the
7114 docstring of `org-agenda-show-1'."
7115 (interactive "P")
7116 (if (integerp n)
7117 (setq org-agenda-cycle-counter n)
7118 (if (not (eq last-command this-command))
7119 (setq org-agenda-cycle-counter 1)
7120 (if (equal org-agenda-cycle-counter 0)
7121 (setq org-agenda-cycle-counter 2)
7122 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7123 (if (> org-agenda-cycle-counter 3)
7124 (setq org-agenda-cycle-counter 0)))))
7125 (org-agenda-show-1 org-agenda-cycle-counter))
7127 (defun org-agenda-recenter (arg)
7128 "Display the Org-mode file which contains the item at point and recenter."
7129 (interactive "P")
7130 (let ((win (selected-window)))
7131 (org-agenda-goto t)
7132 (recenter arg)
7133 (select-window win)))
7135 (defun org-agenda-show-mouse (ev)
7136 "Display the Org-mode file which contains the item at the mouse click."
7137 (interactive "e")
7138 (mouse-set-point ev)
7139 (org-agenda-show))
7141 (defun org-agenda-check-no-diary ()
7142 "Check if the entry is a diary link and abort if yes."
7143 (if (org-get-at-bol 'org-agenda-diary-link)
7144 (org-agenda-error)))
7146 (defun org-agenda-error ()
7147 (error "Command not allowed in this line"))
7149 (defun org-agenda-tree-to-indirect-buffer ()
7150 "Show the subtree corresponding to the current entry in an indirect buffer.
7151 This calls the command `org-tree-to-indirect-buffer' from the original
7152 Org-mode buffer.
7153 With numerical prefix arg ARG, go up to this level and then take that tree.
7154 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7155 use the dedicated frame)."
7156 (interactive)
7157 (org-agenda-check-no-diary)
7158 (let* ((marker (or (org-get-at-bol 'org-marker)
7159 (org-agenda-error)))
7160 (buffer (marker-buffer marker))
7161 (pos (marker-position marker)))
7162 (with-current-buffer buffer
7163 (save-excursion
7164 (goto-char pos)
7165 (call-interactively 'org-tree-to-indirect-buffer)))))
7167 (defvar org-last-heading-marker (make-marker)
7168 "Marker pointing to the headline that last changed its TODO state
7169 by a remote command from the agenda.")
7171 (defun org-agenda-todo-nextset ()
7172 "Switch TODO entry to next sequence."
7173 (interactive)
7174 (org-agenda-todo 'nextset))
7176 (defun org-agenda-todo-previousset ()
7177 "Switch TODO entry to previous sequence."
7178 (interactive)
7179 (org-agenda-todo 'previousset))
7181 (defun org-agenda-todo (&optional arg)
7182 "Cycle TODO state of line at point, also in Org-mode file.
7183 This changes the line at point, all other lines in the agenda referring to
7184 the same tree node, and the headline of the tree node in the Org-mode file."
7185 (interactive "P")
7186 (org-agenda-check-no-diary)
7187 (let* ((col (current-column))
7188 (marker (or (org-get-at-bol 'org-marker)
7189 (org-agenda-error)))
7190 (buffer (marker-buffer marker))
7191 (pos (marker-position marker))
7192 (hdmarker (org-get-at-bol 'org-hd-marker))
7193 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7194 (inhibit-read-only t)
7195 org-agenda-headline-snapshot-before-repeat newhead just-one)
7196 (org-with-remote-undo buffer
7197 (with-current-buffer buffer
7198 (widen)
7199 (goto-char pos)
7200 (org-show-context 'agenda)
7201 (save-excursion
7202 (and (outline-next-heading)
7203 (org-flag-heading nil))) ; show the next heading
7204 (let ((current-prefix-arg arg))
7205 (call-interactively 'org-todo))
7206 (and (bolp) (forward-char 1))
7207 (setq newhead (org-get-heading))
7208 (when (and (org-bound-and-true-p
7209 org-agenda-headline-snapshot-before-repeat)
7210 (not (equal org-agenda-headline-snapshot-before-repeat
7211 newhead))
7212 todayp)
7213 (setq newhead org-agenda-headline-snapshot-before-repeat
7214 just-one t))
7215 (save-excursion
7216 (org-back-to-heading)
7217 (move-marker org-last-heading-marker (point))))
7218 (beginning-of-line 1)
7219 (save-excursion
7220 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7221 (org-move-to-column col))))
7223 (defun org-agenda-add-note (&optional arg)
7224 "Add a time-stamped note to the entry at point."
7225 (interactive "P")
7226 (org-agenda-check-no-diary)
7227 (let* ((marker (or (org-get-at-bol 'org-marker)
7228 (org-agenda-error)))
7229 (buffer (marker-buffer marker))
7230 (pos (marker-position marker))
7231 (hdmarker (org-get-at-bol 'org-hd-marker))
7232 (inhibit-read-only t))
7233 (with-current-buffer buffer
7234 (widen)
7235 (goto-char pos)
7236 (org-show-context 'agenda)
7237 (save-excursion
7238 (and (outline-next-heading)
7239 (org-flag-heading nil))) ; show the next heading
7240 (org-add-note))))
7242 (defun org-agenda-change-all-lines (newhead hdmarker
7243 &optional fixface just-this)
7244 "Change all lines in the agenda buffer which match HDMARKER.
7245 The new content of the line will be NEWHEAD (as modified by
7246 `org-format-agenda-item'). HDMARKER is checked with
7247 `equal' against all `org-hd-marker' text properties in the file.
7248 If FIXFACE is non-nil, the face of each item is modified according to
7249 the new TODO state.
7250 If JUST-THIS is non-nil, change just the current line, not all.
7251 If FORCE-TAGS is non nil, the car of it returns the new tags."
7252 (let* ((inhibit-read-only t)
7253 (line (org-current-line))
7254 (thetags (with-current-buffer (marker-buffer hdmarker)
7255 (save-excursion (save-restriction (widen)
7256 (goto-char hdmarker)
7257 (org-get-tags-at)))))
7258 props m pl undone-face done-face finish new dotime cat tags)
7259 (save-excursion
7260 (goto-char (point-max))
7261 (beginning-of-line 1)
7262 (while (not finish)
7263 (setq finish (bobp))
7264 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7265 (or (not just-this) (= (org-current-line) line))
7266 (equal m hdmarker))
7267 (setq props (text-properties-at (point))
7268 dotime (org-get-at-bol 'dotime)
7269 cat (org-get-at-bol 'org-category)
7270 tags thetags
7272 (let ((org-prefix-format-compiled
7273 (or (get-text-property (point) 'format)
7274 org-prefix-format-compiled)))
7275 (with-current-buffer (marker-buffer hdmarker)
7276 (save-excursion
7277 (save-restriction
7278 (widen)
7279 (org-format-agenda-item (org-get-at-bol 'extra)
7280 newhead cat tags dotime)))))
7281 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7282 undone-face (org-get-at-bol 'undone-face)
7283 done-face (org-get-at-bol 'done-face))
7284 (beginning-of-line 1)
7285 (cond
7286 ((equal new "")
7287 (and (looking-at ".*\n?") (replace-match "")))
7288 ((looking-at ".*")
7289 (replace-match new t t)
7290 (beginning-of-line 1)
7291 (add-text-properties (point-at-bol) (point-at-eol) props)
7292 (when fixface
7293 (add-text-properties
7294 (point-at-bol) (point-at-eol)
7295 (list 'face
7296 (if org-last-todo-state-is-todo
7297 undone-face done-face))))
7298 (org-agenda-highlight-todo 'line)
7299 (beginning-of-line 1))
7300 (t (error "Line update did not work"))))
7301 (beginning-of-line 0)))
7302 (org-finalize-agenda)))
7304 (defun org-agenda-align-tags (&optional line)
7305 "Align all tags in agenda items to `org-agenda-tags-column'."
7306 (let ((inhibit-read-only t) l c)
7307 (save-excursion
7308 (goto-char (if line (point-at-bol) (point-min)))
7309 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7310 (if line (point-at-eol) nil) t)
7311 (add-text-properties
7312 (match-beginning 2) (match-end 2)
7313 (list 'face (delq nil (let ((prop (get-text-property
7314 (match-beginning 2) 'face)))
7315 (or (listp prop) (setq prop (list prop)))
7316 (if (memq 'org-tag prop)
7317 prop
7318 (cons 'org-tag prop))))))
7319 (setq l (- (match-end 2) (match-beginning 2))
7320 c (if (< org-agenda-tags-column 0)
7321 (- (abs org-agenda-tags-column) l)
7322 org-agenda-tags-column))
7323 (delete-region (match-beginning 1) (match-end 1))
7324 (goto-char (match-beginning 1))
7325 (insert (org-add-props
7326 (make-string (max 1 (- c (current-column))) ?\ )
7327 (plist-put (copy-sequence (text-properties-at (point)))
7328 'face nil))))
7329 (goto-char (point-min))
7330 (org-font-lock-add-tag-faces (point-max)))))
7332 (defun org-agenda-priority-up ()
7333 "Increase the priority of line at point, also in Org-mode file."
7334 (interactive)
7335 (org-agenda-priority 'up))
7337 (defun org-agenda-priority-down ()
7338 "Decrease the priority of line at point, also in Org-mode file."
7339 (interactive)
7340 (org-agenda-priority 'down))
7342 (defun org-agenda-priority (&optional force-direction)
7343 "Set the priority of line at point, also in Org-mode file.
7344 This changes the line at point, all other lines in the agenda referring to
7345 the same tree node, and the headline of the tree node in the Org-mode file."
7346 (interactive)
7347 (unless org-enable-priority-commands
7348 (error "Priority commands are disabled"))
7349 (org-agenda-check-no-diary)
7350 (let* ((marker (or (org-get-at-bol 'org-marker)
7351 (org-agenda-error)))
7352 (hdmarker (org-get-at-bol 'org-hd-marker))
7353 (buffer (marker-buffer hdmarker))
7354 (pos (marker-position hdmarker))
7355 (inhibit-read-only t)
7356 newhead)
7357 (org-with-remote-undo buffer
7358 (with-current-buffer buffer
7359 (widen)
7360 (goto-char pos)
7361 (org-show-context 'agenda)
7362 (save-excursion
7363 (and (outline-next-heading)
7364 (org-flag-heading nil))) ; show the next heading
7365 (funcall 'org-priority force-direction)
7366 (end-of-line 1)
7367 (setq newhead (org-get-heading)))
7368 (org-agenda-change-all-lines newhead hdmarker)
7369 (beginning-of-line 1))))
7371 ;; FIXME: should fix the tags property of the agenda line.
7372 (defun org-agenda-set-tags (&optional tag onoff)
7373 "Set tags for the current headline."
7374 (interactive)
7375 (org-agenda-check-no-diary)
7376 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7377 (call-interactively 'org-change-tag-in-region)
7378 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7379 (org-agenda-error)))
7380 (buffer (marker-buffer hdmarker))
7381 (pos (marker-position hdmarker))
7382 (inhibit-read-only t)
7383 newhead)
7384 (org-with-remote-undo buffer
7385 (with-current-buffer buffer
7386 (widen)
7387 (goto-char pos)
7388 (save-excursion
7389 (org-show-context 'agenda))
7390 (save-excursion
7391 (and (outline-next-heading)
7392 (org-flag-heading nil))) ; show the next heading
7393 (goto-char pos)
7394 (if tag
7395 (org-toggle-tag tag onoff)
7396 (call-interactively 'org-set-tags))
7397 (end-of-line 1)
7398 (setq newhead (org-get-heading)))
7399 (org-agenda-change-all-lines newhead hdmarker)
7400 (beginning-of-line 1)))))
7402 (defun org-agenda-set-property ()
7403 "Set a property for the current headline."
7404 (interactive)
7405 (org-agenda-check-no-diary)
7406 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7407 (org-agenda-error)))
7408 (buffer (marker-buffer hdmarker))
7409 (pos (marker-position hdmarker))
7410 (inhibit-read-only t)
7411 newhead)
7412 (org-with-remote-undo buffer
7413 (with-current-buffer buffer
7414 (widen)
7415 (goto-char pos)
7416 (save-excursion
7417 (org-show-context 'agenda))
7418 (save-excursion
7419 (and (outline-next-heading)
7420 (org-flag-heading nil))) ; show the next heading
7421 (goto-char pos)
7422 (call-interactively 'org-set-property)))))
7424 (defun org-agenda-set-effort ()
7425 "Set the effort property for the current headline."
7426 (interactive)
7427 (org-agenda-check-no-diary)
7428 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7429 (org-agenda-error)))
7430 (buffer (marker-buffer hdmarker))
7431 (pos (marker-position hdmarker))
7432 (inhibit-read-only t)
7433 newhead)
7434 (org-with-remote-undo buffer
7435 (with-current-buffer buffer
7436 (widen)
7437 (goto-char pos)
7438 (save-excursion
7439 (org-show-context 'agenda))
7440 (save-excursion
7441 (and (outline-next-heading)
7442 (org-flag-heading nil))) ; show the next heading
7443 (goto-char pos)
7444 (call-interactively 'org-set-effort)
7445 (end-of-line 1)
7446 (setq newhead (org-get-heading)))
7447 (org-agenda-change-all-lines newhead hdmarker))))
7449 (defun org-agenda-toggle-archive-tag ()
7450 "Toggle the archive tag for the current entry."
7451 (interactive)
7452 (org-agenda-check-no-diary)
7453 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7454 (org-agenda-error)))
7455 (buffer (marker-buffer hdmarker))
7456 (pos (marker-position hdmarker))
7457 (inhibit-read-only t)
7458 newhead)
7459 (org-with-remote-undo buffer
7460 (with-current-buffer buffer
7461 (widen)
7462 (goto-char pos)
7463 (org-show-context 'agenda)
7464 (save-excursion
7465 (and (outline-next-heading)
7466 (org-flag-heading nil))) ; show the next heading
7467 (call-interactively 'org-toggle-archive-tag)
7468 (end-of-line 1)
7469 (setq newhead (org-get-heading)))
7470 (org-agenda-change-all-lines newhead hdmarker)
7471 (beginning-of-line 1))))
7473 (defun org-agenda-do-date-later (arg)
7474 (interactive "P")
7475 (cond
7476 ((or (equal arg '(16))
7477 (memq last-command
7478 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7479 (setq this-command 'org-agenda-date-later-minutes)
7480 (org-agenda-date-later-minutes 1))
7481 ((or (equal arg '(4))
7482 (memq last-command
7483 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7484 (setq this-command 'org-agenda-date-later-hours)
7485 (org-agenda-date-later-hours 1))
7487 (org-agenda-date-later (prefix-numeric-value arg)))))
7489 (defun org-agenda-do-date-earlier (arg)
7490 (interactive "P")
7491 (cond
7492 ((or (equal arg '(16))
7493 (memq last-command
7494 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7495 (setq this-command 'org-agenda-date-earlier-minutes)
7496 (org-agenda-date-earlier-minutes 1))
7497 ((or (equal arg '(4))
7498 (memq last-command
7499 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7500 (setq this-command 'org-agenda-date-earlier-hours)
7501 (org-agenda-date-earlier-hours 1))
7503 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7505 (defun org-agenda-date-later (arg &optional what)
7506 "Change the date of this item to ARG day(s) later."
7507 (interactive "p")
7508 (org-agenda-check-type t 'agenda 'timeline)
7509 (org-agenda-check-no-diary)
7510 (let* ((marker (or (org-get-at-bol 'org-marker)
7511 (org-agenda-error)))
7512 (buffer (marker-buffer marker))
7513 (pos (marker-position marker)))
7514 (org-with-remote-undo buffer
7515 (with-current-buffer buffer
7516 (widen)
7517 (goto-char pos)
7518 (if (not (org-at-timestamp-p))
7519 (error "Cannot find time stamp"))
7520 (org-timestamp-change arg (or what 'day)))
7521 (org-agenda-show-new-time marker org-last-changed-timestamp))
7522 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7524 (defun org-agenda-date-earlier (arg &optional what)
7525 "Change the date of this item to ARG day(s) earlier."
7526 (interactive "p")
7527 (org-agenda-date-later (- arg) what))
7529 (defun org-agenda-date-later-minutes (arg)
7530 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7531 (interactive "p")
7532 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7533 (org-agenda-date-later arg 'minute))
7535 (defun org-agenda-date-earlier-minutes (arg)
7536 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7537 (interactive "p")
7538 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7539 (org-agenda-date-earlier arg 'minute))
7541 (defun org-agenda-date-later-hours (arg)
7542 "Change the time of this item, in hour steps."
7543 (interactive "p")
7544 (org-agenda-date-later arg 'hour))
7546 (defun org-agenda-date-earlier-hours (arg)
7547 "Change the time of this item, in hour steps."
7548 (interactive "p")
7549 (org-agenda-date-earlier arg 'hour))
7551 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7552 "Show new date stamp via text properties."
7553 ;; We use text properties to make this undoable
7554 (let ((inhibit-read-only t)
7555 (buffer-invisibility-spec))
7556 (setq stamp (concat " " prefix " => " stamp))
7557 (save-excursion
7558 (goto-char (point-max))
7559 (while (not (bobp))
7560 (when (equal marker (org-get-at-bol 'org-marker))
7561 (org-move-to-column (- (window-width) (length stamp)) t)
7562 (org-agenda-fix-tags-filter-overlays-at (point))
7563 (if (featurep 'xemacs)
7564 ;; Use `duplicable' property to trigger undo recording
7565 (let ((ex (make-extent nil nil))
7566 (gl (make-glyph stamp)))
7567 (set-glyph-face gl 'secondary-selection)
7568 (set-extent-properties
7569 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7570 (insert-extent ex (1- (point)) (point-at-eol)))
7571 (add-text-properties
7572 (1- (point)) (point-at-eol)
7573 (list 'display (org-add-props stamp nil
7574 'face 'secondary-selection))))
7575 (beginning-of-line 1))
7576 (beginning-of-line 0)))))
7578 (defun org-agenda-date-prompt (arg)
7579 "Change the date of this item. Date is prompted for, with default today.
7580 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7581 be used to request time specification in the time stamp."
7582 (interactive "P")
7583 (org-agenda-check-type t 'agenda 'timeline)
7584 (org-agenda-check-no-diary)
7585 (let* ((marker (or (org-get-at-bol 'org-marker)
7586 (org-agenda-error)))
7587 (buffer (marker-buffer marker))
7588 (pos (marker-position marker)))
7589 (org-with-remote-undo buffer
7590 (with-current-buffer buffer
7591 (widen)
7592 (goto-char pos)
7593 (if (not (org-at-timestamp-p t))
7594 (error "Cannot find time stamp"))
7595 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7596 (org-agenda-show-new-time marker org-last-changed-timestamp))
7597 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7599 (defun org-agenda-schedule (arg &optional time)
7600 "Schedule the item at point.
7601 ARG is passed through to `org-schedule'."
7602 (interactive "P")
7603 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7604 (org-agenda-check-no-diary)
7605 (let* ((marker (or (org-get-at-bol 'org-marker)
7606 (org-agenda-error)))
7607 (type (marker-insertion-type marker))
7608 (buffer (marker-buffer marker))
7609 (pos (marker-position marker))
7610 (org-insert-labeled-timestamps-at-point nil)
7612 (set-marker-insertion-type marker t)
7613 (org-with-remote-undo buffer
7614 (with-current-buffer buffer
7615 (widen)
7616 (goto-char pos)
7617 (setq ts (org-schedule arg time)))
7618 (org-agenda-show-new-time marker ts "S"))
7619 (message "Item scheduled for %s" ts)))
7621 (defun org-agenda-deadline (arg &optional time)
7622 "Schedule the item at point.
7623 ARG is passed through to `org-deadline'."
7624 (interactive "P")
7625 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7626 (org-agenda-check-no-diary)
7627 (let* ((marker (or (org-get-at-bol 'org-marker)
7628 (org-agenda-error)))
7629 (buffer (marker-buffer marker))
7630 (pos (marker-position marker))
7631 (org-insert-labeled-timestamps-at-point nil)
7633 (org-with-remote-undo buffer
7634 (with-current-buffer buffer
7635 (widen)
7636 (goto-char pos)
7637 (setq ts (org-deadline arg time)))
7638 (org-agenda-show-new-time marker ts "D"))
7639 (message "Deadline for this item set to %s" ts)))
7641 (defun org-agenda-action ()
7642 "Select entry for agenda action, or execute an agenda action.
7643 This command prompts for another letter. Valid inputs are:
7645 m Mark the entry at point for an agenda action
7646 s Schedule the marked entry to the date at the cursor
7647 d Set the deadline of the marked entry to the date at the cursor
7648 r Call `org-remember' with cursor date as the default date
7649 c Call `org-capture' with cursor date as the default date
7650 SPC Show marked entry in other window
7651 TAB Visit marked entry in other window
7653 The cursor may be at a date in the calendar, or in the Org agenda."
7654 (interactive)
7655 (let (ans)
7656 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7657 (setq ans (read-char-exclusive))
7658 (cond
7659 ((equal ans ?m)
7660 ;; Mark this entry
7661 (if (eq major-mode 'org-agenda-mode)
7662 (let ((m (or (org-get-at-bol 'org-hd-marker)
7663 (org-get-at-bol 'org-marker))))
7664 (if m
7665 (progn
7666 (move-marker org-agenda-action-marker
7667 (marker-position m) (marker-buffer m))
7668 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7669 (error "Don't know which entry to mark")))
7670 (error "This command works only in the agenda")))
7671 ((equal ans ?s)
7672 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7673 ((equal ans ?d)
7674 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7675 ((equal ans ?r)
7676 (org-agenda-do-action '(org-remember) t))
7677 ((equal ans ?c)
7678 (org-agenda-do-action '(org-capture) t))
7679 ((equal ans ?\ )
7680 (let ((cw (selected-window)))
7681 (org-switch-to-buffer-other-window
7682 (marker-buffer org-agenda-action-marker))
7683 (goto-char org-agenda-action-marker)
7684 (org-show-context 'agenda)
7685 (select-window cw)))
7686 ((equal ans ?\C-i)
7687 (org-switch-to-buffer-other-window
7688 (marker-buffer org-agenda-action-marker))
7689 (goto-char org-agenda-action-marker)
7690 (org-show-context 'agenda))
7691 (t (error "Invalid agenda action %c" ans)))))
7693 (defun org-agenda-do-action (form &optional current-buffer)
7694 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7695 (let ((org-overriding-default-time (org-get-cursor-date)))
7696 (if current-buffer
7697 (eval form)
7698 (if (not (marker-buffer org-agenda-action-marker))
7699 (error "No entry has been selected for agenda action")
7700 (with-current-buffer (marker-buffer org-agenda-action-marker)
7701 (save-excursion
7702 (save-restriction
7703 (widen)
7704 (goto-char org-agenda-action-marker)
7705 (eval form))))))))
7707 (defun org-agenda-clock-in (&optional arg)
7708 "Start the clock on the currently selected item."
7709 (interactive "P")
7710 (org-agenda-check-no-diary)
7711 (if (equal arg '(4))
7712 (org-clock-in arg)
7713 (let* ((marker (or (org-get-at-bol 'org-marker)
7714 (org-agenda-error)))
7715 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7716 marker))
7717 (pos (marker-position marker))
7718 newhead)
7719 (org-with-remote-undo (marker-buffer marker)
7720 (with-current-buffer (marker-buffer marker)
7721 (widen)
7722 (goto-char pos)
7723 (org-show-context 'agenda)
7724 (org-show-entry)
7725 (org-cycle-hide-drawers 'children)
7726 (org-clock-in arg)
7727 (setq newhead (org-get-heading)))
7728 (org-agenda-change-all-lines newhead hdmarker)))))
7730 (defun org-agenda-clock-out ()
7731 "Stop the currently running clock."
7732 (interactive)
7733 (unless (marker-buffer org-clock-marker)
7734 (error "No running clock"))
7735 (let ((marker (make-marker)) newhead)
7736 (org-with-remote-undo (marker-buffer org-clock-marker)
7737 (with-current-buffer (marker-buffer org-clock-marker)
7738 (save-excursion
7739 (save-restriction
7740 (widen)
7741 (goto-char org-clock-marker)
7742 (org-back-to-heading t)
7743 (move-marker marker (point))
7744 (org-clock-out)
7745 (setq newhead (org-get-heading))))))
7746 (org-agenda-change-all-lines newhead marker)
7747 (move-marker marker nil)))
7749 (defun org-agenda-clock-cancel (&optional arg)
7750 "Cancel the currently running clock."
7751 (interactive "P")
7752 (unless (marker-buffer org-clock-marker)
7753 (error "No running clock"))
7754 (org-with-remote-undo (marker-buffer org-clock-marker)
7755 (org-clock-cancel)))
7757 (defun org-agenda-clock-goto ()
7758 "Jump to the currently clocked in task within the agenda.
7759 If the currently clocked in task is not listed in the agenda
7760 buffer, display it in another window."
7761 (interactive)
7762 (let (pos)
7763 (mapc (lambda (o)
7764 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7765 (setq pos (overlay-start o))))
7766 (overlays-in (point-min) (point-max)))
7767 (cond (pos (goto-char pos))
7768 ;; If the currently clocked entry is not in the agenda
7769 ;; buffer, we visit it in another window:
7770 (org-clock-current-task
7771 (org-switch-to-buffer-other-window (org-clock-goto)))
7772 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7774 (defun org-agenda-diary-entry-in-org-file ()
7775 "Make a diary entry in the file `org-agenda-diary-file'."
7776 (let (d1 d2 char (text "") dp1 dp2)
7777 (if (equal (buffer-name) "*Calendar*")
7778 (setq d1 (calendar-cursor-to-date t)
7779 d2 (car calendar-mark-ring))
7780 (setq dp1 (get-text-property (point-at-bol) 'day))
7781 (unless dp1 (error "No date defined in current line"))
7782 (setq d1 (calendar-gregorian-from-absolute dp1)
7783 d2 (and (ignore-errors (mark))
7784 (save-excursion
7785 (goto-char (mark))
7786 (setq dp2 (get-text-property (point-at-bol) 'day)))
7787 (calendar-gregorian-from-absolute dp2))))
7788 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7789 (setq char (read-char-exclusive))
7790 (cond
7791 ((equal char ?d)
7792 (setq text (read-string "Day entry: "))
7793 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7794 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7795 ((equal char ?a)
7796 (setq d1 (list (car d1) (nth 1 d1)
7797 (read-number (format "Reference year [%d]: " (nth 2 d1))
7798 (nth 2 d1))))
7799 (setq text (read-string "Anniversary (use %d to show years): "))
7800 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7801 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7802 ((equal char ?b)
7803 (setq text (read-string "Block entry: "))
7804 (unless (and d1 d2 (not (equal d1 d2)))
7805 (error "No block of days selected"))
7806 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7807 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7808 ((equal char ?j)
7809 (org-switch-to-buffer-other-window
7810 (find-file-noselect org-agenda-diary-file))
7811 (require 'org-datetree)
7812 (org-datetree-find-date-create d1)
7813 (org-reveal t))
7814 (t (error "Invalid selection character `%c'" char)))))
7816 (defcustom org-agenda-insert-diary-strategy 'date-tree
7817 "Where in `org-agenda-diary-file' should new entries be added?
7818 Valid values:
7820 date-tree in the date tree, as child of the date
7821 top-level as top-level entries at the end of the file."
7822 :group 'org-agenda
7823 :type '(choice
7824 (const :tag "in a date tree" date-tree)
7825 (const :tag "as top level at end of file" top-level)))
7827 (defcustom org-agenda-insert-diary-extract-time nil
7828 "Non-nil means extract any time specification from the diary entry."
7829 :group 'org-agenda
7830 :type 'boolean)
7832 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7833 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7834 If TEXT is not empty, it will become the headline of the new entry, and
7835 the resulting entry will not be shown. When TEXT is empty, switch to
7836 `org-agenda-diary-file' and let the user finish the entry there."
7837 (let ((cw (current-window-configuration)))
7838 (org-switch-to-buffer-other-window
7839 (find-file-noselect org-agenda-diary-file))
7840 (widen)
7841 (goto-char (point-min))
7842 (cond
7843 ((eq type 'anniversary)
7844 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7845 (progn
7846 (or (org-on-heading-p t)
7847 (progn
7848 (outline-next-heading)
7849 (insert "* Anniversaries\n\n")
7850 (beginning-of-line -1)))))
7851 (outline-next-heading)
7852 (org-back-over-empty-lines)
7853 (backward-char 1)
7854 (insert "\n")
7855 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7856 (nth 2 d1) (car d1) (nth 1 d1) text)))
7857 ((eq type 'day)
7858 (let ((org-prefix-has-time t)
7859 (org-agenda-time-leading-zero t)
7860 fmt time time2)
7861 (if org-agenda-insert-diary-extract-time
7862 ;; Use org-format-agenda-item to parse text for a time-range and
7863 ;; remove it. FIXME: This is a hack, we should refactor
7864 ;; that function to make time extraction available separately
7865 (setq fmt (org-format-agenda-item nil text nil nil t)
7866 time (get-text-property 0 'time fmt)
7867 time2 (if (> (length time) 0)
7868 ;; split-string removes trailing ...... if
7869 ;; no end time given. First space
7870 ;; separates time from date.
7871 (concat " " (car (split-string time "\\.")))
7872 nil)
7873 text (get-text-property 0 'txt fmt)))
7874 (if (eq org-agenda-insert-diary-strategy 'top-level)
7875 (org-agenda-insert-diary-as-top-level text)
7876 (require 'org-datetree)
7877 (org-datetree-find-date-create d1)
7878 (org-agenda-insert-diary-make-new-entry text))
7879 (org-insert-time-stamp (org-time-from-absolute
7880 (calendar-absolute-from-gregorian d1))
7881 nil nil nil nil time2))
7882 (end-of-line 0))
7883 ((eq type 'block)
7884 (if (> (calendar-absolute-from-gregorian d1)
7885 (calendar-absolute-from-gregorian d2))
7886 (setq d1 (prog1 d2 (setq d2 d1))))
7887 (if (eq org-agenda-insert-diary-strategy 'top-level)
7888 (org-agenda-insert-diary-as-top-level text)
7889 (require 'org-datetree)
7890 (org-datetree-find-date-create d1)
7891 (org-agenda-insert-diary-make-new-entry text))
7892 (org-insert-time-stamp (org-time-from-absolute
7893 (calendar-absolute-from-gregorian d1)))
7894 (insert "--")
7895 (org-insert-time-stamp (org-time-from-absolute
7896 (calendar-absolute-from-gregorian d2)))
7897 (end-of-line 0)))
7898 (if (string-match "\\S-" text)
7899 (progn
7900 (set-window-configuration cw)
7901 (message "%s entry added to %s"
7902 (capitalize (symbol-name type))
7903 (abbreviate-file-name org-agenda-diary-file)))
7904 (org-reveal t)
7905 (message "Please finish entry here"))))
7907 (defun org-agenda-insert-diary-as-top-level (text)
7908 "Make new entry as a top-level entry at the end of the file.
7909 Add TEXT as headline, and position the cursor in the second line so that
7910 a timestamp can be added there."
7911 (widen)
7912 (goto-char (point-max))
7913 (or (bolp) (insert "\n"))
7914 (insert "* " text "\n")
7915 (if org-adapt-indentation (org-indent-to-column 2)))
7917 (defun org-agenda-insert-diary-make-new-entry (text)
7918 "Make new entry as last child of current entry.
7919 Add TEXT as headline, and position the cursor in the second line so that
7920 a timestamp can be added there."
7921 (let ((org-show-following-heading t)
7922 (org-show-siblings t)
7923 (org-show-hierarchy-above t)
7924 (org-show-entry-below t)
7925 col)
7926 (outline-next-heading)
7927 (org-back-over-empty-lines)
7928 (or (looking-at "[ \t]*$")
7929 (progn (insert "\n") (backward-char 1)))
7930 (org-insert-heading nil t)
7931 (org-do-demote)
7932 (setq col (current-column))
7933 (insert text "\n")
7934 (if org-adapt-indentation (org-indent-to-column col))
7935 (let ((org-show-following-heading t)
7936 (org-show-siblings t)
7937 (org-show-hierarchy-above t)
7938 (org-show-entry-below t))
7939 (org-show-context))))
7941 (defun org-agenda-diary-entry ()
7942 "Make a diary entry, like the `i' command from the calendar.
7943 All the standard commands work: block, weekly etc.
7944 When `org-agenda-diary-file' points to a file,
7945 `org-agenda-diary-entry-in-org-file' is called instead to create
7946 entries in that Org-mode file."
7947 (interactive)
7948 (org-agenda-check-type t 'agenda 'timeline)
7949 (if (not (eq org-agenda-diary-file 'diary-file))
7950 (org-agenda-diary-entry-in-org-file)
7951 (require 'diary-lib)
7952 (let* ((char (progn
7953 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7954 (read-char-exclusive)))
7955 (cmd (cdr (assoc char
7956 '((?d . insert-diary-entry)
7957 (?w . insert-weekly-diary-entry)
7958 (?m . insert-monthly-diary-entry)
7959 (?y . insert-yearly-diary-entry)
7960 (?a . insert-anniversary-diary-entry)
7961 (?b . insert-block-diary-entry)
7962 (?c . insert-cyclic-diary-entry)))))
7963 (oldf (symbol-function 'calendar-cursor-to-date))
7964 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7965 (point (point))
7966 (mark (or (mark t) (point))))
7967 (unless cmd
7968 (error "No command associated with <%c>" char))
7969 (unless (and (get-text-property point 'day)
7970 (or (not (equal ?b char))
7971 (get-text-property mark 'day)))
7972 (error "Don't know which date to use for diary entry"))
7973 ;; We implement this by hacking the `calendar-cursor-to-date' function
7974 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7975 (let ((calendar-mark-ring
7976 (list (calendar-gregorian-from-absolute
7977 (or (get-text-property mark 'day)
7978 (get-text-property point 'day))))))
7979 (unwind-protect
7980 (progn
7981 (fset 'calendar-cursor-to-date
7982 (lambda (&optional error dummy)
7983 (calendar-gregorian-from-absolute
7984 (get-text-property point 'day))))
7985 (call-interactively cmd))
7986 (fset 'calendar-cursor-to-date oldf))))))
7988 (defun org-agenda-execute-calendar-command (cmd)
7989 "Execute a calendar command from the agenda, with the date associated to
7990 the cursor position."
7991 (org-agenda-check-type t 'agenda 'timeline)
7992 (require 'diary-lib)
7993 (unless (get-text-property (point) 'day)
7994 (error "Don't know which date to use for calendar command"))
7995 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7996 (point (point))
7997 (date (calendar-gregorian-from-absolute
7998 (get-text-property point 'day)))
7999 ;; the following 2 vars are needed in the calendar
8000 (displayed-month (car date))
8001 (displayed-year (nth 2 date)))
8002 (unwind-protect
8003 (progn
8004 (fset 'calendar-cursor-to-date
8005 (lambda (&optional error dummy)
8006 (calendar-gregorian-from-absolute
8007 (get-text-property point 'day))))
8008 (call-interactively cmd))
8009 (fset 'calendar-cursor-to-date oldf))))
8011 (defun org-agenda-phases-of-moon ()
8012 "Display the phases of the moon for the 3 months around the cursor date."
8013 (interactive)
8014 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8016 (defun org-agenda-holidays ()
8017 "Display the holidays for the 3 months around the cursor date."
8018 (interactive)
8019 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8021 (defvar calendar-longitude)
8022 (defvar calendar-latitude)
8023 (defvar calendar-location-name)
8025 (defun org-agenda-sunrise-sunset (arg)
8026 "Display sunrise and sunset for the cursor date.
8027 Latitude and longitude can be specified with the variables
8028 `calendar-latitude' and `calendar-longitude'. When called with prefix
8029 argument, latitude and longitude will be prompted for."
8030 (interactive "P")
8031 (require 'solar)
8032 (let ((calendar-longitude (if arg nil calendar-longitude))
8033 (calendar-latitude (if arg nil calendar-latitude))
8034 (calendar-location-name
8035 (if arg "the given coordinates" calendar-location-name)))
8036 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8038 (defun org-agenda-goto-calendar ()
8039 "Open the Emacs calendar with the date at the cursor."
8040 (interactive)
8041 (org-agenda-check-type t 'agenda 'timeline)
8042 (let* ((day (or (get-text-property (point) 'day)
8043 (error "Don't know which date to open in calendar")))
8044 (date (calendar-gregorian-from-absolute day))
8045 (calendar-move-hook nil)
8046 (calendar-view-holidays-initially-flag nil)
8047 (calendar-view-diary-initially-flag nil))
8048 (calendar)
8049 (calendar-goto-date date)))
8051 ;;;###autoload
8052 (defun org-calendar-goto-agenda ()
8053 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8054 This is a command that has to be installed in `calendar-mode-map'."
8055 (interactive)
8056 (org-agenda-list nil (calendar-absolute-from-gregorian
8057 (calendar-cursor-to-date))
8058 nil))
8060 (defun org-agenda-convert-date ()
8061 (interactive)
8062 (org-agenda-check-type t 'agenda 'timeline)
8063 (let ((day (get-text-property (point) 'day))
8064 date s)
8065 (unless day
8066 (error "Don't know which date to convert"))
8067 (setq date (calendar-gregorian-from-absolute day))
8068 (setq s (concat
8069 "Gregorian: " (calendar-date-string date) "\n"
8070 "ISO: " (calendar-iso-date-string date) "\n"
8071 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8072 "Julian: " (calendar-julian-date-string date) "\n"
8073 "Astron. JD: " (calendar-astro-date-string date)
8074 " (Julian date number at noon UTC)\n"
8075 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8076 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8077 "French: " (calendar-french-date-string date) "\n"
8078 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8079 "Mayan: " (calendar-mayan-date-string date) "\n"
8080 "Coptic: " (calendar-coptic-date-string date) "\n"
8081 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8082 "Persian: " (calendar-persian-date-string date) "\n"
8083 "Chinese: " (calendar-chinese-date-string date) "\n"))
8084 (with-output-to-temp-buffer "*Dates*"
8085 (princ s))
8086 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8088 ;;; Bulk commands
8090 (defvar org-agenda-bulk-marked-entries nil
8091 "List of markers that refer to marked entries in the agenda.")
8093 (defun org-agenda-bulk-marked-p ()
8094 (eq (get-char-property (point-at-bol) 'type)
8095 'org-marked-entry-overlay))
8097 (defun org-agenda-bulk-mark (&optional arg)
8098 "Mark the entry at point for future bulk action."
8099 (interactive "p")
8100 (dotimes (i (max arg 1))
8101 (unless (org-get-at-bol 'org-agenda-diary-link)
8102 (let* ((m (org-get-at-bol 'org-hd-marker))
8104 (unless (org-agenda-bulk-marked-p)
8105 (unless m (error "Nothing to mark at point"))
8106 (push m org-agenda-bulk-marked-entries)
8107 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8108 (org-overlay-display ov "> "
8109 (org-get-todo-face "TODO")
8110 'evaporate)
8111 (overlay-put ov 'type 'org-marked-entry-overlay))
8112 (beginning-of-line 2)
8113 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8114 (beginning-of-line 2))
8115 (message "%d entries marked for bulk action"
8116 (length org-agenda-bulk-marked-entries))))))
8118 (defun org-agenda-bulk-mark-regexp (regexp)
8119 "Mark entries match REGEXP."
8120 (interactive "sMark entries matching regexp: ")
8121 (let (entries-marked)
8122 (save-excursion
8123 (goto-char (point-min))
8124 (goto-char (next-single-property-change (point) 'txt))
8125 (while (re-search-forward regexp nil t)
8126 (when (string-match regexp (get-text-property (point) 'txt))
8127 (setq entries-marked (+ entries-marked 1))
8128 (call-interactively 'org-agenda-bulk-mark))))
8129 (if (not entries-marked)
8130 (message "No entry matching this regexp."))))
8132 (defun org-agenda-bulk-unmark ()
8133 "Unmark the entry at point for future bulk action."
8134 (interactive)
8135 (when (org-agenda-bulk-marked-p)
8136 (org-agenda-bulk-remove-overlays
8137 (point-at-bol) (+ 2 (point-at-bol)))
8138 (setq org-agenda-bulk-marked-entries
8139 (delete (org-get-at-bol 'org-hd-marker)
8140 org-agenda-bulk-marked-entries)))
8141 (beginning-of-line 2)
8142 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8143 (beginning-of-line 2))
8144 (message "%d entries marked for bulk action"
8145 (length org-agenda-bulk-marked-entries)))
8147 (defun org-agenda-bulk-toggle ()
8148 "Toggle marking the entry at point for bulk action."
8149 (interactive)
8150 (if (org-agenda-bulk-marked-p)
8151 (org-agenda-bulk-unmark)
8152 (org-agenda-bulk-mark)))
8154 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8155 "Remove the mark overlays between BEG and END in the agenda buffer.
8156 BEG and END default to the buffer limits.
8158 This only removes the overlays, it does not remove the markers
8159 from the list in `org-agenda-bulk-marked-entries'."
8160 (interactive)
8161 (mapc (lambda (ov)
8162 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8163 (delete-overlay ov)))
8164 (overlays-in (or beg (point-min)) (or end (point-max)))))
8166 (defun org-agenda-bulk-remove-all-marks ()
8167 "Remove all marks in the agenda buffer.
8168 This will remove the markers, and the overlays."
8169 (interactive)
8170 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8171 (setq org-agenda-bulk-marked-entries nil)
8172 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8174 (defun org-agenda-bulk-action (&optional arg)
8175 "Execute an remote-editing action on all marked entries.
8176 The prefix arg is passed through to the command if possible."
8177 (interactive "P")
8178 ;; Make sure we have markers, and only valid ones
8179 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8180 (mapc
8181 (lambda (m)
8182 (unless (and (markerp m)
8183 (marker-buffer m)
8184 (buffer-live-p (marker-buffer m))
8185 (marker-position m))
8186 (error "Marker %s for bulk command is invalid" m)))
8187 org-agenda-bulk-marked-entries)
8189 ;; Prompt for the bulk command
8190 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8191 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8192 (when org-agenda-bulk-custom-functions
8193 (concat " Custom: ["
8194 (mapconcat (lambda(f) (char-to-string (car f)))
8195 org-agenda-bulk-custom-functions "")
8196 "]"))))
8197 (let* ((action (read-char-exclusive))
8198 (org-log-refile (if org-log-refile 'time nil))
8199 (entries (reverse org-agenda-bulk-marked-entries))
8200 redo-at-end
8201 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8202 (cond
8203 ((equal action ?$)
8204 (setq cmd '(org-agenda-archive)))
8206 ((equal action ?A)
8207 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8209 ((member action '(?r ?w))
8210 (setq rfloc (org-refile-get-location
8211 "Refile to"
8212 (marker-buffer (car org-agenda-bulk-marked-entries))
8213 org-refile-allow-creating-parent-nodes))
8214 (if (nth 3 rfloc)
8215 (setcar (nthcdr 3 rfloc)
8216 (move-marker (make-marker) (nth 3 rfloc)
8217 (or (get-file-buffer (nth 1 rfloc))
8218 (find-buffer-visiting (nth 1 rfloc))
8219 (error "This should not happen")))))
8221 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8222 redo-at-end t))
8224 ((equal action ?t)
8225 (setq state (org-icompleting-read
8226 "Todo state: "
8227 (with-current-buffer (marker-buffer (car entries))
8228 (mapcar 'list org-todo-keywords-1))))
8229 (setq cmd `(let ((org-inhibit-blocking t)
8230 (org-inhibit-logging 'note))
8231 (org-agenda-todo ,state))))
8233 ((memq action '(?- ?+))
8234 (setq tag (org-icompleting-read
8235 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8236 (with-current-buffer (marker-buffer (car entries))
8237 (delq nil
8238 (mapcar (lambda (x)
8239 (if (stringp (car x)) x)) org-tag-alist)))))
8240 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8242 ((memq action '(?s ?d))
8243 (let* ((date (unless arg
8244 (org-read-date
8245 nil nil nil
8246 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8247 (ans (if arg nil org-read-date-final-answer))
8248 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8249 (setq cmd `(let* ((bound (fboundp 'read-string))
8250 (old (and bound (symbol-function 'read-string))))
8251 (unwind-protect
8252 (progn
8253 (fset 'read-string (lambda (&rest ignore) ,ans))
8254 (eval '(,c1 arg)))
8255 (if bound
8256 (fset 'read-string old)
8257 (fmakunbound 'read-string)))))))
8259 ((equal action ?S)
8260 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8261 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8262 (let ((days (read-number
8263 (format "Scatter tasks across how many %sdays: "
8264 (if arg "week" "")) 7)))
8265 (setq cmd
8266 `(let ((distance (1+ (random ,days))))
8267 (if arg
8268 (let ((dist distance)
8269 (day-of-week
8270 (calendar-day-of-week
8271 (calendar-gregorian-from-absolute (org-today)))))
8272 (dotimes (i (1+ dist))
8273 (while (member day-of-week org-agenda-weekend-days)
8274 (incf distance)
8275 (incf day-of-week)
8276 (if (= day-of-week 7)
8277 (setq day-of-week 0)))
8278 (incf day-of-week)
8279 (if (= day-of-week 7)
8280 (setq day-of-week 0)))))
8281 ;; silently fail when try to replan a sexp entry
8282 (condition-case nil
8283 (let* ((date (calendar-gregorian-from-absolute
8284 (+ (org-today) distance)))
8285 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8286 (nth 2 date))))
8287 (org-agenda-schedule nil time))
8288 (error nil)))))))
8290 ((assoc action org-agenda-bulk-custom-functions)
8291 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8292 redo-at-end t))
8294 ((equal action ?f)
8295 (setq cmd (list (intern
8296 (org-icompleting-read "Function: "
8297 obarray 'fboundp t nil nil)))))
8299 (t (error "Invalid bulk action")))
8301 ;; Sort the markers, to make sure that parents are handled before children
8302 (setq entries (sort entries
8303 (lambda (a b)
8304 (cond
8305 ((equal (marker-buffer a) (marker-buffer b))
8306 (< (marker-position a) (marker-position b)))
8308 (string< (buffer-name (marker-buffer a))
8309 (buffer-name (marker-buffer b))))))))
8311 ;; Now loop over all markers and apply cmd
8312 (while (setq e (pop entries))
8313 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8314 (if (not pos)
8315 (progn (message "Skipping removed entry at %s" e)
8316 (setq cntskip (1+ cntskip)))
8317 (goto-char pos)
8318 (eval cmd)
8319 (setq org-agenda-bulk-marked-entries
8320 (delete e org-agenda-bulk-marked-entries))
8321 (setq cnt (1+ cnt))))
8322 (setq org-agenda-bulk-marked-entries nil)
8323 (org-agenda-bulk-remove-all-marks)
8324 (when redo-at-end (org-agenda-redo))
8325 (message "Acted on %d entries%s"
8327 (if (= cntskip 0)
8329 (format ", skipped %d (disappeared before their turn)"
8330 cntskip)))))
8332 ;;; Flagging notes
8334 (defun org-agenda-show-the-flagging-note ()
8335 "Display the flagging note in the other window.
8336 When called a second time in direct sequence, offer to remove the FLAGGING
8337 tag and (if present) the flagging note."
8338 (interactive)
8339 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8340 (win (selected-window))
8341 note heading newhead)
8342 (unless hdmarker
8343 (error "No linked entry at point"))
8344 (if (and (eq this-command last-command)
8345 (y-or-n-p "Unflag and remove any flagging note? "))
8346 (progn
8347 (org-agenda-remove-flag hdmarker)
8348 (let ((win (get-buffer-window "*Flagging Note*")))
8349 (and win (delete-window win)))
8350 (message "Entry unflaged"))
8351 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8352 (unless note
8353 (error "No flagging note"))
8354 (org-kill-new note)
8355 (org-switch-to-buffer-other-window "*Flagging Note*")
8356 (erase-buffer)
8357 (insert note)
8358 (goto-char (point-min))
8359 (while (re-search-forward "\\\\n" nil t)
8360 (replace-match "\n" t t))
8361 (goto-char (point-min))
8362 (select-window win)
8363 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8365 (defun org-agenda-remove-flag (marker)
8366 "Remove the FLAGGED tag and any flagging note in the entry."
8367 (let (newhead)
8368 (org-with-point-at marker
8369 (org-toggle-tag "FLAGGED" 'off)
8370 (org-entry-delete nil "THEFLAGGINGNOTE")
8371 (setq newhead (org-get-heading)))
8372 (org-agenda-change-all-lines newhead marker)
8373 (message "Entry unflaged")))
8375 (defun org-agenda-get-any-marker (&optional pos)
8376 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8377 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8379 ;;; Appointment reminders
8381 (defvar appt-time-msg-list)
8383 ;;;###autoload
8384 (defun org-agenda-to-appt (&optional refresh filter)
8385 "Activate appointments found in `org-agenda-files'.
8386 With a \\[universal-argument] prefix, refresh the list of
8387 appointments.
8389 If FILTER is t, interactively prompt the user for a regular
8390 expression, and filter out entries that don't match it.
8392 If FILTER is a string, use this string as a regular expression
8393 for filtering entries out.
8395 FILTER can also be an alist with the car of each cell being
8396 either 'headline or 'category. For example:
8398 '((headline \"IMPORTANT\")
8399 (category \"Work\"))
8401 will only add headlines containing IMPORTANT or headlines
8402 belonging to the \"Work\" category."
8403 (interactive "P")
8404 (if refresh (setq appt-time-msg-list nil))
8405 (if (eq filter t)
8406 (setq filter (read-from-minibuffer "Regexp filter: ")))
8407 (let* ((cnt 0) ; count added events
8408 (org-agenda-new-buffers nil)
8409 (org-deadline-warning-days 0)
8410 ;; Do not use `org-today' here because appt only takes
8411 ;; time and without date as argument, so it may pass wrong
8412 ;; information otherwise
8413 (today (org-date-to-gregorian
8414 (time-to-days (current-time))))
8415 (org-agenda-restrict nil)
8416 (files (org-agenda-files 'unrestricted)) entries file)
8417 ;; Get all entries which may contain an appt
8418 (org-prepare-agenda-buffers files)
8419 (while (setq file (pop files))
8420 (setq entries
8421 (append entries
8422 (org-agenda-get-day-entries
8423 file today :timestamp :scheduled :deadline))))
8424 (setq entries (delq nil entries))
8425 ;; Map thru entries and find if we should filter them out
8426 (mapc
8427 (lambda(x)
8428 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8429 (cat (get-text-property 1 'org-category x))
8430 (tod (get-text-property 1 'time-of-day x))
8431 (ok (or (null filter)
8432 (and (stringp filter) (string-match filter evt))
8433 (and (listp filter)
8434 (or (string-match
8435 (cadr (assoc 'category filter)) cat)
8436 (string-match
8437 (cadr (assoc 'headline filter)) evt))))))
8438 ;; FIXME: Shall we remove text-properties for the appt text?
8439 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8440 (when (and ok tod)
8441 (setq tod (concat "00" (number-to-string tod))
8442 tod (when (string-match
8443 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8444 (concat (match-string 1 tod) ":"
8445 (match-string 2 tod))))
8446 (appt-add tod evt)
8447 (setq cnt (1+ cnt))))) entries)
8448 (org-release-buffers org-agenda-new-buffers)
8449 (if (eq cnt 0)
8450 (message "No event to add")
8451 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8453 (defun org-agenda-todayp (date)
8454 "Does DATE mean today, when considering `org-extend-today-until'?"
8455 (let ((today (org-today))
8456 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8457 date)))
8458 (eq date today)))
8460 (defun org-agenda-todo-yesterday (&optional arg)
8461 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8462 (interactive "P")
8463 (let* ((hour (third (decode-time
8464 (org-current-time))))
8465 (org-extend-today-until (1+ hour)))
8466 (org-agenda-todo arg)))
8468 (provide 'org-agenda)
8470 ;;; org-agenda.el ends here