Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.7 KiB

  1. ;;; hide-lines.el --- Commands for hiding lines based on a regexp
  2. ;; Filename: hide-lines.el
  3. ;; Description: Commands for hiding lines based on a regexp
  4. ;; Author: Mark Hulme-Jones <ture at plig cucumber dot net>
  5. ;; Maintainer: Joe Bloggs <vapniks@yahoo.com>
  6. ;; Version: 20130623.1701
  7. ;; Package-Version: 20130623.1701
  8. ;; Package-Commit: 4bfb4c6f4769bd6c637e4c18bbf65506832fc9f0
  9. ;; Last-Updated: 2013-06-23 16:42:00
  10. ;; By: Joe Bloggs
  11. ;; URL: https://github.com/vapniks/hide-lines
  12. ;; Keywords: convenience
  13. ;; Compatibility: GNU Emacs 24.3.1
  14. ;; Package-Requires:
  15. ;;
  16. ;; Features that might be required by this library:
  17. ;;
  18. ;;
  19. ;;
  20. ;;; This file is NOT part of GNU Emacs
  21. ;;; License
  22. ;;
  23. ;; This program is free software; you can redistribute it and/or modify
  24. ;; it under the terms of the GNU General Public License as published by
  25. ;; the Free Software Foundation; either version 3, or (at your option)
  26. ;; any later version.
  27. ;; This program is distributed in the hope that it will be useful,
  28. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. ;; GNU General Public License for more details.
  31. ;; You should have received a copy of the GNU General Public License
  32. ;; along with this program; see the file COPYING.
  33. ;; If not, see <http://www.gnu.org/licenses/>.
  34. ;;; Commentary
  35. ;;
  36. ;; The simplest way to make hide-lines work is to add the following
  37. ;; lines to your .emacs file:
  38. ;;
  39. ;; (autoload 'hide-lines "hide-lines" "Hide lines based on a regexp" t)
  40. ;; (global-set-key (kbd "C-c /") 'hide-lines)
  41. ;;
  42. ;; Now, when you type C-c /, you will be prompted for a regexp
  43. ;; (regular expression). All lines matching this regexp will be
  44. ;; hidden in the buffer.
  45. ;;
  46. ;; Alternatively, you can type C-u C-c / (ie. provide a prefix
  47. ;; argument to the hide-lines command) to hide all lines that *do not*
  48. ;; match the specified regexp. If you want to reveal previously hidden
  49. ;; lines you can use any other prefix, e.g. C-u C-u C-c /
  50. ;;
  51. ;;; Commands:
  52. ;;
  53. ;; Below are complete command list:
  54. ;;
  55. ;; `hide-lines'
  56. ;; Hide lines matching the specified regexp.
  57. ;; `hide-lines-not-matching'
  58. ;; Hide lines that don't match the specified regexp.
  59. ;; `hide-lines-matching'
  60. ;; Hide lines matching the specified regexp.
  61. ;; `hide-lines-show-all'
  62. ;; Show all areas hidden by the filter-buffer command.
  63. ;;
  64. ;;; Customizable Options:
  65. ;;
  66. ;; Below are customizable option list:
  67. ;;
  68. ;; `hide-lines-reverse-prefix'
  69. ;; If non-nil then `hide-lines' will call `hide-lines-matching' by default, and `hide-lines-not-matching' with a single prefix.
  70. ;; default = nil. This variable is buffer local so you can use different values for different buffers.
  71. ;;; Installation:
  72. ;;
  73. ;; Put hide-lines.el in a directory in your load-path, e.g. ~/.emacs.d/
  74. ;; You can add a directory to your load-path with the following line in ~/.emacs
  75. ;; (add-to-list 'load-path (expand-file-name "~/elisp"))
  76. ;; where ~/elisp is the directory you want to add
  77. ;; (you don't need to do this for ~/.emacs.d - it's added by default).
  78. ;;
  79. ;; Add the following to your ~/.emacs startup file.
  80. ;;
  81. ;; (require 'hide-lines)
  82. ;;; Change log:
  83. ;;
  84. ;; 2013/06/22 - Add namespace prefixes to functions and variables.
  85. ;; Add licence and add to Marmalade repo.
  86. ;; Alter hide-lines so that it can also show all lines
  87. ;;
  88. ;; 24/03/2004 - Incorporate fix for infinite loop bug from David Hansen.
  89. ;;
  90. ;;; Acknowledgements:
  91. ;;
  92. ;; David Hansen.
  93. ;;
  94. ;;; TODO
  95. ;;
  96. ;;
  97. ;;
  98. ;;; Require
  99. ;;; Code:
  100. (defgroup hide-lines nil
  101. "Commands for hiding lines based on a regexp.")
  102. (defvar hide-lines-invisible-areas ()
  103. "List of invisible overlays used by hidelines")
  104. (defcustom hide-lines-reverse-prefix nil
  105. "If non-nil then `hide-lines' will call `hide-lines-matching' by default, and `hide-lines-not-matching' with a single prefix.
  106. Otherwise it's the other way round.
  107. In either case a prefix arg with any value apart from 1 or 4 will call `hide-lines-show-all'."
  108. :type 'boolean
  109. :group 'hide-lines)
  110. (make-variable-buffer-local 'hide-lines-reverse-prefix)
  111. (add-to-invisibility-spec 'hl)
  112. ;;;###autoload
  113. (defun hide-lines (&optional arg)
  114. "Hide lines matching the specified regexp.
  115. With prefix arg of 4 (C-u) hide lines that do not match the specified regexp.
  116. With any other prefix arg, reveal all hidden lines."
  117. (interactive "p")
  118. (cond ((= arg 4) (call-interactively
  119. (if hide-lines-reverse-prefix 'hide-lines-matching
  120. 'hide-lines-not-matching)))
  121. ((= arg 1) (call-interactively
  122. (if hide-lines-reverse-prefix 'hide-lines-not-matching
  123. 'hide-lines-matching)))
  124. (t (call-interactively 'hide-lines-show-all))))
  125. (defun hide-lines-add-overlay (start end)
  126. "Add an overlay from `start' to `end' in the current buffer. Push the
  127. overlay onto the hide-lines-invisible-areas list"
  128. (let ((overlay (make-overlay start end)))
  129. (setq hide-lines-invisible-areas (cons overlay hide-lines-invisible-areas))
  130. (overlay-put overlay 'invisible 'hl)))
  131. ;;;###autoload
  132. (defun hide-lines-not-matching (search-text)
  133. "Hide lines that don't match the specified regexp."
  134. (interactive "MHide lines not matched by regexp: ")
  135. (set (make-local-variable 'line-move-ignore-invisible) t)
  136. (save-excursion
  137. (goto-char (point-min))
  138. (let ((start-position (point-min))
  139. (pos (re-search-forward search-text nil t)))
  140. (while pos
  141. (beginning-of-line)
  142. (hide-lines-add-overlay start-position (point))
  143. (forward-line 1)
  144. (setq start-position (point))
  145. (if (eq (point) (point-max))
  146. (setq pos nil)
  147. (setq pos (re-search-forward search-text nil t))))
  148. (hide-lines-add-overlay start-position (point-max)))))
  149. ;;;###autoload
  150. (defun hide-lines-matching (search-text)
  151. "Hide lines matching the specified regexp."
  152. (interactive "MHide lines matching regexp: ")
  153. (set (make-local-variable 'line-move-ignore-invisible) t)
  154. (save-excursion
  155. (goto-char (point-min))
  156. (let ((pos (re-search-forward search-text nil t))
  157. start-position)
  158. (while pos
  159. (beginning-of-line)
  160. (setq start-position (point))
  161. (end-of-line)
  162. (hide-lines-add-overlay start-position (+ 1 (point)))
  163. (forward-line 1)
  164. (if (eq (point) (point-max))
  165. (setq pos nil)
  166. (setq pos (re-search-forward search-text nil t)))))))
  167. ;;;###autoload
  168. (defun hide-lines-show-all ()
  169. "Show all areas hidden by the filter-buffer command."
  170. (interactive)
  171. (mapc (lambda (overlay) (delete-overlay overlay))
  172. hide-lines-invisible-areas)
  173. (setq hide-lines-invisible-areas ()))
  174. (provide 'hide-lines)
  175. ;; (magit-push)
  176. ;; (yaoddmuse-post "EmacsWiki" "hide-lines.el" (buffer-name) (buffer-string) "update")
  177. ;;; hide-lines.el ends here