In Lisp, Avoid "Cannot open load file" when using require - emacs

I am working on a custom .emacs file that I will be able to use on several different computers. I would like to be able to load a mode if it exists on the system. If it does not exist I would like Emacs to stop showing the error: File error: Cannot open load file, X.
For example:
(require 'darkroom-mode)
Results in:
File error: Cannot open load file, darkroom-mode
I am using file-exists-p to test if certain other files exist but for this test I would assume I need to search my load-path. I am new to Lisp so this is stumping me.

If you just want to keep require from issuing an error, you can do:
; third arg non-nil means to not signal an error if file not found
; a symbol provides documentation at the invocation (and is non-nil)
(require 'darkroom-mode nil 'noerror)
From the documentation (C-h f require RET):
require is a built-in function in `C source code'.
(require feature &optional filename noerror)
If feature feature is not loaded, load it from filename.
If feature is not a member of the list `features', then the feature
is not loaded; so load the file filename.
If filename is omitted, the printname of feature is used as the file name,
and `load' will try to load this name appended with the suffix `.elc' or
`.el', in that order. The name without appended suffix will not be used.
If the optional third argument noerror is non-nil,
then return nil if the file is not found instead of signaling an error.

Related

Racket. Why it doesn't load the data with "require" when the data is defined in anoter file

I have this in one file (the name of the file is ExamplesFile.scm):
(define examples (with-input-from-file "examples.scm" read))
I understand that the data is loaded and stored at the variable examples
If I execute:
examples
in the iteration window it gives the data that is at the file "examples.scm". That is working fine.
Later, in another file located at the same directory I write:
(require "ExamplesFile.scm")
In the iteraction window of this second file the functions defined at "ExamplesFile.scm" are available. But if I execute:
examples
I got this error:
examples: undefined;
cannot reference an identifier before its definition
How can I solve it? How can I have the data that is read and stored in one file in the second file?
In order to make the examples binding in ExamplesFile.scm visible to other files that require it, you need to explicitly provide it, using (e.g.)
(provide example)
Alternatively, if you want to provide everything defined in the file, you can use
(provide (all-defined-out))
All of this is assuming that you're using the #lang racket language; you don't mention this explicitly.
Apologies if I've misunderstood something about your problem!

Loading an Emacs init file

In Emacs, I am putting on the menu an item to load the init.el file, since I am in there almost daily.
menu code is working fine, but the file isn't loading.
So in a buffer, for troubleshooting, I enter:
(load user-init-file)
and use C-x C-e to execute it.
Turns out if fails because it needs double backslashes in the path.
user-init-file resolves to "c:\steve\emacs\init.el"
But should be `
"c:\\steve\\emacs\\init.el"
is there a function already to convert to the double backslashes?
Or how do I do that with a search/replace?
This is similar to other search questions I found except this is for replacing within a string instead of within a buffer.
I think you probably just want (load-file user-init-file). load-file does not use load-path, and it does not try to append .elc or .el.
(If you use MS Windows notation for a file name then you can see what Emacs really thinks the file name is, by calling file-truename on it.)
If you really want to use load, try (load user-init-file nil nil t).
load tries to expand its FILE arg, automatically adding .elc and .el. The 4th argument is NOSUFFIX, which if non-nil prevents that behavior.
C-h f load:
**load-** is a built-in function inC source code`.
(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
Execute a file of Lisp code named FILE.
First try FILE with .elc appended, then try with .el,
then try FILE unmodified (the exact suffixes in the exact order are
determined by load-suffixes). Environment variable references in
FILE are replaced with their values by calling substitute-in-file-name.
This function searches the directories in load-path.
If optional second arg NOERROR is non-nil,
report no error if FILE doesn't exist.
Print messages at start and end of loading unless
optional third arg NOMESSAGE is non-nil (but force-load-messages
overrides that).
If optional fourth arg NOSUFFIX is non-nil, don't try adding
suffixes .elc or .el to the specified name FILE.
If optional fifth arg MUST-SUFFIX is non-nil, insist on
the suffix .elc or .el; don't accept just FILE unless
it ends in one of those suffixes or includes a directory name.
If NOSUFFIX is nil, then if a file could not be found, try looking for
a different representation of the file by adding non-empty suffixes to
its name, before trying another file. Emacs uses this feature to find
compressed versions of files when Auto Compression mode is enabled.
If NOSUFFIX is non-nil, disable this feature.
The suffixes that this function tries out, when NOSUFFIX is nil, are
given by the return value of get-load-suffixes and the values listed
in load-file-rep-suffixes. If MUST-SUFFIX is non-nil, only the
return value of get-load-suffixes is used, i.e. the file name is
required to have a non-empty suffix.
When searching suffixes, this function normally stops at the first
one that exists. If the option load-prefer-newer is non-nil,
however, it tries all suffixes, and uses whichever file is the newest.
Loading a file records its definitions, and its provide and
require calls, in an element of load-history whose
car is the file name loaded. See load-history.
While the file is in the process of being loaded, the variable
load-in-progress is non-nil and the variable load-file-name
is bound to the file's name.
Return t if the file exists and loads successfully.

Path with a folder named tilde(~) can't be recognized by load-file in emacs

I have a folder /var/~/. In config .emacs I wanna load some files from this folder.
I try to use (load-file "/var/~/foobar.el"), but emacs alerts File error: Cannot open load file, ~/foobar.el.
Furthermore I couldn't even open the files under this folder with c-x c-f. In minibuffer the path will auto be redirected to my home.
How could I load files in that folder?
You need to rename your directory.
load-file is a simple wrapper around load, which passes the given file name through substitute-in-file-name. From the docstring of substitute-in-file-name (emphasis mine):
Substitute environment variables referred to in FILENAME. `$FOO' where FOO is an environment variable name means to substitute
the value of that variable. The variable name should be terminated
with a character not a letter, digit or underscore; otherwise, enclose
the entire variable name in braces.
If `/~' appears, all of FILENAME through that `/' is discarded. If `//' appears, everything up to and including the first of those `/' is discarded.
In other words, substitute-in-file-name throws away everything before /~, turning /var/~/foo.el into ~/foo.el.
I completely fail to see any reason in this behaviour, but it is what it is, and you cannot (easily) work around it, so renaming is your best way out of this dilemma.
It's a reasonable thing to do, anyway. Using ~ as directory name is bad idea on Unix systems generally, not just for Emacs alone.
lunaryorn explained your problem well, and I agree with his suggestion that not using ~ in file paths is the best solution. However, If you can't rename these paths for whatever reason, I believe you can work around substitute-in-file-name by loading a relative file path as documented here.
Basically, you need to add nil to your load-path variable, then set your default-directory variable to the troublesome path, finally then load the file using a relative name. e.g.:
; adding nil causes load to also search your 'default-directory'
(setq load-path (append '(nil) load-path))
(setq default-directory "/tmp/~/")
(load "foobar.el")
Note that if you suspect the file name might exist (be loaded from) elsewhere in your load-path you would need to ensure the file you want is first in the load-path.
See How programs do loading.

emacs: have path be a part of buffer name instead of `<2>`

I'm working on a golang project, where there are a lot of files with the same name, in different directories.
For example, there's a parser class, and a handler class, both of which have separate directories, but the filenames in the two directories are nearly identical.
Is there a way to tell emacs to show path as buffer name prefix, instead of affixing <2> to the repeating buffer names as a suffix?
See the Emacs manual, node Uniquify, and user option uniquify-buffer-name-style.
C-h r g uniquify RET
You can customize the option value to forward to get the behavior you request.
In Emacs versions older than Emacs 23 you will not find node Uniquify in the manual, and you will need to explicitly require library uniquify.el in your init file (~/.emacs):
(require 'uniquify)

Opening a file in emacs: Values that are not safe

When I open the file zenburn-theme.el from github.com/bbatsov/zenburn-emacs.git in Emacs 24.3, I get the following warning in a buffer:
Why? Also, why would it not be safe to open (not load or run) a file?
A file, even if opened and not loaded, might contain some configuration values to be applied. Some of them are considered unsafe and Emacs asks you about them unless told otherwise. See Local Variables in Files for details.