Not able to get entire text of current buffer in vim - function

I am trying to get entire text of current buffer. I believe it is represented by '%' (see answer by SnoringFrog at https://vi.stackexchange.com/questions/2319/is-there-a-text-object-for-the-entire-buffer). However, following function gives an error:
function Check ()
echo %
endfunction
I call it with following command:
:call Check()
The error is:
Error detected while processing function Check:
line 1:
E15: Invalid expression: %
E15: Invalid expression: %
Where is the problem and how can it be solved?

Depending on the context, % can be a shortcut for the 1,$ range or a placeholder for the filename associated with the current buffer.
In the first case (the one in your link), it's not meant to be echoed at all which would be completely pointless.
In the second case, it needs to be expanded with expand('%') if you want to use it in a function.
Anyway, none of that matters because % is not what you want at all. What you want is :help getline():
function Check ()
echo getline(1,'$')
endfunction

Related

I can't define functions in Octave

It says that 'x' is not defined and I don't know what to do about it since 'x' is supposed to be an user-defined variable.
I'm new to coding and even newer to Octave and I'm aware that it's an extremely simple, basic question - foolish, even. But even so, if someone could please tell me how to code this, I'd be glad.
function value = sqrmat (x)
% returns true if x is a square matrix and false otherwise
if rows(x)==columns(x)
value=true;
else
value=false;
endif
end
Octave has "script files" and "function files". To create a "script file", use a command (or statement) that has no effect.
Put this command at the top of the file, before all defined functions. Examples:
Declare any number...
In this case, the program assigns this number to the variable "ans".
% script
1;
function value = sqrmat (x)
% returns true if x is a square matrix and false otherwise
if rows(x)==columns(x)
value=true;
else
value=false;
endif
end
A simple message...
It could be a notification, a greeting, and so on.
% script
printf("File accessed\n")

When defining a variable in a julia function, I get an error about an undefined variable on that line

Problem
I'm writing a Julia script, and in the function there is a while loop. Inside the while loop there is a variable. That line is throwing errors about the variable being undefined when in fact that is the very line defining the variable.
The code
The error is on line 65
function cleanTexLoop(fileName::String)
f = open(fileName, "r")
while ! eof(f)
line = readline(f), <-- line 65
#line = sentenceFilter(line)
println(line)
end
close(f)
end
The function opens a file which IS getting passed into a loop. The loop runs until the end of file. While looping the file is read line by line. Each time it is read the line is stored in variable line and the file advances. In the proper version, that one line (66) isn't commented out, however for debugging it is. line is then taken as input into a filter which modifies the line before storing it again as line. The final version of this script will have four filters, but for now, I'd be happy to get this to run with just zero filters.
(Note that a user has kindly pointed out the comma that after hours of looking at the code continued to allude me. I'm waiting for that user to write up an answer)
The error message
cleanTexLoop("test.tex")
ERROR: UndefVarError: line not defined
Stacktrace:
[1] cleanTexLoop(::String) at /home/nero/myScripts/latexCleaner.jl:65
[2] macro expansion at ./REPL.jl:97 [inlined]
[3] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at > ./event.jl:73
Previous working version
I had previous written another version of this which works in entirety, however I needed to make some substantial changes to its structure in order to better suit future plans. Note that some of the names aren't up to the normal naming convention. Namely, I use "!" when no variables are actually being changed.
function CleanTexLoop(fileName::String,regX::String,sub::String)
f = open(fileName, "r")
while ! eof(f)
println(applySub!(f,regX,sub))
end
close(f)
end
function applySub!(file::IOStream,regX::String,sub::String)
return replace(
readline(file),
Base.Regex(regX),
Base.SubstitutionString(sub)
)
end
A simple loop which demonstrates why this should work
x = 0
while x < 4
y = x
println(y)
x = x+1
end
As expected, this prints zero to one, and is, as far as I can tell, representative of what I am doing. In both cases I am passing some variable into the loop which, through some action, defines another variable inside the loop which is then printed. Why this works, and the other doesn't is beyond me.
What I've seen on Google.
From looking this problem up, it appears as if this problem arrises when defining variables outside of a loop, or similar environment, as a result of them failing to be passed into the environment. However, this isn't what's happening in my case. In my case the variable is being defined for the first time.
As mentioned in the comments, the problem was an errant comma.

Vim function to copy a code function to clipboard

I want to have keyboard shortcut in Vim to copy a whole function from a Powershell file to the Windows clipboard. Here is the command for it:
1) va{Vok"*y - visual mode, select {} block, visual line mode, go to selection top, include header line, yank to Windows clipboard.
But it would work only for functions without an inner {} block. Here is a valid workaround for it:
2) va{a{a{a{a{a{a{Vok"*y - the same as (1), but selecting {} block is done multiple times - would work for code blocks that have 7 inner {} braces.
But the thing is - the (1) command works fine when called from a vim function, but (2) misbehaves and selects wrong code block when called from a vim function:
function! CopyCodeBlockToClipboard ()
let cursor_pos = getpos('.')
execute "normal" 'va{a{a{a{a{a{a{Vok"*y'
call setpos('.', cursor_pos)
endfunction
" Copy code block to clipboard
map <C-q> :call CopyCodeBlockToClipboard()<CR>
What am I doing wrong here in the CopyCodeBlockToClipboard?
The (2) command works as expected when executed directly in vim.
UPDATE:
I've noticed that:
if there are more a{ then the included blocks in the function
then vim wouldn't execute V
Looks like vim handles errors differently here. Extra a{ produces some error and regular command execution just ignores it. But execution from withing a function via :normal fails and wouldn't call V (or probably any command that follows the error).
Any workaround for this?
Try this function
function! CopyCodeBlockToClipboard()
let cursor_pos = getpos('.')
let i = 1
let done = 0
while !done
call setpos('.', cursor_pos)
execute "normal" 'v' . i . 'aBVok"*y'
if mode() =~ "^[vV]"
let done = 1
else
let i = i + 1
endif
endwhile
execute "normal \<ESC>"
call setpos('.', cursor_pos)
endfunction
This preforms a execute command to select blocks until it fails to select a block larger block. ([count]aB selects [count] blocks) It seems when the selection fails we end up in visual mode. So we can use mode() to check this.
When this function exits you should be in normal mode and the cursor should be restored to where you started. And the function will be in the * register.
This macro should come close to what you want to achieve:
?Function<CR> jump to first Function before the cursor position
v enter visual mode
/{<CR> extend it to next {
% extend it to the closing }
"*y yank into the system clipboard

Trying to redirect output of a command to a variable

>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#142
>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#144
>>
I tried other stuff like catch etc, and every where, it returns #number. My goal is to be able to print the actual value instead of the number - xyz_blah_blah.
I am new to tcl. Want to understand, if this is an array or a pointer to an array or something like that. When I try the exact same thing with a different command, which returns just a value, then it works. This is a new command which returns value in parenthesis.
Please help. Thanks.
Every Tcl command produces a result value, which you capture and use by putting the call of the command in [square brackets] and putting the whole lot as part of an argument to another command. Thus, in:
set signal_name [get_fanout abc_signal]
the result of the call to get_fanout is used as the second argument to set. I suggest that you might also like to try doing this:
puts "-->[get_fanout abc_signal]<--"
It's just the same, except this time we're concatenating it with some other small string bits and printing the whole lot out. (In case you're wondering, the result of puts itself is always the empty string if there isn't an error, and set returns the contents of the variable.)
If that is still printing the wrong value (as well as the right one beforehand, without arrow marks around it) the real issue may well be that get_fanout is not doing what you expect. While it is possible to capture the standard output of a command, doing so is a considerably more advanced technique; it is probably better to consider whether there is an alternate mechanism to achieve what you want. (The get_fanout command is not a standard part of the Tcl language library or any very common add-on library like Tk or the Tcllib collection, so we can only guess at its behavior.)

Calling an internal "function" in CMD script not updating the variable

In this NT cmd shell\"batch file" I scrabbled together the other day, I'm using the call command to run sections of the script like functions--as I have done many times before in other scripts. But one of this is behaving strange and I can't figure out what might be wrong...
The problem is that the first time the function is called it properly returns the errorcode and sets the (global) variable %RESULT%, but every time it's called again later it fails to update the variable with the new errorcode.
Here's a stripped-down version of the code in question:
:FACL
REM run fileacl.exe with given OPTIONS (%1)
REM uses global variables %TARGET% and %LOGPATH%, sets global %RESULT%
setlocal
set _OPTIONS_=%*
fileacl.exe "%TARGET%" %_OPTIONS_% /SILENT >%LOGPATH%\temp.out 2>%LOGPATH%\temp.err
set _RESULT_=%ERRORLEVEL%
if defined DEBUG echo INSIDE FUNCTION: _RESULT_ = %_RESULT_%
endlocal & set RESULT=%_RESULT_% & goto :EOF
The function is called in lines like this:
call :FACL /LINE
if defined DEBUG echo AFTER TEST #1: RESULT = %RESULT%
...
call :FACL /INHERIT /REPLACE /FORCE
if defined DEBUG echo AFTER FIX #2: RESULT = %RESULT%
You see those if defined DEBUG... lines there? They show me that inside the function, subsequent calls are succeeding and thus printing out the expected %_RESULT_% of 0, but the global %RESULT% remains the same. Here's some example output:
TEST #1:
INSIDE FUNCTION: _RESULT_ = 107 <-- that's what I expect for the first call
AFTER TEST #1: RESULT = 107 <-- the variable was properly set after the first call
FIX #2:
INSIDE FUNCTION: _RESULT_ = 0 <-- command succeeded :)
AFTER FIX #2: RESULT = 107 <-- variable didn't change :(
RETEST:
INSIDE FUNCTION: _RESULT_ = 0 <-- succeeded again
AFTER RETEST: RESULT = 107 <-- still didn't change
You may ask: what else have you tried? Okay:
Removed the setlocal\endlocal tricks and just used the global %RESULT% variable
Explicitly undefined %RESULT% and %_RESULT_% (e.g. set RESULT=) before each time the function is called
...all with the same results. Is there something I'm missing here?
Can't be sure, because we can't see the actual code in context. But the behavior you are describing is to be expected if the FIX 2 CALL and ECHO are within a parenthesized block - perhaps as part of an IF statement or FOR loop.
If that is the case, then you need to use delayed expansion within the parentheses since the entire block is parsed prior to execution and the %RESULT% is expanded at parse time.
Use SET EnableDelayedExpansion to enable delayed expansion, and use !RESULT! instead of %RESULT% to get the value of RESULT at execution time instead of at parse time.