libreoffice calc - varargs in macros - macros

I understand Excel has a TEXTJOINfunction which allows one to display multiple values as a tuple.
I also understand Libre Office does - for whatever reason - not have them.
How do I write an auxiliary macro vec that produces the desired tuple representation for me?
E.g. =vec(A1) should produce ="("&A1&")",
=vec(A1:A3) should produce ="("&A1&","&A2&","&A3&")",
=vec(A1,X5:X99,Z3) should result in ="("&A1&","&"X5"&","&X6&...&x99&","&Z3&")"
etc, etc.
Easy enough a macro to implement in, say, bash, but I would like to just define it once then use it in calc, not constantly copy from console to spreadsheet.
How do I implement this in calc?

According to https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=67880, it is possible for a Basic function to use a variable number of arguments if it is declared with Option Compatible. This makes it behave more like MS Excel. The argument is declared as ParamArray pa().
The link that #tohuwawohu posted shows most of the implementation details needed.
To do it in a way that is more native to LibreOffice, write a Spreadsheet Add-In with a Java declaration that uses any[] as an argument. For information about add-in argument types, see https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/AddIn.html.
The actual function can also be implemented in Java. Or, it can probably be implemented in another language that accepts a variable number of arguments, such as Python *args.

Related

Uniform list pretty-printer

It is known that default printer can be confusing wrt lists because of no output for empty lists and 3 different notations being mixed (, vs (x;y;z) vs 1 2 3) and not obvious indentation/columnization (which is apparently optimized for table data). I am currently using -3! but it is still not ideal.
Is there a ready-made pretty-printer that has consistent uniform output format (basically what I am used to in any other language where list is not special)?
I've started using .j.j for string outputs in error messages more recently in preference to -3!. Mainly I think it is easier to parse in a text log, but also doesn't truncate in the same way.
It still transforms atoms and lists differently so it might not exactly meet your needs, if you really want that you could compose it with the old "ensure this is a list" trick:
myPrinter:('[.j.j;(),])
You might need to supply some examples to better explain your issues and your use-case for pretty-printing.
In general -3! is the most clear visual representation of the data. It is the stringified equivalent to another popular display method which is 0N!.
The parse function is useful for understanding how the interpreter reads/executes commands but I don't think that will be useful in your case

At which lines in my MATLAB code a variable is accessed?

I am defining a variable in the beginning of my source code in MATLAB. Now I would like to know at which lines this variable effects something. In other words, I would like to see all lines in which that variable is read out. This wish does not only include all accesses in the current function, but also possible accesses in sub-functions that use this variable as an input argument. In this way, I can see in a quick way where my change of this variable takes any influence.
Is there any possibility to do so in MATLAB? A graphical marking of the corresponding lines would be nice but a command line output might be even more practical.
You may always use "Find Files" to search for a certain keyword or expression. In my R2012a/Windows version is in Edit > Find Files..., with the keyboard shortcut [CTRL] + [SHIFT] + [F].
The result will be a list of lines where the searched string is found, in all the files found in the specified folder. Please check out the options in the search dialog for more details and flexibility.
Later edit: thanks to #zinjaai, I noticed that #tc88 required that this tool should track the effect of the name of the variable inside the functions/subfunctions. I think this is:
very difficult to achieve. The problem of running trough all the possible values and branching on every possible conditional expression is... well is hard. I think is halting-problem-hard.
in 90% of the case the assumption that the output of a function is influenced by the input is true. But the input and the output are part of the same statement (assigning the result of a function) so looking for where the variable is used as argument should suffice to identify what output variables are affected..
There are perverse cases where functions will alter arguments that are handle-type (because the argument is not copied, but referenced). This side-effect will break the assumption 2, and is one of the main reasons why 1. Outlining the cases when these side effects take place is again, hard, and is better to assume that all of them are modified.
Some other cases are inherently undecidable, because they don't depend on the computer states, but on the state of the "outside world". Example: suppose one calls uigetfile. The function returns a char type when the user selects a file, and a double type for the case when the user chooses not to select a file. Obviously the two cases will be treated differently. How could you know which variables are created/modified before the user deciding?
In conclusion: I think that human intuition, plus the MATLAB Debugger (for run time), and the Find Files (for quick search where a variable is used) and depfun (for quick identification of function dependence) is way cheaper. But I would like to be wrong. :-)

What is the equivalent of Matlab's default display function which outputs to a file instead of stdout?

I apologize if this question is badly named, but let me explain through a simple analogy to Java. Please note, this question is about Matlab, not Java.
In Java, the standard way to write to the stdout stream is to say System.out.println.
If we want to print this exact output to a file, we can create a PrintStream object myPrinter, point it at a file, and replace the call to System.out.println with the call to myPrinter.println.
System.out.println("Hello World");
PrintStream myPrinter = new PrintStream(new File("MyJournal.log"));
myPrinter.println("Hello Log");
In Matlab, one standard way to write to standard out is to write an expression, variable or statement without a following semicolon. How would I rewrite my Matlab statements to get this (ascii) output into a file instead?
I would like to avoid solutions that redirect stdout to a file, because I still want some things printed to the console.
Additionally, I do not want to have to specify the type of the object that is being written to file, because the standard way does not require type specification.
Update: In particular, as far as I know, the printf family of functions requires type specification (different format string depending on the type of object). I am trying to avoid this because I seek a more generic solution.
What you want to do is find a generic string conversion that looks like display(), use that to build your output as strings, and then you can pass it to fprintf() or other normal output functions.
The Matlab output produced by omitting the semicolon is done by calling display() on the result of the expression. Matlab provides a display implementation for all the builtin types and a fallback implementation for user-supplied objects. The disp() function displays the value in the same way, but omits the variable name.
This is basically the equivalent of Java's toString function which is getting called behind the scenes when you print arbitrary objects to a PrintStream. The display functionality is mostly not in PrintStream itself, but in the polymorphic toString method that knows how to convert objects to display strings. The difference is that Java toString returns a String data value, which is easy to work with programmatically and compose with other operations, so you can manipulate it send it where you want, but Matlab's display and disp always output to stdout instead of giving you a string back.
What you want is probably something that will give you display-style output as a string, and then you can output it where you want, using fprintf() or another string output function.
One easy approach is to use evalc() to just call display and capture the output. The evalc function lets you run arbitrary Matlab code, and capture the output that would have gone to stdout to a char variable instead. You can make a generic conversion function and apply it to any values you want.
function out = tostr(x)
out = evalc('disp(x)');
end
The other option is to define a new dispstr() function, write it to convert Matlab built-in types (maybe using the evalc technique), and override dispstr in the objects you write, and call it polymorphically. More work, but it could end up being cleaner and faster, because evalc is slow and sometimes fragile. Also have a look at mat2str for ideas on an alternate output mechanism.
Then you'd be able to output values generically, without specifying the type, with calls like this. Note that in the second form, the placeholders are all just %s regardless of the type of object.
function displaySomeArbitraryDataValues(fh, foo, bar, baz)
% fh is a file handle previously opened with fopen()
fprintf(fh, 'foo=%s, bar=%s and baz=%s', tostr(foo), tostr(baz), tostr(qux))
end
Matlab does not have an formatted output function that automatically calls a display conversion, so you'll have to call the conversions explicitly in a lot of cases, so it'll be a little more wordy than Java. (Unless you want to go all out and write your own polymorphic auto-converting output functions or string wrapper objects.) If you want to use placeholders, it's easy enough by forcing all the args to be converted.
function out = sprintf2(format, varargin)
%SPRINTF2 A sprintf equivalent with polymorphic input conversion
% Use '%s' placeholders for everything in format, regardless of arg type
args = varargin;
strs = {}
for i = 1:numel(args)
strs{i} = tostr(args{i});
end
out = sprintf(format, strs{:});
end
If you want to get fancy, you could do mixed printf conversions by parsing the printf format argument yourself, defining a new placeholder like %y that calls your tostr() on the argument and then prints that string (probably by replacing the placeholder with %s and the argument with the tostr() result), and passes all the normal placeholders along to printf with un-converted arguments. This is kind of difficult to implement and can be computationally expensive, though.
You could make it behave more like Java by providing char conversions that convert object values to display strings, but that's inconsistent with how Matlab's existing numeric to char conversions work, so it wouldn't be fully generic, and you would run in to some odd edge cases, so I wouldn't recommend that. You could do a safer form of this by defining a new #string class that wraps char strings, but does implicit conversions by calling your tostr() instead of char(). This could be a big performance hit, though, because Matlab OOP objects are substantially slower than operations with built-in types and plain functions.
For background: basically, the reason you need to pass explicit conversions to fprintf in Matlab, but you don't need to with the PrintStream stuff in Java is that fprintf is inherited from C, which did not have run time type detection, especially of varargs, in the language. Matlab does, so fprintf is more limited than it needs to be. However, what fprintf does give you is fine-grained control over the formatting of numeric values and a concise calling form. These are useful, as evidenced by how Java programmers complained for years about not having them in Java and eventually got them added. It's just that Matlab sticks to the C-supported fprintf conversions, where they could easily add a couple new generic conversions that examine the type of the input at runtime and give you the generic output support you want.

ANTLR, C-styled macro definitions

What is the easiest (or the best) way to implement macro definitions in ANTLR?
What I want is a mechanism similar to the one that is present in C/C++ language:
#define FUNCTION(a, b) a+b
#define PI 3.1415
How and when should I perform replacement?
If you are doing a pre-processor in the style of C, then you will want to do a separate first pass for pre-processing (which is what this term means - a processing pass before your standard lex/parse pass).
Exactly how you want to do the pass is up to you - you can pass your input text to one grammar in antlr, take the result and hand that off to another grammar, etc.
Or you can create separate programs, which are able to take input on stdin and output to stdout, or pass text between pipes, etc.
Once you have that worked out, its a simple matter of looking for your keywords. Check every token that you see against your table of #defines, and if it matches, replace it with the definition that you have. You will also have to be able to parse function parameters, but that shouldn't add too much effort.

How to pass argument to a Microsoft Word macro?

I need to run a macro in Word with a parameter. I've tried to declare a parameter for the module in the VB Macro Editor but it doesn't work - the macro will be invisible in the macro list when I do so. I don't know how to do this and whether it is posible to do so or not in MS Word 2007.
Please help.
You can only run parameterless macros because there is no default input method to provide the parameter values. If you still want to use parameters, you will have to create a parameterless macro and request the parameter values yourself, either by using an InputBox or using form fields.