From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.bugs Subject: bug#24603: [RFC 06/18] Add support for title-casing letters Date: Tue, 4 Oct 2016 03:10:29 +0200 Message-ID: <1475543441-10493-6-git-send-email-mina86@mina86.com> References: <1475543441-10493-1-git-send-email-mina86@mina86.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1475543542 22756 195.159.176.226 (4 Oct 2016 01:12:22 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 4 Oct 2016 01:12:22 +0000 (UTC) To: 24603@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Oct 04 03:12:19 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brEH9-0003z5-Oh for geb-bug-gnu-emacs@m.gmane.org; Tue, 04 Oct 2016 03:12:08 +0200 Original-Received: from localhost ([::1]:39714 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEH8-0006dr-9x for geb-bug-gnu-emacs@m.gmane.org; Mon, 03 Oct 2016 21:12:06 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55794) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEGD-0005xa-Jg for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:11:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1brEG6-0001RH-CM for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:11:08 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:37323) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brEG6-0001R2-83 for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:11:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1brEG6-0006dv-04 for bug-gnu-emacs@gnu.org; Mon, 03 Oct 2016 21:11:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 04 Oct 2016 01:11:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24603 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 24603-submit@debbugs.gnu.org id=B24603.147554345925483 (code B ref 24603); Tue, 04 Oct 2016 01:11:01 +0000 Original-Received: (at 24603) by debbugs.gnu.org; 4 Oct 2016 01:10:59 +0000 Original-Received: from localhost ([127.0.0.1]:43500 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brEG2-0006cu-LI for submit@debbugs.gnu.org; Mon, 03 Oct 2016 21:10:59 -0400 Original-Received: from mail-wm0-f44.google.com ([74.125.82.44]:34944) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brEG0-0006cG-Q1 for 24603@debbugs.gnu.org; Mon, 03 Oct 2016 21:10:57 -0400 Original-Received: by mail-wm0-f44.google.com with SMTP id f193so138445891wmg.0 for <24603@debbugs.gnu.org>; Mon, 03 Oct 2016 18:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zyLpccqTdbiseGU9k2tVcTQJmghCcjCaJQPj3PkoV8s=; b=QTlJYvhjNzzKuFs6C53a4F75SG+hkqxc40m39Nc5VIXvpb7l0+5YZ8aMGm2/GKLU4E VgO1L8WjHhFvjduT6iCnYubkAEa0BdpQW2ey8oABieJBeWWryuXe4OPsain7XU6gRdr0 zUQ2kMRKIIHmZBcvVeIkStFYCc0j0+RrGBtvA4ZzitqblktH02oZnSk3EOTAnx6E+TO/ zclrW1Wyrr9Q1qvAfzsqspFIfmUC4vhY+QW22aiMlZHRHSBxRtib2lpn028OWy8WjoQs ISuHPpFEhgp3l6ui9rweNV6WJ+0KqabFUHpHEaTMV0iOTw/0td01s8Wpv8hLGoSCrj7T vVJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=zyLpccqTdbiseGU9k2tVcTQJmghCcjCaJQPj3PkoV8s=; b=fIs777Gzf6MzTs0sk+J+eGcJliVaPgps0sajdhlllR5YbSM3oHzvxgwaWWtFghPOx+ 5h7wPOHB4OwDevWfDBrMmD2DW2KIflrSpesGjFxHis7DVcHyroWsah9/2Y9e8o4Mz4nZ 6HMuOIdlsaqL/OuZjghsun4geoHjZsAi9tul7cQgh+/HxXXweE+CU+II5OiA4NSN1cfD RTfItkfN95/mN5tNLAgi1a4hKASsiR2Qsr4hiTPfEl/ZlB27n5tHzPIqYfJO9AF1vlkf mMHR31/c8n0wXxBl0/RkYuF/BCepHrLtvERu0kAVxE00hg37Q86LfMXxQORts9u8+J4L md8g== X-Gm-Message-State: AA6/9RlQHGC0O4fD8P+F+MlcZl2lI/KzmxGdt6Xhjcq29ruKNlNkhXgVq+QpuLKwarshnKcw X-Received: by 10.28.31.144 with SMTP id f138mr1032850wmf.90.1475543450921; Mon, 03 Oct 2016 18:10:50 -0700 (PDT) Original-Received: from mpn.zrh.corp.google.com ([172.16.113.135]) by smtp.gmail.com with ESMTPSA id ya1sm755102wjb.23.2016.10.03.18.10.49 for <24603@debbugs.gnu.org> (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Oct 2016 18:10:49 -0700 (PDT) Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id 7A1971E0209; Tue, 4 Oct 2016 03:10:48 +0200 (CEST) X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1475543441-10493-1-git-send-email-mina86@mina86.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:123990 Archived-At: * src/casefiddle.c (struct casing_context, prepare_casing_context): Add titlecase_char_table member. It’s set to the ‘titlecase’ Unicode property table if capitalisation has been requested. (case_character): Make use of the titlecase_char_table to title-case initial characters when capitalising. * test/src/casefiddle-tests.el (casefiddle-tests--characters, casefiddle-tests-casing): Update test cases which are now passing. --- etc/NEWS | 10 ++++++---- src/casefiddle.c | 27 +++++++++++++++++++++------ test/src/casefiddle-tests.el | 39 ++++++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 61afcc6..f2bbead 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -235,11 +235,13 @@ same as in modes where the character is not whitespace. Instead of only checking the modification time, Emacs now also checks the file's actual content before prompting the user. -** Title case characters are properly converted to upper case. +** Title case characters are properly cased (from and into). 'upcase', 'upcase-region' et al. convert title case characters (such -as Dz) into their upper case form (such as DZ). As a downside, -'capitalize' and 'upcase-initials' produce awkward words where first -two letters are upper case, e.g. DŽungla (instead of Džungla). +as Dz) into their upper case form (such as DZ). + +Similarly, 'capitalize', 'upcase-initials' et al. make use of +title-case forms of initial characters (correctly producing for example +Džungla instead of incorrect DŽungla). * Changes in Specialized Modes and Packages in Emacs 26.1 diff --git a/src/casefiddle.c b/src/casefiddle.c index 2fbd23b..b3ffa86 100644 --- a/src/casefiddle.c +++ b/src/casefiddle.c @@ -33,6 +33,10 @@ enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP}; /* State for casing individual characters. */ struct casing_context { + /* A char-table with title-case character mappings or nil. It being non-nil + implies flag being CASE_CAPITALIZE or CASE_CAPITALIZE_UP (but the reverse + is not true). */ + Lisp_Object titlecase_char_table; /* User-requested action. */ enum case_action flag; /* If true, function operates on a buffer as opposed to a string or character. @@ -54,6 +58,9 @@ prepare_casing_context (struct casing_context *ctx, ctx->flag = flag; ctx->inbuffer = inbuffer; ctx->inword = flag == CASE_DOWN; + ctx->titlecase_char_table = (int)flag >= (int)CASE_CAPITALIZE + ? uniprop_table (intern_c_string ("titlecase")) + : Qnil; /* If the case table is flagged as modified, rescan it. */ if (NILP (XCHAR_TABLE (BVAR (current_buffer, downcase_table))->extras[1])) @@ -68,10 +75,16 @@ prepare_casing_context (struct casing_context *ctx, static int case_character (struct casing_context *ctx, int ch) { + Lisp_Object prop; + if (ctx->inword) ch = ctx->flag == CASE_CAPITALIZE_UP ? ch : downcase (ch); + else if (!NILP (ctx->titlecase_char_table) && + CHARACTERP (prop = CHAR_TABLE_REF (ctx->titlecase_char_table, ch))) + ch = XFASTINT (prop); else ch = upcase(ch); + if ((int) ctx->flag >= (int) CASE_CAPITALIZE) ctx->inword = SYNTAX (ch) == Sword && (!ctx->inbuffer || ctx->inword || !syntax_prefix_flag_p (ch)); @@ -199,8 +212,8 @@ The argument object is not altered--the value is a copy. */) DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0, doc: /* Convert argument to capitalized form and return that. -This means that each word's first character is upper case -and the rest is lower case. +This means that each word's first character is upper case (more +precisely, if available, title case) and the rest is lower case. The argument may be a character or string. The result has the same type. The argument object is not altered--the value is a copy. */) (Lisp_Object obj) @@ -212,7 +225,8 @@ The argument object is not altered--the value is a copy. */) DEFUN ("upcase-initials", Fupcase_initials, Supcase_initials, 1, 1, 0, doc: /* Convert the initial of each word in the argument to upper case. -Do not change the other letters of each word. +(More precisely, if available, initial of each word is converted to +title-case). Do not change the other letters of each word. The argument may be a character or string. The result has the same type. The argument object is not altered--the value is a copy. */) (Lisp_Object obj) @@ -376,8 +390,8 @@ point and the mark is operated on. */) DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r", doc: /* Convert the region to capitalized form. -Capitalized form means each word's first character is upper case -and the rest of it is lower case. +Capitalized form means each word's first character is upper case (more +precisely, if available, title case) and the rest of it is lower case. In programs, give two arguments, the starting and ending character positions to operate on. */) (Lisp_Object beg, Lisp_Object end) @@ -391,7 +405,8 @@ character positions to operate on. */) DEFUN ("upcase-initials-region", Fupcase_initials_region, Supcase_initials_region, 2, 2, "r", doc: /* Upcase the initial of each word in the region. -Subsequent letters of each word are not changed. +(More precisely, if available, initial of each word is converted to +title-case). Subsequent letters of each word are not changed. In programs, give two arguments, the starting and ending character positions to operate on. */) (Lisp_Object beg, Lisp_Object end) diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el index 8d9bf01..def74a0 100644 --- a/test/src/casefiddle-tests.el +++ b/test/src/casefiddle-tests.el @@ -62,13 +62,9 @@ casefiddle-tests--characters (?Ł ?Ł ?ł ?Ł) (?ł ?Ł ?ł ?Ł) - ;; FIXME: Commented one is what we want. - ;;(?DŽ ?DŽ ?dž ?Dž) - (?DŽ ?DŽ ?dž ?DŽ) - ;;(?Dž ?DŽ ?dž ?Dž) - (?Dž ?DŽ ?dž ?DŽ) - ;;(?dž ?DŽ ?dž ?Dž) - (?dž ?DŽ ?dž ?DŽ) + (?DŽ ?DŽ ?dž ?Dž) + (?Dž ?DŽ ?dž ?Dž) + (?dž ?DŽ ?dž ?Dž) (?Σ ?Σ ?σ ?Σ) (?σ ?Σ ?σ ?Σ) @@ -141,19 +137,19 @@ casefiddle-tests--characters ;; input upcase downcase capitalize upcase-initials '(("Foo baR" "FOO BAR" "foo bar" "Foo Bar" "Foo BaR") ("Ⅷ ⅷ" "Ⅷ Ⅷ" "ⅷ ⅷ" "Ⅷ Ⅷ" "Ⅷ Ⅷ") + ;; "DžUNGLA" is an unfortunate result but it’s really best we can + ;; do while still being consistent. Hopefully, users only ever + ;; use upcase-initials on camelCase identifiers not real words. + ("DŽUNGLA" "DŽUNGLA" "džungla" "Džungla" "DžUNGLA") + ("Džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla") + ("džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla") ;; FIXME: Everything below is broken at the moment. Here’s what ;; should happen: - ;;("DŽUNGLA" "DŽUNGLA" "džungla" "Džungla" "DžUNGLA") - ;;("Džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla") - ;;("džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla") ;;("define" "DEFINE" "define" "Define" "Define") ;;("fish" "FIsh" "fish" "Fish" "Fish") ;;("Straße" "STRASSE" "straße" "Straße" "Straße") ;;("ΌΣΟΣ" "ΌΣΟΣ" "όσος" "Όσος" "Όσος") ;; And here’s what is actually happening: - ("DŽUNGLA" "DŽUNGLA" "džungla" "DŽungla" "DŽUNGLA") - ("Džungla" "DŽUNGLA" "džungla" "DŽungla" "DŽungla") - ("džungla" "DŽUNGLA" "džungla" "DŽungla" "DŽungla") ("define" "DEfiNE" "define" "Define" "Define") ("fish" "fiSH" "fish" "fish" "fish") ("Straße" "STRAßE" "straße" "Straße" "Straße") @@ -186,4 +182,21 @@ casefiddle-tests--characters (setq expected (cdr expected))))))))) +(ert-deftest casefiddle-tests-char-casing () + ;; input upcase downcase [titlecase] + (dolist (test '((?a ?A ?a) (?A ?A ?a) + (?ł ?Ł ?ł) (?Ł ?Ł ?ł) + (?ß ?ß ?ß) (?ẞ ?ẞ ?ß) + (?ⅷ ?Ⅷ ?ⅷ) (?Ⅷ ?Ⅷ ?ⅷ) + (?DŽ ?DŽ ?dž ?Dž) (?Dž ?DŽ ?dž ?Dž) (?dž ?DŽ ?dž ?Dž))) + (let ((ch (car test)) + (up (nth 1 test)) + (lo (nth 2 test)) + (tc (or (nth 3 test) (nth 1 test)))) + (should (eq up (upcase ch))) + (should (eq lo (downcase ch))) + (should (eq tc (capitalize ch))) + (should (eq tc (upcase-initials ch)))))) + + ;;; casefiddle-tests.el ends here -- 2.8.0.rc3.226.g39d4020