Вопрос

I like to font-lock lambda into λ in my lisp buffers. I do this with:

(defvar keyword-lambda
  '(("(\\(lambda\\)\\>"
     (0 (prog1 () (compose-region
                   (match-beginning 1)
                   (match-end 1) ?λ))))))
(font-lock-add-keywords 'emacs-lisp-mode keyword-lambda)

The character width of the symbol lambda becomes 1, and indent-sexp respects it, however not in temp buffers it seems. Verify with C-u C-x C-e in an elisp buffer:

(insert
 "\n"
 (with-temp-buffer
  (emacs-lisp-mode)
  (insert "(lambda () (interactive)\n   (foo))")
  (goto-char (point-min))
  (indent-sexp)
  (buffer-substring-no-properties
   (point-min)
   (point-max))))

The code indentation is:

(λ () (interactive)
        (foo))

which is the indentation as if λ was 5 chars wide.

Is there a way around this?

Это было полезно?

Решение

The font-lock-add-keywords serves to adorns the lambda with a little note saying "display it as just λ".

But the way font-lock is not applied "as the text gets inserted in the buffer". Instead it's applied "some time after the text is inserted in the buffer and before it's displayed", where the exact time when it happens depends on various factors and configurations. Also, font-lock is disabled in temp buffers.

All this means is that you need to force font-lock to do its thing before you call indent-sexp, e.g. by calling font-lock-ensure.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top