简体   繁体   中英

How can I highlight tags in Emacs buffers?

I use find-tag and kill-this-buffer to navigate through Fortran 90 code with the help of key bindings like

(global-set-key [(control return)] 'find-tag ) ;
(global-set-key (kbd "C-w") 'kill-this-buffer) ;

This provides a relative smooth experience in code browsing. Unfortunately not all of my tags have the same highlighting. So I ended up in adding targets manually to font-lock-keywords with expressions like

(add-hook 'after-change-major-mode-hook
          (lambda ()
            (font-lock-add-keywords 'f90-mode
              '(
                ("\\(my_function\\)" 1 font-lock-function-name-face t )
                ))))

in my ~/.emacs.d/init.el .

Is there a solution to highlight every name, which is listed in the TAGS file, if it occurs in a buffer, which is in f90-mode?

I am most interested in a solution for f90-mode , but of course a general solution would be even better.

Maybe, but it is not too clear what you mean by "such highlighting" and especially "based on the contents of the TAGS file".

  • "Such highlighting" -- Font-lock highlighting is for the viewing buffer, in this case a buffer in f90-mode . So you really do need to add the font-locking for that mode, in any case. This is independent of how you get into the mode (from visiting TAGS or in some other way).

  • "Based on the contents of the TAGS file" -- Those contents will tell you what names are defined (and where), but nothing more.

But perhaps you are saying that you get the name my_function from the TAGS file, and that you want to font-lock (in f90-mode ) every name listed in TAGS as being defined. If that is the case then yes, you could parse TAGS to get a list of defined names, and add each of them to font-lock-keywords for f90-mode . You can use (tags-completion-table) to get the list of tags (defined names) in TAGS .

Just use mapatoms to iterate over the obarray returned by (tags-completion-table) . The function argument would add a font-lock-keywords entry for the symbol. Something like this (untested):

(defun foo ()
  (require 'etags)
  (let ((defined-symbs  (tags-completion-table)))
    (mapatoms (lambda (symb)
                (font-lock-add-keywords
                  'f90-mode `((,(format "\\(%s\\)" symb)
                              1 font-lock-function-name-face t)))))))

But be aware that (a) that will take a while to execute and (b) that adds every symbol defined in your TAGS table, so it will become font-locked in f90-mode . Is that really what you want? If your TAGS table is specifically created from (only) Fortran files, then that might make sense; otherwise, maybe not.

Because it can take a while, you would invoke foo only once, not each time you enter f90-mode . You could do that again after you update your TAGS file, to pick up any changes. If (b) is a consideration, then you can perhaps add some filtering test to the function arg to mapatoms , so that only certain symbols get font-locked.

Anyway, perhaps this will get you started. HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM