d7d90767124fe6e737878e00b8057c6fb9d8afd6
[gnus] / lisp / gnus-ems.el
1 ;;; gnus-ems.el --- functions for making Gnus work under different Emacsen
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs 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 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29   (require 'cl)
30   (require 'ring))
31
32 ;;; Function aliases later to be redefined for XEmacs usage.
33
34 (defvar gnus-mouse-2 [mouse-2])
35 (defvar gnus-down-mouse-3 [down-mouse-3])
36 (defvar gnus-down-mouse-2 [down-mouse-2])
37 (defvar gnus-widget-button-keymap nil)
38 (defvar gnus-mode-line-modified
39   (if (featurep 'xemacs)
40       '("--**-" . "-----")
41     '("**" "--")))
42
43 (eval-and-compile
44   (autoload 'gnus-xmas-define "gnus-xmas")
45   (autoload 'gnus-xmas-redefine "gnus-xmas"))
46
47 (autoload 'gnus-get-buffer-create "gnus")
48 (autoload 'nnheader-find-etc-directory "nnheader")
49 (autoload 'smiley-region "smiley")
50
51 (defun gnus-kill-all-overlays ()
52   "Delete all overlays in the current buffer."
53   (let* ((overlayss (overlay-lists))
54          (buffer-read-only nil)
55          (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
56     (while overlays
57       (delete-overlay (pop overlays)))))
58
59 ;;; Mule functions.
60
61 (defun gnus-mule-max-width-function (el max-width)
62   `(let* ((val (eval (, el)))
63           (valstr (if (numberp val)
64                       (int-to-string val) val)))
65      (if (> (length valstr) ,max-width)
66          (truncate-string-to-width valstr ,max-width)
67        valstr)))
68
69 (eval-and-compile
70   (if (featurep 'xemacs)
71       (gnus-xmas-define)
72     (defvar gnus-mouse-face-prop 'mouse-face
73       "Property used for highlighting mouse regions.")))
74
75 (defvar gnus-tmp-unread)
76 (defvar gnus-tmp-replied)
77 (defvar gnus-tmp-score-char)
78 (defvar gnus-tmp-indentation)
79 (defvar gnus-tmp-opening-bracket)
80 (defvar gnus-tmp-lines)
81 (defvar gnus-tmp-name)
82 (defvar gnus-tmp-closing-bracket)
83 (defvar gnus-tmp-subject-or-nil)
84 (defvar gnus-check-before-posting)
85 (defvar gnus-mouse-face)
86 (defvar gnus-group-buffer)
87
88 (defun gnus-ems-redefine ()
89   (cond
90    ((featurep 'xemacs)
91     (gnus-xmas-redefine))
92
93    ((featurep 'mule)
94     ;; Mule and new Emacs definitions
95
96     ;; [Note] Now there are three kinds of mule implementations,
97     ;; original MULE, XEmacs/mule and Emacs 20+ including
98     ;; MULE features.  Unfortunately these APIs are different.  In
99     ;; particular, Emacs (including original Mule) and XEmacs are
100     ;; quite different.  However, this version of Gnus doesn't support
101     ;; anything other than XEmacs 20+ and Emacs 20.3+.
102
103     ;; Predicates to check are following:
104     ;; (boundp 'MULE) is t only if Mule (original; anything older than
105     ;;                     Mule 2.3) is running.
106     ;; (featurep 'mule) is t when other mule variants are running.
107
108     ;; It is possible to detect XEmacs/mule by (featurep 'mule) and
109     ;; (featurep 'xemacs).  In this case, the implementation for
110     ;; XEmacs/mule may be shareable between XEmacs and XEmacs/mule.
111
112     (defvar gnus-summary-display-table nil
113       "Display table used in summary mode buffers.")
114     (defalias 'gnus-max-width-function 'gnus-mule-max-width-function)
115
116     (when (boundp 'gnus-check-before-posting)
117       (setq gnus-check-before-posting
118             (delq 'long-lines
119                   (delq 'control-chars gnus-check-before-posting))))
120
121     (defun gnus-summary-line-format-spec ()
122       (insert gnus-tmp-unread gnus-tmp-replied
123               gnus-tmp-score-char gnus-tmp-indentation)
124       (put-text-property
125        (point)
126        (progn
127          (insert
128           gnus-tmp-opening-bracket
129           (format "%4d: %-20s"
130                   gnus-tmp-lines
131                   (if (> (length gnus-tmp-name) 20)
132                       (truncate-string-to-width gnus-tmp-name 20)
133                     gnus-tmp-name))
134           gnus-tmp-closing-bracket)
135          (point))
136        gnus-mouse-face-prop gnus-mouse-face)
137       (insert " " gnus-tmp-subject-or-nil "\n")))))
138
139 ;; Clone of `appt-select-lowest-window' in appt.el.
140 (defun gnus-select-lowest-window ()
141 "Select the lowest window on the frame."
142   (let ((lowest-window (selected-window))
143         (bottom-edge (nth 3 (window-edges))))
144     (walk-windows (lambda (w)
145                     (let ((next-bottom-edge (nth 3 (window-edges w))))
146                       (when (< bottom-edge next-bottom-edge)
147                         (setq bottom-edge next-bottom-edge
148                               lowest-window w)))))
149     (select-window lowest-window)))
150
151 (defun gnus-region-active-p ()
152   "Say whether the region is active."
153   (and (boundp 'transient-mark-mode)
154        transient-mark-mode
155        (boundp 'mark-active)
156        mark-active))
157
158 (defun gnus-mark-active-p ()
159   "Non-nil means the mark and region are currently active in this buffer."
160   mark-active) ; aliased to region-exists-p in XEmacs.
161
162 (autoload 'gnus-alive-p "gnus-util")
163 (autoload 'mm-disable-multibyte "mm-util")
164
165 ;;; Image functions.
166
167 (defun gnus-image-type-available-p (type)
168   (and (fboundp 'image-type-available-p)
169        (image-type-available-p type)
170        (if (fboundp 'display-images-p)
171            (display-images-p)
172          t)))
173
174 (defun gnus-create-image (file &optional type data-p &rest props)
175   (let ((face (plist-get props :face)))
176     (when face
177       (setq props (plist-put props :foreground (face-foreground face)))
178       (setq props (plist-put props :background (face-background face))))
179     (ignore-errors
180       (apply 'create-image file type data-p props))))
181
182 (defun gnus-put-image (glyph &optional string category)
183   (let ((point (point)))
184     (insert-image glyph (or string " "))
185     (put-text-property point (point) 'gnus-image-category category)
186     (unless string
187       (put-text-property (1- (point)) (point)
188                          'gnus-image-text-deletable t))
189     glyph))
190
191 (defun gnus-remove-image (image &optional category)
192   "Remove the image matching IMAGE and CATEGORY found first."
193   (let ((start (point-min))
194         val end)
195     (while (and (not end)
196                 (or (setq val (get-text-property start 'display))
197                     (and (setq start
198                                (next-single-property-change start 'display))
199                          (setq val (get-text-property start 'display)))))
200       (setq end (or (next-single-property-change start 'display)
201                     (point-max)))
202       (if (and (equal val image)
203                (equal (get-text-property start 'gnus-image-category)
204                       category))
205           (progn
206             (put-text-property start end 'display nil)
207             (when (get-text-property start 'gnus-image-text-deletable)
208               (delete-region start end)))
209         (unless (= end (point-max))
210           (setq start end
211                 end nil))))))
212
213 (eval-and-compile
214   ;; XEmacs does not have window-inside-pixel-edges
215   (defalias 'gnus-window-inside-pixel-edges
216     (if (fboundp 'window-inside-pixel-edges)
217         'window-inside-pixel-edges
218       'window-pixel-edges))
219
220   (if (fboundp 'set-process-plist)
221       (progn
222         (defalias 'gnus-set-process-plist 'set-process-plist)
223         (defalias 'gnus-process-plist 'process-plist)
224         (defalias 'gnus-process-get 'process-get)
225         (defalias 'gnus-process-put 'process-put))
226     (defun gnus-set-process-plist (process plist)
227       "Replace the plist of PROCESS with PLIST.  Returns PLIST."
228       (put 'gnus-process-plist-internal process plist))
229
230     (defun gnus-process-plist (process)
231       "Return the plist of PROCESS."
232       ;; This form works but can't prevent the plist data from
233       ;; growing infinitely.
234       ;;(get 'gnus-process-plist-internal process)
235       (let* ((plist (symbol-plist 'gnus-process-plist-internal))
236              (tem (memq process plist)))
237         (prog1
238             (cadr tem)
239           ;; Remove it from the plist data.
240           (when tem
241             (if (eq plist tem)
242                 (progn
243                   (setcar plist (caddr plist))
244                   (setcdr plist (or (cdddr plist) '(nil))))
245               (setcdr (nthcdr (- (length plist) (length tem) 1) plist)
246                       (cddr tem)))))))
247
248     (defun gnus-process-get (process propname)
249       "Return the value of PROCESS' PROPNAME property.
250 This is the last value stored with `(gnus-process-put PROCESS PROPNAME VALUE)'."
251       (plist-get (gnus-process-plist process) propname))
252
253     (defun gnus-process-put (process propname value)
254       "Change PROCESS' PROPNAME property to VALUE.
255 It can be retrieved with `(gnus-process-get PROCESS PROPNAME)'."
256       (gnus-set-process-plist process
257                               (plist-put (gnus-process-plist process)
258                                          propname value)))))
259
260 (provide 'gnus-ems)
261
262 ;;; gnus-ems.el ends here