Skip to content

Commit d84aed2

Browse files
authored
Merge pull request #1557 from vim-jp/hh-update-usr_52
Update usr_52.{txt,jax}
2 parents f33dac4 + 9f65389 commit d84aed2

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

doc/usr_52.jax

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*usr_52.txt* For Vim バージョン 9.1. Last change: 2024 May 16
1+
*usr_52.txt* For Vim バージョン 9.1. Last change: 2024 May 31
22

33
VIM USER MANUAL - by Bram Moolenaar
44

@@ -14,6 +14,7 @@
1414
|52.3| インポート/エクスポートなしのオートロード
1515
|52.4| 他に使えるメカニズム
1616
|52.5| 旧来のスクリプトから Vim9 script を使う
17+
|52.6| Vim9 script サンプル: comment パッケージ, highlight-yank プラグイン
1718

1819
次章: |usr_90.txt| Vim のインストール
1920
前章: |usr_51.txt| プラグインを作る
@@ -340,6 +341,47 @@ script ではこうしたグローバルのアイテムで確実にユニーク
340341
call g:NicePluginTest()
341342
342343
==============================================================================
344+
*52.6* Vim9 script サンプル: comment パッケージ, highlight-yank プラグイン
345+
346+
☆comment パッケージ
347+
348+
Vim には、Vim9 script で記述されたコメントプラグイン |comment-install| が付属
349+
しています。
350+
$VIMRUNTIME/pack/dist/opt/comment/ にあるパッケージを確認してください。
351+
352+
☆highlight-yank プラグイン
353+
354+
ヤンクされた領域をハイライトする例を以下に示します。これは、Vim 9.1.0446 以降
355+
で使用可能な |getregionpos()| 関数を使用します。
356+
357+
以下の例を新しいファイルにコピーしてプラグインディレクトリに配置すると、次回
358+
Vim を起動したときにアクティブになります |add-plugin|: >
359+
360+
vim9script
361+
362+
def HighlightedYank(hlgroup = 'IncSearch', duration = 300, in_visual = true)
363+
if v:event.operator ==? 'y'
364+
if !in_visual && visualmode() != null_string
365+
visualmode(1)
366+
return
367+
endif
368+
var [beg, end] = [getpos("'["), getpos("']")]
369+
var type = v:event.regtype ?? 'v'
370+
var pos = getregionpos(beg, end, {type: type})
371+
var end_offset = (type == 'V' || v:event.inclusive) ? 1 : 0
372+
var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
373+
var col_beg = v[0][2] + v[0][3]
374+
var col_end = v[1][2] + v[1][3] + end_offset
375+
return [v[0][1], col_beg, col_end - col_beg]
376+
}))
377+
var winid = win_getid()
378+
timer_start(duration, (_) => m->matchdelete(winid))
379+
endif
380+
enddef
381+
382+
autocmd TextYankPost * HighlightedYank()
383+
<
384+
==============================================================================
343385

344386
次章: |usr_90.txt| Vim のインストール
345387

en/usr_52.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*usr_52.txt* For Vim version 9.1. Last change: 2024 May 16
1+
*usr_52.txt* For Vim version 9.1. Last change: 2024 May 31
22

33
VIM USER MANUAL - by Bram Moolenaar
44

@@ -13,6 +13,7 @@ smaller parts.
1313
|52.3| Autoloading without import/export
1414
|52.4| Other mechanisms to use
1515
|52.5| Using a Vim9 script from legacy script
16+
|52.6| Vim9 script examples: comment package, highlight-yank plugin
1617

1718
Next chapter: |usr_90.txt| Installing Vim
1819
Previous chapter: |usr_51.txt| Create a plugin
@@ -336,6 +337,46 @@ will have to make sure to use a unique name for these global items. Example: >
336337
call g:NicePluginTest()
337338
338339
==============================================================================
340+
*52.6* Vim9 script examples: comment package, highlight-yank plugin
341+
342+
COMMENT PACKAGE
343+
344+
Vim comes with a comment plugin, written in Vim9 script |comment-install|.
345+
Have a look at the package located at $VIMRUNTIME/pack/dist/opt/comment/
346+
347+
HIGHLIGHT YANK PLUGIN
348+
349+
Here is an example for highlighting the yanked region. It makes use of the
350+
|getregionpos()| function, available since Vim 9.1.0446.
351+
352+
Copy the following example into a new file and place it into your plugin directory
353+
and it will be active next time you start Vim |add-plugin|: >
354+
355+
vim9script
356+
357+
def HighlightedYank(hlgroup = 'IncSearch', duration = 300, in_visual = true)
358+
if v:event.operator ==? 'y'
359+
if !in_visual && visualmode() != null_string
360+
visualmode(1)
361+
return
362+
endif
363+
var [beg, end] = [getpos("'["), getpos("']")]
364+
var type = v:event.regtype ?? 'v'
365+
var pos = getregionpos(beg, end, {type: type})
366+
var end_offset = (type == 'V' || v:event.inclusive) ? 1 : 0
367+
var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
368+
var col_beg = v[0][2] + v[0][3]
369+
var col_end = v[1][2] + v[1][3] + end_offset
370+
return [v[0][1], col_beg, col_end - col_beg]
371+
}))
372+
var winid = win_getid()
373+
timer_start(duration, (_) => m->matchdelete(winid))
374+
endif
375+
enddef
376+
377+
autocmd TextYankPost * HighlightedYank()
378+
<
379+
==============================================================================
339380

340381
Next chapter: |usr_90.txt| Installing Vim
341382

0 commit comments

Comments
 (0)