aboutsummaryrefslogtreecommitdiffhomepage
path: root/x-symbol/lisp/x-symbol-emacs.el
blob: 382fc26371babbd59558438b52cfc1f1d5b8d1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
;;; x-symbol-emacs.el --- make package x-symbol work with Emacs

;; Copyright (C) 2000-2002 Free Software Foundation, Inc.
;;
;; Authors: Stefan Monnier, Christoph Wedler
;; Maintainer: (Please use `M-x x-symbol-package-bug' to contact the maintainer)
;; Version: 4.5.X
;; Keywords: WYSIWYG, LaTeX, HTML, wp, math, internationalization
;; X-URL: http://x-symbol.sourceforge.net/

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; If you want to use package x-symbol, please visit the URL (use
;; \\[x-symbol-package-web]) and read the info (use \\[x-symbol-package-info]).

(provide 'x-symbol-emacs)
;; TODO: Emacs has `buffer-substring-no-properties'

(unless (fboundp 'emacs-version>=)
(defun emacs-version>= (major &optional minor patch)
  "Return true if the Emacs version is >= to the given version.
The version is provided by the required argument MAJOR, and the optional
arguments MINOR and PATCH.  Only the non-nil arguments are used in the
test."
  (cond ((> emacs-major-version major))
	((< emacs-major-version major) nil)
	((null minor))
	((> emacs-minor-version minor))
	((< emacs-minor-version minor) nil)
	((null patch))
	((string-match "^[0-9]+\\.[0-9]+\\.\\([0-9]+\\)" emacs-version)
	 (>= (string-to-number (match-string 1 emacs-version)) patch)))))


;;;===========================================================================
;;;  Emacs-21.4 features
;;;===========================================================================
;; To make use of newer Emacs features with the current stable version of
;; Emacs, you can download individual files from the CVS repository of
;; <http://savannah.gnu.org/projects/emacs/>.  E.g., I use Emacs-21.1.5
;; together with updates of the following files:

;; lisp/font-lock.el,v 1.200 (+ exchange all cc-mode font-lock keywords,
;; recursive backquote not possible in Emacs-21.1), lisp/font-core.el,v
;; 1.8(new), lisp/emacs-lisp/syntax.el,v 1.5(new):
;;   (defvar char-property-alias-alist nil)
;;   (unless (fboundp 'copy-tree) (defalias 'copy-tree 'copy-alist))
;;   (unless (fboundp 'remove-list-of-text-properties)
;;     (defun remove-list-of-text-properties (start end proplist)
;;       (let (props)
;;         (while proplist (push nil props) (push (pop proplist) props))
;;         (remove-text-properties start end props))))
;;   (or (featurep 'font-core) (load "font-core" t))
(defvar x-symbol-emacs-has-font-lock-with-props
  (and (boundp 'font-lock-extra-managed-props)
       (boundp 'char-property-alias-alist)
       (fboundp 'copy-tree)
       (locate-library "font-core")
       (locate-library "syntax")))
;; with the following line, "reveal invisible around point" won't work:
;;   (setq x-symbol-emacs-has-font-lock-with-props 'invisible)

;; lisp/warnings.el,v 1.6(new): recommended
(condition-case nil (require 'warnings) (error))

;; src/fileio.c,v 1.447:
(defvar x-symbol-emacs-has-correct-find-safe-coding
  (not (and (boundp 'char-coding-system-table)
	    (arrayp char-coding-system-table))))

;; lisp/format.el,v 1.39 (with src/fileio.c,v 1.447):
;;   (setq x-symbol-auto-conversion-method 'format)

;; lisp/isearch.el,v 1.213 (makes isearch+GRID work):
;;   (load "isearch")  ; only nec/ when updated after Emacs built

;; lisp/language/european.el,v 1.76 (makes X-Symbol+ispell work)
;;   (load "european")  ; only nec/ when updated after Emacs built

;;; Bugs:

(defvar x-symbol-emacs-after-create-image-function
;; I want to know why `clear-image-cache' is necessary sometimes,
;; 'redraw-display is not enough
  (if (emacs-version>= 21 3 50) nil 'clear-image-cache))

;;; Todo:

;; - Invisible text support.
;; - Use Emacs-21's `display' text-property to make sub/super-scripts.
;; - many more, I'm sure.

;; Changes I (CW) would like to see in Emacs:
;;  * `directory-files': 5th arg FILES-ONLY like in XEmacs
;;  * `file-remote-p' like in XEmacs (defined below)
;;  * tooltip and menu cannot use text props, Emacs display strings with prop
;;    as "("STRING" 0 4 nil...) instead, which is not useful

;; Restrictions, Questions (or other changes I would like to see):
;;  * how to use temp image files for images?

;;; Code:

(require 'cl)
(require 'fontset)			;seems not to be loaded in batch mode

;; temp hack:
(defvar image-types nil)		; necessary in Emacs-21.2 w/o image
					; support, still in Emacs-21.3?
(if (memq 'png image-types) (provide 'png))
(if (memq 'gif image-types) (provide 'gif))

;; No way to test patch level?
(cond ((not (and (boundp 'emacs-major-version)
		 (>= emacs-major-version 21)
		 (or (> emacs-major-version 21) (>= emacs-minor-version 1))))
       (error "Package X-Symbol can only be used with Emacs-21.1+")))

;;(require 'x-symbol-hooks)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   Emacs-20 specific       ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;(unless (fboundp 'puthash) (defalias 'puthash 'cl-puthash))
;;(unless (fboundp 'plist-member) (defalias 'plist-member 'widget-plist-member))
;;(unless (fboundp 'point-at-bol) (defalias 'point-at-bol 'line-end-position))
;;(unless (fboundp 'point-at-eol) (defalias 'point-at-eol 'line-beginning-position))
;;(unless (fboundp 'add-minor-mode)
;;  (defun add-minor-mode (tog name map)
;;    (when name (add-to-list 'minor-mode-alist (list tog name)))
;;    (when map (add-to-list 'minor-mode-map-alist (cons tog map)))))

(unless (and (boundp 'emacs-major-version) (>= emacs-major-version 21))
  (error "X-Symbol requires Emacs-21 or higher"))


;;(define-key isearch-mode-map [?\C-=] nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   Problematic XEmacs compatibility  ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar init-file-loaded t)

;; Used in x-symbol-show-info-and-invisible
(defvar message-stack nil)		;message-stack ~= (current-message)
(unless (fboundp 'warn) (defalias 'warn 'message))
(unless (fboundp 'lwarn)
  (defun lwarn (class level msg &rest args)
    (warn (format "(%s/%s) %s" class level msg) args)))
(unless (fboundp 'display-message)
  (defun display-message (label msg)
    (if (memq label '(help-echo command progress prompt no-log
				garbage-collecting auto-saving))
	;; XEmacs `log-message-ignore-labels'
	(let ((message-log-max nil))	;#dynamic
	  (message "%s" msg))
      (message "%s" msg))))

(unless (fboundp 'face-font-instance)
  (defalias 'face-font-instance 'face-font))
(unless (fboundp 'try-font-name)
  (defun try-font-name (name)
    (and name
	 ;; TODO: shouldn't `x-list-fonts' work when not running on X?
	 (condition-case nil (x-list-fonts name) (error nil))
	 name)))
(unless (fboundp 'put-nonduplicable-text-property)
  (defun put-nonduplicable-text-property (start end prop val &optional obj)
    (let ((inhibit-modification-hooks t)
	  (modified (buffer-modified-p)))
      (unwind-protect
	  (put-text-property start end prop val obj)
	(set-buffer-modified-p modified)))))

;;; list-mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(unless (fboundp 'list-mode)
  (define-derived-mode list-mode completion-list-mode "List"
    "Major mode for buffer containing lists of items."
    (setq buffer-read-only t)))
(unless (fboundp 'add-list-mode-item)
  (defun add-list-mode-item (start end &optional buffer activate-callback
				   ;; activate-callback is ignored
				   user-data)
    ;; BEWARE!!! (list 'highlight) here is tricky.  It makes sure that each
    ;; item has a distinct mouse-face text-property value, so that they
    ;; won't be aggregated arbitrarily (aggregation uses `eq' rather
    ;; than `equal').
    (put-text-property start end 'mouse-face (list 'highlight) buffer)
    (let ((ol (make-overlay start end buffer)))
      (overlay-put ol 'list-mode-item t)
      (if user-data (overlay-put ol 'list-mode-item-user-data user-data))
      ol)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   Plain XEmacs compatibility        ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Extents ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'lucid)
(unless (fboundp 'extent-property) (defalias 'extent-property 'overlay-get))
(unless (fboundp 'map-extents) (defalias 'map-extents 'cl-map-overlays))
(unless (fboundp 'set-extent-end-glyph)
  (defun set-extent-end-glyph (extent glyph)
    (overlay-put extent 'after-string glyph)))
(unless (fboundp 'insert-face)
  (defun insert-face (string face)
    (let ((ol (make-overlay (point) (progn (insert string) (point)))))
      (overlay-put ol 'face face) ol)))

;;; Chars ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(unless (fboundp 'characterp) (defalias 'characterp 'integerp))
(unless (fboundp 'int-to-char) (defalias 'int-to-char 'identity))
(unless (fboundp 'char-to-int) (defalias 'char-to-int 'identity))

;;; Char tables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; TODO: use 'x-symbol instead 'generic with Emacs
(put 'generic 'char-table-extra-slots 0)
(unless (fboundp 'put-char-table)
  (defun put-char-table (range val tab) (set-char-table-range tab range val)))
(unless (fboundp 'get-char-table)
  (defun get-char-table (ch table) (char-table-range table ch)))

;;; Charsets ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun region-active-p () (and transient-mark-mode mark-active))
(unless (fboundp 'make-charset)
  (defun make-charset (name docstring info)
    (let ((registry (plist-get info 'registry))
	  (graphic (plist-get info 'graphic))
	  (dimension (plist-get info 'dimension))
	  (ccl-program (plist-get info 'ccl-program)))
      (define-charset nil name
	(vector dimension
		(plist-get info 'chars)
		(or (plist-get info 'columns) dimension)
		(if (eq (plist-get info 'direction) 'l2r) 0 1)
		(plist-get info 'final)
		graphic
		(symbol-name name)
		(symbol-name name)
		docstring))
      ;; Don't ask for X-Symbol's private charsets (with Emacs-21.0.104+).  But
      ;; you're still asked for latin-{2,3,5,9} chars even if they are encoded
      ;; by X-Symbol (changes in Emacs-21.4: first do annotate functions, then
      ;; determine safe coding).
      (or x-symbol-emacs-has-correct-find-safe-coding
	  (aset char-coding-system-table (make-char name) t))
      (when registry
	(set-fontset-font "fontset-default" name (cons "*" registry))
	(when (eq graphic 0) (set-font-encoding registry name 0))
	(when ccl-program
	  (add-to-list 'font-ccl-encoder-alist (cons registry ccl-program))))
      name)))

(unless (fboundp 'find-charset)
  (defun find-charset (c) (and (charsetp c) c)))

;;; Misc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(autoload 'Info-goto-node "info")

(defvar x-symbol-data-directory (expand-file-name "x-symbol" data-directory))

(unless (fboundp 'signal-error) (defalias 'signal-error 'signal))

(unless (fboundp 'console-type)
  (defun console-type ()
    (case window-system
      (x 'x)
      (w32 'mswindows)
      (nil 'tty)
      (t window-system))))

(unless (fboundp 'symbol-value-in-buffer)
  (defun symbol-value-in-buffer (sym buf &optional def)
    (with-current-buffer buf (if (boundp sym) (symbol-value sym) def))))

(unless (fboundp 'make-key-weak-hashtable)
  (defun make-key-weak-hashtable (size &optional test)
    ;; The :weakness is (hopefully) ignored on Emacs-20
    (make-hash-table :size size :test test :weakness 'key)))

(unless (fboundp 'set-keymap-default-binding)
  (defun set-keymap-default-binding (map cmd)
    (define-key-after map [t] cmd t)))

(unless (fboundp 'destructive-alist-to-plist)
  (defun destructive-alist-to-plist (alist)
    (let ((next alist) tmp head)
      (while next
	(setq head (car next) tmp next)
	(if (not (consp head)) (pop next)
	  (setcar next (car head))
	  (setcar head (cdr head))
	  (setcdr head (setq next (cdr next)))
	  (setcdr tmp head))))
    alist))

(unless (fboundp 'plists-eq)
  ;; All kinds of potential problems here with "shadowed properties".
  (defun plists-eq (a b)
    (when (= (length a) (length b))
      (let (tmp)
	(while (and a
		    (setq tmp (plist-member b (pop a)))
		    (eq (car a) (cadr tmp)))
	  (setq a (cdr a)))
	(null a)))))

;; From XEmacs
(unless (fboundp 'sorted-key-descriptions)
  (defun sorted-key-descriptions (keys &optional separator)
    "Sort and separate the key descriptions for KEYS.
The sorting is done by length (shortest bindings first), and the bindings
are separated with SEPARATOR (\", \" by default)."
    (mapconcat 'key-description
	       (sort keys #'(lambda (x y)
			      (< (length x) (length y))))
	       (or separator ", "))))

;; defined in Emacs-21.3.50
(unless (fboundp 'file-remote-p)
  (defun file-remote-p (file-name)
    "Test whether FILE-NAME is looked for on a remote system."
    (cond ((featurep 'ange-ftp)
	   (if (fboundp 'ange-ftp-ftp-name)
	       (ange-ftp-ftp-name file-name)
	     (ange-ftp-ftp-path file-name)))
	  ((fboundp 'efs-ftp-path) (efs-ftp-path file-name))
	  (t nil))))

(defun x-symbol-directory-files (dirname &optional full match nosort
					 files-only)
  (let ((files (directory-files dirname full match nosort))
	result)
    (if (null files-only)
	files
      (while files
	(if (if (file-directory-p (car files))
		(null (eq files-only t))
	      (eq files-only t))
	    (push (car files) result))
	(setq files (cdr files)))
      (nreverse result))))

(defun x-symbol-event-in-current-buffer ()
  t)

(defun x-symbol-create-image (file type)
  (create-image file type nil :ascent 80))

(defun x-symbol-make-glyph (image)
  (propertize "x" 'display image))

(defun x-symbol-set-glyph-image (glyph image)
  (set-text-properties 0 (length glyph) (list 'display image) glyph))

(unless (fboundp 'event-closest-point)
  (defun event-closest-point (event) (posn-point (event-end event))))

(unless (fboundp 'event-buffer)
  (defun event-buffer (event) (window-buffer (posn-window (event-end event)))))

;;;
;;; X-Symbol functions
;;;

;; Defvarred to prevent their initialization code to run (since that
;; code uses XEmacs'isms that haven't been ported yet).
(defvar x-symbol-heading-strut-glyph " ")

;; Define face's vars for Emacs' font-lock.
(defvar x-symbol-invisible-face 'x-symbol-invisible-face)
(defvar x-symbol-face 'x-symbol-face)
(defvar x-symbol-sub-face 'x-symbol-sub-face)
(defvar x-symbol-sup-face 'x-symbol-sup-face)

(defvar x-symbol-emacs-w32-font-directories
  (mapcar (lambda (dir) (expand-file-name dir x-symbol-data-directory))
	  '("fonts/" "origfonts/" "genfonts/")))

(if (and (eq window-system 'w32)
	 x-symbol-emacs-w32-font-directories
	 (fboundp 'w32-find-bdf-fonts)
	 (boundp 'w32-bdf-filename-alist))
    (setq w32-bdf-filename-alist
	  (nconc w32-bdf-filename-alist
		 (w32-find-bdf-fonts x-symbol-emacs-w32-font-directories))))

;; Invisibility cannot be done this way in Emacs.
(defvar x-symbol-invisible-display-table nil)

(defalias 'x-symbol-window-width 'window-width)

(defun x-symbol-set-face-font (face font charsets default)
  (let ((fontset (concat "fontset-" (symbol-name face))))
    (unless (query-fontset fontset)
      ;; We assume that the first time around we're using latin-8859-1
      (new-fontset fontset
		   (x-complement-fontset-spec (make-vector 14 "*")
					      (list (cons 'ascii font)))))
    (dolist (charset charsets)
      (when charset (set-fontset-font fontset charset font)))
    (set-face-font face fontset)))

(defun x-symbol-event-matches-key-specifier-p (event specifier)
  (if (consp specifier) (setq specifier (event-convert-list specifier)))
  (if (consp event) (setq event (car event)))
  (eq event specifier))

;;;
;;; Possibly useful compat functions
;;;

;;; from Sang-Min
(defun start-itimer (name func value restart isidle)
  (run-with-idle-timer value restart func))
(defun itimer-live-p (obj) (and obj (not (aref obj 0))))

;;; x-symbol-emacs.el ends here