Arbitrary characters for forward-word/backward-word in Emacs - 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

Related

How to instruct Emacs auto-capitalize-mode to automatically lowercase any word that follows e.g. or i.e.?

Emacs auto-capitalize-mode misinterprets the words i.e. and e.g. to signify the end of a sentence, and, accordingly, erroneously capitalizes any word that follows them.
Does anyone have a function that can be called by entering, say, eg or ie, that will insert the characters e.g. and i.e. and then automatically lowercase whatever word gets typed next?
Bonus: Do the same thing... for ellipses.
Add this to your .emacs:
(setq auto-capitalize-predicate
(lambda () (not (looking-back
"\\([Ee]\\.g\\|[Ii]\\.e\\)\\.[^.]*" (- (point) 20)))))
Remember that the I in i.e. will be capitalized to I.e if your
auto-capitalize-words variable is set to contain “I”.
(setq auto-capitalize-words '()) This sets it to nothing.
Here’s a version that also deals with ellipses:
(setq auto-capitalize-predicate
(lambda () (not (looking-back
"\\([Ee]\\.g\\|[Ii]\\.e\\|\\.\\.\\)\\.[^.]*" (- (point) 20)))))
But you might want to look into some abbrev magic that turns three periods into a unicode ellipsis instead. It's up to you.
From auto-capitalize.el:
;; To prevent a word in the `auto-capitalize-words' list from being
;; capitalized or upcased in a particular context (e.g.
;; "GNU.emacs.sources"), insert the following whitespace or
;; punctuation character with `M-x quoted-insert' (e.g. `gnu C-q .').
I use it and it is a comfortable approach.

How to select string that is connected with dot on Emacs

I want to select the whole word connected with dot.
Is there any function in Emacs doing this.
Example:
123.456.[7]89
Cursor is on "7".
Apply "???function".
Region "123.456.789" is selected.
[123.456.789]
Is there "???function" in Emacs?
I use expand-region:
(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)
so I press C-= once and it selects 789; I press it a second time and it selects 123.456.789. It works nice with strings, lines, statements from different languages.
Home: https://github.com/magnars/expand-region.el
Install it with ELPA (M-x list-packages).
ps: http://wikemacs.org/index.php/Elpa
Here's a solution:
(defun mark-whole-word ()
(interactive)
(let ((table (syntax-table)))
(modify-syntax-entry ?. "w" table)
(with-syntax-table table
(backward-word)
(set-mark (point))
(forward-word))))
The key here is modifying the syntax.
So if you replace ?. with ?- above, you can mark similarly
123-456-789.
#abo-abo's answer is good.
Just as another data point, you can also use command thing-region (from library thing-cmds.el -- Thing-At-Point Commands) for this. It prompts you for a type of THING to select. Just accept the default THING type, sexp, and you get what you requested in this case.

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.

Change the definition of paragraph (e.g. in org-mode)

I'd like to use mark-paragraph (also for movement with forward/backward-paragraph) in org-mode buffers in the same way as in other major modes, i.e. to mark a continuous region delimited by empty lines. This should apply also to headings, list items, lines starting with '#' etc. – i.e. I'd like for the purposes of paragraph editing to everything be treated as regular text.
Is this possible?
See the variables paragraph-start and paragraph-separate, and possibly also the use-hard-newlines function which is related (but probably not actually relevant in this case).
(defun use-default-paragraph-delimiters ()
(setq paragraph-start (default-value 'paragraph-start)
paragraph-separate (default-value 'paragraph-separate)))
(add-hook 'org-mode-hook 'use-default-paragraph-delimiters)
Edit: Admittedly, org-mode might depend upon its paragraph definitions for more than just interactive marking and moving, so here is a more targeted approach for customising the paragraph definitions for those commands only when called interactively using their key bindings.
(defmacro with-default-paragraph-definition (&rest body)
"Evaluate body forms using the default definition of a paragraph."
`(let ((paragraph-start (default-value 'paragraph-start))
(paragraph-separate (default-value 'paragraph-separate)))
,#body))
(defalias 'my-org-mark-paragraph 'mark-paragraph)
(defadvice my-org-mark-paragraph
(around my-org-mark-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defalias 'my-org-forward-paragraph 'forward-paragraph)
(defadvice my-org-forward-paragraph
(around my-org-forward-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defalias 'my-org-backward-paragraph 'backward-paragraph)
(defadvice my-org-backward-paragraph
(around my-org-backward-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defun my-org-paragraph-overrides ()
"Use the default paragraph definitions in org-mode
when marking or moving by paragraph."
(local-set-key [remap mark-paragraph] 'my-org-mark-paragraph)
(local-set-key [remap forward-paragraph] 'my-org-forward-paragraph)
(local-set-key [remap backward-paragraph] 'my-org-backward-paragraph))
(add-hook 'org-mode-hook 'my-org-paragraph-overrides)
You can try customising the paragraph-start variable. I'm not sure
what would be appropriate here, org sets it to something quite elaborate
as you see in the quoted docstring below. Setting it to the default
might work, or you could try use-hard-newlines as mentioned in the quote below.
paragraph-start is a variable defined in `paragraphs.el'.
Its value is
"\f\|[ ]$\|\+ \|[ ]#\|\([ ]\([-+]\|\(\([0-9]+\)[.)]\)\)\|[ ]+\*\)\([ ]+\|$\)\|[ ]*[:|]\|\$\$\|\\\(begin\|end\|[][]\)"
Original value was "\f\|[ ]*$"
Local in buffer coding.org; global value is "\f\|[ ]*$"
This variable is safe as a file local variable if its value
satisfies the predicate `stringp'.
Documentation:
Regexp for beginning of a line that starts OR separates paragraphs.
This regexp should match lines that separate paragraphs
and should also match lines that start a paragraph
(and are part of that paragraph).
This is matched against the text at the left margin, which is not necessarily
the beginning of the line, so it should never use "^" as an anchor. This
ensures that the paragraph functions will work equally well within a region
of text indented by a margin setting.
The variable `paragraph-separate' specifies how to distinguish
lines that start paragraphs from lines that separate them.
If the variable `use-hard-newlines' is non-nil, then only lines following a
hard newline are considered to match.

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.