From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "Paul W. Rankin" Newsgroups: gmane.emacs.help Subject: Re: title-case function Date: Sun, 21 Apr 2019 22:45:36 +1000 Message-ID: References: <09C04C7E-B175-45A5-A4E1-4E5AC14B4B66@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; format=flowed Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="12718"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: mu4e 1.0; emacs 26.2 Cc: help-gnu-emacs To: Jean-Christophe Helary Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Apr 21 14:46:00 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIBr5-0003A2-Bz for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Apr 2019 14:46:00 +0200 Original-Received: from localhost ([127.0.0.1]:52880 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hIBr4-0007kn-7w for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Apr 2019 08:45:58 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:50992) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hIBqr-0007jc-T1 for help-gnu-emacs@gnu.org; Sun, 21 Apr 2019 08:45:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hIBqq-0006tf-QW for help-gnu-emacs@gnu.org; Sun, 21 Apr 2019 08:45:45 -0400 Original-Received: from out3-smtp.messagingengine.com ([66.111.4.27]:36411) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hIBqq-0006sF-IB for help-gnu-emacs@gnu.org; Sun, 21 Apr 2019 08:45:44 -0400 Original-Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id D3D3A210DB; Sun, 21 Apr 2019 08:45:43 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 21 Apr 2019 08:45:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paulwrankin.com; h=references:from:to:cc:subject:in-reply-to:date:message-id :mime-version:content-type; s=fm3; bh=VswZski2ct0h1Kegz0B0sxePXs DloEIUTbAWY/U5VPk=; b=VcjpBTXM2NN3VhcqkZRdMAFEmit/9aXV0/UMTCCYFL Wz+LExLaMCFX/ZGBqXMp2G7wkHtgRTDQzSf/YzPPXwXc9tOPZsSXCbramflklHN4 sPGGAVrea7+h7FuQ4XeBAXUQUTGbXbkxmJefWOcpVNJT+qns7lvYzAWNp+j/M6D7 KKZ5I8hy+lrJsPxip5uDpntUYAXZfuZsBcdCKHcv+dpl19m3JPk6yBgWoP9G+HSB TIHuoHBtBZZ2ZXNcdzhmuwM1qpXytR7zmJ9gthk70gzHb/ym2JPrfn8Dv6QV5cdT iOnRWoZxaS1vTvZX70NIwz7WuMcm+Px1wntgFkLTd6LA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=VswZsk i2ct0h1Kegz0B0sxePXsDloEIUTbAWY/U5VPk=; b=cAda24iRZBJzrmzMhBiWvN XshzlmYv6jGvIzvcdEgHYpybzH6L6tqkoniF5fA85iZTfwUeiu9d9Hv302BWZ2zl iS+BPJLx/Xqswqvp9ux2rD/Rq3xLPr2nrKDkXN0zRPiFSlouYssy99g+NDJw10Zy ndmjgoTys2RQ62y43FXO2bcIjm7En4FdiK0tI8ydNp+HcKD/zC8AOgL81T/bm3uH VNnIInGJmDvpzr8NCt4VcpAoDiGZusBSP0nR7tUNej56S6JQFq62SHP7Lz6clyps U67i3IrdXCRtx1bNN4yYd22yvq2r6EM03BTCiPGzMeN7Il2nb6qsudXrkEAIQxiQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeeggdefjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepfhgfhffvufgjfffkgggtsehttdertd dtredtnecuhfhrohhmpedfrfgruhhlucghrdcutfgrnhhkihhnfdcuoehhvghllhhosehp rghulhifrhgrnhhkihhnrdgtohhmqeenucffohhmrghinhepphgruhhlfihrrghnkhhinh drtghomhenucfkphepuddvtddrvddvrddutdekrddufeelnecurfgrrhgrmhepmhgrihhl fhhrohhmpehhvghllhhosehprghulhifrhgrnhhkihhnrdgtohhmnecuvehluhhsthgvrh fuihiivgeptd X-ME-Proxy: Original-Received: from localhost (unknown [120.22.108.139]) by mail.messagingengine.com (Postfix) with ESMTPA id 93024E44B4; Sun, 21 Apr 2019 08:45:41 -0400 (EDT) In-reply-to: <09C04C7E-B175-45A5-A4E1-4E5AC14B4B66@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.27 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:120012 Archived-At: On Sun, Apr 21 2019, Jean-Christophe Helary wrote: > I'm not aware that we have title-case needs in French, or in any other > language I know, but just as a precaution I renamed > "title-case-minor-words" to "title-case-minor-english-words". This seems a little redundant to me if title case is an English-only thing. But also it's a defcustom, so the user is free to make the words any other language (which would then make the name quite silly). That said, anyone pasting this into their init file is more than welcome to rename the option whatever they like! A slight improvement below. Function title-case works on a string object, title-case-region works on the region. Using a temp buffer is a bit annoying though... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (defcustom title-case-minor-words '("the ""a" "an" "and" "but" "for" "of" "or" "nor" "is" "as" "at" "in" "to" "v" "vs" "de") "List of minor word strings that should be downcased in titles. These words should be less than four characters." :type '(repeat string) :group 'editing-basics) (defun title-case (string) "Convert STRING to Title Case. First and last words are capitalized unconditionally, as are words following colons, en dashes and em dashes. Words in list `title-case-minor-words' are downcased." (with-temp-buffer (insert string) (let (last-word) (goto-char (point-max)) (forward-word -1) (setq last-word (point)) (capitalize-word 1) (goto-char (point-min)) (capitalize-word 1) (while (< (point) last-word (point-max)) (if (looking-at "[:\x2013\x2014]") (capitalize-word 1) (skip-syntax-forward "-." last-word) (if (looking-at (concat "\\b" (regexp-opt title-case-minor-words) "\\b")) (downcase-word 1) (capitalize-word 1))))) (buffer-string))) (defun title-case-region (beg end) "Convert region from BEG to END to Title Case." (interactive "r") (save-excursion (goto-char end) (unless (looking-at "\\b") (forward-word 1) (setq end (point))) (goto-char beg) (unless (looking-at "\\b") (forward-word -1)) (title-case (buffer-substring (point) end)))) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- https://www.paulwrankin.com