emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
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
Date: Sun, 12 Feb 2023 17:12:46 +0100	[thread overview]
Message-ID: <AM9PR09MB49773EB425B045F9D152EC5996DC9@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <63e8afc4.5d0a0220.969b4.d166@mx.google.com> (Bruno Barbier's message of "Sun, 12 Feb 2023 10:22:10 +0100")

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

> Hi Arthur,
>
> Arthur Miller <arthur.miller@live.com> writes:
>
>> Bruno Barbier <brubar.cs@gmail.com> writes:
>>
>> ...  but I feel a
>> bit of passive aggressivity here, for no good reason tbh.
>
> I'm just trying to help, giving some valid or invalid advices.  I'm
> sorry that what I wrote, and how I wrote it, made you feel that way.

It is ok, I just don't want us to go into social media discussion style where
there is more important to assert own ego then to come into some insight.

>>>
>>> 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
>>
>
> Thanks for these links. I like cl-flet and cl-labels :-)

They are good for introducing *new* bindings, not so for overriding locally.

>>>> 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 :).
>
> Right. Even when inheriting from the old keymap, it's still building a
> new keymap.  Sorry :-)
>
>
>> 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")))))
>
> Yes. cl-flet looks safe to me :-)
>
>>
>> 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.
>
> Sorry if my words sounding dogmatic.
> Else, I agree too :-)
>
>
>>>
>>>> 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;
>
> ok. We do really agree then :-)
>
>
>> but I don't think it should be > :).
>
> ... oh no ! ;-)
>
>
>>
>> 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.
>
> It's almost like a temporary advice ;-)
>
>
> About your use case, if what you need is asynchronous editing, maybe the
> with-editor package will be of interest to you:
>     https://github.com/magit/with-editor/blob/main/lisp/with-editor.el
>     
> It allows sub-processes to call Emacs for editing tasks. It's used by
> magit. It's easy enough to reuse. I've attached my attempt at it if
> you're interested.
>
> best,
>
> Bruno
>
> (cl-defun my-edit-async (finish &key mode buffer-name setup cancel)
>   "Open a buffer, let the user edit its content.
> Return the editing buffer.  Call FINISH in the editing buffer if
> the user validates his edit (C-c C-c).  When CANCEL is non-nil,
> call CANCEL in the editing buffer if the user cancels his
> edit (C-c C-k). When done, delete the buffer and its content.
>
> When MODE is non-nil, use it as the major-mode.  When BUFFER-NAME
> is non-nil, use it to generate a new unique name of the editing buffer.
> When SETUP is non-nil, call it in the edit buffer to setup the
> buffer before starting the edit."
>   (unless buffer-name (setq buffer-name "@Async edit"))
>   (let ((buf (generate-new-buffer buffer-name)))
>     (with-current-buffer buf
>       (when mode (funcall mode))
>       (when setup
>         (funcall setup))
>       (with-editor-mode 1)
>       (setq with-editor-previous-winconf
>             (current-window-configuration))
>       (add-hook 'with-editor-pre-finish-hook
>                 (lambda () 
>                   (funcall finish)
>                   (set-buffer-modified-p nil))
>                 nil :local)
>       (add-hook 'with-editor-pre-cancel-hook
>                 (lambda ()
>                   (when cancel (funcall cancel))
>                   (set-buffer-modified-p nil))
>                 nil :local)
>       (switch-to-buffer buf))
>     buf))
>
> (my-edit-async 
>  (lambda () (message "My edit:\n%S" (buffer-string)))
>  :cancel (lambda () (message "Canceled (discarded: %s)" (buffer-string)))
>  :setup (lambda () (insert "initial content") (goto-char (point-min))))
>

That looks very nice indeed. I am not aware of that package, I will definitely
use it somewhere, sometime. But with that we are getting now into 1K extra sloc
solution. With this experiment, I was mostly interesting to see how I can re-use
what already is in Emacs. Lisps are great for prototyping and writing new
software. Legend says that Steele was cranking out 10 intepreters a week at his
time :). I don't know how true it is, just read it in some blog, but the
point is that we are rather typing fast new pieces instead of learning how
to re-use stuff, which in the long run I believe is a bad habit, since we are
debugging bits that does the same what someone else already debugged. Don't get
me wrong, I have nothing against using someones package, certainly not
with-editor, seems like a good package; I am just talking about programming in
general.

This was also me trying to get better in Emacs Lisp, but I have to admit I am a
bit dissapointed we can't just override function bindings with cl-flet. Function
slot is just another slot :).

I am using cl-labels and cl-flet myself to introduce "local" functions, to not
pollute global namespace (symbol table), which they are good for, and to keep
code local where it is used, and I think they are also slightly more clear then
lambdas. They are good for that, unfortunately it is a bit of dissapoitment that
we can't locally bind functions slots other then actually overriding them globally.

And for the last time: I am not using this version of read-string, I don't need
it myself; it was just me thinking how to implement something after reading a
blog post. Anyway, thanks for the input, it was valuable to me.

regards
/a


  reply	other threads:[~2023-02-12 16:13 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10 15:05 Problem with let/cl-letf binding stuff with org-capture Arthur Miller
2023-02-10 15:38 ` Ruijie Yu via General discussions about Org-mode.
2023-02-10 16:29   ` Arthur Miller
2023-02-10 19:00 ` Bruno Barbier
2023-02-11  6:33   ` Arthur Miller
2023-02-11  7:58     ` Bruno Barbier
2023-02-11 16:14       ` Arthur Miller
2023-02-11 19:23         ` Bruno Barbier
2023-02-12  7:21           ` Arthur Miller
2023-02-12  9:22             ` Bruno Barbier
2023-02-12 16:12               ` Arthur Miller [this message]
2023-02-12 16:22                 ` Ihor Radchenko
2023-02-13 18:40                   ` Bruno Barbier
2023-02-15 11:45                     ` Arthur Miller
2023-02-15 13:18                       ` Bruno Barbier
2023-02-15 17:36                         ` arthur miller
2023-02-13 18:37                 ` Bruno Barbier
2023-02-11 16:49 ` Ihor Radchenko
2023-02-15 13:06   ` Arthur Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM9PR09MB49773EB425B045F9D152EC5996DC9@AM9PR09MB4977.eurprd09.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=brubar.cs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).