Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a function to a parameter in elisp

Tags:

emacs

elisp

I added the following code to my .emacs file.

(defun delete-right-window ()
  (interactive)
  (windmove-right)
  (delete-window))

(defun delete-left-window ()
  (interactive)
  (windmove-left)
  (delete-window))

(defun delete-below-window ()
  (interactive)
  (windmove-down)
  (delete-window))

(defun delete-above-window ()
  (interactive)
  (windmove-up)
  (delete-window))

(global-set-key (kbd "C-s-<right>") 'delete-right-window)
(global-set-key (kbd "C-s-<left>") 'delete-left-window)
(global-set-key (kbd "C-s-<down>") 'delete-below-window)
(global-set-key (kbd "C-s-<up>") 'delete-above-window)

As you can see, most of the codes are repetitive. I read How do I pass a function as a parameter to in elisp? and tried to refactor the code to passing windmove-* function as follow:

(defun delete-other-window (callback)
  (interactive)
  (funcall callback)
  (delete-window))
...

(defun delete-right ()
  (delete-other-window 'windmove-right))

And I bound keystroke like this:

(global-set-key (kbd "C-s-<right>") 'delete-right)

But when I hit C-s-<right> it doesn't work, only display Wrong type argument: commandp, delete-right in the mini buffer.

What am I missing or what should I do to work the code correctly?

like image 776
ntalbs Avatar asked Aug 17 '26 17:08

ntalbs


1 Answers

Here's a fix:

(defun delete-after (fn)
  `(lambda () (interactive)
      (,fn)
      (delete-window)))

(global-set-key (kbd "C-s-<right>") (delete-after 'windmove-right))
(global-set-key (kbd "C-s-<left>") (delete-after 'windmove-left))
(global-set-key (kbd "C-s-<down>") (delete-after 'windmove-down))
(global-set-key (kbd "C-s-<up>") (delete-after 'windmove-up))
like image 112
abo-abo Avatar answered Aug 19 '26 18:08

abo-abo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!