Merge branch 'hotfix'
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-agenda.el
blobbaa8d80c705c383b5f1461176f96f06c1c3f4ff3
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2012 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 convenient 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 calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 ;; Keep custom values for `org-agenda-filter-preset' compatible with
249 ;; the new variable `org-agenda-tag-filter-preset'.
250 (if (fboundp 'defvaralias)
251 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
252 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
254 (defconst org-agenda-custom-commands-local-options
255 `(repeat :tag "Local settings for this command. Remember to quote values"
256 (choice :tag "Setting"
257 (list :tag "Heading for this block"
258 (const org-agenda-overriding-header)
259 (string :tag "Headline"))
260 (list :tag "Files to be searched"
261 (const org-agenda-files)
262 (list
263 (const :format "" quote)
264 (repeat (file))))
265 (list :tag "Sorting strategy"
266 (const org-agenda-sorting-strategy)
267 (list
268 (const :format "" quote)
269 (repeat
270 ,org-sorting-choice)))
271 (list :tag "Prefix format"
272 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
273 (string))
274 (list :tag "Number of days in agenda"
275 (const org-agenda-span)
276 (choice (const :tag "Day" 'day)
277 (const :tag "Week" 'week)
278 (const :tag "Month" 'month)
279 (const :tag "Year" 'year)
280 (integer :tag "Custom")))
281 (list :tag "Fixed starting date"
282 (const org-agenda-start-day)
283 (string :value "2007-11-01"))
284 (list :tag "Start on day of week"
285 (const org-agenda-start-on-weekday)
286 (choice :value 1
287 (const :tag "Today" nil)
288 (integer :tag "Weekday No.")))
289 (list :tag "Include data from diary"
290 (const org-agenda-include-diary)
291 (boolean))
292 (list :tag "Deadline Warning days"
293 (const org-deadline-warning-days)
294 (integer :value 1))
295 (list :tag "Category filter preset"
296 (const org-agenda-category-filter-preset)
297 (list
298 (const :format "" quote)
299 (repeat
300 (string :tag "+category or -category"))))
301 (list :tag "Tags filter preset"
302 (const org-agenda-tag-filter-preset)
303 (list
304 (const :format "" quote)
305 (repeat
306 (string :tag "+tag or -tag"))))
307 (list :tag "Set daily/weekly entry types"
308 (const org-agenda-entry-types)
309 (list
310 (const :format "" quote)
311 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
312 (const :deadline)
313 (const :scheduled)
314 (const :timestamp)
315 (const :sexp))))
316 (list :tag "Standard skipping condition"
317 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
318 (const org-agenda-skip-function)
319 (list
320 (const :format "" quote)
321 (list
322 (choice
323 :tag "Skipping range"
324 (const :tag "Skip entry" org-agenda-skip-entry-if)
325 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
326 (repeat :inline t :tag "Conditions for skipping"
327 (choice
328 :tag "Condition type"
329 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
330 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
331 (list :tag "TODO state is" :inline t
332 (const 'todo)
333 (choice
334 (const :tag "any not-done state" 'todo)
335 (const :tag "any done state" 'done)
336 (const :tag "any state" 'any)
337 (list :tag "Keyword list"
338 (const :format "" quote)
339 (repeat (string :tag "Keyword")))))
340 (list :tag "TODO state is not" :inline t
341 (const 'nottodo)
342 (choice
343 (const :tag "any not-done state" 'todo)
344 (const :tag "any done state" 'done)
345 (const :tag "any state" 'any)
346 (list :tag "Keyword list"
347 (const :format "" quote)
348 (repeat (string :tag "Keyword")))))
349 (const :tag "scheduled" 'scheduled)
350 (const :tag "not scheduled" 'notscheduled)
351 (const :tag "deadline" 'deadline)
352 (const :tag "no deadline" 'notdeadline)
353 (const :tag "timestamp" 'timestamp)
354 (const :tag "no timestamp" 'nottimestamp))))))
355 (list :tag "Non-standard skipping condition"
356 :value (org-agenda-skip-function)
357 (const org-agenda-skip-function)
358 (sexp :tag "Function or form (quoted!)"))
359 (list :tag "Any variable"
360 (variable :tag "Variable")
361 (sexp :tag "Value (sexp)"))))
362 "Selection of examples for agenda command settings.
363 This will be spliced into the custom type of
364 `org-agenda-custom-commands'.")
367 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
368 ((agenda "") (alltodo))))
369 "Custom commands for the agenda.
370 These commands will be offered on the splash screen displayed by the
371 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
373 (key desc type match settings files)
375 key The key (one or more characters as a string) to be associated
376 with the command.
377 desc A description of the command, when omitted or nil, a default
378 description is built using MATCH.
379 type The command type, any of the following symbols:
380 agenda The daily/weekly agenda.
381 todo Entries with a specific TODO keyword, in all agenda files.
382 search Entries containing search words entry or headline.
383 tags Tags/Property/TODO match in all agenda files.
384 tags-todo Tags/P/T match in all agenda files, TODO entries only.
385 todo-tree Sparse tree of specific TODO keyword in *current* file.
386 tags-tree Sparse tree with all tags matches in *current* file.
387 occur-tree Occur sparse tree for *current* file.
388 ... A user-defined function.
389 match What to search for:
390 - a single keyword for TODO keyword searches
391 - a tags match expression for tags searches
392 - a word search expression for text searches.
393 - a regular expression for occur searches
394 For all other commands, this should be the empty string.
395 settings A list of option settings, similar to that in a let form, so like
396 this: ((opt1 val1) (opt2 val2) ...). The values will be
397 evaluated at the moment of execution, so quote them when needed.
398 files A list of files file to write the produced agenda buffer to
399 with the command `org-store-agenda-views'.
400 If a file name ends in \".html\", an HTML version of the buffer
401 is written out. If it ends in \".ps\", a postscript version is
402 produced. Otherwise, only the plain text is written to the file.
404 You can also define a set of commands, to create a composite agenda buffer.
405 In this case, an entry looks like this:
407 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
409 where
411 desc A description string to be displayed in the dispatcher menu.
412 cmd An agenda command, similar to the above. However, tree commands
413 are not allowed, but instead you can get agenda and global todo list.
414 So valid commands for a set are:
415 (agenda \"\" settings)
416 (alltodo \"\" settings)
417 (stuck \"\" settings)
418 (todo \"match\" settings files)
419 (search \"match\" settings files)
420 (tags \"match\" settings files)
421 (tags-todo \"match\" settings files)
423 Each command can carry a list of options, and another set of options can be
424 given for the whole set of commands. Individual command options take
425 precedence over the general options.
427 When using several characters as key to a command, the first characters
428 are prefix commands. For the dispatcher to display useful information, you
429 should provide a description for the prefix, like
431 (setq org-agenda-custom-commands
432 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
433 (\"hl\" tags \"+HOME+Lisa\")
434 (\"hp\" tags \"+HOME+Peter\")
435 (\"hk\" tags \"+HOME+Kim\")))"
436 :group 'org-agenda-custom-commands
437 :type `(repeat
438 (choice :value ("x" "Describe command here" tags "" nil)
439 (list :tag "Single command"
440 (string :tag "Access Key(s) ")
441 (option (string :tag "Description"))
442 (choice
443 (const :tag "Agenda" agenda)
444 (const :tag "TODO list" alltodo)
445 (const :tag "Search words" search)
446 (const :tag "Stuck projects" stuck)
447 (const :tag "Tags/Property match (all agenda files)" tags)
448 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
449 (const :tag "TODO keyword search (all agenda files)" todo)
450 (const :tag "Tags sparse tree (current buffer)" tags-tree)
451 (const :tag "TODO keyword tree (current buffer)" todo-tree)
452 (const :tag "Occur tree (current buffer)" occur-tree)
453 (sexp :tag "Other, user-defined function"))
454 (string :tag "Match (only for some commands)")
455 ,org-agenda-custom-commands-local-options
456 (option (repeat :tag "Export" (file :tag "Export to"))))
457 (list :tag "Command series, all agenda files"
458 (string :tag "Access Key(s)")
459 (string :tag "Description ")
460 (repeat :tag "Component"
461 (choice
462 (list :tag "Agenda"
463 (const :format "" agenda)
464 (const :tag "" :format "" "")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "TODO list (all keywords)"
467 (const :format "" alltodo)
468 (const :tag "" :format "" "")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "Search words"
471 (const :format "" search)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Stuck projects"
475 (const :format "" stuck)
476 (const :tag "" :format "" "")
477 ,org-agenda-custom-commands-local-options)
478 (list :tag "Tags search"
479 (const :format "" tags)
480 (string :tag "Match")
481 ,org-agenda-custom-commands-local-options)
482 (list :tag "Tags search, TODO entries only"
483 (const :format "" tags-todo)
484 (string :tag "Match")
485 ,org-agenda-custom-commands-local-options)
486 (list :tag "TODO keyword search"
487 (const :format "" todo)
488 (string :tag "Match")
489 ,org-agenda-custom-commands-local-options)
490 (list :tag "Other, user-defined function"
491 (symbol :tag "function")
492 (string :tag "Match")
493 ,org-agenda-custom-commands-local-options)))
495 (repeat :tag "Settings for entire command set"
496 (list (variable :tag "Any variable")
497 (sexp :tag "Value")))
498 (option (repeat :tag "Export" (file :tag "Export to"))))
499 (cons :tag "Prefix key documentation"
500 (string :tag "Access Key(s)")
501 (string :tag "Description ")))))
503 (defcustom org-agenda-query-register ?o
504 "The register holding the current query string.
505 The purpose of this is that if you construct a query string interactively,
506 you can then use it to define a custom command."
507 :group 'org-agenda-custom-commands
508 :type 'character)
510 (defcustom org-stuck-projects
511 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
512 "How to identify stuck projects.
513 This is a list of four items:
514 1. A tags/todo/property matcher string that is used to identify a project.
515 See the manual for a description of tag and property searches.
516 The entire tree below a headline matched by this is considered one project.
517 2. A list of TODO keywords identifying non-stuck projects.
518 If the project subtree contains any headline with one of these todo
519 keywords, the project is considered to be not stuck. If you specify
520 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
521 3. A list of tags identifying non-stuck projects.
522 If the project subtree contains any headline with one of these tags,
523 the project is considered to be not stuck. If you specify \"*\" as
524 a tag, any tag will mark the project unstuck. Note that this is about
525 the explicit presence of a tag somewhere in the subtree, inherited
526 tags to not count here. If inherited tags make a project not stuck,
527 use \"-TAG\" in the tags part of the matcher under (1.) above.
528 4. An arbitrary regular expression matching non-stuck projects.
530 If the project turns out to be not stuck, search continues also in the
531 subtree to see if any of the subtasks have project status.
533 See also the variable `org-tags-match-list-sublevels' which applies
534 to projects matched by this search as well.
536 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
537 or `C-c a #' to produce the list."
538 :group 'org-agenda-custom-commands
539 :type '(list
540 (string :tag "Tags/TODO match to identify a project")
541 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
542 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
543 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
545 (defcustom org-agenda-filter-effort-default-operator "<"
546 "The default operator for effort estimate filtering.
547 If you select an effort estimate limit without first pressing an operator,
548 this one will be used."
549 :group 'org-agenda-custom-commands
550 :type '(choice (const :tag "less or equal" "<")
551 (const :tag "greater or equal"">")
552 (const :tag "equal" "=")))
554 (defgroup org-agenda-skip nil
555 "Options concerning skipping parts of agenda files."
556 :tag "Org Agenda Skip"
557 :group 'org-agenda)
559 (defcustom org-agenda-skip-function-global nil
560 "Function to be called at each match during agenda construction.
561 If this function returns nil, the current match should not be skipped.
562 If the function decided to skip an agenda match, is must return the
563 buffer position from which the search should be continued.
564 This may also be a Lisp form, which will be evaluated.
566 This variable will be applied to every agenda match, including
567 tags/property searches and TODO lists. So try to make the test function
568 do its checking as efficiently as possible. To implement a skipping
569 condition just for specific agenda commands, use the variable
570 `org-agenda-skip-function' which can be set in the options section
571 of custom agenda commands."
572 :group 'org-agenda-skip
573 :type 'sexp)
575 (defgroup org-agenda-daily/weekly nil
576 "Options concerning the daily/weekly agenda."
577 :tag "Org Agenda Daily/Weekly"
578 :group 'org-agenda)
579 (defgroup org-agenda-todo-list nil
580 "Options concerning the global todo list agenda view."
581 :tag "Org Agenda Todo List"
582 :group 'org-agenda)
583 (defgroup org-agenda-match-view nil
584 "Options concerning the general tags/property/todo match agenda view."
585 :tag "Org Agenda Match View"
586 :group 'org-agenda)
587 (defgroup org-agenda-search-view nil
588 "Options concerning the general tags/property/todo match agenda view."
589 :tag "Org Agenda Match View"
590 :group 'org-agenda)
592 (defvar org-agenda-archives-mode nil
593 "Non-nil means the agenda will include archived items.
594 If this is the symbol `trees', trees in the selected agenda scope
595 that are marked with the ARCHIVE tag will be included anyway. When this is
596 t, also all archive files associated with the current selection of agenda
597 files will be included.")
599 (defcustom org-agenda-skip-comment-trees t
600 "Non-nil means skip trees that start with the COMMENT keyword.
601 When nil, these trees are also scanned by agenda commands."
602 :group 'org-agenda-skip
603 :type 'boolean)
605 (defcustom org-agenda-todo-list-sublevels t
606 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
607 When nil, the sublevels of a TODO entry are not checked, resulting in
608 potentially much shorter TODO lists."
609 :group 'org-agenda-skip
610 :group 'org-agenda-todo-list
611 :type 'boolean)
613 (defcustom org-agenda-todo-ignore-with-date nil
614 "Non-nil means don't show entries with a date in the global todo list.
615 You can use this if you prefer to mark mere appointments with a TODO keyword,
616 but don't want them to show up in the TODO list.
617 When this is set, it also covers deadlines and scheduled items, the settings
618 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
619 will be ignored.
620 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
621 :group 'org-agenda-skip
622 :group 'org-agenda-todo-list
623 :type 'boolean)
625 (defcustom org-agenda-todo-ignore-timestamp nil
626 "Non-nil means don't show entries with a timestamp.
627 This applies when creating the global todo list.
628 Valid values are:
630 past Don't show entries for today or in the past.
632 future Don't show entries with a timestamp in the future.
633 The idea behind this is that if it has a future
634 timestamp, you don't want to think about it until the
635 date.
637 all Don't show any entries with a timestamp in the global todo list.
638 The idea behind this is that by setting a timestamp, you
639 have already \"taken care\" of this item.
641 This variable can also have an integer as a value. If positive (N),
642 todos with a timestamp N or more days in the future will be ignored. If
643 negative (-N), todos with a timestamp N or more days in the past will be
644 ignored. If 0, todos with a timestamp either today or in the future will
645 be ignored. For example, a value of -1 will exclude todos with a
646 timestamp in the past (yesterday or earlier), while a value of 7 will
647 exclude todos with a timestamp a week or more in the future.
649 See also `org-agenda-todo-ignore-with-date'.
650 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651 to make his option also apply to the tags-todo list."
652 :group 'org-agenda-skip
653 :group 'org-agenda-todo-list
654 :version "24.1"
655 :type '(choice
656 (const :tag "Ignore future timestamp todos" future)
657 (const :tag "Ignore past or present timestamp todos" past)
658 (const :tag "Ignore all timestamp todos" all)
659 (const :tag "Show timestamp todos" nil)
660 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
662 (defcustom org-agenda-todo-ignore-scheduled nil
663 "Non-nil means, ignore some scheduled TODO items when making TODO list.
664 This applies when creating the global todo list.
665 Valid values are:
667 past Don't show entries scheduled today or in the past.
669 future Don't show entries scheduled in the future.
670 The idea behind this is that by scheduling it, you don't want to
671 think about it until the scheduled date.
673 all Don't show any scheduled entries in the global todo list.
674 The idea behind this is that by scheduling it, you have already
675 \"taken care\" of this item.
677 t Same as `all', for backward compatibility.
679 This variable can also have an integer as a value. See
680 `org-agenda-todo-ignore-timestamp' for more details.
682 See also `org-agenda-todo-ignore-with-date'.
683 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
684 to make his option also apply to the tags-todo list."
685 :group 'org-agenda-skip
686 :group 'org-agenda-todo-list
687 :type '(choice
688 (const :tag "Ignore future-scheduled todos" future)
689 (const :tag "Ignore past- or present-scheduled todos" past)
690 (const :tag "Ignore all scheduled todos" all)
691 (const :tag "Ignore all scheduled todos (compatibility)" t)
692 (const :tag "Show scheduled todos" nil)
693 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
695 (defcustom org-agenda-todo-ignore-deadlines nil
696 "Non-nil means ignore some deadlined TODO items when making TODO list.
697 There are different motivations for using different values, please think
698 carefully when configuring this variable.
700 This applies when creating the global todo list.
701 Valid values are:
703 near Don't show near deadline entries. A deadline is near when it is
704 closer than `org-deadline-warning-days' days. The idea behind this
705 is that such items will appear in the agenda anyway.
707 far Don't show TODO entries where a deadline has been defined, but
708 the deadline is not near. This is useful if you don't want to
709 use the todo list to figure out what to do now.
711 past Don't show entries with a deadline timestamp for today or in the past.
713 future Don't show entries with a deadline timestamp in the future, not even
714 when they become `near' ones. Use it with caution.
716 all Ignore all TODO entries that do have a deadline.
718 t Same as `near', for backward compatibility.
720 This variable can also have an integer as a value. See
721 `org-agenda-todo-ignore-timestamp' for more details.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :type '(choice
729 (const :tag "Ignore near deadlines" near)
730 (const :tag "Ignore near deadlines (compatibility)" t)
731 (const :tag "Ignore far deadlines" far)
732 (const :tag "Ignore all TODOs with a deadlines" all)
733 (const :tag "Show all TODOs, even if they have a deadline" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-tags-todo-honor-ignore-options nil
737 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
738 The variables
739 `org-agenda-todo-ignore-with-date',
740 `org-agenda-todo-ignore-timestamp',
741 `org-agenda-todo-ignore-scheduled',
742 `org-agenda-todo-ignore-deadlines'
743 make the global TODO list skip entries that have time stamps of certain
744 kinds. If this option is set, the same options will also apply for the
745 tags-todo search, which is the general tags/property matcher
746 restricted to unfinished TODO entries only."
747 :group 'org-agenda-skip
748 :group 'org-agenda-todo-list
749 :group 'org-agenda-match-view
750 :type 'boolean)
752 (defcustom org-agenda-skip-scheduled-if-done nil
753 "Non-nil means don't show scheduled items in agenda when they are done.
754 This is relevant for the daily/weekly agenda, not for the TODO list. And
755 it applies only to the actual date of the scheduling. Warnings about
756 an item with a past scheduling dates are always turned off when the item
757 is DONE."
758 :group 'org-agenda-skip
759 :group 'org-agenda-daily/weekly
760 :type 'boolean)
762 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
763 "Non-nil means skip scheduling line if same entry shows because of deadline.
764 In the agenda of today, an entry can show up multiple times because
765 it is both scheduled and has a nearby deadline, and maybe a plain time
766 stamp as well.
767 When this variable is t, then only the deadline is shown and the fact that
768 the entry is scheduled today or was scheduled previously is not shown.
769 When this variable is nil, the entry will be shown several times. When
770 the variable is the symbol `not-today', then skip scheduled previously,
771 but not scheduled today."
772 :group 'org-agenda-skip
773 :group 'org-agenda-daily/weekly
774 :type '(choice
775 (const :tag "Never" nil)
776 (const :tag "Always" t)
777 (const :tag "Not when scheduled today" not-today)))
779 (defcustom org-agenda-skip-deadline-if-done nil
780 "Non-nil means don't show deadlines when the corresponding item is done.
781 When nil, the deadline is still shown and should give you a happy feeling.
782 This is relevant for the daily/weekly agenda. And it applied only to the
783 actually date of the deadline. Warnings about approaching and past-due
784 deadlines are always turned off when the item is DONE."
785 :group 'org-agenda-skip
786 :group 'org-agenda-daily/weekly
787 :type 'boolean)
789 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
790 "Non-nil means skip deadline prewarning when entry is also scheduled.
791 This will apply on all days where a prewarning for the deadline would
792 be shown, but not at the day when the entry is actually due. On that day,
793 the deadline will be shown anyway.
794 This variable may be set to nil, t, or a number which will then give
795 the number of days before the actual deadline when the prewarnings
796 should resume.
797 This can be used in a workflow where the first showing of the deadline will
798 trigger you to schedule it, and then you don't want to be reminded of it
799 because you will take care of it on the day when scheduled."
800 :group 'org-agenda-skip
801 :group 'org-agenda-daily/weekly
802 :version "24.1"
803 :type '(choice
804 (const :tag "Alwas show prewarning" nil)
805 (const :tag "Remove prewarning if entry is scheduled" t)
806 (integer :tag "Restart prewarning N days before deadline")))
808 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
809 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
810 When non-nil, after the search for timestamps has matched once in an
811 entry, the rest of the entry will not be searched."
812 :group 'org-agenda-skip
813 :type 'boolean)
815 (defcustom org-agenda-skip-timestamp-if-done nil
816 "Non-nil means don't select item by timestamp or -range if it is DONE."
817 :group 'org-agenda-skip
818 :group 'org-agenda-daily/weekly
819 :type 'boolean)
821 (defcustom org-agenda-dim-blocked-tasks t
822 "Non-nil means dim blocked tasks in the agenda display.
823 This causes some overhead during agenda construction, but if you
824 have turned on `org-enforce-todo-dependencies',
825 `org-enforce-todo-checkbox-dependencies', or any other blocking
826 mechanism, this will create useful feedback in the agenda.
828 Instead of t, this variable can also have the value `invisible'.
829 Then blocked tasks will be invisible and only become visible when
830 they become unblocked. An exemption to this behavior is when a task is
831 blocked because of unchecked checkboxes below it. Since checkboxes do
832 not show up in the agenda views, making this task invisible you remove any
833 trace from agenda views that there is something to do. Therefore, a task
834 that is blocked because of checkboxes will never be made invisible, it
835 will only be dimmed."
836 :group 'org-agenda-daily/weekly
837 :group 'org-agenda-todo-list
838 :type '(choice
839 (const :tag "Do not dim" nil)
840 (const :tag "Dim to a gray face" t)
841 (const :tag "Make invisible" invisible)))
843 (defcustom org-timeline-show-empty-dates 3
844 "Non-nil means `org-timeline' also shows dates without an entry.
845 When nil, only the days which actually have entries are shown.
846 When t, all days between the first and the last date are shown.
847 When an integer, show also empty dates, but if there is a gap of more than
848 N days, just insert a special line indicating the size of the gap."
849 :group 'org-agenda-skip
850 :type '(choice
851 (const :tag "None" nil)
852 (const :tag "All" t)
853 (integer :tag "at most")))
855 (defgroup org-agenda-startup nil
856 "Options concerning initial settings in the Agenda in Org Mode."
857 :tag "Org Agenda Startup"
858 :group 'org-agenda)
860 (defcustom org-agenda-menu-show-matcher t
861 "Non-nil means show the match string in the agenda dispatcher menu.
862 When nil, the matcher string is not shown, but is put into the help-echo
863 property so than moving the mouse over the command shows it.
864 Setting it to nil is good if matcher strings are very long and/or if
865 you want to use two-column display (see `org-agenda-menu-two-column')."
866 :group 'org-agenda
867 :version "24.1"
868 :type 'boolean)
870 (defcustom org-agenda-menu-two-column nil
871 "Non-nil means, use two columns to show custom commands in the dispatcher.
872 If you use this, you probably want to set `org-agenda-menu-show-matcher'
873 to nil."
874 :group 'org-agenda
875 :version "24.1"
876 :type 'boolean)
878 (defcustom org-finalize-agenda-hook nil
879 "Hook run just before displaying an agenda buffer."
880 :group 'org-agenda-startup
881 :type 'hook)
883 (defcustom org-agenda-mouse-1-follows-link nil
884 "Non-nil means mouse-1 on a link will follow the link in the agenda.
885 A longer mouse click will still set point. Does not work on XEmacs.
886 Needs to be set before org.el is loaded."
887 :group 'org-agenda-startup
888 :type 'boolean)
890 (defcustom org-agenda-start-with-follow-mode nil
891 "The initial value of follow mode in a newly created agenda window."
892 :group 'org-agenda-startup
893 :type 'boolean)
895 (defcustom org-agenda-follow-indirect nil
896 "Non-nil means `org-agenda-follow-mode' displays only the
897 current item's tree, in an indirect buffer."
898 :group 'org-agenda
899 :version "24.1"
900 :type 'boolean)
902 (defcustom org-agenda-show-outline-path t
903 "Non-nil means show outline path in echo area after line motion."
904 :group 'org-agenda-startup
905 :type 'boolean)
907 (defcustom org-agenda-start-with-entry-text-mode nil
908 "The initial value of entry-text-mode in a newly created agenda window."
909 :group 'org-agenda-startup
910 :type 'boolean)
912 (defcustom org-agenda-entry-text-maxlines 5
913 "Number of text lines to be added when `E' is pressed in the agenda.
915 Note that this variable only used during agenda display. Add add entry text
916 when exporting the agenda, configure the variable
917 `org-agenda-add-entry-ext-maxlines'."
918 :group 'org-agenda
919 :type 'integer)
921 (defcustom org-agenda-entry-text-exclude-regexps nil
922 "List of regular expressions to clean up entry text.
923 The complete matches of all regular expressions in this list will be
924 removed from entry text before it is shown in the agenda."
925 :group 'org-agenda
926 :type '(repeat (regexp)))
928 (defvar org-agenda-entry-text-cleanup-hook nil
929 "Hook that is run after basic cleanup of entry text to be shown in agenda.
930 This cleanup is done in a temporary buffer, so the function may inspect and
931 change the entire buffer.
932 Some default stuff like drawers and scheduling/deadline dates will already
933 have been removed when this is called, as will any matches for regular
934 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
936 (defvar org-agenda-include-inactive-timestamps nil
937 "Non-nil means include inactive time stamps in agenda and timeline.")
939 (defgroup org-agenda-windows nil
940 "Options concerning the windows used by the Agenda in Org Mode."
941 :tag "Org Agenda Windows"
942 :group 'org-agenda)
944 (defcustom org-agenda-window-setup 'reorganize-frame
945 "How the agenda buffer should be displayed.
946 Possible values for this option are:
948 current-window Show agenda in the current window, keeping all other windows.
949 other-window Use `switch-to-buffer-other-window' to display agenda.
950 reorganize-frame Show only two windows on the current frame, the current
951 window and the agenda.
952 other-frame Use `switch-to-buffer-other-frame' to display agenda.
953 Also, when exiting the agenda, kill that frame.
954 See also the variable `org-agenda-restore-windows-after-quit'."
955 :group 'org-agenda-windows
956 :type '(choice
957 (const current-window)
958 (const other-frame)
959 (const other-window)
960 (const reorganize-frame)))
962 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
963 "The min and max height of the agenda window as a fraction of frame height.
964 The value of the variable is a cons cell with two numbers between 0 and 1.
965 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
966 :group 'org-agenda-windows
967 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
969 (defcustom org-agenda-restore-windows-after-quit nil
970 "Non-nil means restore window configuration upon exiting agenda.
971 Before the window configuration is changed for displaying the agenda,
972 the current status is recorded. When the agenda is exited with
973 `q' or `x' and this option is set, the old state is restored. If
974 `org-agenda-window-setup' is `other-frame', the value of this
975 option will be ignored."
976 :group 'org-agenda-windows
977 :type 'boolean)
979 (defcustom org-agenda-ndays nil
980 "Number of days to include in overview display.
981 Should be 1 or 7.
982 Obsolete, see `org-agenda-span'."
983 :group 'org-agenda-daily/weekly
984 :type 'integer)
986 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
988 (defcustom org-agenda-span 'week
989 "Number of days to include in overview display.
990 Can be day, week, month, year, or any number of days.
991 Custom commands can set this variable in the options section."
992 :group 'org-agenda-daily/weekly
993 :type '(choice (const :tag "Day" day)
994 (const :tag "Week" week)
995 (const :tag "Month" month)
996 (const :tag "Year" year)
997 (integer :tag "Custom")))
999 (defcustom org-agenda-start-on-weekday 1
1000 "Non-nil means start the overview always on the specified weekday.
1001 0 denotes Sunday, 1 denotes Monday etc.
1002 When nil, always start on the current day.
1003 Custom commands can set this variable in the options section."
1004 :group 'org-agenda-daily/weekly
1005 :type '(choice (const :tag "Today" nil)
1006 (integer :tag "Weekday No.")))
1008 (defcustom org-agenda-show-all-dates t
1009 "Non-nil means `org-agenda' shows every day in the selected range.
1010 When nil, only the days which actually have entries are shown."
1011 :group 'org-agenda-daily/weekly
1012 :type 'boolean)
1014 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1015 "Format string for displaying dates in the agenda.
1016 Used by the daily/weekly agenda and by the timeline. This should be
1017 a format string understood by `format-time-string', or a function returning
1018 the formatted date as a string. The function must take a single argument,
1019 a calendar-style date list like (month day year)."
1020 :group 'org-agenda-daily/weekly
1021 :type '(choice
1022 (string :tag "Format string")
1023 (function :tag "Function")))
1025 (defun org-agenda-format-date-aligned (date)
1026 "Format a date string for display in the daily/weekly agenda, or timeline.
1027 This function makes sure that dates are aligned for easy reading."
1028 (require 'cal-iso)
1029 (let* ((dayname (calendar-day-name date))
1030 (day (cadr date))
1031 (day-of-week (calendar-day-of-week date))
1032 (month (car date))
1033 (monthname (calendar-month-name month))
1034 (year (nth 2 date))
1035 (iso-week (org-days-to-iso-week
1036 (calendar-absolute-from-gregorian date)))
1037 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1038 (1- year))
1039 ((and (= month 12) (<= iso-week 1))
1040 (1+ year))
1041 (t year)))
1042 (weekstring (if (= day-of-week 1)
1043 (format " W%02d" iso-week)
1044 "")))
1045 (format "%-10s %2d %s %4d%s"
1046 dayname day monthname year weekstring)))
1048 (defcustom org-agenda-time-leading-zero nil
1049 "Non-nil means use leading zero for military times in agenda.
1050 For example, 9:30am would become 09:30 rather than 9:30."
1051 :group 'org-agenda-daily/weekly
1052 :version "24.1"
1053 :type 'boolean)
1055 (defcustom org-agenda-timegrid-use-ampm nil
1056 "When set, show AM/PM style timestamps on the timegrid."
1057 :group 'org-agenda
1058 :version "24.1"
1059 :type 'boolean)
1061 (defun org-agenda-time-of-day-to-ampm (time)
1062 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1063 (let* ((hour-number (string-to-number (substring time 0 -3)))
1064 (minute (substring time -2))
1065 (ampm "am"))
1066 (cond
1067 ((equal hour-number 12)
1068 (setq ampm "pm"))
1069 ((> hour-number 12)
1070 (setq ampm "pm")
1071 (setq hour-number (- hour-number 12))))
1072 (concat
1073 (if org-agenda-time-leading-zero
1074 (format "%02d" hour-number)
1075 (format "%02s" (number-to-string hour-number)))
1076 ":" minute ampm)))
1078 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1079 "Conditionally convert TIME to AM/PM format
1080 based on `org-agenda-timegrid-use-ampm'"
1081 (if org-agenda-timegrid-use-ampm
1082 (org-agenda-time-of-day-to-ampm time)
1083 time))
1085 (defcustom org-agenda-weekend-days '(6 0)
1086 "Which days are weekend?
1087 These days get the special face `org-agenda-date-weekend' in the agenda
1088 and timeline buffers."
1089 :group 'org-agenda-daily/weekly
1090 :type '(set :greedy t
1091 (const :tag "Monday" 1)
1092 (const :tag "Tuesday" 2)
1093 (const :tag "Wednesday" 3)
1094 (const :tag "Thursday" 4)
1095 (const :tag "Friday" 5)
1096 (const :tag "Saturday" 6)
1097 (const :tag "Sunday" 0)))
1099 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1100 "Non-nil means jump to today when moving a past date forward in time.
1101 When using S-right in the agenda to move a a date forward, and the date
1102 stamp currently points to the past, the first key press will move it
1103 to today. WHen nil, just move one day forward even if the date stays
1104 in the past."
1105 :group 'org-agenda-daily/weekly
1106 :version "24.1"
1107 :type 'boolean)
1109 (defcustom org-agenda-include-diary nil
1110 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1111 Custom commands can set this variable in the options section."
1112 :group 'org-agenda-daily/weekly
1113 :type 'boolean)
1115 (defcustom org-agenda-include-deadlines t
1116 "If non-nil, include entries within their deadline warning period.
1117 Custom commands can set this variable in the options section."
1118 :group 'org-agenda-daily/weekly
1119 :version "24.1"
1120 :type 'boolean)
1122 (defcustom org-agenda-repeating-timestamp-show-all t
1123 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1124 When set to a list of strings, only show occurrences of repeating
1125 stamps for these TODO keywords. When nil, only one occurrence is
1126 shown, either today or the nearest into the future."
1127 :group 'org-agenda-daily/weekly
1128 :type '(choice
1129 (const :tag "Show repeating stamps" t)
1130 (repeat :tag "Show repeating stamps for these TODO keywords"
1131 (string :tag "TODO Keyword"))
1132 (const :tag "Don't show repeating stamps" nil)))
1134 (defcustom org-scheduled-past-days 10000
1135 "No. of days to continue listing scheduled items that are not marked DONE.
1136 When an item is scheduled on a date, it shows up in the agenda on this
1137 day and will be listed until it is marked done for the number of days
1138 given here."
1139 :group 'org-agenda-daily/weekly
1140 :type 'integer)
1142 (defcustom org-agenda-log-mode-items '(closed clock)
1143 "List of items that should be shown in agenda log mode.
1144 This list may contain the following symbols:
1146 closed Show entries that have been closed on that day.
1147 clock Show entries that have received clocked time on that day.
1148 state Show all logged state changes.
1149 Note that instead of changing this variable, you can also press `C-u l' in
1150 the agenda to display all available LOG items temporarily."
1151 :group 'org-agenda-daily/weekly
1152 :type '(set :greedy t (const closed) (const clock) (const state)))
1154 (defcustom org-agenda-clock-consistency-checks
1155 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1156 :gap-ok-around ("4:00")
1157 :default-face ((:background "DarkRed") (:foreground "white"))
1158 :overlap-face nil :gap-face nil :no-end-time-face nil
1159 :long-face nil :short-face nil)
1160 "This is a property list, with the following keys:
1162 :max-duration Mark clocking chunks that are longer than this time.
1163 This is a time string like \"HH:MM\", or the number
1164 of minutes as an integer.
1166 :min-duration Mark clocking chunks that are shorter that this.
1167 This is a time string like \"HH:MM\", or the number
1168 of minutes as an integer.
1170 :max-gap Mark gaps between clocking chunks that are longer than
1171 this duration. A number of minutes, or a string
1172 like \"HH:MM\".
1174 :gap-ok-around List of times during the day which are usually not working
1175 times. When a gap is detected, but the gap contains any
1176 of these times, the gap is *not* reported. For example,
1177 if this is (\"4:00\" \"13:00\") then gaps that contain
1178 4:00 in the morning (i.e. the night) and 13:00
1179 (i.e. a typical lunch time) do not cause a warning.
1180 You should have at least one time during the night in this
1181 list, or otherwise the first task each morning will trigger
1182 a warning because it follows a long gap.
1184 Furthermore, the following properties can be used to define faces for
1185 issue display.
1187 :default-face the default face, if the specific face is undefined
1188 :overlap-face face for overlapping clocks
1189 :gap-face face for gaps between clocks
1190 :no-end-time-face face for incomplete clocks
1191 :long-face face for clock intervals that are too long
1192 :short-face face for clock intervals that are too short"
1193 :group 'org-agenda-daily/weekly
1194 :group 'org-clock
1195 :version "24.1"
1196 :type 'plist)
1198 (defcustom org-agenda-log-mode-add-notes t
1199 "Non-nil means add first line of notes to log entries in agenda views.
1200 If a log item like a state change or a clock entry is associated with
1201 notes, the first line of these notes will be added to the entry in the
1202 agenda display."
1203 :group 'org-agenda-daily/weekly
1204 :type 'boolean)
1206 (defcustom org-agenda-start-with-log-mode nil
1207 "The initial value of log-mode in a newly created agenda window."
1208 :group 'org-agenda-startup
1209 :group 'org-agenda-daily/weekly
1210 :type 'boolean)
1212 (defcustom org-agenda-start-with-clockreport-mode nil
1213 "The initial value of clockreport-mode in a newly created agenda window."
1214 :group 'org-agenda-startup
1215 :group 'org-agenda-daily/weekly
1216 :type 'boolean)
1218 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1219 "Property list with parameters for the clocktable in clockreport mode.
1220 This is the display mode that shows a clock table in the daily/weekly
1221 agenda, the properties for this dynamic block can be set here.
1222 The usual clocktable parameters are allowed here, but you cannot set
1223 the properties :name, :tstart, :tend, :block, and :scope - these will
1224 be overwritten to make sure the content accurately reflects the
1225 current display in the agenda."
1226 :group 'org-agenda-daily/weekly
1227 :type 'plist)
1229 (defcustom org-agenda-search-view-always-boolean nil
1230 "Non-nil means the search string is interpreted as individual parts.
1232 The search string for search view can either be interpreted as a phrase,
1233 or as a list of snippets that define a boolean search for a number of
1234 strings.
1236 When this is non-nil, the string will be split on whitespace, and each
1237 snippet will be searched individually, and all must match in order to
1238 select an entry. A snippet is then a single string of non-white
1239 characters, or a string in double quotes, or a regexp in {} braces.
1240 If a snippet is preceded by \"-\", the snippet must *not* match.
1241 \"+\" is syntactic sugar for positive selection. Each snippet may
1242 be found as a full word or a partial word, but see the variable
1243 `org-agenda-search-view-force-full-words'.
1245 When this is nil, search will look for the entire search phrase as one,
1246 with each space character matching any amount of whitespace, including
1247 line breaks.
1249 Even when this is nil, you can still switch to Boolean search dynamically
1250 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1251 is a regexp marked with braces like \"{abc}\", this will also switch to
1252 boolean search."
1253 :group 'org-agenda-search-view
1254 :version "24.1"
1255 :type 'boolean)
1257 (if (fboundp 'defvaralias)
1258 (defvaralias 'org-agenda-search-view-search-words-only
1259 'org-agenda-search-view-always-boolean))
1261 (defcustom org-agenda-search-view-force-full-words nil
1262 "Non-nil means, search words must be matches as complete words.
1263 When nil, they may also match part of a word."
1264 :group 'org-agenda-search-view
1265 :version "24.1"
1266 :type 'boolean)
1268 (defgroup org-agenda-time-grid nil
1269 "Options concerning the time grid in the Org-mode Agenda."
1270 :tag "Org Agenda Time Grid"
1271 :group 'org-agenda)
1273 (defcustom org-agenda-search-headline-for-time t
1274 "Non-nil means search headline for a time-of-day.
1275 If the headline contains a time-of-day in one format or another, it will
1276 be used to sort the entry into the time sequence of items for a day.
1277 Some people have time stamps in the headline that refer to the creation
1278 time or so, and then this produces an unwanted side effect. If this is
1279 the case for your, use this variable to turn off searching the headline
1280 for a time."
1281 :group 'org-agenda-time-grid
1282 :type 'boolean)
1284 (defcustom org-agenda-use-time-grid t
1285 "Non-nil means show a time grid in the agenda schedule.
1286 A time grid is a set of lines for specific times (like every two hours between
1287 8:00 and 20:00). The items scheduled for a day at specific times are
1288 sorted in between these lines.
1289 For details about when the grid will be shown, and what it will look like, see
1290 the variable `org-agenda-time-grid'."
1291 :group 'org-agenda-time-grid
1292 :type 'boolean)
1294 (defcustom org-agenda-time-grid
1295 '((daily today require-timed)
1296 "----------------"
1297 (800 1000 1200 1400 1600 1800 2000))
1299 "The settings for time grid for agenda display.
1300 This is a list of three items. The first item is again a list. It contains
1301 symbols specifying conditions when the grid should be displayed:
1303 daily if the agenda shows a single day
1304 weekly if the agenda shows an entire week
1305 today show grid on current date, independent of daily/weekly display
1306 require-timed show grid only if at least one item has a time specification
1308 The second item is a string which will be placed behind the grid time.
1310 The third item is a list of integers, indicating the times that should have
1311 a grid line."
1312 :group 'org-agenda-time-grid
1313 :type
1314 '(list
1315 (set :greedy t :tag "Grid Display Options"
1316 (const :tag "Show grid in single day agenda display" daily)
1317 (const :tag "Show grid in weekly agenda display" weekly)
1318 (const :tag "Always show grid for today" today)
1319 (const :tag "Show grid only if any timed entries are present"
1320 require-timed)
1321 (const :tag "Skip grid times already present in an entry"
1322 remove-match))
1323 (string :tag "Grid String")
1324 (repeat :tag "Grid Times" (integer :tag "Time"))))
1326 (defcustom org-agenda-show-current-time-in-grid t
1327 "Non-nil means show the current time in the time grid."
1328 :group 'org-agenda-time-grid
1329 :version "24.1"
1330 :type 'boolean)
1332 (defcustom org-agenda-current-time-string
1333 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1334 "The string for the current time marker in the agenda."
1335 :group 'org-agenda-time-grid
1336 :version "24.1"
1337 :type 'string)
1339 (defgroup org-agenda-sorting nil
1340 "Options concerning sorting in the Org-mode Agenda."
1341 :tag "Org Agenda Sorting"
1342 :group 'org-agenda)
1344 (defcustom org-agenda-sorting-strategy
1345 '((agenda habit-down time-up priority-down category-keep)
1346 (todo priority-down category-keep)
1347 (tags priority-down category-keep)
1348 (search category-keep))
1349 "Sorting structure for the agenda items of a single day.
1350 This is a list of symbols which will be used in sequence to determine
1351 if an entry should be listed before another entry. The following
1352 symbols are recognized:
1354 time-up Put entries with time-of-day indications first, early first
1355 time-down Put entries with time-of-day indications first, late first
1356 category-keep Keep the default order of categories, corresponding to the
1357 sequence in `org-agenda-files'.
1358 category-up Sort alphabetically by category, A-Z.
1359 category-down Sort alphabetically by category, Z-A.
1360 tag-up Sort alphabetically by last tag, A-Z.
1361 tag-down Sort alphabetically by last tag, Z-A.
1362 priority-up Sort numerically by priority, high priority last.
1363 priority-down Sort numerically by priority, high priority first.
1364 todo-state-up Sort by todo state, tasks that are done last.
1365 todo-state-down Sort by todo state, tasks that are done first.
1366 effort-up Sort numerically by estimated effort, high effort last.
1367 effort-down Sort numerically by estimated effort, high effort first.
1368 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1369 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1370 habit-up Put entries that are habits first
1371 habit-down Put entries that are habits last
1372 alpha-up Sort headlines alphabetically
1373 alpha-down Sort headlines alphabetically, reversed
1375 The different possibilities will be tried in sequence, and testing stops
1376 if one comparison returns a \"not-equal\". For example, the default
1377 '(time-up category-keep priority-down)
1378 means: Pull out all entries having a specified time of day and sort them,
1379 in order to make a time schedule for the current day the first thing in the
1380 agenda listing for the day. Of the entries without a time indication, keep
1381 the grouped in categories, don't sort the categories, but keep them in
1382 the sequence given in `org-agenda-files'. Within each category sort by
1383 priority.
1385 Leaving out `category-keep' would mean that items will be sorted across
1386 categories by priority.
1388 Instead of a single list, this can also be a set of list for specific
1389 contents, with a context symbol in the car of the list, any of
1390 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1392 Custom commands can bind this variable in the options section."
1393 :group 'org-agenda-sorting
1394 :type `(choice
1395 (repeat :tag "General" ,org-sorting-choice)
1396 (list :tag "Individually"
1397 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1398 (repeat ,org-sorting-choice))
1399 (cons (const :tag "Strategy for TODO lists" todo)
1400 (repeat ,org-sorting-choice))
1401 (cons (const :tag "Strategy for Tags matches" tags)
1402 (repeat ,org-sorting-choice))
1403 (cons (const :tag "Strategy for search matches" search)
1404 (repeat ,org-sorting-choice)))))
1406 (defcustom org-agenda-cmp-user-defined nil
1407 "A function to define the comparison `user-defined'.
1408 This function must receive two arguments, agenda entry a and b.
1409 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1410 the user comparison, return nil.
1411 When this is defined, you can make `user-defined-up' and `user-defined-down'
1412 part of an agenda sorting strategy."
1413 :group 'org-agenda-sorting
1414 :type 'symbol)
1416 (defcustom org-sort-agenda-notime-is-late t
1417 "Non-nil means items without time are considered late.
1418 This is only relevant for sorting. When t, items which have no explicit
1419 time like 15:30 will be considered as 99:01, i.e. later than any items which
1420 do have a time. When nil, the default time is before 0:00. You can use this
1421 option to decide if the schedule for today should come before or after timeless
1422 agenda entries."
1423 :group 'org-agenda-sorting
1424 :type 'boolean)
1426 (defcustom org-sort-agenda-noeffort-is-high t
1427 "Non-nil means items without effort estimate are sorted as high effort.
1428 This also applies when filtering an agenda view with respect to the
1429 < or > effort operator. Then, tasks with no effort defined will be treated
1430 as tasks with high effort.
1431 When nil, such items are sorted as 0 minutes effort."
1432 :group 'org-agenda-sorting
1433 :type 'boolean)
1435 (defgroup org-agenda-line-format nil
1436 "Options concerning the entry prefix in the Org-mode agenda display."
1437 :tag "Org Agenda Line Format"
1438 :group 'org-agenda)
1440 (defcustom org-agenda-prefix-format
1441 '((agenda . " %i %-12:c%?-12t% s")
1442 (timeline . " % s")
1443 (todo . " %i %-12:c")
1444 (tags . " %i %-12:c")
1445 (search . " %i %-12:c"))
1446 "Format specifications for the prefix of items in the agenda views.
1447 An alist with five entries, each for the different agenda types. The
1448 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1449 The values are format strings.
1451 This format works similar to a printf format, with the following meaning:
1453 %c the category of the item, \"Diary\" for entries from the diary,
1454 or as given by the CATEGORY keyword or derived from the file name
1455 %e the effort required by the item
1456 %i the icon category of the item, see `org-agenda-category-icon-alist'
1457 %T the last tag of the item (ignore inherited tags, which come first)
1458 %t the HH:MM time-of-day specification if one applies to the entry
1459 %s Scheduling/Deadline information, a short string
1460 %(expression) Eval EXPRESSION and replace the control string
1461 by the result
1463 All specifiers work basically like the standard `%s' of printf, but may
1464 contain two additional characters: a question mark just after the `%'
1465 and a whitespace/punctuation character just before the final letter.
1467 If the first character after `%' is a question mark, the entire field
1468 will only be included if the corresponding value applies to the current
1469 entry. This is useful for fields which should have fixed width when
1470 present, but zero width when absent. For example, \"%?-12t\" will
1471 result in a 12 character time field if a time of the day is specified,
1472 but will completely disappear in entries which do not contain a time.
1474 If there is punctuation or whitespace character just before the final
1475 format letter, this character will be appended to the field value if
1476 the value is not empty. For example, the format \"%-12:c\" leads to
1477 \"Diary: \" if the category is \"Diary\". If the category were be
1478 empty, no additional colon would be inserted.
1480 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1481 which means:
1483 - Indent the line with two space characters
1484 - Give the category a 12 chars wide field, padded with whitespace on
1485 the right (because of `-'). Append a colon if there is a category
1486 (because of `:').
1487 - If there is a time-of-day, put it into a 12 chars wide field. If no
1488 time, don't put in an empty field, just skip it (because of '?').
1489 - Finally, put the scheduling information.
1491 See also the variables `org-agenda-remove-times-when-in-prefix' and
1492 `org-agenda-remove-tags'.
1494 Custom commands can set this variable in the options section."
1495 :type '(choice
1496 (string :tag "General format")
1497 (list :greedy t :tag "View dependent"
1498 (cons (const agenda) (string :tag "Format"))
1499 (cons (const timeline) (string :tag "Format"))
1500 (cons (const todo) (string :tag "Format"))
1501 (cons (const tags) (string :tag "Format"))
1502 (cons (const search) (string :tag "Format"))))
1503 :group 'org-agenda-line-format)
1505 (defvar org-prefix-format-compiled nil
1506 "The compiled version of the most recently used prefix format.
1507 See the variable `org-agenda-prefix-format'.")
1509 (defcustom org-agenda-todo-keyword-format "%-1s"
1510 "Format for the TODO keyword in agenda lines.
1511 Set this to something like \"%-12s\" if you want all TODO keywords
1512 to occupy a fixed space in the agenda display."
1513 :group 'org-agenda-line-format
1514 :type 'string)
1516 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1517 "Text preceding timerange entries in the agenda view.
1518 This is a list with two strings. The first applies when the range
1519 is entirely on one day. The second applies if the range spans several days.
1520 The strings may have two \"%d\" format specifiers which will be filled
1521 with the sequence number of the days, and the total number of days in the
1522 range, respectively."
1523 :group 'org-agenda-line-format
1524 :type '(list
1525 (string :tag "Deadline today ")
1526 (choice :tag "Deadline relative"
1527 (string :tag "Format string")
1528 (function))))
1530 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1531 "Text preceding scheduled items in the agenda view.
1532 This is a list with two strings. The first applies when the item is
1533 scheduled on the current day. The second applies when it has been scheduled
1534 previously, it may contain a %d indicating that this is the nth time that
1535 this item is scheduled, due to automatic rescheduling of unfinished items
1536 for the following day. So this number is one larger than the number of days
1537 that passed since this item was scheduled first."
1538 :group 'org-agenda-line-format
1539 :type '(list
1540 (string :tag "Scheduled today ")
1541 (string :tag "Scheduled previously")))
1543 (defcustom org-agenda-inactive-leader "["
1544 "Text preceding item pulled into the agenda by inactive time stamps.
1545 These entries are added to the agenda when pressing \"[\"."
1546 :group 'org-agenda-line-format
1547 :version "24.1"
1548 :type '(list
1549 (string :tag "Scheduled today ")
1550 (string :tag "Scheduled previously")))
1552 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1553 "Text preceding deadline items in the agenda view.
1554 This is a list with two strings. The first applies when the item has its
1555 deadline on the current day. The second applies when it is in the past or
1556 in the future, it may contain %d to capture how many days away the deadline
1557 is (was)."
1558 :group 'org-agenda-line-format
1559 :type '(list
1560 (string :tag "Deadline today ")
1561 (choice :tag "Deadline relative"
1562 (string :tag "Format string")
1563 (function))))
1565 (defcustom org-agenda-remove-times-when-in-prefix t
1566 "Non-nil means remove duplicate time specifications in agenda items.
1567 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1568 time-of-day specification in a headline or diary entry is extracted and
1569 placed into the prefix. If this option is non-nil, the original specification
1570 \(a timestamp or -range, or just a plain time(range) specification like
1571 11:30-4pm) will be removed for agenda display. This makes the agenda less
1572 cluttered.
1573 The option can be t or nil. It may also be the symbol `beg', indicating
1574 that the time should only be removed when it is located at the beginning of
1575 the headline/diary entry."
1576 :group 'org-agenda-line-format
1577 :type '(choice
1578 (const :tag "Always" t)
1579 (const :tag "Never" nil)
1580 (const :tag "When at beginning of entry" beg)))
1582 (defcustom org-agenda-remove-timeranges-from-blocks nil
1583 "Non-nil means remove time ranges specifications in agenda
1584 items that span on several days."
1585 :group 'org-agenda-line-format
1586 :version "24.1"
1587 :type 'boolean)
1589 (defcustom org-agenda-default-appointment-duration nil
1590 "Default duration for appointments that only have a starting time.
1591 When nil, no duration is specified in such cases.
1592 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1593 :group 'org-agenda-line-format
1594 :type '(choice
1595 (integer :tag "Minutes")
1596 (const :tag "No default duration")))
1598 (defcustom org-agenda-show-inherited-tags t
1599 "Non-nil means show inherited tags in each agenda line."
1600 :group 'org-agenda-line-format
1601 :type 'boolean)
1603 (defcustom org-agenda-hide-tags-regexp nil
1604 "Regular expression used to filter away specific tags in agenda views.
1605 This means that these tags will be present, but not be shown in the agenda
1606 line. Secondary filtering will still work on the hidden tags.
1607 Nil means don't hide any tags."
1608 :group 'org-agenda-line-format
1609 :type '(choice
1610 (const :tag "Hide none" nil)
1611 (string :tag "Regexp ")))
1613 (defcustom org-agenda-remove-tags nil
1614 "Non-nil means remove the tags from the headline copy in the agenda.
1615 When this is the symbol `prefix', only remove tags when
1616 `org-agenda-prefix-format' contains a `%T' specifier."
1617 :group 'org-agenda-line-format
1618 :type '(choice
1619 (const :tag "Always" t)
1620 (const :tag "Never" nil)
1621 (const :tag "When prefix format contains %T" prefix)))
1623 (if (fboundp 'defvaralias)
1624 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1625 'org-agenda-remove-tags))
1627 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1628 "Shift tags in agenda items to this column.
1629 If this number is positive, it specifies the column. If it is negative,
1630 it means that the tags should be flushright to that column. For example,
1631 -80 works well for a normal 80 character screen."
1632 :group 'org-agenda-line-format
1633 :type 'integer)
1635 (if (fboundp 'defvaralias)
1636 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1638 (defcustom org-agenda-fontify-priorities 'cookies
1639 "Non-nil means highlight low and high priorities in agenda.
1640 When t, the highest priority entries are bold, lowest priority italic.
1641 However, settings in `org-priority-faces' will overrule these faces.
1642 When this variable is the symbol `cookies', only fontify the
1643 cookies, not the entire task.
1644 This may also be an association list of priority faces, whose
1645 keys are the character values of `org-highest-priority',
1646 `org-default-priority', and `org-lowest-priority' (the default values
1647 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1648 color as a string, or a list like `(:background \"Red\")'.
1649 If it is a color, the variable `org-faces-easy-properties'
1650 determines if it is a foreground or a background color."
1651 :group 'org-agenda-line-format
1652 :type '(choice
1653 (const :tag "Never" nil)
1654 (const :tag "Defaults" t)
1655 (const :tag "Cookies only" cookies)
1656 (repeat :tag "Specify"
1657 (list (character :tag "Priority" :value ?A)
1658 (choice :tag "Face "
1659 (string :tag "Color")
1660 (sexp :tag "Face"))))))
1662 (defcustom org-agenda-day-face-function nil
1663 "Function called to determine what face should be used to display a day.
1664 The only argument passed to that function is the day. It should
1665 returns a face, or nil if does not want to specify a face and let
1666 the normal rules apply."
1667 :group 'org-agenda-line-format
1668 :version "24.1"
1669 :type 'function)
1671 (defcustom org-agenda-category-icon-alist nil
1672 "Alist of category icon to be displayed in agenda views.
1674 Each entry should have the following format:
1676 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1678 Where CATEGORY-REGEXP is a regexp matching the categories where
1679 the icon should be displayed.
1680 FILE-OR-DATA either a file path or a string containing image data.
1682 The other fields can be omitted safely if not needed:
1683 TYPE indicates the image type.
1684 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1685 image data.
1686 PROPS are additional image attributes to assign to the image,
1687 like, e.g. `:ascent center'.
1689 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1691 If you want to set the display properties yourself, just put a
1692 list as second element:
1694 (CATEGORY-REGEXP (MY PROPERTY LIST))
1696 For example, to display a 16px horizontal space for Emacs
1697 category, you can use:
1699 (\"Emacs\" '(space . (:width (16))))"
1700 :group 'org-agenda-line-format
1701 :version "24.1"
1702 :type '(alist :key-type (string :tag "Regexp matching category")
1703 :value-type (choice (list :tag "Icon"
1704 (string :tag "File or data")
1705 (symbol :tag "Type")
1706 (boolean :tag "Data?")
1707 (repeat :tag "Extra image properties" :inline t symbol))
1708 (list :tag "Display properties" sexp))))
1710 (defgroup org-agenda-column-view nil
1711 "Options concerning column view in the agenda."
1712 :tag "Org Agenda Column View"
1713 :group 'org-agenda)
1715 (defcustom org-agenda-columns-show-summaries t
1716 "Non-nil means show summaries for columns displayed in the agenda view."
1717 :group 'org-agenda-column-view
1718 :type 'boolean)
1720 (defcustom org-agenda-columns-compute-summary-properties t
1721 "Non-nil means recompute all summary properties before column view.
1722 When column view in the agenda is listing properties that have a summary
1723 operator, it can go to all relevant buffers and recompute the summaries
1724 there. This can mean overhead for the agenda column view, but is necessary
1725 to have thing up to date.
1726 As a special case, a CLOCKSUM property also makes sure that the clock
1727 computations are current."
1728 :group 'org-agenda-column-view
1729 :type 'boolean)
1731 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1732 "Non-nil means the duration of an appointment will add to day effort.
1733 The property to which appointment durations will be added is the one given
1734 in the option `org-effort-property'. If an appointment does not have
1735 an end time, `org-agenda-default-appointment-duration' will be used. If that
1736 is not set, an appointment without end time will not contribute to the time
1737 estimate."
1738 :group 'org-agenda-column-view
1739 :type 'boolean)
1741 (defcustom org-agenda-auto-exclude-function nil
1742 "A function called with a tag to decide if it is filtered on '/ RET'.
1743 The sole argument to the function, which is called once for each
1744 possible tag, is a string giving the name of the tag. The
1745 function should return either nil if the tag should be included
1746 as normal, or \"-<TAG>\" to exclude the tag.
1747 Note that for the purpose of tag filtering, only the lower-case version of
1748 all tags will be considered, so that this function will only ever see
1749 the lower-case version of all tags."
1750 :group 'org-agenda
1751 :type 'function)
1753 (defcustom org-agenda-bulk-custom-functions nil
1754 "Alist of characters and custom functions for bulk actions.
1755 For example, this value makes those two functions available:
1757 '((?R set-category)
1758 (?C bulk-cut))
1760 With selected entries in an agenda buffer, `B R' will call
1761 the custom function `set-category' on the selected entries.
1762 Note that functions in this alist don't need to be quoted."
1763 :type 'alist
1764 :version "24.1"
1765 :group 'org-agenda)
1767 (eval-when-compile
1768 (require 'cl))
1769 (require 'org)
1771 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1772 "Execute BODY with point at location given by `org-hd-marker' property.
1773 If STRING is non-nil, the text property will be fetched from position 0
1774 in that string. If STRING is nil, it will be fetched from the beginning
1775 of the current line."
1776 (org-with-gensyms (marker)
1777 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1778 'org-hd-marker ,string)))
1779 (with-current-buffer (marker-buffer ,marker)
1780 (save-excursion
1781 (goto-char ,marker)
1782 ,@body)))))
1783 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1785 (defun org-add-agenda-custom-command (entry)
1786 "Replace or add a command in `org-agenda-custom-commands'.
1787 This is mostly for hacking and trying a new command - once the command
1788 works you probably want to add it to `org-agenda-custom-commands' for good."
1789 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1790 (if ass
1791 (setcdr ass (cdr entry))
1792 (push entry org-agenda-custom-commands))))
1794 ;;; Define the org-agenda-mode
1796 (defvar org-agenda-mode-map (make-sparse-keymap)
1797 "Keymap for `org-agenda-mode'.")
1798 (if (fboundp 'defvaralias)
1799 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1801 (defvar org-agenda-menu) ; defined later in this file.
1802 (defvar org-agenda-restrict) ; defined later in this file.
1803 (defvar org-agenda-follow-mode nil)
1804 (defvar org-agenda-entry-text-mode nil)
1805 (defvar org-agenda-clockreport-mode nil)
1806 (defvar org-agenda-show-log nil)
1807 (defvar org-agenda-redo-command nil)
1808 (defvar org-agenda-query-string nil)
1809 (defvar org-agenda-mode-hook nil
1810 "Hook for `org-agenda-mode', run after the mode is turned on.")
1811 (defvar org-agenda-type nil)
1812 (defvar org-agenda-force-single-file nil)
1813 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1815 (defun org-agenda-mode ()
1816 "Mode for time-sorted view on action items in Org-mode files.
1818 The following commands are available:
1820 \\{org-agenda-mode-map}"
1821 (interactive)
1822 (kill-all-local-variables)
1823 (setq org-agenda-undo-list nil
1824 org-agenda-pending-undo-list nil
1825 org-agenda-bulk-marked-entries nil)
1826 (setq major-mode 'org-agenda-mode)
1827 ;; Keep global-font-lock-mode from turning on font-lock-mode
1828 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1829 (setq mode-name "Org-Agenda")
1830 (use-local-map org-agenda-mode-map)
1831 (easy-menu-add org-agenda-menu)
1832 (if org-startup-truncated (setq truncate-lines t))
1833 (org-set-local 'line-move-visual nil)
1834 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1835 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1836 ;; Make sure properties are removed when copying text
1837 (when (boundp 'buffer-substring-filters)
1838 (org-set-local 'buffer-substring-filters
1839 (cons (lambda (x)
1840 (set-text-properties 0 (length x) nil x) x)
1841 buffer-substring-filters)))
1842 (unless org-agenda-keep-modes
1843 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1844 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1845 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1846 org-agenda-show-log org-agenda-start-with-log-mode))
1848 (easy-menu-change
1849 '("Agenda") "Agenda Files"
1850 (append
1851 (list
1852 (vector
1853 (if (get 'org-agenda-files 'org-restrict)
1854 "Restricted to single file"
1855 "Edit File List")
1856 '(org-edit-agenda-file-list)
1857 (not (get 'org-agenda-files 'org-restrict)))
1858 "--")
1859 (mapcar 'org-file-menu-entry (org-agenda-files))))
1860 (org-agenda-set-mode-name)
1861 (apply
1862 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1863 (list 'org-agenda-mode-hook)))
1865 (substitute-key-definition 'undo 'org-agenda-undo
1866 org-agenda-mode-map global-map)
1867 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1868 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1869 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1870 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1871 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1872 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1873 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1874 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1875 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1876 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1877 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1878 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1879 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1880 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1881 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1882 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1883 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1884 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1885 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1886 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1887 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1888 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1889 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1890 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1891 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1892 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1893 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1894 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1895 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1896 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1897 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1898 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1899 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1900 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1901 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1902 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1903 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1904 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1905 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1906 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1907 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1908 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1909 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1910 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1911 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1913 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1914 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1915 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1916 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1917 (while l (org-defkey org-agenda-mode-map
1918 (int-to-string (pop l)) 'digit-argument)))
1920 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1921 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1922 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1923 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1924 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1925 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1926 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1927 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1928 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1929 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1930 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1931 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1932 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1933 'org-clock-modify-effort-estimate)
1934 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1935 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1936 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1937 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
1938 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1939 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1940 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1941 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1942 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1943 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1944 (substitute-key-definition 'next-line 'org-agenda-next-line
1945 org-agenda-mode-map global-map)
1946 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1947 org-agenda-mode-map global-map)
1948 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1949 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1950 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1951 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1952 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1953 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1954 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1955 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1956 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1957 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1958 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1959 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1960 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1961 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1962 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1963 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1964 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1965 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1966 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1967 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1968 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1969 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1970 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1971 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1972 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1973 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1974 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1975 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1976 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1977 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1979 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1980 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1981 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1982 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1983 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1984 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1985 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
1986 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1987 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1988 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1989 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1991 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1992 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1993 (when org-agenda-mouse-1-follows-link
1994 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1995 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1996 '("Agenda"
1997 ("Agenda Files")
1998 "--"
1999 ("Agenda Dates"
2000 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2001 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2002 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2003 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2004 "--"
2005 ("View"
2006 ["Day View" org-agenda-day-view
2007 :active (org-agenda-check-type nil 'agenda)
2008 :style radio :selected (eq org-agenda-current-span 'day)
2009 :keys "v d (or just d)"]
2010 ["Week View" org-agenda-week-view
2011 :active (org-agenda-check-type nil 'agenda)
2012 :style radio :selected (eq org-agenda-current-span 'week)
2013 :keys "v w (or just w)"]
2014 ["Month View" org-agenda-month-view
2015 :active (org-agenda-check-type nil 'agenda)
2016 :style radio :selected (eq org-agenda-current-span 'month)
2017 :keys "v m"]
2018 ["Year View" org-agenda-year-view
2019 :active (org-agenda-check-type nil 'agenda)
2020 :style radio :selected (eq org-agenda-current-span 'year)
2021 :keys "v y"]
2022 "--"
2023 ["Include Diary" org-agenda-toggle-diary
2024 :style toggle :selected org-agenda-include-diary
2025 :active (org-agenda-check-type nil 'agenda)]
2026 ["Include Deadlines" org-agenda-toggle-deadlines
2027 :style toggle :selected org-agenda-include-deadlines
2028 :active (org-agenda-check-type nil 'agenda)]
2029 ["Use Time Grid" org-agenda-toggle-time-grid
2030 :style toggle :selected org-agenda-use-time-grid
2031 :active (org-agenda-check-type nil 'agenda)]
2032 "--"
2033 ["Show clock report" org-agenda-clockreport-mode
2034 :style toggle :selected org-agenda-clockreport-mode
2035 :active (org-agenda-check-type nil 'agenda)]
2036 ["Show some entry text" org-agenda-entry-text-mode
2037 :style toggle :selected org-agenda-entry-text-mode
2038 :active t]
2039 "--"
2040 ["Show Logbook entries" org-agenda-log-mode
2041 :style toggle :selected org-agenda-show-log
2042 :active (org-agenda-check-type nil 'agenda 'timeline)
2043 :keys "v l (or just l)"]
2044 ["Include archived trees" org-agenda-archives-mode
2045 :style toggle :selected org-agenda-archives-mode :active t
2046 :keys "v a"]
2047 ["Include archive files" (org-agenda-archives-mode t)
2048 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2049 :keys "v A"]
2050 "--"
2051 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2052 ["Write view to file" org-agenda-write t]
2053 ["Rebuild buffer" org-agenda-redo t]
2054 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2055 "--"
2056 ["Show original entry" org-agenda-show t]
2057 ["Go To (other window)" org-agenda-goto t]
2058 ["Go To (this window)" org-agenda-switch-to t]
2059 ["Follow Mode" org-agenda-follow-mode
2060 :style toggle :selected org-agenda-follow-mode :active t]
2061 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2062 "--"
2063 ("TODO"
2064 ["Cycle TODO" org-agenda-todo t]
2065 ["Next TODO set" org-agenda-todo-nextset t]
2066 ["Previous TODO set" org-agenda-todo-previousset t]
2067 ["Add note" org-agenda-add-note t])
2068 ("Archive/Refile/Delete"
2069 ["Archive default" org-agenda-archive-default t]
2070 ["Archive default" org-agenda-archive-default-with-confirmation t]
2071 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2072 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2073 ["Archive subtree" org-agenda-archive t]
2074 "--"
2075 ["Refile" org-agenda-refile t]
2076 "--"
2077 ["Delete subtree" org-agenda-kill t])
2078 ("Bulk action"
2079 ["Mark entry" org-agenda-bulk-mark t]
2080 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2081 ["Unmark entry" org-agenda-bulk-unmark t]
2082 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2083 ["Act on all marked" org-agenda-bulk-action t]
2084 "--"
2085 ("Tags and Properties"
2086 ["Show all Tags" org-agenda-show-tags t]
2087 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2088 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2089 "--"
2090 ["Column View" org-columns t])
2091 ("Deadline/Schedule"
2092 ["Schedule" org-agenda-schedule t]
2093 ["Set Deadline" org-agenda-deadline t]
2094 "--"
2095 ["Mark item" org-agenda-action :active t :keys "k m"]
2096 ["Show mark item" org-agenda-action :active t :keys "k v"]
2097 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2098 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2099 "--"
2100 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2101 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2102 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2103 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2104 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2105 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2106 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2107 ("Clock and Effort"
2108 ["Clock in" org-agenda-clock-in t]
2109 ["Clock out" org-agenda-clock-out t]
2110 ["Clock cancel" org-agenda-clock-cancel t]
2111 ["Goto running clock" org-clock-goto t]
2112 "--"
2113 ["Set Effort" org-agenda-set-effort t]
2114 ["Change clocked effort" org-clock-modify-effort-estimate
2115 (org-clock-is-active)])
2116 ("Priority"
2117 ["Set Priority" org-agenda-priority t]
2118 ["Increase Priority" org-agenda-priority-up t]
2119 ["Decrease Priority" org-agenda-priority-down t]
2120 ["Show Priority" org-agenda-show-priority t])
2121 ("Calendar/Diary"
2122 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2123 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2124 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2125 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2126 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2127 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2128 "--"
2129 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2130 "--"
2131 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2132 "--"
2133 ("MobileOrg"
2134 ["Push Files and Views" org-mobile-push t]
2135 ["Get Captured and Flagged" org-mobile-pull t]
2136 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2137 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2138 "--"
2139 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2140 "--"
2141 ["Quit" org-agenda-quit t]
2142 ["Exit and Release Buffers" org-agenda-exit t]
2145 ;;; Agenda undo
2147 (defvar org-agenda-allow-remote-undo t
2148 "Non-nil means allow remote undo from the agenda buffer.")
2149 (defvar org-agenda-undo-list nil
2150 "List of undoable operations in the agenda since last refresh.")
2151 (defvar org-agenda-undo-has-started-in nil
2152 "Buffers that have already seen `undo-start' in the current undo sequence.")
2153 (defvar org-agenda-pending-undo-list nil
2154 "In a series of undo commands, this is the list of remaining undo items.")
2156 (defun org-agenda-undo ()
2157 "Undo a remote editing step in the agenda.
2158 This undoes changes both in the agenda buffer and in the remote buffer
2159 that have been changed along."
2160 (interactive)
2161 (or org-agenda-allow-remote-undo
2162 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2163 (if (not (eq this-command last-command))
2164 (setq org-agenda-undo-has-started-in nil
2165 org-agenda-pending-undo-list org-agenda-undo-list))
2166 (if (not org-agenda-pending-undo-list)
2167 (error "No further undo information"))
2168 (let* ((entry (pop org-agenda-pending-undo-list))
2169 buf line cmd rembuf)
2170 (setq cmd (pop entry) line (pop entry))
2171 (setq rembuf (nth 2 entry))
2172 (org-with-remote-undo rembuf
2173 (while (bufferp (setq buf (pop entry)))
2174 (if (pop entry)
2175 (with-current-buffer buf
2176 (let ((last-undo-buffer buf)
2177 (inhibit-read-only t))
2178 (unless (memq buf org-agenda-undo-has-started-in)
2179 (push buf org-agenda-undo-has-started-in)
2180 (make-local-variable 'pending-undo-list)
2181 (undo-start))
2182 (while (and pending-undo-list
2183 (listp pending-undo-list)
2184 (not (car pending-undo-list)))
2185 (pop pending-undo-list))
2186 (undo-more 1))))))
2187 (org-goto-line line)
2188 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2190 (defun org-verify-change-for-undo (l1 l2)
2191 "Verify that a real change occurred between the undo lists L1 and L2."
2192 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2193 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2194 (not (eq l1 l2)))
2196 ;;; Agenda dispatch
2198 (defvar org-agenda-restrict nil)
2199 (defvar org-agenda-restrict-begin (make-marker))
2200 (defvar org-agenda-restrict-end (make-marker))
2201 (defvar org-agenda-last-dispatch-buffer nil)
2202 (defvar org-agenda-overriding-restriction nil)
2204 ;;;###autoload
2205 (defun org-agenda (&optional arg keys restriction)
2206 "Dispatch agenda commands to collect entries to the agenda buffer.
2207 Prompts for a command to execute. Any prefix arg will be passed
2208 on to the selected command. The default selections are:
2210 a Call `org-agenda-list' to display the agenda for current day or week.
2211 t Call `org-todo-list' to display the global todo list.
2212 T Call `org-todo-list' to display the global todo list, select only
2213 entries with a specific TODO keyword (the user gets a prompt).
2214 m Call `org-tags-view' to display headlines with tags matching
2215 a condition (the user is prompted for the condition).
2216 M Like `m', but select only TODO entries, no ordinary headlines.
2217 L Create a timeline for the current buffer.
2218 e Export views to associated files.
2219 s Search entries for keywords.
2220 / Multi occur across all agenda files and also files listed
2221 in `org-agenda-text-search-extra-files'.
2222 < Restrict agenda commands to buffer, subtree, or region.
2223 Press several times to get the desired effect.
2224 > Remove a previous restriction.
2225 # List \"stuck\" projects.
2226 ! Configure what \"stuck\" means.
2227 C Configure custom agenda commands.
2229 More commands can be added by configuring the variable
2230 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2231 searches can be pre-defined in this way.
2233 If the current buffer is in Org-mode and visiting a file, you can also
2234 first press `<' once to indicate that the agenda should be temporarily
2235 \(until the next use of \\[org-agenda]) restricted to the current file.
2236 Pressing `<' twice means to restrict to the current subtree or region
2237 \(if active)."
2238 (interactive "P")
2239 (catch 'exit
2240 (let* ((prefix-descriptions nil)
2241 (org-agenda-window-setup (if (equal (buffer-name)
2242 org-agenda-buffer-name)
2243 'current-window
2244 org-agenda-window-setup))
2245 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2246 (org-agenda-custom-commands
2247 ;; normalize different versions
2248 (delq nil
2249 (mapcar
2250 (lambda (x)
2251 (cond ((stringp (cdr x))
2252 (push x prefix-descriptions)
2253 nil)
2254 ((stringp (nth 1 x)) x)
2255 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2256 (t (cons (car x) (cons "" (cdr x))))))
2257 org-agenda-custom-commands)))
2258 (buf (current-buffer))
2259 (bfn (buffer-file-name (buffer-base-buffer)))
2260 entry key type match lprops ans)
2261 ;; Turn off restriction unless there is an overriding one,
2262 (unless org-agenda-overriding-restriction
2263 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2264 ;; There is a request to keep the file list in place
2265 (put 'org-agenda-files 'org-restrict nil))
2266 (setq org-agenda-restrict nil)
2267 (move-marker org-agenda-restrict-begin nil)
2268 (move-marker org-agenda-restrict-end nil))
2269 ;; Delete old local properties
2270 (put 'org-agenda-redo-command 'org-lprops nil)
2271 ;; Delete previously set last-arguments
2272 (put 'org-agenda-redo-command 'last-args nil)
2273 ;; Remember where this call originated
2274 (setq org-agenda-last-dispatch-buffer (current-buffer))
2275 (unless keys
2276 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2277 keys (car ans)
2278 restriction (cdr ans)))
2279 ;; Establish the restriction, if any
2280 (when (and (not org-agenda-overriding-restriction) restriction)
2281 (put 'org-agenda-files 'org-restrict (list bfn))
2282 (cond
2283 ((eq restriction 'region)
2284 (setq org-agenda-restrict t)
2285 (move-marker org-agenda-restrict-begin (region-beginning))
2286 (move-marker org-agenda-restrict-end (region-end)))
2287 ((eq restriction 'subtree)
2288 (save-excursion
2289 (setq org-agenda-restrict t)
2290 (org-back-to-heading t)
2291 (move-marker org-agenda-restrict-begin (point))
2292 (move-marker org-agenda-restrict-end
2293 (progn (org-end-of-subtree t)))))))
2295 ;; For example the todo list should not need it (but does...)
2296 (cond
2297 ((setq entry (assoc keys org-agenda-custom-commands))
2298 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2299 (progn
2300 (setq type (nth 2 entry) match (eval (nth 3 entry))
2301 lprops (nth 4 entry))
2302 (put 'org-agenda-redo-command 'org-lprops lprops)
2303 (cond
2304 ((eq type 'agenda)
2305 (org-let lprops '(org-agenda-list current-prefix-arg)))
2306 ((eq type 'alltodo)
2307 (org-let lprops '(org-todo-list current-prefix-arg)))
2308 ((eq type 'search)
2309 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2310 ((eq type 'stuck)
2311 (org-let lprops '(org-agenda-list-stuck-projects
2312 current-prefix-arg)))
2313 ((eq type 'tags)
2314 (org-let lprops '(org-tags-view current-prefix-arg match)))
2315 ((eq type 'tags-todo)
2316 (org-let lprops '(org-tags-view '(4) match)))
2317 ((eq type 'todo)
2318 (org-let lprops '(org-todo-list match)))
2319 ((eq type 'tags-tree)
2320 (org-check-for-org-mode)
2321 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2322 ((eq type 'todo-tree)
2323 (org-check-for-org-mode)
2324 (org-let lprops
2325 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2326 (regexp-quote match) "\\>"))))
2327 ((eq type 'occur-tree)
2328 (org-check-for-org-mode)
2329 (org-let lprops '(org-occur match)))
2330 ((functionp type)
2331 (org-let lprops '(funcall type match)))
2332 ((fboundp type)
2333 (org-let lprops '(funcall type match)))
2334 (t (error "Invalid custom agenda command type %s" type))))
2335 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2336 ((equal keys "C")
2337 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2338 (customize-variable 'org-agenda-custom-commands))
2339 ((equal keys "a") (call-interactively 'org-agenda-list))
2340 ((equal keys "s") (call-interactively 'org-search-view))
2341 ((equal keys "t") (call-interactively 'org-todo-list))
2342 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2343 ((equal keys "m") (call-interactively 'org-tags-view))
2344 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2345 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2346 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2347 (org-add-hook
2348 'post-command-hook
2349 (lambda ()
2350 (unless (current-message)
2351 (let* ((m (org-agenda-get-any-marker))
2352 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2353 (when note
2354 (message (concat
2355 "FLAGGING-NOTE ([?] for more info): "
2356 (org-add-props
2357 (replace-regexp-in-string
2358 "\\\\n" "//"
2359 (copy-sequence note))
2360 nil 'face 'org-warning)))))))
2361 t t))
2362 ((equal keys "L")
2363 (unless (eq major-mode 'org-mode)
2364 (error "This is not an Org-mode file"))
2365 (unless restriction
2366 (put 'org-agenda-files 'org-restrict (list bfn))
2367 (org-call-with-arg 'org-timeline arg)))
2368 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2369 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2370 ((equal keys "!") (customize-variable 'org-stuck-projects))
2371 (t (error "Invalid agenda key"))))))
2373 (defun org-agenda-append-agenda ()
2374 "Append another agenda view to the current one.
2375 This function allows interactive building of block agendas.
2376 Agenda views are separated by `org-agenda-block-separator'."
2377 (interactive)
2378 (unless (string= (buffer-name) org-agenda-buffer-name)
2379 (error "Can only append from within agenda buffer"))
2380 (let ((org-agenda-multi t))
2381 (org-agenda)
2382 (widen)))
2384 (defun org-agenda-normalize-custom-commands (cmds)
2385 (delq nil
2386 (mapcar
2387 (lambda (x)
2388 (cond ((stringp (cdr x)) nil)
2389 ((stringp (nth 1 x)) x)
2390 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2391 (t (cons (car x) (cons "" (cdr x))))))
2392 cmds)))
2394 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2395 "The user interface for selecting an agenda command."
2396 (catch 'exit
2397 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2398 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2399 (region-p (org-region-active-p))
2400 (custom org-agenda-custom-commands)
2401 (selstring "")
2402 restriction second-time
2403 c entry key type match prefixes rmheader header-end custom1 desc
2404 line lines left right n n1)
2405 (save-window-excursion
2406 (delete-other-windows)
2407 (org-switch-to-buffer-other-window " *Agenda Commands*")
2408 (erase-buffer)
2409 (insert (eval-when-compile
2410 (let ((header
2412 Press key for an agenda command: < Buffer, subtree/region restriction
2413 -------------------------------- > Remove restriction
2414 a Agenda for current week or day e Export agenda views
2415 t List of all TODO entries T Entries with special TODO kwd
2416 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2417 L Timeline for current buffer # List stuck projects (!=configure)
2418 s Search for keywords C Configure custom agenda commands
2419 / Multi-occur ? Find :FLAGGED: entries
2421 (start 0))
2422 (while (string-match
2423 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2424 header start)
2425 (setq start (match-end 0))
2426 (add-text-properties (match-beginning 2) (match-end 2)
2427 '(face bold) header))
2428 header)))
2429 (setq header-end (move-marker (make-marker) (point)))
2430 (while t
2431 (setq custom1 custom)
2432 (when (eq rmheader t)
2433 (org-goto-line 1)
2434 (re-search-forward ":" nil t)
2435 (delete-region (match-end 0) (point-at-eol))
2436 (forward-char 1)
2437 (looking-at "-+")
2438 (delete-region (match-end 0) (point-at-eol))
2439 (move-marker header-end (match-end 0)))
2440 (goto-char header-end)
2441 (delete-region (point) (point-max))
2443 ;; Produce all the lines that describe custom commands and prefixes
2444 (setq lines nil)
2445 (while (setq entry (pop custom1))
2446 (setq key (car entry) desc (nth 1 entry)
2447 type (nth 2 entry)
2448 match (nth 3 entry))
2449 (if (> (length key) 1)
2450 (add-to-list 'prefixes (string-to-char key))
2451 (setq line
2452 (format
2453 "%-4s%-14s"
2454 (org-add-props (copy-sequence key)
2455 '(face bold))
2456 (cond
2457 ((string-match "\\S-" desc) desc)
2458 ((eq type 'agenda) "Agenda for current week or day")
2459 ((eq type 'alltodo) "List of all TODO entries")
2460 ((eq type 'search) "Word search")
2461 ((eq type 'stuck) "List of stuck projects")
2462 ((eq type 'todo) "TODO keyword")
2463 ((eq type 'tags) "Tags query")
2464 ((eq type 'tags-todo) "Tags (TODO)")
2465 ((eq type 'tags-tree) "Tags tree")
2466 ((eq type 'todo-tree) "TODO kwd tree")
2467 ((eq type 'occur-tree) "Occur tree")
2468 ((functionp type) (if (symbolp type)
2469 (symbol-name type)
2470 "Lambda expression"))
2471 (t "???"))))
2472 (if org-agenda-menu-show-matcher
2473 (setq line
2474 (concat line ": "
2475 (cond
2476 ((stringp match)
2477 (setq match (copy-sequence match))
2478 (org-add-props match nil 'face 'org-warning))
2479 (match
2480 (format "set of %d commands" (length match)))
2481 (t ""))))
2482 (if (org-string-nw-p match)
2483 (add-text-properties
2484 0 (length line) (list 'help-echo
2485 (concat "Matcher: "match)) line)))
2486 (push line lines)))
2487 (setq lines (nreverse lines))
2488 (when prefixes
2489 (mapc (lambda (x)
2490 (push
2491 (format "%s %s"
2492 (org-add-props (char-to-string x)
2493 nil 'face 'bold)
2494 (or (cdr (assoc (concat selstring
2495 (char-to-string x))
2496 prefix-descriptions))
2497 "Prefix key"))
2498 lines))
2499 prefixes))
2501 ;; Check if we should display in two columns
2502 (if org-agenda-menu-two-column
2503 (progn
2504 (setq n (length lines)
2505 n1 (+ (/ n 2) (mod n 2))
2506 right (nthcdr n1 lines)
2507 left (copy-sequence lines))
2508 (setcdr (nthcdr (1- n1) left) nil))
2509 (setq left lines right nil))
2510 (while left
2511 (insert "\n" (pop left))
2512 (when right
2513 (if (< (current-column) 40)
2514 (move-to-column 40 t)
2515 (insert " "))
2516 (insert (pop right))))
2518 ;; Make the window the right size
2519 (goto-char (point-min))
2520 (if second-time
2521 (if (not (pos-visible-in-window-p (point-max)))
2522 (org-fit-window-to-buffer))
2523 (setq second-time t)
2524 (org-fit-window-to-buffer))
2526 ;; Ask for selection
2527 (message "Press key for agenda command%s:"
2528 (if (or restrict-ok org-agenda-overriding-restriction)
2529 (if org-agenda-overriding-restriction
2530 " (restriction lock active)"
2531 (if restriction
2532 (format " (restricted to %s)" restriction)
2533 " (unrestricted)"))
2534 ""))
2535 (setq c (read-char-exclusive))
2536 (message "")
2537 (cond
2538 ((assoc (char-to-string c) custom)
2539 (setq selstring (concat selstring (char-to-string c)))
2540 (throw 'exit (cons selstring restriction)))
2541 ((memq c prefixes)
2542 (setq selstring (concat selstring (char-to-string c))
2543 prefixes nil
2544 rmheader (or rmheader t)
2545 custom (delq nil (mapcar
2546 (lambda (x)
2547 (if (or (= (length (car x)) 1)
2548 (/= (string-to-char (car x)) c))
2550 (cons (substring (car x) 1) (cdr x))))
2551 custom))))
2552 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2553 (message "Restriction is only possible in Org-mode buffers")
2554 (ding) (sit-for 1))
2555 ((eq c ?1)
2556 (org-agenda-remove-restriction-lock 'noupdate)
2557 (setq restriction 'buffer))
2558 ((eq c ?0)
2559 (org-agenda-remove-restriction-lock 'noupdate)
2560 (setq restriction (if region-p 'region 'subtree)))
2561 ((eq c ?<)
2562 (org-agenda-remove-restriction-lock 'noupdate)
2563 (setq restriction
2564 (cond
2565 ((eq restriction 'buffer)
2566 (if region-p 'region 'subtree))
2567 ((memq restriction '(subtree region))
2568 nil)
2569 (t 'buffer))))
2570 ((eq c ?>)
2571 (org-agenda-remove-restriction-lock 'noupdate)
2572 (setq restriction nil))
2573 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2574 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2575 ((and (> (length selstring) 0) (eq c ?\d))
2576 (delete-window)
2577 (org-agenda-get-restriction-and-command prefix-descriptions))
2579 ((equal c ?q) (error "Abort"))
2580 (t (error "Invalid key %c" c))))))))
2582 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2583 (defvar org-agenda-last-arguments nil
2584 "The arguments of the previous call to `org-agenda'.")
2585 (defun org-agenda-run-series (name series)
2586 (org-let (nth 1 series) '(org-prepare-agenda name))
2587 (let* ((org-agenda-multi t)
2588 (redo (list 'org-agenda-run-series name (list 'quote series)))
2589 (org-agenda-overriding-arguments
2590 (or org-agenda-overriding-arguments
2591 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2592 (get 'org-agenda-redo-command 'last-args))))
2593 (cmds (car series))
2594 (gprops (nth 1 series))
2595 match ;; The byte compiler incorrectly complains about this. Keep it!
2596 cmd type lprops)
2597 (while (setq cmd (pop cmds))
2598 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2599 (cond
2600 ((eq type 'agenda)
2601 (org-let2 gprops lprops
2602 '(call-interactively 'org-agenda-list)))
2603 ((eq type 'alltodo)
2604 (org-let2 gprops lprops
2605 '(call-interactively 'org-todo-list)))
2606 ((eq type 'search)
2607 (org-let2 gprops lprops
2608 '(org-search-view current-prefix-arg match nil)))
2609 ((eq type 'stuck)
2610 (org-let2 gprops lprops
2611 '(call-interactively 'org-agenda-list-stuck-projects)))
2612 ((eq type 'tags)
2613 (org-let2 gprops lprops
2614 '(org-tags-view current-prefix-arg match)))
2615 ((eq type 'tags-todo)
2616 (org-let2 gprops lprops
2617 '(org-tags-view '(4) match)))
2618 ((eq type 'todo)
2619 (org-let2 gprops lprops
2620 '(org-todo-list match)))
2621 ((fboundp type)
2622 (org-let2 gprops lprops
2623 '(funcall type match)))
2624 (t (error "Invalid type in command series"))))
2625 (widen)
2626 (setq org-agenda-redo-command redo)
2627 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2628 (goto-char (point-min)))
2629 (org-fit-agenda-window)
2630 (org-let (nth 1 series) '(org-finalize-agenda)))
2632 ;;;###autoload
2633 (defmacro org-batch-agenda (cmd-key &rest parameters)
2634 "Run an agenda command in batch mode and send the result to STDOUT.
2635 If CMD-KEY is a string of length 1, it is used as a key in
2636 `org-agenda-custom-commands' and triggers this command. If it is a
2637 longer string it is used as a tags/todo match string.
2638 Parameters are alternating variable names and values that will be bound
2639 before running the agenda command."
2640 (org-eval-in-environment (org-make-parameter-alist parameters)
2641 (if (> (length cmd-key) 2)
2642 (org-tags-view nil cmd-key)
2643 (org-agenda nil cmd-key)))
2644 (set-buffer org-agenda-buffer-name)
2645 (princ (buffer-string)))
2646 (def-edebug-spec org-batch-agenda (form &rest sexp))
2648 (defvar org-agenda-info nil)
2650 ;;;###autoload
2651 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2652 "Run an agenda command in batch mode and send the result to STDOUT.
2653 If CMD-KEY is a string of length 1, it is used as a key in
2654 `org-agenda-custom-commands' and triggers this command. If it is a
2655 longer string it is used as a tags/todo match string.
2656 Parameters are alternating variable names and values that will be bound
2657 before running the agenda command.
2659 The output gives a line for each selected agenda item. Each
2660 item is a list of comma-separated values, like this:
2662 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2664 category The category of the item
2665 head The headline, without TODO kwd, TAGS and PRIORITY
2666 type The type of the agenda entry, can be
2667 todo selected in TODO match
2668 tagsmatch selected in tags match
2669 diary imported from diary
2670 deadline a deadline on given date
2671 scheduled scheduled on given date
2672 timestamp entry has timestamp on given date
2673 closed entry was closed on given date
2674 upcoming-deadline warning about deadline
2675 past-scheduled forwarded scheduled item
2676 block entry has date block including g. date
2677 todo The todo keyword, if any
2678 tags All tags including inherited ones, separated by colons
2679 date The relevant date, like 2007-2-14
2680 time The time, like 15:00-16:50
2681 extra Sting with extra planning info
2682 priority-l The priority letter if any was given
2683 priority-n The computed numerical priority
2684 agenda-day The day in the agenda where this is listed"
2685 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2686 (org-make-parameter-alist parameters))
2687 (if (> (length cmd-key) 2)
2688 (org-tags-view nil cmd-key)
2689 (org-agenda nil cmd-key)))
2690 (set-buffer org-agenda-buffer-name)
2691 (let* ((lines (org-split-string (buffer-string) "\n"))
2692 line)
2693 (while (setq line (pop lines))
2694 (catch 'next
2695 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2696 (setq org-agenda-info
2697 (org-fix-agenda-info (text-properties-at 0 line)))
2698 (princ
2699 (mapconcat 'org-agenda-export-csv-mapper
2700 '(org-category txt type todo tags date time extra
2701 priority-letter priority agenda-day)
2702 ","))
2703 (princ "\n")))))
2704 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2706 (defun org-fix-agenda-info (props)
2707 "Make sure all properties on an agenda item have a canonical form.
2708 This ensures the export commands can easily use it."
2709 (let (tmp re)
2710 (when (setq tmp (plist-get props 'tags))
2711 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2712 (when (setq tmp (plist-get props 'date))
2713 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2714 (let ((calendar-date-display-form '(year "-" month "-" day)))
2715 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2717 (setq tmp (calendar-date-string tmp)))
2718 (setq props (plist-put props 'date tmp)))
2719 (when (setq tmp (plist-get props 'day))
2720 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2721 (let ((calendar-date-display-form '(year "-" month "-" day)))
2722 (setq tmp (calendar-date-string tmp)))
2723 (setq props (plist-put props 'day tmp))
2724 (setq props (plist-put props 'agenda-day tmp)))
2725 (when (setq tmp (plist-get props 'txt))
2726 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2727 (plist-put props 'priority-letter (match-string 1 tmp))
2728 (setq tmp (replace-match "" t t tmp)))
2729 (when (and (setq re (plist-get props 'org-todo-regexp))
2730 (setq re (concat "\\`\\.*" re " ?"))
2731 (string-match re tmp))
2732 (plist-put props 'todo (match-string 1 tmp))
2733 (setq tmp (replace-match "" t t tmp)))
2734 (plist-put props 'txt tmp)))
2735 props)
2737 (defun org-agenda-export-csv-mapper (prop)
2738 (let ((res (plist-get org-agenda-info prop)))
2739 (setq res
2740 (cond
2741 ((not res) "")
2742 ((stringp res) res)
2743 (t (prin1-to-string res))))
2744 (while (string-match "," res)
2745 (setq res (replace-match ";" t t res)))
2746 (org-trim res)))
2749 ;;;###autoload
2750 (defun org-store-agenda-views (&rest parameters)
2751 (interactive)
2752 (eval (list 'org-batch-store-agenda-views)))
2754 ;;;###autoload
2755 (defmacro org-batch-store-agenda-views (&rest parameters)
2756 "Run all custom agenda commands that have a file argument."
2757 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2758 (pop-up-frames nil)
2759 (dir default-directory)
2760 (pars (org-make-parameter-alist parameters))
2761 cmd thiscmdkey files opts cmd-or-set)
2762 (save-window-excursion
2763 (while cmds
2764 (setq cmd (pop cmds)
2765 thiscmdkey (car cmd)
2766 cmd-or-set (nth 2 cmd)
2767 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2768 files (nth (if (listp cmd-or-set) 4 5) cmd))
2769 (if (stringp files) (setq files (list files)))
2770 (when files
2771 (org-eval-in-environment (append org-agenda-exporter-settings
2772 opts pars)
2773 (org-agenda nil thiscmdkey))
2774 (set-buffer org-agenda-buffer-name)
2775 (while files
2776 (org-eval-in-environment (append org-agenda-exporter-settings
2777 opts pars)
2778 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2779 (and (get-buffer org-agenda-buffer-name)
2780 (kill-buffer org-agenda-buffer-name)))))))
2781 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2783 (defun org-agenda-mark-header-line (pos)
2784 "Mark the line at POS as an agenda structure header."
2785 (save-excursion
2786 (goto-char pos)
2787 (put-text-property (point-at-bol) (point-at-eol)
2788 'org-agenda-structural-header t)
2789 (when org-agenda-title-append
2790 (put-text-property (point-at-bol) (point-at-eol)
2791 'org-agenda-title-append org-agenda-title-append))))
2793 (defvar org-mobile-creating-agendas)
2794 (defvar org-agenda-write-buffer-name "Agenda View")
2795 (defun org-agenda-write (file &optional open nosettings)
2796 "Write the current buffer (an agenda view) as a file.
2797 Depending on the extension of the file name, plain text (.txt),
2798 HTML (.html or .htm) or Postscript (.ps) is produced.
2799 If the extension is .ics, run icalendar export over all files used
2800 to construct the agenda and limit the export to entries listed in the
2801 agenda now.
2802 With prefix argument OPEN, open the new file immediately.
2803 If NOSETTINGS is given, do not scope the settings of
2804 `org-agenda-exporter-settings' into the export commands. This is used when
2805 the settings have already been scoped and we do not wish to overrule other,
2806 higher priority settings."
2807 (interactive "FWrite agenda to file: \nP")
2808 (if (not (file-writable-p file))
2809 (error "Cannot write agenda to file %s" file))
2810 (org-let (if nosettings nil org-agenda-exporter-settings)
2811 '(save-excursion
2812 (save-window-excursion
2813 (org-agenda-mark-filtered-text)
2814 (let ((bs (copy-sequence (buffer-string))) beg)
2815 (org-agenda-unmark-filtered-text)
2816 (with-temp-buffer
2817 (rename-buffer org-agenda-write-buffer-name t)
2818 (set-buffer-modified-p nil)
2819 (insert bs)
2820 (org-agenda-remove-marked-text 'org-filtered)
2821 (while (setq beg (text-property-any (point-min) (point-max)
2822 'org-filtered t))
2823 (delete-region
2824 beg (or (next-single-property-change beg 'org-filtered)
2825 (point-max))))
2826 (run-hooks 'org-agenda-before-write-hook)
2827 (cond
2828 ((org-bound-and-true-p org-mobile-creating-agendas)
2829 (org-mobile-write-agenda-for-mobile file))
2830 ((string-match "\\.html?\\'" file)
2831 (require 'htmlize)
2832 (set-buffer (htmlize-buffer (current-buffer)))
2834 (when (and org-agenda-export-html-style
2835 (string-match "<style>" org-agenda-export-html-style))
2836 ;; replace <style> section with org-agenda-export-html-style
2837 (goto-char (point-min))
2838 (kill-region (- (search-forward "<style") 6)
2839 (search-forward "</style>"))
2840 (insert org-agenda-export-html-style))
2841 (write-file file)
2842 (kill-buffer (current-buffer))
2843 (message "HTML written to %s" file))
2844 ((string-match "\\.ps\\'" file)
2845 (require 'ps-print)
2846 (ps-print-buffer-with-faces file)
2847 (message "Postscript written to %s" file))
2848 ((string-match "\\.pdf\\'" file)
2849 (require 'ps-print)
2850 (ps-print-buffer-with-faces
2851 (concat (file-name-sans-extension file) ".ps"))
2852 (call-process "ps2pdf" nil nil nil
2853 (expand-file-name
2854 (concat (file-name-sans-extension file) ".ps"))
2855 (expand-file-name file))
2856 (delete-file (concat (file-name-sans-extension file) ".ps"))
2857 (message "PDF written to %s" file))
2858 ((string-match "\\.ics\\'" file)
2859 (require 'org-icalendar)
2860 (let ((org-agenda-marker-table
2861 (org-create-marker-find-array
2862 (org-agenda-collect-markers)))
2863 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2864 (org-combined-agenda-icalendar-file file))
2865 (apply 'org-export-icalendar 'combine
2866 (org-agenda-files nil 'ifmode))))
2868 (let ((bs (buffer-string)))
2869 (find-file file)
2870 (erase-buffer)
2871 (insert bs)
2872 (save-buffer 0)
2873 (kill-buffer (current-buffer))
2874 (message "Plain text written to %s" file))))))))
2875 (set-buffer org-agenda-buffer-name))
2876 (when open (org-open-file file)))
2878 (defvar org-agenda-tag-filter-overlays nil)
2879 (defvar org-agenda-cat-filter-overlays nil)
2881 (defun org-agenda-mark-filtered-text ()
2882 "Mark all text hidden by filtering with a text property."
2883 (let ((inhibit-read-only t))
2884 (mapc
2885 (lambda (o)
2886 (when (equal (overlay-buffer o) (current-buffer))
2887 (put-text-property
2888 (overlay-start o) (overlay-end o)
2889 'org-filtered t)))
2890 (append org-agenda-tag-filter-overlays
2891 org-agenda-cat-filter-overlays))))
2893 (defun org-agenda-unmark-filtered-text ()
2894 "Remove the filtering text property."
2895 (let ((inhibit-read-only t))
2896 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2898 (defun org-agenda-remove-marked-text (property &optional value)
2899 "Delete all text marked with VALUE of PROPERTY.
2900 VALUE defaults to t."
2901 (let (beg)
2902 (setq value (or value t))
2903 (while (setq beg (text-property-any (point-min) (point-max)
2904 property value))
2905 (delete-region
2906 beg (or (next-single-property-change beg 'org-filtered)
2907 (point-max))))))
2909 (defun org-agenda-add-entry-text ()
2910 "Add entry text to agenda lines.
2911 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2912 entry text following headings shown in the agenda.
2913 Drawers will be excluded, also the line with scheduling/deadline info."
2914 (when (and (> org-agenda-add-entry-text-maxlines 0)
2915 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2916 (let (m txt)
2917 (goto-char (point-min))
2918 (while (not (eobp))
2919 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2920 (beginning-of-line 2)
2921 (setq txt (org-agenda-get-some-entry-text
2922 m org-agenda-add-entry-text-maxlines " > "))
2923 (end-of-line 1)
2924 (if (string-match "\\S-" txt)
2925 (insert "\n" txt)
2926 (or (eobp) (forward-char 1))))))))
2928 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2929 &rest keep)
2930 "Extract entry text from MARKER, at most N-LINES lines.
2931 This will ignore drawers etc, just get the text.
2932 If INDENT is given, prefix every line with this string. If KEEP is
2933 given, it is a list of symbols, defining stuff that should not be
2934 removed from the entry content. Currently only `planning' is allowed here."
2935 (let (txt drawer-re kwd-time-re ind)
2936 (save-excursion
2937 (with-current-buffer (marker-buffer marker)
2938 (if (not (eq major-mode 'org-mode))
2939 (setq txt "")
2940 (save-excursion
2941 (save-restriction
2942 (widen)
2943 (goto-char marker)
2944 (end-of-line 1)
2945 (setq txt (buffer-substring
2946 (min (1+ (point)) (point-max))
2947 (progn (outline-next-heading) (point)))
2948 drawer-re org-drawer-regexp
2949 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2950 ".*\n?"))
2951 (with-temp-buffer
2952 (insert txt)
2953 (when org-agenda-add-entry-text-descriptive-links
2954 (goto-char (point-min))
2955 (while (org-activate-bracket-links (point-max))
2956 (add-text-properties (match-beginning 0) (match-end 0)
2957 '(face org-link))))
2958 (goto-char (point-min))
2959 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2960 (set-text-properties (match-beginning 0) (match-end 0)
2961 nil))
2962 (goto-char (point-min))
2963 (while (re-search-forward drawer-re nil t)
2964 (delete-region
2965 (match-beginning 0)
2966 (progn (re-search-forward
2967 "^[ \t]*:END:.*\n?" nil 'move)
2968 (point))))
2969 (unless (member 'planning keep)
2970 (goto-char (point-min))
2971 (while (re-search-forward kwd-time-re nil t)
2972 (replace-match "")))
2973 (goto-char (point-min))
2974 (when org-agenda-entry-text-exclude-regexps
2975 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2976 (while (setq re (pop re-list))
2977 (goto-char (point-min))
2978 (while (re-search-forward re nil t)
2979 (replace-match "")))))
2980 (goto-char (point-max))
2981 (skip-chars-backward " \t\n")
2982 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2984 ;; find and remove min common indentation
2985 (goto-char (point-min))
2986 (untabify (point-min) (point-max))
2987 (setq ind (org-get-indentation))
2988 (while (not (eobp))
2989 (unless (looking-at "[ \t]*$")
2990 (setq ind (min ind (org-get-indentation))))
2991 (beginning-of-line 2))
2992 (goto-char (point-min))
2993 (while (not (eobp))
2994 (unless (looking-at "[ \t]*$")
2995 (move-to-column ind)
2996 (delete-region (point-at-bol) (point)))
2997 (beginning-of-line 2))
2999 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3001 (goto-char (point-min))
3002 (when indent
3003 (while (and (not (eobp)) (re-search-forward "^" nil t))
3004 (replace-match indent t t)))
3005 (goto-char (point-min))
3006 (while (looking-at "[ \t]*\n") (replace-match ""))
3007 (goto-char (point-max))
3008 (when (> (org-current-line)
3009 n-lines)
3010 (org-goto-line (1+ n-lines))
3011 (backward-char 1))
3012 (setq txt (buffer-substring (point-min) (point)))))))))
3013 txt))
3015 (defun org-agenda-collect-markers ()
3016 "Collect the markers pointing to entries in the agenda buffer."
3017 (let (m markers)
3018 (save-excursion
3019 (goto-char (point-min))
3020 (while (not (eobp))
3021 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3022 (org-get-at-bol 'org-marker)))
3023 (push m markers))
3024 (beginning-of-line 2)))
3025 (nreverse markers)))
3027 (defun org-create-marker-find-array (marker-list)
3028 "Create a alist of files names with all marker positions in that file."
3029 (let (f tbl m a p)
3030 (while (setq m (pop marker-list))
3031 (setq p (marker-position m)
3032 f (buffer-file-name (or (buffer-base-buffer
3033 (marker-buffer m))
3034 (marker-buffer m))))
3035 (if (setq a (assoc f tbl))
3036 (push (marker-position m) (cdr a))
3037 (push (list f p) tbl)))
3038 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3039 tbl)))
3041 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3042 (defun org-check-agenda-marker-table ()
3043 "Check of the current entry is on the marker list."
3044 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3046 (and (setq a (assoc file org-agenda-marker-table))
3047 (save-match-data
3048 (save-excursion
3049 (org-back-to-heading t)
3050 (member (point) (cdr a)))))))
3052 (defun org-check-for-org-mode ()
3053 "Make sure current buffer is in org-mode. Error if not."
3054 (or (eq major-mode 'org-mode)
3055 (error "Cannot execute org-mode agenda command on buffer in %s"
3056 major-mode)))
3058 (defun org-fit-agenda-window ()
3059 "Fit the window to the buffer size."
3060 (and (memq org-agenda-window-setup '(reorganize-frame))
3061 (fboundp 'fit-window-to-buffer)
3062 (org-fit-window-to-buffer
3064 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3065 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3067 ;;; Agenda prepare and finalize
3069 (defvar org-agenda-multi nil) ; dynamically scoped
3070 (defvar org-agenda-buffer-name "*Org Agenda*")
3071 (defvar org-pre-agenda-window-conf nil)
3072 (defvar org-agenda-columns-active nil)
3073 (defvar org-agenda-name nil)
3074 (defvar org-agenda-tag-filter nil)
3075 (defvar org-agenda-category-filter nil)
3076 (defvar org-agenda-tag-filter-while-redo nil)
3077 (defvar org-agenda-tag-filter-preset nil
3078 "A preset of the tags filter used for secondary agenda filtering.
3079 This must be a list of strings, each string must be a single tag preceded
3080 by \"+\" or \"-\".
3081 This variable should not be set directly, but agenda custom commands can
3082 bind it in the options section. The preset filter is a global property of
3083 the entire agenda view. In a block agenda, it will not work reliably to
3084 define a filter for one of the individual blocks. You need to set it in
3085 the global options and expect it to be applied to the entire view.")
3087 (defvar org-agenda-category-filter-preset nil
3088 "A preset of the category filter used for secondary agenda filtering.
3089 This must be a list of strings, each string must be a single category
3090 preceded by \"+\" or \"-\".
3091 This variable should not be set directly, but agenda custom commands can
3092 bind it in the options section. The preset filter is a global property of
3093 the entire agenda view. In a block agenda, it will not work reliably to
3094 define a filter for one of the individual blocks. You need to set it in
3095 the global options and expect it to be applied to the entire view.")
3097 (defun org-prepare-agenda (&optional name)
3098 (setq org-todo-keywords-for-agenda nil)
3099 (setq org-drawers-for-agenda nil)
3100 (unless org-agenda-persistent-filter
3101 (setq org-agenda-tag-filter nil
3102 org-agenda-category-filter nil))
3103 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3104 (put 'org-agenda-category-filter :preset-filter org-agenda-category-filter-preset)
3105 (if org-agenda-multi
3106 (progn
3107 (setq buffer-read-only nil)
3108 (goto-char (point-max))
3109 (unless (or (bobp) org-agenda-compact-blocks
3110 (not org-agenda-block-separator))
3111 (insert "\n"
3112 (if (stringp org-agenda-block-separator)
3113 org-agenda-block-separator
3114 (make-string (window-width) org-agenda-block-separator))
3115 "\n"))
3116 (narrow-to-region (point) (point-max)))
3117 (setq org-done-keywords-for-agenda nil)
3118 (org-agenda-reset-markers)
3119 (setq org-agenda-contributing-files nil)
3120 (setq org-agenda-columns-active nil)
3121 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3122 (setq org-todo-keywords-for-agenda
3123 (org-uniquify org-todo-keywords-for-agenda))
3124 (setq org-done-keywords-for-agenda
3125 (org-uniquify org-done-keywords-for-agenda))
3126 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3127 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3128 (awin (get-buffer-window abuf)))
3129 (cond
3130 ((equal (current-buffer) abuf) nil)
3131 (awin (select-window awin))
3132 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3133 ((equal org-agenda-window-setup 'current-window)
3134 (org-pop-to-buffer-same-window abuf))
3135 ((equal org-agenda-window-setup 'other-window)
3136 (org-switch-to-buffer-other-window abuf))
3137 ((equal org-agenda-window-setup 'other-frame)
3138 (switch-to-buffer-other-frame abuf))
3139 ((equal org-agenda-window-setup 'reorganize-frame)
3140 (delete-other-windows)
3141 (org-switch-to-buffer-other-window abuf)))
3142 ;; additional test in case agenda is invoked from within agenda
3143 ;; buffer via elisp link
3144 (unless (equal (current-buffer) abuf)
3145 (org-pop-to-buffer-same-window abuf)))
3146 (setq buffer-read-only nil)
3147 (let ((inhibit-read-only t)) (erase-buffer))
3148 (org-agenda-mode)
3149 (and name (not org-agenda-name)
3150 (org-set-local 'org-agenda-name name)))
3151 (setq buffer-read-only nil))
3153 (defun org-finalize-agenda ()
3154 "Finishing touch for the agenda buffer, called just before displaying it."
3155 (unless org-agenda-multi
3156 (save-excursion
3157 (let ((inhibit-read-only t))
3158 (goto-char (point-min))
3159 (while (org-activate-bracket-links (point-max))
3160 (add-text-properties (match-beginning 0) (match-end 0)
3161 '(face org-link)))
3162 (org-agenda-align-tags)
3163 (unless org-agenda-with-colors
3164 (remove-text-properties (point-min) (point-max) '(face nil))))
3165 (if (and (boundp 'org-agenda-overriding-columns-format)
3166 org-agenda-overriding-columns-format)
3167 (org-set-local 'org-agenda-overriding-columns-format
3168 org-agenda-overriding-columns-format))
3169 (if (and (boundp 'org-agenda-view-columns-initially)
3170 org-agenda-view-columns-initially)
3171 (org-agenda-columns))
3172 (when org-agenda-fontify-priorities
3173 (org-agenda-fontify-priorities))
3174 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3175 (org-agenda-dim-blocked-tasks))
3176 (org-agenda-mark-clocking-task)
3177 (when org-agenda-entry-text-mode
3178 (org-agenda-entry-text-hide)
3179 (org-agenda-entry-text-show))
3180 (if (functionp 'org-habit-insert-consistency-graphs)
3181 (org-habit-insert-consistency-graphs))
3182 (run-hooks 'org-finalize-agenda-hook)
3183 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3184 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3185 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3186 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3187 (org-agenda-filter-apply org-agenda-category-filter 'category))
3190 (defun org-agenda-mark-clocking-task ()
3191 "Mark the current clock entry in the agenda if it is present."
3192 (mapc (lambda (o)
3193 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3194 (delete-overlay o)))
3195 (overlays-in (point-min) (point-max)))
3196 (when (marker-buffer org-clock-hd-marker)
3197 (save-excursion
3198 (goto-char (point-min))
3199 (let (s ov)
3200 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3201 (goto-char s)
3202 (when (equal (org-get-at-bol 'org-hd-marker)
3203 org-clock-hd-marker)
3204 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3205 (overlay-put ov 'type 'org-agenda-clocking)
3206 (overlay-put ov 'face 'org-agenda-clocking)
3207 (overlay-put ov 'help-echo
3208 "The clock is running in this item")))))))
3210 (defun org-agenda-fontify-priorities ()
3211 "Make highest priority lines bold, and lowest italic."
3212 (interactive)
3213 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3214 (delete-overlay o)))
3215 (overlays-in (point-min) (point-max)))
3216 (save-excursion
3217 (let ((inhibit-read-only t)
3218 b e p ov h l)
3219 (goto-char (point-min))
3220 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3221 (setq h (or (get-char-property (point) 'org-highest-priority)
3222 org-highest-priority)
3223 l (or (get-char-property (point) 'org-lowest-priority)
3224 org-lowest-priority)
3225 p (string-to-char (match-string 1))
3226 b (match-beginning 0)
3227 e (if (eq org-agenda-fontify-priorities 'cookies)
3228 (match-end 0)
3229 (point-at-eol))
3230 ov (make-overlay b e))
3231 (overlay-put
3232 ov 'face
3233 (cond ((org-face-from-face-or-color
3234 'priority nil
3235 (cdr (assoc p org-priority-faces))))
3236 ((and (listp org-agenda-fontify-priorities)
3237 (org-face-from-face-or-color
3238 'priority nil
3239 (cdr (assoc p org-agenda-fontify-priorities)))))
3240 ((equal p l) 'italic)
3241 ((equal p h) 'bold)))
3242 (overlay-put ov 'org-type 'org-priority)))))
3244 (defun org-agenda-dim-blocked-tasks ()
3245 "Dim currently blocked TODO's in the agenda display."
3246 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3247 (delete-overlay o)))
3248 (overlays-in (point-min) (point-max)))
3249 (save-excursion
3250 (let ((inhibit-read-only t)
3251 (org-depend-tag-blocked nil)
3252 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3253 org-blocked-by-checkboxes
3254 invis1 b e p ov h l)
3255 (goto-char (point-min))
3256 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3257 (and pos (goto-char (1+ pos))))
3258 (setq org-blocked-by-checkboxes nil invis1 invis)
3259 (let ((marker (org-get-at-bol 'org-hd-marker)))
3260 (when (and marker
3261 (with-current-buffer (marker-buffer marker)
3262 (save-excursion (goto-char marker)
3263 (org-entry-blocked-p))))
3264 (if org-blocked-by-checkboxes (setq invis1 nil))
3265 (setq b (if invis1
3266 (max (point-min) (1- (point-at-bol)))
3267 (point-at-bol))
3268 e (point-at-eol)
3269 ov (make-overlay b e))
3270 (if invis1
3271 (overlay-put ov 'invisible t)
3272 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3273 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3275 (defvar org-agenda-skip-function nil
3276 "Function to be called at each match during agenda construction.
3277 If this function returns nil, the current match should not be skipped.
3278 Otherwise, the function must return a position from where the search
3279 should be continued.
3280 This may also be a Lisp form, it will be evaluated.
3281 Never set this variable using `setq' or so, because then it will apply
3282 to all future agenda commands. If you do want a global skipping condition,
3283 use the option `org-agenda-skip-function-global' instead.
3284 The correct usage for `org-agenda-skip-function' is to bind it with
3285 `let' to scope it dynamically into the agenda-constructing command.
3286 A good way to set it is through options in `org-agenda-custom-commands'.")
3288 (defun org-agenda-skip ()
3289 "Throw to `:skip' in places that should be skipped.
3290 Also moves point to the end of the skipped region, so that search can
3291 continue from there."
3292 (let ((p (point-at-bol)) to)
3293 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3294 (get-text-property p :org-archived)
3295 (org-end-of-subtree t)
3296 (throw :skip t))
3297 (and org-agenda-skip-comment-trees
3298 (get-text-property p :org-comment)
3299 (org-end-of-subtree t)
3300 (throw :skip t))
3301 (if (equal (char-after p) ?#) (throw :skip t))
3302 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3303 (org-agenda-skip-eval org-agenda-skip-function)))
3304 (goto-char to)
3305 (throw :skip t))))
3307 (defun org-agenda-skip-eval (form)
3308 "If FORM is a function or a list, call (or eval) is and return result.
3309 `save-excursion' and `save-match-data' are wrapped around the call, so point
3310 and match data are returned to the previous state no matter what these
3311 functions do."
3312 (let (fp)
3313 (and form
3314 (or (setq fp (functionp form))
3315 (consp form))
3316 (save-excursion
3317 (save-match-data
3318 (if fp
3319 (funcall form)
3320 (eval form)))))))
3322 (defvar org-agenda-markers nil
3323 "List of all currently active markers created by `org-agenda'.")
3324 (defvar org-agenda-last-marker-time (org-float-time)
3325 "Creation time of the last agenda marker.")
3327 (defun org-agenda-new-marker (&optional pos)
3328 "Return a new agenda marker.
3329 Org-mode keeps a list of these markers and resets them when they are
3330 no longer in use."
3331 (let ((m (copy-marker (or pos (point)))))
3332 (setq org-agenda-last-marker-time (org-float-time))
3333 (push m org-agenda-markers)
3336 (defun org-agenda-reset-markers ()
3337 "Reset markers created by `org-agenda'."
3338 (while org-agenda-markers
3339 (move-marker (pop org-agenda-markers) nil)))
3341 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3342 "Save relative positions of markers in region."
3343 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3344 org-agenda-markers))
3346 ;;; Entry text mode
3348 (defun org-agenda-entry-text-show-here ()
3349 "Add some text from the entry as context to the current line."
3350 (let (m txt o)
3351 (setq m (org-get-at-bol 'org-hd-marker))
3352 (unless (marker-buffer m)
3353 (error "No marker points to an entry here"))
3354 (setq txt (concat "\n" (org-no-properties
3355 (org-agenda-get-some-entry-text
3356 m org-agenda-entry-text-maxlines " > "))))
3357 (when (string-match "\\S-" txt)
3358 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3359 (overlay-put o 'evaporate t)
3360 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3361 (overlay-put o 'after-string txt))))
3363 (defun org-agenda-entry-text-show ()
3364 "Add entry context for all agenda lines."
3365 (interactive)
3366 (save-excursion
3367 (goto-char (point-max))
3368 (beginning-of-line 1)
3369 (while (not (bobp))
3370 (when (org-get-at-bol 'org-hd-marker)
3371 (org-agenda-entry-text-show-here))
3372 (beginning-of-line 0))))
3374 (defun org-agenda-entry-text-hide ()
3375 "Remove any shown entry context."
3376 (delq nil
3377 (mapcar (lambda (o)
3378 (if (eq (overlay-get o 'org-overlay-type)
3379 'agenda-entry-content)
3380 (progn (delete-overlay o) t)))
3381 (overlays-in (point-min) (point-max)))))
3383 (defun org-agenda-get-day-face (date)
3384 "Return the face DATE should be displayed with."
3385 (or (and (functionp org-agenda-day-face-function)
3386 (funcall org-agenda-day-face-function date))
3387 (cond ((org-agenda-todayp date)
3388 'org-agenda-date-today)
3389 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3390 'org-agenda-date-weekend)
3391 (t 'org-agenda-date))))
3393 ;;; Agenda timeline
3395 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3397 (defun org-timeline (&optional dotodo)
3398 "Show a time-sorted view of the entries in the current org file.
3399 Only entries with a time stamp of today or later will be listed. With
3400 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3401 under the current date.
3402 If the buffer contains an active region, only check the region for
3403 dates."
3404 (interactive "P")
3405 (org-compile-prefix-format 'timeline)
3406 (org-set-sorting-strategy 'timeline)
3407 (let* ((dopast t)
3408 (doclosed org-agenda-show-log)
3409 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3410 (current-buffer))))
3411 (date (calendar-current-date))
3412 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3413 (end (if (org-region-active-p) (region-end) (point-max)))
3414 (day-numbers (org-get-all-dates beg end 'no-ranges
3415 t doclosed ; always include today
3416 org-timeline-show-empty-dates))
3417 (org-deadline-warning-days 0)
3418 (org-agenda-only-exact-dates t)
3419 (today (org-today))
3420 (past t)
3421 args
3422 s e rtn d emptyp)
3423 (setq org-agenda-redo-command
3424 (list 'progn
3425 (list 'org-switch-to-buffer-other-window (current-buffer))
3426 (list 'org-timeline (list 'quote dotodo))))
3427 (if (not dopast)
3428 ;; Remove past dates from the list of dates.
3429 (setq day-numbers (delq nil (mapcar (lambda(x)
3430 (if (>= x today) x nil))
3431 day-numbers))))
3432 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3433 (if doclosed (push :closed args))
3434 (push :timestamp args)
3435 (push :deadline args)
3436 (push :scheduled args)
3437 (push :sexp args)
3438 (if dotodo (push :todo args))
3439 (insert "Timeline of file " entry "\n")
3440 (add-text-properties (point-min) (point)
3441 (list 'face 'org-agenda-structure))
3442 (org-agenda-mark-header-line (point-min))
3443 (while (setq d (pop day-numbers))
3444 (if (and (listp d) (eq (car d) :omitted))
3445 (progn
3446 (setq s (point))
3447 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3448 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3449 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3450 (if (and (>= d today)
3451 dopast
3452 past)
3453 (progn
3454 (setq past nil)
3455 (insert (make-string 79 ?-) "\n")))
3456 (setq date (calendar-gregorian-from-absolute d))
3457 (setq s (point))
3458 (setq rtn (and (not emptyp)
3459 (apply 'org-agenda-get-day-entries entry
3460 date args)))
3461 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3462 (progn
3463 (insert
3464 (if (stringp org-agenda-format-date)
3465 (format-time-string org-agenda-format-date
3466 (org-time-from-absolute date))
3467 (funcall org-agenda-format-date date))
3468 "\n")
3469 (put-text-property s (1- (point)) 'face
3470 (org-agenda-get-day-face date))
3471 (put-text-property s (1- (point)) 'org-date-line t)
3472 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3473 (if (equal d today)
3474 (put-text-property s (1- (point)) 'org-today t))
3475 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3476 (put-text-property s (1- (point)) 'day d)))))
3477 (goto-char (point-min))
3478 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3479 (point-min)))
3480 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3481 (org-finalize-agenda)
3482 (setq buffer-read-only t)))
3484 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3485 "Return a list of all relevant day numbers from BEG to END buffer positions.
3486 If NO-RANGES is non-nil, include only the start and end dates of a range,
3487 not every single day in the range. If FORCE-TODAY is non-nil, make
3488 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3489 inactive time stamps (those in square brackets) are included.
3490 When EMPTY is non-nil, also include days without any entries."
3491 (let ((re (concat
3492 (if pre-re pre-re "")
3493 (if inactive org-ts-regexp-both org-ts-regexp)))
3494 dates dates1 date day day1 day2 ts1 ts2 pos)
3495 (if force-today
3496 (setq dates (list (org-today))))
3497 (save-excursion
3498 (goto-char beg)
3499 (while (re-search-forward re end t)
3500 (setq day (time-to-days (org-time-string-to-time
3501 (substring (match-string 1) 0 10)
3502 (current-buffer) (match-beginning 0))))
3503 (or (memq day dates) (push day dates)))
3504 (unless no-ranges
3505 (goto-char beg)
3506 (while (re-search-forward org-tr-regexp end t)
3507 (setq pos (match-beginning 0))
3508 (setq ts1 (substring (match-string 1) 0 10)
3509 ts2 (substring (match-string 2) 0 10)
3510 day1 (time-to-days (org-time-string-to-time
3511 ts1 (current-buffer) pos))
3512 day2 (time-to-days (org-time-string-to-time
3513 ts2 (current-buffer) pos)))
3514 (while (< (setq day1 (1+ day1)) day2)
3515 (or (memq day1 dates) (push day1 dates)))))
3516 (setq dates (sort dates '<))
3517 (when empty
3518 (while (setq day (pop dates))
3519 (setq day2 (car dates))
3520 (push day dates1)
3521 (when (and day2 empty)
3522 (if (or (eq empty t)
3523 (and (numberp empty) (<= (- day2 day) empty)))
3524 (while (< (setq day (1+ day)) day2)
3525 (push (list day) dates1))
3526 (push (cons :omitted (- day2 day)) dates1))))
3527 (setq dates (nreverse dates1)))
3528 dates)))
3530 ;;; Agenda Daily/Weekly
3532 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3533 "Start day for the agenda view.
3534 Custom commands can set this variable in the options section.")
3535 (defvar org-starting-day nil) ; local variable in the agenda buffer
3536 (defvar org-agenda-current-span nil
3537 "The current span used in the agenda view.") ; local variable in the agenda buffer
3538 (defvar org-arg-loc nil) ; local variable
3540 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3541 "List of types searched for when creating the daily/weekly agenda.
3542 This variable is a list of symbols that controls the types of
3543 items that appear in the daily/weekly agenda. Allowed symbols in this
3544 list are are
3546 :timestamp List items containing a date stamp or date range matching
3547 the selected date. This includes sexp entries in
3548 angular brackets.
3550 :sexp List entries resulting from plain diary-like sexps.
3552 :deadline List deadline due on that date. When the date is today,
3553 also list any deadlines past due, or due within
3554 `org-deadline-warning-days'. `:deadline' must appear before
3555 `:scheduled' if the setting of
3556 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3557 any effect.
3559 :scheduled List all items which are scheduled for the given date.
3560 The diary for *today* also contains items which were
3561 scheduled earlier and are not yet marked DONE.
3563 By default, all four types are turned on.
3565 Never set this variable globally using `setq', because then it
3566 will apply to all future agenda commands. Instead, bind it with
3567 `let' to scope it dynamically into the agenda-constructing
3568 command. A good way to set it is through options in
3569 `org-agenda-custom-commands'. For a more flexible (though
3570 somewhat less efficient) way of determining what is included in
3571 the daily/weekly agenda, see `org-agenda-skip-function'.")
3573 ;;;###autoload
3574 (defun org-agenda-list (&optional arg start-day span)
3575 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3576 The view will be for the current day or week, but from the overview buffer
3577 you will be able to go to other days/weeks.
3579 With a numeric prefix argument in an interactive call, the agenda will
3580 span ARG days. Lisp programs should instead specify SPAN to change
3581 the number of days. SPAN defaults to `org-agenda-span'.
3583 START-DAY defaults to TODAY, or to the most recent match for the weekday
3584 given in `org-agenda-start-on-weekday'."
3585 (interactive "P")
3586 (if (and (integerp arg) (> arg 0))
3587 (setq span arg arg nil))
3588 (setq start-day (or start-day org-agenda-start-day))
3589 (if org-agenda-overriding-arguments
3590 (setq arg (car org-agenda-overriding-arguments)
3591 start-day (nth 1 org-agenda-overriding-arguments)
3592 span (nth 2 org-agenda-overriding-arguments)))
3593 (if (stringp start-day)
3594 ;; Convert to an absolute day number
3595 (setq start-day (time-to-days (org-read-date nil t start-day))))
3596 (setq org-agenda-last-arguments (list arg start-day span))
3597 (org-compile-prefix-format 'agenda)
3598 (org-set-sorting-strategy 'agenda)
3599 (let* ((span (org-agenda-ndays-to-span
3600 (or span org-agenda-ndays org-agenda-span)))
3601 (today (org-today))
3602 (sd (or start-day today))
3603 (ndays (org-agenda-span-to-ndays span sd))
3604 (org-agenda-start-on-weekday
3605 (if (eq ndays 7)
3606 org-agenda-start-on-weekday))
3607 (thefiles (org-agenda-files nil 'ifmode))
3608 (files thefiles)
3609 (start (if (or (null org-agenda-start-on-weekday)
3610 (< ndays 7))
3612 (let* ((nt (calendar-day-of-week
3613 (calendar-gregorian-from-absolute sd)))
3614 (n1 org-agenda-start-on-weekday)
3615 (d (- nt n1)))
3616 (- sd (+ (if (< d 0) 7 0) d)))))
3617 (day-numbers (list start))
3618 (day-cnt 0)
3619 (inhibit-redisplay (not debug-on-error))
3620 s e rtn rtnall file date d start-pos end-pos todayp
3621 clocktable-start clocktable-end filter)
3622 (setq org-agenda-redo-command
3623 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3624 (dotimes (n (1- ndays))
3625 (push (1+ (car day-numbers)) day-numbers))
3626 (setq day-numbers (nreverse day-numbers))
3627 (setq clocktable-start (car day-numbers)
3628 clocktable-end (1+ (or (org-last day-numbers) 0)))
3629 (org-prepare-agenda "Day/Week")
3630 (org-set-local 'org-starting-day (car day-numbers))
3631 (org-set-local 'org-arg-loc arg)
3632 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3633 (unless org-agenda-compact-blocks
3634 (let* ((d1 (car day-numbers))
3635 (d2 (org-last day-numbers))
3636 (w1 (org-days-to-iso-week d1))
3637 (w2 (org-days-to-iso-week d2)))
3638 (setq s (point))
3639 (if org-agenda-overriding-header
3640 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3641 nil 'face 'org-agenda-structure) "\n")
3642 (insert (org-agenda-span-name span)
3643 "-agenda"
3644 (if (< (- d2 d1) 350)
3645 (if (= w1 w2)
3646 (format " (W%02d)" w1)
3647 (format " (W%02d-W%02d)" w1 w2))
3649 ":\n")))
3650 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3651 'org-date-line t))
3652 (org-agenda-mark-header-line s))
3653 (while (setq d (pop day-numbers))
3654 (setq date (calendar-gregorian-from-absolute d)
3655 s (point))
3656 (if (or (setq todayp (= d today))
3657 (and (not start-pos) (= d sd)))
3658 (setq start-pos (point))
3659 (if (and start-pos (not end-pos))
3660 (setq end-pos (point))))
3661 (setq files thefiles
3662 rtnall nil)
3663 (while (setq file (pop files))
3664 (catch 'nextfile
3665 (org-check-agenda-file file)
3666 (let ((org-agenda-entry-types org-agenda-entry-types))
3667 (unless org-agenda-include-deadlines
3668 (setq org-agenda-entry-types
3669 (delq :deadline org-agenda-entry-types)))
3670 (cond
3671 ((memq org-agenda-show-log '(only clockcheck))
3672 (setq rtn (org-agenda-get-day-entries
3673 file date :closed)))
3674 (org-agenda-show-log
3675 (setq rtn (apply 'org-agenda-get-day-entries
3676 file date
3677 (append '(:closed) org-agenda-entry-types))))
3679 (setq rtn (apply 'org-agenda-get-day-entries
3680 file date
3681 org-agenda-entry-types)))))
3682 (setq rtnall (append rtnall rtn)))) ;; all entries
3683 (if org-agenda-include-diary
3684 (let ((org-agenda-search-headline-for-time t))
3685 (require 'diary-lib)
3686 (setq rtn (org-get-entries-from-diary date))
3687 (setq rtnall (append rtnall rtn))))
3688 (if (or rtnall org-agenda-show-all-dates)
3689 (progn
3690 (setq day-cnt (1+ day-cnt))
3691 (insert
3692 (if (stringp org-agenda-format-date)
3693 (format-time-string org-agenda-format-date
3694 (org-time-from-absolute date))
3695 (funcall org-agenda-format-date date))
3696 "\n")
3697 (put-text-property s (1- (point)) 'face
3698 (org-agenda-get-day-face date))
3699 (put-text-property s (1- (point)) 'org-date-line t)
3700 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3701 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3702 (when todayp
3703 (put-text-property s (1- (point)) 'org-today t))
3704 (if rtnall (insert ;; all entries
3705 (org-finalize-agenda-entries
3706 (org-agenda-add-time-grid-maybe
3707 rtnall ndays todayp))
3708 "\n"))
3709 (put-text-property s (1- (point)) 'day d)
3710 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3711 (when (and org-agenda-clockreport-mode clocktable-start)
3712 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3713 ;; the above line is to ensure the restricted range!
3714 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3715 tbl)
3716 (setq p (org-plist-delete p :block))
3717 (setq p (plist-put p :tstart clocktable-start))
3718 (setq p (plist-put p :tend clocktable-end))
3719 (setq p (plist-put p :scope 'agenda))
3720 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3721 (setq filter (or org-agenda-tag-filter-while-redo
3722 (get 'org-agenda-tag-filter :preset-filter))))
3723 (setq p (plist-put p :tags (mapconcat (lambda (x)
3724 (if (string-match "[<>=]" x)
3727 filter ""))))
3728 (setq tbl (apply 'org-get-clocktable p))
3729 (insert tbl)))
3730 (goto-char (point-min))
3731 (or org-agenda-multi (org-fit-agenda-window))
3732 (unless (and (pos-visible-in-window-p (point-min))
3733 (pos-visible-in-window-p (point-max)))
3734 (goto-char (1- (point-max)))
3735 (recenter -1)
3736 (if (not (pos-visible-in-window-p (or start-pos 1)))
3737 (progn
3738 (goto-char (or start-pos 1))
3739 (recenter 1))))
3740 (goto-char (or start-pos 1))
3741 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3742 (if (eq org-agenda-show-log 'clockcheck)
3743 (org-agenda-show-clocking-issues))
3744 (org-finalize-agenda)
3745 (setq buffer-read-only t)
3746 (message "")))
3748 (defun org-agenda-ndays-to-span (n)
3749 "Return a span symbol for a span of N days, or N if none matches."
3750 (cond ((symbolp n) n)
3751 ((= n 1) 'day)
3752 ((= n 7) 'week)
3753 (t n)))
3755 (defun org-agenda-span-to-ndays (span start-day)
3756 "Return ndays from SPAN starting at START-DAY."
3757 (cond ((numberp span) span)
3758 ((eq span 'day) 1)
3759 ((eq span 'week) 7)
3760 ((eq span 'month)
3761 (let ((date (calendar-gregorian-from-absolute start-day)))
3762 (calendar-last-day-of-month (car date) (caddr date))))
3763 ((eq span 'year)
3764 (let ((date (calendar-gregorian-from-absolute start-day)))
3765 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3767 (defun org-agenda-span-name (span)
3768 "Return a SPAN name."
3769 (if (null span)
3771 (if (symbolp span)
3772 (capitalize (symbol-name span))
3773 (format "%d days" span))))
3775 ;;; Agenda word search
3777 (defvar org-agenda-search-history nil)
3778 (defvar org-todo-only nil)
3780 (defvar org-search-syntax-table nil
3781 "Special syntax table for org-mode search.
3782 In this table, we have single quotes not as word constituents, to
3783 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3785 (defun org-search-syntax-table ()
3786 (unless org-search-syntax-table
3787 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3788 (modify-syntax-entry ?' "." org-search-syntax-table)
3789 (modify-syntax-entry ?` "." org-search-syntax-table))
3790 org-search-syntax-table)
3792 (defvar org-agenda-last-search-view-search-was-boolean nil)
3794 ;;;###autoload
3795 (defun org-search-view (&optional todo-only string edit-at)
3796 "Show all entries that contain a phrase or words or regular expressions.
3798 With optional prefix argument TODO-ONLY, only consider entries that are
3799 TODO entries. The argument STRING can be used to pass a default search
3800 string into this function. If EDIT-AT is non-nil, it means that the
3801 user should get a chance to edit this string, with cursor at position
3802 EDIT-AT.
3804 The search string can be viewed either as a phrase that should be found as
3805 is, or it can be broken into a number of snippets, each of which must match
3806 in a Boolean way to select an entry. The default depends on the variable
3807 `org-agenda-search-view-always-boolean'.
3808 Even if this is turned off (the default) you can always switch to
3809 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3811 The default is a direct search of the whole phrase, where each space in
3812 the search string can expand to an arbitrary amount of whitespace,
3813 including newlines.
3815 If using a Boolean search, the search string is split on whitespace and
3816 each snippet is searched separately, with logical AND to select an entry.
3817 Words prefixed with a minus must *not* occur in the entry. Words without
3818 a prefix or prefixed with a plus must occur in the entry. Matching is
3819 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3820 match whole words, not parts of a word) if
3821 `org-agenda-search-view-force-full-words' is set (default is nil).
3823 Boolean search snippets enclosed by curly braces are interpreted as
3824 regular expressions that must or (when preceded with \"-\") must not
3825 match in the entry. Snippets enclosed into double quotes will be taken
3826 as a whole, to include whitespace.
3828 - If the search string starts with an asterisk, search only in headlines.
3829 - If (possibly after the leading star) the search string starts with an
3830 exclamation mark, this also means to look at TODO entries only, an effect
3831 that can also be achieved with a prefix argument.
3832 - If (possibly after star and exclamation mark) the search string starts
3833 with a colon, this will mean that the (non-regexp) snippets of the
3834 Boolean search must match as full words.
3836 This command searches the agenda files, and in addition the files listed
3837 in `org-agenda-text-search-extra-files'."
3838 (interactive "P")
3839 (org-compile-prefix-format 'search)
3840 (org-set-sorting-strategy 'search)
3841 (org-prepare-agenda "SEARCH")
3842 (let* ((props (list 'face nil
3843 'done-face 'org-agenda-done
3844 'org-not-done-regexp org-not-done-regexp
3845 'org-todo-regexp org-todo-regexp
3846 'org-complex-heading-regexp org-complex-heading-regexp
3847 'mouse-face 'highlight
3848 'help-echo (format "mouse-2 or RET jump to location")))
3849 (full-words org-agenda-search-view-force-full-words)
3850 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3851 regexp rtn rtnall files file pos
3852 marker category org-category-pos tags c neg re boolean
3853 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3854 (unless (and (not edit-at)
3855 (stringp string)
3856 (string-match "\\S-" string))
3857 (setq string (read-string
3858 (if org-agenda-search-view-always-boolean
3859 "[+-]Word/{Regexp} ...: "
3860 "Phrase, or [+-]Word/{Regexp} ...: ")
3861 (cond
3862 ((integerp edit-at) (cons string edit-at))
3863 (edit-at string))
3864 'org-agenda-search-history)))
3865 (org-set-local 'org-todo-only todo-only)
3866 (setq org-agenda-redo-command
3867 (list 'org-search-view (if todo-only t nil) string
3868 '(if current-prefix-arg 1 nil)))
3869 (setq org-agenda-query-string string)
3871 (if (equal (string-to-char string) ?*)
3872 (setq hdl-only t
3873 words (substring string 1))
3874 (setq words string))
3875 (when (equal (string-to-char words) ?!)
3876 (setq todo-only t
3877 words (substring words 1)))
3878 (when (equal (string-to-char words) ?:)
3879 (setq full-words t
3880 words (substring words 1)))
3881 (if (or org-agenda-search-view-always-boolean
3882 (member (string-to-char words) '(?- ?+ ?\{)))
3883 (setq boolean t))
3884 (setq words (org-split-string words))
3885 (let (www w)
3886 (while (setq w (pop words))
3887 (while (and (string-match "\\\\\\'" w) words)
3888 (setq w (concat (substring w 0 -1) " " (pop words))))
3889 (push w www))
3890 (setq words (nreverse www) www nil)
3891 (while (setq w (pop words))
3892 (when (and (string-match "\\`[-+]?{" w)
3893 (not (string-match "}\\'" w)))
3894 (while (and words (not (string-match "}\\'" (car words))))
3895 (setq w (concat w " " (pop words))))
3896 (setq w (concat w " " (pop words))))
3897 (push w www))
3898 (setq words (nreverse www)))
3899 (setq org-agenda-last-search-view-search-was-boolean boolean)
3900 (when boolean
3901 (let (wds w)
3902 (while (setq w (pop words))
3903 (if (or (equal (substring w 0 1) "\"")
3904 (and (> (length w) 1)
3905 (member (substring w 0 1) '("+" "-"))
3906 (equal (substring w 1 2) "\"")))
3907 (while (and words (not (equal (substring w -1) "\"")))
3908 (setq w (concat w " " (pop words)))))
3909 (and (string-match "\\`\\([-+]?\\)\"" w)
3910 (setq w (replace-match "\\1" nil nil w)))
3911 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3912 (push w wds))
3913 (setq words (nreverse wds))))
3914 (if boolean
3915 (mapc (lambda (w)
3916 (setq c (string-to-char w))
3917 (if (equal c ?-)
3918 (setq neg t w (substring w 1))
3919 (if (equal c ?+)
3920 (setq neg nil w (substring w 1))
3921 (setq neg nil)))
3922 (if (string-match "\\`{.*}\\'" w)
3923 (setq re (substring w 1 -1))
3924 (if full-words
3925 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3926 (setq re (regexp-quote (downcase w)))))
3927 (if neg (push re regexps-) (push re regexps+)))
3928 words)
3929 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3930 regexps+))
3931 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3932 (if (not regexps+)
3933 (setq regexp org-outline-regexp-bol)
3934 (setq regexp (pop regexps+))
3935 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
3936 regexp))))
3937 (setq files (org-agenda-files nil 'ifmode))
3938 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3939 (pop org-agenda-text-search-extra-files)
3940 (setq files (org-add-archive-files files)))
3941 (setq files (append files org-agenda-text-search-extra-files)
3942 rtnall nil)
3943 (while (setq file (pop files))
3944 (setq ee nil)
3945 (catch 'nextfile
3946 (org-check-agenda-file file)
3947 (setq buffer (if (file-exists-p file)
3948 (org-get-agenda-file-buffer file)
3949 (error "No such file %s" file)))
3950 (if (not buffer)
3951 ;; If file does not exist, make sure an error message is sent
3952 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3953 file))))
3954 (with-current-buffer buffer
3955 (with-syntax-table (org-search-syntax-table)
3956 (unless (eq major-mode 'org-mode)
3957 (error "Agenda file %s is not in `org-mode'" file))
3958 (let ((case-fold-search t))
3959 (save-excursion
3960 (save-restriction
3961 (if org-agenda-restrict
3962 (narrow-to-region org-agenda-restrict-begin
3963 org-agenda-restrict-end)
3964 (widen))
3965 (goto-char (point-min))
3966 (unless (or (org-at-heading-p)
3967 (outline-next-heading))
3968 (throw 'nextfile t))
3969 (goto-char (max (point-min) (1- (point))))
3970 (while (re-search-forward regexp nil t)
3971 (org-back-to-heading t)
3972 (skip-chars-forward "* ")
3973 (setq beg (point-at-bol)
3974 beg1 (point)
3975 end (progn (outline-next-heading) (point)))
3976 (catch :skip
3977 (goto-char beg)
3978 (org-agenda-skip)
3979 (setq str (buffer-substring-no-properties
3980 (point-at-bol)
3981 (if hdl-only (point-at-eol) end)))
3982 (mapc (lambda (wr) (when (string-match wr str)
3983 (goto-char (1- end))
3984 (throw :skip t)))
3985 regexps-)
3986 (mapc (lambda (wr) (unless (string-match wr str)
3987 (goto-char (1- end))
3988 (throw :skip t)))
3989 (if todo-only
3990 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3991 regexps+)
3992 regexps+))
3993 (goto-char beg)
3994 (setq marker (org-agenda-new-marker (point))
3995 category (org-get-category)
3996 org-category-pos (get-text-property (point) 'org-category-position)
3997 tags (org-get-tags-at (point))
3998 txt (org-agenda-format-item
4000 (buffer-substring-no-properties
4001 beg1 (point-at-eol))
4002 category tags))
4003 (org-add-props txt props
4004 'org-marker marker 'org-hd-marker marker
4005 'org-todo-regexp org-todo-regexp
4006 'org-complex-heading-regexp org-complex-heading-regexp
4007 'priority 1000 'org-category category
4008 'org-category-position org-category-pos
4009 'type "search")
4010 (push txt ee)
4011 (goto-char (1- end))))))))))
4012 (setq rtn (nreverse ee))
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 "Search words: ")
4018 (add-text-properties (point-min) (1- (point))
4019 (list 'face 'org-agenda-structure))
4020 (setq pos (point))
4021 (insert string "\n")
4022 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4023 (setq pos (point))
4024 (unless org-agenda-multi
4025 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4026 (add-text-properties pos (1- (point))
4027 (list 'face 'org-agenda-structure))))
4028 (org-agenda-mark-header-line (point-min))
4029 (when rtnall
4030 (insert (org-finalize-agenda-entries rtnall) "\n"))
4031 (goto-char (point-min))
4032 (or org-agenda-multi (org-fit-agenda-window))
4033 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4034 (org-finalize-agenda)
4035 (setq buffer-read-only t)))
4037 ;;; Agenda TODO list
4039 (defvar org-select-this-todo-keyword nil)
4040 (defvar org-last-arg nil)
4042 ;;;###autoload
4043 (defun org-todo-list (arg)
4044 "Show all (not done) TODO entries from all agenda file in a single list.
4045 The prefix arg can be used to select a specific TODO keyword and limit
4046 the list to these. When using \\[universal-argument], you will be prompted
4047 for a keyword. A numeric prefix directly selects the Nth keyword in
4048 `org-todo-keywords-1'."
4049 (interactive "P")
4050 (org-compile-prefix-format 'todo)
4051 (org-set-sorting-strategy 'todo)
4052 (org-prepare-agenda "TODO")
4053 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4054 (let* ((today (org-today))
4055 (date (calendar-gregorian-from-absolute today))
4056 (kwds org-todo-keywords-for-agenda)
4057 (completion-ignore-case t)
4058 (org-select-this-todo-keyword
4059 (if (stringp arg) arg
4060 (and arg (integerp arg) (> arg 0)
4061 (nth (1- arg) kwds))))
4062 rtn rtnall files file pos)
4063 (when (equal arg '(4))
4064 (setq org-select-this-todo-keyword
4065 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4066 (mapcar 'list kwds) nil nil)))
4067 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4068 (org-set-local 'org-last-arg arg)
4069 (setq org-agenda-redo-command
4070 '(org-todo-list (or current-prefix-arg org-last-arg)))
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 rtn (org-agenda-get-day-entries file date :todo))
4077 (setq rtnall (append rtnall rtn))))
4078 (if org-agenda-overriding-header
4079 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4080 nil 'face 'org-agenda-structure) "\n")
4081 (insert "Global list of TODO items of type: ")
4082 (add-text-properties (point-min) (1- (point))
4083 (list 'face 'org-agenda-structure
4084 'short-heading
4085 (concat "ToDo: "
4086 (or org-select-this-todo-keyword "ALL"))))
4087 (org-agenda-mark-header-line (point-min))
4088 (setq pos (point))
4089 (insert (or org-select-this-todo-keyword "ALL") "\n")
4090 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4091 (setq pos (point))
4092 (unless org-agenda-multi
4093 (insert "Available with `N r': (0)ALL")
4094 (let ((n 0) s)
4095 (mapc (lambda (x)
4096 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4097 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4098 (insert "\n "))
4099 (insert " " s))
4100 kwds))
4101 (insert "\n"))
4102 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4103 (org-agenda-mark-header-line (point-min))
4104 (when rtnall
4105 (insert (org-finalize-agenda-entries rtnall) "\n"))
4106 (goto-char (point-min))
4107 (or org-agenda-multi (org-fit-agenda-window))
4108 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4109 (org-finalize-agenda)
4110 (setq buffer-read-only t)))
4112 ;;; Agenda tags match
4114 ;;;###autoload
4115 (defun org-tags-view (&optional todo-only match)
4116 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4117 The prefix arg TODO-ONLY limits the search to TODO entries."
4118 (interactive "P")
4119 (org-compile-prefix-format 'tags)
4120 (org-set-sorting-strategy 'tags)
4121 (let* ((org-tags-match-list-sublevels
4122 org-tags-match-list-sublevels)
4123 (completion-ignore-case t)
4124 rtn rtnall files file pos matcher
4125 buffer)
4126 (when (and (stringp match) (not (string-match "\\S-" match)))
4127 (setq match nil))
4128 (setq matcher (org-make-tags-matcher match)
4129 match (car matcher) matcher (cdr matcher))
4130 (org-prepare-agenda (concat "TAGS " match))
4131 (setq org-agenda-query-string match)
4132 (setq org-agenda-redo-command
4133 (list 'org-tags-view (list 'quote todo-only)
4134 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4135 (setq files (org-agenda-files nil 'ifmode)
4136 rtnall nil)
4137 (while (setq file (pop files))
4138 (catch 'nextfile
4139 (org-check-agenda-file file)
4140 (setq buffer (if (file-exists-p file)
4141 (org-get-agenda-file-buffer file)
4142 (error "No such file %s" file)))
4143 (if (not buffer)
4144 ;; If file does not exist, error message to agenda
4145 (setq rtn (list
4146 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4147 rtnall (append rtnall rtn))
4148 (with-current-buffer buffer
4149 (unless (eq major-mode 'org-mode)
4150 (error "Agenda file %s is not in `org-mode'" file))
4151 (save-excursion
4152 (save-restriction
4153 (if org-agenda-restrict
4154 (narrow-to-region org-agenda-restrict-begin
4155 org-agenda-restrict-end)
4156 (widen))
4157 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4158 (setq rtnall (append rtnall rtn))))))))
4159 (if org-agenda-overriding-header
4160 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4161 nil 'face 'org-agenda-structure) "\n")
4162 (insert "Headlines with TAGS match: ")
4163 (add-text-properties (point-min) (1- (point))
4164 (list 'face 'org-agenda-structure
4165 'short-heading
4166 (concat "Match: " match)))
4167 (setq pos (point))
4168 (insert match "\n")
4169 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4170 (setq pos (point))
4171 (unless org-agenda-multi
4172 (insert "Press `C-u r' to search again with new search string\n"))
4173 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4174 (org-agenda-mark-header-line (point-min))
4175 (when rtnall
4176 (insert (org-finalize-agenda-entries rtnall) "\n"))
4177 (goto-char (point-min))
4178 (or org-agenda-multi (org-fit-agenda-window))
4179 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4180 (org-finalize-agenda)
4181 (setq buffer-read-only t)))
4183 ;;; Agenda Finding stuck projects
4185 (defvar org-agenda-skip-regexp nil
4186 "Regular expression used in skipping subtrees for the agenda.
4187 This is basically a temporary global variable that can be set and then
4188 used by user-defined selections using `org-agenda-skip-function'.")
4190 (defvar org-agenda-overriding-header nil
4191 "When set during agenda, todo and tags searches it replaces the header.
4192 This variable should not be set directly, but custom commands can bind it
4193 in the options section.")
4195 (defun org-agenda-skip-entry-when-regexp-matches ()
4196 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4197 If yes, it returns the end position of this entry, causing agenda commands
4198 to skip the entry but continuing the search in the subtree. This is a
4199 function that can be put into `org-agenda-skip-function' for the duration
4200 of a command."
4201 (let ((end (save-excursion (org-end-of-subtree t)))
4202 skip)
4203 (save-excursion
4204 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4205 (and skip end)))
4207 (defun org-agenda-skip-subtree-when-regexp-matches ()
4208 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4209 If yes, it returns the end position of this tree, causing agenda commands
4210 to skip this subtree. This is a function that can be put into
4211 `org-agenda-skip-function' for the duration of a command."
4212 (let ((end (save-excursion (org-end-of-subtree t)))
4213 skip)
4214 (save-excursion
4215 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4216 (and skip end)))
4218 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4219 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4220 If yes, it returns the end position of the current entry (NOT the tree),
4221 causing agenda commands to skip the entry but continuing the search in
4222 the subtree. This is a function that can be put into
4223 `org-agenda-skip-function' for the duration of a command. An important
4224 use of this function is for the stuck project list."
4225 (let ((end (save-excursion (org-end-of-subtree t)))
4226 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4227 skip)
4228 (save-excursion
4229 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4230 (and skip entry-end)))
4232 (defun org-agenda-skip-entry-if (&rest conditions)
4233 "Skip entry if any of CONDITIONS is true.
4234 See `org-agenda-skip-if' for details."
4235 (org-agenda-skip-if nil conditions))
4237 (defun org-agenda-skip-subtree-if (&rest conditions)
4238 "Skip entry if any of CONDITIONS is true.
4239 See `org-agenda-skip-if' for details."
4240 (org-agenda-skip-if t conditions))
4242 (defun org-agenda-skip-if (subtree conditions)
4243 "Checks current entity for CONDITIONS.
4244 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4245 the entry (i.e. the text before the next heading) is checked.
4247 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4248 from different tests. Valid conditions are:
4250 scheduled Check if there is a scheduled cookie
4251 notscheduled Check if there is no scheduled cookie
4252 deadline Check if there is a deadline
4253 notdeadline Check if there is no deadline
4254 timestamp Check if there is a timestamp (also deadline or scheduled)
4255 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4256 regexp Check if regexp matches
4257 notregexp Check if regexp does not match.
4258 todo Check if TODO keyword matches
4259 nottodo Check if TODO keyword does not match
4261 The regexp is taken from the conditions list, it must come right after
4262 the `regexp' or `notregexp' element.
4264 `todo' and `nottodo' accept as an argument a list of todo
4265 keywords, which may include \"*\" to match any todo keyword.
4267 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4269 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4271 Instead of a list, a keyword class may be given. For example:
4273 (org-agenda-skip-entry-if 'nottodo 'done)
4275 would skip entries that haven't been marked with any of \"DONE\"
4276 keywords. Possible classes are: `todo', `done', `any'.
4278 If any of these conditions is met, this function returns the end point of
4279 the entity, causing the search to continue from there. This is a function
4280 that can be put into `org-agenda-skip-function' for the duration of a command."
4281 (let (beg end m)
4282 (org-back-to-heading t)
4283 (setq beg (point)
4284 end (if subtree
4285 (progn (org-end-of-subtree t) (point))
4286 (progn (outline-next-heading) (1- (point)))))
4287 (goto-char beg)
4288 (and
4290 (and (memq 'scheduled conditions)
4291 (re-search-forward org-scheduled-time-regexp end t))
4292 (and (memq 'notscheduled conditions)
4293 (not (re-search-forward org-scheduled-time-regexp end t)))
4294 (and (memq 'deadline conditions)
4295 (re-search-forward org-deadline-time-regexp end t))
4296 (and (memq 'notdeadline conditions)
4297 (not (re-search-forward org-deadline-time-regexp end t)))
4298 (and (memq 'timestamp conditions)
4299 (re-search-forward org-ts-regexp end t))
4300 (and (memq 'nottimestamp conditions)
4301 (not (re-search-forward org-ts-regexp end t)))
4302 (and (setq m (memq 'regexp conditions))
4303 (stringp (nth 1 m))
4304 (re-search-forward (nth 1 m) end t))
4305 (and (setq m (memq 'notregexp conditions))
4306 (stringp (nth 1 m))
4307 (not (re-search-forward (nth 1 m) end t)))
4308 (and (or
4309 (setq m (memq 'nottodo conditions))
4310 (setq m (memq 'todo conditions)))
4311 (org-agenda-skip-if-todo m end)))
4312 end)))
4314 (defun org-agenda-skip-if-todo (args end)
4315 "Helper function for `org-agenda-skip-if', do not use it directly.
4316 ARGS is a list with first element either `todo' or `nottodo'.
4317 The remainder is either a list of TODO keywords, or a state symbol
4318 `todo' or `done' or `any'."
4319 (let ((kw (car args))
4320 (arg (cadr args))
4321 todo-wds todo-re)
4322 (setq todo-wds
4323 (org-uniquify
4324 (cond
4325 ((listp arg) ;; list of keywords
4326 (if (member "*" arg)
4327 (mapcar 'substring-no-properties org-todo-keywords-1)
4328 arg))
4329 ((symbolp arg) ;; keyword class name
4330 (cond
4331 ((eq arg 'todo)
4332 (org-delete-all org-done-keywords
4333 (mapcar 'substring-no-properties
4334 org-todo-keywords-1)))
4335 ((eq arg 'done) org-done-keywords)
4336 ((eq arg 'any)
4337 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4338 (setq todo-re
4339 (concat "^\\*+[ \t]+\\<\\("
4340 (mapconcat 'identity todo-wds "\\|")
4341 "\\)\\>"))
4342 (if (eq kw 'todo)
4343 (re-search-forward todo-re end t)
4344 (not (re-search-forward todo-re end t)))))
4346 ;;;###autoload
4347 (defun org-agenda-list-stuck-projects (&rest ignore)
4348 "Create agenda view for projects that are stuck.
4349 Stuck projects are project that have no next actions. For the definitions
4350 of what a project is and how to check if it stuck, customize the variable
4351 `org-stuck-projects'."
4352 (interactive)
4353 (let* ((org-agenda-skip-function
4354 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4355 ;; We could have used org-agenda-skip-if here.
4356 (org-agenda-overriding-header
4357 (or org-agenda-overriding-header "List of stuck projects: "))
4358 (matcher (nth 0 org-stuck-projects))
4359 (todo (nth 1 org-stuck-projects))
4360 (todo-wds (if (member "*" todo)
4361 (progn
4362 (org-prepare-agenda-buffers (org-agenda-files
4363 nil 'ifmode))
4364 (org-delete-all
4365 org-done-keywords-for-agenda
4366 (copy-sequence org-todo-keywords-for-agenda)))
4367 todo))
4368 (todo-re (concat "^\\*+[ \t]+\\("
4369 (mapconcat 'identity todo-wds "\\|")
4370 "\\)\\>"))
4371 (tags (nth 2 org-stuck-projects))
4372 (tags-re (if (member "*" tags)
4373 (concat org-outline-regexp-bol
4374 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4375 (if tags
4376 (concat org-outline-regexp-bol
4377 ".*:\\("
4378 (mapconcat 'identity tags "\\|")
4379 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4380 (gen-re (nth 3 org-stuck-projects))
4381 (re-list
4382 (delq nil
4383 (list
4384 (if todo todo-re)
4385 (if tags tags-re)
4386 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4387 gen-re)))))
4388 (setq org-agenda-skip-regexp
4389 (if re-list
4390 (mapconcat 'identity re-list "\\|")
4391 (error "No information how to identify unstuck projects")))
4392 (org-tags-view nil matcher)
4393 (with-current-buffer org-agenda-buffer-name
4394 (setq org-agenda-redo-command
4395 '(org-agenda-list-stuck-projects
4396 (or current-prefix-arg org-last-arg))))))
4398 ;;; Diary integration
4400 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4401 (defvar diary-list-entries-hook)
4402 (defvar diary-time-regexp)
4403 (defun org-get-entries-from-diary (date)
4404 "Get the (Emacs Calendar) diary entries for DATE."
4405 (require 'diary-lib)
4406 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4407 (diary-display-hook '(fancy-diary-display))
4408 (diary-display-function 'fancy-diary-display)
4409 (pop-up-frames nil)
4410 (diary-list-entries-hook
4411 (cons 'org-diary-default-entry diary-list-entries-hook))
4412 (diary-file-name-prefix-function nil) ; turn this feature off
4413 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4414 entries
4415 (org-disable-agenda-to-diary t))
4416 (save-excursion
4417 (save-window-excursion
4418 (funcall (if (fboundp 'diary-list-entries)
4419 'diary-list-entries 'list-diary-entries)
4420 date 1)))
4421 (if (not (get-buffer diary-fancy-buffer))
4422 (setq entries nil)
4423 (with-current-buffer diary-fancy-buffer
4424 (setq buffer-read-only nil)
4425 (if (zerop (buffer-size))
4426 ;; No entries
4427 (setq entries nil)
4428 ;; Omit the date and other unnecessary stuff
4429 (org-agenda-cleanup-fancy-diary)
4430 ;; Add prefix to each line and extend the text properties
4431 (if (zerop (buffer-size))
4432 (setq entries nil)
4433 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4434 (setq entries
4435 (with-temp-buffer
4436 (insert entries) (goto-char (point-min))
4437 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4438 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4439 (replace-match (concat "; " (match-string 1)))))
4440 (buffer-string)))))
4441 (set-buffer-modified-p nil)
4442 (kill-buffer diary-fancy-buffer)))
4443 (when entries
4444 (setq entries (org-split-string entries "\n"))
4445 (setq entries
4446 (mapcar
4447 (lambda (x)
4448 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4449 ;; Extend the text properties to the beginning of the line
4450 (org-add-props x (text-properties-at (1- (length x)) x)
4451 'type "diary" 'date date 'face 'org-agenda-diary))
4452 entries)))))
4454 (defvar org-agenda-cleanup-fancy-diary-hook nil
4455 "Hook run when the fancy diary buffer is cleaned up.")
4457 (defun org-agenda-cleanup-fancy-diary ()
4458 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4459 This gets rid of the date, the underline under the date, and
4460 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4461 date. It also removes lines that contain only whitespace."
4462 (goto-char (point-min))
4463 (if (looking-at ".*?:[ \t]*")
4464 (progn
4465 (replace-match "")
4466 (re-search-forward "\n=+$" nil t)
4467 (replace-match "")
4468 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4469 (re-search-forward "\n=+$" nil t)
4470 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4471 (goto-char (point-min))
4472 (while (re-search-forward "^ +\n" nil t)
4473 (replace-match ""))
4474 (goto-char (point-min))
4475 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4476 (replace-match ""))
4477 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4479 ;; Make sure entries from the diary have the right text properties.
4480 (eval-after-load "diary-lib"
4481 '(if (boundp 'diary-modify-entry-list-string-function)
4482 ;; We can rely on the hook, nothing to do
4484 ;; Hook not available, must use advice to make this work
4485 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4486 "Make the position visible."
4487 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4488 (stringp string)
4489 buffer-file-name)
4490 (setq string (org-modify-diary-entry-string string))))))
4492 (defun org-modify-diary-entry-string (string)
4493 "Add text properties to string, allowing org-mode to act on it."
4494 (org-add-props string nil
4495 'mouse-face 'highlight
4496 'help-echo (if buffer-file-name
4497 (format "mouse-2 or RET jump to diary file %s"
4498 (abbreviate-file-name buffer-file-name))
4500 'org-agenda-diary-link t
4501 'org-marker (org-agenda-new-marker (point-at-bol))))
4503 (defun org-diary-default-entry ()
4504 "Add a dummy entry to the diary.
4505 Needed to avoid empty dates which mess up holiday display."
4506 ;; Catch the error if dealing with the new add-to-diary-alist
4507 (when org-disable-agenda-to-diary
4508 (condition-case nil
4509 (org-add-to-diary-list original-date "Org-mode dummy" "")
4510 (error
4511 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4513 (defun org-add-to-diary-list (&rest args)
4514 (if (fboundp 'diary-add-to-list)
4515 (apply 'diary-add-to-list args)
4516 (apply 'add-to-diary-list args)))
4518 (defvar org-diary-last-run-time nil)
4520 ;;;###autoload
4521 (defun org-diary (&rest args)
4522 "Return diary information from org-files.
4523 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4524 It accesses org files and extracts information from those files to be
4525 listed in the diary. The function accepts arguments specifying what
4526 items should be listed. For a list of arguments allowed here, see the
4527 variable `org-agenda-entry-types'.
4529 The call in the diary file should look like this:
4531 &%%(org-diary) ~/path/to/some/orgfile.org
4533 Use a separate line for each org file to check. Or, if you omit the file name,
4534 all files listed in `org-agenda-files' will be checked automatically:
4536 &%%(org-diary)
4538 If you don't give any arguments (as in the example above), the default
4539 arguments (:deadline :scheduled :timestamp :sexp) are used.
4540 So the example above may also be written as
4542 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4544 The function expects the lisp variables `entry' and `date' to be provided
4545 by the caller, because this is how the calendar works. Don't use this
4546 function from a program - use `org-agenda-get-day-entries' instead."
4547 (when (> (- (org-float-time)
4548 org-agenda-last-marker-time)
4550 (org-agenda-reset-markers))
4551 (org-compile-prefix-format 'agenda)
4552 (org-set-sorting-strategy 'agenda)
4553 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4554 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4555 (list entry)
4556 (org-agenda-files t)))
4557 (time (org-float-time))
4558 file rtn results)
4559 (when (or (not org-diary-last-run-time)
4560 (> (- time
4561 org-diary-last-run-time)
4563 (org-prepare-agenda-buffers files))
4564 (setq org-diary-last-run-time time)
4565 ;; If this is called during org-agenda, don't return any entries to
4566 ;; the calendar. Org Agenda will list these entries itself.
4567 (if org-disable-agenda-to-diary (setq files nil))
4568 (while (setq file (pop files))
4569 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4570 (setq results (append results rtn)))
4571 (if results
4572 (concat (org-finalize-agenda-entries results) "\n"))))
4574 ;;; Agenda entry finders
4576 (defun org-agenda-get-day-entries (file date &rest args)
4577 "Does the work for `org-diary' and `org-agenda'.
4578 FILE is the path to a file to be checked for entries. DATE is date like
4579 the one returned by `calendar-current-date'. ARGS are symbols indicating
4580 which kind of entries should be extracted. For details about these, see
4581 the documentation of `org-diary'."
4582 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4583 (let* ((org-startup-folded nil)
4584 (org-startup-align-all-tables nil)
4585 (buffer (if (file-exists-p file)
4586 (org-get-agenda-file-buffer file)
4587 (error "No such file %s" file)))
4588 arg results rtn deadline-results)
4589 (if (not buffer)
4590 ;; If file does not exist, make sure an error message ends up in diary
4591 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4592 (with-current-buffer buffer
4593 (unless (eq major-mode 'org-mode)
4594 (error "Agenda file %s is not in `org-mode'" file))
4595 (let ((case-fold-search nil))
4596 (save-excursion
4597 (save-restriction
4598 (if org-agenda-restrict
4599 (narrow-to-region org-agenda-restrict-begin
4600 org-agenda-restrict-end)
4601 (widen))
4602 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4603 (while (setq arg (pop args))
4604 (cond
4605 ((and (eq arg :todo)
4606 (equal date (calendar-gregorian-from-absolute
4607 (org-today))))
4608 (setq rtn (org-agenda-get-todos))
4609 (setq results (append results rtn)))
4610 ((eq arg :timestamp)
4611 (setq rtn (org-agenda-get-blocks))
4612 (setq results (append results rtn))
4613 (setq rtn (org-agenda-get-timestamps))
4614 (setq results (append results rtn)))
4615 ((eq arg :sexp)
4616 (setq rtn (org-agenda-get-sexps))
4617 (setq results (append results rtn)))
4618 ((eq arg :scheduled)
4619 (setq rtn (org-agenda-get-scheduled deadline-results))
4620 (setq results (append results rtn)))
4621 ((eq arg :closed)
4622 (setq rtn (org-agenda-get-progress))
4623 (setq results (append results rtn)))
4624 ((eq arg :deadline)
4625 (setq rtn (org-agenda-get-deadlines))
4626 (setq deadline-results (copy-sequence rtn))
4627 (setq results (append results rtn))))))))
4628 results))))
4630 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4631 (defun org-agenda-get-todos ()
4632 "Return the TODO information for agenda display."
4633 (let* ((props (list 'face nil
4634 'done-face 'org-agenda-done
4635 'org-not-done-regexp org-not-done-regexp
4636 'org-todo-regexp org-todo-regexp
4637 'org-complex-heading-regexp org-complex-heading-regexp
4638 'mouse-face 'highlight
4639 'help-echo
4640 (format "mouse-2 or RET jump to org file %s"
4641 (abbreviate-file-name buffer-file-name))))
4642 (regexp (format org-heading-keyword-regexp-format
4643 (cond
4644 ((and org-select-this-todo-keyword
4645 (equal org-select-this-todo-keyword "*"))
4646 org-todo-regexp)
4647 (org-select-this-todo-keyword
4648 (concat "\\("
4649 (mapconcat 'identity
4650 (org-split-string
4651 org-select-this-todo-keyword
4652 "|")
4653 "\\|") "\\)"))
4654 (t org-not-done-regexp))))
4655 marker priority category org-category-pos tags todo-state
4656 ee txt beg end)
4657 (goto-char (point-min))
4658 (while (re-search-forward regexp nil t)
4659 (catch :skip
4660 (save-match-data
4661 (beginning-of-line)
4662 (org-agenda-skip)
4663 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4664 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4665 (goto-char (1+ beg))
4666 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4667 (throw :skip nil)))
4668 (goto-char (match-beginning 2))
4669 (setq marker (org-agenda-new-marker (match-beginning 0))
4670 category (org-get-category)
4671 org-category-pos (get-text-property (point) 'org-category-position)
4672 txt (org-trim
4673 (buffer-substring (match-beginning 2) (match-end 0)))
4674 tags (org-get-tags-at (point))
4675 txt (org-agenda-format-item "" txt category tags)
4676 priority (1+ (org-get-priority txt))
4677 todo-state (org-get-todo-state))
4678 (org-add-props txt props
4679 'org-marker marker 'org-hd-marker marker
4680 'priority priority 'org-category category
4681 'org-category-position org-category-pos
4682 'type "todo" 'todo-state todo-state)
4683 (push txt ee)
4684 (if org-agenda-todo-list-sublevels
4685 (goto-char (match-end 2))
4686 (org-end-of-subtree 'invisible))))
4687 (nreverse ee)))
4689 (defun org-agenda-todo-custom-ignore-p (time n)
4690 "Check whether timestamp is farther away then n number of days.
4691 This function is invoked if `org-agenda-todo-ignore-deadlines',
4692 `org-agenda-todo-ignore-scheduled' or
4693 `org-agenda-todo-ignore-timestamp' is set to an integer."
4694 (let ((days (org-days-to-time time)))
4695 (if (>= n 0)
4696 (>= days n)
4697 (<= days n))))
4699 ;;;###autoload
4700 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4701 (&optional end)
4702 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4703 (when (or org-agenda-todo-ignore-with-date
4704 org-agenda-todo-ignore-scheduled
4705 org-agenda-todo-ignore-deadlines
4706 org-agenda-todo-ignore-timestamp)
4707 (setq end (or end (save-excursion (outline-next-heading) (point))))
4708 (save-excursion
4709 (or (and org-agenda-todo-ignore-with-date
4710 (re-search-forward org-ts-regexp end t))
4711 (and org-agenda-todo-ignore-scheduled
4712 (re-search-forward org-scheduled-time-regexp end t)
4713 (cond
4714 ((eq org-agenda-todo-ignore-scheduled 'future)
4715 (> (org-days-to-time (match-string 1)) 0))
4716 ((eq org-agenda-todo-ignore-scheduled 'past)
4717 (<= (org-days-to-time (match-string 1)) 0))
4718 ((numberp org-agenda-todo-ignore-scheduled)
4719 (org-agenda-todo-custom-ignore-p
4720 (match-string 1) org-agenda-todo-ignore-scheduled))
4721 (t)))
4722 (and org-agenda-todo-ignore-deadlines
4723 (re-search-forward org-deadline-time-regexp end t)
4724 (cond
4725 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4726 ((eq org-agenda-todo-ignore-deadlines 'far)
4727 (not (org-deadline-close (match-string 1))))
4728 ((eq org-agenda-todo-ignore-deadlines 'future)
4729 (> (org-days-to-time (match-string 1)) 0))
4730 ((eq org-agenda-todo-ignore-deadlines 'past)
4731 (<= (org-days-to-time (match-string 1)) 0))
4732 ((numberp org-agenda-todo-ignore-deadlines)
4733 (org-agenda-todo-custom-ignore-p
4734 (match-string 1) org-agenda-todo-ignore-deadlines))
4735 (t (org-deadline-close (match-string 1)))))
4736 (and org-agenda-todo-ignore-timestamp
4737 (let ((buffer (current-buffer))
4738 (regexp
4739 (concat
4740 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4741 (start (point)))
4742 ;; Copy current buffer into a temporary one
4743 (with-temp-buffer
4744 (insert-buffer-substring buffer start end)
4745 (goto-char (point-min))
4746 ;; Delete SCHEDULED and DEADLINE items
4747 (while (re-search-forward regexp end t)
4748 (delete-region (match-beginning 0) (match-end 0)))
4749 (goto-char (point-min))
4750 ;; No search for timestamp left
4751 (when (re-search-forward org-ts-regexp nil t)
4752 (cond
4753 ((eq org-agenda-todo-ignore-timestamp 'future)
4754 (> (org-days-to-time (match-string 1)) 0))
4755 ((eq org-agenda-todo-ignore-timestamp 'past)
4756 (<= (org-days-to-time (match-string 1)) 0))
4757 ((numberp org-agenda-todo-ignore-timestamp)
4758 (org-agenda-todo-custom-ignore-p
4759 (match-string 1) org-agenda-todo-ignore-timestamp))
4760 (t))))))))))
4762 (defconst org-agenda-no-heading-message
4763 "No heading for this item in buffer or region.")
4765 (defun org-agenda-get-timestamps ()
4766 "Return the date stamp information for agenda display."
4767 (let* ((props (list 'face 'org-agenda-calendar-event
4768 'org-not-done-regexp org-not-done-regexp
4769 'org-todo-regexp org-todo-regexp
4770 'org-complex-heading-regexp org-complex-heading-regexp
4771 'mouse-face 'highlight
4772 'help-echo
4773 (format "mouse-2 or RET jump to org file %s"
4774 (abbreviate-file-name buffer-file-name))))
4775 (d1 (calendar-absolute-from-gregorian date))
4776 (remove-re
4777 (concat
4778 (regexp-quote
4779 (format-time-string
4780 "<%Y-%m-%d"
4781 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4782 ".*?>"))
4783 (regexp
4784 (concat
4785 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4786 (regexp-quote
4787 (substring
4788 (format-time-string
4789 (car org-time-stamp-formats)
4790 (apply 'encode-time ; DATE bound by calendar
4791 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4792 1 11))
4793 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4794 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4795 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4796 donep tmp priority category org-category-pos ee txt timestr tags
4797 b0 b3 e3 head todo-state end-of-match show-all)
4798 (goto-char (point-min))
4799 (while (setq end-of-match (re-search-forward regexp nil t))
4800 (setq b0 (match-beginning 0)
4801 b3 (match-beginning 3) e3 (match-end 3)
4802 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4803 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4804 (member todo-state
4805 org-agenda-repeating-timestamp-show-all)))
4806 (catch :skip
4807 (and (org-at-date-range-p) (throw :skip nil))
4808 (org-agenda-skip)
4809 (if (and (match-end 1)
4810 (not (= d1 (org-time-string-to-absolute
4811 (match-string 1) d1 nil show-all
4812 (current-buffer) b0))))
4813 (throw :skip nil))
4814 (if (and e3
4815 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4816 (throw :skip nil))
4817 (setq tmp (buffer-substring (max (point-min)
4818 (- b0 org-ds-keyword-length))
4820 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4821 inactivep (= (char-after b0) ?\[)
4822 deadlinep (string-match org-deadline-regexp tmp)
4823 scheduledp (string-match org-scheduled-regexp tmp)
4824 closedp (and org-agenda-include-inactive-timestamps
4825 (string-match org-closed-string tmp))
4826 clockp (and org-agenda-include-inactive-timestamps
4827 (or (string-match org-clock-string tmp)
4828 (string-match "]-+\\'" tmp)))
4829 donep (member todo-state org-done-keywords))
4830 (if (or scheduledp deadlinep closedp clockp
4831 (and donep org-agenda-skip-timestamp-if-done))
4832 (throw :skip t))
4833 (if (string-match ">" timestr)
4834 ;; substring should only run to end of time stamp
4835 (setq timestr (substring timestr 0 (match-end 0))))
4836 (setq marker (org-agenda-new-marker b0)
4837 category (org-get-category b0)
4838 org-category-pos (get-text-property b0 'org-category-position))
4839 (save-excursion
4840 (if (not (re-search-backward org-outline-regexp-bol nil t))
4841 (setq txt org-agenda-no-heading-message)
4842 (goto-char (match-beginning 0))
4843 (setq hdmarker (org-agenda-new-marker)
4844 tags (org-get-tags-at))
4845 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4846 (setq head (or (match-string 1) ""))
4847 (setq txt (org-agenda-format-item
4848 (if inactivep org-agenda-inactive-leader nil)
4849 head category tags timestr
4850 remove-re)))
4851 (setq priority (org-get-priority txt))
4852 (org-add-props txt props
4853 'org-marker marker 'org-hd-marker hdmarker)
4854 (org-add-props txt nil 'priority priority
4855 'org-category category 'date date
4856 'org-category-position org-category-pos
4857 'todo-state todo-state
4858 'type "timestamp")
4859 (push txt ee))
4860 (if org-agenda-skip-additional-timestamps-same-entry
4861 (outline-next-heading)
4862 (goto-char end-of-match))))
4863 (nreverse ee)))
4865 (defun org-agenda-get-sexps ()
4866 "Return the sexp information for agenda display."
4867 (require 'diary-lib)
4868 (let* ((props (list 'face 'org-agenda-calendar-sexp
4869 'mouse-face 'highlight
4870 'help-echo
4871 (format "mouse-2 or RET jump to org file %s"
4872 (abbreviate-file-name buffer-file-name))))
4873 (regexp "^&?%%(")
4874 marker category org-category-pos ee txt tags entry
4875 result beg b sexp sexp-entry todo-state)
4876 (goto-char (point-min))
4877 (while (re-search-forward regexp nil t)
4878 (catch :skip
4879 (org-agenda-skip)
4880 (setq beg (match-beginning 0))
4881 (goto-char (1- (match-end 0)))
4882 (setq b (point))
4883 (forward-sexp 1)
4884 (setq sexp (buffer-substring b (point)))
4885 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4886 (org-trim (match-string 1))
4887 ""))
4888 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4889 (when result
4890 (setq marker (org-agenda-new-marker beg)
4891 category (org-get-category beg)
4892 org-category-pos (get-text-property beg 'org-category-position)
4893 todo-state (org-get-todo-state))
4895 (dolist (r (if (stringp result)
4896 (list result)
4897 result)) ;; we expect a list here
4898 (if (string-match "\\S-" r)
4899 (setq txt r)
4900 (setq txt "SEXP entry returned empty string"))
4902 (setq txt (org-agenda-format-item
4903 "" txt category tags 'time))
4904 (org-add-props txt props 'org-marker marker)
4905 (org-add-props txt nil
4906 'org-category category 'date date 'todo-state todo-state
4907 'org-category-position org-category-pos
4908 'type "sexp")
4909 (push txt ee)))))
4910 (nreverse ee)))
4912 ;; Calendar sanity: define some functions that are independent of
4913 ;; `calendar-date-style'.
4914 ;; Normally I would like to use ISO format when calling the diary functions,
4915 ;; but to make sure we still have Emacs 22 compatibility we bind
4916 ;; also `european-calendar-style' and use european format
4917 (defun org-anniversary (year month day &optional mark)
4918 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4919 (org-no-warnings
4920 (let ((calendar-date-style 'european) (european-calendar-style t))
4921 (diary-anniversary day month year mark))))
4922 (defun org-cyclic (N year month day &optional mark)
4923 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4924 (org-no-warnings
4925 (let ((calendar-date-style 'european) (european-calendar-style t))
4926 (diary-cyclic N day month year mark))))
4927 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4928 "Like `diary-block', but with fixed (ISO) order of arguments."
4929 (org-no-warnings
4930 (let ((calendar-date-style 'european) (european-calendar-style t))
4931 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4932 (defun org-date (year month day &optional mark)
4933 "Like `diary-date', but with fixed (ISO) order of arguments."
4934 (org-no-warnings
4935 (let ((calendar-date-style 'european) (european-calendar-style t))
4936 (diary-date day month year mark))))
4937 (defalias 'org-float 'diary-float)
4939 ;; Define the` org-class' function
4940 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4941 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4942 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
4943 SKIP-WEEKS is any number of ISO weeks in the block period for which the
4944 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
4945 `holidays', then any date that is known by the Emacs calendar to be a
4946 holiday will also be skipped."
4947 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4948 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4949 (d (calendar-absolute-from-gregorian date)))
4950 (and
4951 (<= date1 d)
4952 (<= d date2)
4953 (= (calendar-day-of-week date) dayname)
4954 (or (not skip-weeks)
4955 (progn
4956 (require 'cal-iso)
4957 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4958 (not (and (memq 'holidays skip-weeks)
4959 (calendar-check-holidays date)))
4960 entry)))
4962 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4963 "Like `org-class', but honor `calendar-date-style'.
4964 The order of the first 2 times 3 arguments depends on the variable
4965 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4966 So for American calendars, give this as MONTH DAY YEAR, for European as
4967 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4968 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4969 is any number of ISO weeks in the block period for which the item should
4970 be skipped.
4972 This function is here only for backward compatibility and it is deprecated,
4973 please use `org-class' instead."
4974 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4975 (date2 (org-order-calendar-date-args m2 d2 y2)))
4976 (org-class
4977 (nth 2 date1) (car date1) (nth 1 date1)
4978 (nth 2 date2) (car date2) (nth 1 date2)
4979 dayname skip-weeks)))
4980 (make-obsolete 'org-diary-class 'org-class "")
4982 (defalias 'org-get-closed 'org-agenda-get-progress)
4983 (defun org-agenda-get-progress ()
4984 "Return the logged TODO entries for agenda display."
4985 (let* ((props (list 'mouse-face 'highlight
4986 'org-not-done-regexp org-not-done-regexp
4987 'org-todo-regexp org-todo-regexp
4988 'org-complex-heading-regexp org-complex-heading-regexp
4989 'help-echo
4990 (format "mouse-2 or RET jump to org file %s"
4991 (abbreviate-file-name buffer-file-name))))
4992 (items (if (consp org-agenda-show-log)
4993 org-agenda-show-log
4994 (if (eq org-agenda-show-log 'clockcheck)
4995 '(clock)
4996 org-agenda-log-mode-items)))
4997 (parts
4998 (delq nil
4999 (list
5000 (if (memq 'closed items) (concat "\\<" org-closed-string))
5001 (if (memq 'clock items) (concat "\\<" org-clock-string))
5002 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5003 (parts-re (if parts (mapconcat 'identity parts "\\|")
5004 (error "`org-agenda-log-mode-items' is empty")))
5005 (regexp (concat
5006 "\\(" parts-re "\\)"
5007 " *\\["
5008 (regexp-quote
5009 (substring
5010 (format-time-string
5011 (car org-time-stamp-formats)
5012 (apply 'encode-time ; DATE bound by calendar
5013 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5014 1 11))))
5015 (org-agenda-search-headline-for-time nil)
5016 marker hdmarker priority category org-category-pos tags closedp
5017 statep clockp state ee txt extra timestr rest clocked)
5018 (goto-char (point-min))
5019 (while (re-search-forward regexp nil t)
5020 (catch :skip
5021 (org-agenda-skip)
5022 (setq marker (org-agenda-new-marker (match-beginning 0))
5023 closedp (equal (match-string 1) org-closed-string)
5024 statep (equal (string-to-char (match-string 1)) ?-)
5025 clockp (not (or closedp statep))
5026 state (and statep (match-string 2))
5027 category (org-get-category (match-beginning 0))
5028 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5029 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5030 (when (string-match "\\]" timestr)
5031 ;; substring should only run to end of time stamp
5032 (setq rest (substring timestr (match-end 0))
5033 timestr (substring timestr 0 (match-end 0)))
5034 (if (and (not closedp) (not statep)
5035 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5036 rest))
5037 (progn (setq timestr (concat (substring timestr 0 -1)
5038 "-" (match-string 1 rest) "]"))
5039 (setq clocked (match-string 2 rest)))
5040 (setq clocked "-")))
5041 (save-excursion
5042 (setq extra
5043 (cond
5044 ((not org-agenda-log-mode-add-notes) nil)
5045 (statep
5046 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5047 (match-string 1)))
5048 (clockp
5049 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5050 (match-string 1)))))
5051 (if (not (re-search-backward org-outline-regexp-bol nil t))
5052 (setq txt org-agenda-no-heading-message)
5053 (goto-char (match-beginning 0))
5054 (setq hdmarker (org-agenda-new-marker)
5055 tags (org-get-tags-at))
5056 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5057 (setq txt (match-string 1))
5058 (when extra
5059 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5060 (setq txt (concat (substring txt 0 (match-beginning 1))
5061 " - " extra " " (match-string 2 txt)))
5062 (setq txt (concat txt " - " extra))))
5063 (setq txt (org-agenda-format-item
5064 (cond
5065 (closedp "Closed: ")
5066 (statep (concat "State: (" state ")"))
5067 (t (concat "Clocked: (" clocked ")")))
5068 txt category tags timestr)))
5069 (setq priority 100000)
5070 (org-add-props txt props
5071 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5072 'priority priority 'org-category category
5073 'org-category-position org-category-pos
5074 'type "closed" 'date date
5075 'undone-face 'org-warning 'done-face 'org-agenda-done)
5076 (push txt ee))
5077 (goto-char (point-at-eol))))
5078 (nreverse ee)))
5080 (defun org-agenda-show-clocking-issues ()
5081 "Add overlays, showing issues with clocking.
5082 See also the user option `org-agenda-clock-consistency-checks'."
5083 (interactive)
5084 (let* ((pl org-agenda-clock-consistency-checks)
5085 (re (concat "^[ \t]*"
5086 org-clock-string
5087 "[ \t]+"
5088 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5089 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5090 (tlstart 0.)
5091 (tlend 0.)
5092 (maxtime (org-hh:mm-string-to-minutes
5093 (or (plist-get pl :max-duration) "24:00")))
5094 (mintime (org-hh:mm-string-to-minutes
5095 (or (plist-get pl :min-duration) 0)))
5096 (maxgap (org-hh:mm-string-to-minutes
5097 ;; default 30:00 means never complain
5098 (or (plist-get pl :max-gap) "30:00")))
5099 (gapok (mapcar 'org-hh:mm-string-to-minutes
5100 (plist-get pl :gap-ok-around)))
5101 (def-face (or (plist-get pl :default-face)
5102 '((:background "DarkRed") (:foreground "white"))))
5103 issue face m te ts dt ov)
5104 (goto-char (point-min))
5105 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5106 (setq issue nil face def-face)
5107 (catch 'next
5108 (setq m (org-get-at-bol 'org-marker)
5109 te nil ts nil)
5110 (unless (and m (markerp m))
5111 (setq issue "No valid clock line") (throw 'next t))
5112 (org-with-point-at m
5113 (save-excursion
5114 (goto-char (point-at-bol))
5115 (unless (looking-at re)
5116 (error "No valid Clock line")
5117 (throw 'next t))
5118 (unless (match-end 3)
5119 (setq issue "No end time"
5120 face (or (plist-get pl :no-end-time-face) face))
5121 (throw 'next t))
5122 (setq ts (match-string 1)
5123 te (match-string 3)
5124 ts (org-float-time
5125 (apply 'encode-time (org-parse-time-string ts)))
5126 te (org-float-time
5127 (apply 'encode-time (org-parse-time-string te)))
5128 dt (- te ts))))
5129 (cond
5130 ((> dt (* 60 maxtime))
5131 ;; a very long clocking chunk
5132 (setq issue (format "Clocking interval is very long: %s"
5133 (org-minutes-to-hh:mm-string
5134 (floor (/ (float dt) 60.))))
5135 face (or (plist-get pl :long-face) face)))
5136 ((< dt (* 60 mintime))
5137 ;; a very short clocking chunk
5138 (setq issue (format "Clocking interval is very short: %s"
5139 (org-minutes-to-hh:mm-string
5140 (floor (/ (float dt) 60.))))
5141 face (or (plist-get pl :short-face) face)))
5142 ((and (> tlend 0) (< ts tlend))
5143 ;; Two clock entries are overlapping
5144 (setq issue (format "Clocking overlap: %d minutes"
5145 (/ (- tlend ts) 60))
5146 face (or (plist-get pl :overlap-face) face)))
5147 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5148 ;; There is a gap, lets see if we need to report it
5149 (unless (org-agenda-check-clock-gap tlend ts gapok)
5150 (setq issue (format "Clocking gap: %d minutes"
5151 (/ (- ts tlend) 60))
5152 face (or (plist-get pl :gap-face) face))))
5153 (t nil)))
5154 (setq tlend (or te tlend) tlstart (or ts tlstart))
5155 (when issue
5156 ;; OK, there was some issue, add an overlay to show the issue
5157 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5158 (overlay-put ov 'before-string
5159 (concat
5160 (org-add-props
5161 (format "%-43s" (concat " " issue))
5163 'face face)
5164 "\n"))
5165 (overlay-put ov 'evaporate t)))))
5167 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5168 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5169 (catch 'exit
5170 (unless ok-list
5171 ;; there are no OK times for gaps...
5172 (throw 'exit nil))
5173 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5174 ;; This is more than 24 hours, so it is OK.
5175 ;; because we have at least one OK time, that must be in the
5176 ;; 24 hour interval.
5177 (throw 'exit t))
5178 ;; We have a shorter gap.
5179 ;; Now we have to get the minute of the day when these times are
5180 (let* ((t1dec (decode-time (seconds-to-time t1)))
5181 (t2dec (decode-time (seconds-to-time t2)))
5182 ;; compute the minute on the day
5183 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5184 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5185 (when (< min2 min1)
5186 ;; if min2 is smaller than min1, this means it is on the next day.
5187 ;; Wrap it to after midnight.
5188 (setq min2 (+ min2 1440)))
5189 ;; Now check if any of the OK times is in the gap
5190 (mapc (lambda (x)
5191 ;; Wrap the time to after midnight if necessary
5192 (if (< x min1) (setq x (+ x 1440)))
5193 ;; Check if in interval
5194 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5195 ok-list)
5196 ;; Nope, this gap is not OK
5197 nil)))
5199 (defun org-agenda-get-deadlines ()
5200 "Return the deadline information for agenda display."
5201 (let* ((props (list 'mouse-face 'highlight
5202 'org-not-done-regexp org-not-done-regexp
5203 'org-todo-regexp org-todo-regexp
5204 'org-complex-heading-regexp org-complex-heading-regexp
5205 'help-echo
5206 (format "mouse-2 or RET jump to org file %s"
5207 (abbreviate-file-name buffer-file-name))))
5208 (regexp org-deadline-time-regexp)
5209 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5210 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5211 d2 diff dfrac wdays pos pos1 category org-category-pos
5212 tags suppress-prewarning ee txt head face s todo-state
5213 show-all upcomingp donep timestr)
5214 (goto-char (point-min))
5215 (while (re-search-forward regexp nil t)
5216 (setq suppress-prewarning nil)
5217 (catch :skip
5218 (org-agenda-skip)
5219 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5220 (save-match-data
5221 (string-match org-scheduled-time-regexp
5222 (buffer-substring (point-at-bol)
5223 (point-at-eol)))))
5224 (setq suppress-prewarning
5225 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5226 org-agenda-skip-deadline-prewarning-if-scheduled
5227 0)))
5228 (setq s (match-string 1)
5229 txt nil
5230 pos (1- (match-beginning 1))
5231 todo-state (save-match-data (org-get-todo-state))
5232 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5233 (member todo-state
5234 org-agenda-repeating-timestamp-show-all))
5235 d2 (org-time-string-to-absolute
5236 (match-string 1) d1 'past show-all
5237 (current-buffer) pos)
5238 diff (- d2 d1)
5239 wdays (if suppress-prewarning
5240 (let ((org-deadline-warning-days suppress-prewarning))
5241 (org-get-wdays s))
5242 (org-get-wdays s))
5243 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5244 upcomingp (and todayp (> diff 0)))
5245 ;; When to show a deadline in the calendar:
5246 ;; If the expiration is within wdays warning time.
5247 ;; Past-due deadlines are only shown on the current date
5248 (if (and (or (and (<= diff wdays)
5249 (and todayp (not org-agenda-only-exact-dates)))
5250 (= diff 0)))
5251 (save-excursion
5252 ;; (setq todo-state (org-get-todo-state))
5253 (setq donep (member todo-state org-done-keywords))
5254 (if (and donep
5255 (or org-agenda-skip-deadline-if-done
5256 (not (= diff 0))))
5257 (setq txt nil)
5258 (setq category (org-get-category)
5259 org-category-pos (get-text-property (point) 'org-category-position))
5260 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5261 (setq txt org-agenda-no-heading-message)
5262 (goto-char (match-end 0))
5263 (setq pos1 (match-beginning 0))
5264 (setq tags (org-get-tags-at pos1))
5265 (setq head (buffer-substring-no-properties
5266 (point)
5267 (progn (skip-chars-forward "^\r\n")
5268 (point))))
5269 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5270 (setq timestr
5271 (concat (substring s (match-beginning 1)) " "))
5272 (setq timestr 'time))
5273 (setq txt (org-agenda-format-item
5274 (if (= diff 0)
5275 (car org-agenda-deadline-leaders)
5276 (if (functionp
5277 (nth 1 org-agenda-deadline-leaders))
5278 (funcall
5279 (nth 1 org-agenda-deadline-leaders)
5280 diff date)
5281 (format (nth 1 org-agenda-deadline-leaders)
5282 diff)))
5283 head category tags
5284 (if (not (= diff 0)) nil timestr)))))
5285 (when txt
5286 (setq face (org-agenda-deadline-face dfrac))
5287 (org-add-props txt props
5288 'org-marker (org-agenda-new-marker pos)
5289 'org-hd-marker (org-agenda-new-marker pos1)
5290 'priority (+ (- diff)
5291 (org-get-priority txt))
5292 'org-category category
5293 'org-category-position org-category-pos
5294 'todo-state todo-state
5295 'type (if upcomingp "upcoming-deadline" "deadline")
5296 'date (if upcomingp date d2)
5297 'face (if donep 'org-agenda-done face)
5298 'undone-face face 'done-face 'org-agenda-done)
5299 (push txt ee))))))
5300 (nreverse ee)))
5302 (defun org-agenda-deadline-face (fraction)
5303 "Return the face to displaying a deadline item.
5304 FRACTION is what fraction of the head-warning time has passed."
5305 (let ((faces org-agenda-deadline-faces) f)
5306 (catch 'exit
5307 (while (setq f (pop faces))
5308 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5310 (defun org-agenda-get-scheduled (&optional deadline-results)
5311 "Return the scheduled information for agenda display."
5312 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5313 'org-todo-regexp org-todo-regexp
5314 'org-complex-heading-regexp org-complex-heading-regexp
5315 'done-face 'org-agenda-done
5316 'mouse-face 'highlight
5317 'help-echo
5318 (format "mouse-2 or RET jump to org file %s"
5319 (abbreviate-file-name buffer-file-name))))
5320 (regexp org-scheduled-time-regexp)
5321 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5322 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5324 (deadline-position-alist
5325 (mapcar (lambda (a) (and (setq mm (get-text-property
5326 0 'org-hd-marker a))
5327 (cons (marker-position mm) a)))
5328 deadline-results))
5329 d2 diff pos pos1 category org-category-pos tags donep
5330 ee txt head pastschedp todo-state face timestr s habitp show-all)
5331 (goto-char (point-min))
5332 (while (re-search-forward regexp nil t)
5333 (catch :skip
5334 (org-agenda-skip)
5335 (setq s (match-string 1)
5336 txt nil
5337 pos (1- (match-beginning 1))
5338 todo-state (save-match-data (org-get-todo-state))
5339 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5340 (member todo-state
5341 org-agenda-repeating-timestamp-show-all))
5342 d2 (org-time-string-to-absolute
5343 (match-string 1) d1 'past show-all
5344 (current-buffer) pos)
5345 diff (- d2 d1))
5346 (setq pastschedp (and todayp (< diff 0)))
5347 ;; When to show a scheduled item in the calendar:
5348 ;; If it is on or past the date.
5349 (when (or (and (< diff 0)
5350 (< (abs diff) org-scheduled-past-days)
5351 (and todayp (not org-agenda-only-exact-dates)))
5352 (= diff 0))
5353 (save-excursion
5354 (setq donep (member todo-state org-done-keywords))
5355 (if (and donep
5356 (or org-agenda-skip-scheduled-if-done
5357 (not (= diff 0))
5358 (and (functionp 'org-is-habit-p)
5359 (org-is-habit-p))))
5360 (setq txt nil)
5361 (setq habitp (and (functionp 'org-is-habit-p)
5362 (org-is-habit-p)))
5363 (setq category (org-get-category)
5364 org-category-pos (get-text-property (point) 'org-category-position))
5365 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5366 (setq txt org-agenda-no-heading-message)
5367 (goto-char (match-end 0))
5368 (setq pos1 (match-beginning 0))
5369 (if habitp
5370 (if (or (not org-habit-show-habits)
5371 (and (not todayp)
5372 org-habit-show-habits-only-for-today))
5373 (throw :skip nil))
5374 (if (and
5375 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5376 (and org-agenda-skip-scheduled-if-deadline-is-shown
5377 pastschedp))
5378 (setq mm (assoc pos1 deadline-position-alist)))
5379 (throw :skip nil)))
5380 (setq tags (org-get-tags-at))
5381 (setq head (buffer-substring-no-properties
5382 (point)
5383 (progn (skip-chars-forward "^\r\n") (point))))
5384 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5385 (setq timestr
5386 (concat (substring s (match-beginning 1)) " "))
5387 (setq timestr 'time))
5388 (setq txt (org-agenda-format-item
5389 (if (= diff 0)
5390 (car org-agenda-scheduled-leaders)
5391 (format (nth 1 org-agenda-scheduled-leaders)
5392 (- 1 diff)))
5393 head category tags
5394 (if (not (= diff 0)) nil timestr)
5395 nil habitp))))
5396 (when txt
5397 (setq face
5398 (cond
5399 ((and (not habitp) pastschedp)
5400 'org-scheduled-previously)
5401 (todayp 'org-scheduled-today)
5402 (t 'org-scheduled))
5403 habitp (and habitp (org-habit-parse-todo)))
5404 (org-add-props txt props
5405 'undone-face face
5406 'face (if donep 'org-agenda-done face)
5407 'org-marker (org-agenda-new-marker pos)
5408 'org-hd-marker (org-agenda-new-marker pos1)
5409 'type (if pastschedp "past-scheduled" "scheduled")
5410 'date (if pastschedp d2 date)
5411 'priority (if habitp
5412 (org-habit-get-priority habitp)
5413 (+ 94 (- 5 diff) (org-get-priority txt)))
5414 'org-category category
5415 'org-category-position org-category-pos
5416 'org-habit-p habitp
5417 'todo-state todo-state)
5418 (push txt ee))))))
5419 (nreverse ee)))
5421 (defun org-agenda-get-blocks ()
5422 "Return the date-range information for agenda display."
5423 (let* ((props (list 'face nil
5424 'org-not-done-regexp org-not-done-regexp
5425 'org-todo-regexp org-todo-regexp
5426 'org-complex-heading-regexp org-complex-heading-regexp
5427 'mouse-face 'highlight
5428 'help-echo
5429 (format "mouse-2 or RET jump to org file %s"
5430 (abbreviate-file-name buffer-file-name))))
5431 (regexp org-tr-regexp)
5432 (d0 (calendar-absolute-from-gregorian date))
5433 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5434 todo-state tags pos head donep)
5435 (goto-char (point-min))
5436 (while (re-search-forward regexp nil t)
5437 (catch :skip
5438 (org-agenda-skip)
5439 (setq pos (point))
5440 (let ((start-time (match-string 1))
5441 (end-time (match-string 2)))
5442 (setq s1 (match-string 1)
5443 s2 (match-string 2)
5444 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5445 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5446 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5447 ;; Only allow days between the limits, because the normal
5448 ;; date stamps will catch the limits.
5449 (save-excursion
5450 (setq todo-state (org-get-todo-state))
5451 (setq donep (member todo-state org-done-keywords))
5452 (if (and donep org-agenda-skip-timestamp-if-done)
5453 (throw :skip t))
5454 (setq marker (org-agenda-new-marker (point)))
5455 (setq category (org-get-category)
5456 org-category-pos (get-text-property (point) 'org-category-position))
5457 (if (not (re-search-backward org-outline-regexp-bol nil t))
5458 (setq txt org-agenda-no-heading-message)
5459 (goto-char (match-beginning 0))
5460 (setq hdmarker (org-agenda-new-marker (point)))
5461 (setq tags (org-get-tags-at))
5462 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5463 (setq head (match-string 1))
5464 (let ((remove-re
5465 (if org-agenda-remove-timeranges-from-blocks
5466 (concat
5467 "<" (regexp-quote s1) ".*?>"
5468 "--"
5469 "<" (regexp-quote s2) ".*?>")
5470 nil)))
5471 (setq txt (org-agenda-format-item
5472 (format
5473 (nth (if (= d1 d2) 0 1)
5474 org-agenda-timerange-leaders)
5475 (1+ (- d0 d1)) (1+ (- d2 d1)))
5476 head category tags
5477 (cond ((and (= d1 d0) (= d2 d0))
5478 (concat "<" start-time ">--<" end-time ">"))
5479 ((= d1 d0)
5480 (concat "<" start-time ">"))
5481 ((= d2 d0)
5482 (concat "<" end-time ">"))
5483 (t nil))
5484 remove-re))))
5485 (org-add-props txt props
5486 'org-marker marker 'org-hd-marker hdmarker
5487 'type "block" 'date date
5488 'todo-state todo-state
5489 'priority (org-get-priority txt) 'org-category category
5490 'org-category-position org-category-pos)
5491 (push txt ee))))
5492 (goto-char pos)))
5493 ;; Sort the entries by expiration date.
5494 (nreverse ee)))
5496 ;;; Agenda presentation and sorting
5498 (defvar org-prefix-has-time nil
5499 "A flag, set by `org-compile-prefix-format'.
5500 The flag is set if the currently compiled format contains a `%t'.")
5501 (defvar org-prefix-has-tag nil
5502 "A flag, set by `org-compile-prefix-format'.
5503 The flag is set if the currently compiled format contains a `%T'.")
5504 (defvar org-prefix-has-effort nil
5505 "A flag, set by `org-compile-prefix-format'.
5506 The flag is set if the currently compiled format contains a `%e'.")
5507 (defvar org-prefix-category-length nil
5508 "Used by `org-compile-prefix-format' to remember the category field width.")
5509 (defvar org-prefix-category-max-length nil
5510 "Used by `org-compile-prefix-format' to remember the category field width.")
5512 (defun org-agenda-get-category-icon (category)
5513 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5514 (dolist (entry org-agenda-category-icon-alist)
5515 (when (org-string-match-p (car entry) category)
5516 (if (listp (cadr entry))
5517 (return (cadr entry))
5518 (return (apply 'create-image (cdr entry)))))))
5520 (defun org-agenda-format-item (extra txt &optional category tags dotime
5521 remove-re habitp)
5522 "Format TXT to be inserted into the agenda buffer.
5523 In particular, it adds the prefix and corresponding text properties. EXTRA
5524 must be a string and replaces the `%s' specifier in the prefix format.
5525 CATEGORY (string, symbol or nil) may be used to overrule the default
5526 category taken from local variable or file name. It will replace the `%c'
5527 specifier in the format. DOTIME, when non-nil, indicates that a
5528 time-of-day should be extracted from TXT for sorting of this entry, and for
5529 the `%t' specifier in the format. When DOTIME is a string, this string is
5530 searched for a time before TXT is. TAGS can be the tags of the headline.
5531 Any match of REMOVE-RE will be removed from TXT."
5532 (save-match-data
5533 ;; Diary entries sometimes have extra whitespace at the beginning
5534 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5536 ;; Fix the tags part in txt
5537 (setq txt (org-agenda-fix-displayed-tags
5538 txt tags
5539 org-agenda-show-inherited-tags
5540 org-agenda-hide-tags-regexp))
5541 (let* ((category (or category
5542 (if (stringp org-category)
5543 org-category
5544 (and org-category (symbol-name org-category)))
5545 (if buffer-file-name
5546 (file-name-sans-extension
5547 (file-name-nondirectory buffer-file-name))
5548 "")))
5549 (category-icon (org-agenda-get-category-icon category))
5550 (category-icon (if category-icon
5551 (propertize " " 'display category-icon)
5552 ""))
5553 ;; time, tag, effort are needed for the eval of the prefix format
5554 (tag (if tags (nth (1- (length tags)) tags) ""))
5555 time effort neffort
5556 (ts (if dotime (concat
5557 (if (stringp dotime) dotime "")
5558 (and org-agenda-search-headline-for-time txt))))
5559 (time-of-day (and dotime (org-get-time-of-day ts)))
5560 stamp plain s0 s1 s2 rtn srp l
5561 duration thecategory)
5562 (and (eq major-mode 'org-mode) buffer-file-name
5563 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5564 (when (and dotime time-of-day)
5565 ;; Extract starting and ending time and move them to prefix
5566 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5567 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5568 (setq s0 (match-string 0 ts)
5569 srp (and stamp (match-end 3))
5570 s1 (match-string (if plain 1 2) ts)
5571 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5573 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5574 ;; them, we might want to remove them there to avoid duplication.
5575 ;; The user can turn this off with a variable.
5576 (if (and org-prefix-has-time
5577 org-agenda-remove-times-when-in-prefix (or stamp plain)
5578 (string-match (concat (regexp-quote s0) " *") txt)
5579 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5580 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5581 (= (match-beginning 0) 0)
5583 (setq txt (replace-match "" nil nil txt))))
5584 ;; Normalize the time(s) to 24 hour
5585 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5586 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5588 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5589 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5590 (setq s2
5591 (org-minutes-to-hh:mm-string
5592 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5594 ;; Compute the duration
5595 (when s2
5596 (setq duration (- (org-hh:mm-string-to-minutes s2)
5597 (org-hh:mm-string-to-minutes s1)))))
5599 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5600 txt)
5601 ;; Tags are in the string
5602 (if (or (eq org-agenda-remove-tags t)
5603 (and org-agenda-remove-tags
5604 org-prefix-has-tag))
5605 (setq txt (replace-match "" t t txt))
5606 (setq txt (replace-match
5607 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5608 (match-string 2 txt))
5609 t t txt))))
5610 (when (eq major-mode 'org-mode)
5611 (setq effort
5612 (condition-case nil
5613 (org-get-effort
5614 (or (get-text-property 0 'org-hd-marker txt)
5615 (get-text-property 0 'org-marker txt)))
5616 (error nil)))
5617 (when effort
5618 (setq neffort (org-duration-string-to-minutes effort)
5619 effort (setq effort (concat "[" effort "]")))))
5620 ;; prevent erroring out with %e format when there is no effort
5621 (or effort (setq effort ""))
5623 (when remove-re
5624 (while (string-match remove-re txt)
5625 (setq txt (replace-match "" t t txt))))
5627 ;; Set org-heading property on `txt' to mark the start of the
5628 ;; heading.
5629 (add-text-properties 0 (length txt) '(org-heading t) txt)
5631 ;; Prepare the variables needed in the eval of the compiled format
5632 (setq time (cond (s2 (concat
5633 (org-agenda-time-of-day-to-ampm-maybe s1)
5634 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5635 (if org-agenda-timegrid-use-ampm " ")))
5636 (s1 (concat
5637 (org-agenda-time-of-day-to-ampm-maybe s1)
5638 (if org-agenda-timegrid-use-ampm
5639 "........ "
5640 "......")))
5641 (t ""))
5642 extra (or (and (not habitp) extra) "")
5643 category (if (symbolp category) (symbol-name category) category)
5644 thecategory (copy-sequence category))
5645 (if (string-match org-bracket-link-regexp category)
5646 (progn
5647 (setq l (if (match-end 3)
5648 (- (match-end 3) (match-beginning 3))
5649 (- (match-end 1) (match-beginning 1))))
5650 (when (< l (or org-prefix-category-length 0))
5651 (setq category (copy-sequence category))
5652 (org-add-props category nil
5653 'extra-space (make-string
5654 (- org-prefix-category-length l 1) ?\ ))))
5655 (if (and org-prefix-category-max-length
5656 (>= (length category) org-prefix-category-max-length))
5657 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5658 ;; Evaluate the compiled format
5659 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5661 ;; And finally add the text properties
5662 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5663 (org-add-props rtn nil
5664 'org-category (if thecategory (downcase thecategory) category)
5665 'tags (mapcar 'org-downcase-keep-props tags)
5666 'org-highest-priority org-highest-priority
5667 'org-lowest-priority org-lowest-priority
5668 'time-of-day time-of-day
5669 'duration duration
5670 'effort effort
5671 'effort-minutes neffort
5672 'txt txt
5673 'time time
5674 'extra extra
5675 'format org-prefix-format-compiled
5676 'dotime dotime))))
5678 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5679 "Remove tags string from TXT, and add a modified list of tags.
5680 The modified list may contain inherited tags, and tags matched by
5681 `org-agenda-hide-tags-regexp' will be removed."
5682 (when (or add-inherited hide-re)
5683 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5684 (setq txt (substring txt 0 (match-beginning 0))))
5685 (setq tags
5686 (delq nil
5687 (mapcar (lambda (tg)
5688 (if (or (and hide-re (string-match hide-re tg))
5689 (and (not add-inherited)
5690 (get-text-property 0 'inherited tg)))
5692 tg))
5693 tags)))
5694 (when tags
5695 (let ((have-i (get-text-property 0 'inherited (car tags)))
5697 (setq txt (concat txt " :"
5698 (mapconcat
5699 (lambda (x)
5700 (setq i (get-text-property 0 'inherited x))
5701 (if (and have-i (not i))
5702 (progn
5703 (setq have-i nil)
5704 (concat ":" x))
5706 tags ":")
5707 (if have-i "::" ":"))))))
5708 txt)
5710 (defun org-downcase-keep-props (s)
5711 (let ((props (text-properties-at 0 s)))
5712 (setq s (downcase s))
5713 (add-text-properties 0 (length s) props s)
5716 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5717 (defvar org-agenda-sorting-strategy-selected nil)
5719 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5720 (catch 'exit
5721 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5722 ((and todayp (member 'today (car org-agenda-time-grid))))
5723 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5724 ((member 'weekly (car org-agenda-time-grid)))
5725 (t (throw 'exit list)))
5726 (let* ((have (delq nil (mapcar
5727 (lambda (x) (get-text-property 1 'time-of-day x))
5728 list)))
5729 (string (nth 1 org-agenda-time-grid))
5730 (gridtimes (nth 2 org-agenda-time-grid))
5731 (req (car org-agenda-time-grid))
5732 (remove (member 'remove-match req))
5733 new time)
5734 (if (and (member 'require-timed req) (not have))
5735 ;; don't show empty grid
5736 (throw 'exit list))
5737 (while (setq time (pop gridtimes))
5738 (unless (and remove (member time have))
5739 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5740 (push (org-agenda-format-item
5741 nil string "" nil
5742 (concat (substring time 0 -2) ":" (substring time -2)))
5743 new)
5744 (put-text-property
5745 2 (length (car new)) 'face 'org-time-grid (car new))))
5746 (when (and todayp org-agenda-show-current-time-in-grid)
5747 (push (org-agenda-format-item
5749 org-agenda-current-time-string
5750 "" nil
5751 (format-time-string "%H:%M "))
5752 new)
5753 (put-text-property
5754 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5756 (if (member 'time-up org-agenda-sorting-strategy-selected)
5757 (append new list)
5758 (append list new)))))
5760 (defun org-compile-prefix-format (key)
5761 "Compile the prefix format into a Lisp form that can be evaluated.
5762 The resulting form is returned and stored in the variable
5763 `org-prefix-format-compiled'."
5764 (setq org-prefix-has-time nil org-prefix-has-tag nil
5765 org-prefix-category-length nil
5766 org-prefix-has-effort nil)
5767 (let ((s (cond
5768 ((stringp org-agenda-prefix-format)
5769 org-agenda-prefix-format)
5770 ((assq key org-agenda-prefix-format)
5771 (cdr (assq key org-agenda-prefix-format)))
5772 (t " %-12:c%?-12t% s")))
5773 (start 0)
5774 varform vars var e c f opt)
5775 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5776 s start)
5777 (setq var (or (cdr (assoc (match-string 4 s)
5778 '(("c" . category) ("t" . time) ("s" . extra)
5779 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5780 'eval)
5781 c (or (match-string 3 s) "")
5782 opt (match-beginning 1)
5783 start (1+ (match-beginning 0)))
5784 (if (equal var 'time) (setq org-prefix-has-time t))
5785 (if (equal var 'tag) (setq org-prefix-has-tag t))
5786 (if (equal var 'effort) (setq org-prefix-has-effort t))
5787 (setq f (concat "%" (match-string 2 s) "s"))
5788 (when (equal var 'category)
5789 (setq org-prefix-category-length
5790 (floor (abs (string-to-number (match-string 2 s)))))
5791 (setq org-prefix-category-max-length
5792 (let ((x (match-string 2 s)))
5793 (save-match-data
5794 (if (string-match "\\.[0-9]+" x)
5795 (string-to-number (substring (match-string 0 x) 1)))))))
5796 (if (eq var 'eval)
5797 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5798 (if opt
5799 (setq varform
5800 `(if (equal "" ,var)
5802 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5803 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5804 (setq s (replace-match "%s" t nil s))
5805 (push varform vars))
5806 (setq vars (nreverse vars))
5807 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5809 (defun org-set-sorting-strategy (key)
5810 (if (symbolp (car org-agenda-sorting-strategy))
5811 ;; the old format
5812 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5813 (setq org-agenda-sorting-strategy-selected
5814 (or (cdr (assq key org-agenda-sorting-strategy))
5815 (cdr (assq 'agenda org-agenda-sorting-strategy))
5816 '(time-up category-keep priority-down)))))
5818 (defun org-get-time-of-day (s &optional string mod24)
5819 "Check string S for a time of day.
5820 If found, return it as a military time number between 0 and 2400.
5821 If not found, return nil.
5822 The optional STRING argument forces conversion into a 5 character wide string
5823 HH:MM."
5824 (save-match-data
5825 (when
5826 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5827 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5828 (let* ((h (string-to-number (match-string 1 s)))
5829 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5830 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5831 (am-p (equal ampm "am"))
5832 (h1 (cond ((not ampm) h)
5833 ((= h 12) (if am-p 0 12))
5834 (t (+ h (if am-p 0 12)))))
5835 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5836 (mod h1 24) h1))
5837 (t0 (+ (* 100 h2) m))
5838 (t1 (concat (if (>= h1 24) "+" " ")
5839 (if (and org-agenda-time-leading-zero
5840 (< t0 1000)) "0" "")
5841 (if (< t0 100) "0" "")
5842 (if (< t0 10) "0" "")
5843 (int-to-string t0))))
5844 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5846 (defvar org-agenda-before-sorting-filter-function nil
5847 "Function to be applied to agenda items prior to sorting.
5848 Prior to sorting also means just before they are inserted into the agenda.
5850 To aid sorting, you may revisit the original entries and add more text
5851 properties which will later be used by the sorting functions.
5853 The function should take a string argument, an agenda line.
5854 It has access to the text properties in that line, which contain among
5855 other things, the property `org-hd-marker' that points to the entry
5856 where the line comes from. Note that not all lines going into the agenda
5857 have this property, only most.
5859 The function should return the modified string. It is probably best
5860 to ONLY change text properties.
5862 You can also use this function as a filter, by returning nil for lines
5863 you don't want to have in the agenda at all. For this application, you
5864 could bind the variable in the options section of a custom command.")
5866 (defun org-finalize-agenda-entries (list &optional nosort)
5867 "Sort and concatenate the agenda items."
5868 (setq list (mapcar 'org-agenda-highlight-todo list))
5869 (if nosort
5870 list
5871 (when org-agenda-before-sorting-filter-function
5872 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5873 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5875 (defun org-agenda-highlight-todo (x)
5876 (let ((org-done-keywords org-done-keywords-for-agenda)
5877 (case-fold-search nil)
5879 (if (eq x 'line)
5880 (save-excursion
5881 (beginning-of-line 1)
5882 (setq re (org-get-at-bol 'org-todo-regexp))
5883 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5884 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5885 (add-text-properties (match-beginning 0) (match-end 1)
5886 (list 'face (org-get-todo-face 1)))
5887 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5888 (delete-region (match-beginning 1) (1- (match-end 0)))
5889 (goto-char (match-beginning 1))
5890 (insert (format org-agenda-todo-keyword-format s)))))
5891 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5892 (setq re (get-text-property 0 'org-todo-regexp x))
5893 (when (and re
5894 ;; Test `pl' because if there's no heading content,
5895 ;; there's no point matching to highlight. Note
5896 ;; that if we didn't test `pl' first, and there
5897 ;; happened to be no keyword from `org-todo-regexp'
5898 ;; on this heading line, then the `equal' comparison
5899 ;; afterwards would spuriously succeed in the case
5900 ;; where `pl' is nil -- causing an args-out-of-range
5901 ;; error when we try to add text properties to text
5902 ;; that isn't there.
5904 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5905 x pl) pl))
5906 (add-text-properties
5907 (or (match-end 1) (match-end 0)) (match-end 0)
5908 (list 'face (org-get-todo-face (match-string 2 x)))
5910 (when (match-end 1)
5911 (setq x (concat (substring x 0 (match-end 1))
5912 (format org-agenda-todo-keyword-format
5913 (match-string 2 x))
5914 (org-add-props " " (text-properties-at 0 x))
5915 (substring x (match-end 3)))))))
5916 x)))
5918 (defsubst org-cmp-priority (a b)
5919 "Compare the priorities of string A and B."
5920 (let ((pa (or (get-text-property 1 'priority a) 0))
5921 (pb (or (get-text-property 1 'priority b) 0)))
5922 (cond ((> pa pb) +1)
5923 ((< pa pb) -1)
5924 (t nil))))
5926 (defsubst org-cmp-effort (a b)
5927 "Compare the effort values of string A and B."
5928 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5929 (ea (or (get-text-property 1 'effort-minutes a) def))
5930 (eb (or (get-text-property 1 'effort-minutes b) def)))
5931 (cond ((> ea eb) +1)
5932 ((< ea eb) -1)
5933 (t nil))))
5935 (defsubst org-cmp-category (a b)
5936 "Compare the string values of categories of strings A and B."
5937 (let ((ca (or (get-text-property 1 'org-category a) ""))
5938 (cb (or (get-text-property 1 'org-category b) "")))
5939 (cond ((string-lessp ca cb) -1)
5940 ((string-lessp cb ca) +1)
5941 (t nil))))
5943 (defsubst org-cmp-todo-state (a b)
5944 "Compare the todo states of strings A and B."
5945 (let* ((ma (or (get-text-property 1 'org-marker a)
5946 (get-text-property 1 'org-hd-marker a)))
5947 (mb (or (get-text-property 1 'org-marker b)
5948 (get-text-property 1 'org-hd-marker b)))
5949 (fa (and ma (marker-buffer ma)))
5950 (fb (and mb (marker-buffer mb)))
5951 (todo-kwds
5952 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5953 (and fb (with-current-buffer fb org-todo-keywords-1))))
5954 (ta (or (get-text-property 1 'todo-state a) ""))
5955 (tb (or (get-text-property 1 'todo-state b) ""))
5956 (la (- (length (member ta todo-kwds))))
5957 (lb (- (length (member tb todo-kwds))))
5958 (donepa (member ta org-done-keywords-for-agenda))
5959 (donepb (member tb org-done-keywords-for-agenda)))
5960 (cond ((and donepa (not donepb)) -1)
5961 ((and (not donepa) donepb) +1)
5962 ((< la lb) -1)
5963 ((< lb la) +1)
5964 (t nil))))
5966 (defsubst org-cmp-alpha (a b)
5967 "Compare the headlines, alphabetically."
5968 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5969 (plb (text-property-any 0 (length b) 'org-heading t b))
5970 (ta (and pla (substring a pla)))
5971 (tb (and plb (substring b plb))))
5972 (when pla
5973 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5974 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5975 (setq ta (substring ta (match-end 0))))
5976 (setq ta (downcase ta)))
5977 (when plb
5978 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5979 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5980 (setq tb (substring tb (match-end 0))))
5981 (setq tb (downcase tb)))
5982 (cond ((not ta) +1)
5983 ((not tb) -1)
5984 ((string-lessp ta tb) -1)
5985 ((string-lessp tb ta) +1)
5986 (t nil))))
5988 (defsubst org-cmp-tag (a b)
5989 "Compare the string values of the first tags of A and B."
5990 (let ((ta (car (last (get-text-property 1 'tags a))))
5991 (tb (car (last (get-text-property 1 'tags b)))))
5992 (cond ((not ta) +1)
5993 ((not tb) -1)
5994 ((string-lessp ta tb) -1)
5995 ((string-lessp tb ta) +1)
5996 (t nil))))
5998 (defsubst org-cmp-time (a b)
5999 "Compare the time-of-day values of strings A and B."
6000 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6001 (ta (or (get-text-property 1 'time-of-day a) def))
6002 (tb (or (get-text-property 1 'time-of-day b) def)))
6003 (cond ((< ta tb) -1)
6004 ((< tb ta) +1)
6005 (t nil))))
6007 (defsubst org-cmp-habit-p (a b)
6008 "Compare the todo states of strings A and B."
6009 (let ((ha (get-text-property 1 'org-habit-p a))
6010 (hb (get-text-property 1 'org-habit-p b)))
6011 (cond ((and ha (not hb)) -1)
6012 ((and (not ha) hb) +1)
6013 (t nil))))
6015 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6017 (defun org-entries-lessp (a b)
6018 "Predicate for sorting agenda entries."
6019 ;; The following variables will be used when the form is evaluated.
6020 ;; So even though the compiler complains, keep them.
6021 (let* ((ss org-agenda-sorting-strategy-selected)
6022 (time-up (and (org-em 'time-up 'time-down ss)
6023 (org-cmp-time a b)))
6024 (time-down (if time-up (- time-up) nil))
6025 (priority-up (and (org-em 'priority-up 'priority-down ss)
6026 (org-cmp-priority a b)))
6027 (priority-down (if priority-up (- priority-up) nil))
6028 (effort-up (and (org-em 'effort-up 'effort-down ss)
6029 (org-cmp-effort a b)))
6030 (effort-down (if effort-up (- effort-up) nil))
6031 (category-up (and (or (org-em 'category-up 'category-down ss)
6032 (memq 'category-keep ss))
6033 (org-cmp-category a b)))
6034 (category-down (if category-up (- category-up) nil))
6035 (category-keep (if category-up +1 nil))
6036 (tag-up (and (org-em 'tag-up 'tag-down ss)
6037 (org-cmp-tag a b)))
6038 (tag-down (if tag-up (- tag-up) nil))
6039 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6040 (org-cmp-todo-state a b)))
6041 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6042 (habit-up (and (org-em 'habit-up 'habit-down ss)
6043 (org-cmp-habit-p a b)))
6044 (habit-down (if habit-up (- habit-up) nil))
6045 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6046 (org-cmp-alpha a b)))
6047 (alpha-down (if alpha-up (- alpha-up) nil))
6048 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6049 user-defined-up user-defined-down)
6050 (if (and need-user-cmp org-agenda-cmp-user-defined
6051 (functionp org-agenda-cmp-user-defined))
6052 (setq user-defined-up
6053 (funcall org-agenda-cmp-user-defined a b)
6054 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6055 (cdr (assoc
6056 (eval (cons 'or org-agenda-sorting-strategy-selected))
6057 '((-1 . t) (1 . nil) (nil . nil))))))
6059 ;;; Agenda restriction lock
6061 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6062 "Overlay to mark the headline to which agenda commands are restricted.")
6063 (overlay-put org-agenda-restriction-lock-overlay
6064 'face 'org-agenda-restriction-lock)
6065 (overlay-put org-agenda-restriction-lock-overlay
6066 'help-echo "Agendas are currently limited to this subtree.")
6067 (org-detach-overlay org-agenda-restriction-lock-overlay)
6069 (defun org-agenda-set-restriction-lock (&optional type)
6070 "Set restriction lock for agenda, to current subtree or file.
6071 Restriction will be the file if TYPE is `file', or if type is the
6072 universal prefix '(4), or if the cursor is before the first headline
6073 in the file. Otherwise, restriction will be to the current subtree."
6074 (interactive "P")
6075 (and (equal type '(4)) (setq type 'file))
6076 (setq type (cond
6077 (type type)
6078 ((org-at-heading-p) 'subtree)
6079 ((condition-case nil (org-back-to-heading t) (error nil))
6080 'subtree)
6081 (t 'file)))
6082 (if (eq type 'subtree)
6083 (progn
6084 (setq org-agenda-restrict t)
6085 (setq org-agenda-overriding-restriction 'subtree)
6086 (put 'org-agenda-files 'org-restrict
6087 (list (buffer-file-name (buffer-base-buffer))))
6088 (org-back-to-heading t)
6089 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6090 (move-marker org-agenda-restrict-begin (point))
6091 (move-marker org-agenda-restrict-end
6092 (save-excursion (org-end-of-subtree t)))
6093 (message "Locking agenda restriction to subtree"))
6094 (put 'org-agenda-files 'org-restrict
6095 (list (buffer-file-name (buffer-base-buffer))))
6096 (setq org-agenda-restrict nil)
6097 (setq org-agenda-overriding-restriction 'file)
6098 (move-marker org-agenda-restrict-begin nil)
6099 (move-marker org-agenda-restrict-end nil)
6100 (message "Locking agenda restriction to file"))
6101 (setq current-prefix-arg nil)
6102 (org-agenda-maybe-redo))
6104 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6105 "Remove the agenda restriction lock."
6106 (interactive "P")
6107 (org-detach-overlay org-agenda-restriction-lock-overlay)
6108 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6109 (setq org-agenda-overriding-restriction nil)
6110 (setq org-agenda-restrict nil)
6111 (put 'org-agenda-files 'org-restrict nil)
6112 (move-marker org-agenda-restrict-begin nil)
6113 (move-marker org-agenda-restrict-end nil)
6114 (setq current-prefix-arg nil)
6115 (message "Agenda restriction lock removed")
6116 (or noupdate (org-agenda-maybe-redo)))
6118 (defun org-agenda-maybe-redo ()
6119 "If there is any window showing the agenda view, update it."
6120 (let ((w (get-buffer-window org-agenda-buffer-name t))
6121 (w0 (selected-window)))
6122 (when w
6123 (select-window w)
6124 (org-agenda-redo)
6125 (select-window w0)
6126 (if org-agenda-overriding-restriction
6127 (message "Agenda view shifted to new %s restriction"
6128 org-agenda-overriding-restriction)
6129 (message "Agenda restriction lock removed")))))
6131 ;;; Agenda commands
6133 (defun org-agenda-check-type (error &rest types)
6134 "Check if agenda buffer is of allowed type.
6135 If ERROR is non-nil, throw an error, otherwise just return nil."
6136 (if (memq org-agenda-type types)
6138 (if error
6139 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6140 nil)))
6142 (defun org-agenda-quit ()
6143 "Exit agenda by removing the window or the buffer."
6144 (interactive)
6145 (if org-agenda-columns-active
6146 (org-columns-quit)
6147 (let ((buf (current-buffer)))
6148 (if (eq org-agenda-window-setup 'other-frame)
6149 (progn
6150 (kill-buffer buf)
6151 (org-agenda-reset-markers)
6152 (org-columns-remove-overlays)
6153 (setq org-agenda-archives-mode nil)
6154 (delete-frame))
6155 (and (not (eq org-agenda-window-setup 'current-window))
6156 (not (one-window-p))
6157 (delete-window))
6158 (kill-buffer buf)
6159 (org-agenda-reset-markers)
6160 (org-columns-remove-overlays)
6161 (setq org-agenda-archives-mode nil)))
6162 ;; Maybe restore the pre-agenda window configuration.
6163 (and org-agenda-restore-windows-after-quit
6164 (not (eq org-agenda-window-setup 'other-frame))
6165 org-pre-agenda-window-conf
6166 (set-window-configuration org-pre-agenda-window-conf))))
6168 (defun org-agenda-exit ()
6169 "Exit agenda by removing the window or the buffer.
6170 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6171 Org-mode buffers visited directly by the user will not be touched."
6172 (interactive)
6173 (org-release-buffers org-agenda-new-buffers)
6174 (setq org-agenda-new-buffers nil)
6175 (org-agenda-quit))
6177 (defun org-agenda-execute (arg)
6178 "Execute another agenda command, keeping same window.
6179 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6180 in the agenda."
6181 (interactive "P")
6182 (let ((org-agenda-window-setup 'current-window))
6183 (org-agenda arg)))
6185 (defun org-agenda-redo ()
6186 "Rebuild Agenda.
6187 When this is the global TODO list, a prefix argument will be interpreted."
6188 (interactive)
6189 (let* ((org-agenda-keep-modes t)
6190 (tag-filter org-agenda-tag-filter)
6191 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6192 (cat-filter org-agenda-category-filter)
6193 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6194 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6195 (cols org-agenda-columns-active)
6196 (line (org-current-line))
6197 (window-line (- line (org-current-line (window-start))))
6198 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6199 (put 'org-agenda-tag-filter :preset-filter nil)
6200 (put 'org-agenda-category-filter :preset-filter nil)
6201 (and cols (org-columns-quit))
6202 (message "Rebuilding agenda buffer...")
6203 (org-let lprops '(eval org-agenda-redo-command))
6204 (setq org-agenda-undo-list nil
6205 org-agenda-pending-undo-list nil)
6206 (message "Rebuilding agenda buffer...done")
6207 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6208 (put 'org-agenda-category-filter :preset-filter cat-preset)
6209 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6210 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6211 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6212 (org-goto-line line)
6213 (recenter window-line)))
6215 (defvar org-global-tags-completion-table nil)
6216 (defvar org-agenda-filter-form nil)
6217 (defvar org-agenda-filtered-by-category nil)
6219 (defun org-agenda-filter-by-category (strip)
6220 "Keep only those lines in the agenda buffer that have a specific category.
6221 The category is that of the current line."
6222 (interactive "P")
6223 (if org-agenda-filtered-by-category
6224 (org-agenda-filter-show-all-cat)
6225 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6226 (if cat (org-agenda-filter-apply
6227 (list (concat (if strip "-" "+") cat)) 'category)
6228 (error "No category at point")))))
6230 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6231 "Keep only those lines in the agenda buffer that have a specific tag.
6232 The tag is selected with its fast selection letter, as configured.
6233 With prefix argument STRIP, remove all lines that do have the tag.
6234 A lisp caller can specify CHAR. NARROW means that the new tag should be
6235 used to narrow the search - the interactive user can also press `-' or `+'
6236 to switch to narrowing."
6237 (interactive "P")
6238 (let* ((alist org-tag-alist-for-agenda)
6239 (tag-chars (mapconcat
6240 (lambda (x) (if (and (not (symbolp (car x)))
6241 (cdr x))
6242 (char-to-string (cdr x))
6243 ""))
6244 alist ""))
6245 (efforts (org-split-string
6246 (or (cdr (assoc (concat org-effort-property "_ALL")
6247 org-global-properties))
6248 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6249 "")))
6250 (effort-op org-agenda-filter-effort-default-operator)
6251 (effort-prompt "")
6252 (inhibit-read-only t)
6253 (current org-agenda-tag-filter)
6254 maybe-refresh a n tag)
6255 (unless char
6256 (message
6257 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6258 (if narrow "Narrow" "Filter") tag-chars
6259 (if org-agenda-auto-exclude-function "[RET], " ""))
6260 (setq char (read-char-exclusive)))
6261 (when (member char '(?+ ?-))
6262 ;; Narrowing down
6263 (cond ((equal char ?-) (setq strip t narrow t))
6264 ((equal char ?+) (setq strip nil narrow t)))
6265 (message
6266 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6267 (setq char (read-char-exclusive)))
6268 (when (member char '(?< ?> ?= ??))
6269 ;; An effort operator
6270 (setq effort-op (char-to-string char))
6271 (setq alist nil) ; to make sure it will be interpreted as effort.
6272 (unless (equal char ??)
6273 (loop for i from 0 to 9 do
6274 (setq effort-prompt
6275 (concat
6276 effort-prompt " ["
6277 (if (= i 9) "0" (int-to-string (1+ i)))
6278 "]" (nth i efforts))))
6279 (message "Effort%s: %s " effort-op effort-prompt)
6280 (setq char (read-char-exclusive))
6281 (when (or (< char ?0) (> char ?9))
6282 (error "Need 1-9,0 to select effort" ))))
6283 (when (equal char ?\t)
6284 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6285 (org-set-local 'org-global-tags-completion-table
6286 (org-global-tags-completion-table)))
6287 (let ((completion-ignore-case t))
6288 (setq tag (org-icompleting-read
6289 "Tag: " org-global-tags-completion-table))))
6290 (cond
6291 ((equal char ?\r)
6292 (org-agenda-filter-show-all-tag)
6293 (when org-agenda-auto-exclude-function
6294 (setq org-agenda-tag-filter '())
6295 (dolist (tag (org-agenda-get-represented-tags))
6296 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6297 (if modifier
6298 (push modifier org-agenda-tag-filter))))
6299 (if (not (null org-agenda-tag-filter))
6300 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6301 (setq maybe-refresh t))
6302 ((equal char ?/)
6303 (org-agenda-filter-show-all-tag)
6304 (when (get 'org-agenda-tag-filter :preset-filter)
6305 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6306 (setq maybe-refresh t))
6307 ((equal char ?. )
6308 (setq org-agenda-tag-filter
6309 (mapcar (lambda(tag) (concat "+" tag))
6310 (org-get-at-bol 'tags)))
6311 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6312 (setq maybe-refresh t))
6313 ((or (equal char ?\ )
6314 (setq a (rassoc char alist))
6315 (and (>= char ?0) (<= char ?9)
6316 (setq n (if (= char ?0) 9 (- char ?0 1))
6317 tag (concat effort-op (nth n efforts))
6318 a (cons tag nil)))
6319 (and (= char ??)
6320 (setq tag "?eff")
6321 a (cons tag nil))
6322 (and tag (setq a (cons tag nil))))
6323 (org-agenda-filter-show-all-tag)
6324 (setq tag (car a))
6325 (setq org-agenda-tag-filter
6326 (cons (concat (if strip "-" "+") tag)
6327 (if narrow current nil)))
6328 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6329 (setq maybe-refresh t))
6330 (t (error "Invalid tag selection character %c" char)))
6331 (when (and maybe-refresh
6332 (eq org-agenda-clockreport-mode 'with-filter))
6333 (org-agenda-redo))))
6335 (defun org-agenda-get-represented-tags ()
6336 "Get a list of all tags currently represented in the agenda."
6337 (let (p tags)
6338 (save-excursion
6339 (goto-char (point-min))
6340 (while (setq p (next-single-property-change (point) 'tags))
6341 (goto-char p)
6342 (mapc (lambda (x) (add-to-list 'tags x))
6343 (get-text-property (point) 'tags))))
6344 tags))
6346 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6347 "Refine the current filter. See `org-agenda-filter-by-tag'."
6348 (interactive "P")
6349 (org-agenda-filter-by-tag strip char 'refine))
6351 (defun org-agenda-filter-make-matcher ()
6352 "Create the form that tests a line for agenda filter."
6353 (let (f f1)
6354 ;; first compute the tag-filter matcher
6355 (dolist (x (delete-dups
6356 (append (get 'org-agenda-tag-filter
6357 :preset-filter) org-agenda-tag-filter)))
6358 (if (member x '("-" "+"))
6359 (setq f1 (if (equal x "-") 'tags '(not tags)))
6360 (if (string-match "[<=>?]" x)
6361 (setq f1 (org-agenda-filter-effort-form x))
6362 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6363 (if (equal (string-to-char x) ?-)
6364 (setq f1 (list 'not f1))))
6365 (push f1 f))
6366 ;; then compute the category-filter matcher
6367 (dolist (x (delete-dups
6368 (append (get 'org-agenda-category-filter
6369 :preset-filter) org-agenda-category-filter)))
6370 (setq f1 (list 'equal (substring x 1) 'cat))
6371 (push f1 f))
6372 (cons 'and (nreverse f))))
6374 (defun org-agenda-filter-effort-form (e)
6375 "Return the form to compare the effort of the current line with what E says.
6376 E looks like \"+<2:25\"."
6377 (let (op)
6378 (setq e (substring e 1))
6379 (setq op (string-to-char e) e (substring e 1))
6380 (setq op (cond ((equal op ?<) '<=)
6381 ((equal op ?>) '>=)
6382 ((equal op ??) op)
6383 (t '=)))
6384 (list 'org-agenda-compare-effort (list 'quote op)
6385 (org-duration-string-to-minutes e))))
6387 (defun org-agenda-compare-effort (op value)
6388 "Compare the effort of the current line with VALUE, using OP.
6389 If the line does not have an effort defined, return nil."
6390 (let ((eff (org-get-at-bol 'effort-minutes)))
6391 (if (equal op ??)
6392 (not eff)
6393 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6394 value))))
6396 (defun org-agenda-filter-apply (filter type)
6397 "Set FILTER as the new agenda filter and apply it."
6398 (let (tags cat)
6399 (if (eq type 'tag)
6400 (setq org-agenda-tag-filter filter)
6401 (setq org-agenda-category-filter filter
6402 org-agenda-filtered-by-category t))
6403 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6404 (org-agenda-set-mode-name)
6405 (save-excursion
6406 (goto-char (point-min))
6407 (while (not (eobp))
6408 (if (org-get-at-bol 'org-marker)
6409 (progn
6410 (setq tags (org-get-at-bol 'tags) ; used in eval
6411 cat (get-text-property (point) 'org-category))
6412 (if (not (eval org-agenda-filter-form))
6413 (org-agenda-filter-hide-line type))
6414 (beginning-of-line 2))
6415 (beginning-of-line 2))))
6416 (if (get-char-property (point) 'invisible)
6417 (org-agenda-previous-line))))
6419 (defun org-agenda-filter-hide-line (type)
6420 (let (ov)
6421 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6422 (point-at-eol)))
6423 (overlay-put ov 'invisible t)
6424 (overlay-put ov 'type type)
6425 (if (eq type 'tag)
6426 (push ov org-agenda-tag-filter-overlays)
6427 (push ov org-agenda-cat-filter-overlays))))
6429 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6430 (setq pos (or pos (point)))
6431 (save-excursion
6432 (dolist (ov (overlays-at pos))
6433 (when (and (overlay-get ov 'invisible)
6434 (eq (overlay-get ov 'type) 'tag))
6435 (goto-char pos)
6436 (if (< (overlay-start ov) (point-at-eol))
6437 (move-overlay ov (point-at-eol)
6438 (overlay-end ov)))))))
6440 (defun org-agenda-filter-show-all-tag nil
6441 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6442 (setq org-agenda-tag-filter-overlays nil
6443 org-agenda-tag-filter nil
6444 org-agenda-filter-form nil)
6445 (org-agenda-set-mode-name))
6447 (defun org-agenda-filter-show-all-cat nil
6448 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6449 (setq org-agenda-cat-filter-overlays nil
6450 org-agenda-filtered-by-category nil
6451 org-agenda-category-filter nil
6452 org-agenda-filter-form nil)
6453 (org-agenda-set-mode-name))
6455 (defun org-agenda-manipulate-query-add ()
6456 "Manipulate the query by adding a search term with positive selection.
6457 Positive selection means the term must be matched for selection of an entry."
6458 (interactive)
6459 (org-agenda-manipulate-query ?\[))
6460 (defun org-agenda-manipulate-query-subtract ()
6461 "Manipulate the query by adding a search term with negative selection.
6462 Negative selection means term must not be matched for selection of an entry."
6463 (interactive)
6464 (org-agenda-manipulate-query ?\]))
6465 (defun org-agenda-manipulate-query-add-re ()
6466 "Manipulate the query by adding a search regexp with positive selection.
6467 Positive selection means the regexp must match for selection of an entry."
6468 (interactive)
6469 (org-agenda-manipulate-query ?\{))
6470 (defun org-agenda-manipulate-query-subtract-re ()
6471 "Manipulate the query by adding a search regexp with negative selection.
6472 Negative selection means regexp must not match for selection of an entry."
6473 (interactive)
6474 (org-agenda-manipulate-query ?\}))
6475 (defun org-agenda-manipulate-query (char)
6476 (cond
6477 ((memq org-agenda-type '(timeline agenda))
6478 (let ((org-agenda-include-inactive-timestamps t))
6479 (org-agenda-redo))
6480 (message "Display now includes inactive timestamps as well"))
6481 ((eq org-agenda-type 'search)
6482 (org-add-to-string
6483 'org-agenda-query-string
6484 (if org-agenda-last-search-view-search-was-boolean
6485 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6486 (?\{ . " +{}") (?\} . " -{}"))))
6487 " "))
6488 (setq org-agenda-redo-command
6489 (list 'org-search-view
6490 org-todo-only
6491 org-agenda-query-string
6492 (+ (length org-agenda-query-string)
6493 (if (member char '(?\{ ?\})) 0 1))))
6494 (set-register org-agenda-query-register org-agenda-query-string)
6495 (org-agenda-redo))
6496 (t (error "Cannot manipulate query for %s-type agenda buffers"
6497 org-agenda-type))))
6499 (defun org-add-to-string (var string)
6500 (set var (concat (symbol-value var) string)))
6502 (defun org-agenda-goto-date (date)
6503 "Jump to DATE in agenda."
6504 (interactive (list (let ((org-read-date-prefer-future
6505 (eval org-agenda-jump-prefer-future)))
6506 (org-read-date))))
6507 (org-agenda-list nil date))
6509 (defun org-agenda-goto-today ()
6510 "Go to today."
6511 (interactive)
6512 (org-agenda-check-type t 'timeline 'agenda)
6513 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6514 (cond
6515 (tdpos (goto-char tdpos))
6516 ((eq org-agenda-type 'agenda)
6517 (let* ((sd (org-agenda-compute-starting-span
6518 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6519 (org-agenda-overriding-arguments org-agenda-last-arguments))
6520 (setf (nth 1 org-agenda-overriding-arguments) sd)
6521 (org-agenda-redo)
6522 (org-agenda-find-same-or-today-or-agenda)))
6523 (t (error "Cannot find today")))))
6525 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6526 (goto-char
6527 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6528 (text-property-any (point-min) (point-max) 'org-today t)
6529 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6530 (point-min))))
6532 (defun org-agenda-later (arg)
6533 "Go forward in time by thee current span.
6534 With prefix ARG, go forward that many times the current span."
6535 (interactive "p")
6536 (org-agenda-check-type t 'agenda)
6537 (let* ((span org-agenda-current-span)
6538 (sd org-starting-day)
6539 (greg (calendar-gregorian-from-absolute sd))
6540 (cnt (org-get-at-bol 'org-day-cnt))
6541 greg2)
6542 (cond
6543 ((eq span 'day)
6544 (setq sd (+ arg sd)))
6545 ((eq span 'week)
6546 (setq sd (+ (* 7 arg) sd)))
6547 ((eq span 'month)
6548 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6549 sd (calendar-absolute-from-gregorian greg2))
6550 (setcar greg2 (1+ (car greg2))))
6551 ((eq span 'year)
6552 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6553 sd (calendar-absolute-from-gregorian greg2))
6554 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6556 (setq sd (+ (* span arg) sd))))
6557 (let ((org-agenda-overriding-arguments
6558 (list (car org-agenda-last-arguments) sd span t)))
6559 (org-agenda-redo)
6560 (org-agenda-find-same-or-today-or-agenda cnt))))
6562 (defun org-agenda-earlier (arg)
6563 "Go backward in time by the current span.
6564 With prefix ARG, go backward that many times the current span."
6565 (interactive "p")
6566 (org-agenda-later (- arg)))
6568 (defun org-agenda-view-mode-dispatch ()
6569 "Call one of the view mode commands."
6570 (interactive)
6571 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6572 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6573 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6574 [E]ntryText")
6575 (let ((a (read-char-exclusive)))
6576 (case a
6577 (?\ (call-interactively 'org-agenda-reset-view))
6578 (?d (call-interactively 'org-agenda-day-view))
6579 (?w (call-interactively 'org-agenda-week-view))
6580 (?m (call-interactively 'org-agenda-month-view))
6581 (?y (call-interactively 'org-agenda-year-view))
6582 (?l (call-interactively 'org-agenda-log-mode))
6583 (?L (org-agenda-log-mode '(4)))
6584 (?c (org-agenda-log-mode 'clockcheck))
6585 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6586 (?a (call-interactively 'org-agenda-archives-mode))
6587 (?A (org-agenda-archives-mode 'files))
6588 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6589 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6590 (?G (call-interactively 'org-agenda-toggle-time-grid))
6591 (?D (call-interactively 'org-agenda-toggle-diary))
6592 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6593 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6594 (org-agenda-check-type t 'timeline 'agenda)
6595 (org-agenda-redo))
6596 (message "Display now includes inactive timestamps as well"))
6597 (?q (message "Abort"))
6598 (otherwise (error "Invalid key" )))))
6600 (defun org-agenda-reset-view ()
6601 "Switch to default view for agenda."
6602 (interactive)
6603 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6604 (defun org-agenda-day-view (&optional day-of-year)
6605 "Switch to daily view for agenda.
6606 With argument DAY-OF-YEAR, switch to that day of the year."
6607 (interactive "P")
6608 (org-agenda-change-time-span 'day day-of-year))
6609 (defun org-agenda-week-view (&optional iso-week)
6610 "Switch to daily view for agenda.
6611 With argument ISO-WEEK, switch to the corresponding ISO week.
6612 If ISO-WEEK has more then 2 digits, only the last two encode the
6613 week. Any digits before this encode a year. So 200712 means
6614 week 12 of year 2007. Years in the range 1938-2037 can also be
6615 written as 2-digit years."
6616 (interactive "P")
6617 (org-agenda-change-time-span 'week iso-week))
6618 (defun org-agenda-month-view (&optional month)
6619 "Switch to monthly view for agenda.
6620 With argument MONTH, switch to that month."
6621 (interactive "P")
6622 (org-agenda-change-time-span 'month month))
6623 (defun org-agenda-year-view (&optional year)
6624 "Switch to yearly view for agenda.
6625 With argument YEAR, switch to that year.
6626 If MONTH has more then 2 digits, only the last two encode the
6627 month. Any digits before this encode a year. So 200712 means
6628 December year 2007. Years in the range 1938-2037 can also be
6629 written as 2-digit years."
6630 (interactive "P")
6631 (when year
6632 (setq year (org-small-year-to-year year)))
6633 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6634 (org-agenda-change-time-span 'year year)
6635 (error "Abort")))
6637 (defun org-agenda-change-time-span (span &optional n)
6638 "Change the agenda view to SPAN.
6639 SPAN may be `day', `week', `month', `year'."
6640 (org-agenda-check-type t 'agenda)
6641 (if (and (not n) (equal org-agenda-current-span span))
6642 (error "Viewing span is already \"%s\"" span))
6643 (let* ((sd (or (org-get-at-bol 'day)
6644 org-starting-day))
6645 (sd (org-agenda-compute-starting-span sd span n))
6646 (org-agenda-overriding-arguments
6647 (or org-agenda-overriding-arguments
6648 (list (car org-agenda-last-arguments) sd span t))))
6649 (org-agenda-redo)
6650 (org-agenda-find-same-or-today-or-agenda))
6651 (org-agenda-set-mode-name)
6652 (message "Switched to %s view" span))
6654 (defun org-agenda-compute-starting-span (sd span &optional n)
6655 "Compute starting date for agenda.
6656 SPAN may be `day', `week', `month', `year'. The return value
6657 is a cons cell with the starting date and the number of days,
6658 so that the date SD will be in that range."
6659 (let* ((greg (calendar-gregorian-from-absolute sd))
6660 (dg (nth 1 greg))
6661 (mg (car greg))
6662 (yg (nth 2 greg)))
6663 (cond
6664 ((eq span 'day)
6665 (when n
6666 (setq sd (+ (calendar-absolute-from-gregorian
6667 (list mg 1 yg))
6668 n -1))))
6669 ((eq span 'week)
6670 (let* ((nt (calendar-day-of-week
6671 (calendar-gregorian-from-absolute sd)))
6672 (d (if org-agenda-start-on-weekday
6673 (- nt org-agenda-start-on-weekday)
6676 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6677 (when n
6678 (require 'cal-iso)
6679 (when (> n 99)
6680 (setq y1 (org-small-year-to-year (/ n 100))
6681 n (mod n 100)))
6682 (setq sd
6683 (calendar-absolute-from-iso
6684 (list n 1
6685 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6686 ((eq span 'month)
6687 (let (y1)
6688 (when (and n (> n 99))
6689 (setq y1 (org-small-year-to-year (/ n 100))
6690 n (mod n 100)))
6691 (setq sd (calendar-absolute-from-gregorian
6692 (list (or n mg) 1 (or y1 yg))))))
6693 ((eq span 'year)
6694 (setq sd (calendar-absolute-from-gregorian
6695 (list 1 1 (or n yg))))))
6696 sd))
6698 (defun org-agenda-next-date-line (&optional arg)
6699 "Jump to the next line indicating a date in agenda buffer."
6700 (interactive "p")
6701 (org-agenda-check-type t 'agenda 'timeline)
6702 (beginning-of-line 1)
6703 ;; This does not work if user makes date format that starts with a blank
6704 (if (looking-at "^\\S-") (forward-char 1))
6705 (if (not (re-search-forward "^\\S-" nil t arg))
6706 (progn
6707 (backward-char 1)
6708 (error "No next date after this line in this buffer")))
6709 (goto-char (match-beginning 0)))
6711 (defun org-agenda-previous-date-line (&optional arg)
6712 "Jump to the previous line indicating a date in agenda buffer."
6713 (interactive "p")
6714 (org-agenda-check-type t 'agenda 'timeline)
6715 (beginning-of-line 1)
6716 (if (not (re-search-backward "^\\S-" nil t arg))
6717 (error "No previous date before this line in this buffer")))
6719 ;; Initialize the highlight
6720 (defvar org-hl (make-overlay 1 1))
6721 (overlay-put org-hl 'face 'highlight)
6723 (defun org-highlight (begin end &optional buffer)
6724 "Highlight a region with overlay."
6725 (move-overlay org-hl begin end (or buffer (current-buffer))))
6727 (defun org-unhighlight ()
6728 "Detach overlay INDEX."
6729 (org-detach-overlay org-hl))
6731 ;; FIXME this is currently not used.
6732 (defun org-highlight-until-next-command (beg end &optional buffer)
6733 "Move the highlight overlay to BEG/END, remove it before the next command."
6734 (org-highlight beg end buffer)
6735 (add-hook 'pre-command-hook 'org-unhighlight-once))
6736 (defun org-unhighlight-once ()
6737 "Remove the highlight from its position, and this function from the hook."
6738 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6739 (org-unhighlight))
6741 (defun org-agenda-follow-mode ()
6742 "Toggle follow mode in an agenda buffer."
6743 (interactive)
6744 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6745 (org-agenda-set-mode-name)
6746 (org-agenda-do-context-action)
6747 (message "Follow mode is %s"
6748 (if org-agenda-follow-mode "on" "off")))
6750 (defun org-agenda-entry-text-mode (&optional arg)
6751 "Toggle entry text mode in an agenda buffer."
6752 (interactive "P")
6753 (setq org-agenda-entry-text-mode (or (integerp arg)
6754 (not org-agenda-entry-text-mode)))
6755 (org-agenda-entry-text-hide)
6756 (and org-agenda-entry-text-mode
6757 (let ((org-agenda-entry-text-maxlines
6758 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6759 (org-agenda-entry-text-show)))
6760 (org-agenda-set-mode-name)
6761 (message "Entry text mode is %s. Maximum number of lines is %d"
6762 (if org-agenda-entry-text-mode "on" "off")
6763 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6765 (defun org-agenda-clockreport-mode (&optional with-filter)
6766 "Toggle clocktable mode in an agenda buffer.
6767 With prefix arg WITH-FILTER, make the clocktable respect the current
6768 agenda filter."
6769 (interactive "P")
6770 (org-agenda-check-type t 'agenda)
6771 (if with-filter
6772 (setq org-agenda-clockreport-mode 'with-filter)
6773 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6774 (org-agenda-set-mode-name)
6775 (org-agenda-redo)
6776 (message "Clocktable mode is %s"
6777 (if org-agenda-clockreport-mode "on" "off")))
6779 (defun org-agenda-log-mode (&optional special)
6780 "Toggle log mode in an agenda buffer.
6781 With argument SPECIAL, show all possible log items, not only the ones
6782 configured in `org-agenda-log-mode-items'.
6783 With a double `C-u' prefix arg, show *only* log items, nothing else."
6784 (interactive "P")
6785 (org-agenda-check-type t 'agenda 'timeline)
6786 (setq org-agenda-show-log
6787 (cond
6788 ((equal special '(16)) 'only)
6789 ((eq special 'clockcheck)
6790 (if (eq org-agenda-show-log 'clockcheck)
6791 nil 'clockcheck))
6792 (special '(closed clock state))
6793 (t (not org-agenda-show-log))))
6794 (org-agenda-set-mode-name)
6795 (org-agenda-redo)
6796 (message "Log mode is %s"
6797 (if org-agenda-show-log "on" "off")))
6799 (defun org-agenda-archives-mode (&optional with-files)
6800 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6801 When called with a prefix argument, include all archive files as well."
6802 (interactive "P")
6803 (setq org-agenda-archives-mode
6804 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6805 (org-agenda-set-mode-name)
6806 (org-agenda-redo)
6807 (message
6808 "%s"
6809 (cond
6810 ((eq org-agenda-archives-mode nil)
6811 "No archives are included")
6812 ((eq org-agenda-archives-mode 'trees)
6813 (format "Trees with :%s: tag are included" org-archive-tag))
6814 ((eq org-agenda-archives-mode t)
6815 (format "Trees with :%s: tag and all active archive files are included"
6816 org-archive-tag)))))
6818 (defun org-agenda-toggle-diary ()
6819 "Toggle diary inclusion in an agenda buffer."
6820 (interactive)
6821 (org-agenda-check-type t 'agenda)
6822 (setq org-agenda-include-diary (not org-agenda-include-diary))
6823 (org-agenda-redo)
6824 (org-agenda-set-mode-name)
6825 (message "Diary inclusion turned %s"
6826 (if org-agenda-include-diary "on" "off")))
6828 (defun org-agenda-toggle-deadlines ()
6829 "Toggle inclusion of entries with a deadline in an agenda buffer."
6830 (interactive)
6831 (org-agenda-check-type t 'agenda)
6832 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6833 (org-agenda-redo)
6834 (org-agenda-set-mode-name)
6835 (message "Deadlines inclusion turned %s"
6836 (if org-agenda-include-deadlines "on" "off")))
6838 (defun org-agenda-toggle-time-grid ()
6839 "Toggle time grid in an agenda buffer."
6840 (interactive)
6841 (org-agenda-check-type t 'agenda)
6842 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6843 (org-agenda-redo)
6844 (org-agenda-set-mode-name)
6845 (message "Time-grid turned %s"
6846 (if org-agenda-use-time-grid "on" "off")))
6848 (defun org-agenda-set-mode-name ()
6849 "Set the mode name to indicate all the small mode settings."
6850 (setq mode-name
6851 (list "Org-Agenda"
6852 (if (get 'org-agenda-files 'org-restrict) " []" "")
6854 '(:eval (org-agenda-span-name org-agenda-current-span))
6855 (if org-agenda-follow-mode " Follow" "")
6856 (if org-agenda-entry-text-mode " ETxt" "")
6857 (if org-agenda-include-diary " Diary" "")
6858 (if org-agenda-include-deadlines " Ddl" "")
6859 (if org-agenda-use-time-grid " Grid" "")
6860 (if (and (boundp 'org-habit-show-habits)
6861 org-habit-show-habits) " Habit" "")
6862 (cond
6863 ((consp org-agenda-show-log) " LogAll")
6864 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6865 (org-agenda-show-log " Log")
6866 (t ""))
6867 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
6868 :preset-filter))
6869 '(:eval (org-propertize
6870 (concat " <"
6871 (mapconcat
6872 'identity
6873 (append
6874 (get 'org-agenda-category-filter :preset-filter)
6875 org-agenda-category-filter)
6877 ">")
6878 'face 'org-agenda-filter-category
6879 'help-echo "Category used in filtering"))
6881 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
6882 :preset-filter))
6883 '(:eval (org-propertize
6884 (concat " {"
6885 (mapconcat
6886 'identity
6887 (append
6888 (get 'org-agenda-tag-filter :preset-filter)
6889 org-agenda-tag-filter)
6891 "}")
6892 'face 'org-agenda-filter-tags
6893 'help-echo "Tags used in filtering"))
6895 (if org-agenda-archives-mode
6896 (if (eq org-agenda-archives-mode t)
6897 " Archives"
6898 (format " :%s:" org-archive-tag))
6900 (if org-agenda-clockreport-mode
6901 (if (eq org-agenda-clockreport-mode 'with-filter)
6902 " Clock{}" " Clock")
6903 "")))
6904 (force-mode-line-update))
6906 (defun org-agenda-post-command-hook ()
6907 (setq org-agenda-type
6908 (or (get-text-property (point) 'org-agenda-type)
6909 (get-text-property (max (point-min) (1- (point)))
6910 'org-agenda-type))))
6912 (defun org-agenda-next-line ()
6913 "Move cursor to the next line, and show if follow mode is active."
6914 (interactive)
6915 (call-interactively 'next-line)
6916 (org-agenda-do-context-action))
6918 (defun org-agenda-previous-line ()
6919 "Move cursor to the previous line, and show if follow-mode is active."
6920 (interactive)
6921 (call-interactively 'previous-line)
6922 (org-agenda-do-context-action))
6924 (defun org-agenda-do-context-action ()
6925 "Show outline path and, maybe, follow mode window."
6926 (let ((m (org-get-at-bol 'org-marker)))
6927 (when (and (markerp m) (marker-buffer m))
6928 (and org-agenda-follow-mode
6929 (if org-agenda-follow-indirect
6930 (org-agenda-tree-to-indirect-buffer)
6931 (org-agenda-show)))
6932 (and org-agenda-show-outline-path
6933 (org-with-point-at m (org-display-outline-path t))))))
6935 (defun org-agenda-show-priority ()
6936 "Show the priority of the current item.
6937 This priority is composed of the main priority given with the [#A] cookies,
6938 and by additional input from the age of a schedules or deadline entry."
6939 (interactive)
6940 (let* ((pri (org-get-at-bol 'priority)))
6941 (message "Priority is %d" (if pri pri -1000))))
6943 (defun org-agenda-show-tags ()
6944 "Show the tags applicable to the current item."
6945 (interactive)
6946 (let* ((tags (org-get-at-bol 'tags)))
6947 (if tags
6948 (message "Tags are :%s:"
6949 (org-no-properties (mapconcat 'identity tags ":")))
6950 (message "No tags associated with this line"))))
6952 (defun org-agenda-goto (&optional highlight)
6953 "Go to the Org-mode file which contains the item at point."
6954 (interactive)
6955 (let* ((marker (or (org-get-at-bol 'org-marker)
6956 (org-agenda-error)))
6957 (buffer (marker-buffer marker))
6958 (pos (marker-position marker)))
6959 (switch-to-buffer-other-window buffer)
6960 (widen)
6961 (push-mark)
6962 (goto-char pos)
6963 (when (eq major-mode 'org-mode)
6964 (org-show-context 'agenda)
6965 (save-excursion
6966 (and (outline-next-heading)
6967 (org-flag-heading nil)))) ; show the next heading
6968 (when (outline-invisible-p)
6969 (show-entry)) ; display invisible text
6970 (recenter (/ (window-height) 2))
6971 (run-hooks 'org-agenda-after-show-hook)
6972 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6974 (defvar org-agenda-after-show-hook nil
6975 "Normal hook run after an item has been shown from the agenda.
6976 Point is in the buffer where the item originated.")
6978 (defun org-agenda-kill ()
6979 "Kill the entry or subtree belonging to the current agenda entry."
6980 (interactive)
6981 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6982 (let* ((marker (or (org-get-at-bol 'org-marker)
6983 (org-agenda-error)))
6984 (buffer (marker-buffer marker))
6985 (pos (marker-position marker))
6986 (type (org-get-at-bol 'type))
6987 dbeg dend (n 0) conf)
6988 (org-with-remote-undo buffer
6989 (with-current-buffer buffer
6990 (save-excursion
6991 (goto-char pos)
6992 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
6993 (setq dbeg (progn (org-back-to-heading t) (point))
6994 dend (org-end-of-subtree t t))
6995 (setq dbeg (point-at-bol)
6996 dend (min (point-max) (1+ (point-at-eol)))))
6997 (goto-char dbeg)
6998 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6999 (setq conf (or (eq t org-agenda-confirm-kill)
7000 (and (numberp org-agenda-confirm-kill)
7001 (> n org-agenda-confirm-kill))))
7002 (and conf
7003 (not (y-or-n-p
7004 (format "Delete entry with %d lines in buffer \"%s\"? "
7005 n (buffer-name buffer))))
7006 (error "Abort"))
7007 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7008 (with-current-buffer buffer (delete-region dbeg dend))
7009 (message "Agenda item and source killed"))))
7011 (defvar org-archive-default-command)
7012 (defun org-agenda-archive-default ()
7013 "Archive the entry or subtree belonging to the current agenda entry."
7014 (interactive)
7015 (require 'org-archive)
7016 (org-agenda-archive-with org-archive-default-command))
7018 (defun org-agenda-archive-default-with-confirmation ()
7019 "Archive the entry or subtree belonging to the current agenda entry."
7020 (interactive)
7021 (require 'org-archive)
7022 (org-agenda-archive-with org-archive-default-command 'confirm))
7024 (defun org-agenda-archive ()
7025 "Archive the entry or subtree belonging to the current agenda entry."
7026 (interactive)
7027 (org-agenda-archive-with 'org-archive-subtree))
7029 (defun org-agenda-archive-to-archive-sibling ()
7030 "Move the entry to the archive sibling."
7031 (interactive)
7032 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7034 (defun org-agenda-archive-with (cmd &optional confirm)
7035 "Move the entry to the archive sibling."
7036 (interactive)
7037 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7038 (let* ((marker (or (org-get-at-bol 'org-marker)
7039 (org-agenda-error)))
7040 (buffer (marker-buffer marker))
7041 (pos (marker-position marker)))
7042 (org-with-remote-undo buffer
7043 (with-current-buffer buffer
7044 (if (eq major-mode 'org-mode)
7045 (if (and confirm
7046 (not (y-or-n-p "Archive this subtree or entry? ")))
7047 (error "Abort")
7048 (save-excursion
7049 (goto-char pos)
7050 (org-remove-subtree-entries-from-agenda)
7051 (org-back-to-heading t)
7052 (funcall cmd)))
7053 (error "Archiving works only in Org-mode files"))))))
7055 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7056 "Remove all lines in the agenda that correspond to a given subtree.
7057 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7058 If this information is not given, the function uses the tree at point."
7059 (let ((buf (or buf (current-buffer))) m p)
7060 (save-excursion
7061 (unless (and beg end)
7062 (org-back-to-heading t)
7063 (setq beg (point))
7064 (org-end-of-subtree t)
7065 (setq end (point)))
7066 (set-buffer (get-buffer org-agenda-buffer-name))
7067 (save-excursion
7068 (goto-char (point-max))
7069 (beginning-of-line 1)
7070 (while (not (bobp))
7071 (when (and (setq m (org-get-at-bol 'org-marker))
7072 (equal buf (marker-buffer m))
7073 (setq p (marker-position m))
7074 (>= p beg)
7075 (< p end))
7076 (let ((inhibit-read-only t))
7077 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7078 (beginning-of-line 0))))))
7080 (defun org-agenda-refile (&optional goto rfloc no-update)
7081 "Refile the item at point."
7082 (interactive "P")
7083 (if (equal goto '(16))
7084 (org-refile-goto-last-stored)
7085 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7086 (org-agenda-error)))
7087 (buffer (marker-buffer marker))
7088 (pos (marker-position marker))
7089 (rfloc (or rfloc
7090 (org-refile-get-location
7091 (if goto "Goto" "Refile to") buffer
7092 org-refile-allow-creating-parent-nodes))))
7093 (with-current-buffer buffer
7094 (save-excursion
7095 (save-restriction
7096 (widen)
7097 (goto-char marker)
7098 (org-remove-subtree-entries-from-agenda)
7099 (org-refile goto buffer rfloc)))))
7100 (unless no-update (org-agenda-redo))))
7102 (defun org-agenda-open-link (&optional arg)
7103 "Follow the link in the current line, if any.
7104 This looks for a link in the displayed line in the agenda. It also looks
7105 at the text of the entry itself."
7106 (interactive "P")
7107 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7108 (org-get-at-bol 'org-marker)))
7109 (buffer (and marker (marker-buffer marker)))
7110 (prefix (buffer-substring
7111 (point-at-bol) (point-at-eol))))
7112 (cond
7113 (buffer
7114 (with-current-buffer buffer
7115 (save-excursion
7116 (save-restriction
7117 (widen)
7118 (goto-char marker)
7119 (org-offer-links-in-entry arg prefix)))))
7120 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7121 (save-excursion
7122 (beginning-of-line 1)
7123 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7124 (org-open-link-from-string (match-string 1)))
7125 (t (error "No link to open here")))))
7127 (defun org-agenda-copy-local-variable (var)
7128 "Get a variable from a referenced buffer and install it here."
7129 (let ((m (org-get-at-bol 'org-marker)))
7130 (when (and m (buffer-live-p (marker-buffer m)))
7131 (org-set-local var (with-current-buffer (marker-buffer m)
7132 (symbol-value var))))))
7134 (defun org-agenda-switch-to (&optional delete-other-windows)
7135 "Go to the Org-mode file which contains the item at point."
7136 (interactive)
7137 (if (and org-return-follows-link
7138 (not (org-get-at-bol 'org-marker))
7139 (org-in-regexp org-bracket-link-regexp))
7140 (org-open-link-from-string (match-string 0))
7141 (let* ((marker (or (org-get-at-bol 'org-marker)
7142 (org-agenda-error)))
7143 (buffer (marker-buffer marker))
7144 (pos (marker-position marker)))
7145 (org-pop-to-buffer-same-window buffer)
7146 (and delete-other-windows (delete-other-windows))
7147 (widen)
7148 (goto-char pos)
7149 (when (eq major-mode 'org-mode)
7150 (org-show-context 'agenda)
7151 (save-excursion
7152 (and (outline-next-heading)
7153 (org-flag-heading nil))) ; show the next heading
7154 (when (outline-invisible-p)
7155 (show-entry)))))) ; display invisible text
7157 (defun org-agenda-goto-mouse (ev)
7158 "Go to the Org-mode file which contains the item at the mouse click."
7159 (interactive "e")
7160 (mouse-set-point ev)
7161 (org-agenda-goto))
7163 (defun org-agenda-show (&optional full-entry)
7164 "Display the Org-mode file which contains the item at point.
7165 With prefix argument FULL-ENTRY, make the entire entry visible
7166 if it was hidden in the outline."
7167 (interactive "P")
7168 (let ((win (selected-window)))
7169 (if full-entry
7170 (let ((org-show-entry-below t))
7171 (org-agenda-goto t))
7172 (org-agenda-goto t))
7173 (select-window win)))
7175 (defvar org-agenda-show-window nil)
7176 (defun org-agenda-show-and-scroll-up (&optional arg)
7177 "Display the Org-mode file which contains the item at point.
7178 When called repeatedly, scroll the window that is displaying the buffer.
7179 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7180 `show-subtree' to display the item, so that drawers and logbooks stay
7181 folded."
7182 (interactive "P")
7183 (let ((win (selected-window)))
7184 (if (and (window-live-p org-agenda-show-window)
7185 (eq this-command last-command))
7186 (progn
7187 (select-window org-agenda-show-window)
7188 (ignore-errors (scroll-up)))
7189 (org-agenda-goto t)
7190 (if arg (org-show-entry) (show-subtree))
7191 (setq org-agenda-show-window (selected-window)))
7192 (select-window win)))
7194 (defun org-agenda-show-scroll-down ()
7195 "Scroll down the window showing the agenda."
7196 (interactive)
7197 (let ((win (selected-window)))
7198 (when (window-live-p org-agenda-show-window)
7199 (select-window org-agenda-show-window)
7200 (ignore-errors (scroll-down))
7201 (select-window win))))
7203 (defun org-agenda-show-1 (&optional more)
7204 "Display the Org-mode file which contains the item at point.
7205 The prefix arg selects the amount of information to display:
7207 0 hide the subtree
7208 1 just show the entry according to defaults.
7209 2 show the children view
7210 3 show the subtree view
7211 4 show the entire subtree and any LOGBOOK drawers
7212 5 show the entire subtree and any drawers
7213 With prefix argument FULL-ENTRY, make the entire entry visible
7214 if it was hidden in the outline."
7215 (interactive "p")
7216 (let ((win (selected-window)))
7217 (org-agenda-goto t)
7218 (org-recenter-heading 1)
7219 (cond
7220 ((= more 0)
7221 (hide-subtree)
7222 (save-excursion
7223 (org-back-to-heading)
7224 (run-hook-with-args 'org-cycle-hook 'folded))
7225 (message "Remote: FOLDED"))
7226 ((and (org-called-interactively-p 'any) (= more 1))
7227 (message "Remote: show with default settings"))
7228 ((= more 2)
7229 (show-entry)
7230 (show-children)
7231 (save-excursion
7232 (org-back-to-heading)
7233 (run-hook-with-args 'org-cycle-hook 'children))
7234 (message "Remote: CHILDREN"))
7235 ((= more 3)
7236 (show-subtree)
7237 (save-excursion
7238 (org-back-to-heading)
7239 (run-hook-with-args 'org-cycle-hook 'subtree))
7240 (message "Remote: SUBTREE"))
7241 ((= more 4)
7242 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7243 (org-drawer-regexp
7244 (concat "^[ \t]*:\\("
7245 (mapconcat 'regexp-quote org-drawers "\\|")
7246 "\\):[ \t]*$")))
7247 (show-subtree)
7248 (save-excursion
7249 (org-back-to-heading)
7250 (org-cycle-hide-drawers 'subtree)))
7251 (message "Remote: SUBTREE AND LOGBOOK"))
7252 ((> more 4)
7253 (show-subtree)
7254 (message "Remote: SUBTREE AND ALL DRAWERS")))
7255 (select-window win)))
7257 (defun org-recenter-heading (n)
7258 (save-excursion
7259 (org-back-to-heading)
7260 (recenter n)))
7262 (defvar org-agenda-cycle-counter nil)
7263 (defun org-agenda-cycle-show (&optional n)
7264 "Show the current entry in another window, with default settings.
7265 Default settings are taken from `org-show-hierarchy-above' and siblings.
7266 When use repeatedly in immediate succession, the remote entry will cycle
7267 through visibility
7269 children -> subtree -> folded
7271 When called with a numeric prefix arg, that arg will be passed through to
7272 `org-agenda-show-1'. For the interpretation of that argument, see the
7273 docstring of `org-agenda-show-1'."
7274 (interactive "P")
7275 (if (integerp n)
7276 (setq org-agenda-cycle-counter n)
7277 (if (not (eq last-command this-command))
7278 (setq org-agenda-cycle-counter 1)
7279 (if (equal org-agenda-cycle-counter 0)
7280 (setq org-agenda-cycle-counter 2)
7281 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7282 (if (> org-agenda-cycle-counter 3)
7283 (setq org-agenda-cycle-counter 0)))))
7284 (org-agenda-show-1 org-agenda-cycle-counter))
7286 (defun org-agenda-recenter (arg)
7287 "Display the Org-mode file which contains the item at point and recenter."
7288 (interactive "P")
7289 (let ((win (selected-window)))
7290 (org-agenda-goto t)
7291 (recenter arg)
7292 (select-window win)))
7294 (defun org-agenda-show-mouse (ev)
7295 "Display the Org-mode file which contains the item at the mouse click."
7296 (interactive "e")
7297 (mouse-set-point ev)
7298 (org-agenda-show))
7300 (defun org-agenda-check-no-diary ()
7301 "Check if the entry is a diary link and abort if yes."
7302 (if (org-get-at-bol 'org-agenda-diary-link)
7303 (org-agenda-error)))
7305 (defun org-agenda-error ()
7306 (error "Command not allowed in this line"))
7308 (defun org-agenda-tree-to-indirect-buffer ()
7309 "Show the subtree corresponding to the current entry in an indirect buffer.
7310 This calls the command `org-tree-to-indirect-buffer' from the original
7311 Org-mode buffer.
7312 With numerical prefix arg ARG, go up to this level and then take that tree.
7313 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7314 use the dedicated frame)."
7315 (interactive)
7316 (if (and current-prefix-arg (listp current-prefix-arg))
7317 (org-agenda-do-tree-to-indirect-buffer)
7318 (let ((agenda-window (selected-window))
7319 (indirect-window
7320 (and org-last-indirect-buffer
7321 (get-buffer-window org-last-indirect-buffer))))
7322 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7323 (unwind-protect
7324 (progn
7325 (unless (and indirect-window (window-live-p indirect-window))
7326 (setq indirect-window (split-window agenda-window)))
7327 (select-window indirect-window)
7328 (switch-to-buffer org-last-indirect-buffer :norecord)
7329 (fit-window-to-buffer indirect-window))
7330 (select-window (get-buffer-window org-agenda-buffer-name))))))
7332 (defun org-agenda-do-tree-to-indirect-buffer ()
7333 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7334 (org-agenda-check-no-diary)
7335 (let* ((marker (or (org-get-at-bol 'org-marker)
7336 (org-agenda-error)))
7337 (buffer (marker-buffer marker))
7338 (pos (marker-position marker)))
7339 (with-current-buffer buffer
7340 (save-excursion
7341 (goto-char pos)
7342 (call-interactively 'org-tree-to-indirect-buffer)))))
7344 (defvar org-last-heading-marker (make-marker)
7345 "Marker pointing to the headline that last changed its TODO state
7346 by a remote command from the agenda.")
7348 (defun org-agenda-todo-nextset ()
7349 "Switch TODO entry to next sequence."
7350 (interactive)
7351 (org-agenda-todo 'nextset))
7353 (defun org-agenda-todo-previousset ()
7354 "Switch TODO entry to previous sequence."
7355 (interactive)
7356 (org-agenda-todo 'previousset))
7358 (defun org-agenda-todo (&optional arg)
7359 "Cycle TODO state of line at point, also in Org-mode file.
7360 This changes the line at point, all other lines in the agenda referring to
7361 the same tree node, and the headline of the tree node in the Org-mode file."
7362 (interactive "P")
7363 (org-agenda-check-no-diary)
7364 (let* ((col (current-column))
7365 (marker (or (org-get-at-bol 'org-marker)
7366 (org-agenda-error)))
7367 (buffer (marker-buffer marker))
7368 (pos (marker-position marker))
7369 (hdmarker (org-get-at-bol 'org-hd-marker))
7370 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7371 (inhibit-read-only t)
7372 org-agenda-headline-snapshot-before-repeat newhead just-one)
7373 (org-with-remote-undo buffer
7374 (with-current-buffer buffer
7375 (widen)
7376 (goto-char pos)
7377 (org-show-context 'agenda)
7378 (save-excursion
7379 (and (outline-next-heading)
7380 (org-flag-heading nil))) ; show the next heading
7381 (let ((current-prefix-arg arg))
7382 (call-interactively 'org-todo))
7383 (and (bolp) (forward-char 1))
7384 (setq newhead (org-get-heading))
7385 (when (and (org-bound-and-true-p
7386 org-agenda-headline-snapshot-before-repeat)
7387 (not (equal org-agenda-headline-snapshot-before-repeat
7388 newhead))
7389 todayp)
7390 (setq newhead org-agenda-headline-snapshot-before-repeat
7391 just-one t))
7392 (save-excursion
7393 (org-back-to-heading)
7394 (move-marker org-last-heading-marker (point))))
7395 (beginning-of-line 1)
7396 (save-excursion
7397 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7398 (org-move-to-column col))))
7400 (defun org-agenda-add-note (&optional arg)
7401 "Add a time-stamped note to the entry at point."
7402 (interactive "P")
7403 (org-agenda-check-no-diary)
7404 (let* ((marker (or (org-get-at-bol 'org-marker)
7405 (org-agenda-error)))
7406 (buffer (marker-buffer marker))
7407 (pos (marker-position marker))
7408 (hdmarker (org-get-at-bol 'org-hd-marker))
7409 (inhibit-read-only t))
7410 (with-current-buffer buffer
7411 (widen)
7412 (goto-char pos)
7413 (org-show-context 'agenda)
7414 (save-excursion
7415 (and (outline-next-heading)
7416 (org-flag-heading nil))) ; show the next heading
7417 (org-add-note))))
7419 (defun org-agenda-change-all-lines (newhead hdmarker
7420 &optional fixface just-this)
7421 "Change all lines in the agenda buffer which match HDMARKER.
7422 The new content of the line will be NEWHEAD (as modified by
7423 `org-agenda-format-item'). HDMARKER is checked with
7424 `equal' against all `org-hd-marker' text properties in the file.
7425 If FIXFACE is non-nil, the face of each item is modified according to
7426 the new TODO state.
7427 If JUST-THIS is non-nil, change just the current line, not all.
7428 If FORCE-TAGS is non nil, the car of it returns the new tags."
7429 (let* ((inhibit-read-only t)
7430 (line (org-current-line))
7431 (thetags (with-current-buffer (marker-buffer hdmarker)
7432 (save-excursion (save-restriction (widen)
7433 (goto-char hdmarker)
7434 (org-get-tags-at)))))
7435 props m pl undone-face done-face finish new dotime cat tags)
7436 (save-excursion
7437 (goto-char (point-max))
7438 (beginning-of-line 1)
7439 (while (not finish)
7440 (setq finish (bobp))
7441 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7442 (or (not just-this) (= (org-current-line) line))
7443 (equal m hdmarker))
7444 (setq props (text-properties-at (point))
7445 dotime (org-get-at-bol 'dotime)
7446 cat (org-get-at-bol 'org-category)
7447 tags thetags
7449 (let ((org-prefix-format-compiled
7450 (or (get-text-property (point) 'format)
7451 org-prefix-format-compiled)))
7452 (with-current-buffer (marker-buffer hdmarker)
7453 (save-excursion
7454 (save-restriction
7455 (widen)
7456 (org-agenda-format-item (org-get-at-bol 'extra)
7457 newhead cat tags dotime)))))
7458 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7459 undone-face (org-get-at-bol 'undone-face)
7460 done-face (org-get-at-bol 'done-face))
7461 (beginning-of-line 1)
7462 (cond
7463 ((equal new "")
7464 (and (looking-at ".*\n?") (replace-match "")))
7465 ((looking-at ".*")
7466 (replace-match new t t)
7467 (beginning-of-line 1)
7468 (add-text-properties (point-at-bol) (point-at-eol) props)
7469 (when fixface
7470 (add-text-properties
7471 (point-at-bol) (point-at-eol)
7472 (list 'face
7473 (if org-last-todo-state-is-todo
7474 undone-face done-face))))
7475 (org-agenda-highlight-todo 'line)
7476 (beginning-of-line 1))
7477 (t (error "Line update did not work"))))
7478 (beginning-of-line 0)))
7479 (org-finalize-agenda)))
7481 (defun org-agenda-align-tags (&optional line)
7482 "Align all tags in agenda items to `org-agenda-tags-column'."
7483 (let ((inhibit-read-only t) l c)
7484 (save-excursion
7485 (goto-char (if line (point-at-bol) (point-min)))
7486 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7487 (if line (point-at-eol) nil) t)
7488 (add-text-properties
7489 (match-beginning 2) (match-end 2)
7490 (list 'face (delq nil (let ((prop (get-text-property
7491 (match-beginning 2) 'face)))
7492 (or (listp prop) (setq prop (list prop)))
7493 (if (memq 'org-tag prop)
7494 prop
7495 (cons 'org-tag prop))))))
7496 (setq l (- (match-end 2) (match-beginning 2))
7497 c (if (< org-agenda-tags-column 0)
7498 (- (abs org-agenda-tags-column) l)
7499 org-agenda-tags-column))
7500 (delete-region (match-beginning 1) (match-end 1))
7501 (goto-char (match-beginning 1))
7502 (insert (org-add-props
7503 (make-string (max 1 (- c (current-column))) ?\ )
7504 (plist-put (copy-sequence (text-properties-at (point)))
7505 'face nil))))
7506 (goto-char (point-min))
7507 (org-font-lock-add-tag-faces (point-max)))))
7509 (defun org-agenda-priority-up ()
7510 "Increase the priority of line at point, also in Org-mode file."
7511 (interactive)
7512 (org-agenda-priority 'up))
7514 (defun org-agenda-priority-down ()
7515 "Decrease the priority of line at point, also in Org-mode file."
7516 (interactive)
7517 (org-agenda-priority 'down))
7519 (defun org-agenda-priority (&optional force-direction)
7520 "Set the priority of line at point, also in Org-mode file.
7521 This changes the line at point, all other lines in the agenda referring to
7522 the same tree node, and the headline of the tree node in the Org-mode file."
7523 (interactive)
7524 (unless org-enable-priority-commands
7525 (error "Priority commands are disabled"))
7526 (org-agenda-check-no-diary)
7527 (let* ((marker (or (org-get-at-bol 'org-marker)
7528 (org-agenda-error)))
7529 (hdmarker (org-get-at-bol 'org-hd-marker))
7530 (buffer (marker-buffer hdmarker))
7531 (pos (marker-position hdmarker))
7532 (inhibit-read-only t)
7533 newhead)
7534 (org-with-remote-undo buffer
7535 (with-current-buffer buffer
7536 (widen)
7537 (goto-char pos)
7538 (org-show-context 'agenda)
7539 (save-excursion
7540 (and (outline-next-heading)
7541 (org-flag-heading nil))) ; show the next heading
7542 (funcall 'org-priority force-direction)
7543 (end-of-line 1)
7544 (setq newhead (org-get-heading)))
7545 (org-agenda-change-all-lines newhead hdmarker)
7546 (beginning-of-line 1))))
7548 ;; FIXME: should fix the tags property of the agenda line.
7549 (defun org-agenda-set-tags (&optional tag onoff)
7550 "Set tags for the current headline."
7551 (interactive)
7552 (org-agenda-check-no-diary)
7553 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7554 (call-interactively 'org-change-tag-in-region)
7555 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7556 (org-agenda-error)))
7557 (buffer (marker-buffer hdmarker))
7558 (pos (marker-position hdmarker))
7559 (inhibit-read-only t)
7560 newhead)
7561 (org-with-remote-undo buffer
7562 (with-current-buffer buffer
7563 (widen)
7564 (goto-char pos)
7565 (save-excursion
7566 (org-show-context 'agenda))
7567 (save-excursion
7568 (and (outline-next-heading)
7569 (org-flag-heading nil))) ; show the next heading
7570 (goto-char pos)
7571 (if tag
7572 (org-toggle-tag tag onoff)
7573 (call-interactively 'org-set-tags))
7574 (end-of-line 1)
7575 (setq newhead (org-get-heading)))
7576 (org-agenda-change-all-lines newhead hdmarker)
7577 (beginning-of-line 1)))))
7579 (defun org-agenda-set-property ()
7580 "Set a property for the current headline."
7581 (interactive)
7582 (org-agenda-check-no-diary)
7583 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7584 (org-agenda-error)))
7585 (buffer (marker-buffer hdmarker))
7586 (pos (marker-position hdmarker))
7587 (inhibit-read-only t)
7588 newhead)
7589 (org-with-remote-undo buffer
7590 (with-current-buffer buffer
7591 (widen)
7592 (goto-char pos)
7593 (save-excursion
7594 (org-show-context 'agenda))
7595 (save-excursion
7596 (and (outline-next-heading)
7597 (org-flag-heading nil))) ; show the next heading
7598 (goto-char pos)
7599 (call-interactively 'org-set-property)))))
7601 (defun org-agenda-set-effort ()
7602 "Set the effort property for the current headline."
7603 (interactive)
7604 (org-agenda-check-no-diary)
7605 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7606 (org-agenda-error)))
7607 (buffer (marker-buffer hdmarker))
7608 (pos (marker-position hdmarker))
7609 (inhibit-read-only t)
7610 newhead)
7611 (org-with-remote-undo buffer
7612 (with-current-buffer buffer
7613 (widen)
7614 (goto-char pos)
7615 (save-excursion
7616 (org-show-context 'agenda))
7617 (save-excursion
7618 (and (outline-next-heading)
7619 (org-flag-heading nil))) ; show the next heading
7620 (goto-char pos)
7621 (call-interactively 'org-set-effort)
7622 (end-of-line 1)
7623 (setq newhead (org-get-heading)))
7624 (org-agenda-change-all-lines newhead hdmarker))))
7626 (defun org-agenda-toggle-archive-tag ()
7627 "Toggle the archive tag for the current entry."
7628 (interactive)
7629 (org-agenda-check-no-diary)
7630 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7631 (org-agenda-error)))
7632 (buffer (marker-buffer hdmarker))
7633 (pos (marker-position hdmarker))
7634 (inhibit-read-only t)
7635 newhead)
7636 (org-with-remote-undo buffer
7637 (with-current-buffer buffer
7638 (widen)
7639 (goto-char pos)
7640 (org-show-context 'agenda)
7641 (save-excursion
7642 (and (outline-next-heading)
7643 (org-flag-heading nil))) ; show the next heading
7644 (call-interactively 'org-toggle-archive-tag)
7645 (end-of-line 1)
7646 (setq newhead (org-get-heading)))
7647 (org-agenda-change-all-lines newhead hdmarker)
7648 (beginning-of-line 1))))
7650 (defun org-agenda-do-date-later (arg)
7651 (interactive "P")
7652 (cond
7653 ((or (equal arg '(16))
7654 (memq last-command
7655 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7656 (setq this-command 'org-agenda-date-later-minutes)
7657 (org-agenda-date-later-minutes 1))
7658 ((or (equal arg '(4))
7659 (memq last-command
7660 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7661 (setq this-command 'org-agenda-date-later-hours)
7662 (org-agenda-date-later-hours 1))
7664 (org-agenda-date-later (prefix-numeric-value arg)))))
7666 (defun org-agenda-do-date-earlier (arg)
7667 (interactive "P")
7668 (cond
7669 ((or (equal arg '(16))
7670 (memq last-command
7671 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7672 (setq this-command 'org-agenda-date-earlier-minutes)
7673 (org-agenda-date-earlier-minutes 1))
7674 ((or (equal arg '(4))
7675 (memq last-command
7676 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7677 (setq this-command 'org-agenda-date-earlier-hours)
7678 (org-agenda-date-earlier-hours 1))
7680 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7682 (defun org-agenda-date-later (arg &optional what)
7683 "Change the date of this item to ARG day(s) later."
7684 (interactive "p")
7685 (org-agenda-check-type t 'agenda 'timeline)
7686 (org-agenda-check-no-diary)
7687 (let* ((marker (or (org-get-at-bol 'org-marker)
7688 (org-agenda-error)))
7689 (buffer (marker-buffer marker))
7690 (pos (marker-position marker))
7691 cdate today)
7692 (org-with-remote-undo buffer
7693 (with-current-buffer buffer
7694 (widen)
7695 (goto-char pos)
7696 (if (not (org-at-timestamp-p))
7697 (error "Cannot find time stamp"))
7698 (when (and org-agenda-move-date-from-past-immediately-to-today
7699 (equal arg 1)
7700 (or (not what) (eq what 'day))
7701 (not (save-match-data (org-at-date-range-p))))
7702 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7703 cdate (calendar-absolute-from-gregorian
7704 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7705 today (org-today))
7706 (if (> today cdate)
7707 ;; immediately shift to today
7708 (setq arg (- today cdate))))
7709 (org-timestamp-change arg (or what 'day))
7710 (when (and (org-at-date-range-p)
7711 (re-search-backward org-tr-regexp-both (point-at-bol)))
7712 (let ((end org-last-changed-timestamp))
7713 (org-timestamp-change arg (or what 'day))
7714 (setq org-last-changed-timestamp
7715 (concat org-last-changed-timestamp "--" end)))))
7716 (org-agenda-show-new-time marker org-last-changed-timestamp))
7717 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7719 (defun org-agenda-date-earlier (arg &optional what)
7720 "Change the date of this item to ARG day(s) earlier."
7721 (interactive "p")
7722 (org-agenda-date-later (- arg) what))
7724 (defun org-agenda-date-later-minutes (arg)
7725 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7726 (interactive "p")
7727 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7728 (org-agenda-date-later arg 'minute))
7730 (defun org-agenda-date-earlier-minutes (arg)
7731 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7732 (interactive "p")
7733 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7734 (org-agenda-date-earlier arg 'minute))
7736 (defun org-agenda-date-later-hours (arg)
7737 "Change the time of this item, in hour steps."
7738 (interactive "p")
7739 (org-agenda-date-later arg 'hour))
7741 (defun org-agenda-date-earlier-hours (arg)
7742 "Change the time of this item, in hour steps."
7743 (interactive "p")
7744 (org-agenda-date-earlier arg 'hour))
7746 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7747 "Show new date stamp via text properties."
7748 ;; We use text properties to make this undoable
7749 (let ((inhibit-read-only t)
7750 (buffer-invisibility-spec))
7751 (setq stamp (concat " " prefix " => " stamp))
7752 (save-excursion
7753 (goto-char (point-max))
7754 (while (not (bobp))
7755 (when (equal marker (org-get-at-bol 'org-marker))
7756 (org-move-to-column (- (window-width) (length stamp)) t)
7757 (org-agenda-fix-tags-filter-overlays-at (point))
7758 (if (featurep 'xemacs)
7759 ;; Use `duplicable' property to trigger undo recording
7760 (let ((ex (make-extent nil nil))
7761 (gl (make-glyph stamp)))
7762 (set-glyph-face gl 'secondary-selection)
7763 (set-extent-properties
7764 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7765 (insert-extent ex (1- (point)) (point-at-eol)))
7766 (add-text-properties
7767 (1- (point)) (point-at-eol)
7768 (list 'display (org-add-props stamp nil
7769 'face 'secondary-selection))))
7770 (beginning-of-line 1))
7771 (beginning-of-line 0)))))
7773 (defun org-agenda-date-prompt (arg)
7774 "Change the date of this item. Date is prompted for, with default today.
7775 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7776 be used to request time specification in the time stamp."
7777 (interactive "P")
7778 (org-agenda-check-type t 'agenda 'timeline)
7779 (org-agenda-check-no-diary)
7780 (let* ((marker (or (org-get-at-bol 'org-marker)
7781 (org-agenda-error)))
7782 (buffer (marker-buffer marker))
7783 (pos (marker-position marker)))
7784 (org-with-remote-undo buffer
7785 (with-current-buffer buffer
7786 (widen)
7787 (goto-char pos)
7788 (if (not (org-at-timestamp-p t))
7789 (error "Cannot find time stamp"))
7790 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7791 (org-agenda-show-new-time marker org-last-changed-timestamp))
7792 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7794 (defun org-agenda-schedule (arg &optional time)
7795 "Schedule the item at point.
7796 ARG is passed through to `org-schedule'."
7797 (interactive "P")
7798 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7799 (org-agenda-check-no-diary)
7800 (let* ((marker (or (org-get-at-bol 'org-marker)
7801 (org-agenda-error)))
7802 (type (marker-insertion-type marker))
7803 (buffer (marker-buffer marker))
7804 (pos (marker-position marker))
7805 (org-insert-labeled-timestamps-at-point nil)
7807 (set-marker-insertion-type marker t)
7808 (org-with-remote-undo buffer
7809 (with-current-buffer buffer
7810 (widen)
7811 (goto-char pos)
7812 (setq ts (org-schedule arg time)))
7813 (org-agenda-show-new-time marker ts "S"))
7814 (message "Item scheduled for %s" ts)))
7816 (defun org-agenda-deadline (arg &optional time)
7817 "Schedule the item at point.
7818 ARG is passed through to `org-deadline'."
7819 (interactive "P")
7820 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7821 (org-agenda-check-no-diary)
7822 (let* ((marker (or (org-get-at-bol 'org-marker)
7823 (org-agenda-error)))
7824 (buffer (marker-buffer marker))
7825 (pos (marker-position marker))
7826 (org-insert-labeled-timestamps-at-point nil)
7828 (org-with-remote-undo buffer
7829 (with-current-buffer buffer
7830 (widen)
7831 (goto-char pos)
7832 (setq ts (org-deadline arg time)))
7833 (org-agenda-show-new-time marker ts "D"))
7834 (message "Deadline for this item set to %s" ts)))
7836 (defun org-agenda-action ()
7837 "Select entry for agenda action, or execute an agenda action.
7838 This command prompts for another letter. Valid inputs are:
7840 m Mark the entry at point for an agenda action
7841 s Schedule the marked entry to the date at the cursor
7842 d Set the deadline of the marked entry to the date at the cursor
7843 r Call `org-remember' with cursor date as the default date
7844 c Call `org-capture' with cursor date as the default date
7845 SPC Show marked entry in other window
7846 TAB Visit marked entry in other window
7848 The cursor may be at a date in the calendar, or in the Org agenda."
7849 (interactive)
7850 (let (ans)
7851 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7852 (setq ans (read-char-exclusive))
7853 (cond
7854 ((equal ans ?m)
7855 ;; Mark this entry
7856 (if (eq major-mode 'org-agenda-mode)
7857 (let ((m (or (org-get-at-bol 'org-hd-marker)
7858 (org-get-at-bol 'org-marker))))
7859 (if m
7860 (progn
7861 (move-marker org-agenda-action-marker
7862 (marker-position m) (marker-buffer m))
7863 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7864 (error "Don't know which entry to mark")))
7865 (error "This command works only in the agenda")))
7866 ((equal ans ?s)
7867 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7868 ((equal ans ?d)
7869 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7870 ((equal ans ?r)
7871 (org-agenda-do-action '(org-remember) t))
7872 ((equal ans ?c)
7873 (org-agenda-do-action '(org-capture) t))
7874 ((equal ans ?\ )
7875 (let ((cw (selected-window)))
7876 (org-switch-to-buffer-other-window
7877 (marker-buffer org-agenda-action-marker))
7878 (goto-char org-agenda-action-marker)
7879 (org-show-context 'agenda)
7880 (select-window cw)))
7881 ((equal ans ?\C-i)
7882 (org-switch-to-buffer-other-window
7883 (marker-buffer org-agenda-action-marker))
7884 (goto-char org-agenda-action-marker)
7885 (org-show-context 'agenda))
7886 (t (error "Invalid agenda action %c" ans)))))
7888 (defun org-agenda-do-action (form &optional current-buffer)
7889 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7890 (let ((org-overriding-default-time (org-get-cursor-date)))
7891 (if current-buffer
7892 (eval form)
7893 (if (not (marker-buffer org-agenda-action-marker))
7894 (error "No entry has been selected for agenda action")
7895 (with-current-buffer (marker-buffer org-agenda-action-marker)
7896 (save-excursion
7897 (save-restriction
7898 (widen)
7899 (goto-char org-agenda-action-marker)
7900 (eval form))))))))
7902 (defun org-agenda-clock-in (&optional arg)
7903 "Start the clock on the currently selected item."
7904 (interactive "P")
7905 (org-agenda-check-no-diary)
7906 (if (equal arg '(4))
7907 (org-clock-in arg)
7908 (let* ((marker (or (org-get-at-bol 'org-marker)
7909 (org-agenda-error)))
7910 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7911 marker))
7912 (pos (marker-position marker))
7913 newhead)
7914 (org-with-remote-undo (marker-buffer marker)
7915 (with-current-buffer (marker-buffer marker)
7916 (widen)
7917 (goto-char pos)
7918 (org-show-context 'agenda)
7919 (org-show-entry)
7920 (org-cycle-hide-drawers 'children)
7921 (org-clock-in arg)
7922 (setq newhead (org-get-heading)))
7923 (org-agenda-change-all-lines newhead hdmarker)))))
7925 (defun org-agenda-clock-out ()
7926 "Stop the currently running clock."
7927 (interactive)
7928 (unless (marker-buffer org-clock-marker)
7929 (error "No running clock"))
7930 (let ((marker (make-marker)) newhead)
7931 (org-with-remote-undo (marker-buffer org-clock-marker)
7932 (with-current-buffer (marker-buffer org-clock-marker)
7933 (save-excursion
7934 (save-restriction
7935 (widen)
7936 (goto-char org-clock-marker)
7937 (org-back-to-heading t)
7938 (move-marker marker (point))
7939 (org-clock-out)
7940 (setq newhead (org-get-heading))))))
7941 (org-agenda-change-all-lines newhead marker)
7942 (move-marker marker nil)))
7944 (defun org-agenda-clock-cancel (&optional arg)
7945 "Cancel the currently running clock."
7946 (interactive "P")
7947 (unless (marker-buffer org-clock-marker)
7948 (error "No running clock"))
7949 (org-with-remote-undo (marker-buffer org-clock-marker)
7950 (org-clock-cancel)))
7952 (defun org-agenda-clock-goto ()
7953 "Jump to the currently clocked in task within the agenda.
7954 If the currently clocked in task is not listed in the agenda
7955 buffer, display it in another window."
7956 (interactive)
7957 (let (pos)
7958 (mapc (lambda (o)
7959 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7960 (setq pos (overlay-start o))))
7961 (overlays-in (point-min) (point-max)))
7962 (cond (pos (goto-char pos))
7963 ;; If the currently clocked entry is not in the agenda
7964 ;; buffer, we visit it in another window:
7965 (org-clock-current-task
7966 (org-switch-to-buffer-other-window (org-clock-goto)))
7967 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7969 (defun org-agenda-diary-entry-in-org-file ()
7970 "Make a diary entry in the file `org-agenda-diary-file'."
7971 (let (d1 d2 char (text "") dp1 dp2)
7972 (if (equal (buffer-name) "*Calendar*")
7973 (setq d1 (calendar-cursor-to-date t)
7974 d2 (car calendar-mark-ring))
7975 (setq dp1 (get-text-property (point-at-bol) 'day))
7976 (unless dp1 (error "No date defined in current line"))
7977 (setq d1 (calendar-gregorian-from-absolute dp1)
7978 d2 (and (ignore-errors (mark))
7979 (save-excursion
7980 (goto-char (mark))
7981 (setq dp2 (get-text-property (point-at-bol) 'day)))
7982 (calendar-gregorian-from-absolute dp2))))
7983 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7984 (setq char (read-char-exclusive))
7985 (cond
7986 ((equal char ?d)
7987 (setq text (read-string "Day entry: "))
7988 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7989 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7990 ((equal char ?a)
7991 (setq d1 (list (car d1) (nth 1 d1)
7992 (read-number (format "Reference year [%d]: " (nth 2 d1))
7993 (nth 2 d1))))
7994 (setq text (read-string "Anniversary (use %d to show years): "))
7995 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7996 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7997 ((equal char ?b)
7998 (setq text (read-string "Block entry: "))
7999 (unless (and d1 d2 (not (equal d1 d2)))
8000 (error "No block of days selected"))
8001 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8002 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8003 ((equal char ?j)
8004 (org-switch-to-buffer-other-window
8005 (find-file-noselect org-agenda-diary-file))
8006 (require 'org-datetree)
8007 (org-datetree-find-date-create d1)
8008 (org-reveal t))
8009 (t (error "Invalid selection character `%c'" char)))))
8011 (defcustom org-agenda-insert-diary-strategy 'date-tree
8012 "Where in `org-agenda-diary-file' should new entries be added?
8013 Valid values:
8015 date-tree in the date tree, as child of the date
8016 top-level as top-level entries at the end of the file."
8017 :group 'org-agenda
8018 :type '(choice
8019 (const :tag "in a date tree" date-tree)
8020 (const :tag "as top level at end of file" top-level)))
8022 (defcustom org-agenda-insert-diary-extract-time nil
8023 "Non-nil means extract any time specification from the diary entry."
8024 :group 'org-agenda
8025 :version "24.1"
8026 :type 'boolean)
8028 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8029 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8030 If TEXT is not empty, it will become the headline of the new entry, and
8031 the resulting entry will not be shown. When TEXT is empty, switch to
8032 `org-agenda-diary-file' and let the user finish the entry there."
8033 (let ((cw (current-window-configuration)))
8034 (org-switch-to-buffer-other-window
8035 (find-file-noselect org-agenda-diary-file))
8036 (widen)
8037 (goto-char (point-min))
8038 (cond
8039 ((eq type 'anniversary)
8040 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8041 (progn
8042 (or (org-at-heading-p t)
8043 (progn
8044 (outline-next-heading)
8045 (insert "* Anniversaries\n\n")
8046 (beginning-of-line -1)))))
8047 (outline-next-heading)
8048 (org-back-over-empty-lines)
8049 (backward-char 1)
8050 (insert "\n")
8051 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8052 (nth 2 d1) (car d1) (nth 1 d1) text)))
8053 ((eq type 'day)
8054 (let ((org-prefix-has-time t)
8055 (org-agenda-time-leading-zero t)
8056 fmt time time2)
8057 (if org-agenda-insert-diary-extract-time
8058 ;; Use org-agenda-format-item to parse text for a time-range and
8059 ;; remove it. FIXME: This is a hack, we should refactor
8060 ;; that function to make time extraction available separately
8061 (setq fmt (org-agenda-format-item nil text nil nil t)
8062 time (get-text-property 0 'time fmt)
8063 time2 (if (> (length time) 0)
8064 ;; split-string removes trailing ...... if
8065 ;; no end time given. First space
8066 ;; separates time from date.
8067 (concat " " (car (split-string time "\\.")))
8068 nil)
8069 text (get-text-property 0 'txt fmt)))
8070 (if (eq org-agenda-insert-diary-strategy 'top-level)
8071 (org-agenda-insert-diary-as-top-level text)
8072 (require 'org-datetree)
8073 (org-datetree-find-date-create d1)
8074 (org-agenda-insert-diary-make-new-entry text))
8075 (org-insert-time-stamp (org-time-from-absolute
8076 (calendar-absolute-from-gregorian d1))
8077 nil nil nil nil time2))
8078 (end-of-line 0))
8079 ((eq type 'block)
8080 (if (> (calendar-absolute-from-gregorian d1)
8081 (calendar-absolute-from-gregorian d2))
8082 (setq d1 (prog1 d2 (setq d2 d1))))
8083 (if (eq org-agenda-insert-diary-strategy 'top-level)
8084 (org-agenda-insert-diary-as-top-level text)
8085 (require 'org-datetree)
8086 (org-datetree-find-date-create d1)
8087 (org-agenda-insert-diary-make-new-entry text))
8088 (org-insert-time-stamp (org-time-from-absolute
8089 (calendar-absolute-from-gregorian d1)))
8090 (insert "--")
8091 (org-insert-time-stamp (org-time-from-absolute
8092 (calendar-absolute-from-gregorian d2)))
8093 (end-of-line 0)))
8094 (if (string-match "\\S-" text)
8095 (progn
8096 (set-window-configuration cw)
8097 (message "%s entry added to %s"
8098 (capitalize (symbol-name type))
8099 (abbreviate-file-name org-agenda-diary-file)))
8100 (org-reveal t)
8101 (message "Please finish entry here"))))
8103 (defun org-agenda-insert-diary-as-top-level (text)
8104 "Make new entry as a top-level entry at the end of the file.
8105 Add TEXT as headline, and position the cursor in the second line so that
8106 a timestamp can be added there."
8107 (widen)
8108 (goto-char (point-max))
8109 (or (bolp) (insert "\n"))
8110 (insert "* " text "\n")
8111 (if org-adapt-indentation (org-indent-to-column 2)))
8113 (defun org-agenda-insert-diary-make-new-entry (text)
8114 "Make new entry as last child of current entry.
8115 Add TEXT as headline, and position the cursor in the second line so that
8116 a timestamp can be added there."
8117 (let ((org-show-following-heading t)
8118 (org-show-siblings t)
8119 (org-show-hierarchy-above t)
8120 (org-show-entry-below t)
8121 col)
8122 (outline-next-heading)
8123 (org-back-over-empty-lines)
8124 (or (looking-at "[ \t]*$")
8125 (progn (insert "\n") (backward-char 1)))
8126 (org-insert-heading nil t)
8127 (org-do-demote)
8128 (setq col (current-column))
8129 (insert text "\n")
8130 (if org-adapt-indentation (org-indent-to-column col))
8131 (let ((org-show-following-heading t)
8132 (org-show-siblings t)
8133 (org-show-hierarchy-above t)
8134 (org-show-entry-below t))
8135 (org-show-context))))
8137 (defun org-agenda-diary-entry ()
8138 "Make a diary entry, like the `i' command from the calendar.
8139 All the standard commands work: block, weekly etc.
8140 When `org-agenda-diary-file' points to a file,
8141 `org-agenda-diary-entry-in-org-file' is called instead to create
8142 entries in that Org-mode file."
8143 (interactive)
8144 (org-agenda-check-type t 'agenda 'timeline)
8145 (if (not (eq org-agenda-diary-file 'diary-file))
8146 (org-agenda-diary-entry-in-org-file)
8147 (require 'diary-lib)
8148 (let* ((char (progn
8149 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8150 (read-char-exclusive)))
8151 (cmd (cdr (assoc char
8152 '((?d . insert-diary-entry)
8153 (?w . insert-weekly-diary-entry)
8154 (?m . insert-monthly-diary-entry)
8155 (?y . insert-yearly-diary-entry)
8156 (?a . insert-anniversary-diary-entry)
8157 (?b . insert-block-diary-entry)
8158 (?c . insert-cyclic-diary-entry)))))
8159 (oldf (symbol-function 'calendar-cursor-to-date))
8160 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8161 (point (point))
8162 (mark (or (mark t) (point))))
8163 (unless cmd
8164 (error "No command associated with <%c>" char))
8165 (unless (and (get-text-property point 'day)
8166 (or (not (equal ?b char))
8167 (get-text-property mark 'day)))
8168 (error "Don't know which date to use for diary entry"))
8169 ;; We implement this by hacking the `calendar-cursor-to-date' function
8170 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8171 (let ((calendar-mark-ring
8172 (list (calendar-gregorian-from-absolute
8173 (or (get-text-property mark 'day)
8174 (get-text-property point 'day))))))
8175 (unwind-protect
8176 (progn
8177 (fset 'calendar-cursor-to-date
8178 (lambda (&optional error dummy)
8179 (calendar-gregorian-from-absolute
8180 (get-text-property point 'day))))
8181 (call-interactively cmd))
8182 (fset 'calendar-cursor-to-date oldf))))))
8184 (defun org-agenda-execute-calendar-command (cmd)
8185 "Execute a calendar command from the agenda, with the date associated to
8186 the cursor position."
8187 (org-agenda-check-type t 'agenda 'timeline)
8188 (require 'diary-lib)
8189 (unless (get-text-property (point) 'day)
8190 (error "Don't know which date to use for calendar command"))
8191 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8192 (point (point))
8193 (date (calendar-gregorian-from-absolute
8194 (get-text-property point 'day)))
8195 ;; the following 2 vars are needed in the calendar
8196 (displayed-month (car date))
8197 (displayed-year (nth 2 date)))
8198 (unwind-protect
8199 (progn
8200 (fset 'calendar-cursor-to-date
8201 (lambda (&optional error dummy)
8202 (calendar-gregorian-from-absolute
8203 (get-text-property point 'day))))
8204 (call-interactively cmd))
8205 (fset 'calendar-cursor-to-date oldf))))
8207 (defun org-agenda-phases-of-moon ()
8208 "Display the phases of the moon for the 3 months around the cursor date."
8209 (interactive)
8210 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8212 (defun org-agenda-holidays ()
8213 "Display the holidays for the 3 months around the cursor date."
8214 (interactive)
8215 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8217 (defvar calendar-longitude)
8218 (defvar calendar-latitude)
8219 (defvar calendar-location-name)
8221 (defun org-agenda-sunrise-sunset (arg)
8222 "Display sunrise and sunset for the cursor date.
8223 Latitude and longitude can be specified with the variables
8224 `calendar-latitude' and `calendar-longitude'. When called with prefix
8225 argument, latitude and longitude will be prompted for."
8226 (interactive "P")
8227 (require 'solar)
8228 (let ((calendar-longitude (if arg nil calendar-longitude))
8229 (calendar-latitude (if arg nil calendar-latitude))
8230 (calendar-location-name
8231 (if arg "the given coordinates" calendar-location-name)))
8232 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8234 (defun org-agenda-goto-calendar ()
8235 "Open the Emacs calendar with the date at the cursor."
8236 (interactive)
8237 (org-agenda-check-type t 'agenda 'timeline)
8238 (let* ((day (or (get-text-property (point) 'day)
8239 (error "Don't know which date to open in calendar")))
8240 (date (calendar-gregorian-from-absolute day))
8241 (calendar-move-hook nil)
8242 (calendar-view-holidays-initially-flag nil)
8243 (calendar-view-diary-initially-flag nil))
8244 (calendar)
8245 (calendar-goto-date date)))
8247 ;;;###autoload
8248 (defun org-calendar-goto-agenda ()
8249 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8250 This is a command that has to be installed in `calendar-mode-map'."
8251 (interactive)
8252 (org-agenda-list nil (calendar-absolute-from-gregorian
8253 (calendar-cursor-to-date))
8254 nil))
8256 (defun org-agenda-convert-date ()
8257 (interactive)
8258 (org-agenda-check-type t 'agenda 'timeline)
8259 (let ((day (get-text-property (point) 'day))
8260 date s)
8261 (unless day
8262 (error "Don't know which date to convert"))
8263 (setq date (calendar-gregorian-from-absolute day))
8264 (setq s (concat
8265 "Gregorian: " (calendar-date-string date) "\n"
8266 "ISO: " (calendar-iso-date-string date) "\n"
8267 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8268 "Julian: " (calendar-julian-date-string date) "\n"
8269 "Astron. JD: " (calendar-astro-date-string date)
8270 " (Julian date number at noon UTC)\n"
8271 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8272 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8273 "French: " (calendar-french-date-string date) "\n"
8274 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8275 "Mayan: " (calendar-mayan-date-string date) "\n"
8276 "Coptic: " (calendar-coptic-date-string date) "\n"
8277 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8278 "Persian: " (calendar-persian-date-string date) "\n"
8279 "Chinese: " (calendar-chinese-date-string date) "\n"))
8280 (with-output-to-temp-buffer "*Dates*"
8281 (princ s))
8282 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8284 ;;; Bulk commands
8286 (defvar org-agenda-bulk-marked-entries nil
8287 "List of markers that refer to marked entries in the agenda.")
8289 (defun org-agenda-bulk-marked-p ()
8290 (eq (get-char-property (point-at-bol) 'type)
8291 'org-marked-entry-overlay))
8293 (defun org-agenda-bulk-mark (&optional arg)
8294 "Mark the entry at point for future bulk action."
8295 (interactive "p")
8296 (dotimes (i (max arg 1))
8297 (unless (org-get-at-bol 'org-agenda-diary-link)
8298 (let* ((m (org-get-at-bol 'org-hd-marker))
8300 (unless (org-agenda-bulk-marked-p)
8301 (unless m (error "Nothing to mark at point"))
8302 (push m org-agenda-bulk-marked-entries)
8303 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8304 (org-overlay-display ov "> "
8305 (org-get-todo-face "TODO")
8306 'evaporate)
8307 (overlay-put ov 'type 'org-marked-entry-overlay))
8308 (beginning-of-line 2)
8309 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8310 (beginning-of-line 2))
8311 (message "%d entries marked for bulk action"
8312 (length org-agenda-bulk-marked-entries))))))
8314 (defun org-agenda-bulk-mark-regexp (regexp)
8315 "Mark entries match REGEXP."
8316 (interactive "sMark entries matching regexp: ")
8317 (let (entries-marked)
8318 (save-excursion
8319 (goto-char (point-min))
8320 (goto-char (next-single-property-change (point) 'txt))
8321 (while (re-search-forward regexp nil t)
8322 (when (string-match regexp (get-text-property (point) 'txt))
8323 (setq entries-marked (+ entries-marked 1))
8324 (call-interactively 'org-agenda-bulk-mark))))
8325 (if (not entries-marked)
8326 (message "No entry matching this regexp."))))
8328 (defun org-agenda-bulk-unmark ()
8329 "Unmark the entry at point for future bulk action."
8330 (interactive)
8331 (when (org-agenda-bulk-marked-p)
8332 (org-agenda-bulk-remove-overlays
8333 (point-at-bol) (+ 2 (point-at-bol)))
8334 (setq org-agenda-bulk-marked-entries
8335 (delete (org-get-at-bol 'org-hd-marker)
8336 org-agenda-bulk-marked-entries)))
8337 (beginning-of-line 2)
8338 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8339 (beginning-of-line 2))
8340 (message "%d entries marked for bulk action"
8341 (length org-agenda-bulk-marked-entries)))
8343 (defun org-agenda-bulk-toggle ()
8344 "Toggle marking the entry at point for bulk action."
8345 (interactive)
8346 (if (org-agenda-bulk-marked-p)
8347 (org-agenda-bulk-unmark)
8348 (org-agenda-bulk-mark)))
8350 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8351 "Remove the mark overlays between BEG and END in the agenda buffer.
8352 BEG and END default to the buffer limits.
8354 This only removes the overlays, it does not remove the markers
8355 from the list in `org-agenda-bulk-marked-entries'."
8356 (interactive)
8357 (mapc (lambda (ov)
8358 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8359 (delete-overlay ov)))
8360 (overlays-in (or beg (point-min)) (or end (point-max)))))
8362 (defun org-agenda-bulk-remove-all-marks ()
8363 "Remove all marks in the agenda buffer.
8364 This will remove the markers, and the overlays."
8365 (interactive)
8366 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8367 (setq org-agenda-bulk-marked-entries nil)
8368 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8370 (defun org-agenda-bulk-action (&optional arg)
8371 "Execute an remote-editing action on all marked entries.
8372 The prefix arg is passed through to the command if possible."
8373 (interactive "P")
8374 ;; Make sure we have markers, and only valid ones
8375 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8376 (mapc
8377 (lambda (m)
8378 (unless (and (markerp m)
8379 (marker-buffer m)
8380 (buffer-live-p (marker-buffer m))
8381 (marker-position m))
8382 (error "Marker %s for bulk command is invalid" m)))
8383 org-agenda-bulk-marked-entries)
8385 ;; Prompt for the bulk command
8386 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8387 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8388 (when org-agenda-bulk-custom-functions
8389 (concat " Custom: ["
8390 (mapconcat (lambda(f) (char-to-string (car f)))
8391 org-agenda-bulk-custom-functions "")
8392 "]"))))
8393 (let* ((action (read-char-exclusive))
8394 (org-log-refile (if org-log-refile 'time nil))
8395 (entries (reverse org-agenda-bulk-marked-entries))
8396 redo-at-end
8397 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8398 (cond
8399 ((equal action ?$)
8400 (setq cmd '(org-agenda-archive)))
8402 ((equal action ?A)
8403 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8405 ((member action '(?r ?w))
8406 (setq rfloc (org-refile-get-location
8407 "Refile to"
8408 (marker-buffer (car org-agenda-bulk-marked-entries))
8409 org-refile-allow-creating-parent-nodes))
8410 (if (nth 3 rfloc)
8411 (setcar (nthcdr 3 rfloc)
8412 (move-marker (make-marker) (nth 3 rfloc)
8413 (or (get-file-buffer (nth 1 rfloc))
8414 (find-buffer-visiting (nth 1 rfloc))
8415 (error "This should not happen")))))
8417 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8418 redo-at-end t))
8420 ((equal action ?t)
8421 (setq state (org-icompleting-read
8422 "Todo state: "
8423 (with-current-buffer (marker-buffer (car entries))
8424 (mapcar 'list org-todo-keywords-1))))
8425 (setq cmd `(let ((org-inhibit-blocking t)
8426 (org-inhibit-logging 'note))
8427 (org-agenda-todo ,state))))
8429 ((memq action '(?- ?+))
8430 (setq tag (org-icompleting-read
8431 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8432 (with-current-buffer (marker-buffer (car entries))
8433 (delq nil
8434 (mapcar (lambda (x)
8435 (if (stringp (car x)) x)) org-tag-alist)))))
8436 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8438 ((memq action '(?s ?d))
8439 (let* ((date (unless arg
8440 (org-read-date
8441 nil nil nil
8442 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8443 (ans (if arg nil org-read-date-final-answer))
8444 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8445 (setq cmd `(let* ((bound (fboundp 'read-string))
8446 (old (and bound (symbol-function 'read-string))))
8447 (unwind-protect
8448 (progn
8449 (fset 'read-string (lambda (&rest ignore) ,ans))
8450 (eval '(,c1 arg)))
8451 (if bound
8452 (fset 'read-string old)
8453 (fmakunbound 'read-string)))))))
8455 ((equal action ?S)
8456 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8457 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8458 (let ((days (read-number
8459 (format "Scatter tasks across how many %sdays: "
8460 (if arg "week" "")) 7)))
8461 (setq cmd
8462 `(let ((distance (1+ (random ,days))))
8463 (if arg
8464 (let ((dist distance)
8465 (day-of-week
8466 (calendar-day-of-week
8467 (calendar-gregorian-from-absolute (org-today)))))
8468 (dotimes (i (1+ dist))
8469 (while (member day-of-week org-agenda-weekend-days)
8470 (incf distance)
8471 (incf day-of-week)
8472 (if (= day-of-week 7)
8473 (setq day-of-week 0)))
8474 (incf day-of-week)
8475 (if (= day-of-week 7)
8476 (setq day-of-week 0)))))
8477 ;; silently fail when try to replan a sexp entry
8478 (condition-case nil
8479 (let* ((date (calendar-gregorian-from-absolute
8480 (+ (org-today) distance)))
8481 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8482 (nth 2 date))))
8483 (org-agenda-schedule nil time))
8484 (error nil)))))))
8486 ((assoc action org-agenda-bulk-custom-functions)
8487 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8488 redo-at-end t))
8490 ((equal action ?f)
8491 (setq cmd (list (intern
8492 (org-icompleting-read "Function: "
8493 obarray 'fboundp t nil nil)))))
8495 (t (error "Invalid bulk action")))
8497 ;; Sort the markers, to make sure that parents are handled before children
8498 (setq entries (sort entries
8499 (lambda (a b)
8500 (cond
8501 ((equal (marker-buffer a) (marker-buffer b))
8502 (< (marker-position a) (marker-position b)))
8504 (string< (buffer-name (marker-buffer a))
8505 (buffer-name (marker-buffer b))))))))
8507 ;; Now loop over all markers and apply cmd
8508 (while (setq e (pop entries))
8509 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8510 (if (not pos)
8511 (progn (message "Skipping removed entry at %s" e)
8512 (setq cntskip (1+ cntskip)))
8513 (goto-char pos)
8514 (let (org-loop-over-headlines-in-active-region)
8515 (eval cmd))
8516 (setq org-agenda-bulk-marked-entries
8517 (delete e org-agenda-bulk-marked-entries))
8518 (setq cnt (1+ cnt))))
8519 (setq org-agenda-bulk-marked-entries nil)
8520 (org-agenda-bulk-remove-all-marks)
8521 (when redo-at-end (org-agenda-redo))
8522 (message "Acted on %d entries%s"
8524 (if (= cntskip 0)
8526 (format ", skipped %d (disappeared before their turn)"
8527 cntskip)))))
8529 ;;; Flagging notes
8531 (defun org-agenda-show-the-flagging-note ()
8532 "Display the flagging note in the other window.
8533 When called a second time in direct sequence, offer to remove the FLAGGING
8534 tag and (if present) the flagging note."
8535 (interactive)
8536 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8537 (win (selected-window))
8538 note heading newhead)
8539 (unless hdmarker
8540 (error "No linked entry at point"))
8541 (if (and (eq this-command last-command)
8542 (y-or-n-p "Unflag and remove any flagging note? "))
8543 (progn
8544 (org-agenda-remove-flag hdmarker)
8545 (let ((win (get-buffer-window "*Flagging Note*")))
8546 (and win (delete-window win)))
8547 (message "Entry unflagged"))
8548 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8549 (unless note
8550 (error "No flagging note"))
8551 (org-kill-new note)
8552 (org-switch-to-buffer-other-window "*Flagging Note*")
8553 (erase-buffer)
8554 (insert note)
8555 (goto-char (point-min))
8556 (while (re-search-forward "\\\\n" nil t)
8557 (replace-match "\n" t t))
8558 (goto-char (point-min))
8559 (select-window win)
8560 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8562 (defun org-agenda-remove-flag (marker)
8563 "Remove the FLAGGED tag and any flagging note in the entry."
8564 (let (newhead)
8565 (org-with-point-at marker
8566 (org-toggle-tag "FLAGGED" 'off)
8567 (org-entry-delete nil "THEFLAGGINGNOTE")
8568 (setq newhead (org-get-heading)))
8569 (org-agenda-change-all-lines newhead marker)
8570 (message "Entry unflagged")))
8572 (defun org-agenda-get-any-marker (&optional pos)
8573 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8574 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8576 ;;; Appointment reminders
8578 (defvar appt-time-msg-list)
8580 ;;;###autoload
8581 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8582 "Activate appointments found in `org-agenda-files'.
8583 With a \\[universal-argument] prefix, refresh the list of
8584 appointments.
8586 If FILTER is t, interactively prompt the user for a regular
8587 expression, and filter out entries that don't match it.
8589 If FILTER is a string, use this string as a regular expression
8590 for filtering entries out.
8592 If FILTER is a function, filter out entries against which
8593 calling the function returns nil. This function takes one
8594 argument: an entry from `org-agenda-get-day-entries'.
8596 FILTER can also be an alist with the car of each cell being
8597 either 'headline or 'category. For example:
8599 '((headline \"IMPORTANT\")
8600 (category \"Work\"))
8602 will only add headlines containing IMPORTANT or headlines
8603 belonging to the \"Work\" category.
8605 ARGS are symbols indicating what kind of entries to consider.
8606 By default `org-agenda-to-appt' will use :deadline, :scheduled
8607 and :timestamp entries. See the docstring of `org-diary' for
8608 details and examples."
8609 (interactive "P")
8610 (if refresh (setq appt-time-msg-list nil))
8611 (if (eq filter t)
8612 (setq filter (read-from-minibuffer "Regexp filter: ")))
8613 (let* ((cnt 0) ; count added events
8614 (scope (or args '(:deadline :scheduled :timestamp)))
8615 (org-agenda-new-buffers nil)
8616 (org-deadline-warning-days 0)
8617 ;; Do not use `org-today' here because appt only takes
8618 ;; time and without date as argument, so it may pass wrong
8619 ;; information otherwise
8620 (today (org-date-to-gregorian
8621 (time-to-days (current-time))))
8622 (org-agenda-restrict nil)
8623 (files (org-agenda-files 'unrestricted)) entries file)
8624 ;; Get all entries which may contain an appt
8625 (org-prepare-agenda-buffers files)
8626 (while (setq file (pop files))
8627 (setq entries
8628 (delq nil
8629 (append entries
8630 (apply 'org-agenda-get-day-entries
8631 file today scope)))))
8632 ;; Map thru entries and find if we should filter them out
8633 (mapc
8634 (lambda(x)
8635 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8636 (cat (get-text-property 1 'org-category x))
8637 (tod (get-text-property 1 'time-of-day x))
8638 (ok (or (null filter)
8639 (and (stringp filter) (string-match filter evt))
8640 (and (functionp filter) (funcall filter x))
8641 (and (listp filter)
8642 (let ((cat-filter (cadr (assoc 'category filter)))
8643 (evt-filter (cadr (assoc 'headline filter))))
8644 (or (and (stringp cat-filter)
8645 (string-match cat-filter cat))
8646 (and (stringp evt-filter)
8647 (string-match evt-filter evt))))))))
8648 ;; FIXME: Shall we remove text-properties for the appt text?
8649 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8650 (when (and ok tod)
8651 (setq tod (concat "00" (number-to-string tod))
8652 tod (when (string-match
8653 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8654 (concat (match-string 1 tod) ":"
8655 (match-string 2 tod))))
8656 (appt-add tod evt)
8657 (setq cnt (1+ cnt))))) entries)
8658 (org-release-buffers org-agenda-new-buffers)
8659 (if (eq cnt 0)
8660 (message "No event to add")
8661 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8663 (defun org-agenda-todayp (date)
8664 "Does DATE mean today, when considering `org-extend-today-until'?"
8665 (let ((today (org-today))
8666 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8667 date)))
8668 (eq date today)))
8670 (defun org-agenda-todo-yesterday (&optional arg)
8671 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8672 (interactive "P")
8673 (let* ((hour (third (decode-time
8674 (org-current-time))))
8675 (org-extend-today-until (1+ hour)))
8676 (org-agenda-todo arg)))
8678 (provide 'org-agenda)
8680 ;;; org-agenda.el ends here