1

Published on . Updated on .

Before this morning, I had two barebones Emacs commands which I used to open my website in a browser. One opened the index page in the build folder, and one opened the index page on the deployed site.


(defun mj/browse-my-website ()
  (interactive)
  (browse-url "https://www.maxwelljoslyn.com"))

(defun mj/browse-my-website-locally ()
  (interactive)
  (browse-url site-build-folder))

Since I frequently used the “browse-locally” command to check how the site looks before deploying, but rarely used the command to browse the deployed site, I turned the latter into an option triggered by passing an argument.

More substantially, browsing the index page is still the default, but if I’m viewing a webpage’s source in Emacs, the command instead browses that page on the (local or deployed) site. This is usually what I want when checking pages.

Here is the new and improved command.


(defun mj/browse-my-website (arg)
  "If visiting a webpage source file,
  open corresponding page on the site.
  Otherwise, browse the homepage. 
  
  Local browsing is the the default.
  With prefix ARG, browse deployed site instead."
  (interactive "P")
  (let ((site-name
         (if arg
           "https://www.maxwelljoslyn.com/"
           site-build-folder))
        (visited-file (buffer-file-name))
        (path))
    (cond
     ((not visited-file)
      (setq path "index"))

     ((string-match blog-post-regex visited-file)
      (setq path  (concat "blog/"
                          (match-string 1 visited-file))))
     ((string-match article-regex visited-file)
      (setq path (match-string 1 visited-file)))

     (t
      (setq path  "index")))
    (browse-url (concat site-name path))))

Tags: emacs, upgrade
Go back to Blog Index