Fossil SCM

Factor the code that outputs line-numbered text into a separate subroutine, for clarity of presentation.

drh 2011-02-21 18:20 trunk
Commit a4d57c6b261abe4992e7a9aea6ba1c180c071988
1 file changed +61 -38
+61 -38
--- src/info.c
+++ src/info.c
@@ -1113,10 +1113,70 @@
11131113
}
11141114
}
11151115
return 0;
11161116
}
11171117
1118
+/*
1119
+** The "z" argument is a string that contains the text of a source code
1120
+** file. This routine appends that text to the HTTP reply with line numbering.
1121
+**
1122
+** zLn is the ?ln= parameter for the HTTP query. If there is an argument,
1123
+** then highlight that line number and scroll to it once the page loads.
1124
+** If there are two line numbers, highlight the range of lines.
1125
+*/
1126
+static void output_text_with_line_numbers(
1127
+ const char *z,
1128
+ const char *zLn
1129
+){
1130
+ int iStart, iEnd; /* Start and end of region to highlight */
1131
+ int n = 0; /* Current line number */
1132
+ int i; /* Loop index */
1133
+ int iTop = 0; /* Scroll so that this line is on top of screen. */
1134
+
1135
+ iStart = iEnd = atoi(zLn);
1136
+ if( iStart>0 ){
1137
+ for(i=0; fossil_isdigit(zLn[i]); i++){}
1138
+ if( zLn[i]==',' || zLn[i]=='-' || zLn[i]=='.' ){
1139
+ i++;
1140
+ while( zLn[i]=='.' ){ i++; }
1141
+ iEnd = atoi(&zLn[i]);
1142
+ }
1143
+ if( iEnd<iStart ) iEnd = iStart;
1144
+ iTop = iStart - 15 + (iEnd-iStart)/4;
1145
+ if( iTop>iStart - 2 ) iTop = iStart-2;
1146
+ }
1147
+ @ <pre>
1148
+ while( z[0] ){
1149
+ n++;
1150
+ for(i=0; z[i] && z[i]!='\n'; i++){}
1151
+ if( n==iTop ) cgi_append_content("<span id=\"topln\">", -1);
1152
+ if( n==iStart ){
1153
+ cgi_append_content("<div class=\"selectedText\">",-1);
1154
+ }
1155
+ cgi_printf("%6d ", n);
1156
+ if( i>0 ){
1157
+ char *zHtml = htmlize(z, i);
1158
+ cgi_append_content(zHtml, -1);
1159
+ fossil_free(zHtml);
1160
+ }
1161
+ if( n==iStart-15 ) cgi_append_content("</span>", -1);
1162
+ if( n==iEnd ) cgi_append_content("</div>", -1);
1163
+ else cgi_append_content("\n", 1);
1164
+ z += i;
1165
+ if( z[0]=='\n' ) z++;
1166
+ }
1167
+ if( n<iEnd ) cgi_printf("</div>");
1168
+ @ </pre>
1169
+ if( iStart ){
1170
+ @ <script type="text/JavaScript">
1171
+ @ /* <![CDATA[ */
1172
+ @ document.getElementById('topln').scrollIntoView(true);
1173
+ @ /* ]]> */
1174
+ @ </script>
1175
+ }
1176
+}
1177
+
11181178
11191179
/*
11201180
** WEBPAGE: artifact
11211181
** URL: /artifact?name=ARTIFACTID
11221182
** URL: /artifact?ci=CHECKIN&filename=PATH
@@ -1197,48 +1257,11 @@
11971257
@ <blockquote>
11981258
if( zMime==0 ){
11991259
const char *zLn = P("ln");
12001260
const char *z = blob_str(&content);
12011261
if( zLn ){
1202
- int iStart, iEnd;
1203
- int n = 0;
1204
- int i;
1205
- iStart = iEnd = atoi(zLn);
1206
- if( iStart>0 ){
1207
- for(i=0; zLn[i] && zLn[i]!=','; i++){}
1208
- if( zLn[i] ) iEnd = atoi(&zLn[i+1]);
1209
- if( iEnd<iStart ) iEnd = iStart;
1210
- }
1211
- @ <pre>
1212
- while( z[0] ){
1213
- n++;
1214
- for(i=0; z[i] && z[i]!='\n'; i++){}
1215
- if( n==iStart-15 ) cgi_append_content("<span id=\"topln\">", -1);
1216
- if( n==iStart ){
1217
- cgi_append_content("<div class=\"selectedText\">",-1);
1218
- }
1219
- cgi_printf("%06d ", n);
1220
- if( i>0 ){
1221
- char *zHtml = htmlize(z, i);
1222
- cgi_append_content(zHtml, -1);
1223
- fossil_free(zHtml);
1224
- }
1225
- if( n==iStart-15 ) cgi_append_content("</span>", -1);
1226
- if( n==iEnd ) cgi_append_content("</div>", -1);
1227
- else cgi_append_content("\n", 1);
1228
- z += i;
1229
- if( z[0]=='\n' ) z++;
1230
- }
1231
- if( n<iEnd ) cgi_printf("</div>");
1232
- @ </pre>
1233
- if( iStart ){
1234
- @ <script type="text/JavaScript">
1235
- @ /* <![CDATA[ */
1236
- @ document.getElementById('topln').scrollIntoView(true);
1237
- @ /* ]]> */
1238
- @ </script>
1239
- }
1262
+ output_text_with_line_numbers(z, zLn);
12401263
}else{
12411264
@ <pre>
12421265
@ %h(z);
12431266
@ </pre>
12441267
}
12451268
--- src/info.c
+++ src/info.c
@@ -1113,10 +1113,70 @@
1113 }
1114 }
1115 return 0;
1116 }
1117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1118
1119 /*
1120 ** WEBPAGE: artifact
1121 ** URL: /artifact?name=ARTIFACTID
1122 ** URL: /artifact?ci=CHECKIN&filename=PATH
@@ -1197,48 +1257,11 @@
1197 @ <blockquote>
1198 if( zMime==0 ){
1199 const char *zLn = P("ln");
1200 const char *z = blob_str(&content);
1201 if( zLn ){
1202 int iStart, iEnd;
1203 int n = 0;
1204 int i;
1205 iStart = iEnd = atoi(zLn);
1206 if( iStart>0 ){
1207 for(i=0; zLn[i] && zLn[i]!=','; i++){}
1208 if( zLn[i] ) iEnd = atoi(&zLn[i+1]);
1209 if( iEnd<iStart ) iEnd = iStart;
1210 }
1211 @ <pre>
1212 while( z[0] ){
1213 n++;
1214 for(i=0; z[i] && z[i]!='\n'; i++){}
1215 if( n==iStart-15 ) cgi_append_content("<span id=\"topln\">", -1);
1216 if( n==iStart ){
1217 cgi_append_content("<div class=\"selectedText\">",-1);
1218 }
1219 cgi_printf("%06d ", n);
1220 if( i>0 ){
1221 char *zHtml = htmlize(z, i);
1222 cgi_append_content(zHtml, -1);
1223 fossil_free(zHtml);
1224 }
1225 if( n==iStart-15 ) cgi_append_content("</span>", -1);
1226 if( n==iEnd ) cgi_append_content("</div>", -1);
1227 else cgi_append_content("\n", 1);
1228 z += i;
1229 if( z[0]=='\n' ) z++;
1230 }
1231 if( n<iEnd ) cgi_printf("</div>");
1232 @ </pre>
1233 if( iStart ){
1234 @ <script type="text/JavaScript">
1235 @ /* <![CDATA[ */
1236 @ document.getElementById('topln').scrollIntoView(true);
1237 @ /* ]]> */
1238 @ </script>
1239 }
1240 }else{
1241 @ <pre>
1242 @ %h(z);
1243 @ </pre>
1244 }
1245
--- src/info.c
+++ src/info.c
@@ -1113,10 +1113,70 @@
1113 }
1114 }
1115 return 0;
1116 }
1117
1118 /*
1119 ** The "z" argument is a string that contains the text of a source code
1120 ** file. This routine appends that text to the HTTP reply with line numbering.
1121 **
1122 ** zLn is the ?ln= parameter for the HTTP query. If there is an argument,
1123 ** then highlight that line number and scroll to it once the page loads.
1124 ** If there are two line numbers, highlight the range of lines.
1125 */
1126 static void output_text_with_line_numbers(
1127 const char *z,
1128 const char *zLn
1129 ){
1130 int iStart, iEnd; /* Start and end of region to highlight */
1131 int n = 0; /* Current line number */
1132 int i; /* Loop index */
1133 int iTop = 0; /* Scroll so that this line is on top of screen. */
1134
1135 iStart = iEnd = atoi(zLn);
1136 if( iStart>0 ){
1137 for(i=0; fossil_isdigit(zLn[i]); i++){}
1138 if( zLn[i]==',' || zLn[i]=='-' || zLn[i]=='.' ){
1139 i++;
1140 while( zLn[i]=='.' ){ i++; }
1141 iEnd = atoi(&zLn[i]);
1142 }
1143 if( iEnd<iStart ) iEnd = iStart;
1144 iTop = iStart - 15 + (iEnd-iStart)/4;
1145 if( iTop>iStart - 2 ) iTop = iStart-2;
1146 }
1147 @ <pre>
1148 while( z[0] ){
1149 n++;
1150 for(i=0; z[i] && z[i]!='\n'; i++){}
1151 if( n==iTop ) cgi_append_content("<span id=\"topln\">", -1);
1152 if( n==iStart ){
1153 cgi_append_content("<div class=\"selectedText\">",-1);
1154 }
1155 cgi_printf("%6d ", n);
1156 if( i>0 ){
1157 char *zHtml = htmlize(z, i);
1158 cgi_append_content(zHtml, -1);
1159 fossil_free(zHtml);
1160 }
1161 if( n==iStart-15 ) cgi_append_content("</span>", -1);
1162 if( n==iEnd ) cgi_append_content("</div>", -1);
1163 else cgi_append_content("\n", 1);
1164 z += i;
1165 if( z[0]=='\n' ) z++;
1166 }
1167 if( n<iEnd ) cgi_printf("</div>");
1168 @ </pre>
1169 if( iStart ){
1170 @ <script type="text/JavaScript">
1171 @ /* <![CDATA[ */
1172 @ document.getElementById('topln').scrollIntoView(true);
1173 @ /* ]]> */
1174 @ </script>
1175 }
1176 }
1177
1178
1179 /*
1180 ** WEBPAGE: artifact
1181 ** URL: /artifact?name=ARTIFACTID
1182 ** URL: /artifact?ci=CHECKIN&filename=PATH
@@ -1197,48 +1257,11 @@
1257 @ <blockquote>
1258 if( zMime==0 ){
1259 const char *zLn = P("ln");
1260 const char *z = blob_str(&content);
1261 if( zLn ){
1262 output_text_with_line_numbers(z, zLn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1263 }else{
1264 @ <pre>
1265 @ %h(z);
1266 @ </pre>
1267 }
1268

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button