forked from cheusov/dictem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictem.el
2025 lines (1739 loc) · 58.9 KB
/
dictem.el
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; dictem.el - DICT protocol client (rfc-2229) for [X]Emacs
; This code was initially based on
; dictionary.el written by Torsten Hilbrich <[email protected]>
; but now probably doesn't contain original code.
; Most of the code has been written
; from scratch by Aleksey Cheusov <[email protected]>, 2004-2008.
;
; DictEm 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.
;
; DictEm 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; NOTE! Documentation is in README file.
;
; Latest information about dictem project and sources
; are available at
;
; http://freshmeat.net/projects/dictem
; http://sourceforge.net/projects/dictem
; http://mova.org/~cheusov/pub/dictem
;
(require 'cl)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Custom Things ;;;;;
(defgroup dictem nil
"Client for accessing the DICT server."
:tag "DictEm"
:group 'help
:group 'hypermedia)
(defgroup dictem-faces nil
"Face options for dictem DICT client."
:tag "DictEm faces"
:group 'dictem
:group 'faces)
(defcustom dictem-server nil
"The DICT server"
:group 'dictem
:type '(restricted-sexp :match-alternatives (stringp 'nil)))
(defcustom dictem-port 2628
"The port of the DICT server"
:group 'dictem
:type 'number)
(defcustom dictem-client-prog "dict"
"The command line DICT client.
dictem accesses DICT server through this executable.
dict-1.9.14 or later (or compatible) is strongly recomented."
:group 'dictem
:type 'string)
(defcustom dictem-client-prog-args-list nil
"A list of additional arguments (strings) passed to dict client.
For example '(\"--some-option\")."
:group 'dictem
:type 'list)
(defcustom dictem-option-mime nil
"If `t' the OPTION MIME command (see RFC-2229 for details)
will be sent to the DICT server. i.e. \"dict\" program
will be run with \"-M\" option.
As a result server's response will be prepanded with MIME header
followed by a blank line.
Because of bugs in dict -M (version < 1.10.3) utility,
dict-1.10.3 or later is strongly recommended
"
:group 'dictem
:type 'boolean)
(defcustom dictem-default-strategy nil
"The default search strategy."
:group 'dictem
:type 'string)
(defcustom dictem-default-database nil
"The default database name."
:group 'dictem
:type 'string)
(defcustom dictem-user-databases-alist
nil
"ALIST of user's \"virtual\"databases.
Valid value looks like this:
'((\"en-ru\" . (\"mueller7\" \"korolew_en-ru\"))
((\"en-en\" . (\"foldoc\" \"gcide\" \"wn\")))
((\"gazetteer\" . \"gaz\")))
"
:group 'dictem
:type '(alist :key-type string))
(defcustom dictem-exclude-databases
nil
"ALIST of regexps for databases
that will not appear in autocompletion list.
"
:group 'dictem
:type '(alist :key-type string))
(defcustom dictem-use-user-databases-only
nil
"If `t', only user's dictionaries from dictem-user-databases-alist
will be used by dictem-select-database"
:group 'dictem
:type 'boolean)
(defcustom dictem-mode-hook
nil
"Hook run in dictem mode buffers."
:group 'dictem
:type 'hook)
(defcustom dictem-use-existing-buffer
nil
"If `t' the `dictem-run' function will not create new *dictem* buffer.
Instead, existing buffer will be erased and used to show results.
"
:group 'dictem
:type 'boolean)
(defcustom dictem-empty-initial-input
nil
"If `t' the `dictem-read-query' leave initial input empty"
:group 'dictem
:type 'boolean)
(defcustom dictem-use-content-history t
"If not nil and dictem-use-existing-buffer is also not nil,
buffer content and (point) is saved in dictem-content-history variable
when DEFINE hyperlinks are accessed.
It is restored by dictem-last function.
On slow machines it may better to set this variable to nil"
:group 'dictem)
;;;;; Faces ;;;;;
(defface dictem-reference-definition-face
'((((background light)) (:foreground "blue"))
(((background dark)) (:foreground "cyan")))
"The face that is used for displaying a reference to
a phrase in a DEFINE search."
:group 'dictem-faces)
(defface dictem-reference-m1-face
'((((background light)) (:foreground "darkgreen"))
(((background dark)) (:foreground "lightblue")))
"The face that is used for displaying a reference to
a phrase in a MATCH search."
:group 'dictem-faces)
(defface dictem-reference-m2-face
'((((background light)) (:foreground "blue"))
(((background dark)) (:bold true :foreground "gray")))
"The face that is used for displaying a reference to
a single word in a MATCH search."
:group 'dictem-faces)
(defface dictem-reference-dbname-face
'((((background light)) (:foreground "darkgreen"))
(((background dark)) (:bold t :foreground "white")))
"The face that is used for displaying a reference to database"
:group 'dictem-faces)
(defface dictem-database-description-face
'((((background light)) (:bold t :foreground "darkblue"))
(((background dark)) (:bold t :foreground "white")))
"The face that is used for displaying a database description"
:group 'dictem-faces)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Variables ;;;;;
(defconst dictem-version "1.0.4"
"DictEm version information.")
(defvar dictem-strategy-alist
nil
"ALIST of search strategies")
(defvar dictem-database-alist
nil
"ALIST of databases")
(defvar dictem-strategy-history
nil
"List of strategies entered from minibuffer")
(defvar dictem-database-history
nil
"List of database names entered from minibuffer")
(defvar dictem-query-history
nil
"List of queries entered from minibuffer")
(defvar dictem-last-database
"*"
"Last used database name")
(defvar dictem-last-strategy
"."
"Last used strategy name")
(defvar dictem-mode-map
nil
"Keymap for dictem mode")
(defvar dictem-temp-buffer-name
"*dict-temp*"
"Temporary dictem buffer name")
(defvar dictem-current-dbname
nil
"This variable keeps a database name of the definition
currently processed
by functions run from dictem-postprocess-each-definition-hook.")
(defvar dictem-error-messages
nil
"A list of error messages collected by dictem-run")
(defvar dictem-hyperlinks-alist
nil
"ALIST of hyperlinks collected from dictem buffer by
the function dictem-postprocess-collect-hyperlinks
(add this function to the hook dictem-postprocess-definition-hook).
This variable is local to buffer")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dictem-prepand-special-strats (l)
(cons '(".") l))
(defun dictem-prepand-special-dbs (l)
(cons '("*") (cons '("!") l)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Functions ;;;;;;
(defmacro save-dictem (&rest funs)
`(let ((dictem-port 2628)
(dictem-server nil)
(dictem-database-alist nil)
(dictem-strategy-alist nil)
(dictem-use-user-databases-only nil)
(dictem-user-databases-alist nil)
)
(progn ,@funs)
))
(defun dictem-client-text ()
"Returns a portion of text sent to the server for identifying a client"
(concat "dictem " dictem-version ", DICT client for emacs"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions related to userdb ;;
(defun dictem-make-userdb (name short-name match define)
"Make user database object"
(list name 'dictem-userdb
short-name match define))
(defun dictem-userdb-p (obj)
"Returns t if obj is the dictem error object"
(and obj (listp obj) (cdr obj) (listp (cdr obj))
(eq (cadr obj) 'dictem-userdb)))
(defun dictem-userdb-member (obj name)
"Extract member from userdb object by its name"
(cond ((dictem-userdb-p obj)
(nth (cdr (assoc name
'(("name" . 0) ("short-name" . 2)
("match" . 3) ("define" . 4))))
obj))
(t (error "Invalid type of argument"))))
(defun dictem-userdb-DEFINE (buffer db query host port)
(let* ((fun (dictem-userdb-member db "define"))
(name (dictem-userdb-member db "name"))
(sname (dictem-userdb-member db "short-name"))
(ret (save-excursion (funcall fun query)))
(buf (dictem-get-buffer buffer)))
(save-excursion
(set-buffer buf)
(cond ((dictem-error-p ret)
; (insert "From " sname " [" name "]:\n\n"
; (dictem-error-message ret) "\n\n")
; (insert (dictem-error-message ret) "\n")
(insert (dictem-error-message ret) "\n")
(dictem-error-status ret))
((null ret)
(insert "No matches found" "\n")
20)
((listp ret)
(dolist (definition ret)
(insert "From " sname " [" name "]:\n\n"
(dictem-indent-string definition) "\n\n"))
0)
((stringp ret)
(insert "From " sname " [" name "]:\n\n"
(dictem-indent-string ret) "\n\n")
0)
(t
(error "Invalid type of returned value1"))))))
(defun dictem-userdb-MATCH (buffer db query strat host port)
(let* ((fun (dictem-userdb-member db "match"))
(name (dictem-userdb-member db "name"))
(ret (save-excursion (funcall fun query strat)))
(buf (dictem-get-buffer buffer)))
(save-excursion
(set-buffer buf)
(cond ((dictem-error-p ret)
(insert (dictem-error-message ret) "\n")
(dictem-error-status ret))
((listp ret)
(insert (concat name ":\n"))
(dolist (match ret); (insert (car db) ":\n" ))
(progn
(insert " " match "\n"))
)
0)
(t
(error "Invalid type of returned value2"))))))
(defun dictem-userdb-SEARCH (buffer db query strat host port)
(let* ((funm (dictem-userdb-member db "match"))
(name (dictem-userdb-member db "name"))
(sname (dictem-userdb-member db "short-name"))
(sname nil)
(ret (funcall funm query strat))
(buf (dictem-get-buffer buffer)))
(save-excursion
(set-buffer buf)
(cond ((dictem-error-p ret)
(insert (dictem-error-message ret) "\n")
(dictem-error-status ret))
((listp ret)
(dolist (match ret)
(dictem-userdb-DEFINE buffer db
match host port))
0)
(t
(error "Something strange happened"))
))))
(defun dictem-userdb-SHOW-INFO (buffer db host port)
(let ((sname (dictem-userdb-member db "short-name"))
(buf (dictem-get-buffer buffer)))
(save-excursion
(set-buffer buf)
(cond ((dictem-error-p sname)
(insert (dictem-error-message sname) "\n")
(dictem-error-status sname))
((stringp sname)
(insert sname)
0)
(t
(error "Something strange happened"))
))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions related to error object ;;
(defun dictem-make-error (error_status &optional buffer-or-string)
"Creates dictem error object"
(cond
((stringp buffer-or-string)
(list 'dictem-error error_status buffer-or-string))
((bufferp buffer-or-string)
(dictem-make-error
error_status
(save-excursion
(set-buffer buffer-or-string)
(goto-char (point-min))
(dictem-get-line)
)))
((eq nil buffer-or-string)
(list 'dictem-error error_status buffer-or-string))
(t
(error "Invalid type of argument"))
))
(defun dictem-error-p (OBJECT)
"Returns t if OBJECT is the dictem error object"
(and
(not (null OBJECT))
(listp OBJECT)
(eq (car OBJECT) 'dictem-error)
))
(defun dictem-error-message (err)
"Extract error message from dictem error object"
(cond
((dictem-error-p err)
(nth 2 err))
(t
(error "Invalid type of argument"))
))
(defun dictem-error-status (err)
"Extract error status from dictem error object"
(cond
((dictem-error-p err)
(nth 1 err))
(t
(error "Invalid type of argument"))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dictem-collect-matches ()
; nreverse, setcar and nconc are used to reduce a number of cons
(goto-char (point-min))
(let ((dictem-temp nil))
(loop
(let ((line (dictem-get-line)))
(if (string-match "^[^ ]+:" line)
(progn
(if (consp dictem-temp)
(setcar (cdar dictem-temp)
(nreverse (cadar dictem-temp))))
(setq
dictem-temp
(cons
(list
(substring line (match-beginning 0) (- (match-end 0) 1))
(nreverse
(dictem-tokenize (substring line (match-end 0)))))
dictem-temp)))
(if (consp dictem-temp)
(setcar (cdar dictem-temp)
(nconc (nreverse (dictem-tokenize line))
(cadar dictem-temp))
))
))
(if (or (> (forward-line 1) 0)
(> (current-column) 0))
(return (nreverse dictem-temp)))
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dictem-get-buffer (buf)
(cond
((bufferp buf) buf)
(buf (current-buffer))
(t (get-buffer-create dictem-temp-buffer-name))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; call-process functions
(defun dictem-local-dict-basic-option (host port option-mime)
(let ((server-host (if host host (dictem-get-server))))
(append
(list "-P" "-")
(if server-host
(list "-h" server-host "-p" (dictem-get-port port)))
(if option-mime '("-M"))
dictem-client-prog-args-list
)))
(defun dictem-call-process (buffer host port args)
(let (coding-system
coding-system-for-read
coding-system-for-write)
(if (and (functionp 'coding-system-list)
(member 'utf-8 (coding-system-list)))
(setq coding-system 'utf-8))
(setq coding-system-for-read coding-system)
(setq coding-system-for-write coding-system)
(apply 'call-process
`(,dictem-client-prog
nil
,(dictem-get-buffer buffer)
nil
,@(dictem-local-dict-basic-option host port nil)
,@args
))))
(defun dictem-call-process-SHOW-SERVER (buffer host port)
(dictem-call-process buffer host port '("-I")))
(defun dictem-call-process-SHOW-INFO (buffer db host port)
(dictem-call-process buffer host port (list "-i" db)))
(defun dictem-call-process-SHOW-STRAT (buffer host port)
(dictem-call-process buffer host port '("-S")))
(defun dictem-call-process-SHOW-DB (buffer host port)
(dictem-call-process buffer host port '("-D")))
(defun dictem-call-process-MATCH (buffer db query strat host port)
(dictem-call-process
buffer host port
(list "-m"
"-d" (if db db "*")
"-s" (if strat strat ".")
query)))
(defun dictem-call-process-DEFINE (buffer db query host port)
(dictem-call-process
buffer host port
(list "-d" (if db db "*") query)))
(defun dictem-call-process-SEARCH (buffer db query strat host port)
(dictem-call-process
buffer host port
(list "-d" (if db db "*")
"-s" (if strat strat ".")
query)))
;;;;; GET Functions ;;;;;
(defun dictem-get-matches (query &optional database strategy server port)
"Returns ALIST of matches"
(let ((exit_status
(dictem-call-process-MATCH nil database query strategy server port)
))
(cond
((= exit_status 20) ;20 means "no matches found", See dict(1)
(kill-buffer dictem-temp-buffer-name)
nil)
((= exit_status 0)
(progn
(save-excursion
(set-buffer dictem-temp-buffer-name)
(let ((matches (dictem-collect-matches)))
(kill-buffer dictem-temp-buffer-name)
matches))))
(t
(let
((err (dictem-make-error exit_status
(get-buffer dictem-temp-buffer-name))))
(kill-buffer dictem-temp-buffer-name)
err))
)))
(defun dictem-get-strategies (&optional server port)
"Obtains strategy ALIST from a DICT server
and returns alist containing strategies and their descriptions"
(let ((exit_status
(dictem-call-process-SHOW-STRAT nil server port)
))
(cond
((= exit_status 0)
(save-excursion
(set-buffer dictem-temp-buffer-name)
(goto-char (point-min))
(let ((regexp "^ \\([^ ]+\\) +\\(.*\\)$")
(l nil))
(while (search-forward-regexp regexp nil t)
(setq l (cons
(list
(buffer-substring-no-properties
(match-beginning 1) (match-end 1))
(buffer-substring-no-properties
(match-beginning 2) (match-end 2)))
l)))
(kill-buffer dictem-temp-buffer-name)
l)))
(t
(let
((err (dictem-make-error exit_status
(get-buffer dictem-temp-buffer-name))))
(kill-buffer dictem-temp-buffer-name)
err))
)))
(defun dictem-get-databases (&optional server port)
"Obtains database ALIST from a DICT server
and returns alist containing database names and descriptions"
(let ((exit_status
(dictem-call-process-SHOW-DB nil server port)
))
(cond
((= exit_status 0)
(save-excursion
(set-buffer dictem-temp-buffer-name)
(goto-char (point-min))
(let ((regexp "^ \\([^ ]+\\) +\\(.*\\)$")
(l nil))
(while (search-forward-regexp regexp nil t)
(let ((dbname (buffer-substring-no-properties
(match-beginning 1) (match-end 1)))
(dbdescr (buffer-substring-no-properties
(match-beginning 2) (match-end 2))))
(if (not (string= "--exit--" dbname))
(setq l (cons (list dbname dbdescr) l)))))
(kill-buffer dictem-temp-buffer-name)
l)))
(t
(let
((err (dictem-make-error exit_status
(get-buffer dictem-temp-buffer-name))))
(kill-buffer dictem-temp-buffer-name)
err))
)))
(defun dictem-get-default-strategy (&optional def-strat)
"Gets the default search strategy"
(if def-strat
def-strat
(if dictem-default-strategy
dictem-default-strategy
(if dictem-last-strategy
dictem-last-strategy
"."))))
(defun dictem-extract-dbname (database)
(cond
((consp database) (dictem-extract-dbname (car database)))
((stringp database) database)
(t (error "The database should be either stringp or consp"))
))
(defun dictem-get-default-database (&optional def-db)
"Returns the default database"
(if def-db
(dictem-extract-dbname def-db)
(if dictem-default-database
(dictem-extract-dbname dictem-default-database)
(if dictem-last-database
(dictem-extract-dbname dictem-last-database)
"*"))))
;;;;; Low Level Functions ;;;;;
(defun dictem-db-should-be-excluded (dbname)
"Returns t if a dbname should is not interesting for user.
See dictem-exclude-databases variable"
(let ((ret nil))
(dolist (re dictem-exclude-databases)
(if (string-match re dbname)
(setq ret t)))
ret))
(defun dictem-delete-alist-predicate (l pred)
"makes a copy of l with no items for which (pred item) is true"
(let ((ret nil))
(dolist (item l)
(if (not (funcall pred (car item)))
(setq ret (cons item ret))))
ret))
(defun dictem-get-line ()
"Replacement for (thing-at-point 'line)"
(save-excursion
(buffer-substring-no-properties
(progn (beginning-of-line) (point))
(progn (end-of-line) (point)))))
(defun dictem-list2alist (l)
(cond
((null l) nil)
(t (cons
(list (car l) nil)
(dictem-list2alist (cdr l))))))
(defun dictem-indent-string (str)
(let ((start 0))
(while (string-match "\n" str start)
(progn
(setq start ( + 2 (match-end 0)))
(setq str (replace-match "\n " t t str)))))
(concat " " str))
(defun dictem-replace-spaces (str)
(while (string-match "[ \n][ \n]+" str)
(setq str (replace-match " " t t str)))
(if (string-match "^ +" str)
(setq str (replace-match "" t t str)))
(if (string-match " +$" str)
(setq str (replace-match "" t t str)))
str)
(defun dictem-remove-value-from-alist (l)
(let ((ret nil))
(dolist (i l)
(setq ret (cons (list (car i)) ret)))
(reverse ret)
))
;(defun dictem-remove-value-from-alist (l)
; (cond
; ((symbolp l) l)
; (t (cons (list (caar l))
; (dictem-remove-value-from-alist (cdr l))))))
(defun dictem-select (prompt alist default history)
(let*
((completion-ignore-case t)
(str (completing-read
(concat prompt " [" default "]: ")
alist nil t nil history default))
(str-cons (assoc str alist)))
(cond
((and str-cons (consp str-cons) (cdr str-cons))
str-cons)
((and str-cons (consp str-cons))
(car str-cons))
(t nil))))
(defun dictem-tokenize (s)
(if (string-match "\"[^\"]+\"\\|[^ \"]+" s )
; (substring s (match-beginning 0) (match-end 0))
(cons (substring s (match-beginning 0) (match-end 0))
(dictem-tokenize (substring s (match-end 0))))
nil))
;(defun dictem-search-forward-regexp-cs (REGEXP &optional BOUND NOERROR COUNT)
; "Case-sensitive variant for search-forward-regexp"
; (let ((case-replace nil)
; (case-fold-search nil))
; (search-forward-regexp REGEXP BOUND NOERROR COUNT)))
;(defun dictem-replace-match-cs (NEWTEXT &optional FIXEDCASE LITERAL STRING SUBEXP)
; "Case-sensitive variant for replace-match"
; (let ((case-replace nil)
; (case-fold-search nil))
; (replace-match NEWTEXT FIXEDCASE LITERAL STRING SUBEXP)))
(defun dictem-get-port (&optional port)
(let ((p (if port port dictem-port)))
(cond
((and (stringp p) (string= "" p)) 2628)
((null p) 2628)
((stringp p) p)
((numberp p) (number-to-string p))
(t (error "The value of dictem-port variable should be \
either a string or a number"))
)))
(defun dictem-get-server ()
(cond
((and (stringp dictem-server) (string= "" dictem-server)) nil)
((stringp dictem-server) dictem-server)
((null dictem-server) nil)
(t (error "The value of dictem-server variable should be \
either a string or a nil"))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Main Functions ;;;;;
;;;;;; Functions for Initializing ;;;;;;
(defun dictem-initialize-strategies-alist (&optional server port)
"Obtain strategy ALIST from a DICT server
and sets dictem-strategy-alist variable."
(interactive)
(setq dictem-strategy-alist (dictem-get-strategies
server
(dictem-get-port port))))
(defun dictem-initialize-databases-alist (&optional server port)
"Obtain database ALIST from a DICT server
and sets dictem-database-alist variable."
(interactive)
(setq dictem-database-alist
(dictem-get-databases server (dictem-get-port port)))
(if (dictem-error-p dictem-database-alist)
dictem-database-alist
(setq dictem-database-alist
(dictem-delete-alist-predicate
dictem-database-alist
'dictem-db-should-be-excluded))))
(defun dictem-initialize ()
"Initializes dictem, i.e. obtains
a list of available databases and strategiss from DICT server
and makes other tasks."
(interactive)
(let ((dbs (dictem-initialize-databases-alist))
(strats (dictem-initialize-strategies-alist)))
(if (dictem-error-p dbs)
dbs strats)))
(defun dictem-reinitialize-err ()
"Initializes dictem if it is not initialized yet
and run (error ...) if an initialization fails"
(interactive)
(if (or (dictem-error-p dictem-database-alist)
(null dictem-database-alist))
(if (dictem-error-p (dictem-initialize))
(error (dictem-error-message dictem-database-alist)))))
;;; Functions related to Minibuffer ;;;;
(defun dictem-select-strategy (&optional default-strat)
"Switches to minibuffer and asks the user
to enter a search strategy."
(dictem-reinitialize-err)
(dictem-select
"strategy"
(dictem-prepand-special-strats
(dictem-remove-value-from-alist dictem-strategy-alist))
(dictem-get-default-strategy default-strat)
'dictem-strategy-history))
(defun dictem-select-database (spec-dbs user-dbs &optional default-db)
"Switches to minibuffer and asks user
to enter a database name."
(dictem-reinitialize-err)
(let* ((dbs (dictem-remove-value-from-alist dictem-database-alist))
(dbs2 (if user-dbs
(if dictem-use-user-databases-only
dictem-user-databases-alist
(append dictem-user-databases-alist dbs)
)
dbs)))
(dictem-select
"db"
(if spec-dbs (dictem-prepand-special-dbs dbs2) dbs2)
(dictem-get-default-database default-db)
'dictem-database-history)))
(defun dictem-read-query (&optional default-query)
"Switches to minibuffer and asks user to enter a query."
(if (featurep 'xemacs)
(read-string
(concat "query [" default-query "]: ")
nil 'dictem-query-history default-query)
(read-string
(concat "query [" default-query "]: ")
(if dictem-empty-initial-input nil default-query)
'dictem-query-history default-query t)))
;;;;;;;; Hooks ;;;;;;;;
(defcustom dictem-postprocess-definition-hook
nil
"Hook run in dictem mode buffers containing DEFINE result."
:group 'dictem
:type 'hook
:options '(dictem-postprocess-definition-separator
dictem-postprocess-definition-hyperlinks
dictem-postprocess-each-definition
dictem-postprocess-definition-remove-header
dictem-postprocess-collect-hyperlinks))
(defcustom dictem-postprocess-match-hook
nil
"Hook run in dictem mode buffers containing MATCH result."
:group 'dictem
:type 'hook
:options '(dictem-postprocess-match))
(defcustom dictem-postprocess-show-info-hook
nil
"Hook run in dictem mode buffers containing SHOW INFO result."
:group 'dictem
:type 'hook
:options '(dictem-postprocess-definition-hyperlinks
dictem-postprocess-collect-hyperlinks))
(defcustom dictem-postprocess-show-server-hook
nil
"Hook run in dictem mode buffers containing SHOW SERVER result."
:group 'dictem
:type 'hook)
;;;;;;;; Search Functions ;;;;;;;
(defun dictem-call-dict-internal (fun databases)
(let ((exit-status -1))
(cond
((null databases) 0)
((stringp databases)
(dictem-call-dict-internal fun (list databases)))
((listp databases)
(dolist (db databases)
(let ((ex_st (funcall fun db)))
(cond
((= ex_st 0)
(setq exit-status 0))
(t (if (/= 0 exit-status)
(setq exit-status ex_st)))
)))
(if (= exit-status -1) 0 exit-status)
)
(t (error "wrong type of argument"))
)
))
;(defun dictem-call-dict-internal (fun databases)
; (dolist (db databases)
; (funcall fun db)))
; (funcall fun databases))
(defun dictem-make-url (host port database cmd_sign query &optional strategy)
"Returns dict:// URL"
(concat
"dict://" host ":"
(dictem-get-port (if port port "2628"))
"/" cmd_sign ":" query ":" database
(if strategy (concat ":" (if strategy strategy ".")))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dictem-base-do-selector (cmd hook &optional database &rest args)
(let* ((splitted-url nil)
(databases nil)
(user-db (assoc database dictem-user-databases-alist))
)
(goto-char (point-max))
(cond ((dictem-userdb-p database)
(apply 'dictem-base-do-default-server
(append (list cmd hook database) args)))
((and database (listp database))
(dictem-call-dict-internal
`(lambda (db)
(apply 'dictem-base-do-selector
(append (list ,cmd hook db) args)))
(cdr database))
(setq dictem-last-database (car database)))
((and database (stringp database)
(setq splitted-url (dictem-parse-url database)))
(apply 'dictem-base-do-foreign-server
(append
(list cmd hook
(nth 1 splitted-url)
(dictem-get-port (nth 2 splitted-url))
(nth 3 splitted-url))
args)))
(user-db
(let ((exit_status
(apply 'dictem-base-do-selector
(append
(list cmd hook user-db) args))))
(progn
(setq dictem-last-database database)
exit_status)
))
(t
(apply 'dictem-base-do-default-server
(append (list cmd hook database) args)))
)))
(defun dictem-base-do-foreign-server (cmd hook server port database &rest args)
(let ((dictem-last-database nil)
(dictem-last-strategy nil))
(save-dictem (setq dictem-server server)
(setq dictem-port port)
(setq database database)
(dictem-initialize)
(apply 'dictem-base-do-default-server
(append (list cmd hook database) args))
)))
(defun dictem-base-do-default-server (cmd hook
&optional database query strategy)
(let* ((beg (point))
(fun (if (dictem-userdb-p database)
(dictem-cmd2userdb cmd)
(dictem-cmd2function cmd)))
(exit_status
(save-excursion (apply fun (append (list t)
(if database (list database))
(if query (list query))
(if strategy (list strategy))
(list nil) (list nil))))
))
(cond ((= 0 exit_status)
(save-excursion
(narrow-to-region beg (point-max))
(run-hooks hook)
(widen)))
((= 21 exit_status)