123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include "private.h"
- #include "lub/string.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
- const char *clish_hotkey_list[] = {
- "^@",
- "^A",
- "^B",
- "^C",
- "^D",
- "^E",
- "^F",
- "^G",
- "^H",
- "^I",
- "^J",
- "^K",
- "^L",
- "^M",
- "^N",
- "^O",
- "^P",
- "^Q",
- "^R",
- "^S",
- "^T",
- "^U",
- "^V",
- "^W",
- "^X",
- "^Y",
- "^Z",
- "^[",
- "^\\",
- "^]",
- "^^",
- "^_",
- NULL
- };
- static clish_hotkey_t *find_hotkey(clish_hotkeyv_t *this, int code)
- {
- unsigned int i;
- clish_hotkey_t *result = NULL;
- if (!this)
- return NULL;
-
- for (i = 0; i < this->num; i++) {
- clish_hotkey_t *hk = this->hotkeyv[i];
- if (code == hk->code) {
- result = hk;
- break;
- }
- }
- return result;
- }
- const char *clish_hotkeyv_cmd_by_code(clish_hotkeyv_t *this, int code)
- {
- clish_hotkey_t *hk;
- if (!this)
- return NULL;
- hk = find_hotkey(this, code);
- if (!hk)
- return NULL;
- return hk->cmd;
- }
- int clish_hotkeyv_insert(clish_hotkeyv_t *this,
- const char *key, const char *cmd)
- {
- int code = -1;
- int i;
- if (!this)
- return -1;
-
- i = 0;
- while (clish_hotkey_list[i]) {
- if (!strcmp(clish_hotkey_list[i], key))
- code = i;
- i++;
- }
- if (code < 0)
- return -1;
-
- clish_hotkey_t *hk = find_hotkey(this, code);
- if (hk) {
-
- lub_string_free(hk->cmd);
- } else {
- size_t new_size = ((this->num + 1) * sizeof(clish_hotkey_t *));
- clish_hotkey_t **tmp;
-
- tmp = realloc(this->hotkeyv, new_size);
-
- this->hotkeyv = tmp;
-
- hk = malloc(sizeof(*hk));
- this->hotkeyv[this->num++] = hk;
- hk->code = code;
- }
- hk->cmd = NULL;
- if (cmd)
- hk->cmd = lub_string_dup(cmd);
- return 0;
- }
- clish_hotkeyv_t *clish_hotkeyv_new(void)
- {
- clish_hotkeyv_t *this;
- this = malloc(sizeof(clish_hotkeyv_t));
- this->num = 0;
- this->hotkeyv = NULL;
- return this;
- }
- static void clish_hotkeyv_fini(clish_hotkeyv_t *this)
- {
- unsigned int i;
- for (i = 0; i < this->num; i++) {
- lub_string_free(this->hotkeyv[i]->cmd);
- free(this->hotkeyv[i]);
- }
- free(this->hotkeyv);
- }
- void clish_hotkeyv_delete(clish_hotkeyv_t *this)
- {
- if (!this)
- return;
- clish_hotkeyv_fini(this);
- free(this);
- }
|