summaryrefslogtreecommitdiff
path: root/.config/emacs
diff options
context:
space:
mode:
Diffstat (limited to '.config/emacs')
-rw-r--r--.config/emacs/increment.el84
-rw-r--r--.config/emacs/init.el39
2 files changed, 93 insertions, 30 deletions
diff --git a/.config/emacs/increment.el b/.config/emacs/increment.el
new file mode 100644
index 0000000..ade0246
--- /dev/null
+++ b/.config/emacs/increment.el
@@ -0,0 +1,84 @@
+;;; increment.el -- Increment numbers at point -*- lexical-binding: t; -*-
+
+(defun increment--number-to-binary-string (number)
+ (let (s)
+ (while (not (= number 0))
+ (setq s (concat (if (= 1 (logand number 1)) "1" "0") s)
+ number (lsh number -1)))
+ (when (string= s "")
+ (setq s "0"))
+ s))
+
+(defun increment--format-number-with-base
+ (number base leading-zeros buffer-substr hex-style)
+ (let* ((neg (> 0 number))
+ (number (abs number))
+ (number-string
+ (cond ((= base 2) (increment--number-to-binary-string number))
+ ((= base 8) (format "%o" number))
+ ((= base 10) (number-to-string number))
+ ((= base 16) (format (if (eq hex-style 'lower)
+ "%x"
+ "%X")
+ number))
+ ((t (error (format "Invalid base %d") base)))))
+ (length-diff (- (length buffer-substr)
+ (length number-string)))
+ (leading-zeros (if (> leading-zeros 0)
+ (+ leading-zeros length-diff)
+ 0)))
+ (concat
+ (when neg
+ "-")
+ (cond ((= base 2) "0b")
+ ((= base 8) "0o")
+ ((= base 16) "0x"))
+ (when (> leading-zeros 0)
+ (make-string leading-zeros ?0))
+ number-string)))
+
+;;;###autoload
+(defun increment-number-at-point (&optional arg)
+ "Increment the number at point by ARG or 1 if ARG is nil. If called
+interactively, the universal argument can be used to specify ARG. If
+the number at point has leading zeros then the width of the number is
+preserved."
+ (interactive "*p")
+ (save-match-data
+ (let* (hex-style
+ (case-fold-search nil)
+ (base (cond
+ ((thing-at-point-looking-at
+ "\\(-\\|\\b\\)0x\\(0*\\)\\([0-9a-f]+\\)\\b")
+ (setq hex-style 'lower)
+ 16)
+ ((thing-at-point-looking-at
+ "\\(-\\|\\b\\)0x\\(0*\\)\\([0-9A-Fa-f]+\\)\\b")
+ 16)
+ ((thing-at-point-looking-at
+ "\\(-?\\)\\(0*\\)\\([0-9]+\\)")
+ 10)
+ ((thing-at-point-looking-at
+ "\\(-\\|\\b\\)0o\\(0*\\)\\([0-7]+\\)\\b")
+ 8)
+ ((thing-at-point-looking-at
+ "\\(-\\|\\b\\)0b\\(0*\\)\\([01]+\\)\\b")
+ 2)))
+ (substr (buffer-substring-no-properties (match-beginning 3)
+ (match-end 3)))
+ (sign (if (= (match-beginning 1) (match-end 1)) +1 -1))
+ (result (+ (* (string-to-number substr base) sign)
+ (or arg 1))))
+ (replace-match (increment--format-number-with-base
+ result base
+ (- (match-end 2)
+ (match-beginning 2))
+ substr hex-style)))))
+
+;;;###autoload
+(defun decrement-number-at-point (&optional arg)
+ "The same as `increment-number-at-point', but ARG is negated."
+ (interactive "*p")
+ (increment-number-at-point (- (or arg 1))))
+
+(provide 'increment)
diff --git a/.config/emacs/init.el b/.config/emacs/init.el
index 9fdfc0d..f801eba 100644
--- a/.config/emacs/init.el
+++ b/.config/emacs/init.el
@@ -296,30 +296,14 @@ selection."
(corfu-auto-delay 0))
;;; Increment- and Decrement Numbers
-(defun x-increment-number-at-point (&optional arg)
- "Increment the number at point by ARG or 1 if ARG is nil. If called
-interactively, the universal argument can be used to specify ARG. If
-the number at point has leading zeros then the width of the number is
-preserved."
- (interactive "*p")
- (save-excursion
- (skip-chars-backward "0123456789")
- (when (eq (char-before (point)) ?-)
- (goto-char (1- (point))))
- (save-match-data
- (when (re-search-forward "-?\\([0-9]+\\)" nil :noerror)
- (let ((answer (+ (string-to-number (match-string 0) 10)
- (or arg 1)))
- (width (length (match-string 1))))
- (replace-match
- (format
- (concat "%0" (int-to-string (if (< answer 0) (1+ width) width)) "d")
- answer)))))))
-
-(defun x-decrement-number-at-point (&optional arg)
- "The same as ‘x-increment-number-at-point’, but ARG is negated."
- (interactive "*p")
- (x-increment-number-at-point (- (or arg 1))))
+(use-package increment
+ :ensure nil
+ :commands (increment-number-at-point
+ decrement-number-at-point)
+ :init
+ (evil-define-key '(normal visual) 'global
+ (kbd "<leader> n i") #'increment-number-at-point
+ (kbd "<leader> n d") #'decrement-number-at-point))
;;; Indentation Settings
(setq-default
@@ -939,12 +923,7 @@ the comparison of the ‘company-pseudo-tooltip-overlay’ height and 0 using PR
:normal
:prefix "g"
- ("s" magit-status)
-
- :normal&visual
- :prefix "n"
- ("d" x-decrement-number-at-point)
- ("i" x-increment-number-at-point))
+ ("s" magit-status))
(with-eval-after-load 'eglot
(x-define-evil-bindings