1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
/*
* File: cmd-misc.c
* Purpose: Functions that implement commands.
* Author: Lars Wirzenius
* Version: "@(#)SeX:$Id: cmd-misc.c,v 1.19 2000/09/09 09:22:51 liw Exp $"
*/
#include <assert.h>
#include <ctype.h>
#include <publib.h>
#include "anchor.h"
#include "config.h"
#include "cmd.h"
#include "win.h"
#include "error.h"
#include "x.h"
#include "tab.h"
/*
* Local function prototypes.
*/
static long next_nonspace(Sbuf *, long, long);
static long prev_nonspace(Sbuf *, long);
static long current_indent(Sbuf *, long, long);
static long wanted_indent(Sbuf *, long, long);
static int remove_indent(Sbuf *, Sbufmark *, long, long);
static int insert_indent(Sbufmark *, long, long);
static int add_indent(struct win *, long);
/*
* Function: cmd_insert
* Purpose: Replace current selection with string, and select empty string
* after last inserted character.
* Note: This is function has a different prototype from the rest of
* the cmd_* functions.
*/
int cmd_insert(struct win *win, const char *str, size_t len) {
Sbufmark *sel;
anchor_up(win);
cmd_prev_was_cut = 0;
sel = win_selection(win);
if (sbuf_mark_is_columnar(sel)) {
if (sbuf_strchange(sel, "", 0) == -1) {
error(win, "error changing buffer (out of memory?)");
return -1;
}
sbuf_mark_set_columnar(sel, 0);
}
if (sbuf_strchange(sel, str, len) == -1) {
error(win, "error changing buffer (out of memory?)");
return -1;
}
sbuf_remark(sel, sbuf_mark_end(sel), 0);
win_show(win, sbuf_mark_begin(sel));
return 0;
}
/*
* Function: cmd_indent_selection_space
* Purpose: Add one space to beginning of all lines that have selection.
*/
int cmd_indent_selection_space(struct win *win) {
return add_indent(win, 1);
}
/*
* Function: cmd_undent_selection_space
* Purpose: Remove one space's worth of tabs and spaces from the beginning
* of all lines that have selection.
*/
int cmd_undent_selection_space(struct win *win) {
return add_indent(win, -1);
}
/*
* Function: cmd_indent_selection
* Purpose: Add one tab to beginning of all lines that have selection.
*/
int cmd_indent_selection(struct win *win) {
return add_indent(win, config_get_long(CONFIG_INDENT_WIDTH));
}
/*
* Function: cmd_undent_selection
* Purpose: Remove one tabs worth of tabs and spaces from the beginning
* of all lines that have selection.
*/
int cmd_undent_selection(struct win *win) {
return add_indent(win, -config_get_long(CONFIG_INDENT_WIDTH));
}
/*
* Function: cmd_insert_indent
* Purpose: If the selection is empty, insert tabs and spaces to
* move it to the next indent level. If it is non-empty,
* do nothing.
*/
int cmd_insert_indent(struct win *win) {
Sbuf *buf;
Sbufmark *sel;
long col, wanted_col, indent;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = win_buf(win);
sel = win_selection(win);
if (sbuf_mark_length(sel) > 0)
return 0;
indent = config_get_long(CONFIG_INDENT_WIDTH);
col = sbuf_colno(buf, sbuf_mark_begin(sel), tab_width());
wanted_col = col + indent - (col % indent);
while (tab_next(col) <= wanted_col) {
if (sbuf_strchange(sel, "\t", 1) == -1) {
error(win, "error changing buffer (out of memory?)");
return -1;
}
sbuf_remark(sel, sbuf_mark_end(sel), 0);
col = tab_next(col);
}
while (col < wanted_col) {
if (sbuf_strchange(sel, " ", 1) == -1) {
error(win, "error changing buffer (out of memory?)");
return -1;
}
sbuf_remark(sel, sbuf_mark_end(sel), 0);
++col;
}
win_show(win, sbuf_mark_begin(sel));
return 0;
}
/*
* Function: cmd_indent_line
* Purpose: Fix indentation for current line (possibly non-empty).
* Description: First remove all indentation on current line. Then
* insert indentation again. Indentation is the same as
* the previous line, or one tab more, if previous line
* ends in left curly brace, parenthesis, or bracket.
* If this line begins with right curly brace, parenthesis
* or bracket, it is indented as much the line that contains
* its pair.
*/
int cmd_indent_line(struct win *win) {
Sbuf *buf;
Sbufmark *sel;
long begin, pos, orig_pos, col;
anchor_up(win);
cmd_prev_was_cut = 0;
buf = win_buf(win);
sel = win_selection(win);
begin = sbuf_mark_begin(sel);
orig_pos = pos = sbuf_boln(buf, begin);
col = wanted_indent(buf, pos, begin);
if (remove_indent(buf, sel, pos, begin) == -1) {
error(win, "error removing old indentation");
return -1;
}
if (insert_indent(sel, pos, col) == -1) {
error(win, "error inserting indentation");
return -1;
}
return 0;
}
/*
* Function: cmd_unindent_right_curly
* Purpose: Remove one level of indentation.
* Description: If selection is empty, and current line contains only
* whitespace and one right brace, then do cmd_indent,
* else do nothing.
*/
int cmd_unindent_right_curly(struct win *win) {
Sbuf *buf;
Sbufmark *sel;
long pos, begin;
int c;
anchor_up(win);
sel = win_selection(win);
if (sbuf_mark_length(sel) > 0)
return 0;
buf = win_buf(win);
begin = sbuf_mark_begin(sel);
pos = sbuf_boln(buf, begin);
pos = next_nonspace(buf, pos, begin);
c = sbuf_charat(buf, pos);
if (c == '}' || c == ']' || c == ')') {
c = sbuf_charat(buf, next_nonspace(buf, pos+1, begin));
if (c == '\n' || c == EOF) {
if (cmd_indent_line(win) == -1)
return -1;
return cmd_goto_eoln(win);
}
}
return 0;
}
/*
* Function: cmd_about
* Purpose: Display About text.
*/
int cmd_about(struct win *win) {
cmd_prev_was_cut = 0;
win_set_msg(win, "Unreleased version of SeX, by Lars Wirzenius.");
return 0;
}
/*
* Function: cmd_help
* Purpose: Display help text.
*/
int cmd_help(struct win *win) {
cmd_prev_was_cut = 0;
win_set_msg(win, "Help is in file " HELP_FILE ".");
return 0;
}
/*
* Function: cmd_help_getting
* Purpose: Display "Getting SeX" text.
*/
int cmd_help_getting(struct win *win) {
cmd_prev_was_cut = 0;
win_set_msg(win, "You can't get SeX yet.");
return 0;
}
/*
* Function: cmd_help_bugs
* Purpose: Display bug reporting text.
*/
int cmd_help_bugs(struct win *win) {
cmd_prev_was_cut = 0;
win_set_msg(win, "SeX has no bugs.");
return 0;
}
/*
* Function: cmd_help_list
* Purpose: Display mailing list text.
*/
int cmd_help_list(struct win *win) {
cmd_prev_was_cut = 0;
win_set_msg(win, "There is no mailing list, but see news:alt.sex.");
return 0;
}
/*
* Function: cmd_quit
* Purpose: Terminate the editor; ask if any files should be saved.
*/
int cmd_quit(struct win *win) {
cmd_prev_was_cut = 0;
if (win_destroy_all(win) == -1)
return -1;
exit(0);
}
/*
* Function: cmd_quit_and_save_all
* Purpose: Terminate the editor; save all unsaved files.
*/
int cmd_quit_and_save_all(struct win *win) {
cmd_prev_was_cut = 0;
if (cmd_save_all(win) == -1)
return -1;
exit(0);
}
/*
* Function: cmd_quit_without_saving
* Purpose: Terminate the editor without saving any files.
*/
int cmd_quit_without_saving(struct win *win) {
cmd_prev_was_cut = 0;
exit(0);
}
/**********************************************************************
* Local functions follow.
*/
/*
* Function: current_indent
* Purpose: Compute the indent this line has now.
* Arguments: buf the buffer
* pos beginning of current line
* begin limit for indentation computation
*/
static long current_indent(Sbuf *buf, long pos, long begin) {
return sbuf_colno(buf, next_nonspace(buf, pos, begin), tab_width());
}
/*
* Function: wanted_indent
* Purpose: Compute the indent this line _should_ have.
* Arguments: buf the buffer
* pos beginning of current line
* begin limit for indentation computation
*/
static long wanted_indent(Sbuf *buf, long pos, long begin) {
long i, j, col, prev, tab, indent;
int c;
if (pos == 0)
return sbuf_colno(buf, next_nonspace(buf, pos, begin),
tab_width());
prev = sbuf_boln(buf, pos - 1);
i = next_nonspace(buf, prev, begin);
col = sbuf_colno(buf, i, tab_width());
tab = tab_width();
indent = config_get_long(CONFIG_INDENT_WIDTH);
j = next_nonspace(buf, pos, begin);
c = sbuf_charat(buf, j);
if (c == '}' || c == ']' || c == ')') {
j = sbuf_find_pair(buf, j);
if (j != -1) {
j = next_nonspace(buf, sbuf_boln(buf, j), begin);
col = sbuf_colno(buf, j, tab);
}
} else {
c = sbuf_charat(buf, prev_nonspace(buf, pos-1));
if (c == '{' || c == '(' || c == '[')
col += indent - (col % indent);
}
return col;
}
/*
* Function: remove_indent
* Purpose: Remove indentation from this line.
*/
static int remove_indent(Sbuf *buf, Sbufmark *sel, long pos, long begin) {
long i;
int c;
for (i = pos; (c = sbuf_charat(buf, i)) != '\n' && isspace(c); ++i)
if (i == begin)
break;
sbuf_remark(sel, pos, i-pos);
sbuf_mark_set_columnar(sel, 0);
return sbuf_strchange(sel, "", 0);
}
/*
* Function: insert_indent
* Purpose: Insert indentation to this line.
*/
static int insert_indent(Sbufmark *sel, long pos, long col) {
long tab;
tab = tab_width();
while (col >= tab) {
if (sbuf_strchange(sel, "\t", 1) == -1)
return -1;
sbuf_remark(sel, sbuf_mark_end(sel), 0);
col -= tab;
}
while (col-- > 0) {
if (sbuf_strchange(sel, " ", 1) == -1)
return -1;
sbuf_remark(sel, sbuf_mark_end(sel), 0);
}
sbuf_mark_set_columnar(sel, 0);
return 0;
}
/*
* Function: next_nonspace
* Purpose: Find the next non-space character on this line.
* Arguments: buf the buffer
* pos the starting position
* begin maximum position to search
* Return: The position before the character.
* Note: Can't fail.
*/
static long next_nonspace(Sbuf *buf, long pos, long begin) {
int c;
while (pos<begin && (c = sbuf_charat(buf, pos)) != '\n' && isspace(c))
++pos;
return pos;
}
/*
* Function: prev_nonspace
* Purpose: Find the previous non-space character on this line.
* Arguments: buf the buffer
* pos the starting position
* Return: The position before the character.
* Note: Can't fail.
*/
static long prev_nonspace(Sbuf *buf, long pos) {
while (pos > 0 && sbuf_charat(buf, pos-1) != '\n' &&
isspace(sbuf_charat(buf, pos)))
--pos;
return pos;
}
/*
* Function: add_indent
* Purpose: Add delta space's worth of indentation.
*/
static int add_indent(struct win *win, long delta) {
Sbuf *buf;
Sbufmark *aux, *sel;
long n, pos, begin, end;
buf = win_buf(win);
aux = sbuf_find_mark_by_code(buf, AUX_MARK);
if (aux == NULL) {
error(win, "Couldn't find auxliary mark (internal error?)");
return -1;
}
sel = win_selection(win);
begin = sbuf_mark_begin(sel);
end = sbuf_mark_end(sel);
pos = sbuf_boln(buf, end);
if (pos > begin && pos == end)
pos = sbuf_boln(buf, pos-1);
for (;;) {
end = sbuf_length(buf);
n = current_indent(buf, pos, end);
n += delta;
if (n < 0)
n = 0;
if (remove_indent(buf, aux, pos, end) == -1 ||
insert_indent(aux, pos, n) == -1) {
error(win, "Couldn't modify text (out of memory?)");
return -1;
}
if (pos <= begin)
break;
assert(pos > 0);
pos = sbuf_boln(buf, pos-1);
}
return 0;
}
|