inputline.cc 12 KB

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