Debian Codegraph @

share/_frama-c

raw
1 #compdef frama-c frama-c-gui frama-c.byte frama-c-gui.byte
2 ##########################################################################
3 # #
4 # This file is part of Frama-C. #
5 # #
6 # Copyright (C) 2007-2022 #
7 # CEA (Commissariat à l'énergie atomique et aux énergies #
8 # alternatives) #
9 # #
10 # you can redistribute it and/or modify it under the terms of the GNU #
11 # Lesser General Public License as published by the Free Software #
12 # Foundation, version 2.1. #
13 # #
14 # It is distributed in the hope that it will be useful, #
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
17 # GNU Lesser General Public License for more details. #
18 # #
19 # See the GNU Lesser General Public License version 2.1 #
20 # for more details (enclosed in the file licenses/LGPLv2.1). #
21 # #
22 ##########################################################################
23
24 # zsh completion for Frama-C
25 # ==========================
26 #
27 # Installation
28 # ============
29 #
30 # This file must be placed in a directory listed in the $fpath variable.
31 # You can add a directory to $fpath by adding a line like the following
32 # to your ~/.zshrc file:
33 #
34 # fpath=(~/newdir $fpath)
35 #
36 # It also works with relative paths, such as 'bin/frama-c'.
37 #
38 #
39 # The autocompletion can benefit from the caching system offered by zsh:
40 # `zstyle ':completion:*' use-cache on` to enable caching for all commands
41 # `zstyle ':completion:*:*:frama-c*:*' use-cache on` only for frama-c
42 #
43 # -----------------------------------------------------------------------------
44
45 # TODO:
46 # - use _call_program to call frama-c instead of calling frama-c directly ?
47 # - other ideas when to renew cache ?
48
49 #local curcontext="$curcontext" state state_descr line # expl ret=1 ??
50 #typeset -A opt_args
51
52 # filter_load takes a command line calling frama-c and
53 # removes everything not a -load-module or -load-script
54 # argument 1 is the variable name of the input
55 # argument 2 is the variable name of the output
56 function filter_load () {
57 local next=0
58 local -a my_args
59 my_args=(${(P)1[1]})
60 for w in ${(P)1}; do
61 if [[ $next -eq 1 ]]; then
62 my_args+=($w)
63 next=0
64 else
65 # very strange behaviour when ' is used instead of " around -load-*
66 # actually not related to this
67 if [[ $w = '-load-module' ]] || [[ $w = '-load-script' ]]; then
68 my_args+=("$w")
69 next=1
70 fi
71 fi
72 done
73 eval "$2=($my_args)"
74 }
75
76 function _frama_c () {
77 local ret=1 # the return value (1 if no autocompletion is done, 0 otherwise)
78
79 local -a my_words
80 my_words=($words)
81 my_words[1]=${my_words[1]/-gui} # call frama-c instead of frama-c-gui
82
83 # we do not waste our time on computation if we are not completing an option
84 if [[ -prefix -* ]]; then # if the first character of the current word is a '-'
85 # is the first word on the line executable ?
86 if $my_words[1] 2>/dev/null; then
87 local -a the_args
88 local -a the_previous_args
89 # we keep only parts of the command line relevant to -load-module/-load-script
90 filter_load my_words the_args
91 # we load the previous filtered command from cache if available
92 _retrieve_cache frama-c_previous_command # can overwrite the_previous_args
93 # some gymnastics because the name of the variable matters
94 local -a tmp
95 tmp=($the_previous_args)
96 the_previous_args=($the_args)
97 _store_cache frama-c_previous_command the_previous_args
98 the_previous_args=($tmp)
99
100 # if the time of the most recent modification in
101 # `frama-c -print-plugin-path` is not the same as the one
102 # in the cache, we deduce that it is not the same "frama-c"
103 # as before and recompute the cache.
104 # We put the new date in the cache and store
105 # this information in $recompute
106 local last_change
107 _retrieve_cache frama-c_last_change
108 zmodload -F zsh/stat b:zstat 2>/dev/null
109 local current_last_change=$(zstat +mtime $($my_words[1] -print-plugin-path)/**/*(.om[1]))
110 local recompute
111 (( recompute = $current_last_change != ${last_change:-0} ))
112 if (( $recompute )); then
113 last_change=$current_last_change
114 _store_cache frama-c_last_change last_change
115 fi
116
117 # if something in `frama-c -print-plugin-path` changed,
118 # if the filtered current command is different from the remembered one or
119 # if the cache is unavailable, recompute the list of options,
120 # otherwise just load the cache
121 if (( $recompute )) ||
122 [[ $the_args != $the_previous_args ]] ||
123 _cache_invalid frama-c_autocompletion ||
124 ! _retrieve_cache frama-c_autocompletion
125 then
126 local -a autocompletion
127 local autocomp
128 # call frama-c with all the -load-module ; if it fails, test without the load-modules ;
129 # if it fails again, abort
130 autocomp=$($the_args -autocomplete @all 2>/dev/null) || autocomp=$($my_words[1] -autocomplete @all 2>/dev/null) || unset autocomp
131 (( $+autocomp )) && autocompletion=($(grep -o "\-[^ ]*" <<< $autocomp | sort))
132 (( $#autocompletion )) || _message "$my_words[1] exists, but no option was detected"
133 _store_cache frama-c_autocompletion autocompletion
134 fi
135 _describe 'options' autocompletion && ret=0
136 else
137 _message "$my_words[1] not found, dynamic autocompletion aborted"
138 _files && ret=0 # defaults to _files
139 fi
140 else
141 # if we complete a file (not sure if '_files' is the best default)
142 _files && ret=0
143 fi
144 return $ret
145 }
146
147 # call _frama_c when autocompletion is requested
148 _frama_c "$@"