Function: locate-user-emacs-file

locate-user-emacs-file is a byte-compiled function defined in files.el.gz.

Signature

(locate-user-emacs-file NEW-NAME &optional OLD-NAME)

Documentation

Return an absolute per-user Emacs-specific file name.

If NEW-NAME exists in user-emacs-directory, return it. Else if OLD-NAME is non-nil and ~/OLD-NAME exists, return ~/OLD-NAME. Else return NEW-NAME in user-emacs-directory, creating the directory if it does not exist.

NEW-NAME can also be a list, in which case consider all names in that list, from last to first, and use the first name that exists. If none of them exists, use the car of that list.

View in manual

Probably introduced at or before Emacs version 23.1.

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun locate-user-emacs-file (new-name &optional old-name)
  "Return an absolute per-user Emacs-specific file name.
If NEW-NAME exists in `user-emacs-directory', return it.
Else if OLD-NAME is non-nil and ~/OLD-NAME exists, return ~/OLD-NAME.
Else return NEW-NAME in `user-emacs-directory', creating the
directory if it does not exist.

NEW-NAME can also be a list, in which case consider all names in that
list, from last to first, and use the first name that exists.  If none
of them exists, use the `car' of that list."
  (convert-standard-filename
   (let* ((home (concat "~" (or init-file-user "")))
	  (at-home (and old-name (expand-file-name old-name home)))
          (bestname (abbreviate-file-name
                     (if (listp new-name)
                         (or (car (seq-filter
                                   #'file-exists-p
                                   (mapcar
                                    (lambda (f)
                                      (expand-file-name f user-emacs-directory))
                                    (reverse new-name))))
                             (expand-file-name (car new-name) user-emacs-directory))
                       (expand-file-name new-name user-emacs-directory)))))
     (if (and at-home (not (file-readable-p bestname))
              (file-readable-p at-home))
	 at-home
       ;; Make sure `user-emacs-directory' exists,
       ;; unless we're in batch mode or dumping Emacs.
       (or noninteractive
           dump-mode
	   (let (errtype)
	     (if (file-directory-p user-emacs-directory)
		 (or (file-accessible-directory-p user-emacs-directory)
		     (setq errtype "access"))
               ;; We don't want to create HOME if it doesn't exist.
               (if (and (not (file-exists-p "~"))
                        (string-prefix-p
                         (expand-file-name "~")
                         (expand-file-name user-emacs-directory)))
                   (setq errtype "create")
                 ;; Create `user-emacs-directory'.
                 (with-file-modes ?\700
		   (condition-case nil
		       (make-directory user-emacs-directory t)
		     (error (setq errtype "create"))))))
	     (when (and errtype
			user-emacs-directory-warning
			(not (get 'user-emacs-directory-warning 'this-session)))
	       ;; Warn only once per Emacs session.
	       (put 'user-emacs-directory-warning 'this-session t)
	       (display-warning 'initialization
				(format "\
Unable to %s `user-emacs-directory' (%s).
Any data that would normally be written there may be lost!
If you never want to see this message again,
customize the variable `user-emacs-directory-warning'."
					errtype user-emacs-directory)))))
       bestname))))