[转]为Linux下的Lazarus添加中文输入支持

在Linux环境下,Lazarus不支持中文输入。这是一个臭名昭著的Bug,其根源为Lazarus所使用的SynEdit的问题。
经过一番搜索,我在Lazarus中文社区上找到了一个解决的办法,记录如下。
1. 我是用fpcupdeluxe安装的Lazarus,打开/home/pi/fpcupdeluxe/lazarus/ide/sourceeditor.pp。
2. 在界面下方放下一个TPanel, 里面放下一个TLabel,一个TEdit,适当安排界面,如下图。
3. 在源码里面查找InsertCVSKeyword,会先找到这么一行:
procedure InsertCVSKeyword(const AKeyWord: string);
在这一行下面加上一行:
procedure InsertKeyword(const AKeyWord: string);
 
F3继续找其实现,找到以下代码:
procedure TSourceEditor.InsertCVSKeyword(const AKeyWord: string);
begin
  if ReadOnly then Exit;
 FEditor.InsertTextAtCaret('$'+AKeyWord+'$'+LineEnding);
end;
复制它们,在下面粘贴,改成:
procedure TSourceEditor.InsertKeyword(const AKeyWord: string);
begin
  if ReadOnly then Exit;
  FEditor.InsertTextAtCaret(AKeyWord);
end;
 
这样,就给TSourceEditor增加了一个叫“InsertKeyword”的过程,作用是可以通过编程语句在光标所在位置添加指定字符串。
 
4. 界面上新加的Edit在onKeyPress,输入以下语句:
 
procedure TSourceNotebook.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if key=#13 then
  begin
    GetActiveSE.InsertKeyword(Edit1.text);
    Edit1.Text:='';
    FocusEditor;
  end;
end;   

这样,需要输入汉字时转到下面输入,然后点一下按钮,就自动添到上面源程序光标所在位置,并且将输入焦点转到上面源程序里面。

最后重新编译Lazarus。
在树梅派和银河麒麟arm 64测试通过。

 2021-12-06更新:

更便捷的方法是在需要插入中文的位置按Ctrl+Enter键,修改方法如下:

打开SourceEditor.pp,在TSourceNotebook.FormKeyDown增加下面的代码,然后重新编译lazarus就可以。

 

procedure TSourceNotebook.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var _sIns:String;
begin
  if (ssCtrl in Shift) and (key=13) then
  begin
    key:=0;
    _sIns:=trim(InputBox('输入待插入中文字符','',''));
    if _sIns<>'' then
    begin
      GetActiveSE.InsertKeyword(_sIns);
      FocusEditor;
    end;
  end;
end;

在需要的位置按Ctrl+Enter弹出输入框:

 

原文地址:https://www.cnblogs.com/qiufeng2014/p/15640889.html