0

I have a function (tika-outlhg-regexp) that sets custom outline headings based on major mode.

I activate this with major-mode hooks. Is there some other way?

(defun tika-addhook-hgptn ()
   (add-hook 'emacs-lisp-mode-hook #'tika-outlhg-regexp)
   (add-hook 'sh-mode-hook         #'tika-outlhg-regexp)
   (add-hook 'f90-mode-hook        #'tika-outlhg-regexp)
   (add-hook 'fortran-mode-hook    #'tika-outlhg-regexp)
   (add-hook 'latex-mode-hook      #'tika-outlhg-regexp)
   (add-hook 'plain-tex-mode-hook  #'tika-outlhg-regexp) )

Here is the major mode dependent function for setting custom outline headings.

(defun tika-outlhg-regexp ()

  (cond
    ((eq major-mode 'emacs-lisp-mode)
          (let ( (hrklevels tika-elisp-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels)
            (setq-local outline-level 'outline-level)))

    ((eq major-mode 'sh-mode)
          (let ( (hrklevels tika-bash-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels)))

    ((eq major-mode 'f90-mode)
          (let ( (hrklevels tika-fortran-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels)))

    ((eq major-mode 'fortran-mode)
          (let ( (hrklevels tika-f77-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels)))

    ((eq major-mode 'latex-mode)
          (let ( (hrklevels tika-latex-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels)
            (setq-local outline-level 'outline-level)))

    ((eq major-mode 'plain-tex-mode)
          (let ( (hrklevels tika-tex-hrklevels) )
            (setq outline-regexp
                  (concat (regexp-opt (mapcar 'car hrklevels)) "\\>"))
            (setq outline-heading-alist hrklevels))) ))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10
  • 2
    Does this answer your question? [How to customize major mode initialization with local variables?](https://emacs.stackexchange.com/questions/26274/how-to-customize-major-mode-initialization-with-local-variables) – Drew Jun 23 '23 at 19:24
  • If you suggest use of a local variable, how would that be implemented ? Would I still need the function `tika-outlhg-regexp` ? – Dilna Jun 23 '23 at 20:09
  • The question is about adding a major-mode hook function. What that function does can be anything. It can set buffer-local or non-buffer-local variables. And you can use a different hook function for each mode, instead of using the same function, which then dispatches on the mode. IOW, a mode-hook function should already be in the right mode. All of that - what you do in a hook function - is up to you. – Drew Jun 23 '23 at 20:54
  • Would you be ok with my approach of having `tika-outlhg-regexp` with a `cond` for each mode. Then using hooks as in `tika-addhook-hgptn` ? – Dilna Jun 23 '23 at 21:21
  • What? Both OP’s name and the number of his/her reputation changed! @Drew, did you notice that? Two days ago, his/her name was ‘Randy’; today, it is ‘Bennings’; now it is ‘Dilna’. His/Her reputation was 100-200 several hours ago and now it becomes 1000+! Drew, I have some screenshots that prove it. – shynur Jun 23 '23 at 21:55
  • @Drew: [Here](https://github.com/shynur/misc/tree/main/tmp/pictures), 2 PNGs and 1 PDF. – shynur Jun 23 '23 at 22:13
  • @shynur: (1) Yes, I believe this person changes name/user a lot - why, I don't know. (2) The user name changes, but the questions seem to be ~repeated. ;-) (3) I don't really care what their reputation is. (4) I'm not a moderator here - just a user like everyone else. You can raise the question with a moderator by clicking "Flag". – Drew Jun 24 '23 at 01:48

1 Answers1

-1

Would I activate this with hooks, or some other way?

Yes.

db48x
  • 15,741
  • 1
  • 19
  • 23