From unknown Sat Jun 14 18:58:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#50815: 28.0.50; Alternative method to persist Comint history Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 26 Sep 2021 12:14:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 50815 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: To: 50815@debbugs.gnu.org X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.163265840431757 (code B ref -1); Sun, 26 Sep 2021 12:14:02 +0000 Received: (at submit) by debbugs.gnu.org; 26 Sep 2021 12:13:24 +0000 Received: from localhost ([127.0.0.1]:36086 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mUT24-0008G9-8q for submit@debbugs.gnu.org; Sun, 26 Sep 2021 08:13:24 -0400 Received: from lists.gnu.org ([209.51.188.17]:55980) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mUT22-0008Fz-DS for submit@debbugs.gnu.org; Sun, 26 Sep 2021 08:13:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47156) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mUT22-0003mh-4k for bug-gnu-emacs@gnu.org; Sun, 26 Sep 2021 08:13:22 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:35406) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mUT20-0005ui-Fu for bug-gnu-emacs@gnu.org; Sun, 26 Sep 2021 08:13:21 -0400 Received: by mail-wr1-x429.google.com with SMTP id i23so43009925wrb.2 for ; Sun, 26 Sep 2021 05:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:user-agent:mime-version; bh=/iEqaVmSz+TbDtV9FLp1QLLkEQJ3rH0eSA0Z3D3yCSM=; b=Wcc5vFM3DZy0vR9F4Ll6M5NYG6jgaszsIxuVhm6cZXrxgKW6IhEPhX9VLSpPWL6dW/ Vw+XoHF+3oRjUSeN7Wx13SdyFojEEehmuWHFz1sOslQBUdoz4fsQdTN2F6OIT1b6g0IX UQZU0WCUC+lDRoWGzeLlUxahTb1hcERkHTlUFzzvbJ5k8QUrDDN3GtnGEuOHilIXGFb6 zXdLTVhy2Jc2tSOxAJD6H5H1Y4oEpXwexntVFEYre/tDhveqHGpdy+ctkODCCj3CV6Xi UCVtDaogFB4yN+2vpjVi64Ma+xqqS7SJK2or0GQJg4nZfUl8p4OZMDmtra17J01WABip NvSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=/iEqaVmSz+TbDtV9FLp1QLLkEQJ3rH0eSA0Z3D3yCSM=; b=xJNiZjjbjOGJ1E2XTEegwsHaD3UF+s/SEgiF0KbgYQ4GaOfQJcrNzgYgzo/5FbySGY 9lgZaX6EchMqv41IgqhLEZp70eYGY6xqe/n76pXfyvT1jv93/aoykfNkh7gaxWpmFb/v vkl1LEUyLFtXwH6k24+E6yj3xIR7wqRxLkgBDSZRW7/THH8YJc4vVvto3ckWoYtNPtA7 wD2fEtXevhtVeXyg7G3+ACOSeP/yGUUVWPA+qPoTe+LXsS1qx+qX+qkJfjFF1j1TNHaG jQhI6ojthZ+UdMuHpjA2hw1aOmIn24RSKSxxbVTts9Y+TKuc2KRHqxCHrc5ZLnH1wEs4 U9IA== X-Gm-Message-State: AOAM533MYZsJzFJkNY8vZ5XL5BbD+0aj+ksJOW3SMak8owSzt6SH2ovC bn/mr46AjQA6cxWGidY0RHsYIWNwl6g= X-Google-Smtp-Source: ABdhPJywcEOkO4kxj8q6feTPj9ASoZw3yTVJNjRRa3Pg3Ue1y9OkFP/afYuy/5p8N/ni5Hz+N5+iVQ== X-Received: by 2002:a1c:7906:: with SMTP id l6mr10774647wme.78.1632658398293; Sun, 26 Sep 2021 05:13:18 -0700 (PDT) Received: from ars3 ([2a02:8109:8ac0:56d0::2f72]) by smtp.gmail.com with ESMTPSA id q7sm13857447wru.56.2021.09.26.05.13.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Sep 2021 05:13:17 -0700 (PDT) From: Augusto Stoffel Date: Sun, 26 Sep 2021 14:13:16 +0200 Message-ID: <87zgrzv8kj.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=arstoffel@gmail.com; helo=mail-wr1-x429.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-Spam-Score: -1.3 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) I was trying to make the Python shell persist its history and I couldn't make it work well with the existing method (`comint-read-input-ring' and `comint-write-input-ring'), for the following reasons: - Some user commands (initialization, eval region, completion) send code to the inferior process behind the scenes, and this ends up recorded in the interpreter's history file. - The Python interpreter is not as smart as bash when it comes to saving the history file. It fails to do so if it's killed abruptly (e.g., by killing the comint buffer). Somehow I couldn't make it work via a process sentinel either. - The IPython interpreter, on the other hand, is overly smart and uses a database instead of a plain history file. Now I found an alternative that works well for me. It relies on the savehist mechanism: (defvar-local comint-history-variable nil) (advice-add 'comint-add-to-input-history :after-while (lambda (cmd) (when comint-history-variable (add-to-history comint-history-variable (substring-no-properties cmd))))) (defun comint-history-setup () (setq comint-history-variable (intern (concat (symbol-name major-mode) "-history"))) (unless (boundp comint-history-variable) (set comint-history-variable nil)) (add-to-list 'savehist-minibuffer-history-variables comint-history-variable) (dolist (it (symbol-value comint-history-variable)) (ring-insert-at-beginning comint-input-ring it))) (add-hook 'inferior-python-mode-hook 'comint-history-setup) So the idea is to save each input to the comint twice: once in the buffer-local `comint-input-ring' (for the purposes of `M-n', `M-p', etc.), and once globally in the place pointed by `comint-history-variable' (for the purposes of persistence). This is a bit of a hack, but I think the only alternative would be to make ring.el interact well with `savehist-mode', a much more drastic change (but one that isearch might benefit from, for instance). Note also that many Comints have their version of `M-!', and this funny `comint-history-variable' could be used there as well---one more small reason to say it's a desirable feature. :-) Anyway, do you think this looks reasonable? Then I would format a patch (without the various corner-cuts of the above, of course). This wouldn't change the regular shell, but would add this new feature to comint.el and make the Python shell use it.