Sophie

Sophie

distrib > Mageia > 5 > i586 > by-pkgid > fa13d35ff3faa4effa226a9e80574c09 > files > 7

R-base-3.1.2-2.mga5.src.rpm

### Copyright (C) 2005-2006 Deepayan Sarkar <Deepayan.Sarkar@R-project.org>
###
### This file provides programmable completion for R under the bash
### shell.  It is made available under the terms of the GNU General
### Public License, version 2, or at your option, any later version,
### incorporated herein by reference.
###
### This program is distributed in the hope that it will be
### useful, but WITHOUT ANY WARRANTY; without even the implied
### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
### PURPOSE.  See the GNU General Public License, available at 
### http://www.gnu.org/licenses/gpl.txt, for more details.


### NOTE: This does NOT provide completion for commands within R, only
### for R's command-line options.  For example,
###
### R CMD build --f[TAB] 
###
### will complete to --force and so on. In other words, this is
### potentially useful (for the most part) to package developers, not
### regular users.

### NEWS: Updated for R 2.4.0 (October 2006)

### BUGS: As it stands, this is far from perfect.  Known bugs include: 
###
### (1) file/directory name completion doesn't work when there is a ~
### (1) in the name.  Fortunately, in such situations, one can always
### (1) fall back to standard completion by using M-/ instead of TAB
###
### (2) Not all mutually exclusive options are properly handled
###
### (3) Doesn't test for the existence of R before defining the
### (3) completion function (not strictly a bug, but definitely not a
### (3) good feature)


### INSTALLATION AND USAGE: You need to first install and enable bash
### completion on your system to use this script.  See
###
### http://www.caliban.org/bash/index.shtml#completion
###
### to learn more about bash completion.  On a modern Debian system,
### all the necessary files are already installed by the bash package.
### To enable it, just uncomment the following lines in your .bashrc
### 
### # if [ "$PS1" -a -f /etc/bash_completion ]; then
### #     . /etc/bash_completion
### # fi
### 
### or to try it out in your current shell, type 
### 
### . /etc/bash_completion
### 
### Once you are using bash completion, you can enable completion for
### R by similarly sourcing this file.  For permanent use, place it in
### your $BASH_COMPLETION_DIR (/etc/bash_completion.d in Debian).  If
### you don't have access to do that, copy the contents of this file
### in it's entirety to your ~/.bash_completion, which is sourced by
### the bash_completion script.




_R()
{
    local cur loneOpts helpOpts stdOpts argOpts versOpts cmds cmd furtherOpts
    local fileNames dirNames i lastToken
    # local IFS=$'\t\n'

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    loneOpts='--version RHOME'
    versOpts='-v --version' # only on commands
    helpOpts='-h --help'

    stdOpts='--save --no-save --no-environ --no-site-file            \
    --no-init-file --restore --no-restore-data --no-restore-history  \
    --no-restore --vanilla --no-readline -q --quiet --silent --slave \
    --verbose -d -g --args'

    ## these should end with a '=' and not be followed by a space.
    ## Unfortunately, I haven't figured out how to make that work.
    ## -nospace doesn't suppress the space, and if I add = at the end,
    ## they get completed as "--debugger\= " etc.  Anyway, they are
    ## kept separate in anticipation of some future fix...

    argOpts='--min-vsize --max-vsize --min-nsize --max-nsize \
    --encoding --max-ppsize --debugger --debugger-args \
    --gui --arch'

    cmds='BATCH COMPILE SHLIB INSTALL REMOVE build check LINK Rprof \
    Rdconv Rd2dvi Rd2txt Sd2Rd Sweave Stangle config'




    fileNames='' # possible to have files in certain commands (e.g. BATCH)
    dirNames='' 


    if [[ $COMP_CWORD -eq 1 ]] ; then
	COMPREPLY=(
	    $( compgen -W "CMD $loneOpts $helpOpts $stdOpts" -- $cur )
	    $( compgen -o nospace -W "$argOpts" -- $cur )
	)
	return 0
    fi

    ## certain tokens preclude further activity
    if [[ ${COMP_WORDS[$COMP_CWORD-1]} == @(-v|--version|RHOME|-h|--help) ]] ; then
	return 0
    fi

    ## more complicated stuff
    ## keep last token for use later
    lastToken=${COMP_WORDS[$COMP_CWORD-1]}


    ## if [[ $COMP_CWORD -gt 1 ]] && (implied)
    if [[ ${COMP_WORDS[1]} == "CMD" ]] ; then

	if [[ $COMP_CWORD -eq 2 ]] ; then
	    COMPREPLY=(
		$( compgen -W "$cmds" -- $cur )
	    )
	    return 0
	else

	    ## COMP_CWORD >= 3.  cmd=${COMP_WORDS[2]} is (hopefully)
	    ## one of $cmds.  At this point, all further options
	    ## (except -v|-h|RHOME etc handled above) will be assumed
	    ## repeatable (except for duplicates and exclusives, to be
	    ## handled later).  In the next step, possible completions
	    ## for each cmd will be derived.

	    ## A largely similar derivation is also needed for the
	    ## case when ${COMP_WORDS[2]} is not CMD.  A similar
	    ## completion list will be derived in that case.
	    ## Afterwards, things will be removed from these as
	    ## needed, before the final COMPREPLY is computed.

	    cmd=${COMP_WORDS[2]}
	    case $cmd in
		BATCH)

		    ## if last word is a filename (not starting with
		    ## -) then no more options are allowed, but can be
		    ## followed by any file (outfile)

		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken != -* ]] ; then

			furtherOpts=""
			fileNames=$( compgen -f -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    else

			furtherOpts="$versOpts $helpOpts $stdOpts $argOpts"
			fileNames=$( compgen -f -X '!*.@(R|r|S|s)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi
		    ;;

		COMPILE)
		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken != -* ]] ; then

			furtherOpts=""
			fileNames=$( compgen -f -X '!*.@(c|cc|cpp|C|f)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    else

			furtherOpts="$versOpts $helpOpts -D"
			fileNames=$( compgen -f -X '!*.@(c|cc|cpp|C|f)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi
		    ;;

		SHLIB)
		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken != -* ]] ; then

			furtherOpts=""
			fileNames=$( compgen -f -X '!*.@(a|o)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    else

			furtherOpts="$versOpts $helpOpts -o --output"
			fileNames=$( compgen -f -X '!*.@(a|o)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi

		    ;;

		INSTALL)
		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken == @(-l|--library) ]] ; then

			furtherOpts=""
			dirNames=$( compgen -d -- "$cur" )

		    else

			furtherOpts="$versOpts $helpOpts --configure-args \
--configure-vars -c --clean -s --save --no-save --lazy --no-lazy \
--lazy-data --no-lazy-data -d --debug -l --library --no-configure \
--no-docs --with-package-versions --use-zip-data --use-zip-help \
--use-zip --fake --no-lock --build"
			fileNames=$( compgen -f -X '!*.@(tar.gz|tgz)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi
		    ;;

		REMOVE)
		    furtherOpts="$versOpts $helpOpts -l --library"
		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken == @(-l|--library) ]] ; then
			dirNames=$( compgen -d -- "$cur" )
		    fi
		    ;;

		build)

		    furtherOpts="$versOpts $helpOpts --force \
		    --no-vignettes --binary --no-docs --use-zip-data \
		    --use-zip-help --use-zip"
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		check)

		    furtherOpts="$versOpts $helpOpts -l --library -o \
		    --outdir --no-clean --no-codoc --no-examples \
		    --no-install --no-tests --no-vignettes --no-latex \
		    --use-gct --use-valgrind --rcfile"

		    fileNames=$( compgen -f -X '!*.@(tar.gz|tgz)' -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		LINK)
		    furtherOpts="$versOpts $helpOpts"
		    ;;

		Rprof)
		    furtherOpts="$versOpts $helpOpts"
		    fileNames=$( compgen -f -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		Rdconv)

		    furtherOpts="$versOpts $helpOpts -d --debug -t \
		    --type -o --output --os --OS"

		    fileNames=$( compgen -f -X '!*.@(Rd)' -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		Rd2dvi)

		    furtherOpts="$versOpts $helpOpts --batch --debug \
		    --no-clean --no-preview --os --OS -o --output \
		    --pdf --title -V --verbose"

		    fileNames=$( compgen -f -X '!*.@(Rd)' -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		Rd2txt)
		    furtherOpts="$versOpts $helpOpts"
		    fileNames=$( compgen -f -X '!*.@(Rd)' -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		Sd2Rd)
		    furtherOpts="$versOpts $helpOpts -n -x"
		    fileNames=$( compgen -f -X '!*.@(Sd)' -- "$cur" )
		    dirNames=$( compgen -d -- "$cur" )
		    ;;

		Sweave)

		    ## if last word is a filename (not starting with
		    ## -) then nothing more is allowed

		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken != -* ]] ; then

			return 0

		    else

			furtherOpts=""
			fileNames=$( compgen -f -X '!*.@(Rnw|rnw|Snw|snw)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi
		    ;;

		Stangle)

		    ## if last word is a filename (not starting with
		    ## -) then nothing more is allowed

		    if [[ $COMP_CWORD -gt 3 ]] && \
			[[ $lastToken != -* ]] ; then

			return 0

		    else

			furtherOpts=""
			fileNames=$( compgen -f -X '!*.@(Rnw|rnw|Snw|snw)' -- "$cur" )
			dirNames=$( compgen -d -- "$cur" )

		    fi
		    ;;

		config)

		    if [[ $COMP_CWORD -gt 3 ]] ; then
			
			return 0

		    else

			furtherOpts="$versOpts $helpOpts --ldflags  \
--cppflags BLAS_LIBS CC CFLAGS CPICFLAGS CPP CPPFLAGS CXX CXXCPP    \
CXXFLAGS CXXPICFLAGS DYLIB_LD DYLIB_LDFLAGS F2C F77 FFLAGS FLIBS    \
FPICFLAGS LAPACK_LIBS LDFLAGS MAKE SHLIB_CFLAGS SHLIB_CXXLD         \
SHLIB_CXXLDFLAGS SHLIB_FFLAGS SHLIB_LD SHLIB_LDFLAGS TCLTK_CPPFLAGS \
TCLTK_LIBS"
		    fi
		    ;;

	    esac

	fi

    elif [[ $lastToken == "<" ]] ; then

	## R [options] < <need .R file here>
	furtherOpts=""
	fileNames=$( compgen -f -X '!*.@(R|r|S|s)' -- "$cur" )
	dirNames=$( compgen -d -- "$cur" )

    elif [[ $lastToken == ">" ]] ; then

	## R [options] > <need output file here>
	furtherOpts=""
	fileNames=$( compgen -f -- "$cur" )
	dirNames=$( compgen -d -- "$cur" )

    else

	case $lastToken in 

	    -g|--gui) 
		furtherOpts="X11 gnome Tk none" 
		;;
	    -d|--debugger) ## allow commands
		furtherOpts=$( compgen -c -- "$cur" )
		;;

	    *) ## NOT 'R CMD <something> ': see comments above
		furtherOpts="$versOpts $helpOpts $stdOpts $argOpts" ;;

	esac
    fi

    ## need some removal, 

    ## take out options already given
    for (( i=1; i<=$COMP_CWORD-1; ++i )) ; do

	opt=${COMP_WORDS[$i]}

	case $opt in
	    --*)    optBase=${opt/=*/} ;;
	    -*)     optBase=${opt:0:2} ;;
	esac

	## can't seem to do this using case
	if [[ $opt == "<" ]] ; then
	    ## no more args allowed
	    furtherOpts=""
	elif [[ $opt == ">" ]] ; then
	    ## no more args allowed
	    furtherOpts=""
	fi


	allOpts=" $furtherOpts "
	allOpts=${allOpts/ ${optBase} / }

	#echo $allOpts

        ## take out alternatives and mutually exclusives
	case $optBase in

# not working
# 	    "<") ## only filenames allowed
# 		echo "-----------"
# 		allOpts=">" ;;
# 	    ">") ## only filenames allowed
# 		allOpts="<" ;;

	    -q|--quiet|--silent)       
		allOpts=${allOpts/ -q / }
		allOpts=${allOpts/ --quiet / }
		allOpts=${allOpts/ --silent / }
		#echo $allOpts

		;;
	    --save|-s|--no-save)        
		allOpts=${allOpts/ --save / } 
		allOpts=${allOpts/ --no-save / } 
		allOpts=${allOpts/ -s / } 
		;;
	    --vanilla)     
		allOpts=${allOpts/ --no-save / } 
		allOpts=${allOpts/ --no-restore / } 
		allOpts=${allOpts/ --no-site-file / } 
		allOpts=${allOpts/ --no-init-file / } 
		allOpts=${allOpts/ --no-environ / } 
		;;
	    -d)       allOpts=${allOpts/ --debugger / } ;;
	    --debugger)       allOpts=${allOpts/ -d / } ;;
	    --restore)        allOpts=${allOpts/ --no-restore / } ;;
	    --no-restore)        allOpts=${allOpts/ --restore / } ;;
	    --debugger)       allOpts=${allOpts/ -d / } ;;
	    -g|--gui)       
		allOpts=${allOpts/ -g / }
		allOpts=${allOpts/ --gui / }
		;;

 	    BATCH)       
		allOpts=${allOpts/ --restore / }
		allOpts=${allOpts/ --save / }
		allOpts=${allOpts/ --no-readline / }
		;;

	    ## build
	    --use-zip)     
		allOpts=${allOpts/ --use-zip-data / } 
		allOpts=${allOpts/ --use-zip-help / } 
		;;


	    ## INSTALL
	    --lazy|--no-lazy)        
		allOpts=${allOpts/ --lazy / } 
		allOpts=${allOpts/ --no-lazy / } 
		;;

	    --lazy-data|--no-lazy-data)        
		allOpts=${allOpts/ --lazy-data / } 
		allOpts=${allOpts/ --no-lazy-data / } 
		;;

	esac

    done

    ## and finally:


    COMPREPLY=(
	$( compgen -W "$allOpts $fileNames $dirNames" -- $cur )
    )
    return 0


}


complete -F _R -o filenames R

## Add more aliases here, e.g. 

#complete -F _R -o filenames R-2.4



### Local variables:
### mode: shell-script
### End: