Skip to content

Commit 4834cfa

Browse files
committed
Update core.
1 parent 6e509af commit 4834cfa

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

ivy-file-preview.el

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
;; Description: Preview the current ivy file selection.
88
;; Keyword: file ivy swiper preview select selection
99
;; 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"))
1111
;; URL: https://github.com/jcs090218/ivy-file-preview
1212

1313
;; This file is NOT part of GNU Emacs.
@@ -32,6 +32,7 @@
3232

3333
;;; Code:
3434

35+
(require 'f)
3536
(require 'ivy)
3637

3738
(defgroup ivy-file-preview nil
@@ -40,13 +41,87 @@
4041
:group 'tool
4142
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/ivy-file-preview"))
4243

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+
43116
(defun ivy-file-preview--enable ()
44117
"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))
46120

47121
(defun ivy-file-preview--disable ()
48122
"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))
50125

51126
;;;###autoload
52127
(define-minor-mode ivy-file-preview-mode

0 commit comments

Comments
 (0)