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

Strange behaviour when opening an empty file #187

Closed
AvianY opened this issue Jan 8, 2019 · 34 comments
Closed

Strange behaviour when opening an empty file #187

AvianY opened this issue Jan 8, 2019 · 34 comments

Comments

@AvianY
Copy link

AvianY commented Jan 8, 2019

I am using zsh in alacritty.
When I try to open a file (with enter key or "L"), that is empty, it seemingly doesn't do anything, but instead starts to behave strangely.
I have to press each key twice to do something. And after I close nnn, a new "xterm" terminal opens up vim with that empty file I tried to open before, opened.

I apologise if this has been solved before, but I couldn't find anything like it

@jarun
Copy link
Owner

jarun commented Jan 9, 2019

  1. please try on master
  2. try a different terminal emulator e.g. the default on your system

@jarun
Copy link
Owner

jarun commented Jan 9, 2019

  1. try an empty zsh configuration.

I have tested nnn latest master with zsh and alacritty and everything works as expected. This should be a local issue and I am not sure I can do much about it.

@jarun jarun closed this as completed Jan 9, 2019
@AvianY
Copy link
Author

AvianY commented Jan 9, 2019

I sort of figured out the problem... it's complicated.
If I open the empty file with xdg-open, it open up in the same terminal just as I want. If I open up with mimeopen, it will do the same thing that nnn does right now.

So I uninstalled mimeopen (the package name on manjaro is perl-file-mimeinfo) and now it doesn't open with mimeopen, but xdg-open instead.
BUT now xdg-open changed the way it opens it. It opens it in a web browser.... what is going on?...

@jarun
Copy link
Owner

jarun commented Jan 9, 2019

Probably you are seeing issue #181. nnn supports only xdg-open.

@AvianY
Copy link
Author

AvianY commented Jan 10, 2019

I can open it with xdg-open and it works as it should. But when I try to open it inside nnn, it will open it in browser instead, but only after I close the terminal in which nnn is opened... any idea?

@jarun
Copy link
Owner

jarun commented Jan 10, 2019

are you on master?

@AvianY
Copy link
Author

AvianY commented Jan 10, 2019

yes, I am

@jarun
Copy link
Owner

jarun commented Jan 10, 2019

if the empty file is abc, what is the o/p of file abc?

@jarun
Copy link
Owner

jarun commented Jan 10, 2019

Also, in your file manager, if you right click and see the properties which is the opener?

@AvianY
Copy link
Author

AvianY commented Jan 10, 2019

  1. empty
  2. Vim

@AvianY
Copy link
Author

AvianY commented Jan 10, 2019

This is all so strange. xdg-open by itself performs as it should. If I run xdg-open from another app, such as lf or nnn, it will open the file in a browser.

@jarun
Copy link
Owner

jarun commented Jan 10, 2019

when you run xdg-open from a terminal, it can run vim because vim is a terminal utility.

when you invoke it from another utility, i believe xdg-open detects it's is not called from a tty and it opens in the next best suitable gui application.

Anyway, it's not an issue with nnn and you have the key e to edit the file in VISUAL/EDITOR.

@AvianY
Copy link
Author

AvianY commented Jan 11, 2019

  1. I can open a file if it has some content inside without any problem, as I would expect. The problem is only with an empty file. So I don't think this is an explanation.
  2. I might accidentally want to open it with "L" or "enter" and then nnn will start acting strange (as I explained before)... so I think it's not well thought out

@kopr12
Copy link

kopr12 commented Jan 11, 2019

I just tried this and I can confirm that this is happening.
From other terminal I made a new file with touch test.txt
Then from nnn I tried to open that file and nnn went nuts, had to kill the process eventually.
Using nnn v2.2

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

Gentlemen,

My GUI FM is thunar. When I see the property of the empty file in Thunar, the "open with" application is Vim. When I double click on the file, it opens in Vim (in its own window).

I see the same behaviour with nnn as well, i.e., when I press Enter, the file opens in vim (in its own window). I can't reproduce the issue you see.

So I believe you guys have to debug this in your environment and figure out. It is handled here:
https://github.com/jarun/nnn/blob/master/src/nnn.c#L2724

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

To be more accurate for your cases, here: https://github.com/jarun/nnn/blob/master/src/nnn.c#L2764

file returns empty so the file cannot be opened in VISUAL/EDITOR. The next thing is xdg-open. Can you guys confirm if the opener is indeed xdg-open for you using DEBUG logs?

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

@AvianY when you explicitly call xdg-open filename which application does it open in - vim in the same terminal OR vim in a new window?

@AvianY
Copy link
Author

AvianY commented Jan 11, 2019

It opens in the same terminal

@AvianY
Copy link
Author

AvianY commented Jan 11, 2019

how do I use "DEBUG logs" ?

@kopr12
Copy link

kopr12 commented Jan 11, 2019

Problem is somewhere inside nnn and it's nnn only.

  1. touch test.txt, creates empty file.
  2. xdg-open test.txt , file opens normally with Vim, which is my default editor.

Now, I don't care much about this problem because I don't have empty files that I open with nnn , but had enough time to test this issue out of curiosity.
Basically I'm just confirming this issue and explained how I reproduced it.

If the file is not empty there's no problem.

@jarun jarun changed the title Strange behaviour when opening a file Strange behaviour when opening an empty file Jan 11, 2019
@jarun jarun reopened this Jan 11, 2019
@jarun
Copy link
Owner

jarun commented Jan 11, 2019

@AvianY

Compile nnn with make debug for the debug logs enabling. You can debug print the opener.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

If I set the open with application to vim, and issue xdg-open file in terminal, I see:

$ xdg-open abc
Vim: Warning: Input is not from a terminal
~$ Vim: Error reading input, exiting...
[2;2RVim: Finished.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

I think the problem is xdg-open is behaving differently in different OSes. To handle this case I have an easy solution:

If the file is a not a text file and the size is 0-byte, when Enter is pressed, show a message: empty file - use open with or edit.

@AvianY
Copy link
Author

AvianY commented Jan 11, 2019

  1. I compiled it, but I still don't understand how to debug anything... could you please write detailed steps or send a link
  2. I have just noticed that when I open an empty file with xdg-open, it will open it in vim in read-only mode

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

It opens in the same terminal

So you mean the cli vim, right? (note that nnn assumes xdg-open with open the file with a gui application)

What happens if you set the open with application to some GUI editor, e.g. mousepad/leafpad/gedit and then try to open an empty file with nnn?

@kopr12
Copy link

kopr12 commented Jan 11, 2019

I think the problem is xdg-open is behaving differently in different OSes. To handle this case I have an easy solution:

If the file is a not a text file and the size is 0-byte, when Enter is pressed, show a message: empty file - use open with or edit.

In my case there's no problem with xdg-open opening an empty file, it doesn't open it with default editor in read-only mode like @AvianY said, it opens it with default editor but in normal mode.
Is there a reason for nnn to check if the file is empty before nnn opens it ?

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

I am at a loss because I am unable to reproduce. Instead of new questions please get your hands dirty and debug the issue.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

To answer your question, nnn makes to difference if file is 0 or not today and invokes xdg-open assuming it will open a gui utility.

@kopr12
Copy link

kopr12 commented Jan 11, 2019

Hahah, ok I won't bother you anymore.
It's a little weird though, using terminal file browser which assumes you will open GUI utility instead of a terminal one.
Besides, you shouldn't care about file size, let OS handle that.
Letting this go, good luck.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

It's a little weird though, using terminal file browser which assumes you will open GUI utility instead of a terminal one

No it's intended. That's why xdg-open is used: https://www.freedesktop.org/wiki/Software/xdg-utils/

to integrate with the desktop environment from the terminal.

nnn has no way to know if the intended utility will be a terminal one or gui one. nnn assumes it's a gui one because that's the goal of the project - to integrate the terminal with the DE.

I was thinking of handling the 0-byte file specially so it doesn't lead to the weird scenario. But I can't reproduce it.

Now, if you set the file to be opened with a gui editor (other than vim), does it work as intended?

@kopr12
Copy link

kopr12 commented Jan 11, 2019

I don't have a gui editor, otherwise I would try it.
Also, I'm not using DE but WM.

Weird that you can't reproduce it, it's really happening.
Not weird but I'm not sure why that is.

I'll try later to patch nnn and use mimeopen instead of xdg-open, to see will it change anything.

Again, I asked about 0byte file checking inside of nnn because I don't have a problem with xdg-open emptyfile, so I have to assume that there's a problem somewhere inside nnn.

Have to go now for few hours but I'll let you know whether mimeopen changed anything or not.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

so I have to assume that there's a problem somewhere inside nnn

please refer to the code.

@jarun
Copy link
Owner

jarun commented Jan 11, 2019

manjaro-xfce-18.0.2-stable-x86_64
live system, no modifications

I can see the "open with" application is "mousepad" from thunar and nnn opens the 0-byte file in mousepad.

nnn is for gui-integration. xdg-utils are also about desktop integration: https://www.freedesktop.org/wiki/Software/xdg-utils/

Please set your "open with" to a gui editor. Setting it to a CLI editor (like vim) would lead to unexpected behaviour as nnn assumes the default app is a GUI one and sets the exec() flags accordingly.

As an alternative, use the edit or open with option in nnn to open the 0-byte file.

@jarun jarun closed this as completed Jan 11, 2019
jarun added a commit that referenced this issue Jan 11, 2019
It seems the behaviour of the default opener (xdg-open) varies across platforms
and environments. Details are documented in the issue. To prevent an empty file
from being accidentally opened leading to unexpected behaviour, we are adding a
guard with the message that the empty file should be opened using edit or open
option.
@jarun
Copy link
Owner

jarun commented Jan 11, 2019

I have added a new config option to guard this case against accidental keypress. Yes, it can be annoying.

@jarun jarun mentioned this issue Jan 11, 2019
90 tasks
@jarun jarun mentioned this issue Jan 29, 2019
@jarun jarun mentioned this issue Feb 23, 2019
jarun added a commit that referenced this issue Jun 4, 2019
See #187. The behaviour has always remained so confusing. In my case a 0B file
opens in the GUI editor though I have NNN_USE_EDITOR set. We have a sensible
message in place to help users with what to do next.
@jarun jarun mentioned this issue Jul 13, 2019
10 tasks
@lock lock bot locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants