Skip to content

Instantly share code, notes, and snippets.

@taylskid

taylskid/pico8-mode.el Secret

Last active Feb 21, 2018
Embed
What would you like to do?
;; pico-8.el -- major mode for editing pico-8 cartridges -*- coding: utf-8; lexical-binding: t; -*-
(defun pico-find-data1 (limit)
(let ((start (point)))
(when (re-search-forward "__lua__" limit t)
(set-match-data `(,(point-min) ,(match-end 0)))
(goto-char (match-end 0)))))
(defun pico-find-data2 (limit)
(let ((start (point)))
(when (re-search-forward "__gfx__" limit t)
(goto-char (match-beginning 0))
(set-match-data `(,(point) ,(point-max)))
(goto-char (match-end 0)))))
(setq pico8-font-lock-keywords
(let* ((pico-constants '("true" "fasle" "nil"))
(pico-keywords '("for" "do" "end" "function" "foreach" "if" "then"))
(pico-functions-graphics '("clip" "pget" "pset" "sget" "sset" "fget" "fset" "print" "cursor" "color" "cls" "camera" "circ" "circfill" "line" "rect" "rectfill" "pal" "palt" "spr" "sspr" "fillp"))
(pico-functions-tables '("add" "del" "all" "foreach" "paris"))
(pico-functions-misc '("btn" "btnp" "sfx" "music" "mget" "mset" "map"))
(pico-functions-memory '("peek" "poke" "memcpy" "reload" "cstore" "memset"))
(pico-functions-math '("max" "min" "mid" "flr" "cos" "sin" "atan2" "sqrt" "abs" "rnd" "srand" "band" "bor" "bxor" "bnot" "shl" "shr"))
(pico-events '("print"))
(pico-constants-regexp (regexp-opt pico-constants 'words))
(pico-keywords-regexp (regexp-opt pico-keywords 'words))
(pico-functions-regexp (regexp-opt (append pico-functions-graphics
pico-functions-tables
pico-functions-misc
pico-functions-memory
pico-functions-math)
'words))
(pico-events-regexp (regexp-opt pico-events 'words))
(pico-metadata-regexp "\\(.\\|\n\\)*?__lua__$"))
`((pico-find-data1 . font-lock-comment-face)
(pico-find-data2 . font-lock-comment-face)
(,pico-constants-regexp . font-lock-constant-face)
(,pico-functions-regexp . font-lock-function-name-face)
(,pico-events-regexp . font-lock-builtin-face)
(,pico-keywords-regexp . font-lock-keyword-face))))
(define-derived-mode pico8-mode lua-mode "pico-8"
(setq font-lock-defaults '((pico8-font-lock-keywords))))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.p8\\'" . pico8-mode))
(provide 'pico8-mode)
pico-8 cartridge // http://www.pico-8.com
version 4
__lua__
-- pico-8 api test // by zep
-- demonstrates all pico-8
-- functions and some lua
cls() -- clear screen
rect(0,0,127,127,1)
-- play music from pattern 02
-- (loop back flag set in pat 13)
music(0)
-- play sound 0 on channel 3
sfx(0, 3)
-- draw palette
x=3
rectfill(1,1,7,7,5)
for i=0,15 do
print(i,x,2,i)
x = x + 6 + flr(i/10)*4
end
__gfx__
0000000000ff0ff044444444bbbbbbbb000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0700007000ff0ff04444444433333333000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0070070000fffff042444444b3b3b3b3000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0007700000f1ff104444444433333333000000004444424400000e00000000000000000000000000000000000000000000000000000000000000000000000000
0007700000fffff0444444442222222200070000444424240000e7e0000000000000000000000000000000000000000000000000000000000000000000000000
00700700000444004444424444444444007a70004444424400000e00000000000000000000000000000000000000000000000000000000000000000000000000
07000070000999004444444444444444000700004444444400000300000000000000000000000000000000000000000000000000000000000000000000000000
00000000000f0f004444444444444444000b00004444444400000b00000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
....
__gff__
0000010103010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0004040600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0303030300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202050200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
....
__sfx__
00040000350703507030070330702b070300702b070300603505027040270402b030290202b020300103301001000010000100001000010000100001000010000100001000010000100001000000000000000000
0110000028070000000000000000280700000029070000002b07000000000000000028070000002b070000002d070000002b07000000280700000000000000002607000000000000000024070000000000000000
0110000024070000000000000000280700000000000000001f0700000000000000002807000000000000000024070000000000000000280700000000000000001f07000000000000000028070000000000000000
0110000022070000000000000000260700000000000000001d0700000000000000002607000000000000000022070000000000000000260700000000000000001d07000000000000000026070000000000000000
01100000290700000000000000002d0700000000000000002b0700000029070000002607000000240700000022070000001c0701d070000000000000000000000000000000000000000000000000000000000000
....
__music__
01 01024141
02 03044141
00 40414141
03 40414141
00 41414141
00 41414141
00 41414141
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.