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