Search code examples
emacselisphookmargins

Changing margin for emacs text-mode


The only way I found to change margins in emacs to my liking without things acting funny is this:

(add-hook 'window-configuration-change-hook
          (lambda ()
            (set-window-margins (car (get-buffer-window-list (current-buffer) nil t)) 24 24)))

I would like for this setting to be invoked only in text-mode and change back when I change to other modes. Somewhat naively I tried this:

(add-hook 'text-mode-hook
          (lambda ()
            (set-window-margins (car (get-buffer-window-list (current-buffer) nil t)) 24 24)))

But it's not working. What would be the right code to have the margins only change for buffers in text-mode?


Solution

  • Even though you can set the margins using set-window-margins, they are lost as soon as you change the window in any way. A better solution is to set the variables left-margin-width and right-margin-width. For example:

    (defun my-set-margins ()
      "Set margins in current buffer."
      (setq left-margin-width 24)
      (setq right-margin-width 24))
    
    (add-hook 'text-mode-hook 'my-set-margins)