Skip to content
Commits on Source (14)
*.zip
*.elc
*.tar
build/
*.tmp
VERSION := $(shell grep 'setq zero-version' zero-framework.el | cut -d'"' -f2)
default: dist
#===============
# multiple file
#===============
check:
emacs -Q --batch -l zero-reload-all.el -f zero-rebuild -l zero-table.el -l zero-table-test.el -f ert-run-tests-batch
zip:
git archive -o zero-el-$(VERSION).zip --prefix=zero/ HEAD
#==========================
# single file distribution
#==========================
dist: dist-check
build:
if [ ! -x ~/.local/bin/pytest ]; then python3 -m pip install --user pytest; fi
~/.local/bin/pytest build.py
./build.py
sed -i "s/PKG_VERSION/$(VERSION)/g" zero.el
dist-check: build
emacs -Q --batch -l ~/.emacs.d/elpa/s-1.11.0/s.el -l zero.el -l zero-panel-test.el -l zero-pinyin-service-test.el -l zero-framework-test.el -l zero-pinyin-test.el -l zero-table.el -l zero-table-test.el -f ert-run-tests-batch
#====================
# other make targets
#====================
install-git-hooks:
rsync -air git-hooks/ .git/hooks/
version:
@echo $(VERSION)
.PHONY: default check zip dist build dist-check install-git-hooks version
zero-el
Copyright (C) 2019 Yuanle Song <sylecn@gmail.com>
ibus-compute-pixel-position function in zero.el is copied from ibus.el.
This function is under GPLv3 license. Copyright (C) 2010-2012 S. Irie
zero-input--ibus-compute-pixel-position function in zero-input.el is copied
from ibus.el. This function is under GPLv3 license. Copyright (C) 2010-2012
S. Irie
* COMMENT -*- mode: org -*-
#+Date: 2019-09-01
Time-stamp: <2019-10-16>
Time-stamp: <2020-04-05>
* zero-el
zero-el provides zero-pinyin, an Emacs pinyin input method for Chinese and
zero-framework, which is an emacs Chinese input method framework.
zero-el provides zero-input-pinyin, an Emacs pinyin input method for Chinese
and zero-input-framework, which is an emacs Chinese input method framework.
* File list
- zero-framework.el
zero framework source code. This provides the framework and user interface
for zero-el.
- zero-panel.el
dbus client to zero-panel service.
- zero-pinyin-service.el
dbus client to zero-pinyin-service.
- zero-pinyin.el
Pinyin input method implemented using zero.el
- zero-quickdial.el
proof of concept of how to create an input method in emacs using minor mode.
- zero-reload-all.el
zero-el development utility.
- zero-table.el
serves as an example of how to use zero framework to create new input
methods.
This branch is created for one-file ELPA release. For more information, please
check [[https://gitlab.emacsos.com/sylecn/zero-el/blob/master/README][README in master branch]]. [[https://gitlab.emacsos.com/sylecn/zero-el/blob/master/ChangeLog][ChangeLog]] is also in master branch.
* introduce to zero-el
https://blog.emacsos.com/zero-el.html
......@@ -44,5 +16,5 @@ https://blog.emacsos.com/zero-el.html
* License
zero-el is under Apache License 2.0
zero--ibus-compute-pixel-position function in zero-framework.el is under
zero-input--ibus-compute-pixel-position function in zero-input.el is under
GPLv3. see NOTICE file.
#!/usr/bin/env python3
# coding=utf-8
"""
build zero.el from zero.el.in and other el files
"""
import logging
logger = logging.getLogger(__name__)
def placeholder_for_file(fn):
"""return placeholder that should be used for filename.
"""
return "INCLUDE_" + fn.replace('-', '_').replace('.', '_').upper()
def test_placeholder_for_file():
assert placeholder_for_file("zero-panel.el") == "INCLUDE_ZERO_PANEL_EL"
assert placeholder_for_file("zero-pinyin-service.el") == (
"INCLUDE_ZERO_PINYIN_SERVICE_EL")
def get_el_file_body(fn):
rows = []
append = False
with open(fn) as f:
for line in f:
if append:
if '(require ' in line:
logger.debug("skipping require call: %s", line)
continue
rows.append(line)
if line.startswith('(provide'):
break
else:
if line.startswith(";;; Code:"):
rows.append(";; body of %s\n" % (fn,))
append = True
return "".join(rows)
def expand_placeholder_for_files(old_content, filenames):
"""replace INCLUDE_FOO_BAR_EL by body of foo-bar.el.
"""
result = old_content
for fn in filenames:
result = result.replace(placeholder_for_file(fn),
get_el_file_body(fn))
return result
def main():
logging.basicConfig(
format='%(asctime)s [%(module)s] %(levelname)-8s %(message)s',
level=logging.INFO)
with open("zero.el.in") as tpl:
content = tpl.read()
expanded_content = expand_placeholder_for_files(content, [
"zero-panel.el",
"zero-framework.el",
"zero-table.el",
"zero-pinyin-service.el",
"zero-pinyin.el",
])
with open('zero.el', 'w') as out:
out.write(expanded_content)
if __name__ == '__main__':
main()
#!/bin/sh
set -e
make dist-check
git add zero.el
* COMMENT -*- mode: org -*-
#+Date: 2019-10-08
Time-stamp: <2019-10-16>
#+STARTUP: content
* notes :entry:
** 2019-04-01 zero.el a Chinese IM framework in emacs; FSM :doc:
title was: how to write a modern im for emacs.
cd ~/lisp/elisp/zero/
- DONE can I implement it as a minor mode? yes.
- DONE can I use panel to show candidates? yes.
- zero minor mode FSM
implemented in zero.el
| Imp | state | action | next state | trigger action |
|-----+------------------+-----------------------------------------------------+------------------+---------------------------------------------------------------------------------------------------------|
| Y | IM_OFF | M-x zero-on or zero-toggle | IM_WAITING_INPUT | turn on minor mode |
| Y | IM_WAITING_INPUT | type M-x zero-off or zero-toggle | IM_OFF | turn off minor mode |
| Y | IM_WAITING_INPUT | type character that can start a sequence | IM_PREEDITING | update preedit str, show candidate list |
| Y | IM_WAITING_INPUT | type character that can not start a sequence | IM_WAITING_INPUT | insert character (full-width aware) |
| Y | IM_WAITING_INPUT | type [,.?!\] | IM_WAITING_INPUT | insert Chinese punctuation character (full-width aware) |
| Y | IM_PREEDITING | type character (that is not SPC, digit keys) | IM_PREEDITING | update preedit str, update and show candidate list |
| Y | IM_PREEDITING | type RET | IM_WAITING_INPUT | commit preedit str (full-width aware), hide candidate list, reset preedit str |
| Y | IM_PREEDITING | type SPC | IM_WAITING_INPUT | commit first candidate or preedit str (full-width aware), reset preedit str |
| Y | IM_PREEDITING | type digit keys | IM_WAITING_INPUT | commit nth candidate if it exists, otherwise, append to preedit str |
| | IM_PREEDITING | type C-g | IM_WAITING_INPUT | reset IM (reset preedit str, hide candidate list) |
| Y | IM_PREEDITING | type M-x zero-off or zero-toggle | IM_OFF | reset IM, turn off minor mode |
| Y | IM_PREEDITING | type <backspace>, when preedit str is longer than 1 | IM_PREEDITING | update preedit str, update and show candidate list |
| Y | IM_PREEDITING | type <backspace>, when preedit str is length 1 | IM_WAITING_INPUT | reset IM |
| Y | IM_PREEDITING | focus in | IM_PREEDITING | show candidat list |
| Y | IM_PREEDITING | focus out | IM_PREEDITING | hide panel |
| Y | IM_PREEDITING | type [,.?!\] | IM_WAITING_INPUT | commit first candidate or preedit str (full-width aware), insert Chinese punctuation (full-width aware) |
| Y | IM_PREEDITING | type -/= | IM_PREEDITING | candiate page up/down |
| | | | | |
in IM_OFF state, zero should not do any preedit try nor do punctuation
translate.
- DONE make zero-quickdial IM work in emacs.
see ~/lisp/elisp/zero/zero-quickdial.el
- DONE make zero-table IM work in emacs. with zero-panel.
see ~/lisp/elisp/zero/zero-table.el
- during development, press F8 to byte-compile and load the current el file.
this will also look for errors in the file.
press F9 to run ert tests.
- TODOs
- whenever a command moves point, IM should probably reset()
I can't remap every possible key/function.
- I need hook for buffer/window focus in/out.
currently, when user switch to another buffer, the panel will still show.
user can click mouse in another emacs window.
whenever focus is moved outside current buffer, I need a hook to run
zero-focus-out.
how to reproduce the problem
=============================
open emacs, split, top window show buffer with file t3, bottom window show
buffer with file t4.
in t3 buffer, press F1 to toggle zero on. type "a", candidate list will
show. now press C-x o to switch to t4 buffer. candidate list didn't go
away. because I can't find a hook for it.
the mouse case:
=================
open emacs, split, top window show buffer with file t3, bottom window show
buffer with file t4.
in t3 buffer, press F1 to toggle zero on. type "a", candidate list will
show. now click mouse on t4 buffer. candidate list didn't go away. because
I can't find a hook for it.
- how to check whether string contains character?
without converting char to string.
(zero-table-can-start-sequence) can use this.
** 2019-10-10 documents
- Using of D-Bus
https://www.gnu.org/software/emacs/manual/html_mono/dbus.html
* later :entry:
* current :entry:
**
** 2019-10-15 make package-lint happy on all el files.
- package-lint doesn't support multi-file package.
Need to add headers to make it happy.
Because it doesn't have concept on multi-file package, it insists all
functions in file start with file name prefix. I can't have zero-* utility
function in zero-panel.el
- M-x package-lint-current-buffer fail on zero.el file
this line:
(eval-when-compile (require 'cl-macs))
M-x package-lint-current-buffer fails on a fresh .el file with just that
line.
#+BEGIN_SRC sh
Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
package-lint--map-symbol-match("(\\s-*?require\\s-*?'cl-\\(?:macs\\|seq\\)\\_>" #f(compiled-function (_) #<bytecode 0x15e4cd9>))
package-lint--check-no-use-of-cl-lib-sublibraries()
package-lint--check-all()
package-lint-buffer()
package-lint-current-buffer()
funcall-interactively(package-lint-current-buffer)
call-interactively(package-lint-current-buffer record nil)
command-execute(package-lint-current-buffer record)
execute-extended-command(nil "package-lint-current-buffer" nil)
funcall-interactively(execute-extended-command nil "package-lint-current-buffer" nil)
call-interactively(execute-extended-command nil nil)
command-execute(execute-extended-command)
#+END_SRC
package-lint--check-no-use-of-cl-lib-sublibraries
'warning
"This file is not in the `cl-lib' ELPA compatibility package: require `cl-lib' instead."
says you should use cl-lib instead. may as well try that.
yes. that works.
- create a single file for distribution.
package-lint doesn't support multiple file pkg yet.
- create a single zero.el file for distribution.
rename current zero.el to zero-framework.el
create zero.el.in
- make build
should generate zero.el and run checks on it.
- insert content to zero.el.in
;;; Code:
;;; .*\.el ends here
- now package-lint should be fine.
- I should I put zero.el in a branch or a separate git repo?
separate git repo is better.
how to auto build and commit on source repo push?
use gocd CI, I can trigger a build. just run git add and git push there?
if run in the same repo, two branch is required.
- maybe just add zero.el in master branch and commit it anyway.
-
* done :entry:
** 2019-10-11 move tests to separated files.
otherwise (require 'zero-pinyin) will fail because (require 'ert) is not in
source code.
** 2019-10-09 release zero-el on melpa
melpa/CONTRIBUTING.org at master · melpa/melpa · GitHub
https://github.com/melpa/melpa/blob/master/CONTRIBUTING.org
Making your package ready for inclusion
https://github.com/melpa/melpa/blob/master/CONTRIBUTING.org#making-your-package-ready-for-inclusion
Packaging Basics - GNU Emacs Lisp Reference Manual
https://www.gnu.org/software/emacs/manual/html_node/elisp/Packaging-Basics.html#Packaging-Basics
name: zero
version: 1.2.2
brief: a Chinese input method framework
long: zero is a Chinese input method framework for Emacs, implemented
as an Emacs minor mode. A zero-pinyin input method is included with zero
distribution.
dependencies: s
progress:
Preparing a pull request to MELPA
https://github.com/melpa/melpa/blob/master/CONTRIBUTING.org#preparing-a-pull-request-to-melpa
- problems
- should I create two pkg, one for zero-framework and one for zero-pinyin?
does melpa require two repo for this?
- minimum s version?
I only use s-contains-p s-join.
checked github
tag 1.2.0 already include these functions.
https://github.com/magnars/s.el/blob/1.2.0/s.el
- Note that there is no way to control the order in which files are
byte-compiled.
// seems my pkg compile just fine.
- how to add autoload magic comments?
https://www.gnu.org/software/emacs/manual/html_node/elisp/Autoload.html
A magic autoload comment (often called an autoload cookie) consists of
‘;;;###autoload’, on a line by itself, just before the real definition of
the function in its autoloadable source file.
add this for some user commands.
zero-set-im
zero-set-default-im
zero-on
zero-toggle
- run package-lint
- Prefix function names with #’ (i.e., the special form function) instead of
just ’ (i.e., the special form quote) to tell the compiler this is a
function reference. E.g., (seq-filter #'evenp list).
I don't think this is necessary in my code.
- do I need to fix all checkdoc problems for my pkg to be accepted by melpa?
search: melpa checkdoc one liner functions docstring
yes. they require all checkdoc problem be fixed.
Add recipe for a.el by dotemacs · Pull Request #4830 · melpa/melpa
https://github.com/melpa/melpa/pull/4830
how to specify minimum emacs version?
;; Package-Requires: ((emacs "24"))
- DONE fix all checkdoc problems.
// except for zero-mode, which I don't know how to fix. all other problems
fixed.
- Disambiguate zero-mode by preceding w/ function,command,variable,option
or symbol. (C-h,f,e,n,p,q) [4 times]
this one is really confusing.
org-mode-map doesn't do this.
magit-mode-map doesn't do this.
- TODO how to add dbus as dependencies?
some emacs build may not have dbus.
(require 'dbus)
also how to only allow install on linux? when I test it dbus only worked
in linux.
what's the correct syntax for specifying dbus as dependency?
(dbus "1.0")
didn't work.
- search: package-install-file package--description-file: Wrong type argument: stringp, nil
search: emacs package-install-file install tar package--description-file: Wrong type argument: stringp, nil
install from dir is okay.
install from tar is not. Not sure what's wrong.
** 2019-10-08 support full-width characters and symbols.
全角 半角
- feature requests
- when zero-mode is on, and in full-width mode
- commit English character and symbol should commit full-width character
if there is a full-width character. This should happen before checking
punctuation mapping. if a map is found, punctuation mapping is ignored.
- type letters should still go into preedit str, only commit full-width
char when user press RET.
- when zero-mode is on, and in half-width mode (the default)
- do what it does now. punctuation mapping is checked.
- implementation
- Block Halfwidth and Fullwidth Forms – Codepoints
https://codepoints.net/halfwidth_and_fullwidth_forms
- how to support full width character and symbol in zero?
update fsm table.
add a zero-full-width-mode variable
add default binding shift+space, M-x zero-toggle-full-width-mode
use modeline LIGHTER to show full/half width mode.
when in full-width mode, show ZeroF.
FSM table:
move from gtk-im-module-zero operational file.
- updated FSM.
so it's only a few changes.
- when insert character that can't start a sequence, insert char should be
full-width aware.
zero-self-insert-command
DONE zero-commit-preedit-str
DONE zero-convert-punctuation
- when commit preedit str, insert should be full-width aware.
- when insert Chinese punctuation character, insert should be full-width aware.
- problems
- where is the GB standard file?
- docs
GB/T 15834―2011 标点符号用法 电子版
http://people.ubuntu.com/~happyaron/l10n/GB(T)15834-2011.html
doesn't have much about full width vs half width.
- indeed double quotation mark is different from fullwidth quotation mark.
LEFT DOUBLE QUOTATION MARK “”
FULLWIDTH QUOTATION MARK ""
- search U+FF02
U+FF02 FULLWIDTH QUOTATION MARK – Codepoints
https://codepoints.net/U+FF02
Block Halfwidth and Fullwidth Forms – Codepoints
https://codepoints.net/halfwidth_and_fullwidth_forms
- why bind S-SPC key in zero-mode-map didn't work.
global-set-key does work.
shift-space is always translated to space in minor mode?
shift is not registered as key prefix.
I will just use another key binding for this command.
try M-space. nope.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Keymaps-and-Minor-Modes.html#Keymaps-and-Minor-Modes
Minor modes may bind commands to key sequences consisting of C-c followed
by a punctuation character. However, sequences consisting of C-c followed
by one of {}<>:;, or a control character or digit, are reserved for major
modes. Also, C-c letter is reserved for users. See Key Binding
Conventions.
try use C-c , , and C-c , .
how to add C-c , as prefix key and add binding for those two commands?
it works.
- elisp how to convert unicode hex to char?
1. Non-ASCII Characters
https://ftp.gnu.org/old-gnu/Manuals/elisp-manual-21-2.8/html_chapter/elisp_33.html#SEC541
FF01 to FF65 only 65 chars. I will just type it.
"\uff01" !
char to code point:
(split-char ?\)(unicode 0 255 1)
# FF01
(split-char ?\!)(unicode 0 255 1)
(insert (make-char 'unicode 0 255 1))
(split-char ?\!)(ascii 33)
# FF5A
(split-char ?\z)(unicode 0 255 90)
(insert (make-char 'unicode 0 255 90))
(split-char ?\z)(ascii 122)
# FF65
(split-char ?\・)(unicode 0 255 101)
(insert (make-char 'unicode 0 255 101))
ascii [33, 122]
map to
unicode 0 255 [1, 101].
then pick a few others if they are of importance.
- commit preedit str in full-width mode didn't work.
it still commit half-width str.
is zero-commit-preedit-str called at all? there is no debug message.
that is for commit preedit-str when there is no candidate.
when I press RET, the code is in zero-return.
- type = will insert both full-width char and half-width char.
fixed.
* wontfix :entry:
;;; zero-framework-test.el --- tests for zero-framework.el -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; tests for zero-framework.el
;;; Code:
(require 'zero-framework)
(require 'ert)
(ert-deftest zero-cycle-list ()
(should (= (zero-cycle-list '(1 2 3) 1) 2))
(should (eq (zero-cycle-list '(a b c) 'a) 'b))
(should (eq (zero-cycle-list '(a b c) 'b) 'c))
(should (eq (zero-cycle-list '(a b c) 'c) 'a))
(should (eq (zero-cycle-list '(a b c) 'd) nil)))
(ert-deftest zero-convert-ch-to-full-width ()
(should (= (zero-convert-ch-to-full-width ?\!) ?\!)))
(ert-deftest zero-convert-str-to-full-width ()
(should (string-equal "!" (zero-convert-str-to-full-width "!")))
(should (string-equal "(" (zero-convert-str-to-full-width "(")))
(should (string-equal "(:)" (zero-convert-str-to-full-width "(:)")))
(should (string-equal "ABab" (zero-convert-str-to-full-width "ABab")))
(should (string-equal "hehe" (zero-convert-str-to-full-width "hehe")))
(should (string-equal "(A)" (zero-convert-str-to-full-width "(A)"))))
(provide 'zero-framework-test)
;;; zero-framework-test.el ends here
This diff is collapsed.
This diff is collapsed.
;;; zero-panel-test.el --- tests for zero-panel.el -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;
;;; Code:
(require 'ert)
(require 'zero-panel)
(ert-deftest zero-alist-to-asv ()
(should (equal (zero-alist-to-asv nil) '(:array :signature "{sv}")))
(should (equal (zero-alist-to-asv
'(("name" "foo")
("timeout" :int32 10)))
'(:array
(:dict-entry "name" (:variant "foo"))
(:dict-entry "timeout" (:variant :int32 10))))))
(provide 'zero-panel-test)
;;; zero-panel-test.el ends here
;;; zero-panel --- Provide emacs interface for zero-panel dbus service. -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; use dbus to communicate with zero-panel service.
;;; Code:
;;================
;; implementation
;;================
(require 'dbus)
(require 's)
(defun zero-panel-error-handler (event error)
"Handle dbus errors.
EVENT and ERROR are error-handler arguments."
(when (or (string-equal "com.emacsos.zero.Panel"
(dbus-event-interface-name event))
(s-contains-p "com.emacsos.zero.Panel" (cadr error)))
(error "Zero-panel dbus failed: %S" (cadr error))))
(add-hook 'dbus-event-error-functions 'zero-panel-error-handler)
(defun zero-panel-async-call (method _handler &rest args)
"Call METHOD on zero-panel service asynchronously.
This is a wrapper around `dbus-call-method-asynchronously'.
ARGS optional extra args to pass to the wrapped function."
(apply 'dbus-call-method-asynchronously
:session
"com.emacsos.zero.Panel1" ; well known name
"/com/emacsos/zero/Panel1" ; object path
"com.emacsos.zero.Panel1.PanelInterface" ; interface name
method nil :timeout 500 args))
;;=========================
;; public utility function
;;=========================
(defun zero-alist-to-asv (hints)
"Convert Lisp alist to dbus a{sv} data structure.
HINTS should be an alist of form '((k1 [v1type] v1) (k2 [v2type] v2)).
For example,
\(zero-alist-to-asv
'((\"name\" \"foo\")
(\"timeout\" :int32 10)))
=>
'(:array
(:dict-entry \"name\" (:variant \"foo\"))
(:dict-entry \"timeout\" (:variant :int32 10)))"
(if (null hints)
'(:array :signature "{sv}")
(let ((result '(:array)))
(dolist (item hints)
(push (list :dict-entry (car item) (cons :variant (cdr item))) result))
(reverse result))))
;;============
;; public API
;;============
(defun zero-panel-move (x y)
"Move panel to specific coordinate (X, Y).
Origin (0, 0) is at screen top left corner."
(zero-panel-async-call "Move" nil :int32 x :int32 y))
(defun zero-panel-show-candidates (preedit_str candidate_length candidates &optional hints)
"Show CANDIDATES.
Argument PREEDIT_STR the preedit string.
Argument CANDIDATE_LENGTH how many candidates are in candidates list."
(zero-panel-async-call "ShowCandidates" nil
:string preedit_str
:uint32 candidate_length
(or candidates '(:array))
(zero-alist-to-asv hints)))
(defun zero-panel-show ()
"Show panel."
(zero-panel-async-call "Show" nil))
(defun zero-panel-hide ()
"Hide panel."
(zero-panel-async-call "Hide" nil))
(defun zero-panel-quit ()
"Quit panel application."
(interactive)
(zero-panel-async-call "Quit" nil))
(provide 'zero-panel)
;;; zero-panel.el ends here
;;; zero-pinyin-service-test.el --- tests for zero-pinyin-service.el -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;
;;; Code:
(require 'zero-pinyin-service)
(require 'ert)
(eval-when-compile (require 'cl-macs))
(ert-deftest zero-pinyin-candidate-pinyin-indices-to-dbus-format ()
(should (equal (zero-pinyin-candidate-pinyin-indices-to-dbus-format '((22 31)))
'(:array (:struct :int32 22 :int32 31))))
(should (equal (zero-pinyin-candidate-pinyin-indices-to-dbus-format
'((17 46) (7 55)))
'(:array (:struct :int32 17 :int32 46)
(:struct :int32 7 :int32 55)))))
(ert-deftest zero-pinyin-service-get-candidates ()
(cl-destructuring-bind (cs ls &rest rest)
(zero-pinyin-service-get-candidates "liyifeng" 1)
(should (or (and (equal (car cs) "李易峰")
(= (car ls) 8))
(and (equal (car cs) "利益")
(= (car ls) 4)))))
(cl-destructuring-bind (cs ls &rest rest)
(zero-pinyin-service-get-candidates "wenti" 1)
(should (equal (car cs) "问题"))
(should (= (car ls) 5)))
(cl-destructuring-bind (cs ls &rest rest)
(zero-pinyin-service-get-candidates "meiyou" 1)
(should (equal (car cs) "没有"))
(should (= (car ls) 6)))
(cl-destructuring-bind (cs ls &rest rest)
(zero-pinyin-service-get-candidates "shi" 1)
(should (equal (car cs) "是"))
(should (= (car ls) 3)))
(cl-destructuring-bind (cs ls &rest rest)
(zero-pinyin-service-get-candidates "de" 1)
(should (equal (car cs) "的"))
(should (= (car ls) 2))))
(provide 'zero-pinyin-service-test)
;;; zero-pinyin-service-test.el ends here
;;; zero-pinyin-service.el --- Provide emacs interface for zero-pinyin-service dbus service. -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;; Code:
;;================
;; implementation
;;================
(require 'dbus)
(require 's)
(defun zero-pinyin-service-error-handler (event error)
"Handle dbus errors.
EVENT, ERROR are arguments passed to the handler."
(when (or (string-equal "com.emacsos.zero.ZeroPinyinService1"
(dbus-event-interface-name event))
(s-contains-p "com.emacsos.zero.ZeroPinyinService1" (cadr error)))
(error "`zero-pinyin-service' dbus failed: %S" (cadr error))))
(add-hook 'dbus-event-error-functions 'zero-pinyin-service-error-handler)
(defun zero-pinyin-service-async-call (method handler &rest args)
"Call METHOD on zero-pinin-service asynchronously.
This is a wrapper around `dbus-call-method-asynchronously'.
Argument HANDLER the handler function.
Optional argument ARGS extra arguments to pass to the wrapped function."
(apply 'dbus-call-method-asynchronously
:session "com.emacsos.zero.ZeroPinyinService1"
"/com/emacsos/zero/ZeroPinyinService1"
"com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"
method handler :timeout 1000 args))
(defun zero-pinyin-service-call (method &rest args)
"Call METHOD on zero-pinin-service synchronously.
This is a wrapper around `dbus-call-method'.
Optional argument ARGS extra arguments to pass to the wrapped function."
(apply 'dbus-call-method
:session "com.emacsos.zero.ZeroPinyinService1"
"/com/emacsos/zero/ZeroPinyinService1"
"com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"
method :timeout 1000 args))
;;============
;; public API
;;============
(defun zero-pinyin-service-get-candidates (preedit-str fetch-size)
"Get candidates for pinyin in PREEDIT-STR synchronously.
preedit-str the preedit-str, should be pure pinyin string
FETCH-SIZE try to fetch this many candidates or more"
(zero-pinyin-service-call "GetCandidates" :string preedit-str :uint32 fetch-size))
(defun zero-pinyin-service-get-candidates-async (preedit-str fetch-size get-candidates-complete)
"Get candidates for pinyin in PREEDIT-STR asynchronously.
PREEDIT-STR the preedit string, should be pure pinyin string.
FETCH-SIZE try to fetch this many candidates or more.
GET-CANDIDATES-COMPLETE the async handler function."
(zero-pinyin-service-async-call
"GetCandidates" get-candidates-complete :string preedit-str :uint32 fetch-size))
(defun zero-pinyin-candidate-pinyin-indices-to-dbus-format (candidate_pinyin_indices)
"Convert CANDIDATE_PINYIN_INDICES to Emacs dbus format."
(let (result)
(push :array result)
;; (push :signature result)
;; (push "(ii)" result)
(dolist (pypair candidate_pinyin_indices)
(push (list :struct :int32 (cl-first pypair) :int32 (cl-second pypair))
result))
(reverse result)))
(defun zero-pinyin-service-commit-candidate-async (candidate candidate_pinyin_indices)
"Commit candidate asynchronously.
CANDIDATE the candidate user selected.
CANDIDATE_PINYIN_INDICES the candidate's pinyin shengmu and yunmu index."
;; don't care about the result, so no callback.
(zero-pinyin-service-async-call
"CommitCandidate" nil
:string candidate
(zero-pinyin-candidate-pinyin-indices-to-dbus-format candidate_pinyin_indices)))
(defun zero-pinyin-service-delete-candidates-async (candidate delete-candidate-complete)
"Delete CANDIDATE asynchronously.
DELETE-CANDIDATE-COMPLETE the async handler function."
(zero-pinyin-service-async-call
"DeleteCandidate" delete-candidate-complete :string candidate))
(defun zero-pinyin-service-quit ()
"Quit panel application."
(zero-pinyin-service-async-call "Quit" nil))
(provide 'zero-pinyin-service)
;;; zero-pinyin-service.el ends here
;;; zero-pinyin-test.el --- tests for zero-pinyin.el
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;
;;; Code:
(require 'zero-pinyin)
(require 'ert)
(ert-deftest zero-pinyin-can-start-sequence ()
(should (zero-pinyin-can-start-sequence ?a))
(should (zero-pinyin-can-start-sequence ?l))
(should (zero-pinyin-can-start-sequence ?m))
(should (zero-pinyin-can-start-sequence ?z))
(should-not (zero-pinyin-can-start-sequence ?1))
(should-not (zero-pinyin-can-start-sequence ?.))
(should-not (zero-pinyin-can-start-sequence ?i))
(should-not (zero-pinyin-can-start-sequence ?u))
(should-not (zero-pinyin-can-start-sequence ?v)))
(provide 'zero-pinyin-test)
;;; zero-pinyin-test.el ends here
;;; zero-pinyin.el --- A pinyin input method for zero-framework -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; To use this input method, add in Emacs init file:
;;
;; (add-to-list 'load-path "~/fromsource/zero") ;; omit if install from melpa
;; (require 'zero-pinyin)
;; (zero-set-default-im 'pinyin)
;; ;; Now you may bind a key to zero-toggle to make it easy to
;; ;; switch on/off the input method.
;; (global-set-key (kbd "<f5>") 'zero-toggle)
;;; Code:
;;==============
;; dependencies
;;==============
(require 'zero-framework)
(require 'zero-pinyin-service)
;;===============================
;; basic data and emacs facility
;;===============================
(defvar zero-pinyin-state nil "Zero-pinyin internal state. could be nil or `*zero-pinyin-state-im-partial-commit*'.")
(defconst zero-pinyin--state-im-partial-commit 'IM-PARTIAL-COMMIT)
(defvar zero-pinyin-used-preedit-str-lengths nil
"Accompany `zero-candidates', marks how many preedit-str chars are used for each candidate.")
(defvar zero-pinyin-candidates-pinyin-indices nil
"Store GetCandidates dbus method candidates_pinyin_indices field.")
(defvar zero-pinyin-pending-str "")
(defvar zero-pinyin-pending-preedit-str "")
(defvar zero-pinyin-pending-pinyin-indices nil
"Stores `zero-pinyin-pending-str' corresponds pinyin indices.")
;;=====================
;; key logic functions
;;=====================
(defun zero-pinyin-reset ()
"Reset states."
(setq zero-pinyin-state nil)
(setq zero-pinyin-used-preedit-str-lengths nil)
(setq zero-pinyin-pending-str "")
(setq zero-pinyin-pending-preedit-str ""))
(defun zero-pinyin-init ()
"Called when this im is turned on."
(make-local-variable 'zero-pinyin-state)
(zero-pinyin-reset))
(defun zero-pinyin-preedit-start ()
"Called when enter `*zero-state-im-preediting*' state."
(define-key zero-mode-map [remap digit-argument] 'zero-digit-argument))
(defun zero-pinyin-preedit-end ()
"Called when leave `*zero-state-im-preediting*' state."
(define-key zero-mode-map [remap digit-argument] nil))
(defun zero-pinyin-shutdown ()
"Called when this im is turned off."
(define-key zero-mode-map [remap digit-argument] nil))
(defvar zero-pinyin--build-candidates-use-test-data nil
"If t, `zero-pinyin-build-candidates' will use `zero-pinyin-build-candidates-test'.")
(defun zero-pinyin-build-candidates (preedit-str fetch-size)
"Synchronously build candidates list.
PREEDIT-STR the preedit string.
FETCH-SIZE fetch at least this many candidates if possible."
(if zero-pinyin--build-candidates-use-test-data
(progn
(zero-pinyin-build-candidates-test preedit-str)
(setq zero-fetch-size (max fetch-size (length zero-candidates))))
(zero-debug "zero-pinyin building candidate list synchronously\n")
(let ((result (zero-pinyin-service-get-candidates preedit-str fetch-size)))
(setq zero-fetch-size (max fetch-size (length (cl-first result))))
(setq zero-pinyin-used-preedit-str-lengths (cl-second result))
(setq zero-pinyin-candidates-pinyin-indices (cl-third result))
(cl-first result))))
(defun zero-pinyin-build-candidates-async (preedit-str fetch-size complete-func)
"Asynchronously build candidate list, when done call complete-func on it.
PREEDIT-STR the preedit string.
FETCH-SIZE fetch at least this many candidates if possible.
COMPLETE-FUNC the callback function when async call completes. it's called with
fetched candidates list as parameter."
(zero-debug "zero-pinyin building candidate list asynchronously\n")
(zero-pinyin-service-get-candidates-async
preedit-str
fetch-size
(lambda (candidates matched_preedit_str_lengths candidates_pinyin_indices)
(setq zero-pinyin-used-preedit-str-lengths matched_preedit_str_lengths)
(setq zero-pinyin-candidates-pinyin-indices candidates_pinyin_indices)
(setq zero-fetch-size (max fetch-size (length candidates)))
;; Note: with dynamic binding, this command result in (void-variable
;; complete-func) error.
(funcall complete-func candidates))))
(defun zero-pinyin-can-start-sequence (ch)
"Return t if char CH can start a preedit sequence."
(and (>= ch ?a)
(<= ch ?z)
(not (= ch ?i))
(not (= ch ?u))
(not (= ch ?v))))
(defun zero-pinyin-pending-preedit-str-changed ()
"Update zero states when pending preedit string changed."
(setq zero-fetch-size 0)
(setq zero-current-page 0)
(zero-pinyin-build-candidates-async zero-pinyin-pending-preedit-str zero-initial-fetch-size 'zero-build-candidates-complete))
(defun zero-pinyin-commit-nth-candidate (n)
"Commit Nth candidate and return true if it exists, otherwise, return false."
(let* ((n-prime (+ n (* zero-candidates-per-page zero-current-page)))
(candidate (nth n-prime zero-candidates))
(used-len (when candidate
(nth n-prime zero-pinyin-used-preedit-str-lengths))))
(when candidate
(zero-debug
"zero-pinyin-commit-nth-candidate\n n=%s candidate=%s used-len=%s zero-pinyin-pending-preedit-str=%S\n"
n candidate used-len zero-pinyin-pending-preedit-str)
(cond
((null zero-pinyin-state)
(if (= used-len (length zero-preedit-str))
(progn
(zero-debug "commit in full\n")
(zero-set-state zero--state-im-waiting-input)
(zero-commit-text candidate)
(zero-pinyin-service-commit-candidate-async
candidate
(nth n-prime zero-pinyin-candidates-pinyin-indices))
t)
(zero-debug "partial commit, in partial commit mode now.\n")
(setq zero-pinyin-state zero-pinyin--state-im-partial-commit)
(setq zero-pinyin-pending-str candidate)
(setq zero-pinyin-pending-preedit-str (substring zero-preedit-str used-len))
(setq zero-pinyin-pending-pinyin-indices
(nth n-prime zero-pinyin-candidates-pinyin-indices))
(zero-pinyin-pending-preedit-str-changed)
t))
((eq zero-pinyin-state zero-pinyin--state-im-partial-commit)
(if (= used-len (length zero-pinyin-pending-preedit-str))
(progn
(zero-debug "finishes partial commit\n")
(setq zero-pinyin-state nil)
(zero-set-state zero--state-im-waiting-input)
(zero-commit-text (concat zero-pinyin-pending-str candidate))
(zero-pinyin-service-commit-candidate-async
(concat zero-pinyin-pending-str candidate)
(append zero-pinyin-pending-pinyin-indices
(nth n-prime zero-pinyin-candidates-pinyin-indices)))
t)
(zero-debug "continue partial commit\n")
(setq zero-pinyin-pending-str (concat zero-pinyin-pending-str candidate))
(setq zero-pinyin-pending-preedit-str (substring zero-pinyin-pending-preedit-str used-len))
(setq zero-pinyin-pending-pinyin-indices
(append zero-pinyin-pending-pinyin-indices
(nth n-prime zero-pinyin-candidates-pinyin-indices)))
(zero-pinyin-service-commit-candidate-async
zero-pinyin-pending-str
zero-pinyin-pending-pinyin-indices)
(zero-pinyin-pending-preedit-str-changed)
t))
(t (error "Unexpected zero-pinyin-state: %s" zero-pinyin-state))))))
(defun zero-pinyin-commit-first-candidate-or-preedit-str ()
"Commit first candidate if there is one, otherwise, commit preedit string."
(unless (zero-pinyin-commit-nth-candidate 0)
(zero-commit-preedit-str)))
(defun zero-pinyin-commit-first-candidate-in-full ()
"Commit first candidate and return t if it consumes all preedit-str.
Otherwise, just return nil."
(let ((candidate (nth 0 (zero-candidates-on-page zero-candidates)))
(used-len (nth (* zero-candidates-per-page zero-current-page) zero-pinyin-used-preedit-str-lengths)))
(when candidate
(cond
((null zero-pinyin-state)
(when (= used-len (length zero-preedit-str))
(zero-set-state zero--state-im-waiting-input)
(zero-commit-text candidate)
t))
((eq zero-pinyin-state zero-pinyin--state-im-partial-commit)
(when (= used-len (length zero-pinyin-pending-preedit-str))
(setq zero-pinyin-state nil)
(zero-set-state zero--state-im-waiting-input)
(zero-commit-text (concat zero-pinyin-pending-str candidate))
t))
(t (error "Unexpected zero-pinyin-state: %s" zero-pinyin-state))))))
(defun zero-pinyin-page-down ()
"Handle page down for zero-pinyin.
This is different from zero-framework because I need to support partial commit"
(let ((len (length zero-candidates))
(new-fetch-size (* zero-candidates-per-page (+ 2 zero-current-page))))
(if (and (< len new-fetch-size)
(< zero-fetch-size new-fetch-size))
(let ((preedit-str (if (eq zero-pinyin-state zero-pinyin--state-im-partial-commit) zero-pinyin-pending-preedit-str zero-preedit-str)))
(zero-pinyin-build-candidates-async
preedit-str
new-fetch-size
(lambda (candidates)
(zero-build-candidates-complete candidates)
(zero-just-page-down))))
(zero-just-page-down))))
(defun zero-pinyin-handle-preedit-char (ch)
"Hanlde character insert in `*zero-state-im-preediting*' state.
Override `zero-handle-preedit-char-default'.
CH the character user typed."
(cond
((= ch ?\s)
(zero-pinyin-commit-first-candidate-or-preedit-str))
((and (>= ch ?0) (<= ch ?9))
;; 1 commit the 0th candidate
;; 2 commit the 1st candidate
;; ...
;; 0 commit the 9th candidate
(unless (zero-pinyin-commit-nth-candidate (mod (- (- ch ?0) 1) 10))
(zero-append-char-to-preedit-str ch)
(setq zero-pinyin-state nil)))
((= ch zero-previous-page-key)
(zero-handle-preedit-char-default ch))
((= ch zero-next-page-key)
(zero-pinyin-page-down))
(t (let ((str (zero-convert-punctuation ch)))
(if str
(when (zero-pinyin-commit-first-candidate-in-full)
(zero-set-state zero--state-im-waiting-input)
(insert str))
(setq zero-pinyin-state nil)
(zero-append-char-to-preedit-str ch))))))
(defun zero-pinyin-get-preedit-str-for-panel ()
"Return the preedit string that should show in panel."
(if (eq zero-pinyin-state zero-pinyin--state-im-partial-commit)
(concat zero-pinyin-pending-str zero-pinyin-pending-preedit-str)
zero-preedit-str))
(defun zero-pinyin-preedit-str-changed ()
"Start over for candidate selection process."
(setq zero-pinyin-state nil)
(zero-preedit-str-changed))
(defun zero-pinyin-backspace ()
"Handle backspace key in `*zero-state-im-preediting*' state."
(if (eq zero-pinyin-state zero-pinyin--state-im-partial-commit)
(zero-pinyin-preedit-str-changed)
(zero-backspace-default)))
(defun zero-pinyin-delete-candidate (digit)
"Tell backend to delete candidate at DIGIT position.
DIGIT is the digit key used to select nth candidate.
DIGIT 1 means delete 1st candidate.
DIGIT 2 means delete 2st candidate.
...
DIGIT 0 means delete 10th candidate."
(let ((candidate (nth (mod (- digit 1) 10)
(zero-candidates-on-page zero-candidates))))
(when candidate
(zero-pinyin-service-delete-candidates-async
candidate 'zero-pinyin-preedit-str-changed))))
(defun zero-digit-argument ()
"Allow C-<digit> to DeleteCandidate in `*zero-state-im-preediting*' state."
(interactive)
(unless (eq zero-state zero--state-im-preediting)
(error "`zero-digit-argument' called in non preediting state"))
(if (memq 'control (event-modifiers last-command-event))
(let* ((char (if (integerp last-command-event)
last-command-event
(get last-command-event 'ascii-character)))
(digit (- (logand char ?\177) ?0)))
(zero-pinyin-delete-candidate digit))))
;;===============================
;; register IM to zero framework
;;===============================
(zero-register-im
'pinyin
'((:build-candidates . zero-pinyin-build-candidates)
;; comment to use sync version, uncomment to use async version.
;; (:build-candidates-async . zero-pinyin-build-candidates-async)
(:can-start-sequence . zero-pinyin-can-start-sequence)
(:handle-preedit-char . zero-pinyin-handle-preedit-char)
(:get-preedit-str-for-panel . zero-pinyin-get-preedit-str-for-panel)
(:handle-backspace . zero-pinyin-backspace)
(:init . zero-pinyin-init)
(:shutdown . zero-pinyin-shutdown)
(:preedit-start . zero-pinyin-preedit-start)
(:preedit-end . zero-pinyin-preedit-end)))
;;============
;; public API
;;============
;;===========
;; test data
;;===========
(defun zero-pinyin-build-candidates-test (preedit-str)
"Test data for testing partial commit.
PREEDIT-STR the preedit string."
(cond
((equal preedit-str "liyifeng")
(setq zero-pinyin-used-preedit-str-lengths '(8 4 4 4 2 2 2))
'("李易峰" "利益" "礼仪" "离异" "里" "理" "力"))
((equal preedit-str "feng")
(setq zero-pinyin-used-preedit-str-lengths '(4 4 4 4 4))
'("风" "封" "疯" "丰" "凤"))
((equal preedit-str "yifeng")
(setq zero-pinyin-used-preedit-str-lengths '(6 6 2 2 2 2))
'("一封" "遗风" "艺" "依" "一" "以"))
(t nil)))
(provide 'zero-pinyin)
;;; zero-pinyin.el ends here
;;; zero-quickdial --- quickdial input method written as an emacs minor mode. -*- lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; To use this input method,
;; M-x zero-quickdial-mode ; turn on IM
;; type 1 will insert one
;; type 2 will insert two
;; type 3 will insert three.
;; M-x zero-quickdial-mode ; turn off IM
;;
;; This is just a demo of how Emacs minor mode can work as input method.
;;; Code:
(defun zero-quickdial-insert-one ()
"Insert \"one\"."
(interactive)
(insert "one"))
(defun zero-quickdial-insert-two ()
"Insert \"two\"."
(interactive)
(insert "two"))
(defun zero-quickdial-insert-three ()
"Insert \"three\"."
(interactive)
(insert "three"))
(defvar zero-quickdial-mode-map
'(keymap
(49 . zero-quickdial-insert-one)
(50 . zero-quickdial-insert-two)
(51 . zero-quickdial-insert-three))
"Keymap for zero-quickdial-mode.")
(define-minor-mode zero-quickdial-mode
"a simple input method written as an emacs minor mode"
nil
" Quickdial"
zero-quickdial-mode-map)
(provide 'zero-quickdial)
;;; zero-quickdial.el ends here
;;; zero-reload-all.el --- reload zero-el in correct order -*- no-byte-compile: t; -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;; Code:
(defun zero-rebuild (&optional source-dir)
"Rebuild zero-el.
SOURCE-DIR where to find the zero source dir."
(interactive)
;; for loading s
(package-initialize)
(let ((source-dir (or source-dir "~/lisp/elisp/zero/")))
(dolist (f '("zero-quickdial.el"
"zero-panel.el"
"zero-panel-test.el"
"zero-framework.el"
"zero-framework-test.el"
"zero-pinyin-service.el"
"zero-pinyin-service-test.el"
"zero-pinyin.el"
"zero-pinyin-test.el"
))
(byte-compile-file (concat source-dir f) t))))
(defun zero-reload-all ()
"Recompile and load all zero files."
(interactive)
(byte-recompile-directory "~/lisp/elisp/zero/" 0)
(dolist (f '("zero-quickdial.elc"
"zero-panel.elc"
"zero-panel-test.elc"
"zero-framework.elc"
"zero-framework-test.elc"
"zero-pinyin-service.elc"
"zero-pinyin-service-test.elc"
"zero-pinyin.elc"
"zero-pinyin-test.elc"
"zero-table.el"
"zero-table-test.el"
))
(load-file f)))
;;; zero-reload-all.el ends here
;;; zero-table-test.el --- tests for zero-table.el -*- no-byte-compile: t; lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;
;;; Code:
(require 'zero-table)
(require 'ert)
(ert-deftest zero-table-build-candidates ()
(should (equal (zero-table-build-candidates "ph") '("18612345678")))
(should (equal (zero-table-build-candidates "m")
'("https://msdn.microsoft.com/en-us"
"foo@example.com"
"https://ditu.amap.com/"))))
(ert-deftest zero-table-can-start-sequence ()
(should (zero-table-can-start-sequence ?a))
(should (zero-table-can-start-sequence ?m))
(should-not (zero-table-can-start-sequence ?1))
(should-not (zero-table-can-start-sequence ?b)))
(provide 'zero-table-test)
;;; zero-table-test.el ends here
;;; zero-table.el --- a demo table based input method based on zero.el -*- no-byte-compile: t; lexical-binding: t -*-
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; when you type the key in `zero-table-table', IM will insert the
;; corresponding value.
;;
;; To use this demo IM,
;; (add-to-list 'load-path "~/fromsource/zero")
;; (require 'zero-table)
;; (zero-set-default-im 'zero-table) ; set as default IM
;; or (zero-set-im 'zero-table) ; set as current buffer's IM
;;; Code:
;;==============
;; dependencies
;;==============
(require 'zero-framework)
;;===============================
;; basic data and emacs facility
;;===============================
(defvar zero-table-table nil
"The table used by zero-table input method, map string to string.")
(defvar zero-table-sequence-initials nil "Used in `zero-table-can-start-sequence'.")
;;=====================
;; key logic functions
;;=====================
(defun zero-table-sort-key (lhs rhs)
"A predicate function to sort candidates. Return t if LHS should sort before RHS."
(string< (car lhs) (car rhs)))
(defun zero-table-build-candidates (preedit-str &optional _fetch-size)
"Build candidates by looking up PREEDIT-STR in `zero-table-table'."
(mapcar 'cdr (sort (cl-remove-if-not (lambda (pair) (string-prefix-p preedit-str (car pair))) zero-table-table) 'zero-table-sort-key)))
;; (defun zero-table-build-candidates-async (preedit-str)
;; "build candidate list, when done show it via `zero-table-show-candidates'"
;; (zero-table-debug "building candidate list\n")
;; (let ((candidates (zero-table-build-candidates preedit-str)))
;; ;; update cache to make SPC and digit key selection possible.
;; (setq zero-table-candidates candidates)
;; (zero-table-show-candidates candidates)))
(defun zero-table-can-start-sequence (ch)
"Return t if char CH can start a preedit sequence."
(member (make-string 1 ch) zero-table-sequence-initials))
;;===============================
;; register IM to zero framework
;;===============================
(zero-register-im
'zero-table
'((:build-candidates . zero-table-build-candidates)
(:can-start-sequence . zero-table-can-start-sequence)))
;;============
;; public API
;;============
(defun zero-table-set-table (alist)
"Set the conversion table.
the ALIST should be a list of (key . value) pairs. when user type
\(part of) key, the IM will show all matching value.
e.g.
'((\"phone\" . \"18612345678\")
(\"mail\" . \"foo@example.com\")
(\"map\" . \"https://ditu.amap.com/\")
(\"m\" . \"https://msdn.microsoft.com/en-us\")
(\"address\" . \"123 Happy Street\"))"
(setq zero-table-table alist)
(setq zero-table-sequence-initials
(delete-dups (mapcar (lambda (pair) (substring (car pair) 0 1))
zero-table-table))))
;;===========
;; test data
;;===========
(unless zero-table-table
(zero-table-set-table
'(("phone" . "18612345678")
("pyl" . "http://localdocs.emacsos.com/python2/library/%s.html")
("pyli" . "http://localdocs.emacsos.com/python2/index.html")
("pylm" . "http://localdocs.emacsos.com/python2/py-modindex.html")
("py3li" . "http://localdocs.emacsos.com/python2/index.html")
("py3l" . "http://localdocs.emacsos.com/python3/library/%s.html")
("py3lm" . "http://localdocs.emacsos.com/python3/py-modindex.html")
("pyop" . "http://docs.python.org/library/operator.html")
("pyopl" . "http://localdocs.emacsos.com/python2/library/operator.html")
("pympl" . "http://localdocs.emacsos.com/python2/library/multiprocessing.html")
("py2" . "http://docs.python.org/2/library/%s.html")
("py3" . "http://docs.python.org/3/library/%s.html")
("py2i" . "http://docs.python.org/2/")
("py2m" . "http://docs.python.org/2/py-modindex.html")
("py3i" . "http://docs.python.org/3/")
("py3m" . "http://docs.python.org/3/py-modindex.html")
("pycodec" . "http://localdocs.emacsos.com/python2/library/codecs.html#standard-encodings")
("pycodecs" . "http://localdocs.emacsos.com/python2/library/codecs.html#standard-encodings")
("pycodecsr" . "http://docs.python.org/library/codecs.html#standard-encodings")
("pycodecr" . "http://docs.python.org/library/codecs.html#standard-encodings")
("pep328" . "http://www.python.org/dev/peps/pep-0328/")
("mail" . "foo@example.com")
("map" . "https://ditu.amap.com/")
("m" . "https://msdn.microsoft.com/en-us")
("address" . "123 Happy Street")
("da" . "__da__")
("now" . "__now__"))))
(provide 'zero-table)
;;; zero-table.el ends here