@menu
* Sequencing:: Evaluation in textual order.
* Conditionals:: @code{if}, @code{cond}, @code{when}, @code{unless}.
-* Combining Conditions:: @code{and}, @code{or}, @code{not}.
+* Combining Conditions:: @code{and}, @code{or}, @code{not}, and friends.
* Pattern-Matching Conditional:: How to use @code{pcase} and friends.
* Iteration:: @code{while} loops.
* Generators:: Generic sequences and coroutines.
@section Constructs for Combining Conditions
@cindex combining conditions
- This section describes three constructs that are often used together
-with @code{if} and @code{cond} to express complicated conditions. The
+ This section describes constructs that are often used together with
+@code{if} and @code{cond} to express complicated conditions. The
constructs @code{and} and @code{or} can also be used individually as
kinds of multiple conditional constructs.
@var{arg3})} never evaluates any argument more than once.
@end defspec
+@defun xor condition1 condition2
+This function returns the boolean exclusive-or of @var{condition1} and
+@var{condition2}. That is, @code{xor} returns @code{nil} if either
+both arguments are @code{nil}, or both are non-@code{nil}. Otherwise,
+it returns the value of that argument which is non-@code{nil}.
+
+Note that in contrast to @code{or}, both arguments are always evaluated.
+@end defun
+
@node Pattern-Matching Conditional
@section Pattern-Matching Conditional
@cindex pcase
FOLLOW-SYMLINK parameter to say that symbolic links that point to
other directories should be followed.
++++
+** New function 'xor' returns the boolean exclusive-or of its args.
+The function was previously defined in array.el, but has been moved to
+subr.el so that it is available by default. It now always returns the
+non-nil argument when the other is nil. Several duplicates of 'xor'
+in other packages are now obsolete aliases of 'xor'.
+
\f
* Changes in Emacs 27.1 on Non-Free Operating Systems
((> index limit) limit)
(t index)))
-(defun xor (pred1 pred2)
- "Return the logical exclusive or of predicates PRED1 and PRED2."
- (and (or pred1 pred2)
- (not (and pred1 pred2))))
-
(defun current-line ()
"Return the current buffer line at point. The first line is 0."
(count-lines (point-min) (line-beginning-position)))
"Clear the `spam-caches' entry for a check."
(remhash symbol spam-caches))
-(defun spam-xor (a b)
- "Logical A xor B."
- (and (or a b) (not (and a b))))
+(define-obsolete-function-alias 'spam-xor 'xor "27.1")
(defun spam-set-difference (list1 list2)
"Return a set difference of LIST1 and LIST2.
(goto-char (point-min))
(dolist (article articles)
(insert (spam-get-article-as-string article)))
- (let* ((arg (if (spam-xor unregister article-is-spam-p)
+ (let* ((arg (if (xor unregister article-is-spam-p)
"-spam"
"-good"))
(status
(require 'warnings)
(require 'pcase)
(require 'ert) ; to escape a `condition-case-unless-debug'
-(require 'array) ; xor
\f
;;; Public API
\f
;;; Miscellaneous functions
+;; `xor' was added in Emacs 27.1.
+(defalias 'org-xor
+ (if (fboundp 'xor)
+ #'xor
+ (lambda (a b)
+ "Exclusive or."
+ (if a (not b) b))))
+
(defun org-version-check (version feature level)
(let* ((v1 (mapcar 'string-to-number (split-string version "[.]")))
(v2 (mapcar 'string-to-number (split-string emacs-version "[.]")))
(char-to-string (string-to-number byte 16)))
(cdr (split-string hex "%")) ""))
-(defun org-xor (a b)
- "Exclusive or."
- (if a (not b) b))
-
(defun org-fixup-message-id-for-http (s)
"Replace special characters in a message id, so it can be used in an http query."
(when (string-match "%" s)
(dotimes (y 5x5-grid-size)
(dotimes (x 5x5-grid-size)
(5x5-set-cell xored y x
- (5x5-xor (5x5-cell current y x)
- (5x5-cell best y x)))))
+ (xor (5x5-cell current y x)
+ (5x5-cell best y x)))))
(5x5-mutate-solution xored)))
(defun 5x5-mutate-solution (solution)
;; Support functions
-(defun 5x5-xor (x y)
- "Boolean exclusive-or of X and Y."
- (and (or x y) (not (and x y))))
+(define-obsolete-function-alias '5x5-xor 'xor "27.1")
(defun 5x5-y-or-n-p (prompt)
"5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting."
;;; Sorting
-(defsubst proced-xor (b1 b2)
- "Return the logical exclusive or of args B1 and B2."
- (and (or b1 b2)
- (not (and b1 b2))))
+(define-obsolete-function-alias 'proced-xor 'xor "27.1")
(defun proced-sort-p (p1 p2)
"Predicate for sorting processes P1 and P2."
(k2 (cdr (assq (car sorter) (cdr p2)))))
;; if the attributes are undefined, we should really abort sorting
(if (and k1 k2)
- (proced-xor (funcall (nth 1 sorter) k1 k2)
- (nth 2 sorter))))
+ (xor (funcall (nth 1 sorter) k1 k2)
+ (nth 2 sorter))))
(let ((sort-list proced-sort-internal) sorter predicate k1 k2)
(catch 'done
(while (setq sorter (pop sort-list))
(if (and k1 k2)
(funcall (nth 1 sorter) k1 k2)))
(if (not (eq predicate 'equal))
- (throw 'done (proced-xor predicate (nth 2 sorter)))))
+ (throw 'done (xor predicate (nth 2 sorter)))))
(eq t predicate)))))
(defun proced-sort (process-alist sorter descend)
(let ((bpl (or bpl idlwave-shell-bp-alist)) disabled modified)
(while bpl
(setq disabled (idlwave-shell-bp-get (car bpl) 'disabled))
- (when (idlwave-xor (not disabled) (eq enable 'enable))
+ (when (xor (not disabled) (eq enable 'enable))
(idlwave-shell-toggle-enable-current-bp
(car bpl) (if (eq enable 'enable) 'enable 'disable) no-update)
(push (car bpl) modified))
;; FIXME: Dynamically scoped vars need to use the `idlwave-' prefix.
;; (defvar type)
-(defmacro idlwave-xor (a b)
- `(and (or ,a ,b)
- (not (and ,a ,b))))
+
+(define-obsolete-function-alias 'idlwave-xor 'xor "27.1")
(defun idlwave-routine-entry-compare (a b)
"Compare two routine info entries for sorting.
;; Now: follow JD's ideas about sorting. Looks really simple now,
;; doesn't it? The difficult stuff is hidden above...
(cond
- ((idlwave-xor asysp bsysp) asysp) ; System entries first
- ((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
+ ((xor asysp bsysp) asysp) ; System entries first
+ ((xor aunresp bunresp) bunresp) ; Unresolved last
((and idlwave-sort-prefer-buffer-info
- (idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
- ((idlwave-xor acompp bcompp) acompp) ; Compiled entries
- ((idlwave-xor apathp bpathp) apathp) ; Library before non-library
- ((idlwave-xor anamep bnamep) anamep) ; Correct file names first
- ((and idlwave-twin-class anamep bnamep ; both file names match ->
- (idlwave-xor adefp bdefp)) bdefp) ; __define after __method
- ((> anpath bnpath) t) ; Who is first on path?
- (t nil)))) ; Default
+ (xor abufp bbufp)) abufp) ; Buffers before non-buffers
+ ((xor acompp bcompp) acompp) ; Compiled entries
+ ((xor apathp bpathp) apathp) ; Library before non-library
+ ((xor anamep bnamep) anamep) ; Correct file names first
+ ((and idlwave-twin-class anamep bnamep ; both file names match ->
+ (xor adefp bdefp)) bdefp) ; __define after __method
+ ((> anpath bnpath) t) ; Who is first on path?
+ (t nil)))) ; Default
(defun idlwave-routine-source-file (source)
(if (nth 2 source)
(goto-char omark)
(cond (temp-highlight
(setq-local transient-mark-mode (cons 'only transient-mark-mode)))
- ((or (and arg (region-active-p)) ; (xor arg (not (region-active-p)))
- (not (or arg (region-active-p))))
+ ((xor arg (not (region-active-p)))
(deactivate-mark))
(t (activate-mark)))
nil))
(or (eq char ?\s)
(eq char ?*)))
-;;(defsubst strokes-xor (a b) ### Should I make this an inline function? ###
-;; "T if one and only one of A and B is non-nil; otherwise, returns nil.
-;;NOTE: Don't use this as a numeric xor since it treats all non-nil
-;; values as t including `0' (zero)."
-;; (eq (null a) (not (null b))))
-
(defsubst strokes-xpm-encode-length-as-string (length)
"Given some LENGTH in [0,62) do a fast lookup of its encoding."
(aref strokes-base64-chars length))
(declare (indent 1) (debug t))
(cons 'if (cons cond (cons nil body))))
+(defsubst xor (cond1 cond2)
+ "Return the boolean exclusive-or of COND1 and COND2.
+If only one of the arguments is non-nil, return it; otherwise
+return nil."
+ (declare (pure t) (side-effect-free error-free))
+ (cond ((not cond1) cond2)
+ ((not cond2) cond1)))
+
(defmacro dolist (spec &rest body)
"Loop over a list.
Evaluate BODY with VAR bound to each car from LIST, in turn.
(if (> (- (car forw) orig) (- orig (car back))) back forw)
(or back forw))))
-(defsubst diff-xor (a b) (if a (if (not b) a) b))
+(define-obsolete-function-alias 'diff-xor 'xor "27.1")
(defun diff-find-source-location (&optional other-file reverse noprompt)
"Find current diff location within the source file.
SRC is the variant that was found in the buffer.
SWITCHED is non-nil if the patch is already applied."
(save-excursion
- (let* ((other (diff-xor other-file diff-jump-to-old-file))
+ (let* ((other (xor other-file diff-jump-to-old-file))
(char-offset (- (point) (diff-beginning-of-hunk t)))
;; Check that the hunk is well-formed. Otherwise diff-mode and
;; the user may disagree on what constitutes the hunk
(insert (car new)))
;; Display BUF in a window
(set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
- (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
+ (diff-hunk-status-msg line-offset (xor switched reverse) nil)
(when diff-advance-after-apply-hunk
(diff-hunk-next))))))
(pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,switched)
(diff-find-source-location nil reverse)))
(set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
- (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
+ (diff-hunk-status-msg line-offset (xor reverse switched) t)))
(defun diff-kill-applied-hunks ()
(pop-to-buffer buf)
(goto-char (+ (car pos) (cdr src)))
(when buffer (next-error-found buffer (current-buffer)))
- (diff-hunk-status-msg line-offset (diff-xor reverse switched) t))))
+ (diff-hunk-status-msg line-offset (xor reverse switched) t))))
(defun diff-current-defun ()
(interactive "P")
(save-excursion
(goto-char (point-min))
- (let* ((other (diff-xor other-file diff-jump-to-old-file))
+ (let* ((other (xor other-file diff-jump-to-old-file))
(modified-buffers nil)
(style (save-excursion
(when (re-search-forward diff-hunk-header-re nil t)
the prefix argument is reversed.
When `switch-to-buffer-obey-display-actions' is non-nil,
`switch-to-buffer' commands are also supported."
- (let* ((no-select (not (eq (consp arg) windmove-display-no-select))) ; xor
+ (let* ((no-select (xor (consp arg) windmove-display-no-select))
(old-window (or (minibuffer-selected-window) (selected-window)))
(new-window)
(minibuffer-depth (minibuffer-depth))
(should (equal (macroexpand-all '(when a b c d))
'(if a (progn b c d)))))
+(ert-deftest subr-test-xor ()
+ "Test `xor'."
+ (should-not (xor nil nil))
+ (should (eq (xor nil 'true) 'true))
+ (should (eq (xor 'true nil) 'true))
+ (should-not (xor t t)))
+
(ert-deftest subr-test-version-parsing ()
(should (equal (version-to-list ".5") '(0 5)))
(should (equal (version-to-list "0.9 alpha1") '(0 9 -3 1)))