emacs-devel
[Top][All Lists]
Advanced

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

Question on pcase


From: Oleh Krehel
Subject: Question on pcase
Date: Thu, 22 Oct 2015 16:46:41 +0200

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



reply via email to

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