if_perl.txt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. *if_perl.txt* Nvim
  2. VIM REFERENCE MANUAL by Jacques Germishuys
  3. The perl Interface to Vim *if_perl* *perl*
  4. See |provider-perl| for more information.
  5. Type |gO| to see the table of contents.
  6. ==============================================================================
  7. 1. Commands *perl-commands*
  8. *:perl*
  9. :[range]perl {stmt}
  10. Execute perl statement {stmt}. The current package is
  11. "main". A simple check if the `:perl` command is
  12. working: >
  13. :perl print "Hello"
  14. :[range]perl << [endmarker]
  15. {script}
  16. {endmarker}
  17. Execute perl script {script}.
  18. The {endmarker} after {script} must NOT be preceded by
  19. any white space.
  20. If [endmarker] is omitted, it defaults to a dot '.'
  21. like for the |:append| and |:insert| commands.
  22. Useful for including perl code in Vim scripts.
  23. Requires perl, see |script-here|.
  24. Example: >
  25. function! MyVimMethod()
  26. perl << EOF
  27. sub my_vim_method
  28. {
  29. print "Hello World!\n";
  30. }
  31. EOF
  32. endfunction
  33. To see what version of perl you have: >
  34. :perl print $^V
  35. <
  36. *:perldo*
  37. :[range]perldo {cmd} Execute perl command {cmd} for each line in the[range],
  38. with $_ being set to the test of each line in turn,
  39. without a trailing <EOL>. In addition to $_, $line and
  40. $linenr is also set to the line content and line number
  41. respectively. Setting $_ will change the text, but note
  42. that it is not possible to add or delete lines using
  43. this command.
  44. The default for [range] is the whole file: "1,$".
  45. Examples:
  46. >
  47. :perldo $_ = reverse($_);
  48. :perldo $_ = "".$linenr." => $line";
  49. One can use `:perldo` in conjunction with `:perl` to filter a range using
  50. perl. For example: >
  51. :perl << EOF
  52. sub perl_vim_string_replace
  53. {
  54. my $line = shift;
  55. my $needle = $vim->eval('@a');
  56. my $replacement = $vim->eval('@b');
  57. $line =~ s/$needle/$replacement/g;
  58. return $line;
  59. }
  60. EOF
  61. :let @a='somevalue'
  62. :let @b='newvalue'
  63. :'<,'>perldo $_ = perl_vim_string_replace($_)
  64. <
  65. *:perlfile*
  66. :[range]perlfile {file}
  67. Execute the perl script in {file}. The whole
  68. argument is used as a single file name.
  69. Both of these commands do essentially the same thing - they execute a piece of
  70. perl code, with the "current range" set to the given line range.
  71. In the case of :perl, the code to execute is in the command-line.
  72. In the case of :perlfile, the code to execute is the contents of the given file.
  73. perl commands cannot be used in the |sandbox|.
  74. To pass arguments you need to set @ARGV explicitly. Example: >
  75. :perl @ARGV = ("foo", "bar");
  76. :perlfile myscript.pl
  77. Here are some examples *perl-examples* >
  78. :perl print "Hello"
  79. :perl $current->line (uc ($current->line))
  80. :perl my $str = $current->buffer->[42]; print "Set \$str to: $str"
  81. Note that changes (such as the "use" statements) persist from one command
  82. to the next.
  83. ==============================================================================
  84. 2. The VIM module *perl-vim*
  85. Perl code gets all of its access to Neovim via the "VIM" module.
  86. Overview >
  87. print "Hello" # displays a message
  88. VIM::Msg("Hello") # displays a message
  89. VIM::SetOption("ai") # sets a vim option
  90. $nbuf = VIM::Buffers() # returns the number of buffers
  91. @buflist = VIM::Buffers() # returns array of all buffers
  92. $mybuf = (VIM::Buffers('a.c'))[0] # returns buffer object for 'a.c'
  93. @winlist = VIM::Windows() # returns array of all windows
  94. $nwin = VIM::Windows() # returns the number of windows
  95. ($success, $v) = VIM::Eval('&path') # $v: option 'path', $success: 1
  96. ($success, $v) = VIM::Eval('&xyz') # $v: '' and $success: 0
  97. $v = VIM::Eval('expand("<cfile>")') # expands <cfile>
  98. $curwin->SetHeight(10) # sets the window height
  99. @pos = $curwin->Cursor() # returns (row, col) array
  100. @pos = (10, 10)
  101. $curwin->Cursor(@pos) # sets cursor to @pos
  102. $curwin->Cursor(10,10) # sets cursor to row 10 col 10
  103. $mybuf = $curwin->Buffer() # returns the buffer object for window
  104. $curbuf->Name() # returns buffer name
  105. $curbuf->Number() # returns buffer number
  106. $curbuf->Count() # returns the number of lines
  107. $l = $curbuf->Get(10) # returns line 10
  108. @l = $curbuf->Get(1 .. 5) # returns lines 1 through 5
  109. $curbuf->Delete(10) # deletes line 10
  110. $curbuf->Delete(10, 20) # delete lines 10 through 20
  111. $curbuf->Append(10, "Line") # appends a line
  112. $curbuf->Append(10, "L1", "L2", "L3") # appends 3 lines
  113. @l = ("L1", "L2", "L3")
  114. $curbuf->Append(10, @l) # appends L1, L2 and L3
  115. $curbuf->Set(10, "Line") # replaces line 10
  116. $curbuf->Set(10, "Line1", "Line2") # replaces lines 10 and 11
  117. $curbuf->Set(10, @l) # replaces 3 lines
  118. Module Functions:
  119. *perl-Msg*
  120. VIM::Msg({msg})
  121. Displays the message {msg}.
  122. *perl-SetOption*
  123. VIM::SetOption({arg}) Sets a vim option. {arg} can be any argument that the
  124. ":set" command accepts. Note that this means that no
  125. spaces are allowed in the argument! See |:set|.
  126. *perl-Buffers*
  127. VIM::Buffers([{bn}...]) With no arguments, returns a list of all the buffers
  128. in an array context or returns the number of buffers
  129. in a scalar context. For a list of buffer names or
  130. numbers {bn}, returns a list of the buffers matching
  131. {bn}, using the same rules as Vim's internal
  132. |bufname()| function.
  133. WARNING: the list becomes invalid when |:bwipe| is
  134. used.
  135. *perl-Windows*
  136. VIM::Windows([{wn}...]) With no arguments, returns a list of all the windows
  137. in an array context or returns the number of windows
  138. in a scalar context. For a list of window numbers
  139. {wn}, returns a list of the windows with those
  140. numbers.
  141. WARNING: the list becomes invalid when a window is
  142. closed.
  143. *perl-DoCommand*
  144. VIM::DoCommand({cmd}) Executes Ex command {cmd}.
  145. *perl-Eval*
  146. VIM::Eval({expr}) Evaluates {expr} and returns (success, value) in list
  147. context or just value in scalar context.
  148. success=1 indicates that val contains the value of
  149. {expr}; success=0 indicates a failure to evaluate
  150. the expression. '@x' returns the contents of register
  151. x, '&x' returns the value of option x, 'x' returns the
  152. value of internal |variables| x, and '$x' is equivalent
  153. to perl's $ENV{x}. All |functions| accessible from
  154. the command-line are valid for {expr}.
  155. A |List| is turned into a string by joining the items
  156. and inserting line breaks.
  157. *perl-Blob*
  158. VIM::Blob({expr}) Return Blob literal string 0zXXXX from scalar value.
  159. ==============================================================================
  160. 3. VIM::Buffer objects *perl-buffer*
  161. Methods:
  162. *perl-Buffer-Name*
  163. Name() Returns the filename for the Buffer.
  164. *perl-Buffer-Number*
  165. Number() Returns the number of the Buffer.
  166. *perl-Buffer-Count*
  167. Count() Returns the number of lines in the Buffer.
  168. *perl-Buffer-Get*
  169. Get({lnum}, {lnum}?, ...)
  170. Returns a text string of line {lnum} in the Buffer
  171. for each {lnum} specified. An array can be passed
  172. with a list of {lnum}'s specified.
  173. *perl-Buffer-Delete*
  174. Delete({lnum}, {lnum}?)
  175. Deletes line {lnum} in the Buffer. With the second
  176. {lnum}, deletes the range of lines from the first
  177. {lnum} to the second {lnum}.
  178. *perl-Buffer-Append*
  179. Append({lnum}, {line}, {line}?, ...)
  180. Appends each {line} string after Buffer line {lnum}.
  181. The list of {line}s can be an array.
  182. *perl-Buffer-Set*
  183. Set({lnum}, {line}, {line}?, ...)
  184. Replaces one or more Buffer lines with specified
  185. {lines}s, starting at Buffer line {lnum}. The list of
  186. {line}s can be an array. If the arguments are
  187. invalid, replacement does not occur.
  188. ==============================================================================
  189. 4. VIM::Window objects *perl-window*
  190. Methods:
  191. *perl-Window-SetHeight*
  192. SetHeight({height})
  193. Sets the Window height to {height}, within screen
  194. limits.
  195. *perl-Window-GetCursor*
  196. Cursor({row}?, {col}?)
  197. With no arguments, returns a (row, col) array for the
  198. current cursor position in the Window. With {row} and
  199. {col} arguments, sets the Window's cursor position to
  200. {row} and {col}. Note that {col} is numbered from 0,
  201. Perl-fashion, and thus is one less than the value in
  202. Vim's ruler.
  203. Buffer() *perl-Window-Buffer*
  204. Returns the Buffer object corresponding to the given
  205. Window.
  206. ==============================================================================
  207. 5. Lexical variables *perl-globals*
  208. There are multiple lexical variables.
  209. $curwin The current Window object.
  210. $curbuf The current Buffer object.
  211. $vim A Neovim::Ext object.
  212. $nvim The same as $nvim.
  213. $current A Neovim::Ext::Current object.
  214. These are also available via the "main" package:
  215. $main::curwin The current Window object.
  216. $main::curbuf The current Buffer object.
  217. ==============================================================================
  218. vim:tw=78:ts=8:noet:ft=help:norl: