Function: line-substring-with-bidi-context
line-substring-with-bidi-context is a byte-compiled function defined
in simple.el.gz.
Signature
(line-substring-with-bidi-context START END &optional NO-PROPERTIES)
Documentation
Return buffer text between START and END with its bidi context.
START and END are assumed to belong to the same physical line of buffer text. This function prepends and appends to the text between START and END bidi control characters that preserve the visual order of that text when it is inserted at some other place.
Source Code
;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun line-substring-with-bidi-context (start end &optional no-properties)
"Return buffer text between START and END with its bidi context.
START and END are assumed to belong to the same physical line
of buffer text. This function prepends and appends to the text
between START and END bidi control characters that preserve the
visual order of that text when it is inserted at some other place."
(if (or (< start (point-min))
(> end (point-max)))
(signal 'args-out-of-range (list (current-buffer) start end)))
(let ((buf (current-buffer))
substr para-dir from to)
(save-excursion
(goto-char start)
(setq para-dir (current-bidi-paragraph-direction))
(setq from (line-beginning-position)
to (line-end-position))
(goto-char from)
;; If we don't have any mixed directional characters in the
;; entire line, we can just copy the substring without adding
;; any context.
(if (or (looking-at-p "\\CR*$")
(looking-at-p "\\CL*$"))
(setq substr (if no-properties
(buffer-substring-no-properties start end)
(buffer-substring start end)))
(setq substr
(with-temp-buffer
(if no-properties
(insert-buffer-substring-no-properties buf from to)
(insert-buffer-substring buf from to))
(squeeze-bidi-context 1 (1+ (- start from)))
(squeeze-bidi-context (- end to) nil)
(buffer-substring 1 (point-max)))))
;; Wrap the string in LRI/RLI..PDI pair to achieve 2 effects:
;; (1) force the string to have the same base embedding
;; direction as the paragraph direction at the source, no matter
;; what is the paragraph direction at destination; and (2) avoid
;; affecting the visual order of the surrounding text at
;; destination if there are characters of different
;; directionality there.
(concat (if (eq para-dir 'left-to-right) "\x2066" "\x2067")
substr "\x2069"))))