Skip to content

Instantly share code, notes, and snippets.

@mahmoudimus
Forked from ca7023/emacs.sh
Last active October 2, 2022 18:03
Show Gist options
  • Save mahmoudimus/7450b9f815672d6adb7cf4ba8338f288 to your computer and use it in GitHub Desktop.
Save mahmoudimus/7450b9f815672d6adb7cf4ba8338f288 to your computer and use it in GitHub Desktop.
Run Emacs.app as Client/Server, for someone who likes GUI emacs and emacsclient to speed up start-up time.
#!/bin/bash
BG_RED=`tput setaf 1`
BG_GREEN=`tput setaf 2`
BOLD=`tput bold`
RESET=`tput sgr0`
EMACS='/Applications/Emacs.app'
EMACS_CLIENT='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
DEFAULT_EVAL='(switch-to-buffer "*scratch*")'
DEFAULT_ARGS="-e"
NO_WAIT='-n'
function run_client(){
if [ $# -ne 0 ]
then
${EMACS_CLIENT} ${NO_WAIT} $@
else
${EMACS_CLIENT} ${NO_WAIT} ${DEFAULT_ARGS} "${DEFAULT_EVAL}" &> /dev/null
fi
}
echo -e "Checking Emacs server status...\c"
if pgrep Emacs &> /dev/null
then
echo "${BOLD}${BG_GREEN}Active${RESET}"
echo -e "Connecting...\c"
run_client $*
echo "${BOLD}${BG_GREEN}DONE${RESET}"
else
echo "${BOLD}${BG_RED}Inactive${RESET}"
echo -e "Emacs server is starting...\c"
open -a ${EMACS}
echo "${BOLD}${BG_GREEN}DONE${RESET}"
echo -e "Trying connecting...\c"
until run_client $* &> /dev/null;[ $? -eq 0 ]
do
sleep 1
done
echo "${BOLD}${BG_GREEN}DONE${RESET}"
fi

MacOS Service

Installs dependencies automatically by means of package.el. Requires Emacs 24.

Usage

Clone the repo:

git clone [email protected]:mahmoudimus/emacs.d.git ~/.emacs.d

Emacs Setup

Installation

My preferred installation on MacOS:

brew cask install emacs

This installs:

App '/Applications/Emacs.app'.
Binary '/usr/local/bin/emacs'.
Binary '/usr/local/bin/ebrowse'.
Binary '/usr/local/bin/emacsclient'.
Binary '/usr/local/bin/etags'.

GOTCHA: make sure these are on the system path and have priority over the system's Emacs.

MacOS Service

Create ~/Library/LaunchAgents/my.emacs.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>my.Emacs</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Applications/Emacs.app/Contents/MacOS/emacs</string>
      <string>--fg-daemon</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

The service can then be loaded via:

launchctl load -w ~/Library/LaunchAgents/my.emacs.plist

Or unloaded via:

launchctl unload -w ~/Library/LaunchAgents/my.emacs.plist

Script for running EmacsClient

Placed in ~/bin/run-emacsclient:

#!/usr/bin/env bash

if [ -z "$EMACSCLIENT_OPTS" ]; then
    EMACSCLIENT_OPTS="-nc"
fi    

if [ $# -eq 0 ]; then
    COMMAND='/usr/local/bin/emacsclient '$EMACSCLIENT_OPTS' -e "(if (display-graphic-p) (x-focus-frame nil))"'
else
    COMMAND='/usr/local/bin/emacsclient '$EMACSCLIENT_OPTS' "$@"'
fi

if [ -z "$(shopt | grep login_shell)" ]; then
    echo "$COMMAND" | exec bash --login -s "$@"
else
    eval "exec $COMMAND"        
fi

Also place this in ~/bin/run-emacsclient-cli:

#!/usr/bin/env bash

export EMACSCLIENT_OPTS='-t'
exec "$HOME/bin/run-emacsclient" "$@"

Bash Settings

# Default editor
export EDITOR="$HOME/bin/run-emacsclient-cli"
export VISUAL="$EDITOR"
export ALTERNATE_EDITOR="vim"

# Editor aliases
alias e="$HOME/bin/run-emacsclient-cli"
alias ew="$HOME/bin/run-emacsclient"
alias notes='$HOME/bin/run-emacsclient -e "(if (display-graphic-p) (x-focus-frame nil))" -e "(deft)" | grep -v nil'

MacOS Apps

For executing the emacsclient in GUI mode as a regular app (via Spotlight search, from the Dock, etc), packaged apps are provided in [./apps/MacOS Apps.tar.gz](./apps/MacOS Apps.tar.gz).

These are created and can be edited with the MacOS Automator and can be edited by it.

They do nothing more than to execute $HOME/bin/run-emacsclient (script described above) with certain parameters. The path to the script can be edited via Automator.

@mahmoudimus
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment