dotfiles/dotfiles/lib/shellenv/emacs.sh

119 lines
2.9 KiB
Bash
Raw Normal View History

# ATTENTION: Most of the functions in this file are part of a now
# deprecated system for starting emacs that made the $EDITOR
# environment variable work for git and other things
# This approach is no longer in use.
# alias emacs='_emacs -c -n '
# is_osx && alias emacs='cocoa_emacs'
# alias terminal_emacs='_emacs -t'
# is_ssh && emacs="terminal_emacs"
2014-12-15 18:20:40 -07:00
function kill_all_emacs {
emacs_pids | xargs kill -9
}
function edb {
\emacs --debug-init
}
function e {
2014-11-20 00:23:11 -07:00
[ -z "$*" ] && emacs || emacs_open -n "$@"
}
function cocoa_emacs {
2014-11-23 15:23:09 -07:00
reattach-to-user-namespace zsh -c '_emacs -c -n "$@"'
}
2014-12-27 03:53:47 -07:00
function _emacs_daemon {
local server_name="$(_emacs_server_file)"
if ! emacs_daemon_exists "$server_name"; then
2014-10-27 07:28:27 -06:00
echo "Starting emacs with server name '$server_name'"
2014-12-27 03:53:47 -07:00
command emacs "$@" --daemon="$server_name"
2014-10-27 07:28:27 -06:00
fi
2014-12-27 03:53:47 -07:00
}
function _emacs {
_emacs_daemon
emacsclient "$@" --server-file="$(_emacs_server_file)"
}
function _emacs_daemon_arguments {
_emacs_daemon "$@"
emacsclient -c -n --server-file="$(_emacs_server_file)"
}
function _emacs_server_file {
2014-11-20 00:23:11 -07:00
local server_name="global"
[ ! -z ${PER_DIRECTORY_EMACS+y} ] && server_name="$(_current_dot_directory)"
echo $server_name
}
function emacs_daemon_exists {
2014-10-27 07:28:27 -06:00
! test -z "$(ps aux | grep -v grep | grep -i "emacs.*--daemon=.*$1$")"
}
function _dot_directory {
echo $1 | sed "s:/:.:g"
}
2014-10-28 03:48:51 -06:00
function _current_dot_directory {
local directory="$(git rev-parse --show-toplevel 2> /dev/null || pwd)"
_dot_directory $directory
}
2014-11-04 07:22:20 -07:00
function existing_emacs {
# Return any existing emacs server file or the one that should
# be created if it doesn't exist.
local server_file="$(emacs_get_running_instances | head -n1)"
[ -z "$server_file" ] && server_file="$(_emacs_server_file)"
echo $server_file
}
function emacs_make_frame_if_none_exists {
2014-12-20 05:57:23 -07:00
execute_elisp '(make-frame-if-none-exists-and-focus)' > /dev/null
focus_emacs
}
2014-12-20 05:57:23 -07:00
function emacs_ensure_running_with_frame {
test -z "$(emacs_get_running_instances)" && emacs || \
emacs_make_frame_if_none_exists "$(emacs_get_running_instances | head -n1)"
}
function execute_elisp {
2014-12-20 05:57:23 -07:00
_emacs -e "$1"
}
function focus_emacs {
is_osx && osascript -e 'tell application "Emacs" to activate'
}
2014-12-15 18:20:40 -07:00
function emacs_pids {
local flags=''
is_osx && flags='-i'
pgrep "$flags" emacs
}
function emacs_get_running_instances {
2014-12-15 18:20:40 -07:00
emacs_pids | xargs ps -o command -p | egrep -o " --daemon=(.*)" | awk -F= '{print $2}' | sed 's/\^J3,4\^J//'
}
function emacs_open {
if ! emacs_daemon_exists; then
emacs
fi
2014-12-20 05:57:23 -07:00
emacs_make_frame_if_none_exists
[ ! -z "$*" ] && _emacs "$@"
focus_emacs
}
2014-11-22 06:09:53 -07:00
function time_emacs {
time \emacs --daemon="timing" && emacsclient -e "(kill-emacs)" --server-file="timing"
}
2014-11-04 07:22:20 -07:00
# Make emacs the default editor.
export EDITOR="$HOME/.lib/editor.sh"
2014-11-04 07:22:20 -07:00
export VISUAL="$EDITOR"
export GIT_EDITOR="$EDITOR"
2015-06-15 16:01:04 -06:00
export ALTERNATE_EDITOR=""