summaryrefslogtreecommitdiffstats
path: root/functions
blob: 8e66f39e3efe54b37f3140c99c1bd126468693c5 (plain)
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
pronounce(){
word="${@:-$(xclip -o)}"
if [[ ! -f $XDG_CACHE_HOME/pronounce/$word ]];then 
    wget -qO- $(wget -qO- "http://dictionary.reference.com/browse/$word" | grep 'soundUrl' | head -n 1 | sed 's|.*soundUrl=\([^&]*\)&.*|\1|' | sed 's/%3A/:/g;s/%2F/\//g') | tee $XDG_CACHE_HOME/pronounce/$word |  /usr/bin/mplayer -really-quiet -cache 8192 -  
else
    /usr/bin/mplayer -really-quiet  $XDG_CACHE_HOME/pronounce/$word
fi
}
function pal(){
	pacman -Ql $1 | /bin/grep -v "/usr/share/locale"
}

function translate(){
wget -qO - "http://ajax.googleapis.com/ajax/services/language/translate?langpair=|en&v=1.0&q=`xsel`" |cut -d \" -f 6
}


function clik(){
    scrot -s '%Y-%m-%d-%s_$wx$h.png' -e 'mv $f ~/.scrshots/; notify-send "Scrot" "$f done!"'
}

function mpl() 
{
$HOME/bin/mplayer "[email protected]"
}

function etym(){
word="[email protected]"
/usr/bin/w3m -dump "http://www.etymonline.com/index.php?search=$word&searchmode=none" | sed -n -e "/^$word/,/^$/p" 
}

function getmovie (){

    /usr/bin/get_movie.py `/usr/bin/search_movie.py "[email protected]" | head -3  | tail -1 | cut -d ":" -f 2`

}

function cnkt(){
export TERM=xterm
dtach -A $HOME/.cache/1080 -E ssh [email protected] -L 1080:socks.yahoo.com:1080
#tmux new-session -s cnkt ssh [email protected] -L 1080:socks.yahoo.com:1080
}

function wcnkt(){
#export TERM=xterm
dtach -A $HOME/.cache/wnohang -E ssh [email protected]
#tmux new-session -s cnkt ssh [email protected] -L 1080:socks.yahoo.com:1080
}

function randbash(){
curl -s http://bash.org/?random1|grep -oE "<p class=\"quote\">.*</p>.*</p>"|grep -oE "<p class=\"qt.*?</p>"|sed -e 's/<\/p>/\n/g' -e 's/<p class=\"qt\">//g' -e 's/<p class=\"qt\">//g'|perl -ne 'use HTML::Entities;print decode_entities($_),"\n"'| head -1  
}


function ifind(){
	find . -iname "*[email protected]*"
}

mountole(){

	keyctl list @u | grep -q b8d  || ecryptfs-add-passphrase #&>/dev/null
	keyctl list @u | grep -q b8d && mount | grep  yie | grep -v grep || mount -i /home/raghavendra/wormole
	#sudo sed -i -e '/.*ecryptfs.*/s|^/\(.*\)$|#&|g' /etc/fstab 1>/dev/null  
	if [[ $? = "0" ]];then
		echo "Done!"
	else
		echo "Failed :( "
		return 1
	fi
	ssh-expect 
}


geoip () { curl -s "http://www.geoiptool.com/?IP=$1" | html2text | egrep --color 'City:|IP Address:|Country:'; }

bbcradio() { local s;echo "Select a station:";select s in 1 1x 2 3 4 5 6 7 "Asian Network an" "Nations & Local lcl";do break;done;s=($s);/usr/bin/mplayer -playlist "http://www.bbc.co.uk/radio/listen/live/r"${s[@]: -1}".asx";}


function def(){
 wn "[email protected]" -over
 if [ $? != "0" ];then echo "[email protected]" >> ~/wordlist;fi               
}


function igrep(){
FILENAME=$1
PATTERN=$2
find . -iname "$FILENAME" -exec grep -il "$PATTERN" {} \;
}

function lookup(){
links -dump http://wordnet.princeton.edu/perl/webwn?s=$1 | grep -i --word-regexp --color=always -B 2 -A 5 $1 | sed '$d'
}


function stemp() { 
echo -n `acpi -t | awk -F ',' '{ print $2 }' | cut -d " " -f 2`
echo " `nvidia-smi -a |& grep Temp | cut -c17-19`"
echo `hddtemp /dev/sda`
 }



function rcstart(){
for d in [email protected];do
    if [[ -f /var/run/daemons/$d ]];then
        sudo /etc/rc.d/$d stop; sleep 2; sudo /etc/rc.d/$d start
    else
        sudo /etc/rc.d/$d start
    fi
done
}

function pkgsearch(){
#set -x
if [[ $#  < 1 ]];then
    echo "pkgsearch term pkglist"
    return 1
fi
term="$1"
shift
pkg="[email protected]"
for i in $pkg
do
 for j in `pacman -Ql $i | cut -d " " -f 2`
 do
	=grep -l  $term $j
 done
done
#set +x
}

#function {gdb,cgdb} {
#pid=$(pidof -s $1)
#/usr/bin/$0 -- --quiet --pid=$pid
#}

function genscope(){
	/usr/bin/cscope -b -R -q -k
}
function cscope(){
	#/usr/bin/cscope -d -f $(readlink -f (../)#cscope.out) [email protected]
	file=$(readlink -f (../)#cscope.out)
	[[ $? != 0 ]] && return 1
	pushd -q $file:h
	/usr/bin/cscope -d -p 4 [email protected]
}
function dmode(){
	if [[ $DMODE == 0 ]];then
		unset CC CXX
		export CFLAGS="-march=native -O0 -pipe -g -Wall"
		export CXXFLAGS="-march=native -O0 -pipe -g -Wall"
		unset LDFLAGS
		echo "Switching to debug mode"
	else 
		eval "export `grep -E \"^(CFLAG|CXX|LDF|MAKEF)\" /etc/makepkg.conf | tr '\n' ' '`"
		export CC=colorgcc
		echo "Switching to normal mode"
	fi
}
function cscore()
{
    curl -s -e "http://www.google.com" -A "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" "http://livechat.rediff.com:80/sports/score/score.txt"
}

function ckernel(){
	
	echo "
		file /home/raghavendra/Arch/qemu/vmlinux
		cd /home/raghavendra/Arch/Build/torvalds-git
		set architecture i386:x86-64:intel
		target remote /dev/pts/17
		"

}

function wpack(){
	pacman -Q | grep "[email protected]"
}

function max(){
	~/bin/mplayeraux "[email protected]"
}

function rsize() {
	wget --spider "[email protected]" -o /tmp/wget_tmp; grep -i Length /tmp/wget_tmp; rm /tmp/wget_tmp
}

function buildabs()
{
    ABDIR="$HOME/Arch/repo" 
    abpath=$(find -O3 $ABDIR -maxdepth 2 -name "[email protected]" -type d | head -1)
    [[ -z $abpath ]] && return 1
    cp -R $abpath /dev/shm/ || return 1
    pushd /dev/shm/$1/repos/*-x86_64
    #clear
}   

function vpdf(){
	pdftohtml -q -stdout -noframes "[email protected]" | /usr/bin/w3m -T text/html
}

function view()
{
if [[ -z "[email protected]" ]];then
    ranger ~/Documents
elif [[ $1 == http:* ]];then
    url=${1#*=}   # for urls like http://docs.google.com/viewer?url=<pdfurl>
    dest="$HOME/Documents/${1##*/}"
    wget -O - -q $url > $dest
    detach $VIEWER -r 143 $dest
else
    detach $VIEWER -r 143 "[email protected]" 
fi
}

function rcstop(){
for d in [email protected];do
    sudo /etc/rc.d/$d stop
done
}
gdb_get_backtrace() {
    local exe=$1
    local core=$2

    gdb ${exe} \
        --core ${core} \
        --batch \
        --quiet \
        -ex "thread apply all bt full" \
        -ex "quit"
}

function {sprunge,spaste}(){
    echo "sure y/N"
    read -q || return 1
	if [[ -z $1 ]];then
		if [[  -t 0 ]];then 
			url=$(xclip -o | curl -s -F 'sprunge=<-' http://sprunge.us)
		else 
			url=$(curl -s -F 'sprunge=<-' http://sprunge.us)
		fi
		echo $url 
	else
		for f in [email protected];do
			url=$(curl -s -F 'sprunge=<-' http://sprunge.us < $f)
			echo $url 
		done
	fi
	echo -n $url | xclip -i 
}



function whoru() { echo "Path:"`which [email protected]`; which =$1 |  xargs -i pacman -Qo {} ; }

function fdz() { ls -lth |  egrep "[0-9](M|G)" | sort -k 5 -n | cut -d " " -f 5,6-; }

function dz() { find . -maxdepth $1 -type d -exec du {} \; | sort -n | awk '{ print $2,"\t",$1/1024,"MB" }' | uniq; }
function psp() { ps auxww | /bin/grep -i "[email protected]"; }
function svim() {  sudo vim "[email protected]";}



define() {
  local LNG=$(echo $LANG | cut -d '_' -f 1)
  local CHARSET=$(echo $LANG | cut -d '.' -f 2)
  lynx -accept_all_cookies -dump -hiddenlinks=ignore -nonumbers -assume_charset="$CHARSET" -display_charset="$CHARSET"  "http://www.google.com/search?hl=${LNG}&q=define%3A+${1}&btnG=Google+Search" |  grep -m 5 -C 2 -A 5 -w "*" > /tmp/define

  if [ ! -s /tmp/define ]; then
    echo "Sorry, google doesn't know this one..."
    rm -f /tmp/define
    return 1
  else
      #mecho "$(cat /tmp/define | grep -v Search )" | colorit $1
      echo "$(cat /tmp/define | grep -v Search )" | colorit $1
    echo ""
  fi
  rm -f /tmp/define
  echo ">=========================>"
  caiksaurus $1 2>/dev/null
  echo $1 >> ~/wordlist
  return 0
}


function myip(){
upnpc -s | grep External | cut -d "=" -f 2
}

webman(){
	manum=$1
	term=${2:-$1}
	if [[ -z $2 ]];then
		output=(${(@)$(curl  -s -o /dev/null --write-out \
				"%{url_effective}:%{http_code}\n" \
				"http://linux.die.net/man/[1-9]/$term")}) && \
			url=$output[(r)*200]
		url=${url%:*}
		manum=$url[(ws:/:)-2]
	else
		output=$(curl  -s -o /dev/null --write-out \
				"%{url_effective}:%{http_code}\n" \
				"http://linux.die.net/man/$manum/$term")
		url="http://linux.die.net/man/$manum/$term"

	fi
	[[ -z $url ]] && return
	/usr/bin/w3m $url
	/usr/bin/w3m -dump -T text $url > ~/.helpers/man/man$manum/$term.$manum
}

man(){
	if [[ ! -z $2 || -z $TMUX ]];then /usr/bin/man "[email protected]" ; return; fi
	/usr/bin/man -w [email protected] &>/dev/null || return $?
    tmux split-window -p 90 "LC_ALL=C LANG=C exec /usr/bin/man [email protected]"
}


append(){
    echo "Enter stuff"
    cat >>$1
}


sendmail() {
    #"msmtp -C $HOME/.msmtprc"
    subject="$1"
    mailto=$2
    # It will ask for body -- much better
    /usr/bin/mutt -s "$subject" $mailto  # < "[email protected]"
}



tmpwrite(){
  $file = mktemp
  /usr/bin/vim $file
  mv $file $file.d
}

apod(){
    DATE=`date +%s`
    FILE="$HOME/.cache/apod/apod:$DATE.jpg"
    apodTemp=$(mktemp)
    curl -s http://apod.nasa.gov/apod/ >| $apodTemp
    apodDesc=$(perl -lne 'print $_ if /Explanation:/../Tomorrow/' $apodTemp | /usr/bin/w3m -dump -T text/html | head -n -1)
    url="$(grep IMG $apodTemp | cut -d "\"" -f 2)"
    wget -q -O "$FILE" -c http://apod.nasa.gov/apod/${=url}
    /usr/bin/perlbin/vendor/exiftool -overwrite_original -UserComment="$apodDesc" $FILE
    feh $FILE &
    echo $apodDesc | par f | gxmessage -file - #-timeout 30
    echo "Set as wallpaper: y/n"
    read -q  && set_wall $FILE
}

server(){
if [[ $1 == start ]];then
    python -m SimpleHTTPServer 9914 &>~/logs/server.log &
else
    $TBROWSER http://localhost:9914/
fi
}

loopit(){
 iso="$1"
 [[ -z $iso ]] && return 1
 mntpt=""
 for x in /mnt*;do
 	 if ! mountpoint -q $x;then
 	 	 mntpt=$x
 	 	 break
 	 fi
 done 
 if sudo mount -o loop $iso $mntpt;then
 	 echo $mntpt
 else 
 	 echo "Failed $mntpt" 
 	 return 1
 fi 
}

isurl(){
    url="[email protected]"
    curl -I -s "$url" &>/dev/null || return 1
    return 0
}

mecho(){
    #detect if being invoked through dmenu or terminal - use zenity or stdout
    #based on that
    if [[ -t 1 ]];then
        output="cat"
    else
        output="zenity --text-info --height 400 --width 680"
    fi
    echo "[email protected]" | ${=output}
}


task()
{
if [[ -z $1 || $1 == "add" ]];then
    echo "Name"
    read name
    file="$HOME/.tasks/${name// /-}"
    #touch $file
    echo "Description:"
    read desc
    echo "$desc" >! "${=file}.remain"
else
    echo "Task list"
    counter=1
    for i in $(print ~/.tasks/*.remain);do
        echo -e "\n\nTask #$counter: ${=i}"
        echo "Description:"
        cat "${=i}"
        echo -e "\n\n"
        echo "Is this task done?(y/N)"
        read -q && mv ${=i} ${=i:r}.done
        counter=$(($counter+1))
    done
fi

}

comcache(){
	if [[ $CC == clang || $CXX == clang++ ]];then
		CC="ccache clang -Qunused-arguments -fcolor-diagnostics"
		CXX="ccache clang++ -Qunused-arguments -fcolor-diagnostics"
	else
		[[ ! $CC == ccache* ]] && CC="ccache gcc"
		[[ ! $CXX == ccache* ]] && CXX="ccache g++"
	fi
	#projdir=$PWD
	#path=(/usr/lib/ccache/bin/ $path)
	export CC
	export CXX
	#export CCACHE_DIR="$HOME/.ccache/cache/$projdir"
}

ad2que(){
    trap 'break' INT
    local tset
    local input
    while :;do
        read $input
        if ! which =$input[(w)1] &>/dev/null;then
            input="shellrun $input"
        fi
        tset="$tset
        $input"
    done
    echo "$tset" >> ~/.dque
}


xpaste(){
    file="$1"
    [[ -z $file ]] &&  file="$HOME/note"
    var="$(xsel -o)"
	echo -e "${(qq)var}" >>| ~/.current  >>| $file
	notify-send "Added note" "${(qq)var}"
}

wiki() { w=$(dig +short txt "[email protected]".wp.dg.cx); echo "$w" }



dfilter(){
    # Should return array
    dchoice=$( eval [email protected] | awk '{ print NR,$0 }' | ${=DMENU} | awk '{ print $2 }')
    echo "$dchoice"

}

mangrep(){
    /usr/bin/man "$1" | grep -5 "$2"
}


help(){  run-help "[email protected]" || less -p "[email protected]" ~/.zshguide.txt }

disassemble() {
        emulate -L zsh
            gcc -pipe -S -o - -O -g $* | as -aldh -o /dev/null
        }

profile() {
        ZSH_PROFILE_RC=1 $SHELL "[email protected]"
}


xev(){
	=xev -id $(xdotool getactivewindow) | sed -ne '/^KeyPress/,/^$/p'
}


rsudo(){
echo "[email protected]" | sudo tee
}

search(){
    surfraw google "[email protected]"
}

ins(){
   if [[ $1 == *xz || $1 == *pkg.tar ]];then
      sudo pacman -U $1
   else
     sudo pacman -S "[email protected]"
 fi
}


extract() {
   if [[ -z "$1" ]] ; then
       print -P "usage: \e[1;36mextract\e[1;0m < filename >"
       print -P "       Extract the file specified based on the extension"
   elif [[ -f $1 ]] ; then
       case ${(L)1} in
           *.tar.bz2)  tar -jxvf $1    ;;
           *.tar.xz|*.xz)   tar -xvf $1    ;;
           *.tar.gz)   tar -zxvf $1    ;;
           *.bz2)      bunzip2 $1       ;;
           *.gz)       gunzip $1       ;;
           *.jar)      unzip $1       ;;
           *.rar)      unrar x $1       ;;
           *.tar)      tar -xvf $1       ;;
           *.tbz2)     tar -jxvf $1    ;;
           *.tgz)      tar -zxvf $1    ;;
           *.zip)      unzip $1          ;;
           *.Z)        uncompress $1    ;;
           *)          echo "Unable to extract '$1' :: Unknown extension"
       esac
   else
       echo "File ('$1') does not exist!"
   fi

}

pacnews()
{
    for n in /etc/**/*.pacnew; do 
        if [[ -r ${n%.*} ]]; then 
            sudo vimdiff $n ${n%.*} &&
            echo "Replace?"; read -q && sudo mv $n ${n%.*}
        fi 
    done
}

dlink(){
	curl -s "[email protected]" | /usr/bin/w3m -dump -T text/html
}

#alias help-zshglob=H-Glob
function lsnew () {
    if [[ $1 = "" ]]; then
        lsnew_glob="*"
    else
        lsnew_glob=$1
    fi
    ls -tr -dl $~lsnew_glob(oc[1,30])
}
alias lsnew='noglob lsnew'



#function {news,mutt,ncm,vim,weechat,weechat-curses,newsbeuter,ncmpcpp,atop,rtorrent,torrent} {
function {news,mutt,ncm,weechat,weechat-curses,newsbeuter,ncmpcpp,atop,rtorrent,torrent} {
	: ${(AA)muxaliases:=${(z)MUXAL}}
    if [[ -z $TMUX || $1 == -f || -n $1 ]];then
    	[[ $1 == -f ]] && shift
    	if test $muxaliases[$0];then
    		=$muxaliases[$0] "[email protected]"
    	else
    		=$0 "[email protected]"
    	fi
    	return
    fi
	app=$0

	if [[ $app == vim ]];then
		vimote tabnew [email protected]
		return
	fi

	: ${(AA)appMap:=${(z)WSETS}}

	if pgrep $app;then
		sess=$(tux takeme $app)
		tmux switch-client -t $sess
		return
	fi

	winum=$(~/bin/tux tnum $app)

	if [[ -z $winum || $? != 0  ]];then
		#print "$0 = Dead | Something ==> recreated in its session"
		return 5
	fi

    tmux join-pane -s $appMap[$app]:$winum -p 80 
    if [[ $? -ne 0 ]];then
    	 $0 -f "[email protected]"
	fi

#    if [[ -n $1 ]];then
#		$muxaliases[$app] [email protected] 
#	fi
}
function recflag(){
	eval "export `grep -E \"^(CFLAG|CXX|LDF|MAKEF)\" /etc/makepkg.conf | tr '\n' ' '`"
}

function btime(){
	# Needs some work
	stat -f $1 | /bin/grep -q ext || return 1
	mntdir=$(stat --format %m $1)
	mnt=$(grep $mntdir /proc/mounts | awk '{ print $1 }')
	dir=$(sed  -e "s#$mntdir/\(.*\)#\1#" <<< $PWD)
	debugfs -R "stat $dir/$1" $mnt |& /bin/grep crtime
}

function {reboot,sdown} {
    typeset -A map
    map=(sdown '/sbin/shutdown -h now' reboot 'command reboot')
    command=$0
    echo "Sure y/N"
    if read -q;then
    	~/bin/down 
    	eval "$=map[$command]"
    fi
}
startx(){
	bfs=$(dmesg | /bin/grep BFS)
	if [[ -n $bfs ]];then
		schedtool -I -e  /usr/bin/startx -- [email protected] &>|~/logs/X.log
	else
		/usr/bin/startx -- [email protected] &>|~/logs/X.log
	fi
}

viewjson(){
	python -mjson.tool < $1
}

pacsync(){
	sudo pacman -Sy
}

update(){
	sudo pacman -Su
}

slob(){
	if [[ $# > 1 ]];then
		pattern=${(pj:|:)@}
		pattern="*.($pattern)(Oc[1,-1])"
		eval print -lc $pattern
	else
		print -lc *.$1(Oc[1,-1])
	fi
}

viewlite(){
	sqlite3 $1 .dump
}
function isitdown(){
	dlink http://www.downforeveryoneorjustme.com/$1  | /bin/grep "you"
}
# vim: set ft=sh ts=4 sw=4 foldmethod=syntax tw=80 noet: