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.
This commit is contained in:
Gal Szkolnik 2022-06-29 01:25:12 -04:00
parent 70aa423d53
commit 4c40b724c8
1 changed files with 30 additions and 34 deletions

View File

@ -1,15 +1,24 @@
#!/bin/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 already running, the editor will open a new frame attached to it
### if it's not, it will first launch the daemon
### 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="/dev/null"
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=""
@ -22,37 +31,24 @@ fi
function q_() { printf "'%s'\n" "${@}"; }
function myemacs_full() {
function prep_path() {
local _cli_path="$(q_ "${@}")"
_cli_path="${_cli_path// /\\ }"
_cli_path=$(printf "$_cli_path")
_cli="$(q_ "${@}")"
_cli="${_cli// /\\ }"
_cli=$(printf "$_cli")
# echo $_cli
_cli=$(printf "/usr/bin/emacsclient \
--socket-name=$MY_EMACS_PROFILE \
$MY_EMACS_CREATEFRAME \
$MY_EMACS_NO_WAIT \
--alternate-editor='sh -c \"( \
/usr/bin/emacs --with-profile $MY_EMACS_PROFILE --daemon \
); echo ''Daemon started'' > $MY_SILENT; ( \
/usr/bin/emacsclient \
--socket-name=$MY_EMACS_PROFILE \
$MY_EMACS_NO_WAIT \
--create-frame \
${_cli//\'/\'\'} \
)\"' \
$_cli" \
)
# set -x
#echo $_cli
2>$MY_SILENT eval $_cli
printf $_cli_path
}
# FRAMES was supposed to find existing frames, but it doesn't seem to work right now
# Requires more research
#
# FRAMES=$( 2>/dev/null /usr/bin/emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" | grep t )
# echo "FRAMES: {$FRAMES}" 1>&2
$_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=''
myemacs_full "${@}"
client_args=$(prep_path "${@}")
[ "$client_args" == "''" ] && client_args='--eval "(raise-frame)"'
eval "$_cli $MY_EMACS_CREATEFRAME $MY_EMACS_NO_WAIT $client_args"