~kris/dots

srice

ref: 178c9a25a484ead715392d50e21d03df486b8b41 srice/.config/.plan9/functions.rc -rw-r--r-- 10.7 KiB
178c9a25 — Kris Yotam conky: sync proper config from moirai (was the greenred variant) 2 months ago
                                                                                
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# Plan 9 rc shell functions

# ============================================================================
# acme
# ============================================================================

# B -- plumb files for editing
fn B {
	wdir=`{pwd}
	for(i in $*){
		if(! ~ $i /*)
			i=$wdir/$i
		plumb -s B -d edit $i
	}
}

# E -- edit file, wait for save
fn E {
	if(! ~ $#* 1){
		echo usage: E file >[1=2]
		exit usage
	}
	if(! test -e $1){
		echo E: $1: no such file >[1=2]
		exit 'no file'
	}
	otm=`{mtime $1 | awk '{print $1}'}
	B $1
	while(~ $otm `{mtime $1 | awk '{print $1}'})
		sleep 1
}

# awd -- update acme window label with cwd
fn awd {
	if(test -f /dev/label){
		p=`{pwd}
		echo -n $p/-$sysname > /dev/label >[2]/dev/null
	}
}

# ============================================================================
# dev
# ============================================================================

# b -- build, install, clean cycle
fn b {
	if(~ $#* 0)
		mk install && mk clean
	if not
		for(d in $*) @{
			cd $d
			mk install
			mk clean
		}
}

# bt -- build and test
fn bt {
	mk install && mk clean && {
		if(test -d test)
			mk test
		if not if(test -f test.rc)
			rc test.rc
	}
}

# fndef -- find C function definitions
fn fndef {
	grep -rn '^'$1'(' ${2:-.} | grep -v '/\*' | grep '{'
}

# callers -- find all call sites for a function
fn callers {
	grep -rn $1'(' ${2:-.} | grep -v '^[^:]*:[^:]*:.*^'$1
}

# hdr -- find which header declares a function
fn hdr {
	grep -rn $1 /sys/include /$objtype/include | head -10
}

# loc -- count lines of code
fn loc {
	dir=${1:-.}
	echo -n 'C: '; wc -l $dir/*.[ch] >[2]/dev/null | tail -1
	echo -n 'rc: '; wc -l $dir/*.rc >[2]/dev/null | tail -1
}

# longlines -- find lines over N columns
fn longlines {
	n=${1:-80}
	shift
	for(f in $*)
		awk 'length > '$n' {print FILENAME ":" NR ": " length " cols"}' $f
}

# cleanws -- strip trailing whitespace
fn cleanws {
	for(f in $*){
		sed 's/[ \t]*$//' $f >/tmp/cleanws.$pid
		if(! cmp -s $f /tmp/cleanws.$pid)
			cp /tmp/cleanws.$pid $f
		rm -f /tmp/cleanws.$pid
	}
}

# tabcheck -- find files using spaces for indentation
fn tabcheck {
	dir=${1:-.}
	grep -rn '^    [^ *]' $dir/*.[ch] | head -20
}

# watch -- poll for changes and rebuild
fn watch {
	last=''
	while(){
		sig=`{ls -l *.[ch] mkfile | md5sum}
		if(! ~ $"sig $"last){
			echo rebuilding...
			mk install >[2=1] || echo BUILD FAILED
			last=$sig
		}
		sleep 2
	}
}

# ============================================================================
# doc
# ============================================================================

# ms2pdf -- compile ms document to PDF
fn ms2pdf {
	if(~ $#* 0){
		echo 'usage: ms2pdf input.ms [output.pdf]' >[1=2]
		exit usage
	}
	input=$1
	if(~ $#* 1)
		output=`{echo $input | sed 's/\.ms$/.pdf/'}
	if not
		output=$2
	pic $input | tbl | eqn | troff -ms | dpost | ps2pdf >$output
}

# docview -- format and view a document
fn docview {
	pic $1 | tbl | eqn | troff -ms | page
}

# spellcheck -- check spelling in a troff document
fn spellcheck {
	deroff $* | spell
}

# spellctx -- misspelled words with context
fn spellctx {
	misspelled=`{deroff $1 | spell}
	for(word in $misspelled){
		echo '---' $word '---'
		grep -n $word $1
	}
}

# readlevel -- approximate reading level
fn readlevel {
	if(~ $#* 0)
		input=/fd/0
	if not
		input=$1
	deroff $input | awk '
	{
		for(i=1;i<=NF;i++){
			words++
			w=$i
			gsub(/[^aeiouAEIOU]/,"",w)
			if(length(w)==0) w="a"
			syls+=length(w)
		}
		n=gsub(/[.!?]/,"&")
		sents+=n
	}
	END{
		if(words==0||sents==0){print "insufficient text";exit}
		fk=0.39*(words/sents)+11.8*(syls/words)-15.59
		printf "Words: %d\n",words
		printf "Sentences: %d\n",sents
		printf "Avg words/sentence: %.1f\n",words/sents
		printf "Flesch-Kincaid Grade: %.1f\n",fk
	}'
}

# ============================================================================
# file
# ============================================================================

# lc -- columnar listing
fn lc {
	ls -pF $* | mc
}

# ff -- find files by pattern
fn ff {
	walk -f | grep $*
}

# rgrep -- recursive grep
fn rgrep {
	grep $* `{walk -f}
}

# bigfiles -- find files over 100MB
fn bigfiles {
	walk -f -esp | awk '$1>100000000'
}

# today -- files modified today
fn today {
	walk -emp | awk '$1>'^`{date -n}^'-(3600*12)'
}

# dircp -- copy directory tree
fn dircp {
	switch($#*){
	case 2
		@{builtin cd $1 && tar cif /fd/1 .} | @{builtin cd $2 && tar xTf /fd/0}
	case *
		echo usage: dircp from to >[1=2]
		exit usage
	}
}

# runeinfo -- show bytes vs runes for files
fn runeinfo {
	for(f in $*){
		bytes=`{wc -c <$f}
		runes=`{wc -r <$f}
		lines=`{wc -l <$f}
		echo $f: $bytes bytes, $runes runes, $lines lines
	}
}

# ============================================================================
# git
# ============================================================================

# smart git: no args = status
fn g {
	if(~ $#* 0)
		git status -sb
	if not
		git $*
}

# jump to git root
fn gr {
	cd `{git rev-parse --show-toplevel}
}

# today's commits
fn gtoday {
	git log --since=midnight --author=`{git config user.name} --oneline
}

# show remote url
fn gurl {
	git config --get remote.origin.url
}

# interactive branch checkout (requires fzf)
fn gbr {
	branch=`{git branch -a | sed 's/^..//' | fzf}
	if(! ~ $#branch 0)
		git checkout $branch
}

# WIP commit
fn gwip {
	git add -A
	git commit -m 'WIP: '^`{date +%H:%M}
}

# ============================================================================
# nav
# ============================================================================

# mkdir + cd
fn mkd {
	mkdir -p $1
	cd $1
}

# go up N directories
fn up {
	n=$1
	if(~ $#n 0) n=1
	d=''
	i=0
	while(test $i -lt $n){
		d=$d^../
		i=`{expr $i + 1}
	}
	cd $d
}

# go to src directory or subdirectory
fn src {
	if(~ $#* 0)
		cd $home/src
	if not
		cd $home/src/$1
}

# ============================================================================
# net
# ============================================================================

# doi2bib -- DOI to BibTeX
fn doi2bib {
	hget -r 'Accept: application/x-bibtex' 'https://doi.org/'^$1
}

# webpaste -- paste text to pastebin
fn webpaste {
	if(~ $#* 0)
		file=/fd/0
	if not
		file=$1
	hpost -u http://okturing.com -p / a_body@$file submit:submit fake:fake a_func:add_post url: | grep -e '/body"' | sed 1q | sed 's/^.*href="//g; s/body".*$/body/g'
}

# ============================================================================
# proc
# ============================================================================

# k -- kill processes by name (pipe output to rc to execute)
fn k {
	for(i in $*){
		ps | sed -n '/^'$user' .* '$i'$/s%[^ ]*  *%~>/proc/%
		s%  *.* (.*)%/note} # \1%
		s%~%@{echo kill%p'
	}
}

# broke -- find broken processes
fn broke {
	ps | sed -n '/^'$user' .* Broken/s%[^ ]*  *%~>/proc/%
	s%  *.* (.*)%/ctl # \1%
	s%~%echo kill%p'
}

# psu -- processes for current user
fn psu {
	ps | sed -n '/^'$user' /p'
}

# who -- list logged-in users
fn who {
	ps | sed '/Broken/d; /Exiting/d; s% .*$%%' | sort -u
}

# ============================================================================
# sam
# ============================================================================

# samrename -- rename variable safely (avoiding strings/comments)
fn samrename {
	if(~ $#* 0 1 2){
		echo 'usage: samrename old new file' >[1=2]
		exit usage
	}
	echo ',y/"[^"]*"/ y/''[^'']*''/ x/[a-zA-Z_0-9]+/ g/^'^$1^'$/ c/'^$2^'/
w
q' | sam -d $3
}

# samextract -- extract all matches of a pattern
fn samextract {
	echo ',x/'^$1^'/ p
q' | sam -d $2
}

# samindent -- indent every line by 4 spaces
fn samindent {
	ssam ',x/.*\n/ s/^/    /' <$1
}

# samjoin -- join continuation lines (backslash-newline)
fn samjoin {
	echo ',s/\\\n//g
w
q' | sam -d $1
}

# samtabs -- convert tabs to spaces
fn samtabs {
	echo ',s/\t/    /g
w
q' | sam -d $1
}

# samurls -- extract all URLs from a file
fn samurls {
	ssam -n ',x/https?:\/\/[^ \t\n<>"]+/ p' <$1
}

# samdups -- find doubled words
fn samdups {
	ssam -n ',x/\b([a-zA-Z]+)[ \n]+\1\b/ p' <$1
}

# ============================================================================
# text
# ============================================================================

# gsub -- global substitution across files
fn gsub {
	if(~ $#* 0 1 2){
		echo 'usage: gsub old new files...' >[1=2]
		exit usage
	}
	old=$1; new=$2; shift 2
	for(f in $*){
		echo ',s/'^$old^'/'^$new^'/g
w
q' | sam -d $f
	}
}

# delblank -- remove blank lines
fn delblank {
	echo ',x/\n\n+/c/\n/
w
q' | sam -d $1
}

# deltws -- delete trailing whitespace
fn deltws {
	echo ',x/[ \t]+\n/ c/\n/
w
q' | sam -d $1
}

# delcomments -- delete C-style block comments
fn delcomments {
	echo ',s,/\*[^*]*\*+([^/*][^*]*\*+)*/,,g
w
q' | sam -d $1
}

# keeplines -- keep only lines matching pattern
fn keeplines {
	echo ',x/.*\n/ v/'^$1^'/ d
w
q' | sam -d $2
}

# dellines -- delete lines matching pattern
fn dellines {
	echo ',x/.*\n/ g/'^$1^'/ d
w
q' | sam -d $2
}

# extractpara -- extract paragraphs containing a word
fn extractpara {
	echo ',x/(.+\n)+/ g/'^$1^'/ p
q' | sam -d $2
}

# revlines -- reverse line order
fn revlines {
	echo ',x/.*\n/ m0
w
q' | sam -d $1
}

# headings -- extract markdown headings
fn headings {
	echo ',x/^#+.*\n/ p
q' | sam -d $1
}

# wordfreq -- word frequency count
fn wordfreq {
	deroff $* | tr -cs 'a-zA-Z' '\n' | tr 'A-Z' 'a-z' | sort | uniq -c | sort -rn
}

# vocabsize -- count unique words
fn vocabsize {
	deroff $* | tr -cs 'a-zA-Z' '\n' | tr 'A-Z' 'a-z' | sort -u | wc -l
}

# sentlen -- sentence length distribution
fn sentlen {
	deroff $* | awk 'BEGIN{RS="[.!?]"} NF>0{print NF}' | sort -n | uniq -c | sort -rn
}

# mswc -- manuscript word count (troff-aware)
fn mswc {
	words=`{deroff $* | wc -w}
	pages=`{echo $words / 250 | bc}
	echo $words words, approximately $pages manuscript pages
}

# ============================================================================
# util
# ============================================================================

# extract archive (any format)
fn extract {
	if(! test -f $1){
		echo ''''^$1^'''' is not a file
		exit 'not a file'
	}
	switch($1){
	case *.tar.gz *.tgz
		tar xzf $1
	case *.tar.bz2 *.tbz2
		tar xjf $1
	case *.tar.xz *.txz
		tar xJf $1
	case *.tar
		tar xf $1
	case *.gz
		gunzip $1
	case *.bz2
		bunzip2 $1
	case *.xz
		xz -d $1
	case *.zip
		unzip $1
	case *.7z
		7z x $1
	case *
		echo 'cannot extract '^$1
	}
}

# file/dir size
fn fs {
	du -sh $1
}

# quick HTTP server
fn serve {
	port=$1
	if(~ $#port 0) port=8000
	echo 'Serving on http://localhost:'^$port
	python3 -m http.server $port --bind 127.0.0.1
}

# clipboard (cross-platform)
fn clip {
	if(whatis xclip >/dev/null >[2=1])
		xclip -selection clipboard
	if not if(whatis pbcopy >/dev/null >[2=1])
		pbcopy
	if not
		echo 'no clipboard tool'
}

# weather
fn wttr {
	curl -s 'wttr.in/'^$1^'?format=v2'
}

# command exists check
fn has {
	whatis $1 >/dev/null >[2=1]
}