wordproc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #define tab_width option_tab_spacing
  36. #define NO_FORMAT_CHARS_START "-+*\\,.;:&>"
  37. #define FONT_MEAN_WIDTH 1
  38. static long
  39. line_start (WEdit *edit, long line)
  40. {
  41. long p, l;
  42. l = edit->curs_line;
  43. p = edit->curs1;
  44. if (line < l)
  45. p = edit_move_backward (edit, p, l - line);
  46. else if (line > l)
  47. p = edit_move_forward (edit, p, line - l, 0);
  48. p = edit_bol (edit, p);
  49. while (strchr ("\t ", edit_get_byte (edit, p)))
  50. p++;
  51. return p;
  52. }
  53. static int bad_line_start (WEdit * edit, long p)
  54. {
  55. int c;
  56. c = edit_get_byte (edit, p);
  57. if (c == '.') { /* `...' is acceptable */
  58. if (edit_get_byte (edit, p + 1) == '.')
  59. if (edit_get_byte (edit, p + 2) == '.')
  60. return 0;
  61. return 1;
  62. }
  63. if (c == '-') {
  64. if (edit_get_byte (edit, p + 1) == '-')
  65. if (edit_get_byte (edit, p + 2) == '-')
  66. return 0; /* `---' is acceptable */
  67. return 1;
  68. }
  69. if (strchr (NO_FORMAT_CHARS_START, c))
  70. return 1;
  71. return 0;
  72. }
  73. /*
  74. * Find the start of the current paragraph for the purpose of formatting.
  75. * Return position in the file.
  76. */
  77. static long
  78. begin_paragraph (WEdit *edit, int force)
  79. {
  80. int i;
  81. for (i = edit->curs_line - 1; i >= 0; i--) {
  82. if (line_is_blank (edit, i)) {
  83. i++;
  84. break;
  85. }
  86. if (force) {
  87. if (bad_line_start (edit, line_start (edit, i))) {
  88. i++;
  89. break;
  90. }
  91. }
  92. }
  93. return edit_move_backward (edit, edit_bol (edit, edit->curs1),
  94. edit->curs_line - i);
  95. }
  96. /*
  97. * Find the end of the current paragraph for the purpose of formatting.
  98. * Return position in the file.
  99. */
  100. static long
  101. end_paragraph (WEdit *edit, int force)
  102. {
  103. int i;
  104. for (i = edit->curs_line + 1; i <= edit->total_lines; i++) {
  105. if (line_is_blank (edit, i)) {
  106. i--;
  107. break;
  108. }
  109. if (force)
  110. if (bad_line_start (edit, line_start (edit, i))) {
  111. i--;
  112. break;
  113. }
  114. }
  115. return edit_eol (edit,
  116. edit_move_forward (edit, edit_bol (edit, edit->curs1),
  117. i - edit->curs_line, 0));
  118. }
  119. static unsigned char *
  120. get_paragraph (WEdit *edit, long p, long q, int indent, int *size)
  121. {
  122. unsigned char *s, *t;
  123. #if 0
  124. t = g_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length +
  125. 10);
  126. #else
  127. t = g_malloc (2 * (q - p) + 100);
  128. #endif
  129. if (!t)
  130. return 0;
  131. for (s = t; p < q; p++, s++) {
  132. if (indent)
  133. if (edit_get_byte (edit, p - 1) == '\n')
  134. while (strchr ("\t ", edit_get_byte (edit, p)))
  135. p++;
  136. *s = edit_get_byte (edit, p);
  137. }
  138. *size = (unsigned long) s - (unsigned long) t;
  139. t[*size] = '\n';
  140. return t;
  141. }
  142. static void strip_newlines (unsigned char *t, int size)
  143. {
  144. unsigned char *p = t;
  145. while (size--) {
  146. *p = *p == '\n' ? ' ' : *p;
  147. p++;
  148. }
  149. }
  150. /*
  151. This is a copy of the function
  152. int calc_text_pos (WEdit * edit, long b, long *q, int l)
  153. in propfont.c :(
  154. It calculates the number of chars in a line specified to length l in pixels
  155. */
  156. static inline int next_tab_pos (int x)
  157. {
  158. return x += tab_width - x % tab_width;
  159. }
  160. static int line_pixel_length (unsigned char *t, long b, int l)
  161. {
  162. int x = 0, c, xn = 0;
  163. for (;;) {
  164. c = t[b];
  165. switch (c) {
  166. case '\n':
  167. return b;
  168. case '\t':
  169. xn = next_tab_pos (x);
  170. break;
  171. default:
  172. xn = x + 1;
  173. break;
  174. }
  175. if (xn > l)
  176. break;
  177. x = xn;
  178. b++;
  179. }
  180. return b;
  181. }
  182. static int
  183. next_word_start (unsigned char *t, int q, int size)
  184. {
  185. int i;
  186. int saw_ws = 0;
  187. for (i = q; i < size; i++) {
  188. switch (t[i]) {
  189. case '\n':
  190. return -1;
  191. case '\t':
  192. case ' ':
  193. saw_ws = 1;
  194. break;
  195. default:
  196. if (saw_ws != 0)
  197. return i;
  198. break;
  199. }
  200. }
  201. return -1;
  202. }
  203. /* find the start of a word */
  204. static int
  205. word_start (unsigned char *t, int q, int size)
  206. {
  207. int i = q;
  208. if (t[q] == ' ' || t[q] == '\t')
  209. return next_word_start (t, q, size);
  210. for (;;) {
  211. int c;
  212. if (!i)
  213. return -1;
  214. c = t[i - 1];
  215. if (c == '\n')
  216. return -1;
  217. if (c == ' ' || c == '\t')
  218. return i;
  219. i--;
  220. }
  221. }
  222. /* replaces ' ' with '\n' to properly format a paragraph */
  223. static void format_this (unsigned char *t, int size, int indent)
  224. {
  225. int q = 0, ww;
  226. strip_newlines (t, size);
  227. ww = option_word_wrap_line_length * FONT_MEAN_WIDTH - indent;
  228. if (ww < FONT_MEAN_WIDTH * 2)
  229. ww = FONT_MEAN_WIDTH * 2;
  230. for (;;) {
  231. int p;
  232. q = line_pixel_length (t, q, ww);
  233. if (q > size)
  234. break;
  235. if (t[q] == '\n')
  236. break;
  237. p = word_start (t, q, size);
  238. if (p == -1)
  239. q = next_word_start (t, q, size); /* Return the end of the word if the beginning
  240. of the word is at the beginning of a line
  241. (i.e. a very long word) */
  242. else
  243. q = p;
  244. if (q == -1) /* end of paragraph */
  245. break;
  246. if (q)
  247. t[q - 1] = '\n';
  248. }
  249. }
  250. static void replace_at (WEdit * edit, long q, int c)
  251. {
  252. edit_cursor_move (edit, q - edit->curs1);
  253. edit_delete (edit, 1);
  254. edit_insert_ahead (edit, c);
  255. }
  256. /* replaces a block of text */
  257. static void
  258. put_paragraph (WEdit * edit, unsigned char *t, long p, int indent, int size)
  259. {
  260. long cursor;
  261. int i, c = 0;
  262. cursor = edit->curs1;
  263. if (indent)
  264. while (strchr ("\t ", edit_get_byte (edit, p)))
  265. p++;
  266. for (i = 0; i < size; i++, p++) {
  267. if (i && indent) {
  268. if (t[i - 1] == '\n' && c == '\n') {
  269. while (strchr ("\t ", edit_get_byte (edit, p)))
  270. p++;
  271. } else if (t[i - 1] == '\n') {
  272. long curs;
  273. edit_cursor_move (edit, p - edit->curs1);
  274. curs = edit->curs1;
  275. edit_insert_indent (edit, indent);
  276. if (cursor >= curs)
  277. cursor += edit->curs1 - p;
  278. p = edit->curs1;
  279. } else if (c == '\n') {
  280. edit_cursor_move (edit, p - edit->curs1);
  281. while (strchr ("\t ", edit_get_byte (edit, p))) {
  282. edit_delete (edit, 1);
  283. if (cursor > edit->curs1)
  284. cursor--;
  285. }
  286. p = edit->curs1;
  287. }
  288. }
  289. c = edit_get_byte (edit, p);
  290. if (c != t[i])
  291. replace_at (edit, p, t[i]);
  292. }
  293. edit_cursor_move (edit, cursor - edit->curs1); /* restore cursor position */
  294. }
  295. static int test_indent (WEdit * edit, long p, long q)
  296. {
  297. int indent;
  298. indent = edit_indent_width (edit, p++);
  299. if (!indent)
  300. return 0;
  301. for (; p < q; p++)
  302. if (edit_get_byte (edit, p - 1) == '\n')
  303. if (indent != edit_indent_width (edit, p))
  304. return 0;
  305. return indent;
  306. }
  307. void
  308. format_paragraph (WEdit *edit, int force)
  309. {
  310. long p, q;
  311. int size;
  312. unsigned char *t;
  313. int indent = 0;
  314. if (option_word_wrap_line_length < 2)
  315. return;
  316. if (line_is_blank (edit, edit->curs_line))
  317. return;
  318. p = begin_paragraph (edit, force);
  319. q = end_paragraph (edit, force);
  320. indent = test_indent (edit, p, q);
  321. t = get_paragraph (edit, p, q, indent, &size);
  322. if (!t)
  323. return;
  324. if (!force) {
  325. int i;
  326. if (strchr (NO_FORMAT_CHARS_START, *t)) {
  327. g_free (t);
  328. return;
  329. }
  330. for (i = 0; i < size - 1; i++) {
  331. if (t[i] == '\n') {
  332. if (strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1])) {
  333. g_free (t);
  334. return;
  335. }
  336. }
  337. }
  338. }
  339. format_this (t, q - p, indent);
  340. put_paragraph (edit, t, p, indent, size);
  341. g_free (t);
  342. /* Scroll left as much as possible to show the formatted paragraph */
  343. edit_scroll_left (edit, -edit->start_col);
  344. }