summaryrefslogtreecommitdiff
path: root/.config/emacs/config.org
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-08-18 23:11:32 +0200
committerThomas Voss <mail@thomasvoss.com> 2023-08-18 23:11:32 +0200
commit6153d9bad072d4d659dc2da3b944b267e5ef8166 (patch)
treea7d4da9fac1635a460e73fa7a9205821ce9042fe /.config/emacs/config.org
parent6578d33fd00be0bbd48619b178d4bae4f7b43d60 (diff)
emacs: Add functions to inc- and dec numbers
Diffstat (limited to '.config/emacs/config.org')
-rw-r--r--.config/emacs/config.org39
1 files changed, 38 insertions, 1 deletions
diff --git a/.config/emacs/config.org b/.config/emacs/config.org
index ae97243..c1e0b95 100644
--- a/.config/emacs/config.org
+++ b/.config/emacs/config.org
@@ -543,7 +543,9 @@ autocompleting code but has other uses too I guess.
#+END_SRC
-** Mathematics
+** Math and Numbers
+
+*** Calc
The built-in emacs calculator ~calc~ is genuinely the best calculator program I
have ever used. Annoyingly though, it has a ‘trail’ that is always around. I
@@ -555,6 +557,41 @@ don’t like it.
#+END_SRC
+*** Increment- and Decrement Number at Point
+
+This is a pretty standard Vim feature that I dearly miss having.
+
+#+BEGIN_SRC elisp
+
+ (defun mango-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
+ (save-match-data
+ (skip-chars-backward "0123456789")
+ (when (eq (char-before (point)) ?-)
+ (goto-char (1- (point))))
+ (when (re-search-forward "-?\\([0-9]+\\)" nil t)
+ (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 mango-decrement-number-at-point (&optional arg)
+ "The same as ‘mango-increment-number-at-point’, but ARG is negated."
+ (interactive "p*")
+ (mango-increment-number-at-point (- (or arg 1))))
+
+ (keymap-global-set "C-c a" #'mango-increment-number-at-point)
+ (keymap-global-set "C-c x" #'mango-decrement-number-at-point)
+
+#+END_SRC
+
** Programming
*** Indentation