[Emacs] Better code style, replace macro with function

`if's that had one branch are replaced with `when', and `if's which only
actual branch is the else branch are replaced by `unless'.

`phundrak/eshell-git-status' no longer verifies whether its `$path'
argument points to a git repository, that is now the role of the callee.

`phundrak/eshell-prompt' now makes use of more variables to make the
code more readable.

Some documentation is now formatted correctly
This commit is contained in:
Lucien Cartier-Tilet 2020-10-25 11:17:15 +01:00
parent e8b206ef94
commit 72edabc4f1
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -1380,19 +1380,19 @@
`(propertize ,$str 'face (list ,@$properties)))
#+END_SRC
*** ~phundrak/var-or-if-nill~
*** ~phundrak/var-or-if-nil~
:PROPERTIES:
:CUSTOM_ID: User_Configuration-Custom_functions_macros_and_variables-phundrakvar-or-if-nill-0d320f92
:CUSTOM_ID: User-Configuration-Custom-functions-macros-and-variables_-phundrak-var-or-if-nil--5ed18c8f
: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).
This simple function 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)
(defun phundrak/var-or-if-nil ($var $value)
"Return the result yield by `$value' if `$var' is `nil', return
`$var' otherwise"
`(if (null ,$var)
,@$value
,$var))
(if (null $var)
$value
$var))
#+END_SRC
*** ~phundrak/abbr-path~
@ -1459,7 +1459,7 @@
element from `$elements' is already part of `$list', it will be
ignored."
(dolist ($e $elements)
(if (not (member $e $list))
(unless (member $e $list)
(add-to-list '$list $e)))
$list)
#+END_SRC
@ -1495,7 +1495,6 @@
- `-': unpulled commits
- `-': unpushed commits
- `±': unpulled and unpushed commits"
(if (magit-toplevel $path)
(let* ((git-status-command (concat "cd " $path "; git status"))
(git-stash-status-command (concat "cd " $path "; git stash list"))
(status (eshell-command-result git-status-command))
@ -1504,26 +1503,23 @@
(dirty (s-contains? "Changes not staged for commit" status))
(staged (s-contains? "Changes to be committed" status))
(untracked (s-contains? "Untracked files" status))
(stash (not (null stashstat)))
(pullable (s-contains? "git pull" status))
(pushable (s-contains? "git push" status))
(branch (replace-regexp-in-string "On Branch \\(.*\\)\n\\(.\\|\n\\)*" "\\1" status))
(branch (if (or (string= "master" branch)
(string= "main" branch))
nil
(if (s-contains? " " branch)
nil
branch))))
(branch (unless (or (string= "master" branch)
(string= "main" branch)
detached)
branch)))
(let ((prompt (concat " "
(if detached ">" "")
(if branch (concat " " branch " "))
(if dirty "*")
(if staged "~")
(if untracked "…")
(when branch (concat " " branch " "))
(when dirty "*")
(when staged "~")
(when untracked "…")
(cond ((and pullable pushable) "±")
(pullable "-")
(pushable "+"))
(if stash "$")
(when stashstat "$")
" "))
(accent (cond
(dirty phundrak/nord11)
@ -1539,7 +1535,7 @@
:foreground (if dirty phundrak/nord6 background))
(with-face ""
:background background
:foreground accent))))))
:foreground accent)))))
#+END_SRC
*** ~phundrak/fill-paragraph~
@ -1605,10 +1601,10 @@
(defun phundrak/is-dir-a-git-repo ($path)
"Return `t' if `$path' points to a git repository or one of its
subdirectories"
(cond
((null $path) nil)
((f-dir? (concat $path "/.git")) t)
(t (phundrak/is-dir-a-git-repo (f-parent $path)))))
(when $path
(if (f-dir? (concat $path "/.git"))
t
(phundrak/is-dir-a-git-repo (f-parent $path)))))
#+END_SRC
*** ~phundrak/yas-rust-new-assignments~
@ -1996,22 +1992,21 @@
(let* ((header-bg phundrak/nord0)
($path (phundrak/abbr-path (eshell/pwd)))
($abbr-path (phundrak/abbr-path $path phundrak/prompt--abbreviate))
($background phundrak/nord1))
(concat (with-face " " :background $background)
(with-face $abbr-path
:foreground phundrak/nord14
($background phundrak/nord1)
($foreground phundrak/nord14)
($success phundrak/nord10)
($error phundrak/nord11))
(concat (with-face (concat " " $abbr-path " ")
:foreground $foreground
:background $background)
;; add git status if it exists
(with-face " " :background $background)
(if (phundrak/is-dir-a-git-repo $path)
(phundrak/eshell-git-status $path $background))
(with-face " " :background $background)
(with-face "λ"
(when (phundrak/is-dir-a-git-repo $path)
(concat (phundrak/eshell-git-status $path $background)
(with-face " " :background $background)))
(with-face "λ "
:foreground (if (zerop eshell-last-command-status)
phundrak/nord10
phundrak/nord11)
$success
$error)
:background $background)
(with-face " " :background $background)
(with-face "" :foreground $background)
" ")))
#+END_SRC
@ -2074,7 +2069,7 @@
""
(concat (or prefix org-id-prefix)
"-"))) unique)
(if (equal prefix "-")
(when (equal prefix "-")
(setq prefix ""))
(cond
((memq org-id-method
@ -2084,7 +2079,7 @@
(setq unique (org-id-uuid))))
((eq org-id-method 'org)
(let* ((etime (org-reverse-string (org-id-time-to-b36)))
(postfix (if org-id-include-domain
(postfix (when org-id-include-domain
(progn
(require 'message)
(concat "@"
@ -3793,7 +3788,7 @@
(cond ((char-or-string-p $input-string) (insert $input-string))
((listp $input-string) (dolist (elem $input-string)
(insert (format "%s\n" elem)))))
(if (not $switchbuf)
(unless $switchbuf
(switch-to-buffer oldbuf))))
#+END_SRC