+(defun find-user-init-directory-init-file (&optional init-directory)
+ "Determine the user's init file if in the init directory."
+ (let ((init-directory (or init-directory user-init-directory)))
+ (catch 'found
+ (dolist (file user-init-file-base-list)
+ (let ((expanded (expand-file-name file init-directory)))
+ (when (file-readable-p expanded)
+ (throw 'found expanded)))))))
+
+(defun find-user-home-directory-init-file (&optional home-directory)
+ "Determine the user's init file if in the home directory."
+ (let ((home-directory (or home-directory "~")))
+ (catch 'found
+ (dolist (file user-home-init-file-base-list)
+ (let ((expanded (expand-file-name file home-directory)))
+ (when (file-readable-p expanded)
+ (throw 'found expanded))))
+ nil)))
+
+(defun find-user-init-file (&optional init-directory home-directory)
+ "Determine the user's init file."
+ (if load-home-init-file
+ (find-user-home-directory-init-file home-directory)
+ (or (find-user-init-directory-init-file init-directory)
+ (find-user-home-directory-init-file home-directory))))
+
+(defun maybe-migrate-user-init-file ()
+ "Ask user if she wants to migrate the init file(s) to new location."
+ (if (and (not load-home-init-file)
+ (not (find-user-init-directory-init-file user-init-directory))
+ (stringp user-init-file)
+ (file-readable-p user-init-file))
+ (if (with-output-to-temp-buffer (help-buffer-name nil)
+ (progn
+ (princ "XEmacs recommends that the initialization code in
+")
+ (princ user-init-file)
+ (princ "
+be migrated to the ")
+ (princ user-init-directory)
+ (princ " directory. XEmacs can
+perform the migration automatically.
+
+After the migration, init.el/init.elc holds user-written
+initialization code. Moreover the customize settings will be in
+custom.el.
+
+You can undo the migration at any time with
+M-x maybe-unmigrate-user-init-file.
+
+If you choose not to do this now, XEmacs will not ask you this
+question in the future. However, you can still make XEmacs
+perform the migration at any time with M-x migrate-user-init-file.")
+ (show-temp-buffer-in-current-frame standard-output)
+ (yes-or-no-p-minibuf (concat "Migrate init file to "
+ user-init-directory
+ "? "))))
+ (progn
+ (migrate-user-init-file)
+ (maybe-create-compatibility-dot-emacs))
+ (customize-save-variable 'load-home-init-file t))))
+
+(defun maybe-create-compatibility-dot-emacs ()
+ "Ask user if she wants to create a .emacs compatibility file."
+ (if (with-output-to-temp-buffer (help-buffer-name nil)
+ (progn
+ (princ "The initialization code has now been migrated to the ")
+ (princ user-init-directory)
+ (princ "directory.
+
+For backwards compatibility with, for example, older versions of XEmacs,
+XEmacs can create a special old-style .emacs file in your home
+directory which will load the relocated initialization code.")
+ (show-temp-buffer-in-current-frame standard-output)
+ (yes-or-no-p-minibuf "Create compatibility .emacs? ")))
+ (create-compatibility-dot-emacs)))
+
+(defun migrate-user-init-file ()
+ "Migrate the init file from the home directory."
+ (interactive)
+ (if (not (file-exists-p user-init-directory))
+ (progn
+ (message "Creating %s directory..." user-init-directory)
+ (make-directory user-init-directory)))
+ (message "Migrating custom file...")
+ (customize-set-value 'load-home-init-file nil)
+ (custom-migrate-custom-file (make-custom-file-name user-init-file
+ 'force-new))
+ (message "Moving init file...")
+ (let ((new-user-init-file (expand-file-name user-init-file-base
+ user-init-directory)))
+ (rename-file user-init-file new-user-init-file)
+ (setq user-init-file new-user-init-file))
+ (message "Migration done."))
+
+(defun create-compatibility-dot-emacs ()
+ "Create .emacs compatibility file for migrated setup."
+ (message "Creating .emacs compatibility file.")
+ (with-temp-file (expand-file-name ".emacs" "~")
+ (insert ";;; XEmacs backwards compatibility file\n")
+ (insert "(setq user-init-file\n")
+ (insert " (expand-file-name \"init.el\"\n")
+ (insert " (expand-file-name \".xemacs\" \"~\")))\n")
+ (insert "(setq custom-file\n")
+ (insert " (expand-file-name \"custom.el\"\n")
+ (insert " (expand-file-name \".xemacs\" \"~\")))\n")
+ (insert "\n")
+ (insert "(load-file user-init-file)\n")
+ (insert "(load-file custom-file)"))
+ (message "Created .emacs compatibility file."))
+
+(defun maybe-unmigrate-user-init-file ()
+ "Possibly unmigrate the user's init and custom files."
+ (interactive)
+ (let ((dot-emacs-file-name (expand-file-name ".emacs" "~")))
+ (if (and (not load-home-init-file)
+ (or (not (file-exists-p dot-emacs-file-name))
+ (yes-or-no-p-minibuf (concat "Overwrite " dot-emacs-file-name
+ "? "))))
+ (unmigrate-user-init-file dot-emacs-file-name))))
+
+(defun unmigrate-user-init-file (&optional target-file-name)
+ "Unmigrate the user's init and custom files."
+ (interactive)
+ (let ((target-file-name
+ (or target-file-name (expand-file-name ".emacs" "~"))))
+ (rename-file user-init-file target-file-name 'ok-if-already-exists)
+ (setq user-init-file target-file-name)
+ (let ((old-custom-file custom-file))
+ (custom-migrate-custom-file target-file-name)
+ (customize-save-variable 'load-home-init-file t)
+ (delete-file old-custom-file))))
+