branch: externals/ebdb
commit 1ae4bbfc21085b91e0935b28d7312e1fdb26ff52
Author: Eric Abrahamsen <[email protected]>
Commit: Eric Abrahamsen <[email protected]>
Provide internationalized ebdb-string and ebdb-parse for phones
Fixes #29
* ebdb-i18n.el (ebdb-string, ebdb-parse): Provide internationalized
hooks keyed on the integer country code.
* ebdb-chn.el (ebdb-string-i18n, ebdb-parse-i18n): Implement for
Chinese phone numbers.
---
ebdb-chn.el | 34 ++++++++++++++++++++++++++++++++++
ebdb-i18n.el | 18 ++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/ebdb-chn.el b/ebdb-chn.el
index 4a6192d..dce7dfe 100644
--- a/ebdb-chn.el
+++ b/ebdb-chn.el
@@ -29,6 +29,40 @@
(require 'chinese-pyim-dictools)
+(cl-defmethod ebdb-string-i18n ((phone ebdb-field-phone)
+ (_cc (eql 86)))
+ (with-slots (area-code number extension) phone
+ (concat
+ "+86 "
+ (if area-code
+ (format "%d-%s" area-code number)
+ number)
+ (if extension
+ (format "X%d" extension)
+ ""))))
+
+(cl-defmethod ebdb-parse-i18n ((class (subclass ebdb-field-phone))
+ (str string)
+ (cc (eql 86))
+ &optional slots)
+ ;; First remove everything but the numbers.
+ (let ((num-str (string-trim
+ (replace-regexp-in-string "[^0-9Xx]+" "" str)))
+ a-code number extension)
+ ;; In China, basically everything that starts with a 1 is a cell
+ ;; number, unless it starts with a 10, in which case it's the
+ ;; Beijing area code. Sometimes the area codes are written with a
+ ;; leading zero, but they shouldn't be saved that way.
+ (when (string-match "\\`0?\\(10\\|[2-9][0-9]\\{1,2\\}\\)?\\([0-9]+\\)"
num-str)
+ (setq a-code (match-string 1 num-str)
+ slots (plist-put slots :number (match-string 2 num-str)))
+ (when a-code
+ (setq slots (plist-put slots :area-code (string-to-number a-code)))))
+ (when (string-match "X\\([0-9]+\\)\\'" num-str)
+ (setq slots (plist-put slots :extension
+ (string-to-number (match-string 1 num-str)))))
+ (apply #'make-instance 'ebdb-field-phone slots)))
+
;; This isn't all of them, but it seems like a reasonable subset. See
;; https://en.wikipedia.org/wiki/Chinese_compound_surname for a fuller
;; list.
diff --git a/ebdb-i18n.el b/ebdb-i18n.el
index 2230e27..3224489 100644
--- a/ebdb-i18n.el
+++ b/ebdb-i18n.el
@@ -149,6 +149,24 @@ for their symbol representations.")
slots)))
(cl-call-next-method class slots obj)))
+(cl-defmethod ebdb-string :extra "i18n" ((phone ebdb-field-phone))
+ "Internationally-aware version of `ebdb-string' for phones."
+ (let ((cc (slot-value phone 'country-code)))
+ (or (and cc
+ (condition-case nil
+ (ebdb-string-i18n phone cc)
+ (cl-no-applicable-method nil)))
+ (cl-call-next-method))))
+
+(cl-defmethod ebdb-parse :extra "i18n" ((class (subclass ebdb-field-phone))
+ (str string)
+ &optional slots)
+ (let ((cc (plist-get slots :country-code)))
+ (or (and cc
+ (condition-case nil
+ (ebdb-parse-i18n class str cc slots)
+ (cl-no-applicable-method nil)))
+ (cl-call-next-method))))
;; We don't need to override the `ebdb-read' method for names. It
;; only matters what script the name is in if the user has set