indent.lisp 10.4 KB
Newer Older
ram's avatar
ram committed
1 2 3
;;; -*- Log: hemlock.log; Package: Hemlock -*-
;;;
;;; **********************************************************************
4 5 6 7
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
8
  "$Header: src/hemlock/indent.lisp $")
9
;;;
ram's avatar
ram committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
;;; **********************************************************************
;;;
;;; Hemlock indentation commands
;;;
;;; Written by Bill Maddox and Bill Chiles
;;;
(in-package "HEMLOCK")



(defhvar "Spaces per Tab"
  "The number of spaces a tab is equivalent to.  NOTE: This is not incorporated
   everywhere in Hemlock yet, so do not change it."
  :value 8)

(defun indent-using-tabs (mark column)
  "Inserts at mark a maximum number of tabs and a minimum number of spaces to
   move mark to column.  This assumes mark is at the beginning of a line."
  (multiple-value-bind (tabs spaces) (floor column (value spaces-per-tab))
    (dotimes (i tabs) (insert-character mark #\tab))
    (dotimes (i spaces) (insert-character mark #\space))))

(defhvar "Indent with Tabs"
  "Function that takes a mark and a number of spaces and inserts tabs and spaces
   to indent that number of spaces using \"Spaces per Tab\"."
  :value #'indent-using-tabs)


(defun tab-to-tab-stop (mark)
  (insert-character mark #\tab))

(defhvar "Indent Function"
  "Indentation function which is invoked by \"Indent\" command.
   It takes a :left-inserting mark that may be moved."
  :value #'tab-to-tab-stop)


(defun generic-indent (mark)
  (let* ((line (mark-line mark))
	 (prev (do ((line (line-previous line) (line-previous line)))
		   ((or (null line) (not (blank-line-p line))) line))))
    (unless prev (editor-error))
    (line-start mark prev)
    (find-attribute mark :space #'zerop)
    (let ((indentation (mark-column mark)))
      (line-start mark line)
      (delete-horizontal-space mark)
      (funcall (value indent-with-tabs) mark indentation))))


(defcommand "Indent New Line" (p)
  "Moves point to a new blank line and indents it.
   Any whitespace before point is deleted.  The value of \"Indent Function\"
   is used for indentation unless there is a Fill Prefix, in which case it is
   used.  Any argument is passed onto \"New Line\"."
  "Moves point to a new blank line and indents it.
   Any whitespace before point is deleted.  The value of \"Indent Function\"
   is used for indentation unless there is a Fill Prefix, in which case it is
   used.  Any argument is passed onto \"New Line\"."
  (let ((point (current-point))
	(prefix (value fill-prefix)))
    (delete-horizontal-space point)
    (new-line-command p)
    (if prefix
	(insert-string point prefix)
	(funcall (value indent-function) point))))


(defcommand "Indent" (p)
  "Invokes function held by the Hemlock variable \"Indent Function\",
   moving point past region if called with argument."
  "Invokes function held by the Hemlock variable \"Indent Function\"
   moving point past region if called with argument."
  (let ((point (current-point)))
    (with-mark ((mark point :left-inserting))
      (cond ((or (not p) (zerop p))
	     (funcall (value indent-function) mark))
	    (t
	     (if (plusp p)
		 (unless (line-offset point (1- p))
		   (buffer-end point))
		 (unless (line-offset mark (1+ p))
		   (buffer-start mark)))
	     (indent-region-for-commands (region mark point))
	     (find-attribute (line-start point) :whitespace #'zerop))))))

(defcommand "Indent Region" (p)
  "Invokes function held by Hemlock variable \"Indent Function\" on every
   line between point and mark, inclusively."
  "Invokes function held by Hemlock variable \"Indent Function\" on every
   line between point and mark, inclusively."
  (declare (ignore p))
  (let* ((region (current-region)))
    (with-mark ((start (region-start region) :left-inserting)
		(end (region-end region) :left-inserting))
      (indent-region-for-commands (region start end)))))

(defun indent-region-for-commands (region)
  "Indents region undoably with INDENT-REGION."
  (let* ((start (region-start region))
	 (end (region-end region))
	 (undo-region (copy-region (region (line-start start) (line-end end)))))
    (indent-region region)
    (make-region-undo :twiddle "Indent"
		      (region (line-start (copy-mark start :left-inserting))
			      (line-end (copy-mark end :right-inserting)))
		      undo-region)))

(defun indent-region (region)
  "Invokes function held by Hemlock variable \"Indent Function\" on every
   line of region."
  (let ((indent-function (value indent-function)))
    (with-mark ((start (region-start region) :left-inserting)
		(end (region-end region)))
      (line-start start)
      (line-start end)
      (loop (when (mark= start end)
	      (funcall indent-function start)
	      (return))
	    (funcall indent-function start)
	    (line-offset start 1 0)))))

(defcommand "Center Line" (p)
  "Centers current line using \"Fill Column\".  If an argument is supplied,
   it is used instead of the \"Fill Column\"."
  "Centers current line using fill-column."
  (let* ((indent-function (value indent-with-tabs))
	 (region (if (region-active-p)
		     (current-region)
		     (region (current-point) (current-point))))
	 (end (region-end region)))
    (with-mark ((temp (region-start region) :left-inserting))
      (loop
	(when (mark> temp end) (return))
	(delete-horizontal-space (line-end temp))
	(delete-horizontal-space (line-start temp))
	(let* ((len (line-length (mark-line temp)))
	       (spaces (- (or p (value fill-column)) len)))
	  (if (and (plusp spaces) 
		   (not (zerop len)))
	      (funcall indent-function temp (ceiling spaces 2)))
151 152
	  (unless (line-offset temp 1) (return))
	  (line-start temp))))))
ram's avatar
ram committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285


(defcommand "Quote Tab" (p)
  "Insert tab character."
  "Insert tab character."
  (if (and p (> p 1))
      (insert-string (current-point) (make-string p :initial-element #\tab))
      (insert-character (current-point) #\tab)))


(defcommand "Open Line" (p)
  "Inserts a newline into the buffer without moving the point."
  "Inserts a newline into the buffer without moving the point.
  With argument, inserts p newlines."
  (let ((point (current-point))
	(count (if p p 1)))
    (if (not (minusp count))
	(dotimes (i count)
	  (insert-character point #\newline)
	  (mark-before point))
	(editor-error))))


(defcommand "New Line" (p)
  "Moves the point to a new blank line.
  A newline is inserted if the next two lines are not already blank.
  With an argument, repeats p times."
  "Moves the point to a new blank line."
  (let ((point (current-point))
	(count (if p p 1)))
    (if (not (minusp count))
	(do* ((next (line-next (mark-line point))
		    (line-next (mark-line point)))
	      (i 1 (1+ i)))
	     ((> i count))
	  (cond ((and (blank-after-p point)
		      next (blank-line-p next)
		      (let ((after (line-next next)))
			(or (not after) (blank-line-p after))))
		 (line-start point next)
		 (let ((len (line-length next)))
		   (unless (zerop len)
		     (delete-characters point len))))
		(t
		 (insert-character point #\newline))))
	(editor-error))))


(defattribute "Space"
  "This attribute is used by the indentation commands to determine which
  characters are treated as space."
  '(mod 2) 0)

(setf (character-attribute :space #\space) 1)
(setf (character-attribute :space #\tab) 1)

(defun delete-horizontal-space (mark)
  "Deletes all :space characters on either side of mark."
  (with-mark ((start mark))
    (reverse-find-attribute start :space #'zerop)
    (find-attribute mark :space #'zerop)
    (delete-region (region start mark))))



(defcommand "Delete Indentation" (p)
  "Join current line with the previous one, deleting excess whitespace.
  All whitespace is replaced with a single space, unless it is at the beginning
  of a line, immmediately following a \"(\", or immediately preceding a \")\",
  in which case the whitespace is merely deleted.  If the preceeding character
  is a sentence terminator, two spaces are left instead of one.  If a prefix
  argument is given, the following line is joined with the current line."
  "Join current line with the previous one, deleting excess whitespace."
  (with-mark ((m (current-point) :right-inserting))
    (when p (line-offset m 1))
    (line-start m)
    (unless (delete-characters m -1) (editor-error "No previous line."))
    (delete-horizontal-space m)
    (let ((prev (previous-character m)))
      (when (and prev (char/= prev #\newline))
	(cond ((not (zerop (character-attribute :sentence-terminator prev)))
	       (insert-string m "  "))
	      ((not (or (eq (character-attribute :lisp-syntax prev) :open-paren)
			(eq (character-attribute :lisp-syntax (next-character m))
			    :close-paren)))
	       (insert-character m #\space)))))))


(defcommand "Delete Horizontal Space" (p)
  "Delete spaces and tabs surrounding the point."
  "Delete spaces and tabs surrounding the point."
  (declare (ignore p))
  (delete-horizontal-space (current-point)))

(defcommand "Just One Space" (p)
  "Leave one space.
  Surrounding space is deleted, and then one space is inserted.
  with prefix argument insert that number of spaces."
  "Delete surrounding space and insert P spaces."
  (let ((point (current-point)))
    (delete-horizontal-space point)
    (dotimes (i (or p 1)) (insert-character point #\space))))

(defcommand "Back to Indentation" (p)
  "Move point to the first non-whitespace character on the line."
  "Move point to the first non-whitespace character on the line."
  (declare (ignore p))
  (let ((point (current-point)))
    (line-start point)
    (find-attribute point :whitespace #'zerop)))

(defcommand "Indent Rigidly" (p)
  "Indent the region rigidly by p spaces.
   Each line in the region is moved p spaces to the right (left if p is
   negative).  When moving a line to the left, tabs are converted to spaces."
  "Indent the region rigidly p spaces to the right (left if p is negative)."
  (let ((p (or p (value spaces-per-tab)))
	(region (current-region)))
    (with-mark ((mark1 (region-start region) :left-inserting)
		(mark2 (region-end region) :left-inserting))
      (line-start mark1)
      (line-start mark2)
      (do ()
	  ((mark= mark1 mark2))
	(cond ((empty-line-p mark1))
	      ((blank-after-p mark1)
	       (delete-characters mark1 (line-length (mark-line mark1))))
	      (t (find-attribute mark1 :whitespace #'zerop)
		 (let ((new-column (+ p (mark-column mark1))))
		   (delete-characters mark1 (- (mark-charpos mark1)))
		   (if (plusp new-column)
		       (funcall (value indent-with-tabs) mark1 new-column)))))
	(line-offset mark1 1 0)))))