File: subpar.sl

package info (click to toggle)
jed-extra 2.5.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,972 kB
  • ctags: 2,490
  • sloc: makefile: 75; ruby: 43; sed: 38; sh: 31
file content (199 lines) | stat: -rw-r--r-- 4,591 bytes parent folder | download | duplicates (8)
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
% subpar.sl
% paragraph reformatter
% 
% $Id: subpar.sl,v 1.1 2004/05/27 21:24:49 paul Exp paul $
% Keywords: wp
%
% Copyright (c) 2004 Paul Boekholt.
% Released under the terms of the GNU GPL (version 2 or later).
% 
% Uses a dynamic programming algorithm like Adam Costello's "par" program.  It
% chooses linebreaks so that the paragraph satisfies the following properties:
%
%         1) No line contains more than <L> characters.
%
%         2) The sum of the squares of the differences between <L>
%	     and the lengths of the lines is as small as possible.

%!%+
%\variable{Par_Bullets}
%\synopsis{Bullet characters}
%\usage{String_Type Par_Bullets = "*\\-"}
%\description
%  The \var{par} paragraph formatter will avoid breaking a line at a bullet
%  character.  The '-' is escaped because this is used in a regexp character
%  list.
%\seealso{par}
%!%-
custom_variable("Par_Bullets", "*\\-");

require("comments");

% Get the spaces on this line, and return their positions on the stack,
% skipping double spaces.  Avoid possible line breaks at comment characters.
static define get_spaces(comment_chars)
{
   variable line = line_as_string, pos = 0, len;

   % Skip indentation of first line
   if (string_match(line, "^ +", 1))
     (,pos) = string_match_nth(0);
   pos++;

   variable re = sprintf("\\( +\\)[^ %s%s]", Par_Bullets, comment_chars);
   while (string_match(line, re, pos))
     {
	(pos ,len) = string_match_nth(1);
	if (len == 1)
	  pos;
	pos += len + 1;
     }
   strlen(line);
}

static variable indent;

% Chooses line breaks in a list of words which maximize the sum of squares
% of differences between line lengths and the maximum line length.  Pushes
% the locations of linebreaks on the stack and returns the number of
% linebreaks.  This is actually more like Costello's simplebreaks() than his
% normalbreaks().
static define normalbreaks(s)
{
   variable i = length(s), j;
   variable L = WRAP - indent;
   if (i < 2) return 0;
   variable wordlengths = s - [indent-1, s[[0:i-2]]]; % these are actually wordlengths + 1
   variable linelen, score, scores = Integer_Type[i], next = Integer_Type[i];

   next[*] = -1;
   
   for (i--, linelen = wordlengths[i] - 1;
	i !=-1 and linelen <= L;
	i--, linelen += wordlengths[i])  ;
   
   variable scores_i, next_i;
   for ( ;  i != -1;  i--)
     {
	scores_i = 0;
	for (linelen = wordlengths[i] - 1, j = i, j++;
	   linelen <= L;
	   linelen += wordlengths[j],  j++)
	  {
	     score = scores[j] + sqr(L - linelen);
	     if (orelse {score < scores_i}{not scores_i} )
	       {
		  next_i = j;
		  scores_i = score;
	       }
	  }
	scores[i] = scores_i;
	next[i] = next_i;
     }

   variable n = 0;
   for (i = next[0]; i != -1; i = next[i])
     n++, s[i-1];
   n;
}


%!%+
%\function{par}
%\synopsis{paragraph reformatter}
%\usage{ par()}
%\description
%   A S-Lang paragraph reformatter.  Unlike \var{format_paragraph} it leaves
%   double spaces alone.  It can reformat newline-terminated commments in
%   S-Lang, C++, and SH using the mode's comment_info.
%\seealso{Par_Bullets, set_comment_info}
%!%-
define par()
{
   variable is_comment = 0, comment_chars = "";
   push_spot;
   % get indentation
   bol;
   push_mark;
   skip_white;
   variable comment = get_comment_info;
   if (andelse
       {comment != NULL}
	 {comment.cend == ""}
	 {comment_chars = str_quote_string(comment.cbeg, "[]^\\-", '\\'),
	    re_looking_at(sprintf ("[%s]", comment_chars))})
     {
	skip_chars(comment.cbeg);
	is_comment = 1;
	skip_white;
     }
   
   indent = what_column -1;
   variable prefix = bufsubstr, prefix_length = strlen(prefix);
   
   if (is_comment)	% narrow to the comment
     {
	variable re = sprintf("^%s[^%s]", prefix, comment_chars);
	while (up_1)
	  {
	     bol;
	     if (not re_looking_at(re))
	       {
		  go_down_1;
		  break;
	       }
	  }
	push_mark;
	while (down_1)
	  {
	     if (not re_looking_at(re))
	       {
		  go_up_1;
		  break;
	       }
	  }
	narrow;
     }
   else 	      % narrow to the paragraph
     {	
	backward_paragraph;
	!if (bobp) go_down_1;
	push_mark;
	forward_paragraph;
	!if (eobp) go_up_1;
	narrow;
     }
   
   bob;
   eol;
   while (not eobp)
     {
	del;
	if(is_comment) deln(prefix_length);
	trim;
	insert_char(' ');
	eol;
     }
   
   % paragraph is one line now, look for spaces
   bol;
   [get_spaces(comment_chars)];
   
   % get linebreaks
   variable newlines = normalbreaks();
   
   % insert linebreaks backwards
   loop (newlines)
     {
	_set_point();
	del;
	newline;
	insert(prefix);
	go_up_1;
     }

   widen;
   pop_spot;
}

provide ("subpar");