3

In a buffer with source code that has already been syntax highlighted (eg some Python code in python-mode), how do I access the results of the syntax parsing to select text from elisp that is a particular syntax?

For example if I want to select the next keyword, or select all of the current comment around the point, or all of the string around point.

Thanks

1 Answer 1

2

Select the string around point:

(defun my-string-atpt ()
  "Hightlight string at point. "
  (interactive)
  (let ((pps (parse-partial-sexp (point-min) (point))))
    (when
    (nth 3 pps)
      (goto-char (nth 8 pps))
      (set-mark (point))
      (forward-sexp))))

Comment at point might start that way:

(defun my-comment-atpt ()
  "Highlight comment at point. "
  (interactive)
  (let ((pps (parse-partial-sexp (point-min) (point))))
    (when
    (nth 4 pps)
      (goto-char (nth 8 pps))
      (set-mark (point))...

However, as (nth 4 pps) is reliable only WRT current line, needs a loop still to detect the beginning of a commented section, likewise the end.

A complete implemention as ar-comment-atpt should exist there:

https://github.com/andreas-roehler/werkstatt/blob/master/thing-at-point-utils.el

Select the next item fontified as keyword like this:

(defun my-next-font-lock-keyword-face ()
  (interactive)
  (while (and (not (eobp)) (forward-char 1) (not (eq (face-at-point) 'font-lock-keyword-face))))
  (when (eq (face-at-point) 'font-lock-keyword-face)
    (set-mark (point))
    (while (eq (face-at-point) 'font-lock-keyword-face)
      (forward-char 1))))
1
  • Thanks! Having read the documentation for parse-partial-sexp, I can see that it will be very useful for what I'm trying to do! Commented May 18, 2015 at 10:38

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.