Skip to content
zero-input-framework.el 39.3 KiB
Newer Older
;;; zero-input-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
;;
;;     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:

;; zero-input-framework is a Chinese input method framework for Emacs,
;; implemented as an Emacs minor mode.
;;
;; You can cycle zero-input-punctuation-level in current buffer by C-c , ,
;; You can change default Chinese punctuation level:
;;
;;   (setq-default zero-input-punctuation-level zero-input-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-input-full-width-p t)
;;

;;; Code:

;;==============
;; dependencies
;;==============

(eval-when-compile (require 'cl-lib) (require 'cl-macs))
(require 'ring)
(require 's)
(require 'zero-input-panel)

;;=======
;; utils
;;=======

;; this function is from ibus.el
(defun zero-input--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,
respectively."
  (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)
		       (cond
			;; `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
					    text-scale-mode-amount))))
			 (round (* (frame-char-height frame)
				   (with-no-warnings
				     (expt text-scale-mode-step
					   text-scale-mode-amount)))))
			(t
			 (frame-char-height frame)))))))
    (list ax ay height)))

(defun zero-input-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-input--ibus-compute-pixel-position)
    (cond
     ((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))))
     (t
      ;; <= 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)
	       x)
	    (+ (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)
	       line-height
	       y))))))

(defun zero-input-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)))
    (cond
     ((null r) nil)
     (t (or (cadr r)
	    (car lst))))))

;;=====================
;; key logic functions
;;=====================

;; zero-input-el version
(defvar zero-input-version nil "Zero package version.")
(setq zero-input-version "2.10.3")

(defvar zero-input-panel-is-ephemeral nil
  "Stores whether the panel service is ephemeral or not.

When a zero-input panel service can not persist its content on
key strokes, it should set this to t, so zero-input-framework
will call `zero-input-panel-show-candidates' on every keystroke
to refresh the candidates list even when no change is needed.

A zero-input panel service should revert this variable to nil on
exit.")

;; FSM state
(defconst zero-input--state-im-off 'IM-OFF)
(defconst zero-input--state-im-waiting-input 'IM-WAITING-INPUT)
(defconst zero-input--state-im-preediting 'IM-PREEDITING)

(defconst zero-input-punctuation-level-basic 'BASIC)
(defconst zero-input-punctuation-level-full 'FULL)
(defconst zero-input-punctuation-level-none 'NONE)

(defvar-local zero-input-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
through")
(defvar zero-input-ims nil
  "A list of registered input methods.")

(defvar-local zero-input-buffer nil
  "Stores the associated buffer.
this is used to help with buffer focus in/out events")

(defvar-local zero-input-state zero-input--state-im-off)
(defcustom zero-input-full-width-p 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
`zero-input-toggle-full-width'"
  :group 'zero-input
  :safe t
  :type 'boolean)
(make-variable-buffer-local 'zero-input-full-width-p)

(defcustom zero-input-punctuation-basic-map
  '((?, ",")
    (?, ",")
    (?. "。")				; 0x3002
    (?? "?")
    (?! "!")
    (?\\ "、")				; 0x3001
    (?: ":"))
  "Punctuation map used when `zero-input-punctuation-level' is not 'NONE."
  :group 'zero-input
  :type '(alist :key-type character :value-type (group string)))

(defcustom zero-input-punctuation-full-map
  '((?_ "——")
    (?< "《")				;0x300A
    (?> "》")				;0x300B
    (?\( "(")
    (?\) ")")
Loading full blame...