I have a local abbrev table set up in a file. I have an abbrev that has the "+" character. By default, abbrev will not expand this abbrev because it sees "+" as a word boundary.
The docs say this can be resolved with the :regexp
property.
I tried it out. Consider this setup:
(set-local-abbrevs '(
("mse" "mean square error" nil)
("wlog" "without loss of generality" nil)
("++" "\\+" nil)
))
(defvar my-abbrev-regexp
"\\([[:word:]]+\\)"
;; "\\([0-9A-Za-z._-]+\\)"
"")
(abbrev-table-put local-abbrev-table :regexp my-abbrev-regexp)
(abbrev-mode)
Let's for a minute forget the abbrev with the "+" character and try to expand "wlog". If I comment out the abbrev-table-put
line, it expands it just fine when I press SPC. If I uncomment it, then I cannot get "wlog" to expand. I tried both regular expressions shown, both with and without the "+" quantifier.
I also tried the suggested solution here, adding the "<" character to the regexp, and it still doesn't expand.
Edit: Apparently set-local-abbrevs
is a custom function I had in my config, taken from the Emacs Wiki. It creates a local abbrev table. Below is the definition.
(defun set-local-abbrevs (abbrevs)
"Add ABBREVS to `local-abbrev-table' and make it buffer local.
ABBREVS should be a list of abbrevs as passed to `define-abbrev-table'.
The `local-abbrev-table' will be replaced by a copy with the new
abbrevs added, so that it is not the same as the abbrev table used
in other buffers with the same `major-mode'."
(let* ((bufname (buffer-name))
(prefix (substring (md5 bufname) 0 (length bufname)))
(tblsym (intern (concat prefix "-abbrev-table"))))
(set tblsym (copy-abbrev-table local-abbrev-table))
(dolist (abbrev abbrevs)
(define-abbrev (eval tblsym)
(car abbrev)
(cadr abbrev)
(caddr abbrev)))
(setq-local local-abbrev-table (eval tblsym))))
I'll examine the definition in detail later, but a quick glance isn't finding an obvious cause for my problems.
Update: OK, Tyler's solution works if I put the string verbatim in abbrev-table-put
, but not if I put it in the my-abbrev-regexp
variable. Likely the number of backslashes?
Now that I have "wlog" working, I still can't get ++ to expand.My regexp is \\<\\([+[:word:]]+\\)
. Ideas?
set-local-abbrevs
? It does not seem to be defined in my Emacs.mse
,wlog
) work fine. Now to extend it...abbrev--before-point
(looking-back re (line-beginning-position))
is non-greedy, so it will stop e.g. at theg
ofwlog
instead of consuming as much as possible (i.e. all ofwlog
). You can test by changing that line to add at
as a third argument which makes the search greedy. I thought this was a bug at first, but as the previous comments make clear, I think you have to specify the regexp as in @Tyler's answer to the linked question.