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

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.

Related

Running a function before opening/finding a file

Im trying to make a function that should run before a file is opened/shown in the buffer. This function really only needs to run once.
I try to set a mode depending on the content of the file that is about to be viewed. I know about add-to-list 'auto-mode-alist <filename-regexp> . <mode>
But this is not enough. Ideally i would want to parse the file (or just a small part of it) and check the file content before setting the mode. Usually the view-file (think .js/.php etc. in a mvc framework) has the same extension as the files in "pure" code, so i cant just check for the file extension.
Why?
Basically when using any template language i normally want to use web-mode and when doing "pure" code i want to use the mode for the language.
So basically i would want to parse the file before, and check for some regexp, and if it matches i would set web-mode and if not i would set the language-mode i want to use.
I know i can easily change modes manually, but i would rather have this done by Emacs.
You can use magic-mode-alist for that. You'll probably want to use a MATCH-FUNCTION which checks the buffer's file-name and its content.
You might want to put your function into find-file-hook.
When it is run, the major-mode is already set, but you can change it.
If you own the file in question, you might also want to explore file variables.

CEDET: storing tags manually

I'm using ECB with Cedet - and semantic search engine stores tags about the files I visit in its cache files.
I'm also using ECB's left-symboldef layout - which shows definition of the tag the cursor is on. In order to do that semantic opens the file where the tag was defined.
The problem is - semantic opens almost all of my python scripts all the time - since I have parser defined in all of them - when I parse command line arguments with argparse... So I'd rather stop semantic caching my files automatically, and do it manually with C-c , , on my libs only.
So my question is - how do I prevent semantic from storing cache? I should still be able to use the existing database (which I'll collect manually).
To prevent the automatic parsing of other files in idle time, you can set:
(setq semantic-idle-work-parse-neighboring-files-flag nil)
and if it is pulling them in via includes, you can do this:
(setq semantic-idle-work-update-headers-flag nil)
This is actually the default, and it gets set to true if you use one of the canned configuration options for regular or gaudy code helpers.
In order to not parse all the files, but still let ECB find tag definitions, you will probably need to use a GNU Global database. See semanticdb-enable-gnu-global-databases for more.

Creating vim buffer (NarrowRegions) from multiple files

I was wondering if there's a way to allow a buffer to edit multiple files at once.
Recently, I got vim working with eclim. But now I was wondering if I could edit multiple files at once in one buffer. For example, say I have an interface and a class file where I need to update a method signature is there a way I can load both of them into the same buffer and edit them simultaneously. Narrow region for multiple file regions. It would also be awesome to remember my settings but that could be a future iteration.
I saw this solution but it seems inconvenient to create a separate file to handle this interaction.
You can open all files as split windows (so you see all of them together), and :windo, :bufdo, :argdo allow you to perform mass-operations (like a :substitute) on all of them at once. There's usually no need for such artificial concatenation schemes, and as the linked article shows, it has its downsides over keeping the files separate.

How to write .emacs from Emacs when I've deleted .emacs

I have Emacs open but accidentally I've deleted the .emacs file it read when it started. This represents about 15 years of tweaking. (I know, I know, backups.)
Is there a way to get Emacs to write out the .emacs file I've deleted?
I wouldn't normally ask such a lame question on SO but I know I only have a day or so before this Emacs session ends.
As ayckoster suggests, you might try a file recovery or forensics tool like The Sleuth Kit. Or, and this may seem crazy, if you're on a Unix-like system, you could search through the raw disk device (on the Mac I'm currently on, that would be /dev/rdisk1). Seriously, several times I've been too lazy to break out a full-blown recovery tool but instead used something like sudo less -f /dev/rdisk1, searched for a string I knew was in the file (global-set-key, anyone?), and succeeded in recovering the file's original content.
If you have Emacs' backup feature turned on, you should have a copy of your next-to-last .emacs file in ~/.emacs~. If so, just rename that one to ".emacs" and you will have the .emacs file with all but your latest changes. Even if you don't currently have backups enabled, you might still have a substantial chunk of your .emacs file in the last backup on file. You should also look at the value of the variable "backup-directory-alist" - it specifies location(s) for backup files to be stored if the default (same directory as modified file) isn't used.
Otherwise, how good is your memory... ;-)
EDIT: Since you don't have a backup of your .emacs file but you have a running Emacs instance that was started with that .emacs file, another thing you can do is to save all the custom settings that would have been defined in your .emacs file. To do this, do something like:
(setq custom-file "/my/home/directory/.emacs-custom.el")
(custom-save-all)
Then, you could create a new .emacs file and add the following lines to it:
(setq custom-file "/my/home/directory/.emacs-custom.el")
(load custom-file)
That will at least restore some of the custom variable settings that were in your .emacs file.
Emacs evaluates your .emacs file and afterwards it is closed. So basically you cannot get your .emacs back.
A solution might be to use a file recovery application. The odds of your .emacs being on your hard drive are quite good.
As most such programs cannot deduce the file name or directory name of the deleted file you have to know the content of your .emacs.
Then you can restore all currently deleted files in some folder and recursively search for the contents of your .emacs.
This process might take very long. You have to decide if its worth your effort.
I don't know of any way to get Emacs to provide the original .emacs file, but you can certainly interrogate the loaded function and variable symbols, and obtain their values.
This would be rather a lot of work, but I think in theory you should be able to obtain a good chunk of this data in some form or other, if you succeeded in filtering it all down to what you knew was yours.
For evaluated functions, (symbol-function 'SYMBOL) will return a (less-readable) definition of the supplied function. You could then use (fset 'SYMBOL VALUE), where VALUE is the result of the call to symbol-function, to define that function in a new .emacs file. That would give you an approach for recovering your defined functions.
http://www.gnu.org/s/emacs/manual/html_node/elisp/Function-Cells.html
http://www.gnu.org/s/emacs/manual/html_node/elisp/Symbol-Components.html
You might also look at:
How to print all the defined variables in emacs?
This is a very incomplete starter, but given time constraints I'm posting and marking it community wiki, if anyone wants to run with it.
A how-to for dumping the state of the application in a reliably restorable fashion would be a great start, if the current session is definitely going to be killed (or even if it's not, actually, to guard against crashing or other mishap).
You could potentially re-tag this with some more general data-recovery type tags, to expand the audience.

Multiple "Occur" result buffers in emacs?

Is there a package that allows me to have multiple Occur result buffer for the same buffer (like grep-a-lot: http://www.emacswiki.org/emacs/grep-a-lot.el).
I run into this issue when analyzing log files for multiple keywords (say to see what different but related threads were doing).
You don't need an additional package. C-ur will rename the current occur buffer to an unique name. You can add occur-rename-buffer to occur-hook to make it automatic (see the documentation of occur-rename-buffer which mentions this, too).