From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Michael Maurer Newsgroups: gmane.emacs.help Subject: Re: custom function getting initialized randomly Date: Mon, 27 Mar 2023 17:30:50 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24775"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: tomas@tuxteam.de Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Mar 27 17:31:46 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 1pgop0-0006AZ-Tc for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 27 Mar 2023 17:31:46 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pgooO-0000FQ-6f; Mon, 27 Mar 2023 11:31:08 -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 1pgooM-0000Eu-1E for help-gnu-emacs@gnu.org; Mon, 27 Mar 2023 11:31:06 -0400 Original-Received: from mail-ua1-x933.google.com ([2607:f8b0:4864:20::933]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pgooJ-0007wg-1c for help-gnu-emacs@gnu.org; Mon, 27 Mar 2023 11:31:05 -0400 Original-Received: by mail-ua1-x933.google.com with SMTP id s23so6632817uae.5 for ; Mon, 27 Mar 2023 08:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679931062; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mMIE42tKEFPmCrTF53en0QAILvD4E6tTesQ3odI60kU=; b=Vg1pd7Zf0PYK5vLmhIHdZ5R2cd/7Z3DtmrKe5dCi8qShAp1tGLiHtjM2cKvLZMEq1F pzGv0lf/jssjN5uIOtVLqT7/B0lCIh6Ce28mcoeDzJAbQf7u0EZpKkZJDdSEDwIE+stl S5fvj1VgHa9c7gkJXNRRY5bs4YYfIkRJ4Q/5keQ65btwk/+/hK/taLqnNb45eW02zCS9 cGx9RO+lkXoldfbRCvtU4lmGXDc3GfNz81kCr0sSDzhk29e7SLHo/y28RgnLa7NbSI0L NTVrU+FRd4YN2FiBezk8r71o3D13EEDWVM+VSIr9HKNtzqMv4oBoOvas607ypVPRpMNY 8D2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679931062; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mMIE42tKEFPmCrTF53en0QAILvD4E6tTesQ3odI60kU=; b=JN3vsni2vnAyePei6EETF7PDl++3iFgaZ8V7eFp10keoJLL3W8qAPhFtxRj+ynJ7rP Vs3pRXf0o4BOEPvX7pBfaNuFfgfdikgjuUZJ/MFJ6SyLDtOSrQAZOKNQxsnw4LNkQZSJ 6I+2UioD5cjc9B2nKL40rVfFA9iuf5MgTkgApYko/kMLPZITAX737Abmx6ym3/XuC4C7 aHiVGZmYVh0o+FUVYQc4/0oKZiRU2yx6TkcZLT25o1XkMHBjZYxEdqdJbIVB/yGEn1ah gK45760rlS7++7OPAW90Q+1e2FNhNhhLemHJo1a0VY/AmhIolgYOKqJAHi7l9zNpXGto aSwQ== X-Gm-Message-State: AAQBX9fY5QaTdRmFlONyctLDtI1sgKv0dHM4/vTe/Z3NherwNbGOByrr LF0ieHzAyfbTy2ogb1nsq9NUPexfr3wiZZ2y2CVhzQn9eLk= X-Google-Smtp-Source: AKy350bQOEHnjWZuz3w9Mol6z3q+B94vAnbXne0t1+TR3Q5wH0uRcCTVbrGKyUgG39SMRUNuwbJmmdrTYBSG5U+oavY= X-Received: by 2002:a05:6130:320:b0:688:c23f:c22f with SMTP id ay32-20020a056130032000b00688c23fc22fmr9327328uab.1.1679931061718; Mon, 27 Mar 2023 08:31:01 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::933; envelope-from=maurer.michael@gmail.com; helo=mail-ua1-x933.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, 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-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:143130 Archived-At: On Mon, 27 Mar 2023 at 17:21, wrote: > > On Mon, Mar 27, 2023 at 02:56:01PM +0100, Richard Copley wrote: > > On Mon, 27 Mar 2023 at 10:21, Michael Maurer wrote: > > > > > > 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) > > > > > > > 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. > You're right, that's something I forgot to paste. The function is preceded by (defvar prev-kill-string "") I'll take a look at closures and see how far I get on my own, ty.