/
linum-relative.el
218 lines (180 loc) · 7.45 KB
/
linum-relative.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;;; linum-relative.el --- display relative line number in emacs.
;; Copyright (c) 2013 - 2016 Yen-Chin, Lee.
;;
;; Author: coldnew <coldnew.tw@gmail.com>
;; Keywords: converience
;; X-URL: http://github.com/coldnew/linum-relative
;; Version: 0.5
;; 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 2, 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; [![MELPA](http://melpa.org/packages/linum-relative-badge.svg)](http://melpa.org/#/linum-relative)
;; [![MELPA Stable](http://stable.melpa.org/packages/linum-relative-badge.svg)](http://stable.melpa.org/#/linum-relative)
;; ![Screenshot](https://github.com/coldnew/linum-relative/raw/master/screenshot/screenshot1.jpg)
;;
;; linum-relative lets you display relative line numbers for current buffer.
;;
;;; Installation:
;; If you have `melpa` and `emacs24` installed, simply type:
;;
;; M-x package-install linum-relative
;;
;; And add the following to your .emacs
;;
;; (require 'linum-relative)
;;; Setup & Tips:
;; The non-interactive function *linum-on* (which should already be built into recent GNU Emacs distributions), turns on side-bar line numbering:
;;
;; (linum-on)
;;
;; and alternatively, by enabling `linum-mode`:
;;
;; M-x linum-mode
;;
;; Relative line numbering should already be enabled by default (by installing this package), following *linum-on* or enabling *linum-mode*. One can also use the *linum-relative-toggle* interactive function to switch between relative and non-relative line numbering:
;;
;; M-x linum-relative-toggle
;;
;;; Code:
(eval-when-compile (require 'cl))
(require 'linum)
(defgroup linum-relative nil
"Show relative line numbers on fringe."
:group 'convenience)
;;;; Faces
(defface linum-relative-current-face
'((t :inherit linum :foreground "#CAE682" :background "#444444" :weight bold))
"Face for displaying current line."
:group 'linum-relative)
;;;; Customize Variables
(defcustom linum-relative-current-symbol "0"
"The symbol you want to show on the current line, by default it is 0.
You can use any string like \"->\". If this variable is empty string,
linum-releative will show the real line number at current line."
:type 'string
:group 'linum-relative)
(defcustom linum-relative-plusp-offset 0
"Offset to use for positive relative line numbers."
:type 'integer
:group 'linum-relative)
(defcustom linum-relative-format "%3s"
"Format for each line. Good for adding spaces/paddings like so: \" %3s \""
:type 'string
:group 'linum-relative)
(defcustom linum-relative-lighter " LR"
"Lighter of linum-relative-mode"
:type 'string
:group 'linum-relative)
;;;; Internal Variables
(defvar linum-relative-last-pos 0
"Store last position.")
(defvar linum-relative-user-format linum-format
"Store the users linum-format")
;;;; helm support
(defvar helm-buffer)
(defvar helm-candidate-separator)
(defvar helm-alive-p)
(declare-function with-helm-buffer "ext:helm-lib.el" (&rest body))
(declare-function helm-candidate-number-at-point "ext:helm.el")
(declare-function helm-pos-header-line-p "ext:helm.el")
(defmacro linum-relative-with-helm-buffer (&rest body)
(when (fboundp 'with-helm-buffer)
`(with-helm-buffer ,@body)))
(defun linum-relative-in-helm-p ()
"Return non nil when in an helm session."
(bound-and-true-p helm-alive-p))
(defun linum-relative-for-helm ()
(linum-relative-with-helm-buffer
(make-local-variable 'linum-relative-last-pos))
(linum-update helm-buffer))
;;;; Advices
(defadvice linum-update (before relative-linum-update activate)
"This advice get the last position of linum."
(if (linum-relative-in-helm-p)
(setq linum-relative-last-pos (helm-candidate-number-at-point))
(setq linum-relative-last-pos (line-number-at-pos))))
;;;; Functions
(defun linum-relative (line-number)
(when (linum-relative-in-helm-p)
(linum-relative-with-helm-buffer
(if (looking-at helm-candidate-separator)
(setq line-number (save-excursion
(forward-line 1) (helm-candidate-number-at-point)))
(setq line-number (helm-candidate-number-at-point)))))
(let* ((diff1 (abs (- line-number linum-relative-last-pos)))
(diff (if (minusp diff1)
diff1
(+ diff1 linum-relative-plusp-offset)))
(current-p (= diff linum-relative-plusp-offset))
(current-symbol (if (and linum-relative-current-symbol current-p)
(if (string= "" linum-relative-current-symbol)
(number-to-string line-number)
linum-relative-current-symbol)
(number-to-string diff)))
(face (if current-p 'linum-relative-current-face 'linum)))
(if (and (linum-relative-in-helm-p)
(linum-relative-with-helm-buffer
(or (looking-at helm-candidate-separator)
(eq (point-at-bol) (point-at-eol))
(helm-pos-header-line-p))))
(propertize (format linum-relative-format current-symbol) 'invisible t)
(propertize (format linum-relative-format current-symbol) 'face face))))
(defun linum-relative-on ()
"Turn ON linum-relative."
(unless (eq linum-format 'linum-relative)
(setq linum-relative-user-format linum-format)
(setq linum-format 'linum-relative)))
(defun linum-relative-off ()
"Turn OFF linum-relative."
(setq linum-format linum-relative-user-format))
;;;###autoload
(defun linum-relative-toggle ()
"Toggle between linum-relative and linum."
(interactive)
(if (eq linum-format 'linum-relative)
(linum-relative-off)
(linum-relative-on)))
;;;###autoload
(define-minor-mode linum-relative-mode
"Display relative line numbers for current buffer."
:group 'linum-relative
:lighter linum-relative-lighter
(if linum-relative-mode
(progn
(linum-relative-on)
(linum-mode 1))
(linum-relative-off)
(linum-mode -1)))
;;;###autoload
(define-global-minor-mode linum-relative-global-mode
linum-relative-mode (lambda () (unless (linum-relative-in-helm-p)
(linum-relative-mode 1))))
;;;; Interaction of helm with linum-relative
(defun helm--turn-on-linum-relative ()
(with-helm-buffer (linum-relative-mode 1)))
;;;###autoload
(define-minor-mode helm-linum-relative-mode
"Turn on `linum-relative-mode' in helm."
:group 'helm
(if helm-linum-relative-mode
(progn
(add-hook 'helm-move-selection-after-hook 'linum-relative-for-helm)
(add-hook 'helm-after-initialize-hook 'helm--turn-on-linum-relative)
(add-hook 'helm-after-preselection-hook 'linum-relative-for-helm))
(remove-hook 'helm-move-selection-after-hook 'linum-relative-for-helm)
(remove-hook 'helm-after-initialize-hook 'helm--turn-on-linum-relative)
(remove-hook 'helm-after-preselection-hook 'linum-relative-for-helm)))
(provide 'linum-relative)
;;; linum-relative.el ends here.