3

I need to get weblinks from a buffer (http....) and put them into scratch buffer. This code partially works, but the weblinks are inserted in the same buffer.

I'm new at programming.

(defun get-weblinks ()
  "Get weblinks from a buffer"
  (interactive)
  (let (p1 p2 url)
    (while (search-forward "http")
      (backward-char 4)
      (setq p1 (point))
      (search-forward "jpg")
      (setq p2 (point))
      (setq url (buffer-substring p1 p2))
      (insert url "\n")
      )))

How can I finish the function?

Thank you for your help.

  • The solution will likely entail either re-search-forward or re-search-backward with an appropriate regex that matches the entire link; and then use (match-beginning 0) and (match-end 0) to get the points for the beginning and ending of the link -- assuming the regex matches everything. You can use M-x re-builder to come up with a complete regexp; or you can Google for it and then check with re-builder to see if it matches them all. I saw several examples in my brief Google searches dealing with the generalized type of links with href or src, so you'll need to do some hunting. – lawlist Dec 3 '15 at 22:40
  • 1
    You can push the results into a list and then regurgitate the list when your all done -- e.g., (let (result my-list) ... *while* search stuff (setq result "thelink") (push result my-list) ... move to wherever or whatever buffer ... (mapc (lambda (x) (insert x "\n")) my-list)) – lawlist Dec 3 '15 at 22:50
  • This is the improved function. It is ok for now. The weblinks are printed line by line in message buffer. (defun get-weblinks () (interactive) (let (p1 p2 url) (while (re-search-forward "http.+jpg" nil t) (setq p1 (match-beginning 0)) (setq p2 (match-end 0)) (message (buffer-substring p1 p2) "\n") ))) This is very useful to me. Thank you. @lawlist – Lizardo Reyna Dec 4 '15 at 11:08
2
(defun get-weblinks ()
"Goto the beginning of the current buffer; perform the search and store the links;
go back to the point of origin; create a new buffer; iterate over the list and insert
the links into the new buffer; switch to the new buffer; go to the beginning of the
new buffer."
(interactive)
  (let (url-list)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "http.+jpg" nil t)
        (push
          (buffer-substring-no-properties (match-beginning 0) (match-end 0))
          url-list)))
    (with-current-buffer (get-buffer-create "*URL*")
      (mapc
        (lambda (url)
          (insert url "\n"))
        url-list))
    (switch-to-buffer (get-buffer "*URL*"))
    (goto-char (point-min))))
  • wow.. It works perfect, it is a very useful function to me. I'm motivated to learn more emacs. I will analyze your code to understand step by step. Thank you very much. – Lizardo Reyna Dec 5 '15 at 7:03
  • Nice, always something to learn from @lawlist code, thank you. – gsl Dec 5 '15 at 8:39

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.