Eshel Yaron [Mon, 27 Nov 2023 07:16:47 +0000 (08:16 +0100)]
Improve hole highlighting in the Sweep top-level
* sweeprolog.el (sweeprolog-analyze-end-font-lock): Extract hole
highlighting code to...
(sweeprolog--highlight-holes): New function.
(sweeprolog-colourise-query): Use it.
(sweeprolog-fill-holes): New command.
(sweeprolog--fill-query-holes): New function.
(sweeprolog-top-level-mode): Use it to fill holes in query when
posting it.
Eshel Yaron [Sun, 26 Nov 2023 10:47:15 +0000 (11:47 +0100)]
Use a few more hook functions also in top-level buffers
* sweeprolog.el (sweeprolog-top-level-mode): Set
'forward-sexp-function', 'beginning-of-defun-function',
'end-of-defun-function' and 'syntax-propertize-function' hooks to
their Prolog-specific value that we already use in 'sweeprolog-mode'.
Eshel Yaron [Sun, 26 Nov 2023 10:33:57 +0000 (11:33 +0100)]
Respect 'syntax-table' text properties
Replace all usages of (char-syntax (char-before/after ...)) with
'syntax-after' calls, since the former doesn't take into account the
'syntax-table' text property that 'syntax-propertize-function' adds.
In particular, this fixes an issue with 'forward-sexp' over character
literals such as "0'c".
* sweeprolog.el (sweeprolog-syntax-propertize): Replace with...
(sweeprolog-syntax-propertize-function): New 'defconst'.
(sweeprolog-mode): Use it.
(sweeprolog--op-p, sweeprolog-syntax-class-at): New 'defsubst'.
(sweeprolog--parse-context, sweeprolog--completion-at-point)
(sweeprolog-next-token-boundaries, sweeprolog-last-token-boundaries)
(sweeprolog--forward-term, sweeprolog--backward-term)
(sweeprolog--precedence-at-point,sweeprolog-align-spaces)
(sweeprolog-indent-line): Respect 'syntax-table' text properties.
* sweeprolog-tests.el (backward-sexp-over-char-literal): Test it.
Eshel Yaron [Thu, 23 Nov 2023 10:44:23 +0000 (11:44 +0100)]
Add a few missing CHR-specific faces and help messages
* sweeprolog.el (sweeprolog-deprecated):
(sweeprolog-delimiter):
(sweeprolog-pragma):
(sweeprolog-chr-type): New faces.
(sweeprolog-analyze-fragment-to-faces): Use them.
(sweeprolog-analyze-fragment-help-echo): Add 'help-echo' text for
CHR-specific term kinds.
(sweeprolog--help-echo-for-goal-functor): Shorten messages.
Eshel Yaron [Tue, 14 Nov 2023 08:40:21 +0000 (09:40 +0100)]
Optimize predicate completion at point
* sweep.pl (sweep_heads_collection/2): Optimize for speed. Also
report functor length and arity for each candidate to facilitate
faster sorting.
* sweeprolog.el (sweeprolog-init-args): Add '-O'.
(sweeprolog-predicate-completion-candidates): Improve sorting.
Eshel Yaron [Thu, 9 Nov 2023 19:47:57 +0000 (20:47 +0100)]
Handle SIGHUP in daemon mode when killing a tty top-level
When Emacs is running as a daemon, and deletes the process of a
top-level that uses a tty, the top-level receives SIGHUP. But since
the top-level is a thread is the same process as Emacs itself, this
results in 'kill-emacs'. To prevent this unfortunate outcome, set a
no-op handler for SIGHUP before deleting the top-level process and
restore the original handler afterwards.
* sweep.pl (sweep_nohup/2): New predicate.
* sweeprolog.el (sweeprolog-top-level-filter): New function.
(sweeprolog-top-level-buffer): Use it.
Eshel Yaron [Sun, 22 Oct 2023 19:50:02 +0000 (21:50 +0200)]
ADDED: in-buffer completions for arithmetic functions
* sweep.pl (sweep_function_functors_collection/2)
(sweep_functions_collection/2): New predicates.
(sweep_context_callable_arg/4): Recognize arithmetic functions.
* sweeprolog.el (sweeprolog-arith-functor-completion-candidates)
(sweeprolog-arith-completion-candidates): New functions, used in...
(sweeprolog--atom-or-functor-completion-at-point): ...here.
* sweep.texi (Code Completion): Document arithmetic function completion.
* sweeprolog-tests.el: Test it.
Eshel Yaron [Sun, 22 Oct 2023 16:17:23 +0000 (18:17 +0200)]
ADDED: in-buffer completions for source file specs
* sweep.pl (sweep_alias_source_file_name_collection/2)
(sweep_alias_source_file_name/4)
(sweep_source_file_name_collection/2)
(sweep_source_file_name/3)
(sweep_path_alias_collection/2)
(sweep_matching_path_alias/3): New predicates.
(sweep_context_callable_/3): Simplify, and recognize predicate
arguments that should be source file specifications.
* sweeprolog.el (sweeprolog-source-functor-completion-candidates)
(sweeprolog-alias-source-completion-candidates)
(sweeprolog-source-completion-candidates): New functions, used in...
(sweeprolog--atom-or-functor-completion-at-point): ...here.
(sweeprolog-electric-layout-post-self-insert-function)
(sweeprolog-context-menu-for-region)
(sweeprolog-maybe-extract-region-to-predicate): Adapt.
* sweep.texi (Code Completion): Document file spec completion.
* sweeprolog-tests.el: Test it.
Eshel Yaron [Fri, 20 Oct 2023 18:14:01 +0000 (20:14 +0200)]
Expand Top-level documentation
* sweep.texi (Top-level Interaction): New section, replaces...
(Top-level Completion): Remove.
(The Prolog Top-level): Update menu and document ANSI
escape sequence handling.
Eshel Yaron [Fri, 20 Oct 2023 15:58:47 +0000 (17:58 +0200)]
Emit highlighted messages in the top-level
* sweep.pl: Ensure 'library(ansi_term)' gets loaded.
(sweep_top_level_client/4): Set the flag 'verbose' to 'normal' in
order to enable informational messages, and set 'color_term' to 'true'
to enable highlighting when using a pty.
Eshel Yaron [Fri, 20 Oct 2023 14:51:15 +0000 (17:51 +0300)]
Set Prolog flag 'tty_control' to 'true' when using pty
When 'sweeprolog-top-level-use-pty' is non-nil, let the Prolog
top-level thread know that it controls the tty. This affects some
aspects of the top-level interaction, namely in the presence of choice
points. A significant benefit is that Prolog ensures that the tty
does not echo input, so we can stop setting 'comint-process-echoes'
which had the adverse effect of blocking Emacs when output stalls on
non-echoing terminals.
* sweep.pl (sweep_top_level_client/3): Become
'sweep_top_level_client/4'. New argument says whether the top-level
controls the tty.
(sweep_top_level_pty_client/1,sweep_top_level_server_loop_/2): Adjust.
* sweeprolog.el (sweeprolog-top-level-buffer): Stop setting
'comint-process-echoes'.
(sweeprolog-top-level--post-self-insert-function): When using a tty,
send single character responses directly and assume that the top-level
echoes them back.
Teach 'sweeprolog-extract-region-to-predicate' about existentially
quantified goals and lambda terms.
* sweep.pl (sweep_term_variable_names/2): Remove from export list.
* sweep.pl (sweep_extract_goal/2): New public predicate.
* sweeprolog.el (sweeprolog-extract-region-to-predicate): Use it.
* sweeprolog-tests.el: Test it.
* sweep.texi (Extract Goal): Update documentation.
* sweeprolog.el (sweeprolog-query-replace-term-prompt-old)
(sweeprolog-query-replace-term-prompt-new): New faces.
(sweeprolog-query-replace-term): Use them instead of 'diff-mode'
faces.
; Fix filtering of the just created body when extracting goal
* sweeprolog (sweeprolog-extract-region-to-predicate): When searching
and replacing other goals subsumed by the extracted goal, use a more
robust check to filter out the newly created body term from the search
matches.
Extract subsumed goals when extracting goal to predicate
Extend 'sweeprolog-extract-region-to-predicate' with the ability to
replace other goals in the current buffer that are subsumed by the
extracted goal with calls to the predicate this command creates.
'sweeprolog-extract-region-to-predicate' now does this when it is
called with a prefix argument. Also make
'sweeprolog-insert-term-dwim' propagate the prefix argument to
functions in 'sweeprolog-insert-term-functions'.
* sweeprolog.el (sweeprolog-insert-term-dwim): Add ARG argument and
pass it to functions listed...
(sweeprolog-insert-term-functions): ...here. Adjust docstring.
(sweeprolog-maybe-insert-next-clause)
(sweeprolog-maybe-define-predicate)
(sweeprolog-maybe-extract-region-to-predicate): Adjust accordingly.
(sweeprolog-default-new-predicate-location): Go to end of line in case
there's a comment on the last line of the current predicate
definition.
(sweeprolog-query-replace-term-include-match-function): New variable.
(sweeprolog-query-replace-term): Use it.
(sweeprolog-extract-region-to-predicate): Add ALL argument. When
non-nil, suggest replacing all goals in the buffer that are subsumbed
by the extracted goal with calls to the newly defined predicate.
* sweep.texi (Top, Editing Prolog Code): Mention "Term Replace" in
menus.
(Main Features, Architecture, Alternatives): Mark as sections rather
than chapters.
ADDED: new command 'sweeprolog-query-replace-term'
This patch extends the Term Search infrastructure to support
search-and-replace operations. Namely, we replace the predicate
'sweep_term_search/2' with a new predicate 'sweep_term_replace/2' that
subsumes the functionality of the former. The command
'sweeprolog-term-search' is greatly simplified and adjusted to work
with this new predicate, and we introduce a new command,
'sweeprolog-query-replace-term', that exposes the new
search-and-replace facility of 'sweep_term_replace/2' in an
interactive UI inspired by 'query-replace'.
* sweep.pl (sweep_term_search/2): Remove in favor of...
(sweep_term_replace/2): New predicate.
* sweeprolog.el: (sweeprolog-term-replace-edits): New function.
(sweeprolog-term-search): Rewrite.
(sweeprolog-term-search-last-search)
(sweeprolog-term-search-overlays)
(sweeprolog-term-search-repeat-count)
(sweeprolog-term-search-repeat-backward)
(sweeprolog-term-search-repeat-forward)
(sweeprolog-term-search-abort)
(sweeprolog-term-search-in-buffer)
(sweeprolog-term-search-next)
(sweeprolog-term-search-map): Remove, unused.
(sweeprolog-query-replace-term): New command.
(sweeprolog-mode-map): Bind it.
* sweeprolog-tests.el (term-search)
* sweep.texi (Term Search): Adjust.
(Term Replace): New section.
ENHANCED: Support pty top-level communication instead of TCP
Add the ability for top-level buffers to communicate with their
backing threads via pty instead of a local TCP connection. This is
controlled by a new user option, enabled by default on systems where
Emacs can creates a pty.
* sweep.pl (sweep_top_level_start_pty/2): New predicate.
* sweeprolog.el (sweeprolog-top-level-use-pty): New user option.
(sweeprolog-top-level-buffer): Use it.
* sweep.texi (The Prolog Top-level): Document it.
Only extract goals to separate predicates, not data terms
* sweeprolog.el (sweeprolog-context-menu-for-region)
(sweeprolog-maybe-extract-region-to-predicate): Check that the region
starts in a callable position.
(sweeprolog-extract-region-to-predicate): Reanalyze the buffer as a
last step.
* sweep.texi: Adjust.
Only enable right-click actions on region when click is in region
* sweeprolog.el (sweeprolog-context-menu-region-beg-at-click)
(sweeprolog-context-menu-region-end-at-click): New variables.
(sweeprolog-context-menu-function): Let-bind them.
(sweeprolog-context-menu-for-region): Use them to reliably check that
click is actually inside the selected region.
ADDED: New command 'sweeprolog-extract-region-to-predicate'
Add a command for extracting a part of a clause body into a separate
predicate.
* sweep.pl (sweep_term_variable_names/2)
(sweep_goal_may_cut/2): New helper predicates.
* sweeprolog.el (sweeprolog-extract-region-to-predicate): New command.
(sweeprolog-maybe-extract-region-to-predicate): New function.
(sweeprolog-insert-term-functions): Add it.
* sweep.texi (Insert Term DWIM, Extract Goal): Document it.