0

I'm searching for a way to display messages while replacements are proposed to the users of a script.

I tried to write a macro but I'm in trouble with this code:

(defmacro with-user-explanation (MESSAGE &rest body)
  "DOCSTRING."
  (declare (indent 1))
  `(let* ((temp-buffer-resize-mode temp-buffer-resize-mode)
      (frame-config (current-frame-configuration))
      (fit-frame-to-buffer t))
     
     (temp-buffer-resize-mode 1)
     (resize-temp-buffer-window)
        (unwind-protect
       (progn
         (with-output-to-temp-buffer "REPLACEMENT INFO"
           (princ ,MESSAGE))

         ,@body)
     ;; *CLEANUP*
      (progn
        (kill-buffer "REPLACEMENT INFO")
        (set-frame-configuration frame-config)))))

(defun my-ue-test () 
  "DOCSTRING"
  (interactive)
  (save-excursion
    (let* ((a (point-min-marker))
       (z (point-max-marker)))
      (with-user-explanation
      "This message provide info about this specific string replacement in the document."
    (query-replace "foo" "bar" nil a z)))))

I have 2 issues:

  1. When I run the function with 2 windows opened: enter image description here the temp buffer is not resized: enter image description here I need it to be resized: enter image description here It is correctly resize when I run the function with only one window diplayed.

  2. At the function exit, the layout (the color) of the pointer has changed (from white to gray). I moved this issue here: Cursor's color differs from what specified in the frame parameters

Edit. Maybe using a temp-buffer to display MESSAGE is not a good idea. Everything is messed up, also with the Arch Stanton's solution, if I use e.g. occur:

(defun my-ue-test ()
  "DOCSTRING"
  (interactive)
  (save-excursion
    (let* ((a (point-min-marker))
           (z (point-max-marker)))
      (with-user-explanation
          "This message provide info about this specific string replacement in the document."

        (occur "foo")
        (query-replace "foo" "bar" nil a z)))))

enter image description here

EDIT 2. This last issue is solved with set-window-dedicated-p:

(defmacro with-user-explanation (MESSAGE &rest body)
  "DOCSTRING."
  (declare (indent 1))
  `(let* ((temp-buffer-resize-mode temp-buffer-resize-mode)
          (frame-config (current-frame-configuration))
          (fit-frame-to-buffer t)
    
          (temp-buffer-show-function
           (lambda (bufname)
             (display-buffer bufname '(display-buffer-at-bottom)))))

     (unwind-protect
         (progn
           (with-output-to-temp-buffer "REPLACEMENT INFO"
             (princ ,MESSAGE))
       (fit-window-to-buffer (get-buffer-window "REPLACEMENT INFO"))
       (set-window-dedicated-p (get-buffer-window "REPLACEMENT INFO") t)
           ,@body)
       ;; *CLEANUP*

       (progn
         (kill-buffer "REPLACEMENT INFO")
         (set-frame-configuration frame-config)))))
5
  • You can show a message separately in another buffer. Or you can show it sequentially, before or after some other message (or prompt), using sit-for or sleep-for to pause for it to be read.
    – Drew
    Commented Feb 6, 2021 at 19:07
  • I think I'll go for another buffer. I'ìì just need to resize this new buffer's window, lets say, 1/4 of the main buffer's window.
    – Gabriele
    Commented Feb 6, 2021 at 21:51
  • @Drew I updated my question. Do you think I should have opened another thread?
    – Gabriele
    Commented Feb 7, 2021 at 17:21
  • Please consider rewriting and retitling the question, so it expresses better what you really want to ask. If it's easier to just start over with a new question and delete this one, you can of course do that instead. It's about making what you want clear, so you get the best help possible (and possibly help others too). It's up to you how best to do that. Thx.
    – Drew
    Commented Feb 7, 2021 at 17:40
  • @Drew Done, Thanks.
    – Gabriele
    Commented Feb 7, 2021 at 18:19

1 Answer 1

1

I never encountered any issue with the cursor's colour, so it may still be off for you, but the other issue seems fixed in this version,

(defmacro with-user-explanation (MESSAGE &rest body)
  "DOCSTRING."
  (declare (indent 1))
  `(let* ((temp-buffer-resize-mode temp-buffer-resize-mode)
          (frame-config (current-frame-configuration))
          (fit-frame-to-buffer t)
          (temp-buffer-show-function
           (lambda (bufname)
             (display-buffer bufname '(display-buffer-at-bottom)))))
     (unwind-protect
         (progn
           (with-output-to-temp-buffer "REPLACEMENT INFO"
             (princ ,MESSAGE))
           (fit-window-to-buffer (get-buffer-window "REPLACEMENT INFO"))
           (set-window-dedicated-p (get-buffer-window "REPLACEMENT INFO") t)
           ,@body)
       ;; *CLEANUP*
       (progn
         (kill-buffer "REPLACEMENT INFO")
         (set-frame-configuration frame-config)))))

(defun my-ue-test ()
  "DOCSTRING"
  (interactive)
  (save-excursion
    (let* ((a (point-min-marker))
           (z (point-max-marker)))
      (with-user-explanation
          "This message provide info about this specific string replacement in the document."
        (query-replace "foo" "bar" nil a z)))))

I've set the temp-buffer-show-function (that's where you can choose where to place the REPLACEMENT INFO buffer's window. I chose at the bottom, you can see the other options in the documentation of display-buffer) and replaced (temp-buffer-resize-mode 1) and (resize-temp-buffer-window) with (fit-window-to-buffer (get-buffer-window "REPLACEMENT INFO")).

enter image description here

7
  • Thank you. Your solution helps, but doesn't fix the cursor issue. Moreover I spotted another issue if BODY opens another temp-buffer's window (e.g. occur).
    – Gabriele
    Commented Feb 7, 2021 at 21:15
  • The occur issue is fixed by adding (set-window-dedicated-p (get-buffer-window "REPLACEMENT INFO") t) after (fit-window-to-buffer (get-buffer-window "REPLACEMENT INFO")).
    – Gabriele
    Commented Feb 7, 2021 at 21:28
  • @GabrieleNicolardi When does the problem with the cursor occur? Is it when the function finishes because there are no more words to replace, when you quit it with C-g, or both? Commented Feb 7, 2021 at 21:35
  • It doesn't appear if I comment the string (set-frame-configuration frame-config).
    – Gabriele
    Commented Feb 7, 2021 at 21:36
  • I think I could try to let-bind the cursor status variabie... But I don't now how to do it. Moreover, if I set the cursor's color with set-cursor-color (interactively) "before" calling the function, the cursor's color doesn't change after the function call.
    – Gabriele
    Commented Feb 7, 2021 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.