1
|
1 // pager.cc -*- C++ -*- |
|
2 /* |
|
3 |
529
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
581
|
30 #include <fstream.h> |
1
|
31 #include <stdlib.h> |
|
32 |
|
33 #include "procstream.h" |
|
34 |
|
35 #include "user-prefs.h" |
581
|
36 #include "oct-obj.h" |
|
37 #include "error.h" |
|
38 #include "defun.h" |
1
|
39 #include "input.h" |
|
40 #include "pager.h" |
581
|
41 #include "utils.h" |
|
42 #include "help.h" |
1
|
43 |
|
44 // Where we stash output headed for the screen. |
529
|
45 static ostrstream *pager_buf = 0; |
1
|
46 |
581
|
47 // Nonzero means we write to the diary file. |
|
48 static int write_to_diary_file = 0; |
|
49 |
|
50 // The name of the current diary file. |
|
51 static char *diary_file = "diary"; |
|
52 |
|
53 // The diary file. |
|
54 static ofstream diary_stream; |
|
55 |
1
|
56 static int |
|
57 line_count (char *s) |
|
58 { |
|
59 int count = 0; |
529
|
60 if (s) |
1
|
61 { |
|
62 char c; |
|
63 while ((c = *s++) != '\0') |
|
64 if (c == '\n') |
|
65 count++; |
|
66 } |
|
67 return count; |
|
68 } |
|
69 |
581
|
70 // For now, use the variables from readline. It already handles |
|
71 // SIGWINCH, so these values have a good chance of being correct even |
|
72 // if the window changes size (they will be wrong if, for example, the |
|
73 // luser changes the window size while the pager is running, and the |
|
74 // signal is handled by the pager instead of us. |
|
75 |
1
|
76 int |
|
77 terminal_columns (void) |
|
78 { |
|
79 extern int screenwidth; |
|
80 return screenwidth > 0 ? screenwidth : 80; |
|
81 } |
|
82 |
|
83 int |
|
84 terminal_rows (void) |
|
85 { |
|
86 extern int screenheight; |
|
87 return screenheight > 0 ? screenheight : 24; |
|
88 } |
|
89 |
|
90 void |
|
91 initialize_pager (void) |
|
92 { |
|
93 delete pager_buf; |
|
94 pager_buf = new ostrstream (); |
|
95 } |
|
96 |
|
97 void |
|
98 maybe_page_output (ostrstream& msg_buf) |
|
99 { |
|
100 msg_buf << ends; |
|
101 |
|
102 char *message = msg_buf.str (); |
|
103 |
|
104 if (interactive |
|
105 && user_pref.page_screen_output |
529
|
106 && user_pref.pager_binary) |
1
|
107 { |
|
108 *pager_buf << message; |
|
109 delete [] message; |
|
110 } |
|
111 else |
|
112 { |
|
113 cout << message; |
|
114 cout.flush (); |
|
115 delete [] message; |
|
116 } |
|
117 } |
|
118 |
|
119 void |
|
120 flush_output_to_pager (void) |
|
121 { |
|
122 *pager_buf << ends; |
|
123 |
|
124 char *message = pager_buf->str (); |
|
125 |
529
|
126 if (! message || ! *message) |
1
|
127 { |
|
128 delete [] message; |
|
129 initialize_pager (); |
|
130 return; |
|
131 } |
|
132 |
581
|
133 maybe_write_to_diary_file (message); |
|
134 |
1
|
135 int nlines = line_count (message); |
|
136 |
|
137 if (nlines > terminal_rows () - 2) |
|
138 { |
|
139 char *pgr = user_pref.pager_binary; |
529
|
140 if (pgr) |
1
|
141 { |
|
142 oprocstream pager_stream (pgr); |
|
143 if (pager_stream) |
|
144 { |
|
145 pager_stream << message; |
|
146 pager_stream.flush (); |
|
147 |
|
148 delete [] message; |
|
149 initialize_pager (); |
|
150 return; |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 cout << message; |
|
156 cout.flush (); |
|
157 delete [] message; |
|
158 initialize_pager (); |
|
159 } |
|
160 |
581
|
161 static void |
|
162 open_diary_file (void) |
|
163 { |
|
164 if (diary_stream.is_open ()) |
|
165 diary_stream.close (); |
|
166 |
|
167 diary_stream.open (diary_file, ios::app); |
|
168 |
|
169 if (! diary_stream) |
|
170 error ("diary: can't open diary file `%s'", diary_file); |
|
171 } |
|
172 |
|
173 void |
|
174 close_diary_file (void) |
|
175 { |
|
176 if (diary_stream) |
|
177 diary_stream.close (); |
|
178 } |
|
179 |
|
180 void |
|
181 maybe_write_to_diary_file (const char *s) |
|
182 { |
|
183 if (write_to_diary_file && diary_stream) |
|
184 diary_stream << s; |
|
185 } |
|
186 |
|
187 DEFUN_TEXT ("diary", Fdiary, Sdiary, -1, 1, |
|
188 "diary [on|off]\n\ |
|
189 diary [file]\n\ |
|
190 \n\ |
|
191 redirect all input and screen output to a file.") |
|
192 { |
|
193 Octave_object retval; |
|
194 |
|
195 DEFINE_ARGV("diary"); |
|
196 |
|
197 switch (argc) |
|
198 { |
|
199 case 1: |
|
200 write_to_diary_file = ! write_to_diary_file; |
|
201 open_diary_file (); |
|
202 break; |
623
|
203 |
581
|
204 case 2: |
|
205 { |
|
206 char *arg = argv[1]; |
|
207 if (strcmp (arg, "on") == 0) |
|
208 { |
|
209 write_to_diary_file = 1; |
|
210 open_diary_file (); |
|
211 } |
|
212 else if (strcmp (arg, "off") == 0) |
|
213 write_to_diary_file = 0; |
|
214 else |
|
215 { |
|
216 delete [] diary_file; |
|
217 diary_file = strsave (arg); |
|
218 open_diary_file (); |
|
219 } |
|
220 } |
|
221 break; |
|
222 default: |
|
223 print_usage ("diary"); |
|
224 break; |
|
225 } |
|
226 |
|
227 DELETE_ARGV; |
|
228 |
|
229 return retval; |
|
230 } |
|
231 |
1
|
232 /* |
|
233 ;;; Local Variables: *** |
|
234 ;;; mode: C++ *** |
|
235 ;;; page-delimiter: "^/\\*" *** |
|
236 ;;; End: *** |
|
237 */ |