1
|
1 // pager.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "procstream.h" |
|
33 |
|
34 #include "user-prefs.h" |
|
35 #include "input.h" |
|
36 #include "pager.h" |
|
37 |
|
38 // Where we stash output headed for the screen. |
|
39 static ostrstream *pager_buf; |
|
40 |
|
41 static int |
|
42 line_count (char *s) |
|
43 { |
|
44 int count = 0; |
|
45 if (s != (char *) NULL) |
|
46 { |
|
47 char c; |
|
48 while ((c = *s++) != '\0') |
|
49 if (c == '\n') |
|
50 count++; |
|
51 } |
|
52 return count; |
|
53 } |
|
54 |
|
55 /* |
|
56 * For now, use the variables from readline. It already handles |
|
57 * SIGWINCH, so these values have a good chance of being correct even |
|
58 * if the window changes size (they will be wrong if, for example, the |
|
59 * luser changes the window size while the pager is running, and the |
|
60 * signal is handled by the pager instead of us. |
|
61 */ |
|
62 int |
|
63 terminal_columns (void) |
|
64 { |
|
65 extern int screenwidth; |
|
66 return screenwidth > 0 ? screenwidth : 80; |
|
67 } |
|
68 |
|
69 int |
|
70 terminal_rows (void) |
|
71 { |
|
72 extern int screenheight; |
|
73 return screenheight > 0 ? screenheight : 24; |
|
74 } |
|
75 |
|
76 void |
|
77 initialize_pager (void) |
|
78 { |
|
79 delete pager_buf; |
|
80 pager_buf = new ostrstream (); |
|
81 } |
|
82 |
|
83 void |
|
84 maybe_page_output (ostrstream& msg_buf) |
|
85 { |
|
86 msg_buf << ends; |
|
87 |
|
88 char *message = msg_buf.str (); |
|
89 |
|
90 if (interactive |
|
91 && user_pref.page_screen_output |
|
92 && user_pref.pager_binary != (char *) NULL) |
|
93 { |
|
94 *pager_buf << message; |
|
95 delete [] message; |
|
96 } |
|
97 else |
|
98 { |
|
99 cout << message; |
|
100 cout.flush (); |
|
101 delete [] message; |
|
102 } |
|
103 } |
|
104 |
|
105 void |
|
106 flush_output_to_pager (void) |
|
107 { |
|
108 *pager_buf << ends; |
|
109 |
|
110 char *message = pager_buf->str (); |
|
111 |
|
112 if (message == (char *) NULL || *message == '\0') |
|
113 { |
|
114 delete [] message; |
|
115 initialize_pager (); |
|
116 return; |
|
117 } |
|
118 |
|
119 int nlines = line_count (message); |
|
120 |
|
121 if (nlines > terminal_rows () - 2) |
|
122 { |
|
123 char *pgr = user_pref.pager_binary; |
|
124 if (pgr != (char *) NULL) |
|
125 { |
|
126 oprocstream pager_stream (pgr); |
|
127 if (pager_stream) |
|
128 { |
|
129 pager_stream << message; |
|
130 pager_stream.flush (); |
|
131 |
|
132 delete [] message; |
|
133 initialize_pager (); |
|
134 return; |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 cout << message; |
|
140 cout.flush (); |
|
141 delete [] message; |
|
142 initialize_pager (); |
|
143 } |
|
144 |
|
145 /* |
|
146 ;;; Local Variables: *** |
|
147 ;;; mode: C++ *** |
|
148 ;;; page-delimiter: "^/\\*" *** |
|
149 ;;; End: *** |
|
150 */ |