emacs.plugin.zsh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Emacs 23 daemon capability is a killing feature.
  2. # One emacs process handles all your frames whether
  3. # you use a frame opened in a terminal via a ssh connection or X frames
  4. # opened on the same host.
  5. # Benefits are multiple
  6. # - You don't have the cost of starting Emacs all the time anymore
  7. # - Opening a file is as fast as Emacs does not have anything else to do.
  8. # - You can share opened buffered across opened frames.
  9. # - Configuration changes made at runtime are applied to all frames.
  10. # Require emacs version to be minimum 24
  11. autoload -Uz is-at-least
  12. is-at-least 24 "${${(Az)"$(emacsclient --version 2>/dev/null)"}[2]}" || return 0
  13. # Handle $0 according to the standard:
  14. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  15. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  16. 0="${${(M)0:#/*}:-$PWD/$0}"
  17. # Path to custom emacsclient launcher
  18. export EMACS_PLUGIN_LAUNCHER="${0:A:h}/emacsclient.sh"
  19. # set EDITOR if not already defined.
  20. export EDITOR="${EDITOR:-${EMACS_PLUGIN_LAUNCHER}}"
  21. alias emacs="$EMACS_PLUGIN_LAUNCHER --no-wait"
  22. alias e=emacs
  23. # open terminal emacsclient
  24. alias te="$EMACS_PLUGIN_LAUNCHER -nw"
  25. # same than M-x eval but from outside Emacs.
  26. alias eeval="$EMACS_PLUGIN_LAUNCHER --eval"
  27. # create a new X frame
  28. alias eframe='emacsclient --alternate-editor "" --create-frame'
  29. # Emacs ANSI Term tracking
  30. if [[ -n "$INSIDE_EMACS" ]]; then
  31. chpwd_emacs() { print -P "\033AnSiTc %d"; }
  32. print -P "\033AnSiTc %d" # Track current working directory
  33. print -P "\033AnSiTu %n" # Track username
  34. # add chpwd hook
  35. autoload -Uz add-zsh-hook
  36. add-zsh-hook chpwd chpwd_emacs
  37. fi
  38. # Write to standard output the path to the file
  39. # opened in the current buffer.
  40. function efile {
  41. local cmd="(buffer-file-name (window-buffer))"
  42. local file="$("$EMACS_PLUGIN_LAUNCHER" --eval "$cmd" | tr -d \")"
  43. if [[ -z "$file" ]]; then
  44. echo "Can't deduce current buffer filename." >&2
  45. return 1
  46. fi
  47. echo "$file"
  48. }
  49. # Write to standard output the directory of the file
  50. # opened in the the current buffer
  51. function ecd {
  52. local file
  53. file="$(efile)" || return $?
  54. echo "${file:h}"
  55. }