From d586c41b21702b9644130d6cb50b272c77006892 Mon Sep 17 00:00:00 2001 From: Greg Sexton Date: Sat, 15 Nov 2014 16:38:50 +0000 Subject: [PATCH] Extract out local function --- origami-parsers.el | 49 ++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index f942e61..5ca6cff 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -30,6 +30,7 @@ ;;; Commentary: ;;; Code: +(require 'cl) (defcustom origami-parser-alist '((java-mode . origami-c-style-parser) @@ -53,33 +54,35 @@ (reverse acc)))) (defun origami-build-pair-tree (create open close positions) - ;; this is so horrible, but fast - (let (acc beg (should-continue t)) - (while (and should-continue positions) - (cond ((equal (caar positions) open) - (if beg ;go down a level - (let* ((res (origami-build-pair-tree create open close positions)) - (new-pos (car res)) - (children (cdr res))) - (setq positions (cdr new-pos)) - (setq acc (cons (funcall create beg (cdar new-pos) 0 children) acc)) - (setq beg nil)) - ;; begin a new pair - (setq beg (cdar positions)) - (setq positions (cdr positions)))) - ((equal (caar positions) close) - (if beg - (progn ;close with no children - (setq acc (cons (funcall create beg (cdar positions) 0 nil) acc)) - (setq positions (cdr positions)) - (setq beg nil)) - (setq should-continue nil))))) - (cons positions (reverse acc)))) + (cl-labels ((build (positions) + ;; this is so horrible, but fast + (let (acc beg (should-continue t)) + (while (and should-continue positions) + (cond ((equal (caar positions) open) + (if beg ;go down a level + (let* ((res (build positions)) + (new-pos (car res)) + (children (cdr res))) + (setq positions (cdr new-pos)) + (setq acc (cons (funcall create beg (cdar new-pos) 0 children) acc)) + (setq beg nil)) + ;; begin a new pair + (setq beg (cdar positions)) + (setq positions (cdr positions)))) + ((equal (caar positions) close) + (if beg + (progn ;close with no children + (setq acc (cons (funcall create beg (cdar positions) 0 nil) acc)) + (setq positions (cdr positions)) + (setq beg nil)) + (setq should-continue nil))))) + (cons positions (reverse acc))))) + (cdr (build positions)))) (defun origami-c-style-parser (create) (lambda (content) (let ((positions (origami-get-positions content "[{}]"))) - (cdr (origami-build-pair-tree create "{" "}" positions))))) + (origami-build-pair-tree create "{" "}" positions)))) (defun origami-lisp-parser (create regex) (lambda (content)