elide-head.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; elide-head.el --- hide headers in files
  2. ;; Copyright (C) 1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Keywords: outlines tools
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Functionality for eliding boilerplate text (normally copyright
  18. ;; notices) in file headers to avoid clutter when you know what it
  19. ;; says.
  20. ;;
  21. ;; `elide-head-headers-to-hide' controls what is elided by the command
  22. ;; `elide-head'. A buffer-local invisible overlay manages the
  23. ;; elision.
  24. ;; You might add `elide-head' to appropriate major mode hooks or to
  25. ;; `find-file-hook'. Please do not do this in site init files. If
  26. ;; you do, information may be hidden from users who don't know it
  27. ;; already.
  28. ;; Note that `hs-minor-mode' will do a similar job by default, but
  29. ;; it's not selective about what leading commentary it hides.
  30. ;; Inspired by jwz's hide-copyleft.el, for which we don't have an
  31. ;; assignment.
  32. ;;; Code:
  33. (defgroup elide-head nil
  34. "Eliding copyright headers and the like in source files."
  35. :version "21.1"
  36. :prefix "elide-head"
  37. :group 'tools)
  38. (defcustom elide-head-headers-to-hide
  39. '(("is free software[:;] you can redistribute it" . ; GNU boilerplate
  40. "\\(Boston, MA 0211\\(1-1307\\|0-1301\\), USA\\|\
  41. If not, see <http://www\\.gnu\\.org/licenses/>\\)\\.")
  42. ("The Regents of the University of California\\. All rights reserved\\." .
  43. "SUCH DAMAGE\\.") ; BSD
  44. ("Permission is hereby granted, free of charge" . ; X11
  45. "authorization from the X Consortium\\."))
  46. "Alist of regexps defining start end end of text to elide.
  47. The cars of elements of the list are searched for in order. Text is
  48. elided with an invisible overlay from the end of the line where the
  49. first match is found to the end of the match for the corresponding
  50. cdr."
  51. :group 'elide-head
  52. :type '(alist :key-type (string :tag "Start regexp")
  53. :value-type (string :tag "End regexp")))
  54. (defvar elide-head-overlay nil)
  55. (make-variable-buffer-local 'elide-head-overlay)
  56. ;;;###autoload
  57. (defun elide-head (&optional arg)
  58. "Hide header material in buffer according to `elide-head-headers-to-hide'.
  59. The header is made invisible with an overlay. With a prefix arg, show
  60. an elided material again.
  61. This is suitable as an entry on `find-file-hook' or appropriate mode hooks."
  62. (interactive "P")
  63. (if arg
  64. (elide-head-show)
  65. (save-excursion
  66. (save-restriction
  67. (let ((rest elide-head-headers-to-hide)
  68. beg end)
  69. (widen)
  70. (goto-char (point-min))
  71. (while rest
  72. (save-excursion
  73. (when (re-search-forward (caar rest) nil t)
  74. (setq beg (point))
  75. (when (re-search-forward (cdar rest) nil t)
  76. (setq end (point-marker)
  77. rest nil))))
  78. (if rest (setq rest (cdr rest))))
  79. (if (not (and beg end))
  80. (if (called-interactively-p 'interactive)
  81. (message "No header found"))
  82. (goto-char beg)
  83. (end-of-line)
  84. (if (overlayp elide-head-overlay)
  85. (move-overlay elide-head-overlay (point-marker) end)
  86. (setq elide-head-overlay (make-overlay (point-marker) end)))
  87. (overlay-put elide-head-overlay 'invisible t)
  88. (overlay-put elide-head-overlay 'evaporate t)
  89. (overlay-put elide-head-overlay 'after-string "...")))))))
  90. (defun elide-head-show ()
  91. "Show a header elided current buffer by \\[elide-head]."
  92. (interactive)
  93. (if (and (overlayp elide-head-overlay)
  94. (overlay-buffer elide-head-overlay))
  95. (delete-overlay elide-head-overlay)
  96. (if (called-interactively-p 'interactive)
  97. (message "No header hidden"))))
  98. (provide 'elide-head)
  99. ;;; elide-head.el ends here