Skip to content

Commit

Permalink
Keep track of frame to which changes are applied.
Browse files Browse the repository at this point in the history
  • Loading branch information
joostkremers committed Nov 11, 2015
1 parent 255c9b2 commit 48b1798
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 116 deletions.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Upgrading from version 2 ##

The current version of `writeroom-mode` is 3.0. If you’re upgrading from version 2.x and you have custom global effects, you will probably have to redo them, because the arguments passed to the global effect functions have been changed to make them compatible with those used to (de)activate minor modes. See the section [Adding global effects](#adding-global-effects) for details.
The current version of `writeroom-mode` is 3.1. If you’re upgrading from version 2.x and you have custom global effects, you will probably have to redo them, because the arguments passed to the global effect functions have been changed to make them compatible with those used to (de)activate minor modes. See the section [Adding global effects](#adding-global-effects) for details.


## Installation ##
Expand All @@ -32,6 +32,22 @@ The last three effects are buffer-local. The other effects apply to the current
All these effects can be disabled or customised. In addition, there are several more options that are disabled by default but can be enabled in the customisation buffer.


## Multiple writeroom-mode buffers ##

It is possible to activate `writeroom-mode` in more than one buffer. The global effects are of course activated only once and they remain active until `writeroom-mode` is deactivated in *all* buffers. Alternatively, if you use `writeroom-mode` in multiple buffers with particular major modes (e.g., `text-mode`, `markdown-mode`), you can use the global minor mode `global-writeroom-mode`. This function enables the global effects and activates the buffer-local effects in all (current and future) buffers that have a major mode listed in the user option `writeroom-major-modes` (by default only `text-mode`).

When `global-writeroom-mode` is active, the function `writeroom-mode` can still be called to enable or disable `writeroom-mode` in individual buffers (regardless of their major mode, of course). Calling `global-writeroom-mode` again disables `writeroom-mode` in all buffers in which it is active, also those in which it was activated manually.


## Frame effects ##

Most of the global effects that `writeroom-mode` enables are handled by setting specific frame parameters. This means that they apply to the current frame. If you switch to another frame and display a `writeroom-mode` buffer, only the buffer-local effects will be visible.

`writeroom-mode` tries to make sure that it only affects one frame, and that it restores that particular frame when it is deactivated in the last buffer. This means it should be safe to activate `writeroom-mode` in one frame and deactivate it in another. Killing the `writeroom-mode` frame should also be safe.

The affected frame is always restored to its original state, before `writeroom-mode` was activated, even if you change any of the frame parameters manually while `writeroom-mode` is active.


## Customisation ##

### Global Writeroom Mode ###
Expand Down Expand Up @@ -109,18 +125,11 @@ If, for some reason, you need to look at the full mode line, you can use the com
The first `define-key` disables the binding for `s-?`. Substitute your preferred key binding in the second line to bind `writeroom-toggle-mode-line` to it.


## Multiple writeroom-mode buffers ##

It is possible to activate `writeroom-mode` in more than one buffer. The global effects are of course activated only once and they remain active until `writeroom-mode` is deactivated in *all* buffers. Alternatively, if you use `writeroom-mode` in multiple buffers with particular major modes (e.g., `text-mode`, `markdown-mode`), you can use the global minor mode `global-writeroom-mode`. This function enables the global effects and activates the buffer-local effects in all (current and future) buffers that have a major mode listed in the user option `writeroom-major-modes` (by default only `text-mode`).

When `global-writeroom-mode` is active, the function `writeroom-mode` can still be called to enable or disable `writeroom-mode` in individual buffers (regardless of their major mode, of course). Calling `global-writeroom-mode` again disables `writeroom-mode` in all buffers in which it is active, also those in which it was activated manually.


## Adding global effects ##

It is possible to define your own global effects and have them activated automatically when `writeroom-mode` is activated. For example, you may want to add your own font or colour effects, or replace the default fullscreen function with one that works in an older Emacs version. To do this, you should write a function that takes one argument and that activates the effect if the argument is `1` and disables it if the argument is `-1`. Then add this function to the user option `writeroom-global-effects` by checking the box "Custom effects", clicking the [INS] button and adding the function to the list.
It is possible to add your own global effects to `writeroom-mode`. If there is a global minor mode that you want turned on when `writeroom-mode` is activated for the first time, you can simply add it to the user option `writeroom-global-effects` by checking the box "Custom effects", clicking the [INS] button and adding the function to the list.

To give an example, if you want to activate a minimalist colour theme in `writeroom-mode`, you can write the following function:
Alternatively, you can also write your own function. This function should take one argument and enable the effect if the argument is `1` and disable it if the argument is `-1`. To give an example, if you want to activate a minimalist colour theme in `writeroom-mode`, you can write the following function:

```lisp
(defun my-writeroom-theme (arg)
Expand All @@ -131,4 +140,13 @@ To give an example, if you want to activate a minimalist colour theme in `writer
(disable-theme 'minimalist-dark))))
```

Note that the argument that global effect functions should accept is compatible with the arguments that minor mode functions take when called from Lisp: a positive number (among other possibilities) activates and a negative number deactivates. This makes it possible to add a global minor mode function to `writeroom-global-effects` and have it do the right thing.
If your function affects the frame, you should make sure that it only affects the `writeroom-mode` frame by passing the variable `writeroom--frame` to all frame-changing functions. If your frame-effect involves changing the value of a frame parameter, you ma be able to use the macro `define-writeroom-global-effect`, see its doc string for details.

In principle, it is not a good idea to define a custom global effect function as a toggle, but if you are sure you'll only ever use a single frame, it should be safe enough. For example, sometimes setting the `fullscreen` frame parameter does not work. In this case, if you're on Linux, you could send an X client message directly:

```lisp
(defun my-toggle-fullscreen (_)
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_FULLSCREEN" 0)))
```

28 changes: 19 additions & 9 deletions writeroom-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
;; Maintainer: Joost Kremers <joostkremers@fastmail.fm>
;; Created: 11 July 2012
;; Package-Requires: ((emacs "24.1") (visual-fill-column "1.4"))
;; Version: 3.0
;; Version: 3.1
;; Keywords: text

;; Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,6 +45,10 @@

(require 'visual-fill-column)

(defvar writeroom--frame nil
"The frame in which `writeroom-mode' is activated.
The global effects only apply to this frame.")

(defvar writeroom--buffers nil
"List of buffers in which `writeroom-mode' is activated.")

Expand Down Expand Up @@ -195,13 +199,14 @@ effect is deactivated."
(let ((wfp (intern (format "writeroom-%s" fp))))
`(fset (quote ,(intern (format "writeroom-set-%s" fp)))
(lambda (&optional arg)
(cond
((= arg 1) ; activate
(set-frame-parameter nil (quote ,wfp) (frame-parameter nil (quote ,fp)))
(set-frame-parameter nil (quote ,fp) ,value))
((= arg -1) ; deactivate
(set-frame-parameter nil (quote ,fp) (frame-parameter nil (quote ,wfp)))
(set-frame-parameter nil (quote ,wfp) nil)))))))
(when (frame-live-p writeroom--frame)
(cond
((= arg 1) ; activate
(set-frame-parameter writeroom--frame (quote ,wfp) (frame-parameter writeroom--frame (quote ,fp)))
(set-frame-parameter writeroom--frame (quote ,fp) ,value))
((= arg -1) ; deactivate
(set-frame-parameter writeroom--frame (quote ,fp) (frame-parameter writeroom--frame (quote ,wfp)))
(set-frame-parameter writeroom--frame (quote ,wfp) nil))))))))

(define-writeroom-global-effect fullscreen writeroom-fullscreen-effect)
(define-writeroom-global-effect alpha '(100 100))
Expand Down Expand Up @@ -245,7 +250,8 @@ adjusts `writeroom--buffers' and the global effects accordingly."
(when writeroom-mode
(setq writeroom--buffers (delq (current-buffer) writeroom--buffers))
(when (not writeroom--buffers)
(writeroom--set-global-effects -1))))
(writeroom--set-global-effects -1)
(setq writeroom--frame nil))))

(add-hook 'kill-buffer-hook #'writeroom--kill-buffer-function)

Expand Down Expand Up @@ -282,10 +288,13 @@ activated."
writeroom--local-variables))
(setq writeroom--saved-visual-fill-column visual-fill-column-mode)

;; activate global effects
(when (not writeroom--buffers)
(setq writeroom--frame (selected-frame))
(writeroom--set-global-effects 1)
(if writeroom-restore-window-config
(setq writeroom--saved-window-config (current-window-configuration))))

(push (current-buffer) writeroom--buffers)

(when writeroom-maximize-window
Expand Down Expand Up @@ -326,6 +335,7 @@ buffer in which it was active."
(setq writeroom--buffers (delq (current-buffer) writeroom--buffers))
(when (not writeroom--buffers)
(writeroom--set-global-effects -1)
(setq writeroom--frame nil)
(if writeroom-restore-window-config
(set-window-configuration writeroom--saved-window-config)))

Expand Down
Loading

0 comments on commit 48b1798

Please sign in to comment.