I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.
Related
I have strange thing in my Emacs and I can't locate it, everytime I switch a buffer I get message with major mode name even when I call the function I get minibuffer-inactive-mode
The only global function (for all modes) in my .emacs file (I think) is this:
(add-hook 'after-change-major-mode-hook (lambda ()
(if (not (memql (intern (major-mode))
'(fundamental-mode
erc-mode
text-mode
sql-mode)))
(local-set-key (kbd "RET")
'new-line-and-indent-fix))))
How to find the place that add this annoying thing? What different hook can be executed on each mode?
There is no major-mode function in vanilla Emacs. Whatever that function is in your config, it's probably responsible for displaying the message you're seeing.
You want to fix your code (as per Stefan's comment), but you probably also want to look into that non-standard function:
M-x find-function RET major-mode RET
I am configuring AUCTeX in emacs.
Most of the configurations are put in a LaTeX-mode-hook. When I open a main.tex file, I notice that the major mode is latex-mode and my hooked configurations are not activated. I have to M-x Tex-latex-mode to activate them. But the major-mode is still latex-mode.
(add-hook 'LaTeX-mode-hook
(lambda ()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auctex
(setq TeX-auto-save t)
(setq TeX-parse-self t)
))
So I would like to know what is the difference of these modes and how can I turn on AUCTeX automatically when I open a *.tex file.
The modes provided by AUCTeX are listed at https://www.gnu.org/software/auctex/manual/auctex.html#Modes-and-Hooks and they are
plain-TeX-mode
LaTeX-mode
ams-TeX-mode
ConTeXt-mode
Texinfo-mode
docTeX-mode
Instead,
tex-mode
plain-tex-mode
latex-mode
slitex-mode
doctex-mode
(note the different capitalization) are the major modes provided by the TeX mode package shipped with Emacs.
If you want to open all *.tex files with AUCTeX LaTeX mode add this to your .emacs:
(add-to-list 'auto-mode-alist '("\\.tex$" . LaTeX-mode))
Actually, this shouldn't be necessary, because AUCTeX defines the tex-mode.el mode names as alias of its own modes.
TLDR: Use latex-mode or LaTeX-mode (they mean the same thing), no need to change auto-mode-alist, and use LaTeX-mode-hook for hooking into AucTeX.
Setting up AucTeX can be quite confusing, because it uses advice to override Emacs' built-in support for TeX and friends.
So, after installing AucTeX from ELPA, you should see the following in C-h f latex-mode:
This function has :override advice: ‘TeX-latex-mode’.
Same for all the other tex modes, though the list of modes that AucTeX overrides depends on the value of the TeX-modes variable.
The function LaTeX-mode is not defined in AucTeX (any more?): it's defined in core Emacs, with a cryptic comment about compatibility:
;; The following three autoloaded aliases appear to conflict with
;; AUCTeX. However, even though AUCTeX uses the mixed case variants
;; for all mode relevant variables and hooks, the invocation function
;; and setting of `major-mode' themselves need to be lowercase for
;; AUCTeX to provide a fully functional user-level replacement. So
;; these aliases should remain as they are, in particular since AUCTeX
;; users are likely to use them.
;; Note from Stef: I don't understand the above explanation, the only
;; justification I can find to keep those confusing aliases is for those
;; users who may have files annotated with -*- LaTeX -*- (e.g. because they
;; received them from someone using AUCTeX).
;;;###autoload
(defalias 'TeX-mode #'tex-mode)
;;;###autoload
(defalias 'plain-TeX-mode #'plain-tex-mode)
;;;###autoload
(defalias 'LaTeX-mode #'latex-mode)
What this all means is that, at least in 2021, you do not need to change auto-mode-alist to use AucTeX; just installing it is enough for it to override Emacs' builtin functionality.
Unfortunately, there's one last source of confusion. Even though LaTeX-mode is now mostly just a useless alias for latex-mode, it turns out that code in AucTeX that overrides latex-mode does not call latex-mode-hook (it calls LaTeX-mode-hook, which is different. So the LaTeX- variables, which are the AucTeX ones (as opposed to the lowercase ones that are builtin with Emacs), are still useful.
I'm using emacs-snapshot (24.2.50) on Xubuntu 12.10. For completion I am currently using auto-complete (v1.4).
I have a pretty nice setup of auto-complete with various sources, including the semantic source for code completion of my c++ programming. However, I'd like to switch to predictive-mode for completion of LaTeX documents (I'm using AUCTeX for all LaTeX related stuff).
My current auto-complete config (for LaTeX; I omitted all non-LaTeX config) looks like this:
(require 'auto-complete-latex)
(require 'ac-math)
(add-to-list 'ac-modes 'latex-mode) ; make auto-complete aware of {{{latex-mode}}}
(defun ac-latex-mode-setup () ; add ac-sources to default ac-sources
(setq ac-sources
(append '(ac-source-math-latex ac-source-latex-commands)
ac-sources))
)
(add-hook 'LaTeX-mode-hook 'ac-latex-mode-setup)
After adding predictive to the load-path and doing the auto-load stuff as described here I tried to disable auto-complete for LaTeX-mode and enable predictive-mode in turn by exchanging the above code by this:
(defun ele/latex-mode-completion-setup ()
(auto-complete-mode -1)
(predictive-mode 1)
)
(add-hook 'LaTeX-mode-hook 'ele/latex-mode-completion-setup)
Unfortunately this doesn't work as expected: auto-complete is actually disabled but predictive-mode is not enabled and instead of using AUCTeX the build-in tex-mode is used.
I have uploaded all LaTeX related config here. Note that this is loaded after the above completion setup, but this is the case for the old auto-complete based setup as well. Also note that exchanging that order does not make a difference as far as I can tell. Furthermore I found that simply commenting the auto-complete config (first snippet I posted) results in the same behaviour: tex-mode is used instead of AUCTeX.
I don't want to switch to predictive-mode for all modes but I really like many features it provides when doing LaTeX editing.
Any suggestions?
(remove-hook
might clear the hook, not to run unwanted stuff - just a suggestion indeed
I'm trying to enable auto-complete-mode whenever a .go file is loaded through go-mode. It works if I invoke auto-complete-mode manually for Go source files, but when I tried adding it to .emacs as below, it doesn't work:
(add-hook 'go-mode-hook auto-complete-mode)
I've tried a few variations around it but none seem to work. Following is what the Go-Mode snippet currently looks like in my .emacs:
;; Load Go Mode
(require 'go-mode-load)
(add-hook 'go-mode-hook 'auto-complete-mode)
I tried creating my own hook function like this:
;; Load Go Mode
(require 'go-mode-load)
(defun auto-complete-for-go ()
(auto-complete-mode 1))
(add-hook 'go-mode-hook 'auto-complete-for-go)
I also tried including the hook in go-mode-load.el and go-mode.el, as well as calling auto-complete-mode like this:
(auto-complete-mode t)
(provide 'go-mode)
Doesn't work either way. I also added the go-mode-hook to auto-complete-default function like so:
(defun ac-config-default ()
(setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
(add-hook 'go-mode-hook 'ac-common-setup)
;; Other hooks
(global-auto-complete-mode t))
That doesn't work either. What's the best way to trigger a command just after a major mode is enabled for a buffer?
Here is workaround for now:
(add-to-list 'ac-modes 'go-mode)
I fixed the problem in v1.4 branch with the following commits.
Add go-mode to ac-modes
Add go-mode dictionary
Which variations have you tried? It should work if you add a single-quote in front of auto-complete-mode:
(add-hook 'go-mode-hook 'auto-complete-mode)
Without this quote, auto-complete-mode is interpreted as a variable and the value of that variable is added to go-mode-hook. For this to make sense, such a variable should contain a function reference as its value. Most likely though there will be no variable named auto-complete-mode and Emacs will complain.
By adding a quote, you tell Emacs that this is not a variable, but the actual function you want the hook to call. See also here and here.
I just got fixme-mode.el, and loaded it. It works just fine on C, Lisp and other languages. But it doesn't work on Javascript.
(defcustom fixme-modes '(erlang-mode java-mode c-mode emacs-lisp-mode jde-mode
scheme-mode python-mode ruby-mode cperl-mode
slime-mode common-lisp-mode c++-mode d-mode
js2-mode haskell-mode tuareg-mode lua-mode
pascal-mode fortran-mode prolog-mode asm-mode
csharp-mode sml-mode javascript-mode html-mode)
"The modes which fixme should apply to"
:group 'fixme-mode)
I even added javascript-mode and html-mode to the list, but it still doesn't work.
Is there a way I can make it work for all modes? Is there like an "emacs-mode" that includes all others? If not, why isn't it working on .js files?
Thank you!
if you are emacs emacs inbuilt javascript mode (I guess you are using that) that is js-mode not javascript-mode
add this .emacs file(~/.emacs.d/init.el)
(add-to-list 'fixme-modes 'js-mode)
Generally you don't edit defcustom directly in the source code. .emacs is the one for your customizations
UPDATE:
To make fixme-mode to be available for all buffers we should define a global-minor-mode
(defun turn-on-fixme-mode ()
(fixme-mode 1))
(define-global-minor-mode global-fixme-mode
fixme-mode turn-on-fixme-mode
:group 'fixme-mode)
now call global-fixme-mode