From 4d707d59e3a7fdaffaff12b8c795f6e3fbcd02db Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 21 Oct 2014 15:20:43 -0700 Subject: [PATCH] emacs cocoa stuff, set-path launch agent, as_user function. --- dotfiles/emacs.d | 2 +- dotfiles/lib/cocoa-emacs.scpt | 17 ----------------- dotfiles/lib/shellrc.sh | 1 + dotfiles/lib/shellrc/aliases.sh | 5 +++-- dotfiles/lib/shellrc/exports.sh | 2 +- dotfiles/lib/shellrc/functions.sh | 28 ++++++++++++++++++++++++++++ resources/set-path.plist | 8 ++++---- tasks/osx.py | 17 ++++++++++------- 8 files changed, 48 insertions(+), 32 deletions(-) delete mode 100644 dotfiles/lib/cocoa-emacs.scpt diff --git a/dotfiles/emacs.d b/dotfiles/emacs.d index ad458bce..3a01d4be 160000 --- a/dotfiles/emacs.d +++ b/dotfiles/emacs.d @@ -1 +1 @@ -Subproject commit ad458bced3a0cd204671c8e88c3f0f1e445eaf33 +Subproject commit 3a01d4be1d0c953cd1c4f293e8d0b32b543a2131 diff --git a/dotfiles/lib/cocoa-emacs.scpt b/dotfiles/lib/cocoa-emacs.scpt deleted file mode 100644 index 6d2a8ce0..00000000 --- a/dotfiles/lib/cocoa-emacs.scpt +++ /dev/null @@ -1,17 +0,0 @@ -tell application "Terminal" - try - -- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't - set frameVisible to do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'" - if frameVisible is not "t" then - -- there is a not a visible frame, launch one - do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n" - end if - on error - -- daemon is not running, start the daemon and open a frame - do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon" - do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n" - end try -end tell - --- bring the visible frame to the front -tell application "Emacs" to activate \ No newline at end of file diff --git a/dotfiles/lib/shellrc.sh b/dotfiles/lib/shellrc.sh index 5988a4a0..0e29a808 100644 --- a/dotfiles/lib/shellrc.sh +++ b/dotfiles/lib/shellrc.sh @@ -9,6 +9,7 @@ function add_to_back_of_path { add_to_back_of_path "$HOME/.local/lib/python2.6/site-packages" hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix coreutils)/libexec/gnubin" add_to_front_of_path "/usr/local/bin" +hash brew 2>/dev/null && add_to_front_of_path "$(brew --prefix emacs)/libexec/gnubin" for filename in ~/.lib/shellrc/*; do source $filename diff --git a/dotfiles/lib/shellrc/aliases.sh b/dotfiles/lib/shellrc/aliases.sh index 1240c01b..f2d0dc46 100644 --- a/dotfiles/lib/shellrc/aliases.sh +++ b/dotfiles/lib/shellrc/aliases.sh @@ -1,5 +1,6 @@ -alias emacs="emacsclient -t" -alias xemacs="\emacs --daemon > /dev/null 2&> /dev/null; emacsclient -c &" +alias emacs="emacsclient -c -n" +alias temacs="emacsclient -t" +alias cemacs="emacsclient -c -n" alias tmux="tmux -2" alias reload_tmux_conf="tmux source-file .tmux.conf" alias tmux_cb_to_remote_cb='tmux saveb - | linux_nc_paste_to_remote_clipboard' diff --git a/dotfiles/lib/shellrc/exports.sh b/dotfiles/lib/shellrc/exports.sh index 8de4ff6d..d3d613ab 100644 --- a/dotfiles/lib/shellrc/exports.sh +++ b/dotfiles/lib/shellrc/exports.sh @@ -4,7 +4,7 @@ elif infocmp xterm-256color >/dev/null 2>&1; then export TERM=xterm-256color fi # Make emacs the default editor. -export EDITOR="emacsclient" +export EDITOR="emacsclient -cn" export ALTERNATE_EDITOR="" export VISUAL="emacsclient" diff --git a/dotfiles/lib/shellrc/functions.sh b/dotfiles/lib/shellrc/functions.sh index 659b26c7..e92c4f57 100644 --- a/dotfiles/lib/shellrc/functions.sh +++ b/dotfiles/lib/shellrc/functions.sh @@ -265,3 +265,31 @@ EOF function dirty_talk() { while true; do talk_dirty_to_me | tee >(cat) | say; done } + +function as_user { + local user="$1" + local user_pid=$(ps -axj | awk "/^$user / {print \$2;exit}") + local command="sudo /bin/launchctl bsexec $user_pid sudo -u '$user' $2" + echo "Running:" + echo "$command" + eval $command +} + +function as_current_user { + as_user "$(whoami)" "$*" +} + +function reload_user_agent { + as_current_user /bin/launchctl unload "$1" + as_current_user /bin/launchctl load "$1" +} + + +function reload_root_agent { + as_user 'root' "/bin/launchctl unload '$1'" + as_user 'root' "/bin/launchctl load '$1'" +} + +function ec { + emacsclient -n $1 > /dev/null +} diff --git a/resources/set-path.plist b/resources/set-path.plist index 5c7d02aa..abcec6a2 100644 --- a/resources/set-path.plist +++ b/resources/set-path.plist @@ -3,14 +3,14 @@ Label - set-path + com.example.hello ProgramArguments zsh -c - 'source ~/.zshrc; launchctl setenv PATH $PATH' + source ~/.zshrc && echo password | sudo -Sv && as_current_user /bin/launchctl setenv PATH $PATH - KeepAlive + RunAtLoad - \ No newline at end of file + diff --git a/tasks/osx.py b/tasks/osx.py index 19786e61..7efc2940 100644 --- a/tasks/osx.py +++ b/tasks/osx.py @@ -64,15 +64,18 @@ def brew_install(ctx): @ctask def setup_cocoa_emacs(ctx): - if not os.path.exists('/Applications/emacs'): - ctx.run('ln -s $(brew --prefix emacs) /Applications/emacs', hide=True) + if not os.path.exists('/Applications/Emacs.app'): + ctx.run('ln -s $(brew --prefix emacs)/Emacs.app /Applications/Emacs.app', hide=True) + launch_agent_dir = os.path.expanduser('~/Library/LaunchAgents/') filename = 'set-path.plist' - util.ensure_path_exists(launch_agent_dir) - ctx.run('cp {0} {1}'.format( - os.path.join(util.RESOURCES_DIRECTORY, filename), - os.path.join(launch_agent_dir, filename) - )) + + source = os.path.join(util.RESOURCES_DIRECTORY, filename) + destination = os.path.join(launch_agent_dir, filename) + + if os.path.exists(source) and not os.path.exists(destination): + util.ensure_path_exists(launch_agent_dir) + ctx.run('ln -s {0} {1}'.format(source, destination)) @ctask