From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.devel Subject: [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode'. Date: Sun, 2 Mar 2014 22:55:35 +0100 Message-ID: <1393797335-18125-6-git-send-email-mpn@google.com> References: <1393797335-18125-1-git-send-email-mpn@google.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1393797397 9346 80.91.229.3 (2 Mar 2014 21:56:37 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 2 Mar 2014 21:56:37 +0000 (UTC) To: emacs-devel@gnu.org, Stefan Monnier , =?UTF-8?q?Pavel=20Jan=C3=ADk?= , Milan Zamazal Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Mar 02 22:56:45 2014 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WKENI-0001fe-3H for ged-emacs-devel@m.gmane.org; Sun, 02 Mar 2014 22:56:44 +0100 Original-Received: from localhost ([::1]:36732 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKENH-0002uR-OS for ged-emacs-devel@m.gmane.org; Sun, 02 Mar 2014 16:56:43 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39639) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKEMU-0001c6-CJ for emacs-devel@gnu.org; Sun, 02 Mar 2014 16:55:59 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WKEMP-0008Hy-CH for emacs-devel@gnu.org; Sun, 02 Mar 2014 16:55:54 -0500 Original-Received: from mail-ea0-x236.google.com ([2a00:1450:4013:c01::236]:42278) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKEMP-0008Hc-0s for emacs-devel@gnu.org; Sun, 02 Mar 2014 16:55:49 -0500 Original-Received: by mail-ea0-f182.google.com with SMTP id b10so3029253eae.41 for ; Sun, 02 Mar 2014 13:55:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-type:content-transfer-encoding; bh=/Zny7bL5HecDCZf6/lrQl5SS69gEoUANUBW/Yai9moE=; b=Cprw8Lk0KMh82iUtmYRM6orxcWSSScwgwzGtMkf2vv3FVZczG/chJAkjSyhe6H90aN utrxMcjax4DtfqQTO3IG0b/W/t3Gj/xMRzqvBN0HsoV5HYoxYEYzqb7A9Dqvoab9l8uZ +5H6BU2M5/L+GRcq2D7XB+L61FV9ZJU2qSTZAgrlojHs12vA1NhFh8yC2hQ30ioIGlao A2yBposWQ84V5O+SKuwrAvanbgkpsPqB0TR8sHIhi4KeHmp4uLMMi/OHDMjPsATrNmTx 1vs4iTXK5LSlitzS/t7nzMl2TyMtePBGb6FEaGnyLUL6e7rIeaiztVsctDngHPUjmxvx z6mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=/Zny7bL5HecDCZf6/lrQl5SS69gEoUANUBW/Yai9moE=; b=fbsAPP/PIrws8v9Abc81N1lsfEimBmrfZw3TtHrvouZ5LdpdDAsmhPb/Kae8jsBmw3 6eCi6yxtp7WwuaixJy63tmb+TkO5WEJ6TYArTqaDx9cszscUDXHhGPzk5fv1UJw16b6+ 637Iz7aY8KMWSl19ANinaq/yzRLLgWdfeEblYy5Pen5BJKdo7sF8desCDlXxdOyjMm/8 clq5ifFyoxVSPnW2xlyQcIS7mbJ/rUXDkizKDTcx405TTRiO/ipW2mT7NZ+av7Rvz1WY PPPzZMk6KHMFxWsnQoORGduNcJl0QnBB011Eq0CDUUJbl2eZdfx1BZMkG/DYtDA2iDnm sblA== X-Gm-Message-State: ALoCoQn8oN47DJlQ9gUOlt6lKPGa8Xa8haRCLoAzw4mQWcOr9ieVbSoQ7wRNAPu+ANFaS3u4B9/vsLiqcmNy5FaxwbfVZeKkaKtzOyVBSDKIgyA8BKTNisUT5MdZ5m8t25cCGPuCYUfHufT8g3eM5FyvlITT3umFWfSTeWf8dziHDY7sYbjnyTzlZt2aOhQPA+XI92yrbAymRYjWMhOmq/e/RK7bDHYy/Q== X-Received: by 10.14.3.72 with SMTP id 48mr35885818eeg.34.1393797347990; Sun, 02 Mar 2014 13:55:47 -0800 (PST) Original-Received: from mpn-glaptop.zrh.mina86.com (178-82-34-113.dynamic.hispeed.ch. [178.82.34.113]) by mx.google.com with ESMTPSA id u6sm41551056eep.11.2014.03.02.13.55.46 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 02 Mar 2014 13:55:47 -0800 (PST) X-Mailer: git-send-email 1.9.0 In-Reply-To: <1393797335-18125-1-git-send-email-mpn@google.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::236 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 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-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:170080 Archived-At: From: Michal Nazarewicz The `auto-tildify' function can be used as a `post-self-insert-hook' to automatically convert spaces into hard spaces. It is configured via two new customize variables: `auto-tildify-pattern-alist' and `auto-tildify-check-envs'. `auto-tildify-mode' makes use of that function to enable eletric behaviour of space character. --- lisp/textmodes/tildify.el | 95 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index d18721a..9228bb6 100644 --- a/lisp/textmodes/tildify.el +++ b/lisp/textmodes/tildify.el @@ -4,7 +4,7 @@ ;; Author: Milan Zamazal ;; Michal Nazarewicz -;; Version: 4.5.4 +;; Version: 4.6 ;; Keywords: text, TeX, SGML, wp ;; This file is part of GNU Emacs. @@ -340,6 +340,99 @@ replacements done and response is one of symbols: t (all right), nil (t t)))))) +;;; *** Auto Tildify *** + +(defcustom auto-tildify-pattern-alist + '((t . "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]")) + "Alist specifying whether to insert hard space at point. + +Each alist item is of the form (MAJOR-MODE . REGEXP) or +\(MAJOR-MODE . SYMBOL). + +MAJOR-MODE defines major mode, for which the item applies. It can be either: +- a symbol equal to the major mode of the buffer to be fixed +- t for default item, this applies to all major modes not defined in another + alist item + +REGEXP is a regular expression matching the part of a text that +needs a hard space to be inserted instead of a space. The regexp +is always case sensitive, regardless of the current +`case-fold-search' setting. + +The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this +mode, the item for the mode SYMBOL is looked up in the alist instead." + :group 'tildify + :type '(repeat (cons (choice (const :tag "Default" t) + (symbol :tag "For mode ")) + (choice (regexp :tag "Regexp ") + (symbol :tag "Like mode"))))) + +(defcustom auto-tildify-check-envs t + "Should `auto-tildify' check if point is inside ignored environment." + :group 'tildify + :type 'boolean) + + +;;;###autoload +(defun auto-tildify () + "Convert space before point into a hard space if the context is right. + +If + * character before point is a space character, + * character before that has “w” character syntax (i.e. it's + a word constituent), + * pattern from `auto-tildify-pattern-alist' matches when + `looking-back' (no more than 10 characters) from before the space + character, and + * `auto-tildify-check-envs' is nil or point is not inside of an + environment to ignore +replace the space character with a hard space defined in +`auto-tildify-string'. + +Return t if conversion happened, nil otherwise. + +This function is meant to be used as a `post-self-insert-hook'." + (interactive) + (let (case-fold-search space pattern) + (when (and (> (- (point) (point-min)) 2) + (eq (preceding-char) ?\s) + (eq (char-syntax (char-before (1- (point)))) ?w) + (setq space (tildify-mode-alist tildify-string-alist)) + (not (string-equal " " space)) + (setq pattern (tildify-mode-alist auto-tildify-pattern-alist)) + (save-excursion + (goto-char (1- (point))) + (looking-back pattern (max (point-min) (- (point) 10)))) + (or (not auto-tildify-check-envs) + (let (found) + (tildify-foreach-region-outside-env + (- (point) 2) (1- (point)) + (lambda (beg end) (setq found t) nil)) + found))) + (delete-char -1) + (insert space) + t))) + +;;;###autoload +(define-minor-mode auto-tildify-mode + "Adds electric behaviour to space character. + +When space is inserted into a buffer in a position where hard +space is required instead, that space character is replaced by +a hard space correct for given mode. + +Converting of the space is done by `auto-tildify'." + nil " ~" nil + (when (let ((space (tildify-mode-alist tildify-string-alist))) + (or (not space) (string-equal " " space))) + (message "Hard space for %s is single space character, auto-tildify won't have any effect." major-mode)) + (when (not (tildify-mode-alist auto-tildify-pattern-alist)) + (message "No auto-pattern defined for %s, auto-tildify won't have any effect." major-mode)) + (if auto-tildify-mode + (add-hook 'post-self-insert-hook 'auto-tildify nil t) + (remove-hook 'post-self-insert-hook 'auto-tildify t))) + + ;;; *** Announce *** (provide 'tildify) -- 1.9.0.279.gdc9e3eb