diff options
Diffstat (limited to 'kotl/kfill.el')
| -rw-r--r-- | kotl/kfill.el | 119 |
1 files changed, 57 insertions, 62 deletions
diff --git a/kotl/kfill.el b/kotl/kfill.el index a3e0c28..27a8b6c 100644 --- a/kotl/kfill.el +++ b/kotl/kfill.el @@ -1,4 +1,4 @@ -;;; kfill.el --- Fill and justify koutline cells +;;; kfill.el --- Fill and justify koutline cells -*- lexical-binding:t -*- ;; ;; Author: Bob Weiner ;; @@ -80,12 +80,18 @@ Setting this variable automatically makes it local to the current buffer.") ;;; Public functions ;;; ************************************************************************ +;; FIXME: circular dependency: kview `require's kfill because of +;; kfill:forward-line, and kfill would like to `require' kview because of +;; kcell-view:indent. +(declare-function kcell-view:indent "kview" (&optional pos lsl)) + (defun kfill:forward-line (&optional n) "Move N lines forward (backward if N is negative) to the start of line. If there isn’t room, go as far as possible (no error). Return the number of lines that could not be moved, otherwise 0." (or (integerp n) (setq n 1)) - (let ((opoint (point))) + (let (;; (opoint (point)) + ) (forward-visible-line n) (if (< n 0) nil @@ -106,66 +112,55 @@ number of lines that could not be moved, otherwise 0." (do-auto-fill)) (do-auto-fill)))) -(defun kfill:fill-paragraph (&optional arg skip-prefix-remove) - "Fill paragraph at or after point when in kotl-mode. Prefix ARG means justify as well." - (interactive (progn - (barf-if-buffer-read-only) - (list (if current-prefix-arg 'full) nil))) - ;; This may be called from `fill-region-as-paragraph' in "filladapt.el" - ;; which narrows the region to the current paragraph. A side-effect is - ;; that the cell identifier and indent information needed by this function - ;; when in kotl-mode is no longer visible. So we temporarily rewiden the - ;; buffer here. Don't rewiden past the paragraph of interest or any - ;; following blank line may be removed by the filling routines. - (save-restriction - (if (eq major-mode 'kotl-mode) - (narrow-to-region 1 (point-max))) - ;; Emacs expects a specific symbol here. - (if (and arg (not (symbolp arg))) (setq arg 'full)) - (or skip-prefix-remove (kfill:remove-paragraph-prefix)) - (catch 'done - (if (null fill-prefix) - (let ((paragraph-ignore-fill-prefix nil) - ;; Need this or Emacs ignores fill-prefix when - ;; inside a comment. - (comment-multi-line t) - (fill-paragraph-handle-comment t) - (paragraph-start paragraph-start) - (paragraph-separate paragraph-separate) - fill-prefix) - (if (kfill:adapt t) - (throw 'done (fill-paragraph arg))))) - ;; Kfill:adapt failed or fill-prefix is set, so do a basic - ;; paragraph fill as adapted from par-align.el. - (kfill:fallback-fill-paragraph arg skip-prefix-remove)))) - -;;; -;;; Redefine this built-in function so that it sets `prior-fill-prefix' also. -;;; -(defun set-fill-prefix (&optional turn-off) - "Set `fill-prefix' to the current line up to point or remove it if optional TURN-OFF flag is non-nil. -Also sets `prior-fill-prefix' to the previous value of `fill-prefix'. -Filling removes any prior fill prefix, adjusts line lengths and then adds the -fill prefix at the beginning of each line." - (interactive) - (setq prior-fill-prefix fill-prefix) - (let ((left-margin-pos (save-excursion (move-to-left-margin) (point)))) - (if (> (point) left-margin-pos) - (setq fill-prefix (if turn-off - nil - (buffer-substring left-margin-pos (point)))) - (setq fill-prefix nil))) - (when (equal prior-fill-prefix "") - (setq prior-fill-prefix nil)) - (when (equal fill-prefix "") - (setq fill-prefix nil)) - (cond (fill-prefix - (message "fill-prefix: \"%s\"; prior-fill-prefix: \"%s\"" - fill-prefix (or prior-fill-prefix ""))) - (prior-fill-prefix - (message "fill-prefix cancelled; prior-fill-prefix: \"%s\"" - prior-fill-prefix)) - (t (message "fill-prefix and prior-fill-prefix cancelled")))) +;; Redefine this built-in function. +(advice-add 'fill-paragraph :around #'kill:fill-paragraph) +(defun kill:fill-paragraph (orig-fun arg &optional skip-prefix-remove) + "`kotl-mode' specific paragraph filling code." + (if (not (derived-mode-p 'kotl-mode)) + (funcall orig-fun arg skip-prefix-remove) + ;; This may be called from `fill-region-as-paragraph' in "filladapt.el" + ;; which narrows the region to the current paragraph. A side-effect is + ;; that the cell identifier and indent information needed by this function + ;; when in kotl-mode is no longer visible. So we temporarily rewiden the + ;; buffer here. Don't rewiden past the paragraph of interest or any + ;; following blank line may be removed by the filling routines. + (save-restriction + (narrow-to-region 1 (point-max)) + ;; Emacs expects a specific symbol here. + (if (and arg (not (symbolp arg))) (setq arg 'full)) + (or skip-prefix-remove (kfill:remove-paragraph-prefix)) + (catch 'done + (if (null fill-prefix) + (let ((paragraph-ignore-fill-prefix nil) + ;; Need this or Emacs ignores fill-prefix when + ;; inside a comment. + (comment-multi-line t) + (paragraph-start paragraph-start) + (paragraph-separate paragraph-separate) + fill-prefix) + (if (kfill:adapt t) + (throw 'done (funcall orig-fun arg skip-prefix-remove))))) + ;; Kfill:adapt failed or fill-prefix is set, so do a basic + ;; paragraph fill as adapted from par-align.el. + (kfill:fallback-fill-paragraph arg skip-prefix-remove))))) + +;; Redefine this built-in function so that it sets `prior-fill-prefix' also. +(advice-add 'set-fill-prefix :around #'kfill:set-fill-prefix) +(defun kfill:set-fill-prefix (orig-fun &rest args) + "Set `prior-fill-prefix' to the previous value of `fill-prefix'." + (if (not (derived-mode-p 'kotl-mode)) + (apply orig-fun args) + (setq prior-fill-prefix fill-prefix) + (when (equal prior-fill-prefix "") + (setq prior-fill-prefix nil)) + (apply orig-fun args) + (cond (fill-prefix + (message "fill-prefix: \"%s\"; prior-fill-prefix: \"%s\"" + fill-prefix (or prior-fill-prefix ""))) + (prior-fill-prefix + (message "fill-prefix cancelled; prior-fill-prefix: \"%s\"" + prior-fill-prefix)) + (t (message "fill-prefix and prior-fill-prefix cancelled"))))) ;;; ************************************************************************ ;;; Private functions |
