reddit / emacs

How to define my own embark function/keymap in doom emacs

Err000r__1010 · 2022-01-24 08:15
Hi, I'm using doom emacs on macos and trying to define a keybinding to do following using `embark` and `vertico`: * Say I'm scrolling through a file list (`find-file, org-roam-node-find` etc). * I find a file that I may need, I press `C-; v` and it opens in a new vertical window. Essentially `C-x 4 C-f` but in reverse order. I want to select the file first and *then* open it in a vertical or horizontal window (same frame). This is what I have so far: (map! :map embark-file-map :desc "Open file in v-split" "v" (lambda () (interactive) (+evil/window-vsplit-and-follow) (find-file (embark--vertico-selected))) :desc "Open file in h-split" "h" (lambda () (interactive) (+evil/window-split-and-follow) (find-file (embark--vertico-selected)))) I expected this to let me select and file and then decide weather to open in vertical/horizontal window. But I get this error: `embark PCH: (wrong-type-argument stringp nil)`. I understand the error, but don't know how to pass the file name to the `find-file` function. Also is this the correct way to define an `embark` mapping for doom? Thanks!
organic-bookworm · 2022-01-24 13:46
>I understand the error, but don’t know how to pass the file name to the `find-file` function. See [How does Embark call the actions?](https://github.com/oantolin/embark/#how-does-embark-call-the-actions). For interactive function `embark` prefills the first prompt it opens with a target. And then `embark-allow-edit-actions` controls, whether the input should be accepted right away. For non-interactive function, `embark` passes it a target as a sole argument. ​ Options * call `find-file` interactively yourself, so `embark` prefills its prompt ​ (lambda () (interactive) (+evil/window-vsplit-and-follow) (call-interactively #'find-file)) * add interactive call specs, so `embark` prefills its prompt ​ (lambda (file) (interactive "f") ;; binds =file= to result of (read-file-name ...), when called interactively (+evil/window-vsplit-and-follow) (find-file file)) * make function non-interactive ​ (lambda (file) (+evil/window-vsplit-and-follow) (find-file file)) Also note, that `embark` supports targets in **regular buffers**, not only **minibuffers**. The same `embark-file-map` would be used to act on files in **regular buffers**, so you probably don’t want to couple it tightly with `vertico` in this case anyway. ​ >Also is this the correct way to define an `embark` mapping for doom? See [Creating your own keymaps](https://github.com/oantolin/embark/#creating-your-own-keymaps). Keymaps used by `embark` are regular keymaps. And `map!` is the “Doom’s way” of defining keybindings. So that’s Doom alright, I think.
Err000r__1010 · 2022-01-24 14:43
thank you so much !!
organic-bookworm · 2022-01-24 15:14
You're welcome.
oantolin · 2022-01-24 20:10
Perfect answer, thanks for writing it!