Do not make a face for every pixel color.
[pico8.git] / pico8.el
index 95fcd20..fd6aeed 100644 (file)
--- a/pico8.el
+++ b/pico8.el
@@ -2,7 +2,7 @@
 ;;
 ;; Author: Joe Wreschnig <joe.wreschnig@gmail.com>
 ;; Package-Version: 20170620
-;; Package-Requires: ((emacs "25") (polymode "20170307") (lua-mode "20151025"))
+;; Package-Requires: ((emacs "25") (polymode "20170307") (lua-mode "20151025") (dash "2.12.0"))
 ;; URL: https://git.korewanetadesu.com/pico8.git
 ;; Keywords: convenience
 ;;
@@ -24,6 +24,7 @@
 (require 'polymode)
 (require 'lua-mode)
 (require 'thingatpt)
+(require 'dash)
 
 
 (defgroup pico8 nil
   :tag "PICO-8"
   :group 'languages)
 
-(defgroup pico8-faces nil
-  "Faces for PICO-8 (.p8) cartridge files."
-  :tag "PICO-8 Faces"
-  :group 'pico8)
-
 (defcustom pico8-executable-paths
   '("/Applications/PICO-8.app/Contents/MacOS/pico8" ; macOS
     "/usr/lib/pico8/pico8" ; PocketCHIP
@@ -88,10 +84,9 @@ where the convention is 1."
 (defconst pico8-data-characters
   (append "0123456789abcdef" nil))
 
-(defface pico8-data-face
+(defface pico8-data
   '((t (:inherit fixed-pitch)))
-  "Face for PICO-8 binary data."
-  :group 'pico8-faces)
+  "Face for PICO-8 binary data.")
 
 (define-derived-mode pico8-data-mode fundamental-mode
   "PICO-8 Data"
@@ -271,48 +266,22 @@ Make sure it is installed, and present in `pico8-executable-paths'")))
     (error "Valid flag numbers are 0 to 255, inclusive"))
   (+ (* 2 flag) (if (> flag 128) 1 0)))
 \f
-(defgroup pico8-pixel-faces nil
-  "Font faces to use for PICO-8 pixels.
-
-Rather than customizing each directly, you'll probably just want
-to change `pico8-pixel-face'."
-  :tag "PICO-8 Pixel Faces"
-  :group 'pico8-faces)
-
-(defface pico8-pixel-face
-  '((t (:inherit pico8-data-face :height 100)))
-  "Face for PICO-8 sprite 'pixels'."
-  :group 'pico8-faces)
-
-(dotimes (i (length pico8-colors))
-  (let ((c (nth i pico8-colors)))
-    (eval `(defface ,(intern (format "pico8-pixel-%x" i))
-             '((t (:inherit pico8-pixel-face :foreground ,c)))
-             ,(format "Face for PICO-8 sprite 'pixel' %x" i)
-             :group 'pico8-pixel-faces
-             :tag ,(format "Face for PICO-8 sprite 'pixel' %x." i)))))
+(defface pico8-pixel
+  '((t (:inherit pico8-data :height 100)))
+  "Face for PICO-8 sprite “pixels.”"
+  :group 'pico8)
 
 (defconst pico8-gfx-font-lock-keywords
-  `(("0+" . 'pico8-pixel-0)
-    ("1+" . 'pico8-pixel-1)
-    ("2+" . 'pico8-pixel-2)
-    ("3+" . 'pico8-pixel-3)
-    ("4+" . 'pico8-pixel-4)
-    ("5+" . 'pico8-pixel-5)
-    ("6+" . 'pico8-pixel-6)
-    ("7+" . 'pico8-pixel-7)
-    ("8+" . 'pico8-pixel-8)
-    ("9+" . 'pico8-pixel-9)
-    ("a+" . 'pico8-pixel-a)
-    ("b+" . 'pico8-pixel-b)
-    ("c+" . 'pico8-pixel-c)
-    ("d+" . 'pico8-pixel-d)
-    ("e+" . 'pico8-pixel-e)
-    ("f+" . 'pico8-pixel-f)
-
-    ;; If the \n isn't in the smaller face the line is taller to
-    ;; accommodate the full sized point at the end-of-line.
-    ("\n" . 'pico8-pixel-0)))
+  (cons
+   ;; If the \n isn't in the smaller face the line is taller to
+   ;; accommodate the full sized point at the end-of-line.
+   '("\n" . 'pico8-pixel)
+   (-map-indexed
+    (lambda (i c)
+      `(,(format "%x+" i)
+        0 '(:inherit pico8-pixel :foreground ,c)))
+    pico8-colors)))
+
 
 (defun pico8-gfx-current-position ()
   "Calculate the sprite and in-sprite position of the cursor."
@@ -370,14 +339,14 @@ to change `pico8-pixel-face'."
 (define-key pico8-gfx-mode-map "q" 'pico8-backward-sprite)
 (define-key pico8-gfx-mode-map "w" 'pico8-forward-sprite)
 
-(defface pico8-map-tile-face
-  '((t (:inherit pico8-data-face :height 100)))
+(defface pico8-map-tile
+  '((t (:inherit pico8-data :height 100)))
   "Face for PICO-8 map 'tiles'."
   :group 'pico8-faces)
 
 (defconst pico8-map-font-lock-keywords
-  '(("[0-9a-f]+" . 'pico8-map-tile-face)
-    ("\n" . 'pico8-map-tile-face)))
+  '(("[0-9a-f]+" . 'pico8-map-tile)
+    ("\n" . 'pico8-map-tile)))
 
 (defun pico8-map-lighter ()
   "Calculate the map tile under the cursor."