I'm trying to get a compile command (rake cucumber) to run with a specific ruby version on my Mac OS X system, I use rvm to do this currently in the terminal. My ~/.MacOSX/environment.plist has the correct path in it, but emacs insists on prepending to this path and therefore making it useless. I've also tried:
(when (equal system-type 'darwin)
(setenv "PATH" (concat "/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin:/Users/fearoffish/.rvm/bin"))
(push "/Users/fearoffish/.rvm/bin" exec-path)
(push "/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin" exec-path)
(push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin" exec-path)
(push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin" exec-path)
(push "/Users/fearoffish/.rvm/bin" exec-path))
It was the desperate attempt of an emacs beginner to get what I wanted. It still prepends in front of it, so my path ends up being:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin
I don't want /usr/bin and others prepending, I want my path first and the emacs prepended path to be at the end, I reckon this would fix my problem.
I test this by simply opening Aquamacs and running meta-x compile and then echo $PATH.
Any ideas?
A small modification to the solution by sanityinc (couldn't find a way to enter it in the comments above -- is that just me?)
I use -l option to the shell to force a login shell (which reads .profile or .bash_profile), rather than an interactive shell (which only reads .bashrc).
I do some string trimming on the returned path (as inspection shows a newline sneaking in).
Modified code:
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell
(replace-regexp-in-string "[[:space:]\n]*$" ""
(shell-command-to-string "$SHELL -l -c 'echo $PATH'"))))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
(when (equal system-type 'darwin) (set-exec-path-from-shell-PATH))
Everyone seems to have misunderstood the original issue: the path is already setup correctly in Emacs, and the correct path is already passed to the shell started by the compile command! So what gives? Here is the answer:
In MacOS X, there is a small tool called path_helper(1). It is called by default from /etc/profile, which is executed by Bash on shell startup. When you start a compilation from Emacs, it launches a shell (which by default is Bash on MacOS X), and therefore executes this path_helper tool. And here comes the key point: path_helper rearranges your path, moving the standard directories like /usr/bin in front of your custom added directories, no matter where you originally added them. Try this yourself by opening a shell and first having a look at what PATH is, and then execute /usr/lib/path_helper and have look at the resulting PATH!
The brute force solution for you might be to simply comment out the call to path_helper in /etc/profile. Note however that then you won't automatically get the paths in /etc/paths.d setup by path_helper, which is the tool's main purpose.
I don't have a Mac, so I cannot test this directly, but this can all be found in the *info* page Interactive Inferior Shell.
When you start a shell in Emacs, the process that gets spawned is the program in the Emacs variable explicit-shell-file-name (and if that is nil, the environment variables ESHELL and SHELL are used).
It then sends the contents of ~/.emacs_*shellname* (e.g. if your shell is csh, then ~/.emacs_csh would be sent over. Also, the appropriate .rc files for csh program is sourced, so you can update that as well (in my case .cshrc). Additionally, you can wrap customizations in the .rc file with a check for the environment variable INSIDE_EMACS (which which Emacs sets before it runs a shell).
You need to update those files to change the path in the shell, not the Emacs variable exec-path. exec-path - which is just a list of directories Emacs uses to find executable programs. The behavior of the executables are not affected by changes to exec-path.
I find the environment.plist scheme on Macs pretty ugly, so I use the following snippet, which assumes you want Emacs to use the same PATH that you see in your Terminal.app:
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell (shell-command-to-string "$SHELL -i -c 'echo $PATH'")))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
(This works for me in Emacs 23; haven't tried it in other versions, but I'd expect it to work.)
try this maybe. replace path string with yours.
(add-to-list 'load-path "~/opt/swank-clojure/src/emacs")
As far as I observed, Emacs takes the path variable from the shell it is launched from, so one solution is to change $PATH in the shell before you launch Emacs.
One other approach I used, which is more flexible, is to use a Makefile and append a "source ~/script_that_set_path" in front of each make commands you have.
I have tried so many different approaches to this that ended up not using emacs to setup my compilation command environment.
What I do now is to create a run_helper.sh file that simply initializes a clean environment and then uses exec $* to execute the command passed as argument to run_helper.sh
This run_helper.sh is usually project specific, but I keep a template which I use to start with when I create a new project.
Then I simple run compile from emacs like bash run_helper.sh rspec path/to/tests for example.
If I am using this to run ruby tests, my helper initializes RVM to use the proper ruby and gemset. If I am using some other language it may just export required environment variables or perform some other initialization, but this way I can do it in bash script instead of always having to mess with emacs paths and elisp every time I start a new project.
Here's an example of a run_helper.sh file
#!/bin/bash
cd /Users/simao/Documents/sp
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"
source "$HOME/.rvm/scripts/rvm" # This loads the proper ruby and gemset from .rvmrc
export RAILS_ENV=test
exec $*
This also makes my tests run faster because I have lots of stuff in my .zshrc that I don't want to load just to run some tests.
It worked for me with two things.
First I followed sanityinc advice
An improved and modified version of the code snippet is now published as elisp library called exec-path-from-shell; installable packages are available in Marmalade and Melpa
I still had a problem with compile commands. Valko Sipuli is right there was a problem involving path_helper.
I commented the corresponding line in /etc/profile and it did not help. Problem still there.
I don't use bash but zsh. Digging a little I found /etc/zshenv. This file also calls path_helper.
After commenting the path_helper section in /etc/zshenv my path is finally correct
Related
I've got a makefile for installing my personal repo of config files, part of which is compiling my emacs scripts:
compile:
emacs -batch --eval "(progn (load \"~/.emacs\") (byte-recompile-directory \"~/.emacs.d\" 0))"
The problem is, on OSX, I have an alias called "emacs" that points to the Emacs.app binary for use in a terminal, this is defined in my ~/.bash_profile.
Now, no matter what I do, I can't seem to get the shell that Make is calling to read a startup file to load that alias, so that compilation step always fails.
Does anyone know how to do this?
.bash_profile is only read by interactive login shells. Exported environment variables set in it are inherited through the process environment, which means that these settings are generally available to all programs the user starts (if bash is indeed the login shell, of course).
No such inheritance happens for aliases, though. Bash supports exported functions, but that's an obscure feature which can easily break other programs (for example, those which assume that environment variable values do not contain newlines). If you go that route, you may have to use .bashrc instead, to make sure that these functions are exported by interactive bash shells which are not login shells.
I expected the easiest solution is to put a directory like $HOME/bin on the PATH (in .bash_profile or .bashrc, whatever works best) and put an emacs wrapper script into that directory which invokes the actual binary using exec /path/to/Emacs.app "$#" (or maybe just a symbolic link would do).
That is very strange. Aliases are not exported to sub-shells, and the .bash_profile script is only run by interactive shells: make doesn't invoke an interactive shell (by default). So, it's hard to understand how the shell make invokes would see that alias based on the information you've provided.
Maybe you set the BASH_ENV shell variable somewhere? You should never do that, unless you really know what you're doing.
Maybe you reset make's .SHELLFLAGS variable to force a login shell? You shouldn't to that either.
Anyway, you can try using command which avoids aliases etc. Unfortunately make doesn't know this is a shell-built in, so you have to convince it to run a shell. This will be fixed in the next release of GNU make but Apple will never ship that.
compile:
command emacs -batch --eval "(progn (load \"~/.emacs\") (byte-recompile-directory \"~/.emacs.d\" 0))" && true
Questions with similar titles (linked) do not cover the specific errors I am encountering, hence the new ask.
Context:
I am trying to set up an environment for working with scheme in emacs so I can work through the SICP book. I understand it is possible to integrate the two nicely so they work well together. I understand there are many ways of doing this even with extra features (I've seen the names quack/racket ...) but those seem like the more advanced options. Since I am having trouble setting up this bare basics 'run-scheme' in emacs I have not looked into these options extensively.
Where I'm at / What I've tried:
I downloaded MIT:GNU scheme 9.2 for OS X (x86-64). This works as expected and I can run the interpreter either from the terminal with:
$ scheme
or by executing the file "/Applications/MIT:GNU Scheme.app/Contents/Resources/mit-scheme" (which opens the interpreter in a new terminal window).
At this point M-x run-scheme returns a message saying that no such file or directory exists.
I read these two questions for reference and then:
I created a symbolic link to the binary/exec file "/Applications/MIT:GNU Scheme.app/Contents/Resources/mit-scheme" (which works perfectly when executed from finder/the terminal), then placed this file in "/usr/bin" with name "scheme".
At this point M-x run-scheme returns:
/usr/local/Cellar/emacs/24.5/Emacs.app/Contents/MacOS/Emacs:/usr/bin/scheme: Exec format error
Process scheme exited abnormally with code 126
The symlink file looked very out of place in the bin folder so not knowing what else to try I removed the symlink and instead created an actual copy of the mit-scheme exec file and placed that in "/usr/bin".
M-x run-scheme now returns this:
/usr/bin/scheme: can't find a readable default for option --band.
searched for file all.com in these directories:
/Applications/mit-scheme.app/Contents/Resources
Inconsistency detected.
Process scheme exited abnormally with code 1
Searching for help with understanding these error messages lead me to threads about issues with setting up scheme on windows OS, which is confusing, and I feel I am now out of my depth -
hoping someone knows where I'm going wrong.
End
It seems strange because I know that the 'M-x run-scheme' command is trying to access the location "/usr/bin/scheme" and I have made sure that such a location exists, and that the scheme exec file at that location works properly - I don't understand what other requirements emacs has for this to work.
What is the run-scheme command actually doing? What other requirements does it have to function as expected?
MIT Scheme looks for its libraries (including its 'bands') in a location derived from the executable's path. If the libraries are in a non-standard location, then the mit-scheme executable won't find them.
For example, in my setup, MIT scheme is located at: /usr/local/scheme/bin/mit-scheme and the band, and other stuff, is located at /usr/local/scheme/lib/mit-scheme/.
For Emacs I've defined:
(defun mit-scheme ()
(interactive)
(run-scheme "/usr/local/scheme/bin/mit-scheme -library /usr/local/scheme/lib/mit-scheme"))
Which ensures mit-scheme can find its library when, in Emacs, I perform M-x mit-scheme. If I want MIT Scheme to be the default, I would use:
(setq scheme-program-name "/usr/local/scheme/bin/mit-scheme -library /usr/local/scheme/lib/mit-scheme")
You'll need to set your values according to your installation. You also might have an option of setting MIT Scheme related environment variables to point to your library location. The relevent one is MITSCHEME_LIBRARY_PATH.
$ mit-scheme --help
Usage: mit-scheme --OPTION ARG ... --OPTION ARG ...
This machine accepts the following command-line options. The options
may appear in any order, but they must all appear before any options
for the band.
--library PATH
Sets the library search path to PATH. This is a colon-separated
list of directories that is searched to find various library files,
such as bands. If this option is not given, the value of the
environment variable MITSCHEME_LIBRARY_PATH is used; if that isn't
defined, "/usr/local/lib/mit-scheme" is used.
--band FILENAME
Specifies the initial band to be loaded. Searches for FILENAME in
the working directory and the library directories, returning the
full pathname of the first readable file of that name. If this
option isn't given, the filename is the value of the environment
variable MITSCHEME_BAND, or if that isn't defined, "runtime.com"; in
these cases the library directories are searched, but not the
working directory.
...
I'm also trying to play with the materials from SICP. It took a couple of hours and attempts to get things working.
I realized my paths were wrong after following Don't Panic! on Mojave (macOS 10.14).
I installed mit-scheme 10.1.5 from source running the installation steps from here and in the readme. My paths were different, which caused the --band command to fail.
If you're having issues double check all your paths are what you expect.
I used make install with default paths to install to /usr/local/bin and /usr/local/lib
Mac comes with emacs 22.1.1, which is old, but it works with the extra scripts from the professor for customizing emacs.
NOTE: If you quit emacs 22.1.1, it doesn't warn you about stopping scheme, which will lead to a 100% CPU usage process that you can't stop without Activity Monitor. Newer versions of emacs will prompt you to quit the scheme process before exiting (there may be a setting for older versions as well).
I installed emacs 26.1 using Homebrew and was able to get everything running after fixing these issues:
There is an error on newer versions of emacs (24+?) with ad-advised-definition-p, fix it by commenting out the function in "multi-term.el" around line 334:
; (defcustom multi-term-dedicated-skip-other-window-p nil
; "Default, can have `other-window' select window in cyclic ordering of windows.
; In cases you don't want to select `multi-term' dedicated window, use `other-window'
; and make `multi-term' dedicated window as a viewable sidebar.
;
; So please turn on this option if you want to skip `multi-term' dedicated window with `other-window'.
;
; Default is nil."
; :type 'boolean
; :set (lambda (symbol value)
; (set symbol value)
; (when (ad-advised-definition-p 'other-window)
; (multi-term-dedicated-handle-other-window-advice value)))
; :group 'multi-term)
Path setup
I had a warning:
Warning (initialization): Your ‘load-path’ seems to contain
your ‘.emacs.d’ directory: ~/.emacs.d
This is likely to cause problems...
Consider using a subdirectory instead, e.g.: /Users/paulsolt/.emacs.d/lisp
Which I solved by pointing the top line of the ~/.emacs file to:
;; (setq load-path (append (list "~/.emacs.d") load-path))
(setq load-path (append (list "/Users/paulsolt/.emacs.d/config") load-path))
The config folder already had all the scripts, that I think I was having issues getting Emacs to see.
I installed scheme from source without modifying install paths, so I changed this line:
;; This is the place where you have installed scheme. Be sure to set
;; this to an appropriate value!!!
;;(setq scheme-root "/mit/6.945")
(setq scheme-root "/usr/local")
Next to fix the issue that was causing --band to fail I modified this line.
(setq scheme-program-name
(concat
scheme-root "/bin/mit-scheme "
"--library " scheme-root "/lib/mit-scheme-c "
;;"--band " scheme-root "/lib/mit-scheme-c/all.com "
"--band " scheme-root "/lib/mit-scheme-x86-64/all.com "
"-heap 10000"))
My scheme library was installed in a mit-scheme-x86-64 folder.
Lastly, it appears that there's some other commands I had to comment out, since they don't seem to exist ...
;;(tool-bar-mode -1)
;;(scroll-bar-mode -1)
After all that setup I'm now able to create a scheme script in emacs, and I can run the script using the meta key (iTerm set my Alt key to Esc+):
M-x run-scheme
Tip: Being newer to emacs, I'm working through the emacs tutorial (C-h t) which is essential if you're going the text-based approach. In college I used emacs outside of terminal, but I'm giving it go inside the terminal this time to learn more hotkeys.
In native emacs on windows, how can I specify environment variables for launching my shell inside emacs without modifying emacs' environment? In my specific case I'd like to set HOME to a cygwin-specific value for zsh without modifying where emacs thinks it's config file lives.
I've tried some things like changing my shell to env -u HOME ...\zsh.exe, but that seems to break (shell-command) (it appeared to involve argument order).
If this command existed, it would probably do what I want:
(setq explicit-zsh-environment '("HOME" nil))
I've read a bunch of related questions like (How can I run Cygwin Bash Shell from within Emacs?), but the unusual part for me is that all my config files are cygwin-ln-ed or windows-mklink-ed into a git repo and cygwin and windows take very different and incompatible approaches to symlinks.
Is this about running zsh as a shell inside Emacs (i.e. not about starting Emacs from a zsh shell), and having the environment that the inferior zsh process sees be different to the environment that Emacs has?
If so, you can bind the C-hv process-environment variable when you start a process. e.g.:
(let ((process-environment '("HOME=/tmp")))
(call-interactively 'shell))
$ echo $HOME
/tmp
From the Emacs manual:
Emacs sends the new shell the contents of the file ~/.emacs_shellname as input, if it exists, where shellname is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is ~/.emacs_bash. If this file is not found, Emacs tries with ~/.emacs.d/init_shellname.sh.
So for zsh you would put inside ~/.emacs.d/init_zsh.sh something like:
export HOME=/tmp
Having recently purchased Windows 7 Ultimate in order to gain access to the SUA - http://www.suacommunity.com - subsystem, I have been struggling to get SUA's bash utility (/usr/local/bin/bash) working with EmacsW32. SUA comes with ksh and csh by default, so I installed a community bundle to obtain the bash process.
M-x shell normally invokes a shell process and pipes stdio through an Emacs buffer. This works well with Cygwin. I have tried adjusting emacs variables like w32-shell-* to point it at the SUA bash executable, and also tried invoking bash via the posix.exe tool provided with SUA. I often see that the file descriptors associated with the bash process are deleted as soon as the process is created by EmacsW32.
Cygwin is very slow compared to SUA, so I am very keen to get this facility working with the EmacsW32 + SUA combo. Any tips, experience, solutions would be appreciated.
I don't know the w32-shell-* variables. Maybe you could show some code to illustrate what you mean.
I also don't know SUA.
I use GNU emacs v22 on Windows, and I run powershell as an inferior shell. I had some difficulties initially, similar to yours, and solved them with a better understanding of how to start the shell. Maybe this will help you.
I use these variables:
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ;; interactive, but no command prompt
I had difficulty until I realized two things:
if you don't specify command-line arguments for the shell process, emacs silently defaults to using -i. In the case of powershell, that argument was either not supported or it did something other than what I wanted (I forget). So I had to explicitly set the arguments to invoke the shell.
the name of the variable for specifying arguments to the shell, depends on the name of the program used to start the shell. This isn't documented as far as I know (unless you consider the source code to be documentation!). If you're on Linux and running sh, then the variable is explicit-sh-args. If bsh, then explicit-bsh-args. If you're on Windows, you need to use the proper name of the exe, including the .exe suffix. It makes for a strange looking variable name, but it works.
The full code to start powershell as an inferior emacs shell is like this:
;; get a name for the buffer
(setq buffer (get-buffer-create "*PowerShell*"))
(let ((tmp-shellfile explicit-shell-file-name))
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ; interactive, but no command prompt
;; launch the shell
(shell buffer)
; restore the original shell
(if tmp-shellfile
(setq explicit-shell-file-name tmp-shellfile)))
Maybe something like that would work for you.
Bash completion seems to work correctly for the PATH that is set when I start emacs, but if I change the PATH in an emacs shell buffer, I cannot seem to get Emacs to rethink its list of possible completions.
Is there some way to reinitialize Emacs view of the completions?
In a non-emacs shell, everything works as expected with the occasional hash -r thrown in for good measure.
Assuming you are using completion-at-point, there is a variable exec-path that maintains a list of the directories that are searched for executables. shell-command-completion, which is by default part of shell-dynamic-complete-functions, searches those locations.
You can update that variable with new locations to search for executables, eg. (push "<new-directory>" exec-path).