Mercurial > hg > xemacs-beta
annotate lisp/subr.el @ 4952:19a72041c5ed
Mule-izing, various fixes related to char * arguments
-------------------- ChangeLog entries follow: --------------------
modules/ChangeLog addition:
2010-01-26 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c:
* postgresql/postgresql.c (CHECK_LIVE_CONNECTION):
* postgresql/postgresql.c (print_pgresult):
* postgresql/postgresql.c (Fpq_conn_defaults):
* postgresql/postgresql.c (Fpq_connectdb):
* postgresql/postgresql.c (Fpq_connect_start):
* postgresql/postgresql.c (Fpq_result_status):
* postgresql/postgresql.c (Fpq_res_status):
Mule-ize large parts of it.
2010-01-26 Ben Wing <ben@xemacs.org>
* ldap/eldap.c (print_ldap):
* ldap/eldap.c (allocate_ldap):
Use write_ascstring().
src/ChangeLog addition:
2010-01-26 Ben Wing <ben@xemacs.org>
* alloc.c:
* alloc.c (build_ascstring):
* alloc.c (build_msg_cistring):
* alloc.c (staticpro_1):
* alloc.c (staticpro_name):
* alloc.c (staticpro_nodump_1):
* alloc.c (staticpro_nodump_name):
* alloc.c (unstaticpro_nodump_1):
* alloc.c (mcpro_1):
* alloc.c (mcpro_name):
* alloc.c (object_memory_usage_stats):
* alloc.c (common_init_alloc_early):
* alloc.c (init_alloc_once_early):
* buffer.c (print_buffer):
* buffer.c (vars_of_buffer):
* buffer.c (common_init_complex_vars_of_buffer):
* buffer.c (init_initial_directory):
* bytecode.c (invalid_byte_code):
* bytecode.c (print_compiled_function):
* bytecode.c (mark_compiled_function):
* chartab.c (print_table_entry):
* chartab.c (print_char_table):
* config.h.in:
* console-gtk.c:
* console-gtk.c (gtk_device_to_console_connection):
* console-gtk.c (gtk_semi_canonicalize_console_connection):
* console-gtk.c (gtk_canonicalize_console_connection):
* console-gtk.c (gtk_semi_canonicalize_device_connection):
* console-gtk.c (gtk_canonicalize_device_connection):
* console-stream.c (stream_init_frame_1):
* console-stream.c (vars_of_console_stream):
* console-stream.c (init_console_stream):
* console-x.c (x_semi_canonicalize_console_connection):
* console-x.c (x_semi_canonicalize_device_connection):
* console-x.c (x_canonicalize_device_connection):
* console-x.h:
* data.c (eq_with_ebola_notice):
* data.c (Fsubr_interactive):
* data.c (Fnumber_to_string):
* data.c (digit_to_number):
* device-gtk.c (gtk_init_device):
* device-msw.c (print_devmode):
* device-x.c (x_event_name):
* dialog-msw.c (handle_directory_dialog_box):
* dialog-msw.c (handle_file_dialog_box):
* dialog-msw.c (vars_of_dialog_mswindows):
* doc.c (weird_doc):
* doc.c (Fsnarf_documentation):
* doc.c (vars_of_doc):
* dumper.c (pdump):
* dynarr.c:
* dynarr.c (Dynarr_realloc):
* editfns.c (Fuser_real_login_name):
* editfns.c (get_home_directory):
* elhash.c (print_hash_table_data):
* elhash.c (print_hash_table):
* emacs.c (main_1):
* emacs.c (vars_of_emacs):
* emodules.c:
* emodules.c (_emodules_list):
* emodules.c (Fload_module):
* emodules.c (Funload_module):
* emodules.c (Flist_modules):
* emodules.c (find_make_module):
* emodules.c (attempt_module_delete):
* emodules.c (emodules_load):
* emodules.c (emodules_doc_subr):
* emodules.c (emodules_doc_sym):
* emodules.c (syms_of_module):
* emodules.c (vars_of_module):
* emodules.h:
* eval.c (print_subr):
* eval.c (signal_call_debugger):
* eval.c (build_error_data):
* eval.c (signal_error):
* eval.c (maybe_signal_error):
* eval.c (signal_continuable_error):
* eval.c (maybe_signal_continuable_error):
* eval.c (signal_error_2):
* eval.c (maybe_signal_error_2):
* eval.c (signal_continuable_error_2):
* eval.c (maybe_signal_continuable_error_2):
* eval.c (signal_ferror):
* eval.c (maybe_signal_ferror):
* eval.c (signal_continuable_ferror):
* eval.c (maybe_signal_continuable_ferror):
* eval.c (signal_ferror_with_frob):
* eval.c (maybe_signal_ferror_with_frob):
* eval.c (signal_continuable_ferror_with_frob):
* eval.c (maybe_signal_continuable_ferror_with_frob):
* eval.c (syntax_error):
* eval.c (syntax_error_2):
* eval.c (maybe_syntax_error):
* eval.c (sferror):
* eval.c (sferror_2):
* eval.c (maybe_sferror):
* eval.c (invalid_argument):
* eval.c (invalid_argument_2):
* eval.c (maybe_invalid_argument):
* eval.c (invalid_constant):
* eval.c (invalid_constant_2):
* eval.c (maybe_invalid_constant):
* eval.c (invalid_operation):
* eval.c (invalid_operation_2):
* eval.c (maybe_invalid_operation):
* eval.c (invalid_change):
* eval.c (invalid_change_2):
* eval.c (maybe_invalid_change):
* eval.c (invalid_state):
* eval.c (invalid_state_2):
* eval.c (maybe_invalid_state):
* eval.c (wtaerror):
* eval.c (stack_overflow):
* eval.c (out_of_memory):
* eval.c (print_multiple_value):
* eval.c (issue_call_trapping_problems_warning):
* eval.c (backtrace_specials):
* eval.c (backtrace_unevalled_args):
* eval.c (Fbacktrace):
* eval.c (warn_when_safe):
* event-Xt.c (modwarn):
* event-Xt.c (modbarf):
* event-Xt.c (check_modifier):
* event-Xt.c (store_modifier):
* event-Xt.c (emacs_Xt_format_magic_event):
* event-Xt.c (describe_event):
* event-gtk.c (dragndrop_data_received):
* event-gtk.c (store_modifier):
* event-gtk.c (gtk_reset_modifier_mapping):
* event-msw.c (dde_eval_string):
* event-msw.c (Fdde_alloc_advise_item):
* event-msw.c (mswindows_dde_callback):
* event-msw.c (FROB):
* event-msw.c (emacs_mswindows_format_magic_event):
* event-stream.c (external_debugging_print_event):
* event-stream.c (execute_help_form):
* event-stream.c (vars_of_event_stream):
* events.c (print_event_1):
* events.c (print_event):
* events.c (event_equal):
* extents.c (print_extent_1):
* extents.c (print_extent):
* extents.c (vars_of_extents):
* faces.c (print_face):
* faces.c (complex_vars_of_faces):
* file-coding.c:
* file-coding.c (print_coding_system):
* file-coding.c (print_coding_system_in_print_method):
* file-coding.c (default_query_method):
* file-coding.c (find_coding_system):
* file-coding.c (make_coding_system_1):
* file-coding.c (chain_print):
* file-coding.c (undecided_print):
* file-coding.c (gzip_print):
* file-coding.c (vars_of_file_coding):
* file-coding.c (complex_vars_of_file_coding):
* fileio.c:
* fileio.c (report_file_type_error):
* fileio.c (report_error_with_errno):
* fileio.c (report_file_error):
* fileio.c (barf_or_query_if_file_exists):
* fileio.c (vars_of_fileio):
* floatfns.c (matherr):
* fns.c (print_bit_vector):
* fns.c (Fmapconcat):
* fns.c (add_suffix_to_symbol):
* fns.c (add_prefix_to_symbol):
* frame-gtk.c:
* frame-gtk.c (Fgtk_window_id):
* frame-x.c (def):
* frame-x.c (x_cde_transfer_callback):
* frame.c:
* frame.c (Fmake_frame):
* gc.c (show_gc_cursor_and_message):
* gc.c (vars_of_gc):
* glyphs-eimage.c (png_instantiate):
* glyphs-eimage.c (tiff_instantiate):
* glyphs-gtk.c (gtk_print_image_instance):
* glyphs-msw.c (mswindows_print_image_instance):
* glyphs-x.c (x_print_image_instance):
* glyphs-x.c (update_widget_face):
* glyphs.c (make_string_from_file):
* glyphs.c (print_image_instance):
* glyphs.c (signal_image_error):
* glyphs.c (signal_image_error_2):
* glyphs.c (signal_double_image_error):
* glyphs.c (signal_double_image_error_2):
* glyphs.c (xbm_mask_file_munging):
* glyphs.c (pixmap_to_lisp_data):
* glyphs.h:
* gui.c (gui_item_display_flush_left):
* hpplay.c (player_error_internal):
* hpplay.c (myHandler):
* intl-win32.c:
* intl-win32.c (langcode_to_lang):
* intl-win32.c (sublangcode_to_lang):
* intl-win32.c (Fmswindows_get_locale_info):
* intl-win32.c (lcid_to_locale_mule_or_no):
* intl-win32.c (mswindows_multibyte_to_unicode_print):
* intl-win32.c (complex_vars_of_intl_win32):
* keymap.c:
* keymap.c (print_keymap):
* keymap.c (ensure_meta_prefix_char_keymapp):
* keymap.c (Fkey_description):
* keymap.c (Ftext_char_description):
* lisp.h:
* lisp.h (struct):
* lisp.h (DECLARE_INLINE_HEADER):
* lread.c (Fload_internal):
* lread.c (locate_file):
* lread.c (read_escape):
* lread.c (read_raw_string):
* lread.c (read1):
* lread.c (read_list):
* lread.c (read_compiled_function):
* lread.c (init_lread):
* lrecord.h:
* marker.c (print_marker):
* marker.c (marker_equal):
* menubar-msw.c (displayable_menu_item):
* menubar-x.c (command_builder_operate_menu_accelerator):
* menubar.c (vars_of_menubar):
* minibuf.c (reinit_complex_vars_of_minibuf):
* minibuf.c (complex_vars_of_minibuf):
* mule-charset.c (Fmake_charset):
* mule-charset.c (complex_vars_of_mule_charset):
* mule-coding.c (iso2022_print):
* mule-coding.c (fixed_width_query):
* number.c (bignum_print):
* number.c (ratio_print):
* number.c (bigfloat_print):
* number.c (bigfloat_finalize):
* objects-msw.c:
* objects-msw.c (mswindows_color_to_string):
* objects-msw.c (mswindows_color_list):
* objects-tty.c:
* objects-tty.c (tty_font_list):
* objects-tty.c (tty_find_charset_font):
* objects-xlike-inc.c (xft_find_charset_font):
* objects-xlike-inc.c (endif):
* print.c:
* print.c (write_istring):
* print.c (write_ascstring):
* print.c (Fterpri):
* print.c (Fprint):
* print.c (print_error_message):
* print.c (print_vector_internal):
* print.c (print_cons):
* print.c (print_string):
* print.c (printing_unreadable_object):
* print.c (print_internal):
* print.c (print_float):
* print.c (print_symbol):
* process-nt.c (mswindows_report_winsock_error):
* process-nt.c (nt_canonicalize_host_name):
* process-unix.c (unix_canonicalize_host_name):
* process.c (print_process):
* process.c (report_process_error):
* process.c (report_network_error):
* process.c (make_process_internal):
* process.c (Fstart_process_internal):
* process.c (status_message):
* process.c (putenv_internal):
* process.c (vars_of_process):
* process.h:
* profile.c (vars_of_profile):
* rangetab.c (print_range_table):
* realpath.c (vars_of_realpath):
* redisplay.c (vars_of_redisplay):
* search.c (wordify):
* search.c (Freplace_match):
* sheap.c (sheap_adjust_h):
* sound.c (report_sound_error):
* sound.c (Fplay_sound_file):
* specifier.c (print_specifier):
* symbols.c (Fsubr_name):
* symbols.c (do_symval_forwarding):
* symbols.c (set_default_buffer_slot_variable):
* symbols.c (set_default_console_slot_variable):
* symbols.c (store_symval_forwarding):
* symbols.c (default_value):
* symbols.c (defsymbol_massage_name_1):
* symbols.c (defsymbol_massage_name_nodump):
* symbols.c (defsymbol_massage_name):
* symbols.c (defsymbol_massage_multiword_predicate_nodump):
* symbols.c (defsymbol_massage_multiword_predicate):
* symbols.c (defsymbol_nodump):
* symbols.c (defsymbol):
* symbols.c (defkeyword):
* symbols.c (defkeyword_massage_name):
* symbols.c (check_module_subr):
* symbols.c (deferror_1):
* symbols.c (deferror):
* symbols.c (deferror_massage_name):
* symbols.c (deferror_massage_name_and_message):
* symbols.c (defvar_magic):
* symeval.h:
* symeval.h (DEFVAR_SYMVAL_FWD):
* sysdep.c:
* sysdep.c (init_system_name):
* sysdll.c:
* sysdll.c (MAYBE_PREPEND_UNDERSCORE):
* sysdll.c (dll_function):
* sysdll.c (dll_variable):
* sysdll.c (dll_error):
* sysdll.c (dll_open):
* sysdll.c (dll_close):
* sysdll.c (image_for_address):
* sysdll.c (my_find_image):
* sysdll.c (search_linked_libs):
* sysdll.h:
* sysfile.h:
* sysfile.h (DEFAULT_DIRECTORY_FALLBACK):
* syswindows.h:
* tests.c (DFC_CHECK_LENGTH):
* tests.c (DFC_CHECK_CONTENT):
* tests.c (Ftest_hash_tables):
* text.c (vars_of_text):
* text.h:
* tooltalk.c (tt_opnum_string):
* tooltalk.c (tt_message_arg_ival_string):
* tooltalk.c (Ftooltalk_default_procid):
* tooltalk.c (Ftooltalk_default_session):
* tooltalk.c (init_tooltalk):
* tooltalk.c (vars_of_tooltalk):
* ui-gtk.c (Fdll_load):
* ui-gtk.c (type_to_marshaller_type):
* ui-gtk.c (Fgtk_import_function_internal):
* ui-gtk.c (emacs_gtk_object_printer):
* ui-gtk.c (emacs_gtk_boxed_printer):
* unicode.c (unicode_to_ichar):
* unicode.c (unicode_print):
* unicode.c (unicode_query):
* unicode.c (vars_of_unicode):
* unicode.c (complex_vars_of_unicode):
* win32.c:
* win32.c (mswindows_report_process_error):
* window.c (print_window):
* xemacs.def.in.in:
BASIC IDEA: Further fixing up uses of char * and CIbyte *
to reflect their actual semantics; Mule-izing some code;
redoing of the not-yet-working code to handle message translation.
Clean up code to handle message-translation (not yet working).
Create separate versions of build_msg_string() for working with
Ibyte *, CIbyte *, and Ascbyte * arguments. Assert that Ascbyte *
arguments are pure-ASCII. Make build_msg_string() be the same
as build_msg_ascstring(). Create same three versions of GETTEXT()
and DEFER_GETTEXT(). Also create build_defer_string() and
variants for the equivalent of DEFER_GETTEXT() when building a
string. Remove old CGETTEXT(). Clean up code where GETTEXT(),
DEFER_GETTEXT(), build_msg_string(), etc. was being called and
introduce some new calls to build_msg_string(), etc. Remove
GETTEXT() from calls to weird_doc() -- we assume that the
message snarfer knows about weird_doc(). Remove uses of
DEFER_GETTEXT() from error messages in sysdep.c and instead use
special comments /* @@@begin-snarf@@@ */ and /* @@@end-snarf@@@ */
that the message snarfer presumably knows about.
Create build_ascstring() and use it in many instances in place
of build_string(). The purpose of having Ascbyte * variants is
to make the code more self-documenting in terms of what sort of
semantics is expected for char * strings. In fact in the process
of looking for uses of build_string(), much improperly Mule-ized
was discovered.
Mule-ize a lot of code as described in previous paragraph,
e.g. in sysdep.c.
Make the error functions take Ascbyte * strings and fix up a
couple of places where non-pure-ASCII strings were being passed in
(file-coding.c, mule-coding.c, unicode.c). (It's debatable whether
we really need to make the error functions work this way. It
helps catch places where code is written in a way that message
translation won't work, but we may well never implement message
translation.)
Make staticpro() and friends take Ascbyte * strings instead of
raw char * strings. Create a const_Ascbyte_ptr dynarr type
to describe what's held by staticpro_names[] and friends,
create pdump descriptions for const_Ascbyte_ptr dynarrs, and
use them in place of specially-crafted staticpro descriptions.
Mule-ize certain other functions (e.g. x_event_name) by correcting
raw use of char * to Ascbyte *, Rawbyte * or another such type,
and raw use of char[] buffers to another type (usually Ascbyte[]).
Change many uses of write_c_string() to write_msg_string(),
write_ascstring(), etc.
Mule-ize emodules.c, emodules.h, sysdll.h.
Fix some un-Mule-ized code in intl-win32.c.
A comment in event-Xt.c and the limitations of the message
snarfer (make-msgfile or whatever) is presumably incorrect --
it should be smart enough to handle function calls spread over
more than one line. Clean up code in event-Xt.c that was
written awkwardly for this reason.
In config.h.in, instead of NEED_ERROR_CHECK_TYPES_INLINES,
create a more general XEMACS_DEFS_NEEDS_INLINE_DECLS to
indicate when inlined functions need to be declared in
xemacs.defs.in.in, and make use of it in xemacs.defs.in.in.
We need to do this because postgresql.c now calls qxestrdup(),
which is an inline function.
Make nconc2() and other such functions MODULE_API and put
them in xemacs.defs.in.in since postgresql.c now uses them.
Clean up indentation in lread.c and a few other places.
In text.h, document ASSERT_ASCTEXT_ASCII() and
ASSERT_ASCTEXT_ASCII_LEN(), group together the stand-in
encodings and add some more for DLL symbols, function and
variable names, etc.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Tue, 26 Jan 2010 23:22:30 -0600 |
parents | 0142cb4d1049 |
children | 755ae5b97edb |
rev | line source |
---|---|
428 | 1 ;;; subr.el --- basic lisp subroutines for XEmacs |
2 | |
2525 | 3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 2003 |
4 ;; Free Software Foundation, Inc. | |
428 | 5 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp. |
6 ;; Copyright (C) 1995 Sun Microsystems. | |
1333 | 7 ;; Copyright (C) 2000, 2001, 2002, 2003 Ben Wing. |
428 | 8 |
9 ;; Maintainer: XEmacs Development Team | |
2525 | 10 ;; Keywords: extensions, dumped, internal |
428 | 11 |
12 ;; This file is part of XEmacs. | |
13 | |
14 ;; XEmacs is free software; you can redistribute it and/or modify it | |
15 ;; under the terms of the GNU General Public License as published by | |
16 ;; the Free Software Foundation; either version 2, or (at your option) | |
17 ;; any later version. | |
18 | |
19 ;; XEmacs is distributed in the hope that it will be useful, but | |
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 ;; General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
3000 | 26 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
27 ;; Boston, MA 02110-1301, USA. | |
428 | 28 |
1333 | 29 ;;; Synched up with: FSF 19.34. Some things synched up with later versions. |
428 | 30 |
31 ;;; Commentary: | |
32 | |
33 ;; This file is dumped with XEmacs. | |
34 | |
35 ;; There's not a whole lot in common now with the FSF version, | |
36 ;; be wary when applying differences. I've left in a number of lines | |
37 ;; of commentary just to give diff(1) something to synch itself with to | |
38 ;; provide useful context diffs. -sb | |
39 | |
1333 | 40 ;; BEGIN SYNCHED WITH FSF 21.2 |
41 | |
428 | 42 ;;; Code: |
1333 | 43 (defvar custom-declare-variable-list nil |
44 "Record `defcustom' calls made before `custom.el' is loaded to handle them. | |
45 Each element of this list holds the arguments to one call to `defcustom'.") | |
428 | 46 |
1333 | 47 ;; Use this, rather than defcustom, in subr.el and other files loaded |
1336 | 48 ;; before custom.el. See dumped-lisp.el. |
1333 | 49 (defun custom-declare-variable-early (&rest arguments) |
50 (setq custom-declare-variable-list | |
51 (cons arguments custom-declare-variable-list))) | |
2525 | 52 |
53 | |
54 (defun macro-declaration-function (macro decl) | |
55 "Process a declaration found in a macro definition. | |
56 This is set as the value of the variable `macro-declaration-function'. | |
57 MACRO is the name of the macro being defined. | |
58 DECL is a list `(declare ...)' containing the declarations. | |
59 The return value of this function is not used." | |
60 (dolist (d (cdr decl)) | |
61 (cond ((and (consp d) (eq (car d) 'indent)) | |
62 (put macro 'lisp-indent-function (cadr d))) | |
63 ((and (consp d) (eq (car d) 'debug)) | |
64 (put macro 'edebug-form-spec (cadr d))) | |
65 (t | |
66 (message "Unknown declaration %s" d))))) | |
67 | |
68 (setq macro-declaration-function 'macro-declaration-function) | |
69 | |
428 | 70 |
71 ;;;; Lisp language features. | |
72 | |
73 (defmacro lambda (&rest cdr) | |
74 "Return a lambda expression. | |
75 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is | |
76 self-quoting; the result of evaluating the lambda expression is the | |
77 expression itself. The lambda expression may then be treated as a | |
78 function, i.e., stored as the function value of a symbol, passed to | |
79 funcall or mapcar, etc. | |
80 | |
81 ARGS should take the same form as an argument list for a `defun'. | |
3842 | 82 Optional DOCSTRING is a documentation string. |
83 If present, it should describe how to call the function. Docstrings are | |
84 rarely useful unless the lambda will be named, eg, using `fset'. | |
85 Optional INTERACTIVE should be a call to the function `interactive'. | |
86 BODY should be a list of lisp expressions. | |
87 | |
88 The byte-compiler treats lambda expressions specially. If the lambda | |
89 expression is syntactically a function to be called, it will be compiled | |
90 unless protected by `quote'. Conversely, quoting a lambda expression with | |
91 `function' hints to the byte-compiler that it should compile the expression. | |
92 \(The byte-compiler may or may not actually compile it; for example it will | |
93 never compile lambdas nested in a data structure: `'(#'(lambda (x) x))'). | |
94 | |
95 The byte-compiler will warn about common problems such as the form | |
96 `(fset 'f '(lambda (x) x))' (the lambda cannot be byte-compiled; probably | |
97 the programmer intended `#'', although leaving the lambda unquoted will | |
98 normally suffice), but in general is it the programmer's responsibility to | |
99 quote lambda expressions appropriately." | |
428 | 100 `(function (lambda ,@cdr))) |
101 | |
1333 | 102 ;; FSF 21.2 has various basic macros here. We don't because they're either |
103 ;; in cl*.el (which we dump and hence is always available) or built-in. | |
104 | |
105 ;; More powerful versions in cl.el. | |
106 ;(defmacro push (newelt listname) | |
107 ;(defmacro pop (listname) | |
108 | |
109 ;; Built-in. | |
110 ;(defmacro when (cond &rest body) | |
111 ;(defmacro unless (cond &rest body) | |
112 | |
113 ;; More powerful versions in cl-macs.el. | |
114 ;(defmacro dolist (spec &rest body) | |
115 ;(defmacro dotimes (spec &rest body) | |
116 | |
117 ;; In cl.el. Ours are defun, but cl arranges for them to be inlined anyway. | |
118 ;(defsubst caar (x) | |
119 ;(defsubst cadr (x) | |
120 ;(defsubst cdar (x) | |
121 ;(defsubst cddr (x) | |
122 | |
123 ;; Built-in. Our `last' is more powerful in that it handles circularity. | |
124 ;(defun last (x &optional n) | |
125 ;(defun butlast (x &optional n) | |
126 ;(defun nbutlast (x &optional n) | |
127 | |
128 ;; In cl-seq.el. | |
129 ;(defun remove (elt seq) | |
130 ;(defun remq (elt list) | |
131 | |
428 | 132 (defmacro defun-when-void (&rest args) |
133 "Define a function, just like `defun', unless it's already defined. | |
134 Used for compatibility among different emacs variants." | |
135 `(if (fboundp ',(car args)) | |
136 nil | |
137 (defun ,@args))) | |
138 | |
139 (defmacro define-function-when-void (&rest args) | |
140 "Define a function, just like `define-function', unless it's already defined. | |
141 Used for compatibility among different emacs variants." | |
142 `(if (fboundp ,(car args)) | |
143 nil | |
144 (define-function ,@args))) | |
145 | |
146 | |
1333 | 147 (defun assoc-default (key alist &optional test default) |
148 "Find object KEY in a pseudo-alist ALIST. | |
149 ALIST is a list of conses or objects. Each element (or the element's car, | |
150 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY). | |
151 If that is non-nil, the element matches; | |
152 then `assoc-default' returns the element's cdr, if it is a cons, | |
153 or DEFAULT if the element is not a cons. | |
154 | |
155 If no element matches, the value is nil. | |
156 If TEST is omitted or nil, `equal' is used." | |
157 (let (found (tail alist) value) | |
158 (while (and tail (not found)) | |
159 (let ((elt (car tail))) | |
160 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key) | |
161 (setq found t value (if (consp elt) (cdr elt) default)))) | |
162 (setq tail (cdr tail))) | |
163 value)) | |
164 | |
165 (defun assoc-ignore-case (key alist) | |
166 "Like `assoc', but ignores differences in case and text representation. | |
167 KEY must be a string. Upper-case and lower-case letters are treated as equal." | |
168 (let (element) | |
169 (while (and alist (not element)) | |
170 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t)) | |
171 (setq element (car alist))) | |
172 (setq alist (cdr alist))) | |
173 element)) | |
174 | |
175 (defun assoc-ignore-representation (key alist) | |
176 "Like `assoc', but ignores differences in text representation. | |
177 KEY must be a string." | |
178 (let (element) | |
179 (while (and alist (not element)) | |
180 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil)) | |
181 (setq element (car alist))) | |
182 (setq alist (cdr alist))) | |
183 element)) | |
184 | |
185 (defun member-ignore-case (elt list) | |
186 "Like `member', but ignores differences in case and text representation. | |
187 ELT must be a string. Upper-case and lower-case letters are treated as equal." | |
188 (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t)))) | |
189 (setq list (cdr list))) | |
190 list) | |
191 | |
192 | |
428 | 193 ;;;; Keymap support. |
194 ;; XEmacs: removed to keymap.el | |
195 | |
196 ;;;; The global keymap tree. | |
197 | |
198 ;;; global-map, esc-map, and ctl-x-map have their values set up in | |
199 ;;; keymap.c; we just give them docstrings here. | |
200 | |
201 ;;;; Event manipulation functions. | |
202 | |
203 ;; XEmacs: This stuff is done in C Code. | |
204 | |
1333 | 205 ;;;; Obsolescent names for functions generally appear elsewhere, in |
206 ;;;; obsolete.el or in the files they are related do. Many very old | |
207 ;;;; obsolete stuff has been removed entirely (e.g. anything with `dot' in | |
208 ;;;; place of `point'). | |
209 | |
210 ; alternate names (not obsolete) | |
211 (if (not (fboundp 'mod)) (define-function 'mod '%)) | |
212 (define-function 'move-marker 'set-marker) | |
213 (define-function 'beep 'ding) ; preserve lingual purity | |
214 (define-function 'indent-to-column 'indent-to) | |
215 (define-function 'backward-delete-char 'delete-backward-char) | |
216 (define-function 'search-forward-regexp (symbol-function 're-search-forward)) | |
217 (define-function 'search-backward-regexp (symbol-function 're-search-backward)) | |
218 (define-function 'remove-directory 'delete-directory) | |
219 (define-function 'set-match-data 'store-match-data) | |
220 (define-function 'send-string-to-terminal 'external-debugging-output) | |
428 | 221 |
222 ;; XEmacs: | |
223 (defun local-variable-if-set-p (sym buffer) | |
224 "Return t if SYM would be local to BUFFER after it is set. | |
225 A nil value for BUFFER is *not* the same as (current-buffer), but | |
226 can be used to determine whether `make-variable-buffer-local' has been | |
227 called on SYM." | |
228 (local-variable-p sym buffer t)) | |
229 | |
230 | |
231 ;;;; Hook manipulation functions. | |
232 | |
233 ;; (defconst run-hooks 'run-hooks ...) | |
234 | |
235 (defun make-local-hook (hook) | |
236 "Make the hook HOOK local to the current buffer. | |
1333 | 237 The return value is HOOK. |
238 | |
239 You never need to call this function now that `add-hook' does it for you | |
240 if its LOCAL argument is non-nil. | |
241 | |
428 | 242 When a hook is local, its local and global values |
243 work in concert: running the hook actually runs all the hook | |
244 functions listed in *either* the local value *or* the global value | |
245 of the hook variable. | |
246 | |
247 This function works by making `t' a member of the buffer-local value, | |
248 which acts as a flag to run the hook functions in the default value as | |
249 well. This works for all normal hooks, but does not work for most | |
250 non-normal hooks yet. We will be changing the callers of non-normal | |
251 hooks so that they can handle localness; this has to be done one by | |
252 one. | |
253 | |
254 This function does nothing if HOOK is already local in the current | |
255 buffer. | |
256 | |
1333 | 257 Do not use `make-local-variable' to make a hook variable buffer-local." |
428 | 258 (if (local-variable-p hook (current-buffer)) ; XEmacs |
259 nil | |
260 (or (boundp hook) (set hook nil)) | |
261 (make-local-variable hook) | |
1333 | 262 (set hook (list t))) |
263 hook) | |
428 | 264 |
265 (defun add-hook (hook function &optional append local) | |
266 "Add to the value of HOOK the function FUNCTION. | |
267 FUNCTION is not added if already present. | |
268 FUNCTION is added (if necessary) at the beginning of the hook list | |
269 unless the optional argument APPEND is non-nil, in which case | |
270 FUNCTION is added at the end. | |
271 | |
272 The optional fourth argument, LOCAL, if non-nil, says to modify | |
273 the hook's buffer-local value rather than its default value. | |
1333 | 274 This makes the hook buffer-local if needed. |
428 | 275 To make a hook variable buffer-local, always use |
276 `make-local-hook', not `make-local-variable'. | |
277 | |
278 HOOK should be a symbol, and FUNCTION may be any valid function. If | |
279 HOOK is void, it is first set to nil. If HOOK's value is a single | |
442 | 280 function, it is changed to a list of functions. |
281 | |
282 You can remove this hook yourself using `remove-hook'. | |
283 | |
1333 | 284 See also `add-one-shot-hook'." |
428 | 285 (or (boundp hook) (set hook nil)) |
286 (or (default-boundp hook) (set-default hook nil)) | |
1333 | 287 (if local (unless (local-variable-if-set-p hook (current-buffer)) ; XEmacs |
288 (make-local-hook hook)) | |
289 ;; Detect the case where make-local-variable was used on a hook | |
290 ;; and do what we used to do. | |
291 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) | |
292 (setq local t))) | |
293 (let ((hook-value (if local (symbol-value hook) (default-value hook)))) | |
294 ;; If the hook value is a single function, turn it into a list. | |
295 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda)) | |
296 (setq hook-value (list hook-value))) | |
297 ;; Do the actual addition if necessary | |
298 (unless (member function hook-value) | |
299 (setq hook-value | |
300 (if append | |
301 (append hook-value (list function)) | |
302 (cons function hook-value)))) | |
303 ;; Set the actual variable | |
304 (if local (set hook hook-value) (set-default hook hook-value)))) | |
428 | 305 |
306 (defun remove-hook (hook function &optional local) | |
307 "Remove from the value of HOOK the function FUNCTION. | |
308 HOOK should be a symbol, and FUNCTION may be any valid function. If | |
309 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the | |
310 list of hooks to run in HOOK, then nothing is done. See `add-hook'. | |
311 | |
312 The optional third argument, LOCAL, if non-nil, says to modify | |
313 the hook's buffer-local value rather than its default value. | |
1333 | 314 This makes the hook buffer-local if needed. |
428 | 315 To make a hook variable buffer-local, always use |
316 `make-local-hook', not `make-local-variable'." | |
1333 | 317 (or (boundp hook) (set hook nil)) |
318 (or (default-boundp hook) (set-default hook nil)) | |
319 (if local (unless (local-variable-if-set-p hook (current-buffer)) ; XEmacs | |
320 (make-local-hook hook)) | |
321 ;; Detect the case where make-local-variable was used on a hook | |
322 ;; and do what we used to do. | |
323 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) | |
324 (setq local t))) | |
325 (let ((hook-value (if local (symbol-value hook) (default-value hook)))) | |
326 ;; Remove the function, for both the list and the non-list cases. | |
327 ;; XEmacs: add hook-test, for handling one-shot hooks. | |
328 (flet ((hook-test | |
329 (fn hel) | |
330 (or (equal fn hel) | |
331 (and (symbolp hel) | |
332 (equal fn | |
333 (get hel 'one-shot-hook-fun)))))) | |
334 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda)) | |
335 (if (equal hook-value function) (setq hook-value nil)) | |
336 (setq hook-value (delete* function (copy-sequence hook-value) | |
337 :test 'hook-test))) | |
338 ;; If the function is on the global hook, we need to shadow it locally | |
339 ;;(when (and local (member* function (default-value hook) | |
340 ;; :test 'hook-test) | |
341 ;; (not (member* (cons 'not function) hook-value | |
342 ;; :test 'hook-test))) | |
343 ;; (push (cons 'not function) hook-value)) | |
344 ;; Set the actual variable | |
345 (if local (set hook hook-value) (set-default hook hook-value))))) | |
442 | 346 |
347 ;; XEmacs addition | |
348 ;; #### we need a coherent scheme for indicating compatibility info, | |
349 ;; so that it can be programmatically retrieved. | |
350 (defun add-local-hook (hook function &optional append) | |
351 "Add to the local value of HOOK the function FUNCTION. | |
1333 | 352 You don't need this any more. It's equivalent to specifying the LOCAL |
353 argument to `add-hook'." | |
442 | 354 (add-hook hook function append t)) |
355 | |
356 ;; XEmacs addition | |
357 (defun remove-local-hook (hook function) | |
358 "Remove from the local value of HOOK the function FUNCTION. | |
1333 | 359 You don't need this any more. It's equivalent to specifying the LOCAL |
360 argument to `remove-hook'." | |
361 (remove-hook hook function t)) | |
442 | 362 |
363 (defun add-one-shot-hook (hook function &optional append local) | |
364 "Add to the value of HOOK the one-shot function FUNCTION. | |
365 FUNCTION will automatically be removed from the hook the first time | |
366 after it runs (whether to completion or to an error). | |
367 FUNCTION is not added if already present. | |
368 FUNCTION is added (if necessary) at the beginning of the hook list | |
369 unless the optional argument APPEND is non-nil, in which case | |
370 FUNCTION is added at the end. | |
371 | |
372 HOOK should be a symbol, and FUNCTION may be any valid function. If | |
373 HOOK is void, it is first set to nil. If HOOK's value is a single | |
374 function, it is changed to a list of functions. | |
375 | |
376 You can remove this hook yourself using `remove-hook'. | |
377 | |
1333 | 378 See also `add-hook'." |
442 | 379 (let ((sym (gensym))) |
380 (fset sym `(lambda (&rest args) | |
381 (unwind-protect | |
382 (apply ',function args) | |
383 (remove-hook ',hook ',sym ',local)))) | |
384 (put sym 'one-shot-hook-fun function) | |
385 (add-hook hook sym append local))) | |
386 | |
387 (defun add-local-one-shot-hook (hook function &optional append) | |
388 "Add to the local value of HOOK the one-shot function FUNCTION. | |
1333 | 389 You don't need this any more. It's equivalent to specifying the LOCAL |
390 argument to `add-one-shot-hook'." | |
442 | 391 (add-one-shot-hook hook function append t)) |
428 | 392 |
4461
42fad34efb3f
Support COMPARE-FN in add-to-list; thank you Brian Palmer.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4369
diff
changeset
|
393 (defun add-to-list (list-var element &optional append compare-fn) |
428 | 394 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
4461
42fad34efb3f
Support COMPARE-FN in add-to-list; thank you Brian Palmer.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4369
diff
changeset
|
395 The test for presence of ELEMENT is done with COMPARE-FN; if |
42fad34efb3f
Support COMPARE-FN in add-to-list; thank you Brian Palmer.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4369
diff
changeset
|
396 COMPARE-FN is nil, then it defaults to `equal'. If ELEMENT is added, |
42fad34efb3f
Support COMPARE-FN in add-to-list; thank you Brian Palmer.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4369
diff
changeset
|
397 it is added at the beginning of the list, unless the optional argument |
42fad34efb3f
Support COMPARE-FN in add-to-list; thank you Brian Palmer.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4369
diff
changeset
|
398 APPEND is non-nil, in which case ELEMENT is added at the end. |
878 | 399 |
428 | 400 If you want to use `add-to-list' on a variable that is not defined |
401 until a certain package is loaded, you should put the call to `add-to-list' | |
402 into a hook function that will be run only after loading the package. | |
403 `eval-after-load' provides one way to do this. In some cases | |
404 other hooks, such as major mode hooks, can do the job." | |
4463 | 405 (if (member* element (symbol-value list-var) :test (or compare-fn #'equal)) |
878 | 406 (symbol-value list-var) |
407 (set list-var | |
408 (if append | |
409 (append (symbol-value list-var) (list element)) | |
410 (cons element (symbol-value list-var)))))) | |
428 | 411 |
1333 | 412 ;; END SYNCHED WITH FSF 21.2 |
413 | |
428 | 414 ;; XEmacs additions |
415 ;; called by Fkill_buffer() | |
416 (defvar kill-buffer-hook nil | |
417 "Function or functions to be called when a buffer is killed. | |
418 The value of this variable may be buffer-local. | |
419 The buffer about to be killed is current when this hook is run.") | |
420 | |
421 ;; in C in FSFmacs | |
422 (defvar kill-emacs-hook nil | |
423 "Function or functions to be called when `kill-emacs' is called, | |
424 just before emacs is actually killed.") | |
425 | |
426 ;; not obsolete. | |
427 ;; #### These are a bad idea, because the CL RPLACA and RPLACD | |
428 ;; return the cons cell, not the new CAR/CDR. -hniksic | |
429 ;; The proper definition would be: | |
430 ;; (defun rplaca (conscell newcar) | |
431 ;; (setcar conscell newcar) | |
432 ;; conscell) | |
433 ;; ...and analogously for RPLACD. | |
434 (define-function 'rplaca 'setcar) | |
435 (define-function 'rplacd 'setcdr) | |
436 | |
437 (defun copy-symbol (symbol &optional copy-properties) | |
438 "Return a new uninterned symbol with the same name as SYMBOL. | |
439 If COPY-PROPERTIES is non-nil, the new symbol will have a copy of | |
440 SYMBOL's value, function, and property lists." | |
441 (let ((new (make-symbol (symbol-name symbol)))) | |
442 (when copy-properties | |
443 ;; This will not copy SYMBOL's chain of forwarding objects, but | |
444 ;; I think that's OK. Callers should not expect such magic to | |
445 ;; keep working in the copy in the first place. | |
446 (and (boundp symbol) | |
447 (set new (symbol-value symbol))) | |
448 (and (fboundp symbol) | |
449 (fset new (symbol-function symbol))) | |
450 (setplist new (copy-list (symbol-plist symbol)))) | |
451 new)) | |
452 | |
442 | 453 (defun set-symbol-value-in-buffer (sym val buffer) |
454 "Set the value of SYM to VAL in BUFFER. Useful with buffer-local variables. | |
455 If SYM has a buffer-local value in BUFFER, or will have one if set, this | |
456 function allows you to set the local value. | |
457 | |
458 NOTE: At some point, this will be moved into C and will be very fast." | |
459 (with-current-buffer buffer | |
460 (set sym val))) | |
444 | 461 |
1333 | 462 |
463 ;; BEGIN SYNCHED WITH FSF 21.2 | |
464 | |
465 ;; #### #### #### AAaargh! Must be in C, because it is used insanely | |
466 ;; early in the bootstrap process. | |
467 ;(defun split-path (path) | |
468 ; "Explode a search path into a list of strings. | |
469 ;The path components are separated with the characters specified | |
470 ;with `path-separator'." | |
471 ; (while (or (not stringp path-separator) | |
472 ; (/= (length path-separator) 1)) | |
473 ; (setq path-separator (signal 'error (list "\ | |
474 ;`path-separator' should be set to a single-character string" | |
475 ; path-separator)))) | |
476 ; (split-string-by-char path (aref separator 0))) | |
477 | |
478 (defmacro with-current-buffer (buffer &rest body) | |
479 "Temporarily make BUFFER the current buffer and execute the forms in BODY. | |
480 The value returned is the value of the last form in BODY. | |
481 See also `with-temp-buffer'." | |
482 `(save-current-buffer | |
483 (set-buffer ,buffer) | |
484 ,@body)) | |
485 | |
486 (defmacro with-temp-file (filename &rest forms) | |
487 "Create a new buffer, evaluate FORMS there, and write the buffer to FILENAME. | |
488 The value of the last form in FORMS is returned, like `progn'. | |
489 See also `with-temp-buffer'." | |
490 (let ((temp-file (make-symbol "temp-file")) | |
491 (temp-buffer (make-symbol "temp-buffer"))) | |
492 `(let ((,temp-file ,filename) | |
493 (,temp-buffer | |
494 (get-buffer-create (generate-new-buffer-name " *temp file*")))) | |
495 (unwind-protect | |
496 (prog1 | |
497 (with-current-buffer ,temp-buffer | |
498 ,@forms) | |
499 (with-current-buffer ,temp-buffer | |
500 (widen) | |
501 (write-region (point-min) (point-max) ,temp-file nil 0))) | |
502 (and (buffer-name ,temp-buffer) | |
503 (kill-buffer ,temp-buffer)))))) | |
504 | |
505 ;; FSF compatibility | |
506 (defmacro with-temp-message (message &rest body) | |
507 "Display MESSAGE temporarily while BODY is evaluated. | |
508 The original message is restored to the echo area after BODY has finished. | |
509 The value returned is the value of the last form in BODY. | |
510 If MESSAGE is nil, the echo area and message log buffer are unchanged. | |
511 Use a MESSAGE of \"\" to temporarily clear the echo area. | |
428 | 512 |
1333 | 513 Note that this function exists for FSF compatibility purposes. A better way |
514 under XEmacs is to give the message a particular label (see `display-message'); | |
515 then, the old message is automatically restored when you clear your message | |
516 with `clear-message'." | |
517 ;; FSF additional doc string from 21.2: | |
518 ;; MESSAGE is written to the message log buffer if `message-log-max' is non-nil. | |
519 (let ((current-message (make-symbol "current-message")) | |
520 (temp-message (make-symbol "with-temp-message"))) | |
521 `(let ((,temp-message ,message) | |
522 (,current-message)) | |
523 (unwind-protect | |
524 (progn | |
525 (when ,temp-message | |
526 (setq ,current-message (current-message)) | |
527 (message "%s" ,temp-message)) | |
528 ,@body) | |
529 (and ,temp-message ,current-message | |
530 (message "%s" ,current-message)))))) | |
531 | |
532 (defmacro with-temp-buffer (&rest forms) | |
533 "Create a temporary buffer, and evaluate FORMS there like `progn'. | |
534 See also `with-temp-file' and `with-output-to-string'." | |
535 (let ((temp-buffer (make-symbol "temp-buffer"))) | |
536 `(let ((,temp-buffer | |
537 (get-buffer-create (generate-new-buffer-name " *temp*")))) | |
538 (unwind-protect | |
539 (with-current-buffer ,temp-buffer | |
540 ,@forms) | |
541 (and (buffer-name ,temp-buffer) | |
542 (kill-buffer ,temp-buffer)))))) | |
543 | |
544 (defmacro with-output-to-string (&rest body) | |
545 "Execute BODY, return the text it sent to `standard-output', as a string." | |
546 `(let ((standard-output | |
547 (get-buffer-create (generate-new-buffer-name " *string-output*")))) | |
548 (let ((standard-output standard-output)) | |
549 ,@body) | |
550 (with-current-buffer standard-output | |
551 (prog1 | |
552 (buffer-string) | |
553 (kill-buffer nil))))) | |
554 | |
2135 | 555 (defmacro with-local-quit (&rest body) |
556 "Execute BODY with `inhibit-quit' temporarily bound to nil." | |
557 `(condition-case nil | |
558 (let ((inhibit-quit nil)) | |
559 ,@body) | |
560 (quit (setq quit-flag t)))) | |
561 | |
562 ;; FSF 21.3. | |
1333 | 563 |
564 ; (defmacro combine-after-change-calls (&rest body) | |
565 ; "Execute BODY, but don't call the after-change functions till the end. | |
566 ; If BODY makes changes in the buffer, they are recorded | |
567 ; and the functions on `after-change-functions' are called several times | |
568 ; when BODY is finished. | |
569 ; The return value is the value of the last form in BODY. | |
570 | |
571 ; If `before-change-functions' is non-nil, then calls to the after-change | |
572 ; functions can't be deferred, so in that case this macro has no effect. | |
573 | |
574 ; Do not alter `after-change-functions' or `before-change-functions' | |
575 ; in BODY." | |
2135 | 576 ; (declare (indent 0) (debug t)) |
1333 | 577 ; `(unwind-protect |
578 ; (let ((combine-after-change-calls t)) | |
579 ; . ,body) | |
580 ; (combine-after-change-execute))) | |
801 | 581 |
4369
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
582 (defmacro with-case-table (table &rest body) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
583 "Execute the forms in BODY with TABLE as the current case table. |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
584 The value returned is the value of the last form in BODY." |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
585 (declare (indent 1) (debug t)) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
586 (let ((old-case-table (make-symbol "table")) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
587 (old-buffer (make-symbol "buffer"))) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
588 `(let ((,old-case-table (current-case-table)) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
589 (,old-buffer (current-buffer))) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
590 (unwind-protect |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
591 (progn (set-case-table ,table) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
592 ,@body) |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
593 (with-current-buffer ,old-buffer |
ef9eb714f0e4
Add ascii-case-table, #'with-case-table; make iso8859-1.el more comprehensible.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
594 (set-case-table ,old-case-table)))))) |
2135 | 595 |
596 (defvar delay-mode-hooks nil | |
597 "If non-nil, `run-mode-hooks' should delay running the hooks.") | |
598 (defvar delayed-mode-hooks nil | |
599 "List of delayed mode hooks waiting to be run.") | |
600 (make-variable-buffer-local 'delayed-mode-hooks) | |
601 (put 'delay-mode-hooks 'permanent-local t) | |
602 | |
603 (defun run-mode-hooks (&rest hooks) | |
604 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS. | |
605 Execution is delayed if `delay-mode-hooks' is non-nil. | |
606 Major mode functions should use this." | |
607 (if delay-mode-hooks | |
608 ;; Delaying case. | |
609 (dolist (hook hooks) | |
610 (push hook delayed-mode-hooks)) | |
611 ;; Normal case, just run the hook as before plus any delayed hooks. | |
612 (setq hooks (nconc (nreverse delayed-mode-hooks) hooks)) | |
613 (setq delayed-mode-hooks nil) | |
614 (apply 'run-hooks hooks))) | |
615 | |
616 (defmacro delay-mode-hooks (&rest body) | |
617 "Execute BODY, but delay any `run-mode-hooks'. | |
618 Only affects hooks run in the current buffer." | |
619 `(progn | |
620 (make-local-variable 'delay-mode-hooks) | |
621 (let ((delay-mode-hooks t)) | |
622 ,@body))) | |
623 | |
1333 | 624 (defmacro with-syntax-table (table &rest body) |
625 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE. | |
626 The syntax table of the current buffer is saved, BODY is evaluated, and the | |
627 saved table is restored, even in case of an abnormal exit. | |
628 Value is what BODY returns." | |
629 (let ((old-table (make-symbol "table")) | |
630 (old-buffer (make-symbol "buffer"))) | |
631 `(let ((,old-table (syntax-table)) | |
632 (,old-buffer (current-buffer))) | |
633 (unwind-protect | |
634 (progn | |
635 (set-syntax-table (copy-syntax-table ,table)) | |
636 ,@body) | |
637 (save-current-buffer | |
638 (set-buffer ,old-buffer) | |
639 (set-syntax-table ,old-table)))))) | |
640 | |
641 (put 'with-syntax-table 'lisp-indent-function 1) | |
642 (put 'with-syntax-table 'edebug-form-spec '(form body)) | |
643 | |
644 | |
645 ;; Moved from mule-coding.el. | |
646 (defmacro with-string-as-buffer-contents (str &rest body) | |
647 "With the contents of the current buffer being STR, run BODY. | |
4516
e96f3aca4d63
Document initial position of point in `with-string-as-buffer-contents'.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4504
diff
changeset
|
648 Point starts positioned to end of buffer. |
1333 | 649 Returns the new contents of the buffer, as modified by BODY. |
650 The original current buffer is restored afterwards." | |
651 `(with-temp-buffer | |
652 (insert ,str) | |
653 ,@body | |
654 (buffer-string))) | |
655 | |
656 | |
657 (defmacro save-match-data (&rest body) | |
658 "Execute BODY forms, restoring the global value of the match data." | |
659 (let ((original (make-symbol "match-data"))) | |
660 (list 'let (list (list original '(match-data))) | |
661 (list 'unwind-protect | |
662 (cons 'progn body) | |
663 (list 'store-match-data original))))) | |
664 | |
665 | |
666 (defun match-string (num &optional string) | |
667 "Return string of text matched by last search. | |
668 NUM specifies which parenthesized expression in the last regexp. | |
669 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
670 Zero means the entire text matched by the whole regexp or whole string. | |
671 STRING should be given if the last search was by `string-match' on STRING." | |
672 (if (match-beginning num) | |
673 (if string | |
674 (substring string (match-beginning num) (match-end num)) | |
675 (buffer-substring (match-beginning num) (match-end num))))) | |
801 | 676 |
1333 | 677 (defun match-string-no-properties (num &optional string) |
678 "Return string of text matched by last search, without text properties. | |
679 NUM specifies which parenthesized expression in the last regexp. | |
680 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
681 Zero means the entire text matched by the whole regexp or whole string. | |
682 STRING should be given if the last search was by `string-match' on STRING." | |
683 (if (match-beginning num) | |
684 (if string | |
685 (let ((result | |
686 (substring string (match-beginning num) (match-end num)))) | |
687 (set-text-properties 0 (length result) nil result) | |
688 result) | |
689 (buffer-substring-no-properties (match-beginning num) | |
690 (match-end num))))) | |
691 | |
1425 | 692 (defconst split-string-default-separators "[ \f\t\n\r\v]+" |
693 "The default value of separators for `split-string'. | |
694 | |
695 A regexp matching strings of whitespace. May be locale-dependent | |
696 \(as yet unimplemented). Should not match non-breaking spaces. | |
697 | |
698 Warning: binding this to a different value and using it as default is | |
699 likely to have undesired semantics.") | |
700 | |
701 ;; specification for `split-string' agreed with rms 2003-04-23 | |
702 ;; xemacs design <87vfx5vor0.fsf@tleepslib.sk.tsukuba.ac.jp> | |
703 | |
1495 | 704 ;; The specification says that if both SEPARATORS and OMIT-NULLS are |
705 ;; defaulted, OMIT-NULLS should be treated as t. Simplifying the logical | |
706 ;; expression leads to the equivalent implementation that if SEPARATORS | |
707 ;; is defaulted, OMIT-NULLS is treated as t. | |
708 | |
1425 | 709 (defun split-string (string &optional separators omit-nulls) |
710 "Splits STRING into substrings bounded by matches for SEPARATORS. | |
711 | |
712 The beginning and end of STRING, and each match for SEPARATORS, are | |
713 splitting points. The substrings matching SEPARATORS are removed, and | |
714 the substrings between the splitting points are collected as a list, | |
1333 | 715 which is returned. |
1425 | 716 |
2138 | 717 If SEPARATORS is non-`nil', it should be a regular expression matching text |
718 which separates, but is not part of, the substrings. If `nil' it defaults to | |
1495 | 719 `split-string-default-separators', normally \"[ \\f\\t\\n\\r\\v]+\", and |
2138 | 720 OMIT-NULLS is forced to `t'. |
1333 | 721 |
2138 | 722 If OMIT-NULLS is `t', zero-length substrings are omitted from the list \(so |
1425 | 723 that for the default value of SEPARATORS leading and trailing whitespace |
2138 | 724 are effectively trimmed). If `nil', all zero-length substrings are retained, |
1425 | 725 which correctly parses CSV format, for example. |
726 | |
1495 | 727 Note that the effect of `(split-string STRING)' is the same as |
728 `(split-string STRING split-string-default-separators t)'). In the rare | |
729 case that you wish to retain zero-length substrings when splitting on | |
730 whitespace, use `(split-string STRING split-string-default-separators nil)'. | |
1333 | 731 |
2138 | 732 Modifies the match data when successful; use `save-match-data' if necessary." |
1425 | 733 |
1495 | 734 (let ((keep-nulls (not (if separators omit-nulls t))) |
1425 | 735 (rexp (or separators split-string-default-separators)) |
1333 | 736 (start 0) |
737 notfirst | |
738 (list nil)) | |
739 (while (and (string-match rexp string | |
740 (if (and notfirst | |
741 (= start (match-beginning 0)) | |
742 (< start (length string))) | |
743 (1+ start) start)) | |
1425 | 744 (< start (length string))) |
1333 | 745 (setq notfirst t) |
1425 | 746 (if (or keep-nulls (< start (match-beginning 0))) |
1333 | 747 (setq list |
748 (cons (substring string start (match-beginning 0)) | |
749 list))) | |
750 (setq start (match-end 0))) | |
1425 | 751 (if (or keep-nulls (< start (length string))) |
1333 | 752 (setq list |
753 (cons (substring string start) | |
754 list))) | |
755 (nreverse list))) | |
756 | |
757 (defun subst-char-in-string (fromchar tochar string &optional inplace) | |
758 "Replace FROMCHAR with TOCHAR in STRING each time it occurs. | |
759 Unless optional argument INPLACE is non-nil, return a new string." | |
760 (let ((i (length string)) | |
761 (newstr (if inplace string (copy-sequence string)))) | |
762 (while (> i 0) | |
763 (setq i (1- i)) | |
764 (if (eq (aref newstr i) fromchar) | |
765 (aset newstr i tochar))) | |
766 newstr)) | |
767 | |
768 | |
769 ;; XEmacs addition: | |
428 | 770 (defun replace-in-string (str regexp newtext &optional literal) |
771 "Replace all matches in STR for REGEXP with NEWTEXT string, | |
772 and returns the new string. | |
773 Optional LITERAL non-nil means do a literal replacement. | |
442 | 774 Otherwise treat `\\' in NEWTEXT as special: |
775 `\\&' in NEWTEXT means substitute original matched text. | |
776 `\\N' means substitute what matched the Nth `\\(...\\)'. | |
777 If Nth parens didn't match, substitute nothing. | |
778 `\\\\' means insert one `\\'. | |
779 `\\u' means upcase the next character. | |
780 `\\l' means downcase the next character. | |
781 `\\U' means begin upcasing all following characters. | |
782 `\\L' means begin downcasing all following characters. | |
783 `\\E' means terminate the effect of any `\\U' or `\\L'." | |
428 | 784 (check-argument-type 'stringp str) |
785 (check-argument-type 'stringp newtext) | |
442 | 786 (if (> (length str) 50) |
924 | 787 (let ((cfs case-fold-search)) |
788 (with-temp-buffer | |
789 (setq case-fold-search cfs) | |
790 (insert str) | |
791 (goto-char 1) | |
442 | 792 (while (re-search-forward regexp nil t) |
793 (replace-match newtext t literal)) | |
924 | 794 (buffer-string))) |
795 (let ((start 0) newstr) | |
796 (while (string-match regexp str start) | |
797 (setq newstr (replace-match newtext t literal str) | |
798 start (+ (match-end 0) (- (length newstr) (length str))) | |
799 str newstr)) | |
800 str))) | |
428 | 801 |
1333 | 802 (defun replace-regexp-in-string (regexp rep string &optional |
803 fixedcase literal subexp start) | |
804 "Replace all matches for REGEXP with REP in STRING. | |
805 | |
806 Return a new string containing the replacements. | |
807 | |
4199 | 808 Optional arguments FIXEDCASE and LITERAL are like the arguments with |
809 the same names of function `replace-match'. If START is non-nil, | |
810 start replacements at that index in STRING. | |
811 | |
812 For compatibility with old XEmacs code and with recent GNU Emacs, the | |
813 interpretation of SUBEXP is somewhat complicated. If SUBEXP is a | |
814 buffer, it is interpreted as the buffer which provides syntax tables | |
815 and case tables for the match and replacement. If it is not a buffer, | |
816 the current buffer is used. If SUBEXP is an integer, it is the index | |
817 of the subexpression of REGEXP which is to be replaced. | |
428 | 818 |
1333 | 819 REP is either a string used as the NEWTEXT arg of `replace-match' or a |
820 function. If it is a function it is applied to each match to generate | |
821 the replacement passed to `replace-match'; the match-data at this | |
4199 | 822 point are such that `(match-string SUBEXP STRING)' is the function's |
823 argument if SUBEXP is an integer \(otherwise the whole match is passed | |
824 and replaced). | |
428 | 825 |
1333 | 826 To replace only the first match (if any), make REGEXP match up to \\' |
827 and replace a sub-expression, e.g. | |
828 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1) | |
829 => \" bar foo\" | |
4199 | 830 |
831 Signals `invalid-argument' if SUBEXP is not an integer, buffer, or nil; | |
832 or is an integer, but the indicated subexpression was not matched. | |
833 Signals `invalid-argument' if STRING is nil but the last text matched was a string, | |
834 or if STRING is a string but the last text matched was a buffer." | |
428 | 835 |
1333 | 836 ;; To avoid excessive consing from multiple matches in long strings, |
837 ;; don't just call `replace-match' continually. Walk down the | |
838 ;; string looking for matches of REGEXP and building up a (reversed) | |
839 ;; list MATCHES. This comprises segments of STRING which weren't | |
840 ;; matched interspersed with replacements for segments that were. | |
841 ;; [For a `large' number of replacments it's more efficient to | |
842 ;; operate in a temporary buffer; we can't tell from the function's | |
843 ;; args whether to choose the buffer-based implementation, though it | |
844 ;; might be reasonable to do so for long enough STRING.] | |
845 (let ((l (length string)) | |
846 (start (or start 0)) | |
4199 | 847 (expndx (if (integerp subexp) subexp 0)) |
1333 | 848 matches str mb me) |
849 (save-match-data | |
850 (while (and (< start l) (string-match regexp string start)) | |
851 (setq mb (match-beginning 0) | |
852 me (match-end 0)) | |
853 ;; If we matched the empty string, make sure we advance by one char | |
854 (when (= me mb) (setq me (min l (1+ mb)))) | |
855 ;; Generate a replacement for the matched substring. | |
856 ;; Operate only on the substring to minimize string consing. | |
857 ;; Set up match data for the substring for replacement; | |
858 ;; presumably this is likely to be faster than munging the | |
859 ;; match data directly in Lisp. | |
860 (string-match regexp (setq str (substring string mb me))) | |
861 (setq matches | |
862 (cons (replace-match (if (stringp rep) | |
863 rep | |
4199 | 864 (funcall rep (match-string expndx str))) |
865 ;; no, this subexp shouldn't be expndx | |
1333 | 866 fixedcase literal str subexp) |
867 (cons (substring string start mb) ; unmatched prefix | |
868 matches))) | |
869 (setq start me)) | |
870 ;; Reconstruct a string from the pieces. | |
871 (setq matches (cons (substring string start l) matches)) ; leftover | |
872 (apply #'concat (nreverse matches))))) | |
428 | 873 |
1333 | 874 ;; END SYNCHED WITH FSF 21.2 |
875 | |
876 | |
1899 | 877 ;; BEGIN SYNCHED WITH FSF 21.3 |
878 | |
879 (defun add-to-invisibility-spec (arg) | |
880 "Add elements to `buffer-invisibility-spec'. | |
881 See documentation for `buffer-invisibility-spec' for the kind of elements | |
882 that can be added." | |
883 (if (eq buffer-invisibility-spec t) | |
884 (setq buffer-invisibility-spec (list t))) | |
885 (setq buffer-invisibility-spec | |
886 (cons arg buffer-invisibility-spec))) | |
887 | |
888 (defun remove-from-invisibility-spec (arg) | |
889 "Remove elements from `buffer-invisibility-spec'." | |
890 (if (consp buffer-invisibility-spec) | |
891 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec)))) | |
892 | |
893 ;; END SYNCHED WITH FSF 21.3 | |
894 | |
895 | |
1333 | 896 ;;; Basic string functions |
883 | 897 |
1333 | 898 ;; XEmacs |
899 (defun string-equal-ignore-case (str1 str2) | |
900 "Return t if two strings have identical contents, ignoring case differences. | |
901 Case is not significant. Text properties and extents are ignored. | |
902 Symbols are also allowed; their print names are used instead. | |
428 | 903 |
1333 | 904 See also `equalp'." |
905 (if (symbolp str1) | |
906 (setq str1 (symbol-name str1))) | |
907 (if (symbolp str2) | |
908 (setq str2 (symbol-name str2))) | |
909 (eq t (compare-strings str1 nil nil str2 nil nil t))) | |
428 | 910 |
911 (defun insert-face (string face) | |
912 "Insert STRING and highlight with FACE. Return the extent created." | |
913 (let ((p (point)) ext) | |
914 (insert string) | |
915 (setq ext (make-extent p (point))) | |
916 (set-extent-face ext face) | |
917 ext)) | |
918 | |
919 ;; not obsolete. | |
920 (define-function 'string= 'string-equal) | |
921 (define-function 'string< 'string-lessp) | |
922 (define-function 'int-to-string 'number-to-string) | |
923 (define-function 'string-to-int 'string-to-number) | |
924 | |
925 ;; These two names are a bit awkward, as they conflict with the normal | |
926 ;; foo-to-bar naming scheme, but CLtL2 has them, so they stay. | |
927 (define-function 'char-int 'char-to-int) | |
928 (define-function 'int-char 'int-to-char) | |
929 | |
4329
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
930 ;; XEmacs addition. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
931 (defun integer-to-bit-vector (integer &optional minlength) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
932 "Return INTEGER converted to a bit vector. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
933 Optional argument MINLENGTH gives a minimum length for the returned vector. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
934 If MINLENGTH is not given, zero high-order bits will be ignored." |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
935 (check-argument-type #'integerp integer) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
936 (setq minlength (or minlength 0)) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
937 (check-nonnegative-number minlength) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
938 (read (format (format "#*%%0%db" minlength) integer))) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
939 |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
940 ;; XEmacs addition. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
941 (defun bit-vector-to-integer (bit-vector) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
942 "Return BIT-VECTOR converted to an integer. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
943 If bignum support is available, BIT-VECTOR's length is unlimited. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
944 Otherwise the limit is the number of value bits in an Lisp integer. " |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
945 (check-argument-type #'bit-vector-p bit-vector) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
946 (setq bit-vector (prin1-to-string bit-vector)) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
947 (aset bit-vector 1 ?b) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
948 (read bit-vector)) |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4267
diff
changeset
|
949 |
771 | 950 (defun string-width (string) |
951 "Return number of columns STRING occupies when displayed. | |
952 With international (Mule) support, uses the charset-columns attribute of | |
953 the characters in STRING, which may not accurately represent the actual | |
954 display width when using a window system. With no international support, | |
955 simply returns the length of the string." | |
956 (if (featurep 'mule) | |
957 (let ((col 0) | |
958 (len (length string)) | |
959 (i 0)) | |
772 | 960 (with-fboundp '(charset-width char-charset) |
961 (while (< i len) | |
962 (setq col (+ col (charset-width (char-charset (aref string i))))) | |
963 (setq i (1+ i)))) | |
771 | 964 col) |
965 (length string))) | |
966 | |
777 | 967 (defun char-width (character) |
968 "Return number of columns a CHARACTER occupies when displayed." | |
969 (if (featurep 'mule) | |
970 (with-fboundp '(charset-width char-charset) | |
971 (charset-width (char-charset character))) | |
972 1)) | |
973 | |
974 ;; The following several functions are useful in GNU Emacs 20 because | |
975 ;; of the multibyte "characters" the internal representation of which | |
976 ;; leaks into Lisp. In XEmacs/Mule they are trivial and unnecessary. | |
977 ;; We provide them for compatibility reasons solely. | |
978 | |
979 (defun string-to-sequence (string type) | |
980 "Convert STRING to a sequence of TYPE which contains characters in STRING. | |
981 TYPE should be `list' or `vector'." | |
982 (ecase type | |
983 (list | |
4267 | 984 (append string nil)) |
777 | 985 (vector |
4267 | 986 (vconcat string)))) |
777 | 987 |
988 (defun string-to-list (string) | |
989 "Return a list of characters in STRING." | |
4267 | 990 (append string nil)) |
777 | 991 |
992 (defun string-to-vector (string) | |
993 "Return a vector of characters in STRING." | |
4267 | 994 (vconcat string)) |
777 | 995 |
996 (defun store-substring (string idx obj) | |
997 "Embed OBJ (string or character) at index IDX of STRING." | |
998 (let* ((str (cond ((stringp obj) obj) | |
999 ((characterp obj) (char-to-string obj)) | |
1000 (t (error | |
1001 "Invalid argument (should be string or character): %s" | |
1002 obj)))) | |
1003 (string-len (length string)) | |
1004 (len (length str)) | |
1005 (i 0)) | |
1006 (while (and (< i len) (< idx string-len)) | |
1007 (aset string idx (aref str i)) | |
1008 (setq idx (1+ idx) i (1+ i))) | |
1009 string)) | |
1010 | |
851 | 1011 ;; From FSF 21.1; ELLIPSES is XEmacs addition. |
1012 | |
1013 (defun truncate-string-to-width (str end-column &optional start-column padding | |
1333 | 1014 ellipses) |
777 | 1015 "Truncate string STR to end at column END-COLUMN. |
814 | 1016 The optional 3rd arg START-COLUMN, if non-nil, specifies |
777 | 1017 the starting column; that means to return the characters occupying |
1018 columns START-COLUMN ... END-COLUMN of STR. | |
1019 | |
814 | 1020 The optional 4th arg PADDING, if non-nil, specifies a padding character |
777 | 1021 to add at the end of the result if STR doesn't reach column END-COLUMN, |
1022 or if END-COLUMN comes in the middle of a character in STR. | |
1023 PADDING is also added at the beginning of the result | |
1024 if column START-COLUMN appears in the middle of a character in STR. | |
1025 | |
1026 If PADDING is nil, no padding is added in these cases, so | |
851 | 1027 the resulting string may be narrower than END-COLUMN. |
1028 | |
1029 BUG: Currently assumes that the padding character is of width one. You | |
1030 will get weird results if not. | |
1031 | |
1032 If ELLIPSES is non-nil, add ellipses (specified by ELLIPSES if a string, | |
1033 else `...') if STR extends past END-COLUMN. The ellipses will be added in | |
1034 such a way that the total string occupies no more than END-COLUMN columns | |
1035 -- i.e. if the string goes past END-COLUMN, it will be truncated somewhere | |
1036 short of END-COLUMN so that, with the ellipses added (and padding, if the | |
1037 proper place to truncate the string would be in the middle of a character), | |
1038 the string occupies exactly END-COLUMN columns." | |
777 | 1039 (or start-column |
1040 (setq start-column 0)) | |
814 | 1041 (let ((len (length str)) |
1042 (idx 0) | |
1043 (column 0) | |
1044 (head-padding "") (tail-padding "") | |
1045 ch last-column last-idx from-idx) | |
851 | 1046 |
1047 ;; find the index of START-COLUMN; bail out if end of string reached. | |
814 | 1048 (condition-case nil |
1049 (while (< column start-column) | |
1050 (setq ch (aref str idx) | |
1051 column (+ column (char-width ch)) | |
1052 idx (1+ idx))) | |
1053 (args-out-of-range (setq idx len))) | |
1054 (if (< column start-column) | |
851 | 1055 ;; if string ends before START-COLUMN, return either a blank string |
1056 ;; or a string entirely padded. | |
1057 (if padding (make-string (- end-column start-column) padding) "") | |
814 | 1058 (if (and padding (> column start-column)) |
1059 (setq head-padding (make-string (- column start-column) padding))) | |
1060 (setq from-idx idx) | |
851 | 1061 ;; If END-COLUMN is before START-COLUMN, then bail out. |
814 | 1062 (if (< end-column column) |
851 | 1063 (setq idx from-idx ellipses "") |
1064 | |
1065 ;; handle ELLIPSES | |
1066 (cond ((null ellipses) (setq ellipses "")) | |
1067 ((if (<= (string-width str) end-column) | |
1068 ;; string fits, no ellipses | |
1069 (setq ellipses ""))) | |
1070 (t | |
1071 ;; else, insert default value and ... | |
1072 (or (stringp ellipses) (setq ellipses "...")) | |
1073 ;; ... take away the width of the ellipses from the | |
1074 ;; destination. do all computations with new, shorter | |
1075 ;; width. the padding computed will get us exactly up to | |
1076 ;; the shorted width, which is right -- it just gets added | |
1077 ;; to the right of the ellipses. | |
924 | 1078 (setq end-column (- end-column (string-width ellipses))))) |
851 | 1079 |
1080 ;; find the index of END-COLUMN; bail out if end of string reached. | |
814 | 1081 (condition-case nil |
1082 (while (< column end-column) | |
1083 (setq last-column column | |
1084 last-idx idx | |
1085 ch (aref str idx) | |
1086 column (+ column (char-width ch)) | |
1087 idx (1+ idx))) | |
1088 (args-out-of-range (setq idx len))) | |
851 | 1089 ;; if we went too far (stopped in middle of character), back up. |
814 | 1090 (if (> column end-column) |
1091 (setq column last-column idx last-idx)) | |
851 | 1092 ;; compute remaining padding |
814 | 1093 (if (and padding (< column end-column)) |
1094 (setq tail-padding (make-string (- end-column column) padding)))) | |
851 | 1095 ;; get substring ... |
814 | 1096 (setq str (substring str from-idx idx)) |
851 | 1097 ;; and construct result |
814 | 1098 (if padding |
851 | 1099 (concat head-padding str tail-padding ellipses) |
1100 (concat str ellipses))))) | |
801 | 1101 |
428 | 1102 |
1103 ;; alist/plist functions | |
1104 (defun plist-to-alist (plist) | |
1105 "Convert property list PLIST into the equivalent association-list form. | |
1106 The alist is returned. This converts from | |
1107 | |
1108 \(a 1 b 2 c 3) | |
1109 | |
1110 into | |
1111 | |
1112 \((a . 1) (b . 2) (c . 3)) | |
1113 | |
1114 The original plist is not modified. See also `destructive-plist-to-alist'." | |
1115 (let (alist) | |
1116 (while plist | |
1117 (setq alist (cons (cons (car plist) (cadr plist)) alist)) | |
1118 (setq plist (cddr plist))) | |
1119 (nreverse alist))) | |
1120 | |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1121 ((macro |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1122 . (lambda (map-plist-definition) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1123 "Replace the variable names in MAP-PLIST-DEFINITION with uninterned |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1124 symbols, avoiding the risk of interference with variables in other functions |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1125 introduced by dynamic scope." |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1126 (if-fboundp 'nsublis |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1127 (nsublis |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1128 '((mp-function . #:function) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1129 (plist . #:plist) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1130 (result . #:result)) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1131 map-plist-definition) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1132 map-plist-definition))) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1133 (defun map-plist (mp-function plist) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1134 "Map FUNCTION (a function of two args) over each key/value pair in PLIST. |
783 | 1135 Return a list of the results." |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1136 (let (result) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1137 (while plist |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1138 (push (funcall mp-function (car plist) (cadr plist)) result) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1139 (setq plist (cddr plist))) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1140 (nreverse result)))) |
783 | 1141 |
428 | 1142 (defun destructive-plist-to-alist (plist) |
1143 "Convert property list PLIST into the equivalent association-list form. | |
1144 The alist is returned. This converts from | |
1145 | |
1146 \(a 1 b 2 c 3) | |
1147 | |
1148 into | |
1149 | |
1150 \((a . 1) (b . 2) (c . 3)) | |
1151 | |
1152 The original plist is destroyed in the process of constructing the alist. | |
1153 See also `plist-to-alist'." | |
1154 (let ((head plist) | |
1155 next) | |
1156 (while plist | |
1157 ;; remember the next plist pair. | |
1158 (setq next (cddr plist)) | |
1159 ;; make the cons holding the property value into the alist element. | |
1160 (setcdr (cdr plist) (cadr plist)) | |
1161 (setcar (cdr plist) (car plist)) | |
1162 ;; reattach into alist form. | |
1163 (setcar plist (cdr plist)) | |
1164 (setcdr plist next) | |
1165 (setq plist next)) | |
1166 head)) | |
1167 | |
1168 (defun alist-to-plist (alist) | |
1169 "Convert association list ALIST into the equivalent property-list form. | |
1170 The plist is returned. This converts from | |
1171 | |
1172 \((a . 1) (b . 2) (c . 3)) | |
1173 | |
1174 into | |
1175 | |
1176 \(a 1 b 2 c 3) | |
1177 | |
1178 The original alist is not modified. See also `destructive-alist-to-plist'." | |
1179 (let (plist) | |
1180 (while alist | |
1181 (let ((el (car alist))) | |
1182 (setq plist (cons (cdr el) (cons (car el) plist)))) | |
1183 (setq alist (cdr alist))) | |
1184 (nreverse plist))) | |
1185 | |
1186 ;; getf, remf in cl*.el. | |
1187 | |
444 | 1188 (defmacro putf (plist property value) |
1189 "Add property PROPERTY to plist PLIST with value VALUE. | |
1190 Analogous to (setq PLIST (plist-put PLIST PROPERTY VALUE))." | |
1191 `(setq ,plist (plist-put ,plist ,property ,value))) | |
428 | 1192 |
444 | 1193 (defmacro laxputf (lax-plist property value) |
1194 "Add property PROPERTY to lax plist LAX-PLIST with value VALUE. | |
1195 Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROPERTY VALUE))." | |
1196 `(setq ,lax-plist (lax-plist-put ,lax-plist ,property ,value))) | |
428 | 1197 |
444 | 1198 (defmacro laxremf (lax-plist property) |
1199 "Remove property PROPERTY from lax plist LAX-PLIST. | |
1200 Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROPERTY))." | |
1201 `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,property))) | |
428 | 1202 |
1203 ;;; Error functions | |
1204 | |
442 | 1205 (defun error (datum &rest args) |
1206 "Signal a non-continuable error. | |
1207 DATUM should normally be an error symbol, i.e. a symbol defined using | |
1208 `define-error'. ARGS will be made into a list, and DATUM and ARGS passed | |
1209 as the two arguments to `signal', the most basic error handling function. | |
1210 | |
428 | 1211 This error is not continuable: you cannot continue execution after the |
442 | 1212 error using the debugger `r' command. See also `cerror'. |
1213 | |
1214 The correct semantics of ARGS varies from error to error, but for most | |
1215 errors that need to be generated in Lisp code, the first argument | |
1216 should be a string describing the *context* of the error (i.e. the | |
1217 exact operation being performed and what went wrong), and the remaining | |
1218 arguments or \"frobs\" (most often, there is one) specify the | |
1219 offending object(s) and/or provide additional details such as the exact | |
1220 error when a file error occurred, e.g.: | |
1221 | |
1222 -- the buffer in which an editing error occurred. | |
1223 -- an invalid value that was encountered. (In such cases, the string | |
1224 should describe the purpose or \"semantics\" of the value [e.g. if the | |
1225 value is an argument to a function, the name of the argument; if the value | |
1226 is the value corresponding to a keyword, the name of the keyword; if the | |
1227 value is supposed to be a list length, say this and say what the purpose | |
1228 of the list is; etc.] as well as specifying why the value is invalid, if | |
1229 that's not self-evident.) | |
1230 -- the file in which an error occurred. (In such cases, there should be a | |
1231 second frob, probably a string, specifying the exact error that occurred. | |
1232 This does not occur in the string that precedes the first frob, because | |
1233 that frob describes the exact operation that was happening. | |
1234 | |
1235 For historical compatibility, DATUM can also be a string. In this case, | |
1236 DATUM and ARGS are passed together as the arguments to `format', and then | |
1237 an error is signalled using the error symbol `error' and formatted string. | |
1238 Although this usage of `error' is very common, it is deprecated because it | |
1239 totally defeats the purpose of having structured errors. There is now | |
1240 a rich set of defined errors you can use: | |
1241 | |
563 | 1242 quit |
1243 | |
442 | 1244 error |
1245 invalid-argument | |
563 | 1246 syntax-error |
1247 invalid-read-syntax | |
1248 invalid-regexp | |
1249 structure-formation-error | |
1250 list-formation-error | |
1251 malformed-list | |
1252 malformed-property-list | |
1253 circular-list | |
1254 circular-property-list | |
1255 invalid-function | |
1256 no-catch | |
1257 undefined-keystroke-sequence | |
1258 invalid-constant | |
442 | 1259 wrong-type-argument |
1260 args-out-of-range | |
1261 wrong-number-of-arguments | |
428 | 1262 |
442 | 1263 invalid-state |
1264 void-function | |
1265 cyclic-function-indirection | |
1266 void-variable | |
1267 cyclic-variable-indirection | |
509 | 1268 invalid-byte-code |
563 | 1269 stack-overflow |
1270 out-of-memory | |
1271 invalid-key-binding | |
1272 internal-error | |
442 | 1273 |
1274 invalid-operation | |
1275 invalid-change | |
1276 setting-constant | |
563 | 1277 protected-field |
442 | 1278 editing-error |
1279 beginning-of-buffer | |
1280 end-of-buffer | |
1281 buffer-read-only | |
1282 io-error | |
509 | 1283 file-error |
1284 file-already-exists | |
1285 file-locked | |
1286 file-supersession | |
563 | 1287 end-of-file |
1288 process-error | |
1289 network-error | |
509 | 1290 tooltalk-error |
563 | 1291 gui-error |
1292 dialog-box-error | |
1293 sound-error | |
1294 conversion-error | |
1295 text-conversion-error | |
1296 image-conversion-error | |
1297 base64-conversion-error | |
1298 selection-conversion-error | |
442 | 1299 arith-error |
1300 range-error | |
1301 domain-error | |
1302 singularity-error | |
1303 overflow-error | |
1304 underflow-error | |
509 | 1305 search-failed |
563 | 1306 printing-unreadable-object |
1307 unimplemented | |
509 | 1308 |
563 | 1309 Note the semantic differences between some of the more common errors: |
442 | 1310 |
563 | 1311 -- `invalid-argument' is for all cases where a bad value is encountered. |
1312 -- `invalid-constant' is for arguments where only a specific set of values | |
1313 is allowed. | |
1314 -- `syntax-error' is when complex structures (parsed strings, lists, | |
1315 and the like) are badly formed. If the problem is just a single bad | |
1316 value inside the structure, you should probably be using something else, | |
1317 e.g. `invalid-constant', `wrong-type-argument', or `invalid-argument'. | |
442 | 1318 -- `invalid-state' means that some settings have been changed in such a way |
1319 that their current state is unallowable. More and more, code is being | |
1320 written more carefully, and catches the error when the settings are being | |
1321 changed, rather than afterwards. This leads us to the next error: | |
1322 -- `invalid-change' means that an attempt is being made to change some settings | |
1323 into an invalid state. `invalid-change' is a type of `invalid-operation'. | |
1324 -- `invalid-operation' refers to all cases where code is trying to do something | |
563 | 1325 that's disallowed, or when an error occurred during an operation. (These |
1326 two concepts are merged because there's no clear distinction between them.) | |
1327 -- `io-error' refers to errors involving interaction with any external | |
1328 components (files, other programs, the operating system, etc). | |
442 | 1329 |
1330 See also `cerror', `signal', and `signal-error'." | |
1331 (while t (apply | |
1332 'cerror datum args))) | |
1333 | |
1334 (defun cerror (datum &rest args) | |
428 | 1335 "Like `error' but signals a continuable error." |
442 | 1336 (cond ((stringp datum) |
1337 (signal 'error (list (apply 'format datum args)))) | |
1338 ((defined-error-p datum) | |
1339 (signal datum args)) | |
1340 (t | |
1341 (error 'invalid-argument "datum not string or error symbol" datum)))) | |
428 | 1342 |
1343 (defmacro check-argument-type (predicate argument) | |
1344 "Check that ARGUMENT satisfies PREDICATE. | |
442 | 1345 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue, |
1346 this function signals a continuable `wrong-type-argument' error until the | |
1347 returned value satisfies PREDICATE, and assigns the returned value | |
1348 to ARGUMENT. Otherwise, this function signals a non-continuable | |
1349 `wrong-type-argument' error if the returned value does not satisfy PREDICATE." | |
1350 (if (symbolp argument) | |
1351 `(if (not (,(eval predicate) ,argument)) | |
1352 (setq ,argument | |
1353 (wrong-type-argument ,predicate ,argument))) | |
1354 `(if (not (,(eval predicate) ,argument)) | |
1355 (signal-error 'wrong-type-argument (list ,predicate ,argument))))) | |
428 | 1356 |
872 | 1357 (defun args-out-of-range (value min max) |
1358 "Signal an error until the correct in-range value is given by the user. | |
1359 This function loops, signalling a continuable `args-out-of-range' error | |
1360 with VALUE, MIN and MAX as the data associated with the error and then | |
1361 checking the returned value to make sure it's not outside the given | |
1362 boundaries \(nil for either means no boundary on that side). At that | |
1363 point, the gotten value is returned." | |
1364 (loop | |
1365 for newval = (signal 'args-out-of-range (list value min max)) | |
1366 do (setq value newval) | |
1367 finally return value | |
1368 while (not (argument-in-range-p value min max)))) | |
1369 | |
1370 (defun argument-in-range-p (argument min max) | |
1371 "Return true if ARGUMENT is within the range of [MIN, MAX]. | |
1372 This includes boundaries. nil for either value means no limit on that side." | |
1373 (and (or (not min) (<= min argument)) | |
1374 (or (not max) (<= argument max)))) | |
1375 | |
1376 (defmacro check-argument-range (argument min max) | |
1377 "Check that ARGUMENT is within the range [MIN, MAX]. | |
1378 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue, | |
1379 this function signals a continuable `args-out-of-range' error until the | |
1380 returned value is within range, and assigns the returned value | |
1381 to ARGUMENT. Otherwise, this function signals a non-continuable | |
1382 `args-out-of-range' error if the returned value is out of range." | |
1383 (if (symbolp argument) | |
1384 `(if (not (argument-in-range-p ,argument ,min ,max)) | |
924 | 1385 (setq ,argument |
1386 (args-out-of-range ,argument ,min ,max))) | |
872 | 1387 (let ((newsym (gensym))) |
1388 `(let ((,newsym ,argument)) | |
924 | 1389 (if (not (argument-in-range-p ,newsym ,min ,max)) |
4103 | 1390 (signal-error 'args-out-of-range (list ,newsym ,min ,max))))))) |
872 | 1391 |
428 | 1392 (defun signal-error (error-symbol data) |
1393 "Signal a non-continuable error. Args are ERROR-SYMBOL, and associated DATA. | |
1394 An error symbol is a symbol defined using `define-error'. | |
1395 DATA should be a list. Its elements are printed as part of the error message. | |
1396 If the signal is handled, DATA is made available to the handler. | |
1397 See also `signal', and the functions to handle errors: `condition-case' | |
1398 and `call-with-condition-handler'." | |
1399 (while t | |
1400 (signal error-symbol data))) | |
1401 | |
1402 (defun define-error (error-sym doc-string &optional inherits-from) | |
1403 "Define a new error, denoted by ERROR-SYM. | |
1404 DOC-STRING is an informative message explaining the error, and will be | |
1405 printed out when an unhandled error occurs. | |
1406 ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error'). | |
1407 | |
1408 \[`define-error' internally works by putting on ERROR-SYM an `error-message' | |
1409 property whose value is DOC-STRING, and an `error-conditions' property | |
1410 that is a list of ERROR-SYM followed by each of its super-errors, up | |
1411 to and including `error'. You will sometimes see code that sets this up | |
1412 directly rather than calling `define-error', but you should *not* do this | |
1413 yourself.]" | |
1414 (check-argument-type 'symbolp error-sym) | |
1415 (check-argument-type 'stringp doc-string) | |
1416 (put error-sym 'error-message doc-string) | |
1417 (or inherits-from (setq inherits-from 'error)) | |
1418 (let ((conds (get inherits-from 'error-conditions))) | |
1419 (or conds (signal-error 'error (list "Not an error symbol" error-sym))) | |
1420 (put error-sym 'error-conditions (cons error-sym conds)))) | |
1421 | |
442 | 1422 (defun defined-error-p (sym) |
1423 "Returns non-nil if SYM names a currently-defined error." | |
1424 (and (symbolp sym) (not (null (get sym 'error-conditions))))) | |
1425 | |
793 | 1426 (defun backtrace-in-condition-handler-eliminating-handler (handler-arg-name) |
1427 "Return a backtrace inside of a condition handler, eliminating the handler. | |
1428 This is for use in the condition handler inside of call-with-condition-handler, | |
1429 when written like this: | |
1430 | |
1431 \(call-with-condition-handler | |
1432 #'(lambda (__some_weird_arg__) | |
1433 do the handling ...) | |
1434 #'(lambda () | |
1435 do the stuff that might cause an error)) | |
1436 | |
1437 Pass in the name (a symbol) of the argument used in the lambda function | |
1438 that specifies the handler, and make sure the argument name is unique, and | |
1439 this function generates a backtrace and strips off the part above where the | |
1440 error occurred (i.e. the handler itself)." | |
1441 (let* ((bt (with-output-to-string (backtrace nil t))) | |
1442 (bt (save-match-data | |
1443 ;; Try to eliminate the part of the backtrace | |
1444 ;; above where the error occurred. | |
1445 (if (string-match | |
1446 (concat "bind (\\(?:.* \\)?" (symbol-name handler-arg-name) | |
1447 "\\(?:.* \\)?)[ \t\n]*\\(?:(lambda \\|#<compiled-function \\)(" | |
1448 (symbol-name handler-arg-name) | |
1449 ").*\n\\(\\(?:.\\|\n\\)*\\)$") | |
1450 bt) (match-string 1 bt) bt)))) | |
1451 bt)) | |
1452 | |
1453 (put 'with-trapping-errors 'lisp-indent-function 0) | |
1454 (defmacro with-trapping-errors (&rest keys-body) | |
1455 "Trap errors in BODY, outputting a warning and a backtrace. | |
1456 Usage looks like | |
1457 | |
1458 \(with-trapping-errors | |
1459 [:operation OPERATION] | |
1460 [:error-form ERROR-FORM] | |
1461 [:no-backtrace NO-BACKTRACE] | |
1462 [:class CLASS] | |
1463 [:level LEVEL] | |
1464 [:resignal RESIGNAL] | |
1465 BODY) | |
1466 | |
1467 Return value without error is whatever BODY returns. With error, return | |
1468 result of ERROR-FORM (which will be evaluated only when the error actually | |
1469 occurs), which defaults to nil. OPERATION is given in the warning message. | |
1470 CLASS and LEVEL are the warning class and level (default to class | |
1471 `general', level `warning'). If NO-BACKTRACE is given, no backtrace is | |
1472 displayed. If RESIGNAL is given, the error is resignaled after the warning | |
1473 is displayed and the ERROR-FORM is executed." | |
1474 (let ((operation "unknown") | |
1475 (error-form nil) | |
1476 (no-backtrace nil) | |
1477 (class ''general) | |
1478 (level ''warning) | |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1479 (resignal nil) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1480 (cte-cc-var '#:cte-cc-var) |
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1481 (call-trapping-errors-arg '#:call-trapping-errors-Ldc9FC5Hr)) |
793 | 1482 (let* ((keys '(operation error-form no-backtrace class level resignal)) |
1483 (keys-with-colon | |
1484 (mapcar #'(lambda (sym) | |
1485 (intern (concat ":" (symbol-name sym)))) keys))) | |
1486 (while (memq (car keys-body) keys-with-colon) | |
1487 (let* ((key-with-colon (pop keys-body)) | |
1488 (key (intern (substring (symbol-name key-with-colon) 1)))) | |
1489 (set key (pop keys-body))))) | |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1490 `(condition-case ,(if resignal cte-cc-var nil) |
793 | 1491 (call-with-condition-handler |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1492 #'(lambda (,call-trapping-errors-arg) |
793 | 1493 (let ((errstr (error-message-string |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1494 ,call-trapping-errors-arg))) |
793 | 1495 ,(if no-backtrace |
1496 `(lwarn ,class ,level | |
1497 (if (warning-level-< | |
1498 ,level | |
1499 display-warning-minimum-level) | |
1500 "Error in %s: %s" | |
1501 "Error in %s:\n%s\n") | |
1502 ,operation errstr) | |
1503 `(lwarn ,class ,level | |
1504 "Error in %s: %s\n\nBacktrace follows:\n\n%s" | |
1505 ,operation errstr | |
1506 (backtrace-in-condition-handler-eliminating-handler | |
4806
fd36a980d701
Use uninterned symbols in various information-hiding contexts.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4575
diff
changeset
|
1507 ',call-trapping-errors-arg))))) |
793 | 1508 #'(lambda () |
1509 (progn ,@keys-body))) | |
1510 (error | |
1511 ,error-form | |
4817
0142cb4d1049
Fix a bug I introduced in #'with-trapping-errors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4806
diff
changeset
|
1512 ,@(if resignal `((signal (car ,cte-cc-var) (cdr ,cte-cc-var))))) |
793 | 1513 ))) |
1514 | |
428 | 1515 ;;;; Miscellanea. |
1516 | |
1517 ;; This is now in C. | |
444 | 1518 ;(defun buffer-substring-no-properties (start end) |
1519 ; "Return the text from START to END, without text properties, as a string." | |
1520 ; (let ((string (buffer-substring start end))) | |
428 | 1521 ; (set-text-properties 0 (length string) nil string) |
1522 ; string)) | |
1523 | |
1524 (defun get-buffer-window-list (&optional buffer minibuf frame) | |
1525 "Return windows currently displaying BUFFER, or nil if none. | |
1526 BUFFER defaults to the current buffer. | |
1527 See `walk-windows' for the meaning of MINIBUF and FRAME." | |
1528 (cond ((null buffer) | |
1529 (setq buffer (current-buffer))) | |
1530 ((not (bufferp buffer)) | |
1531 (setq buffer (get-buffer buffer)))) | |
1532 (let (windows) | |
1533 (walk-windows (lambda (window) | |
1534 (if (eq (window-buffer window) buffer) | |
1535 (push window windows))) | |
1536 minibuf frame) | |
1537 windows)) | |
1538 | |
1539 (defun ignore (&rest ignore) | |
1540 "Do nothing and return nil. | |
1541 This function accepts any number of arguments, but ignores them." | |
1542 (interactive) | |
1543 nil) | |
1544 | |
883 | 1545 ;; defined in lisp/bindings.el in GNU Emacs. |
1546 (defmacro bound-and-true-p (var) | |
1547 "Return the value of symbol VAR if it is bound, else nil." | |
1548 `(and (boundp (quote ,var)) ,var)) | |
1549 | |
1550 ;; `propertize' is a builtin in GNU Emacs 21. | |
1551 (defun propertize (string &rest properties) | |
1552 "Return a copy of STRING with text properties added. | |
1553 First argument is the string to copy. | |
1554 Remaining arguments form a sequence of PROPERTY VALUE pairs for text | |
1555 properties to add to the result." | |
1556 (let ((str (copy-sequence string))) | |
1557 (add-text-properties 0 (length str) | |
1558 properties | |
1559 str) | |
1560 str)) | |
1561 | |
1562 ;; `delete-and-extract-region' is a builtin in GNU Emacs 21. | |
1563 (defun delete-and-extract-region (start end) | |
1564 "Delete the text between START and END and return it." | |
1565 (let ((region (buffer-substring start end))) | |
1566 (delete-region start end) | |
1567 region)) | |
1568 | |
428 | 1569 (define-function 'eval-in-buffer 'with-current-buffer) |
1570 (make-obsolete 'eval-in-buffer 'with-current-buffer) | |
1571 | |
1572 ;;; The real defn is in abbrev.el but some early callers | |
1573 ;;; (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded... | |
1574 | |
1575 (if (not (fboundp 'define-abbrev-table)) | |
1576 (progn | |
1577 (setq abbrev-table-name-list '()) | |
924 | 1578 (fset 'define-abbrev-table |
1579 (function (lambda (name defs) | |
1580 ;; These are fixed-up when abbrev.el loads. | |
1581 (setq abbrev-table-name-list | |
1582 (cons (cons name defs) | |
1583 abbrev-table-name-list))))))) | |
428 | 1584 |
1585 ;;; `functionp' has been moved into C. | |
1586 | |
1587 ;;(defun functionp (object) | |
1588 ;; "Non-nil if OBJECT can be called as a function." | |
1589 ;; (or (and (symbolp object) (fboundp object)) | |
1590 ;; (subrp object) | |
1591 ;; (compiled-function-p object) | |
1592 ;; (eq (car-safe object) 'lambda))) | |
1593 | |
1594 (defun function-interactive (function) | |
1595 "Return the interactive specification of FUNCTION. | |
1596 FUNCTION can be any funcallable object. | |
1597 The specification will be returned as the list of the symbol `interactive' | |
1598 and the specs. | |
1599 If FUNCTION is not interactive, nil will be returned." | |
1600 (setq function (indirect-function function)) | |
1601 (cond ((compiled-function-p function) | |
1602 (compiled-function-interactive function)) | |
1603 ((subrp function) | |
1604 (subr-interactive function)) | |
1605 ((eq (car-safe function) 'lambda) | |
1606 (let ((spec (if (stringp (nth 2 function)) | |
1607 (nth 3 function) | |
1608 (nth 2 function)))) | |
1609 (and (eq (car-safe spec) 'interactive) | |
1610 spec))) | |
1611 (t | |
1612 (error "Non-funcallable object: %s" function)))) | |
1613 | |
442 | 1614 (defun function-allows-args (function n) |
1615 "Return whether FUNCTION can be called with N arguments." | |
1616 (and (<= (function-min-args function) n) | |
1617 (or (null (function-max-args function)) | |
1618 (<= n (function-max-args function))))) | |
1619 | |
428 | 1620 ;; This function used to be an alias to `buffer-substring', except |
1621 ;; that FSF Emacs 20.4 added a BUFFER argument in an incompatible way. | |
1622 ;; The new FSF's semantics makes more sense, but we try to support | |
1623 ;; both for backward compatibility. | |
1624 (defun buffer-string (&optional buffer old-end old-buffer) | |
1625 "Return the contents of the current buffer as a string. | |
1626 If narrowing is in effect, this function returns only the visible part | |
1627 of the buffer. | |
1628 | |
1629 If BUFFER is specified, the contents of that buffer are returned. | |
1630 | |
1631 The arguments OLD-END and OLD-BUFFER are supported for backward | |
1632 compatibility with pre-21.2 XEmacsen times when arguments to this | |
1633 function were (buffer-string &optional START END BUFFER)." | |
1634 (cond | |
1635 ((or (stringp buffer) (bufferp buffer)) | |
1636 ;; Most definitely the new way. | |
1637 (buffer-substring nil nil buffer)) | |
1638 ((or (stringp old-buffer) (bufferp old-buffer) | |
1639 (natnump buffer) (natnump old-end)) | |
1640 ;; Definitely the old way. | |
1641 (buffer-substring buffer old-end old-buffer)) | |
1642 (t | |
1643 ;; Probably the old way. | |
1644 (buffer-substring buffer old-end old-buffer)))) | |
1645 | |
1333 | 1646 ;; BEGIN SYNC WITH FSF 21.2 |
1647 | |
428 | 1648 ;; This was not present before. I think Jamie had some objections |
1649 ;; to this, so I'm leaving this undefined for now. --ben | |
1650 | |
1651 ;;; The objection is this: there is more than one way to load the same file. | |
1652 ;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different | |
1653 ;;; ways to load the exact same code. `eval-after-load' is too stupid to | |
1654 ;;; deal with this sort of thing. If this sort of feature is desired, then | |
1655 ;;; it should work off of a hook on `provide'. Features are unique and | |
1656 ;;; the arguments to (load) are not. --Stig | |
1657 | |
1658 ;; We provide this for FSFmacs compatibility, at least until we devise | |
1659 ;; something better. | |
1660 | |
1661 ;;;; Specifying things to do after certain files are loaded. | |
1662 | |
1663 (defun eval-after-load (file form) | |
1664 "Arrange that, if FILE is ever loaded, FORM will be run at that time. | |
1665 This makes or adds to an entry on `after-load-alist'. | |
1666 If FILE is already loaded, evaluate FORM right now. | |
1667 It does nothing if FORM is already on the list for FILE. | |
1333 | 1668 FILE must match exactly. Normally FILE is the name of a library, |
1669 with no directory or extension specified, since that is how `load' | |
1670 is normally called." | |
1671 ;; Make sure `load-history' contains the files dumped with Emacs | |
1672 ;; for the case that FILE is one of the files dumped with Emacs. | |
1673 (if-fboundp 'load-symbol-file-load-history | |
1674 (load-symbol-file-load-history)) | |
428 | 1675 ;; Make sure there is an element for FILE. |
1676 (or (assoc file after-load-alist) | |
1677 (setq after-load-alist (cons (list file) after-load-alist))) | |
1678 ;; Add FORM to the element if it isn't there. | |
1679 (let ((elt (assoc file after-load-alist))) | |
1680 (or (member form (cdr elt)) | |
1681 (progn | |
1682 (nconc elt (list form)) | |
1683 ;; If the file has been loaded already, run FORM right away. | |
1684 (and (assoc file load-history) | |
1685 (eval form))))) | |
1686 form) | |
1687 (make-compatible 'eval-after-load "") | |
1688 | |
1689 (defun eval-next-after-load (file) | |
1690 "Read the following input sexp, and run it whenever FILE is loaded. | |
1691 This makes or adds to an entry on `after-load-alist'. | |
1692 FILE should be the name of a library, with no directory name." | |
1693 (eval-after-load file (read))) | |
1694 (make-compatible 'eval-next-after-load "") | |
1695 | |
1333 | 1696 ;; END SYNC WITH FSF 21.2 |
428 | 1697 |
3000 | 1698 ;; BEGIN SYNC WITH FSF 22.0.50.1 (CVS) |
1699 (defun delete-dups (list) | |
1700 "Destructively remove `equal' duplicates from LIST. | |
1701 Store the result in LIST and return it. LIST must be a proper list. | |
1702 Of several `equal' occurrences of an element in LIST, the first | |
1703 one is kept." | |
1704 (let ((tail list)) | |
1705 (while tail | |
1706 (setcdr tail (delete (car tail) (cdr tail))) | |
1707 (setq tail (cdr tail)))) | |
1708 list) | |
1709 | |
1710 ;; END SYNC WITH FSF 22.0.50.1 (CVS) | |
1711 | |
2525 | 1712 ;; (defun shell-quote-argument (argument) in process.el. |
1713 | |
1714 ;; (defun make-syntax-table (&optional oldtable) in syntax.el. | |
1715 | |
4575
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1716 ;; (defun syntax-after (pos) in syntax.el. |
2525 | 1717 |
1718 ;; global-set-key, local-set-key, global-unset-key, local-unset-key in | |
1719 ;; keymap.el. | |
1720 | |
1721 ;; frame-configuration-p is in frame.el. | |
1722 | |
1723 ;; functionp is built-in. | |
1724 | |
1725 ;; interactive-form in obsolete.el. | |
1726 | |
1727 ;; assq-del-all in obsolete.el. | |
1728 | |
4266 | 1729 ;; make-temp-file in files.el. |
2525 | 1730 |
1731 ;; add-minor-mode in modeline.el. | |
1732 | |
1733 ;; text-clone stuff #### doesn't exist; should go in text-props.el and | |
1734 ;; requires changes to extents.c (modification hooks). | |
1735 | |
1736 ;; play-sound is built-in. | |
1737 | |
1738 ;; define-mail-user-agent is in simple.el. | |
1739 | |
4501
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1740 ;; XEmacs; added. |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1741 (defun skip-chars-quote (string) |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1742 "Return a string that means all characters in STRING will be skipped, |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1743 if passed to `skip-chars-forward' or `skip-chars-backward'. |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1744 |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1745 Ranges and carets are not treated specially. This implementation is |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1746 in Lisp; do not use it in performance-critical code." |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1747 (let ((list (delete-duplicates (string-to-list string) :test #'=))) |
4504
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1748 (when (/= 1 (length list)) ;; No quoting needed in a string of length 1. |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1749 (when (eq ?^ (car list)) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1750 (setq list (nconc (cdr list) '(?^)))) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1751 (when (memq ?\\ list) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1752 (setq list (delq ?\\ list) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1753 list (nconc (list ?\\ ?\\) list))) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1754 (when (memq ?- list) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1755 (setq list (delq ?- list) |
b82fdf7305ee
Correct the implementation, add a few basic tests for #'skip-chars-quote.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4501
diff
changeset
|
1756 list (nconc list '(?\\ ?-))))) |
4501
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1757 (apply #'string list))) |
c4fd85dd95bd
Add #'skip-chars-quote to subr.el
Aidan Kehoe <kehoea@parhasard.net>
parents:
4463
diff
changeset
|
1758 |
4575
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1759 ;; XEmacs addition to subr.el; docstring and API taken initially from GNU's |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1760 ;; data.c, revision 1.275, GPLv2. |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1761 (defun subr-arity (subr) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1762 "Return minimum and maximum number of args allowed for SUBR. |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1763 SUBR must be a built-in function (not just a symbol that refers to one). |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1764 The returned value is a pair (MIN . MAX). MIN is the minimum number |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1765 of args. MAX is the maximum number or the symbol `many', for a |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1766 function with `&rest' args, or `unevalled' for a special form. |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1767 |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1768 See also `special-form-p', `subr-min-args', `subr-max-args', |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1769 `function-allows-args'. " |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1770 (check-argument-type #'subrp subr) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1771 (cons (subr-min-args subr) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1772 (cond |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1773 ((special-form-p subr) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1774 'unevalled) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1775 ((null (subr-max-args subr)) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1776 'many) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1777 (t (subr-max-args subr))))) |
eecd28508f4a
Add #'subr-arity, API taken from GNU, implementation our own.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4516
diff
changeset
|
1778 |
428 | 1779 ;;; subr.el ends here |