1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#!/bin/bash
# Define paths and phrases
path="${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/dwm/thesiah"
readme_file="${XDG_DATA_HOME:-${HOME}/.local/share}/thesiah/thesiah.mom"
output_file="$path.mom"
temp_file_before="$path.tmp.before"
temp_file_after="$path.tmp.after"
start_phrase="When config.def.h in DWM and ST is saved, key bindings will be extracted and updated below."
end_phrase=".HEADING 2 \"Other buttons\""
[ -f "$path.mom" ] && source_file="$path.mom" || source_file="$path-default.mom"
# Extract the section before the end marker
sed "/$end_phrase/,\$d" "$source_file" >"$temp_file_before"
# Extract the section from the end marker to the end of the file
sed -n "/$end_phrase/,\$p" "$source_file" >"$temp_file_after"
# Remove the contents between the start and end phrases, including the markers themselves
sed -i "/$start_phrase/,/$end_phrase/d" "$temp_file_before"
# Verify that the section has been removed
echo "Section between markers removed. Check $temp_file_before for correctness."
# Re-append the start marker since it was deleted
echo "$start_phrase" >>"$temp_file_before"
# Define your configuration files
mapfile -t config_files <<EOF
${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/dwm/config.def.h
${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless/st/config.def.h
EOF
# Process each configuration file and append content
for file_path in "${config_files[@]}"; do
project_name=$(basename "$(dirname "$file_path")")
project_name=${project_name^^}
echo ".HEADING 3 \"$project_name\"" >>"$temp_file_before"
if [ "$project_name" == "DWM" ]; then
printf ".PP\nTHESIAH's window manager. I do not use a desktop environment.\n" >>"$temp_file_before"
elif [ "$project_name" == "ST" ]; then
printf ".PP\nTHESIAH's default terminal in C. It is light, configurable, and fast.\n" >>"$temp_file_before"
fi
echo ".LI" >>"$temp_file_before"
awk 'BEGIN {flag=0} /static[[:space:]]+(const[[:space:]]+)?(Keychord|Shortcut)[[:space:]]+(*keychords|shortcuts)[[:space:]]*\[\][[:space:]]*=[[:space:]]*{/ {flag=1} /\};/ {flag=0} flag' "$file_path" | while read -r line; do
if [[ "$line" =~ \/\*.*\*\/ || "$line" =~ .*\"\\.* || "$line" =~ ^$ || "$line" =~ STACKKEYS || "$line" =~ TAGKEYS || "$line" =~ static\ Keychord\ \*keychords || "$line" =~ static\ Shortcut\ \shortcuts || "$line" =~ ^\#.* ]]; then
continue
fi
if [[ "$line" =~ ^\/\/.* ]]; then
echo ".LIST OFF" >>"$temp_file_before"
line=$(echo "$line" | sed -e 's/\/\/\s*//g' | awk '{for (i=1; i<=NF; i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2)); print}')
output_line=".HEADING 5 \"$line\""
echo "$output_line" >>"$temp_file_before"
echo ".LI" >>"$temp_file_before"
continue
fi
if echo "$line" | grep -q "Keychord"; then
line=$(echo "$line" | sed -e "s/&((Keychord){[0-9], {{\(.*\)}),/\1/g;s/^0\(.*XF86XK_.*\)/MEDIA\1/g;/XF86XK_/!s/^0/Null/;s/\([MEDIA\|WIN\|ALT\|ULTRA\|EXTRA\|Shift\|Control]\w\+\),\s\(.*\)}},\s\+\(\w\+\),\s\([^,]*\)/\1|\2|\3|\4/g;s/},{0,//g;s/},{\(Shift\|Ctrl\),/\1/g;s/},{//g;s/Null//g")
else
line=$(echo "$line" | sed -e "s/^{ \(.*\) },/\1/g;s/{ \([^, ]*\), \([^, ]*\), \([^, ]*\), \([^}]*\) }/\1 \2 \3 \4/g;s/,/|/g")
fi
modkey=$(echo "$line" | awk -F'|' '{print $1}' | sed "s/\b0//g;
s/WINKEY/WIN/g;s/WINMODALL/WIN+Ctrl+Shift/g;s/WINMOD2/WIN+Ctrl/g;s/WINMOD/WIN+Shift/g;
s/ALTKEY/ALT/g;s/ALTMODALL/ALT+Ctrl+Shift/g;s/ALTMOD2/ALT+Ctrl/g;s/ALTMOD/ALT+Shift/g;
s/ULTRAKEY/WIN+ALT/g;s/ULTRAMODALL/WIN+ALT+Ctrl+Shift/g;s/ULTRAMOD2/WIN+ALT+Ctrl/g;s/ULTRAMOD/WIN+ALT+Shift/g;
s/EXTRAMOD/Ctrl+Shift/g;
s/\(Shift\|Control\)Mask/\1/g;
s/Control/Ctrl/g;
s/ //g;
s/XK_NO_MOD/NO_MOD/g;
s/XK_ANY_MOD/ANY_MOD/g")
key=$(echo "$line" | awk -F'|' '{print $2}' | sed "s/\b0//g;
s/XF86XK_//g;
s/XK_//g;
s/\s*\(.*\)_R$/Right_\1/g;
s/\s*\(.*\)_L$/Left_\1/g;
s/MODKEY\(\d*\)/MOD\1/g;
s/\(Shift\|Control\)Mask[?,]/\<\1\>/g;
s/Control/Ctrl/g;
s/space/<space>/g;
s/BackSpace/<backspace>/g;
s/apostrophe/\'/g;
s/comma/,/g;
s/bracketleft/[/g;
s/bracketright/]/g;
s/equal/=/g;
s/grave/\`/g;
s/minus/-/g;
s/period/./g;
s/slash/\//g;
s/semicolon/;/g;
s/|/+/g;
s/ //g")
func=$(echo "$line" | awk -F'|' '{print $3}' | grep -v "spawn" | sed "s/ //g")
args=$(echo "$line" | cut -d'|' -f4- | sed -E 's/.*\.v\s*=\s*\(const\s*char\s*\*\[\]\)\s*\{\s*([^}]*)\s*\}.*/\1/g;
s/.*\.v\s*=\s*\(int\s*\[\]\)\s*\{\s*([^}]*)\s*\}.*/\1/g;
s/.*SHCMD\((.*)\).*/\1/g;
s/.*\{\s*\.\w*\s*=\s*(.*)\s*\}.*/\1/g;
s/\{0\}//g;
s/\"//g;
s/\,//g;
s/NULL//g;
s/TERMINAL\s*-e\s*//g;
s/&layouts\[0\]/"[]="/g;
s/&layouts\[1\]/"[M]"/g;
s/&layouts\[2\]/"|||"/g;
s/&layouts\[3\]/"[@]"/g;
s/&layouts\[4\]/"[\\\\]"/g;
s/&layouts\[5\]/"H[]"/g;
s/&layouts\[6\]/"TTT"/g;
s/&layouts\[7\]/"==="/g;
s/&layouts\[8\]/"HHH"/g;
s/&layouts\[9\]/"###"/g;
s/&layouts\[10\]/"---"/g;
s/&layouts\[11\]/":::"/g;
s/&layouts\[12\]/"|M|"/g;
s/&layouts\[13\]/">M>"/g;
s/^\s*//g;
/^\s*0\s*$/d
')
# args=$(echo "$line" | cut -d',' -f4- | sed "s/\.v = (const char \*\[\])//g;s/{\s*\.v = (int \[\])//g;s/SHCMD(\(.*\))/\1/g;s/\.. = //g")
if [[ -z "$modkey" ]]; then
if [[ -z $func ]]; then
output_line="\\f(CW${key}\\fP \\(en ${args}"
else
output_line="\\f(CW${key}\\fP \\(en ${func} ${args}"
fi
else
if [[ -z $func ]]; then
output_line="\\f(CW${modkey}+${key}\\fP \\(en ${args}"
else
output_line="\\f(CW${modkey}+${key}\\fP \\(en ${func} ${args}"
fi
fi
echo ".ITEM" >>"$temp_file_before"
echo "$output_line" >>"$temp_file_before"
done
echo ".LIST OFF" >>"$temp_file_before"
done
# Append the latter part of the document that follows the end marker
cat "$temp_file_after" >>"$temp_file_before"
# Replace the original file with the updated content
mv -f "$temp_file_before" "$output_file"
cp -f "$output_file" "$readme_file"
# Clean up the temporary after-section file
rm -f "$temp_file_after"
notify-send "👍 Operation completed successfully."
|