summaryrefslogtreecommitdiff
path: root/support/brief-t/sort.cb
diff options
context:
space:
mode:
Diffstat (limited to 'support/brief-t/sort.cb')
-rw-r--r--support/brief-t/sort.cb157
1 files changed, 157 insertions, 0 deletions
diff --git a/support/brief-t/sort.cb b/support/brief-t/sort.cb
new file mode 100644
index 0000000000..33d4fed19a
--- /dev/null
+++ b/support/brief-t/sort.cb
@@ -0,0 +1,157 @@
+/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
+/* */
+/* P†l Hedne 1991 */
+/* */
+/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
+sort (...)
+{
+ string pattern,text,word;
+ int line,col,line1,col1,linex,colx,mtype,wlength,ind;
+ int buf_key,scratch_key;
+ int i,j,wsum,linesum,linesummin,iline,nline;
+
+ pause_on_error(0);
+ scratch_key=create_buffer("Kladd ",NULL,1);
+/* error("scratch_key = %d",scratch_key);*/
+ buf_key=inq_buffer();
+
+ inq_position (line, col);
+ mtype = inq_marked (line1, col1, linex, colx);
+ if (mtype > 0)
+ {
+ pattern="";
+ text="Sort on word no.: ";
+ if (get_parm (0, pattern, text, NULL,pattern))
+ {
+ ind=atoi(pattern);
+ sprintf(text,"Sorting on column %d...",ind);
+ message(text);
+/* error("Sorting marked block on word %d",ind);*/
+ for (j=line1; j<=linex; ++j)
+ {
+ move_abs(j,col1);
+ text=read();
+ word=get_word(text,ind);
+ wlength=strlen(word);
+/* error(" Word returned = %s",word);*/
+ wsum=0;
+ if (wlength>4) wlength=4;
+ for (i=1; i<=wlength; ++i)
+ {
+/* error("sub=%s,atoi=%d",substr(word,i,1),atoi(substr(word,i,1),0));*/
+ wsum=wsum+exp127(atoi(substr(word,i,1),0),5-i);
+/* error(" Wsum(%d) = %d",i,wsum);*/
+ }
+/* error(" Wsum = %d",wsum);*/
+ set_buffer(scratch_key);
+ end_of_buffer();
+ sprintf(text,"%d\n",wsum);
+ insert(text);
+ set_buffer(buf_key);
+ }
+ nline=linex-line1+1;
+ set_buffer(scratch_key);
+ for (i=1; i<=nline; ++i)
+ {
+ linesummin=2147483647;
+ iline=i;
+ move_abs(i,1);
+ sprintf(text,"%d/%d done",i,nline);
+ message(text);
+ for (j=i; j<=nline; ++j)
+ {
+ text=read();
+ linesum=atoi(text);
+/* error("Line=%d sum=%d min=%d",j,linesum,linesummin);*/
+ if (linesum<linesummin)
+ {linesummin=linesum;
+ iline=j;
+ }
+ move_rel(1,0);
+ }
+ if (iline>i)
+ {
+ swap_lines(i,iline);
+ set_buffer(buf_key);
+ swap_lines(line1+i-1,line1+iline-1);
+ set_buffer(scratch_key);
+ }
+ }
+ set_buffer(buf_key);
+ }
+ }
+ pause_on_error(0);
+ move_abs(line,col);
+ message(" ");
+}
+/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
+get_word(string text, int wno)
+{
+ string mword;
+ int ind,wlength;
+ int wcount=0;
+
+ text=ltrim(text);
+/* error("Index = %d Text = %s",ind,text);*/
+ do
+ {
+ ind=search_string("[\t\n, ]",text,0,1,0);
+/* error("Ind1 = %d text = %s",ind,text);*/
+ if (ind>0)
+ {
+ ++wcount;
+ if (wcount==wno)
+ {
+ mword=substr(text,1,ind-1);
+ return(mword);
+ }
+ text=substr(text,ind);
+/* error("Word no. %d = %s",wcount,mword);*/
+
+ ind=search_string("[~\t\n, ]",text,0,1,0);
+/* error("Ind2 = %d text = %s",ind,text);*/
+ text=substr(text,ind);
+ }
+ }
+ while((wcount<wno)&&(ind>0));
+ return(" ");
+}
+/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
+exp127(int num, int exp)
+{
+ int i;
+ int val=1;
+
+ for (i=1; i<exp; ++i)
+ {
+ val=val*127;
+/* error(" val(%d) = %d",i,val);*/
+ }
+ num=num*val;
+ return num;
+}
+/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
+swap_lines(int line1, int line2)
+{
+ string text1,text2;
+ int line,col;
+
+ inq_position (line, col);
+
+ move_abs(line1,1);
+ text1=read();
+ text1=trim(text1);
+ delete_to_eol();
+
+ move_abs(line2,1);
+ text2=read();
+ text2=trim(text2);
+ delete_to_eol();
+
+ move_abs(line2,1);
+ insert(text1);
+ move_abs(line1,1);
+ insert(text2);
+
+ move_abs(line,col);
+}