Initial Commit
[packages] / xemacs-packages / auctex / tex-bar.el
1 ;;; tex-bar.el --- toolbar icons on AUCTeX in GNU emacs and XEmacs
2
3 ;; Copyright (C) 2004-2008, 2012-2014 Free Software Foundation, Inc.
4
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation; either version 3 of
8 ;; the License, or (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be
11 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
12 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 ;; PURPOSE.  See the GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public
16 ;; License along with this program; if not, write to the Free
17 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 ;; MA 02110-1301 USA
19
20 ;; Author: Miguel V. S. Frasson <frasson@math.leidenuniv.nl>
21 ;; Keywords: tool-bar, tex, latex
22
23 ;;; Commentary:
24 ;;
25
26 ;; This package also needs `toolbar-x.el', and `latex.el' for the
27 ;; symbol-toolbar.
28
29 ;;; Use of this preliminary version:
30
31 ;; -  Add `LaTeX-install-toolbar' to `LaTeX-mode-hook'.
32
33 ;; Special requirements for the use of experimental symbol-toolbar:
34
35 ;; -  Customize `TeX-bar-LaTeX-buttons', adding the label
36 ;;    `LaTeX-symbols-experimental' at the end.
37
38 ;; -  You should have a folder called "symb-pics" with the pics of the
39 ;;    symbols (xpm format is a good one), and the *parent* of this
40 ;;    folder should be in `load-path'.
41
42 ;; Did you read carefully this item?  I will say again: the folder
43 ;; "symb-pics" should *not* be in `load-path', but its *parent*.
44
45 ;; -  each image file is named after the command that it represents in
46 ;;    the following rules: the base name is the name of the command
47 ;;    without the escape character "\", like \delta -> "delta.xpm";
48 ;;    however, since in some OS filenames are case insensitive, all
49 ;;    occurences of capital letter should be replaced by the letter
50 ;;    plus a dash: \Rightarrow -> "R-ightarrow.xpm" --- actually, for
51 ;;    the correct name, apply `TeX-bar-img-filename' to "Rightarrow"
52 ;;     (TeX-bar-img-filename "Rightarrow")
53 ;;            -->  "R-ightarrow"
54 ;;    The function `TeX-bar-img-filename' also treats special commands
55 ;;    like `\{', `\|', etc.
56
57 ;; You can get the symbol images on (temporary solution)
58 ;;    http://www.math.leidenuniv.nl/~frasson/symb-pics.tar.gz
59
60 ;;; Code:
61
62 (require 'custom)
63
64 (require 'toolbar-x)
65
66 ;; for error handling
67 (require 'tex-buf)
68
69 ;; For the symbol toolbar
70 (require 'latex)
71
72 ;;; Standard buttons
73
74 ;; help strings
75 (defun TeX-bar-help-from-command-list (item)
76   "Return the help string of ITEM in `TeX-command-list'.
77 If there is no help, the empty string is returned."
78   (let ((help (nth 1 (memq :help (assoc item TeX-command-list)))))
79     (if help help "")))
80
81 (defgroup TeX-tool-bar nil
82   "Tool bar support in AUCTeX."
83   :group 'AUCTeX)
84
85 (defcustom TeX-bar-TeX-buttons
86   '(new-file open-file dired kill-buffer save-buffer cut copy paste undo
87              [separator nil] tex next-error view bibtex spell)
88   "List of buttons available in `tex-mode'.
89 It should be a list in the same format of the BUTTONS parameter
90 in function `toolbarx-install-toolbar', often a symbol that
91 labels a button or Emacs/XEmacs choice of buttons.
92
93 Type `\\[TeX-bar-TeX-buttons]' for a list of available buttons.
94
95 Buttons are defined in alists (labels associated to properties
96 that define a button).  For a list of variables that hold such
97 alists, see variable `TeX-bar-TeX-all-button-alists'."
98   :type '(list (set :inline t
99                     (const new-file)
100                     (const open-file)
101                     (const dired)
102                     (const kill-buffer)
103                     (const save-buffer)
104                     (const write-file)
105                     (const undo)
106                     (const cut)
107                     (const copy)
108                     (const paste)
109                     (const search-forward)
110                     (const print-buffer)
111                     (const [separator nil])
112                     (const tex)
113                     (const next-error)
114                     (const view)
115                     (const file)
116                     (const bibtex)
117                     (const clean)
118                     (const spell))
119                     ;; (const latex-symbols-experimental)
120                (repeat (choice (symbol :tag "Label")
121                                (vector :args ((symbol :tag "Label in Emacs ")
122                                               (symbol :tag "Label in XEmacs"))
123                                        :tag "Emacs/XEmacs choice")
124                                (sexp :tag "General element"))))
125   :group 'TeX-tool-bar)
126
127 (defun TeX-bar-TeX-buttons ()
128   "Display in a buffer a list of buttons for `tex-bar.el'."
129   (interactive)
130   (let ((assqs-button-alists)
131         (labels))
132     (dolist (m-alist TeX-bar-TeX-all-button-alists)
133       (setq labels nil)
134       (dolist (as (eval m-alist))
135         (setq labels (cons (car as) labels)))
136       (setq assqs-button-alists (cons (cons m-alist (nreverse labels))
137                                        assqs-button-alists)))
138     (setq assqs-button-alists (nreverse assqs-button-alists))
139     ;; displaying results
140     (with-current-buffer (get-buffer-create "*TeX tool bar buttons*")
141       (erase-buffer)
142       (insert "Available buttons for TeX mode
143 ================================")
144       (dolist (i assqs-button-alists)
145         (insert (format "\n\n`%s' provides the following buttons:\n  " (car i)))
146         (dolist (j (cdr i))
147           (insert (format " %s" j)))
148         (fill-region (point-at-bol) (point-at-eol))))
149     (display-buffer "*TeX tool bar buttons*" t)))
150
151 (defgroup TeX-tool-bar-button-definitions nil
152   "Collections of button definitions."
153   :group 'TeX-tool-bar)
154
155 (defcustom TeX-bar-TeX-all-button-alists
156   '(TeX-bar-TeX-button-alist
157     toolbarx-default-toolbar-meaning-alist)
158   "List of variables that hold buttons properties.
159 Each element should be a symbol bound to list in the format of
160 the argument BUTTON-ALIST in function `toolbarx-install-toolbar'."
161   :type '(repeat variable)
162   :group 'TeX-tool-bar-button-definitions)
163
164 (defcustom TeX-bar-TeX-button-alist
165   '((tex :image (lambda nil (if TeX-PDF-mode "pdftex" "tex"))
166          :command (progn
167                     (TeX-save-document (TeX-master-file))
168                     (TeX-command "TeX" 'TeX-master-file -1))
169          :help (lambda (&rest ignored)
170                  (TeX-bar-help-from-command-list "TeX")))
171     (pdftex :image "pdftex"
172             :command (progn
173                        (TeX-save-document (TeX-master-file))
174                        (TeX-command "PDFTeX" 'TeX-master-file -1))
175             :help (lambda (&rest ignored)
176                     (TeX-bar-help-from-command-list "PDFTeX")))
177     (next-error :image "error"
178                 :command TeX-next-error
179                 :enable (plist-get TeX-error-report-switches
180                                    (intern (TeX-master-file)))
181                 :visible (plist-get TeX-error-report-switches
182                                     (intern (TeX-master-file))))
183     (view :image (lambda nil (if TeX-PDF-mode "viewpdf" "viewdvi"))
184           :command (TeX-command "View" 'TeX-master-file -1)
185           :help (lambda (&rest ignored)
186                   (TeX-bar-help-from-command-list "View")))
187     (file :image "dvips"
188           :command (TeX-command "File" 'TeX-master-file -1)
189           :visible (not TeX-PDF-mode)
190           :help (lambda (&rest ignored)
191                   (TeX-bar-help-from-command-list "File")))
192     (bibtex :image "bibtex"
193             :command (TeX-command "BibTeX" 'TeX-master-file -1)
194             :help (lambda (&rest ignored)
195                     (TeX-bar-help-from-command-list "BibTeX")))
196     (clean  :image "delete"
197             :command (TeX-command "Clean" 'TeX-master-file -1)
198             :help (lambda (&rest ignored)
199                     (TeX-bar-help-from-command-list "Clean")))
200     (spell  :image "spell"
201             :command (TeX-command "Spell" 'TeX-master-file -1)
202             :help (lambda (&rest ignored)
203                     (TeX-bar-help-from-command-list "Spell"))))
204   ;; latex-symbols-experimental?
205   "Alist for button definitions in TeX bar.
206 Value should le a list where each element is of format (KEY .
207 PROPS), where KEY is a symbol that labels the button and PROPS is
208 a list of properties of the button.  For a description of the
209 format of PROPS, please see documentation of function
210 `toolbarx-install-toolbar'.  This custom variable is in the same
211 format of the argument MEANING-ALIST in the mentioned function."
212   :type '(alist :key-type symbol :value-type sexp)
213   :group 'TeX-tool-bar-button-definitions)
214
215 ;;; Installation of the tool bar
216 ;;;###autoload
217 (defun TeX-install-toolbar ()
218   "Install toolbar buttons for TeX mode."
219   (interactive)
220   (require 'toolbar-x)
221   (add-to-list 'toolbarx-image-path
222                (expand-file-name "images" TeX-data-directory))
223   (add-hook 'TeX-PDF-mode-hook 'toolbarx-refresh nil t)
224   (toolbarx-install-toolbar TeX-bar-TeX-buttons
225                             (let ((append-list))
226                               (dolist (elt TeX-bar-TeX-all-button-alists)
227                                 (setq append-list (append append-list
228                                                           (eval elt))))
229                               append-list)))
230
231 (defcustom TeX-bar-LaTeX-buttons
232   '(new-file open-file dired kill-buffer save-buffer cut copy paste undo
233               [separator nil] latex next-error view bibtex spell)
234   "List of buttons available in `latex-mode'.
235 It should be a list in the same format of the BUTTONS parameter
236 in function `toolbarx-install-toolbar', often a symbol that
237 labels a button or Emacs/XEmacs choice of buttons.
238
239 Type `\\[TeX-bar-LaTeX-buttons]' for a list of available buttons.
240
241 Buttons are defined in alists (labels associated to properties
242 that define a button).  For a list of variables that hold such
243 alists, see variable `TeX-bar-LaTeX-all-button-alists'."
244   :type '(list (set :inline t
245                     (const new-file)
246                     (const open-file)
247                     (const dired)
248                     (const kill-buffer)
249                     (const save-buffer)
250                     (const write-file)
251                     (const undo)
252                     (const cut)
253                     (const copy)
254                     (const paste)
255                     (const search-forward)
256                     (const print-buffer)
257                     (const [separator nil])
258                     (const latex)
259                     (const next-error)
260                     (const view)
261                     (const file)
262                     (const bibtex)
263                     (const clean)
264                     (const spell)
265                     (const latex-symbols-experimental))
266                (repeat (choice (symbol :tag "Label")
267                                (vector :args ((symbol :tag "Label in Emacs ")
268                                               (symbol :tag "Label in XEmacs"))
269                                        :tag "Emacs/XEmacs choice")
270                                (sexp :tag "General element"))))
271   :group 'TeX-tool-bar)
272
273 (defun TeX-bar-LaTeX-buttons ()
274   "Display in a buffer a list of buttons for `tex-bar.el'."
275   (interactive)
276   (let ((assqs-button-alists)
277         (labels))
278     (dolist (m-alist TeX-bar-LaTeX-all-button-alists)
279       (setq labels nil)
280       (dolist (as (eval m-alist))
281         (setq labels (cons (car as) labels)))
282       (setq assqs-button-alists (cons (cons m-alist (nreverse labels))
283                                        assqs-button-alists)))
284     (setq assqs-button-alists (nreverse assqs-button-alists))
285     ;; displaying results
286     (with-current-buffer (get-buffer-create "*TeX tool bar buttons*")
287       (erase-buffer)
288       (insert "Available buttons for LaTeX mode
289 ================================")
290       (dolist (i assqs-button-alists)
291         (insert (format "\n\n`%s' provides the following buttons:\n  " (car i)))
292         (dolist (j (cdr i))
293           (insert (format " %s" j)))
294         (fill-region (point-at-bol) (point-at-eol))))
295     (display-buffer "*TeX tool bar buttons*" t)))
296
297 (defgroup TeX-tool-bar-button-definitions nil
298   "Collections of button definitions."
299   :group 'TeX-tool-bar)
300
301 (defcustom TeX-bar-LaTeX-all-button-alists
302   '(TeX-bar-LaTeX-button-alist
303     toolbarx-default-toolbar-meaning-alist)
304   "List of variables that hold buttons properties.
305 Each element should be a symbol bound to list in the format of
306 the argument BUTTON-ALIST in function `toolbarx-install-toolbar'."
307   :type '(repeat variable)
308   :group 'TeX-tool-bar-button-definitions)
309
310 (defcustom TeX-bar-LaTeX-button-alist
311   '((latex :image (lambda nil (if TeX-PDF-mode "pdftex" "tex"))
312            :command (progn
313                       (TeX-save-document (TeX-master-file))
314                       (TeX-command "LaTeX" 'TeX-master-file -1))
315            :help (lambda (&rest ignored)
316                    (TeX-bar-help-from-command-list "LaTeX")))
317     (pdflatex :image "pdftex"
318               :command (progn
319                          (TeX-save-document (TeX-master-file))
320                          (TeX-command "PDFLaTeX" 'TeX-master-file -1))
321               :help (lambda (&rest ignored)
322                       (TeX-bar-help-from-command-list "PDFLaTeX")))
323     (next-error :image "error"
324                 :command TeX-next-error
325                 :enable (plist-get TeX-error-report-switches
326                                    (intern (TeX-master-file)))
327                 :visible (plist-get TeX-error-report-switches
328                                     (intern (TeX-master-file))))
329     (view :image (lambda nil (if TeX-PDF-mode "viewpdf" "viewdvi"))
330           :command (TeX-command "View" 'TeX-master-file -1)
331           :help (lambda (&rest ignored)
332                   (TeX-bar-help-from-command-list "View")))
333     (file :image "dvips"
334           :command (TeX-command "File" 'TeX-master-file -1)
335           :visible (not TeX-PDF-mode)
336           :help (lambda (&rest ignored)
337                   (TeX-bar-help-from-command-list "File")))
338     (bibtex :image "bibtex"
339             :command (TeX-command (if LaTeX-using-Biber "Biber" "BibTeX")
340                                   'TeX-master-file -1)
341             :help (lambda (&rest ignored)
342                     (TeX-bar-help-from-command-list
343                      (if LaTeX-using-Biber "Biber" "BibTeX"))))
344     (clean  :image "delete"
345             :command (TeX-command "Clean" 'TeX-master-file -1)
346             :help (lambda (&rest ignored)
347                     (TeX-bar-help-from-command-list "Clean")))
348     (spell  :image "spell"
349             :command (TeX-command "Spell" 'TeX-master-file -1)
350             :help (lambda (&rest ignored)
351                     (TeX-bar-help-from-command-list "Spell")))
352     (latex-symbols-experimental . (:alias :eval-group
353                                           LaTeX-symbols-toolbar-switch-contents
354                                           LaTeX-symbols-toolbar-contents)))
355   "Alist for button definitions in TeX bar.
356 Value should le a list where each element is of format (KEY .
357 PROPS), where KEY is a symbol that labels the button and PROPS is
358 a list of properties of the button.  For a description of the
359 format of PROPS, please see documentation of function
360 `toolbarx-install-toolbar'.  This custom variable is in the same
361 format of the argument MEANING-ALIST in the mentioned function."
362   :type '(alist :key-type symbol :value-type sexp)
363   :group 'TeX-tool-bar-button-definitions)
364
365 ;;; Installation of the tool bar
366 ;;;###autoload
367 (defun LaTeX-install-toolbar ()
368   "Install toolbar buttons for LaTeX mode."
369   (interactive)
370   (require 'toolbar-x)
371   (add-to-list 'toolbarx-image-path
372                (expand-file-name "images" TeX-data-directory))
373   (add-hook 'TeX-PDF-mode-hook 'toolbarx-refresh nil t)
374   ;; Refresh the toolbar after styles update because `LaTeX-using-Biber' value
375   ;; could have been changed.  Append the refresh to the hook so it is run after
376   ;; the other styles-related changes.
377   (add-hook 'TeX-update-style-hook 'toolbarx-refresh t t)
378   (toolbarx-install-toolbar TeX-bar-LaTeX-buttons
379                             (let ((append-list))
380                               (dolist (elt TeX-bar-LaTeX-all-button-alists)
381                                 (setq append-list (append append-list
382                                                           (eval elt))))
383                               append-list)))
384
385 ;;; Experimental Symbol Toolbar
386
387 ;;; symbol toolbar
388 (defun TeX-bar-img-filename (tex-command)
389   "Return the filename (no extension) for the image button of TEX-COMMAND."
390   (let ((str-list (append tex-command nil))
391         (str-result))
392     (dolist (i str-list)
393       (cond
394        ;; capital letter -> letter + "-"
395        ((and (>= i ?A) (<= i ?Z))
396         (setq str-result (cons ?- (cons i str-result))))
397        ;; lowercase letter -> letter
398        ((and (>= i ?a) (<= i ?z))
399         (setq str-result (cons i str-result)))
400        ;; open curly brackets `{' -> "ocb--"
401        ((eq i ?{)
402         (setq str-result (cons ?o str-result))
403         (setq str-result (cons ?c str-result))
404         (setq str-result (cons ?b str-result))
405         (setq str-result (cons ?- str-result))
406         (setq str-result (cons ?- str-result)))
407        ;; close curly brackets `}' -> "ccb--"
408        ((eq i ?})
409         (setq str-result (cons ?c str-result))
410         (setq str-result (cons ?c str-result))
411         (setq str-result (cons ?b str-result))
412         (setq str-result (cons ?- str-result))
413         (setq str-result (cons ?- str-result)))
414        ;; vertical bar `|' -> "v--"
415        ((eq i ?|)
416         (setq str-result (cons ?v str-result))
417         (setq str-result (cons ?- str-result))
418         (setq str-result (cons ?- str-result)))
419        ;; slash `/' -> "s--"
420        ((eq i ?/)
421         (setq str-result (cons ?s str-result))
422         (setq str-result (cons ?- str-result))
423         (setq str-result (cons ?- str-result)))))
424     (concat (nreverse str-result))))
425
426 (let* ((menu-strings-buttons-alist
427         ;; make a alist os strings with the symbol classes and store it in
428         ;; `menu-strings-alist'
429         (let* ((menu-strings-alist-temp))
430           (dolist (item-external (cdr LaTeX-math-menu)
431                                  (nreverse menu-strings-alist-temp))
432             (when (listp item-external)
433               ;; if first element is vector, I am supposing that all are
434               ;; vectors as well
435               (if (vectorp (cadr item-external))
436                   (let* ((menu-str (car item-external))
437                          (menu-buttons))
438                     (dolist (button (cdr item-external))
439                       (setq menu-buttons
440                             (cons (list (intern (TeX-bar-img-filename
441                                                  (aref button 0)))
442                                         :image
443                                         (concat "symb-pics/"
444                                                 (TeX-bar-img-filename
445                                                  (aref button 0)))
446                                         :help (aref button 0)
447                                         :command (aref button 1))
448                                   menu-buttons)))
449                     (setq menu-buttons (nreverse menu-buttons))
450                     (setq menu-strings-alist-temp
451                           (cons (cons menu-str (list menu-buttons))
452                                 menu-strings-alist-temp)))
453                 ;; if another list (therefore, up to second level menu)
454                 (let ((parent-str (concat (car item-external) " ")))
455                   (dolist (item-internal (cdr item-external))
456                     (unless (equal (car item-internal) "Special")
457                       (let* ((menu-str (concat parent-str
458                                                (car item-internal)))
459                              (menu-buttons))
460                         (dolist (button (cdr item-internal))
461                           (setq menu-buttons
462                                 (cons (list (intern (aref button 0))
463                                             :image
464                                             (concat "symb-pics/"
465                                                     (TeX-bar-img-filename
466                                                      (aref button 0)))
467                                             :help (aref button 0)
468                                             :command (aref button 1))
469                                       menu-buttons)))
470                         (setq menu-buttons (nreverse menu-buttons))
471                         (setq menu-strings-alist-temp
472                               (cons (cons menu-str (list menu-buttons))
473                                     menu-strings-alist-temp)))))))))))
474        (list-strings (let* ((list-str-temp))
475                        (dolist (i menu-strings-buttons-alist
476                                   (nreverse list-str-temp))
477                          (setq list-str-temp (cons (car i)
478                                                    list-str-temp))))))
479   (defvar LaTeX-symbols-toolbar-visible-flag nil
480     "Non-nil means that the LaTeX symbols on toolbar are visible.
481 Internal variable.")
482   (defconst LaTeX-symbols-toolbar-switch-contents
483     `(;; the on-off switch button
484       (latex-symbols-switch
485        :image (lambda nil (if LaTeX-symbols-toolbar-visible-flag
486                               "ltx-symb-turn-off"
487                             "ltx-symb-turn-on"))
488        :command (progn
489                   (setq LaTeX-symbols-toolbar-visible-flag
490                         (not LaTeX-symbols-toolbar-visible-flag))
491                   (toolbarx-refresh))
492        ;; help message depends on if symb-toolbar is on or off, and in
493        ;; the name of the current class of symbols
494        :help (lambda (&rest ignore)
495                (concat "Turn "
496                        (if LaTeX-symbols-toolbar-visible-flag "off " "on ")
497                        "the toolbar of LaTeX symbols (current class: "
498                        (nth (1- LaTeX-symbols-active-menuitem)
499                             (quote ,list-strings))
500                        ")")))
501       ;; the dropdown button, that also switch on the symbols
502       ,(append '(:dropdown-group)
503                list-strings
504                '(:variable
505                  LaTeX-symbols-active-menuitem
506                  :save offer
507                  :dropdown-prepend-command
508                  (setq LaTeX-symbols-toolbar-visible-flag t)
509                  :dropdown-help "Select a class of symbols to be displayed"))))
510   (defconst LaTeX-symbols-toolbar-contents
511     (let* ((ltx-symb)
512            (count 0))
513       (dolist (i menu-strings-buttons-alist
514                  (append (nreverse ltx-symb)
515                          '(:insert
516                            LaTeX-symbols-toolbar-visible-flag
517                            :toolbar (bottom . top))))
518         (setq count (1+ count))
519         (setq ltx-symb
520               (cons (append (cdr i)
521                             `(:insert (eq LaTeX-symbols-active-menuitem
522                                           ,count)))
523                     ltx-symb))))))
524
525 (provide 'tex-bar)
526
527 ;;; tex-bar.el ends here