diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/ChangeLog | 15 | ||||
-rw-r--r-- | lisp/textmodes/tildify.el | 88 |
2 files changed, 102 insertions, 1 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 22a06a9168c..8bcfe977a9c 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,18 @@ +2014-01-20 Michal Nazarewicz <mina86@mina86.com> + + * textmodes/tildify.el (tildify-space): A new function + which can be used as a `post-self-insert-hook' to automatically + convert spaces into hard spaces. + (tildify-space-pattern): A new variable specifying pattern where + `tildify-space' should take effect. + (tildify-space-predicates): A new variable specifying list of + predicate functions that all must return non-nil for + `tildify-space' to take effect. + (tildify-space-region-predicate): A new functions meant to be + used as a predicate in `tildify-space-predicates' list. + (tildify-mode): A new minor mode enabling `tildify-space' as a + `post-self-insert-hook' + 2015-01-20 Daniel Colascione <dancol@dancol.org> * vc/vc-dir.el (vc-dir): Default to repository root, not diff --git a/lisp/textmodes/tildify.el b/lisp/textmodes/tildify.el index 9382b32845d..b11d7dae272 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.7 +;; Version: 4.6 ;; Keywords: text, TeX, SGML, wp ;; This file is part of GNU Emacs. @@ -401,6 +401,92 @@ replacements done and response is one of symbols: t (all right), nil (t t)))))) +;;; *** Tildify Mode *** + +(defcustom tildify-space-pattern "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]" + "Pattern specifying whether to insert a hard space at point. + +If the pattern matches `looking-back', a hard space needs to be inserted instead +of a space at point. The regexp is always case sensitive, regardless of the +current `case-fold-search' setting." + :version "25.1" + :group 'tildify + :type 'string) + +(defcustom tildify-space-predicates '(tildify-space-region-predicate) + "A list of predicate functions for `tildify-space' function." + :version "25.1" + :group 'tildify + :type '(repeat 'function)) + + +;;;###autoload +(defun tildify-space () + "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), + * `tildify-space-pattern' matches when `looking-back' (no more than 10 + characters) from before the space character, and + * all predicates in `tildify-space-predicates' return non-nil, +replace the space character with a hard space specified by +`tildify-space-string' (note that the function does not take +`tildify-string-alist' into consideration). + +Return t if conversion happened, nil otherwise. + +This function is meant to be used as a `post-self-insert-hook'." + (interactive) + (let ((p (point)) case-fold-search) + (when (and (> (- p (point-min)) 2) + (eq (preceding-char) ?\s) + (eq (char-syntax (char-before (1- p))) ?w) + (not (string-equal " " tildify-space-string)) + (save-excursion + (goto-char (1- p)) + (looking-back tildify-space-pattern + (max (point-min) (- p 10)))) + (run-hook-with-args-until-failure 'tildify-space-predicates)) + (delete-char -1) + (insert tildify-space-string) + t))) + +(defun tildify-space-region-predicate () + "Check whether character before point should be tildified. +Based on `tildify-foreach-region-function', check whether character before, +which is assumed to be a space character, should be replaced with a hard space." + (catch 'found + (tildify--foreach-region (lambda (_b _e) (throw 'found t)) (1- (point)) (point)))) + +;;;###autoload +(define-minor-mode tildify-mode + "Adds electric behaviour to space character. + +When space is inserted into a buffer in a position where hard space is required +instead (determined by `tildify-space-pattern' and `tildify-space-predicates'), +that space character is replaced by a hard space specified by +`tildify-space-string'. Converting of the space is done by `tildify-space'. + +When `tildify-mode' is enabled, if `tildify-string-alist' specifies a hard space +representation for current major mode, the `tildify-space-string' buffer-local +variable will be set to the representation." + nil " ~" nil + (when tildify-mode + (let ((space (tildify--pick-alist-entry tildify-string-alist))) + (if (not (string-equal " " (or space tildify-space-string))) + (when space + (setq tildify-space-string space)) + (message (eval-when-compile + (concat "Hard space is a single space character, tildify-" + "mode won't have any effect, disabling."))) + (setq tildify-mode nil)))) + (if tildify-mode + (add-hook 'post-self-insert-hook 'tildify-space nil t) + (remove-hook 'post-self-insert-hook 'tildify-space t))) + + ;;; *** Announce *** (provide 'tildify) |