Last active
June 14, 2024 03:46
-
-
Save akagr/18ec650bc5f7e0ef663af9a30ce02973 to your computer and use it in GitHub Desktop.
MacOS launch helper to start emacs daemon or attach to one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ███████╗███╗░░░███╗░█████╗░░█████╗░░██████╗ | |
-- ██╔════╝████╗░████║██╔══██╗██╔══██╗██╔════╝ | |
-- █████╗░░██╔████╔██║███████║██║░░╚═╝╚█████╗░ | |
-- ██╔══╝░░██║╚██╔╝██║██╔══██║██║░░██╗░╚═══██╗ | |
-- ███████╗██║░╚═╝░██║██║░░██║╚█████╔╝██████╔╝ | |
-- ╚══════╝╚═╝░░░░░╚═╝╚═╝░░╚═╝░╚════╝░╚═════╝░ | |
-- | |
-- Open this script with 'Script Editor' on MacOS, then save it | |
-- inside /Applications as an 'Application', not 'Script'. | |
-- | |
-- Prerequisites: | |
-- | |
-- 1. There should be Emacs in your /Applications | |
-- If you downloaded emacs-plus from homebrew: | |
-- ln -s /usr/local/Cellar/emacs-plus@27/27.2/Emacs.app /Applications/Emacs.app | |
-- | |
-- 2. Make sure you have a /usr/local/bin/emacsclient | |
-- If you don't, update the script below to the correct path | |
-- | |
-- Akash Agrawal <[email protected]> | |
tell application "Finder" | |
try | |
log "checking for existing emacs client frames" | |
set frameVisible to do shell script "/usr/local/bin/emacsclient -n -e '(> (length (frame-list)) 1)'" | |
if frameVisible is not "t" then | |
log "server exists, attaching a new client frame" | |
do shell script "/usr/local/bin/emacsclient -c -n" | |
else | |
log frameVisible | |
log "server and window exists, bringing it to foreground" | |
end if | |
on error | |
log "starting server" | |
do shell script "/usr/local/bin/emacs --daemon" | |
do shell script "/usr/local/bin/emacsclient -c -n" | |
end try | |
tell application "Emacs" to activate | |
end tell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Complimentary shell helper to add an 'em' command if you're opening files from terminal. | |
# It will try to connect to a running emacs daemon. If not found, it starts one. | |
# | |
# Source this file from your ~/.zlogin, ~/.zshrc, ~/.bashrc etc, | |
# or add this code directly to those files. | |
# | |
# Example Usage: | |
# > em ~/.zshrc | |
# | |
# Akash Agrawal <[email protected]> | |
em () { | |
# Check if a frame(window) already exists | |
emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" | grep -q t | |
# If it doesn't create a new window (start server if not running) | |
# Else, use the same window to open the file. | |
if [ "$?" = "1" ]; then | |
emacsclient -c -n -a "" "$@" | |
else | |
emacsclient -n -a "" "$@" | |
fi | |
} |
@AtomicNess123 Kinda. To run M-x start-server
, you have to have already started emacs. This script allows you to open spotlight, enter something like 'emacs' or 'em' or whatever you name it, and open emacs in daemon mode with a visible frame.
If you close emacs (without killing server), opening emacs from spotlight again will just connect to the running instance, so it's gonna be fast.
Thanks, I think I get the idea :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does
M-x server-start
not accomplish the same thing as this script? I may be confused. Newbie here.