comparison lisp/subr.el @ 89909:68c22ea6027c

Sync to HEAD
author Kenichi Handa <handa@m17n.org>
date Fri, 16 Apr 2004 12:51:06 +0000
parents 375f2633d815
children 4c90ffeb71c5
comparison
equal deleted inserted replaced
89908:ee1402f7b568 89909:68c22ea6027c
1 ;;; subr.el --- basic lisp subroutines for Emacs 1 ;;; subr.el --- basic lisp subroutines for Emacs
2 2
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 2003 3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 03, 2004
4 ;; Free Software Foundation, Inc. 4 ;; Free Software Foundation, Inc.
5 5
6 ;; Maintainer: FSF 6 ;; Maintainer: FSF
7 ;; Keywords: internal 7 ;; Keywords: internal
8 8
60 60
61 ;;;; Lisp language features. 61 ;;;; Lisp language features.
62 62
63 (defalias 'not 'null) 63 (defalias 'not 'null)
64 64
65 (defmacro noreturn (form)
66 "Evaluates FORM, with the expectation that the evaluation will signal an error
67 instead of returning to its caller. If FORM does return, an error is
68 signalled."
69 `(prog1 ,form
70 (error "Form marked with `noreturn' did return")))
71
72 (defmacro 1value (form)
73 "Evaluates FORM, with the expectation that all the same value will be returned
74 from all evaluations of FORM. This is the global do-nothing
75 version of `1value'. There is also `testcover-1value' that
76 complains if FORM ever does return differing values."
77 form)
78
65 (defmacro lambda (&rest cdr) 79 (defmacro lambda (&rest cdr)
66 "Return a lambda expression. 80 "Return a lambda expression.
67 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is 81 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
68 self-quoting; the result of evaluating the lambda expression is the 82 self-quoting; the result of evaluating the lambda expression is the
69 expression itself. The lambda expression may then be treated as a 83 expression itself. The lambda expression may then be treated as a
141 (,(car spec) ,start)) 155 (,(car spec) ,start))
142 (while (< ,(car spec) ,temp) 156 (while (< ,(car spec) ,temp)
143 ,@body 157 ,@body
144 (setq ,(car spec) (1+ ,(car spec)))) 158 (setq ,(car spec) (1+ ,(car spec))))
145 ,@(cdr (cdr spec))))) 159 ,@(cdr (cdr spec)))))
160
161 (defmacro declare (&rest specs)
162 "Do not evaluate any arguments and return nil.
163 Treated as a declaration when used at the right place in a
164 `defmacro' form. \(See Info anchor `(elisp)Definition of declare'."
165 nil)
146 166
147 (defsubst caar (x) 167 (defsubst caar (x)
148 "Return the car of the car of X." 168 "Return the car of the car of X."
149 (car (car x))) 169 (car (car x)))
150 170
187 (and (< n m) 207 (and (< n m)
188 (progn 208 (progn
189 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil)) 209 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
190 x)))) 210 x))))
191 211
212 (defun delete-dups (list)
213 "Destructively remove `equal' duplicates from LIST.
214 Store the result in LIST and return it. LIST must be a proper list.
215 Of several `equal' occurrences of an element in LIST, the first
216 one is kept."
217 (let ((tail list))
218 (while tail
219 (setcdr tail (delete (car tail) (cdr tail)))
220 (setq tail (cdr tail))))
221 list)
222
192 (defun number-sequence (from &optional to inc) 223 (defun number-sequence (from &optional to inc)
193 "Return a sequence of numbers from FROM to TO (both inclusive) as a list. 224 "Return a sequence of numbers from FROM to TO (both inclusive) as a list.
194 INC is the increment used between numbers in the sequence. 225 INC is the increment used between numbers in the sequence and defaults to 1.
195 So, the Nth element of the list is (+ FROM (* N INC)) where N counts from 226 So, the Nth element of the list is \(+ FROM \(* N INC)) where N counts from
196 zero. 227 zero. TO is only included if there is an N for which TO = FROM + N * INC.
197 If INC is nil, it defaults to 1 (one). 228 If TO is nil or numerically equal to FROM, return \(FROM).
198 If TO is nil, it defaults to FROM. 229 If INC is positive and TO is less than FROM, or INC is negative
199 If TO is less than FROM, the value is nil. 230 and TO is larger than FROM, return nil.
200 Note that FROM, TO and INC can be integer or float." 231 If INC is zero and TO is neither nil nor numerically equal to
201 (if (not to) 232 FROM, signal an error.
233
234 This function is primarily designed for integer arguments.
235 Nevertheless, FROM, TO and INC can be integer or float. However,
236 floating point arithmetic is inexact. For instance, depending on
237 the machine, it may quite well happen that
238 \(number-sequence 0.4 0.6 0.2) returns the one element list \(0.4),
239 whereas \(number-sequence 0.4 0.8 0.2) returns a list with three
240 elements. Thus, if some of the arguments are floats and one wants
241 to make sure that TO is included, one may have to explicitly write
242 TO as \(+ FROM \(* N INC)) or use a variable whose value was
243 computed with this exact expression. Alternatively, you can,
244 of course, also replace TO with a slightly larger value
245 \(or a slightly more negative value if INC is negative)."
246 (if (or (not to) (= from to))
202 (list from) 247 (list from)
203 (or inc (setq inc 1)) 248 (or inc (setq inc 1))
204 (let (seq) 249 (when (zerop inc) (error "The increment can not be zero"))
205 (while (<= from to) 250 (let (seq (n 0) (next from))
206 (setq seq (cons from seq) 251 (if (> inc 0)
207 from (+ from inc))) 252 (while (<= next to)
253 (setq seq (cons next seq)
254 n (1+ n)
255 next (+ from (* n inc))))
256 (while (>= next to)
257 (setq seq (cons next seq)
258 n (1+ n)
259 next (+ from (* n inc)))))
208 (nreverse seq)))) 260 (nreverse seq))))
209 261
210 (defun remove (elt seq) 262 (defun remove (elt seq)
211 "Return a copy of SEQ with all occurrences of ELT removed. 263 "Return a copy of SEQ with all occurrences of ELT removed.
212 SEQ must be a list, vector, or string. The comparison is done with `equal'." 264 SEQ must be a list, vector, or string. The comparison is done with `equal'."
261 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key) 313 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
262 (setq found t value (if (consp elt) (cdr elt) default)))) 314 (setq found t value (if (consp elt) (cdr elt) default))))
263 (setq tail (cdr tail))) 315 (setq tail (cdr tail)))
264 value)) 316 value))
265 317
318 (make-obsolete 'assoc-ignore-case 'assoc-string)
266 (defun assoc-ignore-case (key alist) 319 (defun assoc-ignore-case (key alist)
267 "Like `assoc', but ignores differences in case and text representation. 320 "Like `assoc', but ignores differences in case and text representation.
268 KEY must be a string. Upper-case and lower-case letters are treated as equal. 321 KEY must be a string. Upper-case and lower-case letters are treated as equal.
269 Unibyte strings are converted to multibyte for comparison." 322 Unibyte strings are converted to multibyte for comparison."
270 (let (element) 323 (assoc-string key alist t))
271 (while (and alist (not element)) 324
272 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t)) 325 (make-obsolete 'assoc-ignore-representation 'assoc-string)
273 (setq element (car alist)))
274 (setq alist (cdr alist)))
275 element))
276
277 (defun assoc-ignore-representation (key alist) 326 (defun assoc-ignore-representation (key alist)
278 "Like `assoc', but ignores differences in text representation. 327 "Like `assoc', but ignores differences in text representation.
279 KEY must be a string. 328 KEY must be a string.
280 Unibyte strings are converted to multibyte for comparison." 329 Unibyte strings are converted to multibyte for comparison."
281 (let (element) 330 (assoc-string key alist nil))
282 (while (and alist (not element))
283 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
284 (setq element (car alist)))
285 (setq alist (cdr alist)))
286 element))
287 331
288 (defun member-ignore-case (elt list) 332 (defun member-ignore-case (elt list)
289 "Like `member', but ignores differences in case and text representation. 333 "Like `member', but ignores differences in case and text representation.
290 ELT must be a string. Upper-case and lower-case letters are treated as equal. 334 ELT must be a string. Upper-case and lower-case letters are treated as equal.
291 Unibyte strings are converted to multibyte for comparison. 335 Unibyte strings are converted to multibyte for comparison.
632 (and (consp object) 676 (and (consp object)
633 (eq (car object) 'mouse-movement))) 677 (eq (car object) 'mouse-movement)))
634 678
635 (defsubst event-start (event) 679 (defsubst event-start (event)
636 "Return the starting position of EVENT. 680 "Return the starting position of EVENT.
637 If EVENT is a mouse press or a mouse click, this returns the location 681 If EVENT is a mouse or key press or a mouse click, this returns the location
638 of the event. 682 of the event.
639 If EVENT is a drag, this returns the drag's starting position. 683 If EVENT is a drag, this returns the drag's starting position.
640 The return value is of the form 684 The return value is of the form
641 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 685 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
686 IMAGE (DX . DY) (WIDTH . HEIGHT))
642 The `posn-' functions access elements of such lists." 687 The `posn-' functions access elements of such lists."
643 (if (consp event) (nth 1 event) 688 (if (consp event) (nth 1 event)
644 (list (selected-window) (point) '(0 . 0) 0))) 689 (list (selected-window) (point) '(0 . 0) 0)))
645 690
646 (defsubst event-end (event) 691 (defsubst event-end (event)
647 "Return the ending location of EVENT. EVENT should be a click or drag event. 692 "Return the ending location of EVENT.
693 EVENT should be a click, drag, or key press event.
648 If EVENT is a click event, this function is the same as `event-start'. 694 If EVENT is a click event, this function is the same as `event-start'.
649 The return value is of the form 695 The return value is of the form
650 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 696 (WINDOW AREA-OR-POS (X . Y) TIMESTAMP OBJECT POS (COL . ROW)
697 IMAGE (DX . DY) (WIDTH . HEIGHT))
651 The `posn-' functions access elements of such lists." 698 The `posn-' functions access elements of such lists."
652 (if (consp event) (nth (if (consp (nth 2 event)) 2 1) event) 699 (if (consp event) (nth (if (consp (nth 2 event)) 2 1) event)
653 (list (selected-window) (point) '(0 . 0) 0))) 700 (list (selected-window) (point) '(0 . 0) 0)))
654 701
655 (defsubst event-click-count (event) 702 (defsubst event-click-count (event)
657 The return value is a positive integer." 704 The return value is a positive integer."
658 (if (and (consp event) (integerp (nth 2 event))) (nth 2 event) 1)) 705 (if (and (consp event) (integerp (nth 2 event))) (nth 2 event) 1))
659 706
660 (defsubst posn-window (position) 707 (defsubst posn-window (position)
661 "Return the window in POSITION. 708 "Return the window in POSITION.
662 POSITION should be a list of the form 709 POSITION should be a list of the form returned by the `event-start'
663 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 710 and `event-end' functions."
664 as returned by the `event-start' and `event-end' functions."
665 (nth 0 position)) 711 (nth 0 position))
712
713 (defsubst posn-area (position)
714 "Return the window area recorded in POSITION, or nil for the text area.
715 POSITION should be a list of the form returned by the `event-start'
716 and `event-end' functions."
717 (let ((area (if (consp (nth 1 position))
718 (car (nth 1 position))
719 (nth 1 position))))
720 (and (symbolp area) area)))
666 721
667 (defsubst posn-point (position) 722 (defsubst posn-point (position)
668 "Return the buffer location in POSITION. 723 "Return the buffer location in POSITION.
669 POSITION should be a list of the form 724 POSITION should be a list of the form returned by the `event-start'
670 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 725 and `event-end' functions."
671 as returned by the `event-start' and `event-end' functions." 726 (or (nth 5 position)
672 (if (consp (nth 1 position)) 727 (if (consp (nth 1 position))
673 (car (nth 1 position)) 728 (car (nth 1 position))
674 (nth 1 position))) 729 (nth 1 position))))
730
731 (defun posn-set-point (position)
732 "Move point to POSITION.
733 Select the corresponding window as well."
734 (if (not (windowp (posn-window position)))
735 (error "Position not in text area of window"))
736 (select-window (posn-window position))
737 (if (numberp (posn-point position))
738 (goto-char (posn-point position))))
675 739
676 (defsubst posn-x-y (position) 740 (defsubst posn-x-y (position)
677 "Return the x and y coordinates in POSITION. 741 "Return the x and y coordinates in POSITION.
678 POSITION should be a list of the form 742 POSITION should be a list of the form returned by the `event-start'
679 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 743 and `event-end' functions."
680 as returned by the `event-start' and `event-end' functions."
681 (nth 2 position)) 744 (nth 2 position))
682 745
683 (defun posn-col-row (position) 746 (defun posn-col-row (position)
684 "Return the column and row in POSITION, measured in characters. 747 "Return the nominal column and row in POSITION, measured in characters.
685 POSITION should be a list of the form 748 The column and row values are approximations calculated from the x
686 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 749 and y coordinates in POSITION and the frame's default character width
687 as returned by the `event-start' and `event-end' functions. 750 and height.
688 For a scroll-bar event, the result column is 0, and the row 751 For a scroll-bar event, the result column is 0, and the row
689 corresponds to the vertical position of the click in the scroll bar." 752 corresponds to the vertical position of the click in the scroll bar.
690 (let* ((pair (nth 2 position)) 753 POSITION should be a list of the form returned by the `event-start'
691 (window (posn-window position))) 754 and `event-end' functions."
692 (if (eq (if (consp (nth 1 position)) 755 (let* ((pair (posn-x-y position))
693 (car (nth 1 position)) 756 (window (posn-window position))
694 (nth 1 position)) 757 (area (posn-area position)))
695 'vertical-scroll-bar) 758 (cond
696 (cons 0 (scroll-bar-scale pair (1- (window-height window)))) 759 ((null window)
697 (if (eq (if (consp (nth 1 position)) 760 '(0 . 0))
698 (car (nth 1 position)) 761 ((eq area 'vertical-scroll-bar)
699 (nth 1 position)) 762 (cons 0 (scroll-bar-scale pair (1- (window-height window)))))
700 'horizontal-scroll-bar) 763 ((eq area 'horizontal-scroll-bar)
701 (cons (scroll-bar-scale pair (window-width window)) 0) 764 (cons (scroll-bar-scale pair (window-width window)) 0))
702 (let* ((frame (if (framep window) window (window-frame window))) 765 (t
703 (x (/ (car pair) (frame-char-width frame))) 766 (let* ((frame (if (framep window) window (window-frame window)))
704 (y (/ (cdr pair) (+ (frame-char-height frame) 767 (x (/ (car pair) (frame-char-width frame)))
705 (or (frame-parameter frame 'line-spacing) 768 (y (/ (cdr pair) (+ (frame-char-height frame)
706 default-line-spacing 769 (or (frame-parameter frame 'line-spacing)
707 0))))) 770 default-line-spacing
708 (cons x y)))))) 771 0)))))
772 (cons x y))))))
773
774 (defun posn-actual-col-row (position)
775 "Return the actual column and row in POSITION, measured in characters.
776 These are the actual row number in the window and character number in that row.
777 Return nil if POSITION does not contain the actual position; in that case
778 `posn-col-row' can be used to get approximate values.
779 POSITION should be a list of the form returned by the `event-start'
780 and `event-end' functions."
781 (nth 6 position))
709 782
710 (defsubst posn-timestamp (position) 783 (defsubst posn-timestamp (position)
711 "Return the timestamp of POSITION. 784 "Return the timestamp of POSITION.
712 POSITION should be a list of the form 785 POSITION should be a list of the form returned by the `event-start'
713 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) 786 and `event-end' functions."
714 as returned by the `event-start' and `event-end' functions."
715 (nth 3 position)) 787 (nth 3 position))
788
789 (defsubst posn-string (position)
790 "Return the string object of POSITION, or nil if a buffer position.
791 POSITION should be a list of the form returned by the `event-start'
792 and `event-end' functions."
793 (nth 4 position))
794
795 (defsubst posn-image (position)
796 "Return the image object of POSITION, or nil if a not an image.
797 POSITION should be a list of the form returned by the `event-start'
798 and `event-end' functions."
799 (nth 7 position))
800
801 (defsubst posn-object (position)
802 "Return the object (image or string) of POSITION.
803 POSITION should be a list of the form returned by the `event-start'
804 and `event-end' functions."
805 (or (posn-image position) (posn-string position)))
806
807 (defsubst posn-object-x-y (position)
808 "Return the x and y coordinates relative to the object of POSITION.
809 POSITION should be a list of the form returned by the `event-start'
810 and `event-end' functions."
811 (nth 8 position))
812
813 (defsubst posn-object-width-height (position)
814 "Return the pixel width and height of the object of POSITION.
815 POSITION should be a list of the form returned by the `event-start'
816 and `event-end' functions."
817 (nth 9 position))
716 818
717 819
718 ;;;; Obsolescent names for functions. 820 ;;;; Obsolescent names for functions.
719 821
720 (defalias 'dot 'point) 822 (defalias 'dot 'point)
876 HOOK should be a symbol, and FUNCTION may be any valid function. If 978 HOOK should be a symbol, and FUNCTION may be any valid function. If
877 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the 979 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
878 list of hooks to run in HOOK, then nothing is done. See `add-hook'. 980 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
879 981
880 The optional third argument, LOCAL, if non-nil, says to modify 982 The optional third argument, LOCAL, if non-nil, says to modify
881 the hook's buffer-local value rather than its default value. 983 the hook's buffer-local value rather than its default value."
882 This makes the hook buffer-local if needed."
883 (or (boundp hook) (set hook nil)) 984 (or (boundp hook) (set hook nil))
884 (or (default-boundp hook) (set-default hook nil)) 985 (or (default-boundp hook) (set-default hook nil))
885 (if local (unless (local-variable-if-set-p hook) 986 ;; Do nothing if LOCAL is t but this hook has no local binding.
886 (set (make-local-variable hook) (list t))) 987 (unless (and local (not (local-variable-p hook)))
887 ;; Detect the case where make-local-variable was used on a hook 988 ;; Detect the case where make-local-variable was used on a hook
888 ;; and do what we used to do. 989 ;; and do what we used to do.
889 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) 990 (when (and (local-variable-p hook)
890 (setq local t))) 991 (not (and (consp (symbol-value hook))
891 (let ((hook-value (if local (symbol-value hook) (default-value hook)))) 992 (memq t (symbol-value hook)))))
892 ;; Remove the function, for both the list and the non-list cases. 993 (setq local t))
893 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda)) 994 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
894 (if (equal hook-value function) (setq hook-value nil)) 995 ;; Remove the function, for both the list and the non-list cases.
895 (setq hook-value (delete function (copy-sequence hook-value)))) 996 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
896 ;; If the function is on the global hook, we need to shadow it locally 997 (if (equal hook-value function) (setq hook-value nil))
897 ;;(when (and local (member function (default-value hook)) 998 (setq hook-value (delete function (copy-sequence hook-value))))
898 ;; (not (member (cons 'not function) hook-value))) 999 ;; If the function is on the global hook, we need to shadow it locally
899 ;; (push (cons 'not function) hook-value)) 1000 ;;(when (and local (member function (default-value hook))
900 ;; Set the actual variable 1001 ;; (not (member (cons 'not function) hook-value)))
901 (if (not local) 1002 ;; (push (cons 'not function) hook-value))
902 (set-default hook hook-value) 1003 ;; Set the actual variable
903 (if (equal hook-value '(t)) 1004 (if (not local)
904 (kill-local-variable hook) 1005 (set-default hook hook-value)
905 (set hook hook-value))))) 1006 (if (equal hook-value '(t))
1007 (kill-local-variable hook)
1008 (set hook hook-value))))))
906 1009
907 (defun add-to-list (list-var element &optional append) 1010 (defun add-to-list (list-var element &optional append)
908 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. 1011 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
909 The test for presence of ELEMENT is done with `equal'. 1012 The test for presence of ELEMENT is done with `equal'.
910 If ELEMENT is added, it is added at the beginning of the list, 1013 If ELEMENT is added, it is added at the beginning of the list,
1178 (while (not success) 1281 (while (not success)
1179 (let ((first (read-passwd prompt nil default)) 1282 (let ((first (read-passwd prompt nil default))
1180 (second (read-passwd "Confirm password: " nil default))) 1283 (second (read-passwd "Confirm password: " nil default)))
1181 (if (equal first second) 1284 (if (equal first second)
1182 (progn 1285 (progn
1183 (and (arrayp second) (fillarray second ?\0)) 1286 (and (arrayp second) (clear-string second))
1184 (setq success first)) 1287 (setq success first))
1185 (and (arrayp first) (fillarray first ?\0)) 1288 (and (arrayp first) (clear-string first))
1186 (and (arrayp second) (fillarray second ?\0)) 1289 (and (arrayp second) (clear-string second))
1187 (message "Password not repeated accurately; please start over") 1290 (message "Password not repeated accurately; please start over")
1188 (sit-for 1)))) 1291 (sit-for 1))))
1189 success) 1292 success)
1190 (let ((pass nil) 1293 (let ((pass nil)
1191 (c 0) 1294 (c 0)
1197 (setq c (read-char-exclusive nil t)) 1300 (setq c (read-char-exclusive nil t))
1198 (and (/= c ?\r) (/= c ?\n) (/= c ?\e))) 1301 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1199 (clear-this-command-keys) 1302 (clear-this-command-keys)
1200 (if (= c ?\C-u) 1303 (if (= c ?\C-u)
1201 (progn 1304 (progn
1202 (and (arrayp pass) (fillarray pass ?\0)) 1305 (and (arrayp pass) (clear-string pass))
1203 (setq pass "")) 1306 (setq pass ""))
1204 (if (and (/= c ?\b) (/= c ?\177)) 1307 (if (and (/= c ?\b) (/= c ?\177))
1205 (let* ((new-char (char-to-string c)) 1308 (let* ((new-char (char-to-string c))
1206 (new-pass (concat pass new-char))) 1309 (new-pass (concat pass new-char)))
1207 (and (arrayp pass) (fillarray pass ?\0)) 1310 (and (arrayp pass) (clear-string pass))
1208 (fillarray new-char ?\0) 1311 (clear-string new-char)
1209 (setq c ?\0) 1312 (setq c ?\0)
1210 (setq pass new-pass)) 1313 (setq pass new-pass))
1211 (if (> (length pass) 0) 1314 (if (> (length pass) 0)
1212 (let ((new-pass (substring pass 0 -1))) 1315 (let ((new-pass (substring pass 0 -1)))
1213 (and (arrayp pass) (fillarray pass ?\0)) 1316 (and (arrayp pass) (clear-string pass))
1214 (setq pass new-pass)))))) 1317 (setq pass new-pass))))))
1215 (message nil) 1318 (message nil)
1216 (or pass default "")))) 1319 (or pass default ""))))
1320
1321 ;; This should be used by `call-interactively' for `n' specs.
1322 (defun read-number (prompt &optional default)
1323 (let ((n nil))
1324 (when default
1325 (setq prompt
1326 (if (string-match "\\(\\):[^:]*" prompt)
1327 (replace-match (format " [%s]" default) t t prompt 1)
1328 (concat prompt (format " [%s] " default)))))
1329 (while
1330 (progn
1331 (let ((str (read-from-minibuffer prompt nil nil nil nil
1332 (number-to-string default))))
1333 (setq n (cond
1334 ((zerop (length str)) default)
1335 ((stringp str) (read str)))))
1336 (unless (numberp n)
1337 (message "Please enter a number.")
1338 (sit-for 1)
1339 t)))
1340 n))
1217 1341
1218 ;;; Atomic change groups. 1342 ;;; Atomic change groups.
1219 1343
1220 (defmacro atomic-change-group (&rest body) 1344 (defmacro atomic-change-group (&rest body)
1221 "Perform BODY as an atomic change group. 1345 "Perform BODY as an atomic change group.
1319 1443
1320 ;; For compatibility. 1444 ;; For compatibility.
1321 (defalias 'redraw-modeline 'force-mode-line-update) 1445 (defalias 'redraw-modeline 'force-mode-line-update)
1322 1446
1323 (defun force-mode-line-update (&optional all) 1447 (defun force-mode-line-update (&optional all)
1324 "Force the mode line of the current buffer to be redisplayed. 1448 "Force redisplay of the current buffer's mode line and header line.
1325 With optional non-nil ALL, force redisplay of all mode lines." 1449 With optional non-nil ALL, force redisplay of all mode lines and
1450 header lines. This function also forces recomputation of the
1451 menu bar menus and the frame title."
1326 (if all (save-excursion (set-buffer (other-buffer)))) 1452 (if all (save-excursion (set-buffer (other-buffer))))
1327 (set-buffer-modified-p (buffer-modified-p))) 1453 (set-buffer-modified-p (buffer-modified-p)))
1328 1454
1329 (defun momentary-string-display (string pos &optional exit-char message) 1455 (defun momentary-string-display (string pos &optional exit-char message)
1330 "Momentarily display STRING in the buffer at POS. 1456 "Momentarily display STRING in the buffer at POS.
1495 (remove-list-of-text-properties start end yank-excluded-properties)))) 1621 (remove-list-of-text-properties start end yank-excluded-properties))))
1496 1622
1497 (defvar yank-undo-function) 1623 (defvar yank-undo-function)
1498 1624
1499 (defun insert-for-yank (string) 1625 (defun insert-for-yank (string)
1626 "Calls `insert-for-yank-1' repetitively for each `yank-handler' segment.
1627
1628 See `insert-for-yank-1' for more details."
1629 (let (to)
1630 (while (setq to (next-single-property-change 0 'yank-handler string))
1631 (insert-for-yank-1 (substring string 0 to))
1632 (setq string (substring string to))))
1633 (insert-for-yank-1 string))
1634
1635 (defun insert-for-yank-1 (string)
1500 "Insert STRING at point, stripping some text properties. 1636 "Insert STRING at point, stripping some text properties.
1637
1501 Strip text properties from the inserted text according to 1638 Strip text properties from the inserted text according to
1502 `yank-excluded-properties'. Otherwise just like (insert STRING). 1639 `yank-excluded-properties'. Otherwise just like (insert STRING).
1503 1640
1504 If STRING has a non-nil `yank-handler' property on the first character, 1641 If STRING has a non-nil `yank-handler' property on the first character,
1505 the normal insert behaviour is modified in various ways. The value of 1642 the normal insert behaviour is modified in various ways. The value of
1548 "Insert before point a part of buffer BUFFER, stripping some text properties. 1685 "Insert before point a part of buffer BUFFER, stripping some text properties.
1549 BUFFER may be a buffer or a buffer name. Arguments START and END are 1686 BUFFER may be a buffer or a buffer name. Arguments START and END are
1550 character numbers specifying the substring. They default to the 1687 character numbers specifying the substring. They default to the
1551 beginning and the end of BUFFER. Strip text properties from the 1688 beginning and the end of BUFFER. Strip text properties from the
1552 inserted text according to `yank-excluded-properties'." 1689 inserted text according to `yank-excluded-properties'."
1690 ;; Since the buffer text should not normally have yank-handler properties,
1691 ;; there is no need to handle them here.
1553 (let ((opoint (point))) 1692 (let ((opoint (point)))
1554 (insert-buffer-substring buf start end) 1693 (insert-buffer-substring buf start end)
1555 (remove-yank-excluded-properties opoint (point)))) 1694 (remove-yank-excluded-properties opoint (point))))
1556 1695
1557 1696
1739 (defvar delay-mode-hooks nil 1878 (defvar delay-mode-hooks nil
1740 "If non-nil, `run-mode-hooks' should delay running the hooks.") 1879 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1741 (defvar delayed-mode-hooks nil 1880 (defvar delayed-mode-hooks nil
1742 "List of delayed mode hooks waiting to be run.") 1881 "List of delayed mode hooks waiting to be run.")
1743 (make-variable-buffer-local 'delayed-mode-hooks) 1882 (make-variable-buffer-local 'delayed-mode-hooks)
1883 (put 'delay-mode-hooks 'permanent-local t)
1744 1884
1745 (defun run-mode-hooks (&rest hooks) 1885 (defun run-mode-hooks (&rest hooks)
1746 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS. 1886 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1747 Execution is delayed if `delay-mode-hooks' is non-nil. 1887 Execution is delayed if `delay-mode-hooks' is non-nil.
1748 Major mode functions should use this." 1888 Major mode functions should use this."
1870 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. 2010 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1871 Zero means the entire text matched by the whole regexp or whole string. 2011 Zero means the entire text matched by the whole regexp or whole string.
1872 STRING should be given if the last search was by `string-match' on STRING." 2012 STRING should be given if the last search was by `string-match' on STRING."
1873 (if (match-beginning num) 2013 (if (match-beginning num)
1874 (if string 2014 (if string
1875 (let ((result 2015 (substring-no-properties string (match-beginning num)
1876 (substring string (match-beginning num) (match-end num)))) 2016 (match-end num))
1877 (set-text-properties 0 (length result) nil result)
1878 result)
1879 (buffer-substring-no-properties (match-beginning num) 2017 (buffer-substring-no-properties (match-beginning num)
1880 (match-end num))))) 2018 (match-end num)))))
1881 2019
1882 (defun looking-back (regexp &optional limit) 2020 (defun looking-back (regexp &optional limit)
1883 "Return non-nil if text before point matches regular expression REGEXP. 2021 "Return non-nil if text before point matches regular expression REGEXP.
2131 configuration." 2269 configuration."
2132 (and (consp object) 2270 (and (consp object)
2133 (eq (car object) 'frame-configuration))) 2271 (eq (car object) 'frame-configuration)))
2134 2272
2135 (defun functionp (object) 2273 (defun functionp (object)
2136 "Non-nil iff OBJECT is a type of object that can be called as a function." 2274 "Non-nil if OBJECT is any kind of function or a special form.
2275 Also non-nil if OBJECT is a symbol and its function definition is
2276 \(recursively) a function or special form. This does not include
2277 macros."
2137 (or (and (symbolp object) (fboundp object) 2278 (or (and (symbolp object) (fboundp object)
2138 (condition-case nil 2279 (condition-case nil
2139 (setq object (indirect-function object)) 2280 (setq object (indirect-function object))
2140 (error nil)) 2281 (error nil))
2141 (eq (car-safe object) 'autoload) 2282 (eq (car-safe object) 'autoload)
2142 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object))))))) 2283 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object)))))))
2143 (subrp object) (byte-code-function-p object) 2284 (subrp object) (byte-code-function-p object)
2144 (eq (car-safe object) 'lambda))) 2285 (eq (car-safe object) 'lambda)))
2145
2146 (defun interactive-form (function)
2147 "Return the interactive form of FUNCTION.
2148 If function is a command (see `commandp'), value is a list of the form
2149 \(interactive SPEC). If function is not a command, return nil."
2150 (setq function (indirect-function function))
2151 (when (commandp function)
2152 (cond ((byte-code-function-p function)
2153 (when (> (length function) 5)
2154 (let ((spec (aref function 5)))
2155 (if spec
2156 (list 'interactive spec)
2157 (list 'interactive)))))
2158 ((subrp function)
2159 (subr-interactive-form function))
2160 ((eq (car-safe function) 'lambda)
2161 (setq function (cddr function))
2162 (when (stringp (car function))
2163 (setq function (cdr function)))
2164 (let ((form (car function)))
2165 (when (eq (car-safe form) 'interactive)
2166 (copy-sequence form)))))))
2167 2286
2168 (defun assq-delete-all (key alist) 2287 (defun assq-delete-all (key alist)
2169 "Delete from ALIST all elements whose car is KEY. 2288 "Delete from ALIST all elements whose car is KEY.
2170 Return the modified alist. 2289 Return the modified alist.
2171 Elements of ALIST that are not conses are ignored." 2290 Elements of ALIST that are not conses are ignored."
2217 ;; If a minor mode is not defined with define-minor-mode, 2336 ;; If a minor mode is not defined with define-minor-mode,
2218 ;; add it here explicitly. 2337 ;; add it here explicitly.
2219 ;; isearch-mode is deliberately excluded, since you should 2338 ;; isearch-mode is deliberately excluded, since you should
2220 ;; not call it yourself. 2339 ;; not call it yourself.
2221 (defvar minor-mode-list '(auto-save-mode auto-fill-mode abbrev-mode 2340 (defvar minor-mode-list '(auto-save-mode auto-fill-mode abbrev-mode
2222 overwrite-mode view-mode) 2341 overwrite-mode view-mode
2342 hs-minor-mode)
2223 "List of all minor mode functions.") 2343 "List of all minor mode functions.")
2224 2344
2225 (defun add-minor-mode (toggle name &optional keymap after toggle-fun) 2345 (defun add-minor-mode (toggle name &optional keymap after toggle-fun)
2226 "Register a new minor mode. 2346 "Register a new minor mode.
2227 2347
2446 (put symbol 'composefunc composefunc) 2566 (put symbol 'composefunc composefunc)
2447 (put symbol 'sendfunc sendfunc) 2567 (put symbol 'sendfunc sendfunc)
2448 (put symbol 'abortfunc (or abortfunc 'kill-buffer)) 2568 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
2449 (put symbol 'hookvar (or hookvar 'mail-send-hook))) 2569 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
2450 2570
2571 ;;; arch-tag: f7e0e6e5-70aa-4897-ae72-7a3511ec40bc
2451 ;;; subr.el ends here 2572 ;;; subr.el ends here