---
title: "Fcitx4.2英文输入切换的改造"
author: "flytreeleft"
date: "2012-02-20T12:31:58Z"
tags: []
categories: ["开源软件"]
slug: "fcitx4-2"
draft: false
lastmod: "2012-04-23T14:00:24Z"
id: 1412260
url: "https://flytreeleft.iteye.com/blog/1412260"
---

最近Fcitx版本升级到了4.2,由于4.1版本中的英文输入切换考虑的情况太少,其仅在首个输入为大写字母时才切换到英文状态,而对于在中途输入大写字母并不能切换到英文输入.所以,我想看看4.2在这个问题上是否有所改善.于是,便下载/编译并安装试用.

    使用时,发现其还是没有将中途输入大写字母的情况切换到英文输入状态,于是,我又开始尝试修改源代码.

    按照前文&lt;[Fcitx中文状态下的英文输入问题解决方案](/blog/1410471)&gt;的方法,我打开文件"src/module/autoeng/AutoEng.c",并找到函数"ProcessAutoEng",结果发现作者对该函数进行了修改,前文的方法已经不适用了.不过,经过分析和调试,我发现,作者新增了一个函数"SwitchToEng",从名字看出,应该是切换到英文输入的判断方法,其源代码如下:

```
boolean SwitchToEng(FcitxAutoEngState* autoEngState, char *str)
{
    AUTO_ENG*       autoeng;
    for (autoeng = (AUTO_ENG *) utarray_front(autoEngState->autoEng);
            autoeng != NULL;
            autoeng = (AUTO_ENG *) utarray_next(autoEngState->autoEng, autoeng)) {
        printf("switch to english: str-%s,autoeng->str-%s\n", str, autoeng->str);
        if (!strcmp(str, autoeng->str))
            return true;
    }

    return false;
}
```

     其中,printf语句为便于分析该函数作用而写的调试输入,在编译,安装(由于fcitx将在/usr/lib和/usr/local/lib下搜索运行库,故必须进行安装),运行后发现,autoeng-&gt;str输出的为如"https","http:"等,并且在输入开头为这些字符串时,fcitx将自动进入英文状态,这样,可以判断,该函数的确是自动切换英文输入的方法,在返回true时,fcitx进入英文输入状态.

    

    于是,我在for循环之后,增加如下代码:

 

```
int index = autoEngState->index - 1;
char last = autoEngState->buf[index];
if (index > 0 && ('A' <= last && 'Z' >= last)) {
    return true;
}
```

 

     autoEngState-&gt;buf中存放的是当前输入的字符(最后一个为'\\0'),这里,我仅对"中途"输入大写字母的情况进行了判断,如果中途输入了大写字母,则返回true,这样fcitx便自动进入英文输入状态.

    如此,我的目的便轻松地达到了.![](/images/smiles/icon_biggrin.gif)

    如果,这样的判断还是不能满足要求,那就在if语句中再增加判断条件吧!

    Fcitx-4.2.0的源代码及编译所需的pinyin.tar.gz和table.tar.gz已上传到附件中,可以直接下载.所上传代码为原始代码,并未做修改,需要的童鞋,可以直接打开文件"src/module/autoeng/AutoEng.c",并将函数"SwitchToEng"修改为如下代码:

```
boolean SwitchToEng(FcitxAutoEngState* autoEngState, char *str)
{
    AUTO_ENG*       autoeng;
    for (autoeng = (AUTO_ENG *) utarray_front(autoEngState->autoEng);
            autoeng != NULL;
            autoeng = (AUTO_ENG *) utarray_next(autoEngState->autoEng, autoeng)) {
        // 如果输入以"http:","https"等开头,则进入英文输入状态
        if (!strcmp(str, autoeng->str))
            return true;
    }
    // 那再对是否含有大写字母或标点进行判断,是,则进入英文输入状态
    int index = autoEngState->index - 1;
    char last = autoEngState->buf[index];
    if (index > 0 && ('A' <= last && 'Z' >= last)) {
        return true;
    }

    return false;
}
```

      在控制台中输入如下命令进行编译,安装:

```
$ cmake . && make && sudo make install && pkill fcitx && fcitx
```

  

    原始下载地址为: [http://code.google.com/p/fcitx/downloads/list](http://code.google.com/p/fcitx/downloads/list)

 

后记: 现在[4.2.2](http://code.google.com/p/fcitx/downloads/detail?name=fcitx-4.2.2.tar.xz&can=2&q= "Fcitx-4.2.2下载")版已经能够支持首字母非大写的英文输入了,非常感谢作者一直对Fcitx的更新!