Only use 2 mode in Emacs with web-mode - emacs

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

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 openfile in terminal don't load configuration correctly. But open file in emacs do load configuration correctly

Emacs openfile in terminal don't load configuration correctly. But open file in emacs do load configuration correctly.
I have tried several emacs configuration for cpp:
(eval-after-load "cc-vars" '(lambda() (c-set-style "linux")))
Another one
(add-hook 'c-mode-hook '(lambda() (c-set-style "linux")))
Another one
(eval-after-load "cc-vars" '(setq c-default-style "linux")))
If I type emacs filename they can't be loaded. It seems the emacs always load the default gnu style.
After I type M-x c-mode They can be load correctly. Or I open emacs in terminal and then C-x C-f load the file, those configuration will also be loaded.
Is there any reason for this?
I also tried putting
(c-set-style "linux")
in configuration directly.
They work with emacs filename. But when I open non cpp file there will be an error says can't apply c-set-style on that file.
From the CC-mode manual:
When you create a new buffer, its style will be set from c-default-style.
From the Emacs manual
You can also set the variable c-default-style to specify the default style for various major modes
From the documentation of the c-set-style function:
Set the current buffer to use the style STYLENAME.
This means that any call to c-set-style must ensure that the current buffer is the one you want to set the style for; placing such a call at the toplevel in your init file is then definitely a bad idea.
The simplest fix is just to put (setq c-default-style "linux") at the top level, and then wrap it up in an eval-after-load if you really need one. The variable is intended for this, and hooks should be reserved to control more precisely the style of the current file if you need it (as is suggested by c-default-style documentation).
It seems that for me, (eval-after-load "cc-vars" '(setq c-default-style "linux")) works fine: if I open any file covered by CC-mode, the Linux style is applied. Note that c-mode and CC modes are different things, that there exist the c-mode-hook, c++-mode-hook, c-mode-common-hook and they are all different and will run at different times.

Set emacs comment style based on file extension

There are multiple questions along these lines but I haven't been able to find what I need from start to finish. I have been using emacs for a few years but am not used to its customization.
I have a unique file type, identified by its extension, which emacs is not configured for. Its comment style is
<!-- text -->
and I would like to set the variables comment-start and comment-end to the relevant values (which I am assuming will allow me to use comment-region). I don't know the correct way to do this so that it will always be configured when I open this file type but won't affect the default behavior of emacs.
Do I need to make a new major mode for this file type, and then set the variables, or is there an easier way of doing it? An example of the complete requirements for my .emacs file would be much appreciated!
See here. I think this will work:
(add-to-list 'auto-mode-alist
'("\\.extension\\'" . (lambda ()
(setq-local comment-start "<!--")
(setq-local comment-end "-->"))))
Alternatively, if this file extension is well known (or if these files are close enough to a well known syntax), you may be able to just find a major-mode online that does what you want. For example, NXML Mode may just give you the comment syntax you want along with some other useful features.

Emacs: auto-complete-mode on but no auto-suggestions showing (Emacs lisp mode)

I'm puzzled as to why nothing pops up (in Emacs lisp mode) when I begin typing a function name. For example, after typing (def on a new line, I would assume that auto-complete should be showing me a alist of options which includes defun. Am not sure how long the default delay is, but I waited for a few seconds and nothing happened. Any suggestions?
Details regarding my installation process:
Installed using package-install via Melpa
Added the following two lines to my init.el file:
(require 'auto-complete-config)
(ac-config-default)
Confirmed that load-path includes the folder containing the .el files associated with auto-complete. (I have it set-up to recursively add all folders under path/to/my/.emacs.d/.)
Confirmed (via describe-variable) that ac-dictionary-directories includes the correct directories when Emacs starts up. As reference, it includes the following two directories:
ac-dictionary-directories is a variable defined in 'auto-complete.el'.
Its value is ("/home/dchaudh/Dropbox/dchaudhUbuntu/emacs/.emacs.d/elpa/auto-complete-20140824.1658/dict")
Confirmed that auto-complete-mode is on when I open my init.el file, which obviously triggers emacs-lisp-mode (I can see Emacs Lisp in my mode line). The following is included in the summary of active modes (i.e., via describe-mode):
Global-Auto-Complete minor mode (no indicator)
Toggle Auto-Complete mode in all buffers.
With prefix ARG, enable Global-Auto-Complete mode if ARG is positive;
otherwise, disable it. If called from Lisp, enable the mode if
ARG is omitted or nil.
Not a direct answer, but company works fine out of the box in emacs-lisp-mode, so you might want to try that one.
In my experience, other modes (e.g. flyspell) can interfere with auto-complete operation. (There is a workaround for slyspell built into auto-complete but you have to activate it in your. emacs file.)
I'd suggest trying it with an empty. emacs and then gradually adding parts of your configuration back in. You should be able to find the problem that way.

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