nox在进行代码导航(跳转)时,以只读模式打开非项目文件(库文件)

我使用 nox+mspyls 来做 python 代码补全和导航,但是在使用 xref-find-definitions 跳转定义到一些库文件时,发现不是以只读模式打开的文件,这样就可能会不小心键入了一些内容到库文件,所以我想以只读模式在代码导航的时候打开那些非项目文件。我不知道这里应该如何设置,希望大家能给我一些思路。另外,我发现在对 elisp 代码导航到库文件时,它是以只读模式打开的。

advice

谢谢懒猫大大的回复,请问 advice 是加在 xref-find-definition 上吗,当它打开一个非项目文件的 buffer 的时候,将 buffer 设置为只读?我对 emacs lisp 还不是特别熟悉,希望您能再指点一下,然后我去试试

是的,可以看一下上个命令是否是nox调用的,再设置成只读打开

感谢 @manateelazycat 的 hint,我的问题解决了,实验室的师兄给出了解决办法哈哈,本想让他在帖子下面给出解答的,结果没想到他在论坛没有注册账号哈哈, 当初还是他告诉我这个论坛的 =_=.

   (defun xref--pop-to-location@around (func item &optional action)
   (let* ((marker (save-excursion
               (xref-location-marker (xref-item-location item))))
     (buf (marker-buffer marker))
     (file (xref-location-group (xref-item-location item)))
     (directory (projectile-project-root)))
(if (not (string-match directory file))
    (with-current-buffer buf
      (read-only-mode)))
(funcall func item action)
))

(advice-add 'xref--pop-to-location :around #'xref--pop-to-location@around)