Function: hif-merge-ifdef-region

hif-merge-ifdef-region is a byte-compiled function defined in hideif.el.gz.

Signature

(hif-merge-ifdef-region START END)

Documentation

This function merges nearby ifdef regions to form a bigger overlay.

The region is defined by START and END. This will decrease the number of overlays created.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/hideif.el.gz
(defun hif-merge-ifdef-region (start end)
  "This function merges nearby ifdef regions to form a bigger overlay.
The region is defined by START and END.  This will decrease the number of
overlays created."
  ;; Generally there is no need to call itself recursively since there should
  ;; originally exists no un-merged regions; however, if a part of the file is
  ;; hidden with `hide-ifdef-lines' equals to nil while another part with 't,
  ;; this case happens.
  ;; TODO: Should we merge? or just create a container overlay? -- this can
  ;; prevent `hideif-show-ifdef' expanding too many hidden contents since there
  ;; is only a big overlay exists there without any smaller overlays.
  (save-restriction
    (widen) ; Otherwise `point-min' and `point-max' will be restricted and thus
                                        ; fail to find neighbor overlays
    (let ((begovrs (overlays-in
                    (max (- start 2) (point-min))
                    (max (- start 1) (point-min))))
          (endovrs (overlays-in
                    (min (+ end 1) (point-max))
                    (min (+ end 2) (point-max))))
          (ob nil)
          (oe nil)
          b e)
      ;; Merge overlays before START
      (dolist (o begovrs)
        (when (overlay-get o 'hide-ifdef)
          (setq b (min start (overlay-start o))
                e (max end (overlay-end o)))
          (move-overlay o b e)
          (hif-merge-ifdef-region b e)
          (setq ob o)))
      ;; Merge overlays after END
      (dolist (o endovrs)
        (when (overlay-get o 'hide-ifdef)
          (setq b (min start (overlay-start o))
                e (max end (overlay-end o)))
          (move-overlay o b e)
          (hif-merge-ifdef-region b e)
          (setf oe o)))
      ;; If both START and END merging happens, merge into bigger one
      (when (and ob oe)
        (let ((b (min (overlay-start ob) (overlay-start oe)))
              (e (max (overlay-end ob) (overlay-end oe))))
          (delete-overlay oe)
          (move-overlay ob b e)
          (hif-merge-ifdef-region b e)))
      (or ob oe))))