我的.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)))
Run Code Online (Sandbox Code Playgroud)
大部分都是从http://www.jwz.org/doc/tabs-vs-spaces.html中删除的.这会导致emacs untabify在保存C++文件之前在缓冲区上运行.
问题是,在我加载了C++文件之后,untabify钩子正被应用于所有后续文件写入,即使对于其他文件类型的缓冲区也是如此.这意味着如果我打开一个C++文件然后编辑一个制表符分隔的文本文件,那么在保存文件时这些选项卡会被破坏.
我不是一个elisp大师,但我认为该(make-local-hook 'write-contents-hooks)行试图使添加write-contents-hooks仅适用于本地缓冲区.但是,它不起作用,并且c++-mode-untabify适用于write-contents-hooks所有缓冲区.
我在Windows XP机器上使用EmacsW32 22.0.有没有人知道如何在write-contents-hooks特定缓冲区本地进行更改或如何nil在切换到其他非C++缓冲区时将其重置为?
小智 21
write-contents-hooks也已经过时了.这就是你所追求的:
(add-hook 'c++-mode-hook
'(lambda ()
(add-hook 'before-save-hook
(lambda ()
(untabify (point-min) (point-max))))))
Run Code Online (Sandbox Code Playgroud)
这是从我使用的东西中提炼出来的,它还做了一些其他事情,并被抽象出来以使用特定于编程的模式:
(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)
Run Code Online (Sandbox Code Playgroud)
我的Emacs中的文档说自从21.1开始,make-local-hook现在已经过时了,因为add-hook现在需要一个可选的参数来创建一个hook buffer-local.所以你可以尝试:
(add-hook 'c++-mode-hook
'(lambda ()
(add-hook 'write-contents-hooks 'c++-mode-untabify nil t)))
Run Code Online (Sandbox Code Playgroud)
另一种选择是让c ++ - mode-untabify函数检查当前模式.我可能只是写这样的东西:
(defun c++-mode-untabify ()
(if (string= (substring mode-name 0 3) "C++")
(save-excursion
(delete-trailing-whitespace)
(untabify (point-min) (point-max)))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5239 次 |
| 最近记录: |