I created a minor mode for some specific file extension to fontify the syntax and provide correct indentation.

1)The indentation doesn't work for some reason.

2) How to enable prog-mode when the minor mode is enabled?

(define-minor-mode kwds-gl-mode
  "kwds gl mode."
  nil " kg" nil
  (if kwds-gl-mode
      (font-lock-add-keywords nil kwds-gl)
    (font-lock-remove-keywords nil kwds-gl)
  ;; Don't know if this actually works
  (if kwds-gl-mode
      (setq tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120)))
  (if (fboundp 'font-lock-flush)
    (when font-lock-mode
      (with-no-warnings (font-lock-fontify-buffer)))))

(defcustom kwds-gl-modes '(gl-mode)
  "List of modes where Lisp Extra Font Lock Global mode should be
  :type '(repeat symbol)
  :group 'kwds-gl-lock)

(define-global-minor-mode kwds-gl-global-mode
  (lambda ()
    (when (apply 'derived-mode-p kwds-gl-modes) (kwds-gl-mode 1)))
  :group 'kwds-gl)

(add-to-list 'auto-mode-alist '("\\.gl\\'" . kwds-gl-mode))
(kwds-gl-global-mode 1)

Second version:

  (define-derived-mode gl-mode prog-mode "gl"
    "major mode for gl language code."
    (setq font-lock-defaults '(kwds-gl)))
  (add-to-list 'auto-mode-alist '("\\.gl\\'" . gl-mode))
  (add-hook 'gl-mode-hook (lambda () (flycheck-mode 0)))
  • prog-mode is a major mode, and is only used as base for deriving other major modes. Calling a major mode from a minor mode is an extremely uncommon thing to do in the first place, and calling prog-mode explicitly just sounds wrong in any case. Why do you want to do this? – phils Jan 8 '20 at 1:11
  • Thanks Phils, this minor mode is for a programming low level language and I want the files to have prog-mode applied so I can use other packages which get activated on prog-mode. Like: (add-hook 'prog-mode-hook #'flycheck-mode). Basically I want it to act as a C++-mode or python-mode. – SFbay007 Jan 8 '20 at 1:13
  • You almost certainly want to be defining a major mode then? You do that with define-derived-mode, and you would use prog-mode as the parent if there's nothing more appropriate. – phils Jan 8 '20 at 1:18
  • 1
    Refer to C-h i g (elisp)Major Modes for information on writing major modes. – phils Jan 8 '20 at 1:23
  • 1
    Adding to auto-mode-alist certainly happens outside of the mode definition -- otherwise it would never take effect (unless the mode was invoked by some other means). Remember that there are vast numbers of examples to examine in the Emacs codebase. Some modes are more complex than others, but you can generally find how things are normally done by looking at similar existing code. – phils Jan 8 '20 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.