ポイントが行のどこにあるかによって異なる動作をするのは簡単です((if (looking-back "^") ...)
コードを参照)。「通常はタブとして機能する」というのは、状況に応じて難しいことです。
これが1つのアプローチですが、後で、より堅牢な方法は、TABの独自のバインディングを使用してマイナーモードを定義し、その関数にフォールバックバインディングを動的に検索させることだと考えていました。最後のビットを実行する方法がわかりませんでしたが、ここに解決策があります:
Emacsキーバインディングフォールバック
(defvar my-major-mode-tab-function-alist nil)
(defmacro make-my-tab-function ()
"Return a major mode-specific function suitable for binding to TAB.
Performs the original TAB behaviour when point is at the beginning of
a line, and moves point to the end of the line otherwise."
;; If we have already defined a custom function for this mode,
;; return that (otherwise that would be our fall-back function).
(or (cdr (assq major-mode my-major-mode-tab-function-alist))
;; Otherwise find the current binding for this mode, and
;; specify it as the fall-back for our custom function.
(let ((original-tab-function (key-binding (kbd "TAB") t)))
`(let ((new-tab-function
(lambda ()
(interactive)
(if (looking-back "^") ;; point is at bol
(,original-tab-function)
(move-end-of-line nil)))))
(add-to-list 'my-major-mode-tab-function-alist
(cons ',major-mode new-tab-function))
new-tab-function))))
(add-hook
'java-mode-hook
(lambda () (local-set-key (kbd "TAB") (make-my-tab-function)))
t) ;; Append, so that we run after the other hooks.