No edit summary |
(Change to TipImported template + severe manual clean) |
||
Line 1: | Line 1: | ||
{{review}} |
{{review}} |
||
+ | {{TipImported |
||
− | {{Tip |
||
|id=904 |
|id=904 |
||
+ | |previous=903 |
||
− | |title=Convenience wrapper for setline |
||
+ | |next=905 |
||
− | |created=March 29, 2005 |
+ | |created=March 29, 2005 |
|complexity=basic |
|complexity=basic |
||
− | |author=salmanhalim |
+ | |author=salmanhalim |
|version=5.7 |
|version=5.7 |
||
|rating=2/3 |
|rating=2/3 |
||
⚫ | |||
− | |text= |
||
− | Occasionally, when writing scripts, I have to use the setline functionality to change the contents of a line; more often than not, the cursor is already on the line that I want changed. Just to make life a little bit easier, I wrote a version of setline (called Setline) that does two things: |
+ | Occasionally, when writing scripts, I have to use the setline functionality to change the contents of a line; more often than not, the cursor is already on the line that I want changed. Just to make life a little bit easier, I wrote a version of setline (called Setline) that does two things: |
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | return |
||
⚫ | |||
⚫ | |||
+ | endif |
||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | return |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | endif |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
endfunction |
endfunction |
||
+ | </pre> |
||
− | Ivan Tishchenko |
||
− | , March 30, 2005 2:59 |
||
---- |
---- |
||
I wrote mine a while ago, and for some reason, wanted something that wouldn't abort with an error. Your version, however, is truer to the original. |
I wrote mine a while ago, and for some reason, wanted something that wouldn't abort with an error. Your version, however, is truer to the original. |
||
− | salmanhalim--AT--hotmail.com |
||
− | , March 30, 2005 7:40 |
||
---- |
---- |
||
− | I probably didn't realize the existence of the ternary operator back when I wrote it, so melding your idea into mine: |
+ | I probably didn't realize the existence of the ternary operator back when I wrote it, so melding your idea into mine: |
+ | <pre> |
||
− | function! ReplaceLine (...) |
+ | function! ReplaceLine (...) |
− | + | return a:0 < 1 ? 1 : setline ( a:0 > 1 ? a:2 : line( '.' ), a:1 ) |
|
− | endfunction |
+ | endfunction |
+ | </pre> |
||
− | Still does what I want (doesn't abort), but is a bit more efficient (ternary overload, of course). |
+ | Still does what I want (doesn't abort), but is a bit more efficient (ternary overload, of course). |
− | Returns: |
+ | Returns: |
− | 1 if no argument specified (this is like a failed call to setline) |
+ | 1 if no argument specified (this is like a failed call to setline) |
The return from setline (probably 0, as the line number). |
The return from setline (probably 0, as the line number). |
||
− | salmanhalim--AT--hotmail.com |
||
− | , March 30, 2005 7:45 |
||
− | ---- |
||
− | Please, writing code on one line does not make it more efficient. |
||
− | |||
− | |||
− | Sunita |
||
− | , March 31, 2005 19:42 |
||
− | ---- |
||
− | *Sigh* |
||
− | |||
− | I realize that. It's more efficient because: |
||
− | |||
− | a) it doesn't define any local variables any more (the variable was defined and used in precisely one place, so inlining it makes it more efficient) and |
||
− | |||
− | b) it doesn't use an if/then clause, but rather uses a combination of operators, which is more efficient by definition. |
||
− | |||
− | Not to continue the bickering, but I'm curious as to whether commenting on the efficiency is merited (especially when it's misplaced) -- either make it more efficient (like a previous comment). |
||
− | |||
− | In other words, provide constructive criticism, please; this isn't a forum for you to question other people's intelligence with snide remarks. |
||
− | |||
− | salmanhalim--AT--hotmail.com |
||
− | , April 1, 2005 6:57 |
||
− | ---- |
||
− | Writing your code on one line does make it more efficient, because the parser and scripting execution is rather primitive. |
||
− | |||
− | ciaranm--AT--gentoo.org |
||
− | , April 2, 2005 14:55 |
||
---- |
---- |
||
− | <!-- parsed by vimtips.py in 0.523389 seconds--> |
Revision as of 09:59, 9 December 2007
created March 29, 2005 · complexity basic · author salmanhalim · version 5.7
Occasionally, when writing scripts, I have to use the setline functionality to change the contents of a line; more often than not, the cursor is already on the line that I want changed. Just to make life a little bit easier, I wrote a version of setline (called Setline) that does two things:
1. Changes the order of the input parameters so the text for replacement is provided before the line number.
2. If no line number is provided (only one parameter) it assumes you mean the current line -- I use this mode the most.
Otherwise, it is just a wrapper for the built-in setline and calls it after setting up the line number and swapping the order of the parameters.
Here is my wrapper for the built-in setline function:
function! Setline(...) let lineno=line('.') "check arguments if (a:0 < 1) return elseif (a:0 > 1) let lineno=a:2 endif return setline (lineno, a:1) endfunction
Comments
Here is a bit simpler version of your function:
function Setline(text,...) abort let l = ( a:0>0 ? a:1 : line('.') ) return setline(a:text,l) endfunction
I wrote mine a while ago, and for some reason, wanted something that wouldn't abort with an error. Your version, however, is truer to the original.
I probably didn't realize the existence of the ternary operator back when I wrote it, so melding your idea into mine:
function! ReplaceLine (...) return a:0 < 1 ? 1 : setline ( a:0 > 1 ? a:2 : line( '.' ), a:1 ) endfunction
Still does what I want (doesn't abort), but is a bit more efficient (ternary overload, of course).
Returns:
1 if no argument specified (this is like a failed call to setline) The return from setline (probably 0, as the line number).