From 0db03a3d92fda3c0cf368826d0fe5a224ad688d0 Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 23 Sep 2022 00:59:24 +0100 Subject: cleanups for pdf navigation --- attic/elisp/misc.el | 85 +++++++++++++++++++++++++++++++++++++++++++++++ custom/jao-custom-exwm.el | 8 +++-- custom/jao-custom-org.el | 2 +- init.el | 18 +++++----- lib/doc/jao-org-links.el | 34 +++++++++---------- lib/doc/jao-org-notes.el | 13 ++++++++ lib/doc/jao-pdf.el | 16 --------- lib/eos/jao-wayland.el | 4 +-- lib/net/jao-notmuch.el | 84 ---------------------------------------------- 9 files changed, 131 insertions(+), 133 deletions(-) diff --git a/attic/elisp/misc.el b/attic/elisp/misc.el index d448ae7..870bb37 100644 --- a/attic/elisp/misc.el +++ b/attic/elisp/misc.el @@ -319,6 +319,91 @@ (interactive) (jao-notify "Volume" (format "%s%%" (jao-player-volume)))) +;;; outline mode for notmuch tree view + +(defun jao-notmuch-tree--msg-prefix (msg) + (insert (propertize (if (plist-get msg :first) "> " " ") 'display " "))) + +(defun jao-notmuch-tree--mode-setup () + (setq-local outline-regexp "^> \\|^En") + (outline-minor-mode t)) + +(defun jao-notmuch-tree-hide-others (&optional and-show) + (interactive) + (outline-hide-body) + (outline-show-entry) + (when and-show (notmuch-tree-show-message nil))) + +(defsubst jao-notmuch-tree--message-open () + (and (buffer-live-p notmuch-tree-message-buffer) + (get-buffer-window notmuch-tree-message-buffer))) + +(defsubst jao-notmuch--get-prop (prop &optional props) + (or (and props (plist-get props prop)) + (notmuch-tree-get-prop prop) + (notmuch-show-get-prop prop))) + +(defun jao-notmuch--looking-at-match-p () + (and (jao-notmuch--get-prop :match) + (equal (jao-notmuch--get-prop :orig-tags) + (jao-notmuch--get-prop :tags)))) + +(defun jao-notmuch-tree--next (prev thread no-exit &optional ignore-new) + (let ((line-move-ignore-invisible nil)) + (cond ((and (not ignore-new) + (jao-notmuch--looking-at-match-p) + (not (jao-notmuch-tree--message-open)))) + (thread + (notmuch-tree-next-thread prev) + (unless (or (not (notmuch-tree-get-message-properties)) + (jao-notmuch--looking-at-match-p)) + (notmuch-tree-matching-message prev (not no-exit)))) + (t (notmuch-tree-matching-message prev (not no-exit))))) + (when (notmuch-tree-get-message-id) + (jao-notmuch-tree-hide-others t)) + (when prev (forward-char 2))) + +(defvar jao-notmuch-tree--prefix-map + (let ((m (make-keymap "Thread operations"))) + (define-key m (kbd "TAB") #'outline-cycle) + (define-key m (kbd "t") #'outline-toggle-children) + (define-key m (kbd "s") #'outline-show-entry) + (define-key m (kbd "S") #'outline-show-all) + (define-key m (kbd "h") #'outline-hide-entry) + (define-key m (kbd "H") #'outline-hide-body) + (define-key m (kbd "o") #'jao-notmuch-tree-hide-others) + (define-key m (kbd "n") #'outline-hide-other) + m)) + +(defun jao-notmuch-tree-outline-setup (&optional prefix) + (define-key notmuch-tree-mode-map (kbd (or prefix "T")) + jao-notmuch-tree--prefix-map) + (define-key notmuch-tree-mode-map (kbd "TAB") #'outline-cycle) + (define-key notmuch-tree-mode-map (kbd "M-TAB") #'outline-cycle-buffer) + (add-hook 'notmuch-tree-mode-hook #'jao-notmuch-tree--mode-setup) + (advice-add 'notmuch-tree-insert-msg :before #'jao-notmuch-tree--msg-prefix)) + +(defun jao-notmuch-tree-next (thread &optional no-exit) + "Next message or thread in forest, taking care of thread visibility." + (interactive "P") + (jao-notmuch-tree--next nil thread no-exit)) + +(defun jao-notmuch-tree-next-thread (&optional exit) + "Next thread in forest, taking care of thread visibility." + (interactive "P") + (jao-notmuch-tree--next nil t exit)) + +(defun jao-notmuch-tree-previous (thread) + "Previous message or thread in forest, taking care of thread visibility." + (interactive "P") + (jao-notmuch-tree--next t thread t)) + +(defun jao-notmuch-tree-previous-thread (&optional exit) + "Previous thread in forest, taking care of thread visibility." + (interactive "P") + (jao-notmuch-tree--next t t exit)) + + ;;; snippets (defun jao-sway-run-or-focus-tidal () (interactive) diff --git a/custom/jao-custom-exwm.el b/custom/jao-custom-exwm.el index cbc82b7..169fb26 100644 --- a/custom/jao-custom-exwm.el +++ b/custom/jao-custom-exwm.el @@ -339,7 +339,7 @@ (defun jao-exwm-zathura-goto-org (&optional arg) (interactive "P") (when-let ((info (jao-zathura--file-info (current-buffer)))) - (when-let ((file (jao-org-pdf-to-org-file (car info)))) + (when-let ((file (jao-org-find-for-pdf (car info)))) (let ((newp (not (file-exists-p file)))) (when (or arg newp) (org-store-link nil t)) (find-file-other-window file) @@ -374,11 +374,15 @@ (setq jao-org-links-pdf-store-fun nil) (setq jao-open-doc-fun #'jao-find-or-open)) +(defun jao-exwm-org-to-pdf-file () + (expand-file-name (concat "doc/" (file-name-base buffer-file-name) ".pdf") + (file-name-directory jao-org-notes-dir))) + (defun jao-exwm-zathura-goto-pdf () (interactive) (if jao-browse-doc-use-emacs-p (jao-org-goto-pdf) - (when-let (pdf (jao-org-org-to-pdf-file)) + (when-let (pdf (jao-exwm-org-to-pdf-file)) (jao-zathura-open-doc pdf)))) (with-eval-after-load "org" diff --git a/custom/jao-custom-org.el b/custom/jao-custom-org.el index b98f665..8c4d88c 100644 --- a/custom/jao-custom-org.el +++ b/custom/jao-custom-org.el @@ -194,7 +194,7 @@ (jao-transient-major-mode org ["Notes" ("o" "find and open note" jao-org-notes-open) - ("c" "open or create note" jao-org-notes-open-or-create) + ("c" "open or create note" jao-org-notes-create) ("\\" "grep notes" jao-org-notes-grep) ("r" "recoll notes" jao-org-notes-recoll)] ["Current note" :if jao-org-notes-note-p diff --git a/init.el b/init.el index 61fa309..52184d9 100644 --- a/init.el +++ b/init.el @@ -1550,7 +1550,7 @@ (let ((title (or title (jao-shell-string "xdotool" "getactivewindow" "getwindowname")))) - (jao-pdf-goto-zathura-org title no-ask))) + (jao-org-open-from-zathura title no-ask))) (defun jao-find-or-open (file &optional page height) (cond ((and jao-browse-doc-use-emacs-p window-system) @@ -1579,6 +1579,14 @@ (jao-buffer-same-mode '(pdf-view-mode doc-view-mode) #'jao-afio-goto-docs)) +(defun jao-open-with-zathura () + (interactive) + (when-let (f buffer-file-name) + (let ((p (jao-doc-view-current-page))) + (cond (jao-river-enabled (jao-river-open-with-zathura f p)) + (jao-sway-enabled (jao-sway-open-with-zathura f p)) + (t (jao-zathura-open f p)))))) + ;; doc:// links for browse-url (defun jao-open-doc-url (url &rest _) (when (string-match "doc://\\([^?]+\\)\\(\\?.*\\)?" url) @@ -1609,14 +1617,6 @@ (interactive) (jao-org-pdf-goto-org 4)) -(defun jao-open-with-zathura () - (interactive) - (when-let (f buffer-file-name) - (let ((p (jao-doc-view-current-page))) - (cond (jao-river-enabled (jao-river-open-with-zathura f p)) - (jao-sway-enabled (jao-sway-open-with-zathura f p)) - (t (jao-zathura-open f p)))))) - (jao-transient-major-mode doc-view ["Notes" ("o" "notes file" jao-org-pdf-goto-org) diff --git a/lib/doc/jao-org-links.el b/lib/doc/jao-org-links.el index acfe176..8b5c42e 100644 --- a/lib/doc/jao-org-links.el +++ b/lib/doc/jao-org-links.el @@ -77,25 +77,21 @@ (insert (format "[[doc:%s][%s]]" (jao-pdf-title-to-file-name title) title))) ;;;###autoload -(defun jao-org-org-to-pdf-file () - (expand-file-name (concat "doc/" (file-name-base buffer-file-name) ".pdf") - (file-name-directory jao-org-notes-dir))) - -;;;###autoload -(defun jao-org-pdf-to-org-file (&optional file-name no-ask) - (let* ((file-name (or file-name buffer-file-name)) - (bn (file-name-base file-name)) - (rx (format "%s\\.org$" (regexp-quote bn)))) - (save-some-buffers nil - (lambda () - (string-prefix-p jao-org-notes-dir buffer-file-name))) - (or (car (directory-files-recursively jao-org-notes-dir rx)) - (let ((d (if no-ask - "" - (concat (completing-read "Notes subdir: " - (jao-org-notes-cats) nil t) - "/")))) - (expand-file-name (concat dir bn ".org") jao-org-notes-dir))))) +(defun jao-org-open-from-zathura (title &optional no-ask) + (when-let* ((info (jao-pdf-zathura-file-info title)) + (pdf-file (car info)) + (page (cadr info)) + (file (jao-org-notes-find-for-pdf pdf-file))) + (ignore-errors (jao-afio-goto-docs)) + (let ((exists (file-exists-p file))) + (find-file file) + (unless exists (jao-org-insert-doc-skeleton)) + (let ((lnk (jao-pdf--zathura-link info))) + (jao-doc-session-mark) + (if (or (not exists) (and (not no-ask) (y-or-n-p "Insert link?"))) + (insert lnk "\n") + (kill-new lnk) + (message "Link to %s (%s) killed" file page)))))) ;;;###autoload (defun jao-org-insert-doc-skeleton (&optional title) diff --git a/lib/doc/jao-org-notes.el b/lib/doc/jao-org-notes.el index 3f3c5ea..b7700ec 100644 --- a/lib/doc/jao-org-notes.el +++ b/lib/doc/jao-org-notes.el @@ -140,6 +140,19 @@ (save-buffer) (buffer-file-name)) +;;;###autoload +(defun jao-org-notes-find-for-pdf (&optional file-name) + "Given a PDF file name, find its org notes counterpart." + (let* ((file-name (or file-name buffer-file-name)) + (bn (file-name-base file-name)) + (rx (format "%s\\.org$" (regexp-quote bn))) + (pred (lambda () (string-prefix-p jao-org-notes-dir buffer-file-name)))) + (save-some-buffers nil pred) + (or (car (directory-files-recursively jao-org-notes-dir rx)) + (let* ((d (completing-read "Notes subdir: " (jao-org-notes-cats) nil t)) + (d (file-name-as-directory d))) + (expand-file-name (concat d bn ".org") jao-org-notes-dir))))) + ;;;###autoload (defun jao-org-notes-grep () "Perform a grep search on all org notes body, via consult-ripgrep." diff --git a/lib/doc/jao-pdf.el b/lib/doc/jao-pdf.el index ad34b48..213d806 100644 --- a/lib/doc/jao-pdf.el +++ b/lib/doc/jao-pdf.el @@ -112,21 +112,5 @@ The result is cached as a local buffer variable." (defun jao-pdf-zathura-org-link (title) (jao-pdf--zathura-link (jao-pdf-zathura-file-info title))) -(defun jao-pdf-goto-zathura-org (title &optional no-ask) - (when-let* ((info (jao-pdf-zathura-file-info title)) - (pdf-file (car info)) - (page (cadr info)) - (file (jao-org-pdf-to-org-file pdf-file))) - (ignore-errors (jao-afio-goto-docs)) - (let ((exists (file-exists-p file))) - (find-file file) - (unless exists (jao-org-insert-doc-skeleton)) - (let ((lnk (jao-pdf--zathura-link info))) - (jao-doc-session-mark) - (if (or (not exists) (and (not no-ask) (y-or-n-p "Insert link?"))) - (insert lnk "\n") - (kill-new lnk) - (message "Link to %s (%s) killed" file page)))))) - (provide 'jao-pdf) ;;; jao-pdf.el ends here diff --git a/lib/eos/jao-wayland.el b/lib/eos/jao-wayland.el index 9f503f8..5a457a4 100644 --- a/lib/eos/jao-wayland.el +++ b/lib/eos/jao-wayland.el @@ -66,7 +66,7 @@ (defun jao-river-zathura-to-org () (let ((title (jao-river-get-focused-title))) (jao-river-to-ws 1) - (jao-pdf-goto-zathura-org title t))) + (jao-org-open-from-zathura title t))) (defun jao-river-zathura-kill-link () (when-let* ((title (jao-river-get-focused-title)) @@ -129,7 +129,7 @@ (and (string-match "\"\\(.+\\)\"" tl) (match-string 1 tl)))) (defun jao-sway-zathura-org () - (jao-pdf-goto-zathura-org (jao-sway-get-active-title) t)) + (jao-org-open-from-zathura (jao-sway-get-active-title) t)) (defun jao-sway-open-with-zathura (file page) (let* ((n (file-name-nondirectory file)) diff --git a/lib/net/jao-notmuch.el b/lib/net/jao-notmuch.el index d7e27b6..6dcb73e 100644 --- a/lib/net/jao-notmuch.el +++ b/lib/net/jao-notmuch.el @@ -270,90 +270,6 @@ (concat " " subject))) -;;; outline mode for tree view - -(defun jao-notmuch-tree--msg-prefix (msg) - (insert (propertize (if (plist-get msg :first) "> " " ") 'display " "))) - -(defun jao-notmuch-tree--mode-setup () - (setq-local outline-regexp "^> \\|^En") - (outline-minor-mode t)) - -(defun jao-notmuch-tree-hide-others (&optional and-show) - (interactive) - (outline-hide-body) - (outline-show-entry) - (when and-show (notmuch-tree-show-message nil))) - -(defsubst jao-notmuch-tree--message-open () - (and (buffer-live-p notmuch-tree-message-buffer) - (get-buffer-window notmuch-tree-message-buffer))) - -(defsubst jao-notmuch--get-prop (prop &optional props) - (or (and props (plist-get props prop)) - (notmuch-tree-get-prop prop) - (notmuch-show-get-prop prop))) - -(defun jao-notmuch--looking-at-match-p () - (and (jao-notmuch--get-prop :match) - (equal (jao-notmuch--get-prop :orig-tags) - (jao-notmuch--get-prop :tags)))) - -(defun jao-notmuch-tree--next (prev thread no-exit &optional ignore-new) - (let ((line-move-ignore-invisible nil)) - (cond ((and (not ignore-new) - (jao-notmuch--looking-at-match-p) - (not (jao-notmuch-tree--message-open)))) - (thread - (notmuch-tree-next-thread prev) - (unless (or (not (notmuch-tree-get-message-properties)) - (jao-notmuch--looking-at-match-p)) - (notmuch-tree-matching-message prev (not no-exit)))) - (t (notmuch-tree-matching-message prev (not no-exit))))) - (when (notmuch-tree-get-message-id) - (jao-notmuch-tree-hide-others t)) - (when prev (forward-char 2))) - -(defvar jao-notmuch-tree--prefix-map - (let ((m (make-keymap "Thread operations"))) - (define-key m (kbd "TAB") #'outline-cycle) - (define-key m (kbd "t") #'outline-toggle-children) - (define-key m (kbd "s") #'outline-show-entry) - (define-key m (kbd "S") #'outline-show-all) - (define-key m (kbd "h") #'outline-hide-entry) - (define-key m (kbd "H") #'outline-hide-body) - (define-key m (kbd "o") #'jao-notmuch-tree-hide-others) - (define-key m (kbd "n") #'outline-hide-other) - m)) - -(defun jao-notmuch-tree-outline-setup (&optional prefix) - (define-key notmuch-tree-mode-map (kbd (or prefix "T")) - jao-notmuch-tree--prefix-map) - (define-key notmuch-tree-mode-map (kbd "TAB") #'outline-cycle) - (define-key notmuch-tree-mode-map (kbd "M-TAB") #'outline-cycle-buffer) - (add-hook 'notmuch-tree-mode-hook #'jao-notmuch-tree--mode-setup) - (advice-add 'notmuch-tree-insert-msg :before #'jao-notmuch-tree--msg-prefix)) - -(defun jao-notmuch-tree-next (thread &optional no-exit) - "Next message or thread in forest, taking care of thread visibility." - (interactive "P") - (jao-notmuch-tree--next nil thread no-exit)) - -(defun jao-notmuch-tree-next-thread (&optional exit) - "Next thread in forest, taking care of thread visibility." - (interactive "P") - (jao-notmuch-tree--next nil t exit)) - -(defun jao-notmuch-tree-previous (thread) - "Previous message or thread in forest, taking care of thread visibility." - (interactive "P") - (jao-notmuch-tree--next t thread t)) - -(defun jao-notmuch-tree-previous-thread (&optional exit) - "Previous thread in forest, taking care of thread visibility." - (interactive "P") - (jao-notmuch-tree--next t t exit)) - ;;; tree setup overall function (defun jao-notmuch-tree--sentinel (proc) -- cgit v1.2.3