void DocumentView::trim_trailing_whitespace() { //FIXME: Don't strip whitespace if the language doesn't like it (e.g. 'diff') auto current = buffer()->begin(); while(current != buffer()->end()) { current.forward_to_line_end(); //Move to the newline character auto line_end = current; //Store the end of the line while(is_whitespace(--current)) {} //Move backward until we get to non-whitespace current = buffer()->erase(++current, line_end); current.forward_line(); //Move to the next line, or the end of the final line } }
tree texmacs_invarianted_extend (tree id, string src) { int b, e, n= N(src); get_range (id, b, e, src); //cout << "Extending" << LF << HRULE << src (b, e) << LF << HRULE; rewind_spaces (src, b); while (b>0 && src[b-1] == '\n') { int prev= b-1; rewind_line (src, prev); if (is_comment_line (src (prev, b-1))) b= prev; else break; } skip_spaces (src, e); if (e<n && src[e] == '%') skip_line (src, e); while (e<n && src[e] == '\n') { int next= e+1; forward_line (src, next); if (is_comment_line (src (e+1, next))) e= next; else break; } //cout << "Extended to" << LF << HRULE << src (b, e) << LF << HRULE; return as_string (b) * ":" * as_string (e); }
void DocumentView::build_widgets() { scrolled_window_.add(view_); view_.set_buffer(buffer_); view_.signal_populate_popup().connect(sigc::mem_fun(this, &DocumentView::populate_popup)); auto manager = Gsv::StyleSchemeManager::get_default(); view_.get_source_buffer()->set_style_scheme(manager->get_scheme("delimit")); auto provider = Gsv::CompletionWords::create("", Glib::RefPtr<Gdk::Pixbuf>()); provider->register_provider(buffer_); provider->property_priority().set_value(1); view_.get_completion()->add_provider(provider); view_.get_completion()->signal_show().connect([&]() { completion_visible_ = true; }); view_.get_completion()->signal_hide().connect([&]() { completion_visible_ = false; }); auto coverage_attrs = Gsv::MarkAttributes::create(); Gdk::RGBA coverage_colour; coverage_colour.set_rgba(1.0, 0.5, 0, 0.12); coverage_attrs->set_background(coverage_colour); view_.set_mark_attributes("coverage", coverage_attrs, 0); auto linter_attrs = Gsv::MarkAttributes::create(); linter_attrs->set_icon_name("dialog-error"); g_signal_connect(linter_attrs->gobj(), "query-tooltip-markup", G_CALLBACK(get_tooltip), view_.gobj()); view_.set_mark_attributes("linter", linter_attrs, 10); auto breakpoint = Gsv::MarkAttributes::create(); breakpoint->set_icon_name("stop"); view_.set_mark_attributes("breakpoint", breakpoint, 11); view_.add_events(Gdk::BUTTON_PRESS_MASK); view_.signal_button_press_event().connect([&](GdkEventButton* evt) -> bool { if(!buffer_->get_language() || buffer_->get_language()->get_name() != "Python") { return false; } if(evt->type == GDK_2BUTTON_PRESS && evt->window == view_.get_window(Gtk::TEXT_WINDOW_LEFT)->gobj()) { //The user clicked within the left gutter if(evt->button == 1) { //Left click int x_buf, y_buf; view_.window_to_buffer_coords( Gtk::TEXT_WINDOW_LEFT, int(evt->x), int(evt->y), x_buf, y_buf ); //Line bounds Gtk::TextBuffer::iterator iter; int line_top; view_.get_line_at_y(iter, y_buf, line_top); auto mark_list = buffer_->get_source_marks_at_iter(iter, "breakpoint"); if(!mark_list.empty()) { for(auto& mark: mark_list) { buffer_->delete_mark(mark); } auto end_iter = iter; end_iter.forward_line(); unicode line = buffer_->get_slice(iter, end_iter).c_str(); if(line.contains("import ipdb; ipdb.set_trace()")) { buffer_->erase(iter, end_iter); } } else { buffer_->create_source_mark("breakpoint", iter); auto end_iter = iter; end_iter.forward_line(); unicode line_text = buffer_->get_slice(iter, end_iter).c_str(); uint32_t i = 0; for(; i < line_text.length(); ++i) { if(line_text[i] != ' ' && line_text[i] != '\t') { break; } } auto indentation = line_text.slice(nullptr, i); auto replacement_text = _u("{0}import ipdb; ipdb.set_trace();\n").format(indentation); buffer_->insert(iter, replacement_text.encode()); } } } return false; }); apply_settings("text/plain"); scrolled_window_.show_all(); }
void webvtt_text_parser_Feed( webvtt_text_parser_t *p, char *psz_line ) { if( psz_line == NULL ) { if( p->p_cue ) { if( p->pf_cue_done ) p->pf_cue_done( p->priv, p->p_cue ); p->p_cue = NULL; } return; } free(p->reads[0]); p->reads[0] = p->reads[1]; p->reads[1] = p->reads[2]; p->reads[2] = psz_line; /* Lookup keywords */ if( unlikely(p->section == WEBVTT_SECTION_UNDEFINED) ) { if( KeywordMatch( psz_line, "\xEF\xBB\xBFWEBVTT" ) || KeywordMatch( psz_line, "WEBVTT" ) ) { p->section = WEBVTT_SECTION_UNDEFINED; if( p->p_cue ) { if( p->pf_cue_done ) p->pf_cue_done( p->priv, p->p_cue ); p->p_cue = NULL; } return; } else if( KeywordMatch( psz_line, "STYLE" ) ) { p->section = WEBVTT_SECTION_STYLE; forward_line( p, psz_line, true ); return; } else if( KeywordMatch( psz_line, "REGION" ) ) { p->section = WEBVTT_SECTION_REGION; forward_line( p, psz_line, true ); return; } else if( KeywordMatch( psz_line, "NOTE" ) ) { p->section = WEBVTT_SECTION_NOTE; return; } else if( psz_line[0] != 0 ) { p->section = WEBVTT_SECTION_CUES; } } if( likely(p->section == WEBVTT_SECTION_CUES) ) { if( p->p_cue ) { if( psz_line[0] == 0 ) { if( p->p_cue ) { if( p->pf_cue_done ) p->pf_cue_done( p->priv, p->p_cue ); p->p_cue = NULL; } } else { char *psz_merged; if( -1 < asprintf( &psz_merged, "%s\n%s", p->p_cue->psz_text, psz_line ) ) { free( p->p_cue->psz_text ); p->p_cue->psz_text = psz_merged; } return; } } if( p->reads[1] == NULL ) return; const char *psz_split = strstr( p->reads[1], " --> " ); if( psz_split ) { vlc_tick_t i_start, i_stop; if( webvtt_scan_time( p->reads[1], &i_start ) && webvtt_scan_time( psz_split + 5, &i_stop ) && i_start <= i_stop ) { const char *psz_attrs = strchr( psz_split + 5 + 9, ' ' ); p->p_cue = ( p->pf_get_cue ) ? p->pf_get_cue( p->priv ) : NULL; if( p->p_cue ) { p->p_cue->psz_attrs = ( psz_attrs ) ? strdup( psz_attrs ) : NULL; p->p_cue->psz_id = p->reads[0]; p->reads[0] = NULL; p->p_cue->psz_text = p->reads[2]; p->reads[2] = NULL; p->p_cue->i_start = i_start; p->p_cue->i_stop = i_stop; } } } } else if( p->section == WEBVTT_SECTION_STYLE ) { forward_line( p, psz_line, false ); if( psz_line[0] == 0 ) p->section = WEBVTT_SECTION_UNDEFINED; } else if( p->section == WEBVTT_SECTION_REGION ) { forward_line( p, psz_line, false ); if( psz_line[0] == 0 ) /* End of region declaration */ p->section = WEBVTT_SECTION_UNDEFINED; } else if( p->section == WEBVTT_SECTION_NOTE ) { if( psz_line[0] == 0 ) p->section = WEBVTT_SECTION_UNDEFINED; } }
int vedit(const char *filename, const char *saveheader, char *bname) { int ch, foo; int lastcharindent = -1; BOOL firstkey = TRUE; char bakfile[PATHLEN]; int old_rows = t_lines, old_columns = t_columns; sethomefile(bakfile, curuser.userid, UFNAME_EDIT); if ((saveheader || uinfo.mode == EDITPLAN || uinfo.mode == EDITBMWEL) && isfile(bakfile) /* lthuang */ #ifdef GUEST && strcmp(curuser.userid, GUEST) #endif ) { clear(); outs(_msg_edit_8); if (getkey() != '2') mycp(bakfile, filename); } if (!isfile(filename)) unlink(bakfile); if (saveheader) do_article_sig(filename); read_file(filename); top_of_win = firstline; currline = firstline; curr_window_line = 0; currpnt = 0; ansi_mode = FALSE; clear(); display_buffer(); move(curr_window_line, currpnt); while ((ch = getkey()) != EOF) { if (firstkey) { firstkey = FALSE; show_help_msg(); } if (talkrequest) /* lthuang */ { backup_file(bakfile); talkreply(); pressreturn(); ch = CTRL('G'); /* redraw screen */ } else if (msqrequest) /* lthuang */ { msqrequest = FALSE; msq_reply(); #if 0 ch = CTRL('G'); #endif } if (old_rows != t_lines || old_columns != t_columns) { static const char *msg_resized = "[1;34;47m螢幕大小已改變, 按(Ctrl-G)回到頁首![m"; old_rows = t_lines; old_columns = t_columns; top_of_win = firstline; currline = top_of_win; curr_window_line = 0; currpnt = 0; shift = 0; redraw_everything = TRUE; move(t_lines / 2, (t_columns - strlen(msg_resized)) / 2); outs(msg_resized); while (getkey() != CTRL('G')); } else if (ch < 0x100 && isprint2(ch)) { insert_char(ch); lastcharindent = -1; } else switch (ch) { case KEY_UP: if (lastcharindent == -1) lastcharindent = currpnt; if (!currline->prev) { bell(); break; } curr_window_line--; currline = currline->prev; currpnt = (currline->len > lastcharindent) ? lastcharindent : currline->len; break; case KEY_DOWN: if (lastcharindent == -1) lastcharindent = currpnt; if (!currline->next) { bell(); break; } curr_window_line++; currline = currline->next; currpnt = (currline->len > lastcharindent) ? lastcharindent : currline->len; break; default: lastcharindent = -1; switch (ch) { case CTRL('T'): top_of_win = back_line(lastline, b_lines - 2); currline = lastline; curr_window_line = getlineno(); currpnt = 0; redraw_everything = TRUE; break; case CTRL('S'): top_of_win = firstline; currline = top_of_win; curr_window_line = 0; currpnt = 0; redraw_everything = TRUE; break; case '\t': do { insert_char(' '); } while (currpnt & 0x7); break; case CTRL('U'): case CTRL('V'): insert_char(0x1b); break; case CTRL('C'): insert_char(0x1b); insert_char('['); insert_char('m'); break; case KEY_RIGHT: case CTRL('F'): if (currline->len == currpnt) { if (!currline->next) bell(); else { currpnt = 0; curr_window_line++; currline = currline->next; } } else currpnt++; break; case KEY_LEFT: case CTRL('B'): if (currpnt == 0) { if (!currline->prev) bell(); else { currline = currline->prev; currpnt = currline->len; curr_window_line--; } } else currpnt--; break; case CTRL('G'): clear(); redraw_everything = TRUE; break; case CTRL('Z'): vedit_help(); break; case KEY_PGUP: case CTRL('P'): top_of_win = back_line(top_of_win, b_lines - 2); currline = top_of_win; currpnt = 0; curr_window_line = 0; redraw_everything = TRUE; break; case KEY_PGDN: case CTRL('N'): top_of_win = forward_line(top_of_win, b_lines - 2); currline = top_of_win; currpnt = 0; curr_window_line = 0; redraw_everything = TRUE; break; case KEY_HOME: case CTRL('A'): currpnt = 0; break; case KEY_END: case CTRL('E'): currpnt = currline->len; break; case CTRL('R'): #if 0 backup_file(bakfile); #endif msq_reply(); #if 0 redraw_everything = TRUE; /* lthuang */ #endif break; case CTRL('Q'): ansi_mode = TRUE; display_buffer(); pressreturn(); ansi_mode = FALSE; display_buffer(); break; case CTRL('X'): case CTRL('W'): backup_file(bakfile); clear(); foo = write_file(filename, saveheader, bname); if (foo == BACKUP_EDITING) return foo; else if (foo != KEEP_EDITING) { unlink(bakfile); return foo; } redraw_everything = TRUE; /* lthuang */ break; case '\r': case '\n': split(currline, currpnt); /* lthuang: reduce the times of backup */ if (total_num_of_line % 7 == 0) backup_file(bakfile); break; case '\177': case CTRL('H'): if (currpnt == 0) { if (!currline->prev) { bell(); break; } curr_window_line--; currline = currline->prev; currpnt = currline->len; if (*killsp(currline->next->data) == '\0') { delete_line(currline->next); redraw_everything = TRUE; } else { join_currline(); } if (curr_window_line == -1) { curr_window_line = 0; top_of_win = currline; rscroll(); } break; } currpnt--; delete_char(); break; case CTRL('D'): if (currline->len == currpnt) join_currline(); else delete_char(); break; case CTRL('Y'): currpnt = 0; currline->len = 0; delete_currline(); break; case CTRL('K'): if (currline->len == 0) delete_currline(); else if (currline->len == currpnt) join_currline(); else { currline->len = currpnt; currline->data[currpnt] = '\0'; } break; default: break; } break; } if (curr_window_line == -1) { curr_window_line = 0; if (!top_of_win->prev) { indigestion(6); bell(); } else { top_of_win = top_of_win->prev; rscroll(); } } if (curr_window_line == t_lines - 1) { curr_window_line = t_lines - 2; if (!top_of_win->next) { indigestion(7); bell(); } else { top_of_win = top_of_win->next; scroll(); } } /* lthuang: 99/07 */ if (currpnt - shift >= t_columns - 1) { shift = (currpnt / (t_columns - 1)) * (t_columns - 1) - 1; redraw_everything = TRUE; } else if (currpnt - shift < 0) { shift = 0; redraw_everything = TRUE; } if (redraw_everything) { display_buffer(); redraw_everything = FALSE; } else { move(curr_window_line, 0); clrtoeol(); /* lthuang */ vedit_outs(currline->data); } move(curr_window_line, currpnt - shift); /* lthuang: 99/07 */ } if (uinfo.mode == POSTING || uinfo.mode == SMAIL) unlink(filename); return ABORT_EDITING; }
void TE_View::down() { forward_line(1); }