emacs-devel
[Top][All Lists]
Advanced

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

Re: info faces for strings and quotations


From: Stefan
Subject: Re: info faces for strings and quotations
Date: Tue, 05 Oct 2004 07:56:40 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/21.3.50 (darwin)

> (defun info-fontify-quotations ()
>   "Fontify double-quote strings (\"...\") and text between single-quotes
> (`...')
> For single-quotes, use `info-quoted-name-face'.
> For double-quotes, use `info-string-face'."
>   (goto-char (point-min))
>   (let (;; double-quote strings: "...", "...\", "...\\", etc.; m1=\*
>         ;; or single-quote strings: `...'
>         (either-re "\"[^\"]*\\([\\\\]*\\)\"\\|`[^'\n]+'")
>         ;; ", \", \\", \\\" etc.; m2=\*
>         (dblquote-re "\\([\\\\]*\\)\"")
>         m0 p0b p0e                      ; Whole match: `...' or "..."
>         m1 p1b p1e                      ; \* subexp of "...\*" match
>         m2 p2b p2e                      ; \* subexp of "...\*" match
>         escaped-dblquote-p)
>     (while (re-search-forward either-re nil t)
>       (setq m0 (match-string 0)         ; Whole match string
>             p0b (nth 0 (match-data))    ; Beginning of m0

Never do (nth x (match-data)).  Always use (match-beginning N) or
(match-end N) instead.  Much more readable (and efficient).

>             p0e (nth 1 (match-data))    ; End of m0
>             m1 (match-string 1)         ; \* subexp of "...\*" match

The code never seems to use `m1' so you can spare this string-allocation.

>             p1b (nth 2 (match-data))    ; Beginning of m1
>             p1e (nth 3 (match-data)))   ; End of m2
>       (when (equal (char-after p0b) ?\") ; double-quote string: "..."
>         (when (> p1e p1b)               ; May be escaped: \ inside "...\*"
>           (when (= (mod (- p1e p1b) 2) 1) ; Escaped (odd number of
> backslashes: \", \\\",...)
>             (setq escaped-dblquote-p t)
>             (while escaped-dblquote-p
>               (if (not (re-search-forward dblquote-re nil t)) ; Look for \*"
>                   (setq escaped-dblquote-p nil) ; No \*"
>                 (setq m2 (match-string 0) ; \*"

Similary `m2' is never used.

>                       p2b (nth 0 (match-data)) ; Beginning of \*": \ or "
>                       p2e (nth 1 (match-data)) ; End of \*": "
>                       p0e p2e)          ; Update pointer
>                 (if (= p2e p2b)
>                     (setq escaped-dblquote-p nil) ; Not escaped - ", \\",
> \\\\", etc.
>                   (when (= (mod (- p2e p2b) 2) 1) (setq escaped-dblquote-p
> nil))))))))

Why not use (either-re "\"\\([^\\\"]\\|\\\\[\\\"]\\)*\"\\|`[^'\n]+'")
and get rid of all this code (i.e. the regexp-matching does the
escape-counting for you)?

>       (if (eq ?` (aref m0 0))

Use (eq ?` (char-after p0b)) and you can get rid of `m0'.

>           (put-text-property (1+ p0b) (1- p0e) 'face 'info-quoted-name-face)
>         (put-text-property p0b p0e 'face 'info-string-face)))))

Why fontify the interior of `...' but fontify both the interior and the
quotes for "..." ?  I.e. why not use

    (put-text-property p0b p0e 'face
     (if (eq ?` (char-after p0b)) 'info-quoted-name-face 'info-string-face))
or
    (put-text-property (1+ p0b) (1- p0e) 'face
     (if (eq ?` (char-after p0b)) 'info-quoted-name-face 'info-string-face))

Is it just because your quoted face is bold and you don't like to see the `
and ' in bold, or is there a deeper reason?


        Stefan




reply via email to

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