Emacs disable modes - emacs

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.

Related

Can't properly disable flyspell in org mode

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?

Auto-complete with go-mode

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.

how to disable hl-line feature in specified mode

i enable hl-mode in global scope with the following code.
(global-hl-line-mode t)
to turn off hl-line feature in a specified mode. i do it with the following code.
(add-hook 'org-mode-hook (lambda () (global-hl-line-mode 0)))
but it turns off the hl-line feature for global scope.
how to disable hl-line feature in a specified mode?
There is often information and documentation in the Commentary
section in the source file.
[...]
;; 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.
[...]
Thus, you can put something like this in your .emacs.
(global-hl-line-mode)
(make-variable-buffer-local 'global-hl-line-mode)
(add-hook 'some-mode-hook (lambda () (setq global-hl-line-mode nil)))
...
use hl-line-mode insted of global-hl-line-mode.
EDIT: You're right. This doesn't work.
The commentary says that the global-mode isn't meant to be used. I take it to
mean that you can't selectively disable it once enabled.
I enable the hl-line-mode in major-mode hooks where I need it.
write the following line in your dotEmacs file, and use f5 to toggle the hl-line-mode
(global-set-key [f5] 'hl-line-mode)

emacs auto-fill-mode doesn't initialise

I want to enable line-wrapping without having to type 'M-x auto-fill-mode' everytime I start emacs. I've tried putting (setq auto-fill-mode 1) and (auto-fill-mode 1) in the .emacs file, but neither work. Why is this, and how do I fix it?
Thanks
It is a minor-mode so you need to enable it for the modes where you want it used. So, for example, if you want auto-fill-mode enabled in text mode, you need to add the following to your .emacs file:
(add-hook 'text-mode-hook '(lambda ()
(auto-fill-mode 1)))
auto-fill-mode is a minor mode so (setq auto-fill-mode 1) wont start it.
You can add a hook to start auto-fill-mode with the text-mode (with which it is normally used) or any other mode you normally use it with, by doing
(add-hook 'text-mode-hook 'turn-on-auto-fill)
Alternatively, if you want the auto-fill-mode on for all the files you edit. You can start it when any type of file is opened with:
(setq auto-mode-alist (cons '("*" . auto-fill-mode) auto-mode-alist))
But having it always on is irritating at times, so its better to bind the starting of the mode to a familiar key sequence
(global-set-key (kbd "C-c q") 'auto-fill-mode)

emacs: visual-line-mode and fill-paragraph

I am now using Emacs 23 with visual-line-mode turned of for text editing but keep hitting M-q out of habit (thus adding hard-wrapping line endings...). I wonder if there is a way to add a conditional to disable fill-paragraph (or remove the binding to M-q) for modes in which visual-line-mode is turned on, but to re-enable it for those in which I am still using the auto-fill-mode? Thanks!
(defun maybe-fill-paragraph (&optional justify region)
"Fill paragraph at or after point (see `fill-paragraph').
Does nothing if `visual-line-mode' is on."
(interactive (progn
(barf-if-buffer-read-only)
(list (if current-prefix-arg 'full) t)))
(or visual-line-mode
(fill-paragraph justify region)))
;; Replace M-q with new binding:
(global-set-key "\M-q" 'maybe-fill-paragraph)
Instead of using global-set-key, you can also rebind M-q only in specific modes. (Or, you could change the global binding, and then bind M-q back to fill-paragraph in a specific mode.) Note that many modes are autoloaded, so their keymap may not be defined until the mode is activated. To set a mode-specific binding, I usually use a function like this:
(add-hook 'text-mode-hook
(defun cjm-fix-text-mode ()
(define-key text-mode-map "\M-q" 'maybe-fill-paragraph)
(remove-hook 'text-mode-hook 'cjm-fix-text-mode)))
(The remove-hook isn't strictly necessary, but the function only needs to run once.)
you can use an advise for this.
For your .emacs:
(defadvice fill-paragraph (around disable-for-visual-line-mode activate)
(unless visual-line-mode
ad-do-it))
This will change fill-paragraph to do nothing when visual-line-mode is on. You can also add an error if you prefer that.
visual-line-mode has its own keymap: visual-line-mode-map. I recommend rebinding M-q only in that keymap.
The map is defined as part of startup, so you don’t need eval-after-load. Just disable the binding in that mode:
(define-key visual-line-mode-map [remap fill-paragraph] 'ignore)