Index: src/import-export/qif-import/qif-guess-map.scm
===================================================================
--- src/import-export/qif-import/qif-guess-map.scm	(revision 16927)
+++ src/import-export/qif-import/qif-guess-map.scm	(working copy)
@@ -21,19 +21,34 @@ Commenting changes only
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;  qif-import:load-map-prefs
-;;  load the saved mappings file, and make a table of all the 
-;;  accounts with their full names and pointers for later 
+;;
+;;  Load the saved mappings file, and make a table of all the
+;;  accounts with their full names and pointers for later
 ;;  guessing of a mapping.
+;;
+;;  We'll be returning a list with the following members:
+;;   - a list of all the known gnucash accounts in
+;;     (shortname fullname Account*) format
+;;   - a hash of QIF account name to gnucash account info
+;;   - a hash of QIF category to gnucash account info
+;;   - a hash of QIF memo/payee to gnucash account info
+;;     (older saved prefs may not have this one)
+;;   - a hash of QIF stock name to gnc-commodity*
+;;     (older saved prefs may not have this one)
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (define (qif-import:load-map-prefs)
+
+  ;; This procedure builds a list of all descendants of an existing
+  ;; GnuCash account. Each member of the list is itself a list with
+  ;; the form: (shortname fullname Account*)
   (define (extract-all-account-info an-account root-name)
     (if (null? an-account)
         '()
         (let ((children-list (gnc-account-get-children-sorted an-account))
               (names '()))
           
-          ;; now descend the tree of child accounts.
+          ;; Recursively walk the account tree.
           (for-each 
            (lambda (child-acct)
              (let* ((name (xaccAccountGetName child-acct))
@@ -54,21 +69,12 @@ The first block of comments here has been moved to the procedure's introduction.
     (false-if-exception 
      (read)))
 
-  ;; we'll be returning a list:  
-  ;;  - a list of all the known gnucash accounts in 
-  ;;    (shortname fullname account*) format.
-  ;;  - a hash of QIF account name to gnucash account info
-  ;;  - a hash of QIF category to gnucash account info
-  ;;  - a hash of QIF memo/payee to gnucash account info  
-  ;;    (older saved prefs may not have this one)
-  ;;  - a hash of QIF stock name to gnc-commodity*
-  ;;    (older saved prefs may not have this one)
   (let* ((pref-filename (gnc-build-dotgnucash-path "qif-accounts-map"))
          (results '()))
     
-    ;; first, read the account map and category map from the 
-    ;; user's qif-accounts-map file.     
+    ;; Get the user's saved mappings.
     (if (access? pref-filename R_OK)
+        ;; We have access to the mapping file (qif-accounts-map).
         (with-input-from-file pref-filename
           (lambda ()
             (let ((qif-account-list #f)
@@ -78,50 +84,67 @@ Now reads the old separator from the qif-accounts-map file.
                   (qif-account-hash #f)
                   (qif-cat-hash #f)
                   (qif-memo-hash #f)
-                  (qif-stock-hash #f))
+                  (qif-stock-hash #f)
+                  (saved-sep #f))
+
+              ;; Read the mapping file.
               (set! qif-account-list (safe-read))
+              (set! qif-cat-list (safe-read))
+              (set! qif-memo-list (safe-read))
+              (set! qif-stock-list (safe-read))
+              (set! saved-sep (safe-read))
+
+              ;; Process the QIF account mapping.
               (if (not (list? qif-account-list))
                   (set! qif-account-hash (make-hash-table 20))
                   (set! qif-account-hash 
-                        (qif-import:read-map qif-account-list)))
+                        (qif-import:read-map qif-account-list
+                                             saved-sep)))
               
-              (set! qif-cat-list (safe-read))
+              ;; Process the QIF category mapping.
               (if (not (list? qif-cat-list))
                   (set! qif-cat-hash (make-hash-table 20))
-                  (set! qif-cat-hash (qif-import:read-map qif-cat-list)))
+                  (set! qif-cat-hash (qif-import:read-map qif-cat-list
+                                                          saved-sep)))
 
-              (set! qif-memo-list (safe-read))
+              ;; Process the QIF payee/memo mapping.
               (if (not (list? qif-memo-list))
                   (set! qif-memo-hash (make-hash-table 20))
-                  (set! qif-memo-hash (qif-import:read-map qif-memo-list)))
+                  (set! qif-memo-hash (qif-import:read-map qif-memo-list
+                                                           saved-sep)))
 
-              (set! qif-stock-list (safe-read))
+              ;; Process the QIF security mapping.
               (if (not (list? qif-stock-list))
                   (set! qif-stock-hash (make-hash-table 20))
                   (set! qif-stock-hash (qif-import:read-commodities
-                                        qif-stock-list)))              
-              (set! results 
-                    (list qif-account-hash qif-cat-hash 
-                          qif-memo-hash qif-stock-hash)))))
-        (begin 
-          (set! results (list (make-hash-table 20)
-                              (make-hash-table 20)
-                              (make-hash-table 20)
-                              (make-hash-table 20)))))
+                                          qif-stock-list)))
+
+              ;; Put all the mappings together in a list.
+              (set! results (list qif-account-hash
+                                  qif-cat-hash 
+                                  qif-memo-hash
+                                  qif-stock-hash)))))
+
+        ;; Otherwise, we can't get any saved mappings. Use empty tables.
+        (set! results (list (make-hash-table 20)
+                            (make-hash-table 20)
+                            (make-hash-table 20)
+                            (make-hash-table 20))))
     
-    ;; now build the list of all known account names 
+    ;; Build the list of all known account names.
     (let* ((all-accounts (gnc-get-current-root-account))
            (all-account-info (extract-all-account-info all-accounts #f)))
       (set! results (cons all-account-info results)))
+
     results))
 
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;  dump the mapping hash tables to a file.  The hash tables are 
-;;  updated when the user clicks the big "OK" button on the dialog,
-;;  so your selections get lost if you do Cancel.
-;;  we initialize the number of transactions to 0 here so 
-;;  bogus accounts don't get created if you have funny stuff
-;;  in your map.
+;;  qif-import:write-map
+;;
+;;  Writes out a mapping hash table, setting the number of
+;;  transactions to 0 along the way to prevent the creation
+;;  of bogus accounts if you have funny stuff in your map.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (define (qif-import:write-map hashtab)
@@ -132,17 +155,30 @@ GnuCash account names are new adjusted for any separator changes.
        #f) #f hashtab)
     (write table)))
 
-(define (qif-import:read-map tablist)
-  (let ((table (make-hash-table 20)))
+(define (qif-import:read-map tablist tab-sep)
+  (let* ((table (make-hash-table 20))
+         (sep (string-ref (gnc-get-account-separator-string) 0))
+         (changed-sep? (and (char? tab-sep) (not (char=? tab-sep sep)))))
+
     (for-each 
      (lambda (entry)
        (let ((key (car entry))
              (value (simple-obj-from-list (cdr entry) <qif-map-entry>)))
+
+         ;; If the account separator has changed, fix the account name.
+         (if changed-sep?
+           (let ((acct-name (qif-map-entry:gnc-name value)))
+             (if (string? acct-name)
+                 (qif-map-entry:set-gnc-name! value
+                   (string-map (lambda (c) (if (char=? c tab-sep) sep c))
+                               acct-name)))))
+
          (qif-map-entry:set-display?! value #f)
          (hash-set! table key value)))
      tablist)
     table))
 
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;  qif-import:read-commodities
 ;;
@@ -186,6 +222,15 @@
     (write table)))
 
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;  qif-import:save-map-prefs
+;;
+;;  This procedure saves all the user's mapping preferences to a
+;;  file.  This only gets called when the user clicks the Apply
+;;  button in the druid, so any new mappings will be lost if the
+;;  user cancels the import instead.
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (define (qif-import:save-map-prefs acct-map cat-map memo-map stock-map)
   (let* ((pref-filename (gnc-build-dotgnucash-path "qif-accounts-map")))
     ;; does the file exist? if not, create it; in either case,
@@ -211,6 +256,10 @@ The qif-accounts-map now keeps track of the separator used to create it.
 
             (display ";;; map from QIF stock name to GNC commodity") (newline)
             (qif-import:write-commodities stock-map)           
+            (newline)
+
+            (display ";;; GnuCash separator used in these mappings") (newline)
+            (write (string-ref (gnc-get-account-separator-string) 0))
             (newline)))))
 
 
