10

Long ago, I came across a text editor function that replaced the word 'lambda' with the character 'λ'. I would like to do this in emacs. My preferences:

  • I would like to activate this function in certain modes only (activate it for Scheme-mode but turn it off for C-mode for example).

  • At work I still use an older version of emacs, so the solution should work for emacs22 and emacs23.

How should I proceed?

1 Answer 1

8

You create a mode hook, which replaces lambda with the Greek character.

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

Then you add this hook to your mode:

   (add-hook 'emacs-lisp-mode-hook 'sm-greek-lambda)
6
  • 2
    It would be better to rename sm-lambda-mode-hook by sm-replace-lambda or sm-greek-lambda. XXX-mode-hook is usually used to identify hooks ran by run-hook by the specified mode and thus to which you can use add-hook.
    – Daimrod
    Commented Jan 26, 2012 at 12:11
  • Is make-char just there to support 22?
    – Jon Purdy
    Commented Jan 26, 2012 at 13:08
  • @Daimrod: Can you please explain the difference between a XXX-mode-hook and this custom one defined in the answer? Or at least point me to a useful documentation?
    – rahmu
    Commented Jan 26, 2012 at 13:29
  • @rahmu: xxx-mode-hook is usually the name of the hook ran - with run-hooks - by the mode xxx. i.e. emacs-lisp-mode-hook is ran when the function emacs-mode is called. In this case, no hook is ran but a function is added to a hook. Look at the Elisp Manual (Hooks) for more information.
    – Daimrod
    Commented Jan 26, 2012 at 14:10
  • @rahmu: Just to add a small example, you can do (add-hook 'emacs-lisp-mode-hook 'some-function) but (add-hook 'sm-lambda-mode-hook 'some-function) will never work unless you've some piece of code that does (run-hooks 'sm-lambda-mode-hook).
    – Daimrod
    Commented Jan 26, 2012 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .