Skip to content

Commit

Permalink
Add/Change: (ement-complete-room) Complete in room list buffers
Browse files Browse the repository at this point in the history
And use keyword args.
  • Loading branch information
alphapapa committed Apr 15, 2022
1 parent 163d262 commit 12c2abf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ement-room.el
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ Interactively, set the current buffer's ROOM's TOPIC."
"Leave ROOM on SESSION.
ROOM may be an `ement-room' struct, or a room ID or alias
string."
(interactive (ement-complete-room (ement-complete-session)))
(interactive (ement-complete-room :session (ement-complete-session)))
(cl-assert room) (cl-assert session)
(cl-etypecase room
(ement-room)
Expand Down
50 changes: 40 additions & 10 deletions ement.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@

;; Third-party.

(require 'magit-section)

;; This package.
(require 'ement-api)
(require 'ement-macros)
(require 'ement-structs)
(require 'ement-room)
(require 'ement-notify)

;;;;; Compilation

;; To avoid compilation warnings.

(eval-when-compile
(require 'taxy-magit-section))

;;;; Variables

(defvar ement-sessions nil
Expand Down Expand Up @@ -273,7 +282,7 @@ in them won't work."
"Switch to a buffer showing ROOM on SESSION.
Calls `pop-to-buffer-same-window'. Interactively, with prefix,
call `pop-to-buffer'."
(interactive (ement-complete-room (ement-complete-session) nil))
(interactive (ement-complete-room :session (ement-complete-session) :suggest nil))
(pcase-let* (((cl-struct ement-room (local (map buffer))) room))
(unless (buffer-live-p buffer)
(setf buffer (ement-room--buffer session room
Expand Down Expand Up @@ -419,7 +428,7 @@ new one automatically if necessary."
(interactive
(let* ((session (ement-complete-session))
(user-id (ement-complete-user-id))
(room (car (ement-complete-room session))))
(room (car (ement-complete-room :session session))))
(list user-id room session)))
(pcase-let* ((endpoint (format "rooms/%s/invite"
(url-hexify-string (ement-room-id room))))
Expand Down Expand Up @@ -546,12 +555,17 @@ If no URI is found, prompt the user for the hostname."
(alist-get selected-id ement-sessions nil nil #'equal)))
(otherwise (user-error "No active sessions. Call `ement-connect' to log in"))))

(cl-defun ement-complete-room (&optional session (suggest t))
(cl-defun ement-complete-room (&key session predicate
(prompt "Room: ") (suggest t))
"Return a (room session) list selected from SESSION with completion.
If SESSION is nil, select from rooms in all of `ement-sessions'.
When SUGGEST, suggest current buffer's room as initial
input (i.e. it should be set to nil when switching from one room
buffer to another)."
When SUGGEST, suggest current buffer's room (or a room at point
in a room list buffer) as initial input (i.e. it should be set to
nil when switching from one room buffer to another). PROMPT may
override the default prompt. PREDICATE may be a function to
select which rooms are offered; it is also applied to the
suggested room."
(declare (indent defun))
(pcase-let* ((sessions (if session
(list session)
(mapcar #'cdr ement-sessions)))
Expand All @@ -561,12 +575,28 @@ buffer to another)."
collect (cons (ement--format-room room)
(list room session)))))
(names (mapcar #'car name-to-room-session))
(selected-name (completing-read "Room: " names nil t
(when (and suggest (equal major-mode 'ement-room-mode))
;; Suggest current buffer's room.
(ement--format-room ement-room)))))
(selected-name (completing-read
prompt names nil t
(when suggest
(when-let ((suggestion (ement--room-at-point)))
(when (or (not predicate)
(funcall predicate suggestion))
suggestion))))))
(alist-get selected-name name-to-room-session nil nil #'string=)))

(defun ement--room-at-point ()
"Return room at point.
Works in major-modes `ement-room-mode', `ement-room-list-mode',
and `ement-taxy-mode'."
(pcase major-mode
('ement-room-mode (ement--format-room ement-room))
('ement-room-list-mode (ement--format-room (tabulated-list-get-id)))
('ement-taxy-mode
(cl-typecase (oref (magit-current-section) value)
(taxy-magit-section nil)
(t (pcase (oref (magit-current-section) value)
(`[,room ,_session] (ement--format-room room))))))))

(defun ement--format-room (room)
"Return ROOM formatted with name, alias, ID, and topic.
Suitable for use in completion, etc."
Expand Down

0 comments on commit 12c2abf

Please sign in to comment.