Skip to content

Instantly share code, notes, and snippets.

@cofi
Created August 21, 2011 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cofi/1161133 to your computer and use it in GitHub Desktop.
Save cofi/1161133 to your computer and use it in GitHub Desktop.
(eval-when-compile
(require 'cl))
(defun cofi/inc-at-pt (amount)
"Increment the number at point by `amount'"
(interactive "p*")
(save-match-data
(or
;; find binary literals
(when (looking-back "0[bB][01]*")
;; already ensured there's only one -
(skip-chars-backward "01")
(search-forward-regexp "[01]*")
(replace-match
(cofi/format-binary (+ amount (string-to-number (match-string 0) 2))
(- (match-end 0) (match-beginning 0))))
t)
;; find octal literals
(when (looking-back "0[oO]-?[0-7]*")
;; already ensured there's only one -
(skip-chars-backward "-01234567")
(search-forward-regexp "-?\\([0-7]+\\)")
(replace-match
(format (format "%%0%do" (- (match-end 1) (match-beginning 1)))
(+ amount (string-to-number (match-string 0) 8))))
t)
;; find hex literals
(when (looking-back "0[xX]-?[0-9a-fA-F]*")
;; already ensured there's only one -
(skip-chars-backward "-0123456789abcdefABCDEF")
(search-forward-regexp "-?\\([0-9a-fA-F]+\\)")
(replace-match
(format (format "%%0%dX" (- (match-end 1) (match-beginning 1)))
(+ amount (string-to-number (match-string 0) 16))))
t)
;; find decimal literals
(progn
(skip-chars-backward "0123456789")
(skip-chars-backward "-")
(when (looking-at "-?\\([0-9]+\\)")
(replace-match
(format (format "%%0%dd" (- (match-end 1) (match-beginning 1)))
(+ amount (string-to-number (match-string 0) 10))))
t))
(error "No number at point"))))
(defun* cofi/format-binary (number &optional width (fillchar ?0))
"Format `NUMBER' as binary.
Fill up to `WIDTH' with `FILLCHAR' (defaults to ?0) if binary
representation of `NUMBER' is smaller."
(let (nums)
(do ((num number (truncate num 2)))
((= num 0))
(push (number-to-string (% num 2)) nums))
(let ((len (length nums)))
(apply #'concat
(if (and width (< len width))
(make-string (- width len) fillchar)
"")
nums))))
(defun cofi/dec-at-pt (amount)
"Decrement the number at point by `amount'"
(interactive "p*")
(cofi/inc-at-pt (- amount)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment