最近剛好寫了一個python的折疊功能。能簡單。是基于emacs自帶的pythonmode。可以支持各種類型的block(如if,for等)。
關鍵是:mypython-fold/unfold-block。我把它綁到了F10.可以自己改。其他都只是些支持代碼。
(deffacecodepilot-folding-overlay
'((default(:inheritregion:box(:line-width1:color"DarkSeaGreen1":stylereleased-button)))
(((classcolor))(:background"DarkSeaGreen2":foreground"black")))
"*Fontusedbyfoldingoverlay."
:group'codepilot)
(defuncptree-ov-delete()
(interactive)
(dolist(o(overlays-at(point)))
(cptree-delete-overlayo'cptree)))
(defvarcptree--overlay-keymapnil"keymapforfoldingoverlay")
(unlesscptree--overlay-keymap
(let((map(make-sparse-keymap)))
(define-keymap[mouse-1]'cptree-ov-delete)
(define-keymap"
"'cptree-ov-delete)
(setqcptree--overlay-keymapmap)))
(defuncptree-delete-overlay(oprop)
(when(eq(overlay-geto'cptree-tag)prop)
(delete-overlayo)
t))
(defuncptree-hide-region(fromtoprop)
"Hidesaregionbymakinganinvisibleoverlayoveritandsavethe
overlayonthehide-region-overlays\"ring\""
(interactive)
(let((new-overlay(make-overlayfromto)))
;;(overlay-putnew-overlay'invisiblenil)
(overlay-putnew-overlay'cptree-tagprop)
(overlay-putnew-overlay'face'codepilot-folding-overlay)
(overlay-putnew-overlay'display
(propertize
(format"...<%dlines>..."
(1-(count-lines(overlay-startnew-overlay)
(overlay-endnew-overlay))))))
(overlay-putnew-overlay'priority(-0from))
(overlay-putnew-overlay'keymapcptree--overlay-keymap)
(overlay-putnew-overlay'pointer'hand)))
(require'python)
(defunmypython-fold/unfold-block()
"foldtheblock"
(interactive)
(let(retbe)
(dolist(o(overlays-at(if(python-open-block-statement-p)
(save-excursion
(python-end-of-statement)
(point)
)
(point))))
(when(cptree-delete-overlayo'cptree)
(setqrett)))
(unlessret
(save-excursion
(unless(python-open-block-statement-p)
(python-beginning-of-block))
(python-end-of-statement)
(setqb(point))
(python-end-of-block)
(setqe(1-(point)))
(cptree-hide-regionbe'cptree)))))
(define-keypython-mode-map[(f10)]'mypython-fold/unfold-block)