Initial Commit
[packages] / xemacs-packages / x-symbol / lisp / x-symbol-emacs.el
1 ;;; x-symbol-emacs.el --- make package x-symbol work with Emacs
2
3 ;; Copyright (C) 2000-2002 Free Software Foundation, Inc.
4 ;;
5 ;; Authors: Stefan Monnier, Christoph Wedler
6 ;; Maintainer: (Please use `M-x x-symbol-package-bug' to contact the maintainer)
7 ;; Version: 4.5.X
8 ;; Keywords: WYSIWYG, LaTeX, HTML, wp, math, internationalization
9 ;; X-URL: http://x-symbol.sourceforge.net/
10
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)
14 ;; any later version.
15 ;;
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.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; if not, write to the Free Software
23 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; If you want to use package x-symbol, please visit the URL (use
28 ;; \\[x-symbol-package-web]) and read the info (use \\[x-symbol-package-info]).
29
30 (provide 'x-symbol-emacs)
31 ;; TODO: Emacs has `buffer-substring-no-properties'
32
33 (unless (fboundp 'emacs-version>=)
34 (defun emacs-version>= (major &optional minor patch)
35   "Return true if the Emacs version is >= to the given version.
36 The version is provided by the required argument MAJOR, and the optional
37 arguments MINOR and PATCH.  Only the non-nil arguments are used in the
38 test."
39   (cond ((> emacs-major-version major))
40         ((< emacs-major-version major) nil)
41         ((null minor))
42         ((> emacs-minor-version minor))
43         ((< emacs-minor-version minor) nil)
44         ((null patch))
45         ((string-match "^[0-9]+\\.[0-9]+\\.\\([0-9]+\\)" emacs-version)
46          (>= (string-to-int (match-string 1 emacs-version)) patch)))))
47
48
49 ;;;===========================================================================
50 ;;;  Emacs-21.4 features
51 ;;;===========================================================================
52 ;; To make use of newer Emacs features with the current stable version of
53 ;; Emacs, you can download individual files from the CVS repository of
54 ;; <http://savannah.gnu.org/projects/emacs/>.  E.g., I use Emacs-21.1.5
55 ;; together with updates of the following files:
56
57 ;; lisp/font-lock.el,v 1.200 (+ exchange all cc-mode font-lock keywords,
58 ;; recursive backquote not possible in Emacs-21.1), lisp/font-core.el,v
59 ;; 1.8(new), lisp/emacs-lisp/syntax.el,v 1.5(new):
60 ;;   (defvar char-property-alias-alist nil)
61 ;;   (unless (fboundp 'copy-tree) (defalias 'copy-tree 'copy-alist))
62 ;;   (unless (fboundp 'remove-list-of-text-properties)
63 ;;     (defun remove-list-of-text-properties (start end proplist)
64 ;;       (let (props)
65 ;;         (while proplist (push nil props) (push (pop proplist) props))
66 ;;         (remove-text-properties start end props))))
67 ;;   (or (featurep 'font-core) (load "font-core" t))
68 (defvar x-symbol-emacs-has-font-lock-with-props
69   (and (boundp 'font-lock-extra-managed-props)
70        (boundp 'char-property-alias-alist)
71        (fboundp 'copy-tree)
72        (locate-library "font-core")
73        (locate-library "syntax")))
74 ;; with the following line, "reveal invisible around point" won't work:
75 ;;   (setq x-symbol-emacs-has-font-lock-with-props 'invisible)
76
77 ;; lisp/warnings.el,v 1.6(new): recommended
78 (condition-case nil (require 'warnings) (error))
79
80 ;; src/fileio.c,v 1.447:
81 (defvar x-symbol-emacs-has-correct-find-safe-coding
82   (not (and (boundp 'char-coding-system-table)
83             (arrayp char-coding-system-table))))
84
85 ;; lisp/format.el,v 1.39 (with src/fileio.c,v 1.447):
86 ;;   (setq x-symbol-auto-conversion-method 'format)
87
88 ;; lisp/isearch.el,v 1.213 (makes isearch+GRID work):
89 ;;   (load "isearch")  ; only nec/ when updated after Emacs built
90
91 ;; lisp/language/european.el,v 1.76 (makes X-Symbol+ispell work)
92 ;;   (load "european")  ; only nec/ when updated after Emacs built
93
94 ;;; Bugs:
95
96 (defvar x-symbol-emacs-after-create-image-function
97 ;; I want to know why `clear-image-cache' is necessary sometimes,
98 ;; 'redraw-display is not enough
99   (if (emacs-version>= 21 3 50) nil 'clear-image-cache))
100
101 ;;; Todo:
102
103 ;; - Invisible text support.
104 ;; - Use Emacs-21's `display' text-property to make sub/super-scripts.
105 ;; - many more, I'm sure.
106
107 ;; Changes I (CW) would like to see in Emacs:
108 ;;  * `directory-files': 5th arg FILES-ONLY like in XEmacs
109 ;;  * `file-remote-p' like in XEmacs (defined below)
110 ;;  * tooltip and menu cannot use text props, Emacs display strings with prop
111 ;;    as "("STRING" 0 4 nil...) instead, which is not useful
112
113 ;; Restrictions, Questions (or other changes I would like to see):
114 ;;  * how to use temp image files for images?
115
116 ;;; Code:
117
118 (require 'cl)
119 (require 'fontset)                      ;seems not to be loaded in batch mode
120
121 ;; temp hack:
122 (defvar image-types nil)                ; necessary in Emacs-21.2 w/o image
123                                         ; support, still in Emacs-21.3?
124 (if (memq 'png image-types) (provide 'png))
125 (if (memq 'gif image-types) (provide 'gif))
126
127 ;; No way to test patch level?
128 (cond ((not (and (boundp 'emacs-major-version)
129                  (>= emacs-major-version 21)
130                  (or (> emacs-major-version 21) (>= emacs-minor-version 1))))
131        (error "Package X-Symbol can only be used with Emacs-21.1+")))
132
133 ;;(require 'x-symbol-hooks)
134
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;;;   Emacs-20 specific       ;;;
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138
139 ;;(unless (fboundp 'puthash) (defalias 'puthash 'cl-puthash))
140 ;;(unless (fboundp 'plist-member) (defalias 'plist-member 'widget-plist-member))
141 ;;(unless (fboundp 'point-at-bol) (defalias 'point-at-bol 'line-end-position))
142 ;;(unless (fboundp 'point-at-eol) (defalias 'point-at-eol 'line-beginning-position))
143 ;;(unless (fboundp 'add-minor-mode)
144 ;;  (defun add-minor-mode (tog name map)
145 ;;    (when name (add-to-list 'minor-mode-alist (list tog name)))
146 ;;    (when map (add-to-list 'minor-mode-map-alist (cons tog map)))))
147
148 (unless (and (boundp 'emacs-major-version) (>= emacs-major-version 21))
149   (error "X-Symbol requires Emacs-21 or higher"))
150
151
152 ;;(define-key isearch-mode-map [?\C-=] nil)
153
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;;;   Problematic XEmacs compatibility  ;;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157
158 (defvar init-file-loaded t)
159
160 ;; Used in x-symbol-show-info-and-invisible
161 (defvar message-stack nil)              ;message-stack ~= (current-message)
162 (unless (fboundp 'warn) (defalias 'warn 'message))
163 (unless (fboundp 'lwarn)
164   (defun lwarn (class level msg &rest args)
165     (warn (format "(%s/%s) %s" class level msg) args)))
166 (unless (fboundp 'display-message)
167   (defun display-message (label msg)
168     (if (memq label '(help-echo command progress prompt no-log
169                                 garbage-collecting auto-saving))
170         ;; XEmacs `log-message-ignore-labels'
171         (let ((message-log-max nil))    ;#dynamic
172           (message "%s" msg))
173       (message "%s" msg))))
174
175 (unless (fboundp 'face-font-instance)
176   (defalias 'face-font-instance 'face-font))
177 (unless (fboundp 'try-font-name)
178   (defun try-font-name (name)
179     (and name
180          ;; TODO: shouldn't `x-list-fonts' work when not running on X?
181          (condition-case nil (x-list-fonts name) (error nil))
182          name)))
183 (unless (fboundp 'put-nonduplicable-text-property)
184   (defun put-nonduplicable-text-property (start end prop val &optional obj)
185     (let ((inhibit-modification-hooks t)
186           (modified (buffer-modified-p)))
187       (unwind-protect
188           (put-text-property start end prop val obj)
189         (set-buffer-modified-p modified)))))
190
191 ;;; list-mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 (unless (fboundp 'list-mode)
193   (define-derived-mode list-mode completion-list-mode "List"
194     "Major mode for buffer containing lists of items."
195     (setq buffer-read-only t)))
196 (unless (fboundp 'add-list-mode-item)
197   (defun add-list-mode-item (start end &optional buffer activate-callback
198                                    ;; activate-callback is ignored
199                                    user-data)
200     ;; BEWARE!!! (list 'highlight) here is tricky.  It makes sure that each
201     ;; item has a distinct mouse-face text-property value, so that they
202     ;; won't be aggregated arbitrarily (aggregation uses `eq' rather
203     ;; than `equal').
204     (put-text-property start end 'mouse-face (list 'highlight) buffer)
205     (let ((ol (make-overlay start end buffer)))
206       (overlay-put ol 'list-mode-item t)
207       (if user-data (overlay-put ol 'list-mode-item-user-data user-data))
208       ol)))
209
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;;;   Plain XEmacs compatibility        ;;;
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213
214 ;;; Extents ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215
216 (require 'lucid)
217 (unless (fboundp 'extent-property) (defalias 'extent-property 'overlay-get))
218 (unless (fboundp 'map-extents) (defalias 'map-extents 'cl-map-overlays))
219 (unless (fboundp 'set-extent-end-glyph)
220   (defun set-extent-end-glyph (extent glyph)
221     (overlay-put extent 'after-string glyph)))
222 (unless (fboundp 'insert-face)
223   (defun insert-face (string face)
224     (let ((ol (make-overlay (point) (progn (insert string) (point)))))
225       (overlay-put ol 'face face) ol)))
226
227 ;;; Chars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228
229 (unless (fboundp 'characterp) (defalias 'characterp 'integerp))
230 (unless (fboundp 'int-to-char) (defalias 'int-to-char 'identity))
231 (unless (fboundp 'char-to-int) (defalias 'char-to-int 'identity))
232
233 ;;; Char tables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234
235 ;; TODO: use 'x-symbol instead 'generic with Emacs
236 (put 'generic 'char-table-extra-slots 0)
237 (unless (fboundp 'put-char-table)
238   (defun put-char-table (range val tab) (set-char-table-range tab range val)))
239 (unless (fboundp 'get-char-table)
240   (defun get-char-table (ch table) (char-table-range table ch)))
241
242 ;;; Charsets ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243
244 (defun region-active-p () (and transient-mark-mode mark-active))
245 (unless (fboundp 'make-charset)
246   (defun make-charset (name docstring info)
247     (let ((registry (plist-get info 'registry))
248           (graphic (plist-get info 'graphic))
249           (dimension (plist-get info 'dimension))
250           (ccl-program (plist-get info 'ccl-program)))
251       (define-charset nil name
252         (vector dimension
253                 (plist-get info 'chars)
254                 (or (plist-get info 'columns) dimension)
255                 (if (eq (plist-get info 'direction) 'l2r) 0 1)
256                 (plist-get info 'final)
257                 graphic
258                 (symbol-name name)
259                 (symbol-name name)
260                 docstring))
261       ;; Don't ask for X-Symbol's private charsets (with Emacs-21.0.104+).  But
262       ;; you're still asked for latin-{2,3,5,9} chars even if they are encoded
263       ;; by X-Symbol (changes in Emacs-21.4: first do annotate functions, then
264       ;; determine safe coding).
265       (or x-symbol-emacs-has-correct-find-safe-coding
266           (aset char-coding-system-table (make-char name) t))
267       (when registry
268         (set-fontset-font "fontset-default" name (cons "*" registry))
269         (when (eq graphic 0) (set-font-encoding registry name 0))
270         (when ccl-program
271           (add-to-list 'font-ccl-encoder-alist (cons registry ccl-program))))
272       name)))
273
274 (unless (fboundp 'find-charset)
275   (defun find-charset (c) (and (charsetp c) c)))
276
277 ;;; Misc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278
279 (autoload 'Info-goto-node "info")
280
281 (defvar x-symbol-data-directory (expand-file-name "x-symbol" data-directory))
282
283 (unless (fboundp 'signal-error) (defalias 'signal-error 'signal))
284
285 (unless (fboundp 'console-type)
286   (defun console-type ()
287     (case window-system
288       (x 'x)
289       (w32 'mswindows)
290       (nil 'tty)
291       (t window-system))))
292
293 (unless (fboundp 'symbol-value-in-buffer)
294   (defun symbol-value-in-buffer (sym buf &optional def)
295     (with-current-buffer buf (if (boundp sym) (symbol-value sym) def))))
296
297 (unless (fboundp 'make-key-weak-hashtable)
298   (defun make-key-weak-hashtable (size &optional test)
299     ;; The :weakness is (hopefully) ignored on Emacs-20
300     (make-hash-table :size size :test test :weakness 'key)))
301
302 (unless (fboundp 'set-keymap-default-binding)
303   (defun set-keymap-default-binding (map cmd)
304     (define-key-after map [t] cmd t)))
305
306 (unless (fboundp 'destructive-alist-to-plist)
307   (defun destructive-alist-to-plist (alist)
308     (let ((next alist) tmp head)
309       (while next
310         (setq head (car next) tmp next)
311         (if (not (consp head)) (pop next)
312           (setcar next (car head))
313           (setcar head (cdr head))
314           (setcdr head (setq next (cdr next)))
315           (setcdr tmp head))))
316     alist))
317
318 (unless (fboundp 'plists-eq)
319   ;; All kinds of potential problems here with "shadowed properties".
320   (defun plists-eq (a b)
321     (when (= (length a) (length b))
322       (let (tmp)
323         (while (and a
324                     (setq tmp (plist-member b (pop a)))
325                     (eq (car a) (cadr tmp)))
326           (setq a (cdr a)))
327         (null a)))))
328
329 ;; From XEmacs
330 (unless (fboundp 'sorted-key-descriptions)
331   (defun sorted-key-descriptions (keys &optional separator)
332     "Sort and separate the key descriptions for KEYS.
333 The sorting is done by length (shortest bindings first), and the bindings
334 are separated with SEPARATOR (\", \" by default)."
335     (mapconcat 'key-description
336                (sort keys #'(lambda (x y)
337                               (< (length x) (length y))))
338                (or separator ", "))))
339
340 ;; defined in Emacs-21.3.50
341 (unless (fboundp 'file-remote-p)
342   (defun file-remote-p (file-name)
343     "Test whether FILE-NAME is looked for on a remote system."
344     (cond ((featurep 'ange-ftp)
345            (if (fboundp 'ange-ftp-ftp-name)
346                (ange-ftp-ftp-name file-name)
347              (ange-ftp-ftp-path file-name)))
348           ((fboundp 'efs-ftp-path) (efs-ftp-path file-name))
349           (t nil))))
350
351 (defun x-symbol-directory-files (dirname &optional full match nosort
352                                          files-only)
353   (let ((files (directory-files dirname full match nosort))
354         result)
355     (if (null files-only)
356         files
357       (while files
358         (if (if (file-directory-p (car files))
359                 (null (eq files-only t))
360               (eq files-only t))
361             (push (car files) result))
362         (setq files (cdr files)))
363       (nreverse result))))
364
365 (defun x-symbol-event-in-current-buffer ()
366   t)
367
368 (defun x-symbol-create-image (file type)
369   (create-image file type nil :ascent 80))
370
371 (defun x-symbol-make-glyph (image)
372   (propertize "x" 'display image))
373
374 (defun x-symbol-set-glyph-image (glyph image)
375   (set-text-properties 0 (length glyph) (list 'display image) glyph))
376
377 (unless (fboundp 'event-closest-point)
378   (defun event-closest-point (event) (posn-point (event-end event))))
379
380 (unless (fboundp 'event-buffer)
381   (defun event-buffer (event) (window-buffer (posn-window (event-end event)))))
382
383 ;;;
384 ;;; X-Symbol functions
385 ;;;
386
387 ;; Defvarred to prevent their initialization code to run (since that
388 ;; code uses XEmacs'isms that haven't been ported yet).
389 (defvar x-symbol-heading-strut-glyph " ")
390
391 ;; Define face's vars for Emacs' font-lock.
392 (defvar x-symbol-invisible-face 'x-symbol-invisible-face)
393 (defvar x-symbol-face 'x-symbol-face)
394 (defvar x-symbol-sub-face 'x-symbol-sub-face)
395 (defvar x-symbol-sup-face 'x-symbol-sup-face)
396
397 (defvar x-symbol-emacs-w32-font-directories
398   (mapcar (lambda (dir) (expand-file-name dir x-symbol-data-directory))
399           '("fonts/" "origfonts/" "genfonts/")))
400
401 (if (and (eq window-system 'w32)
402          x-symbol-emacs-w32-font-directories
403          (fboundp 'w32-find-bdf-fonts)
404          (boundp 'w32-bdf-filename-alist))
405     (setq w32-bdf-filename-alist
406           (nconc w32-bdf-filename-alist
407                  (w32-find-bdf-fonts x-symbol-emacs-w32-font-directories))))
408
409 ;; Invisibility cannot be done this way in Emacs.
410 (defvar x-symbol-invisible-display-table nil)
411
412 (defalias 'x-symbol-window-width 'window-width)
413
414 (defun x-symbol-set-face-font (face font charsets default)
415   (let ((fontset (concat "fontset-" (symbol-name face))))
416     (unless (query-fontset fontset)
417       ;; We assume that the first time around we're using latin-8859-1
418       (new-fontset fontset
419                    (x-complement-fontset-spec (make-vector 14 "*")
420                                               (list (cons 'ascii font)))))
421     (dolist (charset charsets)
422       (when charset (set-fontset-font fontset charset font)))
423     (set-face-font face fontset)))
424
425 (defun x-symbol-event-matches-key-specifier-p (event specifier)
426   (if (consp specifier) (setq specifier (event-convert-list specifier)))
427   (if (consp event) (setq event (car event)))
428   (eq event specifier))
429
430 ;;;
431 ;;; Possibly useful compat functions
432 ;;;
433
434 ;;; from Sang-Min
435 (defun start-itimer (name func value restart isidle)
436   (run-with-idle-timer value restart func))
437 (defun itimer-live-p (obj) (and obj (not (aref obj 0))))
438
439 ;;; x-symbol-emacs.el ends here