eudc-hotlist.el 6.4 KB

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