Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 113 lines (95 sloc) 4.663 kB
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
1 ;;; tidyall.el -- Apply tidyall (https://metacpan.org/module/tidyall) to the current buffer
2
3 ;; Copyright (C) 2012 Jonathan Swartz
4
5 ;; Author: Jonathan Swartz <swartz@pobox.com>
6 ;; Keywords: extensions
7 ;; Status: Tested with Emacs 24.1.1
8
9 ;; This file is *NOT* part of GNU Emacs.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21
22 ;; This package implements a single function, tidyall-buffer, which
23 ;; runs tidyall (https://metacpan.org/module/tidyall) on the current buffer.
24
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
25 ;; If successful, the contents of the buffer are replaced with the tidied contents, and
26 ;; the buffer is saved if tidyall-autosave is true. The modifications should be
27 ;; undoable.
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
28
29 ;; If tidyall generates any errors, the buffer is not changed, and a separate window
30 ;; called *tidyall-output* is opened displaying the error.
31
77c3d21 @jonswar clarify
jonswar authored
32 ;; To operate on just a region of the buffer, use narrow-to-region.
33
df7b702 @jonswar fix comments
jonswar authored
34 ;; To assign this command to ctrl-t globally:
35 ;;
36 ;; (global-set-key "\C-t" 'tidyall-buffer)
37 ;;
38 ;; Or to assign it locally in, e.g., perl-mode:
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
39 ;;
40 ;; (setq perl-mode-hook
41 ;; '(lambda ()
42 ;; (local-set-key "\C-t" 'tidyall-buffer)
43 ;; ))
44 ;;
45 ;; (This replaces the default binding to transpose-chars, which I never use but ymmv.)
46
47 ;; The variable `tidyall-cmd` contains the path to the tidyall command.
48 ;;
49 (setq tidyall-cmd "tidyall")
50
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
51 ;; The variable `tidyall-autosave` indicates whether to save the buffer after a successful
52 ;; tidy - defaults to t
53 ;;
54 (setq tidyall-autosave t)
55
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
56 (defun tidyall-buffer ()
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
57 "Run tidyall on the current buffer."
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
58 (interactive)
59 (let ((file (buffer-file-name)))
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
60 (cond ((null file)
61 (message "buffer has no filename"))
62 (t
63 (let* ((command (concat tidyall-cmd " -m editor --pipe " file))
e8ce646 @jonswar adjust for pipe stdout passthrough; always save even when not changed
jonswar authored
64 (output-buffer (get-buffer-create "*tidyall-output*"))
65 (error-buffer (get-buffer-create "*tidyall-error*"))
66 (error-file (make-temp-file "tidyall_error"))
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
67 (start (point-min))
68 (end (point-max))
69 (orig-window-start (window-start (selected-window)))
1ca6127 @jonswar add etc/editors/tidyall.el
jonswar authored
70 (orig-point (point)))
e8ce646 @jonswar adjust for pipe stdout passthrough; always save even when not changed
jonswar authored
71 (with-current-buffer output-buffer (erase-buffer))
72 (with-current-buffer error-buffer (erase-buffer))
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
73 (let* ((result
74 (call-process-region
75 start end shell-file-name nil
e8ce646 @jonswar adjust for pipe stdout passthrough; always save even when not changed
jonswar authored
76 (list output-buffer error-file) nil shell-command-switch command))
77 (output (with-current-buffer output-buffer (buffer-string))))
78 (kill-buffer output-buffer)
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
79 (cond ((zerop result)
80
81 ;; Success. Replace content if it changed
82 ;;
83 (cond ((not (equal output (buffer-string)))
84 (delete-region start end)
85 (insert output)
86
87 ;; Restore original window start and point as much as
88 ;; possible. Go to beginning of line since we'll probably be
89 ;; at a random point around our original line after the tidy.
90 ;;
91 (set-window-start (selected-window) orig-window-start)
92 (goto-char orig-point)
93 (beginning-of-line)
94 (message (concat "tidied " file)))
95 (t
212116c @jonswar delete tidyall error buffer on success
jonswar authored
96 (message (concat "checked " file))))
e8ce646 @jonswar adjust for pipe stdout passthrough; always save even when not changed
jonswar authored
97 (when tidyall-autosave
98 (save-buffer))
99 (delete-windows-on error-buffer)
100 (kill-buffer error-buffer))
212116c @jonswar delete tidyall error buffer on success
jonswar authored
101
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
102 (t
103 ;; Error. Display in other window
104 ;;
e8ce646 @jonswar adjust for pipe stdout passthrough; always save even when not changed
jonswar authored
105 (with-current-buffer error-buffer
106 (insert-file-contents error-file))
9f44c81 @jonswar overhaul tidyall-buffer to work with stdin/stdout instead of with tem…
jonswar authored
107 (when (< (length (window-list)) 2)
108 (split-window-vertically))
315f86a emacs mode: clean up temp error-file.
George Hartzell authored
109 (set-window-buffer (next-window) error-buffer))))
110 (delete-file error-file))))))
88a6b64 emacs support: add a provide statement so require works.
George Hartzell authored
111
112 (provide 'tidyall)
Something went wrong with that request. Please try again.