help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: copy a link + hyperlink & text


From: John Mastro
Subject: Re: copy a link + hyperlink & text
Date: Fri, 3 Jun 2016 21:33:18 -0700

Sharon Kimble <boudiccas@skimble.plus.com> wrote:
> How can I copy a link with the text and the hyperlink all at once from
> either emacs w3m or from gnus please? Preferably all with one keypress?
> It will be pasted into a org-mode document as its destination.

By "the text" do you mean the link text you see in the rendered web
page? If so, I'm not aware of a w3m built-in function to get that,
though Alex and Emanuel provided solutions to get the linked-to URL in
w3m and gnus.

Here's something quick-and-dirty that kinda-sorta does what I believe
you're going for with w3m. I say kinda-sorta because (a) it only grabs
the full link text if point is on the first character, (b) I only
lightly tested it so there are probably some scenarios where it won't do
the right thing.

It copies the link-with-text in Org's external link format, since you
mentioned Org, but that would be easy to change.

(defun w3m-link-at-point ()
  "Return a list of (URL LINK-TEXT) for the link at point."
  (save-excursion
    (let ((url (w3m-anchor)))
      (when url
        (let* ((beg (point))
               (end (progn
                      (goto-char (next-single-property-change
                                  beg 'w3m-href-anchor))
                      (point))))
          (list url (buffer-substring-no-properties beg end)))))))

(defun w3m-copy-org-link ()
  "Copy the link at point to the kill ring in Org's link format."
  (interactive)
  (let ((href (w3m-link-at-point)))
    (if href
        (kill-new (format "[[%s][%s]]" (car href) (cadr href)))
      (user-error "No link at point"))))

Hope that helps

        John



reply via email to

[Prev in Thread] Current Thread [Next in Thread]