home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / src / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-09  |  16.5 KB  |  629 lines

  1. /* Indentation functions.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "indent.h"
  25. #include "window.h"
  26. #include "termchar.h"
  27. #include "termopts.h"
  28.  
  29. #define CR '\015'
  30.  
  31. /* Indentation can insert tabs if this is non-zero;
  32.    otherwise always uses spaces */
  33. int indent_tabs_mode;
  34.  
  35. #define min(a, b) ((a) < (b) ? (a) : (b))
  36. #define max(a, b) ((a) > (b) ? (a) : (b))
  37.  
  38. /* These three values memoize the current column to avoid recalculation */
  39. /* Some things in set last_known_column_point to -1
  40.   to mark the memoized value as invalid */
  41. /* Last value returned by current_column */
  42. int last_known_column;
  43. /* Value of point when current_column was called */
  44. int last_known_column_point;
  45. /* Value of MODIFF when current_column was called */
  46. int last_known_column_modified;
  47.  
  48. extern int minibuf_prompt_width;
  49.  
  50. DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
  51.   "Return the horizontal position of point.  Beginning of line is column 0.\n\
  52. This is calculated by adding together the widths of all the displayed\n\
  53. representations of the character between the start of the previous line\n\
  54. and point.  (eg control characters will have a width of 2 or 4, tabs\n\
  55. will have a variable width)\n\
  56. Ignores finite width of screen, which means that this function may return\n\
  57. values greater than (screen-width).\n\
  58. Whether the line is visible (if `selective-display' is t) has no effect.")
  59.   ()
  60. {
  61.   Lisp_Object temp;
  62.   XFASTINT (temp) = current_column ();
  63.   return temp;
  64. }
  65.  
  66. int
  67. current_column ()
  68. {
  69.   register int col;
  70.   register unsigned char *ptr, *stop, c;
  71.   register int tab_seen;
  72.   register int post_tab;
  73.   register int tab_width = XINT (current_buffer->tab_width);
  74.   int ctl_arrow = (!NULL (current_buffer->ctl_arrow))
  75.                   + (EQ (current_buffer->ctl_arrow, Qt));
  76.  
  77.   if (point == last_known_column_point
  78.       && MODIFF == last_known_column_modified)
  79.     return last_known_column;
  80.  
  81.   /* Make a pointer for decrementing through the chars before point.  */
  82.   ptr = &FETCH_CHAR (point - 1) + 1;
  83.   /* Make a pointer to where consecutive chars leave off,
  84.      going backwards from point.  */
  85.   if (point == BEGV)
  86.     stop = ptr;
  87.   else if (point <= GPT || BEGV > GPT)
  88.     stop = BEGV_ADDR;
  89.   else
  90.     stop = GAP_END_ADDR;
  91.  
  92.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  93.  
  94.   col = 0, tab_seen = 0, post_tab = 0;
  95.  
  96.   while (1)
  97.     {
  98.       if (ptr == stop)
  99.     {
  100.       /* We stopped either for the beginning of the buffer
  101.          or for the gap.  */
  102.       if (ptr == BEGV_ADDR)
  103.         break;
  104.       /* It was the gap.  Jump back over it.  */
  105.       stop = BEGV_ADDR;
  106.       ptr = GPT_ADDR;
  107.       /* Check whether that brings us to beginning of buffer.  */
  108.       if (BEGV >= GPT) break;
  109.     }
  110.  
  111.       c = *--ptr;
  112.       if (c >= 040 && c < 0177)
  113.     {
  114.       col++;
  115.     }
  116.       else if (c == '\n')
  117.     break;
  118.       else if (c == '\r' && EQ (current_buffer->selective_display, Qt))
  119.     break;
  120.       else if (c == '\t')
  121.     {
  122.       if (tab_seen)
  123.         col = ((col + tab_width) / tab_width) * tab_width;
  124.  
  125.       post_tab += col;
  126.       col = 0;
  127.       tab_seen = 1;
  128.     }
  129.       else
  130.       col += (ctl_arrow == 1 && c >= 0177) ? 1 :
  131.                (ctl_arrow && c < 0200) ? 2 : 4;
  132.     }
  133.  
  134.   if (tab_seen)
  135.     {
  136.       col = ((col + tab_width) / tab_width) * tab_width;
  137.       col += post_tab;
  138.     }
  139.  
  140.   last_known_column = col;
  141.   last_known_column_point = point;
  142.   last_known_column_modified = MODIFF;
  143.  
  144.   return col;
  145. }
  146.  
  147. ToCol (col)
  148.      int col;
  149. {
  150.   register int fromcol = current_column ();
  151.   register int n;
  152.   register int tab_width = XINT (current_buffer->tab_width);
  153.  
  154.   if (fromcol > col)
  155.     return;
  156.  
  157.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  158.  
  159.   if (indent_tabs_mode)
  160.     {
  161.       n = col / tab_width - fromcol / tab_width;
  162.       if (n)
  163.     {
  164.       while (n-- > 0)
  165.         insert ("\t", 1);
  166.  
  167.       fromcol = (col / tab_width) * tab_width;
  168.     }
  169.     }
  170.  
  171.   while (fromcol < col)
  172.     {
  173.       insert ("        ", min (8, col - fromcol));
  174.       fromcol += min (8, col - fromcol);
  175.     }
  176.  
  177.   last_known_column = col;
  178.   last_known_column_point = point;
  179.   last_known_column_modified = MODIFF;
  180. }
  181.  
  182. DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
  183.   "Indent from point with tabs and spaces until COLUMN is reached.\n\
  184. Always do at least MIN spaces even if that goes past COLUMN;\n\
  185. by default, MIN is zero.")
  186.   (col, minimum)
  187.      Lisp_Object col, minimum;
  188. {
  189.   int mincol;
  190.   register int fromcol;
  191.   register int tab_width = XINT (current_buffer->tab_width);
  192.  
  193.   CHECK_NUMBER (col, 0);
  194.   if (NULL (minimum))
  195.     XFASTINT (minimum) = 0;
  196.   CHECK_NUMBER (minimum, 1);
  197.  
  198.   fromcol = current_column ();
  199.   mincol = fromcol + XINT (minimum);
  200.   if (mincol < XINT (col)) mincol = XINT (col);
  201.  
  202.   if (fromcol == mincol)
  203.     return make_number (fromcol);
  204.  
  205.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  206.  
  207.   if (indent_tabs_mode)
  208.     {
  209.       Lisp_Object n;
  210.       XFASTINT (n) = mincol / tab_width - fromcol / tab_width;
  211.       if (XFASTINT (n) != 0)
  212.     {
  213.       Finsert_char (make_number ('\t'), n);
  214.  
  215.       fromcol = (mincol / tab_width) * tab_width;
  216.     }
  217.     }
  218.  
  219.   XFASTINT (col) = mincol - fromcol;
  220.   Finsert_char (make_number (' '), col);
  221.  
  222.   last_known_column = mincol;
  223.   last_known_column_point = point;
  224.   last_known_column_modified = MODIFF;
  225.  
  226.   XSETINT (col, mincol);
  227.   return col;
  228. }
  229.  
  230. DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
  231.   0, 0, 0,
  232.   "Return the indentation of the current line.\n\
  233. This is the horizontal position of the character\n\
  234. following any initial whitespace.")
  235.   ()
  236. {
  237.   Lisp_Object val;
  238.  
  239.   XFASTINT (val) = position_indentation (find_next_newline (point, -1));
  240.   return val;
  241. }
  242.  
  243. position_indentation (pos)
  244.      register int pos;
  245. {
  246.   register int column = 0;
  247.   register int tab_width = XINT (current_buffer->tab_width);
  248.   register unsigned char *p;
  249.   register unsigned char *stop;
  250.  
  251.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  252.  
  253.   stop = &FETCH_CHAR (BufferSafeCeiling (pos)) + 1;
  254.   p = &FETCH_CHAR (pos);
  255.   while (1)
  256.     {
  257.       while (p == stop)
  258.     {
  259.       if (pos == ZV)
  260.         return column;
  261.       pos += p - &FETCH_CHAR (pos);
  262.       p = &FETCH_CHAR (pos);
  263.       stop = &FETCH_CHAR (BufferSafeCeiling (pos)) + 1;
  264.     }
  265.       switch (*p++)
  266.     {
  267.     case ' ':
  268.       column++;
  269.       break;
  270.     case '\t':
  271.       column += tab_width - column % tab_width;
  272.       break;
  273.     default:
  274.       return column;
  275.     }
  276.     }
  277. }
  278.  
  279. DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 1, 0,
  280.   "Move point to column COLUMN in the current line.\n\
  281. COLUMN is calculated by adding together the widths of all the displayed\n\
  282. representations of the character between the start of the previous line\n\
  283. and point.  (eg control characters will have a width of 2 or 4, tabs\n\
  284. will have a variable width)\n\
  285. Ignores finite width of screen, which means that this function may be\n\
  286. passed values greater than (screen-width)")
  287.   (column)
  288.      Lisp_Object column;
  289. {
  290.   register int pos = point;
  291.   register int col = current_column ();
  292.   register int goal;
  293.   register int end = ZV;
  294.   register int tab_width = XINT (current_buffer->tab_width);
  295.   register int ctl_arrow = (!NULL (current_buffer->ctl_arrow))
  296.                          + (EQ (current_buffer->ctl_arrow, Qt));
  297.  
  298.   Lisp_Object val;
  299.  
  300.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  301.   CHECK_NUMBER (column, 0);
  302.   goal = XINT (column);
  303.   if (col > goal)
  304.     {
  305.       pos = find_next_newline (pos, -1);
  306.       col = 0;
  307.     }
  308.  
  309.   while (col < goal && pos < end)
  310.     {
  311.       int c = FETCH_CHAR (pos);
  312.       if (c == '\n')
  313.     break;
  314.       if (c == '\r' && EQ (current_buffer->selective_display, Qt))
  315.     break;
  316.       pos++;
  317.       col++;
  318.       if (c == '\t')
  319.     {
  320.       col += tab_width - 1;
  321.       col = col / tab_width * tab_width;
  322.     }
  323.       else if (ctl_arrow == 1 && c >= 040)
  324.       continue;
  325.       else if (ctl_arrow && (c < 040 || c == 0177))
  326.         col++;
  327.       else if (c < 040 || c >= 0177)
  328.         col += 3;
  329.     }
  330.  
  331.   SET_PT (pos);
  332.  
  333.   last_known_column = col;
  334.   last_known_column_point = point;
  335.   last_known_column_modified = MODIFF;
  336.  
  337.   XFASTINT (val) = col;
  338.   return val;
  339. }
  340.  
  341. struct position val_compute_motion;
  342.  
  343. struct position *
  344. compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, tab_offset)
  345.      int from, fromvpos, fromhpos, to, tovpos, tohpos;
  346.      register int width;
  347.      int hscroll, tab_offset;
  348. {
  349. /* Note that `cpos' is CURRENT_VPOS << SHORTBITS + CURRENT_HPOS,
  350.    and that CURRENT_HPOS may be negative.  Use these macros
  351.    to extract the hpos or the vpos from cpos or anything like it.
  352.  */
  353. #ifndef SHORT_CAST_BUG
  354. #define HPOS(VAR) (short) (VAR)
  355. #else
  356. #define HPOS(VAR) (((VAR) & (1 << (SHORTBITS - 1)) \
  357.             ? ~((1 << SHORTBITS) - 1) : 0) \
  358.            | (VAR) & ((1 << SHORTBITS) - 1))
  359. /* #define HPOS(VAR) (((VAR) & 0x8000 ? 0xffff0000 : 0) | ((VAR) & 0xffff)) */
  360. #endif /* SHORT_CAST_BUG */
  361.  
  362. #define VPOS(VAR) (((VAR) >> SHORTBITS) + (HPOS (VAR) < 0))
  363.  
  364.  
  365. #ifndef TAHOE_REGISTER_BUG
  366.   register
  367. #endif /* TAHOE_REGISTER_BUG */
  368.     int cpos = fromhpos + (fromvpos << SHORTBITS);
  369.   register int target = tohpos + (tovpos << SHORTBITS);
  370.   register int pos;
  371.   register int c;
  372.   register int tab_width = XFASTINT (current_buffer->tab_width);
  373.   register int ctl_arrow = (!NULL (current_buffer->ctl_arrow))
  374.                          + (EQ (current_buffer->ctl_arrow, Qt));
  375.   int selective
  376.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  377.       ? XINT (current_buffer->selective_display)
  378.     : !NULL (current_buffer->selective_display) ? -1 : 0;
  379.   int prevpos;
  380.  
  381.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  382.   for (pos = from; pos < to && cpos < target; pos++)
  383.     {
  384.       prevpos = cpos;
  385.       c = FETCH_CHAR (pos);
  386.       if (c >= 040 && c < 0177)
  387.     cpos++;
  388.       else if (c == '\t')
  389.     {
  390.       cpos += tab_width
  391.         - HPOS (cpos + tab_offset + hscroll - (hscroll > 0)
  392.             /* Add tab_width here to make sure positive.
  393.                cpos can be negative after continuation
  394.                but can't be less than -tab_width.  */
  395.             + tab_width)
  396.           % tab_width;
  397.     }
  398.       else if (c == '\n')
  399.     {
  400.       if (selective > 0 && position_indentation (pos + 1) >= selective)
  401.         {
  402.           /* Skip any number of invisible lines all at once */
  403.           do
  404.         {
  405.           while (++pos < to && FETCH_CHAR(pos) != '\n');
  406.         }
  407.           while (selective > 0 && position_indentation (pos + 1) >= selective);
  408.           pos--;
  409.           /* Allow for the " ..." that is displayed for them. */
  410.           if (!NULL (current_buffer->selective_display_ellipses))
  411.         {
  412.           cpos += 4;
  413.           if (HPOS (cpos) >= width)
  414.             cpos -= HPOS (cpos) - width;
  415.         }
  416.         }
  417.       else
  418.         cpos += (1 << SHORTBITS) - HPOS (cpos);
  419.       cpos -= hscroll;
  420.       if (hscroll > 0) cpos++; /* Count the ! on column 0 */
  421.       tab_offset = 0;
  422.     }
  423.       else if (c == CR && selective < 0)
  424.     {
  425.       /* In selective display mode,
  426.          everything from a ^M to the end of the line is invisible */
  427.       while (pos < to && FETCH_CHAR(pos) != '\n') pos++;
  428.       pos--;
  429.       /* Allow for the " ..." that is displayed for them. */
  430.       if (!NULL (current_buffer->selective_display_ellipses))
  431.         {
  432.           cpos += 4;
  433.           if (HPOS (cpos) >= width)
  434.         cpos -= HPOS (cpos) - width;
  435.         }
  436.     }
  437.       else
  438.         cpos += (ctl_arrow == 1 && c >= 040 ) ? 1 : 
  439.             (ctl_arrow && c < 0200) ? 2: 4;
  440.  
  441.       if (HPOS (cpos) >= width
  442.       && (HPOS (cpos) > width
  443.           || (pos < ZV - 1
  444.           && FETCH_CHAR (pos + 1) != '\n')))
  445.     {
  446.       if (cpos >= target)
  447.         break;
  448.       if (hscroll
  449.           || (truncate_partial_width_windows
  450.           && width + 1 < screen_width)
  451.           || !NULL (current_buffer->truncate_lines))
  452.         {
  453.           while (pos < to && FETCH_CHAR(pos) != '\n') pos++;
  454.           pos--;
  455.         }
  456.       else
  457.         {
  458.           cpos += (1 << SHORTBITS) - width;
  459.           tab_offset += width;
  460.         }
  461.  
  462.     }
  463.     }
  464.  
  465.   val_compute_motion.bufpos = pos;
  466.   val_compute_motion.hpos = HPOS (cpos);
  467.   val_compute_motion.vpos = VPOS (cpos);
  468.   val_compute_motion.prevhpos = HPOS (prevpos);
  469.  
  470.   /* Nonzero if have just continued a line */
  471.   val_compute_motion.contin
  472.     = pos != from
  473.       && (val_compute_motion.vpos != VPOS (prevpos))
  474.       && c != '\n';
  475.  
  476.   return &val_compute_motion;
  477. }
  478. #undef HPOS
  479. #undef VPOS
  480.  
  481.  
  482. pos_tab_offset (w, pos)
  483.      struct window *w;
  484.      register int pos;
  485. {
  486.   int opoint = point;
  487.   int col;
  488.  
  489.   if (pos == BEGV || FETCH_CHAR (pos - 1) == '\n')
  490.     return 0;
  491.   SET_PT (pos);
  492.   col = current_column ();
  493.   SET_PT (opoint);
  494.   return col - (col % (XFASTINT (w->width) - 1));
  495. }
  496.  
  497. /* start_hpos is the hpos of the first character of the buffer:
  498.    zero except for the minibuffer window,
  499.    where it is the width of the prompt.  */
  500.  
  501. struct position val_vmotion;
  502.  
  503. struct position *
  504. vmotion (from, vtarget, width, hscroll, window)
  505.      register int from, vtarget, width;
  506.      int hscroll;
  507.      Lisp_Object window;
  508. {
  509.   struct position pos;
  510.   /* vpos is cumulative vertical position, changed as from is changed */
  511.   register int vpos = 0;
  512.   register int prevline;
  513.   register int first;
  514.   int lmargin = hscroll > 0 ? 1 - hscroll : 0;
  515.   int selective
  516.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  517.       ? XINT (current_buffer->selective_display)
  518.     : !NULL (current_buffer->selective_display) ? -1 : 0;
  519.   int start_hpos = (EQ (window, minibuf_window) ? minibuf_prompt_width : 0);
  520.  
  521.  retry:
  522.   if (vtarget > vpos)
  523.     {
  524.       /* Moving downward is simple, but must calculate from beg of line 
  525.      to determine hpos of starting point */
  526.       if (from > BEGV && FETCH_CHAR (from - 1) != '\n')
  527.     {
  528.       prevline = find_next_newline (from, -1);
  529.       while (selective > 0
  530.          && prevline > BEGV
  531.          && position_indentation (prevline) >= selective)
  532.         prevline = find_next_newline (prevline - 1, -1);
  533.       pos = *compute_motion (prevline, 0,
  534.                  lmargin + (prevline == 1 ? start_hpos : 0),
  535.                  from, 10000, 10000,
  536.                  width, hscroll, 0);
  537.     }
  538.       else
  539.     {
  540.       pos.hpos = lmargin + (from == 1 ? start_hpos : 0);
  541.       pos.vpos = 0;
  542.     }
  543.       return compute_motion (from, vpos, pos.hpos,
  544.                  ZV, vtarget, - (1 << (SHORTBITS - 1)),
  545.                  width, hscroll, pos.vpos * width);
  546.     }
  547.  
  548.   /* To move upward, go a line at a time until
  549.      we have gone at least far enough */
  550.  
  551.   first = 1;
  552.  
  553.   while ((vpos > vtarget || first) && from > BEGV)
  554.     {
  555.       prevline = from;
  556.       while (1)
  557.     {
  558.       prevline = find_next_newline (prevline - 1, -1);
  559.       if (prevline == BEGV
  560.           || selective <= 0
  561.           || position_indentation (prevline) < selective)
  562.         break;
  563.     }
  564.       pos = *compute_motion (prevline, 0,
  565.                  lmargin + (prevline == 1 ? start_hpos : 0),
  566.                  from, 10000, 10000,
  567.                  width, hscroll, 0);
  568.       vpos -= pos.vpos;
  569.       first = 0;
  570.       from = prevline;
  571.     }
  572.  
  573.   /* If we made exactly the desired vertical distance,
  574.      or if we hit beginning of buffer,
  575.      return point found */
  576.   if (vpos >= vtarget)
  577.     {
  578.       val_vmotion.bufpos = from;
  579.       val_vmotion.vpos = vpos;
  580.       val_vmotion.hpos = lmargin;
  581.       val_vmotion.contin = 0;
  582.       val_vmotion.prevhpos = 0;
  583.       return &val_vmotion;
  584.     }
  585.   
  586.   /* Otherwise find the correct spot by moving down */
  587.   goto retry;
  588. }
  589.  
  590. DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 1, 0,
  591.   "Move to start of screen line LINES lines down.\n\
  592. If LINES is negative, this is moving up.\n\
  593. Sets point to position found; this may be start of line\n\
  594.  or just the start of a continuation line.\n\
  595. Returns number of lines moved; may be closer to zero than LINES\n\
  596.  if beginning or end of buffer was reached.")
  597.   (lines)
  598.      Lisp_Object lines;
  599. {
  600.   struct position pos;
  601.   register struct window *w = XWINDOW (selected_window);
  602.  
  603.   CHECK_NUMBER (lines, 0);
  604.  
  605.   pos = *vmotion (point, XINT (lines),
  606.           XFASTINT (w->width) - 1
  607.           - (XFASTINT (w->width) + XFASTINT (w->left)
  608.              != XFASTINT (XWINDOW (minibuf_window)->width)),
  609.           /* Not XFASTINT since perhaps could be negative */
  610.           XINT (w->hscroll), selected_window);
  611.  
  612.   SET_PT (pos.bufpos);
  613.   return make_number (pos.vpos);
  614. }
  615.  
  616. syms_of_indent ()
  617. {
  618.   DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
  619.     "*Indentation can insert tabs if this is non-nil.\n\
  620. Setting this variable automatically makes it local to the current buffer.");
  621.   indent_tabs_mode = 1;
  622.  
  623.   defsubr (&Scurrent_indentation);
  624.   defsubr (&Sindent_to);
  625.   defsubr (&Scurrent_column);
  626.   defsubr (&Smove_to_column);
  627.   defsubr (&Svertical_motion);
  628. }
  629.