3

I would like a way to kill the URL under the point. I have tried mark-thing, next-visible-thing etc. from the thing-cmds package, but they either do not know how to move over URLs in nxml-mode or fail with an error.

goto-address-mode works for copying the URL, but it requires several extra steps (clicking on the link, switching to my external web browser window, and copying the URL from the browser address bar) and more importantly, does not work for URLs that redirect. I would like something more convenient.

  • Does nxml-mode highlight the links in any special way? (If so, you could use the face or some other text property to pick out the link w/o parsing). Is this something you need for nxml-mode only? (There's a way to do that in some other modes, such as w3m and org-mode). – wvxvw Jan 13 '16 at 15:06
  • It doesn't - but goto-address-mode does. – Robin Green Jan 13 '16 at 15:07
5

The url-get-url-at-point function from the url-util (emacs built-in) package should do the job for you. It is used in interactive commands like browse-url-at-point.

(require 'url-util)
(defun my/kill-url-at-point ()
  "Kill the url at point."
  (interactive)
  (kill-new (url-get-url-at-point)))

If you need to copy URL from an HTML page while in an emacs browser like eww, you can use the shr-copy-url command directly. The fun thing is that if you consecutively call that command twice while the point is on a hyperlink, it removes the link re-directions too!

From C-h f shr-copy-url:

shr-copy-url is an interactive compiled Lisp function in `shr.el'.

(shr-copy-url &optional IMAGE-URL)

For more information check the manuals.

Copy the URL under point to the kill ring. If IMAGE-URL (the prefix) is non-nil, or there is no link under point, but there is an image under point then copy the URL of the image under point instead. If called twice, then try to fetch the URL and see whether it redirects somewhere else.


Here's an example of how I use shr-copy-url in one of my eww snippets.

1

This is the way to get a run of text with the same face under point:

(defun face-under-point ()
  (interactive)
  (let ((face (face-at-point t))
        (start (point))
        (end (point))
        msg)
    (cl-loop while (eql face
                        (or (get-char-property start 'read-face-name)
                            (get-char-property start 'face)))
             do (decf start))
    (cl-loop while (eql face
                        (or (get-char-property end 'read-face-name)
                            (get-char-property end 'face)))
             do (incf end))
    (setq msg (buffer-substring-no-properties (+ start 1) end))
    (message "face under point was: %s" msg)))

Once I'm home I could look into how to also put this into yank stack.

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.