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
|
## Copyright (C) 2005-2014 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/licenses.html, 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) Not all mutually exclusive options are properly handled
###
### (2) Doesn't test for the existence of R before defining the
### (2) completion function (not strictly a bug, but definitely not a
### (2) 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 \
--interactive --verbose -d -g --args -f -e'
## 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 --file'
cmds='BATCH COMPILE SHLIB INSTALL REMOVE build check LINK Rprof \
Rdconv Rd2pdf Rd2txt Stangle Sweave Rdiff config javareconf rtags'
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 --no-timing"
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 -c --clean \
--preclean -n --dry-run"
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 --preclean -d --debug -l \
--library --no-configure --no-docs --html --no-html --latex --example \
--fake --no-lock --lock --pkglock --build --install-tests --no-R \
--no-libs --no-data --no-help --no-demo --no-exec --no-inst \
--no-multiarch --libs-only --data-compress --resave-data \
--compact-docs --with-keep.source --without-keep.source --byte-compile \
--no-byte-compile --no-test-load --no-clean-on-error --merge-multiarch \
--built-timestamp"
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 --keep-empty-dirs --no-build-vignettes --no-manual --resave-data \
--no-resave-data --compact-vignettes --md5"
dirNames=$( compgen -d -- "$cur" )
;;
check)
furtherOpts="$versOpts $helpOpts \
-l --library -o --output \
--no-clean --no-codoc --no-examples --no-install --no-tests \
--no-manual --no-vignettes --no-build-vignettes --run-dontrun \
--run-donttest --use-gct --use-valgrind --timings \
--install-args --test-dir --check-subdirs --as-cran \
--extra-arch --multiarch --no-multiarch --force-multiarch"
fileNames=$( compgen -f -X '!*.@(tar.gz|tgz)' -- "$cur" )
dirNames=$( compgen -d -- "$cur" )
;;
LINK)
furtherOpts="$versOpts $helpOpts"
;;
Rprof)
furtherOpts="$versOpts $helpOpts --lines --total --self --linesonly --min%total --min%self"
fileNames=$( compgen -f -- "$cur" )
dirNames=$( compgen -d -- "$cur" )
;;
Rdconv)
furtherOpts="$versOpts $helpOpts -t --type \
--encoding --package -o --output --os --OS"
fileNames=$( compgen -f -X '!*.@(Rd)' -- "$cur" )
dirNames=$( compgen -d -- "$cur" )
;;
Rd2pdf)
furtherOpts="$versOpts $helpOpts --batch \
--no-clean --no-preview --encoding --outputEncoding \
--os --OS -o --output --force --title \
--no-index --no-description --internals"
fileNames=$( compgen -f -X '!*.@(Rd)' -- "$cur" )
dirNames=$( compgen -d -- "$cur" )
;;
Rd2txt)
furtherOpts="$versOpts $helpOpts -t --type --encoding --package -o --output --os --OS"
fileNames=$( compgen -f -X '!*.@(Rd)' -- "$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="$versOpts $helpOpts --driver --engine --encoding --options --pdf --compact"
fileNames=$( compgen -f -X '!*.@(Rnw|rnw|Snw|snw|Rmd|rmd)' -- "$cur" )
dirNames=""
## 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="$versOpts $helpOpts --engine --encoding --options"
fileNames=$( compgen -f -X '!*.@(Rnw|rnw|Snw|snw|Rmd|rmd)' -- "$cur" )
dirNames=""
## dirNames=$( compgen -d -- "$cur" )
fi
;;
config)
if [[ $COMP_CWORD -gt 3 ]] ; then
return 0
else
furtherOpts="$versOpts $helpOpts --ldflags --cppflags \
--no-user-files --no-site-files \
BLAS_LIBS CC CFLAGS CPICFLAGS CPP CPPFLAGS CXX CXXCPP CXXFLAGS \
CXXPICFLAGS DYLIB_EXT DYLIB_LD DYLIB_LDFLAGS F77 FFLAGS FLIBS \
FPICFLAGS FC FCFLAGS FCPICFLAGS JAR JAVA JAVAC JAVAH JAVA_HOME \
JAVA_LIBS JAVA_CPPFLAGS LAPACK_LIBS LIBnn LDFLAGS OBJC OBJCFLAGS MAKE \
SAFE_FFLAGS SHLIB_CFLAGS SHLIB_CXXLD SHLIB_CXXLDFLAGS SHLIB_EXT \
SHLIB_FFLAGS SHLIB_LD SHLIB_LDFLAGS SHLIB_FCLD SHLIB_FCLDFLAGS \
TCLTK_CPPFLAGS TCLTK_LIBS"
fi
;;
javareconf)
furtherOpts="$versOpts $helpOpts -n --dry-run -e"
;;
rtags)
furtherOpts="$versOpts $helpOpts -o --output --no-c --no-R \
--no-Rd -a --append -V --verbose"
dirNames=$( compgen -d -- "$cur" )
;;
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 Tk"
;;
-d|--debugger) ## allow commands
furtherOpts=$( compgen -c -- "$cur" )
;;
-f|--file)
fileNames=$( compgen -f -X '!*.@(R|r|S|s)' -- "$cur" )
dirNames=""
## dirNames=$( compgen -d -- "$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|--no-save)
allOpts=${allOpts/ --save / }
allOpts=${allOpts/ --no-save / }
;;
--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 / } ;;
-g|--gui)
allOpts=${allOpts/ -g / }
allOpts=${allOpts/ --gui / }
;;
BATCH)
allOpts=${allOpts/ --restore / }
allOpts=${allOpts/ --save / }
allOpts=${allOpts/ --no-readline / }
;;
esac
done
## and finally:
COMPREPLY=(
$( compgen -W "$allOpts $fileNames $dirNames" -- $cur )
)
return 0
}
complete -F _R -o default R
## Add more aliases here, e.g.
# for e in R-devel R-patched; do complete -F _R -o default ${e}; done
### Local variables:
### mode: shell-script
### End:
|