line-number: fix backward compatibility

There was an error in `linum-on` when `dotspacemacs-line-numbers` was set to
`'relative`.

- new function `spacemacs//linum-backward-compabitility` to test for old
supported values.
- Use an :around advice for `linum-on` instead of redefining it.
- move linum init time config to `:init` section of `use-package`
- fix relative linum initialization by testing if `dotspacemacs-line-numbers` is
  a list first.
This commit is contained in:
syl20bnr 2017-02-05 13:50:50 -05:00
parent ddfb996b20
commit d5d80eb1a1
3 changed files with 47 additions and 31 deletions

View File

@ -1034,12 +1034,32 @@ if prefix argument ARG is given, switch to it in an other, possibly new window."
(when compilation-last-buffer
(delete-windows-on compilation-last-buffer)))
;; Line number
(defun spacemacs/no-linum (&rest ignore)
"Disable linum if current buffer."
(when (or 'linum-mode global-linum-mode)
(linum-mode 0)))
(defun spacemacs/linum-update-window-scale-fix (win)
(defun spacemacs/enable-line-numbers-p ()
"Return non-nil if line numbers should be enabled for current buffer.
Decision is based on `dotspacemacs-line-numbers'."
(and dotspacemacs-line-numbers
(spacemacs//linum-current-buffer-is-not-special)
(or (spacemacs//linum-backward-compabitility)
(spacemacs//linum-curent-buffer-is-not-too-big)
;; explicitly enabled buffers take priority over explicitly disabled
;; ones
(or (spacemacs//linum-enabled-for-current-major-mode)
(not (spacemacs//linum-disabled-for-current-major-mode))))))
(defun spacemacs//linum-on (origfunc &rest args)
"Advice function to improve `linum-on' function."
(when (spacemacs/enable-line-numbers-p)
(apply origfunc args)))
(defun spacemacs//linum-update-window-scale-fix (win)
"Fix linum for scaled text in the window WIN."
(set-window-margins win
(ceiling (* (if (boundp 'text-scale-mode-step)
@ -1048,6 +1068,14 @@ if prefix argument ARG is given, switch to it in an other, possibly new window."
(if (car (window-margins))
(car (window-margins)) 1)))))
(defun spacemacs//linum-backward-compabitility ()
"Return non-nil if `dotspacemacs-line-numbers' has an old format and if
`linum' should be enabled."
(and dotspacemacs-line-numbers
(not (listp dotspacemacs-line-numbers))
(or (eq dotspacemacs-line-numbers t)
(eq dotspacemacs-line-numbers 'relative))))
(defun spacemacs//linum-current-buffer-is-not-special ()
"Return non-nil if current buffer is not a special buffer."
(not (string-match-p "\\*.*\\*" (buffer-name))))
@ -1070,14 +1098,3 @@ if prefix argument ARG is given, switch to it in an other, possibly new window."
(and (spacemacs/mplist-get dotspacemacs-line-numbers :disabled-for-modes)
(memq major-mode (spacemacs/mplist-get dotspacemacs-line-numbers
:disabled-for-modes))))
(defun spacemacs/enable-line-numbers-p ()
"Return non-nil if line numbers should be enabled for current buffer.
Decision is based on `dotspacemacs-line-numbers'."
(and dotspacemacs-line-numbers
(spacemacs//linum-current-buffer-is-not-special)
(spacemacs//linum-curent-buffer-is-not-too-big)
;; explicitly enabled buffers take priority over explicitly disabled
;; ones
(or (spacemacs//linum-enabled-for-current-major-mode)
(not (spacemacs//linum-disabled-for-current-major-mode)))))

View File

@ -241,26 +241,24 @@
(defun spacemacs-base/init-linum ()
(use-package linum
:init
(progn
(setq linum-format "%4d")
(spacemacs|add-toggle line-numbers
:mode linum-mode
:documentation "Show the line numbers."
:evil-leader "tn")
(advice-add #'linum-update-window
:after #'spacemacs//linum-update-window-scale-fix)
(advice-add #'linum-on
:around #'spacemacs//linum-on))
:config
(progn
(if (or (eq dotspacemacs-line-numbers t)
(eq dotspacemacs-line-numbers 'relative))
(progn
(add-hook 'prog-mode-hook 'linum-mode)
(add-hook 'text-mode-hook 'linum-mode)))
(defun linum-on ()
"Overwrite the original `linum-on' function with a more selective one."
(when (spacemacs/enable-line-numbers-p)
(linum-mode)))
(when (spacemacs//linum-backward-compabitility)
(add-hook 'prog-mode-hook 'linum-mode)
(add-hook 'text-mode-hook 'linum-mode))
(when dotspacemacs-line-numbers
(global-linum-mode))))
(setq linum-format "%4d")
(spacemacs|add-toggle line-numbers
:mode linum-mode
:documentation "Show the line numbers."
:evil-leader "tn")
(advice-add #'linum-update-window
:after #'spacemacs/linum-update-window-scale-fix))
(global-linum-mode)))))
(defun spacemacs-base/init-occur-mode ()
(evilified-state-evilify-map occur-mode-map

View File

@ -260,8 +260,9 @@
:init
(progn
(when (or (eq dotspacemacs-line-numbers 'relative)
(car (spacemacs/mplist-get dotspacemacs-line-numbers
:relative)))
(and (listp dotspacemacs-line-numbers)
(car (spacemacs/mplist-get dotspacemacs-line-numbers
:relative))))
(add-hook 'spacemacs-post-user-config-hook 'linum-relative-on))
(spacemacs/set-leader-keys "tr" 'spacemacs/linum-relative-toggle))
:config