isamert / jaro

a highly customizable xdg-open alternative
GNU General Public License v3.0
67 stars 3 forks source link

[enhancement ] Capture slice of `#:pattern' #1

Closed xxzozaxx closed 5 years ago

xxzozaxx commented 5 years ago

Hi, first of all thanks for this great application.

I found it so useful, a wonder if you have any plans to add feature of capture part of #:pattern and play around with it.

for examble: this example show neatmail(1)1 in work suppose that I have inbox with somethig like that:

N0140    [From]    [mail header]

and N140 is the mail ID, now the feature is to have something like that

(assoc
    #:pattern "N([0-9]+)"
    #:program "grap_the_full_message_program $1")

which allow you to capture the ID without N and put it in $1.

This idea have been shown by Plan9OS program called plumber(1)[2] introduced by Rop Pike but it's kind of mess, the /Rule/ of plumer written as this

# urls go to web browser
type is text
data matches '(https?|ftp|file|gopher|mailto|news|nntp|telnet|wais|prospero)://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*'
plumb to web
plumb start web $0

FootNote:

[2]: paper: http://doc.cat-v.org/plan_9/4th_edition/papers/plumb man: http://man.cat-v.org/plan_9/1/plumb

isamert commented 5 years ago

I really liked the idea, I will work on it as soon as I find some time.

Meanwhile you can use this following solution but it requires some typing and scheme. You can pass a lambda instead of a string to #:program option. That lambda should take two parameters, file name and mime type. Within that lambda you can extract the information that you want and run your program. Something like this may work for you:

(assoc
    #:pattern "N[0-9]+"
    #:program (lambda (file-path mime-type)
                (define mail-id (regexp-substitute #f (string-match "N([0-9]+)" file-path) 1))  ;; 1 is the first capture group here.
                (system* "grap_the_full_message_program" mail-id)))

string-match applies given regexp and returns a regexp object. Then we can call regexp-substitute function to replace whole text with only the first capture group.

xxzozaxx commented 5 years ago

It works. Thanks so much.

isamert commented 5 years ago

With the latest commit you can do this:

(assoc
    #:pattern "N([0-9]+)"
    #:program "grap_the_full_message_program %1")
xxzozaxx commented 5 years ago

Thanks for your efforts. I was planning to do it myself, but I did'nt find time for reading the code, and I'm a noob schemer.