writeroom-mode icon indicating copy to clipboard operation
writeroom-mode copied to clipboard

Weird wrong calculated width

Open ACEMerlin opened this issue 6 years ago • 10 comments

I only have writeroom-mode installed using use-package

(use-package writeroom-mode
  :init
  (setq writeroom-width 0.5)
  (global-set-key (kbd "C-c w") 'writeroom-mode))

Steps:

  1. open emacs (no frame-parameter whatsoever), press C-c w
  2. Weird width, so I change the code to this:
(defun writeroom--calculate-width ()
  "Calculate the width of the writing area."
  (if (floatp writeroom-width)
      (progn
	(message "%s" (window-total-width))
	(truncate (* (window-total-width) writeroom-width)))
    writeroom-width))
  1. goto step 1, and It prints out 96(the old width before fullscreen), which should be 192 when in fullscreen.
  2. but when I edebug this function and go step by step, it prints out 192 and show things correctly...
  3. if I fullscreen first then call writeroom-mode, it also works correctly.
  4. that's weird, you can see the screen capture below:

bug

ACEMerlin avatar Jan 09 '19 16:01 ACEMerlin

Strange, and unfortunately I can't replicate it. What OS and Emacn version are you using?

The fact that window-total-width returns an incorrect value suggest that it's a problem between Emacs and full-screen on your system, though. Not sure if I can do much about it in that case.

What happens if you build a small pause into writeroom--calculate-width?

(defun writeroom--calculate-width ()
  "Calculate the width of the writing area."
  (if (floatp writeroom-width)
      (progn
	(sleep-for 2)
        (message "%s" (window-total-width))
	(truncate (* (window-total-width) writeroom-width)))
    writeroom-width))

Perhaps it takes a few moments for the UI to catch up...

joostkremers avatar Jan 09 '19 21:01 joostkremers

yes, It works correctly with a tiny pause. After reading the document, I suggest the following code to fix this:

...
  (redisplay)
  (setq visual-fill-column-width (writeroom--calculate-width)
...

Of course, you can put (redisplay) anywhere you like

ACEMerlin avatar Jan 10 '19 06:01 ACEMerlin

Honestly, I'm a little hesitant to add (redisplay), because I don't know if it's the right way to deal with this. I've asked on the Emacs mailing list and will let you know if and when I get an answer.

If people advise me against adding (redisplay), it'll still be fairly easy to solve your issue, though. You could simply write a custom fullscreen function that calls (redisplay) and use that instead of the default fullscreen option. I'd be happy to help with the details.

But let's first wait and see what the mailing list says.

joostkremers avatar Jan 10 '19 20:01 joostkremers

I received some answers to my questions, but I haven't had time to go into them yet. My suggestion would be to add the (redisplay) to your local copy of writeroom-mode.el for the moment. Once I implement a permanent solution, I'll let you know.

joostkremers avatar Jan 13 '19 16:01 joostkremers

got it, thx

ACEMerlin avatar Jan 14 '19 12:01 ACEMerlin

I pushed a commit to the devel branch that hopefully fixes this. The cause of the problem is simply a race condition between the WM enabling full screen and Emacs. The fix involves calculating the width of the text area in the hook window-size-change-functions, which should run once full screen has been enabled. Apparently, however, there's no guarantee that this will indeed be the case...

Could you try the devel branch and see if it solves the issue for you?

joostkremers avatar Feb 12 '19 16:02 joostkremers

I just found that my proposed solution causes an annoying bug if a user doesn't actually activate full screen, so I'm gonna have to try and think of something else...

joostkremers avatar Apr 06 '19 21:04 joostkremers

I seem to be experiencing this issue too now. Does the redisplay fix not work?

ntdef avatar Mar 03 '20 19:03 ntdef

With redisplay, the chances of getting a correct result are better, but the underlying problem isn't solved, so I haven't adopted it. I've been meaning to look for a better solution but never got round to it.

joostkremers avatar Mar 04 '20 21:03 joostkremers

Thanks for the update. Any way I can help?

On Mar 4, 2020, at 4:12 PM, Joost Kremers [email protected] wrote:

 With redisplay, the chances of getting a correct result are better, but the underlying problem isn't solved, so I haven't adopted it. I've been meaning to look for a better solution but never got round to it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

ntdef avatar Mar 04 '20 22:03 ntdef