diff options
| author | Michal Nazarewicz | 2014-10-15 10:01:26 +0200 |
|---|---|---|
| committer | Michal Nazarewicz | 2015-01-20 13:55:02 +0100 |
| commit | 571441fc790fadfdc48a6287328828495719e990 (patch) | |
| tree | 42be47520cddbf1f0f96bcab4ddfef4cade0d155 /lisp | |
| parent | f9775f21fcddd3d3715cb3249090d99322488a45 (diff) | |
| download | emacs-571441fc790fadfdc48a6287328828495719e990.tar.gz emacs-571441fc790fadfdc48a6287328828495719e990.zip | |
tildify: add `tildify-space' and `tildify-mode'
* lisp/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'
* tests/automated/tildify-tests.el (tildify-space-test--test):
A new helper function for testing `tildify-space' function.
(tildify-space-test-html, tildify-space-test-html-nbsp)
(tildify-space-test-xml, tildify-space-test-tex): New tests for
`tildify-space' function.
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 @@ | |||
| 1 | 2014-01-20 Michal Nazarewicz <mina86@mina86.com> | ||
| 2 | |||
| 3 | * textmodes/tildify.el (tildify-space): A new function | ||
| 4 | which can be used as a `post-self-insert-hook' to automatically | ||
| 5 | convert spaces into hard spaces. | ||
| 6 | (tildify-space-pattern): A new variable specifying pattern where | ||
| 7 | `tildify-space' should take effect. | ||
| 8 | (tildify-space-predicates): A new variable specifying list of | ||
| 9 | predicate functions that all must return non-nil for | ||
| 10 | `tildify-space' to take effect. | ||
| 11 | (tildify-space-region-predicate): A new functions meant to be | ||
| 12 | used as a predicate in `tildify-space-predicates' list. | ||
| 13 | (tildify-mode): A new minor mode enabling `tildify-space' as a | ||
| 14 | `post-self-insert-hook' | ||
| 15 | |||
| 1 | 2015-01-20 Daniel Colascione <dancol@dancol.org> | 16 | 2015-01-20 Daniel Colascione <dancol@dancol.org> |
| 2 | 17 | ||
| 3 | * vc/vc-dir.el (vc-dir): Default to repository root, not | 18 | * 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 @@ | |||
| 4 | 4 | ||
| 5 | ;; Author: Milan Zamazal <pdm@zamazal.org> | 5 | ;; Author: Milan Zamazal <pdm@zamazal.org> |
| 6 | ;; Michal Nazarewicz <mina86@mina86.com> | 6 | ;; Michal Nazarewicz <mina86@mina86.com> |
| 7 | ;; Version: 4.5.7 | 7 | ;; Version: 4.6 |
| 8 | ;; Keywords: text, TeX, SGML, wp | 8 | ;; Keywords: text, TeX, SGML, wp |
| 9 | 9 | ||
| 10 | ;; This file is part of GNU Emacs. | 10 | ;; This file is part of GNU Emacs. |
| @@ -401,6 +401,92 @@ replacements done and response is one of symbols: t (all right), nil | |||
| 401 | (t t)))))) | 401 | (t t)))))) |
| 402 | 402 | ||
| 403 | 403 | ||
| 404 | ;;; *** Tildify Mode *** | ||
| 405 | |||
| 406 | (defcustom tildify-space-pattern "[,:;(][ \t]*[a]\\|\\<[AIKOSUVWZikosuvwz]" | ||
| 407 | "Pattern specifying whether to insert a hard space at point. | ||
| 408 | |||
| 409 | If the pattern matches `looking-back', a hard space needs to be inserted instead | ||
| 410 | of a space at point. The regexp is always case sensitive, regardless of the | ||
| 411 | current `case-fold-search' setting." | ||
| 412 | :version "25.1" | ||
| 413 | :group 'tildify | ||
| 414 | :type 'string) | ||
| 415 | |||
| 416 | (defcustom tildify-space-predicates '(tildify-space-region-predicate) | ||
| 417 | "A list of predicate functions for `tildify-space' function." | ||
| 418 | :version "25.1" | ||
| 419 | :group 'tildify | ||
| 420 | :type '(repeat 'function)) | ||
| 421 | |||
| 422 | |||
| 423 | ;;;###autoload | ||
| 424 | (defun tildify-space () | ||
| 425 | "Convert space before point into a hard space if the context is right. | ||
| 426 | |||
| 427 | If | ||
| 428 | * character before point is a space character, | ||
| 429 | * character before that has “w” character syntax (i.e. it's a word | ||
| 430 | constituent), | ||
| 431 | * `tildify-space-pattern' matches when `looking-back' (no more than 10 | ||
| 432 | characters) from before the space character, and | ||
| 433 | * all predicates in `tildify-space-predicates' return non-nil, | ||
| 434 | replace the space character with a hard space specified by | ||
| 435 | `tildify-space-string' (note that the function does not take | ||
| 436 | `tildify-string-alist' into consideration). | ||
| 437 | |||
| 438 | Return t if conversion happened, nil otherwise. | ||
| 439 | |||
| 440 | This function is meant to be used as a `post-self-insert-hook'." | ||
| 441 | (interactive) | ||
| 442 | (let ((p (point)) case-fold-search) | ||
| 443 | (when (and (> (- p (point-min)) 2) | ||
| 444 | (eq (preceding-char) ?\s) | ||
| 445 | (eq (char-syntax (char-before (1- p))) ?w) | ||
| 446 | (not (string-equal " " tildify-space-string)) | ||
| 447 | (save-excursion | ||
| 448 | (goto-char (1- p)) | ||
| 449 | (looking-back tildify-space-pattern | ||
| 450 | (max (point-min) (- p 10)))) | ||
| 451 | (run-hook-with-args-until-failure 'tildify-space-predicates)) | ||
| 452 | (delete-char -1) | ||
| 453 | (insert tildify-space-string) | ||
| 454 | t))) | ||
| 455 | |||
| 456 | (defun tildify-space-region-predicate () | ||
| 457 | "Check whether character before point should be tildified. | ||
| 458 | Based on `tildify-foreach-region-function', check whether character before, | ||
| 459 | which is assumed to be a space character, should be replaced with a hard space." | ||
| 460 | (catch 'found | ||
| 461 | (tildify--foreach-region (lambda (_b _e) (throw 'found t)) (1- (point)) (point)))) | ||
| 462 | |||
| 463 | ;;;###autoload | ||
| 464 | (define-minor-mode tildify-mode | ||
| 465 | "Adds electric behaviour to space character. | ||
| 466 | |||
| 467 | When space is inserted into a buffer in a position where hard space is required | ||
| 468 | instead (determined by `tildify-space-pattern' and `tildify-space-predicates'), | ||
| 469 | that space character is replaced by a hard space specified by | ||
| 470 | `tildify-space-string'. Converting of the space is done by `tildify-space'. | ||
| 471 | |||
| 472 | When `tildify-mode' is enabled, if `tildify-string-alist' specifies a hard space | ||
| 473 | representation for current major mode, the `tildify-space-string' buffer-local | ||
| 474 | variable will be set to the representation." | ||
| 475 | nil " ~" nil | ||
| 476 | (when tildify-mode | ||
| 477 | (let ((space (tildify--pick-alist-entry tildify-string-alist))) | ||
| 478 | (if (not (string-equal " " (or space tildify-space-string))) | ||
| 479 | (when space | ||
| 480 | (setq tildify-space-string space)) | ||
| 481 | (message (eval-when-compile | ||
| 482 | (concat "Hard space is a single space character, tildify-" | ||
| 483 | "mode won't have any effect, disabling."))) | ||
| 484 | (setq tildify-mode nil)))) | ||
| 485 | (if tildify-mode | ||
| 486 | (add-hook 'post-self-insert-hook 'tildify-space nil t) | ||
| 487 | (remove-hook 'post-self-insert-hook 'tildify-space t))) | ||
| 488 | |||
| 489 | |||
| 404 | ;;; *** Announce *** | 490 | ;;; *** Announce *** |
| 405 | 491 | ||
| 406 | (provide 'tildify) | 492 | (provide 'tildify) |