2014-11-18 02:50:59 -08:00
|
|
|
alias emacs='_emacs -c -n '
|
2014-10-28 03:37:53 -07:00
|
|
|
is_osx && alias emacs='cocoa_emacs'
|
|
|
|
alias terminal_emacs='_emacs -t'
|
2014-11-14 03:13:43 -08:00
|
|
|
is_ssh && emacs="terminal_emacs"
|
2014-10-27 01:55:02 -07:00
|
|
|
|
2014-11-19 23:15:16 -08:00
|
|
|
function e {
|
2014-11-19 23:23:11 -08:00
|
|
|
[ -z "$*" ] && emacs || emacs_open -n "$@"
|
2014-11-19 23:15:16 -08:00
|
|
|
}
|
|
|
|
|
2014-10-28 03:37:53 -07:00
|
|
|
function cocoa_emacs {
|
2014-11-23 14:23:09 -08:00
|
|
|
reattach-to-user-namespace zsh -c '_emacs -c -n "$@"'
|
2014-10-28 03:37:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 01:55:02 -07:00
|
|
|
function _emacs {
|
2014-11-18 02:50:59 -08:00
|
|
|
local server_name="$(_emacs_server_file)"
|
|
|
|
if ! emacs_daemon_exists "$server_name"; then
|
2014-10-27 13:28:27 +00:00
|
|
|
echo "Starting emacs with server name '$server_name'"
|
2014-10-28 03:37:53 -07:00
|
|
|
command emacs --daemon="$server_name"
|
2014-10-27 13:28:27 +00:00
|
|
|
fi
|
2014-11-18 02:50:59 -08:00
|
|
|
emacsclient "$@" --server-file=$server_name
|
|
|
|
}
|
|
|
|
|
|
|
|
function _emacs_server_file {
|
2014-11-19 23:23:11 -08:00
|
|
|
local server_name="global"
|
|
|
|
[ ! -z ${PER_DIRECTORY_EMACS+y} ] && server_name="$(_current_dot_directory)"
|
2014-11-18 02:50:59 -08:00
|
|
|
echo $server_name
|
2014-10-27 01:55:02 -07:00
|
|
|
}
|
|
|
|
|
2014-11-18 02:50:59 -08:00
|
|
|
function emacs_daemon_exists {
|
2014-10-27 13:28:27 +00:00
|
|
|
! test -z "$(ps aux | grep -v grep | grep -i "emacs.*--daemon=.*$1$")"
|
|
|
|
}
|
|
|
|
|
2014-10-27 02:20:32 -07:00
|
|
|
function _dot_directory {
|
2014-10-27 03:21:33 -07:00
|
|
|
echo $1 | sed "s:/:.:g"
|
2014-10-27 01:55:02 -07:00
|
|
|
}
|
2014-10-28 02:48:51 -07:00
|
|
|
|
|
|
|
function _current_dot_directory {
|
|
|
|
local directory="$(git rev-parse --show-toplevel 2> /dev/null || pwd)"
|
|
|
|
_dot_directory $directory
|
|
|
|
}
|
2014-11-04 06:22:20 -08:00
|
|
|
|
2014-11-18 02:50:59 -08:00
|
|
|
function existing_emacs {
|
|
|
|
# Return any existing emacs server file or the one that should
|
|
|
|
# be created if it doesn't exist.
|
2014-11-19 16:48:16 -08:00
|
|
|
local server_file="$(emacs_get_running_instances | head -n1)"
|
2014-11-18 02:50:59 -08:00
|
|
|
[ -z "$server_file" ] && server_file="$(_emacs_server_file)"
|
|
|
|
echo $server_file
|
|
|
|
}
|
|
|
|
|
|
|
|
function emacs_make_frame_if_none_exists {
|
2014-11-20 22:14:12 -08:00
|
|
|
emacsclient -e '(make-frame-if-none-exists-and-focus)' --server-file=$1 > /dev/null
|
2014-11-18 16:41:30 -08:00
|
|
|
focus_emacs
|
2014-11-18 02:50:59 -08:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:41:30 -08:00
|
|
|
function focus_emacs {
|
2014-11-19 15:26:04 -08:00
|
|
|
is_osx && osascript -e 'tell application "Emacs" to activate'
|
2014-11-18 16:41:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function emacs_get_running_instances {
|
2014-12-10 14:36:23 -08:00
|
|
|
local command='pgrep'
|
|
|
|
is_osx && command='pgrep -i'
|
|
|
|
$command emacs | xargs ps -o command -p | egrep -o " --daemon=(.*)" | awk -F= '{print $2}' | sed 's/\^J3,4\^J//'
|
2014-11-18 02:50:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function emacs_open {
|
2014-11-18 16:41:30 -08:00
|
|
|
if ! emacs_daemon_exists; then
|
|
|
|
emacs
|
2014-11-18 02:50:59 -08:00
|
|
|
fi
|
2014-11-18 16:41:30 -08:00
|
|
|
local server_file="$(emacs_get_running_instances | head -n1)"
|
2014-11-18 02:50:59 -08:00
|
|
|
emacs_make_frame_if_none_exists $server_file
|
2014-11-19 22:55:02 -08:00
|
|
|
[ ! -z "$*" ] && emacsclient "$@" --server-file="$server_file"
|
2014-12-10 14:36:23 -08:00
|
|
|
is_osx && focus_emacs
|
2014-11-18 02:50:59 -08:00
|
|
|
}
|
|
|
|
|
2014-11-22 05:09:53 -08:00
|
|
|
function time_emacs {
|
|
|
|
time \emacs --daemon="timing" && emacsclient -e "(kill-emacs)" --server-file="timing"
|
|
|
|
}
|
|
|
|
|
2014-11-04 06:22:20 -08:00
|
|
|
# Make emacs the default editor.
|
2014-11-20 22:14:12 -08:00
|
|
|
export EDITOR='emacs_open'
|
2014-11-04 06:22:20 -08:00
|
|
|
export VISUAL="$EDITOR"
|
2014-11-20 22:14:12 -08:00
|
|
|
# This is ugly as sin but I can't figure out how else to do it.
|
2014-12-10 14:36:23 -08:00
|
|
|
export GIT_EDITOR="zsh -c 'emacs_open '"'"$@"; return 0'
|