| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
(require 'erc) |
|---|
| 36 |
(require 'erc-stamp) |
|---|
| 37 |
|
|---|
| 38 |
(defgroup erc-fill nil |
|---|
| 39 |
"Filling means to reformat long lines in different ways." |
|---|
| 40 |
:group 'erc) |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
(erc-define-minor-mode erc-fill-mode |
|---|
| 44 |
"Toggle ERC fill mode. |
|---|
| 45 |
With numeric arg, turn ERC fill mode on if and only if arg is |
|---|
| 46 |
positive. In ERC fill mode, messages in the channel buffers are |
|---|
| 47 |
filled." |
|---|
| 48 |
nil nil nil |
|---|
| 49 |
:global t :group 'erc-fill |
|---|
| 50 |
(if erc-fill-mode |
|---|
| 51 |
(erc-fill-enable) |
|---|
| 52 |
(erc-fill-disable))) |
|---|
| 53 |
|
|---|
| 54 |
(defun erc-fill-enable () |
|---|
| 55 |
"Setup hooks for `erc-fill-mode'." |
|---|
| 56 |
(interactive) |
|---|
| 57 |
(add-hook 'erc-insert-modify-hook 'erc-fill) |
|---|
| 58 |
(add-hook 'erc-send-modify-hook 'erc-fill)) |
|---|
| 59 |
|
|---|
| 60 |
(defun erc-fill-disable () |
|---|
| 61 |
"Cleanup hooks, disable `erc-fill-mode'." |
|---|
| 62 |
(interactive) |
|---|
| 63 |
(remove-hook 'erc-insert-modify-hook 'erc-fill) |
|---|
| 64 |
(remove-hook 'erc-send-modify-hook 'erc-fill)) |
|---|
| 65 |
|
|---|
| 66 |
(defcustom erc-fill-prefix nil |
|---|
| 67 |
"Values used as `fill-prefix' for `erc-fill-variable'. |
|---|
| 68 |
nil means fill with space, a string means fill with this string." |
|---|
| 69 |
:group 'erc-fill |
|---|
| 70 |
:type '(choice (const nil) string)) |
|---|
| 71 |
|
|---|
| 72 |
(defcustom erc-fill-function 'erc-fill-variable |
|---|
| 73 |
"Function to use for filling messages. |
|---|
| 74 |
|
|---|
| 75 |
Variable Filling with an `erc-fill-prefix' of nil: |
|---|
| 76 |
|
|---|
| 77 |
<shortnick> this is a very very very long message with no |
|---|
| 78 |
meaning at all |
|---|
| 79 |
|
|---|
| 80 |
Variable Filling with an `erc-fill-prefix' of four spaces: |
|---|
| 81 |
|
|---|
| 82 |
<shortnick> this is a very very very long message with no |
|---|
| 83 |
meaning at all |
|---|
| 84 |
|
|---|
| 85 |
Static Filling with `erc-fill-static-center' of 27: |
|---|
| 86 |
|
|---|
| 87 |
<shortnick> foo bar baz |
|---|
| 88 |
<a-very-long-nick> foo bar baz quuuuux |
|---|
| 89 |
<shortnick> this is a very very very long message with no |
|---|
| 90 |
meaning at all |
|---|
| 91 |
|
|---|
| 92 |
These two styles are implemented using `erc-fill-variable' and |
|---|
| 93 |
`erc-fill-static'. You can, of course, define your own filling |
|---|
| 94 |
function. Narrowing to the region in question is in effect while your |
|---|
| 95 |
function is called." |
|---|
| 96 |
:group 'erc-fill |
|---|
| 97 |
:type '(choice (const :tag "Variable Filling" erc-fill-variable) |
|---|
| 98 |
(const :tag "Static Filling" erc-fill-static) |
|---|
| 99 |
function)) |
|---|
| 100 |
|
|---|
| 101 |
(defcustom erc-fill-static-center 27 |
|---|
| 102 |
"Column around which all statically filled messages will be |
|---|
| 103 |
centered. This column denotes the point where the ' ' character |
|---|
| 104 |
between <nickname> and the entered text will be put, thus aligning |
|---|
| 105 |
nick names right and text left." |
|---|
| 106 |
:group 'erc-fill |
|---|
| 107 |
:type 'integer) |
|---|
| 108 |
|
|---|
| 109 |
(defcustom erc-fill-variable-maximum-indentation 17 |
|---|
| 110 |
"If we indent a line after a long nick, don't indent more then this |
|---|
| 111 |
characters. Set to nil to disable." |
|---|
| 112 |
:group 'erc-fill |
|---|
| 113 |
:type 'integer) |
|---|
| 114 |
|
|---|
| 115 |
(defcustom erc-fill-column 78 |
|---|
| 116 |
"The column at which a filled paragraph is broken." |
|---|
| 117 |
:group 'erc-fill |
|---|
| 118 |
:type 'integer) |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
(defun erc-fill () |
|---|
| 122 |
"Fill a region using the function referenced in `erc-fill-function'. |
|---|
| 123 |
You can put this on `erc-insert-modify-hook' and/or `erc-send-modify-hook'." |
|---|
| 124 |
(unless (erc-string-invisible-p (buffer-substring (point-min) (point-max))) |
|---|
| 125 |
(when erc-fill-function |
|---|
| 126 |
|
|---|
| 127 |
(goto-char (point-min)) |
|---|
| 128 |
(save-match-data |
|---|
| 129 |
(while (and (looking-at "[ \t\n]*$") |
|---|
| 130 |
(= (forward-line 1) 0)))) |
|---|
| 131 |
(unless (eobp) |
|---|
| 132 |
(save-restriction |
|---|
| 133 |
(narrow-to-region (point) (point-max)) |
|---|
| 134 |
(funcall erc-fill-function)))))) |
|---|
| 135 |
|
|---|
| 136 |
(defun erc-fill-static () |
|---|
| 137 |
"Fills a text such that messages start at column `erc-fill-static-center'." |
|---|
| 138 |
(save-match-data |
|---|
| 139 |
(goto-char (point-min)) |
|---|
| 140 |
(looking-at "^\\(\\S-+\\)") |
|---|
| 141 |
(let ((nick (match-string 1))) |
|---|
| 142 |
(let ((fill-column (- erc-fill-column (erc-timestamp-offset))) |
|---|
| 143 |
(fill-prefix (make-string erc-fill-static-center 32))) |
|---|
| 144 |
(insert (make-string (max 0 (- erc-fill-static-center |
|---|
| 145 |
(length nick) 1)) |
|---|
| 146 |
32)) |
|---|
| 147 |
(erc-fill-regarding-timestamp)) |
|---|
| 148 |
(erc-restore-text-properties)))) |
|---|
| 149 |
|
|---|
| 150 |
(defun erc-fill-variable () |
|---|
| 151 |
"Fill from `point-min' to `point-max'." |
|---|
| 152 |
(let ((fill-prefix erc-fill-prefix) |
|---|
| 153 |
(fill-column (or erc-fill-column fill-column))) |
|---|
| 154 |
(goto-char (point-min)) |
|---|
| 155 |
(if fill-prefix |
|---|
| 156 |
(let ((first-line-offset (make-string (erc-timestamp-offset) 32))) |
|---|
| 157 |
(insert first-line-offset) |
|---|
| 158 |
(fill-region (point-min) (point-max) t t) |
|---|
| 159 |
(goto-char (point-min)) |
|---|
| 160 |
(delete-char (length first-line-offset))) |
|---|
| 161 |
(save-match-data |
|---|
| 162 |
(let* ((nickp (looking-at "^\\(\\S-+\\)")) |
|---|
| 163 |
(nick (if nickp |
|---|
| 164 |
(match-string 1) |
|---|
| 165 |
"")) |
|---|
| 166 |
(fill-column (- erc-fill-column (erc-timestamp-offset))) |
|---|
| 167 |
(fill-prefix (make-string (min (+ 1 (length nick)) |
|---|
| 168 |
(- fill-column 1) |
|---|
| 169 |
(or erc-fill-variable-maximum-indentation |
|---|
| 170 |
fill-column)) |
|---|
| 171 |
32))) |
|---|
| 172 |
(erc-fill-regarding-timestamp)))) |
|---|
| 173 |
(erc-restore-text-properties))) |
|---|
| 174 |
|
|---|
| 175 |
(defun erc-fill-regarding-timestamp () |
|---|
| 176 |
"Fills a text such that messages start at column `erc-fill-static-center'." |
|---|
| 177 |
(fill-region (point-min) (point-max) t t) |
|---|
| 178 |
(goto-char (point-min)) |
|---|
| 179 |
(forward-line) |
|---|
| 180 |
(indent-rigidly (point) (point-max) (erc-timestamp-offset))) |
|---|
| 181 |
|
|---|
| 182 |
(defun erc-timestamp-offset () |
|---|
| 183 |
"Get length of timestamp if inserted left." |
|---|
| 184 |
(if (and (boundp 'erc-timestamp-format) |
|---|
| 185 |
erc-timestamp-format |
|---|
| 186 |
(eq erc-insert-timestamp-function 'erc-insert-timestamp-left) |
|---|
| 187 |
(not erc-hide-timestamps)) |
|---|
| 188 |
(length (format-time-string erc-timestamp-format)) |
|---|
| 189 |
0)) |
|---|
| 190 |
|
|---|
| 191 |
(provide 'erc-fill) |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|