Can't use (setq *-mode) instead of (global-*-mode) in Emacs - emacs

I use (global-hl-line-mode) to enable hl-line-mode, But i want to use
it in certain mode like cc-mode, so i add this line to mode-hook,
(setq hl-line-mode t), it doesn't work, i enable hl-line-mode use
M-x, it shows disabled, which means at first, it's really enabled, but
i can't see any highlight.
Same problem occurs to linum-mode, maybe there are others.
Anyone knows what's wrong with it?
Thanks.

In general, it's a good idea to turn the mode on through the function call, rather than just setting the variable. The function call will set the variable for you, and likely do some other work.
Try this:
(add-hook 'c-mode-common-hook
(lambda () (hl-line-mode 1)
(linum-mode 1)))

Related

Disabling 'linum-mode' for speedbar when global 'linum-mode' is active

I have linum-mode enabled globally in my Emacs configuration. Being enabled globally means it is also applied to the speedbar which is undesirable.
The only suggestion I found for this issue, was in the archived Emacs help mailing list, which suggests the following speedbar-mode-hook:
(add-hook 'speedbar-mode-hook (lambda () (linum-mode -1)))
Unfortunately adding this to my configuration doesn't have the desired effect and the speedbar still has line numbers.
Edit: the above add-hook seems to work correctly after all, at least for Emacs >= 24.3. Leaving question for reference purposes since there isn't any other relating to this matter on StackOverflow.
You can use speedbar-before-popup-hook hook for achieving what you want:
(add-hook 'speedbar-before-popup-hook (lambda () (linum-mode -1)))
I am not sure why the generic mode hook is not working, though.

emacs only delete-trailing-whitespace while saving in programming mode

following line removes all training white space while saving.
(add-hook 'write-file-hooks 'delete-trailing-whitespace)
but I want to hook this feature only when i'm in programming mode, so i did
(defun nuke_traling ()
(add-hook 'write-file-hooks 'delete-trailing-whitespace)
)
(add-hook 'prog-mode-hook 'nuke_traling)
which doesn't is not stopping which are not in programming mode.
Making the hook variable buffer-local has been mentioned. Don't do that. Or rather, don't do it using make-local-variable.
The normal hook mechanisms have buffer-local support built in -- that's the purpose of the LOCAL argument to add-hook. When the hook is run, it runs both the global and the buffer-local values.
So taking the example code in the question, you could change it to use:
(add-hook 'write-file-hooks 'delete-trailing-whitespace nil t)
And then delete-trailing-whitespace would be called whenever write-file-hooks was run, but only in the buffers in which prog-mode-hook had run.
However there are better ways to achieve this.
I agree with Drew that you are better to test whether your mode is derived from prog-mode, and with juanleon that before-save-hook is a better hook to use. So you might do something like:
(add-hook 'before-save-hook 'my-prog-nuke-trailing-whitespace)
(defun my-prog-nuke-trailing-whitespace ()
(when (derived-mode-p 'prog-mode)
(delete-trailing-whitespace)))
But what I actually recommend is using either ws-trim or ws-butler to take care of this in a smarter way.
Blindly removing all trailing whitespace from a file is a great way to wind up committing loads of unrelated lines to a version-control repository. Both of the libraries mentioned will ensure that your own commits are free of trailing whitespace, without also introducing unwanted modifications elsewhere in the file.
write-file-hooks is obsolete since Emacs-22, replaced by write-file-functions. But this hook is a bit delicate to use (because it can also be used to perform the write), so I recommend you use before-save-hook instead. And to make it apply only to the current buffer, just pass a non-nil value for the local argument of add-hook, as in:
(defun nuke_traling ()
(add-hook 'before-save-hook #'delete-trailing-whitespace nil t))
(add-hook 'prog-mode-hook #'nuke_traling)
Yes, because as soon as you enter a prog-mode mode, you add the function to write-file-hooks, where it remains. And that hook applies to writing any file, regardless of the mode of its buffer.
Instead of putting that simple function on the hook, you can add a function that tests the mode and only does the whitespace deletion when it is a mode where you want to do that.
Or else you would need to make write-file-hooks buffer-local (which I doubt you would want want to do --- the hook is used more generally).
Bad way:
(add-to-list 'write-file-functions 'delete-trailing-whitespace)
Better way is using ws-butler:
(straight-use-package 'ws-butler)
(add-hook 'prog-mode-hook #'ws-butler-mode)
ws-butler-mode remove spaces only on changed lines.
You would need to make the variable buffer local:
(defun nuke_traling ()
(make-variable-buffer-local 'write-file-hooks)
(add-hook 'write-file-hooks 'delete-trailing-whitespace))
But I would recommend using before-save-hook instead:
(defun nuke_traling ()
(add-to-list 'before-save-hook 'delete-trailing-whitespace))
write-file-hooks may be risky if used as a file-local variable, and documentation recomends using before-save-hook instead for thing like you want to do.
In emacs 21 or later you can add this hook to a perticular mode like this:
(add-hook 'prog-mode-hook
(lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))

How to get whitespace-mode enabled only for certain modes

I'm trying to get emacs whitespace-mode enabled automatically only in certain modes. According to the documentation, enabling global-whitespace-mode and setting the whitespace-global-modes variable should do exactly that. But I can't get it to work correctly.
In my .emacs.el I have:
(require 'whitespace)
(global-whitespace-mode t)
(setq whitespace-global-modes '(c-mode c++-mode))
but the definition of whitespace-global-modes seems to be ignored; global-whitespace-mode is enabled in every buffer. I know that I've got the variable name correctly, because C-h v whitespace-global-modes tells me:
whitespace-global-modes's value is (c-mode c++mode)
Documentation:
Modes for which global `whitespace-mode' is automagically turned on.
...
So what am I doing wrong? Have I misunderstood the purpose of whitespace-global-modes?
I'm running emacs 23.2.1.
Apparently the meaning of whitespace-global-modes is very different from what you (and I) understand.
How about trying
(require 'whitespace)
(add-hook 'c-mode-hook
(function (lambda ()
(whitespace-mode t))))
and repeating the same thing for c++-mode?
It turns out that the commands in my .emacs.el were (almost) working after all. What confused me was that "WS" appears in the modeline of all buffers, even though only C and C++ buffers were getting the effect of whitespace-mode, as desired.
The other problem was that I had a typo: c++mode rather than c++-mode.

emacs ( recompile -y )

Is it possible to pass a "-yes" flag to the 'recompile' command in emacs?
Excuse my complete lack of (e)lisp know-how. I got sick of going outside Emacs to compile my latex code, so i added the following key binding to my .emacs:
(global-set-key (kbd "<f12>") 'recompile);
Is it possible to automatically answer 'yes' to the following prompt that might appear:
"A compilation process is running; kill it? (yes or no)."
Also, is it possible to make the window that opens and shows the output to scroll to the bottom automatically. The interesting stuff is typically down there. Maybe its possible to chain the following command after recompile: "C-x o, end-of-buffer".
Thanks!
Here's some code to solve your first problem (interrupting the current compilation):
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors (kill-compilation))
(recompile))
For your second problem (scrolling the compilation output), just customize the user setting compilation-scroll-output.
This behaviour is governed by the compilation-always-kill global variable. Customize it via customize-variable and set it to t.
Not sure which version of emacs first had this, but 26 and newer certainly does.
I somehow need to put kill-compilation into a ignore-errors with Emacs 23.2 to get it to work when no process is running. Otherwise works great.
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors
(kill-compilation))
(recompile)
)
Whenever I tried using kill-compilation with latex/pdflatex it did not work. I assume it is because latex does not respond to SIGINT.
Instead I am using the following hack, which first sets the process-kill-without-query bit of the compilation-buffer and then closes it (which kills the running process).
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors
(process-kill-without-query
(get-buffer-process
(get-buffer "*compilation*"))))
(ignore-errors
(kill-buffer "*compilation*"))
(recompile)
)
The other solutions didn't work for me for the same reason as sfeuz, but I didn't like the nuclear option of killing the hardcoded buffer by name.
Here's a short solution that autoanswers yes to that specific question by advising yes-or-no-p:
ftp://download.tuxfamily.org/user42/compilation-always-kill.el
(source: http://www.emacswiki.org/CompilationMode)

Disable auto fill mode in noweb-mode

Please for the love of god how can I make Emacs stop auto-filling? I use visual-line-mode, I do not want auto fill. I can turn it off with M-x auto-fill-mode RET but in Noweb mode it gets turned back on when I move into a code chunk and back out again. Please, I just want to globally turn of auto fill mode, it's driving me crazy.
I've tried
(auto-fill-mode 0)
and a bunch of crazy things like
(add-hook 'Rnw-mode-hook '(lambda () (auto-fill-mode 0)))
(add-hook 'latex-mode-hook '(lambda () (auto-fill-mode 0)))
But nothing seems to work. Please help me.
Instead of adding further hooks to your system, you should check if you could remove some to disable auto-fill.
If you see noweb-mode's source code, around line 211 you find this chunk:
(add-hook 'noweb-select-doc-mode-hook 'noweb-auto-fill-doc-mode)
(add-hook 'noweb-select-code-mode-hook 'noweb-auto-fill-code-mode)
To disable auto filling, put the following one or two lines in your dotemacs (depending on whether you want to disable auto-fill in both code and documentation).
(remove-hook 'noweb-select-doc-mode-hook 'noweb-auto-fill-doc-mode)
(remove-hook 'noweb-select-code-mode-hook 'noweb-auto-fill-code-mode)
OK, I worked out a hack that does the trick:
(setq auto-fill-mode -1)
(setq-default fill-column 99999)
(setq fill-column 99999)
If I can't turn off auto-fill mode, at least I can make it harmless by setting it to fill only in column 99999. I'll be annoyed if I type a paragraph with more than 99999 characters, but if that happens I'll have bigger things to worry about...
If you look at simple.el, at the very top text-mode-hook is defined, which is ultimately responsible for turning on this abomination (or at least, it was for me).
To get to simple.el and look for yourself: (C-h f, auto-fill-mode, click / follow simple.el link)
To disable, M-x customize-variable text-mode-hook
Screen shot
Uncheck turn-on-auto-fill. Be sure to click "Save for future sessions" at the top.