]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/ascii-art-to-unicode/ascii-art-to-unicode.el
ascii-art-to-unicode.el (aa2u-replacement): Use cl-case instead of case.
[gnu-emacs-elpa] / packages / ascii-art-to-unicode / ascii-art-to-unicode.el
index 9aaa7ef8f9061061d1f7d8e3126117e681594567..a4cdf6d5b0fba3f541d13aaa7be8a37ed9370b3e 100644 (file)
 ;;
 ;; Much easier on the eyes now!
 ;;
+;; Normally, lines are drawn with the `LIGHT' weight.  If you set var
+;; `aa2u-uniform-weight' to symbol `HEAVY', you will see, instead:
+;;
+;;   ┏━━━━━━━━━━━━━━━┓
+;;   ┃               ┃
+;;   ┃       ┏━━━━━━━╋━━┓
+;;   ┃       ┃       ┃  ┃
+;;   ┃       ┃       ┃  ┃
+;;   ┃       ┃       ┃  ┃
+;;   ┗━━━━━━━╋━━━━━━━┛  ┃
+;;           ┃          ┃
+;;           ┃          ┃
+;;           ┃          ┃
+;;           ┗━━━━━━━━━━┛
+;;
 ;;
 ;; See Also
 ;; - HACKING: <http://git.sv.gnu.org/cgit/emacs/elpa.git/tree/packages/ascii-art-to-unicode/HACKING>
 (require 'cl-lib)
 (require 'pcase)
 
+(defvar aa2u-uniform-weight 'LIGHT
+  "A symbol, either `LIGHT' or `HEAVY'.
+This specifies the weight of all the lines.")
+
 ;;;---------------------------------------------------------------------------
 ;;; support
 
-(defun aa2u-ucs-bd-uniform-name (weight &rest components)
+(defun aa2u-ucs-bd-uniform-name (&rest components)
   "Return a string naming UCS char w/ WEIGHT and COMPONENTS.
-The string begins with \"BOX DRAWINGS\"; followed by WEIGHT,
-a symbol from the set:
-
-  HEAVY
-  LIGHT
-
-followed by COMPONENTS, a list of one or two symbols from the set:
+The string begins with \"BOX DRAWINGS\"; followed by the weight
+as per variable `aa2u-uniform-weight', followed by COMPONENTS,
+a list of one or two symbols from the set:
 
   VERTICAL
   HORIZONTAL
@@ -94,7 +109,7 @@ string includes \"AND\" between the elements of COMPONENTS.
 
 Lastly, all words are separated by space (U+20)."
   (format "BOX DRAWINGS %s %s"
-          weight
+          aa2u-uniform-weight
           (mapconcat 'symbol-name components
                      " AND ")))
 
@@ -114,11 +129,11 @@ Their values are STRINGIFIER and COMPONENTS, respectively."
 
 (defun aa2u-phase-1 ()
   (goto-char (point-min))
-  (let ((vert (aa2u-1c 'aa2u-ucs-bd-uniform-name 'LIGHT 'VERTICAL)))
+  (let ((vert (aa2u-1c 'aa2u-ucs-bd-uniform-name 'VERTICAL)))
     (while (search-forward "|" nil t)
       (replace-match vert t t)))
   (goto-char (point-min))
-  (let ((horz (aa2u-1c 'aa2u-ucs-bd-uniform-name 'LIGHT 'HORIZONTAL)))
+  (let ((horz (aa2u-1c 'aa2u-ucs-bd-uniform-name 'HORIZONTAL)))
     (while (search-forward "-" nil t)
       (replace-match horz t t))))
 
@@ -136,9 +151,9 @@ Their values are STRINGIFIER and COMPONENTS, respectively."
                     ;;              |      +---|
                     (eq ?+ (char-after pos))
                     ;; Require properly directional neighborliness.
-                    (memq (case name
-                            ((n s) 'VERTICAL)
-                            ((w e) 'HORIZONTAL))
+                    (memq (cl-case name
+                            ((UP DOWN)    'VERTICAL)
+                            ((LEFT RIGHT) 'HORIZONTAL))
                           (get-text-property pos 'aa2u-components)))
                name))
          (v (name dir) (let ((bol (line-beginning-position dir))
@@ -151,28 +166,20 @@ Their values are STRINGIFIER and COMPONENTS, respectively."
                          (unless (or (> bol pos)
                                      (<= eol pos))
                            (ok name pos))))
-         (light (&rest components) (apply 'aa2u-1c
-                                          'aa2u-ucs-bd-uniform-name
-                                          'LIGHT components)))
-      (let* ((n (v 'n 0))
-             (s (v 's 2))
-             (w (h 'w -1))
-             (e (h 'e  1)))
-        (pcase (delq nil (list n s w e))
-          (`(n s w e) (light 'VERTICAL 'HORIZONTAL))
-          (`(s e)     (light 'DOWN 'RIGHT))
-          (`(s w)     (light 'DOWN 'LEFT))
-          (`(n e)     (light 'UP 'RIGHT))
-          (`(n w)     (light 'UP 'LEFT))
-          (`(n s e)   (light 'VERTICAL 'RIGHT))
-          (`(n s w)   (light 'VERTICAL 'LEFT))
-          (`(n w e)   (light 'UP 'HORIZONTAL))
-          (`(s w e)   (light 'DOWN 'HORIZONTAL))
-          (`(n)       (light 'UP))
-          (`(s)       (light 'DOWN))
-          (`(w)       (light 'LEFT))
-          (`(e)       (light 'RIGHT))
-          (_          nil))))))
+         (two-p (ls) (= 2 (length ls)))
+         (just (&rest args) (delq nil args)))
+      (apply 'aa2u-1c
+             'aa2u-ucs-bd-uniform-name
+             (just (pcase (just (v 'UP   0)
+                                (v 'DOWN 2))
+                     ((pred two-p) 'VERTICAL)
+                     (`(,vc)        vc)
+                     (_             nil))
+                   (pcase (just (h 'LEFT  -1)
+                                (h 'RIGHT  1))
+                     ((pred two-p) 'HORIZONTAL)
+                     (`(,hc)        hc)
+                     (_             nil)))))))
 
 (defun aa2u-phase-2 ()
   (goto-char (point-min))
@@ -228,6 +235,9 @@ More precisely, hyphen and vertical bar are substituted unconditionally,
 first, and plus is substituted with a character depending on its north,
 south, east and west neighbors.
 
+NB: Actually, `aa2u' can also use \"HEAVY\" instead of \"LIGHT\",
+depending on the value of variable `aa2u-uniform-weight'.
+
 This command operates on either the active region,
 or the accessible portion otherwise."
   (interactive "r\np")