how to disable hl-line feature in specified mode - emacs

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)

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?

how to enable Show-Paren mode only for *.el files

How can I enable Show-Paren mode only for *.el files?
I have tried
(add-hook 'emacs-lisp-mode-hook '(lambda()
(show-paren-mode 1)
))
But it still enables Show-Paren mode for all the cases. Even in *scratch* buffer I have Show-Paren mode enabled.
As already said, show-paren-mode is a global minor mode. That said, one might be able to run it only on some buffer with something like:
(show-paren-mode) ;; activate the needed timer
(setq show-paren-mode ()) ;; The timer will do nothing if this is nil
(defun show-paren-local-mode ()
(interactive)
(make-local-variable 'show-paren-mode) ;; The value of shom-paren-mode will be local to this buffer.
(setq show-paren-mode t))
(add-hook 'emacs-lisp-mode-hook 'show-paren-local-mode)
It's untested, it might not work. Looking at the doc in might work, but looking at the code it might work. This might work only with some version of show-paren-mode.
show-paren-mode is a global minor-mode. It means exactly how it sounds.
This is very much by design, as most people (myself included) find this
minor-mode helpful across all buffers. Why do you want to disable it for any
file?
from the documentation
Show Paren mode is a global minor mode. When enabled, any
matching parenthesis is highlighted in show-paren-style' after
show-paren-delay' seconds of Emacs idle time.
Your code is correct. However, you should consider the fact that the *scratch* buffer's major mode is lisp-interaction-mode which derives from emacs-lisp-mode (which is mostly irrelevant) and the mode's definition:
(define-minor-mode show-paren-mode
"Toggle visualization of matching parens (Show Paren mode).
With a prefix argument ARG, enable Show Paren mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil.
Show Paren mode is a global minor mode. When enabled, any
matching parenthesis is highlighted in `show-paren-style' after
`show-paren-delay' seconds of Emacs idle time."
:global t :group 'paren-showing
...)
:global t is the key thing here - the mode is global and is enabled in all buffers regardless of their major mode.
I think you can use
(setq-default show-paren-data-function #'ignore)
(show-paren-mode)
to formally enable the mode but keeping it quiet. And then something like
(defun set-up-emacs-lisp-mode ()
(setq-local show-paren-data-function #'show-paren--default))
(add-hook 'emacs-lisp-mode-hook #'set-up-emacs-lisp-mode)
to enable it in Emacs Lisp buffers. I've not tested this set-up, only the opposite (usually enabled, disabled just in Text Mode).
I used to use (setq-local show-paren-mode nil) but this makes Emacs highlight the braces in Ido promps, so I prefer (setq-default show-paren-data-function #'ignore).

Set custom keybinding for specific Emacs mode

Though I know how to set a global key-binding in Emacs, I find it hard to even Google out the code for a local (minor-mode specific) key-binding. For instance, I have this code in my .emacs:
;; PDFLaTeX from AucTeX
(global-set-key (kbd "C-c M-p")
(lambda ()
(interactive)
(shell-command (concat "pdflatex " buffer-file-name))))
I don't want to set it globally. Is there a function like local-set-key?
I use the following:
(add-hook 'LaTeX-mode-hook
(lambda () (local-set-key (kbd "C-0") #'run-latexmk)))
to have a bind defined for LaTeX mode alone.
To bind a key in a mode, you need to wait for the mode to be loaded before defining the key. One could require the mode, or use eval-after-load
(eval-after-load 'latex
'(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
Don't forget either '—eval-after-load is not a macro, so it needs them.
You need to identify the key map for that mode (for example, LaTeX-mode-map) and use the function define-key. As an example, along with activating outline-minor-mode within LaTeX mode, I have:
(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
In this case the major mode (LaTeX) holds the key binding, but there is also an outline-minor-mode-map.
None of the other answers satisfied my needs. So this may help other people. I wanted Tab to jump to the beginning of the line if I'm in Evil's normal mode (basically this means everywhere in Emacs), but I instead wanted it to cycle between org item states if I am in an org-mode document.
One option was to mess around with separate bindings and constant binding-rebinding whenever I switched buffers (because evil allows only one binding per key in its normal state).
But a more efficient option was to make Tab run my own code which runs the required function based on which major mode the current buffer uses. So if I am in a org buffer, this code runs org-cycle, and otherwise it runs evil-first-non-blank (go to the first non-whitespace character on the line).
The technique I used here can also be used by calling your custom function via global-set-key instead, for people who use regular non-evil Emacs.
For those who don't know Emacs lisp, the first line after the "if" statement is the true-action, and the line after that is the false-action. So if major-mode equals org-mode, we run org-cycle, otherwise we run evil-first-non-blank in all other modes:
(defun my/tab-jump-or-org-cycle ()
"jumps to beginning of line in all modes except org mode, where it cycles"
(interactive)
(if (equal major-mode 'org-mode)
(org-cycle)
(evil-first-non-blank))
)
(define-key evil-normal-state-map (kbd "<tab>") 'my/tab-jump-or-org-cycle)

Auto-complete mode doesn't turn on automatically in ObjC buffers

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.

Emacs disable modes

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.