emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] scratch/hyperbole-lexbind e1a95cc 04/20: Improve paragraph fillin


From: Stefan Monnier
Subject: [elpa] scratch/hyperbole-lexbind e1a95cc 04/20: Improve paragraph filling to match newer Emacs versions
Date: Wed, 14 Aug 2019 04:30:21 -0400 (EDT)

branch: scratch/hyperbole-lexbind
commit e1a95cc9820cb9d5aaca947b3ddd836a055d3c60
Author: Bob Weiner <address@hidden>
Commit: Bob Weiner <address@hidden>

    Improve paragraph filling to match newer Emacs versions
---
 kotl/kfill.el     | 97 +++++++++++++++++++++++++------------------------------
 kotl/kotl-mode.el | 11 ++++---
 2 files changed, 51 insertions(+), 57 deletions(-)

diff --git a/kotl/kfill.el b/kotl/kfill.el
index 0ceadb2..60ee307 100644
--- a/kotl/kfill.el
+++ b/kotl/kfill.el
@@ -29,12 +29,6 @@
 ;;; Public variables
 ;;; ************************************************************************
 
-(defvar kfill:function-table
-  (if (featurep 'filladapt)
-      filladapt-function-table
-    (list (cons 'fill-paragraph (symbol-function 'fill-paragraph))))
-  "Table containing the old function definitions that kfill overrides.")
-
 (defvar kfill:prefix-table
   '(
     ;; Lists with hanging indents, e.g.
@@ -117,44 +111,44 @@ number of lines that could not be moved, otherwise 0."
              ;; Need this or Emacs ignores fill-prefix when inside a
              ;; comment.
              (comment-multi-line t)
+             (fill-paragraph-handle-comment t)
              fill-prefix)
          (kfill:adapt nil)
          (do-auto-fill))
       (do-auto-fill))))
 
-;;; Redefine this built-in function.
-
-(defun fill-paragraph (arg &optional skip-prefix-remove)
-  "Fill paragraph at or after point.  Prefix ARG means justify as well."
-  (interactive "*P")
-  (if (not (eq major-mode 'kotl-mode))
-      (kfill:funcall 'fill-paragraph arg)
-    ;; 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)
-                 (paragraph-start paragraph-start)
-                 (paragraph-separate paragraph-separate)
-                 fill-prefix)
-             (if (kfill:adapt t)
-                 (throw 'done (kfill:funcall '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)))))
+(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.
@@ -165,16 +159,17 @@ 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
-       fill-prefix (if turn-off
-                       nil
-                     (buffer-substring
-                      (save-excursion (beginning-of-line) (point))
-                      (point))))
-  (if (equal prior-fill-prefix "")
-      (setq prior-fill-prefix nil))
-  (if (equal fill-prefix "")
-      (setq fill-prefix nil))
+  (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 "")))
@@ -237,10 +232,6 @@ fill prefix at the beginning of each line."
                 (funcall function justify-flag)))
          (fill-region-as-paragraph from (point) justify-flag)))))
 
-(defun kfill:funcall (function &rest args)
-  "Call the original FUNCTION with rest of ARGS that kfill overloaded."
-  (apply (cdr (assq function kfill:function-table)) args))
-
 (defun kfill:hanging-list (paragraph)
   (let (prefix match beg end)
     (setq prefix (make-string (- (match-end 0) (match-beginning 0)) ?\ ))
diff --git a/kotl/kotl-mode.el b/kotl/kotl-mode.el
index 7deefb5..ce3e570 100644
--- a/kotl/kotl-mode.el
+++ b/kotl/kotl-mode.el
@@ -79,6 +79,7 @@ It provides the following keys:
       ;; Some package such as filladapt has overwritten the primitives
       ;; defined in kfill.el, so reload it.
       (load "kfill"))
+  (setq fill-paragraph-function #'kfill:fill-paragraph)
   ;; Ensure that outline structure data is saved when save-buffer is called
   ;; from save-some-buffers, {C-x s}.
   (add-hook 'local-write-file-hooks #'kotl-mode:update-buffer)
@@ -153,7 +154,7 @@ It provides the following keys:
       (kvspec:activate))))
   ;; We have been converting a buffer from a foreign format to a koutline.
   ;; Now that it is converted, ensure that `kotl-previous-mode' is set to
-  ;; koutline now.
+  ;; koutline.
   (setq kotl-previous-mode 'kotl-mode)
   (run-hooks 'kotl-mode-hook)
   (add-hook 'change-major-mode-hook #'kotl-mode:show-all nil t))
@@ -675,15 +676,17 @@ too long."
 With arg N, insert N newlines."
   (interactive "*p")
   (let* ((bolp (and (kotl-mode:bolp) (not (kotl-mode:bocp))))
-        (indent (kcell-view:indent)))
+        (indent (kcell-view:indent))
+        (add-prefix (and (stringp fill-prefix)
+                         (not (string-empty-p fill-prefix)))))
     (while (> arg 0)
       (save-excursion
         (insert ?\n)
-       (if (and (not bolp) fill-prefix)
+       (if (and (not bolp) add-prefix)
            (insert fill-prefix)
          (insert-char ?\  indent)))
       (setq arg (1- arg)))
-    (if (and bolp fill-prefix)
+    (if (and bolp add-prefix)
        (progn (delete-horizontal-space)
               (insert fill-prefix)))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]