unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Is it ok to use (redisplay), or should this be left to the experts?
@ 2019-01-10 20:43 Joost Kremers
  2019-01-10 22:02 ` Stefan Monnier
  2019-01-11  7:24 ` Eli Zaretskii
  0 siblings, 2 replies; 8+ messages in thread
From: Joost Kremers @ 2019-01-10 20:43 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi,

I have a package for distraction-free writing[1], which, among 
other things, activates full screen and sets the text width to a 
user-defined value. There is an option to set the text width as a 
percentage of (window-total-width), which is now causing an issue, 
as reported by a user.[2]

The problem is that although full screen is activated before 
calculating the text width, (window-total-width) still reports the 
window width that existed before full screen was activated. This 
seems to be some sort of race condition, because adding (sleep-for 
2) before calling (window-total-width) solves the issue.

However, I don't want to force a one or two second wait on all my 
users just because in some conditions, on some system, there may 
be a race condition. (I can't reproduce the issue on my own 
computer, for example).

It seems that a call to (redisplay) also does the trick, but I'm 
hesitant to add this, because, well, I'm not sure why, exactly, 
it's just that I'm not entirely sure when, i.e., in what 
circumstances, (redisplay) is meant to be used, what side effects 
to watch out for (in any), etc.

So I guess what I'm asking is whether someone can tell me whether 
(redisplay) is the right way to deal with this issue, or whether 
there is a better way. Or perhaps the user is SOL and needs to do 
some local configuration to get things to behave?

TIA

Joost




Footnotes: 
[1]  https://github.com/joostkremers/writeroom-mode

[2]  https://github.com/joostkremers/writeroom-mode/issues/48

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-01-10 20:43 Is it ok to use (redisplay), or should this be left to the experts? Joost Kremers
@ 2019-01-10 22:02 ` Stefan Monnier
  2019-02-11 23:27   ` Joost Kremers
  2019-01-11  7:24 ` Eli Zaretskii
  1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2019-01-10 22:02 UTC (permalink / raw)
  To: help-gnu-emacs

> The problem is that although full screen is activated before calculating the
> text width, (window-total-width) still reports the window width that existed
> before full screen was activated.

Adding a call to `redisplay` is fine to handle such cases.
This said, it may be insufficient (e.g. the window-manager may actually
execute the full-screen request asynchronously).

I think a better solution is to use window-size-change-functions in
order to detect when the window-total-width changes and only change the
text width at that point.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-01-10 20:43 Is it ok to use (redisplay), or should this be left to the experts? Joost Kremers
  2019-01-10 22:02 ` Stefan Monnier
@ 2019-01-11  7:24 ` Eli Zaretskii
  2019-02-11 23:43   ` Joost Kremers
  1 sibling, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2019-01-11  7:24 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Joost Kremers <joostkremers@fastmail.fm>
> Date: Thu, 10 Jan 2019 21:43:44 +0100
> 
> The problem is that although full screen is activated before 
> calculating the text width, (window-total-width) still reports the 
> window width that existed before full screen was activated. This 
> seems to be some sort of race condition, because adding (sleep-for 
> 2) before calling (window-total-width) solves the issue.

Does (sit-for 0) also solve the issue?  If not, try values of argument
smaller than a second, starting from, say, 0.02, and see if some small
enough value resolves the problem.

> It seems that a call to (redisplay) also does the trick, but I'm 
> hesitant to add this, because, well, I'm not sure why, exactly, 
> it's just that I'm not entirely sure when, i.e., in what 
> circumstances, (redisplay) is meant to be used, what side effects 
> to watch out for (in any), etc.

There should be no problem with invoking 'redisplay', it just slows
down the program a bit, especially if the code in question will or
might be executed frequently.  However, sit-for is IMO better, because
in some use cases you must allow for the window manager to respond to
the full-screen request and for Emacs to receive a notification from
the window manager.  And that means waiting for some short time.

> So I guess what I'm asking is whether someone can tell me whether 
> (redisplay) is the right way to deal with this issue, or whether 
> there is a better way. Or perhaps the user is SOL and needs to do 
> some local configuration to get things to behave?

I cannot answer this because it isn't clear to me who initiated
full-screen: the user or the package.  If it's the package, then the
package should also accommodate these issues, perhaps only when the
frame size changes significantly.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-01-10 22:02 ` Stefan Monnier
@ 2019-02-11 23:27   ` Joost Kremers
  2019-02-12  3:58     ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Joost Kremers @ 2019-02-11 23:27 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Stefan,

Sorry about the late reply...

On Thu, Jan 10 2019, Stefan Monnier wrote:
>> The problem is that although full screen is activated before 
>> calculating the
>> text width, (window-total-width) still reports the window width 
>> that existed
>> before full screen was activated.
>
> Adding a call to `redisplay` is fine to handle such cases.
> This said, it may be insufficient (e.g. the window-manager may 
> actually
> execute the full-screen request asynchronously).
>
> I think a better solution is to use window-size-change-functions 
> in
> order to detect when the window-total-width changes and only 
> change the
> text width at that point.

The new text width only needs to be calculated once, so if I solve 
this with a function in `window-size-change-functions', the 
function would need to remove itself from that hook again. I guess 
it's feasible to call remove-hook inside the function, right?

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-01-11  7:24 ` Eli Zaretskii
@ 2019-02-11 23:43   ` Joost Kremers
  0 siblings, 0 replies; 8+ messages in thread
From: Joost Kremers @ 2019-02-11 23:43 UTC (permalink / raw)
  To: help-gnu-emacs


On Fri, Jan 11 2019, Eli Zaretskii wrote:
>> It seems that a call to (redisplay) also does the trick, but 
>> I'm 
>> hesitant to add this, because, well, I'm not sure why, exactly, 
>> it's just that I'm not entirely sure when, i.e., in what 
>> circumstances, (redisplay) is meant to be used, what side 
>> effects 
>> to watch out for (in any), etc.
>
> There should be no problem with invoking 'redisplay', it just 
> slows
> down the program a bit, especially if the code in question will 
> or
> might be executed frequently.

It's only called once, when the relevant minor mode is activated 
for the first time, so that shouldn't be an issue.

>  However, sit-for is IMO better, because
> in some use cases you must allow for the window manager to 
> respond to
> the full-screen request and for Emacs to receive a notification 
> from
> the window manager.  And that means waiting for some short time.

An unpredictable amount of time, I guess. Although it's unlikely 
to be very long, if I handle the problem by introducing an 
artificial delay, it would have to be long enough to handle the 
slowest of slow computers. Which might be a noticeable delay, 
which would inconvenience everyone else...

>> So I guess what I'm asking is whether someone can tell me 
>> whether 
>> (redisplay) is the right way to deal with this issue, or 
>> whether 
>> there is a better way. Or perhaps the user is SOL and needs to 
>> do 
>> some local configuration to get things to behave?
>
> I cannot answer this because it isn't clear to me who initiated
> full-screen: the user or the package.  If it's the package, then 
> the
> package should also accommodate these issues, perhaps only when 
> the
> frame size changes significantly.

Yes, it's the package that initiates full-screen. And I suspect 
that in most cases, it will be a significant size-change.

So I'm leaning toward Stefan's proposed solution, which is to use 
`window-size-change-functions'...

Thanks,

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-02-11 23:27   ` Joost Kremers
@ 2019-02-12  3:58     ` Stefan Monnier
  2019-02-12  8:06       ` Joost Kremers
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2019-02-12  3:58 UTC (permalink / raw)
  To: help-gnu-emacs

> The new text width only needs to be calculated once, so if I solve this with
> a function in `window-size-change-functions', the function would need to
> remove itself from that hook again.  I guess it's feasible to call
> remove-hook inside the function, right?

It should work fine, yes.  If not, please report it as a bug (and you
can work around it by postponing the removal to some silly one-shot
timer so it's removed from outside the function).


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-02-12  3:58     ` Stefan Monnier
@ 2019-02-12  8:06       ` Joost Kremers
  2019-02-12 14:45         ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Joost Kremers @ 2019-02-12  8:06 UTC (permalink / raw)
  To: help-gnu-emacs


On Tue, Feb 12 2019, Stefan Monnier wrote:
>> The new text width only needs to be calculated once, so if I 
>> solve this with
>> a function in `window-size-change-functions', the function 
>> would need to
>> remove itself from that hook again.  I guess it's feasible to 
>> call
>> remove-hook inside the function, right?
>
> It should work fine, yes.  If not, please report it as a bug 
> (and you
> can work around it by postponing the removal to some silly 
> one-shot
> timer so it's removed from outside the function).

One more question: can I assume that full screen will have been 
activated when the functions in `window-size-change-functions' are 
called, or do I need to do something like:

```
(while (not (> (window-pixel-width) 
(window-pixel-width-before-size-change)))
  (sleep-for 0.1))
```



-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is it ok to use (redisplay), or should this be left to the experts?
  2019-02-12  8:06       ` Joost Kremers
@ 2019-02-12 14:45         ` Stefan Monnier
  0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2019-02-12 14:45 UTC (permalink / raw)
  To: help-gnu-emacs

> One more question: can I assume that full screen will have been activated
> when the functions in `window-size-change-functions' are called, or do

Good question.  Maybe the fullscreen operation will trigger several
window size changes?  You'll have to see for yourself.  Ideally the
resize should be done in a single step, but the reality often falls
short of the ideal.

> (while (not (> (window-pixel-width)
> (window-pixel-width-before-size-change)))
>  (sleep-for 0.1))

The sleep-for should not be needed if you use
window-size-change-functions.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-02-12 14:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-10 20:43 Is it ok to use (redisplay), or should this be left to the experts? Joost Kremers
2019-01-10 22:02 ` Stefan Monnier
2019-02-11 23:27   ` Joost Kremers
2019-02-12  3:58     ` Stefan Monnier
2019-02-12  8:06       ` Joost Kremers
2019-02-12 14:45         ` Stefan Monnier
2019-01-11  7:24 ` Eli Zaretskii
2019-02-11 23:43   ` Joost Kremers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).