move_horizontally.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "sh.h"
  2. /*
  3. ** Move horizontally between words
  4. ** When position on the a space, move
  5. ** Move to the left and right sides till it encounter the first space
  6. */
  7. void words_move(int side, t_e *e)
  8. {
  9. if (side == -1)
  10. {
  11. if (e->cmd[e->pos] == ' ')
  12. {
  13. e->pos--;
  14. term_action("le");
  15. }
  16. while (e->pos != 0 && e->cmd[e->pos] != ' ')
  17. move_left(e);
  18. }
  19. else if (side == 1)
  20. {
  21. if (e->cmd[e->pos] == ' ')
  22. {
  23. e->pos++;
  24. term_action("nd");
  25. }
  26. while (e->pos != e->len && e->cmd[e->pos] != ' ')
  27. move_right(e);
  28. }
  29. }
  30. /*
  31. ** Move left
  32. ** block moving at the beginning of the cmd str
  33. ** move left with 'le' char
  34. ** decrement position
  35. ** calcul vertical position
  36. ** if must move vertical position and with GNOME Terminal:
  37. ** move up and full right
  38. */
  39. void move_left(t_e *e)
  40. {
  41. int line;
  42. int i;
  43. if (e->pos <= 0)
  44. return ;
  45. term_action("le");
  46. --e->pos;
  47. line = e->linep;
  48. e->linep = (e->plen + e->pos) / e->col_nbr;
  49. if (ft_strcmp(get_tenv(e, "TERM_PROGRAM"), "iTerm.app") != 0
  50. && line > e->linep)
  51. {
  52. i = 0;
  53. term_action("up");
  54. while (i++ < e->col_nbr)
  55. term_action("nd");
  56. }
  57. }
  58. /*
  59. ** Move right
  60. ** block moving till the end of the cmd str
  61. ** increment position
  62. ** calcul vertical position
  63. ** if must move vertical position: move down
  64. ** if GNOME Terminal move full left
  65. ** else: move right with 'nd' char
  66. */
  67. void move_right(t_e *e)
  68. {
  69. int line;
  70. int i;
  71. if (e->pos >= e->len)
  72. return ;
  73. ++e->pos;
  74. line = e->linep;
  75. e->linep = (e->plen + e->pos) / e->col_nbr;
  76. if (line < e->linep)
  77. {
  78. term_action("do");
  79. if (ft_strcmp(get_tenv(e, "TERM_PROGRAM"), "iTerm.app") != 0)
  80. {
  81. i = e->col_nbr;
  82. while (i-- > 0)
  83. term_action("le");
  84. }
  85. }
  86. else
  87. term_action("nd");
  88. }
  89. /*
  90. ** Go at the end of the cmd
  91. ** Go down lines if line exist
  92. ** As not expected, 'do' brings to the first position
  93. ** Recalculate pos till first position of bottom line
  94. ** Then, go right till the end of the cmd
  95. */
  96. void end(t_e *e)
  97. {
  98. while (e->linep < e->maxline)
  99. {
  100. term_action("do");
  101. e->pos = (1 + e->linep) * e->col_nbr - e->plen;
  102. e->linep++;
  103. }
  104. while (e->pos < e->len)
  105. {
  106. term_action("nd");
  107. e->pos++;
  108. }
  109. }
  110. /*
  111. ** Go at the begin of the cmd
  112. ** Go upper line till the first one
  113. ** Go left till the begin of the cmd
  114. */
  115. void begin(t_e *e)
  116. {
  117. while (e->linep > 0)
  118. {
  119. term_action("up");
  120. e->pos -= e->col_nbr;
  121. e->linep--;
  122. }
  123. while (0 < e->pos)
  124. {
  125. term_action("le");
  126. e->pos--;
  127. }
  128. }