I have the following code that counts the number of opening and closing braces, but getting the error

and: Wrong type argument: stringp, 40

Here is the code:

    (defcustom rk-bracemk '("()" "[]" "{}" "<>")
      "Two-character strings of open and close delimiters."
      :type '(repeat string)
      :group 'convenience)
    (defun rk-bracemk-count (brcmarks reg-beg reg-end)
      "Count brace marks within a region.
    BRCMARKS  String defining opening and closing brace marks.
    REG-BEG   Marker defining start of region.
    REG-END   Marker defining end of region.
          M-x rk-bracemk-count"
        (completing-read "Bracemarks: " rk-bracemk nil t)
        (let* ( (begmk (aref brcmarks 0))
            (endmk (aref brcmarks 1))
                (a 0) (b 0) )
          (goto-char reg-beg)
          (while (and (< (point) reg-end)
                      (search-forward begmk reg-end t))
            (cl-incf a))
          (goto-char reg-beg)
          (while (and (< (point) reg-end)
                      (search-forward endmk reg-end t))
            (cl-incf b))
          (message "Markers: %c %c" begmk endmk)
          (message "Counts %d %d" a b)
          (goto-char reg-beg)) ))

1 Answer 1


It seems pretty clear -- you're passing the number 40 to something that requires a string. Use the debugger to see where that's happening:

M-x toggle-debug-on-error

Perhaps you think aref on a string will return a string? It returns a character (which is an integer). 40 is (. You probably want char-to-string.

  • Yes, the problem is that OP is passing the char ?\( (40) as the first arg to search-forward, and that arg needs to be a string.
    – Drew
    Mar 20, 2022 at 5:24
  • Done as suggested, solving the problem (p (aref brcmarks 0)) (q (aref brcmarks 1)) (begmk (char-to-string p)) (endmk (char-to-string q))
    – Dilna
    Mar 20, 2022 at 11:30

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.