Increment decrement number under cursor?
Hello all! First of all, thank you for great Evil! Is it hard to implement increment/decrement number at point? C-a C-x in vim I'm emacs beginner but probably can implement it myself with some mentoring from gurus :) Thanks in advance! -- http://antono.info/ http://twitter.com/antono http://github.com/antono
Am Mittwoch, 17. August 2011 schrieb Antono Vasiljev:
Hello all!
First of all, thank you for great Evil!
Is it hard to implement increment/decrement number at point? C-a C-x in vim
I'm emacs beginner but probably can implement it myself with some mentoring from gurus :)
Well, this is not really an evil issue. Just look at EmacsWiki for "increment number" and you will find examples, e.g., (defun increment-number-at-point () (interactive) (skip-chars-backward "0123456789") (or (looking-at "[0123456789]+") (error "No number at point")) (replace-match (number-to-string (1+ (string-to-number (match-string 0)))))) Just bind it globally or, if you prefer, in evil-normal-state-map (define-key evil-normal-state-map (kbd "C-a") 'increment-number-at-point) If you prefer some evil-fun, you can define an operator which does the same except in visual-state, where it only increments the selected number: (evil-define-operator increment-number-at-point (beg end type) :motion evil-inner-word (let ((txt (buffer-substring-no-properties beg end))) (unless (string-match "^[0-9]+$" txt) (error "No number selected.")) (delete-region beg end) (goto-char beg) (insert (number-to-string (1+ (string-to-number txt)))) (backward-char))) I'm sure you can figure out decrementing or even count-arguments. Frank
On Aug 17, 2011, at 4:29 AM, Frank Fischer wrote:
Am Mittwoch, 17. August 2011 schrieb Antono Vasiljev:
Hello all!
First of all, thank you for great Evil!
Is it hard to implement increment/decrement number at point? C-a C-x in vim
I'm emacs beginner but probably can implement it myself with some mentoring from gurus :)
Well, this is not really an evil issue. Just look at EmacsWiki for "increment number" and you will find examples, e.g.,
(defun increment-number-at-point () (interactive) (skip-chars-backward "0123456789") (or (looking-at "[0123456789]+") (error "No number at point")) (replace-match (number-to-string (1+ (string-to-number (match-string 0))))))
Just bind it globally or, if you prefer, in evil-normal-state-map
(define-key evil-normal-state-map (kbd "C-a") 'increment-number-at-point)
If you prefer some evil-fun, you can define an operator which does the same except in visual-state, where it only increments the selected number:
(evil-define-operator increment-number-at-point (beg end type) :motion evil-inner-word (let ((txt (buffer-substring-no-properties beg end))) (unless (string-match "^[0-9]+$" txt) (error "No number selected.")) (delete-region beg end) (goto-char beg) (insert (number-to-string (1+ (string-to-number txt)))) (backward-char)))
I'm sure you can figure out decrementing or even count-arguments.
Frank
This version doesn't handle negative numbers. I should post my improved version on the wiki… but here's a paste (feel free to share) http://paste.lisp.org/display/124090 There's obviously a HUGE keybinding collision with binding "c-x" to decrement number without some kind of prefix, so I decided that C-c + / C-c - would be a fine substitute. Tim
On Aug 17, 2011, at 11:06 AM, Timothy Harper wrote:
This version doesn't handle negative numbers. I should post my improved version on the wiki… but here's a paste (feel free to share)
Whoops… last minute refactoring fail. Use this instead: http://paste.lisp.org/display/124091 Tim
On 17 Aug 2011, Frank Fischer wrote:
Am Mittwoch, 17. August 2011 schrieb Antono Vasiljev:
Hello all!
First of all, thank you for great Evil!
Is it hard to implement increment/decrement number at point? C-a C-x in vim
I'm emacs beginner but probably can implement it myself with some mentoring from gurus :)
Well, this is not really an evil issue.
While it's not an evil issue it's worth to provide an implementation. It's an vim-emulation after all :) Because I wanted to improve my own inc/dec function for a while I took the afternoon and went for vim emulation. Except that it keeps the width in binary (but that looks like a vim bug). Behold: https://gist.github.com/1161133 It's not exactly pretty and there's a function waiting to jump out but according to my tests it works as its vim counterpart does. So please test it and give me feedback :) Michael
Because I wanted to improve my own inc/dec function for a while I took the afternoon and went for vim emulation. Except that it keeps the width in binary (but that looks like a vim bug).
Behold: https://gist.github.com/1161129
It's not exactly pretty and there's a function waiting to jump out but according to my tests it works as its vim counterpart does.
So please test it and give me feedback :)
One difference i found: it does not increment numbers at end of word like vim does: my<cur>word12 should increment to myword13 Anyway, thank you for your snippet. Probably you can share it as evil-incdecr? -- http://antono.info/ http://twitter.com/antono http://github.com/antono
On 27 Aug 2011, Antono Vasiljev wrote:
Because I wanted to improve my own inc/dec function for a while I took the afternoon and went for vim emulation. Except that it keeps the width
in binary (but that looks like a vim bug).
Behold: https://gist.github.com/1161129
It's not exactly pretty and there's a function waiting to jump out but according to my tests it works as its vim counterpart does.
So please test it and give me feedback :)
One difference i found: it does not increment numbers at end of word like vim does:
my<cur>word12
should increment to myword13
Thanks, knowing this I tested that (in vim): my<cur>word word word12 increments to my<cur>word word word13 At least it doesn't try to increment over several lines ;) My code doesn't do this, but I think it's worthwhile. And a funny piece: my<cur>word word word 0b01 increments to my<cur>word word word 1b01 vim's support for binary literals sure has some issues.
Anyway, thank you for your snippet. Probably you can share it as evil-incdecr?
As add-on-package you mean? Sure. But as I already pointed out: Something alike should go into evil. Before that it sure needs some work: - I'll drop "support" for negative octal and hex literals as things just don't work and won't work since there's no single useful definition (I'm not sure what rode my while I implemented that). - Find a number from point/cursor on before inc/dec I hope you find more issues! Michael
On 27 Aug 2011, Antono Vasiljev wrote:
One difference i found: it does not increment numbers at end of word like vim does:
This works now.
Probably you can share it as evil-incdecr?
No, but as evil-numbers: https://github.com/cofi/evil-numbers So please test and have fun! :) Michael
On Fri, 02 Sep 2011 06:24:05 +0200, Michael Markert <[email protected]> wrote:
One difference i found: it does not increment numbers at end of word like vim does:
This works now.
Probably you can share it as evil-incdecr?
No, but as evil-numbers: https://github.com/cofi/evil-numbers
Great name :)
So please test and have fun! :)
It only worked for me if defined like this: -(define-key 'evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) -(define-key 'evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt) +(define-key evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) +(define-key evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt) It really works. Thank you for great plugin! -- http://antono.info/ http://twitter.com/antono http://github.com/antono
On 3 Sep 2011, Antono Vasiljev wrote:
On Fri, 02 Sep 2011 06:24:05 +0200, Michael Markert <[email protected]> wrote:
One difference i found: it does not increment numbers at end of word like vim does:
This works now.
Probably you can share it as evil-incdecr?
No, but as evil-numbers: https://github.com/cofi/evil-numbers
Great name :)
Thought so, too. :)
So please test and have fun! :)
It only worked for me if defined like this: -(define-key 'evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) -(define-key 'evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt) +(define-key evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) +(define-key evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt)
Oh, stupid mistake. Thanks for catching! Fixed in 6c6441e3.
It really works. Thank you for great plugin!
Great :) Because of your 'plugin' cue I edited the emacswiki page for evil[1] and added Tim's surround and this plugin. Michael Footnotes: [1] http://www.emacswiki.org/emacs/Evil
Hi Mike, Thanks for the "evil-numbers". I've just fixed a bug and have sent a pull request at github. York On Sat, Sep 3, 2011 at 6:48 AM, Michael Markert <[email protected]> wrote:
On 3 Sep 2011, Antono Vasiljev wrote:
On Fri, 02 Sep 2011 06:24:05 +0200, Michael Markert <[email protected]> wrote:
One difference i found: it does not increment numbers at end of word like vim does:
This works now.
Probably you can share it as evil-incdecr?
No, but as evil-numbers: https://github.com/cofi/evil-numbers
Great name :)
Thought so, too. :)
So please test and have fun! :)
It only worked for me if defined like this: -(define-key 'evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) -(define-key 'evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt) +(define-key evil-normal-state-map (kbd "C-c +") 'evil-numbers/inc-at-pt) +(define-key evil-normal-state-map (kbd "C-c -") 'evil-numbers/dec-at-pt)
Oh, stupid mistake. Thanks for catching! Fixed in 6c6441e3.
It really works. Thank you for great plugin!
Great :) Because of your 'plugin' cue I edited the emacswiki page for evil[1] and added Tim's surround and this plugin.
Michael
Footnotes:
[1] http://www.emacswiki.org/emacs/Evil
_______________________________________________ implementations-list mailing list [email protected] https://lists.ourproject.org/cgi-bin/mailman/listinfo/implementations-list
On 6 Sep 2011, York Zhao wrote:
Hi Mike,
Thanks for the "evil-numbers". I've just fixed a bug and have sent a pull request at github.
Thanks! I merged it for 0.2. But I thanked the commit message because I couldn't make heads or tails From it (also a reason why it took me so long). So get your evil numbers while it's hot! And find some more bugs :) Michael
participants (5)
-
Antono Vasiljev -
Frank Fischer -
Michael Markert -
Timothy Harper -
York Zhao