[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ebdb e326805 08/17: Simplify address formatting
From: |
Eric Abrahamsen |
Subject: |
[elpa] externals/ebdb e326805 08/17: Simplify address formatting |
Date: |
Tue, 26 Sep 2017 17:15:33 -0400 (EDT) |
branch: externals/ebdb
commit e326805edf7421648a782f80dff75d3362615476
Author: Eric Abrahamsen <address@hidden>
Commit: Eric Abrahamsen <address@hidden>
Simplify address formatting
The idea is to make the basic address formatting function very simple.
For all other requirements, users should be loading ebdb-i18n.
* ebdb.el (ebdb-default-address-format-function): New option
specifying a basic formatting function to use for all addresses.
(ebdb-string): Call that function from the ebdb-string method for
addresses.
Deleted the ebdb-address-format-list option, and the
ebdb-format-address function.
---
ebdb.el | 196 ++++++++++++----------------------------------------------------
1 file changed, 37 insertions(+), 159 deletions(-)
diff --git a/ebdb.el b/ebdb.el
index b0ee788..b112f22 100644
--- a/ebdb.el
+++ b/ebdb.el
@@ -277,6 +277,16 @@ case this variable will be ignored."
:group 'ebdb
:type '(list-of string))
+(defcustom ebdb-default-address-format-function #'ebdb-format-address-default
+ "Default function used to format an address.
+This function produces a string that looks more or less like a US
+address. Either write a custom function to format all addresses,
+or load `ebdb-i18n' to format addresses based on country-specific
+rules."
+
+ :group 'ebdb
+ :type 'function)
+
(defcustom ebdb-auto-revert nil
"If t revert unchanged database without querying.
@@ -485,76 +495,6 @@ It takes one argument, the name as extracted by
;;; Record editing
-;; The following two options should be obviated by ebdb-i18n.el
-;; See http://en.wikipedia.org/wiki/Postal_address
-;;
http://www.upu.int/en/activities/addressing/postal-addressing-systems-in-member-countstateries.html
-(defcustom ebdb-address-format-list
- '(((arg) "splrc" "@address@hidden, @%l@, address@hidden@" "@%l@")
- ((aus) "slrpc" "@address@hidden@ %r@ address@hidden@" "@%l@")
- ((aut due esp che)
- "splrc" "@address@hidden @%l@ (%r)@\n%c@" "@%l@")
- ((can) "slrcp" "@address@hidden@, address@hidden@ %p@" "@%l@")
- ((chn) "slprc" "@address@hidden@\n%p@ address@hidden@" "@%l@") ; English
format
- ; (("China") "cprls" "@%c @address@hidden @%l@ %s@" "@%l@") ; Chinese
format
- ((ind) "slprc" "@address@hidden@ %p@ (%r)@\n%c@" "@%l@")
- ((usa) "slrpc" "@address@hidden@, %r@ address@hidden@" "@%l@")
- (t ebdb-edit-address-default ebdb-format-address-default "@%l@"))
- "List of address editing and formatting rules for EBDB.
-Each rule is a list (IDENTIFIER EDIT FORMAT FORMAT).
-The first rule for which IDENTIFIER matches an address is used for editing
-and formatting the address.
-
-IDENTIFIER may be a list of countries.
-IDENTIFIER may also be a function that is called with one arg, the address
-to be used. The rule applies if the function returns non-nil.
-See `ebdb-address-continental-p' for an example.
-If IDENTIFIER is t, this rule always applies. Usually, this should be
-the last rule that becomes a fall-back (default).
-
-EDIT may be a function that is called with one argument, the address.
-See `ebdb-edit-address-default' for an example.
-
-EDIT may also be an editting format string. It is a string containing
-the five letters s, c, p, S, and C that specify the order for editing
-the five elements of an address:
-
-s streets
-l locality
-p postcode
-r region
-c country
-
-The first FORMAT of each rule is used for multi-line layout, the second FORMAT
-is used for one-line layout.
-
-FORMAT may be a function that is called with one argument, the address.
-See `ebdb-format-address-default' for an example.
-
-FORMAT may also be a format string. It consists of formatting elements
-separated by a delimiter defined via the first (and last) character of FORMAT.
-Each formatting element may contain one of the following format specifiers:
-
-%s streets (used repeatedly for each street part)
-%l locality
-%p postcode
-%r region
-%c country
-
-A formatting element will be applied only if the corresponding part
-of the address is a non-empty string.
-
-See also `ebdb-print-address-format-list'."
- :group 'ebdb-record-edit
- :type '(repeat (list (choice (const :tag "Default" t)
- (function :tag "Function")
- (repeat (string)))
- (choice (string)
- (function :tag "Function"))
- (choice (string)
- (function :tag "Function"))
- (choice (string)
- (function :tag "Function")))))
-
(defcustom ebdb-default-separator '("[,;]" ", ")
"The default field separator. It is a list (SPLIT-RE JOIN).
This is used for fields which do not have an entry in `ebdb-separator-alist'."
@@ -1038,7 +978,8 @@ in." nil)
:initarg :object-name
:custom (choice (const :tag "Empty" nil)
string)
- :initform nil)
+ :initform nil
+ :documentation "String label")
(label-list
:initform nil
:type (or null symbol)
@@ -1685,14 +1626,32 @@ first one."
((ebdb-empty quit) nil))
(reverse list)))
-;; Addresses are displayed using `ebdb-format-address', which is
-;; probably a candidate for refactoring. In the meantime, provide a
-;; stop-gap `ebdb-string' method for addresses that doesn't go through
-;; the whole formatting routine, and additionally fits on one line.
-;; This should probably get re-thought/re-worked later.
-
(cl-defmethod ebdb-string ((address ebdb-field-address))
- (ebdb-format-address address 2))
+ (funcall ebdb-default-address-format-function address))
+
+(defun ebdb-format-address-default (address)
+ "Return formatted ADDRESS as a string.
+This is the default format; it is used in the US, for example.
+The result looks like this:
+ label: street
+ street
+ ...
+ locality, region postcode
+ country."
+ (let ((country (ebdb-address-country address))
+ (streets (ebdb-address-streets address)))
+ (when (symbolp country)
+ (require 'ebdb-i18n)
+ (setq country (car-safe (rassq
+ country
+ (ebdb-i18n-countries)))))
+ (concat (if streets
+ (concat (mapconcat 'identity streets "\n") "\n"))
+ (ebdb-concat ", " (ebdb-address-locality address)
+ (ebdb-concat " " (ebdb-address-region address)
+ (ebdb-address-postcode address)))
+ (unless (or (not country) (string= "" country))
+ (concat "\n" country)))))
;;; Phone fields
@@ -4951,87 +4910,6 @@ actual speedup."
(defvar ebdb-i18n-countries-pref-scripts)
-;; This function can provide some guidance for writing
-;; your own address formatting function
-(defun ebdb-format-address-default (address)
- "Return formatted ADDRESS as a string.
-This is the default format; it is used in the US, for example.
-The result looks like this:
- label: street
- street
- ...
- locality, region postcode
- country.
-
-This function is a possible formatting function for
-`ebdb-address-format-list'."
- (let ((country (ebdb-address-country address))
- (streets (ebdb-address-streets address)))
- (when (symbolp country)
- (require 'ebdb-i18n)
- (setq country (car-safe (rassq
- country
- (ebdb-i18n-countries)))))
- (concat (if streets
- (concat (mapconcat 'identity streets "\n") "\n"))
- (ebdb-concat ", " (ebdb-address-locality address)
- (ebdb-concat " " (ebdb-address-region address)
- (ebdb-address-postcode address)))
- (unless (or (not country) (string= "" country))
- (concat "\n" country)))))
-
-(defun ebdb-format-address (address layout)
- "Format ADDRESS using LAYOUT. Return result as a string.
-The formatting rules are defined in `ebdb-address-format-list'."
- (let ((list ebdb-address-format-list)
- (country (ebdb-address-country address))
- elt string)
- (while (and (not string) (setq elt (pop list)))
- (let ((identifier (car elt))
- (format (nth layout elt))
- ;; recognize case for format identifiers
- case-fold-search str)
- (when (or (eq t identifier) ; default
- (and (functionp identifier)
- (funcall identifier address))
- (and country
- (listp identifier)
- ;; ignore case for countries
- (assq country identifier)))
- (cond ((functionp format)
- (setq string (funcall format address)))
- ((stringp format)
- (setq string "")
- (dolist (form (split-string (substring format 1 -1)
- (substring format 0 1) t))
- (cond ((string-match "%s" form) ; street
- (mapc (lambda (s) (setq string (concat string
(format form s))))
- (ebdb-address-streets address)))
- ((string-match "%l" form) ; locality
- (unless (or (not (setq str (ebdb-address-locality
address))) (string= "" str))
- (setq string (concat string (format
(replace-regexp-in-string "%l" "%s" form) str)))))
- ((string-match "%p" form) ; postcode
- (unless (or (not (setq str (ebdb-address-postcode
address))) (string= "" str))
- (setq string (concat string (format
(replace-regexp-in-string "%p" "%s" form) str)))))
- ((string-match "%r" form) ; region
- (unless (or (not (setq str (ebdb-address-region
address))) (string= "" str))
- (setq string (concat string (format
(replace-regexp-in-string "%r" "%s" form t) str)))))
- ((string-match "%c" form) ; country
- (when (symbolp country)
- (require 'ebdb-i18n)
- (setq country (or (car-safe
- (rassq
- country
- (append
ebdb-i18n-countries-pref-scripts
-
ebdb-i18n-countries))))))
- (unless (or (not country) (string= "" country))
- (setq string (concat string (format
(replace-regexp-in-string "%c" "%s" form t) country)))))
- (t (error "Malformed address format element %s"
form)))))
- (t (error "Malformed address format %s" format))))))
- (unless string
- (error "No match of `ebdb-address-format-list'"))
- string))
-
;;; Citing EBDB records
- [elpa] externals/ebdb 26d632b 05/17: Finish ebdb-internationalize-addresses, (continued)
- [elpa] externals/ebdb 26d632b 05/17: Finish ebdb-internationalize-addresses, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb 3f1eb0d 04/17: New defsubst ebdb-i18n-countries, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb f143265 07/17: Quietly swap order of arguments to ebdb-read-i18n for addresses, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb 880be9f 14/17: Fix to dumb mistakes in 8fc7b6d69, fast lookups, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb b371784 15/17: Change ebdb-vcard to use generic ebdb-field-tags, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb c5aac30 01/17: Change name of automatic save option, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb fb7e4d2 11/17: Add formatting for Russian phone numbers, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb e947f59 06/17: Drop postcode guards, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb e78dd19 10/17: Provide an ebdb-parse method for addresses, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb 7e7d6cd 09/17: Start collecting random internationalized methods, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb e326805 08/17: Simplify address formatting,
Eric Abrahamsen <=
- [elpa] externals/ebdb 4f8e2a0 16/17: Use ebdb-i18n-countries defsubst in i18n address reading, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb 91d73f8 17/17: Bump version to 0.3.3, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb 7c16983 12/17: Fix all when-let and if-let to use starred versions, Eric Abrahamsen, 2017/09/26
- [elpa] externals/ebdb bd7b45a 13/17: Revert "Use value of ebdb-mua-folder-list in VM splitting", Eric Abrahamsen, 2017/09/26