Skip to content

Commit

Permalink
Use (if|when)-let\* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pkryger committed Oct 30, 2024
1 parent 0317634 commit 7bf0537
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 115 deletions.
4 changes: 2 additions & 2 deletions modules/init-company.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
(when (or (save-match-data (looking-at "\\W"))
(= (point) (point-max)))
(cons (or (match-string 1) "") t)))))
(candidates (when-let ((repo (forge-get-repository 'full)))
(candidates (when-let* ((repo (forge-get-repository 'full)))
(cl-remove-if-not
(lambda (assignee)
(string-prefix-p arg assignee))
(mapcar (lambda (assignee)
(propertize (cadr assignee)
'full-name (caddr assignee)))
(oref repo assignees)))))
(annotation (when-let ((assignee (get-text-property 0 'full-name arg)))
(annotation (when-let* ((assignee (get-text-property 0 'full-name arg)))
(format " [%s]" assignee)))))

:config
Expand Down
30 changes: 15 additions & 15 deletions modules/init-flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ See URL `http://mypy-lang.org/'."
:predicate flycheck-buffer-saved-p
:error-explainer
(lambda (error)
(when-let ((error-code (flycheck-error-id error))
(mypy-version
(replace-regexp-in-string
"mypy \\(\\(?:[0-9]\\.\\)+[0-9]\\).*\n"
"\\1"
(shell-command-to-string "mypy --version")))
(error-codes-alist
(exordium-setf-when-nil
(alist-get (intern mypy-version)
exordium--flycheck-mypy-error-codes-alist)
(exodrium--flycheck-mypy-retrieve-error-codes
mypy-version)))
(explanation (alist-get (intern error-code)
error-codes-alist)))
(when-let* ((error-code (flycheck-error-id error))
(mypy-version
(replace-regexp-in-string
"mypy \\(\\(?:[0-9]\\.\\)+[0-9]\\).*\n"
"\\1"
(shell-command-to-string "mypy --version")))
(error-codes-alist
(exordium-setf-when-nil
(alist-get (intern mypy-version)
exordium--flycheck-mypy-error-codes-alist)
(exodrium--flycheck-mypy-retrieve-error-codes
mypy-version)))
(explanation (alist-get (intern error-code)
error-codes-alist)))
(lambda ()
(with-current-buffer standard-output
(insert explanation)
Expand Down Expand Up @@ -161,7 +161,7 @@ See URL `http://pypi.python.org/pypi/ruff'."
line-end))
:error-explainer
(lambda (error)
(when-let (error-code (flycheck-error-id error))
(when-let* (error-code (flycheck-error-id error))
(lambda ()
(flycheck-call-checker-process
'exordium-python-ruff nil standard-output t "rule" error-code)
Expand Down
60 changes: 30 additions & 30 deletions modules/init-forge.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,26 @@ USERNAME, AUTH, and HOST behave as for `ghub-request'."
(defun exordium-forge-mark-ready-for-rewiew ()
"Mark the thing (the PR) at point as ready for review."
(interactive)
(if-let ((url (forge-get-url (or (forge-post-at-point)
(forge-current-topic))))
(_ (string-match
"//\\([^/]+\\)/\\([^/]+\\)/\\([^/]+\\)/pull/\\([0-9]+\\)$"
url))
(number (match-string 4 url))
(host (car (alist-get (match-string 1 url)
forge-alist
nil nil #'string=)))
(username (magit-git-string "config"
(concat "github." host ".user")))
(id (exordium-ghub-graphql--pull-request-id
(match-string 2 url)
(match-string 3 url)
(string-to-number number)
:username username :auth 'forge :host host))
(not-a-draft-anymore
(not (exordium-ghub-grqphql--mark-pull-request-ready-for-review
id
:username username :auth 'forge :host host))))
(if-let* ((url (forge-get-url (or (forge-post-at-point)
(forge-current-topic))))
(_ (string-match
"//\\([^/]+\\)/\\([^/]+\\)/\\([^/]+\\)/pull/\\([0-9]+\\)$"
url))
(number (match-string 4 url))
(host (car (alist-get (match-string 1 url)
forge-alist
nil nil #'string=)))
(username (magit-git-string "config"
(concat "github." host ".user")))
(id (exordium-ghub-graphql--pull-request-id
(match-string 2 url)
(match-string 3 url)
(string-to-number number)
:username username :auth 'forge :host host))
(not-a-draft-anymore
(not (exordium-ghub-grqphql--mark-pull-request-ready-for-review
id
:username username :auth 'forge :host host))))
(message "PR #%s is ready for review." number)
(user-error "Nothing at point that is a PR or mark failed")))

Expand Down Expand Up @@ -167,15 +167,15 @@ Each element of TO-DELETE is in the same format as used in
(defun exordium-forge-cleanup-known-repositories ()
"Cleanup forge repositories whose worktree doesn't exist anymore."
(interactive)
(if-let ((to-delete (cl-remove-if
(lambda (repo)
(if-let ((worktree (car repo)))
(file-exists-p worktree)
t))
(forge-sql [:select [worktree githost owner name]
:from repository
:order-by [(asc owner) (asc name)]]
[worktree githost owner name]))))
(if-let* ((to-delete (cl-remove-if
(lambda (repo)
(if-let* ((worktree (car repo)))
(file-exists-p worktree)
t))
(forge-sql [:select [worktree githost owner name]
:from repository
:order-by [(asc owner) (asc name)]]
[worktree githost owner name]))))
(when (yes-or-no-p (exordium-forge-cleanup-known-repositories--question
to-delete))
(async-start
Expand All @@ -187,7 +187,7 @@ Each element of TO-DELETE is in the same format as used in
,(async-inject-variables "\\`\\(to-delete\\|forge-alist\\)\\'")
(let (results)
(dolist (repo to-delete)
(when-let ((forge-repo (forge-get-repository (cdr repo))))
(when-let* ((forge-repo (forge-get-repository (cdr repo))))
(let ((t0 (current-time))
;; Timeout is huge (10x what was the default at the
;; time of writing this) as db ops are long sometimes.
Expand Down
4 changes: 2 additions & 2 deletions modules/init-helm-projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
(defun exordium-helm-projectile--exit-helm-and-do-ag ()
"Exit helm and run ag on first selected candidate."
(interactive)
(if-let ((project (car (helm-marked-candidates))))
(if-let* ((project (car (helm-marked-candidates))))
(helm-run-after-exit #'helm-do-ag
project)
(error "No candidates selected")))
Expand All @@ -56,7 +56,7 @@
(defun exordium-helm-projectile--exit-helm-and-do-rg ()
"Exit helm and switch project to first selected candidate and run rg there."
(interactive)
(if-let ((project (car (helm-marked-candidates))))
(if-let* ((project (car (helm-marked-candidates))))
(helm-run-after-exit #'exordium-helm-projectile--switch-project-and-do-rg
project)
(error "No candidates selected")))
Expand Down
18 changes: 9 additions & 9 deletions modules/init-iwyu.el
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ arguments in `exordium-iwyu-extra-args'."
with '.h' extension) it uses the corresponding implementation, i.e., the file
with '.cpp' extension."
(interactive)
(if-let ((compile-commands-json
(cl-find-if #'file-exists-p
(mapcar (lambda (build-dir)
(concat (file-name-as-directory (concat (projectile-project-root)
build-dir))
"compile_commands.json"))
'("cmake.bld/Linux" "build" "cmake-build" "cmake-build/linux_64_static_ninja_Debug"))))
(file-name (file-name-nondirectory buffer-file-name)))
(if-let* ((compile-commands-json
(cl-find-if #'file-exists-p
(mapcar (lambda (build-dir)
(concat (file-name-as-directory (concat (projectile-project-root)
build-dir))
"compile_commands.json"))
'("cmake.bld/Linux" "build" "cmake-build" "cmake-build/linux_64_static_ninja_Debug"))))
(file-name (file-name-nondirectory buffer-file-name)))
(iwyu-start-process-for
compile-commands-json
(if (string= "h" (file-name-extension file-name))
(concat (file-name-sans-extension file-name) ".cpp")
file-name))
file-name))
(message "Cannot find compile_commands.json for this project")))


Expand Down
2 changes: 1 addition & 1 deletion modules/init-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dirs. Input is a string and output is a list of strings."
(defun exordium-browse-url-at-point ()
"Open an URL at point."
(interactive)
(when-let ((url (thing-at-point 'url)))
(when-let* ((url (thing-at-point 'url)))
(browse-url url)))

(defmacro exordium-setf-when-nil (&rest args)
Expand Down
8 changes: 4 additions & 4 deletions modules/init-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ This is a spin off https://stackoverflow.com/a/66911315/519827, but REFRESH is s
(org-mark-subtree)
(setq beg (point))
(setq end (mark)))
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result params)))
((string-match-p "graphics" result-params)))
(when-let* ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result params)))
((string-match-p "graphics" result-params)))
(org-display-inline-images nil nil beg end)))))
(add-hook 'org-babel-after-execute-hook #'exordium--org-babel-after-execute)

Expand Down
10 changes: 5 additions & 5 deletions modules/init-treesit.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
;;;; Configuration for Treesitter

(defun exordium--add-forward-ts-hook (mode)
(when-let ((ts-hook (intern (concat (symbol-name mode) "-ts-mode-hook")))
(hook (intern (concat (symbol-name mode) "-mode-hook")))
((and (symbolp ts-hook) (symbolp hook))))
(when-let* ((ts-hook (intern (concat (symbol-name mode) "-ts-mode-hook")))
(hook (intern (concat (symbol-name mode) "-mode-hook")))
((and (symbolp ts-hook) (symbolp hook))))
(add-hook ts-hook
#'(lambda ()
(run-hooks hook)))))
Expand Down Expand Up @@ -60,8 +60,8 @@
:custom
(font-lock-maximum-decoration t)
:config
(when-let ((language-name (alist-get 'ruby-mode
tree-sitter-major-mode-language-alist)))
(when-let* ((language-name (alist-get 'ruby-mode
tree-sitter-major-mode-language-alist)))
(add-to-list 'tree-sitter-major-mode-language-alist
(cons 'enh-ruby-mode language-name)))
(add-to-list 'tree-sitter-major-mode-language-alist
Expand Down
12 changes: 6 additions & 6 deletions modules/init-util-cl.t.el
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"Collect results into `exordium--test-found-obsoltete-aliases'.
This is meant to be applied as an advice around `elisp-refs--show-results'."
(when-let ((files (mapcar (lambda (result)
(string-trim-right
(string-trim-left (buffer-name (cdr result))
" \\*refs-")
"\\*"))
(cl-third args))))
(when-let* ((files (mapcar (lambda (result)
(string-trim-right
(string-trim-left (buffer-name (cdr result))
" \\*refs-")
"\\*"))
(cl-third args))))
(add-to-list 'exordium--test-found-obsolete-aliases
(cons (cl-first args) files))))

Expand Down
82 changes: 41 additions & 41 deletions modules/init-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -503,53 +503,53 @@ Otherwise escape quotes in the inner string (rationalising escaping)."
;; string delimeter: first and last two quotes are a separate sexp, and
;; the `forward-sexp' only works from the most inner quote. However,
;; in emacs-27, the `forward-sexp' works only from the most outer quote.
(when-let ((orig-start (if (region-active-p)
(let* ((pt (min (region-beginning)
(region-end)))
(c (char-after pt)))
(when (or (eq c ?\') (eq c ?\"))
pt))
(when-let ((pos (nth 8 (syntax-ppss)))
(c (char-after pos)))
(if (and (< (1+ (point-min)) pos)
(eq c (char-after (- pos 1)))
(eq c (char-after (- pos 2))))
(- pos 2)
(- pos 0)))))
(orig-quote (char-after orig-start))
(new-quote (pcase orig-quote
(?\' ?\")
(?\" ?\')))
(quote-length (if (and (eq orig-quote
(when-let* ((orig-start (if (region-active-p)
(let* ((pt (min (region-beginning)
(region-end)))
(c (char-after pt)))
(when (or (eq c ?\') (eq c ?\"))
pt))
(when-let* ((pos (nth 8 (syntax-ppss)))
(c (char-after pos)))
(if (and (< (1+ (point-min)) pos)
(eq c (char-after (- pos 1)))
(eq c (char-after (- pos 2))))
(- pos 2)
(- pos 0)))))
(orig-quote (char-after orig-start))
(new-quote (pcase orig-quote
(?\' ?\")
(?\" ?\')))
(quote-length (if (and (eq orig-quote
(char-after (+ orig-start 1)))
(eq orig-quote
(char-after (+ orig-start 2))))
3
1))
(orig-end (if (region-active-p)
(let* ((pt (max (region-beginning)
(region-end)))
(c (char-after (1- pt))))
(when (eq c orig-quote)
pt))
(save-excursion
(goto-char orig-start)
(forward-char (if (version< emacs-version "28")
0
(logand quote-length 2)))
(forward-sexp)
(forward-char (if (version< emacs-version "28")
0
(logand quote-length 2)))
(point)))))
(eq orig-quote
(char-after (+ orig-start 2))))
3
1))
(orig-end (if (region-active-p)
(let* ((pt (max (region-beginning)
(region-end)))
(c (char-after (1- pt))))
(when (eq c orig-quote)
pt))
(save-excursion
(goto-char orig-start)
(forward-char (if (version< emacs-version "28")
0
(logand quote-length 2)))
(forward-sexp)
(forward-char (if (version< emacs-version "28")
0
(logand quote-length 2)))
(point)))))
(goto-char orig-start)
(delete-char quote-length)
(insert-char new-quote quote-length)
(while (< (point) (- orig-end quote-length))
(if flip-inner
(if-let ((a-quote (pcase (char-after)
(?\' ?\")
(?\" ?\'))))
(if-let* ((a-quote (pcase (char-after)
(?\' ?\")
(?\" ?\'))))
(progn
(delete-char 1)
(insert-char a-quote))
Expand Down

0 comments on commit 7bf0537

Please sign in to comment.