summaryrefslogtreecommitdiff
blob: 942378fff0f659f3a0abe830c2736705dada45e8 (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
#! /bin/bash

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.

# bash_completion for latexmk
#
#
# Author:  Christoph Junghans
#          junghans@gentoo.org
#
# Revision history:
#  0.1   26-05-10 --- initial version
#  0.2   15-02-11 --- clean up
#
# HOWTO:
# source this file to enable it

_latexmk()
{
  local cur output aopts opts prev
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}

  output=$( $1 -help 2> /dev/null)
  #options with args ,pattern: ^   (-XXX) <YYYY>.*$
  aopts=" $( echo " $output" | sed -n 's/^[[:space:]]\+\(-[^[:space:]]\+\)[[:space:]]\+<[^>]\+>.*$/\1/p' | sort -u | tr '\n' ' ')"
  #if previous option in in $aopts
  if [[ -n "$prev" ]] && [[ -z "${aopts//* $prev *}" ]]; then
    #argument of $prev ,pattern: ^ -XXX (<YYYY>).*$
    opts=$(echo "$output" | sed -n "s/^[[:space:]]\+$prev[[:space:]]\+\(<[^>]\+>\).*\$/\1/p")
    COMPREPLY=( $( compgen -W '$opts' -- $cur ) )
  elif [[ "$cur" == -* ]]; then
    #all options, pattern: ^    (-XXX).*$
    opts=$( echo "$output" | sed -n 's/^[[:space:]]\+\(-[^[:space:]]\+\).*$/\1/p'| sort -u )
    COMPREPLY=( $( compgen -W '$opts' -- $cur ) )
  else
    #filenames *.tex and dirs, rest is done by '-o filenames' below
    COMPREPLY=( $( eval compgen -f -X "!*.tex" -- ${cur} ) $( compgen -d -- $cur ) )
  fi
}

complete -F _latexmk -o filenames latexmk