From: Michal Nazarewicz <mpn@google.com>
To: emacs-devel@gnu.org, "Stefan Monnier" <monnier@iro.umontreal.ca>,
"Pavel Janík" <Pavel@Janik.cz>, "Milan Zamazal" <pdm@zamazal.org>
Subject: [PATCH 5/5] tildify.el: Add `auto-tildify' and `auto-tildify-mode'.
Date: Sun, 2 Mar 2014 22:55:35 +0100 [thread overview]
Message-ID: <1393797335-18125-6-git-send-email-mpn@google.com> (raw)
In-Reply-To: <1393797335-18125-1-git-send-email-mpn@google.com>
From: Michal Nazarewicz <mina86@mina86.com>
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 <pdm@zamazal.org>
;; Michal Nazarewicz <mina86@mina86.com>
-;; 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
prev parent reply other threads:[~2014-03-02 21:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-02 21:55 [PATCH 0/5] Auto tildify improvements Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 1/5] tildify.el: Improve defcustom's types Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 2/5] tildify.el: Change XML hard space to numeric reference Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 3/5] tildify.el: Optimise environments regexes Michal Nazarewicz
2014-03-02 21:55 ` [PATCH 4/5] tildify.el: Rewrite `tildify-region' and co., add foreach function Michal Nazarewicz
2014-03-02 21:55 ` Michal Nazarewicz [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1393797335-18125-6-git-send-email-mpn@google.com \
--to=mpn@google.com \
--cc=Pavel@Janik.cz \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=pdm@zamazal.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.