emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Instead of pcase


From: Richard Stallman
Subject: Re: Instead of pcase
Date: Mon, 27 Nov 2023 21:46:09 -0500

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

    (defun ffap-other-window (filename)
      "Like `ffap', but put buffer in another window.
    Only intended for interactive use."
      (interactive (list (ffap-prompter nil " other window")))
      (pcase (save-window-excursion (find-file-at-point filename))
        ((or (and (pred bufferp) b) `(,(and (pred bufferp) b) . ,_))
         (switch-to-buffer-other-window b))))

I had to struggle to understand that use of pcase.
Once I understood it, I found these ways to rewrite it.

  (cond* (:bind b1 (b (save-window-excursion (find-file-at-point filename))))
         ((bufferp b) (setq b1 b))
         ((bufferp (car-safe b)) (setq b1 (car-safe b))))
  (if b1 (switch-to-buffer-other-window b)))

  (cond* (:bind b1 (b (save-window-excursion (find-file-at-point filename))))
         ((bufferp b) (switch-to-buffer-other-window b))
         ((bufferp (car-safe b)) (switch-to-buffer-other-window (car-safe b)))))

I concluded it was essirable to give the macro `match-set' an optional third
argument, which is an expression that does the job of what pcase calls
a "guard".  It is an ordinary Lisp expression.

Also, to make `match-set' return t if it succeeded in matching.

Thus I came up with this.

  (let ((b (save-window-excursion (find-file-at-point filename))) b1)
    (if (or (match-set b1 b (bufferp b1))
            (match-set b1 (car-safe b) (bufferp b1)))
        (switch-to-buffer-other-window b1)))

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]