Skip to content

Commit c3c85a2

Browse files
committed
move yas integration into its own file, requiring yasnippet there
removes some compilation-warnings
1 parent 441bbc8 commit c3c85a2

File tree

2 files changed

+168
-130
lines changed

2 files changed

+168
-130
lines changed

dix-yas.el

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
;;; dix-yas.el --- optional yas-integration with dix.el
2+
3+
;; Copyright (C) 2015-2016 Kevin Brubeck Unhammer
4+
5+
;; Author: Kevin Brubeck Unhammer <unhammer@fsfe.org>
6+
;; Keywords: languages
7+
8+
;; This file is not part of GNU Emacs.
9+
10+
;; This program is free software; you can redistribute it and/or modify
11+
;; it under the terms of the GNU General Public License as published by
12+
;; the Free Software Foundation; either version 2, or (at your option)
13+
;; any later version.
14+
;;
15+
;; This program is distributed in the hope that it will be useful,
16+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
;; GNU General Public License for more details.
19+
;;
20+
;; You should have received a copy of the GNU General Public License
21+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
23+
;;; Commentary:
24+
25+
;; Extensions to dix.el for using it with Yasnippet.
26+
27+
;; Usage:
28+
29+
;; (require 'dix-yas)
30+
;; (add-hook 'dix-mode-hook #'dix-maybe-yas-activate)
31+
32+
;;; Code:
33+
34+
(require 'dix)
35+
(require 'yasnippet)
36+
37+
;;;============================================================================
38+
;;;
39+
;;; Yasnippet helpers
40+
;;;
41+
42+
43+
;;; Note, due to
44+
;;; https://github.com/AndreaCrotti/yasnippet-snippets/issues/41 you
45+
;;; should do (remhash 'nxml-mode yas--tables) after (yas-reload-all)
46+
47+
(defun dix-maybe-yas-activate ()
48+
"Turn on `yas-minor-mode' in `dix-mode' if possible."
49+
(when (fboundp 'yas-activate-extra-mode)
50+
(yas-activate-extra-mode 'dix-mode)))
51+
52+
;;; In case yasnippet is lazy-loaded after dix-mode, ensure dix-mode
53+
;;; snippets are used:
54+
(eval-after-load 'yasnippet
55+
'(mapc (lambda (buf)
56+
(when dix-mode (dix-maybe-yas-activate)))
57+
(buffer-list)))
58+
59+
60+
61+
(defvar-local dix-yas-key-rex ""
62+
"Used by `dix-yas-update-key-rex' for caching the regex-opt of
63+
possible snippet keys.")
64+
(defvar-local dix-yas-key-rex-tables nil
65+
"Used by `dix-yas-update-key-rex' for checking if we need to
66+
update `dix-yas-key-rex'.")
67+
68+
(defun dix-yas-update-key-rex ()
69+
"Update `dix-yas-key-rex', used by `dix-yas-skip-backwards-to-key'."
70+
(let ((tables (yas--get-snippet-tables)))
71+
(unless (equal tables dix-yas-key-rex-tables)
72+
;; Only update key-rex if tables changed (but is this equal test slow?):
73+
(setq dix-yas-key-rex-tables tables)
74+
(setq dix-yas-key-rex
75+
(let (keys) (mapc
76+
(lambda (table)
77+
(let* ((keyhash (yas--table-hash table)))
78+
(when keyhash
79+
(maphash (lambda (k v) (push k keys)) keyhash))))
80+
dix-yas-key-rex-tables)
81+
(concat (regexp-opt keys) "$"))))))
82+
83+
(defun dix-yas-skip-backwards-to-key (start-point)
84+
"Skip backwards to the first possible yasnippet key.
85+
86+
This is meant to be used in `yas-key-syntaxes' (which see for
87+
information on START-POINT), since the defaults don't let you
88+
expand e.g. \"<s>\" without having whitespace before it. To use
89+
this function, put the following in your init file:
90+
91+
(eval-after-load 'yasnippet
92+
'(add-to-list 'yas-key-syntaxes 'dix-yas-skip-backwards-to-key))
93+
94+
Only has an effect in `dix-mode' so the above shouldn't change
95+
how yasnippet expansion works in other modes."
96+
(when dix-mode
97+
(dix-yas-update-key-rex)
98+
(let* ((linebeg (save-excursion (goto-char start-point)
99+
(line-beginning-position)))
100+
(haystack (buffer-substring-no-properties linebeg start-point)))
101+
(when (string-match dix-yas-key-rex haystack)
102+
(goto-char (+ linebeg (match-beginning 0)))))))
103+
104+
105+
106+
(defun dix-yas-prev-lemma ()
107+
(dix-yas-prev-thing "lemma" 'dix-lemma-at-point))
108+
109+
(defun dix-yas-prev-par ()
110+
(dix-yas-prev-thing "lemma__POS" 'dix-par-at-point))
111+
112+
(defun dix-yas-prev-thing (default thing-at-point-fn)
113+
(condition-case nil
114+
(save-excursion
115+
(dix-up-to "e" "section")
116+
(condition-case nil
117+
(progn
118+
(dix-with-sexp (backward-sexp))
119+
(let ((thing (funcall thing-at-point-fn)))
120+
(if (string-match "^$\\|^\\$" thing)
121+
default
122+
thing)))
123+
(error default)))
124+
(dix-parse-error default)))
125+
126+
(defun dix-yas-message-pardef (pdname)
127+
"Just show the full pardef of `PDNAME' at point in *Messages* buffer."
128+
(save-excursion
129+
(save-restriction
130+
(widen)
131+
(dix-goto-pardef pdname)
132+
(let* ((beg (point))
133+
(end (1+ (nxml-scan-element-forward beg))))
134+
(message (buffer-substring-no-properties beg end)))))
135+
pdname)
136+
137+
(defun dix-yas-pdname-to-pos (string)
138+
(if (numberp (string-match "__\\([^_]+\\)$" string))
139+
(format "<s n=\"%s\"/>" (match-string 1 string))
140+
""))
141+
142+
(defun dix-yas-pdname-to-suffix (string)
143+
(if (numberp (string-match "/\\([^_]+\\)" string))
144+
(match-string-no-properties 1 string)
145+
""))
146+
147+
(defun dix-yas-fix-suffix-w/pdname ()
148+
(let* ((suffix (dix-yas-pdname-to-suffix yas-text))
149+
(suffix/i-rex (concat (regexp-quote suffix) "</i>")))
150+
(when (re-search-backward suffix/i-rex (line-beginning-position) 'noerror)
151+
(replace-match "</i>" 'fixedcase 'literal))))
152+
153+
(defun dix-yas-choose-pdname ()
154+
(when yas-moving-away-p
155+
(dix-yas-fix-suffix-w/pdname))
156+
(dix-yas-message-pardef
157+
(yas-choose-value
158+
(dix-pardef-suggest-for (dix-lemma-at-point)))))
159+
160+
(defun dix-yas-lm-to-i ()
161+
(replace-regexp-in-string " " "<b/>" yas-text))
162+
163+
(provide 'dix-yas)
164+
165+
;;;============================================================================
166+
167+
;;; dix-yas.el ends here

dix.el

+1-130
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
;; do with spaces
104104
;; - `dix-reverse' should be able to reverse on a regexp match, so
105105
;; that we can do `dix-suffix-sort' by eg. <l>-elements.
106-
;; - yas-related code in another file that requires yas
107106

108107
;;; Code:
109108

@@ -142,9 +141,7 @@ Entering dix-mode calls the hook dix-mode-hook.
142141
:init-value nil
143142
:lighter " dix"
144143
:keymap dix-mode-map
145-
:require nxml-mode
146-
147-
(dix-maybe-yas-activate))
144+
:require nxml-mode)
148145

149146

150147
;;;============================================================================
@@ -668,132 +665,6 @@ edit the paths, and add the path to the list
668665
(add-hook 'dix-load-hook #'dix-schemas)
669666

670667

671-
;;;============================================================================
672-
;;;
673-
;;; Yasnippet helpers
674-
;;;
675-
676-
677-
;;; Note, due to
678-
;;; https://github.com/AndreaCrotti/yasnippet-snippets/issues/41 you
679-
;;; should do (remhash 'nxml-mode yas--tables) after (yas-reload-all)
680-
681-
(defun dix-maybe-yas-activate ()
682-
"Turn on `yas-minor-mode' in `dix-mode' if possible."
683-
(when (fboundp 'yas-activate-extra-mode)
684-
(yas-activate-extra-mode 'dix-mode)))
685-
686-
;;; In case yasnippet is lazy-loaded after dix-mode, ensure dix-mode
687-
;;; snippets are used:
688-
(eval-after-load 'yasnippet
689-
'(mapc (lambda (buf)
690-
(when dix-mode (dix-maybe-yas-activate)))
691-
(buffer-list)))
692-
693-
694-
695-
(defvar-local dix-yas-key-rex ""
696-
"Used by `dix-yas-update-key-rex' for caching the regex-opt of
697-
possible snippet keys.")
698-
(defvar-local dix-yas-key-rex-tables nil
699-
"Used by `dix-yas-update-key-rex' for checking if we need to
700-
update `dix-yas-key-rex'.")
701-
702-
(defun dix-yas-update-key-rex ()
703-
"Update `dix-yas-key-rex', used by `dix-yas-skip-backwards-to-key'."
704-
(let ((tables (yas--get-snippet-tables)))
705-
(unless (equal tables dix-yas-key-rex-tables)
706-
;; Only update key-rex if tables changed (but is this equal test slow?):
707-
(setq dix-yas-key-rex-tables tables)
708-
(setq dix-yas-key-rex
709-
(let (keys) (mapc
710-
(lambda (table)
711-
(let* ((keyhash (yas--table-hash table)))
712-
(when keyhash
713-
(maphash (lambda (k v) (push k keys)) keyhash))))
714-
dix-yas-key-rex-tables)
715-
(concat (regexp-opt keys) "$"))))))
716-
717-
(defun dix-yas-skip-backwards-to-key (start-point)
718-
"Skip backwards to the first possible yasnippet key.
719-
720-
This is meant to be used in `yas-key-syntaxes' (which see for
721-
information on START-POINT), since the defaults don't let you
722-
expand e.g. \"<s>\" without having whitespace before it. To use
723-
this function, put the following in your init file:
724-
725-
(eval-after-load 'yasnippet
726-
'(add-to-list 'yas-key-syntaxes 'dix-yas-skip-backwards-to-key))
727-
728-
Only has an effect in `dix-mode' so the above shouldn't change
729-
how yasnippet expansion works in other modes."
730-
(when dix-mode
731-
(dix-yas-update-key-rex)
732-
(let* ((linebeg (save-excursion (goto-char start-point)
733-
(line-beginning-position)))
734-
(haystack (buffer-substring-no-properties linebeg start-point)))
735-
(when (string-match dix-yas-key-rex haystack)
736-
(goto-char (+ linebeg (match-beginning 0)))))))
737-
738-
739-
740-
(defun dix-yas-prev-lemma ()
741-
(dix-yas-prev-thing "lemma" 'dix-lemma-at-point))
742-
743-
(defun dix-yas-prev-par ()
744-
(dix-yas-prev-thing "lemma__POS" 'dix-par-at-point))
745-
746-
(defun dix-yas-prev-thing (default thing-at-point-fn)
747-
(condition-case nil
748-
(save-excursion
749-
(dix-up-to "e" "section")
750-
(condition-case nil
751-
(progn
752-
(dix-with-sexp (backward-sexp))
753-
(let ((thing (funcall thing-at-point-fn)))
754-
(if (string-match "^$\\|^\\$" thing)
755-
default
756-
thing)))
757-
(error default)))
758-
(dix-parse-error default)))
759-
760-
(defun dix-yas-message-pardef (pdname)
761-
"Just show the full pardef of `PDNAME' at point in *Messages* buffer."
762-
(save-excursion
763-
(save-restriction
764-
(widen)
765-
(dix-goto-pardef pdname)
766-
(let* ((beg (point))
767-
(end (1+ (nxml-scan-element-forward beg))))
768-
(message (buffer-substring-no-properties beg end)))))
769-
pdname)
770-
771-
(defun dix-yas-pdname-to-pos (string)
772-
(if (numberp (string-match "__\\([^_]+\\)$" string))
773-
(format "<s n=\"%s\"/>" (match-string 1 string))
774-
""))
775-
776-
(defun dix-yas-pdname-to-suffix (string)
777-
(if (numberp (string-match "/\\([^_]+\\)" string))
778-
(match-string-no-properties 1 string)
779-
""))
780-
781-
(defun dix-yas-fix-suffix-w/pdname ()
782-
(let* ((suffix (dix-yas-pdname-to-suffix yas-text))
783-
(suffix/i-rex (concat (regexp-quote suffix) "</i>")))
784-
(when (re-search-backward suffix/i-rex (line-beginning-position) 'noerror)
785-
(replace-match "</i>" 'fixedcase 'literal))))
786-
787-
(defun dix-yas-choose-pdname ()
788-
(when yas-moving-away-p
789-
(dix-yas-fix-suffix-w/pdname))
790-
(dix-yas-message-pardef
791-
(yas-choose-value
792-
(dix-pardef-suggest-for (dix-lemma-at-point)))))
793-
794-
(defun dix-yas-lm-to-i ()
795-
(replace-regexp-in-string " " "<b/>" yas-text))
796-
797668
;;;============================================================================
798669
;;;
799670
;;; Interactive functions

0 commit comments

Comments
 (0)