/hide-region

Improved hide-region for Emacs

Primary LanguageEmacs Lisp

hide-region.el

About

“hide-region” is Emacs package to hide the selected region in the buffer.

The original version was copied from https://gist.github.com/mplscorwin/7832d7de7530ff355bff5778e46f477d/, and I adapt the code and made some improvement.

The old library introduction can be found in https://www.emacswiki.org/emacs/HideRegion.

Improvement

  1. Add hide-region-folded-face for the overlay, and remove hide-region-before-string, hide-region-after-string and hide-region-propertize-markers.
  2. Add hide-region-set-up-overlay-fn to allow customize the overaly.
  3. Add keymap hide-region-overlay-map for the overlay.
    By default, “<tab>” is binding to hide-region-unhide.
  4. Re-implement hide-region-hide, hide-region-unhide, and hide-region-unhide-all.
  5. Remove hide-region-unhide-below and hide-region-toggle.
  6. Add hide-region-toggle-hide.
  7. Support to navigate over all the hide regions.

Usage

Install

Firstly clone this git repo to “${user-emacs-directory}/packages/hide-region”, and add folowing lines to Emacs config file:

(use-package hide-region
  :defer t
  :ensure nil ; it is github package
  ;; If the path is relative, it is expanded within `user-emacs-directory'
  :load-path "packages/hide-region"
  :bind (("C-<f9>" . hide-region-toggle-hide)
         ("C-M-<f9>" . hide-region-unhide-all)
         ;; :map hide-region-overlay-map
         ;; ("<tab>" . hide-region-unhide)
         ;; ("n" . hide-region-next-ov)
         ;; ("p" . hide-region-pre-ov)
         )
  :init
  (let ((pkg-name "hide-region"))
    (ignore-errors
        (package-generate-autoloads pkg-name
                                    (expand-file-name (concat "packages/" pkg-name)
                                                      user-emacs-directory)))
    (load (concat pkg-name "-autoloads.el")))
  )

Usage

Commands

  1. hide-region-hide
    Hide the selected region.
  2. hide-region-unhide
    Unhide the hide region.
  3. hide-region-toggle-hide
    If the selected region is active, run hide-region-hide to hide the region; otherwise, get the overlays of current point, and unhide the region.
  4. hide-region-unhide-all
    Unhide all the region in the buffer.

Customize variable

  1. hide-region-set-up-overlay-fn
    Customize the overlay.
  2. hide-region-overlay-map
    Set new keybindings to the overlay keymap. \ By default,
    keyCommand
    “<tab>”hide-region-unhide
    “n”hide-region-next-ov
    “p”hide-region-pre-ov

Snapshot

  1. Default behavior

    ./image/image1.png

  2. Customize hide-region-set-up-overlay-fn by adding the info of number of lines
    (use-package hide-region
      :custom
      (hide-region-set-up-overlay-fn #'my-hide-region-folded-overlay)
      :init
      (defun my-hide-region-folded-overlay (ov)
        (let* ((nlines (count-lines (overlay-start ov) (overlay-end ov)))
               (info (format " ... #%d " nlines)))
          (overlay-put ov 'display (propertize info 'face hide-region-folded-face))))
      )
        

    ./image/image2.png