1

I am trying to make emacs open the url displayed under the current point when I left click and press the super key at the same time.

I am trying with this code but it does not work

(global-set-key (kbd "<s-mouse-1>") #'(lambda ()
                                   (interactive)
                                   (browse-url)
                                   (kbd "<return>")))

The (kbd "<return>") part would be used to invoke to invoke a return in the mini-buffer: when browse-url is called the user must confirm to open the url by pressing return in the mini-buffer.

  • Have you tried (browse-url (thing-at-point 'url t))? – DoMiNeLa10 Oct 21 '18 at 16:46
  • 1
    Have you unbound the mouse down binding? multiple-cursors.el has a explanation here: github.com/magnars/multiple-cursors.el#binding-mouse-events – Yuan Fu Oct 21 '18 at 17:33
  • @DoMiNeLa10 I get wrong type argument: stringp, nil when clicking – Nisba Oct 21 '18 at 18:05
  • @YuanFu I can bind the event, the problem is the code within the lambda which does not work – Nisba Oct 21 '18 at 18:06
  • Try (browse-url (browse-url-url-at-point)), that's what browse-url uses in its source. @DoMiNeLa10♦ 's snippet should work tho. You probably get that error because (thing-at-point 'url t) didn't find the url and returned nil. – Yuan Fu Oct 21 '18 at 19:26
0

You want to use the mouse to invoke your command, so you need the command to accept a mouse event as argument. This does what you want:

(global-set-key (kbd "<s-mouse-1>")
                (lambda (event)
                  (interactive (list last-command-event))
                  (posn-set-point (event-end event))
                  (browse-url (thing-at-point 'url t))))

(You might also need to set <s-down-mouse-1> to nil. Or you might need to change the code above to use <s-down-mouse-1>. It depends on what you're starting with.)


(kbd "<return>") does nothing in your code, BTW. It just returns the vector [return], which is a more internal way of writing the key sequence of hitting the Return key. Return such a vector from a function does not invoke a command.

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.