How to run a vim plugin function from vimrc? - vim-plugin

A plugin defines a function named HLMarks():
hi Marks term=reverse ctermfg=0 ctermbg=40 guibg=Grey40
function! HLMarks(group)
call clearmatches()
let index = char2nr('a')
while index < char2nr('z')
call matchadd( a:group, '\%'.line( "'".nr2char(index)).'l')
let index = index + 1
endwhile
endfunction
I want the HLMarks() function to run automatically every time vim opens a file.
It works when I call the function manually:
:call HLMarks("Marks")
Adding this line to the end of the plugin didn't do anything:
call HLMarks("Marks")
Calling the function from vimrc got this error:
E117: Unknown function: HLMarks
How to automatically call the HLMarks("Marks") function when a file is opened?
The plugin is described on http://www.vim.org/scripts/script.php?script_id=3394
and down loaded from http://www.vim.org/scripts/download_script.php?src_id=21611
The plugin's markHL.vim file is in my ~/.vim/plugin/ directory.
The ":function" command lists:
function HLMarks(group)

The solution is to add this line to vimrc:
autocmd BufReadPost * call HLMarks("Marks")
Details are at https://groups.google.com/forum/#!topic/vim_use/i2HWD_9V-28

If you define the function in .vimrc then:
function! yourFunc()
" ...
endfunction
call yourFunc()
simply adding the call yourFunc() after the definition will work.

Related

How to use function that belongs to module 1 inside module 2?

I'm trying to use a function that is inside a file (module 1) in another file (module) 2. But it gives me an error "ERROR: UndefVarError: t1 not defined". I have tried to export the function in module 1 but it also doesn't work. I'm new to Julia and I do not know very well how to handle modules.
Here is the code that I'm having problems.
First File: t1.jl
module A
function soma(a::Int64, b::Int64)
return a + b
end
end
Second File: t2.jl
module B
include("t1.jl")
using .t1
function main()
s = soma(2, 3)
println(s)
end
main()
end
Changing t2.jl to:
module B
include("t1.jl")
using .A
function main()
s = A.soma(2, 3)
println(s)
end
main()
end
prints out 5 as expected.
include is basically as if you'd copy-pasted the code from the included file into the current file - so, once you've included t1.jl into module B, the fact that the file is called t1.jl has no relevance. It's module A that's in the current scope, and A is the namespace that contains the soma function we need. Hence, using .A is what's needed to make soma available within B.

Calling a module subroutine using a fully qualified name

I created a simple test module ./mods/My/Module.pm6:
unit module My::Module;
use v6;
sub hello () is export {
say "Hello";
}
Then, I have a test script ./p.p6:
#! /usr/bin/env perl6
use v6;
use My::Module;
My::Module::hello();
Then I set PERL6LIB to include the folder ./mods, and then run the script:
$ ./p.p6
Could not find symbol '&hello'
in block <unit> at ./p.p6 line 7
However, if I replace the line My::Module::hello() in the script with hello() it works fine. What am I missing here?
If you export hello you can simply use it
use v6;
use lib <lib>; # hint: no need to tinker with the environment
use My::Module;
hello();
If you really WANT to use a fully qualified name, you have to use the our keyword.
our sub hello () is export {
say "Hello";
}

How to know which line of code was being executed when a signal is received

I'm trying to do something like this
$SIG{ALRM} = sub {
print $line_number_when_alarm_went_off;
};
alarm 10;
# rest of the script
I'm using ALRM as an example, I will end up using a different signal to kill from the outside to trigger it. Is there a neat way of doing this sort of operation?
I have some slow scripts and sometimes I would like to send them a signal to know where the code is at that moment.
I want to make this as unobtrusive as possible so I could package it and add it to legacy code.
You can use caller in list context to get the package, file and line number of the place that the current sub got called from.
$SIG{ALRM} = sub {
my ($pkg, $file, $line) = caller;
CORE::say $line;
die;
};
alarm 2;
while (1) {
1;
}
This will output 11 (if I counted correctly, in my file it's 1740, and the $SIG line is 1730.
It also works with other signal handlers, like warn.
$SIG{__WARN__} = sub {
my ($pkg, $file, $line) = caller;
CORE::say $line;
};
warn 'foo';
This will output 7
Note that your code has a syntax error. You are assigning a hash reference as a signal handler, not a sub reference!

Batch Method with Argument

Is there a way to put a argument into a method in batch script? I know I can do that in java programming.
Example #1 (Java)
public class Test {
public static void main (String [] args) {
Test t1=new Test();
System.out.print(t1.method1(false));
}
public int method1 (boolean val1) {
if (val1==false) {
return 0;}
else {
return 1;}
}
}
I want to have something like this so when the method runs, depending on the argument, the method will produce varying results.
Example #2 (Batch - partial pseudocode)
:method1
::With an argument a1 (by default a1=1)
if %a1%==1 echo Option #1
if %a1%==2 echo Option #2
So when I call method1, depending on the argument, I could have two results.
Is there a way to do that? Or suggestions on how one method can have different results? Thanx
Try the inline help for the call built-in statement.
C:\>call /?
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters Specifies any command-line information required by the
batch program.
If Command Extensions are enabled CALL changes as follows:
CALL command now accepts labels as the target of the CALL. The syntax
is:
CALL :label arguments
A new batch file context is created with the specified arguments and
control is passed to the statement after the label specified. You must
"exit" twice by reaching the end of the batch script file twice. The
first time you read the end, control will return to just after the CALL
statement. The second time will exit the batch script. Type GOTO /?
for a description of the GOTO :EOF extension that will allow you to
"return" from a batch script.
<continutes>

OCaml Module : include AND open?

I'm fairly new with OCaml Module and I haven't managed to use my own module without combining both an "include" and an "open".
I've tried to put the signature in a separate .mli file, without success.
Below I'm indicated a minimum (not) working example, that I'm trying to compile with
ocamlc -o main Robot.ml main.ml
What to I need to do to only have to use "open", or only "include", but not both of them ?
File "Robot.ml" :
module type RobotSignature =
sig
val top: unit -> unit
end
module Robot =
struct
let top () =
begin
Printf.printf "top\n"
end
(* Should not be visible from the 'main' *)
let dummy () =
begin
Printf.printf "dummy\n"
end
end
File "main.ml" (not working) :
open Robot;;
top();
File "main.ml" (working) :
include Robot;;
open Robot;;
top();
You've got two levels of Robot. Since you explicitly called your module "Robot" within the file robot.ml you'll need to open Robot and then call Robot.top(). Anything in the robot.ml file is already put implicitly inside of a Robot module.
You could get rid of the extra 'module Robot' declaration in robot.ml.
robot.ml would become:
module type RobotSignature =
sig
val top: unit -> unit
end
let top () =
begin
Printf.printf "top\n"
end
Then it should work as you have it in your main.ml.
Update based on comment below: If you're concerned that everything in robot.ml will now be visible when you 'open Robot' you can define a robot.mli file which specifies the functions which are available externally. For example, let's say you add a function called helper in robot.ml:
let top () =
begin
Printf.printf "top\n"
end
let helper () =
Printf.printf "helper\n"
...and then you define your robot.mli as follows:
val top: unit -> unit
Then let's say you try to call helper from main.ml:
open Robot;;
top();
(* helper will not be visible here and you'll get a compile error*)
helper ()
Then when you try to compile you'll get an error:
$ ocamlc -o main robot.mli robot.ml main.ml
File "main.ml", line 4, characters 0-6:
Error: Unbound value helper
You have two ways to do this:
First, you can constrain your sub-structure to be of the right signature:
module Robot : RobotSignature = struct ... end
Then in main.ml, you can do open Robot.Robot: the first Robot means the compilation unit associated to robot.ml, the second Robot is the submodule you have defined inside robot.ml
You can also remove one level and create robot.mli containing:
val top: unit -> unit
and robot.ml containing:
let top () =
Printf.printf "top\n"
(* Should not be visible from the 'main' *)
let dummy () =
Printf.printf "dummy\n"
You can compile the modules using ocamlc -c robot.mli && ocamlc -c robot.ml and then in main.ml simply use open Robot.