emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] for review - Allow expansion of "~" (as opposed to "~user")


From: Michal Nazarewicz
Subject: Re: [PATCH] for review - Allow expansion of "~" (as opposed to "~user")
Date: Mon, 02 Mar 2015 10:44:07 +0100
User-agent: Notmuch/0.19+53~g2e63a09 (http://notmuchmail.org) Emacs/25.0.50.1 (x86_64-unknown-linux-gnu)

On Sat, Feb 28 2015, Eli Zaretskii wrote:
>> Date: Fri, 27 Feb 2015 16:06:02 -0800
>> From: Pete Williamson <address@hidden>
>> 
>> I'm porting Emacs to run on NaCl (Chrome Native Client) and the Chromebook.
>> (You can see slides about this in the FOSDEM 2015 archives).
>> 
>> The NaCl platform does not support expanding the "~user" syntax for filenames
>> (where user is the name of the logged in user), but it does support expanding
>> "~" in filenames when looking for the init.el file.
>> 
>> I've made a patch to startup.el to check in "~" for .emacs.d/init.el if it is
>> not found in "~user". Is taking this patch a good idea? I'm not sure how much
>> emacs should adapt to the platform, and how much should be done in a private
>> patch that does not affect other platforms, and I would be grateful for any
>> guidance.
>> 
>> Currently for the NaCl port, we have a patch in naclports which does OS
>> specific fixes, and I can leave this patch in naclports if that is thought by
>> others to be a better place, or I would be happy to contribute it back to Gnu
>> if it is seen as generally useful.
>> 
>> All comments on the patch welcome.
>
> Simply reuse for NaCl what we already do for MS-Windows:
>
>         (if (file-directory-p (expand-file-name
>                                ;; We don't support ~USER on MS-Windows
>                                ;; and MS-DOS except for the current
>                                ;; user, and always load .emacs from
>                                ;; the current user's home directory
>                                ;; (see below).  So always check "~",
>                                ;; even if invoked with "-u USER", or
>                                ;; if $USER or $LOGNAME are set to
>                                ;; something different.
>                                (if (memq system-type '(windows-nt ms-dos))
>                                    "~"
>                                  (concat "~" init-file-user))))
>
> If you still need something beyond that, please explain why, as the
> situation you describe seems to be identical to what happens on
> MS-Windows.

Looking at that code, it appears it’s inconsistent on Windows when it
comes to handling ~/.emacs.d/init.el.  If I understand it correctly,
here’s how Emacs behave on Windows:

* emacs        -> load ~/.emacs, ~/_emacs or ~/.emacs.d/init.el
* emacs -u foo -> load ~/.emacs or ~/_emacs

I would expect it to try loading ~/.emacs.d/init.el as well.  Perhaps we
need something along the lines of:

diff --git a/lisp/startup.el b/lisp/startup.el
index 999e53e..5998e62 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -1042,6 +1042,22 @@ (defun command-line ()
     ;; the startup screen.
     (setq inhibit-startup-screen nil)
 
+    ;; Load that user's init file, or the default one, or none.
+    (let (debug-on-error-from-init-file
+         debug-on-error-should-be-set
+         (debug-on-error-initial
+          (if (eq init-file-debug t) 'startup init-file-debug))
+         (orig-enable-multibyte (default-value 'enable-multibyte-characters))
+          (home-dir (expand-file-name
+                     ;; We don't support ~USER on MS-Windows and MS-DOS except
+                     ;; for the current user, and always load .emacs from the
+                     ;; current user's home directory (see below).  So always
+                     ;; check "~", even if invoked with "-u USER", or if $USER
+                     ;; or $LOGNAME are set to something different.
+                     (if (memq system-type '(windows-nt ms-dos))
+                         "~"
+                       (concat "~" init-file-user)))))
+
     ;; Warn for invalid user name.
     (when init-file-user
       (if (string-match "[~/:\n]" init-file-user)
@@ -1049,19 +1065,7 @@ (defun command-line ()
                           (format "Invalid user name %s"
                                   init-file-user)
                           :error)
-       (if (file-directory-p (expand-file-name
-                              ;; We don't support ~USER on MS-Windows
-                              ;; and MS-DOS except for the current
-                              ;; user, and always load .emacs from
-                              ;; the current user's home directory
-                              ;; (see below).  So always check "~",
-                              ;; even if invoked with "-u USER", or
-                              ;; if $USER or $LOGNAME are set to
-                              ;; something different.
-                              (if (memq system-type '(windows-nt ms-dos))
-                                  "~"
-                                (concat "~" init-file-user))))
-           nil
+       (unless (file-directory-p home-dir)
          (display-warning 'initialization
                           (format "User %s has no home directory"
                                   (if (equal init-file-user "")
@@ -1069,82 +1073,73 @@ (defun command-line ()
                                     init-file-user))
                           :error))))
 
-    ;; Load that user's init file, or the default one, or none.
-    (let (debug-on-error-from-init-file
-         debug-on-error-should-be-set
-         (debug-on-error-initial
-          (if (eq init-file-debug t) 'startup init-file-debug))
-         (orig-enable-multibyte (default-value 'enable-multibyte-characters)))
       (let ((debug-on-error debug-on-error-initial)
            ;; This function actually reads the init files.
            (inner
             (function
              (lambda ()
-               (if init-file-user
-                   (let ((user-init-file-1
-                          (cond
-                            ((eq system-type 'ms-dos)
-                             (concat "~" init-file-user "/_emacs"))
-                            ((not (eq system-type 'windows-nt))
-                             (concat "~" init-file-user "/.emacs"))
-                            ;; Else deal with the Windows situation
-                            ((directory-files "~" nil 
"^\\.emacs\\(\\.elc?\\)?$")
-                             ;; Prefer .emacs on Windows.
-                             "~/.emacs")
-                            ((directory-files "~" nil "^_emacs\\(\\.elc?\\)?$")
-                             ;; Also support _emacs for compatibility, but 
warn about it.
-                             (push '(initialization
-                                     "`_emacs' init file is deprecated, please 
use `.emacs'")
-                                   delayed-warnings-list)
-                             "~/_emacs")
-                            (t ;; But default to .emacs if _emacs does not 
exist.
-                             "~/.emacs"))))
-                     ;; This tells `load' to store the file name found
-                     ;; into user-init-file.
-                     (setq user-init-file t)
-                     (load user-init-file-1 t t)
-
-                     (when (eq user-init-file t)
-                       ;; If we did not find ~/.emacs, try
-                       ;; ~/.emacs.d/init.el.
-                       (let ((otherfile
-                              (expand-file-name
-                               "init"
-                               (file-name-as-directory
-                                (concat "~" init-file-user "/.emacs.d")))))
-                         (load otherfile t t)
-
-                         ;; If we did not find the user's init file,
-                         ;; set user-init-file conclusively.
-                         ;; Don't let it be set from default.el.
-                         (when (eq user-init-file t)
-                           (setq user-init-file user-init-file-1))))
-
-                     ;; If we loaded a compiled file, set
-                     ;; `user-init-file' to the source version if that
-                     ;; exists.
-                     (when (and user-init-file
-                                (equal (file-name-extension user-init-file)
-                                       "elc"))
-                       (let* ((source (file-name-sans-extension 
user-init-file))
-                              (alt (concat source ".el")))
-                         (setq source (cond ((file-exists-p alt) alt)
-                                            ((file-exists-p source) source)
-                                            (t nil)))
-                         (when source
-                           (when (file-newer-than-file-p source user-init-file)
-                             (message "Warning: %s is newer than %s"
-                                      source user-init-file)
-                             (sit-for 1))
-                           (setq user-init-file source))))
-
-                     (unless inhibit-default-init
-                        (let ((inhibit-startup-screen nil))
-                          ;; Users are supposed to be told their rights.
-                          ;; (Plus how to get help and how to undo.)
-                          ;; Don't you dare turn this off for anyone
-                          ;; except yourself.
-                          (load "default" t t)))))))))
+               (when init-file-user
+                  (let ((user-init-file-1
+                         (cond
+                          ((eq system-type 'ms-dos)
+                           ;; DOS does not support dot files
+                           (concat home-dir "_emacs"))
+                          ((not (eq system-type 'windows-nt))
+                           ;; Everything sane, just use .emacs
+                           (concat home-dir ".emacs"))
+                          ((or (file-exists-p (concat home-dir ".emacs"))
+                               (file-exists-p (concat home-dir ".emacs.elc")))
+                           ;; Prefer .emacs on Windows.
+                           (concat home-dir ".emacs"))
+                          ((or (file-exists-p (concat home-dir "_emacs"))
+                               (file-exists-p (concat home-dir "_emacs.elc")))
+                           ;; Support _emacs for compatibility, but warn.
+                          (push '(initialization
+                                  "`_emacs' init file is deprecated, please 
use `.emacs'")
+                                delayed-warnings-list)
+                           (concat home-dir "_emacs"))
+                         (t ;; But default to .emacs if _emacs does not exist.
+                          (concat home-dir "_emacs")))))
+                    ;; This tells `load' to store the file name found
+                    ;; into user-init-file.
+                    (setq user-init-file t)
+                    (load user-init-file-1 t t)
+
+                    (when (eq user-init-file t)
+                      (when (eq system-type 'ms-dos)
+                        ;; If we did not find ~/.emacs, try ~/.emacs.d/init.el.
+                        (load (concat "~" home-dir "/.emacs.d/init.el")) t t)
+                      ;; If we did not find the user's init file, set
+                      ;; user-init-file conclusively.  Don't let it be set from
+                      ;; default.el.
+                      (when (eq user-init-file t)
+                        (setq user-init-file user-init-file-1)))
+
+                    ;; If we loaded a compiled file, set
+                    ;; `user-init-file' to the source version if that
+                    ;; exists.
+                    (when (and user-init-file
+                               (equal (file-name-extension user-init-file)
+                                      "elc"))
+                      (let* ((source (file-name-sans-extension user-init-file))
+                             (alt (concat source ".el")))
+                        (setq source (cond ((file-exists-p alt) alt)
+                                           ((file-exists-p source) source)
+                                           (t nil)))
+                        (when source
+                          (when (file-newer-than-file-p source user-init-file)
+                            (message "Warning: %s is newer than %s"
+                                     source user-init-file)
+                            (sit-for 1))
+                          (setq user-init-file source))))
+
+                    (unless inhibit-default-init
+                      (let ((inhibit-startup-screen nil))
+                        ;; Users are supposed to be told their rights.
+                        ;; (Plus how to get help and how to undo.)
+                        ;; Don't you dare turn this off for anyone
+                        ;; except yourself.
+                        (load "default" t t)))))))))
        (if init-file-debug
            ;; Do this without a condition-case if the user wants to debug.
            (funcall inner)


This moves the whole checking of ~ vs. ~USER to a single place (where
home-dir is set).

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +--<address@hidden>--<xmpp:address@hidden>--ooO--(_)--Ooo--



reply via email to

[Prev in Thread] Current Thread [Next in Thread]