PrettyLambda

Last edit

Summary: fixed

Changed:

< <pre>

to

> {{{

Changed:

< </pre>

to

> }}}


Using Character Composition

This page is about displaying the word ‘lambda’ using the Greek letter. See also PrettySymbol.


Library pretty-lambdada.el defines commands, including minor modes (local and global), that let you do this, either for specific modes or generally. The core display code is similar to the following snippet:

(defun pretty-lambdas ()
  (font-lock-add-keywords
   nil `(("(\\(lambda\\>\\)"
          (0 (progn (compose-region (match-beginning 1) (match-end 1)
                                    ,(make-char 'greek-iso8859-7 107))
                    nil))))))

There are several versions of such snippets floating around. The one above is from LawrenceMitchell, modified from StefanMonnier’s rewrite of OliverScholz’s rewrite of LukeGorrie’s original pretty-lambda.el code (http://fresh.homeunix.net/~luke/misc/emacs/pretty-lambda.el).

And here is a variant by TrentBuck:

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

[new] I stopped using this hack [1] because Emacs was actually saving weird escape characters in my files such that when I reloaded them I’d still get lambda-symbols, even with this turned off. But it makes the files unreadable to other programs. Does anyone else have this problem, and know the fix? Since it’s all done in font-lock I didn’t expect the saved files to be affected. – LukeGorrie

[new]

This should happen only if the file was saved in the coding system Emacs Mule, because emacs-mule preserves the composition property. (Composition is actually part of Emacs’ m17n support.) Normally you should not use emacs-mule; however, auto-save files are saved in that coding system. So if you restore a file after a crash, for instance, you can get something like what you describe. A fix would be to use a hook to remove all composition properties before auto-saving. – OliverScholz

Using Font-Lock and Overlays

This library defines a minor mode that uses font-lock and overlays to display ‘lambda’ as the Greek letter: http://dishevelled.net/elisp/lambda-mode.el.

[new] I use the misc-fixed font instead of the default, so lambda-mode displayed what looked like a Korean character. You can fix it by adding to your .emacs:

(setq lambda-symbol (string #x1d77a))

Using an Image

Sugawara offers another approach: a minor mode that displays ‘lambda’ as a large Greek letter using an image.

(defvar big-lambda-image
  (create-image
   (base64-decode-string
    "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigk
JyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVF
RUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wAARCAAwAEADASIA
AhEBAxEB/8QAGgABAQEBAAMAAAAAAAAAAAAAAAUEAwIGB//EACgQAAEEAQIEBgMAAAAAAAAAAAEA
AgMEERIhBTFhcQYUIjJBgRVRkf/EABcBAQEBAQAAAAAAAAAAAAAAAAABAwL/xAAcEQEBAQACAwEA
AAAAAAAAAAAAAQIRIQMxQVH/2gAMAwEAAhEDEQA/APriIiAThZqvEKd5zxUtQzmP3CN4dj+Lyu1G
XqklaRzmxyDDtJwSM7jsRsehWHiUbKlrh1qJgYWTCu7TtmN/p09g7QfpaYznXX1FVERZqIiICIiA
ofiK6IvJV2t1vdZhkfv7I2yNy4/ZAHforM80deCSaZ4ZHG0uc48gBuSoTqE13g1+1Kwi5dj1MYec
bW7xs+uZ6krfwyTU1r0lewIuNOyy5ThsR+yZgeOxGV2WNnF4qiIigIiIJd+N/EL8NIsd5WPE87iN
n4PoZ13GT0AHyqiIurrmSfgixT/gpJa9hjxRc4vgmYwuEeTksdgbYJJB5YOPjffQuOvNllETo4Ne
InPBaZG4GXYO4Gc4/eMrWi61ua7s7QREWav/2Q==")
 'jpeg t))

(defvar big-lambda-font-lock-keywords
  '((".+" (0 (prog1 nil
               (big-lambda-remove-region
                (match-beginning 0) (match-end 0)))))
    ("(\\(lambda\\)\\>"
     (0 (prog1 nil
          (big-lambda-region (match-beginning 1) (match-end 1)))))))
  
(defun big-lambda-remove-region (beg end)
  "Remove big lambda property in region between BEG and END."
  (let (pos)
    (while (setq pos (text-property-any beg end 'display big-lambda-image))
      (remove-text-properties
       pos
       (or (next-single-property-change pos 'display) end)
       '(display)))))

(defun big-lambda-region (beg end)
  "Add big lambda property in region between BEG and END."
  (put-text-property beg end 'display big-lambda-image))

(define-minor-mode big-lambda-mode
  "Display big lambda."
  :lighter " Lambda"
  (if big-lambda-mode
      (progn
        (save-restriction
          (widen)
          (let ((font-lock-keywords big-lambda-font-lock-keywords))
            (font-lock-fontify-buffer)))
        (font-lock-add-keywords nil big-lambda-font-lock-keywords))
    (font-lock-remove-keywords nil big-lambda-font-lock-keywords)
    (save-restriction
      (widen)
      (let ((modified-p (buffer-modified-p)))
        (big-lambda-remove-region (point-min) (point-max))
        (set-buffer-modified-p modified-p)))))

(defun big-lambda-mode-turn-on ()
  "Turn on `big-lambda-mode'."
  (interactive)
  (big-lambda-mode 1))

(add-hook 'emacs-lisp-mode 'big-lambda-mode-turn-on)

CategoryDisplay