[Emacs] Code style, prompt fix for Eshell

A bug was introduced in b97bbf8 with the fix for Eshell, where the
abbreviated path was not used anymore. This commit fixes it.

This commit also introduces the new macro `phundrak/var-or-if-nil'
which is documented in the code. It has replaced some code already.

The coding style of some recent functions have been updated too,
arguments names now begin with a dollar `$' sign. This is just stylistic
and it has no influence on the code whatsoever.

Finally, some nested `if's in `phundrak/abbr-pwd' were converted in a
single `cond', which led to renaming two variables in order to avoid a
collision between the variable and the function `push' – `pull' has been
renamed accordingly.
This commit is contained in:
Lucien Cartier-Tilet 2020-10-22 12:38:05 +02:00
parent e6bb054b7a
commit 9cfa1c1ac4
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -1375,8 +1375,21 @@
create strings with faces defined as properties to the string passed as the create strings with faces defined as properties to the string passed as the
first argument. Here is how it is implemented: first argument. Here is how it is implemented:
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defmacro with-face (str &rest properties) (defmacro with-face ($str &rest $properties)
`(propertize ,str 'face (list ,@properties))) `(propertize ,$str 'face (list ,@$properties)))
#+END_SRC
*** ~phundrak/var-or-if-nill~
:PROPERTIES:
:CUSTOM_ID: User_Configuration-Custom_functions,_macros,_and_variables-phundrakvar-or-if-nill-0d320f92
:END:
This simple macro helps me return either the value ~var~ holds, or if it is
~nil~ it will return the value ~value~ holds (or will return).
#+BEGIN_SRC emacs-lisp
(defmacro phundrak/var-or-if-nil ($var &rest $value)
`(if (null ,$var)
,@$value
,$var))
#+END_SRC #+END_SRC
*** ~phundrak/abbr-pwd~ *** ~phundrak/abbr-pwd~
@ -1388,21 +1401,21 @@
path, but leaves the current one written in full. It also abbreviates the path, but leaves the current one written in full. It also abbreviates the
equivalent of the ~$HOME~ (~/home/<username>/~) directory to a simple =~=. equivalent of the ~$HOME~ (~/home/<username>/~) directory to a simple =~=.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun phundrak/abbr-pwd (&optional abbreviate path abbreviating) (defun phundrak/abbr-pwd (&optional $abbreviate $path $abbreviating)
(cond (cond
((and (null path) ((and (null $path)
abbreviating) "") $abbreviating) "")
((and abbreviate ((and $abbreviate
(= 1 (length path))) (= 1 (length $path)))
(car path)) (car $path))
((and abbreviate path) ((and $abbreviate $path)
(f-join (let* ((dir (car path)) (f-join (let* ((dir (car $path))
(first-char (s-left 1 dir))) (first-char (s-left 1 dir)))
(if (string= "." first-char) (if (string= "." first-char)
(s-left 2 dir) (s-left 2 dir)
first-char)) first-char))
(phundrak/abbr-pwd t (cdr path) t))) (phundrak/abbr-pwd t (cdr $path) t)))
(abbreviate (f-short (phundrak/abbr-pwd t (f-split (phundrak/abbr-pwd))))) ($abbreviate (f-short (phundrak/abbr-pwd t (f-split (phundrak/abbr-pwd)))))
(t (f-short (eshell/pwd))))) (t (f-short (eshell/pwd)))))
#+END_SRC #+END_SRC
@ -1446,7 +1459,7 @@
build a git prompt that will be inserted in my Eshell prompt. And just for build a git prompt that will be inserted in my Eshell prompt. And just for
shit and giggles, Ive made it so it is a powerline prompt. shit and giggles, Ive made it so it is a powerline prompt.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun phundrak/eshell-git-status ($path &optional background-color) (defun phundrak/eshell-git-status ($path &optional $background-color)
"Returns a string indicating the status of the current "Returns a string indicating the status of the current
repository if it exists. It should also append the name of the repository if it exists. It should also append the name of the
current branch if it is not `master' or `main'. The theme is current branch if it is not `master' or `main'. The theme is
@ -1476,8 +1489,8 @@
(staged (s-contains? "Changes to be committed" status)) (staged (s-contains? "Changes to be committed" status))
(untracked (s-contains? "Untracked files" status)) (untracked (s-contains? "Untracked files" status))
(stash (not (null stashstat))) (stash (not (null stashstat)))
(pull (s-contains? "git pull" status)) (pullable (s-contains? "git pull" status))
(push (s-contains? "git push" status)) (pushable (s-contains? "git push" status))
(branch (replace-regexp-in-string "On Branch \\(.*\\)\n\\(.\\|\n\\)*" "\\1" status)) (branch (replace-regexp-in-string "On Branch \\(.*\\)\n\\(.\\|\n\\)*" "\\1" status))
(branch (if (or (string= "master" branch) (branch (if (or (string= "master" branch)
(string= "main" branch)) (string= "main" branch))
@ -1491,16 +1504,17 @@
(if dirty "*") (if dirty "*")
(if staged "~") (if staged "~")
(if untracked "…") (if untracked "…")
(if (and pull push) "±" (cond ((and pullable pushable) "±")
(if pull "-" (pullable "-")
(if push "+"))) (pushable "+"))
(if stash "$") (if stash "$")
" ")) " "))
(accent (cond (accent (cond
(dirty phundrak/nord11) (dirty phundrak/nord11)
(staged phundrak/nord13) (staged phundrak/nord13)
(t phundrak/nord14))) (t phundrak/nord14)))
(background (if background-color background-color phundrak/nord0))) (background (phundrak/var-or-if-nil $background-color
phundrak/nord0)))
(concat (with-face "" (concat (with-face ""
:background accent :background accent
:foreground background) :foreground background)
@ -1949,7 +1963,7 @@
($abbr-path (phundrak/abbr-pwd phundrak/prompt--abbreviate)) ($abbr-path (phundrak/abbr-pwd phundrak/prompt--abbreviate))
($background phundrak/nord1)) ($background phundrak/nord1))
(concat (with-face " " :background $background) (concat (with-face " " :background $background)
(with-face $path (with-face $abbr-path
:foreground phundrak/nord14 :foreground phundrak/nord14
:background $background) :background $background)
;; add git status if it exists ;; add git status if it exists