Mercurial > emacs
comparison lisp/simple.el @ 12565:c0a5d0d00c18
(shell-command): Use save-match-data.
Put the buffer in shell mode, don't use shell-command-filter.
(shell-command-filter): Deleted.
(shell-command-sentinel): Don't reset the mode line.
Don't test the buffer name--what was that for, anyway?.
(column-number-mode): New variable and new command.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 17 Jul 1995 23:03:53 +0000 |
parents | ec77cb3940f1 |
children | 8ba65cbc8abe |
comparison
equal
deleted
inserted
replaced
12564:3cd5105aeb2b | 12565:c0a5d0d00c18 |
---|---|
740 (defun shell-command (command &optional output-buffer) | 740 (defun shell-command (command &optional output-buffer) |
741 "Execute string COMMAND in inferior shell; display output, if any. | 741 "Execute string COMMAND in inferior shell; display output, if any. |
742 | 742 |
743 If COMMAND ends in ampersand, execute it asynchronously. | 743 If COMMAND ends in ampersand, execute it asynchronously. |
744 The output appears in the buffer `*Async Shell Command*'. | 744 The output appears in the buffer `*Async Shell Command*'. |
745 | 745 That buffer is in shell mode. |
746 Otherwise, COMMAND is executed synchronously. The output appears | 746 |
747 in the buffer `*Shell Command Output*'. | 747 Otherwise, COMMAND is executed synchronously. The output appears in the |
748 buffer `*Shell Command Output*'. | |
748 If the output is one line, it is displayed in the echo area *as well*, | 749 If the output is one line, it is displayed in the echo area *as well*, |
749 but it is nonetheless available in buffer `*Shell Command Output*', | 750 but it is nonetheless available in buffer `*Shell Command Output*', |
750 even though that buffer is not automatically displayed. | 751 even though that buffer is not automatically displayed. |
751 If there is no output, or if output is inserted in the current buffer, | 752 If there is no output, or if output is inserted in the current buffer, |
752 then `*Shell Command Output*' is deleted. | 753 then `*Shell Command Output*' is deleted. |
769 ;; "if ($?prompt) exit" before things which are not useful | 770 ;; "if ($?prompt) exit" before things which are not useful |
770 ;; non-interactively. Besides, if someone wants their other | 771 ;; non-interactively. Besides, if someone wants their other |
771 ;; aliases for shell commands then they can still have them. | 772 ;; aliases for shell commands then they can still have them. |
772 (call-process shell-file-name nil t nil | 773 (call-process shell-file-name nil t nil |
773 shell-command-switch command) | 774 shell-command-switch command) |
774 ;; This is like exchange-point-and-mark, but doesn't activate the mark. | 775 ;; This is like exchange-point-and-mark, but doesn't |
775 ;; It is cleaner to avoid activation, even though the command | 776 ;; activate the mark. It is cleaner to avoid activation, |
776 ;; loop would deactivate the mark because we inserted text. | 777 ;; even though the command loop would deactivate the mark |
778 ;; because we inserted text. | |
777 (goto-char (prog1 (mark t) | 779 (goto-char (prog1 (mark t) |
778 (set-marker (mark-marker) (point) | 780 (set-marker (mark-marker) (point) |
779 (current-buffer))))) | 781 (current-buffer))))) |
780 ;; Preserve the match data in case called from a program. | 782 ;; Preserve the match data in case called from a program. |
781 (let ((data (match-data))) | 783 (save-match-data |
782 (unwind-protect | 784 (if (string-match "[ \t]*&[ \t]*$" command) |
783 (if (string-match "[ \t]*&[ \t]*$" command) | 785 ;; Command ending with ampersand means asynchronous. |
784 ;; Command ending with ampersand means asynchronous. | 786 (let ((buffer (get-buffer-create |
785 (let ((buffer (get-buffer-create | 787 (or output-buffer "*Asynch Shell Command*"))) |
786 (or output-buffer "*Asynch Shell Command*"))) | 788 (directory default-directory) |
787 (directory default-directory) | 789 proc) |
788 proc) | 790 ;; Remove the ampersand. |
789 ;; Remove the ampersand. | 791 (setq command (substring command 0 (match-beginning 0))) |
790 (setq command (substring command 0 (match-beginning 0))) | 792 ;; If will kill a process, query first. |
791 ;; If will kill a process, query first. | 793 (setq proc (get-buffer-process buffer)) |
792 (setq proc (get-buffer-process buffer)) | 794 (if proc |
793 (if proc | 795 (if (yes-or-no-p "A command is running. Kill it? ") |
794 (if (yes-or-no-p "A command is running. Kill it? ") | 796 (kill-process proc) |
795 (kill-process proc) | 797 (error "Shell command in progress"))) |
796 (error "Shell command in progress"))) | 798 (save-excursion |
797 (save-excursion | 799 (set-buffer buffer) |
798 (set-buffer buffer) | 800 (setq buffer-read-only nil) |
799 (setq buffer-read-only nil) | 801 (erase-buffer) |
800 (erase-buffer) | 802 (display-buffer buffer) |
801 (display-buffer buffer) | 803 (setq default-directory directory) |
802 (setq default-directory directory) | 804 (setq proc (start-process "Shell" buffer shell-file-name |
803 (setq proc (start-process "Shell" buffer | 805 shell-command-switch command)) |
804 shell-file-name | 806 (setq mode-line-process '(":%s")) |
805 shell-command-switch command)) | 807 (require 'shell) (shell-mode) |
806 (setq mode-line-process '(":%s")) | 808 (set-process-sentinel proc 'shell-command-sentinel) |
807 (set-process-sentinel proc 'shell-command-sentinel) | 809 )) |
808 (set-process-filter proc 'shell-command-filter) | 810 (shell-command-on-region (point) (point) command nil) |
809 )) | 811 )))) |
810 (shell-command-on-region (point) (point) command nil)) | |
811 (store-match-data data))))) | |
812 | 812 |
813 ;; We have a sentinel to prevent insertion of a termination message | 813 ;; We have a sentinel to prevent insertion of a termination message |
814 ;; in the buffer itself. | 814 ;; in the buffer itself. |
815 (defun shell-command-sentinel (process signal) | 815 (defun shell-command-sentinel (process signal) |
816 (if (and (memq (process-status process) '(exit signal)) | 816 (if (memq (process-status process) '(exit signal)) |
817 (buffer-name (process-buffer process))) | 817 (message "%s: %s." |
818 (progn | 818 (car (cdr (cdr (process-command process)))) |
819 (message "%s: %s." | 819 (substring signal 0 -1)))) |
820 (car (cdr (cdr (process-command process)))) | |
821 (substring signal 0 -1)) | |
822 (save-excursion | |
823 (set-buffer (process-buffer process)) | |
824 (setq mode-line-process nil)) | |
825 (delete-process process)))) | |
826 | |
827 (defun shell-command-filter (proc string) | |
828 ;; Do save-excursion by hand so that we can leave point numerically unchanged | |
829 ;; despite an insertion immediately after it. | |
830 (let* ((obuf (current-buffer)) | |
831 (buffer (process-buffer proc)) | |
832 opoint | |
833 (window (get-buffer-window buffer)) | |
834 (pos (window-start window))) | |
835 (unwind-protect | |
836 (progn | |
837 (set-buffer buffer) | |
838 (or (= (point) (point-max)) | |
839 (setq opoint (point))) | |
840 (goto-char (point-max)) | |
841 (insert-before-markers string)) | |
842 ;; insert-before-markers moved this marker: set it back. | |
843 (set-window-start window pos) | |
844 ;; Finish our save-excursion. | |
845 (if opoint | |
846 (goto-char opoint)) | |
847 (set-buffer obuf)))) | |
848 | 820 |
849 (defun shell-command-on-region (start end command | 821 (defun shell-command-on-region (start end command |
850 &optional output-buffer replace) | 822 &optional output-buffer replace) |
851 "Execute string COMMAND in inferior shell with region as input. | 823 "Execute string COMMAND in inferior shell with region as input. |
852 Normally display output (if any) in temp buffer `*Shell Command Output*'; | 824 Normally display output (if any) in temp buffer `*Shell Command Output*'; |
2586 (setq line-number-mode | 2558 (setq line-number-mode |
2587 (if (null arg) (not line-number-mode) | 2559 (if (null arg) (not line-number-mode) |
2588 (> (prefix-numeric-value arg) 0))) | 2560 (> (prefix-numeric-value arg) 0))) |
2589 (force-mode-line-update)) | 2561 (force-mode-line-update)) |
2590 | 2562 |
2563 (defvar column-number-mode nil | |
2564 "*Non-nil means display column number in mode line.") | |
2565 | |
2566 (defun column-number-mode (arg) | |
2567 "Toggle Column Number mode. | |
2568 With arg, turn Column Number mode on iff arg is positive. | |
2569 When Column Number mode is enabled, the column number appears | |
2570 in the mode line." | |
2571 (interactive "P") | |
2572 (setq column-number-mode | |
2573 (if (null arg) (not column-number-mode) | |
2574 (> (prefix-numeric-value arg) 0))) | |
2575 (force-mode-line-update)) | |
2576 | |
2591 (defvar blink-matching-paren t | 2577 (defvar blink-matching-paren t |
2592 "*Non-nil means show matching open-paren when close-paren is inserted.") | 2578 "*Non-nil means show matching open-paren when close-paren is inserted.") |
2593 | 2579 |
2594 (defconst blink-matching-paren-distance 12000 | 2580 (defconst blink-matching-paren-distance 12000 |
2595 "*If non-nil, is maximum distance to search for matching open-paren.") | 2581 "*If non-nil, is maximum distance to search for matching open-paren.") |