From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "James R. Van Zandt" Newsgroups: gmane.emacs.bugs Subject: backslash-region for sh-script.el Date: Tue, 11 Jan 2005 21:24:34 -0500 Message-ID: NNTP-Posting-Host: deer.gmane.org X-Trace: sea.gmane.org 1105496748 23884 80.91.229.6 (12 Jan 2005 02:25:48 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 12 Jan 2005 02:25:48 +0000 (UTC) Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Jan 12 03:25:41 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1CoYCa-0005zy-00 for ; Wed, 12 Jan 2005 03:25:41 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1CoYOA-0006QI-OE for geb-bug-gnu-emacs@m.gmane.org; Tue, 11 Jan 2005 21:37:38 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1CoYNJ-00050G-Ps for bug-gnu-emacs@gnu.org; Tue, 11 Jan 2005 21:36:45 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1CoYNE-0004ov-Sj for bug-gnu-emacs@gnu.org; Tue, 11 Jan 2005 21:36:41 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1CoYNE-0004oT-NN for bug-gnu-emacs@gnu.org; Tue, 11 Jan 2005 21:36:40 -0500 Original-Received: from [204.127.198.35] (helo=rwcrmhc11.comcast.net) by monty-python.gnu.org with esmtp (Exim 4.34) id 1CoYBY-0004Bx-1d for bug-gnu-emacs@gnu.org; Tue, 11 Jan 2005 21:24:36 -0500 Original-Received: from vanzandt.comcast.net (h000f66444d29.ne.client2.attbi.com[24.61.94.90]) by comcast.net (rwcrmhc11) with ESMTP id <2005011202243401300t728se>; Wed, 12 Jan 2005 02:24:34 +0000 Original-Received: from jrv by vanzandt.comcast.net with local (Exim 3.36 #1 (Debian)) id 1CoYBW-0008O7-00 for ; Tue, 11 Jan 2005 21:24:34 -0500 Original-To: bug-gnu-emacs@gnu.org X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.bugs:10392 X-Report-Spam: http://spam.gmane.org/gmane.emacs.bugs:10392 cc-mode and makefile-mode have commands to insert, align, or delete end-of-line backslashes on the lines in the region. This patch adds similar commands for sh-mode. - Jim Van Zandt --- /usr/share/emacs/21.3/lisp/progmodes/sh-script.el 2003-09-09 17:42:15.000000000 -0400 +++ ./sh-script.el 2005-01-11 20:59:57.000000000 -0500 @@ -428,6 +428,7 @@ (define-key map "\C-c=" 'sh-set-indent) (define-key map "\C-c<" 'sh-learn-line-indent) (define-key map "\C-c>" 'sh-learn-buffer-indent) + (define-key map "\C-c\C-\\" 'sh-backslash-region) (define-key map "=" 'sh-assignment) (define-key map "\C-c+" 'sh-add) @@ -1113,6 +1114,16 @@ :type `(choice ,@ sh-number-or-symbol-list) :group 'sh-indentation) +(defcustom sh-backslash-column 48 + "*Column in which `sh-backslash-region' inserts backslashes." + :type 'integer + :group 'sh) + +(defcustom sh-backslash-align t + "*If non-nil, `sh-backslash-region' will align backslashes." + :type 'boolean + :group 'sh) + ;; Internal use - not designed to be changed by the user: (defun sh-mkword-regexpr (word) @@ -3510,6 +3521,77 @@ (if (re-search-forward sh-end-of-command nil t) (goto-char (match-end 1)))) +;; Backslashification. Stolen from make-mode.el. + +(defun sh-backslash-region (from to delete-flag) + "Insert, align, or delete end-of-line backslashes on the lines in the region. +With no argument, inserts backslashes and aligns existing backslashes. +With an argument, deletes the backslashes. + +This function does not modify the last line of the region if the region ends +right at the start of the following line; it does not modify blank lines +at the start of the region. So you can put the region around an entire +shell command and conveniently use this command." + (interactive "r\nP") + (save-excursion + (goto-char from) + (let ((column sh-backslash-column) + (endmark (make-marker))) + (move-marker endmark to) + ;; Compute the smallest column number past the ends of all the lines. + (if sh-backslash-align + (progn + (if (not delete-flag) + (while (< (point) to) + (end-of-line) + (if (= (preceding-char) ?\\) + (progn (forward-char -1) + (skip-chars-backward " \t"))) + (setq column (max column (1+ (current-column)))) + (forward-line 1))) + ;; Adjust upward to a tab column, if that doesn't push + ;; past the margin. + (if (> (% column tab-width) 0) + (let ((adjusted (* (/ (+ column tab-width -1) tab-width) + tab-width))) + (if (< adjusted (window-width)) + (setq column adjusted)))))) + ;; Don't modify blank lines at start of region. + (goto-char from) + (while (and (< (point) endmark) (eolp)) + (forward-line 1)) + ;; Add or remove backslashes on all the lines. + (while (and (< (point) endmark) + ;; Don't backslashify the last line + ;; if the region ends right at the start of the next line. + (save-excursion + (forward-line 1) + (< (point) endmark))) + (if (not delete-flag) + (sh-append-backslash column) + (sh-delete-backslash)) + (forward-line 1)) + (move-marker endmark nil)))) + +(defun sh-append-backslash (column) + (end-of-line) + ;; Note that "\\\\" is needed to get one backslash. + (if (= (preceding-char) ?\\) + (progn (forward-char -1) + (delete-horizontal-space) + (indent-to column (if sh-backslash-align nil 1))) + (indent-to column (if sh-backslash-align nil 1)) + (insert "\\"))) + +(defun sh-delete-backslash () + (end-of-line) + (or (bolp) + (progn + (forward-char -1) + (if (looking-at "\\\\") + (delete-region (1+ (point)) + (progn (skip-chars-backward " \t") (point))))))) + (provide 'sh-script) ;;; sh-script.el ends here