在保存某些文件类型

在保存某些文件类型

本文介绍了在保存某些文件类型(并且仅这些文件类型)时让 Emacs 取消标签化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following in my .emacs file:

 (defun c++-mode-untabify ()
   (save-excursion
     (goto-char (point-min))
     (while (re-search-forward "[ 	]+$" nil t)
       (delete-region (match-beginning 0) (match-end 0)))
     (goto-char (point-min))
     (if (search-forward "	" 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 在保存 C++ 文件之前在缓冲区上运行 untabify.

Mostly ripped off from http://www.jwz.org/doc/tabs-vs-spaces.html. This causes emacs to run untabify on the buffer before saving a C++ file.

问题是,在我加载了一个 C++ 文件之后,untabify 钩子被应用于所有 后续文件写入,即使对于其他文件类型的缓冲区也是如此.这意味着,如果我打开一个 C++ 文件,然后编辑一个以制表符分隔的文本文件,则在保存文件时制表符会被破坏.

The problem is that after I have loaded a C++ file, the untabify hook is being applied to all subsequent file writes, even for buffers of other file types. This means that if I open a C++ file and then edit, say, a tab-delimited text file, the tabs get clobbered when saving the file.

我不是 elisp 大师,但我认为 (make-local-hook 'write-contents-hooks) 行试图添加 write-contents-hooks 仅适用于本地缓冲区.但是,它不起作用,并且 c++-mode-untabify 位于所有缓冲区的 write-contents-hooks 中.

I'm not an elisp guru, but I think the (make-local-hook 'write-contents-hooks) line is trying to make the addition to write-contents-hooks apply only to the local buffer. However, it isn't working, and c++-mode-untabify is in write-contents-hooks for all buffers.

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

I'm using EmacsW32 22.0 on a Windows XP box. Does anyone have any idea how to make the write-contents-hooks change local to a specific buffer or how to reset it to nil when switching to other, non-C++ buffers?

推荐答案

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

write-contents-hooks is also obsolete. This is what you're after:

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

这是从我使用的东西中提炼出来的,它做一些其他的事情,并被抽象出来以使用特定于编程的模式:

This is distilled from what I use, which does a few other things and is abstracted out to work with programming-specific modes:

(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 取消标签化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:41