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.
Related
Background:
I modularised the init.el file into several files and tried to load them one by one from init.el.
Each file has a:
(provide 'xxx)
at the end and I use:
(require 'xxx)
to load them.
(before I used load-library to load them, but recent I learned the require command will only load them if they are not loaded.)
Then some wired behaviour appears.
I'm not sure if this is the problem of not loading the library in the right order or of not loading certain file.
So the question is:
How can I see which file are loaded by the require command? (In the 'Message' buffer, I can see files loaded by 'load-libarry', but not by 'require')
You might inspect the variable load-history.
You could look at featurep function and features variable: each time a provide statement is encountered, the features variable is updated.
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.
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.
I have an org-mode task list that I keep in version control. I would like to press a key and turn the current position into a bookmark target that I will be able to access anywhere I have the list checked out, regardless of other changes that have been made to the document. This is why (bookmark-set) will not work.
Additionally I would like to ensure that this target only occurs once in the file. If I put the target at a different position I want the original target to go away.
Essentially, I want to combine features of Emacs bookmarks (the singleton aspect), and org-mode links (more robust persistence). What's the best way to do this?
You want to read up about Markers:
M-: (info "(elisp) Markers") RET
This feature is what allows the mark ring to retain its relative locations regardless of buffer changes, for example.
If you want the marker to be stored within the file itself, then you might want to include it as a local variable in the file itself, and use before-save-hook to update that to the current value.
The local variable may need to be an integer, in which case you would need to translate it on loading and saving.
(This is a little speculative, but I suspect it will do the trick.)
The best solution I've been able to come up with is to use the text <<<BOOKMARK>>>, and to search for it when necessary. At some point I might write some functions that place this bookmark and delete it from elsewhere.
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.