Use `format-spec' to return the weather string.
[google-weather-el.git] / org-google-weather.el
blob0b71b5b0626885b2ea3777231cdf158762e494f7
1 ;;; org-google-weather.el --- Show Google Weather forecasts in Org agenda.
3 ;; Copyright (C) 2010 Julien Danjou
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: comm
8 ;; This file is NOT part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
24 ;; This module allows to display the weather forecast fetched from Google in
25 ;; your Org agenda.
27 ;; Wednesday 8 September 2010
28 ;; Weather: Pluie, 12/18 ℃
29 ;; Thursday 9 September 2010
30 ;; Weather: Couverture nuageuse partielle, 11/21 ℃
32 ;; Just add the following in an Org buffer:
33 ;; %%(org-google-weather)
35 ;;; Code:
37 (require 'google-weather)
38 (require 'image)
39 (require 'format-spec)
41 (defgroup org-google-weather nil
42 "Google Weather for Org mode."
43 :group 'comm)
45 (defcustom org-google-weather-location "Paris"
46 "Default location for org-google-weather."
47 :group 'org-google-weather)
49 (defcustom org-google-weather-format "%i %c, %l-%h %s"
50 "String to return to describe the weather.
51 Valid %-sequences are:
52 - %i the icon
53 - %c means the weather condition
54 - %L the location
55 - %l the lower temperature
56 - %h the higher temperature
57 - %s the temperature unit symbol")
59 (defcustom org-google-weather-cache-time 43200
60 "Define for how many seconds we should cache the weather."
61 :group 'org-google-weather)
63 (defcustom org-google-weather-display-icon-p t
64 "Display icons."
65 :group 'org-google-weather)
67 (defcustom org-google-weather-icon-directory "/usr/share/icons/gnome/16x16/status"
68 "Directory where to find icon listed in `org-google-weather-icon-alist'."
69 :group 'org-google-weather)
71 (defcustom org-google-weather-icon-alist
72 '((chance_of_rain . "weather-showers-scattered.png")
73 (chance_of_snow . "weather-snow.png")
74 (chance_of_storm . "weather-storm.png")
75 (cn_heavyrun . "weather-showers.png")
76 (cloudy . "weather-overcast.png")
77 (dust . "weather-fog.png")
78 (flurries . "weather-storm.png")
79 (fog . "weather-fog.png")
80 (haze . "weather-fog.png")
81 (icy . "weather-snow.png")
82 (mist . "weather-storm.png")
83 (mostly_cloudy . "weather-overcast.png")
84 (mostly_sunny . "weather-clear.png")
85 (partly_cloudy . "weather-few-clouds.png")
86 (rain . "weather-showers.png")
87 (sleet . "weather-snow.png")
88 (smoke . "weather-fog.png")
89 (snow . "weather-snow.png")
90 (storm . "weather-storm.png")
91 (thunderstorm . "weather-storm.png")
92 (sunny . "weather-clear.png"))
93 "Icons to used to illustrate the weather.")
95 ;;;###autoload
96 (defun org-google-weather (&optional location language)
97 "Return Org entry with the weather for LOCATION in LANGUAGE.
98 If LOCATION is not set, use org-google-weather-location."
99 (let* ((data (google-weather-get-data (or location
100 org-google-weather-location)
101 language
102 org-google-weather-cache-time))
103 (forecast (google-weather-data->forecast-for-date data date)))
104 (when forecast
105 (let ((condition (cadr (assoc 'condition forecast)))
106 (low (cadr (assoc 'low forecast)))
107 (high (cadr (assoc 'high forecast)))
108 ;; But *they* told me it's just about calling functions!
109 (icon (cdr
110 (assoc
111 (intern
112 (file-name-sans-extension
113 (file-name-nondirectory
114 (cadr (assoc 'icon forecast)))))
115 org-google-weather-icon-alist)))
116 (temp-symbol (google-weather-data->temperature-symbol data)))
117 (format-spec org-google-weather-format
118 `((?i . ,(if org-google-weather-display-icon-p
119 (concat (propertize "icon"
120 'display
121 (create-image
122 (concat
123 org-google-weather-icon-directory
125 icon))
126 'rear-nonsticky '(display))
127 " ")
128 ""))
129 (?c . ,condition)
130 (?L . ,location)
131 (?l . ,low)
132 (?h . ,high)
133 (?s . ,temp-symbol)))))))
135 (provide 'org-google-weather)