summaryrefslogtreecommitdiff
path: root/elpa/linum-relative-20180124.1047/linum-relative.el
blob: 581e60c6332061f88e8aa0b4873cbc7a37731677 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;;; linum-relative.el --- display relative line number in emacs.

;; Copyright (c) 2013 - 2018 Yen-Chin, Lee.
;;
;; Author: coldnew <coldnew.tw@gmail.com>
;; Keywords: converience
;; Package-Version: 20180124.1047
;; Package-Commit: c74a6981b688a5e1e6b8e0809363963ff558ce4d
;; X-URL: http://github.com/coldnew/linum-relative
;; Version: 0.6

;; 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 using command:
;;
;;     M-x linum-relative-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
;;

;;; Backends

;; By default, linum-relative use *linum-mode* as backend, since linum-mode is based on emacs-lisp, you may have performance issue on large file.
;;
;; Since linum-relative 0.6, if you also use emacs version 26.1 or above, you can setup `linum-relative-backend' to make linum-relative-mode use `display-line-number-mode' as backend, which is implement in C so the performance is really nice.
;;
;; However some linum-relative's customize function may not work propely.
;;
;; Here's how to use `display-line-number-mode' as backend:
;;
;; ```elisp
;;      ;; Use `display-line-number-mode' as linum-mode's backend for smooth performance
;;	(setq linum-relative-backend 'display-line-numbers-mode)
;; ```


;;; Code:

(eval-when-compile (require 'cl))

;; display-line-numbers is shipped with emacs 26.0.50
(when (version<= "26.0.50" emacs-version)
  (require 'display-line-numbers))
(require 'linum)


(defgroup linum-relative nil
  "Show relative line numbers on fringe."
  :group 'convenience)

;;;; Faces
;; NOTE: can't work on `display-line-numbers-mode'
(defface linum-relative-current-face
  '((t :inherit linum :foreground "#CAE682" :background "#444444" :weight bold))
  "Face for displaying current line.

This won't take effect if you choose `display-line-numbers-mode' backend."
  :group 'linum-relative)

;;;; Customize Variables

;; NOTE: can't work on `display-line-numbers-mode'
(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.

This won't take effect if you choose `display-line-numbers-mode' backend."
  :type 'string
  :group 'linum-relative)

;; NOTE: can't work on `display-line-numbers-mode'
(defcustom linum-relative-plusp-offset 0
  "Offset to use for positive relative line numbers.

This won't take effect if you choose `display-line-numbers-mode' backend."
  :type 'integer
  :group 'linum-relative)

;; NOTE: can't work on `display-line-numbers-mode'
(defcustom linum-relative-format "%3s"
  "Format for each line. Good for adding spaces/paddings like so: \" %3s \"

This won't take effect if you choose `display-line-numbers-mode' backend."
  :type 'string
  :group 'linum-relative)

(defcustom linum-relative-lighter " LR"
  "Lighter of linum-relative-mode"
  :type 'string
  :group 'linum-relative)

(defcustom linum-relative-backend 'linum-mode
  "The default backend for `linum-relative', by default we use
`linum-mode' (slow), you can switch to `display-line-numbers-mode' if
you has emacs-version greater than 26.0.50."
  :group 'linum-relative
  :type '(choice (const :tag "Use display-line-numbers-mode as backend" display-line-numbers-mode)
                 (other :tag "Use linum-mode as backend" linum-mode)))

;;;; Internal Variables

;; NOTE: can't work on `display-line-numbers-mode'
(defvar linum-relative-last-pos 0
  "Store last position.")

;; NOTE: can't work on `display-line-numbers-mode'
(defvar linum-relative-user-format linum-format
  "Store the users linum-format")

(defvar linum-relative-user-type (bound-and-true-p display-line-numbers-type)
  "Store the user's `display-line-number-type' value")

;;;; 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."
  (cond
   ;; if use `display-line-numbers-mode'
   ((eq linum-relative-backend 'display-line-numbers-mode)
    (unless (eq linum-relative-user-type 'relative)
      (setq linum-relative-user-type display-line-numbers-type)
      (setq display-line-numbers-type 'relative))
    (display-line-numbers-mode 1))
   ;; default for linum-mode backend
   (t (unless (eq linum-format 'linum-relative)
        (setq linum-relative-user-format linum-format)
        (setq linum-format 'linum-relative))
      (linum-mode 1))))

(defun linum-relative-off ()
  "Turn OFF linum-relative."
  (cond
   ;; if use `display-line-numbers-mode'
   ((eq linum-relative-backend 'display-line-numbers-mode)
    (setq display-line-numbers-type linum-relative-user-type)
    (display-line-numbers-mode -1))
   ;; default for linum-mode backend
   (t (setq linum-format linum-relative-user-format)
      (linum-mode -1))))

;;;###autoload
(defun linum-relative-toggle ()
  "Toggle between linum-relative and linum."
  (interactive)
  (cond
   ;; if use `display-line-numbers-mode'
   ((eq linum-relative-backend 'display-line-numbers-mode)
    (if (eq display-line-numbers-type 'relative)
        (linum-relative-off)
        (linum-relative-on)))
   ;; default for linum-mode backend
   (t (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
      (linum-relative-on)
      (linum-relative-off)))

;;;###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.
Copyright 2019--2024 Marius PETER