user1854496
user1854496

Reputation: 622

Trouble with defining simple emacs command

I'm new to emacs and newer to lisp

I'm trying to set Meta + spacebar to set the mark for highlighting text (at current cursor position). searching around online and experimenting I've ended up with the command

(global-set-key (kbd "M-SPC") 'push-mark nil nil 1)

The above command isn't working for me though, I'm getting an "incorrect number of arguments error".

Got the function definition,

    push-mark &optional position nomsg activate
from elisp manual here

    Position: nil for position should default to current cursor position
    nomsg: I don't care about (I think)
    activate: apparently isn't true by default so I need to set it to...something.

How would I format the command to pass in three values?

The error is definitely due to the push-mark function call as other functions such as backward-char (which I'm not passing inputs to) work correctly

Upvotes: 1

Views: 98

Answers (2)

Drew
Drew

Reputation: 30708

To add a bit to what @phils says:

  1. push-mark is not a command: its definition has no interactive spec. Note that phils's example includes (interactive) in the anonymous function, making it a command.

  2. Unlike push-mark, push-mark-command, as its name suggests, is a command. If you want the effect of push-mark then just bind push-mark-command, or better yet set-mark-command.

Upvotes: 2

phils
phils

Reputation: 73345

As C-hf global-set-key RET tells you, global-set-key takes two arguments: (global-set-key KEY COMMAND)

You're passing five arguments: (global-set-key (kbd "M-SPC") 'push-mark nil nil 1)

Hence "wrong number of arguments".

You can either supply the symbol for a named function, or an anonymous function / lambda.

e.g.: (global-set-key (kbd "M-SPC") (lambda () (interactive) (push-mark nil nil 1)))

Upvotes: 2

Related Questions