Commit 72196558 authored by Yuanle Song's avatar Yuanle Song
Browse files

v1.2.6 use a single file distribution for melpa.

- renamed zero.el back to zero-framework.el
- to build a dist, just run "make".
  the zero.el template is in
- zero.el is built and added to git repo in git pre-commit hook. it will
  be kept in master branch.
- fixed a few package-lint style warning.
parent 66e0dbb5
VERSION := $(shell grep 'setq zero-version' zero.el | cut -d'"' -f2)
VERSION := $(shell grep 'setq zero-version' zero-framework.el | cut -d'"' -f2)
default: compile
sed "s/PKG_VERSION/$(VERSION)/g" zero-pkg.el.tpl > zero-pkg.el
default: dist
# multiple file
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: pkg-el
git archive -o zero-el-$(VERSION).zip --prefix=zero/ HEAD
pkg: pkg-el
# Note: install from tar is not working. install from dir does work.
@echo "Creating tar for use with M-x package-install-file"
git archive -o zero-$(VERSION).tar --prefix=zero-$(VERSION)/ HEAD
@echo "Done"
# single file distribution
dist: dist-check
if [ ! -x ~/.local/bin/pytest ]; then python3 -m pip install --user pytest; fi
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
rsync -air git-hooks/ .git/hooks/
@echo $(VERSION)
.PHONY: default pkg-el compile zip pkg install-git-hooks version
.PHONY: default check zip dist build dist-check install-git-hooks version
* COMMENT -*- mode: org -*-
#+Date: 2019-09-01
Time-stamp: <2019-10-11>
Time-stamp: <2019-10-16>
* zero-el
zero-el provides zero-pinyin, an Emacs pinyin input method for Chinese and
zero library, which is an emacs Chinese input method framework.
zero-framework, which is an emacs Chinese input method framework.
* File list
- zero.el
- zero-framework.el
zero framework source code. This provides the framework and user interface
for zero-el.
......@@ -44,5 +44,5 @@
* License
zero-el is under Apache License 2.0
ibus-compute-pixel-position function in zero.el is under GPLv3.
see NOTICE file.
zero--ibus-compute-pixel-position function in zero-framework.el is under
GPLv3. see NOTICE file.
#!/usr/bin/env python3
# coding=utf-8
build zero.el from 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") == (
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)
if line.startswith('(provide'):
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),
return result
def main():
format='%(asctime)s [%(module)s] %(levelname)-8s %(message)s',
with open("") as tpl:
content =
expanded_content = expand_placeholder_for_files(content, [
with open('zero.el', 'w') as out:
if __name__ == '__main__':
set -e
make pkg-el compile
git add zero-pkg.el
make dist-check
git add zero.el
* COMMENT -*- mode: org -*-
#+Date: 2019-10-08
Time-stamp: <2019-10-11>
Time-stamp: <2019-10-16>
#+STARTUP: content
* notes :entry:
** 2019-04-01 zero.el a Chinese IM framework in emacs; FSM :doc:
......@@ -83,6 +83,65 @@ cd ~/lisp/elisp/zero/
* 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
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>))
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)
"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
- make build
should generate zero.el and run checks on it.
- insert content to
;;; 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
;;; zero-test.el --- tests for zero.el -*- lexical-binding: t -*-
;;; 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.
......@@ -14,11 +14,11 @@
;;; Commentary:
;; tests for zero-framework.el
;;; Code:
(require 'zero)
(require 'zero-framework)
(require 'ert)
(ert-deftest zero-cycle-list ()
......@@ -39,6 +39,6 @@
(should (string-equal "hehe" (zero-convert-str-to-full-width "hehe")))
(should (string-equal "(A)" (zero-convert-str-to-full-width "(A)"))))
(provide 'zero-test)
(provide 'zero-framework-test)
;;; zero-test.el ends here
;;; zero-framework-test.el ends here
;;; zero-framework.el --- Zero Chinese input method 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
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; zero-framework is a Chinese input method framework for Emacs, implemented
;; as an Emacs minor mode.
;; You can cycle zero-punctuation-level in current buffer by C-c , ,
;; You can change default Chinese punctuation level:
;; (setq-default zero-punctuation-level *zero-punctuation-level-full*)
;; You can toggle full-width mode in current buffer by C-c , .
;; You can enable full-width mode by default:
;; (setq-default zero-full-width-mode t)
;;; Code:
;; dependencies
(eval-when-compile (require 'cl-lib))
(require 's)
(require 'zero-panel)
;; utils
;; this function is from ibus.el
(defun zero--ibus-compute-pixel-position (&optional pos window)
"Return geometry of object at POS in WINDOW as a list like \(X Y H).
X and Y are pixel coordinates relative to top left corner of frame which
WINDOW is in. H is the pixel height of the object.
Omitting POS and WINDOW means use current position and selected window,
(let* ((frame (window-frame (or window (selected-window))))
(posn (posn-at-point (or pos (window-point window)) window))
(line (cdr (posn-actual-col-row posn)))
(line-height (and line
(or (window-line-height line window)
(and (redisplay t)
(window-line-height line window)))))
(x-y (or (posn-x-y posn)
(let ((geom (pos-visible-in-window-p
(or pos (window-point window)) window t)))
(and geom (cons (car geom) (cadr geom))))
'(0 . 0)))
(ax (+ (car (window-inside-pixel-edges window))
(car x-y)))
(ay (+ (cadr (window-pixel-edges window))
(or (nth 2 line-height) (cdr x-y))))
(height (or (car line-height)
(with-current-buffer (window-buffer window)
;; `posn-object-width-height' returns an incorrect value
;; when the header line is displayed (Emacs bug #4426).
((and posn
(null header-line-format))
(cdr (posn-object-width-height posn)))
((and (bound-and-true-p text-scale-mode)
(not (zerop (with-no-warnings
(round (* (frame-char-height frame)
(expt text-scale-mode-step
(frame-char-height frame)))))))
(list ax ay height)))
(defun zero-get-point-position ()
"Return current point's position (x y).
Origin (0, 0) is at screen top left corner."
(cl-destructuring-bind (x y line-height) (zero--ibus-compute-pixel-position)
((functionp 'window-absolute-pixel-position)
;; introduced in emacs 26
(cl-destructuring-bind (x . y) (window-absolute-pixel-position)
(list x (+ y line-height))))
((functionp 'frame-edges)
;; introduced in emacs 25
(cl-destructuring-bind (frame-x frame-y &rest rest)
(frame-edges nil 'inner-edges)
(list (+ frame-x x) (+ frame-y y line-height))))
;; <= emacs 24, used guessed pixel size for tool-bar, menu-bar, WM title
;; bar. Since I can't get that from elisp.
(list (+ (frame-parameter nil 'left)
(if (and (> (frame-parameter nil 'tool-bar-lines) 0)
(eq (frame-parameter nil 'tool-bar-position) 'left))
96 0)
(+ (frame-parameter nil 'top)
(if (and (> (frame-parameter nil 'tool-bar-lines) 0)
(eq (frame-parameter nil 'tool-bar-position) 'top))
42 0)
(if (> (frame-parameter nil 'menu-bar-lines) 0) (+ 30 30) 0)
(defun zero-cycle-list (lst item)
"Return the object next to given ITEM in LST.
If item is the last object, return the first object in lst.
If item is not in lst, return nil."
(let ((r (member item lst)))
((null r) nil)
(t (or (cadr r)
(car lst))))))
;; key logic functions
;; zero-el version
(defvar zero-version nil "Zero package version.")
(setq zero-version "1.2.6")
;; FSM state
(defconst zero--state-im-off 'IM-OFF)
(defconst zero--state-im-waiting-input 'IM-WAITING-INPUT)
(defconst zero--state-im-preediting 'IM-PREEDITING)
(defconst zero-punctuation-level-basic 'BASIC)
(defconst zero-punctuation-level-full 'FULL)
(defconst zero-punctuation-level-none 'NONE)
(defvar zero-im nil
"Stores current input method.
If nil, the empty input method will be used. In the empty input
method, only punctuation is handled. Other keys are pass
(defvar zero-ims nil
"A list of registered input methods.")
(defvar zero-buffer nil
"Stores the associated buffer.
this is used to help with buffer focus in/out events")
(defvar zero-state zero--state-im-off)
(defvar zero-full-width-mode nil
"Set to t to enable full-width mode.
In full-width mode, commit ascii char will insert full-width char if there is a
corresponding full-width char. This full-width char map is
independent from punctuation map. You can change this via
(defvar zero-punctuation-level zero-punctuation-level-basic
"Punctuation level.
Should be one of
(defvar zero-punctuation-levels (list zero-punctuation-level-basic
"Punctuation levels to use when `zero-cycle-punctuation-level'.")
(defvar zero-double-quote-flag nil
"Non-nil means next double quote insert close quote.
Used when converting double quote to Chinese quote.
If nil, next double quote insert open quote.
Otherwise, next double quote insert close quote.")
(defvar zero-single-quote-flag nil
"Non-nil means next single quote insert close quote.
Used when converting single quote to Chinese quote.
If nil, next single quote insert open quote.
Otherwise, next single quote insert close quote.")
(defvar zero-preedit-str "")
(defvar zero-candidates nil)
(defcustom zero-candidates-per-page 10
"How many candidates to show on each page."
:group 'zero
:type 'integer)
(defvar zero-current-page 0 "Current page number. count from 0.")
(defvar zero-initial-fetch-size 20
"How many candidates to fetch for the first call to GetCandidates.")
;; zero-fetch-size is reset to 0 when preedit-str changes.
;; zero-fetch-size is set to fetch-size in build-candidates-async complete-func
;; lambda.
(defvar zero-fetch-size 0 "Last GetCandidates call's fetch-size.")
(defvar zero-previous-page-key ?\- "Previous page key.")
(defvar zero-next-page-key ?\= "Next page key.")
;;; concrete input method should define these functions and set them in the
;;; corresponding *-func variable.
(defun zero-build-candidates-default (_preedit-str _fetch-size)
"Default implementation for `zero-build-candidates-func'."
(defun zero-can-start-sequence-default (_ch)
"Default implementation for `zero-can-start-sequence-func'."
(defun zero-get-preedit-str-for-panel-default ()
"Default implementation for `zero-get-preedit-str-for-panel-func'."
(defvar zero-build-candidates-func 'zero-build-candidates-default
"Contains a function to build candidates from preedit-str. The function accepts param preedit-str, fetch-size, returns candidate list.")
(defvar zero-build-candidates-async-func 'zero-build-candidates-async-default
"Contains a function to build candidates from preedit-str. The function accepts param preedit-str, fetch-size, and a complete-func that should be called on returned candidate list.")
(defvar zero-can-start-sequence-func 'zero-can-start-sequence-default
"Contains a function to decide whether a char can start a preedit sequence.")
(defvar zero-handle-preedit-char-func 'zero-handle-preedit-char-default
"Contains a function to handle IM-PREEDITING state char insert.
The function should return t if char is handled.
This allow input method to override default logic.")
(defvar zero-get-preedit-str-for-panel-func 'zero-get-preedit-str-for-panel-default
"Contains a function that return preedit-str to show in zero-panel.")
(defvar zero-backspace-func 'zero-backspace-default
"Contains a function to handle <backward> char.")
(defvar zero-handle-preedit-char-func 'zero-handle-preedit-char-default
"Hanlde character insert in `zero--state-im-preediting' mode.")
(defvar zero-preedit-start-func 'nil
"Called when enter `zero--state-im-preediting' state.")
(defvar zero-preedit-end-func 'nil
"Called when leave `zero--state-im-preediting' state.")
(defvar zero-enable-debug nil
"Whether to enable debug.
if t, `zero-debug' will output debug msg in *zero-debug* buffer")
(defvar zero-debug-buffer-max-size 30000
"Max characters in *zero-debug* buffer. If reached, first half data will be deleted.")
(defun zero-debug (string &rest objects)
"Log debug message in *zero-debug* buffer.
STRING and OBJECTS are passed to `format'"
(if zero-enable-debug
(with-current-buffer (get-buffer-create "*zero-debug*")
(goto-char (point-max))
(insert (apply 'format string objects))
(when (> (point) zero-debug-buffer-max-size)
(insert "removing old data\n")
(delete-region (point-min) (/ zero-debug-buffer-max-size 2))))))
;; (zero-debug "msg1\n")
;; (zero-debug "msg2: %s\n" "some obj")
;; (zero-debug "msg3: %s\n" 24)
;; (zero-debug "msg4: %s %s\n" 24 1)
(defun zero-enter-preedit-state ()
"Config keymap when enter preedit state."
(if (functionp zero-preedit-start-func)
(funcall zero-preedit-start-func)))
(defun zero-leave-preedit-state ()
"Config keymap when leave preedit state."
(if (functionp zero-preedit-end-func)
(funcall zero-preedit-end-func)))
(defun zero-set-state (state)
"Set zero state to given STATE."
(zero-debug "set state to %s\n" state)
(setq zero-state state)
(if (eq state zero--state-im-preediting)
(defun zero-candidates-on-page (candidates)
"Return candidates on current page for given CANDIDATES list."
(cl-flet ((take (n lst)
"take the first n element from lst. if there is not
enough elements, return lst as it is."
for lst* = lst then (cdr lst*)
for n* = n then (1- n*)
until (or (zerop n*) (null lst*))
collect (car lst*)))
(drop (n lst)
"drop the first n elements from lst"
for lst* = lst then (cdr lst*)
for n* = n then (1- n*)
until (or (zerop n*) (null lst*))
finally (return lst*))))
(take zero-candidates-per-page
(drop (* zero-candidates-per-page zero-current-page) candidates))))
(defun zero-show-candidates (&optional candidates)
"Show CANDIDATES using zero-panel via IPC/RPC."
(let ((candidates-on-page (zero-candidates-on-page (or candidates
(cl-destructuring-bind (x y) (zero-get-point-position)
(funcall zero-get-preedit-str-for-panel-func)
(length candidates-on-page)
`(("in_emacs" t)
("filename" ,(or (buffer-file-name) ""))
("page_number" ,(1+ zero-current-page))
("has_next_page" ,(or (> (length (or candidates zero-candidates)) (* zero-candidates-per-page (1+ zero-current-page))) (< zero-fetch-size (* zero-candidates-per-page (+ 2 zero-current-page)))))
("has_previous_page" ,(> zero-current-page 0))
("move_x" :int32 ,x)
("move_y" :int32 ,y)))
(zero-debug "candidates: %s\n" (s-join ", " candidates-on-page)))))
(defun zero-build-candidates (preedit-str fetch-size)
"Build candidates list synchronously.
Try to find at least FETCH-SIZE number of candidates for PREEDIT-STR."
;; (zero-debug "zero-build-candidates\n")
(unless (functionp zero-build-candidates-func)
(signal 'wrong-type-argument (list 'functionp zero-build-candidates-func)))
(prog1 (funcall zero-build-candidates-func preedit-str fetch-size)
(setq zero-fetch-size (max fetch-size (length zero-candidates)))))
(defun zero-build-candidates-complete (candidates)
"Called when `zero-build-candidates-async' return.
CANDIDATES is returned candidates list from async call."
(setq zero-candidates candidates)
(zero-show-candidates candidates))
(defun zero-build-candidates-async-default (preedit-str fetch-size complete-func)
"Build candidate list, when done show it via `zero-show-candidates'.
PREEDIT-STR the preedit-str.
FETCH-SIZE try to find at least this many candidates for preedit-str.
COMPLETE-FUNC the function to call when build candidates completes."
;; (zero-debug "zero-build-candidates-async-default\n")
(let ((candidates (zero-build-candidates preedit-str fetch-size)))
;; update cache to make SPC and digit key selection possible.
(funcall complete-func candidates)))
(defvar zero-full-width-char-map
;; ascii 33 to 126 map to
;; unicode FF01 to FF5E
for i from 33 to 126
collect (cons (make-char 'ascii i) (make-char 'unicode 0 255 (- i 32))))
"An alist that map half-width char to full-width char.")
(defun zero-convert-ch-to-full-width (ch)
"Convert half-width char CH to full-width.
If there is no full-width char for CH, return it unchanged."
(let ((pair (assoc ch zero-full-width-char-map)))
(if pair (cdr pair) ch)))
(defun zero-convert-str-to-full-width (s)
"Convert each char in S to their full-width char if there is one."
(concat (mapcar 'zero-convert-ch-to-full-width s)))
(defun zero-convert-str-to-full-width-maybe (s)
"If in `zero-full-width-mode', convert char in S to their full-width char; otherwise, return s unchanged."
(if zero-full-width-mode (zero-convert-str-to-full-width s) s))
(defun zero-insert-full-width-char (ch)
"If in `zero-full-width-mode', insert full-width char for given CH and return true, otherwise just return nil."
(when zero-full-width-mode
(let ((full-width-ch (zero-convert-ch-to-full-width ch)))
(insert full-width-ch)