1

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)
  &quot;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.

Usage
      M-x rk-bracemk-count&quot;

  (interactive
   (list
    (completing-read &quot;Bracemarks: &quot; rk-bracemk nil t)
    (region-beginning)
    (region-end)))

  (save-excursion

    (let* ( (begmk (aref brcmarks 0))
        (endmk (aref brcmarks 1))
            (a 0) (b 0) )

      (goto-char reg-beg)
      (while (and (&lt; (point) reg-end)
                  (search-forward begmk reg-end t))
        (cl-incf a))

      (goto-char reg-beg)
      (while (and (&lt; (point) reg-end)
                  (search-forward endmk reg-end t))
        (cl-incf b))

      (message &quot;Markers: %c %c&quot; begmk endmk)
      (message &quot;Counts %d %d&quot; a b)
      (goto-char reg-beg)) ))

Dilna
  • 1
  • 3
  • 11

1 Answers1

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.

phils
  • 50,977
  • 3
  • 79
  • 122
  • 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 '22 at 05: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 '22 at 11:30