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