Commits

Anonymous committed b474ca9

- replaced list representation by symbol properties
- access/set functions are now macros

  • Participants
  • Parent commits b2ed721

Comments (0)

Files changed (1)

File connection.el

 
 (defun connection-create-data (buffer process point)
   "Create a new connection data based on `buffer', `process', and `point'."
-  (list point buffer process))
+  (let ((connection (make-symbol "connection")))
+    (put connection 'connection t)
+    (connection-set-read-point connection point)
+    (connection-set-process connection process)
+    (connection-set-buffer connection buffer)
+    connection))
 
-(defun connection-data-buffer (connection-data)
-  "Return the buffer associated with the connection."
-  (nth 1 connection-data))
+(defmacro connection-p (connection)
+  "Returns non-nil if `connection' is a connection object"
+  (list 'get connection ''connection))
 
-(defun connection-data-process (connection-data)
-  "Return the networking process associated with the connection."
-  (nth 2 connection-data))
+(defmacro connection-read-point (connection)
+  "Return the read point of the connection object."
+  (list 'get connection ''connection-read-point))
 
-(defun connection-data-point (connection-data)
-  "Return the read-point associated with the connection."
-  (car connection-data))
+(defmacro connection-process (connection)
+  "Return the process of the connection object."
+  (list 'get connection ''connection-process))
 
-(defun connection-data-set-buffer (connection-data buffer)
-  "Set the buffer of `connection-data' to `buffer'."
-  (setcar (cdr connection-data) buffer))
+(defmacro connection-buffer (connection)
+  "Return the buffer of the connection object."
+  (list 'get connection ''connection-buffer))
 
-(defun connection-data-set-process (connection-data process)
-  "Set the process of `connection-data' to `process'."
-  (setcar (cdr (cdr connection-data)) process))
+(defmacro connection-set-read-point (connection point)
+  "Set the read-point for `connection' to `point'."
+  (list 'put connection ''connection-read-point point))
 
-(defun connection-data-set-point (connection-data point)
-  "Set the read-point of `connection-data' to `point'."
-  (setcar connection-data point))
+(defmacro connection-set-process (connection process)
+  "Set the process for `connection' to `process'."
+  (list 'put connection ''connection-process process))
+
+(defmacro connection-set-buffer (connection buffer)
+  "Set the buffer for `connection' to `buffer'."
+  (list 'put connection ''connection-buffer buffer))
+
 
 (defun connection-open (server port)
   "Open a connection to `server' and `port'.
 A data structure identifing the connection is returned"
 
-  (let ((process-buffer (generate-new-buffer (format " connection to %s:%d"
+  (let ((process-buffer (generate-new-buffer (format " connection to %s:%s"
 						     server
 						     port)))
 	(process))
 					 server port))
       (connection-create-data process-buffer process (point-min)))))
 
-(defun connection-status (connection-data)
+(defun connection-status (connection)
   "Return the status of the connection.
 Possible return values are the symbols:
-'none: argument is no connection
+nil: argument is no connection object
+'none: argument has no connection
 'up: connection is open and buffer is existing
 'down: connection is closed
 'alone: connection is not associated with a buffer"
-  (let ((process (connection-data-process connection-data))
-	(buffer (connection-data-buffer connection-data)))
-    (if (not process)
-	'none
-      (if (not (buffer-live-p buffer))
-	  'alone
-	(if (not (eq (process-status process) 'open))
-	    'down
-	  'up)))))
+  (if (connection-p connection)
+      (let ((process (connection-process connection))
+	    (buffer (connection-buffer connection)))
+	(if (not process)
+	    'none
+	  (if (not (buffer-live-p buffer))
+	      'alone
+	    (if (not (eq (process-status process) 'open))
+		'down
+	      'up))))
+    nil))
 
-(defun connection-close (connection-data)
+(defun connection-close (connection)
   "Force closing of the connection."
-  (if connection-data
+  (if (connection-p connection)
       (progn
-	(let ((buffer (connection-data-buffer connection-data))
-	      (process (connection-data-process connection-data)))
+	(let ((buffer (connection-buffer connection))
+	      (process (connection-process connection)))
 	  (if process
 	      (delete-process process))
 	  (if buffer
 	      (kill-buffer buffer))
 	  
-	  (connection-data-set-process connection-data nil)
-	  (connection-data-set-buffer connection-data nil)))))
+	  (connection-set-process connection nil)
+	  (connection-set-buffer connection nil)))))
 
 (defun connection-send (connection data)
   "Send `data' to the process."
+  (unless (eq (connection-status connection) 'up)
+    (error "Connection is not up"))
   (save-excursion
-    (set-buffer (connection-data-buffer connection))
+    (set-buffer (connection-buffer connection))
     (goto-char (point-max))
-    (connection-data-set-point connection (point))
-    (process-send-string (connection-data-process connection) data)))
+    (connection-set-read-point connection (point))
+    (process-send-string (connection-process connection) data)))
 
 (defun connection-send-crlf (connection data)
   "Send `data' together with CRLF to the process."
 
 (defun connection-read (connection delimiter)
   "Read data until `delimiter' is found inside the buffer."
+  (unless (eq (connection-status connection) 'up)
+    (error "Connection is not up"))
   (let ((case-fold-search nil)
 	match-end)
     (save-excursion
-      (set-buffer (connection-data-buffer connection))
-      (goto-char (connection-data-point connection))
+      (set-buffer (connection-buffer connection))
+      (goto-char (connection-read-point connection))
       ;; Wait until there is enough data 
       (while (not (search-forward delimiter nil t))
-	(accept-process-output (connection-data-process connection) 3)
-	(goto-char (connection-data-point connection)))
+	(accept-process-output (connection-process connection) 3)
+	(goto-char (connection-read-point connection)))
       (setq match-end (point))
       ;; Return the result
-      (let ((result (buffer-substring (connection-data-point connection)
+      (let ((result (buffer-substring (connection-read-point connection)
 				      match-end)))
-	(connection-data-set-point connection match-end)
+	(connection-set-read-point connection match-end)
 	result))))
 
 (defun connection-read-crlf (connection)
   "Read until a line is completed with CRLF"
   (connection-read connection "\r\n"))
 
-(defun connection-read-point (connection)
+(defun connection-read-to-point (connection)
   "Read until a line is consisting of a single point"
   (connection-read connection "\r\n.\r\n"))