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

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

Re: elisp optimization question


From: harven
Subject: Re: elisp optimization question
Date: Thu, 8 May 2008 17:00:30 -0700 (PDT)
User-agent: G2/1.0

hi,
you can save some typing by using an alist. Here is what i use to
convert
accented-letters into html and back.

(defun accent-html (prefix)
 "Accented letter translation     é -> &eacute.
  With an argument,  reverse    é <- &eacute.
  Works on the whole buffer"
 (interactive "P")
 (save-excursion
   (let ((association
          '(("É" . "&Eacute;") ("á" . "&aacute;")  ("à" . "&agrave;")
            ("â" . "&acirc;")  ("ä" . "&auml;")    (""" . "&atilde;")
            ("é" . "&eacute;") ("è" . "&egrave;")  ("ê" . "&ecirc;")
            ("ë" . "&euml;")   ("í" . "&iacute;")  ("ì" . "&igrave;")
            ("î" . "&icirc;")  ("ï" . "&iuml;")    ("ñ" . "&ntilde;")
            ("ó" . "&oacute;") ("ò" . "&ograve;")  ("ô" . "&ocirc;")
            ("ö" . "&ouml;")   ("ı" . "&otilde;")  ("ú" .
"&uacute;")
            ("ù" . "&ugrave;") ("û" . "&ucirc;")   ("ü" . "&uuml;")
            ("ç" . "&ccedil;")))
        (case-fold-search nil))
   (dolist (paire association)
     (when prefix
       (setq paire (cons (cdr paire) (car paire))))
     (goto-char (point-min))
     (while (search-forward (car paire) nil t)
         (replace-match (cdr paire) nil t))))))

This is not more efficient than your own defun. If you only want to
translate characters, the function (subst-char-in-region) is a
primitive
that saves a while loop and is probably faster.


reply via email to

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