Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:q only disables Goyo #16

Closed
ghost opened this issue Feb 6, 2014 · 26 comments
Closed

:q only disables Goyo #16

ghost opened this issue Feb 6, 2014 · 26 comments

Comments

@ghost
Copy link

ghost commented Feb 6, 2014

Hi,

First of all, this is an excellent plugin. There's just one minor problem I'm having with it, it's that when Goyo is active :q doesn't quit but simply closes Goyo. Therefore two quits are needed to really quit. I've "fixed" it this way but I'm thinking maybe there is a cleaner way to do it?

" Quitting whether Goyo is active or not
ca wq :w<cr>:call Quit()<cr>
ca q :call Quit()<cr>
function! Quit()
    if winnr('$') > 3
        Goyo
    endif
    quit
endfunction

What do you think?

Thank you and best regards,
Maxime.

@junegunn
Copy link
Owner

junegunn commented Feb 6, 2014

Hi, thanks for your interest in Goyo. Actually it's intended behavior. I designed Goyo to work like an ordinary tab, which you can open with :tab split or :tab new. If you like to really quit Vim at once, you can :qa or :wqa.

@ghost
Copy link
Author

ghost commented Feb 6, 2014

Thanks for the reply! I get your point but as far as I'm concerned my muscle memory is too used to quitting single windows with :q. But anyway my "fix" seems to work pretty well so I'll keep using it.

@junegunn
Copy link
Owner

junegunn commented Feb 6, 2014

Fair enough, for your information, you can use exists('#goyo') to check if you're in Goyo mode.

@junegunn junegunn closed this as completed Apr 7, 2014
@axelGschaider
Copy link

lovely plugin!

But I agree with mm2703.
I tested his workaround (works for me too) and integrated your (junegunn) advice on how to check Goyo mode:

" Quitting whether Goyo is active or not
ca wq :w<cr>:call Quit()<cr>
ca q :call Quit()<cr>
function! Quit()
    if exists('#goyo')
        Goyo
    endif
    quit
endfunction

as far as I'm concerned (of course ~95% are mm2703) feel free to add this your customization page in the wiki.

@junegunn
Copy link
Owner

junegunn commented Apr 8, 2014

Will do, thanks!

To explain the reasoning behind the default behavior is that I see Goyo as a "mode" (like "zen mode" from other editors) which I can quickly switch on and off during an editing session. But of course you can have a different point of view.

@axelGschaider
Copy link

No need to explain :)

But me for example I configured mutt (the email client) to open vim in Goyo mode right away. And from there I want to close vim right away. And of course mm2703 has a point with the muscle memory.

Anyway: I am happy as it is but i would, of course, prefer to have this as an "official" config option. I don't know if I'll ever have the time to do this but if I implemented this "properly" would you be willing to pull? (wouldn't want to implement something that you don't want to have in your code base in the first place)

@junegunn
Copy link
Owner

junegunn commented Apr 9, 2014

Thanks, but I think an additional option is unnecessary as we already have a workaround.
By the way, I think I have a more solid one, which uses goyo_after callback.

function! g:goyo_before()
  let b:quitting = 0
  autocmd QuitPre <buffer> let b:quitting = 1
endfunction

function! g:goyo_after()
  " Quit Vim if this is the only remaining buffer
  if b:quitting && len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
    qa
  endif
endfunction

let g:goyo_callbacks = [function('g:goyo_before'), function('g:goyo_after')]

(EDIT: Updated to quit only on :q, and not on :bd, :close, gt, etc.)

@axelGschaider
Copy link

ok thanks. I'll try the new version as soon as I'm able

@ghost
Copy link
Author

ghost commented Apr 11, 2014

I would like to point out that the workaround works when you simply type :q and have only one tab open but closes all other tabs if you have more than one. Which is why I don't use it anymore.

@axelGschaider
Copy link

not sure why but the workaround also breaks ":q!" some situations

@junegunn
Copy link
Owner

Strange, I don't see any issue with both cases you mentioned. If you could provide me with a script that reproduces the problem, I'll take a look.

@junegunn
Copy link
Owner

One thing I noticed it that QuitPre event was added and bug-fixed pretty recently. Maybe you're running a slightly older version of Vim?

Patch 7.3.753
Problem:    When there is a QuitPre autocommand using ":q" twice does not work
        for exiting when there are more files to edit.
Solution:   Do not decrement quitmore in an autocommand. (Techlive Zheng)
Files:      src/ex_docmd.c, src/fileio.c, src/proto/fileio.pro

Patch 7.3.544
Problem:    There is no good way to close a quickfix window when closing the
        last ordinary window.
Solution:   Add the QuitPre autocommand.
Files:      src/ex_docmd.c, src/fileio.c, src/vim.h

@ghost
Copy link
Author

ghost commented Apr 15, 2014

As far as I'm concerned I'm running MacVim, I don't think there's a way I can update it?

@axelGschaider
Copy link

@junegunn: sorry. I was a bit vague :/
usually I give better error descriptions . . . I swear!

Yes. I'm running 7.3. with the corrections 1-547
I'll try to update soon. But now that I had the time to think about it all for 30 seconds I don't believe it's going to solve it.

The situation is this:

  • start vim WITHOUT opening a file (this is important)
  • write some characters
  • go back to normal/command mode and start typing ":q!"
  • upon typing the "!" (note: before hitting return) I get the error message that I haven't written since last edit and should force with "!"

I'm nowhere good enough in vimscript to debug this. But it sure looks like the workaround is being run prematurely.

@junegunn
Copy link
Owner

@mm2703 You mean the system-default Vim (/usr/bin/vim) that comes with OSX? I'm running Mavericks and the default Vim doesn't even have QuitPre:

Error detected while processing function <SNR>3_lod_cmd..<SNR>57_goyo..<SNR>57_goyo_on..g:goyo_before:
line   10:
E216: No such group or event: QuitPre <buffer> let b:quitting = 1

Installing a newer version of vim and macvim is trivial if you use Homebrew, brew install vim macvim


@axelGschaider I see what you're getting at. Just change qa in the callback into qa!.


If you don't mind Vim quitting not just on q, but also on any other command that causes Goyo window to close (e.g. close, tabclose, etc), this is the version without QuitPre:

function! g:goyo_before()
endfunction

function! g:goyo_after()
  " Quit Vim if this is the only remaining buffer
  if len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
    qa
  endif
endfunction

let g:goyo_callbacks = [function('g:goyo_before'), function('g:goyo_after')]

@junegunn
Copy link
Owner

@axelGschaider Hmm, changing it to qa! also causes q to quit unconditionally. I'll try to come up with a better solution.

@junegunn
Copy link
Owner

@axelGschaider Try this:

function! g:goyo_before()
  let b:quitting = 0
  let b:quitting_bang = 0
  autocmd QuitPre <buffer> let b:quitting = 1
  cabbrev <buffer> q! let b:quitting_bang = 1 <bar> q!
endfunction

function! g:goyo_after()
  " Quit Vim if this is the only remaining buffer
  if b:quitting && len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
    if b:quitting_bang
      qa!
    else
      qa
    endif
  endif
endfunction

let g:goyo_callbacks = [function('g:goyo_before'), function('g:goyo_after')]

@axelGschaider
Copy link

@junegunn wonderfull. Works like a charm. Very nice.

:q and :q! work just the way they should
:wq does not work (it saves and than closes goyo but not vim).
But I'm happy so far :)

@mwcz
Copy link

mwcz commented Jul 10, 2014

This is great! Many thanks to everyone who solved this problem. It works when I launch vim and then run :Goyo. Even :wq works for me, @axelGschaider (meaning it saves, then quits vim, not just goyo).

However, when I launch vim with...

vim +Goyo
# or
vim -c "Goyo"

The goyo_callbacks solution does not work, and :q goes back to its "quit only goyo" behavior.

I'd like this to work, specifically for when I launch vim from mutt, or from vim-anywhere. In both cases I'd like Goyo to be active from the start, and for :wq to save and quit vim entirely.

I'm not sure where to look from here!

@mwcz
Copy link

mwcz commented Jul 10, 2014

Arg, please disregard. It was user error. 👎

@junegunn
Copy link
Owner

@mwcz Okay. And please note that the recent version of Vim requires a function name to start with an uppercase letter, so you might want to change the names of the callback functions accordingly. e.g g:goyo_before to GoyoBefore

@mwcz
Copy link

mwcz commented Jul 11, 2014

@junegunn Interesting, thanks. I'm using Vim 7.4 and the lower-case g: functions work, for what it's worth.

@junegunn
Copy link
Owner

@mwcz Yeah, it was very recently changed, April this year. See: ftp://ftp.vim.org/pub/vim/patches/7.4/7.4.260

mrtazz added a commit to mrtazz/vim-zenroom2 that referenced this issue Aug 29, 2015
this allows for setting custom goyo callbacks in addition to the zenroom ones. This is useful for adding workarounds like the one mentioned in junegunn/goyo.vim#16. Right now those custom callbacks would get overwritten, this change makes it append the callbacks.
@kminh
Copy link

kminh commented Nov 12, 2015

I'm trying out the suggestion on the wiki page, if I type :q when there are unsaved changes, the warning message is not shown:

screenshot from 2015-11-12 23-47-56

Not sure if this is expected?

It's a clean install of Goyo and the only callbacks being used are the ones on the wiki page.

@junegunn
Copy link
Owner

@kminh Then Goyo is closed but you still stay in Vim, right? I didn't intend it but I don't know how to "fix" it. Let me know if you find a workaround.

@kminh
Copy link

kminh commented Nov 15, 2015

@junegunn Vim is closed as well if that's the last buffer, so everything seems correct, except that the warning message ("Save changes to ....? Yes, No, Cancel") is not shown under ":q". It seems to be hidden somewhere (perhaps in the original tab?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants