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

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

Re: Shortening words with multiple rules


From: Arash Esbati
Subject: Re: Shortening words with multiple rules
Date: Wed, 17 Aug 2022 12:07:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50

uzibalqa <uzibalqa@proton.me> writes:

> Trying the code upon the word "please" results in "pase" because you
> have not checked for mid-word in the final search.

For me, it turns "please" into "lase".  Maybe I didn't understand this
correctly, but do you want to apply all the rules to a word?  In that
case, you have adjust the code to do so like this:

(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)
         (e (make-marker))
         (p (point-marker)))
    (when s
      (set-marker e (cdr bounds))
      (goto-char s)
      
      ;;-----------------------------------------------
      ;; Insert `k' for words with initial
      ;; `cog', `col', `com', `con', `cor', `coun', `cum'.
      (when (looking-at
             (concat  "\\<"
                      (regexp-opt '("cog" "col" "com" "con" "cor" "cum" 
                                    "coun"))))

        (replace-match "k")
        (goto-char s))
      

      ;;-----------------------------------------------
      ;; Insert `l' for words with final
      ;; `ley', `ily', and `ly'.
      (when (save-excursion
              (re-search-forward (concat
                                  (regexp-opt '("ley" "ily" "ly")) "\\>")
                                 e t))
        (replace-match "l")
        (goto-char s))

      ;; Make sure we don't match anything at the beginning of the
      ;; string by going one char forward:
      (when (save-excursion
              (forward-char)
              (re-search-forward "ple" e t))
        (replace-match "l")
        (goto-char s))
      
      (goto-char p))
    (set-marker e nil)
    (set-marker p nil)))

This turns "completely" to "kltel".  If you want only one rule to apply,
adjust the last version with `cond' to your needs.

Best, Arash



reply via email to

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