What is a use case for cl-defun, where defun wouldn't work or would be hard to work with? I am using it to count the number of opening and closing bracemarks.

    (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  (and brcmk-end (char-to-string brcmk-end))) )
          (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 encourages opinion-based answers. Maybe ask a more specific question, saying what it is about cl-defun that you don't understand etc.
    – Drew
    Apr 13, 2022 at 19:55

1 Answer 1


Use cl-defun if you want (as you do) default values for optional arguments or, with &key, keyword arguments. There is more which you can find starting at C-h f <RET> cl-defun.

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.