2

I am working on a Linux driver, and I would like to use emacs. My problem is that indenting does not work, no matter what. by this, I mean that when I press enter from a line, it goes at the beginning of the new line, and pressing TAB even a million times does not change this behavior. I've spent the last hour trying to fix this by changing my .emacs, but absolutely no results. Here's a copy of my .emacs:

(load-theme 'misterioso)

;; Disable toolbar and menubar
(tool-bar-mode -1)
(scroll-bar-mode -1)
(menu-bar-mode -1)

;; Remember position in opened files
(require 'saveplace)
(setq-default save-place t)

;; Close all buffers
(defun close-all-buffers ()
  (interactive)
  (mapc 'kill-buffer (buffer-list)))

;; Remove trailing whitespaces before saving
(add-hook 'before-save-hook 'delete-trailing-whitespace)

;; Display line and column number in toolbar
(setq line-number-mode t)
(setq column-number-mode t)

;; Show matching parentheses
(show-paren-mode t)

;; Goto-line command
(global-set-key "\C-cg" 'goto-line)

;; Kill rectangular regions
(global-set-key "\C-cr" 'kill-rectangle)

;; Comment region command
(global-set-key "\C-cc" 'comment-region)
(global-set-key "\C-cv" 'uncomment-region)

;; Display question in 'y/n' instead of 'yes/no'
(fset 'yes-or-no-p 'y-or-n-p)

(setq load-path (cons (expand-file-name "~/.emacs.d/") load-path))

;; Color the code
(require 'font-lock)
(global-font-lock-mode t)

;; Max decoration
(setq font-lock-maximum-decoration t)

;; Delete selected text when typing
(delete-selection-mode 1)

;; Highlight current line
(hl-line-mode t)

;; Put a final newline in saved files
(setq require-final-newline t)

;; LATEX!
(setq ispell-program-name "aspell")
; could be ispell as well, depending on your preferences
(setq ispell-dictionary "english")
; this can obviously be set to any language your spell-checking program     supports
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-buffer)
(require 'flymake)

;; Set backup directory
(setq
 backup-by-copying t
 backup-directory-alist
 '(("." . "~/.emacs-backups"))
 delete-old-versions t
 kept-new-versions 6
 kept-old-versions 2
 version-control t)

(defun count-words-region (start end)
  (interactive "r")
  (save-excursion
    (let ((n 0))
      (goto-char start)
      (while (< (point) end)
        (if (forward-word 1)
            (setq n (1+ n))))
      (message "Region has %d words" n)
      n)))

(setq-default indent-tabs-mode t)

;; Tab width
(setq default-tab-width 8)
(custom-set-variables
 '(inhibit-startup-screen t))

(require 'whitespace)
(setq whitespace-line-column 80) ;; limit line length
(setq whitespace-style '(face lines-tail))

(add-hook 'prog-mode-hook 'whitespace-mode)

(defun c-lineup-arglist-tabs-only (ignored)
   "Line up argument lists by tabs, not spaces"
   (let* ((anchor (c-langelem-pos c-syntactic-element))
     (column (c-langelem-2nd-pos c-syntactic-element))
     (offset (- (1+ column) anchor))
     (steps (floor offset c-basic-offset)))
     (* (max steps 1)
        c-basic-offset)))

 (add-hook 'c-mode-common-hook
           (lambda ()
             ;; Add kernel style
             (c-add-style
              "linux-tabs-only"
              '("linux" (c-offsets-alist
                         (arglist-cont-nonempty
                          c-lineup-gcc-asm-reg
                          c-lineup-arglist-tabs-only))))))

(setq-default major-mode 'text-mode)
     (add-hook 'text-mode-hook 'turn-on-auto-fill)

(electric-indent-mode 1)
(dolist (command '(yank yank-pop))
   (eval `(defadvice ,command (after indent-region activate)
            (and (not current-prefix-arg)
                 (member major-mode '(emacs-lisp-mode lisp-mode
                                                      clojure-mode scheme-mode
                                                      haskell-mode    ruby-mode
                                                      rspec-mode          python-mode
                                                      c-mode          c++-mode
                                                      objc-mode       latex-mode
                                                      plain-tex-mode))
                 (let ((mark-even-if-inactive transient-mark-mode))
                   (indent-region (region-beginning) (region-end) nil))))))

Can anyone help me please? I am starting to get desperate.... :(

Thanks a lot in advance!!

  • I just tested your init file out and it's indenting properly (meaning it will add a level if I'm in a function) but with two spaces, not a tab. – nanny Jan 21 '15 at 14:42
1

You're missing the variable c-basic-offset. You need to set this to 8 if you want your indentation levels to be tabs. Add

(setq c-basic-offset 8)

somewhere to your init file. Perhaps next to indent-tabs-mode, or as the first expression in the lambda of your c-mode-common-hook.

If you still can't get indents, I would try removing this line from your init file:

(setq load-path (cons (expand-file-name "~/.emacs.d/") load-path))

then restart Emacs.

  • It (the (setq command)) works perfectly, thanks a lot!!!!! :) – RH6 Jan 22 '15 at 6:29
  • @RH6 You're welcome! Feel free to mark my answer as accepted so that people know the question is answered (and I get those sweet internet points). – nanny Jan 22 '15 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.