The following code selects a string with two delimiters from the predefined list stored in delimiter-strings. The first delimiter being the opening bracketing mark, the second delimiter being the closing bracketing mark.

I want to adopt the function to accept the pre-defined list in delimiter-strings, but also let users input their preferred delimiters (e.g. "(]", "(}", ...). In case users input just one or more than two characters, I can count each of them.

    (defcustom delimiter-strings '("()" "[]" "{}")
      "Two-character strings of open and close delimiters."
      :type '(repeat string)
      :group 'convenience)

    (defun echo-delimiter-chars (string start end)
      "Enter a 2-char string of delimiters, see what they are, return them."
      (interactive (list (completing-read "Delimiter pair: " delimiter-strings nil t)
      (let ((open   (aref string 0))
            (close  (aref string 1)))
        (message "Delimiters you've chosen: `%c' and `%c'" open close)
        (list open close)))

1 Answer 1


Just use this instead of the completing-read call you show:

(completing-read "Delimiter pair: " delimiter-strings)

That doesn't require the user input to match one of the delimiter-strings. See what C-h f completing-read tells you about optional arg REQUIRE-MATCH.

But then you probably want to put that call in a while loop, testing whether what the user input is a string of length 2. Or else just use the first two chars of the string input. Or else use its first and last chars. And at least test that the string is at least 2 chars. Etc.

IOW, you need to actually define the behavior you're looking for. Specify the behavior first, then implement it.

  • I could accept a sequence of characters, and count for each character.
    – Dilna
    Commented Mar 20, 2022 at 15:21
  • Please don't use comments to respecify or evolve your question. Try to specify your question clearly from the beginning. This isn't a discussion forum. Comments are not searchable, and they can be deleted at any time. Questions and answers need to stand on their own.
    – Drew
    Commented Mar 20, 2022 at 15:36
  • Ok. How can I check for two elements? Would the check go in the let* part ?
    – Dilna
    Commented Mar 20, 2022 at 16:31
  • No. What I suggested was replacing the call to completing-read in the interactive spec with a while look that keeps calling completing-read until what was input is a string of (at least two chars). IOW, test the result of completing-read till it's OK to use.
    – Drew
    Commented Mar 20, 2022 at 20:53
  • I would then need to test whether what the user input is a string of length 2, as you pointed out to me. Have now set a variable rqmatch that sets the REQUIRE-MATCH for built-in function completing-read.
    – Dilna
    Commented Mar 20, 2022 at 23:27

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.