Flyspell: is it possible to ignore words based on pattern? - emacs

I would really like to disable flyspell checking of camel-cased words - most of the times they aren't valid English. Is there any way to do that?

You could set the variable flyspell-generic-check-word-predicate to a function that returns nil if point is at a camel-cased word.
Note that this variable automatically becomes buffer-local when set, so setting it with a plain setq in your .emacs file wouldn't work. Set it from a mode hook instead, e.g.:
(add-hook 'java-mode-hook
(lambda ()
(setq flyspell-generic-check-word-predicate 'my-new-function)))
(replacing my-new-function with the name of your camel-case detecting function)

Related

Disabling input method inside a math formula in Emacs with AUCTeX

I am typing a math document in Russian using Emacs 24.3.1 and AUCTeX 11.87. I am using the russian-computer input method to type Russian. It would be very convenient to disable this input method inside math delimiters such as \( \) so that inside formulas I automatically switch to typing English without pressing Ctrl-\. For example, I could type "Рассмотрим формулу \(\)" ("Consider the formula \(\)"), put the point between the parentheses and start typing \forall. Currently, I would be typing \ащкфдд, but I would like Emacs to recognize that I am inside a formula and switch off the Russian input method.
I suspect that this can be done using post-self-insert-hook and texmathp from AUCTeX, but I am not sure if this is the most elegant method.
I found solution to the problem at http://lists.gnu.org/archive/html/help-gnu-emacs/2009-08/msg00200.html The code works almost as expected. But drawback is that one can't switch to English from e.g. Russian outside of math delimiters.
This function does the job: switch between english and your current input method.
(defun LaTeX-dynamic-input-method-toggle-maybe ()
(when (or (and current-input-method
(texmathp))
(and (not current-input-method)
(not (texmathp))))
(toggle-input-method)))
"Define a minor-mode, so this behaviour can be conviniently
enabled/disabled. The minor-mode puts the above function on the
`post-command-hook', so that the above function gets called
everytime you do something (e.g. move point)."
(define-minor-mode LaTeX-dynamic-input-method
"Dynamically disable input-method in math-mode."
nil nil nil
(if LaTeX-dynamic-input-method
(add-hook 'post-command-hook 'LaTeX-dynamic-input-method-toggle-maybe nil t)
(remove-hook 'post-command-hook 'LaTeX-dynamic-input-method-toggle-maybe t)))
Enable the mode in LaTeX-mode.
(add-hook 'LaTeX-mode-hook 'LaTeX-dynamic-input-method)

Why is font-lock-keyword-face highlighting text within double quotes in Emacs?

I am trying to set up a major mode in Emacs where I would like to highlight certain keywords.
Using a template from this page: http://ergoemacs.org/emacs/elisp_syntax_coloring.html I tried:
(setq testing-font-lock-keywords
`((font-lock-keyword-face)
))
(define-derived-mode testing-mode fundamental-mode
"testing file mode"
"Major mode for editing test files"
(setq font-lock-defaults '(testing-font-lock-keywords))
(setq mode-name "testing")
)
(provide 'testing-mode)
If I use this mode on a simple test file, and type "hello" the text hello is marked in different color. That is, any text within double quotes is highlighted. Why is this happening?
I think it is related to the variable font-lock-keyword-face. But if I type C-h v and font-lock-keyword-face it says:
font-lock-keyword-face is a variable defined in `font-lock.el'.
Its value is font-lock-keyword-face
Update
It seems like it is not related to font-lock-keyword-face anyway, since defining testing-font-lock-keywords like:
(setq test-keywords '("TEST"))
(setq testing-font-lock-keywords
`((,test-keywords)))
gives the same behavior.
This is directed by variable `font-lock-syntactic-face-function'
Emacs fontifies two things: 1) Syntactic, this includes comments and strings as declared in the syntax table. 2) Keywords.
Typically, you want the first phase to run, but you might need to update your syntax table to match the syntax of the language.
In addition, font-lock keywords can be written so that they overwrite existing colors, so that you can highlight text inside pre-colored comments and string. See the OVERRIDE flag in font-lock-keywords.

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 set defcustom variable

There is cperl-mode
and the setting I see in source:
(defcustom cperl-indent-parens-as-block nil
"*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
but for trailing \",\" inside the group, which won't increase indentation.
One should tune up `cperl-close-paren-offset' as well."
:type 'boolean
:group 'cperl-indentation-details)
I was trying to use (custom-set-variable '(cperl-indent-parens-as-block t)) but that doesn't work so how can I change this to t as global setting?
The function is called ...-variables:
(custom-set-variables '(cperl-indent-parens-as-block t))
or you can just use setq, since the variable doesn't have a setter defined:
(setq cperl-indent-parens-as-block t)
Easiest way to do it is M-x customize-variable cperl-indent-parens-as-block.
This shows you a nice menu with the possible values etc, and adds the values to your init.el (or emacs.el) afterwards.

how to change buffer-local variable for a major mode in Emacs?

Generally, how can I customize the value of a buffer-local variable in Emacs? For example, the variable w3m-lnum-mode is buffer-local, if I set (setq w3m-lnum-mode t) in .emacs, its value in a w3m mode buffer is still nil. How could I set it to t in w3m major mode?
Major modes have a hook variable for this sort of thing. Look for w3m-mode-hook.
(defun my-w3m-hook nil
(setq w3m-lnum-mode t))
(add-hook 'w3m-mode-hook #'my-w3m-hook)
The indirection to hook a separate function is not absolutely necessary, but simplifies the management of the hook functionality (otherwise you'd have to restart Emacs or jump through several hoops to add something to an existing hook; now all you need to do is evaluate a new defun of the function called from the hook).
You can set a default like so:
(setq-default w3m-lnum-mode t)
For fine-grained control, use a hook as RNAer suggests. As far as I can tell though, this is not a normal local variable but a minor mode variable. You actually probably want to do (w3m-lnum-mode 1).