diff options
-rw-r--r-- | lib/doc/jao-recoll.el | 105 |
1 files changed, 66 insertions, 39 deletions
diff --git a/lib/doc/jao-recoll.el b/lib/doc/jao-recoll.el index 0c781d9..a49d183 100644 --- a/lib/doc/jao-recoll.el +++ b/lib/doc/jao-recoll.el @@ -31,57 +31,84 @@ (define-derived-mode recoll-mode org-mode "Recoll" "Simple mode for showing recoll query results" - (setq org-startup-folded nil) (read-only-mode 1)) (defvar jao-recoll--file-regexp - "\\(\\w+/\\w+\\)\t+\\[\\([^]]+\\)\\]\t+\\[\\([^]]+\\)\\].+") + "\\(\\w+/.+\\)\t+\\[\\([^]]+\\)\\]\t+\\[\\([^]]+\\)\\].+") -(defvar jao-recoll-flags "-A -p 5 -n 50") +(defvar jao-recoll-flags "-A -p 5 -n 100") + +(defvar jao-recoll-single-buffer t) +(defvar-local jao-recoll--last-query nil) +(defvar-local jao-recoll--last-full-query nil) + +(defun jao-recoll-show-query () + (interactive) + (message (concat jao-recoll--last-query "\n" + jao-recoll--last-full-query))) + +(defun jao-recoll-requery () + (interactive) + (jao-recoll jao-recoll--last-query)) + +(defun jao-recoll--buffer (q) + (get-buffer-create (if jao-recoll-single-buffer + "*Recoll*" + (format "*Recoll: '%s'*" q)))) ;;;###autoload -(defun jao-recoll (keywords) +(defun jao-recoll (&optional prefix-query) "Performs a query using recoll and shows the results in a buffer using org mode." - (interactive "sRecoll query string: ") - (with-current-buffer (get-buffer-create (format "* Recoll: '%s' *" keywords)) - (read-only-mode -1) - (delete-region (point-min) (point-max)) - (let ((c (format "recoll %s -t %s" - jao-recoll-flags (shell-quote-argument keywords))) - (lnk)) - (shell-command c t)) - (goto-char (point-min)) - (when (looking-at-p "Recoll query:") - (let ((kill-whole-line t)) (kill-line)) - (forward-line 1)) - (open-line 1) - (while (search-forward-regexp jao-recoll--file-regexp nil t) - (setq lnk - (cond ((string= (match-string 1) "application/pdf") - (concat "doc:" (file-name-nondirectory (match-string 2)))) - ((string= (match-string 1) "message/rfc822") - (concat "message:" (substring (match-string 2) 7))) - (t (match-string 2)))) - (replace-match (format "* [[%s][\\3]] (\\1)" lnk)) - (forward-line) - (let ((kill-whole-line t)) (kill-line)) - (while (and (not (eobp)) (not (looking-at-p "/SNIPPETS"))) - (if (looking-at "^\\([1-9][0-9]*\\) : ") - (replace-match (format " - [[%s::\\1][\\1]] : " lnk)) - (insert " - ")) - (forward-line 1)) - (unless (eobp) (let ((kill-whole-line t)) (kill-line)))) - (recoll-mode) - (pop-to-buffer (current-buffer)) - (goto-char (point-min)) - (org-next-visible-heading 1))) + (interactive) + (let* ((query (read-string "Recoll query: " prefix-query)) + (cmd (format "recoll %s -t %s" + jao-recoll-flags (shell-quote-argument query))) + (inhibit-read-only t) + (lnk nil)) + (with-current-buffer (jao-recoll--buffer query) + (recoll-mode) + (delete-region (point-min) (point-max)) + (shell-command cmd t) + (setq jao-recoll--last-query query) + (goto-char (point-min)) + (when (looking-at-p "Recoll query:") + (setq jao-recoll--last-full-query + (string-trim (thing-at-point 'line))) + (let ((kill-whole-line nil)) + (kill-line) + (insert query)) + (forward-line 2)) + (open-line 1) + (while (search-forward-regexp jao-recoll--file-regexp nil t) + (setq lnk + (cond ((string= (match-string 1) "application/pdf") + (concat "doc:" + (file-name-nondirectory (match-string 2)))) + ((string= (match-string 1) "message/rfc822") + (concat "message:" (substring (match-string 2) 7))) + (t (match-string 2)))) + (replace-match (format "* [[%s][\\3]] (\\1)" lnk)) + (forward-line) + (let ((kill-whole-line t)) (kill-line)) + (while (and (not (eobp)) (not (looking-at-p "/SNIPPETS"))) + (if (looking-at "^\\([1-9][0-9]*\\) : ") + (replace-match (format " - [[%s::\\1][\\1]] : " lnk)) + (insert " - ")) + (forward-line 1)) + (unless (eobp) (let ((kill-whole-line t)) (kill-line)))) + (pop-to-buffer (current-buffer)) + (goto-char (point-min)) + (org-next-visible-heading 1) + (org-overview) + (jao-recoll-show-query)))) (define-key recoll-mode-map [?n] 'org-next-link) (define-key recoll-mode-map [?p] 'org-previous-link) (define-key recoll-mode-map [?q] 'bury-buffer) -(define-key recoll-mode-map [?r] 'jao-recoll) - +(define-key recoll-mode-map [?r] 'jao-recoll-requery) +(define-key recoll-mode-map [?q] 'jao-recoll-requery) +(define-key recoll-mode-map [?w] 'jao-recoll-show-query) (provide 'jao-recoll) |