📄 htformat.cc
字号:
/* * HT Editor * htformat.cc * * Copyright (C) 1999-2002 Stefan Weyergraf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "htformat.h"#include "htsearch.h"#include <stdlib.h>#include <string.h>#include <time.h>#include <exception>#include "blockop.h"#include "cmds.h"#include "htapp.h" // for popup_view_list(..)#include "atom.h"#include "htclipboard.h"#include "htctrl.h"#include "endianess.h"#include "hteval.h"#include "hthist.h"#include "htiobox.h"#include "keyb.h"#include "htpal.h"#include "httag.h"#include "textedit.h"#include "textfile.h"#include "htprocess.h"#include "snprintf.h"#include "tools.h"extern "C" {#include "evalx.h"#include "regex.h"}/*cmd_rec ht_format_viewer_cmds[] = { {cmd_file_truncate, true, true, NULL}};*/void clear_line_id(LINE_ID *l){ l->id1 = 0; l->id2 = 0; l->id3 = 0; l->id4 = 0; l->id5 = 0;}bool compeq_line_id(const LINE_ID &a, const LINE_ID &b){ return (a.id1 == b.id1 && a.id2 == b.id2 && a.id3 == b.id3 && a.id4 == b.id4 && a.id5 == b.id5);}/* * CLASS ht_data_tagstring */class ht_data_tagstring: public Object {public: char *value; ht_data_tagstring(const char *tagstr = NULL) : value(tag_strdup(tagstr)) { } ~ht_data_tagstring() { free(value); }};/* * CLASS ht_search_request */ht_search_request::ht_search_request(uint _search_class, uint _type, uint _flags){ search_class = _search_class; type = _type; flags = _flags;}/* * CLASS ht_format_group */void ht_format_group::init(Bounds *b, int options, const char *desc, File *f, bool own_f, bool editable_f, format_viewer_if **i, ht_format_group *format_group){ ht_format_viewer::init(b, desc, 0, f, format_group); VIEW_DEBUG_NAME("ht_format_group"); xgroup = new ht_xgroup(); xgroup->init(b, options, desc); xgroup->group = group; format_views = new Array(true); // a list of ht_format_viewer_entrys own_file = own_f; editable_file = editable_f; if (i) init_ifs(i);}void ht_format_group::done(){ done_ifs(); delete format_views; xgroup->done(); delete xgroup; ht_format_viewer::done(); if (own_file) delete file;}int ht_format_group::childcount() const{ return xgroup->childcount();}bool ht_format_group::done_if(format_viewer_if *i, ht_view *v){ remove(v); if (i->done) { i->done(v); } else { v->done(); delete v; } return true;}void ht_format_group::done_ifs(){ int j = 0; while (1) { ht_format_viewer_entry *e=(ht_format_viewer_entry*)(*format_views)[j]; if (!(e && e->instance)) break; done_if(e->interface, e->instance); j++; }}bool ht_format_group::edit(){ return (file->getAccessMode() & IOAM_WRITE);}bool ht_format_group::focus(ht_view *view){ bool r = ht_format_viewer::focus(view); if (!r) r = xgroup->focus(view); return r;}const char *ht_format_group::func(uint i, bool execute){ return ht_format_viewer::func(i, execute);}void ht_format_group::getbounds(Bounds *b){ xgroup->getbounds(b);}void *ht_format_group::get_shared_data(){ return shared_data;}ht_view *ht_format_group::getfirstchild(){ return xgroup->getfirstchild();}ht_view *ht_format_group::getselected(){ return xgroup->getselected();}int ht_format_group::get_pindicator_str(char *buf, int max_len){ ht_view *c = xgroup->current; if (c && (c->options & VO_FORMAT_VIEW)) { return ((ht_format_viewer*)c)->get_pindicator_str(buf, max_len); } else { if (max_len > 0) *buf = 0; return 0; }}bool ht_format_group::get_hscrollbar_pos(int *pstart, int *psize){ ht_view *c = xgroup->current; if (c && (c->options & VO_FORMAT_VIEW)) { return ((ht_format_viewer*)c)->get_hscrollbar_pos(pstart, psize); } return false;}bool ht_format_group::get_vscrollbar_pos(int *pstart, int *psize){ ht_view *c = xgroup->current; if (c && (c->options & VO_FORMAT_VIEW)) { return ((ht_format_viewer*)c)->get_vscrollbar_pos(pstart, psize); } return false;}void ht_format_group::handlemsg(htmsg *msg){ switch (msg->msg) { case msg_keypressed: { int i = 0; switch (msg->data1.integer) { case K_F12: i++; case K_F11: i++; case K_F10: i++; case K_F9: i++; case K_F8: i++; case K_F7: i++; case K_F6: i++; case K_F5: i++; case K_F4: i++; case K_F3: i++; case K_F2: i++; case K_F1: { i++; htmsg m; m.msg = msg_funcquery; m.type = mt_empty; m.data1.integer = i; sendmsg(&m); if (m.msg == msg_retval) { sendmsg(msg_funcexec, i); clearmsg(msg); return; } break; } } break; } } ht_format_viewer::handlemsg(msg); xgroup->handlemsg(msg); switch (msg->msg) { case msg_funcexec: if (func(msg->data1.integer, 1)) { clearmsg(msg); return; } break; case msg_funcquery: { const char *s=func(msg->data1.integer, 0); if (s) { msg->msg=msg_retval; msg->data1.cstr=s; } break; } }}bool ht_format_group::init_if(format_viewer_if *i){ Bounds b; getbounds(&b); b.x=0; b.y=0; bool r=0; ht_view *v=0; if (i->init) { try { v=i->init(&b, file, this); if (v) { v->sendmsg(msg_complete_init, 0); insert(v); r=1; } } catch (const Exception &x) { errorbox("unhandled exception: %y", &x); } catch (const std::exception &x) { errorbox("unhandled exception: %s", x.what()); } catch (...) { errorbox("unhandled exception: unknown"); } } ht_format_viewer_entry *e = new ht_format_viewer_entry(); e->interface = i; e->instance = v; format_views->insert(e); return r;}void ht_format_group::init_ifs(format_viewer_if **ifs){ format_viewer_if **i=ifs; while (*i) { init_if(*i); i++; } ifs=i;}void ht_format_group::insert(ht_view *view){ xgroup->insert(view);}void ht_format_group::move(int rx, int ry){ ht_format_viewer::move(rx, ry); xgroup->move(rx, ry);}void ht_format_group::receivefocus(){ xgroup->receivefocus();}void ht_format_group::redraw(){ xgroup->redraw();}void ht_format_group::releasefocus(){ xgroup->releasefocus();}void ht_format_group::remove(ht_view *view){ xgroup->remove(view);}void ht_format_group::resize(int rw, int rh){ ht_format_viewer::resize(rw, rh); xgroup->resize(rw, rh);}void ht_format_group::setgroup(ht_group *_group){ xgroup->setgroup(_group);}bool ht_format_group::func_handler(eval_scalar *result, char *name, eval_scalarlist *params){ ht_format_viewer *v = dynamic_cast<ht_format_viewer *>(xgroup->current); if (v) { return v->func_handler(result, name, params); } else { return false; }}bool ht_format_group::symbol_handler(eval_scalar *result, char *name){ ht_format_viewer *v = dynamic_cast<ht_format_viewer *>(xgroup->current); if (v) { return v->symbol_handler(result, name); } else { return false; }}/* * CLASS ht_viewer */void ht_viewer::init(Bounds *b, const char *desc, uint c){ ht_view::init(b, VO_OWNBUFFER | VO_BROWSABLE | VO_SELECTABLE | VO_MOVE | VO_RESIZE, desc); caps = c; growmode = MK_GM(GMH_FIT, GMV_FIT);}const char *ht_viewer::func(uint i, bool execute){ return NULL;}void ht_viewer::handlemsg(htmsg *msg){ int i=0; switch (msg->msg) { case msg_keypressed: { switch (msg->data1.integer) { case K_F12: i++; case K_F11: i++; case K_F10: i++; case K_F9: i++; case K_F8: i++; case K_F7: i++; case K_F6: i++; case K_F5: i++; case K_F4: i++; case K_F3: i++; case K_F2: i++; case K_F1: { i++; htmsg m; m.msg = msg_funcquery; m.type = mt_empty; m.data1.integer = i; sendmsg(&m); if (m.msg == msg_retval) { sendmsg(msg_funcexec, i); clearmsg(msg); return; } break; } } break; } case msg_funcexec: if (func(msg->data1.integer, 1)) { clearmsg(msg); return; } break; case msg_funcquery: { const char *s = func(msg->data1.integer, 0); if (s) { msg->msg = msg_retval; msg->data1.cstr = s; } break; } } ht_view::handlemsg(msg);}/* * CLASS ht_format_viewer */void ht_format_viewer::init(Bounds *b, const char *desc, uint caps, File *f, ht_format_group *fg){ ht_viewer::init(b, desc, caps); options |= VO_FORMAT_VIEW; VIEW_DEBUG_NAME("ht_format_viewer"); file = f; format_group = fg; last_search_request = NULL;/* vs_history = new ht_stack(); vs_history->init();*/}void ht_format_viewer::done(){ delete last_search_request; ht_viewer::done();}bool ht_format_viewer::pos_to_offset(viewer_pos pos, FileOfs *ofs){ return false;}void ht_format_viewer::clear_viewer_pos(viewer_pos *p){}bool ht_format_viewer::compeq_viewer_pos(viewer_pos *a, viewer_pos *b){ return false;}bool ht_format_viewer::continue_search(){ if (last_search_request) { ht_search_result *r = NULL; if (last_search_physical) { FileOfs o, no; if (get_current_offset(&o)) { try { if (last_search_request->search_class==SC_PHYSICAL) { if (next_logical_offset(o, &no)) { r=psearch(last_search_request, no, last_search_end_ofs); } } } catch (const Exception &e) { errorbox("error: %y", &e); } } } else { viewer_pos a, na; if (get_current_pos(&a)) { try { if (last_search_request->search_class==SC_VISUAL) { if (next_logical_pos(a, &na)) { r=vsearch(last_search_request, na, last_search_end_pos); } } } catch (const Exception &e) { errorbox("error: %y", &e); } } } if (r) return show_search_result(r); } return false;}bool ht_format_viewer::func_handler(eval_scalar *result, char *name, eval_scalarlist *params){ return false;}bool ht_format_viewer::symbol_handler(eval_scalar *result, char *name){ return false;}bool ht_format_viewer::get_current_offset(FileOfs *ofs){ return false;}bool ht_format_viewer::get_current_pos(viewer_pos *pos){ return false;}bool ht_format_viewer::get_current_real_offset(FileOfs *ofs){ return get_current_offset(ofs);}File *ht_format_viewer::get_file(){ return file;}int ht_format_viewer::get_pindicator_str(char *buf, int max_len){ if (max_len > 0) *buf = 0; return 0;}bool ht_format_viewer::get_hscrollbar_pos(int *pstart, int *psize){ return false;}bool ht_format_viewer::get_vscrollbar_pos(int *pstart, int *psize){ return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -