Anonymous avatar Anonymous committed fd85291

optimized reading reply and splitting it

Comments (0)

Files changed (1)

-;;; dictionary.el -- an interface to RFC 2229 dictionary server
+;; dictionary.el -- an interface to RFC 2229 dictionary server
 
 ;; Author: Torsten Hilbrich <Torsten.Hilbrich@gmx.net>
 ;; Keywords: interface, dictionary
     (setq dictionary-window-configuration window-configuration)
     (dictionary-mode)
     (condition-case message
-	(dictionary-check-connection server port)
+    (dictionary-check-connection server port)
       (error (progn
 	       (dictionary-close)
-	       (error "Unable to open connection to %s:%d - %s"
+	       (error "Unable to open connection to %s:%s - %s"
 		      server port (nth 1 message)))))))
 
 (unless dictionary-mode-map
 	      string nil))))
     (nreverse list)))
 
-(defun dictionary-read-reply-list ()
+(defun dictionary-read-reply-and-split ()
   "Read the reply, split it into words and return it"
-  (dictionary-split-string (dictionary-read-reply)))
+  (let ((answer (make-symbol "reply-data"))
+	(reply (dictionary-read-reply)))
+    (let ((reply-list (dictionary-split-string reply)))
+      (put answer 'reply reply)
+      (put answer 'reply-list reply-list)
+      (put answer 'reply-code (string-to-number (car reply-list)))
+      answer)))
+
+(defmacro dictionary-reply-code (reply)
+  "Return the reply code stored in `reply'."
+  (list 'get reply ''reply-code))
+
+(defmacro dictionary-reply (reply)
+  "Return the string reply stored in `reply'."
+  (list 'get reply ''reply))
+
+(defmacro dictionary-reply-list (reply)
+  "Return the reply list stored in `reply'."
+  (list 'get reply ''reply-list))
 
 (defun dictionary-read-answer ()
   "Read an answer delimited by a . on a single line"
-  (let ((answer (connection-read-point dictionary-connection))
+  (let ((answer (connection-read-to-point dictionary-connection))
 	(start 0))
     (while (string-match "^\\." answer start)
       (setq answer (replace-match "" t t answer))
       (setq start (1- (match-end 0))))
     answer))
 
-(defun dictionary-check-reply (string code)
-  "Check if the reply in `string' has the `code'."
-  (let* ((number (if (numberp code)
-		    code
-		  (string-to-number code)))
-	 (list (dictionary-split-string string))
-	 (reply-code (string-to-number (car list))))
-    (= number reply-code)))
-    
+(defun dictionary-check-reply (reply code)
+  "Check if the reply in `reply' has the `code'."
+  (let ((number (dictionary-reply-code reply)))
+    (and (numberp number)
+	 (= number code))))
     
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Communication functions
 
 (defun dictionary-check-initial-reply ()
   "Read the first reply from server and check it."
-  (let ((string (dictionary-read-reply)))
-    (unless (dictionary-check-reply string 220)
+  (let ((reply (dictionary-read-reply-and-split)))
+    (unless (dictionary-check-reply reply 220)
       (connection-close dictionary-connection)
-      (error "Server returned: %s" string))))
+      (error "Server returned: %s" (dictionary-reply reply)))))
 
 ;; Store the current state
 (defun dictionary-store-state (word dictionary kind pos window)
 
   (dictionary-send-command (concat "define \"" dictionary "\" \""
 				   word "\""))
-  (let ((reply (dictionary-read-reply)))
+  (let ((reply (dictionary-read-reply-and-split)))
     (if (dictionary-check-reply reply 552)
 	(error "Word \"%s\" in dictionary \"%s\" not found"
 	       word dictionary)
 (defun dictionary-display-search-result (reply)
   "This function starts displaying the result starting with the `reply'."
   
-  (let* ((reply-list (dictionary-split-string reply))
-	 (number (nth 1 reply-list)))
+  (let* ((number (nth 1 (dictionary-reply-list reply))))
     (toggle-read-only 0)
     (erase-buffer)
     (insert number (if (equal number "1")
 		       " definition"
 		     " definitions")
 	    " found\n\n")
-    (setq reply (dictionary-read-reply))
+    (setq reply (dictionary-read-reply-and-split))
     (while (dictionary-check-reply reply 151)
-      (let* ((reply-list (dictionary-split-string reply))
+      (let* ((reply-list (dictionary-reply-list reply))
 	     (database (nth 2 reply-list))
 	     (description (nth 3 reply-list))
 	     (word (nth 1 reply-list)))
 	(dictionary-display-word-entry word database description)
 	(setq reply (dictionary-read-answer))
 	(dictionary-display-word-definition reply word database)
-	(setq reply (dictionary-read-reply))))
+	(setq reply (dictionary-read-reply-and-split))))
     (goto-char (point-min))
     (set-buffer-modified-p nil)
     (toggle-read-only 1)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.