diff --git a/gnu-indent.el b/gnu-indent.el index 1a37851e96..8aea4161dc 100644 --- a/gnu-indent.el +++ b/gnu-indent.el @@ -51,18 +51,17 @@ ;; Autoload so that users can set it as file local variable without ;; warning. ;;;###autoload -(progn - (defcustom gnu-indent-options nil - "Arguments to pass to GNU Indent." - :type '(repeat string) - :safe (lambda (val) - (let ((valid t)) - (while (and valid val) - (unless (stringp (car val)) - (setq valid nil)) - (setq val (cdr val))) - valid)) - :group 'gnu-indent)) +(defcustom gnu-indent-options nil + "Arguments to pass to GNU Indent." + :type '(repeat string) + :safe (lambda (val) + (let ((valid t)) + (while (and valid val) + (unless (stringp (car val)) + (setq valid nil)) + (setq val (cdr val))) + valid)) + :group 'gnu-indent) ;;;###autoload (defun gnu-indent-region (beg end) @@ -88,14 +87,15 @@ When called non-interactively, indent text between BEG and END." (send-region process beg end) (process-send-eof process) (redisplay) - (while (process-live-p process) - (sleep-for 0.01)) + (while (accept-process-output process nil 10)) (unless (eq (process-exit-status process) 0) - (display-buffer (process-buffer process)) + (pop-to-buffer (process-buffer process)) (error "GNU Indent exited with non-zero status")) (save-restriction (let ((inhibit-read-only t)) (narrow-to-region beg end) + ;; Perhaps something should be done to try an preserve + ;; the point after indentation? (insert-file-contents temp-file nil nil nil t)))) (delete-file temp-file))) @@ -108,11 +108,24 @@ When called non-interactively, indent text between BEG and END." (interactive) (gnu-indent-region (point-min) (point-max))) +;; A little suggestion +;;;###autoload +(defun gnu-indent-defun-or-fill (arg) + "Indent current function with GNU Indent. +If point is in a comment, call `fill-paragraph' instead. A +prefix argument ARG is passed to `fill-paragraph'." + (interactive "P") + (if (nth 8 (syntax-ppss)) ;if in a comment + (fill-paragraph arg) + (let ((bounds (bounds-of-thing-at-point 'defun))) + (if (consp bounds) + (gnu-indent-region (car bounds) (cdr bounds)) + (user-error "No defun at point"))))) + ;;;###autoload (define-minor-mode gnu-indent-mode "Indent buffer automatically with GNU Indent." :lighter " GNU-Indent" - :keymap nil (if gnu-indent-mode (add-hook 'before-save-hook #'gnu-indent-buffer nil t) (remove-hook 'before-save-hook #'gnu-indent-buffer t)))