org-agenda.el: Correctly set the `org-category-pos' property.
[org-mode.git] / lisp / org-agenda.el
blob047e3e36fff3fdb856e2fad8b49863d6c4ca5e18
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 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 (org-split-string org-select-this-todo-keyword "|") "\\|")
4583 "\\)\\>"))
4584 org-not-done-regexp)
4585 "[^\n\r]*\\)"))
4586 marker priority category tags todo-state
4587 ee txt beg end)
4588 (goto-char (point-min))
4589 (while (re-search-forward regexp nil t)
4590 (catch :skip
4591 (save-match-data
4592 (beginning-of-line)
4593 (org-agenda-skip)
4594 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4595 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4596 (goto-char (1+ beg))
4597 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4598 (throw :skip nil)))
4599 (goto-char (match-beginning 1))
4600 (setq marker (org-agenda-new-marker (match-beginning 0))
4601 category (org-get-category)
4602 category-pos (get-text-property (point) 'org-category-position)
4603 txt (match-string 1)
4604 tags (org-get-tags-at (point))
4605 txt (org-format-agenda-item "" txt category tags)
4606 priority (1+ (org-get-priority txt))
4607 todo-state (org-get-todo-state))
4608 (org-add-props txt props
4609 'org-marker marker 'org-hd-marker marker
4610 'priority priority 'org-category category
4611 'org-category-position category-pos
4612 'type "todo" 'todo-state todo-state)
4613 (push txt ee)
4614 (if org-agenda-todo-list-sublevels
4615 (goto-char (match-end 1))
4616 (org-end-of-subtree 'invisible))))
4617 (nreverse ee)))
4619 (defun org-agenda-todo-custom-ignore-p (time n)
4620 "Check whether timestamp is farther away then n number of days.
4621 This function is invoked if `org-agenda-todo-ignore-deadlines',
4622 `org-agenda-todo-ignore-scheduled' or
4623 `org-agenda-todo-ignore-timestamp' is set to an integer."
4624 (let ((days (org-days-to-time time)))
4625 (if (>= n 0)
4626 (>= days n)
4627 (<= days n))))
4629 ;;;###autoload
4630 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4631 (&optional end)
4632 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4633 (when (or org-agenda-todo-ignore-with-date
4634 org-agenda-todo-ignore-scheduled
4635 org-agenda-todo-ignore-deadlines
4636 org-agenda-todo-ignore-timestamp)
4637 (setq end (or end (save-excursion (outline-next-heading) (point))))
4638 (save-excursion
4639 (or (and org-agenda-todo-ignore-with-date
4640 (re-search-forward org-ts-regexp end t))
4641 (and org-agenda-todo-ignore-scheduled
4642 (re-search-forward org-scheduled-time-regexp end t)
4643 (cond
4644 ((eq org-agenda-todo-ignore-scheduled 'future)
4645 (> (org-days-to-time (match-string 1)) 0))
4646 ((eq org-agenda-todo-ignore-scheduled 'past)
4647 (<= (org-days-to-time (match-string 1)) 0))
4648 ((numberp org-agenda-todo-ignore-scheduled)
4649 (org-agenda-todo-custom-ignore-p
4650 (match-string 1) org-agenda-todo-ignore-scheduled))
4651 (t)))
4652 (and org-agenda-todo-ignore-deadlines
4653 (re-search-forward org-deadline-time-regexp end t)
4654 (cond
4655 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4656 ((eq org-agenda-todo-ignore-deadlines 'far)
4657 (not (org-deadline-close (match-string 1))))
4658 ((eq org-agenda-todo-ignore-deadlines 'future)
4659 (> (org-days-to-time (match-string 1)) 0))
4660 ((eq org-agenda-todo-ignore-deadlines 'past)
4661 (<= (org-days-to-time (match-string 1)) 0))
4662 ((numberp org-agenda-todo-ignore-deadlines)
4663 (org-agenda-todo-custom-ignore-p
4664 (match-string 1) org-agenda-todo-ignore-deadlines))
4665 (t (org-deadline-close (match-string 1)))))
4666 (and org-agenda-todo-ignore-timestamp
4667 (let ((buffer (current-buffer))
4668 (regexp
4669 (concat
4670 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4671 (start (point)))
4672 ;; Copy current buffer into a temporary one
4673 (with-temp-buffer
4674 (insert-buffer-substring buffer start end)
4675 (goto-char (point-min))
4676 ;; Delete SCHEDULED and DEADLINE items
4677 (while (re-search-forward regexp end t)
4678 (delete-region (match-beginning 0) (match-end 0)))
4679 (goto-char (point-min))
4680 ;; No search for timestamp left
4681 (when (re-search-forward org-ts-regexp nil t)
4682 (cond
4683 ((eq org-agenda-todo-ignore-timestamp 'future)
4684 (> (org-days-to-time (match-string 1)) 0))
4685 ((eq org-agenda-todo-ignore-timestamp 'past)
4686 (<= (org-days-to-time (match-string 1)) 0))
4687 ((numberp org-agenda-todo-ignore-timestamp)
4688 (org-agenda-todo-custom-ignore-p
4689 (match-string 1) org-agenda-todo-ignore-timestamp))
4690 (t))))))))))
4692 (defconst org-agenda-no-heading-message
4693 "No heading for this item in buffer or region.")
4695 (defun org-agenda-get-timestamps ()
4696 "Return the date stamp information for agenda display."
4697 (let* ((props (list 'face nil
4698 'org-not-done-regexp org-not-done-regexp
4699 'org-todo-regexp org-todo-regexp
4700 'org-complex-heading-regexp org-complex-heading-regexp
4701 'mouse-face 'highlight
4702 'help-echo
4703 (format "mouse-2 or RET jump to org file %s"
4704 (abbreviate-file-name buffer-file-name))))
4705 (d1 (calendar-absolute-from-gregorian date))
4706 (remove-re
4707 (concat
4708 (regexp-quote
4709 (format-time-string
4710 "<%Y-%m-%d"
4711 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4712 ".*?>"))
4713 (regexp
4714 (concat
4715 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4716 (regexp-quote
4717 (substring
4718 (format-time-string
4719 (car org-time-stamp-formats)
4720 (apply 'encode-time ; DATE bound by calendar
4721 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4722 1 11))
4723 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4724 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4725 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4726 donep tmp priority category category-pos ee txt timestr tags
4727 b0 b3 e3 head todo-state end-of-match show-all)
4728 (goto-char (point-min))
4729 (while (setq end-of-match (re-search-forward regexp nil t))
4730 (setq b0 (match-beginning 0)
4731 b3 (match-beginning 3) e3 (match-end 3)
4732 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4733 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4734 (member todo-state
4735 org-agenda-repeating-timestamp-show-all)))
4736 (catch :skip
4737 (and (org-at-date-range-p) (throw :skip nil))
4738 (org-agenda-skip)
4739 (if (and (match-end 1)
4740 (not (= d1 (org-time-string-to-absolute
4741 (match-string 1) d1 nil show-all))))
4742 (throw :skip nil))
4743 (if (and e3
4744 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4745 (throw :skip nil))
4746 (setq tmp (buffer-substring (max (point-min)
4747 (- b0 org-ds-keyword-length))
4749 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4750 inactivep (= (char-after b0) ?\[)
4751 deadlinep (string-match org-deadline-regexp tmp)
4752 scheduledp (string-match org-scheduled-regexp tmp)
4753 closedp (and org-agenda-include-inactive-timestamps
4754 (string-match org-closed-string tmp))
4755 clockp (and org-agenda-include-inactive-timestamps
4756 (or (string-match org-clock-string tmp)
4757 (string-match "]-+\\'" tmp)))
4758 donep (member todo-state org-done-keywords))
4759 (if (or scheduledp deadlinep closedp clockp
4760 (and donep org-agenda-skip-timestamp-if-done))
4761 (throw :skip t))
4762 (if (string-match ">" timestr)
4763 ;; substring should only run to end of time stamp
4764 (setq timestr (substring timestr 0 (match-end 0))))
4765 (setq marker (org-agenda-new-marker b0)
4766 category (org-get-category b0)
4767 category-pos (get-text-property b0 'org-category-position))
4768 (save-excursion
4769 (if (not (re-search-backward org-outline-regexp-bol nil t))
4770 (setq txt org-agenda-no-heading-message)
4771 (goto-char (match-beginning 0))
4772 (setq hdmarker (org-agenda-new-marker)
4773 tags (org-get-tags-at))
4774 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4775 (setq head (or (match-string 1) ""))
4776 (setq txt (org-format-agenda-item
4777 (if inactivep org-agenda-inactive-leader nil)
4778 head category tags timestr
4779 remove-re)))
4780 (setq priority (org-get-priority txt))
4781 (org-add-props txt props
4782 'org-marker marker 'org-hd-marker hdmarker)
4783 (org-add-props txt nil 'priority priority
4784 'org-category category 'date date
4785 'org-category-position category-pos
4786 'todo-state todo-state
4787 'type "timestamp")
4788 (push txt ee))
4789 (if org-agenda-skip-additional-timestamps-same-entry
4790 (outline-next-heading)
4791 (goto-char end-of-match))))
4792 (nreverse ee)))
4794 (defun org-agenda-get-sexps ()
4795 "Return the sexp information for agenda display."
4796 (require 'diary-lib)
4797 (let* ((props (list 'mouse-face 'highlight
4798 'help-echo
4799 (format "mouse-2 or RET jump to org file %s"
4800 (abbreviate-file-name buffer-file-name))))
4801 (regexp "^&?%%(")
4802 marker category category-pos ee txt tags entry
4803 result beg b sexp sexp-entry todo-state)
4804 (goto-char (point-min))
4805 (while (re-search-forward regexp nil t)
4806 (catch :skip
4807 (org-agenda-skip)
4808 (setq beg (match-beginning 0))
4809 (goto-char (1- (match-end 0)))
4810 (setq b (point))
4811 (forward-sexp 1)
4812 (setq sexp (buffer-substring b (point)))
4813 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4814 (org-trim (match-string 1))
4815 ""))
4816 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4817 (when result
4818 (setq marker (org-agenda-new-marker beg)
4819 category (org-get-category beg)
4820 category-pos (get-text-property beg 'org-category-position)
4821 todo-state (org-get-todo-state))
4823 (dolist (r (if (stringp result)
4824 (list result)
4825 result)) ;; we expect a list here
4826 (if (string-match "\\S-" r)
4827 (setq txt r)
4828 (setq txt "SEXP entry returned empty string"))
4830 (setq txt (org-format-agenda-item
4831 "" txt category tags 'time))
4832 (org-add-props txt props 'org-marker marker)
4833 (org-add-props txt nil
4834 'org-category category 'date date 'todo-state todo-state
4835 'org-category-position category-pos
4836 'type "sexp")
4837 (push txt ee)))))
4838 (nreverse ee)))
4840 ;; Calendar sanity: define some functions that are independent of
4841 ;; `calendar-date-style'.
4842 ;; Normally I would like to use ISO format when calling the diary functions,
4843 ;; but to make sure we still have Emacs 22 compatibility we bind
4844 ;; also `european-calendar-style' and use european format
4845 (defun org-anniversary (year month day &optional mark)
4846 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4847 (org-no-warnings
4848 (let ((calendar-date-style 'european) (european-calendar-style t))
4849 (diary-anniversary day month year mark))))
4850 (defun org-cyclic (N year month day &optional mark)
4851 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4852 (org-no-warnings
4853 (let ((calendar-date-style 'european) (european-calendar-style t))
4854 (diary-cyclic N day month year mark))))
4855 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4856 "Like `diary-block', but with fixed (ISO) order of arguments."
4857 (org-no-warnings
4858 (let ((calendar-date-style 'european) (european-calendar-style t))
4859 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4860 (defun org-date (year month day &optional mark)
4861 "Like `diary-date', but with fixed (ISO) order of arguments."
4862 (org-no-warnings
4863 (let ((calendar-date-style 'european) (european-calendar-style t))
4864 (diary-date day month year mark))))
4865 (defalias 'org-float 'diary-float)
4867 ;; Define the` org-class' function
4868 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4869 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4870 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4871 is any number of ISO weeks in the block period for which the item should
4872 be skipped."
4873 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4874 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4875 (d (calendar-absolute-from-gregorian date)))
4876 (and
4877 (<= date1 d)
4878 (<= d date2)
4879 (= (calendar-day-of-week date) dayname)
4880 (or (not skip-weeks)
4881 (progn
4882 (require 'cal-iso)
4883 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4884 entry)))
4886 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4887 "Like `org-class', but honor `calendar-date-style'.
4888 The order of the first 2 times 3 arguments depends on the variable
4889 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4890 So for American calendars, give this as MONTH DAY YEAR, for European as
4891 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4892 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4893 is any number of ISO weeks in the block period for which the item should
4894 be skipped.
4896 This function is here only for backward compatibility and it is deprecated,
4897 please use `org-class' instead."
4898 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4899 (date2 (org-order-calendar-date-args m2 d2 y2)))
4900 (org-class
4901 (nth 2 date1) (car date1) (nth 1 date1)
4902 (nth 2 date2) (car date2) (nth 1 date2)
4903 dayname skip-weeks)))
4905 (defalias 'org-get-closed 'org-agenda-get-progress)
4906 (defun org-agenda-get-progress ()
4907 "Return the logged TODO entries for agenda display."
4908 (let* ((props (list 'mouse-face 'highlight
4909 'org-not-done-regexp org-not-done-regexp
4910 'org-todo-regexp org-todo-regexp
4911 'org-complex-heading-regexp org-complex-heading-regexp
4912 'help-echo
4913 (format "mouse-2 or RET jump to org file %s"
4914 (abbreviate-file-name buffer-file-name))))
4915 (items (if (consp org-agenda-show-log)
4916 org-agenda-show-log
4917 (if (eq org-agenda-show-log 'clockcheck)
4918 '(clock)
4919 org-agenda-log-mode-items)))
4920 (parts
4921 (delq nil
4922 (list
4923 (if (memq 'closed items) (concat "\\<" org-closed-string))
4924 (if (memq 'clock items) (concat "\\<" org-clock-string))
4925 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4926 (parts-re (if parts (mapconcat 'identity parts "\\|")
4927 (error "`org-agenda-log-mode-items' is empty")))
4928 (regexp (concat
4929 "\\(" parts-re "\\)"
4930 " *\\["
4931 (regexp-quote
4932 (substring
4933 (format-time-string
4934 (car org-time-stamp-formats)
4935 (apply 'encode-time ; DATE bound by calendar
4936 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4937 1 11))))
4938 (org-agenda-search-headline-for-time nil)
4939 marker hdmarker priority category category-pos tags closedp
4940 statep clockp state ee txt extra timestr rest clocked)
4941 (goto-char (point-min))
4942 (while (re-search-forward regexp nil t)
4943 (catch :skip
4944 (org-agenda-skip)
4945 (setq marker (org-agenda-new-marker (match-beginning 0))
4946 closedp (equal (match-string 1) org-closed-string)
4947 statep (equal (string-to-char (match-string 1)) ?-)
4948 clockp (not (or closedp statep))
4949 state (and statep (match-string 2))
4950 category (org-get-category (match-beginning 0))
4951 category-pos (get-text-property (match-beginning 0) 'org-category-position)
4952 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4953 (when (string-match "\\]" timestr)
4954 ;; substring should only run to end of time stamp
4955 (setq rest (substring timestr (match-end 0))
4956 timestr (substring timestr 0 (match-end 0)))
4957 (if (and (not closedp) (not statep)
4958 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4959 rest))
4960 (progn (setq timestr (concat (substring timestr 0 -1)
4961 "-" (match-string 1 rest) "]"))
4962 (setq clocked (match-string 2 rest)))
4963 (setq clocked "-")))
4964 (save-excursion
4965 (setq extra
4966 (cond
4967 ((not org-agenda-log-mode-add-notes) nil)
4968 (statep
4969 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4970 (match-string 1)))
4971 (clockp
4972 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4973 (match-string 1)))))
4974 (if (not (re-search-backward org-outline-regexp-bol nil t))
4975 (setq txt org-agenda-no-heading-message)
4976 (goto-char (match-beginning 0))
4977 (setq hdmarker (org-agenda-new-marker)
4978 tags (org-get-tags-at))
4979 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4980 (setq txt (match-string 1))
4981 (when extra
4982 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4983 (setq txt (concat (substring txt 0 (match-beginning 1))
4984 " - " extra " " (match-string 2 txt)))
4985 (setq txt (concat txt " - " extra))))
4986 (setq txt (org-format-agenda-item
4987 (cond
4988 (closedp "Closed: ")
4989 (statep (concat "State: (" state ")"))
4990 (t (concat "Clocked: (" clocked ")")))
4991 txt category tags timestr)))
4992 (setq priority 100000)
4993 (org-add-props txt props
4994 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4995 'priority priority 'org-category category
4996 'org-category-position category-pos
4997 'type "closed" 'date date
4998 'undone-face 'org-warning 'done-face 'org-agenda-done)
4999 (push txt ee))
5000 (goto-char (point-at-eol))))
5001 (nreverse ee)))
5003 (defun org-agenda-show-clocking-issues ()
5004 "Add overlays, showing issues with clocking.
5005 See also the user option `org-agenda-clock-consistency-checks'."
5006 (interactive)
5007 (let* ((pl org-agenda-clock-consistency-checks)
5008 (re (concat "^[ \t]*"
5009 org-clock-string
5010 "[ \t]+"
5011 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5012 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5013 (tlstart 0.)
5014 (tlend 0.)
5015 (maxtime (org-hh:mm-string-to-minutes
5016 (or (plist-get pl :max-duration) "24:00")))
5017 (mintime (org-hh:mm-string-to-minutes
5018 (or (plist-get pl :min-duration) 0)))
5019 (maxgap (org-hh:mm-string-to-minutes
5020 ;; default 30:00 means never complain
5021 (or (plist-get pl :max-gap) "30:00")))
5022 (gapok (mapcar 'org-hh:mm-string-to-minutes
5023 (plist-get pl :gap-ok-around)))
5024 (def-face (or (plist-get pl :default-face)
5025 '((:background "DarkRed") (:foreground "white"))))
5026 issue face m te ts dt ov)
5027 (goto-char (point-min))
5028 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5029 (setq issue nil face def-face)
5030 (catch 'next
5031 (setq m (org-get-at-bol 'org-marker)
5032 te nil ts nil)
5033 (unless (and m (markerp m))
5034 (setq issue "No valid clock line") (throw 'next t))
5035 (org-with-point-at m
5036 (save-excursion
5037 (goto-char (point-at-bol))
5038 (unless (looking-at re)
5039 (error "No valid Clock line")
5040 (throw 'next t))
5041 (unless (match-end 3)
5042 (setq issue "No end time"
5043 face (or (plist-get pl :no-end-time-face) face))
5044 (throw 'next t))
5045 (setq ts (match-string 1)
5046 te (match-string 3)
5047 ts (org-float-time
5048 (apply 'encode-time (org-parse-time-string ts)))
5049 te (org-float-time
5050 (apply 'encode-time (org-parse-time-string te)))
5051 dt (- te ts))))
5052 (cond
5053 ((> dt (* 60 maxtime))
5054 ;; a very long clocking chunk
5055 (setq issue (format "Clocking interval is very long: %s"
5056 (org-minutes-to-hh:mm-string
5057 (floor (/ (float dt) 60.))))
5058 face (or (plist-get pl :long-face) face)))
5059 ((< dt (* 60 mintime))
5060 ;; a very short clocking chunk
5061 (setq issue (format "Clocking interval is very short: %s"
5062 (org-minutes-to-hh:mm-string
5063 (floor (/ (float dt) 60.))))
5064 face (or (plist-get pl :short-face) face)))
5065 ((and (> tlend 0) (< ts tlend))
5066 ;; Two clock entries are overlapping
5067 (setq issue (format "Clocking overlap: %d minutes"
5068 (/ (- tlend ts) 60))
5069 face (or (plist-get pl :overlap-face) face)))
5070 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5071 ;; There is a gap, lets see if we need to report it
5072 (unless (org-agenda-check-clock-gap tlend ts gapok)
5073 (setq issue (format "Clocking gap: %d minutes"
5074 (/ (- ts tlend) 60))
5075 face (or (plist-get pl :gap-face) face))))
5076 (t nil)))
5077 (setq tlend (or te tlend) tlstart (or ts tlstart))
5078 (when issue
5079 ;; OK, there was some issue, add an overlay to show the issue
5080 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5081 (overlay-put ov 'before-string
5082 (concat
5083 (org-add-props
5084 (format "%-43s" (concat " " issue))
5086 'face face)
5087 "\n"))
5088 (overlay-put ov 'evaporate t)))))
5090 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5091 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5092 (catch 'exit
5093 (unless ok-list
5094 ;; there are no OK times for gaps...
5095 (throw 'exit nil))
5096 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5097 ;; This is more than 24 hours, so it is OK.
5098 ;; because we have at least one OK time, that must be in the
5099 ;; 24 hour interval.
5100 (throw 'exit t))
5101 ;; We have a shorter gap.
5102 ;; Now we have to get the minute of the day when these times are
5103 (let* ((t1dec (decode-time (seconds-to-time t1)))
5104 (t2dec (decode-time (seconds-to-time t2)))
5105 ;; compute the minute on the day
5106 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5107 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5108 (when (< min2 min1)
5109 ;; if min2 is smaller than min1, this means it is on the next day.
5110 ;; Wrap it to after midnight.
5111 (setq min2 (+ min2 1440)))
5112 ;; Now check if any of the OK times is in the gap
5113 (mapc (lambda (x)
5114 ;; Wrap the time to after midnight if necessary
5115 (if (< x min1) (setq x (+ x 1440)))
5116 ;; Check if in interval
5117 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5118 ok-list)
5119 ;; Nope, this gap is not OK
5120 nil)))
5122 (defun org-agenda-get-deadlines ()
5123 "Return the deadline information for agenda display."
5124 (let* ((props (list 'mouse-face 'highlight
5125 'org-not-done-regexp org-not-done-regexp
5126 'org-todo-regexp org-todo-regexp
5127 'org-complex-heading-regexp org-complex-heading-regexp
5128 'help-echo
5129 (format "mouse-2 or RET jump to org file %s"
5130 (abbreviate-file-name buffer-file-name))))
5131 (regexp org-deadline-time-regexp)
5132 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5133 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5134 d2 diff dfrac wdays pos pos1 category tags
5135 suppress-prewarning
5136 ee txt head face s todo-state show-all upcomingp donep timestr)
5137 (goto-char (point-min))
5138 (while (re-search-forward regexp nil t)
5139 (setq suppress-prewarning nil)
5140 (catch :skip
5141 (org-agenda-skip)
5142 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5143 (save-match-data
5144 (string-match org-scheduled-time-regexp
5145 (buffer-substring (point-at-bol)
5146 (point-at-eol)))))
5147 (setq suppress-prewarning
5148 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5149 org-agenda-skip-deadline-prewarning-if-scheduled
5150 0)))
5151 (setq s (match-string 1)
5152 txt nil
5153 pos (1- (match-beginning 1))
5154 todo-state (save-match-data (org-get-todo-state))
5155 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5156 (member todo-state
5157 org-agenda-repeating-timestamp-show-all))
5158 d2 (org-time-string-to-absolute
5159 (match-string 1) d1 'past show-all)
5160 diff (- d2 d1)
5161 wdays (if suppress-prewarning
5162 (let ((org-deadline-warning-days suppress-prewarning))
5163 (org-get-wdays s))
5164 (org-get-wdays s))
5165 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5166 upcomingp (and todayp (> diff 0)))
5167 ;; When to show a deadline in the calendar:
5168 ;; If the expiration is within wdays warning time.
5169 ;; Past-due deadlines are only shown on the current date
5170 (if (and (or (and (<= diff wdays)
5171 (and todayp (not org-agenda-only-exact-dates)))
5172 (= diff 0)))
5173 (save-excursion
5174 ;; (setq todo-state (org-get-todo-state))
5175 (setq donep (member todo-state org-done-keywords))
5176 (if (and donep
5177 (or org-agenda-skip-deadline-if-done
5178 (not (= diff 0))))
5179 (setq txt nil)
5180 (setq category (org-get-category)
5181 category-pos (get-text-property (point) 'org-category-position))
5182 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5183 (setq txt org-agenda-no-heading-message)
5184 (goto-char (match-end 0))
5185 (setq pos1 (match-beginning 0))
5186 (setq tags (org-get-tags-at pos1))
5187 (setq head (buffer-substring-no-properties
5188 (point)
5189 (progn (skip-chars-forward "^\r\n")
5190 (point))))
5191 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5192 (setq timestr
5193 (concat (substring s (match-beginning 1)) " "))
5194 (setq timestr 'time))
5195 (setq txt (org-format-agenda-item
5196 (if (= diff 0)
5197 (car org-agenda-deadline-leaders)
5198 (if (functionp
5199 (nth 1 org-agenda-deadline-leaders))
5200 (funcall
5201 (nth 1 org-agenda-deadline-leaders)
5202 diff date)
5203 (format (nth 1 org-agenda-deadline-leaders)
5204 diff)))
5205 head category tags
5206 (if (not (= diff 0)) nil timestr)))))
5207 (when txt
5208 (setq face (org-agenda-deadline-face dfrac))
5209 (org-add-props txt props
5210 'org-marker (org-agenda-new-marker pos)
5211 'org-hd-marker (org-agenda-new-marker pos1)
5212 'priority (+ (- diff)
5213 (org-get-priority txt))
5214 'org-category category
5215 'org-category-position category-pos
5216 'todo-state todo-state
5217 'type (if upcomingp "upcoming-deadline" "deadline")
5218 'date (if upcomingp date d2)
5219 'face (if donep 'org-agenda-done face)
5220 'undone-face face 'done-face 'org-agenda-done)
5221 (push txt ee))))))
5222 (nreverse ee)))
5224 (defun org-agenda-deadline-face (fraction)
5225 "Return the face to displaying a deadline item.
5226 FRACTION is what fraction of the head-warning time has passed."
5227 (let ((faces org-agenda-deadline-faces) f)
5228 (catch 'exit
5229 (while (setq f (pop faces))
5230 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5232 (defun org-agenda-get-scheduled (&optional deadline-results)
5233 "Return the scheduled information for agenda display."
5234 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5235 'org-todo-regexp org-todo-regexp
5236 'org-complex-heading-regexp org-complex-heading-regexp
5237 'done-face 'org-agenda-done
5238 'mouse-face 'highlight
5239 'help-echo
5240 (format "mouse-2 or RET jump to org file %s"
5241 (abbreviate-file-name buffer-file-name))))
5242 (regexp org-scheduled-time-regexp)
5243 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5244 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5246 (deadline-position-alist
5247 (mapcar (lambda (a) (and (setq mm (get-text-property
5248 0 'org-hd-marker a))
5249 (cons (marker-position mm) a)))
5250 deadline-results))
5251 d2 diff pos pos1 category tags donep
5252 ee txt head pastschedp todo-state face timestr s habitp show-all)
5253 (goto-char (point-min))
5254 (while (re-search-forward regexp nil t)
5255 (catch :skip
5256 (org-agenda-skip)
5257 (setq s (match-string 1)
5258 txt nil
5259 pos (1- (match-beginning 1))
5260 todo-state (save-match-data (org-get-todo-state))
5261 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5262 (member todo-state
5263 org-agenda-repeating-timestamp-show-all))
5264 d2 (org-time-string-to-absolute
5265 (match-string 1) d1 'past show-all)
5266 diff (- d2 d1))
5267 (setq pastschedp (and todayp (< diff 0)))
5268 ;; When to show a scheduled item in the calendar:
5269 ;; If it is on or past the date.
5270 (when (or (and (< diff 0)
5271 (< (abs diff) org-scheduled-past-days)
5272 (and todayp (not org-agenda-only-exact-dates)))
5273 (= diff 0))
5274 (save-excursion
5275 (setq donep (member todo-state org-done-keywords))
5276 (if (and donep
5277 (or org-agenda-skip-scheduled-if-done
5278 (not (= diff 0))
5279 (and (functionp 'org-is-habit-p)
5280 (org-is-habit-p))))
5281 (setq txt nil)
5282 (setq habitp (and (functionp 'org-is-habit-p)
5283 (org-is-habit-p)))
5284 (setq category (org-get-category)
5285 category-pos (get-text-property (point) 'org-category-position))
5286 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5287 (setq txt org-agenda-no-heading-message)
5288 (goto-char (match-end 0))
5289 (setq pos1 (match-beginning 0))
5290 (if habitp
5291 (if (or (not org-habit-show-habits)
5292 (and (not todayp)
5293 org-habit-show-habits-only-for-today))
5294 (throw :skip nil))
5295 (if (and
5296 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5297 (and org-agenda-skip-scheduled-if-deadline-is-shown
5298 pastschedp))
5299 (setq mm (assoc pos1 deadline-position-alist)))
5300 (throw :skip nil)))
5301 (setq tags (org-get-tags-at))
5302 (setq head (buffer-substring-no-properties
5303 (point)
5304 (progn (skip-chars-forward "^\r\n") (point))))
5305 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5306 (setq timestr
5307 (concat (substring s (match-beginning 1)) " "))
5308 (setq timestr 'time))
5309 (setq txt (org-format-agenda-item
5310 (if (= diff 0)
5311 (car org-agenda-scheduled-leaders)
5312 (format (nth 1 org-agenda-scheduled-leaders)
5313 (- 1 diff)))
5314 head category tags
5315 (if (not (= diff 0)) nil timestr)
5316 nil habitp))))
5317 (when txt
5318 (setq face
5319 (cond
5320 ((and (not habitp) pastschedp)
5321 'org-scheduled-previously)
5322 (todayp 'org-scheduled-today)
5323 (t 'org-scheduled))
5324 habitp (and habitp (org-habit-parse-todo)))
5325 (org-add-props txt props
5326 'undone-face face
5327 'face (if donep 'org-agenda-done face)
5328 'org-marker (org-agenda-new-marker pos)
5329 'org-hd-marker (org-agenda-new-marker pos1)
5330 'type (if pastschedp "past-scheduled" "scheduled")
5331 'date (if pastschedp d2 date)
5332 'priority (if habitp
5333 (org-habit-get-priority habitp)
5334 (+ 94 (- 5 diff) (org-get-priority txt)))
5335 'org-category category
5336 'org-category-position category-pos
5337 'org-habit-p habitp
5338 'todo-state todo-state)
5339 (push txt ee))))))
5340 (nreverse ee)))
5342 (defun org-agenda-get-blocks ()
5343 "Return the date-range information for agenda display."
5344 (let* ((props (list 'face nil
5345 'org-not-done-regexp org-not-done-regexp
5346 'org-todo-regexp org-todo-regexp
5347 'org-complex-heading-regexp org-complex-heading-regexp
5348 'mouse-face 'highlight
5349 'help-echo
5350 (format "mouse-2 or RET jump to org file %s"
5351 (abbreviate-file-name buffer-file-name))))
5352 (regexp org-tr-regexp)
5353 (d0 (calendar-absolute-from-gregorian date))
5354 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5355 head donep)
5356 (goto-char (point-min))
5357 (while (re-search-forward regexp nil t)
5358 (catch :skip
5359 (org-agenda-skip)
5360 (setq pos (point))
5361 (let ((start-time (match-string 1))
5362 (end-time (match-string 2)))
5363 (setq s1 (match-string 1)
5364 s2 (match-string 2)
5365 d1 (time-to-days (org-time-string-to-time s1))
5366 d2 (time-to-days (org-time-string-to-time s2)))
5367 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5368 ;; Only allow days between the limits, because the normal
5369 ;; date stamps will catch the limits.
5370 (save-excursion
5371 (setq todo-state (org-get-todo-state))
5372 (setq donep (member todo-state org-done-keywords))
5373 (if (and donep org-agenda-skip-timestamp-if-done)
5374 (throw :skip t))
5375 (setq marker (org-agenda-new-marker (point)))
5376 (setq category (org-get-category)
5377 category-pos (get-text-property (point) 'org-category-position))
5378 (if (not (re-search-backward org-outline-regexp-bol nil t))
5379 (setq txt org-agenda-no-heading-message)
5380 (goto-char (match-beginning 0))
5381 (setq hdmarker (org-agenda-new-marker (point)))
5382 (setq tags (org-get-tags-at))
5383 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5384 (setq head (match-string 1))
5385 (let ((remove-re
5386 (if org-agenda-remove-timeranges-from-blocks
5387 (concat
5388 "<" (regexp-quote s1) ".*?>"
5389 "--"
5390 "<" (regexp-quote s2) ".*?>")
5391 nil)))
5392 (setq txt (org-format-agenda-item
5393 (format
5394 (nth (if (= d1 d2) 0 1)
5395 org-agenda-timerange-leaders)
5396 (1+ (- d0 d1)) (1+ (- d2 d1)))
5397 head category tags
5398 (cond ((= d1 d0)
5399 (concat "<" start-time ">"))
5400 ((= d2 d0)
5401 (concat "<" end-time ">"))
5402 (t nil))
5403 remove-re))))
5404 (org-add-props txt props
5405 'org-marker marker 'org-hd-marker hdmarker
5406 'type "block" 'date date
5407 'todo-state todo-state
5408 'priority (org-get-priority txt) 'org-category category
5409 'org-category-position category-pos)
5410 (push txt ee))))
5411 (goto-char pos)))
5412 ;; Sort the entries by expiration date.
5413 (nreverse ee)))
5415 ;;; Agenda presentation and sorting
5417 (defvar org-prefix-has-time nil
5418 "A flag, set by `org-compile-prefix-format'.
5419 The flag is set if the currently compiled format contains a `%t'.")
5420 (defvar org-prefix-has-tag nil
5421 "A flag, set by `org-compile-prefix-format'.
5422 The flag is set if the currently compiled format contains a `%T'.")
5423 (defvar org-prefix-has-effort nil
5424 "A flag, set by `org-compile-prefix-format'.
5425 The flag is set if the currently compiled format contains a `%e'.")
5426 (defvar org-prefix-category-length nil
5427 "Used by `org-compile-prefix-format' to remember the category field width.")
5428 (defvar org-prefix-category-max-length nil
5429 "Used by `org-compile-prefix-format' to remember the category field width.")
5431 (defun org-agenda-get-category-icon (category)
5432 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5433 (dolist (entry org-agenda-category-icon-alist)
5434 (when (org-string-match-p (car entry) category)
5435 (if (listp (cadr entry))
5436 (return (cadr entry))
5437 (return (apply 'create-image (cdr entry)))))))
5439 (defun org-format-agenda-item (extra txt &optional category tags dotime
5440 remove-re habitp)
5441 "Format TXT to be inserted into the agenda buffer.
5442 In particular, it adds the prefix and corresponding text properties. EXTRA
5443 must be a string and replaces the `%s' specifier in the prefix format.
5444 CATEGORY (string, symbol or nil) may be used to overrule the default
5445 category taken from local variable or file name. It will replace the `%c'
5446 specifier in the format. DOTIME, when non-nil, indicates that a
5447 time-of-day should be extracted from TXT for sorting of this entry, and for
5448 the `%t' specifier in the format. When DOTIME is a string, this string is
5449 searched for a time before TXT is. TAGS can be the tags of the headline.
5450 Any match of REMOVE-RE will be removed from TXT."
5451 (save-match-data
5452 ;; Diary entries sometimes have extra whitespace at the beginning
5453 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5455 ;; Fix the tags part in txt
5456 (setq txt (org-agenda-fix-displayed-tags
5457 txt tags
5458 org-agenda-show-inherited-tags
5459 org-agenda-hide-tags-regexp))
5460 (let* ((category (or category
5461 (if (stringp org-category)
5462 org-category
5463 (and org-category (symbol-name org-category)))
5464 (if buffer-file-name
5465 (file-name-sans-extension
5466 (file-name-nondirectory buffer-file-name))
5467 "")))
5468 (category-icon (org-agenda-get-category-icon category))
5469 (category-icon (if category-icon
5470 (propertize " " 'display category-icon)
5471 ""))
5472 ;; time, tag, effort are needed for the eval of the prefix format
5473 (tag (if tags (nth (1- (length tags)) tags) ""))
5474 time effort neffort
5475 (ts (if dotime (concat
5476 (if (stringp dotime) dotime "")
5477 (and org-agenda-search-headline-for-time txt))))
5478 (time-of-day (and dotime (org-get-time-of-day ts)))
5479 stamp plain s0 s1 s2 rtn srp l
5480 duration thecategory)
5481 (and (org-mode-p) buffer-file-name
5482 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5483 (when (and dotime time-of-day)
5484 ;; Extract starting and ending time and move them to prefix
5485 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5486 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5487 (setq s0 (match-string 0 ts)
5488 srp (and stamp (match-end 3))
5489 s1 (match-string (if plain 1 2) ts)
5490 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5492 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5493 ;; them, we might want to remove them there to avoid duplication.
5494 ;; The user can turn this off with a variable.
5495 (if (and org-prefix-has-time
5496 org-agenda-remove-times-when-in-prefix (or stamp plain)
5497 (string-match (concat (regexp-quote s0) " *") txt)
5498 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5499 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5500 (= (match-beginning 0) 0)
5502 (setq txt (replace-match "" nil nil txt))))
5503 ;; Normalize the time(s) to 24 hour
5504 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5505 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5507 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5508 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5509 (setq s2
5510 (org-minutes-to-hh:mm-string
5511 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5513 ;; Compute the duration
5514 (when s2
5515 (setq duration (- (org-hh:mm-string-to-minutes s2)
5516 (org-hh:mm-string-to-minutes s1)))))
5518 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5519 txt)
5520 ;; Tags are in the string
5521 (if (or (eq org-agenda-remove-tags t)
5522 (and org-agenda-remove-tags
5523 org-prefix-has-tag))
5524 (setq txt (replace-match "" t t txt))
5525 (setq txt (replace-match
5526 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5527 (match-string 2 txt))
5528 t t txt))))
5529 (when (org-mode-p)
5530 (setq effort
5531 (condition-case nil
5532 (org-get-effort
5533 (or (get-text-property 0 'org-hd-marker txt)
5534 (get-text-property 0 'org-marker txt)))
5535 (error nil)))
5536 (when effort
5537 (setq neffort (org-duration-string-to-minutes effort)
5538 effort (setq effort (concat "[" effort "]")))))
5539 ;; prevent erroring out with %e format when there is no effort
5540 (or effort (setq effort ""))
5542 (when remove-re
5543 (while (string-match remove-re txt)
5544 (setq txt (replace-match "" t t txt))))
5546 ;; Set org-heading property on `txt' to mark the start of the
5547 ;; heading.
5548 (add-text-properties 0 (length txt) '(org-heading t) txt)
5550 ;; Prepare the variables needed in the eval of the compiled format
5551 (setq time (cond (s2 (concat
5552 (org-agenda-time-of-day-to-ampm-maybe s1)
5553 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5554 (if org-agenda-timegrid-use-ampm " ")))
5555 (s1 (concat
5556 (org-agenda-time-of-day-to-ampm-maybe s1)
5557 (if org-agenda-timegrid-use-ampm
5558 "........ "
5559 "......")))
5560 (t ""))
5561 extra (or (and (not habitp) extra) "")
5562 category (if (symbolp category) (symbol-name category) category)
5563 thecategory (copy-sequence category))
5564 (if (string-match org-bracket-link-regexp category)
5565 (progn
5566 (setq l (if (match-end 3)
5567 (- (match-end 3) (match-beginning 3))
5568 (- (match-end 1) (match-beginning 1))))
5569 (when (< l (or org-prefix-category-length 0))
5570 (setq category (copy-sequence category))
5571 (org-add-props category nil
5572 'extra-space (make-string
5573 (- org-prefix-category-length l 1) ?\ ))))
5574 (if (and org-prefix-category-max-length
5575 (>= (length category) org-prefix-category-max-length))
5576 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5577 ;; Evaluate the compiled format
5578 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5580 ;; And finally add the text properties
5581 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5582 (org-add-props rtn nil
5583 'org-category (if thecategory (downcase thecategory) category)
5584 'tags (mapcar 'org-downcase-keep-props tags)
5585 'org-highest-priority org-highest-priority
5586 'org-lowest-priority org-lowest-priority
5587 'time-of-day time-of-day
5588 'duration duration
5589 'effort effort
5590 'effort-minutes neffort
5591 'txt txt
5592 'time time
5593 'extra extra
5594 'format org-prefix-format-compiled
5595 'dotime dotime))))
5597 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5598 "Remove tags string from TXT, and add a modified list of tags.
5599 The modified list may contain inherited tags, and tags matched by
5600 `org-agenda-hide-tags-regexp' will be removed."
5601 (when (or add-inherited hide-re)
5602 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5603 (setq txt (substring txt 0 (match-beginning 0))))
5604 (setq tags
5605 (delq nil
5606 (mapcar (lambda (tg)
5607 (if (or (and hide-re (string-match hide-re tg))
5608 (and (not add-inherited)
5609 (get-text-property 0 'inherited tg)))
5611 tg))
5612 tags)))
5613 (when tags
5614 (let ((have-i (get-text-property 0 'inherited (car tags)))
5616 (setq txt (concat txt " :"
5617 (mapconcat
5618 (lambda (x)
5619 (setq i (get-text-property 0 'inherited x))
5620 (if (and have-i (not i))
5621 (progn
5622 (setq have-i nil)
5623 (concat ":" x))
5625 tags ":")
5626 (if have-i "::" ":"))))))
5627 txt)
5629 (defun org-downcase-keep-props (s)
5630 (let ((props (text-properties-at 0 s)))
5631 (setq s (downcase s))
5632 (add-text-properties 0 (length s) props s)
5635 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5636 (defvar org-agenda-sorting-strategy-selected nil)
5638 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5639 (catch 'exit
5640 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5641 ((and todayp (member 'today (car org-agenda-time-grid))))
5642 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5643 ((member 'weekly (car org-agenda-time-grid)))
5644 (t (throw 'exit list)))
5645 (let* ((have (delq nil (mapcar
5646 (lambda (x) (get-text-property 1 'time-of-day x))
5647 list)))
5648 (string (nth 1 org-agenda-time-grid))
5649 (gridtimes (nth 2 org-agenda-time-grid))
5650 (req (car org-agenda-time-grid))
5651 (remove (member 'remove-match req))
5652 new time)
5653 (if (and (member 'require-timed req) (not have))
5654 ;; don't show empty grid
5655 (throw 'exit list))
5656 (while (setq time (pop gridtimes))
5657 (unless (and remove (member time have))
5658 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5659 (push (org-format-agenda-item
5660 nil string "" nil
5661 (concat (substring time 0 -2) ":" (substring time -2)))
5662 new)
5663 (put-text-property
5664 2 (length (car new)) 'face 'org-time-grid (car new))))
5665 (when (and todayp org-agenda-show-current-time-in-grid)
5666 (push (org-format-agenda-item
5668 org-agenda-current-time-string
5669 "" nil
5670 (format-time-string "%H:%M "))
5671 new)
5672 (put-text-property
5673 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5675 (if (member 'time-up org-agenda-sorting-strategy-selected)
5676 (append new list)
5677 (append list new)))))
5679 (defun org-compile-prefix-format (key)
5680 "Compile the prefix format into a Lisp form that can be evaluated.
5681 The resulting form is returned and stored in the variable
5682 `org-prefix-format-compiled'."
5683 (setq org-prefix-has-time nil org-prefix-has-tag nil
5684 org-prefix-category-length nil
5685 org-prefix-has-effort nil)
5686 (let ((s (cond
5687 ((stringp org-agenda-prefix-format)
5688 org-agenda-prefix-format)
5689 ((assq key org-agenda-prefix-format)
5690 (cdr (assq key org-agenda-prefix-format)))
5691 (t " %-12:c%?-12t% s")))
5692 (start 0)
5693 varform vars var e c f opt)
5694 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5695 s start)
5696 (setq var (or (cdr (assoc (match-string 4 s)
5697 '(("c" . category) ("t" . time) ("s" . extra)
5698 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5699 'eval)
5700 c (or (match-string 3 s) "")
5701 opt (match-beginning 1)
5702 start (1+ (match-beginning 0)))
5703 (if (equal var 'time) (setq org-prefix-has-time t))
5704 (if (equal var 'tag) (setq org-prefix-has-tag t))
5705 (if (equal var 'effort) (setq org-prefix-has-effort t))
5706 (setq f (concat "%" (match-string 2 s) "s"))
5707 (when (equal var 'category)
5708 (setq org-prefix-category-length
5709 (floor (abs (string-to-number (match-string 2 s)))))
5710 (setq org-prefix-category-max-length
5711 (let ((x (match-string 2 s)))
5712 (save-match-data
5713 (if (string-match "\\.[0-9]+" x)
5714 (string-to-number (substring (match-string 0 x) 1)))))))
5715 (if (eq var 'eval)
5716 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5717 (if opt
5718 (setq varform
5719 `(if (equal "" ,var)
5721 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5722 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5723 (setq s (replace-match "%s" t nil s))
5724 (push varform vars))
5725 (setq vars (nreverse vars))
5726 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5728 (defun org-set-sorting-strategy (key)
5729 (if (symbolp (car org-agenda-sorting-strategy))
5730 ;; the old format
5731 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5732 (setq org-agenda-sorting-strategy-selected
5733 (or (cdr (assq key org-agenda-sorting-strategy))
5734 (cdr (assq 'agenda org-agenda-sorting-strategy))
5735 '(time-up category-keep priority-down)))))
5737 (defun org-get-time-of-day (s &optional string mod24)
5738 "Check string S for a time of day.
5739 If found, return it as a military time number between 0 and 2400.
5740 If not found, return nil.
5741 The optional STRING argument forces conversion into a 5 character wide string
5742 HH:MM."
5743 (save-match-data
5744 (when
5745 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5746 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5747 (let* ((h (string-to-number (match-string 1 s)))
5748 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5749 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5750 (am-p (equal ampm "am"))
5751 (h1 (cond ((not ampm) h)
5752 ((= h 12) (if am-p 0 12))
5753 (t (+ h (if am-p 0 12)))))
5754 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5755 (mod h1 24) h1))
5756 (t0 (+ (* 100 h2) m))
5757 (t1 (concat (if (>= h1 24) "+" " ")
5758 (if (and org-agenda-time-leading-zero
5759 (< t0 1000)) "0" "")
5760 (if (< t0 100) "0" "")
5761 (if (< t0 10) "0" "")
5762 (int-to-string t0))))
5763 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5765 (defvar org-agenda-before-sorting-filter-function nil
5766 "Function to be applied to agenda items prior to sorting.
5767 Prior to sorting also means just before they are inserted into the agenda.
5769 To aid sorting, you may revisit the original entries and add more text
5770 properties which will later be used by the sorting functions.
5772 The function should take a string argument, an agenda line.
5773 It has access to the text properties in that line, which contain among
5774 other things, the property `org-hd-marker' that points to the entry
5775 where the line comes from. Note that not all lines going into the agenda
5776 have this property, only most.
5778 The function should return the modified string. It is probably best
5779 to ONLY change text properties.
5781 You can also use this function as a filter, by returning nil for lines
5782 you don't want to have in the agenda at all. For this application, you
5783 could bind the variable in the options section of a custom command.")
5785 (defun org-finalize-agenda-entries (list &optional nosort)
5786 "Sort and concatenate the agenda items."
5787 (setq list (mapcar 'org-agenda-highlight-todo list))
5788 (if nosort
5789 list
5790 (when org-agenda-before-sorting-filter-function
5791 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5792 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5794 (defun org-agenda-highlight-todo (x)
5795 (let ((org-done-keywords org-done-keywords-for-agenda)
5796 (case-fold-search nil)
5798 (if (eq x 'line)
5799 (save-excursion
5800 (beginning-of-line 1)
5801 (setq re (org-get-at-bol 'org-todo-regexp))
5802 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5803 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5804 (add-text-properties (match-beginning 0) (match-end 1)
5805 (list 'face (org-get-todo-face 1)))
5806 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5807 (delete-region (match-beginning 1) (1- (match-end 0)))
5808 (goto-char (match-beginning 1))
5809 (insert (format org-agenda-todo-keyword-format s)))))
5810 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5811 (setq re (get-text-property 0 'org-todo-regexp x))
5812 (when (and re
5813 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5814 x (or pl 0)) pl))
5815 (add-text-properties
5816 (or (match-end 1) (match-end 0)) (match-end 0)
5817 (list 'face (org-get-todo-face (match-string 2 x)))
5819 (when (match-end 1)
5820 (setq x (concat (substring x 0 (match-end 1))
5821 (format org-agenda-todo-keyword-format
5822 (match-string 2 x))
5823 (org-add-props " " (text-properties-at 0 x))
5824 (substring x (match-end 3)))))))
5825 x)))
5827 (defsubst org-cmp-priority (a b)
5828 "Compare the priorities of string A and B."
5829 (let ((pa (or (get-text-property 1 'priority a) 0))
5830 (pb (or (get-text-property 1 'priority b) 0)))
5831 (cond ((> pa pb) +1)
5832 ((< pa pb) -1)
5833 (t nil))))
5835 (defsubst org-cmp-effort (a b)
5836 "Compare the effort values of string A and B."
5837 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5838 (ea (or (get-text-property 1 'effort-minutes a) def))
5839 (eb (or (get-text-property 1 'effort-minutes b) def)))
5840 (cond ((> ea eb) +1)
5841 ((< ea eb) -1)
5842 (t nil))))
5844 (defsubst org-cmp-category (a b)
5845 "Compare the string values of categories of strings A and B."
5846 (let ((ca (or (get-text-property 1 'org-category a) ""))
5847 (cb (or (get-text-property 1 'org-category b) "")))
5848 (cond ((string-lessp ca cb) -1)
5849 ((string-lessp cb ca) +1)
5850 (t nil))))
5852 (defsubst org-cmp-todo-state (a b)
5853 "Compare the todo states of strings A and B."
5854 (let* ((ma (or (get-text-property 1 'org-marker a)
5855 (get-text-property 1 'org-hd-marker a)))
5856 (mb (or (get-text-property 1 'org-marker b)
5857 (get-text-property 1 'org-hd-marker b)))
5858 (fa (and ma (marker-buffer ma)))
5859 (fb (and mb (marker-buffer mb)))
5860 (todo-kwds
5861 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5862 (and fb (with-current-buffer fb org-todo-keywords-1))))
5863 (ta (or (get-text-property 1 'todo-state a) ""))
5864 (tb (or (get-text-property 1 'todo-state b) ""))
5865 (la (- (length (member ta todo-kwds))))
5866 (lb (- (length (member tb todo-kwds))))
5867 (donepa (member ta org-done-keywords-for-agenda))
5868 (donepb (member tb org-done-keywords-for-agenda)))
5869 (cond ((and donepa (not donepb)) -1)
5870 ((and (not donepa) donepb) +1)
5871 ((< la lb) -1)
5872 ((< lb la) +1)
5873 (t nil))))
5875 (defsubst org-cmp-alpha (a b)
5876 "Compare the headlines, alphabetically."
5877 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5878 (plb (text-property-any 0 (length b) 'org-heading t b))
5879 (ta (and pla (substring a pla)))
5880 (tb (and plb (substring b plb))))
5881 (when pla
5882 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5883 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5884 (setq ta (substring ta (match-end 0))))
5885 (setq ta (downcase ta)))
5886 (when plb
5887 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5888 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5889 (setq tb (substring tb (match-end 0))))
5890 (setq tb (downcase tb)))
5891 (cond ((not ta) +1)
5892 ((not tb) -1)
5893 ((string-lessp ta tb) -1)
5894 ((string-lessp tb ta) +1)
5895 (t nil))))
5897 (defsubst org-cmp-tag (a b)
5898 "Compare the string values of the first tags of A and B."
5899 (let ((ta (car (last (get-text-property 1 'tags a))))
5900 (tb (car (last (get-text-property 1 'tags b)))))
5901 (cond ((not ta) +1)
5902 ((not tb) -1)
5903 ((string-lessp ta tb) -1)
5904 ((string-lessp tb ta) +1)
5905 (t nil))))
5907 (defsubst org-cmp-time (a b)
5908 "Compare the time-of-day values of strings A and B."
5909 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5910 (ta (or (get-text-property 1 'time-of-day a) def))
5911 (tb (or (get-text-property 1 'time-of-day b) def)))
5912 (cond ((< ta tb) -1)
5913 ((< tb ta) +1)
5914 (t nil))))
5916 (defsubst org-cmp-habit-p (a b)
5917 "Compare the todo states of strings A and B."
5918 (let ((ha (get-text-property 1 'org-habit-p a))
5919 (hb (get-text-property 1 'org-habit-p b)))
5920 (cond ((and ha (not hb)) -1)
5921 ((and (not ha) hb) +1)
5922 (t nil))))
5924 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5926 (defun org-entries-lessp (a b)
5927 "Predicate for sorting agenda entries."
5928 ;; The following variables will be used when the form is evaluated.
5929 ;; So even though the compiler complains, keep them.
5930 (let* ((ss org-agenda-sorting-strategy-selected)
5931 (time-up (and (org-em 'time-up 'time-down ss)
5932 (org-cmp-time a b)))
5933 (time-down (if time-up (- time-up) nil))
5934 (priority-up (and (org-em 'priority-up 'priority-down ss)
5935 (org-cmp-priority a b)))
5936 (priority-down (if priority-up (- priority-up) nil))
5937 (effort-up (and (org-em 'effort-up 'effort-down ss)
5938 (org-cmp-effort a b)))
5939 (effort-down (if effort-up (- effort-up) nil))
5940 (category-up (and (or (org-em 'category-up 'category-down ss)
5941 (memq 'category-keep ss))
5942 (org-cmp-category a b)))
5943 (category-down (if category-up (- category-up) nil))
5944 (category-keep (if category-up +1 nil))
5945 (tag-up (and (org-em 'tag-up 'tag-down ss)
5946 (org-cmp-tag a b)))
5947 (tag-down (if tag-up (- tag-up) nil))
5948 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5949 (org-cmp-todo-state a b)))
5950 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5951 (habit-up (and (org-em 'habit-up 'habit-down ss)
5952 (org-cmp-habit-p a b)))
5953 (habit-down (if habit-up (- habit-up) nil))
5954 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5955 (org-cmp-alpha a b)))
5956 (alpha-down (if alpha-up (- alpha-up) nil))
5957 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5958 user-defined-up user-defined-down)
5959 (if (and need-user-cmp org-agenda-cmp-user-defined
5960 (functionp org-agenda-cmp-user-defined))
5961 (setq user-defined-up
5962 (funcall org-agenda-cmp-user-defined a b)
5963 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5964 (cdr (assoc
5965 (eval (cons 'or org-agenda-sorting-strategy-selected))
5966 '((-1 . t) (1 . nil) (nil . nil))))))
5968 ;;; Agenda restriction lock
5970 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5971 "Overlay to mark the headline to which agenda commands are restricted.")
5972 (overlay-put org-agenda-restriction-lock-overlay
5973 'face 'org-agenda-restriction-lock)
5974 (overlay-put org-agenda-restriction-lock-overlay
5975 'help-echo "Agendas are currently limited to this subtree.")
5976 (org-detach-overlay org-agenda-restriction-lock-overlay)
5978 (defun org-agenda-set-restriction-lock (&optional type)
5979 "Set restriction lock for agenda, to current subtree or file.
5980 Restriction will be the file if TYPE is `file', or if type is the
5981 universal prefix '(4), or if the cursor is before the first headline
5982 in the file. Otherwise, restriction will be to the current subtree."
5983 (interactive "P")
5984 (and (equal type '(4)) (setq type 'file))
5985 (setq type (cond
5986 (type type)
5987 ((org-at-heading-p) 'subtree)
5988 ((condition-case nil (org-back-to-heading t) (error nil))
5989 'subtree)
5990 (t 'file)))
5991 (if (eq type 'subtree)
5992 (progn
5993 (setq org-agenda-restrict t)
5994 (setq org-agenda-overriding-restriction 'subtree)
5995 (put 'org-agenda-files 'org-restrict
5996 (list (buffer-file-name (buffer-base-buffer))))
5997 (org-back-to-heading t)
5998 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5999 (move-marker org-agenda-restrict-begin (point))
6000 (move-marker org-agenda-restrict-end
6001 (save-excursion (org-end-of-subtree t)))
6002 (message "Locking agenda restriction to subtree"))
6003 (put 'org-agenda-files 'org-restrict
6004 (list (buffer-file-name (buffer-base-buffer))))
6005 (setq org-agenda-restrict nil)
6006 (setq org-agenda-overriding-restriction 'file)
6007 (move-marker org-agenda-restrict-begin nil)
6008 (move-marker org-agenda-restrict-end nil)
6009 (message "Locking agenda restriction to file"))
6010 (setq current-prefix-arg nil)
6011 (org-agenda-maybe-redo))
6013 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6014 "Remove the agenda restriction lock."
6015 (interactive "P")
6016 (org-detach-overlay org-agenda-restriction-lock-overlay)
6017 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6018 (setq org-agenda-overriding-restriction nil)
6019 (setq org-agenda-restrict nil)
6020 (put 'org-agenda-files 'org-restrict nil)
6021 (move-marker org-agenda-restrict-begin nil)
6022 (move-marker org-agenda-restrict-end nil)
6023 (setq current-prefix-arg nil)
6024 (message "Agenda restriction lock removed")
6025 (or noupdate (org-agenda-maybe-redo)))
6027 (defun org-agenda-maybe-redo ()
6028 "If there is any window showing the agenda view, update it."
6029 (let ((w (get-buffer-window org-agenda-buffer-name t))
6030 (w0 (selected-window)))
6031 (when w
6032 (select-window w)
6033 (org-agenda-redo)
6034 (select-window w0)
6035 (if org-agenda-overriding-restriction
6036 (message "Agenda view shifted to new %s restriction"
6037 org-agenda-overriding-restriction)
6038 (message "Agenda restriction lock removed")))))
6040 ;;; Agenda commands
6042 (defun org-agenda-check-type (error &rest types)
6043 "Check if agenda buffer is of allowed type.
6044 If ERROR is non-nil, throw an error, otherwise just return nil."
6045 (if (memq org-agenda-type types)
6047 (if error
6048 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6049 nil)))
6051 (defun org-agenda-quit ()
6052 "Exit agenda by removing the window or the buffer."
6053 (interactive)
6054 (if org-agenda-columns-active
6055 (org-columns-quit)
6056 (let ((buf (current-buffer)))
6057 (if (eq org-agenda-window-setup 'other-frame)
6058 (progn
6059 (kill-buffer buf)
6060 (org-agenda-reset-markers)
6061 (org-columns-remove-overlays)
6062 (setq org-agenda-archives-mode nil)
6063 (delete-frame))
6064 (and (not (eq org-agenda-window-setup 'current-window))
6065 (not (one-window-p))
6066 (delete-window))
6067 (kill-buffer buf)
6068 (org-agenda-reset-markers)
6069 (org-columns-remove-overlays)
6070 (setq org-agenda-archives-mode nil)))
6071 ;; Maybe restore the pre-agenda window configuration.
6072 (and org-agenda-restore-windows-after-quit
6073 (not (eq org-agenda-window-setup 'other-frame))
6074 org-pre-agenda-window-conf
6075 (set-window-configuration org-pre-agenda-window-conf))))
6077 (defun org-agenda-exit ()
6078 "Exit agenda by removing the window or the buffer.
6079 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6080 Org-mode buffers visited directly by the user will not be touched."
6081 (interactive)
6082 (org-release-buffers org-agenda-new-buffers)
6083 (setq org-agenda-new-buffers nil)
6084 (org-agenda-quit))
6086 (defun org-agenda-execute (arg)
6087 "Execute another agenda command, keeping same window.
6088 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6089 in the agenda."
6090 (interactive "P")
6091 (let ((org-agenda-window-setup 'current-window))
6092 (org-agenda arg)))
6094 (defun org-agenda-redo ()
6095 "Rebuild Agenda.
6096 When this is the global TODO list, a prefix argument will be interpreted."
6097 (interactive)
6098 (let* ((org-agenda-keep-modes t)
6099 (filter org-agenda-filter)
6100 (preset (get 'org-agenda-filter :preset-filter))
6101 (org-agenda-filter-while-redo (or filter preset))
6102 (cols org-agenda-columns-active)
6103 (line (org-current-line))
6104 (window-line (- line (org-current-line (window-start))))
6105 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6106 (put 'org-agenda-filter :preset-filter nil)
6107 (and cols (org-columns-quit))
6108 (message "Rebuilding agenda buffer...")
6109 (org-let lprops '(eval org-agenda-redo-command))
6110 (setq org-agenda-undo-list nil
6111 org-agenda-pending-undo-list nil)
6112 (message "Rebuilding agenda buffer...done")
6113 (put 'org-agenda-filter :preset-filter preset)
6114 (and (or filter preset) (org-agenda-filter-apply filter))
6115 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6116 (org-goto-line line)
6117 (recenter window-line)))
6120 (defvar org-global-tags-completion-table nil)
6121 (defvar org-agenda-filter-form nil)
6122 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6123 "Keep only those lines in the agenda buffer that have a specific tag.
6124 The tag is selected with its fast selection letter, as configured.
6125 With prefix argument STRIP, remove all lines that do have the tag.
6126 A lisp caller can specify CHAR. NARROW means that the new tag should be
6127 used to narrow the search - the interactive user can also press `-' or `+'
6128 to switch to narrowing."
6129 (interactive "P")
6130 (let* ((alist org-tag-alist-for-agenda)
6131 (tag-chars (mapconcat
6132 (lambda (x) (if (and (not (symbolp (car x)))
6133 (cdr x))
6134 (char-to-string (cdr x))
6135 ""))
6136 alist ""))
6137 (efforts (org-split-string
6138 (or (cdr (assoc (concat org-effort-property "_ALL")
6139 org-global-properties))
6140 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6141 "")))
6142 (effort-op org-agenda-filter-effort-default-operator)
6143 (effort-prompt "")
6144 (inhibit-read-only t)
6145 (current org-agenda-filter)
6146 maybe-refresh a n tag)
6147 (unless char
6148 (message
6149 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6150 (if narrow "Narrow" "Filter") tag-chars
6151 (if org-agenda-auto-exclude-function "[RET], " ""))
6152 (setq char (read-char)))
6153 (when (member char '(?+ ?-))
6154 ;; Narrowing down
6155 (cond ((equal char ?-) (setq strip t narrow t))
6156 ((equal char ?+) (setq strip nil narrow t)))
6157 (message
6158 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6159 (setq char (read-char)))
6160 (when (member char '(?< ?> ?= ??))
6161 ;; An effort operator
6162 (setq effort-op (char-to-string char))
6163 (setq alist nil) ; to make sure it will be interpreted as effort.
6164 (unless (equal char ??)
6165 (loop for i from 0 to 9 do
6166 (setq effort-prompt
6167 (concat
6168 effort-prompt " ["
6169 (if (= i 9) "0" (int-to-string (1+ i)))
6170 "]" (nth i efforts))))
6171 (message "Effort%s: %s " effort-op effort-prompt)
6172 (setq char (read-char))
6173 (when (or (< char ?0) (> char ?9))
6174 (error "Need 1-9,0 to select effort" ))))
6175 (when (equal char ?\t)
6176 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6177 (org-set-local 'org-global-tags-completion-table
6178 (org-global-tags-completion-table)))
6179 (let ((completion-ignore-case t))
6180 (setq tag (org-icompleting-read
6181 "Tag: " org-global-tags-completion-table))))
6182 (cond
6183 ((equal char ?\r)
6184 (org-agenda-filter-by-tag-show-all)
6185 (when org-agenda-auto-exclude-function
6186 (setq org-agenda-filter '())
6187 (dolist (tag (org-agenda-get-represented-tags))
6188 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6189 (if modifier
6190 (push modifier org-agenda-filter))))
6191 (if (not (null org-agenda-filter))
6192 (org-agenda-filter-apply org-agenda-filter)))
6193 (setq maybe-refresh t))
6194 ((equal char ?/)
6195 (org-agenda-filter-by-tag-show-all)
6196 (when (get 'org-agenda-filter :preset-filter)
6197 (org-agenda-filter-apply org-agenda-filter))
6198 (setq maybe-refresh t))
6199 ((or (equal char ?\ )
6200 (setq a (rassoc char alist))
6201 (and (>= char ?0) (<= char ?9)
6202 (setq n (if (= char ?0) 9 (- char ?0 1))
6203 tag (concat effort-op (nth n efforts))
6204 a (cons tag nil)))
6205 (and (= char ??)
6206 (setq tag "?eff")
6207 a (cons tag nil))
6208 (and tag (setq a (cons tag nil))))
6209 (org-agenda-filter-by-tag-show-all)
6210 (setq tag (car a))
6211 (setq org-agenda-filter
6212 (cons (concat (if strip "-" "+") tag)
6213 (if narrow current nil)))
6214 (org-agenda-filter-apply org-agenda-filter)
6215 (setq maybe-refresh t))
6216 (t (error "Invalid tag selection character %c" char)))
6217 (when (and maybe-refresh
6218 (eq org-agenda-clockreport-mode 'with-filter))
6219 (org-agenda-redo))))
6221 (defun org-agenda-get-represented-tags ()
6222 "Get a list of all tags currently represented in the agenda."
6223 (let (p tags)
6224 (save-excursion
6225 (goto-char (point-min))
6226 (while (setq p (next-single-property-change (point) 'tags))
6227 (goto-char p)
6228 (mapc (lambda (x) (add-to-list 'tags x))
6229 (get-text-property (point) 'tags))))
6230 tags))
6232 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6233 "Refine the current filter. See `org-agenda-filter-by-tag'."
6234 (interactive "P")
6235 (org-agenda-filter-by-tag strip char 'refine))
6237 (defun org-agenda-filter-make-matcher ()
6238 "Create the form that tests a line for the agenda filter."
6239 (let (f f1)
6240 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6241 org-agenda-filter))
6242 (if (member x '("-" "+"))
6243 (setq f1 (if (equal x "-") 'tags '(not tags)))
6244 (if (string-match "[<=>?]" x)
6245 (setq f1 (org-agenda-filter-effort-form x))
6246 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6247 (if (equal (string-to-char x) ?-)
6248 (setq f1 (list 'not f1))))
6249 (push f1 f))
6250 (cons 'and (nreverse f))))
6252 (defun org-agenda-filter-effort-form (e)
6253 "Return the form to compare the effort of the current line with what E says.
6254 E looks like \"+<2:25\"."
6255 (let (op)
6256 (setq e (substring e 1))
6257 (setq op (string-to-char e) e (substring e 1))
6258 (setq op (cond ((equal op ?<) '<=)
6259 ((equal op ?>) '>=)
6260 ((equal op ??) op)
6261 (t '=)))
6262 (list 'org-agenda-compare-effort (list 'quote op)
6263 (org-duration-string-to-minutes e))))
6265 (defun org-agenda-compare-effort (op value)
6266 "Compare the effort of the current line with VALUE, using OP.
6267 If the line does not have an effort defined, return nil."
6268 (let ((eff (org-get-at-bol 'effort-minutes)))
6269 (if (equal op ??)
6270 (not eff)
6271 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6272 value))))
6274 (defun org-agenda-filter-apply (filter)
6275 "Set FILTER as the new agenda filter and apply it."
6276 (let (tags)
6277 (setq org-agenda-filter filter
6278 org-agenda-filter-form (org-agenda-filter-make-matcher))
6279 (org-agenda-set-mode-name)
6280 (save-excursion
6281 (goto-char (point-min))
6282 (while (not (eobp))
6283 (if (org-get-at-bol 'org-marker)
6284 (progn
6285 (setq tags (org-get-at-bol 'tags)) ; used in eval
6286 (if (not (eval org-agenda-filter-form))
6287 (org-agenda-filter-by-tag-hide-line))
6288 (beginning-of-line 2))
6289 (beginning-of-line 2))))
6290 (if (get-char-property (point) 'invisible)
6291 (org-agenda-previous-line))))
6293 (defun org-agenda-filter-by-tag-hide-line ()
6294 (let (ov)
6295 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6296 (point-at-eol)))
6297 (overlay-put ov 'invisible t)
6298 (overlay-put ov 'type 'tags-filter)
6299 (push ov org-agenda-filter-overlays)))
6301 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6302 (setq pos (or pos (point)))
6303 (save-excursion
6304 (dolist (ov (overlays-at pos))
6305 (when (and (overlay-get ov 'invisible)
6306 (eq (overlay-get ov 'type) 'tags-filter))
6307 (goto-char pos)
6308 (if (< (overlay-start ov) (point-at-eol))
6309 (move-overlay ov (point-at-eol)
6310 (overlay-end ov)))))))
6312 (defun org-agenda-filter-by-tag-show-all ()
6313 (mapc 'delete-overlay org-agenda-filter-overlays)
6314 (setq org-agenda-filter-overlays nil)
6315 (setq org-agenda-filter nil)
6316 (setq org-agenda-filter-form nil)
6317 (org-agenda-set-mode-name))
6319 (defun org-agenda-manipulate-query-add ()
6320 "Manipulate the query by adding a search term with positive selection.
6321 Positive selection means the term must be matched for selection of an entry."
6322 (interactive)
6323 (org-agenda-manipulate-query ?\[))
6324 (defun org-agenda-manipulate-query-subtract ()
6325 "Manipulate the query by adding a search term with negative selection.
6326 Negative selection means term must not be matched for selection of an entry."
6327 (interactive)
6328 (org-agenda-manipulate-query ?\]))
6329 (defun org-agenda-manipulate-query-add-re ()
6330 "Manipulate the query by adding a search regexp with positive selection.
6331 Positive selection means the regexp must match for selection of an entry."
6332 (interactive)
6333 (org-agenda-manipulate-query ?\{))
6334 (defun org-agenda-manipulate-query-subtract-re ()
6335 "Manipulate the query by adding a search regexp with negative selection.
6336 Negative selection means regexp must not match for selection of an entry."
6337 (interactive)
6338 (org-agenda-manipulate-query ?\}))
6339 (defun org-agenda-manipulate-query (char)
6340 (cond
6341 ((memq org-agenda-type '(timeline agenda))
6342 (let ((org-agenda-include-inactive-timestamps t))
6343 (org-agenda-redo))
6344 (message "Display now includes inactive timestamps as well"))
6345 ((eq org-agenda-type 'search)
6346 (org-add-to-string
6347 'org-agenda-query-string
6348 (if org-agenda-last-search-view-search-was-boolean
6349 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6350 (?\{ . " +{}") (?\} . " -{}"))))
6351 " "))
6352 (setq org-agenda-redo-command
6353 (list 'org-search-view
6354 org-todo-only
6355 org-agenda-query-string
6356 (+ (length org-agenda-query-string)
6357 (if (member char '(?\{ ?\})) 0 1))))
6358 (set-register org-agenda-query-register org-agenda-query-string)
6359 (org-agenda-redo))
6360 (t (error "Cannot manipulate query for %s-type agenda buffers"
6361 org-agenda-type))))
6363 (defun org-add-to-string (var string)
6364 (set var (concat (symbol-value var) string)))
6366 (defun org-agenda-goto-date (date)
6367 "Jump to DATE in agenda."
6368 (interactive (list (let ((org-read-date-prefer-future
6369 (eval org-agenda-jump-prefer-future)))
6370 (org-read-date))))
6371 (org-agenda-list nil date))
6373 (defun org-agenda-goto-today ()
6374 "Go to today."
6375 (interactive)
6376 (org-agenda-check-type t 'timeline 'agenda)
6377 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6378 (cond
6379 (tdpos (goto-char tdpos))
6380 ((eq org-agenda-type 'agenda)
6381 (let* ((sd (org-agenda-compute-starting-span
6382 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6383 (org-agenda-overriding-arguments org-agenda-last-arguments))
6384 (setf (nth 1 org-agenda-overriding-arguments) sd)
6385 (org-agenda-redo)
6386 (org-agenda-find-same-or-today-or-agenda)))
6387 (t (error "Cannot find today")))))
6389 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6390 (goto-char
6391 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6392 (text-property-any (point-min) (point-max) 'org-today t)
6393 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6394 (point-min))))
6396 (defun org-agenda-later (arg)
6397 "Go forward in time by thee current span.
6398 With prefix ARG, go forward that many times the current span."
6399 (interactive "p")
6400 (org-agenda-check-type t 'agenda)
6401 (let* ((span org-agenda-current-span)
6402 (sd org-starting-day)
6403 (greg (calendar-gregorian-from-absolute sd))
6404 (cnt (org-get-at-bol 'org-day-cnt))
6405 greg2)
6406 (cond
6407 ((eq span 'day)
6408 (setq sd (+ arg sd)))
6409 ((eq span 'week)
6410 (setq sd (+ (* 7 arg) sd)))
6411 ((eq span 'month)
6412 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6413 sd (calendar-absolute-from-gregorian greg2))
6414 (setcar greg2 (1+ (car greg2))))
6415 ((eq span 'year)
6416 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6417 sd (calendar-absolute-from-gregorian greg2))
6418 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6420 (setq sd (+ (* span arg) sd))))
6421 (let ((org-agenda-overriding-arguments
6422 (list (car org-agenda-last-arguments) sd span t)))
6423 (org-agenda-redo)
6424 (org-agenda-find-same-or-today-or-agenda cnt))))
6426 (defun org-agenda-earlier (arg)
6427 "Go backward in time by the current span.
6428 With prefix ARG, go backward that many times the current span."
6429 (interactive "p")
6430 (org-agenda-later (- arg)))
6432 (defun org-agenda-view-mode-dispatch ()
6433 "Call one of the view mode commands."
6434 (interactive)
6435 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6436 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6437 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6438 [E]ntryText")
6439 (let ((a (read-char-exclusive)))
6440 (case a
6441 (?\ (call-interactively 'org-agenda-reset-view))
6442 (?d (call-interactively 'org-agenda-day-view))
6443 (?w (call-interactively 'org-agenda-week-view))
6444 (?m (call-interactively 'org-agenda-month-view))
6445 (?y (call-interactively 'org-agenda-year-view))
6446 (?l (call-interactively 'org-agenda-log-mode))
6447 (?L (org-agenda-log-mode '(4)))
6448 (?c (org-agenda-log-mode 'clockcheck))
6449 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6450 (?a (call-interactively 'org-agenda-archives-mode))
6451 (?A (org-agenda-archives-mode 'files))
6452 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6453 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6454 (?G (call-interactively 'org-agenda-toggle-time-grid))
6455 (?D (call-interactively 'org-agenda-toggle-diary))
6456 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6457 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6458 (org-agenda-check-type t 'timeline 'agenda)
6459 (org-agenda-redo))
6460 (message "Display now includes inactive timestamps as well"))
6461 (?q (message "Abort"))
6462 (otherwise (error "Invalid key" )))))
6464 (defun org-agenda-reset-view ()
6465 "Switch to default view for agenda."
6466 (interactive)
6467 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6468 (defun org-agenda-day-view (&optional day-of-year)
6469 "Switch to daily view for agenda.
6470 With argument DAY-OF-YEAR, switch to that day of the year."
6471 (interactive "P")
6472 (org-agenda-change-time-span 'day day-of-year))
6473 (defun org-agenda-week-view (&optional iso-week)
6474 "Switch to daily view for agenda.
6475 With argument ISO-WEEK, switch to the corresponding ISO week.
6476 If ISO-WEEK has more then 2 digits, only the last two encode the
6477 week. Any digits before this encode a year. So 200712 means
6478 week 12 of year 2007. Years in the range 1938-2037 can also be
6479 written as 2-digit years."
6480 (interactive "P")
6481 (org-agenda-change-time-span 'week iso-week))
6482 (defun org-agenda-month-view (&optional month)
6483 "Switch to monthly view for agenda.
6484 With argument MONTH, switch to that month."
6485 (interactive "P")
6486 (org-agenda-change-time-span 'month month))
6487 (defun org-agenda-year-view (&optional year)
6488 "Switch to yearly view for agenda.
6489 With argument YEAR, switch to that year.
6490 If MONTH has more then 2 digits, only the last two encode the
6491 month. Any digits before this encode a year. So 200712 means
6492 December year 2007. Years in the range 1938-2037 can also be
6493 written as 2-digit years."
6494 (interactive "P")
6495 (when year
6496 (setq year (org-small-year-to-year year)))
6497 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6498 (org-agenda-change-time-span 'year year)
6499 (error "Abort")))
6501 (defun org-agenda-change-time-span (span &optional n)
6502 "Change the agenda view to SPAN.
6503 SPAN may be `day', `week', `month', `year'."
6504 (org-agenda-check-type t 'agenda)
6505 (if (and (not n) (equal org-agenda-current-span span))
6506 (error "Viewing span is already \"%s\"" span))
6507 (let* ((sd (or (org-get-at-bol 'day)
6508 org-starting-day))
6509 (sd (org-agenda-compute-starting-span sd span n))
6510 (org-agenda-overriding-arguments
6511 (or org-agenda-overriding-arguments
6512 (list (car org-agenda-last-arguments) sd span t))))
6513 (org-agenda-redo)
6514 (org-agenda-find-same-or-today-or-agenda))
6515 (org-agenda-set-mode-name)
6516 (message "Switched to %s view" span))
6518 (defun org-agenda-compute-starting-span (sd span &optional n)
6519 "Compute starting date for agenda.
6520 SPAN may be `day', `week', `month', `year'. The return value
6521 is a cons cell with the starting date and the number of days,
6522 so that the date SD will be in that range."
6523 (let* ((greg (calendar-gregorian-from-absolute sd))
6524 (dg (nth 1 greg))
6525 (mg (car greg))
6526 (yg (nth 2 greg)))
6527 (cond
6528 ((eq span 'day)
6529 (when n
6530 (setq sd (+ (calendar-absolute-from-gregorian
6531 (list mg 1 yg))
6532 n -1))))
6533 ((eq span 'week)
6534 (let* ((nt (calendar-day-of-week
6535 (calendar-gregorian-from-absolute sd)))
6536 (d (if org-agenda-start-on-weekday
6537 (- nt org-agenda-start-on-weekday)
6540 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6541 (when n
6542 (require 'cal-iso)
6543 (when (> n 99)
6544 (setq y1 (org-small-year-to-year (/ n 100))
6545 n (mod n 100)))
6546 (setq sd
6547 (calendar-absolute-from-iso
6548 (list n 1
6549 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6550 ((eq span 'month)
6551 (let (y1)
6552 (when (and n (> n 99))
6553 (setq y1 (org-small-year-to-year (/ n 100))
6554 n (mod n 100)))
6555 (setq sd (calendar-absolute-from-gregorian
6556 (list (or n mg) 1 (or y1 yg))))))
6557 ((eq span 'year)
6558 (setq sd (calendar-absolute-from-gregorian
6559 (list 1 1 (or n yg))))))
6560 sd))
6562 (defun org-agenda-next-date-line (&optional arg)
6563 "Jump to the next line indicating a date in agenda buffer."
6564 (interactive "p")
6565 (org-agenda-check-type t 'agenda 'timeline)
6566 (beginning-of-line 1)
6567 ;; This does not work if user makes date format that starts with a blank
6568 (if (looking-at "^\\S-") (forward-char 1))
6569 (if (not (re-search-forward "^\\S-" nil t arg))
6570 (progn
6571 (backward-char 1)
6572 (error "No next date after this line in this buffer")))
6573 (goto-char (match-beginning 0)))
6575 (defun org-agenda-previous-date-line (&optional arg)
6576 "Jump to the previous line indicating a date in agenda buffer."
6577 (interactive "p")
6578 (org-agenda-check-type t 'agenda 'timeline)
6579 (beginning-of-line 1)
6580 (if (not (re-search-backward "^\\S-" nil t arg))
6581 (error "No previous date before this line in this buffer")))
6583 ;; Initialize the highlight
6584 (defvar org-hl (make-overlay 1 1))
6585 (overlay-put org-hl 'face 'highlight)
6587 (defun org-highlight (begin end &optional buffer)
6588 "Highlight a region with overlay."
6589 (move-overlay org-hl begin end (or buffer (current-buffer))))
6591 (defun org-unhighlight ()
6592 "Detach overlay INDEX."
6593 (org-detach-overlay org-hl))
6595 ;; FIXME this is currently not used.
6596 (defun org-highlight-until-next-command (beg end &optional buffer)
6597 "Move the highlight overlay to BEG/END, remove it before the next command."
6598 (org-highlight beg end buffer)
6599 (add-hook 'pre-command-hook 'org-unhighlight-once))
6600 (defun org-unhighlight-once ()
6601 "Remove the highlight from its position, and this function from the hook."
6602 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6603 (org-unhighlight))
6605 (defun org-agenda-follow-mode ()
6606 "Toggle follow mode in an agenda buffer."
6607 (interactive)
6608 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6609 (org-agenda-set-mode-name)
6610 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6611 (org-agenda-show))
6612 (message "Follow mode is %s"
6613 (if org-agenda-follow-mode "on" "off")))
6615 (defun org-agenda-entry-text-mode (&optional arg)
6616 "Toggle entry text mode in an agenda buffer."
6617 (interactive "P")
6618 (setq org-agenda-entry-text-mode (or (integerp arg)
6619 (not org-agenda-entry-text-mode)))
6620 (org-agenda-entry-text-hide)
6621 (and org-agenda-entry-text-mode
6622 (let ((org-agenda-entry-text-maxlines
6623 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6624 (org-agenda-entry-text-show)))
6625 (org-agenda-set-mode-name)
6626 (message "Entry text mode is %s. Maximum number of lines is %d"
6627 (if org-agenda-entry-text-mode "on" "off")
6628 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6630 (defun org-agenda-clockreport-mode (&optional with-filter)
6631 "Toggle clocktable mode in an agenda buffer.
6632 With prefix arg WITH-FILTER, make the clocktable respect the current
6633 agenda filter."
6634 (interactive "P")
6635 (org-agenda-check-type t 'agenda)
6636 (if with-filter
6637 (setq org-agenda-clockreport-mode 'with-filter)
6638 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6639 (org-agenda-set-mode-name)
6640 (org-agenda-redo)
6641 (message "Clocktable mode is %s"
6642 (if org-agenda-clockreport-mode "on" "off")))
6644 (defun org-agenda-log-mode (&optional special)
6645 "Toggle log mode in an agenda buffer.
6646 With argument SPECIAL, show all possible log items, not only the ones
6647 configured in `org-agenda-log-mode-items'.
6648 With a double `C-u' prefix arg, show *only* log items, nothing else."
6649 (interactive "P")
6650 (org-agenda-check-type t 'agenda 'timeline)
6651 (setq org-agenda-show-log
6652 (cond
6653 ((equal special '(16)) 'only)
6654 ((eq special 'clockcheck)
6655 (if (eq org-agenda-show-log 'clockcheck)
6656 nil 'clockcheck))
6657 (special '(closed clock state))
6658 (t (not org-agenda-show-log))))
6659 (org-agenda-set-mode-name)
6660 (org-agenda-redo)
6661 (message "Log mode is %s"
6662 (if org-agenda-show-log "on" "off")))
6664 (defun org-agenda-archives-mode (&optional with-files)
6665 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6666 When called with a prefix argument, include all archive files as well."
6667 (interactive "P")
6668 (setq org-agenda-archives-mode
6669 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6670 (org-agenda-set-mode-name)
6671 (org-agenda-redo)
6672 (message
6673 "%s"
6674 (cond
6675 ((eq org-agenda-archives-mode nil)
6676 "No archives are included")
6677 ((eq org-agenda-archives-mode 'trees)
6678 (format "Trees with :%s: tag are included" org-archive-tag))
6679 ((eq org-agenda-archives-mode t)
6680 (format "Trees with :%s: tag and all active archive files are included"
6681 org-archive-tag)))))
6683 (defun org-agenda-toggle-diary ()
6684 "Toggle diary inclusion in an agenda buffer."
6685 (interactive)
6686 (org-agenda-check-type t 'agenda)
6687 (setq org-agenda-include-diary (not org-agenda-include-diary))
6688 (org-agenda-redo)
6689 (org-agenda-set-mode-name)
6690 (message "Diary inclusion turned %s"
6691 (if org-agenda-include-diary "on" "off")))
6693 (defun org-agenda-toggle-deadlines ()
6694 "Toggle inclusion of entries with a deadline in an agenda buffer."
6695 (interactive)
6696 (org-agenda-check-type t 'agenda)
6697 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6698 (org-agenda-redo)
6699 (org-agenda-set-mode-name)
6700 (message "Deadlines inclusion turned %s"
6701 (if org-agenda-include-deadlines "on" "off")))
6703 (defun org-agenda-toggle-time-grid ()
6704 "Toggle time grid in an agenda buffer."
6705 (interactive)
6706 (org-agenda-check-type t 'agenda)
6707 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6708 (org-agenda-redo)
6709 (org-agenda-set-mode-name)
6710 (message "Time-grid turned %s"
6711 (if org-agenda-use-time-grid "on" "off")))
6713 (defun org-agenda-set-mode-name ()
6714 "Set the mode name to indicate all the small mode settings."
6715 (setq mode-name
6716 (list "Org-Agenda"
6717 (if (get 'org-agenda-files 'org-restrict) " []" "")
6719 '(:eval (org-agenda-span-name org-agenda-current-span))
6720 (if org-agenda-follow-mode " Follow" "")
6721 (if org-agenda-entry-text-mode " ETxt" "")
6722 (if org-agenda-include-diary " Diary" "")
6723 (if org-agenda-include-deadlines " Ddl" "")
6724 (if org-agenda-use-time-grid " Grid" "")
6725 (if (and (boundp 'org-habit-show-habits)
6726 org-habit-show-habits) " Habit" "")
6727 (cond
6728 ((consp org-agenda-show-log) " LogAll")
6729 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6730 (org-agenda-show-log " Log")
6731 (t ""))
6732 (if (or org-agenda-filter (get 'org-agenda-filter
6733 :preset-filter))
6734 (concat " {" (mapconcat
6735 'identity
6736 (append (get 'org-agenda-filter
6737 :preset-filter)
6738 org-agenda-filter) "") "}")
6740 (if org-agenda-archives-mode
6741 (if (eq org-agenda-archives-mode t)
6742 " Archives"
6743 (format " :%s:" org-archive-tag))
6745 (if org-agenda-clockreport-mode
6746 (if (eq org-agenda-clockreport-mode 'with-filter)
6747 " Clock{}" " Clock")
6748 "")))
6749 (force-mode-line-update))
6751 (defun org-agenda-post-command-hook ()
6752 (setq org-agenda-type
6753 (or (get-text-property (point) 'org-agenda-type)
6754 (get-text-property (max (point-min) (1- (point)))
6755 'org-agenda-type))))
6757 (defun org-agenda-next-line ()
6758 "Move cursor to the next line, and show if follow mode is active."
6759 (interactive)
6760 (call-interactively 'next-line)
6761 (org-agenda-do-context-action))
6763 (defun org-agenda-previous-line ()
6764 "Move cursor to the previous line, and show if follow-mode is active."
6765 (interactive)
6766 (call-interactively 'previous-line)
6767 (org-agenda-do-context-action))
6769 (defun org-agenda-do-context-action ()
6770 "Show outline path and, maybe, follow mode window."
6771 (let ((m (org-get-at-bol 'org-marker)))
6772 (if (and org-agenda-follow-mode m)
6773 (org-agenda-show))
6774 (if (and m org-agenda-show-outline-path)
6775 (org-with-point-at m
6776 (org-display-outline-path t)))))
6778 (defun org-agenda-show-priority ()
6779 "Show the priority of the current item.
6780 This priority is composed of the main priority given with the [#A] cookies,
6781 and by additional input from the age of a schedules or deadline entry."
6782 (interactive)
6783 (let* ((pri (org-get-at-bol 'priority)))
6784 (message "Priority is %d" (if pri pri -1000))))
6786 (defun org-agenda-show-tags ()
6787 "Show the tags applicable to the current item."
6788 (interactive)
6789 (let* ((tags (org-get-at-bol 'tags)))
6790 (if tags
6791 (message "Tags are :%s:"
6792 (org-no-properties (mapconcat 'identity tags ":")))
6793 (message "No tags associated with this line"))))
6795 (defun org-agenda-goto (&optional highlight)
6796 "Go to the Org-mode file which contains the item at point."
6797 (interactive)
6798 (let* ((marker (or (org-get-at-bol 'org-marker)
6799 (org-agenda-error)))
6800 (buffer (marker-buffer marker))
6801 (pos (marker-position marker)))
6802 (switch-to-buffer-other-window buffer)
6803 (widen)
6804 (push-mark)
6805 (goto-char pos)
6806 (when (org-mode-p)
6807 (org-show-context 'agenda)
6808 (save-excursion
6809 (and (outline-next-heading)
6810 (org-flag-heading nil)))) ; show the next heading
6811 (when (outline-invisible-p)
6812 (show-entry)) ; display invisible text
6813 (recenter (/ (window-height) 2))
6814 (run-hooks 'org-agenda-after-show-hook)
6815 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6817 (defvar org-agenda-after-show-hook nil
6818 "Normal hook run after an item has been shown from the agenda.
6819 Point is in the buffer where the item originated.")
6821 (defun org-agenda-kill ()
6822 "Kill the entry or subtree belonging to the current agenda entry."
6823 (interactive)
6824 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6825 (let* ((marker (or (org-get-at-bol 'org-marker)
6826 (org-agenda-error)))
6827 (buffer (marker-buffer marker))
6828 (pos (marker-position marker))
6829 (type (org-get-at-bol 'type))
6830 dbeg dend (n 0) conf)
6831 (org-with-remote-undo buffer
6832 (with-current-buffer buffer
6833 (save-excursion
6834 (goto-char pos)
6835 (if (and (org-mode-p) (not (member type '("sexp"))))
6836 (setq dbeg (progn (org-back-to-heading t) (point))
6837 dend (org-end-of-subtree t t))
6838 (setq dbeg (point-at-bol)
6839 dend (min (point-max) (1+ (point-at-eol)))))
6840 (goto-char dbeg)
6841 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6842 (setq conf (or (eq t org-agenda-confirm-kill)
6843 (and (numberp org-agenda-confirm-kill)
6844 (> n org-agenda-confirm-kill))))
6845 (and conf
6846 (not (y-or-n-p
6847 (format "Delete entry with %d lines in buffer \"%s\"? "
6848 n (buffer-name buffer))))
6849 (error "Abort"))
6850 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6851 (with-current-buffer buffer (delete-region dbeg dend))
6852 (message "Agenda item and source killed"))))
6854 (defvar org-archive-default-command)
6855 (defun org-agenda-archive-default ()
6856 "Archive the entry or subtree belonging to the current agenda entry."
6857 (interactive)
6858 (require 'org-archive)
6859 (org-agenda-archive-with org-archive-default-command))
6861 (defun org-agenda-archive-default-with-confirmation ()
6862 "Archive the entry or subtree belonging to the current agenda entry."
6863 (interactive)
6864 (require 'org-archive)
6865 (org-agenda-archive-with org-archive-default-command 'confirm))
6867 (defun org-agenda-archive ()
6868 "Archive the entry or subtree belonging to the current agenda entry."
6869 (interactive)
6870 (org-agenda-archive-with 'org-archive-subtree))
6872 (defun org-agenda-archive-to-archive-sibling ()
6873 "Move the entry to the archive sibling."
6874 (interactive)
6875 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6877 (defun org-agenda-archive-with (cmd &optional confirm)
6878 "Move the entry to the archive sibling."
6879 (interactive)
6880 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6881 (let* ((marker (or (org-get-at-bol 'org-marker)
6882 (org-agenda-error)))
6883 (buffer (marker-buffer marker))
6884 (pos (marker-position marker)))
6885 (org-with-remote-undo buffer
6886 (with-current-buffer buffer
6887 (if (org-mode-p)
6888 (if (and confirm
6889 (not (y-or-n-p "Archive this subtree or entry? ")))
6890 (error "Abort")
6891 (save-excursion
6892 (goto-char pos)
6893 (org-remove-subtree-entries-from-agenda)
6894 (org-back-to-heading t)
6895 (funcall cmd)))
6896 (error "Archiving works only in Org-mode files"))))))
6898 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6899 "Remove all lines in the agenda that correspond to a given subtree.
6900 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6901 If this information is not given, the function uses the tree at point."
6902 (let ((buf (or buf (current-buffer))) m p)
6903 (save-excursion
6904 (unless (and beg end)
6905 (org-back-to-heading t)
6906 (setq beg (point))
6907 (org-end-of-subtree t)
6908 (setq end (point)))
6909 (set-buffer (get-buffer org-agenda-buffer-name))
6910 (save-excursion
6911 (goto-char (point-max))
6912 (beginning-of-line 1)
6913 (while (not (bobp))
6914 (when (and (setq m (org-get-at-bol 'org-marker))
6915 (equal buf (marker-buffer m))
6916 (setq p (marker-position m))
6917 (>= p beg)
6918 (< p end))
6919 (let ((inhibit-read-only t))
6920 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6921 (beginning-of-line 0))))))
6923 (defun org-agenda-refile (&optional goto rfloc no-update)
6924 "Refile the item at point."
6925 (interactive "P")
6926 (if (equal goto '(16))
6927 (org-refile-goto-last-stored)
6928 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6929 (org-agenda-error)))
6930 (buffer (marker-buffer marker))
6931 (pos (marker-position marker))
6932 (rfloc (or rfloc
6933 (org-refile-get-location
6934 (if goto "Goto" "Refile to") buffer
6935 org-refile-allow-creating-parent-nodes))))
6936 (with-current-buffer buffer
6937 (save-excursion
6938 (save-restriction
6939 (widen)
6940 (goto-char marker)
6941 (org-remove-subtree-entries-from-agenda)
6942 (org-refile goto buffer rfloc)))))
6943 (unless no-update (org-agenda-redo))))
6945 (defun org-agenda-open-link (&optional arg)
6946 "Follow the link in the current line, if any.
6947 This looks for a link in the displayed line in the agenda. It also looks
6948 at the text of the entry itself."
6949 (interactive "P")
6950 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6951 (org-get-at-bol 'org-marker)))
6952 (buffer (and marker (marker-buffer marker)))
6953 (prefix (buffer-substring
6954 (point-at-bol) (point-at-eol))))
6955 (cond
6956 (buffer
6957 (with-current-buffer buffer
6958 (save-excursion
6959 (save-restriction
6960 (widen)
6961 (goto-char marker)
6962 (org-offer-links-in-entry arg prefix)))))
6963 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6964 (save-excursion
6965 (beginning-of-line 1)
6966 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6967 (org-open-link-from-string (match-string 1)))
6968 (t (error "No link to open here")))))
6970 (defun org-agenda-copy-local-variable (var)
6971 "Get a variable from a referenced buffer and install it here."
6972 (let ((m (org-get-at-bol 'org-marker)))
6973 (when (and m (buffer-live-p (marker-buffer m)))
6974 (org-set-local var (with-current-buffer (marker-buffer m)
6975 (symbol-value var))))))
6977 (defun org-agenda-switch-to (&optional delete-other-windows)
6978 "Go to the Org-mode file which contains the item at point."
6979 (interactive)
6980 (if (and org-return-follows-link
6981 (not (org-get-at-bol 'org-marker))
6982 (org-in-regexp org-bracket-link-regexp))
6983 (org-open-link-from-string (match-string 0))
6984 (let* ((marker (or (org-get-at-bol 'org-marker)
6985 (org-agenda-error)))
6986 (buffer (marker-buffer marker))
6987 (pos (marker-position marker)))
6988 (org-pop-to-buffer-same-window buffer)
6989 (and delete-other-windows (delete-other-windows))
6990 (widen)
6991 (goto-char pos)
6992 (when (org-mode-p)
6993 (org-show-context 'agenda)
6994 (save-excursion
6995 (and (outline-next-heading)
6996 (org-flag-heading nil))) ; show the next heading
6997 (when (outline-invisible-p)
6998 (show-entry)))))) ; display invisible text
7000 (defun org-agenda-goto-mouse (ev)
7001 "Go to the Org-mode file which contains the item at the mouse click."
7002 (interactive "e")
7003 (mouse-set-point ev)
7004 (org-agenda-goto))
7006 (defun org-agenda-show (&optional full-entry)
7007 "Display the Org-mode file which contains the item at point.
7008 With prefix argument FULL-ENTRY, make the entire entry visible
7009 if it was hidden in the outline."
7010 (interactive "P")
7011 (let ((win (selected-window)))
7012 (if full-entry
7013 (let ((org-show-entry-below t))
7014 (org-agenda-goto t))
7015 (org-agenda-goto t))
7016 (select-window win)))
7018 (defvar org-agenda-show-window nil)
7019 (defun org-agenda-show-and-scroll-up ()
7020 "Display the Org-mode file which contains the item at point.
7021 When called repeatedly, scroll the window that is displaying the buffer."
7022 (interactive)
7023 (let ((win (selected-window)))
7024 (if (and (window-live-p org-agenda-show-window)
7025 (eq this-command last-command))
7026 (progn
7027 (select-window org-agenda-show-window)
7028 (ignore-errors (scroll-up)))
7029 (org-agenda-goto t)
7030 (show-subtree)
7031 (setq org-agenda-show-window (selected-window)))
7032 (select-window win)))
7034 (defun org-agenda-show-scroll-down ()
7035 "Scroll down the window showing the agenda."
7036 (interactive)
7037 (let ((win (selected-window)))
7038 (when (window-live-p org-agenda-show-window)
7039 (select-window org-agenda-show-window)
7040 (ignore-errors (scroll-down))
7041 (select-window win))))
7043 (defun org-agenda-show-1 (&optional more)
7044 "Display the Org-mode file which contains the item at point.
7045 The prefix arg selects the amount of information to display:
7047 0 hide the subtree
7048 1 just show the entry according to defaults.
7049 2 show the children view
7050 3 show the subtree view
7051 4 show the entire subtree and any LOGBOOK drawers
7052 5 show the entire subtree and any drawers
7053 With prefix argument FULL-ENTRY, make the entire entry visible
7054 if it was hidden in the outline."
7055 (interactive "p")
7056 (let ((win (selected-window)))
7057 (org-agenda-goto t)
7058 (org-recenter-heading 1)
7059 (cond
7060 ((= more 0)
7061 (hide-subtree)
7062 (save-excursion
7063 (org-back-to-heading)
7064 (run-hook-with-args 'org-cycle-hook 'folded))
7065 (message "Remote: FOLDED"))
7066 ((and (org-called-interactively-p 'any) (= more 1))
7067 (message "Remote: show with default settings"))
7068 ((= more 2)
7069 (show-entry)
7070 (show-children)
7071 (save-excursion
7072 (org-back-to-heading)
7073 (run-hook-with-args 'org-cycle-hook 'children))
7074 (message "Remote: CHILDREN"))
7075 ((= more 3)
7076 (show-subtree)
7077 (save-excursion
7078 (org-back-to-heading)
7079 (run-hook-with-args 'org-cycle-hook 'subtree))
7080 (message "Remote: SUBTREE"))
7081 ((= more 4)
7082 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7083 (org-drawer-regexp
7084 (concat "^[ \t]*:\\("
7085 (mapconcat 'regexp-quote org-drawers "\\|")
7086 "\\):[ \t]*$")))
7087 (show-subtree)
7088 (save-excursion
7089 (org-back-to-heading)
7090 (org-cycle-hide-drawers 'subtree)))
7091 (message "Remote: SUBTREE AND LOGBOOK"))
7092 ((> more 4)
7093 (show-subtree)
7094 (message "Remote: SUBTREE AND ALL DRAWERS")))
7095 (select-window win)))
7097 (defun org-recenter-heading (n)
7098 (save-excursion
7099 (org-back-to-heading)
7100 (recenter n)))
7102 (defvar org-agenda-cycle-counter nil)
7103 (defun org-agenda-cycle-show (&optional n)
7104 "Show the current entry in another window, with default settings.
7105 Default settings are taken from `org-show-hierarchy-above' and siblings.
7106 When use repeatedly in immediate succession, the remote entry will cycle
7107 through visibility
7109 children -> subtree -> folded
7111 When called with a numeric prefix arg, that arg will be passed through to
7112 `org-agenda-show-1'. For the interpretation of that argument, see the
7113 docstring of `org-agenda-show-1'."
7114 (interactive "P")
7115 (if (integerp n)
7116 (setq org-agenda-cycle-counter n)
7117 (if (not (eq last-command this-command))
7118 (setq org-agenda-cycle-counter 1)
7119 (if (equal org-agenda-cycle-counter 0)
7120 (setq org-agenda-cycle-counter 2)
7121 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7122 (if (> org-agenda-cycle-counter 3)
7123 (setq org-agenda-cycle-counter 0)))))
7124 (org-agenda-show-1 org-agenda-cycle-counter))
7126 (defun org-agenda-recenter (arg)
7127 "Display the Org-mode file which contains the item at point and recenter."
7128 (interactive "P")
7129 (let ((win (selected-window)))
7130 (org-agenda-goto t)
7131 (recenter arg)
7132 (select-window win)))
7134 (defun org-agenda-show-mouse (ev)
7135 "Display the Org-mode file which contains the item at the mouse click."
7136 (interactive "e")
7137 (mouse-set-point ev)
7138 (org-agenda-show))
7140 (defun org-agenda-check-no-diary ()
7141 "Check if the entry is a diary link and abort if yes."
7142 (if (org-get-at-bol 'org-agenda-diary-link)
7143 (org-agenda-error)))
7145 (defun org-agenda-error ()
7146 (error "Command not allowed in this line"))
7148 (defun org-agenda-tree-to-indirect-buffer ()
7149 "Show the subtree corresponding to the current entry in an indirect buffer.
7150 This calls the command `org-tree-to-indirect-buffer' from the original
7151 Org-mode buffer.
7152 With numerical prefix arg ARG, go up to this level and then take that tree.
7153 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7154 use the dedicated frame)."
7155 (interactive)
7156 (org-agenda-check-no-diary)
7157 (let* ((marker (or (org-get-at-bol 'org-marker)
7158 (org-agenda-error)))
7159 (buffer (marker-buffer marker))
7160 (pos (marker-position marker)))
7161 (with-current-buffer buffer
7162 (save-excursion
7163 (goto-char pos)
7164 (call-interactively 'org-tree-to-indirect-buffer)))))
7166 (defvar org-last-heading-marker (make-marker)
7167 "Marker pointing to the headline that last changed its TODO state
7168 by a remote command from the agenda.")
7170 (defun org-agenda-todo-nextset ()
7171 "Switch TODO entry to next sequence."
7172 (interactive)
7173 (org-agenda-todo 'nextset))
7175 (defun org-agenda-todo-previousset ()
7176 "Switch TODO entry to previous sequence."
7177 (interactive)
7178 (org-agenda-todo 'previousset))
7180 (defun org-agenda-todo (&optional arg)
7181 "Cycle TODO state of line at point, also in Org-mode file.
7182 This changes the line at point, all other lines in the agenda referring to
7183 the same tree node, and the headline of the tree node in the Org-mode file."
7184 (interactive "P")
7185 (org-agenda-check-no-diary)
7186 (let* ((col (current-column))
7187 (marker (or (org-get-at-bol 'org-marker)
7188 (org-agenda-error)))
7189 (buffer (marker-buffer marker))
7190 (pos (marker-position marker))
7191 (hdmarker (org-get-at-bol 'org-hd-marker))
7192 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7193 (inhibit-read-only t)
7194 org-agenda-headline-snapshot-before-repeat newhead just-one)
7195 (org-with-remote-undo buffer
7196 (with-current-buffer buffer
7197 (widen)
7198 (goto-char pos)
7199 (org-show-context 'agenda)
7200 (save-excursion
7201 (and (outline-next-heading)
7202 (org-flag-heading nil))) ; show the next heading
7203 (let ((current-prefix-arg arg))
7204 (call-interactively 'org-todo))
7205 (and (bolp) (forward-char 1))
7206 (setq newhead (org-get-heading))
7207 (when (and (org-bound-and-true-p
7208 org-agenda-headline-snapshot-before-repeat)
7209 (not (equal org-agenda-headline-snapshot-before-repeat
7210 newhead))
7211 todayp)
7212 (setq newhead org-agenda-headline-snapshot-before-repeat
7213 just-one t))
7214 (save-excursion
7215 (org-back-to-heading)
7216 (move-marker org-last-heading-marker (point))))
7217 (beginning-of-line 1)
7218 (save-excursion
7219 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7220 (org-move-to-column col))))
7222 (defun org-agenda-add-note (&optional arg)
7223 "Add a time-stamped note to the entry at point."
7224 (interactive "P")
7225 (org-agenda-check-no-diary)
7226 (let* ((marker (or (org-get-at-bol 'org-marker)
7227 (org-agenda-error)))
7228 (buffer (marker-buffer marker))
7229 (pos (marker-position marker))
7230 (hdmarker (org-get-at-bol 'org-hd-marker))
7231 (inhibit-read-only t))
7232 (with-current-buffer buffer
7233 (widen)
7234 (goto-char pos)
7235 (org-show-context 'agenda)
7236 (save-excursion
7237 (and (outline-next-heading)
7238 (org-flag-heading nil))) ; show the next heading
7239 (org-add-note))))
7241 (defun org-agenda-change-all-lines (newhead hdmarker
7242 &optional fixface just-this)
7243 "Change all lines in the agenda buffer which match HDMARKER.
7244 The new content of the line will be NEWHEAD (as modified by
7245 `org-format-agenda-item'). HDMARKER is checked with
7246 `equal' against all `org-hd-marker' text properties in the file.
7247 If FIXFACE is non-nil, the face of each item is modified according to
7248 the new TODO state.
7249 If JUST-THIS is non-nil, change just the current line, not all.
7250 If FORCE-TAGS is non nil, the car of it returns the new tags."
7251 (let* ((inhibit-read-only t)
7252 (line (org-current-line))
7253 (thetags (with-current-buffer (marker-buffer hdmarker)
7254 (save-excursion (save-restriction (widen)
7255 (goto-char hdmarker)
7256 (org-get-tags-at)))))
7257 props m pl undone-face done-face finish new dotime cat tags)
7258 (save-excursion
7259 (goto-char (point-max))
7260 (beginning-of-line 1)
7261 (while (not finish)
7262 (setq finish (bobp))
7263 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7264 (or (not just-this) (= (org-current-line) line))
7265 (equal m hdmarker))
7266 (setq props (text-properties-at (point))
7267 dotime (org-get-at-bol 'dotime)
7268 cat (org-get-at-bol 'org-category)
7269 tags thetags
7271 (let ((org-prefix-format-compiled
7272 (or (get-text-property (point) 'format)
7273 org-prefix-format-compiled)))
7274 (with-current-buffer (marker-buffer hdmarker)
7275 (save-excursion
7276 (save-restriction
7277 (widen)
7278 (org-format-agenda-item (org-get-at-bol 'extra)
7279 newhead cat tags dotime)))))
7280 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7281 undone-face (org-get-at-bol 'undone-face)
7282 done-face (org-get-at-bol 'done-face))
7283 (beginning-of-line 1)
7284 (cond
7285 ((equal new "")
7286 (and (looking-at ".*\n?") (replace-match "")))
7287 ((looking-at ".*")
7288 (replace-match new t t)
7289 (beginning-of-line 1)
7290 (add-text-properties (point-at-bol) (point-at-eol) props)
7291 (when fixface
7292 (add-text-properties
7293 (point-at-bol) (point-at-eol)
7294 (list 'face
7295 (if org-last-todo-state-is-todo
7296 undone-face done-face))))
7297 (org-agenda-highlight-todo 'line)
7298 (beginning-of-line 1))
7299 (t (error "Line update did not work"))))
7300 (beginning-of-line 0)))
7301 (org-finalize-agenda)))
7303 (defun org-agenda-align-tags (&optional line)
7304 "Align all tags in agenda items to `org-agenda-tags-column'."
7305 (let ((inhibit-read-only t) l c)
7306 (save-excursion
7307 (goto-char (if line (point-at-bol) (point-min)))
7308 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7309 (if line (point-at-eol) nil) t)
7310 (add-text-properties
7311 (match-beginning 2) (match-end 2)
7312 (list 'face (delq nil (let ((prop (get-text-property
7313 (match-beginning 2) 'face)))
7314 (or (listp prop) (setq prop (list prop)))
7315 (if (memq 'org-tag prop)
7316 prop
7317 (cons 'org-tag prop))))))
7318 (setq l (- (match-end 2) (match-beginning 2))
7319 c (if (< org-agenda-tags-column 0)
7320 (- (abs org-agenda-tags-column) l)
7321 org-agenda-tags-column))
7322 (delete-region (match-beginning 1) (match-end 1))
7323 (goto-char (match-beginning 1))
7324 (insert (org-add-props
7325 (make-string (max 1 (- c (current-column))) ?\ )
7326 (plist-put (copy-sequence (text-properties-at (point)))
7327 'face nil))))
7328 (goto-char (point-min))
7329 (org-font-lock-add-tag-faces (point-max)))))
7331 (defun org-agenda-priority-up ()
7332 "Increase the priority of line at point, also in Org-mode file."
7333 (interactive)
7334 (org-agenda-priority 'up))
7336 (defun org-agenda-priority-down ()
7337 "Decrease the priority of line at point, also in Org-mode file."
7338 (interactive)
7339 (org-agenda-priority 'down))
7341 (defun org-agenda-priority (&optional force-direction)
7342 "Set the priority of line at point, also in Org-mode file.
7343 This changes the line at point, all other lines in the agenda referring to
7344 the same tree node, and the headline of the tree node in the Org-mode file."
7345 (interactive)
7346 (unless org-enable-priority-commands
7347 (error "Priority commands are disabled"))
7348 (org-agenda-check-no-diary)
7349 (let* ((marker (or (org-get-at-bol 'org-marker)
7350 (org-agenda-error)))
7351 (hdmarker (org-get-at-bol 'org-hd-marker))
7352 (buffer (marker-buffer hdmarker))
7353 (pos (marker-position hdmarker))
7354 (inhibit-read-only t)
7355 newhead)
7356 (org-with-remote-undo buffer
7357 (with-current-buffer buffer
7358 (widen)
7359 (goto-char pos)
7360 (org-show-context 'agenda)
7361 (save-excursion
7362 (and (outline-next-heading)
7363 (org-flag-heading nil))) ; show the next heading
7364 (funcall 'org-priority force-direction)
7365 (end-of-line 1)
7366 (setq newhead (org-get-heading)))
7367 (org-agenda-change-all-lines newhead hdmarker)
7368 (beginning-of-line 1))))
7370 ;; FIXME: should fix the tags property of the agenda line.
7371 (defun org-agenda-set-tags (&optional tag onoff)
7372 "Set tags for the current headline."
7373 (interactive)
7374 (org-agenda-check-no-diary)
7375 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7376 (call-interactively 'org-change-tag-in-region)
7377 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7378 (org-agenda-error)))
7379 (buffer (marker-buffer hdmarker))
7380 (pos (marker-position hdmarker))
7381 (inhibit-read-only t)
7382 newhead)
7383 (org-with-remote-undo buffer
7384 (with-current-buffer buffer
7385 (widen)
7386 (goto-char pos)
7387 (save-excursion
7388 (org-show-context 'agenda))
7389 (save-excursion
7390 (and (outline-next-heading)
7391 (org-flag-heading nil))) ; show the next heading
7392 (goto-char pos)
7393 (if tag
7394 (org-toggle-tag tag onoff)
7395 (call-interactively 'org-set-tags))
7396 (end-of-line 1)
7397 (setq newhead (org-get-heading)))
7398 (org-agenda-change-all-lines newhead hdmarker)
7399 (beginning-of-line 1)))))
7401 (defun org-agenda-set-property ()
7402 "Set a property for the current headline."
7403 (interactive)
7404 (org-agenda-check-no-diary)
7405 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7406 (org-agenda-error)))
7407 (buffer (marker-buffer hdmarker))
7408 (pos (marker-position hdmarker))
7409 (inhibit-read-only t)
7410 newhead)
7411 (org-with-remote-undo buffer
7412 (with-current-buffer buffer
7413 (widen)
7414 (goto-char pos)
7415 (save-excursion
7416 (org-show-context 'agenda))
7417 (save-excursion
7418 (and (outline-next-heading)
7419 (org-flag-heading nil))) ; show the next heading
7420 (goto-char pos)
7421 (call-interactively 'org-set-property)))))
7423 (defun org-agenda-set-effort ()
7424 "Set the effort property for the current headline."
7425 (interactive)
7426 (org-agenda-check-no-diary)
7427 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7428 (org-agenda-error)))
7429 (buffer (marker-buffer hdmarker))
7430 (pos (marker-position hdmarker))
7431 (inhibit-read-only t)
7432 newhead)
7433 (org-with-remote-undo buffer
7434 (with-current-buffer buffer
7435 (widen)
7436 (goto-char pos)
7437 (save-excursion
7438 (org-show-context 'agenda))
7439 (save-excursion
7440 (and (outline-next-heading)
7441 (org-flag-heading nil))) ; show the next heading
7442 (goto-char pos)
7443 (call-interactively 'org-set-effort)
7444 (end-of-line 1)
7445 (setq newhead (org-get-heading)))
7446 (org-agenda-change-all-lines newhead hdmarker))))
7448 (defun org-agenda-toggle-archive-tag ()
7449 "Toggle the archive tag for the current entry."
7450 (interactive)
7451 (org-agenda-check-no-diary)
7452 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7453 (org-agenda-error)))
7454 (buffer (marker-buffer hdmarker))
7455 (pos (marker-position hdmarker))
7456 (inhibit-read-only t)
7457 newhead)
7458 (org-with-remote-undo buffer
7459 (with-current-buffer buffer
7460 (widen)
7461 (goto-char pos)
7462 (org-show-context 'agenda)
7463 (save-excursion
7464 (and (outline-next-heading)
7465 (org-flag-heading nil))) ; show the next heading
7466 (call-interactively 'org-toggle-archive-tag)
7467 (end-of-line 1)
7468 (setq newhead (org-get-heading)))
7469 (org-agenda-change-all-lines newhead hdmarker)
7470 (beginning-of-line 1))))
7472 (defun org-agenda-do-date-later (arg)
7473 (interactive "P")
7474 (cond
7475 ((or (equal arg '(16))
7476 (memq last-command
7477 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7478 (setq this-command 'org-agenda-date-later-minutes)
7479 (org-agenda-date-later-minutes 1))
7480 ((or (equal arg '(4))
7481 (memq last-command
7482 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7483 (setq this-command 'org-agenda-date-later-hours)
7484 (org-agenda-date-later-hours 1))
7486 (org-agenda-date-later (prefix-numeric-value arg)))))
7488 (defun org-agenda-do-date-earlier (arg)
7489 (interactive "P")
7490 (cond
7491 ((or (equal arg '(16))
7492 (memq last-command
7493 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7494 (setq this-command 'org-agenda-date-earlier-minutes)
7495 (org-agenda-date-earlier-minutes 1))
7496 ((or (equal arg '(4))
7497 (memq last-command
7498 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7499 (setq this-command 'org-agenda-date-earlier-hours)
7500 (org-agenda-date-earlier-hours 1))
7502 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7504 (defun org-agenda-date-later (arg &optional what)
7505 "Change the date of this item to ARG day(s) later."
7506 (interactive "p")
7507 (org-agenda-check-type t 'agenda 'timeline)
7508 (org-agenda-check-no-diary)
7509 (let* ((marker (or (org-get-at-bol 'org-marker)
7510 (org-agenda-error)))
7511 (buffer (marker-buffer marker))
7512 (pos (marker-position marker)))
7513 (org-with-remote-undo buffer
7514 (with-current-buffer buffer
7515 (widen)
7516 (goto-char pos)
7517 (if (not (org-at-timestamp-p))
7518 (error "Cannot find time stamp"))
7519 (org-timestamp-change arg (or what 'day)))
7520 (org-agenda-show-new-time marker org-last-changed-timestamp))
7521 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7523 (defun org-agenda-date-earlier (arg &optional what)
7524 "Change the date of this item to ARG day(s) earlier."
7525 (interactive "p")
7526 (org-agenda-date-later (- arg) what))
7528 (defun org-agenda-date-later-minutes (arg)
7529 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7530 (interactive "p")
7531 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7532 (org-agenda-date-later arg 'minute))
7534 (defun org-agenda-date-earlier-minutes (arg)
7535 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7536 (interactive "p")
7537 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7538 (org-agenda-date-earlier arg 'minute))
7540 (defun org-agenda-date-later-hours (arg)
7541 "Change the time of this item, in hour steps."
7542 (interactive "p")
7543 (org-agenda-date-later arg 'hour))
7545 (defun org-agenda-date-earlier-hours (arg)
7546 "Change the time of this item, in hour steps."
7547 (interactive "p")
7548 (org-agenda-date-earlier arg 'hour))
7550 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7551 "Show new date stamp via text properties."
7552 ;; We use text properties to make this undoable
7553 (let ((inhibit-read-only t)
7554 (buffer-invisibility-spec))
7555 (setq stamp (concat " " prefix " => " stamp))
7556 (save-excursion
7557 (goto-char (point-max))
7558 (while (not (bobp))
7559 (when (equal marker (org-get-at-bol 'org-marker))
7560 (org-move-to-column (- (window-width) (length stamp)) t)
7561 (org-agenda-fix-tags-filter-overlays-at (point))
7562 (if (featurep 'xemacs)
7563 ;; Use `duplicable' property to trigger undo recording
7564 (let ((ex (make-extent nil nil))
7565 (gl (make-glyph stamp)))
7566 (set-glyph-face gl 'secondary-selection)
7567 (set-extent-properties
7568 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7569 (insert-extent ex (1- (point)) (point-at-eol)))
7570 (add-text-properties
7571 (1- (point)) (point-at-eol)
7572 (list 'display (org-add-props stamp nil
7573 'face 'secondary-selection))))
7574 (beginning-of-line 1))
7575 (beginning-of-line 0)))))
7577 (defun org-agenda-date-prompt (arg)
7578 "Change the date of this item. Date is prompted for, with default today.
7579 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7580 be used to request time specification in the time stamp."
7581 (interactive "P")
7582 (org-agenda-check-type t 'agenda 'timeline)
7583 (org-agenda-check-no-diary)
7584 (let* ((marker (or (org-get-at-bol 'org-marker)
7585 (org-agenda-error)))
7586 (buffer (marker-buffer marker))
7587 (pos (marker-position marker)))
7588 (org-with-remote-undo buffer
7589 (with-current-buffer buffer
7590 (widen)
7591 (goto-char pos)
7592 (if (not (org-at-timestamp-p t))
7593 (error "Cannot find time stamp"))
7594 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7595 (org-agenda-show-new-time marker org-last-changed-timestamp))
7596 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7598 (defun org-agenda-schedule (arg &optional time)
7599 "Schedule the item at point.
7600 ARG is passed through to `org-schedule'."
7601 (interactive "P")
7602 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7603 (org-agenda-check-no-diary)
7604 (let* ((marker (or (org-get-at-bol 'org-marker)
7605 (org-agenda-error)))
7606 (type (marker-insertion-type marker))
7607 (buffer (marker-buffer marker))
7608 (pos (marker-position marker))
7609 (org-insert-labeled-timestamps-at-point nil)
7611 (set-marker-insertion-type marker t)
7612 (org-with-remote-undo buffer
7613 (with-current-buffer buffer
7614 (widen)
7615 (goto-char pos)
7616 (setq ts (org-schedule arg time)))
7617 (org-agenda-show-new-time marker ts "S"))
7618 (message "Item scheduled for %s" ts)))
7620 (defun org-agenda-deadline (arg &optional time)
7621 "Schedule the item at point.
7622 ARG is passed through to `org-deadline'."
7623 (interactive "P")
7624 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7625 (org-agenda-check-no-diary)
7626 (let* ((marker (or (org-get-at-bol 'org-marker)
7627 (org-agenda-error)))
7628 (buffer (marker-buffer marker))
7629 (pos (marker-position marker))
7630 (org-insert-labeled-timestamps-at-point nil)
7632 (org-with-remote-undo buffer
7633 (with-current-buffer buffer
7634 (widen)
7635 (goto-char pos)
7636 (setq ts (org-deadline arg time)))
7637 (org-agenda-show-new-time marker ts "D"))
7638 (message "Deadline for this item set to %s" ts)))
7640 (defun org-agenda-action ()
7641 "Select entry for agenda action, or execute an agenda action.
7642 This command prompts for another letter. Valid inputs are:
7644 m Mark the entry at point for an agenda action
7645 s Schedule the marked entry to the date at the cursor
7646 d Set the deadline of the marked entry to the date at the cursor
7647 r Call `org-remember' with cursor date as the default date
7648 c Call `org-capture' with cursor date as the default date
7649 SPC Show marked entry in other window
7650 TAB Visit marked entry in other window
7652 The cursor may be at a date in the calendar, or in the Org agenda."
7653 (interactive)
7654 (let (ans)
7655 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7656 (setq ans (read-char-exclusive))
7657 (cond
7658 ((equal ans ?m)
7659 ;; Mark this entry
7660 (if (eq major-mode 'org-agenda-mode)
7661 (let ((m (or (org-get-at-bol 'org-hd-marker)
7662 (org-get-at-bol 'org-marker))))
7663 (if m
7664 (progn
7665 (move-marker org-agenda-action-marker
7666 (marker-position m) (marker-buffer m))
7667 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7668 (error "Don't know which entry to mark")))
7669 (error "This command works only in the agenda")))
7670 ((equal ans ?s)
7671 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7672 ((equal ans ?d)
7673 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7674 ((equal ans ?r)
7675 (org-agenda-do-action '(org-remember) t))
7676 ((equal ans ?c)
7677 (org-agenda-do-action '(org-capture) t))
7678 ((equal ans ?\ )
7679 (let ((cw (selected-window)))
7680 (org-switch-to-buffer-other-window
7681 (marker-buffer org-agenda-action-marker))
7682 (goto-char org-agenda-action-marker)
7683 (org-show-context 'agenda)
7684 (select-window cw)))
7685 ((equal ans ?\C-i)
7686 (org-switch-to-buffer-other-window
7687 (marker-buffer org-agenda-action-marker))
7688 (goto-char org-agenda-action-marker)
7689 (org-show-context 'agenda))
7690 (t (error "Invalid agenda action %c" ans)))))
7692 (defun org-agenda-do-action (form &optional current-buffer)
7693 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7694 (let ((org-overriding-default-time (org-get-cursor-date)))
7695 (if current-buffer
7696 (eval form)
7697 (if (not (marker-buffer org-agenda-action-marker))
7698 (error "No entry has been selected for agenda action")
7699 (with-current-buffer (marker-buffer org-agenda-action-marker)
7700 (save-excursion
7701 (save-restriction
7702 (widen)
7703 (goto-char org-agenda-action-marker)
7704 (eval form))))))))
7706 (defun org-agenda-clock-in (&optional arg)
7707 "Start the clock on the currently selected item."
7708 (interactive "P")
7709 (org-agenda-check-no-diary)
7710 (if (equal arg '(4))
7711 (org-clock-in arg)
7712 (let* ((marker (or (org-get-at-bol 'org-marker)
7713 (org-agenda-error)))
7714 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7715 marker))
7716 (pos (marker-position marker))
7717 newhead)
7718 (org-with-remote-undo (marker-buffer marker)
7719 (with-current-buffer (marker-buffer marker)
7720 (widen)
7721 (goto-char pos)
7722 (org-show-context 'agenda)
7723 (org-show-entry)
7724 (org-cycle-hide-drawers 'children)
7725 (org-clock-in arg)
7726 (setq newhead (org-get-heading)))
7727 (org-agenda-change-all-lines newhead hdmarker)))))
7729 (defun org-agenda-clock-out ()
7730 "Stop the currently running clock."
7731 (interactive)
7732 (unless (marker-buffer org-clock-marker)
7733 (error "No running clock"))
7734 (let ((marker (make-marker)) newhead)
7735 (org-with-remote-undo (marker-buffer org-clock-marker)
7736 (with-current-buffer (marker-buffer org-clock-marker)
7737 (save-excursion
7738 (save-restriction
7739 (widen)
7740 (goto-char org-clock-marker)
7741 (org-back-to-heading t)
7742 (move-marker marker (point))
7743 (org-clock-out)
7744 (setq newhead (org-get-heading))))))
7745 (org-agenda-change-all-lines newhead marker)
7746 (move-marker marker nil)))
7748 (defun org-agenda-clock-cancel (&optional arg)
7749 "Cancel the currently running clock."
7750 (interactive "P")
7751 (unless (marker-buffer org-clock-marker)
7752 (error "No running clock"))
7753 (org-with-remote-undo (marker-buffer org-clock-marker)
7754 (org-clock-cancel)))
7756 (defun org-agenda-clock-goto ()
7757 "Jump to the currently clocked in task within the agenda.
7758 If the currently clocked in task is not listed in the agenda
7759 buffer, display it in another window."
7760 (interactive)
7761 (let (pos)
7762 (mapc (lambda (o)
7763 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7764 (setq pos (overlay-start o))))
7765 (overlays-in (point-min) (point-max)))
7766 (cond (pos (goto-char pos))
7767 ;; If the currently clocked entry is not in the agenda
7768 ;; buffer, we visit it in another window:
7769 (org-clock-current-task
7770 (org-switch-to-buffer-other-window (org-clock-goto)))
7771 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7773 (defun org-agenda-diary-entry-in-org-file ()
7774 "Make a diary entry in the file `org-agenda-diary-file'."
7775 (let (d1 d2 char (text "") dp1 dp2)
7776 (if (equal (buffer-name) "*Calendar*")
7777 (setq d1 (calendar-cursor-to-date t)
7778 d2 (car calendar-mark-ring))
7779 (setq dp1 (get-text-property (point-at-bol) 'day))
7780 (unless dp1 (error "No date defined in current line"))
7781 (setq d1 (calendar-gregorian-from-absolute dp1)
7782 d2 (and (ignore-errors (mark))
7783 (save-excursion
7784 (goto-char (mark))
7785 (setq dp2 (get-text-property (point-at-bol) 'day)))
7786 (calendar-gregorian-from-absolute dp2))))
7787 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7788 (setq char (read-char-exclusive))
7789 (cond
7790 ((equal char ?d)
7791 (setq text (read-string "Day entry: "))
7792 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7793 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7794 ((equal char ?a)
7795 (setq d1 (list (car d1) (nth 1 d1)
7796 (read-number (format "Reference year [%d]: " (nth 2 d1))
7797 (nth 2 d1))))
7798 (setq text (read-string "Anniversary (use %d to show years): "))
7799 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7800 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7801 ((equal char ?b)
7802 (setq text (read-string "Block entry: "))
7803 (unless (and d1 d2 (not (equal d1 d2)))
7804 (error "No block of days selected"))
7805 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7806 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7807 ((equal char ?j)
7808 (org-switch-to-buffer-other-window
7809 (find-file-noselect org-agenda-diary-file))
7810 (require 'org-datetree)
7811 (org-datetree-find-date-create d1)
7812 (org-reveal t))
7813 (t (error "Invalid selection character `%c'" char)))))
7815 (defcustom org-agenda-insert-diary-strategy 'date-tree
7816 "Where in `org-agenda-diary-file' should new entries be added?
7817 Valid values:
7819 date-tree in the date tree, as child of the date
7820 top-level as top-level entries at the end of the file."
7821 :group 'org-agenda
7822 :type '(choice
7823 (const :tag "in a date tree" date-tree)
7824 (const :tag "as top level at end of file" top-level)))
7826 (defcustom org-agenda-insert-diary-extract-time nil
7827 "Non-nil means extract any time specification from the diary entry."
7828 :group 'org-agenda
7829 :type 'boolean)
7831 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7832 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7833 If TEXT is not empty, it will become the headline of the new entry, and
7834 the resulting entry will not be shown. When TEXT is empty, switch to
7835 `org-agenda-diary-file' and let the user finish the entry there."
7836 (let ((cw (current-window-configuration)))
7837 (org-switch-to-buffer-other-window
7838 (find-file-noselect org-agenda-diary-file))
7839 (widen)
7840 (goto-char (point-min))
7841 (cond
7842 ((eq type 'anniversary)
7843 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7844 (progn
7845 (or (org-on-heading-p t)
7846 (progn
7847 (outline-next-heading)
7848 (insert "* Anniversaries\n\n")
7849 (beginning-of-line -1)))))
7850 (outline-next-heading)
7851 (org-back-over-empty-lines)
7852 (backward-char 1)
7853 (insert "\n")
7854 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7855 (nth 2 d1) (car d1) (nth 1 d1) text)))
7856 ((eq type 'day)
7857 (let ((org-prefix-has-time t)
7858 (org-agenda-time-leading-zero t)
7859 fmt time time2)
7860 (if org-agenda-insert-diary-extract-time
7861 ;; Use org-format-agenda-item to parse text for a time-range and
7862 ;; remove it. FIXME: This is a hack, we should refactor
7863 ;; that function to make time extraction available separately
7864 (setq fmt (org-format-agenda-item nil text nil nil t)
7865 time (get-text-property 0 'time fmt)
7866 time2 (if (> (length time) 0)
7867 ;; split-string removes trailing ...... if
7868 ;; no end time given. First space
7869 ;; separates time from date.
7870 (concat " " (car (split-string time "\\.")))
7871 nil)
7872 text (get-text-property 0 'txt fmt)))
7873 (if (eq org-agenda-insert-diary-strategy 'top-level)
7874 (org-agenda-insert-diary-as-top-level text)
7875 (require 'org-datetree)
7876 (org-datetree-find-date-create d1)
7877 (org-agenda-insert-diary-make-new-entry text))
7878 (org-insert-time-stamp (org-time-from-absolute
7879 (calendar-absolute-from-gregorian d1))
7880 nil nil nil nil time2))
7881 (end-of-line 0))
7882 ((eq type 'block)
7883 (if (> (calendar-absolute-from-gregorian d1)
7884 (calendar-absolute-from-gregorian d2))
7885 (setq d1 (prog1 d2 (setq d2 d1))))
7886 (if (eq org-agenda-insert-diary-strategy 'top-level)
7887 (org-agenda-insert-diary-as-top-level text)
7888 (require 'org-datetree)
7889 (org-datetree-find-date-create d1)
7890 (org-agenda-insert-diary-make-new-entry text))
7891 (org-insert-time-stamp (org-time-from-absolute
7892 (calendar-absolute-from-gregorian d1)))
7893 (insert "--")
7894 (org-insert-time-stamp (org-time-from-absolute
7895 (calendar-absolute-from-gregorian d2)))
7896 (end-of-line 0)))
7897 (if (string-match "\\S-" text)
7898 (progn
7899 (set-window-configuration cw)
7900 (message "%s entry added to %s"
7901 (capitalize (symbol-name type))
7902 (abbreviate-file-name org-agenda-diary-file)))
7903 (org-reveal t)
7904 (message "Please finish entry here"))))
7906 (defun org-agenda-insert-diary-as-top-level (text)
7907 "Make new entry as a top-level entry at the end of the file.
7908 Add TEXT as headline, and position the cursor in the second line so that
7909 a timestamp can be added there."
7910 (widen)
7911 (goto-char (point-max))
7912 (or (bolp) (insert "\n"))
7913 (insert "* " text "\n")
7914 (if org-adapt-indentation (org-indent-to-column 2)))
7916 (defun org-agenda-insert-diary-make-new-entry (text)
7917 "Make new entry as last child of current entry.
7918 Add TEXT as headline, and position the cursor in the second line so that
7919 a timestamp can be added there."
7920 (let ((org-show-following-heading t)
7921 (org-show-siblings t)
7922 (org-show-hierarchy-above t)
7923 (org-show-entry-below t)
7924 col)
7925 (outline-next-heading)
7926 (org-back-over-empty-lines)
7927 (or (looking-at "[ \t]*$")
7928 (progn (insert "\n") (backward-char 1)))
7929 (org-insert-heading nil t)
7930 (org-do-demote)
7931 (setq col (current-column))
7932 (insert text "\n")
7933 (if org-adapt-indentation (org-indent-to-column col))
7934 (let ((org-show-following-heading t)
7935 (org-show-siblings t)
7936 (org-show-hierarchy-above t)
7937 (org-show-entry-below t))
7938 (org-show-context))))
7940 (defun org-agenda-diary-entry ()
7941 "Make a diary entry, like the `i' command from the calendar.
7942 All the standard commands work: block, weekly etc.
7943 When `org-agenda-diary-file' points to a file,
7944 `org-agenda-diary-entry-in-org-file' is called instead to create
7945 entries in that Org-mode file."
7946 (interactive)
7947 (org-agenda-check-type t 'agenda 'timeline)
7948 (if (not (eq org-agenda-diary-file 'diary-file))
7949 (org-agenda-diary-entry-in-org-file)
7950 (require 'diary-lib)
7951 (let* ((char (progn
7952 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7953 (read-char-exclusive)))
7954 (cmd (cdr (assoc char
7955 '((?d . insert-diary-entry)
7956 (?w . insert-weekly-diary-entry)
7957 (?m . insert-monthly-diary-entry)
7958 (?y . insert-yearly-diary-entry)
7959 (?a . insert-anniversary-diary-entry)
7960 (?b . insert-block-diary-entry)
7961 (?c . insert-cyclic-diary-entry)))))
7962 (oldf (symbol-function 'calendar-cursor-to-date))
7963 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7964 (point (point))
7965 (mark (or (mark t) (point))))
7966 (unless cmd
7967 (error "No command associated with <%c>" char))
7968 (unless (and (get-text-property point 'day)
7969 (or (not (equal ?b char))
7970 (get-text-property mark 'day)))
7971 (error "Don't know which date to use for diary entry"))
7972 ;; We implement this by hacking the `calendar-cursor-to-date' function
7973 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7974 (let ((calendar-mark-ring
7975 (list (calendar-gregorian-from-absolute
7976 (or (get-text-property mark 'day)
7977 (get-text-property point 'day))))))
7978 (unwind-protect
7979 (progn
7980 (fset 'calendar-cursor-to-date
7981 (lambda (&optional error dummy)
7982 (calendar-gregorian-from-absolute
7983 (get-text-property point 'day))))
7984 (call-interactively cmd))
7985 (fset 'calendar-cursor-to-date oldf))))))
7987 (defun org-agenda-execute-calendar-command (cmd)
7988 "Execute a calendar command from the agenda, with the date associated to
7989 the cursor position."
7990 (org-agenda-check-type t 'agenda 'timeline)
7991 (require 'diary-lib)
7992 (unless (get-text-property (point) 'day)
7993 (error "Don't know which date to use for calendar command"))
7994 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7995 (point (point))
7996 (date (calendar-gregorian-from-absolute
7997 (get-text-property point 'day)))
7998 ;; the following 2 vars are needed in the calendar
7999 (displayed-month (car date))
8000 (displayed-year (nth 2 date)))
8001 (unwind-protect
8002 (progn
8003 (fset 'calendar-cursor-to-date
8004 (lambda (&optional error dummy)
8005 (calendar-gregorian-from-absolute
8006 (get-text-property point 'day))))
8007 (call-interactively cmd))
8008 (fset 'calendar-cursor-to-date oldf))))
8010 (defun org-agenda-phases-of-moon ()
8011 "Display the phases of the moon for the 3 months around the cursor date."
8012 (interactive)
8013 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8015 (defun org-agenda-holidays ()
8016 "Display the holidays for the 3 months around the cursor date."
8017 (interactive)
8018 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8020 (defvar calendar-longitude)
8021 (defvar calendar-latitude)
8022 (defvar calendar-location-name)
8024 (defun org-agenda-sunrise-sunset (arg)
8025 "Display sunrise and sunset for the cursor date.
8026 Latitude and longitude can be specified with the variables
8027 `calendar-latitude' and `calendar-longitude'. When called with prefix
8028 argument, latitude and longitude will be prompted for."
8029 (interactive "P")
8030 (require 'solar)
8031 (let ((calendar-longitude (if arg nil calendar-longitude))
8032 (calendar-latitude (if arg nil calendar-latitude))
8033 (calendar-location-name
8034 (if arg "the given coordinates" calendar-location-name)))
8035 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8037 (defun org-agenda-goto-calendar ()
8038 "Open the Emacs calendar with the date at the cursor."
8039 (interactive)
8040 (org-agenda-check-type t 'agenda 'timeline)
8041 (let* ((day (or (get-text-property (point) 'day)
8042 (error "Don't know which date to open in calendar")))
8043 (date (calendar-gregorian-from-absolute day))
8044 (calendar-move-hook nil)
8045 (calendar-view-holidays-initially-flag nil)
8046 (calendar-view-diary-initially-flag nil))
8047 (calendar)
8048 (calendar-goto-date date)))
8050 ;;;###autoload
8051 (defun org-calendar-goto-agenda ()
8052 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8053 This is a command that has to be installed in `calendar-mode-map'."
8054 (interactive)
8055 (org-agenda-list nil (calendar-absolute-from-gregorian
8056 (calendar-cursor-to-date))
8057 nil))
8059 (defun org-agenda-convert-date ()
8060 (interactive)
8061 (org-agenda-check-type t 'agenda 'timeline)
8062 (let ((day (get-text-property (point) 'day))
8063 date s)
8064 (unless day
8065 (error "Don't know which date to convert"))
8066 (setq date (calendar-gregorian-from-absolute day))
8067 (setq s (concat
8068 "Gregorian: " (calendar-date-string date) "\n"
8069 "ISO: " (calendar-iso-date-string date) "\n"
8070 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8071 "Julian: " (calendar-julian-date-string date) "\n"
8072 "Astron. JD: " (calendar-astro-date-string date)
8073 " (Julian date number at noon UTC)\n"
8074 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8075 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8076 "French: " (calendar-french-date-string date) "\n"
8077 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8078 "Mayan: " (calendar-mayan-date-string date) "\n"
8079 "Coptic: " (calendar-coptic-date-string date) "\n"
8080 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8081 "Persian: " (calendar-persian-date-string date) "\n"
8082 "Chinese: " (calendar-chinese-date-string date) "\n"))
8083 (with-output-to-temp-buffer "*Dates*"
8084 (princ s))
8085 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8087 ;;; Bulk commands
8089 (defvar org-agenda-bulk-marked-entries nil
8090 "List of markers that refer to marked entries in the agenda.")
8092 (defun org-agenda-bulk-marked-p ()
8093 (eq (get-char-property (point-at-bol) 'type)
8094 'org-marked-entry-overlay))
8096 (defun org-agenda-bulk-mark (&optional arg)
8097 "Mark the entry at point for future bulk action."
8098 (interactive "p")
8099 (dotimes (i (max arg 1))
8100 (unless (org-get-at-bol 'org-agenda-diary-link)
8101 (let* ((m (org-get-at-bol 'org-hd-marker))
8103 (unless (org-agenda-bulk-marked-p)
8104 (unless m (error "Nothing to mark at point"))
8105 (push m org-agenda-bulk-marked-entries)
8106 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8107 (org-overlay-display ov "> "
8108 (org-get-todo-face "TODO")
8109 'evaporate)
8110 (overlay-put ov 'type 'org-marked-entry-overlay))
8111 (beginning-of-line 2)
8112 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8113 (beginning-of-line 2))
8114 (message "%d entries marked for bulk action"
8115 (length org-agenda-bulk-marked-entries))))))
8117 (defun org-agenda-bulk-mark-regexp (regexp)
8118 "Mark entries match REGEXP."
8119 (interactive "sMark entries matching regexp: ")
8120 (let (entries-marked)
8121 (save-excursion
8122 (goto-char (point-min))
8123 (goto-char (next-single-property-change (point) 'txt))
8124 (while (re-search-forward regexp nil t)
8125 (when (string-match regexp (get-text-property (point) 'txt))
8126 (setq entries-marked (+ entries-marked 1))
8127 (call-interactively 'org-agenda-bulk-mark))))
8128 (if (not entries-marked)
8129 (message "No entry matching this regexp."))))
8131 (defun org-agenda-bulk-unmark ()
8132 "Unmark the entry at point for future bulk action."
8133 (interactive)
8134 (when (org-agenda-bulk-marked-p)
8135 (org-agenda-bulk-remove-overlays
8136 (point-at-bol) (+ 2 (point-at-bol)))
8137 (setq org-agenda-bulk-marked-entries
8138 (delete (org-get-at-bol 'org-hd-marker)
8139 org-agenda-bulk-marked-entries)))
8140 (beginning-of-line 2)
8141 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8142 (beginning-of-line 2))
8143 (message "%d entries marked for bulk action"
8144 (length org-agenda-bulk-marked-entries)))
8146 (defun org-agenda-bulk-toggle ()
8147 "Toggle marking the entry at point for bulk action."
8148 (interactive)
8149 (if (org-agenda-bulk-marked-p)
8150 (org-agenda-bulk-unmark)
8151 (org-agenda-bulk-mark)))
8153 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8154 "Remove the mark overlays between BEG and END in the agenda buffer.
8155 BEG and END default to the buffer limits.
8157 This only removes the overlays, it does not remove the markers
8158 from the list in `org-agenda-bulk-marked-entries'."
8159 (interactive)
8160 (mapc (lambda (ov)
8161 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8162 (delete-overlay ov)))
8163 (overlays-in (or beg (point-min)) (or end (point-max)))))
8165 (defun org-agenda-bulk-remove-all-marks ()
8166 "Remove all marks in the agenda buffer.
8167 This will remove the markers, and the overlays."
8168 (interactive)
8169 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8170 (setq org-agenda-bulk-marked-entries nil)
8171 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8173 (defun org-agenda-bulk-action (&optional arg)
8174 "Execute an remote-editing action on all marked entries.
8175 The prefix arg is passed through to the command if possible."
8176 (interactive "P")
8177 ;; Make sure we have markers, and only valid ones
8178 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8179 (mapc
8180 (lambda (m)
8181 (unless (and (markerp m)
8182 (marker-buffer m)
8183 (buffer-live-p (marker-buffer m))
8184 (marker-position m))
8185 (error "Marker %s for bulk command is invalid" m)))
8186 org-agenda-bulk-marked-entries)
8188 ;; Prompt for the bulk command
8189 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8190 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8191 (when org-agenda-bulk-custom-functions
8192 (concat " Custom: ["
8193 (mapconcat (lambda(f) (char-to-string (car f)))
8194 org-agenda-bulk-custom-functions "")
8195 "]"))))
8196 (let* ((action (read-char-exclusive))
8197 (org-log-refile (if org-log-refile 'time nil))
8198 (entries (reverse org-agenda-bulk-marked-entries))
8199 redo-at-end
8200 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8201 (cond
8202 ((equal action ?$)
8203 (setq cmd '(org-agenda-archive)))
8205 ((equal action ?A)
8206 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8208 ((member action '(?r ?w))
8209 (setq rfloc (org-refile-get-location
8210 "Refile to"
8211 (marker-buffer (car org-agenda-bulk-marked-entries))
8212 org-refile-allow-creating-parent-nodes))
8213 (if (nth 3 rfloc)
8214 (setcar (nthcdr 3 rfloc)
8215 (move-marker (make-marker) (nth 3 rfloc)
8216 (or (get-file-buffer (nth 1 rfloc))
8217 (find-buffer-visiting (nth 1 rfloc))
8218 (error "This should not happen")))))
8220 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8221 redo-at-end t))
8223 ((equal action ?t)
8224 (setq state (org-icompleting-read
8225 "Todo state: "
8226 (with-current-buffer (marker-buffer (car entries))
8227 (mapcar 'list org-todo-keywords-1))))
8228 (setq cmd `(let ((org-inhibit-blocking t)
8229 (org-inhibit-logging 'note))
8230 (org-agenda-todo ,state))))
8232 ((memq action '(?- ?+))
8233 (setq tag (org-icompleting-read
8234 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8235 (with-current-buffer (marker-buffer (car entries))
8236 (delq nil
8237 (mapcar (lambda (x)
8238 (if (stringp (car x)) x)) org-tag-alist)))))
8239 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8241 ((memq action '(?s ?d))
8242 (let* ((date (unless arg
8243 (org-read-date
8244 nil nil nil
8245 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8246 (ans (if arg nil org-read-date-final-answer))
8247 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8248 (setq cmd `(let* ((bound (fboundp 'read-string))
8249 (old (and bound (symbol-function 'read-string))))
8250 (unwind-protect
8251 (progn
8252 (fset 'read-string (lambda (&rest ignore) ,ans))
8253 (eval '(,c1 arg)))
8254 (if bound
8255 (fset 'read-string old)
8256 (fmakunbound 'read-string)))))))
8258 ((equal action ?S)
8259 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8260 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8261 (let ((days (read-number
8262 (format "Scatter tasks across how many %sdays: "
8263 (if arg "week" "")) 7)))
8264 (setq cmd
8265 `(let ((distance (1+ (random ,days))))
8266 (if arg
8267 (let ((dist distance)
8268 (day-of-week
8269 (calendar-day-of-week
8270 (calendar-gregorian-from-absolute (org-today)))))
8271 (dotimes (i (1+ dist))
8272 (while (member day-of-week org-agenda-weekend-days)
8273 (incf distance)
8274 (incf day-of-week)
8275 (if (= day-of-week 7)
8276 (setq day-of-week 0)))
8277 (incf day-of-week)
8278 (if (= day-of-week 7)
8279 (setq day-of-week 0)))))
8280 ;; silently fail when try to replan a sexp entry
8281 (condition-case nil
8282 (let* ((date (calendar-gregorian-from-absolute
8283 (+ (org-today) distance)))
8284 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8285 (nth 2 date))))
8286 (org-agenda-schedule nil time))
8287 (error nil)))))))
8289 ((assoc action org-agenda-bulk-custom-functions)
8290 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8291 redo-at-end t))
8293 ((equal action ?f)
8294 (setq cmd (list (intern
8295 (org-icompleting-read "Function: "
8296 obarray 'fboundp t nil nil)))))
8298 (t (error "Invalid bulk action")))
8300 ;; Sort the markers, to make sure that parents are handled before children
8301 (setq entries (sort entries
8302 (lambda (a b)
8303 (cond
8304 ((equal (marker-buffer a) (marker-buffer b))
8305 (< (marker-position a) (marker-position b)))
8307 (string< (buffer-name (marker-buffer a))
8308 (buffer-name (marker-buffer b))))))))
8310 ;; Now loop over all markers and apply cmd
8311 (while (setq e (pop entries))
8312 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8313 (if (not pos)
8314 (progn (message "Skipping removed entry at %s" e)
8315 (setq cntskip (1+ cntskip)))
8316 (goto-char pos)
8317 (eval cmd)
8318 (setq org-agenda-bulk-marked-entries
8319 (delete e org-agenda-bulk-marked-entries))
8320 (setq cnt (1+ cnt))))
8321 (setq org-agenda-bulk-marked-entries nil)
8322 (org-agenda-bulk-remove-all-marks)
8323 (when redo-at-end (org-agenda-redo))
8324 (message "Acted on %d entries%s"
8326 (if (= cntskip 0)
8328 (format ", skipped %d (disappeared before their turn)"
8329 cntskip)))))
8331 ;;; Flagging notes
8333 (defun org-agenda-show-the-flagging-note ()
8334 "Display the flagging note in the other window.
8335 When called a second time in direct sequence, offer to remove the FLAGGING
8336 tag and (if present) the flagging note."
8337 (interactive)
8338 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8339 (win (selected-window))
8340 note heading newhead)
8341 (unless hdmarker
8342 (error "No linked entry at point"))
8343 (if (and (eq this-command last-command)
8344 (y-or-n-p "Unflag and remove any flagging note? "))
8345 (progn
8346 (org-agenda-remove-flag hdmarker)
8347 (let ((win (get-buffer-window "*Flagging Note*")))
8348 (and win (delete-window win)))
8349 (message "Entry unflaged"))
8350 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8351 (unless note
8352 (error "No flagging note"))
8353 (org-kill-new note)
8354 (org-switch-to-buffer-other-window "*Flagging Note*")
8355 (erase-buffer)
8356 (insert note)
8357 (goto-char (point-min))
8358 (while (re-search-forward "\\\\n" nil t)
8359 (replace-match "\n" t t))
8360 (goto-char (point-min))
8361 (select-window win)
8362 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8364 (defun org-agenda-remove-flag (marker)
8365 "Remove the FLAGGED tag and any flagging note in the entry."
8366 (let (newhead)
8367 (org-with-point-at marker
8368 (org-toggle-tag "FLAGGED" 'off)
8369 (org-entry-delete nil "THEFLAGGINGNOTE")
8370 (setq newhead (org-get-heading)))
8371 (org-agenda-change-all-lines newhead marker)
8372 (message "Entry unflaged")))
8374 (defun org-agenda-get-any-marker (&optional pos)
8375 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8376 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8378 ;;; Appointment reminders
8380 (defvar appt-time-msg-list)
8382 ;;;###autoload
8383 (defun org-agenda-to-appt (&optional refresh filter)
8384 "Activate appointments found in `org-agenda-files'.
8385 With a \\[universal-argument] prefix, refresh the list of
8386 appointments.
8388 If FILTER is t, interactively prompt the user for a regular
8389 expression, and filter out entries that don't match it.
8391 If FILTER is a string, use this string as a regular expression
8392 for filtering entries out.
8394 FILTER can also be an alist with the car of each cell being
8395 either 'headline or 'category. For example:
8397 '((headline \"IMPORTANT\")
8398 (category \"Work\"))
8400 will only add headlines containing IMPORTANT or headlines
8401 belonging to the \"Work\" category."
8402 (interactive "P")
8403 (if refresh (setq appt-time-msg-list nil))
8404 (if (eq filter t)
8405 (setq filter (read-from-minibuffer "Regexp filter: ")))
8406 (let* ((cnt 0) ; count added events
8407 (org-agenda-new-buffers nil)
8408 (org-deadline-warning-days 0)
8409 ;; Do not use `org-today' here because appt only takes
8410 ;; time and without date as argument, so it may pass wrong
8411 ;; information otherwise
8412 (today (org-date-to-gregorian
8413 (time-to-days (current-time))))
8414 (org-agenda-restrict nil)
8415 (files (org-agenda-files 'unrestricted)) entries file)
8416 ;; Get all entries which may contain an appt
8417 (org-prepare-agenda-buffers files)
8418 (while (setq file (pop files))
8419 (setq entries
8420 (append entries
8421 (org-agenda-get-day-entries
8422 file today :timestamp :scheduled :deadline))))
8423 (setq entries (delq nil entries))
8424 ;; Map thru entries and find if we should filter them out
8425 (mapc
8426 (lambda(x)
8427 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8428 (cat (get-text-property 1 'org-category x))
8429 (tod (get-text-property 1 'time-of-day x))
8430 (ok (or (null filter)
8431 (and (stringp filter) (string-match filter evt))
8432 (and (listp filter)
8433 (or (string-match
8434 (cadr (assoc 'category filter)) cat)
8435 (string-match
8436 (cadr (assoc 'headline filter)) evt))))))
8437 ;; FIXME: Shall we remove text-properties for the appt text?
8438 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8439 (when (and ok tod)
8440 (setq tod (concat "00" (number-to-string tod))
8441 tod (when (string-match
8442 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8443 (concat (match-string 1 tod) ":"
8444 (match-string 2 tod))))
8445 (appt-add tod evt)
8446 (setq cnt (1+ cnt))))) entries)
8447 (org-release-buffers org-agenda-new-buffers)
8448 (if (eq cnt 0)
8449 (message "No event to add")
8450 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8452 (defun org-agenda-todayp (date)
8453 "Does DATE mean today, when considering `org-extend-today-until'?"
8454 (let ((today (org-today))
8455 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8456 date)))
8457 (eq date today)))
8459 (defun org-agenda-todo-yesterday (&optional arg)
8460 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8461 (interactive "P")
8462 (let* ((hour (third (decode-time
8463 (org-current-time))))
8464 (org-extend-today-until (1+ hour)))
8465 (org-agenda-todo arg)))
8467 (provide 'org-agenda)
8469 ;;; org-agenda.el ends here