【发布时间】:2012-02-02 20:19:33
【问题描述】:
所以我正在编写一些 elisp 来测试 Web 服务,但在语法高亮方面遇到了麻烦。我正在使用 url-retrieve-synchronously 来获取 HTTP 响应,然后编辑文本以获取我需要查看的 XML。不幸的是,语法高亮在返回的缓冲区中不起作用,即使我已将其设置为 nxml-mode 并在脚本中使用了“font-lock-fontify-buffer”。但是,如果我执行“M-x font-lock-fontify-buffer”,则突出显示会按我的预期工作。在 elisp 和在 emacs 中使用它有什么区别吗?
以下是我正在整理的脚本的相关部分。我事先承认这是我做过的第一个 elisp 脚本,而且我可能以一种非常不正确的方式做事,但到目前为止一切正常。
(defun modality-http-request (url args request-type)
(let ((url-request-method request-type)
(url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")))
(url-retrieve-synchronously url)))
(defun modality-http-get (url args)
(modality-http-request url args "GET"))
(defun modality-http-post (url args)
(modality-http-request url args "POST"))
(defun test-modality (test)
(interactive "s\Test: ")
(let ((buffer (modality-http-get (concat (get-modality-path) test) nil)))
(set-buffer buffer)
(setq modality-beginning (point))
(forward-paragraph)
(next-line)
(beginning-of-line)
(setq modality-end (point))
(delete-region modality-beginning modality-end)
(bf-pretty-print-xml-region)
(switch-to-buffer buffer)
(font-lock-fontify-buffer)))
(defun bf-pretty-print-xml-region ()
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char (point-min))
(while (search-forward-regexp "\>[ \\t]*\<" nil t)
(backward-char) (insert "\n"))
(indent-region (point-min) (point-max))
))
【问题讨论】:
-
通常情况下,您不必手动调用
font-lock-fontify-buffer,因为它与font-lock-mode相同,(我认为)默认启用。 -
好吧,无论出于何种原因,它都不能在 url-retrieve-synchronously 创建的缓冲区中工作。特别奇怪的是,如果我将缓冲区的内容复制并粘贴到我通过访问文件创建的缓冲区中,语法高亮显示是立即的。一定有一些关于缓冲区的东西我不明白。动态创建的缓冲区不会显示在“缓冲区”菜单中,尽管我可以从缓冲区列表函数中看到它。这是一个线索,还是我找错树了?
-
我已经删除了超级用户网站上的问题。我的错。
-
这可能是因为这是一个连接到异步进程的缓冲区,即它不包含任何开始的内容,并且在数据到达时被填充。我不认为 font-lock 旨在处理这种情况。我想你可以添加一个进程过滤器来听到数据何时到达并确保缓冲区是字体化的。