From 5c8e1d582896f7a79d8562f90fed5f3992bc5319 Mon Sep 17 00:00:00 2001 From: cage Date: Fri, 6 Aug 2021 18:25:15 +0200 Subject: [PATCH] - added a module to expand abbreviations for the command window. --- modules/expand-abbrev-command-window.lisp | 54 +++++++++++++++++++++++ src/command-window.lisp | 5 ++- src/hooks.lisp | 3 ++ src/package.lisp | 3 +- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 modules/expand-abbrev-command-window.lisp diff --git a/modules/expand-abbrev-command-window.lisp b/modules/expand-abbrev-command-window.lisp new file mode 100644 index 0000000..15ad1bb --- /dev/null +++ b/modules/expand-abbrev-command-window.lisp @@ -0,0 +1,54 @@ +;; tinmop module to expand abbreviations on the command window +;; Copyright © 2021 cage + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(in-package :modules) + +(defparameter *expand-abbrev-rewriting-rules* '(("^\\^g" "gemini://")) + "Before displaying messages that module will rewrites the first + element of each item of this list with the second + +Example + + (\"foo\" \"bar\") + ^^^ + first + ^^^ + second + +will replace 'foo' with 'bar'. + +So the whole list is like: '((\"foo\" \"bar\") (\"old\" \"new\") ...)") + +(defun abbrev-re (abbrev) + (first abbrev)) + +(defun abbrev-replace (abbrev) + (second abbrev)) + +(defun expand-abbrev-command-hook-fn (command-window) + (let ((expanded (command-window:command-line command-window))) + (loop for expansion in *expand-abbrev-rewriting-rules* + when (scan (abbrev-re expansion) expanded) + do + (setf expanded (regex-replace (abbrev-re expansion) + expanded + (abbrev-replace expansion)))) + (setf (command-window:command-line command-window) expanded) + (point-tracker:move-point-to-end command-window expanded)) + command-window) + +(hooks:add-hook 'hooks:*after-char-to-command-window* + #'expand-abbrev-command-hook-fn) diff --git a/src/command-window.lisp b/src/command-window.lisp index a67ef13..6e3f359 100644 --- a/src/command-window.lisp +++ b/src/command-window.lisp @@ -21,7 +21,7 @@ :initform () :initarg :command-line :accessor command-line - :documentation "A list of keys so far inserted by the user") + :documentation "A list of keys so far inserted by the user, or the input string, depending on the mode.") (echo-character :initform nil :initarg :echo-character @@ -539,6 +539,9 @@ command line." (win-show suggestions-win) (setf command-line (insert-at-point command-window event command-line)) + (when 'hooks:*after-char-to-command-window* + (hooks:run-hook 'hooks:*after-char-to-command-window* + command-window)) (show-candidate-completion command-window))))))) command-window) diff --git a/src/hooks.lisp b/src/hooks.lisp index 9af714c..35dea99 100644 --- a/src/hooks.lisp +++ b/src/hooks.lisp @@ -108,3 +108,6 @@ Each function takes 1 parameter: the database row for the saved status.") (defparameter *before-displaying-links-hook* '() "Run this hooks before sending the list of URLs to the window that allow the user to open the links") + +(defparameter *after-char-to-command-window* '() + "Run this hooks after a character has been typed by the user on the command window.") diff --git a/src/package.lisp b/src/package.lisp index 30c98b3..7889fca 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -1554,7 +1554,8 @@ :*before-sending-message* :*skip-message-hook* :*after-saving-message* - :*before-displaying-links-hook*)) + :*before-displaying-links-hook* + :*after-char-to-command-window*)) (defpackage :keybindings (:use