]> git.eshelyaron.com Git - dict.git/commitdiff
ADDED: user option specifying where to define new predicates V8.5.20-sweep-0.8.6
authorEshel Yaron <me@eshelyaron.com>
Fri, 11 Nov 2022 10:39:58 +0000 (12:39 +0200)
committerEshel Yaron <me@eshelyaron.com>
Fri, 11 Nov 2022 10:39:58 +0000 (12:39 +0200)
* sweeprolog.el (sweeprolog-default-new-predicate-location)
(sweeprolog-new-predicate-location-above-current): new functions.
(sweeprolog-new-predicate-location-function): new user option.
(sweeprolog-maybe-define-predicate): use it.

NEWS.org
README.org
sweeprolog-tests.el
sweeprolog.el

index 8a30a5f380905641d1113a8bafe2bc5c3f18563b..23602eb6f98e1a938d2ea62b54543aad18e6a299 100644 (file)
--- a/NEWS.org
+++ b/NEWS.org
@@ -11,6 +11,24 @@ SWI-Prolog in Emacs.
 For further details, please consult the manual:
 <https://eshelyaron.com/sweep.html>.
 
+* Version 0.8.6 on 2022-11-11
+
+** New user option ~sweeprolog-new-predicate-location-function~
+
+This user option specifies a function to be called from
+~sweeprolog-insert-term-dwim~ when defining a new predicate to choose
+the location of the new predicate definition.  The default value of
+the option is a function ~sweeprolog-default-new-predicate-location~
+which preserves the current behavior of placing the new predicate
+right below the current predicate.  Other options include the new
+function ~sweeprolog-new-predicate-location-above-current~ which places
+the new predicate above the current one.
+
+** Fixes
+
+- Fixed issue where ~sweeprolog-describe-predicate~ would throw an error
+  when describing predicates that were cross referenced but not loaded.
+
 * Version 0.8.5 on 2022-11-10
 
 ** New command ~sweeprolog-xref-project-source-files~
@@ -19,7 +37,7 @@ This command updates ~sweep~'s cross reference data for all Prolog
 source files in the current project.  Bound to ~X~ in
 ~sweeprolog-prefix-map~.
 
-** Minor bug fixed
+** Minor bug fixes
 
 - Fixed issue where ~sweeprolog-predicate-location~ sometimes returned a
   file importing the predicate in question, rather than actually
index 1220b1b3cf91f1a285e8f01bc495917181a74268..1d09df0b24be1479be050489ee5a0e392745f92f 100644 (file)
@@ -984,12 +984,16 @@ functions, in order:
 
 #+FINDEX: sweeprolog-maybe-insert-next-clause
 #+FINDEX: sweeprolog-maybe-define-predicate
+#+VINDEX: sweeprolog-new-predicate-location-function
 - ~sweeprolog-maybe-insert-next-clause~ :: If the last token before
   point is a fullstop ending a predicate clause, insert a new clause
   below it.
 - ~sweeprolog-maybe-define-predicate~ :: If point is over a call to an
-  undefined predicate, insert a definition for that predicate below
-  the last clause of the current predicate definition.
+  undefined predicate, insert a definition for that predicate.  By
+  default, the new predicate definition is inserted right below the
+  last clause of the current predicate definition.  The user option
+  ~sweeprolog-new-predicate-location-function~ can be customized to
+  control where this function inserts new predicate definitions.
 
 *** Filling Holes
 :PROPERTIES:
index 8dbe50000af0887de684f5f64222b4a232746032..2f3e5a5500e49190a2fdf7c0f6b43ffe1b87fee2 100644 (file)
@@ -294,6 +294,31 @@ foo :- bar.
 bar :- foo.
 "))))
 
+
+(ert-deftest dwim-define-predicate-above ()
+  "Tests adherence to `sweeprolog-new-predicate-location-function'."
+  (with-temp-buffer
+    (sweeprolog-mode)
+    (insert "
+%!  foo is det.
+
+foo :- bar.
+")
+    (backward-word)
+    (let ((sweeprolog-new-predicate-location-function
+           #'sweeprolog-new-predicate-location-above-current))
+      (sweeprolog-insert-term-dwim))
+    (call-interactively #'kill-region)
+    (insert "foo")
+    (should (string= (buffer-string)
+                     "
+bar :- foo.
+
+%!  foo is det.
+
+foo :- bar.
+"))))
+
 (ert-deftest end-of-top-term-with-univ ()
   "Tests detecting the fullstop in presence of `=..'."
   (with-temp-buffer
index 0aa088a44eef789e6df2673106e8d06097a45f74..52ed33471efa2c6a4d1967b203b52504e4eb103f 100644 (file)
@@ -6,7 +6,7 @@
 ;; Maintainer: Eshel Yaron <~eshel/dev@lists.sr.ht>
 ;; Keywords: prolog languages extensions
 ;; URL: https://git.sr.ht/~eshel/sweep
-;; Package-Version: 0.8.5
+;; Package-Version: 0.8.6
 ;; Package-Requires: ((emacs "28.1"))
 
 ;; This file is NOT part of GNU Emacs.
@@ -309,6 +309,21 @@ clause."
   :type 'boolean
   :group 'sweeprolog)
 
+(defcustom sweeprolog-new-predicate-location-function
+  #'sweeprolog-default-new-predicate-location
+  "Function used to choose a location for a new predicate definition.
+It should take one argument, the name of the new predicate given
+as a string, and move point to a suitable position in the current
+buffer where the new predicate defintion should be inserted."
+  :package-version '((sweeprolog "0.8.6"))
+  :type '(choice (const    :tag "Below Current Predicate"
+                        sweeprolog-default-new-predicate-location)
+                 (const    :tag "Above Current Predicate"
+                        sweeprolog-new-predicate-location-above-current)
+                 (function :tag "Custom Function"))
+  :group 'sweeprolog)
+
+
 
 ;;;; Keymaps
 
@@ -2423,11 +2438,19 @@ instead."
     (sweeprolog-insert-clause functor arity)
     t))
 
+(defun sweeprolog-default-new-predicate-location (_pred)
+  (sweeprolog-end-of-predicate-at-point))
+
+(defun sweeprolog-new-predicate-location-above-current (_pred)
+  (sweeprolog-beginning-of-predicate-at-point)
+  (let ((last (or (caddr (sweeprolog-last-token-boundaries))
+                  (point-min))))
+    (while (re-search-backward (rx bol "%" (or "%" "!")) last t))))
+
 (defun sweeprolog-maybe-define-predicate (point _kind _beg _end)
   (when-let ((pred (sweeprolog-identifier-at-point point)))
     (unless (sweeprolog-predicate-properties pred)
-      (push-mark)
-      (sweeprolog-end-of-predicate-at-point)
+      (funcall sweeprolog-new-predicate-location-function pred)
       (let ((functor-arity (sweeprolog--mfn-to-functor-arity pred)))
         (sweeprolog-insert-clause (car functor-arity)
                                   (cdr functor-arity)))