org-agenda: Refactor org-agenda-overriding-header code

* lisp/org-agenda.el (org-agenda--insert-overriding-header): Add macro.
(org-agenda-list)
(org-search-view)
(org-todo-list)
(org-tags-view): Use macro.
(org-agenda-overriding-header): Update docstring.

* etc/ORG-NEWS: Explain that header can be disabled with empty string.

Replace org-agenda-overriding-header tests in these four functions with
calls to a macro, eliminating the duplicate code.  Also, disable the
header when the variable is set to the empty string.
This commit is contained in:
Adam Porter 2017-08-19 21:26:12 -05:00 committed by Nicolas Goaziou
parent 601b8e1d52
commit b6c5a174da
2 changed files with 82 additions and 61 deletions

View File

@ -135,6 +135,10 @@ See docstring for details.
=org-agenda-tags-column= can now be set to =auto=, which will =org-agenda-tags-column= can now be set to =auto=, which will
automatically align tags to the right edge of the window. This is now automatically align tags to the right edge of the window. This is now
the default setting. the default setting.
**** Disable =org-agenda-overriding-header= by setting to empty string
The =org-agenda-overriding-header= inserted into agenda views can now be
disabled by setting it to an empty string.
*** New value for ~org-publish-sitemap-sort-folders~ *** New value for ~org-publish-sitemap-sort-folders~

View File

@ -2064,6 +2064,22 @@ works you probably want to add it to `org-agenda-custom-commands' for good."
(setcdr ass (cdr entry)) (setcdr ass (cdr entry))
(push entry org-agenda-custom-commands)))) (push entry org-agenda-custom-commands))))
(defmacro org-agenda--insert-overriding-header (default)
"Insert header into agenda view depending on value of `org-agenda-overriding-header'.
If the empty string, don't insert a header. If any other string,
insert it as a header. If nil, insert DEFAULT, which should
evaluate to a string."
(declare (debug (form)) (indent defun))
`(pcase org-agenda-overriding-header
("" nil) ; Don't insert a header if set to empty string
;; Insert user-specified string
((pred stringp) (insert
(org-add-props (copy-sequence org-agenda-overriding-header)
nil 'face 'org-agenda-structure)
"\n"))
;; When nil, make string automatically and insert it
((pred null) (insert ,default))))
;;; Define the org-agenda-mode ;;; Define the org-agenda-mode
(defvar org-agenda-mode-map (make-sparse-keymap) (defvar org-agenda-mode-map (make-sparse-keymap)
@ -4155,10 +4171,8 @@ items if they have an hour specification like [h]h:mm."
(w1 (org-days-to-iso-week d1)) (w1 (org-days-to-iso-week d1))
(w2 (org-days-to-iso-week d2))) (w2 (org-days-to-iso-week d2)))
(setq s (point)) (setq s (point))
(if org-agenda-overriding-header (org-agenda--insert-overriding-header
(insert (org-add-props (copy-sequence org-agenda-overriding-header) (concat (org-agenda-span-name span)
nil 'face 'org-agenda-structure) "\n")
(insert (org-agenda-span-name span)
"-agenda" "-agenda"
(if (< (- d2 d1) 350) (if (< (- d2 d1) 350)
(if (= w1 w2) (if (= w1 w2)
@ -4576,9 +4590,8 @@ in `org-agenda-text-search-extra-files'."
(goto-char (1- end)))))))))) (goto-char (1- end))))))))))
(setq rtn (nreverse ee)) (setq rtn (nreverse ee))
(setq rtnall (append rtnall rtn))) (setq rtnall (append rtnall rtn)))
(if org-agenda-overriding-header (org-agenda--insert-overriding-header
(insert (org-add-props (copy-sequence org-agenda-overriding-header) (with-temp-buffer
nil 'face 'org-agenda-structure) "\n")
(insert "Search words: ") (insert "Search words: ")
(add-text-properties (point-min) (1- (point)) (add-text-properties (point-min) (1- (point))
(list 'face 'org-agenda-structure)) (list 'face 'org-agenda-structure))
@ -4594,7 +4607,8 @@ Press `\\[org-agenda-manipulate-query-add]', \
`\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \ `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
`\\[universal-argument] \\[org-agenda-redo]' to edit\n")) `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
(add-text-properties pos (1- (point)) (add-text-properties pos (1- (point))
(list 'face 'org-agenda-structure)))) (list 'face 'org-agenda-structure)))
(buffer-string)))
(org-agenda-mark-header-line (point-min)) (org-agenda-mark-header-line (point-min))
(when rtnall (when rtnall
(insert (org-agenda-finalize-entries rtnall 'search) "\n")) (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
@ -4672,9 +4686,8 @@ for a keyword. A numeric prefix directly selects the Nth keyword in
(org-check-agenda-file file) (org-check-agenda-file file)
(setq rtn (org-agenda-get-day-entries file date :todo)) (setq rtn (org-agenda-get-day-entries file date :todo))
(setq rtnall (append rtnall rtn)))) (setq rtnall (append rtnall rtn))))
(if org-agenda-overriding-header (org-agenda--insert-overriding-header
(insert (org-add-props (copy-sequence org-agenda-overriding-header) (with-temp-buffer
nil 'face 'org-agenda-structure) "\n")
(insert "Global list of TODO items of type: ") (insert "Global list of TODO items of type: ")
(add-text-properties (point-min) (1- (point)) (add-text-properties (point-min) (1- (point))
(list 'face 'org-agenda-structure (list 'face 'org-agenda-structure
@ -4696,7 +4709,8 @@ for a keyword. A numeric prefix directly selects the Nth keyword in
(insert " " s)) (insert " " s))
kwds)) kwds))
(insert "\n")) (insert "\n"))
(add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))) (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
(buffer-string)))
(org-agenda-mark-header-line (point-min)) (org-agenda-mark-header-line (point-min))
(when rtnall (when rtnall
(insert (org-agenda-finalize-entries rtnall 'todo) "\n")) (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
@ -4774,9 +4788,8 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
matcher matcher
org--matcher-tags-todo-only)) org--matcher-tags-todo-only))
(setq rtnall (append rtnall rtn)))))))) (setq rtnall (append rtnall rtn))))))))
(if org-agenda-overriding-header (org-agenda--insert-overriding-header
(insert (org-add-props (copy-sequence org-agenda-overriding-header) (with-temp-buffer
nil 'face 'org-agenda-structure) "\n")
(insert "Headlines with TAGS match: ") (insert "Headlines with TAGS match: ")
(add-text-properties (point-min) (1- (point)) (add-text-properties (point-min) (1- (point))
(list 'face 'org-agenda-structure (list 'face 'org-agenda-structure
@ -4791,7 +4804,8 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
"Press `\\[universal-argument] \\[org-agenda-redo]' \ "Press `\\[universal-argument] \\[org-agenda-redo]' \
to search again with new search string\n"))) to search again with new search string\n")))
(add-text-properties pos (1- (point)) (add-text-properties pos (1- (point))
(list 'face 'org-agenda-structure))) (list 'face 'org-agenda-structure))
(buffer-string)))
(org-agenda-mark-header-line (point-min)) (org-agenda-mark-header-line (point-min))
(when rtnall (when rtnall
(insert (org-agenda-finalize-entries rtnall 'tags) "\n")) (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
@ -4815,8 +4829,11 @@ used by user-defined selections using `org-agenda-skip-function'.")
(defvar org-agenda-overriding-header nil (defvar org-agenda-overriding-header nil
"When set during agenda, todo and tags searches it replaces the header. "When set during agenda, todo and tags searches it replaces the header.
This variable should not be set directly, but custom commands can bind it If an empty string, no header will be inserted. If any other
in the options section.") string, it will be inserted as a header. If nil, a header will
be generated automatically according to the command. This
variable should not be set directly, but custom commands can bind
it in the options section.")
(defun org-agenda-skip-entry-if (&rest conditions) (defun org-agenda-skip-entry-if (&rest conditions)
"Skip entry if any of CONDITIONS is true. "Skip entry if any of CONDITIONS is true.