Tweaking Emacs Electric Behaviour - emacs

I want to override behaviour of electric mode to only make comma electric but not semicolon.
I currently have
(defun c-no-hanging-semi ()
;; TODO How do I get information about if comma or semicolon was pressed?
nil)
(add-to-list 'c-hanging-semi&comma-criteria 'c-no-hanging-semi)
How do I check in c-no-hanging-semi if a comma or semicolon was just pressed?

Unless I've misunderstood the requirement(?), presumably the simplest thing is to stop ; from calling c-electric-semi&comma
(eval-after-load "cc-mode"
'(define-key c-mode-base-map ";" nil))

Related

How to make Emacs not highlight trailing whitespace in term?

I'm using this in my .emacs config
'(show-trailing-whitespace t)
And I'm generally very happy with it.
The only problem is that it highlights whitespace when I'm in 'M-x term' buffer and I have no control over that whitespace. How to prevent this?
Use this:
(add-hook
'term-mode-hook
(lambda() (setq show-trailing-whitespace nil)))

How to get auto indent (not smart indent) in emacs in all modes

I'm new to emacs, and its indenting is driving me up the walls. It's too smart for its own good; it (incorrectly) thinks it knows how I want to format my source, but I don't have time to chase down every setting for every mode for every different language that I write code for; and many of those languages don't have any mode enabled at all.
Here's the behaviour I'd like:
TAB inserts indent
RET inserts a new line then copies the blank characters from the start of the previous line to the first non-blank character, or end of line, whichever comes sooner
DEL (backspace key) in the blank text between line start and first non-blank character / end of line deletes one indent if possible, otherwise single character like normal
No auto-indent on {
No auto-unindent on }
In fact, no smart-ass indenting behaviour anywhere anytime, just copy previous line's indent on RET.
Two variables to be configured per source file format: display tab width, and contents of indent. Preferably these can be configured for random source code formats without having to write a major mode for them, unless writing a major mode is a one-liner in .emacs, consisting of two setqs.
This would get me logical and consistent behaviour across all languages. It would leave the work of formatting the code to me, but that's OK, I've been doing that for 20 years, and I know how to make other macros that make it efficient. More importantly, it saves me from endless fiddling with configuration settings trying to get the automatic behaviour to suit my preferences. And my macros can rely on consistent behaviour so they work correctly in all modes.
Is the above possible? Surely someone else has done this before? Is there some minor mode out there that makes it so?
Here's the code:
(setq tab-width 4)
(defun plain-tab ()
(interactive)
(insert (make-string tab-width ?\ )))
(defun plain-ret ()
(interactive)
(looking-back "^\\( +\\).*")
(newline)
(insert (match-string 1)))
(defun plain-del ()
(interactive)
(backward-delete-char
(if (looking-back (format " \\{%d\\}" tab-width)) tab-width 1)))
(defvar all-the-mode-maps
'(c-mode-map c++-mode-map java-mode-map
js-mode-map emacs-lisp-mode-map
clojure-mode-map))
(require 'cc-mode)
(require 'js)
(require 'clojure-mode)
(eval `(mapc
(lambda(map)
(define-key map [tab] 'plain-tab)
(define-key map [return] 'plain-ret)
(define-key map [backspace] 'plain-del)
(define-key map "{" (lambda()(interactive)(insert "{")))
(define-key map "}" (lambda()(interactive)(insert "}"))))
(list ,#all-the-mode-maps)))

Emacs Auctex custom syntax highlight

I'd like to highlight a new command I created in LaTeX:
\newcommand{\conceito}[3]{
\subsection{#1} (Original: \textit{#2} #3).
}
I use this code in this way:
\conceito{Foo}{Bar}{Bla}
I followed the manual and put this code in my ~/.emacs, but it didn't work:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'((""\\<\\(\\conceito)\\>"" 1 font-lock-warning-face t)))))
What's wrong?
EDIT: Deokhwan Kim originally pointed out that your regexp contains two consecutive double quotes, and that the closing parenthesis ) needs to be escaped with double quotes as well:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\<\\(\\conceito\\)\\>" 1 font-lock-warning-face t)))))
In addition to the points pointed out by Deokhwan Kim, there are also the following two issues:
You need four backslashs instead of two in front of 'conceito': \\\\conceito
The backslash sequence \\< matches the empty string only at the beginning of a word, however, the backslash at the beginning of your new LaTeX command is not considered part of a word, so \\< will not match.
Try this instead:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\(\\\\conceito\\)\\>" 1 font-lock-warning-face t)))
EDIT: Another good observation that Deokhwan Kim made is that in this particular case, you don't really need the parenthesis at all, because you're attempting to match the whole expression anyway. So an alternative to the last line could be:
'(("\\\\conceito\\>" 0 font-lock-warning-face t)))))
The point about the parenthesis is correct, but you could actually extend your regexp to only match when an opening curly brace { follows the word "conceito". But since you don't really want to highlight that brace, using sub-groups defined by parentheses is the way to go:
(add-hook 'LaTeX-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\(\\\\conceito\\)\\s-*{" 1 font-lock-warning-face t)))
Note that since we're testing for a { that follows directly after "conceito" (unless there's whitespace in between), we don't need the test for \\> any more at all.
In general, try M-x re-builder to craft regular expression interactively: you can edit a new regexp in a small buffer and instantly see what is highlighted in the buffer from which you invoked the re-builder.
GNU AUCTeX has a built-in way of defining custom highlighting to user-defined macros. Have a look at the variable font-latex-user-keyword-classes and the AUCTeX documentation.
Here's a simple example (my configuration):
(setq font-latex-user-keyword-classes
'(("shadow-hidden" (("hide" "{")) shadow command)
("shadow-mycomment" (("mycomment" "{")) shadow command)
("shadow-comment" (("comment" "{")) shadow command)))
This will show the contents of \hide{}, \mycomment{}, and \comment{} macros in the dim shadow face.

Arbitrary characters for forward-word/backward-word in Emacs

How to add an arbitrary character (hyphen, for example) to forward/backward-word functionality, so Emacs would handle words like "k-vector" as a single word. The same question about double-click on the word.
Syntax tables define which character is considered a word constituent. You can read about it here.
Emacs's notion of "word" is rather fixed and doesn't quite match what you want. You can mess with the syntax-table, but it might break functionality of the major-mode.
AFAICT what you want is to handle what Emacs calls "symbols" rather than "words". E.g. use forward-symbol and backward-symbol commands.
The easiest way to use those other commands might be to enable superword-mode.
Add following to your .emacs file to make hyphen included in words for every major mode:
(add-hook 'after-change-major-mode-hook
(lambda ()
(modify-syntax-entry ?- "w")))
You can use this code it use regular expression it use space or parents as word separator
(defun backward-word-with-dash ()
(interactive)
(search-backward-regexp "[ ()][A-Za-z\-]+ *")
(forward-char))
(defun forward-word-with-dash ()
(interactive)
(search-forward-regexp " *[A-Za-z\-]+[ ()]")
(backward-char))
(global-set-key "\M-b" 'backward-word-with-dash)
(global-set-key "\M-f" 'forward-word-with-dash)
if you want other characters just add it to the regex

Prevent Abbrev expansion after certain symbols in Emacs

Is there a way to prevent automatic expansion of an abbreviation in the built-in abbrev-mode after certain symbols? E.g. I want my abbrev to expand after whitespace, newline, comma etc., but not after a dash or underscore.
I know I can hit C-q before typing the (say) underscore, but an automatic solution would be much nicer since this occurs for me very often.
There are some abbrev hooks in the manual, but since I am a total beginner with Elisp I don't see an obvious solution...
Thank you very much!
Make underscore be a word-constituent character for the current mode. From the Emacs manual, node Expanding Abbrevs:
[A]ny character that is not a word constituent expands an abbrev, and any word-constituent character can be part of an abbrev.
Use function modify-syntax-entry to modify the syntax class of _, to make it a word constituent:
(modify-syntax-entry ?_ "w")
This solution is useful only if it is not otherwise bothersome for _ to be a word-constituent character. Do you want _ to act as if it is a part of a word or not? That's the first question.
OK, so a hint of the solution was already in the question itself. This is what works for me:
(defun protect-underscore ()
(interactive)
(insert "_"))
(defun protect-dash ()
(interactive)
(insert "-"))
(defun protect-equal ()
(interactive)
(insert "="))
(global-set-key (kbd "_") 'protect-underscore)
(global-set-key (kbd "-") 'protect-dash)
(global-set-key (kbd "=") 'protect-equal)
I am sure there has to be a more elegant solution... thanks goes to Magnar.