From 8f0e3539c0cb86986365f21021a21ad03b2908cc Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:16:01 +0800 Subject: Remove unused local binding --- wttrin.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wttrin.el b/wttrin.el index 199ade6..c8b6847 100644 --- a/wttrin.el +++ b/wttrin.el @@ -38,7 +38,7 @@ (defun wttrin-exec () "Display weather information." (interactive) - (let* ((title) (name (generate-new-buffer-name (concat "wttr.in - " wttrin-query))) + (let* ((name (generate-new-buffer-name (concat "wttr.in - " wttrin-query))) (buf (get-buffer-create name))) (switch-to-buffer buf)) (insert (xterm-color-filter (wttrin-fetch wttrin-query))) -- cgit v1.2.3 From 06f7449302f60d4a35ff4c2dd16f70e1beda8472 Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:19:26 +0800 Subject: Prevent creating duplicated buffers --- wttrin.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wttrin.el b/wttrin.el index c8b6847..564eb81 100644 --- a/wttrin.el +++ b/wttrin.el @@ -38,8 +38,7 @@ (defun wttrin-exec () "Display weather information." (interactive) - (let* ((name (generate-new-buffer-name (concat "wttr.in - " wttrin-query))) - (buf (get-buffer-create name))) + (let ((buf (get-buffer-create (format "*wttr.in - %s*" wttrin-query)))) (switch-to-buffer buf)) (insert (xterm-color-filter (wttrin-fetch wttrin-query))) (goto-char (point-min)) -- cgit v1.2.3 From 00d2e385a5c939bc4fe673a9bb6de5d8b62f7354 Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:21:22 +0800 Subject: Rename function --- README.md | 4 ++-- wttrin.el | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 65c0616..b0c1859 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Set variable `wttrin-query`: (defvar wttrin-query "Taipei") ``` -Run `M-x wttrin-exec` to get the information: +Run `M-x wttrin` to get the information: ![screenshot] @@ -22,4 +22,4 @@ Run `M-x wttrin-exec` to get the information: MIT [wttr.in]: http://wttr.in/ -[screenshot]: wttrin.png \ No newline at end of file +[screenshot]: wttrin.png diff --git a/wttrin.el b/wttrin.el index 564eb81..87c2471 100644 --- a/wttrin.el +++ b/wttrin.el @@ -35,7 +35,7 @@ (decode-coding-string (buffer-string) 'utf-8)))) -(defun wttrin-exec () +(defun wttrin () "Display weather information." (interactive) (let ((buf (get-buffer-create (format "*wttr.in - %s*" wttrin-query)))) -- cgit v1.2.3 From 30e8abcbd629969bbd20e7159a62c0fcb6f8e11b Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:36:44 +0800 Subject: More universal C-u M-x wttrin --- wttrin.el | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/wttrin.el b/wttrin.el index 87c2471..460e991 100644 --- a/wttrin.el +++ b/wttrin.el @@ -20,31 +20,36 @@ :prefix "wttrin-" :group 'comm) -(defcustom wttrin-query "Taipei" +(defcustom wttring-default-city "Taipei" "Specify a query condition to get the weather information." :group 'wttrin :type 'string) -(defun wttrin-fetch (query) +(defun wttrin-fetch-raw-string (query) "Get the weather information based on your QUERY." (let ((url-request-extra-headers '(("User-Agent" . "curl")))) (with-current-buffer - (url-retrieve-synchronously - (concat "http://wttr.in/" query) - (lambda (status) (switch-to-buffer (current-buffer)))) + (url-retrieve-synchronously + (concat "http://wttr.in/" query) + (lambda (status) (switch-to-buffer (current-buffer)))) (decode-coding-string (buffer-string) 'utf-8)))) -(defun wttrin () - "Display weather information." +(defun wttrin (&optional force-ask) + "Display weather information. +Add C-u prefix to force to ask city name." (interactive) - (let ((buf (get-buffer-create (format "*wttr.in - %s*" wttrin-query)))) - (switch-to-buffer buf)) - (insert (xterm-color-filter (wttrin-fetch wttrin-query))) - (goto-char (point-min)) - (re-search-forward "^$") - (delete-region (point-min) (1+ (point))) - (setq buffer-read-only t)) + (let* ((ask? (or current-prefix-arg force-ask)) + (city-name (if ask? + (read-from-minibuffer "City name: ") + (or wttring-default-city (read-from-minibuffer "City name: ")))) + (buf (get-buffer-create (format "*wttr.in - %s*" city-name)))) + (switch-to-buffer buf) + (insert (xterm-color-filter (wttrin-fetch-raw-string city-name))) + (goto-char (point-min)) + (re-search-forward "^$") + (delete-region (point-min) (1+ (point))) + (setq buffer-read-only t))) (provide 'wttrin) -- cgit v1.2.3 From 9f92cd40632a39e1b398531fd879e87dc26dd339 Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:40:06 +0800 Subject: Refactoring to more universal functions --- wttrin.el | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/wttrin.el b/wttrin.el index 460e991..c370a07 100644 --- a/wttrin.el +++ b/wttrin.el @@ -34,6 +34,16 @@ (lambda (status) (switch-to-buffer (current-buffer)))) (decode-coding-string (buffer-string) 'utf-8)))) +(defun wttrin-query (city-name) + "Query weather of CITY-NAME via wttrin, and display the result +in another buffer." + (let ((buf (get-buffer-create (format "*wttr.in - %s*" city-name)))) + (switch-to-buffer buf) + (insert (xterm-color-filter (wttrin-fetch-raw-string city-name))) + (goto-char (point-min)) + (re-search-forward "^$") + (delete-region (point-min) (1+ (point))) + (setq buffer-read-only t))) (defun wttrin (&optional force-ask) "Display weather information. @@ -42,14 +52,9 @@ Add C-u prefix to force to ask city name." (let* ((ask? (or current-prefix-arg force-ask)) (city-name (if ask? (read-from-minibuffer "City name: ") - (or wttring-default-city (read-from-minibuffer "City name: ")))) - (buf (get-buffer-create (format "*wttr.in - %s*" city-name)))) - (switch-to-buffer buf) - (insert (xterm-color-filter (wttrin-fetch-raw-string city-name))) - (goto-char (point-min)) - (re-search-forward "^$") - (delete-region (point-min) (1+ (point))) - (setq buffer-read-only t))) + (or wttring-default-city (read-from-minibuffer "City name: "))))) + (wttrin-query city-name))) + (provide 'wttrin) -- cgit v1.2.3 From ae1377c2176e2c64d800f412674bd8a8006cc4ff Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 01:54:45 +0800 Subject: Error handling --- wttrin.el | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/wttrin.el b/wttrin.el index c370a07..ee28cbe 100644 --- a/wttrin.el +++ b/wttrin.el @@ -21,7 +21,7 @@ :group 'comm) (defcustom wttring-default-city "Taipei" - "Specify a query condition to get the weather information." + "Specify a defauly city to get the weather information." :group 'wttrin :type 'string) @@ -37,13 +37,16 @@ (defun wttrin-query (city-name) "Query weather of CITY-NAME via wttrin, and display the result in another buffer." - (let ((buf (get-buffer-create (format "*wttr.in - %s*" city-name)))) - (switch-to-buffer buf) - (insert (xterm-color-filter (wttrin-fetch-raw-string city-name))) - (goto-char (point-min)) - (re-search-forward "^$") - (delete-region (point-min) (1+ (point))) - (setq buffer-read-only t))) + (let ((raw-string (wttrin-fetch-raw-string city-name))) + (if (string-match "ERROR" raw-string) + (message "Cannot get weather data. Maybe you inputed a wrong city name?") + (let ((buffer (get-buffer-create (format "*wttr.in - %s*" city-name)))) + (switch-to-buffer buffer) + (insert (xterm-color-filter raw-string)) + (goto-char (point-min)) + (re-search-forward "^$") + (delete-region (point-min) (1+ (point))) + (setq buffer-read-only t))))) (defun wttrin (&optional force-ask) "Display weather information. -- cgit v1.2.3 From 743a7e044d1fb748947781c09c25a0e9e960933f Mon Sep 17 00:00:00 2001 From: kuanyui Date: Sun, 6 Mar 2016 02:08:50 +0800 Subject: wttrin-default-cities & completion --- README.md | 9 +++------ wttrin.el | 19 +++++++------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b0c1859..d3055ec 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,15 @@ Emacs frontend for weather web service [wttr.in]. -wttrin.el provides the weather information from wttr.in based on your -query condition. - ## Usage -Set variable `wttrin-query`: +set a default cities list for completion: ```elisp -(defvar wttrin-query "Taipei") +(setq wttrin-default-city "Taipei") ``` -Run `M-x wttrin` to get the information: +Then run `M-x wttrin` to get the information. ![screenshot] diff --git a/wttrin.el b/wttrin.el index ee28cbe..ca35eca 100644 --- a/wttrin.el +++ b/wttrin.el @@ -14,16 +14,17 @@ ;;; Code: (require 'url) (require 'xterm-color) +(require 'ido) (defgroup wttrin nil "Emacs frontend for weather web service wttr.in." :prefix "wttrin-" :group 'comm) -(defcustom wttring-default-city "Taipei" - "Specify a defauly city to get the weather information." +(defcustom wttrin-default-cities '("Taipei" "Keelung" "Taichung" "Tainan") + "Specify default cities list to for quick completion" :group 'wttrin - :type 'string) + :type 'list) (defun wttrin-fetch-raw-string (query) "Get the weather information based on your QUERY." @@ -48,16 +49,10 @@ in another buffer." (delete-region (point-min) (1+ (point))) (setq buffer-read-only t))))) -(defun wttrin (&optional force-ask) - "Display weather information. -Add C-u prefix to force to ask city name." +(defun wttrin () + "Display weather information." (interactive) - (let* ((ask? (or current-prefix-arg force-ask)) - (city-name (if ask? - (read-from-minibuffer "City name: ") - (or wttring-default-city (read-from-minibuffer "City name: "))))) - (wttrin-query city-name))) - + (wttrin-query (ido-completing-read "City name: " wttrin-default-cities nil nil))) (provide 'wttrin) -- cgit v1.2.3