Skip to content

Instantly share code, notes, and snippets.

@toumorokoshi
Created April 18, 2013 04:32
Show Gist options
  • Save toumorokoshi/5410151 to your computer and use it in GitHub Desktop.
Save toumorokoshi/5410151 to your computer and use it in GitHub Desktop.
Increment and Decrement integers at the cursor
;; 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