【问题标题】:Why doesn't font-lock-fontify-buffer work from elisp when it works from the minibuffer?为什么 font-lock-fontify-buffer 在 minibuffer 中工作时不能在 elisp 中工作?
【发布时间】: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 旨在处理这种情况。我想你可以添加一个进程过滤器来听到数据何时到达并确保缓冲区是字体化的。

标签: emacs elisp


【解决方案1】:

URL 使用临时/内部缓冲区(通过其名称以空格开头的事实来识别)。它们很普通,但有些函数会特别对待它们:字体锁定不会被激活,缓冲区通常不会显示给用户(例如 C-x b TAB 不会显示这些缓冲区)。 因此,要么在启用字体锁定之前重命名缓冲区,要么将所需的文本复制到另一个名称不以空格开头的缓冲区中。

【讨论】:

  • 就在我阅读这个答案之前,我做了第二个。制作一个新的缓冲区并复制到其中非常有效。
猜你喜欢
  • 2013-05-06
  • 2019-06-29
  • 2019-11-14
  • 2010-10-24
  • 2014-02-15
  • 2014-11-26
  • 2014-02-21
  • 2021-03-25
  • 1970-01-01
相关资源
最近更新 更多