Refactor and enhance 'completion-at-point' support
Replace 'sweeprolog-completion-at-point-functions' with a single
function 'sweeprolog-completion-at-point' that implements a much more
refined context-sensitive decision tree to determine what kind of
completion to perform.
Eshel Yaron [Tue, 27 Jun 2023 18:36:45 +0000 (21:36 +0300)]
Add grouping and affixation functions for module completion
This adds a 'group-function' and an 'affixation-function' to
'sweeprolog-module-completion-table', for Emacs 28+
The affixation API allows us to compute the appropriate padding to
insert before annotations in a cleaner way. Modules are group
according to their classes.
* sweep.pl (sweep_module_class/2): New predicate.
* sweeprolog.el (sweeprolog-module-minibuffer-annotation-1):
Extract helper function from...
(sweeprolog-module-minibuffer-annotation): ...here. Adapt.
(sweeprolog-module-minibuffer-affixation)
(sweeprolog-module-minibuffer-group): New functions.
(sweeprolog-module-completion-table): Use them.
* sweeprolog.el (sweeprolog-module-completion-table)
(sweeprolog-module-p, sweeprolog-module-minibuffer-annotation)
(sweeprolog-module-annotation): New functions.
(sweeprolog-modules-collection)
(sweeprolog-read-module-name): Adapt.
(sweeprolog-find-module): Improve feedback for modules that are not
backed by files.
Eshel Yaron [Fri, 23 Jun 2023 08:59:18 +0000 (11:59 +0300)]
MODIFIED: Replace "faces styles" with a theme emulating PceEmacs
This adds a custom theme called 'sweeprolog-pce' that mimics the
highlighting of SWI-Prolog's built-in editor, PceEmacs. It obsoletes
the "faces styles" feature that Sweep provided thus far. For backward
compatibility, The user option 'sweeprolog-faces-style' is retained
and mostly still respected.
Thanks to Stefan Monnier for suggesting this approach for simplifying
Sweep's code and making it more conformant with Emacs conventions.
* sweeprolog-pce-theme.el: New file.
* sweeprolog.el: Remove 'sweeprolog-defface' macro its
'sweeprolog-*-face' functions, use plain face definitions and refer to
them directly by their names instead. Also extend
'custom-theme-load-path' to include the new sweeprolog-pce-theme.el.
* README.org (Semantic Highlighting): Replace "Available Styles"
subsection with new subsection "PceEmacs Theme".
Eshel Yaron [Sun, 18 Jun 2023 19:32:33 +0000 (22:32 +0300)]
ADDED: Persistent history for Sweep top-levels
This adds a new user option that controls the value of
'comint-input-ring-file-name' in Sweep top-level buffers.
* sweeprolog.el (sweeprolog-top-level-persistent-history): New user
option.
(sweeprolog-top-level-sentinel)
(sweeprolog-top-level-setup-history): New functions.
(sweeprolog-top-level-setup-buffer): Call
'sweeprolog-top-level-setup-history' when creating a new top-level
buffer.
Eshel Yaron [Wed, 14 Jun 2023 07:37:02 +0000 (10:37 +0300)]
FIXED: Correctly recognize DCG grammar rules in 'C-c C-d'
* sweeprolog.el (sweeprolog-beginning-of-predicate-at-point): Also
return the module and neck operator of the predicate at point.
(sweeprolog-insert-predicate-documentation)
(sweeprolog-read-predicate-documentation-function)
(sweeprolog-read-predicate-documentation-with-holes)
(sweeprolog-read-predicate-documentation-default-function)
(sweeprolog-read-predicate-documentation)
(sweeprolog-document-predicate-at-point): Adapt to support DCGs and
non-local predicates.
Eshel Yaron [Tue, 13 Jun 2023 15:54:50 +0000 (18:54 +0300)]
Simplify 'sweeprolog-beginning-of-top-term'
Extract the core of 'sweeprolog-beginning-of-top-term' to
'sweeprolog-beginning-of-top-term-once' and make it leave point at the
very beginning of the top term, rather than at the following
character.
* sweeprolog.el (sweeprolog-beginning-of-top-term-once): New function.
(sweeprolog-beginning-of-top-term): Use it.
(sweeprolog-local-variables-collection)
(sweeprolog-analyze-term)
(sweeprolog-analyze-some-terms)
(sweeprolog-beginning-of-predicate-at-point)
(sweeprolog-terms-at-point): Adapt.
Eshel Yaron [Sun, 11 Jun 2023 18:05:13 +0000 (21:05 +0300)]
ENHANCED: Guess argument names for DCG grammar rules
* sweeprolog.el (sweeprolog-predicate-completion-at-point)
(sweeprolog-insert-clause): Support guessing argument names from
documentation also for DCG grammar rules.
Allow finding modules and predicates in another window with prefix
argument. Also improve prompt for 'sweeprolog-find-predicate'.
* sweeprolog.el (sweeprolog-read-predicate-prompt): Remove trailing
colon and space.
(sweeprolog-predicate-visible-p-function): New user option.
(sweeprolog-read-predicate): Use it, and format PROMPT with predicate
at point as the default argument.
(sweeprolog-find-module)
(sweeprolog-find-predicate): Add optional argument OTHER-WINDOW.
(sweeprolog-describe-predicate): Adapt PROMPT argument passed to
'sweeprolog-read-predicate'.
Eshel Yaron [Thu, 8 Jun 2023 17:46:45 +0000 (20:46 +0300)]
ENHANCED: Improve DCG support
Take into account DCG grammar rules and ensure they're supported all
around. Crucially, allow 'sweeprolog-describe-predicate' to display
documentation for DCG grammar rules.
Eshel Yaron [Thu, 8 Jun 2023 17:33:55 +0000 (20:33 +0300)]
Give a better name to 'sweeprolog--mfn-to-functor-arity'
* sweeprolog.el (sweeprolog--pi-to-functor-arity): New function,
renamed from 'sweeprolog--mfn-to-functor-arity'.
(sweeprolog-native-predicate-location): Use it.
* sweeprolog.el (sweeprolog-declaration-option): New face, used in...
(sweeprolog-analyze-fragment-to-faces): ...for 'decl_option' frags.
(sweeprolog--help-echo-for-declaration-option): New function.
(sweeprolog-analyze-fragment-help-echo): Use it.
Eshel Yaron [Tue, 30 May 2023 19:29:34 +0000 (22:29 +0300)]
ADDED: command for inserting example usage comments
* sweeprolog.el (sweeprolog-top-level-example-mode): New minor mode.
(sweeprolog-make-example-usage-comment): New command.
(sweeprolog-mode-map): Bind it.
* README.org (Example Usage Comments): New section.
Eshel Yaron [Fri, 26 May 2023 07:56:15 +0000 (10:56 +0300)]
ADDED: New command for expanding macros
* sweep.pl (sweep_expand_macro/2): New predicate.
* sweeprolog.el (sweeprolog-expand-macro-at-pos): New function.
(sweeprolog-expand-macro-at-point): New command.
(sweeprolog-context-menu-functions): Add...
(sweeprolog-context-menu-for-macro): New function.
* README.org (Macro Expansion): New section.
Eshel Yaron [Fri, 26 May 2023 07:18:38 +0000 (10:18 +0300)]
FIXED: tokenizing adjacent operators
Make Sweep's Prolog tokenization functions more careful in their
handling of unrelated adjacent operators. This fixes an issue that
affects term-based movement commands and other commands that rely on
'sweeprolog--forward-sexp' and friends.
* sweeprolog.el (sweeprolog-next-token-boundaries)
(sweeprolog-last-token-boundaries): Fix handling of
adjacent (distinct) operators.
* sweeprolog-tests.el
(forward-sexp-with-adjacent-operators): New test case.
Eshel Yaron [Wed, 24 May 2023 07:11:44 +0000 (10:11 +0300)]
Use 'nil' for some 'defface's instead of ':inherit default'
Change 'defface's that only specify inheritance from 'default' to
empty (nil) definitions, such that the appearance of text these with
faces can be determined by other faces applied to the same text. This
is important, for example, when using Sweep in Org mode source blocks.
Eshel Yaron [Sat, 20 May 2023 14:52:17 +0000 (17:52 +0300)]
ENHANCED: faster completion-at-point for predicates
Filter predicate completion candidates according to the text near
point before formatting and computing argument names. This makes
completion-at-point for predicates much faster when there are lots of
known predicates.
* sweeprolog.el (sweeprolog-predicate-completion-at-point): Pass
(partial) functor at point to...
* sweep.pl (sweep_predicate_completion_candidates/2): Filter
predicates based on new Sub argument.
Eshel Yaron [Thu, 16 Feb 2023 12:34:45 +0000 (14:34 +0200)]
ADDED: mode line indication for loaded buffers
* sweep.pl (sweep_source_file_load_time/2): new predicate.
(sweep_load_buffer/2): also update source modification time based.
* sweeprolog.el (sweeprolog-buffer-load-time)
(sweeprolog-buffer-loaded-since-last-modification-p): new functions.
(sweeprolog-load-buffer): use it.
(sweeprolog-mode): add mode line indication if buffer is loaded.
Eshel Yaron [Mon, 13 Feb 2023 18:35:10 +0000 (20:35 +0200)]
MODIFIED: sweeprolog-update-dependencies choice of directives
* sweeprolog.el (sweeprolog-dependency-directive): New user option.
Determines which directive to use in...
(sweeprolog-update-dependencies): Fix edge cases in finding where to
insert new directives. Optionally infer directive to add based on
current buffer contents.
Eshel Yaron [Tue, 7 Feb 2023 19:57:30 +0000 (21:57 +0200)]
ADDED: new commands for managing numbered variables
* sweeprolog.el (sweeprolog-increment-numbered-variables)
(sweeprolog-decrement-numbered-variables): new commands.
(sweeprolog-mode-map): bind them to C-c C-+ and C-c C--.
(sweeprolog-context-menu-for-variable): allow calling these new
commands from context menus for numbered variables.
* sweeprolog-tests.el: add test for incrementing numbered variables.
* README.org (Numbered Variables): new manual section.
Eshel Yaron [Sat, 4 Feb 2023 21:09:23 +0000 (23:09 +0200)]
ENHANCED: handle renaming variables to existing variable names
* sweeprolog.el (sweeprolog-rename-variable-allow-existing): new user
option, used by...
(sweeprolog-read-new-variable-try): new command, exists the minibuffer
if it contains a valid variable name. It checks if the selected
variable name already exists. Used by...
(sweeprolog-read-new-variable): new function, reads a Prolog variable
name in the minibuffer with a dedicated exit command.
(sweeprolog-rename-variable): use it.
Eshel Yaron [Wed, 1 Feb 2023 18:09:39 +0000 (20:09 +0200)]
Avoid moving cursor while renaming variables from context menu
* sweeprolog.el (sweeprolog-analyze-term-at-point): new optional
argument POINT.
(sweeprolog-rename-variable): use it instead of moving point during
prompting to avoid the cursor being displayed at the click position
when called from the context menu.
Eshel Yaron [Wed, 1 Feb 2023 16:28:09 +0000 (18:28 +0200)]
ENHANCED: provide exact buffer positions for cross references
* sweep.pl (reference_span/5): new predicate.
(sweep_predicate_references/2): use it to find exact character offset of
predicate references.
* sweeprolog.el (xref-backend-references): adapt accordingly.
Eshel Yaron [Sun, 29 Jan 2023 21:35:33 +0000 (23:35 +0200)]
ADDED: command for renaming a variable in Prolog term
* sweeprolog.el (sweeprolog-rename-variable): new command.
(sweeprolog-context-menu-rename-variable): new command used in context
menus for variables.
(sweeprolog-context-menu-for-variable): new function, used in...
(sweeprolog-context-menu-functions): add it to the list.
(sweeprolog-mode-map): bind it to C-c C-r.
* README.org (Renaming Variables): new section.
Eshel Yaron [Sat, 28 Jan 2023 16:38:00 +0000 (18:38 +0200)]
ENHANCED: use docs from the SWI-Prolog manual to guess hole names
* sweep.pl (predicate_argument_names/2): also consult the SWI-Prolog
manual via pldoc_man:load_man_object/4 to find argument names for
built-in predicates.
Eshel Yaron [Thu, 26 Jan 2023 15:21:09 +0000 (17:21 +0200)]
ADDED: numeric argument for sweeprolog-forward/backward-hole
* sweeprolog.el (sweeprolog-count-holes): new command, counts holes
in the current buffer.
(sweeprolog-forward-hole, sweeprolog-backward-hole): new numeric
argument, corresponding to the prefix argument when called
interactively. With positive/negative numeric argument, move over
that many holes. With zero numeric argument, call
sweeprolog-count-holes instead.
Eshel Yaron [Mon, 23 Jan 2023 18:20:02 +0000 (20:20 +0200)]
yank holes back as plain variables
* sweeprolog.el (sweeprolog-mode): add sweeprolog-hole to
yank-excluded-properties buffer-locally.
* sweeprolog-tests.el: test it.
* README.org (Holes): mention it.
Eshel Yaron [Sun, 22 Jan 2023 06:13:15 +0000 (08:13 +0200)]
ADDED: command to pipe output of Prolog goals to Emacs buffers
* sweep.c (sweep_open_channel(), sweep_fd_open()): new helper
functions for obtaining Prolog streams from Emacs pipe buffers.
* sweep.pl (sweep_async_goal/2, sweep_interrupt_async_goal/2): new
predicates.
* sweeprolog.el (sweeprolog-async-goal): new command, executes a goal
in a separate thread and redirects its output to a buffer with mode..
(sweeprolog-async-goal-output-mode): new major mode, derived from
Compilation mode.
(sweeprolog-mode-map, sweeprolog-prefix-map, sweeprolog-menu): bind
sweeprolog-async-goal.
* README.org (Executing Prolog Asynchronously): new manual section.
Eshel Yaron [Fri, 20 Jan 2023 18:45:14 +0000 (20:45 +0200)]
ENHANCED: consult with library index for finding predicate defs
* sweep.pl (sweep_predicate_location_/4): also check library_index/3.
* sweeprolog-tests.el: add test for finding the source location of a
predicate in the package clib.
Eshel Yaron [Thu, 19 Jan 2023 09:15:35 +0000 (11:15 +0200)]
ADDED: right-click context menus in sweeprolog-mode
* sweeprolog.el (sweeprolog-context-menu-find-module)
(sweeprolog-context-menu-find-module-other-window)
(sweeprolog-context-menu-describe-module)
(sweeprolog-context-menu-find-file)
(sweeprolog-context-menu-find-file-other-window)
(sweeprolog-context-menu-describe-predicate): new commands, add to context menus by...
(sweeprolog-context-menu-for-predicate)
(sweeprolog-context-menu-for-module)
(sweeprolog-context-menu-for-file): new functions, used in...
(sweeprolog-context-menu-functions): new abnormal hook, used by...
(sweeprolog-context-menu-function): new function.
(sweeprolog-mode): add it to context-menu-functions.