Last active
December 10, 2016 11:36
-
-
Save howardabrams/9244369 to your computer and use it in GitHub Desktop.
A simple approach to easily incrementing (or decrementing) the next number in an Emacs buffer. This seems to be helpful when editing odd XML data files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun increment-decrement (amount) | |
(let* ((for-number "[0-9][0-9]*") | |
(end (re-search-forward for-number)) | |
(start (match-beginning 0)) | |
(str (buffer-substring start end)) | |
(num (+ amount (string-to-number str)))) | |
(delete-region start end) | |
(insert (number-to-string num)) | |
(goto-char start))) | |
(defun increment (start end) | |
"Searches forward in the buffer and increments the first number | |
it finds." | |
(interactive "r") | |
(increment-decrement 1)) | |
(defun decrement (start end) | |
"Searches forward in the buffer and decrement the first number | |
it finds." | |
(interactive "r") | |
(increment-decrement -1)) | |
(global-set-key (kbd "C-c <up>") 'increment) | |
(global-set-key (kbd "C-c <down>") 'decrement) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment