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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
/* $Id: kbd.c,v 1.8 2000/11/04 13:44:58 amura Exp $ */
/*
* Terminal independent keyboard handling.
*/
/*
* $Log: kbd.c,v $
* Revision 1.8 2000/11/04 13:44:58 amura
* undo memory exception is more safety
*
* Revision 1.7 2000/10/02 16:13:06 amura
* ignore mouse event in minibuffer editing
*
* Revision 1.6 2000/09/21 17:28:30 amura
* replace macro _WIN32 to WIN32 for Cygwin
*
* Revision 1.5 2000/09/13 21:03:53 amura
* can compile option UNDO without KANJI
*
* Revision 1.4 2000/07/20 12:45:17 amura
* support undo with auto-fill mode
*
* Revision 1.3 2000/07/16 15:44:41 amura
* undo bug on autofill fixed
*
* Revision 1.2 2000/06/27 01:49:43 amura
* import to CVS
*
* Revision 1.1 2000/06/01 05:29:03 amura
* Initial revision
*
*/
/* 90.01.29 Modified for Ng 1.0 by S.Yoshida */
#include "config.h" /* 90.12.20 by S.Yoshida */
#include "def.h"
#include "kbd.h"
#ifdef UNDO
#include "undo.h"
#endif
#define EXTERN
#include "key.h"
#ifndef NO_MACRO
#include "macro.h"
#endif
#ifdef DO_METAKEY
#ifndef METABIT
#ifdef KANJI /* 90.01.29 by S.Yoshida */
#define METABIT 0x100
#else /* NOT KANJI */
#define METABIT 0x80
#endif /* KANJI */
#endif /* METABIT */
#ifdef CANNA
#include <canna/jrkanji.h>
extern jrKanjiStatus ks;
int henkan( );
#endif
int use_metakey = TRUE;
#ifdef MOUSE
int allow_mouse_event = FALSE; /* allow mouse event */
#endif
/*
* Toggle the value of use_metakey
*/
do_meta(f, n)
{
if(f & FFARG) use_metakey = n > 0;
else use_metakey = !use_metakey;
ewprintf("Meta keys %sabled", use_metakey ? "en" : "dis");
#ifdef PC9801
if (use_metakey)
setezkey();
else
resetezkey();
#endif
return TRUE;
}
#endif
#ifdef BSMAP
static int bs_map = BSMAP;
/*
* Toggle backspace mapping
*/
bsmap(f, n)
{
if(f & FFARG) bs_map = n > 0;
else bs_map = ! bs_map;
ewprintf("Backspace mapping %sabled", bs_map ? "en" : "dis");
return TRUE;
}
#endif
#ifndef NO_DPROMPT
#define PROMPTL 80
char prompt[PROMPTL], *promptp;
#endif
static int pushed = FALSE;
static int pushedc;
VOID ungetkey(c)
int c;
{
#ifdef DO_METAKEY
if(use_metakey && pushed && c==CCHR('[')) pushedc |= METABIT;
else
#endif
pushedc = c;
#ifdef BSMAP
if(bs_map)
if(pushedc==CCHR('H')) pushedc=CCHR('?');
else if(pushedc==CCHR('?')) pushedc=CCHR('H');
#endif
pushed = TRUE;
}
int getkey(flag)
int flag;
{
int c;
char *keyname();
#ifndef NO_DPROMPT
if(flag && !pushed) {
if(prompt[0]!='\0' && ttwait()) {
ewprintf("%s", prompt); /* avoid problems with % */
update(); /* put the cursor back */
epresf = KPROMPT;
}
if(promptp > prompt) *(promptp-1) = ' ';
}
#endif
if(pushed) {
c = pushedc;
pushed = FALSE;
#ifdef KANJI /* 90.01.29 by S.Yoshida */
} else c = kgetkey();
#else /* NOT KANJI */
} else c = getkbd();
#endif /* KANJI */
#ifdef BSMAP
if(bs_map)
if(c==CCHR('H')) c=CCHR('?');
else if(c==CCHR('?')) c=CCHR('H');
#endif
#ifdef DO_METAKEY
if(use_metakey && (c&METABIT)) {
pushedc = c & ~METABIT;
pushed = TRUE;
c = CCHR('[');
}
#endif
#ifndef NO_DPROMPT
if(flag && promptp < &prompt[PROMPTL - 5]) {
promptp = keyname(promptp, c);
*promptp++ = '-';
*promptp = '\0';
}
#endif
return c;
}
/*
* doscan scans a keymap for a keyboard character and returns a pointer
* to the function associated with that character. Sets ele to the
* keymap element the keyboard was found in as a side effect.
*/
MAP_ELEMENT *ele;
PF doscan(map, c)
register KEYMAP *map;
register int c;
{
register MAP_ELEMENT *elec = &map->map_element[0]; /* local register copy for faster access */
register MAP_ELEMENT *last = &map->map_element[map->map_num];
PF funct;
while(elec < last && c > elec->k_num) elec++;
ele = elec; /* used by prefix and binding code */
if(elec >= last || c < elec->k_base)
funct = map->map_default;
else
funct = elec->k_funcp[c - elec->k_base];
return funct;
}
doin()
{
KEYMAP *curmap;
PF funct;
int d;
#ifdef UNDO
int s;
BUFFER *bp;
#endif
#ifndef NO_DPROMPT
*(promptp = prompt) = '\0';
#endif
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_on();
#endif
#ifdef MOUSE
allow_mouse_event = TRUE;
#endif
curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
key.k_count = 0;
d=getkey(TRUE);
#ifdef MOUSE
allow_mouse_event = FALSE;
#endif
#ifdef CANNA
if( (curbp->b_flag & BFCANNA) &&
(ks.length != 0 || !(d==' '||ISCTRL(d)||ISKANJI(d))) )
return henkan(d);
else
#endif
while((funct=doscan(curmap,(key.k_chars[key.k_count++]=d)))
== prefix) {
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
curmap = ele->k_prefmap;
d = getkey(TRUE);
}
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO)
macro[macrocount++].m_funct = funct;
#endif
if (epresf == TRUE)
eerase();
#ifdef UNDO
bp = curbp;
ublock_open(bp);
s = (*funct)(0, 1);
ublock_close(bp);
return s;
#else
return (*funct)(0, 1);
#endif
}
rescan(f, n)
int f, n;
{
int c;
register KEYMAP *curmap;
int i;
PF fp = (PF)NULL;
int mode = curbp->b_nmodes;
for(;;) {
if(ISUPPER(key.k_chars[key.k_count-1])) {
c = TOLOWER(key.k_chars[key.k_count-1]);
curmap = curbp->b_modes[mode]->p_map;
for(i=0; i < key.k_count-1; i++) {
if((fp=doscan(curmap,(key.k_chars[i]))) != prefix) break;
curmap = ele->k_prefmap;
}
if(fp==prefix) {
if((fp = doscan(curmap, c)) == prefix) {
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
while((fp=doscan(curmap,key.k_chars[key.k_count++] =
getkey(TRUE))) == prefix)
curmap = ele->k_prefmap;
}
if(fp!=rescan) {
#ifndef NO_MACRO
if(macrodef && macrocount <= MAXMACRO)
macro[macrocount-1].m_funct = fp;
#endif
return (*fp)(f, n);
}
}
}
/* try previous mode */
if(--mode < 0) return ABORT;
curmap = curbp->b_modes[mode]->p_map;
for(i=0; i < key.k_count; i++) {
if((fp=doscan(curmap,(key.k_chars[i]))) != prefix) break;
curmap = ele->k_prefmap;
}
if(fp==prefix) {
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
while((fp=doscan(curmap,key.k_chars[i++]=getkey(TRUE)))
== prefix)
curmap = ele->k_prefmap;
key.k_count = i;
}
if(fp!=rescan && i>=key.k_count-1) {
#ifndef NO_MACRO
if(macrodef && macrocount <= MAXMACRO)
macro[macrocount-1].m_funct = fp;
#endif
return (*fp)(f, n);
}
}
}
int universal_argument pro((int, int));
universal_argument(f, n)
int f, n;
{
int c, nn=4;
KEYMAP *curmap;
PF funct;
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
if(f&FFUNIV) nn *= n;
for(;;) {
key.k_chars[0] = c = getkey(TRUE);
key.k_count = 1;
if(c == '-') return negative_argument(f, nn);
if(c >= '0' && c <= '9') return digit_argument(f, nn);
curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
while((funct=doscan(curmap,c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
}
if(funct != universal_argument) {
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO-1) {
if(f&FFARG) macrocount--;
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return (*funct)(FFUNIV, nn);
}
nn <<= 2;
}
}
/*ARGSUSED*/
digit_argument(f, n)
int f, n;
{
int nn, c;
KEYMAP *curmap;
PF funct;
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
nn = key.k_chars[key.k_count-1] - '0';
for(;;) {
c = getkey(TRUE);
if(c < '0' || c > '9') break;
nn *= 10;
nn += c - '0';
}
key.k_chars[0] = c;
key.k_count = 1;
curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
while((funct=doscan(curmap,c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
}
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO-1) {
if(f&FFARG) macrocount--;
else macro[macrocount-1].m_funct = universal_argument;
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return (*funct)(FFOTHARG, nn);
}
negative_argument(f, n)
int f, n;
{
int nn = 0, c;
KEYMAP *curmap;
PF funct;
#ifdef FEPCTRL /* 90.11.26 by K.Takano */
fepmode_off();
#endif
for(;;) {
c = getkey(TRUE);
if(c < '0' || c > '9') break;
nn *= 10;
nn += c - '0';
}
if(nn) nn = -nn;
else nn = -n;
key.k_chars[0] = c;
key.k_count = 1;
curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
while((funct=doscan(curmap,c)) == prefix) {
curmap = ele->k_prefmap;
key.k_chars[key.k_count++] = c = getkey(TRUE);
}
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO-1) {
if(f&FFARG) macrocount--;
else macro[macrocount-1].m_funct = universal_argument;
macro[macrocount++].m_count = nn;
macro[macrocount++].m_funct = funct;
}
#endif
return (*funct)(FFNEGARG, nn);
}
/*
* Insert a character. While defining a macro, create a "LINE" containing
* all inserted characters.
*/
#ifdef KANJI /* 90.01.29 by S.Yoshida */
int no_k2nd = FALSE; /* We have no KANJI 2nd byte */
int inkfill = FALSE; /* Now we are in a fillword(). */
#endif /* KANJI */
selfinsert(f, n)
int f, n;
{
register int c;
int count;
VOID lchange();
#ifndef NO_MACRO
LINE *lp;
int insert pro((int, int));
#endif
#ifdef KANJI /* 90.01.29 by S.Yoshida */
int lkanji2nd; /* For over write mode. */
#endif /* KANJI */
if (n < 0) return FALSE;
if (n == 0) return TRUE;
c = key.k_chars[key.k_count-1];
#ifdef READONLY /* 91.01.05 by S.Yoshida */
if (curbp->b_flag & BFRONLY) { /* If this buffer is read-only, */
warnreadonly(); /* do only displaying warning. */
return TRUE;
}
#endif /* READONLY */
#ifdef KANJI /* 90.01.29 by S.Yoshida */
/* In a autofill mode, normally fill trigger is only ' ' char. But */
/* KANJI version require any KANJI char is also fill trigger. */
/* This must be done at keymap.c to add KANJI fill trigger list in */
/* a fillmap. But there are too many KANJI chars, so we use this */
/* easy way. */
if(curbp->b_flag & BFAUTOFILL && /* Autofill mode and */
!inkfill && no_k2nd) { /* KANJI 2nd byte. */
int s;
#ifdef UNDO
if (isundo()) {
if (lastflag & CFINS2) {
if (undostart == undoptr) {
curbp->b_utop--;
if (curbp->b_utop < 0)
curbp->b_utop = UNDOSIZE;
undostart = &curbp->b_ustack[curbp->b_utop];
}
if (undobefore == NULL)
panic("selfinsert: Run insert error");
undoptr = undobefore;
}
}
#endif
inkfill = TRUE;
s = fillword(f, n); /* fill word with KANJI char. */
inkfill = FALSE;
return (s);
} /* End of autofill mode add routine. */
if (no_k2nd) { /* If there is only KANJI 1st byte, */
no_k2nd = FALSE; /* we believe 'c' is KANJI 2nd byte.*/
} else if (ISKANJI(c)) {
if ((n % 2) == 0) { /* This is easy bug fix. */
n |= 0x01;
}
no_k2nd = TRUE; /* When there is no KANJI 2nd */
/* byte, we don't do update(). */
}
#endif /* KANJI */
#ifndef NO_MACRO
if(macrodef && macrocount < MAXMACRO) {
if(f & FFARG) macrocount -= 2;
if(lastflag & CFINS) { /* last command was insert -- tack on end */
macrocount--;
if(maclcur->l_size < maclcur->l_used + n) {
if((lp = lallocx(maclcur->l_used + n)) == NULL)
return FALSE;
lp->l_fp = maclcur->l_fp;
lp->l_bp = maclcur->l_bp;
lp->l_fp->l_bp = lp->l_bp->l_fp = lp;
bcopy(maclcur->l_text, lp->l_text, maclcur->l_used);
for(count = maclcur->l_used; count < lp->l_used; count++)
lp->l_text[count] = c;
free((char *)maclcur);
maclcur = lp;
} else {
maclcur->l_used += n;
for(count = maclcur->l_used-n; count < maclcur->l_used; count++)
maclcur->l_text[count] = c;
}
} else {
macro[macrocount-1].m_funct = insert;
if((lp = lallocx(n)) == NULL) return FALSE;
lp->l_bp = maclcur;
lp->l_fp = maclcur->l_fp;
maclcur->l_fp = lp;
maclcur = lp;
for(count = 0; count < n; count++)
lp->l_text[count] = c;
}
thisflag |= CFINS;
}
#endif
if(c == '\n') {
do {
count = lnewline();
} while (--n && count==TRUE);
return count;
}
#ifdef UNDO
if (isundo()) {
thisflag |= CFINS2;
if(curbp->b_flag & BFOVERWRITE) { /* Overwrite mode */
UNDO_DATA *undo;
if (lastflag & CFINS2) {
#ifdef KANJI
if (!inkfill && undostart==undoptr) {
#else
if (undostart==undoptr) {
#endif
curbp->b_utop--;
if (curbp->b_utop < 0)
curbp->b_utop = UNDOSIZE;
undostart = &curbp->b_ustack[curbp->b_utop];
}
if (undobefore == NULL)
panic("selfinsert: Run insert error");
undoptr = undobefore;
undo = *undoptr;
} else {
undo_setup(undo);
if (!isundo())
goto noundo;
undo->u_dotlno = get_lineno(curbp,curwp->w_dotp);
undo->u_doto = curwp->w_doto;
undo->u_type = UDOVER;
undo->u_used = 0;
undo->u_code[0] = '\0';
}
if (!undo_bgrow(undo, n))
goto noundo;
lchange(WFEDIT);
#ifdef KANJI /* 90.01.29 by S.Yoshida */
if (undo->u_code[0]) {
lputc(curwp->w_dotp, curwp->w_doto, undo->u_code[0]);
lkanji2nd = TRUE;
} else
lkanji2nd = FALSE;
#endif /* KANJI */
count = undo->u_used;
while(curwp->w_doto < llength(curwp->w_dotp) && n--) {
undo->u_buffer[count] =
lgetc(curwp->w_dotp, curwp->w_doto);
#ifdef KANJI /* 90.01.29 by S.Yoshida */
if (lkanji2nd) {
lkanji2nd = FALSE;
} else if (ISKANJI(undo->u_buffer[count])){
lkanji2nd = TRUE;
}
#endif /* KANJI */
lputc(curwp->w_dotp, curwp->w_doto++, c);
count++;
}
undo->u_used = count;
#ifdef KANJI /* 90.01.29 by S.Yoshida */
if (lkanji2nd && curwp->w_doto < llength(curwp->w_dotp)) {
undo->u_code[0] = lgetc(curwp->w_dotp, curwp->w_doto);
lputc(curwp->w_dotp, curwp->w_doto, ' ');
} else
undo->u_code[0] = '\0';
#endif /* KANJI */
undo_finish(&(undo->u_next));
if (!(lastflag & CFINS2)) {
if (undoptr!=NULL && *undoptr!=NULL)
(*undoptr)->u_type = UDNONE;
}
if (n<=0) return TRUE;
} else if (lastflag & CFINS2) {/* not Overwrite mode */
#ifdef KANJI
if (!inkfill && undostart==undoptr) {
#else
if (undostart==undoptr) {
#endif
curbp->b_utop--;
if (curbp->b_utop < 0)
curbp->b_utop = UNDOSIZE;
undostart = &curbp->b_ustack[curbp->b_utop];
}
if (undobefore == NULL)
panic("selfinsert: Run insert error");
undoptr = undobefore;
}
/* if this is NOT, somecase linsert() panic */
else if (*undoptr != NULL)
(*undoptr)->u_type = UDNONE;
} else
noundo:
#endif /* UNDO */
if(curbp->b_flag & BFOVERWRITE) { /* Overwrite mode */
lchange(WFEDIT);
#ifdef KANJI /* 90.01.29 by S.Yoshida */
lkanji2nd = FALSE;
#endif /* KANJI */
while(curwp->w_doto < llength(curwp->w_dotp) && n--) {
#ifdef KANJI /* 90.01.29 by S.Yoshida */
if (lkanji2nd) {
lkanji2nd = FALSE;
} else if (ISKANJI(lgetc(curwp->w_dotp, curwp->w_doto))) {
lkanji2nd = TRUE;
}
#endif /* KANJI */
lputc(curwp->w_dotp, curwp->w_doto++, c);
}
#ifdef KANJI /* 90.01.29 by S.Yoshida */
if (lkanji2nd && curwp->w_doto < llength(curwp->w_dotp)) {
lputc(curwp->w_dotp, curwp->w_doto, ' ');
}
#endif /* KANJI */
if(n<=0) return TRUE;
}
return linsert(n, c);
}
/*
* this could be implemented as a keymap with everthing defined
* as self-insert.
*/
quote(f, n)
{
register int c;
key.k_count = 1;
if((key.k_chars[0] = getkey(TRUE)) >= '0' && key.k_chars[0] <= '7') {
key.k_chars[0] -= '0';
if((c = getkey(TRUE)) >= '0' && c <= '7') {
key.k_chars[0] <<= 3;
key.k_chars[0] += c - '0';
if((c = getkey(TRUE)) >= '0' && c <= '7') {
key.k_chars[0] <<= 3;
key.k_chars[0] += c - '0';
} else ungetkey(c);
} else ungetkey(c);
}
return selfinsert(f, n);
}
|