diff -r 7b50afd31037 -r ad9650969b89 runtime/indent/javascript.vim
--- a/runtime/indent/javascript.vim	Fri Aug 19 22:29:02 2011 +0200
+++ b/runtime/indent/javascript.vim	Sun Aug 21 17:07:19 2011 +0530
@@ -11,6 +11,7 @@
 
 " C indenting is not too bad.
 setlocal cindent
-setlocal cinoptions+=j1,J1
+setlocal cinwords+=function,with
+setlocal cinoptions+=J1
 
-let b:undo_indent = "setl cin<"
+let b:undo_indent = "setl cin< cinwords< cino<"
diff -r 7b50afd31037 -r ad9650969b89 src/misc1.c
--- a/src/misc1.c	Fri Aug 19 22:29:02 2011 +0200
+++ b/src/misc1.c	Sun Aug 21 17:07:19 2011 +0530
@@ -4939,11 +4939,11 @@
 static int	cin_get_equal_amount __ARGS((linenr_T lnum));
 static int	cin_ispreproc __ARGS((char_u *));
 static int	cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
-static int	cin_iscomment __ARGS((char_u *));
+static int	cin_iscomment_start __ARGS((char_u *));
 static int	cin_islinecomment __ARGS((char_u *));
 static int	cin_isterminated __ARGS((char_u *, int, int));
 static int	cin_isinit __ARGS((void));
-static int	cin_isfuncdecl __ARGS((char_u **, linenr_T));
+static int	cin_isfuncdecl __ARGS((char_u *, linenr_T));
 static int	cin_isif __ARGS((char_u *));
 static int	cin_iselse __ARGS((char_u *));
 static int	cin_isdo __ARGS((char_u *));
@@ -5181,7 +5181,14 @@
 		    return TRUE;
 	    }
 	}
-	return FALSE;
+	/* JS allows things like case (a ? b : c) : provided that the expression 
+	 * evaluates to the switch variable's value 
+	 * So if the statement has 'case ...' this is ok. 
+	 */
+	if (strict)
+	    return FALSE;
+	else
+	    return TRUE;
     }
 
     if (cin_isdefault(s))
@@ -5396,6 +5403,9 @@
 		|| (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
 	    p = s;
     }
+    /* FIXME: this should be done only if 'J' flag is set */
+    else if (len == 3 && STRNCMP(p, "var", 3) == 0)
+	p = skipwhite(p + 3);
     for (len = 0; vim_isIDc(p[len]); ++len)
 	;
     if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
@@ -5436,7 +5446,7 @@
     line = s = ml_get(lnum);
     while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
     {
-	if (cin_iscomment(s))	/* ignore comments */
+	if (cin_iscomment_start(s))	/* ignore comments */
 	    s = cin_skipcomment(s);
 	else
 	    ++s;
@@ -5464,7 +5474,8 @@
 cin_ispreproc(s)
     char_u *s;
 {
-    if (*skipwhite(s) == '#')
+    s = skipwhite(s);
+    if (*s == '#')
 	return TRUE;
     return FALSE;
 }
@@ -5507,7 +5518,7 @@
  * Recognize the start of a C or C++ comment.
  */
     static int
-cin_iscomment(p)
+cin_iscomment_start(p)
     char_u  *p;
 {
     return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
@@ -5584,26 +5595,18 @@
  * "lnum" is where we start looking.
  */
     static int
-cin_isfuncdecl(sp, first_lnum)
-    char_u	**sp;
+cin_isfuncdecl(s, first_lnum)
+    char_u	*s;
     linenr_T	first_lnum;
 {
-    char_u	*s;
     linenr_T	lnum = first_lnum;
     int		retval = FALSE;
 
-    if (sp == NULL)
+    if (s == NULL)
 	s = ml_get(lnum);
-    else
-	s = *sp;
-
-    /* Ignore line starting with #. */
-    if (cin_ispreproc(s))
-	return FALSE;
-
     while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
     {
-	if (cin_iscomment(s))	/* ignore comments */
+	if (cin_iscomment_start(s))	/* ignore comments */
 	    s = cin_skipcomment(s);
 	else
 	    ++s;
@@ -5650,15 +5653,15 @@
 	    if (!comma && *s != ',' && *s != ')')
 		break;
 	}
-	else if (cin_iscomment(s))	/* ignore comments */
+	else if (cin_iscomment_start(s))	/* ignore comments */
 	    s = cin_skipcomment(s);
 	else
 	    ++s;
     }
 
 done:
-    if (lnum != first_lnum && sp != NULL)
-	*sp = ml_get(first_lnum);
+    if (lnum != first_lnum && s != NULL)
+	s = ml_get(first_lnum);
 
     return retval;
 }
@@ -5667,6 +5670,7 @@
 cin_isif(p)
     char_u  *p;
 {
+    p = skipwhite(p);
     return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
 }
 
@@ -5683,6 +5687,7 @@
 cin_isdo(p)
     char_u  *p;
 {
+    p = skipwhite(p);
     return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
 }
 
@@ -6028,7 +6033,7 @@
     p = line = ml_get(trypos->lnum);
     while (*p && (colnr_T)(p - line) < trypos->col)
     {
-	if (cin_iscomment(p))
+	if (cin_iscomment_start(p))
 	    p = cin_skipcomment(p);
 	else
 	{
@@ -6137,16 +6142,18 @@
     int		i;
     int		retval = FALSE;
     int		open_count = 0;
-
+int in_re = 0;
     curwin->w_cursor.col = 0;		    /* default is start of line */
 
     for (i = 0; l[i]; i++)
     {
-	i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
-	i = (int)(skip_string(l + i) - l);    /* ignore parens in quotes */
-	if (l[i] == start)
+	i = (int)(cin_skipcomment(l + i) - l); /* ignore 'start' in comments */
+	i = (int)(skip_string(l + i) - l);    /* ignore 'start' in quotes */
+	/* not great ; this could also be a division operator :/ */
+	if(l[i - 1] == '/' && l[i-2] != '\\') { if(in_re) in_re = 0; else in_re = 1;}
+	if (!in_re && l[i] == start)
 	    ++open_count;
-	else if (l[i] == end)
+	else if (!in_re && l[i] == end)
 	{
 	    if (open_count > 0)
 		--open_count;
@@ -6319,7 +6326,7 @@
     /*
      * max lines to search for an open paren
      */
-    int ind_maxparen = 20;
+    int ind_maxparen = 100;
 
     /*
      * max lines to search for an open comment
@@ -6332,7 +6339,17 @@
     int	ind_java = 0;
 
     /*
-     * not to confuse JS object properties with labels
+     * Handle JavaScript code:
+     * - not to confuse JS object properties with labels
+     * indenting braces inside parenthesized structures 
+     * f.e. 
+     * window.Foo(1, long_argument,
+     *     2,3, 
+     *     (fun ()
+     *     {
+     *         some statements
+     *     })(),
+     *     some, more, args);
      */
     int ind_js = 0;
 
@@ -6347,14 +6364,16 @@
     int ind_cpp_namespace = 0;
 
     pos_T	cur_curpos;
-    int		amount;
-    int		scope_amount;
+    int		amount = 0;
+    int		scope_amount = 0;
+    int		paren_scope_amount = 0;
     int		cur_amount = MAXCOL;
     colnr_T	col;
     char_u	*theline;
     char_u	*linecopy;
-    pos_T	*trypos;
+    pos_T	*trypos = NULL;
     pos_T	*tryposBrace = NULL;
+    pos_T	*tryposBracket = NULL;
     pos_T	our_paren_pos;
     char_u	*start;
     int		start_brace;
@@ -6384,7 +6403,9 @@
     int		fraction = 0;	    /* init for GCC */
     int		divider;
     int		n;
-    int		iscase;
+    int		iscase = 0;
+    int iselse = 0;
+    int iswhileofdo = 0;
     int		lookfor_break;
     int		lookfor_cpp_namespace = FALSE;
     int		cont_amount = 0;    /* amount for continuation line */
@@ -6457,7 +6478,7 @@
 	    case 'g': ind_scopedecl = n; break;
 	    case 'h': ind_scopedecl_code = n; break;
 	    case 'j': ind_java = n; break;
-	    case 'J': ind_js = n; break;
+	    case 'J': ind_js = n; ind_unclosed = curbuf->b_p_sw; break;
 	    case 'l': ind_keep_case_label = n; break;
 	    case '#': ind_hash_comment = n; break;
 	    case 'N': ind_cpp_namespace = n; break;
@@ -6503,7 +6524,7 @@
     /*
      * #defines and so on always go at the left when included in 'cinkeys'.
      */
-    if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
+    if (!ind_js && *theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
     {
 	amount = 0;
     }
@@ -6534,7 +6555,7 @@
      * If we're inside a comment and not looking at the start of the
      * comment, try using the 'comments' option.
      */
-    else if (!cin_iscomment(theline)
+    else if (!cin_iscomment_start(theline)
 	    && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
     {
 	int	lead_start_len = 2;
@@ -6550,8 +6571,8 @@
 	/* find how indented the line beginning the comment is */
 	getvcol(curwin, trypos, &col, NULL, NULL);
 	amount = col;
-	*lead_start = NUL;
-	*lead_middle = NUL;
+       *lead_start = NUL; 
+       *lead_middle = NUL;
 
 	p = curbuf->b_p_com;
 	while (*p != NUL)
@@ -6648,7 +6669,7 @@
 	 */
 	if (done)
 	    ;
-	else if (theline[0] == '*')
+	else if (*theline == '*')
 	    amount += 1;
 	else
 	{
@@ -6683,17 +6704,46 @@
 	    }
 	}
     }
+    /* attempt some quick steps to find indent 
+    else if (*theline == '}' && *skipwhite(ml_get(cur_curpos.lnum -1)) == '}')
+    {
+	amount = get_indent_lnum(cur_curpos.lnum - 1) - ind_level;
+    }
+    */
 
     /*
      * Are we inside parentheses or braces?
      */						    /* XXX */
-    else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
+    else if ((((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
 		&& ind_java == 0)
-	    || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
-	    || trypos != NULL)
-    {
+	    || ((tryposBrace = find_start_brace(ind_maxcomment)) != NULL)
+	    || (ind_js && (tryposBracket = findmatchlimit(NULL, '[', FM_BLOCKSTOP, 0)) != NULL) 
+	    || (trypos != NULL))
+    {
+	/* 
+	 * if we found a paren above and didn't search for braces
+	 * let's try to find a brace also
+	 */
+	if (ind_js && trypos != NULL && tryposBrace == NULL)
+	{
+	    curwin->w_cursor  = cur_curpos;
+	    tryposBrace = find_start_brace(ind_maxcomment);
+
+	    /* FIXME: attempt to find a bracket and use it */
+	}
+
+	if (ind_js && tryposBracket == NULL)
+	{
+	    curwin->w_cursor  = cur_curpos;
+	    tryposBracket = findmatchlimit(NULL, '[', FM_BLOCKSTOP, 0);
+	}
+
       if (trypos != NULL && tryposBrace != NULL)
       {
+	  if (ind_js && trypos->lnum == tryposBrace->lnum)
+	      trypos = NULL;
+	  else
+	  {
 	  /* Both an unmatched '(' and '{' is found.  Use the one which is
 	   * closer to the current cursor position, set the other to NULL. */
 	  if (trypos->lnum != tryposBrace->lnum
@@ -6702,6 +6752,24 @@
 	      trypos = NULL;
 	  else
 	      tryposBrace = NULL;
+	  }
+      }
+      
+      if (ind_js && tryposBrace != NULL && tryposBracket != NULL)
+      {
+	  if (tryposBracket->lnum == tryposBrace->lnum && tryposBracket->col < tryposBrace->col)
+	      tryposBracket = NULL;
+	  else
+	  {
+	      /* Both an unmatched '[' and '{' is found.  Use the one which is
+	       * closer to the current cursor position, set the other to NULL. */
+	      if (tryposBracket->lnum != tryposBrace->lnum
+		      ? tryposBracket->lnum < tryposBrace->lnum
+		      : tryposBracket->col < tryposBrace->col)
+		  tryposBracket = NULL;
+	      else
+		  tryposBrace = NULL;
+	  }
       }
 
       if (trypos != NULL)
@@ -6710,7 +6778,7 @@
 	 * If the matching paren is more than one line away, use the indent of
 	 * a previous non-empty line that matches the same paren.
 	 */
-	if (theline[0] == ')' && ind_paren_prev)
+	if (*theline == ')' && ind_paren_prev)
 	{
 	    /* Line up with the start of the matching paren line. */
 	    amount = get_indent_lnum(curwin->w_cursor.lnum - 1);  /* XXX */
@@ -6724,7 +6792,7 @@
 		l = skipwhite(ml_get(lnum));
 		if (cin_nocode(l))		/* skip comment lines */
 		    continue;
-		if (cin_ispreproc_cont(&l, &lnum))
+		if (!ind_js && cin_ispreproc_cont(&l, &lnum))
 		    continue;			/* ignore #define, #if, etc. */
 		curwin->w_cursor.lnum = lnum;
 
@@ -6742,15 +6810,15 @@
 			&& trypos->lnum == our_paren_pos.lnum
 			&& trypos->col == our_paren_pos.col)
 		{
-			amount = get_indent_lnum(lnum);	/* XXX */
-
-			if (theline[0] == ')')
-			{
-			    if (our_paren_pos.lnum != lnum
-						       && cur_amount > amount)
-				cur_amount = amount;
-			    amount = -1;
-			}
+		    amount = get_indent_lnum(lnum);	/* XXX */
+
+		    if (*theline == ')')
+		    {
+			if (our_paren_pos.lnum != lnum
+				&& cur_amount > amount)
+			    cur_amount = amount;
+			amount = -1;
+		    }
 		    break;
 		}
 	    }
@@ -6779,6 +6847,7 @@
 		line = ml_get_curline();
 		look_col = (int)(look - line);
 		curwin->w_cursor.col = look_col + 1;
+		/* nested parentheses in single line */
 		if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen))
 								      != NULL
 			  && trypos->lnum == our_paren_pos.lnum
@@ -6788,7 +6857,7 @@
 		curwin->w_cursor.lnum = save_lnum;
 		look = ml_get(our_paren_pos.lnum) + look_col;
 	    }
-	    if (theline[0] == ')' || ind_unclosed == 0
+	    if (*theline == ')' || ind_unclosed == 0
 		    || (!ind_unclosed_noignore && *look == '('
 						    && ignore_paren_col == 0))
 	    {
@@ -6801,11 +6870,11 @@
 		 * outer paren and add ind_unclosed_wrapped (for very long
 		 * lines).
 		 */
-		if (theline[0] != ')')
+		if (*theline != ')')
 		{
 		    cur_amount = MAXCOL;
 		    l = ml_get(our_paren_pos.lnum);
-		    if (ind_unclosed_wrapped
+		    if ( ind_unclosed_wrapped
 				       && cin_ends_in(l, (char_u *)"(", NULL))
 		    {
 			/* look for opening unmatched paren, indent one level
@@ -6850,12 +6919,11 @@
 		if (our_paren_pos.col > 0)
 		{
 		    getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
-		    if (cur_amount > (int)col)
-			cur_amount = col;
+		    cur_amount = col;
 		}
 	    }
 
-	    if (theline[0] == ')' && ind_matching_paren)
+	    if (*theline == ')' && (ind_js ||ind_matching_paren))
 	    {
 		/* Line up with the start of the matching paren line. */
 	    }
@@ -6869,18 +6937,26 @@
 	    {
 		/* Add ind_unclosed2 for each '(' before our matching one, but
 		 * ignore (void) before the line (ignore_paren_col). */
-		col = our_paren_pos.col;
-		while ((int)our_paren_pos.col > ignore_paren_col)
+		col =  our_paren_pos.col;
+		if (ind_js)
 		{
-		    --our_paren_pos.col;
-		    switch (*ml_get_pos(&our_paren_pos))
+		    getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
+		    amount = col + 1; //FIXME: find the non-white char and use it.
+		}
+		else
+		{
+		    while ((int)our_paren_pos.col > ignore_paren_col)
 		    {
-			case '(': amount += ind_unclosed2;
-				  col = our_paren_pos.col;
-				  break;
-			case ')': amount -= ind_unclosed2;
-				  col = MAXCOL;
-				  break;
+			--our_paren_pos.col;
+			switch (*ml_get_pos(&our_paren_pos))
+			{
+			    case '(': amount += ind_unclosed2;
+				      col = our_paren_pos.col;
+				      break;
+			    case ')': amount -= ind_unclosed2;
+				      col = MAXCOL;
+				      break;
+			}
 		    }
 		}
 
@@ -6888,11 +6964,12 @@
 		 * braces */
 		if (col == MAXCOL)
 		    amount += ind_unclosed;
-		else
+		else if(!ind_js)
 		{
 		    curwin->w_cursor.lnum = our_paren_pos.lnum;
 		    curwin->w_cursor.col = col;
-		    if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL)
+		    if (find_match_paren(ind_maxparen,
+				    ind_maxcomment) != NULL)
 			amount += ind_unclosed2;
 		    else
 			amount += ind_unclosed;
@@ -6909,18 +6986,25 @@
 		    amount = cur_amount;
 	    }
 	}
-
+	paren_scope_amount = amount;
+	
 	/* add extra indent for a comment */
-	if (cin_iscomment(theline))
+	if (cin_iscomment_start(theline))
 	    amount += ind_comment;
+	amount = 0;
       }
 
       /*
        * Are we at least inside braces, then?
        */
-      else
+
+      /* Let the brackets be our braces */
+      if (tryposBrace != NULL || tryposBracket != NULL)
       {
-	trypos = tryposBrace;
+	trypos = tryposBrace ? tryposBrace : tryposBracket;
+
+	tryposBrace = NULL;
+	tryposBracket = NULL;
 
 	ourscope = trypos->lnum;
 	start = ml_get(ourscope);
@@ -6932,11 +7016,11 @@
 	 * a whole and then add the "imaginary indent" to that.
 	 */
 	look = skipwhite(start);
-	if (*look == '{')
+	if ((*look == '{') || (*look == '['))
 	{
 	    getvcol(curwin, trypos, &col, NULL, NULL);
 	    amount = col;
-	    if (*start == '{')
+	    if ((*start == '{') || (*start == '['))
 		start_brace = BRACE_IN_COL0;
 	    else
 		start_brace = BRACE_AT_START;
@@ -6979,7 +7063,7 @@
 	 * we want to be.  otherwise, add the amount of room
 	 * that an indent is supposed to be.
 	 */
-	if (theline[0] == '}')
+	if ((*theline == '}') || (*theline == ']'))
 	{
 	    /*
 	     * they may want closing braces to line up with something
@@ -7028,7 +7112,7 @@
 	    if (start_brace == BRACE_IN_COL0)	    /* '{' is in column 0 */
 	    {
 		amount = ind_open_left_imag;
-		lookfor_cpp_namespace = TRUE;
+		if(!ind_js) lookfor_cpp_namespace = TRUE;
 	    }
 	    else if (start_brace == BRACE_AT_START &&
 		    lookfor_cpp_namespace)	  /* '{' is at start */
@@ -7062,7 +7146,7 @@
 		lookfor = LOOKFOR_CASE;	/* find a previous switch() label */
 		amount += ind_case;
 	    }
-	    else if (cin_isscopedecl(theline))	/* private:, ... */
+	    else if (!ind_js && cin_isscopedecl(theline))	/* private:, ... */
 	    {
 		lookfor = LOOKFOR_SCOPEDECL;	/* class decl is this block */
 		amount += ind_scopedecl;
@@ -7075,6 +7159,7 @@
 		lookfor = LOOKFOR_INITIAL;
 		amount += ind_level;	/* ind_level from start of block */
 	    }
+	    if(!paren_scope_amount)//CHECK
 	    scope_amount = amount;
 	    whilelevel = 0;
 
@@ -7118,7 +7203,7 @@
 			     * initialization) */
 			    if (cont_amount > 0)
 				amount = cont_amount;
-			    else if (!ind_js)
+			    else
 				amount += ind_continuation;
 			    break;
 			}
@@ -7140,7 +7225,7 @@
 			/*
 			 * Skip preprocessor directives and blank lines.
 			 */
-			if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
+			if (!ind_js && cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
 			    continue;
 
 			if (cin_nocode(l))
@@ -7154,7 +7239,7 @@
 			 * (it's a variable declaration).
 			 */
 			if (start_brace != BRACE_IN_COL0
-				|| !cin_isfuncdecl(&l, curwin->w_cursor.lnum))
+				|| !cin_isfuncdecl(l, curwin->w_cursor.lnum))
 			{
 			    /* if the line is terminated with another ','
 			     * it is a continued variable initialization.
@@ -7166,7 +7251,7 @@
 			    if (terminated == ',')
 				break;
 
-			    /* if it es a enum declaration or an assignment,
+			    /* if it is an enum declaration or an assignment,
 			     * we are done.
 			     */
 			    if (terminated != ';' && cin_isinit())
@@ -7188,9 +7273,12 @@
 				trypos = find_match_paren(ind_maxparen,
 					ind_maxcomment);
 
+			    if (trypos == NULL && find_last_paren(l,'[', ']'))
+				trypos = findmatchlimit(NULL, '[', FM_BLOCKSTOP, 0);
 			    if (trypos == NULL && find_last_paren(l, '{', '}'))
 				trypos = find_start_brace(ind_maxcomment);
 
+
 			    if (trypos != NULL)
 			    {
 				curwin->w_cursor.lnum = trypos->lnum + 1;
@@ -7260,7 +7348,7 @@
 					  && lookfor != LOOKFOR_CPP_BASECLASS)
 		    {
 			amount = scope_amount;
-			if (theline[0] == '{')
+			if (*theline == '{' || (ind_js && *theline == '['))
 			    amount += ind_open_extra;
 		    }
 		    break;
@@ -7278,12 +7366,17 @@
 
 		l = ml_get_curline();
 
+		if (ind_js && lookfor == LOOKFOR_INITIAL && cin_ends_in(l, (char_u *)"=", NULL))
+		{
+		    amount += ind_continuation;
+		    break;
+		}
 		/*
 		 * If this is a switch() label, may line up relative to that.
 		 * If this is a C++ scope declaration, do the same.
 		 */
 		iscase = cin_iscase(l, FALSE);
-		if (iscase || cin_isscopedecl(l))
+		if (iscase || (!ind_js && cin_isscopedecl(l)))
 		{
 		    /* we are only looking for cpp base class
 		     * declaration/initialization any longer */
@@ -7315,7 +7408,7 @@
 		     *	    x = 333;
 		     *	case yy:
 		     */
-		    if (       (iscase && lookfor == LOOKFOR_CASE)
+		    if ((iscase && lookfor == LOOKFOR_CASE)
 			    || (iscase && lookfor_break)
 			    || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
 		    {
@@ -7364,9 +7457,10 @@
 		    {
 			amount = n;
 			l = after_label(ml_get_curline());
+			/* NOTE: this makes indenting dependent on 'cinw' */
 			if (l != NULL && cin_is_cinword(l))
 			{
-			    if (theline[0] == '{')
+			    if (*theline == '{' || (ind_js && *theline == '['))
 				amount += ind_open_extra;
 			    else
 				amount += ind_level + ind_no_brace;
@@ -7420,7 +7514,7 @@
 		 * unlocked it)
 		 */
 		l = ml_get_curline();
-		if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
+		if ((!ind_js && cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
 							     || cin_nocode(l))
 		    continue;
 
@@ -7429,7 +7523,7 @@
 		 * constructor initialization?
 		 */						    /* XXX */
 		n = FALSE;
-		if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
+		if (!ind_js && lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
 		{
 		    n = cin_is_cpp_baseclass(&col);
 		    l = ml_get_curline();
@@ -7443,7 +7537,7 @@
 			else
 			    amount += ind_continuation;
 		    }
-		    else if (theline[0] == '{')
+		    else if (*theline == '{') 
 		    {
 			/* Need to find start of the declaration. */
 			lookfor = LOOKFOR_UNTERM;
@@ -7481,7 +7575,9 @@
 		terminated = cin_isterminated(l, FALSE, TRUE);
 
 		if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
-							&& terminated == ','))
+							&& terminated == ',')
+		
+		   )
 		{
 		    /*
 		     * if we're in the middle of a paren thing,
@@ -7494,18 +7590,23 @@
 		     * position the cursor over the rightmost paren, so that
 		     * matching it will take us back to the start of the line.
 		     */
-		    (void)find_last_paren(l, '(', ')');
-		    trypos = find_match_paren(
-				 corr_ind_maxparen(ind_maxparen, &cur_curpos),
-							      ind_maxcomment);
-
+			if (find_last_paren(l, '(', ')'))
+			{
+			    trypos = find_match_paren(
+				    corr_ind_maxparen(ind_maxparen, &cur_curpos),
+				    ind_maxcomment);
+			}
 		    /*
 		     * If we are looking for ',', we also look for matching
 		     * braces.
 		     */
 		    if (trypos == NULL && terminated == ','
-					      && find_last_paren(l, '{', '}'))
+			    && find_last_paren(l, '{', '}'))
 			trypos = find_start_brace(ind_maxcomment);
+		    
+		    if (ind_js && trypos == NULL && terminated == ','
+			    && find_last_paren(l, '[', ']'))
+			trypos = findmatchlimit(NULL, '[', FM_BLOCKSTOP, 0);
 
 		    if (trypos != NULL)
 		    {
@@ -7538,10 +7639,20 @@
 			{
 			    l = ml_get(curwin->w_cursor.lnum - 1);
 			    if (*l == NUL || l[STRLEN(l) - 1] != '\\')
+			    {
 				break;
+			    }
 			    --curwin->w_cursor.lnum;
 			    curwin->w_cursor.col = 0;
 			}
+			if (ind_js) 
+			{
+			    amount = get_indent();
+			    if (!cin_ends_in(l, (char_u *)"[", NULL) 
+				    && !cin_isterminated(l, TRUE, TRUE))
+				amount += ind_continuation;
+			    break;
+			}
 		    }
 
 		    /*
@@ -7560,8 +7671,8 @@
 		     * ->	{
 		     *		}
 		     */
-		    if (terminated != ',' && lookfor != LOOKFOR_TERM
-							 && theline[0] == '{')
+		    if ((terminated != ',') && lookfor != LOOKFOR_TERM
+							 && *theline == '{')
 		    {
 			amount = cur_amount;
 			/*
@@ -7574,7 +7685,7 @@
 			if (*skipwhite(l) != '{')
 			    amount += ind_open_extra;
 
-			if (ind_cpp_baseclass)
+			if (!ind_js && ind_cpp_baseclass)
 			{
 			    /* have to look back, whether it is a cpp base
 			     * class declaration or initialization */
@@ -7598,7 +7709,7 @@
 			 * ->		here;
 			 */
 			if (lookfor == LOOKFOR_UNTERM
-					   || lookfor == LOOKFOR_ENUM_OR_INIT)
+				|| lookfor == LOOKFOR_ENUM_OR_INIT)
 			{
 			    if (cont_amount > 0)
 				amount = cont_amount;
@@ -7621,7 +7732,7 @@
 			 * ->	here;
 			 */
 			amount = cur_amount;
-			if (theline[0] == '{')
+			if (*theline == '{')
 			    amount += ind_open_extra;
 			if (lookfor != LOOKFOR_TERM)
 			{
@@ -7734,6 +7845,7 @@
 			     */
 			    if (lookfor == LOOKFOR_INITIAL && terminated == ',')
 			    {
+				if (ind_js) break;
 				lookfor = LOOKFOR_ENUM_OR_INIT;
 				cont_amount = cin_first_id_amount();
 			    }
@@ -7743,8 +7855,7 @@
 					&& *l != NUL
 					&& l[STRLEN(l) - 1] == '\\')
 								/* XXX */
-				    cont_amount = cin_get_equal_amount(
-						       curwin->w_cursor.lnum);
+				    cont_amount = cin_get_equal_amount(curwin->w_cursor.lnum);
 				if (lookfor != LOOKFOR_TERM)
 				    lookfor = LOOKFOR_UNTERM;
 			    }
@@ -7781,7 +7892,7 @@
 		    {
 			lookfor = LOOKFOR_TERM;
 			amount = get_indent();	    /* XXX */
-			if (theline[0] == '{')
+			if (*theline == '{')
 			    amount += ind_open_extra;
 		    }
 		    ++whilelevel;
@@ -7862,7 +7973,7 @@
 		     * To know what needs to be done look further backward for
 		     * a terminated line.
 		     */
-		    else
+		    else if (!paren_scope_amount)
 		    {
 			/*
 			 * position the cursor over the rightmost paren, so
@@ -7909,10 +8020,15 @@
 			 * Get indent and pointer to text for current line,
 			 * ignoring any jump label.
 			 */
+			//CHECK
+		    if (!ind_js)
 			amount = skip_label(curwin->w_cursor.lnum,
-							  &l, ind_maxcomment);
-
-			if (theline[0] == '{')
+				&l, ind_maxcomment);
+		    else
+			amount = get_indent();
+			
+
+			if (*theline == '{')
 			    amount += ind_open_extra;
 			/* See remark above: "Only add ind_open_extra.." */
 			l = skipwhite(l);
@@ -7945,6 +8061,15 @@
 			 * If we're at the end of a block, skip to the start of
 			 * that block.
 			 */
+			/* 
+			   int a[] =
+			   {
+			       1, 2,
+			       3, 4};
+			       printf("here");
+			   */
+			
+			
 			if (find_last_paren(l, '{', '}')
 				&& (trypos = find_start_brace(ind_maxcomment))
 							    != NULL) /* XXX */
@@ -7963,9 +8088,11 @@
 	    }
 	}
       }
+      if (amount < paren_scope_amount)
+	  amount += paren_scope_amount;
 
       /* add extra indent for a comment */
-      if (cin_iscomment(theline))
+      if (cin_iscomment_start(theline))
 	  amount += ind_comment;
 
       /* subtract extra left-shift for jump labels */
@@ -7988,12 +8115,20 @@
 	 * prevailing indent and make sure it looks like the start
 	 * of a function
 	 */
-
-	if (theline[0] == '{')
+	/* what if we are deep inside multiple open braces?
+	 * ans: this is supposed to be handled before by tryposBrace
+	 */
+   
+	if (!ind_js && theline[0] == '{')
 	{
 	    amount = ind_first_open;
 	}
-
+	else if (cin_iscomment_start(theline))
+	{
+	    /* add extra indent for a comment */
+	    amount = ind_comment;
+	    goto theend;
+	}
 	/*
 	 * If the NEXT line is a function declaration, the current
 	 * line needs to be indented as a function type spec.
@@ -8001,13 +8136,14 @@
 	 * current line is terminated, ie. ends in ';', or if the current line
 	 * contains { or }: "void f() {\n if (1)"
 	 */
-	else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
+	else if (!ind_js /* not for JS code */
+		&& cur_curpos.lnum < curbuf->b_ml.ml_line_count
 		&& !cin_nocode(theline)
 		&& vim_strchr(theline, '{') == NULL
 		&& vim_strchr(theline, '}') == NULL
 		&& !cin_ends_in(theline, (char_u *)":", NULL)
 		&& !cin_ends_in(theline, (char_u *)",", NULL)
-		&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1)
+		&& (cin_isfuncdecl(NULL, cur_curpos.lnum + 1))
 		&& !cin_isterminated(theline, FALSE, TRUE))
 	{
 	    amount = ind_func_type;
@@ -8017,6 +8153,27 @@
 	    amount = 0;
 	    curwin->w_cursor = cur_curpos;
 
+	    /* Flag the else and do..while statements */
+	    iselse =  cin_iselse(theline);
+	    iswhileofdo = (!iselse && cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen));
+
+	    lookfor = LOOKFOR_INITIAL;
+	    if (iselse)
+		lookfor = LOOKFOR_IF;
+	    else if (iswhileofdo)
+		lookfor = LOOKFOR_DO;
+
+	    if (lookfor != LOOKFOR_INITIAL)
+	    {
+		curwin->w_cursor.lnum = cur_curpos.lnum;
+		/* using ourscope as line 0 */
+		if (find_match(lookfor, 0, ind_maxparen,
+							ind_maxcomment) == OK)
+		{
+		    amount = get_indent();	/* XXX */
+		    goto theend;
+		}
+	    }
 	    /* search backwards until we find something we recognize */
 
 	    while (curwin->w_cursor.lnum > 1)
@@ -8026,8 +8183,10 @@
 
 		l = ml_get_curline();
 
-		/*
-		 * If we're in a comment now, skip to the start of the comment.
+		if (cin_nocode(l)) 
+		    continue;
+		 /* 
+		  * If we're in a comment now, skip to the start of the comment.
 		 */						/* XXX */
 		if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
 		{
@@ -8041,7 +8200,7 @@
 		 * constructor initialization?
 		 */						    /* XXX */
 		n = FALSE;
-		if (ind_cpp_baseclass != 0 && theline[0] != '{')
+		if (!ind_js && ind_cpp_baseclass != 0 && theline[0] != '{')
 		{
 		    n = cin_is_cpp_baseclass(&col);
 		    l = ml_get_curline();
@@ -8057,10 +8216,7 @@
 		/*
 		 * Skip preprocessor directives and blank lines.
 		 */
-		if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
-		    continue;
-
-		if (cin_nocode(l))
+		if (!ind_js && cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
 		    continue;
 
 		/*
@@ -8085,6 +8241,30 @@
 						     ind_maxcomment)) != NULL)
 			curwin->w_cursor = *trypos;
 
+		    if(ind_js)
+		    {
+			if(trypos != NULL)
+			    l = skipwhite(ml_get_curline());
+			/* rough check to not add extra indent on
+			 * }, and ], 
+			 */
+			if (*skipwhite(l) == '}' || *skipwhite(l) == ']')
+			{
+			    amount = get_indent();
+			    break;
+			}
+			/* var a = 121,
+			 *     b = 345,
+			 *     c= 34343;
+			 */
+			else if (STRNCMP(skipwhite(l), "var", 3) == 0 
+				&& !vim_isIDc(l[3]))
+			{
+			    amount = get_indent();
+			    amount += ind_continuation;
+			    break;
+			}
+		    }
 		    /* For a line ending in ',' that is a continuation line go
 		     * back to the first line with a backslash:
 		     * char *foo = "bla\
@@ -8094,14 +8274,23 @@
 		    while (n == 0 && curwin->w_cursor.lnum > 1)
 		    {
 			l = ml_get(curwin->w_cursor.lnum - 1);
-			if (*l == NUL || l[STRLEN(l) - 1] != '\\')
+			/* FIXME: get the indent which started 
+			 * the statement which is continued
+			 */
+			if (*l == NUL || (l[STRLEN(l) - 1] != '\\' 
+				    && !cin_ends_in(l, (char_u *)",", NULL)))
 			    break;
+			if (ind_js && (STRNCMP(skipwhite(l), "var", 3) == 0 
+				    && !vim_isIDc(l[3])))
+			{
+			    break;
+			}
+
 			--curwin->w_cursor.lnum;
 			curwin->w_cursor.col = 0;
 		    }
-
 		    amount = get_indent();	    /* XXX */
-
+		    /* -> static struct foo   b,  -> indent of "b" */
 		    if (amount == 0)
 			amount = cin_first_id_amount();
 		    if (amount == 0)
@@ -8109,11 +8298,59 @@
 		    break;
 		}
 
+		/* Try to find parens around here */
+		if (find_last_paren(l, '(', ')') 
+			&& (trypos = find_match_paren(ind_maxparen,
+				ind_maxcomment)) != NULL)
+		{
+		    curwin->w_cursor = *trypos;
+		    l = ml_get_curline();
+		    if (!vim_iswhite(*l))
+			break;
+		    /* This position could be on a
+		     * continuation line. So find the start of this statement
+		     * before getting any indent or deciding on it
+		     * ... 
+		     * checking backwards
+		     */
+		    while (curwin->w_cursor.lnum > 1)
+		    {
+			curwin->w_cursor.lnum--;
+			curwin->w_cursor.col = 0;
+			l = ml_get_curline();
+			if (*l == NUL || cin_isterminated(l,TRUE,FALSE))
+			    break;
+		    }
+		}
+
+		if ((trypos = find_start_brace(ind_maxcomment)) != NULL)
+		{
+		    curwin->w_cursor = *trypos;
+		    l = ml_get_curline();
+		    if (find_last_paren(l, '(', ')') 
+			    && (trypos = find_match_paren(ind_maxparen,
+				    ind_maxcomment)) != NULL)
+		    {
+			curwin->w_cursor = *trypos;
+			l = ml_get_curline();
+		    }
+		    if(!vim_iswhite(*l))
+			break;
+		    /* 
+		     * Assuming that we need to check further only if
+		     * theline is an else or whileofdo
+		     */
+		    if (!iselse && !iswhileofdo)
+			break;
+		    curwin->w_cursor = *trypos;
+		    curwin->w_cursor.lnum ++;
+		    continue;
+		}
 		/*
 		 * If the line looks like a function declaration, and we're
 		 * not in a comment, put it the left margin.
 		 */
-		if (cin_isfuncdecl(NULL, cur_curpos.lnum))  /* XXX */
+		if (!ind_js && cin_isfuncdecl(NULL, cur_curpos.lnum))  /* XXX */
 		    break;
 		l = ml_get_curline();
 
@@ -8121,7 +8358,10 @@
 		 * Finding the closing '}' of a previous function.  Put
 		 * current line at the left margin.  For when 'cino' has "fs".
 		 */
-		if (*skipwhite(l) == '}')
+		/* 
+		 * OK: We would reach here 
+		 */
+		if (*skipwhite(l) == '}' || (ind_js && *skipwhite(l) == ']'))
 		    break;
 
 		/*			    (matching {)
@@ -8129,16 +8369,29 @@
 		 * comments) align at column 0.  For example:
 		 * char *string_array[] = { "foo",
 		 *     / * x * / "b};ar" }; / * foobar * /
+		 *  
+		 *  OR: 
+		 *  if (foo) { bar():}
+		 *  else {baz();}
+		 *  if(lala) { dododo(); } <-- don't indent this line because it does not belong to the else above.
+		 *  else {zzzlzll(); }
+		 *
+		 *  Also,
+		 *  while (foo) dothis();
+		 *  if(1) something();
+		 *
 		 */
-		if (cin_ends_in(l, (char_u *)"};", NULL))
+		if (cin_ends_in(l, (char_u *)"};", NULL) || 
+			(ind_js && (cin_ends_in(l, (char_u *)"}", NULL) || cin_ends_in(l, (char_u *)");", NULL))))
 		    break;
 
 		/*
 		 * If the PREVIOUS line is a function declaration, the current
 		 * line (and the ones that follow) needs to be indented as
 		 * parameters.
+		 * ** NOT if the J flag is set
 		 */
-		if (cin_isfuncdecl(&l, curwin->w_cursor.lnum))
+		if (!ind_js && cin_isfuncdecl(l, curwin->w_cursor.lnum))
 		{
 		    amount = ind_param;
 		    break;
@@ -8160,24 +8413,37 @@
 		    l = ml_get_curline();
 		}
 
-		/*
-		 * Doesn't look like anything interesting -- so just
-		 * use the indent of this line.
-		 *
-		 * Position the cursor over the rightmost paren, so that
-		 * matching it will take us back to the start of the line.
-		 */
-		find_last_paren(l, '(', ')');
-
-		if ((trypos = find_match_paren(ind_maxparen,
-						     ind_maxcomment)) != NULL)
-		    curwin->w_cursor = *trypos;
-		amount = get_indent();	    /* XXX */
+		if (cin_is_cinword(l))
+		{
+		    lookfor = LOOKFOR_INITIAL;
+		    if (cin_iselse(skipwhite(l)))
+			lookfor = LOOKFOR_IF;
+		    else if (cin_iswhileofdo(l, curwin->w_cursor.lnum, ind_maxparen)) 
+			lookfor = LOOKFOR_DO;
+		    if(lookfor != LOOKFOR_INITIAL)
+		    {
+			/* using ourscope as line 0 */
+			if (find_match(lookfor, 0, ind_maxparen,
+				    ind_maxcomment) == OK)
+			{
+			    amount = get_indent();	/* XXX */
+			}
+		    }
+		    else
+		    {
+			amount = get_indent(); /* XXX */
+		    }
+		    if (*theline == '{' || (ind_js && *theline == '['))
+			amount += ind_open_extra;
+		    else
+			amount += ind_level + ind_no_brace;
+		    break;
+		}
 		break;
 	    }
 
 	    /* add extra indent for a comment */
-	    if (cin_iscomment(theline))
+	    if (cin_iscomment_start(theline))
 		amount += ind_comment;
 
 	    /* add extra indent if the previous line ended in a backslash:
@@ -8224,6 +8490,7 @@
     char_u	*mightbeif;
     int		elselevel;
     int		whilelevel;
+    pos_T	*trypos = NULL;
 
     if (lookfor == LOOKFOR_IF)
     {
@@ -8244,17 +8511,23 @@
 	curwin->w_cursor.col = 0;
 
 	look = cin_skipcomment(ml_get_curline());
-	if (cin_iselse(look)
-		|| cin_isif(look)
-		|| cin_isdo(look)			    /* XXX */
-		|| cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
+	if (cin_nocode(look)) continue;
+	if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
+	{
+	    curwin->w_cursor.lnum = trypos->lnum + 1;
+	    continue;
+	}
+	if ((elselevel && (cin_iselse(look)
+		|| cin_isif(look)))
+		|| (whilelevel && (cin_isdo(look)			    /* XXX */
+		|| cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))))
 	{
 	    /*
 	     * if we've gone outside the braces entirely,
 	     * we must be out of scope...
 	     */
 	    theirscope = find_start_brace(ind_maxcomment);  /* XXX */
-	    if (theirscope == NULL)
+	    if (ourscope && theirscope == NULL)
 		break;
 
 	    /*
@@ -8262,7 +8535,7 @@
 	     * back than the one enclosing the else, we're
 	     * out of luck too.
 	     */
-	    if (theirscope->lnum < ourscope)
+	    if (ourscope && theirscope->lnum < ourscope)
 		break;
 
 	    /*
@@ -8270,7 +8543,7 @@
 	     * then we can ignore it because it's in a
 	     * different scope...
 	     */
-	    if (theirscope->lnum > ourscope)
+	    if (ourscope && theirscope->lnum > ourscope)
 		continue;
 
 	    /*
@@ -8279,7 +8552,7 @@
 	     * increment elselevel
 	     */
 	    look = cin_skipcomment(ml_get_curline());
-	    if (cin_iselse(look))
+	    if (elselevel && cin_iselse(look))
 	    {
 		mightbeif = cin_skipcomment(look + 4);
 		if (!cin_isif(mightbeif))
@@ -8291,7 +8564,7 @@
 	     * if it was a "while" then we need to go back to
 	     * another "do", so increment whilelevel.  XXX
 	     */
-	    if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
+	    if (whilelevel && cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
 	    {
 		++whilelevel;
 		continue;
@@ -8299,7 +8572,7 @@
 
 	    /* If it's an "if" decrement elselevel */
 	    look = cin_skipcomment(ml_get_curline());
-	    if (cin_isif(look))
+	    if (elselevel && cin_isif(look))
 	    {
 		elselevel--;
 		/*
@@ -8311,7 +8584,7 @@
 	    }
 
 	    /* If it's a "do" decrement whilelevel */
-	    if (cin_isdo(look))
+	    if (whilelevel && cin_isdo(look))
 		whilelevel--;
 
 	    /*
diff -r 7b50afd31037 -r ad9650969b89 src/proto/misc1.pro
--- a/src/proto/misc1.pro	Fri Aug 19 22:29:02 2011 +0200
+++ b/src/proto/misc1.pro	Sun Aug 21 17:07:19 2011 +0530
@@ -74,6 +74,7 @@
 void add_pathsep __ARGS((char_u *p));
 char_u *FullName_save __ARGS((char_u *fname, int force));
 pos_T *find_start_comment __ARGS((int ind_maxcomment));
+int	cin_iscomment_start __ARGS((char_u *));
 void do_c_expr_indent __ARGS((void));
 int cin_islabel __ARGS((int ind_maxcomment));
 int cin_iscase __ARGS((char_u *s, int strict));
