1 ;;; ascii-art-to-unicode.el --- a small artist adjunct -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Thien-Thi Nguyen <ttn@gnu.org>
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; The command `aa2u' converts simple ASCII art line drawings in
24 ;; the {active,accessible} region of the current buffer to Unicode.
27 ;; - M-x artist-mode RET
28 ;; - C-c C-a r ; artist-select-op-rectangle
29 ;; - (draw two rectangles)
37 ;; +-------+-------+ |
43 ;; - C-c C-c ; artist-mode-off (optional)
44 ;; - C-x n n ; narrow-to-region
53 ;; └───────┼───────┘ │
59 ;; Much easier on the eyes now!
61 ;; Normally, lines are drawn with the `LIGHT' weight. If you set var
62 ;; `aa2u-uniform-weight' to symbol `HEAVY', you will see, instead:
70 ;; ┗━━━━━━━╋━━━━━━━┛ ┃
78 ;; - HACKING: <http://git.sv.gnu.org/cgit/emacs/elpa.git/tree/packages/ascii-art-to-unicode/HACKING>
79 ;; - Tip Jar: <http://www.gnuvola.org/software/aa2u/>
86 (defvar aa2u-uniform-weight 'LIGHT
87 "A symbol, either `LIGHT' or `HEAVY'.
88 This specifies the weight of all the lines.")
90 ;;;---------------------------------------------------------------------------
93 (defun aa2u-ucs-bd-uniform-name (&rest components)
94 "Return a string naming UCS char w/ WEIGHT and COMPONENTS.
95 The string begins with \"BOX DRAWINGS\"; followed by the weight
96 as per variable `aa2u-uniform-weight', followed by COMPONENTS,
97 a list of one or two symbols from the set:
106 If of length two, the first element in COMPONENTS should be
107 the \"Y-axis\" (VERTICAL, DOWN, UP). In that case, the returned
108 string includes \"AND\" between the elements of COMPONENTS.
110 Lastly, all words are separated by space (U+20)."
111 (format "BOX DRAWINGS %s %s"
113 (mapconcat 'symbol-name components
116 (defun aa2u-1c (stringifier &rest components)
117 "Apply STRINGIFIER to COMPONENTS; return the UCS char w/ this name.
118 The char is a string (of length one), with two properties:
123 Their values are STRINGIFIER and COMPONENTS, respectively."
124 (let ((s (string (cdr (assoc-string (apply stringifier components)
127 'aa2u-stringifier stringifier
128 'aa2u-components components)))
130 (defun aa2u-phase-1 ()
131 (goto-char (point-min))
132 (let ((vert (aa2u-1c 'aa2u-ucs-bd-uniform-name 'VERTICAL)))
133 (while (search-forward "|" nil t)
134 (replace-match vert t t)))
135 (goto-char (point-min))
136 (let ((horz (aa2u-1c 'aa2u-ucs-bd-uniform-name 'HORIZONTAL)))
137 (while (search-forward "-" nil t)
138 (replace-match horz t t))))
140 (defun aa2u-replacement (pos)
141 (let ((cc (- pos (line-beginning-position))))
145 ;; Infer LIGHTness between "snug" ‘?+’es.
147 ;; +-----------++--+ +
148 ;; | somewhere ++--+---+-+----+
149 ;; +-+---------+ nowhere |+--+
152 (eq ?+ (char-after pos))
153 ;; Require properly directional neighborliness.
155 ((UP DOWN) 'VERTICAL)
156 ((LEFT RIGHT) 'HORIZONTAL))
157 (get-text-property pos 'aa2u-components)))
159 (v (name dir) (let ((bol (line-beginning-position dir))
160 (eol (line-end-position dir)))
161 (when (< cc (- eol bol))
162 (ok name (+ bol cc)))))
163 (h (name dir) (let ((bol (line-beginning-position))
164 (eol (line-end-position))
166 (unless (or (> bol pos)
169 (two-p (ls) (= 2 (length ls)))
170 (just (&rest args) (delq nil args)))
172 'aa2u-ucs-bd-uniform-name
173 (just (pcase (just (v 'UP 0)
175 ((pred two-p) 'VERTICAL)
178 (pcase (just (h 'LEFT -1)
180 ((pred two-p) 'HORIZONTAL)
184 (defun aa2u-phase-2 ()
185 (goto-char (point-min))
187 ;; (phase 2.1 -- what WOULD change)
188 ;; This is for the benefit of ‘aa2u-replacement ok’, which
189 ;; otherwise (monolithic phase 2) would need to convert the
190 ;; "properly directional neighborliness" impl from a simple
191 ;; ‘memq’ to an ‘intersction’.
192 (while (search-forward "+" nil t)
194 (push (cons p (or (aa2u-replacement (1- p))
197 ;; (phase 2.2 -- apply changes)
203 (defun aa2u-phase-3 ()
204 (remove-text-properties (point-min) (point-max)
205 (list 'aa2u-stringifier nil
206 'aa2u-components nil)))
208 ;;;---------------------------------------------------------------------------
212 (defun aa2u (beg end &optional interactive)
213 "Convert simple ASCII art line drawings to Unicode.
214 Specifically, perform the following replacements:
216 - (hyphen) BOX DRAWINGS LIGHT HORIZONTAL
217 | (vertical bar) BOX DRAWINGS LIGHT VERTICAL
219 BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL
220 BOX DRAWINGS LIGHT DOWN AND RIGHT
221 BOX DRAWINGS LIGHT DOWN AND LEFT
222 BOX DRAWINGS LIGHT UP AND RIGHT
223 BOX DRAWINGS LIGHT UP AND LEFT
224 BOX DRAWINGS LIGHT VERTICAL AND RIGHT
225 BOX DRAWINGS LIGHT VERTICAL AND LEFT
226 BOX DRAWINGS LIGHT UP AND HORIZONTAL
227 BOX DRAWINGS LIGHT DOWN AND HORIZONTAL
228 BOX DRAWINGS LIGHT UP
229 BOX DRAWINGS LIGHT DOWN
230 BOX DRAWINGS LIGHT LEFT
231 BOX DRAWINGS LIGHT RIGHT
234 More precisely, hyphen and vertical bar are substituted unconditionally,
235 first, and plus is substituted with a character depending on its north,
236 south, east and west neighbors.
238 NB: Actually, `aa2u' can also use \"HEAVY\" instead of \"LIGHT\",
239 depending on the value of variable `aa2u-uniform-weight'.
241 This command operates on either the active region,
242 or the accessible portion otherwise."
244 ;; This weirdness, along w/ the undocumented "p" in the ‘interactive’
245 ;; form, is to allow ‘M-x aa2u’ (interactive invocation) w/ no region
246 ;; selected to default to the accessible portion (as documented), which
247 ;; was the norm in ascii-art-to-unicode.el prior to 1.5. A bugfix,
248 ;; essentially. This is ugly, unfortunately -- is there a better way?!
249 (when (and interactive (not (region-active-p)))
250 (setq beg (point-min)
255 (narrow-to-region beg end)
260 ;;;---------------------------------------------------------------------------
263 (provide 'ascii-art-to-unicode)
265 ;;; ascii-art-to-unicode.el ends here