1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
;;; org-interactive-query.el --- Interactive modification of agenda query
;;
;; Copyright 2007-2021 Free Software Foundation, Inc.
;;
;; Author: Christopher League <league at contrapunctus dot net>
;; Version: 1.0
;; Keywords: org, wp
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This file is DEPRECATED. The functionality here has been mostly subsumed by
;; features added to Org agenda, especially commands that begin with
;; org-agenda-filter*.
;; This library implements interactive modification of a tags/todo query
;; in the org-agenda. It adds 4 keys to the agenda
;;
;; / add a keyword as a positive selection criterion
;; \ add a keyword as a newgative selection criterion
;; = clear a keyword from the selection string
;; ;
(require 'org)
(org-defkey org-agenda-mode-map "=" 'org-agenda-query-clear-cmd)
(org-defkey org-agenda-mode-map "/" 'org-agenda-query-and-cmd)
(org-defkey org-agenda-mode-map ";" 'org-agenda-query-or-cmd)
(org-defkey org-agenda-mode-map "\\" 'org-agenda-query-not-cmd)
;;; Agenda interactive query manipulation
(defcustom org-agenda-query-selection-single-key t
"Non-nil means query manipulation exits after first change.
When nil, you have to press RET to exit it.
During query selection, you can toggle this flag with `C-c'.
This variable can also have the value `expert'. In this case, the window
displaying the tags menu is not even shown, until you press C-c again."
:group 'org-agenda
:type '(choice
(const :tag "No" nil)
(const :tag "Yes" t)
(const :tag "Expert" expert)))
(defun org-agenda-query-selection (current op table &optional todo-table)
"Fast query manipulation with single keys.
CURRENT is the current query string, OP is the initial
operator (one of \"+|-=\"), TABLE is an alist of tags and
corresponding keys, possibly with grouping information.
TODO-TABLE is a similar table with TODO keywords, should these
have keys assigned to them. If the keys are nil, a-z are
automatically assigned. Returns the new query string, or nil to
not change the current one."
(let* ((fulltable (append table todo-table))
(maxlen (apply 'max (mapcar
(lambda (x)
(if (stringp (car x)) (string-width (car x)) 0))
fulltable)))
(fwidth (+ maxlen 3 1 3))
(ncol (/ (- (window-width) 4) fwidth))
(expert (eq org-agenda-query-selection-single-key 'expert))
(exit-after-next org-agenda-query-selection-single-key)
(done-keywords org-done-keywords)
tbl char cnt e groups ingroup
tg c2 c c1 ntable rtn)
(save-window-excursion
(if expert
(set-buffer (get-buffer-create " *Org tags*"))
(delete-other-windows)
(split-window-vertically)
(org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
(erase-buffer)
(setq-local org-done-keywords done-keywords)
(insert "Query: " current "\n")
(org-agenda-query-op-line op)
(insert "\n\n")
(org-fast-tag-show-exit exit-after-next)
(setq tbl fulltable char ?a cnt 0)
(while (setq e (pop tbl))
(cond
((equal e '(:startgroup))
(push '() groups) (setq ingroup t)
(when (not (= cnt 0))
(setq cnt 0)
(insert "\n"))
(insert "{ "))
((equal e '(:endgroup))
(setq ingroup nil cnt 0)
(insert "}\n"))
(t
(setq tg (car e) c2 nil)
(if (cdr e)
(setq c (cdr e))
;; automatically assign a character.
(setq c1 (string-to-char
(downcase (substring
tg (if (= (string-to-char tg) ?@) 1 0)))))
(if (or (rassoc c1 ntable) (rassoc c1 table))
(while (or (rassoc char ntable) (rassoc char table))
(setq char (1+ char)))
(setq c2 c1))
(setq c (or c2 char)))
(if ingroup (push tg (car groups)))
(setq tg (org-add-props tg nil 'face
(cond
((not (assoc tg table))
(org-get-todo-face tg))
(t nil))))
(if (and (= cnt 0) (not ingroup)) (insert " "))
(insert "[" c "] " tg (make-string
(- fwidth 4 (length tg)) ?\ ))
(push (cons tg c) ntable)
(when (= (setq cnt (1+ cnt)) ncol)
(insert "\n")
(if ingroup (insert " "))
(setq cnt 0)))))
(setq ntable (nreverse ntable))
(insert "\n")
(goto-char (point-min))
(if (and (not expert) (fboundp 'fit-window-to-buffer))
(fit-window-to-buffer))
(setq rtn
(catch 'exit
(while t
(message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
(if groups " [!] no groups" " [!]groups")
(if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
(setq c (let ((inhibit-quit t)) (read-char-exclusive)))
(cond
((= c ?\r) (throw 'exit t))
((= c ?!)
(setq groups (not groups))
(goto-char (point-min))
(while (re-search-forward "[{}]" nil t) (replace-match " ")))
((= c ?\C-c)
(if (not expert)
(org-fast-tag-show-exit
(setq exit-after-next (not exit-after-next)))
(setq expert nil)
(delete-other-windows)
(split-window-vertically)
(org-switch-to-buffer-other-window " *Org tags*")
(and (fboundp 'fit-window-to-buffer)
(fit-window-to-buffer))))
((or (= c ?\C-g)
(and (= c ?q) (not (rassoc c ntable))))
(setq quit-flag t))
((= c ?\ )
(setq current "")
(if exit-after-next (setq exit-after-next 'now)))
((= c ?\[) ; clear left
(org-agenda-query-decompose current)
(setq current (concat "/" (match-string 2 current)))
(if exit-after-next (setq exit-after-next 'now)))
((= c ?\]) ; clear right
(org-agenda-query-decompose current)
(setq current (match-string 1 current))
(if exit-after-next (setq exit-after-next 'now)))
((= c ?\t)
(condition-case nil
(setq current (read-string "Query: " current))
(quit))
(if exit-after-next (setq exit-after-next 'now)))
;; operators
((or (= c ?/) (= c ?+)) (setq op "+"))
((or (= c ?\;) (= c ?|)) (setq op "|"))
((or (= c ?\\) (= c ?-)) (setq op "-"))
((= c ?=) (setq op "="))
;; todos
((setq e (rassoc c todo-table) tg (car e))
(setq current (org-agenda-query-manip
current op groups 'todo tg))
(if exit-after-next (setq exit-after-next 'now)))
;; tags
((setq e (rassoc c ntable) tg (car e))
(setq current (org-agenda-query-manip
current op groups 'tag tg))
(if exit-after-next (setq exit-after-next 'now))))
(if (eq exit-after-next 'now) (throw 'exit t))
(goto-char (point-min))
(beginning-of-line 1)
(delete-region (point) (point-at-eol))
(insert "Query: " current)
(beginning-of-line 2)
(delete-region (point) (point-at-eol))
(org-agenda-query-op-line op)
(goto-char (point-min)))))
(if rtn current nil))))
(defun org-agenda-query-op-line (op)
(insert "Operator: "
(org-agenda-query-op-entry (equal op "+") "/+" "and")
(org-agenda-query-op-entry (equal op "|") ";|" "or")
(org-agenda-query-op-entry (equal op "-") "\\-" "not")
(org-agenda-query-op-entry (equal op "=") "=" "clear")))
(defun org-agenda-query-op-entry (matchp chars str)
(if matchp
(org-add-props (format "[%s %s] " chars (upcase str))
nil 'face 'org-todo)
(format "[%s]%s " chars str)))
(defun org-agenda-query-decompose (current)
(string-match "\\([^/]*\\)/?\\(.*\\)" current))
(defun org-agenda-query-clear (current prefix tag)
(if (string-match (concat prefix "\\b" (regexp-quote tag) "\\b") current)
(replace-match "" t t current)
current))
(defun org-agenda-query-manip (current op groups kind tag)
"Apply an operator to a query string and a tag.
CURRENT is the current query string, OP is the operator, GROUPS is a
list of lists of tags that are mutually exclusive. KIND is 'tag for a
regular tag, or 'todo for a TODO keyword, and TAG is the tag or
keyword string."
;; If this tag is already in query string, remove it.
(setq current (org-agenda-query-clear current "[-\\+&|]?" tag))
(if (equal op "=") current
;; When using AND, also remove mutually exclusive tags.
(if (equal op "+")
(loop for g in groups do
(if (member tag g)
(mapc (lambda (x)
(setq current
(org-agenda-query-clear current "\\+" x)))
g))))
;; Decompose current query into q1 (tags) and q2 (TODOs).
(org-agenda-query-decompose current)
(let* ((q1 (match-string 1 current))
(q2 (match-string 2 current)))
(cond
((eq kind 'tag)
(concat q1 op tag "/" q2))
;; It's a TODO; when using AND, drop all other TODOs.
((equal op "+")
(concat q1 "/+" tag))
(t
(concat q1 "/" q2 op tag))))))
(defun org-agenda-query-global-todo-keys (&optional files)
"Return alist of all TODO keywords and their fast keys, in all FILES."
(let (alist)
(unless (and files (car files))
(setq files (org-agenda-files)))
(save-excursion
(loop for f in files do
(set-buffer (find-file-noselect f))
(loop for k in org-todo-key-alist do
(setq alist (org-agenda-query-merge-todo-key
alist k)))))
alist))
(defun org-agenda-query-merge-todo-key (alist entry)
(let (e)
(cond
;; if this is not a keyword (:startgroup, etc), ignore it
((not (stringp (car entry))))
;; if keyword already exists, replace char if it's null
((setq e (assoc (car entry) alist))
(when (null (cdr e)) (setcdr e (cdr entry))))
;; if char already exists, prepend keyword but drop char
((rassoc (cdr entry) alist)
(message "TRACE POSITION 2")
(setq alist (cons (cons (car entry) nil) alist)))
;; else, prepend COPY of entry
(t
(setq alist (cons (cons (car entry) (cdr entry)) alist)))))
alist)
(defun org-agenda-query-generic-cmd (op)
"Activate query manipulation with OP as initial operator."
(let ((q (org-agenda-query-selection org-agenda-query-string op
org-tag-alist
(org-agenda-query-global-todo-keys))))
(when q
(setq org-agenda-query-string q)
(org-agenda-redo))))
(defun org-agenda-query-clear-cmd ()
"Activate query manipulation, to clear a tag from the string."
(interactive)
(org-agenda-query-generic-cmd "="))
(defun org-agenda-query-and-cmd ()
"Activate query manipulation, initially using the AND (+) operator."
(interactive)
(org-agenda-query-generic-cmd "+"))
(defun org-agenda-query-or-cmd ()
"Activate query manipulation, initially using the OR (|) operator."
(interactive)
(org-agenda-query-generic-cmd "|"))
(defun org-agenda-query-not-cmd ()
"Activate query manipulation, initially using the NOT (-) operator."
(interactive)
(org-agenda-query-generic-cmd "-"))
(provide 'org-interactive-query)
|