Mercurial > emacs
view src/w32select.c @ 98644:e1cc41b9282d
2008-10-12 Carsten Dominik <carsten.dominik@gmail.com>
* org.el (org-edit-fixed-width-region): Exclude final newline from
picture area.
* org-export-latex.el (org-export-latex-subcontent): Add labels to
sections, to make internal links work.
(org-export-latex-fontify-headline): Do not remove all text
properties, to make sure that target properties survive this
process.
* org-exp.el (org-export-preprocess-string): Change sequence of
modifications, to make sure links are prepared before the LaTeX
conversions do happen.
* org-attach.el (org-attach-delete-all): Renamed from
`org-attch-delete'. Add a security query before deleting the
entire directory. New optional argument FORCE can overrule the
security query.
(org-attach-delete-one): New command.
* org-attach.el (org-attach-file-list): Fix bug with directory.
* org.el (org-apps-regexp-alist): New function.
(org-file-apps): Add auto-mode to the default value.
(org-open-file): Use the new structure of org-file-apps.
* org-attach.el (org-attach): Support the new keys.
(org-attach-method): New option.
* org-bbdb.el (org-bbdb-anniversaries): Fix but with 29 Feb/1
March.
* org.el (org-remove-uniherited-tags): Fix reverse interpretation
of the list value o `org-use-tag-inheritance'.
* org-attach.el (org-attach-auto-tag): New option.
(org-attach-tag, org-attach-untag): New functions.
(org-attach-attach, org-attach-new, org-attach-sync): Call
`org-attach-tag'.
(org-attach-delete): Call `org-attach-untag'.
* org-attach.el: New file.
* org-table.el (orgtbl-self-insert-command): Make this work for
the keypad as well.
* org.el (org-add-log-setup): Limit searc for drawers to entry
text, not to subtree.
* org-clock.el (org-clock-heading-for-remember): New variable.
(org-clock-in): Set `org-clock-heading-for-remember'.
2008-10-12 James TD Smith <ahktenzero@mohorovi.cc>
* org-remember.el (org-remember-apply-template): Add new
expansions: %k, %K for currently clocked task and a link to the
currently clocked task, and %< to file notes in the currently
clocked task.
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org-export-latex.el (org-export-latex-make-header): Also insert
the content of the property :latex-header-extra.
* org-exp.el (org-infile-export-plist): Put the content of
#+LATEX_HEADER: into the property :latex-header-extra.
* org-colview.el (org-columns-get-format-and-top-level): Remove
resetting the marker.
* org-colview-xemacs.el (org-columns-get-format-and-top-level):
Remove resetting the marker.
* org.el (org-entry-property-inherited-from): Improve docstring.
(org-entry-get-with-inheritance): Reset marker before starting the
search.
* org-exp.el (org-infile-export-plist): Allow multiple STYLE lines.
* org.el (org-entry-get-multivalued-property)
(org-entry-protect-space, org-entry-restore-space): New
functions.
(org-file-apps-defaults-macosx): Let postscript files be opened by
preview.
(org-time-stamp-inactive): Call `org-time-stamp'.
(org-time-stamp): New argument `inactive'. Also edit inacive
stamps. Convert time stamp type.
(org-open-file): Interpret the `default' value for the `command'
in `org-file-apps'.
* org-id.el (org-id-int-to-b36-one-digit)
(org-id-b36-to-int-one-digit, org-id-int-to-b36)
(org-id-b36-to-int, org-id-time-to-b36): Modified from b62 to
b36.
* org-id.el (org-id-reverse-string): New function.
(org-id-new): Use `org-id-reverse-string' to make sure the
beginning chars of the ID are mutating fast. This allows to use a
directory structure to spread things better.
(org-id-prefix): Changed default to nil.
* org-list.el (org-move-item-down, org-move-item-up): Remember and
restore the column of the cursor position.
* org-remember.el (org-remember-apply-template): Remove properties
from `initial'.
* org-wl.el (org-wl-open): Remove useless call to
`wl-thread-open-all'.
* org-remember.el (org-remember-handler): Fix bug with `bottom'
location.
* org-bbdb.el (org-bbdb-anniversaries): Require bbdb in
`org-bbdb-anniversaries'.
* org.el (org-get-next-sibling, org-forward-same-level): New
functions, similar to the outline versions, but invisible headings
are OK.
2008-10-12 Bastien Guerry <bzg@altern.org>
* org.el (org-auto-repeat-maybe): Insert a space between
the timestamp's type and the timestamp itself.
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org-table.el (org-table-sum): Do not format the result with %g,
it does rounding when there are too many digits.
* org.el (org-map-entries): Protect the keyword-selecting variables.
2008-10-12 Bastien Guerry <bzg@altern.org>
* org-agenda.el (org-agenda-to-appt): Make sure the function check
against all agenda files.
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org-list.el: New file, aggregating list functions from org.el
and org-export-latex.el.
* org.el (org-edit-src-region-extra): New option.
* org-agenda.el (org-agenda-to-appt): Fix bug with appointment
time before 1am.
2008-10-12 Bastien Guerry <bzg@altern.org>
* org-export-latex.el (org-export-latex-keywords-maybe): Bug fix.
2008-10-12 James TA Smith <ahktenzero@mohorovi.cc>
* org-plot.el (org-plot/gnuplot): Make tables starting with a
hline work correctly.
(org-plot/gnuplot-script): Put commas at the end of each script
line.
* org.el (org-get-refile-targets): Replace links with their
descriptions
(org-imenu-get-tree): Replace links with their descriptions.
* org-remember.el (org-remember-apply-template): Add a new
expansion for adding properties to remember items.
* org.el (org-add-log-setup): Skip over drawers (properties,
clocks etc) when adding notes.
* org-agenda.el (org-agenda-get-closed): show durations of clocked
items as well as the start and end times.
* org-compat.el (org-get-x-clipboard-compat): Add a compat
function for fetching the X clipboard on XEmacs and GNU Emacs 21.
* org-remember.el (org-get-x-clipboard): Use the compat
function to get clipboard values when x-selection-value is
unavailable. Use substring-no-properties instead of
set-text-properties to remove text properties from the clipboard
value.
* lisp/org-clock.el (org-update-mode-line): Support limiting the
modeline clock string, and display the full todo value in the
tooltip. Set a local keymap so mouse-3 on the clock string goes to
the currently clocked task.
(org-clock-string-limit): Add a custom value for the maximum
length of the clock string in the modeline.
(org-clock-mode-map): Add a keymap for the modeline string
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org-compat.el (org-propertize): New function.
2008-10-12 Bastien Guerry <bzg@altern.org>
* org-export-latex.el (org-export-latex-tables): protect exported
tables from further special chars conversion.
(org-export-latex-preprocess): Preserve LaTeX environments.
(org-list-parse-list): Parse descriptive lists.
(org-list-to-generic, org-list-to-latex, org-list-to-html)
(org-list-to-texinfo): Export descriptive lists.
(org-quote-chars): Remove.
(org-export-latex-keywords-maybe): Use `replace-regexp-in-string'.
(org-export-latex-list-beginning-re): Rename to
`org-list-beginning-re'
(org-list-item-begin): Rename to `org-list-item-beginning'
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-refile): Allow refiling to the last entry in the
buffer.
(org-get-tags-at): Fix bug when inheritance is turned off.
* org.el (org-indent-line-function): No longer check for src
regions, this is too much overhead.
* org-agenda.el (org-agenda-highlight-todo): Fix bugs with keyword
matching.
* org.el (org-scan-tags): Make sure that tags matching is not case
sensitive. TODO keyword matching is case sensitive, however, to
avoid confusion with similar words that are not meant to be
keywords.
* org.el (org-get-local-tags-at): New function.
(org-get-local-tags): New function.
* org-exp.el (org-export-get-categories): New function.
* org-agenda.el (org-sorting-choice)
(org-agenda-sorting-strategy, org-agenda-get-todos)
(org-agenda-get-timestamps, org-agenda-get-deadlines)
(org-agenda-get-scheduled, org-agenda-get-blocks)
(org-entries-lessp): Implement sorting by TODO state.
(org-cmp-todo-state): New defsubst.
* org-colview.el (org-colview-construct-allowed-dates): New
function.
(org-columns-next-allowed-value): Use
`org-colview-construct-allowed-dates'.
* org-colview-xemacs.el (org-colview-construct-allowed-dates): New
function.
(org-columns-next-allowed-value): Use
`org-colview-construct-allowed-dates'.
* org.el (org-protect-slash): New function.
(org-get-refile-targets): Use `org-protect-slash'.
* org-agenda.el (org-global-tags-completion-table): New variable.
* org-exp.el (org-export-handle-export-tags): New function.
(org-export-preprocess-string): Call
`org-export-handle-export-tags'.
* org-plot.el: New file.
* org-publish.el (org-publish-expand-components): Function removed.
(org-publish-expand-projects): Allow components to have components.
* org.el (org-indent-line-function): Do not indent in regions that
are external source code.
(org-yank-and-fold-if-subtree): New function.
* org-agenda.el (org-agenda-todayp): New function.
(org-agenda-get-deadlines, org-agenda-get-scheduled): Use
`org-agenda-todayp'.
* org.el (org-insert-heading-respect-content)
(org-insert-todo-heading-respect-content): New commands.
(org-insert-heading-respect-content): New option.
(org-insert-heading): Respect `org-insert-heading-respect-content'.
* org-clock.el (org-clock-find-position): Make sure the note after
the clock line gets moved into the new clock drawer.
* org-id.el (org-id-new): New option.
* org-table.el (org-table-copy-down): Avoid overflow during
increment. Use prefix argument 0 to temporarily disable the
increment.
* org-exp.el (org-export-as-html): Do not turn on the major mode
if the buffer will be killed anyway.
(org-get-current-options): Exclude the #+TEXT field.
(org-export-as-html): Make sure text before the first headline is
a paragraph.
* org-publish.el (org-publish-org-to): Tell the exporter that this
buffer will be killed, so it is not necessary to do major mode
initialization.
* org-archive.el (org-archive-to-archive-sibling): Show empty
lines after folding the archive sibling.
* org.el (org-log-note-extra): New variable.
2008-10-12 Bastien Guerry <bzg@altern.org>
* org.el (org-additional-option-like-keywords): Added keywords for
the _QUOTE, _VERSE and _SRC environments.
* org-export-latex.el (org-export-latex-preprocess): Fix bug when
exporting _QUOTE and _VERSE environments.
2008-10-12 Carsten Dominik <dominik@science.uva.nl>
* org-agenda.el (org-agenda-filter-by-tag): New command.
* org-exp.el (org-get-current-options): Remove angular brackets
from the date entry.
* org.el (org-edit-fixed-width-region): New function.
(org-edit-fixed-width-region): Also try
`org-edit-fixed-width-region'.
(org-edit-fixed-width-region-mode): New option.
(org-activate-code): Only interprete lines starting with colon
plus a space as example lines.
* org-remember.el (org-remember-templates): Add nil instead of
empty strings to fix the length of remember templates.
* org-table.el (org-calc-default-modes): Fix the time format for
calc, from 12 hour to 24 hour clock.
* org-agenda.el (org-agenda-get-deadlines)
(org-agenda-get-scheduled): Avoid `time-of-day' extraction for
entries that are pre-warnings of deadlines or reminders.
* org.el (org-sort-entries-or-items): Make numeric and alpha
comparisons ignore any TODO keyword and priority cookie.
* org-remember.el (org-remember-handler): Reinterpretation of the
prefix argument.
* org-agenda.el (org-agenda-get-scheduled): Use new
`org-scheduled' face.
* org-faces.el (org-scheduled): New face.
* org-wl.el (org-wl-open): Remove incorrect declaration.
* org-gnus.el (org-gnus-store-link): Support for :to information
in gnus links.
* org-exp.el (org-export-as-html): Fixed typo in creator
information.
(org-export-protect-examples): New parameter indent. Insert extra
spaces only when this parameter is specified.
(org-export-preprocess-string): Call `org-export-protect-examples'
with an indentation parameter when exporting to ASCII.
* org-remember.el (org-remember-templates)
(org-remember-apply-template): Allow the file component to be a
function.
* org.el (org-goto-local-search-headings): Renamed from
`org-goto-local-search-forward-headings'. Added the possibility
to search backwards.
* org-export-latex.el (org-export-latex): New customization
group.
* org-agenda.el (org-write-agenda): Erase buffer for txt export.
* org-exp.el (org-html-do-expand): Allow {} to terminate
tex macro
* org.el (org-buffer-list): Select buffers based on major mode,
not on file name.
* org-agenda.el (org-agenda-align-tags): Fix bug with malformed
face property.
* org-colview.el (org-columns-display-here): Use
`org-columns-modify-value-for-display-function'.
* org-colview-xemacs.el (org-columns-display-here): Use
`org-columns-modify-value-for-display-function'.
* org.el (org-columns-modify-value-for-display-function): New option.
* org-publish.el (org-publish-file): Make sure the directory match
for the publishing directory works correctly.
* org-agenda.el (org-agenda-execute-calendar-command)
(org-agenda-diary-entry): Additional optional argument.
author | Carsten Dominik <dominik@science.uva.nl> |
---|---|
date | Sun, 12 Oct 2008 06:12:44 +0000 |
parents | 8971ddf55736 |
children | e038c1a8307c |
line wrap: on
line source
/* Selection processing for Emacs on the Microsoft W32 API. Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. This file is part of GNU Emacs. GNU Emacs 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 3 of the License, or (at your option) any later version. GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ /* Written by Kevin Gallo, Benjamin Riefenstahl */ /* * Notes on usage of selection-coding-system and * next-selection-coding-system on MS Windows: * * The selection coding system variables apply only to the version of * the clipboard data that is closest in type, i.e. when a 16-bit * Unicode coding system is given, they apply to he Unicode clipboard * (CF_UNICODETEXT), when a well-known console codepage is given, they * apply to the console version of the clipboard data (CF_OEMTEXT), * else they apply to the normal 8-bit text clipboard (CF_TEXT). * * When pasting (getting data from the OS), the clipboard format that * matches the {next-}selection-coding-system is retrieved. If * Unicode is requested, but not available, 8-bit text (CF_TEXT) is * used. In all other cases the OS will transparently convert * formats, so no other fallback is needed. * * When copying or cutting (sending data to the OS), the data is * announced and stored internally, but only actually rendered on * request. The requester determines the format provided. The * {next-}selection-coding-system is only used, when its corresponding * clipboard type matches the type requested. * * Scenarios to use the facilities for customizing the selection * coding system are: * * ;; Generally use KOI8-R instead of the russian MS codepage for * ;; the 8-bit clipboard. * (set-selection-coding-system 'koi8-r-dos) * * Or * * ;; Create a special clipboard copy function that uses codepage * ;; 1253 (Greek) to copy Greek text to a specific non-Unicode * ;; application. * (defun greek-copy (beg end) * (interactive "r") * (set-next-selection-coding-system 'cp1253-dos) * (copy-region-as-kill beg end)) * (global-set-key "\C-c\C-c" 'greek-copy) */ /* * Ideas for further directions: * * The encoding and decoding routines could be moved to Lisp code * similar to how xselect.c does it (using well-known routine names * for the delayed rendering). If the definition of which clipboard * types should be supported is also moved to Lisp, functionality * could be expanded to CF_HTML, CF_RTF and maybe other types. */ #include <config.h> #include "lisp.h" #include "w32term.h" /* for all of the w32 includes */ #include "w32heap.h" /* os_subtype */ #include "blockinput.h" #include "keyboard.h" /* cmd_error_internal() */ #include "charset.h" #include "coding.h" #include "character.h" #include "composite.h" static HGLOBAL convert_to_handle_as_ascii (void); static HGLOBAL convert_to_handle_as_coded (Lisp_Object coding_system); static Lisp_Object render (Lisp_Object oformat); static Lisp_Object render_locale (void); static Lisp_Object render_all (void); static void run_protected (Lisp_Object (*code) (), Lisp_Object arg); static Lisp_Object lisp_error_handler (Lisp_Object error); static LRESULT CALLBACK owner_callback (HWND win, UINT msg, WPARAM wp, LPARAM lp); static HWND create_owner (void); static void setup_config (void); static BOOL WINAPI enum_locale_callback (/*const*/ char* loc_string); static UINT cp_from_locale (LCID lcid, UINT format); static Lisp_Object coding_from_cp (UINT codepage); static Lisp_Object validate_coding_system (Lisp_Object coding_system); static void setup_windows_coding_system (Lisp_Object coding_system, struct coding_system * coding); /* A remnant from X11: Symbol for the CLIPBORD selection type. Other selections are not used on Windows, so we don't need symbols for PRIMARY and SECONDARY. */ Lisp_Object QCLIPBOARD; /* Coding system for communicating with other programs via the clipboard. */ static Lisp_Object Vselection_coding_system; /* Coding system for the next communication with other programs. */ static Lisp_Object Vnext_selection_coding_system; /* Internal pseudo-constants, initialized in globals_of_w32select() based on current system parameters. */ static LCID DEFAULT_LCID; static UINT ANSICP, OEMCP; static Lisp_Object QUNICODE, QANSICP, QOEMCP; /* A hidden window just for the clipboard management. */ static HWND clipboard_owner; /* A flag to tell WM_DESTROYCLIPBOARD who is to blame this time (just checking GetClipboardOwner() doesn't work, sadly). */ static int modifying_clipboard = 0; /* Configured transfer parameters, based on the last inspection of selection-coding-system. */ static Lisp_Object cfg_coding_system; static UINT cfg_codepage; static LCID cfg_lcid; static UINT cfg_clipboard_type; /* The current state for delayed rendering. */ static Lisp_Object current_text; static Lisp_Object current_coding_system; static int current_requires_encoding, current_num_nls; static UINT current_clipboard_type; static LCID current_lcid; #if TRACE #define ONTRACE(stmt) stmt #else #define ONTRACE(stmt) /*stmt*/ #endif /* This function assumes that there is no multibyte character in current_text, so we can short-cut encoding. */ static HGLOBAL convert_to_handle_as_ascii (void) { HGLOBAL htext = NULL; int nbytes; int truelen; unsigned char *src; unsigned char *dst; ONTRACE (fprintf (stderr, "convert_to_handle_as_ascii\n")); nbytes = SBYTES (current_text) + 1; src = SDATA (current_text); /* We need to add to the size the number of LF chars where we have to insert CR chars (the standard CF_TEXT clipboard format uses CRLF line endings, while Emacs uses just LF internally). */ truelen = nbytes + current_num_nls; if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, truelen)) == NULL) return NULL; if ((dst = (unsigned char *) GlobalLock (htext)) == NULL) { GlobalFree (htext); return NULL; } /* convert to CRLF line endings expected by clipboard */ while (1) { unsigned char *next; /* copy next line or remaining bytes including '\0' */ next = _memccpy (dst, src, '\n', nbytes); if (next) { /* copied one line ending with '\n' */ int copied = next - dst; nbytes -= copied; src += copied; /* insert '\r' before '\n' */ next[-1] = '\r'; next[0] = '\n'; dst = next + 1; } else /* copied remaining partial line -> now finished */ break; } GlobalUnlock (htext); return htext; } /* This function assumes that there are multibyte or NUL characters in current_text, or that we need to construct Unicode. It runs the text through the encoding machinery. */ static HGLOBAL convert_to_handle_as_coded (Lisp_Object coding_system) { HGLOBAL htext; unsigned char *dst = NULL; struct coding_system coding; ONTRACE (fprintf (stderr, "convert_to_handle_as_coded: %s\n", SDATA (SYMBOL_NAME (coding_system)))); setup_windows_coding_system (coding_system, &coding); coding.dst_bytes = SBYTES(current_text) * 2; coding.destination = (unsigned char *) xmalloc (coding.dst_bytes); encode_coding_object (&coding, current_text, 0, 0, SCHARS (current_text), SBYTES (current_text), Qnil); htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, coding.produced +2); if (htext != NULL) dst = (unsigned char *) GlobalLock (htext); if (dst != NULL) { memcpy (dst, coding.destination, coding.produced); /* Add the string terminator. Add two NULs in case we are producing Unicode here. */ dst[coding.produced] = dst[coding.produced+1] = '\0'; GlobalUnlock (htext); } xfree (coding.destination); return htext; } static Lisp_Object render (Lisp_Object oformat) { HGLOBAL htext = NULL; UINT format = XFASTINT (oformat); ONTRACE (fprintf (stderr, "render\n")); if (NILP (current_text)) return Qnil; if (current_requires_encoding || format == CF_UNICODETEXT) { if (format == current_clipboard_type) htext = convert_to_handle_as_coded (current_coding_system); else switch (format) { case CF_UNICODETEXT: htext = convert_to_handle_as_coded (QUNICODE); break; case CF_TEXT: case CF_OEMTEXT: { Lisp_Object cs; cs = coding_from_cp (cp_from_locale (current_lcid, format)); htext = convert_to_handle_as_coded (cs); break; } } } else htext = convert_to_handle_as_ascii (); ONTRACE (fprintf (stderr, "render: htext = 0x%08X\n", (unsigned) htext)); if (htext == NULL) return Qnil; if (SetClipboardData (format, htext) == NULL) { GlobalFree(htext); return Qnil; } return Qt; } static Lisp_Object render_locale (void) { HANDLE hlocale = NULL; LCID * lcid_ptr; ONTRACE (fprintf (stderr, "render_locale\n")); if (current_lcid == LOCALE_NEUTRAL || current_lcid == DEFAULT_LCID) return Qt; hlocale = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, sizeof (current_lcid)); if (hlocale == NULL) return Qnil; if ((lcid_ptr = (LCID *) GlobalLock (hlocale)) == NULL) { GlobalFree(hlocale); return Qnil; } *lcid_ptr = current_lcid; GlobalUnlock (hlocale); if (SetClipboardData (CF_LOCALE, hlocale) == NULL) { GlobalFree(hlocale); return Qnil; } return Qt; } /* At the end of the program, we want to ensure that our clipboard data survives us. This code will do that. */ static Lisp_Object render_all (void) { ONTRACE (fprintf (stderr, "render_all\n")); /* According to the docs we should not call OpenClipboard() here, but testing on W2K and working code in other projects shows that it is actually necessary. */ OpenClipboard (NULL); /* There is no usefull means to report errors here, there are none expected anyway, and even if there were errors, they wouldn't do any harm. So we just go ahead and do what has to be done without bothering with error handling. */ ++modifying_clipboard; EmptyClipboard (); --modifying_clipboard; /* For text formats that we don't render here, the OS can use its own translation rules instead, so we don't really need to offer everything. To minimize memory consumption we cover three possible situations based on our primary format as detected from selection-coding-system (see setup_config()): - Post CF_TEXT only. Let the OS convert to CF_OEMTEXT and the OS (on NT) or the application (on 9x/Me) convert to CF_UNICODETEXT. - Post CF_OEMTEXT only. Similar automatic conversions happen as for CF_TEXT. - Post CF_UNICODETEXT + CF_TEXT. 9x itself ignores CF_UNICODETEXT, even though some applications can still handle it. Note 1: We render the less capable CF_TEXT *before* the more capable CF_UNICODETEXT, to prevent clobbering through automatic conversions, just in case. Note 2: We could check os_subtype here and only render the additional CF_TEXT on 9x/Me. But OTOH with current_clipboard_type == CF_UNICODETEXT we don't involve the automatic conversions anywhere else, so to get consistent results, we probably don't want to rely on it here either. */ render_locale(); if (current_clipboard_type == CF_UNICODETEXT) render (make_number (CF_TEXT)); render (make_number (current_clipboard_type)); CloseClipboard (); return Qnil; } static void run_protected (Lisp_Object (*code) (), Lisp_Object arg) { /* FIXME: This works but it doesn't feel right. Too much fiddling with global variables and calling strange looking functions. Is this really the right way to run Lisp callbacks? */ extern int waiting_for_input; int owfi; BLOCK_INPUT; /* Fsignal calls abort() if it sees that waiting_for_input is set. */ owfi = waiting_for_input; waiting_for_input = 0; internal_condition_case_1 (code, arg, Qt, lisp_error_handler); waiting_for_input = owfi; UNBLOCK_INPUT; } static Lisp_Object lisp_error_handler (Lisp_Object error) { Vsignaling_function = Qnil; cmd_error_internal (error, "Error in delayed clipboard rendering: "); Vinhibit_quit = Qt; return Qt; } static LRESULT CALLBACK owner_callback (HWND win, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_RENDERFORMAT: ONTRACE (fprintf (stderr, "WM_RENDERFORMAT\n")); run_protected (render, make_number (wp)); return 0; case WM_RENDERALLFORMATS: ONTRACE (fprintf (stderr, "WM_RENDERALLFORMATS\n")); run_protected (render_all, Qnil); return 0; case WM_DESTROYCLIPBOARD: if (!modifying_clipboard) { ONTRACE (fprintf (stderr, "WM_DESTROYCLIPBOARD (other)\n")); current_text = Qnil; current_coding_system = Qnil; } else { ONTRACE (fprintf (stderr, "WM_DESTROYCLIPBOARD (self)\n")); } return 0; case WM_DESTROY: if (win == clipboard_owner) clipboard_owner = NULL; break; } return DefWindowProc (win, msg, wp, lp); } static HWND create_owner (void) { static const char CLASSNAME[] = "Emacs Clipboard"; WNDCLASS wc; memset (&wc, 0, sizeof (wc)); wc.lpszClassName = CLASSNAME; wc.lpfnWndProc = owner_callback; RegisterClass (&wc); return CreateWindow (CLASSNAME, CLASSNAME, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL); } /* Called on exit by term_ntproc() in w32.c */ void term_w32select (void) { /* This is needed to trigger WM_RENDERALLFORMATS. */ if (clipboard_owner != NULL) DestroyWindow (clipboard_owner); } static void setup_config (void) { const char *coding_name; const char *cp; char *end; int slen; Lisp_Object coding_system; Lisp_Object dos_coding_system; CHECK_SYMBOL (Vselection_coding_system); coding_system = NILP (Vnext_selection_coding_system) ? Vselection_coding_system : Vnext_selection_coding_system; dos_coding_system = validate_coding_system (coding_system); if (NILP (dos_coding_system)) Fsignal (Qerror, list2 (build_string ("Coding system is invalid or doesn't have " "an eol variant for dos line ends"), coding_system)); /* Check if we have it cached */ if (!NILP (cfg_coding_system) && EQ (cfg_coding_system, dos_coding_system)) return; cfg_coding_system = dos_coding_system; /* Set some sensible fallbacks */ cfg_codepage = ANSICP; cfg_lcid = LOCALE_NEUTRAL; cfg_clipboard_type = CF_TEXT; /* Interpret the coding system symbol name */ coding_name = SDATA (SYMBOL_NAME (cfg_coding_system)); /* "(.*-)?utf-16.*" -> CF_UNICODETEXT */ cp = strstr (coding_name, "utf-16"); if (cp != NULL && (cp == coding_name || cp[-1] == '-')) { cfg_clipboard_type = CF_UNICODETEXT; return; } /* "cp[0-9]+.*" or "windows-[0-9]+.*" -> CF_TEXT or CF_OEMTEXT */ slen = strlen (coding_name); if (slen >= 4 && coding_name[0] == 'c' && coding_name[1] == 'p') cp = coding_name + 2; else if (slen >= 10 && memcmp (coding_name, "windows-", 8) == 0) cp = coding_name + 8; else return; end = (char*)cp; cfg_codepage = strtol (cp, &end, 10); /* Error return from strtol() or number of digits < 2 -> Restore the default and drop it. */ if (cfg_codepage == 0 || (end-cp) < 2 ) { cfg_codepage = ANSICP; return; } /* Is it the currently active system default? */ if (cfg_codepage == ANSICP) { /* cfg_clipboard_type = CF_TEXT; */ return; } if (cfg_codepage == OEMCP) { cfg_clipboard_type = CF_OEMTEXT; return; } /* Else determine a suitable locale the hard way. */ EnumSystemLocales (enum_locale_callback, LCID_INSTALLED); } static BOOL WINAPI enum_locale_callback (/*const*/ char* loc_string) { LCID lcid; UINT codepage; lcid = strtoul (loc_string, NULL, 16); /* Is the wanted codepage the "ANSI" codepage for this locale? */ codepage = cp_from_locale (lcid, CF_TEXT); if (codepage == cfg_codepage) { cfg_lcid = lcid; cfg_clipboard_type = CF_TEXT; return FALSE; /* Stop enumeration */ } /* Is the wanted codepage the OEM codepage for this locale? */ codepage = cp_from_locale (lcid, CF_OEMTEXT); if (codepage == cfg_codepage) { cfg_lcid = lcid; cfg_clipboard_type = CF_OEMTEXT; return FALSE; /* Stop enumeration */ } return TRUE; /* Continue enumeration */ } static UINT cp_from_locale (LCID lcid, UINT format) { char buffer[20] = ""; UINT variant, cp; variant = format == CF_TEXT ? LOCALE_IDEFAULTANSICODEPAGE : LOCALE_IDEFAULTCODEPAGE; GetLocaleInfo (lcid, variant, buffer, sizeof (buffer)); cp = strtoul (buffer, NULL, 10); if (cp == CP_ACP) return ANSICP; else if (cp == CP_OEMCP) return OEMCP; else return cp; } static Lisp_Object coding_from_cp (UINT codepage) { char buffer[30]; sprintf (buffer, "cp%d-dos", (int) codepage); return intern (buffer); /* We don't need to check that this coding system actually exists right here, because that is done later for all coding systems used, regardless of where they originate. */ } static Lisp_Object validate_coding_system (Lisp_Object coding_system) { Lisp_Object eol_type; /* Make sure the input is valid. */ if (NILP (Fcoding_system_p (coding_system))) return Qnil; /* Make sure we use a DOS coding system as mandated by the system specs. */ eol_type = Fcoding_system_eol_type (coding_system); /* Already a DOS coding system? */ if (EQ (eol_type, make_number (1))) return coding_system; /* Get EOL_TYPE vector of the base of CODING_SYSTEM. */ if (!VECTORP (eol_type)) { eol_type = Fcoding_system_eol_type (Fcoding_system_base (coding_system)); if (!VECTORP (eol_type)) return Qnil; } return AREF (eol_type, 1); } static void setup_windows_coding_system (Lisp_Object coding_system, struct coding_system * coding) { memset (coding, 0, sizeof (*coding)); setup_coding_system (coding_system, coding); /* Unset CODING_ANNOTATE_COMPOSITION_MASK. Previous code had comments about crashes in encode_coding_iso2022 trying to dereference a null pointer when composition was on. Selection data should not contain any composition sequence on Windows. CODING_ANNOTATION_MASK also includes CODING_ANNOTATE_DIRECTION_MASK and CODING_ANNOTATE_CHARSET_MASK, which both apply to ISO6429 only. We don't know if these really need to be unset on Windows, but it probably doesn't hurt either. */ coding->mode &= ~CODING_ANNOTATION_MASK; coding->mode |= CODING_MODE_LAST_BLOCK | CODING_MODE_SAFE_ENCODING; } DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data, Sw32_set_clipboard_data, 1, 2, 0, doc: /* This sets the clipboard data to the given text. */) (string, ignored) Lisp_Object string, ignored; { BOOL ok = TRUE; int nbytes; unsigned char *src; unsigned char *dst; unsigned char *end; /* This parameter used to be the current frame, but we don't use that any more. */ (void) ignored; CHECK_STRING (string); setup_config (); current_text = string; current_coding_system = cfg_coding_system; current_clipboard_type = cfg_clipboard_type; current_lcid = cfg_lcid; current_num_nls = 0; current_requires_encoding = 0; BLOCK_INPUT; /* Check for non-ASCII characters. While we are at it, count the number of LFs, so we know how many CRs we will have to add later (just in the case where we can use our internal ASCII rendering, see code and comment in convert_to_handle_as_ascii() above). */ nbytes = SBYTES (string); src = SDATA (string); for (dst = src, end = src+nbytes; dst < end; dst++) { if (*dst == '\n') current_num_nls++; else if (*dst >= 0x80 || *dst == 0) { current_requires_encoding = 1; break; } } if (!current_requires_encoding) { /* If all we have is ASCII we don't need to pretend we offer anything fancy. */ current_coding_system = Qraw_text; current_clipboard_type = CF_TEXT; current_lcid = LOCALE_NEUTRAL; } if (!OpenClipboard (clipboard_owner)) goto error; ++modifying_clipboard; ok = EmptyClipboard (); --modifying_clipboard; /* If we have something non-ASCII we may want to set a locale. We do that directly (non-delayed), as it's just a small bit. */ if (ok) ok = !NILP(render_locale()); if (ok) { if (clipboard_owner == NULL) { /* If for some reason we don't have a clipboard_owner, we just set the text format as chosen by the configuration and than forget about the whole thing. */ ok = !NILP(render (make_number (current_clipboard_type))); current_text = Qnil; current_coding_system = Qnil; } else { /* Advertise all supported formats so that whatever the requester chooses, only one encoding step needs to be made. This is intentionally different from what we do in the handler for WM_RENDERALLFORMATS. */ SetClipboardData (CF_UNICODETEXT, NULL); SetClipboardData (CF_TEXT, NULL); SetClipboardData (CF_OEMTEXT, NULL); } } CloseClipboard (); /* With delayed rendering we haven't really "used" this coding system yet, and it's even unclear if we ever will. But this is a way to tell the upper level what we *would* use under ideal circumstances. We don't signal the actually used coding-system later when we finally render, because that can happen at any time and we don't want to disturb the "foreground" action. */ if (ok) Vlast_coding_system_used = current_coding_system; Vnext_selection_coding_system = Qnil; if (ok) goto done; error: ok = FALSE; current_text = Qnil; current_coding_system = Qnil; done: UNBLOCK_INPUT; return (ok ? string : Qnil); } DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data, Sw32_get_clipboard_data, 0, 1, 0, doc: /* This gets the clipboard data in text format. */) (ignored) Lisp_Object ignored; { HGLOBAL htext; Lisp_Object ret = Qnil; UINT actual_clipboard_type; int use_configured_coding_system = 1; /* This parameter used to be the current frame, but we don't use that any more. */ (void) ignored; /* Don't pass our own text from the clipboard (which might be troublesome if the killed text includes null characters). */ if (!NILP (current_text)) return ret; setup_config (); actual_clipboard_type = cfg_clipboard_type; BLOCK_INPUT; if (!OpenClipboard (clipboard_owner)) goto done; if ((htext = GetClipboardData (actual_clipboard_type)) == NULL) { /* If we want CF_UNICODETEXT but can't get it, the current coding system is useless. OTOH we can still try and decode CF_TEXT based on the locale that the system gives us and that we get down below. */ if (actual_clipboard_type == CF_UNICODETEXT) { htext = GetClipboardData (CF_TEXT); if (htext != NULL) { actual_clipboard_type = CF_TEXT; use_configured_coding_system = 0; } } } if (htext == NULL) goto closeclip; { unsigned char *src; unsigned char *dst; int nbytes; int truelen; int require_decoding = 0; if ((src = (unsigned char *) GlobalLock (htext)) == NULL) goto closeclip; /* If the clipboard data contains any non-ascii code, we need to decode it with a coding system. */ if (actual_clipboard_type == CF_UNICODETEXT) { nbytes = lstrlenW ((WCHAR *)src) * 2; require_decoding = 1; } else { int i; nbytes = strlen (src); for (i = 0; i < nbytes; i++) { if (src[i] >= 0x80) { require_decoding = 1; break; } } } if (require_decoding) { struct coding_system coding; Lisp_Object coding_system = Qnil; Lisp_Object dos_coding_system; /* `next-selection-coding-system' should override everything, even when the locale passed by the system disagrees. The only exception is when `next-selection-coding-system' requested CF_UNICODETEXT and we couldn't get that. */ if (use_configured_coding_system && !NILP (Vnext_selection_coding_system)) coding_system = Vnext_selection_coding_system; /* If we have CF_TEXT or CF_OEMTEXT, we want to check out CF_LOCALE, too. */ else if (actual_clipboard_type != CF_UNICODETEXT) { HGLOBAL hlocale; LCID lcid = DEFAULT_LCID; UINT cp; /* Documentation says that the OS always generates CF_LOCALE info automatically, so the locale handle should always be present. Fact is that this is not always true on 9x ;-(. */ hlocale = GetClipboardData (CF_LOCALE); if (hlocale != NULL) { const LCID * lcid_ptr; lcid_ptr = (const LCID *) GlobalLock (hlocale); if (lcid_ptr != NULL) { lcid = *lcid_ptr; GlobalUnlock (hlocale); } /* 9x has garbage as the sort order (to be exact there is another instance of the language id in the upper word). We don't care about sort order anyway, so we just filter out the unneeded mis-information to avoid irritations. */ lcid = MAKELCID (LANGIDFROMLCID (lcid), SORT_DEFAULT); } /* If we are using fallback from CF_UNICODETEXT, we can't use the configured coding system. Also we don't want to use it, if the system has supplied us with a locale and it is not just the system default. */ if (!use_configured_coding_system || lcid != DEFAULT_LCID) { cp = cp_from_locale (lcid, actual_clipboard_type); /* If it's just our current standard setting anyway, use the coding system that the user has selected. Otherwise create a new spec to match the locale that was specified by the other side or the system. */ if (!use_configured_coding_system || cp != cfg_codepage) coding_system = coding_from_cp (cp); } } if (NILP (coding_system)) coding_system = Vselection_coding_system; Vnext_selection_coding_system = Qnil; dos_coding_system = validate_coding_system (coding_system); if (!NILP (dos_coding_system)) { setup_windows_coding_system (dos_coding_system, &coding); coding.source = src; decode_coding_object (&coding, Qnil, 0, 0, nbytes, nbytes, Qt); ret = coding.dst_object; Vlast_coding_system_used = CODING_ID_NAME (coding.id); } } else { /* FIXME: We may want to repeat the code in this branch for the Unicode case. */ /* Need to know final size after CR chars are removed because we can't change the string size manually, and doing an extra copy is silly. We only remove CR when it appears as part of CRLF. */ truelen = nbytes; dst = src; /* avoid using strchr because it recomputes the length everytime */ while ((dst = memchr (dst, '\r', nbytes - (dst - src))) != NULL) { if (dst[1] == '\n') /* safe because of trailing '\0' */ truelen--; dst++; } ret = make_uninit_string (truelen); /* Convert CRLF line endings (the standard CF_TEXT clipboard format) to LF endings as used internally by Emacs. */ dst = SDATA (ret); while (1) { unsigned char *next; /* copy next line or remaining bytes excluding '\0' */ next = _memccpy (dst, src, '\r', nbytes); if (next) { /* copied one line ending with '\r' */ int copied = next - dst; nbytes -= copied; dst += copied; src += copied; if (*src == '\n') dst--; /* overwrite '\r' with '\n' */ } else /* copied remaining partial line -> now finished */ break; } Vlast_coding_system_used = Qraw_text; } GlobalUnlock (htext); } closeclip: CloseClipboard (); done: UNBLOCK_INPUT; return (ret); } /* Support checking for a clipboard selection. */ DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, 0, 1, 0, doc: /* Whether there is an owner for the given X Selection. The arg should be the name of the selection in question, typically one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. \(Those are literal upper-case symbol names, since that's what X expects.) For convenience, the symbol nil is the same as `PRIMARY', and t is the same as `SECONDARY'. */) (selection) Lisp_Object selection; { CHECK_SYMBOL (selection); /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check if the clipboard currently has valid text format contents. */ if (EQ (selection, QCLIPBOARD)) { Lisp_Object val = Qnil; setup_config (); if (OpenClipboard (NULL)) { UINT format = 0; while ((format = EnumClipboardFormats (format))) /* Check CF_TEXT in addition to cfg_clipboard_type, because we can fall back on that if CF_UNICODETEXT is not available. Actually a check for CF_TEXT only should be enough. */ if (format == cfg_clipboard_type || format == CF_TEXT) { val = Qt; break; } CloseClipboard (); } return val; } return Qnil; } /* One-time init. Called in the un-dumped Emacs, but not in the dumped version. */ void syms_of_w32select () { defsubr (&Sw32_set_clipboard_data); defsubr (&Sw32_get_clipboard_data); defsubr (&Sx_selection_exists_p); DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system, doc: /* Coding system for communicating with other programs. When sending or receiving text via cut_buffer, selection, and clipboard, the text is encoded or decoded by this coding system. The default value is the current system default encoding on 9x/Me and `utf-16le-dos' (Unicode) on NT/W2K/XP. */); /* The actual value is set dynamically in the dumped Emacs, see below. */ Vselection_coding_system = Qnil; DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system, doc: /* Coding system for the next communication with other programs. Usually, `selection-coding-system' is used for communicating with other programs. But, if this variable is set, it is used for the next communication only. After the communication, this variable is set to nil. */); Vnext_selection_coding_system = Qnil; DEFSYM (QCLIPBOARD, "CLIPBOARD"); cfg_coding_system = Qnil; staticpro (&cfg_coding_system); current_text = Qnil; staticpro (¤t_text); current_coding_system = Qnil; staticpro (¤t_coding_system); DEFSYM (QUNICODE, "utf-16le-dos"); QANSICP = Qnil; staticpro (&QANSICP); QOEMCP = Qnil; staticpro (&QOEMCP); } /* One-time init. Called in the dumped Emacs, but not in the un-dumped version. */ void globals_of_w32select () { DEFAULT_LCID = GetUserDefaultLCID (); /* Drop the sort order from the LCID, so we can compare this with CF_LOCALE objects that have the same fix on 9x. */ DEFAULT_LCID = MAKELCID (LANGIDFROMLCID (DEFAULT_LCID), SORT_DEFAULT); ANSICP = GetACP (); OEMCP = GetOEMCP (); QANSICP = coding_from_cp (ANSICP); QOEMCP = coding_from_cp (OEMCP); if (os_subtype == OS_NT) Vselection_coding_system = QUNICODE; else if (inhibit_window_system) Vselection_coding_system = QOEMCP; else Vselection_coding_system = QANSICP; clipboard_owner = create_owner (); } /* arch-tag: c96e9724-5eb1-4dad-be07-289f092fd2af (do not change this comment) */