Friday, March 20, 2015

[vim] auto complete matlab flow control [for, if, while] with indentation in vim

Okay, it turns out just combining some keys is good enough :(

:inoremap <c-n><c-n> <esc>yyp^d$a
:inoremap <c-c><c-c> <esc>yyp^d$aend<esc>kyyp^d$a
 
hit ctrl-c twice (holding ctrl) in the edit mode , use ctrl-n twice to go next line

------------------------------------------------------------------- no need something huge --------------------------
"=============================================================================
" matlabFlowComplete.vim v0.0
" Author: Xianmin Hu <xianmin.hu@gmail.com>
" http://scriptdemo.blogspot.com
" Last Change: 19-Mar-2015.
"=============================================================================
"
"key cc : to complete for, if and while flow
"key nn : go to next line with proper indentation

:function! CompleteMatlabFlow()
" complete for, while and if flow control with proper indentation
: let isValidForWhileIf=0
: let tmpline=substitute(getline(".")," \\+"," ","g")
: let tmpline=substitute(tmpline," \\+$","","g")
: call setline(".",tmpline)
: let tmpline=substitute(tmpline,"^ \\+","","g")
: let clinesp=split(tmpline," ")
: unlet tmpline
: execute "normal! o"
: execute "normal! kk^"
: if len(clinesp) > 1
:    if clinesp[0] == "for" && len(clinesp)>2
:       let isValidForWhileIf=1
:       if len(clinesp)==3
:          let newcline="for " . clinesp[1] . " = " . clinesp[2] . ":1:" . clinesp[2]
:       elseif len(clinesp)==4
:         if clinesp[2]<clinesp[3]
:            let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":" . clinesp[3]
:         else
:           let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":-1:" . clinesp[3]
:        endif
:      elseif len(clinesp)>=5
:       let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":" . clinesp[3] . ":" . clinesp[4]
:      endif
:      let cpos = col(".")
:      let preline=split(getline(".")," ")
:      if len(preline) > 0
:         if preline[0] == "for"
:            let cpos += 4
:         elseif preline[0] == "while"
:            let cpos += 6
:         elseif preline[0] == "if"
:            let cpos += 3
:         endif
:      endif
:      while cpos > 1
:               let newcline = " " . newcline
:               let cpos -= 1
:       endwhile
:       execute "normal! j"
:       call setline(".",newcline)
:       execute "normal! yyp"
:       execute "normal! ^d$"
:       call setline(".",getline(".") . "end")
:       execute "normal! jdd"
:       execute "normal! kyykpd$i "
:       unlet newcline
:       unlet cpos
:    endif
"
:    if (clinesp[0] == "if" || clinesp[0] == "while") && len(clinesp)>1
:       let isValidForWhileIf=1
:       let cpos = col(".")
:       let preline=split(getline(".")," ")
:       if len(preline) > 0
:          if preline[0] == "for"
:             let cpos += 4
:          elseif preline[0] == "while"
:             let cpos += 6
:          elseif preline[0] == "if"
:             let cpos += 3
:          endif
:       endif
:       let padStr=""
:       while cpos > 1
:           let padStr = " " . padStr
:           let cpos -= 1
:      endwhile
:      execute "normal! j^0d^"
:      call setline(".", padStr . getline("."))
:      execute "normal! yyp^d$"
:      call setline(".",getline(".") . "end")
:      execute "normal! jdd"
:      execute "normal! kyykpd$i "
:      unlet padStr
:      unlet cpos
:   endif
:   unlet clinesp
: endif
: if isValidForWhileIf < 1
:    execute "normal! u"
:    execute "normal! $"
: endif
: unlet isValidForWhileIf
:endfunction

:function! GoMatlabNext()
" go next line and use same indent from previous line
: execute "normal! yyp"
: execute "normal! ^d$a"
:endfunction

:nmap cc :call CompleteMatlabFlow()<CR>a
:nmap nn :call GoMatlabNext()<CR>a
Example:
1) type "for a 1 4", then run cc in normal mode, ==>
for a = 1:4

end
2) type "for a 4 1", then run cc in normal mode, ==>
for a = 4:-1:4

end
3) type "for a 2 4 12", then run cc in normal mode, ==>
for a = 2:4:12

end
4) type "while a =4", then run cc in normal mode, ==>
while a=4

end

5) auto-indent
if you have"
%==============
if isLoop==1
for n loopStart loopStep loopEnd (run cc in normal mode)
end

it will result in:
%==============
if isLoop==1
   for n = loopStart:loopStep:loopEnd
         
   end
end

Friday, March 13, 2015

[matlab] relocate an object in the figure window

function movePosition(myh,myfrac,opt)
% move an object to a new location relative to current axes
% usage:
%      movePosition(obj-handle,fraction,opt)
%             opt: 'x' ==> x-coordiante is given in fraction of xlim
%                    'y' ==> y-coordiante is given in fraction of ylim
%                  'xy' ==> both x,y-coordiante is given in fraction
%                                (could be a 2-element vector)
% http://scriptdemo.blogspot.ca

if nargin~=3
   help movePosition
   return
end
if numel(myh)>1
   for nh=1:numel(myh)
       movePosition(myh(nh),myfrac,opt);
   end
   return
end
if ~ishandle(myh)
   error('need a valid object handle')
end

oriPos=get(myh,'position');
if isempty(oriPos)
    error('no position attribute is available')
end
if isnumeric(opt)
   oriPos(1)=myfrac;
   oriPos(2)=opt;
else
   switch lower(opt)
    case {'x'}
        cXLim=get(gca,'xlim');
        oriPos(1)=oriPos(1)+myfrac*(cXLim(2)-cXLim(1));
    case {'y'}
        cYLim=get(gca,'ylim');
        oriPos(2)=oriPos(2)+myfrac*(cYLim(2)-cYLim(1));
    case {'xy'}
        if numel(myfrac)==1
            myfrac=[myfrac myfrac];
        end
        cXLim=get(gca,'xlim');
        oriPos(1)=oriPos(1)+myfrac(1)*(cXLim(2)-cXLim(1));
        cYLim=get(gca,'ylim');
        oriPos(2)=oriPos(2)+myfrac(2)*(cYLim(2)-cYLim(1));
    otherwise
        error(['unknown opt: ',opt])
   end
end
set(myh,'position',oriPos);

[matlab] show available tags

function showtags(figH)
% to show all the tags in figure(s)
% usage:
%        showtags([figure_handles])
% e.g.,
%        showtags(gcf)
% http://scriptdemo.blogspot.ca

if nargin==1
   availableTag=unique(get(findobj(figH),'tag'));
   availableTagC=unique(get(findobj(figH),'Tag'));
else
   availableTag=unique(get(findobj(),'tag'));
   availableTagC=unique(get(findobj(),'Tag'));
end
if sum(~cellfun('isempty',availableTag))~=0
    disp('Available tags: ')
    disp(availableTag)
end
if sum(~cellfun('isempty',availableTagC))~=0
    disp('Available Tags: ')
    disp(availableTagC)
end

ShowCalendar