Merge from origin/emacs-24
[emacs.git] / test / biditest.el
blobe221208383058ac278730763867644e479368837
1 ;;; biditest.el --- test bidi reordering in GNU Emacs display engine.
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Eli Zaretskii
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Package: emacs
9 ;; This program is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Produce a specially-formatted text file from BidiCharacterTest.txt
25 ;; file that is part of the Unicode Standard's UCD package. The file
26 ;; shows the expected results of reordering according to the UBA. The
27 ;; file is supposed to be visited in Emacs, and the resulting display
28 ;; compared with the expected one.
30 ;;; Code:
32 (defun biditest-generate-testfile (input-file output-file)
33 "Generate a bidi test file OUTPUT-FILE from data in INPUT-FILE.
35 INPUT-FILE should be in the format of the BidiCharacterTest.txt file
36 available from the Unicode site, as part of the UCD database, see
37 http://www.unicode.org/Public/UCD/latest/ucd/BidiCharacterTest.txt.
39 The resulting file should be viewed with `inhibit-bidi-mirroring' set to t."
40 (let ((output-buf (get-buffer-create "*biditest-output*"))
41 (lnum 1)
42 tbuf)
43 (with-temp-buffer
44 (message "Generating output in %s ..." output-file)
45 (setq tbuf (current-buffer))
46 (insert-file-contents input-file)
47 (goto-char (point-min))
48 (while (not (eobp))
49 (when (looking-at "^\\([0-9A-F ]+\\);\\([012]\\);\\([01]\\);\\([0-9 ]+\\);\\([0-9 ]+\\)$")
50 (let ((codes (match-string 1))
51 (default-paragraph (match-string 2))
52 (resolved-paragraph (match-string 3))
53 ;; FIXME: Should compare LEVELS with what the display
54 ;; engine actually produced.
55 (levels (match-string 4))
56 (indices (match-string 5)))
57 (setq codes (split-string codes " ")
58 indices (split-string indices " "))
59 (switch-to-buffer output-buf)
60 (insert (format "Test on line %d:\n\n" lnum))
61 ;; Force paragraph direction to what the UCD test
62 ;; specifies.
63 (insert (cond
64 ((string= default-paragraph "0") ;L2R
65 #x200e)
66 ((string= default-paragraph "1") ;R2L
67 #x200f)
68 (t ""))) ; dynamic
69 ;; Insert the characters
70 (mapc (lambda (code)
71 (insert (string-to-number code 16)))
72 codes)
73 (insert "\n\n")
74 ;; Insert the expected results
75 (insert "Expected result:\n\n")
76 ;; We want the expected results displayed exactly as
77 ;; specified in the test file, without any reordering, so
78 ;; we override the directional properties of all of the
79 ;; characters in the expected result by prepending
80 ;; LRO/RLO.
81 (cond ((string= resolved-paragraph "0")
82 (insert #x200e #x202d))
83 ((string= resolved-paragraph "1")
84 (insert #x200f #x202e)
85 ;; We need to reverse the list of indices for R2L
86 ;; paragraphs, so that their logical order on
87 ;; display matches user expectations.
88 (setq indices (nreverse indices))))
89 (mapc (lambda (index)
90 (insert (string-to-number
91 (nth (string-to-number index 10) codes)
92 16)))
93 indices)
94 (insert #x202c) ; end the embedding
95 (insert "\n\n"))
96 (switch-to-buffer tbuf))
97 (forward-line 1)
98 (setq lnum (1+ lnum)))
99 (switch-to-buffer output-buf)
100 (let ((coding-system-for-write 'utf-8-unix))
101 (write-file output-file))
102 (message "Generating output in %s ... done" output-file))))
104 (defun biditest-create-test ()
105 "Create a test file for testing the Emacs bidirectional display.
107 The resulting file should be viewed with `inhibit-bidi-mirroring' set to t."
108 (biditest-generate-testfile (pop command-line-args-left)
109 (or (pop command-line-args-left)
110 "biditest.txt")))
112 ;; A handy function for displaying the resolved bidi levels.
113 (defun bidi-levels ()
114 "Display the resolved bidirectional levels of characters on current line.
116 The results can be compared with the levels stated in the
117 BidiCharacterTest.txt file."
118 (interactive)
119 (message "%s" (bidi-resolved-levels)))
121 (define-key global-map [f8] 'bidi-levels)