mh-compat.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. ;;; mh-compat.el --- make MH-E compatible with various versions of Emacs
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Bill Wohler <wohler@newt.com>
  4. ;; Maintainer: Bill Wohler <wohler@newt.com>
  5. ;; Keywords: mail
  6. ;; See: mh-e.el
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Change Log:
  20. ;;; Code:
  21. ;; This is a good place to gather code that is used for compatibility
  22. ;; between different versions of Emacs. Please document which versions
  23. ;; of Emacs that the defsubst, defalias, or defmacro applies. That
  24. ;; way, it's easy to occasionally go through this file and see which
  25. ;; macros we can retire.
  26. ;; Please use mh-gnus.el when providing compatibility with different
  27. ;; versions of Gnus.
  28. ;; Items are listed alphabetically (except for mh-require which is
  29. ;; needed sooner it would normally appear).
  30. (require 'mh-acros)
  31. (mh-do-in-gnu-emacs
  32. (defalias 'mh-require 'require))
  33. (mh-do-in-xemacs
  34. (defun mh-require (feature &optional filename noerror)
  35. "If feature FEATURE is not loaded, load it from FILENAME.
  36. If FEATURE is not a member of the list `features', then the feature
  37. is not loaded; so load the file FILENAME.
  38. If FILENAME is omitted, the printname of FEATURE is used as the file name.
  39. If the optional third argument NOERROR is non-nil,
  40. then return nil if the file is not found instead of signaling an error.
  41. Simulate NOERROR argument in XEmacs which lacks it."
  42. (if (not (featurep feature))
  43. (if filename
  44. (load filename noerror t)
  45. (load (format "%s" feature) noerror t)))))
  46. (defun-mh mh-assoc-string assoc-string (key list case-fold)
  47. "Like `assoc' but specifically for strings.
  48. Case is ignored if CASE-FOLD is non-nil.
  49. This function is used by Emacs versions that lack `assoc-string',
  50. introduced in Emacs 22."
  51. (if case-fold
  52. (assoc-ignore-case key list)
  53. (assoc key list)))
  54. ;; For XEmacs.
  55. (defalias 'mh-cancel-timer
  56. (if (fboundp 'cancel-timer)
  57. 'cancel-timer
  58. 'delete-itimer))
  59. (defun mh-display-color-cells (&optional display)
  60. "Return the number of color cells supported by DISPLAY.
  61. This function is used by XEmacs to return 2 when `device-color-cells'
  62. or `display-color-cells' returns nil. This happens when compiling or
  63. running on a tty and causes errors since `display-color-cells' is
  64. expected to return an integer."
  65. (cond ((fboundp 'display-color-cells) ; GNU Emacs, XEmacs 21.5b28
  66. (or (display-color-cells display) 2))
  67. ((fboundp 'device-color-cells) ; XEmacs 21.4
  68. (or (device-color-cells display) 2))
  69. (t 2)))
  70. (defmacro mh-display-completion-list (completions &optional common-substring)
  71. "Display the list of COMPLETIONS.
  72. See documentation for `display-completion-list' for a description of the
  73. arguments COMPLETIONS and perhaps COMMON-SUBSTRING.
  74. This macro is used by Emacs versions that lack a COMMON-SUBSTRING
  75. argument, introduced in Emacs 22."
  76. (if (< emacs-major-version 22)
  77. `(display-completion-list ,completions)
  78. `(display-completion-list ,completions ,common-substring)))
  79. (defmacro mh-face-foreground (face &optional frame inherit)
  80. "Return the foreground color name of FACE, or nil if unspecified.
  81. See documentation for `face-foreground' for a description of the
  82. arguments FACE, FRAME, and perhaps INHERIT.
  83. This macro is used by Emacs versions that lack an INHERIT argument,
  84. introduced in Emacs 22."
  85. (if (< emacs-major-version 22)
  86. `(face-foreground ,face ,frame)
  87. `(face-foreground ,face ,frame ,inherit)))
  88. (defmacro mh-face-background (face &optional frame inherit)
  89. "Return the background color name of face, or nil if unspecified.
  90. See documentation for `back-foreground' for a description of the
  91. arguments FACE, FRAME, and INHERIT.
  92. This macro is used by Emacs versions that lack an INHERIT argument,
  93. introduced in Emacs 22."
  94. (if (< emacs-major-version 22)
  95. `(face-background ,face ,frame)
  96. `(face-background ,face ,frame ,inherit)))
  97. (defun-mh mh-font-lock-add-keywords font-lock-add-keywords
  98. (mode keywords &optional how)
  99. "XEmacs does not have `font-lock-add-keywords'.
  100. This function returns nil on that system.")
  101. (defun-mh mh-image-load-path-for-library
  102. image-load-path-for-library (library image &optional path no-error)
  103. "Return a suitable search path for images used by LIBRARY.
  104. It searches for IMAGE in `image-load-path' (excluding
  105. \"`data-directory'/images\") and `load-path', followed by a path
  106. suitable for LIBRARY, which includes \"../../etc/images\" and
  107. \"../etc/images\" relative to the library file itself, and then
  108. in \"`data-directory'/images\".
  109. Then this function returns a list of directories which contains
  110. first the directory in which IMAGE was found, followed by the
  111. value of `load-path'. If PATH is given, it is used instead of
  112. `load-path'.
  113. If NO-ERROR is non-nil and a suitable path can't be found, don't
  114. signal an error. Instead, return a list of directories as before,
  115. except that nil appears in place of the image directory.
  116. Here is an example that uses a common idiom to provide
  117. compatibility with versions of Emacs that lack the variable
  118. `image-load-path':
  119. ;; Shush compiler.
  120. (defvar image-load-path)
  121. (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
  122. (image-load-path (cons (car load-path)
  123. (when (boundp 'image-load-path)
  124. image-load-path))))
  125. (mh-tool-bar-folder-buttons-init))"
  126. (unless library (error "No library specified"))
  127. (unless image (error "No image specified"))
  128. (let (image-directory image-directory-load-path)
  129. ;; Check for images in image-load-path or load-path.
  130. (let ((img image)
  131. (dir (or
  132. ;; Images in image-load-path.
  133. (mh-image-search-load-path image)
  134. ;; Images in load-path.
  135. (locate-library image)))
  136. parent)
  137. ;; Since the image might be in a nested directory (for
  138. ;; example, mail/attach.pbm), adjust `image-directory'
  139. ;; accordingly.
  140. (when dir
  141. (setq dir (file-name-directory dir))
  142. (while (setq parent (file-name-directory img))
  143. (setq img (directory-file-name parent)
  144. dir (expand-file-name "../" dir))))
  145. (setq image-directory-load-path dir))
  146. ;; If `image-directory-load-path' isn't Emacs's image directory,
  147. ;; it's probably a user preference, so use it. Then use a
  148. ;; relative setting if possible; otherwise, use
  149. ;; `image-directory-load-path'.
  150. (cond
  151. ;; User-modified image-load-path?
  152. ((and image-directory-load-path
  153. (not (equal image-directory-load-path
  154. (file-name-as-directory
  155. (expand-file-name "images" data-directory)))))
  156. (setq image-directory image-directory-load-path))
  157. ;; Try relative setting.
  158. ((let (library-name d1ei d2ei)
  159. ;; First, find library in the load-path.
  160. (setq library-name (locate-library library))
  161. (if (not library-name)
  162. (error "Cannot find library %s in load-path" library))
  163. ;; And then set image-directory relative to that.
  164. (setq
  165. ;; Go down 2 levels.
  166. d2ei (file-name-as-directory
  167. (expand-file-name
  168. (concat (file-name-directory library-name) "../../etc/images")))
  169. ;; Go down 1 level.
  170. d1ei (file-name-as-directory
  171. (expand-file-name
  172. (concat (file-name-directory library-name) "../etc/images"))))
  173. (setq image-directory
  174. ;; Set it to nil if image is not found.
  175. (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
  176. ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
  177. ;; Use Emacs's image directory.
  178. (image-directory-load-path
  179. (setq image-directory image-directory-load-path))
  180. (no-error
  181. (message "Could not find image %s for library %s" image library))
  182. (t
  183. (error "Could not find image %s for library %s" image library)))
  184. ;; Return an augmented `path' or `load-path'.
  185. (nconc (list image-directory)
  186. (delete image-directory (copy-sequence (or path load-path))))))
  187. (defun-mh mh-image-search-load-path
  188. image-search-load-path (file &optional path)
  189. "Emacs 21 and XEmacs don't have `image-search-load-path'.
  190. This function returns nil on those systems."
  191. nil)
  192. ;; For XEmacs.
  193. (defalias 'mh-line-beginning-position
  194. (if (fboundp 'line-beginning-position)
  195. 'line-beginning-position
  196. 'point-at-bol))
  197. ;; For XEmacs.
  198. (defalias 'mh-line-end-position
  199. (if (fboundp 'line-end-position)
  200. 'line-end-position
  201. 'point-at-eol))
  202. (mh-require 'mailabbrev nil t)
  203. (defun-mh mh-mail-abbrev-make-syntax-table
  204. mail-abbrev-make-syntax-table ()
  205. "Emacs 21 and XEmacs don't have `mail-abbrev-make-syntax-table'.
  206. This function returns nil on those systems."
  207. nil)
  208. (defun-mh mh-match-string-no-properties
  209. match-string-no-properties (num &optional string)
  210. "Return string of text matched by last search, without text properties.
  211. This function is used by XEmacs that lacks `match-string-no-properties'.
  212. The function `buffer-substring-no-properties' is used instead.
  213. The argument STRING is ignored."
  214. (buffer-substring-no-properties
  215. (match-beginning num) (match-end num)))
  216. (defun-mh mh-replace-regexp-in-string replace-regexp-in-string
  217. (regexp rep string &optional fixedcase literal subexp start)
  218. "Replace REGEXP with REP everywhere in STRING and return result.
  219. This function is used by XEmacs that lacks `replace-regexp-in-string'.
  220. The function `replace-in-string' is used instead.
  221. The arguments FIXEDCASE, SUBEXP, and START, used by
  222. `replace-in-string' are ignored."
  223. (replace-in-string string regexp rep literal))
  224. (defun-mh mh-test-completion
  225. test-completion (string collection &optional predicate)
  226. "Return non-nil if STRING is a valid completion.
  227. XEmacs does not have `test-completion'. This function returns nil
  228. on that system." nil)
  229. ;; Copy of constant from url-util.el in Emacs 22; needed by Emacs 21.
  230. (if (not (boundp 'url-unreserved-chars))
  231. (defconst mh-url-unreserved-chars
  232. '(
  233. ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
  234. ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
  235. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
  236. ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
  237. "A list of characters that are _NOT_ reserved in the URL spec.
  238. This is taken from RFC 2396."))
  239. (defun-mh mh-url-hexify-string url-hexify-string (str)
  240. "Escape characters in a string.
  241. This is a copy of `url-hexify-string' from url-util.el in Emacs
  242. 22; needed by Emacs 21."
  243. (mapconcat
  244. (lambda (char)
  245. ;; Fixme: use a char table instead.
  246. (if (not (memq char mh-url-unreserved-chars))
  247. (if (> char 255)
  248. (error "Hexifying multibyte character %s" str)
  249. (format "%%%02X" char))
  250. (char-to-string char)))
  251. str ""))
  252. (defun-mh mh-view-mode-enter
  253. view-mode-enter (&optional return-to exit-action)
  254. "Enter View mode.
  255. This function is used by XEmacs that lacks `view-mode-enter'.
  256. The function `view-mode' is used instead.
  257. The arguments RETURN-TO and EXIT-ACTION are ignored."
  258. ;; Shush compiler.
  259. (if return-to nil)
  260. (if exit-action nil)
  261. (view-mode 1))
  262. (defun-mh mh-window-full-height-p
  263. window-full-height-p (&optional WINDOW)
  264. "Return non-nil if WINDOW is not the result of a vertical split.
  265. This function is defined in XEmacs as it lacks
  266. `window-full-height-p'. The values of the functions
  267. `window-height' and `frame-height' are compared instead. The
  268. argument WINDOW is ignored."
  269. (= (1+ (window-height))
  270. (frame-height)))
  271. (defmacro mh-write-file-functions ()
  272. "Return `write-file-functions' if it exists.
  273. Otherwise return `local-write-file-hooks'.
  274. This macro exists purely for compatibility. The former symbol is used
  275. in Emacs 22 onward while the latter is used in previous versions and
  276. XEmacs."
  277. (if (boundp 'write-file-functions)
  278. ''write-file-functions ;Emacs 22 on
  279. ''local-write-file-hooks)) ;XEmacs
  280. (provide 'mh-compat)
  281. ;; Local Variables:
  282. ;; no-byte-compile: t
  283. ;; indent-tabs-mode: nil
  284. ;; sentence-end-double-space: nil
  285. ;; End:
  286. ;;; mh-compat.el ends here