editbox.cc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  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. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #include <config.h>
  17. #include <algorithm>
  18. #include "editbox.h"
  19. #include "scrollbar.h"
  20. #include "transtbl.h"
  21. #include "univalues.h"
  22. #include "themes.h"
  23. #include "dbg.h"
  24. TranslationTable EditBox::transtbl;
  25. TranslationTable EditBox::reprtbl;
  26. TranslationTable EditBox::altkbdtbl;
  27. // DOS's EOP is actually two characters: CR + LF, but we represent it
  28. // internally as one character to make processing much simpler. The value we
  29. // choose for it is in Unicode's Private Area block.
  30. #define DOS_PS 0xF880
  31. #define MAC_PS 0x0D
  32. #define UNIX_PS 0x0A
  33. // Initialization {{{
  34. EditBox::EditBox()
  35. {
  36. create_window();
  37. status_listener = NULL;
  38. error_listener = NULL;
  39. margin_before = 0;
  40. margin_after = 1;
  41. primary_mark.para = -1;
  42. optimal_vis_column = -1;
  43. bidi_enabled = true;
  44. visual_cursor_movement = false;
  45. modified = false;
  46. scroll_step = 4;
  47. update_region = rgnAll;
  48. alt_kbd = false;
  49. auto_justify = false;
  50. justification_column = 72;
  51. auto_indent = false;
  52. translation_mode = false;
  53. read_only = false;
  54. smart_typing = false;
  55. wrap_type = wrpAtWhiteSpace;
  56. dir_algo = algoContextRTL;
  57. tab_width = 8;
  58. show_tabs = true;
  59. show_explicits = true;
  60. show_paragraph_endings = true;
  61. rtl_nsm_display = rtlnsmTransliterated;
  62. maqaf_display = mqfAsis;
  63. syn_hlt = synhltOff;
  64. underline_hlt = false;
  65. rfc2646_trailing_space = true;
  66. data_transfer.in_transfer = false;
  67. old_width = -1;
  68. prev_command_type = current_command_type = cmdtpUnknown;
  69. paragraphs.push_back(new Paragraph());
  70. }
  71. EditBox::~EditBox()
  72. {
  73. for (int i = 0; i < parags_count(); i++)
  74. delete paragraphs[i];
  75. }
  76. void EditBox::new_document()
  77. {
  78. for (int i = 0; i < parags_count(); i++)
  79. delete paragraphs[i];
  80. paragraphs.clear();
  81. paragraphs.push_back(new Paragraph());
  82. undo_stack.clear();
  83. unset_primary_mark();
  84. set_modified(false);
  85. cursor.zero();
  86. scroll_to_cursor_line();
  87. request_update(rgnAll);
  88. }
  89. void EditBox::sync_scrollbar(Scrollbar *scrollbar)
  90. {
  91. scrollbar->set_total_size(parags_count());
  92. scrollbar->set_page_size(window_height());
  93. scrollbar->set_page_pos(top_line.para);
  94. }
  95. // }}}
  96. // Utility methods dealing with End-Of-Paragraphs {{{
  97. static inline bool is_eop(unichar ch) {
  98. return (ch == DOS_PS || ch == MAC_PS || ch == UNIX_PS || ch == UNICODE_PS);
  99. }
  100. static inline eop_t get_eop_type(unichar ch)
  101. {
  102. switch (ch) {
  103. case DOS_PS: return eopDOS;
  104. case MAC_PS: return eopMac;
  105. case UNIX_PS: return eopUnix;
  106. case UNICODE_PS: return eopUnicode;
  107. default: return eopNone;
  108. }
  109. }
  110. unichar EditBox::get_eop_char(eop_t eop)
  111. {
  112. switch (eop) {
  113. case eopDOS: return DOS_PS;
  114. case eopMac: return MAC_PS;
  115. case eopUnix: return UNIX_PS;
  116. case eopUnicode: return UNICODE_PS;
  117. default: return UNIX_PS; // we shouldn't arrive here
  118. }
  119. }
  120. // get_curr_eop_char() returns the character corresponding to the current
  121. // paragraph's EOP.
  122. //
  123. // When creating new paragraphs, e.g. as a reslt of pressing ENTER or of
  124. // justifying lines, we copy the EOP of the current paragraph to the new
  125. // paragraphs. Since our document may contain differenet EOPs (for different
  126. // paragraphs), this seems to be the most sensible solution.
  127. //
  128. // When the current paragraph has no EOP (when it's the last in the buffer),
  129. // we look at the previous one. As a last resort we use Unix's EOP.
  130. unichar EditBox::get_curr_eop_char()
  131. {
  132. if (curr_para()->eop == eopNone) {
  133. if (cursor.para > 0)
  134. return get_eop_char(paragraphs[cursor.para - 1]->eop);
  135. else
  136. return UNIX_PS; // default
  137. } else {
  138. return get_eop_char(curr_para()->eop);
  139. }
  140. }
  141. // toggle_eops() is an interactive command to change all EOPs in the buffer.
  142. //
  143. // It'd be especially appreciated by Unix users who would like to convert
  144. // DOS line ends to Unix line ends, but it was written to toggle among all
  145. // possible EOPs.
  146. void EditBox::set_eops(eop_t new_eop)
  147. {
  148. if (read_only) {
  149. NOTIFY_ERROR(read_only);
  150. return;
  151. }
  152. for (int i = 0; i < parags_count() - 1; i++)
  153. paragraphs[i]->eop = new_eop;
  154. set_modified(true);
  155. request_update(rgnAll);
  156. }
  157. INTERACTIVE void EditBox::toggle_eops()
  158. {
  159. eop_t new_eop = eopNone; // silence the compiler
  160. switch (paragraphs[0]->eop) {
  161. case eopUnix: new_eop = eopDOS; break;
  162. case eopDOS: new_eop = eopMac; break;
  163. case eopMac: new_eop = eopUnicode; break;
  164. case eopUnicode: new_eop = eopUnix; break;
  165. case eopNone: return; // no eops in document
  166. }
  167. set_eops(new_eop);
  168. }
  169. // }}}
  170. // Setters / Getters / Togglers {{{
  171. // The following are trivial set_xxx methods.
  172. void EditBox::set_read_only(bool value) {
  173. read_only = value;
  174. NOTIFY_CHANGE(read_only);
  175. }
  176. void EditBox::set_smart_typing(bool val) {
  177. smart_typing = val;
  178. NOTIFY_CHANGE(smart_typing);
  179. }
  180. void EditBox::set_modified(bool value) {
  181. modified = value;
  182. NOTIFY_CHANGE(modification);
  183. }
  184. void EditBox::set_auto_indent(bool value) {
  185. auto_indent = value;
  186. NOTIFY_CHANGE(auto_indent);
  187. }
  188. void EditBox::set_auto_justify(bool value) {
  189. auto_justify = value;
  190. NOTIFY_CHANGE(auto_justify);
  191. }
  192. void EditBox::set_justification_column(int value) {
  193. if (value > 0)
  194. justification_column = value;
  195. }
  196. void EditBox::set_scroll_step(int value) {
  197. if (value > 0)
  198. scroll_step = value;
  199. }
  200. void EditBox::set_primary_mark(const Point &point) {
  201. primary_mark = point;
  202. NOTIFY_CHANGE(selection);
  203. }
  204. void EditBox::unset_primary_mark() {
  205. if (is_primary_mark_set()) {
  206. primary_mark.para = -1;
  207. request_update(rgnAll);
  208. NOTIFY_CHANGE(selection);
  209. }
  210. }
  211. INTERACTIVE void EditBox::toggle_primary_mark() {
  212. if (is_primary_mark_set())
  213. unset_primary_mark();
  214. else
  215. set_primary_mark();
  216. }
  217. // }}}
  218. // Misc {{{
  219. // calc_distance() - count the number of characters, counting EOP as 1,
  220. // between two points.
  221. int EditBox::calc_distance(Point p1, Point p2)
  222. {
  223. int dist;
  224. if (p2 < p1)
  225. p2.swap(p1);
  226. if (p1.para == p2.para) {
  227. dist = p2.pos - p1.pos;
  228. } else {
  229. dist = paragraphs[p1.para]->str.len() - p1.pos + 1;
  230. while (++p1.para < p2.para)
  231. dist += paragraphs[p1.para]->str.len() + 1;
  232. dist += p2.pos;
  233. }
  234. return dist;
  235. }
  236. // calc_inner_line() returns the inner-line on which the cursor stands.
  237. // In our terminology, "inner-line" is a paragraph screen-line, zero-based.
  238. int EditBox::calc_inner_line()
  239. {
  240. for (int line_num = 0; line_num < curr_para()->breaks_count(); line_num++) {
  241. idx_t line_break = curr_para()->line_breaks[line_num];
  242. if (cursor.pos < line_break
  243. || (cursor.pos == line_break
  244. && cursor.pos == curr_para()->str.len()))
  245. return line_num;
  246. }
  247. return 0;
  248. }
  249. // }}}
  250. // Clipboard {{{
  251. INTERACTIVE void EditBox::copy() {
  252. cut_or_copy(true);
  253. }
  254. INTERACTIVE void EditBox::cut() {
  255. cut_or_copy(false);
  256. }
  257. INTERACTIVE void EditBox::paste() {
  258. insert_text(clipboard);
  259. }
  260. // cut_or_copy() -- cut and/or copies text into the clipboard. It does it by
  261. // calling copy_text() and delete_text().
  262. //
  263. // @param just_copy - don't delete the text.
  264. void EditBox::cut_or_copy(bool just_copy)
  265. {
  266. if (is_primary_mark_set()) {
  267. int len = calc_distance(cursor, primary_mark);
  268. // copy_text() and delete_text() advance the cursor --
  269. // that's why this code is a bit confusing: we restore the
  270. // cursor position after each call.
  271. Point orig_cursor = cursor;
  272. if (cursor > primary_mark)
  273. cursor = primary_mark;
  274. if (just_copy) {
  275. copy_text(len);
  276. cursor = orig_cursor;
  277. } else {
  278. Point tmp = cursor;
  279. copy_text(len);
  280. cursor = tmp;
  281. delete_text(len);
  282. }
  283. unset_primary_mark();
  284. } else {
  285. NOTIFY_ERROR(no_selection);
  286. }
  287. }
  288. // copy_text() -- copies "len" characters to the clipboard. advances the
  289. // cursor.
  290. // if the 'append' parameter is true, the characters don't replace the
  291. // clipboard but are appended to it.
  292. void EditBox::copy_text(int len, bool append)
  293. {
  294. if (!append)
  295. clipboard.clear();
  296. while (len > 0) {
  297. if (cursor.pos == curr_para()->str.len()) {
  298. if (cursor.para < parags_count() - 1) {
  299. clipboard.push_back(get_curr_eop_char());
  300. cursor.para++;
  301. cursor.pos = 0;
  302. }
  303. len--;
  304. } else {
  305. int to_copy = MIN(curr_para()->str.len() - cursor.pos, len);
  306. clipboard.append(curr_para()->str.begin() + cursor.pos, to_copy);
  307. cursor.pos += to_copy;
  308. len -= to_copy;
  309. }
  310. }
  311. }
  312. // }}}
  313. // Undo {{{
  314. INTERACTIVE void EditBox::undo()
  315. {
  316. undo_op(undo_stack.get_prev_op());
  317. // If we undo all the changes to the buffer, then we return it to its
  318. // initial state. clear the "modified" flag.
  319. if (!undo_stack.is_undo_available()
  320. && !undo_stack.disabled() && !undo_stack.was_truncated())
  321. set_modified(false);
  322. }
  323. INTERACTIVE void EditBox::redo()
  324. {
  325. redo_op(undo_stack.get_next_op());
  326. }
  327. // undo_op() -- undoes en operation. For example, if the operation was to
  328. // delete some text then we re-insert the deleted text.
  329. //
  330. // There are three fundamental operations: inserting text, deleting text and
  331. // replacing text.
  332. void EditBox::undo_op(UndoOp *op)
  333. {
  334. if (!op)
  335. return;
  336. switch (op->type) {
  337. case opDelete:
  338. cursor = op->point;
  339. insert_text(op->deleted_text, true);
  340. break;
  341. case opInsert:
  342. cursor = op->point;
  343. delete_text(op->inserted_text.len(), true);
  344. break;
  345. case opReplace:
  346. cursor = op->point;
  347. replace_text(op->deleted_text, op->inserted_text.len(), true);
  348. break;
  349. }
  350. }
  351. void EditBox::redo_op(UndoOp *op)
  352. {
  353. if (!op)
  354. return;
  355. switch (op->type) {
  356. case opDelete:
  357. cursor = op->point;
  358. delete_text(op->deleted_text.len(), true);
  359. break;
  360. case opInsert:
  361. cursor = op->point;
  362. insert_text(op->inserted_text, true);
  363. break;
  364. case opReplace:
  365. cursor = op->point;
  366. replace_text(op->inserted_text, op->deleted_text.len(), true);
  367. break;
  368. }
  369. }
  370. INTERACTIVE void EditBox::toggle_key_for_key_undo()
  371. {
  372. set_key_for_key_undo(!is_key_for_key_undo());
  373. }
  374. // }}}
  375. // Data Transfer {{{
  376. // start_data_transfer() - must be called to start any data transfer; it
  377. // initializes the data tranfser state (the "data_transfer" struct).
  378. //
  379. // @param dir - the direction in which the data is transferred. Either into
  380. // EditBox (dataTransferIn) or out of EditBox (dataTransferOut).
  381. //
  382. // @param new_doc - clear the buffer before transferring data into EditBox.
  383. //
  384. // @param selection_only - when transferring data out, transfer only the
  385. // selection, not the whole buffer.
  386. void EditBox::start_data_transfer(data_transfer_direction dir,
  387. bool new_doc,
  388. bool selection_only)
  389. {
  390. data_transfer.dir = dir;
  391. data_transfer.in_transfer = true;
  392. if (dir == dataTransferIn) {
  393. // Loading a file
  394. if (new_doc) {
  395. // "Open new file"
  396. new_document();
  397. data_transfer.skip_undo = true;
  398. data_transfer.clear_modified_flag = true;
  399. } else {
  400. // "Insert file"
  401. data_transfer.skip_undo = false;
  402. data_transfer.clear_modified_flag = false;
  403. }
  404. data_transfer.prev_is_cr = false;
  405. data_transfer.cursor_origin = cursor;
  406. } else {
  407. // Saving a file
  408. data_transfer.cursor_origin = cursor;
  409. if (selection_only && is_primary_mark_set()) {
  410. data_transfer.ntransferred_out = 0;
  411. data_transfer.ntransferred_out_max = calc_distance(cursor, primary_mark);
  412. if (cursor > primary_mark)
  413. cursor = primary_mark;
  414. } else {
  415. data_transfer.ntransferred_out_max = -1;
  416. cursor.zero();
  417. }
  418. data_transfer.at_eof = false;
  419. // An error may occur during saving, so only a an object that knows
  420. // the IO result may clear the modification flag.
  421. data_transfer.clear_modified_flag = false;
  422. }
  423. }
  424. bool EditBox::is_in_data_transfer() const {
  425. return data_transfer.in_transfer;
  426. }
  427. int EditBox::transfer_data(unichar *buf, int len)
  428. {
  429. if (data_transfer.dir == dataTransferIn)
  430. return transfer_data_in(buf, len);
  431. else
  432. return transfer_data_out(buf, len);
  433. }
  434. // transfer_data_out() - transfers data out of EditBox. returns the number of
  435. // characters placed in "buf". when it returns 0, we know we've reached end
  436. // of buffer.
  437. int EditBox::transfer_data_out(unichar *buf, int len)
  438. {
  439. if (data_transfer.at_eof)
  440. return 0; // end-of-buffer: no more chars to write
  441. int nwritten = 0; // number of chars we've written to buf
  442. while (nwritten < len && !data_transfer.at_eof) {
  443. if (cursor.pos == curr_para()->str.len()) {
  444. // write EOP
  445. if (curr_para()->eop != eopNone) {
  446. if (curr_para()->eop == eopDOS) {
  447. // DOS's EOP is two characters. If there's not
  448. // enough space in buf, we exit and do it in the
  449. // next call.
  450. if (len - nwritten < 2)
  451. break;
  452. *buf++ = '\r';
  453. *buf++ = '\n';
  454. nwritten += 2;
  455. } else {
  456. *buf++ = get_curr_eop_char();
  457. nwritten++;
  458. }
  459. data_transfer.ntransferred_out++;
  460. if (data_transfer.ntransferred_out_max != -1
  461. && data_transfer.ntransferred_out
  462. >= data_transfer.ntransferred_out_max)
  463. data_transfer.at_eof = true;
  464. }
  465. if (cursor.para < parags_count() - 1) {
  466. // advance to next paragraph
  467. cursor.para++;
  468. cursor.pos = 0;
  469. } else {
  470. // we've reached end of buffer
  471. data_transfer.at_eof = true;
  472. }
  473. } else {
  474. // write the [cursor, end-of-paragraph) segment into buf.
  475. int to_copy = MIN(curr_para()->str.len() - cursor.pos, (len - nwritten));
  476. if (data_transfer.ntransferred_out_max != -1
  477. && data_transfer.ntransferred_out + to_copy
  478. > data_transfer.ntransferred_out_max) {
  479. to_copy = MAX(data_transfer.ntransferred_out_max
  480. - data_transfer.ntransferred_out, 0);
  481. }
  482. unichar *src = curr_para()->str.begin() + cursor.pos;
  483. cursor.pos += to_copy;
  484. nwritten += to_copy;
  485. data_transfer.ntransferred_out += to_copy;
  486. while (to_copy--)
  487. *buf++ = *src++;
  488. if (data_transfer.ntransferred_out_max != -1
  489. && data_transfer.ntransferred_out
  490. >= data_transfer.ntransferred_out_max)
  491. data_transfer.at_eof = true;
  492. }
  493. }
  494. return nwritten;
  495. }
  496. #define INSERT_DOS_PS() \
  497. do { \
  498. unichar ch = DOS_PS; \
  499. insert_text(&ch, 1, data_transfer.skip_undo); \
  500. } while (0)
  501. #define INSERT_CR() \
  502. do { \
  503. unichar ch = '\r'; \
  504. insert_text(&ch, 1, data_transfer.skip_undo); \
  505. } while (0)
  506. // transfer_data_in() - transfers data into EditBox.
  507. int EditBox::transfer_data_in(unichar *data, int len)
  508. {
  509. // What makes the implementation complicated is that we need to convert
  510. // DOS's EOP (CR + LF) to one pseudo character. Since these two characters
  511. // may be passed to this method in two separate calls, we have to maintain
  512. // state (prev_is_cr = "last character was CR").
  513. int start = 0;
  514. if (data_transfer.prev_is_cr && len) {
  515. if (data[0] == '\n') {
  516. INSERT_DOS_PS();
  517. start = 1;
  518. } else {
  519. INSERT_CR();
  520. }
  521. data_transfer.prev_is_cr = false;
  522. }
  523. for (int i = start; i < len; i++) {
  524. if (data[i] == '\r') {
  525. insert_text(data + start, i - start, data_transfer.skip_undo);
  526. start = i;
  527. if (i + 1 < len) {
  528. if (data[i + 1] == '\n') {
  529. INSERT_DOS_PS();
  530. start += 2;
  531. i++;
  532. }
  533. } else {
  534. data_transfer.prev_is_cr = true;
  535. start++;
  536. }
  537. }
  538. }
  539. insert_text(data + start, len - start, data_transfer.skip_undo);
  540. return 0; // return value is meaningless
  541. }
  542. // end_data_transfer() -- must be called when transfer is completed.
  543. void EditBox::end_data_transfer()
  544. {
  545. if (data_transfer.dir == dataTransferIn) {
  546. if (data_transfer.prev_is_cr)
  547. INSERT_CR();
  548. }
  549. if (data_transfer.clear_modified_flag)
  550. set_modified(false);
  551. data_transfer.in_transfer = false;
  552. cursor = data_transfer.cursor_origin;
  553. if (data_transfer.dir == dataTransferIn)
  554. last_modification = cursor;
  555. scroll_to_cursor_line();
  556. request_update(rgnAll);
  557. }
  558. #undef INSERT_DOS_PS
  559. #undef INSERT_CR
  560. // }}}
  561. // Scrolling {{{
  562. // add_rows_to_line() - adds screen lines to a CombinedLine. For example, if
  563. // we advance the cursor forward by one screen line, we could find the new
  564. // line using:
  565. //
  566. // CombinedLine line(cursor.para, calc_inner_line());
  567. // add_rows_to_line(line, 1);
  568. //
  569. // @params rows - screen rows to add. can be negative.
  570. void EditBox::add_rows_to_line(CombinedLine &combline, int rows)
  571. {
  572. if (rows > 0) {
  573. int remaining_line_breaks = paragraphs[combline.para]->breaks_count()
  574. - combline.inner_line - 1;
  575. if (rows <= remaining_line_breaks) {
  576. // Our final position is inside the paragraph.
  577. // We zero rows to finish the loop.
  578. combline.inner_line += rows;
  579. rows = 0;
  580. } else {
  581. if (combline.para < parags_count() - 1) {
  582. // We have way to go. Move to the next paragraph,
  583. // decrease rows, and let the loop handle the rest.
  584. combline.para++;
  585. combline.inner_line = 0;
  586. rows -= remaining_line_breaks + 1;
  587. } else {
  588. // We are at the last paragraph. Move to the last line.
  589. combline.inner_line = paragraphs[combline.para]->breaks_count() - 1;
  590. rows = 0;
  591. }
  592. }
  593. if (rows != 0) // way to go?
  594. add_rows_to_line(combline, rows);
  595. }
  596. if (rows < 0) {
  597. if (-rows <= combline.inner_line) {
  598. // Our final position is inside the paragraph.
  599. // We zero rows to finish the loop.
  600. combline.inner_line -= -rows;
  601. rows = 0;
  602. } else {
  603. if (combline.para > 0) {
  604. // We have way to go. Move to the previous paragraph, increase
  605. // rows (towards zero), and let the loop handle the rest.
  606. combline.para--;
  607. rows += combline.inner_line + 1;
  608. combline.inner_line = paragraphs[combline.para]->breaks_count() - 1;
  609. } else {
  610. // We are at the first paragraph. Move to the first line.
  611. combline.inner_line = 0;
  612. rows = 0;
  613. }
  614. }
  615. if (rows != 0) // way to go?
  616. add_rows_to_line(combline, rows);
  617. }
  618. }
  619. // lines_diff() - calculates the difference, in screen lines, between
  620. // two CombinedLines.
  621. int EditBox::lines_diff(CombinedLine L1, CombinedLine L2)
  622. {
  623. if (L2 < L1)
  624. L2.swap(L1);
  625. if (L1.para == L2.para)
  626. return L2.inner_line - L1.inner_line;
  627. int rows = paragraphs[L1.para]->breaks_count() - L1.inner_line;
  628. L1.para++;
  629. while (L1.para < L2.para) {
  630. rows += paragraphs[L1.para]->breaks_count();
  631. L1.para++;
  632. }
  633. rows += L2.inner_line;
  634. return rows;
  635. }
  636. // scroll_to_cursor_line() - makes sure the cursor line (=current line) is
  637. // visible on screen. If not, it scrolls. It is called at the end of various
  638. // movement commands.
  639. void EditBox::scroll_to_cursor_line()
  640. {
  641. CombinedLine curr(cursor.para, calc_inner_line());
  642. if (curr < top_line) {
  643. // if the current line is smaller than top_line, it means it's outside
  644. // the window view and we have to scroll up to make it visible.
  645. // calculate the new top line
  646. CombinedLine old_top_line = top_line;
  647. top_line = curr;
  648. add_rows_to_line(top_line, -(get_effective_scroll_step() - 1));
  649. // Performance: we can either repaint all the window (rgnAll),
  650. // or use curses' wscrl() and paint only the lines that aren't already
  651. // on screen.
  652. if (top_line.is_near(old_top_line)) {
  653. int diff = lines_diff(top_line, old_top_line);
  654. scrollok(wnd, TRUE);
  655. wscrl(wnd, -diff);
  656. scrollok(wnd, FALSE);
  657. request_update(top_line.para, old_top_line.para - 1);
  658. } else {
  659. request_update(rgnAll);
  660. }
  661. return;
  662. }
  663. // We don't need to scroll up. Now we check whether we need to scroll down.
  664. // calculate the bottom line
  665. CombinedLine bottom_line = top_line;
  666. add_rows_to_line(bottom_line, window_height() - 1);
  667. if (curr > bottom_line) {
  668. // if the current line is past bottom_line, we scroll down to make
  669. // it visible.
  670. // calculate the new bottom line
  671. bottom_line = curr;
  672. add_rows_to_line(bottom_line, get_effective_scroll_step() - 1);
  673. // and the new top line
  674. CombinedLine old_top_line = top_line;
  675. top_line = bottom_line;
  676. add_rows_to_line(top_line, -(window_height() - 1));
  677. // Performance: either repaint everything or just the lines that
  678. // aren't already on screen.
  679. if (top_line.is_near(old_top_line)) {
  680. int diff = lines_diff(top_line, old_top_line);
  681. CombinedLine from = bottom_line;
  682. add_rows_to_line(from, -(diff - 1));
  683. scrollok(wnd, TRUE);
  684. wscrl(wnd, diff);
  685. scrollok(wnd, FALSE);
  686. request_update(from.para, bottom_line.para);
  687. } else {
  688. request_update(rgnAll);
  689. }
  690. }
  691. }
  692. // move_forward_page() - interactive command called in response to the
  693. // "Page Down" key. It mimics emacs.
  694. INTERACTIVE void EditBox::move_forward_page()
  695. {
  696. // First, check whether the end of the buffer is already on screen (by
  697. // calculating the bottom line). If so, move to the last line and quit.
  698. CombinedLine bottom_line = top_line;
  699. add_rows_to_line(bottom_line, window_height() - 1);
  700. if (lines_diff(top_line, bottom_line) < window_height() - 1) {
  701. move_relative_line(window_height());
  702. return;
  703. }
  704. // No, the end of the buffer is not on screen. Advance top_line by
  705. // window_height() lines (but keep 2 lines of context).
  706. add_rows_to_line(top_line, window_height() - 2);
  707. // if the cursor is now outside the view ...
  708. if (CombinedLine(cursor.para, calc_inner_line()) < top_line) {
  709. // ... then move it to the top line.
  710. cursor.para = top_line.para;
  711. cursor.pos = (top_line.inner_line > 0)
  712. ? curr_para()->line_breaks[top_line.inner_line - 1]
  713. : 0;
  714. invalidate_optimal_vis_column();
  715. }
  716. post_vertical_movement();
  717. request_update(rgnAll);
  718. }
  719. // move_backward_page() - interactive command called in response to the
  720. // "Page Up" key. It mimics emacs.
  721. INTERACTIVE void EditBox::move_backward_page()
  722. {
  723. // First, check whether the first line of the buffer if already shown. If
  724. // so, move to that first line and quit.
  725. if (top_line.para == 0 && top_line.inner_line == 0) {
  726. move_relative_line(-window_height());
  727. return;
  728. }
  729. // No, we can go back. Substract window_height() lines (minus 2 lines of
  730. // context) from top_line.
  731. add_rows_to_line(top_line, -(window_height() - 2));
  732. // Calculate the new bottom line
  733. CombinedLine bottom_line = top_line;
  734. add_rows_to_line(bottom_line, window_height() - 1);
  735. // if the cursor is now past the bottom line ...
  736. if (CombinedLine(cursor.para, calc_inner_line()) > bottom_line) {
  737. // ... then move it to the bottom line.
  738. cursor.para = bottom_line.para;
  739. cursor.pos = (bottom_line.inner_line > 0)
  740. ? curr_para()->line_breaks[bottom_line.inner_line - 1]
  741. : 0;
  742. invalidate_optimal_vis_column();
  743. }
  744. post_vertical_movement();
  745. request_update(rgnAll);
  746. }
  747. // center_line() - interactive command used to center the current line.
  748. // (like emacs's C-l)
  749. INTERACTIVE void EditBox::center_line()
  750. {
  751. // Calculate new top line
  752. CombinedLine new_top_line(cursor.para, calc_inner_line());
  753. add_rows_to_line(new_top_line, -(window_height() / 2));
  754. top_line = new_top_line;
  755. request_update(rgnAll);
  756. }
  757. // }}}
  758. // Movement commands {{{
  759. // post_horizontal_movement() -- should be called at the end of horizontal
  760. // movement commands. It makes sure the cursor is on screen and notify our
  761. // listeners.
  762. void EditBox::post_horizontal_movement()
  763. {
  764. scroll_to_cursor_line();
  765. request_update(rgnCursor);
  766. invalidate_optimal_vis_column();
  767. NOTIFY_CHANGE(position);
  768. }
  769. // post_vertical_movement() -- should be called at the end of vertical
  770. // movement commands.
  771. void EditBox::post_vertical_movement()
  772. {
  773. scroll_to_cursor_line();
  774. request_update(rgnCursor);
  775. NOTIFY_CHANGE(position);
  776. }
  777. // move_forward_char() - interactive command to move to the next logical
  778. // character.
  779. INTERACTIVE void EditBox::move_forward_char()
  780. {
  781. if (cursor.pos < curr_para()->str.len()) {
  782. cursor.pos++;
  783. } else if (cursor.para < parags_count() - 1) {
  784. cursor.para++;
  785. cursor.pos = 0;
  786. }
  787. post_horizontal_movement();
  788. }
  789. // move_backward_char() - interactive command to move the the previous logical
  790. // character.
  791. INTERACTIVE void EditBox::move_backward_char()
  792. {
  793. if (cursor.pos > 0) {
  794. cursor.pos--;
  795. } else if (cursor.para > 0) {
  796. cursor.para--;
  797. cursor.pos = curr_para()->str.len();
  798. }
  799. post_horizontal_movement();
  800. }
  801. // key_left() - responds to the "left arrow" key.
  802. INTERACTIVE void EditBox::key_left()
  803. {
  804. if (curr_para()->is_rtl()) {
  805. if (visual_cursor_movement)
  806. move_forward_visual_char();
  807. else
  808. move_forward_char();
  809. } else {
  810. if (visual_cursor_movement)
  811. move_backward_visual_char();
  812. else
  813. move_backward_char();
  814. }
  815. }
  816. // key_left() - responds to the "right arrow" key.
  817. INTERACTIVE void EditBox::key_right()
  818. {
  819. if (curr_para()->is_rtl()) {
  820. if (visual_cursor_movement)
  821. move_backward_visual_char();
  822. else
  823. move_backward_char();
  824. } else {
  825. if (visual_cursor_movement)
  826. move_forward_visual_char();
  827. else
  828. move_forward_char();
  829. }
  830. }
  831. bool EditBox::is_at_end_of_buffer() {
  832. return cursor.para == parags_count() - 1
  833. && cursor.pos == curr_para()->str.len();
  834. }
  835. bool EditBox::is_at_beginning_of_buffer() {
  836. return cursor.para == 0 && cursor.pos == 0;
  837. }
  838. unichar EditBox::get_current_char()
  839. {
  840. if (cursor.pos < curr_para()->str.len())
  841. return curr_para()->str[cursor.pos];
  842. else
  843. return get_curr_eop_char();
  844. }
  845. // move_forward_word() -- interactive command to move to the end of the
  846. // current or next word.
  847. INTERACTIVE void EditBox::move_forward_word()
  848. {
  849. while (!BiDi::is_wordch(get_current_char()) && !is_at_end_of_buffer())
  850. move_forward_char();
  851. while (BiDi::is_wordch(get_current_char()) && !is_at_end_of_buffer())
  852. move_forward_char();
  853. }
  854. bool EditBox::is_at_beginning_of_word()
  855. {
  856. if (cursor.pos == curr_para()->str.len())
  857. return false;
  858. if (cursor.pos == 0)
  859. return BiDi::is_wordch(curr_para()->str[0]);
  860. return (BiDi::is_wordch(curr_para()->str[cursor.pos])
  861. && !BiDi::is_wordch(curr_para()->str[cursor.pos-1]));
  862. }
  863. // move_backward_word() -- interactive command to move to the start of the
  864. // current or previous word.
  865. INTERACTIVE void EditBox::move_backward_word()
  866. {
  867. if (is_at_beginning_of_word())
  868. move_backward_char();
  869. while (!is_at_beginning_of_word() && !is_at_beginning_of_buffer())
  870. move_backward_char();
  871. }
  872. // move_beginning_of_buffer() -- interactive command to move to the beginning
  873. // of the buffer
  874. INTERACTIVE void EditBox::move_beginning_of_buffer()
  875. {
  876. cursor.zero();
  877. post_vertical_movement();
  878. }
  879. // move_end_of_buffer() -- interactive command to move to the end of the
  880. // buffer
  881. INTERACTIVE void EditBox::move_end_of_buffer()
  882. {
  883. cursor.para = parags_count() - 1;
  884. cursor.pos = 0;
  885. post_vertical_movement();
  886. }
  887. // move_beginning_of_line() - interactive command to move to the beginning of
  888. // the current screen line.
  889. INTERACTIVE void EditBox::move_beginning_of_line()
  890. {
  891. idx_t prev_line_break = 0;
  892. for (int line_num = 0; line_num < curr_para()->breaks_count(); line_num++) {
  893. idx_t line_break = curr_para()->line_breaks[line_num];
  894. if (cursor.pos < line_break
  895. || (cursor.pos == line_break
  896. && cursor.pos == curr_para()->str.len())) {
  897. cursor.pos = prev_line_break;
  898. break;
  899. }
  900. prev_line_break = line_break;
  901. }
  902. post_horizontal_movement();
  903. }
  904. // move_end_of_line() - interactive command to move to the end of
  905. // the current screen line.
  906. INTERACTIVE void EditBox::move_end_of_line()
  907. {
  908. for (int line_num = 0; line_num < curr_para()->breaks_count(); line_num++) {
  909. idx_t line_break = curr_para()->line_breaks[line_num];
  910. if (cursor.pos < line_break) {
  911. cursor.pos = line_break;
  912. if (line_num != curr_para()->breaks_count() - 1)
  913. cursor.pos--;
  914. break;
  915. }
  916. }
  917. post_horizontal_movement();
  918. }
  919. INTERACTIVE void EditBox::move_last_modification()
  920. {
  921. if (last_modification.para || last_modification.pos)
  922. set_cursor_position(last_modification);
  923. }
  924. // set_cursor_position(Point) - allow other objects to position the cursor
  925. // at a specific paragraph and column.
  926. void EditBox::set_cursor_position(const Point &point)
  927. {
  928. cursor.para = MIN(MAX(point.para, 0), parags_count() - 1);
  929. cursor.pos = MIN(MAX(point.pos, 0), curr_para()->str.len());
  930. post_horizontal_movement();
  931. }
  932. // move_absolute_line() -- jumps to a specific paragraph (used by the user when
  933. // he knows where he wants to jump to, i.e. when reading compiler error messages).
  934. void EditBox::move_absolute_line(int line)
  935. {
  936. set_cursor_position(Point(line, 0));
  937. }
  938. // move_relative_line(diff) - moves the cursor diff lines up or down.
  939. void EditBox::move_relative_line(int diff)
  940. {
  941. if (!valid_optimal_vis_column())
  942. optimal_vis_column = calc_vis_column();
  943. CombinedLine curr(cursor.para, calc_inner_line());
  944. add_rows_to_line(curr, diff);
  945. cursor.para = curr.para;
  946. // move the cursor to the beginning of inner_line
  947. cursor.pos = (curr.inner_line > 0)
  948. ? curr_para()->line_breaks[curr.inner_line - 1]
  949. : 0;
  950. // reposition the cursor at the right column
  951. move_to_vis_column(optimal_vis_column);
  952. post_vertical_movement();
  953. }
  954. // move_next_line() - interactive command to move the cursor to the next
  955. // screen line (e.g. in response to "key down")
  956. INTERACTIVE void EditBox::move_next_line()
  957. {
  958. move_relative_line(1);
  959. }
  960. // move_previous_line() - interactive command to move the cursor to the
  961. // previous screen line (e.g. in response to "key up")
  962. INTERACTIVE void EditBox::move_previous_line()
  963. {
  964. move_relative_line(-1);
  965. }
  966. // search_forward() - a simple method for searching. It's not adequate for
  967. // most uses: it can't ignore non-spacing marks and there's no corresponding
  968. // method for searching backwards. I'll have to work on that.
  969. //
  970. // returns true if the string was found.
  971. bool EditBox::search_forward(unistring str)
  972. {
  973. str = str.toupper_ascii();
  974. const unichar *first, *last;
  975. for (int i = cursor.para; i < parags_count(); i++) {
  976. const unistring para = paragraphs[i]->str.toupper_ascii();
  977. first = para.begin();
  978. last = para.end();
  979. if (i == cursor.para) {
  980. first += cursor.pos;
  981. // skip the character under the cursor
  982. if (first != last)
  983. first++;
  984. }
  985. const unichar *pos = std::search(first, last, str.begin(), str.end());
  986. if (pos != last) {
  987. cursor.pos = pos - para.begin();
  988. cursor.para = i;
  989. post_horizontal_movement();
  990. return true;
  991. }
  992. }
  993. return false;
  994. }
  995. // move_first_char(ch) - Moves to the first ch in the buffer. When saving the
  996. // buffer to a file the charset conversion may fail. This method is used to
  997. // position the cursor on the offending character.
  998. void EditBox::move_first_char(unichar ch)
  999. {
  1000. for (int i = 0; i < parags_count(); i++) {
  1001. Paragraph &para = *paragraphs[i];
  1002. idx_t len = para.str.len();
  1003. for (int pos = 0; pos <= len; pos++)
  1004. if (
  1005. (pos < len && para.str[pos] == ch) ||
  1006. (pos == len && para.eop != eopNone
  1007. && get_eop_char(para.eop) == ch)
  1008. )
  1009. {
  1010. cursor.para = i;
  1011. cursor.pos = pos;
  1012. post_horizontal_movement();
  1013. return;
  1014. }
  1015. }
  1016. }
  1017. // }}}
  1018. // Visual movement commands {{{
  1019. void EditBox::set_visual_cursor_movement(bool v)
  1020. {
  1021. visual_cursor_movement = v;
  1022. NOTIFY_CHANGE(selection); // I don't have a special notification for this event.
  1023. }
  1024. INTERACTIVE void EditBox::toggle_visual_cursor_movement()
  1025. {
  1026. set_visual_cursor_movement(!get_visual_cursor_movement());
  1027. }
  1028. bool EditBox::is_at_end_of_screen_line()
  1029. {
  1030. idx_t end_pos = curr_para()->str.len();
  1031. for (int line_num = 0; line_num < curr_para()->breaks_count(); line_num++) {
  1032. idx_t line_break = curr_para()->line_breaks[line_num];
  1033. if (cursor.pos < line_break) {
  1034. end_pos = line_break;
  1035. if (line_num != curr_para()->breaks_count() - 1)
  1036. end_pos--;
  1037. break;
  1038. }
  1039. }
  1040. return cursor.pos == end_pos;
  1041. }
  1042. bool EditBox::is_at_first_screen_line()
  1043. {
  1044. return cursor.para == 0 && calc_inner_line() == 0;
  1045. }
  1046. bool EditBox::is_at_last_screen_line()
  1047. {
  1048. return (cursor.para == parags_count()-1)
  1049. && (calc_inner_line() == curr_para()->breaks_count()-1);
  1050. }
  1051. // move_forward_visual_char() - moves a char forward, visually.
  1052. INTERACTIVE void EditBox::move_forward_visual_char()
  1053. {
  1054. int vis_column = calc_vis_column();
  1055. if (is_at_end_of_screen_line()) {
  1056. // move to visual start of next screen line
  1057. if (!is_at_last_screen_line()) {
  1058. move_relative_line(1);
  1059. move_to_vis_column(0);
  1060. }
  1061. } else {
  1062. move_to_vis_column(vis_column+1);
  1063. if (calc_vis_column() == vis_column) {
  1064. // We're stuck on a tab or on a wide character.
  1065. // Ugly hack, sorry :-(
  1066. move_forward_char();
  1067. }
  1068. }
  1069. post_horizontal_movement();
  1070. }
  1071. // move_backward_visual_char() - moves a char backward, visually.
  1072. INTERACTIVE void EditBox::move_backward_visual_char()
  1073. {
  1074. int vis_column = calc_vis_column();
  1075. if (vis_column == 0 // we need to move up a line,
  1076. && !is_at_first_screen_line()) // but only if we're not
  1077. // already at start of buffer
  1078. {
  1079. move_relative_line(-1);
  1080. move_end_of_line();
  1081. } else {
  1082. move_to_vis_column(vis_column-1);
  1083. }
  1084. post_horizontal_movement();
  1085. }
  1086. INTERACTIVE void EditBox::move_beginning_of_visual_line()
  1087. {
  1088. move_to_vis_column(0);
  1089. post_horizontal_movement();
  1090. }
  1091. INTERACTIVE void EditBox::key_home()
  1092. {
  1093. if (visual_cursor_movement)
  1094. move_beginning_of_visual_line();
  1095. else
  1096. move_beginning_of_line();
  1097. }
  1098. // }}}
  1099. // Typing related: justification / indentation / alt_kbd / translation {{{
  1100. // insert_char() - inserts a character into the buffer. It assumes the
  1101. // character was typed by the user, so it also handles auto_justify.
  1102. // If you want to insert a character without this extra processing, use
  1103. // insert_text() instead.
  1104. void EditBox::insert_char(unichar ch)
  1105. {
  1106. insert_text(&ch, 1);
  1107. if (auto_justify && cursor.pos > justification_column) {
  1108. // find previous whitespace
  1109. idx_t wspos = cursor.pos - 1;
  1110. while (wspos > 0 && !BiDi::is_space(curr_para()->str[wspos]))
  1111. --wspos;
  1112. if (wspos > 0) {
  1113. idx_t new_cursor_pos = cursor.pos - wspos - 1;
  1114. cursor.pos = wspos;
  1115. ch = get_curr_eop_char();
  1116. if (rfc2646_trailing_space) {
  1117. // keep the space at the end of the line
  1118. cursor.pos++;
  1119. insert_text(&ch, 1);
  1120. cursor.pos = new_cursor_pos;
  1121. } else {
  1122. // overwrite the space with a new-line
  1123. replace_text(&ch, 1, 1);
  1124. cursor.pos = new_cursor_pos;
  1125. }
  1126. scroll_to_cursor_line();
  1127. }
  1128. }
  1129. last_modification = cursor;
  1130. }
  1131. // key_enter() - called when the user hits Enter
  1132. void EditBox::key_enter()
  1133. {
  1134. if (auto_indent) {
  1135. unistring indent;
  1136. unistring &para = curr_para()->str;
  1137. indent.push_back(get_curr_eop_char());
  1138. for (idx_t i = 0; i < cursor.pos && BiDi::is_space(para[i]); i++)
  1139. indent.push_back(para[i]);
  1140. insert_text(indent);
  1141. } else {
  1142. insert_char(get_curr_eop_char());
  1143. }
  1144. }
  1145. // key_enter() - interactive command to insert maqaf into the buffer
  1146. INTERACTIVE void EditBox::insert_maqaf()
  1147. {
  1148. insert_char(UNI_HEB_MAQAF);
  1149. }
  1150. // key_dash() - called when the user types '-'
  1151. void EditBox::key_dash()
  1152. {
  1153. // if the previous [non-NSM] char is RTL and we're in smart_typing mode,
  1154. // insert maqaf.
  1155. if (smart_typing && cursor.pos > 0
  1156. && (BiDi::is_rtl(curr_para()->str[cursor.pos-1])
  1157. || (BiDi::is_nsm(curr_para()->str[cursor.pos-1])
  1158. && cursor.pos > 1
  1159. && BiDi::is_rtl(curr_para()->str[cursor.pos-2]))) )
  1160. insert_maqaf();
  1161. else
  1162. insert_char('-');
  1163. }
  1164. void EditBox::set_translation_mode(bool value)
  1165. {
  1166. if (!transtbl.empty()) {
  1167. translation_mode = value;
  1168. NOTIFY_CHANGE(translation_mode);
  1169. } else {
  1170. NOTIFY_ERROR(no_translation_table);
  1171. }
  1172. }
  1173. void EditBox::set_alt_kbd(bool val)
  1174. {
  1175. if (!altkbdtbl.empty()) {
  1176. alt_kbd = val;
  1177. NOTIFY_CHANGE(alt_kbd);
  1178. }
  1179. }
  1180. INTERACTIVE void EditBox::toggle_alt_kbd()
  1181. {
  1182. if (!altkbdtbl.empty()) {
  1183. set_alt_kbd(!get_alt_kbd());
  1184. } else {
  1185. NOTIFY_ERROR(no_alt_kbd);
  1186. }
  1187. }
  1188. // handle_event() - deals with literal keys. Special keys (control & alt
  1189. // combinations, function keys, arrows, etc) are dealt by the base class,
  1190. // Dispatcher.
  1191. //
  1192. // When "tranlation mode" is active, it translates the character according to
  1193. // the "transtbl" table. When "alt_kbd" is active -- according to the
  1194. // "altkbdtbl" table.
  1195. bool EditBox::handle_event(const Event &evt)
  1196. {
  1197. current_command_type = cmdtpUnknown;
  1198. if (evt.is_literal()) {
  1199. unichar ch = evt.ch;
  1200. if (in_translation_mode()) {
  1201. transtbl.translate_char(ch);
  1202. set_translation_mode(false);
  1203. } else if (alt_kbd) {
  1204. altkbdtbl.translate_char(ch);
  1205. }
  1206. switch (ch) {
  1207. case 13: key_enter(); break;
  1208. case '-': key_dash(); break;
  1209. default:
  1210. insert_char(ch);
  1211. }
  1212. prev_command_type = current_command_type;
  1213. return true;
  1214. } else {
  1215. bool ret = Dispatcher::handle_event(evt);
  1216. prev_command_type = current_command_type;
  1217. return ret;
  1218. }
  1219. }
  1220. static bool is_empty_or_starts_with_space(const unistring &str) {
  1221. return (str.empty() || BiDi::is_space(str[0]));
  1222. }
  1223. static bool is_blank(const unistring &str) {
  1224. for (idx_t i = 0; i < str.len(); i++)
  1225. if (!BiDi::is_space(str[i]))
  1226. return false;
  1227. return true;
  1228. }
  1229. // justify() - interactive command to justify the current "paragraph". Here
  1230. // "paragraph" means a bunch of Paragraphs (what a user thinks of as "lines")
  1231. // separated by blank or indented Paragraphs.
  1232. INTERACTIVE void EditBox::justify()
  1233. {
  1234. // Step 1.
  1235. // Move to a non-blank line
  1236. while (cursor.para < parags_count() - 1
  1237. && is_blank(curr_para()->str))
  1238. move_next_line();
  1239. // Step 2.
  1240. // Find the min and max lines that constitute this paragraph.
  1241. int min_para, max_para;
  1242. min_para = max_para = cursor.para;
  1243. // paragraphs are separated by blank lines or indentations.
  1244. while (min_para > 0
  1245. && !is_empty_or_starts_with_space(paragraphs[min_para - 1]->str))
  1246. --min_para;
  1247. while (max_para < parags_count() - 1
  1248. && !is_empty_or_starts_with_space(paragraphs[max_para + 1]->str))
  1249. ++max_para;
  1250. // if we're an indented paragraph, include the indented line.
  1251. if (min_para > 0
  1252. && !is_empty_or_starts_with_space(paragraphs[min_para]->str)
  1253. && !is_blank(paragraphs[min_para - 1]->str))
  1254. --min_para;
  1255. // Step 3.
  1256. // Collect all the lines into 'text'
  1257. unistring text;
  1258. for (int i = min_para; i <= max_para; i++) {
  1259. if (!text.empty() && !BiDi::is_space(text.back()))
  1260. text.push_back(' ');
  1261. text.append(paragraphs[i]->str);
  1262. }
  1263. // Step 4.
  1264. // Justify 'text' into 'justified'
  1265. unistring justified;
  1266. while (!text.empty()) {
  1267. if (!justified.empty()) {
  1268. if (rfc2646_trailing_space)
  1269. justified.push_back(' ');
  1270. justified.push_back(get_curr_eop_char());
  1271. }
  1272. int wspos = justification_column;
  1273. if (wspos >= text.len()) {
  1274. justified.append(text);
  1275. text.clear();
  1276. } else {
  1277. while (wspos > 0 && !BiDi::is_space(text[wspos]))
  1278. --wspos;
  1279. if (wspos > 0) {
  1280. justified.append(text, wspos);
  1281. text.erase_head(wspos + 1);
  1282. } else {
  1283. justified.append(text, justification_column);
  1284. text.erase_head(justification_column);
  1285. }
  1286. }
  1287. }
  1288. // Step 5.
  1289. // Delete the unjustified lines from the buffer and insert
  1290. // the 'justified' string instead.
  1291. cursor.pos = 0;
  1292. cursor.para = min_para;
  1293. int delete_len = calc_distance(cursor,
  1294. Point(max_para, paragraphs[max_para]->str.len()));
  1295. replace_text(justified, delete_len);
  1296. // Move past this paragraph, so that the next "justify" command
  1297. // justifies the next paragraph and so on.
  1298. move_next_line();
  1299. move_beginning_of_line();
  1300. }
  1301. // }}}
  1302. // Paragraph Base Direction {{{
  1303. void EditBox::set_dir_algo(diralgo_t value)
  1304. {
  1305. dir_algo = value;
  1306. for (int i = 0; i < parags_count(); i++)
  1307. paragraphs[i]->determine_base_dir(dir_algo);
  1308. if (dir_algo == algoContextStrong || dir_algo == algoContextRTL)
  1309. calc_contextual_dirs(0, parags_count() - 1, false);
  1310. request_update(rgnAll);
  1311. NOTIFY_CHANGE(dir_algo);
  1312. }
  1313. // toggle_dir_algo() - interactive command to toggle the directionality
  1314. // algorithm used.
  1315. INTERACTIVE void EditBox::toggle_dir_algo() {
  1316. switch (dir_algo) {
  1317. case algoUnicode: set_dir_algo(algoContextStrong); break;
  1318. case algoContextStrong: set_dir_algo(algoContextRTL); break;
  1319. case algoContextRTL: set_dir_algo(algoForceLTR); break;
  1320. case algoForceLTR: set_dir_algo(algoForceRTL); break;
  1321. case algoForceRTL: set_dir_algo(algoUnicode); break;
  1322. }
  1323. }
  1324. // calc_contextual_dirs() - this method implements the "contextual" algorithm.
  1325. // Other algorithms are implemented in BiDi::determine_base_dir(), but the
  1326. // BiDi class doesn't know about the context of the paragraph, so we have to
  1327. // do most of the work in EditBox.
  1328. //
  1329. // The contextual algorithm has the following effect:
  1330. //
  1331. // 1. If there's an RTL character in the paragraph, set the base
  1332. // direction to RTL;
  1333. // 2. Else: if there's an LTR character in the paragraph, set the base
  1334. // direction to LTR;
  1335. // 3. Else: the paragraph is neutral; set the base direction to the
  1336. // base direction of the previous non-neutral paragraph. If all
  1337. // previous paragraphs are neuteral, set it to the base direction
  1338. // of the next non-neutral paragraph. If all paragraphs are
  1339. // neutral, set it to LTR.
  1340. //
  1341. // #1 and #2 are implemented in BiDi::determine_base_dir(). #3 is implemented
  1342. // here.
  1343. void EditBox::calc_contextual_dirs(int min_para, int max_para,
  1344. bool update_display)
  1345. {
  1346. // find the previous strong paragraph
  1347. while (min_para > 0) {
  1348. min_para--;
  1349. if (paragraphs[min_para]->individual_base_dir != dirN)
  1350. break;
  1351. }
  1352. // find the next strong paragraph
  1353. while (max_para < parags_count() - 1) {
  1354. max_para++;
  1355. if (paragraphs[max_para]->individual_base_dir != dirN)
  1356. break;
  1357. }
  1358. // loop from min_para to max_para and whenever you find a neutral
  1359. // paragraph, set its base direction to the last strong direction you
  1360. // encountered (prev_strong).
  1361. direction_t prev_strong = dirN;
  1362. for (int i = min_para; i <= max_para; i++) {
  1363. Paragraph &p = *paragraphs[i];
  1364. if (p.individual_base_dir == dirN) {
  1365. set_contextual_dir(p, prev_strong, update_display);
  1366. } else {
  1367. set_contextual_dir(p, p.individual_base_dir, update_display);
  1368. prev_strong = p.individual_base_dir;
  1369. }
  1370. }
  1371. // Do the same but now loop backwards: from max_para to min_para.
  1372. prev_strong = dirN;
  1373. for (int i = max_para; i >= min_para; i--) {
  1374. Paragraph &p = *paragraphs[i];
  1375. if (p.contextual_base_dir == dirN) {
  1376. set_contextual_dir(p, prev_strong, update_display);
  1377. } else {
  1378. prev_strong = p.contextual_base_dir;
  1379. }
  1380. }
  1381. // If there're any neutral paragraphs left, set their base direction to
  1382. // LTR
  1383. for (int i = min_para; i <= max_para; i++) {
  1384. Paragraph &p = *paragraphs[i];
  1385. if (p.contextual_base_dir == dirN)
  1386. set_contextual_dir(p, dirLTR, update_display);
  1387. }
  1388. }
  1389. inline void EditBox::set_contextual_dir(Paragraph &p, direction_t dir,
  1390. bool update_display)
  1391. {
  1392. if (!update_display)
  1393. p.contextual_base_dir = dir;
  1394. else {
  1395. if (p.contextual_base_dir != dir) {
  1396. // if only the current paragraph has changed its direction,
  1397. // we don't need to repaint all the window.
  1398. p.contextual_base_dir = dir;
  1399. if (&p == curr_para())
  1400. request_update(rgnCurrent);
  1401. else
  1402. request_update(rgnAll);
  1403. }
  1404. }
  1405. }
  1406. // }}}
  1407. // Modification {{{
  1408. // post_modification() - should be called at the end of any primitive command
  1409. // that changes the buffer. It invalidates the selection, the optimal visual
  1410. // column and notifies our listeners.
  1411. void EditBox::post_modification()
  1412. {
  1413. invalidate_optimal_vis_column();
  1414. last_modification = cursor;
  1415. cache.invalidate();
  1416. if (is_primary_mark_set())
  1417. unset_primary_mark();
  1418. if (!is_modified())
  1419. set_modified(true);
  1420. NOTIFY_CHANGE(position);
  1421. }
  1422. // post_para_modification() - should be called for every paragraph that has
  1423. // been modified. It recalculates its base direcion and rewraps it.
  1424. void EditBox::post_para_modification(Paragraph &p)
  1425. {
  1426. p.determine_base_dir(dir_algo);
  1427. wrap_para(p);
  1428. }
  1429. // delete_forward_char() - interactive command to delete the character
  1430. // we're on. Usually bound to the DELETE key.
  1431. INTERACTIVE void EditBox::delete_forward_char()
  1432. {
  1433. delete_text(1);
  1434. }
  1435. // delete_backward_char() - interactive command to delete the previous
  1436. // character. Usually bound to the BACKSPACE key.
  1437. INTERACTIVE void EditBox::delete_backward_char()
  1438. {
  1439. if (!is_at_beginning_of_buffer()) {
  1440. move_backward_char();
  1441. delete_forward_char();
  1442. }
  1443. }
  1444. // cut_end_of_paragraph() - interactive command to cut till
  1445. // end of paragraph. If the cursor is already at the end, it deletes
  1446. // the EOP (ala emacs).
  1447. INTERACTIVE void EditBox::cut_end_of_paragraph()
  1448. {
  1449. int count;
  1450. if (cursor.pos == curr_para()->str.len())
  1451. count = 1;
  1452. else
  1453. count = curr_para()->str.len() - cursor.pos;
  1454. Point orig_cursor = cursor;
  1455. copy_text(count, (prev_command_type == cmdtpKill));
  1456. cursor = orig_cursor;
  1457. delete_text(count);
  1458. current_command_type = cmdtpKill;
  1459. }
  1460. // delete_paragraph() - interactive command to delete the current
  1461. // paragraph.
  1462. INTERACTIVE void EditBox::delete_paragraph()
  1463. {
  1464. cursor.pos = 0;
  1465. delete_text(curr_para()->str.len() + 1);
  1466. }
  1467. // delete_forward_word() - interactive command to delete till the
  1468. // end of the current or next word.
  1469. INTERACTIVE void EditBox::delete_forward_word()
  1470. {
  1471. Point start = cursor;
  1472. move_forward_word();
  1473. int len = calc_distance(cursor, start);
  1474. cursor = start;
  1475. delete_text(len);
  1476. }
  1477. // delete_backward_word() - interactive command to delete till the
  1478. // start of the current or previous word.
  1479. INTERACTIVE void EditBox::delete_backward_word()
  1480. {
  1481. Point end = cursor;
  1482. move_backward_word();
  1483. int len = calc_distance(cursor, end);
  1484. delete_text(len);
  1485. }
  1486. // replace_text() - is a combination of delete_text() and insert_text(). we
  1487. // should use it instead of calling these two methods separately because it
  1488. // records these two operations on the undo stack as one atomic operation.
  1489. void EditBox::replace_text(const unichar *str, int len, int delete_len,
  1490. bool skip_undo)
  1491. {
  1492. unistring deleted;
  1493. Point point = cursor;
  1494. delete_text(delete_len, true, &deleted);
  1495. insert_text(str, len, true);
  1496. if (!skip_undo) {
  1497. UndoOp op;
  1498. op.type = opReplace;
  1499. op.point = point;
  1500. op.deleted_text = deleted;
  1501. op.inserted_text.append(str, len);
  1502. undo_stack.record_op(op);
  1503. }
  1504. }
  1505. // delete_text() - deletes len characters, starting at the cursor.
  1506. //
  1507. // @param skip_undo - don't record this operation on the undo stack.
  1508. // @param alt_deleted - copy the deleted text here.
  1509. void EditBox::delete_text(int len, bool skip_undo, unistring *alt_deleted)
  1510. {
  1511. if (read_only) {
  1512. NOTIFY_ERROR(read_only);
  1513. return;
  1514. }
  1515. unistring _deleted, *deleted;
  1516. deleted = alt_deleted ? alt_deleted : &_deleted;
  1517. int parags_deleted = 0; // we keep count of how many parags we delete.
  1518. while (len > 0) {
  1519. if (cursor.pos == curr_para()->str.len()) {
  1520. if (cursor.para < parags_count() - 1) {
  1521. // Delete the EOP. that is, append the next
  1522. // paragraph to the current paragraph.
  1523. deleted->push_back(get_curr_eop_char());
  1524. Paragraph *next_para = paragraphs[cursor.para + 1];
  1525. curr_para()->str.append(next_para->str);
  1526. curr_para()->eop = next_para->eop;
  1527. delete next_para;
  1528. paragraphs.erase(paragraphs.begin() + cursor.para + 1);
  1529. }
  1530. len--;
  1531. parags_deleted++;
  1532. } else {
  1533. // delete the [cursor, end-of-paragraph) segment
  1534. int to_delete = MIN(curr_para()->str.len() - cursor.pos, len);
  1535. unichar *cursor_ptr = curr_para()->str.begin() + cursor.pos;
  1536. deleted->append(cursor_ptr, to_delete);
  1537. curr_para()->str.erase(cursor_ptr, cursor_ptr + to_delete);
  1538. len -= to_delete;
  1539. }
  1540. }
  1541. // optimization: we save some values and latter check whether they changed.
  1542. int orig_num_lines = curr_para()->breaks_count();
  1543. direction_t orig_individual_base_dir = curr_para()->individual_base_dir;
  1544. post_para_modification(*curr_para());
  1545. // if no para was deleted, and the direction of the current para has not
  1546. // changed, it cannot affect the surrounding parags, so we don't call
  1547. // calc_contextual_dirs()
  1548. if (parags_deleted ||
  1549. curr_para()->individual_base_dir != orig_individual_base_dir)
  1550. calc_contextual_dirs(cursor.para, cursor.para, true);
  1551. // if no para was deleted, if no screen lines were deleted, we can
  1552. // update only rgnCurrent instead of the whole window.
  1553. if (!parags_deleted && curr_para()->breaks_count() == orig_num_lines)
  1554. request_update(rgnCurrent);
  1555. else
  1556. request_update(rgnAll);
  1557. scroll_to_cursor_line();
  1558. if (!skip_undo) {
  1559. UndoOp op;
  1560. op.type = opDelete;
  1561. op.point = cursor;
  1562. op.deleted_text = *deleted;
  1563. undo_stack.record_op(op);
  1564. }
  1565. post_modification();
  1566. }
  1567. // insert_text() - inserts text into the buffer. advances the cursor.
  1568. //
  1569. // @param skip_undo - don't record this operation on the undo stack.
  1570. void EditBox::insert_text(const unichar *str, int len, bool skip_undo)
  1571. {
  1572. if (read_only && !is_in_data_transfer()) {
  1573. if (len)
  1574. NOTIFY_ERROR_ARG(read_only, str[0]);
  1575. return;
  1576. }
  1577. if (!skip_undo) {
  1578. UndoOp op;
  1579. op.type = opInsert;
  1580. op.point = cursor;
  1581. op.inserted_text.append(str, len);
  1582. undo_stack.record_op(op);
  1583. }
  1584. int min_changed_para, max_changed_para; // which parags have changed?
  1585. min_changed_para = cursor.para;
  1586. while (len > 0) {
  1587. if (is_eop(str[0])) {
  1588. // inserting EOP is like pressing Enter: split the current
  1589. // paragraph into two.
  1590. Paragraph *p = new Paragraph();
  1591. p->str.insert(0, &curr_para()->str[cursor.pos],
  1592. curr_para()->str.end());
  1593. p->eop = curr_para()->eop;
  1594. curr_para()->str.erase(&curr_para()->str[cursor.pos],
  1595. curr_para()->str.end());
  1596. curr_para()->eop = get_eop_type(str[0]);
  1597. paragraphs.insert(paragraphs.begin() + cursor.para + 1, p);
  1598. cursor.para++;
  1599. cursor.pos = 0;
  1600. len--;
  1601. str++;
  1602. } else {
  1603. int line_len = 0;
  1604. while (line_len < len && !is_eop(str[line_len]))
  1605. line_len++;
  1606. curr_para()->str.insert(curr_para()->str.begin() + cursor.pos,
  1607. str, str + line_len);
  1608. cursor.pos += line_len;
  1609. len -= line_len;
  1610. str += line_len;
  1611. }
  1612. }
  1613. max_changed_para = cursor.para;
  1614. // optimization: we save some values and latter check whether they changed.
  1615. int orig_num_lines = curr_para()->breaks_count();
  1616. direction_t orig_individual_base_dir = curr_para()->individual_base_dir;
  1617. // update state variables of the affected parags.
  1618. for (int i = min_changed_para; i <= max_changed_para; i++)
  1619. post_para_modification(*paragraphs[i]);
  1620. // if only one para was modified, and its direction has not changed,
  1621. // it doesn't affect the surrounding parags, so we don't call
  1622. // calc_contextual_dirs()
  1623. if (!(min_changed_para == max_changed_para &&
  1624. curr_para()->individual_base_dir == orig_individual_base_dir))
  1625. calc_contextual_dirs(min_changed_para, max_changed_para, true);
  1626. // if only one para was modified, and no screen lines were added, we can
  1627. // update only rgnCurrent instead of the whole window.
  1628. if (min_changed_para == max_changed_para
  1629. && curr_para()->breaks_count() == orig_num_lines)
  1630. request_update(rgnCurrent);
  1631. else
  1632. request_update(rgnAll);
  1633. scroll_to_cursor_line();
  1634. post_modification();
  1635. }
  1636. // }}}