emacsdot-emacs

How to use HTMLIZE in a function and change the local buffer to HTMLIZE output buffer?


I'm six years into emacs and only just getting into the details. I have a hyrda in my init for browser activity, using engine-mode, browse-url, and browse-url-of-buffer. I've written a new function 'print-to-browser' that htmlizes the buffer and opens it in the default browser.

Of course browse-url-of-buffer acts on the original buffer and not htlmizes output buffer. ADD-HOOK, which I use, has a LOCAL or GLOBAL argument that toggles browse-url-of-buffer acting on both buffers or the original buffer alone, but not the new buffer alone.

Somewhere in there htmlize creates a new buffer which is probably added to the end of the buffer-list. I want either to pass last-buffer, which calls the last buffer from the selected windows buffer-list, to browse-url-of-buffer, or pass the name of the htmlized buffer to switch-to-buffer, then call browse-url-of-buffer.

Does anyone know how to do this?

Here is print-to-browser:

(defun print-to-browser ()
  "Depends Htmlize. Htmlize buffer, go there, send buffer to browser"
(interactive)
(add-hook 'htmlize-after-hook 'browse-url-of-buffer nil nil)
(htmlize-buffer)
(run-hook-with-args 'htmlize-after-hook)
) ; end print-to-browser

and here is the full code block from myinit.org:

#+BEGIN_SRC emacs-lisp
(use-package engine-mode
:after hydra
:commands hydra-search/body
:config
;(engine-mode t)
;engine mode configuration
(defengine duckduckgo
"https://duckduckgo.com/?q=%s"
;:keybinding "d"
)
(defengine github
"https://github.com/search?ref=simplesearch&q=%s"
;:keybinding "h"
)
(defengine google
"http://www.google.com/search?ie=utf-8&oe=utf-8&q=%s"
;:keybinding "g"
)
(defengine google-images
"http://www.google.com/images?hl=en&source=hp&biw=1440&bih=795&gbv=2&aq=f&aqi=&aql=&oq=&q=%s"
;:keybinding "i"
)
(defengine google-maps
"http://maps.google.com/maps?q=%s"
:docstring "Mappin' it up."
;:keybinding "m"
)
(defengine stack-overflow
"https://stackoverflow.com/search?q=%s"
;:keybinding "q"
)
(defengine wikipedia
"http://www.wikipedia.org/search-redirect.php?language=en&go=Go&search=%s"
;:keybinding "w"
:docstring "Searchin' the wikis.")
(defengine youtube
"http://www.youtube.com/results?aq=f&oq=&search_query=%s"
;:keybinding "y"
)
;custom function print buffer in browser
(defun print-to-browser ()
  "Depends Htmlize. Htmlize buffer, go there, send buffer to browser"
(interactive)
(add-hook 'htmlize-after-hook 'browse-url-of-buffer nil nil)
(htmlize-buffer)
(run-hook-with-args 'htmlize-after-hook)
) ; end print-to-browser

:bind 
("<C-m> i" . hydra-search/body)
:hydra 
(hydra-search (:color blue :hint none)
"
              ^Browser Search or Print^
--------------------------------------------
  _d_uck  _g_oogle   _i_mages   _w_iki 
  _m_aps  _y_outube  _s_tack    _h_ub     
  _u_rl   _p_rint    _h_tml     


"
     ("w" engine/search-wikipedia       "wikipedia")
     ("d" engine/search-duckduckgo      "duckduckgo")
     ("h" engine/search-github          "github")
     ("g" engine/search-google          "google")
     ("i" engine/search-google-images   "google-images")
     ("m" engine/search-google-maps     "google-maps")
     ("y" engine/search-youtube         "youtube")
     ("s" engine/search-stack-overflow  "stack-overflow")
     ("u" browse-url                    "browse-url-at-point")
     ("p" print-to-browser              "print-to-browser")
     ("h" browse-url-of-buffer          "buffer-to-browser")
))
#+END_SRC

Presumably there are multiple simple solutions.


Solution

  • htmlize-buffer returns the htmlized buffer, so you can just use that as the buffer arg to browser-url-of-buffer.

    (defun print-to-browser ()
      (interactive)
      (browse-url-of-buffer (htmlize-buffer)))
    

    I learned this by invoking describe-function (C-h f) on these commands.