Emacs modes: highlighting the use of a variable under a dot

In Python mode, if I put a point in an argument xin the following function definition

def f(x):
  #   ^------ point here
  print x

use xin the body of the function is highlighted in yellow.

  • How it works?
  • If I were to write my own Emacs mode, how can I add such highlighting to my language?
+4
source share
4 answers

I would not implement such a function in native mode. The general small mode, which tries to support as many basic modes as possible based on common Emacs interfaces (for example, syntax tables), is more appropriate, and it is not surprising that someone already wrote such a mode: highlight-symbol .

init.el Emacs (, , , ) :

(add-hook 'prog-mode-hook #'highlight-symbol-mode)

, highlight-symbol-nav-mode, C-n C-p , , , .

+3
(require 'hi-lock)


(defvar args-highlighted-p nil)
(defvar args-highlighted-regexp "\_<.+\_>")

(defun highlight-args ()
  "With cursor at function argument, highlight args in body.

When already highlighted, un-highlight. "
  (interactive)
  (save-excursion
    (if args-highlighted-p
    (progn
      (setq args-highlighted-p nil)
      (hi-lock-unface-buffer args-highlighted-regexp))
      (let ((erg (prin1-to-string (symbol-at-point)))
        (end (funcall end-of-defun-function)))
    (if
        erg
        (progn
          (setq args-highlighted-p t)
          (setq args-highlighted-regexp (concat "\\_<" erg "\\_>"))
          (hi-lock-face-symbol-at-point)
          (while
          (re-search-forward "\_<erg\_>" nil t end)
        (hi-lock-face-symbol-at-point))))))))
+1

hlt-highlight-symbol highlight.el , . (. hlt-auto-faces-flag.) , highlight-symbol ( ) . . .

, Emacs 24.4 (.. ), hi-lock-face-symbol-at-point , , (-).

+1

, , a highlight-tags , , - sgml.

, , , highlight-tags-update/hide/show . , , sgml , . , .

, :

  • word-at-point
  • (, python-nav-*-block search-forward)
  • start/end
  • overlay start/end make-overlay .

, -, (mapcar #'delete-overlay overlays).

, hook post-command-hook, .

0

Source: https://habr.com/ru/post/1531997/


All Articles