0
|
1 ;;; gopher.el --- an emacs gopher client
|
|
2
|
|
3 ;; Copyright (C) 1992 scott snyder
|
|
4
|
|
5 ;; Author: scott snyder <snyder@fnald0.fnal.gov>
|
|
6 ;; Created: 29 Jun 1992
|
|
7 ;; Version: 1.03
|
|
8 ;; Keywords: gopher, comm
|
|
9
|
|
10 ;; LCD Archive Entry:
|
|
11 ;; gopher|scott snyder|snyder@fnald0.fnal.gov|
|
|
12 ;; An emacs gopher client.|
|
|
13 ;; 20-Apr-1993|1.02|~/interfaces/gopher.el.Z|
|
|
14
|
|
15 ;; This file is not part of GNU Emacs, but is distributed under the
|
|
16 ;; same conditions.
|
|
17 ;;
|
|
18 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY. No author or distributor
|
|
20 ;; accepts responsibility to anyone for the consequences of using it
|
|
21 ;; or for whether it serves any particular purpose or works at all,
|
|
22 ;; unless he says so in writing. Refer to the GNU Emacs General Public
|
|
23 ;; License for full details.
|
|
24 ;;
|
|
25 ;; Everyone is granted permission to copy, modify and redistribute
|
|
26 ;; GNU Emacs, but only under the conditions described in the
|
|
27 ;; GNU Emacs General Public License. A copy of this license is
|
|
28 ;; supposed to have been given to you along with GNU Emacs so you
|
|
29 ;; can know your rights and responsibilities. It should be in a
|
|
30 ;; file named COPYING. Among other things, the copyright notice
|
|
31 ;; and this notice must be preserved on all copies.
|
|
32 ;;
|
|
33 ;;; Synched up with: Not in FSF.
|
|
34 ;;
|
|
35 ;; An emacs gopher client. Currently supports directory, text, CSO,
|
|
36 ;; index, image, and telnet objects.
|
|
37 ;; Requires forms.el and background.el.
|
|
38 ;;
|
|
39 ;; Written by scott snyder <snyder@fnald0.fnal.gov>
|
|
40 ;; Some code borrowed from GNUS (by Masanobu UMEDA).
|
|
41 ;; Some code (bookmarks, xterms, error handling) contributed
|
|
42 ;; by Stewart Clamen <clamen@cs.cmu.edu>.
|
|
43
|
|
44 ;;; Commentary:
|
|
45 ;; OPERATING INSTRUCTIONS
|
|
46 ;;
|
|
47 ;; To use, `M-x gopher'. To specify a different root server, use
|
|
48 ;; `C-u M-x gopher'. If you want to use bookmarks, set the variable
|
|
49 ;; gopher-support-bookmarks appropriately.
|
|
50 ;; The command `M-x gopher-atpoint' will attempt to interpret the text
|
|
51 ;; around point as a gopher bookmark specification and will retrieve
|
|
52 ;; that item.
|
|
53 ;;
|
|
54 ;; Sample .emacs configuration:
|
|
55 ;; (autoload 'gopher "gopher")
|
|
56 ;; (autoload 'gopher-atpoint "gopher")
|
|
57 ;; (setq gopher-support-bookmarks t)
|
|
58 ;;
|
|
59 ;; In directory mode:
|
|
60 ;; Space, return, `f', or `e' selects the line point is on.
|
|
61 ;; With a numeric prefix argument, select that object.
|
|
62 ;; `q', `l', or `u' will return to the previous node.
|
|
63 ;; `n' and `p' to the next and previous lines.
|
|
64 ;; `a' will add an object to your bookmark list.
|
|
65 ;; `v' will display your bookmark list.
|
|
66 ;; `=' gives detailed information about an object.
|
|
67 ;; In the bookmark list, all of the above (except `a'), plus:
|
|
68 ;; `C-k' will delete an object from the bookmark list.
|
|
69 ;; `C-y' will yank the most recently deleted bookmark object back into
|
|
70 ;; the bookmark buffer.
|
|
71 ;; `s' will save the bookmark list.
|
|
72 ;; `Q' will quit gopher entirely, killing all gopher buffers.
|
|
73 ;;
|
|
74 ;; All commands which operate on a specific object can take an optional
|
|
75 ;; numeric prefix argument giving the index of the object on which
|
|
76 ;; to operate.
|
|
77 ;;
|
|
78 ;; In document mode:
|
|
79 ;; Space pages forward.
|
|
80 ;; Delete pages backward.
|
|
81 ;; `q', `l' or `u' returns to the last node.
|
|
82 ;;
|
|
83 ;; In the CSO entry form:
|
|
84 ;; `C-c RET' performs a look-up, based on the field contents
|
|
85 ;; you've filed in.
|
|
86 ;; `C-c l' returns to the previous node.
|
|
87 ;;
|
|
88 ;; Telnets:
|
|
89 ;; If you have an X server set, gopher will try to create an xterm
|
|
90 ;; running telnet. If not, the emacs-lisp telnet mode will be used.
|
|
91 ;; From the emacs-lisp telnet mode, use `C-c l' to kill the session
|
|
92 ;; and return to the previous node.
|
|
93 ;; See also the variable gopher-telnet-command.
|
|
94 ;;
|
|
95 ;; Images:
|
|
96 ;; Images are displayed using the command gopher-image-display-command.
|
|
97 ;; The default setting for this variable uses xv.
|
|
98 ;;
|
|
99 ;; Note:
|
|
100 ;; If gopher consistently hangs while trying to retrieve an object,
|
|
101 ;; try turning on gopher-buggy-accept (which see).
|
|
102 ;;
|
|
103 ;; VMS notes:
|
|
104 ;; To use this on VMS, you'll need my emacs subprocess patches (recently
|
|
105 ;; posted on gnu.emacs.sources; if you can't find them, send me mail).
|
|
106 ;; To be able to run telnet in a separate decterm, you'll also need
|
|
107 ;; to (setq shell-file-name "docmd") and create the file
|
|
108 ;; emacs_library:[etc]docmd.com containing the following:
|
|
109 ;; $ if p1 .eqs. "-C" then p1 = ""
|
|
110 ;; $ deass sys$input
|
|
111 ;; $ 'p1 'p2 'p3 'p4 'p5 'p6 'p7 'p8
|
|
112 ;; $ eoj
|
|
113
|
|
114 ;;; Change Log:
|
|
115 ;;
|
|
116 ;; Version 1.03 30-APR-1993
|
|
117 ;; * Add a buffer-local variable gopher-obj to all gopher buffers bound
|
|
118 ;; to the description of the object contained in that buffer.
|
|
119 ;;
|
|
120 ;; Version 1.02 20-APR-1993
|
|
121 ;; * Avoid using replace-regexp in gopher-clean-text. (Suggested by
|
|
122 ;; sbyrnes@rice.edu (Steven Byrnes)).
|
|
123 ;; * Added gopher-port-aliases.
|
|
124 ;; * Print ports as strings in gopher-directory-show-object.
|
|
125 ;; * Don't (ding) when the net stream closes unexpectedly.
|
|
126 ;; * Added image display (based on code from beldar@MicroUnity.com
|
|
127 ;; (Gardner Cohen)).
|
|
128 ;; * Attempt to improve error reporting.
|
|
129 ;; * Reworked gopher-parse-bookmark to handle out-of-order fields.
|
|
130 ;; * Added gopher-atpoint.
|
|
131 ;; * Moved bookmark init stuff to gopher-read-bookmarks.
|
|
132 ;; * Added gopher-quit (based on code from Thomas L|fgren
|
|
133 ;; <tde9104@abacus.hgs.se>).
|
|
134 ;; * Change usage of background for XEmacs.
|
|
135 ;; (Patch from "William M. Perry" <wmperry@raisin.ucs.indiana.edu>).
|
|
136 ;; * Added a (provide 'gopher) at end.
|
|
137 ;; * Added `f' and `e' bindings in directory mode.
|
|
138 ;;
|
|
139 ;; Version 1.01
|
|
140 ;; * Added patch suggested by Humberto Ortiz-Zuazaga
|
|
141 ;; <zuazaga@ucunix.san.uc.EDU> to allow null Path= items in .gopherrc.
|
|
142 ;;
|
|
143 ;; Version 1.00 29-AUG-1992
|
|
144 ;; * Added gopher-buggy-accept.
|
|
145 ;; * Reworked telnet stuff to use an arbitrary command string to start up
|
|
146 ;; the telnet process. This can be used to start the telnet in a
|
|
147 ;; separate terminal window.
|
|
148 ;; Based on code from Stewart Clamen.
|
|
149 ;; * Stewart Clamen <clamen@cs.cmu.edu> added bookmarks.
|
|
150 ;; * Added 's key binding to save bookmarks.
|
|
151 ;; * Added a prefix argument to gopher-directory-buffer and
|
|
152 ;; gopher-add-bookmark.
|
|
153 ;; * Added standard emacs-lisp header.
|
|
154 ;; * Stewart Clamen <clamen@cs.cmu.edu> added some error trapping and
|
|
155 ;; recovery (gopher-retrieve-document-cleanly).
|
|
156 ;; * Appended node description to the node's buffer's name.
|
|
157 ;; * Reformat bookmark buffers when returning to them via gopher-last-node
|
|
158 ;; or when an item is deleted.
|
|
159 ;; * Added gopher-yank-bookmark.
|
|
160 ;; * Added gopher-bookmark-modified-tick to prevent reformatting bookmark
|
|
161 ;; buffers needlessly.
|
|
162 ;;
|
|
163 ;; Version 0.92 27-JUL-1992
|
|
164 ;; * Added gopher-hostname-aliases.
|
|
165 ;;
|
|
166 ;; Version 0.91 30-JUN-1992
|
|
167 ;; * Deal with servers which send stuff after the CR.
|
|
168 ;; * Prevent gopher-directory-show-object from clearing the read-only flag.
|
|
169 ;; * Allow specification of port number in `C-u M-x gopher'.
|
|
170 ;;
|
|
171 ;; Version 0.9 29-JUN-1992
|
|
172 ;; * Initial release.
|
|
173
|
|
174 ;;; Code:
|
|
175
|
|
176 (require 'electric)
|
|
177 (require 'forms)
|
|
178
|
|
179 ;; background has the same name as an epoch function.
|
|
180 ;; Rename it to gopher-background...
|
|
181 ;; also, the version i got from the archive didn't have a provide...
|
|
182 (cond ((and (string-lessp "19" emacs-version)
|
|
183 (not (boundp 'epoch::version)))
|
|
184 ;; background is obsolete in emacs19: just add a & to shell-command.
|
|
185 (defun gopher-background (command)
|
|
186 (shell-command (concat command "&"))))
|
|
187 (t
|
|
188 ;; background has the same name as an epoch function.
|
|
189 ;; Rename it to gopher-background...
|
|
190 ;; also, the version i got from the archive didn't have a provide...
|
|
191 (if (not (fboundp 'gopher-background))
|
|
192 (if (fboundp 'background)
|
|
193 (let ((old-background (symbol-function 'background)))
|
|
194 (load-library "background")
|
|
195 (fset 'gopher-background (symbol-function 'background))
|
|
196 (fset 'background old-background))
|
|
197 (load-library "background")
|
|
198 (fset 'gopher-background (symbol-function 'background))
|
|
199 ))
|
|
200 ))
|
|
201
|
134
|
202 (defgroup gopher nil
|
|
203 "An Emacs gopher client."
|
|
204 :group 'hypermedia)
|
|
205
|
0
|
206
|
134
|
207 (defcustom gopher-root-node (vector ?1 "root" "" "ucs_gopher" 70)
|
|
208 "The root gopher server, as a gopher object."
|
|
209 :type '(vector character string string
|
|
210 (string :tag "Host")
|
|
211 (integer :tag "Port"))
|
|
212 :group 'gopher
|
|
213 :group 'local)
|
|
214
|
|
215 (defcustom gopher-directory-mode-hook nil
|
|
216 "*Invoked when entering a new gopher directory."
|
|
217 :type 'hook
|
|
218 :group 'gopher)
|
0
|
219 (defvar gopher-directory-mode-map (make-keymap)
|
|
220 "Keymap for gopher-directory-mode.")
|
|
221
|
134
|
222 (defcustom gopher-document-mode-hook nil
|
|
223 "*Invoked when showing gopher document."
|
|
224 :type 'hook
|
|
225 :group 'gopher)
|
0
|
226 (defvar gopher-document-mode-map (make-keymap)
|
|
227 "Keymap for gopher-document-mode.")
|
|
228
|
134
|
229 (defcustom gopher-form-mode-hooks nil
|
|
230 "*Invoked with entering a gopher form (i.e., for CSO)."
|
|
231 :type 'hook
|
|
232 :group 'gopher)
|
0
|
233 (defvar gopher-form-mode-map (make-keymap)
|
|
234 "Keymap for gopher-form-mode.")
|
|
235
|
|
236 (defvar gopher-tmp-buf nil
|
|
237 "Buffer used to receive output from gopher.")
|
|
238
|
134
|
239 (defcustom gopher-debug-read t
|
|
240 "*If non-nil, show the current status about reading the gopher server output."
|
|
241 :type 'boolean
|
|
242 :group 'gopher)
|
0
|
243
|
|
244 ;; On some systems (such as SGI Iris), accept-process-output doesn't seem
|
|
245 ;; to return for the last packet received on a connection. Turn this on
|
|
246 ;; to work around the problem, but does anyone know what causes this?
|
134
|
247 (defcustom gopher-buggy-accept nil
|
0
|
248 "*If non-nil, use sit-for instead of accept-process-output.
|
134
|
249 If gopher consistently hangs while fetching an object, try turning this on."
|
|
250 :type 'boolean
|
|
251 :group 'gopher)
|
0
|
252
|
134
|
253 (defcustom gopher-hostname-aliases
|
0
|
254 '(("128.230.33.31" . "oliver.syr.edu"))
|
|
255 "Emacs can't deal with raw IP addresses used as a hostname.
|
134
|
256 Use this to work around..."
|
|
257 :type '(repeat (cons (string :tag "IP Address")
|
|
258 (string :tag "Host Name")))
|
|
259 :group 'gopher)
|
0
|
260
|
134
|
261 (defcustom gopher-port-aliases
|
0
|
262 '(("whois_port" . 43))
|
|
263 "Some losing hosts send a port name instead of a number.
|
134
|
264 Use this table to convert..."
|
|
265 :type '(repeat (cons (string :tag "Named Port")
|
|
266 (integer :tag "Port Number")))
|
|
267 :group 'gopher)
|
0
|
268
|
|
269
|
134
|
270 (defcustom gopher-support-bookmarks nil
|
0
|
271 "*If nil, do not implement bookmarks.
|
|
272 If 'unix or t, read and write bookmarks to ~/.gopherrc.
|
|
273 If a filename, read and save vector from there directly (not implemented yet).
|
134
|
274 If a vector, treat as a built-in directory."
|
|
275 :type '(choice (const :tag "off" nil)
|
|
276 (const t) (const unix)
|
|
277 file
|
|
278 vector)
|
|
279 :group 'gopher)
|
0
|
280
|
|
281 (defconst gopher-bookmarks nil "Internal bookmark directory.")
|
|
282 (defconst gopher-bookmarks-modified nil "Do bookmarks need to be saved?")
|
|
283 (defconst gopher-killed-bookmark nil "The last bookmark object to be killed")
|
|
284 (defconst gopher-bookmark-directory-p nil
|
|
285 "Is this buffer a bookmark directory? A buffer-local variable.")
|
|
286
|
|
287 (defvar gopher-bookmark-modified-tick 0
|
|
288 "Counts each time the bookmark vector is modified.")
|
|
289
|
|
290
|
134
|
291 (defcustom gopher-telnet-command
|
0
|
292 (cond ((eq system-type 'vax-vms)
|
|
293 (if (getenv "DECW$DISPLAY")
|
|
294 "create/terminal/wait/window=(title=\"telnet\") telnet"))
|
|
295 (t
|
|
296 (if (getenv "DISPLAY")
|
|
297 "xterm -e telnet"))
|
|
298 )
|
|
299 "*Command to use to start a telnet session.
|
|
300 If this is nil, the emacs-lisp telnet package will be used.
|
|
301 The default setting is to create a terminal window running telnet
|
134
|
302 if you've specified an X server, and to use the emacs-lisp telnet otherwise."
|
|
303 :type '(choice (const nil) string)
|
|
304 :group 'gopher)
|
0
|
305
|
|
306
|
134
|
307 (defcustom gopher-image-display-command "xv -geometry +200+200"
|
|
308 "*The command used to try to display an image object."
|
|
309 :type 'string
|
|
310 :group 'gopher)
|
0
|
311
|
|
312
|
134
|
313 (defcustom gopher-object-type-alist
|
0
|
314 '(( ?0 "" gopher-document-object)
|
|
315 ( ?1 "/" gopher-directory-object)
|
|
316 ( ?2 " <CSO>" gopher-cso-object)
|
|
317 ( ?3 " <error>" gopher-unimplemented-object)
|
|
318 ( ?4 " <binhex>" gopher-binary-object)
|
|
319 ( ?5 " <DOS>" gopher-binary-object)
|
|
320 ( ?6 " <UU>" gopher-binary-object)
|
|
321 ( ?7 " <?>" gopher-index-object)
|
|
322 ( ?8 " <TEL>" gopher-telnet-object)
|
|
323 ( ?9 " <bin>" gopher-binary-object)
|
|
324 ( ?T " <T>" gopher-unimplemented-object)
|
|
325 ( ?s " <)" gopher-binary-object)
|
|
326 ( ?M " <MIME>" gopher-unimplemented-object)
|
|
327 ( ?h " <html>" gopher-unimplemented-object)
|
|
328 ( ?I " <image>" gopher-image-object)
|
|
329 ( ?c " <cal>" gopher-unimplemented-object)
|
|
330 ( ?g " <GIF>" gopher-image-object)
|
|
331 )
|
|
332 "*Alist describing the types of gopher objects this client know about.
|
|
333 The keys are the gopher type characters.
|
|
334 The second element in each list is the string to tag onto the end
|
|
335 of an object's description, to identify it to the user.
|
|
336 The third element is the function to use to retrieve the object.
|
|
337 It is called with two arguments: the gopher object to retrieve and
|
|
338 the buffer which should be returned to when the user is done
|
134
|
339 with this object."
|
|
340 :type '(repeat (list character string function))
|
|
341 :group 'object)
|
0
|
342
|
|
343
|
|
344 ;;;
|
|
345 ;;; The data structure describing a gopher object is a vector of five elements:
|
|
346 ;;; [ TYPE DESCR SELECTOR HOST PORT ]
|
|
347 ;;;
|
|
348 ;;; TYPE is the type character.
|
|
349 ;;; DESCR is the human-readable description of the object.
|
|
350 ;;; SELECTOR is the opaque selector to be sent to HOST to retrieve the obj.
|
|
351 ;;; HOST is the name of the Internet host on which the object resides.
|
|
352 ;;; PORT is the TCP/IP port on which the host is listening.
|
|
353 ;;;
|
|
354 ;;; The following macros set and fetch elements of this structure.
|
|
355 ;;;
|
|
356
|
|
357 (defconst gopher-object-length 5)
|
|
358
|
|
359 (defmacro gopher-object-type (object)
|
|
360 "Return the gopher type of OBJECT."
|
|
361 (` (aref (, object) 0)))
|
|
362
|
|
363 (defmacro gopher-object-descr (object)
|
|
364 "Return the gopher description of OBJECT."
|
|
365 (` (aref (, object) 1)))
|
|
366
|
|
367 (defmacro gopher-object-selector (object)
|
|
368 "Return the gopher selector string for OBJECT."
|
|
369 (` (aref (, object) 2)))
|
|
370
|
|
371 (defmacro gopher-object-host (object)
|
|
372 "Return the gopher hostname for OBJECT."
|
|
373 (` (aref (, object) 3)))
|
|
374
|
|
375 (defmacro gopher-object-port (object)
|
|
376 "Return the gopher TCP port number for OBJECT."
|
|
377 (` (aref (, object) 4)))
|
|
378
|
|
379
|
|
380 (defmacro gopher-set-object-type (object type)
|
|
381 "Set the gopher type of OBJECT to TYPE."
|
|
382 (` (aset (, object) 0 (, type))))
|
|
383
|
|
384 (defmacro gopher-set-object-descr (object descr)
|
|
385 "Set the gopher description of OBJECT to DESCR."
|
|
386 (` (aset (, object) 1 (, descr))))
|
|
387
|
|
388 (defmacro gopher-set-object-selector (object selector)
|
|
389 "Set the gopher selector string for OBJECT to SELECTOR."
|
|
390 (` (aset (, object) 2 (, selector))))
|
|
391
|
|
392 (defmacro gopher-set-object-host (object host)
|
|
393 "Set the gopher hostname for OBJECT to HOST."
|
|
394 (` (aset (, object) 3 (, host))))
|
|
395
|
|
396 (defmacro gopher-set-object-port (object port)
|
|
397 "Set the gopher TCP port number for OBJECT to PORT."
|
|
398 (` (aset (, object) 4 (, port))))
|
|
399
|
|
400
|
|
401 (defmacro gopher-retrieve-document-cleanly (args handle &rest body)
|
|
402 "Call gopher-retrieve-document with condition-case wrapped around,
|
|
403 applying HANDLE if appropriate."
|
|
404 (` (condition-case nil
|
|
405 (progn
|
|
406 (gopher-retrieve-document (,@ args))
|
|
407 (,@ body))
|
|
408 (error (, handle)))))
|
|
409
|
|
410
|
|
411 ;;
|
|
412 ;; buffer-local variables.
|
|
413 ;; declared here to prevent warnings from the new byte-compiler.
|
|
414 ;;
|
|
415
|
|
416 (defvar gopher-dir nil)
|
|
417 (defvar gopher-last nil)
|
|
418 (defvar gopher-obj nil)
|
|
419 (defvar gopher-telnet-process-name nil)
|
|
420 (defvar gopher-bookmark-buffer-tick nil)
|
|
421 (defvar forms-accept-action nil)
|
|
422
|
|
423
|
|
424 ;;;;--------------------------------------------------------------------------
|
|
425 ;;;; main dispatching logic.
|
|
426 ;;;;
|
|
427
|
|
428 ;;;###autoload
|
|
429 (defun gopher (&optional askserv)
|
|
430 "Start a gopher session. With C-u, prompt for a gopher server."
|
|
431 (interactive "P")
|
|
432 (if askserv
|
|
433 (progn
|
|
434 (gopher-set-object-host
|
|
435 gopher-root-node
|
|
436 (read-string "Gopher server: "
|
|
437 (gopher-object-host gopher-root-node)))
|
|
438
|
|
439 (let (portstr port)
|
|
440 (while (not (numberp port))
|
|
441 (setq portstr
|
|
442 (read-string "Port: "
|
|
443 (int-to-string
|
|
444 (gopher-object-port gopher-root-node))))
|
|
445
|
|
446 (setq port (condition-case nil
|
|
447 (car (read-from-string portstr))
|
|
448 (error nil)))
|
|
449
|
|
450 (if (not (numberp port))
|
|
451 (progn
|
|
452 (ding)
|
|
453 (message "Port must be numeric")
|
|
454 (sit-for 1)))
|
|
455 )
|
|
456
|
|
457 (gopher-set-object-port gopher-root-node port))))
|
|
458
|
|
459 (gopher-read-bookmarks)
|
|
460
|
|
461 (gopher-dispatch-object gopher-root-node nil))
|
|
462
|
|
463
|
|
464
|
|
465 ;;;###autoload
|
|
466 (defun gopher-atpoint nil
|
|
467 "Try to interpret the text around point as a gopher bookmark, and dispatch
|
|
468 to that object."
|
|
469 (interactive)
|
|
470
|
|
471 (let (bkmk)
|
|
472 (save-excursion
|
|
473 (re-search-backward "^#[ \t]*$\\|^[ \t]*$\\|\\`")
|
|
474 (skip-chars-forward " \t\n")
|
|
475 (setq bkmk (gopher-parse-bookmark)))
|
|
476 (if bkmk
|
|
477 (progn
|
|
478 (gopher-read-bookmarks)
|
|
479 (gopher-dispatch-object bkmk nil))
|
|
480 (error "Illformed bookmark"))))
|
|
481
|
|
482
|
|
483 (defun gopher-dispatch-object (obj lastbuf)
|
|
484 "Dispatch a gopher object depending on its type."
|
|
485 (let ((typedesc (assq (gopher-object-type obj) gopher-object-type-alist)))
|
|
486
|
|
487 (if typedesc
|
|
488 (funcall (nth 2 typedesc) obj lastbuf)
|
|
489 (gopher-unimplemented-object obj lastbuf))))
|
|
490
|
|
491
|
|
492 (defun gopher-unimplemented-object (obj lastbuf)
|
|
493 (error "unimplemented object type"))
|
|
494
|
|
495
|
|
496 ;;;;--------------------------------------------------------------------------
|
|
497 ;;;; utilities
|
|
498 ;;;;
|
|
499
|
|
500 (defun gopher-next-field nil
|
|
501 "Returns as a string all chars between point and the next tab or newline.
|
|
502 Point is advanced to after the tab (or to the end-of-line)."
|
|
503
|
|
504 (let ((beg (point)) s)
|
|
505 (skip-chars-forward "^\t\n")
|
|
506 (setq s (buffer-substring beg (point)))
|
|
507 (if (eq (following-char) ?\t)
|
|
508 (forward-char))
|
|
509 s))
|
|
510
|
|
511
|
|
512 ;; from GNUS
|
|
513 (defun gopher-make-local-vars (&rest pairs)
|
|
514 ;; Take VARIABLE-VALUE pairs and makes local variables initialized to the
|
|
515 ;; value.
|
|
516 (while pairs
|
|
517 (make-local-variable (car pairs))
|
|
518 (set (car pairs) (car (cdr pairs)))
|
|
519 (setq pairs (cdr (cdr pairs)))))
|
|
520
|
|
521
|
|
522 (defun gopher-get-tmp-buf nil
|
|
523 "Get a temporary buffer in which to receive gopher output."
|
|
524 (or (bufferp gopher-tmp-buf)
|
|
525 (progn
|
|
526 (setq gopher-tmp-buf (get-buffer-create " *gopher-tmp*"))
|
|
527 (buffer-flush-undo gopher-tmp-buf)))
|
|
528 gopher-tmp-buf)
|
|
529
|
|
530
|
|
531 (defun gopher-get-dir-buf (descr)
|
|
532 "Get a new buffer suitable for a gopher directory or document."
|
|
533 (let ((buf (generate-new-buffer (concat "*gopher*" descr))))
|
|
534 (buffer-flush-undo buf)
|
|
535 buf))
|
|
536
|
|
537 (fset 'gopher-get-doc-buf (symbol-function 'gopher-get-dir-buf))
|
|
538
|
|
539
|
|
540 (defun gopher-trim-blanks (str)
|
|
541 "Remove leading and trailing blanks from STR."
|
|
542 (string-match "\\`[ \t\n]*" str)
|
|
543 (substring str
|
|
544 (match-end 0)
|
|
545 (string-match "[ \t\n]*\\'" str (match-end 0))))
|
|
546
|
|
547
|
|
548 ;;;;--------------------------------------------------------------------------
|
|
549 ;;;; directory handling
|
|
550 ;;;;
|
|
551
|
|
552
|
|
553 (defun gopher-directory-object (obj oldbuf)
|
|
554 "Retrieve and display a gopher directory."
|
|
555
|
|
556 (let ((tmpbuf (gopher-get-tmp-buf))
|
|
557 (dirbuf (gopher-get-dir-buf (gopher-object-descr obj))))
|
|
558
|
|
559 ;; Get the directory...
|
|
560 (gopher-retrieve-document-cleanly (tmpbuf
|
|
561 (gopher-object-selector obj)
|
|
562 (gopher-object-host obj)
|
|
563 (gopher-object-port obj))
|
|
564
|
|
565 (progn
|
|
566 (kill-buffer dirbuf)
|
|
567 (error "Problems retrieving directory."))
|
|
568
|
|
569 ;; Parse it and store our internal representation in gopher-dir.
|
|
570 (switch-to-buffer dirbuf)
|
|
571 (gopher-make-local-vars
|
|
572 'gopher-dir (gopher-parse-directory tmpbuf)
|
|
573 'gopher-obj obj
|
|
574 'gopher-last oldbuf)
|
|
575
|
|
576 ;; Format it for your viewing pleasure.
|
|
577 (gopher-format-directory gopher-dir dirbuf)
|
|
578 (goto-char (point-min))
|
|
579 (if (> (- (point-max) (point)) 7) (forward-char 7))
|
|
580
|
|
581 ;; Turn on directory mode and put the description in the mode line.
|
|
582 (gopher-directory-mode)
|
|
583 (setq mode-line-buffer-identification (concat "Gopher: "
|
|
584 (gopher-object-descr obj)))
|
|
585 )))
|
|
586
|
|
587
|
|
588 (defun gopher-parse-directory (buf)
|
|
589 "Parse the gopher directory in buffer BUF into our internal representation.
|
|
590 Returns a vector of gopher objects."
|
|
591
|
|
592 (save-excursion
|
|
593 (set-buffer buf)
|
|
594 (goto-char (point-min))
|
|
595
|
|
596 (let* ((len (count-lines (point-min) (point-max)))
|
|
597 (dir (make-vector len nil))
|
|
598 (i 0))
|
|
599
|
|
600 (while (not (eobp))
|
|
601 (aset dir i (gopher-parse-directory-line))
|
|
602 (setq i (1+ i))
|
|
603 (forward-line 1))
|
|
604
|
|
605 dir)))
|
|
606
|
|
607
|
|
608 (defun gopher-parse-directory-line nil
|
|
609 "Parse the line containing point as a gopher directory entry.
|
|
610 Returns the corresponding gopher object."
|
|
611
|
|
612 (let (type descr selector host port)
|
|
613 (beginning-of-line)
|
|
614 (setq type (following-char))
|
|
615 (forward-char)
|
|
616 (setq descr (gopher-next-field))
|
|
617 (setq selector (gopher-next-field))
|
|
618 (setq host (gopher-next-field))
|
|
619 (setq port (gopher-next-field))
|
|
620
|
|
621 (if (string-match "^[0-9]+$" port)
|
|
622 (setq port (string-to-int port)))
|
|
623
|
|
624 (vector type descr selector host port)))
|
|
625
|
|
626
|
|
627 (defun gopher-format-directory (dir buf)
|
|
628 "Print the directory vector DIR into buffer BUF."
|
|
629
|
|
630 (save-excursion
|
|
631 (set-buffer buf)
|
|
632 (erase-buffer)
|
|
633 (let ((i 0)
|
|
634 (len (length dir)))
|
|
635 (while (< i len)
|
|
636 (gopher-format-directory-line (aref dir i) (1+ i))
|
|
637 (setq i (1+ i)))
|
|
638
|
|
639 )))
|
|
640
|
|
641
|
|
642 (defun gopher-format-directory-line (obj ndx)
|
|
643 "Insert a line describing the gopher object OBJ into the current buffer.
|
|
644 NDX is a numeric index to display to the left of the object description."
|
|
645
|
|
646 (let ((ndx-str (int-to-string ndx))
|
|
647 (typedesc (assq (gopher-object-type obj) gopher-object-type-alist)))
|
|
648
|
|
649 ;; display the index number. use 5 digits, right-justified.
|
|
650 (if (< (length ndx-str) 5)
|
|
651 (insert (make-string (- 5 (length ndx-str)) ? )))
|
|
652 (insert ndx-str)
|
|
653 (insert ". ")
|
|
654
|
|
655 ;; add the object description.
|
|
656 (insert (gopher-object-descr obj))
|
|
657
|
|
658 ;; add a tag indicating the gopher object type.
|
|
659 (insert (if typedesc
|
|
660 (nth 1 typedesc)
|
|
661 (concat " ???" (char-to-string (gopher-object-type obj)))))
|
|
662
|
|
663 (insert "\n")))
|
|
664
|
|
665
|
|
666 (defun gopher-directory-mode nil
|
|
667 "Gopher directory mode.
|
|
668
|
|
669 \\{gopher-directory-mode-map}
|
|
670 "
|
|
671 (use-local-map gopher-directory-mode-map)
|
|
672 (setq major-mode 'gopher-directory-mode)
|
|
673 (setq mode-name "gopher dir")
|
|
674 (run-hooks 'gopher-directory-mode-hook)
|
|
675 (setq buffer-read-only t)
|
|
676
|
|
677 (require 'mode-motion)
|
|
678 (make-local-variable 'mode-motion-hook)
|
|
679 (setq mode-motion-hook 'mode-motion-highlight-line)
|
|
680 )
|
|
681
|
|
682 ;;; keymap for directory mode
|
|
683 (suppress-keymap gopher-directory-mode-map)
|
|
684 (define-key gopher-directory-mode-map "\r" 'gopher-directory-choose)
|
|
685 (define-key gopher-directory-mode-map " " 'gopher-directory-choose)
|
|
686 (define-key gopher-directory-mode-map "l" 'gopher-last-node)
|
|
687 (define-key gopher-directory-mode-map "q" 'gopher-last-node)
|
|
688 (define-key gopher-directory-mode-map "u" 'gopher-last-node)
|
|
689 (define-key gopher-directory-mode-map "=" 'gopher-directory-show-object)
|
|
690 (define-key gopher-directory-mode-map "Q" 'gopher-quit)
|
|
691 (define-key gopher-directory-mode-map "f" 'gopher-directory-choose)
|
|
692 (define-key gopher-directory-mode-map "e" 'gopher-directory-choose)
|
|
693
|
|
694 ; Virginia Peck <vapeck@cs> Mon Aug 10 1992
|
|
695 (define-key gopher-directory-mode-map "n" 'next-line)
|
|
696 (define-key gopher-directory-mode-map "p" 'previous-line)
|
|
697 ;;(define-key gopher-directory-mode-map "\C-xk" 'gopher-last-node)
|
|
698
|
|
699 ; Stewart Clamen <clamen@cs.cmu.edu> Mon Aug 17 1992
|
|
700 (define-key gopher-directory-mode-map "v" 'gopher-display-bookmarks)
|
|
701 (define-key gopher-directory-mode-map "a" 'gopher-add-bookmark)
|
|
702 (define-key gopher-directory-mode-map "\C-k" 'gopher-delete-bookmark)
|
|
703 (define-key gopher-directory-mode-map "s" 'gopher-directory-save-bookmarks)
|
|
704 (define-key gopher-directory-mode-map "\C-y" 'gopher-yank-bookmark)
|
|
705
|
|
706 (define-key gopher-directory-mode-map 'button2 'gopher-mouse-directory-choose)
|
|
707 (define-key gopher-directory-mode-map 'button3 'gopher-directory-menu)
|
|
708
|
|
709
|
|
710 (defvar gopher-directory-menu
|
|
711 '("Gopher Commands"
|
|
712 ["Select This Link" gopher-directory-choose t]
|
|
713 ["Goto Last Node" gopher-last-node t]
|
|
714 ["Show Object Internals" gopher-directory-show-object t]
|
|
715 ["Quit" gopher-quit t]
|
|
716 ))
|
|
717
|
|
718 (defun gopher-directory-menu (event)
|
|
719 (interactive "e")
|
|
720 (mouse-set-point event)
|
|
721 (beginning-of-line)
|
|
722 (popup-menu gopher-directory-menu))
|
|
723
|
|
724 (defun gopher-mouse-directory-choose (event arg)
|
|
725 (interactive "e\nP")
|
|
726 (mouse-set-point event)
|
|
727 (beginning-of-line)
|
|
728 (gopher-directory-choose arg))
|
|
729
|
|
730
|
|
731 (defun gopher-directory-nth-obj (n)
|
|
732 "Returns the Nth object (starting at 1) in a gopher directory buffer."
|
|
733 (if (or (<= n 0) (> n (length gopher-dir)))
|
|
734 (error "Out of range."))
|
|
735 (aref gopher-dir (1- n)))
|
|
736
|
|
737
|
|
738 (defun gopher-directory-n (arg)
|
|
739 "Return the index of the object specified by ARG (starting at 1).
|
|
740 If ARG is nil, this is the index of the current line.
|
|
741 Otherwise, it is the value of ARG (as a prefix argument)."
|
|
742 (if arg
|
|
743 (prefix-numeric-value arg)
|
|
744 (if (eq (point) (point-max))
|
|
745 (1+ (count-lines (point-min) (point-max)))
|
|
746 (count-lines (point-min) (1+ (point))))))
|
|
747
|
|
748
|
|
749 (defun gopher-directory-obj (arg)
|
|
750 "Return the gopher object given by prefix arg ARG.
|
|
751 If it is nil, return the object given by the line point is on.
|
|
752 Otherwise, ARG is the index of the object."
|
|
753 (gopher-directory-nth-obj (gopher-directory-n arg)))
|
|
754
|
|
755
|
|
756 (defun gopher-directory-choose (arg)
|
|
757 "Choose an item from the directory, and do whatever is appropriate
|
|
758 based on the object's type. Default is to choose the object given by the
|
|
759 line the cursor is on. With numeric prefix argument N, choose object N."
|
|
760 (interactive "P")
|
|
761 (gopher-dispatch-object (gopher-directory-obj arg) (current-buffer)))
|
|
762
|
|
763
|
|
764 (defun gopher-directory-show-object (arg)
|
|
765 "Dump the internal information in a gopher object.
|
|
766 With numeric prefix argument N, show information about the Nth object."
|
|
767 (interactive "P")
|
|
768 (let* ((obj (gopher-directory-obj arg))
|
|
769 (type (gopher-object-type obj))
|
|
770 (typespec (assq type gopher-object-type-alist))
|
|
771 (typetag (if typespec (nth 1 typespec) "?"))
|
|
772 (typeproc (if typespec (nth 2 typespec) "?")))
|
|
773 (with-output-to-temp-buffer "*Gopher object*"
|
|
774 (princ (format "Type : %c `%s' %s\n" type typetag typeproc))
|
|
775 (princ (format "Description : %s\n" (gopher-object-descr obj)))
|
|
776 (princ (format "Selector : %s\n" (gopher-object-selector obj)))
|
|
777 (princ (format "Host : %s\n" (gopher-object-host obj)))
|
|
778 (princ (format "Port : %s\n" (gopher-object-port obj)))
|
|
779 (current-buffer)
|
|
780 ))
|
|
781 (shrink-window-if-larger-than-buffer (get-buffer-window "*Gopher object*"))
|
|
782
|
|
783 ;; shrink-window-if-larger-than-buffer screws these up...
|
|
784 (set-buffer-modified-p nil)
|
|
785 (setq buffer-read-only t))
|
|
786
|
|
787
|
|
788 (defun gopher-last-node nil
|
|
789 "Return to the previous gopher node.
|
|
790 By convention, a gopher buffer has the local variable gopher-last which
|
|
791 contains the buffer to which we should return."
|
|
792 (interactive)
|
|
793 (let ((oldbuf (current-buffer)))
|
|
794 (if gopher-last
|
|
795 (progn
|
|
796 (switch-to-buffer gopher-last)
|
|
797 (kill-buffer oldbuf)
|
|
798 (and (gopher-bookmark-directory-p)
|
|
799 (> gopher-bookmark-modified-tick gopher-bookmark-buffer-tick)
|
|
800 (let ((ppos (1- (gopher-directory-n nil))))
|
|
801 (gopher-format-bookmarks)
|
|
802 (forward-line ppos)
|
|
803 (if (> (- (point-max) (point)) 7) (forward-char 7)))))
|
|
804 (if (and gopher-support-bookmarks
|
|
805 gopher-bookmarks-modified
|
|
806 (y-or-n-p
|
|
807 "Changes have been made to the Bookmark directory. Save? "))
|
|
808 (gopher-save-bookmarks))
|
|
809 (kill-buffer oldbuf))))
|
|
810
|
|
811
|
|
812 (defun gopher-directory-save-bookmarks ()
|
|
813 "Save the bookmark list."
|
|
814 (interactive)
|
|
815
|
|
816 (if (not (gopher-bookmark-directory-p))
|
|
817 (error "This isn't the bookmark directory."))
|
|
818
|
|
819 (gopher-save-bookmarks))
|
|
820
|
|
821
|
|
822
|
|
823 ;;; Gopher clean-up and quit.
|
|
824 ;;; Originally from Thomas L|fgren <tde9104@abacus.hgs.se>
|
|
825
|
|
826 (defun gopher-quit nil
|
|
827 "Quit gopher, and kill all gopher buffers.
|
|
828 If there are unsaved changes to your bookmark directory, you will be
|
|
829 asked if you want to save them"
|
|
830 (interactive)
|
|
831 (if (y-or-n-p "Do you really want to kill all gopher buffers? ")
|
|
832 (progn
|
|
833 (if (and gopher-support-bookmarks
|
|
834 gopher-bookmarks-modified
|
|
835 (y-or-n-p
|
|
836 "Changes have been made to the Bookmark directory. Save? "))
|
|
837 (gopher-save-bookmarks))
|
|
838 (let ((buflist (buffer-list))
|
|
839 (case-fold-search t))
|
|
840 (while buflist
|
|
841 (if (eq (string-match "\\*gopher" (buffer-name (car buflist))) 0)
|
|
842 (kill-buffer (car buflist)))
|
|
843 (setq buflist (cdr buflist)))))))
|
|
844
|
|
845
|
|
846 ;;;;--------------------------------------------------------------------------
|
|
847 ;;;; bookmarks (Implemented originally by clamen@cs.cmu.edu)
|
|
848 ;;;;
|
|
849
|
|
850
|
|
851 (defun gopher-read-bookmarks ()
|
|
852 (cond ((null gopher-support-bookmarks))
|
|
853 ((or (equal gopher-support-bookmarks 'unix)
|
|
854 (equal gopher-support-bookmarks t))
|
|
855 (setq gopher-bookmarks
|
|
856 (gopher-read-unix-bookmarks)))
|
|
857 ((stringp gopher-support-bookmarks)
|
|
858 (gopher-read-lisp-bookmarks gopher-support-bookmarks))
|
|
859 ((vectorp gopher-support-bookmarks)
|
|
860 (setq gopher-bookmarks gopher-support-bookmarks))
|
|
861 (t
|
|
862 (message "Illformed gopher-bookmarks, assuming none"))))
|
|
863
|
|
864
|
|
865 (defun gopher-read-unix-bookmarks ()
|
|
866 "Read bookmarks out of ~/.gopherrc file."
|
|
867 (let ((rcfile "~/.gopherrc"))
|
|
868 (if (file-exists-p rcfile)
|
|
869 (let* ((rcbuf (find-file-noselect rcfile))
|
|
870 (bkmks (gopher-parse-bookmark-buffer rcbuf)))
|
|
871 (kill-buffer rcbuf)
|
|
872 (setq gopher-bookmarks-modified nil)
|
|
873 (setq gopher-bookmark-modified-tick
|
|
874 (1+ gopher-bookmark-modified-tick))
|
|
875 bkmks)
|
|
876 (message "No %s exists." rcfile)
|
|
877 nil)))
|
|
878
|
|
879 (defun gopher-parse-bookmark-buffer (buf)
|
|
880 "Read buffer containing bookmarks, formatted like ~.gopherrc
|
|
881 in UNIX gopher client."
|
|
882 (save-excursion
|
|
883 (set-buffer buf)
|
|
884 (goto-char (point-min))
|
|
885 (if (re-search-forward "^bookmarks:\n" (point-max) t)
|
|
886 (let (bkmk bkmks)
|
|
887 (while (setq bkmk (gopher-parse-bookmark))
|
|
888 (setq bkmks (cons bkmk bkmks)))
|
|
889 (apply 'vector (reverse bkmks))))))
|
|
890
|
|
891 (defun gopher-parse-bookmark-line (regexp end setf bkmk)
|
|
892 (save-excursion
|
|
893 (if (re-search-forward regexp end t)
|
|
894 (eval (list setf bkmk
|
|
895 (buffer-substring (match-beginning 1) (match-end 1))))
|
|
896 )))
|
|
897
|
|
898
|
|
899 (defun gopher-parse-bookmark ()
|
|
900 "Read next bookmark. Return a directory object."
|
|
901 (if (looking-at "^#$")
|
|
902 (forward-line))
|
|
903 (if (not (eobp))
|
|
904 (let ((end (save-excursion
|
|
905 (forward-line 5)
|
|
906 (point)))
|
|
907 (bkmk (make-vector 5 nil)))
|
|
908 (prog1
|
|
909 (and (gopher-parse-bookmark-line "^Type *= *\\(.+\\) *$" end
|
|
910 'gopher-set-object-type bkmk)
|
|
911 (gopher-parse-bookmark-line "^Name *= *\\(.*\\) *$" end
|
|
912 'gopher-set-object-descr bkmk)
|
|
913 (gopher-parse-bookmark-line "^Path *= *\\(.*\\) *$" end
|
|
914 'gopher-set-object-selector bkmk)
|
|
915 (gopher-parse-bookmark-line "^Host *= *\\(.+\\) *$" end
|
|
916 'gopher-set-object-host bkmk)
|
|
917 (gopher-parse-bookmark-line "^Port *= *\\(.+\\) *$" end
|
|
918 'gopher-set-object-port bkmk)
|
|
919 (progn
|
|
920 (gopher-set-object-type
|
|
921 bkmk (string-to-char (gopher-object-type bkmk)))
|
|
922 (gopher-set-object-port
|
|
923 bkmk (string-to-int (gopher-object-port bkmk)))
|
|
924 bkmk))
|
|
925 (goto-char end))
|
|
926 )))
|
|
927
|
|
928 (defun gopher-format-bookmarks ()
|
|
929 "Make the current buffer (which is assumed to be a bookmark buffer)
|
|
930 contain an up-to-date listing of the bookmark list."
|
|
931
|
|
932 (let ((buffer-read-only nil))
|
|
933 (erase-buffer)
|
|
934 (setq gopher-dir gopher-bookmarks)
|
|
935
|
|
936 ;; Format it for your viewing pleasure.
|
|
937 (gopher-format-directory gopher-dir (current-buffer))
|
|
938 (goto-char (point-min))
|
|
939 (if (> (- (point-max) (point)) 7) (forward-char 7))
|
|
940 (setq gopher-bookmark-buffer-tick gopher-bookmark-modified-tick)))
|
|
941
|
|
942 (defun gopher-display-bookmarks ()
|
|
943 "Retrieve and display the gopher bookmark directory."
|
|
944 (interactive)
|
|
945
|
|
946 (if (> (length gopher-bookmarks) 0)
|
|
947 (let ((oldbuf (current-buffer))
|
|
948 (dirbuf (gopher-get-dir-buf "*Gopher Bookmarks*")))
|
|
949
|
|
950 ;; Store our internal representation in gopher-dir.
|
|
951 (switch-to-buffer dirbuf)
|
|
952 (gopher-make-local-vars
|
|
953 'gopher-dir gopher-bookmarks
|
|
954 'gopher-bookmark-directory-p t
|
|
955 'gopher-bookmark-buffer-tick gopher-bookmark-modified-tick
|
|
956 'gopher-obj 'bookmark
|
|
957 'gopher-last oldbuf)
|
|
958
|
|
959 (gopher-format-bookmarks)
|
|
960
|
|
961 ;; Turn on directory mode and put the description in the mode line.
|
|
962 (gopher-directory-mode)
|
|
963 (setq mode-line-buffer-identification (concat "Gopher: *Bookmarks*"))
|
|
964 )
|
|
965 (error "No bookmarks supported.")))
|
|
966
|
|
967
|
|
968 (defun gopher-save-bookmarks ()
|
|
969 "Save bookmarks."
|
|
970 (cond
|
|
971 ((or (equal gopher-support-bookmarks 'unix)
|
|
972 (equal gopher-support-bookmarks t))
|
|
973 (gopher-save-unix-bookmarks))
|
|
974 ((stringp gopher-support-bookmarks)
|
|
975 (gopher-save-lisp-bookmarks gopher-support-bookmarks))
|
|
976 (t
|
|
977 (message "Illformed gopher-support-bookmarks, assuming none")))
|
|
978
|
|
979 (setq gopher-bookmarks-modified nil))
|
|
980
|
|
981
|
|
982 (defun gopher-save-unix-bookmarks ()
|
|
983 "Save bookmarks out to ~/.gopherrc file."
|
|
984 (save-excursion
|
|
985 (let* ((rcfile "~/.gopherrc")
|
|
986 (new-file-p (not (file-exists-p rcfile)))
|
|
987 (rcbuf (find-file-noselect rcfile)))
|
|
988 (set-buffer rcbuf)
|
|
989 (if new-file-p
|
|
990 (insert "bookmarks:\n")
|
|
991 (goto-char (point-min))
|
|
992 (if (re-search-forward "^bookmarks:\n" nil t)
|
|
993 (delete-region (point) (point-max))
|
|
994 (goto-char (point-max))
|
|
995 (insert "bookmarks:\n")))
|
|
996
|
|
997 ;; Now, insert defined bookmarks into file
|
|
998
|
|
999 (let ((obj-count 0))
|
|
1000 (while (< obj-count (length gopher-bookmarks))
|
|
1001 (let ((obj (aref gopher-bookmarks obj-count)))
|
|
1002 (insert "#"
|
|
1003 "\nType=" (gopher-object-type obj)
|
|
1004 "\nName=" (gopher-object-descr obj)
|
|
1005 "\nPath=" (gopher-object-selector obj)
|
|
1006 "\nHost=" (gopher-object-host obj)
|
|
1007 "\nPort=" (int-to-string (gopher-object-port obj))
|
|
1008 "\n")
|
|
1009 (setq obj-count (1+ obj-count)))))
|
|
1010
|
|
1011 (write-file rcfile))))
|
|
1012
|
|
1013
|
|
1014 (defun gopher-add-bookmark (arg)
|
|
1015 "Add current object to menu of bookmarks.
|
|
1016 With numeric prefix argument N, add Nth object."
|
|
1017 (interactive "P")
|
|
1018 (if (gopher-bookmark-directory-p)
|
|
1019 (error "That item is already a bookmark!")
|
|
1020 (let ((existing-bookmarks gopher-bookmarks)
|
|
1021 (new-bookmarks (make-vector (1+ (length gopher-bookmarks)) nil))
|
|
1022 (obj (copy-sequence (gopher-directory-obj arg)))
|
|
1023 (l (length gopher-bookmarks)))
|
|
1024 (gopher-set-object-descr
|
|
1025 obj
|
|
1026 (read-from-minibuffer "Node Name: "
|
|
1027 (gopher-object-descr obj)))
|
|
1028 (aset new-bookmarks l obj)
|
|
1029 (while (> l 0)
|
|
1030 (progn (setq l (1- l))
|
|
1031 (aset new-bookmarks l (aref existing-bookmarks l))))
|
|
1032 (setq gopher-bookmarks new-bookmarks
|
|
1033 gopher-bookmarks-modified t
|
|
1034 gopher-bookmark-modified-tick (1+ gopher-bookmark-modified-tick))
|
|
1035 )))
|
|
1036
|
|
1037
|
|
1038 (defun gopher-delete-bookmark (arg)
|
|
1039 "Delete current bookmark.
|
|
1040 With numeric prefix argument N, delete Nth bookmark."
|
|
1041 (interactive "P")
|
|
1042 (if (not (gopher-bookmark-directory-p))
|
|
1043 (error "Can only delete object in Bookmark directory.")
|
|
1044 (let ((new-bookmarks (make-vector (1- (length gopher-bookmarks)) nil))
|
|
1045 (pos (1- (gopher-directory-n arg)))
|
|
1046 (l (length gopher-bookmarks))
|
|
1047 (i 0))
|
|
1048 (while (< i pos)
|
|
1049 (progn (aset new-bookmarks i (aref gopher-bookmarks i))
|
|
1050 (setq i (1+ i))))
|
|
1051 (while (< i (1- l))
|
|
1052 (progn (aset new-bookmarks i (aref gopher-bookmarks (1+ i)))
|
|
1053 (setq i (1+ i))))
|
|
1054 (setq gopher-killed-bookmark (aref gopher-bookmarks pos)
|
|
1055 gopher-bookmarks new-bookmarks
|
|
1056 gopher-dir new-bookmarks
|
|
1057 gopher-bookmarks-modified t
|
|
1058 gopher-bookmark-modified-tick (1+ gopher-bookmark-modified-tick))
|
|
1059 (let ((ppos (1- (gopher-directory-n nil))))
|
|
1060 (if (< pos ppos)
|
|
1061 (setq ppos (1- ppos)))
|
|
1062 (gopher-format-bookmarks)
|
|
1063 (goto-char (point-min))
|
|
1064 (forward-line ppos)
|
|
1065 (forward-char 7)))
|
|
1066 (if (= (point) (point-max)) (previous-line 1))
|
|
1067 ; (let ((buffer-read-only nil))
|
|
1068 ; (beginning-of-line 1)
|
|
1069 ; (kill-line 1)
|
|
1070 ; (if (= (point) (point-max)) (previous-line 1)))
|
|
1071 (if (zerop (length gopher-bookmarks))
|
|
1072 (gopher-last-node))))
|
|
1073
|
|
1074
|
|
1075 (defun gopher-yank-bookmark (arg)
|
|
1076 "Yank the most recently killed bookmark at the current position.
|
|
1077 With numeric prefix argument N, yank into position N."
|
|
1078 (interactive "P")
|
|
1079 (cond ((not (gopher-bookmark-directory-p))
|
|
1080 (error "Can only yank bookmark objects into bookmark directory."))
|
|
1081 ((null gopher-killed-bookmark)
|
|
1082 (error "No killed bookmark object"))
|
|
1083 (t
|
|
1084 (let* ((len (length gopher-bookmarks))
|
|
1085 (new-bookmarks (make-vector (1+ len) nil))
|
|
1086 (pos (1- (gopher-directory-n arg)))
|
|
1087 i)
|
|
1088
|
|
1089 (if (or (< pos 0) (> pos (length gopher-bookmarks)))
|
|
1090 (error "Out of range."))
|
|
1091
|
|
1092 (setq i (1- pos))
|
|
1093 (while (>= i 0)
|
|
1094 (aset new-bookmarks i (aref gopher-bookmarks i))
|
|
1095 (setq i (1- i)))
|
|
1096
|
|
1097 (aset new-bookmarks pos gopher-killed-bookmark)
|
|
1098
|
|
1099 (setq i pos)
|
|
1100 (while (< i len)
|
|
1101 (aset new-bookmarks (1+ i) (aref gopher-bookmarks i))
|
|
1102 (setq i (1+ i)))
|
|
1103
|
|
1104 (setq gopher-bookmarks new-bookmarks
|
|
1105 gopher-bookmarks-modified t
|
|
1106 gopher-killed-bookmark nil
|
|
1107 gopher-bookmark-modified-tick
|
|
1108 (1+ gopher-bookmark-modified-tick))
|
|
1109
|
|
1110 (let ((ppos (1- (gopher-directory-n nil))))
|
|
1111 (if (<= pos ppos)
|
|
1112 (setq ppos (1+ ppos)))
|
|
1113 (gopher-format-bookmarks)
|
|
1114 (goto-char (point-min))
|
|
1115 (forward-line ppos)
|
|
1116 (forward-char 7))
|
|
1117 ))))
|
|
1118
|
|
1119
|
|
1120 (defun gopher-bookmark-directory-p ()
|
|
1121 "Return T if currently displaying Bookmark directory."
|
|
1122 gopher-bookmark-directory-p)
|
|
1123 ; (equal gopher-dir gopher-bookmarks))
|
|
1124
|
|
1125
|
|
1126 (defun gopher-read-lisp-bookmarks (fn)
|
|
1127 "currently unsupported"
|
|
1128 (error "gopher-read-lisp-bookmark is not yet supported. Sorry."))
|
|
1129
|
|
1130 (defun gopher-save-lisp-bookmarks (fn)
|
|
1131 "currently unsupported"
|
|
1132 (error "gopher-save-lisp-bookmark is not yet supported. Sorry."))
|
|
1133
|
|
1134
|
|
1135
|
|
1136 ;;;;--------------------------------------------------------------------------
|
|
1137 ;;;; gopher documents
|
|
1138 ;;;;
|
|
1139
|
|
1140
|
|
1141 (defun gopher-document-object (obj oldbuf &optional end-regexp)
|
|
1142 "Retrieve and display a gopher document.
|
|
1143 Optional argument END-REGEXP is used if the data will not be ended by `.'."
|
|
1144
|
|
1145 (let ((docbuf (gopher-get-doc-buf (gopher-object-descr obj))))
|
|
1146
|
|
1147 ;; Snarf the data into the buffer.
|
|
1148 (gopher-retrieve-document-cleanly (docbuf
|
|
1149 (gopher-object-selector obj)
|
|
1150 (gopher-object-host obj)
|
|
1151 (gopher-object-port obj)
|
|
1152 end-regexp)
|
|
1153
|
|
1154 (progn
|
|
1155 (kill-buffer docbuf)
|
|
1156 (error "Problems retrieving document."))
|
|
1157
|
|
1158 ;; Turn on document mode and put the description in the mode line.
|
|
1159 (switch-to-buffer docbuf)
|
|
1160 (gopher-make-local-vars
|
|
1161 'gopher-obj obj
|
|
1162 'gopher-last oldbuf)
|
|
1163 (goto-char (point-min))
|
|
1164 (gopher-document-mode)
|
|
1165 (setq mode-line-buffer-identification (concat "Gopher: "
|
|
1166 (gopher-object-descr obj)))
|
|
1167 )))
|
|
1168
|
|
1169
|
|
1170 ;; keymap for document mode
|
|
1171 (suppress-keymap gopher-document-mode-map)
|
|
1172
|
|
1173 ;Virginia Peck <vapeck@cs> Mon Aug 10 21:44:35 1992
|
|
1174 ;;(define-key gopher-document-mode-map "\C-xk" 'gopher-last-node)
|
|
1175
|
|
1176 (define-key gopher-document-mode-map "l" 'gopher-last-node)
|
|
1177 (define-key gopher-document-mode-map "q" 'gopher-last-node)
|
|
1178 (define-key gopher-document-mode-map "u" 'gopher-last-node)
|
|
1179 (define-key gopher-document-mode-map " " 'scroll-up)
|
|
1180 (define-key gopher-document-mode-map "\C-?" 'scroll-down)
|
|
1181 (define-key gopher-document-mode-map "\r" 'gopher-scroll-one-line-up)
|
|
1182
|
|
1183
|
|
1184 (defun gopher-document-mode nil
|
|
1185 "Gopher document mode.
|
|
1186
|
|
1187 \\{gopher-document-mode-map}
|
|
1188 "
|
|
1189 (use-local-map gopher-document-mode-map)
|
|
1190 (setq major-mode 'gopher-document-mode)
|
|
1191 (setq mode-name "gopher doc")
|
|
1192 (run-hooks 'gopher-document-mode-hook)
|
|
1193 (setq buffer-read-only t))
|
|
1194
|
|
1195
|
|
1196 ;; from gosmacs.el
|
|
1197 (defun gopher-scroll-one-line-up (&optional arg)
|
|
1198 "Scroll the selected window up (forward in the text) one line (or N lines)."
|
|
1199 (interactive "p")
|
|
1200 (scroll-up (or arg 1)))
|
|
1201
|
|
1202
|
|
1203 ;;;;--------------------------------------------------------------------------
|
|
1204 ;;;; CSO handling.
|
|
1205 ;;;;
|
|
1206 ;;;; uses a subset of forms mode to handle data entry.
|
|
1207 ;;;;
|
|
1208
|
|
1209 (defun gopher-cso-object (obj oldbuf)
|
|
1210 "Display a CSO lookup form."
|
|
1211
|
|
1212 ;; The following will create a buffer displaying the form described
|
|
1213 ;; by the list in the last argument (cf. forms-mode). When the user
|
|
1214 ;; accepts the data in the form (by pressing `C-c RET'), the function
|
|
1215 ;; gopher-do-cso will be called with the data the user supplied.
|
|
1216 (gopher-form (gopher-object-descr obj)
|
|
1217 'gopher-do-cso
|
|
1218 4
|
|
1219 '("====== phone directory lookup ======"
|
|
1220 "\n Press `C-c RET' to lookup, `C-c l' to return to the last gopher object."
|
|
1221 "\n (you must fill in at least one of the first three fields)"
|
|
1222 "\n"
|
|
1223 "Name : " 1
|
|
1224 "\n"
|
|
1225 "Phone : " 2
|
|
1226 "\n"
|
|
1227 "E-Mail : " 3
|
|
1228 "\n"
|
|
1229 "Address : " 4
|
|
1230 ))
|
|
1231
|
|
1232 ;; Record gopher-last so gopher-last-node knows where to go.
|
|
1233 ;; Record gopher-obj so gopher-do-cso knows what server to query.
|
|
1234 (gopher-make-local-vars
|
|
1235 'gopher-last oldbuf
|
|
1236 'gopher-obj obj))
|
|
1237
|
|
1238
|
|
1239 (defconst gopher-cso-fields '("name" "phone" "email" "address")
|
|
1240 "Field names to use in CSO queries.")
|
|
1241
|
|
1242 (defun gopher-do-cso (vals)
|
|
1243 "Make a CSO query. VALS is the data the user entered in the form,
|
|
1244 as a list of strings."
|
|
1245
|
|
1246 ;; Check that the required data was provided.
|
|
1247 (if (zerop (+ (length (nth 0 vals))
|
|
1248 (length (nth 1 vals))
|
|
1249 (length (nth 2 vals))))
|
|
1250 (error "Must specify name, phone, or email."))
|
|
1251
|
|
1252 (let ((query "query")
|
|
1253 (fields gopher-cso-fields)
|
|
1254 (obj gopher-obj))
|
|
1255
|
|
1256 ;; Form the query string
|
|
1257 (while vals
|
|
1258
|
|
1259 (if (not (zerop (length (car vals))))
|
|
1260 (setq query (concat query " " (car fields) "=" (car vals))))
|
|
1261
|
|
1262 (setq vals (cdr vals))
|
|
1263 (setq fields (cdr fields)))
|
|
1264
|
|
1265 ;; Use this string as the object selector.
|
|
1266 (gopher-set-object-selector gopher-obj query)
|
|
1267
|
|
1268 ;; Retrieve the data from the server. Unlike gopher, the CSO data
|
|
1269 ;; does not use `.' as a terminator.
|
|
1270 (gopher-document-object gopher-obj (current-buffer) "^[2-9]")
|
|
1271
|
|
1272 ;; Strip CSO control information from the buffer.
|
|
1273 (gopher-clean-cso-buffer obj)))
|
|
1274
|
|
1275
|
|
1276 (defun gopher-clean-cso-buffer (obj)
|
|
1277 "Strip CSO control information from the current buffer."
|
|
1278
|
|
1279 (let ((req "")
|
|
1280 (buffer-read-only nil)
|
|
1281 beg nreq)
|
|
1282 (goto-char (point-min))
|
|
1283 (insert "\n")
|
|
1284 (while (not (eobp))
|
|
1285 (cond ((and (>= (following-char) ?3) (<= (following-char) ?9))
|
|
1286 (delete-char 4)
|
|
1287 (insert (concat (gopher-object-selector obj) "\n")))
|
|
1288
|
|
1289 ((eq (following-char) ?-)
|
|
1290 (delete-char 5)
|
|
1291 (setq beg (point))
|
|
1292 (skip-chars-forward "^:")
|
|
1293 (setq nreq (buffer-substring beg (point)))
|
|
1294 (goto-char beg)
|
|
1295 (or (string= req nreq)
|
|
1296 (insert (concat "--------------------------"
|
|
1297 "-----------------------------\n")))
|
|
1298 (setq req nreq)
|
|
1299 (setq beg (point))
|
|
1300 (skip-chars-forward "^:")
|
|
1301 (forward-char)
|
|
1302 (delete-region beg (point)))
|
|
1303
|
|
1304 (t
|
|
1305 (setq beg (point))
|
|
1306 (forward-line 1)
|
|
1307 (delete-region beg (point))
|
|
1308 (forward-line -1))
|
|
1309 )
|
|
1310 (forward-line 1))
|
|
1311
|
|
1312 (goto-char (point-min))
|
|
1313 (delete-char 1)))
|
|
1314
|
|
1315
|
|
1316 ;;;;--------------------------------------------------------------------------
|
|
1317 ;;;; indices.
|
|
1318 ;;;;
|
|
1319 ;;;; To query an index, the search string is appended to the selector.
|
|
1320 ;;;; The index returns a gopher directory.
|
|
1321 ;;;;
|
|
1322
|
|
1323
|
|
1324 (defun gopher-index-object (obj oldbuf)
|
|
1325 "Query a gopher directory object."
|
|
1326
|
|
1327 ;; Get the search string from the user.
|
|
1328 (let ((str (read-from-minibuffer "Key: "))
|
|
1329 (newobj (copy-sequence obj)))
|
|
1330
|
|
1331 ;; Append it to the selector and retrieve the modified object
|
|
1332 ;; like a directory.
|
|
1333 (setq str (gopher-trim-blanks str))
|
|
1334 (if (> (length str) 0)
|
|
1335 (progn
|
|
1336 (gopher-set-object-selector newobj
|
|
1337 (concat (gopher-object-selector obj) "\t"
|
|
1338 str))
|
|
1339 (gopher-directory-object newobj (current-buffer)))
|
|
1340 )))
|
|
1341
|
|
1342
|
|
1343
|
|
1344 ;;;;--------------------------------------------------------------------------
|
|
1345 ;;;; telneting.
|
|
1346 ;;;;
|
|
1347
|
|
1348 (defun gopher-telnet-object (obj oldbuf)
|
|
1349 "Start a telnet session to a gopher object.
|
108
|
1350 If gopher-telnet-command is non-nil, then that is a command to start
|
0
|
1351 a telnet session in a subprocess. Otherwise, the emacs-lisp telnet
|
|
1352 package is used."
|
|
1353
|
|
1354 ;; make the telnet argument string
|
|
1355 (let ((arg (gopher-object-host obj))
|
|
1356 (port (gopher-object-port obj)))
|
|
1357 (if (not (zerop port))
|
|
1358 (setq arg (concat arg
|
|
1359 (if (eq system-type 'vax-vms)
|
|
1360 "/port="
|
|
1361 " ")
|
|
1362 port)))
|
|
1363
|
|
1364 (if gopher-telnet-command
|
|
1365
|
|
1366 ;; start up telnet as a separate process
|
|
1367 (save-window-excursion
|
|
1368 (gopher-background
|
|
1369 (concat gopher-telnet-command " " arg)))
|
|
1370
|
|
1371 ;; use telnet-mode
|
|
1372 (telnet arg)
|
|
1373 ;; set things up so we can get back to the last node.
|
|
1374 (gopher-make-local-vars
|
|
1375 'gopher-obj obj
|
|
1376 'gopher-last oldbuf
|
|
1377 'gopher-telnet-process-name (concat arg "-telnet"))
|
|
1378 (local-set-key "\C-cl" 'gopher-telnet-quit)
|
|
1379 (local-set-key "\C-xk" 'gopher-telnet-quit)
|
|
1380 )
|
|
1381
|
|
1382 ;; show the login info to the user
|
|
1383 (if (not (zerop (length (gopher-object-selector obj))))
|
|
1384 (progn
|
|
1385 (beep)
|
|
1386 (message (concat
|
|
1387 "Login as: "
|
|
1388 (gopher-object-selector obj)
|
|
1389 ))
|
|
1390 ))
|
|
1391 ))
|
|
1392
|
|
1393
|
|
1394 (defun gopher-telnet-quit nil
|
|
1395 "Clean up a telnet session and return to the previous gopher node."
|
|
1396 (interactive)
|
|
1397 (condition-case nil
|
|
1398 (delete-process gopher-telnet-process-name)
|
|
1399 (error t))
|
|
1400 (gopher-last-node))
|
|
1401
|
|
1402
|
|
1403
|
|
1404 ;;;;--------------------------------------------------------------------------
|
|
1405 ;;;; Images/sounds.
|
|
1406 ;;;;
|
|
1407
|
|
1408 (defun gopher-image-object (obj oldbuf)
|
|
1409 "Retrieve what we hope is an image and show it."
|
|
1410 (let (
|
|
1411 (showit (y-or-n-p "Display this item? "))
|
|
1412 (fname)
|
|
1413 (buf (gopher-get-doc-buf (gopher-object-descr obj))))
|
|
1414 (if showit
|
|
1415 (setq fname (make-temp-name "/tmp/gopherimg"))
|
|
1416 (setq fname(read-file-name "File to save in: ")))
|
|
1417 (gopher-retrieve-document-cleanly (buf
|
|
1418 (gopher-object-selector obj)
|
|
1419 (gopher-object-host obj)
|
|
1420 (gopher-object-port obj)
|
|
1421 'none)
|
|
1422 (progn
|
|
1423 (error "Problems retrieving object.")
|
|
1424 (kill-buffer buf))
|
|
1425
|
|
1426 (save-excursion
|
|
1427 (set-buffer buf)
|
|
1428 (write-file fname))
|
|
1429 (kill-buffer buf)
|
|
1430 (if (and showit gopher-image-display-command)
|
|
1431
|
|
1432 ;; Spawn a process to display the image.
|
|
1433 ;; But modify its sentinel so that the file we wrote
|
|
1434 ;; will get deleted when the process exits.
|
|
1435 (save-window-excursion
|
|
1436 (let ((p (gopher-background
|
|
1437 (concat gopher-image-display-command " " fname))))
|
|
1438 (set-process-sentinel p
|
|
1439 (` (lambda (process msg)
|
|
1440 ((, (process-sentinel p)) process msg)
|
|
1441 (if (not (eq (process-status process) 'run))
|
|
1442 (delete-file (, fname)))
|
|
1443 )))
|
|
1444 ))
|
|
1445 ))))
|
|
1446
|
|
1447
|
|
1448
|
|
1449 ;;;;--------------------------------------------------------------------------
|
|
1450 ;;;; Various opaque objects. Just save them in a file for now.
|
|
1451 ;;;;
|
|
1452
|
|
1453 (defun gopher-binary-object (obj oldbuf)
|
|
1454 "Retrieve a gopher object and save it to a file,
|
|
1455 without trying to interpret it in any way."
|
|
1456 (let ((fname (read-file-name "File to save in: "))
|
|
1457 (buf (gopher-get-doc-buf (gopher-object-descr obj))))
|
|
1458
|
|
1459 (gopher-retrieve-document-cleanly (buf
|
|
1460 (gopher-object-selector obj)
|
|
1461 (gopher-object-host obj)
|
|
1462 (gopher-object-port obj)
|
|
1463 'none)
|
|
1464
|
|
1465 (progn
|
|
1466 (error "Problems retrieving object.")
|
|
1467 (kill-buffer buf))
|
|
1468
|
|
1469 (save-excursion
|
|
1470 (set-buffer buf)
|
|
1471 (write-file fname))
|
|
1472 (kill-buffer buf)
|
|
1473 )))
|
|
1474
|
|
1475
|
|
1476 ;;;;--------------------------------------------------------------------------
|
|
1477 ;;;; forms stuff
|
|
1478 ;;;;
|
|
1479 ;;;; Uses some of the internal routines from forms.el to present
|
|
1480 ;;;; a form which is not associated with a file.
|
|
1481 ;;;;
|
|
1482
|
|
1483 (defun gopher-form (form-name accept-action number-of-fields format-list)
|
|
1484 "Display a buffer containing a form for the user to enter data.
|
|
1485 The form is described by NUMBER-OF-FIELDS and FORMAT-LIST (cf. forms-mode).
|
|
1486 FORM-NAME is a string to put in the modeline.
|
|
1487 When the user accepts the data in the form by pressing `C-c RET', the
|
|
1488 function ACCEPT-ACTION is called with a list of the strings which
|
|
1489 the user entered."
|
|
1490
|
|
1491 (switch-to-buffer (generate-new-buffer "*gopher form*"))
|
|
1492
|
|
1493 (gopher-make-local-vars
|
|
1494 'forms-format-list format-list
|
|
1495 'forms-number-of-fields number-of-fields
|
|
1496 'forms-field-sep "\t"
|
|
1497 'forms-read-only nil
|
|
1498 'forms-multi-line nil
|
|
1499 'forms--number-of-markers nil
|
|
1500 'forms--markers nil
|
|
1501 'forms--format nil
|
|
1502 'forms--parser nil
|
|
1503 'forms--dynamic-text nil
|
|
1504 'forms-fields nil
|
|
1505 'forms-the-record-list nil
|
|
1506 'forms-accept-action accept-action
|
|
1507 )
|
|
1508
|
|
1509 (forms--process-format-list)
|
|
1510 (forms--make-format)
|
|
1511 (forms--make-parser)
|
|
1512
|
|
1513 (erase-buffer)
|
|
1514
|
|
1515 ;; make local variables
|
|
1516 (make-local-variable 'forms--file-buffer)
|
|
1517 (make-local-variable 'forms--total-records)
|
|
1518 (make-local-variable 'forms--current-record)
|
|
1519 (make-local-variable 'forms--the-record-list)
|
|
1520 (make-local-variable 'forms--search-rexexp)
|
|
1521
|
|
1522 ;; set the major mode indicator
|
|
1523 (setq major-mode 'gopher-form-mode)
|
|
1524 (setq mode-name "gopher form")
|
|
1525
|
|
1526 (set-buffer-modified-p nil)
|
|
1527
|
|
1528 (use-local-map gopher-form-mode-map)
|
|
1529
|
|
1530 (forms--show-record (make-string (1- number-of-fields) ?\t))
|
|
1531
|
|
1532 (run-hooks 'gopher-form-mode-hooks))
|
|
1533
|
|
1534
|
|
1535 (defun gopher-form-accept nil
|
|
1536 (interactive)
|
|
1537
|
|
1538 (funcall forms-accept-action (forms--parse-form)))
|
|
1539
|
|
1540 (define-key gopher-form-mode-map "\C-c\r" 'gopher-form-accept)
|
|
1541 (define-key gopher-form-mode-map "\C-cl" 'gopher-last-node)
|
|
1542
|
|
1543
|
|
1544 ;;;;--------------------------------------------------------------------------
|
|
1545 ;;;; low-level communications routines
|
|
1546 ;;;;
|
|
1547
|
|
1548
|
|
1549 (defun gopher-retrieve-document (buf sel host port &optional end-regexp)
|
|
1550 "Retrieve a gopher object into BUF.
|
|
1551 The object is identified by a SEL HOST PORT triple.
|
|
1552 Optional argument END-REGEXP is used for data which is not `.'-terminated.
|
|
1553 If END-REGEXP is non-nil and not a string, then it is assumed that
|
|
1554 the data is binary, and reading will continue until the sender disconnects.
|
|
1555 Returns NIL if an error occured during the attempt to retrieve the
|
|
1556 document, otherwise T.
|
|
1557 "
|
|
1558
|
|
1559 ;; Default is single period termination.
|
|
1560 (or end-regexp (setq end-regexp "^\\.\r$"))
|
|
1561
|
|
1562 (save-excursion
|
|
1563 (set-buffer buf)
|
|
1564 (erase-buffer)
|
|
1565
|
|
1566 (let ((h (assoc host gopher-hostname-aliases)))
|
|
1567 (if h (setq host (cdr h))))
|
|
1568
|
|
1569 ;; Open the connection to the server.
|
|
1570 ;; If we get an unknown service error, try looking the port up in
|
|
1571 ;; gopher-port-aliases. If we find it there, try the connect again
|
|
1572 ;; with that translation.
|
|
1573 (let (wait
|
|
1574 (gopher-server-process
|
|
1575 (let (p (try-again t))
|
|
1576 (while try-again
|
|
1577 (setq try-again nil)
|
|
1578 (condition-case errinfo
|
|
1579 (setq p (open-network-stream "gopher" (current-buffer)
|
|
1580 host port))
|
|
1581 (error (if (and (string-match "^Unknown service .*$"
|
|
1582 (nth 1 errinfo))
|
|
1583 (setq port (cdr (assoc port
|
|
1584 gopher-port-aliases))))
|
|
1585 (setq try-again t)
|
|
1586 (ding)
|
70
|
1587 (message "%s: %s" (nth 0 errinfo) (nth 1 errinfo))
|
0
|
1588 ))))
|
|
1589 p)))
|
|
1590
|
|
1591 (cond (gopher-server-process
|
|
1592
|
|
1593 ;; keep the emacs end-of-process status line out of the buffer
|
|
1594 (set-process-sentinel gopher-server-process 'gopher-sentinel)
|
|
1595
|
|
1596 ;; send the selector to the server
|
|
1597 (process-send-string gopher-server-process (concat sel "\r\n"))
|
|
1598
|
|
1599 ;; receive the response from the server
|
|
1600 ;; based on nntp.el from GNUS
|
|
1601 (setq wait t)
|
|
1602 (while wait
|
|
1603 (if (stringp end-regexp)
|
|
1604 (progn
|
|
1605 (goto-char (point-max))
|
|
1606 (forward-line -1)))
|
|
1607 (if (and (stringp end-regexp)
|
|
1608 (looking-at end-regexp))
|
|
1609 (setq wait nil)
|
|
1610 (if (not (memq (process-status gopher-server-process)
|
|
1611 '(open run)))
|
|
1612 (progn
|
|
1613 (message "gopher: connection closed")
|
|
1614 (setq wait nil))
|
|
1615 (if gopher-debug-read
|
|
1616 (message "gopher: Reading..."))
|
|
1617 (cond (gopher-buggy-accept
|
|
1618 (sit-for 1))
|
|
1619 ((and (boundp 'epoch::version) epoch::version)
|
|
1620 (accept-process-output gopher-server-process 2))
|
|
1621 (t
|
|
1622 (accept-process-output gopher-server-process))
|
|
1623 )
|
|
1624 (if gopher-debug-read
|
|
1625 (message " ")))
|
|
1626 ))
|
|
1627
|
|
1628 ;; be sure the net connection has gone away...
|
|
1629 (condition-case nil
|
|
1630 (delete-process gopher-server-process)
|
|
1631 (error t))
|
|
1632
|
|
1633 ;; clean up the text buffer
|
|
1634 (if (stringp end-regexp)
|
|
1635 (gopher-clean-text))
|
|
1636
|
|
1637 t)
|
|
1638
|
|
1639 (t nil))
|
|
1640 )))
|
|
1641
|
|
1642
|
|
1643 ;;; adapted from GNUS
|
|
1644 (defun gopher-clean-text ()
|
|
1645 "Decode text transmitted by gopher.
|
|
1646 0. Delete status line.
|
|
1647 1. Delete `^M' at end of line.
|
|
1648 2. Delete `.' at end of buffer (end of text mark).
|
|
1649 3. Delete `.' at beginning of line. (does gopher want this?)"
|
|
1650
|
|
1651 ;; Insert newline at end of buffer.
|
|
1652 (goto-char (point-max))
|
|
1653 (if (not (bolp))
|
|
1654 (insert "\n"))
|
|
1655 ;; Delete `^M' at end of line.
|
|
1656 (goto-char (point-min))
|
|
1657 (while (re-search-forward "\r[^\n]*$" nil t)
|
|
1658 (replace-match ""))
|
|
1659 ; (goto-char (point-min))
|
|
1660 ; (while (not (eobp))
|
|
1661 ; (end-of-line)
|
|
1662 ; (if (= (preceding-char) ?\r)
|
|
1663 ; (delete-char -1))
|
|
1664 ; (forward-line 1)
|
|
1665 ; )
|
|
1666 ;; Delete `.' at end of buffer (end of text mark).
|
|
1667 (goto-char (point-max))
|
|
1668 (forward-line -1) ;(beginning-of-line)
|
|
1669 (while (looking-at "^\\.$")
|
|
1670 (delete-region (point) (progn (forward-line 1) (point)))
|
|
1671 (forward-line -1))
|
|
1672 ;; Replace `..' at beginning of line with `.'.
|
|
1673 (goto-char (point-min))
|
|
1674 ;; (replace-regexp "^\\.\\." ".")
|
|
1675 (while (search-forward "\n.." nil t)
|
|
1676 (delete-char -1))
|
|
1677 )
|
|
1678
|
|
1679
|
|
1680 (defun gopher-sentinel (proc status)
|
|
1681 nil)
|
|
1682
|
|
1683 (provide 'gopher)
|
|
1684
|
|
1685 ;;; gopher.el ends here
|
|
1686
|
|
1687 ;;;(gopher.el) Local Variables:
|
|
1688 ;;;(gopher.el) eval: (put 'gopher-retrieve-document-cleanly 'lisp-indent-hook 2)
|
|
1689 ;;;(gopher.el) End:
|
|
1690
|