>>>> "Aidan" == Aidan Kehoe
<kehoea(a)parhasard.net> writes:
Ar an seachtú lá de mí Eanair, scríobh Uwe Brauer:
> Anyhow the code worked, but there is a problem:
>
> (defvar tex2utf8symb-trans-tab
> '(
> ("\\in" "∈")
> ("\\infty" "∞")
> ("\\int" "∫")
>
> ))
>
> While
> \in and \int are transformed correctly
> \infty is changed to ∈fty,
>
> which is wrong (on the other hand I don't understand why
> \int is not transformed to ∈t but anyhow).
>
> So the question is how shall I define the tex commands uniquely?
What you need to do is rewrite the code to use
#'posix-search-forward, which guarantees that it will return the
longest match (that is, it will always try to match "\\infty" if
possible, and only then look for "\in".) You might also want to
look into the #'regexp-opt function, which, given a list of
strings, returns a theoretically fast regexp to match that
list--you’ll need the regexp to pass to
posix-search-forward. (I’m not however certain if regexp-opt
interacts well with posix-search-forward; anyone else know?)
Hm the code is this
(defun fix-tex2utf8symbol ()
"Replace SGML entity references with ISO 8859-1 (aka Latin-1) characters."
(interactive)
; (if (member major-mode utf8symbol-modes-list)
(let ((buffer-modified-p (buffer-modified-p)))
(unwind-protect
(utf8symbol-translate-conventions tex2utf8symbol-trans-tab)
(set-buffer-modified-p buffer-modified-p))))
And the most relevant function:
(defun utf8symbol-translate-conventions (trans-tab)
"Use the translation table argument to translate the current buffer."
(save-excursion
(let ((beg (point-min-marker)) ; see the `(elisp)Narrowing' Info node
(end (point-max-marker)))
(unwind-protect
(progn
(widen)
(goto-char (point-min))
(let ((buffer-read-only nil) ; (inhibit-read-only t)?
(case-fold-search nil))
(while trans-tab
(save-excursion
(let ((trans-this (car trans-tab)))
(while (search-forward (car trans-this) nil t)
(replace-match (car (cdr trans-this)) t t)))
(setq trans-tab (cdr trans-tab))))))
(narrow-to-region beg end)))))
So tried to replace the line:
(while (search-forward (car trans-this) nil t)
By
(while (posix-search-forward (car trans-this) nil t)
but I obtained an error.
So I thought of using regexp-opt, as you suggested
For me the following looked "logical":
(defun utf8symbol-translate-conventions (trans-tab)
"Use the translation table argument to translate the current buffer."
(save-excursion
(let ((beg (point-min-marker)) ; see the `(elisp)Narrowing' Info node
(end (point-max-marker)))
(unwind-protect
(progn
(widen)
(goto-char (point-min))
(let ((buffer-read-only nil) ; (inhibit-read-only t)?
(case-fold-search nil))
(while trans-tab
(save-excursion
(let ((trans-this (car trans-tab)))
(regexp-opt trans-this)
^^^^^^^^^^^^^^^^^^^^^^
(while (search-forward (car trans-this) nil t)
(replace-match (car (cdr trans-this)) t t)))
(setq trans-tab (cdr trans-tab))))))
(narrow-to-region beg end)))))
But this did not work neither.
Uwe
_______________________________________________
XEmacs-Beta mailing list
XEmacs-Beta(a)xemacs.org
http://calypso.tux.org/cgi-bin/mailman/listinfo/xemacs-beta