seagle0128 / grip-mode

Instant Github-flavored Markdown/Org preview using grip
GNU General Public License v3.0
275 stars 6 forks source link

Custom variable to define browser to use #17

Closed shubham-cpp closed 3 years ago

shubham-cpp commented 3 years ago

Having a custom variable to define a web browser for previewing orgs/markdown would be nice. Back when I was using nvim I used surf for previewing markdown files(the reason why I used surf was because it had only one task that is view markdown files) . I searched online and found that we could change default browser for emacs globally but I wouldn't like since I also use mu4e and would like to launch urls with firefox. Now using firefox only for previewing markdown/org file is overkill. I dont know enough elisp but this is what I have come up with by searching online

(defcustom grip-url-browser nil
  "Browser to launch Markdown/Org previews."
  :type 'string
  :group 'grip)

(defun grip--browser (url)
  "Use browser specified by user to load URL.
Use default browser if nil."
  (if grip-url-browser
      (let ((browse-url-generic-program grip-url-browser))
        browse-url-browser-function 'browse-url-generic))
  (browse-url url))

;; Then change browse-url to our custom function
(defun grip--browse-url (url)
  "Ask the browser to load URL.

Use default browser unless `xwidget' is available."
  (if (and grip-preview-use-webkit
           (featurep 'xwidget-internal))
      (progn
        (xwidget-webkit-browse-url url)
        (let ((buf (xwidget-buffer (xwidget-webkit-current-session))))
          (when (buffer-live-p buf)
            (and (eq buf (current-buffer)) (quit-window))
            (pop-to-buffer buf))))
    (grip--browser url)))

I dont know if this would work cause I dont have enough knowledge about elisp

seagle0128 commented 3 years ago

Good idea. The PR is welcome!

shubham-cpp commented 3 years ago

Does the code look right?. How can test it. I recently switched to doom emacs so dont know much about packages

seagle0128 commented 3 years ago

Just eval the code snippet to test.

BTW, I think grip--browser should be:

(defun grip--browser (url)
  "Use browser specified by user to load URL.
Use default browser if nil."
  (if grip-url-browser
      (let ((browse-url-generic-program grip-url-browser)
              (browse-url-browser-function 'browse-url-generic))
  (browse-url url))
shubham-cpp commented 3 years ago

Made the changes but its giving me error symbol's function definition void : browse-url-browser-function

seagle0128 commented 3 years ago
(defun grip--browser (url)
  "Use browser specified by user to load URL.
Use default browser if nil."
  (if grip-url-browser
      (let ((browse-url-generic-program grip-url-browser)
              (browse-url-browser-function 'browse-url-generic))
  (browse-url url))