Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to use Emacs (GTK Version) as a CLI Program?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
RaphaelLamperouge
n00b
n00b


Joined: 09 Oct 2015
Posts: 5

PostPosted: Fri Oct 09, 2015 7:30 am    Post subject: How to use Emacs (GTK Version) as a CLI Program? Reply with quote

Emacs GTK > Emacs NOX
But it's cumbersome to open emacs from a terminal, since it's a graphical application...

So I came up with the following Bash function, which additionally handles other graphical applications the user might want to open from the terminal:
Code:

function e () {
    if [[ $2 ]]; then
        if [[ $1 == mpv ]]; then
            echo mpv --fullscreen=no "'"$2"'" '&&' urxvt > /tmp/2 &&
                bash /tmp/2 & i3-msg kill
        fi
        echo $1 "'"$2"'" '&&' urxvt > /tmp/2 &&
            bash /tmp/2 & i3-msg kill
    else
        echo emacs "'"$1"'" '&&' urxvt > /tmp/2 &&
            bash /tmp/2  & i3-msg kill
    fi
}


So basically if I type 'e .zshrc' this function will kill the current terminal, start emacs and then create another terminal when emacs dies, if I pass two arguments, it will run the first argument instead of emacs. A special case is made so mpv opens videos with full screen disabled.

As you can see, this script has issues and I would like to improve it.

** I'm using the i3 function "i3-msg kill" which means this script will no longer be functional if I switch to another window manager. However if I use 'exit' or 'bye' instead of 'i3-msg kill' it waits until "urxvt" is started before killing the terminal, making it useless.

** If I try to open multiple files with an app it fails big time, it doesn't take the third argument since I'm only passing the 1st and 2nd. I'm also failing to figure out how to make the script ls /usr/bin | grep $1 and check if the first argument is an app or not.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Fri Oct 09, 2015 9:04 am    Post subject: Re: How to use Emacs (GTK Version) as a CLI Program? Reply with quote

RaphaelLamperouge wrote:
Emacs GTK > Emacs NOX
But it's cumbersome to open emacs from a terminal, since it's a graphical application...

Raphael ... yes, but that is the reason that emacsclient exists. You can assign xemacs to a workspace and then, using emacsclient, send files to it for editing ... no need to close, or restart, your terminal/shell.

~/.i3/config ... or ~/.config/i3/config
Code:
assign [class="(?i)emacs"] 2

... that 'class' maybe 'xemacs', I'm not sure ... look for the 'WM_CLASS(STRING)' in the output of 'xprop'.

RaphaelLamperouge wrote:
** If I try to open multiple files with an app it fails big time, it doesn't take the third argument since I'm only passing the 1st and 2nd.

Yes, but you could check the number of args and act accordingly ... but again, I'm not sure this sort of function is that useful.

RaphaelLamperouge wrote:
I'm also failing to figure out how to make the script ls /usr/bin | grep $1 and check if the first argument is an app or not.

Well, as I see you have a .zshrc ... zsh has an array called 'commands' that contains all executables in $path so its simple to use this array for tests, eg:

Code:
% a=vim ; echo $commands[$a]
/usr/bin/vim
% (($+commands[vim])) && echo true
true

HTH & best ... khay
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6276

PostPosted: Fri Oct 09, 2015 2:06 pm    Post subject: Reply with quote

I did not understand what you want to do with the script.
However, using a predictable name in /tmp is a big security mistake as it opens the gate to symlink attacks:
Either use mktemp or avoid the tmpfile at all, Maybe you can use something like bash -c $(...) instead?
Also, I have no idea what i3-msg-kill does, but maybe you are looking for nohup?
Back to top
View user's profile Send private message
RaphaelLamperouge
n00b
n00b


Joined: 09 Oct 2015
Posts: 5

PostPosted: Fri Oct 09, 2015 4:44 pm    Post subject: Reply with quote

@mv
Basically I wanna type "emacs file" and see emacs opening in the same terminal.
I currently accomplish with with "emacs -nw" but it's inferior.
I edit LaTeX files and Emacs GTK is leagues ahead than Emacs NOX for LaTeX editing so I'd like to use Emacs GTK instead, that's it =)

@khay
Thank you a lot, I'll look into your suggestions.
Using GTK emacs as a server and linking it to a specific workspace sounds interesting, it's not the same as using a CLI app but it's very close.

It would fail for casual file editing though since all jobs go into the main emacs server, causing me to use GTK Emacs to work and Emacs NOX for system maintenance.

Quote:
Also, I have no idea what i3-msg-kill does, but maybe you are looking for nohup?

The script should do the following:

-- Kill the current terminal.
-- Start emacs in the same terminal.
-- When emacs dies, start a terminal where emacs was.

So basically it should mimic the way a terminal application works. The terminal turns into the app and when the app is closed, the terminal is back there in the same position.

The basic idea is
Code:
emacs file & exit

But it fails half the time saying there are running jobs. i3-msg kill is an i3-builtin function to kill the current window.

The problem is when I try to make urxvt run after emacs is closed, if I try to run any combination of:
Quote:
bash $( emacs && urxvt ) & i3-msg kill

it waits until urxvt is started before running i3-msg kill (to kill the terminal).
It's ugly but the only way I managed to make it work is printing emacs and urxvt into a file and reading from the file.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6276

PostPosted: Fri Oct 09, 2015 6:08 pm    Post subject: Reply with quote

RaphaelLamperouge wrote:
@mv
Basically I wanna type "emacs file" and see emacs opening in the same terminal.
I currently accomplish with with "emacs -nw" but it's inferior.

I still do not understand: Running emacs without -nw (and without unsetting DISPLAY) starts emacs in its own graphical window - running such a window "within" a terminal is not possible AFAIK. So the phrase "in the same terminal" makes no sense to me.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Fri Oct 09, 2015 6:36 pm    Post subject: Reply with quote

RaphaelLamperouge wrote:
@khay Thank you a lot, I'll look into your suggestions.

Raphael ... you're welcome.

RaphaelLamperouge wrote:
It would fail for casual file editing though since all jobs go into the main emacs server, causing me to use GTK Emacs to work and Emacs NOX for system maintenance.

No, you could still use 'emacs -nw' as 'emacsclient' is a seperate application/script ... never thought I'd be giving emacs tips, haven't used it in practically 20yrs :P

RaphaelLamperouge wrote:
-- Kill the current terminal.
-- Start emacs in the same terminal.
-- When emacs dies, start a terminal where emacs was.

hehe, you're mixing metaphors, 'kill the terminal', 'open emacs (in the current workspace)', 'on exit, start a terminal (again, in the current workspace)'. It's probably odd for most of us because it goes against the idea we have of 'focus', and how the terminal might be used as an application starter. With i3wm you can have a specific 'class' (ie, emacs) behave in a certain way, so have focus when started, with all other windows behind it (and so have the input 'focused' to it), once it (emacs) exits the previous window/container is then recieving focus.

best ... khay
Back to top
View user's profile Send private message
RaphaelLamperouge
n00b
n00b


Joined: 09 Oct 2015
Posts: 5

PostPosted: Fri Oct 09, 2015 8:32 pm    Post subject: Reply with quote

Unlike terminal multiplexers, Window Managers also tile graphical windows. I'm using i3 so I believe there's no point to using emacs -nw if plain emacs is better.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Fri Oct 09, 2015 9:01 pm    Post subject: Reply with quote

RaphaelLamperouge wrote:
Unlike terminal multiplexers, Window Managers also tile graphical windows. I'm using i3 so I believe there's no point to using emacs -nw if plain emacs is better.

Raphael ... I'm not sure I understand any of what you're trying to say, or how the first sentence relates to the second.

best ... khay
Back to top
View user's profile Send private message
RaphaelLamperouge
n00b
n00b


Joined: 09 Oct 2015
Posts: 5

PostPosted: Fri Oct 09, 2015 10:34 pm    Post subject: Reply with quote

I'm not sure why it's so hard to explain this '-'

GTK Emacs > CLI Emacs
Thus, I want to use GTK emacs.
This function tries to emulate the behaviour of a CLI program.
I can't open graphical apps "inside" a terminal.
So instead, I close the current terminal, start the graphical application and then when the application is closed, the terminal is open again.
Effectively almost the same as running emacs -nw.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Fri Oct 09, 2015 11:16 pm    Post subject: Reply with quote

RaphaelLamperouge wrote:
I'm not sure why it's so hard to explain this '-'

Raphael ... I think because, as I said, it goes against what we understand as how such things work, or can be made to work ... in short "unix" like workflows.

RaphaelLamperouge wrote:
GTK Emacs > CLI Emacs
Thus, I want to use GTK emacs.
This function tries to emulate the behaviour of a CLI program.
I can't open graphical apps "inside" a terminal.
So instead, I close the current terminal, start the graphical application and then when the application is closed, the terminal is open again.
Effectively almost the same as running emacs -nw.

All of which I understood from your first post ... what doesn't make sense is that your 'emulation' is something which is pretty much already there. The fact that you're opening emacs so as to edit a text doesn't necessitate that you close the terminal, the 'new window' (emacs) can as easily be placed in 'focus', and cover, or background, said terminal. Once emacs is closed, the previous window, the terminal, is then regaining the focus. So, all the above 'function' is doing is unnecessary, in fact it seems that your not really understanding the way that windowing works. Now, if you can provide some reason why the terminal must exit on emacs starting, and start again once it closes, and why it makes sense to script such a thing then I'm willing to hear you out ... but I personally can't think of one, and mv doesn't seem to understand your reasoning either.

best ... khay
Back to top
View user's profile Send private message
RaphaelLamperouge
n00b
n00b


Joined: 09 Oct 2015
Posts: 5

PostPosted: Sat Oct 10, 2015 11:30 pm    Post subject: Reply with quote

In a tiling window manager the previous terminal will stay there occupying screen space and you'll have to move it somewhere or close it manually
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Sat Oct 10, 2015 11:41 pm    Post subject: Reply with quote

RaphaelLamperouge wrote:
In a tiling window manager the previous terminal will stay there occupying screen space and you'll have to move it somewhere or close it manually

Raphael ... actually, no.

~/.i3/config ... or ~/.config/i3/config
Code:
for_window [class="^xemacs$"] fullscreen

... and there are probably other ways to manage what gets focus, or occupies the workspace.

best ... khay
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum