1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-04 08:47:11 +00:00
emacs/lisp/tabify.el

105 lines
4.1 KiB
EmacsLisp
Raw Normal View History

;;; tabify.el --- tab conversion commands for Emacs -*- lexical-binding: t -*-
1992-05-30 21:11:25 +00:00
;; Copyright (C) 1985, 1994, 2001-2021 Free Software Foundation, Inc.
1989-10-31 16:00:07 +00:00
;; Maintainer: emacs-devel@gnu.org
;; Package: emacs
1992-07-22 02:58:48 +00:00
1989-10-31 16:00:07 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1989-10-31 16:00:07 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1989-10-31 16:00:07 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
1989-10-31 16:00:07 +00:00
1993-03-22 16:53:22 +00:00
;;; Commentary:
;; Commands to optimize spaces to tabs or expand tabs to spaces in a region
;; (`tabify' and `untabify'). The variable tab-width does the obvious.
1992-07-14 21:51:51 +00:00
;;; Code:
1989-10-31 16:00:07 +00:00
1991-05-09 21:50:55 +00:00
;;;###autoload
lisp/*.el: Remove lexical-binding warnings; additional small cleanups. * calculator.el (calculator): Mark unused argument. (calculator-paste, calculator-quit, calculator-integer-p): Use ignore-errors. (calculator-string-to-number, calculator-decimal, calculator-exp) (calculator-op-or-exp): Use string-match-p. * dired-aux.el (dired-compress): Use ignore-errors. (dired-do-chxxx, dired-do-chmod, dired-trample-file-versions) (dired-do-async-shell-command, dired-do-shell-command) (dired-shell-stuff-it, dired-compress-file, dired-insert-subdir) (dired-insert-subdir-validate): Use string-match-p. (dired-map-dired-file-lines, dired-subdir-hidden-p): Use looking-at-p. (dired-add-entry): Use string-match-p, looking-at-p. (dired-insert-subdir-newpos): Remove unused local variable. * dired.el (dired-buffer-more-recently-used-p): Declare. (dired-insert-set-properties, dired-insert-old-subdirs): Use ignore-errors. * filenotify.el (file-notify-callback): Remove unused local variable. * filesets.el (filesets-error): Mark unused argument. (filesets-which-command-p, filesets-filter-dir-names) (filesets-directory-files, filesets-get-external-viewer) (filesets-ingroup-get-data): Use string-match-p. * find-file.el (ff-other-file-name, ff-other-file-name) (ff-find-the-other-file, ff-cc-hh-converter): Remove unused local variables. (ff-get-file-name): Use string-match-p. (ff-all-dirs-under): Use ignore-errors. * follow.el (follow-comint-scroll-to-bottom): Mark unused argument. (follow-select-if-visible): Remove unused local variable. * forms.el (read-file-filter): Move declaration. (forms--make-format, forms--make-parser, forms-insert-record): Quote function with #'. (forms--update): Use string-match-p. Quote function with #'. * help-mode.el (help-dir-local-var-def): Mark unused argument. (help-make-xrefs): Use looking-at-p. (help-xref-on-pp): Use looking-at-p, ignore-errors. * ibuffer.el (ibuffer-ext-visible-p): Declare. (ibuffer-confirm-operation-on): Use string-match-p. * msb.el (msb-item-handler, msb-dired-item-handler): Mark unused arguments. * ses.el (ses-decode-cell-symbol) (ses-kill-override): Remove unused local variable. (ses-create-cell-variable, ses-relocate-formula): Use string-match-p. (ses-load): Use ignore-errors, looking-at-p. (ses-jump-safe): Use ignore-errors. (ses-export-tsv, ses-export-tsf, ses-unsafe): Mark unused arguments. * tabify.el (untabify, tabify): Mark unused arguments. * thingatpt.el (thing-at-point--bounds-of-well-formed-url): Mark unused argument. (bounds-of-thing-at-point, thing-at-point-bounds-of-list-at-point) (thing-at-point-newsgroup-p, form-at-point): Use ignore-errors.
2013-08-10 15:17:29 +00:00
(defun untabify (start end &optional _arg)
1989-10-31 16:00:07 +00:00
"Convert all tabs in region to multiple spaces, preserving columns.
If called interactively with prefix ARG, convert for the entire
buffer.
1992-10-30 05:38:03 +00:00
Called non-interactively, the region is specified by arguments
START and END, rather than by the position of point and mark.
The variable `tab-width' controls the spacing of tab stops."
(interactive (if current-prefix-arg
(list (point-min) (point-max) current-prefix-arg)
(list (region-beginning) (region-end) nil)))
(let ((c (current-column)))
(save-excursion
(save-restriction
(narrow-to-region (point-min) end)
(goto-char start)
(while (search-forward "\t" nil t) ; faster than re-search
(forward-char -1)
(let ((tab-beg (point))
(indent-tabs-mode nil)
column)
(skip-chars-forward "\t")
(setq column (current-column))
(delete-region tab-beg (point))
(indent-to column)))))
(move-to-column c)))
1989-10-31 16:00:07 +00:00
(defvar tabify-regexp " [ \t]+"
"Regexp matching whitespace that tabify should consider.
2006-11-24 16:42:26 +00:00
Usually this will be \" [ \\t]+\" to match a space followed by whitespace.
\"^\\t* [ \\t]+\" is also useful, for tabifying only initial whitespace.")
1991-05-09 21:50:55 +00:00
;;;###autoload
lisp/*.el: Remove lexical-binding warnings; additional small cleanups. * calculator.el (calculator): Mark unused argument. (calculator-paste, calculator-quit, calculator-integer-p): Use ignore-errors. (calculator-string-to-number, calculator-decimal, calculator-exp) (calculator-op-or-exp): Use string-match-p. * dired-aux.el (dired-compress): Use ignore-errors. (dired-do-chxxx, dired-do-chmod, dired-trample-file-versions) (dired-do-async-shell-command, dired-do-shell-command) (dired-shell-stuff-it, dired-compress-file, dired-insert-subdir) (dired-insert-subdir-validate): Use string-match-p. (dired-map-dired-file-lines, dired-subdir-hidden-p): Use looking-at-p. (dired-add-entry): Use string-match-p, looking-at-p. (dired-insert-subdir-newpos): Remove unused local variable. * dired.el (dired-buffer-more-recently-used-p): Declare. (dired-insert-set-properties, dired-insert-old-subdirs): Use ignore-errors. * filenotify.el (file-notify-callback): Remove unused local variable. * filesets.el (filesets-error): Mark unused argument. (filesets-which-command-p, filesets-filter-dir-names) (filesets-directory-files, filesets-get-external-viewer) (filesets-ingroup-get-data): Use string-match-p. * find-file.el (ff-other-file-name, ff-other-file-name) (ff-find-the-other-file, ff-cc-hh-converter): Remove unused local variables. (ff-get-file-name): Use string-match-p. (ff-all-dirs-under): Use ignore-errors. * follow.el (follow-comint-scroll-to-bottom): Mark unused argument. (follow-select-if-visible): Remove unused local variable. * forms.el (read-file-filter): Move declaration. (forms--make-format, forms--make-parser, forms-insert-record): Quote function with #'. (forms--update): Use string-match-p. Quote function with #'. * help-mode.el (help-dir-local-var-def): Mark unused argument. (help-make-xrefs): Use looking-at-p. (help-xref-on-pp): Use looking-at-p, ignore-errors. * ibuffer.el (ibuffer-ext-visible-p): Declare. (ibuffer-confirm-operation-on): Use string-match-p. * msb.el (msb-item-handler, msb-dired-item-handler): Mark unused arguments. * ses.el (ses-decode-cell-symbol) (ses-kill-override): Remove unused local variable. (ses-create-cell-variable, ses-relocate-formula): Use string-match-p. (ses-load): Use ignore-errors, looking-at-p. (ses-jump-safe): Use ignore-errors. (ses-export-tsv, ses-export-tsf, ses-unsafe): Mark unused arguments. * tabify.el (untabify, tabify): Mark unused arguments. * thingatpt.el (thing-at-point--bounds-of-well-formed-url): Mark unused argument. (bounds-of-thing-at-point, thing-at-point-bounds-of-list-at-point) (thing-at-point-newsgroup-p, form-at-point): Use ignore-errors.
2013-08-10 15:17:29 +00:00
(defun tabify (start end &optional _arg)
1989-10-31 16:00:07 +00:00
"Convert multiple spaces in region to tabs when possible.
A group of spaces is partially replaced by tabs
when this can be done without changing the column they end at.
If called interactively with prefix ARG, convert for the entire
buffer.
1992-10-30 05:38:03 +00:00
Called non-interactively, the region is specified by arguments
START and END, rather than by the position of point and mark.
The variable `tab-width' controls the spacing of tab stops."
(interactive (if current-prefix-arg
(list (point-min) (point-max) current-prefix-arg)
(list (region-beginning) (region-end) nil)))
1989-10-31 16:00:07 +00:00
(save-excursion
(save-restriction
;; Include the beginning of the line in the narrowing
;; since otherwise it will throw off current-column.
(goto-char start)
(beginning-of-line)
(narrow-to-region (point) end)
1989-10-31 16:00:07 +00:00
(goto-char start)
(let ((indent-tabs-mode t))
(while (re-search-forward tabify-regexp nil t)
;; The region between (match-beginning 0) and (match-end 0) is just
;; spacing which we want to adjust to use TABs where possible.
(let ((end-col (current-column))
(beg-col (save-excursion (goto-char (match-beginning 0))
(skip-chars-forward "\t")
(current-column))))
(if (= (/ end-col tab-width) (/ beg-col tab-width))
;; The spacing (after some leading TABs which we wouldn't
;; want to touch anyway) does not straddle a TAB boundary,
;; so it neither contains a TAB, nor will we be able to use
;; a TAB here anyway: there's nothing to do.
nil
(delete-region (match-beginning 0) (point))
(indent-to end-col))))))))
1992-05-30 21:11:25 +00:00
1993-11-25 03:59:28 +00:00
(provide 'tabify)
1992-05-30 21:11:25 +00:00
;;; tabify.el ends here