summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2022-08-14 19:33:42 +0100
committerjao <jao@gnu.org>2022-08-14 19:33:42 +0100
commitf36b727a91c9c6a51dc50498a7074725ca130d17 (patch)
treec98a4a24d5a02918db26b8a53a34fa2ac8b93e31
parent61c01d3980c33727d5fb3e0fbc0d2030396ac0d8 (diff)
search for snippet text in text mime types
-rw-r--r--consult-recoll.el32
-rw-r--r--readme.org7
2 files changed, 29 insertions, 10 deletions
diff --git a/consult-recoll.el b/consult-recoll.el
index d6b57bd..1f31431 100644
--- a/consult-recoll.el
+++ b/consult-recoll.el
@@ -131,19 +131,33 @@ Set to nil to use the default 'title (path)' format."
(defsubst consult-recoll--snippets (&optional candidate)
(get-text-property 0 'snippets (or candidate consult-recoll--current "")))
-(defsubst consult-recoll--find-file (file &optional _page) (find-file file))
+(defun consult-recoll--search-snippet (candidate mime)
+ "When CANDIDATE is the text of a snippet, search for it in current buffer."
+ (when (string-match "^\s+0 : " candidate)
+ (let ((txt (replace-match "" nil nil candidate)))
+ (goto-char (point-min))
+ (when (or (search-forward txt nil t)
+ (and (derived-mode-p 'org-mode)
+ (search-forward (replace-regexp-in-string "\\]\\].+" ""
+ txt nil t)))
+ (and (string= mime "text/html")
+ (search-forward (substring txt 0 (/ (length txt) 2)) nil t)))
+ (goto-char (match-beginning 0))
+ (when (derived-mode-p 'org-mode) (org-reveal))))))
(defun consult-recoll--open (candidate)
"Open file of corresponding completion CANDIDATE."
(when candidate
- (let ((url (consult-recoll--candidate-url candidate))
- (open (alist-get (consult-recoll--candidate-mime candidate)
- consult-recoll-open-fns
- (or consult-recoll-open-fn 'consult-recoll--find-file)
- nil 'string=)))
- (if consult-recoll-inline-snippets
- (funcall open url (consult-recoll--candidate-page candidate))
- (funcall open url)))))
+ (let* ((url (consult-recoll--candidate-url candidate))
+ (mime (consult-recoll--candidate-mime candidate))
+ (open (or (cdr (assoc mime consult-recoll-open-fns))
+ consult-recoll-open-fn
+ (lambda (f &optional _ignored) (find-file f)))))
+ (if (not consult-recoll-inline-snippets)
+ (funcall open url)
+ (funcall open url (consult-recoll--candidate-page candidate))
+ (when (string-prefix-p "text/" mime)
+ (consult-recoll--search-snippet candidate mime))))))
(defun consult-recoll--transformer (str)
"Decode STR, as returned by recollq."
diff --git a/readme.org b/readme.org
index 738d29c..54e67a7 100644
--- a/readme.org
+++ b/readme.org
@@ -160,7 +160,12 @@ it directly via =M-x package-install RET consult-recoll RET=.
If ~consult-recoll-inline-snippets~ is set, the functions above take two
arguments: the URL of the file to open and, if present, the snippet page
number (or ~nil~ if it is not available, e.g., because the selected candidate
- is the one showing the document data)
+ is the one showing the document data).
+
+ If the selected candidate is a snippet corresponding to a text MIME and the
+ page number of the snippet is 0 (as is often the case, since text files are
+ normally not paginated), ~consult-recoll~ will perform a search for the
+ snippet text after opening the file.
See also [[#embark][Integration with embark-collect]] for an alternative way of listing
and opening search results using embark.