mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.75k stars 709 forks source link

Autosave Question #805

Closed bhajneet closed 7 years ago

bhajneet commented 7 years ago

Is this functionality possible with buffers and an outside script?

lenormf commented 7 years ago

No.

Feel free to join #kakoune @ irc.freenode.net if you have quick questions, and don't forget to read the documentation.

bhajneet commented 7 years ago

I figured it would cut down on the same questions if they were saved here. I got a lot of good answers from older questions, but didn't see an auto-save one.

I tried the IRC chat, it's quite empty and in the chance someone can help me if they look later, I might not be there. This seemed to be the most logical choice for even simple questions.

Anyhow, thanks to your suggestion of reading the documentation, I got pretty close to auto save. Hook on focus out to execute :w on all buffers. Do you think that's the best option? I have to manually delete the debug buffer for it to work in projects, but it does save time over all when building. I suppose I could even hook to have it delete the debug buffer when starting kakoune, but that seems risque.

mawww commented 7 years ago

I'd suggest :wa to write all file related buffers, instead of iterating on all buffers and doing :w, that way you wont have the *debug* buffer problem.

bhajneet commented 7 years ago

Ah, I had read the documentation wrong as: "Write all buffers related to file". That makes a lot more sense! Thank you.

lenormf commented 7 years ago

Not sure what channel you joined that was "empty", but the official one hovers over under 20 people at all times.

bhajneet commented 7 years ago

That's the one

sroccaserra commented 1 year ago

Would saving on leaving insert mode or on entering normal mode (I'm not sure what is the better option here), and on NormalIdle implement something similar to autosave ?

hook global ModeChange .*:insert:.* %{ try %{
    write
} }

hook global NormalIdle .* %{ try %{
    write
} }

Something like that in the kakrc file? What would be the shortcomings of such an approach in your opinion?

tucker-m commented 1 year ago

^ That pretty much does everything I want it to, except I also like it to save when I switch to a different window. For example, switching to a terminal window to run a compiler, so you want changes to be saved. If your terminal has FocusOut events (I'm using tmux, which does), then you can also add:

hook global FocusOut .* %{ try %{
    write
} }

That was my setup with neovim and I don't think I had to manually save a file for years.

EDIT: I just realized that this is exactly what bhajneet already said a few years ago. Well, +1 to that idea.

Veraellyunjie commented 6 months ago

@tucker-m kudos for researching and sharing on FocusOut. You mention tmux, but for me on the contrary, it breaks FocusOut. Using snippets from a similar issue:

hook global BufCreate /.* %{
    hook buffer FocusOut .* %{
        try %{
            %sh{ notify-send FocusOut }
        }
    }
}

Works in tilda and kitty terminals, doesn't work in tilda+tmux and kitty+tmux. Doesn't work in xterm.

Tried set-option -g focus-events on in tmux to no avail.

Is there any special configuration for tmux? Also, can FocusOut work in xterm?

Screwtapello commented 6 months ago

I suspect that it's not FocusOut breaking inside tmux, but notify-send.

If I launch Kakoune with:

kak -n -e 'hook global FocusOut .* %{ echo "lost focus" }; hook global FocusIn .* %{ echo "gained focus" }'

...then I get the "lost focus" and "gained focus" messages as expected in tmux 3.3a, with set-option -g focus-events on in my ~/.tmux.conf file. Note that if you switch to the Kakoune pane by clicking with the mouse, that automatically dismisses the "gained focus" message; if you switch with the keyboard you'll be able to see it.

It also works the same way in XTerm 388.

Veraellyunjie commented 6 months ago

@Screwtapello I tried

kak -n -e 'hook global FocusOut .* %{ echo "lost focus" }; hook global FocusIn .* %{ echo "gained focus" }'

same result: in kitty and tilda can see messages, while nothing in tmux and xterm

> tmux -V
tmux openbsd-7.4
> xterm -version
XTerm/OpenBSD(378)
tucker-m commented 6 months ago

Is there any special configuration for tmux?

Sorry @Veraellyunjie, I'm not sure why that's not working. What I posted in my message is still what's in my config, and it works on my computer. I'm using iterm2 and tmux on macOS. I'm not familiar with xterm and won't be able to try it out, unfortunately.

My entire tmux config is just:

set -s escape-time 0
set-option -g focus-events on

and I don't think escape-time would be affecting the FocusOut event.

Veraellyunjie commented 6 months ago

Having rebooted, tmux works for FocusOut.
Before rebooting, I had tried interactively Ctrl+B+:+set-option -g focus-events on which failed to make it work. Dunno...

My thanks to contributors.

FocusOut in xterm - with and without tmux - still doesn't work for me...