[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Converting backslash sequences to (non-ascii) characters
From: |
Eduardo Ochs |
Subject: |
Re: Converting backslash sequences to (non-ascii) characters |
Date: |
Wed, 9 Jun 2021 00:49:57 -0300 |
Let me add a license to play safe...
The code below is a sort of a fix for a problem in eev,
in the function `find-pdflike-page-links'. I am the author of eev,
and I declare that this sort-of-fix is also GPL3'ed, as eev is.
Eduardo Ochs
On Wed, 9 Jun 2021 at 00:02, Eduardo Ochs <eduardoochs@gmail.com> wrote:
>
> (setq ee-elisp-bsl-re
> (rx "\\" (or (group-n 1 (any "abtnvfres\\d"))
> (group-n 2 (regexp "[0-7][0-7][0-7]"))
> (and (any "x") (group-n 3 hex hex))
> (and (any "uU") (group-n 4 hex hex hex hex
> (opt hex hex) (opt hex hex)))
> )))
>
> (defun ee-elisp-bsl-convert1 (str)
> (save-match-data
> (string-match ee-elisp-bsl-re str)
> (let ((char (match-string 1 str))
> (octal (match-string 2 str))
> (hexa (match-string 3 str))
> (hexb (match-string 4 str)))
> (cond (char (match-string 0 str))
> (octal (format "%c" (string-to-number octal 8)))
> (hexa (format "%c" (string-to-number hexa 16)))
> (hexb (format "%c" (string-to-number hexb 16)))
> ))))
>
> (defun ee-elisp-bsl-replace (s e)
> "Replace some backslash sequences in the region.
> The sequences \\ooo, \\xhh and \\uhhhh, where the `o's are octal
> digits and the `h's are hex digits, are replaced by their
> corresponding characters."
> (interactive "r")
> (save-excursion
> (save-restriction
> (narrow-to-region s e)
> (goto-char (point-min))
> (while (re-search-forward ee-elisp-bsl-re nil 'noerror)
> (replace-match (ee-elisp-bsl-convert1 (match-string 0)) 'fixedcase 'literal)
> ))))
>
> (defalias 'bsl 'ee-elisp-bsl-replace)
>
> ;; Tests:
> ;; (ee-elisp-bsl-convert1 "\\n")
> ;; (ee-elisp-bsl-convert1 "\\234")
> ;; (ee-elisp-bsl-convert1 "\\351")
> ;; (ee-elisp-bsl-convert1 "_\\351_")
> ;; (ee-elisp-bsl-convert1 "\\x41")
> ;; (ee-elisp-bsl-convert1 "\\u2022")
> ;; (find-einsert '((0 255)))
> ;;
> ;; ...and mark the next line and run `M-x bsl' on it:
> ;; "Foo \n\\\234\u2022\xa2"