aboutsummaryrefslogtreecommitdiffstats
path: root/modules/init-nlinum.el
blob: 622bd3281c636c12683e855f75a53fca6282ad68 (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
;;; init-nlinum.el --- .Emacs Configuration -*- lexical-binding: t -*-
;;; Commentary:
;;

;;; Code:
;;----------------------------------------------------------------------------
;; Line numbers
;;----------------------------------------------------------------------------
;; Linum snippets from: https://www.emacswiki.org/emacs/LineNumbers
(use-package nlinum
  :config
  (add-hook 'find-file-hook (lambda () (linum-mode 1)))
  (setq global-linum-mode t)
)

(use-package linum
  :config
  (linum-mode 1)
  :hook
  (apache-mode   . linum-mode)
  (c-mode        . linum-mode)
  (c++-mode      . linum-mode)
  (elisp-mode    . linum-mode)
  (html-mode     . linum-mode)
  (js-mode       . linum-mode)
  (js2-mode      . linum-mode)
  (less-mode     . linum-mode)
  (markdown-mode . linum-mode)
  (nginx-mode    . linum-mode)
  (php-mode      . linum-mode)
  (scss-mode     . linum-mode)
  (sass-mode     . linum-mode)
  (web-mode      . linum-mode))

(defun my-linum-get-format-string ()
  "Format the string of the column in the buffer.
It helps to show the numbering plus a separation bar."
  (let* ((width (1+ (length (number-to-string
                             (count-lines (point-min) (point-max))))))
         (format (concat "%" (number-to-string width) "d \u2502")))
    (setq my-linum-format-string format)))

(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)

(defvar my-linum-current-line-number 0)

(defun my-linum-format (line-number)
  "Defines LINE-NUMBER the numbering format of each.
this helps maintain the format in the numbering"
  (propertize (format my-linum-format-string line-number) 'face
              (if (eq line-number my-linum-current-line-number)
                  'my-linum-hl
                'linum)))
(setq linum-format 'my-linum-format)

(defadvice linum-update (around my-linum-update)
    "Defines LINUM-UPDATE for update lines.
this helps maintain the format in the numbering"
  (let ((my-linum-current-line-number (line-number-at-pos)))
    ad-do-it))
(ad-activate 'linum-update)

(defvar *linum-mdown-line* nil)

(defun line-at-click ()
  "Funtions for position lines.
this helps maintain the move visual."
  (save-excursion
    (let ((click-y (cdr (cdr (mouse-position))))
          (line-move-visual-store line-move-visual))
      (setq line-move-visual t)
      (goto-char (window-start))

      (with-no-warnings
        (next-line (1- click-y)))

      (setq line-move-visual line-move-visual-store)
      ;; If you are using tabbar substitute the next line with
      ;; (line-number-at-pos))))
      (1+ (line-number-at-pos)))))

(defun md-select-linum ()
  "Funtions for position lines.
this helps maintain the move visual."
  (interactive)
  (with-no-warnings
    (goto-line (line-at-click)))
  (set-mark (point))
  (setq *linum-mdown-line*
        (line-number-at-pos)))

(defun mu-select-linum ()
  "Funtions for position lines.
this helps maintain the move visual."
  (interactive)
  (when *linum-mdown-line*
    (let (mu-line)
      ;; (goto-line (line-at-click))
      (setq mu-line (line-at-click))
      (with-no-warnings
        (goto-line (max *linum-mdown-line* mu-line)))

      (set-mark (line-end-position))
      (with-no-warnings
        (goto-line (min *linum-mdown-line* mu-line)))
      (setq *linum-mdown*
            nil))))

(global-set-key (kbd "<left-margin> <down-mouse-1>") 'md-select-linum)
(global-set-key (kbd "<left-margin> <mouse-1>") 'mu-select-linum)
(global-set-key (kbd "<left-margin> <drag-mouse-1>") 'mu-select-linum)

(provide 'init-nlinum)

;; Local Variables:
;; byte-compile-warnings: (not free-vars)
;; End:
;;; init-nlinum.el ends here