From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: debounce (was: Some developement questions) Date: Tue, 04 Sep 2018 01:15:10 +0300 Organization: LINKOV.NET Message-ID: <87musyut1t.fsf_-_@mail.linkov.net> References: <444779489.8504194.1534538988289.ref@mail.yahoo.com> <444779489.8504194.1534538988289@mail.yahoo.com> <83sh3cfb3t.fsf@gnu.org> <87sh36inql.fsf@himinbjorg.adminart.net> <8336v6cvem.fsf@gnu.org> <8736v6icgt.fsf@himinbjorg.adminart.net> <83tvnmb958.fsf@gnu.org> <877ekigiiw.fsf@himinbjorg.adminart.net> <837ekhb2me.fsf@gnu.org> <87zhxcbmtr.fsf@himinbjorg.adminart.net> <83in409lub.fsf@gnu.org> <871sanb71j.fsf@himinbjorg.adminart.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1536014591 2670 195.159.176.226 (3 Sep 2018 22:43:11 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 3 Sep 2018 22:43:11 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu) Cc: emacs-devel@gnu.org To: hw Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Sep 04 00:43:07 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fwxYo-0000XJ-N8 for ged-emacs-devel@m.gmane.org; Tue, 04 Sep 2018 00:43:07 +0200 Original-Received: from localhost ([::1]:47221 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fwxau-0008Ai-Rn for ged-emacs-devel@m.gmane.org; Mon, 03 Sep 2018 18:45:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fwxan-00088V-8t for emacs-devel@gnu.org; Mon, 03 Sep 2018 18:45:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fwxai-00080s-Ar for emacs-devel@gnu.org; Mon, 03 Sep 2018 18:45:09 -0400 Original-Received: from pop.dreamhost.com ([64.90.62.162]:37784 helo=pdx1-sub0-mail-a22.g.dreamhost.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fwxah-00080N-O5 for emacs-devel@gnu.org; Mon, 03 Sep 2018 18:45:04 -0400 Original-Received: from pdx1-sub0-mail-a22.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a22.g.dreamhost.com (Postfix) with ESMTP id 210EB7F30A; Mon, 3 Sep 2018 15:45:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=linkov.net; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=linkov.net; bh=dhA2c+bu4mdV3RdzPyPbC0+aNpw=; b= hxmEYdxLjuwSBfsCV6ZXKyKn4q3dQTZCAQVlUElhIkxxjovzUUxkVzSB80tF/yvp kW/RT3DINuWUmZ/ngn+1+LPo7hUHlPZLcx2aGpRiLTC3KBehZZ9qGhOBoY8EV2HP /Da8Ce0dkjeE5l0OMrTzGWTWtF2tE9bc8952uNBGC14= Original-Received: from localhost.linkov.net (m91-129-101-104.cust.tele2.ee [91.129.101.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: jurta@jurta.org) by pdx1-sub0-mail-a22.g.dreamhost.com (Postfix) with ESMTPSA id 2266B7F0BB; Mon, 3 Sep 2018 15:45:00 -0700 (PDT) In-Reply-To: <871sanb71j.fsf@himinbjorg.adminart.net> (hw@adminart.net's message of "Sat, 25 Aug 2018 03:31:18 +0200") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.90.62.162 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:229220 Archived-At: --=-=-= Content-Type: text/plain >>> (setq Man-width 75) >> >> Emacs nowadays calculates the width dynamically, depending on the >> dimensions of the window. > > Why not make Emacs dynamically size it's windows to the width of the > display first? ;~O Implementing this in bug#32536 revealed that mouse drag events become too laggy when calling Man-update-manpage after every drag event on the window separator. JavaScript libraries provide a good solution that makes web UI more responsible, see https://duckduckgo.com/?q=javascript+debounce It could be good to have analogous in Emacs to e.g. make window resizing more responsible. So instead of (add-hook 'window-size-change-functions 'Man-window-size-change) in Man-mode this patch will allow (add-hook 'window-size-change-functions (debounce 'Man-window-size-change 2)) where the last arg means 2 seconds sliding window to wait, and make the final call once there are no more resizing events during that time period. Since its args are similar to the args of run-with-timer, it makes sense to add it to timer.el: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=debounce.1.patch diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el index 795554fec5..26c8426d3e 100644 --- a/lisp/emacs-lisp/timer.el +++ b/lisp/emacs-lisp/timer.el @@ -545,6 +545,22 @@ internal-timer-start-idle (dolist (timer timer-idle-list) (if (timerp timer) ;; FIXME: Why test? (setf (timer--triggered timer) nil)))) + + +(defmacro debounce (function secs) + "Postpone FUNCTION call until after SECS seconds have elapsed. +Return a debounced function that will postpone FUNCTION call +until after SECS seconds have elapsed since the last time +it was invoked." + (declare (indent 0) (debug t)) + (let ((timer-sym (make-symbol "timer"))) + `(let (,timer-sym) + (lambda (&rest args) + (when (timerp ,timer-sym) + (cancel-timer ,timer-sym)) + (setq ,timer-sym + (run-with-timer ,secs nil (lambda () (apply ,function args)))))))) + (provide 'timer) --=-=-=--