Skip to content

Commit

Permalink
Use structs for fkeys, allow short emacs-style key specs
Browse files Browse the repository at this point in the history
- Instead of verbose keyword names like :key-ctrl-alt-arrow-left,
  store function keys as structs where modifiers crtl, alt and shift
  are slots.
- Some keys like :key-arrow-left can thus be renamed back to their
  previous short names like :left.
- Add functions to parse emacs-style key specs and key chains to
  characters and key structs:
- This allows emacs-style, compact key descriptions like
  C-M-S-<left>, that now can be used with bind for key bindings.
- define-keymap can also use structs for modified function keys
  but cant yet bind key chains.
- Adapt event-case to switch on key-name in case of key events.
- Add equalp as the test function to all assoc calls where key
  structs are used.
- Adapt functions that assumed that function keys are keywords to
  assume they are structs.
- Rename all mouse events from past for to infinitive, e.g. from
  :button-1-clicked to :button-1-click.
- Remove the modifiers flot from mouse-event because modifiers
  are now encoded in the event-key struct.
- Add method key-to-string for key structs.
- Add code-key and key-code, equivalent of cl:char-code and
  cl:code-char, to translate between key structs and ncurses codes.
- Add internal function get-key-event to check if the event is
  :mouse and to make the event object accordingly, which is then
  used for both get-event and get-wide-event.
- Fix wrap to handle long words at the end of a line.
- Fix element keymap definitions. An empty list for missing parent
  keymaps has to be explicitely given, like in defclass.
  • Loading branch information
McParen committed Mar 17, 2024
1 parent 282145f commit a1924cc
Show file tree
Hide file tree
Showing 16 changed files with 904 additions and 704 deletions.
38 changes: 38 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@

Notable changes since v0.2 (231014), which corresponds to Quicklisp release 231021:

*** Use structs for fkeys, allow short emacs-style key specs (240317)

- Instead of verbose keyword names like =:key-ctrl-alt-arrow-left=,
store function keys as structs where modifiers crtl, alt and shift
are slots.
- Some keys like :key-arrow-left can thus be renamed back to their
previous short names like :left.
- Add functions to parse emacs-style key specs and key chains to
characters and key structs:

#+BEGIN_EXAMPLE
CL-USER> (parse-key "C-x M-c C-M-S-<home>")
(#\Can #\Esc #\c #S(KEY :NAME :HOME :CTRL T :ALT T :SHIFT T))
#+END_EXAMPLE

- This allows emacs-style, compact key descriptions like
=C-M-S-<left>=, that now can be used with bind for key bindings.
- =define-keymap= can also use structs for modified function keys
but cant yet bind key chains.
- Adapt event-case to switch on key-name in case of key events.
- Add equalp as the test function to all assoc calls where key
structs are used.
- Adapt functions that assumed that function keys are keywords to
assume they are structs.
- Rename all mouse events from past for to infinitive, e.g. from
=:button-1-clicked= to =:button-1-click.=
- Remove the modifiers flot from mouse-event because modifiers
are now encoded in the event-key struct.
- Add method =key-to-string= for key structs.
- Add =code-key= and =key-code=, equivalent of cl:char-code and
cl:code-char, to translate between key structs and ncurses codes.
- Add internal function =get-key-event= to check if the event is
:mouse and to make the event object accordingly, which is then
used for both get-event and get-wide-event.
- Fix wrap to handle long words at the end of a line.
- Fix element keymap definitions. An empty list for missing parent
keymaps has to be explicitely given, like in defclass.

*** Add extended function keys to the list of recognized events (240127)

- After initializing the screen, check whether common extended
Expand Down
41 changes: 30 additions & 11 deletions src/classes.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,39 @@ component +--> widget ---+--> window +--> screen
+--> form --------> form-window
|#

(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct key
"Stuct representing a function key or a mouse button with modifier keys.
A key struct is associated with an escape sequence which is parsed by ncurses
and returned from getch or get_wch as an integer code.
Every combination of a key name and the modifiers is represented by a separate
code.
Supported modifiers are ctrl (C), alt (M) and shift (S).
In bindings, emacs notation is used to specify the keys which are then parsed
to structs, for example C-S-M-<left>, C-<button-1-click>.
Also see: `bind'."
(name nil :type keyword)
(ctrl nil :type boolean)
(alt nil :type boolean)
(shift nil :type boolean))

;; load this during compile time so we can read structs.
(defmethod make-load-form ((key key) &optional env)
(declare (ignore env))
(make-load-form-saving-slots key)))

(defclass event ()
((key
:initarg :key
:initform nil
:reader event-key
:type (or null integer keyword character)
:documentation "Character or keyword representing a function key, terminal :resize or mouse event.")
:type (or null integer keyword character key)
:documentation "Lisp character representing a character key or key struct representing a function key or mouse button.")

(code
:initarg :code
Expand All @@ -52,7 +78,7 @@ component +--> widget ---+--> window +--> screen
:type (or null integer)
:documentation "Integer code representing the character or function key as returned by ncurses:getch or get_wch."))

(:documentation ""))
(:documentation "And event represents characters, function keys, mouse buttons and terminal resize events."))

(defclass mouse-event (event)
((position-y
Expand All @@ -65,14 +91,7 @@ component +--> widget ---+--> window +--> screen
:initarg :x
:initform nil
:type (or null integer)
:documentation "The x coordinate (column) of the mouse event.")

(modifiers
:initarg :modifiers
:initform nil
:reader event-modifiers
:type (or null cons)
:documentation "A list containing any combination of :ctrl, :shift and :alt"))
:documentation "The x coordinate (column) of the mouse event."))

(:documentation "The class represents the ncurses MEVENT struct as returned by getmouse."))

Expand Down

0 comments on commit a1924cc

Please sign in to comment.