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

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

Re: Creating a Tab binding to use within comments and other text ??


From: Pascal J. Bourguignon
Subject: Re: Creating a Tab binding to use within comments and other text ??
Date: Sat, 19 Sep 2009 21:15:43 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

William Case <billlinux@rogers.com> writes:

> Hi;
>
> I started out this morning to create a key binding for a different TAB
> key but nothing is working right.
>
> I want to keep TAB, M-i and M-j just as they are.  I use them regularly.
>
> But for text writing, particularly within comments, I want a 'text-tab'
> that I can use to lineup lists and bullet points, sub topics etc.  I
> don't want this 'text-tab' to interfere with the operation of the above.
>
> It would be nice if I had a way to set tab stops for this 'text-tab'
> that is independent of the main tab-stop bar.
>
> So far I have being trying to bind something to s-i (s-<TAB> would be
> useful as well) but almost any key combination would do.
>
> Nothing on the Wiki jumped out at me.  All the possibilities there seem
> to be for altering the main TAB functions.
>
> Any suggestions?

You may bind your own command, that would first check whether it's
inside a comment, and if not, it would call the original command.


However, each mode may override the key bindings, such as that of TAB.
If you want to be able to override any tab command, the we would have
to hook to a lower level, that is, you would have to patch the C
function call-interactively.   

An alternative would be to modify the mode where you want to have this
behavior (possibly thru a mode specific hook).



Here is a more ad-hoc way to do it: 


(defvar *original-tab-command* nil)
(make-local-variable '*original-tab-command*)

(defun install-my-tab ()
  (interactive)
  (unless (eql (key-binding (kbd "TAB")) 'my-tab)
    (setf *original-tab-command* (key-binding (kbd "TAB")))
    (local-set-key (kbd "TAB") 'my-tab)))

(defun my-tab ()
  (interactive)
  (if (save-excursion (comment-beginning))
      ;; inside a comment
      (progn
        ;; do whatever you want. For example, insert four spaces:
        (insert "    "))
      (call-interactively *original-tab-command*)))


So in a buffer where you want to override the current TAB command in
comments, you could run M-x install-my-tab RET.


-- 
__Pascal Bourguignon__


reply via email to

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