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."
(interactive
(list (read-char-from-minibuffer "Opening Bracemark: ")
(read-char-from-minibuffer "Closing Bracemark: ")
(region-beginning)
(region-end) ))
(save-excursion
(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) )))
read-char-from-minibuffer
cannot return "empty" or return more than one char ("composed of spaces"). Please clarify what you're asking.read-char-from-minibuffer
) that returnnil
when you don't get the input you want. I guess that would make the function use the default argument. (Personally, I haven't usedcl-defun
so I'm not familiar with default arguments.)