diff options
| author | Kenichi Handa | 2003-06-17 10:56:24 +0000 |
|---|---|---|
| committer | Kenichi Handa | 2003-06-17 10:56:24 +0000 |
| commit | c7d9df18e4160d8d75a99b7baf7204195adec22c (patch) | |
| tree | e96f280750ce67f7158f8dd174a58ea0a9ea9185 | |
| parent | f147fd76db3f412d36c2d764dcce44c91b307ca8 (diff) | |
| download | emacs-c7d9df18e4160d8d75a99b7baf7204195adec22c.tar.gz emacs-c7d9df18e4160d8d75a99b7baf7204195adec22c.zip | |
(x-select-request-type): New variable.
(x-select-utf8-or-ctext): New function.
(x-selection-value): New function.
(x-cut-buffer-or-selection-value): Call x-selection-value to get
a selection data. Set next-selection-coding-system to nil.
| -rw-r--r-- | lisp/term/x-win.el | 122 |
1 files changed, 104 insertions, 18 deletions
diff --git a/lisp/term/x-win.el b/lisp/term/x-win.el index e49836f0c2b..ac5d14d114d 100644 --- a/lisp/term/x-win.el +++ b/lisp/term/x-win.el | |||
| @@ -2138,6 +2138,105 @@ This is in addition to, but in preference to, the primary selection." | |||
| 2138 | (setq x-last-selected-text-clipboard text)) | 2138 | (setq x-last-selected-text-clipboard text)) |
| 2139 | ) | 2139 | ) |
| 2140 | 2140 | ||
| 2141 | (defvar x-select-request-type nil | ||
| 2142 | "*Data type request for X selection. | ||
| 2143 | The value is nil, one of the following data types, or a list of them: | ||
| 2144 | `COMPOUND_TEXT', `UTF8_STRING', `STRING', `TEXT' | ||
| 2145 | |||
| 2146 | If the value is nil, try `COMPOUND_TEXT' and `UTF8_STRING', and | ||
| 2147 | use the more appropriate result. If both fail, try `STRING', and | ||
| 2148 | then `TEXT'. | ||
| 2149 | |||
| 2150 | If the value is one of the above symbols, try only the specified | ||
| 2151 | type. | ||
| 2152 | |||
| 2153 | If the value is a list of them, try each of them in the specified | ||
| 2154 | order until succeed.") | ||
| 2155 | |||
| 2156 | ;; Helper function for x-selection-value. Select UTF8 or CTEXT | ||
| 2157 | ;; whichever is more appropriate. Here, we use this heurisitcs. | ||
| 2158 | ;; | ||
| 2159 | ;; (1) If their lengthes are different, select the longer one. This | ||
| 2160 | ;; is because an X client may just cut off unsupported characters. | ||
| 2161 | ;; | ||
| 2162 | ;; (2) Otherwise, if the Nth character of CTEXT is an ASCII | ||
| 2163 | ;; character that is different from the Nth character of UTF8, | ||
| 2164 | ;; select UTF8. This is because an X client may replace unsupported | ||
| 2165 | ;; characters with some ASCII character (typically ` ' or `?') in | ||
| 2166 | ;; CTEXT. | ||
| 2167 | ;; | ||
| 2168 | ;; (3) Otherwise, select CTEXT. This is because legacy charsets are | ||
| 2169 | ;; better for the current Emacs, especially when the selection owner | ||
| 2170 | ;; is also Emacs. | ||
| 2171 | |||
| 2172 | (defun x-select-utf8-or-ctext (utf8 ctext) | ||
| 2173 | (let ((len-utf8 (length utf8)) | ||
| 2174 | (len-ctext (length ctext)) | ||
| 2175 | (selected ctext) | ||
| 2176 | (i 0) | ||
| 2177 | char) | ||
| 2178 | (if (/= len-utf8 len-ctext) | ||
| 2179 | (if (> len-utf8 len-ctext) utf8 ctext) | ||
| 2180 | (while (< i len-utf8) | ||
| 2181 | (setq char (aref ctext i)) | ||
| 2182 | (if (and (< char 128) (/= char (aref utf8 i))) | ||
| 2183 | (setq selected utf8 | ||
| 2184 | i len-utf8) | ||
| 2185 | (setq i (1+ i)))) | ||
| 2186 | selected))) | ||
| 2187 | |||
| 2188 | (defun x-selection-value (type) | ||
| 2189 | (let (text) | ||
| 2190 | (cond ((null x-select-request-type) | ||
| 2191 | (let (utf8 ctext utf8-coding) | ||
| 2192 | ;; We try both UTF8_STRING and COMPOUND_TEXT, and choose | ||
| 2193 | ;; the more appropriate one. If both fail, try STRING. | ||
| 2194 | |||
| 2195 | ;; At first try UTF8_STRING. | ||
| 2196 | (setq utf8 (condition-case nil | ||
| 2197 | (x-get-selection type 'UTF8_STRING) | ||
| 2198 | (error nil)) | ||
| 2199 | utf8-coding last-coding-system-used) | ||
| 2200 | (if utf8 | ||
| 2201 | ;; If it is a locale selection, choose it. | ||
| 2202 | (or (get-text-property 0 'foreign-selection utf8) | ||
| 2203 | (setq text utf8))) | ||
| 2204 | ;; If not yet decided, try COMPOUND_TEXT. | ||
| 2205 | (if (not text) | ||
| 2206 | (if (setq ctext (condition-case nil | ||
| 2207 | (x-get-selection type 'COMPOUND_TEXT) | ||
| 2208 | (error nil))) | ||
| 2209 | ;; If UTF8_STRING was also successful, choose the | ||
| 2210 | ;; more appropriate one from UTF8 and CTEXT. | ||
| 2211 | (if utf8 | ||
| 2212 | (setq text (x-select-utf8-or-ctext utf8 ctext)) | ||
| 2213 | ;; Othewise, choose CTEXT. | ||
| 2214 | (setq text ctext)))) | ||
| 2215 | ;; If not yet decided, try STRING. | ||
| 2216 | (or text | ||
| 2217 | (setq text (condition-case nil | ||
| 2218 | (x-get-selection type 'STRING) | ||
| 2219 | (error nil)))) | ||
| 2220 | (if (eq text utf8) | ||
| 2221 | (setq last-coding-system-used utf8-coding)))) | ||
| 2222 | |||
| 2223 | ((consp x-select-request-type) | ||
| 2224 | (let ((tail x-select-request-type)) | ||
| 2225 | (while (and tail (not text)) | ||
| 2226 | (condition-case nil | ||
| 2227 | (setq text (x-get-selection type (car tail))) | ||
| 2228 | (error nil)) | ||
| 2229 | (setq tail (cdr tail))))) | ||
| 2230 | |||
| 2231 | (t | ||
| 2232 | (condition-case nil | ||
| 2233 | (setq text (x-get-selection type x-select-request-type)) | ||
| 2234 | (error nil)))) | ||
| 2235 | |||
| 2236 | (if text | ||
| 2237 | (remove-text-properties 0 (length text) '(foreign-selection nil) text)) | ||
| 2238 | text)) | ||
| 2239 | |||
| 2141 | ;;; Return the value of the current X selection. | 2240 | ;;; Return the value of the current X selection. |
| 2142 | ;;; Consult the selection, and the cut buffer. Treat empty strings | 2241 | ;;; Consult the selection, and the cut buffer. Treat empty strings |
| 2143 | ;;; as if they were unset. | 2242 | ;;; as if they were unset. |
| @@ -2147,15 +2246,7 @@ This is in addition to, but in preference to, the primary selection." | |||
| 2147 | (defun x-cut-buffer-or-selection-value () | 2246 | (defun x-cut-buffer-or-selection-value () |
| 2148 | (let (clip-text primary-text cut-text) | 2247 | (let (clip-text primary-text cut-text) |
| 2149 | (when x-select-enable-clipboard | 2248 | (when x-select-enable-clipboard |
| 2150 | ;; Don't die if x-get-selection signals an error. | 2249 | (setq clip-text (x-selection-value 'CLIPBOARD)) |
| 2151 | (if (null clip-text) | ||
| 2152 | (condition-case c | ||
| 2153 | (setq clip-text (x-get-selection 'CLIPBOARD 'COMPOUND_TEXT)) | ||
| 2154 | (error nil))) | ||
| 2155 | (if (null clip-text) | ||
| 2156 | (condition-case c | ||
| 2157 | (setq clip-text (x-get-selection 'CLIPBOARD 'STRING)) | ||
| 2158 | (error nil))) | ||
| 2159 | (if (string= clip-text "") (setq clip-text nil)) | 2250 | (if (string= clip-text "") (setq clip-text nil)) |
| 2160 | 2251 | ||
| 2161 | ;; Check the CLIPBOARD selection for 'newness', is it different | 2252 | ;; Check the CLIPBOARD selection for 'newness', is it different |
| @@ -2175,15 +2266,7 @@ This is in addition to, but in preference to, the primary selection." | |||
| 2175 | (setq x-last-selected-text-clipboard clip-text)))) | 2266 | (setq x-last-selected-text-clipboard clip-text)))) |
| 2176 | ) | 2267 | ) |
| 2177 | 2268 | ||
| 2178 | ;; Don't die if x-get-selection signals an error. | 2269 | (setq primary-text (x-selection-value 'PRIMARY)) |
| 2179 | (if (null primary-text) | ||
| 2180 | (condition-case c | ||
| 2181 | (setq primary-text (x-get-selection 'PRIMARY 'COMPOUND_TEXT)) | ||
| 2182 | (error nil))) | ||
| 2183 | (if (null primary-text) | ||
| 2184 | (condition-case c | ||
| 2185 | (setq primary-text (x-get-selection 'PRIMARY 'STRING)) | ||
| 2186 | (error nil))) | ||
| 2187 | ;; Check the PRIMARY selection for 'newness', is it different | 2270 | ;; Check the PRIMARY selection for 'newness', is it different |
| 2188 | ;; from what we remebered them to be last time we did a | 2271 | ;; from what we remebered them to be last time we did a |
| 2189 | ;; cut/paste operation. | 2272 | ;; cut/paste operation. |
| @@ -2218,6 +2301,9 @@ This is in addition to, but in preference to, the primary selection." | |||
| 2218 | (t | 2301 | (t |
| 2219 | (setq x-last-selected-text-cut cut-text)))) | 2302 | (setq x-last-selected-text-cut cut-text)))) |
| 2220 | 2303 | ||
| 2304 | ;; As we have done one selection, clear this now. | ||
| 2305 | (setq next-selection-coding-system nil) | ||
| 2306 | |||
| 2221 | ;; At this point we have recorded the current values for the | 2307 | ;; At this point we have recorded the current values for the |
| 2222 | ;; selection from clipboard (if we are supposed to) primary, | 2308 | ;; selection from clipboard (if we are supposed to) primary, |
| 2223 | ;; and cut buffer. So return the first one that has changed | 2309 | ;; and cut buffer. So return the first one that has changed |