From b9ecf4b3550e6ad88127a01db0f0cc2601011409 Mon Sep 17 00:00:00 2001 From: forflo Date: Mon, 6 Jun 2016 02:52:41 +0200 Subject: [PATCH 1/3] Hacked support for Makros in C and C++ mode --- origami-parsers.el | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index ef43abb..d034cd2 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -115,6 +115,37 @@ position in the CONTENT." build-nodes cdr)))) +;; Added support for C-Makros +(defun origami-build-pair-tree-makro-c (create open openM close closeM positions) + (cl-labels ((build (positions) + ;; this is so horrible, but fast + (let (acc beg (should-continue t)) + (while (and should-continue positions) + (cond ((or (equal (caar positions) open) + (equal (caar positions) openM)) + (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) (length open) children) + acc)) + (setq beg nil)) + ;; begin a new pair + (setq beg (cdar positions)) + (setq positions (cdr positions)))) + ( (or (equal (caar positions) closeM) + (equal (caar positions) close)) + (if beg + (progn ;close with no children + (setq acc (cons (funcall create beg (cdar positions) (length close) nil) + acc)) + (setq positions (cdr positions)) + (setq beg nil)) + (setq should-continue nil))))) + (cons positions (reverse acc))))) + (cdr (build positions)))) + (defun origami-build-pair-tree (create open close positions) (cl-labels ((build (positions) ;; this is so horrible, but fast @@ -143,6 +174,13 @@ position in the CONTENT." (cons positions (reverse acc))))) (cdr (build positions)))) +;; Support for C-Makros +(defun origami-c-with-makros-parser (create) + (lambda (content) + (let ((positions (origami-get-positions content "#ifdef\\|#endif\\|[{}]"))) + (origami-build-pair-tree-makro-c + create "{" "#ifdef" "}" "#endif" positions)))) + ;;; TODO: tag these nodes? have ability to manipulate nodes that are ;;; tagged? in a scoped fashion? (defun origami-javadoc-parser (create) @@ -208,8 +246,8 @@ position in the CONTENT." (defcustom origami-parser-alist `((java-mode . origami-java-parser) - (c-mode . origami-c-style-parser) - (c++-mode . origami-c-style-parser) + (c-mode . origami-c-with-makros-parser) + (c++-mode . origami-c-with-makros-parser) (perl-mode . origami-c-style-parser) (cperl-mode . origami-c-style-parser) (js-mode . origami-c-style-parser) From f608e09be3e4c5108f76db5a75aca6d645f52482 Mon Sep 17 00:00:00 2001 From: Greg Sexton Date: Sun, 3 Jul 2016 16:21:41 +0100 Subject: [PATCH 2/3] Reuse existing machinery --- origami-parsers.el | 60 ++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index d034cd2..812bf59 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -115,44 +115,13 @@ position in the CONTENT." build-nodes cdr)))) -;; Added support for C-Makros -(defun origami-build-pair-tree-makro-c (create open openM close closeM positions) - (cl-labels ((build (positions) - ;; this is so horrible, but fast - (let (acc beg (should-continue t)) - (while (and should-continue positions) - (cond ((or (equal (caar positions) open) - (equal (caar positions) openM)) - (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) (length open) children) - acc)) - (setq beg nil)) - ;; begin a new pair - (setq beg (cdar positions)) - (setq positions (cdr positions)))) - ( (or (equal (caar positions) closeM) - (equal (caar positions) close)) - (if beg - (progn ;close with no children - (setq acc (cons (funcall create beg (cdar positions) (length close) nil) - acc)) - (setq positions (cdr positions)) - (setq beg nil)) - (setq should-continue nil))))) - (cons positions (reverse acc))))) - (cdr (build positions)))) - (defun origami-build-pair-tree (create open close positions) (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 + (if beg ;go down a level (let* ((res (build positions)) (new-pos (car res)) (children (cdr res))) @@ -165,7 +134,7 @@ position in the CONTENT." (setq positions (cdr positions)))) ((equal (caar positions) close) (if beg - (progn ;close with no children + (progn ;close with no children (setq acc (cons (funcall create beg (cdar positions) (length close) nil) acc)) (setq positions (cdr positions)) @@ -174,13 +143,6 @@ position in the CONTENT." (cons positions (reverse acc))))) (cdr (build positions)))) -;; Support for C-Makros -(defun origami-c-with-makros-parser (create) - (lambda (content) - (let ((positions (origami-get-positions content "#ifdef\\|#endif\\|[{}]"))) - (origami-build-pair-tree-makro-c - create "{" "#ifdef" "}" "#endif" positions)))) - ;;; TODO: tag these nodes? have ability to manipulate nodes that are ;;; tagged? in a scoped fashion? (defun origami-javadoc-parser (create) @@ -203,6 +165,20 @@ position in the CONTENT." (if (listp face) face (list face))))))))) (origami-build-pair-tree create "{" "}" positions)))) +(defun origami-c-macro-parser (create) + (lambda (content) + (let ((positions (origami-get-positions content "#ifdef\\|#endif"))) + (origami-build-pair-tree create "#ifdef" "#endif" positions)))) + +(defun origami-c-parser (create) + (let ((c-style (origami-c-style-parser create)) + (macros (origami-c-macro-parser create))) + (lambda (content) + (origami-fold-children + (origami-fold-shallow-merge + (origami-fold-root-node (funcall c-style content)) + (origami-fold-root-node (funcall macros content))))))) + (defun origami-java-parser (create) (let ((c-style (origami-c-style-parser create)) (javadoc (origami-javadoc-parser create))) @@ -246,8 +222,8 @@ position in the CONTENT." (defcustom origami-parser-alist `((java-mode . origami-java-parser) - (c-mode . origami-c-with-makros-parser) - (c++-mode . origami-c-with-makros-parser) + (c-mode . origami-c-parser) + (c++-mode . origami-c-parser) (perl-mode . origami-c-style-parser) (cperl-mode . origami-c-style-parser) (js-mode . origami-c-style-parser) From af481d2b423957c58fa635ccb4c7ef52b906cbb4 Mon Sep 17 00:00:00 2001 From: Greg Sexton Date: Sun, 3 Jul 2016 16:28:33 +0100 Subject: [PATCH 3/3] Use more general #if --- origami-parsers.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 812bf59..072769a 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -167,8 +167,8 @@ position in the CONTENT." (defun origami-c-macro-parser (create) (lambda (content) - (let ((positions (origami-get-positions content "#ifdef\\|#endif"))) - (origami-build-pair-tree create "#ifdef" "#endif" positions)))) + (let ((positions (origami-get-positions content "#if\\|#endif"))) + (origami-build-pair-tree create "#if" "#endif" positions)))) (defun origami-c-parser (create) (let ((c-style (origami-c-style-parser create))