如何让 Emacs 识别用于编译的 bash 环境变量? [英] How do I make Emacs recognize bash environment variables for compilation?

查看:16
本文介绍了如何让 Emacs 识别用于编译的 bash 环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Emacs 的编译模式编译 u-boot,看起来 Emacs 不知道如何找到 bash 环境变量.即使我设置了它们,并且可以通过 Emacs shell 仿真进行编译,编译模式仍然会尝试编译,就好像它们不存在一样.

I'm trying to compile u-boot via Emacs' compilation mode, and it looks like Emacs doesn't know how to find bash environment variables. Even though I set them, and can compile via Emacs shell emulation, compilation mode still tries to compile as if they aren't there.

我需要做什么才能使其更具环保意识?

What do I need to do to make it more environment conscious?

推荐答案

您可以尝试在 .emacs 中添加类似的内容:

You can try adding something like to your .emacs:

(let ((path (shell-command-to-string ". ~/.bashrc; echo -n $PATH")))
  (setenv "PATH" path)
  (setq exec-path 
        (append
         (split-string-and-unquote path ":")
         exec-path)))

根据您是在 .bash_profile 还是 .bashrc 中设置环境变量,您可能需要稍微调整此代码段.该示例针对 PATH 变量,它有点特殊(因为您还必须在 Emacs 中设置 exec-path),但可以扩展以适用于任意变量——你可以有一个必须从 .bashrc 读取并设置到 Emacs 中的变量列表.

Depending on whether you've set the env variables in .bash_profile or .bashrc you might need to slightly adjust this snippet. The example is for the PATH variable, which is a bit more special (since you have to set exec-path in Emacs as well), but can be extended to work for arbitrary variables - you could have a list of variables that have to be read from .bashrc and set into Emacs.

我不确定您使用的是 OS X 还是 GNU/Linux.在 Linux 中从 GUI 的菜单栏启动 Emacs 通常会导致 Emacs 与从命令行启动的 Emacs 没有相同的 PATH.这个问题可以追溯到第一个 xdm Xsession 脚本,虽然它们很容易修复(基本上使用一个 Xsessionwrapper 脚本,它执行 exec $SHELL -c Xsession 所以 shell 在运行用户的 Xsession 之前运行),在很长一段时间内没有人愿意这样做(我怀疑有人会这样做).据我所知,该问题甚至存在于 kdmgdm 等现代 xdm 后代中.

I'm not sure whether you're using OS X or GNU/Linux. Starting Emacs from the GUI's menu-bar in Linux will typically result in an Emacs that does not have the same PATH as one launched from the command line. This problem dates back to the first xdm Xsession scripts, and while they are fairly easy to fix (basically use an Xsessionwrapper script that does exec $SHELL -c Xsession so the shell gets run before running the user's Xsession), nobody has bother to do so in a very long time (and I doubt that anyone will). As far as I know the problem is present even into moder xdm descendants such as kdm and gdm.

在 OS X 上,处理 env 变量完全是另一个问题,要获取 ENV 变量,您通常必须像这样从命令行运行 Emacs /Applications/Emacs.app/Contents/MacOS/Emacs 或玩 ~/.MacOSX/environment.plist.不过,我提供的代码片段应该涵盖了这两种情况.

On OS X the handling of the env variables is another problem entirely and to get your ENV variables you typically have to run Emacs from the command line like this /Applications/Emacs.app/Contents/MacOS/Emacs or play with ~/.MacOSX/environment.plist. The code snippet I've provided should cover you in both cases though.

更新

最近,exec-path-from-shell 扩展使这个过程变得更容易.它以大致相同的方式设置 emacs $PATH,但使用扩展通常比自己破解解决方案更可取.

Recently this process was made easier by the exec-path-from-shell extension. It sets the emacs $PATH in more or less the same manner, but using an extension is generally preferable to hacking the solution yourself.

这篇关于如何让 Emacs 识别用于编译的 bash 环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆