From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: custom function getting initialized randomly Date: Mon, 27 Mar 2023 17:20:22 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="sUiMOm6dY4enWFYm" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38960"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Mar 27 17:21:16 2023 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 1pgoeo-0009qB-Cr for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 27 Mar 2023 17:21:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pgoeF-0004eS-85; Mon, 27 Mar 2023 11:20:39 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pgoe5-0004eB-UM for help-gnu-emacs@gnu.org; Mon, 27 Mar 2023 11:20:30 -0400 Original-Received: from mail.tuxteam.de ([5.199.139.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pgoe3-0004lC-TY for help-gnu-emacs@gnu.org; Mon, 27 Mar 2023 11:20:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=d3I2I5T2dom6SvBEb5q2Q+k9SJH/bi5337eV1DS2Lr8=; b=oQle9RPxWyqn+hO540f2xZ/CRs z+DbWSpU0wdJ6IkifjrlumWDmsdQzLbgjotJAV00Ak+kOpKcLawNKdbJFTZbG0wsL3PVUT9FSe/0D DUvSU0BhusiOAY1fYek2Mlj9PHCyHAewKndcGmKMoy2v1oPESYkuK9+5VlR3uHfkvpWcErx9IwMa2 Dh5ld2YtKKfoUMAA4znO6zVp3LNvmABAfYhf3c8OKT9bG2pc77zkqDsNuoR1ru8IxohLseILzpKvZ vvHE/kqNzOh8WCkbzl2y+AWRYMRTIG4+y5ek+cZkN+nJCkEzVnqoDhHS1OouGrv6Ih2ErRsgnrQdw aYyjkexA==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.94.2) (envelope-from ) id 1pgody-0005QI-9z for help-gnu-emacs@gnu.org; Mon, 27 Mar 2023 17:20:22 +0200 Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 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-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:143129 Archived-At: --sUiMOm6dY4enWFYm Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 27, 2023 at 02:56:01PM +0100, Richard Copley wrote: > On Mon, 27 Mar 2023 at 10:21, Michael Maurer w= rote: > > > > Hello, > > > > I wrote the following function to copy the kill-ring to the scratch > > buffer, but it only gets initialized sometimes at the start of emacs, > > sometimes not. Why? > > > > (defun copy-to-scratch-on-kill > > () > > "Copy every kill (delete or yank) to *scratch* buffer." > > (let > > ((cur-kill-string > > (current-kill 0 t))) > > (when > > (and > > (not > > (equal cur-kill-string "")) > > (not > > (equal cur-kill-string prev-kill-string)) > > (get-buffer "*scratch*")) > > (with-current-buffer "*scratch*" > > (goto-char > > (point-max)) > > (insert cur-kill-string "\n") > > (goto-char > > (point-max)))) > > (setq prev-kill-string cur-kill-string))) > > > > (add-hook 'post-command-hook #'copy-to-scratch-on-kill) > > >=20 > If an error propagates from the hook function (most likely "Kill ring > is empty", signalled by `current-kill`) then Emacs removes the > function from the hook. See the docstring for variable > `post-command-hook`. There's more: where does the variable `prev-kill-string' come from? - if nobody has set it previously, this will be the error killing your hook (this is my hunch) - if somebody has, you are lucky if that somebody was you (otherwise you are into an entertaining debugging session If you are using a global variable like that, it makes sense to declare, document and perhaps initialize it with `defvar' Another possibility would be to hide it away in a closure [1] (you are using lexical binding, aren't you?). Just ask around here if the concept is unfamiliar to you. Cheers --=20 t --sUiMOm6dY4enWFYm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCZCG0LgAKCRAFyCz1etHa RgiaAKCBX19vVeDWd7ATtUcC4R/gpavJeACfe7STtOstHWByJkp/vPwVokthTAc= =qdad -----END PGP SIGNATURE----- --sUiMOm6dY4enWFYm--