0

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
      ;;Tab:4
      (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)))
    nil
    )
  (if (fboundp 'font-lock-flush)
      (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
        enabled."
  :type '(repeat symbol)
  :group 'kwds-gl-lock)

;;;###autoload
(define-global-minor-mode kwds-gl-global-mode
  kwds-gl-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)))
SFbay007
  • 554
  • 4
  • 13
  • `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 08 '20 at 01: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 08 '20 at 01: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 08 '20 at 01:18
  • So define a derived mode instead would perform the same purpose as the code above? – SFbay007 Jan 08 '20 at 01:20
  • 1
    Refer to `C-h i g (elisp)Major Modes` for information on writing major modes. – phils Jan 08 '20 at 01:23
  • At a glance, my impression is that there's an existing major mode `gl-mode` and you're trying to enhance it, but that maybe this `gl-mode` isn't derived from `prog-mode`? That makes it sound like (a) you want your custom `kwds-gl-mode` to derive from `gl-mode`, but (b) `gl-mode` might need to be modified to derive from `prog-mode` (assuming that's a sane thing to do)? It's hard to tell, because I don't have a `gl-mode` to look at. – phils Jan 08 '20 at 01:31
  • If it's *not* sane for `gl-mode` to be derived from `prog-mode` then you presumably don't actually want `prog-mode` involved in your `kwds-gl-mode` either (even if it seems like that would be convenient). – phils Jan 08 '20 at 01:35
  • Note that you can `(run-hooks 'prog-mode-hook)` directly, if you really really need to do that. – phils Jan 08 '20 at 01:38
  • There is no mode associated with this file type. With the above code it defaults to fundamental. I changed the code to include a derived mode instead. I am still not sure if associating the file types is done outside the definition of the derived mode as I did in the code or it could be done as part of the definition of the derived mode. The code is above in the question part. – SFbay007 Jan 08 '20 at 01:44
  • 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 08 '20 at 02:08
  • Sounds good. Thanks. – SFbay007 Jan 08 '20 at 02:10
  • Please clarify: in which sense "the indentation doesn't work", why you want to define a minor-mode rather than a major-mode, and why you think you need to enable prog-mode. – Stefan Jan 08 '20 at 02:53
  • Stefan, as I mentioned in an earlier comment. prog-mode already hooked to a number of modes that I want activated for any programming related file. Is there a disadvantage enabling it in this case for this mode? Regarding indentation when I hit TAB on a line, whatever code is there it moves away from the indented 4 space to another 4. I am trying to find a way to keep indentation as is, if the code is indented correctly and indent it to 4 spaces in case it is not. Based on the code above whenever I hit tab it always moves 4 spaces and pushes the code already indented 4 more spaces. – SFbay007 Jan 08 '20 at 03:13

0 Answers0