Remove the "Version" header in Org libraries (leave it in org.el).
[org-mode.git] / lisp / org-agenda.el
blobf34324457bc87d4b5fdc1b302143e6c1c8a947d7
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a conveniant way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function org-datetree-find-date-create "org-datetree"
70 (date &optional keep-restriction))
71 (declare-function org-columns-quit "org-colview" ())
72 (declare-function diary-date-display-form "diary-lib" (&optional type))
73 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
74 (declare-function org-habit-insert-consistency-graphs
75 "org-habit" (&optional line))
76 (declare-function org-is-habit-p "org-habit" (&optional pom))
77 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
78 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
79 (declare-function org-pop-to-buffer-same-window "org-compat"
80 (&optional buffer-or-name norecord label))
82 (defvar calendar-mode-map)
83 (defvar org-clock-current-task) ; defined in org-clock.el
84 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
85 (defvar org-habit-show-habits)
86 (defvar org-habit-show-habits-only-for-today)
88 ;; Defined somewhere in this file, but used before definition.
89 (defvar org-agenda-buffer-name)
90 (defvar org-agenda-overriding-header)
91 (defvar org-agenda-title-append nil)
92 (defvar entry)
93 (defvar date)
94 (defvar org-agenda-undo-list)
95 (defvar org-agenda-pending-undo-list)
96 (defvar original-date) ; dynamically scoped, calendar.el does scope this
98 (defcustom org-agenda-confirm-kill 1
99 "When set, remote killing from the agenda buffer needs confirmation.
100 When t, a confirmation is always needed. When a number N, confirmation is
101 only needed when the text to be killed contains more than N non-white lines."
102 :group 'org-agenda
103 :type '(choice
104 (const :tag "Never" nil)
105 (const :tag "Always" t)
106 (integer :tag "When more than N lines")))
108 (defcustom org-agenda-compact-blocks nil
109 "Non-nil means make the block agenda more compact.
110 This is done globally by leaving out lines like the agenda span
111 name and week number or the separator lines."
112 :group 'org-agenda
113 :type 'boolean)
115 (defcustom org-agenda-block-separator ?=
116 "The separator between blocks in the agenda.
117 If this is a string, it will be used as the separator, with a newline added.
118 If it is a character, it will be repeated to fill the window width.
119 If nil the separator is disabled. In `org-agenda-custom-commands' this
120 addresses the separator between the current and the previous block."
121 :group 'org-agenda
122 :type '(choice
123 (const :tag "Disabled" nil)
124 (character)
125 (string)))
127 (defgroup org-agenda-export nil
128 "Options concerning exporting agenda views in Org-mode."
129 :tag "Org Agenda Export"
130 :group 'org-agenda)
132 (defcustom org-agenda-with-colors t
133 "Non-nil means use colors in agenda views."
134 :group 'org-agenda-export
135 :type 'boolean)
137 (defcustom org-agenda-exporter-settings nil
138 "Alist of variable/value pairs that should be active during agenda export.
139 This is a good place to set options for ps-print and for htmlize.
140 Note that the way this is implemented, the values will be evaluated
141 before assigned to the variables. So make sure to quote values you do
142 *not* want evaluated, for example
144 (setq org-agenda-exporter-settings
145 '((ps-print-color-p 'black-white)))"
146 :group 'org-agenda-export
147 :type '(repeat
148 (list
149 (variable)
150 (sexp :tag "Value"))))
152 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
153 "Hook run in temporary buffer before writing it to an export file.
154 A useful function is `org-agenda-add-entry-text'."
155 :group 'org-agenda-export
156 :type 'hook
157 :options '(org-agenda-add-entry-text))
159 (defcustom org-agenda-add-entry-text-maxlines 0
160 "Maximum number of entry text lines to be added to agenda.
161 This is only relevant when `org-agenda-add-entry-text' is part of
162 `org-agenda-before-write-hook', which it is by default.
163 When this is 0, nothing will happen. When it is greater than 0, it
164 specifies the maximum number of lines that will be added for each entry
165 that is listed in the agenda view.
167 Note that this variable is not used during display, only when exporting
168 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
169 and `org-agenda-entry-text-maxlines'."
170 :group 'org-agenda
171 :type 'integer)
173 (defcustom org-agenda-add-entry-text-descriptive-links t
174 "Non-nil means export org-links as descriptive links in agenda added text.
175 This variable applies to the text added to the agenda when
176 `org-agenda-add-entry-text-maxlines' is larger than 0.
177 When this variable nil, the URL will (also) be shown."
178 :group 'org-agenda
179 :type 'boolean)
181 (defcustom org-agenda-export-html-style ""
182 "The style specification for exported HTML Agenda files.
183 If this variable contains a string, it will replace the default <style>
184 section as produced by `htmlize'.
185 Since there are different ways of setting style information, this variable
186 needs to contain the full HTML structure to provide a style, including the
187 surrounding HTML tags. The style specifications should include definitions
188 the fonts used by the agenda, here is an example:
190 <style type=\"text/css\">
191 p { font-weight: normal; color: gray; }
192 .org-agenda-structure {
193 font-size: 110%;
194 color: #003399;
195 font-weight: 600;
197 .org-todo {
198 color: #cc6666;
199 font-weight: bold;
201 .org-agenda-done {
202 color: #339933;
204 .org-done {
205 color: #339933;
207 .title { text-align: center; }
208 .todo, .deadline { color: red; }
209 .done { color: green; }
210 </style>
212 or, if you want to keep the style in a file,
214 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
216 As the value of this option simply gets inserted into the HTML <head> header,
217 you can \"misuse\" it to also add other text to the header. However,
218 <style>...</style> is required, if not present the variable will be ignored."
219 :group 'org-agenda-export
220 :group 'org-export-html
221 :type 'string)
223 (defcustom org-agenda-persistent-filter nil
224 "When set, keep filters from one agenda view to the next."
225 :group 'org-agenda
226 :type 'boolean)
228 (defgroup org-agenda-custom-commands nil
229 "Options concerning agenda views in Org-mode."
230 :tag "Org Agenda Custom Commands"
231 :group 'org-agenda)
233 (defconst org-sorting-choice
234 '(choice
235 (const time-up) (const time-down)
236 (const category-keep) (const category-up) (const category-down)
237 (const tag-down) (const tag-up)
238 (const priority-up) (const priority-down)
239 (const todo-state-up) (const todo-state-down)
240 (const effort-up) (const effort-down)
241 (const habit-up) (const habit-down)
242 (const alpha-up) (const alpha-down)
243 (const user-defined-up) (const user-defined-down))
244 "Sorting choices.")
246 (defconst org-agenda-custom-commands-local-options
247 `(repeat :tag "Local settings for this command. Remember to quote values"
248 (choice :tag "Setting"
249 (list :tag "Heading for this block"
250 (const org-agenda-overriding-header)
251 (string :tag "Headline"))
252 (list :tag "Files to be searched"
253 (const org-agenda-files)
254 (list
255 (const :format "" quote)
256 (repeat (file))))
257 (list :tag "Sorting strategy"
258 (const org-agenda-sorting-strategy)
259 (list
260 (const :format "" quote)
261 (repeat
262 ,org-sorting-choice)))
263 (list :tag "Prefix format"
264 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
265 (string))
266 (list :tag "Number of days in agenda"
267 (const org-agenda-span)
268 (choice (const :tag "Day" 'day)
269 (const :tag "Week" 'week)
270 (const :tag "Month" 'month)
271 (const :tag "Year" 'year)
272 (integer :tag "Custom")))
273 (list :tag "Fixed starting date"
274 (const org-agenda-start-day)
275 (string :value "2007-11-01"))
276 (list :tag "Start on day of week"
277 (const org-agenda-start-on-weekday)
278 (choice :value 1
279 (const :tag "Today" nil)
280 (integer :tag "Weekday No.")))
281 (list :tag "Include data from diary"
282 (const org-agenda-include-diary)
283 (boolean))
284 (list :tag "Deadline Warning days"
285 (const org-deadline-warning-days)
286 (integer :value 1))
287 (list :tag "Tags filter preset"
288 (const org-agenda-filter-preset)
289 (list
290 (const :format "" quote)
291 (repeat
292 (string :tag "+tag or -tag"))))
293 (list :tag "Set daily/weekly entry types"
294 (const org-agenda-entry-types)
295 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
296 (const :deadline)
297 (const :scheduled)
298 (const :timestamp)
299 (const :sexp)))
300 (list :tag "Standard skipping condition"
301 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
302 (const org-agenda-skip-function)
303 (list
304 (const :format "" quote)
305 (list
306 (choice
307 :tag "Skipping range"
308 (const :tag "Skip entry" org-agenda-skip-entry-if)
309 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
310 (repeat :inline t :tag "Conditions for skipping"
311 (choice
312 :tag "Condition type"
313 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
314 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
315 (list :tag "TODO state is" :inline t
316 (const 'todo)
317 (choice
318 (const :tag "any not-done state" 'todo)
319 (const :tag "any done state" 'done)
320 (const :tag "any state" 'any)
321 (list :tag "Keyword list"
322 (const :format "" quote)
323 (repeat (string :tag "Keyword")))))
324 (list :tag "TODO state is not" :inline t
325 (const 'nottodo)
326 (choice
327 (const :tag "any not-done state" 'todo)
328 (const :tag "any done state" 'done)
329 (const :tag "any state" 'any)
330 (list :tag "Keyword list"
331 (const :format "" quote)
332 (repeat (string :tag "Keyword")))))
333 (const :tag "scheduled" 'scheduled)
334 (const :tag "not scheduled" 'notscheduled)
335 (const :tag "deadline" 'deadline)
336 (const :tag "no deadline" 'notdeadline)
337 (const :tag "timestamp" 'timestamp)
338 (const :tag "no timestamp" 'nottimestamp))))))
339 (list :tag "Non-standard skipping condition"
340 :value (org-agenda-skip-function)
341 (const org-agenda-skip-function)
342 (sexp :tag "Function or form (quoted!)"))
343 (list :tag "Any variable"
344 (variable :tag "Variable")
345 (sexp :tag "Value (sexp)"))))
346 "Selection of examples for agenda command settings.
347 This will be spliced into the custom type of
348 `org-agenda-custom-commands'.")
351 (defcustom org-agenda-custom-commands nil
352 "Custom commands for the agenda.
353 These commands will be offered on the splash screen displayed by the
354 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
356 (key desc type match settings files)
358 key The key (one or more characters as a string) to be associated
359 with the command.
360 desc A description of the command, when omitted or nil, a default
361 description is built using MATCH.
362 type The command type, any of the following symbols:
363 agenda The daily/weekly agenda.
364 todo Entries with a specific TODO keyword, in all agenda files.
365 search Entries containing search words entry or headline.
366 tags Tags/Property/TODO match in all agenda files.
367 tags-todo Tags/P/T match in all agenda files, TODO entries only.
368 todo-tree Sparse tree of specific TODO keyword in *current* file.
369 tags-tree Sparse tree with all tags matches in *current* file.
370 occur-tree Occur sparse tree for *current* file.
371 ... A user-defined function.
372 match What to search for:
373 - a single keyword for TODO keyword searches
374 - a tags match expression for tags searches
375 - a word search expression for text searches.
376 - a regular expression for occur searches
377 For all other commands, this should be the empty string.
378 settings A list of option settings, similar to that in a let form, so like
379 this: ((opt1 val1) (opt2 val2) ...). The values will be
380 evaluated at the moment of execution, so quote them when needed.
381 files A list of files file to write the produced agenda buffer to
382 with the command `org-store-agenda-views'.
383 If a file name ends in \".html\", an HTML version of the buffer
384 is written out. If it ends in \".ps\", a postscript version is
385 produced. Otherwise, only the plain text is written to the file.
387 You can also define a set of commands, to create a composite agenda buffer.
388 In this case, an entry looks like this:
390 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
392 where
394 desc A description string to be displayed in the dispatcher menu.
395 cmd An agenda command, similar to the above. However, tree commands
396 are no allowed, but instead you can get agenda and global todo list.
397 So valid commands for a set are:
398 (agenda \"\" settings)
399 (alltodo \"\" settings)
400 (stuck \"\" settings)
401 (todo \"match\" settings files)
402 (search \"match\" settings files)
403 (tags \"match\" settings files)
404 (tags-todo \"match\" settings files)
406 Each command can carry a list of options, and another set of options can be
407 given for the whole set of commands. Individual command options take
408 precedence over the general options.
410 When using several characters as key to a command, the first characters
411 are prefix commands. For the dispatcher to display useful information, you
412 should provide a description for the prefix, like
414 (setq org-agenda-custom-commands
415 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
416 (\"hl\" tags \"+HOME+Lisa\")
417 (\"hp\" tags \"+HOME+Peter\")
418 (\"hk\" tags \"+HOME+Kim\")))"
419 :group 'org-agenda-custom-commands
420 :type `(repeat
421 (choice :value ("x" "Describe command here" tags "" nil)
422 (list :tag "Single command"
423 (string :tag "Access Key(s) ")
424 (option (string :tag "Description"))
425 (choice
426 (const :tag "Agenda" agenda)
427 (const :tag "TODO list" alltodo)
428 (const :tag "Search words" search)
429 (const :tag "Stuck projects" stuck)
430 (const :tag "Tags/Property match (all agenda files)" tags)
431 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
432 (const :tag "TODO keyword search (all agenda files)" todo)
433 (const :tag "Tags sparse tree (current buffer)" tags-tree)
434 (const :tag "TODO keyword tree (current buffer)" todo-tree)
435 (const :tag "Occur tree (current buffer)" occur-tree)
436 (sexp :tag "Other, user-defined function"))
437 (string :tag "Match (only for some commands)")
438 ,org-agenda-custom-commands-local-options
439 (option (repeat :tag "Export" (file :tag "Export to"))))
440 (list :tag "Command series, all agenda files"
441 (string :tag "Access Key(s)")
442 (string :tag "Description ")
443 (repeat :tag "Component"
444 (choice
445 (list :tag "Agenda"
446 (const :format "" agenda)
447 (const :tag "" :format "" "")
448 ,org-agenda-custom-commands-local-options)
449 (list :tag "TODO list (all keywords)"
450 (const :format "" alltodo)
451 (const :tag "" :format "" "")
452 ,org-agenda-custom-commands-local-options)
453 (list :tag "Search words"
454 (const :format "" search)
455 (string :tag "Match")
456 ,org-agenda-custom-commands-local-options)
457 (list :tag "Stuck projects"
458 (const :format "" stuck)
459 (const :tag "" :format "" "")
460 ,org-agenda-custom-commands-local-options)
461 (list :tag "Tags search"
462 (const :format "" tags)
463 (string :tag "Match")
464 ,org-agenda-custom-commands-local-options)
465 (list :tag "Tags search, TODO entries only"
466 (const :format "" tags-todo)
467 (string :tag "Match")
468 ,org-agenda-custom-commands-local-options)
469 (list :tag "TODO keyword search"
470 (const :format "" todo)
471 (string :tag "Match")
472 ,org-agenda-custom-commands-local-options)
473 (list :tag "Other, user-defined function"
474 (symbol :tag "function")
475 (string :tag "Match")
476 ,org-agenda-custom-commands-local-options)))
478 (repeat :tag "Settings for entire command set"
479 (list (variable :tag "Any variable")
480 (sexp :tag "Value")))
481 (option (repeat :tag "Export" (file :tag "Export to"))))
482 (cons :tag "Prefix key documentation"
483 (string :tag "Access Key(s)")
484 (string :tag "Description ")))))
486 (defcustom org-agenda-query-register ?o
487 "The register holding the current query string.
488 The purpose of this is that if you construct a query string interactively,
489 you can then use it to define a custom command."
490 :group 'org-agenda-custom-commands
491 :type 'character)
493 (defcustom org-stuck-projects
494 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
495 "How to identify stuck projects.
496 This is a list of four items:
497 1. A tags/todo/property matcher string that is used to identify a project.
498 See the manual for a description of tag and property searches.
499 The entire tree below a headline matched by this is considered one project.
500 2. A list of TODO keywords identifying non-stuck projects.
501 If the project subtree contains any headline with one of these todo
502 keywords, the project is considered to be not stuck. If you specify
503 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
504 3. A list of tags identifying non-stuck projects.
505 If the project subtree contains any headline with one of these tags,
506 the project is considered to be not stuck. If you specify \"*\" as
507 a tag, any tag will mark the project unstuck. Note that this is about
508 the explicit presence of a tag somewhere in the subtree, inherited
509 tags to not count here. If inherited tags make a project not stuck,
510 use \"-TAG\" in the tags part of the matcher under (1.) above.
511 4. An arbitrary regular expression matching non-stuck projects.
513 If the project turns out to be not stuck, search continues also in the
514 subtree to see if any of the subtasks have project status.
516 See also the variable `org-tags-match-list-sublevels' which applies
517 to projects matched by this search as well.
519 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
520 or `C-c a #' to produce the list."
521 :group 'org-agenda-custom-commands
522 :type '(list
523 (string :tag "Tags/TODO match to identify a project")
524 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
525 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
526 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
528 (defcustom org-agenda-filter-effort-default-operator "<"
529 "The default operator for effort estimate filtering.
530 If you select an effort estimate limit without first pressing an operator,
531 this one will be used."
532 :group 'org-agenda-custom-commands
533 :type '(choice (const :tag "less or equal" "<")
534 (const :tag "greater or equal"">")
535 (const :tag "equal" "=")))
537 (defgroup org-agenda-skip nil
538 "Options concerning skipping parts of agenda files."
539 :tag "Org Agenda Skip"
540 :group 'org-agenda)
542 (defcustom org-agenda-skip-function-global nil
543 "Function to be called at each match during agenda construction.
544 If this function returns nil, the current match should not be skipped.
545 If the function decided to skip an agenda match, is must return the
546 buffer position from which the search should be continued.
547 This may also be a Lisp form, which will be evaluated.
549 This variable will be applied to every agenda match, including
550 tags/property searches and TODO lists. So try to make the test function
551 do its checking as efficiently as possible. To implement a skipping
552 condition just for specific agenda commands, use the variable
553 `org-agenda-skip-function' which can be set in the options section
554 of custom agenda commands."
555 :group 'org-agenda-skip
556 :type 'sexp)
558 (defgroup org-agenda-daily/weekly nil
559 "Options concerning the daily/weekly agenda."
560 :tag "Org Agenda Daily/Weekly"
561 :group 'org-agenda)
562 (defgroup org-agenda-todo-list nil
563 "Options concerning the global todo list agenda view."
564 :tag "Org Agenda Todo List"
565 :group 'org-agenda)
566 (defgroup org-agenda-match-view nil
567 "Options concerning the general tags/property/todo match agenda view."
568 :tag "Org Agenda Match View"
569 :group 'org-agenda)
570 (defgroup org-agenda-search-view nil
571 "Options concerning the general tags/property/todo match agenda view."
572 :tag "Org Agenda Match View"
573 :group 'org-agenda)
575 (defvar org-agenda-archives-mode nil
576 "Non-nil means the agenda will include archived items.
577 If this is the symbol `trees', trees in the selected agenda scope
578 that are marked with the ARCHIVE tag will be included anyway. When this is
579 t, also all archive files associated with the current selection of agenda
580 files will be included.")
582 (defcustom org-agenda-skip-comment-trees t
583 "Non-nil means skip trees that start with the COMMENT keyword.
584 When nil, these trees are also scanned by agenda commands."
585 :group 'org-agenda-skip
586 :type 'boolean)
588 (defcustom org-agenda-todo-list-sublevels t
589 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
590 When nil, the sublevels of a TODO entry are not checked, resulting in
591 potentially much shorter TODO lists."
592 :group 'org-agenda-skip
593 :group 'org-agenda-todo-list
594 :type 'boolean)
596 (defcustom org-agenda-todo-ignore-with-date nil
597 "Non-nil means don't show entries with a date in the global todo list.
598 You can use this if you prefer to mark mere appointments with a TODO keyword,
599 but don't want them to show up in the TODO list.
600 When this is set, it also covers deadlines and scheduled items, the settings
601 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
602 will be ignored.
603 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
604 :group 'org-agenda-skip
605 :group 'org-agenda-todo-list
606 :type 'boolean)
608 (defcustom org-agenda-todo-ignore-timestamp nil
609 "Non-nil means don't show entries with a timestamp.
610 This applies when creating the global todo list.
611 Valid values are:
613 past Don't show entries for today or in the past.
615 future Don't show entries with a timestamp in the future.
616 The idea behind this is that if it has a future
617 timestamp, you don't want to think about it until the
618 date.
620 all Don't show any entries with a timestamp in the global todo list.
621 The idea behind this is that by setting a timestamp, you
622 have already \"taken care\" of this item.
624 This variable can also have an integer as a value. If positive (N),
625 todos with a timestamp N or more days in the future will be ignored. If
626 negative (-N), todos with a timestamp N or more days in the past will be
627 ignored. If 0, todos with a timestamp either today or in the future will
628 be ignored. For example, a value of -1 will exclude todos with a
629 timestamp in the past (yesterday or earlier), while a value of 7 will
630 exclude todos with a timestamp a week or more in the future.
632 See also `org-agenda-todo-ignore-with-date'.
633 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
634 to make his option also apply to the tags-todo list."
635 :group 'org-agenda-skip
636 :group 'org-agenda-todo-list
637 :type '(choice
638 (const :tag "Ignore future timestamp todos" future)
639 (const :tag "Ignore past or present timestamp todos" past)
640 (const :tag "Ignore all timestamp todos" all)
641 (const :tag "Show timestamp todos" nil)
642 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
644 (defcustom org-agenda-todo-ignore-scheduled nil
645 "Non-nil means, ignore some scheduled TODO items when making TODO list.
646 This applies when creating the global todo list.
647 Valid values are:
649 past Don't show entries scheduled today or in the past.
651 future Don't show entries scheduled in the future.
652 The idea behind this is that by scheduling it, you don't want to
653 think about it until the scheduled date.
655 all Don't show any scheduled entries in the global todo list.
656 The idea behind this is that by scheduling it, you have already
657 \"taken care\" of this item.
659 t Same as `all', for backward compatibility.
661 This variable can also have an integer as a value. See
662 `org-agenda-todo-ignore-timestamp' for more details.
664 See also `org-agenda-todo-ignore-with-date'.
665 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
666 to make his option also apply to the tags-todo list."
667 :group 'org-agenda-skip
668 :group 'org-agenda-todo-list
669 :type '(choice
670 (const :tag "Ignore future-scheduled todos" future)
671 (const :tag "Ignore past- or present-scheduled todos" past)
672 (const :tag "Ignore all scheduled todos" all)
673 (const :tag "Ignore all scheduled todos (compatibility)" t)
674 (const :tag "Show scheduled todos" nil)
675 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
677 (defcustom org-agenda-todo-ignore-deadlines nil
678 "Non-nil means ignore some deadlined TODO items when making TODO list.
679 There are different motivations for using different values, please think
680 carefully when configuring this variable.
682 This applies when creating the global todo list.
683 Valid values are:
685 near Don't show near deadline entries. A deadline is near when it is
686 closer than `org-deadline-warning-days' days. The idea behind this
687 is that such items will appear in the agenda anyway.
689 far Don't show TODO entries where a deadline has been defined, but
690 the deadline is not near. This is useful if you don't want to
691 use the todo list to figure out what to do now.
693 past Don't show entries with a deadline timestamp for today or in the past.
695 future Don't show entries with a deadline timestamp in the future, not even
696 when they become `near' ones. Use it with caution.
698 all Ignore all TODO entries that do have a deadline.
700 t Same as `near', for backward compatibility.
702 This variable can also have an integer as a value. See
703 `org-agenda-todo-ignore-timestamp' for more details.
705 See also `org-agenda-todo-ignore-with-date'.
706 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
707 to make his option also apply to the tags-todo list."
708 :group 'org-agenda-skip
709 :group 'org-agenda-todo-list
710 :type '(choice
711 (const :tag "Ignore near deadlines" near)
712 (const :tag "Ignore near deadlines (compatibility)" t)
713 (const :tag "Ignore far deadlines" far)
714 (const :tag "Ignore all TODOs with a deadlines" all)
715 (const :tag "Show all TODOs, even if they have a deadline" nil)
716 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
718 (defcustom org-agenda-tags-todo-honor-ignore-options nil
719 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
720 The variables
721 `org-agenda-todo-ignore-with-date',
722 `org-agenda-todo-ignore-timestamp',
723 `org-agenda-todo-ignore-scheduled',
724 `org-agenda-todo-ignore-deadlines'
725 make the global TODO list skip entries that have time stamps of certain
726 kinds. If this option is set, the same options will also apply for the
727 tags-todo search, which is the general tags/property matcher
728 restricted to unfinished TODO entries only."
729 :group 'org-agenda-skip
730 :group 'org-agenda-todo-list
731 :group 'org-agenda-match-view
732 :type 'boolean)
734 (defcustom org-agenda-skip-scheduled-if-done nil
735 "Non-nil means don't show scheduled items in agenda when they are done.
736 This is relevant for the daily/weekly agenda, not for the TODO list. And
737 it applies only to the actual date of the scheduling. Warnings about
738 an item with a past scheduling dates are always turned off when the item
739 is DONE."
740 :group 'org-agenda-skip
741 :group 'org-agenda-daily/weekly
742 :type 'boolean)
744 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
745 "Non-nil means skip scheduling line if same entry shows because of deadline.
746 In the agenda of today, an entry can show up multiple times because
747 it is both scheduled and has a nearby deadline, and maybe a plain time
748 stamp as well.
749 When this variable is t, then only the deadline is shown and the fact that
750 the entry is scheduled today or was scheduled previously is not shown.
751 When this variable is nil, the entry will be shown several times. When
752 the variable is the symbol `not-today', then skip scheduled previously,
753 but not scheduled today."
754 :group 'org-agenda-skip
755 :group 'org-agenda-daily/weekly
756 :type '(choice
757 (const :tag "Never" nil)
758 (const :tag "Always" t)
759 (const :tag "Not when scheduled today" not-today)))
761 (defcustom org-agenda-skip-deadline-if-done nil
762 "Non-nil means don't show deadlines when the corresponding item is done.
763 When nil, the deadline is still shown and should give you a happy feeling.
764 This is relevant for the daily/weekly agenda. And it applied only to the
765 actually date of the deadline. Warnings about approaching and past-due
766 deadlines are always turned off when the item is DONE."
767 :group 'org-agenda-skip
768 :group 'org-agenda-daily/weekly
769 :type 'boolean)
771 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
772 "Non-nil means skip deadline prewarning when entry is also scheduled.
773 This will apply on all days where a prewarning for the deadline would
774 be shown, but not at the day when the entry is actually due. On that day,
775 the deadline will be shown anyway.
776 This variable may be set to nil, t, or a number which will then give
777 the number of days before the actual deadline when the prewarnings
778 should resume.
779 This can be used in a workflow where the first showing of the deadline will
780 trigger you to schedule it, and then you don't want to be reminded of it
781 because you will take care of it on the day when scheduled."
782 :group 'org-agenda-skip
783 :group 'org-agenda-daily/weekly
784 :type '(choice
785 (const :tag "Alwas show prewarning" nil)
786 (const :tag "Remove prewarning if entry is scheduled" t)
787 (integer :tag "Restart prewarning N days before deadline")))
789 (defcustom org-agenda-skip-additional-timestamps-same-entry t
790 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
791 When non-nil, after the search for timestamps has matched once in an
792 entry, the rest of the entry will not be searched."
793 :group 'org-agenda-skip
794 :type 'boolean)
796 (defcustom org-agenda-skip-timestamp-if-done nil
797 "Non-nil means don't select item by timestamp or -range if it is DONE."
798 :group 'org-agenda-skip
799 :group 'org-agenda-daily/weekly
800 :type 'boolean)
802 (defcustom org-agenda-dim-blocked-tasks t
803 "Non-nil means dim blocked tasks in the agenda display.
804 This causes some overhead during agenda construction, but if you
805 have turned on `org-enforce-todo-dependencies',
806 `org-enforce-todo-checkbox-dependencies', or any other blocking
807 mechanism, this will create useful feedback in the agenda.
809 Instead of t, this variable can also have the value `invisible'.
810 Then blocked tasks will be invisible and only become visible when
811 they become unblocked. An exemption to this behavior is when a task is
812 blocked because of unchecked checkboxes below it. Since checkboxes do
813 not show up in the agenda views, making this task invisible you remove any
814 trace from agenda views that there is something to do. Therefore, a task
815 that is blocked because of checkboxes will never be made invisible, it
816 will only be dimmed."
817 :group 'org-agenda-daily/weekly
818 :group 'org-agenda-todo-list
819 :type '(choice
820 (const :tag "Do not dim" nil)
821 (const :tag "Dim to a grey face" t)
822 (const :tag "Make invisible" invisible)))
824 (defcustom org-timeline-show-empty-dates 3
825 "Non-nil means `org-timeline' also shows dates without an entry.
826 When nil, only the days which actually have entries are shown.
827 When t, all days between the first and the last date are shown.
828 When an integer, show also empty dates, but if there is a gap of more than
829 N days, just insert a special line indicating the size of the gap."
830 :group 'org-agenda-skip
831 :type '(choice
832 (const :tag "None" nil)
833 (const :tag "All" t)
834 (integer :tag "at most")))
836 (defgroup org-agenda-startup nil
837 "Options concerning initial settings in the Agenda in Org Mode."
838 :tag "Org Agenda Startup"
839 :group 'org-agenda)
841 (defcustom org-agenda-menu-show-matcher t
842 "Non-nil means show the match string in the agenda dispatcher menu.
843 When nil, the matcher string is not shown, but is put into the help-echo
844 property so than moving the mouse over the command shows it.
845 Setting it to nil is good if matcher strings are very long and/or if
846 you want to use two-column display (see `org-agenda-menu-two-column')."
847 :group 'org-agenda
848 :type 'boolean)
850 (defcustom org-agenda-menu-two-column nil
851 "Non-nil means, use two columns to show custom commands in the dispatcher.
852 If you use this, you probably want to set `org-agenda-menu-show-matcher'
853 to nil."
854 :group 'org-agenda
855 :type 'boolean)
857 (defcustom org-finalize-agenda-hook nil
858 "Hook run just before displaying an agenda buffer."
859 :group 'org-agenda-startup
860 :type 'hook)
862 (defcustom org-agenda-mouse-1-follows-link nil
863 "Non-nil means mouse-1 on a link will follow the link in the agenda.
864 A longer mouse click will still set point. Does not work on XEmacs.
865 Needs to be set before org.el is loaded."
866 :group 'org-agenda-startup
867 :type 'boolean)
869 (defcustom org-agenda-start-with-follow-mode nil
870 "The initial value of follow mode in a newly created agenda window."
871 :group 'org-agenda-startup
872 :type 'boolean)
874 (defcustom org-agenda-show-outline-path t
875 "Non-nil means show outline path in echo area after line motion."
876 :group 'org-agenda-startup
877 :type 'boolean)
879 (defcustom org-agenda-start-with-entry-text-mode nil
880 "The initial value of entry-text-mode in a newly created agenda window."
881 :group 'org-agenda-startup
882 :type 'boolean)
884 (defcustom org-agenda-entry-text-maxlines 5
885 "Number of text lines to be added when `E' is pressed in the agenda.
887 Note that this variable only used during agenda display. Add add entry text
888 when exporting the agenda, configure the variable
889 `org-agenda-add-entry-ext-maxlines'."
890 :group 'org-agenda
891 :type 'integer)
893 (defcustom org-agenda-entry-text-exclude-regexps nil
894 "List of regular expressions to clean up entry text.
895 The complete matches of all regular expressions in this list will be
896 removed from entry text before it is shown in the agenda."
897 :group 'org-agenda
898 :type '(repeat (regexp)))
900 (defvar org-agenda-entry-text-cleanup-hook nil
901 "Hook that is run after basic cleanup of entry text to be shown in agenda.
902 This cleanup is done in a temporary buffer, so the function may inspect and
903 change the entire buffer.
904 Some default stuff like drawers and scheduling/deadline dates will already
905 have been removed when this is called, as will any matches for regular
906 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
908 (defvar org-agenda-include-inactive-timestamps nil
909 "Non-nil means include inactive time stamps in agenda and timeline.")
911 (defgroup org-agenda-windows nil
912 "Options concerning the windows used by the Agenda in Org Mode."
913 :tag "Org Agenda Windows"
914 :group 'org-agenda)
916 (defcustom org-agenda-window-setup 'reorganize-frame
917 "How the agenda buffer should be displayed.
918 Possible values for this option are:
920 current-window Show agenda in the current window, keeping all other windows.
921 other-window Use `switch-to-buffer-other-window' to display agenda.
922 reorganize-frame Show only two windows on the current frame, the current
923 window and the agenda.
924 other-frame Use `switch-to-buffer-other-frame' to display agenda.
925 Also, when exiting the agenda, kill that frame.
926 See also the variable `org-agenda-restore-windows-after-quit'."
927 :group 'org-agenda-windows
928 :type '(choice
929 (const current-window)
930 (const other-frame)
931 (const other-window)
932 (const reorganize-frame)))
934 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
935 "The min and max height of the agenda window as a fraction of frame height.
936 The value of the variable is a cons cell with two numbers between 0 and 1.
937 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
938 :group 'org-agenda-windows
939 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
941 (defcustom org-agenda-restore-windows-after-quit nil
942 "Non-nil means restore window configuration upon exiting agenda.
943 Before the window configuration is changed for displaying the agenda,
944 the current status is recorded. When the agenda is exited with
945 `q' or `x' and this option is set, the old state is restored. If
946 `org-agenda-window-setup' is `other-frame', the value of this
947 option will be ignored."
948 :group 'org-agenda-windows
949 :type 'boolean)
951 (defcustom org-agenda-ndays nil
952 "Number of days to include in overview display.
953 Should be 1 or 7.
954 Obsolete, see `org-agenda-span'."
955 :group 'org-agenda-daily/weekly
956 :type 'integer)
958 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
960 (defcustom org-agenda-span 'week
961 "Number of days to include in overview display.
962 Can be day, week, month, year, or any number of days.
963 Custom commands can set this variable in the options section."
964 :group 'org-agenda-daily/weekly
965 :type '(choice (const :tag "Day" day)
966 (const :tag "Week" week)
967 (const :tag "Month" month)
968 (const :tag "Year" year)
969 (integer :tag "Custom")))
971 (defcustom org-agenda-start-on-weekday 1
972 "Non-nil means start the overview always on the specified weekday.
973 0 denotes Sunday, 1 denotes Monday etc.
974 When nil, always start on the current day.
975 Custom commands can set this variable in the options section."
976 :group 'org-agenda-daily/weekly
977 :type '(choice (const :tag "Today" nil)
978 (integer :tag "Weekday No.")))
980 (defcustom org-agenda-show-all-dates t
981 "Non-nil means `org-agenda' shows every day in the selected range.
982 When nil, only the days which actually have entries are shown."
983 :group 'org-agenda-daily/weekly
984 :type 'boolean)
986 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
987 "Format string for displaying dates in the agenda.
988 Used by the daily/weekly agenda and by the timeline. This should be
989 a format string understood by `format-time-string', or a function returning
990 the formatted date as a string. The function must take a single argument,
991 a calendar-style date list like (month day year)."
992 :group 'org-agenda-daily/weekly
993 :type '(choice
994 (string :tag "Format string")
995 (function :tag "Function")))
997 (defun org-agenda-format-date-aligned (date)
998 "Format a date string for display in the daily/weekly agenda, or timeline.
999 This function makes sure that dates are aligned for easy reading."
1000 (require 'cal-iso)
1001 (let* ((dayname (calendar-day-name date))
1002 (day (cadr date))
1003 (day-of-week (calendar-day-of-week date))
1004 (month (car date))
1005 (monthname (calendar-month-name month))
1006 (year (nth 2 date))
1007 (iso-week (org-days-to-iso-week
1008 (calendar-absolute-from-gregorian date)))
1009 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1010 (1- year))
1011 ((and (= month 12) (<= iso-week 1))
1012 (1+ year))
1013 (t year)))
1014 (weekstring (if (= day-of-week 1)
1015 (format " W%02d" iso-week)
1016 "")))
1017 (format "%-10s %2d %s %4d%s"
1018 dayname day monthname year weekstring)))
1020 (defcustom org-agenda-time-leading-zero nil
1021 "Non-nil means use leading zero for military times in agenda.
1022 For example, 9:30am would become 09:30 rather than 9:30."
1023 :group 'org-agenda-daily/weekly
1024 :type 'boolean)
1026 (defcustom org-agenda-timegrid-use-ampm nil
1027 "When set, show AM/PM style timestamps on the timegrid."
1028 :group 'org-agenda
1029 :type 'boolean)
1031 (defun org-agenda-time-of-day-to-ampm (time)
1032 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1033 (let* ((hour-number (string-to-number (substring time 0 -3)))
1034 (minute (substring time -2))
1035 (ampm "am"))
1036 (cond
1037 ((equal hour-number 12)
1038 (setq ampm "pm"))
1039 ((> hour-number 12)
1040 (setq ampm "pm")
1041 (setq hour-number (- hour-number 12))))
1042 (concat
1043 (if org-agenda-time-leading-zero
1044 (format "%02d" hour-number)
1045 (format "%02s" (number-to-string hour-number)))
1046 ":" minute ampm)))
1048 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1049 "Conditionally convert TIME to AM/PM format
1050 based on `org-agenda-timegrid-use-ampm'"
1051 (if org-agenda-timegrid-use-ampm
1052 (org-agenda-time-of-day-to-ampm time)
1053 time))
1055 (defcustom org-agenda-weekend-days '(6 0)
1056 "Which days are weekend?
1057 These days get the special face `org-agenda-date-weekend' in the agenda
1058 and timeline buffers."
1059 :group 'org-agenda-daily/weekly
1060 :type '(set :greedy t
1061 (const :tag "Monday" 1)
1062 (const :tag "Tuesday" 2)
1063 (const :tag "Wednesday" 3)
1064 (const :tag "Thursday" 4)
1065 (const :tag "Friday" 5)
1066 (const :tag "Saturday" 6)
1067 (const :tag "Sunday" 0)))
1069 (defcustom org-agenda-include-diary nil
1070 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1071 Custom commands can set this variable in the options section."
1072 :group 'org-agenda-daily/weekly
1073 :type 'boolean)
1075 (defcustom org-agenda-include-deadlines t
1076 "If non-nil, include entries within their deadline warning period.
1077 Custom commands can set this variable in the options section."
1078 :group 'org-agenda-daily/weekly
1079 :type 'boolean)
1081 (defcustom org-agenda-include-all-todo nil
1082 "Set means weekly/daily agenda will always contain all TODO entries.
1083 The TODO entries will be listed at the top of the agenda, before
1084 the entries for specific days.
1085 This option is deprecated, it is better to define a block agenda instead."
1086 :group 'org-agenda-daily/weekly
1087 :type 'boolean)
1089 (defcustom org-agenda-repeating-timestamp-show-all t
1090 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1091 When set to a list of strings, only show occurrences of repeating
1092 stamps for these TODO keywords. When nil, only one occurrence is
1093 shown, either today or the nearest into the future."
1094 :group 'org-agenda-daily/weekly
1095 :type '(choice
1096 (const :tag "Show repeating stamps" t)
1097 (repeat :tag "Show repeating stamps for these TODO keywords"
1098 (string :tag "TODO Keyword"))
1099 (const :tag "Don't show repeating stamps" nil)))
1101 (defcustom org-scheduled-past-days 10000
1102 "No. of days to continue listing scheduled items that are not marked DONE.
1103 When an item is scheduled on a date, it shows up in the agenda on this
1104 day and will be listed until it is marked done for the number of days
1105 given here."
1106 :group 'org-agenda-daily/weekly
1107 :type 'integer)
1109 (defcustom org-agenda-log-mode-items '(closed clock)
1110 "List of items that should be shown in agenda log mode.
1111 This list may contain the following symbols:
1113 closed Show entries that have been closed on that day.
1114 clock Show entries that have received clocked time on that day.
1115 state Show all logged state changes.
1116 Note that instead of changing this variable, you can also press `C-u l' in
1117 the agenda to display all available LOG items temporarily."
1118 :group 'org-agenda-daily/weekly
1119 :type '(set :greedy t (const closed) (const clock) (const state)))
1121 (defcustom org-agenda-clock-consistency-checks
1122 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1123 :gap-ok-around ("4:00")
1124 :default-face ((:background "DarkRed") (:foreground "white"))
1125 :overlap-face nil :gap-face nil :no-end-time-face nil
1126 :long-face nil :short-face nil)
1127 "This is a property list, with the following keys:
1129 :max-duration Mark clocking chunks that are longer than this time.
1130 This is a time string like \"HH:MM\", or the number
1131 of minutes as an integer.
1133 :min-duration Mark clocking chunks that are shorter that this.
1134 This is a time string like \"HH:MM\", or the number
1135 of minutes as an integer.
1137 :max-gap Mark gaps between clocking chunks that are longer than
1138 this duration. A number of minutes, or a string
1139 like \"HH:MM\".
1141 :gap-ok-around List of times during the day which are usually not working
1142 times. When a gap is detected, but the gap contains any
1143 of these times, the gap is *not* reported. For example,
1144 if this is (\"4:00\" \"13:00\") then gaps that contain
1145 4:00 in the morning (i.e. the night) and 13:00
1146 (i.e. a typical lunch time) do not cause a warning.
1147 You should have at least one time during the night in this
1148 list, or otherwise the first task each morning will trigger
1149 a warning because it follows a long gap.
1151 Furthermore, the following properties can be used to define faces for
1152 issue display.
1154 :default-face the default face, if the specific face is undefined
1155 :overlap-face face for overlapping clocks
1156 :gap-face face for gaps between clocks
1157 :no-end-time-face face for incomplete clocks
1158 :long-face face for clock intervals that are too long
1159 :short-face face for clock intervals that are too short"
1160 :group 'org-agenda-daily/weekly
1161 :group 'org-clock
1162 :type 'plist)
1164 (defcustom org-agenda-log-mode-add-notes t
1165 "Non-nil means add first line of notes to log entries in agenda views.
1166 If a log item like a state change or a clock entry is associated with
1167 notes, the first line of these notes will be added to the entry in the
1168 agenda display."
1169 :group 'org-agenda-daily/weekly
1170 :type 'boolean)
1172 (defcustom org-agenda-start-with-log-mode nil
1173 "The initial value of log-mode in a newly created agenda window."
1174 :group 'org-agenda-startup
1175 :group 'org-agenda-daily/weekly
1176 :type 'boolean)
1178 (defcustom org-agenda-start-with-clockreport-mode nil
1179 "The initial value of clockreport-mode in a newly created agenda window."
1180 :group 'org-agenda-startup
1181 :group 'org-agenda-daily/weekly
1182 :type 'boolean)
1184 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1185 "Property list with parameters for the clocktable in clockreport mode.
1186 This is the display mode that shows a clock table in the daily/weekly
1187 agenda, the properties for this dynamic block can be set here.
1188 The usual clocktable parameters are allowed here, but you cannot set
1189 the properties :name, :tstart, :tend, :block, and :scope - these will
1190 be overwritten to make sure the content accurately reflects the
1191 current display in the agenda."
1192 :group 'org-agenda-daily/weekly
1193 :type 'plist)
1195 (defcustom org-agenda-search-view-always-boolean nil
1196 "Non-nil means the search string is interpreted as individual parts.
1198 The search string for search view can either be interpreted as a phrase,
1199 or as a list of snippets that define a boolean search for a number of
1200 strings.
1202 When this is non-nil, the string will be split on whitespace, and each
1203 snippet will be searched individually, and all must match in order to
1204 select an entry. A snippet is then a single string of non-white
1205 characters, or a string in double quotes, or a regexp in {} braces.
1206 If a snippet is preceded by \"-\", the snippet must *not* match.
1207 \"+\" is syntactic sugar for positive selection. Each snippet may
1208 be found as a full word or a partial word, but see the variable
1209 `org-agenda-search-view-force-full-words'.
1211 When this is nil, search will look for the entire search phrase as one,
1212 with each space character matching any amount of whitespace, including
1213 line breaks.
1215 Even when this is nil, you can still switch to Boolean search dynamically
1216 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1217 is a regexp marked with braces like \"{abc}\", this will also switch to
1218 boolean search."
1219 :group 'org-agenda-search-view
1220 :type 'boolean)
1222 (if (fboundp 'defvaralias)
1223 (defvaralias 'org-agenda-search-view-search-words-only
1224 'org-agenda-search-view-always-boolean))
1226 (defcustom org-agenda-search-view-force-full-words nil
1227 "Non-nil means, search words must be matches as complete words.
1228 When nil, they may also match part of a word."
1229 :group 'org-agenda-search-view
1230 :type 'boolean)
1232 (defgroup org-agenda-time-grid nil
1233 "Options concerning the time grid in the Org-mode Agenda."
1234 :tag "Org Agenda Time Grid"
1235 :group 'org-agenda)
1237 (defcustom org-agenda-search-headline-for-time t
1238 "Non-nil means search headline for a time-of-day.
1239 If the headline contains a time-of-day in one format or another, it will
1240 be used to sort the entry into the time sequence of items for a day.
1241 Some people have time stamps in the headline that refer to the creation
1242 time or so, and then this produces an unwanted side effect. If this is
1243 the case for your, use this variable to turn off searching the headline
1244 for a time."
1245 :group 'org-agenda-time-grid
1246 :type 'boolean)
1248 (defcustom org-agenda-use-time-grid t
1249 "Non-nil means show a time grid in the agenda schedule.
1250 A time grid is a set of lines for specific times (like every two hours between
1251 8:00 and 20:00). The items scheduled for a day at specific times are
1252 sorted in between these lines.
1253 For details about when the grid will be shown, and what it will look like, see
1254 the variable `org-agenda-time-grid'."
1255 :group 'org-agenda-time-grid
1256 :type 'boolean)
1258 (defcustom org-agenda-time-grid
1259 '((daily today require-timed)
1260 "----------------"
1261 (800 1000 1200 1400 1600 1800 2000))
1263 "The settings for time grid for agenda display.
1264 This is a list of three items. The first item is again a list. It contains
1265 symbols specifying conditions when the grid should be displayed:
1267 daily if the agenda shows a single day
1268 weekly if the agenda shows an entire week
1269 today show grid on current date, independent of daily/weekly display
1270 require-timed show grid only if at least one item has a time specification
1272 The second item is a string which will be placed behind the grid time.
1274 The third item is a list of integers, indicating the times that should have
1275 a grid line."
1276 :group 'org-agenda-time-grid
1277 :type
1278 '(list
1279 (set :greedy t :tag "Grid Display Options"
1280 (const :tag "Show grid in single day agenda display" daily)
1281 (const :tag "Show grid in weekly agenda display" weekly)
1282 (const :tag "Always show grid for today" today)
1283 (const :tag "Show grid only if any timed entries are present"
1284 require-timed)
1285 (const :tag "Skip grid times already present in an entry"
1286 remove-match))
1287 (string :tag "Grid String")
1288 (repeat :tag "Grid Times" (integer :tag "Time"))))
1290 (defcustom org-agenda-show-current-time-in-grid t
1291 "Non-nil means show the current time in the time grid."
1292 :group 'org-agenda-time-grid
1293 :type 'boolean)
1295 (defcustom org-agenda-current-time-string
1296 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1297 "The string for the current time marker in the agenda."
1298 :group 'org-agenda-time-grid
1299 :type 'string)
1301 (defgroup org-agenda-sorting nil
1302 "Options concerning sorting in the Org-mode Agenda."
1303 :tag "Org Agenda Sorting"
1304 :group 'org-agenda)
1306 (defcustom org-agenda-sorting-strategy
1307 '((agenda habit-down time-up priority-down category-keep)
1308 (todo priority-down category-keep)
1309 (tags priority-down category-keep)
1310 (search category-keep))
1311 "Sorting structure for the agenda items of a single day.
1312 This is a list of symbols which will be used in sequence to determine
1313 if an entry should be listed before another entry. The following
1314 symbols are recognized:
1316 time-up Put entries with time-of-day indications first, early first
1317 time-down Put entries with time-of-day indications first, late first
1318 category-keep Keep the default order of categories, corresponding to the
1319 sequence in `org-agenda-files'.
1320 category-up Sort alphabetically by category, A-Z.
1321 category-down Sort alphabetically by category, Z-A.
1322 tag-up Sort alphabetically by last tag, A-Z.
1323 tag-down Sort alphabetically by last tag, Z-A.
1324 priority-up Sort numerically by priority, high priority last.
1325 priority-down Sort numerically by priority, high priority first.
1326 todo-state-up Sort by todo state, tasks that are done last.
1327 todo-state-down Sort by todo state, tasks that are done first.
1328 effort-up Sort numerically by estimated effort, high effort last.
1329 effort-down Sort numerically by estimated effort, high effort first.
1330 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1331 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1332 habit-up Put entries that are habits first
1333 habit-down Put entries that are habits last
1334 alpha-up Sort headlines alphabetically
1335 alpha-down Sort headlines alphabetically, reversed
1337 The different possibilities will be tried in sequence, and testing stops
1338 if one comparison returns a \"not-equal\". For example, the default
1339 '(time-up category-keep priority-down)
1340 means: Pull out all entries having a specified time of day and sort them,
1341 in order to make a time schedule for the current day the first thing in the
1342 agenda listing for the day. Of the entries without a time indication, keep
1343 the grouped in categories, don't sort the categories, but keep them in
1344 the sequence given in `org-agenda-files'. Within each category sort by
1345 priority.
1347 Leaving out `category-keep' would mean that items will be sorted across
1348 categories by priority.
1350 Instead of a single list, this can also be a set of list for specific
1351 contents, with a context symbol in the car of the list, any of
1352 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1354 Custom commands can bind this variable in the options section."
1355 :group 'org-agenda-sorting
1356 :type `(choice
1357 (repeat :tag "General" ,org-sorting-choice)
1358 (list :tag "Individually"
1359 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1360 (repeat ,org-sorting-choice))
1361 (cons (const :tag "Strategy for TODO lists" todo)
1362 (repeat ,org-sorting-choice))
1363 (cons (const :tag "Strategy for Tags matches" tags)
1364 (repeat ,org-sorting-choice))
1365 (cons (const :tag "Strategy for search matches" search)
1366 (repeat ,org-sorting-choice)))))
1368 (defcustom org-agenda-cmp-user-defined nil
1369 "A function to define the comparison `user-defined'.
1370 This function must receive two arguments, agenda entry a and b.
1371 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1372 the user comparison, return nil.
1373 When this is defined, you can make `user-defined-up' and `user-defined-down'
1374 part of an agenda sorting strategy."
1375 :group 'org-agenda-sorting
1376 :type 'symbol)
1378 (defcustom org-sort-agenda-notime-is-late t
1379 "Non-nil means items without time are considered late.
1380 This is only relevant for sorting. When t, items which have no explicit
1381 time like 15:30 will be considered as 99:01, i.e. later than any items which
1382 do have a time. When nil, the default time is before 0:00. You can use this
1383 option to decide if the schedule for today should come before or after timeless
1384 agenda entries."
1385 :group 'org-agenda-sorting
1386 :type 'boolean)
1388 (defcustom org-sort-agenda-noeffort-is-high t
1389 "Non-nil means items without effort estimate are sorted as high effort.
1390 This also applies when filtering an agenda view with respect to the
1391 < or > effort operator. Then, tasks with no effort defined will be treated
1392 as tasks with high effort.
1393 When nil, such items are sorted as 0 minutes effort."
1394 :group 'org-agenda-sorting
1395 :type 'boolean)
1397 (defgroup org-agenda-line-format nil
1398 "Options concerning the entry prefix in the Org-mode agenda display."
1399 :tag "Org Agenda Line Format"
1400 :group 'org-agenda)
1402 (defcustom org-agenda-prefix-format
1403 '((agenda . " %i %-12:c%?-12t% s")
1404 (timeline . " % s")
1405 (todo . " %i %-12:c")
1406 (tags . " %i %-12:c")
1407 (search . " %i %-12:c"))
1408 "Format specifications for the prefix of items in the agenda views.
1409 An alist with five entries, each for the different agenda types. The
1410 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1411 The values are format strings.
1413 This format works similar to a printf format, with the following meaning:
1415 %c the category of the item, \"Diary\" for entries from the diary,
1416 or as given by the CATEGORY keyword or derived from the file name
1417 %i the icon category of the item, see `org-agenda-category-icon-alist'
1418 %T the last tag of the item (ignore inherited tags, which come first)
1419 %t the HH:MM time-of-day specification if one applies to the entry
1420 %s Scheduling/Deadline information, a short string
1421 %(expression) Eval EXPRESSION and replace the control string
1422 by the result
1424 All specifiers work basically like the standard `%s' of printf, but may
1425 contain two additional characters: a question mark just after the `%'
1426 and a whitespace/punctuation character just before the final letter.
1428 If the first character after `%' is a question mark, the entire field
1429 will only be included if the corresponding value applies to the current
1430 entry. This is useful for fields which should have fixed width when
1431 present, but zero width when absent. For example, \"%?-12t\" will
1432 result in a 12 character time field if a time of the day is specified,
1433 but will completely disappear in entries which do not contain a time.
1435 If there is punctuation or whitespace character just before the final
1436 format letter, this character will be appended to the field value if
1437 the value is not empty. For example, the format \"%-12:c\" leads to
1438 \"Diary: \" if the category is \"Diary\". If the category were be
1439 empty, no additional colon would be inserted.
1441 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1442 which means:
1444 - Indent the line with two space characters
1445 - Give the category a 12 chars wide field, padded with whitespace on
1446 the right (because of `-'). Append a colon if there is a category
1447 (because of `:').
1448 - If there is a time-of-day, put it into a 12 chars wide field. If no
1449 time, don't put in an empty field, just skip it (because of '?').
1450 - Finally, put the scheduling information.
1452 See also the variables `org-agenda-remove-times-when-in-prefix' and
1453 `org-agenda-remove-tags'.
1455 Custom commands can set this variable in the options section."
1456 :type '(choice
1457 (string :tag "General format")
1458 (list :greedy t :tag "View dependent"
1459 (cons (const agenda) (string :tag "Format"))
1460 (cons (const timeline) (string :tag "Format"))
1461 (cons (const todo) (string :tag "Format"))
1462 (cons (const tags) (string :tag "Format"))
1463 (cons (const search) (string :tag "Format"))))
1464 :group 'org-agenda-line-format)
1466 (defvar org-prefix-format-compiled nil
1467 "The compiled version of the most recently used prefix format.
1468 See the variable `org-agenda-prefix-format'.")
1470 (defcustom org-agenda-todo-keyword-format "%-1s"
1471 "Format for the TODO keyword in agenda lines.
1472 Set this to something like \"%-12s\" if you want all TODO keywords
1473 to occupy a fixed space in the agenda display."
1474 :group 'org-agenda-line-format
1475 :type 'string)
1477 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1478 "Text preceding timerange entries in the agenda view.
1479 This is a list with two strings. The first applies when the range
1480 is entirely on one day. The second applies if the range spans several days.
1481 The strings may have two \"%d\" format specifiers which will be filled
1482 with the sequence number of the days, and the total number of days in the
1483 range, respectively."
1484 :group 'org-agenda-line-format
1485 :type '(list
1486 (string :tag "Deadline today ")
1487 (choice :tag "Deadline relative"
1488 (string :tag "Format string")
1489 (function))))
1491 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1492 "Text preceding scheduled items in the agenda view.
1493 This is a list with two strings. The first applies when the item is
1494 scheduled on the current day. The second applies when it has been scheduled
1495 previously, it may contain a %d indicating that this is the nth time that
1496 this item is scheduled, due to automatic rescheduling of unfinished items
1497 for the following day. So this number is one larger than the number of days
1498 that passed since this item was scheduled first."
1499 :group 'org-agenda-line-format
1500 :type '(list
1501 (string :tag "Scheduled today ")
1502 (string :tag "Scheduled previously")))
1504 (defcustom org-agenda-inactive-leader "["
1505 "Text preceding item pulled into the agenda by inactive time stamps.
1506 These entries are added to the agenda when pressing \"[\"."
1507 :group 'org-agenda-line-format
1508 :type '(list
1509 (string :tag "Scheduled today ")
1510 (string :tag "Scheduled previously")))
1512 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1513 "Text preceding deadline items in the agenda view.
1514 This is a list with two strings. The first applies when the item has its
1515 deadline on the current day. The second applies when it is in the past or
1516 in the future, it may contain %d to capture how many days away the deadline
1517 is (was)."
1518 :group 'org-agenda-line-format
1519 :type '(list
1520 (string :tag "Deadline today ")
1521 (choice :tag "Deadline relative"
1522 (string :tag "Format string")
1523 (function))))
1525 (defcustom org-agenda-remove-times-when-in-prefix t
1526 "Non-nil means remove duplicate time specifications in agenda items.
1527 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1528 time-of-day specification in a headline or diary entry is extracted and
1529 placed into the prefix. If this option is non-nil, the original specification
1530 \(a timestamp or -range, or just a plain time(range) specification like
1531 11:30-4pm) will be removed for agenda display. This makes the agenda less
1532 cluttered.
1533 The option can be t or nil. It may also be the symbol `beg', indicating
1534 that the time should only be removed when it is located at the beginning of
1535 the headline/diary entry."
1536 :group 'org-agenda-line-format
1537 :type '(choice
1538 (const :tag "Always" t)
1539 (const :tag "Never" nil)
1540 (const :tag "When at beginning of entry" beg)))
1542 (defcustom org-agenda-remove-timeranges-from-blocks nil
1543 "Non-nil means remove time ranges specifications in agenda
1544 items that span on several days."
1545 :group 'org-agenda-line-format
1546 :type 'boolean)
1548 (defcustom org-agenda-default-appointment-duration nil
1549 "Default duration for appointments that only have a starting time.
1550 When nil, no duration is specified in such cases.
1551 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1552 :group 'org-agenda-line-format
1553 :type '(choice
1554 (integer :tag "Minutes")
1555 (const :tag "No default duration")))
1557 (defcustom org-agenda-show-inherited-tags t
1558 "Non-nil means show inherited tags in each agenda line."
1559 :group 'org-agenda-line-format
1560 :type 'boolean)
1562 (defcustom org-agenda-hide-tags-regexp nil
1563 "Regular expression used to filter away specific tags in agenda views.
1564 This means that these tags will be present, but not be shown in the agenda
1565 line. Secondary filtering will still work on the hidden tags.
1566 Nil means don't hide any tags."
1567 :group 'org-agenda-line-format
1568 :type '(choice
1569 (const :tag "Hide none" nil)
1570 (string :tag "Regexp ")))
1572 (defcustom org-agenda-remove-tags nil
1573 "Non-nil means remove the tags from the headline copy in the agenda.
1574 When this is the symbol `prefix', only remove tags when
1575 `org-agenda-prefix-format' contains a `%T' specifier."
1576 :group 'org-agenda-line-format
1577 :type '(choice
1578 (const :tag "Always" t)
1579 (const :tag "Never" nil)
1580 (const :tag "When prefix format contains %T" prefix)))
1582 (if (fboundp 'defvaralias)
1583 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1584 'org-agenda-remove-tags))
1586 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1587 "Shift tags in agenda items to this column.
1588 If this number is positive, it specifies the column. If it is negative,
1589 it means that the tags should be flushright to that column. For example,
1590 -80 works well for a normal 80 character screen."
1591 :group 'org-agenda-line-format
1592 :type 'integer)
1594 (if (fboundp 'defvaralias)
1595 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1597 (defcustom org-agenda-fontify-priorities 'cookies
1598 "Non-nil means highlight low and high priorities in agenda.
1599 When t, the highest priority entries are bold, lowest priority italic.
1600 However, settings in `org-priority-faces' will overrule these faces.
1601 When this variable is the symbol `cookies', only fontify the
1602 cookies, not the entire task.
1603 This may also be an association list of priority faces, whose
1604 keys are the character values of `org-highest-priority',
1605 `org-default-priority', and `org-lowest-priority' (the default values
1606 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1607 color as a string, or a list like `(:background \"Red\")'.
1608 If it is a color, the variable `org-faces-easy-properties'
1609 determines if it is a foreground or a background color."
1610 :group 'org-agenda-line-format
1611 :type '(choice
1612 (const :tag "Never" nil)
1613 (const :tag "Defaults" t)
1614 (const :tag "Cookies only" cookies)
1615 (repeat :tag "Specify"
1616 (list (character :tag "Priority" :value ?A)
1617 (choice :tag "Face "
1618 (string :tag "Color")
1619 (sexp :tag "Face"))))))
1621 (defcustom org-agenda-day-face-function nil
1622 "Function called to determine what face should be used to display a day.
1623 The only argument passed to that function is the day. It should
1624 returns a face, or nil if does not want to specify a face and let
1625 the normal rules apply."
1626 :group 'org-agenda-line-format
1627 :type 'function)
1629 (defcustom org-agenda-category-icon-alist nil
1630 "Alist of category icon to be displayed in agenda views.
1632 Each entry should have the following format:
1634 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1636 Where CATEGORY-REGEXP is a regexp matching the categories where
1637 the icon should be displayed.
1638 FILE-OR-DATA either a file path or a string containing image data.
1640 The other fields can be omited safely if not needed:
1641 TYPE indicates the image type.
1642 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1643 image data.
1644 PROPS are additional image attributes to assign to the image,
1645 like, e.g. `:ascent center'.
1647 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1649 If you want to set the display properties yourself, just put a
1650 list as second element:
1652 (CATEGORY-REGEXP (MY PROPERTY LIST))
1654 For example, to display a 16px horizontal space for Emacs
1655 category, you can use:
1657 (\"Emacs\" '(space . (:width (16))))"
1658 :group 'org-agenda-line-format
1659 :type '(alist :key-type (string :tag "Regexp matching category")
1660 :value-type (choice (list :tag "Icon"
1661 (string :tag "File or data")
1662 (symbol :tag "Type")
1663 (boolean :tag "Data?")
1664 (repeat :tag "Extra image properties" :inline t symbol))
1665 (list :tag "Display properties" sexp))))
1667 (defgroup org-agenda-column-view nil
1668 "Options concerning column view in the agenda."
1669 :tag "Org Agenda Column View"
1670 :group 'org-agenda)
1672 (defcustom org-agenda-columns-show-summaries t
1673 "Non-nil means show summaries for columns displayed in the agenda view."
1674 :group 'org-agenda-column-view
1675 :type 'boolean)
1677 (defcustom org-agenda-columns-compute-summary-properties t
1678 "Non-nil means recompute all summary properties before column view.
1679 When column view in the agenda is listing properties that have a summary
1680 operator, it can go to all relevant buffers and recompute the summaries
1681 there. This can mean overhead for the agenda column view, but is necessary
1682 to have thing up to date.
1683 As a special case, a CLOCKSUM property also makes sure that the clock
1684 computations are current."
1685 :group 'org-agenda-column-view
1686 :type 'boolean)
1688 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1689 "Non-nil means the duration of an appointment will add to day effort.
1690 The property to which appointment durations will be added is the one given
1691 in the option `org-effort-property'. If an appointment does not have
1692 an end time, `org-agenda-default-appointment-duration' will be used. If that
1693 is not set, an appointment without end time will not contribute to the time
1694 estimate."
1695 :group 'org-agenda-column-view
1696 :type 'boolean)
1698 (defcustom org-agenda-auto-exclude-function nil
1699 "A function called with a tag to decide if it is filtered on '/ RET'.
1700 The sole argument to the function, which is called once for each
1701 possible tag, is a string giving the name of the tag. The
1702 function should return either nil if the tag should be included
1703 as normal, or \"-<TAG>\" to exclude the tag.
1704 Note that for the purpose of tag filtering, only the lower-case version of
1705 all tags will be considered, so that this function will only ever see
1706 the lower-case version of all tags."
1707 :group 'org-agenda
1708 :type 'function)
1710 (defcustom org-agenda-bulk-custom-functions nil
1711 "Alist of characters and custom functions for bulk actions.
1712 For example, this value makes those two functions available:
1714 '((?R set-category)
1715 (?C bulk-cut))
1717 With selected entries in an agenda buffer, `B R' will call
1718 the custom function `set-category' on the selected entries.
1719 Note that functions in this alist don't need to be quoted."
1720 :type 'alist
1721 :group 'org-agenda)
1723 (eval-when-compile
1724 (require 'cl))
1725 (require 'org)
1727 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1728 "Execute BODY with point at location given by `org-hd-marker' property.
1729 If STRING is non-nil, the text property will be fetched from position 0
1730 in that string. If STRING is nil, it will be fetched from the beginning
1731 of the current line."
1732 (org-with-gensyms (marker)
1733 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1734 'org-hd-marker string)))
1735 (with-current-buffer (marker-buffer ,marker)
1736 (save-excursion
1737 (goto-char ,marker)
1738 ,@body)))))
1739 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1741 (defun org-add-agenda-custom-command (entry)
1742 "Replace or add a command in `org-agenda-custom-commands'.
1743 This is mostly for hacking and trying a new command - once the command
1744 works you probably want to add it to `org-agenda-custom-commands' for good."
1745 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1746 (if ass
1747 (setcdr ass (cdr entry))
1748 (push entry org-agenda-custom-commands))))
1750 ;;; Define the Org-agenda-mode
1752 (defvar org-agenda-mode-map (make-sparse-keymap)
1753 "Keymap for `org-agenda-mode'.")
1754 (if (fboundp 'defvaralias)
1755 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1757 (defvar org-agenda-menu) ; defined later in this file.
1758 (defvar org-agenda-restrict) ; defined later in this file.
1759 (defvar org-agenda-follow-mode nil)
1760 (defvar org-agenda-entry-text-mode nil)
1761 (defvar org-agenda-clockreport-mode nil)
1762 (defvar org-agenda-show-log nil)
1763 (defvar org-agenda-redo-command nil)
1764 (defvar org-agenda-query-string nil)
1765 (defvar org-agenda-mode-hook nil
1766 "Hook for `org-agenda-mode', run after the mode is turned on.")
1767 (defvar org-agenda-type nil)
1768 (defvar org-agenda-force-single-file nil)
1769 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1771 (defun org-agenda-mode ()
1772 "Mode for time-sorted view on action items in Org-mode files.
1774 The following commands are available:
1776 \\{org-agenda-mode-map}"
1777 (interactive)
1778 (kill-all-local-variables)
1779 (setq org-agenda-undo-list nil
1780 org-agenda-pending-undo-list nil
1781 org-agenda-bulk-marked-entries nil)
1782 (setq major-mode 'org-agenda-mode)
1783 ;; Keep global-font-lock-mode from turning on font-lock-mode
1784 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1785 (setq mode-name "Org-Agenda")
1786 (use-local-map org-agenda-mode-map)
1787 (easy-menu-add org-agenda-menu)
1788 (if org-startup-truncated (setq truncate-lines t))
1789 (org-set-local 'line-move-visual nil)
1790 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1791 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1792 ;; Make sure properties are removed when copying text
1793 (when (boundp 'buffer-substring-filters)
1794 (org-set-local 'buffer-substring-filters
1795 (cons (lambda (x)
1796 (set-text-properties 0 (length x) nil x) x)
1797 buffer-substring-filters)))
1798 (unless org-agenda-keep-modes
1799 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1800 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1801 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1802 org-agenda-show-log org-agenda-start-with-log-mode))
1804 (easy-menu-change
1805 '("Agenda") "Agenda Files"
1806 (append
1807 (list
1808 (vector
1809 (if (get 'org-agenda-files 'org-restrict)
1810 "Restricted to single file"
1811 "Edit File List")
1812 '(org-edit-agenda-file-list)
1813 (not (get 'org-agenda-files 'org-restrict)))
1814 "--")
1815 (mapcar 'org-file-menu-entry (org-agenda-files))))
1816 (org-agenda-set-mode-name)
1817 (apply
1818 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1819 (list 'org-agenda-mode-hook)))
1821 (substitute-key-definition 'undo 'org-agenda-undo
1822 org-agenda-mode-map global-map)
1823 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1824 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1825 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1826 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1827 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1828 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1829 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1830 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1831 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1832 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1833 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1834 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1835 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1836 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1837 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1838 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1839 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1840 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1841 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1842 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1843 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1844 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1845 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1846 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1847 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1848 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1849 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1850 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1851 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1852 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1853 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1854 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1855 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1856 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1857 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1858 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1859 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1860 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1861 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1862 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1863 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1864 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1865 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1866 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1867 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1869 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1870 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1871 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1872 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1873 (while l (org-defkey org-agenda-mode-map
1874 (int-to-string (pop l)) 'digit-argument)))
1876 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1877 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1878 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1879 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1880 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1881 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1882 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1883 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1884 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1885 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1886 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1887 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1888 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1889 'org-clock-modify-effort-estimate)
1890 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1891 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1892 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1893 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1894 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1895 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1896 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1897 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1898 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1899 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1900 (substitute-key-definition 'next-line 'org-agenda-next-line
1901 org-agenda-mode-map global-map)
1902 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1903 org-agenda-mode-map global-map)
1904 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1905 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1906 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1907 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1908 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1909 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1910 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1911 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1912 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1913 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1914 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1915 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1916 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1917 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1918 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1919 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1920 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1921 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1922 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1923 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1924 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1925 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1926 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1927 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1928 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1929 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1930 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1931 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1932 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1933 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1935 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1936 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1937 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1938 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1939 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1940 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1941 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1942 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1943 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1944 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1946 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1947 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1948 (when org-agenda-mouse-1-follows-link
1949 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1950 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1951 '("Agenda"
1952 ("Agenda Files")
1953 "--"
1954 ("Agenda Dates"
1955 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1956 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1957 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1958 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1959 "--"
1960 ("View"
1961 ["Day View" org-agenda-day-view
1962 :active (org-agenda-check-type nil 'agenda)
1963 :style radio :selected (eq org-agenda-current-span 'day)
1964 :keys "v d (or just d)"]
1965 ["Week View" org-agenda-week-view
1966 :active (org-agenda-check-type nil 'agenda)
1967 :style radio :selected (eq org-agenda-current-span 'week)
1968 :keys "v w (or just w)"]
1969 ["Month View" org-agenda-month-view
1970 :active (org-agenda-check-type nil 'agenda)
1971 :style radio :selected (eq org-agenda-current-span 'month)
1972 :keys "v m"]
1973 ["Year View" org-agenda-year-view
1974 :active (org-agenda-check-type nil 'agenda)
1975 :style radio :selected (eq org-agenda-current-span 'year)
1976 :keys "v y"]
1977 "--"
1978 ["Include Diary" org-agenda-toggle-diary
1979 :style toggle :selected org-agenda-include-diary
1980 :active (org-agenda-check-type nil 'agenda)]
1981 ["Include Deadlines" org-agenda-toggle-deadlines
1982 :style toggle :selected org-agenda-include-deadlines
1983 :active (org-agenda-check-type nil 'agenda)]
1984 ["Use Time Grid" org-agenda-toggle-time-grid
1985 :style toggle :selected org-agenda-use-time-grid
1986 :active (org-agenda-check-type nil 'agenda)]
1987 "--"
1988 ["Show clock report" org-agenda-clockreport-mode
1989 :style toggle :selected org-agenda-clockreport-mode
1990 :active (org-agenda-check-type nil 'agenda)]
1991 ["Show some entry text" org-agenda-entry-text-mode
1992 :style toggle :selected org-agenda-entry-text-mode
1993 :active t]
1994 "--"
1995 ["Show Logbook entries" org-agenda-log-mode
1996 :style toggle :selected org-agenda-show-log
1997 :active (org-agenda-check-type nil 'agenda 'timeline)
1998 :keys "v l (or just l)"]
1999 ["Include archived trees" org-agenda-archives-mode
2000 :style toggle :selected org-agenda-archives-mode :active t
2001 :keys "v a"]
2002 ["Include archive files" (org-agenda-archives-mode t)
2003 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2004 :keys "v A"]
2005 "--"
2006 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2007 ["Write view to file" org-write-agenda t]
2008 ["Rebuild buffer" org-agenda-redo t]
2009 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2010 "--"
2011 ["Show original entry" org-agenda-show t]
2012 ["Go To (other window)" org-agenda-goto t]
2013 ["Go To (this window)" org-agenda-switch-to t]
2014 ["Follow Mode" org-agenda-follow-mode
2015 :style toggle :selected org-agenda-follow-mode :active t]
2016 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2017 "--"
2018 ("TODO"
2019 ["Cycle TODO" org-agenda-todo t]
2020 ["Next TODO set" org-agenda-todo-nextset t]
2021 ["Previous TODO set" org-agenda-todo-previousset t]
2022 ["Add note" org-agenda-add-note t])
2023 ("Archive/Refile/Delete"
2024 ["Archive default" org-agenda-archive-default t]
2025 ["Archive default" org-agenda-archive-default-with-confirmation t]
2026 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2027 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2028 ["Archive subtree" org-agenda-archive t]
2029 "--"
2030 ["Refile" org-agenda-refile t]
2031 "--"
2032 ["Delete subtree" org-agenda-kill t])
2033 ("Bulk action"
2034 ["Mark entry" org-agenda-bulk-mark t]
2035 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2036 ["Unmark entry" org-agenda-bulk-unmark t]
2037 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2038 ["Act on all marked" org-agenda-bulk-action t]
2039 "--"
2040 ("Tags and Properties"
2041 ["Show all Tags" org-agenda-show-tags t]
2042 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2043 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2044 "--"
2045 ["Column View" org-columns t])
2046 ("Deadline/Schedule"
2047 ["Schedule" org-agenda-schedule t]
2048 ["Set Deadline" org-agenda-deadline t]
2049 "--"
2050 ["Mark item" org-agenda-action :active t :keys "k m"]
2051 ["Show mark item" org-agenda-action :active t :keys "k v"]
2052 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2053 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2054 "--"
2055 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2056 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2057 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2058 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2059 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2060 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2061 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2062 ("Clock and Effort"
2063 ["Clock in" org-agenda-clock-in t]
2064 ["Clock out" org-agenda-clock-out t]
2065 ["Clock cancel" org-agenda-clock-cancel t]
2066 ["Goto running clock" org-clock-goto t]
2067 "--"
2068 ["Set Effort" org-agenda-set-effort t]
2069 ["Change clocked effort" org-clock-modify-effort-estimate
2070 (org-clock-is-active)])
2071 ("Priority"
2072 ["Set Priority" org-agenda-priority t]
2073 ["Increase Priority" org-agenda-priority-up t]
2074 ["Decrease Priority" org-agenda-priority-down t]
2075 ["Show Priority" org-agenda-show-priority t])
2076 ("Calendar/Diary"
2077 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2078 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2079 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2080 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2081 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2082 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2083 "--"
2084 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2085 "--"
2086 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2087 "--"
2088 ("MobileOrg"
2089 ["Push Files and Views" org-mobile-push t]
2090 ["Get Captured and Flagged" org-mobile-pull t]
2091 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2092 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2093 "--"
2094 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2095 "--"
2096 ["Quit" org-agenda-quit t]
2097 ["Exit and Release Buffers" org-agenda-exit t]
2100 ;;; Agenda undo
2102 (defvar org-agenda-allow-remote-undo t
2103 "Non-nil means allow remote undo from the agenda buffer.")
2104 (defvar org-agenda-undo-list nil
2105 "List of undoable operations in the agenda since last refresh.")
2106 (defvar org-agenda-undo-has-started-in nil
2107 "Buffers that have already seen `undo-start' in the current undo sequence.")
2108 (defvar org-agenda-pending-undo-list nil
2109 "In a series of undo commands, this is the list of remaining undo items.")
2111 (defun org-agenda-undo ()
2112 "Undo a remote editing step in the agenda.
2113 This undoes changes both in the agenda buffer and in the remote buffer
2114 that have been changed along."
2115 (interactive)
2116 (or org-agenda-allow-remote-undo
2117 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2118 (if (not (eq this-command last-command))
2119 (setq org-agenda-undo-has-started-in nil
2120 org-agenda-pending-undo-list org-agenda-undo-list))
2121 (if (not org-agenda-pending-undo-list)
2122 (error "No further undo information"))
2123 (let* ((entry (pop org-agenda-pending-undo-list))
2124 buf line cmd rembuf)
2125 (setq cmd (pop entry) line (pop entry))
2126 (setq rembuf (nth 2 entry))
2127 (org-with-remote-undo rembuf
2128 (while (bufferp (setq buf (pop entry)))
2129 (if (pop entry)
2130 (with-current-buffer buf
2131 (let ((last-undo-buffer buf)
2132 (inhibit-read-only t))
2133 (unless (memq buf org-agenda-undo-has-started-in)
2134 (push buf org-agenda-undo-has-started-in)
2135 (make-local-variable 'pending-undo-list)
2136 (undo-start))
2137 (while (and pending-undo-list
2138 (listp pending-undo-list)
2139 (not (car pending-undo-list)))
2140 (pop pending-undo-list))
2141 (undo-more 1))))))
2142 (org-goto-line line)
2143 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2145 (defun org-verify-change-for-undo (l1 l2)
2146 "Verify that a real change occurred between the undo lists L1 and L2."
2147 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2148 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2149 (not (eq l1 l2)))
2151 ;;; Agenda dispatch
2153 (defvar org-agenda-restrict nil)
2154 (defvar org-agenda-restrict-begin (make-marker))
2155 (defvar org-agenda-restrict-end (make-marker))
2156 (defvar org-agenda-last-dispatch-buffer nil)
2157 (defvar org-agenda-overriding-restriction nil)
2159 ;;;###autoload
2160 (defun org-agenda (&optional arg keys restriction)
2161 "Dispatch agenda commands to collect entries to the agenda buffer.
2162 Prompts for a command to execute. Any prefix arg will be passed
2163 on to the selected command. The default selections are:
2165 a Call `org-agenda-list' to display the agenda for current day or week.
2166 t Call `org-todo-list' to display the global todo list.
2167 T Call `org-todo-list' to display the global todo list, select only
2168 entries with a specific TODO keyword (the user gets a prompt).
2169 m Call `org-tags-view' to display headlines with tags matching
2170 a condition (the user is prompted for the condition).
2171 M Like `m', but select only TODO entries, no ordinary headlines.
2172 L Create a timeline for the current buffer.
2173 e Export views to associated files.
2174 s Search entries for keywords.
2175 / Multi occur across all agenda files and also files listed
2176 in `org-agenda-text-search-extra-files'.
2177 < Restrict agenda commands to buffer, subtree, or region.
2178 Press several times to get the desired effect.
2179 > Remove a previous restriction.
2180 # List \"stuck\" projects.
2181 ! Configure what \"stuck\" means.
2182 C Configure custom agenda commands.
2184 More commands can be added by configuring the variable
2185 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2186 searches can be pre-defined in this way.
2188 If the current buffer is in Org-mode and visiting a file, you can also
2189 first press `<' once to indicate that the agenda should be temporarily
2190 \(until the next use of \\[org-agenda]) restricted to the current file.
2191 Pressing `<' twice means to restrict to the current subtree or region
2192 \(if active)."
2193 (interactive "P")
2194 (catch 'exit
2195 (let* ((prefix-descriptions nil)
2196 (org-agenda-window-setup (if (equal (buffer-name)
2197 org-agenda-buffer-name)
2198 'current-window
2199 org-agenda-window-setup))
2200 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2201 (org-agenda-custom-commands
2202 ;; normalize different versions
2203 (delq nil
2204 (mapcar
2205 (lambda (x)
2206 (cond ((stringp (cdr x))
2207 (push x prefix-descriptions)
2208 nil)
2209 ((stringp (nth 1 x)) x)
2210 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2211 (t (cons (car x) (cons "" (cdr x))))))
2212 org-agenda-custom-commands)))
2213 (buf (current-buffer))
2214 (bfn (buffer-file-name (buffer-base-buffer)))
2215 entry key type match lprops ans)
2216 ;; Turn off restriction unless there is an overriding one,
2217 (unless org-agenda-overriding-restriction
2218 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2219 ;; There is a request to keep the file list in place
2220 (put 'org-agenda-files 'org-restrict nil))
2221 (setq org-agenda-restrict nil)
2222 (move-marker org-agenda-restrict-begin nil)
2223 (move-marker org-agenda-restrict-end nil))
2224 ;; Delete old local properties
2225 (put 'org-agenda-redo-command 'org-lprops nil)
2226 ;; Delete previously set last-arguments
2227 (put 'org-agenda-redo-command 'last-args nil)
2228 ;; Remember where this call originated
2229 (setq org-agenda-last-dispatch-buffer (current-buffer))
2230 (unless keys
2231 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2232 keys (car ans)
2233 restriction (cdr ans)))
2234 ;; Establish the restriction, if any
2235 (when (and (not org-agenda-overriding-restriction) restriction)
2236 (put 'org-agenda-files 'org-restrict (list bfn))
2237 (cond
2238 ((eq restriction 'region)
2239 (setq org-agenda-restrict t)
2240 (move-marker org-agenda-restrict-begin (region-beginning))
2241 (move-marker org-agenda-restrict-end (region-end)))
2242 ((eq restriction 'subtree)
2243 (save-excursion
2244 (setq org-agenda-restrict t)
2245 (org-back-to-heading t)
2246 (move-marker org-agenda-restrict-begin (point))
2247 (move-marker org-agenda-restrict-end
2248 (progn (org-end-of-subtree t)))))))
2250 ;; For example the todo list should not need it (but does...)
2251 (cond
2252 ((setq entry (assoc keys org-agenda-custom-commands))
2253 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2254 (progn
2255 (setq type (nth 2 entry) match (eval (nth 3 entry))
2256 lprops (nth 4 entry))
2257 (put 'org-agenda-redo-command 'org-lprops lprops)
2258 (cond
2259 ((eq type 'agenda)
2260 (org-let lprops '(org-agenda-list current-prefix-arg)))
2261 ((eq type 'alltodo)
2262 (org-let lprops '(org-todo-list current-prefix-arg)))
2263 ((eq type 'search)
2264 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2265 ((eq type 'stuck)
2266 (org-let lprops '(org-agenda-list-stuck-projects
2267 current-prefix-arg)))
2268 ((eq type 'tags)
2269 (org-let lprops '(org-tags-view current-prefix-arg match)))
2270 ((eq type 'tags-todo)
2271 (org-let lprops '(org-tags-view '(4) match)))
2272 ((eq type 'todo)
2273 (org-let lprops '(org-todo-list match)))
2274 ((eq type 'tags-tree)
2275 (org-check-for-org-mode)
2276 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2277 ((eq type 'todo-tree)
2278 (org-check-for-org-mode)
2279 (org-let lprops
2280 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2281 (regexp-quote match) "\\>"))))
2282 ((eq type 'occur-tree)
2283 (org-check-for-org-mode)
2284 (org-let lprops '(org-occur match)))
2285 ((functionp type)
2286 (org-let lprops '(funcall type match)))
2287 ((fboundp type)
2288 (org-let lprops '(funcall type match)))
2289 (t (error "Invalid custom agenda command type %s" type))))
2290 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2291 ((equal keys "C")
2292 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2293 (customize-variable 'org-agenda-custom-commands))
2294 ((equal keys "a") (call-interactively 'org-agenda-list))
2295 ((equal keys "s") (call-interactively 'org-search-view))
2296 ((equal keys "t") (call-interactively 'org-todo-list))
2297 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2298 ((equal keys "m") (call-interactively 'org-tags-view))
2299 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2300 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2301 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2302 (org-add-hook
2303 'post-command-hook
2304 (lambda ()
2305 (unless (current-message)
2306 (let* ((m (org-agenda-get-any-marker))
2307 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2308 (when note
2309 (message (concat
2310 "FLAGGING-NOTE ([?] for more info): "
2311 (org-add-props
2312 (replace-regexp-in-string
2313 "\\\\n" "//"
2314 (copy-sequence note))
2315 nil 'face 'org-warning)))))))
2316 t t))
2317 ((equal keys "L")
2318 (unless (org-mode-p)
2319 (error "This is not an Org-mode file"))
2320 (unless restriction
2321 (put 'org-agenda-files 'org-restrict (list bfn))
2322 (org-call-with-arg 'org-timeline arg)))
2323 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2324 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2325 ((equal keys "!") (customize-variable 'org-stuck-projects))
2326 (t (error "Invalid agenda key"))))))
2328 (defun org-agenda-append-agenda ()
2329 "Append another agenda view to the current one.
2330 This function allows interactive building of block agendas.
2331 Agenda views are separated by `org-agenda-block-separator'."
2332 (interactive)
2333 (unless (string= (buffer-name) org-agenda-buffer-name)
2334 (error "Can only append from within agenda buffer"))
2335 (let ((org-agenda-multi t))
2336 (org-agenda)
2337 (widen)))
2339 (defun org-agenda-normalize-custom-commands (cmds)
2340 (delq nil
2341 (mapcar
2342 (lambda (x)
2343 (cond ((stringp (cdr x)) nil)
2344 ((stringp (nth 1 x)) x)
2345 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2346 (t (cons (car x) (cons "" (cdr x))))))
2347 cmds)))
2349 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2350 "The user interface for selecting an agenda command."
2351 (catch 'exit
2352 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2353 (restrict-ok (and bfn (org-mode-p)))
2354 (region-p (org-region-active-p))
2355 (custom org-agenda-custom-commands)
2356 (selstring "")
2357 restriction second-time
2358 c entry key type match prefixes rmheader header-end custom1 desc
2359 line lines left right n n1)
2360 (save-window-excursion
2361 (delete-other-windows)
2362 (org-switch-to-buffer-other-window " *Agenda Commands*")
2363 (erase-buffer)
2364 (insert (eval-when-compile
2365 (let ((header
2367 Press key for an agenda command: < Buffer, subtree/region restriction
2368 -------------------------------- > Remove restriction
2369 a Agenda for current week or day e Export agenda views
2370 t List of all TODO entries T Entries with special TODO kwd
2371 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2372 L Timeline for current buffer # List stuck projects (!=configure)
2373 s Search for keywords C Configure custom agenda commands
2374 / Multi-occur ? Find :FLAGGED: entries
2376 (start 0))
2377 (while (string-match
2378 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2379 header start)
2380 (setq start (match-end 0))
2381 (add-text-properties (match-beginning 2) (match-end 2)
2382 '(face bold) header))
2383 header)))
2384 (setq header-end (move-marker (make-marker) (point)))
2385 (while t
2386 (setq custom1 custom)
2387 (when (eq rmheader t)
2388 (org-goto-line 1)
2389 (re-search-forward ":" nil t)
2390 (delete-region (match-end 0) (point-at-eol))
2391 (forward-char 1)
2392 (looking-at "-+")
2393 (delete-region (match-end 0) (point-at-eol))
2394 (move-marker header-end (match-end 0)))
2395 (goto-char header-end)
2396 (delete-region (point) (point-max))
2398 ;; Produce all the lines that describe custom commands and prefixes
2399 (setq lines nil)
2400 (while (setq entry (pop custom1))
2401 (setq key (car entry) desc (nth 1 entry)
2402 type (nth 2 entry)
2403 match (nth 3 entry))
2404 (if (> (length key) 1)
2405 (add-to-list 'prefixes (string-to-char key))
2406 (setq line
2407 (format
2408 "%-4s%-14s"
2409 (org-add-props (copy-sequence key)
2410 '(face bold))
2411 (cond
2412 ((string-match "\\S-" desc) desc)
2413 ((eq type 'agenda) "Agenda for current week or day")
2414 ((eq type 'alltodo) "List of all TODO entries")
2415 ((eq type 'search) "Word search")
2416 ((eq type 'stuck) "List of stuck projects")
2417 ((eq type 'todo) "TODO keyword")
2418 ((eq type 'tags) "Tags query")
2419 ((eq type 'tags-todo) "Tags (TODO)")
2420 ((eq type 'tags-tree) "Tags tree")
2421 ((eq type 'todo-tree) "TODO kwd tree")
2422 ((eq type 'occur-tree) "Occur tree")
2423 ((functionp type) (if (symbolp type)
2424 (symbol-name type)
2425 "Lambda expression"))
2426 (t "???"))))
2427 (if org-agenda-menu-show-matcher
2428 (setq line
2429 (concat line ": "
2430 (cond
2431 ((stringp match)
2432 (setq match (copy-sequence match))
2433 (org-add-props match nil 'face 'org-warning))
2434 (match
2435 (format "set of %d commands" (length match)))
2436 (t ""))))
2437 (if (org-string-nw-p match)
2438 (add-text-properties
2439 0 (length line) (list 'help-echo
2440 (concat "Matcher: "match)) line)))
2441 (push line lines)))
2442 (setq lines (nreverse lines))
2443 (when prefixes
2444 (mapc (lambda (x)
2445 (push
2446 (format "%s %s"
2447 (org-add-props (char-to-string x)
2448 nil 'face 'bold)
2449 (or (cdr (assoc (concat selstring
2450 (char-to-string x))
2451 prefix-descriptions))
2452 "Prefix key"))
2453 lines))
2454 prefixes))
2456 ;; Check if we should display in two columns
2457 (if org-agenda-menu-two-column
2458 (progn
2459 (setq n (length lines)
2460 n1 (+ (/ n 2) (mod n 2))
2461 right (nthcdr n1 lines)
2462 left (copy-sequence lines))
2463 (setcdr (nthcdr (1- n1) left) nil))
2464 (setq left lines right nil))
2465 (while left
2466 (insert "\n" (pop left))
2467 (when right
2468 (if (< (current-column) 40)
2469 (move-to-column 40 t)
2470 (insert " "))
2471 (insert (pop right))))
2473 ;; Make the window the right size
2474 (goto-char (point-min))
2475 (if second-time
2476 (if (not (pos-visible-in-window-p (point-max)))
2477 (org-fit-window-to-buffer))
2478 (setq second-time t)
2479 (org-fit-window-to-buffer))
2481 ;; Ask for selection
2482 (message "Press key for agenda command%s:"
2483 (if (or restrict-ok org-agenda-overriding-restriction)
2484 (if org-agenda-overriding-restriction
2485 " (restriction lock active)"
2486 (if restriction
2487 (format " (restricted to %s)" restriction)
2488 " (unrestricted)"))
2489 ""))
2490 (setq c (read-char-exclusive))
2491 (message "")
2492 (cond
2493 ((assoc (char-to-string c) custom)
2494 (setq selstring (concat selstring (char-to-string c)))
2495 (throw 'exit (cons selstring restriction)))
2496 ((memq c prefixes)
2497 (setq selstring (concat selstring (char-to-string c))
2498 prefixes nil
2499 rmheader (or rmheader t)
2500 custom (delq nil (mapcar
2501 (lambda (x)
2502 (if (or (= (length (car x)) 1)
2503 (/= (string-to-char (car x)) c))
2505 (cons (substring (car x) 1) (cdr x))))
2506 custom))))
2507 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2508 (message "Restriction is only possible in Org-mode buffers")
2509 (ding) (sit-for 1))
2510 ((eq c ?1)
2511 (org-agenda-remove-restriction-lock 'noupdate)
2512 (setq restriction 'buffer))
2513 ((eq c ?0)
2514 (org-agenda-remove-restriction-lock 'noupdate)
2515 (setq restriction (if region-p 'region 'subtree)))
2516 ((eq c ?<)
2517 (org-agenda-remove-restriction-lock 'noupdate)
2518 (setq restriction
2519 (cond
2520 ((eq restriction 'buffer)
2521 (if region-p 'region 'subtree))
2522 ((memq restriction '(subtree region))
2523 nil)
2524 (t 'buffer))))
2525 ((eq c ?>)
2526 (org-agenda-remove-restriction-lock 'noupdate)
2527 (setq restriction nil))
2528 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2529 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2530 ((and (> (length selstring) 0) (eq c ?\d))
2531 (delete-window)
2532 (org-agenda-get-restriction-and-command prefix-descriptions))
2534 ((equal c ?q) (error "Abort"))
2535 (t (error "Invalid key %c" c))))))))
2537 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2538 (defvar org-agenda-last-arguments nil
2539 "The arguments of the previous call to `org-agenda'.")
2540 (defun org-agenda-run-series (name series)
2541 (org-let (nth 1 series) '(org-prepare-agenda name))
2542 (let* ((org-agenda-multi t)
2543 (redo (list 'org-agenda-run-series name (list 'quote series)))
2544 (org-agenda-overriding-arguments
2545 (or org-agenda-overriding-arguments
2546 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2547 (get 'org-agenda-redo-command 'last-args))))
2548 (cmds (car series))
2549 (gprops (nth 1 series))
2550 match ;; The byte compiler incorrectly complains about this. Keep it!
2551 cmd type lprops)
2552 (while (setq cmd (pop cmds))
2553 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2554 (cond
2555 ((eq type 'agenda)
2556 (org-let2 gprops lprops
2557 '(call-interactively 'org-agenda-list)))
2558 ((eq type 'alltodo)
2559 (org-let2 gprops lprops
2560 '(call-interactively 'org-todo-list)))
2561 ((eq type 'search)
2562 (org-let2 gprops lprops
2563 '(org-search-view current-prefix-arg match nil)))
2564 ((eq type 'stuck)
2565 (org-let2 gprops lprops
2566 '(call-interactively 'org-agenda-list-stuck-projects)))
2567 ((eq type 'tags)
2568 (org-let2 gprops lprops
2569 '(org-tags-view current-prefix-arg match)))
2570 ((eq type 'tags-todo)
2571 (org-let2 gprops lprops
2572 '(org-tags-view '(4) match)))
2573 ((eq type 'todo)
2574 (org-let2 gprops lprops
2575 '(org-todo-list match)))
2576 ((fboundp type)
2577 (org-let2 gprops lprops
2578 '(funcall type match)))
2579 (t (error "Invalid type in command series"))))
2580 (widen)
2581 (setq org-agenda-redo-command redo)
2582 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2583 (goto-char (point-min)))
2584 (org-fit-agenda-window)
2585 (org-let (nth 1 series) '(org-finalize-agenda)))
2587 ;;;###autoload
2588 (defmacro org-batch-agenda (cmd-key &rest parameters)
2589 "Run an agenda command in batch mode and send the result to STDOUT.
2590 If CMD-KEY is a string of length 1, it is used as a key in
2591 `org-agenda-custom-commands' and triggers this command. If it is a
2592 longer string it is used as a tags/todo match string.
2593 Parameters are alternating variable names and values that will be bound
2594 before running the agenda command."
2595 (org-eval-in-environment (org-make-parameter-alist parameters)
2596 (if (> (length cmd-key) 2)
2597 (org-tags-view nil cmd-key)
2598 (org-agenda nil cmd-key)))
2599 (set-buffer org-agenda-buffer-name)
2600 (princ (org-encode-for-stdout (buffer-string))))
2601 (def-edebug-spec org-batch-agenda (form &rest sexp))
2603 ;(defun org-encode-for-stdout (string)
2604 ; (if (fboundp 'encode-coding-string)
2605 ; (encode-coding-string string buffer-file-coding-system)
2606 ; string))
2608 (defun org-encode-for-stdout (string)
2609 string)
2611 (defvar org-agenda-info nil)
2613 ;;;###autoload
2614 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2615 "Run an agenda command in batch mode and send the result to STDOUT.
2616 If CMD-KEY is a string of length 1, it is used as a key in
2617 `org-agenda-custom-commands' and triggers this command. If it is a
2618 longer string it is used as a tags/todo match string.
2619 Parameters are alternating variable names and values that will be bound
2620 before running the agenda command.
2622 The output gives a line for each selected agenda item. Each
2623 item is a list of comma-separated values, like this:
2625 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2627 category The category of the item
2628 head The headline, without TODO kwd, TAGS and PRIORITY
2629 type The type of the agenda entry, can be
2630 todo selected in TODO match
2631 tagsmatch selected in tags match
2632 diary imported from diary
2633 deadline a deadline on given date
2634 scheduled scheduled on given date
2635 timestamp entry has timestamp on given date
2636 closed entry was closed on given date
2637 upcoming-deadline warning about deadline
2638 past-scheduled forwarded scheduled item
2639 block entry has date block including g. date
2640 todo The todo keyword, if any
2641 tags All tags including inherited ones, separated by colons
2642 date The relevant date, like 2007-2-14
2643 time The time, like 15:00-16:50
2644 extra Sting with extra planning info
2645 priority-l The priority letter if any was given
2646 priority-n The computed numerical priority
2647 agenda-day The day in the agenda where this is listed"
2648 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2649 (org-make-parameter-alist parameters))
2650 (if (> (length cmd-key) 2)
2651 (org-tags-view nil cmd-key)
2652 (org-agenda nil cmd-key)))
2653 (set-buffer org-agenda-buffer-name)
2654 (let* ((lines (org-split-string (buffer-string) "\n"))
2655 line)
2656 (while (setq line (pop lines))
2657 (catch 'next
2658 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2659 (setq org-agenda-info
2660 (org-fix-agenda-info (text-properties-at 0 line)))
2661 (princ
2662 (org-encode-for-stdout
2663 (mapconcat 'org-agenda-export-csv-mapper
2664 '(org-category txt type todo tags date time extra
2665 priority-letter priority agenda-day)
2666 ",")))
2667 (princ "\n")))))
2668 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2670 (defun org-fix-agenda-info (props)
2671 "Make sure all properties on an agenda item have a canonical form.
2672 This ensures the export commands can easily use it."
2673 (let (tmp re)
2674 (when (setq tmp (plist-get props 'tags))
2675 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2676 (when (setq tmp (plist-get props 'date))
2677 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2678 (let ((calendar-date-display-form '(year "-" month "-" day)))
2679 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2681 (setq tmp (calendar-date-string tmp)))
2682 (setq props (plist-put props 'date tmp)))
2683 (when (setq tmp (plist-get props 'day))
2684 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2685 (let ((calendar-date-display-form '(year "-" month "-" day)))
2686 (setq tmp (calendar-date-string tmp)))
2687 (setq props (plist-put props 'day tmp))
2688 (setq props (plist-put props 'agenda-day tmp)))
2689 (when (setq tmp (plist-get props 'txt))
2690 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2691 (plist-put props 'priority-letter (match-string 1 tmp))
2692 (setq tmp (replace-match "" t t tmp)))
2693 (when (and (setq re (plist-get props 'org-todo-regexp))
2694 (setq re (concat "\\`\\.*" re " ?"))
2695 (string-match re tmp))
2696 (plist-put props 'todo (match-string 1 tmp))
2697 (setq tmp (replace-match "" t t tmp)))
2698 (plist-put props 'txt tmp)))
2699 props)
2701 (defun org-agenda-export-csv-mapper (prop)
2702 (let ((res (plist-get org-agenda-info prop)))
2703 (setq res
2704 (cond
2705 ((not res) "")
2706 ((stringp res) res)
2707 (t (prin1-to-string res))))
2708 (while (string-match "," res)
2709 (setq res (replace-match ";" t t res)))
2710 (org-trim res)))
2713 ;;;###autoload
2714 (defun org-store-agenda-views (&rest parameters)
2715 (interactive)
2716 (eval (list 'org-batch-store-agenda-views)))
2718 ;;;###autoload
2719 (defmacro org-batch-store-agenda-views (&rest parameters)
2720 "Run all custom agenda commands that have a file argument."
2721 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2722 (pop-up-frames nil)
2723 (dir default-directory)
2724 (pars (org-make-parameter-alist parameters))
2725 cmd thiscmdkey files opts cmd-or-set)
2726 (save-window-excursion
2727 (while cmds
2728 (setq cmd (pop cmds)
2729 thiscmdkey (car cmd)
2730 cmd-or-set (nth 2 cmd)
2731 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2732 files (nth (if (listp cmd-or-set) 4 5) cmd))
2733 (if (stringp files) (setq files (list files)))
2734 (when files
2735 (org-eval-in-environment (append org-agenda-exporter-settings
2736 opts pars)
2737 (org-agenda nil thiscmdkey))
2738 (set-buffer org-agenda-buffer-name)
2739 (while files
2740 (org-eval-in-environment (append org-agenda-exporter-settings
2741 opts pars)
2742 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2743 (and (get-buffer org-agenda-buffer-name)
2744 (kill-buffer org-agenda-buffer-name)))))))
2745 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2747 (defun org-agenda-mark-header-line (pos)
2748 "Mark the line at POS as an agenda structure header."
2749 (save-excursion
2750 (goto-char pos)
2751 (put-text-property (point-at-bol) (point-at-eol)
2752 'org-agenda-structural-header t)
2753 (when org-agenda-title-append
2754 (put-text-property (point-at-bol) (point-at-eol)
2755 'org-agenda-title-append org-agenda-title-append))))
2757 (defvar org-mobile-creating-agendas)
2758 (defun org-write-agenda (file &optional open nosettings)
2759 "Write the current buffer (an agenda view) as a file.
2760 Depending on the extension of the file name, plain text (.txt),
2761 HTML (.html or .htm) or Postscript (.ps) is produced.
2762 If the extension is .ics, run icalendar export over all files used
2763 to construct the agenda and limit the export to entries listed in the
2764 agenda now.
2765 With prefix argument OPEN, open the new file immediately.
2766 If NOSETTINGS is given, do not scope the settings of
2767 `org-agenda-exporter-settings' into the export commands. This is used when
2768 the settings have already been scoped and we do not wish to overrule other,
2769 higher priority settings."
2770 (interactive "FWrite agenda to file: \nP")
2771 (if (not (file-writable-p file))
2772 (error "Cannot write agenda to file %s" file))
2773 (org-let (if nosettings nil org-agenda-exporter-settings)
2774 '(save-excursion
2775 (save-window-excursion
2776 (org-agenda-mark-filtered-text)
2777 (let ((bs (copy-sequence (buffer-string))) beg)
2778 (org-agenda-unmark-filtered-text)
2779 (with-temp-buffer
2780 (rename-buffer "Agenda View" t)
2781 (set-buffer-modified-p nil)
2782 (insert bs)
2783 (org-agenda-remove-marked-text 'org-filtered)
2784 (while (setq beg (text-property-any (point-min) (point-max)
2785 'org-filtered t))
2786 (delete-region
2787 beg (or (next-single-property-change beg 'org-filtered)
2788 (point-max))))
2789 (run-hooks 'org-agenda-before-write-hook)
2790 (cond
2791 ((org-bound-and-true-p org-mobile-creating-agendas)
2792 (org-mobile-write-agenda-for-mobile file))
2793 ((string-match "\\.html?\\'" file)
2794 (require 'htmlize)
2795 (set-buffer (htmlize-buffer (current-buffer)))
2797 (when (and org-agenda-export-html-style
2798 (string-match "<style>" org-agenda-export-html-style))
2799 ;; replace <style> section with org-agenda-export-html-style
2800 (goto-char (point-min))
2801 (kill-region (- (search-forward "<style") 6)
2802 (search-forward "</style>"))
2803 (insert org-agenda-export-html-style))
2804 (write-file file)
2805 (kill-buffer (current-buffer))
2806 (message "HTML written to %s" file))
2807 ((string-match "\\.ps\\'" file)
2808 (require 'ps-print)
2809 (ps-print-buffer-with-faces file)
2810 (message "Postscript written to %s" file))
2811 ((string-match "\\.pdf\\'" file)
2812 (require 'ps-print)
2813 (ps-print-buffer-with-faces
2814 (concat (file-name-sans-extension file) ".ps"))
2815 (call-process "ps2pdf" nil nil nil
2816 (expand-file-name
2817 (concat (file-name-sans-extension file) ".ps"))
2818 (expand-file-name file))
2819 (delete-file (concat (file-name-sans-extension file) ".ps"))
2820 (message "PDF written to %s" file))
2821 ((string-match "\\.ics\\'" file)
2822 (require 'org-icalendar)
2823 (let ((org-agenda-marker-table
2824 (org-create-marker-find-array
2825 (org-agenda-collect-markers)))
2826 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2827 (org-combined-agenda-icalendar-file file))
2828 (apply 'org-export-icalendar 'combine
2829 (org-agenda-files nil 'ifmode))))
2831 (let ((bs (buffer-string)))
2832 (find-file file)
2833 (erase-buffer)
2834 (insert bs)
2835 (save-buffer 0)
2836 (kill-buffer (current-buffer))
2837 (message "Plain text written to %s" file))))))))
2838 (set-buffer org-agenda-buffer-name))
2839 (when open (org-open-file file)))
2841 (defvar org-agenda-filter-overlays nil)
2843 (defun org-agenda-mark-filtered-text ()
2844 "Mark all text hidden by filtering with a text property."
2845 (let ((inhibit-read-only t))
2846 (mapc
2847 (lambda (o)
2848 (when (equal (overlay-buffer o) (current-buffer))
2849 (put-text-property
2850 (overlay-start o) (overlay-end o)
2851 'org-filtered t)))
2852 org-agenda-filter-overlays)))
2854 (defun org-agenda-unmark-filtered-text ()
2855 "Remove the filtering text property."
2856 (let ((inhibit-read-only t))
2857 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2859 (defun org-agenda-remove-marked-text (property &optional value)
2860 "Delete all text marked with VALUE of PROPERTY.
2861 VALUE defaults to t."
2862 (let (beg)
2863 (setq value (or value t))
2864 (while (setq beg (text-property-any (point-min) (point-max)
2865 property value))
2866 (delete-region
2867 beg (or (next-single-property-change beg 'org-filtered)
2868 (point-max))))))
2870 (defun org-agenda-add-entry-text ()
2871 "Add entry text to agenda lines.
2872 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2873 entry text following headings shown in the agenda.
2874 Drawers will be excluded, also the line with scheduling/deadline info."
2875 (when (and (> org-agenda-add-entry-text-maxlines 0)
2876 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2877 (let (m txt)
2878 (goto-char (point-min))
2879 (while (not (eobp))
2880 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2881 (beginning-of-line 2)
2882 (setq txt (org-agenda-get-some-entry-text
2883 m org-agenda-add-entry-text-maxlines " > "))
2884 (end-of-line 1)
2885 (if (string-match "\\S-" txt)
2886 (insert "\n" txt)
2887 (or (eobp) (forward-char 1))))))))
2889 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2890 &rest keep)
2891 "Extract entry text from MARKER, at most N-LINES lines.
2892 This will ignore drawers etc, just get the text.
2893 If INDENT is given, prefix every line with this string. If KEEP is
2894 given, it is a list of symbols, defining stuff that should not be
2895 removed from the entry content. Currently only `planning' is allowed here."
2896 (let (txt drawer-re kwd-time-re ind)
2897 (save-excursion
2898 (with-current-buffer (marker-buffer marker)
2899 (if (not (org-mode-p))
2900 (setq txt "")
2901 (save-excursion
2902 (save-restriction
2903 (widen)
2904 (goto-char marker)
2905 (end-of-line 1)
2906 (setq txt (buffer-substring
2907 (min (1+ (point)) (point-max))
2908 (progn (outline-next-heading) (point)))
2909 drawer-re org-drawer-regexp
2910 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2911 ".*\n?"))
2912 (with-temp-buffer
2913 (insert txt)
2914 (when org-agenda-add-entry-text-descriptive-links
2915 (goto-char (point-min))
2916 (while (org-activate-bracket-links (point-max))
2917 (add-text-properties (match-beginning 0) (match-end 0)
2918 '(face org-link))))
2919 (goto-char (point-min))
2920 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2921 (set-text-properties (match-beginning 0) (match-end 0)
2922 nil))
2923 (goto-char (point-min))
2924 (while (re-search-forward drawer-re nil t)
2925 (delete-region
2926 (match-beginning 0)
2927 (progn (re-search-forward
2928 "^[ \t]*:END:.*\n?" nil 'move)
2929 (point))))
2930 (unless (member 'planning keep)
2931 (goto-char (point-min))
2932 (while (re-search-forward kwd-time-re nil t)
2933 (replace-match "")))
2934 (goto-char (point-min))
2935 (when org-agenda-entry-text-exclude-regexps
2936 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2937 (while (setq re (pop re-list))
2938 (goto-char (point-min))
2939 (while (re-search-forward re nil t)
2940 (replace-match "")))))
2941 (goto-char (point-max))
2942 (skip-chars-backward " \t\n")
2943 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2945 ;; find and remove min common indentation
2946 (goto-char (point-min))
2947 (untabify (point-min) (point-max))
2948 (setq ind (org-get-indentation))
2949 (while (not (eobp))
2950 (unless (looking-at "[ \t]*$")
2951 (setq ind (min ind (org-get-indentation))))
2952 (beginning-of-line 2))
2953 (goto-char (point-min))
2954 (while (not (eobp))
2955 (unless (looking-at "[ \t]*$")
2956 (move-to-column ind)
2957 (delete-region (point-at-bol) (point)))
2958 (beginning-of-line 2))
2960 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2962 (goto-char (point-min))
2963 (when indent
2964 (while (and (not (eobp)) (re-search-forward "^" nil t))
2965 (replace-match indent t t)))
2966 (goto-char (point-min))
2967 (while (looking-at "[ \t]*\n") (replace-match ""))
2968 (goto-char (point-max))
2969 (when (> (org-current-line)
2970 n-lines)
2971 (org-goto-line (1+ n-lines))
2972 (backward-char 1))
2973 (setq txt (buffer-substring (point-min) (point)))))))))
2974 txt))
2976 (defun org-agenda-collect-markers ()
2977 "Collect the markers pointing to entries in the agenda buffer."
2978 (let (m markers)
2979 (save-excursion
2980 (goto-char (point-min))
2981 (while (not (eobp))
2982 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2983 (org-get-at-bol 'org-marker)))
2984 (push m markers))
2985 (beginning-of-line 2)))
2986 (nreverse markers)))
2988 (defun org-create-marker-find-array (marker-list)
2989 "Create a alist of files names with all marker positions in that file."
2990 (let (f tbl m a p)
2991 (while (setq m (pop marker-list))
2992 (setq p (marker-position m)
2993 f (buffer-file-name (or (buffer-base-buffer
2994 (marker-buffer m))
2995 (marker-buffer m))))
2996 (if (setq a (assoc f tbl))
2997 (push (marker-position m) (cdr a))
2998 (push (list f p) tbl)))
2999 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3000 tbl)))
3002 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3003 (defun org-check-agenda-marker-table ()
3004 "Check of the current entry is on the marker list."
3005 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3007 (and (setq a (assoc file org-agenda-marker-table))
3008 (save-match-data
3009 (save-excursion
3010 (org-back-to-heading t)
3011 (member (point) (cdr a)))))))
3013 (defun org-check-for-org-mode ()
3014 "Make sure current buffer is in org-mode. Error if not."
3015 (or (org-mode-p)
3016 (error "Cannot execute org-mode agenda command on buffer in %s"
3017 major-mode)))
3019 (defun org-fit-agenda-window ()
3020 "Fit the window to the buffer size."
3021 (and (memq org-agenda-window-setup '(reorganize-frame))
3022 (fboundp 'fit-window-to-buffer)
3023 (org-fit-window-to-buffer
3025 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3026 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3028 ;;; Agenda prepare and finalize
3030 (defvar org-agenda-multi nil) ; dynamically scoped
3031 (defvar org-agenda-buffer-name "*Org Agenda*")
3032 (defvar org-pre-agenda-window-conf nil)
3033 (defvar org-agenda-columns-active nil)
3034 (defvar org-agenda-name nil)
3035 (defvar org-agenda-filter nil)
3036 (defvar org-agenda-filter-while-redo nil)
3037 (defvar org-agenda-filter-preset nil
3038 "A preset of the tags filter used for secondary agenda filtering.
3039 This must be a list of strings, each string must be a single tag preceded
3040 by \"+\" or \"-\".
3041 This variable should not be set directly, but agenda custom commands can
3042 bind it in the options section. The preset filter is a global property of
3043 the entire agenda view. In a block agenda, it will not work reliably to
3044 define a filter for one of the individual blocks. You need to set it in
3045 the global options and expect it to be applied to the entire view.")
3047 (defun org-prepare-agenda (&optional name)
3048 (setq org-todo-keywords-for-agenda nil)
3049 (setq org-done-keywords-for-agenda nil)
3050 (setq org-drawers-for-agenda nil)
3051 (unless org-agenda-persistent-filter
3052 (setq org-agenda-filter nil))
3053 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3054 (if org-agenda-multi
3055 (progn
3056 (setq buffer-read-only nil)
3057 (goto-char (point-max))
3058 (unless (or (bobp) org-agenda-compact-blocks
3059 (not org-agenda-block-separator))
3060 (insert "\n"
3061 (if (stringp org-agenda-block-separator)
3062 org-agenda-block-separator
3063 (make-string (window-width) org-agenda-block-separator))
3064 "\n"))
3065 (narrow-to-region (point) (point-max)))
3066 (org-agenda-reset-markers)
3067 (setq org-agenda-contributing-files nil)
3068 (setq org-agenda-columns-active nil)
3069 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3070 (setq org-todo-keywords-for-agenda
3071 (org-uniquify org-todo-keywords-for-agenda))
3072 (setq org-done-keywords-for-agenda
3073 (org-uniquify org-done-keywords-for-agenda))
3074 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3075 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3076 (awin (get-buffer-window abuf)))
3077 (cond
3078 ((equal (current-buffer) abuf) nil)
3079 (awin (select-window awin))
3080 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3081 ((equal org-agenda-window-setup 'current-window)
3082 (org-pop-to-buffer-same-window abuf))
3083 ((equal org-agenda-window-setup 'other-window)
3084 (org-switch-to-buffer-other-window abuf))
3085 ((equal org-agenda-window-setup 'other-frame)
3086 (switch-to-buffer-other-frame abuf))
3087 ((equal org-agenda-window-setup 'reorganize-frame)
3088 (delete-other-windows)
3089 (org-switch-to-buffer-other-window abuf)))
3090 ;; additional test in case agenda is invoked from within agenda
3091 ;; buffer via elisp link
3092 (unless (equal (current-buffer) abuf)
3093 (org-pop-to-buffer-same-window abuf)))
3094 (setq buffer-read-only nil)
3095 (let ((inhibit-read-only t)) (erase-buffer))
3096 (org-agenda-mode)
3097 (and name (not org-agenda-name)
3098 (org-set-local 'org-agenda-name name)))
3099 (setq buffer-read-only nil))
3101 (defun org-finalize-agenda ()
3102 "Finishing touch for the agenda buffer, called just before displaying it."
3103 (unless org-agenda-multi
3104 (save-excursion
3105 (let ((inhibit-read-only t))
3106 (goto-char (point-min))
3107 (while (org-activate-bracket-links (point-max))
3108 (add-text-properties (match-beginning 0) (match-end 0)
3109 '(face org-link)))
3110 (org-agenda-align-tags)
3111 (unless org-agenda-with-colors
3112 (remove-text-properties (point-min) (point-max) '(face nil))))
3113 (if (and (boundp 'org-agenda-overriding-columns-format)
3114 org-agenda-overriding-columns-format)
3115 (org-set-local 'org-agenda-overriding-columns-format
3116 org-agenda-overriding-columns-format))
3117 (if (and (boundp 'org-agenda-view-columns-initially)
3118 org-agenda-view-columns-initially)
3119 (org-agenda-columns))
3120 (when org-agenda-fontify-priorities
3121 (org-agenda-fontify-priorities))
3122 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3123 (org-agenda-dim-blocked-tasks))
3124 (org-agenda-mark-clocking-task)
3125 (when org-agenda-entry-text-mode
3126 (org-agenda-entry-text-hide)
3127 (org-agenda-entry-text-show))
3128 (if (functionp 'org-habit-insert-consistency-graphs)
3129 (org-habit-insert-consistency-graphs))
3130 (run-hooks 'org-finalize-agenda-hook)
3131 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3132 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3133 (org-agenda-filter-apply org-agenda-filter))
3136 (defun org-agenda-mark-clocking-task ()
3137 "Mark the current clock entry in the agenda if it is present."
3138 (mapc (lambda (o)
3139 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3140 (delete-overlay o)))
3141 (overlays-in (point-min) (point-max)))
3142 (when (marker-buffer org-clock-hd-marker)
3143 (save-excursion
3144 (goto-char (point-min))
3145 (let (s ov)
3146 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3147 (goto-char s)
3148 (when (equal (org-get-at-bol 'org-hd-marker)
3149 org-clock-hd-marker)
3150 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3151 (overlay-put ov 'type 'org-agenda-clocking)
3152 (overlay-put ov 'face 'org-agenda-clocking)
3153 (overlay-put ov 'help-echo
3154 "The clock is running in this item")))))))
3156 (defun org-agenda-fontify-priorities ()
3157 "Make highest priority lines bold, and lowest italic."
3158 (interactive)
3159 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3160 (delete-overlay o)))
3161 (overlays-in (point-min) (point-max)))
3162 (save-excursion
3163 (let ((inhibit-read-only t)
3164 b e p ov h l)
3165 (goto-char (point-min))
3166 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3167 (setq h (or (get-char-property (point) 'org-highest-priority)
3168 org-highest-priority)
3169 l (or (get-char-property (point) 'org-lowest-priority)
3170 org-lowest-priority)
3171 p (string-to-char (match-string 1))
3172 b (match-beginning 0)
3173 e (if (eq org-agenda-fontify-priorities 'cookies)
3174 (match-end 0)
3175 (point-at-eol))
3176 ov (make-overlay b e))
3177 (overlay-put
3178 ov 'face
3179 (cond ((org-face-from-face-or-color
3180 'priority nil
3181 (cdr (assoc p org-priority-faces))))
3182 ((and (listp org-agenda-fontify-priorities)
3183 (org-face-from-face-or-color
3184 'priority nil
3185 (cdr (assoc p org-agenda-fontify-priorities)))))
3186 ((equal p l) 'italic)
3187 ((equal p h) 'bold)))
3188 (overlay-put ov 'org-type 'org-priority)))))
3190 (defun org-agenda-dim-blocked-tasks ()
3191 "Dim currently blocked TODO's in the agenda display."
3192 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3193 (delete-overlay o)))
3194 (overlays-in (point-min) (point-max)))
3195 (save-excursion
3196 (let ((inhibit-read-only t)
3197 (org-depend-tag-blocked nil)
3198 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3199 org-blocked-by-checkboxes
3200 invis1 b e p ov h l)
3201 (goto-char (point-min))
3202 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3203 (and pos (goto-char (1+ pos))))
3204 (setq org-blocked-by-checkboxes nil invis1 invis)
3205 (let ((marker (org-get-at-bol 'org-hd-marker)))
3206 (when (and marker
3207 (not (with-current-buffer (marker-buffer marker)
3208 (save-excursion
3209 (goto-char marker)
3210 (if (org-entry-get nil "NOBLOCKING")
3211 t ;; Never block this entry
3212 (run-hook-with-args-until-failure
3213 'org-blocker-hook
3214 (list :type 'todo-state-change
3215 :position marker
3216 :from 'todo
3217 :to 'done)))))))
3218 (if org-blocked-by-checkboxes (setq invis1 nil))
3219 (setq b (if invis1
3220 (max (point-min) (1- (point-at-bol)))
3221 (point-at-bol))
3222 e (point-at-eol)
3223 ov (make-overlay b e))
3224 (if invis1
3225 (overlay-put ov 'invisible t)
3226 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3227 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3229 (defvar org-agenda-skip-function nil
3230 "Function to be called at each match during agenda construction.
3231 If this function returns nil, the current match should not be skipped.
3232 Otherwise, the function must return a position from where the search
3233 should be continued.
3234 This may also be a Lisp form, it will be evaluated.
3235 Never set this variable using `setq' or so, because then it will apply
3236 to all future agenda commands. If you do want a global skipping condition,
3237 use the option `org-agenda-skip-function-global' instead.
3238 The correct usage for `org-agenda-skip-function' is to bind it with
3239 `let' to scope it dynamically into the agenda-constructing command.
3240 A good way to set it is through options in `org-agenda-custom-commands'.")
3242 (defun org-agenda-skip ()
3243 "Throw to `:skip' in places that should be skipped.
3244 Also moves point to the end of the skipped region, so that search can
3245 continue from there."
3246 (let ((p (point-at-bol)) to)
3247 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3248 (get-text-property p :org-archived)
3249 (org-end-of-subtree t)
3250 (throw :skip t))
3251 (and org-agenda-skip-comment-trees
3252 (get-text-property p :org-comment)
3253 (org-end-of-subtree t)
3254 (throw :skip t))
3255 (if (equal (char-after p) ?#) (throw :skip t))
3256 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3257 (org-agenda-skip-eval org-agenda-skip-function)))
3258 (goto-char to)
3259 (throw :skip t))))
3261 (defun org-agenda-skip-eval (form)
3262 "If FORM is a function or a list, call (or eval) is and return result.
3263 `save-excursion' and `save-match-data' are wrapped around the call, so point
3264 and match data are returned to the previous state no matter what these
3265 functions do."
3266 (let (fp)
3267 (and form
3268 (or (setq fp (functionp form))
3269 (consp form))
3270 (save-excursion
3271 (save-match-data
3272 (if fp
3273 (funcall form)
3274 (eval form)))))))
3276 (defvar org-agenda-markers nil
3277 "List of all currently active markers created by `org-agenda'.")
3278 (defvar org-agenda-last-marker-time (org-float-time)
3279 "Creation time of the last agenda marker.")
3281 (defun org-agenda-new-marker (&optional pos)
3282 "Return a new agenda marker.
3283 Org-mode keeps a list of these markers and resets them when they are
3284 no longer in use."
3285 (let ((m (copy-marker (or pos (point)))))
3286 (setq org-agenda-last-marker-time (org-float-time))
3287 (push m org-agenda-markers)
3290 (defun org-agenda-reset-markers ()
3291 "Reset markers created by `org-agenda'."
3292 (while org-agenda-markers
3293 (move-marker (pop org-agenda-markers) nil)))
3295 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3296 "Save relative positions of markers in region."
3297 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3298 org-agenda-markers))
3300 ;;; Entry text mode
3302 (defun org-agenda-entry-text-show-here ()
3303 "Add some text from the entry as context to the current line."
3304 (let (m txt o)
3305 (setq m (org-get-at-bol 'org-hd-marker))
3306 (unless (marker-buffer m)
3307 (error "No marker points to an entry here"))
3308 (setq txt (concat "\n" (org-no-properties
3309 (org-agenda-get-some-entry-text
3310 m org-agenda-entry-text-maxlines " > "))))
3311 (when (string-match "\\S-" txt)
3312 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3313 (overlay-put o 'evaporate t)
3314 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3315 (overlay-put o 'after-string txt))))
3317 (defun org-agenda-entry-text-show ()
3318 "Add entry context for all agenda lines."
3319 (interactive)
3320 (save-excursion
3321 (goto-char (point-max))
3322 (beginning-of-line 1)
3323 (while (not (bobp))
3324 (when (org-get-at-bol 'org-hd-marker)
3325 (org-agenda-entry-text-show-here))
3326 (beginning-of-line 0))))
3328 (defun org-agenda-entry-text-hide ()
3329 "Remove any shown entry context."
3330 (delq nil
3331 (mapcar (lambda (o)
3332 (if (eq (overlay-get o 'org-overlay-type)
3333 'agenda-entry-content)
3334 (progn (delete-overlay o) t)))
3335 (overlays-in (point-min) (point-max)))))
3337 (defun org-agenda-get-day-face (date)
3338 "Return the face DATE should be displayed with."
3339 (or (and (functionp org-agenda-day-face-function)
3340 (funcall org-agenda-day-face-function date))
3341 (cond ((org-agenda-todayp date)
3342 'org-agenda-date-today)
3343 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3344 'org-agenda-date-weekend)
3345 (t 'org-agenda-date))))
3347 ;;; Agenda timeline
3349 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3351 (defun org-timeline (&optional include-all)
3352 "Show a time-sorted view of the entries in the current org file.
3353 Only entries with a time stamp of today or later will be listed. With
3354 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3355 under the current date.
3356 If the buffer contains an active region, only check the region for
3357 dates."
3358 (interactive "P")
3359 (org-compile-prefix-format 'timeline)
3360 (org-set-sorting-strategy 'timeline)
3361 (let* ((dopast t)
3362 (dotodo include-all)
3363 (doclosed org-agenda-show-log)
3364 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3365 (current-buffer))))
3366 (date (calendar-current-date))
3367 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3368 (end (if (org-region-active-p) (region-end) (point-max)))
3369 (day-numbers (org-get-all-dates beg end 'no-ranges
3370 t doclosed ; always include today
3371 org-timeline-show-empty-dates))
3372 (org-deadline-warning-days 0)
3373 (org-agenda-only-exact-dates t)
3374 (today (org-today))
3375 (past t)
3376 args
3377 s e rtn d emptyp)
3378 (setq org-agenda-redo-command
3379 (list 'progn
3380 (list 'org-switch-to-buffer-other-window (current-buffer))
3381 (list 'org-timeline (list 'quote include-all))))
3382 (if (not dopast)
3383 ;; Remove past dates from the list of dates.
3384 (setq day-numbers (delq nil (mapcar (lambda(x)
3385 (if (>= x today) x nil))
3386 day-numbers))))
3387 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3388 (if doclosed (push :closed args))
3389 (push :timestamp args)
3390 (push :deadline args)
3391 (push :scheduled args)
3392 (push :sexp args)
3393 (if dotodo (push :todo args))
3394 (insert "Timeline of file " entry "\n")
3395 (add-text-properties (point-min) (point)
3396 (list 'face 'org-agenda-structure))
3397 (org-agenda-mark-header-line (point-min))
3398 (while (setq d (pop day-numbers))
3399 (if (and (listp d) (eq (car d) :omitted))
3400 (progn
3401 (setq s (point))
3402 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3403 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3404 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3405 (if (and (>= d today)
3406 dopast
3407 past)
3408 (progn
3409 (setq past nil)
3410 (insert (make-string 79 ?-) "\n")))
3411 (setq date (calendar-gregorian-from-absolute d))
3412 (setq s (point))
3413 (setq rtn (and (not emptyp)
3414 (apply 'org-agenda-get-day-entries entry
3415 date args)))
3416 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3417 (progn
3418 (insert
3419 (if (stringp org-agenda-format-date)
3420 (format-time-string org-agenda-format-date
3421 (org-time-from-absolute date))
3422 (funcall org-agenda-format-date date))
3423 "\n")
3424 (put-text-property s (1- (point)) 'face
3425 (org-agenda-get-day-face date))
3426 (put-text-property s (1- (point)) 'org-date-line t)
3427 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3428 (if (equal d today)
3429 (put-text-property s (1- (point)) 'org-today t))
3430 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3431 (put-text-property s (1- (point)) 'day d)))))
3432 (goto-char (point-min))
3433 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3434 (point-min)))
3435 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3436 (org-finalize-agenda)
3437 (setq buffer-read-only t)))
3439 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3440 "Return a list of all relevant day numbers from BEG to END buffer positions.
3441 If NO-RANGES is non-nil, include only the start and end dates of a range,
3442 not every single day in the range. If FORCE-TODAY is non-nil, make
3443 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3444 inactive time stamps (those in square brackets) are included.
3445 When EMPTY is non-nil, also include days without any entries."
3446 (let ((re (concat
3447 (if pre-re pre-re "")
3448 (if inactive org-ts-regexp-both org-ts-regexp)))
3449 dates dates1 date day day1 day2 ts1 ts2)
3450 (if force-today
3451 (setq dates (list (org-today))))
3452 (save-excursion
3453 (goto-char beg)
3454 (while (re-search-forward re end t)
3455 (setq day (time-to-days (org-time-string-to-time
3456 (substring (match-string 1) 0 10))))
3457 (or (memq day dates) (push day dates)))
3458 (unless no-ranges
3459 (goto-char beg)
3460 (while (re-search-forward org-tr-regexp end t)
3461 (setq ts1 (substring (match-string 1) 0 10)
3462 ts2 (substring (match-string 2) 0 10)
3463 day1 (time-to-days (org-time-string-to-time ts1))
3464 day2 (time-to-days (org-time-string-to-time ts2)))
3465 (while (< (setq day1 (1+ day1)) day2)
3466 (or (memq day1 dates) (push day1 dates)))))
3467 (setq dates (sort dates '<))
3468 (when empty
3469 (while (setq day (pop dates))
3470 (setq day2 (car dates))
3471 (push day dates1)
3472 (when (and day2 empty)
3473 (if (or (eq empty t)
3474 (and (numberp empty) (<= (- day2 day) empty)))
3475 (while (< (setq day (1+ day)) day2)
3476 (push (list day) dates1))
3477 (push (cons :omitted (- day2 day)) dates1))))
3478 (setq dates (nreverse dates1)))
3479 dates)))
3481 ;;; Agenda Daily/Weekly
3483 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3484 "Start day for the agenda view.
3485 Custom commands can set this variable in the options section.")
3486 (defvar org-starting-day nil) ; local variable in the agenda buffer
3487 (defvar org-agenda-current-span nil
3488 "The current span used in the agenda view.") ; local variable in the agenda buffer
3489 (defvar org-include-all-loc nil) ; local variable
3491 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3492 "List of types searched for when creating the daily/weekly agenda.
3493 This variable is a list of symbols that controls the types of
3494 items that appear in the daily/weekly agenda. Allowed symbols in this
3495 list are are
3497 :timestamp List items containing a date stamp or date range matching
3498 the selected date. This includes sexp entries in
3499 angular brackets.
3501 :sexp List entries resulting from plain diary-like sexps.
3503 :deadline List deadline due on that date. When the date is today,
3504 also list any deadlines past due, or due within
3505 `org-deadline-warning-days'. `:deadline' must appear before
3506 `:scheduled' if the setting of
3507 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3508 any effect.
3510 :scheduled List all items which are scheduled for the given date.
3511 The diary for *today* also contains items which were
3512 scheduled earlier and are not yet marked DONE.
3514 By default, all four types are turned on.
3516 Never set this variable globally using `setq', because then it
3517 will apply to all future agenda commands. Instead, bind it with
3518 `let' to scope it dynamically into the agenda-constructing
3519 command. A good way to set it is through options in
3520 `org-agenda-custom-commands'. For a more flexible (though
3521 somewhat less efficient) way of determining what is included in
3522 the daily/weekly agenda, see `org-agenda-skip-function'.")
3524 ;;;###autoload
3525 (defun org-agenda-list (&optional include-all start-day span)
3526 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3527 The view will be for the current day or week, but from the overview buffer
3528 you will be able to go to other days/weeks.
3530 With a numeric prefix argument in an interactive call, the agenda will
3531 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3532 the number of days. SPAN defaults to `org-agenda-span'.
3534 START-DAY defaults to TODAY, or to the most recent match for the weekday
3535 given in `org-agenda-start-on-weekday'."
3536 (interactive "P")
3537 (if (and (integerp include-all) (> include-all 0))
3538 (setq span include-all include-all nil))
3539 (setq start-day (or start-day org-agenda-start-day))
3540 (if org-agenda-overriding-arguments
3541 (setq include-all (car org-agenda-overriding-arguments)
3542 start-day (nth 1 org-agenda-overriding-arguments)
3543 span (nth 2 org-agenda-overriding-arguments)))
3544 (if (stringp start-day)
3545 ;; Convert to an absolute day number
3546 (setq start-day (time-to-days (org-read-date nil t start-day))))
3547 (setq org-agenda-last-arguments (list include-all start-day span))
3548 (org-compile-prefix-format 'agenda)
3549 (org-set-sorting-strategy 'agenda)
3550 (let* ((span (org-agenda-ndays-to-span
3551 (or span org-agenda-ndays org-agenda-span)))
3552 (today (org-today))
3553 (sd (or start-day today))
3554 (ndays (org-agenda-span-to-ndays span sd))
3555 (org-agenda-start-on-weekday
3556 (if (eq ndays 7)
3557 org-agenda-start-on-weekday))
3558 (thefiles (org-agenda-files nil 'ifmode))
3559 (files thefiles)
3560 (start (if (or (null org-agenda-start-on-weekday)
3561 (< ndays 7))
3563 (let* ((nt (calendar-day-of-week
3564 (calendar-gregorian-from-absolute sd)))
3565 (n1 org-agenda-start-on-weekday)
3566 (d (- nt n1)))
3567 (- sd (+ (if (< d 0) 7 0) d)))))
3568 (day-numbers (list start))
3569 (day-cnt 0)
3570 (inhibit-redisplay (not debug-on-error))
3571 s e rtn rtnall file date d start-pos end-pos todayp
3572 clocktable-start clocktable-end filter)
3573 (setq org-agenda-redo-command
3574 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3575 (dotimes (n (1- ndays))
3576 (push (1+ (car day-numbers)) day-numbers))
3577 (setq day-numbers (nreverse day-numbers))
3578 (setq clocktable-start (car day-numbers)
3579 clocktable-end (1+ (or (org-last day-numbers) 0)))
3580 (org-prepare-agenda "Day/Week")
3581 (org-set-local 'org-starting-day (car day-numbers))
3582 (org-set-local 'org-include-all-loc include-all)
3583 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3584 (unless org-agenda-compact-blocks
3585 (let* ((d1 (car day-numbers))
3586 (d2 (org-last day-numbers))
3587 (w1 (org-days-to-iso-week d1))
3588 (w2 (org-days-to-iso-week d2)))
3589 (setq s (point))
3590 (if org-agenda-overriding-header
3591 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3592 nil 'face 'org-agenda-structure) "\n")
3593 (insert (org-agenda-span-name span)
3594 "-agenda"
3595 (if (< (- d2 d1) 350)
3596 (if (= w1 w2)
3597 (format " (W%02d)" w1)
3598 (format " (W%02d-W%02d)" w1 w2))
3600 ":\n")))
3601 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3602 'org-date-line t))
3603 (org-agenda-mark-header-line s))
3604 (while (setq d (pop day-numbers))
3605 (setq date (calendar-gregorian-from-absolute d)
3606 s (point))
3607 (if (or (setq todayp (= d today))
3608 (and (not start-pos) (= d sd)))
3609 (setq start-pos (point))
3610 (if (and start-pos (not end-pos))
3611 (setq end-pos (point))))
3612 (setq files thefiles
3613 rtnall nil)
3614 (while (setq file (pop files))
3615 (catch 'nextfile
3616 (org-check-agenda-file file)
3617 (let ((org-agenda-entry-types org-agenda-entry-types))
3618 (unless org-agenda-include-deadlines
3619 (setq org-agenda-entry-types
3620 (delq :deadline org-agenda-entry-types)))
3621 (cond
3622 ((memq org-agenda-show-log '(only clockcheck))
3623 (setq rtn (org-agenda-get-day-entries
3624 file date :closed)))
3625 (org-agenda-show-log
3626 (setq rtn (apply 'org-agenda-get-day-entries
3627 file date
3628 (append '(:closed) org-agenda-entry-types))))
3630 (setq rtn (apply 'org-agenda-get-day-entries
3631 file date
3632 org-agenda-entry-types)))))
3633 (setq rtnall (append rtnall rtn))))
3634 (if org-agenda-include-diary
3635 (let ((org-agenda-search-headline-for-time t))
3636 (require 'diary-lib)
3637 (setq rtn (org-get-entries-from-diary date))
3638 (setq rtnall (append rtnall rtn))))
3639 (if (or rtnall org-agenda-show-all-dates)
3640 (progn
3641 (setq day-cnt (1+ day-cnt))
3642 (insert
3643 (if (stringp org-agenda-format-date)
3644 (format-time-string org-agenda-format-date
3645 (org-time-from-absolute date))
3646 (funcall org-agenda-format-date date))
3647 "\n")
3648 (put-text-property s (1- (point)) 'face
3649 (org-agenda-get-day-face date))
3650 (put-text-property s (1- (point)) 'org-date-line t)
3651 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3652 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3653 (when todayp
3654 (put-text-property s (1- (point)) 'org-today t))
3655 (if rtnall (insert
3656 (org-finalize-agenda-entries
3657 (org-agenda-add-time-grid-maybe
3658 rtnall ndays todayp))
3659 "\n"))
3660 (put-text-property s (1- (point)) 'day d)
3661 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3662 (when (and org-agenda-clockreport-mode clocktable-start)
3663 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3664 ;; the above line is to ensure the restricted range!
3665 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3666 tbl)
3667 (setq p (org-plist-delete p :block))
3668 (setq p (plist-put p :tstart clocktable-start))
3669 (setq p (plist-put p :tend clocktable-end))
3670 (setq p (plist-put p :scope 'agenda))
3671 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3672 (setq filter (or org-agenda-filter-while-redo
3673 (get 'org-agenda-filter :preset-filter))))
3674 (setq p (plist-put p :tags (mapconcat (lambda (x)
3675 (if (string-match "[<>=]" x)
3678 filter ""))))
3679 (setq tbl (apply 'org-get-clocktable p))
3680 (insert tbl)))
3681 (goto-char (point-min))
3682 (or org-agenda-multi (org-fit-agenda-window))
3683 (unless (and (pos-visible-in-window-p (point-min))
3684 (pos-visible-in-window-p (point-max)))
3685 (goto-char (1- (point-max)))
3686 (recenter -1)
3687 (if (not (pos-visible-in-window-p (or start-pos 1)))
3688 (progn
3689 (goto-char (or start-pos 1))
3690 (recenter 1))))
3691 (goto-char (or start-pos 1))
3692 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3693 (if (eq org-agenda-show-log 'clockcheck)
3694 (org-agenda-show-clocking-issues))
3695 (org-finalize-agenda)
3696 (setq buffer-read-only t)
3697 (message "")))
3699 (defun org-agenda-ndays-to-span (n)
3700 "Return a span symbol for a span of N days, or N if none matches."
3701 (cond ((symbolp n) n)
3702 ((= n 1) 'day)
3703 ((= n 7) 'week)
3704 (t n)))
3706 (defun org-agenda-span-to-ndays (span start-day)
3707 "Return ndays from SPAN starting at START-DAY."
3708 (cond ((numberp span) span)
3709 ((eq span 'day) 1)
3710 ((eq span 'week) 7)
3711 ((eq span 'month)
3712 (let ((date (calendar-gregorian-from-absolute start-day)))
3713 (calendar-last-day-of-month (car date) (caddr date))))
3714 ((eq span 'year)
3715 (let ((date (calendar-gregorian-from-absolute start-day)))
3716 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3718 (defun org-agenda-span-name (span)
3719 "Return a SPAN name."
3720 (if (null span)
3722 (if (symbolp span)
3723 (capitalize (symbol-name span))
3724 (format "%d days" span))))
3726 ;;; Agenda word search
3728 (defvar org-agenda-search-history nil)
3729 (defvar org-todo-only nil)
3731 (defvar org-search-syntax-table nil
3732 "Special syntax table for org-mode search.
3733 In this table, we have single quotes not as word constituents, to
3734 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3736 (defun org-search-syntax-table ()
3737 (unless org-search-syntax-table
3738 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3739 (modify-syntax-entry ?' "." org-search-syntax-table)
3740 (modify-syntax-entry ?` "." org-search-syntax-table))
3741 org-search-syntax-table)
3743 (defvar org-agenda-last-search-view-search-was-boolean nil)
3745 ;;;###autoload
3746 (defun org-search-view (&optional todo-only string edit-at)
3747 "Show all entries that contain a phrase or words or regular expressions.
3749 With optional prefix argument TODO-ONLY, only consider entries that are
3750 TODO entries. The argument STRING can be used to pass a default search
3751 string into this function. If EDIT-AT is non-nil, it means that the
3752 user should get a chance to edit this string, with cursor at position
3753 EDIT-AT.
3755 The search string can be viewed either as a phrase that should be found as
3756 is, or it can be broken into a number of snippets, each of which must match
3757 in a Boolean way to select an entry. The default depends on the variable
3758 `org-agenda-search-view-always-boolean'.
3759 Even if this is turned off (the default) you can always switch to
3760 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3762 The default is a direct search of the whole phrase, where each space in
3763 the search string can expand to an arbitrary amount of whitespace,
3764 including newlines.
3766 If using a Boolean search, the search string is split on whitespace and
3767 each snippet is searched separately, with logical AND to select an entry.
3768 Words prefixed with a minus must *not* occur in the entry. Words without
3769 a prefix or prefixed with a plus must occur in the entry. Matching is
3770 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3771 match whole words, not parts of a word) if
3772 `org-agenda-search-view-force-full-words' is set (default is nil).
3774 Boolean search snippets enclosed by curly braces are interpreted as
3775 regular expressions that must or (when preceded with \"-\") must not
3776 match in the entry. Snippets enclosed into double quotes will be taken
3777 as a whole, to include whitespace.
3779 - If the search string starts with an asterisk, search only in headlines.
3780 - If (possibly after the leading star) the search string starts with an
3781 exclamation mark, this also means to look at TODO entries only, an effect
3782 that can also be achieved with a prefix argument.
3783 - If (possibly after star and exclamation mark) the search string starts
3784 with a colon, this will mean that the (non-regexp) snippets of the
3785 Boolean search must match as full words.
3787 This command searches the agenda files, and in addition the files listed
3788 in `org-agenda-text-search-extra-files'."
3789 (interactive "P")
3790 (org-compile-prefix-format 'search)
3791 (org-set-sorting-strategy 'search)
3792 (org-prepare-agenda "SEARCH")
3793 (let* ((props (list 'face nil
3794 'done-face 'org-agenda-done
3795 'org-not-done-regexp org-not-done-regexp
3796 'org-todo-regexp org-todo-regexp
3797 'org-complex-heading-regexp org-complex-heading-regexp
3798 'mouse-face 'highlight
3799 'help-echo (format "mouse-2 or RET jump to location")))
3800 (full-words org-agenda-search-view-force-full-words)
3801 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3802 regexp rtn rtnall files file pos
3803 marker category tags c neg re boolean
3804 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3805 (unless (and (not edit-at)
3806 (stringp string)
3807 (string-match "\\S-" string))
3808 (setq string (read-string
3809 (if org-agenda-search-view-always-boolean
3810 "[+-]Word/{Regexp} ...: "
3811 "Phrase, or [+-]Word/{Regexp} ...: ")
3812 (cond
3813 ((integerp edit-at) (cons string edit-at))
3814 (edit-at string))
3815 'org-agenda-search-history)))
3816 (org-set-local 'org-todo-only todo-only)
3817 (setq org-agenda-redo-command
3818 (list 'org-search-view (if todo-only t nil) string
3819 '(if current-prefix-arg 1 nil)))
3820 (setq org-agenda-query-string string)
3822 (if (equal (string-to-char string) ?*)
3823 (setq hdl-only t
3824 words (substring string 1))
3825 (setq words string))
3826 (when (equal (string-to-char words) ?!)
3827 (setq todo-only t
3828 words (substring words 1)))
3829 (when (equal (string-to-char words) ?:)
3830 (setq full-words t
3831 words (substring words 1)))
3832 (if (or org-agenda-search-view-always-boolean
3833 (member (string-to-char words) '(?- ?+ ?\{)))
3834 (setq boolean t))
3835 (setq words (org-split-string words))
3836 (let (www w)
3837 (while (setq w (pop words))
3838 (while (and (string-match "\\\\\\'" w) words)
3839 (setq w (concat (substring w 0 -1) " " (pop words))))
3840 (push w www))
3841 (setq words (nreverse www) www nil)
3842 (while (setq w (pop words))
3843 (when (and (string-match "\\`[-+]?{" w)
3844 (not (string-match "}\\'" w)))
3845 (while (and words (not (string-match "}\\'" (car words))))
3846 (setq w (concat w " " (pop words))))
3847 (setq w (concat w " " (pop words))))
3848 (push w www))
3849 (setq words (nreverse www)))
3850 (setq org-agenda-last-search-view-search-was-boolean boolean)
3851 (when boolean
3852 (let (wds w)
3853 (while (setq w (pop words))
3854 (if (or (equal (substring w 0 1) "\"")
3855 (and (> (length w) 1)
3856 (member (substring w 0 1) '("+" "-"))
3857 (equal (substring w 1 2) "\"")))
3858 (while (and words (not (equal (substring w -1) "\"")))
3859 (setq w (concat w " " (pop words)))))
3860 (and (string-match "\\`\\([-+]?\\)\"" w)
3861 (setq w (replace-match "\\1" nil nil w)))
3862 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3863 (push w wds))
3864 (setq words (nreverse wds))))
3865 (if boolean
3866 (mapc (lambda (w)
3867 (setq c (string-to-char w))
3868 (if (equal c ?-)
3869 (setq neg t w (substring w 1))
3870 (if (equal c ?+)
3871 (setq neg nil w (substring w 1))
3872 (setq neg nil)))
3873 (if (string-match "\\`{.*}\\'" w)
3874 (setq re (substring w 1 -1))
3875 (if full-words
3876 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3877 (setq re (regexp-quote (downcase w)))))
3878 (if neg (push re regexps-) (push re regexps+)))
3879 words)
3880 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3881 regexps+))
3882 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3883 (if (not regexps+)
3884 (setq regexp org-outline-regexp-bol)
3885 (setq regexp (pop regexps+))
3886 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3887 regexp))))
3888 (setq files (org-agenda-files nil 'ifmode))
3889 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3890 (pop org-agenda-text-search-extra-files)
3891 (setq files (org-add-archive-files files)))
3892 (setq files (append files org-agenda-text-search-extra-files)
3893 rtnall nil)
3894 (while (setq file (pop files))
3895 (setq ee nil)
3896 (catch 'nextfile
3897 (org-check-agenda-file file)
3898 (setq buffer (if (file-exists-p file)
3899 (org-get-agenda-file-buffer file)
3900 (error "No such file %s" file)))
3901 (if (not buffer)
3902 ;; If file does not exist, make sure an error message is sent
3903 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3904 file))))
3905 (with-current-buffer buffer
3906 (with-syntax-table (org-search-syntax-table)
3907 (unless (org-mode-p)
3908 (error "Agenda file %s is not in `org-mode'" file))
3909 (let ((case-fold-search t))
3910 (save-excursion
3911 (save-restriction
3912 (if org-agenda-restrict
3913 (narrow-to-region org-agenda-restrict-begin
3914 org-agenda-restrict-end)
3915 (widen))
3916 (goto-char (point-min))
3917 (unless (or (org-on-heading-p)
3918 (outline-next-heading))
3919 (throw 'nextfile t))
3920 (goto-char (max (point-min) (1- (point))))
3921 (while (re-search-forward regexp nil t)
3922 (org-back-to-heading t)
3923 (skip-chars-forward "* ")
3924 (setq beg (point-at-bol)
3925 beg1 (point)
3926 end (progn (outline-next-heading) (point)))
3927 (catch :skip
3928 (goto-char beg)
3929 (org-agenda-skip)
3930 (setq str (buffer-substring-no-properties
3931 (point-at-bol)
3932 (if hdl-only (point-at-eol) end)))
3933 (mapc (lambda (wr) (when (string-match wr str)
3934 (goto-char (1- end))
3935 (throw :skip t)))
3936 regexps-)
3937 (mapc (lambda (wr) (unless (string-match wr str)
3938 (goto-char (1- end))
3939 (throw :skip t)))
3940 (if todo-only
3941 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3942 regexps+)
3943 regexps+))
3944 (goto-char beg)
3945 (setq marker (org-agenda-new-marker (point))
3946 category (org-get-category)
3947 tags (org-get-tags-at (point))
3948 txt (org-format-agenda-item
3950 (buffer-substring-no-properties
3951 beg1 (point-at-eol))
3952 category tags))
3953 (org-add-props txt props
3954 'org-marker marker 'org-hd-marker marker
3955 'org-todo-regexp org-todo-regexp
3956 'org-complex-heading-regexp org-complex-heading-regexp
3957 'priority 1000 'org-category category
3958 'type "search")
3959 (push txt ee)
3960 (goto-char (1- end))))))))))
3961 (setq rtn (nreverse ee))
3962 (setq rtnall (append rtnall rtn)))
3963 (if org-agenda-overriding-header
3964 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3965 nil 'face 'org-agenda-structure) "\n")
3966 (insert "Search words: ")
3967 (add-text-properties (point-min) (1- (point))
3968 (list 'face 'org-agenda-structure))
3969 (setq pos (point))
3970 (insert string "\n")
3971 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3972 (setq pos (point))
3973 (unless org-agenda-multi
3974 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3975 (add-text-properties pos (1- (point))
3976 (list 'face 'org-agenda-structure))))
3977 (org-agenda-mark-header-line (point-min))
3978 (when rtnall
3979 (insert (org-finalize-agenda-entries rtnall) "\n"))
3980 (goto-char (point-min))
3981 (or org-agenda-multi (org-fit-agenda-window))
3982 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3983 (org-finalize-agenda)
3984 (setq buffer-read-only t)))
3986 ;;; Agenda TODO list
3988 (defvar org-select-this-todo-keyword nil)
3989 (defvar org-last-arg nil)
3991 ;;;###autoload
3992 (defun org-todo-list (arg)
3993 "Show all (not done) TODO entries from all agenda file in a single list.
3994 The prefix arg can be used to select a specific TODO keyword and limit
3995 the list to these. When using \\[universal-argument], you will be prompted
3996 for a keyword. A numeric prefix directly selects the Nth keyword in
3997 `org-todo-keywords-1'."
3998 (interactive "P")
3999 (org-compile-prefix-format 'todo)
4000 (org-set-sorting-strategy 'todo)
4001 (org-prepare-agenda "TODO")
4002 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4003 (let* ((today (org-today))
4004 (date (calendar-gregorian-from-absolute today))
4005 (kwds org-todo-keywords-for-agenda)
4006 (completion-ignore-case t)
4007 (org-select-this-todo-keyword
4008 (if (stringp arg) arg
4009 (and arg (integerp arg) (> arg 0)
4010 (nth (1- arg) kwds))))
4011 rtn rtnall files file pos)
4012 (when (equal arg '(4))
4013 (setq org-select-this-todo-keyword
4014 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4015 (mapcar 'list kwds) nil nil)))
4016 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4017 (org-set-local 'org-last-arg arg)
4018 (setq org-agenda-redo-command
4019 '(org-todo-list (or current-prefix-arg org-last-arg)))
4020 (setq files (org-agenda-files nil 'ifmode)
4021 rtnall nil)
4022 (while (setq file (pop files))
4023 (catch 'nextfile
4024 (org-check-agenda-file file)
4025 (setq rtn (org-agenda-get-day-entries file date :todo))
4026 (setq rtnall (append rtnall rtn))))
4027 (if org-agenda-overriding-header
4028 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4029 nil 'face 'org-agenda-structure) "\n")
4030 (insert "Global list of TODO items of type: ")
4031 (add-text-properties (point-min) (1- (point))
4032 (list 'face 'org-agenda-structure
4033 'short-heading
4034 (concat "ToDo: "
4035 (or org-select-this-todo-keyword "ALL"))))
4036 (org-agenda-mark-header-line (point-min))
4037 (setq pos (point))
4038 (insert (or org-select-this-todo-keyword "ALL") "\n")
4039 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4040 (setq pos (point))
4041 (unless org-agenda-multi
4042 (insert "Available with `N r': (0)ALL")
4043 (let ((n 0) s)
4044 (mapc (lambda (x)
4045 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4046 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4047 (insert "\n "))
4048 (insert " " s))
4049 kwds))
4050 (insert "\n"))
4051 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4052 (org-agenda-mark-header-line (point-min))
4053 (when rtnall
4054 (insert (org-finalize-agenda-entries rtnall) "\n"))
4055 (goto-char (point-min))
4056 (or org-agenda-multi (org-fit-agenda-window))
4057 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4058 (org-finalize-agenda)
4059 (setq buffer-read-only t)))
4061 ;;; Agenda tags match
4063 ;;;###autoload
4064 (defun org-tags-view (&optional todo-only match)
4065 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4066 The prefix arg TODO-ONLY limits the search to TODO entries."
4067 (interactive "P")
4068 (org-compile-prefix-format 'tags)
4069 (org-set-sorting-strategy 'tags)
4070 (let* ((org-tags-match-list-sublevels
4071 org-tags-match-list-sublevels)
4072 (completion-ignore-case t)
4073 rtn rtnall files file pos matcher
4074 buffer)
4075 (when (and (stringp match) (not (string-match "\\S-" match)))
4076 (setq match nil))
4077 (setq matcher (org-make-tags-matcher match)
4078 match (car matcher) matcher (cdr matcher))
4079 (org-prepare-agenda (concat "TAGS " match))
4080 (setq org-agenda-query-string match)
4081 (setq org-agenda-redo-command
4082 (list 'org-tags-view (list 'quote todo-only)
4083 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4084 (setq files (org-agenda-files nil 'ifmode)
4085 rtnall nil)
4086 (while (setq file (pop files))
4087 (catch 'nextfile
4088 (org-check-agenda-file file)
4089 (setq buffer (if (file-exists-p file)
4090 (org-get-agenda-file-buffer file)
4091 (error "No such file %s" file)))
4092 (if (not buffer)
4093 ;; If file does not exist, error message to agenda
4094 (setq rtn (list
4095 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4096 rtnall (append rtnall rtn))
4097 (with-current-buffer buffer
4098 (unless (org-mode-p)
4099 (error "Agenda file %s is not in `org-mode'" file))
4100 (save-excursion
4101 (save-restriction
4102 (if org-agenda-restrict
4103 (narrow-to-region org-agenda-restrict-begin
4104 org-agenda-restrict-end)
4105 (widen))
4106 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4107 (setq rtnall (append rtnall rtn))))))))
4108 (if org-agenda-overriding-header
4109 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4110 nil 'face 'org-agenda-structure) "\n")
4111 (insert "Headlines with TAGS match: ")
4112 (add-text-properties (point-min) (1- (point))
4113 (list 'face 'org-agenda-structure
4114 'short-heading
4115 (concat "Match: " match)))
4116 (setq pos (point))
4117 (insert match "\n")
4118 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4119 (setq pos (point))
4120 (unless org-agenda-multi
4121 (insert "Press `C-u r' to search again with new search string\n"))
4122 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4123 (org-agenda-mark-header-line (point-min))
4124 (when rtnall
4125 (insert (org-finalize-agenda-entries rtnall) "\n"))
4126 (goto-char (point-min))
4127 (or org-agenda-multi (org-fit-agenda-window))
4128 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4129 (org-finalize-agenda)
4130 (setq buffer-read-only t)))
4132 ;;; Agenda Finding stuck projects
4134 (defvar org-agenda-skip-regexp nil
4135 "Regular expression used in skipping subtrees for the agenda.
4136 This is basically a temporary global variable that can be set and then
4137 used by user-defined selections using `org-agenda-skip-function'.")
4139 (defvar org-agenda-overriding-header nil
4140 "When set during agenda, todo and tags searches it replaces the header.
4141 This variable should not be set directly, but custom commands can bind it
4142 in the options section.")
4144 (defun org-agenda-skip-entry-when-regexp-matches ()
4145 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4146 If yes, it returns the end position of this entry, causing agenda commands
4147 to skip the entry but continuing the search in the subtree. This is a
4148 function that can be put into `org-agenda-skip-function' for the duration
4149 of a command."
4150 (let ((end (save-excursion (org-end-of-subtree t)))
4151 skip)
4152 (save-excursion
4153 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4154 (and skip end)))
4156 (defun org-agenda-skip-subtree-when-regexp-matches ()
4157 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4158 If yes, it returns the end position of this tree, causing agenda commands
4159 to skip this subtree. This is a function that can be put into
4160 `org-agenda-skip-function' for the duration of a command."
4161 (let ((end (save-excursion (org-end-of-subtree t)))
4162 skip)
4163 (save-excursion
4164 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4165 (and skip end)))
4167 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4168 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4169 If yes, it returns the end position of the current entry (NOT the tree),
4170 causing agenda commands to skip the entry but continuing the search in
4171 the subtree. This is a function that can be put into
4172 `org-agenda-skip-function' for the duration of a command. An important
4173 use of this function is for the stuck project list."
4174 (let ((end (save-excursion (org-end-of-subtree t)))
4175 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4176 skip)
4177 (save-excursion
4178 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4179 (and skip entry-end)))
4181 (defun org-agenda-skip-entry-if (&rest conditions)
4182 "Skip entry if any of CONDITIONS is true.
4183 See `org-agenda-skip-if' for details."
4184 (org-agenda-skip-if nil conditions))
4186 (defun org-agenda-skip-subtree-if (&rest conditions)
4187 "Skip entry if any of CONDITIONS is true.
4188 See `org-agenda-skip-if' for details."
4189 (org-agenda-skip-if t conditions))
4191 (defun org-agenda-skip-if (subtree conditions)
4192 "Checks current entity for CONDITIONS.
4193 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4194 the entry, i.e. the text before the next heading is checked.
4196 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4197 from different tests. Valid conditions are:
4199 scheduled Check if there is a scheduled cookie
4200 notscheduled Check if there is no scheduled cookie
4201 deadline Check if there is a deadline
4202 notdeadline Check if there is no deadline
4203 timestamp Check if there is a timestamp (also deadline or scheduled)
4204 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4205 regexp Check if regexp matches
4206 notregexp Check if regexp does not match.
4207 todo Check if TODO keyword matches
4208 nottodo Check if TODO keyword does not match
4210 The regexp is taken from the conditions list, it must come right after
4211 the `regexp' or `notregexp' element.
4213 `todo' and `nottodo' accept as an argument a list of todo
4214 keywords, which may include \"*\" to match any todo keyword.
4216 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4218 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4220 Instead of a list a keyword class may be given
4222 (org-agenda-skip-entry-if 'nottodo 'done)
4224 would skip entries that haven't been marked with any of \"DONE\"
4225 keywords. Possible classes are: `todo', `done', `any'.
4227 If any of these conditions is met, this function returns the end point of
4228 the entity, causing the search to continue from there. This is a function
4229 that can be put into `org-agenda-skip-function' for the duration of a command."
4230 (let (beg end m)
4231 (org-back-to-heading t)
4232 (setq beg (point)
4233 end (if subtree
4234 (progn (org-end-of-subtree t) (point))
4235 (progn (outline-next-heading) (1- (point)))))
4236 (goto-char beg)
4237 (and
4239 (and (memq 'scheduled conditions)
4240 (re-search-forward org-scheduled-time-regexp end t))
4241 (and (memq 'notscheduled conditions)
4242 (not (re-search-forward org-scheduled-time-regexp end t)))
4243 (and (memq 'deadline conditions)
4244 (re-search-forward org-deadline-time-regexp end t))
4245 (and (memq 'notdeadline conditions)
4246 (not (re-search-forward org-deadline-time-regexp end t)))
4247 (and (memq 'timestamp conditions)
4248 (re-search-forward org-ts-regexp end t))
4249 (and (memq 'nottimestamp conditions)
4250 (not (re-search-forward org-ts-regexp end t)))
4251 (and (setq m (memq 'regexp conditions))
4252 (stringp (nth 1 m))
4253 (re-search-forward (nth 1 m) end t))
4254 (and (setq m (memq 'notregexp conditions))
4255 (stringp (nth 1 m))
4256 (not (re-search-forward (nth 1 m) end t)))
4257 (and (or
4258 (setq m (memq 'todo conditions))
4259 (setq m (memq 'nottodo conditions)))
4260 (org-agenda-skip-if-todo m end)))
4261 end)))
4263 (defun org-agenda-skip-if-todo (args end)
4264 "Helper function for `org-agenda-skip-if', do not use it directly.
4265 ARGS is a list with first element either `todo' or `nottodo'.
4266 The remainder is either a list of TODO keywords, or a state symbol
4267 `todo' or `done' or `any'."
4268 (let ((kw (car args))
4269 (arg (cadr args))
4270 todo-wds todo-re)
4271 (setq todo-wds
4272 (org-uniquify
4273 (cond
4274 ((listp arg) ;; list of keywords
4275 (if (member "*" arg)
4276 (mapcar 'substring-no-properties org-todo-keywords-1)
4277 arg))
4278 ((symbolp arg) ;; keyword class name
4279 (cond
4280 ((eq arg 'todo)
4281 (org-delete-all org-done-keywords
4282 (mapcar 'substring-no-properties
4283 org-todo-keywords-1)))
4284 ((eq arg 'done) org-done-keywords)
4285 ((eq arg 'any)
4286 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4287 (setq todo-re
4288 (concat "^\\*+[ \t]+\\<\\("
4289 (mapconcat 'identity todo-wds "\\|")
4290 "\\)\\>"))
4291 (if (eq kw 'todo)
4292 (re-search-forward todo-re end t)
4293 (not (re-search-forward todo-re end t)))))
4295 ;;;###autoload
4296 (defun org-agenda-list-stuck-projects (&rest ignore)
4297 "Create agenda view for projects that are stuck.
4298 Stuck projects are project that have no next actions. For the definitions
4299 of what a project is and how to check if it stuck, customize the variable
4300 `org-stuck-projects'."
4301 (interactive)
4302 (let* ((org-agenda-skip-function
4303 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4304 ;; We could have used org-agenda-skip-if here.
4305 (org-agenda-overriding-header
4306 (or org-agenda-overriding-header "List of stuck projects: "))
4307 (matcher (nth 0 org-stuck-projects))
4308 (todo (nth 1 org-stuck-projects))
4309 (todo-wds (if (member "*" todo)
4310 (progn
4311 (org-prepare-agenda-buffers (org-agenda-files
4312 nil 'ifmode))
4313 (org-delete-all
4314 org-done-keywords-for-agenda
4315 (copy-sequence org-todo-keywords-for-agenda)))
4316 todo))
4317 (todo-re (concat "^\\*+[ \t]+\\("
4318 (mapconcat 'identity todo-wds "\\|")
4319 "\\)\\>"))
4320 (tags (nth 2 org-stuck-projects))
4321 (tags-re (if (member "*" tags)
4322 (org-re (concat org-outline-regexp-bol
4323 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4324 (if tags
4325 (concat org-outline-regexp-bol
4326 ".*:\\("
4327 (mapconcat 'identity tags "\\|")
4328 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4329 (gen-re (nth 3 org-stuck-projects))
4330 (re-list
4331 (delq nil
4332 (list
4333 (if todo todo-re)
4334 (if tags tags-re)
4335 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4336 gen-re)))))
4337 (setq org-agenda-skip-regexp
4338 (if re-list
4339 (mapconcat 'identity re-list "\\|")
4340 (error "No information how to identify unstuck projects")))
4341 (org-tags-view nil matcher)
4342 (with-current-buffer org-agenda-buffer-name
4343 (setq org-agenda-redo-command
4344 '(org-agenda-list-stuck-projects
4345 (or current-prefix-arg org-last-arg))))))
4347 ;;; Diary integration
4349 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4350 (defvar list-diary-entries-hook)
4351 (defvar diary-time-regexp)
4352 (defun org-get-entries-from-diary (date)
4353 "Get the (Emacs Calendar) diary entries for DATE."
4354 (require 'diary-lib)
4355 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4356 (diary-display-hook '(fancy-diary-display))
4357 (diary-display-function 'fancy-diary-display)
4358 (pop-up-frames nil)
4359 (list-diary-entries-hook
4360 (cons 'org-diary-default-entry list-diary-entries-hook))
4361 (diary-file-name-prefix-function nil) ; turn this feature off
4362 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4363 entries
4364 (org-disable-agenda-to-diary t))
4365 (save-excursion
4366 (save-window-excursion
4367 (funcall (if (fboundp 'diary-list-entries)
4368 'diary-list-entries 'list-diary-entries)
4369 date 1)))
4370 (if (not (get-buffer diary-fancy-buffer))
4371 (setq entries nil)
4372 (with-current-buffer diary-fancy-buffer
4373 (setq buffer-read-only nil)
4374 (if (zerop (buffer-size))
4375 ;; No entries
4376 (setq entries nil)
4377 ;; Omit the date and other unnecessary stuff
4378 (org-agenda-cleanup-fancy-diary)
4379 ;; Add prefix to each line and extend the text properties
4380 (if (zerop (buffer-size))
4381 (setq entries nil)
4382 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4383 (setq entries
4384 (with-temp-buffer
4385 (insert entries) (goto-char (point-min))
4386 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4387 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4388 (replace-match (concat "; " (match-string 1)))))
4389 (buffer-string)))))
4390 (set-buffer-modified-p nil)
4391 (kill-buffer diary-fancy-buffer)))
4392 (when entries
4393 (setq entries (org-split-string entries "\n"))
4394 (setq entries
4395 (mapcar
4396 (lambda (x)
4397 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4398 ;; Extend the text properties to the beginning of the line
4399 (org-add-props x (text-properties-at (1- (length x)) x)
4400 'type "diary" 'date date 'face 'org-agenda-diary))
4401 entries)))))
4403 (defvar org-agenda-cleanup-fancy-diary-hook nil
4404 "Hook run when the fancy diary buffer is cleaned up.")
4406 (defun org-agenda-cleanup-fancy-diary ()
4407 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4408 This gets rid of the date, the underline under the date, and
4409 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4410 date. It also removes lines that contain only whitespace."
4411 (goto-char (point-min))
4412 (if (looking-at ".*?:[ \t]*")
4413 (progn
4414 (replace-match "")
4415 (re-search-forward "\n=+$" nil t)
4416 (replace-match "")
4417 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4418 (re-search-forward "\n=+$" nil t)
4419 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4420 (goto-char (point-min))
4421 (while (re-search-forward "^ +\n" nil t)
4422 (replace-match ""))
4423 (goto-char (point-min))
4424 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4425 (replace-match ""))
4426 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4428 ;; Make sure entries from the diary have the right text properties.
4429 (eval-after-load "diary-lib"
4430 '(if (boundp 'diary-modify-entry-list-string-function)
4431 ;; We can rely on the hook, nothing to do
4433 ;; Hook not available, must use advice to make this work
4434 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4435 "Make the position visible."
4436 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4437 (stringp string)
4438 buffer-file-name)
4439 (setq string (org-modify-diary-entry-string string))))))
4441 (defun org-modify-diary-entry-string (string)
4442 "Add text properties to string, allowing org-mode to act on it."
4443 (org-add-props string nil
4444 'mouse-face 'highlight
4445 'help-echo (if buffer-file-name
4446 (format "mouse-2 or RET jump to diary file %s"
4447 (abbreviate-file-name buffer-file-name))
4449 'org-agenda-diary-link t
4450 'org-marker (org-agenda-new-marker (point-at-bol))))
4452 (defun org-diary-default-entry ()
4453 "Add a dummy entry to the diary.
4454 Needed to avoid empty dates which mess up holiday display."
4455 ;; Catch the error if dealing with the new add-to-diary-alist
4456 (when org-disable-agenda-to-diary
4457 (condition-case nil
4458 (org-add-to-diary-list original-date "Org-mode dummy" "")
4459 (error
4460 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4462 (defun org-add-to-diary-list (&rest args)
4463 (if (fboundp 'diary-add-to-list)
4464 (apply 'diary-add-to-list args)
4465 (apply 'add-to-diary-list args)))
4467 (defvar org-diary-last-run-time nil)
4469 ;;;###autoload
4470 (defun org-diary (&rest args)
4471 "Return diary information from org-files.
4472 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4473 It accesses org files and extracts information from those files to be
4474 listed in the diary. The function accepts arguments specifying what
4475 items should be listed. For a list of arguments allowed here, see the
4476 variable `org-agenda-entry-types'.
4478 The call in the diary file should look like this:
4480 &%%(org-diary) ~/path/to/some/orgfile.org
4482 Use a separate line for each org file to check. Or, if you omit the file name,
4483 all files listed in `org-agenda-files' will be checked automatically:
4485 &%%(org-diary)
4487 If you don't give any arguments (as in the example above), the default
4488 arguments (:deadline :scheduled :timestamp :sexp) are used.
4489 So the example above may also be written as
4491 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4493 The function expects the lisp variables `entry' and `date' to be provided
4494 by the caller, because this is how the calendar works. Don't use this
4495 function from a program - use `org-agenda-get-day-entries' instead."
4496 (when (> (- (org-float-time)
4497 org-agenda-last-marker-time)
4499 (org-agenda-reset-markers))
4500 (org-compile-prefix-format 'agenda)
4501 (org-set-sorting-strategy 'agenda)
4502 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4503 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4504 (list entry)
4505 (org-agenda-files t)))
4506 (time (org-float-time))
4507 file rtn results)
4508 (when (or (not org-diary-last-run-time)
4509 (> (- time
4510 org-diary-last-run-time)
4512 (org-prepare-agenda-buffers files))
4513 (setq org-diary-last-run-time time)
4514 ;; If this is called during org-agenda, don't return any entries to
4515 ;; the calendar. Org Agenda will list these entries itself.
4516 (if org-disable-agenda-to-diary (setq files nil))
4517 (while (setq file (pop files))
4518 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4519 (setq results (append results rtn)))
4520 (if results
4521 (concat (org-finalize-agenda-entries results) "\n"))))
4523 ;;; Agenda entry finders
4525 (defun org-agenda-get-day-entries (file date &rest args)
4526 "Does the work for `org-diary' and `org-agenda'.
4527 FILE is the path to a file to be checked for entries. DATE is date like
4528 the one returned by `calendar-current-date'. ARGS are symbols indicating
4529 which kind of entries should be extracted. For details about these, see
4530 the documentation of `org-diary'."
4531 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4532 (let* ((org-startup-folded nil)
4533 (org-startup-align-all-tables nil)
4534 (buffer (if (file-exists-p file)
4535 (org-get-agenda-file-buffer file)
4536 (error "No such file %s" file)))
4537 arg results rtn deadline-results)
4538 (if (not buffer)
4539 ;; If file does not exist, make sure an error message ends up in diary
4540 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4541 (with-current-buffer buffer
4542 (unless (org-mode-p)
4543 (error "Agenda file %s is not in `org-mode'" file))
4544 (let ((case-fold-search nil))
4545 (save-excursion
4546 (save-restriction
4547 (if org-agenda-restrict
4548 (narrow-to-region org-agenda-restrict-begin
4549 org-agenda-restrict-end)
4550 (widen))
4551 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4552 (while (setq arg (pop args))
4553 (cond
4554 ((and (eq arg :todo)
4555 (equal date (calendar-gregorian-from-absolute
4556 (org-today))))
4557 (setq rtn (org-agenda-get-todos))
4558 (setq results (append results rtn)))
4559 ((eq arg :timestamp)
4560 (setq rtn (org-agenda-get-blocks))
4561 (setq results (append results rtn))
4562 (setq rtn (org-agenda-get-timestamps))
4563 (setq results (append results rtn)))
4564 ((eq arg :sexp)
4565 (setq rtn (org-agenda-get-sexps))
4566 (setq results (append results rtn)))
4567 ((eq arg :scheduled)
4568 (setq rtn (org-agenda-get-scheduled deadline-results))
4569 (setq results (append results rtn)))
4570 ((eq arg :closed)
4571 (setq rtn (org-agenda-get-progress))
4572 (setq results (append results rtn)))
4573 ((eq arg :deadline)
4574 (setq rtn (org-agenda-get-deadlines))
4575 (setq deadline-results (copy-sequence rtn))
4576 (setq results (append results rtn))))))))
4577 results))))
4579 (defun org-agenda-get-todos ()
4580 "Return the TODO information for agenda display."
4581 (let* ((props (list 'face nil
4582 'done-face 'org-agenda-done
4583 'org-not-done-regexp org-not-done-regexp
4584 'org-todo-regexp org-todo-regexp
4585 'org-complex-heading-regexp org-complex-heading-regexp
4586 'mouse-face 'highlight
4587 'help-echo
4588 (format "mouse-2 or RET jump to org file %s"
4589 (abbreviate-file-name buffer-file-name))))
4590 (regexp (concat "^\\*+[ \t]+\\("
4591 (if org-select-this-todo-keyword
4592 (if (equal org-select-this-todo-keyword "*")
4593 org-todo-regexp
4594 (concat "\\<\\("
4595 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4596 "\\)\\>"))
4597 org-not-done-regexp)
4598 "[^\n\r]*\\)"))
4599 marker priority category tags todo-state
4600 ee txt beg end)
4601 (goto-char (point-min))
4602 (while (re-search-forward regexp nil t)
4603 (catch :skip
4604 (save-match-data
4605 (beginning-of-line)
4606 (org-agenda-skip)
4607 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4608 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4609 (goto-char (1+ beg))
4610 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4611 (throw :skip nil)))
4612 (goto-char (match-beginning 1))
4613 (setq marker (org-agenda-new-marker (match-beginning 0))
4614 category (org-get-category)
4615 txt (match-string 1)
4616 tags (org-get-tags-at (point))
4617 txt (org-format-agenda-item "" txt category tags)
4618 priority (1+ (org-get-priority txt))
4619 todo-state (org-get-todo-state))
4620 (org-add-props txt props
4621 'org-marker marker 'org-hd-marker marker
4622 'priority priority 'org-category category
4623 'type "todo" 'todo-state todo-state)
4624 (push txt ee)
4625 (if org-agenda-todo-list-sublevels
4626 (goto-char (match-end 1))
4627 (org-end-of-subtree 'invisible))))
4628 (nreverse ee)))
4630 (defun org-agenda-todo-custom-ignore-p (time n)
4631 "Check whether timestamp is farther away then n number of days.
4632 This function is invoked if `org-agenda-todo-ignore-deadlines',
4633 `org-agenda-todo-ignore-scheduled' or
4634 `org-agenda-todo-ignore-timestamp' is set to an integer."
4635 (let ((days (org-days-to-time time)))
4636 (if (>= n 0)
4637 (>= days n)
4638 (<= days n))))
4640 ;;;###autoload
4641 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4642 (&optional end)
4643 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4644 (when (or org-agenda-todo-ignore-with-date
4645 org-agenda-todo-ignore-scheduled
4646 org-agenda-todo-ignore-deadlines
4647 org-agenda-todo-ignore-timestamp)
4648 (setq end (or end (save-excursion (outline-next-heading) (point))))
4649 (save-excursion
4650 (or (and org-agenda-todo-ignore-with-date
4651 (re-search-forward org-ts-regexp end t))
4652 (and org-agenda-todo-ignore-scheduled
4653 (re-search-forward org-scheduled-time-regexp end t)
4654 (cond
4655 ((eq org-agenda-todo-ignore-scheduled 'future)
4656 (> (org-days-to-time (match-string 1)) 0))
4657 ((eq org-agenda-todo-ignore-scheduled 'past)
4658 (<= (org-days-to-time (match-string 1)) 0))
4659 ((numberp org-agenda-todo-ignore-scheduled)
4660 (org-agenda-todo-custom-ignore-p
4661 (match-string 1) org-agenda-todo-ignore-scheduled))
4662 (t)))
4663 (and org-agenda-todo-ignore-deadlines
4664 (re-search-forward org-deadline-time-regexp end t)
4665 (cond
4666 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4667 ((eq org-agenda-todo-ignore-deadlines 'far)
4668 (not (org-deadline-close (match-string 1))))
4669 ((eq org-agenda-todo-ignore-deadlines 'future)
4670 (> (org-days-to-time (match-string 1)) 0))
4671 ((eq org-agenda-todo-ignore-deadlines 'past)
4672 (<= (org-days-to-time (match-string 1)) 0))
4673 ((numberp org-agenda-todo-ignore-deadlines)
4674 (org-agenda-todo-custom-ignore-p
4675 (match-string 1) org-agenda-todo-ignore-deadlines))
4676 (t (org-deadline-close (match-string 1)))))
4677 (and org-agenda-todo-ignore-timestamp
4678 (let ((buffer (current-buffer))
4679 (regexp
4680 (concat
4681 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4682 (start (point)))
4683 ;; Copy current buffer into a temporary one
4684 (with-temp-buffer
4685 (insert-buffer-substring buffer start end)
4686 (goto-char (point-min))
4687 ;; Delete SCHEDULED and DEADLINE items
4688 (while (re-search-forward regexp end t)
4689 (delete-region (match-beginning 0) (match-end 0)))
4690 (goto-char (point-min))
4691 ;; No search for timestamp left
4692 (when (re-search-forward org-ts-regexp nil t)
4693 (cond
4694 ((eq org-agenda-todo-ignore-timestamp 'future)
4695 (> (org-days-to-time (match-string 1)) 0))
4696 ((eq org-agenda-todo-ignore-timestamp 'past)
4697 (<= (org-days-to-time (match-string 1)) 0))
4698 ((numberp org-agenda-todo-ignore-timestamp)
4699 (org-agenda-todo-custom-ignore-p
4700 (match-string 1) org-agenda-todo-ignore-timestamp))
4701 (t))))))))))
4703 (defconst org-agenda-no-heading-message
4704 "No heading for this item in buffer or region.")
4706 (defun org-agenda-get-timestamps ()
4707 "Return the date stamp information for agenda display."
4708 (let* ((props (list 'face nil
4709 'org-not-done-regexp org-not-done-regexp
4710 'org-todo-regexp org-todo-regexp
4711 'org-complex-heading-regexp org-complex-heading-regexp
4712 'mouse-face 'highlight
4713 'help-echo
4714 (format "mouse-2 or RET jump to org file %s"
4715 (abbreviate-file-name buffer-file-name))))
4716 (d1 (calendar-absolute-from-gregorian date))
4717 (remove-re
4718 (concat
4719 (regexp-quote
4720 (format-time-string
4721 "<%Y-%m-%d"
4722 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4723 ".*?>"))
4724 (regexp
4725 (concat
4726 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4727 (regexp-quote
4728 (substring
4729 (format-time-string
4730 (car org-time-stamp-formats)
4731 (apply 'encode-time ; DATE bound by calendar
4732 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4733 1 11))
4734 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4735 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4736 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4737 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4738 todo-state end-of-match show-all)
4739 (goto-char (point-min))
4740 (while (setq end-of-match (re-search-forward regexp nil t))
4741 (setq b0 (match-beginning 0)
4742 b3 (match-beginning 3) e3 (match-end 3)
4743 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4744 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4745 (member todo-state
4746 org-agenda-repeating-timestamp-show-all)))
4747 (catch :skip
4748 (and (org-at-date-range-p) (throw :skip nil))
4749 (org-agenda-skip)
4750 (if (and (match-end 1)
4751 (not (= d1 (org-time-string-to-absolute
4752 (match-string 1) d1 nil show-all))))
4753 (throw :skip nil))
4754 (if (and e3
4755 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4756 (throw :skip nil))
4757 (setq tmp (buffer-substring (max (point-min)
4758 (- b0 org-ds-keyword-length))
4760 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4761 inactivep (= (char-after b0) ?\[)
4762 deadlinep (string-match org-deadline-regexp tmp)
4763 scheduledp (string-match org-scheduled-regexp tmp)
4764 closedp (and org-agenda-include-inactive-timestamps
4765 (string-match org-closed-string tmp))
4766 clockp (and org-agenda-include-inactive-timestamps
4767 (or (string-match org-clock-string tmp)
4768 (string-match "]-+\\'" tmp)))
4769 donep (member todo-state org-done-keywords))
4770 (if (or scheduledp deadlinep closedp clockp
4771 (and donep org-agenda-skip-timestamp-if-done))
4772 (throw :skip t))
4773 (if (string-match ">" timestr)
4774 ;; substring should only run to end of time stamp
4775 (setq timestr (substring timestr 0 (match-end 0))))
4776 (setq marker (org-agenda-new-marker b0)
4777 category (org-get-category b0))
4778 (save-excursion
4779 (if (not (re-search-backward org-outline-regexp-bol nil t))
4780 (setq txt org-agenda-no-heading-message)
4781 (goto-char (match-beginning 0))
4782 (setq hdmarker (org-agenda-new-marker)
4783 tags (org-get-tags-at))
4784 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4785 (setq head (or (match-string 1) ""))
4786 (setq txt (org-format-agenda-item
4787 (if inactivep org-agenda-inactive-leader nil)
4788 head category tags timestr
4789 remove-re)))
4790 (setq priority (org-get-priority txt))
4791 (org-add-props txt props
4792 'org-marker marker 'org-hd-marker hdmarker)
4793 (org-add-props txt nil 'priority priority
4794 'org-category category 'date date
4795 'todo-state todo-state
4796 'type "timestamp")
4797 (push txt ee))
4798 (if org-agenda-skip-additional-timestamps-same-entry
4799 (outline-next-heading)
4800 (goto-char end-of-match))))
4801 (nreverse ee)))
4803 (defun org-agenda-get-sexps ()
4804 "Return the sexp information for agenda display."
4805 (require 'diary-lib)
4806 (let* ((props (list 'mouse-face 'highlight
4807 'help-echo
4808 (format "mouse-2 or RET jump to org file %s"
4809 (abbreviate-file-name buffer-file-name))))
4810 (regexp "^&?%%(")
4811 marker category ee txt tags entry result beg b sexp sexp-entry
4812 todo-state)
4813 (goto-char (point-min))
4814 (while (re-search-forward regexp nil t)
4815 (catch :skip
4816 (org-agenda-skip)
4817 (setq beg (match-beginning 0))
4818 (goto-char (1- (match-end 0)))
4819 (setq b (point))
4820 (forward-sexp 1)
4821 (setq sexp (buffer-substring b (point)))
4822 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4823 (org-trim (match-string 1))
4824 ""))
4825 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4826 (when result
4827 (setq marker (org-agenda-new-marker beg)
4828 category (org-get-category beg)
4829 todo-state (org-get-todo-state))
4831 (dolist (r (if (stringp result)
4832 (list result)
4833 result)) ;; we expect a list here
4834 (if (string-match "\\S-" r)
4835 (setq txt r)
4836 (setq txt "SEXP entry returned empty string"))
4838 (setq txt (org-format-agenda-item
4839 "" txt category tags 'time))
4840 (org-add-props txt props 'org-marker marker)
4841 (org-add-props txt nil
4842 'org-category category 'date date 'todo-state todo-state
4843 'type "sexp")
4844 (push txt ee)))))
4845 (nreverse ee)))
4847 ;; Calendar sanity: define some functions that are independent of
4848 ;; `calendar-date-style'.
4849 ;; Normally I would like to use ISO format when calling the diary functions,
4850 ;; but to make sure we still have Emacs 22 compatibility we bind
4851 ;; also `european-calendar-style' and use european format
4852 (defun org-anniversary (year month day &optional mark)
4853 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4854 (org-no-warnings
4855 (let ((calendar-date-style 'european) (european-calendar-style t))
4856 (diary-anniversary day month year mark))))
4857 (defun org-cyclic (N year month day &optional mark)
4858 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4859 (org-no-warnings
4860 (let ((calendar-date-style 'european) (european-calendar-style t))
4861 (diary-cyclic N day month year mark))))
4862 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4863 "Like `diary-block', but with fixed (ISO) order of arguments."
4864 (org-no-warnings
4865 (let ((calendar-date-style 'european) (european-calendar-style t))
4866 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4867 (defun org-date (year month day &optional mark)
4868 "Like `diary-date', but with fixed (ISO) order of arguments."
4869 (org-no-warnings
4870 (let ((calendar-date-style 'european) (european-calendar-style t))
4871 (diary-date day month year mark))))
4872 (defalias 'org-float 'diary-float)
4874 ;; Define the` org-class' function
4875 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4876 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4877 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4878 is any number of ISO weeks in the block period for which the item should
4879 be skipped."
4880 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4881 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4882 (d (calendar-absolute-from-gregorian date)))
4883 (and
4884 (<= date1 d)
4885 (<= d date2)
4886 (= (calendar-day-of-week date) dayname)
4887 (or (not skip-weeks)
4888 (progn
4889 (require 'cal-iso)
4890 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4891 entry)))
4893 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4894 "Like `org-class', but honor `calendar-date-style'.
4895 The order of the first 2 times 3 arguments depends on the variable
4896 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4897 So for American calendars, give this as MONTH DAY YEAR, for European as
4898 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4899 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4900 is any number of ISO weeks in the block period for which the item should
4901 be skipped.
4903 This function is here only for backward compatibility and it is deprecated,
4904 please use `org-class' instead."
4905 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4906 (date2 (org-order-calendar-date-args m2 d2 y2)))
4907 (org-class
4908 (nth 2 date1) (car date1) (nth 1 date1)
4909 (nth 2 date2) (car date2) (nth 1 date2)
4910 dayname skip-weeks)))
4912 (defalias 'org-get-closed 'org-agenda-get-progress)
4913 (defun org-agenda-get-progress ()
4914 "Return the logged TODO entries for agenda display."
4915 (let* ((props (list 'mouse-face 'highlight
4916 'org-not-done-regexp org-not-done-regexp
4917 'org-todo-regexp org-todo-regexp
4918 'org-complex-heading-regexp org-complex-heading-regexp
4919 'help-echo
4920 (format "mouse-2 or RET jump to org file %s"
4921 (abbreviate-file-name buffer-file-name))))
4922 (items (if (consp org-agenda-show-log)
4923 org-agenda-show-log
4924 (if (eq org-agenda-show-log 'clockcheck)
4925 '(clock)
4926 org-agenda-log-mode-items)))
4927 (parts
4928 (delq nil
4929 (list
4930 (if (memq 'closed items) (concat "\\<" org-closed-string))
4931 (if (memq 'clock items) (concat "\\<" org-clock-string))
4932 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4933 (parts-re (if parts (mapconcat 'identity parts "\\|")
4934 (error "`org-agenda-log-mode-items' is empty")))
4935 (regexp (concat
4936 "\\(" parts-re "\\)"
4937 " *\\["
4938 (regexp-quote
4939 (substring
4940 (format-time-string
4941 (car org-time-stamp-formats)
4942 (apply 'encode-time ; DATE bound by calendar
4943 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4944 1 11))))
4945 (org-agenda-search-headline-for-time nil)
4946 marker hdmarker priority category tags closedp statep clockp state
4947 ee txt extra timestr rest clocked)
4948 (goto-char (point-min))
4949 (while (re-search-forward regexp nil t)
4950 (catch :skip
4951 (org-agenda-skip)
4952 (setq marker (org-agenda-new-marker (match-beginning 0))
4953 closedp (equal (match-string 1) org-closed-string)
4954 statep (equal (string-to-char (match-string 1)) ?-)
4955 clockp (not (or closedp statep))
4956 state (and statep (match-string 2))
4957 category (org-get-category (match-beginning 0))
4958 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4960 (when (string-match "\\]" timestr)
4961 ;; substring should only run to end of time stamp
4962 (setq rest (substring timestr (match-end 0))
4963 timestr (substring timestr 0 (match-end 0)))
4964 (if (and (not closedp) (not statep)
4965 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4966 (progn (setq timestr (concat (substring timestr 0 -1)
4967 "-" (match-string 1 rest) "]"))
4968 (setq clocked (match-string 2 rest)))
4969 (setq clocked "-")))
4970 (save-excursion
4971 (setq extra
4972 (cond
4973 ((not org-agenda-log-mode-add-notes) nil)
4974 (statep
4975 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4976 (match-string 1)))
4977 (clockp
4978 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4979 (match-string 1)))))
4980 (if (not (re-search-backward org-outline-regexp-bol nil t))
4981 (setq txt org-agenda-no-heading-message)
4982 (goto-char (match-beginning 0))
4983 (setq hdmarker (org-agenda-new-marker)
4984 tags (org-get-tags-at))
4985 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4986 (setq txt (match-string 1))
4987 (when extra
4988 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4989 (setq txt (concat (substring txt 0 (match-beginning 1))
4990 " - " extra " " (match-string 2 txt)))
4991 (setq txt (concat txt " - " extra))))
4992 (setq txt (org-format-agenda-item
4993 (cond
4994 (closedp "Closed: ")
4995 (statep (concat "State: (" state ")"))
4996 (t (concat "Clocked: (" clocked ")")))
4997 txt category tags timestr)))
4998 (setq priority 100000)
4999 (org-add-props txt props
5000 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5001 'priority priority 'org-category category
5002 'type "closed" 'date date
5003 'undone-face 'org-warning 'done-face 'org-agenda-done)
5004 (push txt ee))
5005 (goto-char (point-at-eol))))
5006 (nreverse ee)))
5008 (defun org-agenda-show-clocking-issues ()
5009 "Add overlays, showing issues with clocking.
5010 See also the user option `org-agenda-clock-consistency-checks'."
5011 (interactive)
5012 (let* ((pl org-agenda-clock-consistency-checks)
5013 (re (concat "^[ \t]*"
5014 org-clock-string
5015 "[ \t]+"
5016 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5017 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5018 (tlstart 0.)
5019 (tlend 0.)
5020 (maxtime (org-hh:mm-string-to-minutes
5021 (or (plist-get pl :max-duration) "24:00")))
5022 (mintime (org-hh:mm-string-to-minutes
5023 (or (plist-get pl :min-duration) 0)))
5024 (maxgap (org-hh:mm-string-to-minutes
5025 ;; default 30:00 means never complain
5026 (or (plist-get pl :max-gap) "30:00")))
5027 (gapok (mapcar 'org-hh:mm-string-to-minutes
5028 (plist-get pl :gap-ok-around)))
5029 (def-face (or (plist-get pl :default-face)
5030 '((:background "DarkRed") (:foreground "white"))))
5031 issue face m te ts dt ov)
5032 (goto-char (point-min))
5033 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5034 (setq issue nil face def-face)
5035 (catch 'next
5036 (setq m (org-get-at-bol 'org-marker)
5037 te nil ts nil)
5038 (unless (and m (markerp m))
5039 (setq issue "No valid clock line") (throw 'next t))
5040 (org-with-point-at m
5041 (save-excursion
5042 (goto-char (point-at-bol))
5043 (unless (looking-at re)
5044 (error "No valid Clock line")
5045 (throw 'next t))
5046 (unless (match-end 3)
5047 (setq issue "No end time"
5048 face (or (plist-get pl :no-end-time-face) face))
5049 (throw 'next t))
5050 (setq ts (match-string 1)
5051 te (match-string 3)
5052 ts (org-float-time
5053 (apply 'encode-time (org-parse-time-string ts)))
5054 te (org-float-time
5055 (apply 'encode-time (org-parse-time-string te)))
5056 dt (- te ts))))
5057 (cond
5058 ((> dt (* 60 maxtime))
5059 ;; a very long clocking chunk
5060 (setq issue (format "Clocking interval is very long: %s"
5061 (org-minutes-to-hh:mm-string
5062 (floor (/ (float dt) 60.))))
5063 face (or (plist-get pl :long-face) face)))
5064 ((< dt (* 60 mintime))
5065 ;; a very short clocking chunk
5066 (setq issue (format "Clocking interval is very short: %s"
5067 (org-minutes-to-hh:mm-string
5068 (floor (/ (float dt) 60.))))
5069 face (or (plist-get pl :short-face) face)))
5070 ((and (> tlend 0) (< ts tlend))
5071 ;; Two clock entries are overlapping
5072 (setq issue (format "Clocking overlap: %d minutes"
5073 (/ (- tlend ts) 60))
5074 face (or (plist-get pl :overlap-face) face)))
5075 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5076 ;; There is a gap, lets see if we need to report it
5077 (unless (org-agenda-check-clock-gap tlend ts gapok)
5078 (setq issue (format "Clocking gap: %d minutes"
5079 (/ (- ts tlend) 60))
5080 face (or (plist-get pl :gap-face) face))))
5081 (t nil)))
5082 (setq tlend (or te tlend) tlstart (or ts tlstart))
5083 (when issue
5084 ;; OK, there was some issue, add an overlay to show the issue
5085 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5086 (overlay-put ov 'before-string
5087 (concat
5088 (org-add-props
5089 (format "%-43s" (concat " " issue))
5091 'face face)
5092 "\n"))
5093 (overlay-put ov 'evaporate t)))))
5095 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5096 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5097 (catch 'exit
5098 (unless ok-list
5099 ;; there are no OK times for gaps...
5100 (throw 'exit nil))
5101 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5102 ;; This is more than 24 hours, so it is OK.
5103 ;; because we have at least one OK time, that must be in the
5104 ;; 24 hour interval.
5105 (throw 'exit t))
5106 ;; We have a shorter gap.
5107 ;; Now we have to get the minute of the day when these times are
5108 (let* ((t1dec (decode-time (seconds-to-time t1)))
5109 (t2dec (decode-time (seconds-to-time t2)))
5110 ;; compute the minute on the day
5111 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5112 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5113 (when (< min2 min1)
5114 ;; if min2 is smaller than min1, this means it is on the next day.
5115 ;; Wrap it to after midnight.
5116 (setq min2 (+ min2 1440)))
5117 ;; Now check if any of the OK times is in the gap
5118 (mapc (lambda (x)
5119 ;; Wrap the time to after midnight if necessary
5120 (if (< x min1) (setq x (+ x 1440)))
5121 ;; Check if in interval
5122 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5123 ok-list)
5124 ;; Nope, this gap is not OK
5125 nil)))
5127 (defun org-agenda-get-deadlines ()
5128 "Return the deadline information for agenda display."
5129 (let* ((props (list 'mouse-face 'highlight
5130 'org-not-done-regexp org-not-done-regexp
5131 'org-todo-regexp org-todo-regexp
5132 'org-complex-heading-regexp org-complex-heading-regexp
5133 'help-echo
5134 (format "mouse-2 or RET jump to org file %s"
5135 (abbreviate-file-name buffer-file-name))))
5136 (regexp org-deadline-time-regexp)
5137 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5138 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5139 d2 diff dfrac wdays pos pos1 category tags
5140 suppress-prewarning
5141 ee txt head face s todo-state show-all upcomingp donep timestr)
5142 (goto-char (point-min))
5143 (while (re-search-forward regexp nil t)
5144 (setq suppress-prewarning nil)
5145 (catch :skip
5146 (org-agenda-skip)
5147 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5148 (save-match-data
5149 (string-match org-scheduled-time-regexp
5150 (buffer-substring (point-at-bol)
5151 (point-at-eol)))))
5152 (setq suppress-prewarning
5153 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5154 org-agenda-skip-deadline-prewarning-if-scheduled
5155 0)))
5156 (setq s (match-string 1)
5157 txt nil
5158 pos (1- (match-beginning 1))
5159 todo-state (save-match-data (org-get-todo-state))
5160 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5161 (member todo-state
5162 org-agenda-repeating-timestamp-show-all))
5163 d2 (org-time-string-to-absolute
5164 (match-string 1) d1 'past show-all)
5165 diff (- d2 d1)
5166 wdays (if suppress-prewarning
5167 (let ((org-deadline-warning-days suppress-prewarning))
5168 (org-get-wdays s))
5169 (org-get-wdays s))
5170 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5171 upcomingp (and todayp (> diff 0)))
5172 ;; When to show a deadline in the calendar:
5173 ;; If the expiration is within wdays warning time.
5174 ;; Past-due deadlines are only shown on the current date
5175 (if (and (or (and (<= diff wdays)
5176 (and todayp (not org-agenda-only-exact-dates)))
5177 (= diff 0)))
5178 (save-excursion
5179 ;; (setq todo-state (org-get-todo-state))
5180 (setq donep (member todo-state org-done-keywords))
5181 (if (and donep
5182 (or org-agenda-skip-deadline-if-done
5183 (not (= diff 0))))
5184 (setq txt nil)
5185 (setq category (org-get-category))
5186 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5187 (setq txt org-agenda-no-heading-message)
5188 (goto-char (match-end 0))
5189 (setq pos1 (match-beginning 0))
5190 (setq tags (org-get-tags-at pos1))
5191 (setq head (buffer-substring-no-properties
5192 (point)
5193 (progn (skip-chars-forward "^\r\n")
5194 (point))))
5195 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5196 (setq timestr
5197 (concat (substring s (match-beginning 1)) " "))
5198 (setq timestr 'time))
5199 (setq txt (org-format-agenda-item
5200 (if (= diff 0)
5201 (car org-agenda-deadline-leaders)
5202 (if (functionp
5203 (nth 1 org-agenda-deadline-leaders))
5204 (funcall
5205 (nth 1 org-agenda-deadline-leaders)
5206 diff date)
5207 (format (nth 1 org-agenda-deadline-leaders)
5208 diff)))
5209 head category tags
5210 (if (not (= diff 0)) nil timestr)))))
5211 (when txt
5212 (setq face (org-agenda-deadline-face dfrac))
5213 (org-add-props txt props
5214 'org-marker (org-agenda-new-marker pos)
5215 'org-hd-marker (org-agenda-new-marker pos1)
5216 'priority (+ (- diff)
5217 (org-get-priority txt))
5218 'org-category category
5219 'todo-state todo-state
5220 'type (if upcomingp "upcoming-deadline" "deadline")
5221 'date (if upcomingp date d2)
5222 'face (if donep 'org-agenda-done face)
5223 'undone-face face 'done-face 'org-agenda-done)
5224 (push txt ee))))))
5225 (nreverse ee)))
5227 (defun org-agenda-deadline-face (fraction)
5228 "Return the face to displaying a deadline item.
5229 FRACTION is what fraction of the head-warning time has passed."
5230 (let ((faces org-agenda-deadline-faces) f)
5231 (catch 'exit
5232 (while (setq f (pop faces))
5233 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5235 (defun org-agenda-get-scheduled (&optional deadline-results)
5236 "Return the scheduled information for agenda display."
5237 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5238 'org-todo-regexp org-todo-regexp
5239 'org-complex-heading-regexp org-complex-heading-regexp
5240 'done-face 'org-agenda-done
5241 'mouse-face 'highlight
5242 'help-echo
5243 (format "mouse-2 or RET jump to org file %s"
5244 (abbreviate-file-name buffer-file-name))))
5245 (regexp org-scheduled-time-regexp)
5246 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5247 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5249 (deadline-position-alist
5250 (mapcar (lambda (a) (and (setq mm (get-text-property
5251 0 'org-hd-marker a))
5252 (cons (marker-position mm) a)))
5253 deadline-results))
5254 d2 diff pos pos1 category tags donep
5255 ee txt head pastschedp todo-state face timestr s habitp show-all)
5256 (goto-char (point-min))
5257 (while (re-search-forward regexp nil t)
5258 (catch :skip
5259 (org-agenda-skip)
5260 (setq s (match-string 1)
5261 txt nil
5262 pos (1- (match-beginning 1))
5263 todo-state (save-match-data (org-get-todo-state))
5264 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5265 (member todo-state
5266 org-agenda-repeating-timestamp-show-all))
5267 d2 (org-time-string-to-absolute
5268 (match-string 1) d1 'past show-all)
5269 diff (- d2 d1))
5270 (setq pastschedp (and todayp (< diff 0)))
5271 ;; When to show a scheduled item in the calendar:
5272 ;; If it is on or past the date.
5273 (when (or (and (< diff 0)
5274 (< (abs diff) org-scheduled-past-days)
5275 (and todayp (not org-agenda-only-exact-dates)))
5276 (= diff 0))
5277 (save-excursion
5278 (setq donep (member todo-state org-done-keywords))
5279 (if (and donep
5280 (or org-agenda-skip-scheduled-if-done
5281 (not (= diff 0))
5282 (and (functionp 'org-is-habit-p)
5283 (org-is-habit-p))))
5284 (setq txt nil)
5285 (setq habitp (and (functionp 'org-is-habit-p)
5286 (org-is-habit-p)))
5287 (setq category (org-get-category))
5288 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5289 (setq txt org-agenda-no-heading-message)
5290 (goto-char (match-end 0))
5291 (setq pos1 (match-beginning 0))
5292 (if habitp
5293 (if (or (not org-habit-show-habits)
5294 (and (not todayp)
5295 org-habit-show-habits-only-for-today))
5296 (throw :skip nil))
5297 (if (and
5298 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5299 (and org-agenda-skip-scheduled-if-deadline-is-shown
5300 pastschedp))
5301 (setq mm (assoc pos1 deadline-position-alist)))
5302 (throw :skip nil)))
5303 (setq tags (org-get-tags-at))
5304 (setq head (buffer-substring-no-properties
5305 (point)
5306 (progn (skip-chars-forward "^\r\n") (point))))
5307 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5308 (setq timestr
5309 (concat (substring s (match-beginning 1)) " "))
5310 (setq timestr 'time))
5311 (setq txt (org-format-agenda-item
5312 (if (= diff 0)
5313 (car org-agenda-scheduled-leaders)
5314 (format (nth 1 org-agenda-scheduled-leaders)
5315 (- 1 diff)))
5316 head category tags
5317 (if (not (= diff 0)) nil timestr)
5318 nil habitp))))
5319 (when txt
5320 (setq face
5321 (cond
5322 ((and (not habitp) pastschedp)
5323 'org-scheduled-previously)
5324 (todayp 'org-scheduled-today)
5325 (t 'org-scheduled))
5326 habitp (and habitp (org-habit-parse-todo)))
5327 (org-add-props txt props
5328 'undone-face face
5329 'face (if donep 'org-agenda-done face)
5330 'org-marker (org-agenda-new-marker pos)
5331 'org-hd-marker (org-agenda-new-marker pos1)
5332 'type (if pastschedp "past-scheduled" "scheduled")
5333 'date (if pastschedp d2 date)
5334 'priority (if habitp
5335 (org-habit-get-priority habitp)
5336 (+ 94 (- 5 diff) (org-get-priority txt)))
5337 'org-category category
5338 'org-habit-p habitp
5339 'todo-state todo-state)
5340 (push txt ee))))))
5341 (nreverse ee)))
5343 (defun org-agenda-get-blocks ()
5344 "Return the date-range information for agenda display."
5345 (let* ((props (list 'face nil
5346 'org-not-done-regexp org-not-done-regexp
5347 'org-todo-regexp org-todo-regexp
5348 'org-complex-heading-regexp org-complex-heading-regexp
5349 'mouse-face 'highlight
5350 'help-echo
5351 (format "mouse-2 or RET jump to org file %s"
5352 (abbreviate-file-name buffer-file-name))))
5353 (regexp org-tr-regexp)
5354 (d0 (calendar-absolute-from-gregorian date))
5355 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5356 head donep)
5357 (goto-char (point-min))
5358 (while (re-search-forward regexp nil t)
5359 (catch :skip
5360 (org-agenda-skip)
5361 (setq pos (point))
5362 (let ((start-time (match-string 1))
5363 (end-time (match-string 2)))
5364 (setq s1 (match-string 1)
5365 s2 (match-string 2)
5366 d1 (time-to-days (org-time-string-to-time s1))
5367 d2 (time-to-days (org-time-string-to-time s2)))
5368 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5369 ;; Only allow days between the limits, because the normal
5370 ;; date stamps will catch the limits.
5371 (save-excursion
5372 (setq todo-state (org-get-todo-state))
5373 (setq donep (member todo-state org-done-keywords))
5374 (if (and donep org-agenda-skip-timestamp-if-done)
5375 (throw :skip t))
5376 (setq marker (org-agenda-new-marker (point)))
5377 (setq category (org-get-category))
5378 (if (not (re-search-backward org-outline-regexp-bol nil t))
5379 (setq txt org-agenda-no-heading-message)
5380 (goto-char (match-beginning 0))
5381 (setq hdmarker (org-agenda-new-marker (point)))
5382 (setq tags (org-get-tags-at))
5383 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5384 (setq head (match-string 1))
5385 (let ((remove-re
5386 (if org-agenda-remove-timeranges-from-blocks
5387 (concat
5388 "<" (regexp-quote s1) ".*?>"
5389 "--"
5390 "<" (regexp-quote s2) ".*?>")
5391 nil)))
5392 (setq txt (org-format-agenda-item
5393 (format
5394 (nth (if (= d1 d2) 0 1)
5395 org-agenda-timerange-leaders)
5396 (1+ (- d0 d1)) (1+ (- d2 d1)))
5397 head category tags
5398 (cond ((= d1 d0)
5399 (concat "<" start-time ">"))
5400 ((= d2 d0)
5401 (concat "<" end-time ">"))
5402 (t nil))
5403 remove-re))))
5404 (org-add-props txt props
5405 'org-marker marker 'org-hd-marker hdmarker
5406 'type "block" 'date date
5407 'todo-state todo-state
5408 'priority (org-get-priority txt) 'org-category category)
5409 (push txt ee))))
5410 (goto-char pos)))
5411 ;; Sort the entries by expiration date.
5412 (nreverse ee)))
5414 ;;; Agenda presentation and sorting
5416 (defvar org-prefix-has-time nil
5417 "A flag, set by `org-compile-prefix-format'.
5418 The flag is set if the currently compiled format contains a `%t'.")
5419 (defvar org-prefix-has-tag nil
5420 "A flag, set by `org-compile-prefix-format'.
5421 The flag is set if the currently compiled format contains a `%T'.")
5422 (defvar org-prefix-has-effort nil
5423 "A flag, set by `org-compile-prefix-format'.
5424 The flag is set if the currently compiled format contains a `%e'.")
5425 (defvar org-prefix-category-length nil
5426 "Used by `org-compile-prefix-format' to remember the category field width.")
5427 (defvar org-prefix-category-max-length nil
5428 "Used by `org-compile-prefix-format' to remember the category field width.")
5430 (defun org-agenda-get-category-icon (category)
5431 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5432 (dolist (entry org-agenda-category-icon-alist)
5433 (when (org-string-match-p (car entry) category)
5434 (if (listp (cadr entry))
5435 (return (cadr entry))
5436 (return (apply 'create-image (cdr entry)))))))
5438 (defun org-format-agenda-item (extra txt &optional category tags dotime
5439 remove-re habitp)
5440 "Format TXT to be inserted into the agenda buffer.
5441 In particular, it adds the prefix and corresponding text properties. EXTRA
5442 must be a string and replaces the `%s' specifier in the prefix format.
5443 CATEGORY (string, symbol or nil) may be used to overrule the default
5444 category taken from local variable or file name. It will replace the `%c'
5445 specifier in the format. DOTIME, when non-nil, indicates that a
5446 time-of-day should be extracted from TXT for sorting of this entry, and for
5447 the `%t' specifier in the format. When DOTIME is a string, this string is
5448 searched for a time before TXT is. TAGS can be the tags of the headline.
5449 Any match of REMOVE-RE will be removed from TXT."
5450 (save-match-data
5451 ;; Diary entries sometimes have extra whitespace at the beginning
5452 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5454 ;; Fix the tags part in txt
5455 (setq txt (org-agenda-fix-displayed-tags
5456 txt tags
5457 org-agenda-show-inherited-tags
5458 org-agenda-hide-tags-regexp))
5459 (let* ((category (or category
5460 (if (stringp org-category)
5461 org-category
5462 (and org-category (symbol-name org-category)))
5463 (if buffer-file-name
5464 (file-name-sans-extension
5465 (file-name-nondirectory buffer-file-name))
5466 "")))
5467 (category-icon (org-agenda-get-category-icon category))
5468 (category-icon (if category-icon
5469 (propertize " " 'display category-icon)
5470 ""))
5471 ;; time, tag, effort are needed for the eval of the prefix format
5472 (tag (if tags (nth (1- (length tags)) tags) ""))
5473 time effort neffort
5474 (ts (if dotime (concat
5475 (if (stringp dotime) dotime "")
5476 (and org-agenda-search-headline-for-time txt))))
5477 (time-of-day (and dotime (org-get-time-of-day ts)))
5478 stamp plain s0 s1 s2 rtn srp l
5479 duration thecategory)
5480 (and (org-mode-p) buffer-file-name
5481 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5482 (when (and dotime time-of-day)
5483 ;; Extract starting and ending time and move them to prefix
5484 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5485 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5486 (setq s0 (match-string 0 ts)
5487 srp (and stamp (match-end 3))
5488 s1 (match-string (if plain 1 2) ts)
5489 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5491 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5492 ;; them, we might want to remove them there to avoid duplication.
5493 ;; The user can turn this off with a variable.
5494 (if (and org-prefix-has-time
5495 org-agenda-remove-times-when-in-prefix (or stamp plain)
5496 (string-match (concat (regexp-quote s0) " *") txt)
5497 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5498 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5499 (= (match-beginning 0) 0)
5501 (setq txt (replace-match "" nil nil txt))))
5502 ;; Normalize the time(s) to 24 hour
5503 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5504 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5506 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5507 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5508 (setq s2
5509 (org-minutes-to-hh:mm-string
5510 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5512 ;; Compute the duration
5513 (when s2
5514 (setq duration (- (org-hh:mm-string-to-minutes s2)
5515 (org-hh:mm-string-to-minutes s1)))))
5517 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5518 txt)
5519 ;; Tags are in the string
5520 (if (or (eq org-agenda-remove-tags t)
5521 (and org-agenda-remove-tags
5522 org-prefix-has-tag))
5523 (setq txt (replace-match "" t t txt))
5524 (setq txt (replace-match
5525 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5526 (match-string 2 txt))
5527 t t txt))))
5528 (when (org-mode-p)
5529 (setq effort
5530 (condition-case nil
5531 (org-get-effort
5532 (or (get-text-property 0 'org-hd-marker txt)
5533 (get-text-property 0 'org-marker txt)))
5534 (error nil)))
5535 (when effort
5536 (setq neffort (org-duration-string-to-minutes effort)
5537 effort (setq effort (concat "[" effort "]")))))
5538 ;; prevent erroring out with %e format when there is no effort
5539 (or effort (setq effort ""))
5541 (when remove-re
5542 (while (string-match remove-re txt)
5543 (setq txt (replace-match "" t t txt))))
5545 ;; Set org-heading property on `txt' to mark the start of the
5546 ;; heading.
5547 (add-text-properties 0 (length txt) '(org-heading t) txt)
5549 ;; Prepare the variables needed in the eval of the compiled format
5550 (setq time (cond (s2 (concat
5551 (org-agenda-time-of-day-to-ampm-maybe s1)
5552 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5553 (if org-agenda-timegrid-use-ampm " ")))
5554 (s1 (concat
5555 (org-agenda-time-of-day-to-ampm-maybe s1)
5556 (if org-agenda-timegrid-use-ampm
5557 "........ "
5558 "......")))
5559 (t ""))
5560 extra (or (and (not habitp) extra) "")
5561 category (if (symbolp category) (symbol-name category) category)
5562 thecategory (copy-sequence category))
5563 (if (string-match org-bracket-link-regexp category)
5564 (progn
5565 (setq l (if (match-end 3)
5566 (- (match-end 3) (match-beginning 3))
5567 (- (match-end 1) (match-beginning 1))))
5568 (when (< l (or org-prefix-category-length 0))
5569 (setq category (copy-sequence category))
5570 (org-add-props category nil
5571 'extra-space (make-string
5572 (- org-prefix-category-length l 1) ?\ ))))
5573 (if (and org-prefix-category-max-length
5574 (>= (length category) org-prefix-category-max-length))
5575 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5576 ;; Evaluate the compiled format
5577 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5579 ;; And finally add the text properties
5580 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5581 (org-add-props rtn nil
5582 'org-category (if thecategory (downcase thecategory) category)
5583 'tags (mapcar 'org-downcase-keep-props tags)
5584 'org-highest-priority org-highest-priority
5585 'org-lowest-priority org-lowest-priority
5586 'time-of-day time-of-day
5587 'duration duration
5588 'effort effort
5589 'effort-minutes neffort
5590 'txt txt
5591 'time time
5592 'extra extra
5593 'format org-prefix-format-compiled
5594 'dotime dotime))))
5596 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5597 "Remove tags string from TXT, and add a modified list of tags.
5598 The modified list may contain inherited tags, and tags matched by
5599 `org-agenda-hide-tags-regexp' will be removed."
5600 (when (or add-inherited hide-re)
5601 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5602 (setq txt (substring txt 0 (match-beginning 0))))
5603 (setq tags
5604 (delq nil
5605 (mapcar (lambda (tg)
5606 (if (or (and hide-re (string-match hide-re tg))
5607 (and (not add-inherited)
5608 (get-text-property 0 'inherited tg)))
5610 tg))
5611 tags)))
5612 (when tags
5613 (let ((have-i (get-text-property 0 'inherited (car tags)))
5615 (setq txt (concat txt " :"
5616 (mapconcat
5617 (lambda (x)
5618 (setq i (get-text-property 0 'inherited x))
5619 (if (and have-i (not i))
5620 (progn
5621 (setq have-i nil)
5622 (concat ":" x))
5624 tags ":")
5625 (if have-i "::" ":"))))))
5626 txt)
5628 (defun org-downcase-keep-props (s)
5629 (let ((props (text-properties-at 0 s)))
5630 (setq s (downcase s))
5631 (add-text-properties 0 (length s) props s)
5634 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5635 (defvar org-agenda-sorting-strategy-selected nil)
5637 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5638 (catch 'exit
5639 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5640 ((and todayp (member 'today (car org-agenda-time-grid))))
5641 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5642 ((member 'weekly (car org-agenda-time-grid)))
5643 (t (throw 'exit list)))
5644 (let* ((have (delq nil (mapcar
5645 (lambda (x) (get-text-property 1 'time-of-day x))
5646 list)))
5647 (string (nth 1 org-agenda-time-grid))
5648 (gridtimes (nth 2 org-agenda-time-grid))
5649 (req (car org-agenda-time-grid))
5650 (remove (member 'remove-match req))
5651 new time)
5652 (if (and (member 'require-timed req) (not have))
5653 ;; don't show empty grid
5654 (throw 'exit list))
5655 (while (setq time (pop gridtimes))
5656 (unless (and remove (member time have))
5657 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5658 (push (org-format-agenda-item
5659 nil string "" nil
5660 (concat (substring time 0 -2) ":" (substring time -2)))
5661 new)
5662 (put-text-property
5663 2 (length (car new)) 'face 'org-time-grid (car new))))
5664 (when (and todayp org-agenda-show-current-time-in-grid)
5665 (push (org-format-agenda-item
5667 org-agenda-current-time-string
5668 "" nil
5669 (format-time-string "%H:%M "))
5670 new)
5671 (put-text-property
5672 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5674 (if (member 'time-up org-agenda-sorting-strategy-selected)
5675 (append new list)
5676 (append list new)))))
5678 (defun org-compile-prefix-format (key)
5679 "Compile the prefix format into a Lisp form that can be evaluated.
5680 The resulting form is returned and stored in the variable
5681 `org-prefix-format-compiled'."
5682 (setq org-prefix-has-time nil org-prefix-has-tag nil
5683 org-prefix-category-length nil org-prefix-has-effort nil)
5684 (let ((s (cond
5685 ((stringp org-agenda-prefix-format)
5686 org-agenda-prefix-format)
5687 ((assq key org-agenda-prefix-format)
5688 (cdr (assq key org-agenda-prefix-format)))
5689 (t " %-12:c%?-12t% s")))
5690 (start 0)
5691 varform vars var e c f opt)
5692 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5693 s start)
5694 (setq var (or (cdr (assoc (match-string 4 s)
5695 '(("c" . category) ("t" . time) ("s" . extra)
5696 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5697 'eval)
5698 c (or (match-string 3 s) "")
5699 opt (match-beginning 1)
5700 start (1+ (match-beginning 0)))
5701 (if (equal var 'time) (setq org-prefix-has-time t))
5702 (if (equal var 'tag) (setq org-prefix-has-tag t))
5703 (if (equal var 'effort) (setq org-prefix-has-effort t))
5704 (setq f (concat "%" (match-string 2 s) "s"))
5705 (when (equal var 'category)
5706 (setq org-prefix-category-length
5707 (floor (abs (string-to-number (match-string 2 s)))))
5708 (setq org-prefix-category-max-length
5709 (let ((x (match-string 2 s)))
5710 (save-match-data
5711 (if (string-match "\\.[0-9]+" x)
5712 (string-to-number (substring (match-string 0 x) 1)))))))
5713 (if (eq var 'eval)
5714 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5715 (if opt
5716 (setq varform
5717 `(if (equal "" ,var)
5719 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5720 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5721 (setq s (replace-match "%s" t nil s))
5722 (push varform vars))
5723 (setq vars (nreverse vars))
5724 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5726 (defun org-set-sorting-strategy (key)
5727 (if (symbolp (car org-agenda-sorting-strategy))
5728 ;; the old format
5729 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5730 (setq org-agenda-sorting-strategy-selected
5731 (or (cdr (assq key org-agenda-sorting-strategy))
5732 (cdr (assq 'agenda org-agenda-sorting-strategy))
5733 '(time-up category-keep priority-down)))))
5735 (defun org-get-time-of-day (s &optional string mod24)
5736 "Check string S for a time of day.
5737 If found, return it as a military time number between 0 and 2400.
5738 If not found, return nil.
5739 The optional STRING argument forces conversion into a 5 character wide string
5740 HH:MM."
5741 (save-match-data
5742 (when
5743 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5744 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5745 (let* ((h (string-to-number (match-string 1 s)))
5746 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5747 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5748 (am-p (equal ampm "am"))
5749 (h1 (cond ((not ampm) h)
5750 ((= h 12) (if am-p 0 12))
5751 (t (+ h (if am-p 0 12)))))
5752 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5753 (mod h1 24) h1))
5754 (t0 (+ (* 100 h2) m))
5755 (t1 (concat (if (>= h1 24) "+" " ")
5756 (if (and org-agenda-time-leading-zero
5757 (< t0 1000)) "0" "")
5758 (if (< t0 100) "0" "")
5759 (if (< t0 10) "0" "")
5760 (int-to-string t0))))
5761 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5763 (defvar org-agenda-before-sorting-filter-function nil
5764 "Function to be applied to agenda items prior to sorting.
5765 Prior to sorting also means just before they are inserted into the agenda.
5767 To aid sorting, you may revisit the original entries and add more text
5768 properties which will later be used by the sorting functions.
5770 The function should take a string argument, an agenda line.
5771 It has access to the text properties in that line, which contain among
5772 other things, the property `org-hd-marker' that points to the entry
5773 where the line comes from. Note that not all lines going into the agenda
5774 have this property, only most.
5776 The function should return the modified string. It is probably best
5777 to ONLY change text properties.
5779 You can also use this function as a filter, by returning nil for lines
5780 you don't want to have in the agenda at all. For this application, you
5781 could bind the variable in the options section of a custom command.")
5783 (defun org-finalize-agenda-entries (list &optional nosort)
5784 "Sort and concatenate the agenda items."
5785 (setq list (mapcar 'org-agenda-highlight-todo list))
5786 (if nosort
5787 list
5788 (when org-agenda-before-sorting-filter-function
5789 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5790 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5792 (defun org-agenda-highlight-todo (x)
5793 (let ((org-done-keywords org-done-keywords-for-agenda)
5794 (case-fold-search nil)
5796 (if (eq x 'line)
5797 (save-excursion
5798 (beginning-of-line 1)
5799 (setq re (org-get-at-bol 'org-todo-regexp))
5800 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5801 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5802 (add-text-properties (match-beginning 0) (match-end 1)
5803 (list 'face (org-get-todo-face 1)))
5804 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5805 (delete-region (match-beginning 1) (1- (match-end 0)))
5806 (goto-char (match-beginning 1))
5807 (insert (format org-agenda-todo-keyword-format s)))))
5808 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5809 (setq re (get-text-property 0 'org-todo-regexp x))
5810 (when (and re
5811 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5812 x (or pl 0)) pl))
5813 (add-text-properties
5814 (or (match-end 1) (match-end 0)) (match-end 0)
5815 (list 'face (org-get-todo-face (match-string 2 x)))
5817 (when (match-end 1)
5818 (setq x (concat (substring x 0 (match-end 1))
5819 (format org-agenda-todo-keyword-format
5820 (match-string 2 x))
5821 (org-add-props " " (text-properties-at 0 x))
5822 (substring x (match-end 3)))))))
5823 x)))
5825 (defsubst org-cmp-priority (a b)
5826 "Compare the priorities of string A and B."
5827 (let ((pa (or (get-text-property 1 'priority a) 0))
5828 (pb (or (get-text-property 1 'priority b) 0)))
5829 (cond ((> pa pb) +1)
5830 ((< pa pb) -1)
5831 (t nil))))
5833 (defsubst org-cmp-effort (a b)
5834 "Compare the effort values of string A and B."
5835 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5836 (ea (or (get-text-property 1 'effort-minutes a) def))
5837 (eb (or (get-text-property 1 'effort-minutes b) def)))
5838 (cond ((> ea eb) +1)
5839 ((< ea eb) -1)
5840 (t nil))))
5842 (defsubst org-cmp-category (a b)
5843 "Compare the string values of categories of strings A and B."
5844 (let ((ca (or (get-text-property 1 'org-category a) ""))
5845 (cb (or (get-text-property 1 'org-category b) "")))
5846 (cond ((string-lessp ca cb) -1)
5847 ((string-lessp cb ca) +1)
5848 (t nil))))
5850 (defsubst org-cmp-todo-state (a b)
5851 "Compare the todo states of strings A and B."
5852 (let* ((ma (or (get-text-property 1 'org-marker a)
5853 (get-text-property 1 'org-hd-marker a)))
5854 (mb (or (get-text-property 1 'org-marker b)
5855 (get-text-property 1 'org-hd-marker b)))
5856 (fa (and ma (marker-buffer ma)))
5857 (fb (and mb (marker-buffer mb)))
5858 (todo-kwds
5859 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5860 (and fb (with-current-buffer fb org-todo-keywords-1))))
5861 (ta (or (get-text-property 1 'todo-state a) ""))
5862 (tb (or (get-text-property 1 'todo-state b) ""))
5863 (la (- (length (member ta todo-kwds))))
5864 (lb (- (length (member tb todo-kwds))))
5865 (donepa (member ta org-done-keywords-for-agenda))
5866 (donepb (member tb org-done-keywords-for-agenda)))
5867 (cond ((and donepa (not donepb)) -1)
5868 ((and (not donepa) donepb) +1)
5869 ((< la lb) -1)
5870 ((< lb la) +1)
5871 (t nil))))
5873 (defsubst org-cmp-alpha (a b)
5874 "Compare the headlines, alphabetically."
5875 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5876 (plb (text-property-any 0 (length b) 'org-heading t b))
5877 (ta (and pla (substring a pla)))
5878 (tb (and plb (substring b plb))))
5879 (when pla
5880 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5881 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5882 (setq ta (substring ta (match-end 0))))
5883 (setq ta (downcase ta)))
5884 (when plb
5885 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5886 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5887 (setq tb (substring tb (match-end 0))))
5888 (setq tb (downcase tb)))
5889 (cond ((not ta) +1)
5890 ((not tb) -1)
5891 ((string-lessp ta tb) -1)
5892 ((string-lessp tb ta) +1)
5893 (t nil))))
5895 (defsubst org-cmp-tag (a b)
5896 "Compare the string values of the first tags of A and B."
5897 (let ((ta (car (last (get-text-property 1 'tags a))))
5898 (tb (car (last (get-text-property 1 'tags b)))))
5899 (cond ((not ta) +1)
5900 ((not tb) -1)
5901 ((string-lessp ta tb) -1)
5902 ((string-lessp tb ta) +1)
5903 (t nil))))
5905 (defsubst org-cmp-time (a b)
5906 "Compare the time-of-day values of strings A and B."
5907 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5908 (ta (or (get-text-property 1 'time-of-day a) def))
5909 (tb (or (get-text-property 1 'time-of-day b) def)))
5910 (cond ((< ta tb) -1)
5911 ((< tb ta) +1)
5912 (t nil))))
5914 (defsubst org-cmp-habit-p (a b)
5915 "Compare the todo states of strings A and B."
5916 (let ((ha (get-text-property 1 'org-habit-p a))
5917 (hb (get-text-property 1 'org-habit-p b)))
5918 (cond ((and ha (not hb)) -1)
5919 ((and (not ha) hb) +1)
5920 (t nil))))
5922 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5924 (defun org-entries-lessp (a b)
5925 "Predicate for sorting agenda entries."
5926 ;; The following variables will be used when the form is evaluated.
5927 ;; So even though the compiler complains, keep them.
5928 (let* ((ss org-agenda-sorting-strategy-selected)
5929 (time-up (and (org-em 'time-up 'time-down ss)
5930 (org-cmp-time a b)))
5931 (time-down (if time-up (- time-up) nil))
5932 (priority-up (and (org-em 'priority-up 'priority-down ss)
5933 (org-cmp-priority a b)))
5934 (priority-down (if priority-up (- priority-up) nil))
5935 (effort-up (and (org-em 'effort-up 'effort-down ss)
5936 (org-cmp-effort a b)))
5937 (effort-down (if effort-up (- effort-up) nil))
5938 (category-up (and (or (org-em 'category-up 'category-down ss)
5939 (memq 'category-keep ss))
5940 (org-cmp-category a b)))
5941 (category-down (if category-up (- category-up) nil))
5942 (category-keep (if category-up +1 nil))
5943 (tag-up (and (org-em 'tag-up 'tag-down ss)
5944 (org-cmp-tag a b)))
5945 (tag-down (if tag-up (- tag-up) nil))
5946 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5947 (org-cmp-todo-state a b)))
5948 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5949 (habit-up (and (org-em 'habit-up 'habit-down ss)
5950 (org-cmp-habit-p a b)))
5951 (habit-down (if habit-up (- habit-up) nil))
5952 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5953 (org-cmp-alpha a b)))
5954 (alpha-down (if alpha-up (- alpha-up) nil))
5955 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5956 user-defined-up user-defined-down)
5957 (if (and need-user-cmp org-agenda-cmp-user-defined
5958 (functionp org-agenda-cmp-user-defined))
5959 (setq user-defined-up
5960 (funcall org-agenda-cmp-user-defined a b)
5961 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5962 (cdr (assoc
5963 (eval (cons 'or org-agenda-sorting-strategy-selected))
5964 '((-1 . t) (1 . nil) (nil . nil))))))
5966 ;;; Agenda restriction lock
5968 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5969 "Overlay to mark the headline to which agenda commands are restricted.")
5970 (overlay-put org-agenda-restriction-lock-overlay
5971 'face 'org-agenda-restriction-lock)
5972 (overlay-put org-agenda-restriction-lock-overlay
5973 'help-echo "Agendas are currently limited to this subtree.")
5974 (org-detach-overlay org-agenda-restriction-lock-overlay)
5976 (defun org-agenda-set-restriction-lock (&optional type)
5977 "Set restriction lock for agenda, to current subtree or file.
5978 Restriction will be the file if TYPE is `file', or if type is the
5979 universal prefix '(4), or if the cursor is before the first headline
5980 in the file. Otherwise, restriction will be to the current subtree."
5981 (interactive "P")
5982 (and (equal type '(4)) (setq type 'file))
5983 (setq type (cond
5984 (type type)
5985 ((org-at-heading-p) 'subtree)
5986 ((condition-case nil (org-back-to-heading t) (error nil))
5987 'subtree)
5988 (t 'file)))
5989 (if (eq type 'subtree)
5990 (progn
5991 (setq org-agenda-restrict t)
5992 (setq org-agenda-overriding-restriction 'subtree)
5993 (put 'org-agenda-files 'org-restrict
5994 (list (buffer-file-name (buffer-base-buffer))))
5995 (org-back-to-heading t)
5996 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5997 (move-marker org-agenda-restrict-begin (point))
5998 (move-marker org-agenda-restrict-end
5999 (save-excursion (org-end-of-subtree t)))
6000 (message "Locking agenda restriction to subtree"))
6001 (put 'org-agenda-files 'org-restrict
6002 (list (buffer-file-name (buffer-base-buffer))))
6003 (setq org-agenda-restrict nil)
6004 (setq org-agenda-overriding-restriction 'file)
6005 (move-marker org-agenda-restrict-begin nil)
6006 (move-marker org-agenda-restrict-end nil)
6007 (message "Locking agenda restriction to file"))
6008 (setq current-prefix-arg nil)
6009 (org-agenda-maybe-redo))
6011 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6012 "Remove the agenda restriction lock."
6013 (interactive "P")
6014 (org-detach-overlay org-agenda-restriction-lock-overlay)
6015 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6016 (setq org-agenda-overriding-restriction nil)
6017 (setq org-agenda-restrict nil)
6018 (put 'org-agenda-files 'org-restrict nil)
6019 (move-marker org-agenda-restrict-begin nil)
6020 (move-marker org-agenda-restrict-end nil)
6021 (setq current-prefix-arg nil)
6022 (message "Agenda restriction lock removed")
6023 (or noupdate (org-agenda-maybe-redo)))
6025 (defun org-agenda-maybe-redo ()
6026 "If there is any window showing the agenda view, update it."
6027 (let ((w (get-buffer-window org-agenda-buffer-name t))
6028 (w0 (selected-window)))
6029 (when w
6030 (select-window w)
6031 (org-agenda-redo)
6032 (select-window w0)
6033 (if org-agenda-overriding-restriction
6034 (message "Agenda view shifted to new %s restriction"
6035 org-agenda-overriding-restriction)
6036 (message "Agenda restriction lock removed")))))
6038 ;;; Agenda commands
6040 (defun org-agenda-check-type (error &rest types)
6041 "Check if agenda buffer is of allowed type.
6042 If ERROR is non-nil, throw an error, otherwise just return nil."
6043 (if (memq org-agenda-type types)
6045 (if error
6046 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6047 nil)))
6049 (defun org-agenda-quit ()
6050 "Exit agenda by removing the window or the buffer."
6051 (interactive)
6052 (if org-agenda-columns-active
6053 (org-columns-quit)
6054 (let ((buf (current-buffer)))
6055 (if (eq org-agenda-window-setup 'other-frame)
6056 (progn
6057 (kill-buffer buf)
6058 (org-agenda-reset-markers)
6059 (org-columns-remove-overlays)
6060 (setq org-agenda-archives-mode nil)
6061 (delete-frame))
6062 (and (not (eq org-agenda-window-setup 'current-window))
6063 (not (one-window-p))
6064 (delete-window))
6065 (kill-buffer buf)
6066 (org-agenda-reset-markers)
6067 (org-columns-remove-overlays)
6068 (setq org-agenda-archives-mode nil)))
6069 ;; Maybe restore the pre-agenda window configuration.
6070 (and org-agenda-restore-windows-after-quit
6071 (not (eq org-agenda-window-setup 'other-frame))
6072 org-pre-agenda-window-conf
6073 (set-window-configuration org-pre-agenda-window-conf))))
6075 (defun org-agenda-exit ()
6076 "Exit agenda by removing the window or the buffer.
6077 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6078 Org-mode buffers visited directly by the user will not be touched."
6079 (interactive)
6080 (org-release-buffers org-agenda-new-buffers)
6081 (setq org-agenda-new-buffers nil)
6082 (org-agenda-quit))
6084 (defun org-agenda-execute (arg)
6085 "Execute another agenda command, keeping same window.
6086 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6087 in the agenda."
6088 (interactive "P")
6089 (let ((org-agenda-window-setup 'current-window))
6090 (org-agenda arg)))
6092 (defun org-agenda-redo ()
6093 "Rebuild Agenda.
6094 When this is the global TODO list, a prefix argument will be interpreted."
6095 (interactive)
6096 (let* ((org-agenda-keep-modes t)
6097 (filter org-agenda-filter)
6098 (preset (get 'org-agenda-filter :preset-filter))
6099 (org-agenda-filter-while-redo (or filter preset))
6100 (cols org-agenda-columns-active)
6101 (line (org-current-line))
6102 (window-line (- line (org-current-line (window-start))))
6103 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6104 (put 'org-agenda-filter :preset-filter nil)
6105 (and cols (org-columns-quit))
6106 (message "Rebuilding agenda buffer...")
6107 (org-let lprops '(eval org-agenda-redo-command))
6108 (setq org-agenda-undo-list nil
6109 org-agenda-pending-undo-list nil)
6110 (message "Rebuilding agenda buffer...done")
6111 (put 'org-agenda-filter :preset-filter preset)
6112 (and (or filter preset) (org-agenda-filter-apply filter))
6113 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6114 (org-goto-line line)
6115 (recenter window-line)))
6118 (defvar org-global-tags-completion-table nil)
6119 (defvar org-agenda-filter-form nil)
6120 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6121 "Keep only those lines in the agenda buffer that have a specific tag.
6122 The tag is selected with its fast selection letter, as configured.
6123 With prefix argument STRIP, remove all lines that do have the tag.
6124 A lisp caller can specify CHAR. NARROW means that the new tag should be
6125 used to narrow the search - the interactive user can also press `-' or `+'
6126 to switch to narrowing."
6127 (interactive "P")
6128 (let* ((alist org-tag-alist-for-agenda)
6129 (tag-chars (mapconcat
6130 (lambda (x) (if (and (not (symbolp (car x)))
6131 (cdr x))
6132 (char-to-string (cdr x))
6133 ""))
6134 alist ""))
6135 (efforts (org-split-string
6136 (or (cdr (assoc (concat org-effort-property "_ALL")
6137 org-global-properties))
6138 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6139 "")))
6140 (effort-op org-agenda-filter-effort-default-operator)
6141 (effort-prompt "")
6142 (inhibit-read-only t)
6143 (current org-agenda-filter)
6144 maybe-refresh a n tag)
6145 (unless char
6146 (message
6147 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6148 (if narrow "Narrow" "Filter") tag-chars
6149 (if org-agenda-auto-exclude-function "[RET], " ""))
6150 (setq char (read-char)))
6151 (when (member char '(?+ ?-))
6152 ;; Narrowing down
6153 (cond ((equal char ?-) (setq strip t narrow t))
6154 ((equal char ?+) (setq strip nil narrow t)))
6155 (message
6156 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6157 (setq char (read-char)))
6158 (when (member char '(?< ?> ?= ??))
6159 ;; An effort operator
6160 (setq effort-op (char-to-string char))
6161 (setq alist nil) ; to make sure it will be interpreted as effort.
6162 (unless (equal char ??)
6163 (loop for i from 0 to 9 do
6164 (setq effort-prompt
6165 (concat
6166 effort-prompt " ["
6167 (if (= i 9) "0" (int-to-string (1+ i)))
6168 "]" (nth i efforts))))
6169 (message "Effort%s: %s " effort-op effort-prompt)
6170 (setq char (read-char))
6171 (when (or (< char ?0) (> char ?9))
6172 (error "Need 1-9,0 to select effort" ))))
6173 (when (equal char ?\t)
6174 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6175 (org-set-local 'org-global-tags-completion-table
6176 (org-global-tags-completion-table)))
6177 (let ((completion-ignore-case t))
6178 (setq tag (org-icompleting-read
6179 "Tag: " org-global-tags-completion-table))))
6180 (cond
6181 ((equal char ?\r)
6182 (org-agenda-filter-by-tag-show-all)
6183 (when org-agenda-auto-exclude-function
6184 (setq org-agenda-filter '())
6185 (dolist (tag (org-agenda-get-represented-tags))
6186 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6187 (if modifier
6188 (push modifier org-agenda-filter))))
6189 (if (not (null org-agenda-filter))
6190 (org-agenda-filter-apply org-agenda-filter)))
6191 (setq maybe-refresh t))
6192 ((equal char ?/)
6193 (org-agenda-filter-by-tag-show-all)
6194 (when (get 'org-agenda-filter :preset-filter)
6195 (org-agenda-filter-apply org-agenda-filter))
6196 (setq maybe-refresh t))
6197 ((or (equal char ?\ )
6198 (setq a (rassoc char alist))
6199 (and (>= char ?0) (<= char ?9)
6200 (setq n (if (= char ?0) 9 (- char ?0 1))
6201 tag (concat effort-op (nth n efforts))
6202 a (cons tag nil)))
6203 (and (= char ??)
6204 (setq tag "?eff")
6205 a (cons tag nil))
6206 (and tag (setq a (cons tag nil))))
6207 (org-agenda-filter-by-tag-show-all)
6208 (setq tag (car a))
6209 (setq org-agenda-filter
6210 (cons (concat (if strip "-" "+") tag)
6211 (if narrow current nil)))
6212 (org-agenda-filter-apply org-agenda-filter)
6213 (setq maybe-refresh t))
6214 (t (error "Invalid tag selection character %c" char)))
6215 (when (and maybe-refresh
6216 (eq org-agenda-clockreport-mode 'with-filter))
6217 (org-agenda-redo))))
6219 (defun org-agenda-get-represented-tags ()
6220 "Get a list of all tags currently represented in the agenda."
6221 (let (p tags)
6222 (save-excursion
6223 (goto-char (point-min))
6224 (while (setq p (next-single-property-change (point) 'tags))
6225 (goto-char p)
6226 (mapc (lambda (x) (add-to-list 'tags x))
6227 (get-text-property (point) 'tags))))
6228 tags))
6230 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6231 "Refine the current filter. See `org-agenda-filter-by-tag."
6232 (interactive "P")
6233 (org-agenda-filter-by-tag strip char 'refine))
6235 (defun org-agenda-filter-make-matcher ()
6236 "Create the form that tests a line for the agenda filter."
6237 (let (f f1)
6238 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6239 org-agenda-filter))
6240 (if (member x '("-" "+"))
6241 (setq f1 (if (equal x "-") 'tags '(not tags)))
6242 (if (string-match "[<=>?]" x)
6243 (setq f1 (org-agenda-filter-effort-form x))
6244 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6245 (if (equal (string-to-char x) ?-)
6246 (setq f1 (list 'not f1))))
6247 (push f1 f))
6248 (cons 'and (nreverse f))))
6250 (defun org-agenda-filter-effort-form (e)
6251 "Return the form to compare the effort of the current line with what E says.
6252 E looks like \"+<2:25\"."
6253 (let (op)
6254 (setq e (substring e 1))
6255 (setq op (string-to-char e) e (substring e 1))
6256 (setq op (cond ((equal op ?<) '<=)
6257 ((equal op ?>) '>=)
6258 ((equal op ??) op)
6259 (t '=)))
6260 (list 'org-agenda-compare-effort (list 'quote op)
6261 (org-duration-string-to-minutes e))))
6263 (defun org-agenda-compare-effort (op value)
6264 "Compare the effort of the current line with VALUE, using OP.
6265 If the line does not have an effort defined, return nil."
6266 (let ((eff (org-get-at-bol 'effort-minutes)))
6267 (if (equal op ??)
6268 (not eff)
6269 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6270 value))))
6272 (defun org-agenda-filter-apply (filter)
6273 "Set FILTER as the new agenda filter and apply it."
6274 (let (tags)
6275 (setq org-agenda-filter filter
6276 org-agenda-filter-form (org-agenda-filter-make-matcher))
6277 (org-agenda-set-mode-name)
6278 (save-excursion
6279 (goto-char (point-min))
6280 (while (not (eobp))
6281 (if (org-get-at-bol 'org-marker)
6282 (progn
6283 (setq tags (org-get-at-bol 'tags)) ; used in eval
6284 (if (not (eval org-agenda-filter-form))
6285 (org-agenda-filter-by-tag-hide-line))
6286 (beginning-of-line 2))
6287 (beginning-of-line 2))))
6288 (if (get-char-property (point) 'invisible)
6289 (org-agenda-previous-line))))
6291 (defun org-agenda-filter-by-tag-hide-line ()
6292 (let (ov)
6293 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6294 (point-at-eol)))
6295 (overlay-put ov 'invisible t)
6296 (overlay-put ov 'type 'tags-filter)
6297 (push ov org-agenda-filter-overlays)))
6299 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6300 (setq pos (or pos (point)))
6301 (save-excursion
6302 (dolist (ov (overlays-at pos))
6303 (when (and (overlay-get ov 'invisible)
6304 (eq (overlay-get ov 'type) 'tags-filter))
6305 (goto-char pos)
6306 (if (< (overlay-start ov) (point-at-eol))
6307 (move-overlay ov (point-at-eol)
6308 (overlay-end ov)))))))
6310 (defun org-agenda-filter-by-tag-show-all ()
6311 (mapc 'delete-overlay org-agenda-filter-overlays)
6312 (setq org-agenda-filter-overlays nil)
6313 (setq org-agenda-filter nil)
6314 (setq org-agenda-filter-form nil)
6315 (org-agenda-set-mode-name))
6317 (defun org-agenda-manipulate-query-add ()
6318 "Manipulate the query by adding a search term with positive selection.
6319 Positive selection means the term must be matched for selection of an entry."
6320 (interactive)
6321 (org-agenda-manipulate-query ?\[))
6322 (defun org-agenda-manipulate-query-subtract ()
6323 "Manipulate the query by adding a search term with negative selection.
6324 Negative selection means term must not be matched for selection of an entry."
6325 (interactive)
6326 (org-agenda-manipulate-query ?\]))
6327 (defun org-agenda-manipulate-query-add-re ()
6328 "Manipulate the query by adding a search regexp with positive selection.
6329 Positive selection means the regexp must match for selection of an entry."
6330 (interactive)
6331 (org-agenda-manipulate-query ?\{))
6332 (defun org-agenda-manipulate-query-subtract-re ()
6333 "Manipulate the query by adding a search regexp with negative selection.
6334 Negative selection means regexp must not match for selection of an entry."
6335 (interactive)
6336 (org-agenda-manipulate-query ?\}))
6337 (defun org-agenda-manipulate-query (char)
6338 (cond
6339 ((memq org-agenda-type '(timeline agenda))
6340 (let ((org-agenda-include-inactive-timestamps t))
6341 (org-agenda-redo))
6342 (message "Display now includes inactive timestamps as well"))
6343 ((eq org-agenda-type 'search)
6344 (org-add-to-string
6345 'org-agenda-query-string
6346 (if org-agenda-last-search-view-search-was-boolean
6347 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6348 (?\{ . " +{}") (?\} . " -{}"))))
6349 " "))
6350 (setq org-agenda-redo-command
6351 (list 'org-search-view
6352 org-todo-only
6353 org-agenda-query-string
6354 (+ (length org-agenda-query-string)
6355 (if (member char '(?\{ ?\})) 0 1))))
6356 (set-register org-agenda-query-register org-agenda-query-string)
6357 (org-agenda-redo))
6358 (t (error "Cannot manipulate query for %s-type agenda buffers"
6359 org-agenda-type))))
6361 (defun org-add-to-string (var string)
6362 (set var (concat (symbol-value var) string)))
6364 (defun org-agenda-goto-date (date)
6365 "Jump to DATE in agenda."
6366 (interactive (list (let ((org-read-date-prefer-future
6367 (eval org-agenda-jump-prefer-future)))
6368 (org-read-date))))
6369 (org-agenda-list nil date))
6371 (defun org-agenda-goto-today ()
6372 "Go to today."
6373 (interactive)
6374 (org-agenda-check-type t 'timeline 'agenda)
6375 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6376 (cond
6377 (tdpos (goto-char tdpos))
6378 ((eq org-agenda-type 'agenda)
6379 (let* ((sd (org-agenda-compute-starting-span
6380 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6381 (org-agenda-overriding-arguments org-agenda-last-arguments))
6382 (setf (nth 1 org-agenda-overriding-arguments) sd)
6383 (org-agenda-redo)
6384 (org-agenda-find-same-or-today-or-agenda)))
6385 (t (error "Cannot find today")))))
6387 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6388 (goto-char
6389 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6390 (text-property-any (point-min) (point-max) 'org-today t)
6391 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6392 (point-min))))
6394 (defun org-agenda-later (arg)
6395 "Go forward in time by thee current span.
6396 With prefix ARG, go forward that many times the current span."
6397 (interactive "p")
6398 (org-agenda-check-type t 'agenda)
6399 (let* ((span org-agenda-current-span)
6400 (sd org-starting-day)
6401 (greg (calendar-gregorian-from-absolute sd))
6402 (cnt (org-get-at-bol 'org-day-cnt))
6403 greg2)
6404 (cond
6405 ((eq span 'day)
6406 (setq sd (+ arg sd)))
6407 ((eq span 'week)
6408 (setq sd (+ (* 7 arg) sd)))
6409 ((eq span 'month)
6410 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6411 sd (calendar-absolute-from-gregorian greg2))
6412 (setcar greg2 (1+ (car greg2))))
6413 ((eq span 'year)
6414 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6415 sd (calendar-absolute-from-gregorian greg2))
6416 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6418 (setq sd (+ (* span arg) sd))))
6419 (let ((org-agenda-overriding-arguments
6420 (list (car org-agenda-last-arguments) sd span t)))
6421 (org-agenda-redo)
6422 (org-agenda-find-same-or-today-or-agenda cnt))))
6424 (defun org-agenda-earlier (arg)
6425 "Go backward in time by the current span.
6426 With prefix ARG, go backward that many times the current span."
6427 (interactive "p")
6428 (org-agenda-later (- arg)))
6430 (defun org-agenda-view-mode-dispatch ()
6431 "Call one of the view mode commands."
6432 (interactive)
6433 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6434 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6435 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6436 [E]ntryText")
6437 (let ((a (read-char-exclusive)))
6438 (case a
6439 (?\ (call-interactively 'org-agenda-reset-view))
6440 (?d (call-interactively 'org-agenda-day-view))
6441 (?w (call-interactively 'org-agenda-week-view))
6442 (?m (call-interactively 'org-agenda-month-view))
6443 (?y (call-interactively 'org-agenda-year-view))
6444 (?l (call-interactively 'org-agenda-log-mode))
6445 (?L (org-agenda-log-mode '(4)))
6446 (?c (org-agenda-log-mode 'clockcheck))
6447 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6448 (?a (call-interactively 'org-agenda-archives-mode))
6449 (?A (org-agenda-archives-mode 'files))
6450 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6451 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6452 (?G (call-interactively 'org-agenda-toggle-time-grid))
6453 (?D (call-interactively 'org-agenda-toggle-diary))
6454 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6455 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6456 (org-agenda-check-type t 'timeline 'agenda)
6457 (org-agenda-redo))
6458 (message "Display now includes inactive timestamps as well"))
6459 (?q (message "Abort"))
6460 (otherwise (error "Invalid key" )))))
6462 (defun org-agenda-reset-view ()
6463 "Switch to default view for agenda."
6464 (interactive)
6465 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6466 (defun org-agenda-day-view (&optional day-of-year)
6467 "Switch to daily view for agenda.
6468 With argument DAY-OF-YEAR, switch to that day of the year."
6469 (interactive "P")
6470 (org-agenda-change-time-span 'day day-of-year))
6471 (defun org-agenda-week-view (&optional iso-week)
6472 "Switch to daily view for agenda.
6473 With argument ISO-WEEK, switch to the corresponding ISO week.
6474 If ISO-WEEK has more then 2 digits, only the last two encode the
6475 week. Any digits before this encode a year. So 200712 means
6476 week 12 of year 2007. Years in the range 1938-2037 can also be
6477 written as 2-digit years."
6478 (interactive "P")
6479 (org-agenda-change-time-span 'week iso-week))
6480 (defun org-agenda-month-view (&optional month)
6481 "Switch to monthly view for agenda.
6482 With argument MONTH, switch to that month."
6483 (interactive "P")
6484 (org-agenda-change-time-span 'month month))
6485 (defun org-agenda-year-view (&optional year)
6486 "Switch to yearly view for agenda.
6487 With argument YEAR, switch to that year.
6488 If MONTH has more then 2 digits, only the last two encode the
6489 month. Any digits before this encode a year. So 200712 means
6490 December year 2007. Years in the range 1938-2037 can also be
6491 written as 2-digit years."
6492 (interactive "P")
6493 (when year
6494 (setq year (org-small-year-to-year year)))
6495 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6496 (org-agenda-change-time-span 'year year)
6497 (error "Abort")))
6499 (defun org-agenda-change-time-span (span &optional n)
6500 "Change the agenda view to SPAN.
6501 SPAN may be `day', `week', `month', `year'."
6502 (org-agenda-check-type t 'agenda)
6503 (if (and (not n) (equal org-agenda-current-span span))
6504 (error "Viewing span is already \"%s\"" span))
6505 (let* ((sd (or (org-get-at-bol 'day)
6506 org-starting-day))
6507 (sd (org-agenda-compute-starting-span sd span n))
6508 (org-agenda-overriding-arguments
6509 (or org-agenda-overriding-arguments
6510 (list (car org-agenda-last-arguments) sd span t))))
6511 (org-agenda-redo)
6512 (org-agenda-find-same-or-today-or-agenda))
6513 (org-agenda-set-mode-name)
6514 (message "Switched to %s view" span))
6516 (defun org-agenda-compute-starting-span (sd span &optional n)
6517 "Compute starting date for agenda.
6518 SPAN may be `day', `week', `month', `year'. The return value
6519 is a cons cell with the starting date and the number of days,
6520 so that the date SD will be in that range."
6521 (let* ((greg (calendar-gregorian-from-absolute sd))
6522 (dg (nth 1 greg))
6523 (mg (car greg))
6524 (yg (nth 2 greg)))
6525 (cond
6526 ((eq span 'day)
6527 (when n
6528 (setq sd (+ (calendar-absolute-from-gregorian
6529 (list mg 1 yg))
6530 n -1))))
6531 ((eq span 'week)
6532 (let* ((nt (calendar-day-of-week
6533 (calendar-gregorian-from-absolute sd)))
6534 (d (if org-agenda-start-on-weekday
6535 (- nt org-agenda-start-on-weekday)
6538 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6539 (when n
6540 (require 'cal-iso)
6541 (when (> n 99)
6542 (setq y1 (org-small-year-to-year (/ n 100))
6543 n (mod n 100)))
6544 (setq sd
6545 (calendar-absolute-from-iso
6546 (list n 1
6547 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6548 ((eq span 'month)
6549 (let (y1)
6550 (when (and n (> n 99))
6551 (setq y1 (org-small-year-to-year (/ n 100))
6552 n (mod n 100)))
6553 (setq sd (calendar-absolute-from-gregorian
6554 (list (or n mg) 1 (or y1 yg))))))
6555 ((eq span 'year)
6556 (setq sd (calendar-absolute-from-gregorian
6557 (list 1 1 (or n yg))))))
6558 sd))
6560 (defun org-agenda-next-date-line (&optional arg)
6561 "Jump to the next line indicating a date in agenda buffer."
6562 (interactive "p")
6563 (org-agenda-check-type t 'agenda 'timeline)
6564 (beginning-of-line 1)
6565 ;; This does not work if user makes date format that starts with a blank
6566 (if (looking-at "^\\S-") (forward-char 1))
6567 (if (not (re-search-forward "^\\S-" nil t arg))
6568 (progn
6569 (backward-char 1)
6570 (error "No next date after this line in this buffer")))
6571 (goto-char (match-beginning 0)))
6573 (defun org-agenda-previous-date-line (&optional arg)
6574 "Jump to the previous line indicating a date in agenda buffer."
6575 (interactive "p")
6576 (org-agenda-check-type t 'agenda 'timeline)
6577 (beginning-of-line 1)
6578 (if (not (re-search-backward "^\\S-" nil t arg))
6579 (error "No previous date before this line in this buffer")))
6581 ;; Initialize the highlight
6582 (defvar org-hl (make-overlay 1 1))
6583 (overlay-put org-hl 'face 'highlight)
6585 (defun org-highlight (begin end &optional buffer)
6586 "Highlight a region with overlay."
6587 (move-overlay org-hl begin end (or buffer (current-buffer))))
6589 (defun org-unhighlight ()
6590 "Detach overlay INDEX."
6591 (org-detach-overlay org-hl))
6593 ;; FIXME this is currently not used.
6594 (defun org-highlight-until-next-command (beg end &optional buffer)
6595 "Move the highlight overlay to BEG/END, remove it before the next command."
6596 (org-highlight beg end buffer)
6597 (add-hook 'pre-command-hook 'org-unhighlight-once))
6598 (defun org-unhighlight-once ()
6599 "Remove the highlight from its position, and this function from the hook."
6600 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6601 (org-unhighlight))
6603 (defun org-agenda-follow-mode ()
6604 "Toggle follow mode in an agenda buffer."
6605 (interactive)
6606 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6607 (org-agenda-set-mode-name)
6608 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6609 (org-agenda-show))
6610 (message "Follow mode is %s"
6611 (if org-agenda-follow-mode "on" "off")))
6613 (defun org-agenda-entry-text-mode (&optional arg)
6614 "Toggle entry text mode in an agenda buffer."
6615 (interactive "P")
6616 (setq org-agenda-entry-text-mode (or (integerp arg)
6617 (not org-agenda-entry-text-mode)))
6618 (org-agenda-entry-text-hide)
6619 (and org-agenda-entry-text-mode
6620 (let ((org-agenda-entry-text-maxlines
6621 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6622 (org-agenda-entry-text-show)))
6623 (org-agenda-set-mode-name)
6624 (message "Entry text mode is %s. Maximum number of lines is %d"
6625 (if org-agenda-entry-text-mode "on" "off")
6626 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6628 (defun org-agenda-clockreport-mode (&optional with-filter)
6629 "Toggle clocktable mode in an agenda buffer.
6630 With prefix arg WITH-FILTER, make the clocktable respect the current
6631 agenda filter."
6632 (interactive "P")
6633 (org-agenda-check-type t 'agenda)
6634 (if with-filter
6635 (setq org-agenda-clockreport-mode 'with-filter)
6636 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6637 (org-agenda-set-mode-name)
6638 (org-agenda-redo)
6639 (message "Clocktable mode is %s"
6640 (if org-agenda-clockreport-mode "on" "off")))
6642 (defun org-agenda-log-mode (&optional special)
6643 "Toggle log mode in an agenda buffer.
6644 With argument SPECIAL, show all possible log items, not only the ones
6645 configured in `org-agenda-log-mode-items'.
6646 With a double `C-u' prefix arg, show *only* log items, nothing else."
6647 (interactive "P")
6648 (org-agenda-check-type t 'agenda 'timeline)
6649 (setq org-agenda-show-log
6650 (cond
6651 ((equal special '(16)) 'only)
6652 ((eq special 'clockcheck)
6653 (if (eq org-agenda-show-log 'clockcheck)
6654 nil 'clockcheck))
6655 (special '(closed clock state))
6656 (t (not org-agenda-show-log))))
6657 (org-agenda-set-mode-name)
6658 (org-agenda-redo)
6659 (message "Log mode is %s"
6660 (if org-agenda-show-log "on" "off")))
6662 (defun org-agenda-archives-mode (&optional with-files)
6663 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6664 When called with a prefix argument, include all archive files as well."
6665 (interactive "P")
6666 (setq org-agenda-archives-mode
6667 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6668 (org-agenda-set-mode-name)
6669 (org-agenda-redo)
6670 (message
6671 "%s"
6672 (cond
6673 ((eq org-agenda-archives-mode nil)
6674 "No archives are included")
6675 ((eq org-agenda-archives-mode 'trees)
6676 (format "Trees with :%s: tag are included" org-archive-tag))
6677 ((eq org-agenda-archives-mode t)
6678 (format "Trees with :%s: tag and all active archive files are included"
6679 org-archive-tag)))))
6681 (defun org-agenda-toggle-diary ()
6682 "Toggle diary inclusion in an agenda buffer."
6683 (interactive)
6684 (org-agenda-check-type t 'agenda)
6685 (setq org-agenda-include-diary (not org-agenda-include-diary))
6686 (org-agenda-redo)
6687 (org-agenda-set-mode-name)
6688 (message "Diary inclusion turned %s"
6689 (if org-agenda-include-diary "on" "off")))
6691 (defun org-agenda-toggle-deadlines ()
6692 "Toggle inclusion of entries with a deadline in an agenda buffer."
6693 (interactive)
6694 (org-agenda-check-type t 'agenda)
6695 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6696 (org-agenda-redo)
6697 (org-agenda-set-mode-name)
6698 (message "Deadlines inclusion turned %s"
6699 (if org-agenda-include-deadlines "on" "off")))
6701 (defun org-agenda-toggle-time-grid ()
6702 "Toggle time grid in an agenda buffer."
6703 (interactive)
6704 (org-agenda-check-type t 'agenda)
6705 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6706 (org-agenda-redo)
6707 (org-agenda-set-mode-name)
6708 (message "Time-grid turned %s"
6709 (if org-agenda-use-time-grid "on" "off")))
6711 (defun org-agenda-set-mode-name ()
6712 "Set the mode name to indicate all the small mode settings."
6713 (setq mode-name
6714 (list "Org-Agenda"
6715 (if (get 'org-agenda-files 'org-restrict) " []" "")
6717 '(:eval (org-agenda-span-name org-agenda-current-span))
6718 (if org-agenda-follow-mode " Follow" "")
6719 (if org-agenda-entry-text-mode " ETxt" "")
6720 (if org-agenda-include-diary " Diary" "")
6721 (if org-agenda-include-deadlines " Ddl" "")
6722 (if org-agenda-use-time-grid " Grid" "")
6723 (if (and (boundp 'org-habit-show-habits)
6724 org-habit-show-habits) " Habit" "")
6725 (cond
6726 ((consp org-agenda-show-log) " LogAll")
6727 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6728 (org-agenda-show-log " Log")
6729 (t ""))
6730 (if (or org-agenda-filter (get 'org-agenda-filter
6731 :preset-filter))
6732 (concat " {" (mapconcat
6733 'identity
6734 (append (get 'org-agenda-filter
6735 :preset-filter)
6736 org-agenda-filter) "") "}")
6738 (if org-agenda-archives-mode
6739 (if (eq org-agenda-archives-mode t)
6740 " Archives"
6741 (format " :%s:" org-archive-tag))
6743 (if org-agenda-clockreport-mode
6744 (if (eq org-agenda-clockreport-mode 'with-filter)
6745 " Clock{}" " Clock")
6746 "")))
6747 (force-mode-line-update))
6749 (defun org-agenda-post-command-hook ()
6750 (setq org-agenda-type
6751 (or (get-text-property (point) 'org-agenda-type)
6752 (get-text-property (max (point-min) (1- (point)))
6753 'org-agenda-type))))
6755 (defun org-agenda-next-line ()
6756 "Move cursor to the next line, and show if follow mode is active."
6757 (interactive)
6758 (call-interactively 'next-line)
6759 (org-agenda-do-context-action))
6761 (defun org-agenda-previous-line ()
6762 "Move cursor to the previous line, and show if follow-mode is active."
6763 (interactive)
6764 (call-interactively 'previous-line)
6765 (org-agenda-do-context-action))
6767 (defun org-agenda-do-context-action ()
6768 "Show outline path and, maybe, follow mode window."
6769 (let ((m (org-get-at-bol 'org-marker)))
6770 (if (and org-agenda-follow-mode m)
6771 (org-agenda-show))
6772 (if (and m org-agenda-show-outline-path)
6773 (org-with-point-at m
6774 (org-display-outline-path t)))))
6776 (defun org-agenda-show-priority ()
6777 "Show the priority of the current item.
6778 This priority is composed of the main priority given with the [#A] cookies,
6779 and by additional input from the age of a schedules or deadline entry."
6780 (interactive)
6781 (let* ((pri (org-get-at-bol 'priority)))
6782 (message "Priority is %d" (if pri pri -1000))))
6784 (defun org-agenda-show-tags ()
6785 "Show the tags applicable to the current item."
6786 (interactive)
6787 (let* ((tags (org-get-at-bol 'tags)))
6788 (if tags
6789 (message "Tags are :%s:"
6790 (org-no-properties (mapconcat 'identity tags ":")))
6791 (message "No tags associated with this line"))))
6793 (defun org-agenda-goto (&optional highlight)
6794 "Go to the Org-mode file which contains the item at point."
6795 (interactive)
6796 (let* ((marker (or (org-get-at-bol 'org-marker)
6797 (org-agenda-error)))
6798 (buffer (marker-buffer marker))
6799 (pos (marker-position marker)))
6800 (switch-to-buffer-other-window buffer)
6801 (widen)
6802 (push-mark)
6803 (goto-char pos)
6804 (when (org-mode-p)
6805 (org-show-context 'agenda)
6806 (save-excursion
6807 (and (outline-next-heading)
6808 (org-flag-heading nil)))) ; show the next heading
6809 (when (outline-invisible-p)
6810 (show-entry)) ; display invisible text
6811 (recenter (/ (window-height) 2))
6812 (run-hooks 'org-agenda-after-show-hook)
6813 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6815 (defvar org-agenda-after-show-hook nil
6816 "Normal hook run after an item has been shown from the agenda.
6817 Point is in the buffer where the item originated.")
6819 (defun org-agenda-kill ()
6820 "Kill the entry or subtree belonging to the current agenda entry."
6821 (interactive)
6822 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6823 (let* ((marker (or (org-get-at-bol 'org-marker)
6824 (org-agenda-error)))
6825 (buffer (marker-buffer marker))
6826 (pos (marker-position marker))
6827 (type (org-get-at-bol 'type))
6828 dbeg dend (n 0) conf)
6829 (org-with-remote-undo buffer
6830 (with-current-buffer buffer
6831 (save-excursion
6832 (goto-char pos)
6833 (if (and (org-mode-p) (not (member type '("sexp"))))
6834 (setq dbeg (progn (org-back-to-heading t) (point))
6835 dend (org-end-of-subtree t t))
6836 (setq dbeg (point-at-bol)
6837 dend (min (point-max) (1+ (point-at-eol)))))
6838 (goto-char dbeg)
6839 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6840 (setq conf (or (eq t org-agenda-confirm-kill)
6841 (and (numberp org-agenda-confirm-kill)
6842 (> n org-agenda-confirm-kill))))
6843 (and conf
6844 (not (y-or-n-p
6845 (format "Delete entry with %d lines in buffer \"%s\"? "
6846 n (buffer-name buffer))))
6847 (error "Abort"))
6848 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6849 (with-current-buffer buffer (delete-region dbeg dend))
6850 (message "Agenda item and source killed"))))
6852 (defvar org-archive-default-command)
6853 (defun org-agenda-archive-default ()
6854 "Archive the entry or subtree belonging to the current agenda entry."
6855 (interactive)
6856 (require 'org-archive)
6857 (org-agenda-archive-with org-archive-default-command))
6859 (defun org-agenda-archive-default-with-confirmation ()
6860 "Archive the entry or subtree belonging to the current agenda entry."
6861 (interactive)
6862 (require 'org-archive)
6863 (org-agenda-archive-with org-archive-default-command 'confirm))
6865 (defun org-agenda-archive ()
6866 "Archive the entry or subtree belonging to the current agenda entry."
6867 (interactive)
6868 (org-agenda-archive-with 'org-archive-subtree))
6870 (defun org-agenda-archive-to-archive-sibling ()
6871 "Move the entry to the archive sibling."
6872 (interactive)
6873 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6875 (defun org-agenda-archive-with (cmd &optional confirm)
6876 "Move the entry to the archive sibling."
6877 (interactive)
6878 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6879 (let* ((marker (or (org-get-at-bol 'org-marker)
6880 (org-agenda-error)))
6881 (buffer (marker-buffer marker))
6882 (pos (marker-position marker)))
6883 (org-with-remote-undo buffer
6884 (with-current-buffer buffer
6885 (if (org-mode-p)
6886 (if (and confirm
6887 (not (y-or-n-p "Archive this subtree or entry? ")))
6888 (error "Abort")
6889 (save-excursion
6890 (goto-char pos)
6891 (org-remove-subtree-entries-from-agenda)
6892 (org-back-to-heading t)
6893 (funcall cmd)))
6894 (error "Archiving works only in Org-mode files"))))))
6896 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6897 "Remove all lines in the agenda that correspond to a given subtree.
6898 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6899 If this information is not given, the function uses the tree at point."
6900 (let ((buf (or buf (current-buffer))) m p)
6901 (save-excursion
6902 (unless (and beg end)
6903 (org-back-to-heading t)
6904 (setq beg (point))
6905 (org-end-of-subtree t)
6906 (setq end (point)))
6907 (set-buffer (get-buffer org-agenda-buffer-name))
6908 (save-excursion
6909 (goto-char (point-max))
6910 (beginning-of-line 1)
6911 (while (not (bobp))
6912 (when (and (setq m (org-get-at-bol 'org-marker))
6913 (equal buf (marker-buffer m))
6914 (setq p (marker-position m))
6915 (>= p beg)
6916 (< p end))
6917 (let ((inhibit-read-only t))
6918 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6919 (beginning-of-line 0))))))
6921 (defun org-agenda-refile (&optional goto rfloc no-update)
6922 "Refile the item at point."
6923 (interactive "P")
6924 (if (equal goto '(16))
6925 (org-refile-goto-last-stored)
6926 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6927 (org-agenda-error)))
6928 (buffer (marker-buffer marker))
6929 (pos (marker-position marker))
6930 (rfloc (or rfloc
6931 (org-refile-get-location
6932 (if goto "Goto" "Refile to") buffer
6933 org-refile-allow-creating-parent-nodes))))
6934 (with-current-buffer buffer
6935 (save-excursion
6936 (save-restriction
6937 (widen)
6938 (goto-char marker)
6939 (org-remove-subtree-entries-from-agenda)
6940 (org-refile goto buffer rfloc)))))
6941 (unless no-update (org-agenda-redo))))
6943 (defun org-agenda-open-link (&optional arg)
6944 "Follow the link in the current line, if any.
6945 This looks for a link in the displayed line in the agenda. It also looks
6946 at the text of the entry itself."
6947 (interactive "P")
6948 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6949 (org-get-at-bol 'org-marker)))
6950 (buffer (and marker (marker-buffer marker)))
6951 (prefix (buffer-substring
6952 (point-at-bol) (point-at-eol))))
6953 (cond
6954 (buffer
6955 (with-current-buffer buffer
6956 (save-excursion
6957 (save-restriction
6958 (widen)
6959 (goto-char marker)
6960 (org-offer-links-in-entry arg prefix)))))
6961 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6962 (save-excursion
6963 (beginning-of-line 1)
6964 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6965 (org-open-link-from-string (match-string 1)))
6966 (t (error "No link to open here")))))
6968 (defun org-agenda-copy-local-variable (var)
6969 "Get a variable from a referenced buffer and install it here."
6970 (let ((m (org-get-at-bol 'org-marker)))
6971 (when (and m (buffer-live-p (marker-buffer m)))
6972 (org-set-local var (with-current-buffer (marker-buffer m)
6973 (symbol-value var))))))
6975 (defun org-agenda-switch-to (&optional delete-other-windows)
6976 "Go to the Org-mode file which contains the item at point."
6977 (interactive)
6978 (if (and org-return-follows-link
6979 (not (org-get-at-bol 'org-marker))
6980 (org-in-regexp org-bracket-link-regexp))
6981 (org-open-link-from-string (match-string 0))
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 (org-pop-to-buffer-same-window buffer)
6987 (and delete-other-windows (delete-other-windows))
6988 (widen)
6989 (goto-char pos)
6990 (when (org-mode-p)
6991 (org-show-context 'agenda)
6992 (save-excursion
6993 (and (outline-next-heading)
6994 (org-flag-heading nil))) ; show the next heading
6995 (when (outline-invisible-p)
6996 (show-entry)))))) ; display invisible text
6998 (defun org-agenda-goto-mouse (ev)
6999 "Go to the Org-mode file which contains the item at the mouse click."
7000 (interactive "e")
7001 (mouse-set-point ev)
7002 (org-agenda-goto))
7004 (defun org-agenda-show (&optional full-entry)
7005 "Display the Org-mode file which contains the item at point.
7006 With prefix argument FULL-ENTRY, make the entire entry visible
7007 if it was hidden in the outline."
7008 (interactive "P")
7009 (let ((win (selected-window)))
7010 (if full-entry
7011 (let ((org-show-entry-below t))
7012 (org-agenda-goto t))
7013 (org-agenda-goto t))
7014 (select-window win)))
7016 (defvar org-agenda-show-window nil)
7017 (defun org-agenda-show-and-scroll-up ()
7018 "Display the Org-mode file which contains the item at point.
7019 When called repeatedly, scroll the window that is displaying the buffer."
7020 (interactive)
7021 (let ((win (selected-window)))
7022 (if (and (window-live-p org-agenda-show-window)
7023 (eq this-command last-command))
7024 (progn
7025 (select-window org-agenda-show-window)
7026 (ignore-errors (scroll-up)))
7027 (org-agenda-goto t)
7028 (show-subtree)
7029 (setq org-agenda-show-window (selected-window)))
7030 (select-window win)))
7032 (defun org-agenda-show-scroll-down ()
7033 "Scroll down the window showing the agenda."
7034 (interactive)
7035 (let ((win (selected-window)))
7036 (when (window-live-p org-agenda-show-window)
7037 (select-window org-agenda-show-window)
7038 (ignore-errors (scroll-down))
7039 (select-window win))))
7041 (defun org-agenda-show-1 (&optional more)
7042 "Display the Org-mode file which contains the item at point.
7043 The prefix arg selects the amount of information to display:
7045 0 hide the subtree
7046 1 just show the entry according to defaults.
7047 2 show the children view
7048 3 show the subtree view
7049 4 show the entire subtree and any LOGBOOK drawers
7050 5 show the entire subtree and any drawers
7051 With prefix argument FULL-ENTRY, make the entire entry visible
7052 if it was hidden in the outline."
7053 (interactive "p")
7054 (let ((win (selected-window)))
7055 (org-agenda-goto t)
7056 (org-recenter-heading 1)
7057 (cond
7058 ((= more 0)
7059 (hide-subtree)
7060 (save-excursion
7061 (org-back-to-heading)
7062 (run-hook-with-args 'org-cycle-hook 'folded))
7063 (message "Remote: FOLDED"))
7064 ((and (org-called-interactively-p 'any) (= more 1))
7065 (message "Remote: show with default settings"))
7066 ((= more 2)
7067 (show-entry)
7068 (show-children)
7069 (save-excursion
7070 (org-back-to-heading)
7071 (run-hook-with-args 'org-cycle-hook 'children))
7072 (message "Remote: CHILDREN"))
7073 ((= more 3)
7074 (show-subtree)
7075 (save-excursion
7076 (org-back-to-heading)
7077 (run-hook-with-args 'org-cycle-hook 'subtree))
7078 (message "Remote: SUBTREE"))
7079 ((= more 4)
7080 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7081 (org-drawer-regexp
7082 (concat "^[ \t]*:\\("
7083 (mapconcat 'regexp-quote org-drawers "\\|")
7084 "\\):[ \t]*$")))
7085 (show-subtree)
7086 (save-excursion
7087 (org-back-to-heading)
7088 (org-cycle-hide-drawers 'subtree)))
7089 (message "Remote: SUBTREE AND LOGBOOK"))
7090 ((> more 4)
7091 (show-subtree)
7092 (message "Remote: SUBTREE AND ALL DRAWERS")))
7093 (select-window win)))
7095 (defun org-recenter-heading (n)
7096 (save-excursion
7097 (org-back-to-heading)
7098 (recenter n)))
7100 (defvar org-agenda-cycle-counter nil)
7101 (defun org-agenda-cycle-show (&optional n)
7102 "Show the current entry in another window, with default settings.
7103 Default settings are taken from `org-show-hierarchy-above' and siblings.
7104 When use repeatedly in immediate succession, the remote entry will cycle
7105 through visibility
7107 children -> subtree -> folded
7109 When called with a numeric prefix arg, that arg will be passed through to
7110 `org-agenda-show-1'. For the interpretation of that argument, see the
7111 docstring of `org-agenda-show-1'."
7112 (interactive "P")
7113 (if (integerp n)
7114 (setq org-agenda-cycle-counter n)
7115 (if (not (eq last-command this-command))
7116 (setq org-agenda-cycle-counter 1)
7117 (if (equal org-agenda-cycle-counter 0)
7118 (setq org-agenda-cycle-counter 2)
7119 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7120 (if (> org-agenda-cycle-counter 3)
7121 (setq org-agenda-cycle-counter 0)))))
7122 (org-agenda-show-1 org-agenda-cycle-counter))
7124 (defun org-agenda-recenter (arg)
7125 "Display the Org-mode file which contains the item at point and recenter."
7126 (interactive "P")
7127 (let ((win (selected-window)))
7128 (org-agenda-goto t)
7129 (recenter arg)
7130 (select-window win)))
7132 (defun org-agenda-show-mouse (ev)
7133 "Display the Org-mode file which contains the item at the mouse click."
7134 (interactive "e")
7135 (mouse-set-point ev)
7136 (org-agenda-show))
7138 (defun org-agenda-check-no-diary ()
7139 "Check if the entry is a diary link and abort if yes."
7140 (if (org-get-at-bol 'org-agenda-diary-link)
7141 (org-agenda-error)))
7143 (defun org-agenda-error ()
7144 (error "Command not allowed in this line"))
7146 (defun org-agenda-tree-to-indirect-buffer ()
7147 "Show the subtree corresponding to the current entry in an indirect buffer.
7148 This calls the command `org-tree-to-indirect-buffer' from the original
7149 Org-mode buffer.
7150 With numerical prefix arg ARG, go up to this level and then take that tree.
7151 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7152 use the dedicated frame)."
7153 (interactive)
7154 (org-agenda-check-no-diary)
7155 (let* ((marker (or (org-get-at-bol 'org-marker)
7156 (org-agenda-error)))
7157 (buffer (marker-buffer marker))
7158 (pos (marker-position marker)))
7159 (with-current-buffer buffer
7160 (save-excursion
7161 (goto-char pos)
7162 (call-interactively 'org-tree-to-indirect-buffer)))))
7164 (defvar org-last-heading-marker (make-marker)
7165 "Marker pointing to the headline that last changed its TODO state
7166 by a remote command from the agenda.")
7168 (defun org-agenda-todo-nextset ()
7169 "Switch TODO entry to next sequence."
7170 (interactive)
7171 (org-agenda-todo 'nextset))
7173 (defun org-agenda-todo-previousset ()
7174 "Switch TODO entry to previous sequence."
7175 (interactive)
7176 (org-agenda-todo 'previousset))
7178 (defun org-agenda-todo (&optional arg)
7179 "Cycle TODO state of line at point, also in Org-mode file.
7180 This changes the line at point, all other lines in the agenda referring to
7181 the same tree node, and the headline of the tree node in the Org-mode file."
7182 (interactive "P")
7183 (org-agenda-check-no-diary)
7184 (let* ((col (current-column))
7185 (marker (or (org-get-at-bol 'org-marker)
7186 (org-agenda-error)))
7187 (buffer (marker-buffer marker))
7188 (pos (marker-position marker))
7189 (hdmarker (org-get-at-bol 'org-hd-marker))
7190 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7191 (inhibit-read-only t)
7192 org-agenda-headline-snapshot-before-repeat newhead just-one)
7193 (org-with-remote-undo buffer
7194 (with-current-buffer buffer
7195 (widen)
7196 (goto-char pos)
7197 (org-show-context 'agenda)
7198 (save-excursion
7199 (and (outline-next-heading)
7200 (org-flag-heading nil))) ; show the next heading
7201 (let ((current-prefix-arg arg))
7202 (call-interactively 'org-todo))
7203 (and (bolp) (forward-char 1))
7204 (setq newhead (org-get-heading))
7205 (when (and (org-bound-and-true-p
7206 org-agenda-headline-snapshot-before-repeat)
7207 (not (equal org-agenda-headline-snapshot-before-repeat
7208 newhead))
7209 todayp)
7210 (setq newhead org-agenda-headline-snapshot-before-repeat
7211 just-one t))
7212 (save-excursion
7213 (org-back-to-heading)
7214 (move-marker org-last-heading-marker (point))))
7215 (beginning-of-line 1)
7216 (save-excursion
7217 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7218 (org-move-to-column col))))
7220 (defun org-agenda-add-note (&optional arg)
7221 "Add a time-stamped note to the entry at point."
7222 (interactive "P")
7223 (org-agenda-check-no-diary)
7224 (let* ((marker (or (org-get-at-bol 'org-marker)
7225 (org-agenda-error)))
7226 (buffer (marker-buffer marker))
7227 (pos (marker-position marker))
7228 (hdmarker (org-get-at-bol 'org-hd-marker))
7229 (inhibit-read-only t))
7230 (with-current-buffer buffer
7231 (widen)
7232 (goto-char pos)
7233 (org-show-context 'agenda)
7234 (save-excursion
7235 (and (outline-next-heading)
7236 (org-flag-heading nil))) ; show the next heading
7237 (org-add-note))))
7239 (defun org-agenda-change-all-lines (newhead hdmarker
7240 &optional fixface just-this)
7241 "Change all lines in the agenda buffer which match HDMARKER.
7242 The new content of the line will be NEWHEAD (as modified by
7243 `org-format-agenda-item'). HDMARKER is checked with
7244 `equal' against all `org-hd-marker' text properties in the file.
7245 If FIXFACE is non-nil, the face of each item is modified according to
7246 the new TODO state.
7247 If JUST-THIS is non-nil, change just the current line, not all.
7248 If FORCE-TAGS is non nil, the car of it returns the new tags."
7249 (let* ((inhibit-read-only t)
7250 (line (org-current-line))
7251 (thetags (with-current-buffer (marker-buffer hdmarker)
7252 (save-excursion (save-restriction (widen)
7253 (goto-char hdmarker)
7254 (org-get-tags-at)))))
7255 props m pl undone-face done-face finish new dotime cat tags)
7256 (save-excursion
7257 (goto-char (point-max))
7258 (beginning-of-line 1)
7259 (while (not finish)
7260 (setq finish (bobp))
7261 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7262 (or (not just-this) (= (org-current-line) line))
7263 (equal m hdmarker))
7264 (setq props (text-properties-at (point))
7265 dotime (org-get-at-bol 'dotime)
7266 cat (org-get-at-bol 'org-category)
7267 tags thetags
7269 (let ((org-prefix-format-compiled
7270 (or (get-text-property (point) 'format)
7271 org-prefix-format-compiled)))
7272 (with-current-buffer (marker-buffer hdmarker)
7273 (save-excursion
7274 (save-restriction
7275 (widen)
7276 (org-format-agenda-item (org-get-at-bol 'extra)
7277 newhead cat tags dotime)))))
7278 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7279 undone-face (org-get-at-bol 'undone-face)
7280 done-face (org-get-at-bol 'done-face))
7281 (beginning-of-line 1)
7282 (cond
7283 ((equal new "")
7284 (and (looking-at ".*\n?") (replace-match "")))
7285 ((looking-at ".*")
7286 (replace-match new t t)
7287 (beginning-of-line 1)
7288 (add-text-properties (point-at-bol) (point-at-eol) props)
7289 (when fixface
7290 (add-text-properties
7291 (point-at-bol) (point-at-eol)
7292 (list 'face
7293 (if org-last-todo-state-is-todo
7294 undone-face done-face))))
7295 (org-agenda-highlight-todo 'line)
7296 (beginning-of-line 1))
7297 (t (error "Line update did not work"))))
7298 (beginning-of-line 0)))
7299 (org-finalize-agenda)))
7301 (defun org-agenda-align-tags (&optional line)
7302 "Align all tags in agenda items to `org-agenda-tags-column'."
7303 (let ((inhibit-read-only t) l c)
7304 (save-excursion
7305 (goto-char (if line (point-at-bol) (point-min)))
7306 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7307 (if line (point-at-eol) nil) t)
7308 (add-text-properties
7309 (match-beginning 2) (match-end 2)
7310 (list 'face (delq nil (let ((prop (get-text-property
7311 (match-beginning 2) 'face)))
7312 (or (listp prop) (setq prop (list prop)))
7313 (if (memq 'org-tag prop)
7314 prop
7315 (cons 'org-tag prop))))))
7316 (setq l (- (match-end 2) (match-beginning 2))
7317 c (if (< org-agenda-tags-column 0)
7318 (- (abs org-agenda-tags-column) l)
7319 org-agenda-tags-column))
7320 (delete-region (match-beginning 1) (match-end 1))
7321 (goto-char (match-beginning 1))
7322 (insert (org-add-props
7323 (make-string (max 1 (- c (current-column))) ?\ )
7324 (plist-put (copy-sequence (text-properties-at (point)))
7325 'face nil))))
7326 (goto-char (point-min))
7327 (org-font-lock-add-tag-faces (point-max)))))
7329 (defun org-agenda-priority-up ()
7330 "Increase the priority of line at point, also in Org-mode file."
7331 (interactive)
7332 (org-agenda-priority 'up))
7334 (defun org-agenda-priority-down ()
7335 "Decrease the priority of line at point, also in Org-mode file."
7336 (interactive)
7337 (org-agenda-priority 'down))
7339 (defun org-agenda-priority (&optional force-direction)
7340 "Set the priority of line at point, also in Org-mode file.
7341 This changes the line at point, all other lines in the agenda referring to
7342 the same tree node, and the headline of the tree node in the Org-mode file."
7343 (interactive)
7344 (unless org-enable-priority-commands
7345 (error "Priority commands are disabled"))
7346 (org-agenda-check-no-diary)
7347 (let* ((marker (or (org-get-at-bol 'org-marker)
7348 (org-agenda-error)))
7349 (hdmarker (org-get-at-bol 'org-hd-marker))
7350 (buffer (marker-buffer hdmarker))
7351 (pos (marker-position hdmarker))
7352 (inhibit-read-only t)
7353 newhead)
7354 (org-with-remote-undo buffer
7355 (with-current-buffer buffer
7356 (widen)
7357 (goto-char pos)
7358 (org-show-context 'agenda)
7359 (save-excursion
7360 (and (outline-next-heading)
7361 (org-flag-heading nil))) ; show the next heading
7362 (funcall 'org-priority force-direction)
7363 (end-of-line 1)
7364 (setq newhead (org-get-heading)))
7365 (org-agenda-change-all-lines newhead hdmarker)
7366 (beginning-of-line 1))))
7368 ;; FIXME: should fix the tags property of the agenda line.
7369 (defun org-agenda-set-tags (&optional tag onoff)
7370 "Set tags for the current headline."
7371 (interactive)
7372 (org-agenda-check-no-diary)
7373 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7374 (call-interactively 'org-change-tag-in-region)
7375 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7376 (org-agenda-error)))
7377 (buffer (marker-buffer hdmarker))
7378 (pos (marker-position hdmarker))
7379 (inhibit-read-only t)
7380 newhead)
7381 (org-with-remote-undo buffer
7382 (with-current-buffer buffer
7383 (widen)
7384 (goto-char pos)
7385 (save-excursion
7386 (org-show-context 'agenda))
7387 (save-excursion
7388 (and (outline-next-heading)
7389 (org-flag-heading nil))) ; show the next heading
7390 (goto-char pos)
7391 (if tag
7392 (org-toggle-tag tag onoff)
7393 (call-interactively 'org-set-tags))
7394 (end-of-line 1)
7395 (setq newhead (org-get-heading)))
7396 (org-agenda-change-all-lines newhead hdmarker)
7397 (beginning-of-line 1)))))
7399 (defun org-agenda-set-property ()
7400 "Set a property for the current headline."
7401 (interactive)
7402 (org-agenda-check-no-diary)
7403 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7404 (org-agenda-error)))
7405 (buffer (marker-buffer hdmarker))
7406 (pos (marker-position hdmarker))
7407 (inhibit-read-only t)
7408 newhead)
7409 (org-with-remote-undo buffer
7410 (with-current-buffer buffer
7411 (widen)
7412 (goto-char pos)
7413 (save-excursion
7414 (org-show-context 'agenda))
7415 (save-excursion
7416 (and (outline-next-heading)
7417 (org-flag-heading nil))) ; show the next heading
7418 (goto-char pos)
7419 (call-interactively 'org-set-property)))))
7421 (defun org-agenda-set-effort ()
7422 "Set the effort property for the current headline."
7423 (interactive)
7424 (org-agenda-check-no-diary)
7425 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7426 (org-agenda-error)))
7427 (buffer (marker-buffer hdmarker))
7428 (pos (marker-position hdmarker))
7429 (inhibit-read-only t)
7430 newhead)
7431 (org-with-remote-undo buffer
7432 (with-current-buffer buffer
7433 (widen)
7434 (goto-char pos)
7435 (save-excursion
7436 (org-show-context 'agenda))
7437 (save-excursion
7438 (and (outline-next-heading)
7439 (org-flag-heading nil))) ; show the next heading
7440 (goto-char pos)
7441 (call-interactively 'org-set-effort)
7442 (end-of-line 1)
7443 (setq newhead (org-get-heading)))
7444 (org-agenda-change-all-lines newhead hdmarker))))
7446 (defun org-agenda-toggle-archive-tag ()
7447 "Toggle the archive tag for the current entry."
7448 (interactive)
7449 (org-agenda-check-no-diary)
7450 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7451 (org-agenda-error)))
7452 (buffer (marker-buffer hdmarker))
7453 (pos (marker-position hdmarker))
7454 (inhibit-read-only t)
7455 newhead)
7456 (org-with-remote-undo buffer
7457 (with-current-buffer buffer
7458 (widen)
7459 (goto-char pos)
7460 (org-show-context 'agenda)
7461 (save-excursion
7462 (and (outline-next-heading)
7463 (org-flag-heading nil))) ; show the next heading
7464 (call-interactively 'org-toggle-archive-tag)
7465 (end-of-line 1)
7466 (setq newhead (org-get-heading)))
7467 (org-agenda-change-all-lines newhead hdmarker)
7468 (beginning-of-line 1))))
7470 (defun org-agenda-do-date-later (arg)
7471 (interactive "P")
7472 (cond
7473 ((or (equal arg '(16))
7474 (memq last-command
7475 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7476 (setq this-command 'org-agenda-date-later-minutes)
7477 (org-agenda-date-later-minutes 1))
7478 ((or (equal arg '(4))
7479 (memq last-command
7480 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7481 (setq this-command 'org-agenda-date-later-hours)
7482 (org-agenda-date-later-hours 1))
7484 (org-agenda-date-later (prefix-numeric-value arg)))))
7486 (defun org-agenda-do-date-earlier (arg)
7487 (interactive "P")
7488 (cond
7489 ((or (equal arg '(16))
7490 (memq last-command
7491 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7492 (setq this-command 'org-agenda-date-earlier-minutes)
7493 (org-agenda-date-earlier-minutes 1))
7494 ((or (equal arg '(4))
7495 (memq last-command
7496 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7497 (setq this-command 'org-agenda-date-earlier-hours)
7498 (org-agenda-date-earlier-hours 1))
7500 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7502 (defun org-agenda-date-later (arg &optional what)
7503 "Change the date of this item to ARG day(s) later."
7504 (interactive "p")
7505 (org-agenda-check-type t 'agenda 'timeline)
7506 (org-agenda-check-no-diary)
7507 (let* ((marker (or (org-get-at-bol 'org-marker)
7508 (org-agenda-error)))
7509 (buffer (marker-buffer marker))
7510 (pos (marker-position marker)))
7511 (org-with-remote-undo buffer
7512 (with-current-buffer buffer
7513 (widen)
7514 (goto-char pos)
7515 (if (not (org-at-timestamp-p))
7516 (error "Cannot find time stamp"))
7517 (org-timestamp-change arg (or what 'day)))
7518 (org-agenda-show-new-time marker org-last-changed-timestamp))
7519 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7521 (defun org-agenda-date-earlier (arg &optional what)
7522 "Change the date of this item to ARG day(s) earlier."
7523 (interactive "p")
7524 (org-agenda-date-later (- arg) what))
7526 (defun org-agenda-date-later-minutes (arg)
7527 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7528 (interactive "p")
7529 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7530 (org-agenda-date-later arg 'minute))
7532 (defun org-agenda-date-earlier-minutes (arg)
7533 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7534 (interactive "p")
7535 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7536 (org-agenda-date-earlier arg 'minute))
7538 (defun org-agenda-date-later-hours (arg)
7539 "Change the time of this item, in hour steps."
7540 (interactive "p")
7541 (org-agenda-date-later arg 'hour))
7543 (defun org-agenda-date-earlier-hours (arg)
7544 "Change the time of this item, in hour steps."
7545 (interactive "p")
7546 (org-agenda-date-earlier arg 'hour))
7548 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7549 "Show new date stamp via text properties."
7550 ;; We use text properties to make this undoable
7551 (let ((inhibit-read-only t)
7552 (buffer-invisibility-spec))
7553 (setq stamp (concat " " prefix " => " stamp))
7554 (save-excursion
7555 (goto-char (point-max))
7556 (while (not (bobp))
7557 (when (equal marker (org-get-at-bol 'org-marker))
7558 (org-move-to-column (- (window-width) (length stamp)) t)
7559 (org-agenda-fix-tags-filter-overlays-at (point))
7560 (if (featurep 'xemacs)
7561 ;; Use `duplicable' property to trigger undo recording
7562 (let ((ex (make-extent nil nil))
7563 (gl (make-glyph stamp)))
7564 (set-glyph-face gl 'secondary-selection)
7565 (set-extent-properties
7566 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7567 (insert-extent ex (1- (point)) (point-at-eol)))
7568 (add-text-properties
7569 (1- (point)) (point-at-eol)
7570 (list 'display (org-add-props stamp nil
7571 'face 'secondary-selection))))
7572 (beginning-of-line 1))
7573 (beginning-of-line 0)))))
7575 (defun org-agenda-date-prompt (arg)
7576 "Change the date of this item. Date is prompted for, with default today.
7577 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7578 be used to request time specification in the time stamp."
7579 (interactive "P")
7580 (org-agenda-check-type t 'agenda 'timeline)
7581 (org-agenda-check-no-diary)
7582 (let* ((marker (or (org-get-at-bol 'org-marker)
7583 (org-agenda-error)))
7584 (buffer (marker-buffer marker))
7585 (pos (marker-position marker)))
7586 (org-with-remote-undo buffer
7587 (with-current-buffer buffer
7588 (widen)
7589 (goto-char pos)
7590 (if (not (org-at-timestamp-p t))
7591 (error "Cannot find time stamp"))
7592 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7593 (org-agenda-show-new-time marker org-last-changed-timestamp))
7594 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7596 (defun org-agenda-schedule (arg &optional time)
7597 "Schedule the item at point.
7598 ARG is passed through to `org-schedule'."
7599 (interactive "P")
7600 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7601 (org-agenda-check-no-diary)
7602 (let* ((marker (or (org-get-at-bol 'org-marker)
7603 (org-agenda-error)))
7604 (type (marker-insertion-type marker))
7605 (buffer (marker-buffer marker))
7606 (pos (marker-position marker))
7607 (org-insert-labeled-timestamps-at-point nil)
7609 (set-marker-insertion-type marker t)
7610 (org-with-remote-undo buffer
7611 (with-current-buffer buffer
7612 (widen)
7613 (goto-char pos)
7614 (setq ts (org-schedule arg time)))
7615 (org-agenda-show-new-time marker ts "S"))
7616 (message "Item scheduled for %s" ts)))
7618 (defun org-agenda-deadline (arg &optional time)
7619 "Schedule the item at point.
7620 ARG is passed through to `org-deadline'."
7621 (interactive "P")
7622 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7623 (org-agenda-check-no-diary)
7624 (let* ((marker (or (org-get-at-bol 'org-marker)
7625 (org-agenda-error)))
7626 (buffer (marker-buffer marker))
7627 (pos (marker-position marker))
7628 (org-insert-labeled-timestamps-at-point nil)
7630 (org-with-remote-undo buffer
7631 (with-current-buffer buffer
7632 (widen)
7633 (goto-char pos)
7634 (setq ts (org-deadline arg time)))
7635 (org-agenda-show-new-time marker ts "D"))
7636 (message "Deadline for this item set to %s" ts)))
7638 (defun org-agenda-action ()
7639 "Select entry for agenda action, or execute an agenda action.
7640 This command prompts for another letter. Valid inputs are:
7642 m Mark the entry at point for an agenda action
7643 s Schedule the marked entry to the date at the cursor
7644 d Set the deadline of the marked entry to the date at the cursor
7645 r Call `org-remember' with cursor date as the default date
7646 c Call `org-capture' with cursor date as the default date
7647 SPC Show marked entry in other window
7648 TAB Visit marked entry in other window
7650 The cursor may be at a date in the calendar, or in the Org agenda."
7651 (interactive)
7652 (let (ans)
7653 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7654 (setq ans (read-char-exclusive))
7655 (cond
7656 ((equal ans ?m)
7657 ;; Mark this entry
7658 (if (eq major-mode 'org-agenda-mode)
7659 (let ((m (or (org-get-at-bol 'org-hd-marker)
7660 (org-get-at-bol 'org-marker))))
7661 (if m
7662 (progn
7663 (move-marker org-agenda-action-marker
7664 (marker-position m) (marker-buffer m))
7665 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7666 (error "Don't know which entry to mark")))
7667 (error "This command works only in the agenda")))
7668 ((equal ans ?s)
7669 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7670 ((equal ans ?d)
7671 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7672 ((equal ans ?r)
7673 (org-agenda-do-action '(org-remember) t))
7674 ((equal ans ?c)
7675 (org-agenda-do-action '(org-capture) t))
7676 ((equal ans ?\ )
7677 (let ((cw (selected-window)))
7678 (org-switch-to-buffer-other-window
7679 (marker-buffer org-agenda-action-marker))
7680 (goto-char org-agenda-action-marker)
7681 (org-show-context 'agenda)
7682 (select-window cw)))
7683 ((equal ans ?\C-i)
7684 (org-switch-to-buffer-other-window
7685 (marker-buffer org-agenda-action-marker))
7686 (goto-char org-agenda-action-marker)
7687 (org-show-context 'agenda))
7688 (t (error "Invalid agenda action %c" ans)))))
7690 (defun org-agenda-do-action (form &optional current-buffer)
7691 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7692 (let ((org-overriding-default-time (org-get-cursor-date)))
7693 (if current-buffer
7694 (eval form)
7695 (if (not (marker-buffer org-agenda-action-marker))
7696 (error "No entry has been selected for agenda action")
7697 (with-current-buffer (marker-buffer org-agenda-action-marker)
7698 (save-excursion
7699 (save-restriction
7700 (widen)
7701 (goto-char org-agenda-action-marker)
7702 (eval form))))))))
7704 (defun org-agenda-clock-in (&optional arg)
7705 "Start the clock on the currently selected item."
7706 (interactive "P")
7707 (org-agenda-check-no-diary)
7708 (if (equal arg '(4))
7709 (org-clock-in arg)
7710 (let* ((marker (or (org-get-at-bol 'org-marker)
7711 (org-agenda-error)))
7712 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7713 marker))
7714 (pos (marker-position marker))
7715 newhead)
7716 (org-with-remote-undo (marker-buffer marker)
7717 (with-current-buffer (marker-buffer marker)
7718 (widen)
7719 (goto-char pos)
7720 (org-show-context 'agenda)
7721 (org-show-entry)
7722 (org-cycle-hide-drawers 'children)
7723 (org-clock-in arg)
7724 (setq newhead (org-get-heading)))
7725 (org-agenda-change-all-lines newhead hdmarker)))))
7727 (defun org-agenda-clock-out ()
7728 "Stop the currently running clock."
7729 (interactive)
7730 (unless (marker-buffer org-clock-marker)
7731 (error "No running clock"))
7732 (let ((marker (make-marker)) newhead)
7733 (org-with-remote-undo (marker-buffer org-clock-marker)
7734 (with-current-buffer (marker-buffer org-clock-marker)
7735 (save-excursion
7736 (save-restriction
7737 (widen)
7738 (goto-char org-clock-marker)
7739 (org-back-to-heading t)
7740 (move-marker marker (point))
7741 (org-clock-out)
7742 (setq newhead (org-get-heading))))))
7743 (org-agenda-change-all-lines newhead marker)
7744 (move-marker marker nil)))
7746 (defun org-agenda-clock-cancel (&optional arg)
7747 "Cancel the currently running clock."
7748 (interactive "P")
7749 (unless (marker-buffer org-clock-marker)
7750 (error "No running clock"))
7751 (org-with-remote-undo (marker-buffer org-clock-marker)
7752 (org-clock-cancel)))
7754 (defun org-agenda-clock-goto ()
7755 "Jump to the currently clocked in task within the agenda.
7756 If the currently clocked in task is not listed in the agenda
7757 buffer, display it in another window."
7758 (interactive)
7759 (let (pos)
7760 (mapc (lambda (o)
7761 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7762 (setq pos (overlay-start o))))
7763 (overlays-in (point-min) (point-max)))
7764 (cond (pos (goto-char pos))
7765 ;; If the currently clocked entry is not in the agenda
7766 ;; buffer, we visit it in another window:
7767 (org-clock-current-task
7768 (org-switch-to-buffer-other-window (org-clock-goto)))
7769 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7771 (defun org-agenda-diary-entry-in-org-file ()
7772 "Make a diary entry in the file `org-agenda-diary-file'."
7773 (let (d1 d2 char (text "") dp1 dp2)
7774 (if (equal (buffer-name) "*Calendar*")
7775 (setq d1 (calendar-cursor-to-date t)
7776 d2 (car calendar-mark-ring))
7777 (setq dp1 (get-text-property (point-at-bol) 'day))
7778 (unless dp1 (error "No date defined in current line"))
7779 (setq d1 (calendar-gregorian-from-absolute dp1)
7780 d2 (and (ignore-errors (mark))
7781 (save-excursion
7782 (goto-char (mark))
7783 (setq dp2 (get-text-property (point-at-bol) 'day)))
7784 (calendar-gregorian-from-absolute dp2))))
7785 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7786 (setq char (read-char-exclusive))
7787 (cond
7788 ((equal char ?d)
7789 (setq text (read-string "Day entry: "))
7790 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7791 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7792 ((equal char ?a)
7793 (setq d1 (list (car d1) (nth 1 d1)
7794 (read-number (format "Reference year [%d]: " (nth 2 d1))
7795 (nth 2 d1))))
7796 (setq text (read-string "Anniversary (use %d to show years): "))
7797 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7798 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7799 ((equal char ?b)
7800 (setq text (read-string "Block entry: "))
7801 (unless (and d1 d2 (not (equal d1 d2)))
7802 (error "No block of days selected"))
7803 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7804 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7805 ((equal char ?j)
7806 (org-switch-to-buffer-other-window
7807 (find-file-noselect org-agenda-diary-file))
7808 (require 'org-datetree)
7809 (org-datetree-find-date-create d1)
7810 (org-reveal t))
7811 (t (error "Invalid selection character `%c'" char)))))
7813 (defcustom org-agenda-insert-diary-strategy 'date-tree
7814 "Where in `org-agenda-diary-file' should new entries be added?
7815 Valid values:
7817 date-tree in the date tree, as child of the date
7818 top-level as top-level entries at the end of the file."
7819 :group 'org-agenda
7820 :type '(choice
7821 (const :tag "in a date tree" date-tree)
7822 (const :tag "as top level at end of file" top-level)))
7824 (defcustom org-agenda-insert-diary-extract-time nil
7825 "Non-nil means extract any time specification from the diary entry."
7826 :group 'org-agenda
7827 :type 'boolean)
7829 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7830 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7831 If TEXT is not empty, it will become the headline of the new entry, and
7832 the resulting entry will not be shown. When TEXT is empty, switch to
7833 `org-agenda-diary-file' and let the user finish the entry there."
7834 (let ((cw (current-window-configuration)))
7835 (org-switch-to-buffer-other-window
7836 (find-file-noselect org-agenda-diary-file))
7837 (widen)
7838 (goto-char (point-min))
7839 (cond
7840 ((eq type 'anniversary)
7841 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7842 (progn
7843 (or (org-on-heading-p t)
7844 (progn
7845 (outline-next-heading)
7846 (insert "* Anniversaries\n\n")
7847 (beginning-of-line -1)))))
7848 (outline-next-heading)
7849 (org-back-over-empty-lines)
7850 (backward-char 1)
7851 (insert "\n")
7852 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7853 (nth 2 d1) (car d1) (nth 1 d1) text)))
7854 ((eq type 'day)
7855 (let ((org-prefix-has-time t)
7856 (org-agenda-time-leading-zero t)
7857 fmt time time2)
7858 (if org-agenda-insert-diary-extract-time
7859 ;; Use org-format-agenda-item to parse text for a time-range and
7860 ;; remove it. FIXME: This is a hack, we should refactor
7861 ;; that function to make time extraction available separately
7862 (setq fmt (org-format-agenda-item nil text nil nil t)
7863 time (get-text-property 0 'time fmt)
7864 time2 (if (> (length time) 0)
7865 ;; split-string removes trailing ...... if
7866 ;; no end time given. First space
7867 ;; separates time from date.
7868 (concat " " (car (split-string time "\\.")))
7869 nil)
7870 text (get-text-property 0 'txt fmt)))
7871 (if (eq org-agenda-insert-diary-strategy 'top-level)
7872 (org-agenda-insert-diary-as-top-level text)
7873 (require 'org-datetree)
7874 (org-datetree-find-date-create d1)
7875 (org-agenda-insert-diary-make-new-entry text))
7876 (org-insert-time-stamp (org-time-from-absolute
7877 (calendar-absolute-from-gregorian d1))
7878 nil nil nil nil time2))
7879 (end-of-line 0))
7880 ((eq type 'block)
7881 (if (> (calendar-absolute-from-gregorian d1)
7882 (calendar-absolute-from-gregorian d2))
7883 (setq d1 (prog1 d2 (setq d2 d1))))
7884 (if (eq org-agenda-insert-diary-strategy 'top-level)
7885 (org-agenda-insert-diary-as-top-level text)
7886 (require 'org-datetree)
7887 (org-datetree-find-date-create d1)
7888 (org-agenda-insert-diary-make-new-entry text))
7889 (org-insert-time-stamp (org-time-from-absolute
7890 (calendar-absolute-from-gregorian d1)))
7891 (insert "--")
7892 (org-insert-time-stamp (org-time-from-absolute
7893 (calendar-absolute-from-gregorian d2)))
7894 (end-of-line 0)))
7895 (if (string-match "\\S-" text)
7896 (progn
7897 (set-window-configuration cw)
7898 (message "%s entry added to %s"
7899 (capitalize (symbol-name type))
7900 (abbreviate-file-name org-agenda-diary-file)))
7901 (org-reveal t)
7902 (message "Please finish entry here"))))
7904 (defun org-agenda-insert-diary-as-top-level (text)
7905 "Make new entry as a top-level entry at the end of the file.
7906 Add TEXT as headline, and position the cursor in the second line so that
7907 a timestamp can be added there."
7908 (widen)
7909 (goto-char (point-max))
7910 (or (bolp) (insert "\n"))
7911 (insert "* " text "\n")
7912 (if org-adapt-indentation (org-indent-to-column 2)))
7914 (defun org-agenda-insert-diary-make-new-entry (text)
7915 "Make new entry as last child of current entry.
7916 Add TEXT as headline, and position the cursor in the second line so that
7917 a timestamp can be added there."
7918 (let ((org-show-following-heading t)
7919 (org-show-siblings t)
7920 (org-show-hierarchy-above t)
7921 (org-show-entry-below t)
7922 col)
7923 (outline-next-heading)
7924 (org-back-over-empty-lines)
7925 (or (looking-at "[ \t]*$")
7926 (progn (insert "\n") (backward-char 1)))
7927 (org-insert-heading nil t)
7928 (org-do-demote)
7929 (setq col (current-column))
7930 (insert text "\n")
7931 (if org-adapt-indentation (org-indent-to-column col))
7932 (let ((org-show-following-heading t)
7933 (org-show-siblings t)
7934 (org-show-hierarchy-above t)
7935 (org-show-entry-below t))
7936 (org-show-context))))
7938 (defun org-agenda-diary-entry ()
7939 "Make a diary entry, like the `i' command from the calendar.
7940 All the standard commands work: block, weekly etc.
7941 When `org-agenda-diary-file' points to a file,
7942 `org-agenda-diary-entry-in-org-file' is called instead to create
7943 entries in that Org-mode file."
7944 (interactive)
7945 (org-agenda-check-type t 'agenda 'timeline)
7946 (if (not (eq org-agenda-diary-file 'diary-file))
7947 (org-agenda-diary-entry-in-org-file)
7948 (require 'diary-lib)
7949 (let* ((char (progn
7950 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7951 (read-char-exclusive)))
7952 (cmd (cdr (assoc char
7953 '((?d . insert-diary-entry)
7954 (?w . insert-weekly-diary-entry)
7955 (?m . insert-monthly-diary-entry)
7956 (?y . insert-yearly-diary-entry)
7957 (?a . insert-anniversary-diary-entry)
7958 (?b . insert-block-diary-entry)
7959 (?c . insert-cyclic-diary-entry)))))
7960 (oldf (symbol-function 'calendar-cursor-to-date))
7961 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7962 (point (point))
7963 (mark (or (mark t) (point))))
7964 (unless cmd
7965 (error "No command associated with <%c>" char))
7966 (unless (and (get-text-property point 'day)
7967 (or (not (equal ?b char))
7968 (get-text-property mark 'day)))
7969 (error "Don't know which date to use for diary entry"))
7970 ;; We implement this by hacking the `calendar-cursor-to-date' function
7971 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7972 (let ((calendar-mark-ring
7973 (list (calendar-gregorian-from-absolute
7974 (or (get-text-property mark 'day)
7975 (get-text-property point 'day))))))
7976 (unwind-protect
7977 (progn
7978 (fset 'calendar-cursor-to-date
7979 (lambda (&optional error dummy)
7980 (calendar-gregorian-from-absolute
7981 (get-text-property point 'day))))
7982 (call-interactively cmd))
7983 (fset 'calendar-cursor-to-date oldf))))))
7985 (defun org-agenda-execute-calendar-command (cmd)
7986 "Execute a calendar command from the agenda, with the date associated to
7987 the cursor position."
7988 (org-agenda-check-type t 'agenda 'timeline)
7989 (require 'diary-lib)
7990 (unless (get-text-property (point) 'day)
7991 (error "Don't know which date to use for calendar command"))
7992 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7993 (point (point))
7994 (date (calendar-gregorian-from-absolute
7995 (get-text-property point 'day)))
7996 ;; the following 2 vars are needed in the calendar
7997 (displayed-month (car date))
7998 (displayed-year (nth 2 date)))
7999 (unwind-protect
8000 (progn
8001 (fset 'calendar-cursor-to-date
8002 (lambda (&optional error dummy)
8003 (calendar-gregorian-from-absolute
8004 (get-text-property point 'day))))
8005 (call-interactively cmd))
8006 (fset 'calendar-cursor-to-date oldf))))
8008 (defun org-agenda-phases-of-moon ()
8009 "Display the phases of the moon for the 3 months around the cursor date."
8010 (interactive)
8011 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8013 (defun org-agenda-holidays ()
8014 "Display the holidays for the 3 months around the cursor date."
8015 (interactive)
8016 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8018 (defvar calendar-longitude)
8019 (defvar calendar-latitude)
8020 (defvar calendar-location-name)
8022 (defun org-agenda-sunrise-sunset (arg)
8023 "Display sunrise and sunset for the cursor date.
8024 Latitude and longitude can be specified with the variables
8025 `calendar-latitude' and `calendar-longitude'. When called with prefix
8026 argument, latitude and longitude will be prompted for."
8027 (interactive "P")
8028 (require 'solar)
8029 (let ((calendar-longitude (if arg nil calendar-longitude))
8030 (calendar-latitude (if arg nil calendar-latitude))
8031 (calendar-location-name
8032 (if arg "the given coordinates" calendar-location-name)))
8033 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8035 (defun org-agenda-goto-calendar ()
8036 "Open the Emacs calendar with the date at the cursor."
8037 (interactive)
8038 (org-agenda-check-type t 'agenda 'timeline)
8039 (let* ((day (or (get-text-property (point) 'day)
8040 (error "Don't know which date to open in calendar")))
8041 (date (calendar-gregorian-from-absolute day))
8042 (calendar-move-hook nil)
8043 (calendar-view-holidays-initially-flag nil)
8044 (calendar-view-diary-initially-flag nil))
8045 (calendar)
8046 (calendar-goto-date date)))
8048 ;;;###autoload
8049 (defun org-calendar-goto-agenda ()
8050 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8051 This is a command that has to be installed in `calendar-mode-map'."
8052 (interactive)
8053 (org-agenda-list nil (calendar-absolute-from-gregorian
8054 (calendar-cursor-to-date))
8055 nil))
8057 (defun org-agenda-convert-date ()
8058 (interactive)
8059 (org-agenda-check-type t 'agenda 'timeline)
8060 (let ((day (get-text-property (point) 'day))
8061 date s)
8062 (unless day
8063 (error "Don't know which date to convert"))
8064 (setq date (calendar-gregorian-from-absolute day))
8065 (setq s (concat
8066 "Gregorian: " (calendar-date-string date) "\n"
8067 "ISO: " (calendar-iso-date-string date) "\n"
8068 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8069 "Julian: " (calendar-julian-date-string date) "\n"
8070 "Astron. JD: " (calendar-astro-date-string date)
8071 " (Julian date number at noon UTC)\n"
8072 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8073 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8074 "French: " (calendar-french-date-string date) "\n"
8075 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8076 "Mayan: " (calendar-mayan-date-string date) "\n"
8077 "Coptic: " (calendar-coptic-date-string date) "\n"
8078 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8079 "Persian: " (calendar-persian-date-string date) "\n"
8080 "Chinese: " (calendar-chinese-date-string date) "\n"))
8081 (with-output-to-temp-buffer "*Dates*"
8082 (princ s))
8083 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8085 ;;; Bulk commands
8087 (defvar org-agenda-bulk-marked-entries nil
8088 "List of markers that refer to marked entries in the agenda.")
8090 (defun org-agenda-bulk-marked-p ()
8091 (eq (get-char-property (point-at-bol) 'type)
8092 'org-marked-entry-overlay))
8094 (defun org-agenda-bulk-mark (&optional arg)
8095 "Mark the entry at point for future bulk action."
8096 (interactive "p")
8097 (dotimes (i (max arg 1))
8098 (unless (org-get-at-bol 'org-agenda-diary-link)
8099 (let* ((m (org-get-at-bol 'org-hd-marker))
8101 (unless (org-agenda-bulk-marked-p)
8102 (unless m (error "Nothing to mark at point"))
8103 (push m org-agenda-bulk-marked-entries)
8104 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8105 (org-overlay-display ov "> "
8106 (org-get-todo-face "TODO")
8107 'evaporate)
8108 (overlay-put ov 'type 'org-marked-entry-overlay))
8109 (beginning-of-line 2)
8110 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8111 (beginning-of-line 2))
8112 (message "%d entries marked for bulk action"
8113 (length org-agenda-bulk-marked-entries))))))
8115 (defun org-agenda-bulk-mark-regexp (regexp)
8116 "Mark entries match REGEXP."
8117 (interactive "sMark entries matching regexp: ")
8118 (let (entries-marked)
8119 (save-excursion
8120 (goto-char (point-min))
8121 (goto-char (next-single-property-change (point) 'txt))
8122 (while (re-search-forward regexp nil t)
8123 (when (string-match regexp (get-text-property (point) 'txt))
8124 (setq entries-marked (+ entries-marked 1))
8125 (call-interactively 'org-agenda-bulk-mark))))
8126 (if (not entries-marked)
8127 (message "No entry matching this regexp."))))
8129 (defun org-agenda-bulk-unmark ()
8130 "Unmark the entry at point for future bulk action."
8131 (interactive)
8132 (when (org-agenda-bulk-marked-p)
8133 (org-agenda-bulk-remove-overlays
8134 (point-at-bol) (+ 2 (point-at-bol)))
8135 (setq org-agenda-bulk-marked-entries
8136 (delete (org-get-at-bol 'org-hd-marker)
8137 org-agenda-bulk-marked-entries)))
8138 (beginning-of-line 2)
8139 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8140 (beginning-of-line 2))
8141 (message "%d entries marked for bulk action"
8142 (length org-agenda-bulk-marked-entries)))
8144 (defun org-agenda-bulk-toggle ()
8145 "Toggle marking the entry at point for bulk action."
8146 (interactive)
8147 (if (org-agenda-bulk-marked-p)
8148 (org-agenda-bulk-unmark)
8149 (org-agenda-bulk-mark)))
8151 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8152 "Remove the mark overlays between BEG and END in the agenda buffer.
8153 BEG and END default to the buffer limits.
8155 This only removes the overlays, it does not remove the markers
8156 from the list in `org-agenda-bulk-marked-entries'."
8157 (interactive)
8158 (mapc (lambda (ov)
8159 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8160 (delete-overlay ov)))
8161 (overlays-in (or beg (point-min)) (or end (point-max)))))
8163 (defun org-agenda-bulk-remove-all-marks ()
8164 "Remove all marks in the agenda buffer.
8165 This will remove the markers, and the overlays."
8166 (interactive)
8167 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8168 (setq org-agenda-bulk-marked-entries nil)
8169 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8171 (defun org-agenda-bulk-action (&optional arg)
8172 "Execute an remote-editing action on all marked entries.
8173 The prefix arg is passed through to the command if possible."
8174 (interactive "P")
8175 ;; Make sure we have markers, and only valid ones
8176 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8177 (mapc
8178 (lambda (m)
8179 (unless (and (markerp m)
8180 (marker-buffer m)
8181 (buffer-live-p (marker-buffer m))
8182 (marker-position m))
8183 (error "Marker %s for bulk command is invalid" m)))
8184 org-agenda-bulk-marked-entries)
8186 ;; Prompt for the bulk command
8187 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8188 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8189 (when org-agenda-bulk-custom-functions
8190 (concat " Custom: ["
8191 (mapconcat (lambda(f) (char-to-string (car f)))
8192 org-agenda-bulk-custom-functions "")
8193 "]"))))
8194 (let* ((action (read-char-exclusive))
8195 (org-log-refile (if org-log-refile 'time nil))
8196 (entries (reverse org-agenda-bulk-marked-entries))
8197 redo-at-end
8198 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8199 (cond
8200 ((equal action ?$)
8201 (setq cmd '(org-agenda-archive)))
8203 ((equal action ?A)
8204 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8206 ((member action '(?r ?w))
8207 (setq rfloc (org-refile-get-location
8208 "Refile to"
8209 (marker-buffer (car org-agenda-bulk-marked-entries))
8210 org-refile-allow-creating-parent-nodes))
8211 (if (nth 3 rfloc)
8212 (setcar (nthcdr 3 rfloc)
8213 (move-marker (make-marker) (nth 3 rfloc)
8214 (or (get-file-buffer (nth 1 rfloc))
8215 (find-buffer-visiting (nth 1 rfloc))
8216 (error "This should not happen")))))
8218 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8219 redo-at-end t))
8221 ((equal action ?t)
8222 (setq state (org-icompleting-read
8223 "Todo state: "
8224 (with-current-buffer (marker-buffer (car entries))
8225 (mapcar 'list org-todo-keywords-1))))
8226 (setq cmd `(let ((org-inhibit-blocking t)
8227 (org-inhibit-logging 'note))
8228 (org-agenda-todo ,state))))
8230 ((memq action '(?- ?+))
8231 (setq tag (org-icompleting-read
8232 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8233 (with-current-buffer (marker-buffer (car entries))
8234 (delq nil
8235 (mapcar (lambda (x)
8236 (if (stringp (car x)) x)) org-tag-alist)))))
8237 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8239 ((memq action '(?s ?d))
8240 (let* ((date (unless arg
8241 (org-read-date
8242 nil nil nil
8243 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8244 (ans (if arg nil org-read-date-final-answer))
8245 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8246 (setq cmd `(let* ((bound (fboundp 'read-string))
8247 (old (and bound (symbol-function 'read-string))))
8248 (unwind-protect
8249 (progn
8250 (fset 'read-string (lambda (&rest ignore) ,ans))
8251 (eval '(,c1 arg)))
8252 (if bound
8253 (fset 'read-string old)
8254 (fmakunbound 'read-string)))))))
8256 ((equal action ?S)
8257 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8258 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8259 (let ((days (read-number
8260 (format "Scatter tasks across how many %sdays: "
8261 (if arg "week" "")) 7)))
8262 (setq cmd
8263 `(let ((distance (1+ (random ,days))))
8264 (if arg
8265 (let ((dist distance)
8266 (day-of-week
8267 (calendar-day-of-week
8268 (calendar-gregorian-from-absolute (org-today)))))
8269 (dotimes (i (1+ dist))
8270 (while (member day-of-week org-agenda-weekend-days)
8271 (incf distance)
8272 (incf day-of-week)
8273 (if (= day-of-week 7)
8274 (setq day-of-week 0)))
8275 (incf day-of-week)
8276 (if (= day-of-week 7)
8277 (setq day-of-week 0)))))
8278 ;; silently fail when try to replan a sexp entry
8279 (condition-case nil
8280 (let* ((date (calendar-gregorian-from-absolute
8281 (+ (org-today) distance)))
8282 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8283 (nth 2 date))))
8284 (org-agenda-schedule nil time))
8285 (error nil)))))))
8287 ((assoc action org-agenda-bulk-custom-functions)
8288 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8289 redo-at-end t))
8291 ((equal action ?f)
8292 (setq cmd (list (intern
8293 (org-icompleting-read "Function: "
8294 obarray 'fboundp t nil nil)))))
8296 (t (error "Invalid bulk action")))
8298 ;; Sort the markers, to make sure that parents are handled before children
8299 (setq entries (sort entries
8300 (lambda (a b)
8301 (cond
8302 ((equal (marker-buffer a) (marker-buffer b))
8303 (< (marker-position a) (marker-position b)))
8305 (string< (buffer-name (marker-buffer a))
8306 (buffer-name (marker-buffer b))))))))
8308 ;; Now loop over all markers and apply cmd
8309 (while (setq e (pop entries))
8310 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8311 (if (not pos)
8312 (progn (message "Skipping removed entry at %s" e)
8313 (setq cntskip (1+ cntskip)))
8314 (goto-char pos)
8315 (eval cmd)
8316 (setq org-agenda-bulk-marked-entries
8317 (delete e org-agenda-bulk-marked-entries))
8318 (setq cnt (1+ cnt))))
8319 (setq org-agenda-bulk-marked-entries nil)
8320 (org-agenda-bulk-remove-all-marks)
8321 (when redo-at-end (org-agenda-redo))
8322 (message "Acted on %d entries%s"
8324 (if (= cntskip 0)
8326 (format ", skipped %d (disappeared before their turn)"
8327 cntskip)))))
8329 ;;; Flagging notes
8331 (defun org-agenda-show-the-flagging-note ()
8332 "Display the flagging note in the other window.
8333 When called a second time in direct sequence, offer to remove the FLAGGING
8334 tag and (if present) the flagging note."
8335 (interactive)
8336 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8337 (win (selected-window))
8338 note heading newhead)
8339 (unless hdmarker
8340 (error "No linked entry at point"))
8341 (if (and (eq this-command last-command)
8342 (y-or-n-p "Unflag and remove any flagging note? "))
8343 (progn
8344 (org-agenda-remove-flag hdmarker)
8345 (let ((win (get-buffer-window "*Flagging Note*")))
8346 (and win (delete-window win)))
8347 (message "Entry unflaged"))
8348 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8349 (unless note
8350 (error "No flagging note"))
8351 (org-kill-new note)
8352 (org-switch-to-buffer-other-window "*Flagging Note*")
8353 (erase-buffer)
8354 (insert note)
8355 (goto-char (point-min))
8356 (while (re-search-forward "\\\\n" nil t)
8357 (replace-match "\n" t t))
8358 (goto-char (point-min))
8359 (select-window win)
8360 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8362 (defun org-agenda-remove-flag (marker)
8363 "Remove the FLAGGED tag and any flagging note in the entry."
8364 (let (newhead)
8365 (org-with-point-at marker
8366 (org-toggle-tag "FLAGGED" 'off)
8367 (org-entry-delete nil "THEFLAGGINGNOTE")
8368 (setq newhead (org-get-heading)))
8369 (org-agenda-change-all-lines newhead marker)
8370 (message "Entry unflaged")))
8372 (defun org-agenda-get-any-marker (&optional pos)
8373 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8374 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8376 ;;; Appointment reminders
8378 (defvar appt-time-msg-list)
8380 ;;;###autoload
8381 (defun org-agenda-to-appt (&optional refresh filter)
8382 "Activate appointments found in `org-agenda-files'.
8383 With a \\[universal-argument] prefix, refresh the list of
8384 appointments.
8386 If FILTER is t, interactively prompt the user for a regular
8387 expression, and filter out entries that don't match it.
8389 If FILTER is a string, use this string as a regular expression
8390 for filtering entries out.
8392 FILTER can also be an alist with the car of each cell being
8393 either 'headline or 'category. For example:
8395 '((headline \"IMPORTANT\")
8396 (category \"Work\"))
8398 will only add headlines containing IMPORTANT or headlines
8399 belonging to the \"Work\" category."
8400 (interactive "P")
8401 (if refresh (setq appt-time-msg-list nil))
8402 (if (eq filter t)
8403 (setq filter (read-from-minibuffer "Regexp filter: ")))
8404 (let* ((cnt 0) ; count added events
8405 (org-agenda-new-buffers nil)
8406 (org-deadline-warning-days 0)
8407 ;; Do not use `org-today' here because appt only takes
8408 ;; time and without date as argument, so it may pass wrong
8409 ;; information otherwise
8410 (today (org-date-to-gregorian
8411 (time-to-days (current-time))))
8412 (org-agenda-restrict nil)
8413 (files (org-agenda-files 'unrestricted)) entries file)
8414 ;; Get all entries which may contain an appt
8415 (org-prepare-agenda-buffers files)
8416 (while (setq file (pop files))
8417 (setq entries
8418 (append entries
8419 (org-agenda-get-day-entries
8420 file today :timestamp :scheduled :deadline))))
8421 (setq entries (delq nil entries))
8422 ;; Map thru entries and find if we should filter them out
8423 (mapc
8424 (lambda(x)
8425 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8426 (cat (get-text-property 1 'org-category x))
8427 (tod (get-text-property 1 'time-of-day x))
8428 (ok (or (null filter)
8429 (and (stringp filter) (string-match filter evt))
8430 (and (listp filter)
8431 (or (string-match
8432 (cadr (assoc 'category filter)) cat)
8433 (string-match
8434 (cadr (assoc 'headline filter)) evt))))))
8435 ;; FIXME: Shall we remove text-properties for the appt text?
8436 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8437 (when (and ok tod)
8438 (setq tod (concat "00" (number-to-string tod))
8439 tod (when (string-match
8440 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8441 (concat (match-string 1 tod) ":"
8442 (match-string 2 tod))))
8443 (appt-add tod evt)
8444 (setq cnt (1+ cnt))))) entries)
8445 (org-release-buffers org-agenda-new-buffers)
8446 (if (eq cnt 0)
8447 (message "No event to add")
8448 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8450 (defun org-agenda-todayp (date)
8451 "Does DATE mean today, when considering `org-extend-today-until'?"
8452 (let ((today (org-today))
8453 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8454 date)))
8455 (eq date today)))
8457 (defun org-agenda-todo-yesterday (&optional arg)
8458 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8459 (interactive "P")
8460 (let* ((hour (third (decode-time
8461 (org-current-time))))
8462 (org-extend-today-until (1+ hour)))
8463 (org-agenda-todo arg)))
8465 (provide 'org-agenda)
8467 ;;; org-agenda.el ends here