• Autosetting Tab-Expansion Mode

    From Lawrence D'Oliveiro@21:1/5 to All on Tue Mar 19 07:05:46 2024
    I have gone off putting tabs in source files; so I normally have
    things set so that pressing the tab key inserts the right number of
    spaces to take the position to the next tab stop.

    However, when working with files from other sources that already have
    tabs in them, it is handy to be able to remain compatible by having
    the tab key insert a tab in those files, without any special checking
    on my part.

    Here is a hook for Emacs that automatically runs every time a file is
    opened; it does a quick sniff around to see if it can spot any tabs,
    and if it does, then it sets the tab key to insert a tab, otherwise it
    sets it to insert spaces.

    (add-hook 'find-file-hook
    (lambda ()
    "sets tab expansion depending on whether the current buffer contains tabs."
    (save-excursion
    ; check text within the region of point; if tabs are found, then
    ; indent with tabs, else indent with spaces
    (let
    (
    (checkamt 2048) ; how much text to check
    )
    (when (<= (point-max) (+ (point) checkamt))
    (goto-char
    (cond
    ((>= (point-max) checkamt)
    (- (point-max) checkamt)
    )
    (t
    (point-min)
    )
    ) ; cond
    ) ; goto-char
    ) ; when
    (setq indent-tabs-mode (search-forward "\t" (+ (point) checkamt) t))
    ) ; let
    ) ; save-excursion
    ) ; lambda
    ) ; add-hook

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)