mogenslund / liquid

Clojure Text Editor, for editing clojure code and markdown. Written entirely in Clojure with inspiration from Emacs and Vim.
Eclipse Public License 1.0
965 stars 42 forks source link

Keybindings. #13

Closed pieceofduke closed 5 years ago

pieceofduke commented 6 years ago

Hello again. Managed to fire up your creation, already feeling the potential. Whoa. The thing is, I use Colemak keyboard layout, so I want to redefine ALL the keys right away. So I had to dive into the source code to see where those are defined. And those are hardcoded all over the different files. Wouldn't it be better to extract them to edn config files?..

mogenslund commented 6 years ago

The mappings are defined for each "app", so that is why they are spread a bit. The most common app is textapp, which is for editing text files. All the bindings for that is defined in textapp.clj. There is a global keymapping, which is used if the app-level keybinding does not resolve. I like the concept os being able to map keys directly to functions. If I had to use edn files, I would need another mapping from the values in the edn to the real functions. That would take away the power to map to an inline function.

I made a function in textapp to allow users to overwrite the keybindings. In the liana example it would look like:

(ns liana.core
  (:require [dk.salza.liq.editor :as editor]
            [dk.salza.liq.apps.textapp :as textapp]
            [dk.salza.liq.core]))

(def newkeys
  {"k" editor/backward-line
   "h" editor/backward-char
   "j" editor/forward-line
   "l" editor/forward-char})

(defn -main
  [& args]
  (doseq [[k v] newkeys]
    (textapp/set-navigation-key k v))
  (apply dk.salza.liq.core/startup args)
  (dk.salza.liq.core/init-editor)
  (editor/add-snippet "I am Liana")

  (editor/updated))

(ns user
  (:require [dk.salza.liq.tools.cshell :refer :all]
            [dk.salza.additives.blob :refer :all]))

Notice that in practise there will not be many more lines than remapped keys, which would also be the case with edn file.

And if for some reason you want to call a custom inline function (like moving forward twice) it could look like:

(textapp/set-navigation-key "l" #(do (forward-char) (forward-char)))

If you want a very customized app, you can take a copy of like textapp.clj. Call it like mynewtextapp.clj. Do the changes and redefine the editor to use it as default with:

(editor/set-default-app mynewtextapp/run)

See https://github.com/mogenslund/liquid-vim/blob/master/src/dk/salza/liquid_vim/core.clj (An example of a more vim-like rewrite.)

pieceofduke commented 6 years ago

This is awesome, thank.you

On Fri, 13 Apr 2018, 20:45 Mogens Brødsgaard Lund, notifications@github.com wrote:

The mappings are defined for each "app", so that is why they are spread a bit. The most common app is textapp, which is for editing text files. All the bindings for that is defined in textapp.clj. There is a global keymapping, which is used if the app-level keybinding does not resolve. I like the concept os being able to map keys directly to functions. If I had to use edn files, I would need another mapping from the values in the edn to the real functions. That would take away the power to map to an inline function.

I made a function in textapp to allow users to overwrite the keybindings. In the liana example it would look like:

(ns liana.core (:require [dk.salza.liq.editor :as editor] [dk.salza.liq.apps.textapp :as textapp] [dk.salza.liq.core]))

(def newkeys {"k" editor/backward-line "h" editor/backward-char "j" editor/forward-line "l" editor/forward-char})

(defn -main [& args] (doseq [[k v] newkeys] (textapp/set-navigation-key k v)) (apply dk.salza.liq.core/startup args) (dk.salza.liq.core/init-editor) (editor/add-snippet "I am Liana")

(editor/updated))

(ns user (:require [dk.salza.liq.tools.cshell :refer :all] [dk.salza.additives.blob :refer :all]))

Notice that in practise there will not be many more lines than remapped keys, which would also be the case with edn file.

And if for some reason you want to call a custom inline function (like moving forward twice) it could look like:

(textapp/set-navigation-key "l" #(do (forward-char) (forward-char)))

If you want a very customized app, you can take a copy of like textapp.clj. Call it like mynewtextapp.clj. Do the changes and redefine the editor to use it as default with:

(editor/set-default-app mynewtextapp/run)

See https://github.com/mogenslund/liquid-vim/blob/master/src/dk/salza/liquid_vim/core.clj (An example of a more vim-like rewrite.)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mogenslund/liquid/issues/13#issuecomment-381211507, or mute the thread https://github.com/notifications/unsubscribe-auth/ATdJY6N51tFkpeeX_nsMj5oMNfFfthSWks5toOROgaJpZM4TToJe .

mogenslund commented 5 years ago

No more work needed.