8484(defvar ivy-file-preview--overlay-timer nil
8585 " Timer to make overlays in buffer." )
8686
87+ (defvar ivy-file-preview--current-dir " "
88+ " Record the current present directory.
89+ It could either by `project-directory' or `default-directory' " )
90+
8791; ;; Util
8892
8993(defun ivy-file-preview--project-path ()
9094 " Get current project path."
9195 (cdr (project-current )))
9296
97+ (defun ivy-file-preview--get-file-path (fn )
98+ " Resolve FN for full file path."
99+ (let ((project-dir (ivy-file-preview--project-path)))
100+ (when (string-match-p " /" fn) (setq ivy-file-preview--current-dir project-dir))
101+ (cond ((and (string-match-p " /" fn) project-dir)
102+ (setq fn (f-join project-dir fn)))
103+ (t (setq fn (f-join ivy-file-preview--current-dir fn)))))
104+ fn)
105+
93106(defun ivy-file-preview--goto-line (ln )
94107 " Goto LN line number."
95108 (goto-char (point-min ))
109122 " Make a new overlay with BEG, END and face (FC).
110123If CURRENT-OV is non-nil it create overlay that are currently selected."
111124 (let ((ol (make-overlay beg end)))
112- (overlay-put ol 'face (if ( = beg ( point )) 'ivy-current-match
125+ (overlay-put ol 'face (if current-ov 'ivy-current-match
113126 'ivy-minibuffer-match-highlight ))
114127 (overlay-put ol 'priority (if current-ov 100 0 ))
115128 (push ol ivy-file-preview--overlays) ; NOTE: Eventually get managed bt list.
@@ -145,10 +158,7 @@ If CURRENT-OV is non-nil it create overlay that are currently selected."
145158
146159(defun ivy-file-preview--extract-candidates-overlay-data ()
147160 " Extract the overlay data from current ivy candidates."
148- (let* ((project-dir (ivy-file-preview--project-path))
149- (fn (if project-dir
150- (s-replace project-dir " " ivy-file-preview--selected-file)
151- ivy-file-preview--selected-file))
161+ (let* ((fn (s-replace ivy-file-preview--current-dir " " ivy-file-preview--selected-file))
152162 (cands (ivy-file-preview--candidates))
153163 (cands-len (length cands)) current-cand entered ln-data
154164 ln col
@@ -186,9 +196,9 @@ If CURRENT-OV is non-nil it create overlay that are currently selected."
186196 delta-ln (- ln current-ln)
187197 pos (ivy-file-preview--convert-pos-delta delta-ln col)))
188198 (setq current-ov-p (= pos (point )))
189- (if current-ov-p
190- (ivy-file-preview--make- current-overlay pos len)
191- (ivy-file-preview--make-overlay pos ( + pos len) )))))
199+ (ivy-file-preview--make-overlay pos ( + pos len))
200+ ( when current-ov-p
201+ (ivy-file-preview--make-current- overlay pos len)))))
192202
193203(defun ivy-file-preview--delay-make-overlays ()
194204 " Seconds to delay display overlays."
@@ -204,7 +214,8 @@ If POS is nil then it won't moves."
204214 ((not ivy-file-preview-details) (setq is-fild-p nil ))
205215 ((find-buffer-visiting fn) (switch-to-buffer fn))
206216 (t (setq is-fild-p nil )))
207- (when is-fild-p
217+ (if (not is-fild-p)
218+ (setq ivy-file-preview--selected-file " " )
208219 (setq ivy-file-preview--selected-file fn)
209220 (cond ((consp pos)
210221 (ivy-file-preview--goto-line (car pos))
@@ -249,8 +260,7 @@ FN is the file path. POS can either be one of the following type:
249260 (progn
250261 (ivy-file-preview--delete-overlays)
251262 (ivy-file-preview--back-to-pos))
252- (let* ((project-dir (ivy-file-preview--project-path))
253- (cands (ivy-file-preview--candidates))
263+ (let* ((cands (ivy-file-preview--candidates))
254264 (current-selection (or (nth ivy--index cands) " " ))
255265 (sel-lst (split-string current-selection " :" ))
256266 (fn (nth 0 sel-lst)) (ln (nth 1 sel-lst)) (cl (nth 2 sel-lst))
@@ -259,9 +269,7 @@ FN is the file path. POS can either be one of the following type:
259269 (when can-preview-p
260270 (setq ln (ignore-errors (cl-parse-integer ln))
261271 cl (ignore-errors (cl-parse-integer cl)))
262- (cond ((and (string-match-p " /" fn) project-dir)
263- (setq fn (f-join project-dir fn)))
264- (t (setq fn (f-join default-directory fn))))
272+ (setq fn (ivy-file-preview--get-file-path fn))
265273 (ivy-file-preview--do-preview fn (if cl (cons ln cl) ln))))))
266274
267275(defun ivy-file-preview--back-to-pos ()
@@ -280,6 +288,7 @@ FN is the file path. POS can either be one of the following type:
280288(defun ivy-file-preview--enter ()
281289 " Execution after minibuffer setup."
282290 (setq ivy-file-preview--window-status '())
291+ (setq ivy-file-preview--current-dir default-directory)
283292 (with-selected-window minibuffer-scroll-window
284293 (ivy-file-preview--put-window-plist :file (current-buffer ))
285294 (ivy-file-preview--put-window-plist :window-point (window-point ))
@@ -295,6 +304,7 @@ FN is the file path. POS can either be one of the following type:
295304 (ignore-errors (kill-buffer (f-filename fn)))))
296305 (setq ivy-file-preview--selected-file " " )
297306 (setq ivy-file-preview--ivy-text " " )
307+ (setq ivy-file-preview--current-dir " " )
298308 (setq ivy-file-preview--preview-files '()))
299309
300310; ;; Entry
0 commit comments