help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Daemon and client: Only open new frame if there isn't one already?


From: Hugo Heden
Subject: Re: Daemon and client: Only open new frame if there isn't one already?
Date: Sun, 15 Mar 2009 13:31:45 +0100

On Fri, Mar 13, 2009 at 10:20 PM, Johan Bockgård
<bojohan+news@dd.chalmers.se> wrote:
> Hugo Heden <hugoheden@gmail.com> writes:
>
>
>> The first time I invoke emacsclient it opens in a terminal, not in a
>> window-manager-managed GUI-frame. I have to manually create a
>> GUI-frame using --create-frame.
>
> That seems to be the intended behavior
>
>    "If the Emacs process has no existing frame--which can happen if it
>     was started as a daemon--then Emacs opens a frame on the terminal
>     in which you called `emacsclient', as though you had used the `-t'
>     option."
>
> (info "(emacs) Invoking emacsclient")
>

Below follows a bash-script that does what I want to do:

If there is no daemon running, one is started automatically. If the
user has *not* specified -nw, -t, -tty, -c or --create-frame, then the
script figures out whether there is a frame open on the current
$DISPLAY, and if not the script *adds* the option --create-frame to
the options before passing them on to the real emacsclient.

Best regards

Hugo Heden

--

#!/bin/bash

# If user has specified -nw, -t or -tty, or for that matter -c or
# --create-frame, then we will do nothing special, just pass on all
# arguments arguments to emacsclient. But if none of that is
# specified, we will add --create-frame if (and only if) there is no
# frame currently open on the current $DISPLAY


function start_daemon() {
    emacs-snapshot --daemon
}

function invoke_client() {
    emacsclient.emacs-snapshot $@
}

args="$@"

if [ "x$DISPLAY" != "x" ] ; then

    user_has_specified=false;
    for opt in $args ; do
        case $opt in
            -t)   user_has_specified=true; break ;; # break out of the loop
            -tty) user_has_specified=true; break ;;
            -nw)  user_has_specified=true; break ;;
            -c)   user_has_specified=true; break ;;
            --create-frame) user_has_specified=true; break ;;
        esac
    done

    if [ $user_has_specified == false ] ; then
        display="\"$DISPLAY\""
            # display will contain something like "", ":0.0" or
            # "localhost:10.0" (including the quotes)
        current_emacs_displays=`invoke_client --eval
"(x-display-list)" 2>/dev/null || start_daemon`
            # current_emacs_displays will contain something like (":0.0"
            # "localhost:10.0") current_emacs_displays may also be
empty if there
            # was no daemon running, of if there is no frame open.
        if [[ ! "$current_emacs_displays" =~ "$display" ]] ; then
                # If there is a match (display is included in
                # current_emacs_displays), then there is already a frame open in
                # this $DISPLAY. *Otherwise* we'll add the --create-frame
                # option
            args="$args --create-frame"
        fi
    fi

fi # if DISPLAY

invoke_client $args || ( start_daemon && invoke_client $args )




reply via email to

[Prev in Thread] Current Thread [Next in Thread]