3

I'm trying to use a very simple minor mode I've written to personnalize the appearance of the buffer when I'm running R in ESS.

Basically, I've set the linespacing in my init file to increase it and want this line spacing be turned to nil when I load ESS.

Here is the .el file I've written so far.

(define-minor-mode ess_XXX-mode
  "Personal configuration of ESS'"
  :lighter " ess_XXX")

(setq line-spacing 1)
(set-face-attribute 'default nil :height 110)

(add-to-list 'minor-mode-alist '(ess_XXX-mode " ess_XXX"))

(provide 'ess_XXX-mode)

When I eval the buffer, this works but on all buffers. When I run Emacs, I'm not able to load the minor-mode...

I've added the following in my init file

(require 'ess_XXX-mode)
(add-hook 'iESS-mode-hook 'ess_XXX-mode)

but again this works on all the buffers without any restriction. It seems Emacs cannot find the minor mode (I've stored the .el file in my .emacs.d/ directory).

Drew
  • 75,699
  • 9
  • 109
  • 225
Guillaume
  • 179
  • 4

3 Answers3

4

You don't need a minor mode to customize the appearance of a major mode. You can do that more simply with a function called from the mode hook:

(defun my-ess-hook ()
    (setq line-spacing 1))

(add-hook 'iESS-mode-hook 'my-ess-hook)

(set-face-attribute 'default ...) will change the default face for the whole Emacs frame, it doesn't apply to a single mode or buffer alone. I'm not sure there is a straightforward way to customize the default face for one buffer or mode only.

Tyler
  • 21,719
  • 1
  • 52
  • 92
2

Your ess_XXX-mode.el file needs to be in a directory which is present in your C-hv load-path list.

Your ~/.emacs.d directory is not present in that list by default (and you should not add it).

If necessary, create a new directory such as ~/.emacs.d/lisp and add that to your load-path.

(add-to-list 'load-path (expand-file-name "~/.emacs.d/lisp"))
phils
  • 48,657
  • 3
  • 76
  • 115
  • OK, now my minor mode loads every time I launch Emacs. It seems I'm not able to differentiate the loading to a specific mode. – Guillaume Oct 04 '21 at 06:42
  • Of course -- `require` is for loading libraries. If you want to load-on-demand then use `autoload`. – phils Oct 04 '21 at 09:26
2

See the doc string for define-minor-mode: (define-minor-mode MODE DOC [KEYWORD VAL ... &rest BODY]). IOW, the code that should be executed when the mode is enabled (and also when the mode is disabled) has to be included as the BODY in the define-minor-mode form. Something like this:

(define-minor-mode ess_XXX-mode
  "Personal configuration of ESS'"    ; DOC
  :lighter " ess_XXX"                 ; KEYWORD-VALUE pairs
  ;; here comes the BODY
  (cond
    (ess_XXX-mode
     ;; mode was turned on
     ... code to execute when the mode is turned on ...
       )
    (t
     ;; mode was turned off
     ... code to execute when the mode is turned off ...
       )))

Many minor modes do not need a BODY at all, but since you are modifying global variables, it seems to me that yours does.

NickD
  • 27,023
  • 3
  • 23
  • 42