;; notices) in file headers to avoid clutter when you know what it
;; says.
;;
-;; `elide-head-headers-to-hide' controls what is elided by the command
-;; `elide-head'. A buffer-local invisible overlay manages the
-;; elision.
+;; `elide-head-headers-to-hide' controls what is elided by the minor
+;; mode `elide-head-mode'. A buffer-local invisible overlay manages
+;; the elision.
-;; You might add `elide-head' to appropriate major mode hooks or to
-;; `find-file-hook'. Please do not do this in site init files. If
+;; You might add `elide-head-mode' to appropriate major mode hooks or
+;; to `find-file-hook'. Please do not do this in site init files. If
;; you do, information may be hidden from users who don't know it
;; already.
The cars of elements of the list are searched for in order. Text is
elided with an invisible overlay from the end of the line where the
first match is found to the end of the match for the corresponding
-cdr."
+cdr.
+
+This affects `elide-head-mode'."
:type '(alist :key-type (regexp :tag "Start regexp")
:value-type (regexp :tag "End regexp"))
:version "29.1")
(defvar-local elide-head-overlay nil)
+(defun elide-head--delete-overlay ()
+ "Delete the overlay in `elide-head-overlay'."
+ (when (overlayp elide-head-overlay)
+ (delete-overlay elide-head-overlay)))
+
+(defun elide-head--hide ()
+ "Hide elided (hidden) headers."
+ (save-excursion
+ (save-restriction
+ (let ((rest elide-head-headers-to-hide)
+ beg end)
+ (widen)
+ (goto-char (point-min))
+ (while rest
+ (save-excursion
+ (when (re-search-forward (caar rest) nil t)
+ (setq beg (point))
+ (when (re-search-forward (cdar rest) nil t)
+ (setq end (point-marker)
+ rest nil))))
+ (if rest (setq rest (cdr rest))))
+ (if (not (and beg end))
+ (if (called-interactively-p 'interactive)
+ (message "No header found"))
+ (goto-char beg)
+ (end-of-line)
+ (if (overlayp elide-head-overlay)
+ (move-overlay elide-head-overlay (point-marker) end)
+ (setq elide-head-overlay (make-overlay (point-marker) end)))
+ (overlay-put elide-head-overlay 'invisible t)
+ (overlay-put elide-head-overlay 'evaporate t)
+ (overlay-put elide-head-overlay 'after-string "..."))))))
+
+(defun elide-head--show ()
+ "Show elided (hidden) headers."
+ (if (and (overlayp elide-head-overlay)
+ (overlay-buffer elide-head-overlay))
+ (elide-head--delete-overlay)
+ (if (called-interactively-p 'interactive)
+ (message "No header hidden"))))
+
+;;;###autoload
+(define-minor-mode elide-head-mode
+ "Toggle eliding (hiding) header material in the current buffer.
+
+When Elide Header mode is enabled, headers are hidden according
+to `elide-head-headers-to-hide'.
+
+This is suitable as an entry on `find-file-hook' or appropriate
+mode hooks."
+ :group 'elide-head
+ (if elide-head-mode
+ (progn
+ (elide-head--hide)
+ (add-hook 'change-major-mode-hook 'elide-head--delete-overlay nil 'local))
+ (elide-head--show)
+ (remove-hook 'change-major-mode-hook 'elide-head--delete-overlay 'local)))
+
+\f
+;;; Obsolete
+
;;;###autoload
(defun elide-head (&optional arg)
"Hide header material in buffer according to `elide-head-headers-to-hide'.
an elided material again.
This is suitable as an entry on `find-file-hook' or appropriate mode hooks."
+ (declare (obsolete elide-head-mode "29.1"))
(interactive "P")
(if arg
- (elide-head-show)
- (save-excursion
- (save-restriction
- (let ((rest elide-head-headers-to-hide)
- beg end)
- (widen)
- (goto-char (point-min))
- (while rest
- (save-excursion
- (when (re-search-forward (caar rest) nil t)
- (setq beg (point))
- (when (re-search-forward (cdar rest) nil t)
- (setq end (point-marker)
- rest nil))))
- (if rest (setq rest (cdr rest))))
- (if (not (and beg end))
- (if (called-interactively-p 'interactive)
- (message "No header found"))
- (goto-char beg)
- (end-of-line)
- (if (overlayp elide-head-overlay)
- (move-overlay elide-head-overlay (point-marker) end)
- (setq elide-head-overlay (make-overlay (point-marker) end)))
- (overlay-put elide-head-overlay 'invisible t)
- (overlay-put elide-head-overlay 'evaporate t)
- (overlay-put elide-head-overlay 'after-string "...")))))))
+ (elide-head--show)
+ (elide-head--hide)))
(defun elide-head-show ()
"Show a header in the current buffer elided by \\[elide-head]."
+ (declare (obsolete elide-head-mode "29.1"))
(interactive)
- (if (and (overlayp elide-head-overlay)
- (overlay-buffer elide-head-overlay))
- (delete-overlay elide-head-overlay)
- (if (called-interactively-p 'interactive)
- (message "No header hidden"))))
+ (elide-head--show))
(provide 'elide-head)
(require 'elide-head)
(require 'ert)
+(require 'ert-x)
-(ert-deftest elide-head-tests-elide-head ()
+(ert-deftest elide-head-tests-elide-head-mode ()
(let ((elide-head-headers-to-hide '(("START" . "END"))))
(with-temp-buffer
(insert "foo\nSTART\nHIDDEN\nEND\nbar")
- (elide-head)
+ (elide-head-mode 1)
(let ((o (car (overlays-at 14))))
(should (= (overlay-start o) 10))
(should (= (overlay-end o) 21))
(should (overlay-get o 'invisible))
(should (overlay-get o 'evaporate))))))
-(ert-deftest elide-head-tests-elide-head-with-prefix-arg ()
+(ert-deftest elide-head-tests-elide-head-mode/enable-disable ()
(let ((elide-head-headers-to-hide '(("START" . "END"))))
(with-temp-buffer
(insert "foo\nSTART\nHIDDEN\nEND\nbar")
- (elide-head)
+ (elide-head-mode 1)
(should (overlays-at 14))
- (elide-head t)
+ (elide-head-mode -1)
(should-not (overlays-at 14)))))
-(ert-deftest elide-head-tests-show ()
- (let ((elide-head-headers-to-hide '(("START" . "END"))))
- (with-temp-buffer
- (insert "foo\nSTART\nHIDDEN\nEND\nbar")
- (elide-head)
- (should (overlays-at 14))
- (elide-head-show)
- (should-not (overlays-at 14)))))
+(ert-deftest elide-head-tests-elide-head-mode/normal-mode ()
+ (ert-with-temp-file fil
+ (with-temp-file fil
+ (insert "foo\nSTART\nHIDDEN\nEND\nbar"))
+ (let ((elide-head-headers-to-hide '(("START" . "END")))
+ (buf (find-file-noselect fil)))
+ (save-excursion
+ (unwind-protect
+ (progn
+ (set-buffer buf)
+ (elide-head-mode 1)
+ (should (= 1 (length (overlays-in (point-min) (point-max)))))
+ (normal-mode)
+ (should (= 0 (length (overlays-in (point-min) (point-max))))))
+ (when buf (kill-buffer buf)))))))
+
+(ert-deftest elide-head-tests-elide-head-mode/revert-buffer ()
+ (ert-with-temp-file fil
+ (with-temp-file fil
+ (insert "foo\nSTART\nHIDDEN\nEND\nbar"))
+ (let ((elide-head-headers-to-hide '(("START" . "END")))
+ (buf (find-file-noselect fil)))
+ (save-excursion
+ (unwind-protect
+ (progn
+ (set-buffer buf)
+ (elide-head-mode 1)
+ (should (= 1 (length (overlays-in (point-min) (point-max)))))
+ (revert-buffer nil t)
+ (elide-head-mode 1)
+ (should (= 1 (length (overlays-in (point-min) (point-max))))))
+ (when buf (kill-buffer buf)))))))
+
(defmacro elide-head--add-test (name text search-str)
`(ert-deftest ,(intern (format "elide-head--test-headers-to-hide/%s" name)) ()
(with-temp-buffer
(insert ,text)
- (elide-head)
+ (elide-head-mode 1)
(goto-char (point-min))
(re-search-forward ,search-str)
(let ((o (car (overlays-at (match-beginning 0)))))
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
" "This program is distributed in the hope that")
+\f
+;;; Obsolete
+
+(with-suppressed-warnings ((obsolete elide-head)
+ (obsolete elide-head-show))
+ (ert-deftest elide-head-tests-elide-head ()
+ (let ((elide-head-headers-to-hide '(("START" . "END"))))
+ (with-temp-buffer
+ (insert "foo\nSTART\nHIDDEN\nEND\nbar")
+ (elide-head)
+ (let ((o (car (overlays-at 14))))
+ (should (= (overlay-start o) 10))
+ (should (= (overlay-end o) 21))
+ (should (overlay-get o 'invisible))
+ (should (overlay-get o 'evaporate))))))
+
+ (ert-deftest elide-head-tests-elide-head-with-prefix-arg ()
+ (let ((elide-head-headers-to-hide '(("START" . "END"))))
+ (with-temp-buffer
+ (insert "foo\nSTART\nHIDDEN\nEND\nbar")
+ (elide-head)
+ (should (overlays-at 14))
+ (elide-head t)
+ (should-not (overlays-at 14)))))
+
+ (ert-deftest elide-head-tests-show ()
+ (let ((elide-head-headers-to-hide '(("START" . "END"))))
+ (with-temp-buffer
+ (insert "foo\nSTART\nHIDDEN\nEND\nbar")
+ (elide-head)
+ (should (overlays-at 14))
+ (elide-head-show)
+ (should-not (overlays-at 14))))))
+
(provide 'elide-head-tests)
;;; elide-head-tests.el ends here