From c6e95114aeaef5beb9d28355b92089defa892933 Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Fri, 1 Mar 2019 16:46:01 +0700 Subject: [PATCH] Fix #123: invalid-regexp "Trailing backslash" on Windows. Also see https://gitlab.com/bennya/shrink-path.el/issues/2. --- doom-modeline-core.el | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doom-modeline-core.el b/doom-modeline-core.el index 00807b9..ebaeeb8 100644 --- a/doom-modeline-core.el +++ b/doom-modeline-core.el @@ -517,6 +517,28 @@ If DEFAULT is non-nil, set the default mode-line for all buffers." (if (eq idx len) "\"};" "\",\n"))))) 'xpm t :ascent 'center))))) +;; Fix: invalid-regexp "Trailing backslash" while handling $HOME on Windows +(defun doom-modeline-shrink-path--dirs-internal (full-path &optional truncate-all) + "Return fish-style truncated string based on FULL-PATH. +Optional parameter TRUNCATE-ALL will cause the function to truncate the last +directory too." + (let* ((home (getenv "HOME")) + (path (replace-regexp-in-string + (s-concat "^" home "$") "~" full-path)) + (split (s-split "/" path 'omit-nulls)) + (split-len (length split)) + shrunk) + (->> split + (--map-indexed (if (= it-index (1- split-len)) + (if truncate-all (shrink-path--truncate it) it) + (shrink-path--truncate it))) + (s-join "/") + (setq shrunk)) + (s-concat (unless (s-matches? (rx bos (or "~" "/")) shrunk) "/") + shrunk + (unless (s-ends-with? "/" shrunk) "/")))) +(advice-add #'shrink-path--dirs-internal :override #'doom-modeline-shrink-path--dirs-internal) + (defun doom-modeline-buffer-file-name () "Propertized variable `buffer-file-name' based on `doom-modeline-buffer-file-name-style'." (let* ((buffer-file-name (file-local-name (or (buffer-file-name (buffer-base-buffer)) "")))