-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
helm-file-preview.el
176 lines (141 loc) · 6.48 KB
/
helm-file-preview.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; helm-file-preview.el --- Preview the current helm file selection -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2024 Shen, Jen-Chieh
;; Created date 2019-06-18 14:03:53
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/helm-file-preview
;; Version: 0.1.5
;; Package-Requires: ((emacs "25.1") (helm "2.0"))
;; Keywords: convenience file helm preview select selection
;; This file is NOT part of GNU Emacs.
;; 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:
;;
;; Preview the current helm file selection.
;;
;;; Code:
(require 'project)
(require 'helm)
(defgroup helm-file-preview nil
"Preview the current helm file selection."
:prefix "helm-file-preview-"
:group 'tools
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/helm-file-preview"))
(defcustom helm-file-preview-only-when-line-numbers t
"Find the file only when the line numbers appears in the selection."
:type 'boolean
:group 'helm-file-preview)
(defcustom helm-file-preview-preview-only t
"Preview the file instead of actually opens the file."
:type 'boolean
:group 'helm-file-preview)
(defvar helm-file-preview--prev-window nil
"Record down the previous window before we do `helm-' related commands.")
(defvar helm-file-preview--file-buffer-list '()
"List of file buffer that are previewing, and ready to be killed again.")
(defvar helm-file-preview--current-select-fb nil
"Record current selecting file buffer.")
(defvar helm-file-preview--prev-buffer-list '()
"Record the previous buffer list.")
(defvar helm-file-preview--exiting t
"Exit flag for this minor mode.")
;;; Externals
(declare-function project-root "project" (project))
;;; Core
(defun helm-file-preview--do-preview (fp ln cl)
"Do preview with filepath (FP), line number (LN), column (CL)."
(let (did-find-file)
(save-selected-window
(when (or (not helm-file-preview-only-when-line-numbers)
(and helm-file-preview-only-when-line-numbers ln))
(select-window helm-file-preview--prev-window)
(find-file fp)
(setq did-find-file t)
(when helm-file-preview-preview-only
(setq helm-file-preview--current-select-fb (current-buffer))
(push helm-file-preview--current-select-fb helm-file-preview--file-buffer-list)
(delete-dups helm-file-preview--file-buffer-list)))
(when did-find-file
(let (ln-num cl-num)
(when ln
(setq ln-num (string-to-number ln))
(when (< 0 ln-num)
(goto-char (point-min))
(forward-line (1- ln-num))
(when cl
(setq cl-num (string-to-number cl))
(when (< 0 cl-num)
(move-to-column (1- cl-num)))))))))))
(defun helm-file-preview--helm-move-selection-after-hook (&rest _args)
"Helm after move selection for `helm-' related commands preview action.
ARGS : rest of the arguments."
(let ((selection-str (helm-get-selection nil t)))
(when (and helm-file-preview--prev-window
selection-str)
(let* ((sel-lst (split-string selection-str ":"))
(fn (nth 0 sel-lst)) ; filename
(ln (nth 1 sel-lst)) ; line
(cl (nth 2 sel-lst)) ; column
(root (if (fboundp #'project-root)
(ignore-errors (project-root (project-current)))
(cdr (project-current))))
(fp (concat root fn)) ; file path
)
;; NOTE: Try expand file, if the file not found relative to
;; project directory.
(unless (file-exists-p fp) (setq fp (expand-file-name fn)))
(when (file-exists-p fp)
(helm-file-preview--do-preview fp ln cl))))))
(defun helm-file-preview--opened-buffer (in-list in-buf)
"Check if the IN-BUF in the opened buffer list, IN-LIST."
(cl-some #'(lambda (buf) (equal buf in-buf)) in-list))
(defun helm-file-preview--helm-before-initialize-hook ()
"Record all necessary info for `helm-file-preview' package to work."
(setq helm-file-preview--prev-window (selected-window))
(setq helm-file-preview--file-buffer-list '())
(setq helm-file-preview--current-select-fb nil)
(setq helm-file-preview--prev-buffer-list (buffer-list))
(setq helm-file-preview--exiting nil))
(defun helm-file-preview--cleanup ()
"Clean up and kill preview files."
(when (and helm-file-preview-preview-only
(not helm-file-preview--exiting))
(dolist (fb helm-file-preview--file-buffer-list)
(when (and (not (equal helm-file-preview--current-select-fb fb))
(not (helm-file-preview--opened-buffer helm-file-preview--prev-buffer-list fb)))
(kill-buffer fb)))
(setq helm-file-preview--exiting t)))
(defun helm-file-preview--exit ()
"When exit this minor mode."
(setq helm-file-preview--current-select-fb nil)
(helm-file-preview--cleanup))
;;; Entry
(defun helm-file-preview--enable ()
"Enable `helm-file-preview'."
(add-hook 'helm-before-initialize-hook #'helm-file-preview--helm-before-initialize-hook)
(add-hook 'helm-cleanup-hook #'helm-file-preview--cleanup)
(add-hook 'minibuffer-exit-hook #'helm-file-preview--exit)
(advice-add 'helm-mark-current-line :after 'helm-file-preview--helm-move-selection-after-hook))
(defun helm-file-preview--disable ()
"Disable `helm-file-preview'."
(remove-hook 'helm-before-initialize-hook #'helm-file-preview--helm-before-initialize-hook)
(remove-hook 'helm-cleanup-hook #'helm-file-preview--cleanup)
(remove-hook 'minibuffer-exit-hook #'helm-file-preview--exit)
(advice-remove 'helm-mark-current-line 'helm-file-preview--helm-move-selection-after-hook))
;;;###autoload
(define-minor-mode helm-file-preview-mode
"Minor mode `helm-file-preview-mode'."
:global t
:require 'helm-file-preview
:group 'helm-file-preview
(if helm-file-preview-mode (helm-file-preview--enable) (helm-file-preview--disable)))
(provide 'helm-file-preview)
;;; helm-file-preview.el ends here