Created
April 18, 2013 04:32
-
-
Save toumorokoshi/5410151 to your computer and use it in GitHub Desktop.
Increment and Decrement integers at the cursor
This file contains hidden or 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
;; check if string is an integer | |
(defun string-integer-p (string) | |
(if (string-match "\\`[-+]?[0-9]+\\'" string) | |
t | |
nil)) | |
;; Decrement Int | |
(defun decrement () | |
"Decrement the integer that the cursor is on." | |
(interactive) | |
(let ((x (thing-at-point 'symbol))) | |
(when (string-integer-p x) | |
(let ((x-int (string-to-number x)) | |
(bds (bounds-of-thing-at-point 'symbol))) | |
(progn | |
(delete-region (car bds) (cdr bds)) | |
(insert (number-to-string (- x-int 1))) | |
) | |
) | |
) | |
) | |
) | |
;; Increment Int | |
(defun increment () | |
"Increment the integer that the cursor is on." | |
(interactive) | |
(let ((x (thing-at-point 'symbol))) | |
(when (string-integer-p x) | |
(let ((x-int (string-to-number x)) | |
(bds (bounds-of-thing-at-point 'symbol))) | |
(progn | |
(delete-region (car bds) (cdr bds)) | |
(insert (number-to-string (+ x-int 1))) | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment