Tuesday, November 15, 2011

Emacs keybindings

Emacs keybindings

I'm an Emacs guy - I finally converted after years of resisting it.
I can't function if my keybindings aren't available to me.
C-a, C-e and C-k are especially important, and their use seem to be somewhat common in the unix world.

The Factor listener, however, does not implement those.
In fact, out of the box it uses C-a for selecting all text (C-x h in emacs), C-e for Factor's "edit" command (writing + and pressing C-e in stock Factor is equivalent to writing \ + edit and pressing <RET>), and C-k for killing an entire line (emacs' C-a C-k) or the whole text (emacs' C-x h C-w), I can't remember which.

I had made a patch for fixing this severe oversight, and mrjbq7 was kind enough to apply a modified version of it. Life was good. But then Slava reverted the patch, and I went back to a dysfunctional listener. The reason the patch was reverted was because it does not provide a real solution (which would be to make keybindings customizable), and it breaks muscle memory for those who were used to Factor's keybindings and that are not emacs users. I think that's fair - I still remember how it felt to not be an emacs user - everything seemed so emacs-centric!
Now that I'm a convert nothing ever feels emacs-centric enough :).

Not to worry, though, because qx opened my eyes to the fact that I could override the stock keybinding settings using my ~/.factor-rc file.

So I thought I'd post my ~/.factor-rc file whole in hopes that it will help a fellow emacs user (or a regular unix command line user).

The only thing I'm missing is better word navigation using C-← and C-→ (currently it stops on every boundary and does not behave like emacs' M-f and M-b). I'll see if I can fix that later.

Here it is, my unadultered ~/.factor-rc file:

USING: definitions editors.emacs io.pathnames multiline
namespaces parser ui.commands ui.gadgets.editors ui.gestures
ui.operations ui.tools.listener vocabs.hierarchy vocabs.refresh
;
QUALIFIED: editors

! ------------------------------------------------
! to get a trimmed USING: vocabs list, do this:
! - start factor normally
! - edit this file and comment all lines above this comment
! - comment in the auto-use line below
! - on the open Factor listener, run "~/.factor-rc" run-file
! - you can't really type ~, so please pre-expand it
! - resolve ambiguities if any restarts are thrown
! - copy and paste the suggested USING: here
! - comment out the auto-use line
! - go to #concatenative and thank qx for his patience
! auto-use ---------------------------------------

! setup emacs to work from Factor
"/usr/local/bin/emacsclient" \ emacsclient-path set-global

! quick troubleshooting - if the above does not work:
! make sure this has been done once:
! - sudo ln -s /Applications/Emacs.app/Contents/MacOS/bin/emacsclient /usr/local/bin/emacsclient
! make sure this is included in ~/.emacs.d/init.el:
! - (server-start)

! ----------
! setup the following keybindings as emacs has them:
! C-a, C-e, C-k

! basis/ui/gadgets/editors/editors.factor
! this adds C-k, C-a, C-e
editor "caret-motion" f {
{ T{ button-down } position-caret }
{ T{ key-down f f "LEFT" } previous-character }
{ T{ key-down f f "RIGHT" } next-character }
{ T{ key-down f { C+ } "LEFT" } previous-word }
{ T{ key-down f { C+ } "RIGHT" } next-word }
{ T{ key-down f f "HOME" } start-of-line }
{ T{ key-down f f "END" } end-of-line }
{ T{ key-down f { C+ } "HOME" } start-of-document }
{ T{ key-down f { C+ } "END" } end-of-document }
{ T{ key-down f { C+ } "k" } delete-to-end-of-line } ! +
{ T{ key-down f { C+ } "a" } start-of-line } ! +
{ T{ key-down f { C+ } "e" } end-of-line } ! +
} define-command-map

! basis/ui/tools/listener/listener.factor
! this substracts a flawed C-k
interactor "interactor" f {
{ T{ key-down f f "RET" } evaluate-input }
! { T{ key-down f { C+ } "k" } clear-editor } ! -
} define-command-map

! basis/ui/tools/operations/operations.factor
! these replace C-e with C-b to invoke the "edit" command
[ pathname? ] \ editors:edit-file H{
! { +keyboard+ T{ key-down f { C+ } "e" } } ! -
{ +keyboard+ T{ key-down f { C+ } "b" } } ! +
{ +primary+ t }
{ +secondary+ t }
{ +listener+ t }
} define-operation
! ---
[ definition? ] \ editors:edit H{
! { +keyboard+ T{ key-down f { C+ } "e" } } ! -
{ +keyboard+ T{ key-down f { C+ } "b" } } ! +
{ +listener+ t }
} define-operation

! end keybindings
! ----------

! uncomment and run Factor on the command line to sort USING: vocabs
! << manifest get pprint-manifest >>

No comments:

Post a Comment