File: rfcview.sl

package info (click to toggle)
jed-extra 2.5.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,972 kB
  • ctags: 2,490
  • sloc: makefile: 75; ruby: 43; sed: 38; sh: 31
file content (438 lines) | stat: -rw-r--r-- 9,285 bytes parent folder | download | duplicates (4)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
% rfcview.sl	-*- mode: Slang; mode: Fold -*-
% RFC viewer
% 
% $Id: rfcview.sl,v 1.7 2009/01/11 09:26:44 paul Exp paul $
% Keywords: docs
%
% Copyright (c) 2003-2009 Paul Boekholt.
% Released under the terms of the GNU GPL (version 2 or later).
% 
% Not as pretty as Emacs' rfcview, but more features.
% -Pick rfc's from the rfc-index
% -follow links to other rfc's
% -try to display ToC in separate buffer
% -pop up references with mouse
% -selectively display only headers, or sections matching any of a list
%  of regexps.

provide("rfcview");
require("curl");
require("view");

% A comma separated list of directories to search for RFC's
custom_variable ("Rfc_Path", "/usr/doc/rfc");

private variable mode = "rfcview";


%{{{ syntax table

create_syntax_table(mode);
#ifdef HAS_DFA_SYNTAX
%%% DFA_CACHE_BEGIN %%%
static define setup_dfa_callback (mode)
{
   dfa_define_highlight_rule ("RFC ?[0-9]+", "keyword1", mode);
   dfa_define_highlight_rule ("^[0-9\\.]+", "preprocess", mode);
   dfa_define_highlight_rule ("\\[RFC ?[0-9]+\\]", "QKkeyword1", mode);
   dfa_define_highlight_rule ("\\[.*\\]", "QKnormal", mode);
   dfa_build_highlight_table(mode);
}
dfa_set_init_callback (&setup_dfa_callback, mode);
%%% DFA_CACHE_END %%%
enable_dfa_syntax_for_mode(mode);
#endif

%}}}
%{{{ rfc fetcher

private define write_callback (v, data)
{
   insert(data);
   return 0;
}

private define get_rfc(rfc)
{
   rfc = strtrim_beg(rfc, "0");
   variable c = curl_new (sprintf("ftp://ftp@ftp.ietf.org/rfc/rfc%s.txt",
				  rfc));
   ()=read_file(sprintf("%s/rfc%s.txt",
			extract_element(Rfc_Path, 0, ','),
			rfc));
   variable v;
   curl_setopt(c, CURLOPT_WRITEFUNCTION, &write_callback, &v);
   curl_perform (c);
   save_buffer();
}
   
%}}}

%{{{ find rfc by number
public define rfc_mode();

private define open_rfc(rfc)
{
   variable file;
   foreach file ([sprintf("rfc%s.txt", rfc),
		 sprintf("rfc%s.txt.gz", rfc),
		 sprintf("rfc%s.txt", strtrim_beg(rfc, "0")),
		 sprintf("rfc%s.txt.gz", strtrim_beg(rfc, "0"))])
     {
	file = search_path_for_file(Rfc_Path, file, ',');
	if (file != NULL)
	  {
	     ()=read_file(file);
	     break;
	  }
     }
   then
     {
	get_rfc(rfc);
     }
   return whatbuf();
}

private define find_rfc(rfc)
{ 
   % if there are three digits, prepend a 0
   if (strlen(rfc) == 3) rfc = "0" + rfc;
   variable buf = whatbuf();
   pop2buf(open_rfc(rfc));
   rfc_mode;
   pop2buf(buf);
}

%}}}

%{{{ index

% get RFC from rfc-index
private define get_rfc_from_index()
{
   ifnot (re_bsearch("^[0-9]")) throw RunTimeError, "not looking at rfc";
   push_mark;
   skip_chars("0-9");
   find_rfc(bufsubstr);
}

%}}}

%{{{ outline
#ifdef HAS_LINE_ATTR

private define hide_body()
{
   set_matching_hidden(0, "^[0-9]");
   message("a: show all  s: show section  d: hide section");
}

% (un)hide a section, unhide the first line
private define hide_show_section(hide)
{
   push_spot;
   eol;
   EXIT_BLOCK
     {
	pop_spot;
     }
   ifnot(re_bsearch("^[0-9]")) return;
   variable first_mark = create_user_mark;
   push_mark;
   go_down_1;
   if(re_fsearch("^[0-9]")) go_left_1; else eob;
   set_region_hidden(hide);
   goto_user_mark(first_mark);
   set_line_hidden(0);
}

private define hide_section()
{
   hide_show_section(1);
}

private define show_section()
{
   hide_show_section(0);
}

private variable last_pattern = "";

% rolo for a pattern, or a | separated list of patterns, in the index or
% in the rfc, if its section headers begin with a number
private define rolo()
{
   variable pattern, subpattern;
   ifnot (_NARGS) read_mini("pattern to rolo for", "", last_pattern);
   pattern = ();
   if (pattern == "") return set_buffer_hidden(0);
   last_pattern = pattern;

   push_spot_bob;
   ifnot (re_fsearch("^[0-9]")) 
     return pop_spot, message ("no section separators");
   push_spot;
   push_mark_eob;
   set_region_hidden(1);
   foreach subpattern (strchop(pattern, '|', '\\'))
     {
	goto_spot;
	while (re_fsearch(subpattern))
	  {
	     set_line_hidden(0);
	     show_section();
	     skip_hidden_lines_forward(0);
	  }
     }
   pop_spot;
   pop_spot;
   message("type 'a' to cancel selective display");
}
#endif

%}}}

%{{{ follow RFC link

private define follow_rfc()
{ 
   bskip_chars("0-9");
   bskip_white;
   bskip_chars("RFC");
   push_mark;
   skip_chars("RFC 0-9");
   variable rfc = string_get_match(bufsubstr, "RFC ?\\([0-9]+\\)", 1, 1);
   if (strlen(rfc)) find_rfc(rfc);
   else if (is_substr(whatbuf, "index")) get_rfc_from_index;
}

%}}}

%{{{ ToC

% from toc or index window, scroll other window
private define scroll_other(cmd)
{
   variable buf = whatbuf;
   otherwindow;
   try
     {
	call(cmd);
     }
   finally
     {
	pop2buf(buf);
     }
}

private define scroll_other_down()
{
   scroll_other("page_down");
}

private define scroll_other_up()
{
   scroll_other("page_up");
}

% go to a page from the ToC
private define goto_page()
{
   variable pages = get_blocal_var("pages"), mark, buf = whatbuf;
   eol;
   push_mark;
   bskip_chars("0-9");
   variable page = bufsubstr;
   if (strlen(page) && assoc_key_exists(pages, page))
     {
	mark = pages[page];
	pop2buf(user_mark_buffer(mark));
	goto_user_mark(mark);
	recenter(1);
     }
   pop2buf(buf);
}

% kill other buffer
private define rfc_close_buffer_hook(buf)
{
   variable obuf = get_blocal_var("otherbuffer");
   if (bufferp(obuf))
     delbuf(obuf);
}

%}}}

%{{{ show bibliographic reference

private define show_reference(l,c,b,s)
{
   variable ref, ref_mark, first_line, lines = 10;
   push_spot;
   EXIT_BLOCK
     {
	pop_spot;
	1;
     }
   ifnot (bfind_char('[')) return;
   push_mark;
   ifnot (ffind_char(']'))
     return pop_mark_0;
   go_right_1;
   ref = bufsubstr;
   eob;
   ifnot (re_bsearch("^[0-9.]*[ \t]*references") && fsearch(ref))
     return;
   ref_mark = create_user_mark;
   goto_spot;
   onewindow();
   splitwindow();
   if (window_info ('t') < 3) otherwindow();
   goto_user_mark(ref_mark);
   push_spot;
   push_mark;
   first_line = what_line;
   eol;
   if (re_fsearch("^[ \t]*\\["))
     lines = what_line - first_line;
   set_region_hidden(0);
   if (lines > 10) lines = 10;
   pop_spot;
   window_info('r') - lines;
   otherwindow;
   loop () enlargewin;
   otherwindow;
   recenter(1);
   otherwindow;
   message ("o: switch to reference window  1: close reference window  0: close this window");
}

%}}}

%{{{ mode for index and rfc

public define rfc_mode()
{
   variable line, page, pages = Assoc_Type[Mark_Type],
     body, toc;
   
   % remove form feeds
   bob;
   push_mark();
   while (bol_fsearch("\f"))
     {
	delete_line;
	bskip_chars("\n");
	line = line_as_string;
	delete_line;
	page = string_get_match(line, "Page +\\([0-9]+\\)", 1, 1);
	exchange_point_and_mark();
	if (strlen(page)) pages[page] = create_user_mark;
	pop_mark_1();
	push_mark();
	skip_chars("\n");
	if (looking_at("RFC")) delete_line;
	bskip_chars("\n");
	push_mark;
	skip_chars("\n");
	del_region;
	insert("\n\n\n");
     }
   pop_mark_0();
   set_buffer_modified_flag(0);

   view_mode;
   use_keymap("rfcview");
   set_mode(mode, 0);
   use_syntax_table(mode);
   set_buffer_hook("newline_indent_hook", &follow_rfc);
   set_buffer_hook("mouse_up", &show_reference);

#ifdef HAS_DFA_SYNTAX
   % try to highlight references
   eob;
   if (re_bsearch("^[0-9\\.]*[ \t]*references"))
     {
	while (re_fsearch("^[ \t]*\\(\\[[^\\]]{1,20}\\]\\)"))
	  {
	     add_keyword(mode, regexp_nth_match(1));
	     eol;
	  }
     }
#endif

   % try to find the table of contents
   bob;
   ifnot (fsearch("table of contents"))
     return;
   push_mark;
   ifnot (re_fsearch("^[ \t]*\\(1[.0-9]*\\)"))
     return pop_mark_0, bob;
   % try to find end of ToC
   body = regexp_nth_match(1);
   eol;
   ifnot (bol_fsearch(body))
     return pop_mark_0, bob;
   go_left_1;
   toc = bufsubstr;
   bob;
   variable buffer = whatbuf,
     otherbuffer = sprintf("*ToC for %s*", whatbuf);
   define_blocal_var("otherbuffer", otherbuffer);
   define_blocal_var("close_buffer_hook", &rfc_close_buffer_hook);
   if (bufferp(otherbuffer)) delbuf(otherbuffer);
   sw2buf(otherbuffer);
   define_blocal_var("close_buffer_hook", &rfc_close_buffer_hook);
   define_blocal_var("otherbuffer", buffer);
   insert (toc);
   bob;
   define_blocal_var("pages", pages);
   view_mode;
   use_keymap("rfc_toc");
   set_buffer_hook("newline_indent_hook", &goto_page);
}

%}}}

%{{{ keymaps
ifnot (keymap_p("rfcview"))
  copy_keymap("rfcview", "view");

$1= _stkdepth;

"delete_window", "0";
"one_window", "1";
"split_window", "2";
"enlarge_window", "^";
"other_window", "o";
". eol \"^[0-9]\" re_fsearch pop 2 recenter", "n";
". bol \"^[0-9]\" re_bsearch pop 2 recenter", "p";
". \"Enter: follow link  n: next section  p: previous section  ? search sections\" message", "h";

#ifdef HAS_LINE_ATTR
&rolo, "?";
% outline-like bindings
&hide_body, "t";
"set_buffer_hidden(0)", "a";
&hide_section, "d";
&show_section, "s";
#endif

loop ((_stkdepth - $1) /2)
  definekey ("rfcview");

% keymap for ToC and index
ifnot (keymap_p("rfc_toc"))
  copy_keymap("rfc_toc", "rfcview");
definekey(&scroll_other_down, " ", "rfc_toc");
definekey(&scroll_other_up, Key_BS, "rfc_toc");

%}}}

public define rfcview()
{
   sw2buf(open_rfc("-index"));
   rfc_mode();
   use_keymap("rfc_toc");
}