help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using use-package


From: Phillip Lord
Subject: Re: using use-package
Date: Wed, 29 Jun 2016 18:02:16 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>>>>> Well I would have thought that needing eval-after-load and relatives
>>>>> is evidence of a badly written (in Stefan's sense) package
>> Yeah, I missed the "not" in this paragraph.
>>> I do think it's a problem, but admittedly, the way keymaps are normally
>>> setup there's no standard&easy way for the package to "do the right
>>> thing".
>> But not evidence of a badly written package:-)
>
> Not until "the right thing to do" is designed and implemented, no.

What about something like this below? We can now do something like:

(extend-key 'fake-feature-map "\C-f" 'foward-char)

It has the same syntax as define-key but works whether fake-feature-map
has been defined or not. Could be macro'd to remove the quote, of course.

Phil


(defvar extend-key--pending-definitions nil
  "Key definitions to occur on load")

(defun extend-key--define-now (keymap-symbol key def)
  "In KEYMAP-SYMBOL define KEY with definition DEF."
  (define-key (symbol-value keymap-symbol) key def))

(defun extend-key--define-later (keymap-symbol key def)
  "In KEYMAP-SYMBOL define KEY with definition DEF, once
KEYMAP-SYMBOL has been defined."
  (setq
   extend-key--pending-definitions
   (cons
    (list keymap-symbol key def)
    extend-key--pending-definitions)))

(defun extend-key--after-load-function (_)
  (setq extend-key--pending-definitions
        (seq-filter
         (lambda (n)
           (seq-let
               [keymap-symbol key def] n
             (when (extend-key--boundp keymap-symbol)
               (extend-key--define-now keymap-symbol key def))))
         extend-key--pending-definitions)))

(add-hook 'after-load-functions
          'extend-key--after-load-function)

(defun extend-key--boundp (keymap-symbol)
  (or (boundp keymap-symbol)
      (fboundp keymap-symbol)))

(defun extend-key (keymap-symbol key def)
  "When KEYMAP-SYMBOL is defined, define key sequence KEY as DEF."
  (if (extend-key--boundp keymap-symbol)
      (extend-key--define-now keymap-symbol key def)
    (extend-key--define-later keymap-symbol key def)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]