This function is supposed to replace matches with a shortened version using multiple rules rather than just a single one. For the word counter
, coun
gets replaced by k
, resulting in the shortened word kter
.
But I am struggling for the case where the replacement has to be done at the end. So that family
gets replaced by faml
(replacing ily
match with l
).
(defun shorten-word-b ()
"Shortens a word according to specific rules."
(interactive)
(let* ((bounds (bounds-of-thing-at-point 'word))
(s (car bounds))
(case-fold-search nil)
(p (point-marker)))
(when s
(goto-char s)
(cond
;;-----------------------------------------------
((looking-at (regexp-opt
'("cog" "col" "com" "con" "cor" "cum" "coun") "\\<\\("))
(replace-match "k"))
;;-----------------------------------------------
((looking-at (regexp-opt
'("ley" "ily" "ly") "\\)\\>"))
(replace-match "l"))
;;-----------------------------------------------
(t nil))
(goto-char p))
(set-marker p nil)))
Here are some examples
Before: Counter
After: kter
Before: family
After: faml
Where point can be placed anywhere, with next character being a character of word.
bounds-of-things-at-point
returns a dotted pair whosecar
is the beginning of thething
-at-point and whosecdr
is the end of it (in this case thething
is a word). So(goto-char s)
putspoint
at the beginning of the word in preparation for thecond
.