root/closebracket/closebracket.conf

Revision 1357, 12.2 kB (checked in by alpt, 1 year ago)

Online help added: ] --help, ][ --help

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/bin/bash
2 #
3 # This source code is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 2 of the License,
6 # or (at your option) any later version.
7 #
8 # (c) Copyright 2006 AlpT (@freaknet.org)
9
10 # To add your actions see what's already here and copy the style ;)
11 #
12 # The `CB_ACTION' array is scanned by `]', while `CB_ACTION_II' by `]['
13 # Note that the order of each CB_ACTION[] is relevant.
14 #
15 # If your function deals with filename, remember to use "$@" and not simply $@.
16 # Doing so you will support filenames with spaces too.
17 #       
18 # That's all, have fun
19
20 ################### CONFIGURE HERE #################
21 SHELL_LIST_FILE="$CLOSEBRACKET_DIR/shells"
22 FILE_EXTENSIONS_LIST="$CLOSEBRACKET_DIR/file_extensions"
23
24 CB_EDITOR='vim'
25 CB_BROWSER='firefox'
26 CB_HTTP_DOWNLOADER='wget'
27 CB_TEXT_BROWSER='links'
28 CB_IMAGE_VIEWER='gqview'
29 CB_IMAGE_TTY_VIEWER='seejpeg'
30 CB_IMAGE_EDITOR='gimp'
31 CB_MEDIA_PLAYER='mplayer'
32 CB_CALC='bc -l'
33 CB_MAIL_AGENT='mutt'
34
35 CB_REMOTE_COPY='scp'
36 CB_REMOTE_COPY_DIR='scp -r'
37 CB_REMOTE_COPY_PREFIX='scp://'
38 #CB_REMOTE_COPY='rsync'
39 #CB_REMOTE_COPY_DIR='rsync -a'
40 #CB_REMOTE_COPY_PREFIX='rsync://'
41
42 CB_BITTORRENT='bittorrent'
43 CB_BITTORRENT_TTY='bittorrent-curses'
44
45 CB_PDF_VIEWER='xpdf_hack' # see xpdf_hack() below
46
47 CB_SHELL_GREP="$CLOSEBRACKET_DIR/scripts/shell"
48 CB_UNTARRA="$CLOSEBRACKET_DIR/scripts/untarra"
49
50 ################### CONFIGURE END #################
51
52
53 CB_OK=79
54 CB_SKIP=0
55 cbi=0
56
57 ##
58 ### Filters based on regular expressions
59 ##
60
61 ((cbi++))
62 CB_ACTION[$cbi]="cb_http_browser"       # open URI with browser
63 CB_ACTION_II[$cbi]="cb_http_wget"       # download URI with wget
64
65 ((cbi++))
66 CB_ACTION[$cbi]="cb_remote_edit"        # ] remoteshell:file
67 CB_ACTION_II[$cbi]="cb_remote_copy"     # scp
68
69 ((cbi++))
70 CB_ACTION[$cbi]="cb_file_colon_line"
71 CB_ACTION_II[$cbi]="cb_donothing"
72
73
74 # Open mutt to send a new mail when "$1" is a mail address
75 ((cbi++))
76 CB_ACTION[$cbi]="cb_mail_agent"
77 CB_ACTION_II[$cbi]="cb_donothing"
78 CB_ACTION_II[$cbi]="cb_donothing"
79
80 #((cbi++))
81 #CB_ACTION[$cbi]="cb_calc"              # Use bc -l: ] 3+3.14*(21^2)
82 #CB_ACTION_II[$cbi]="cb_donothing"
83
84
85 ##
86 ### Filters based on file type
87 ##
88
89 # File extensions match
90 ((cbi++))
91 CB_ACTION[$cbi]="cb_file_exts"
92 CB_ACTION_II[$cbi]="cb_file_exts_II"
93
94 ##
95 ### General filters
96 ##
97
98 ## When we have no argument
99 ((cbi++))
100 CB_ACTION[$cbi]="cb_list_dir"   # do `ls' when "$1" is null
101 CB_ACTION_II[$cbi]="cb_cdhome"  # go $HOME/
102
103 ## When the first argument is a directory
104 ((cbi++))
105 CB_ACTION[$cbi]="cb_cdcd"       # change dir if "$1" is a dir
106 CB_ACTION_II[$cbi]="cb_lsdir"   # ls dir/
107
108 # Vim
109 ((cbi++))
110 CB_ACTION[$cbi]="cb_vim"        # vim file
111 CB_ACTION_II[$cbi]="cb_cat"     # cat file
112
113 # If the file exists but it hasn't any associated action, just do an ls of it
114 ((cbi++))
115 CB_ACTION[$cbi]="cb_unknown_file"       # ls file
116 CB_ACTION_II[$cbi]="cb_donothing"
117
118 # non existent file
119 ((cbi++))
120 CB_ACTION[$cbi]="cb_donothing"
121 CB_ACTION_II[$cbi]="cb_notexistent"     # vim new_file
122
123 ((cbi++))
124 CB_ACTION[$cbi]="cb_shell"              # shell $1
125 CB_ACTION_II[$cbi]="cb_donothing"
126
127
128
129 ############ FUNCTIONS ###############
130
131
132 cb_list_dir() {
133         if [ -z "$1" ]
134         then
135                 eval ls
136                 return $CB_OK
137         fi
138 }
139 cb_list_dir_help() { echo "List current directory"; }
140 cb_list_dir_exp() { echo "]"; }
141
142 cb_cdhome() {
143         if [ -z "$1" ]
144         then
145                 eval cd $HOME
146                 return $CB_OK
147         fi
148 }
149 cb_cdhome_help() { echo "Go home"; }
150 cb_cdhome_exp() { echo "]["; }
151
152
153 cb_cdcd() {
154         if [ -d "$1" ]
155         then
156                 eval 'cd "$1"'
157                 return $CB_OK
158         fi
159 }
160 cb_cdcd_help() { echo "Change directory to $@"; }
161 cb_cdcd_exp() { echo "] /tmp"; }
162
163 cb_lsdir() {
164         if [ -d "$1" ]
165         then
166                 eval 'ls "$@"'
167                 return $CB_OK
168         fi
169 }
170 cb_lsdir_help() { echo "List directory"; }
171 cb_lsdir_exp() { echo "][ /tmp"; }
172
173 __cb_match_extension() {
174     local fname
175     fname="$1"
176     shift
177     for i in "$@"; do
178         i="`echo $i | sed -e 's#\.#\\\\.#'`"
179         res=`expr match "$fname" ".*$i$"`
180         if [ $res -ne 0 ]; then
181             return 0
182         fi
183     done
184     return 1
185 }
186
187 cb_file_exts() {
188         local idx i _help
189         idx=1
190
191         if [ "$1" == "__CB_HELP__" ]; then _help=1; shift; fi
192         if [ -z "$1" ]; then return; fi
193
194         while [ ! -z "${CB_EXT_PROG[$idx]}" ]
195         do
196                 if __cb_match_extension "$1" ${CB_EXT_EXTS[$idx]}
197                 then
198                         if [ ! -z "$_help" ]; then
199                                 echo "I will open \"$@\" with ${CB_EXT_PROG[$idx]}"
200                         else
201                                 ${CB_EXT_PROG[$idx]} "$@"
202                         fi
203                         return $CB_OK
204                 fi
205                 ((idx++))
206         done
207 }
208 cb_file_exts_help() {
209         if [ -z "$1" ]; then
210                 echo "Different actions by file extension"
211         else
212                 cb_file_exts "__CB_HELP__" "$@";
213         fi
214 }
215 cb_file_exts_exp() { echo "] file.avi
216 ] file.pdf
217 ] file.mp3"
218 }
219
220 cb_file_exts_II() {
221         local idx i _help
222         idx=1
223
224         if [ "$1" == "__CB_HELP__" ]; then _help=1; shift; fi
225         if [ -z "$1" ]; then return; fi
226
227         while [ ! -z "${CB_EXT_PROG[$idx]}" ]
228         do
229                 if __cb_match_extension "$1" ${CB_EXT_EXTS[$idx]}
230                 then
231                         if [ ! -z "$_help" ]; then
232                                 echo "I will open \"$@\" with ${CB_EXT_PROG_II[$idx]}"
233                         else
234                                 ${CB_EXT_PROG_II[$idx]} "$@"
235                         fi
236                         return $CB_OK
237                 fi
238                 ((idx++))
239         done
240 }
241 cb_file_exts_II_help() {
242         if [ -z "$1" ]; then
243                 echo "Different secondary actions by file extension"
244         else
245                 cb_file_exts_II "__CB_HELP__" "$@";
246         fi
247 }
248 cb_file_exts_II_exp() { echo "][ file.jpg file.png
249 ][ file.svg"
250 }
251
252
253 cb_tarra() {
254         if [ -z "$1" ]; then return; fi
255         # TODO
256         return $CB_SKIP
257 }
258
259 cb_untarra() {
260         local base tf
261         if [ -z "$1" ]; then return; fi
262
263         echo untarra...
264         tf=$(tempfile)
265         $CB_UNTARRA "$@" > $tf
266         base=`cat $tf | line`
267         rm $tf
268
269         if [ -d "$base" ]
270         then
271                 echo cd $base
272                 cd $base
273         fi
274
275         return $CB_OK
276 }
277
278 cb_vim() {
279         if [ -z "$1" ]; then return; fi
280         if [ -f "$1" ]
281         then
282                 $CB_EDITOR "$@"
283                 return $CB_OK
284         fi
285 }
286 cb_vim_help() { echo "Open file with vim"; }
287 cb_vim_exp() { echo "] /etc/passwd"; }
288
289 cb_cat() {
290         if [ -z "$1" ]; then return; fi
291         if [ -f "$1" ]
292         then
293                 cat "$@"
294                 return $CB_OK
295         fi
296 }
297 cb_cat_help() { echo "Print file with cat"; }
298 cb_cat_exp() { echo "][ /etc/passwd"; }
299
300 cb_htm() {
301         if [ -z "$1" ]; then return; fi
302         if [ -z "$DISPLAY" ]
303         then
304                 $CB_TEXT_BROWSER "$@"
305         else
306                 echo ] $CB_BROWSER...
307                 $CB_BROWSER "$@"
308         fi
309         return $CB_OK
310 }
311
312 cb_img_viewer() {
313         if [ -z "$1" ]; then return; fi
314         if [ -z "$DISPLAY" ]
315         then
316                 echo ] $CB_IMAGE_TTY_VIEWER...
317                 $CB_IMAGE_TTY_VIEWER "$@"
318         else
319                 echo ] $CB_IMAGE_VIEWER...
320                 $CB_IMAGE_VIEWER "$@"
321         fi
322         return $CB_OK
323 }
324
325 cb_img_editor() {
326         if [ -z "$1" ]; then return; fi
327         if [ -z "$DISPLAY" ]
328         then
329                 echo ] $CB_IMAGE_TTY_VIEWER...
330                 $CB_IMAGE_TTY_VIEWER "$@"
331         else
332                 echo ] $CB_IMAGE_EDITOR...
333                 $CB_IMAGE_EDITOR "$@"
334         fi
335         return $CB_OK
336 }
337
338 cb_bittorrent() {
339         if [ -z "$1" ]; then return; fi
340         if [ -z "$DISPLAY" ]
341         then
342                 echo ] $CB_BITTORRENT_TTY...
343                 $CB_BITTORRENT_TTY "$@"
344         else
345                 echo ] $CB_BITTORRENT...
346                 $CB_BITTORRENT "$@"
347         fi
348         return $CB_OK
349 }
350
351 cb_pdf() {
352         local tmp
353         if [ -z "$1" ]; then return; fi
354         if [ -z "$DISPLAY" ]
355         then
356                 echo "] $1 -> $tmp.html"
357
358                 tmp=$(tempfile)
359                 pdftohtml "$1" -stdout > $tmp.html
360                 if [ $? -eq 0 ]; then
361                         $CB_TEXT_BROWSER $tmp.html
362                         rm $tmp.html
363                 elif [ $? -eq 127 ]; then
364                         echo "Please install pdftohtml. It's a nice utility"
365                 else
366                         echo "pdftohtml "$1": failed"
367                 fi
368         else
369                 echo ] $CB_PDF_VIEWER...
370                 $CB_PDF_VIEWER "$@"
371         fi
372         return $CB_OK
373 }
374
375 cb_mail_agent() {
376         if [ -z "$1" ]; then return; fi
377         echo "$1" | perl -e \
378         'if(<>=~/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) {
379                 exit 0;
380         }
381         exit 1;'
382         if [ $? -eq 0 ]
383         then
384                 $CB_MAIL_AGENT "$@"
385                 return $CB_OK
386         fi     
387 }
388 cb_mail_agent_help() { echo "Open mutt to send a new mail"; }
389 cb_mail_agent_exp() { echo "] foo@bar.org"; }
390
391 cb_shell() {
392         if [ -z "$1" ]; then return; fi
393
394         $CB_SHELL_GREP "$@"
395         if [ $? -eq 0 ]
396         then
397                 return $CB_OK
398         fi
399 }
400 cb_shell_help() {
401         echo "ssh to remote shell"
402 }
403 cb_shell_exp() { echo "] shellalias
404 ] fullshellhostname"; }
405
406 cb_unknown_file() {
407         if [ -e "$1" ]
408         then
409                 ls "$@"
410                 return $CB_OK
411         fi
412 }
413 cb_unknown_file_help() { true; }
414 cb_unknown_file_exp() { true; }
415
416 cb_donothing() {
417         return $CB_SKIP
418 }
419 cb_donothing_help() { true; }
420 cb_donothing_exp() { true; }
421
422 cb_notexistent() {
423         local cbf;
424         if [ -z "$1" ]; then return; fi
425         cbf="`echo $1`"
426         if [ ! -e "$cbf" ]
427         then
428                 $CB_EDITOR "$@"
429                 return $CB_OK
430         fi
431 }
432 cb_notexistent_help() { echo "Open a new file with vim"; }
433 cb_notexistent_exp() { echo "][ /tmp/non_existent_file"; }
434
435 cb_http_browser() {
436         if [ -z "$1" ]; then return; fi
437         case $1 in http://*|https://*|ftp://*)
438                 if [ -z "$DISPLAY" ]
439                 then
440                         $CB_TEXT_BROWSER "$@"
441                 else
442                         echo $CB_BROWSER...
443                         $CB_BROWSER "$@"
444                 fi
445                 return $CB_OK
446         esac
447 }
448 cb_http_browser_help() {
449         echo "Open the URI with browser (\$CB_BROWSER=$CB_BROWSER, \$CB_TEXT_BROWSER=$CB_TEXT_BROWSER)"
450 }
451 cb_http_browser_exp() { echo "] http://www.google.com"; }
452
453
454 cb_http_wget() {
455         if [ -z "$1" ]; then return; fi
456         case $1 in http://*|https://*|ftp://*)
457                 $CB_HTTP_DOWNLOADER "$@"
458                 return $CB_OK;;
459         esac
460 }
461 cb_http_wget_help() { echo "Download the URI with $CB_HTTP_DOWNLOADER (\$CB_HTTP_DOWNLOADER)"; }
462 cb_http_wget_exp() {
463         echo "][ http://images.freshmeat.net/img/logo.gif
464 ][ ftp://hostname/pub/file"
465 }
466
467 cb_remote_edit() {
468         local host rpath user
469         local uno
470         uno="$1"
471
472         if [ -z "$1" ]; then return; fi
473         case $1 in scp://*|rsync://*)
474                 $CB_EDITOR "$@"
475                 return $CB_OK;;
476         esac
477         case $uno in *:*)
478                 case $uno in
479                     *@*)
480                         user=$(echo "$uno" | cut -d '@' -f 1)
481                         host=$(echo "$uno" | cut -d '@' -f 2 | cut -d ':' -f 1)
482                         rpath=$(echo "$uno" | cut -d '@' -f 2 | cut -d ':' -f 2-) ;;
483                     *)
484                         host=$(echo "$uno" | cut -d ':' -f 1)
485                         rpath=$(echo "$uno" | cut -d ':' -f 2-) ;;
486                 esac
487                 if grep -q $host $SHELL_LIST_FILE; then
488                         host=$(grep $host $SHELL_LIST_FILE | line)
489                         if [ ! -z $user ]; then user="$user@"; fi
490                         $CB_EDITOR scp://$user$host/$rpath
491                         return $CB_OK
492                 fi;;
493         esac
494 }
495 cb_remote_edit_help() { echo "Open a remote file with $CB_EDITOR (\$CB_EDITOR)"; }
496 cb_remote_edit_exp() { echo "] shellalias:file
497 ] scp://fullshellhostname/file
498 ] ftp://ftp.isi.edu/in-notes/rfc2481.txt"
499 }
500
501 cb_remote_copy() {
502         local host rpath user lpath last_arg remotelocal
503         if [ -z "$1" ]; then return; fi
504
505         remote_copy_parse_uri() {
506                 case $1 in *:*)
507                         case $1 in
508                             *@*)
509                                 user=$(echo "$1" | cut -d '@' -f 1)
510                                 host=$(echo "$1" | cut -d '@' -f 2- | cut -d ':' -f 1)
511                                 rpath=$(echo "$1" | cut -d '@' -f 2- | cut -d ':' -f 2-) ;;
512                             *)
513                                 host=$(echo "$1" | cut -d ':' -f 1)
514                                 rpath=$(echo "$1" | cut -d ':' -f 2-) ;;
515                         esac;;
516                 esac
517         }
518        
519         if (( $# == 1 ))
520         then
521                 case $1 in $CB_REMOTE_COPY_PREFIX)
522                         rpath=$(echo "$1" | cut -d '/' -f 3-)
523                         host=$(echo $rpath | cut -d '/' -f 1)
524                         rpath=$(echo $rpath | cut -d '/' -f 2-)
525                         $CB_REMOTE_COPY_DIR $host:$rpath .
526                         return $CB_OK;;
527                 esac
528                
529                 case $1 in *:*)
530                         remote_copy_parse_uri "$1"
531                         lpath="."
532                         remotelocal="from_remote";;
533                 esac
534         else
535                 case $1 in
536                         *:*)
537                         remote_copy_parse_uri "$1"
538                         shift
539                         while (($#)); do lpath="$lpath \"$1\""; shift; done
540                         remotelocal="from_remote";;
541
542                         *)
543                         while (($#)); do
544                                 if [ ! -z "$last_arg" ]; then
545                                         lpath="$lpath \"$last_arg\""
546                                 fi
547                                 last_arg="$1"
548                                 shift
549                         done
550                         remote_copy_parse_uri $last_arg
551                         remotelocal="from_local";;
552                 esac
553         fi
554
555         if [ ! -z "$host" -a ! -z "$lpath" ]; then
556                 if grep -q $host $SHELL_LIST_FILE; then
557                         host=$(grep $host $SHELL_LIST_FILE | line)
558                         if [ ! -z $user ]; then user="$user@"; fi
559                        
560                         if [ "$remotelocal" = "from_remote" ]; then
561                                 eval $CB_REMOTE_COPY_DIR $user$host:$rpath $lpath
562                         else
563                                 eval $CB_REMOTE_COPY_DIR $lpath $user$host:$rpath
564                         fi
565                        
566                         return $CB_OK
567                 fi
568         fi
569 }
570 cb_remote_copy_help() { echo "Copy a (remote) file/dir to a (remote) location with $CB_REMOTE_COPY_DIR (\$CB_REMOTE_COPY_DIR)"; }
571 cb_remote_copy_exp() { echo "][ /etc/passwd shellalias:/tmp/
572 ][ shellalias:/etc/passwd ./copied_passwd
573 ][ my_dir shellalias:path/
574 ][ file scp://fullshellhostname/path/"
575 }
576
577 cb_file_colon_line() {
578         local f l
579         if [ -z "$1" ]; then return; fi
580
581         local IFS=:
582         set -- $1
583         f=$1 l=$2
584         if [ ! -f "$f" ] || [ -z "$l" ] || echo $l | grep '[^0-9]'; then return; fi
585
586         vim $f +$l
587         return $CB_OK
588 }
589 cb_file_colon_line_help() { echo "Open the file at the specified line: vim file +line"; }
590 cb_file_colon_line_exp() { echo "] file:line"; }
591
592 cb_calc() {
593         if [ -z "$1" ]; then return; fi
594         echo "$@" | perl -e \
595         'if(<>=~/^[0-9]+[\.]*[0-9]*\s*[\+\-\^\*%]\s*[0-9]+[\.]*[0-9]*/) {
596                 exit 0;
597         }
598         exit 1;'
599         if [ $? -eq 0 ]
600         then
601                 echo $@ | $CB_CALC
602                 return $CB_OK
603         fi     
604 }
605
606 xpdf_hack() { #xpdf doesn't allow multiple file arguments.
607         for i in "$@"
608         do
609                 xpdf "$i"&
610         done
611 }
Note: See TracBrowser for help on using the browser.