[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,8 +1459,8 @@
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,51 +1495,47 @@
- `-': 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)) (stashstat (eshell-command-result git-stash-status-command))
(stashstat (eshell-command-result git-stash-status-command)) (detached (s-contains? "HEAD detached" status))
(detached (s-contains? "HEAD detached" status)) (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)) (pullable (s-contains? "git pull" status))
(stash (not (null stashstat))) (pushable (s-contains? "git push" status))
(pullable (s-contains? "git pull" status)) (branch (replace-regexp-in-string "On Branch \\(.*\\)\n\\(.\\|\n\\)*" "\\1" status))
(pushable (s-contains? "git push" status)) (branch (unless (or (string= "master" branch)
(branch (replace-regexp-in-string "On Branch \\(.*\\)\n\\(.\\|\n\\)*" "\\1" status)) (string= "main" branch)
(branch (if (or (string= "master" branch) detached)
(string= "main" branch)) branch)))
nil (let ((prompt (concat " "
(if (s-contains? " " branch) (if detached ">" "")
nil (when branch (concat " " branch " "))
branch)))) (when dirty "*")
(let ((prompt (concat " " (when staged "~")
(if detached ">" "") (when untracked "…")
(if branch (concat " " branch " ")) (cond ((and pullable pushable) "±")
(if dirty "*") (pullable "-")
(if staged "~") (pushable "+"))
(if untracked "…") (when stashstat "$")
(cond ((and pullable pushable) "±") " "))
(pullable "-") (accent (cond
(pushable "+")) (dirty phundrak/nord11)
(if stash "$") (staged phundrak/nord13)
" ")) (t phundrak/nord14)))
(accent (cond (background (phundrak/var-or-if-nil $background-color
(dirty phundrak/nord11) phundrak/nord0)))
(staged phundrak/nord13) (concat (with-face ""
(t phundrak/nord14))) :background accent
(background (phundrak/var-or-if-nil $background-color :foreground background)
phundrak/nord0))) (with-face prompt
(concat (with-face "" :background accent
:background accent :foreground (if dirty phundrak/nord6 background))
:foreground background) (with-face ""
(with-face prompt :background background
:background accent :foreground accent)))))
:foreground (if dirty phundrak/nord6 background))
(with-face ""
:background background
: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~
@ -1993,25 +1989,24 @@
Finally, a lambda character is displayed, either in blue or in Finally, a lambda character is displayed, either in blue or in
red depending on if the last eshell command was a success or a red depending on if the last eshell command was a success or a
failure respectively." failure respectively."
(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 "λ "
(with-face " " :background $background)
(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
@ -2061,12 +2056,12 @@
An ID consists of two parts separated by a colon: An ID consists of two parts separated by a colon:
- a prefix - a prefix
- a unique part that will be created according to - a unique part that will be created according to
`org-id-method'. `org-id-method'.
PREFIX can specify the prefix, the default is given by the PREFIX can specify the prefix, the default is given by the
variable `org-id-prefix'. However, if PREFIX is the symbol variable `org-id-prefix'. However, if PREFIX is the symbol
`none', don't use any prefix even if `org-id-prefix' specifies `none', don't use any prefix even if `org-id-prefix' specifies
one. one.
So a typical ID could look like \"Org-4nd91V40HI\"." So a typical ID could look like \"Org-4nd91V40HI\"."
@ -2074,8 +2069,8 @@
"" ""
(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
'(uuidgen uuid)) '(uuidgen uuid))
@ -2084,11 +2079,11 @@
(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 "@"
(message-make-fqdn)))))) (message-make-fqdn))))))
(setq unique (concat etime postfix)))) (setq unique (concat etime postfix))))
(t (error "Invalid `org-id-method'"))) (t (error "Invalid `org-id-method'")))
(concat prefix (car (split-string unique "-"))))) (concat prefix (car (split-string unique "-")))))
@ -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