-
Notifications
You must be signed in to change notification settings - Fork 11
/
ebdb-i18n-basic.el
376 lines (342 loc) · 10.5 KB
/
ebdb-i18n-basic.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
;;; ebdb-i18n-basic.el --- Basic internationalization methods for EBDB -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2024 Free Software Foundation, Inc.
;; Author: Eric Abrahamsen <[email protected]>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides internationalized methods for a few common
;; countries, "common" here meaning what used to be in
;; `ebdb-address-format-list'.
;;; Code:
(eval-when-compile
;; For `with-slots'.
(require 'eieio))
(declare-function ebdb-i18n-countries "ebdb-i18n")
(declare-function ebdb-with-exit "ebdb")
(declare-function ebdb-address-region "ebdb")
(declare-function ebdb-read-string "ebdb")
;;; USA
(defvar ebdb-i18n-usa-states
'(("Alabama" . "AL")
("Alaska" . "AK")
("American Samoa " . "AS")
("Arizona" . "AZ")
("Arkansas" . "AR")
("California" . "CA")
("Colorado" . "CO")
("Connecticut" . "CT")
("Delaware" . "DE")
("Dist. of Columbia" . "DC")
("Florida" . "FL")
("Georgia" . "GA")
("Guam" . "GU")
("Hawaii" . "HI")
("Idaho" . "ID")
("Illinois" . "IL")
("Indiana" . "IN")
("Iowa" . "IA")
("Kansas" . "KS")
("Kentucky" . "KY")
("Louisiana" . "LA")
("Maine" . "ME")
("Maryland" . "MD")
("Marshall Islands" . "MH")
("Massachusetts" . "MA")
("Michigan" . "MI")
("Micronesia" . "FM")
("Minnesota" . "MN")
("Mississippi" . "MS")
("Missouri" . "MO")
("Montana" . "MT")
("Nebraska" . "NE")
("Nevada" . "NV")
("New Hampshire" . "NH")
("New Jersey" . "NJ")
("New Mexico" . "NM")
("New York" . "NY")
("North Carolina" . "NC")
("North Dakota" . "ND")
("Northern Marianas" . "MP")
("Ohio" . "OH")
("Oklahoma" . "OK")
("Oregon" . "OR")
("Palau" . "PW")
("Pennsylvania" . "PA")
("Puerto Rico" . "PR")
("Rhode Island" . "RI")
("South Carolina" . "SC")
("South Dakota" . "SD")
("Tennessee" . "TN")
("Texas" . "TX")
("Utah" . "UT")
("Vermont" . "VT")
("Virginia" . "VA")
("Virgin Islands" . "VI")
("Washington" . "WA")
("West Virginia" . "WV")
("Wisconsin" . "WI")
("Wyoming" . "WY"))
"All the states in the US, for use with completion.")
(cl-defmethod ebdb-parse-i18n ((_class (subclass ebdb-field-phone))
(str string)
(_cc (eql 1))
&optional slots)
"Parse a US phone number.
Uses first three digits as the area code, next seven as the
number, and any remaining as an extension."
(let ((numstr (replace-regexp-in-string "[^[:digit:]]+" "" str)))
(unless (plist-get slots :area-code)
(setq slots
(plist-put slots :area-code
(string-to-number (substring numstr 0 3)))
numstr (substring numstr 3)))
(setq slots (plist-put slots :number (substring numstr 0 7))
numstr (substring numstr 7))
(condition-case nil
(setq slots (plist-put
slots
:extension
(when (and (null (string-empty-p numstr))
(string-match-p "[[:digit:]]+" numstr))
(string-to-number numstr))))
(args-out-of-range nil))
slots))
;; Defined in `ebdb.el'
(defvar ebdb-default-phone-country)
(cl-defmethod ebdb-string-i18n ((phone ebdb-field-phone)
(_cc (eql 1)))
(with-slots (area-code number extension) phone
(format "%s(%d) %s-%s%s"
(if (eql ebdb-default-phone-country 1)
"" "+1 ")
area-code
(substring number 0 3)
(substring number 3)
(if extension (format "X%d" extension) ""))))
(cl-defmethod ebdb-read-i18n ((_class (subclass ebdb-field-address))
(_cc (eql usa))
&optional slots obj)
(unless (plist-member slots :region)
(setq slots
(plist-put
slots :region
(cdr (assoc-string
(ebdb-read-string
"Address state"
(when obj (rassoc (ebdb-address-region obj)
ebdb-i18n-usa-states))
ebdb-i18n-usa-states t)
ebdb-i18n-usa-states)))))
slots)
(cl-defmethod ebdb-parse-i18n ((_class (subclass ebdb-field-address))
(str string)
(_cc (eql usa))
&optional slots)
(let ((states (mapcar #'cdr ebdb-i18n-usa-states)))
(unless (plist-member slots :country)
(setq slots (plist-put slots :country 'usa)))
(with-temp-buffer
(insert str)
(when (re-search-backward "[[:digit:]]\\{5\\}\\(?:-[[:digit:]]\\{4\\}\\)?"
nil t)
(setq slots (plist-put slots :postcode (match-string 0))))
(when (re-search-backward (concat "\\(" (regexp-opt states) "\\)[ ,]+")
(line-beginning-position) t)
(setq slots (plist-put slots :region (match-string 1))))
(when (re-search-backward "\\(?:^\\|, \\)\\([[:alpha:].-]+ ?[[:alpha:].-]+\\)[ ,]+"
(line-beginning-position) t)
(setq slots (plist-put slots :locality (match-string 1))))
(setq slots (plist-put slots :streets
(split-string (buffer-substring (point-min) (point))
"[,\n]" t "[[:blank:]]"))))
slots))
;;; France
(cl-defmethod ebdb-string-i18n ((phone ebdb-field-phone)
(_cc (eql 33)))
(with-slots (area-code number extension) phone
(concat
(unless (eql ebdb-default-phone-country 33)
"+33 ")
(when area-code
(format "%02d" area-code))
(apply #'format "%s%s %s%s %s%s %s%s"
(split-string number "" t))
(when extension
(format "X%d" extension)))))
;;; Germany
(cl-defmethod ebdb-string-i18n ((phone ebdb-field-phone)
(_cc (eql 49)))
"Display a German phone number."
(let ((is-default (eql ebdb-default-phone-country 49))
num-len)
(with-slots (area-code number extension) phone
(setq num-len (length number))
(concat
(unless is-default
"+49 ")
(when area-code
(format (if is-default "(%03d) " "%d ") area-code))
(if (>= 4 num-len)
number
(mapconcat #'identity
(seq-partition number
(if (= 0 (mod num-len 3))
3 4))
" "))
(when extension
(format "-%d" extension))))))
(cl-defmethod ebdb-parse-i18n ((_class (subclass ebdb-field-phone))
(str string)
(_cc (eql 49))
&optional slots)
"Parse a German phone number.
Uses first block of digits as the area code, anything following a
hyphen as the extension, and everything in between as the number
itself."
(let ((area-code-regexp "^(?\\([[:digit:]]+\\))? +")
(extension-regexp "-\\([[:digit:]]+\\)\\'"))
(setq slots
(plist-put slots :area-code
(when (string-match area-code-regexp str)
(prog1
(string-to-number (match-string 1 str))
(setq str (replace-regexp-in-string
area-code-regexp "" str)))))
slots
(plist-put slots :extension
(when (string-match extension-regexp str)
(prog1
(string-to-number (match-string 1 str))
(setq str (replace-regexp-in-string
extension-regexp "" str))))))
(setq slots (plist-put
slots
:number
(replace-regexp-in-string
"[^[:digit:]]" "" str)))
slots))
(defvar ebdb-i18n-german-states
'(("Baden-Württemberg" . "BW")
("Bayern" . "BY")
("Berlin" . "BE")
("Brandenburg" . "BB")
("Bremen" . "HB")
("Hamburg" . "HH")
("Hessen" . "HE")
("Mecklenburg-Vorpommern" . "MV")
("Niedersachsen" . "NI")
("Nordrhein-Westfalen" . "NW")
("Rheinland-Pfalz" . "RP")
("Saarland" . "SL")
("Sachsen" . "SN")
("Sachsen-Anhalt" . "ST")
("Schleswig-Holstein" . "SH")
("Thüringen" . "TH"))
"All the states in Germany, for use with completion.")
(cl-defmethod ebdb-read-i18n ((_class (subclass ebdb-field-address))
(_cc (eql deu))
&optional slots obj)
(unless (plist-member slots :region)
(let ((state (ebdb-with-exit
(ebdb-read-string
"State"
(when obj (ebdb-address-region obj))
ebdb-i18n-german-states t))))
(setq slots
(plist-put
slots :region
(if state
(cdr (assoc-string state ebdb-i18n-german-states))
"")))))
slots)
(cl-defmethod ebdb-string-i18n ((address ebdb-field-address)
(_cc (eql deu)))
(with-slots (streets neighborhood locality region postcode) address
(concat
(when streets
(concat (mapconcat #'identity streets "\n") "\n"))
(when postcode
(format "%s " postcode))
locality
"\n"
(car-safe (rassq 'deu (ebdb-i18n-countries))))))
;;; India
(defvar ebdb-i18n-india-states
'("Andhra Pradesh"
"Arunachal Pradesh"
"Assam"
"Bihar"
"Chhattisgarh"
"Goa"
"Gujarat"
"Haryana"
"Himachal Pradesh"
"Jammu and Kashmir"
"Jharkhand"
"Karnataka"
"Kerala"
"Madhya Pradesh"
"Maharashtra"
"Manipur"
"Meghalaya"
"Mizoram"
"Nagaland"
"Odisha"
"Punjab"
"Rajasthan"
"Sikkim"
"Tamil Nadu"
"Telangana"
"Tripura"
"Uttar Pradesh"
"Uttarakhand"
"West Bengal")
"A list of states in India, for completion.")
(cl-defmethod ebdb-read-i18n ((_class (subclass ebdb-field-address))
(_cc (eql ind))
&optional slots obj)
(unless (plist-member slots :region)
(setq slots
(plist-put
slots :region
(cdr (assoc-string
(ebdb-read-string
"State"
(when obj (ebdb-address-region obj))
ebdb-i18n-india-states t)
ebdb-i18n-india-states)))))
slots)
;;; Russia
(cl-defmethod ebdb-string-i18n ((phone ebdb-field-phone)
(_cc (eql 8)))
(with-slots (area-code number extension) phone
(concat
(unless (eql ebdb-default-phone-country 8)
"+8 ")
(when area-code (format "%d " area-code))
(apply #'format
(cl-case (length number)
(5 "%s-%s%s-%s%s")
(6 "%s%s-%s%s-%s%s")
(7 "%s%s%s-%s%s-%s%s"))
(split-string number "" t))
(when extension (format " X%s" extension)))))
;;; Singapore
(cl-defmethod ebdb-read-i18n ((_cls (subclass ebdb-field-address))
(_cc (eql sgp))
&optional slots _obj)
"Singapore doesn't have localities, cities, or neighborhoods."
(setq slots (plist-put slots :locality "")
slots (plist-put slots :neighborhood "")
slots (plist-put slots :region ""))
slots)
(provide 'ebdb-i18n-basic)
;;; ebdb-i18n-basic.el ends here