[emacs] python代码折叠
水木某大牛写的emacs python代码折叠代码, 试了下, 非常好用. 感谢作者: jcjbrian
http://www.newsmth.net/nForum/#!article/Emacs/81688
代码如下:
发信人: jcjbrian (Brian Jiang), 信区: Emacs
标 题: Re: python代码怎么折叠啊?
发信站: 水木社区 (Sun Sep 6 16:59:38 2009), 转信
最近刚好写了一个python的折叠功能。 能简单。 是基于emacs自带的python mode。可以支持各种类型的block (如if, for等)。
关键是: mypython-fold/unfold-block。 我把它绑到了F10. 可以自己改。 其他都只是些支持代码。
-------
(defface codepilot-folding-overlay '((default (:inherit region :box (:line-width 1 :color "DarkSeaGreen1" :style released-button))) (((class color)) (:background "DarkSeaGreen2" :foreground "black"))) "*Font used by folding overlay." :group 'codepilot) (defun cptree-ov-delete () (interactive) (dolist (o (overlays-at (point))) (cptree-delete-overlay o 'cptree))) (defvar cptree--overlay-keymap nil "keymap for folding overlay") (unless cptree--overlay-keymap (let ((map (make-sparse-keymap))) (define-key map [mouse-1] 'cptree-ov-delete) (define-key map "\r" 'cptree-ov-delete) (setq cptree--overlay-keymap map))) (defun cptree-delete-overlay(o prop) (when (eq (overlay-get o 'cptree-tag) prop) (delete-overlay o) t)) (defun cptree-hide-region (from to prop) "Hides a region by making an invisible overlay over it and save the overlay on the hide-region-overlays \"ring\"" (interactive) (let ((new-overlay (make-overlay from to))) ;;(overlay-put new-overlay 'invisible nil) (overlay-put new-overlay 'cptree-tag prop) (overlay-put new-overlay 'face 'codepilot-folding-overlay) (overlay-put new-overlay 'display (propertize (format "...<%d lines>..." (1- (count-lines (overlay-start new-overlay) (overlay-end new-overlay)))))) (overlay-put new-overlay 'priority (- 0 from)) (overlay-put new-overlay 'keymap cptree--overlay-keymap) (overlay-put new-overlay 'pointer 'hand))) (require 'python) (defun mypython-fold/unfold-block () "fold the block" (interactive) (let (ret b e) (dolist (o (overlays-at (if (python-open-block-statement-p) (save-excursion (python-end-of-statement) (point) ) (point)))) (when (cptree-delete-overlay o 'cptree) (setq ret t))) (unless ret (save-excursion (unless (python-open-block-statement-p) (python-beginning-of-block)) (python-end-of-statement) (setq b (point)) (python-end-of-block) (setq e (1- (point))) (cptree-hide-region b e 'cptree))))) (define-key python-mode-map [(f10)] 'mypython-fold/unfold-block)
posted on 2012-08-23 15:04 foreveryl 阅读(1451) 评论(0) 编辑 收藏 举报