summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl X. Su <bcbcarl@gmail.com>2016-03-06 02:34:03 +0800
committerCarl X. Su <bcbcarl@gmail.com>2016-03-06 02:34:03 +0800
commitb7660e58f59b57be0783a93fcc17616581b07086 (patch)
treea49d627359f28c817dae197b3d044564221a8989
parentaf5156fa66a9ad348504b47e23e6dbeffe51899c (diff)
parent0b6f748a202658751a3122cf3ec1da10a2efe7ba (diff)
Merge branch 'kuanyui-master'
-rw-r--r--README.md11
-rw-r--r--wttrin.el41
2 files changed, 28 insertions, 24 deletions
diff --git a/README.md b/README.md
index 65c0616..6e119ff 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-cities ("Taipei" "Tainan"))
```
-Run `M-x wttrin-exec` to get the information:
+Then run `M-x wttrin` to get the information.
![screenshot]
@@ -22,4 +19,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 c8b6847..a56b248 100644
--- a/wttrin.el
+++ b/wttrin.el
@@ -12,40 +12,47 @@
;; Provides the weather information from wttr.in based on your query condition.
;;; Code:
+
(require 'url)
(require 'xterm-color)
+(require 'ido)
(defgroup wttrin nil
"Emacs frontend for weather web service wttr.in."
:prefix "wttrin-"
:group 'comm)
-(defcustom wttrin-query "Taipei"
- "Specify a query condition to get the weather information."
+(defcustom wttrin-default-cities '("Taipei" "Keelung" "Taichung" "Tainan")
+ "Specify default cities list for quick completion."
:group 'wttrin
- :type 'string)
+ :type 'list)
-(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-exec ()
+(defun wttrin-query (city-name)
+ "Query weather of CITY-NAME via wttrin, and display the result in new buffer."
+ (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 ()
"Display weather information."
(interactive)
- (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)))
- (goto-char (point-min))
- (re-search-forward "^$")
- (delete-region (point-min) (1+ (point)))
- (setq buffer-read-only t))
+ (wttrin-query (ido-completing-read "City name: " wttrin-default-cities nil nil)))
(provide 'wttrin)