comparison lisp/simple.el @ 5683:98f762d06c5f

Import GNU's #'delete-trailing-whitespace, thank you GNU. lisp/ChangeLog addition: 2012-09-08 Aidan Kehoe <kehoea@parhasard.net> * simple.el: * simple.el (delete-trailing-lines): New. * simple.el (delete-trailing-whitespace): New. Import this function and an associated variable from GNU, thank you GNU.
author Aidan Kehoe <kehoea@parhasard.net>
date Sat, 08 Sep 2012 13:34:20 +0100
parents b7ae5f44b950
children 0eb4e96fd261
comparison
equal deleted inserted replaced
5682:dae33b5feffe 5683:98f762d06c5f
366 (point-max))))) 366 (point-max)))))
367 ;; Handle the special case where point is followed by newline and eob. 367 ;; Handle the special case where point is followed by newline and eob.
368 ;; Delete the line, leaving point at eob. 368 ;; Delete the line, leaving point at eob.
369 (if (looking-at "^[ \t]*\n\\'") 369 (if (looking-at "^[ \t]*\n\\'")
370 (delete-region (point) (point-max))))) 370 (delete-region (point) (point-max)))))
371
372 (defcustom delete-trailing-lines t
373 "If non-nil, \\[delete-trailing-whitespace] deletes trailing lines.
374 Trailing lines are deleted only if `delete-trailing-whitespace'
375 is called on the entire buffer (rather than an active region)."
376 :type 'boolean
377 :group 'editing)
378 ; :version "24.2")
379
380 (defun delete-trailing-whitespace (&optional start end)
381 "Delete trailing whitespace between START and END.
382 If called interactively, START and END are the start/end of the
383 region if the mark is active, or of the buffer's accessible
384 portion if the mark is inactive.
385
386 This command deletes whitespace characters after the last
387 non-whitespace character in each line between START and END. It
388 does not consider formfeed characters to be whitespace.
389
390 If this command acts on the entire buffer (i.e. if called
391 interactively with the mark inactive, or called from Lisp with
392 END nil), it also deletes all trailing lines at the end of the
393 buffer if the variable `delete-trailing-lines' is non-nil."
394 ;; XEmacs; "*r" instead of re-implementing it.
395 (interactive "*r")
396 (save-match-data
397 (save-excursion
398 (let ((end-marker (copy-marker (or end (point-max))))
399 (start (or start (point-min))))
400 (goto-char start)
401 (while (re-search-forward "\\s-$" end-marker t)
402 (skip-syntax-backward "-" (line-beginning-position))
403 ;; Don't delete formfeeds, even if they are considered whitespace.
404 ;; XEmacs; #'looking-at-p not (yet) available
405 (if (save-match-data (looking-at ".*\f"))
406 (goto-char (match-end 0)))
407 (delete-region (point) (match-end 0)))
408 ;; Delete trailing empty lines.
409 (goto-char end-marker)
410 (when (and (not end)
411 delete-trailing-lines
412 ;; Really the end of buffer.
413 (save-restriction (widen) (eobp))
414 (<= (skip-chars-backward "\n") -2))
415 (delete-region (1+ (point)) end-marker))
416 (set-marker end-marker nil))))
417 ;; Return nil for the benefit of `write-file-functions'.
418 nil)
371 419
372 (defun back-to-indentation () 420 (defun back-to-indentation ()
373 "Move point to the first non-whitespace character on this line." 421 "Move point to the first non-whitespace character on this line."
374 ;; XEmacs change 422 ;; XEmacs change
375 (interactive "_") 423 (interactive "_")