Go to file
copilot-swe-agent[bot] 50d41ed30f
fix: enforce kinsoku rules for CJK opening punctuation in box splitting
Opening brackets ((, 「, 《, etc.) are now attached to the following
character instead of the preceding one, preventing them from appearing
at the end of a line which violates kinsoku typographic rules.

Fixes #9

Agent-Logs-Url: https://github.com/Kinneyzhang/emacs-kp/sessions/d05069f4-db64-4601-9816-f582edad4a1b

Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
2026-05-24 15:22:52 +00:00
.phrase Makefile supports multiple plantform 2026-01-26 11:51:19 +08:00
dictionaries first commit 2025-07-26 23:52:04 +08:00
ekp_c refactor: Linus-style code quality improvements across Elisp and C modules 2026-02-10 12:18:00 +00:00
images update readme 2025-07-27 21:35:44 +08:00
tests add test 2026-01-25 20:12:38 +08:00
.gitignore improve C module 2026-01-26 01:24:47 +08:00
AGENTS.md code improve 2026-01-24 20:44:38 +08:00
DEVELOPER_ZH.md add dynamic moudle to improve prefermance 2026-01-25 13:50:46 +08:00
DEVELOPER.md add dynamic moudle to improve prefermance 2026-01-25 13:50:46 +08:00
ekp-hyphen.el refactor ekp cache and code improve 2026-01-24 22:40:53 +08:00
ekp-utils.el fix: enforce kinsoku rules for CJK opening punctuation in box splitting 2026-05-24 15:22:52 +00:00
ekp-utils.elc fix: enforce kinsoku rules for CJK opening punctuation in box splitting 2026-05-24 15:22:52 +00:00
ekp.el fix: address code review feedback - improve docstrings and use defalias consistently 2026-02-10 12:19:22 +00:00
readme_zh.md add dynamic moudle to improve prefermance 2026-01-25 13:50:46 +08:00
readme.md add dynamic moudle to improve prefermance 2026-01-25 13:50:46 +08:00

Emacs-KP: Knuth-Plass Line Breaking for Emacs

中文文档 | Developer Guide

Emacs-kp implements the Knuth-Plass optimal line breaking algorithm with full support for CJK (Chinese, Japanese, Korean) and Latin mixed text typesetting.

Demo

Features

  • Optimal Line Breaking: Uses Knuth-Plass algorithm for globally optimal paragraph layout.
  • CJK Support: Full support for Chinese, Japanese, Korean with mixed Latin text.
  • Hyphenation: Frank Liang's algorithm with language-specific dictionaries.
  • Text Properties Preserved: Font faces, colors, and other Emacs text properties are maintained.
  • C Module Acceleration: Optional multi-threaded C module for 16-29x speedup.
  • Automatic Font Handling: Spacing parameters computed from actual font metrics.

User Guide

Quick Start

  1. Install Dependencies: Ensure you have a C compiler if you plan to use the C module (recommended for performance).

  2. Configuration:

(add-to-list 'load-path "/path/to/emacs-kp")
(require 'ekp)

;; Basic usage: justify text to 600 pixels width
(ekp-pixel-justify "Your paragraph text here..." 600)

;; Find optimal width in a range (returns (text . optimal-width))
(ekp-pixel-range-justify "Your text" 400 800)

Configuration

Language Settings

ekp-latin-lang (default: "en_US")

Primary Latin language for hyphenation. Supported languages are in dictionaries/ directory:

  • en_US, en_GB - English
  • de_DE - German
  • fr - French
  • es - Spanish
  • And many more...
(setq ekp-latin-lang "de_DE")

Spacing Parameters

Use ekp-param-set to configure spacing (in pixels). If not set, defaults are computed automatically from font metrics.

(ekp-param-set lws-ideal lws-stretch lws-shrink
               mws-ideal mws-stretch mws-shrink
               cws-ideal cws-stretch cws-shrink)
Parameter Group Description
lws-* Latin Word Space: between Latin words
mws-* Mixed Word Space: between Latin and CJK
cws-* CJK Word Space: between CJK characters

K-P Algorithm Parameters

Variable Default Description
ekp-line-penalty 10 Base cost per line break
ekp-hyphen-penalty 50 Extra cost for hyphenated breaks
ekp-adjacent-fitness-penalty 100 Cost for inconsistent line tightness
ekp-last-line-min-ratio 0.5 Minimum fill ratio for last line
ekp-looseness 0 Target line count offset (±n lines)

For large texts, the optional C module provides significant performance improvement through multi-threaded parallel computation.

Building

cd ekp_c
make

Requirements: C11 compiler, Emacs 27.1+

Loading

(require 'ekp-utils)

;; Load and initialize C module
(ekp-c-module-load)

;; Optional: Load hyphenation dictionary for C module
(ekp-c-load-dictionary "en_US")

Once loaded, ekp-use-c-module defaults to t, and all justification functions will automatically use the C module.


Algorithm & Architecture

For a detailed explanation of the internal architecture, algorithms, and API reference, please refer to the Developer Guide.

Credits