scheme interaction mode
[bpt/emacs.git] / lisp / net / eudc-hotlist.el
CommitLineData
c38e0c97 1;;; eudc-hotlist.el --- hotlist management for EUDC -*- coding: utf-8 -*-
7970b229 2
ba318903 3;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
7970b229 4
89d0ce25 5;; Author: Oscar Figueiredo <oscar@cpe.fr>
c38e0c97 6;; Maintainer: Pavel Janík <Pavel@Janik.cz>
ab651127 7;; Keywords: comm
bd78fa1d 8;; Package: eudc
7970b229
GM
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
7970b229 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
7970b229
GM
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7970b229
GM
24
25;;; Commentary:
26
27;;; Usage:
28;; See the corresponding info file
29
30;;; Code:
31
32(require 'eudc)
33
34(defvar eudc-hotlist-menu nil)
7970b229
GM
35(defvar eudc-hotlist-list-beginning nil)
36
a0310a6c
DN
37(defvar eudc-hotlist-mode-map
38 (let ((map (make-sparse-keymap)))
39 (define-key map "a" 'eudc-hotlist-add-server)
40 (define-key map "d" 'eudc-hotlist-delete-server)
41 (define-key map "s" 'eudc-hotlist-select-server)
42 (define-key map "t" 'eudc-hotlist-transpose-servers)
43 (define-key map "q" 'eudc-hotlist-quit-edit)
44 (define-key map "x" 'kill-this-buffer)
45 map))
46
1b3b87df 47(define-derived-mode eudc-hotlist-mode fundamental-mode "EUDC-Servers"
7970b229
GM
48 "Major mode used to edit the hotlist of servers.
49
50These are the special commands of this mode:
51 a -- Add a new server to the list.
52 d -- Delete the server at point from the list.
53 s -- Select the server at point.
54 t -- Transpose the server at point and the previous one
55 q -- Commit the changes and quit.
da6062e6 56 x -- Quit without committing the changes."
5ba583d1
RS
57 (when (featurep 'xemacs)
58 (setq mode-popup-menu eudc-hotlist-menu)
59 (when (featurep 'menubar)
60 (set-buffer-menubar current-menubar)
61 (add-submenu nil (cons "EUDC-Hotlist" (cdr (cdr eudc-hotlist-menu))))))
1b3b87df 62 (setq buffer-read-only t))
7970b229
GM
63
64;;;###autoload
65(defun eudc-edit-hotlist ()
66 "Edit the hotlist of directory servers in a specialized buffer."
67 (interactive)
082d147b 68 (let ((proto-col 10)
7970b229
GM
69 gap)
70 (switch-to-buffer (get-buffer-create "*EUDC Servers*"))
71 (setq buffer-read-only nil)
72 (erase-buffer)
1b3b87df
SM
73 (dolist (entry eudc-server-hotlist)
74 (setq proto-col (max (length (car entry)) proto-col)))
7970b229
GM
75 (setq proto-col (+ 3 proto-col))
76 (setq gap (make-string (- proto-col 6) ?\ ))
77 (insert " EUDC Servers\n"
78 " ============\n"
79 "\n"
80 "Server" gap "Protocol\n"
81 "------" gap "--------\n"
82 "\n")
83 (setq eudc-hotlist-list-beginning (point))
1b3b87df
SM
84 (dolist (entry eudc-server-hotlist)
85 (insert (car entry))
86 (indent-to proto-col)
87 (insert (symbol-name (cdr entry)) "\n"))
88 (eudc-hotlist-mode)))
7970b229
GM
89
90(defun eudc-hotlist-add-server ()
91 "Add a new server to the list after current one."
92 (interactive)
1b3b87df 93 (if (not (derived-mode-p 'eudc-hotlist-mode))
7970b229
GM
94 (error "Not in a EUDC hotlist edit buffer"))
95 (let ((server (read-from-minibuffer "Server: "))
96 (protocol (completing-read "Protocol: "
4f91a816 97 (mapcar (lambda (elt)
7970b229
GM
98 (cons (symbol-name elt)
99 elt))
100 eudc-known-protocols)))
101 (buffer-read-only nil))
102 (if (not (eobp))
103 (forward-line 1))
104 (insert server)
105 (indent-to 30)
106 (insert protocol "\n")))
107
108(defun eudc-hotlist-delete-server ()
109 "Delete the server at point from the list."
110 (interactive)
1b3b87df 111 (if (not (derived-mode-p 'eudc-hotlist-mode))
7970b229
GM
112 (error "Not in a EUDC hotlist edit buffer"))
113 (let ((buffer-read-only nil))
114 (save-excursion
115 (beginning-of-line)
aed3fbc3 116 (if (and (>= (point) eudc-hotlist-list-beginning)
7970b229
GM
117 (looking-at "^\\([-.a-zA-Z:0-9]+\\)[ \t]+\\([a-zA-Z]+\\)"))
118 (kill-line 1)
119 (error "No server on this line")))))
120
121(defun eudc-hotlist-quit-edit ()
122 "Quit the hotlist editing mode and save changes to the hotlist."
123 (interactive)
1b3b87df 124 (if (not (derived-mode-p 'eudc-hotlist-mode))
7970b229
GM
125 (error "Not in a EUDC hotlist edit buffer"))
126 (let (hotlist)
127 (goto-char eudc-hotlist-list-beginning)
128 (while (looking-at "^\\([-.a-zA-Z:0-9]+\\)[ \t]+\\([a-zA-Z]+\\)")
129 (setq hotlist (cons (cons (match-string 1)
130 (intern (match-string 2)))
131 hotlist))
132 (forward-line 1))
133 (if (not (looking-at "^[ \t]*$"))
aed3fbc3 134 (error "Malformed entry in hotlist, discarding edits"))
7970b229
GM
135 (setq eudc-server-hotlist (nreverse hotlist))
136 (eudc-install-menu)
137 (eudc-save-options)
138 (kill-this-buffer)))
139
140(defun eudc-hotlist-select-server ()
141 "Select the server at point as the current server."
142 (interactive)
1b3b87df 143 (if (not (derived-mode-p 'eudc-hotlist-mode))
7970b229
GM
144 (error "Not in a EUDC hotlist edit buffer"))
145 (save-excursion
146 (beginning-of-line)
147 (if (and (>= (point) eudc-hotlist-list-beginning)
148 (looking-at "^\\([-.a-zA-Z:0-9]+\\)[ \t]+\\([a-zA-Z]+\\)"))
149 (progn
150 (eudc-set-server (match-string 1) (intern (match-string 2)))
151 (message "Current directory server is %s (%s)" eudc-server eudc-protocol))
152 (error "No server on this line"))))
aed3fbc3 153
7970b229
GM
154(defun eudc-hotlist-transpose-servers ()
155 "Swap the order of the server with the previous one in the list."
156 (interactive)
1b3b87df 157 (if (not (derived-mode-p 'eudc-hotlist-mode))
7970b229
GM
158 (error "Not in a EUDC hotlist edit buffer"))
159 (let ((buffer-read-only nil))
160 (save-excursion
161 (beginning-of-line)
162 (if (and (>= (point) eudc-hotlist-list-beginning)
163 (looking-at "^\\([-.a-zA-Z:0-9]+\\)[ \t]+\\([a-zA-Z]+\\)")
aed3fbc3 164 (progn
7970b229
GM
165 (forward-line -1)
166 (looking-at "^\\([-.a-zA-Z:0-9]+\\)[ \t]+\\([a-zA-Z]+\\)")))
167 (progn
168 (forward-line 1)
169 (transpose-lines 1))))))
aed3fbc3 170
7970b229
GM
171(defconst eudc-hotlist-menu
172 '("EUDC Hotlist Edit"
173 ["---" nil nil]
174 ["Add New Server" eudc-hotlist-add-server t]
175 ["Delete Server" eudc-hotlist-delete-server t]
176 ["Select Server" eudc-hotlist-select-server t]
177 ["Transpose Servers" eudc-hotlist-transpose-servers t]
178 ["Save and Quit" eudc-hotlist-quit-edit t]
179 ["Exit without Saving" kill-this-buffer t]))
180
f8246027
DN
181(when (not (featurep 'xemacs))
182 (easy-menu-define eudc-hotlist-emacs-menu
183 eudc-hotlist-mode-map
184 ""
185 eudc-hotlist-menu))
7970b229
GM
186
187;;; eudc-hotlist.el ends here