当我尝试加载我的 TAGS 文件时,Emacs 一直说由于某种原因它不是有效的标签文件。创建命令非常简单:
$ ctags -e command-not-found.c
它看起来也很正常:
^L
command-not-found.c,246
#define MAXSGST ^?MAXSGST^A6,80
#define MIN(^?MIN^A7,99
char** getdirlst()^?getdirlst^A9,143
void freedirlst(char **dirlst)^?freedirlst^A30,581
int levenshtein(const char *s, const char *t)^?levenshtein^A39,726
int main(int argc, char **argv)^?main^A62,1321
确切的错误消息是:File /Users/Ron/Documents/Code/command-not-found/TAGS is not a valid tags table
。前几天还在用,有人知道怎么回事吗?在过去的几天里,我没有以任何方式更改我的配置。
编辑:今天在上周工作的现有 TAGS 文件中尝试了同样的事情,我得到了同样的错误。
编辑2:似乎我链接到的一些代码find-file-hook
导致了问题。我编写了一个函数来确定文件是否为二进制文件,如果是,则以hexl-mode
. 显然 emacs 在 TAGS 文件中时无法读取它们hexl-mode
,因此它说它无效。对此的快速解决方法是确定该文件是否为标签文件并将其置之不理:
;; open binaries in hexl-mode
(defun byte-plaintext-p (byte)
"Determine whether BYTE is plaintext or not."
(cl-case byte
((?\n ?\r ?\t) t)
(otherwise (and (<= byte 126) (>= byte 32)))))
(defun file-binary-p (file)
"Determine whether FILE is a binary file."
(with-temp-buffer
(insert-file-contents file)
(cl-loop for c across
(buffer-substring-no-properties
1 (min 100 (buffer-size)))
thereis (not (byte-plaintext-p c)))))
(defun file-tags-naive-p (file) ;; check if first character is '^L'
"Determine whether FILE is a TAGS file."
(let ((c (with-temp-buffer
(insert-file-contents file nil 0 1) (buffer-string))))
(if (= (aref c 0) 12) t nil)))
(add-hook 'find-file-hook
(lambda ()
(if (and (file-exists-p (buffer-file-name))
(> (buffer-size) 0)
(not (file-tags-naive-p (buffer-file-name))))
(if (and (file-binary-p (buffer-file-name))
(string= (buffer-local-value 'major-mode
(current-buffer))
"fundamental-mode"))
(hexl-mode)))))
有没有更好的解决方案?