9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

multi.c (1844B)


      1 #include "sam.h"
      2 
      3 List	file = { 'p' };
      4 ushort	tag;
      5 
      6 File *
      7 newfile(void)
      8 {
      9 	File *f;
     10 
     11 	f = fileopen();
     12 	inslist(&file, 0, f);
     13 	f->tag = tag++;
     14 	if(downloaded)
     15 		outTs(Hnewname, f->tag);
     16 	/* already sorted; file name is "" */
     17 	return f;
     18 }
     19 
     20 int
     21 whichmenu(File *f)
     22 {
     23 	int i;
     24 
     25 	for(i=0; i<file.nused; i++)
     26 		if(file.filepptr[i]==f)
     27 			return i;
     28 	return -1;
     29 }
     30 
     31 void
     32 delfile(File *f)
     33 {
     34 	int w = whichmenu(f);
     35 
     36 	if(w < 0)	/* e.g. x/./D */
     37 		return;
     38 	if(downloaded)
     39 		outTs(Hdelname, f->tag);
     40 	dellist(&file, w);
     41 	fileclose(f);
     42 }
     43 
     44 void
     45 fullname(String *name)
     46 {
     47 	if(name->n > 0 && name->s[0]!='/' && name->s[0]!=0)
     48 		Strinsert(name, &curwd, (Posn)0);
     49 }
     50 
     51 void
     52 fixname(String *name)
     53 {
     54 	String *t;
     55 	char *s;
     56 
     57 	fullname(name);
     58 	s = Strtoc(name);
     59 	if(strlen(s) > 0)
     60 		s = cleanname(s);
     61 	t = tmpcstr(s);
     62 	Strduplstr(name, t);
     63 	free(s);
     64 	freetmpstr(t);
     65 
     66 	if(Strispre(&curwd, name))
     67 		Strdelete(name, 0, curwd.n);
     68 }
     69 
     70 void
     71 sortname(File *f)
     72 {
     73 	int i, cmp, w;
     74 	int dupwarned;
     75 
     76 	w = whichmenu(f);
     77 	dupwarned = FALSE;
     78 	dellist(&file, w);
     79 	if(f == cmd)
     80 		i = 0;
     81 	else{
     82 		for(i=0; i<file.nused; i++){
     83 			cmp = Strcmp(&f->name, &file.filepptr[i]->name);
     84 			if(cmp==0 && !dupwarned){
     85 				dupwarned = TRUE;
     86 				warn_S(Wdupname, &f->name);
     87 			}else if(cmp<0 && (i>0 || cmd==0))
     88 				break;
     89 		}
     90 	}
     91 	inslist(&file, i, f);
     92 	if(downloaded)
     93 		outTsS(Hmovname, f->tag, &f->name);
     94 }
     95 
     96 void
     97 state(File *f, int cleandirty)
     98 {
     99 	if(f == cmd)
    100 		return;
    101 	f->unread = FALSE;
    102 	if(downloaded && whichmenu(f)>=0){	/* else flist or menu */
    103 		if(f->mod && cleandirty!=Dirty)
    104 			outTs(Hclean, f->tag);
    105 		else if(!f->mod && cleandirty==Dirty)
    106 			outTs(Hdirty, f->tag);
    107 	}
    108 	if(cleandirty == Clean)
    109 		f->mod = FALSE;
    110 	else
    111 		f->mod = TRUE;
    112 }
    113 
    114 File *
    115 lookfile(String *s)
    116 {
    117 	int i;
    118 
    119 	for(i=0; i<file.nused; i++)
    120 		if(Strcmp(&file.filepptr[i]->name, s) == 0)
    121 			return file.filepptr[i];
    122 	return 0;
    123 }