.emacs-my
changeset 1734 ae2c6a001464
parent 1733 316d6f077e1c
child 1738 1f9eaaf10690
--- a/.emacs-my	Tue Jun 15 14:54:36 2021 +0300
+++ b/.emacs-my	Wed Jun 16 12:50:08 2021 +0300
@@ -230,6 +230,25 @@
               obarray)
     (remove-if-not 'identity result) ))
 
+
+(defun my-path-append-to-beginning (pathenv path)
+  "Append PATH to the beginning of PATHENV, pruning existing entries."
+  (let* ( (pathenv (concat ":" pathenv ":"))
+	  (beg (cl-search (concat ":" path ":") pathenv))
+	  lh rh )
+    (when beg
+      (setq lh (substring pathenv 0 beg))
+      (setq rh (substring pathenv (1+ (cl-search ":" pathenv :start2 (1+ beg))) nil))
+      (setq pathenv (concat lh ":" rh)))
+    ;; pathenv keeps ":" as a prefix (OK for the next concat) and suffix (need one to strip).
+    (setq pathenv (substring pathenv 0 (1- (length pathenv))))
+    (concat path pathenv)))
+
+;; (my-path-append-to-beginning "a:b:c" "a")
+;; (my-path-append-to-beginning "a:b:c" "b")
+;; (my-path-append-to-beginning "a:b:c" "c")
+;; (my-path-append-to-beginning "a:b:c" "d")
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 (message "lisp, elisp")
 
@@ -555,13 +574,17 @@
 (when (and (eq system-type 'cygwin) (fboundp #'cygwin-winpath-activate))
   (cygwin-winpath-activate))
 
-;; Add some standard places to PATH as Emacs is not started from login shell
-;; on Windows...
-(when (eq system-type 'cygwin)
-  (mapc (lambda (path)
-          (unless (cl-search (concat ":" path ":") (concat ":" (getenv "PATH") ":"))
-            (setenv "PATH" (concat (getenv "PATH") ":" path))))
-        (list (expand-file-name "~/.local/bin") "/usr/local/bin")))
+;; Add some standard places to PATH if they are not set by login script.
+;; Rearrange the order of paths so system's are first, user's are last.
+;; For Cygwin this helps with Cygwin's paths to be situated before
+;; "C:/Windows" (Emacs is not started from a login shell on Windows!).
+(unless (eq system-type 'windows-nt)
+  (setenv "PATH"
+          (cl-reduce #'my-path-append-to-beginning
+	             (list (getenv "PATH")
+                           (expand-file-name "~/usr/bin") (expand-file-name "~/.local/bin")
+                           "/usr/local/bin" "/usr/bin" "/bin"
+                           "/usr/sbin" "/sbin")))
 
 (defun follow-cygwin-symlink ()
   "Follow new-style (and also UCS-16) Cygwin symlinks."