From a77d7f16345296bbbccc4f3d0fe45587c3907493 Mon Sep 17 00:00:00 2001 From: Greg Sexton Date: Sat, 24 Sep 2016 10:14:16 +0100 Subject: [PATCH] Add python parser Closes #22 --- origami-parsers.el | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 072769a..04524ae 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -187,6 +187,26 @@ position in the CONTENT." (origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content)) (origami-fold-root-node (funcall javadoc content))))))) +(defun origami-python-parser (create) + (lambda (content) + (with-temp-buffer + (insert content) + (python-mode) + (goto-char (point-min)) + (beginning-of-defun -1) + (let (beg (end (point-max)) offset acc) + (while (not (= (point) end)) + (setq beg (point)) + (search-forward-regexp ":" nil t) + (setq offset (- (point) beg)) + (end-of-defun) + (backward-char) + (setq end (point)) + (when (> offset 0) + (setq acc (cons (funcall create beg end offset nil) acc))) + (beginning-of-defun -1)) + (reverse acc))))) + (defun origami-lisp-parser (create regex) (lambda (content) (with-temp-buffer @@ -199,8 +219,8 @@ position in the CONTENT." (search-forward-regexp regex nil t) (setq offset (- (point) beg)) (end-of-defun) - (backward-char) ;move point to one after the last paren - (setq end (1- (point))) ;don't include the last paren in the fold + (backward-char) ;move point to one after the last paren + (setq end (1- (point))) ;don't include the last paren in the fold (when (> offset 0) (setq acc (cons (funcall create beg end offset nil) acc))) (beginning-of-defun -1)) @@ -231,7 +251,7 @@ position in the CONTENT." (js3-mode . origami-c-style-parser) (go-mode . origami-c-style-parser) (php-mode . origami-c-style-parser) - (python-mode . origami-indent-parser) + (python-mode . origami-python-parser) (emacs-lisp-mode . origami-elisp-parser) (lisp-interaction-mode . origami-elisp-parser) (clojure-mode . origami-clj-parser)