在文件顶部隐藏长GPL版权声明


10

我正在处理很多* cpp和* h文件,这些文件开始包含很长的版权声明。我希望emacs将这些文件显示为好像不存在,而无需实际删除文本。

Thas是这样的:

/*
 * Copyright (C) 2006-2008 Author A
 * Copyright (C) 2006-2008 Author B
 * Copyright (C) 2006-2008 Author C
 * Copyright (C) 2006-2008 Author D
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * As a special exception, you may use this file as part of a free
 * software library without restriction. Specifically, if other files
 * instantiate templates or use macros or inline functions from this
 * file, or you compile this file and link it with other files to
 * produce an executable, this file does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License. This exception does not however invalidate any other
 * reasons why the executable file might be covered by the GNU Library
 * General Public License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

 #ifndef FILENAME
 #define FILENAME
 ...

应该看起来像这样

#ifndef FILENAME
#define FILENAME
...

Answers:


13

Emacs附带的elide-head.el功能可以满足您的要求。

要使用它,请添加elide-head到主模式挂钩或find-file-hook(在您的情况下c-mode-common-hook应该可以使用)。它可以将GPL许可证注释隐藏起来。要隐藏其他冗长的标题,请自定义elide-head-headers-to-hide

请注意,它不会在缓冲区顶部仅隐藏任何注释,而是使用正则表达式来匹配许可证的开头和结尾。


1
我喜欢这个命令。非常好。
Tu Do

每次都打我。每当我写东西时,都会有人首先想到它:)
wvxvw 2015年

12

这是一种方法:

将此添加到您的初始化文件:

(defun hide-banner ()
  (save-excursion
    (let* ((start (progn (beginning-of-buffer) (point)))
           (end (progn (forward-comment (buffer-size)) (point)))
           (over (make-overlay start end)))
      (overlay-put over 'invisible t))))

在要隐藏初始注释的缓冲区中添加:

// -*- eval: (hide-banner) -*-

或将相同的代码添加到缓冲区挂钩。或者,您当然可以更改要隐藏的注释的标识方式(如果您希望它选择#ifndef / #define对,则需要修改该hide-banner函数以搜索该注释,而不是第一个注释的结尾)。


作品!这样好多了,谢谢。万一任何身体都需要,请按以下步骤进行:(add-hook 'c-mode-common-hook 'hide-banner)
初学者
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.