|
7 | 7 | ;; Description: Preview the current ivy file selection. |
8 | 8 | ;; Keyword: file ivy swiper preview select selection |
9 | 9 | ;; Version: 0.0.1 |
10 | | -;; Package-Requires: ((emacs "24.3") (ivy "0.8.0")) |
| 10 | +;; Package-Requires: ((emacs "25.1") (ivy "0.8.0") (f "0.20.0")) |
11 | 11 | ;; URL: https://github.com/jcs090218/ivy-file-preview |
12 | 12 |
|
13 | 13 | ;; This file is NOT part of GNU Emacs. |
|
32 | 32 |
|
33 | 33 | ;;; Code: |
34 | 34 |
|
| 35 | +(require 'f) |
35 | 36 | (require 'ivy) |
36 | 37 |
|
37 | 38 | (defgroup ivy-file-preview nil |
|
40 | 41 | :group 'tool |
41 | 42 | :link '(url-link :tag "Repository" "https://github.com/jcs-elpa/ivy-file-preview")) |
42 | 43 |
|
| 44 | +(defcustom ivy-file-preview-preview-only t |
| 45 | + "Preview the file instead of actually opens the file." |
| 46 | + :type 'boolean |
| 47 | + :group 'ivy-file-preview) |
| 48 | + |
| 49 | +(defcustom ivy-file-preview-details t |
| 50 | + "Preview file only when there are more details in the selection." |
| 51 | + :type 'boolean |
| 52 | + :group 'ivy-file-preview) |
| 53 | + |
| 54 | +(defvar ivy-file-preview--preview-files '() |
| 55 | + "Files that are previewing, and will be closed after action is done.") |
| 56 | + |
| 57 | +(defvar ivy-file-preview--selected-file "" |
| 58 | + "Record down the current selected file.") |
| 59 | + |
| 60 | +;;; Util |
| 61 | + |
| 62 | +(defun ivy-file-preview--project-path () |
| 63 | + "Get current project path." |
| 64 | + (cdr (project-current))) |
| 65 | + |
| 66 | +(defun ivy-file-preview--goto-line (ln) |
| 67 | + "Goto LN line number." |
| 68 | + (goto-char (point-min)) |
| 69 | + (forward-line (1- ln))) |
| 70 | + |
| 71 | +;;; Core |
| 72 | + |
| 73 | +(defun ivy-file-preview--open-file (fn ln cl) |
| 74 | + "Open the file (FN), line number (LN), and column (CL)." |
| 75 | + (setq ivy-file-preview--selected-file fn) |
| 76 | + (find-file fn) |
| 77 | + (ivy-file-preview--goto-line ln) |
| 78 | + (move-to-column cl)) |
| 79 | + |
| 80 | +(defun ivy-file-preview--do-preview (fn ln cl project-dir) |
| 81 | + "Do file preview execution. |
| 82 | +FN represents file path. LN represents line numbers. CL represents columns. |
| 83 | +PROJECT-DIR represents the path of the project root directory." |
| 84 | + (save-selected-window |
| 85 | + (with-selected-window minibuffer-scroll-window |
| 86 | + (when project-dir (setq fn (f-join project-dir fn))) |
| 87 | + (when (and ivy-file-preview-preview-only (not (find-buffer-visiting fn))) |
| 88 | + (push fn ivy-file-preview--preview-files)) |
| 89 | + (ivy-file-preview--open-file fn ln cl)))) |
| 90 | + |
| 91 | +(defun ivy-file-preview--after-select (&rest _) |
| 92 | + "Execution after selection." |
| 93 | + (let* ((project-dir (ivy-file-preview--project-path)) |
| 94 | + (cands (or ivy--old-cands ivy--all-candidates '())) |
| 95 | + (current-selection (or (nth ivy--index cands) "")) |
| 96 | + (sel-lst (split-string current-selection ":")) |
| 97 | + fn ln cl) |
| 98 | + (when (< 2 (length sel-lst)) |
| 99 | + (setq fn (nth 0 sel-lst) ln (nth 1 sel-lst) cl (nth 2 sel-lst))) |
| 100 | + (when (and ivy-file-preview-details ln cl) |
| 101 | + (setq ln (string-to-number ln) |
| 102 | + cl (string-to-number cl)) |
| 103 | + (ivy-file-preview--do-preview fn ln cl project-dir)))) |
| 104 | + |
| 105 | +(defun ivy-file-preview--exit () |
| 106 | + "Execution before minibuffer exits." |
| 107 | + (delete-dups ivy-file-preview--preview-files) |
| 108 | + (dolist (fn ivy-file-preview--preview-files) |
| 109 | + (unless (string= ivy-file-preview--selected-file fn) |
| 110 | + (kill-buffer (f-filename fn)))) |
| 111 | + (setq ivy-file-preview--selected-file "") |
| 112 | + (setq ivy-file-preview--preview-files '())) |
| 113 | + |
| 114 | +;;; Entry |
| 115 | + |
43 | 116 | (defun ivy-file-preview--enable () |
44 | 117 | "Enable `ivy-file-preview'." |
45 | | - ) |
| 118 | + (add-hook 'minibuffer-exit-hook #'ivy-file-preview--exit) |
| 119 | + (advice-add 'ivy--exhibit :after #'ivy-file-preview--after-select)) |
46 | 120 |
|
47 | 121 | (defun ivy-file-preview--disable () |
48 | 122 | "Disable `ivy-file-preview'." |
49 | | - ) |
| 123 | + (remove-hook 'minibuffer-exit-hook #'ivy-file-preview--exit) |
| 124 | + (advice-remove 'ivy--exhibit #'ivy-file-preview--after-select)) |
50 | 125 |
|
51 | 126 | ;;;###autoload |
52 | 127 | (define-minor-mode ivy-file-preview-mode |
|
0 commit comments