[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 1e68955 16/26: Corrections after another review iteration
From: |
João Távora |
Subject: |
[elpa] master 1e68955 16/26: Corrections after another review iteration with Rasmus |
Date: |
Fri, 19 Dec 2014 19:07:29 +0000 |
branch: master
commit 1e68955ed7724b0c4613a2c648cda4860a57b1c9
Author: João Távora <address@hidden>
Commit: João Távora <address@hidden>
Corrections after another review iteration with Rasmus
* darkroom.el (darkroom-margins): New default.
(darkroom-margins): Tweak docstring.
(darkroom-margins): Better type spec.
(darkroom-margins-if-failed-guess): New defcustom.
(darkroom--real-window-width): New function.
(darkroom-guess-margins): Simplified.
(darkroom--compute-margins)
(darkroom--saved-mode-line-format)
(darkroom--saved-header-line-format, darkroom--saved-margins): Add
docstring.
(darkroom-mode): Added an assertion.
(darkroom--tentative-mode-driving): New variable.
(darkroom--maybe-enable): Use `darkroom--tentative-mode-driving'
(darkroom-mode): Attempt to ensure `darkroom--set-margins' always
happens after `darkroom--maybe-enable' in
`window-configuration-change-hook'.
---
darkroom.el | 115 ++++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 74 insertions(+), 41 deletions(-)
diff --git a/darkroom.el b/darkroom.el
index 2c08440..1a6ff43 100644
--- a/darkroom.el
+++ b/darkroom.el
@@ -50,7 +50,7 @@
:prefix "darkroom-"
:group 'emulations)
-(defcustom darkroom-margins 0.15
+(defcustom darkroom-margins 'darkroom-guess-margins
"Margins to use in `darkroom-mode'.
Its value can be:
@@ -62,10 +62,14 @@ Its value can be:
in columns.
- a function of no arguments that returns a cons cell interpreted
- like the previous option.
+ like the previous option. An example is
+ `darkroom-guess-margins', which see.
Value is effective when `darkroom-mode' is toggled."
- :type 'float
+ :type '(choice float
+ (cons integer integer)
+ (function-item darkroom-guess-margins :doc "Guess margins")
+ (function darkroom-guess-margins))
:group 'darkroom)
(defcustom darkroom-text-scale-increase 2
@@ -84,6 +88,36 @@ Value is passed to `text-scale-increase'."
:type 'float
:group 'darkroom)
+(defcustom darkroom-margins-if-failed-guess 0.15
+ "Margins when `darkroom-guess-margins' fails.
+If `darkroom-guess-margins' failed to figure out margins to
+center the text, use this percentage of window width for the
+symmetical margins."
+ :type 'float
+ :group 'darkroom)
+
+(defun darkroom--real-window-width ()
+ "Horrible hack to get the window width in characters.
+`window-width' ignores text scaling."
+ (let ((inhibit-read-only t)
+ (buffer-undo-list t)
+ (truncate-lines nil)
+ (truncate-partial-width-windows nil)
+ (word-wrap t)
+ (line-move-visual t))
+ (save-excursion
+ (with-silent-modifications
+ (let ((begin (point)))
+ (unwind-protect
+ (progn
+ (insert (make-string 10000 ?!))
+ (save-excursion
+ (goto-char begin)
+ (next-line)
+ (backward-char)
+ (current-column)))
+ (delete-region begin (point))))))))
+
(defun darkroom-guess-margins ()
"Guess suitable margins for `darkroom-margins'.
Collects some statistics about the buffer's line lengths, and
@@ -91,12 +125,9 @@ apply a heuristic to figure out how wide to set the
margins. If
the buffer's paragraphs are mostly filled to `fill-column',
margins should center it on the window, otherwise, margins of
0.15 percent are used."
- ;;; FIXME: broken when darkroom-text-scale-increase is anything but
- ;;; 0, since window-width ignores text scaling. Otherwise, a
- ;;; suitable default to put in `darkroom-margins', I guess.
(if visual-line-mode
- 0.15
- (let* ((window-width (window-width))
+ darkroom-margins-if-failed-guess
+ (let* ((window-width (darkroom--real-window-width))
(line-widths (save-excursion
(goto-char (point-min))
(cl-loop for start = (point)
@@ -107,34 +138,22 @@ margins should center it on the window, otherwise,
margins of
unless (zerop width)
collect width)))
(_longest-width (cl-reduce #'max line-widths :initial-value 0))
- (top-quartile-avg (cl-loop with n = (/ (length line-widths)
- 4)
- for width in (copy-sequence
- (sort line-widths #'>))
- for i from 1 upto n
- sum width into total
- finally (return (/ total n 1.0)))))
+ (top-quartile-avg
+ (let ((n4 (/ (length line-widths) 4)))
+ (/ (apply '+ (cl-subseq (sort line-widths '>) 0 n4)) n4))))
(cond
((> top-quartile-avg
window-width)
- (when (y-or-n-p
- (format
- (mapconcat
- #'identity
- '("Long lines detected!"
- "(top 25%% average %s chars and window-width is %s)"
- "Perhaps turn on visual-line-mode for a better darkroom?")
- "\n")
- top-quartile-avg window-width))
- (visual-line-mode 1))
- 0.15)
+ (message "Long lines detected. Consider turning on `visual-line-mode'")
+ darkroom-margins-if-failed-guess)
((> top-quartile-avg (* 0.9 fill-column))
(let ((margin (truncate (/ (- window-width top-quartile-avg) 2))))
(cons margin margin)))
(t
- 0.15)))))
+ darkroom-margins-if-failed-guess)))))
(defun darkroom--compute-margins ()
+ "Computes (LEFT . RIGHT) margins from `darkroom-margins'."
(let ((darkroom-margins
(if (functionp darkroom-margins)
(funcall darkroom-margins)
@@ -202,9 +221,18 @@ Set by `darkroom--set-margins'")
(define-key map (kbd "C-M--") 'darkroom-decrease-margins)
map))
-(defvar darkroom--saved-mode-line-format nil)
-(defvar darkroom--saved-header-line-format nil)
-(defvar darkroom--saved-margins nil)
+(defvar darkroom--saved-mode-line-format nil
+ "Mode line before `darkroom-mode' is turned on.")
+(defvar darkroom--saved-header-line-format nil
+ "Header line before `darkroom-mode' is turned on.")
+(defvar darkroom--saved-margins nil
+ "Margins before `darkroom-mode' is turned on.")
+;; (defvar darkroom--saved-text-scale-mode-amount nil
+;; "Text scale before `darkroom-mode' is turned on.")
+
+(defvar darkroom--tentative-mode-driving nil
+ "Non-nil if `darkroom-tentative-mode' toggles
+ `darkroom-mode'.")
(define-minor-mode darkroom-mode
"Remove visual distractions and focus on writing. When this
@@ -212,6 +240,10 @@ mode is active, everything but the buffer's text is elided
from
view. The buffer margins are set so that text is centered on
screen. Text size is increased (display engine allowing) by
`darkroom-text-scale-increase'." nil nil nil
+ (when (and darkroom-tentative-mode
+ (not darkroom--tentative-mode-driving))
+ (error
+ "Don't try to toggle `darkroom-mode' when in `darkroom-tentative-mode'"))
(cond (darkroom-mode
(set (make-local-variable 'darkroom--saved-margins) (window-margins))
(set (make-local-variable 'darkroom--saved-mode-line-format)
@@ -223,7 +255,7 @@ screen. Text size is increased (display engine allowing) by
(text-scale-increase darkroom-text-scale-increase)
(darkroom--set-margins (darkroom--compute-margins))
(add-hook 'window-configuration-change-hook 'darkroom--set-margins
- nil t))
+ t t))
(t
(setq mode-line-format darkroom--saved-mode-line-format
header-line-format darkroom--saved-header-line-format)
@@ -234,19 +266,20 @@ screen. Text size is increased (display engine allowing)
by
t))))
(defun darkroom--maybe-enable ()
- (cond ((and (not darkroom-mode) (= (count-windows) 1))
- (darkroom-mode 1))
- ((and darkroom-mode (> (count-windows) 1))
- (darkroom-mode -1))
- (t
- ;; (message "debug: buffer: %s windows: %s darkroom-mode: %s"
- ;; (current-buffer) (count-windows) darkroom-mode)
- )))
+ (let ((darkroom--tentative-mode-driving t))
+ (cond ((and (not darkroom-mode) (= (count-windows) 1))
+ (darkroom-mode 1))
+ ((and darkroom-mode (> (count-windows) 1))
+ (darkroom-mode -1))
+ (t
+ ;; (message "debug: buffer: %s windows: %s darkroom-mode: %s"
+ ;; (current-buffer) (count-windows) darkroom-mode)
+ ))))
(define-minor-mode darkroom-tentative-mode
- "Minor mode that enters `darkroom-mode' when all windws are deleted"
- nil "DarkroomT" nil
+ "Enters `darkroom-mode' when all other windows are deleted."
+ nil " Room" nil
(cond (darkroom-tentative-mode
(add-hook 'window-configuration-change-hook
'darkroom--maybe-enable nil t)
- [elpa] master 6cbfebe 09/26: Integrate some comments from Rasmus, (continued)
- [elpa] master 6cbfebe 09/26: Integrate some comments from Rasmus, João Távora, 2014/12/19
- [elpa] master b36123e 11/26: Tidy up margin calculation, João Távora, 2014/12/19
- [elpa] master a16a665 01/26: Initial commit, João Távora, 2014/12/19
- [elpa] master 3a1db96 13/26: Add some docstrings, João Távora, 2014/12/19
- [elpa] master a39ab2e 15/26: Minor tweaks, João Távora, 2014/12/19
- [elpa] master 90f5299 14/26: Prefix internal symbols with "darkroom--", João Távora, 2014/12/19
- [elpa] master 8b0daf6 12/26: Add (still non-functioning) `darkroom-compute-margins', João Távora, 2014/12/19
- [elpa] master 13717fb 17/26: Attempt a clearer design of modes, João Távora, 2014/12/19
- [elpa] master 20d5539 21/26: load cl-lib, João Távora, 2014/12/19
- [elpa] master 4bf9c10 19/26: Fix `darkroom-tentative-mode', João Távora, 2014/12/19
- [elpa] master 1e68955 16/26: Corrections after another review iteration with Rasmus,
João Távora <=
- [elpa] master 15e976b 22/26: Merge pull request #1 from syohex/require-cl-lib, João Távora, 2014/12/19
- [elpa] master 9674d79 20/26: Fix automatic margin calculation, João Távora, 2014/12/19
- [elpa] master 2887b5e 18/26: Redesign setting of margins, which now works per-window, João Távora, 2014/12/19
- [elpa] master 96e61a9 26/26: Add packages/darkroom by merging its upstream subtree, João Távora, 2014/12/19
- [elpa] master 5de9a5d 24/26: Fix darkroom-tentative-mode when switching window's buffer, João Távora, 2014/12/19
- [elpa] master 654e5fd 23/26: Fix margin calculation yet again., João Távora, 2014/12/19
- [elpa] master 7a8b5b6 25/26: Prepare for inclusion in GNU ELPA, João Távora, 2014/12/19