Определение внутренней переменной Elisp для второстепенного режима (setq / make-local-variable / let) - PullRequest
2 голосов
/ 28 сентября 2011

Я написал этот небольшой второстепенный режим, чтобы заставить клавишу TAB продолжить отступ после того, как основной режим выполнил начальное поведение отступа и / или заставить отступ, когда основной режим считает, что отступ не требуется.

Мне было отмечено, что комбинация setq и make-local-variable, вероятно, может быть упрощена до объема let.Учитывая, что это должно работать одновременно с несколькими буферами, как можно изменить это, чтобы использовать let вместо make-local-variable?

;;; dwim-tab.el --- minor mode to force indentation when TAB would otherwise stall

; internal tracking variables
(setq dwim-tab-point-before nil)
(setq dwim-tab-point-after nil)

(defun dwim-tab ()
  "Indents normally once, then switches to tab-to-tab-stop if invoked again.
Always performs tab-to-tab-stop if the first TAB press does not cause the
point to move."
  (interactive)
  (setq dwim-tab-point-before (point))
  (if (eq dwim-tab-point-before dwim-tab-point-after) ; pressed TAB again
      (tab-to-tab-stop) 
    (indent-for-tab-command))
  (if (eq (point) dwim-tab-point-before) ; point didn't move
      (tab-to-tab-stop))
  (setq dwim-tab-point-after (point)))

(define-minor-mode dwim-tab-mode
  "Toggle dwim-tab-mode.
With a non-nil argument, turns on dwim-tab-mode. With a nil argument, turns it
off.

When dwim-tab-mode is enabled, pressing the TAB key once will behave as normal,
but pressing it subsequent times, will continue to indent, using
tab-to-tab-stop.

If dwim-tab determines that the first TAB key press resulted in no movement of
the point, it will indent according to tab-to-tab-stop instead."
  :init-value nil
  :lighter " DWIM"
  :keymap '(("\t" . dwim-tab))
  (make-local-variable 'dwim-tab-point-before)
  (make-local-variable 'dwim-tab-point-after))

(provide 'dwim-tab)

1 Ответ

6 голосов
/ 28 сентября 2011

Делает ли это то, что вы хотите?Послушай, ма, без переменных!

(defun tab-dwim ()
  (interactive)
  (when (or (eq last-command this-command)      
            (= (point) (progn
                         (indent-for-tab-command)
                         (point))))
      (tab-to-tab-stop)))

Проверка последней команды не обязательна, предполагая, что indent-for-tab-command не будет магически начинать отступ.Но он немного эффективнее процессора.

...