Add commands to increment and decrement an integer at point

This commit is contained in:
Bozhidar Batsov 2013-07-25 15:51:01 +03:00
parent a752457a62
commit 93c94a6f82
3 changed files with 68 additions and 1 deletions

View file

@ -232,6 +232,8 @@ Keybinding | Description
<kbd>C-c t</kbd> | Open a terminal emulator (`ansi-term`). <kbd>C-c t</kbd> | Open a terminal emulator (`ansi-term`).
<kbd>C-c k</kbd> | Kill all open buffers except the one you're currently in. <kbd>C-c k</kbd> | Kill all open buffers except the one you're currently in.
<kbd>C-c h</kbd> | Open Helm (a useful means of navigating your buffers and project files). <kbd>C-c h</kbd> | Open Helm (a useful means of navigating your buffers and project files).
<kbd>C-c +</kbd> | Increment integer at point.
<kbd>C-c -</kbd> | Decrement integer at point.
<kbd>Super-r</kbd> | Recent files <kbd>Super-r</kbd> | Recent files
<kbd>Super-x</kbd> | Expand region <kbd>Super-x</kbd> | Expand region
<kbd>Super-j</kbd> | Join lines <kbd>Super-j</kbd> | Join lines

View file

@ -453,5 +453,66 @@ Doesn't mess with special buffers."
(prelude-recompile-init) (prelude-recompile-init)
(message "Update finished. Restart Emacs to complete the process."))) (message "Update finished. Restart Emacs to complete the process.")))
(defun thing-at-point-goto-end-of-integer ()
"Go to end of integer at point."
(let ((inhibit-changing-match-data t))
;; Skip over optional sign
(when (looking-at "[+-]")
(forward-char 1))
;; Skip over digits
(skip-chars-forward "[[:digit:]]")
;; Check for at least one digit
(unless (looking-back "[[:digit:]]")
(error "No integer here"))))
(put 'integer 'beginning-op 'thing-at-point-goto-end-of-integer)
(defun thing-at-point-goto-beginning-of-integer ()
"Go to end of integer at point."
(let ((inhibit-changing-match-data t))
;; Skip backward over digits
(skip-chars-backward "[[:digit:]]")
;; Check for digits and optional sign
(unless (looking-at "[+-]?[[:digit:]]")
(error "No integer here"))
;; Skip backward over optional sign
(when (looking-back "[+-]")
(backward-char 1))))
(put 'integer 'beginning-op 'thing-at-point-goto-beginning-of-integer)
(defun thing-at-point-bounds-of-integer-at-point ()
"Get boundaries of integer at point."
(save-excursion
(let (beg end)
(thing-at-point-goto-beginning-of-integer)
(setq beg (point))
(thing-at-point-goto-end-of-integer)
(setq end (point))
(cons beg end))))
(put 'integer 'bounds-of-thing-at-point 'thing-at-point-bounds-of-integer-at-point)
(defun thing-at-point-integer-at-point ()
"Get integer at point."
(let ((bounds (bounds-of-thing-at-point 'integer)))
(string-to-number (buffer-substring (car bounds) (cdr bounds)))))
(put 'integer 'thing-at-point 'thing-at-point-integer-at-point)
(defun prelude-increment-integer-at-point (&optional inc)
"Increment integer at point by one.
With numeric prefix arg INC, increment the integer by INC amount."
(interactive "p")
(let ((inc (or inc 1))
(n (thing-at-point 'integer))
(bounds (bounds-of-thing-at-point 'integer)))
(delete-region (car bounds) (cdr bounds))
(insert (int-to-string (+ n inc)))))
(defun prelude-decrement-integer-at-point (&optional dec)
"Decrement integer at point by one.
With numeric prefix arg DEC, decrement the integer by DEC amount."
(interactive "p")
(prelude-increment-integer-at-point (- (or dec 1))))
(provide 'prelude-core) (provide 'prelude-core)
;;; prelude-core.el ends here ;;; prelude-core.el ends here

View file

@ -60,6 +60,8 @@
(define-key map (kbd "C-c k") 'prelude-kill-other-buffers) (define-key map (kbd "C-c k") 'prelude-kill-other-buffers)
(define-key map (kbd "C-c TAB") 'prelude-indent-rigidly-and-copy-to-clipboard) (define-key map (kbd "C-c TAB") 'prelude-indent-rigidly-and-copy-to-clipboard)
(define-key map (kbd "C-c h") 'helm-prelude) (define-key map (kbd "C-c h") 'helm-prelude)
(define-key map (kbd "C-c +") 'prelude-increment-integer-at-point)
(define-key map (kbd "C-c -") 'prelude-decrement-integer-at-point)
;; make some use of the Super key ;; make some use of the Super key
(define-key map [?\s-d] 'projectile-find-dir) (define-key map [?\s-d] 'projectile-find-dir)
(define-key map [?\s-p] 'projectile-switch-project) (define-key map [?\s-p] 'projectile-switch-project)
@ -99,7 +101,9 @@
["Duplicate line or region" prelude-duplicate-current-line-or-region] ["Duplicate line or region" prelude-duplicate-current-line-or-region]
["Indent rigidly and copy to clipboard" prelude-indent-rigidly-and-copy-to-clipboard] ["Indent rigidly and copy to clipboard" prelude-indent-rigidly-and-copy-to-clipboard]
["Insert date" prelude-insert-date] ["Insert date" prelude-insert-date]
["Eval and replace" prelude-eval-and-replace]) ["Eval and replace" prelude-eval-and-replace]
["Increment integer at point" prelude-increment-integer-at-point]
["Decrement integer at point" prelude-decrement-integer-at-point])
("Navigation" ("Navigation"
["Helm" helm-prelude]) ["Helm" helm-prelude])