The section "19) Editor modelines and other cruft" in
Documentation/process/coding-style.rst clearly says,
"Do not include any of these in source files."
I recently receive a patch to explicitly add a new one.
Let's do treewide cleanups, otherwise some people follow the existing
code and attempt to upstream their favoriate editor setups.
It is even nicer if scripts/checkpatch.pl can check it.
If we like to impose coding style in an editor-independent manner,
I think editorconfig (patch [1]) is a saner solution.
[1] https://lore.kernel.org/lkml/20200703073143.423557-1-da...@kdrag0n.dev/
Signed-off-by: Masahiro Yamada
---
You might wonder if I worked on this huge patch manually or generated it by
a tool. I wrote a Python script to generate this patch, but not from a scratch.
I contributed a similar tool to U-Boot some years ago.
(https://github.com/u-boot/u-boot/commit/8ba1f5de4571566be12efaffdad404a506b978e3)
I reused and modified it.
For completeness, this is the code I wrote (ugly since this is not intended for
upstream)
#!/usr/bin/env python3
import copy
import os
import re
import sys
pattern_local_variables = re.compile(r'Local (V|v)ariables')
pattern_end = re.compile(r'End:')
pattern_blank = re.compile(r'^\s*$')# empty line
pattern_comment_begin = re.compile(r'^(\s*)/\*')
pattern_comment_end = re.compile(r'\*/$')
pattern_comment_null = re.compile(r' \*$')
pattern_dash = re.compile(r'---')
def extend_matched_lines(lines, matched, pre_patterns, post_patterns,
extend_pre,
extend_post):
extended_matched = []
j = matched[0]
for i in matched:
if i == 0 or i < j:
continue
j = i
while j in matched:
j += 1
if j >= len(lines):
break
for p in pre_patterns:
if p.search(lines[i - 1]):
break
else:
# not matched
continue
for p in post_patterns:
if p.search(lines[j]):
break
else:
# not matched
continue
if extend_pre:
extended_matched.append(i - 1)
if extend_post:
extended_matched.append(j)
matched += extended_matched
matched.sort()
def cleanup_one_file(filepath, patterns):
#print(filepath)
with open(filepath) as f:
lines = f.readlines()
matched = []
for i, line in enumerate(lines):
if i - 1 in matched and lines[i - 1][-2:] == '\\\n':
matched.append(i)
continue
for pattern in patterns:
if pattern.search(line):
#print("hit {}".format(line), end='')
m = pattern_comment_begin.match(line)
if m and not pattern_comment_end.search(line):
#print("replace {}".format(line), end='')
lines[i] = m.group(1) + '/*\n'
if i + 1 < len(lines) and
pattern_comment_end.search(lines[i + 1]):
matched.append(i)
matched.append(i + 1)
else:
matched.append(i)
break
if not matched:
return
while True:
old_matched = copy.copy(matched)
extend_matched_lines(lines, matched, [pattern_local_variables],
[pattern_end], True, True)
extend_matched_lines(lines, matched, [pattern_comment_begin],
[pattern_comment_end], True, True)
extend_matched_lines(lines, matched, [pattern_blank, pattern_dash],
[pattern_comment_end], True, False)
extend_matched_lines(lines, matched, [pattern_comment_begin,
pattern_comment_null],
[pattern_comment_null], False, True)
extend_matched_lines(lines, matched, [pattern_blank],
[pattern_blank], False, True)
if matched == old_matched:
break
# remove blank lines at the end of file
if matched and matched[-1] == len(lines) -1:
i = matched[-1] - 1
while i >= 0 and (i in matched or pattern_blank.search(lines[i])):
matched.append(i)
i -= 1
matched.sort()
with open(filepath, 'w') as f:
for i, line in enumerate(lines):
if i not in matched:
f.write(line)
def main():
cwd = os.getcwd()
if len(sys.argv) > 1:
topdir = os.path.join(cwd, sys.argv[1])
else:
topdir = cwd
exclude_dirs = [ os.path.join(cwd, d) for d in ('.git', 'Documentation') ]
# patterns to remove
strings = ('c-indent-level:', 'tab-width:',
'vim:', 'version-control:', 'c-basic-offset:',
'indent-tabs-mode:', 'c-file-style:', 'fill-column:',
'kept-new-versions:',
'ispell-local-dictionary', r'mode: (C|c)$', 'Emacs about
preferred coding style',