wordproc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* wordproc.c - word-processor mode for the editor: does dynamic
  2. paragraph formatting.
  3. Copyright (C) 1996 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. /** \file
  18. * \brief Source: word-processor mode for the editor: does dynamic paragraph formatting
  19. * \author Paul Sheer
  20. * \date 1996
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <stdlib.h>
  32. #include "../src/global.h"
  33. #include "edit-impl.h"
  34. #include "edit-widget.h"
  35. #include "../src/main.h" /* option_tab_spacing */
  36. #define tab_width option_tab_spacing
  37. #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
  38. #define FONT_MEAN_WIDTH 1
  39. static long
  40. line_start (WEdit *edit, long line)
  41. {
  42. long p, l;
  43. l = edit->curs_line;
  44. p = edit->curs1;
  45. if (line < l)
  46. p = edit_move_backward (edit, p, l - line);
  47. else if (line > l)
  48. p = edit_move_forward (edit, p, line - l, 0);
  49. p = edit_bol (edit, p);
  50. while (strchr ("\t ", edit_get_byte (edit, p)))
  51. p++;
  52. return p;
  53. }
  54. static int bad_line_start (WEdit * edit, long p)
  55. {
  56. int c;
  57. c = edit_get_byte (edit, p);
  58. if (c == '.') { /* `...' is acceptable */
  59. if (edit_get_byte (edit, p + 1) == '.')
  60. if (edit_get_byte (edit, p + 2) == '.')
  61. return 0;
  62. return 1;
  63. }
  64. if (c == '-') {
  65. if (edit_get_byte (edit, p + 1) == '-')
  66. if (edit_get_byte (edit, p + 2) == '-')
  67. return 0; /* `---' is acceptable */
  68. return 1;
  69. }
  70. if (strchr (NO_FORMAT_CHARS_START, c))
  71. return 1;
  72. return 0;
  73. }
  74. /*
  75. * Find the start of the current paragraph for the purpose of formatting.
  76. * Return position in the file.
  77. */
  78. static long
  79. begin_paragraph (WEdit *edit, int force)
  80. {
  81. long i;
  82. for (i = edit->curs_line - 1; i >= 0; i--) {
  83. if (line_is_blank (edit, i)) {
  84. i++;
  85. break;
  86. }
  87. if (force) {
  88. if (bad_line_start (edit, line_start (edit, i))) {
  89. i++;
  90. break;
  91. }
  92. }
  93. }
  94. return edit_move_backward (edit, edit_bol (edit, edit->curs1),
  95. edit->curs_line - i);
  96. }
  97. /*
  98. * Find the end of the current paragraph for the purpose of formatting.
  99. * Return position in the file.
  100. */
  101. static long
  102. end_paragraph (WEdit *edit, int force)
  103. {
  104. int i;
  105. for (i = edit->curs_line + 1; i <= edit->total_lines; i++) {
  106. if (line_is_blank (edit, i)) {
  107. i--;
  108. break;
  109. }
  110. if (force)
  111. if (bad_line_start (edit, line_start (edit, i))) {
  112. i--;
  113. break;
  114. }
  115. }
  116. return edit_eol (edit,
  117. edit_move_forward (edit, edit_bol (edit, edit->curs1),
  118. i - edit->curs_line, 0));
  119. }
  120. static unsigned char *
  121. get_paragraph (WEdit *edit, long p, long q, int indent, int *size)
  122. {
  123. unsigned char *s, *t;
  124. #if 0
  125. t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length +
  126. 10);
  127. #else
  128. t = g_try_malloc (2 * (q - p) + 100);
  129. #endif
  130. if (t == NULL)
  131. return NULL;
  132. for (s = t; p < q; p++, s++) {
  133. if (indent)
  134. if (edit_get_byte (edit, p - 1) == '\n')
  135. while (strchr ("\t ", edit_get_byte (edit, p)))
  136. p++;
  137. *s = edit_get_byte (edit, p);
  138. }
  139. *size = (unsigned long) s - (unsigned long) t;
  140. t[*size] = '\n';
  141. return t;
  142. }
  143. static inline void
  144. strip_newlines (unsigned char *t, int size)
  145. {
  146. unsigned char *p = t;
  147. while (size-- != 0) {
  148. if (*p == '\n')
  149. *p = ' ';
  150. p++;
  151. }
  152. }
  153. /*
  154. This function calculates the number of chars in a line specified to length l in pixels
  155. */
  156. static inline int
  157. next_tab_pos (int x)
  158. {
  159. return x += tab_width - x % tab_width;
  160. }
  161. static inline int
  162. line_pixel_length (unsigned char *t, long b, int l)
  163. {
  164. int x = 0, c, xn = 0;
  165. for (;;) {
  166. c = t[b];
  167. switch (c) {
  168. case '\n':
  169. return b;
  170. case '\t':
  171. xn = next_tab_pos (x);
  172. break;
  173. default:
  174. xn = x + 1;
  175. break;
  176. }
  177. if (xn > l)
  178. break;
  179. x = xn;
  180. b++;
  181. }
  182. return b;
  183. }
  184. static int
  185. next_word_start (unsigned char *t, int q, int size)
  186. {
  187. int i;
  188. int saw_ws = 0;
  189. for (i = q; i < size; i++) {
  190. switch (t[i]) {
  191. case '\n':
  192. return -1;
  193. case '\t':
  194. case ' ':
  195. saw_ws = 1;
  196. break;
  197. default:
  198. if (saw_ws != 0)
  199. return i;
  200. break;
  201. }
  202. }
  203. return -1;
  204. }
  205. /* find the start of a word */
  206. static inline int
  207. word_start (unsigned char *t, int q, int size)
  208. {
  209. int i = q;
  210. if (t[q] == ' ' || t[q] == '\t')
  211. return next_word_start (t, q, size);
  212. for (;;) {
  213. int c;
  214. if (!i)
  215. return -1;
  216. c = t[i - 1];
  217. if (c == '\n')
  218. return -1;
  219. if (c == ' ' || c == '\t')
  220. return i;
  221. i--;
  222. }
  223. }
  224. /* replaces ' ' with '\n' to properly format a paragraph */
  225. static inline void
  226. format_this (unsigned char *t, int size, int indent)
  227. {
  228. int q = 0, ww;
  229. strip_newlines (t, size);
  230. ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent;
  231. if (ww < FONT_MEAN_WIDTH * 2)
  232. ww = FONT_MEAN_WIDTH * 2;
  233. for (;;) {
  234. int p;
  235. q = line_pixel_length (t, q, ww);
  236. if (q > size)
  237. break;
  238. if (t[q] == '\n')
  239. break;
  240. p = word_start (t, q, size);
  241. if (p == -1)
  242. q = next_word_start (t, q, size); /* Return the end of the word if the beginning
  243. of the word is at the beginning of a line
  244. (i.e. a very long word) */
  245. else
  246. q = p;
  247. if (q == -1) /* end of paragraph */
  248. break;
  249. if (q)
  250. t[q - 1] = '\n';
  251. }
  252. }
  253. static inline void
  254. replace_at (WEdit * edit, long q, int c)
  255. {
  256. edit_cursor_move (edit, q - edit->curs1);
  257. edit_delete (edit, 1);
  258. edit_insert_ahead (edit, c);
  259. }
  260. /* replaces a block of text */
  261. static inline void
  262. put_paragraph (WEdit * edit, unsigned char *t, long p, int indent, int size)
  263. {
  264. long cursor;
  265. int i, c = 0;
  266. cursor = edit->curs1;
  267. if (indent)
  268. while (strchr ("\t ", edit_get_byte (edit, p)))
  269. p++;
  270. for (i = 0; i < size; i++, p++) {
  271. if (i && indent) {
  272. if (t[i - 1] == '\n' && c == '\n') {
  273. while (strchr ("\t ", edit_get_byte (edit, p)))
  274. p++;
  275. } else if (t[i - 1] == '\n') {
  276. long curs;
  277. edit_cursor_move (edit, p - edit->curs1);
  278. curs = edit->curs1;
  279. edit_insert_indent (edit, indent);
  280. if (cursor >= curs)
  281. cursor += edit->curs1 - p;
  282. p = edit->curs1;
  283. } else if (c == '\n') {
  284. edit_cursor_move (edit, p - edit->curs1);
  285. while (strchr ("\t ", edit_get_byte (edit, p))) {
  286. edit_delete (edit, 1);
  287. if (cursor > edit->curs1)
  288. cursor--;
  289. }
  290. p = edit->curs1;
  291. }
  292. }
  293. c = edit_get_byte (edit, p);
  294. if (c != t[i])
  295. replace_at (edit, p, t[i]);
  296. }
  297. edit_cursor_move (edit, cursor - edit->curs1); /* restore cursor position */
  298. }
  299. static inline int
  300. test_indent (WEdit * edit, long p, long q)
  301. {
  302. int indent;
  303. indent = edit_indent_width (edit, p++);
  304. if (!indent)
  305. return 0;
  306. for (; p < q; p++)
  307. if (edit_get_byte (edit, p - 1) == '\n')
  308. if (indent != edit_indent_width (edit, p))
  309. return 0;
  310. return indent;
  311. }
  312. void
  313. format_paragraph (WEdit *edit, int force)
  314. {
  315. long p, q;
  316. int size;
  317. unsigned char *t;
  318. int indent = 0;
  319. if (option_word_wrap_line_length < 2)
  320. return;
  321. if (line_is_blank (edit, edit->curs_line))
  322. return;
  323. p = begin_paragraph (edit, force);
  324. q = end_paragraph (edit, force);
  325. indent = test_indent (edit, p, q);
  326. t = get_paragraph (edit, p, q, indent, &size);
  327. if (!t)
  328. return;
  329. if (!force) {
  330. int i;
  331. if (strchr (NO_FORMAT_CHARS_START, *t)) {
  332. g_free (t);
  333. return;
  334. }
  335. for (i = 0; i < size - 1; i++) {
  336. if (t[i] == '\n') {
  337. if (strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1])) {
  338. g_free (t);
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. format_this (t, q - p, indent);
  345. put_paragraph (edit, t, p, indent, size);
  346. g_free (t);
  347. /* Scroll left as much as possible to show the formatted paragraph */
  348. edit_scroll_left (edit, -edit->start_col);
  349. }