summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-recoll.el
blob: 7c64c7d9eabea456fa8ffa871dfdfcd7ccacc78b (plain)
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
;; jao-recoll.el -- Displaying recoll queries

;; Copyright (c) 2017, 2020, 2021 Jose Antonio Ortega Ruiz

;; This file 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 of the License, or
;; (at your option) any later version.

;; This file 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Wed Nov 01, 2017 18:14


;;; Comentary:

;; A simple interactive command to perform recoll queries and display
;; its results using org-mode.

;;; Code:


(require 'org)

(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+\\[\\([^]]+\\)\\].+")

(defvar jao-recoll-flags "-A -p 5 -n 20")

;;;###autoload
(defun jao-recoll (keywords)
  "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)))

(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)



(provide 'jao-recoll)
;;; jao-recoll.el ends here