From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Ian Dunn Newsgroups: gmane.emacs.devel Subject: Re: [ELPA] New package: Auto Capitalize Mode Date: Sun, 27 Aug 2017 20:49:16 -0400 Message-ID: <87k21ocy9f.fsf@escafil> References: <87d17hdmnm.fsf@escafil> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1503881415 6833 195.159.176.226 (28 Aug 2017 00:50:15 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 28 Aug 2017 00:50:15 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Aug 28 02:50:10 2017 Return-path: Envelope-to: ged-emacs-devel@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 1dm8Fb-0000jv-CN for ged-emacs-devel@m.gmane.org; Mon, 28 Aug 2017 02:49:59 +0200 Original-Received: from localhost ([::1]:36342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dm8Ff-0001zB-6F for ged-emacs-devel@m.gmane.org; Sun, 27 Aug 2017 20:50:03 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60118) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dm8FU-0001yK-OI for emacs-devel@gnu.org; Sun, 27 Aug 2017 20:49:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dm8FT-0004Zy-LU for emacs-devel@gnu.org; Sun, 27 Aug 2017 20:49:52 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:33842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dm8FT-0004Zn-GE; Sun, 27 Aug 2017 20:49:51 -0400 Original-Received: from [2604:6000:1010:176:da4d:3352:bae5:f50e] (port=44476 helo=escafil) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1dm8FT-00020R-3N; Sun, 27 Aug 2017 20:49:51 -0400 In-Reply-To: (Stefan Monnier's message of "Sun, 27 Aug 2017 12:36:10 -0400") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:217859 Archived-At: --=-=-= Content-Type: text/plain >>>>> "Stefan" == Stefan Monnier writes: >> If there are no issues, I'll push it to the git repo myself. Stefan> No objection on my side. Thanks you. Just one comment: >> (defvar-local auto-capitalize-sentence-bounds-function >> #'auto-capitalize--default-sentence-bounds "Function to determine >> the bounds of the current sentence. >> >> Should return the bounds just as `bounds-of-thing-at-point'.") Stefan> You only use the `car` part of the return value AFAICT, so Stefan> requiring the cdr part just imposes an extra burden. Stefan> IOW, I'd remove the `car` you have (twice) in Stefan> auto-capitalize--run and put it inside Stefan> auto-capitalize--default-sentence-bounds instead (and rename Stefan> since it doesn't return "bounds" any more). >> (defun turn-on-auto-capitalize-mode () (auto-capitalize-mode 1)) Stefan> AKA Stefan> (defun turn-on-auto-capitalize-mode () Stefan> (auto-capitalize-mode)) Stefan> hence, it's unnecessary, you can just do: Stefan> ;;;###autoload (define-globalized-minor-mode Stefan> global-auto-capitalize-mode auto-capitalize-mode Stefan> auto-capitalize-mode) I agree on both counts. I've attached a file with the fixes applied. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=auto-capitalize-mode.el Content-Transfer-Encoding: quoted-printable ;;; auto-capitalize-mode.el --- Minor mode to automatically capitalize the = start of a sentence. -*- lexical-binding: t; -*- ;; Copyright (C) 2017 Ian Dunn ;; Author: Ian Dunn ;; Maintainer: Ian Dunn ;; Keywords: editing ;; Version: 1.0 ;; Created: 11 Apr 2016 ;; Modified: 11 May 2016 ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Automatically capitalizes the first word of a sentence as you type. The ;; bounds of a sentence are determined by local variable ;; `auto-capitalize-sentence-start-function'. ;; Auto-capitalize can be used as a local or global minor mode. Further co= ntrol ;; can be obtained by modifying the local variable `auto-capitalize-predica= te'. ;;; Code: (eval-when-compile (require 'subr-x)) (require 'thingatpt) (defvar-local auto-capitalize-predicate nil "Predicate to check for auto-capitalization. Should be a function of no arguments, and return non-nil if auto-capitalization should be performed at the current point. For example, this could be a function to check if point is in a comment.") (defun auto-capitalize-should-capitalize-p () "Return non-nil if auto-capitalization should be performed." (when auto-capitalize-predicate (funcall auto-capitalize-predicate))) (defun auto-capitalize--default-sentence-start () "Default value of `auto-capitalize-sentence-start-function'. Just runs `bounds-of-thing-at-point' for a sentence." (car (bounds-of-thing-at-point 'sentence))) (defvar-local auto-capitalize-sentence-start-function #'auto-capitalize--default-sentence-start "Function to determine the start of the current sentence. This should be a function of no arguments that returns the point at which the current function begins.") (defun auto-capitalize--run () "Automatically capitalize the word at point if it is the start of a sente= nce." ;; Only do this if the last inserted character isn't part of a word, ;; (preceding-char) is the previously inserted character; ;; I also need the character before that (It must be a word constituent) (when (and (auto-capitalize-should-capitalize-p) (not (or (bobp) (eq (char-syntax (preceding-char)) ?w))) (eq (char-syntax (save-excursion (forward-char -1) (preceding-char))) ?w)) (save-excursion ;; Move back to the word that was just finished, and determine if it ;; starts a sentence. (backward-word-strictly) ;; Ensure we're still expected to capitalize. Case: New character pu= t us ;; inside a string, but backward-word-strictly brought us outside the ;; string. (when (auto-capitalize-should-capitalize-p) (when-let ((word-bounds (bounds-of-thing-at-point 'word)) (sentence-start (funcall auto-capitalize-sentence-start-= function))) (cond ((let ((case-fold-search nil)) (string-match-p "[[:upper:]]" (word-at-point))) nil) ((eq (car word-bounds) sentence-start) (capitalize-word 1)) ;; Word bound will only ever be one greater than the sentence bo= und if ;; the sentence begins with some sort of punctuation. Remember,= spaces ;; don=E2=80=99t count, so if we have a sentence starting with "= A ball...", ;; =E2=80=99ball=E2=80=99 wont trigger this, only =E2=80=99A=E2= =80=99. ((eq (car word-bounds) (1+ sentence-start)) (capitalize-word 1)))))))) ;;;###autoload (define-minor-mode auto-capitalize-mode "Automatically capitalize the start of a sentence." :init-value nil :lighter " Auto-Capitalize" :global nil (if auto-capitalize-mode (progn (add-hook 'post-self-insert-hook 'auto-capitalize--run nil t)) (remove-hook 'post-self-insert-hook 'auto-capitalize--run t))) ;;;###autoload (define-globalized-minor-mode global-auto-capitalize-mode auto-capitalize-mode auto-capitalize-mode) (provide 'auto-capitalize-mode) ;;; auto-capitalize-mode.el ends here --=-=-= Content-Type: text/plain -- Ian Dunn --=-=-=--