How to correctly format function calls in emacs - emacs

Does anybody know how to remove unwanted whitespace between function parameters.
for example I have a badly formatted function
foo ( int a , int b );
and I would like to get
foo (int a, int b);
I am looking for somethink similar to clean-ups or colon and brace hanging in CC mode. Ideally the correction is done when typing or pressing tab.
Thanks!

Using query-replace-regexp:
M-C-S 5( +Enter(Enter!
M-C-S 5+,Enter,Enter!
M-C-S 5+)Enter)Enter!

Related

Ncurses no-wrap mode when adding strings to window

I'm adding strings to a window, with waddwstr() function, one line after other, in consecutive rows. I don't want ncurses to automatically wrap lines for me – I'm overwriting them with consecutive calls to waddwstr() and sometimes tail of previous line is left displaying. Can ncurses just stop when right edge of window is reached?
The non-wrapping functions have "ch" in their name, e.g., wadd_wchstr.
The same is true of the non-wide interfaces waddstr versus waddchstr.
However, the wrapping/non-wrapping functions differ by more than that. They use different parameter types. The wrapping functions rely upon the video attributes set via wattr_set, etc., while the non-wrapping functions combine the video-attributes with the character data:
waddstr and waddchstr use char* and chtype* parameters, respectively
waddwstr and wadd_chstr use wchar_t* and cchar_t* parameters.
Converting between the two forms can be a nuisance, because X/Open, etc., did not define functions for doing the conversion.
The manual page for bkgd describes how these video attributes are combined with the background character to obtain the actual display.
The accepted answer (by Mr. Dickey) is correct. However, the "ch" functions do not work with ordinary C strings (array of bytes). Another solution is to create a wrapper for waddstr which checks the current cursor position and window size and prints only as much as would fit.
For example:
int waddstr_trunc(WINDOW *win, const char *str)
{
int cur_x, max_x, dummy [[maybe_unused]];
getyx(win, dummy, cur_x);
getmaxyx(win, dummy, max_x);
int w=max_x - cur_x;
if (w <= 0) return 0;
char *str2 = strndup(str, w);
if (str2 == NULL) return 1;
int rv = waddstr(win, str2);
free(str2);
return rv;
}

Passing command lines to function to evaluate in Matlab

I have a function which is very open-ended that I use in several different applications. Instead of changing it everytime, I would like to pass several command lines as input for the function to evaluate using something like the eval function.
Thus, for example, my argument would be:
str={'a=32;
b=a+3*a^2+pi;
c=sin(a)+cos(b)^2;'}
then I could call the function with str as an argument:
x=func(str)
these lines would be evaluated inside the function
how?
Thanks alot!
I think #Daniel is right that function handles are the way forward. Based on your example, this is how you'd do it:
function x = testfun( a, bfun, cfun )
b = bfun(a);
c = cfun(a, b);
x = a + b + c;
end
Then you'd call it like this:
x = testfun( 32, #(a)(a+3*a^2+pi), #(a,b)(sin(a)+cos(b)^2) );

Perl - Pattern contains new line

I am trying to grab all functions from a C file in a perl script.
Pattern example :
function return type
function name (function parameters)
{
So far I have: m/^(.*)\((.*)\)/
But this grabs functions inside as well, such as if statements, so I was hoping to match for the { as well since that would eliminate all internal functions but m/^(.*)\((.*)\)/\n\{/
doesn't work.
How do I match for the \n{ i.e the { in the next line, so that I can catch
add(int a, int b)
{
... but avoid, say
if(a = b)
Have a look at C::Scan over at CPAN
There are no asterisks you want to match in the C source. Therefore, remove the backslashes before asterisks in the pattern.
The following might be closer to what you want:
m/^(.*?\(.*?\))\s*\n{/m

how to make matlab detect incompatible type assignments?

hi have a major problem in matlab. I have a function and it sometimes returns control ascii characters. How do i check for the presence of these control ascii ?.
my code looks like this
d = out.autoc
d sometimes receives control ascii characters instead of a actual double value. Does someone know how to catch such incompatible assignments ?
I think this should work but you may want to double check the ASCII codes to exclude.
%here I load Ctrl-C
s = sprintf('%s', 3);
code = bin2dec(dec2bin(s,8));
if code < 32
fprintf('ignore');
else
fprintf('do somsething');
end
If you want to check that the value of d is double, and not a string. You can check it this way:
if ~isnumeric(d) || ~isdouble(d)
fprintf('d is not of class double\n');
end
But if you want to assign the value of out.autoc to d only if out.autoc is double, you can do this:
if isnumeric(out.autoc) && isequal(class(out.autoc), 'double')
d = out.autoc;
else
fprintf('out.autoc is not of class double, no assignment made.\n');
end

aligning or prettifying code in emacs

I remember this was possible in emacs, but don't know how. If I have something like:
'abc' => 1,
'abcabc' =>2,
'abcabcabc' => 3,
How can I align the keys, arrows and values to something like this?
'abc' => 1,
'abcabc' => 2,
'abcabcabc' => 3,
Cheers
Select the region.
Type M-x align-regexp RET
Type = and hit enter.
You can also use the align command instead of align-regexp. The difference is that align automatically chooses the regular expression(s) to use based on the major-mode of the buffer. So if you are trying to align a block of variable initializations and assignments in a c-mode file, then it will automatically do the right thing without you needing to think of the regular expressions which are needed. Can be convenient.
For example select the following lines:
int x = 3;
double y = 9.0;
unsigned int z = 6;
const char c = 'A';
And type M-x align RET. The result is:
int x = 3;
double y = 9.0;
unsigned int z = 6;
const char c = 'A';
I should add, though, that this will not always work. If there are no regular expressions defined for the major-mode of the current buffer, then the call to align will do nothing. Then, you need to fall back on align-regexp. But this is hardly a large inconvenience. I actually use align-regexp fairly frequently. For convenience, I have defined an alias to save myself a few key-strokes:
(defalias 'ar #'align-regexp)