A 'hello world' example for a major mode in Emacs?

后端 未结 4 668
悲哀的现实
悲哀的现实 2020-12-14 21:25

Can anyone provide me with a hello world example for a major mode in emacs? I guess it\'s a beginner question, still I really like to write a major mode, both to learn emac

4条回答
  •  有刺的猬
    2020-12-14 22:10

    Ok, after some more googling I am at least one step furhter :

    (define-derived-mode sample-mode ...) 
    

    since the providing isn't defining the mode as I thought first.. This I found on :

    http://xahlee.org/emacs/elisp_syntax_coloring.html

    A very very nice site for emacs-lovers.

    With the help of that : I have made a HelloWorld example myself now : It's a (as small as possible) Csharp mode. I have used Euler1 as example rather than HelloWorld. The files you need to know about are :

    • the file the mode will be applied on Euler1.cs
    • the .emacs
    • and of course the mode itself

    Since a pic is worth, at least a bunch of words : all files on 1 screen :

    alt text

    But since this nice pic seems to disappear half the time (anyone a clue? Open in new tab always brings it on, and the url is ok) some words too :-) :

    1. The mode : cs-mode.el

      (setq myKeywords 
       '(("WriteLine" . font-lock-function-name-face)
         ("public\\|static\\|void\\|int\\|for\\|if\\|class"
      . font-lock-constant-face)))
      
      (define-derived-mode cs-mode fundamental-mode
        (setq font-lock-defaults '(myKeywords)))
      
      (provide 'cs-mode)
      
    2. The .emacs, that makes the .cs files open in the right mode :

    ;; cs
    (require 'cs-mode)
    (add-to-list 'auto-mode-alist '("\\.cs\\'" . cs-mode))
    

    And thats all : the cs-code itself is useless her, cause it won't show the effect of coloring the key-words. To see that see the pic, or open the pic in another tab/window.

    Cheers, ph

提交回复
热议问题