[libcode-tidyall-perl] 193/374: overhaul tidyall-buffer to work with stdin/stdout instead of with temporary files; allows it to work in narrowed region

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:17 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit 9f44c81fe32850e653ed67b634f391a0458d1787
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Thu Sep 6 12:56:23 2012 -0700

    overhaul tidyall-buffer to work with stdin/stdout instead of with temporary files; allows it to work in narrowed region
---
 etc/editors/tidyall.el |   79 +++++++++++++++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 31 deletions(-)

diff --git a/etc/editors/tidyall.el b/etc/editors/tidyall.el
index d672afc..ef3f7c9 100644
--- a/etc/editors/tidyall.el
+++ b/etc/editors/tidyall.el
@@ -22,10 +22,9 @@
 ;; This package implements a single function, tidyall-buffer, which
 ;; runs tidyall (https://metacpan.org/module/tidyall) on the current buffer.
 
-;; More specifically, the function runs tidyall on the current file, outputting
-;; its results to a temporary file; then replaces the contents of the current
-;; buffer with the contents of the temporary file and saves the buffer.
-;; Any modifications should be undoable.
+;; If successful, the contents of the buffer are replaced with the tidied contents, and
+;; the buffer is saved if tidyall-autosave is true.  The modifications should be
+;; undoable.
 
 ;; If tidyall generates any errors, the buffer is not changed, and a separate window
 ;; called *tidyall-output* is opened displaying the error.
@@ -50,37 +49,55 @@
 
 ;; The variable `tidyall-cmd` contains the path to the tidyall command.
 ;;
-
 (setq tidyall-cmd "tidyall")
 
+;; The variable `tidyall-autosave` indicates whether to save the buffer after a successful
+;; tidy - defaults to t
+;;
+(setq tidyall-autosave t)
+
 (defun tidyall-buffer ()
-  "Run tidyall on the current file."
+  "Run tidyall on the current buffer."
   (interactive)
   (let ((file (buffer-file-name)))
-    (cond (file
-           (if (buffer-modified-p)
-               (save-buffer))
-           (let* ((cmd (concat tidyall-cmd " --refresh-cache --output-suffix .tdy -m editor " file))
+    (cond ((null file)
+           (message "buffer has no filename"))
+          (t
+           (let* ((command (concat tidyall-cmd " -m editor --pipe " file))
                   (tidyall-buffer (get-buffer-create "*tidyall-output*"))
-                  (result (shell-command cmd tidyall-buffer))
-                  (tidied-file (concat file ".tdy"))
-                  (output (with-current-buffer tidyall-buffer (buffer-string)))
-                  (window-positions (mapcar (lambda (w) (window-start w)) (window-list)))
+                  (start (point-min))
+                  (end (point-max))
+                  (orig-window-start (window-start (selected-window)))
                   (orig-point (point)))
-             (when (string-match "[\t\n ]*$" output)
-               (replace-match "" nil nil output))
-             (cond ((zerop result)
-                    (cond ((string-match "\\[tidied\\]" output)
-                           (cond ((file-exists-p tidied-file)
-                                  (erase-buffer)
-                                  (insert-file-contents tidied-file)
-                                  (delete-file tidied-file)
-                                  (mapcar (lambda (w) (set-window-start w (pop window-positions))) (window-list))
-                                  (goto-char orig-point)
-                                  (save-buffer))
-                                 (t
-                                  (message (concat "Could not find '" tidied-file "'!")))))))
-                   (t
-                    (message nil)
-                    (split-window-vertically)
-                    (set-window-buffer (next-window) tidyall-buffer))))))))
+             (with-current-buffer tidyall-buffer (erase-buffer))
+             (let* ((result
+                     (call-process-region
+                      start end shell-file-name nil
+                      (list tidyall-buffer t) nil shell-command-switch command))
+                    (output (with-current-buffer tidyall-buffer (buffer-string))))
+               (cond ((zerop result)
+
+                      ;; Success. Replace content if it changed
+                      ;;
+                      (cond ((not (equal output (buffer-string)))
+                             (delete-region start end)
+                             (insert output)
+
+                             ;; Restore original window start and point as much as
+                             ;; possible. Go to beginning of line since we'll probably be
+                             ;; at a random point around our original line after the tidy.
+                             ;;
+                             (set-window-start (selected-window) orig-window-start)
+                             (goto-char orig-point)
+                             (beginning-of-line)
+                             (when tidyall-autosave
+                               (save-buffer))
+                             (message (concat "tidied " file)))
+                            (t
+                             (message (concat "checked " file)))))
+                     (t
+                      ;; Error. Display in other window
+                      ;;
+                      (when (< (length (window-list)) 2)
+                        (split-window-vertically))
+                      (set-window-buffer (next-window) tidyall-buffer)))))))))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list