Skip to content

Instantly share code, notes, and snippets.

@svspire
Created December 18, 2020 01:43
Show Gist options
  • Save svspire/d5cf6744551b98e6ff264a3794696f4d to your computer and use it in GitHub Desktop.
Save svspire/d5cf6744551b98e6ff264a3794696f4d to your computer and use it in GitHub Desktop.
Defclass Emacs keyboard macro
;Add a command control-meta-s which expands a slot name or list of slot-names
;into a full slot specification.
; Based on fred-clos-help.lisp
; by Alan Ruttenberg
; [email protected]
; Converted to emacs lisp by Shannon Spires
;(foo bar)<control-meta-s>
;-->
;((foo :initarg :foo :initform nil :accessor foo)
; (bar :initarg :bar :initform nil :accessor bar))
(defun expand-slots-list (slots)
(let ((res (mapcar (lambda (slotname)
(list slotname :initarg (intern (concat ":" (symbol-name slotname)))
:initform nil :accessor slotname))
(if (atom slots)
(list slots)
slots))))
(if (consp slots) res (car res))))
(defun insert-full-slot-form (prefix)
"Expands a slot name or list of slot-names at point into a full slot specification."
(interactive "P")
(backward-sexp)
(setf myform (thing-at-point 'sexp))
(when myform
(kill-sexp)
(let ((beg (point)))
(setf myform (car (read-from-string myform)))
(pp (expand-slots-list myform) (current-buffer))
(indent-region beg (point)))))
(global-set-key (kbd "C-M-s") 'insert-full-slot-form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment