Difference between revisions of "Bash completion"

From Apertium
Jump to navigation Jump to search
(Created page with 'I sent these in to the bash_completion people but it probably takes a while to get through. I haven't added for all the apertium-* programs. Put the files in /etc/bash_completion…')
 
 
(19 intermediate revisions by one other user not shown)
Line 1: Line 1:
  +
If you want “intelligent” bash completion on TAB for apertium, lttoolbox, vislcg3 and hfst, do e.g.:
I sent these in to the bash_completion people but it probably takes a while to get through. I haven't added for all the apertium-* programs. Put the files in /etc/bash_completion.d/ or wherever (see http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_1 ). Probably it'd be simpler to use [http://limpet.net/mbrubeck/2009/10/30/compleat.html compleat] instead.
 
  +
  +
$ git clone https://github.com/unhammer/apertium-completion.git ~/apertium-completion
  +
  +
and add this to your ~/.bashrc:
   
 
<pre>
 
<pre>
  +
if ! shopt -oq posix && \
# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
 
  +
[[ ( -z "$INSIDE_EMACS" || "$EMACS_BASH_COMPLETE" = "t" ) ]]; then
# ex: ts=8 sw=8 noet filetype=sh
 
  +
for f in ~/apertium-completion/completions/*; do
#
 
  +
[[ -f $f ]] && source "$f"
# lttoolbox completion by Kevin Brubeck Unhammer <unhammer@gmail.com>
 
  +
done
# Licence: GNU GPL v2
 
  +
fi
  +
</pre>
   
  +
Alternatively, if your OS sources files from e.g. /etc/bash_completion_d automatically, just sudo cp ~/apertium-completion/completions/* /etc/bash_completion_d/ (then you don’t have to add anything to ~/.bashrc).
_ltproc()
 
{
 
local cur
 
   
COMPREPLY=()
 
cur=`_get_cword`
 
   
  +
Now open a new terminal and try typing
case "$cur" in
 
  +
* "apertium" and press TAB twice, you should get a list of installed language pairs
-*)
 
  +
* "apertium -d ." and press TAB twice should show you a list of the possible translation modes in this directory
COMPREPLY=( $( compgen -W '-a -c -g -n -d -p -s -t -z -w \
 
  +
* ."/autogen.sh" and press TAB twice and you should get --with-langN=../../languages/apertium-foo (if the pair has monolingual dependencies)
-v -h --analysis --case-sensitive --generation --non-marked-gen \
 
--debugged-gen --post-generation --sao --transliteration \
 
--null-flush --dictionary-case --version --help' -- $cur ) )
 
;;
 
*)
 
_filedir bin
 
;;
 
esac
 
 
return 0
 
}
 
complete -F _ltproc $filenames lt-proc
 
   
_ltcomp()
 
{
 
local cur
 
   
  +
{{TOCD}}
COMPREPLY=()
 
cur=`_get_cword`
 
   
case "$cur" in
 
*)
 
if [ $COMP_CWORD -eq 1 ]; then
 
COMPREPLY=( $( compgen -W 'lr rl' -- $cur ) )
 
elif [ $COMP_CWORD -eq 2 ]; then
 
_filedir '@(xml|dix)'
 
elif [ $COMP_CWORD -eq 3 ]; then
 
# of course, we _could_ only remove automorf.bin or
 
# autogen.bin depending on lr and rl, or even find
 
# nn-nb/nb-nn directions from that... but who calls
 
# lt-comp manually anyway?
 
_filedir bin
 
fi
 
# no reason to offer more arguments than this, right?
 
;;
 
esac
 
 
return 0
 
}
 
complete -F _ltcomp $filenames lt-comp
 
   
_ltexpand()
 
{
 
local cur
 
   
  +
A little recording showing off the tips on this page: https://asciinema.org/a/11111
COMPREPLY=()
 
cur=`_get_cword`
 
   
  +
case "$cur" in
 
  +
==apertium -d . takes too long to type==
*)
 
  +
PROTIP: If you add
if [ $COMP_CWORD -eq 1 ]; then
 
  +
_filedir '@(xml|dix)'
 
  +
<pre>"\e\C-d": "apertium -d . \t\t"</pre>
elif [ $COMP_CWORD -eq 2 ]; then
 
  +
_filedir bin
 
  +
to ~/.inputrc you can simply type "alt+ctrl+d" and your terminal will fill out "apertium -d . " and press TAB twice (showing the list of modes of your current directory).
fi
 
  +
# no reason to offer more arguments than this, right?
 
  +
==I checked out all of SVN – how do I quickly cd to apertium-lol-wat?==
;;
 
  +
esac
 
  +
Save https://gist.github.com/unhammer/f5ade1c20f0f5d653b8c to e.g. ~/src/apertium-ca.sh, then put "source ~/src/apertium-ca.sh" in ~/.bashrc
 
  +
return 0
 
  +
Now you can type "ca nno" to go to languages/apertium-nno and "ca kir kaz" to go to nursery/apertium-kaz-kir – even though you typed it in the other order.
  +
  +
  +
== I'm sicking of typing "cd ../../languages/apertium-lol; svn up; cd ../apertium-wat; svn up; cd ../../incubator/apertium-lol-wat" ==
  +
If you've got a language pair that depends on monolingual [[Languages|languages]]-modules, you can put the following function in your ~/.bashrc, open a new terminal and then just type "up" in the language pair to update the dependents as well:
  +
  +
<pre>
  +
up ()
  +
{
  +
grep ^AP_SRC config.log 2> /dev/null | while IFS='=' read -r var dir; do
  +
printf "%s\t" "${var}";
  +
( [[ -z "$1" ]] && cd "${dir//\'}" && up last )
  +
done;
  +
if svn info &> /dev/null; then
  +
printf "%s\t%s\t" svn "$(pwd)";
  +
svn up;
  +
else
  +
if git config --get svn-remote.svn.fetch &> /dev/null; then
  +
printf "%s\t%s\t" git-svn "$(pwd)";
  +
git svn rebase;
  +
else
  +
if git config --get core.bare &> /dev/null; then
  +
printf "%s\t%s\t" git "$(pwd)";
  +
git pull;
  +
else
  +
echo "No repo found" 1>&2;
  +
fi;
  +
fi;
  +
fi
 
}
 
}
complete -F _ltexpand $filenames lt-expand
 
 
</pre>
 
</pre>
   
  +
This script works even if you used git or git-svn for any of the dependents :-)
<pre>
 
# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
 
# ex: ts=8 sw=8 noet filetype=sh
 
#
 
# apertium completion by Kevin Brubeck Unhammer <unhammer@gmail.com>
 
# Licence: GNU GPL v2
 
   
  +
==Can I get syntax highlighting of apertium output?==
_apertium()
 
  +
Like
{
 
local cur prev
 
   
  +
[[File:Ap_colour.png]]
COMPREPLY=()
 
cur=`_get_cword`
 
prev=${COMP_WORDS[COMP_CWORD-1]}
 
   
  +
? Sure. Put the following in your ~/.bashrc
case "$prev" in
 
  +
<pre>
-d)
 
  +
__ap_colour () {
_filedir -d
 
  +
gawk '
;;
 
-m)
+
BEGIN {
  +
white="\033[0;1m"
_filedir tmx
 
  +
GREEN="\033[1;32m"
;;
 
  +
green="\033[0;32m"
-f)
 
  +
yellow="\033[0;33m"
COMPREPLY=( $( compgen -W 'txt html rtf odt docx wxml \
 
  +
grey="\033[0;37m"
xlsx pptx xpresstag wikimedia' -- $cur ) )
 
  +
redbg="\033[0;41m"
;;
 
  +
yellowbg="\033[0;43m"
-o)
 
  +
none="\033[00m"
COMPREPLY=( $( compgen -W "$(eval apertium -l)" -- $cur ) )
 
  +
}
;;
 
*)
+
{
  +
$0 = gensub(/#([^ <]+)/, redbg"#\\1"none, "g")
case "$cur" in
 
  +
$0 = gensub(/(^| )@([^ <]+)/, "\\1"yellowbg"@\\2"none, "g")
-*)
 
  +
$0 = gensub(/[*]([^ <]+)/, white"*\\1"none, "g")
COMPREPLY=( $( compgen -W '-d -f -a -u -m -o -l' -- $cur ) )
 
  +
$0 = gensub(/([^>])<([^>]+)>/, "\\1<"GREEN"\\2"none">", "g")
;;
 
  +
$0 = gensub(/><(@[^>]+)/, "><"yellow"\\1"none, "g")
*)
 
local dirs=$(eval apertium -l)
+
$0 = gensub(/ (@[^ ]+)/, " "yellow"\\1"none, "g")
  +
$0 = gensub(/><([^>]+)/, "><"green"\\1"none, "g")
local dirpat=$( echo $dirs | sed 's/ /|/g' )
 
  +
$0 = gensub(/[<>{}^$\/]/, grey"\\0"none, "g")
local langset=0
 
  +
print
# look through previous words and see if we've
 
  +
}'
# set the language already
 
  +
}
for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
 
  +
do
 
  +
apertium () {
if [[ "${COMP_WORDS[i]}" == @($dirpat) ]]; then
 
  +
if [[ ${TERM} != dumb && -t 1 ]]; then
# ignore -o-prefixed language code
 
  +
command apertium "$@" | __ap_colour
if [[ "${COMP_WORDS[i-1]}" != "-o" ]]; then
 
  +
else
langset=1
 
  +
command apertium "$@"
fi
 
fi
+
fi
done
 
if [ $langset -eq 1 ]; then
 
_filedir
 
else
 
COMPREPLY=( $( compgen -W "$dirs" -- $cur ) )
 
fi
 
;;
 
# direction should only happen once!
 
esac
 
esac
 
 
return 0
 
 
}
 
}
complete -F _apertium $filenames apertium
 
 
</pre>
 
</pre>
  +
  +
It's a bash function that wraps the apertium script and calls the regular apertium program, appending syntax highlighting if we're outputting to an interactive terminal.
  +
  +
If you want to colour stuff that's already been through apertium, just append <code>| __ap_colour</code>. For example, <code>grep stuff processed_corpus.txt | fmt -120 | __ap_colour</code>.
  +
  +
==External links==
  +
* https://github.com/unhammer/apertium-completion
  +
  +
[[Category:Tools]]
  +
[[Category:Bash]]
  +
[[Category:Installation]]
  +
[[Category:Documentation in English]]

Latest revision as of 15:27, 27 April 2017

If you want “intelligent” bash completion on TAB for apertium, lttoolbox, vislcg3 and hfst, do e.g.:

$ git clone https://github.com/unhammer/apertium-completion.git ~/apertium-completion

and add this to your ~/.bashrc:

if ! shopt -oq posix && \
   [[ ( -z "$INSIDE_EMACS" || "$EMACS_BASH_COMPLETE" = "t" ) ]]; then
    for f in ~/apertium-completion/completions/*; do
        [[ -f $f ]] && source "$f"
    done
fi

Alternatively, if your OS sources files from e.g. /etc/bash_completion_d automatically, just sudo cp ~/apertium-completion/completions/* /etc/bash_completion_d/ (then you don’t have to add anything to ~/.bashrc).


Now open a new terminal and try typing

  • "apertium" and press TAB twice, you should get a list of installed language pairs
  • "apertium -d ." and press TAB twice should show you a list of the possible translation modes in this directory
  • ."/autogen.sh" and press TAB twice and you should get --with-langN=../../languages/apertium-foo (if the pair has monolingual dependencies)



A little recording showing off the tips on this page: https://asciinema.org/a/11111


apertium -d . takes too long to type[edit]

PROTIP: If you add

"\e\C-d": "apertium -d . \t\t"

to ~/.inputrc you can simply type "alt+ctrl+d" and your terminal will fill out "apertium -d . " and press TAB twice (showing the list of modes of your current directory).

I checked out all of SVN – how do I quickly cd to apertium-lol-wat?[edit]

Save https://gist.github.com/unhammer/f5ade1c20f0f5d653b8c to e.g. ~/src/apertium-ca.sh, then put "source ~/src/apertium-ca.sh" in ~/.bashrc

Now you can type "ca nno" to go to languages/apertium-nno and "ca kir kaz" to go to nursery/apertium-kaz-kir – even though you typed it in the other order.


I'm sicking of typing "cd ../../languages/apertium-lol; svn up; cd ../apertium-wat; svn up; cd ../../incubator/apertium-lol-wat"[edit]

If you've got a language pair that depends on monolingual languages-modules, you can put the following function in your ~/.bashrc, open a new terminal and then just type "up" in the language pair to update the dependents as well:

up () 
{ 
    grep ^AP_SRC config.log 2> /dev/null | while IFS='=' read -r var dir; do
        printf "%s\t" "${var}";
	( [[ -z "$1" ]] && cd "${dir//\'}" && up last )
    done;
    if svn info &> /dev/null; then
        printf "%s\t%s\t" svn "$(pwd)";
        svn up;
    else
        if git config --get svn-remote.svn.fetch &> /dev/null; then
            printf "%s\t%s\t" git-svn "$(pwd)";
            git svn rebase;
        else
            if git config --get core.bare &> /dev/null; then
                printf "%s\t%s\t" git "$(pwd)";
                git pull;
            else
                echo "No repo found" 1>&2;
            fi;
        fi;
    fi
}

This script works even if you used git or git-svn for any of the dependents :-)

Can I get syntax highlighting of apertium output?[edit]

Like

Ap colour.png

? Sure. Put the following in your ~/.bashrc

__ap_colour () {
    gawk '
    BEGIN {
        white="\033[0;1m"
        GREEN="\033[1;32m"
        green="\033[0;32m"
        yellow="\033[0;33m"
        grey="\033[0;37m"
        redbg="\033[0;41m"
        yellowbg="\033[0;43m"
        none="\033[00m"
    }
    {
        $0 = gensub(/#([^ <]+)/, redbg"#\\1"none, "g")
        $0 = gensub(/(^| )@([^ <]+)/, "\\1"yellowbg"@\\2"none, "g")
        $0 = gensub(/[*]([^ <]+)/, white"*\\1"none, "g")
        $0 = gensub(/([^>])<([^>]+)>/, "\\1<"GREEN"\\2"none">", "g")
        $0 = gensub(/><(@[^>]+)/, "><"yellow"\\1"none, "g")
        $0 = gensub(/ (@[^ ]+)/, " "yellow"\\1"none, "g")
        $0 = gensub(/><([^>]+)/, "><"green"\\1"none, "g")
        $0 = gensub(/[<>{}^$\/]/, grey"\\0"none, "g")
        print
    }'
}

apertium () {
    if [[ ${TERM} != dumb && -t 1 ]]; then
        command apertium "$@" | __ap_colour
    else
        command apertium "$@"
    fi
}

It's a bash function that wraps the apertium script and calls the regular apertium program, appending syntax highlighting if we're outputting to an interactive terminal.

If you want to colour stuff that's already been through apertium, just append | __ap_colour. For example, grep stuff processed_corpus.txt | fmt -120 | __ap_colour.

External links[edit]