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

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)

Related

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.

Emacs: How to associate different initialization configurations with different file formats?

What's the best way to associate file extensions with my own customizations? For example, when I open a .py file the frame would be bigger and split into 2 windows, but when a .tex file is opened the frame would be smaller with just one window. Should I split my .emacs and write all configurations associated with python in a .el file (key bindings, python shell = ipython, etc ...) and for latex in another .el file (load auctex, pdf mode = default, etc ...)? How would I "call" the files and make them work appropriately (if that'a possible and good solution)?
(First, +1 to #phils's comment. You will get better help if you are more specific about what you need/want.)
Depending on just what you need/want, see also variable (not option) file-name-handler-alist. You might not need it, but you might.
You can make use of it if you intend all or particular operations on the files to involve additional actions (such as those you describe). For any operations where you do not need special treatment, just provide the default behavior. For the others, provide the default behavior plus the extra behavior (in whichever order is appropriate).
See (elisp) Magic File Names for more information.

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 bookmark and accessing across different local path

When I bookmark a File in emacs, it has the full-path C:/WindowPath/CommanPath/File I can access the same directory from Linux with /LinuxPath/CommanPath/File. But, when I try to bookmark in one OS and access in other OS, the file-path are different and I cannot access the same file from M-x list-bookmark interface. How I can resolve this? Please note that the 'CommanPath' is same for both OS.
Is there something in bookmark library that I can use? If that is not the case, then how should I create shortcuts so it can work seamlessly in both OS, or for that matter in any path ?
Thanks.
Hm. How does Emacs find the same file starting with different absolute file names on different OS's? If you can track that down then you can likely make the bookmark code do the same thing. The bookmark just saves the absolute file name in the form of the OS you were using at the time you created the bookmark. Giving that to Emacs to find should be no different from giving it to find-file, which you say works.
(That is, I think that's what you're saying: you can type the Windows form of the file name at C-x C-f when on Linux and it just works, and vice versa.)
The basic file-finding function is find-file-noselect, but all it seems to do in this regard is (abbreviate-file-name (expand-file-name FILENAME)), which would not be sufficient if given FILENAME as an absolute name from the wrong file system.
If you cannot figure out how to code this generally, you might be able to use directory-abbrev-alist to make your own explicit correspondence between the two directory prefixes. Dunno.
Interesting question.

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?