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

问题描述

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

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

主要从。这导致emacs在保存C ++文件之前在缓冲区上运行 untabify



问题是,在我有加载了一个C ++文件, untabify 钩子正在应用于所有后续文件写入,即使是其他文件类型的缓冲区。这意味着如果我打开一个C ++文件,然后编辑,比如一个制表符分隔的文本文件,那么在保存文件时,这些选项卡会被破坏。



我不是一个elisp大师,但我认为(make-local-hook'write-contents-hooks)行试图添加到内容挂钩仅适用于本地缓冲区。但是,它不起作用,所有缓冲区的 c ++ - mode-untabify write-contents-hooks 中。



我在Windows XP盒子上使用了EmacsW32 22.0。有没有人有任何想法如何使写入内容挂钩将本地更改为特定缓冲区或如何重置为 nil 切换到其他非C ++缓冲区?

解决方案

write-contents-hooks也已过时。这是你之后:

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

这是从我使用的,它做了一些其他的东西,并抽象出来使用编程特定的模式: / p>

 (defun untabify-buffer()
取消当前缓冲区
(interactive)
(点数最小)(点最大值))

(defun progmodes-hooks()
编程模式的钩子
(yas / minor-mode- on)
(add-hook'before-save-hook'progmodes-write-hooks))

(defun progmodes-write-hooks()
运行的钩子文件写入编程模式
(prog1 nil
(set-buffer-file-coding-system'utf-8-unix)
(untabify-buffer)
(copyright-更新)
(maybe-delete-trailing-whitesp ace)))

(defun delete-trailing-whitespacep()
我们应该在保存此文件时删除尾随空格吗?
(save-excursion
goto-char(point-min))
(ignore-errors(next-line 25))
(let((pos(point)))
(goto-char ))
(和(重新搜索转发(concat@author +用户全名)pos t)t))))

(defun maybe-delete-trailing -whitespace()
如果我是这个文件的作者,删除尾随空格。
(interactive)
(和(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)


I have the following in my .emacs file:

 (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)))

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.

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.

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.

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 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在保存某些文件类型(并且只有那些文件类型)时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:01