Am unsure whether making brcmk-end optional is possible for interactive use. If one can have optional arguments, what would be the changed necessary? Would the interactive expression change?

    (defun rk-bracemk-count-2 (brcmk-beg brcmk-end rgn-beg rgn-end)
      "Count brace marks within a selected region.
    BRCMK-BEG  Opening brace mark.
    BRCMK-END  Closing brace mark.
    RGN-BEG    Region start marker or position.
    RGN-END    Region end marker or position."
       (list (read-char-from-minibuffer "Opening bracemark: ")
             (read-char-from-minibuffer "Closing bracemark: ")
             (region-end) ))
        (let ( ($a  0) ($b  0) 
               ($begmk  (char-to-string brcmk-beg))
               ($endmk  (char-to-string brcmk-end)) )
          (goto-char rgn-beg)
          (while (and (< (point) rgn-end)
                      (search-forward $begmk rgn-end t))
            (cl-incf $a))
          (goto-char rgn-beg)
          (while (and (< (point) rgn-end)
                      (search-forward $endmk rgn-end t))
            (cl-incf $b))
          (message "Marks: %s %s  Counts: %d %d" $begmk $endmk $a $b)
          (goto-char rgn-beg) )))

  • Can I ask why you aren't using (interactive "cOpening bracemark: \ncClosing bracemark: \nr")? Commented Mar 29, 2022 at 21:40

1 Answer 1


You can make any of the args after the first N (N=0...) args optional. The first N are required (but if N is zero then none are required).

The interactive spec need only provide the required args. It can of course provide some or all of the other args, as well.

If a caller of the function, or if the interactive spec, does not provide some optional arg then its value is nil.

If the body of the function needs a non-nil value for such an omitted optional arg then the body has to provide that itself (e.g. change the value from nil to something else, if needed).

In your command, if you make brcmk-end optional then your interactive spec can remain as it is, because it provides a reasonable value for that argument. If youre interactive spec did not read or otherwise provide that arg then when the body tried to evaluate (char-to-string brcmk-end) an error would be raised.

  • Does it make sense to have the beginning and end region (rgn-beg, rgn-end) be optional ?
    – Dilna
    Commented Mar 28, 2022 at 22:21
  • How can one avoid the error trying to evaluate (char-to-string brcmk-end) ?
    – Dilna
    Commented Mar 28, 2022 at 22:26
  • 1. Whatever you want to do "makes sense". 2. In interactive you already avoid the error. In Lisp code the caller needs to provide brcmk-end as a character.
    – Drew
    Commented Mar 29, 2022 at 1:57
  • In lisp code, I also want for brcmk-end to be optional.
    – Dilna
    Commented Mar 29, 2022 at 5:19
  • If I put brcmk-end as optional, would rgn-beg and rgn-end have to be optional as well when brcmk-end is set to optional? Would like to keep brcmk-end to follow brcmk-beg; and rgn-end to follow rgn-beg.
    – Dilna
    Commented Mar 29, 2022 at 14:01

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.