emacs-devel
[Top][All Lists]
Advanced

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

Re: Bidirectional text and URLs


From: Lars Magne Ingebrigtsen
Subject: Re: Bidirectional text and URLs
Date: Sun, 30 Nov 2014 20:06:48 +0100
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Ok, it was a bit longer than 7 lines, and I'm not quite sure what to do
about further embedded LRM, RLM, ALM, LRE, RLE, LRO, RLO, PDF, LRI, RLI,
FSI, PDI characters (perhaps just drop them?), but here's a kinda weak
proof of concept.

Eval the following, and you sort of get what you'd expect.

(concat (string ?\x202e) "---" (ensure-left-to-right-string 
"http://אבג.דהוזחט.קום/yes/indeed.קום///";))

?\x202e is the right-to-left override.

Compare with the output you get if you don't hack up the URL and
sprinkle LROs:

(concat (string ?\x202e) "---" "http://אבג.דהוזחט.קום/yes/indeed.קום///";)

(defun ensure-left-to-right-string (string)
  (let ((prev (get-char-code-property (aref string 0) 'bidi-class))
        (start 0)
        (pos 0)
        (bits nil))
    (while (< pos (length string))
      (setq current (get-char-code-property (aref string pos) 'bidi-class))
      (when (or (and (eq prev 'L)
                     (memq current '(R AL)))
                (and (memq prev '(R AL))
                     (eq current 'L)))
        (push (substring string start pos) bits)
        (when (memq current '(L R AL))
          (setq prev current))
        (setq start pos))
      (cl-incf pos))
    (push (substring string start pos) bits)
    (mapconcat
     (lambda (bit)
       (if (cl-notany (lambda (char)
                        (memq (get-char-code-property char 'bidi-class) '(R 
AL)))
                      bit)
           ;; Wrap the string in LRO and PDF.
           (concat (string ?\x202d) bit (string ?\x202C))
         ;; And RLO and PDF for the right-to-left bits.
         (concat (string ?\x202e) bit (string ?\x202C))))
     (nreverse bits)
     "")))

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





reply via email to

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