Emacs change file extension - mode association - emacs

My Emacs opens .m files in ObjC mode. However I want to open them in Octave mode. I have already added to the .emacs file:
(autoload 'octave-mode "octave-mod" nil t)
(setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))
What else should I do? I do have Octave mode installed.

Fortunately everything is working now and unfortunately I don't remember how I fixed it :) Maybe there was an error in my .emacs earlier. This is the more correct code:
(add-to-list 'auto-mode-alist '("\\.m$" . octave-mode))
Autoloading is unneeded in recent versions; if you do need to enable it, note that "octave-mode" is not a typo.
(autoload 'octave-mode "octave-mod" nil t)

Use this.
;; octave-mode
(autoload 'octave-mode "octave-mode" "Loding octave-mode" t)
(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))

Just ran into this exact problem.
Your statement is correct, but your .emacs file probably isn't loading up correctly. Emacs searches the "HOME" variable to load up preferences, lisp code etc.
To see what your HOME variable is:
Open scratch buffer (this is a "play place" to try things out):
C-x C-b *scratch* <RET>
Evaluate this expression by typing it, then putting the cursor to the right, then hitting C-x C-e
insert (getenv "HOME")
Emacs will display your home path at the bottom (mine defaulted to ...Documents and Settings\UserName)
I haven't worked out a good way to change it, but you're supposed to be able to simply add HOME as an environment variable (that didn't work for me).
It's also talked about a bit more over here:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html
Also remember that the file has to be ".emacs" and not myConfig.emacs or something of the like. Use bash command ren to rename the file (windows explorer won't let you have nameless files)

Related

Running a .el file when opening emacs

I downloaded two .el files
One is to highlight current column where cursor is and another one to highlight some specific words.
I followed next steps in the file:
(add-to-list 'load-path "~/.xemacs/packages/") //path where I saved .el files
(load "column-marker") //name of file without .el extension
To this step it works fine, I find those when I press M-x column-marker and I can use it.
My problem begins when I want to use it everytime i open emacs and I found that I could use something similar to this:
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
Which in my case I am typing:
(add-to-list 'auto-mode-alist '("\\.\\'" . column-marker)) //I want to enable it for all .something files
But... when doing that, there is an error when opening emacs and it starts with default setup.
What am I doing wrong? I have tried many ways and none work.
Thanks in advance
Always post the error message you see. That helps others help you.
C-h f auto-mode-alist tells you that the functions you use in it must implement major modes. column-marker is not a major-mode function. It is not even a function; it is a file.
What you need to do, for each mode where you want some function in file column-marker.el to be invoked, is to put that function on the major-mode hook for that function. For example:
(add-hook 'emacs-lisp-mode (lambda () (interactive) (column-marker-1 80))
And lo and behold, what does the Commentary in file column-marker.el tell you?
;; Installation: Place this file on your load path, and put this in
;; your init file (`.emacs'):
;;
;; (require 'column-marker)
;;
;; Other init file suggestions (examples):
;;
;; ;; Highlight column 80 in foo mode.
;; (add-hook 'foo-mode-hook (lambda () (interactive) (column-marker-1 80)))
Couldn't be clearer. Provided you actually read it.
Seriously, a minimum of investigation is in order, before you post a question to StackOverflow. You should do a Google search, open README files and read them, and so on --- first. And in the case of Emacs questions, IMHO, you should ask Emacs first (e.g. C-h v auto-mode-alist).
According to the rules of S.O. posting, not doing preliminary simple research is even grounds for closing a question. So do not be surprised if this question gets closed or downvoted.

Octave mode in Emacs won't automatically load for a .m file [duplicate]

My Emacs opens .m files in ObjC mode. However I want to open them in Octave mode. I have already added to the .emacs file:
(autoload 'octave-mode "octave-mod" nil t)
(setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))
What else should I do? I do have Octave mode installed.
Fortunately everything is working now and unfortunately I don't remember how I fixed it :) Maybe there was an error in my .emacs earlier. This is the more correct code:
(add-to-list 'auto-mode-alist '("\\.m$" . octave-mode))
Autoloading is unneeded in recent versions; if you do need to enable it, note that "octave-mode" is not a typo.
(autoload 'octave-mode "octave-mod" nil t)
Use this.
;; octave-mode
(autoload 'octave-mode "octave-mode" "Loding octave-mode" t)
(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))
Just ran into this exact problem.
Your statement is correct, but your .emacs file probably isn't loading up correctly. Emacs searches the "HOME" variable to load up preferences, lisp code etc.
To see what your HOME variable is:
Open scratch buffer (this is a "play place" to try things out):
C-x C-b *scratch* <RET>
Evaluate this expression by typing it, then putting the cursor to the right, then hitting C-x C-e
insert (getenv "HOME")
Emacs will display your home path at the bottom (mine defaulted to ...Documents and Settings\UserName)
I haven't worked out a good way to change it, but you're supposed to be able to simply add HOME as an environment variable (that didn't work for me).
It's also talked about a bit more over here:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html
Also remember that the file has to be ".emacs" and not myConfig.emacs or something of the like. Use bash command ren to rename the file (windows explorer won't let you have nameless files)

emacs: open all .txt files in a specific directory in a specific major mode

EDIT: It turns out that the second edit to my .emacs file actually works. (See the comments below this entry.)
I tried a couple of addition to the .emacs to make all txt files opened in emacs use orgmode. They did not work. How can I make it happen?
;;SET EMACS AS DEFAULT MAJOR MODE TO FOR ALL FILES WITH AN UNSPECIFIED MODE
(setq default-major-mode 'org-mode)
;;OPEN ALL TXT FILES IN ORGMODE
(add-to-list 'auto-mode-alist '("\\.txt$" . org-mode))
Additionally:
It would be even better to open only txt files in a certain directory orgmode. Any hint as to how that could be done would also be appreciated.
Another way to do this is using directory-local variables. This is nice because you can put a file in any directory where you want this behavior to engage, and it works recursively in any subdirectories.
Create a file called .dir-locals.el in the desired directory.
Here are the contents:
((nil (eval . (if (string-match ".txt$" (buffer-file-name))(org-mode)))))
Read this like so: for any major-mode (nil), evaluate the following form:
(if .... (org-mode))
The regex in auto-mode-alist could be something more complex, like "^/path/to/.*\\.txt$"
You can implement a hook which verifies the file directory and modifies the buffer mode:
(add-hook 'find-file-hooks
(lambda ()
(let ((file (buffer-file-name)))
(when (and file (equal (file-name-directory file) "c:/temp/"))
(org-mode)))))
As an alternative you can add the mode line in the beginning of your text file. In this case emacs will set the specified mode.
; -*- mode: org;-*-
* header 1
** header 2
I glued together some code from Oleg Pavliv's answer here, and from yibe's at elisp - File extension hook in Emacs - Stack Overflow
(defun use-org-mode-for-dot-txt-files-in-owncloud ()
(when (and (string-match owncloud buffer-file-name)
(string-match "\\.txt\\'" buffer-file-name))
(org-mode)))
(add-hook 'find-file-hook 'use-org-mode-for-dot-txt-files-in-owncloud)
This way, though ownCloud Web and phone apps are currently friendly only with .txt files, from my PC I can use Emacs' Org-mode for them.
(If I set all .txt files to use Org-mode, it breaks todotxt-mode.)
(Note that owncloud is a string variable equal to my ownCloud path.)

Emacs (Aquamacs) using wrong mode (sometimes)

I'm using version 2.1 of Aquamacs and django-mode from https://github.com/myfreeweb/django-mode. I installed it (after installing yasnippets) by adding
(require 'django-html-mode)
(require 'django-mode)
(yas/load-directory "path-to/django-mode/snippets")
(add-to-list 'auto-mode-alist '("\\.djhtml$" . django-html-mode))
to my .emacs file. But sometimes Aquamacs uses the standard HTML mode instead of django-html-mode. This happens whenever a file starts with an html tag.
What do I have to change to make Aquamacs prioritize the file's extension instead of it's content (at least for .djthml files - everything else works just fine)?
Check magic-mode-alist and magic-fallback-mode-alist (although the latter shouldn't be applied if the file extension is in auto-mode-alist). You also need to watch out for case; Emacs is likely to consider FOO.DJHTML to not match "\\.djhtml". I don't have Aquamacs installed, but Emacs.app has HTML recognition regexps in magic-fallback-mode-alist.
BTW, if it is case sensitivity, change the auto-mode-alist line to
(add-to-list 'auto-mode-alist '("\\.[Dd][Jj][Hh][Tt][Mm][Ll]$" . django-html-mode))

How to tell emacs to open .h file in C++ mode?

What lines should I add to my _emacs (on Windows) file to have it open .h files in C++ mode? The default is C mode.
Try this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Whenever you open .h files, C++-mode will be used.
Another approach for using both c-mode and c++-mode as appropriate, is to use directory local variables to set the mode.
Directory variables are evaluated after the mode has been set1, so you can actually write a .dir-locals.el file for your C++ project containing this:
((c-mode . ((mode . c++))))
And Emacs will change the mode to c++-mode whenever it had initially set it to c-mode.
If you work with a mix of C and C++ projects, this makes for a pretty trivial solution on a per-project basis.
Of course, if the majority of your projects are C++, you might set c++-mode as the default2, and you could then use this approach in reverse to switch to c-mode where appropriate.
1 normal-mode calls (set-auto-mode) and (hack-local-variables) in that order. See also: How can I access directory-local variables in my major mode hooks?
2 To do so, add
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
to your .emacs file which open .h files in C++ mode by default.
If you don't want this to apply to every .h file, you can add the following to the bottom of your C++ header files.
// Local Variables:
// mode: c++
// End:
This will work for any Emacs variables that you want to set on a per file basis. Emacs ignores the leading characters, so use whatever comment characters are appropriate for the file type.
Apparently you can also put this at the top of the file:
// -*-c++-*-
to tell Emacs it's a C++ file.
I use this since I quite frequently end up on a vanilla Emacs and it works without configuring Emacs in any way.
Since I use both C and C++ regularly, I wrote this function to try and "guess" whether a .h file is meant to be C or C++
;; function decides whether .h file is C or C++ header, sets C++ by
;; default because there's more chance of there being a .h without a
;; .cc than a .h without a .c (ie. for C++ template files)
(defun c-c++-header ()
"sets either c-mode or c++-mode, whichever is appropriate for
header"
(interactive)
(let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
(if (file-exists-p c-file)
(c-mode)
(c++-mode))))
(add-to-list 'auto-mode-alist '("\\.h\\'" . c-c++-header))
And if that doesn't work I set a key to toggle between C and C++ modes
;; and if that doesn't work, a function to toggle between c-mode and
;; c++-mode
(defun c-c++-toggle ()
"toggles between c-mode and c++-mode"
(interactive)
(cond ((string= major-mode "c-mode")
(c++-mode))
((string= major-mode "c++-mode")
(c-mode))))
It's not perfect, there might be a better heuristic for deciding whether a header is C or C++ but it works for me.
I could swear I saw this question answered appropriately already? Weird.
You want this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))