The following counts the number of opening (brcmk-beg) and closing (brcmk-end) braces. Having brcmk-end an optional argument, I would like to print

$begmk nil $count-begmk 0

when the user either hits Return or uses a blank space when using the minibeffer with the call (read-char-from-minibuffer "Closing Bracemark: ").

    (cl-defun rk-bracemark-count-from-char
        (brcmk-beg &optional brcmk-end
                             (rgn-beg (region-beginning))
                             (rgn-end (region-end)))
      "Count number of opening and closing brace marks.
    Search is applied within a region."
       (list (read-char-from-minibuffer "Opening Bracemark: ")
             (read-char-from-minibuffer "Closing Bracemark: ")
             (region-end) ))
        (let ( ($count-begmk  0) ($count-endmk  0) 
               ($begmk  (char-to-string brcmk-beg))
               ($endmk  (or (and brcmk-end (char-to-string brcmk-end))
                "nil")) )
          (goto-char rgn-beg)
          (while (and (< (point) rgn-end)
                      (search-forward $begmk rgn-end t))
            (cl-incf $count-begmk))
          (goto-char rgn-beg)
          (while (and brcmk-end
                  (< (point) rgn-end)
                  (search-forward $endmk rgn-end t))
            (cl-incf $count-endmk))
          (message "Marks: %s %s  Counts: %d %d" $begmk $endmk
               $count-begmk $count-endmk)
          (goto-char rgn-beg) )))

  • The question isn't clear. read-char-from-minibuffer cannot return "empty" or return more than one char ("composed of spaces"). Please clarify what you're asking.
    – Drew
    Apr 13, 2022 at 19:54
  • 1
    Why don't you write you own read function (based on read-char-from-minibuffer) that return nil when you don't get the input you want. I guess that would make the function use the default argument. (Personally, I haven't used cl-defun so I'm not familiar with default arguments.) Apr 13, 2022 at 20:36

1 Answer 1

(require 's) ; for s-blank-str-p

    (list (read-char-from-minibuffer "Opening Bracemark: ")
          (let ((x (read-char-from-minibuffer "Closing Bracemark: ")))
            (unless (s-blank-str-p (char-to-string x)) x))
  ;; at this point brcmk-end is either nil or a closing delimiter
  • So when user hits Return, the local variable x is nil. Otherwise x gets converted to a string. Right?
    – Dilna
    Apr 13, 2022 at 21:27
  • Would I need to install the feature s?
    – Dilna
    Apr 13, 2022 at 21:29
  • I figure that the line (unless (s-blank-str-p (char-to-string x)) x) should be (unless (s-blank-str-p (char-to-string x)) x))
    – Dilna
    Apr 13, 2022 at 23:49
  • 1
    Yes, package s is in the MELPA repository (and probably others). It's a comprehensive library of string-oriented functions. Apr 14, 2022 at 11:06
  • You're quite right about the parens, fixed now. Apr 14, 2022 at 11:09

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.