0

When writing utilities that operate on "strings", the syntax-table can be used along with the parse state (syntax-ppss), however some major modes (such as text-mode) won't treat quoted text as strings.

This means the following text:

Hello "this is a string". 

Is there a convenient way to detect this which doesn't involve hard-coding a list of known major modes?

3
  • You can position point somewhere in a string and then ask syntax-ppss to parse it: if (nth 3 (syntax-ppss)) says nil, then that syntax table does not recognize strings of whatever form you asked about. Whether that's enough to cover all your cases (and whether it's robust enough to work properly for all of them), I don't know - but it's worth trying, I think. Do C-h f syntax-ppss and C-h f parse-partial-sexp for the details of the return value. Commented Oct 31 at 3:37
  • This is something that needs to be done quickly as part of a package, so it's not practical to move the point around - and I can't guess what the user would have in their buffer. Commented Oct 31 at 5:34
  • You don't need to move the point in the original buffer or know what's in it. See my answer. Commented Oct 31 at 20:41

2 Answers 2

1

You could scan the syntax table for a string-quote character (code 7), bearing in mind that a syntax quote character might be in a parent syntax table but also the child syntax table might disable it. I don't think you can do much about people who set the syntax-table text property.

Something like:

(defun quotable () (let (qq (st (syntax-table))) (while (and st (not qq)) (map-char-table (lambda (k v) (if (and (= 7 (car v)) (= 7 (car (aref (syntax-table) k)))) (setq qq k))) st) (setq st (char-table-parent st))) qq)) 
0

Per my comment, here is a function that you can call to tell you whether strings are recognized by the syntax table of some mode:

(defun my/syntax-string-capable-p (mode) (with-temp-buffer (funcall mode) (insert "\"This is a string\"") (goto-char (- (point) 2)) (if (nth 3 (syntax-ppss)) t nil))) 

It takes a symbol as argument (the symbol corresponding to a major mode). The function then creates a temp buffer and sets the mode as specified, inserts a purported string and asks syntax-ppss to parse it. It returns t or nil according to the third (counting from 0) value in the list that syntax-ppss returns (see the doc string of parse-partial-sexp for an explanation of the values in that list).

You can call it like this: (my/syntax-string-capable-p major-mode) to test the capability of the mode of the current buffer, or you can call it like this: (my/syntax-string-capable-p 'text-mode) to test the capability of the given mode.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.