emacs.plugin.zsh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # Path to custom emacsclient launcher
  14. export EMACS_PLUGIN_LAUNCHER="${0:A:h}/emacsclient.sh"
  15. # set EDITOR if not already defined.
  16. export EDITOR="${EDITOR:-${EMACS_PLUGIN_LAUNCHER}}"
  17. alias emacs="$EMACS_PLUGIN_LAUNCHER --no-wait"
  18. alias e=emacs
  19. # open terminal emacsclient
  20. alias te="$EMACS_PLUGIN_LAUNCHER -nw"
  21. # same than M-x eval but from outside Emacs.
  22. alias eeval="$EMACS_PLUGIN_LAUNCHER --eval"
  23. # create a new X frame
  24. alias eframe='emacsclient --alternate-editor "" --create-frame'
  25. # Emacs ANSI Term tracking
  26. if [[ -n "$INSIDE_EMACS" ]]; then
  27. chpwd_emacs() { print -P "\033AnSiTc %d"; }
  28. print -P "\033AnSiTc %d" # Track current working directory
  29. print -P "\033AnSiTu %n" # Track username
  30. # add chpwd hook
  31. autoload -Uz add-zsh-hook
  32. add-zsh-hook chpwd chpwd_emacs
  33. fi
  34. # Write to standard output the path to the file
  35. # opened in the current buffer.
  36. function efile {
  37. local cmd="(buffer-file-name (window-buffer))"
  38. local file="$("$EMACS_PLUGIN_LAUNCHER" --eval "$cmd" | tr -d \")"
  39. if [[ -z "$file" ]]; then
  40. echo "Can't deduce current buffer filename." >&2
  41. return 1
  42. fi
  43. echo "$file"
  44. }
  45. # Write to standard output the directory of the file
  46. # opened in the the current buffer
  47. function ecd {
  48. local file
  49. file="$(efile)" || return $?
  50. echo "${file:h}"
  51. }