Skip to content

Commit

Permalink
Add a text property to display string in the right margin (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaiKinono authored Apr 12, 2020
1 parent f1180c7 commit b2f74f3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ The format is based on [Keep a Changelog].
* We now bind `minibuffer-completing-file-name` during
`read-file-name`, in conformance with the standard Emacs interface
([#30]).
* A new text property `selectrum-candidate-display-right-margin` is
added, to display a string at the right margin after a candidate
([#44]).

### Bugs fixed
* You can now use the undo system in the minibuffer. Previously,
Expand All @@ -50,6 +53,7 @@ The format is based on [Keep a Changelog].
[#32]: https://github.com/raxod502/selectrum/issues/32
[#33]: https://github.com/raxod502/selectrum/pull/33
[#34]: https://github.com/raxod502/selectrum/pull/34
[#44]: https://github.com/raxod502/selectrum/pull/44

## 1.0 (released 2020-03-23)
### Added
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ which may be applied to candidates using `propertize`:
`find-file`, the canonical representation of each candidate is its
absolute path on the filesystem.

Besides, we have:

* `selectrum-candidate-display-right-margin`: if this property is
presented, its value is displayed at the right margin after the
candidate. Currently Selectrum doesn't make use of this property. It
can be used to display supplementary information in user-defined
commands.

Note that sorting, filtering, and highlighting is done on the standard
values of candidates, before any of these text properties are handled.

Expand Down
79 changes: 59 additions & 20 deletions selectrum.el
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ Possible values are:
(const :tag "Count matches and show current match"
'current/matches)))

(defcustom selectrum-right-margin-padding 1
"The number of spaces to add after right margin text.
This only takes effect when the
`selectrum-candidate-display-right-margin' property is presented
in candidates.
This option is a workaround for 2 problems:
- Some terminals will wrap the last character of a line when it
exactly fits.
- Emacs doesn't provide a method to calculate the exact pixel
width of a unicode char, so a wide char can cause line
wrapping."
:type 'integer)

;;;; Utility functions

;;;###autoload
Expand Down Expand Up @@ -363,6 +379,9 @@ Passed to various hook functions.")
(defvar selectrum--count-overlay nil
"Overlay used to display count information before prompt.")

(defvar selectrum--right-margin-overlays nil
"A list of overlays used to display right margin text.")

;;;; Hook functions

(defun selectrum--count-info ()
Expand Down Expand Up @@ -420,6 +439,8 @@ Passed to various hook functions.")
0)))
(overlay-put selectrum--count-overlay
'before-string (selectrum--count-info))
(while selectrum--right-margin-overlays
(delete-overlay (pop selectrum--right-margin-overlays)))
(setq input (or selectrum--visual-input input))
(let ((first-index-displayed
(if selectrum--current-candidate-index
Expand Down Expand Up @@ -459,25 +480,41 @@ Passed to various hook functions.")
(minibuffer-prompt-end) bound
'(face selectrum-current-candidate)))
(let ((index 0))
(dolist (candidate (mapcar
(lambda (candidate)
(concat
(get-text-property
0 'selectrum-candidate-display-prefix
candidate)
candidate
(get-text-property
0 'selectrum-candidate-display-suffix
candidate)))
(funcall
selectrum-highlight-candidates-function
input
displayed-candidates)))
(when (equal index highlighted-index)
(setq candidate (propertize
candidate
'face 'selectrum-current-candidate)))
(insert "\n" candidate)
(dolist (candidate (funcall
selectrum-highlight-candidates-function
input
displayed-candidates))
(let ((displayed-candidate
(concat
(get-text-property
0 'selectrum-candidate-display-prefix
candidate)
candidate
(get-text-property
0 'selectrum-candidate-display-suffix
candidate)))
(right-margin (get-text-property
0 'selectrum-candidate-display-right-margin
candidate)))
(when (equal index highlighted-index)
(setq displayed-candidate
(propertize
displayed-candidate
'face 'selectrum-current-candidate)))
(insert "\n" displayed-candidate)
(when right-margin
(let ((ol (make-overlay (point) (point))))
(overlay-put
ol 'after-string
(concat
(propertize
" "
'display
`(space :align-to (- right-fringe
,(string-width right-margin)
selectrum-right-margin-padding)))
right-margin))
(push ol selectrum--right-margin-overlays))))
(cl-incf index))))
(add-text-properties bound (point-max) '(read-only t))
(setq selectrum--end-of-input-marker (set-marker (make-marker) bound))
Expand All @@ -493,7 +530,9 @@ Passed to various hook functions.")
(remove-hook 'minibuffer-exit-hook #'selectrum--minibuffer-exit-hook 'local)
(when (overlayp selectrum--count-overlay)
(delete-overlay selectrum--count-overlay))
(setq selectrum--count-overlay nil))
(setq selectrum--count-overlay nil)
(while selectrum--right-margin-overlays
(delete-overlay (pop selectrum--right-margin-overlays))))

(cl-defun selectrum--minibuffer-setup-hook
(candidates &key default-candidate initial-input require-match)
Expand Down

0 comments on commit b2f74f3

Please sign in to comment.