1

I have the following code to count the number of opening and closing brackets in a selected region. How can I allow a user to select the bracketing sign (,[,{ to count, and return two counts, one for the opening mark and another for the closing mark?

    (defun veak-count (region-start region-end)
      "Count open-parens between REGION-START and REGION-END."
      (interactive "r")  ; gets region start and end
      (message "Counting bracketing marks ...")
      (save-excursion
        (let ( (a 0) (b 0) (c 0) (d 0))
    
          (goto-char region-start)
          (while (and (< (point) region-end)
                      (search-forward "(" region-end t))
            (cl-incf a))
    
          (goto-char region-start)
          (while (and (< (point) region-end)
                      (search-forward ")" region-end t))
            (cl-incf b))
    
          (message "%s %d %d" "count: ()" a b)
    
          (goto-char region-start)
          (while (and (< (point) region-end)
                      (search-forward "[" region-end t))
            (cl-incf c))
    
          (goto-char region-start)
          (while (and (< (point) region-end)
                      (search-forward "]" region-end t))
            (cl-incf d))
    
          (message "%s %d %d" "count: {}" c d) )) )

Dilna
  • 1,173
  • 3
  • 10

1 Answers1

0

Changing as little of your code as possible. (You can add the ability to use more than one pair of delimiters...)

(defun veak-count (open-char close-char region-start region-end)
  "Count open-parens between REGION-START and REGION-END."
  (interactive "cOpening delimiter character: \ncClosing delimiter character: \nr")
  (message "Counting bracketing marks ...")
  (save-excursion
    (let ((open   (char-to-string open-char))
          (close  (char-to-string close-char))
          (a 0) (b 0) (c 0) (d 0))
      (goto-char region-start)
      (while (and (< (point) region-end)
                  (search-forward open region-end t))
        (cl-incf a))
      (goto-char region-start)
      (while (and (< (point) region-end)
                  (search-forward close region-end t))
        (cl-incf b))
      (message "count %s: %d, count %s: %d" open a close b)
      (goto-char region-start))))
Drew
  • 75,699
  • 9
  • 109
  • 225
  • I would like to scroll through a list such as `()`, `[]`, `{}`, `<>`. Otherwise, allow the user to enter his own `open` and `close` string. When using `(interactive "cOpening delimiter character: \ncClosing delimiter character: \nr")` the input is never shown, the output to the minibuffer gets very weird. – Dilna Mar 18 '22 at 14:31
  • Please do not try to "evolve" your questions - moving goal posts, extending, enlarging to a discussion, etc.. And please keep each post to *one question*. Post other, narrow/specific questions as needed. It sounds like you maybe have a question in store, about how to prompt for pairs of characters, with defaulting. That's not this question. – Drew Mar 18 '22 at 15:12