Mailing List Archive

Emacs? (was Re: total idiot question: +=, .=, etc...)
catlee@my-deja.com writes:

> I've got the following in my .vimrc file:
> au BufReadPost * if b:current_syntax == "python"
> au BufReadPost * map! += ^[:s/\(\S*\)$/\1=\1+/^M:noh^MA
> au BufReadPost * endif

I'm thinking of doing a similar thing in (X)Emacs. It's my editor of
choice though I've never actually *programmed* something into emacs
itself. I'm wondering if anyone's already done a similar thing so I
can spare the trouble (and the learning exercise, bah.)

I'm thinking the best way might be to hook in to abbrev mode or
something... I guess I have some homework to do in this area. I was
just wondering if someone's already done something like this.

In case you're just joining this thread, the general idea is to have
your editor expand this:

foo += 1

to this:

foo = foo + 1

In theory it sounds easy enough. It's just a simple matter of
programming.

Thanks,

---Preston
Emacs? (was Re: total idiot question: +=, .=, etc...) [ In reply to ]
Preston Landers <planders@mail.utexas.edu> writes:

> I'm thinking of doing a similar thing in (X)Emacs. It's my editor of
> choice though I've never actually *programmed* something into emacs
> itself. I'm wondering if anyone's already done a similar thing so I
> can spare the trouble (and the learning exercise, bah.)
>
> I'm thinking the best way might be to hook in to abbrev mode or
> something... I guess I have some homework to do in this area. I was
> just wondering if someone's already done something like this.

Not that I know of -- and that doesn't mean much --, but since I'm
always willing to learn more about Emacs (wink-wink, nudge-nudge),
here are some macros that kind of approximate it...

In Emacs, creating a macro is easy: C-x( [type stuff here] C-x)
Binding the macros to sensible keystrokes seems to be the hard part in
this case. I challenge you to find a way to integrate these into
emacs's python-mode. :-)

Put this in your .emacs

8< cut

;; Macros for Python
;; Alex Rice <alex@mindlube.com>

(defalias '++
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y +1 C-e"))
(defalias '--
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y -1 C-e"))
(defalias '+=
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y + C-e"))
(defalias '-=
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y - C-e"))
(defalias '*=
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y * C-e"))
(defalias '/=
(read-kbd-macro "C-SPC ESC b ESC w ESC f = C-y / C-e"))

8< cut

Now for example typing

spam M-x++

yields

spam=spam+1

BTW, _Learning GNU Emacs_ by O'Reilly & Assoc is a good gentle intro
to Emacs if you are like me and sometimes can't figure it out just
from the Info browser.

Cheers,

Alex Rice