2
votes

So I'm hacking up some elisp to test a web service, and I'm running into trouble with syntax highlighting. I'm using url-retrieve-synchronously to get an HTTP response, then editing the text to get down to just the XML I need to see. Unfortunately, syntax highlighting doesn't work in the returned buffer, even though I've set it to nxml-mode and used "font-lock-fontify-buffer" in the script. However, if I do "M-x font-lock-fontify-buffer", the highlighting works as I would expect. Is there some difference between using it in elisp and from inside emacs?

Here are the relevant parts of the script I'm putting together. I admit up front that this is the first elisp scripting I've ever done, and I'm probably doing things in a ludicrously incorrect manner, but it's all worked thus far.

(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))
      ))
1
Typically, you don't have to call font-lock-fontify-buffer manually, as it does the same as font-lock-mode, which (I think) is enabled by default. - Lindydancer
Well, for whatever reason, it isn't working in the buffer created by url-retrieve-synchronously. What's particularly bizarre is that if I copy and paste the contents of the buffer into one I created by visiting a file, syntax highlighting is immediate. There must be SOMETHING about buffers I don't understand. The buffer created on the fly doesn't show up in the Buffers menu, although I can see it from the buffer-list function. Is that a clue, or am I barking up the wrong tree? - Lee Crabtree
I've deleted the question on the SuperUser site. My bad. - Lee Crabtree
It might be because this is a buffer connected to an asynchronous process, i.e. it does not contain anything to start with and is filled as data arrives. I don't think font-lock is designed to handle this case. I guess that you could add a process filter to hear when data has arrived and make sure the buffer is fontified then. - Lindydancer

1 Answers

1
votes

URL uses temporary/internal buffers (recognized by the fact that their name starts with a space). They're plain normal, but some functions treat them specially: font-lock will not be activated, and the buffer will usually not be shown to the user (e.g. C-x b TAB will not show those buffers). So either rename the buffer before enabling font-lock, or copy the text you need into another buffer whose name doesn't start with a space.