xref: /freebsd/crypto/krb5/src/util/krb5-batch-reindent.el (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1;;; -*- mode: emacs-lisp; indent-tabs-mode: nil -*-
2(if (not noninteractive)
3    (error "to be used only with -batch"))
4;; Avoid vc-mode interference.
5(setq vc-handled-backends nil)
6
7;; for debugging
8(defun report-tabs ()
9  (let ((tab-found (search-forward "\t" nil t)))
10    (if tab-found
11        (message "Tab found @%s." tab-found)
12      (message "No tabs found."))))
13
14(defun whitespace-new ()
15    ;; Sometimes whitespace-cleanup gets its internals confused
16    ;; when whitespace-mode hasn't been activated on the buffer.
17    (let ((whitespace-indent-tabs-mode indent-tabs-mode)
18          (whitespace-style '(empty trailing)))
19      ;; Only clean up tab issues if indent-tabs-mode is explicitly
20      ;; set in the file local variables.
21      (if (local-variable-p 'indent-tabs-mode)
22          (progn
23            (message "Enabling tab cleanups.")
24            (add-to-list 'whitespace-style 'indentation)
25            (add-to-list 'whitespace-style 'space-before-tab)
26            (add-to-list 'whitespace-style 'space-after-tab)))
27;;      (message "indent-tabs-mode=%s" indent-tabs-mode)
28      (message "Cleaning whitespace...")
29      (whitespace-cleanup)))
30
31;; Old style whitespace.el uses different variables.
32(defun whitespace-old ()
33  (let (whitespace-check-buffer-indent
34        whitespace-check-buffer-spacetab)
35    (if (local-variable-p 'indent-tabs-mode)
36        (progn
37          (message "Enabling tab cleanups.")
38          (setq whitespace-check-buffer-indent indent-tabs-mode)
39          (setq whitespace-check-buffer-spacetab t)))
40    (message "Cleaning whitespace...")
41    (whitespace-cleanup)))
42
43(while command-line-args-left
44  (let ((filename (car command-line-args-left))
45        ;; No backup files; we have version control.
46        (make-backup-files nil))
47    (find-file filename)
48    (message "Read %s." filename)
49
50    (if (not indent-tabs-mode)
51        (progn
52          (message "Untabifying...")
53          (untabify (point-min) (point-max))))
54
55    ;; Only reindent if the file C style is guessed to be "krb5".
56    ;; Note that krb5-c-style.el already has a heuristic for setting
57    ;; the C style if the file has "c-basic-offset: 4;
58    ;; indent-tabs-mode: nil".
59    (if (equal c-indentation-style "krb5")
60        (c-indent-region (point-min) (point-max)))
61
62    (if (fboundp 'whitespace-newline-mode)
63        (whitespace-new)
64      (whitespace-old))
65
66    (save-buffer)
67    (kill-buffer nil)
68    (setq command-line-args-left (cdr command-line-args-left))))
69