【发布时间】:2013-10-21 02:10:23
【问题描述】:
困境:可读性还是可维护性?
让我们看看下面的函数。
它做什么并不重要,重要的是
它使用了两倍的字符串"(let\\*?[ \t]*":
(defun setq-expression-or-sexp ()
"Return the smallest list that contains point.
If inside VARLIST part of `let' form,
return the corresponding `setq' expression."
(interactive)
(ignore-errors
(save-excursion
(up-list)
(let ((sexp (preceding-sexp)))
(backward-list 1)
(cond
((looking-back "(let\\*?[ \t]*")
(cons 'setq
(if (= (length sexp) 1)
(car sexp)
(cl-mapcan
(lambda (x) (unless (listp x) (list x nil)))
sexp))))
((progn
(up-list)
(backward-list 1)
(looking-back "(let\\*?[ \t]*"))
(cons 'setq sexp))
(t
sexp))))))
由于必须在两个(或更多)位置更新字符串是一件令人头疼的事情,
我必须像这样defconst:
(defconst regex-let-form "(let\\*?[ \t]*")
虽然代码变得更易于维护,但它的可读性也降低了,
因为很难一眼看出regex-let-form到底是什么:
(defun setq-expression-or-sexp ()
"Return the smallest list that contains point.
If inside VARLIST part of `let' form,
return the corresponding `setq' expression."
(interactive)
(ignore-errors
(save-excursion
(up-list)
(let ((sexp (preceding-sexp)))
(backward-list 1)
(cond
((looking-back regex-let-form)
(cons 'setq
(if (= (length sexp) 1)
(car sexp)
(cl-mapcan
(lambda (x) (unless (listp x) (list x nil)))
sexp))))
((progn
(up-list)
(backward-list 1)
(looking-back regex-let-form))
(cons 'setq sexp))
(t
sexp))))))
想法:为什么不两者兼而有之?
既然它是一个常数,为什么不font-lock它
并使regex-let-form 看起来好像是"(let\\*?[ \t]*"?
这是一项可行的工作,因为:
可以像这样对标识符进行字体锁定:http://www.emacswiki.org/emacs/PrettyLambda, 甚至是:rainbow-mode。
并且可以对常量进行字体锁定。它已经为 c++-mode 完成了, 但据我所知,emacs-lisp-mode 还没有。
那么剩下的只是将两者联系起来。不幸的是,我不知道
足够的font-lock innards 可以做到这一点,但也许其他人会这样做?
还是已经有一个包可以做到这一点?
【问题讨论】: