Commits

camlspotter committed 68968bb

simplification

Comments (0)

Files changed (1)

 ;; the overlay
 (defvar offside-trap-overlay (make-overlay 1 1))
 (defface offside-trap-face
-  '((t (:background "#000022")))
+  '((t (:background "#000055")))
   "Face for offside-trap highlight"
   :group 'offside-trap)
 (overlay-put offside-trap-overlay 'face 'offside-trap-face)
 
-;; get current point position
-(defun offside-trap-lines-of-point ()
-  (count-lines (point-min) (min (1+ (point)) (point-max))))
-(defun offside-trap-chars-of-point ()
-  (- (point) (line-beginning-position)))
-
 ;; get the line string at the point
 (defun offside-trap-string-of-line ()
   (buffer-substring-no-properties
    (line-beginning-position) (line-end-position)))
 
-;; count the indent length of the argument
-(defun offside-trap-indent-of-string (str)
-  (string-match "^[ ]*" str) ; TAB chars? Who cares ?
-  (match-end 0))
-
-;; get the indent length of the current line
-(defun offside-trap-indent-of-line ()
-  (offside-trap-indent-of-string (offside-trap-string-of-line)))
-
 ;; check the line is "sticky"
 (defun offside-trap-is-sticky-line (chars)
-  (let ((str (offside-trap-string-of-line)))
-    (or (string-match "^[ ]*$" str)
-	(<= chars (offside-trap-indent-of-string str)))))
+  (or (string-match "^[ ]*$" (offside-trap-string-of-line))
+      (<= chars (current-indentation))))
 
 ;; goto line
 (defun offside-trap-goto-line (line)
 
 ;; find sticky lines
 (defun offside-trap-find-sticky-lines ()
-  (let ((line (offside-trap-lines-of-point))
-	(char (offside-trap-chars-of-point))
-	(indent (offside-trap-indent-of-line))
+  (let ((line (line-number-at-pos))
+	(char (current-column))
+	(indent (current-indentation))
 	(last-line (count-lines (point-min) (point-max))))
     (let ((min-indent (max char indent)) ; CR: is it really useful ?
 	  (cur-line line))
 ;; put the overlay for the sticky lines
 (defun offside-trap-overlay-sticky-lines ()
   (interactive)
-  (let ((line (offside-trap-lines-of-point))
+  (let ((line (line-number-at-pos))
 	(sticky-line-end (offside-trap-find-sticky-lines)))
     (move-overlay offside-trap-overlay
 		  (point)
 
 ;; move the point to the indent head of the line
 (defun offside-trap-move-to-indent-head ()
-  (let ((indent (offside-trap-indent-of-line)))
+  (let ((indent (current-indentation)))
     (goto-char (+ (line-beginning-position) indent))))
 
 ;; push back the last event and exit from the offside trap mode
 ;; CR: should be buffer local ?
 (setq offside-trap-block nil)
 
+(defun offside-trap-beginning-position-of-line (line)
+  (save-excursion
+    (offside-trap-goto-line)
+    (line-beginning-position)))
+
+(defun offside-trap-end-position-of-line (line)
+  (save-excursion
+    (offside-trap-goto-line)
+    (line-end-position)))
+
 ;; point must be at the indent head
 ;; the sticky lines must be registered in offside-trap-block
 (defun offside-trap-block-indent-step-gen (f)
   ; (offside-trap-move-to-indent-head) ; not required. point is at the head already
   ; CR: check offside-trap-block is non nil
-  (let ((old-lines (offside-trap-lines-of-point))
-	(old-chars (offside-trap-chars-of-point)))
+  (let ((old-lines (line-number-at-pos))
+	(old-chars (current-column)))
     (apply f ())
     (save-excursion
-      (let* ((new-lines (offside-trap-lines-of-point))
-	     (new-chars (offside-trap-chars-of-point))
+      (let* ((new-lines (line-number-at-pos))
+	     (new-chars (current-column))
 	     (diff-lines (- new-lines old-lines))
 	     (diff-chars (- new-chars old-chars)))
         ; Line might be changed. Rebind offside-trap-block.
 					 (+ diff-lines line-end))))
 	(if offside-trap-block
 	    (destructuring-bind (line-start line-end) offside-trap-block
-	      ; (message (format "%d~%d (%d)" line-start line-end diff-chars))
-	      (let ((i line-start))
-		(while (<= i line-end)
-		  (offside-trap-goto-line i)
-		  (if (not (string-match "^[ ]*$" (offside-trap-string-of-line))) ;; if the line is empty, do nothing
-		      (if (> diff-chars 0)
-			  (insert-char 32 diff-chars) ; 32 is space
-			(delete-char (- diff-chars))))
-		  (setq i (1+ i))))))))))
+	      (indent-rigidly (offside-trap-beginning-position-of-line line-start)
+			      (offside-trap-end-position-of-line line-end)
+			      diff-chars)))))))
 
 (defun offside-trap-block-indent-step ()
   (interactive)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.