AddressFormatUK

Here’s how I format UK addresses in BbdbMode. Gives results like:

           home: ...
                 18 Charlie Way
                 Myhamlet
                 MYTOWN
                 A12 1AA
                 UK

Code:

(defun my-bbdb-format-address-uk (addr &optional indent)
  "Insert formated UK address ADDR in current buffer.

This function is a possible formatting function for
`bbdb-address-formatting-alist'.

The result looks like this:
       location: street
                 street
                 ...
                 CITY, 
                 state 
                 zip
                 country"

  (setq indent (or indent 14))
  (let ((fmt (format " %%%ds: " indent))
      (indent (+ 3 indent)))
          (insert (format fmt (bbdb-address-location addr)))
          (bbdb-format-streets addr indent)
          (let ((c (bbdb-address-city addr))
              (s (bbdb-address-state addr))
              (z (bbdb-address-zip addr))) 
              (if (> (length c) 0) (progn (indent-to indent) (insert (upcase c) "\n")))
		  (if (> (length s) 0) (progn (indent-to indent) (insert s "\n")))
                  (if (> (length z) 0) (progn (indent-to indent) (insert z "\n")))
              )
     (let ((str (bbdb-address-country addr)))
         (if (= 0 (length str)) nil
         (indent-to indent) (insert str "\n")))))

Note that this code requires BBDB 2.x. ATOW (2011-11-22) it breaks under 3.x.

Making this work automatically

Add this to your .emacs also:

(defun bbdb-address-is-uk (addr)
    "Return non-nil if the address ADDR is a continental address.
This is done by comparing the zip code to a UK postcode matching regexp.

This is a possible identifying function for
`bbdb-address-formatting-alist' and
`bbdb-address-print-formatting-alist'."
    (string-match "^\\s *[A-Z][A-Z]?[0-9][0-9]?\\s *[0-9][A-Z][A-Z]?" (bbdb-address-zip addr)))

(setq bbdb-address-formatting-alist '((bbdb-address-is-uk . my-bbdb-format-address-uk) (nil . bbdb-format-address-default)))

Comments to ChrisParsons.

BBDB v3 version

Here’s my (PhilHudson) setting for ‘bbdb-address-format-list’, which is the relevant user option in BBDB v3. It gives the same result as described above. I chose to make UK formatting the default; thus, the last element (with qualifying condition ‘t’), is the only thing different from the original out-of-the-box setting at time of writing (December 2014):

((("Argentina")
     "spcSC" "@%s\n@%p, @%c@, %S@\n%C@" "@%c@")
    (("Australia")
        "scSpC" "@%s\n@%c@ %S@ %p@\n%C@" "@%c@")
    (("Austria" "Germany" "Spain" "Switzerland")
        "spcSC" "@%s\n@%p @%c@ (%S)@\n%C@" "@%c@")
    (("Canada")
        "scSCp" "@%s\n@%c@, %S@\n%C@ %p@" "@%c@")
    (("China")
        "scpSC" "@%s\n@%c@\n%p@ %S@\n%C@" "@%c@")
    (("India")
        "scpSC" "@%s\n@%c@ %p@ (%S)@\n%C@" "@%c@")
    (("USA")
        "scSpC" "@%s\n@%c@, %S@ %p@\n%C@" "@%c@")
    (t "scSpC" "@%s\n@%c\n@%S\n@%p\n@%C@" "@%s ,@%c, @%S ,@%p ,@%C@"))

Wiki Note

Should this node be on BBDBAddressFormatUK? Just followed the existing convention.


CategoryBbdb