Emacs (Aquamacs) using wrong mode (sometimes) - emacs

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))

Related

How to use the same file extension with different modes?

I have files with a .new extension that contain LaTeX code (they are the output of a program that does things on the original .tex source).
In order to check them I need that they have the LaTeX syntax highlighted and that they can be opened in read-only mode to avoid introducing errors. So I put these lines in my .emacs:
(setq auto-mode-alist
(append '(("\\.new\\'" . read-only-mode)
("\\.new\\'" . latex-mode))
auto-mode-alist))
But it does not work as expected because only the first mode is applied. How can I force emacs to apply both read-only-mode and latex-mode to the same file extension?
An easy solution is to associate your file extension with a custom derived major mode:
(define-derived-mode read-only-latex-mode latex-mode "LaTeX"
"Major mode for viewing files of input for LaTeX."
(read-only-mode 1))
(add-to-list 'auto-mode-alist '("\\.new\\'" . read-only-latex-mode))
The new read-only-latex-mode does everything that latex-mode does (including running latex-mode-hook if you're already using that), but it additionally enables read-only-mode after all of the parent mode's actions have taken place.
Any other code which cares about the major mode being latex-mode should already be testing that using the standard approach of (derived-mode-p 'latex-mode) which is still true for this new mode; so in principle this approach should Just Work.
Not sure I can really provide an answer, but here's some things to consider:
read-only-mode is a minor mode, while
auto-mode-alist is meant to turn on major modes. For major modes, it makes sense that you can have only one.
You can put something like -*- buffer-read-only: t; -*- on your first line in the file (possibly behind some comments or shebangs, depending on the file type) which would turn on read-only-mode. Maybe automate this using auto-insert-alist and match the .new filename pattern.
There's some good suggestions over at https://www.reddit.com/r/emacs/comments/jqxgiz/how_can_i_make_some_files_automatically_readonly/ , namely try file local variables and use find-file-hook
Hope some of that helps.

Emacs: How to use a major mode for non-standard file extension

I'd like to use R major mode for another file extension in emacs (for an unsupported language with syntax similar to R).
How do I force emacs to change the major mode for a buffer I'm editing? How do I change my .emacs to permanently associate a major mode with a particular file extension?
This should work:
(add-to-list 'auto-mode-alist '("\\.rr" . R-mode))

Only use 2 mode in Emacs with web-mode

I use web-mode in Emacs, everything is fine except that when editing a .js.erb file in Rails.
Because there's only 2 modes in .js.erb file: js and ruby and web-mode only recognize javascipt code in <scipt type="javascript"></scipt>.
I'm not very familiar with emacs and web-mode, maybe there's a way to config.
You can change the "default" mode of an active buffer by modifying the variable web-mode-content-type, e.g. from M-::
(setq web-mode-content-type "javascript")
Default modes may be specified by file name (or pattern) by modifying the web-mode-content-types alist somewhere in your configuration, e.g.
(eval-after-load "web-mode"
'(add-to-list 'web-mode-content-types '("javascript" . "\\.js\\.erb\\'")))
The web-mode's author already fixed this, so just update your web-mode

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 change file extension - mode association

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)