Deepin Linux 下 软开神器Lazarus支持中文变量名的安装方法
**
前言:
**
国产Linux在美国的打压下迎来了春天,但Linux天生软件很少,开发工具更是寒岑,不擅长GUI 的C++、缓慢的python 、Java、庞大的QT ,麻烦的Electron,又大又慢又贵的.Net, 就没有一个能打的。
没想到1999年出生的开发神器Lazarus2终于成熟,它继承了曾经的王者Delphi的血统,新增大量优点:开源、跨平台、原生编译、优雅简洁、Bug极少,最快的编译和运行速度-没有之一,支持几乎所有OS和CPU(包括咱的龙芯),消耗很少的资源,甚至能跑在树梅派之类的小板板上;虽然它用的Free Pascal 语言十分简洁且强壮,但功能一点不少,语法上支持大部分C++的特性,重载操作符/函数/模板/泛型/接口…
它有多简单?菜鸟我只需花一天不和女神聊天就为神器新增“中文变量名”功能,从此告别迷一样的拼音变量名和蹩脚冗长的英文名。本文在Deepin Linux 15.8 和 20.3 均测试无恙!
一、下载
用GUI 工具fpcupdeluxe下载最方便。
在黑终端直接运行 ./fpcupdeluxe-x86_64-linux进入如下界面
设置为中文好看,选择fpc stable 和lazarus 2.2.4版本下载安装。
默认安装在主目录下的fpcupdeluxe中
二、修改源码
感谢网友“秋·风”的神作, 参考后,修改一些bug, 本菜鸟亲自编写的如下2个脚本:
首先是修改编译器fpc , 其次修改ide : lazarus
1. 修改 fpc 编译器 源码
#!/bin/bash
if [ ! -d "./fpcsrc" ];then
echo "./fpcsrc 目录不存在, 请确保有才能执行!"
exit 1
fi
sed -i "4054c\ 'A'..'Z',#\$80..#\$FF :" ./fpcsrc/compiler/scanner.pas
sed -i "4729c\ if (c in ['A'..'Z','a'..'z','_',#\$80..#\$FF]) then " ./fpcsrc/compiler/scanner.pas
sed -i "4822c\ if not ((c in ['_','A'..'Z','a'..'z',#\$80..#\$FF])) then " ./fpcsrc/compiler/scanner.pas
sed -i "5456c\ 'a'..'z', #\$80..#\$FF:" ./fpcsrc/compiler/scanner.pas
echo "修改./fpcsrc/compiler/scanner.pas..."
sed -i "815c\ Alpha = ['A'..'Z', 'a'..'z', '_',#\$80..#\$FF];" ./fpcsrc/rtl/objpas/sysutils/sysstr.inc
echo "修改./fpcsrc/rtl/objpas/sysutils/sysstr.inc..."
sed -i "1321c\ 'a'..'z',#\$80..#\$FF :" ./fpcsrc/compiler/assemble.pas
echo "修改./fpcsrc/compiler/assemble.pas..."
sed -i "96c\ else if not(value_str[i] in ['_','A'..'Z','a'..'z','0'..'9',':',#\$80..#\$FF]) then " ./fpcsrc/compiler/objcutil.pas
echo "修改./fpcsrc/compiler/objcutil.pas..."
sed -i "181c\ if (s='') or not (s[1] in ['A'..'Z','a'..'z','_',#\$80..#\$FF]) then " ./fpcsrc/compiler/options.pas
sed -i "184c\ if not (s[I] in ['A'..'Z','a'..'z','0'..'9','_',#\$80..#\$FF]) then " ./fpcsrc/compiler/options.pas
sed -i "2714c\ while (i<length(fn)) and (fn[i+1] in ['a'..'z','A'..'Z','0'..'9','_','-',#\$80..#\$FF]) do " ./fpcsrc/compiler/options.pas
echo "修改./fpcsrc/compiler/options.pas..."
sed -i "82c\ Result:=fBuf[fPos] in ['_','A'..'Z','a'..'z',#\$80..#\$FF]; " ./fpcsrc/rtl/objpas/classes/parser.inc
sed -i "444c\ '_','A'..'Z','a'..'z',#\$80..#\$FF : HandleAlphaNum; " ./fpcsrc/rtl/objpas/classes/parser.inc
echo "修改./fpcsrc/rtl/objpas/classes/parser.inc..."
sed -i "75c\ Result:=pchar(fLine)[fLinePos] in ['_','A'..'Z','a'..'z',#\$80..#\$FF]; " ./fpcsrc/packages/fcl-res/src/dfmreader.pp
echo "修改./fpcsrc/packages/fcl-res/src/dfmreader.pp..."
echo "增加fpc3.2.2对中文支持,全部完成。 若要laz支持中文,请在laz目录下运行sh laz2cn.sh"
把以上代码保存为 fpc2cn.sh , 放在fpcupdeluxe目录下,运行之:
sh fpc2cn.sh
然后用fpcupdeluxe编译,为了防止它下载新代码覆盖我们刚做的修改,注意在“设置”中勾选“仅重新构建”如下图,然后回到主界面,点击“安装fpc”。
如果手动 make 编译 fpc 会导致”没有3.06版本“错误,因为它用自己旧版编译新版,并不像其他语言一样,要用C语言编译器编译。这样的好处是能充分验证自己编译器,大大减少自己的Bug。
2. 修改 Lazarus 源码
#!/bin/bash
# sed 后如要引用单引号需要用双引号,引用$需要转意用\$, 单引号内的$不需要转意
if [ ! -d "./lazarus" ];then #[空格 布尔条件 空格] 必须要有空格, 分号表示一行多指令, 这里是为了接then
echo "./lazarus 目录不存在, 请确保有才能执行!"
exit 1
fi
# the follow code for linux下Lazarus 2.2.0 GTK2版代码编辑器及SynEdit控件不支持中文输入
sed -i '4a\ {$ifdef linux}' ./lazarus/components/synedit/lazsyngtk2imm.pas
sed -i '5a\ {$define WITH_GTK2_IM} {$define LCLGtk2}' ./lazarus/components/synedit/lazsyngtk2imm.pas
sed -i '6a\ {$endif}' ./lazarus/components/synedit/lazsyngtk2imm.pas
sed -i '12a\ {$ifdef linux}' ./lazarus/lcl/interfaces/gtk2/gtk2globals.pp
sed -i '13a\ {$define WITH_GTK2_IM}' ./lazarus/lcl/interfaces/gtk2/gtk2globals.pp
sed -i '14a\ {$endif}' ./lazarus/lcl/interfaces/gtk2/gtk2globals.pp
sed -i '20a\ {$ifdef linux}' ./lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc
sed -i '21a\ {$define WITH_GTK2_IM}' ./lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc
sed -i '22a\ {$endif}' ./lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc
sed -i '246a\ if ord(str[0])>127 then im_context_use:=true' ./lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc
sed -i '247a\ else if length(str)>1 Then im_context_use:=true;' ./lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc
echo "修复lazarus在linux搜狗词组只能输入第一个字的Bug..."
sed -i '21a\ {$ifdef linux}' ./lazarus/lcl/interfaces/gtk2/gtk2proc.inc
sed -i '22a\ {$define WITH_GTK2_IM}' ./lazarus/lcl/interfaces/gtk2/gtk2proc.inc
sed -i '23a\ {$endif}' ./lazarus/lcl/interfaces/gtk2/gtk2proc.inc
sed -i '21a\ {$ifdef linux}' ./lazarus/components/synedit/synedit.inc
sed -i '22a\ {$define Gtk2IME}' ./lazarus/components/synedit/synedit.inc
sed -i '23a\ {$endif}' ./lazarus/components/synedit/synedit.inc
echo "解决GTK2版代码编辑器及SynEdit控件不支持中文输入..."
sed -i "13c\ unitdir=../../lcl/interfaces/gtk2 ../../packager/units/\$(CPU_TARGET)-\$(OS_TARGET) ../lazutils/lib/\$(CPU_TARGET)-\$(OS_TARGET) ../freetype/lib/\$(CPU_TARGET)-\$(OS_TARGET) ../../lcl/units/\$(CPU_TARGET)-\$(OS_TARGET) ../../lcl/units/\$(CPU_TARGET)-\$(OS_TARGET)/\$(LCL_PLATFORM) . " ./lazarus/components/synedit/Makefile.fpc
#synedit/Makefile.fpc 13行修改加入../../lcl/interfaces/gtk2 到 unitdir= ../../packager/...” 中
# 再执行 fpcmake 帮我们生成新的makefile. 否则编译时会出现找不到gtk2globals
fpcmake ./lazarus/components/synedit/Makefile.fpc
echo "修改./lazarus/components/synedit/Makefile.fpc 生成新的makefile"
# lazarus linux使用StringGrid和DBGrid等控件时,如果内容包含#13时会出现乱码
sed -i '1311a\ i:integer; ' ./lazarus/lcl/interfaces/gtk2/gtk2devicecontext.inc
sed -i '1321a\ for i:=0 to length(AText)-1 do ' ./lazarus/lcl/interfaces/gtk2/gtk2devicecontext.inc
sed -i '1322a\ if ord(AText[i])<32 Then AText[i]:=#32; ' ./lazarus/lcl/interfaces/gtk2/gtk2devicecontext.inc
echo "解决Grid等控件时,如果内容包含#13时会出现乱码..."
# 支持中文标识符
echo "增加支持中文标识符..."
sed -i "851c\ IsIdentStartChar[c]:=c in ['a'..'z','A'..'Z','_',#\$80..#\$FF];" ./lazarus/components/codetools/keywordfunclists.pas
sed -i "852c\ IsIdentChar[c]:=c in ['a'..'z','A'..'Z','_','0'..'9',#\$80..#\$FF];" ./lazarus/components/codetools/keywordfunclists.pas
echo "修改./lazarus/components/codetools/keywordfunclists.pas..."
#sed -i lazarus/lcl/interfaces/gtk2/gtk2winapi.inc
sed -i "1167c\ '_','A'..'Z','a'..'z',#\$80..#\$FF: " ./lazarus/components/codetools/customcodetool.pas
sed -i '1342c\ { #192..#255: ' ./lazarus/components/codetools/customcodetool.pas
sed -i '1365c\ end; } ' ./lazarus/components/codetools/customcodetool.pas
sed -i "1621c\ 'A'..'Z','a'..'z',#\$80..#\$FF: " ./lazarus/components/codetools/customcodetool.pas
echo "修改./lazarus/components/codetools/customcodetool.pas"
sed -i "720c\ if not (Name[i] in ['0'..'9','_','a'..'z','A'..'Z',#\$80..#\$FF]) then " ./lazarus/lcl/lresources.pp
sed -i "1344c\ if (not (c in ['a'..'z','A'..'Z','0'..'9','_',#\$80..#\$FF])) then " ./lazarus/lcl/lresources.pp
sed -i "2039c\ if c in ['a'..'z','A'..'Z','0'..'9','_',#\$80..#\$FF] then " ./lazarus/lcl/lresources.pp
sed -i "2083c\ and (LFMSource[p] in ['a'..'z','A'..'Z','0'..'9','_',#\$80..#\$FF]) do " ./lazarus/lcl/lresources.pp
sed -i "2091c\ and (LFMSource[p] in ['a'..'z','A'..'Z','0'..'9','_',#\$80..#\$FF]) do " ./lazarus/lcl/lresources.pp
sed -i "2099c\ and (LFMSource[p] in ['a'..'z','A'..'Z','0'..'9','_',#\$80..#\$FF]) do " ./lazarus/lcl/lresources.pp
sed -i "5465c\ Result:=fBuf[fPos] in ['_','A'..'Z','a'..'z',#\$80..#\$FF]; " ./lazarus/lcl/lresources.pp
sed -i "5777c\ '_','A'..'Z','a'..'z',#\$80..#\$FF : HandleAlphaNum; " ./lazarus/lcl/lresources.pp
echo "修改./lazarus/lcl/lresources.pp..."
sed -i "1420c\ Alpha = ['A'..'Z', 'a'..'z', '_',#\$80..#\$FF]; " ./lazarus/components/lazutils/lazstringutils.pas
echo "修改./lazarus/components/lazutils/lazstringutils.pas..."
sed -i "4927c (Result[I] in ['a'..'z', 'A'..'Z', '_',#\$80..#\$FF]) or " ./lazarus/components/ideintf/propedits.pp
echo "修改./lazarus/components/ideintf/propedits.pp..."
echo "laz2.2.4修改bug和汉化全部完成。另外需要运行sh fpc2cn.sh修改fpc3.2.2"
该脚本保存为 laz2cn.sh, 终端中进入目录运行之:
cd ~/fpcupdeluxe
sh laz2cn.sh
眨眼就结束,结果如图:
三、编译并测试
重新编译,为了简单,因为fpc已经OK了,我们就直接用命令编译:
cd lazarus
make all
简单吧,哈哈哈!
在我的8年前的联想X250电脑上只需几分钟,编译完成如下图:
完成后,桌面自动生成了快捷图标,美丽的花豹!双击运行。
填入测试代码如下图:
我这里的变量和控件名都用了中文!
用快捷 F9 编译运行如下:
四、后记
自己动手快速为一个大型开发工具新增“中文变量名”支持功能,在以前依赖别人的商品软件时代是不可想象的。我从未见过如此厚颜NB 之物,现在你就见到咯,它就是开源的Lazarus !
现在我正在用它为客户开发一套跨平台的中文软件,速度快得惊人,我可以直接用它写需求分析模型,让用户立马可见所期望的软件模样…。
别人996加班写bug找bug,有了它,我就可自由吃饭游泳打东东!