Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Filepath: Add to_slash() and from_slash() #586

Merged
merged 5 commits into from
Jun 11, 2018
Merged

Conversation

ujihisa
Copy link
Member

@ujihisa ujihisa commented Jun 10, 2018

@mattn++ and @lambdalisue++

3年前から眠っていた #358 を古の眠りから覚まして改めて作ってみました。
mattnさんのコミットをベースに、document書いてtest追加したものがこちらとなっております。

#358 の方針のとおり。名前変えようという話もあったけど、3年そのままだったし、このままでまずマージするのがよさそうな気がしてます。

Copy link
Member

@tyru tyru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この件気になってたので @ujihisa++。方針的には #358 でも言ったけど賛成です。いくつか指摘したのでお願いします。

return substitute(a:path, '/', '\', 'g')
endif
return a:path
endfunction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go の path/filepath.ToSlash() の実装を見てみました。
Go のソースコードは BSD なので、厳密にいうと ujihisa さんが見てしまうとライセンス違反な気もするので、自分が日本語で説明します。

Go の実装は、vital のコードでいうと s:separator() ==# '/' ならそのまま return、そうでないなら s:separator()/ に置換する感じです (ちなみに置換は tr() 使うと \V でエスケープ不要になるのと短くなるので良さそう)。
Go のはおそらくパスセパレータが /\ だけじゃない環境も考慮してるんだと思います (VMS とか古い Mac とか : だった気がする。まぁ vital でそこまでやるかという話ではありますが…)。
path/filepath.FromSlash() はセパレータが / なら return するのは同じですが、 / -> s:separator() の逆パターンですね。

あと、スクリプトのトップレベルでセパレータ文字列をチェックして、 / なら引数そのまま return な function を定義すると無駄なオーバーヘッドが無くなって良さそうです。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2コミット積んで対応しました。(VMSと古いMacの対応はとくになし)

* Switch function definition itself, assuming the OS doesn't change while Vim is up
* Thanks @tyru!
@@ -6,6 +6,26 @@
let s:save_cpo = &cpo
set cpo&vim

if has('win32') || has('win64')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下で s:is_windows 定義してるのでそれ使うと良さそう?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(windowsに詳しくないのでwindowsに詳しい人の意見欲しい) それだとwin16とwin95も入っちゃうけど大丈夫かな?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そもそも @h-east さんによると has('win32') || has('win64')has('win32') だけでいいそうです (help にもそう書いてある)。

win32 Win32 version of Vim (MS-Windows 95 and later, 32 or
64 bits)
win64 Win64 version of Vim (MS-Windows 64 bit).
win95 Win32 version for MS-Windows 95/98/ME.

つまり win32win95win64 を含む Windows の判定として使えます (今調べて気付いた)。
そして win16 に至ってはそもそも feature-list に残ってすらいない (help は 7.4.1382, 実装は 7.4.1375, 7.4.1368, 7.4.1364 で削除済み) ので、総括すると以下の判定でいいという事になります。

修正前: let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
修正後: let s:is_windows = has('win32')

feature-list の判定の実装

そもそも has('win32') がどういう場合に non-zero になるか気になったので調べたら、以下のようになっていました。

  • win32: WIN32 マクロが定義されている場合に non-zero
  • win64: WIN64 あるいは _WIN64 マクロが定義されている場合に non-zero
  • win95: feature-list にはあるみたいですが、実装は見つけられませんでした。もしかして既にサポート切られてドキュメントが古い?

@k-takata さんが言ってたんですが、64bitであっても WIN32, _WIN32 は定義されるため、help の言う通り 64bit でも win32 だけで non-zero を返します。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.0.0029 で WinXP より前のサポートは打ち切られました。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。win95 が無いのはそのせいでしたか。
win95 が help に残ってる件について Bram に尋ねてみたら、「過去あった物なので残してる (今は常に false を返す点については追記しておく)」との事だったので、「win16 も過去あったよ」とだけ伝えました (なので復元するべき、まで伝えるべきでした…)。
https://groups.google.com/d/msg/vim_dev/owxmPRf_Pog/plmWQZLSBQAJ

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dos16, dos32, os2 も復活させるべき。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おおーなるほど、こうなってたんですね!

とりあえず派生pullreq作ってみました。主目的は上記議論のdocument化です #589
ひとまずそこではwin16, dos{16,32}, os2はスルーしてます。

endfunction
endif

if has('win32') || has('win64')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下で s:is_windows 定義してるのでそれ使うと良さそう?

@ujihisa
Copy link
Member Author

ujihisa commented Jun 11, 2018

7e54598 にて、既存の s:is_windows を使うようにしました。 s:is_windows の値そのものについての議論は #589 に任せるとして、独立して考えることができます。

現状まとめ:

Copy link
Member

@tyru tyru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :feelsgood:

@ujihisa ujihisa merged commit cf11958 into master Jun 11, 2018
@ujihisa ujihisa deleted the filepath-slash branch June 11, 2018 03:20
@ujihisa
Copy link
Member Author

ujihisa commented Jun 11, 2018

3年越しの新機能がついに入りました 👏

https://twitter.com/ujm/status/1005992629757071360

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants