From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: graceful shutdown of non-interactive Elisp program Date: Sun, 14 Jun 2020 18:01:10 +0300 Message-ID: <83imft3d55.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="100013"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Paul Pogonyshev Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Jun 14 17:02:20 2020 Return-path: Envelope-to: ged-emacs-devel@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 1jkU9L-000PuU-VL for ged-emacs-devel@m.gmane-mx.org; Sun, 14 Jun 2020 17:02:19 +0200 Original-Received: from localhost ([::1]:56452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jkU9L-00036n-1H for ged-emacs-devel@m.gmane-mx.org; Sun, 14 Jun 2020 11:02:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36918) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jkU8L-0002Uj-Ok for emacs-devel@gnu.org; Sun, 14 Jun 2020 11:01:17 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:46624) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jkU8L-0002Ts-EE; Sun, 14 Jun 2020 11:01:17 -0400 Original-Received: from [176.228.60.248] (port=4793 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1jkU8K-00031o-Sq; Sun, 14 Jun 2020 11:01:17 -0400 In-Reply-To: (message from Paul Pogonyshev on Sat, 13 Jun 2020 23:42:51 +0200) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:252233 Archived-At: > From: Paul Pogonyshev > Date: Sat, 13 Jun 2020 23:42:51 +0200 > > I'm trying to make a Elisp program that is run in a non-interactive mode, i.e. essentially as `emacs --batch > --load myfile.el'. A normal way to shutdown terminal programs is with C-c, which is expected to be "graceful" > shutdown, e.g. the program still has a chance to save files etc. > > However, with Elisp I'm not sure how to achieve that except for constantly modifying `kill-emacs-hook', which > would be a nightmare from coding perspective. > > Naively I would expect this print "GOING DOWN" when aborted with C-c: > > $ emacs --batch --eval "(unwind-protect (while t) (message \"GOING DOWN\"))" > > For example, Python's handler of SIGINT raises an exception within the program, which unwinds the stack > as usual and, unless caught, cause program termination after cleaning up as expected (e.g. running all > `finally' clauses and closing all `with' context managers). However, in Elisp, as I understand, there is no way > to have a say in handling SIGINT other than adding a function to `kill-emacs-hook'. I don't think I follow. If you want to raise an exception from a kill-emacs-hook (assuming that you want the unwind-protect forms to do something), you can do that from kill-emacs-hook, no? And if you had a say in handling SIGINT, what would you do in the handler that you cannot do in kill-emacs-hook? Also, you mention batch mode, but is it relevant? That is, are you saying that Emacs behaves differently in an interactive session when it gets a fatal signal? I'm confused.