aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/_text_filenames.bash
blob: 9cc1c722ae0319661bcf75fc8a8868eb098c75f9 (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
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
# Exclude files by filesystem type and extension that likely aren't
# viewable/editable in plain text
#
# I've seen some very clever people figure out ways to actually read the files
# or run something like file(1) over them to make an educated guess as to
# whether they're binary or not, but I don't really want to go that far. It's
# not supposed to be perfect, just a bit more likely to complete singly with
# the thing I want, and I want it to stay fast.
#
_text_filenames() {
    local item
    while IFS= read -r item ; do

        # Exclude blanks
        [[ -n $item ]] || continue

        # Exclude nonexistent (some sort of error)
        [[ -e $item ]] || continue

        # Exclude files with block, character, pipe, or socket type
        ! [[ -b $item ]] || continue
        ! [[ -c $item ]] || continue
        ! [[ -p $item ]] || continue
        ! [[ -S $item ]] || continue

        # Accept directories
        if [[ -d $item ]] ; then
            COMPREPLY[${#COMPREPLY[@]}]=$item
            continue
        fi

        # Check the filename extension to know what to exclude
        (
            # Case-insensitive matching available since 3.1-alpha
            shopt -qs nocasematch 2>/dev/null

            # Match against known binary patterns
            case $item in

                # Archives
                (*.7z) ;;
                (*.bz2) ;;
                (*.gz) ;;
                (*.jar) ;;
                (*.rar) ;;
                (*.tar) ;;
                (*.xz) ;;
                (*.zip) ;;

                # Bytecode
                (*.class) ;;
                (*.pyc) ;;

                # Databases
                (*.db) ;;
                (*.dbm) ;;
                (*.sdbm) ;;
                (*.sqlite) ;;

                # Disk
                (*.adf) ;;
                (*.bin) ;;
                (*.hdf) ;;
                (*.iso) ;;

                # Documents
                (*.docx) ;;
                (*.djvu) ;;
                (*.odp) ;;
                (*.ods) ;;
                (*.odt) ;;
                (*.pdf) ;;
                (*.ppt) ;;
                (*.xls) ;;
                (*.xlsx) ;;

                # Encrypted
                (*.asc) ;;
                (*.gpg) ;;

                # Executables
                (*.exe) ;;

                # Fonts
                (*.ttf) ;;

                # Images
                (*.bmp) ;;
                (*.gd2) ;;
                (*.gif) ;;
                (*.ico) ;;
                (*.jpeg) ;;
                (*.jpg) ;;
                (*.pbm) ;;
                (*.png) ;;
                (*.psd) ;;
                (*.tga) ;;
                (*.xbm) ;;
                (*.xcf) ;;
                (*.xpm) ;;

                # Incomplete
                (*.filepart) ;;

                # Objects
                (*.a) ;;
                (*.o) ;;

                # Sound
                (*.au) ;;
                (*.aup) ;;
                (*.flac) ;;
                (*.mid) ;;
                (*.m4a) ;;
                (*.mp3) ;;
                (*.ogg) ;;
                (*.opus) ;;
                (*.s3m) ;;
                (*.wav) ;;

                # System-specific
                (.DS_Store) ;;

                # Translation
                (*.gmo) ;;

                # Version control
                (.git) ;;
                (.hg) ;;
                (.svn) ;;

                # Video
                (*.avi) ;;
                (*.gifv) ;;
                (*.mp4) ;;
                (*.ogv) ;;
                (*.rm) ;;
                (*.swf) ;;
                (*.webm) ;;

                # Vim
                (*~) ;;
                (*.swp) ;;

                # Not binary that we can tell; maybe editable
                (*) exit 0 ;;

            esac

            # Known usually-binary extension; flag failure
            exit 1

        ) || continue

        # Complete everything else; some of it will still be binary
        COMPREPLY[${#COMPREPLY[@]}]=$item

    done < <(compgen -A file -- "${COMP_WORDS[COMP_CWORD]}")
}