1

Is there a way to set the display margins for markdown-mode buffers? I've read https://www.gnu.org/software/emacs/manual/html_node/elisp/Display-Margins.html and various suggested solutions but nothing has worked for me. This is what I have currently:

(defun my-margins () (set-window-margins (selected-window) 30 30)) (add-hook 'markdown-mode-hook 'my-margins) 

However, nothing happens when I open a .md file. If I evaluate (my-margins) manually, however, then I get the desired result, until I switch to a different buffer, and then the margins are lost.

Ideally, I would like all markdown-mode buffers to have the desired display margins, regardless of whether I open a new frame, switch buffers, etc.

5
  • Are you trying to center the text in the window ? Commented Jul 3, 2023 at 17:54
  • @TristanRiehs Yes, that's my intention. Is there a better way to do this? Commented Jul 4, 2023 at 20:46
  • 1
    Yes, the visual-fill-column-center-text feature from visual-fill-column allows you to do it very easily. @jth Commented Jul 5, 2023 at 10:29
  • Also see github.com/rnkn/olivetti Commented Jul 28, 2024 at 1:33
  • stackoverflow.com/q/62911403/324105 and emacs.stackexchange.com/q/47676/454 are similar questions. (The former provides more dynamic behaviour; the latter is basically a duplicate but for fringes rather than margins, but with essentially the same solution.) Commented Jul 28, 2024 at 2:13

2 Answers 2

1

The simplest solution I know is:

(defun my-markdown-mode-hook () "Called in `markdown-mode-hook'." (setq left-margin-width 30 right-margin-width 30)) (add-hook 'markdown-mode-hook 'my-markdown-mode-hook) 

For details refer to https://emacs.stackexchange.com/a/47679/454 (which was about fringes rather than margins, but the relevant mechanisms are the same).

Note that set-window-buffer by default "resets WINDOW’s position, display margins, fringe widths, and scroll bar settings, based on the local variables in the specified buffer" (which is where the left-margin-width and right-margin-width buffer-local variables come into play).

Also remember that markdown-mode-hook only runs once for a given markdown buffer, whereas that buffer may go on to be displayed in many different windows during its lifetime, and so calling set-window-margins in markdown-mode-hook couldn't be the solution.

0

When you call the my-margins function you provided using markdown-mode-hook, selected-window does not return the window you expect. For example, if you call find-file test.md from the *scratch* buffer, selected-window will return the window displaying the *scratch* buffer, not the new one displaying the buffer of test.md.

To make your function work, you can call it from another hook, one where in the previous example selected-window would return the window displaying the markdown file. You can use window-configuration-change-hook.

We have to adapt my-margins since this hook is not only fired in markdown-mode :

(defun markdown-margins () (when (equal major-mode 'markdown-mode) (set-window-margins (selected-window) 30 30))) (add-hook 'window-configuration-change-hook 'my-margins) 
3
  • If the window displaying the *scratch* buffer in your example is repurposed to display the test.md buffer, then it's the exact same window object. The bigger problems are that the major mode hook only runs once whereas the buffer may be displayed in many different windows (so set-window-margins during the major mode hook isn't useful), and furthermore that set-window-buffer resets the window margins based upon the new buffer. If using window-configuration-change-hook for this, I would use the buffer-local hook value as shown in emacs.stackexchange.com/a/47679/454 Commented Jul 28, 2024 at 1:58
  • n.b. See the documentation for window-configuration-change-hook regarding how its global vs buffer-local values are used (for this and most of the other window change hooks), as they have notably different purposes. Commented Jul 28, 2024 at 2:08
  • @phils Thank you for explaining. The problem is much deeper than I thought. Commented Jul 29, 2024 at 3:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.