]> git.eshelyaron.com Git - dict.git/commitdiff
ADDED: sweep-file-name-handler
authorEshel Yaron <me@eshelyaron.com>
Sat, 3 Sep 2022 12:44:35 +0000 (15:44 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sat, 3 Sep 2022 15:42:35 +0000 (18:42 +0300)
README.org
sweep.el
sweep.pl

index 87f0ee34ee2e8129808d587365e8808a5d237496..44c7f51a288b37374821075393225e25f28b0e20 100644 (file)
@@ -325,6 +325,17 @@ Along with =M-x sweep-find-module=, =sweep= provides the
 command =M-x sweep-find-predicate= jumping to the definition a
 loaded or auto-loadable Prolog predicate.
 
+** Prolog file specification expansion
+
+=sweep= defines a handler for file name the Emacs function
+=expand-file-file= that recognizes Prolog file specifications, such as
+=library(lists)=, and expands them to the corresponding absolute paths.
+This means that one can use Prolog file specifications with Emacs'
+standard =find-file= (=C-x C-f=) to locate Prolog resources directly.
+
+For example, typing =C-x C-f library(pldoc/doc_man)= will open the
+source of the =pldoc_man= module from the Prolog library.
+
 * Installing Prolog packages
 :PROPERTIES:
 :CUSTOM_ID: prolog-packages
@@ -354,6 +365,8 @@ to a prefix key, e.g. =C-c p=, use:
 As an example, with the above binding the =sweep= top-level can be
 access from anywhere with =C-c p t=.
 
+#+html: <!--
+
 * Indices
 :PROPERTIES:
 :CUSTOM_ID: indices
@@ -376,3 +389,5 @@ access from anywhere with =C-c p t=.
 :INDEX: cp
 :CUSTOM_ID: cindex
 :END:
+
+#+html: -->
index bd1d33e34affd8ec3d3e28bcdbe4d134173723b7..42c00a0f7509544281fc56ef8e9d01a4f5d11c7c 100644 (file)
--- a/sweep.el
+++ b/sweep.el
@@ -672,6 +672,53 @@ Interactively, a prefix arg means to prompt for BUFFER."
     map)
   "Keymap for `sweep' global commands.")
 
+;;;###autoload
+(defun sweep-file-name-handler (operation &rest args)
+  (cond ((eq operation 'expand-file-name)
+         (let ((fn (car  args))
+               (dn (cadr args)))
+           (sweep-open-query "user"
+                             "sweep"
+                             "sweep_expand_file_name"
+                             (cons fn dn))
+           (let ((sol (sweep-next-solution)))
+             (sweep-close-query)
+             (if (sweep-true-p sol)
+                 (cdr sol)
+               (let ((inhibit-file-name-handlers
+                      (cons 'sweep-file-name-handler
+                            (and (eq inhibit-file-name-operation operation)
+                                 inhibit-file-name-handlers)))
+                     (inhibit-file-name-operation operation))
+                 (apply operation args))))))
+        (t (let ((inhibit-file-name-handlers
+                  (cons 'sweep-file-name-handler
+                        (and (eq inhibit-file-name-operation operation)
+                             inhibit-file-name-handlers)))
+                 (inhibit-file-name-operation operation))
+             (apply operation args)))))
+
+(add-to-list 'file-name-handler-alist
+             (cons (rx (seq bol (one-or-more lower) "("))
+                   #'sweep-file-name-handler))
+
+(defun sweep-beginning-of-top-term ()
+  (unless (bobp)
+    (when-let ((safe-start (nth 8 (syntax-ppss))))
+      (goto-char safe-start))
+    (re-search-backward (rx (seq bol graph)) nil t)
+    (let ((safe-start (nth 8 (syntax-ppss))))
+      (while (and safe-start (not (bobp)))
+        (goto-char safe-start)
+        (re-search-backward (rx (seq bol graph)) nil t)
+        (setq safe-start (nth 8 (syntax-ppss)))))))
+
+(defun sweep-end-of-top-term ()
+  (unless (eobp)
+    (while (nth 8 (syntax-ppss))
+      (forward-char))
+    (or (re-search-forward (rx (seq "." (or white "\n"))) nil t)
+        (goto-char (point-max)))))
 ;;;; Testing:
 
 ;; (add-to-list 'load-path (file-name-directory (buffer-file-name)))
index d0461cf3d287311f4eb9840d883c405ef495b415..9f9987b7973b4c4eebae549e4494b53538e041fd 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -33,6 +33,7 @@
 :- module(sweep,
           [ sweep_colors/2,
             sweep_documentation/2,
+            sweep_expand_file_name/2,
             sweep_predicate_location/2,
             sweep_predicates_collection/2,
             sweep_modules_collection/2,
@@ -346,3 +347,24 @@ sweep_color_goal(goal).
 sweep_color_goal(goal_term).
 sweep_color_goal(head).
 sweep_color_goal(head_term).
+
+
+sweep_expand_file_name([String|Dir], Exp) :-
+    term_string(Spec, String, [syntax_errors(quiet)]),
+    sweep_expand_file_name_(Dir, Spec, Atom),
+    (   exists_file(Atom)
+    ->  true
+    ;   exists_directory(Atom)
+    ),
+    atom_string(Atom, Exp).
+
+sweep_expand_file_name_([], Spec, Atom) :-
+    absolute_file_name(Spec, Atom, [file_errors(fail),
+                                    solutions(all),
+                                    extensions(['', '.pl'])]).
+sweep_expand_file_name_(Dir, Spec, Exp) :-
+    !,
+    absolute_file_name(Spec, Exp, [file_errors(fail),
+                                   relative_to(Dir),
+                                   solutions(all),
+                                   extensions(['', '.pl'])]).