From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>
Received: from mp11.migadu.com ([2001:41d0:2:4a6f::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms5.migadu.com with LMTPS
	id CNNSDryT6GOnKgEAbAwnHQ
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Sun, 12 Feb 2023 08:22:36 +0100
Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp11.migadu.com with LMTPS
	id KG9GDryT6GPKYQEA9RJhRA
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Sun, 12 Feb 2023 08:22:36 +0100
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by aspmx1.migadu.com (Postfix) with ESMTPS id ECDAA268A
	for <larch@yhetil.org>; Sun, 12 Feb 2023 08:22:35 +0100 (CET)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <emacs-orgmode-bounces@gnu.org>)
	id 1pR6gF-0004KK-0i; Sun, 12 Feb 2023 02:21:48 -0500
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 <arthur.miller@live.com>)
 id 1pR6gB-0004KB-S4
 for emacs-orgmode@gnu.org; Sun, 12 Feb 2023 02:21:44 -0500
Received: from mail-vi1eur05olkn2065.outbound.protection.outlook.com
 ([40.92.90.65] helo=EUR05-VI1-obe.outbound.protection.outlook.com)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <arthur.miller@live.com>)
 id 1pR6g8-0006KB-No
 for emacs-orgmode@gnu.org; Sun, 12 Feb 2023 02:21:43 -0500
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=UKCInynKAXEJQ7QU8ctoxqLevI9fgUZbyBJ9Wdl95MMXtdkGiVFJK8o6O3qexe4uWutQwEaaPMw0CM7xjKw9Orh4WGDfecvZcwMEEdj4ENO2IzncDmk49fJBuA7dwCStnwWnOls/+CU4SXHtA2BbrALWohH+tfwfSeD0qZc1JjmTsOPLHzpijVqr+qBlerhw2vF0oczkXklE3HOR9MJFu47iYS3ghujLSVgZjJdimVKfyg265DsyUKF2enYXLOU3wySGd/CJ17n83IFcbl9FB5YptVoEbCs02ao0+7AoLQiTCqjvB9aBvYiTn6TOc89f3h8VAwHROU3tjOViQBMMZQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; 
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
 bh=3tt/c1p1JJ6c2v81qM7T8eVKntSXVLts0g2UGkM/V1M=;
 b=HOGRVO2QwV8Ys+5ENXIVWtZmJpmUuwScPh1l+DMYRwET0HH4hjWOG5vfbfviW96Q911btH6OfkuabaPHwyBUCYMbwxF8mJRjOgWPTjkYqGYpVsW2jx0fnf/ZCq7qx+rEIORzO6YF3Gb4YX6GgD7TLmumRHOdCw3DvENSBD6h5CKGO6GUjNp4TtM2v86wyFhK7QIFcD8MgCpJW1ruk+Y07aOced3CLUyEgCaqJOR1NDagcy2y6Pfbz34Rcl2kNstiqPWN0E9JxZC9lT/I6CP1N5X+qbN9M+GwRaf/bASc1D4Ox54z6fX2uPeign/WvLjeYr+0aw6Cu6A2EWyOWctNAA==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
 dkim=none; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; 
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=3tt/c1p1JJ6c2v81qM7T8eVKntSXVLts0g2UGkM/V1M=;
 b=toWg8gLt+B/EJSkJKlgnXBvMuZg/nwQ8tfeuX/q6FDlB7aRJMDdoQKA78YXCt+Ba1aU0+mNxuQOJn2cS2iK1pIzWwaf1gNEB9ssQCraEvdHtSuvKDCwL7WfVVUgPLOxAPo8OCgQmAkVPWKA9rXiVShNoqPlyTLpQyCh4rCASkgF8BS7IFUfZNYLJx+RG0HULej8wOSd2X7IcibTX2vny3p/acOOncaWsaEoRgsQX6rL1krkAKB4indND+vaYJETB/1lAaztz7uKVshwBhllurhDKAs9mlfpsCH512iWsvpEhTzqdu7cqJKfY6tKS5MFnuLhsQhe1uODIdcf9xk+sgw==
Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20)
 by AM7PR09MB4213.eurprd09.prod.outlook.com (2603:10a6:20b:11f::11)
 with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.19; Sun, 12 Feb
 2023 07:21:35 +0000
Received: from AM9PR09MB4977.eurprd09.prod.outlook.com
 ([fe80::f2af:9752:58df:ad9]) by AM9PR09MB4977.eurprd09.prod.outlook.com
 ([fe80::f2af:9752:58df:ad9%7]) with mapi id 15.20.6086.023; Sun, 12 Feb 2023
 07:21:35 +0000
From: Arthur Miller <arthur.miller@live.com>
To: Bruno Barbier <brubar.cs@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Problem with let/cl-letf binding stuff with org-capture
In-Reply-To: <63e7eb19.050a0220.56539.6aa7@mx.google.com> (Bruno Barbier's
 message of "Sat, 11 Feb 2023 20:23:03 +0100")
References: <AM9PR09MB4977E7E66F2E6BD0345DF46496DE9@AM9PR09MB4977.eurprd09.prod.outlook.com>
 <63e69450.050a0220.9052f.652e@mx.google.com>
 <AM9PR09MB4977E771D98F9997736E0B7F96DF9@AM9PR09MB4977.eurprd09.prod.outlook.com>
 <63e74a8b.df0a0220.7f45d.5b05@mx.google.com>
 <AM9PR09MB4977306B77F9A5338764324596DF9@AM9PR09MB4977.eurprd09.prod.outlook.com>
 <63e7eb19.050a0220.56539.6aa7@mx.google.com>
Date: Sun, 12 Feb 2023 08:21:33 +0100
Message-ID: <AM9PR09MB49778B9E1DAC014FAB17920796DC9@AM9PR09MB4977.eurprd09.prod.outlook.com>
User-Agent: Gnus/5.13 (Gnus v5.13)
Content-Type: text/plain
X-TMN: [OOwsCiSF4sr3DQaZHpyF0RGQ3xIzLaa+]
X-ClientProxiedBy: BE1P281CA0127.DEUP281.PROD.OUTLOOK.COM
 (2603:10a6:b10:7a::7) To AM9PR09MB4977.eurprd09.prod.outlook.com
 (2603:10a6:20b:304::20)
X-Microsoft-Original-Message-ID: <87o7pzbb7m.fsf@live.com>
MIME-Version: 1.0
X-MS-Exchange-MessageSentRepresentingType: 1
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: AM9PR09MB4977:EE_|AM7PR09MB4213:EE_
X-MS-Office365-Filtering-Correlation-Id: f963859a-5258-4a79-414e-08db0cc9c5ba
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: 3v3lT/YmPYeQmMFm5jNB9wOk3PiGz7OZzzQ95E0OafCs/AdkESIIhyFhtcYN0KCIca9Bu/7qGDY8WUnt0JNCeWFiU1LxEdXUdsNpMzgRuW13Jl0cPvb+xGkUHMBVlealaWBzKyJQIysxnOuFKvyQEvicNBn4GRX96F+UL+vH6Vspl26miWAD4LhDoMDIXGB8Ly1HmSWVX03CdbSrsmX33njyp5KpUCYsUG839UVuRC7VIJXarcZKFDYxQvSdtCwKVCVQKDJGwVdLNEqKQaKZoMi5WNq3n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-msonline-outlook-64da6.templateTenant
X-MS-Exchange-CrossTenant-Network-Message-Id: f963859a-5258-4a79-414e-08db0cc9c5ba
X-MS-Exchange-CrossTenant-AuthSource: AM9PR09MB4977.eurprd09.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Feb 2023 07:21:35.3177 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000
X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR09MB4213
Received-SPF: pass client-ip=40.92.90.65; envelope-from=arthur.miller@live.com;
 helo=EUR05-VI1-obe.outbound.protection.outlook.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, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: emacs-orgmode@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "General discussions about Org-mode." <emacs-orgmode.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/emacs-orgmode>
List-Post: <mailto:emacs-orgmode@gnu.org>
List-Help: <mailto:emacs-orgmode-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=subscribe>
Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org
Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org
X-TUID: 5RB6D2Wb4qPe

Bruno Barbier <brubar.cs@gmail.com> writes:

> Arthur Miller <arthur.miller@live.com> writes:
>
>> Bruno Barbier <brubar.cs@gmail.com> writes:
>>
>>> Arthur Miller <arthur.miller@live.com> writes:
>>>
>>> The hook `org-capture-mode-hook' will be run in your special
>>> capture buffer. You can override the "C-c C-c" binding only there.
>>
>> Yes and in every other capture buffer
>
> No. If you modify the hook only during your call to 'org-capture', it
> will be called only once in your buffer.
>
>>> Even if I could let bind the function at the right time, I would avoid
>>> that solution, as I can't garantuee that this global hack will not break
>>> other parts of Emacs (other captures, output filters, threads, timers,
>>> etc.).
>>
>> Why do you think it will break other parts? This is not a global hack, on contrary it
>> exactly tries to prevent to be "global" in entire Emacs, by let-binding a name
>> to a local lambda, which becomes "global" only in that buffer. If that
>> explains.
>
> You are assigning a local value to the global binding. Everything in
> Emacs will use your functions until you exit the cl-letf. It's like if
> you were using 'fset'.

Yes, you are totally correct; unfortunately binding with cl-flet or cl-labels,
does not work, so binding the global is the only one that works.

> Here is an example that uses cl-letf. Note that the call to
> async-shell-command is outside the "local" binding, yet, the cl-letf
> breaks it. You should try this in an other Emacs, just in case.
>
> (defun oops ()
>   (let ((v (async-shell-command "date" "!sh async")))
>     (cl-letf
>         (((symbol-function 'comint-output-filter)
>           (lambda (proc string)
>             (message "async-shell-command is using my binding: %s" string)
>             (read-string "What's the password?"))))
>       (read-string "what: ")
>       )))
> (oops)

Yes, I definitely agree with you about the concerns, which are certainly
valid, but you would get same effect with advice, it is not different at
all. The difference is that let-binding is automatically removed and installed,
and only active during the cl-letf, while advice is manually installed and
active until manually removed. It is not about the tool, but what you do with
the tool.

>> Here is another version on the same theme, where I don't think you could modify the local
>> environment without let-binding at all:
>>
>> #+begin_src emacs-lisp
>> (defun my-read-string (prompt)
>>   (let ((delta 20 )
>>         (minibuffer-mode-map org-mode-map))
>>     (window-resize (minibuffer-window) delta)
>>     (cl-letf (((symbol-function 'org-ctrl-c-ctrl-c)
>>                (lambda ()
>>                  (interactive)
>>                  (let ((s (buffer-string)))
>>                    (exit-minibuffer) s)))
>>               ((symbol-function 'minibuffer-mode) #'org-mode)
>>               ((symbol-function 'minibuffer-complete-and-exit) #'org-return)
>>               ((symbol-function 'org-kill-note-or-show-branches) #'keyboard-escape-quit))
>>       (read-string (concat "# Press C-c C-c to continue, C-c C-k to cancel\n# " prompt "\n\n")))))
>> #+end_src
>
> I hope I've convinced you to not do that. I definitely will not try it,
> as Emacs needs a working minibuffer for plenty of things: debugging,
> saving, quitting, etc.

Your minibuffer will continue to work during and after that function. If you
don't use recursive minibuffer, that will only affect the internal buffer
created by read-string.

If recursive minibuffer is enabled, yes they will also be affected. Of course,
there is no reason for you to try that, that was just an example, but I feel a
bit of passive aggressivity here, for no good reason tbh.

>> read-string is written in C and creates its own minibuffer, which is deleted by
>> the time read-string exits. I don't know of any other way to cutomize exactly
>> *that* minibuffer, without installing a hook or advising some functions, which I
>> think is way less clean and much more "global" than just running the function in
>> a local environment. As I understand, let binding for this purpose is a normal
>> technique in lisps, but I am not an expert as said; I am actually experimenting
>> with this for the purpose of learning and seeing what is possible.
>
> Yes, let binding is fundamental. But I think it's the first time I see
> 'cl-letf' with the 'symbol-function' place.

https://nullprogram.com/blog/2017/10/27/
https://endlessparentheses.com/understanding-letf-and-how-it-replaces-flet.html
https://stackoverflow.com/questions/39550578/in-emacs-what-is-the-difference-between-cl-flet-and-cl-letf

>> but I am not sure if I can do anything here without introducing at-least an
>> extra keymap, to not install into the org-capture-mode-map, so I can as well
>> create a minor mode, but at this point it is not much different than
>> re-invinting the read-string, so I'll terminate my experiment here :).
>
> You can replace the buffer keymap with a keymap that only contain your custom
> keys, and inherits everything else from org-capture-mode-map.

Isn't that what I wrote: introducing an extra keymap?

Of course I can solve the problem differently, but that was not what question
was about :).

>> But I wouldn't speak in some generic terms like "use hooks" or "advise" instead
>> of let-binding.
>
> I didn't mean to say to not use let bindings. I'm trying to say that
> using 'fset' (i.e. cl-letf + the symbol-function place) looks like a
> really bad idea to me.

Well, I definitely understand you, and agree that overwriting function for
everyone and everything is not the best idea, but unfortunately bindings work as
they do in Emacs. I would prefer to have a local binding, with cl-flet, but this
does not work in Emacs:

(defun my-read-string (prompt)
  (let ((delta 20 )
        (minibuffer-mode-map org-mode-map))
    (window-resize (minibuffer-window) delta)
    (cl-flet ((org-ctrl-c-ctrl-c ()
                (interactive)
                (let ((s (buffer-string)))
                  (exit-minibuffer) s))
              (minibuffer-mode () #'org-mode)
              (minibuffer-complete-and-exit () #'org-return)
              (org-kill-note-or-show-branches () #'keyboard-escape-quit))
      (read-string (concat "# Press C-c C-c to continue, C-c C-k to cancel\n# "
  prompt "\n\n")))))

>                        And, in this case, hooks and adivces are what is
> usually used.

Hooks serve a different purpose. Advice can serve same purpose with exactly
same side effect, and some other limitations. With some care, let-binding is
still more "local" then advice. With other words, I agree with you about the
problems, but not with dogmatic approach that it should never be done, and
that hooks and advices are the replacement.

>> (defun org-project-new-project ()
>>   (interactive)
>>   (let ((org-capture-templates org-project-templates))
>>     (org-capture)))
>>
>> ...
>
>> I don't know what would be the alternative, but let-binding on
>> org-capture-templates, let me clearly re-use the functionality without polluting
>> the global org-capture-templates variable and without re-implementing pretty
>> much anything.
>
> That looks to me like a perfect use of a let binding.  You bind a dynamic
> *variable* using *let*, and, that variable is rebound, and used, during, and
> only during 'org-capture'.
>
>
>
>> I am very interested to hear more on the topic, since I would definitely like to
>> learn more about different techniques.
>
> Variables are designed to be overriden (let bounds). Functions are not

I have never heard before that functions are not designed to be overriden. I
think of them as two slots in a symbol structure; let creates bindings for value
slot, and flet for function slot. Functions are just objects or data as any
other value in lisp.

> (as there is only one binding at any given time).

Yes, unfortunately, in Emacs it is so; but I don't think it should be :).

There is an interesting package by Nick Ferrier

https://github.com/nicferrier/emacs-noflet

but it does not seem to work, at least not for me.

regards
/a