home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 033 / atcp40de.zip / GOPHER.EL < prev    next >
Lisp/Scheme  |  1994-10-07  |  54KB  |  1,625 lines

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