emacs-devel
[Top][All Lists]
Advanced

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

Re: mykie.el


From: Stefan Monnier
Subject: Re: mykie.el
Date: Mon, 06 Jan 2014 19:37:01 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

SM> Is that really much better than [vanilla Emacs lisp]?
> Yes.  The difference is huge for a beginner.

I'm far from convinced.  The beginner will have to learn the
mykie-specific sublanguage.  Maybe it's slightly simpler, but then this
effort won't help him understand other code.

Making it easier to customize Emacs is great, but it's better if it also
gets you some way towards hacking on Emacs so as to become a contributor.

Of course, there are cosmetic issues in the mykie.el syntax that rub me
the wrong way.  Most obvious is the need to quote code.  I already spend
enough time telling people not to quote their lambdas and trying to fix
code that abuses `eval' in all kinds of useless ways.

Starting from

(mykie:global-set-key "C-j"
   :default  '(progn
                (delete-trailing-whitespace)
                (case major-mode
                  (org-mode (org-return-indent))
                  (t        (newline-and-indent))))
   :C-u&eolp '(fill-region (point-at-bol) (point-at-eol))
   :region   'query-replace-regexp)

I'd suggest to first get rid of the quotes and the
`mykie:global-set-key' (so that it can be used with
local-set-key, define-key, you name it):

   (global-set-key "C-j"
      (smkie
       :default  (progn
                  (delete-trailing-whitespace)
                  (case major-mode
                    (org-mode (org-return-indent))
                    (t        (newline-and-indent))))
       :C-u&eolp (fill-region (point-at-bol) (point-at-eol))
       :region   'query-replace-regexp))

Then I suggest we use a parenthesized syntax, and descriptive name, to
be more Lispish:

   (global-set-key "C-j"
      (sm-combined-command
       (:default
        (delete-trailing-whitespace)
        (case major-mode
          (org-mode (org-return-indent))
          (t        (newline-and-indent))))
       (:C-u&eolp (fill-region (point-at-bol) (point-at-eol)))
       (:region   'query-replace-regexp)))

Also, since the user knows about "t is default" from "case", we might as
well get rid of ":default":

   (global-set-key "C-j"
      (sm-combined-command
       (:C-u&eolp (fill-region (point-at-bol) (point-at-eol)))
       (:region   'query-replace-regexp)
       (t
        (delete-trailing-whitespace)
        (case major-mode
          (org-mode (org-return-indent))
          (t        (newline-and-indent))))))

And sm-combined-command is a very simple macro:

  (defconst sm-combined-command-predicates
    '((:region . (use-region-p))
      (:C-u . current-prefix-arg)
      (:C-u&eolp . (and current-prefix-arg (eolp)))
      ...))

  (defmacro sm-combined-command (branches)
    `(lambda ()
       (interactive)
       (cond
        ,@(mapcar (lambda (branch)
                    `(,(or (cdr (assq (car branch)
                                      sm-combined-command-predicates))
                           (car branch))
                      ,@(pcase (cdr branch)
                          (`(',cmd) `((call-interactively ',cmd)))
                          (,cmds cmds))))
                  branches))))
          
-- Stefan



reply via email to

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