| 1 |
;;; ascii-braille.el --- Replace Unicode Braille with Ascii Braille overlays |
| 2 |
|
| 3 |
;; Copyright (C) 2020 Christian Egli |
| 4 |
|
| 5 |
;; Author: Christian Egli <[email protected]> |
| 6 |
;; Version: 0.1 |
| 7 |
;; URL: https://github.com/liblouis/ascii-braille-overlay/ |
| 8 |
;; Keywords: faces, matching, braille |
| 9 |
;; Package-Requires: ((emacs "24.3") (ht "2.2")) |
| 10 |
|
| 11 |
;; This program is free software; you can redistribute it and/or modify |
| 12 |
;; it under the terms of the GNU General Public License as published by |
| 13 |
;; the Free Software Foundation, either version 3 of the License, or |
| 14 |
;; (at your option) any later version. |
| 15 |
|
| 16 |
;; This program is distributed in the hope that it will be useful, |
| 17 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
;; GNU General Public License for more details. |
| 20 |
|
| 21 |
;; You should have received a copy of the GNU General Public License |
| 22 |
;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
|
| 24 |
;;; Commentary: |
| 25 |
|
| 26 |
;; Replace unicode braille with ascii braille overlays while. It was |
| 27 |
;; originally inspired by the package `symbol-overlays'. |
| 28 |
|
| 29 |
;; Usage |
| 30 |
|
| 31 |
;; Toggle the `ascii-braille-mode' minor mode to see the unicode |
| 32 |
;; braille in your buffer as ascii braille. Disabling the minor will |
| 33 |
;; remove all overlays. |
| 34 |
|
| 35 |
;; TODO |
| 36 |
|
| 37 |
;; - I suppose it would be nice if you could edit the ascii braille |
| 38 |
;; and have it reflected in the unicode braille. |
| 39 |
;; - Add a modify hook that updates the ascii braille if the unicode |
| 40 |
;; braille changes. |
| 41 |
|
| 42 |
|
| 43 |
;;; Code: |
| 44 |
|
| 45 |
(require 'ht) |
| 46 |
|
| 47 |
(defconst ascii-braille-unicode-to-ascii-map |
| 48 |
(ht |
| 49 |
(?⠀ ? ) |
| 50 |
(?⠁ ?a) |
| 51 |
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
(?⠘ ?>) |
| 109 |
(?⠨ ?$) |
| 110 |
(?⠸ ?_) |
| 111 |
(?⠰ ?<) |
| 112 |
(?⠠ ?'))) |
| 113 |
|
| 114 |
(defconst ascii-braille-dots-to-unicode-map |
| 115 |
'((?1 #x1) |
| 116 |
(?2 #x2) |
| 117 |
(?3 #x4) |
| 118 |
(?4 #x8) |
| 119 |
(?5 #x10) |
| 120 |
(?6 #x20) |
| 121 |
(?7 #x40) |
| 122 |
(?8 #x80))) |
| 123 |
|
| 124 |
(defun ascii-braille-dots-to-unicode (dots) |
| 125 |
"Convert a string of DOTS such as \"1234\" to a unicode character." |
| 126 |
;; see https://en.wikipedia.org/wiki/Braille_Patterns |
| 127 |
(let* ((offsets (seq-map (lambda (tuple) |
| 128 |
(seq-let (dot value) tuple |
| 129 |
(if (seq-contains-p dots dot) value 0))) |
| 130 |
ascii-braille-dots-to-unicode-map)) |
| 131 |
(offset-sum (seq-reduce #'+ offsets 0)) |
| 132 |
(unicode-braille-base #x2800)) |
| 133 |
(+ unicode-braille-base offset-sum))) |
| 134 |
|
| 135 |
(defgroup ascii-braille nil |
| 136 |
"Convert unicode braille to ascii braille using overlays." |
| 137 |
:group 'convenience) |
| 138 |
|
| 139 |
(defface ascii-braille-overlay-default-face |
| 140 |
'((t (:inherit highlight))) |
| 141 |
"Ascii braille overlay default face" |
| 142 |
:group 'ascii-braille) |
| 143 |
|
| 144 |
(defun ascii-braille-unicode-to-ascii (char) |
| 145 |
"Convert a unicode braille char to ascii braille. If the char |
| 146 |
is not a unicode braille char just return the char itself." |
| 147 |
(ht-get ascii-braille-unicode-to-ascii-map char char)) |
| 148 |
|
| 149 |
(defun ascii-braille-to-ascii (s) |
| 150 |
"Convert a unicode braille string to a ascii braille string." |
| 151 |
(seq-concatenate 'string (seq-map #'ascii-braille-unicode-to-ascii s))) |
| 152 |
|
| 153 |
(defun ascii-braille-overlay-put-one () |
| 154 |
"Put overlay on current occurrence of unicode braille after a match." |
| 155 |
(let ((ov (make-overlay (match-beginning 0) (match-end 0))) |
| 156 |
(ascii-braille (ascii-braille-to-ascii (match-string 0)))) |
| 157 |
; (overlay-put ov 'face 'ascii-braille-overlay-default-face) |
| 158 |
;(overlay-put ov 'invisible t) |
| 159 |
(overlay-put ov 'evaporate t) |
| 160 |
(overlay-put ov 'braille t) |
| 161 |
(add-face-text-property 0 (length ascii-braille) 'ascii-braille-overlay-default-face nil ascii-braille) |
| 162 |
(overlay-put ov 'before-string ascii-braille))) |
| 163 |
|
| 164 |
(defun ascii-braille-overlay-put-all () |
| 165 |
"Put overlays on all occurrences of unicode braille in the buffer." |
| 166 |
(let* ((case-fold-search nil)) |
| 167 |
(save-excursion |
| 168 |
(save-restriction |
| 169 |
(goto-char (point-min)) |
| 170 |
(let ((re "[⠀-⠿]+")) |
| 171 |
(while (re-search-forward re nil t) |
| 172 |
(ascii-braille-overlay-put-one))))))) |
| 173 |
|
| 174 |
|
| 175 |
(define-minor-mode ascii-braille-mode |
| 176 |
"Toggle Ascii Braille mode. |
| 177 |
Hide all Unicode Braille and put an overlay with Ascii Braille over top of it." |
| 178 |
;; The initial value. |
| 179 |
nil |
| 180 |
;; The indicator for the mode line. |
| 181 |
" AsciiBraille" |
| 182 |
:group 'ascii-braille |
| 183 |
(if ascii-braille-mode |
| 184 |
(ascii-braille-overlay-put-all) |
| 185 |
(remove-overlays nil nil 'braille t))) |