Using kmacro-exec-ring-item, is it possible to wrap this in an elisp macro or function so its much less verbose?

In this example the only unique part is ds( and the function name.

 (lambda (&optional arg) "" (interactive "p")
   (kmacro-exec-ring-item (quote ([?d ?s ?( ] 0 %d)) arg)))

Could this be wrapped so it could be written as something more simple, for eg:

(fset 'delete-surround-parens (my-kmacro-wrapper "ds("))

I'm not sure how to convert the literal string into an array to pass to kmacro-exec-ring-item, but interested to know if its possible.

  • 1
    What are you really trying to do? Why do you want to make it less verbose? What is the problem you are trying to solve? disk space? memory? debugging ease? This sounds, so far, like it might be an X Y question. – Drew Nov 28 '16 at 15:28
;; Keyboard Macro
(fset 'delete-surround-parens "ds(")

;; or normal Function
(defun delete-surround-parens ()
  (command-execute "ds("))

ds( here is valid Emacs key representation, for complex keys, wrap that with kbd, for example,

(fset 'next-4-lines (kbd "C-u M-x next-line RET"))

Adding own answer since this cam be written as a macro:

(defmacro kmacro-fn (keys)
  `(lambda (&optional arg) "" (interactive "p")
     (kmacro-exec-ring-item (quote (,keys 0 "%d")) arg)))

Example use:

(fset 'delete-surround-backtick     (kmacro-fn [?d ?s ?`]))
(fset 'delete-surround-single_quote (kmacro-fn [?d ?s ?']))
(fset 'delete-surround-double_quote (kmacro-fn [?d ?s ?\"]))
(fset 'delete-surround-paren        (kmacro-fn [?d ?s ?(]))
(fset 'delete-surround-bracket      (kmacro-fn [?d ?s ?[]))
(fset 'delete-surround-brace        (kmacro-fn [?d ?s ?{]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.