johnsu01 (johnsu01) wrote,

Checking Emacs buffers for broken links

This was a quick effort to write a command for checking the current buffer for broken links. Thoughts, feelings and improvements welcome.

(defun campaigns/check-buffer-urls ()
  "Checks the current buffer for broken links, skipping those
that match entries in the `no-check' list."
  (let ((matches '())
        (no-check '("mailto"))
        (fails 0))
      (goto-char (point-min))
          (re-search-forward thing-at-point-url-regexp nil t)
        (add-to-list 'matches (match-string-no-properties 0)) t))
    (pop-to-buffer "*Link checking results*")
    (insert "Checking your links...\n\n")
     (lambda (url)
       (let ((parsed (url-generic-parse-url url)))
         (insert (format "* %s ..." url))
         (if (member (elt parsed 0) no-check)
             (insert "SKIPPING\n")
           (condition-case err
               (if (url-http-file-exists-p url)
                   (insert "OK\n")
                 (insert "ERROR!\n")
                 (setq fails (1+ fails)))
             (error (insert "ERROR!\n")
                (setq fails (1+ fails)))))))
    (insert (format "\nFinish checking %d links. There were %d problems.\n"
                    (length matches) fails))))
Tags: emacs, lisp
  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment