如何在函数中使用 HTMLIZE 并将本地缓冲区更改为 HTMLIZE 输出缓冲区?

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

我已经使用 emacs 六年了,只是刚刚开始了解细节。我在浏览器 activity 的初始化中有一个 hyrda,使用引擎模式、浏览器-url 和浏览器-url-缓冲区。我编写了一个新函数 'print-to-browser',可以将缓冲区 html 化并在默认浏览器中打开它。

当然 browse-url-of-buffer 作用于原始缓冲区而不是 htmlizes 输出缓冲区。我使用的 ADD-HOOK 有一个 LOCAL 或 GLOBAL 参数,可以切换 browse-url-of-buffer 作用于两个缓冲区或单独作用于原始缓冲区,而不是单独作用于新缓冲区。

htmlize 在那里的某个地方创建了一个新缓冲区,它可能被添加到缓冲区列表的末尾。我想传递 last-buffer,它调用所选 windows 缓冲区列表中的最后一个缓冲区,以浏览-url-of-buffer,或者将 html 化缓冲区的名称传递给 switch- to-buffer,然后调用 browse-url-of-buffer。

有人知道怎么做吗?

这是打印到浏览器的:

(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

这是来自 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://whosebug.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

大概有多种简单的解决方案。

我的第一个解决方案有效(到目前为止)。只需要阅读 elisp 入门,请参阅此处:http://ergoemacs.org/emacs/elisp_basics.html

这是打印到浏览器的工作方式:

(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)
(switch-to-buffer
(let ((a (buffer-name)) (b ".html"))
(concat a b)))
(run-hook-with-args 'htmlize-after-hook)
) ; end print-to-browser

htmlize-buffer returns html 化缓冲区,因此您可以将其用作 browser-url-of-buffer.

的缓冲区参数
(defun print-to-browser ()
  (interactive)
  (browse-url-of-buffer (htmlize-buffer)))

我通过对这些命令调用 describe-function (C-h f) 了解到这一点。