project autoload extension
[emacsstuff.git] / cc-ide / cc-ide.el
index 78f193b..1adbd3f 100644 (file)
@@ -59,6 +59,10 @@ correctly included.")
 
 (defvar ccide-clang-format-command nil)
 
+(defvar ccide-uncrustify-command "uncrustify")
+(defvar ccide-uncrustify-config nil)
+(defvar ccide-auto-format-tag nil)
+
 (defconst c-user-prefix-re (regexp-opt c-user-prefixes t))
 
 (defconst ccide-doxy-tag-re
@@ -1570,7 +1574,17 @@ instatiations at point."
   (ccide-directory-load-config)
   (ccide-auto-decorate-new-files)
   (if ccide-clang-format-command
-      (add-hook 'write-contents-functions 'ccide-clang-format-buffer nil t)))
+      (add-hook 'write-contents-functions 'ccide-clang-format-buffer nil t))
+  (if ccide-uncrustify-config
+      (add-hook 'write-contents-functions 'ccide-uncrustify-format-buffer nil t)))
+
+(defun ccide-match-auto-format-tag ()
+  (or (not ccide-auto-format-tag)
+      (and (save-excursion
+             (goto-char (point-min))
+             (search-forward ccide-auto-format-tag
+                             (save-excursion (forward-line 4) (point))
+                             t)) t)))
 
 (require 'xmltok)
 
@@ -1604,6 +1618,11 @@ instatiations at point."
                 while (not (eq tok 'end-tag))
                 concat (clang-format-parse-replacement-text)))))
 
+(defun clang-format-parse-header ()
+  (let ((incflag (clang-format-xmltok-attribute "incomplete_format")))
+    (if (string= incflag "true")
+        (warn "clang-format: C++ syntax error in file"))))
+
 (defun clang-format-parse-replacements (buffer)
   (save-excursion
     (set-buffer buffer)
@@ -1611,10 +1630,11 @@ instatiations at point."
     (xmltok-forward-prolog)
     (loop for tok = (xmltok-forward)
           while tok
+          for n = (buffer-substring-no-properties (1+ xmltok-start) xmltok-name-end)
           if (and (eq tok 'start-tag)
-                  (string= (buffer-substring-no-properties (1+ xmltok-start) xmltok-name-end)
-                           "replacement"))
-          collect (clang-format-parse-replacement))))
+                  (string= n "replacement")) collect (clang-format-parse-replacement)
+          else if (and (eq tok ' start-tag)
+                       (string= n "replacements")) do (clang-format-parse-header))))
 
 (defun clang-format-apply-replacements (replacements)
   (loop for (offset length replacement) in (nreverse replacements)
@@ -1625,7 +1645,7 @@ instatiations at point."
 
 (defun ccide-clang-format-buffer ()
   (interactive)
-  (if ccide-clang-format-command
+  (if (and ccide-clang-format-command (ccide-match-auto-format-tag))
       (let ((replacementsBuffer (get-buffer-create " *clang-format-replacements*")))
         (save-excursion (set-buffer replacementsBuffer) (erase-buffer))
         (shell-command-on-region (point-min) (point-max) ccide-clang-format-command
@@ -1635,10 +1655,60 @@ instatiations at point."
             (clang-format-apply-replacements replacements)))))
   nil)
 
-(defun ccide-project-load-config ()
-  (if (buffer-file-name)
-      (let ((conf (ccide-project-search-upwards '(".project.el" "project.el")
-                                                (file-name-directory (buffer-file-name)))))
+(defun ccide-parse-ed-diff (buffer)
+  ;; an ed-diff already has the replacements in reverse order
+  (save-excursion
+    (set-buffer buffer)
+    (goto-char (point-min))
+    (loop while (= (forward-line 1) 0)
+          if (looking-at "Unmatched")
+            do (error (buffer-substring-no-properties (point) (progn (end-of-line) (point))))
+          for b = (point)
+          for e = (save-excursion (end-of-line) (point))
+          for k = (search-forward "," e t)
+          for m = (buffer-substring-no-properties (1- e) e)
+          for rb = (when (not (equal m "d"))
+                     (forward-line 1) (point))
+          for re = (when (not (equal m "d"))
+                     (while (and (not (looking-at "\\.$")) (= (forward-line 1) 0))) (point))
+          for bl = (string-to-number (buffer-substring-no-properties b (1- (or k e))))
+          for el = (string-to-number (buffer-substring-no-properties (or k b) (1- e)))
+          collect (list (if (equal m "a") (1+ bl) bl)
+                        (1+ el)
+                        (if (not (equal m "d"))
+                            (buffer-substring-no-properties rb re)
+                          "")))))
+
+(defun ccide-apply-ed-diff (replacements)
+  (loop for (first last replacement) in replacements
+        do (progn
+             (goto-line first)
+             (delete-region (point) (progn (goto-line last) (point)))
+             (insert replacement))))
+
+(defun ccide-uncrustify-format-buffer ()
+  (interactive)
+  (if (and ccide-uncrustify-config (ccide-match-auto-format-tag))
+      (let ((tempFile (make-temp-file "ccideuncrustify"))
+            (diffBuffer (get-buffer-create " *uncrustify-format-diff*")))
+        (unwind-protect
+            (progn
+              (write-region (point-min) (point-max) tempFile)
+              (shell-command (concat (shell-quote-argument ccide-uncrustify-command)
+                                     " -c " (shell-quote-argument
+                                             (expand-file-name ccide-uncrustify-config))
+                                     " -l CPP -f " (shell-quote-argument tempFile)
+                                     " | diff -e " (shell-quote-argument tempFile) " -")
+                             diffBuffer nil))
+          (delete-file tempFile))
+        (let ((replacements (ccide-parse-ed-diff diffBuffer)))
+          (save-excursion
+            (ccide-apply-ed-diff replacements))))
+    nil))
+
+(defun ccide-project-load-config (&optional force)
+  (if (or force (buffer-file-name))
+      (let ((conf (ccide-project-search-upwards '(".project.el" "project.el") default-directory)))
         (when conf
           (set (make-local-variable 'ccide-project-root) (file-name-directory conf))
           (load-file conf)))))