0

I'm beginner user of Emacs and I found trouble while trying to bind a custom function to a key.

I successfully bind some keys to edit-indirect-region using following code:

(global-set-key (kbd "C-c C-c C-e") 'edit-indirect-region)

Now what I am trying to achieve is to bind a custom function which first calls edit-indirect-region then switches to the buffer and calls scss-mode.

This is what I tried:

(defun edit-indirect-scss (beg end &optional display-buffer)
  (interactive)
  (edit-indirect-region beg end &optional display-buffer)
  (scss-mode))

(global-set-key (kbd "C-c C-c C-s") 'edit-indirect-scss)

I am receiving this kind of error:

funcall-interactively: 
Wrong number of arguments: (lambda (beg end &optional display-buffer) (interactive) (edit-indirect-region beg end &optional display-buffer) (scss-mode)), 0

How can I get it to work?

EDIT:

As @Drew suggested I did change my function definition to:

(defun edit-indirect-scss (beg end &optional display-buffer)
  (interactive)
  (edit-indirect-region beg end display-buffer))

but I'm still experiencing an error while trying to visual select few lines and then hit "C-c C-c C-s"

funcall-interactively: Wrong number of arguments: (lambda (beg end &optional display-buffer) (interactive) (edit-indirect-region beg end display-buffer)), 0
1

You have this

(defun edit-indirect-scss (beg end &optional display-buffer)
  (interactive)
  (edit-indirect-region beg end &optional display-buffer)
  (scss-mode))

This sexp passes 4, not 3, arguments to function edit-indirect-region:

(edit-indirect-region beg end &optional display-buffer)

Get rid of the argument &optional.

&optional is used as a keyword in a defining special form such as defun. You do not pass it as an actual argument to the function.

| improve this answer | |
  • Hi @Drew I still have the same error I did edit my question to let you help me. – FieryCod Feb 2 '18 at 15:34
  • 2
    (interactive "r") – jpkotta Feb 2 '18 at 16:49
  • Thanks @jpkotta now my code looks like this (defun edit-indirect-scss (begin end) (interactive "r") (edit-indirect-region begin end)) There are no error but the problem is that the buffer is created but Emacs did not switch to it. – FieryCod Feb 2 '18 at 17:45
  • Ok I managed it to work i will post a full answer today, thanks guys you are all great. – FieryCod Feb 2 '18 at 17:55
  • Glad it was helpful. But please keep in mind that emacs.SE is not a please-debug-my-code site. It is a Q&A site. (And comments can be deleted at any time.) Questions about code should be about something specific - one thing per question. – Drew Feb 2 '18 at 17:57
0

I'm posting the answer to fulfil the question. I got several issues with code which inhibited me from creating a proper function.

The first mistake is like @Drew noticed the &optional keyword.

The second mistake I made is that I should use (interactive "r"), it gets the region the user highlights and passes it to begin, end variables.

Here is how it should look like at this point.

(defun edit-indirect-scss (begin end)
  "Enables to quickly edit scss mode in the indirect buffer"
  (interactive "r")
  (edit-indirect-region begin end)
  (scss-mode))

The last part is that I should switch to a buffer which (edit-indirect-region begin end) creates. To achieve it used switch-to-buffer function.

Final working example:

 (defun edit-indirect-scss (begin end)
  "Enables to quickly edit scss mode in the indirect buffer"
  (interactive "r")
  (switch-to-buffer (edit-indirect-region begin end))
  (scss-mode))

The code solves the problem. It firstly gets the region highlighted by the user, then creates new indirect buffer and switch to it and then changes the mode of the buffer.

| improve this answer | |

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.