Setting window margins for particular buffers (org, in particular)

My objective:

My goal is that I want all my buffers to have the same basic margins; in org-mode, I have line numbers off for performance reasons, so I want to pad the margins. Here’s what it looks like now:

My config.el with line numbers

An org file without line numbers

I’ve tried:

I have the following attached to the org-mode-hook, to turn off line numbers (which it does) and change the window size (which it doesn’t):

  (defun jq-no-lines-but-gutter ()
    (doom-disable-line-numbers-h)
    (set-window-margins (selected-window) 3 3)
    )
  (add-hook 'org-mode-hook #'jq-no-lines-but-gutter)

However, it does work if I, at the top of the org buffer C-c C-c “Local setup has been refreshed”:

Request:

How can I make it that all my org buffers automatically have the marginal padding without having the line number mode? I didn’t see a different relevant hook on the org documentation, but I wouldn’t be surprised if I missed something obvious.

Thanks!

What specifically would I use? The variable that looks most relevant, visual-fill-column-extra-text-width isn’t accomplishing the job.

New code in config.el:

  (defun jq-no-lines-but-gutter ()
    (doom-disable-line-numbers-h)
    (setq visual-fill-column-extra-text-width '(3 . 3))    ; <<--- new
    )
  (add-hook 'org-mode-hook #'jq-no-lines-but-gutter)
  (add-hook 'org-mode-hook #'visual-fill-column-mode) ; <<--- new

but when I open an org buffer, nothing is padded:

Can you point me to the specific thing I should try with this package to accomplish the padding?

GitHub - elken/doom: Doom Emacs config is what I use to give

Thanks, that helps. I see that you’re using the (visual-fill-column-center-text t) which does push it from the margin, so that’s a help when my window is large enough to exceed visual-fill-column-width.

But if I’m working with emacs in a smaller window (which I regularly do), the text is “centered” and so pushed up against the left margin again.

Anyone else manage to adjust the margins? My (set-window-margins (selected-window) 3 3) works except that the org-mode-hook doesn’t load it. Anyone have a clue why that is the case?

Is something like this what you’re looking for? It sets the left margin for org buffers to give it some padding.

(add-hook! 'org-mode-hook
  (setq left-margin-width 2))

I like the sound of this, so I just tried it. It seems like this is fragile to window configuration changes. I’ll try whipping something up.

Mmm, I think I do like this. Here’s an unpushed excerpt from my config:

Display-wise, somehow I don’t mind code buffers without any margin on the left, but it feels a bit off with text buffers once the padding provided by line numbers is stripped away.

(defvar +text-mode-left-margin-width 1
  "The `left-margin-width' to be used in `text-mode' buffers.")

(defun +setup-text-mode-left-margin ()
  (when (and (derived-mode-p 'text-mode)
             (eq (current-buffer) ; Check current buffer is active.
                 (window-buffer (frame-selected-window))))
    (setq left-margin-width (if display-line-numbers
                                0 +text-mode-left-margin-width))
    (set-window-buffer (get-buffer-window (current-buffer))
                       (current-buffer))))

Now we just need to hook this up to all the events which could either indicate a change in the conditions, or a require the setup to be re-applied.

(add-hook 'window-configuration-change-hook #'+setup-text-mode-left-margin)
(add-hook 'display-line-numbers-mode-hook #'+setup-text-mode-left-margin)
(add-hook 'text-mode-hook #'+setup-text-mode-left-margin)

There’s one little niggle with Doom, as doom/toggle-line-numbers doesn’t run display-line-numbers-mode-hook, so some advice is needed.

(defadvice! +doom/toggle-line-numbers--call-hook-a ()
  :after #'doom/toggle-line-numbers
  (run-hooks 'display-line-numbers-mode-hook))

Lastly, I think I actually like this enough that I’ll go ahead and remove line numbers in text mode.

(remove-hook 'text-mode-hook #'display-line-numbers-mode)
2 Likes

This is perfect. To get it just where I wanted, I just made +text-mode-left-margin-width 3.

The code works across restarts, window resizings and such. Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.