Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 bbdb3 (3.1.2-5) unstable; urgency=medium
 .
   * track upstream changes, mainly minor maintenance
Author: Barak A. Pearlmutter <bap@debian.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- bbdb3-3.1.2.orig/ChangeLog
+++ bbdb3-3.1.2/ChangeLog
@@ -1,3 +1,172 @@
+2015-09-10  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-anniv.el, lisp/bbdb-mu4e.el, lisp/bbdb-print.el:
+	* lisp/bbdb-snarf.el, lisp/bbdb-com.el, lisp/bbdb-message.el:
+	* lisp/bbdb-mua.el, lisp/bbdb-rmail.el, lisp/bbdb-gnus.el:
+	* lisp/bbdb-mhe.el, lisp/bbdb-pgp.el, lisp/bbdb-sc.el:
+	* lisp/bbdb-vm.el, lisp/bbdb-ispell.el, lisp/bbdb-migrate.el:
+	* lisp/bbdb-site.el.in, lisp/bbdb.el: Conform to Emacs Lisp
+	package format convention.
+	(Bug#45910)
+
+2015-09-10  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb.el (bbdb-separator-alist): Use two newline characters
+	to separate records.
+
+2015-09-07  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-com.el (bbdb-search-changed): Fix docstring.
+
+2015-09-07  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-com.el (bbdb-delete-field-or-record): Use delete for
+	phone and address fields.
+
+2015-09-07  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-com.el (bbdb-copy-records-as-kill): Fix docstring.
+	Delete unused local variable marker.
+
+2015-09-07  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-com.el (bbdb-copy-fields-as-kill): New command.
+	* lisp/bbdb.el (bbdb-separator-alist, bbdb-mode-map): Update
+	accordingly.
+
+2015-05-23  Eric Abrahamsen <eric@ericabrahamsen.net>
+	* lisp/bbdb.el (bbdb-record-set-field, bbdb-parse-records): Use
+	equal for comparison when populating lists of labels.
+
+2015-05-22  Roland Winkler  <winkler@gnu.org>
+	Add basic support for mu4e mailer.
+	* NEWS, README: Update accordingly.
+	* m4/emacs_mu4e.m4: New file.
+	* configure.ac: Use it.
+	* lisp/bbdb-mu4e.el: New file.
+	* lisp/Makefile.am, lisp/makefile-temp: Compile it.
+	* lisp/bbdb.el (bbdb-init-forms): Add entry for mu4e.
+	(bbdb-initialize): Update docstring.
+	* lisp/bbdb-mua.el: Define mu4e~view-buffer-name.
+	(bbdb-mua-mode-alist): Add element for mu4e.
+	(bbdb-mua): Update docstring.
+	(bbdb-mua-update-records, bbdb-mua-wrapper): Handle mu4e.
+
+2015-05-22  Roland Winkler  <winkler@gnu.org>
+	Remove variables bbdb/MUA-update-records-p.
+	* lisp/bbdb-mua.el (bbdb-update-records): Rely only on arg
+	update-p.
+	* lisp/bbdb-gnus.el (bbdb/gnus-update-records-p): Remove.
+	* lisp/bbdb-message.el (bbdb/mail-update-records-p)
+	(bbdb/message-update-records-p): Remove.
+	* lisp/bbdb-mh.el (bbdb/mh-update-records-p): Remove.
+	* lisp/bbdb-rmail.el (bbdb/rmail-update-records-p): Remove.
+	* lisp/bbdb-vm.el (bbdb/vm-update-records-p): Remove.
+	* README, NEWS: Update accordingly.
+
+2015-05-22  Roland Winkler  <winkler@gnu.org>
+	Handle prefix command bbdb-do-all-records more robustly.
+	* lisp/bbdb.el (bbdb-do-all-records): New variable.
+	(bbdb-modeline-info): Add two new slots.
+	(bbdb-mode): Use them.
+	* lisp/bbdb-com.el (bbdb-prefix-message): New function.
+	(bbdb-do-all-records, bbdb-do-records): Use variable
+	bbdb-do-all-records.
+	(bbdb-append-display-p): Update displayed message.
+	(bbdb-append-display): Use bbdb-prefix-message.
+	(bbdb-search-invert): Ditto.  Simplify.
+
+2015-05-22  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb.el (bbdb-redisplay-record): Display an undisplayed
+	record only if we do not want to delete it.
+	(bbdb-maybe-update-display): Only consider records that are
+	already displayed.  Improve docstring.
+
+2015-05-22  Roland Winkler  <winkler@gnu.org>
+	Update copyright year in all files.
+
+2014-08-30  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-print.el (bbdb-print-require): Improve docstring.
+
+2014-08-30  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-print.el (bbdb-print): Clarify prompt for file name.
+	Issue message on what to do with TeX file.
+
+2014-08-30  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-print.el (bbdb-print-record): Handle xfields the value
+	of which are sexps.
+
+2014-08-09  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-snarf.el (bbdb-snarf): Always install and display the
+	new record.
+
+2014-08-01  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb-com.el (bbdb-mail-alias-list): New function.
+	(bbdb-add-mail-alias): Handle multiple records via * prefix.
+	Allow addition or deletion of multiple aliases per record.  Fix
+	docstring.
+
+2014-08-01  Roland Winkler  <winkler@gnu.org>
+	* lisp/makefile-temp: Fix previous change.
+
+2014-07-22  Roland Winkler  <winkler@gnu.org>
+	* lisp/Makefile.am: Do not load init files or site files for byte
+	compilation (Bug#42482). Use long options.
+	* lisp/makefile-temp: Ditto.  New variable emacs_compile.
+
+2014-05-15  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb.el (bbdb-redisplay-record): Delete record from
+	bbdb-records if record is undisplayed.
+	* lisp/bbdb-com.el (bbdb-omit-record): Simplify. Handle records at
+	beginning and end of bbdb-buffer properly.
+
+2014-05-15  Roland Winkler  <winkler@gnu.org>
+	Fix and improve previous patch.
+	* lisp/bbdb.el (bbdb-update-unchanged-records): Renamed from
+	bbdb-save-unchanged-records.
+	(bbdb-with-print-loadably): Put at beginning of bbdb.el.
+	(bbdb-change-record): Return record only if we updated it.
+	* lisp/bbdb-com.el (bbdb-touch-records): Use
+	bbdb-update-unchanged-records.
+	(bbdb-insert-field, bbdb-edit-field): Issue message if record
+	remained unchanged.
+
+2014-05-12  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb.el (bbdb-change-record): If an editing command did not
+	change a record compared to its value in bbdb-buffer, do not call
+	bbdb-change-hook and do not save it.
+	(bbdb-save-unchanged-records): New internal variable.
+	* lisp/bbdb-com.el (bbdb-touch-records): New command.
+
+2014-05-12  Roland Winkler  <winkler@gnu.org>
+	* lisp/makefile-temp: Create bbdb-pkg.el from bbdb-pkg.el.in.
+
+2014-05-06  Roland Winkler  <winkler@gnu.org>
+	Do not treat bbdb-change-hook special when inside
+	bbdb-notice-mail-hook or bbdb-notice-record-hook.
+	* lisp/bbdb.el (bbdb-notice-mail-hook, bbdb-notice-record-hook):
+	Update docstring.
+	(bbdb-notice-hook-pending): Remove.
+	(bbdb-change-record): Always call bbdb-change-hook if a record was
+	changed.
+	* lisp/bbdb-mua.el (bbdb-update-records, bbdb-annotate-message):
+	Change accordingly.
+
+2014-05-06  Roland Winkler  <winkler@gnu.org>
+	Allow arbitrary lisp expressions as values of xfields.
+	* lisp/bbdb.el (bbdb-record-type): Update accordingly.
+	(bbdb-string-trim): New optional arg null.
+	(bbdb-record-xfield-intern): Return xfield value unmodified if it
+	is not a string.
+	(bbdb-record-xfield-string): New function.
+	(bbdb-record-xfield-split): Throw error if xfield value is not a
+	string.
+	(bbdb-record-set-xfield, bbdb-record-set-field)
+	(bbdb-merge-xfield, bbdb-display-record-one-line)
+	(bbdb-display-record-multi-line)
+	* lisp/bbdb-com.el (bbdb-search, bbdb-read-field)
+	(bbdb-edit-field, bbdb-read-xfield): Allow xfield values that are
+	not a string.
+	(bbdb-add-mail-alias): Simplify.
+
+2014-05-06  Roland Winkler  <winkler@gnu.org>
+	* lisp/bbdb.el (bbdb-parse-postcode): Finish immediately if one
+	test succeeds.
+
 2014-04-27  Roland Winkler  <winkler@gnu.org>
 	* configure.ac: Increase BBDB version number to 3.1.2.
 	* NEWS: Update for release.
@@ -1865,7 +2034,7 @@
 ;; coding: utf-8
 ;; End:
 
-  Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+  Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
   This file is part of the Insidious Big Brother Database (aka BBDB),
 
--- bbdb3-3.1.2.orig/Makefile.am
+++ bbdb3-3.1.2/Makefile.am
@@ -1,7 +1,7 @@
 # main Makefile.am for BBDB
 #
 # Copyright (C) 2013 Christian Egli <christian.egli@sbs.ch>
-# Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 #
 # This file is part of the Insidious Big Brother Database (aka BBDB),
 #
--- bbdb3-3.1.2.orig/NEWS
+++ bbdb3-3.1.2/NEWS
@@ -1,10 +1,25 @@
 BBDB NEWS -- history of user-visible changes.
 
-Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 See the end of the file for license conditions.
 
 This file is about changes in BBDB version 3.
 
+* BBDB 3.2
+** Support for Mu4e has been added.
+
+** Incompatible Changes in BBDB 3.2
+
+*** The variables bbdb/MUA-update-records-p have been removed.
+This includes the variables bbdb/gnus-update-records-p,
+bbdb/mail-update-records-p, bbdb/message-update-records-p,
+bbdb/mh-update-records-p, bbdb/rmail-update-records-p,
+bbdb/vm-update-records-p.
+
+These fall-back variables collided with the user variables
+bbdb-mua-update-interactive-p and bbdb-mua-auto-update-p.
+Use function bbdb-mua to define your own function to get
+MUA-specific values.
 
 * BBDB 3.1.2
 ** Bug fix release
--- bbdb3-3.1.2.orig/README
+++ bbdb3-3.1.2/README
@@ -1,10 +1,10 @@
-Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 See the end of the file for license conditions.
 
 BBDB is the Insidious Big Brother Database for GNU Emacs.
 It provides an address book for email and snail mail addresses,
 phone numbers and the like.  It can be linked with various Emacs mail
-clients (Message and Mail mode, Rmail, Gnus, MH-E, and VM).
+clients (Message and Mail mode, Rmail, Gnus, MH-E, Mu4e, and VM).
 BBDB is fully customizable.
 
 BBDB is available at
@@ -37,6 +37,9 @@ To compile and install BBDB with `make':
 
    The `configure' script comes with various options:
 
+   `--with-mu4e-dir=DIR' specifies the path where Mu4e can be found.
+     Without this option the resulting BBDB build does not support Mu4e.
+
    `--with-vm-dir=DIR' specifies the path where VM can be found.
      Without this option the resulting BBDB build does not support VM.
 
@@ -108,7 +111,7 @@ BBDB interface with mail user agents (MU
 ===========================================
 
 BBDB can interface with various mail user agents (MUAs).
-These include Rmail, Gnus, VM, MH-E, Message and Mail mode.
+These include Rmail, Gnus, VM, MH-E, Mu4e, Message and Mail mode.
 This lets you
 
  - display the BBDB records for the sender and/or recipients of a
@@ -158,11 +161,8 @@ WITHOUT-PREFIX and WITH-PREFIX may take
  read         Read the value interactively.
 
 BBDB 2 also used MUA-specific variables bbdb/MUA-update-records-mode
-to control its interfaces with MUAs.  In BBDB 3 the variables
-bbdb/MUA-update-records-p are only used as fallback if the generic
-(MUA-independent) variables bbdb-mua-update-interactive-p,
-bbdb-update-records-p or bbdb-mua-auto-update-p result in a value of
-nil for the arg UPDATE-P of bbdb-update-records.
+to control its interfaces with MUAs.  Use function bbdb-mua to define
+your own function to get MUA-specific values.
 
 Noninteractive functions
 ------------------------
@@ -206,7 +206,7 @@ sequence of calls is
 
 ==================================================================
 
-Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 This file is part of the Insidious Big Brother Database (aka BBDB),
 
--- bbdb3-3.1.2.orig/TODO
+++ bbdb3-3.1.2/TODO
@@ -1,6 +1,6 @@
 BBDB todo List                                                   -*-outline-*-
 
-Copyright (C) 2011-2014 Roland Winkler <winkler@gnu.org>
+Copyright (C) 2011-2015 Roland Winkler <winkler@gnu.org>
 See the end of the file for license conditions.
 
 * Bug fixes
@@ -52,7 +52,7 @@ See the end of the file for license cond
    2011-05-05 Leo <sdl.web@gmail.com>
 
 
-Copyright (C) 2011-2014 Roland Winkler <winkler@gnu.org>
+Copyright (C) 2011-2015 Roland Winkler <winkler@gnu.org>
 
 This file is part of the Insidious Big Brother Database (aka BBDB),
 
--- bbdb3-3.1.2.orig/autogen.sh
+++ bbdb3-3.1.2/autogen.sh
@@ -2,7 +2,7 @@
 ### autogen.sh - tool to help build BBDB from a git checkout
 
 ## Copyright (C) 2013 Christian Egli <christian.egli@sbs.ch>
-## Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+## Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 ##
 ## This file is part of the Insidious Big Brother Database (aka BBDB),
 ##
--- bbdb3-3.1.2.orig/configure.ac
+++ bbdb3-3.1.2/configure.ac
@@ -1,7 +1,7 @@
 # configure.ac --- configuration setup for BBDB
 
 # Copyright (C) 2000-2001 Didier Verna <didier@xemacs.org>
-# Copyright (C) 2011-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2011-2015 Roland Winkler <winkler@gnu.org>
 # 
 # Author:        Didier Verna <didier@xemacs.org>
 # Maintainer:    Roland Winkler <winkler@gnu.org>
@@ -40,6 +40,7 @@ AS_IF([test "$EMACS" = no], [AC_MSG_ERRO
 
 # Checks for libraries.
 EMACS_VM
+EMACS_MU4E
 AC_SUBST([AM_ELCFLAGS])
 
 # Generate lisp/bbdb-site.el via lisp/Makefile as pkgdatadir is only known
--- bbdb3-3.1.2.orig/doc/Makefile.am
+++ bbdb3-3.1.2/doc/Makefile.am
@@ -1,7 +1,7 @@
 # doc/Makefile.am for BBDB
 #
 # Copyright (C) 2013 Christian Egli <christian.egli@sbs.ch>
-# Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 # 
 # This file is part of the Insidious Big Brother Database (aka BBDB),
 # 
--- bbdb3-3.1.2.orig/doc/bbdb.texi
+++ bbdb3-3.1.2/doc/bbdb.texi
@@ -7,7 +7,7 @@
 @copying
 This file documents the Insidious Big Brother Database (BBDB)
 
-Copyright (C) 2011-2014 Roland Winkler <winkler@@gnu.org>
+Copyright (C) 2011-2015 Roland Winkler <winkler@@gnu.org>
 
 @quotation
 Permission is granted to copy, distribute and/or modify this document
--- bbdb3-3.1.2.orig/lisp/Makefile.am
+++ bbdb3-3.1.2/lisp/Makefile.am
@@ -1,6 +1,6 @@
 # lisp/Makefile.am for BBDB
 #
-# Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 # Author: Roland Winkler <winkler@gnu.org>
 #  	  Christian Egli <christian.egli@sbs.ch>
@@ -20,6 +20,9 @@
 # You should have received a copy of the GNU General Public License
 # along with BBDB.  If not, see <http://www.gnu.org/licenses/>.
 
+# --batch implies --no-init-file, yet let's be explicit about what we want
+AM_ELCFLAGS += --no-init-file --no-site-file
+
 dist_lisp_LISP = 				\
 	bbdb.el					\
 	bbdb-anniv.el				\
@@ -43,6 +46,12 @@ dist_lisp_LISP += bbdb-vm.el
 # in the Emacs load path when compiling BBDB with VM support.
 endif
 
+if MU4E
+dist_lisp_LISP += bbdb-mu4e.el
+# We use the Automake variable AM_ELCFLAGS to include the Mu4e lisp directory
+# in the Emacs load path when compiling BBDB with Mu4e support.
+endif
+
 lisp_DATA = bbdb-loaddefs.el
 
 MOSTLYCLEANFILES = bbdb-loaddefs.el
@@ -70,10 +79,11 @@ bbdb-loaddefs.el: $(dist_lisp_LISP)
 	@echo "" >> $@;
 #	Generated autoload-file must have an absolute path,
 #	$srcdir can be relative.
-	$(EMACS) -batch -l autoload \
+	$(EMACS) --batch $(AM_ELCFLAGS) $(ELCFLAGS) \
+		--load autoload \
 		--eval '(setq generated-autoload-file "'$(abs_builddir)/$@'")' \
 		--eval '(setq make-backup-files nil)' \
-		-f batch-update-autoloads $(srcdir)
+		--funcall batch-update-autoloads $(srcdir)
 
 # Generate bbdb-site.el here as pkgdatadir is only known at "make" time.
 # We protect the autoconf variables in the sed regular expressions
--- bbdb3-3.1.2.orig/lisp/bbdb-anniv.el
+++ bbdb3-3.1.2/lisp/bbdb-anniv.el
@@ -1,6 +1,6 @@
 ;;; bbdb-anniv.el --- get anniversaries from BBDB
 
-;; Copyright (C) 2011-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2011-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -34,6 +34,8 @@
 ;;
 ;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 (require 'diary-lib)
@@ -167,3 +169,5 @@ To enable this feature, put the followin
                      ""))))))))))
 
 (provide 'bbdb-anniv)
+
+;;; bbdb-anniv.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-com.el
+++ bbdb3-3.1.2/lisp/bbdb-com.el
@@ -1,7 +1,7 @@
 ;;; bbdb-com.el --- user-level commands of BBDB
 
 ;; Copyright (C) 1991, 1992, 1993 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -22,6 +22,8 @@
 ;;; This file contains most of the user-level interactive commands for BBDB.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'mailabbrev)
 
@@ -62,23 +64,33 @@ If FULL is non-nil, assume that RECORDS
         (if (vectorp records) (list records) records))))
 
 ;; Note about BBDB prefix commands:
-;; - `bbdb-do-all-records' behaves more like a proper prefix command
-;;   in the sense that it must immediately precede the main command.
-;;   YET: a simple M-x makes the prefix go away...
-;; - `bbdb-append-display' and `bbdb-search-invert' are fake prefix
-;;   commands. They need not precede the main commands.
-;;   Also, `bbdb-append-display' can act on multiple commands.
-;; FIXME: Make this more uniform and robust.
+;; `bbdb-do-all-records', `bbdb-append-display' and `bbdb-search-invert'
+;; are fake prefix commands. They need not precede the main commands.
+;; Also, `bbdb-append-display' can act on multiple commands.
+
+(defun bbdb-prefix-message ()
+  "Display a message about selected BBDB prefix commands."
+  (let ((msg (bbdb-concat " " (elt bbdb-modeline-info 1)
+                          (elt bbdb-modeline-info 3)
+                          (elt bbdb-modeline-info 5))))
+    (unless (string= "" msg) (message "%s" msg))))
 
 ;;;###autoload
-(defun bbdb-do-all-records ()
+(defun bbdb-do-all-records (&optional arg)
   "Command prefix for operating on all records currently displayed.
+With prefix ARG a positive number, operate on all records.
+With prefix ARG a negative number, operate on current record only.
 This only works for certain commands."
-  (interactive)
-  (message (substitute-command-keys
-            "\\<bbdb-mode-map>\\[bbdb-do-all-records]"))
-  (setq prefix-arg current-prefix-arg
-        last-command this-command))
+  (interactive "P")
+  (setq bbdb-do-all-records
+        (or (and (numberp arg) (< 0 arg))
+            (and (not (numberp arg)) (not bbdb-do-all-records))))
+  (aset bbdb-modeline-info 4 (if bbdb-do-all-records "all"))
+  (aset bbdb-modeline-info 5
+        (if bbdb-do-all-records
+            (substitute-command-keys
+             "\\<bbdb-mode-map>\\[bbdb-do-all-records]")))
+  (bbdb-prefix-message))
 
 ;;;###autoload
 (defun bbdb-do-records (&optional full)
@@ -87,8 +99,12 @@ Normally this list includes only the cur
 It includes all currently displayed records if the command prefix \
 \\<bbdb-mode-map>\\[bbdb-do-all-records] is used.
 If FULL is non-nil, the list of records includes display information."
-  (if (eq last-command 'bbdb-do-all-records)
-      (if full bbdb-records (mapcar 'car bbdb-records))
+  (if bbdb-do-all-records
+      (progn
+        (setq bbdb-do-all-records nil)
+        (aset bbdb-modeline-info 4 nil)
+        (aset bbdb-modeline-info 5 nil)
+        (if full bbdb-records (mapcar 'car bbdb-records)))
     (list (bbdb-current-record full))))
 
 ;;;###autoload
@@ -107,7 +123,8 @@ If FULL is non-nil, the list of records
            (aset bbdb-modeline-info 0
                  (format "(add %dx)" bbdb-append-display)))
           ((not bbdb-append-display)
-           (aset bbdb-modeline-info 0 nil)))
+           (aset bbdb-modeline-info 0 nil)
+           (aset bbdb-modeline-info 1 nil)))
     job))
 
 ;;;###autoload
@@ -128,13 +145,11 @@ With ARG a negative number do not append
               ((eq t bbdb-append-display) "Add")
               (bbdb-append-display "add")
               (t nil)))
-  (aset bbdb-modeline-info 2
+  (aset bbdb-modeline-info 1
         (if bbdb-append-display
             (substitute-command-keys
              "\\<bbdb-mode-map>\\[bbdb-append-display]")))
-  (let ((msg (bbdb-concat " " (elt bbdb-modeline-info 2)
-                          (elt bbdb-modeline-info 3))))
-    (unless (string= "" msg) (message "%s" msg))))
+  (bbdb-prefix-message))
 
 (defsubst bbdb-layout-prefix ()
   "Set the LAYOUT arg interactively using the prefix arg."
@@ -147,7 +162,7 @@ With ARG a negative number do not append
 To set it again, use command `bbdb-search-invert'."
   (let ((result bbdb-search-invert))
     (setq bbdb-search-invert nil)
-    (aset bbdb-modeline-info 1 nil)
+    (aset bbdb-modeline-info 2 nil)
     (aset bbdb-modeline-info 3 nil)
     result))
 
@@ -157,18 +172,14 @@ To set it again, use command `bbdb-searc
 With prefix ARG a positive number, invert next search.
 With prefix ARG a negative number, do not invert next search."
   (interactive "P")
-  (if (setq bbdb-search-invert
-            (or (and (numberp arg) (< 0 arg))
-                (and (not (numberp arg)) (not bbdb-search-invert))))
-      (progn
-        (aset bbdb-modeline-info 1 "inv")
-        (aset bbdb-modeline-info 3
-              (substitute-command-keys
-               "\\<bbdb-mode-map>\\[bbdb-search-invert]")))
-    (aset bbdb-modeline-info 1 nil)
-    (aset bbdb-modeline-info 3 nil))
-  (message "%s" (bbdb-concat " " (elt bbdb-modeline-info 2)
-                             (elt bbdb-modeline-info 3))))
+  (setq bbdb-search-invert
+        (or (and (numberp arg) (< 0 arg))
+            (and (not (numberp arg)) (not bbdb-search-invert))))
+  (aset bbdb-modeline-info 2 (if bbdb-search-invert "inv"))
+  (aset bbdb-modeline-info 3 (if bbdb-search-invert
+                                 (substitute-command-keys
+                                  "\\<bbdb-mode-map>\\[bbdb-search-invert]")))
+  (bbdb-prefix-message))
 
 (defmacro bbdb-search (records &optional name-re org-re mail-re xfield-re
                                phone-re address-re)
@@ -257,13 +268,14 @@ but not allowing for regexps."
         (push `(cond ((stringp ,xfield-re)
                       ;; check xfield `bbdb-default-xfield'
                       (string-match ,xfield-re
-                                    (or (bbdb-record-xfield record bbdb-default-xfield) "")))
+                                    (or (bbdb-record-xfield-string
+                                         record bbdb-default-xfield) "")))
                      ((eq (car ,xfield-re) '*)
                       ;; check all xfields
                       (let ((labels bbdb-xfield-label-list) done tmp)
                         (if (bbdb-record-xfields record)
                             (while (and (not done) labels)
-                              (setq tmp (bbdb-record-xfield record (car labels))
+                              (setq tmp (bbdb-record-xfield-string record (car labels))
                                     done (and tmp (string-match (cdr ,xfield-re)
                                                                 tmp))
                                     labels (cdr labels)))
@@ -273,7 +285,7 @@ but not allowing for regexps."
                         done))
                      (t ; check one field
                       (string-match (cdr ,xfield-re)
-                                    (or (bbdb-record-xfield
+                                    (or (bbdb-record-xfield-string
                                          record (car ,xfield-re)) ""))))
               clauses))
     `(let ((case-fold-search bbdb-case-fold-search)
@@ -352,8 +364,9 @@ in either the name(s), organization, add
 
 ;;;###autoload
 (defun bbdb-search-changed (&optional layout)
-  "Display all records in the bbdb database which have changed since
-the database was last saved."
+  ;; FIXME: "changes" in BBDB lingo are often called "modifications"
+  ;; in Emacs lingo
+  "Display records which have been changed since BBDB was last saved."
   (interactive (list (bbdb-layout-prefix)))
   (if (bbdb-search-invert-p)
       (let (unchanged-records)
@@ -485,7 +498,7 @@ The search results are displayed in the
 (defun bbdb-fix-records (records)
   "Fix broken RECORDS.
 Interactively, use BBDB prefix \
-\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records',"
+\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records'."
   (interactive (list (bbdb-do-records)))
   (bbdb-editable)
   (dolist (record (bbdb-record-list records))
@@ -499,6 +512,16 @@ Interactively, use BBDB prefix \
     (bbdb-record-set-field record 'mail (bbdb-record-mail record))
     (bbdb-change-record record)))
 
+(defun bbdb-touch-records (records)
+  "Touch RECORDS by calling `bbdb-change-hook' unconditionally.
+Interactively, use BBDB prefix \
+\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records'."
+  (interactive (list (bbdb-do-records)))
+  (bbdb-editable)
+  (let ((bbdb-update-unchanged-records t))
+    (dolist (record (bbdb-record-list records))
+      (bbdb-change-record record))))
+
 ;;; Time-based functions
 
 (defmacro bbdb-compare-records (cmpval label compare)
@@ -862,20 +885,20 @@ A non-nil prefix arg is passed on to `bb
          (bbdb-record-set-field record 'aka value))
         ;; xfields
         ((assq field (bbdb-record-xfields record))
-         (error "xfield \"%s\" already exists" field))
+         (error "Xfield \"%s\" already exists" field))
         (t
          (bbdb-record-set-xfield record field value)))
-  (let (bbdb-layout)
-    (bbdb-change-record record)))
+  (unless (bbdb-change-record record)
+    (message "Record unchanged")))
 
 (defun bbdb-read-field (record field &optional flag)
-  "For RECORD read FIELD interactively.
-If inserting a new phone number, the phone number style
-is controlled via `bbdb-phone-style'.  A non-nil FLAG inverts the style,
-
-If inserting a new mail address lacking a domain, BBDB appends
-`bbdb-default-domain' if this variable non-nil.  With non-nil FLAG
-\(or `bbdb-default-domain' being nil) do not alter the mail address."
+  "For RECORD read new FIELD interactively.
+- The phone number style is controlled via `bbdb-phone-style'.
+  A prefix FLAG inverts the style,
+- If a mail address lacks a domain, append `bbdb-default-domain'
+  if this variable non-nil.  With prefix FLAG do not alter the mail address.
+- The value of an xfield is a string.  With prefix FLAG the value may be
+  any lisp object."
   (let* ((init-f (intern-soft (concat "bbdb-init-" (symbol-name field))))
          (init (if (and init-f (functionp init-f))
                    (funcall init-f record))))
@@ -918,7 +941,7 @@ If inserting a new mail address lacking
                (y-or-n-p
                 (format "\"%s\" is an unknown field name.  Define it? " field))
                (error "Aborted"))
-           (bbdb-read-xfield field init)))))
+           (bbdb-read-xfield field init flag)))))
 
 ;;;###autoload
 (defun bbdb-edit-field (record field &optional value flag)
@@ -927,7 +950,10 @@ If point is in the middle of a multi-lin
 then the entire field is edited, not just the current line.
 For editing phone numbers or addresses, VALUE must be the phone number
 or address that gets edited. An error is thrown when attempting to edit
-a phone number or address with VALUE being nil."
+a phone number or address with VALUE being nil.
+
+- The value of an xfield is a string.  With prefix FLAG the value may be
+  any lisp object."
   (interactive
    (save-excursion
      (bbdb-editable)
@@ -954,7 +980,7 @@ a phone number or address with VALUE bei
              record 'name
              (bbdb-read-name
               (if flag
-                  ;; Here we try to obey the name-format field for
+                  ;; Here we try to obey the name-format xfield for
                   ;; editing the name field.  Is this useful?  Or is this
                   ;; irritating overkill and we better obey consistently
                   ;; `bbdb-read-name-format'?
@@ -985,8 +1011,9 @@ a phone number or address with VALUE bei
           (t ; xfield
            (bbdb-record-set-xfield
             record field
-            (bbdb-read-xfield field (bbdb-record-xfield record field)))))
-    (bbdb-change-record record bbdb-need-to-sort)))
+            (bbdb-read-xfield field (bbdb-record-xfield record field) flag))))
+    (unless (bbdb-change-record record bbdb-need-to-sort)
+      (message "Record unchanged"))))
 
 (defun bbdb-edit-foo (record field &optional nvalue)
   "For RECORD edit some FIELD (mostly interactively).
@@ -1071,13 +1098,16 @@ of FIELD is the cdr of this variable."
       (bbdb-insert-field record field
                          (bbdb-read-field record field)))))
 
-(defun bbdb-read-xfield (field &optional init)
+(defun bbdb-read-xfield (field &optional init sexp)
   "Read xfield FIELD with optional INIT.
 This calls bbdb-read-xfield-FIELD if it exists."
   (let ((read-fun (intern-soft (format "bbdb-read-xfield-%s" field))))
-    (if (fboundp read-fun)
-        (funcall read-fun init)
-      (bbdb-read-string (format "%s: " field) init))))
+    (cond ((fboundp read-fun)
+           (funcall read-fun init))
+          ((and (not sexp) (string-or-null-p init))
+           (bbdb-read-string (format "%s: " field) init))
+          (t (read-minibuffer (format "%s (sexp): " field)
+                              (prin1-to-string init))))))
 
 (defun bbdb-read-organization (&optional init)
   "Read organization."
@@ -1342,8 +1372,11 @@ If prefix NOPROMPT is non-nil, do not co
           (cond ((memq type '(phone address))
                  (bbdb-record-set-field
                   record type
-                  (delq (nth 1 field)
-                        (bbdb-record-field record type))))
+                  ;; We use `delete' which deletes all phone and address
+                  ;; fields equal to the current one.  This works for
+                  ;; multiple records.
+                  (delete (nth 1 field)
+                          (bbdb-record-field record type))))
                 ((memq type '(affix organization mail aka))
                  (bbdb-record-set-field record type nil))
                 ((eq type 'xfields)
@@ -1459,12 +1492,15 @@ Interactively, use BBDB prefix \
   "Remove current record from the display without deleting it from BBDB.
 With prefix N, omit the next N records.  If negative, omit backwards."
   (interactive "p")
-  (while (not (= n 0))
-    (if (< n 0) (bbdb-prev-record 1))
-    (let ((record (bbdb-current-record t)))
-      (bbdb-redisplay-record (car record) t)
-      (setq bbdb-records (delete record bbdb-records)))
-    (setq n (if (> n 0) (1- n) (1+ n)))))
+  (let ((num  (get-text-property (if (and (not (bobp)) (eobp))
+                                     (1- (point)) (point))
+                                 'bbdb-record-number)))
+    (if (> n 0)
+        (setq n (min n (- (length bbdb-records) num)))
+      (setq n (min (- n) num))
+      (bbdb-prev-record n))
+    (dotimes (i n)
+      (bbdb-redisplay-record (bbdb-current-record) t))))
 
 ;;; Fixing up bogus records
 
@@ -2369,41 +2405,61 @@ Rebuilding the aliases is enforced if pr
         (add-to-list 'result alias)))))
 
 ;;;###autoload
-(defun bbdb-add-mail-alias (record &optional alias delete)
-  "Add ALIAS to RECORD.
-If pefix DELETE is non-nil, remove ALIAS from RECORD."
-  (interactive
-   (let* ((_ (bbdb-editable))
-          (record (bbdb-current-record))
-          (init-f (concat "bbdb-init-" (symbol-name bbdb-mail-alias-field)))
-          (init (if (and (setq init-f (intern-soft init-f))
-                         (functionp init-f))
-                    (funcall init-f record))))
-     (list record
-           (completing-read
-            (format "%s mail alias: "
-                    (if current-prefix-arg "Remove" "Add"))
-            (if current-prefix-arg
-                (or (bbdb-record-xfield-split record bbdb-mail-alias-field)
-                    (error "Record has no alias"))
-              (bbdb-get-mail-aliases))
-            nil nil init) current-prefix-arg)))
-  (setq alias (bbdb-string-trim alias))
-  (unless (string= "" alias)
-    (let ((aliases (bbdb-record-xfield-split record bbdb-mail-alias-field)))
-      (if delete
-          (setq aliases (delete alias aliases))
-        ;; Add alias only if it is not there yet
-        (add-to-list 'aliases alias))
-      (setq aliases (bbdb-concat bbdb-mail-alias-field aliases))
-      (bbdb-record-set-xfield record bbdb-mail-alias-field aliases))
-    (bbdb-change-record record)
-    ;; Rebuilt mail aliases
-    (setq bbdb-mail-aliases-need-rebuilt
+(defsubst bbdb-mail-alias-list (alias)
+  (if (stringp alias)
+      (bbdb-split bbdb-mail-alias-field alias)
+    alias))
+
+(defun bbdb-add-mail-alias (records &optional alias delete)
+  "Add ALIAS to RECORDS.
+If prefix DELETE is non-nil, remove ALIAS from RECORDS.
+Interactively, use BBDB prefix \
+\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records'.
+Arg ALIAS is ignored if list RECORDS contains more than one record.
+Instead read ALIAS interactively for each record in RECORDS.
+If the function `bbdb-init-mail-alias' is defined, it is called with
+one arg RECORD to define the default value for ALIAS of RECORD."
+  (interactive (list (bbdb-do-records) nil current-prefix-arg))
+  (bbdb-editable)
+  (setq records (bbdb-record-list records))
+  (if (< 1 (length records)) (setq alias nil))
+  (let* ((tmp (intern-soft
+               (concat "bbdb-init-" (symbol-name bbdb-mail-alias-field))))
+         (init-f (if (functionp tmp) tmp)))
+    (dolist (record records)
+      (let ((r-a-list (bbdb-record-xfield-split record bbdb-mail-alias-field))
+            (alias alias)
+            a-list)
+        (if alias
+            (setq a-list (bbdb-mail-alias-list alias))
+          (when init-f
+            (setq a-list (bbdb-mail-alias-list (funcall init-f record))
+                  alias (if a-list (bbdb-concat bbdb-mail-alias-field a-list))))
+          (let ((crm-separator
+                 (concat "[ \t\n]*"
+                         (cadr (assq bbdb-mail-alias-field bbdb-separator-alist))
+                         "[ \t\n]*"))
+                (crm-local-completion-map bbdb-crm-local-completion-map)
+                (prompt (format "%s mail alias:%s " (if delete "Remove" "Add")
+                                (if alias (format " (default %s)" alias) "")))
+                (collection (if delete
+                                (or r-a-list (error "Record has no alias"))
+                              (bbdb-get-mail-aliases))))
+            (setq a-list (if (string< "24.3" (substring emacs-version 0 4))
+                             (completing-read-multiple prompt collection nil
+                                                       delete nil nil alias)
+                          (bbdb-split bbdb-mail-alias-field
+                                      (completing-read prompt collection nil
+                                                       delete nil nil alias))))))
+        (dolist (a a-list)
           (if delete
-              'deleted
-            (if (bbdb-record-mail record)
-                'new)))))
+              (setq r-a-list (delete a r-a-list))
+            ;; Add alias only if it is not there yet
+            (add-to-list 'r-a-list a)))
+        ;; This also handles `bbdb-mail-aliases-need-rebuilt'
+        (bbdb-record-set-xfield record bbdb-mail-alias-field
+                                (bbdb-concat bbdb-mail-alias-field r-a-list))
+        (bbdb-change-record record)))))
 
 ;;; Dialing numbers from BBDB
 
@@ -2499,11 +2555,11 @@ Default is the first URL."
 
 ;;;###autoload
 (defun bbdb-copy-records-as-kill (records)
-  "Copy displayed RECORDS to kill ring.
+  "Copy RECORDS to kill ring.
 Interactively, use BBDB prefix \
 \\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records'."
   (interactive (list (bbdb-do-records t)))
-  (let (drec marker)
+  (let (drec)
     (dolist (record (bbdb-record-list records t))
       (push (buffer-substring (nth 2 record)
                               (or (nth 2 (car (cdr (memq record bbdb-records))))
@@ -2513,6 +2569,56 @@ Interactively, use BBDB prefix \
                "[ \t\n]*\\'" "\n"
                (mapconcat 'identity (nreverse drec) "")))))
 
+;;;###autoload
+(defun bbdb-copy-fields-as-kill (records field &optional num)
+  "For RECORDS copy values of FIELD at point to kill ring.
+If FIELD is an address or phone with a label, copy only field values
+with the same label.  With numeric prefix NUM, if the value of FIELD
+is a list, copy only the NUMth list element.
+Interactively, use BBDB prefix \
+\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records'."
+  (interactive
+   (list (bbdb-do-records t) (bbdb-current-field)
+         (and current-prefix-arg
+              (prefix-numeric-value current-prefix-arg))))
+  (unless field (error "Not a field"))
+  (let* ((type (if (eq (car field) 'xfields)
+                   (car (nth 1 field))
+                 (car field)))
+         (label (if (memq type '(phone address))
+                    (aref (cadr field) 0)))
+	 (ident (and (< 1 (length records))
+                     (not (eq type 'name))))
+	 val-list)
+    (dolist (record (bbdb-record-list records))
+      (let ((raw-val (bbdb-record-field (car record) type))
+            value)
+        (if raw-val
+            (cond ((eq type 'phone)
+                   (dolist (elt raw-val)
+                     (if (equal label (aref elt 0))
+                         (push (bbdb-phone-string elt) value)))
+                   (setq value (bbdb-concat 'phone (nreverse value))))
+                  ((eq type 'address)
+                   (dolist (elt raw-val)
+                     (if (equal label (aref elt 0))
+                         (push (bbdb-format-address
+                                elt (if (eq (nth 1 record) 'one-line) 3 2))
+                               value)))
+                   (setq value (bbdb-concat 'address (nreverse value))))
+                  ((consp raw-val)
+                   (setq value (if num (nth num raw-val)
+                                 (bbdb-concat type raw-val))))
+                  (t (setq value raw-val))))
+        (if value
+            (push (if ident
+                      (bbdb-concat 'name-field
+                                   (bbdb-record-name (car record)) value)
+                    value) val-list))))
+    (let ((str (bbdb-concat 'record (nreverse val-list))))
+      (kill-new str)
+      (message "%s" str))))
+
 ;;; Help and documentation
 
 ;;;###autoload
@@ -2531,3 +2637,5 @@ mode help: \\[describe-mode]; \
 info: \\[bbdb-info]")))
 
 (provide 'bbdb-com)
+
+;;; bbdb-com.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-gnus.el
+++ bbdb3-3.1.2/lisp/bbdb-gnus.el
@@ -1,7 +1,7 @@
 ;;; bbdb-gnus.el --- BBDB interface to Gnus
 
 ;; Copyright (C) 1991, 1992, 1993 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -22,54 +22,14 @@
 ;;; This file contains the BBDB interface to Gnus.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 (require 'bbdb-mua)
 (require 'gnus)
 
-(defcustom bbdb/gnus-update-records-p
-  (lambda () (let ((bbdb-update-records-p 'query))
-               (bbdb-select-message)))
-  "How `bbdb-mua-update-records' processes mail addresses in Gnus.
-This Gnus-specific variable is normally not used.  It is a fallback
-if the generic (MUA-independent) variables `bbdb-mua-auto-update-p',
-`bbdb-update-records-p' or `bbdb-mua-update-interactive-p' result
-in a value of nil for the arg UPDATE-P of `bbdb-update-records'.
-
-Allowed values are:
- nil          Do nothing.
- search       Search for existing records.
- update       Search for existing records, update if necessary.
- query        Update existing records or query for creating new ones.
- create or t  Update existing records or create new ones.
- a function   This functions will be called with no arguments.
-                It should return one of the above values."
-  :group 'bbdb-mua-gnus
-  :type '(choice (const :tag "do nothing" nil)
-                 (const :tag "search for existing records"
-                        (lambda () (let ((bbdb-update-records-p 'search))
-                                     (bbdb-select-message))))
-                 (const :tag "update existing records"
-                        (lambda () (let ((bbdb-update-records-p 'update))
-                                     (bbdb-select-message))))
-                 (const :tag "query annotation of all messages"
-                        (lambda () (let ((bbdb-update-records-p 'query))
-                                     (bbdb-select-message))))
-                 (const :tag "annotate (query) only new messages"
-                        (if (equal "" (gnus-summary-article-mark
-                                       (gnus-summary-article-number)))
-                            (bbdb-select-message) 'search))
-                 (const :tag "annotate all messages"
-                        (lambda () (let ((bbdb-update-records-p 'create))
-                                     (bbdb-select-message))))
-                 (const :tag "accept messages" bbdb-accept-message)
-                 (const :tag "ignore messages" bbdb-ignore-message)
-                 (const :tag "select messages" bbdb-select-message)
-                 (sexp  :tag "user defined")))
-
-;;
 ;; Scoring
-;;
 
 (defcustom bbdb/gnus-score-field 'gnus-score
   "This variable contains the name of the BBDB field which should be
@@ -434,3 +394,5 @@ Do not call this in your init file.  Use
   ;;    'bbdb/gnus-score))
 
 (provide 'bbdb-gnus)
+
+;;; bbdb-gnus.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-ispell.el
+++ bbdb3-3.1.2/lisp/bbdb-ispell.el
@@ -1,7 +1,7 @@
 ;;; bbdb-ispell.el --- export names from BBDB to personal ispell dictionaries
 
 ;; Copyright (C) 2011-2013 Ivan Kanis <ivan.kanis@googlemail.com>
-;; Copyright (C) 2011-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2011-2015 Roland Winkler <winkler@gnu.org>
 
 ;; Author: Ivan Kanis <ivan.kanis@googlemail.com>
 
@@ -124,3 +124,5 @@ Allowed elements are as in the return va
         ((sequencep field) (mapc 'bbdb-ispell-collect-words field))))
 
 (provide 'bbdb-ispell)
+
+;;; bbdb-ispell.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-message.el
+++ bbdb3-3.1.2/lisp/bbdb-message.el
@@ -1,6 +1,6 @@
 ;;; bbdb-message.el --- BBDB interface to Mail Composition Packages.
 
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -21,48 +21,12 @@
 ;;; This file contains the BBDB interface to Mail Composition Packages.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'message)
 (require 'sendmail)
 
-(defcustom bbdb/message-update-records-p 'bbdb-select-message
-  "How `bbdb-mua-update-records' processes mail addresses in outgoing messages.
-This MUA-specific variable is normally not used.  It is a fallback
-if the generic (MUA-independent) variables `bbdb-mua-auto-update-p',
-`bbdb-update-records-p' or `bbdb-mua-update-interactive-p' result
-in a value of nil for the arg UPDATE-P of `bbdb-update-records'.
-
-Allowed values are:
- nil          Do nothing.
- search       Search for existing records.
- update       Search for existing records, update if necessary.
- query        Update existing records or query for creating new ones.
- create or t  Update existing records or create new ones.
- a function   This functions will be called with no arguments.
-                It should return one of the above values."
-  :group 'bbdb-mua-message
-  :type '(choice (const :tag "do nothing"
-                        nil)
-                 (const :tag "search for existing records"
-                        (lambda () (let ((bbdb-update-records-p 'search))
-                                     (bbdb-select-message))))
-                 (const :tag "update existing records"
-                        (lambda () (let ((bbdb-update-records-p 'update))
-                                     (bbdb-select-message))))
-                 (const :tag "query annotation of all messages"
-                        (lambda () (let ((bbdb-update-records-p 'query))
-                                     (bbdb-select-message))))
-                 (const :tag "annotate all messages"
-                        (lambda () (let ((bbdb-update-records-p 'create))
-                                     (bbdb-select-message))))
-                 (const :tag "accept messages" bbdb-accept-message)
-                 (const :tag "ignore messages" bbdb-ignore-message)
-                 (const :tag "select messages" bbdb-select-message)
-                 (sexp  :tag "user defined function")))
-(defvaralias 'bbdb/mail-update-records-p 'bbdb/message-update-records-p)
-
-
-
 ;;;###autoload
 (defun bbdb-insinuate-message ()
   "Hook BBDB into Message Mode.
@@ -88,3 +52,5 @@ Do not call this in your init file.  Use
       (define-key mail-mode-map "\M-\t" 'bbdb-complete-mail)))
 
 (provide 'bbdb-message)
+
+;;; bbdb-message.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-mhe.el
+++ bbdb3-3.1.2/lisp/bbdb-mhe.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 1991 Todd Kaufmann <toad@cs.cmu.edu>
 ;; Modified: 28-Jul-94 by Fritz Knabe <knabe@ecrc.de>
 ;;                        Jack Repenning <jackr@dblues.wpd.sgi.com>
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -24,6 +24,8 @@
 ;;; This file contains the BBDB interface to mh-e.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 (require 'bbdb-mua)
@@ -32,46 +34,6 @@
     (require 'mh-comp))              ; For mh-e 4.x
 (require 'advice)
 
-(defcustom bbdb/mh-update-records-p
-  (lambda () (let ((bbdb-update-records-p 'query))
-               (bbdb-select-message)))
-  "How `bbdb-mua-update-records' processes mail addresses in MH-E.
-This MH-E-specific variable is normally not used.  It is a fallback
-if the generic (MUA-independent) variables `bbdb-mua-auto-update-p',
-`bbdb-update-records-p' or `bbdb-mua-update-interactive-p' result
-in a value of nil for the arg UPDATE-P of `bbdb-update-records'.
-
-Allowed values are:
- nil          Do nothing.
- search       Search for existing records.
- update       Search for existing records, update if necessary.
- query        Update existing records or query for creating new ones.
- create or t  Update existing records or create new ones.
- a function   This functions will be called with no arguments.
-                It should return one of the above values."
-  :group 'bbdb-mua-gnus
-  :type '(choice (const :tag "do nothing" nil)
-                 (const :tag "search for existing records"
-                        (lambda () (let ((bbdb-update-records-p 'search))
-                                     (bbdb-select-message))))
-                 (const :tag "update existing records"
-                        (lambda () (let ((bbdb-update-records-p 'update))
-                                     (bbdb-select-message))))
-                 (const :tag "query annotation of all messages"
-                        (lambda () (let ((bbdb-update-records-p 'query))
-                                     (bbdb-select-message))))
-                 (const :tag "annotate (query) only new messages"
-                        (if (equal "" (gnus-summary-article-mark
-                                       (gnus-summary-article-number)))
-                            (bbdb-select-message) 'search))
-                 (const :tag "annotate all messages"
-                        (lambda () (let ((bbdb-update-records-p 'create))
-                                     (bbdb-select-message))))
-                 (const :tag "accept messages" bbdb-accept-message)
-                 (const :tag "ignore messages" bbdb-ignore-message)
-                 (const :tag "select messages" bbdb-select-message)
-                 (sexp  :tag "user defined")))
-
 ;; A simplified `mail-fetch-field'.  We could use instead (like rmail):
 ;; (mail-header (intern-soft (downcase header)) (mail-header-extract))
 (defun bbdb/mh-header (header)
@@ -136,3 +98,5 @@ Do not call this in your init file.  Use
       (define-key mh-letter-mode-map "\e\t" 'bbdb-complete-mail)))
 
 (provide 'bbdb-mhe)
+
+;;; bbdb-mhe.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-migrate.el
+++ bbdb3-3.1.2/lisp/bbdb-migrate.el
@@ -1,7 +1,7 @@
 ;;; bbdb-migrate.el --- migration functions for BBDB
 
 ;; Copyright (C) 1991, 1992, 1993, 1994 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -22,6 +22,8 @@
 ;;; This file contains the migration functions for BBDB.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 
 ;;; Migrating the BBDB
@@ -290,3 +292,5 @@ for outdated BBDB variables that are set
     list))
 
 (provide 'bbdb-migrate)
+
+;;; bbdb-migrate.el ends here
--- /dev/null
+++ bbdb3-3.1.2/lisp/bbdb-mu4e.el
@@ -0,0 +1,43 @@
+;;; bbdb-mu4e.el --- BBDB interface to mu4e
+
+;; Copyright (C) 2015 David Sterratt <david@sterratt.me.uk>
+;; Copyright (C) 2015 Roland Winkler <winkler@gnu.org>
+
+;; This file is part of the Insidious Big Brother Database (aka BBDB),
+
+;; BBDB 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.
+
+;; BBDB 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 BBDB.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;; This file contains the BBDB interface to mu4e.
+;;; See the BBDB info manual for documentation.
+
+;;; Code:
+
+(require 'bbdb)
+(require 'mu4e-view)
+
+;;;###autoload
+(defun bbdb-insinuate-mu4e ()
+  "Hook BBDB into mu4e.
+Do not call this in your init file.  Use `bbdb-initialize'."
+  ;; Tackle headers later
+  ;; (define-key mu4e-headers-mode-map ":" 'bbdb-mua-display-sender)
+  ;; (define-key mu4e-headers-mode-map ";" 'bbdb-mua-edit-field-sender)
+  ;; Do we need keybindings for more commands?  Suggestions welcome.
+  (define-key mu4e-view-mode-map ":" 'bbdb-mua-display-sender)
+  (define-key mu4e-view-mode-map ";" 'bbdb-mua-edit-field-sender))
+
+(provide 'bbdb-mu4e)
+
+;;; bbdb-mu4e.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-mua.el
+++ bbdb3-3.1.2/lisp/bbdb-mua.el
@@ -1,7 +1,7 @@
 ;;; bbdb-mua.el --- various MUA functionality for BBDB
 
 ;; Copyright (C) 1991, 1992, 1993 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -33,6 +33,8 @@
 ;;;
 ;;; Read the docstrings; read the texinfo file.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 
@@ -54,6 +56,8 @@
   (autoload 'mh-show "mh-show")
   (defvar mh-show-buffer)
 
+  (defvar mu4e~view-buffer-name)
+
   (autoload 'message-field-value "message")
   (autoload 'mail-decode-encoded-word-string "mail-parse"))
 
@@ -63,7 +67,8 @@
     (rmail rmail-mode rmail-summary-mode)
     (mh mhe-mode mhe-summary-mode mh-folder-mode)
     (message message-mode)
-    (mail mail-mode))
+    (mail mail-mode)
+    (mu4e mu4e-view-mode)) ; Tackle `mu4e-headers-mode' later
   "Alist of MUA modes supported by BBDB.
 Each element is of the form (MUA MODE MODE ...), where MODEs are used by MUA.")
 
@@ -75,6 +80,7 @@ Return values include
   vm        Viewmail
   mh        Emacs interface to the MH mail system (aka MH-E)
   message   Mail and News composition mode that goes with Gnus
+  mu4e      Mu4e
   mail      Emacs Mail Mode."
   (let ((mm-alist bbdb-mua-mode-alist)
         elt mua)
@@ -108,6 +114,7 @@ MIME encoded headers are decoded.  Retur
                     ((eq mua 'vm) (bbdb/vm-header header))
                     ((eq mua 'rmail) (bbdb/rmail-header header))
                     ((eq mua 'mh) (bbdb/mh-header header))
+                    ((eq mua 'mu4e) (message-field-value header))
                     ((memq mua '(message mail)) (message-field-value header))
                     (t (error "BBDB/%s: header function undefined" mua)))))
     (if val (mail-decode-encoded-word-string val))))
@@ -212,11 +219,9 @@ UPDATE-P may take the following values:
                 query for creation of a new record if the record does not exist.
  create or t  Search for existing records matching ADDRESS;
                 create a new record if it does not yet exist.
+ nil          Do nothing.
  a function   This functions will be called with no arguments.
                 It should return one of the above values.
- nil          Take the MUA-specific variable `bbdb/MUA-update-records-p'
-                which may take one of the above values.
-                If this still gives nil, `bbdb-update-records' returns nil.
 
 If SORT is non-nil, sort records according to `bbdb-record-lessp'.
 Ottherwise, the records are ordered according to ADDRESS-LIST.
@@ -228,19 +233,10 @@ Usually this function is called by the w
   ;; We resolve UPDATE-P repeatedly.  This is needed, for example,
   ;; with the chain `bbdb-mua-auto-update-p' -> `bbdb-select-message'
   ;; -> `bbdb-update-records-p'.
-  (let (done fallback)
-    (while (not done)
-      (cond ((and (functionp update-p)
-                  ;; Bad! `search' is a function in `cl-seq.el'.
-                  (not (eq update-p 'search)))
-             (setq update-p (funcall update-p)))
-            ((not (or update-p fallback))
-             ;; The fallback is applied at most once.
-             (setq update-p (symbol-value
-                             (intern-soft (format "bbdb/%s-update-records-p"
-                                                  (bbdb-mua))))
-                   fallback t))
-            ((setq done t)))))
+  (while (and (functionp update-p)
+              ;; Bad! `search' is a function in `cl-seq.el'.
+              (not (eq update-p 'search)))
+    (setq update-p (funcall update-p)))
   (cond ((eq t update-p)
          (setq update-p 'create))
         ((not (memq update-p '(search update query create nil)))
@@ -298,9 +294,8 @@ Usually this function is called by the w
 
     (unless bbdb-read-only
       (bbdb-editable)
-      (let ((bbdb-notice-hook-pending t))
-        (dolist (record records)
-          (run-hook-with-args 'bbdb-notice-record-hook record))))
+      (dolist (record records)
+        (run-hook-with-args 'bbdb-notice-record-hook record)))
 
     records))
 
@@ -566,8 +561,7 @@ Return the records matching ADDRESS or n
                         (message "noticed naked address \"%s\"" mail))))
                (bbdb-change-record record (eq change-p 'sort))))
 
-        (let ((bbdb-notice-hook-pending t))
-          (run-hook-with-args 'bbdb-notice-mail-hook record))
+        (run-hook-with-args 'bbdb-notice-mail-hook record)
         (push record new-records)))
 
     (nreverse new-records)))
@@ -603,7 +597,12 @@ If SORT is non-nil, sort records accordi
         (set-buffer rmail-buffer)
         (bbdb-update-records (bbdb-get-address-components header-class)
                              update-p sort))
-       ;; Message and Mail
+       ;; mu4e
+       ((eq mua 'mu4e)
+        (set-buffer mu4e~view-buffer-name)
+        (bbdb-update-records (bbdb-get-address-components header-class)
+                             update-p sort))
+      ;; Message and Mail
        ((memq mua '(message mail))
         (bbdb-update-records (bbdb-get-address-components header-class)
                              update-p sort))))))
@@ -620,10 +619,10 @@ If SORT is non-nil, sort records accordi
             (save-current-buffer
               (gnus-summary-select-article) ; sets buffer `gnus-summary-buffer'
               ,@body))
-           ((memq mua '(mail message rmail mh vm))
+           ((memq mua '(mail message rmail mh vm mu4e))
             (cond ((eq mua 'vm) (vm-follow-summary-cursor))
                   ((eq mua 'mh) (mh-show)))
-            ;; rmail, mail and message do not require any wrapper
+            ;; rmail, mail, message and mu4e do not require any wrapper
             ,@body))))
 
 (defun bbdb-mua-update-interactive-p ()
@@ -1052,3 +1051,5 @@ if this xfield is in the poster's record
         " "))))
 
 (provide 'bbdb-mua)
+
+;;; bbdb-mua.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-pgp.el
+++ bbdb3-3.1.2/lisp/bbdb-pgp.el
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 1997,1999 Kevin Davidson
 ;; Copyright (C) 2013 Gijs Hillenius
-;; Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -236,3 +236,5 @@ to call the command `bbdb-pgp' manually,
                          (setq ranked-actions nil)))))))))))
 
 (provide 'bbdb-pgp)
+
+;;; bbdb-pgp.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-print.el
+++ bbdb3-3.1.2/lisp/bbdb-print.el
@@ -1,7 +1,7 @@
-;;; bbdb-print.el -- for printing BBDB databases using TeX.
+;;; bbdb-print.el --- for printing BBDB databases using TeX.
 
 ;; Copyright (C) 1993 Boris Goldowsky
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; Authors: Boris Goldowsky <boris@cs.rochester.edu>
 ;;          Dirk Grunwald <grunwald@cs.colorado.edu>
@@ -81,14 +81,15 @@ See also `bbdb-print-require'."
 
 (defcustom bbdb-print-require '(or address phone)
   "What fields are required for printing a record.
-This is evaluated for each record, and the record will be printed only
-if it returns non-nil.  The symbols name, organization, mail, phone,
-address, and notes will be set to appropriate values when this is
-evaluated; they will be nil if the field does not exist or is elided.
+This is a lisp expression and a record will be printed only if the evaluation
+of this expression yields a non-nil value for this records.
+The symbols name, organization, mail, phone, address, and notes will be set
+to appropriate values when this is evaluated; they will be nil if the field
+does not exist or is elided.
 
 The value of this variable can be any lisp expression, but typically
-it will be used for a boolean combination of the field variables, as
-in the following examples:
+it will be a boolean combination of the field variables, as in
+the following examples:
 
   Print only people whose phone numbers are known:
     (setq bbdb-print-require 'phone)
@@ -345,7 +346,7 @@ of the printout, notably the variables `
   (interactive
    (list (bbdb-do-records)
          (read-file-name
-          (format "Print to file (default %s): "
+          (format "TeX file: (default %s) "
                   (abbreviate-file-name bbdb-print-file))
           (file-name-directory bbdb-print-file)
           bbdb-print-file)
@@ -397,7 +398,8 @@ of the printout, notably the variables `
             (bbdb-print-record record current-letter
                                       brief pofl n-phones n-addresses)))
     (insert bbdb-print-epilog)
-    (goto-char (point-min))))
+    (goto-char (point-min)))
+  (message "Process this file with TeX (not LaTeX)"))
 
 (defun bbdb-print-record (record current-letter
                                  brief pofl n-phones n-addresses)
@@ -514,13 +516,14 @@ The return value is the new CURRENT-LETT
       ;; xfields
       (dolist (xfield xfields)
         (when (bbdb-print-field-p (car xfield))
-          (if (eq 'notes (car xfield))
-              (insert (format "\\notes{%s}\n"
-                              (bbdb-print-tex-quote (cdr xfield))))
-            (insert (format "\\note{%s}{%s}\n"
-                            (bbdb-print-tex-quote (symbol-name (car xfield)))
-                            (bbdb-print-tex-quote (cdr xfield)))))))
-
+          ;; The value of the xfield may be a sexp.  Ideally, a sexp
+          ;; should be formatted by `pp-to-string' then printed verbatim.
+          (let ((value (bbdb-print-tex-quote (format "%s" (cdr xfield)))))
+            (insert (if (eq 'notes (car xfield))
+                        (format "\\notes{%s}\n" value)
+                      (format "\\note{%s}{%s}\n"
+                              (bbdb-print-tex-quote (symbol-name (car xfield)))
+                              value))))))
       ;; Mark end of the record.
       (insert "\\endrecord\n%\n")
       (setq current-letter first-letter)))
@@ -538,4 +541,4 @@ Omit-area-code is one of the allowed sym
 
 (provide 'bbdb-print)
 
-;;; bbdb-print ends here.
+;;; bbdb-print ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-rmail.el
+++ bbdb3-3.1.2/lisp/bbdb-rmail.el
@@ -1,7 +1,7 @@
 ;;; bbdb-rmail.el --- BBDB interface to Rmail
 
 ;; Copyright (C) 1991, 1992 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -22,6 +22,8 @@
 ;;; This file contains the BBDB interface to Rmail.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 (require 'bbdb-mua)
@@ -29,49 +31,6 @@
 (require 'rmailsum)
 (require 'mailheader)
 
-(defcustom bbdb/rmail-update-records-p
-  (lambda ()
-    (let ((bbdb-update-records-p
-           (if (bbdb/rmail-new-flag) 'query 'search)))
-      (bbdb-select-message)))
-  "How `bbdb-mua-update-records' processes mail addresses in Rmail.
-This Rmail-specific variable is normally not used.  It is a fallback
-if the generic (MUA-independent) variables `bbdb-mua-auto-update-p',
-`bbdb-update-records-p' or `bbdb-mua-update-interactive-p' result
-in a value of nil for the arg UPDATE-P of `bbdb-update-records'.
-
-Allowed values are:
- nil          Do nothing.
- search       Search for existing records.
- update       Search for existing records, update if necessary.
- query        Update existing records or query for creating new ones.
- create or t  Update existing records or create new ones.
- a function   This functions will be called with no arguments.
-                It should return one of the above values."
-  :group 'bbdb-mua-rmail
-  :type '(choice (const :tag "do nothing" nil)
-                 (const :tag "search for existing records"
-                        (lambda () (let ((bbdb-update-records-p 'search))
-                                     (bbdb-select-message))))
-                 (const :tag "update existing records"
-                        (lambda () (let ((bbdb-update-records-p 'update))
-                                     (bbdb-select-message))))
-                 (const :tag "query annotation of all messages"
-                        (lambda () (let ((bbdb-update-records-p 'query))
-                                     (bbdb-select-message))))
-                 (const :tag "annotate (query) only new messages"
-                        (lambda ()
-                          (let ((bbdb-update-records-p
-                                 (if (bbdb/rmail-new-flag) 'query 'search)))
-                            (bbdb-select-message))))
-                 (const :tag "annotate all messages"
-                        (lambda () (let ((bbdb-update-records-p 'create))
-                                     (bbdb-select-message))))
-                 (const :tag "accept messages" bbdb-accept-message)
-                 (const :tag "ignore messages" bbdb-ignore-message)
-                 (const :tag "select messages" bbdb-select-message)
-                 (sexp  :tag "user defined function")))
-
 (defun bbdb/rmail-new-flag ()
   "Returns t if the current message in buffer BUF is new."
   (rmail-message-labels-p rmail-current-message ", ?\\(unseen\\),"))
@@ -100,3 +59,5 @@ Do not call this in your init file.  Use
   (define-key rmail-summary-mode-map ";" 'bbdb-mua-edit-field-sender))
 
 (provide 'bbdb-rmail)
+
+;;; bbdb-rmail.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-sc.el
+++ bbdb3-3.1.2/lisp/bbdb-sc.el
@@ -1,7 +1,7 @@
 ;;; bbdb-sc.el --- BBDB interface to Supercite
 
 ;; Copyright (C) 1991, 1992 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -65,6 +65,8 @@
 ;;                                          (bbdb-sc-update-from)
 ;;                                          (setq sc-mail-headers-end (point))))))
 
+;;; Code:
+
 (require 'bbdb-com)
 (require 'bbdb-mua)
 (require 'supercite)
@@ -191,4 +193,4 @@ BBDB into Supercite."
 
 (provide 'bbdb-sc)
 
-;;; end of bbdb-sc.el
+;;; bbdb-sc.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-site.el.in
+++ bbdb3-3.1.2/lisp/bbdb-site.el.in
@@ -1,6 +1,6 @@
 ;;; bbdb-site.el.in --- site-specific variables for BBDB
 
-;; Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -33,3 +33,5 @@
   :type '(repeat (directory :tag "Directory")))
 
 (provide 'bbdb-site)
+
+;;; bbdb-site.el.in ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-snarf.el
+++ bbdb3-3.1.2/lisp/bbdb-snarf.el
@@ -1,7 +1,7 @@
-;;; bbdb-snarf.el -- convert free-form text to BBDB records
+;;; bbdb-snarf.el --- convert free-form text to BBDB records
 
 ;; Copyright (C) 1997 John Heidemann <johnh@isi.edu>
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -36,6 +36,8 @@
 ;;; its snarfing algorithms too simplistic to be useful in real life.
 ;;; How can this possibly be improved?  Suggestions welcome.
 
+;;; Code:
+
 (require 'bbdb-com)
 
 (defcustom bbdb-snarf-rule-alist
@@ -295,7 +297,7 @@ See `bbdb-snarf-rule-alist' for details.
 
 ;;;###autoload
 (defun bbdb-snarf (string &optional rule)
-  "Snarf a BBDB record in STRING using RULE.  Return this record.
+  "Snarf a BBDB record in STRING using RULE.  Display and return this record.
 Interactively, STRING is the current region.
 RULE defaults to `bbdb-snarf-rule-default'.
 See `bbdb-snarf-rule-alist' for details."
@@ -318,12 +320,11 @@ See `bbdb-snarf-rule-alist' for details.
                                          (bbdb-record-firstname record)
                                          (bbdb-record-lastname record))
                             (car (bbdb-record-mail record))))))
-      (if old-record
-          (bbdb-merge-records old-record record)
-        ;; create new record
-        (bbdb-change-record record t t)
-        (bbdb-display-records (list record))
-        record))))
+      ;; Install RECORD after searching for OLD-RECORD
+      (bbdb-change-record record t t)
+      (if old-record (bbdb-merge-records old-record record)))
+    (bbdb-display-records (list record))
+    record))
 
 ;; Some test cases
 ;;
@@ -355,3 +356,5 @@ See `bbdb-snarf-rule-alist' for details.
 ;; test@person.net
 
 (provide 'bbdb-snarf)
+
+;;; bbdb-snarf.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb-vm.el
+++ bbdb3-3.1.2/lisp/bbdb-vm.el
@@ -1,7 +1,7 @@
 ;;; bbdb-vm.el --- BBDB interface to VM
 
 ;; Copyright (C) 1991, 1992, 1993 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -22,6 +22,8 @@
 ;;; This file contains the BBDB interface to VM.
 ;;; See the BBDB info manual for documentation.
 
+;;; Code:
+
 (require 'bbdb)
 (require 'bbdb-com)
 (require 'bbdb-mua)
@@ -35,52 +37,6 @@
 (require 'vm-message)
 (require 'vm-misc)
 
-(defcustom bbdb/vm-update-records-p
-  (lambda ()
-    (let ((bbdb-update-records-p
-           (if (vm-new-flag (car vm-message-pointer))
-               'query 'search)))
-      (bbdb-select-message)))
-  "How `bbdb-mua-update-records' processes mail addresses in VM.
-This VM-specific variable is normally not used.  It is a fallback
-if the generic (MUA-independent) variables `bbdb-mua-auto-update-p',
-`bbdb-update-records-p' or `bbdb-mua-update-interactive-p' result
-in a value of nil for the arg UPDATE-P of `bbdb-update-records'.
-
-Allowed values are:
- nil          Do nothing.
- search       Search for existing records.
- update       Search for existing records, update if necessary.
- query        Update existing records or query for creating new ones.
- create or t  Update existing records or create new ones.
- a function   This functions will be called with no arguments.
-                It should return one of the above values."
-  :group 'bbdb-mua-vm
-  :type '(choice (const :tag "do nothing"
-                        nil)
-                 (const :tag "search for existing records"
-                        (lambda () (let ((bbdb-update-records-p 'search))
-                                     (bbdb-select-message))))
-                 (const :tag "update existing records"
-                        (lambda () (let ((bbdb-update-records-p 'update))
-                                     (bbdb-select-message))))
-                 (const :tag "query annotation of all messages"
-                        (lambda () (let ((bbdb-update-records-p 'query))
-                                     (bbdb-select-message))))
-                 (const :tag "annotate (query) only new messages"
-                        (lambda ()
-                          (let ((bbdb-update-records-p
-                                 (if (vm-new-flag (car vm-message-pointer))
-                                     'query 'search)))
-                            (bbdb-select-message))))
-                 (const :tag "annotate all messages"
-                        (lambda () (let ((bbdb-update-records-p 'create))
-                                     (bbdb-select-message))))
-                 (const :tag "accept messages" bbdb-accept-message)
-                 (const :tag "ignore messages" bbdb-ignore-message)
-                 (const :tag "select messages" bbdb-select-message)
-                 (sexp  :tag "user defined function")))
-
 (defun bbdb/vm-header (header)
   (save-current-buffer
     (vm-select-folder-buffer)
@@ -361,3 +317,5 @@ Do not call this in your init file.  Use
             (lambda (m) (bbdb-mua-summary-mark (vm-su-from m))))))
 
 (provide 'bbdb-vm)
+
+;;; bbdb-vm.el ends here
--- bbdb3-3.1.2.orig/lisp/bbdb.el
+++ bbdb3-3.1.2/lisp/bbdb.el
@@ -1,7 +1,7 @@
 ;;; bbdb.el --- core of BBDB
 
 ;; Copyright (C) 1991, 1992, 1993, 1994 Jamie Zawinski <jwz@netscape.com>.
-;; Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+;; Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 
 ;; This file is part of the Insidious Big Brother Database (aka BBDB),
 
@@ -33,6 +33,8 @@
 ;;; |  version of BBDB you have.                                            |
 ;;;  -----------------------------------------------------------------------
 
+;;; Code:
+
 (require 'timezone)
 (require 'bbdb-site)
 
@@ -533,7 +535,10 @@ This is used for fields which do not hav
   :type '(list regexp string))
 
 (defcustom bbdb-separator-alist
-  '((name-first-last "[ ,;]" " ") (name-last-first "[ ,;]" ", ")
+  '((record "\n\n" "\n\n") ; used by `bbdb-copy-fields-as-kill'
+    (name-first-last "[ ,;]" " ") (name-last-first "[ ,;]" ", ")
+    (name-field ":\n" ":\n") ; used by `bbdb-copy-fields-as-kill'
+    (phone "[,;]" ", ") (address ";\n" ";\n") ; ditto
     (organization "[,;]" ", ") (affix "[,;]"  ", ") (aka "[,;]" ", ")
     (mail "[,;]" ", ") (mail-alias "[,;]" ", ") (vm-folder "[,;]" ", ")
     (birthday "\n" "\n") (wedding "\n" "\n") (anniversary "\n" "\n")
@@ -1114,10 +1119,6 @@ to determine the header and class of the
 to `bbdb-message-headers'.  See `bbdb-auto-notes' for how to annotate records
 using `bbdb-update-records-address' and the headers of a mail message.
 
-The record need not have been modified for this hook to be called;
-use `bbdb-change-hook' for that.  `bbdb-change-hook' will NOT be called
-as a result of modifications you may make to the record inside this hook.
-
 If a message contains multiple mail addresses belonging to one BBDB record,
 this hook is run for each mail address.  Use `bbdb-notice-record-hook'
 if you want to notice each record only once per message."
@@ -1132,10 +1133,6 @@ contains multiple mail addresses belongi
 is nonetheless run only once.  Use `bbdb-notice-mail-hook' if you want to run
 a hook function for each mail address in a message.
 
-The record need not have been modified for this hook to be called;
-use `bbdb-change-hook' for that.  `bbdb-change-hook' will NOT be called
-as a result of modifications you may make to the record inside this hook.
-
 Hook is run with one argument, the record."
   :group 'bbdb-mua
   :type 'hook)
@@ -1637,7 +1634,7 @@ You really should not disable debugging.
            (repeat (vector string (repeat string) string string
                            string string)) ; address
            (repeat string) ; mail
-           (repeat (cons symbol string)) ; xfields
+           (repeat (cons symbol sexp)) ; xfields
            sexp) ; cache
   "Pseudo-code for the structure of a record.  Used by `bbdb-record-type'.")
 
@@ -1658,10 +1655,6 @@ You really should not disable debugging.
   "Bind this to t to quiet things down - do not set it.
 See also `bbdb-silent'.")
 
-(defvar bbdb-notice-hook-pending nil
-  "Bound to t if inside `bbdb-notice-mail-hook' or `bbdb-notice-record-hook'.
-Calls of `bbdb-change-hook' are suppressed when this is non-nil.")
-
 (defvar bbdb-init-forms
   '((gnus                       ; gnus 3.15 or newer
      (add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus))
@@ -1678,6 +1671,8 @@ Calls of `bbdb-change-hook' are suppress
             (add-hook 'mail-setup-hook 'bbdb-insinuate-mail)))
     (message                    ; the gnus mail user agent
      (add-hook 'message-setup-hook 'bbdb-insinuate-message))
+    (mu4e                       ; the mu4e user agent
+     (add-hook 'mu4e-main-mode-hook 'bbdb-insinuate-mu4e))
 
     (sc                         ; supercite
      (add-hook 'sc-load-hook 'bbdb-insinuate-sc))
@@ -1692,6 +1687,9 @@ Calls of `bbdb-change-hook' are suppress
 (defvar bbdb-search-invert nil
   "Bind this variable to t in order to invert the result of `bbdb-search'.")
 
+(defvar bbdb-do-all-records nil
+  "Controls the behavior of the command `bbdb-do-all-records'.")
+
 (defvar bbdb-append-display nil
   "Controls the behavior of the command `bbdb-append-display'.")
 
@@ -1745,12 +1743,19 @@ and mail.  In elisp lingo, this is reall
 (defvar bbdb-country-list nil
   "List of countries known to BBDB.")
 
-(defvar bbdb-modeline-info (make-vector 4 nil)
+(defvar bbdb-modeline-info (make-vector 6 nil)
   "Precalculated mode line info for BBDB commands.
-This is a vector [APPEND-M INVERT-M APPEND INVERT].
+This is a vector [APPEND-M APPEND INVERT-M INVERT ALL-M ALL].
 APPEND-M is the mode line info if `bbdb-append-display' is non-nil.
 INVERT-M is the mode line info if `bbdb-search-invert' is non-nil.
-APPEND and INVERT appear in the message area.")
+ALL-M is the mode line info if `bbdb-do-all-records' is non-nil.
+APPEND, INVERT, and ALL appear in the message area.")
+
+(defvar bbdb-update-unchanged-records nil
+  "If non-nil update unchanged records in the database.
+Normally calls of `bbdb-change-hook' and updating of a record are suppressed,
+if an editing command did not really change the record.  Bind this to t
+if you want to call `bbdb-change-hook' and update the record unconditionally.")
 
 ;;; Keymap
 (defvar bbdb-mode-map
@@ -1784,7 +1789,8 @@ APPEND and INVERT appear in the message
     (define-key km "?"          'bbdb-help)
     ;; (define-key km "q"       'quit-window) ; part of `special-mode' bindings
     (define-key km "\C-x\C-t"   'bbdb-transpose-fields)
-    (define-key km "C"          'bbdb-copy-records-as-kill)
+    (define-key km "Cr"         'bbdb-copy-records-as-kill)
+    (define-key km "Cf"         'bbdb-copy-fields-as-kill)
     (define-key km "u"          'bbdb-browse-url)
     ;; (define-key km "P"       'bbdb-print)
     (define-key km "="          'delete-other-windows)
@@ -1867,6 +1873,7 @@ This is a child of `special-mode-map'.")
      ["Dial phone number" bbdb-dial t]
      ["Browse URL" bbdb-browse-url t]
      ["Copy records as kill" bbdb-copy-records-as-kill t]
+     ["Copy fields as kill" bbdb-copy-fields-as-kill t]
      "--"
      ["Print records" bbdb-print t])
     ("Manipulate database"
@@ -1912,16 +1919,18 @@ ARGS are passed to `message'."
   (ding t)
   (apply 'message args))
 
-(defsubst bbdb-string-trim (string)
+(defun bbdb-string-trim (string &optional null)
   "Remove leading and trailing whitespace and all properties from STRING.
-If STRING is nil return an empty string."
+If STRING is nil return an empty string unless NULL is non-nil."
   (if (null string)
-      ""
+      (unless null "")
+    (setq string (substring-no-properties string))
     (if (string-match "\\`[ \t\n]+" string)
-        (setq string (substring string (match-end 0))))
+        (setq string (substring-no-properties string (match-end 0))))
     (if (string-match "[ \t\n]+\\'" string)
-        (setq string (substring string 0 (match-beginning 0))))
-    (substring-no-properties string)))
+        (setq string (substring-no-properties string 0 (match-beginning 0))))
+    (unless (and null (string= "" string))
+      string)))
 
 (defsubst bbdb-string= (str1 str2)
   "Return t if strings STR1 and STR2 are equal, ignoring case."
@@ -2033,6 +2042,21 @@ You really should not disable debugging.
       `(let ((debug-on-error t))
          ,@body)))
 
+;; inspired by `gnus-bind-print-variables'
+(defmacro bbdb-with-print-loadably (&rest body)
+  "Bind print-* variables for BBDB and evaluate BODY.
+This macro is used with `prin1', `prin1-to-string', etc. in order to ensure
+printed Lisp objects are loadable by BBDB."
+  (declare (indent 0))
+  `(let ((print-escape-newlines t) ;; BBDB needs this!
+         print-escape-nonascii print-escape-multibyte
+         print-quoted print-length print-level)
+         ;; print-circle print-gensym
+         ;; print-continuous-numbering
+         ;; print-number-table
+         ;; float-output-format
+     ,@body))
+
 (defun bbdb-timestamp (record)
   "For use as an element of `bbdb-change-hook'.
 Maintains an xfield `timestamp' for RECORD which contains
@@ -2526,19 +2550,30 @@ Build and store it if necessary."
 Return nil if xfield LABEL is undefined."
   (cdr (assq label (bbdb-record-xfields record))))
 
-;; The values of xfields are always strings.  The following function
+;; The values of xfields are normally strings.  The following function
 ;; comes handy if we want to treat these values as symbols.
 (defun bbdb-record-xfield-intern (record label)
   "For RECORD return interned value of xfield LABEL.
 Return nil if xfield LABEL does not exist."
   (let ((value (bbdb-record-xfield record label)))
-    (if value (intern value))))
+    ;; If VALUE is not a string, return whatever it is.
+    (if (stringp value) (intern value) value)))
+
+(defun bbdb-record-xfield-string (record label)
+  "For RECORD return value of xfield LABEL as string.
+Return nil if xfield LABEL does not exist."
+  (let ((value (bbdb-record-xfield record label)))
+    (if (string-or-null-p value)
+        value
+      (let ((print-escape-newlines t))
+        (prin1-to-string value)))))
 
 (defsubst bbdb-record-xfield-split (record label)
   "For RECORD return value of xfield LABEL split as a list.
 Splitting is based on `bbdb-separator-alist'."
   (let ((val (bbdb-record-xfield record label)))
-    (if val (bbdb-split label val))))
+    (cond ((stringp val) (bbdb-split label val))
+          (val (error "Cannot split `%s'" val)))))
 
 (defun bbdb-record-set-xfield (record label value)
   "For RECORD set xfield LABEL to VALUE.
@@ -2551,7 +2586,7 @@ Return VALUE."
       (error "xfield label `%s' illegal" label))
   (if (eq label 'mail-alias)
       (setq bbdb-mail-aliases-need-rebuilt 'edit))
-  (if (and value (string= "" value)) (setq value nil))
+  (if (stringp value) (setq value (bbdb-string-trim value t)))
   (let ((old-xfield (assq label (bbdb-record-xfields record))))
     ;; Do nothing if both OLD-XFIELD and VALUE are nil.
     (cond ((and old-xfield value) ; update
@@ -2809,7 +2844,7 @@ See also `bbdb-record-field'."
                                                    value 'equal)))
            (if check (bbdb-check-type value (bbdb-record-phone record-type) t))
            (dolist (phone value)
-             (add-to-list 'bbdb-phone-label-list (bbdb-phone-label phone) nil 'eq))
+             (add-to-list 'bbdb-phone-label-list (bbdb-phone-label phone)))
            (bbdb-record-set-phone record value))
 
           ;; Address
@@ -2818,7 +2853,7 @@ See also `bbdb-record-field'."
                                                    value 'equal)))
            (if check (bbdb-check-type value (bbdb-record-address record-type) t))
            (dolist (address value)
-             (add-to-list 'bbdb-address-label-list (bbdb-address-label address) nil 'eq)
+             (add-to-list 'bbdb-address-label-list (bbdb-address-label address))
              (mapc (lambda (street) (bbdb-add-to-list 'bbdb-street-list street))
                    (bbdb-address-streets address))
              (bbdb-add-to-list 'bbdb-city-list (bbdb-address-city address))
@@ -2838,7 +2873,7 @@ See also `bbdb-record-field'."
              (if check (bbdb-check-type new-xfields (bbdb-record-xfields record-type) t))
              (dolist (xfield (nreverse value))
                ;; Ignore junk
-               (when (and (cdr xfield) (not (string= "" (cdr xfield))))
+               (when (and (cdr xfield) (not (equal "" (cdr xfield))))
                  (push xfield new-xfields)
                  (add-to-list 'bbdb-xfield-label-list (car xfield) nil 'eq)))
              (bbdb-record-set-xfields record new-xfields)))
@@ -2848,7 +2883,8 @@ See also `bbdb-record-field'."
            (if merge
                (setq value (bbdb-merge-xfield field (bbdb-record-xfield record field)
                                               value)))
-           (if check (bbdb-check-type value 'string t))
+           ;; The following test always succeeds
+           ;; (if check (bbdb-check-type value 'sexp t))
            ;; This removes xfield FIELD if its value is nil.
            (bbdb-record-set-xfield record field value))
 
@@ -2900,18 +2936,18 @@ If L1 or L2 are not lists, they are repl
 If LABEL has an entry in `bbdb-merge-xfield-function-alist', use it.
 If VALUE1 or VALUE2 is a substring of the other, return the longer one.
 Otherwise use `bbdb-concat'.  Return nil if we have nothing to merge."
-  (setq value1 (bbdb-string-trim value1)) ; converts nil to ""
-  (setq value2 (bbdb-string-trim value2)) ; converts nil to ""
-  (let ((b1 (not (string= "" value1)))
-        (b2 (not (string= "" value2))))
-    (cond ((and b1 b2)
-           (let ((fun (cdr (assq label bbdb-merge-xfield-function-alist))))
-             (cond (fun (funcall fun value1 value2))
-                   ((string-match (regexp-quote value1) value2) value2)
-                   ((string-match (regexp-quote value2) value1) value1)
-                   (t (bbdb-concat label value1 value2)))))
-          (b1 value1)
-          (b2 value2))))
+  (if (stringp value1) (setq value1 (bbdb-string-trim value1 t)))
+  (if (stringp value2) (setq value2 (bbdb-string-trim value2 t)))
+  (cond ((and value1 value2)
+         (let ((fun (cdr (assq label bbdb-merge-xfield-function-alist))))
+           (cond (fun (funcall fun value1 value2))
+                 ((not (and (stringp value1) (stringp value2)))
+                  (cons value1 value2)) ; concatenate lists
+                 ((string-match (regexp-quote value1) value2) value2)
+                 ((string-match (regexp-quote value2) value1) value1)
+                 (t (bbdb-concat label value1 value2)))))
+        (value1)
+        (value2)))
 
 ;;; Parsing other things
 
@@ -2940,12 +2976,13 @@ LAST is always a string (possibly empty)
 (defun bbdb-parse-postcode (string)
   "Check whether STRING is a legal postcode.
 Do this only if `bbdb-check-postcode' is non-nil."
-  (if (and bbdb-check-postcode
-           (not (memq t (mapcar (lambda (regexp)
-                                  ;; if it matches, (not (not index-of-match)) returns t
-                                  (not (not (string-match regexp string))))
-                                bbdb-legal-postcodes))))
-      (error "not a valid postcode.")
+  (if bbdb-check-postcode
+      (let ((postcodes bbdb-legal-postcodes) re done)
+        (while (setq re (pop postcodes))
+          (if (string-match re string)
+              (setq done t postcodes nil)))
+        (if done string
+          (error "not a valid postcode.")))
     string))
 
 (defun bbdb-phone-string (phone)
@@ -3239,9 +3276,9 @@ If `bbdb-file' uses an outdated format,
 
           ;; Set the completion lists
           (dolist (phone (bbdb-record-phone record))
-            (add-to-list 'bbdb-phone-label-list (bbdb-phone-label phone) nil 'eq))
+            (add-to-list 'bbdb-phone-label-list (bbdb-phone-label phone)))
           (dolist (address (bbdb-record-address record))
-            (add-to-list 'bbdb-address-label-list (bbdb-address-label address) nil 'eq)
+            (add-to-list 'bbdb-address-label-list (bbdb-address-label address))
             (mapc (lambda (street) (bbdb-add-to-list 'bbdb-street-list street))
                   (bbdb-address-streets address))
             (bbdb-add-to-list 'bbdb-city-list (bbdb-address-city address))
@@ -3320,43 +3357,72 @@ If `bbdb-file' uses an outdated format,
           (set-buffer-modified-p nil)))))
 
 (defun bbdb-change-record (record &optional need-to-sort new)
-  "Update the database after a change of RECORD.  Return RECORD.
+  "Update the database after a change of RECORD.
+Return RECORD if RECORD got changed compared with the database,
+return nil otherwise.
 NEED-TO-SORT is t when the name has changed.
 If NEW is t treat RECORD as new.  New records are hashed.
 If RECORD is not new, it is redisplayed.  Yet it is then the caller's
 responsibility to update the hash-table for RECORD."
   (if bbdb-read-only
       (error "The Insidious Big Brother Database is read-only."))
-  ;; Do the changing.
   ;; The call of `bbdb-records' checks file synchronization.
   ;; If RECORD refers to an existing record that has been changed,
   ;; yet in the meanwhile we reverted the BBDB file, then RECORD
   ;; no longer refers to a record in `bbdb-records'.  So we are stuck!
   ;; All changes will be lost.
-  ;; To avoid this problem we would have to inhibit that `bbdb-file'
-  ;; may change on disc.
-  (cond ((memq record (bbdb-records))
-         (unless bbdb-notice-hook-pending
-           (run-hook-with-args 'bbdb-change-hook record))
-         (if (not need-to-sort) ;; If we do not need to sort, overwrite RECORD.
-             (bbdb-overwrite-record-internal record)
-           ;; Since we need to sort, delete then insert RECORD.
-           ;; Do not mess with the hash table here.
-           ;; We assume it got updated by the caller.
-           (bbdb-delete-record-internal record)
-           (bbdb-insert-record-internal record))
-         ;; If RECORD is currently displayed update display.
-         (bbdb-maybe-update-display record))
-        (new ;; Record is new and not yet in database, so add it.
-         (run-hook-with-args 'bbdb-create-hook record)
-         (unless bbdb-notice-hook-pending
-           (run-hook-with-args 'bbdb-change-hook record))
-         (bbdb-insert-record-internal record)
-         (bbdb-hash-record record))
-        (t (error "Changes are lost.")))
-  (add-to-list 'bbdb-changed-records record nil 'eq)
-  (run-hook-with-args 'bbdb-after-change-hook record)
-  record)
+  ;; FIXME: Once all records have a UUID, we can identify the corresponding
+  ;; record on disk that got edited, so that the user can merge the edited
+  ;; record with what is now on disk (or do whatever with these two records).
+  ;; This implies, first of all, that *here* we make sure that UUIDs are
+  ;; always unique inside BBDB.  Should we maintain a second cache for that?
+  ;; If a new record happens to have the same UUID as an exisiting record,
+  ;; this should also throw an error / branch appropriately.  So the arg NEW
+  ;; will really not be needed anymore and all these things will have a natural
+  ;; solution.
+  (let ((tail (memq record (bbdb-records))))
+    (cond (tail ; RECORD is not new
+           ;; If the string we currently have for RECORD in `bbdb-buffer'
+           ;; is `equal' to the string we would write to `bbdb-buffer',
+           ;; we really did not change RECORD at all.  So we don't update RECORD
+           ;; unless `bbdb-update-unchanged-records' tells us to do so anyway.
+           ;; Also, we only call `bbdb-change-hook' and `bbdb-after-change-hook'
+           ;; if RECORD got changed.
+           (when (or bbdb-update-unchanged-records
+                     (not (string= (bbdb-with-db-buffer
+                                     (buffer-substring-no-properties
+                                      (bbdb-record-marker record)
+                                      (1- (if (cdr tail)
+                                              (bbdb-record-marker (cadr tail))
+                                            bbdb-end-marker))))
+                                   (let ((cache (bbdb-record-cache record))
+                                         (inhibit-quit t))
+                                     (bbdb-record-set-cache record nil)
+                                     (prog1 (bbdb-with-print-loadably
+                                              (prin1-to-string record))
+                                       (bbdb-record-set-cache record cache))))))
+             (run-hook-with-args 'bbdb-change-hook record)
+             (if (not need-to-sort) ;; If we do not need to sort, overwrite RECORD.
+                 (bbdb-overwrite-record-internal record)
+               ;; Since we need to sort, delete then insert RECORD.
+               ;; Do not mess with the hash table here.
+               ;; We assume it got updated by the caller.
+               (bbdb-delete-record-internal record)
+               (bbdb-insert-record-internal record))
+             (add-to-list 'bbdb-changed-records record nil 'eq)
+             (run-hook-with-args 'bbdb-after-change-hook record)
+             ;; If RECORD is currently displayed update display.
+             (bbdb-maybe-update-display record)
+             record))
+          (new ;; Record is new and not yet in database, so add it.
+           (run-hook-with-args 'bbdb-create-hook record)
+           (run-hook-with-args 'bbdb-change-hook record)
+           (bbdb-insert-record-internal record)
+           (bbdb-hash-record record)
+           (add-to-list 'bbdb-changed-records record nil 'eq)
+           (run-hook-with-args 'bbdb-after-change-hook record)
+           record)
+          (t (error "Changes are lost.")))))
 
 (defun bbdb-delete-record-internal (record &optional completely)
   "Delete RECORD in the database file.
@@ -3384,21 +3450,6 @@ from the hash table."
           (bbdb-remhash aka record))))
     (bbdb-record-set-sortkey record nil)))
 
-;; inspired by `gnus-bind-print-variables'
-(defmacro bbdb-with-print-loadably (&rest body)
-  "Bind print-* variables for BBDB and evaluate BODY.
-This macro is used with `prin1', `prin1-to-string', etc. in order to ensure
-printed Lisp objects are loadable by BBDB."
-  (declare (indent 0))
-  `(let ((print-escape-newlines t) ;; BBDB needs this!
-         print-escape-nonascii print-escape-multibyte
-         print-quoted print-length print-level)
-         ;; print-circle print-gensym
-         ;; print-continuous-numbering
-         ;; print-number-table
-         ;; float-output-format
-     ,@body))
-
 (defun bbdb-insert-record-internal (record)
   "Insert RECORD into the database file.  Return RECORD.
 Do not call this function directly, call instead `bbdb-change-record'
@@ -3719,11 +3770,18 @@ FIELD-LIST is the list of actually displ
                    (bbdb-display-list aka 'aka "; "))))
             ;; xfields
             (t
-             (let ((xfield (assq field (bbdb-record-xfields record))))
-               (if xfield
-                   (bbdb-display-text (concat (replace-regexp-in-string
-                                               "\n" "; " (cdr xfield)) "; ")
-                                      `(xfields ,xfield)))))))
+             (let* ((xfield (assq field (bbdb-record-xfields record)))
+                    (value (cdr xfield)))
+               (if value
+                   (bbdb-display-text
+                    (concat (if (stringp value)
+                                (replace-regexp-in-string
+                                 "\n" "; " value)
+                              ;; value of xfield is a sexp
+                              (let ((print-escape-newlines t))
+                                (prin1-to-string value)))
+                            "; ")
+                    `(xfields ,xfield)))))))
     ;; delete the trailing "; "
     (if (looking-back "; ")
         (backward-delete-char 2))
@@ -3785,13 +3843,23 @@ FIELD-LIST is the list of actually displ
                  (bbdb-display-list aka 'aka "\n"))))
             ;; xfields
             (t
-             (let ((xfield (assq field (bbdb-record-xfields record))))
-               (when xfield
+             (let* ((xfield (assq field (bbdb-record-xfields record)))
+                    (value (cdr xfield)))
+               (when value
                  (bbdb-display-text (format fmt field)
                                     `(xfields ,xfield field-label)
                                     'bbdb-field-name)
                  (setq start (point))
-                 (insert (bbdb-indent-string (cdr xfield) indent) "\n")
+                 (insert (bbdb-indent-string
+                          (if (stringp value)
+                              value
+                            ;; value of xfield is a sexp
+                            (let ((string (pp-to-string value)))
+                              (if (string-match "[ \t\n]+\\'" string)
+                                  (substring-no-properties
+                                   string 0 (match-beginning 0))
+                                string)))
+                          indent) "\n")
                  (bbdb-field-property start `(xfields ,xfield)))))))
     (insert "\n")))
 
@@ -3960,8 +4028,9 @@ The BBDB buffer must be current when thi
   ;; Ideally, `bbdb-redisplay-record' should put the point such that it
   ;; matches the previous value `bbdb-ident-point'.
   (let ((full-record (assq record bbdb-records)))
-    (if (null full-record) ; new record
-        (bbdb-display-records (list record) nil t)
+    (if (null full-record)
+        (unless delete-p
+          (bbdb-display-records (list record) nil t)) ; new record
       (let ((marker (nth 2 full-record))
             (end-marker (nth 2 (car (cdr (memq full-record bbdb-records)))))
             buffer-read-only record-number)
@@ -3981,25 +4050,26 @@ The BBDB buffer must be current when thi
           (delete-region (point) (or end-marker (point-max)))
           ;; If we deleted a record we need to update the subsequent
           ;; record numbers.
-          (if delete-p
-              (let* ((markers (append (mapcar (lambda (x) (nth 2 x))
-                                              (cdr (memq full-record bbdb-records)))
-                                      (list (point-max))))
-                     (start (pop markers)))
-                (dolist (end markers)
-                  (put-text-property start end
-                                     'bbdb-record-number record-number)
-                  (setq start end
-                        record-number (1+ record-number)))))
+          (when delete-p
+            (let* ((markers (append (mapcar (lambda (x) (nth 2 x))
+                                            (cdr (memq full-record bbdb-records)))
+                                    (list (point-max))))
+                   (start (pop markers)))
+              (dolist (end markers)
+                (put-text-property start end
+                                   'bbdb-record-number record-number)
+                (setq start end
+                      record-number (1+ record-number))))
+            (setq bbdb-records (delq full-record bbdb-records)))
           (run-hooks 'bbdb-display-hook))))))
 
 (defun bbdb-maybe-update-display (record &optional delete-p)
-  "If RECORD is currently displayed update display.
+  "Update display of RECORD in all BBDB buffers.
 If DELETE-P is nil RECORD is removed from the BBDB buffers."
   (dolist (buffer (buffer-list))
     (with-current-buffer buffer
       (if (and (eq major-mode 'bbdb-mode)
-               (memq record (bbdb-records)))
+               (memq record (mapcar 'car bbdb-records)))
           (let ((window (get-buffer-window bbdb-buffer-name)))
             (if window
                 (with-selected-window window
@@ -4193,7 +4263,8 @@ There are numerous hooks.  M-x apropos ^
                                 (length bbdb-records))))
               '(:eval (concat "  "
                               (bbdb-concat " " (elt bbdb-modeline-info 0)
-                                           (elt bbdb-modeline-info 1)))))
+                                           (elt bbdb-modeline-info 2)
+                                           (elt bbdb-modeline-info 4)))))
         mode-line-modified
         ;; For the mode-line we want to be fast. So we skip the checks
         ;; performed by `bbdb-with-db-buffer'.
@@ -4437,6 +4508,7 @@ List MUAS may include the following symb
 mail/news readers, composers, and miscellaneous packages:
   gnus       Gnus mail/news reader.
   mh-e       MH-E mail reader.
+  mu4e       Mu4e mail reader.
   rmail      Rmail mail reader.
   vm         VM mail reader.
   mail       Mail (M-x mail).
@@ -4469,3 +4541,5 @@ for example only for outgoing messages."
 
 
 (provide 'bbdb)
+
+;;; bbdb.el ends here
--- bbdb3-3.1.2.orig/lisp/makefile-temp
+++ bbdb3-3.1.2/lisp/makefile-temp
@@ -1,5 +1,5 @@
 # Cheap BBDB makefile  -*- Makefile -*-
-# Copyright (C) 2010-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2010-2015 Roland Winkler <winkler@gnu.org>
 # 
 # This file is part of the Insidious Big Brother Database (aka BBDB),
 # 
@@ -43,20 +43,28 @@ EMACS = emacs
 EMACSOPT =
 
 # The actual Emacs command run in the targets below.
-emacs = LC_ALL=C $(EMACS) --batch --directory=./ $(EMACSOPT)
+# --batch implies --no-init-file, yet let's be explicit about what we want
+emacs = LC_ALL=C $(EMACS) --batch --no-init-file --no-site-file \
+		--directory=./ $(EMACSOPT)
+emacs_compile = $(emacs) --funcall batch-byte-compile
+
+# Mu4e is not part of GNU Emacs.  If you want to use BBDB with Mu4e
+# then the variable MU4EDIR should point to your mu4e lisp directory.
+MU4EDIR =
+MU4E = -eval '(unless (string= "$(MU4EDIR)" "") (push "$(MU4EDIR)" load-path))'
 
-# If you want to use BBDB with VM this should point to your vm/lisp directory.
-# See also the target all below.
+# VM is not part of GNU Emacs.  If you want to use BBDB with VM
+# then the variable VMDIR should point to your vm/lisp directory.
 VMDIR =
-
-VM = -eval '(unless (string-match "$(VMDIR)" "") (push "$(VMDIR)" load-path))'
+VM = -eval '(unless (string= "$(VMDIR)" "") (push "$(VMDIR)" load-path))'
 
 .SUFFIXES: .elc .el .tar .Z .gz .uu
 
 SRCS =	bbdb.el bbdb-site.el bbdb-com.el bbdb-print.el bbdb-anniv.el \
 	bbdb-migrate.el bbdb-snarf.el \
 	bbdb-mua.el bbdb-message.el bbdb-rmail.el \
-	bbdb-gnus.el bbdb-mhe.el bbdb-vm.el bbdb-pgp.el bbdb-sc.el \
+	bbdb-gnus.el bbdb-mhe.el bbdb-mu4e.el bbdb-vm.el bbdb-pgp.el \
+	bbdb-sc.el \
 	bbdb-ispell.el bbdb-pkg.el
 
 # ELC =	$(patsubst %.el,%.elc,$(SRCS)) # GNU Make
@@ -69,6 +77,7 @@ ELC =	bbdb.elc bbdb-site.elc bbdb-com.el
 all: bbdb
 
 bbdb:	bbdb-loaddefs.el $(ELC)
+mu4e:	bbdb-mu4e.elc
 vm:	bbdb-vm.elc
 
 bbdb-loaddefs.el: $(SRCS)
@@ -89,51 +98,55 @@ bbdb-loaddefs.el: $(SRCS)
 	@echo "" >> $@;
 #	Generated autoload-file must have an absolute path,
 #	$(srcdir) can be relative.
-	$(emacs) -l autoload \
+	$(emacs) --load autoload \
 		--eval '(setq generated-autoload-file "'`pwd`/$@'")' \
 		--eval '(setq make-backup-files nil)' \
-		-f batch-update-autoloads `pwd`
+		--funcall batch-update-autoloads `pwd`
 
 .el.elc:
-	$(emacs) -f batch-byte-compile $<
+	$(emacs_compile) $<
 
 # Not perfect, but better than nothing:  If we do not have / do not use
 # autotools, we simply copy bbdb-site.el.in to bbdb-site.el.
 bbdb-site.el: bbdb-site.el.in
 	$(CP) $< $@
 bbdb-site.elc: bbdb-site.el
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
+bbdb-pkg.el: bbdb-pkg.el.in
+	$(CP) $< $@
 
 bbdb.elc: bbdb.el bbdb-site.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 
 bbdb-com.elc: bbdb-com.el bbdb.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-mua.elc: bbdb-mua.el bbdb-com.elc
 	$(emacs) -eval '(unless (string= "$(VMDIR)" "") (push "$(VMDIR)" load-path) (load "vm" t t))' \
-	-f batch-byte-compile $(@:.elc=.el)
+	 -eval '(unless (string= "$(MU4EDIR)" "") (push "$(MU4EDIR)" load-path) (load "mu4e" t t))' \
+	 --funcall batch-byte-compile $(@:.elc=.el)
 bbdb-rmail.elc: bbdb-rmail.el bbdb-mua.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-gnus.elc: bbdb-gnus.el bbdb-mua.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-mhe.elc: bbdb-mhe.el bbdb-mua.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
-# VM is not part of GNU Emacs
+	$(emacs_compile) $(@:.elc=.el)
+bbdb-mu4e.elc: bbdb-mu4e.el bbdb-mua.elc
+	$(emacs) $(MU4E) --funcall batch-byte-compile $(@:.elc=.el)
 bbdb-vm.elc: bbdb-vm.el bbdb-mua.elc
-	$(emacs) $(VM) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs) $(VM) --funcall batch-byte-compile $(@:.elc=.el)
 bbdb-sc.elc: bbdb-sc.el bbdb-mua.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 
 bbdb-print.elc: bbdb-print.el bbdb-com.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-migrate.elc: bbdb-migrate.el bbdb.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-anniv.elc: bbdb-anniv.el bbdb-com.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-ispell.elc: bbdb-ispell.el bbdb.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 bbdb-snarf.elc: bbdb-snarf.el bbdb-com.elc
-	$(emacs) -f batch-byte-compile $(@:.elc=.el)
+	$(emacs_compile) $(@:.elc=.el)
 
 install-el: all
 	$(INSTALL) -d -m 0755 "$(lispdir)/"
--- /dev/null
+++ bbdb3-3.1.2/m4/emacs_mu4e.m4
@@ -0,0 +1,39 @@
+### emacs_mu4e.m4
+
+## Copyright (C) 2015 Roland Winkler <winkler@gnu.org>
+##
+## This file is part of the Insidious Big Brother Database (aka BBDB),
+##
+## BBDB 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.
+##
+## BBDB 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 BBDB.  If not, see <http://www.gnu.org/licenses/>.
+
+AC_DEFUN([EMACS_MU4E],
+[
+AC_ARG_WITH([mu4e-dir],
+AS_HELP_STRING([--with-mu4e-dir=DIR], [where to find Mu4e lisp directory]),
+# if Mu4e was requested, make sure we have access to the source
+[if test "x$with_mu4e_dir" != xno -a "x$with_mu4e_dir" != "x"; then
+    AC_MSG_CHECKING([for Mu4e files])
+    # convert path to absolute and canonicalize it.
+    MU4EDIR=$(${EMACS} -batch --quick -eval "(message \"%s\" (expand-file-name \"${with_mu4e_dir}\"))" 2>&1)
+    MU4E_LOCATE=$(${EMACS} -batch --quick --directory="${MU4EDIR}" -eval "(if (locate-library \"mu4e-vars\") (message \"mu4e\"))" 2>&1)
+    if test "x$MU4E_LOCATE" = "x"; then
+       AC_MSG_ERROR([*** MU4E mu4e-vars.el must exist in directory passed to --with-mu4e-dir.])
+    fi
+    AC_MSG_RESULT($MU4EDIR)
+    # append MU4EDIR to AM_ELCFLAGS
+    AM_ELCFLAGS="--directory=$MU4EDIR $AM_ELCFLAGS"
+ fi])
+# New conditional MU4E
+AM_CONDITIONAL([MU4E], [test x$MU4EDIR != x])
+])
--- bbdb3-3.1.2.orig/m4/emacs_vm.m4
+++ bbdb3-3.1.2/m4/emacs_vm.m4
@@ -1,6 +1,6 @@
 ### emacs_vm.m4
 
-## Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+## Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 ##
 ## This file is part of the Insidious Big Brother Database (aka BBDB),
 ##
--- bbdb3-3.1.2.orig/m4/package_date.m4
+++ bbdb3-3.1.2/m4/package_date.m4
@@ -1,6 +1,6 @@
 ### package_date.m4
 
-## Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+## Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 ##
 ## This file is part of the Insidious Big Brother Database (aka BBDB),
 ##
--- bbdb3-3.1.2.orig/tex/Makefile.am
+++ bbdb3-3.1.2/tex/Makefile.am
@@ -1,7 +1,7 @@
 # tex/Makefile.am for BBDB
 #
 # Copyright (C) 2013 Christian Egli <christian.egli@sbs.ch>
-# Copyright (C) 2013-2014 Roland Winkler <winkler@gnu.org>
+# Copyright (C) 2013-2015 Roland Winkler <winkler@gnu.org>
 # 
 # This file is part of the Insidious Big Brother Database (aka BBDB),
 # 
