Skip to content

Commit d3a6013

Browse files
committed
Add retab util to this branch
1 parent c9bd791 commit d3a6013

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ tags
4040
*.orig
4141
*.rej
4242
*~
43+
# Ignore backups of retabbed files
44+
*.notab

tools/dev/retab.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: binary -*-
3+
4+
# Replace leading tabs with 2-width spaces.
5+
# I'm sure there's a sed/awk/perl oneliner that's
6+
# a million times better but this is more readable for me.
7+
8+
require 'fileutils'
9+
require 'find'
10+
11+
dir = ARGV[0] || "."
12+
raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)
13+
14+
Find.find(dir) do |infile|
15+
next unless File.file? infile
16+
next unless infile =~ /rb$/
17+
outfile = infile
18+
backup = "#{infile}.notab"
19+
FileUtils.cp infile, backup
20+
21+
data = File.open(infile, "rb") {|f| f.read f.stat.size}
22+
fixed = []
23+
data.each_line do |line|
24+
fixed << line
25+
next unless line =~ /^\x09/
26+
index = []
27+
i = 0
28+
line.each_char do |char|
29+
break unless char =~ /[\x20\x09]/
30+
index << i if char == "\x09"
31+
i += 1
32+
end
33+
index.reverse.each do |idx|
34+
line[idx] = " "
35+
end
36+
fixed[-1] = line
37+
end
38+
39+
fh = File.open(outfile, "wb")
40+
fh.write fixed.join
41+
fh.close
42+
puts "Retabbed #{fh.path}"
43+
end

0 commit comments

Comments
 (0)