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

后端 未结 4 679
悲哀的现实
悲哀的现实 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 21:58

    Well, let's start with this answer, which uses define-generic-mode.

    Flesh it out with some comment characters like: /* */, some keywords: hello hi etc., re-use the face from the original answer, a file extension .hello, and a function call to do further customization.

    There's the additional line to get autoloading working, but you have to generate the loaddefs.el file. That's more advanced than hello world.

    And, you end up with this:

    (make-face 'my-date-face)
    (set-face-attribute 'my-date-face nil :underline t)
    (set-face-attribute 'my-date-face nil :family "times")
    (set-face-attribute 'my-date-face nil :slant 'normal)
    (set-face-attribute 'my-date-face nil :height '340)
    
    ;;;###autoload
    (define-generic-mode hello-world
      '(("/*" . "*/"))                           ; comment characters
      '("hello" "hi" "howdy" "greetings" "hola") ; keywords
      '(("\\([0-9]+/[0-9]+/[0-9]+\\)"
         (1 'my-date-face)))                ; font lock
      '("\\.hello$")                        ; auto-mode-alist  
      '(hello-world-special-setup)          ; function-list
      "An example major mode.
    We have comments, keywords, a special face for dates, and recognize .hello files.")
    
    (defun hello-world-special-setup ()
      "Some custom setup stuff done here by mode writer."
      (message "You've just enabled the most amazing mode ever."))
    

提交回复
热议问题