Wednesday, March 11, 2015

Re: conversion file pattern <-> regex pattern

Hi Enno!

On Fr, 06 Mär 2015, Enno wrote:

> To check if a file path 'path' matches &backupskip,
> the naive try
>
> path ~=# &backupskip
>
> does not work because &backupskip is a file pattern
> as used for autocmd events. Is there a function
> 'file2regex()' that converts &backupskip to a Vim regex
> pattern so that
>
> path ~=# file2regex(&backupskip)
>
> is true if and only if path matches &backupskip?

I am afraid there is none[1]. You would basically have write a translating
function yourself as defined at :h file-pattern. It shouldn't be too
hard.

[1] There is a function inside Vims core, that does this
file_pat_to_reg_pat() but it is not made available as VimL function.
That should be trivial to achieve:

diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -575,6 +575,7 @@ static void f_getwinposy __ARGS((typval_
static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
@@ -8143,6 +8144,7 @@ static struct fst
{"getwinvar", 2, 3, f_getwinvar},
{"glob", 1, 3, f_glob},
{"globpath", 2, 4, f_globpath},
+ {"glob2regpat", 1, 1, f_glob2regpat},
{"has", 1, 1, f_has},
{"has_key", 2, 2, f_has_key},
{"haslocaldir", 0, 0, f_haslocaldir},
@@ -12490,6 +12492,20 @@ f_globpath(argvars, rettv)
}

/*
+ * "glob2regpat()" function
+ */
+ static void
+f_glob2regpat(argvars, rettv)
+ typval_T *argvars;
+ typval_T *rettv;
+{
+ char_u *pat = get_tv_string_chk(&argvars[0]);
+
+ rettv->v_type = VAR_STRING;
+ rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
+}
+
+/*
* "has()" function
*/
static void




Best,
Christian
--
Große Schatten werfen ihre Ereignisse hinter sich.
-- Heinz Erhardt

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: