C: Fix cc-format-with-uncrustify

master
Pierre Neidhardt 2018-11-17 14:01:29 +01:00
parent baea094673
commit 82cc40002a
1 changed files with 29 additions and 9 deletions

View File

@ -82,6 +82,7 @@ If the command is not `make', run it normally. "
(compile (format "%s -B" compile-command))
(recompile))))
;; TODO: See https://github.com/koko1000ban/emacs-uncrustify-mode.
(defun ambrevar/cc-format-with-uncrustify (&optional cfg-file start end)
"Run uncrustify(1) on current buffer or region."
(interactive "f\nr")
@ -90,19 +91,38 @@ If the command is not `make', run it normally. "
(point-min)))
(end (or (and (region-active-p) end)
(point-max)))
(formatbuf (get-buffer-create " *C format buffer*")))
(formatbuf (get-buffer-create " *C format buffer*"))
(stderr (make-temp-file "uncrustify")))
(setq status
(call-process-region start end "uncrustify" nil formatbuf nil "-lc" "-q" "-c"
(call-process-region start end
"uncrustify"
nil (list formatbuf stderr) nil
;; "-lc" ; Uncrustify should be able to auto-detect.
"-c"
(or cfg-file
(expand-file-name ".uncrustify.cfg" (getenv "HOME")))))
(if (/= status 0)
(error "error running uncrustify")
(let ((old-point (point)))
(let ((error-message
(with-temp-buffer
(insert-file-contents-literally stderr)
(buffer-string))))
(delete-file stderr)
(kill-buffer formatbuf)
(error "error running uncrustify: %s" error-message))
(delete-file stderr)
(let ((old-line (line-number-at-pos))
(old-column (current-column))
(old-window-start-line (- (line-number-at-pos)
(line-number-at-pos (window-start)))))
(save-mark-and-excursion
(delete-region (or start (point-min)) (or end (point-max)))
(insert-buffer-substring formatbuf))
(goto-char old-point))
(kill-buffer formatbuf))))
(goto-line old-line)
(move-to-column old-column)
(recenter old-window-start-line))
(kill-buffer formatbuf)))
;; Return nil if in a `write-file-functions'.
nil)
(defun ambrevar/cc-format-file-lookup ()
"Find .clang-format or .uncrustify.cfg in parent folder up to Git root.
@ -138,7 +158,7 @@ This is suitable for a `before-save-hook'."
(defun ambrevar/cc-turn-on-format ()
"Add `ambrevar/cc-format' to `before-save-hook' locally.
You can add your mode hook (e.g. `c-mode-hook')."
(add-hook 'before-save-hook 'ambrevar/cc-format nil 'local))
(add-hook 'write-file-functions 'ambrevar/cc-format nil 'local))
;;; GMP documentation
(with-eval-after-load "info-look"
@ -306,8 +326,8 @@ fprintf(stderr, \"\\n\"); \\
#include <unistd.h>
int main(int argc, char **argv) {" \n
> @ _ \n
> "return 0;
> @ _ \n
> "return 0;
}" \n)
(define-skeleton cc-usage-version