aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorThien-Thi Nguyen2018-05-21 18:16:35 +0200
committerThien-Thi Nguyen2018-05-27 17:14:27 +0200
commit567cb9046d098b617c76541a75516ac6ef563be7 (patch)
treecac40d9b8ea3162c5e4762d0a35baaf0d598f983 /lisp
parent4d7e54acff0869d42bfb5b95014f7e6b988666d5 (diff)
downloademacs-567cb9046d098b617c76541a75516ac6ef563be7.tar.gz
emacs-567cb9046d098b617c76541a75516ac6ef563be7.zip
Overhaul pcase documentation
Suggested by Drew Adams (Bug#31311). * doc/lispref/control.texi (Control Structures): Add "Pattern-Matching Conditional" to menu, before "Iteration". (Conditionals): Delete menu. (Pattern matching case statement): Delete node/subsection, by actually moving, renaming, and overhauling it to... (Pattern-Matching Conditional): ...new node/section. (pcase Macro): New node/subsection. (Extending pcase): Likewise. (Backquote Patterns): Likewise. * doc/lispref/elisp.texi (Top) In @detailmenu, add "Pattern-Matching Conditional" under "Control Structures" section and delete "Conditionals" section. * lisp/emacs-lisp/pcase.el (pcase): Rewrite docstring. (pcase-defmacro \` (qpat) ...): Likewise.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/emacs-lisp/pcase.el102
1 files changed, 51 insertions, 51 deletions
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 38e434de375..fa7b1de8b4d 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -110,56 +110,41 @@
110(defmacro pcase (exp &rest cases) 110(defmacro pcase (exp &rest cases)
111 "Evaluate EXP to get EXPVAL; try passing control to one of CASES. 111 "Evaluate EXP to get EXPVAL; try passing control to one of CASES.
112CASES is a list of elements of the form (PATTERN CODE...). 112CASES is a list of elements of the form (PATTERN CODE...).
113 113For the first CASE whose PATTERN \"matches\" EXPVAL,
114A structural PATTERN describes a template that identifies a class 114evaluate its CODE..., and return the value of the last form.
115of values. For example, the pattern \\=`(,foo ,bar) matches any 115If no CASE has a PATTERN that matches, return nil.
116two element list, binding its elements to symbols named `foo' and 116
117`bar' -- in much the same way that `cl-destructuring-bind' would. 117Each PATTERN expands, in essence, to a predicate to call
118 118on EXPVAL. When the return value of that call is non-nil,
119A significant difference from `cl-destructuring-bind' is that, if 119PATTERN matches. PATTERN can take one of the forms:
120a pattern match fails, the next case is tried until either a 120
121successful match is found or there are no more cases. The CODE 121 _ matches anything.
122expression corresponding to the matching pattern determines the 122 \\='VAL matches if EXPVAL is `equal' to VAL.
123return value. If there is no match the returned value is nil. 123 KEYWORD shorthand for \\='KEYWORD
124 124 INTEGER shorthand for \\='INTEGER
125Another difference is that pattern elements may be quoted, 125 STRING shorthand for \\='STRING
126meaning they must match exactly: The pattern \\='(foo bar) 126 SYMBOL matches anything and binds it to SYMBOL.
127matches only against two element lists containing the symbols 127 If a SYMBOL is used twice in the same pattern
128`foo' and `bar' in that order. (As a short-hand, atoms always 128 the second occurrence becomes an `eq'uality test.
129match themselves, such as numbers or strings, and need not be 129 (pred FUN) matches if FUN called on EXPVAL returns non-nil.
130quoted.) 130 (app FUN PAT) matches if FUN called on EXPVAL matches PAT.
131 131 (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil.
132Lastly, a pattern can be logical, such as (pred numberp), that 132 (let PAT EXPR) matches if EXPR matches PAT.
133matches any number-like element; or the symbol `_', that matches 133 (and PAT...) matches if all the patterns match.
134anything. Also, when patterns are backquoted, a comma may be 134 (or PAT...) matches if any of the patterns matches.
135used to introduce logical patterns inside backquoted patterns. 135
136 136FUN in `pred' and `app' can take one of the forms:
137The complete list of standard patterns is as follows: 137 SYMBOL or (lambda ARGS BODY)
138 138 call it with one argument
139 _ matches anything. 139 (F ARG1 .. ARGn)
140 SYMBOL matches anything and binds it to SYMBOL. 140 call F with ARG1..ARGn and EXPVAL as n+1'th argument
141 If a SYMBOL is used twice in the same pattern 141
142 the second occurrence becomes an `eq'uality test. 142FUN, BOOLEXP, EXPR, and subsequent PAT can refer to variables
143 (or PAT...) matches if any of the patterns matches. 143bound earlier in the pattern by a SYMBOL pattern.
144 (and PAT...) matches if all the patterns match.
145 \\='VAL matches if the object is `equal' to VAL.
146 ATOM is a shorthand for \\='ATOM.
147 ATOM can be a keyword, an integer, or a string.
148 (pred FUN) matches if FUN applied to the object returns non-nil.
149 (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil.
150 (let PAT EXP) matches if EXP matches PAT.
151 (app FUN PAT) matches if FUN applied to the object matches PAT.
152 144
153Additional patterns can be defined using `pcase-defmacro'. 145Additional patterns can be defined using `pcase-defmacro'.
154 146
155The FUN argument in the `app' pattern may have the following forms: 147See Info node `(elisp) Pattern-Matching Conditional' in the
156 SYMBOL or (lambda ARGS BODY) in which case it's called with one argument.
157 (F ARG1 .. ARGn) in which case F gets called with an n+1'th argument
158 which is the value being matched.
159So a FUN of the form SYMBOL is equivalent to (FUN).
160FUN can refer to variables bound earlier in the pattern.
161
162See Info node `(elisp) Pattern matching case statement' in the
163Emacs Lisp manual for more information and examples." 148Emacs Lisp manual for more information and examples."
164 (declare (indent 1) (debug (form &rest (pcase-PAT body)))) 149 (declare (indent 1) (debug (form &rest (pcase-PAT body))))
165 ;; We want to use a weak hash table as a cache, but the key will unavoidably 150 ;; We want to use a weak hash table as a cache, but the key will unavoidably
@@ -926,14 +911,29 @@ Otherwise, it defers to REST which is a list of branches of the form
926 sexp)) 911 sexp))
927 912
928(pcase-defmacro \` (qpat) 913(pcase-defmacro \` (qpat)
929 "Backquote-style pcase patterns. 914 "Backquote-style pcase patterns: \\=`QPAT
930QPAT can take the following forms: 915QPAT can take the following forms:
931 (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. 916 (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr.
932 [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match 917 [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match
933 its 0..(n-1)th elements, respectively. 918 its 0..(n-1)th elements, respectively.
934 ,PAT matches if the pcase pattern PAT matches. 919 ,PAT matches if the `pcase' pattern PAT matches.
935 ATOM matches if the object is `equal' to ATOM. 920 SYMBOL matches if EXPVAL is `equal' to SYMBOL.
936 ATOM can be a symbol, an integer, or a string." 921 KEYWORD likewise for KEYWORD.
922 INTEGER likewise for INTEGER.
923 STRING likewise for STRING.
924
925The list or vector QPAT is a template. The predicate formed
926by a backquote-style pattern is a combination of those
927formed by any sub-patterns, wrapped in a top-level condition:
928EXPVAL must be \"congruent\" with the template. For example:
929
930 \\=`(technical ,forum)
931
932The predicate is the logical-AND of:
933 - Is EXPVAL a list of two elements?
934 - Is the first element the symbol `technical'?
935 - True! (The second element can be anything, and for the sake
936 of the body forms, its value is bound to the symbol `forum'.)"
937 (declare (debug (pcase-QPAT))) 937 (declare (debug (pcase-QPAT)))
938 (cond 938 (cond
939 ((eq (car-safe qpat) '\,) (cadr qpat)) 939 ((eq (car-safe qpat) '\,) (cadr qpat))