Skip to content

Instantly share code, notes, and snippets.

@beshrkayali
Last active November 15, 2025 11:02
Show Gist options
  • Select an option

  • Save beshrkayali/bea7596284e7a2025ce8ffa8996cfe7c to your computer and use it in GitHub Desktop.

Select an option

Save beshrkayali/bea7596284e7a2025ce8ffa8996cfe7c to your computer and use it in GitHub Desktop.
Poly RaspberryPi PIO mode
;; Simple Polymode for RaspberryPi Pico PIO files that uses
;; both asm-mode as host-mode and c-mode for pass-through section
(define-hostmode poly-pio-hostmode
:mode 'asm-mode)
(define-innermode poly-pio-c-innermode
:mode 'c-mode
:head-matcher "^% c-sdk {$"
:tail-matcher "^%}$"
:head-mode 'poly-head-tail-mode
:tail-mode 'poly-head-tail-mode)
(define-polymode poly-pio-mode
:hostmode 'poly-pio-hostmode
:innermodes '(poly-pio-c-innermode))
(add-to-list 'auto-mode-alist '("\\.pio$" . poly-pio-mode))
@8dcc
Copy link

8dcc commented Nov 2, 2025

There is a bug in this code. The poly-pio-c-innermode symbol should be defined using define-innermode instead of define-auto-innermode, since the latter expects a :mode-matcher which isn't provided (nor necessary). See the documentation of pm-inner-auto-chunkmode (used by the former), opposed to pm-inner-chunkmode (used by the latter).

Without this change, Emacs would print the following warnings:

error in polymode-post-command: (pm-switch-to-buffer 89): Unbound slot: pm-inner-auto-chunkmode, "#<pm-inner-auto-chunkmode pio-c>", mode-matcher, oref
Error during redisplay: (poly-lock-function 1) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 88) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 169) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
[...]
Error during redisplay: (poly-lock-function 1) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 88) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 169) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
error in polymode-post-command: (pm-switch-to-buffer 89): Unbound slot: pm-inner-auto-chunkmode, "#<pm-inner-auto-chunkmode pio-c>", mode-matcher, oref
Error during redisplay: (poly-lock-function 1) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 88) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 1) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 88) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)
Error during redisplay: (poly-lock-function 169) signaled (unbound-slot pm-inner-auto-chunkmode "#<pm-inner-auto-chunkmode pio-c>" mode-matcher oref)

Furthermore, I would edit the values of :head-mode and :tail-mode so they are not highlighted using asm-mode syntax; I changed it to 'poly-head-tail-mode. The full definition is:

(define-innermode poly-pio-c-innermode
  :mode 'c-mode
  :head-matcher "^% c-sdk {$"
  :tail-matcher "^%}$"
  :head-mode 'poly-head-tail-mode
  :tail-mode 'poly-head-tail-mode)

For example:

image

@beshrkayali
Copy link
Author

@8dcc thanks for the tips!

@8dcc
Copy link

8dcc commented Nov 15, 2025

Since then, I have written a pio-mode package, which contains a major mode that is derived from asm-mode but only with the instructions specified by Raspberry Pi in their datasheets. In case someone is interested: 8dcc/pio-mode.el.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment