-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvim-empty-lines-mode.el
204 lines (165 loc) · 7.28 KB
/
vim-empty-lines-mode.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
;;; vim-empty-lines-mode.el --- Vim-like empty line indicator at end of files.
;; Copyright (C) 2015 Jonne Mickelin
;; Author: Jonne Mickelin <jonne@ljhms.com>
;; Created: 06 Jan 2015
;; Version: 0.1
;; Keywords: emulations
;; URL: https://github.com/jmickelin/vim-empty-lines-mode
;; Package-Requires: ((emacs "23"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This mode emulates the way that vim indicates the end of a file,
;; that is by putting a "tilde" character (~) on any empty line that
;; follows the end of file.
;; Emacs provides a similar functionality that inserts a bitmap in the
;; fringe on the extraneous lines. By customizing
;; `indicate-empty-lines' and `fringe-indicator-alist' it is possible
;; to nearly emulate the vim behaviour.
;; However, there is a slight difference in what the two editors
;; consider to be "empty lines".
;; A line in vim can be considered to contain the newline character
;; that ended the previous line, as well as the other visible
;; characters. Equivalently, a line is empty only if it contains no
;; text and the previous line has no trailing whitespace.
;; Example:
;; foo <- not empty
;; \nbar <- not empty
;; \n <- not empty
;; ~ <- empty
;; ~ <- empty
;;
;; foo <- not empty
;; \nbar <- not empty
;; ~ <- empty
;; ~ <- empty
;; A line in emacs, on the other hand, will contain the newline
;; character that breaks it. Thus a line is empty even if the previous
;; line has a trailing linebreak.
;; Example:
;; foo\n <- not empty
;; bar\n <- not empty
;; ~ <- empty
;; ~ <- empty
;; ;
;; foo\n <- not empty
;; bar <- not empty
;; ~ <- empty
;; ~ <- empty
;; Note that Emacs displays the two cases identically!
;; There is currently (as of Emacs 24.4) no way to implement the
;; vim-like behaviour for `indicate-empty-lines' short of modifying
;; the Emacs core.
;; This module emulates the vim-like behaviour using a different
;; approach, namely by inserting at the end of the buffer a read-only
;; overlay containing the indicators for the empty lines. This has the
;; added advantage that it's trivial to customize the indicator to an
;; arbitrary string, and customize its text properties.
;; To enable `vim-empty-lines-mode' in a buffer, run
;; (vim-empty-lines-mode)
;; To enable it globally, run
;; (global-vim-empty-lines-mode)
;; The string that indicates an empty line can be customized, e.g.
;; (setq vim-empty-lines-indicator "**********************")
;; The face that is used to display the indicators is `vim-empty-lines-face'.
;;; Code:
(defgroup vim-empty-lines-mode
nil
"Vim-like empty line indicators."
:group 'emulations
:prefix "vim-empty-lines")
(defface vim-empty-lines-face
'((t (:inherit font-lock-comment-face)))
"Face for empty lines in `vim-empty-lines-mode'."
:group 'vim-empty-lines-mode)
(defcustom vim-empty-lines-indicator "~"
"String to display on lines following end-of-buffer in `vim-empty-lines-mode'.
Must not contain '\\n'."
:group 'vim-empty-lines-mode)
(defvar vim-empty-lines-overlay nil
"Overlay that displays the empty line indicators.")
(defun vim-empty-lines-create-overlay ()
(setq vim-empty-lines-overlay (make-overlay (point-max)
(point-max)
nil
t t))
(overlay-put vim-empty-lines-overlay 'window t))
(defun vim-empty-lines-nlines-after-buffer-end (window)
(- (window-height window)
(- (line-number-at-pos (point-max))
(line-number-at-pos (window-start window)))))
(defun vim-empty-lines-update-overlay (&optional window _window-start)
(let ((w (or window
(let ((w (get-buffer-window)))
(and (window-valid-p w) w)))))
;; `w' could be nil but it's ok for `window-height', `window-start' etc.
(with-current-buffer (window-buffer w)
(when (overlayp vim-empty-lines-overlay)
(vim-empty-lines-update-overlay-aux
(apply 'max
(vim-empty-lines-nlines-after-buffer-end w)
(mapcar 'vim-empty-lines-nlines-after-buffer-end
(remq w (get-buffer-window-list nil nil t)))))))))
(defun vim-empty-lines-update-overlay-aux (nlines-after-buffer-end)
(when (> nlines-after-buffer-end 1)
(save-excursion
(let ((indicators
(apply 'concat
(make-list nlines-after-buffer-end
(concat "\n" vim-empty-lines-indicator)))))
(overlay-put vim-empty-lines-overlay
'after-string
(concat (propertize " "
;; Forbid movement past
;; the beginning of the
;; after-string.
'cursor nlines-after-buffer-end)
(propertize indicators
'face 'vim-empty-lines-face)))))))
(defun vim-empty-lines-hide-overlay ()
(when (overlayp vim-empty-lines-overlay)
(let ((ov vim-empty-lines-overlay))
(overlay-put ov 'invisible nil)
(overlay-put ov 'display nil)
(overlay-put ov 'after-string nil))))
;;;###autoload
(define-minor-mode vim-empty-lines-mode
"Display `vim-empty-lines-indicator' on visible lines after the end of the buffer.
This differs from `indicate-empty-lines' in the way that it deals
with trailing newlines."
:lighter " ~"
:global nil
(if vim-empty-lines-mode
(progn
(make-local-variable 'vim-empty-lines-overlay)
(vim-empty-lines-create-overlay)
(vim-empty-lines-update-overlay)
(add-hook 'post-command-hook 'vim-empty-lines-update-overlay t)
(add-hook 'window-scroll-functions 'vim-empty-lines-update-overlay t))
(remove-hook 'post-command-hook 'vim-empty-lines-update-overlay t)
(remove-hook 'window-scroll-functions 'vim-empty-lines-update-overlay t)
(when (overlayp vim-empty-lines-overlay)
(delete-overlay vim-empty-lines-overlay)
(setq vim-empty-lines-overlay nil))))
;;;###autoload
(define-global-minor-mode global-vim-empty-lines-mode
vim-empty-lines-mode
(lambda ()
(unless (or (minibufferp)
;; Is there really no built-in function for detecting
;; the echo area?
(string-match-p "\\*Echo Area [0-9]+\\*" (buffer-name)))
(vim-empty-lines-mode +1)))
:group 'vim-empty-lines-mode
:require 'vim-empty-lines-mode)
(provide 'vim-empty-lines-mode)
;;; vim-empty-lines-mode.el ends here