0

Would it be possible that for a particular window width, I can automatically adapt the text to show up to a certain column number? If possible not via face attributes but using text-scale-mode at buffer-local level.

As example, use ruler-mode and change the window size until you get to column 32. Then call squeeze with desired column being 75. The result is not a window with extent of 75 characters on the ruler.

(defun squeeze (nc)
  (interactive "nDesired number of columns: ")

  (let ( (ncwin (window-width)) )
    (when (> ncwin 0)

      (let* ( (scaling-factor (/ (float nc) ncwin))
              (step text-scale-mode-step)
              (scale-steps (ceiling (/ scaling-factor step)))
              (print 1))

        (when (> nc ncwin)
          (setq scale-steps (* -1 scale-steps)))

        (text-scale-set scale-steps)) )))
Dilna
  • 1,173
  • 3
  • 10
  • The problem is that the levels used by `text-scale-set` are absolute, not relative to the current size (i.e. setting to level 1.1 will always be the same size and will not continue to scale the text). The scaling that occurs is relative to the height of face of the text in your buffer, not the current size of the display. Also, part of your problem with debugging may be that you are printing your scaling-factor as an integer in your `message` call rather than as a float. – D. Gillis Jul 14 '23 at 22:47
  • I'm afraid I don't understand what you're asking for. Can you please provide an example of the buffer before you apply the code and after you apply the code? Here's an example of a question with that kind of example, if you need an example: https://emacs.stackexchange.com/questions/75469/how-to-get-a-non-intersecting-overdue-next-7-days-agenda/75685#75685 – Trevoke Jul 14 '23 at 22:48
  • As for doing the scaling automatically, you could make a minor mode and remap `self-insert-command` to determine whether text-scaling should occur on each insertion. Org-mode does something similar using `org-self-insert-command`, which it puts into the keymap using `org-remap`. – D. Gillis Jul 14 '23 at 22:51
  • I think `visual-line-mode` might just do what you want (show the text on one line between both margins and hide the rest of the line) but somehow I think you want something else. – Trevoke Jul 14 '23 at 22:54
  • Also you might just be looking for `olivetti-mode`. https://github.com/rnkn/olivetti – Trevoke Jul 14 '23 at 22:55
  • I'm sorry, but I still don't understand. You say the size is 32x21 -- but 32 what and 21 what? Characters? If window-size is not in terms of characters, what is it in terms of? Can we have an example? How do you want the scaling to activate? Can we have an example? – Trevoke Jul 15 '23 at 00:45
  • Okay, I think we're almost there, but let me check. Let's say I launch emacs and I turn on ruler-mode, and I expand the window until column 32. Then I call `squeeze 75` and... The window changes size so it displays 75 characters? Or the font size changes so the window can display 75 characters? You said "the result is *not*" ... What *is* the result? – Trevoke Jul 15 '23 at 01:16
  • You call `squeeze 75`. The scale would be expected to change to fit 75 characters. The actual frame font does not change, but the text changes scale to fit 75 characters on the ruler in the local buffer. – Dilna Jul 15 '23 at 11:09
  • And what happens if you resize the window after you call `(squeeze 75)` ? – Trevoke Jul 15 '23 at 12:12
  • Nothing should happen if you resize the window. Only when calling `squeeze` should a rescaling be done. – Dilna Jul 15 '23 at 14:14

1 Answers1

0

I believe this package solves your problem.

fit-text-scale is an automation to set the scale so that the text uses the maximal space to fit in the window.

Scale stands for the zoom of the font.

There are three functions:

  • Choose the maximal text scale to still see the full line.
  • Choose the maximal text scale to still see the full lines.
  • Choose the maximal text scale to still see all lines of a buffer.
Trevoke
  • 2,375
  • 21
  • 34