Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / lisp / toolbar-items.el
1 ;;; toolbar-items.el -- Static initialization of XEmacs toolbar
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1994 Andy Piper <andyp@parallax.demon.co.uk>
5 ;; Copyright (C) 1995 Board of Trustees, University of Illinois
6 ;; Copyright (C) 1996 Ben Wing <ben@xemacs.org>
7
8 ;; Maintainer: XEmacs development team
9 ;; Keywords: frames, dumped
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the 
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Synched up:  Not in FSF
29
30 ;;; Commentary:
31
32 ;; This file is dumped with XEmacs (when window system and toolbar support
33 ;; is compiled in).
34
35 ;; Miscellaneous toolbar functions, useful for users to redefine, in
36 ;; order to get different behavior.
37
38 ;;; Code:
39
40 ;; Suppress warning message from bytecompiler
41 (eval-when-compile
42   (defvar pending-delete-mode)
43   ;; #### The compiler still warns about missing
44   ;; `pending-delete-pre-hook'.  Any way to get rid of the warning?
45   )
46
47 (defgroup toolbar nil
48   "Configure XEmacs Toolbar functions and properties"
49   :group 'environment)
50
51 ;; #### The following function is slightly obnoxious as it stands.  I
52 ;; think it should print a message like "Toolbar not configured; press
53 ;; me again to configure it", and when the button is pressed again
54 ;; (within a reasonable period of time), `customize-variable' should
55 ;; be invoked for the appropriate variable.
56
57 (defun toolbar-not-configured ()
58   (interactive)
59   ;; Note: we don't use `susbtitute-command-keys' here, because
60   ;; Customize is bound to `C-h C' by default, and that binding is not
61   ;; familiar to people.  This is more descriptive.
62   (error
63    "Configure the item via `M-x customize RET toolbar RET'"))
64
65 (defcustom toolbar-open-function 'find-file
66   "*Function to call when the open icon is selected."
67   :type '(radio (function-item find-file)
68                 (function :tag "Other"))
69   :group 'toolbar)
70
71 (defun toolbar-open ()
72   (interactive)
73   (call-interactively toolbar-open-function))
74
75 (defcustom toolbar-dired-function 'dired
76   "*Function to call when the dired icon is selected."
77   :type '(radio (function-item dired)
78                 (function :tag "Other"))
79   :group 'toolbar)
80
81 (defun toolbar-dired ()
82   (interactive)
83   (call-interactively toolbar-dired-function))
84
85 (defcustom toolbar-save-function 'save-buffer
86   "*Function to call when the save icon is selected."
87   :type '(radio (function-item save-buffer)
88                 (function :tag "Other"))
89   :group 'toolbar)
90
91 (defun toolbar-save ()
92   (interactive)
93   (call-interactively toolbar-save-function))
94
95 (defcustom toolbar-print-function 'lpr-buffer
96   "*Function to call when the print icon is selected."
97   :type '(radio (function-item lpr-buffer)
98                 (function :tag "Other"))
99   :group 'toolbar)
100
101 (defun toolbar-print ()
102   (interactive)
103   (call-interactively toolbar-print-function))
104
105 (defcustom toolbar-cut-function 'kill-primary-selection
106   "*Function to call when the cut icon is selected."
107   :type '(radio (function-item kill-primary-selection)
108                 (function :tag "Other"))
109   :group 'toolbar)
110
111 (defun toolbar-cut ()
112   (interactive)
113   (call-interactively toolbar-cut-function))
114
115 (defcustom toolbar-copy-function 'copy-primary-selection
116   "*Function to call when the copy icon is selected."
117   :type '(radio (function-item copy-primary-selection)
118                 (function :tag "Other"))
119   :group 'toolbar)
120
121 (defun toolbar-copy ()
122   (interactive)
123   (call-interactively toolbar-copy-function))
124
125 (defcustom toolbar-paste-function 'yank-clipboard-selection
126   "*Function to call when the paste icon is selected."
127   :type '(radio (function-item yank-clipboard-selection)
128                 (function :tag "Other"))
129   :group 'toolbar)
130
131 (defun toolbar-paste ()
132   (interactive)
133   ;; This horrible kludge is for pending-delete to work correctly.
134   (and (boundp 'pending-delete-mode)
135        pending-delete-mode
136        (let ((this-command toolbar-paste-function))
137          (pending-delete-pre-hook)))
138   (call-interactively toolbar-paste-function))
139
140 (defcustom toolbar-undo-function 'undo
141   "*Function to call when the undo icon is selected."
142   :type '(radio (function-item undo)
143                 (function :tag "Other"))
144   :group 'toolbar)
145
146 (defun toolbar-undo ()
147   (interactive)
148   (call-interactively toolbar-undo-function))
149
150 (defcustom toolbar-replace-function 'query-replace
151   "*Function to call when the replace icon is selected."
152   :type '(radio (function-item query-replace)
153                 (function :tag "Other"))
154   :group 'toolbar)
155
156 (defun toolbar-replace ()
157   (interactive)
158   (call-interactively toolbar-replace-function))
159
160 ;;
161 ;; toolbar ispell variables and defuns
162 ;;
163
164 (defun toolbar-ispell-internal ()
165   (interactive)
166   (cond
167    ((region-active-p) (ispell-region (region-beginning) (region-end)))
168    ((eq major-mode 'mail-mode) (ispell-message))
169    ((eq major-mode 'message-mode) (ispell-message))
170    (t (ispell-buffer))))
171
172 (defcustom toolbar-ispell-function 'toolbar-ispell-internal
173   "*Function to call when the ispell icon is selected."
174   :type '(radio (function-item toolbar-ispell-internal)
175                 (function :tag "Other"))
176   :group 'toolbar)
177
178 (defun toolbar-ispell ()
179   "Intelligently spell the region or buffer."
180   (interactive)
181   (call-interactively toolbar-ispell-function))
182
183 ;;
184 ;; toolbar mail variables and defuns
185 ;;
186
187 ;; This used to be a macro that expanded its arguments to a form that
188 ;; called `call-process'.  With the advent of customize, it's better
189 ;; to have it as a defun, to make customization easier.
190 (defun toolbar-external (process &rest args)
191   (interactive)
192   (apply 'call-process process nil 0 nil args))
193
194 (defcustom toolbar-mail-commands-alist
195   `((not-configured . toolbar-not-configured)
196     (vm         . vm)
197     (gnus       . gnus-no-server)
198     (rmail      . rmail)
199     (mh         . mh-rmail)
200     (pine       . (toolbar-external "xterm" "-e" "pine")) ; *gag*
201     (elm        . (toolbar-external "xterm" "-e" "elm"))
202     (mutt       . (toolbar-external "xterm" "-e" "mutt"))
203     (exmh       . (toolbar-external "exmh"))
204     (netscape   . (toolbar-external "netscape" "mailbox:"))
205     (send       . mail))
206   "*Alist of mail readers and their commands.
207 The car of each alist element is the mail reader, and the cdr is the form
208 used to start it."
209   :type '(repeat (cons :format "%v"
210                        (symbol :tag "Mailer") (function :tag "Start with")))
211   :group 'toolbar)
212
213 (defcustom toolbar-mail-reader 'not-configured
214   "*Mail reader toolbar will invoke.
215 The legal values are the keys from `toolbar-mail-command-alist', which
216  should be used to add new mail readers.
217 Mail readers known by default are vm, gnus, rmail, mh, pine, elm,
218  mutt, exmh, netscape and send."
219   :type '(choice (const :tag "Not Configured" not-configured)
220                  (const vm) (const gnus) (const rmail) (const mh)
221                  (const pine) (const elm) (const mutt) (const exmh)
222                  (const netscape)
223                  (const send)
224                  (symbol :tag "Other"
225                          :validate (lambda (wid)
226                                      (if (assq (widget-value wid)
227                                                toolbar-mail-commands-alist)
228                                          nil
229                                        (widget-put wid :error
230                                                    "Unknown mail reader")
231                                        wid))))
232   :group 'toolbar)
233
234
235 (defun toolbar-mail ()
236   "Run mail in a separate frame."
237   (interactive)
238   (let ((command (cdr (assq toolbar-mail-reader toolbar-mail-commands-alist))))
239     (or command
240         (error "Uknown mail reader %s" toolbar-mail-reader))
241     (if (symbolp command)
242         (call-interactively command)
243       (eval command))))
244
245 ;;
246 ;; toolbar info variables and defuns
247 ;;
248
249 (defcustom toolbar-info-use-separate-frame t
250   "*Whether Info is invoked in a separate frame."
251   :type 'boolean
252   :group 'toolbar)
253
254 (defcustom toolbar-info-frame-plist
255   ;; Info pages are 80 characters wide, so it makes a good default.
256   `(width 80 ,@(let ((h (plist-get default-frame-plist 'height)))
257                  (and h `(height ,h))))
258   "*The properties of the frame in which news is displayed."
259   :type 'plist
260   :group 'info)
261
262 (define-obsolete-variable-alias 'Info-frame-plist
263   'toolbar-info-frame-plist)
264
265 (defvar toolbar-info-frame nil
266   "The frame in which info is displayed.")
267
268 (defun toolbar-info ()
269   "Run info in a separate frame."
270   (interactive)
271   (when toolbar-info-use-separate-frame
272     (cond ((or (not toolbar-info-frame)
273                (not (frame-live-p toolbar-info-frame)))
274            ;; We used to raise frame here, but it's a bad idea,
275            ;; because raising is a matter of WM policy.  However, we
276            ;; *must* select it, to ensure that the info buffer goes to
277            ;; the right frame.
278            (setq toolbar-info-frame (make-frame toolbar-info-frame-plist))
279            (select-frame toolbar-info-frame))
280           (t
281            ;; However, if the frame already exists, and the user
282            ;; clicks on info, it's OK to raise it.
283            (select-frame toolbar-info-frame)
284            (raise-frame toolbar-info-frame)))
285     (when (frame-iconified-p toolbar-info-frame)
286       (deiconify-frame toolbar-info-frame)))
287   (info))
288
289 ;;
290 ;; toolbar debug variables and defuns
291 ;;
292
293 (defun toolbar-debug ()
294   (interactive)
295   (if (featurep 'eos-debugger)
296       (call-interactively 'eos::start-debugger)
297     (require 'gdbsrc)
298     (call-interactively 'gdbsrc)))
299
300 (defvar compile-command)
301 (defvar toolbar-compile-already-run nil)
302
303 (defun toolbar-compile ()
304   "Run compile without having to touch the keyboard."
305   (interactive)
306   (require 'compile)
307   (if toolbar-compile-already-run
308       (compile compile-command)
309     (setq toolbar-compile-already-run t)
310     (if (should-use-dialog-box-p)
311        (popup-dialog-box
312         `(,(concat "Compile:\n        " compile-command)
313           ["Compile" (compile compile-command) t]
314           ["Edit command" compile t]
315           nil
316           ["Cancel" (message "Quit") t]))
317       (compile compile-command))))
318
319 ;;
320 ;; toolbar news variables and defuns
321 ;;
322
323 (defcustom toolbar-news-commands-alist
324   `((not-configured . toolbar-not-configured)
325     (gnus       . toolbar-gnus)                 ; M-x all-hail-gnus
326     (rn         . (toolbar-external "xterm" "-e" "rn"))
327     (nn         . (toolbar-external "xterm" "-e" "nn"))
328     (trn        . (toolbar-external "xterm" "-e" "trn"))
329     (xrn        . (toolbar-external "xrn"))
330     (slrn       . (toolbar-external "xterm" "-e" "slrn"))
331     (pine       . (toolbar-external "xterm" "-e" "pine")) ; *gag*
332     (tin        . (toolbar-external "xterm" "-e" "tin")) ; *gag*
333     (netscape   . (toolbar-external "netscape" "news:")))
334   "*Alist of news readers and their commands.
335 The car of each alist element the pair is the news reader, and the cdr
336 is the form used to start it."
337   :type '(repeat (cons :format "%v"
338                        (symbol :tag "Reader") (sexp :tag "Start with")))
339   :group 'toolbar)
340
341 (defcustom toolbar-news-reader 'gnus
342   "*News reader toolbar will invoke.
343 The legal values are the keys from `toolbar-news-command-alist', which should
344  be used to add new news readers.
345 Newsreaders known by default are gnus, rn, nn, trn, xrn, slrn, pine
346  and netscape."
347   :type '(choice (const :tag "Not Configured" not-configured)
348                  (const gnus) (const rn) (const nn) (const trn)
349                  (const xrn) (const slrn) (const pine) (const tin)
350                  (const netscape)
351                  (symbol :tag "Other"
352                          :validate (lambda (wid)
353                                      (if (assq (widget-value wid)
354                                                toolbar-news-commands-alist)
355                                          nil
356                                        (widget-put wid :error
357                                                    "Unknown news reader")
358                                        wid))))
359   :group 'toolbar)
360
361 (defcustom toolbar-news-use-separate-frame t
362   "*Whether Gnus is invoked in a separate frame."
363   :type 'boolean
364   :group 'toolbar)
365
366 (defvar toolbar-news-frame nil
367   "The frame in which news is displayed.")
368
369 (defcustom toolbar-news-frame-plist nil
370   "*The properties of the frame in which news is displayed."
371   :type 'plist
372   :group 'toolbar)
373
374 (define-obsolete-variable-alias 'toolbar-news-frame-properties
375   'toolbar-news-frame-plist)
376
377 (defun toolbar-gnus ()
378   "Run Gnus in a separate frame."
379   (interactive)
380   (if (not toolbar-news-use-separate-frame)
381       (gnus)
382     (unless (frame-live-p toolbar-news-frame)
383       (setq toolbar-news-frame (make-frame toolbar-news-frame-plist))
384       (add-hook 'gnus-exit-gnus-hook
385                 (lambda ()
386                   (when (frame-live-p toolbar-news-frame)
387                     (if (cdr (frame-list))
388                         (delete-frame toolbar-news-frame))
389                     (setq toolbar-news-frame nil))))
390       (select-frame toolbar-news-frame)
391       (gnus))
392     (when (framep toolbar-news-frame)
393       (when (frame-iconified-p toolbar-news-frame)
394         (deiconify-frame toolbar-news-frame))
395       (select-frame toolbar-news-frame)
396       (raise-frame toolbar-news-frame))))
397
398 (defun toolbar-news ()
399   "Run News."
400   (interactive)
401   (let ((command (cdr-safe
402                   (assq toolbar-news-reader toolbar-news-commands-alist))))
403     (or command
404         (error "Unkown news reader %s" toolbar-news-reader))
405     (if (symbolp command)
406         (call-interactively command)
407       (eval command))))
408
409 (defvar toolbar-last-win-icon nil "A `last-win' icon set.")
410 (defvar toolbar-next-win-icon nil "A `next-win' icon set.")
411 (defvar toolbar-file-icon     nil "A `file' icon set.")
412 (defvar toolbar-folder-icon   nil "A `folder' icon set")
413 (defvar toolbar-disk-icon     nil "A `disk' icon set.")
414 (defvar toolbar-printer-icon  nil "A `printer' icon set.")
415 (defvar toolbar-cut-icon      nil "A `cut' icon set.")
416 (defvar toolbar-copy-icon     nil "A `copy' icon set.")
417 (defvar toolbar-paste-icon    nil "A `paste' icon set.")
418 (defvar toolbar-undo-icon     nil "An `undo' icon set.")
419 (defvar toolbar-spell-icon    nil "A `spell' icon set.")
420 (defvar toolbar-replace-icon  nil "A `replace' icon set.")
421 (defvar toolbar-mail-icon     nil "A `mail' icon set.")
422 (defvar toolbar-info-icon     nil "An `info' icon set.")
423 (defvar toolbar-compile-icon  nil "A `compile' icon set.")
424 (defvar toolbar-debug-icon    nil "A `debugger' icon set.")
425 (defvar toolbar-news-icon     nil "A `news' icon set.")
426
427 ;;; each entry maps a variable to the prefix used.
428
429 (defvar init-x-toolbar-list
430   '((toolbar-last-win-icon . "last-win")
431     (toolbar-next-win-icon . "next-win")
432     (toolbar-file-icon     . "file")
433     (toolbar-folder-icon   . "folder")
434     (toolbar-disk-icon     . "disk")
435     (toolbar-printer-icon  . "printer")
436     (toolbar-cut-icon      . "cut")
437     (toolbar-copy-icon     . "copy")
438     (toolbar-paste-icon    . "paste")
439     (toolbar-undo-icon     . "undo")
440     (toolbar-spell-icon    . "spell")
441     (toolbar-replace-icon  . "replace")
442     (toolbar-mail-icon     . "mail")
443     (toolbar-info-icon     . "info-def")
444     (toolbar-compile-icon  . "compile")
445     (toolbar-debug-icon    . "debug")
446     (toolbar-news-icon     . "news")))
447
448 (defun init-x-toolbar ()
449   (toolbar-add-item-data init-x-toolbar-list )
450   ;; do this now because errors will occur if the icon symbols
451   ;; are not initted
452   (set-specifier default-toolbar initial-toolbar-spec))
453   
454 (defun toolbar-add-item-data ( icon-list &optional icon-dir )
455   (if (eq icon-dir nil)
456       (setq icon-dir toolbar-icon-directory))
457   (mapcar
458    (lambda (cons)
459      (let ((prefix (expand-file-name (cdr cons)  icon-dir)))
460        ;; #### This should use a better mechanism for finding the
461        ;; glyphs, allowing for formats other than x[pb]m.  Look at
462        ;; `widget-glyph-find' for an example how it might be done.
463        (set (car cons)
464             (if (featurep 'xpm)
465                 (toolbar-make-button-list
466                  (concat prefix "-up.xpm")
467                  nil
468                  (concat prefix "-xx.xpm")
469                  (concat prefix "-cap-up.xpm")
470                  nil
471                  (concat prefix "-cap-xx.xpm"))
472               (toolbar-make-button-list
473                (concat prefix "-up.xbm")
474                (concat prefix "-dn.xbm")
475                (concat prefix "-xx.xbm"))))))
476    icon-list))
477
478 (defvar toolbar-vector-open 
479   [toolbar-file-icon            toolbar-open    t       "Open a file"]
480   "Define the vector for the \"Open\" toolbar button")
481
482 (defvar toolbar-vector-dired
483   [toolbar-folder-icon          toolbar-dired   t       "Edit a directory"]
484   "Define the vector for the \"Dired\" toolbar button")
485
486 (defvar toolbar-vector-save
487   [toolbar-disk-icon            toolbar-save    t       "Save buffer"]
488   "Define the vector for the \"Save\" toolbar button")
489
490 (defvar toolbar-vector-print
491   [toolbar-printer-icon         toolbar-print   t       "Print buffer"]
492   "Define the vector for the \"Printer\" toolbar button")
493
494 (defvar toolbar-vector-cut
495   [toolbar-cut-icon             toolbar-cut     t       "Kill region"]
496   "Define the vector for the \"Cut\" toolbar button")
497
498 (defvar toolbar-vector-copy
499   [toolbar-copy-icon            toolbar-copy    t       "Copy region"]
500   "Define the vector for the \"Copy\" toolbar button")
501
502 (defvar toolbar-vector-paste
503   [toolbar-paste-icon           toolbar-paste   t       "Paste from clipboard"]
504   "Define the vector for the \"Paste\" toolbar button")
505
506 (defvar toolbar-vector-undo
507   [toolbar-undo-icon            toolbar-undo    t       "Undo edit"]
508   "Define the vector for the \"Undo\" toolbar button")
509
510 (defvar toolbar-vector-spell
511   [toolbar-spell-icon           toolbar-ispell  t       "Check spelling"]
512   "Define the vector for the \"Spell\" toolbar button")
513
514 (defvar toolbar-vector-replace
515   [toolbar-replace-icon         toolbar-replace t       "Search & Replace"]  
516   "Define the vector for the \"Replace\" toolbar button")
517
518 (defvar toolbar-vector-mail
519   [toolbar-mail-icon            toolbar-mail    t       "Read mail"]
520   "Define the vector for the \"Mail\" toolbar button")
521
522 (defvar toolbar-vector-info
523   [toolbar-info-icon            toolbar-info    t       "Info documentation"]
524   "Define the vector for the \"Info\" toolbar button")
525
526 (defvar toolbar-vector-compile
527   [toolbar-compile-icon         toolbar-compile t       "Start a compilation"]
528   "Define the vector for the \"Compile\" toolbar button")
529
530 (defvar toolbar-vector-debug
531   [toolbar-debug-icon           toolbar-debug   t       "Start a debugger"]
532   "Define the vector for the \"Debug\" toolbar button")
533
534 (defvar toolbar-vector-news
535   [toolbar-news-icon            toolbar-news    t       "Read news"]
536   "Define the vector for the \"News\" toolbar button")
537
538 (defvar initial-toolbar-spec
539   (list
540     ;;[toolbar-last-win-icon    pop-window-configuration
541     ;;(frame-property (selected-frame)
542     ;;          'window-config-stack) t "Most recent window config"]
543     ;; #### Illicit knowledge?
544     ;; #### These don't work right - not consistent!
545     ;; I don't know what's wrong; perhaps `selected-frame' is wrong
546     ;; sometimes when this is evaluated.  Note that I even tried to
547     ;; kludge-fix this by calls to `set-specifier-dirty-flag' in
548     ;; pop-window-configuration and such.
549
550     ;;[toolbar-next-win-icon    unpop-window-configuration
551     ;;(frame-property (selected-frame)
552     ;;  'window-config-unpop-stack) t "Undo \"Most recent window config\""]
553     ;; #### Illicit knowledge?
554     toolbar-vector-open
555     toolbar-vector-dired
556     toolbar-vector-save
557     toolbar-vector-print
558     toolbar-vector-cut
559     toolbar-vector-copy
560     toolbar-vector-paste
561     toolbar-vector-undo
562     toolbar-vector-spell
563     toolbar-vector-replace
564     toolbar-vector-mail
565     toolbar-vector-info
566     toolbar-vector-compile
567     toolbar-vector-debug
568     toolbar-vector-news
569     )
570   "The initial toolbar for a buffer.")
571
572 (defun x-init-toolbar-from-resources (locale)
573   (x-init-specifier-from-resources
574    top-toolbar-height 'natnum locale
575    '("topToolBarHeight" . "TopToolBarHeight"))
576   (x-init-specifier-from-resources
577    bottom-toolbar-height 'natnum locale
578    '("bottomToolBarHeight" . "BottomToolBarHeight"))
579   (x-init-specifier-from-resources
580    left-toolbar-width 'natnum locale
581    '("leftToolBarWidth" . "LeftToolBarWidth"))
582   (x-init-specifier-from-resources
583    right-toolbar-width 'natnum locale
584    '("rightToolBarWidth" . "RightToolBarWidth"))
585   (x-init-specifier-from-resources
586    top-toolbar-border-width 'natnum locale
587    '("topToolBarBorderWidth" . "TopToolBarBorderWidth"))
588   (x-init-specifier-from-resources
589    bottom-toolbar-border-width 'natnum locale
590    '("bottomToolBarBorderWidth" . "BottomToolBarBorderWidth"))
591   (x-init-specifier-from-resources
592    left-toolbar-border-width 'natnum locale
593    '("leftToolBarBorderWidth" . "LeftToolBarBorderWidth"))
594   (x-init-specifier-from-resources
595    right-toolbar-border-width 'natnum locale
596    '("rightToolBarBorderWidth" . "RightToolBarBorderWidth")))
597
598 ;;; toolbar-items.el ends here