ã¿ã¤ã ã¢ã¦ãã¤ã url-retrieve-synchronously
SKK ã® ML ã«æ¸ããå 容ãããã°ã«ãã¡ã¢ãã¨ãã¾ãã
url-retrieve-synchronously ã¯åæçã« HTTP ã§æå®ãã URL ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãããç¸æãµã¤ããéãã¨åå¿ãä¸ã è¿ã£ã¦ãããEmacs ãåºã¾ã£ã¦ãã¾ãã¾ãã
ãã®åé¡ã解決ããããã«ãã¿ã¤ã ã¢ã¦ãã¤ãã® url-retrieve-synchronously ãä½ã£ã¦ã¿ã¾ããã
åç´ã« run-with-timer 㧠url-retrieve ã®ããã»ã¹ãç£è¦ããã¿ã¤ããä½ããã¿ã¤ããå±èµ·ããã㨠url-retrieve ã®ããã»ã¹ã殺ãããä»çµã¿ã§ãã
ã¿ã¤ãã¦ãã¯ãããã©ã«ãã§2ç§ã§çºçãã¾ãã
(require 'url-http) (defun my-url-retrieve-with-timeout (url coding-system &optional timeout-interval) (let ((url-max-redirections 0) ; URL ãªãã¤ã¬ã¯ããæå¶ãã ;; `url-retrieve' ã®ããã®ã³ã¼ã«ããã¯é¢æ° (url-callback #'(lambda (status coding) (let (p) (setq done t) (when (setq p (url-http-symbol-value-in-buffer 'url-http-end-of-headers (current-buffer))) (setq jsonp (decode-coding-string (buffer-substring (1+ p) (point-max)) coding)))))) ;; ã¿ã¤ã ã¢ã¦ãçºçæã«å¼ã°ããã³ã¼ã«ããã¯é¢æ° (timeout-callback #'(lambda () ;; `url-retrieve' ã®ããã»ã¹ãæ¢ãã (when (processp proc) (delete-process proc)) ;; `url-retrieve' ã¯çµãã£ããã®ã¨è¦åã (unless done (setq done t)))) ;; ã¿ã¤ã ã¢ã¦ãæé (timeout-interval (if timeout-interval timeout-interval 2)) (done nil) buf proc jsonp timeout) (unwind-protect ;; condition-case ãç¨ãã¦å ¨ã¦ã®ã¨ã©ã¼ãææãããããã§ãææãã ;; ã¨ã©ã¼ã¯ç¡è¦ãããéçºããããã°æã«ã¯ condition-case ã®ããã㯠;; ãå¤ãã (condition-case e (when (setq buf (url-retrieve url url-callback (list coding-system))) (setq proc (get-buffer-process buf) ;; ã¿ã¤ã ã¢ã¦ãç£è¦ç¨ã® timer ãè¨å®ãã timeout (run-with-timer timeout-interval timeout-interval timeout-callback)) ;; `url-retrieve' ã¯éåæãªã®ã§ãåæçã«çµæãå¾ãããã ;; ãã«å¾ ã¡åããããããã§ã¯ã ;; `url-retrieve-synchronously' ãè¡ã£ã¦ãããã¨ã¨ã»ã¼åç ;; ã®ãã¨ããã¦ãããæ¬é¢æ°ã§ã¯ãURL ãªãã¤ã¯ã¬ãã¯ããªã ;; (ã¯ã)ãªã®ã§ãURL ãªãã¤ã¬ã¯ãã«å¯¾ãã対çã®é¨åã®ã¿åé¤ ;; ãã¦ããã (while (null done) (when (and proc (memq (process-status proc) '(closed exit signal failed)) (eq proc (or (get-buffer-process buf) proc))) (delete-process proc) (setq done t)) (unless (or (with-local-quit (accept-process-output proc)) (null proc)) (when quit-flag (delete-process proc)) (setq proc (and (not quit-flag) (get-buffer-process buf)))))) (error ;; å ¨ã¦ã®ã¨ã©ã¼ã¯ç¡è¦ãã nil)) (when (bufferp buf) (kill-buffer buf)) ;; ã¿ã¤ã ã¢ã¦ãç¨ã® timer ãåé¤ãã (when timeout (cancel-timer timeout) ;; ä¸å¿ãåæåããã¦ãã (setq timeout nil))) jsonp))
å©ç¨ä¾ã¨ãã¦ã¯ãsocial ime ç¸æã«ä½¿ãã¨ä»¥ä¸ã®ãããªæãã§ãã
ããã2ç§ä»¥å
ã« social ime ããå¿çãç¡ããã°ãmy-url-retrieve-with-timeout 㯠nil ãè¿ãã¾ãã
(let ((url (concat "http://www.social-ime.com/api/" "?string=" (url-hexify-string (encode-coding-string "ã" 'utf-8)))) res) (when (setq res (my-url-retrieve-with-timeout url 'euc-jp)) (split-string (substring res 0 (1- (length res))) "\t" t))) ;; => ("äº" "å" "å¨" "é¿" "è" "å¾" "äº" "å" "å " "å©" "æ¤" "ç¾" "é" "é" "é¼" "é´" "ã" "ã¢")
timeout ã®ééãçãããå ´åãmy-url-retrieve-with-timeout ã®ç¬¬3å¼æ°ã«æ°å¤ãæå®ãã¾ãã
å°æ°ã§ããªãã±ã¼ã§ãã
(my-url-retrieve-with-timeout url 'euc-jp 0.5)
ãã£ã¨ä¸æãããæ¹ãããã ãããªã¼ã¨ãæããªããããããªãããªã§ã
æ´æ°æå»
- 2011/11/12 23:20