Skip to content

Instantly share code, notes, and snippets.

@gongo
Created November 27, 2009 13:58
Show Gist options
  • Save gongo/244029 to your computer and use it in GitHub Desktop.
Save gongo/244029 to your computer and use it in GitHub Desktop.
バッファ内で、カーソルの位置にある数字列をインクリメント(もしくは引数に指定した数字を加算)する
(defun increment-string-as-number (number)
"Replace progression string of the position of the cursor
by string that added NUMBER.
Interactively, NUMBER is the prefix arg.
examle:
At the cursor string \"12\"
M-x increment-string-as-number ;; replaced by \"13\"
C-u 10 M-x increment-string-as-number ;; replaced by \"22\"
At the cursor string \"-12\"
M-x increment-string-as-number ;; replaced by \"-11\"
C-u 100 M-x increment-string-as-number ;; replaced by \"88\""
(interactive "P")
(let ((col (current-column))
(p (if (integerp number) number 1)))
(skip-chars-backward "-0123456789")
(or (looking-at "-?[0123456789]+")
(error "No number at point"))
(replace-match
(number-to-string (+ p (string-to-number (match-string 0)))))
(move-to-column col)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment