From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Andrea Greselin Newsgroups: gmane.emacs.help Subject: =?UTF-8?Q?Re=3A_Ignore_spurious_focus_events_for_=E2=80=98after=2Dfocus=2D?= =?UTF-8?Q?change=2Dfunction=E2=80=99?= Date: Mon, 18 Jan 2021 20:09:44 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34275"; mail-complaints-to="usenet@ciao.gmane.io" To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jan 18 20:11:01 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l1ZvZ-0008nJ-IV for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 18 Jan 2021 20:11:01 +0100 Original-Received: from localhost ([::1]:50306 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l1ZvY-0003zX-Kg for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 18 Jan 2021 14:11:00 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l1Zuy-0003zI-Oj for help-gnu-emacs@gnu.org; Mon, 18 Jan 2021 14:10:24 -0500 Original-Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]:40213) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l1Zuw-0004BI-QC for help-gnu-emacs@gnu.org; Mon, 18 Jan 2021 14:10:24 -0500 Original-Received: by mail-pl1-x633.google.com with SMTP id q4so9128521plr.7 for ; Mon, 18 Jan 2021 11:10:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=dwge2DNNxY2nJ0XUE1YuDJRys3Au0IZbkVnGtPNkJ5w=; b=blG2u54jrQXQyvLecXEU+7NEnZ3r5S3AEcRcgXo9P7d35hA/bfqgUlDZy6RjnJ0SCN BWJvakcNf4nmrxN2FK2HcGUqKNN2wy+uTYLwhdCXl2GijBpR/OdjOE+LP91su4/8VFSo qY/cfo+bE1dUbOkrdwPfh5k1s1L467soO2CuDsIa1R6Ijd4rNlW8v0WLSslXe5pN6LWx gYD7bwxYwHQlS/kqR2urw57BJanHSvPon9gE5euCy/kxBY9loVbn+RoME9FGCtIp19I5 PtB/105BUQjdHpB/rgk8QQWwH0UTLFjP0flrrm7StGcvLiCwGCRf+gZ2Lp0z5Q+1fzTw x/0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=dwge2DNNxY2nJ0XUE1YuDJRys3Au0IZbkVnGtPNkJ5w=; b=J5DJ1idT0i9K+e18Af5m+ipQSL0XzvU9YdVLI+q6z8S6iggsLsbJXJFqaqaXTLRIFN lLzVaHbgUcOlwTLld6PUvxR3MYRI3VuqGt9lEZ+L21Bv2Z3eEHxWuXeXtTX36bD2/c6a WBfKsat0fKs6FTYmo395bD8EGkhubSdWPTj7FmH9q0G1V1jjjnSip0qW3SaHP1XlTx6B /ETpGOihIcqANOVBsxKZyUyXV6cSaiuAj/nBIacJIUp5zpEKBXuiI2Wj0pqBDGdA7rtq ivMwKTCUyP7JzTkprXrf2NM1N58LU6TBGU20t+/5C+u/jJ7ExC7ngErhqiC95L4ayI/P bz6Q== X-Gm-Message-State: AOAM533cFRkJc09c5+owVKGsa7DQKZdwPRBOzRpHyF7tn9Glko2zx7RE F97AW2WpHis3bq0qnjpRofJYmXFN3vQW5xddeNMOWXTRqT2HKQ== X-Google-Smtp-Source: ABdhPJxkPOgMfN2rSST1lxlIpO7TijDS+RJ7rsEp9brA0H6RWfTAchn+v95jbDB3ceXttlj+q/JFKzQV7HZXXFegq6o= X-Received: by 2002:a17:90b:287:: with SMTP id az7mr863445pjb.70.1610997020843; Mon, 18 Jan 2021 11:10:20 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::633; envelope-from=greselin.andrea@gmail.com; helo=mail-pl1-x633.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:127290 Archived-At: I've settled with these home-made remakes of the obsoleted hooks: ;; Set up hooks to be run on focus in and focus out ;; Mutter sends spurious focus events, including sending focus out ;; events when Emacs never really was unfocused. ;; The timer filters out these false focus out events, while ;; checking the =E2=80=98last-focus-state=E2=80=99 allows ignoring repeat= ed events ;; of the same kind. ;; REVIEW: false focus out events are still received sometimes when ;; using the menu bar. ;; Adapted from a suggestion by DasEwigeLicht on ;; https://www.reddit.com/r/emacs/comments/kxsgtn/ignore_spurious_focus_events= _for/ (defvar focus-events-timer nil) (defvar last-focus-state (frame-focus-state)) (defvar my-focus-in-hook nil "Normal hook run when all frames lose input focus.") (defvar my-focus-out-hook nil "Normal hook run when a frame gains focus.") (defun change-of-focus-functions () (unless (equal last-focus-state (frame-focus-state)) (if (frame-focus-state) (run-hooks 'my-focus-in-hook) (run-hooks 'my-focus-out-hook))) (setq focus-events-timer nil) (setq last-focus-state (frame-focus-state))) (defun run-change-of-focus-functions-with-timer () (setq focus-events-timer (unless (timerp focus-events-timer) (run-at-time "0.05 sec" nil ; Found by trial and error. #'change-of-focus-functions)))) (add-function :after after-focus-change-function #'run-change-of-focus-functions-with-timer) ;; Test (defun focus-in-test () (message "focus-in")) (defun focus-out-test () (message "focus-out")) (add-hook 'my-focus-in-hook #'focus-in-test) (add-hook 'my-focus-out-hook #'focus-out-test) ;; Actual use (add-hook 'my-focus-out-hook #'do-auto-save) Please reply with suggestions if there's something wrong or less-than-optimal. Andrea