[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/el-search a38665c 115/332: Add section "Patterns for st
From: |
Stefan Monnier |
Subject: |
[elpa] externals/el-search a38665c 115/332: Add section "Patterns for stylistic rewriting" and pattern `iffy-if' |
Date: |
Tue, 1 Dec 2020 15:48:24 -0500 (EST) |
branch: externals/el-search
commit a38665c83d4a25896201aa94b49c9f999e08d535
Author: Michael Heerdegen <michael_heerdegen@web.de>
Commit: Michael Heerdegen <michael_heerdegen@web.de>
Add section "Patterns for stylistic rewriting" and pattern `iffy-if'
---
el-search-x.el | 45 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/el-search-x.el b/el-search-x.el
index 7c050cf..92d2dc3 100644
--- a/el-search-x.el
+++ b/el-search-x.el
@@ -109,6 +109,51 @@ repository's HEAD commit."
`(guard (el-search--changed-p (point) ,revision)))
+;;; Patterns for stylistic rewriting
+
+;;;; Iffy `if's
+
+(defun el-search--nested-if-1 (expr)
+ ;; EXPR is a (potentially nested) `if' expression. Return a list L so
+ ;; that (cond . L) is semantically equivalent to EXPR. For example,
+ ;; when EXPR == (if x 1 (if y 2 3)), return ((x 1) (y 2) (t 3))
+ (pcase-exhaustive expr
+ (`(if ,condition ,then ,(and `(if . ,_) inner-if))
+ `((,condition ,then) ,@(el-search--nested-if-1 inner-if)))
+ (`(if ,condition ,then)
+ `((,condition ,then)))
+ (`(if ,condition ,then . ,else)
+ `((,condition ,then)
+ (t . ,else)))))
+
+(el-search-defpattern -nested-if (&optional var)
+ (let ((test-pattern '`(if ,_ ,_ (if ,_ ,_ ,_ . ,_))))
+ (if (not var) test-pattern
+ (let ((cases (make-symbol "cases")))
+ `(and ,test-pattern
+ (app el-search--nested-if-1 ,cases)
+ (let ,var `(cond . ,,cases)))))))
+
+(el-search-defpattern iffy-if (&optional var)
+ "Matches `if'-clauses that could be replaced with a more suitable form.
+
+Match `if' clauses that would fit better into either `cond',
+`when' or `unless'. With symbol VAR given, bind that to such a
+semantically equivalent expression suitable to replace the
+current match."
+ (cl-callf or var '_)
+ (let ((condition (make-symbol "condition"))
+ (then (make-symbol "then"))
+ (clauses (make-symbol "clauses")))
+ `(or (-nested-if ,var)
+ (and `(if (not ,,condition) ,,then)
+ (let ,var `(unless ,,condition ,,then)))
+ (and `(if ,,condition ,,then)
+ (let ,var `(when ,,condition ,,then)))
+ (and `(if ,,condition ,,then (cond . ,,clauses))
+ (let ,var `(cond (,,condition ,,then) . ,,clauses))))))
+
+
(provide 'el-search-x)
;;; el-search-x.el ends here
- [elpa] externals/el-search f1ebc2e 201/332: * el-search/el-search.el: Get rid of orgstruct-mode, (continued)
- [elpa] externals/el-search f1ebc2e 201/332: * el-search/el-search.el: Get rid of orgstruct-mode, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 5eb7614 206/332: Tweak el-search--get-search-description-string, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 179aa59 209/332: * el-search/el-search.el: Bump version to 1.5, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search b5b35ef 232/332: * el-search/el-search-x.el: Some more tweaks, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 63172a4 222/332: Treat inserted replacements as potential to-replace candidates, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search e0e2994 111/332: Some details, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search f26277b 117/332: Rewrite of el-search for version 1.0, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 1ef4266 114/332: Make el-search--make-docstring require a NAME argument, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 278a930 125/332: Speed up multi searching; version 1.1, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 0c2f0e5 127/332: Don't use the non-word "heuristical", Stefan Monnier, 2020/12/01
- [elpa] externals/el-search a38665c 115/332: Add section "Patterns for stylistic rewriting" and pattern `iffy-if',
Stefan Monnier <=
- [elpa] externals/el-search 8fdb91d 124/332: New command `el-search-load-path', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 4202501 148/332: el-search version 1.3: Lots of fixes and improvements, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search d54491c 147/332: Add a FILE argument to `el-search-dired-marked-files', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search a26b6e0 152/332: Reset wrap flag even when no match, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 6919e8d 141/332: Also count pattern types starting with "_" as internal, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search b8e3812 144/332: Improve half-baked introduction of key bindings, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search e96c054 156/332: Handle read errors when reading and validating replacement, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 6f0f22c 155/332: Some details, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 6e6013a 159/332: Fix and speed up the `change' pattern type, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search c878a24 173/332: Fix "Make amount of context around occur matches adjustable", Stefan Monnier, 2020/12/01