JM445
JM445

Reputation: 178

Pass bash function parameters to emacs

I'm facing a problem while trying to make an emacs daemon management function in Bash.

Here is the function snippet:

function ne
{
if [ $# -ge 2 -a "$1" '==' "-s" ]
then
    server="$2";
    param=${@:3};
else
    server="default";
    param=${@:1};
fi
nbsrv=`ls ~/.emacs.d/server | grep "$server" | wc --chars`
if [ "$nbsrv" '==' "0" ]
then
    echo "Starting server '$server'";
    emacs --daemon=$server
fi
emacsclient --server-file=$server -nw $param;
}

It almost works, the problem is with:

param=${@:x}

For exemple, if I run:

ne -s srv1 file1 file2

It does not open me 2 new files but one named "file1 file2"

Have you got an idea of how I can make this works fine?

Thank's !

JM445

PS: Sorry if my english is not perfect, I'm french

Upvotes: 0

Views: 108

Answers (1)

phils
phils

Reputation: 73236

Don't bother with bash arrays for this. Just shift off the positional parameters you don't want, and pass the remainder to emacsclient with "$@"

Your script with this modification looks like:

if [ $# -ge 2 -a "$1" '==' "-s" ]
then
    server="$2";
    shift 2;
else
    server="default";
fi
nbsrv=`ls ~/.emacs.d/server | grep "$server" | wc --chars`
if [ "$nbsrv" '==' "0" ]
then
    echo "Starting server '$server'";
    emacs --daemon=$server
fi
emacsclient --server-file=$server -nw "$@";

Upvotes: 2

Related Questions