org-interactive-query.el 11 KB

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