gpt4 book ai didi

emacs - 在保存某些文件类型(并且仅这些文件类型)时让 Emacs 取消制表符

转载 作者:行者123 更新时间:2023-12-04 00:12:27 25 4
gpt4 key购买 nike

我的 .emacs 文件中有以下内容:

 (defun c++-mode-untabify ()
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(if (search-forward "\t" nil t)
(untabify (1- (point)) (point-max))))
nil)

(add-hook 'c++-mode-hook
'(lambda ()
(make-local-hook 'write-contents-hooks)
(add-hook 'write-contents-hooks 'c++-mode-untabify)))

大部分来自 http://www.jwz.org/doc/tabs-vs-spaces.html .这会导致 emacs 运行 untabify在保存 C++ 文件之前在缓冲区上。

问题是在我加载了一个 C++ 文件后, untabify钩子(Hook)被应用到 全部 后续文件写入,即使是其他文件类型的缓冲区。这意味着,如果我打开一个 C++ 文件,然后编辑一个制表符分隔的文本文件,则在保存文件时制表符会被破坏。

我不是 elisp 大师,但我认为 (make-local-hook 'write-contents-hooks)行正在尝试添加到 write-contents-hooks仅适用于本地缓冲区。但是,它不起作用,并且 c++-mode-untabifywrite-contents-hooks对于所有缓冲区。

我在 Windows XP 机器上使用 EmacsW32 22.0。有谁知道如何制作 write-contents-hooks将本地更改为特定缓冲区或如何将其重置为 nil切换到其他非 C++ 缓冲区时?

最佳答案

write-contents-hooks 也已过时。这就是你所追求的:

(add-hook 'c++-mode-hook
'(lambda ()
(add-hook 'before-save-hook
(lambda ()
(untabify (point-min) (point-max))))))

这是从我使用的东西中提炼出来的,它做了一些其他的事情,并被抽象出来以使用特定于编程的模式:
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))

(defun progmodes-hooks ()
"Hooks for programming modes"
(yas/minor-mode-on)
(add-hook 'before-save-hook 'progmodes-write-hooks))

(defun progmodes-write-hooks ()
"Hooks which run on file write for programming modes"
(prog1 nil
(set-buffer-file-coding-system 'utf-8-unix)
(untabify-buffer)
(copyright-update)
(maybe-delete-trailing-whitespace)))

(defun delete-trailing-whitespacep ()
"Should we delete trailing whitespace when saving this file?"
(save-excursion
(goto-char (point-min))
(ignore-errors (next-line 25))
(let ((pos (point)))
(goto-char (point-min))
(and (re-search-forward (concat "@author +" user-full-name) pos t) t))))

(defun maybe-delete-trailing-whitespace ()
"Delete trailing whitespace if I am the author of this file."
(interactive)
(and (delete-trailing-whitespacep) (delete-trailing-whitespace)))

(add-hook 'php-mode-hook 'progmodes-hooks)
(add-hook 'python-mode-hook 'progmodes-hooks)
(add-hook 'js2-mode-hook 'progmodes-hooks)

关于emacs - 在保存某些文件类型(并且仅这些文件类型)时让 Emacs 取消制表符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/318553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com