inputline.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #if HAVE_DIRENT_H
  18. # include <dirent.h> // for filename completion
  19. # define NAMLEN(dirent) strlen((dirent)->d_name)
  20. #else
  21. # define dirent direct
  22. # define NAMLEN(dirent) (dirent)->d_namlen
  23. # if HAVE_SYS_NDIR_H
  24. # include <sys/ndir.h>
  25. # endif
  26. # if HAVE_SYS_DIR_H
  27. # include <sys/dir.h>
  28. # endif
  29. # if HAVE_NDIR_H
  30. # include <ndir.h>
  31. # endif
  32. #endif
  33. #include <unistd.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <algorithm>
  37. #include <map>
  38. #include "inputline.h"
  39. #include "io.h" // expand_tilde
  40. #include "themes.h"
  41. #include "dbg.h"
  42. // A history is stored as a StringArray. There are usually several histories
  43. // in one application (for example, the history for an "Open file:" stores
  44. // file names, and the history for a "Find:" stores search strings). The
  45. // histories are stored in a history map, and an identifier, history_set, that
  46. // is passed to InputLine's constructor, is used in selecting the appropriate
  47. // history.
  48. static std::map<int, InputLine::StringArray> histories;
  49. // init_history() - initializes the "history" pointer and pushes an initial
  50. // value into it. when history_set is zero, history is disabled for this
  51. // InputLine object.
  52. void InputLine::init_history(int history_set)
  53. {
  54. if (history_set == 0) {
  55. history = NULL;
  56. } else {
  57. history = &histories[history_set];
  58. history_idx = 0;
  59. // Add the current text to history, provided it's not already
  60. // there; if the last entry is the empty string, overwrite
  61. // it instead of preserving it.
  62. if (history->empty()) {
  63. history->insert(history->begin(), get_text());
  64. } else {
  65. if (history->front().empty())
  66. history->front() = get_text();
  67. else if (get_text() != history->front())
  68. history->insert(history->begin(), get_text());
  69. }
  70. }
  71. }
  72. InputLine::InputLine(const char *aLabel, const unistring &default_text,
  73. int history_set, CompleteType complete)
  74. {
  75. set_label(aLabel);
  76. set_text(default_text);
  77. init_history(history_set);
  78. complete_type = complete;
  79. wrap_type = wrpOff;
  80. event_num = 0;
  81. last_tab_event_num = -2;
  82. set_modal();
  83. }
  84. void InputLine::set_label(const char *aLabel)
  85. {
  86. unistring label;
  87. label.init_from_utf8(aLabel);
  88. label_dir = BiDi::determine_base_dir(label.begin(), label.size(), algoUnicode);
  89. BiDi::simple_log2vis(label, label_dir, vis_label);
  90. label_width = get_str_width(vis_label.begin(), vis_label.size());
  91. margin_before = 2 + label_width;
  92. margin_after = 2;
  93. }
  94. void InputLine::set_text(const unistring &s)
  95. {
  96. new_document();
  97. insert_text(s, true);
  98. }
  99. // redraw_paragraph() - override EditBox's method. it calls base and then
  100. // draws the label.
  101. void InputLine::redraw_paragraph(Paragraph &p,
  102. int window_start_line, bool only_cursor, int para_num)
  103. {
  104. // If the label is RTL, it's printed on the right, else --
  105. // on the left. We use either margin_before or margin_after to
  106. // reserve the space for it.
  107. if ( (p.is_rtl() && label_dir == dirRTL)
  108. || (!p.is_rtl() && label_dir == dirLTR) ) {
  109. margin_before = 2 + label_width;
  110. margin_after = 1;
  111. } else {
  112. margin_after = 2 + label_width;
  113. margin_before = 1;
  114. }
  115. // Curses bug: don't paint on the bottom-right cell, because
  116. // some Curses implementations get confused.
  117. if (label_dir == dirLTR && !p.is_rtl()) {
  118. margin_after = 2;
  119. }
  120. EditBox::redraw_paragraph(p, window_start_line, only_cursor, para_num);
  121. if (!only_cursor) {
  122. // draw label
  123. if (label_dir == dirRTL) {
  124. wmove(wnd, 0, window_width()
  125. - (p.is_rtl() ? margin_before : margin_after)
  126. + 1);
  127. draw_unistr(vis_label.begin(), vis_label.size());
  128. } else {
  129. wmove(wnd, 0, 1);
  130. draw_unistr(vis_label.begin(), vis_label.size());
  131. }
  132. // reposition the cursor
  133. EditBox::redraw_paragraph(p, window_start_line, true, para_num);
  134. }
  135. }
  136. // get_directory_files() - populates files_list.
  137. //
  138. // @param directory - the directory to list.
  139. void InputLine::get_directory_files(u8string directory, const char *prefix)
  140. {
  141. expand_tilde(directory);
  142. files_list.clear();
  143. DIR *dir = opendir(directory.c_str());
  144. if (dir == NULL) {
  145. signal_error();
  146. return;
  147. }
  148. dirent *ent;
  149. u8string pathname = directory;
  150. int path_file_pos = pathname.size();
  151. while ((ent = readdir(dir))) {
  152. const char *d_name = ent->d_name;
  153. if (STREQ(d_name, ".") || STREQ(d_name, ".."))
  154. continue;
  155. if (prefix && (strncmp(prefix, d_name, strlen(prefix)) != 0))
  156. continue;
  157. pathname.erase(path_file_pos, pathname.size());
  158. pathname += d_name;
  159. struct stat file_info;
  160. stat(pathname.c_str(), &file_info);
  161. bool is_dir = S_ISDIR(file_info.st_mode);
  162. unistring filename;
  163. filename.init_from_filename(d_name);
  164. if (is_dir)
  165. filename.push_back('/');
  166. if (complete_type == cmpltAll
  167. || (complete_type == cmpltDirectories && is_dir))
  168. files_list.push_back(filename);
  169. }
  170. closedir(dir);
  171. // it's important that this list be sorted.
  172. std::sort(files_list.begin(), files_list.end());
  173. }
  174. // init_completion() - initialize filename completion. it reads the directory
  175. // and (partial) filename components under the cursor, calls
  176. // get_directory_files() to list the files, and sets slice_begin and
  177. // slice_end to the relevant range in files_list.
  178. void InputLine::init_completion()
  179. {
  180. trim();
  181. unistring &line = curr_para()->str;
  182. // get directory, filename components
  183. unistring filename;
  184. u8string directory;
  185. int i = cursor.pos - 1;
  186. while (i >= 0 && line[i] != '/')
  187. --i;
  188. ++i; // move past '/'
  189. // get the directory component
  190. directory.init_from_unichars(line.begin(), i);
  191. if (directory.empty())
  192. directory = "./";
  193. // get the filename component
  194. filename.append(line.begin() + i, cursor.pos - i);
  195. if (0) {
  196. // if we haven't changed directory, no need to reread
  197. // directory contents.
  198. if (files_directory != directory) {
  199. files_directory = directory;
  200. get_directory_files(directory);
  201. }
  202. } else {
  203. // All in all, the following is faster, because it
  204. // doesn't stat(2) everything in the directory.
  205. get_directory_files(directory, u8string(filename).c_str());
  206. }
  207. insertion_pos = cursor.pos;
  208. prefix_len = filename.len();
  209. slice_begin = slice_end = curr_choice = -1;
  210. for (unsigned i = 0; i < files_list.size(); i++) {
  211. unistring &entry = files_list[i];
  212. if (entry.len() >= prefix_len)
  213. if (std::equal(filename.begin(), filename.end(), entry.begin())) {
  214. if (slice_begin == -1)
  215. slice_begin = i;
  216. slice_end = i;
  217. }
  218. }
  219. }
  220. // complete() - completes the filename under the cursor.
  221. //
  222. // @param forward - complete to the next filename if true; to the
  223. // previous one if false.
  224. void InputLine::complete(bool forward)
  225. {
  226. // event_num and last_tab_event_num are used as a crude mechanism to
  227. // determine if a TAB session has ended, e.g. as a result of a key
  228. // other than TAB being pressed.
  229. //
  230. // On each event event_num is incremented (see handle_event()). if
  231. // last_tab_event_num is event_num shy 1, it means our TAB session
  232. // was not interrupted.
  233. bool restart = (event_num - 1 != last_tab_event_num);
  234. last_tab_event_num = event_num;
  235. if (restart) {
  236. // starts a TAB session.
  237. init_completion();
  238. // give warning when the length of the partial filename component
  239. // is 0. This means that we browse through _all_ the files in
  240. // the directory.
  241. if (prefix_len == 0)
  242. signal_error();
  243. }
  244. if (slice_begin == -1) {
  245. // no filenames begin with the partial filename component.
  246. signal_error();
  247. return;
  248. }
  249. int prev_choice_len = 0;
  250. if (curr_choice != -1)
  251. prev_choice_len = files_list[curr_choice].size() - prefix_len;
  252. // wrap around the completion slice if we bumped into its end.
  253. if (curr_choice != -1)
  254. curr_choice += forward ? 1 : -1;
  255. if (curr_choice > slice_end || curr_choice < slice_begin)
  256. curr_choice = -1;
  257. if (curr_choice == -1)
  258. curr_choice = forward ? slice_begin : slice_end;
  259. cursor.pos = insertion_pos;
  260. replace_text(files_list[curr_choice].begin() + prefix_len,
  261. files_list[curr_choice].size() - prefix_len, prev_choice_len);
  262. // a special case: if we've uniquely completed a direcory name, we want
  263. // the next TAB to start a new TAB session to complete the files within.
  264. if (slice_begin == slice_end
  265. && *(files_list[curr_choice].end() - 1) == '/')
  266. last_tab_event_num = -2;
  267. }
  268. // previous_completion() - interactive command to move backward in the
  269. // completion list. Usually bound to the M-TAB key.
  270. INTERACTIVE void InputLine::previous_completion()
  271. {
  272. if (complete_type != cmpltOff)
  273. complete(false);
  274. }
  275. // next_completion() - interactive command to move forward in the
  276. // completion list. Usually bound to the TAB key.
  277. INTERACTIVE void InputLine::next_completion()
  278. {
  279. if (complete_type != cmpltOff)
  280. complete(true);
  281. }
  282. bool InputLine::handle_event(const Event &evt)
  283. {
  284. // Emulate contemporary GUIs, where the input is
  285. // cleared on first letter typed.
  286. if (event_num == 0 && evt.is_literal() && evt.ch != 13) {
  287. // we don't use new_document() because we want to
  288. // be able to undo this operation.
  289. delete_paragraph();
  290. }
  291. if (event_num == 0)
  292. request_update(rgnAll); // make sure do_syntax_highlight is called.
  293. event_num++;
  294. if (!Dispatcher::handle_event(evt))
  295. return EditBox::handle_event(evt);
  296. return false;
  297. }
  298. // do_syntax_highlight() - Emulate comtemporary GUIs: when event_num is 0,
  299. // select all the text to hint the user the next letter will erase everything.
  300. void InputLine::do_syntax_highlight(const unistring &str,
  301. AttributeArray &attributes, int para_num)
  302. {
  303. if (event_num == 0) {
  304. attribute_t selected_attr = get_attr(EDIT_SELECTED_ATTR);
  305. for (idx_t i = 0; i < str.len(); i++)
  306. attributes[i] = selected_attr;
  307. }
  308. }
  309. // trim() - removes the wspaces at the start and end of the text
  310. // the user typed.
  311. void InputLine::trim()
  312. {
  313. unistring &line = curr_para()->str;
  314. // delete wspaces at start of line
  315. idx_t i = 0;
  316. while (i < line.len() && BiDi::is_space(line[i]))
  317. i++;
  318. if (i) {
  319. idx_t prev_pos = cursor.pos;
  320. cursor.pos = 0;
  321. delete_text(i);
  322. if (prev_pos >= i)
  323. cursor.pos = prev_pos - i;
  324. request_update(rgnCurrent);
  325. }
  326. // delete wspaces at end of line
  327. i = line.len() - 1;
  328. while (i >= 0 && BiDi::is_space(line[i]))
  329. i--;
  330. i++;
  331. if (i < line.len()) {
  332. idx_t prev_pos = cursor.pos;
  333. cursor.pos = i;
  334. delete_text(line.len() - i);
  335. if (prev_pos < i)
  336. cursor.pos = prev_pos;
  337. request_update(rgnCurrent);
  338. }
  339. }
  340. INTERACTIVE void InputLine::end_modal()
  341. {
  342. trim();
  343. if (history) {
  344. // User presses Enter.
  345. // If the user has altered the default text, don't overwrite the
  346. // history entry but create a new entry intead.
  347. if (history_idx == 0 && get_text() != history->front()
  348. && !history->front().empty())
  349. history->insert(history->begin(), get_text());
  350. else
  351. update_history();
  352. }
  353. EditBox::end_modal();
  354. }
  355. void InputLine::update_history()
  356. {
  357. if (history)
  358. (*history)[history_idx] = get_text();
  359. }
  360. // previous_history() - interactive command to move to the previous history
  361. // entry. Usually bound to the "Arrow Up" or C-p key.
  362. INTERACTIVE void InputLine::previous_history()
  363. {
  364. if (history && history_idx < (int)history->size() - 1) {
  365. update_history();
  366. history_idx++;
  367. set_text((*history)[history_idx]);
  368. event_num = 0;
  369. }
  370. }
  371. // next_history() - interactive command to move to the next history
  372. // entry. Usually bound to the "Arrow Down" or C-n key.
  373. INTERACTIVE void InputLine::next_history()
  374. {
  375. if (history && history_idx > 0) {
  376. update_history();
  377. history_idx--;
  378. set_text((*history)[history_idx]);
  379. event_num = 0;
  380. }
  381. }