aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTorsten Hilbrich2020-10-09 05:00:02 +0200
committerTorsten Hilbrich2020-10-09 20:05:38 +0200
commit1773b9b68742c95b1648a90c56eb7b56c77db591 (patch)
treeea976801b46d9cf23d7456b0c578d244690e41e6
parent99a7e918c82c0d5c39a729668ac582a945877900 (diff)
downloademacs-1773b9b68742c95b1648a90c56eb7b56c77db591.tar.gz
emacs-1773b9b68742c95b1648a90c56eb7b56c77db591.zip
Dictionary now uses button
* net/lisp/dictionary-link.el: Removed now obsolete file * net/lisp/dictionary.el: Use insert-button and make-button * net/lisp/dictionary.el (dictionary-mode-map): Now defined using defvar I had to add a conversion function as parameter for the button 'action as I need to be able to pass nil data to my function. This is not possible with the regular button 'action function and the 'button-data value. The functionality of searching a link in all dictionaries has been removed for now. It might appear again once I have an idea how to implement it.
-rw-r--r--lisp/net/dictionary-link.el122
-rw-r--r--lisp/net/dictionary.el163
2 files changed, 67 insertions, 218 deletions
diff --git a/lisp/net/dictionary-link.el b/lisp/net/dictionary-link.el
deleted file mode 100644
index 549f199e02a..00000000000
--- a/lisp/net/dictionary-link.el
+++ /dev/null
@@ -1,122 +0,0 @@
1;;; dictionary-link.el --- Hypertext links in text buffers
2
3;; Author: Torsten Hilbrich <torsten.hilbrich@gmx.net>
4;; Keywords: interface, hypermedia
5;; Version: 1.11
6
7;; This file is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 2, or (at your option)
10;; any later version.
11
12;; This file is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20;; Boston, MA 02111-1307, USA.
21
22;;; Commentary:
23
24;; This file contains functions for using links in buffers. A link is
25;; a part of the buffer marked with a special face, beeing
26;; hightlighted while the mouse points to it and beeing activated when
27;; pressing return or clicking the button2.
28
29;; Which each link a function and some data are associated. Upon
30;; clicking the function is called with the data as only
31;; argument. Both the function and the data are stored in text
32;; properties.
33;;
34;; dictionary-link-create-link - insert a new link for the text in the given range
35;; dictionary-link-initialize-keymap - install the keybinding for selecting links
36
37;;; Code:
38
39(defun dictionary-link-create-link (start end face function &optional data help)
40 "Create a link in the current buffer starting from `start' going to `end'.
41The `face' is used for displaying, the `data' are stored together with the
42link. Upon clicking the `function' is called with `data' as argument."
43 (let ((properties `(face ,face
44 mouse-face highlight
45 link t
46 link-data ,data
47 help-echo ,help
48 link-function ,function)))
49 (remove-text-properties start end properties)
50 (add-text-properties start end properties)))
51
52(defun dictionary-link-insert-link (text face function &optional data help)
53 "Insert the `text' at point to be formatted as link.
54The `face' is used for displaying, the `data' are stored together with the
55link. Upon clicking the `function' is called with `data' as argument."
56 (let ((start (point)))
57 (insert text)
58 (dictionary-link-create-link start (point) face function data help)))
59
60(defun dictionary-link-selected (&optional all)
61 "Is called upon clicking or otherwise visiting the link."
62 (interactive)
63
64 (let* ((properties (text-properties-at (point)))
65 (function (plist-get properties 'link-function))
66 (data (plist-get properties 'link-data)))
67 (if function
68 (funcall function data all))))
69
70(defun dictionary-link-selected-all ()
71 "Called for meta clicking the link"
72 (interactive)
73 (dictionary-link-selected 'all))
74
75(defun dictionary-link-mouse-click (event &optional all)
76 "Is called upon clicking the link."
77 (interactive "@e")
78
79 (mouse-set-point event)
80 (dictionary-link-selected))
81
82(defun dictionary-link-mouse-click-all (event)
83 "Is called upon meta clicking the link."
84 (interactive "@e")
85
86 (mouse-set-point event)
87 (dictionary-link-selected-all))
88
89(defun dictionary-link-next-link ()
90 "Return the position of the next link or nil if there is none"
91 (let* ((pos (point))
92 (pos (next-single-property-change pos 'link)))
93 (if pos
94 (if (text-property-any pos (min (1+ pos) (point-max)) 'link t)
95 pos
96 (next-single-property-change pos 'link))
97 nil)))
98
99
100(defun dictionary-link-prev-link ()
101 "Return the position of the previous link or nil if there is none"
102 (let* ((pos (point))
103 (pos (previous-single-property-change pos 'link)))
104 (if pos
105 (if (text-property-any pos (1+ pos) 'link t)
106 pos
107 (let ((val (previous-single-property-change pos 'link)))
108 (if val
109 val
110 (text-property-any (point-min) (1+ (point-min)) 'link t))))
111 nil)))
112
113(defun dictionary-link-initialize-keymap (keymap)
114 "Defines the necessary bindings inside keymap"
115
116 (define-key keymap [mouse-2] 'dictionary-link-mouse-click)
117 (define-key keymap [M-mouse-2] 'dictionary-link-mouse-click-all)
118 (define-key keymap "\r" 'dictionary-link-selected)
119 (define-key keymap "\M-\r" 'dictionary-link-selected-all))
120
121(provide 'dictionary-link)
122;;; dictionary-link.el ends here
diff --git a/lisp/net/dictionary.el b/lisp/net/dictionary.el
index a0e43b89d96..b25dda5c69c 100644
--- a/lisp/net/dictionary.el
+++ b/lisp/net/dictionary.el
@@ -38,7 +38,7 @@
38(require 'easymenu) 38(require 'easymenu)
39(require 'custom) 39(require 'custom)
40(require 'dictionary-connection) 40(require 'dictionary-connection)
41(require 'dictionary-link) 41(require 'button)
42 42
43;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 43;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44;; Stuff for customizing. 44;; Stuff for customizing.
@@ -296,8 +296,24 @@ is utf-8"
296;; Global variables 296;; Global variables
297;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 297;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298(defvar dictionary-mode-map 298(defvar dictionary-mode-map
299 nil 299 (let ((map (make-sparse-keymap)))
300 "Keymap for dictionary mode") 300 (suppress-keymap map)
301 (set-keymap-parent map button-buffer-map)
302
303 (define-key map "q" 'dictionary-close)
304 (define-key map "h" 'dictionary-help)
305 (define-key map "s" 'dictionary-search)
306 (define-key map "d" 'dictionary-lookup-definition)
307 (define-key map "D" 'dictionary-select-dictionary)
308 (define-key map "M" 'dictionary-select-strategy)
309 (define-key map "m" 'dictionary-match-words)
310 (define-key map "l" 'dictionary-previous)
311 (define-key map "n" 'forward-button)
312 (define-key map "p" 'backward-button)
313 (define-key map " " 'scroll-up)
314 (define-key map (read-kbd-macro "M-SPC") 'scroll-down)
315 map)
316 "Keymap for the dictionary mode.")
301 317
302(defvar dictionary-connection 318(defvar dictionary-connection
303 nil 319 nil
@@ -340,7 +356,6 @@ is utf-8"
340 * M select the default search strategy 356 * M select the default search strategy
341 357
342 * Return or Button2 visit that link 358 * Return or Button2 visit that link
343 * M-Return or M-Button2 search the word beneath link in all dictionaries
344 " 359 "
345 360
346 (unless (eq major-mode 'dictionary-mode) 361 (unless (eq major-mode 'dictionary-mode)
@@ -394,39 +409,6 @@ is utf-8"
394 (dictionary-pre-buffer) 409 (dictionary-pre-buffer)
395 (dictionary-post-buffer)) 410 (dictionary-post-buffer))
396 411
397
398(unless dictionary-mode-map
399 (setq dictionary-mode-map (make-sparse-keymap))
400 (suppress-keymap dictionary-mode-map)
401
402 (define-key dictionary-mode-map "q" 'dictionary-close)
403 (define-key dictionary-mode-map "h" 'dictionary-help)
404 (define-key dictionary-mode-map "s" 'dictionary-search)
405 (define-key dictionary-mode-map "d" 'dictionary-lookup-definition)
406 (define-key dictionary-mode-map "D" 'dictionary-select-dictionary)
407 (define-key dictionary-mode-map "M" 'dictionary-select-strategy)
408 (define-key dictionary-mode-map "m" 'dictionary-match-words)
409 (define-key dictionary-mode-map "l" 'dictionary-previous)
410
411 (if (and (string-match "GNU" (emacs-version))
412 (not window-system))
413 (define-key dictionary-mode-map [9] 'dictionary-next-link)
414 (define-key dictionary-mode-map [tab] 'dictionary-next-link))
415
416 ;; shift-tabs normally is supported on window systems only, but
417 ;; I do not enforce it
418 (define-key dictionary-mode-map [(shift tab)] 'dictionary-prev-link)
419 (define-key dictionary-mode-map "\e\t" 'dictionary-prev-link)
420 (define-key dictionary-mode-map [backtab] 'dictionary-prev-link)
421
422 (define-key dictionary-mode-map "n" 'dictionary-next-link)
423 (define-key dictionary-mode-map "p" 'dictionary-prev-link)
424
425 (define-key dictionary-mode-map " " 'scroll-up)
426 (define-key dictionary-mode-map [(meta space)] 'scroll-down)
427
428 (dictionary-link-initialize-keymap dictionary-mode-map))
429
430(defmacro dictionary-reply-code (reply) 412(defmacro dictionary-reply-code (reply)
431 "Return the reply code stored in `reply'." 413 "Return the reply code stored in `reply'."
432 (list 'get reply ''reply-code)) 414 (list 'get reply ''reply-code))
@@ -696,43 +678,48 @@ This function knows about the special meaning of quotes (\")"
696 (error "Unknown server answer: %s" (dictionary-reply reply))) 678 (error "Unknown server answer: %s" (dictionary-reply reply)))
697 (funcall function reply))))) 679 (funcall function reply)))))
698 680
681(define-button-type 'dictionary-link
682 'face 'dictionary-reference-face
683 'action (lambda (button) (funcall (button-get button 'callback)
684 (button-get button 'data))))
685
686(define-button-type 'dictionary-button
687 :supertype 'dictionary-link
688 'face 'dictionary-button-face)
689
699(defun dictionary-pre-buffer () 690(defun dictionary-pre-buffer ()
700 "These commands are executed at the begin of a new buffer" 691 "These commands are executed at the begin of a new buffer"
701 (setq buffer-read-only nil) 692 (setq buffer-read-only nil)
702 (erase-buffer) 693 (erase-buffer)
703 (if dictionary-create-buttons 694 (if dictionary-create-buttons
704 (progn 695 (progn
705 (dictionary-link-insert-link "[Back]" 'dictionary-button-face 696 (insert-button "[Back]" :type 'dictionary-button
706 'dictionary-restore-state nil 697 'callback 'dictionary-restore-state
707 "Mouse-2 to go backwards in history") 698 'help-echo (purecopy "Mouse-2 to go backwards in history"))
708 (insert " ") 699 (insert " ")
709 (dictionary-link-insert-link "[Search Definition]" 700 (insert-button "[Search Definition]" :type 'dictionary-button
710 'dictionary-button-face 701 'callback 'dictionary-search
711 'dictionary-search nil 702 'help-echo (purecopy "Mouse-2 to look up a new word"))
712 "Mouse-2 to look up a new word")
713 (insert " ") 703 (insert " ")
714 704
715 (dictionary-link-insert-link "[Matching words]" 705 (insert-button "[Matching words]" :type 'dictionary-button
716 'dictionary-button-face 706 'callback 'dictionary-match-words
717 'dictionary-match-words nil 707 'help-echo (purecopy "Mouse-2 to find matches for a pattern"))
718 "Mouse-2 to find matches for a pattern")
719 (insert " ") 708 (insert " ")
720 709
721 (dictionary-link-insert-link "[Quit]" 'dictionary-button-face 710 (insert-button "[Quit]" :type 'dictionary-button
722 'dictionary-close nil 711 'callback 'dictionary-close
723 "Mouse-2 to close this window") 712 'help-echo (purecopy "Mouse-2 to close this window"))
724 713
725 (insert "\n ") 714 (insert "\n ")
726 715
727 (dictionary-link-insert-link "[Select Dictionary]" 716 (insert-button "[Select Dictionary]" :type 'dictionary-button
728 'dictionary-button-face 717 'callback 'dictionary-select-dictionary
729 'dictionary-select-dictionary nil 718 'help-echo (purecopy "Mouse-2 to select dictionary for future searches"))
730 "Mouse-2 to select dictionary for future searches")
731 (insert " ") 719 (insert " ")
732 (dictionary-link-insert-link "[Select Match Strategy]" 720 (insert-button "[Select Match Strategy]" :type 'dictionary-button
733 'dictionary-button-face 721 'callback 'dictionary-select-strategy
734 'dictionary-select-strategy nil 722 'help-echo (purecopy "Mouse-2 to select matching algorithm"))
735 "Mouse-2 to select matching algorithm")
736 (insert "\n\n"))) 723 (insert "\n\n")))
737 (setq dictionary-marker (point-marker))) 724 (setq dictionary-marker (point-marker)))
738 725
@@ -810,10 +797,11 @@ The word is taken from the buffer, the `dictionary' is given as argument."
810 (setq word (replace-match "" t t word))) 797 (setq word (replace-match "" t t word)))
811 798
812 (unless (equal word displayed-word) 799 (unless (equal word displayed-word)
813 (dictionary-link-create-link start end 'dictionary-reference-face 800 (make-button start end :type 'dictionary-link
814 call (cons word dictionary) 801 'callback call
815 (concat "Press Mouse-2 to lookup \"" 802 'data (cons word dictionary)
816 word "\" in \"" dictionary "\""))))) 803 'help-echo (concat "Press Mouse-2 to lookup \""
804 word "\" in \"" dictionary "\"")))))
817 805
818(defun dictionary-select-dictionary (&rest ignored) 806(defun dictionary-select-dictionary (&rest ignored)
819 "Save the current state and start a dictionary selection" 807 "Save the current state and start a dictionary selection"
@@ -871,11 +859,10 @@ If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
871 (if dictionary 859 (if dictionary
872 (if (equal dictionary "--exit--") 860 (if (equal dictionary "--exit--")
873 (insert "(end of default search list)\n") 861 (insert "(end of default search list)\n")
874 (dictionary-link-insert-link (concat dictionary ": " translated) 862 (insert-button (concat dictionary ": " translated) :type 'dictionary-link
875 'dictionary-reference-face 863 'callback 'dictionary-set-dictionary
876 'dictionary-set-dictionary 864 'data (cons dictionary description)
877 (cons dictionary description) 865 'help-echo (purecopy "Mouse-2 to select this dictionary"))
878 "Mouse-2 to select this dictionary")
879 (insert "\n"))))) 866 (insert "\n")))))
880 867
881(defun dictionary-set-dictionary (param &optional more) 868(defun dictionary-set-dictionary (param &optional more)
@@ -907,10 +894,10 @@ If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
907 (error "Unknown server answer: %s" (dictionary-reply reply))) 894 (error "Unknown server answer: %s" (dictionary-reply reply)))
908 (dictionary-pre-buffer) 895 (dictionary-pre-buffer)
909 (insert "Information on dictionary: ") 896 (insert "Information on dictionary: ")
910 (dictionary-link-insert-link description 'dictionary-reference-face 897 (insert-button description :type 'dictionary-link
911 'dictionary-set-dictionary 898 'callback 'dictionary-set-dictionary
912 (cons dictionary description) 899 'data (cons dictionary description)
913 "Mouse-2 to select this dictionary") 900 'help-echo (purecopy "Mouse-2 to select this dictionary"))
914 (insert "\n\n") 901 (insert "\n\n")
915 (setq reply (dictionary-read-answer)) 902 (setq reply (dictionary-read-answer))
916 (insert reply) 903 (insert reply)
@@ -958,9 +945,10 @@ If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
958 (description (cadr list))) 945 (description (cadr list)))
959 (if strategy 946 (if strategy
960 (progn 947 (progn
961 (dictionary-link-insert-link description 'dictionary-reference-face 948 (insert-button description :type 'dictionary-link
962 'dictionary-set-strategy strategy 949 'callback 'dictionary-set-strategy
963 "Mouse-2 to select this matching algorithm") 950 'data strategy
951 'help-echo (purecopy "Mouse-2 to select this matching algorithm"))
964 (insert "\n"))))) 952 (insert "\n")))))
965 953
966(defun dictionary-set-strategy (strategy &rest ignored) 954(defun dictionary-set-strategy (strategy &rest ignored)
@@ -1060,11 +1048,10 @@ If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
1060 (mapc (lambda (word) 1048 (mapc (lambda (word)
1061 (setq word (dictionary-decode-charset word dictionary)) 1049 (setq word (dictionary-decode-charset word dictionary))
1062 (insert " ") 1050 (insert " ")
1063 (dictionary-link-insert-link word 1051 (insert-button word :type 'dictionary-button
1064 'dictionary-reference-face 1052 'callback 'dictionary-new-search
1065 'dictionary-new-search 1053 'data (cons word dictionary)
1066 (cons word dictionary) 1054 'help-echo (purecopy "Mouse-2 to lookup word"))
1067 "Mouse-2 to lookup word")
1068 (insert "\n")) (reverse word-list)) 1055 (insert "\n")) (reverse word-list))
1069 (insert "\n"))) 1056 (insert "\n")))
1070 list)) 1057 list))
@@ -1119,22 +1106,6 @@ It presents the word at point as default input and allows editing it."
1119 (error "Current buffer is no dictionary buffer")) 1106 (error "Current buffer is no dictionary buffer"))
1120 (dictionary-restore-state)) 1107 (dictionary-restore-state))
1121 1108
1122(defun dictionary-next-link ()
1123 "Place the cursor to the next link."
1124 (interactive)
1125 (let ((pos (dictionary-link-next-link)))
1126 (if pos
1127 (goto-char pos)
1128 (error "There is no next link"))))
1129
1130(defun dictionary-prev-link ()
1131 "Place the cursor to the previous link."
1132 (interactive)
1133 (let ((pos (dictionary-link-prev-link)))
1134 (if pos
1135 (goto-char pos)
1136 (error "There is no previous link"))))
1137
1138(defun dictionary-help () 1109(defun dictionary-help ()
1139 "Display a little help" 1110 "Display a little help"
1140 (interactive) 1111 (interactive)