0

How can I make this function callable only whilst in c-mode?

(defun nuket-c (actm)
  "Code templates for the C language"

  (interactive
   (list
    (let ( (cseq '("struct" "if" "ifelse" "ifladr" "ifword" "while"
           "swcnumic" "swcletr" "func")) )
      (completing-read "Nuke: " cseq nil t "struct"))))

  (pcase actm
    ("struct"    (insert nuket-c-struct))
    ;;-----------------------------
    ("if"        (insert nuket-c-if))
    ("ifelse"    (insert nuket-c-ifelse))
    ("ifladr"    (insert nuket-c-ifladr))
    ("ifword"    (insert nuket-c-ifword))
    ;;-----------------------------
    ("while"     (insert nuket-c-while))
    ("swcnumic"  (insert nuket-c-swcnumic))
    ("swcletr"   (insert nuket-c-swcletr))
    ;;-----------------------------
    ("func"      (insert nuket-c-func))) )
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

0

You cannot. You could have the function check the value of the major-mode variable. It could then exit early if called while in a buffer that isn’t in c-mode, perhaps after printing an error message.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • My only chance could me a minor-mode that is only activated on `c-mode`. – Dilna Oct 19 '22 at 20:51
  • No, the function would still be exist and be callable. All a minor mode does is apply a new keymap while the mode is active. – db48x Oct 19 '22 at 21:44
  • Are you sure? Can't one use a hook on a mode? Or perhaps there are some special constructs for doing so? – Dilna Oct 19 '22 at 22:42
  • The user can call any function which exists and is interactive. I suppose you could add a function to `c-mode-hook` which _defines_ your `nuket_c` function, and another on `kill-buffer-hook` that deletes the function, but that will break if the user opens two buffers in `c-mode` and then closes one; the user would try to use the function in the second buffer but it would be gone. This is the wrong thing to try to do. Just make the function check `current-mode`. Better yet, just don’t bother. – db48x Oct 20 '22 at 03:25
  • Will think more about it and seriously consider whether not to bother. – Dilna Oct 20 '22 at 04:13