summaryrefslogtreecommitdiff
path: root/usub/pager
blob: 79e70b92f16bb10833d2a3d488cc7eea1cb9365b (plain)
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
#!./curseperl

eval <<'EndOfMain';   $evaloffset = 3;	# line number of this line

    $| = 1;		# command buffering on stdout
    &initterm;
    &inithelp;
    &slurpfile && &pagearray;

EndOfMain

&endwin;

if ($@) {
    print "";		# force flush of stdout
    $@ =~ s/\(eval\)/$0/ && $@ =~ s/line (\d+)/'line ' . ($1 + $evaloffset)/e;
    die $@;
}

exit;

################################################################################

sub initterm {

    &initscr; &cbreak; &noecho; &scrollok($stdscr, 1);
    &defbell unless defined &bell;

    $lines = $LINES; $lines1 = $lines - 1; $lines2 = $lines - 2;
    $cols = $COLS;   $cols1  = $cols  - 1; $cols2  = $cols  - 2;;

    $dl = &getcap('dl');
    $al = &getcap('al');
    $ho = &getcap('ho');
    $ce = &getcap('ce');
}

sub slurpfile {
    while (<>) {
	s/^(\t+)/'        ' x length($1)/e;
	&expand($_) if /\t/;
	if (length($_) < $cols) {
	    push(@lines, $_);
	}
	else {
	    while ($_ && $_ ne "\n") {
		push(@lines, substr($_,0,$cols));
		substr($_,0,$cols) = '';
	    }
	}
    }
    1;
}

sub drawscreen {
    &move(0,0);
    for ($line .. $line + $lines2) {
	&addstr($lines[$_]);
    }
    &clrtobot;
    &percent;
    &refresh;
}

sub expand {
    while (($off = index($_[0],"\t")) >= 0) {
	substr($_[0], $off, 1) = ' ' x (8 - $off % 8);
    }
}

sub pagearray {
    $line = 0;

    $| = 1;

    for (&drawscreen;;&drawscreen) {

	$ch = &getch;
	$ch = "j" if $ch eq "\n";

	if ($ch eq ' ') {
	    last if $percent >= 100;
	    &move(0,0);
	    $line += $lines1;
	}
	elsif ($ch eq 'b') {
	    $line -= $lines1;
	    &move(0,0);
	    $line = 0 if $line < 0;
	}
	elsif ($ch eq "j") {
	    $line += 1;
	    if ($dl) {
		print $ho, $dl;
		&mvcur(0,0,$lines2,0);
		print $ce,$lines[$line+$lines2],$ce;
		&wmove($curscr,0,0);
		&wdeleteln($curscr);
		&wmove($curscr,$lines2,0);
		&waddstr($curscr,$lines[$line+$lines2]);
	    }
	    &wmove($stdscr,0,0);
	    &wdeleteln($stdscr);
	    &wmove($stdscr,$lines2,0);
	    &waddstr($stdscr,$lines[$line+$lines2]);
	    &percent;
	    &refresh;
	    redo;
	}
	elsif ($ch eq "k") {
	    next if $line <= 0;
	    $line -= 1;
	    if ($al) {
		print $ho, $al, $ce, $lines[$line];
		&wmove($curscr,0,0);
		&winsertln($curscr);
		&waddstr($curscr,$lines[$line]);
	    }
	    &wmove($stdscr,0,0);
	    &winsertln($stdscr);
	    &waddstr($stdscr,$lines[$line]);
	    &percent;
	    &refresh;
	    redo;
	}
	elsif ($ch eq "\f") {
	    &clear;
	}
	elsif ($ch eq "q") {
	    last;
	}
	elsif ($ch eq "h") {
	    &clear;
	    &help;
	    &clear;
	}
	else {
	    &bell;
	}
    }
}

sub defbell {
    eval q#
	sub bell {
	    print "\007";
	}
    #;
}

sub help {
    local(*lines) = *helplines;
    local($line);
    &pagearray;
}

sub inithelp {
    @helplines = split(/\n/,<<'EOT');

      Commands marked with * may be preceeded by a number, N.

  h              Display this help.
  q              Exit.

  f, SPACE    *  Forward  N lines, default one screen.
  b           *  Backward N lines, default one screen.
  e, j, CR    *  Forward  N lines, default 1 line.
  y, k        *  Backward N lines, default 1 line.
  d           *  Forward  N lines, default 10 or last N to d or u command.
  u           *  Backward N lines, default 10 or last N to d or u command.
  r              Repaint screen.
  R              Repaint screen, discarding buffered input.

  /pattern    *  Search forward for N-th line containing the pattern.
  ?pattern    *  Search backward for N-th line containing the pattern.
  n           *  Repeat previous search (for N-th occurence).

  g           *  Go to line N, default 1.
  G           *  Like g, but default is last line in file.
  p, %        *  Position to N percent into the file.
  m<letter>      Mark the current position with <letter>.
  '<letter>      Return to a previously marked position.
  ''             Return to previous position.

  E [file]       Examine a new file.
  N           *  Examine the next file (from the command line).
  P           *  Examine the previous file (from the command line).
  =              Print current file name.
  V              Print version number of "less".

  -<flag>        Toggle a command line flag.
  +cmd           Execute the less cmd each time a new file is examined.

  !command       Passes the command to a shell to be executed.
  v              Edit the current file with $EDITOR.
EOT
    for (@helplines) {
	s/$/\n/;
    }
}

sub percent {
    &standout;
      $percent = int(($line + $lines1) * 100 / @lines);
      &move($lines1,0);
      &addstr("($percent%)");
    &standend;
    &clrtoeol;
}