aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman <[email protected]>2005-01-15 18:07:21 +0000
committerRichard M. Stallman <[email protected]>2005-01-15 18:07:21 +0000
commitbdd5fa993233b9c57af2068bdbdd54a792d10981 (patch)
tree095dd6ce42af523ead0d98a0466d43975b21eb07
parentad8f7f494f69b7705953475ba9cb68797a9172da (diff)
(sh-mode-map): Bind C-c C-\.
(sh-backslash-column, sh-backslash-align): New variables. (sh-backslash-region, sh-append-backslash): New functions.
-rw-r--r--lisp/progmodes/sh-script.el82
1 files changed, 82 insertions, 0 deletions
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index bcabc505a4..019a19ed00 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -448,6 +448,7 @@ This is buffer-local in every such buffer.")
(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)
@@ -1183,6 +1184,16 @@ This is for the rc shell."
: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)
@@ -3547,6 +3558,77 @@ The document is bounded by `sh-here-document-word'."
(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)
;;; arch-tag: eccd8b72-f337-4fc2-ae86-18155a69d937