Mercurial > emacs
comparison lisp/simple.el @ 715:7af12ccaa6c1
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Fri, 12 Jun 1992 22:23:00 +0000 |
parents | 7cbd4fcd8b0f |
children | 540b047ece4d |
comparison
equal
deleted
inserted
replaced
714:d105ddc785b8 | 715:7af12ccaa6c1 |
---|---|
98 (interactive "*") | 98 (interactive "*") |
99 (let (thisblank singleblank) | 99 (let (thisblank singleblank) |
100 (save-excursion | 100 (save-excursion |
101 (beginning-of-line) | 101 (beginning-of-line) |
102 (setq thisblank (looking-at "[ \t]*$")) | 102 (setq thisblank (looking-at "[ \t]*$")) |
103 ;; Set singleblank if there is just one blank line here. | |
103 (setq singleblank | 104 (setq singleblank |
104 (and thisblank | 105 (and thisblank |
105 (not (looking-at "[ \t]*\n[ \t]*$")) | 106 (not (looking-at "[ \t]*\n[ \t]*$")) |
106 (or (bobp) | 107 (or (bobp) |
107 (progn (forward-line -1) | 108 (progn (forward-line -1) |
108 (not (looking-at "[ \t]*$"))))))) | 109 (not (looking-at "[ \t]*$"))))))) |
110 ;; Delete preceding blank lines, and this one too if it's the only one. | |
109 (if thisblank | 111 (if thisblank |
110 (progn | 112 (progn |
111 (beginning-of-line) | 113 (beginning-of-line) |
112 (if singleblank (forward-line 1)) | 114 (if singleblank (forward-line 1)) |
113 (delete-region (point) | 115 (delete-region (point) |
114 (if (re-search-backward "[^ \t\n]" nil t) | 116 (if (re-search-backward "[^ \t\n]" nil t) |
115 (progn (forward-line 1) (point)) | 117 (progn (forward-line 1) (point)) |
116 (point-min))))) | 118 (point-min))))) |
119 ;; Delete following blank lines, unless the current line is blank | |
120 ;; and there are no following blank lines. | |
117 (if (not (and thisblank singleblank)) | 121 (if (not (and thisblank singleblank)) |
118 (save-excursion | 122 (save-excursion |
119 (end-of-line) | 123 (end-of-line) |
120 (forward-line 1) | 124 (forward-line 1) |
121 (delete-region (point) | 125 (delete-region (point) |
122 (if (re-search-forward "[^ \t\n]" nil t) | 126 (if (re-search-forward "[^ \t\n]" nil t) |
123 (progn (beginning-of-line) (point)) | 127 (progn (beginning-of-line) (point)) |
124 (point-max))))))) | 128 (point-max))))) |
129 ;; Handle the special case where point is followed by newline and eob. | |
130 ;; Delete the line, leaving point at eob. | |
131 (if (looking-at "^[ \t]*\n\\'") | |
132 (delete-region (point) (point-max))))) | |
125 | 133 |
126 (defun back-to-indentation () | 134 (defun back-to-indentation () |
127 "Move point to the first non-whitespace character on this line." | 135 "Move point to the first non-whitespace character on this line." |
128 (interactive) | 136 (interactive) |
129 (beginning-of-line 1) | 137 (beginning-of-line 1) |
233 ;; Scroll to put point near bottom--show nearly maximum amount of text, | 241 ;; Scroll to put point near bottom--show nearly maximum amount of text, |
234 ;; but leave room to add something. | 242 ;; but leave room to add something. |
235 (recenter -3))) | 243 (recenter -3))) |
236 | 244 |
237 (defun mark-whole-buffer () | 245 (defun mark-whole-buffer () |
238 "Put point at beginning and mark at end of buffer." | 246 "Put point at beginning and mark at end of buffer. |
247 You probably should not use this function in Lisp programs; | |
248 it is usually a mistake for a Lisp function to use any subroutine | |
249 that uses or sets the mark." | |
239 (interactive) | 250 (interactive) |
240 (push-mark (point)) | 251 (push-mark (point)) |
241 (push-mark (point-max)) | 252 (push-mark (point-max)) |
242 (goto-char (point-min))) | 253 (goto-char (point-min))) |
243 | 254 |
589 Repeating \\[universal-argument] without digits or minus sign | 600 Repeating \\[universal-argument] without digits or minus sign |
590 multiplies the argument by 4 each time." | 601 multiplies the argument by 4 each time." |
591 (interactive nil) | 602 (interactive nil) |
592 (let ((factor 4) | 603 (let ((factor 4) |
593 key) | 604 key) |
594 (describe-arg (list factor) 1) | 605 ;; (describe-arg (list factor) 1) |
595 (setq key (read-key-sequence nil)) | 606 (setq key (read-key-sequence nil t)) |
596 (while (equal (key-binding key) 'universal-argument) | 607 (while (equal (key-binding key) 'universal-argument) |
597 (setq factor (* 4 factor)) | 608 (setq factor (* 4 factor)) |
598 (describe-arg (list factor) 1) | 609 ;; (describe-arg (list factor) 1) |
599 (setq key (read-key-sequence nil))) | 610 (setq key (read-key-sequence nil t))) |
600 (prefix-arg-internal key factor nil))) | 611 (prefix-arg-internal key factor nil))) |
601 | 612 |
602 (defun prefix-arg-internal (key factor value) | 613 (defun prefix-arg-internal (key factor value) |
603 (let ((sign 1)) | 614 (let ((sign 1)) |
604 (if (and (numberp value) (< value 0)) | 615 (if (and (numberp value) (< value 0)) |
605 (setq sign -1 value (- value))) | 616 (setq sign -1 value (- value))) |
606 (if (eq value '-) | 617 (if (eq value '-) |
607 (setq sign -1 value nil)) | 618 (setq sign -1 value nil)) |
608 (describe-arg value sign) | 619 ;; (describe-arg value sign) |
609 (while (equal key "-") | 620 (while (equal key "-") |
610 (setq sign (- sign) factor nil) | 621 (setq sign (- sign) factor nil) |
611 (describe-arg value sign) | 622 ;; (describe-arg value sign) |
612 (setq key (read-key-sequence nil))) | 623 (setq key (read-key-sequence nil t))) |
613 (while (and (= (length key) 1) | 624 (while (and (= (length key) 1) |
614 (not (string< key "0")) | 625 (not (string< key "0")) |
615 (not (string< "9" key))) | 626 (not (string< "9" key))) |
616 (setq value (+ (* (if (numberp value) value 0) 10) | 627 (setq value (+ (* (if (numberp value) value 0) 10) |
617 (- (aref key 0) ?0)) | 628 (- (aref key 0) ?0)) |
618 factor nil) | 629 factor nil) |
619 (describe-arg value sign) | 630 ;; (describe-arg value sign) |
620 (setq key (read-key-sequence nil))) | 631 (setq key (read-key-sequence nil t))) |
621 (setq prefix-arg | 632 (setq prefix-arg |
622 (cond (factor (list factor)) | 633 (cond (factor (list factor)) |
623 ((numberp value) (* value sign)) | 634 ((numberp value) (* value sign)) |
624 ((= sign -1) '-))) | 635 ((= sign -1) '-))) |
625 ;; Calling universal-argument after digits | 636 ;; Calling universal-argument after digits |
626 ;; terminates the argument but is ignored. | 637 ;; terminates the argument but is ignored. |
627 (if (eq (key-binding key) 'universal-argument) | 638 (if (eq (key-binding key) 'universal-argument) |
628 (progn | 639 (progn |
629 (describe-arg value sign) | 640 (describe-arg value sign) |
630 (setq key (read-key-sequence nil)))) | 641 (setq key (read-key-sequence nil t)))) |
631 (if (= (length key) 1) | 642 (if (= (length key) 1) |
632 ;; Make sure self-insert-command finds the proper character; | 643 ;; Make sure self-insert-command finds the proper character; |
633 ;; unread the character and let the command loop process it. | 644 ;; unread the character and let the command loop process it. |
634 (setq unread-command-char (string-to-char key)) | 645 (setq unread-command-char (string-to-char key)) |
635 ;; We can't push back a longer string, so we'll emulate the | 646 ;; We can't push back a longer string, so we'll emulate the |
686 (if (looking-at "[ \t]*$") | 697 (if (looking-at "[ \t]*$") |
687 (forward-line 1) | 698 (forward-line 1) |
688 (end-of-line))) | 699 (end-of-line))) |
689 (point)))) | 700 (point)))) |
690 | 701 |
691 ;;;; The kill ring | 702 ;;;; Window system cut and paste hooks. |
703 | |
704 (defvar interprogram-cut-function nil | |
705 "Function to call to make a killed region available to other programs. | |
706 | |
707 Most window systems provide some sort of facility for cutting and | |
708 pasting text between the windows of different programs. On startup, | |
709 this variable is set to a function which emacs will call whenever text | |
710 is put in the kill ring to make the new kill available to other | |
711 programs. | |
712 | |
713 The function takes one argument, TEXT, which is a string containing | |
714 the text which should be made available.") | |
715 | |
716 (defvar interprogram-paste-function nil | |
717 "Function to call to get text cut from other programs. | |
718 | |
719 Most window systems provide some sort of facility for cutting and | |
720 pasting text between the windows of different programs. On startup, | |
721 this variable is set to a function which emacs will call to obtain | |
722 text that other programs have provided for pasting. | |
723 | |
724 The function should be called with no arguments. If the function | |
725 returns nil, then no other program has provided such text, and the top | |
726 of the Emacs kill ring should be used. If the function returns a | |
727 string, that string should be put in the kill ring as the latest kill.") | |
728 | |
729 | |
730 | |
731 ;;;; The kill ring data structure. | |
692 | 732 |
693 (defvar kill-ring nil | 733 (defvar kill-ring nil |
694 "List of killed text sequences.") | 734 "List of killed text sequences. |
735 Since the kill ring is supposed to interact nicely with cut-and-paste | |
736 facilities offered by window systems, use of this variable should | |
737 interact nicely with `interprogram-cut-function' and | |
738 `interprogram-paste-function'. The functions `kill-new', | |
739 `kill-append', and `current-kill' are supposed to implement this | |
740 interaction; you may want to use them instead of manipulating the kill | |
741 ring directly.") | |
695 | 742 |
696 (defconst kill-ring-max 30 | 743 (defconst kill-ring-max 30 |
697 "*Maximum length of kill ring before oldest elements are thrown away.") | 744 "*Maximum length of kill ring before oldest elements are thrown away.") |
698 | 745 |
699 (defvar kill-ring-yank-pointer nil | 746 (defvar kill-ring-yank-pointer nil |
700 "The tail of the kill ring whose car is the last thing yanked.") | 747 "The tail of the kill ring whose car is the last thing yanked.") |
701 | 748 |
749 (defun kill-new (string) | |
750 "Make STRING the latest kill in the kill ring. | |
751 Set the kill-ring-yank pointer to point to it. | |
752 If `interprogram-cut-function' is non-nil, apply it to STRING." | |
753 (setq kill-ring (cons string kill-ring)) | |
754 (if (> (length kill-ring) kill-ring-max) | |
755 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) | |
756 (setq kill-ring-yank-pointer kill-ring) | |
757 (if interprogram-cut-function | |
758 (funcall interprogram-cut-function string))) | |
759 | |
702 (defun kill-append (string before-p) | 760 (defun kill-append (string before-p) |
761 "Append STRING to the end of the latest kill in the kill ring. | |
762 If BEFORE-P is non-nil, prepend STRING to the kill. | |
763 If 'interprogram-cut-function' is set, pass the resulting kill to | |
764 it." | |
703 (setcar kill-ring | 765 (setcar kill-ring |
704 (if before-p | 766 (if before-p |
705 (concat string (car kill-ring)) | 767 (concat string (car kill-ring)) |
706 (concat (car kill-ring) string)))) | 768 (concat (car kill-ring) string))) |
707 | 769 (if interprogram-cut-function |
708 (defvar interprogram-cut-function nil | 770 (funcall interprogram-cut-function (car kill-ring)))) |
709 "Function to call to make a killed region available to other programs. | 771 |
710 | 772 (defun current-kill (n &optional do-not-move) |
711 Most window systems provide some sort of facility for cutting and | 773 "Rotate the yanking point by N places, and then return that kill. |
712 pasting text between the windows of different programs. On startup, | 774 If N is zero, `interprogram-paste-function' is set, and calling it |
713 this variable is set to a function which emacs will call to make the | 775 returns a string, then that string is added to the front of the |
714 most recently killed text available to other programs. | 776 kill ring and returned as the latest kill. |
715 | 777 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the |
716 The function takes one argument, TEXT, which is a string containing | 778 yanking point; just return the Nth kill forward." |
717 the text which should be made available.") | 779 (let ((interprogram-paste (and (= n 0) |
780 interprogram-paste-function | |
781 (funcall interprogram-paste-function)))) | |
782 (if interprogram-paste | |
783 (progn | |
784 ;; Disable the interprogram cut function when we add the new | |
785 ;; text to the kill ring, so Emacs doesn't try to own the | |
786 ;; selection, with identical text. | |
787 (let ((interprogram-cut-function nil)) | |
788 (kill-new interprogram-paste)) | |
789 interprogram-paste) | |
790 (or kill-ring (error "Kill ring is empty")) | |
791 (let* ((length (length kill-ring)) | |
792 (ARGth-kill-element | |
793 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer))) | |
794 length) | |
795 kill-ring))) | |
796 (or do-not-move | |
797 (setq kill-ring-yank-pointer ARGth-kill-element)) | |
798 (car ARGth-kill-element))))) | |
799 | |
800 | |
801 | |
802 ;;;; Commands for manipulating the kill ring. | |
718 | 803 |
719 (defun kill-region (beg end) | 804 (defun kill-region (beg end) |
720 "Kill between point and mark. | 805 "Kill between point and mark. |
721 The text is deleted but saved in the kill ring. | 806 The text is deleted but saved in the kill ring. |
722 The command \\[yank] can retrieve it from there. | 807 The command \\[yank] can retrieve it from there. |
728 Any command that calls this function is a \"kill command\". | 813 Any command that calls this function is a \"kill command\". |
729 If the previous command was also a kill command, | 814 If the previous command was also a kill command, |
730 the text killed this time appends to the text killed last time | 815 the text killed this time appends to the text killed last time |
731 to make one entry in the kill ring." | 816 to make one entry in the kill ring." |
732 (interactive "r") | 817 (interactive "r") |
733 (if (and (not (eq buffer-undo-list t)) | 818 (cond |
734 (not (eq last-command 'kill-region)) | 819 (buffer-read-only |
735 (not (eq beg end)) | 820 (copy-region-as-kill beg end)) |
736 (not buffer-read-only)) | 821 ((not (or (eq buffer-undo-list t) |
737 ;; Don't let the undo list be truncated before we can even access it. | 822 (eq last-command 'kill-region) |
738 (let ((undo-high-threshold (+ (- (max beg end) (min beg end)) 100))) | 823 (eq beg end))) |
739 (delete-region beg end) | 824 ;; Don't let the undo list be truncated before we can even access it. |
740 ;; Take the same string recorded for undo | 825 (let ((undo-high-threshold (+ (- (max beg end) (min beg end)) 100))) |
741 ;; and put it in the kill-ring. | 826 (delete-region beg end) |
742 (setq kill-ring (cons (car (car buffer-undo-list)) kill-ring)) | 827 ;; Take the same string recorded for undo |
743 (if interprogram-cut-function | 828 ;; and put it in the kill-ring. |
744 (funcall interprogram-cut-function (car kill-ring))) | 829 (kill-new (car (car buffer-undo-list))) |
745 (if (> (length kill-ring) kill-ring-max) | 830 (setq this-command 'kill-region))) |
746 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) | 831 (t |
747 (setq this-command 'kill-region) | |
748 (setq kill-ring-yank-pointer kill-ring)) | |
749 (copy-region-as-kill beg end) | 832 (copy-region-as-kill beg end) |
750 (or buffer-read-only (delete-region beg end)))) | 833 (delete-region beg end)))) |
751 | 834 |
752 (defun copy-region-as-kill (beg end) | 835 (defun copy-region-as-kill (beg end) |
753 "Save the region as if killed, but don't kill it. | 836 "Save the region as if killed, but don't kill it. |
754 If `interprogram-cut-function' is non-nil, also save the text for a window | 837 If `interprogram-cut-function' is non-nil, also save the text for a window |
755 system cut and paste." | 838 system cut and paste." |
756 (interactive "r") | 839 (interactive "r") |
757 (if (eq last-command 'kill-region) | 840 (if (eq last-command 'kill-region) |
758 (kill-append (buffer-substring beg end) (< end beg)) | 841 (kill-append (buffer-substring beg end) (< end beg)) |
759 (setq kill-ring (cons (buffer-substring beg end) kill-ring)) | 842 (kill-new (buffer-substring beg end))) |
760 (if (> (length kill-ring) kill-ring-max) | 843 (setq this-command 'kill-region) |
761 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) | |
762 (if interprogram-cut-function | |
763 (funcall interprogram-cut-function (car kill-ring))) | |
764 (setq this-command 'kill-region | |
765 kill-ring-yank-pointer kill-ring) | |
766 nil) | 844 nil) |
767 | 845 |
768 (defun kill-ring-save (beg end) | 846 (defun kill-ring-save (beg end) |
769 "Save the region as if killed, but don't kill it." | 847 "Save the region as if killed, but don't kill it." |
770 (interactive "r") | 848 (interactive "r") |
771 (copy-region-as-kill beg end) | 849 (copy-region-as-kill beg end) |
772 (message "%d characters copied to kill ring" | 850 (save-excursion |
773 (- (max beg end) (min beg end)))) | 851 (let ((other-end (if (= (point) beg) end beg))) |
852 (if (pos-visible-in-window-p other-end (selected-window)) | |
853 (progn | |
854 (goto-char other-end) | |
855 (sit-for 1)) | |
856 (let* ((killed-text (current-kill 0)) | |
857 (message-len (min (length killed-text) 40))) | |
858 (message | |
859 (if (= (point) beg) | |
860 (format "Killed until \"%s\"" | |
861 (substring killed-text (- message-len))) | |
862 (format "Killed from \"%s\"" | |
863 (substring killed-text 0 message-len))))))))) | |
774 | 864 |
775 (defun append-next-kill () | 865 (defun append-next-kill () |
776 "Cause following command, if kill, to append to previous kill." | 866 "Cause following command, if kill, to append to previous kill." |
777 (interactive) | 867 (interactive) |
778 (if (interactive-p) | 868 (if (interactive-p) |
779 (progn | 869 (progn |
780 (setq this-command 'kill-region) | 870 (setq this-command 'kill-region) |
781 (message "If the next command is a kill, it will append")) | 871 (message "If the next command is a kill, it will append")) |
782 (setq last-command 'kill-region))) | 872 (setq last-command 'kill-region))) |
783 | 873 |
784 (defun rotate-yank-pointer (arg) | |
785 "Rotate the yanking point in the kill ring." | |
786 (interactive "p") | |
787 (let ((length (length kill-ring))) | |
788 (if (zerop length) | |
789 (error "Kill ring is empty") | |
790 (setq kill-ring-yank-pointer | |
791 (nthcdr (% (+ arg (- length (length kill-ring-yank-pointer))) | |
792 length) | |
793 kill-ring))))) | |
794 | |
795 (defun yank-pop (arg) | 874 (defun yank-pop (arg) |
796 "Replace just-yanked stretch of killed-text with a different stretch. | 875 "Replace just-yanked stretch of killed-text with a different stretch. |
797 This command is allowed only immediately after a yank or a yank-pop. | 876 This command is allowed only immediately after a yank or a yank-pop. |
798 At such a time, the region contains a stretch of reinserted | 877 At such a time, the region contains a stretch of reinserted |
799 previously-killed text. yank-pop deletes that text and inserts in its | 878 previously-killed text. yank-pop deletes that text and inserts in its |
809 (if (not (eq last-command 'yank)) | 888 (if (not (eq last-command 'yank)) |
810 (error "Previous command was not a yank")) | 889 (error "Previous command was not a yank")) |
811 (setq this-command 'yank) | 890 (setq this-command 'yank) |
812 (let ((before (< (point) (mark)))) | 891 (let ((before (< (point) (mark)))) |
813 (delete-region (point) (mark)) | 892 (delete-region (point) (mark)) |
814 (rotate-yank-pointer arg) | |
815 (set-mark (point)) | 893 (set-mark (point)) |
816 (insert (car kill-ring-yank-pointer)) | 894 (insert (current-kill arg)) |
817 (if before (exchange-point-and-mark)))) | 895 (if before (exchange-point-and-mark)))) |
818 | 896 |
819 (defun yank (&optional arg) | 897 (defun yank (&optional arg) |
820 "Reinsert the last stretch of killed text. | 898 "Reinsert the last stretch of killed text. |
821 More precisely, reinsert the stretch of killed text most recently | 899 More precisely, reinsert the stretch of killed text most recently |
823 With just C-U as argument, same but put point in front (and mark at end). | 901 With just C-U as argument, same but put point in front (and mark at end). |
824 With argument n, reinsert the nth most recently killed stretch of killed | 902 With argument n, reinsert the nth most recently killed stretch of killed |
825 text. | 903 text. |
826 See also the command \\[yank-pop]." | 904 See also the command \\[yank-pop]." |
827 (interactive "*P") | 905 (interactive "*P") |
828 (rotate-yank-pointer (if (listp arg) 0 | |
829 (if (eq arg '-) -1 | |
830 (1- arg)))) | |
831 (push-mark (point)) | 906 (push-mark (point)) |
832 (insert (car kill-ring-yank-pointer)) | 907 (insert (current-kill (cond |
908 ((listp arg) 0) | |
909 ((eq arg '-) -1) | |
910 (t (1- arg))))) | |
833 (if (consp arg) | 911 (if (consp arg) |
834 (exchange-point-and-mark))) | 912 (exchange-point-and-mark))) |
913 | |
914 (defun rotate-yank-pointer (arg) | |
915 "Rotate the yanking point in the kill ring. | |
916 With argument, rotate that many kills forward (or backward, if negative)." | |
917 (interactive "p") | |
918 (current-kill arg)) | |
919 | |
835 | 920 |
836 (defun insert-buffer (buffer) | 921 (defun insert-buffer (buffer) |
837 "Insert after point the contents of BUFFER. | 922 "Insert after point the contents of BUFFER. |
838 Puts mark after the inserted text. | 923 Puts mark after the inserted text. |
839 BUFFER may be a buffer or a buffer name." | 924 BUFFER may be a buffer or a buffer name." |
854 It is inserted into that buffer before its point. | 939 It is inserted into that buffer before its point. |
855 | 940 |
856 When calling from a program, give three arguments: | 941 When calling from a program, give three arguments: |
857 BUFFER (or buffer name), START and END. | 942 BUFFER (or buffer name), START and END. |
858 START and END specify the portion of the current buffer to be copied." | 943 START and END specify the portion of the current buffer to be copied." |
859 (interactive "BAppend to buffer: \nr") | 944 (interactive |
945 (list (read-buffer "Append to buffer: " (other-buffer nil t) t))) | |
860 (let ((oldbuf (current-buffer))) | 946 (let ((oldbuf (current-buffer))) |
861 (save-excursion | 947 (save-excursion |
862 (set-buffer (get-buffer-create buffer)) | 948 (set-buffer (get-buffer-create buffer)) |
863 (insert-buffer-substring oldbuf start end)))) | 949 (insert-buffer-substring oldbuf start end)))) |
864 | 950 |