emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Bruno Barbier <brubar.cs@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>,
	Jack Kamm <jackkamm@gmail.com>, Matt <matt@excalamus.com>
Subject: Re: Pending contents in org documents
Date: Mon, 03 Jun 2024 11:04:12 +0000	[thread overview]
Message-ID: <87plsyz3rn.fsf@localhost> (raw)
In-Reply-To: <665abf9f.050a0220.d2a6e.ef6b@mx.google.com>

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

>> Fair. Although, it feels like a common use case to replace the region
>> with :success value. Maybe the library should provide some ready-to-use
>> functions that can be used as the value of :on-outcome.
>
> I've recycled the old function used by `org-pending-user-edit',
> improved it and made it the default :on-outcome handler: see
> `org-pending-on-outcome-replace'.  I've simplified the example
> accordingly, removing the custom :on-outcome.

Thanks!
I have one suggestion though. You now do

    Use the function ON-OUTCOME to update the region with the outcome; if it
    is nil, set it to the function `org-pending-on-outcome-replace'.

However, `org-pending' is defined via `cl-defun', so you can instead
just put the default value for :on-outcome key and mention that it is
the default in the docstring. Then, you do not need to do any additional
checks in the function body; `cl-defun' will take care about assigning
the default value.

> From the many examples provided in the branch, do you see any that
> should be included in the library as an other precooked-wrapper, that
> should be included in the section "Basic use of locks" ?

Not sure (I did not look deep into the implementation yet to keep my
perspective closer to end user who first encountered the library)

As a general rule, we should (1) provide simple examples that are easy
to understand and copy/paste; (2) not-so-simple, but useful examples
that are ready to use in practice. These examples should not be complex
either, with all the complexity hidden behind library API (API should be
modified if complexity is unavoidable).

I tried to run your example and have several observations:

1. On failure, it is not obvious that failure happened:
   - The failure overlay disappear very quickly, and is not visible at
      all if I happen to look elsewhere in the buffer. Maybe we can
      simply keep it and remove the overlay on click
   - After failure, the "!" fringe indicator is visible, but it is not
     obvious at all that user can click to get details
     I first tried to click on the fringe itself to no avail. Then, I
     randomly clicked on the text and got the description buffer; but
     that was unexpected - the text I clicked did not have any
     indication of its "clickability" - neither some kind of underline
     face, nor an overlay or a mouse hint.

2. I tried to do M-x undo while the reglock was active, and got an
   error. I'd expect that undo would work, skipping the region.

3. I tried M-x undo _after_ reglock was unlocked, and I got "TO REPLACE"
   word highlighted. I did not expect it to be highlighted.

4. If I try to cancel the reglock, it does get canceled, but *Region
   Lock* buffer is not updated - Live? still displays "yes Cancel".


>> What is the difference between "canceling" and "killing" the reglock?
>> Do they need to be separate?
>
> If you cut out, from the example, the part where they differ, they do
> look the same indeed :)
>
> I'm apparently failing to explain and document this correctly, as it
> looks like a recurring topic, sorry.
>
> Yes, they need to be separate as they are two different operations.
>
>  - cancel: The *user* may request a *cancel*; it's a polite way to
>    tell org-pending that the user doesn't care anymore about the
>    outcome.  A valid implementation is to ignore the user request.
>    The default implementation is to unlock the region (sending a
>    cancel :failure using 'org-pending-send-update'): it unlocks the
>    region, ignoring why it was locked..
>
>  - kill: *Emacs* may have to *kill* some locks, because Emacs is
>    killed, or the lock buffer is killed.  org-pending will intercept
>    the operations of this kind, ask the user to confirm the
>    destruction, and, if confirmed, it will give a chance to the lock
>    to do some cleanup by calling the 'before-kill-function'.

Does it mean that clicking "cancel" does not guarantee that the region
will not be updated by the running process/timer?

In my eyes, there is no difference between user request and "kill". If
users asks things to stop, no modifications should be allowed to
the region.

> I modified the example to rely on the reglock when possible (as
> opposed to values kept from the creation time).

I tried to simplify your example as the following:

(cl-defstruct my/counter (state 0) timer)
(defun my/counter-update (counter reglock &optional force-landing)
  "Increase COUNTER and send update to REGLOCK.
At the end of sequence, cancel COUNTER timer.
When FORCE-LANDING is symbol `land', report :success \"Landed early\" and cancel
the timer."
  (org-pending-send-update
   reglock
   (pcase (my/counter-state counter)
     ((guard (eq force-landing 'land))
      (when (timerp (my/counter-timer counter))
	(cancel-timer (my/counter-timer counter)))
      '(:success "Landed early"))
     ((guard (eq force-landing 'crashed))
      (when (timerp (my/counter-timer counter))
	(cancel-timer (my/counter-timer counter)))
      '(:failure "Crashed"))
     (0 '(:progress "Taking off..."))
     (1 '(:progress "Flying..."))
     (2 '(:progress "Landing..."))
     (_
      (when (timerp (my/counter-timer counter))
	(cancel-timer (my/counter-timer counter)))
      (if (= 0 (random 2))
	  '(:success "Landed successfully")
        '(:failure "Landing malfunction")))))
  (cl-incf (my/counter-state counter)))

(let ((lock-buffer (generate-new-buffer "*Pending region example*"))
      reglock state)
  (with-current-buffer lock-buffer
    (insert "
Buffer displaying pending content.
OUTPUT>>>

TO REPLACE

<<<OUTPUT
More text.
")

    (goto-char (point-min))
    (re-search-forward "TO REPLACE")

    ;; We lock the 'region', defining how to update it when the
    ;; outcome is available.
    (setq reglock (org-pending (cons (match-beginning 0) (match-end 0))))

    (pop-to-buffer lock-buffer)

    (setq state (make-my/counter))
    ;; We create a timer to update our state every few seconds.
    (setf (my/counter-timer state)
	  (run-with-timer 2 2 #'my/counter-update state reglock)))

  (setf (org-pending-reglock-user-cancel-function reglock)
        `(lambda (rlock)
           (warn "Initiating emergency landing...")
           (sleep-for 1)
           (my/counter-update ,state rlock 'land)
           (warn "Initiating emergency landing... done")))

  (setf (org-pending-reglock-before-kill-function reglock)
        `(lambda (_rlock)
           (cancel-timer (my/counter-timer ,state))
           (warn "Transponder signal lost")))

  (setf (org-pending-reglock-insert-details-function reglock)
        `(lambda (rlock _start _end)
           (insert (format "State: %s\n"
                           (my/counter-state ,state)))))

  )

In the above, it is not fully clear for me what BEG and END arguments in
`org-pending-reglock-insert-details-function' mean and where the
insertion happens.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


  reply	other threads:[~2024-06-03 11:08 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-01 11:58 [BUG] Unexpected result when evaluating python src block asynchronously [9.7-pre (release_9.6.17-1131-gc9ed03.dirty @ /home/yantar92/.emacs.d/straight/build/org/)] Ihor Radchenko
2024-02-01 14:56 ` Bruno Barbier
2024-02-03  1:30   ` Jack Kamm
2024-02-04 15:07     ` Ihor Radchenko
2024-02-05  1:37       ` Jack Kamm
2024-02-05 14:29         ` Ihor Radchenko
2024-02-06 19:24           ` Bruno Barbier
2024-02-07 16:19             ` Ihor Radchenko
2024-02-07 17:40               ` Bruno Barbier
2024-02-08  3:21             ` Jack Kamm
2024-02-15 20:02             ` Asynchronous blocks for everything (was Re: [BUG] Unexpected result when evaluating python src block asynchronously [9.7-pre (release_9.6.17-1131-gc9ed03.dirty @ /home/yantar92/.emacs.d/straight/build/org/)]) Matt
2024-02-16 17:52               ` Bruno Barbier
2024-02-18 21:14                 ` Matt
2024-02-19  0:31                   ` Jack Kamm
2024-02-20 10:28                   ` Ihor Radchenko
2024-02-20 10:46                     ` tomas
2024-02-20 11:00                       ` Ihor Radchenko
2024-02-20 11:03                         ` tomas
2024-02-21 15:27                   ` Bruno Barbier
     [not found]                   ` <notmuch-sha1-61e086e33bd1faf1a123c1b0353cf2102c71bdac>
2024-02-28 10:18                     ` Pending contents in org documents (Re: Asynchronous blocks for everything (was Re: ...)) Bruno Barbier
2024-03-02 10:03                       ` Ihor Radchenko
2024-03-02 10:57                         ` Bruno Barbier
2024-03-02 11:13                           ` Ihor Radchenko
2024-03-02 18:06                             ` Bruno Barbier
     [not found]                             ` <notmuch-sha1-d2799a191385bf51811d7788856a83b4f5a1fe3b>
2024-03-07 17:08                               ` Bruno Barbier
2024-03-07 18:29                                 ` Ihor Radchenko
2024-03-08 14:19                                   ` Bruno Barbier
2024-03-13  9:48                                     ` Ihor Radchenko
2024-03-19  9:33                                       ` Bruno Barbier
2024-03-20 10:23                                         ` Ihor Radchenko
2024-03-21 10:06                                           ` Bruno Barbier
2024-03-21 12:15                                             ` Ihor Radchenko
2024-03-25 17:46                                               ` Bruno Barbier
2024-03-27 11:29                                                 ` Ihor Radchenko
2024-03-30 22:53                                                   ` Rudolf Adamkovič
2024-04-04 16:35                                                     ` Bruno Barbier
2024-04-04 16:33                                                   ` Bruno Barbier
2024-04-11 11:44                                                     ` Ihor Radchenko
2024-04-19 11:23                                                       ` Bruno Barbier
2024-04-20 10:07                                                         ` Ihor Radchenko
2024-05-12 16:43                                                           ` Bruno Barbier
2024-05-19  9:39                                                             ` Ihor Radchenko
2024-05-23 16:31                                                           ` Bruno Barbier
2024-05-24  9:49                                                             ` Ihor Radchenko
2024-05-30 19:01                                                               ` Bruno Barbier
2024-05-31  9:48                                                                 ` Ihor Radchenko
2024-06-01  6:28                                                                   ` Pending contents in org documents Bruno Barbier
2024-06-03 11:04                                                                     ` Ihor Radchenko [this message]
2024-06-15  7:49                                                                       ` Bruno Barbier
2024-06-16  9:31                                                                         ` Ihor Radchenko
2024-02-19  0:15                 ` Asynchronous blocks for everything (was Re: [BUG] Unexpected result when evaluating python src block asynchronously [9.7-pre (release_9.6.17-1131-gc9ed03.dirty @ /home/yantar92/.emacs.d/straight/build/org/)]) Jack Kamm
2024-02-21 15:43                   ` Bruno Barbier
2024-02-19  9:06                 ` Ihor Radchenko
2024-02-19 19:47                   ` Matt
2024-02-19 20:10                     ` Ihor Radchenko
2024-02-20  8:32                     ` Ihor Radchenko
2024-02-20 17:04                     ` Jack Kamm
2024-02-21 16:03                   ` Bruno Barbier
2024-02-23 12:11                     ` Ihor Radchenko
2024-02-23 13:24                       ` Bruno Barbier
2024-02-24 11:59                         ` Ihor Radchenko
2024-02-24 16:42                           ` Bruno Barbier
2024-02-24 19:54                             ` Matt
2024-02-28 10:21                               ` Bruno Barbier
2024-02-08  3:26           ` [BUG] Unexpected result when evaluating python src block asynchronously [9.7-pre (release_9.6.17-1131-gc9ed03.dirty @ /home/yantar92/.emacs.d/straight/build/org/)] Jack Kamm

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=87plsyz3rn.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=brubar.cs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=jackkamm@gmail.com \
    --cc=matt@excalamus.com \
    /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).