I'm trying to configure Emacs to not activate Auto-Fill when editing an XML document.
In my .emacs file, I add a hook so that text mode will have Auto-Fill on by default:
(add-hook 'text-mode-hook 'turn-on-auto-fill)
I have added a directory to my load path:
(add-to-list 'load-path "~/.emacs.d/lisp/")
Inside that directory, I have written a file xml.el for this workstation, and I have tried each of the following in it, to no avail:
(add-hook 'xml-mode-hook 'turn-off-auto-fill)
(add-hook 'xml-mode-hook 'auto-fill-mode)
(remove-hook 'xml-mode-hook 'turn-on-auto-fill)
(remove-hook 'xml-mode-hook 'auto-fill-mode)
How can I disable Auto-Fill in XML mode?
EDIT: It appears this is caused by my text-mode-hook mentioned above. How can I override this hook in nxml-mode?
Ah, nxml-mode derives from text-mode. That's slightly surprising to me (although on closer inspection, it does appear to be standard for markup language modes in Emacs).
In that case you can either disable it again in nxml-mode-hook (as text-mode-hook has already run by that point):
(add-hook 'nxml-mode-hook 'turn-off-auto-fill)
(n.b. you said in the comments that this didn't work for you, but it certainly works for me).
or else just change your text-mode-hook code to something like the following, in order to catch this case before auto-fill is enabled:
(defun my-text-mode-hook ()
"Custom behaviours for `text-mode'."
;; Enable `auto-fill-mode', except in `nxml-mode' (which is derived
;; from `text-mode').
(unless (eq major-mode 'nxml-mode)
(turn-on-auto-fill)))
(add-hook 'text-mode-hook 'my-text-mode-hook)
Related
Usually, I put the confugire .el files in src directory for all kinds of languages. Such as Go, the go-conf.el file:
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-.") 'godef-jump)))
(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd "M-,") 'godef-jump-back)))
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(add-hook 'after-init-hook #'global-flycheck-mode)
(require 'flycheck)
(require 'go-autocomplete)
(require 'auto-complete-config)
(ac-config-default)
)
(provide 'go-conf)
Then, in init.el, I write this line
(require 'go-conf)
Although go-conf can be loaded successfully, emacs launches slowly. It is because that emacs loads go-conf whatever files are opened. I can not tolerate it.
It is better that only when Go file is opened, go-conf is loaded.
I modify the init.el as :
(add-hook 'go-mode-hook '(lambda ()
(require 'go-conf)
(go-conf)
))
But it does not work!!
who can help me?
Your code seems to assume that the whole Emacs only has a single buffer and mode, whereas that is not the case. E.g. (add-hook 'before-save-hook 'gofmt-before-save) affects all buffers, whether they're using go-mode or not. Same for (add-hook 'after-init-hook #'global-flycheck-mode). Emacs is designed such that you can start it once and then edit hundreds of different files at the same time in that one Emacs session. So you should probably rewrite your code along the lines of:
(defun my-go-lang-config ()
(add-hook 'before-save-hook #'gofmt-before-save nil 'local)
(local-set-key (kbd "M-.") 'godef-jump)
(local-set-key (kbd "M-,") 'godef-jump-back)
(add-to-list 'load-path "/usr/local/go/src/github.com/dougm/goflymake")
(require 'go-autocomplete))
(add-hook 'go-mode-hook #'my-go-lang-config)
(require 'auto-complete-config)
(ac-config-default)
(global-flycheck-mode 1)
where the last three lines are part of your "generic" config (not specific to support for the Go language), meaning that you want to use flycheck and auto-complete whenever it's available rather than only in go-mode.
Your code to add to the hook doesn't work because the hook is run only after the mode is turned on, and the mode is not defined until the library is loaded. It makes no sense to load the same library in the mode hook.
If Emacs becomes slow after loading some library, it is probably due to that library. Is is slow after loading the library even if you do not turn the mode on?
You can try byte-compiling the library code. That can sometimes make a big difference in performance. You can use M-x byte-compile to compile a given file.
If compiling does not help, and if you do not seen anything suspect in buffer *Messages* (e.g., warnings that seem like they might be pertinent), then consider contacting the library maintainer, reporting the problem and asking for a remedy.
If go-mode itself is already available (most likely loaded on demand via an addition to auto-mode-alist, which is probably taken care of automatically if it was installed as an ELPA package), and you're just looking to load your custom library at the same time, then you can use eval-after-load:
(eval-after-load 'go-mode
'(require 'go-conf))
Make sure that the parent directory for your go-conf.el library is in the load-path, of course, otherwise require won't find it.
I set up emacs to turn flyspell on by default for text mode using
(add-hook 'flyspell-mode-hook 'flyspell-buffer)
(add-hook 'text-mode-hook 'flyspell-mode)
I would like to disable it automatically in org mode files, and I did manage to disable it using a lambda function in the org mode hook:
(add-hook 'org-mode-hook (lambda () (flyspell-mode -1)))
This works, but the syntax highlight changes that flyspell did still remain, and interfere with org mode's syntax highlight. I also see in the minibuffer that flyspell does run when I visit that file, so apparently it is turned off only afterwards. How do I turn it off in such a way that would leave no trace of it in the org mode file, or better yet not run at all?
You can selectively enable flyspell in text-mode hook by checking the major-mode. The following will enable flyspell in text-mode and its derived modes except org-mode
(add-hook 'flyspell-mode-hook 'flyspell-buffer)
(add-hook 'text-mode-hook (lambda ()
(when (not (equal major-mode 'org-mode))
(flyspell-mode t))))
Regarding the first question (remove the highlightings), the following call should do it:
(flyspell-delete-all-overlays)
However, are you really, really, really sure you want to disable flyspell in Org mode??? Is there a really, really, really good reason for that?
If yes, shouldn't you solve that one, instead of trying the above?
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 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.
I'd like to disable line highlighting in term-mode. I tried this:
(add-hook 'term-mode-hook '(lambda () (global-hl-mode 0)))
but it complains about the symbol being void.
I have this further in my .emacs:
(global-hl-line-mode 1)
I agree with Ashutosh that that may be the source of your symbol error, but I'm not sure that that's the right approach anyways. I'm pretty sure that will disable highlighting everywhere, not just in terminal windows, when you load a terminal window.
I think the right thing is this:
(add-hook 'term-mode-hook '(lambda() (set (make-local-variable 'global-hl-line-mode) nil)))
...I'm going off hl-line.el where it says this:
;; You could make variable `global-hl-line-mode' buffer-local and set
;; it to nil to avoid highlighting specific buffers, when the global
;; mode is used.