[vim/vim] add small inner trim function (#1280)

147 views
Skip to first unread message

Bukn

unread,
Nov 25, 2016, 8:41:12 AM11/25/16
to vim/vim

I add a small inner function just like php's trim or python's strip.
If merged, in vim script, we would be able to use
let str = trim(" some_string_with_spaces_quoted \t\t ")
to trim the spaces. And str would be "some_string_with_spaces_quoted"
If we want to trim other characters, just add the second parameter.
let str = trim("xxxyyyJUST NEED THISzzzwww", "wxyz")
We would got s = "JUST NEED THIS"
I'm a 7-yeared vim user. This is my first commit. Thank you for you guys built such a great editor.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/1280

Commit Summary

  • add small inner trim function

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

Justin M. Keyes

unread,
Nov 25, 2016, 8:54:11 AM11/25/16
to vim/vim

Needs tests. https://github.com/vim/vim/blob/master/src/testdir/README.txt

I am often annoyed that trim() does not exist. It can be achieved with substitute(), if one is careful, but it's still annoying. So I would welcome this change.

Bukn

unread,
Nov 25, 2016, 9:15:59 AM11/25/16
to vim/vim, Push

@bukn pushed 1 commit.


You are receiving this because you are subscribed to this thread.

View it on GitHub

mattn

unread,
Nov 25, 2016, 9:20:52 AM11/25/16
to vim/vim

Needs tests.

and doc.

mattn

unread,
Nov 25, 2016, 9:29:35 AM11/25/16
to vim/vim

This trim doesn't support multi-byte.

Bukn

unread,
Nov 25, 2016, 9:54:39 AM11/25/16
to vim/vim, Push

@bukn pushed 1 commit.

  • 0edca28 add test for new trim inner function


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bukn

unread,
Nov 25, 2016, 9:57:50 AM11/25/16
to vim/vim

Test added. But simple.
Can someone give me a few instructions on how to add docs?
And I will work on multibyte support

Naruhiko Nishino

unread,
Nov 25, 2016, 10:15:32 AM11/25/16
to vim/vim

Can someone give me a few instructions on how to add docs?

https://github.com/vim/vim/pull/1277/files

Okey. Please refer to changes of This PR.

Naruhiko Nishino

unread,
Nov 25, 2016, 10:18:40 AM11/25/16
to vim/vim

And I will work on multibyte support

Please show me your Vim script codes working on multibyte support and you should add multibyte test.

mattn

unread,
Nov 25, 2016, 10:21:25 AM11/25/16
to vim/vim

Justin M. Keyes

unread,
Nov 25, 2016, 10:25:22 AM11/25/16
to vim/vim

This should have at least 30 different test cases. There are many possible input variations.

mattn

unread,
Nov 25, 2016, 10:25:32 AM11/25/16
to vim/vim

Oops, my patch is wrong. will fix soon.

mattn

unread,
Nov 25, 2016, 10:27:48 AM11/25/16
to vim/vim

fixed my patch. feel free to use.

Bukn

unread,
Nov 25, 2016, 11:00:22 AM11/25/16
to vim/vim

How can I merge mattn's patch ? I'm still thinking on how to test multibyte. Maybe test Chinese characters, since I'm from China.

mattn

unread,
Nov 25, 2016, 11:01:58 AM11/25/16
to vim/vim

mattn

unread,
Nov 25, 2016, 11:36:55 AM11/25/16
to vim/vim

Sorry again. please wait merging. my patch still wrong.

mattn

unread,
Nov 25, 2016, 11:49:26 AM11/25/16
to vim/vim

Should be fixed.

Bukn

unread,
Nov 27, 2016, 5:55:57 AM11/27/16
to vim/vim, Push

@bukn pushed 12 commits.


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bukn

unread,
Nov 27, 2016, 5:59:52 AM11/27/16
to vim/vim

Hope the multibytes test cases are enough.

Bukn

unread,
Nov 27, 2016, 6:25:50 AM11/27/16
to vim/vim, Push

@bukn pushed 1 commit.


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bukn

unread,
Nov 27, 2016, 6:26:31 AM11/27/16
to vim/vim

Doc added~

mattn

unread,
Nov 27, 2016, 7:03:43 AM11/27/16
to vim/vim

please add more three tests.

  • trim("", "")
  • trim("", "a")
  • trim("a", "")

Bukn

unread,
Nov 27, 2016, 7:24:53 AM11/27/16
to vim/vim, Push

@bukn pushed 1 commit.

  • d994fe1 add empty string test cases


You are receiving this because you are subscribed to this thread.

View it on GitHub

K.Takata

unread,
Nov 27, 2016, 8:56:43 AM11/27/16
to vim/vim

@k-takata commented on this pull request.


In runtime/doc/eval.txt:

> @@ -2824,8 +2826,8 @@ char2nr({expr}[, {utf8}])					*char2nr()*
 			char2nr("ABC")		returns 65
 <		When {utf8} is omitted or zero, the current 'encoding' is used.
 		Example for "utf-8": >
-			char2nr("�")		returns 225
-			char2nr("�"[0])		returns 195
+			char2nr("?")		returns 225
+			char2nr("?"[0])		returns 195

This change is not needed.
It's better to set enc=utf-8 when you edit eval.txt.

mattn

unread,
Nov 27, 2016, 9:08:19 AM11/27/16
to vim/vim

@mattn commented on this pull request.


In runtime/doc/eval.txt:

> @@ -2824,8 +2826,8 @@ char2nr({expr}[, {utf8}])					*char2nr()*
 			char2nr("ABC")		returns 65
 <		When {utf8} is omitted or zero, the current 'encoding' is used.
 		Example for "utf-8": >
-			char2nr("�")		returns 225
-			char2nr("�"[0])		returns 195
+			char2nr("?")		returns 225
+			char2nr("?"[0])		returns 195

should be latin-1. :e ++enc=latin-1 runtime/doc/eval.txt

Bukn

unread,
Nov 27, 2016, 11:53:04 AM11/27/16
to vim/vim, Push

@bukn pushed 1 commit.


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bukn

unread,
Nov 28, 2016, 8:24:47 AM11/28/16
to vim/vim

@bukn commented on this pull request.


In runtime/doc/eval.txt:

> @@ -2824,8 +2826,8 @@ char2nr({expr}[, {utf8}])					*char2nr()*
 			char2nr("ABC")		returns 65
 <		When {utf8} is omitted or zero, the current 'encoding' is used.
 		Example for "utf-8": >
-			char2nr("�")		returns 225
-			char2nr("�"[0])		returns 195
+			char2nr("?")		returns 225
+			char2nr("?"[0])		returns 195

fixed


You are receiving this because you are subscribed to this thread.

Bukn

unread,
Nov 28, 2016, 8:25:22 AM11/28/16
to vim/vim

How to pass the first check? It seemed that not this pr's problem.

K.Takata

unread,
Nov 28, 2016, 10:21:14 AM11/28/16
to vim/vim

How to pass the first check?

Do you mean coveralls?
You don't need to worry about it if your test covers the newly written part. (Sometimes coveralls reports unexpected result.)
However seeing the result, f_trim() doesn't seem to be executed. Something missing with your test?

Ismael Santos Kafeltz

unread,
Nov 28, 2016, 11:33:43 AM11/28/16
to vim/vim

I'm just curious why this function didn't exists all this vim lifetime so far.

LCD 047

unread,
Nov 28, 2016, 12:05:53 PM11/28/16
to vim/vim

I'm just curious why this function didn't exists all this vim lifetime so far.

Because it isn't that hard to get the same effect without it:

let trimmed = substitute('xxxyyyJUST NEED THISzzzwww', '\m^[wxyz]*\(.\{-}\)[wxyz]*$', '\1', '')

or

let trimmed = matchstr('xxxyyyJUST NEED THISzzzwww', '\m^[wxyz]*\zs.\{-}\ze[wxyz]*$')

mattn

unread,
Nov 28, 2016, 12:52:06 PM11/28/16
to vim/vim

As you know, it's difficult to use substitute in map().

:echo map(list, "sustitute(v:val, '\\m^[wxyz]*\\zs.\{-}\\ze[wxyz]*$')")

but trim is

:echo map(list, "trim(v:val, 'wxyz')")

Christian Brabandt

unread,
Nov 28, 2016, 1:16:39 PM11/28/16
to vim/vim

well, lambda expressions make it readable again. Also, while I agree that the pattern becomes hard to read, you could always store the pattern in a separate variable and use that one explicitly in the map() call.

That doesn't mean, I am against a trim() function

LCD 047

unread,
Nov 28, 2016, 1:30:02 PM11/28/16
to vim/vim

As you know, it's difficult to use substitute in map().

Sure, then you can do something like this:

function! Trim(what, junk)
    let rx = '[' . substitute(a:junk, '\m\([]^\\-]\)', '\\\1', 'g') . ']*'
    return matchstr(a:what, '\m^' . rx . '\zs.\{-}\ze' . rx . '$')
endfunction

let trimmed_list = map(list, "Trim(v:val, 'wxyz')")

Which isn't to say I'm against adding a trim() function, either. Just pointing out that missing it until now wasn't a crippling issue. There are other functions that are much harder to replace.

Bukn

unread,
Nov 29, 2016, 8:57:24 PM11/29/16
to vim/vim

However seeing the result, f_trim() doesn't seem to be executed. Something missing with your test?

I added test followed this instruction https://github.com/vim/vim/blob/master/src/testdir/README.txt
It says:
2) Add test_.vim to NEW_TESTS in Make_all.mak in alphabetical order.
But I saw that in Make_all.mak, NEW_TESTS section, the cases are all named with .res postfix. Is this the problem? And is it a little mistake of the document ?

Bukn

unread,
Nov 29, 2016, 8:58:30 PM11/29/16
to vim/vim

Bukn

unread,
Nov 29, 2016, 9:07:11 PM11/29/16
to vim/vim, Push

@bukn pushed 1 commit.

  • 01ed05d .vim->.res in Make_all.mak


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bram Moolenaar

unread,
Nov 30, 2016, 4:23:44 PM11/30/16
to vim/vim

Bukn wrote:

> > However seeing the result, f_trim() doesn't seem to be executed. Something missing with your test?
>
> I added test followed this instruction https://github.com/vim/vim/blob/master/src/testdir/README.txt
> It says:
>
> > 2) Add test_.vim to NEW_TESTS in Make_all.mak in alphabetical order.

>
> But I saw that in Make_all.mak, NEW_TESTS section, the cases are all
> named with .res postfix. Is this the problem? And is it a little
> mistake of the document ?

The README is wrong.


--
hundred-and-one symptoms of being an internet addict:
59. Your wife says communication is important in a marriage...so you buy
another computer and install a second phone line so the two of you can
chat.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


You are receiving this because you are subscribed to this thread.

Bukn

unread,
Dec 1, 2016, 8:02:07 PM12/1/16
to vim/vim, Push

@bukn pushed 1 commit.


You are receiving this because you are subscribed to this thread.

View it on GitHub

Bukn

unread,
Dec 7, 2016, 11:11:23 PM12/7/16
to vim/vim

Ah, is there anything I need to improve? How to know vim's new feature merge plan?


You are receiving this because you are subscribed to this thread.

K.Takata

unread,
Dec 8, 2016, 12:10:13 AM12/8/16
to vim/vim

Your patch has been already added in the todo.txt.
https://github.com/vim/vim/blob/3421566376b5723213af502bd3c2b9debe025ef1/runtime/doc/todo.txt#L192
So just wait patiently. Maybe a few weeks or a few months, (or sometimes even a few years).
Adding new feature has lower priority than fixing bugs.

How to know vim's new feature merge plan?

Only Bram knows. 😉

Bram Moolenaar

unread,
Mar 22, 2018, 6:04:49 PM3/22/18
to vim/vim, Subscribed

Closed #1280 via 295ac5a.

Reply all
Reply to author
Forward
0 new messages