Playdate on Emacs — some helpful elisp

Hi everyone,

Here is some Elisp that I use to make playdate dev a smidge easier. If there are any other emacs wonks, I'd consider turning it into en elisp package for distribution through melpa.

(use-package lua-mode)

(use-package luarocks)

(setenv "PLAYDATE_SDK_PATH" (expand-file-name "~/Developer/PlaydateSDK"))

(defun playdate/sdk-bin (binary)
  (shell-quote-argument (expand-file-name (concat (getenv "PLAYDATE_SDK_PATH")
                                                  "/bin/"
                                                  binary))))

(defun playdate/compile ()
   (interactive "")
   (playdate/expect-dir)
   (projectile-with-default-dir (projectile-acquire-root)
     (let* ((cmd-buf (with-current-buffer (get-buffer-create "*pdc-output*") (erase-buffer) (current-buffer)))
            (cmd-result
             (call-process "pdc"
                           nil
                           cmd-buf
                           nil
                           "source"
                           (playdate/pdx-dir)
                           ))
            (cmd-output (with-current-buffer cmd-buf (buffer-string))))
       (if (> cmd-result 0)
           (error (format "Compilation Failed(%d): %s" cmd-result cmd-output))
         (message cmd-output))
       cmd-result)))

(defun playdate/pdx-dir()
  (interactive)
  (expand-file-name (concat (playdate/game-name) ".pdx")
                    (projectile-project-root)))

(defun playdate/run-emu ()
  (interactive)
  (shell-command
   (concat "/usr/bin/open "
           "-a "
           (playdate/sdk-bin "Playdate Simulator.app")
           " "                          
           (shell-quote-argument (playdate/pdx-dir)))))

(defun playdate/compile-and-run ()
  (interactive)
  (playdate/expect-dir)
  (playdate/compile)
  (playdate/run-emu))

(use-package ini
  :straight t)

(defun playdate/read-pdxinfo ()
  (cdr (assoc "pdxinfo"
               (ini-decode (expand-file-name "source/pdxinfo" (projectile-project-root))))))

(defun playdate/game-name ()
  (cdr (assoc "name" (playdate/read-pdxinfo))))
4 Likes

How does one setup eMac support?
I have a iMac G3 with OSX 10.3, is it possible to make everything run on it with this snippet?


I just realized that it has absolutely nothing to do with Apples eMac line of computers.

1 Like

Updated the code a bit:

  • playdate/compile now stores the result of the last compile command in a buffer
  • playdate/compile errors out if there is an error during compilation.
  • playdate/compile returns the error code of the pdc command
  • playdate/compile-and-run exists

I'd love to get an LSP server working with emacs to hook in a debugger, but... I am so out of my depth with it. One day!

1 Like

Thanks for putting this together! Very helpful!

I just tried it out, and wanted to let you know it's not quite working as written -- it looks like the playdate/expect-dir function is missing, and I can't quite tell from context what it should be doing. I'm guessing it's just intended to verify that expected files/directories exist, since everything seems to work OK if I comment it out.

I may take a look at how the implementation compares to the Nova extension, since that seems to allow compile/run without a pdxinfo file (which I usually don't have for things I'm just experimenting with). I'll post back here if I get anything figured out.

Thanks again for sharing this :smile:

Well, that was easier than I expected! Not exactly robust, but this will let it fall back to projectile project name if there's no pdxinfo file (or some other sort of error reading the name out of it):

(defun playdate/read-pdxinfo ()
  (ignore-errors
    (cdr (assoc "pdxinfo"
                (ini-decode (expand-file-name "source/pdxinfo" (projectile-project-root)))))))

(defun playdate/game-name ()
  (or
   (cdr (assoc "name" (playdate/read-pdxinfo)))
   (projectile-project-name)))