spacing(next_argument),
numbervars(true)]).
-sweep_context_callable([], true) :- !.
-sweep_context_callable([[":"|2]], true) :- !.
sweep_context_callable([H|T], R) :-
+ H = [F0|_],
+ atom_string(F, F0),
+ ( xref_op(_, op(1200, _, F))
+ -> true
+ ; current_op(1200, _, F)
+ ),
+ !,
+ sweep_context_callable_(T, R).
+sweep_context_callable([_|T], R) :-
+ sweep_context_callable(T, R).
+
+sweep_context_callable_([], true) :- !.
+sweep_context_callable_([[":"|2]], true) :- !.
+sweep_context_callable_([["("|_]|T], R) :-
+ sweep_context_callable_(T, R).
+sweep_context_callable_([H|T], R) :-
H = [F0|N],
atom_string(F, F0),
- ( sweep_context_callable_(F, N)
- -> sweep_context_callable(T, R)
+ ( sweep_context_callable_arg(F, N)
+ -> sweep_context_callable_(T, R)
; R = []
).
-sweep_context_callable_(Neck, _) :-
+sweep_context_callable_arg(Neck, _) :-
( xref_op(_, op(1200, _, Neck))
-> true
; current_op(1200, _, Neck)
).
-sweep_context_callable_(F, N) :-
+sweep_context_callable_arg(F, N) :-
( current_predicate(F/M), pi_head(F/M,Head)
; xref_defined(_, Head, _), pi_head(F/M,Head)
),
(ppre (sweeprolog-op-prefix-precedence op)))
(cond
((and (string= "." op)
- (member (char-syntax (char-after (1+ obeg))) '(?> ? )))
+ (or (not (char-after (1+ obeg)))
+ (member (char-syntax (char-after (1+ obeg)))
+ '(?> ? ))))
nil)
((string= "," op)
(setq pos
(setq commas 0)))))))
context)))
+(defun sweeprolog-context-callable-p ()
+ (sweeprolog--query-once "sweep" "sweep_context_callable"
+ (sweeprolog--parse-context)))
+
(defun sweeprolog-predicate-completion-at-point ()
"Prolog predicate completion backend for `completion-at-point'."
(when-let ((bounds (bounds-of-thing-at-point 'symbol))
(let ((first (char-after beg)))
(not (or (sweeprolog--char-uppercase-p first)
(= first ?_))))
- (sweeprolog--query-once "sweep"
- "sweep_context_callable"
- (sweeprolog--parse-context)))
+ (sweeprolog-context-callable-p))
(when-let
((col (sweeprolog--query-once "sweep" "sweep_predicate_completion_candidates"
nil)))
(setq cur (point)))
(skip-chars-forward " \t\n")
(push (list cur (point) nil) ws)
- (cons (list beg end nil)
- (cons (list beg end (sweeprolog-fullstop-face))
+ (cons (list beg (point) nil)
+ (cons (list beg end (sweeprolog-fullstop-face))
ws)))))
("functor"
(list (list beg end (sweeprolog-functor-face))))
oend)))
(`(operator ,obeg ,oend)
(if (and (string= "." (buffer-substring-no-properties obeg oend))
- (member (char-syntax (char-after (1+ obeg))) '(?> ? )))
+ (or (not (char-after (1+ obeg)))
+ (member (char-syntax (char-after (1+ obeg)))
+ '(?> ? ))))
(signal 'scan-error
(list "Cannot scan backwards beyond fullstop."
obeg
(delete-horizontal-space)
(insert (make-string num ? ))))))))))
+(defun sweeprolog-electric-layout-post-self-insert-function ()
+ (if (nth 8 (syntax-ppss))
+ (when (member (buffer-substring-no-properties (line-beginning-position)
+ (point))
+ '("%%" "%!"))
+ (insert " "))
+ (when (member (char-before) (string-to-list "(;>"))
+ (pcase (sweeprolog-last-token-boundaries)
+ ((or `(open ,beg ,end)
+ `(operator ,beg ,end))
+ (when (and (member (buffer-substring-no-properties beg end)
+ '("(" ";" "->" "*->"))
+ (sweeprolog-context-callable-p))
+ (insert (make-string (+ 4 beg (- end)) ? ))))))))
+
+;;;###autoload
+(define-minor-mode sweeprolog-electric-layout-mode
+ "Automatically insert whitespace in `sweeprolog-mode' buffers."
+ :group 'sweeprolog
+ (if sweeprolog-electric-layout-mode
+ (progn
+ (add-hook 'post-self-insert-hook
+ #'sweeprolog-electric-layout-post-self-insert-function
+ nil t))
+ (remove-hook 'post-self-insert-hook
+ #'sweeprolog-electric-layout-post-self-insert-function
+ t)))
+
(defun sweeprolog--update-buffer-last-modified-time (&rest _)
(setq sweeprolog--buffer-last-modified-time (float-time)
sweeprolog--buffer-modified t))