Initial commit

This commit is contained in:
BirDt_ 2025-08-13 16:39:06 +08:00
commit d01a242121
3 changed files with 152 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
# Remove Emacs garbage
*~
\#*\#

22
README.org Normal file
View file

@ -0,0 +1,22 @@
#+title: hybrid-linum-mode
#+author: Jakub Nowak
=hybrid-linum-mode= is a minor mode that provides an alternative line number display to =display-line-numbers-mode=, which looks slightly more like vim's hybrid line numbering.
The main difference between this and =(setq display-line-numbers 'relative)= is that the current line number can be modified via =current-line-number-display-function=, and by default is the absolute line number which is left aligned (unlike =display-line-numbers-mode=, which right aligns all text).
=relative-line-number-face= and =current-line-number-face= are provided to customise the display.
* Installation
=hybrid-linum-mode.el= can be placed in the emacs user directory (usually =~/.emacs.d=) and then ~(load "hybrid-linum-mode")~ should be added to your init file to automatically load the package.
The mode can be automatically enabled, for example in ~prog-mode~ to automatically show line numbers in programming modes, by adding ~(add-hook 'prog-mode-hook #'hybrid-linum-mode)~ to your init file.
** Straight
The following snippet can be used to install the package via Straight.
#+begin_src elisp
(straight-use-package
'(hybrid-linum-mode :type git
:host nil
:repo "https://git.cyan.sh/BirDt/hybrid-linum-mode.git"
:files ("*.el")))
#+end_src

127
hybrid-linum-mode.el Normal file
View file

@ -0,0 +1,127 @@
;;; hybrid-linum-mode.el --- Hybrid line numbers in Emacs -*- lexical-binding: t -*-
;; Author: Jakub Nowak
;; Maintainer: Jakub Nowak
;; Version: 1.0
;; Package-Requires: none
;; Homepage: git.cyan.sh/birdt_/hybrid-linum-mode
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is not likely particularly fast, but it should not be particularly slow either.
;;; Code:
(defface relative-line-number-face
'((t :inherit shadow))
"Face used for showing surrounding (relative) line numbers."
:group 'basic-faces
:group 'hybrid-linum-mode)
(defface current-line-number-face
'((t :inherit font-lock-keyword-face))
"Face used for showing the current (absolute) line number."
:group 'basic-faces
:group 'hybrid-linum-mode)
(defcustom current-line-number-display-function
(lambda () (number-to-string (line-number-at-pos last-post-command-position)))
"Function that returns the string to display at the current line."
:type 'function
:group 'hybrid-linum-mode)
(defun relative-line-number (&optional pos)
"Return relative line number for POS (defaults to point).
The current line returns 0."
(let* ((pos-line (line-number-at-pos (or pos (point))))
(current-line (line-number-at-pos (point))))
(abs (- pos-line current-line))))
(defun build-hybrid-line-number ()
"Generate line number string for current line."
(let* ((line (number-to-string (relative-line-number last-post-command-position))))
(propertize
(if (equal "0" line)
(funcall current-line-number-display-function)
(concat " "
(when (= 1 (length line)) " ") ;; Pad with an extra space if the number is a single digit.
line " "))
'face (if (equal "0" line)
'current-line-number-face
'relative-line-number-face))))
(defun display-hybrid-line-numbers (start limit)
"Display line numbers in the margin between START and LIMIT."
(goto-char start)
(while (< (point) limit)
(let ((line-str (build-hybrid-line-number))
(ov (make-overlay (line-beginning-position) (line-beginning-position))))
(overlay-put ov 'hybrid-linum-margin t)
(overlay-put ov 'before-string
(propertize " "
'display `((margin left-margin) ,line-str))))
(forward-line 1)))
(defvar last-post-command-position 0
"Holds the cursor position from the last run of post-command-hooks.")
(make-variable-buffer-local 'last-post-command-position)
(defvar hybrid-linum-mode-enabled )
(defvar previous-margin-width 0
"Holds the previous margin width before hybrid-linum-mode was activated.")
(make-variable-buffer-local 'previous-margin-width)
(defvar previous-line-number-mode 0
"Holds the previous value of display-line-numbers-mode before hybrid-linum-mode was activated.")
(make-variable-buffer-local 'previous-line-number-mode)
(defun update-linenum-on-cursor-move ()
"Update hybrid line number if the cursor position changed."
(unless (equal (point) last-post-command-position)
(setq last-post-command-position (point))
(save-excursion
(remove-overlays (point-min) (point-max) 'hybrid-linum-margin t)
(display-hybrid-line-numbers (window-start) (window-end)))))
(define-minor-mode hybrid-linum-mode
"Show left-aligned line numbers in the left margin."
:init-value nil
(if hybrid-linum-mode
(progn
(setq previous-line-number-mode display-line-numbers)
(setq previous-margin-width left-margin-width)
(setq display-line-numbers nil)
(setq left-margin-width 5)
(setq last-post-command-position (point))
(set-window-buffer nil (current-buffer))
(remove-overlays (point-min) (point-max) 'hybrid-linum-margin t)
(save-excursion
(display-hybrid-line-numbers (window-start) (window-end)))
(add-hook 'post-command-hook #'update-linenum-on-cursor-move 0 t))
(remove-overlays (point-min) (point-max) 'hybrid-linum-margin t)
(remove-hook 'post-command-hook #'update-linenum-on-cursor-move)
(setq left-margin-width previous-margin-width)
(setq display-line-numbers previous-line-number-mode)
(set-window-buffer nil (current-buffer))))
(provide 'hybrid-linum-mode)
;;; hybrid-linum-mode.el ends here