Emacs server can sort of open files from finder.... this is hard though. Also emacs_open command.

This commit is contained in:
2014-11-18 02:50:59 -08:00
parent 9f57705921
commit 510f32b5d6
45 changed files with 509 additions and 141 deletions

View File

@@ -1,4 +1,4 @@
alias emacs='_emacs -c -n'
alias emacs='_emacs -c -n '
is_osx && alias emacs='cocoa_emacs'
alias terminal_emacs='_emacs -t'
alias ec='_emacs -n '
@@ -10,16 +10,21 @@ function cocoa_emacs {
}
function _emacs {
local server_name="$GLOBAL_EMACS"
[ -z $GLOBAL_EMACS ] && server_name="$(_current_dot_directory)"
if ! _emacs_daemon_exists "$server_name"; then
local server_name="$(_emacs_server_file)"
if ! emacs_daemon_exists "$server_name"; then
echo "Starting emacs with server name '$server_name'"
command emacs --daemon="$server_name"
fi
emacsclient $* --server-file=$server_name
emacsclient "$@" --server-file=$server_name
}
function _emacs_daemon_exists {
function _emacs_server_file {
local server_name="$GLOBAL_EMACS"
[ -z $GLOBAL_EMACS ] && server_name="$(_current_dot_directory)"
echo $server_name
}
function emacs_daemon_exists {
! test -z "$(ps aux | grep -v grep | grep -i "emacs.*--daemon=.*$1$")"
}
@@ -32,6 +37,32 @@ function _current_dot_directory {
_dot_directory $directory
}
function existing_emacs {
# Return any existing emacs server file or the one that should
# be created if it doesn't exist.
local server_file="$(\ls ~/.emacs.d/server | head -n1)"
[ -z "$server_file" ] && server_file="$(_emacs_server_file)"
echo $server_file
}
function emacs_make_frame_if_none_exists {
emacsclient -e '(make-frame-if-none-exists)' --server-file=$1
}
function get_running_emacs_instances {
pgrep -i emacs | xargs ps -o command -p | egrep -o " --daemon=(.*)" | awk -F= '{print $2}' | sed 's/\^J3,4\^J//'
}
function emacs_open {
local server_file="$(get_running_emacs_instances | head -n1)"
if [ -z $server_file ]; then
_emacs -c -n "$@"
return
fi
emacs_make_frame_if_none_exists $server_file
[ ! -z "$@" ] && emacsclient "$@" -n --server-file="$server_file"
}
# Make emacs the default editor.
export EDITOR="$(which emacsclient) -n -s "
export ALTERNATE_EDITOR=""

View File

@@ -83,3 +83,64 @@ function set_osx_hostname() {
sudo scutil --set LocalHostName $new_hostname
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string $new_hostname
}
function get_bundle_identifier() {
defaults read "$1/Contents/Info" CFBundleIdentifier
}
_BUDDY="/usr/libexec/PlistBuddy"
_PLIST="$HOME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
function create_filename_rule {
$_BUDDY -c "Add LSHandlers:0 dict" $_PLIST
$_BUDDY -c "Add LSHandlers:0:LSHandlerRoleAll string $1" $_PLIST
$_BUDDY -c "Add LSHandlers:0:LSHandlerContentTag string $2" $_PLIST
$_BUDDY -c "Add LSHandlers:0:LSHandlerContentTagClass string public.filename-extension" $_PLIST
$_BUDDY -c "Add LSHandlers:0:LSHandlerPreferredVersions dict" $_PLIST
$_BUDDY -c "Add LSHandlers:0:LSHandlerPreferredVersions:LSHandlerRoleAll string -" $_PLIST
}
function set_application_for_file_extension() {
local PLIST="$HOME/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
# the key to match with the desired value
KEY="LSHandlerContentType"
# the value for which we'll replace the handler
VALUE="${1-public.plain-text}"
# the new handler for all roles
HANDLER="${1}"
$_BUDDY -c 'Print "LSHandlers"' $_PLIST >/dev/null 2>&1
if [[ $? -ne 0 ]] ; then
echo "There is no LSHandlers entry in $_PLIST" >&2
exit 1
fi
declare -i I=0
while [ true ] ; do
$_BUDDY -c "Print LSHandlers:$I" $_PLIST >/dev/null 2>&1
[[ $? -eq 0 ]] || { echo "Finished, no $VALUE found, setting it to $HANDLER" ; create_entry ; exit ; }
OUT="$( $_BUDDY -c "Print 'LSHandlers:$I:$KEY'" $_PLIST 2>/dev/null )"
if [[ $? -ne 0 ]] ; then
I=$I+1
continue
fi
CONTENT=$( echo "$OUT" )
if [[ $CONTENT = $VALUE ]] ; then
echo "Replacing $CONTENT handler with $HANDLER"
$_BUDDY -c "Delete 'LSHandlers:$I'" $_PLIST
create_entry
exit
else
I=$I+1
fi
done
}
function reload_preferences {
killall -u $(whoami) cfprefsd
}

View File

@@ -129,14 +129,10 @@ function separator {
export JOB_COUNT_COLOR="$fg[blue]"
prompt_tomorrow_colors
# For reasons which are currently beyond me,
# For reasons which are currently beyond me, it is not possible to use
# $? in PROMPT which is why the second line is so strangely
# constructed.
export PROMPT='⚡ % $(print_with_color "%n" "$USERNAME_COLOR") $(separator "at") $(print_with_color "`hostname -s`" "$HOSTNAME_COLOR") $(separator "in") $(print_with_color "`current_directory`" "$CURRENT_DIRECTORY_COLOR")$(git_prompt_info)
$(colored_job_count)%(?.$(print_with_color "$(command_line_character) " $PROMPT_CHAR_SUCCESS).$(print_with_color "$(command_line_character) " $PROMPT_CHAR_ERROR)) '
PS2='(%_) '
case "$TERM" in
dumb)
export PROMPT='> '
;;
esac