|
57 | 57 | (defvar ivy-file-preview--selected-file "" |
58 | 58 | "Record down the current selected file.") |
59 | 59 |
|
| 60 | +(defvar ivy-file-preview--window-status '() |
| 61 | + "Record windows status for when canceling command.") |
| 62 | + |
60 | 63 | ;;; Util |
61 | 64 |
|
62 | 65 | (defun ivy-file-preview--project-path () |
@@ -102,24 +105,42 @@ PROJECT-DIR represents the path of the project root directory." |
102 | 105 | cl (string-to-number cl)) |
103 | 106 | (ivy-file-preview--do-preview fn ln cl project-dir)))) |
104 | 107 |
|
| 108 | +(defun ivy-file-preview--cancel-revert () |
| 109 | + "Revert frame status if user cancel the commands." |
| 110 | + (unless ivy-exit |
| 111 | + (switch-to-buffer (pop ivy-file-preview--window-status)) |
| 112 | + (ivy-file-preview--goto-line (pop ivy-file-preview--window-status)) |
| 113 | + (move-to-column (pop ivy-file-preview--window-status)))) |
| 114 | + |
| 115 | +(defun ivy-file-preview--enter () |
| 116 | + "Execution after minibuffer setup." |
| 117 | + (setq ivy-file-preview--window-status '()) |
| 118 | + (with-selected-window minibuffer-scroll-window |
| 119 | + (push (current-column) ivy-file-preview--window-status) |
| 120 | + (push (line-number-at-pos) ivy-file-preview--window-status) |
| 121 | + (push (buffer-name) ivy-file-preview--window-status))) |
| 122 | + |
105 | 123 | (defun ivy-file-preview--exit () |
106 | 124 | "Execution before minibuffer exits." |
107 | 125 | (delete-dups ivy-file-preview--preview-files) |
108 | 126 | (dolist (fn ivy-file-preview--preview-files) |
109 | 127 | (unless (string= ivy-file-preview--selected-file fn) |
110 | 128 | (kill-buffer (f-filename fn)))) |
| 129 | + (ivy-file-preview--cancel-revert) ; If already empty, revert it. |
111 | 130 | (setq ivy-file-preview--selected-file "") |
112 | 131 | (setq ivy-file-preview--preview-files '())) |
113 | 132 |
|
114 | 133 | ;;; Entry |
115 | 134 |
|
116 | 135 | (defun ivy-file-preview--enable () |
117 | 136 | "Enable `ivy-file-preview'." |
| 137 | + (add-hook 'minibuffer-setup-hook #'ivy-file-preview--enter) |
118 | 138 | (add-hook 'minibuffer-exit-hook #'ivy-file-preview--exit) |
119 | 139 | (advice-add 'ivy--exhibit :after #'ivy-file-preview--after-select)) |
120 | 140 |
|
121 | 141 | (defun ivy-file-preview--disable () |
122 | 142 | "Disable `ivy-file-preview'." |
| 143 | + (remove-hook 'minibuffer-setup-hook #'ivy-file-preview--enter) |
123 | 144 | (remove-hook 'minibuffer-exit-hook #'ivy-file-preview--exit) |
124 | 145 | (advice-remove 'ivy--exhibit #'ivy-file-preview--after-select)) |
125 | 146 |
|
|
0 commit comments