johnsu01 (johnsu01) wrote,
johnsu01
johnsu01

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."
  (interactive)
  (let ((matches '())
        (no-check '("mailto"))
        (fails 0))
    (save-excursion
      (goto-char (point-min))
      (while
          (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*")
    (erase-buffer)
    (insert "Checking your links...\n\n")
    (mapc
     (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)))))))
     matches)
    (insert (format "\nFinish checking %d links. There were %d problems.\n"
                    (length matches) fails))))
Tags: emacs, lisp
Subscribe
  • Post a new comment

    Error

    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