[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 8b0daf6 12/26: Add (still non-functioning) `darkroom-compu
From: |
Jo�o T�vora |
Subject: |
[elpa] master 8b0daf6 12/26: Add (still non-functioning) `darkroom-compute-margins' |
Date: |
Fri, 19 Dec 2014 19:07:27 +0000 |
branch: master
commit 8b0daf6d1bbb355ab700cf30399e94d40aff3dab
Author: João Távora <address@hidden>
Commit: João Távora <address@hidden>
Add (still non-functioning) `darkroom-compute-margins'
* darkroom.el: Require 'cl-lib
(darkroom-guess-margins): New function for putting in
`darkroom-margins'. But broken due to `window-width' not being
suitable probably.
(darkroom-compute-margins): Redesigned.
(darkroom-set-margins): Don't `darkroom-compute-margins'
(darkroom-mode): Reordered statements.
---
darkroom.el | 126 ++++++++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 90 insertions(+), 36 deletions(-)
diff --git a/darkroom.el b/darkroom.el
index 864a6e4..23b4d05 100644
--- a/darkroom.el
+++ b/darkroom.el
@@ -4,6 +4,7 @@
;; Author: João Távora <address@hidden>
;; Keywords: convenience, emulations
+;; Package-Requires: ((cl-lib "0.5"))
;; Version: 0.1
;; This program is free software; you can redistribute it and/or modify
@@ -84,16 +85,69 @@ Value is passed to `text-scale-increase'."
:type 'float
:group 'darkroom)
+(defun darkroom-guess-margins ()
+ "Guess suitable margins for `darkroom-margins'.
+Collects some statistics about the buffer's line lengths, and
+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))
+ (line-widths (save-excursion
+ (goto-char (point-min))
+ (cl-loop for start = (point)
+ while (search-forward "\n"
+ 20000
+ 'no-error)
+ for width = (- (point) start 1)
+ 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)))))
+ (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)
+ ((> top-quartile-avg (* 0.9 fill-column))
+ (let ((margin (round (/ (- window-width top-quartile-avg) 2))))
+ (cons margin margin)))
+ (t
+ 0.15)))))
+
(defun darkroom-compute-margins ()
- (or darkroom-buffer-margins
- (cond ((functionp darkroom-margins)
- (funcall darkroom-margins))
- ((consp darkroom-margins)
- darkroom-margins)
- ((and (floatp darkroom-margins)
- (< darkroom-margins 1))
- (let ((delta (darkroom-float-to-columns darkroom-margins)))
- (cons delta delta))))))
+ (let ((darkroom-margins
+ (if (functionp darkroom-margins)
+ (funcall darkroom-margins)
+ darkroom-margins)))
+ (cond ((consp darkroom-margins)
+ darkroom-margins)
+ ((and (floatp darkroom-margins)
+ (< darkroom-margins 1))
+ (let ((delta (darkroom-float-to-columns darkroom-margins)))
+ (cons delta delta)))
+ (t
+ (error "Illegal value in `darkroom-margins'")))))
(defun darkroom-float-to-columns (f)
(ceiling (* (let ((edges (window-edges)))
@@ -105,13 +159,13 @@ Value is passed to `text-scale-increase'."
Set by `darkroom-set-margins'")
(defun darkroom-set-margins (&optional margins)
- "Set margins from MARGINS or `darkroom-compute-margins'."
+ "Set margins from MARGINS or `darkroom-buffer-margins'."
(let* ((window-configuration-change-hook nil))
(when margins
(when (null (car margins)) (setcar margins 0))
(when (null (cdr margins)) (setcdr margins 0)))
(set (make-local-variable 'darkroom-buffer-margins)
- (or margins (darkroom-compute-margins)))
+ (or margins darkroom-buffer-margins))
(walk-windows #'(lambda (w)
(when (eq (window-buffer w) (current-buffer))
(setq fringes-outside-margins
@@ -152,31 +206,31 @@ Set by `darkroom-set-margins'")
(defvar darkroom-saved-margins nil)
(define-minor-mode darkroom-mode
- "Remove visual distractions and focus on writing.
-When this 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
-(cond (darkroom-mode
- (set (make-local-variable 'darkroom-saved-mode-line-format)
- mode-line-format)
- (set (make-local-variable 'darkroom-saved-header-line-format)
- header-line-format)
- (set (make-local-variable 'darkroom-saved-margins) (window-margins))
- (setq mode-line-format nil)
- (setq header-line-format nil)
- (darkroom-set-margins)
- (text-scale-increase darkroom-text-scale-increase)
- (add-hook 'window-configuration-change-hook 'darkroom-set-margins
- nil t))
- (t
- (setq mode-line-format darkroom-saved-mode-line-format
- header-line-format darkroom-saved-header-line-format)
- (text-scale-decrease 2)
- (let (darkroom-buffer-margins)
- (darkroom-set-margins darkroom-saved-margins))
- (remove-hook 'window-configuration-change-hook 'darkroom-set-margins
- t))))
+ "Remove visual distractions and focus on writing. When this
+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
+ (cond (darkroom-mode
+ (set (make-local-variable 'darkroom-saved-margins) (window-margins))
+ (set (make-local-variable 'darkroom-saved-mode-line-format)
+ mode-line-format)
+ (set (make-local-variable 'darkroom-saved-header-line-format)
+ header-line-format)
+ (setq mode-line-format nil)
+ (setq header-line-format nil)
+ (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
+ (setq mode-line-format darkroom-saved-mode-line-format
+ header-line-format darkroom-saved-header-line-format)
+ (text-scale-decrease darkroom-text-scale-increase)
+ (let (darkroom-buffer-margins)
+ (darkroom-set-margins darkroom-saved-margins))
+ (remove-hook 'window-configuration-change-hook 'darkroom-set-margins
+ t))))
(defun darkroom-maybe-enable ()
(cond ((and (not darkroom-mode) (= (count-windows) 1))
- [elpa] master c6e0c3a 07/26: Leave darkroom-mode when leaving tentative-mode, (continued)
- [elpa] master c6e0c3a 07/26: Leave darkroom-mode when leaving tentative-mode, Jo�o T�vora, 2014/12/19
- [elpa] master 744d72a 08/26: Add .gitignore, Jo�o T�vora, 2014/12/19
- [elpa] master 9cd8dfd 06/26: Now works with multiple windows for same darkroom buffer, Jo�o T�vora, 2014/12/19
- [elpa] master 786b5a3 10/26: Integrate another comment from Rasmus, Jo�o T�vora, 2014/12/19
- [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 <=
- [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, 2014/12/19
- [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