dotfiles.2022/_home/private_dot_local/bin/executable_edit-with-spacemacs
Gal Szkolnik 4c40b724c8 edit-with-emacs fixed
It actually is doing what it's suppose to do:
1. Ensure that emacs-daemon is running.
2. If no frame exists a new frame is opened
3. If frame exists, open within that frame
4. CURRENTLY NOT WORKING: Set focus on frame. Focus is actually set, but
   frame isn't coming forward, will require additional research into
   this issue.
2022-06-29 01:36:44 -04:00

54 lines
1.7 KiB
Bash

#! /usr/bin/env bash
# For some details on how this code got to be:
# https://emacs.stackexchange.com/a/72377/38372
###
### edit-with-spacemacs, ensures a daemon named spacemacs is working
### if it's not running, it will launch the daemon
### Then it will check if a frame already exists, if it does, it will
### open the file in that frame.
### If a frame doesn't exist, a new frame will open.
### Note that if that frame is in another virtual desktop, you will
### need to manually switch there.
###
MY_EMACS_PROFILE=${CHEMACS_PROFILE:-spacemacs}
MY_SILENT=${MY_SILENT:-"/dev/null"}
MY_EMACS_CREATEFRAME="--create-frame"
MY_EMACS_NO_WAIT="--no-wait"
DEBUG_MY_EMACS=${DEBUG_MY_EMACS}
_cli="/usr/bin/emacsclient --socket-name=$MY_EMACS_PROFILE "
# MY_SILENT="/dev/tty"
# MY_EMACS_NO_WAIT=""
if [ "$DISPLAY" = "" ]; then
MY_EMACS_NO_WAIT=""
fi
# set | grep '^MY_EMACS' 1>&2
function q_() { printf "'%s'\n" "${@}"; }
function prep_path() {
local _cli_path="$(q_ "${@}")"
_cli_path="${_cli_path// /\\ }"
_cli_path=$(printf "$_cli_path")
printf $_cli_path
}
$_cli --eval '(+ 1 0)' > /dev/null || {
echo "Starting server..."
2>$MY_SILENT /usr/bin/emacs --bg-daemon=$MY_EMACS_PROFILE -- --with-profile=spacemacs
}
# At this point the server should be up and running, and so now we just
# need to see if we have any frames open
# If there are frames, we are NOT going to create a new frame, and so
# MY_EMACS_CREATEFRAME will be set to blank.
[ "$($_cli --eval '(> (length (frame-list)) 1)')" == "t" ] && MY_EMACS_CREATEFRAME=''
client_args=$(prep_path "${@}")
[ "$client_args" == "''" ] && client_args='--eval "(raise-frame)"'
eval "$_cli $MY_EMACS_CREATEFRAME $MY_EMACS_NO_WAIT $client_args"