diff options
Diffstat (limited to 'lib/doc')
-rw-r--r-- | lib/doc/jao-counsel-recoll.el | 60 | ||||
-rw-r--r-- | lib/doc/jao-doc-view.el | 153 | ||||
-rw-r--r-- | lib/doc/jao-recoll.el | 82 |
3 files changed, 295 insertions, 0 deletions
diff --git a/lib/doc/jao-counsel-recoll.el b/lib/doc/jao-counsel-recoll.el new file mode 100644 index 0000000..adae881 --- /dev/null +++ b/lib/doc/jao-counsel-recoll.el @@ -0,0 +1,60 @@ +;;; jao-counsel-recoll.el --- counsel and recoll -*- lexical-binding: t; -*- + +;; Copyright (C) 2020 jao + +;; Author: jao <mail@jao.io> +;; Keywords: docs + +;; 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 of the License, 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 this program. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;; Helpers for using recoll with counsel + +;;; Code: + +(require 'jao-recoll) +(require 'counsel) +(require 'ivy) + +(defvar jao-counsel-recoll--history nil) +(defun jao-counsel-recoll--function (str) + (let ((xs (counsel-recoll-function str))) + (cl-remove-if-not (lambda (x) (string-prefix-p "file://" x)) xs))) + +;;;###autoload +(defun jao-counsel-recoll (&optional initial-input) + (interactive) + (counsel-require-program "recoll") + (ivy-read "recoll: " 'jao-counsel-recoll--function + :initial-input initial-input + :dynamic-collection t + :history 'jao-counsel-recoll--history + :action (lambda (x) + (when (string-match "file://\\(.*\\)\\'" x) + (let ((file-name (match-string 1 x))) + (if (string-match "pdf$" x) + (jao-open-doc file-name) + (find-file file-name))))) + :unwind #'counsel-delete-process + :caller 'jao-counsel-recoll)) + +(defun jao-counsel-recoll--recoll (_s) (jao-recoll ivy-text)) + +(ivy-set-actions 'jao-counsel-recoll + '(("x" jao-counsel-recoll--recoll "List in buffer"))) + + +(provide 'jao-counsel-recoll) +;;; jao-counsel-recoll.el ends here diff --git a/lib/doc/jao-doc-view.el b/lib/doc/jao-doc-view.el new file mode 100644 index 0000000..5060452 --- /dev/null +++ b/lib/doc/jao-doc-view.el @@ -0,0 +1,153 @@ +;; jao-doc-view.el -- Remembering visited documents + +;; Copyright (c) 2013, 2015, 2017, 2018, 2019 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: Fri Feb 15, 2013 01:21 + +;;; Comentary: + +;; Some utilities to keep track of visited documents and the last +;; visited page. + +;;; Code: + +(defvar jao-doc-view-bmk-file "~/.emacs.d/doc-view-bmk") +(defvar jao-doc-view-session-file "~/.emacs.d/doc-view-session") +(defvar jao-doc-view--current-bmks nil) + +(defun jao-doc-view--read-file (file) + (let ((buff (find-file-noselect file))) + (ignore-errors + (with-current-buffer buff + (goto-char (point-min))) + (read buff)))) + +(defun jao-doc-view--save-to-file (file value) + (with-current-buffer (find-file-noselect file) + (erase-buffer) + (insert (format "%S" value)) + (save-buffer))) + +(defun jao-doc-view--read-bmks () + (let ((bmks (jao-doc-view--read-file jao-doc-view-bmk-file))) + (if (hash-table-p bmks) bmks (make-hash-table :test 'equal)))) + +(defun jao-doc-view--current-bmks () + (or jao-doc-view--current-bmks + (setq jao-doc-view--current-bmks (jao-doc-view--read-bmks)))) + +(defun jao-doc-view-purge-bmks () + (interactive) + (when jao-doc-view--current-bmks + (maphash (lambda (k v) + (when (or (not k) (= 1 v) (not (file-exists-p k))) + (remhash k jao-doc-view--current-bmks))) + jao-doc-view--current-bmks))) + +(defun jao-doc-view-goto-bmk () + (interactive) + (when (eq major-mode 'pdf-view-mode) + (let* ((bmks (jao-doc-view--current-bmks)) + (fname (buffer-file-name)) + (p (when fname (gethash (expand-file-name fname) bmks 1)))) + (when (and (numberp p) (> p 1)) + (message "Found bookmark at page %d" p) + (ignore-errors (pdf-view-goto-page p)))))) + +(defun jao-doc-view-open (file) + (let* ((buffs (buffer-list)) + (b (catch 'done + (while buffs + (when (string-equal (buffer-file-name (car buffs)) file) + (throw 'done (car buffs))) + (setq buffs (cdr buffs)))))) + (if b + (pop-to-buffer b) + (when (file-exists-p file) (find-file file))))) + +(defun jao-doc-view-session (&optional file) + (let ((file (or file jao-doc-view-session-file))) + (jao-doc-view--read-file file))) + +(defun jao-doc-view-load-session (&optional file) + (interactive) + (let ((docs (jao-doc-view-session file))) + (when (not (listp docs)) (error "Empty session")) + (dolist (d docs) (other-window 1) (jao-doc-view-open d)))) + +(defun jao-doc-view--save-bmks () + (jao-doc-view-purge-bmks) + (jao-doc-view--save-to-file jao-doc-view-bmk-file + (jao-doc-view--current-bmks))) + +(defun jao-doc-view--save-bmk (&rest ignored) + (when (eq major-mode 'pdf-view-mode) + (ignore-errors + (puthash (buffer-file-name) + (max (pdf-view-current-page) 1) + (jao-doc-view--current-bmks))))) + +(defun jao-doc-view-save-session (&optional skip-current) + (interactive) + (let ((docs '()) + (cb (when skip-current (current-buffer)))) + (dolist (b (buffer-list)) + (with-current-buffer b + (when (and (equalp major-mode 'pdf-view-mode) + (not (equalp cb b))) + (jao-doc-view--save-bmk) + (add-to-list 'docs (buffer-file-name))))) + (jao-doc-view--save-bmks) + (when (> (length docs) 0) + (jao-doc-view--save-to-file jao-doc-view-session-file docs)))) + +(defun jao-doc-view--save-session-1 () + (when (equalp major-mode 'pdf-view-mode) + (jao-doc-view-purge-bmks) + (jao-doc-view-save-session t))) + +(defvar jao-doc-session-timer nil) +(defvar jao-doc-session-timer-seconds 60) + +(defun jao-doc-view-stop-session-timer () + (interactive) + (when jao-doc-session-timer + (cancel-timer jao-doc-session-timer) + (setq jao-doc-session-timer nil))) + +(defun jao-doc-view--save-session () + (let ((inhibit-message t) + (message-log-max nil)) + (jao-doc-view-save-session))) + +(defun jao-doc-view-start-session-timer () + (interactive) + (setq jao-doc-session-timer + (run-with-idle-timer jao-doc-session-timer-seconds + t + 'jao-doc-view--save-session))) + +(defun jao-doc-view-install () + (jao-doc-view--current-bmks) + (add-hook 'kill-buffer-hook 'jao-doc-view--save-bmk) + (add-hook 'kill-buffer-hook 'jao-doc-view--save-session-1 t) + (add-hook 'kill-emacs-hook 'jao-doc-view-save-session) + (jao-doc-view-start-session-timer)) + + + +(provide 'jao-doc-view) diff --git a/lib/doc/jao-recoll.el b/lib/doc/jao-recoll.el new file mode 100644 index 0000000..28a1c1a --- /dev/null +++ b/lib/doc/jao-recoll.el @@ -0,0 +1,82 @@ +;; jao-recoll.el -- Displaying recoll queries + +;; Copyright (c) 2017, 2020 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" + (read-only-mode 1)) + +(defvar jao-recoll--file-regexp + "\\(\\w+/\\w+\\)\t+\\[\\([^]]+\\)\\]\t+\\[\\([^]]+\\)\\].+") + +(defvar jao-recoll-flags "-A") + +;;;###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)))) + (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) + (replace-match "* [[\\2][\\3]] (\\1)") + (forward-line) + (beginning-of-line) + (let ((kill-whole-line nil)) (kill-line)) + (forward-line) + (let ((p (point))) + (re-search-forward "/ABSTRACT") + (beginning-of-line) + (fill-region p (point)) + (let ((kill-whole-line nil)) (kill-line)))) + (recoll-mode) + (pop-to-buffer (current-buffer)) + (goto-char (point-min)) + (org-cycle '(4)) + (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 |