From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Oleh Krehel Newsgroups: gmane.emacs.devel Subject: Question on pcase Date: Thu, 22 Oct 2015 16:46:41 +0200 Message-ID: <871tcngdv2.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1445525197 16773 80.91.229.3 (22 Oct 2015 14:46:37 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 22 Oct 2015 14:46:37 +0000 (UTC) Cc: monnier@iro.umontreal.ca To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 22 16:46:32 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZpH8I-0001rD-Dj for ged-emacs-devel@m.gmane.org; Thu, 22 Oct 2015 16:46:22 +0200 Original-Received: from localhost ([::1]:60414 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpH8I-0007wX-2R for ged-emacs-devel@m.gmane.org; Thu, 22 Oct 2015 10:46:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51220) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpH8D-0007vf-Vt for emacs-devel@gnu.org; Thu, 22 Oct 2015 10:46:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZpH88-0007DK-QT for emacs-devel@gnu.org; Thu, 22 Oct 2015 10:46:17 -0400 Original-Received: from mail-wi0-x233.google.com ([2a00:1450:400c:c05::233]:33659) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpH88-0007D1-LQ for emacs-devel@gnu.org; Thu, 22 Oct 2015 10:46:12 -0400 Original-Received: by wijp11 with SMTP id p11so35795394wij.0 for ; Thu, 22 Oct 2015 07:46:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version:content-type; bh=6L+2i2abamKH/+3N3lbMWyflSwsYLMv/3bl8Yhm65mE=; b=eoLSpboAbKYcKSvyClZidEcy4haf66V3gYVZMbN252/aaUPuv13DEpJuxbwRyidfd6 o2R/XnsXddAskplnkvgTVayDUeQqbzbMfpOhfOU07dKpaJIIdo0iR3vYAJ5Z40sAcDaz 4A23woHLAW7vxwEQOhjoDtOjTkGrpoueyjU7VyUWjNHEBsbEdqIdp/KrfMdvltCBy4ck 4VgreNDWDb93duU53pwESvTGAGcj0AYTYd/9uQ8lAv6fIE1HTvFI2aQ+Hzo4Gv4QZ/bF IWapxNBb8S82Ryl47Tkr7NvRSEUU50H1FUMyxCJevFLjJmy6Ievkyo63Y0pRdo0/JHwE MkSA== X-Received: by 10.194.205.131 with SMTP id lg3mr18013574wjc.20.1445525172136; Thu, 22 Oct 2015 07:46:12 -0700 (PDT) Original-Received: from firefly (dyn069045.nbw.tue.nl. [131.155.69.45]) by smtp.gmail.com with ESMTPSA id ka10sm17277291wjc.30.2015.10.22.07.46.11 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 22 Oct 2015 07:46:11 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::233 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:192390 Archived-At: To be frank, I'm not a fan of `pcase', and really prefer a "for dummies" coding style. But if it makes other people feel more productive, I'm fine with it as long as I can read and debug it. One piece that I'm missing currently is the ability to eval a `pcase' pattern. To explain what I mean, here's a function that I was debugging just now: (defun completion-at-point () "Perform completion on the text around point. The completion method is determined by `completion-at-point-functions'." (interactive) (let ((res (run-hook-wrapped 'completion-at-point-functions #'completion--capf-wrapper 'all))) (pcase res (`(,_ . ,(and (pred functionp) f)) (funcall f)) (`(,hookfun . (,start ,end ,collection . ,plist)) (unless (markerp start) (setq start (copy-marker start))) (let* ((completion-extra-properties plist) (completion-in-region-mode-predicate (lambda () ;; We're still in the same completion field. (let ((newstart (car-safe (funcall hookfun)))) (and newstart (= newstart start)))))) (completion-in-region start end collection (plist-get plist :predicate)))) ;; Maybe completion already happened and the function returned t. (_ (when (cdr res) (message "Warning: %S failed to return valid completion data!" (car res))) (cdr res))))) I have `res' stored in a global variable and would like to know which branch will match, and store e.g. `hookfn' etc in global variables as well. What I want is an interface that allows me to position the point like this (just so it's clear which branch I've selected): |(`(,hookfun . (,start ,end ,collection . ,plist)) and press something akin to "C-x C-e" that either says "The pattern did not match", or evaluates: (progn (setq hook-fn (car res)) (setq rest (cdr res)) (setq start (nth 0 (cdr res))) (setq end (nth 1 (cdr res))) (setq collection (nth 2 (cdr res))) (setq plist (nthcdr 3 (cdr res)))) The single function that I need is basically: (should (equal (pcase--expand-if-match '(,hookfun . (,start ,end ,collection . ,plist)) 'res) '(progn (setq hookfun (car res)) (setq start (nth 0 (cdr res))) (setq end (nth 1 (cdr res))) (setq collection (nth 2 (cdr res))) (setq plist (nthcdr 3 (cdr res)))))) Could someone please help me to implement `pcase--expand-if-match'. I think it should be trivial to do for a person that understands how `pcase' works. I don't want to learn how to write `pcase', but the above functionality would be more than enough for me to be able to read and edit its inner contents. thanks in advance, Oleh