1

I am using the function tika-keymap-showhrk which takes a list of modes, then constructs the associated -map variable value for use with define-key.

But I get the error

Debugger entered--Lisp error: (void-variable sh-mode-map)
  symbol-value(sh-mode-map)

Here is the implementation code

(defun tika-keymap-showhrk (mdlist)
  "Bind key sequence in keymap to the outline-show-all command."
  (dolist (mjmode mdlist)
    (let ( ($map (symbol-value (intern (format "%s-map" mjmode)))) )
      (define-key $map (kbd "H-o a") #'outline-show-all))) )

(defvar tika-mjmode
  '(emacs-lisp-mode sh-mode f90-mode fortran-mode latex-mode
     plain-tex-mode) )

(defun tika-keymap-outlhrk ()
  "My Elisp stuff."
  (tika-keymap-showhrk tika-mjmode))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

0

That variable is defined and its default value set in sh-script.el. If you want to take advantage of the bindings it defines, in addition to adding bindings to those, then you need to get that default definition by loading sh-script.el[c].

If for some reason you don't want to require sh-script.el when you invoke tika-keymap-showhrk, you can instead add a defvar that defines all of the bindings you want, including the default bindings. But then, if the default bindings change in some future Emacs release your defvar would need to be updated if you want to take advantage of those changes.

(defun tika-keymap-showhrk (mdlist)
  "Bind key sequence in keymap to the outline-show-all command."
  (require 'sh-script)  ; <============
  (dolist (mjmode  mdlist)
    (let (($map  (symbol-value (intern (format "%s-map" mjmode)))))
      (define-key $map (kbd "H-o a") #'outline-show-all))) )
Drew
  • 75,699
  • 9
  • 109
  • 225
  • Would I need to do the same for all possible major mode inputs ? – Dilna Jun 25 '23 at 15:25
  • Probably. The library defining the mode likely defines its keymap. If you expect the keymap to be defined you need to either define it yourself or load the code that defines it. – Drew Jun 25 '23 at 16:29
  • Is this the only way to define the keybindings or am I making things difficult for myself ? – Dilna Jun 25 '23 at 16:36
  • Would you suggest to set the major mode keymap only after loading the elisp file or bash script file? – Dilna Jun 25 '23 at 17:21
  • I don't understand your comment questions (and please post questions separately). Loading the file that defines the map "sets" the map. If you want to change the bindings (e.g., add to them) then the bindings have to exist. If you only want to replace all bindings that the library will define then you can just put your own `defvar` in your code, and not bother with `require`ing the library - it will anyway be loaded when you use the mode, and its `defvar` will then be ignored. – Drew Jun 25 '23 at 19:32