aboutsummaryrefslogtreecommitdiff
path: root/elisp/ellinika-dict-mode.el
blob: eefec368a3095505ca8e162b6b8fec17c0f68dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;; ellinika-dict-mode.el --- major mode for editing Ellinika dictionary files

;; Authors: 2004 Sergey Poznyakoff
;; Version:  1.0
;; Keywords: Ellinika, greek, dictionary
;; $Id$

;; This file is part of Ellinika.
;; Copyright (C) 2004 Sergey Poznyakoff.

;; Ellinika is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; Ellinika is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with Ellinika; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

(eval-when-compile
  ;; We use functions from these modules
  (mapcar 'require '(ellinika-mode)))

(defun ellinika-dict-guess-syntax ()
  (save-excursion
    (catch 'loop
      (while (search-backward-regexp "<\\(/?[^ >]+\\)\\(\\s +[^>]*\\)?>"
				     nil t)
	(let ((tag (buffer-substring
		    (match-beginning 1)
		    (match-end 1))))
	  (cond
	   ((or (string-equal tag "T") (string-equal tag "/T"))
	    (throw 'loop (ellinika-dict-guess-syntax)))
	   ((looking-at "</[MKXPF]>")
	    (throw 'loop 'node))
	   ((or (looking-at "</NODE>") (looking-at "<DICT>"))
	    (throw 'loop 'initial))
	   ((looking-at "<M>")
	    (throw 'loop 'alternative-input))
	   ((looking-at "<[KX]>")
	    (throw 'loop 'greek-input))
	   ((looking-at "<[PF]")
	    (throw 'loop 'greek-input))
	   ((looking-at "<[CE]>")) ;; continue
	   (t
	    (throw 'loop (intern tag))))))
      'initial)))
  
(defun ellinika-dict-select-input-method nil
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'ascii-input)
      (inactivate-input-method))
     ((eq syntax 'alternative-input)
      (alt-input nil))
     ((eq syntax 'greek-input)
      (greek-input nil))
     ((or (eq syntax 'node) (eq syntax 'initial))
      (inactivate-input-method)))))

(defun ellinika-dict-newline (arg)
  (interactive "p")
  (ellinika-dict-select-input-method)
  (newline-and-indent))

(defun ellinika-dict-begin-node (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'initial)
      (beginning-of-line)
      (insert "<NODE>\n <K></K>\n</NODE>")
      (forward-line -1)
      (beginning-of-line)
      (forward-char 4)
      (greek-input nil))
     (t
      (message "Cannot start node here")))))

(defun ellinika-dict-init-block (tag &optional input-method)
  (beginning-of-line)
  (if (string-equal (ellinika-find-open-tag) "P")
      (insert " "))
  (insert " <" tag "></" tag ">")
  (backward-char (+ (length tag) 3))
  (when input-method
    (set-input-method input-method)))

(defun ellinika-dict-begin-article (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "M" (if (boundp 'alternative-input-method)
				   alternative-input-method
				 nil)))
     (t
      (message "Cannot start key here")))))

(defun ellinika-dict-begin-key (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "K" 'greek))
     (t
      (message "Cannot start key here")))))

(defun ellinika-dict-begin-pos (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "P" 'greek))
     (t
      (message "Cannot start pos here")))))

(defun ellinika-dict-begin-forms (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "F" 'greek))
     (t
      (message "Cannot start forms here")))))

(defun ellinika-dict-begin-xref (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "X" 'greek))
     (t
      (message "Cannot start xref here")))))

(defun ellinika-dict-begin-ant (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'node)
      (ellinika-dict-init-block "A" 'greek))
     (t
      (message "Cannot start antonym here")))))

(defun ellinika-dict-begin-topic (arg)
  (interactive "p")
  (let ((syntax (ellinika-dict-guess-syntax)))
    (cond
     ((eq syntax 'initial)
      (insert "<T ID=\"\">\n</T>")
      (forward-line -1)
      (forward-char 7)
      (greek-input nil))
     ((eq syntax 'node)
      (insert "<T ID=\"\" />")
      (backward-char 4))
     (t
      (message "Cannot start topic here")))))

(defun ellinika-dict-begin-comment (arg)
  (interactive "p")
  (let ((tag (ellinika-find-open-tag)))
    (cond
     ((or (string-equal tag "M")
	  (string-equal tag "F"))
      (insert "<C></C>")
      (backward-char 4))
     (t
      (message "Cannot start comment here")))))

(defun ellinika-dict-begin-expl (arg)
  (interactive "p")
  (let ((tag (ellinika-find-open-tag)))
    (cond
     ((or (string-equal tag "M")
	  (string-equal tag "F"))
      (insert "<E></E>")
      (backward-char 4))
     (t
      (message "Cannot start explanation here")))))

;;;###autoload
(define-derived-mode ellinika-dict-mode ellinika-mode "Ellinika-Dict"
  "Major mode for editing Ellinika dictionary sources.

  Key bindings:
\\{ellinika-dict-mode-map}"

  (ellinika-set-close-tag-hook 'ellinika-dict-select-input-method)
  
  (define-key ellinika-dict-mode-map "\C-c\C-b" 'ellinika-dict-begin-node)
  (define-key ellinika-dict-mode-map "\C-c\C-m" 'ellinika-dict-begin-article)
  (define-key ellinika-dict-mode-map "\C-c\C-k" 'ellinika-dict-begin-key)
  (define-key ellinika-dict-mode-map "\C-c\C-p" 'ellinika-dict-begin-pos)
  (define-key ellinika-dict-mode-map "\C-c\C-f" 'ellinika-dict-begin-forms)
  (define-key ellinika-dict-mode-map "\C-c\C-x" 'ellinika-dict-begin-xref)
  (define-key ellinika-dict-mode-map "\C-c\C-a" 'ellinika-dict-begin-ant)
  (define-key ellinika-dict-mode-map "\C-c\C-t" 'ellinika-dict-begin-topic)
  (define-key ellinika-dict-mode-map "\C-c\C-c" 'ellinika-dict-begin-comment)
  (define-key ellinika-dict-mode-map "\C-c\C-e" 'ellinika-dict-begin-expl)
  (define-key ellinika-dict-mode-map "\C-m" 'ellinika-dict-newline))

(provide 'ellinika-dict-mode)

;;;; End of ellinika-dict-mode.el

Return to:

Send suggestions and report system problems to the System administrator.