skeeto / impatient-mode

Impatient html mode. See your changes in the browser as you type
215 stars 19 forks source link

impatient-mode should start httpd and open the browser #22

Open hackerb9 opened 4 years ago

hackerb9 commented 4 years ago

Impatient mode is great, but it is klunky that you have to run two commands to start it, M-x httpd-start and M-x impatient-mode. Here is a patch which makes M-x impatient-mode start the httpd server if it isn't running yet and then load up the browser to the correct page.

;;;###autoload
(define-minor-mode impatient-mode
  "Serves the buffer live over HTTP."
  :group 'impatient-mode
  :lighter " imp"
  :keymap impatient-mode-map
  (if (not impatient-mode)
      (progn
        (imp--cleanup-timer)
        (remove-hook 'after-change-functions #'imp--on-change t))
    (add-hook 'kill-buffer-hook #'imp--cleanup-timer nil t)
    (add-hook 'after-change-functions #'imp--on-change nil t)
    (imp-remove-user-filter)
    (unless (process-status "httpd") (httpd-start))
    (let ((url (format "http://%s:%d/imp/live/%s/"
               (cl-case httpd-host
             ((nil) "0.0.0.0")
             ((local) "localhost")
             (otherwise httpd-host))
               httpd-port
               (url-hexify-string (buffer-name)))))
      (browse-url url))))

This could possibly be shortened by a call to (imp-visit-buffer) once the bug in that function (#19) gets fixed.