Loading an Emacs init file - emacs

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.

Related

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)

How do I prevent emacs from resolving directory symlinks in paths?

I have a project, call it 'foobar', that when I checkout has all its source in the folder "foobar/foobar". Because the top level foobar directory contains nothing except for the inner foobar directory, it's pointless, but that's how things were originally checked into the project and it's outside my control. This has the unfortunate effect of making paths longer and harder to read, so I rename the toplevel foobar to "foobar-checkout" and then make a symlink called "foobar" that links to "foobar-checkout/foobar". This way I can open "foobar/source.c" instead of "foobar/foobar/source.c".
This works for when I'm in the shell, and when I first open the file in emacs, but after that emacs will resolve the symlink. So if I have source.c open and I press Ctrl+x Ctrl+f to open a new file, the path it lists is "foobar-checkout/foobar/" rather than "foobar/". Is there a way to get emacs to not resolve the symlink so I can enjoy the shorter path?
I've just tried this on GNU Emacs 22.2.1, and it doesn't seem to resolve my symlinks. Is it possible that the resolving of symlinks is not vanilla emacs behavior and is rather something unintentionally introduced with a file opening module, such as ffap.el?
Either way, I couldn't test my idea, but it occured to me that you might override file-symlink-p, described currently as:
file-symlink-p is a built-in function in `C source code'.
(file-symlink-p FILENAME)
Return non-nil if file FILENAME is the name of a symbolic link.
The value is the link target, as a string.
Otherwise it returns nil.
This function returns t when given the name of a symlink that
points to a nonexistent file.
If you modify that to always return nil, perhaps emacs won't resolve the symlinks:
(defun file-symlink-p (FILENAME)
nil)
Of course, this will probably break some other stuff, but maybe it's worth a shot.
You might like to use directory-abbrev-alist or maybe vc-follow-symlinks.

Emacs: Open a specific Info section

When i do describe-function info, documentation says: Called from a program, FILE-OR-NODE may specify an Info node of the form "(FILENAME)NODENAME".
I used this syntax to open specific sections in Emacs manual like eval-expression (info "(emacs)mark").
But now i have three questions:
How to find out filename of a certain info file? (example: "elisp" for Emacs Lisp Intro)
How to find out the section name? (example: "mark" for "11 The Mark and the Region")
Is it possible to list info files and their sections before calling one with eval-expression?
Optional: is it possible to do all of the above without running info mode?
To clarify, my questions arise from the fact that the node name (example: "Shell") and title of a corresponding section (example: "36 Running Shell Commands from Emacs") differ.
How to find out filename of a certain info file? (example: "elisp" for Emacs Lisp Intro)
C-hm from the *info* buffer will tell you that the c key will Put name of current Info node in the kill ring (as well as displaying it in the echo area). From there you can yank it with C-y as usual.
The part of the node name in parenthesis is the name of the file (minus its suffix, and directory path).
To obtain the filename including its directory path (but still minus any suffix), you can call (Info-find-file "file") for the file in question ("emacs" in your example).
Note that the suffix for the file (if any) can be anything in the Info-suffix-list variable.
You can in fact pass a full file path as the argument to the info function as well. That's generally less useful, but may be handy if you managed to end up with conflicting info files. Interactively, C-uC-hi will prompt you for a full filename.
(To enter a node name interactively, you can use C-hig.)
How to find out the section name? (example: "mark" for "11 The Mark and the Region")
That's essentially the same as the first question, except it's the part of the node name after the parentheses that you want. Again, just use c to find out.
Is it possible to list info files and their sections before calling one with eval-expression?
I'm not entirely sure what you mean by this, but maybe these help?
d will jump to the main directory (as C-hi takes you to initially). This is generated from all the dir files found in the directories in the Info-directory-list variable. Each dir file contains the paths for each file, but you're unlikely to need to see those.
< or t takes you to the top node of the file, which generally presents the top-level contents for the file.
T opens an all-on-one-page table of contents for the current file.
I presents you with index entries matching a pattern, for the current file.
C-s can search through all nodes of the current file (simply type it again each time you hit the end of the current node to start searching in the next).
M-x info-apropos can be used to search all info files.
And obviously once you've arrived at a particular node via any method, you can use c to find out its name.
Also as previously mentioned, once you're in the *info* buffer, you can type g to go to any node in any info file, using the same (file) node syntax. You can omit (file) if you are already in that file, or omit node to get to the top node of the specified file. You may or may not prefer that to using
M-: (info "(file) node") RET
As a special case, g*RET renders the entire current info file in a single buffer.
Other useful Q&As on the general subject:
Reading multiple Emacs info files simultaneously
how to open *.info file in emacs in info mode?
info indexing (within and without emacs)
Also:
http://www.emacswiki.org/emacs/InfoMode
And of course:
M-: (info "(info) Top") RET
C-hih
C-hi?

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

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.