Skip to content

Commit

Permalink
Modernise using Artur Malabarba's fill/unfill toggle code
Browse files Browse the repository at this point in the history
  • Loading branch information
purcell committed Aug 17, 2016
1 parent d5f3dba commit 234fee7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
28 changes: 14 additions & 14 deletions test.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ second paragraph.
This is the second paragraph.
"))))

(ert-deftest toggle-fill-unfill-on-paragraph ()
(ert-deftest unfill-toggle-on-paragraph ()
(with-temp-buffer
(let ((initial (apply 'concat "blah" (make-list 70 " blah"))) wrapped)
(insert initial)
Expand All @@ -51,21 +51,21 @@ This is the second paragraph.
(should-not (string= wrapped initial))
;; Toggling once: unfill
(setq last-command 'self-insert-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) initial))
;; Toggling twice: fill
(setq last-command this-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) wrapped))
;; Toggling three times: unfill
(setq last-command this-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) initial)))))

(ert-deftest toggle-fill-unfill-on-region ()
(ert-deftest unfill-toggle-on-region ()
(with-temp-buffer
(let ((initial (concat (apply 'concat "blah" (make-list 70 " blah")) "\n\nSecond paragraph goes here")) wrapped)
(insert initial)
Expand All @@ -79,16 +79,16 @@ This is the second paragraph.
(transient-mark-mode 1)
;; Toggling once: unfill
(setq last-command 'self-insert-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) initial))
;; Toggling twice: fill
(setq last-command this-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) wrapped))
;; Toggling three times: unfill
(setq last-command this-command)
(setq this-command 'toggle-fill-unfill)
(toggle-fill-unfill)
(setq this-command 'unfill-toggle)
(unfill-toggle)
(should (string= (buffer-string) initial)))))
56 changes: 28 additions & 28 deletions unfill.el
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
;;; unfill.el --- The inverse of fill-paragraph and fill-region
;;; unfill.el --- Unfill paragraphs or regions, and toggle between filled & unfilled

;; Copyright (C) 2012 Steve Purcell.
;; Copyright (C) 2012-2016 Steve Purcell.

;; Author: Steve Purcell <steve@sanityinc.com>
;; X-URL: https://github.com/purcell/unfill
;; Version: DEV
;; Keywords: utilities

;; Based on Xah Lee's examples: http://xahlee.org/emacs/emacs_unfill-paragraph.html
;;; Commentary:

;; Provides commands for explicitly unfilling (ie. unwrapping)
;; paragraphs and regions, and also a command that will toggle between
;; filling and unfilling the current paragraph or region.

;; Based initially on Xah Lee's examples, and later rewritten based on an article by Artur Malabarba.
;; http://endlessparentheses.com/fill-and-unfill-paragraphs-with-a-single-key.html
;; http://xahlee.org/emacs/emacs_unfill-paragraph.html
;; http://xahlee.org/emacs/modernization_fill-paragraph.html

;; This file is NOT part of GNU Emacs.

;;; Code:

;;;###autoload
(defun unfill-paragraph ()
"Replace newline chars in current paragraph by single spaces.
This command does the inverse of `fill-paragraph'."
(interactive)
(let ((fill-column most-positive-fixnum))
(fill-paragraph nil)))
(call-interactively 'fill-paragraph)))

;;;###autoload
(defun unfill-region (start end)
Expand All @@ -27,31 +39,19 @@ This command does the inverse of `fill-region'."
(fill-region start end)))

;;;###autoload
(defun toggle-fill-unfill ()
"Remove or add line ending chars on current paragraph. This command is similar to a toggle of `fill-paragraph'. When there is a text selection, act on the region."
(defun unfill-toggle ()
"Toggle filling/unfilling of the current region, or current paragraph if no region active."
(interactive)
;; This command symbol has a property “'stateIsCompact-p”.
(let (currentStateIsCompact
(bigFillColumnVal most-positive-fixnum)
(deactivate-mark nil))
(save-excursion
;; Determine whether the text is currently compact.
(setq currentStateIsCompact
(if (eq last-command this-command)
(get this-command 'stateIsCompact-p)
(if (> (- (line-end-position) (line-beginning-position)) fill-column) t nil) ) )

(if (use-region-p)
(if currentStateIsCompact
(fill-region (region-beginning) (region-end))
(let ((fill-column bigFillColumnVal))
(fill-region (region-beginning) (region-end))) )
(if currentStateIsCompact
(fill-paragraph nil)
(let ((fill-column bigFillColumnVal))
(fill-paragraph nil)) ) )

(put this-command 'stateIsCompact-p (if currentStateIsCompact nil t)) ) ) )
(let (deactivate-mark
(fill-column
(if (eq last-command this-command)
(progn (setq this-command nil)
most-positive-fixnum)
fill-column)))
(call-interactively 'fill-paragraph)))

;;;###autoload
(define-obsolete-function-alias 'toggle-fill-unfill 'unfill-toggle)

(provide 'unfill)
;;; unfill.el ends here

0 comments on commit 234fee7

Please sign in to comment.