File: verbiste.el

package info (click to toggle)
verbiste 0.1.47-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,168 kB
  • sloc: xml: 24,823; sh: 12,426; ansic: 9,087; cpp: 4,483; makefile: 1,021; yacc: 288; perl: 281; lisp: 215; java: 47; sed: 16
file content (294 lines) | stat: -rw-r--r-- 9,449 bytes parent folder | download | duplicates (2)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; verbiste.el --- Emacs interface to verbiste
;;
;; git clone git://gaffer.ptitcanardnoir.org/verbiste-el.git
;; Heavily inspired from dict.el, by Max Vasin.
;;

;; Copyright (c) 2002, 2003 Max Vasin
;; Copyright (c) 2006, 2007, 2008 by Ben Voui <intrigeri@boum.org>

;; Version: 0.7

;; This program 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
;; of the License, or (at your option) any later version.
;;
;; This program 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 this program; if not, write to the Free Software Foundation, Inc.,
;; 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

;;; Code:
(defgroup Verbiste nil
  "Interface to the verbiste French conjugation system."
  :prefix "verbiste-"
  :group 'external)

(defcustom verbiste-always-quote-terms nil
  "If t verbiste will always quote terms."
  :type 'boolean
  :group 'Verbiste)

;;;;
;;;; Key binding customisation variables and helper functions
;;;;

(defun verbiste-set-key-binding (key value)
  "Stub for setting KEY binding to VALUE."
  (set-default key value))

(defun verbiste-set-enable-key-bindings (key value)
  "Stub for setting KEY binding to VALUE."
  (set-default key value))

(defun verbiste-mode-set-key-binding (key value)
  "Stub for setting KEY binding to VALUE."
  (set-default key value))

(defcustom verbiste-conjugate-key-binding "\\C-cvc"
  "Specifies a key binding to run french-conjugator and display
the results in the Emacs buffer."
  :type 'string
  :group 'Verbiste
  :set 'verbiste-set-key-binding
  :require 'verbiste)

(defcustom verbiste-deconjugate-key-binding "\\C-cvd"
  "Specifies a key binding to run french-deconjugator and display
the results in the Emacs buffer."
  :type 'string
  :group 'Verbiste
  :set 'verbiste-set-key-binding
  :require 'verbiste)

(defcustom verbiste-enable-key-bindings nil
  "Enables key bindings for verbiste.el commands."
  :type 'boolean
  :group 'Verbiste
  :set 'verbiste-set-enable-key-bindings
  :require 'verbiste)

(defgroup Verbiste-Mode nil
  "Verbiste-mode key bindings"
  :tag "Verbiste-mode"
  :prefix "verbiste-mode-"
  :group 'Verbiste)

(defcustom verbiste-mode-conjugate-key-binding "c"
  "Specifies a key binding to run french-conjugator and display
the results in the Emacs buffer."
  :tag "Conjugate"
  :type 'string
  :group 'Verbiste-Mode
  :set 'verbiste-mode-set-key-binding
  :require 'verbiste)

(defcustom verbiste-mode-deconjugate-key-binding "d"
  "Specifies a key binding to run french-deconjugator and display
the results in the Emacs buffer."
  :tag "Deconjugate"
  :type 'string
  :group 'Verbiste-Mode
  :set 'verbiste-mode-set-key-binding
  :require 'verbiste)

(defcustom verbiste-buffer-coding-system 'utf-8
  "Specifies coding system to use in verbiste buffer.
Depending on your Emacs version and locales settings, you may want to change this, e.g. for 'latin-1"
  :tag "Input coding system for Verbiste buffer"
  :type 'sexp
  :group 'Verbiste-Mode)

(defcustom verbiste-mode-hook nil
  "*Hook run by `verbiste-mode'."
  :type  'hook
  :group 'Verbiste-Mode)

(defun verbiste-quote (word)
  "Quote WORD if necessary."
  (if verbiste-always-quote-terms
      (if (or (and (eq (aref word 0) ?\")
		   (eq (aref word (- (length word) 1)) ?\"))
	      (and (eq (aref word 0) ?\')
		   (eq (aref word (- (length word) 1)) ?\')))
	  word
	(concat "'" word "'"))
    word))

(defun verbiste-get-answer (what deconj)
  "Receive the conjugation of WHAT from the verbiste and insert
in the buffer.
If DECONJ is non-nil, deconjugate instead"
  (let* ((word what)
	 (buffer-name (concat "*VERBISTE " word "*"))
	 (buffer (or (get-buffer buffer-name) (generate-new-buffer buffer-name)))
	 (coding-system-for-read verbiste-buffer-coding-system)
	 (coding-system-for-write verbiste-buffer-coding-system))
    (save-excursion
      (set-buffer buffer)
      (kill-region (point-min) (point-max))
      (verbiste-mode))
    (message "%s %s in the background"
	     (if deconj "Deconjugating" "Conjugating")
	     word)
    (set-process-sentinel
     (apply 'start-process "verbiste" buffer
            (append
             (if deconj
                 '("french-deconjugator")
               '("french-conjugator" "--pronouns"))
             (list word)))
     'verbiste-bgproc-sentinel)))

(defun verbiste-bgproc-sentinel (process msg)
  "Verbiste background PROCESS sentinel (handling MSG)."
  (let ((buffer (process-buffer process)))
    (cond
     ((string= "finished\n" msg)
      (save-excursion (set-buffer buffer)
		      (goto-char (point-min))
		      (display-buffer buffer)))
     ((string-match "exited abnormally with code" msg)
      (message msg)))))

(defsubst verbiste-default-verbiste-entry ()
  "Make a guess at a default verbiste entry.
This guess is based on the text surrounding the cursor."
  (let ((word (or (current-word)
                  "")))
    (if (string-match "[._]+$" word)
        (substring word 0 (match-beginning 0))
      word)))

;;;;
;;;; Lookup functions
;;;;

(defun verbiste-conjugate (verb)
  "Conjugate a VERB using Verbiste."
  (interactive (list (let* ((default-entry (verbiste-default-verbiste-entry))
	     (input (read-string
		     (format "Verb%s: "
			     (if (string= default-entry "")
				 ""
			       (format " (default %s)" default-entry))))))
	(if (string= input "")
	    (if (string= default-entry "")
		(error "No verbiste args given") default-entry) input))))
  (verbiste-get-answer verb nil))

(defun verbiste-deconjugate (word)
  "Deconjugate a WORD using Verbiste."
  (interactive (list (let* ((default-entry (verbiste-default-verbiste-entry))
	     (input (read-string
		     (format "Word%s: "
			     (if (string= default-entry "")
				 ""
			       (format " (default %s)" default-entry))))))
	(if (string= input "")
	    (if (string= default-entry "")
		(error "No verbiste args given") default-entry) input))))
  (verbiste-get-answer word t))

(defun verbiste-set-key-binding (key value)
  "Set KEY binding customisation variable to VALUE."
  (let ((result (set-default key value)))
    (set-default 'verbiste-enable-key-bindings t)
    (verbiste-update-key-bindings)
    result))

(defun verbiste-set-enable-key-bindings (key value)
  "Set KEY to VALUE and update verbiste key bindings."
  (let ((result (set-default key value)))
    (verbiste-update-key-bindings)
    result))

(defun verbiste-process-key-binding (string)
  "Process a STRING representing a key binding to allow easy key
  binding customisation."
  (read (concat "\"" string "\"")))

(defvar verbiste-mode-keymap (make-sparse-keymap))

(defun verbiste-mode-set-key-binding (key value)
  "Set KEY binding customisation variable to VALUE (for Verbiste-mode)."
  (let ((result (set-default key value)))
    (verbiste-mode-update-key-bindings)
    result))

(defun verbiste-mode()
  "Major mode for Verbiste output.

\\{verbiste-mode-keymap}"

  (use-local-map verbiste-mode-keymap)
  (setq mode-name "Verbiste")
  (setq major-mode 'verbiste-mode)
  (setq buffer-read-only t)

  (if (eval-when-compile (fboundp 'run-mode-hooks))
      (run-mode-hooks 'verbiste-mode-hook) ;; xemacs21,emacs22 up
    (run-hooks 'verbiste-mode-hook)))

;; for `M-x customize-mode' in a verbiste-mode buffer, because the group is
;; not called lower-case `verbiste-mode'
(put 'verbiste-mode 'custom-mode-group 'Verbiste-Mode)

(defun verbiste-update-key-bindings ()
  "Update verbiste key bindings."
  (when verbiste-enable-key-bindings
    ;; Setup global key binding `C-c v c' for running french-conjugator...
    (global-set-key (verbiste-process-key-binding
		     verbiste-conjugate-key-binding)
		    'verbiste-conjugate)
    ;; ... `C-c v d' for running french-deconjugator...
    (global-set-key (verbiste-process-key-binding
		     verbiste-deconjugate-key-binding)
		    'verbiste-deconjugate)))

(defun verbiste-mode-update-key-bindings ()
  "Update verbiste key bindings for Verbiste-mode."
  ;; Setup Verbiste-mode key binding `c' for running french-conjugator...
  (define-key verbiste-mode-keymap (verbiste-process-key-binding
				    verbiste-mode-conjugate-key-binding)
                                   'verbiste-conjugate)
  ;; ... `d' for running french-deconjugator...
  (define-key verbiste-mode-keymap (verbiste-process-key-binding
				    verbiste-mode-deconjugate-key-binding)
                                   'verbiste-deconjugate))

;;;;
;;;; Informational functions
;;;;

(defun verbiste-version ()
  "Display verbiste version information."
  (interactive)
  (shell-command "french-conjugator --version"))

(defconst verbiste-version
  "$Rev: 7 $"
  "Version number for 'verbiste' package.")

(defun verbiste-version-number ()
  "Return 'verbiste' version number."
  (string-match "[0123456789.]+" verbiste-version)
  (match-string 0 verbiste-version))

(defun verbiste-display-version ()
  "Display 'verbiste' version."
  (interactive)
  (message "Emacs Verbiste client version <%s>." (verbiste-version-number)))

(verbiste-update-key-bindings)
(verbiste-mode-update-key-bindings)

(provide 'verbiste)

;;; verbiste.el ends here