Add operate-on-number

Replace prelude functions for increment and decrement of numbers.

Signed-off-by: Tu Do <tu.h.do@dektech.com.au>
This commit is contained in:
Tu Do 2014-05-21 11:52:50 +07:00
parent a3d336ba9a
commit 5bf38b05da
4 changed files with 18 additions and 68 deletions

View file

@ -477,67 +477,6 @@ With a prefix ARG updates all installed packages."
(epl-installed-packages)))) (epl-installed-packages))))
(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))))
;;; Emacs in OSX already has fullscreen support ;;; Emacs in OSX already has fullscreen support
;;; Emacs has a similar built-in command in 24.4 ;;; Emacs has a similar built-in command in 24.4
(defun prelude-fullscreen () (defun prelude-fullscreen ()

View file

@ -374,6 +374,18 @@ indent yanked text (with prefix arg don't indent)."
(global-set-key [remap kill-ring-save] 'easy-kill) (global-set-key [remap kill-ring-save] 'easy-kill)
(global-set-key [remap mark-sexp] 'easy-mark) (global-set-key [remap mark-sexp] 'easy-mark)
;;
(require 'operate-on-number)
(smartrep-define-key global-map "C-c ."
'(("+" . apply-operation-to-number-at-point)
("-" . apply-operation-to-number-at-point)
("*" . apply-operation-to-number-at-point)
("/" . apply-operation-to-number-at-point)
("^" . apply-operation-to-number-at-point)
("<" . apply-operation-to-number-at-point)
(">" . apply-operation-to-number-at-point)
("'" . operate-on-number-at-point)))
(provide 'prelude-editor) (provide 'prelude-editor)
;;; prelude-editor.el ends here ;;; prelude-editor.el ends here

View file

@ -63,8 +63,6 @@
(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)
(define-key map (kbd "C-c I") 'prelude-find-user-init-file) (define-key map (kbd "C-c I") 'prelude-find-user-init-file)
(define-key map (kbd "C-c S") 'prelude-find-shell-init-file) (define-key map (kbd "C-c S") 'prelude-find-shell-init-file)
;; make some use of the Super key ;; make some use of the Super key
@ -109,8 +107,7 @@
["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])

View file

@ -64,8 +64,10 @@
projectile projectile
magit magit
move-text move-text
operate-on-number
rainbow-mode rainbow-mode
smartparens smartparens
smartrep
undo-tree undo-tree
volatile-highlights volatile-highlights
zenburn-theme) zenburn-theme)