emacs打开 html文件,在emacs中使用shr.el自动打开.htm .html文件

因为这是emacs,所以做你想做的最难的部分是决定什么是最好的方法。这在很大程度上取决于个人品味/工作流程。我强烈建议您更详细地查看browse-url包。我使用的一件事是一个允许我在使用eww或我的默认系统浏览器之间切换的功能 - 这意味着我可以轻松地在emacs或chrome / safari /中呈现web内容。

几年前,我写了一个实用程序,它允许我在emacs中查看许多不同的文件格式,包括渲染的html。我现在很少使用它,因为doc-view几乎取代了大部分功能并且更好。但是,它确实显示了如何使用defadvice来修改视图文件功能,以便id根据文件类型执行不同的操作。请注意,由于这是旧的emacs代码和emacs已经改进,现在可能有更好的方法。我也知道'建议'的东西已被重新制作,但这些传统的东西仍然可以正常工作。应该让你开始。请注意,MS doc,docx,pdf等的功能依赖于外部可执行文件。

我首选的工作流程是编写一个函数,允许我将browse-url-browser-function重置为eww-browse-url或browse-url-default-browser并将其绑定到密钥。然后,我可以选择在emacs或外部浏览器中显示html,并利用已在browse-url中完成的所有工作。

(require 'custom)

(require 'browse-url)

;; make-temp-file is part of apel prior to emacs 22

;;(static-when (= emacs-major-version 21)

;; (require 'poe))

(defgroup txutils nil

"Customize group for txutils."

:prefix "txutils-"

:group 'External)

(defcustom txutils-convert-alist

'( ;; MS Word

("\\.\\(?:DOC\\|doc\\)$" doc "/usr/bin/wvText" nil nil nil nil nil)

;; PDF

("\\.\\(?:PDF\\|pdf\\)$" pdf "/usr/bin/pdftotext" nil nil nil nil nil)

;; PostScript

("\\.\\(?:PS\\|ps\\)$" ps "/usr/bin/pstotext" "-output" t nil nil nil)

;; MS PowerPoint

("\\.\\(?:PPT\\|ppt\\)$" ppt "/usr/bin/ppthtml" nil nil nil t t))

"*Association for program convertion.

Each element has the following form:

(REGEXP SYMBOL CONVERTER SWITCHES INVERT REDIRECT-INPUT REDIRECT-OUTPUT HTML-OUTPUT)

Where:

REGEXP is a regexp to match file type to convert.

SYMBOL is a symbol to designate the fyle type.

CONVERTER is a program to convert the fyle type to text or HTML.

SWITCHES is a string which gives command line switches for the conversion

program. Nil means there are no switches needed.

INVERT indicates if input and output program option is to be

inverted or not. Non-nil means to invert, that is, output

option first then input option. Nil means do not invert,

that is, input option first then output option.

REDIRECT-INPUT indicates to use < to direct input from the input

file. This is useful for utilities which accept input

from stdin rather than a file.

REDIRECT-OUTPUT indicates to use > to direct output to the output

file. This is useful for utilities that only send output to

stdout.

HTML-OUTPUT Indicates the conversion program creates HTML output

rather than plain text."

:type '(repeat

(list :tag "Convertion"

(regexp :tag "File Type Regexp")

(symbol :tag "File Type Symbol")

(string :tag "Converter")

(choice :menu-tag "Output Option"

:tag "Output Option"

(const :tag "None" nil)

string)

(boolean :tag "Invert I/O Option")

(boolean :tag "Redirect Standard Input")

(boolean :tag "Redirect Standard Output")

(boolean :tag "HTML Output")))

:group 'txutils)

(defun txutils-run-command (cmd &optional output-buffer)

"Execute shell command with arguments, putting output in buffer."

(= 0 (shell-command cmd (if output-buffer

output-buffer

"*txutils-output*")

(if output-buffer

"*txutils-output*"))))

(defun txutils-quote-expand-file-name (file-name)

"Expand file name and quote special chars if required."

(shell-quote-argument (expand-file-name file-name)))

(defun txutils-file-alist (file-name)

"Return alist associated with file of this type."

(let ((al txutils-convert-alist))

(while (and al

(not (string-match (caar al) file-name)))

(setq al (cdr al)))

(if al

(cdar al)

nil)))

(defun txutils-make-temp-name (orig-name type-alist)

"Create a temp file name from original file name"

(make-temp-file (file-name-sans-extension

(file-name-nondirectory orig-name)) nil

(if (nth 7 type-alist)

".html"

".txt")))

(defun txutils-build-cmd (input-file output-file type-alist)

"Create the command string from conversion alist."

(let ((f1 (if (nth 3 type-alist)

output-file

input-file))

(f2 (if (nth 3 type-alist)

input-file

output-file)))

(concat

(nth 1 type-alist)

(if (nth 2 type-alist) ; Add cmd line switches

(concat " " (nth 2 type-alist)))

(if (nth 4 type-alist) ; redirect input (which may be output

(concat " < " f1) ; if arguments are inverted!)

(concat " " f1))

(if (nth 5 type-alist) ; redirect output (see above comment)

(concat " > " f2)

(concat " " f2)))))

(defun txutils-do-file-conversion (file-name)

"Based on file extension, convert file to text. Return name of text file"

(interactive "fFile to convert: ")

(let ((f-alist (txutils-file-alist file-name))

output-file)

(when f-alist

(message "Performing file conversion for %s." file-name)

(setq output-file (txutils-make-temp-name file-name f-alist))

(message "Command: %s" (txutils-build-cmd file-name output-file f-alist))

(if (txutils-run-command

(txutils-build-cmd (txutils-quote-expand-file-name file-name)

(txutils-quote-expand-file-name

output-file) f-alist))

output-file

file-name))))

(defadvice view-file (around txutils pre act comp)

"Perform file conversion or call web browser to view contents of file."

(let ((file-arg (ad-get-arg 0)))

(if (txutils-file-alist file-arg)

(ad-set-arg 0 (txutils-do-file-conversion file-arg)))

(if (string-match "\\.\\(?:HTML?\\|html?\\)$" (ad-get-arg 0))

(browse-url-of-file (ad-get-arg 0))

ad-do-it)))

(provide 'init-text-convert)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值