help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

search-forward-regexp for median character sequence


From: uzibalqa
Subject: search-forward-regexp for median character sequence
Date: Wed, 17 Aug 2022 20:47:05 +0000

The following function shortens words.  It takes care of replacing matches
for prefixes (e.g. replace starting `coun' with `k') and suffixes (e.g. replace
ending `ily' with letter `l').

I want to handle the case where the character sequence `ple' occurs medially
within a word (i.e. the word contains at least one character to either side
of the search pattern `ple').

I want to be able to use `search-forward-regexp' to do this, as is common
for prefixes and suffixes.

(defun shorten-word ()
  "Shortens a word according to specific rules."

  (interactive)

  (let* ((bounds (bounds-of-thing-at-point 'word))
         (s (car bounds))
         (case-fold-search nil)
         (e (make-marker))
         (p (point-marker)))

    (when s
      (set-marker e (cdr bounds))
      (goto-char s)
      (cond

       ;;-----------------------------------------------
       ;; Insert `k' for words with initial
       ;; `cog', `col', `com', `con', `cor', `coun', `cum'.
       ((search-forward-regexp
           (concat  "\\<"
              (regexp-opt '("cog" "col" "com" "con" "cor" "cum" "coun")))
           (cdr bounds)
           t)

         (replace-match "k"))

       ;;-----------------------------------------------
       ;; Insert `l' for words with final
       ;; `ley', `ily', and `ly'.
       ((search-forward-regexp
           (concat (regexp-opt '("ley" "ily" "ly")) "\\>")
           (cdr bounds)
           t)

        (replace-match "l"))

       ;;-----------------------------------------------
       ;; Insert letter `p, for median `ple'

       ;;-----------------------------------------------
       (t nil))
      (goto-char p))
    (set-marker e nil)
    (set-marker p nil)))








reply via email to

[Prev in Thread] Current Thread [Next in Thread]