File: menu-tree.cc

package info (click to toggle)
menu 2.1.5-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,476 kB
  • ctags: 1,251
  • sloc: cpp: 6,222; ansic: 2,306; sh: 453; makefile: 296; sed: 93
file content (369 lines) | stat: -rw-r--r-- 9,748 bytes parent folder | download
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
#include "menu-tree.h"
#include "install-menu.h"
#include "hints.h"
#include <ctype.h>
#include <list>
#include <set>

bool operator< (const StrVec &left, const StrVec &right){
  cStrVec::iterator i,j;

  for(i=left.begin(), j=right.begin();
      (i!=left.end()) && (j!=right.end());
      i++, j++){
    if((*i)<(*j))
      return true;
    else if((*i)>(*j))
      return false;
  }
  if(j==right.end())
    return false;
  else
    return true;
}


void menuentry::add_entry(StrVec s,
			  map <String, String, less<String> > &v){
  submenu_container::iterator f,firstsec_i;
  StrVec subs;
  StrVec firstsec;
  cStrVec::iterator i;
  bool use_forced=false;
  
  //there are two ways to store the menuentries in the `tree'
  // - `flat', all menuentries in the toplevel submenus map.
  //   This is used when working with `hints'. In this case, we
  //   will later sort out the tree.
  // - as a true tree.
  //However, even in the `flat', hint-processing case, if there already
  //exists a submenu with exactly the right s[0], then we do desent,
  //if it has a v[FORCED_VAR] set.

  firstsec.push_back(s[0]);
  firstsec_i=submenus.find(firstsec);

  if(firstsec_i!=submenus.end())
    if((*firstsec_i).second->forced)
      use_forced=true;

  if(((!config->hint_optimize)||use_forced)&&
     (s.size()>1)){
    
    for(i=s.begin()+1;i!=s.end(); i++)
      if((*i).size())
	subs.push_back((*i));
    
    if(firstsec_i==submenus.end()){
      submenus[firstsec]=new menuentry;
      submenus[firstsec]->section=subs;
      (submenus[firstsec]->vars)[TITLE_VAR]=firstsec[0];
    }
    submenus[firstsec]->add_entry(subs,v);
  }else {
    f=submenus.find(s);
    if(f==submenus.end()){
      menuentry *m;
      m=new menuentry;
      submenus[s]=m;
      m->section=s;
      m->vars=v;
    }
  }
}

void menuentry::add_menuentry_ptr(StrVec s,
				  menuentry *m){
  submenu_container::iterator f;
  StrVec subs,firstsec;
  cStrVec::iterator i;

  if(s.size()>1){
    for(i=s.begin()+1;i!=s.end(); i++)
      if((*i).size())
	subs.push_back((*i));
    
    firstsec.push_back(s[0]);
    f=submenus.find(firstsec);
    if(f==submenus.end()){
      submenus[firstsec]=new menuentry;
      submenus[firstsec]->section=subs;
      submenus[firstsec]->forced=m->forced;
      (submenus[firstsec]->vars)[TITLE_VAR]=firstsec[0];
    }
    submenus[firstsec]->add_menuentry_ptr(subs,m);
  }else {
    f=submenus.find(s);
    if(f==submenus.end())
      submenus[s]=m;
  }
}

void menuentry::output(){
  String treew=config->treewalk();
  //  map <String, menuentry *, less<String> >::iterator i;
  submenu_container::iterator j;
  multimap <String, menuentry *, less<String> > sorted;
  multimap <String, menuentry *, less<String> >::iterator i;

  
  //sort the submenus in sorted:
  for(j=submenus.begin(); j!=submenus.end(); j++){
    String s;
    if(config->sort)
      s=config->sort->soutput((*j).second->vars);
    else
      s=(*j).second->vars[TITLE_VAR];
    sorted.insert(pair<String,menuentry *>(s,(*j).second));
  };

  for(string::size_type j=0;j<treew.length(); j++){
    bool children_too=false;
    switch(treew[j]){
    case 'c':
      for(i=sorted.begin(); i!=sorted.end(); i++)
	if((*i).second->submenus.size())
	  (*i).second->output();
      break;
    case '(':
      if(submenus.size())
	if(config->startmenu&& testuniqueness(vars))
	  config->startmenu->output(vars);
      break;
    case ')':
      if(submenus.size())
	if(config->endmenu && testuniqueness(vars))
	  config->endmenu->output(vars);
      break;
    case 'M':
      children_too=true;
    case 'm':;
      for(i=sorted.begin(); i!=sorted.end(); i++)
        if(((*i).second->vars[COMMAND_VAR].length())&&
	   testuniqueness((*i).second->vars))
          supported->subst((*i).second->vars);
        else{
	  if((config->submenutitle)&&
	     ((*i).second->submenus.size())&&
	     testuniqueness((*i).second->vars))
	    config->submenutitle->output((*i).second->vars);
          if(children_too)
            (*i).second->output();
        }
    }
  }
}

void menuentry::store_hints(){
  /* put the $hint variables contents in the hint of
     the submenu[] map. For submenu entries (without a command),
     put the $hint variable in all menuentries that lie below
     that one.
  */
  submenu_container::iterator i,j;
  cStrVec::iterator k;
  unsigned int l;

  // Make sure menuhints are empty:
  for(i=submenus.begin(); i!=submenus.end(); i++){
    (*i).second->menuhints.erase((*i).second->menuhints.begin(),
				 (*i).second->menuhints.end());
  }

  for(i=submenus.begin(); i!=submenus.end(); i++){
    const String &hs=(*i).second->vars[HINTS_VAR];

    if(hs.size() != 0){
      StrVec h;
      
      break_commas(hs,h);
      j=i;
      do{
	for(k=h.begin(); k!=h.end(); k++)
	  (*j).second->menuhints.push_back(*k);
	j++;
	for(l=0; (l!=(*i).first.size());  l++)
	  if((*i).first[l]!=(*j).first[l])
	    break;
      } while (l==(*i).first.size());
    }
  }  
}


void menuentry::process_hints(const StrVec &pref){
  vector <StrVec> hint_list, hint_out;
  vector <const StrVec>::iterator k,m;
  cStrVec::iterator l;
  submenu_container::iterator i;
  submenu_container oldsubmenus;
  hints h;

  // first, process hints of children.
  for(i=submenus.begin(); i!=submenus.end(); i++){
    if(((*i).second->vars[COMMAND_VAR]).size()==0){
      StrVec v(pref);
      cStrVec::iterator j;
      for(j=(*i).first.begin(); j!=(*i).first.end(); j++)
	v.push_back((*j));
      (*i).second->process_hints(v);
    }
  }
  store_hints();

  for(i=submenus.begin(); i!=submenus.end(); i++){
    //if(((*i).second->vars[COMMAND_VAR]).size()!=0){
      StrVec v;
      
      for(l=(*i).first.begin(); l!=(*i).first.end(); l++)
	v.push_back(*l);
      //if(((*i).second->vars[COMMAND_VAR]).size()!=0)
      if(v.size()>1){
	v.pop_back();
	if(config->hint_debug)
	  cout<<"Adding to hint_list: "<<(*i).first
	      <<", hints="<<(*i).second->menuhints<<endl;
	for(l=(*i).second->menuhints.begin(); 
	    l!=(*i).second->menuhints.end(); 
	    l++)
	  v.push_back(*l);
	hint_list.push_back(v);
      } else{
	StrVec empty; // to make sure hint_list[i] and submenus[i] match
	hint_list.push_back(empty);
      }
      //}
  }
  h.set_nentry(      config->hint_nentry);
  h.set_topnentry(config->hint_topnentry);
  h.set_mixedpenalty(config->hint_mixedpenalty);
  h.set_minhintfreq(config->hint_minhintfreq);
  h.set_max_local_penalty(config->hint_mlpenalty);
  h.set_max_ntry(config->hint_max_ntry);
  h.set_max_iter_hint(config->hint_max_iter_hint);
  h.set_debug(      config->hint_debug);

  h.calc_tree(hint_list,hint_out);

  oldsubmenus=submenus;
  submenus.erase(submenus.begin(), submenus.end());

  for(k=hint_list.begin(), m=hint_out.begin(), i=oldsubmenus.begin(); 
      k!=hint_list.end(); 
      k++, m++, i++){
    StrVec v;

    v=(*m);
    v.push_back((*i).first[(*i).first.size()-1]); // restore title

    add_menuentry_ptr(v,(*i).second);
  }
}

void menuentry::postprocess(int n_parent, int level, String prev){
  // Postprocess will set or correct some variables in the
  // menuentry tree (vars), that were not known at the time of
  // creation of the menuentry classes (due to hints, or other reasons)
  //
  // n_parent: number of elements in menu of parent
  // level: how deep this entry is nested in menu tree.

  submenu_container::iterator i;
  int index;

  vars[PRIVATE_LEVEL_VAR]=itoString(level);
  if(!level)
    vars[SECTION_VAR]=prev;
  for(i=submenus.begin(), index=0; 
      i!=submenus.end(); 
      i++, index++){
    String title;
    cStrVec::iterator j;

    title=prev;
    for(j=(*i).first.begin(); j!=(*i).first.end(); j++)
      title += String("/") + (*j);

    (*i).second->vars[SECTION_VAR]=title;

    (*i).second->vars[PRIVATE_ENTRYINDEX_VAR]=itoString(index);
    
    if((*i).second->submenus.size())
      (*i).second->postprocess(submenus.size(), level+1, title);
    else {
      (*i).second->vars[PRIVATE_ENTRYCOUNT_VAR]=itoString(submenus.size());
      (*i).second->vars[PRIVATE_LEVEL_VAR]=itoString(level+1);
    }
  }

  generate_hotkeys();
  vars[PRIVATE_ENTRYCOUNT_VAR]=itoString(n_parent);
}

char menuentry::hotkeyconv(char h){
  if (config->hotkeycase)
    return h;
  else
    return tolower(h);
}
void menuentry::generate_hotkeys(){
  string::size_type i,j;
  list<int>::iterator k, old_k;
  submenu_container::iterator subi;
  map <String, String, less<String> >::iterator l;
  StrVec keys;
  list<int> todo;
  set<char, less<char> > used_chars;
  String s;
  String str0;
  char c;

  str0="0";
  str0[0]='\0';
  if(config->hkexclude)
    s=config->hkexclude->soutput(vars);
  for(i=0;i!=s.length();i++)
    used_chars.insert(hotkeyconv(s[i]));

  for(subi=submenus.begin(), i=0; subi!=submenus.end(); subi++, i++){
    todo.push_back(i);
    l=(*subi).second->vars.find(HOTKEY_VAR);
    if(l!=(*subi).second->vars.end())
      keys.push_back((*l).second);
    else
      keys.push_back(str0);
    keys[i]+=sort_hotkey((*subi).second->vars[TITLE_VAR]);
  }
  j=0;
  while(todo.size()){
    for(k=todo.begin();k!=todo.end();){
      i=*k; 
      old_k=k++; //k++ here, to be able to todo.erase(old_k) safely.
      if(j>=keys[i].length()){
	keys[i]=str0;
	todo.erase(old_k);  //no hotkey found -- give up on this entry.
	continue;
      }
      c=keys[i][j];
      if(c){
	if(used_chars.find(hotkeyconv(c))==used_chars.end()){
	  todo.erase(old_k); //found a hotkey for this entry.
	  keys[i]=String("")+c;
	  used_chars.insert(hotkeyconv(c));
	  continue;
	}
	else
	  keys[i].replace(j,1,str0);
      }
    }
    j++;
  }
  for(subi=submenus.begin(), i=0; subi!=submenus.end(); subi++, i++){
    c=keys[i][0];
    if(c){
      (*subi).second->vars[HOTKEY_VAR]=String("")+c;
    }
  }
}