Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/emacs --script |
| 2 | |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 3 | ;; Insert style boilerplate if it's not already there |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 4 | ;; |
| 5 | ;; Breaking the string in half keeps emacs |
| 6 | ;; from trying to interpret the local variable |
| 7 | ;; settings e.g. when it reads the lisp source code |
| 8 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 9 | (defun insert-style-boilerplate () (interactive) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 10 | (save-excursion |
| 11 | (goto-char (point-min)) |
| 12 | (if (eq nil (search-forward "coding-style-patch-verification" |
| 13 | (point-max) t)) |
| 14 | (let ((junk 0)) (goto-char (point-max)) |
| 15 | (insert " |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 16 | /* |
| 17 | * fd.io coding-style-patch-verification: ON |
| 18 | * |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 19 | * Local Var" "iables: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 20 | * eval: (c-set-style \"gnu\") |
| 21 | * End: |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 22 | */"))))) |
| 23 | |
| 24 | ;; (cons xxx <list>) means insert xxx at the head of <list> |
| 25 | ;; Build a sorted list of *INDENT-OFF* lines, by searching |
| 26 | ;; backwards. The initial (setq indent-offset-list nil) |
| 27 | ;; results in (cdr <last-cell>) nil, which makes it a proper list |
| 28 | |
| 29 | (defun find-indent-offs () (interactive) |
| 30 | (save-excursion |
| 31 | (if (boundp 'indent-offset-list) |
| 32 | (makunbound 'indent-offset-list)) |
| 33 | (setq indent-offset-list nil) |
| 34 | (goto-char (point-max)) |
| 35 | (while (search-backward "*INDENT-OFF*" (point-min) t) |
| 36 | (move-beginning-of-line nil) |
| 37 | (setq indent-offset-list (cons (point) indent-offset-list)) |
| 38 | (previous-line)))) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 39 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 40 | ;; Insert indent-off ... indent-on brackets around |
| 41 | ;; a certain xxx_foreach macro, etc. which "indent" |
| 42 | ;; completely screws up. Doesn't handle nesting, of which there |
| 43 | ;; are few examples (fortunately). |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 44 | |
| 45 | (defun fix-initializer (what) (interactive) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 46 | (find-indent-offs) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 47 | (save-excursion |
| 48 | (goto-char (point-min)) |
| 49 | (while (search-forward-regexp what (point-max) t) |
| 50 | (move-beginning-of-line nil) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 51 | (previous-line) |
| 52 | (let ((index 0)(pointval 0)) |
| 53 | (while (and (< pointval (point))(elt indent-offset-list index)) |
| 54 | (setq pointval (elt indent-offset-list index)) |
| 55 | (setq index (1+ index))) |
| 56 | (if (not (eq pointval (point))) |
| 57 | (let ((junk 0)) |
| 58 | (next-line) |
| 59 | (open-line 1) |
| 60 | (c-indent-line-or-region) |
| 61 | (insert "/* *INDENT-OFF* */") |
| 62 | (search-forward "{") |
| 63 | (backward-char) |
| 64 | (forward-sexp) |
| 65 | (move-end-of-line nil) |
| 66 | (newline 1) |
| 67 | (c-indent-line-or-region) |
| 68 | (insert "/* *INDENT-ON* */") |
| 69 | (find-indent-offs)) |
| 70 | (search-forward "*INDENT-ON*")))))) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 71 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 72 | (defun fix-pool-foreach () (interactive) |
| 73 | (fix-initializer "pool_foreach *(")) |
| 74 | |
Keith Burns (alagalah) | 5f3ca64 | 2016-08-06 11:03:59 -0700 | [diff] [blame] | 75 | (defun fix-pool-foreach-index () (interactive) |
| 76 | (fix-initializer "pool_foreach_index *(")) |
| 77 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 78 | (defun fix-hash-foreach () (interactive) |
| 79 | (fix-initializer "hash_foreach *(")) |
| 80 | |
| 81 | (defun fix-hash-foreach-pair () (interactive) |
| 82 | (fix-initializer "hash_foreach_pair *(")) |
| 83 | |
Keith Burns (alagalah) | 114e8a9 | 2016-08-04 09:27:23 -0700 | [diff] [blame] | 84 | (defun fix-hash-foreach-mem () (interactive) |
| 85 | (fix-initializer "hash_foreach_mem *(")) |
| 86 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 87 | (defun fix-clib-fifo-foreach () (interactive) |
| 88 | (fix-initializer "clib_fifo_foreach *(")) |
| 89 | |
| 90 | (defun fix-clib-bitmap-foreach () (interactive) |
| 91 | (fix-initializer "clib_bitmap_foreach *(")) |
| 92 | |
| 93 | (defun fix-foreach-ip-interface-address () (interactive) |
| 94 | (fix-initializer "foreach_ip_interface_address *(")) |
| 95 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 96 | (defun fix-vlib-register-thread () (interactive) |
| 97 | (fix-initializer "VLIB_REGISTER_THREAD *(")) |
| 98 | |
| 99 | (defun fix-vlib-cli-command () (interactive) |
| 100 | (fix-initializer "VLIB_CLI_COMMAND *(")) |
| 101 | |
| 102 | (defun fix-vlib-register-node () (interactive) |
| 103 | (fix-initializer "VLIB_REGISTER_NODE *(")) |
| 104 | |
Dave Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 105 | (defun fix-reply-macro2 () (interactive) |
| 106 | (fix-initializer "REPLY_MACRO2 *(")) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 107 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 108 | (defun fix-vnet-device-class () (interactive) |
| 109 | (fix-initializer "VNET_DEVICE_CLASS *(")) |
| 110 | |
| 111 | (defun fix-vnet-hw-interface-class () (interactive) |
| 112 | (fix-initializer "VNET_HW_INTERFACE_CLASS *(")) |
| 113 | |
Dave Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 114 | (defun fix-clib-packed () (interactive) |
| 115 | (fix-initializer "CLIB_PACKED *(")) |
Ed Warnicke | c1be59d | 2016-08-11 13:30:57 -0700 | [diff] [blame] | 116 | (defun fix-vl-api-packed () (interactive) |
| 117 | (fix-initializer "VL_API_PACKED *(")) |
Dave Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 118 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 119 | ;; Driver routine which runs the set of functions |
| 120 | ;; defined above, as well as the bottom boilerplate function |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 121 | |
| 122 | (defun fd-io-styleify () (interactive) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 123 | (fix-pool-foreach) |
Keith Burns (alagalah) | 5f3ca64 | 2016-08-06 11:03:59 -0700 | [diff] [blame] | 124 | (fix-pool-foreach-index) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 125 | (fix-hash-foreach) |
| 126 | (fix-hash-foreach-pair) |
Keith Burns (alagalah) | 114e8a9 | 2016-08-04 09:27:23 -0700 | [diff] [blame] | 127 | (fix-hash-foreach-mem) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 128 | (fix-foreach-ip-interface-address) |
| 129 | (fix-clib-fifo-foreach) |
| 130 | (fix-clib-bitmap-foreach) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 131 | (fix-vlib-register-thread) |
| 132 | (fix-vlib-cli-command) |
| 133 | (fix-vlib-register-node) |
Dave Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 134 | (fix-reply-macro2) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 135 | (fix-vnet-device-class) |
| 136 | (fix-vnet-hw-interface-class) |
Dave Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 137 | (fix-clib-packed) |
Ed Warnicke | c1be59d | 2016-08-11 13:30:57 -0700 | [diff] [blame] | 138 | (fix-vl-api-packed) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 139 | (insert-style-boilerplate) |
| 140 | (if (boundp 'indent-offset-list) |
| 141 | (makunbound 'indent-offset-list))) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 142 | |
| 143 | ;; When run as a script, this sexp |
| 144 | ;; walks the list of files supplied on the command line. |
| 145 | ;; |
| 146 | ;; (elt argv index) returns nil if you M-x eval-buffer |
| 147 | ;; or M-x load-file the file, so we won't accidentally |
| 148 | ;; evaluate (save-buffers-kill-emacs)... |
| 149 | |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 150 | (let ((file-index 0)) |
| 151 | (if (elt argv file-index) |
| 152 | (while (elt argv file-index) |
| 153 | (find-file (elt argv file-index)) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 154 | (fd-io-styleify) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 155 | (message "Done %s..." (elt argv file-index)) |
| 156 | (setq file-index (1+ file-index)))) |
| 157 | (if (> file-index 0) |
| 158 | (let ((junk 0)) |
| 159 | (message "Save and quit...") |
| 160 | (save-buffers-kill-emacs t)))) |
Dave Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 161 | |
| 162 | |