Skip to content

Instantly share code, notes, and snippets.

@hchbaw
Created November 4, 2009 15:10
Show Gist options
  • Save hchbaw/226115 to your computer and use it in GitHub Desktop.
Save hchbaw/226115 to your computer and use it in GitHub Desktop.
(defun* my-funcall-and-eval-last-sexp (before &optional (eval-last-sexp 'eval-last-sexp))
"Call 0 arg procedure BEFORE then call interactive command EVAL-LAST-SEXP."
(save-excursion
(funcall before)
(call-interactively eval-last-sexp)))
(defmacro define-my-eval-sexp* (eval-last-sexp inner-sexp inner-list)
(declare (indent 1))
`(progn
(defun ,inner-sexp ()
(interactive)
(my-funcall-and-eval-last-sexp (if (eq (char-after) ? )
'ignore
'forward-sexp)
',eval-last-sexp))
(defun ,inner-list (&optional arg)
(interactive "P")
(unless arg (setq arg 1))
(my-funcall-and-eval-last-sexp (lambda ()
(dotimes (_ arg) (backward-up-list))
(forward-list))
',eval-last-sexp))))
(defmacro define-my-eval-last-sexp1 (command-name eval-last-sexp)
"Define an interactive command COMMAND-NAME kind of EVAL-LAST-SEXP;
such that ignores any prefix arguments."
`(defun ,command-name ()
(interactive)
(let (current-prefix-arg)
(call-interactively ',eval-last-sexp))))
;; Entry point.
(defmacro define-my-eval-sexp (command-name-prefix eval-last-sexp)
(let ((my-eval-last-sexp1
(intern (format "my-%s1" (symbol-name eval-last-sexp)))))
`(progn
(define-my-eval-last-sexp1 ,my-eval-last-sexp1 ,eval-last-sexp)
(define-my-eval-sexp* ,my-eval-last-sexp1
,@(mapcar (lambda (post)
(intern (concat (symbol-name command-name-prefix) post)))
'("-inner-sexp" "-inner-list"))))))
;; elisp
(define-my-eval-sexp my-eval-sexp eval-last-sexp)
(define-key lisp-interaction-mode-map (kbd "s-c s-e") 'my-eval-sexp-inner-list)
(define-key lisp-interaction-mode-map (kbd "s-c s-c s-e") 'my-eval-sexp-inner-sexp)
(define-key emacs-lisp-mode-map (kbd "s-c s-e") 'my-eval-sexp-inner-list)
(define-key emacs-lisp-mode-map (kbd "s-c s-c s-e") 'my-eval-sexp-inner-sexp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment