5

Is it possible to highlight matching quotes as show-paren-mode does with parentheses? The characters I'm looking to highlight are "dumb" quotes, both single (Unicode: apostrophe, U+0027) and double (Unicode: quotation mark, U+0022).

Sometimes I get lost in Fortran format specifiers, like '("strg1", i2, "strg2", i11)'. Highlighting matching quotes there would help me tell apart the pieces of the specifier.

I'd prefer not to use smartparens. If your solution uses it please provide a set-up that just highlights matching quotes without otherwise altering the pairing or highlighting of matching delimiters.

  • Please specify what you mean by matching quotes. Which characters? – Drew Jul 18 '18 at 19:34
  • @Drew Both ' and ". – Arch Stanton Jul 18 '18 at 21:14
  • Put that info in the question, please. So no "curly" quotes - just double-quote (you might mention a programming "string") and apostrophe chars. Giving their Unicode names or code points would be clearest. – Drew Jul 18 '18 at 23:03
2

Of course it is.

(defun show-paren--match-quotes ()
  (let ((ppss (syntax-ppss)))
    ;; In order to distinguish which quote is opening and which is starting,
    ;; check that that point is not within a string (or comment, for that
    ;; matter).  Also ignore escaped quotes.
    (unless (or (nth 8 ppss) (nth 5 ppss))
      (or
       (and (not (bobp))
            (eq 7 (car-safe (syntax-after (1- (point)))))
            (save-excursion
              (let ((end (point))
                    (ppss (syntax-ppss (1- (point)))))
                (when (nth 3 ppss)
                  (let ((beg (nth 8 ppss)))
                    (list beg
                          (1+ beg)
                          (1- end)
                          end))))))
       (and (not (eobp))
            (eq 7 (car-safe (syntax-after (point))))
            (save-excursion
              (let ((beg (point)))
                (condition-case nil
                    (progn
                      (forward-sexp 1)
                      (list beg
                            (1+ beg)
                            (1- (point))
                            (point)))))))))))

(advice-add 'show-paren--default :after-until #'show-paren--match-quotes)
  • Very nice! But I wanted this because I was getting lost on a Fortran format specifier sort of like '("blahblah", i2, "blah", i11)'. In it, show-paren--match-quotes only highlights the outer single quotes. – Arch Stanton Jul 18 '18 at 21:26
  • Shouldn't the advice-add rather be add-function on show-paren-data-function? – npostavs Jul 19 '18 at 9:34
  • @npostavs: Indeed. – Stefan Jul 19 '18 at 12:39
  • @ArchStanton: sorry, I missed that part of your question. I'm not sufficiently familiar with Fortran to know offhand how to fix that. What major mode do you use? f90-mode? – Stefan Jul 20 '18 at 12:01
  • @Stefan Yes. Btw don't worry, that was an edit that I added later. – Arch Stanton Jul 20 '18 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.