unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 95b388cbf84cda4b5acdbb02f0cf322ed44cbd6a 13221 bytes (raw)
name: lisp/erc/erc-fill.el 	 # note: path name is non-authoritative(*)

  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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
 
;;; erc-fill.el --- Filling IRC messages in various ways  -*- lexical-binding: t; -*-

;; Copyright (C) 2001-2004, 2006-2023 Free Software Foundation, Inc.

;; Author: Andreas Fuchs <asf@void.at>
;;         Mario Lang <mlang@delysid.org>
;; Maintainer: Amin Bandali <bandali@gnu.org>, F. Jason Park <jp@neverwas.me>
;; URL: https://www.emacswiki.org/emacs/ErcFilling

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This package implements filling of messages sent and received.  Use
;; `erc-fill-mode' to switch it on.  Customize `erc-fill-function' to
;; change the style.

;;; Code:

(require 'erc)
(require 'erc-stamp); for the timestamp stuff

(defgroup erc-fill nil
  "Filling means to reformat long lines in different ways."
  :group 'erc)

;;;###autoload(autoload 'erc-fill-mode "erc-fill" nil t)
(define-erc-module fill nil
  "Manage filling in ERC buffers.
ERC fill mode is a global minor mode.  When enabled, messages in
the channel buffers are filled."
  ;; FIXME ensure a consistent ordering relative to hook members from
  ;; other modules.  Ideally, this module's processing should happen
  ;; after "morphological" modifications to a message's text but
  ;; before superficial decorations.
  ((add-hook 'erc-insert-modify-hook #'erc-fill)
   (add-hook 'erc-send-modify-hook #'erc-fill))
  ((remove-hook 'erc-insert-modify-hook #'erc-fill)
   (remove-hook 'erc-send-modify-hook #'erc-fill)))

(defcustom erc-fill-prefix nil
  "Values used as `fill-prefix' for `erc-fill-variable'.
nil means fill with space, a string means fill with this string."
  :type '(choice (const nil) string))

(defcustom erc-fill-function 'erc-fill-variable
  "Function to use for filling messages.

Variable Filling with an `erc-fill-prefix' of nil:

<shortnick> this is a very very very long message with no
	    meaning at all

Variable Filling with an `erc-fill-prefix' of four spaces:

<shortnick> this is a very very very long message with no
    meaning at all

Static Filling with `erc-fill-static-center' of 27:

		<shortnick> foo bar baz
	 <a-very-long-nick> foo bar baz quuuuux
		<shortnick> this is a very very very long message with no
			    meaning at all

These two styles are implemented using `erc-fill-variable' and
`erc-fill-static'.  You can, of course, define your own filling
function.  Narrowing to the region in question is in effect while your
function is called.

A third style resembles static filling but \"wraps\" instead of
fills, courtesy of `visual-line-mode' mode, which ERC
automatically enables when this option is `erc-fill-wrap' or
`erc-fill-wrap-mode' is active.  Set `erc-fill-static-center' to
your preferred initial \"prefix\" width.  For adjusting the width
during a session, see the command `erc-fill-wrap-nudge'."
  :type '(choice (const :tag "Variable Filling" erc-fill-variable)
                 (const :tag "Static Filling" erc-fill-static)
                 (const :tag "Dynamic word-wrap" erc-fill-wrap)
                 function))

(defcustom erc-fill-static-center 27
  "Column around which all statically filled messages will be centered.
This column denotes the point where the ` ' character between
<nickname> and the entered text will be put, thus aligning nick
names right and text left.

Also used by the `erc-fill-function' variant `erc-fill-wrap' for
its initial leading \"prefix\" width."
  :type 'integer)

(defcustom erc-fill-variable-maximum-indentation 17
  "Don't indent a line after a long nick more than this many characters.
Set to nil to disable."
  :type 'integer)

(defcustom erc-fill-column 78
  "The column at which a filled paragraph is broken."
  :type 'integer)

;;;###autoload
(defun erc-fill ()
  "Fill a region using the function referenced in `erc-fill-function'.
You can put this on `erc-insert-modify-hook' and/or `erc-send-modify-hook'."
  (unless (erc-string-invisible-p (buffer-substring (point-min) (point-max)))
    (when erc-fill-function
      ;; skip initial empty lines
      (goto-char (point-min))
      (save-match-data
        (while (and (looking-at "[ \t\n]*$")
                    (= (forward-line 1) 0))))
      (unless (eobp)
        (save-restriction
          (narrow-to-region (point) (point-max))
          (funcall erc-fill-function))))))

(defun erc-fill-static ()
  "Fills a text such that messages start at column `erc-fill-static-center'."
  (save-restriction
    (goto-char (point-min))
    (looking-at "^\\(\\S-+\\)")
    (let ((nick (match-string 1)))
        (let ((fill-column (- erc-fill-column (erc-timestamp-offset)))
              (fill-prefix (make-string erc-fill-static-center 32)))
          (insert (make-string (max 0 (- erc-fill-static-center
                                         (length nick) 1))
                               32))
          (erc-fill-regarding-timestamp))
        (erc-restore-text-properties))))

(defun erc-fill-variable ()
  "Fill from `point-min' to `point-max'."
  (let ((fill-prefix erc-fill-prefix)
        (fill-column (or erc-fill-column fill-column)))
    (goto-char (point-min))
    (if fill-prefix
        (let ((first-line-offset (make-string (erc-timestamp-offset) 32)))
          (insert first-line-offset)
          (fill-region (point-min) (point-max) t t)
          (goto-char (point-min))
          (delete-char (length first-line-offset)))
      (save-match-data
        (let* ((nickp (looking-at "^\\(\\S-+\\)"))
               (nick (if nickp
                         (match-string 1)
                       ""))
               (fill-column (- erc-fill-column (erc-timestamp-offset)))
               (fill-prefix (make-string (min (+ 1 (length nick))
                                              (- fill-column 1)
                                              (or erc-fill-variable-maximum-indentation
                                                  fill-column))
                                         32)))
          (erc-fill-regarding-timestamp))))
    (erc-restore-text-properties)))

(defvar-local erc-fill--wrap-prefix nil)
(defvar-local erc-fill--wrap-value nil)

(define-erc-module fill-wrap nil
  "Fill style leveraging `visual-line-mode'.
This local module depends on the global `fill' module.  To use
it, either include `fill-wrap' in `erc-modules' or set
`erc-fill-function' to `erc-fill-wrap'.  You can also manually
invoke one of the minor-mode toggles."
  ((let (msg)
     (unless erc-fill-mode
       (unless (memq 'fill erc-modules)
         (setq msg
               (concat "WARNING: enabling default global module `fill' needed "
                       " by local module `fill-wrap'.  This will impact all"
                       " ERC sessions.  Add `fill' to `erc-modules' to avoid "
                       " this warning. See Info:\"(erc) Modules\" for more.")))
       (erc-fill-mode +1))
     (unless (eq erc-fill-function #'erc-fill-wrap)
       (setq-local erc-fill-function #'erc-fill-wrap))
     (when-let* ((vars (or erc--server-reconnecting erc--target-priors))
                 ((alist-get 'erc-fill-wrap-mode vars)))
       (setq erc-fill--wrap-value (alist-get 'erc-fill--wrap-value vars)
             erc-fill--wrap-prefix (alist-get 'erc-fill--wrap-prefix vars)))
     (when (eq erc-timestamp-use-align-to 'margin)
       (erc-timestamp--display-margin-mode +1))
     (setq erc-fill--wrap-value
           (or erc-fill--wrap-value erc-fill-static-center)
           ;;
           erc-fill--wrap-prefix
           (or erc-fill--wrap-prefix
               (list 'space :width erc-fill--wrap-value)))
     (visual-line-mode +1)
     (when msg
       (erc-display-error-notice nil msg))))
  ((when erc-timestamp--display-margin-mode
     (erc-timestamp--display-margin-mode -1))
   (kill-local-variable 'erc-button--add-nickname-face-function)
   (kill-local-variable 'erc-fill--wrap-prefix)
   (kill-local-variable 'erc-fill--wrap-value)
   (kill-local-variable 'erc-fill-function)
   (visual-line-mode -1))
  'local)

(defvar-local erc-fill--wrap-length-function nil
  "Function to determine length of perceived nickname.
It should return an integer representing the length of the
nickname, including any enclosing brackets, or nil, to fall back
to the default behavior of taking the length from the first word.")

(defun erc-fill-wrap ()
  "Use text props to mimic the effect of `erc-fill-static'.
See `erc-fill-wrap-mode' for details."
  (unless erc-fill-wrap-mode
    (erc-fill-wrap-mode +1))
  (save-excursion
    (goto-char (point-min))
    (let ((len (or (and erc-fill--wrap-length-function
                        (funcall erc-fill--wrap-length-function))
                   (progn (skip-syntax-forward "^-")
                          (- (point) (point-min))))))
      (erc-put-text-properties (point-min) (point-max)
                               '(line-prefix wrap-prefix) nil
                               `((space :width ,(- erc-fill--wrap-value 1 len))
                                 ,erc-fill--wrap-prefix)))))

;; This is an experimental helper for third-party modules.  You could,
;; for example, use this to automatically resize the prefix to a
;; fraction of the window's width on some event change.

(defun erc-fill--wrap-fix (&optional value)
  "Re-wrap from `point-min' to `point-max'.
Reset prefix to VALUE, when given."
  (save-excursion
    (when value
      (setq erc-fill--wrap-value value
            erc-fill--wrap-prefix (list 'space :width value)))
    (let ((inhibit-field-text-motion t)
          (inhibit-read-only t))
      (goto-char (point-min))
      (while (and (zerop (forward-line))
                  (< (point) (min (point-max) erc-insert-marker)))
        (save-restriction
          (narrow-to-region (pos-bol) (pos-eol))
          (erc-fill-wrap))))))

(defun erc-fill--wrap-nudge (arg)
  (save-excursion
    (save-restriction
      (widen)
      (let ((inhibit-field-text-motion t)
            (inhibit-read-only t) ; necessary?
            (p (goto-char (point-min))))
        (when (zerop arg)
          (setq arg (- erc-fill-static-center erc-fill--wrap-value)))
        (cl-incf (caddr erc-fill--wrap-prefix) arg)
        (cl-incf erc-fill--wrap-value arg)
        (while (setq p (next-single-property-change p 'line-prefix))
          (when-let ((v (get-text-property p 'line-prefix)))
            (cl-incf (caddr v) arg)
            (when-let
                ((e (text-property-not-all p (point-max) 'line-prefix v)))
              (goto-char e)))))))
  arg)

(defun erc-fill-wrap-nudge (arg)
  "Adjust `erc-fill-wrap' by ARG columns.
Offer to repeat command in a manner similar to
`text-scale-adjust'.  Note that misalignment may occur when
messages contain decorations applied by third-party modules.
See `erc-fill--wrap-fix' for a workaround."
  (interactive "p")
  (unless erc-fill--wrap-value
    (cl-assert (not erc-fill-wrap-mode))
    (user-error "Minor mode `erc-fill-wrap-mode' disabled"))
  (let ((total (erc-fill--wrap-nudge arg))
        (start (window-start))
        (marker (set-marker (make-marker) (point))))
    (when (zerop arg)
      (setq arg 1))
    (set-transient-map
     (let ((map (make-sparse-keymap)))
       (dolist (key '(?+ ?= ?- ?0))
         (let ((a (pcase key
                    (?0 0)
                    (?- (- (abs arg)))
                    (_ (abs arg)))))
           (define-key map (vector (list key))
                       (lambda ()
                         (interactive)
                         (cl-incf total (erc-fill--wrap-nudge a))
                         (set-window-start (selected-window) start)
                         (goto-char marker)))))
       map)
     t
     (lambda ()
       (set-marker marker nil)
       (message "Fill prefix: %d (%+d col%s)"
                erc-fill--wrap-value total (if (> (abs total) 1) "s" "")))
     "Use %k for further adjustment"
     1)
    (goto-char marker)
    (set-window-start (selected-window) start)))

(defun erc-fill-regarding-timestamp ()
  "Fills a text such that messages start at column `erc-fill-static-center'."
  (fill-region (point-min) (point-max) t t)
  (goto-char (point-min))
  (forward-line)
  (indent-rigidly (point) (point-max) (erc-timestamp-offset)))

(defun erc-timestamp-offset ()
  "Get length of timestamp if inserted left."
  (if (and (boundp 'erc-timestamp-format)
           erc-timestamp-format
           (eq erc-insert-timestamp-function 'erc-insert-timestamp-left)
           (not erc-hide-timestamps))
      (length (format-time-string erc-timestamp-format))
    0))

(provide 'erc-fill)

;;; erc-fill.el ends here
;; Local Variables:
;; generated-autoload-file: "erc-loaddefs.el"
;; End:

debug log:

solving 95b388cbf84 ...
found 95b388cbf84 in https://yhetil.org/emacs-bugs/87tu0nao77.fsf@neverwas.me/
found caf401bf222 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 caf401bf2225c4ab1445c54a3df59ee7cfc0fa25	lisp/erc/erc-fill.el

applying [1/1] https://yhetil.org/emacs-bugs/87tu0nao77.fsf@neverwas.me/
diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index caf401bf222..95b388cbf84 100644

Checking patch lisp/erc/erc-fill.el...
Applied patch lisp/erc/erc-fill.el cleanly.

index at:
100644 95b388cbf84cda4b5acdbb02f0cf322ed44cbd6a	lisp/erc/erc-fill.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).