@kyanny's blog

My life. Opinions are my own.

Cocoa Emacs で環境変数を一発で設定する (~/.MacOSX/environment.plist 自動生成)

FAQ っていうか Info に書いてあった。手動で environment.plist ファイルを作る必要はない。

(emacs)Top:: > *Note Mac OS / GNUstep:: > Mac / GNUstep Basics に載っていた。 M-x info mEmacs [RET] で、 Mac OS / GNUstep -> Mac / GNUstep Basics の項目を読む。

要するに

$ /Applications/Emacs.app/Contents/MacOS/bin/mac-fix-env

というコマンドを叩けばいい。 ~/.MacOSX/environment.plist に環境変数 PATH の値を書き込んでくれる。 XML ファイルを自動で作ってくれるのでコピペいらず。

F.1.1 Grabbing environment variables
------------------------------------

Many programs which may run under Emacs like latex or man depend on the
settings of environment variables.  If Emacs is launched from the
shell, it will automatically inherit these environment variables and
its subprocesses will inherit them from it.  But if Emacs is launched
from the Finder it is not a descendant of any shell, so its environment
variables haven't been set which often causes the subprocesses it
launches to behave differently than they would when launched from the
shell.

   To solve this problem for Emacs, there are two solutions.  First is
to run, from the command line:

     .../Emacs.app/Contents/MacOS/bin/mac-fix-env

   This will pick up your environment settings and save them into a
special file `~/.MacOSX/environment.plist', which the desktop
environment will use to set the environment for all launched
applications.  The drawback of this method is it needs to be run again
whenever something changes.

   The other approach is to use the `ns-grabenv' command inside Emacs.
This function will run a subshell and copy its environment variables
into Emacs.

   Adding this line to your `~/.emacs' will grab the csh environment
whenever emacs runs under a window system.

     (if window-system (ns-grabenv))

   If you have a different shell you will have to give `ns-grabenv' some
arguments.  For zsh you would do this.

     (if window-system (ns-grabenv "/usr/bin/zsh"
                                "source /etc/zshenv"
                                "source ~/.zshenv"))

   The reason that `ns-grabenv' is not done by default is that it adds
up to a second or two to the Emacs startup time.

もうひとつの Emacs Lisp でやる方法も試してみたけど、うまくいってるのかどうかよくわからなかった。 http://twitter.com/ivey/status/1085951214 なんていうのも見つけて真似してみたけど、よくわからず。まぁどっちか出来てればちゃんと動くので、前者を採用で。