1 ;;; liece-xemacs.el --- XEmacs specific routines.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
9 ;; This file is part of Liece.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
33 (require 'liece-inlines)
34 (require 'liece-crypt)
35 (require 'liece-commands))
37 (autoload 'liece-command-dcc-send "liece-dcc")
38 (defvar liece-nick-popup-menu)
40 (defgroup liece-toolbar nil
41 "Toolbar of your XEmacs"
45 (defgroup liece-toolbar-icons nil
46 "Toolbar Icons of your XEmacs"
48 :prefix "liece-toolbar-"
51 (defmacro liece-xemacs-icon-path (file)
52 "Search icon FILE and return absolete path of the file."
53 `(or (and liece-icon-directory
54 (expand-file-name ,file liece-icon-directory))
55 (let ((path (liece-find-path ,file "icons")))
57 (setq liece-icon-directory
58 (file-name-directory path)))
61 (define-widget 'liece-toolbar-icon 'list
62 "Edit toolbar spec entries"
63 :match (lambda (widget value)
64 (valid-plist-p value))
65 :convert-widget 'liece-toolbar-icon-convert)
68 (defconst liece-toolbar-icon-states
69 '(:up :down :disabled :cap-up :cap-down :cap-disabled)
70 "toolbar event states")
72 (defun liece-toolbar-icon-convert-1 (state)
73 (list 'group :inline t :format "%t: %v"
74 :tag (capitalize (substring (symbol-name state) 1))
75 (list 'const :format "" :value state
76 (list 'radio '(const :tag "none" nil) 'file)))))
78 (defun liece-toolbar-icon-convert (widget)
79 "Widget converter of the WIDGET `liece-toolbar-icon'."
80 (apply #'widget-put widget :args
82 (mapcar #'liece-toolbar-icon-convert-1
83 liece-toolbar-icon-states)))
86 (defcustom liece-use-toolbar (if (featurep 'toolbar)
89 "*If nil, do not use a toolbar.
90 If it is non-nil, it must be a toolbar. The five valid values are
91 `default-toolbar', `top-toolbar', `bottom-toolbar',
92 `right-toolbar', and `left-toolbar'."
93 :type '(choice (const default-toolbar)
94 (const top-toolbar) (const bottom-toolbar)
95 (const left-toolbar) (const right-toolbar)
96 (const :tag "no toolbar" nil))
97 :group 'liece-toolbar)
99 (defcustom liece-toolbar-back-icon '(:up "back.xpm")
101 :type 'liece-toolbar-icon
102 :group 'liece-toolbar-icons)
104 (defcustom liece-toolbar-forward-icon '(:up "forward.xpm")
106 :type 'liece-toolbar-icon
107 :group 'liece-toolbar-icons)
109 (defcustom liece-toolbar-reload-icon '(:up "reload.xpm")
111 :type 'liece-toolbar-icon
112 :group 'liece-toolbar-icons)
114 (defcustom liece-toolbar-home-icon '(:up "home.xpm")
116 :type 'liece-toolbar-icon
117 :group 'liece-toolbar-icons)
119 (defcustom liece-toolbar-search-icon '(:up "search.xpm")
121 :type 'liece-toolbar-icon
122 :group 'liece-toolbar-icons)
124 (defcustom liece-toolbar-location-icon '(:up "location.xpm")
126 :type 'liece-toolbar-icon
127 :group 'liece-toolbar-icons)
129 (defcustom liece-toolbar-crypt-active-icon '(:up "encrypt.xpm")
130 "Crypt button (active)."
131 :type 'liece-toolbar-icon
132 :group 'liece-toolbar-icons)
134 (defcustom liece-toolbar-crypt-inactive-icon '(:up "crypt.xpm")
135 "Crypt button (inactive)."
136 :type 'liece-toolbar-icon
137 :group 'liece-toolbar-icons)
139 (defcustom liece-toolbar-crypt-icon
140 liece-toolbar-crypt-inactive-icon
142 :type 'liece-toolbar-icon
143 :group 'liece-toolbar-icons)
145 (defcustom liece-toolbar-stop-icon '(:up "stop.xpm")
147 :type 'liece-toolbar-icon
148 :group 'liece-toolbar-icons)
150 ;;; @ internal variables
152 (defvar liece-glyph-cache nil)
153 (defvar liece-toolbar-position default-toolbar-position)
155 (defvar liece-toolbar-back-glyph nil)
156 (defvar liece-toolbar-forward-glyph nil)
157 (defvar liece-toolbar-reload-glyph nil)
158 (defvar liece-toolbar-home-glyph nil)
159 (defvar liece-toolbar-search-glyph nil)
160 (defvar liece-toolbar-location-glyph nil)
161 (defvar liece-toolbar-crypt-glyph nil)
162 (defvar liece-toolbar-crypt-active-glyph nil)
163 (defvar liece-toolbar-crypt-inactive-glyph nil)
164 (defvar liece-toolbar-stop-glyph nil)
166 (defvar liece-toolbar-spec-list
167 '([liece-toolbar-back-glyph
168 liece-command-previous-channel t "Previous Channel"]
169 [liece-toolbar-forward-glyph
170 liece-command-next-channel t "Next Channel"]
171 [liece-toolbar-reload-glyph
172 liece-command-list t "List Channel"]
173 [liece-toolbar-home-glyph
174 liece-switch-to-channel-no-1 t "Go Home Channel"]
175 [liece-toolbar-search-glyph
176 liece-command-finger t "Finger"]
177 [liece-toolbar-location-glyph
178 liece-command-join t "Join Channel"]
179 [liece-toolbar-crypt-glyph
180 liece-toolbar-toggle-crypt t "Toggle Crypt Mode"]
181 [liece-toolbar-stop-glyph
182 liece-command-quit t "Quit IRC"]))
186 (defun liece-toolbar-icon-plist-get (spec prop)
187 "Return absolete path of icon file which SPEC has PROP."
188 (let ((icon (plist-get spec prop)))
189 (if icon (liece-locate-icon-file icon))))
191 (defun liece-toolbar-map-button-list (plist)
192 "Make toolbar icon list based on status PLIST."
193 (apply #'toolbar-make-button-list
196 (liece-toolbar-icon-plist-get plist prop))
197 liece-toolbar-icon-states)))
199 (defun liece-xemacs-setup-toolbar (bar &optional force)
200 "Prepare icons of toolbar BAR.
201 If optional argument FORCE is non-nil, always update toolbar."
203 (set-default-toolbar-position liece-toolbar-position)
205 (setq icon (aref spec 0)
208 (substring (prin1-to-string icon) -5 0)
211 (not (symbol-value icon)))
212 (set icon (liece-toolbar-map-button-list plist))))
213 (run-hooks 'liece-xemacs-setup-toolbar-hook)))
215 (add-hook 'liece-xemacs-setup-toolbar-hook 'liece-toolbar-setup-crypt-glyph)
217 (defun liece-toolbar-setup-crypt-glyph ()
218 "Set crypt icons in two states."
219 (setq liece-toolbar-crypt-active-glyph
220 (liece-toolbar-map-button-list liece-toolbar-crypt-active-icon)
221 liece-toolbar-crypt-inactive-glyph
222 (liece-toolbar-map-button-list liece-toolbar-crypt-inactive-icon)))
224 (defun liece-toolbar-toggle-crypt ()
225 "Toolbar button handler for crypt mode."
227 (liece-command-toggle-crypt)
228 (setq liece-toolbar-crypt-glyph
229 (if liece-crypt-mode-active
230 liece-toolbar-crypt-active-glyph
231 liece-toolbar-crypt-inactive-glyph))
232 (and liece-use-toolbar
233 (set-specifier (symbol-value liece-use-toolbar)
234 (cons (current-buffer) liece-toolbar-spec-list))))
236 ;;; @ modeline decoration
238 (defun liece-xemacs-hide-modeline ()
239 "Remove modeline from current window."
240 (set-specifier has-modeline-p (cons (current-buffer) nil)))
242 (when (featurep 'scrollbar)
243 (defun liece-xemacs-hide-scrollbars ()
245 ((boundp 'horizontal-scrollbar-visible-p)
246 (set-specifier horizontal-scrollbar-visible-p nil
248 ((boundp 'scrollbar-height)
249 (set-specifier scrollbar-height (cons (current-buffer) 0)))))
250 (add-hook 'liece-nick-mode-hook 'liece-xemacs-hide-scrollbars)
251 (add-hook 'liece-channel-list-mode-hook 'liece-xemacs-hide-scrollbars))
253 (add-hook 'liece-nick-mode-hook 'liece-xemacs-hide-modeline)
254 (add-hook 'liece-channel-list-mode-hook 'liece-xemacs-hide-modeline)
256 (defvar liece-xemacs-modeline-left-extent
257 (let ((ext (copy-extent modeline-buffer-id-left-extent)))
260 (defvar liece-xemacs-modeline-right-extent
261 (let ((ext (copy-extent modeline-buffer-id-right-extent)))
264 (add-hook 'liece-command-mode-hook 'liece-setup-toolbar)
266 (defun liece-setup-toolbar ()
267 "Prepare toolbar if wanted."
268 (when liece-use-toolbar
269 (liece-xemacs-setup-toolbar liece-toolbar-spec-list)
270 (set-specifier (symbol-value liece-use-toolbar)
271 (cons (current-buffer) liece-toolbar-spec-list))))
273 (defun liece-xemacs-modeline-glyph ()
274 "Return a glyph of modeline pointer."
279 (if (setq file (liece-locate-icon-file
280 "liece-pointer.xpm"))
281 (list (vector 'xpm :file file)))
282 (if (setq file (liece-locate-icon-file
283 "liece-pointer.xbm"))
284 (list (vector 'xbm :file file)))
285 '([string :data "Liece:"]))))))
286 (set-glyph-face glyph 'modeline-buffer-id)
289 (defun liece-xemacs-mode-line-buffer-identification (line)
290 "Decorate 1st element of `mode-line-buffer-identification' LINE.
291 Modify whole identification by side effect."
292 (let ((id (car line)) chop)
293 (if (and (stringp id) (string-match "^Liece:" id))
295 (setq chop (match-end 0))
298 (let ((glyph (liece-xemacs-modeline-glyph)))
300 (cons liece-xemacs-modeline-left-extent glyph)
301 (cons liece-xemacs-modeline-left-extent
302 (substring id 0 chop))))
303 (cons liece-xemacs-modeline-right-extent
304 (substring id chop)))
308 (defun liece-xemacs-suppress-modeline-format ()
309 "Remove unnecessary information from `modeline-format'."
310 (setq modeline-format
311 (remrassq 'modeline-modified
312 (delq 'modeline-multibyte-status
313 (copy-sequence mode-line-format)))))
317 (defun liece-xemacs-nick-popup-menu (widget &optional event)
318 "Trigger function for popup menu."
319 (let ((pos (widget-event-point event)))
322 (if (eq major-mode 'liece-nick-mode)
323 (liece-nick-update-region))
324 (let ((menu (cdr liece-nick-popup-menu)))
325 (setq menu (nconc (list "IRCHAT" ; title: not displayed
327 "--:shadowDoubleEtchedOut")
328 (mapcar (lambda (spec)
333 (let (popup-menu-titles)
334 (popup-menu menu))))))
336 (fset 'liece-nick-popup-menu 'liece-xemacs-nick-popup-menu)
338 ;;; @ nick buffer decoration
340 (defun liece-xemacs-create-nick-glyph (file &optional string)
341 "Return a glyph of nick indicator from FILE or STRING."
343 (cdr-safe (assoc file liece-glyph-cache))
347 (if (setq file (liece-locate-icon-file file))
348 (list (vector 'xpm :file file)))
350 (list (vector 'string :data string)))))))
351 (push (cons file glyph) liece-glyph-cache)
352 (set-glyph-face glyph 'default)
355 (defun liece-xemacs-glyph-nick-region (start end)
356 "Decorate nick buffer between START and END."
358 (setq start (progn (goto-char start)(beginning-of-line)(point))
359 end (progn (goto-char end)(beginning-of-line 2)(point)))
361 (narrow-to-region start end)
362 (let ((buffer-read-only nil)
363 (inhibit-read-only t)
364 (case-fold-search nil)
365 mark file glyph ext ant)
370 e 'liece-xemacs-glyph-nick-extent)
372 e 'liece-xemacs-glyph-nick-annotation))
374 (current-buffer) start end)
375 (dolist (entry liece-nick-image-alist)
376 (setq mark (car entry)
378 glyph (liece-xemacs-create-nick-glyph
379 file (char-to-string mark)))
383 (when (eq (char-after) mark)
384 (mapcar 'delete-annotation
385 (annotations-at (1+ (point))))
386 (setq ext (make-extent (point) (1+ (point)))
387 ant (make-annotation glyph (1+ (point)) 'text))
388 (set-extent-property ext 'end-open t)
389 (set-extent-property ext 'start-open t)
390 (set-extent-property ext 'invisible t)
391 (set-extent-property ext 'intangible t)
393 ant 'liece-xemacs-glyph-nick-extent ext)
395 ext 'liece-xemacs-glyph-nick-annotation ant))
396 (beginning-of-line 2))))))))
398 (defun liece-xemacs-set-drop-functions (start end)
399 "Initialize drag and drop in DCC between START and END.
400 This function needs window system independent drag and drop
401 support (21.0 b39 or later)"
403 (liece-xemacs-set-drop-functions-buffer
404 (current-buffer) start end)
407 (defun liece-xemacs-set-drop-functions-buffer (&optional buffer start end)
408 "Initialize BUFFER drag and drop DCC settings between START and END.
409 This function needs window system independent drag and drop
410 support (21.0 b39 or later)"
412 (when (and (featurep 'x) (featurep 'dragdrop))
416 (setq start (or start (point-min))
417 end (or end (point-max)))
419 (setq start (line-beginning-position))
421 (setq end (line-beginning-position))
424 (beginning-of-line 2)
427 (narrow-to-region start end)
428 (let (buffer-read-only case-fold-search)
432 (when (extent-property e 'liece-xemacs-drop-extent)
436 (let (st nd nick func)
440 nd (line-end-position)
441 nick (buffer-substring st nd))
442 (mapcar 'delete-annotation (annotations-at nd))
443 (setq func (intern (concat "liece-xemacs-drop-function-" nick)))
445 (list 'lambda (list 'object)
446 (list 'liece-xemacs-drop-function 'object nick)))
447 (let ((ext (make-extent st nd)))
448 (set-extent-property ext 'liece-xemacs-drop-extent t)
449 (set-extent-property ext 'dragdrop-drop-functions (list func)))
450 (beginning-of-line 2))))))))
452 (defun liece-xemacs-drop-function (object nick)
453 "Drag and drop handler.
454 Always two arguments are passed, OBJECT and NICK."
455 (if (and (eq (car object) 'dragdrop_URL)
456 (stringp (cdr object))
457 (string-match "^[^:]*:\\(.*\\)" (cdr object)))
458 (let ((filename (match-string 1 (cdr object))))
459 (liece-command-dcc-send filename nick))))
461 (defadvice easy-menu-add-item
462 (around liece-fix-menu-path-switch-buffer activate)
463 "Advice for XEmacs 20.4 or earlier."
465 (set-buffer liece-command-buffer)
467 (cons (car (ad-get-arg 0)) (ad-get-arg 1))
468 (ad-get-arg 2) (ad-get-arg 3))))
471 (setq liece-x-face-insert-function
472 (function liece-x-face-insert-with-xemacs))
474 (defun liece-x-face-insert-with-xemacs (buffer str nick)
476 (let ((glyph (cdr-safe (assoc nick liece-glyph-cache))))
478 (setq glyph (make-glyph
480 ((and (featurep 'xface)
481 (memq (console-type) '(x mswindows)))
483 (t `[string :data ,str]))))
485 (push (cons nick glyph) liece-glyph-cache)
486 (set-glyph-face glyph 'default)))
488 (goto-char (point-max))
490 (set-extent-end-glyph (make-extent (point) (point)) glyph))))))
497 `(,(if (and (boundp 'filename)
499 (file-exists-p filename))
500 (file-name-directory filename)
504 (when (featurep 'xpm)
506 (defmacro liece-xemacs-logo ()
507 (let ((logo "liece.xpm")
508 (dir (if (and (boundp 'filename)
510 (file-exists-p filename))
511 (file-name-directory filename)
513 (setq logo (expand-file-name logo dir))
514 (if (file-exists-p logo)
515 (let ((buffer (generate-new-buffer " *liece-logo*"))
516 (coding-system-for-read (quote binary))
517 buffer-file-format format-alist
518 insert-file-contents-post-hook
519 insert-file-contents-pre-hook)
523 (insert-file-contents logo)
525 (kill-buffer buffer)))
528 "Warning: file \"%s\" not found." logo)
532 (defconst liece-xemacs-logo
533 (when (featurep 'xpm)
534 (liece-xemacs-logo)))
536 (defun liece-xemacs-splash-at-point (&optional height)
537 "Display splash logo in HEIGHT."
538 (or (bolp) (insert "\n"))
540 (glyph (make-glyph `[xpm :data ,liece-xemacs-logo]))
541 (lh (/ (window-pixel-height) (window-height)))
542 (lw (/ (window-pixel-width) (window-width)))
543 (liece-insert-environment-version nil)
546 (insert-char ?\n (max 0 (/ (- (or height (window-height))
547 (/ (glyph-height glyph) lh))
549 (insert-char ?\ (max 0 (/ (- (window-width)
550 (/ (glyph-width glyph) lw))
552 (when (and (featurep 'xpm) (memq (console-type) '(x mswindows)))
553 (set-extent-end-glyph
554 (make-extent (point) (point))
557 (insert-char ?\ (max 0 (/ (- (window-width) (length (liece-version))) 2)))
559 (insert (liece-version))
560 (and (find-face 'bold-italic)
561 (put-text-property bov (point) 'face 'bold-italic))
563 (set-window-start (get-buffer-window (current-buffer)) (point))
566 (defun liece-xemacs-splash (&optional arg)
567 "Display splash logo interactively.
568 If ARG is given, don't hide splash buffer."
570 (and liece-xemacs-logo
571 (let ((frame (selected-frame))
573 (liece-insert-environment-version nil))
577 (setq config (current-window-configuration))
579 (setq buffer (generate-new-buffer
580 (concat (if arg "*" " *")
581 (liece-version) "*"))))
582 (delete-other-windows)
583 (liece-xemacs-splash-at-point)
584 (set-buffer-modified-p nil)
585 (or arg (sleep-for 2)))
588 (set-window-configuration config)
589 (redisplay-frame frame)))))))
591 (or (eq 'stream (device-type))
592 (liece-xemacs-splash))
595 ;;; @ emulation functions
597 (defun liece-xemacs-map-extents (function)
598 "Map FUNCTION over the extents which overlap the current buffer."
599 (map-extents (lambda (extent ignore)
600 (if (overlayp extent) (funcall function extent)))))
602 (defun liece-xemacs-kill-all-overlays ()
603 "Delete all extents in the current buffer."
604 (liece-xemacs-map-extents #'delete-extent))
606 (defun liece-xemacs-overlays-at (pos)
607 "Return a list of the overlays that contain position POS."
608 (let ((ext (extent-at pos)))
609 (and ext (overlayp ext) (list ext))))
611 (fset 'liece-mode-line-buffer-identification
612 'liece-xemacs-mode-line-buffer-identification)
614 (fset 'liece-suppress-mode-line-format
615 'liece-xemacs-suppress-modeline-format)
617 (fset 'liece-kill-all-overlays 'liece-xemacs-kill-all-overlays)
618 (fset 'liece-map-overlays 'liece-xemacs-map-extents)
619 (fset 'liece-locate-data-directory 'locate-data-directory)
621 (add-hook 'liece-nick-insert-hook 'liece-xemacs-glyph-nick-region)
622 (add-hook 'liece-nick-insert-hook 'liece-xemacs-set-drop-functions)
624 (add-hook 'liece-nick-replace-hook 'liece-xemacs-glyph-nick-region)
625 (add-hook 'liece-nick-replace-hook 'liece-xemacs-set-drop-functions)
627 (provide 'liece-xemacs)
629 ;;; liece-xemacs.el ends here