unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Question on pcase
@ 2015-10-22 14:46 Oleh Krehel
  2015-10-22 21:19 ` Michael Heerdegen
  0 siblings, 1 reply; 51+ messages in thread
From: Oleh Krehel @ 2015-10-22 14:46 UTC (permalink / raw)
  To: emacs-devel; +Cc: monnier


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



^ permalink raw reply	[flat|nested] 51+ messages in thread

end of thread, other threads:[~2015-10-30  1:33 UTC | newest]

Thread overview: 51+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-22 14:46 Question on pcase Oleh Krehel
2015-10-22 21:19 ` Michael Heerdegen
2015-10-23  6:30   ` Eli Zaretskii
2015-10-23 11:58     ` Oleh Krehel
2015-10-23 12:23       ` Michael Heerdegen
2015-10-23 12:42         ` Oleh Krehel
2015-10-23 13:07           ` Michael Heerdegen
2015-10-23 13:26             ` Oleh Krehel
2015-10-23 13:59               ` Michael Heerdegen
2015-10-27 23:50                 ` Johan Bockgård
2015-10-30  1:33                   ` Michael Heerdegen
2015-10-23 14:23               ` Michael Heerdegen
2015-10-23 12:17     ` Michael Heerdegen
2015-10-23 12:22       ` Oleh Krehel
2015-10-23 13:26       ` Eli Zaretskii
2015-10-23 14:14         ` Michael Heerdegen
2015-10-23 14:41           ` Eli Zaretskii
2015-10-23 18:38             ` Michael Heerdegen
2015-10-23 18:43               ` Eli Zaretskii
2015-10-23 19:59               ` Przemysław Wojnowski
2015-10-23 21:01                 ` Michael Heerdegen
2015-10-23 20:23               ` Przemysław Wojnowski
2015-10-23 20:39                 ` Michael Heerdegen
2015-10-24 11:37                   ` Przemysław Wojnowski
2015-10-24  9:01               ` Alan Mackenzie
2015-10-24 12:58                 ` Stephen Berman
2015-10-24 17:47                   ` Alan Mackenzie
2015-10-24 19:10                     ` Stephen Berman
2015-10-24 19:28                       ` Alan Mackenzie
2015-10-25  0:00                         ` pcase docstring tweaks (was: Question on pcase) Michael Heerdegen
2015-10-27 14:54                           ` pcase docstring tweaks Michael Heerdegen
2015-10-27 18:57                             ` Stefan Monnier
2015-10-27 20:14                               ` Michael Heerdegen
2015-10-28  3:15                             ` Richard Stallman
2015-10-28 17:08                               ` Michael Heerdegen
2015-10-24 17:00                 ` Question on pcase Drew Adams
2015-10-24 17:22                   ` Alan Mackenzie
2015-10-24 17:36                     ` Drew Adams
2015-10-24 20:03                     ` Johan Bockgård
2015-10-24 23:11                       ` Michael Heerdegen
2015-10-26 15:55               ` Oleh Krehel
2015-10-26 16:07                 ` Michael Heerdegen
2015-10-27  8:42                   ` Oleh Krehel
2015-10-26 16:20                 ` Michael Heerdegen
2015-10-27  8:42                   ` Oleh Krehel
2015-10-27 14:27                     ` Michael Heerdegen
2015-10-27 14:47                       ` Michael Heerdegen
2015-10-28 18:05                         ` Michael Heerdegen
2015-10-29  9:44                           ` Oleh Krehel
2015-10-30  1:11                             ` Michael Heerdegen
2015-10-26 16:35                 ` Andreas Schwab

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).