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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions autoload/vital/__vital__/System/Filepath.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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はスルーしてます。

function! s:to_slash(path) abort
return tr(a:path, '\', '/')
endfunction
else
function! s:to_slash(path) abort
return a:path
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 定義してるのでそれ使うと良さそう?

function! s:from_slash(path) abort
return tr(a:path, '/', '\')
endfunction
else
function! s:from_slash(path) abort
return a:path
endfunction
endif

let s:path_sep_pattern = (exists('+shellslash') ? '[\\/]' : '/') . '\+'
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
let s:is_cygwin = has('win32unix')
Expand Down
23 changes: 23 additions & 0 deletions doc/vital/System/Filepath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ INTERFACE *Vital.System.Filepath-interface*
------------------------------------------------------------------------------
FUNCTIONS *Vital.System.Filepath-functions*

to_slash({path}) *Vital.System.Filepath.to_slash()*
Use when you quote path string for scripting languages, without
worrying about the runtime operating system is if windows or not.

e.g.
>
let code = printf(
\ '(neoclojure-init "%s")',
\ shellescape(s:F.to_slash(path)))
..
let path = s:F.from_slash(
\ s:get_path_like_string_from_something())
<

This function intentionally ignores 'shellslash' option, so that you
can simply use this function for path string not only from Vim but
also from others, such as environment vars, problem output, or really
anything.
See also: |Vital.System.Filepath.from_slash()|

from_slash({path}) *Vital.System.Filepath.from_slash()*
This is opposite to |Vital.System.Filepath.to_slash()|. See its doc.

separator() *Vital.System.Filepath.separator()*
Return OS related directory separator as string.
This returns "/" on non-Windows, or Windows and |'shellslash'| is on.
Expand Down
21 changes: 21 additions & 0 deletions test/System/Filepath.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ Describe System.Filepath
let is_case_sensitive_file_system = !(isdirectory(tolower(g:root)) && isdirectory(toupper(g:root)))
End

Describe .to_slash()
It converts / to \ only when it's necessary
Assert Equals('a/b', FP.to_slash('a/b'))
if has('win32') || has('win64')
Assert Equals('a/b', FP.to_slash('a\b'))
else
Assert Equals('a\b', FP.to_slash('a\b'))
endif
End
End

Describe .from_slash()
It converts \ to / only when it's necessary
if has('win32') || has('win64')
Assert Equals('a\b', FP.from_slash('a/b'))
else
Assert Equals('a/b', FP.from_slash('a/b'))
endif
End
End

Describe .is_absolute()
if !s:is_windows
It returns TRUE if {path} is an absolute path
Expand Down