ReverseFlux

hide

Feb 2nd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. ;;; hideshowvis.el --- Add markers to the fringe for regions foldable by hideshow.el
  2. ;;
  3. ;; Copyright 2008-2012 Jan Rehders
  4. ;;
  5. ;; Author: Jan Rehders <[email protected]>
  6. ;; Version: 0.5
  7. ;; Contributions and bug fixes by Bryan Waite, Michael Heerdegen, John Yates and
  8. ;; Matthew Fidler.
  9. ;;
  10. ;; This file is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14. ;;
  15. ;; This file is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;;
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to
  22. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24. ;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; This minor mode will add little +/- displays to foldable regions in the
  29. ;; buffer and to folded regions. It is indented to be used in conjunction with
  30. ;; hideshow.el which is a part of GNU Emacs since version 20.
  31. ;;
  32. ;; Currently it works for me but is not tested heavily. Please report any bugs
  33. ;; to the above email address
  34. ;;
  35. ;;; Installation:
  36. ;; Add the following to your .emacs:
  37. ;;
  38. ;; (autoload 'hideshowvis-enable "hideshowvis" "Highlight foldable regions")
  39. ;;
  40. ;; (autoload 'hideshowvis-minor-mode
  41. ;; "hideshowvis"
  42. ;; "Will indicate regions foldable with hideshow in the fringe."
  43. ;; 'interactive)
  44. ;;
  45. ;;
  46. ;; (dolist (hook (list 'emacs-lisp-mode-hook
  47. ;; 'c++-mode-hook))
  48. ;; (add-hook hook 'hideshowvis-enable))
  49. ;;
  50. ;; If enabling hideshowvis-minor-mode is slow on your machine use M-x,
  51. ;; customize-option, hideshowvis-ignore-same-line and set it to nil. This will
  52. ;; then display - icons for foldable regions of one line, too but is faster
  53. ;;
  54. ;; To enable displaying a + symbol in the fringe for folded regions,
  55. ;; use:
  56. ;;
  57. ;; (hideshowvis-symbols)
  58. ;;
  59. ;; in your ~/.emacs
  60. ;;
  61. ;; It is not enabled by default because it might interfere with custom
  62. ;; hs-set-up-overlay functions
  63. ;;
  64. ;;; Changelog
  65. ;;
  66. ;; v0.5, 2012-09-11
  67. ;; - Made ELPA compliant and added function `hideshowvis-symbols'
  68. ;;
  69. ;; v0.4, 2012-03-13
  70. ;; - fixed bug causing transpose-words to be broken as well as causing problems
  71. ;; when auto-fill-mode was enabled
  72. ;;
  73. ;; v0.3, 2010-08-26
  74. ;; - added autoload cookies
  75. ;; - fixed bug causing major mode menu to disappear, among other things
  76. ;;
  77. ;; v0.2, 2009-08-09
  78. ;; - '-' symbol in fringe is clickable
  79. ;; - don't show '-' in fringe if the foldable region ends on the same line
  80. ;;
  81.  
  82. (define-fringe-bitmap 'hideshowvis-hideable-marker [0 0 0 126 126 0 0 0])
  83.  
  84. (defconst hideshowvis-version "v0.5" "Version of hideshowvis minor mode")
  85.  
  86. (defface hideshowvis-hidable-face
  87. '((t (:foreground "#ccc" :box t)))
  88. "Face to highlight foldable regions"
  89. :group 'hideshow)
  90.  
  91. (defcustom hideshowvis-ignore-same-line t
  92. "Do not display foldable regions in the fringe if the matching
  93. closing parenthesis is on the same line. Set this to nil if
  94. enabling the minor mode is slow on your machine"
  95. :group 'hideshow)
  96.  
  97. (defun hideshowvis-highlight-hs-regions-in-fringe (&optional start end old-text-length)
  98. (when hs-minor-mode
  99. (save-excursion
  100. (save-restriction
  101. (when (and start end)
  102. (narrow-to-region start end))
  103. (goto-char (point-min))
  104. (remove-overlays (point-min) (point-max) 'hideshowvis-hs t)
  105. (while (search-forward-regexp hs-block-start-regexp nil t)
  106. (let* ((ovl (make-overlay (match-beginning 0) (match-end 0)))
  107. (marker-string "*hideshowvis*")
  108. (doit
  109. (if hideshowvis-ignore-same-line
  110. (let (begin-line)
  111. (setq begin-line
  112. (save-excursion
  113. (goto-char (match-beginning 0))
  114. (line-number-at-pos (point))))
  115. (save-excursion
  116. (goto-char (match-beginning 0))
  117. (ignore-errors
  118. (progn
  119. (funcall hs-forward-sexp-func 1)
  120. (> (line-number-at-pos (point)) begin-line)))))
  121. t)))
  122. (when doit
  123. (put-text-property 0
  124. (length marker-string)
  125. 'display
  126. (list 'left-fringe
  127. 'hideshowvis-hideable-marker
  128. 'hideshowvis-hidable-face)
  129. marker-string)
  130. (overlay-put ovl 'before-string marker-string)
  131. (overlay-put ovl 'hideshowvis-hs t))))))))
  132.  
  133. ;;;###autoload
  134. (defun hideshowvis-click-fringe (event)
  135. (interactive "e")
  136. (mouse-set-point event)
  137. (end-of-line)
  138. (if (save-excursion
  139. (end-of-line 1)
  140. (or (hs-already-hidden-p)
  141. (progn
  142. (forward-char 1)
  143. (hs-already-hidden-p))))
  144. (hs-show-block)
  145. (hs-hide-block)
  146. (beginning-of-line)))
  147.  
  148. (defvar hideshowvis-mode-map
  149. (let ((hideshowvis-mode-map (make-sparse-keymap)))
  150. (define-key hideshowvis-mode-map [left-fringe mouse-1]
  151. 'hideshowvis-click-fringe)
  152. hideshowvis-mode-map)
  153. "Keymap for hideshowvis mode")
  154.  
  155. ;;;###autoload
  156. (define-minor-mode hideshowvis-minor-mode ()
  157. "Will indicate regions foldable with hideshow in the fringe."
  158. :init-value nil
  159. :require 'hideshow
  160. :group 'hideshow
  161. :keymap hideshowvis-mode-map
  162. (condition-case nil
  163. (if hideshowvis-minor-mode
  164. (progn
  165. (hs-minor-mode 1)
  166. (hideshowvis-highlight-hs-regions-in-fringe (point-min) (point-max) 0)
  167. (add-to-list 'after-change-functions
  168. 'hideshowvis-highlight-hs-regions-in-fringe))
  169. (remove-overlays (point-min) (point-max) 'hideshowvis-hs t)
  170. (setq after-change-functions
  171. (remove 'hideshowvis-highlight-hs-regions-in-fringe
  172. after-change-functions)))
  173. (error
  174. (message "Failed to toggle hideshowvis-minor-mode")
  175. )))
  176.  
  177. ;;;###autoload
  178. (defun hideshowvis-enable ()
  179. "Will enable hideshowvis minor mode"
  180. (interactive)
  181. (hideshowvis-minor-mode 1))
  182.  
  183. ;;;###autoload
  184. (defun hideshowvis-symbols ()
  185. "Defines the things necessary to get a + symbol in the fringe
  186. and a yellow marker indicating the number of hidden lines at
  187. the end of the line for hidden regions."
  188. (interactive)
  189.  
  190. (define-fringe-bitmap 'hs-marker [0 24 24 126 126 24 24 0])
  191.  
  192. (defcustom hs-fringe-face 'hs-fringe-face
  193. "*Specify face used to highlight the fringe on hidden regions."
  194. :type 'face
  195. :group 'hideshow)
  196.  
  197. (defface hs-fringe-face
  198. '((t (:foreground "#888" :box (:line-width 2 :color "grey75" :style released-button))))
  199. "Face used to highlight the fringe on folded regions"
  200. :group 'hideshow)
  201.  
  202. (defcustom hs-face 'hs-face
  203. "*Specify the face to to use for the hidden region indicator"
  204. :type 'face
  205. :group 'hideshow)
  206.  
  207. (defface hs-face
  208. '((t (:background "#ff8" :box t)))
  209. "Face to hightlight the ... area of hidden regions"
  210. :group 'hideshow)
  211.  
  212. (defun display-code-line-counts (ov)
  213. (when (eq 'code (overlay-get ov 'hs))
  214. (let* ((marker-string "*fringe-dummy*")
  215. (marker-length (length marker-string))
  216. (display-string (format "(%d)..." (count-lines (overlay-start ov) (overlay-end ov))))
  217. )
  218. (overlay-put ov 'help-echo "Hiddent text. C-c,= to show")
  219. (put-text-property 0 marker-length 'display (list 'left-fringe 'hs-marker 'hs-fringe-face) marker-string)
  220. (overlay-put ov 'before-string marker-string)
  221. (put-text-property 0 (length display-string) 'face 'hs-face display-string)
  222. (overlay-put ov 'display display-string)
  223. )))
  224.  
  225. (setq hs-set-up-overlay 'display-code-line-counts))
  226.  
  227. (provide 'hideshowvis)
  228.  
  229. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  230. ;;; hideshowvis.el ends here
Advertisement
Add Comment
Please, Sign In to add comment