From 1cf60e7a19e4128e13cc7d926b3cc22c03c7dbd3 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 26 Nov 2002 19:29:39 +0000 Subject: [PATCH] Finish the business of strengthening the sed scripts and beautifying the resultant C++ code. Also results in less bloat. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5723 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/xforms/ChangeLog | 18 ++++ src/frontends/xforms/FormBase.h | 3 +- src/frontends/xforms/Makefile.am | 2 + src/frontends/xforms/fdesign_base.C | 29 ++++++ src/frontends/xforms/fdesign_base.h | 27 ++++++ src/frontends/xforms/forms/Makefile.am | 4 +- src/frontends/xforms/forms/fdfix.sh | 11 +++ src/frontends/xforms/forms/fdfixc.sed | 120 ++++++++++++------------- src/frontends/xforms/forms/fdfixh.sed | 70 +++++++-------- 9 files changed, 180 insertions(+), 104 deletions(-) create mode 100644 src/frontends/xforms/fdesign_base.C create mode 100644 src/frontends/xforms/fdesign_base.h diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 77c3e9f972..4457579fd7 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,21 @@ +2002-11-26 Angus Leeming + + * FormBase.h (FormDB::form): Implement in 1 line not 2. + + * fdesign_base.[Ch]: new files. All fdesign-generated structures will + derive from FD_base, to minimise code duplication. + + * Makefile.am: add new files. + + * forms/Makefile.am (SRCS): move form_wrap.fd to the end of the list. + + * forms/fdfix.sh: add "c_str" to namespace anon in the generated .C + files if required. + + * forms/fdfix[ch].sed: the structs now derive from FD_base, so they + don't need d-tors to clean up the form. + Beautify the sed to produce beautiful looking C++ code. + 2002-11-26 Jean-Marc Lasgouttes * FileDialog.C (save): actually return the result from diff --git a/src/frontends/xforms/FormBase.h b/src/frontends/xforms/FormBase.h index 9fe1f6eba7..9871b5484c 100644 --- a/src/frontends/xforms/FormBase.h +++ b/src/frontends/xforms/FormBase.h @@ -158,8 +158,7 @@ FormDB::FormDB(string const & t, bool allowResize) template FL_FORM * FormDB::form() const { - if (dialog_.get()) return dialog_->form; - return 0; + return dialog_.get() ? dialog_->form : 0; } diff --git a/src/frontends/xforms/Makefile.am b/src/frontends/xforms/Makefile.am index 57cbad72be..4bf9e7a78a 100644 --- a/src/frontends/xforms/Makefile.am +++ b/src/frontends/xforms/Makefile.am @@ -27,6 +27,8 @@ libxforms_la_SOURCES = \ checkedwidgets.h \ combox.C \ combox.h \ + fdesign_base.C \ + fdesign_base.h \ input_validators.C \ input_validators.h \ lyx_gui.C \ diff --git a/src/frontends/xforms/fdesign_base.C b/src/frontends/xforms/fdesign_base.C new file mode 100644 index 0000000000..b84c1f302f --- /dev/null +++ b/src/frontends/xforms/fdesign_base.C @@ -0,0 +1,29 @@ +/** + * \file fdesign_base.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS + */ + +#include + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "fdesign_base.h" +#include FORMS_H_LOCATION + +FD_base::~FD_base() +{ + if (!form) + return; + + if (form->visible) + fl_hide_form(form); + + fl_free_form(form); +} diff --git a/src/frontends/xforms/fdesign_base.h b/src/frontends/xforms/fdesign_base.h new file mode 100644 index 0000000000..77751c51c6 --- /dev/null +++ b/src/frontends/xforms/fdesign_base.h @@ -0,0 +1,27 @@ +// -*- C++ -*- +/** + * \file fdesign_base.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS + */ + +#ifndef FDESIGN_BASE_H +#define FDESIGN_BASE_H + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "forms_fwd.h" + +/// A base class for the fdesign-generated structs +struct FD_base { + virtual ~FD_base(); + FL_FORM * form; +}; + +#endif // FDESIGN_BASE_H diff --git a/src/frontends/xforms/forms/Makefile.am b/src/frontends/xforms/forms/Makefile.am index 1dd0be27fc..68fac49fdf 100644 --- a/src/frontends/xforms/forms/Makefile.am +++ b/src/frontends/xforms/forms/Makefile.am @@ -19,7 +19,6 @@ SRCS = form_aboutlyx.fd \ form_external.fd \ form_filedialog.fd \ form_float.fd \ - form_wrap.fd \ form_forks.fd \ form_graphics.fd \ form_include.fd \ @@ -44,7 +43,8 @@ SRCS = form_aboutlyx.fd \ form_texinfo.fd \ form_thesaurus.fd \ form_toc.fd \ - form_url.fd + form_url.fd \ + form_wrap.fd libfdesign_la_SOURCES = $(SRCS:.fd=.C) $(SRCS:.fd=.h) diff --git a/src/frontends/xforms/forms/fdfix.sh b/src/frontends/xforms/forms/fdfix.sh index 1c10603200..a8c7f51c4a 100644 --- a/src/frontends/xforms/forms/fdfix.sh +++ b/src/frontends/xforms/forms/fdfix.sh @@ -121,6 +121,17 @@ FDFIXC=${DIRNAME}/fdfixc.sed OUTPUT_FILE=${COUT}; INTRO_MESSAGE +# This "c_str" is potentially used many times in many functions +# so add it to the top of the generated file. +grep -E 'fl_add.*".*[|].*"' ${CIN} > /dev/null && + cat - >> ${COUT} <" >> ${COUT} echo "#include \"forms_gettext.h\"" >> ${COUT} echo "#include \"gettext.h\"" >> ${COUT} diff --git a/src/frontends/xforms/forms/fdfixc.sed b/src/frontends/xforms/forms/fdfixc.sed index 5777928020..55dc43d258 100644 --- a/src/frontends/xforms/forms/fdfixc.sed +++ b/src/frontends/xforms/forms/fdfixc.sed @@ -12,97 +12,96 @@ # It contains the instructions that sed requires to manipulate # the .c files produced by fdesign into a form usable by LyX -# Delete the fdesign comment +# Pretty formatting; remove trailing whitespace. +s/[ ]*$// + + +# Delete blank lines and fdesign comments. +/^$/d /generated with fdesign/d +/^\/\*-----/d -# Rename struct FD_form_xxx as FD_xxx -s/FD_form_\(.*\)/FD_\1/ - -# In the struct declaration, we have renamed -# FL_FORM * form_xxx; -# as -# FL_FORM * form; -# so we must do so here too. -# -# Rewrite "fdui->form_xxx" as "fdui->form" -# xxx is followed by ' ', '->' or ')', so use these to flag when xxx ends. -/fdui->form/s/\(fdui->form\)_[^ )-]*/\1/ # Replace "forms.h" by FORMS_H_LOCATION in the #include directives s/#include \"forms\.h\"/#include FORMS_H_LOCATION/ -# Create the destructor. -# When the wrappers are here, delete from here -/\(.*\) *create_form_form/{ -h -s/\(.*\) \*create_form_form[^ ]*/\1::~\1()/p -/\(.*\)::~\(.*\)()/{ -i\ -{\ - if ( form->visible ) fl_hide_form( form );\ - fl_free_form( form );\ -}\ -} -x -} -# When the wrappers are here, delete to here +# Pretty formatting; replace all leading whitespace with a tab +/^[ ]/s/^[ ]*/ / + + +# Rename struct "FD_form_xxx" as "FD_xxx" +s/FD_form_\(.*\)/FD_\1/ + # Rename the function create_form_form_xxx(void) as build_xxx() -s/\(.*\) \*create_form_form_\(.*\)\([(]void[)]\)/\1 * build_\2(void * parent)/ +s/\(.*\) \*create_form_form_\(.*\)\([(]void[)]\)/\ +\ +\1 * build_\2(void * parent)/ -# We use new/delete not malloc/free so change to suit + +# Pretty formatting +s/FL_OBJECT \*obj;/FL_OBJECT * obj;\ +/ + +# Insert a line before each new FL_OBJECT +/obj = /i\ + +# Ditto for fl_bgn +/fl_bgn/i\ + +# Ditto for fl_end +/fl_end/i\ + + +# Rewrite "fdui->form_xxx" as "fdui->form" +# xxx is followed by ' ', '->' or ')', so use these to flag when xxx ends. +/fdui->form/s/\(fdui->form\)_[^ )-]*/\1/ + + +# We use new/delete not malloc/free so change to suit. s/\(\(FD_[^ ]*\) \*fdui =\).*sizeof(\*fdui))/\1 new \2/ + +# We need to store a pointer to the dialog in u_vdata so that the +# callbacks will work. +s/\( fdui->form\)\(.*bgn_form.*\)/\1\2\ +\1->u_vdata = parent;/ + + # For all lines containing "bmtable", # replace "fl_add_button" with "fl_add_bmtable" /bmtable/ s/fl_add_button/fl_add_bmtable/ + # For all lines containing "fl_" and a string _not_ containing |, # replace the string with _(string) /fl_/ s/".[^|]*"/_(&)/ + # For all lines containing "_shortcut" and a string containing |, # replace the string with scex(_(string)) /_shortcut/ s/".*[|].*"/scex(_(&))/ -# gettext will get confused if the string contains a "%" unless the line is -# preceeded immediately by // xgettext:no-c-format -/_(".*[%].*")/i\ - // xgettext:no-c-format # For all lines containing "fl_add" and a string containing | -# do several things. -# Eg -# fdui->counter_zoom = obj = fl_add_counter(FL_NORMAL_COUNTER,1,2,3,4,"Zoom %|#Z"); -# +# change so: +# fdui->counter_zoom = obj = fl_add_counter(...,"Zoom %|#Z"); # becomes -# fdui->counter_zoom = obj; -# { -# // xgettext:no-c-format -# char const * const dummy = _("Zoom %|#Z"); -# fdui->counter_zoom = obj = fl_add_counter(FL_NORMAL_COUNTER,1,2,3,4,idex(dummy)); -# fl_set_button_shortcut(obj,scex(dummy),1); -# } +# c_str = _("Zoom %|#Z"); +# fdui->counter_zoom = obj = fl_add_counter(...,idex(c_str)); +# fl_set_button_shortcut(obj,scex(c_str),1); -/fl_add/{ -/".*[|].*"/{ +/fl_add.*".*[|].*"/s/fdui\(.*\)"\(.*\)".*/c_str = _("\2");\ + fdui\1idex(c_str));\ + fl_set_button_shortcut(obj,scex(c_str),1);/ - s/fdui\(.*\)"\(.*\)".*/ {\ - char const * const dummy = _("\2");\ - fdui\1idex(dummy));\ - fl_set_button_shortcut(obj,scex(dummy),1);\ - }/ - /_(".*[%].*");/ s/\(.*\)\(char const [*]\)/\1\/\/ xgettext:no-c-format\ - \2/ -} -} +# gettext will get confused if the string contains a "%" unless the line is +# preceeded immediately by "// xgettext:no-c-format" +/_(".*[%].*")/i\ + // xgettext:no-c-format -# We need to store a pointer to the dialog in u_vdata so that the -# callbacks will work. -s/\(fdui->form\)\(.*bgn_form.*\)/\1\2\ - \1->u_vdata = parent;/ # Someone got busy and put spaces in after commas but didn't allow for the # autogeneration of the files so their pretty formatting got lost. Not anymore. @@ -113,4 +112,3 @@ s/,\([^ ]\)/, \1/g # while ensuring "...", "..." isn't affected. # s/\("[^"]+,\) \("\)/\1\2/g - diff --git a/src/frontends/xforms/forms/fdfixh.sed b/src/frontends/xforms/forms/fdfixh.sed index 93e80dc24d..62c5e2969a 100644 --- a/src/frontends/xforms/forms/fdfixh.sed +++ b/src/frontends/xforms/forms/fdfixh.sed @@ -12,7 +12,7 @@ # It contains the instructions that sed requires to manipulate # the .h files produced by fdesign into a form usable by LyX -# Remove trailing whitespace. +# Pretty formatting; remove trailing whitespace. s/[ ]*$// @@ -29,13 +29,12 @@ s/[ ]*$// # Immediately after line "#define FD_xxx_h_" that starts off the header file, -# forward declare FL_FORM and FL_OBJECT and append the contents of file -# "extern.tmp". This latter is a sorted, unique list of any function -# declarations. +# #include "fdesign_base.h" and append the contents of file "extern.tmp". +# This latter is a sorted, unique list of any function declarations. /#define FD/{ a\ \ -#include "forms_fwd.h"\ +#include "fdesign_base.h"\ r extern.tmp } @@ -53,61 +52,54 @@ s/extern \(.*\) create_form_form_\(.*\)[(]void[)]/\1 build_\2(void *)/ # Manipulate the structs: # # Rewrite as -# typedef struct { struct FD_xxx { -# FL_FORM *form_xxx; ~FD_xxx(); -# void *vdata; FL_FORM * form; -# char *cdata; FL_OBJECT * some_obj; -# long ldata; ... -# FL_OBJECT *some_obj; }; +# typedef struct { struct FD_xxx : public FD_base { +# FL_FORM *form_xxx; FL_OBJECT * some_obj; +# void *vdata; ... +# char *cdata; }; +# long ldata; +# FL_OBJECT *some_obj; # ... # } FD_xxx; # # This is detailed more closely below -# Delete lines containing: -/typedef struct/d -/vdata/d -/cdata/d -/ldata/d +# We retain only those lines in the struct that start " FL_OBJECT *", +# placing them in the hold space until the end of the struct is reached +# and we can ascertain the struct's name. -# Place all lines containing FL_FORM and FL_OBJECT in the hold space, deleting -# them from the pattern space. +# All other lines are deleted: +/^typedef struct/d +/^ FL_FORM/d +/[vcl]data/d -# For all lines starting with FL_FORM... -/^ FL_FORM/{ - -# 1. Rewrite "FL_FORM *form_xxx;" as "FL_FORM * form; -s/FL_FORM[ ]*\*form.*/FL_FORM * form;/ - -# 2. We know that FL_FORM comes before any FL_OBJECT, so can initialise -# the hold space with it. Delete from the pattern space. -h -d -} # For all lines starting with FL_OBJECT... /^ FL_OBJECT/{ # 1. Perform a little pretty formatting. -s/FL_OBJECT \*\(.*\)/FL_OBJECT * \1/ +s/\(FL_OBJECT \*\)/\1 / # 2. Append to the hold space and delete from the pattern space. -H -d +H; d } + # The struct is ended by "} FD_xxx;", so now's the time to paste back the # contents of the hold space. /} FD_/{ -# 1. Insert an empty line. -i\ -# 2. Rewrite "} FD_xxx;" as "struct FD_xxx {" and append a d-tor. -s/} \(.*\);/struct \1 {\ - ~\1();/ +# 1. Rewrite "} FD_xxx;" as "\nstruct FD_xxx : public FD_base {". +s/} \(.*\);/\ +struct \1 : public FD_base {/ -# 3. Paste the contents of the hold space beneath it. -G +# 2. The hold space contains the FL_OBJECT lines, preceded by a new line. +# To get rid of this new line, we exchange the contents of the hold and +# pattern spaces, remove the new line and then exchange back. +x; s/^\n//; x + +# 3. Paste the contents of the hold space beneath the "struct FD_xxx" line. +# and empty the hold space +G; h; s/.*//; x # 4. Close the struct and append an empty line. a\