jimeh / build-emacs-for-macos

Somewhat hacky script to automate building of Emac.app on macOS.
484 stars 50 forks source link

Improve CLI integration #41

Closed jimeh closed 3 years ago

jimeh commented 3 years ago

I believe CLI integration to get a emacs command for use in terminals can be improved over the current alias method suggested in the readme.

Essentially, add a shell script called emacs into Emacs.app/Contents/MacOS/bin, where emacsclient is already located. The script would need to resolve it's own absolute path on disk, then execute Emacs from the parent folder passing in -nw arguments.

Then making emacs and emacsclient available in the terminal would simply be a matter of adding the bin path:

if [ -d "/Applications/Emacs.app/Contents/MacOS/bin" ]; then
  export PATH="/Applications/Emacs.app/Contents/MacOS/bin:$PATH"
fi

As for the emacs shell script in question, I believe this should work:

#!/usr/bin/env bash

resolve_link() {
  "$(type -p greadlink readlink | head -1)" "$1"
}

abs_dirname() {
  local path="$1"
  local name
  local cwd
  cwd="$(pwd)"

  while [ -n "$path" ]; do
    cd "${path%/*}" || exit 1
    name="${path##*/}"
    path="$(resolve_link "$name" || true)"
  done

  pwd
  cd "$cwd" || exit 1
}

exec "$(dirname "$(abs_dirname "$0")")/Emacs" -nw "$@"

I will need to perform a bit of testing to ensure such a wrapper script doesn't introduce any issues before adding it, but I believe it'll work just fine :)