0

I need to change a space to a | here and there and i guessed that the best possibility were to bind a mouse click and the insertion of the pipe

(global-set-key [s-mouse-1] '(insert "|"))

But when I tested my idea I was told

Wrong type argument: commandp, (insert "|")

I have read 49.3.10 Rebinding Mouse Buttons but it did not mention which types of commands are bindable to mouse clicks.

gboffi
  • 594
  • 2
  • 19
  • Insert is a function but not a command. You need to write an interactive command for the mouse click to call. – Dan Sep 25 '18 at 11:52
  • @Dan Ah, I've tagged properly my question! Thank you for your comment. – gboffi Sep 25 '18 at 12:16

2 Answers2

1

A function for which commandp returns t is an interactive function, i.e. one which calls (interactive ...) as the first thing in its body. So you can write a function like this:

(defun insert-vertbar ()
   (interactive)
   (insert "|"))

and bind it to the mouse click:

(global-set-key [S-mouse-2] #'insert-vertbar)

interactive has a lot of wrinkles (which are not necessary in this case, but they can be valuable when you need to write a more complicated function): you can find out more by using your emacs's help facility: C-h f interactive, and/or looking it up in the the emacs lisp manual.

N.B. In my case, [S-mouse-1] was intercepted by the window manager before emacs could get to it, so I was not able to bind it to that. I had to use [S-mouse-2] instead.

NickD
  • 27,023
  • 3
  • 23
  • 42
0

With a bit of help here and there I have put together what I need

(defun insert-vertbar (ev)
  (interactive "e")
  (let ((p1 (posn-point (event-start ev))))
    (goto-char p1)
    (delete-char 1)
    (insert "|")))
(global-set-key [s-mouse-1] #'insert-vertbar)

I'm a bit doubtful that

  (let ((p1 (posn-point (event-start ev)))) ...

is the best — most clean way of changing the position of the cursor, if you'd like to comment you're welcome...

gboffi
  • 594
  • 2
  • 19