How can I apply a new Emacs C style to reformat all my source files? - emacs

I'd like to re-format all my source files using the Google formatting function for emacs: google-c-style.el (see here).
How can I apply this function to all my source files at once, so that they are all formatted and indented correctly according to the Google style?

There are several pieces to this:
you need to come up with EMACS functions to do all the reformatting you want. indent-region is a start, but you might also want to untabify or some other things.
you need to invoke them on each file, and since the indent functions work on ranges, you need a function that sets mark to cover the whole file: mark-whole-buffer.
you need to invoke EMACS on each file: this means invoking emacs with the --batch file.
There's a couple of nice blog posts on doing this here and here.

I have done this before by using a keyboard defined macro. I would load all of the files into emacs (something like find . -name "*.cpp" | xargs emacs) and then type the following keys. I've annotated each key combination with what it does.
C-x-( 'Begin recording macro
M-< 'Go to start of file
C-space 'Mark current location (now start of file)
M-> 'Go to end of file
M-x indent-region 'Indent entire file according to coding style
C-x C-s 'Save the current buffer
C-x C-k 'Close the current buffer
C-x-) 'End recording macro
Now you can run this on a buffer by typing C-x e. If you have loaded several files you can run something like C-u 100 C-x e to run this on 100 files. If this is more than the number of files, that is ok, you'll just get some "bell ring" or other error you can ignore once all the processing is complete.

I believe that this script does not do reformatting. Instead it's an example of how to build a custom "style" as described in: CC mode manual - Styles
CC-mode manual also says:
If you want to reformat old code, you're probably better off using some other tool instead, e.g. GNU indent, which has more powerful reformatting capabilities than CC Mode.
CC mode manual - Limitations-and-Known-Bugs

If you want to mark the source files in a dired buffer and then run a function to format each you can do something like this:
(defun clean-file(filename)
(your-function-goes-here))
(defun clean-each-dired-marked-file()
(interactive)
(for-each-dired-marked-file 'clean-file))
(defun for-each-dired-marked-file(fn)
"Do stuff for each marked file, only works in dired window"
(interactive)
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files)))
(mapcar fn filenames))
(error (format "Not a Dired buffer \(%s\)" major-mode))))

Related

How do I find org-mode files with completion in Emacs?

I have a growing set of org files stored in org-directory. How can I navigate between them, preferably with interactive filtering and completion?
I thought there was a way to get org-mode to produce a list of known org files for quick navigation, but I can't seem to find it. If org-mode does not have this feature, how can I make a simple command that launches something like helm or icicles to find them?
The question is not very clear to me. But if your Org-mode files all have a certain file-name pattern (e.g. *.org) and all are in the same directory (org-directory) then you can use several methods Emacs method to access them:
C-x C-f *.org RETURN in org-directory opens them all (the buffers are visiting them but only the last one is shown).
C-x C-f *.org TAB in org-directory, to show them using completion, then pick whichever one you want (or pick more than one, using a glob pattern, as in #1).
The same as #2, using Icicles or Helm. In Icicles, at least, you can also match using regexps and in other ways.
Open Dired for just those files: C-x d *.org.
There are really any number of ways to do what you've described. But I'm guessing that you have not really described your request/problem/question well enough, and when you do you will get a narrower set of answers.
UPDATE after your comments:
Here's one way: Open Dired on all of your Org files in and under org-directory.
(defun foo ()
"Open Dired for (only) the Org files in and under `org-directory`."
(interactive)
(cd org-directory)
(dired "*.org" "-lRF"))
Test it with M-x foo. Put this in your init file:
(foo)
And here's another way: M-x bar or bind bar to a key.
(defun bar ()
"Open an Org file in or under `org-directory`."
(interactive)
(let ((default-directory org-directory)
(icicle-file-match-regexp ".*\\.org"))
(icicle-locate-file-of-content)))
I have a package that does just that: plain-org-wiki.
It's nothing too elaborate: I just dump all of my 45 org files into a single directory and get completion for them.
How about org-iswitchb, which is provided by org already?
Switch between Org buffers.
With one prefix argument, restrict available buffers to files.
With two prefix arguments, restrict available buffers to agenda files.
Add this to your startup file after org is loaded:
(global-set-key "\C-cb" 'org-iswitchb)
My favorite solution to this is helm-mode which is activated with the helm package from MELPA. Here's a demo:
It really makes for a great environment for searching ones files quickly. In addition, one can enable fuzzy completion! Here's a minimal configuration (after installing the helm package):
(require 'helm-config)
(helm-mode 1)
(global-set-key (kbd "C-x C-f") 'helm-find-files)
You can also run grep on the files if you'd like to search their content. Take a look at this amazing guide if you'd like to learn more.

How to make emacs stay in the current directory

When I start working on a project in emacs, I use M-x cd to get into the project root directory. But every time I use C-x C-f to open a file in one of the subdirectories (like app/model/Store.rb) emacs changes current directory to that of the file. Is there a way to make emacs stay at the root?
How about this? It replaces the regular find-file command with your own which always starts in some "root" directory (customize the find-file-root-dir variable):
(defvar find-file-root-dir "~/"
"Directory from which to start all find-file's")
(defun find-file-in-root ()
"Make find-file always start at some root directory."
(interactive)
(let ((default-directory find-file-root-dir))
(call-interactively 'find-file)))
(global-set-key (kbd "C-x C-f") 'find-file-in-root)
Assuming that you want the working directory of a file to be set to whatever the working directory was before you executed find-file, you could try the following:
(defmacro disallow-cd-in-function (fun)
"Prevent FUN (or any function that FUN calls) from changing directory."
`(defadvice ,fun (around dissallow-cd activate)
(let ((old-dir default-directory) ; Save old directory
(new-buf ad-do-it)) ; Capture new buffer
;; If FUN returns a buffer, operate in that buffer in addition
;; to current one.
(when (bufferp new-buf)
(set-buffer new-buf)
(setq default-directory old-dir))
;; Set default-directory in the current buffer
(setq default-directory old-dir))))
Armed with this macro, go search for operations that set the variable default-directory: M-x find-library files; M-x occur (setq default-directory. After some investigation, you discover that the desired function is called find-file-noselect-1. Also, it looks like set-visited-file-name is also a candidate. So:
(disallow-cd-in-function find-file-noselect-1)
(disallow-cd-in-function set-visited-file-name)
Note
Note that (disallow-cd-in-function find-file) would work just fine, but then if you switched to ido-mode, you'd be opening files with ido-find-file instead of find-file. Both of these functions ultimately use find-file-noselect-1, so hitting that with the macro is a more univeral solution.
Is there a way to make emacs stay at the root?
No, there isn't. C-x C-f always visits starting from the default directory of the buffer you are already vising. The default directory, by default, is the same directory as the file. You can change these (separately for every buffer) using M-x cd.
But that is not what you want. What you should do is C-x b to *scratch* (whose default directory is the same as where you launched Emacs from -- in your words "root"), and then visit a new file. And if you need to do this frequently, just open up a dired in there and work your way thru.
I appreciate I'm not answering your question directly, but I noticed you were more specific in your requirements in one of your comments: "I don't use compile or recompile, I just tend to close files I am not working on, since it takes fewer keystrokes to open a file again".
Have you got ido turned on for buffer switching? If you exclude the directory thing for a moment, switching files or buffers with ido is an identical number of keystrokes (C-x C-f vs C-x b, followed by a few characters in the file name). If you include the directory thing, switching files is more tricky for the precisely the reasons you mention. Sticking with buffers is much easier.
Going a step further, with the help of 'anything.el' it's quite easy to abstract away whether a given file is in a buffer or in a file using the file cache. For example, if you do the following:
(file-cache-add-directory-recursively "/my/ruby/project") ".*\\.rb$")
and run 'anything-for-files' (I have it bound to C-x f) all your open buffers are listed, along with all of the files you've just added to the file cache; isolating a given file usually only takes one or two more characters.
Any file in your project is thus 4 or 5 key presses away, and the directory they are in or whether or not they are in a buffer becomes irrelevant.
Hope that's helpful...
Sorry I haven't worked out the details, but you might be able to add a function to find-file-hook that resets the default directory to whatever you want.

Don't show uninteresting files in Emacs completion window

How do I prevent Emacs from showing me all the files I'm not interested in (such as ~ backup files, .pyc files, or .orig files) when I: C-x C-f TAB ?
It is working in one respect: if I know the file I want to open begins with foo and I type foo TAB then the mini-buffer correctly autocompletes all the way to foo.py. It correctly ignored foo~ and foo.pyc, because both ~ and .pyc are in completion-ignored-extensions. It also correctly lets me open either ignored file if I really want to by typing in all the letters my self.
However, if I just hit TAB to to bring up the completion list buffer then that list includes files with extensions in completion-ignored-extensions, which makes it very difficult to find what I'm looking for.
Clearly the code to ignore uninteresting files is there and working. How do I get the completion list buffer to respect completion-ignored-extensions?
(by-the-by, can I make dired behave similarly?)
This piece of advice filters out files with extensions listed in 'completion-ignored-extensions:
(defadvice completion--file-name-table (after
ignoring-backups-f-n-completion
activate)
"Filter out results when they match `completion-ignored-extensions'."
(let ((res ad-return-value))
(if (and (listp res)
(stringp (car res))
(cdr res)) ; length > 1, don't ignore sole match
(setq ad-return-value
(completion-pcm--filename-try-filter res)))))
Note: This doesn't affect dired.
For the dired issue, add this to your .emacs
(eval-after-load "dired"
'(require 'dired-x))
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)))
Read the documentation for dired-x to get an idea of what's available there.
I would recommend using ido-mode to ignore files; it comes with Emacs by default and adds many other useful enhancements that you'll quickly learn to love. The Ignorance is Bliss section from this Mastering Emacs blog post covers how to ignore files, directories, and buffers:
ido-ignore-buffers Takes a list of buffers to ignore in C-x b
ido-ignore-directories Takes a list of directories to ignore in C-x d and C-x C-f
ido-ignore-files Takes a list of files to ignore in C-x C-f
Icicles does what you expect by default. It always respects completion-ignored-extensions for file-name completion, including for buffer *Completions*. And you can toggle this ignoring on/off at anytime, by hitting C-. in the minibuffer.
In addition, if you use library completion-ignored-build.el by Kevin Ryde, then Icicles automatically takes advantage of that library's dynamic adjustment of ignored file extensions. (Just load completion-ignored-build.el -- do not enable its minor mode or advice.)
I don't know of an answer for completion, I'm afraid. I think this is by design - when you know the name you're looking for, you probably don't want e.g. the backup file. But when you don't know, it's probably better to have a list of all of the files.
However, for dired, you can load the 'dired-x' package on startup (in your .emacs), and this provides dired-omit-mode:
(load "dired-x")
You can use 'M-x customize-variable<RET>dired-omit-files' to set the actual patterns to ignore. Then when you are in dired mode you can use M-O (the letter, not the number) to toggle 'omission' on and off.

Auto-formatting a source file in emacs

How do I apply a set of formatting rules to an existing source file in emacs?
Specifically I have an assembly (*.s) file, but I would like a generic command for all types of files.
I am trying to use M-x c-set-style with gnu style, but I am getting an error:
Buffer *.s is not a CC Mode buffer (c-set-style)
Open the file and then indent it by indenting the entire region:
M-x find-file /path/to/file RET
C-x h (M-x mark-whole-buffer)
C-M-\ (M-x indent-region)
Now, it looks like you're trying to apply C indentation to a buffer that's not in C mode. To get it into C mode
M-x c-mode
Or c++-mode, or whatever mode you want. But, since it's assembler code, you probably want assembler mode (which Emacs will do by default for .s files). In which case, the indentation command above (C-M-\ is also known as M-x indent-region) should work for you.
Note: the command sequence at the top can be rolled into a single command like this:
(defun indent-file (file)
"prompt for a file and indent it according to its major mode"
(interactive "fWhich file do you want to indent: ")
(find-file file)
;; uncomment the next line to force the buffer into a c-mode
;; (c-mode)
(indent-region (point-min) (point-max)))
And, if you want to learn how to associate major-modes with files based on extensions, check out the documentation for auto-mode-alist. To be fair, it's not necessarily extension based, just regular expressions matched against the filename.
Try M-x asm-mode. That will switch to assembler mode. Not sure how it will go with assembler embedded in the middle of a C file.
if you want indent current buffer
(defun iwb ()
"indent whole buffer"
(interactive)
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
emacs will use the file name extension to identify the mode, you should add some assemble language mode style in your custom.el file
The major mode it's using for your .s files won't be cc-mode hence c-set-style makes no sense. However you can always manually enter cc-mode (M-x cc-mode) and then do the c-set-style you want. However as the C styles are keyed for C source code and not assembler this is almost certainly not what you want to do.
if you want to indent from the command line use :
emacs --batch <filenames.v> -f verilog-batch-indent

Using Emacs, how could I indent/format a code segment in a TXT file?

I am writing a document with Emacs. As you know, there are some code segments in the text file I am working with. Typically, when I open this file, emacs will get into text-mode automatically. And it works fine for me to edit the ordinary paragraphs. But for those code segments, how could I indent them into gnu or linux style just like what I could do in c-mode (by c-set-style && do Ctrl-Alt-\ in certain region)?
BTW, actually, I could turn the buffer into c-mode by invoking M-x c-mode to do this, however, I think there should be much a graceful way to do this in text-mode.
orgmode manages to do it by copying the code out to a temporary buffer where you edit & format it, and updating the changed text when you're done.
If switching to orgmode is an option, then you do it like this:
#+BEGIN_SRC emacs-lisp
(defun org-xor (a b)
"Exclusive or."
(if a (not b) b))
#+END_SRC
and start and finish editing with C-c '.
Edit: Emacswiki has a list of multiple modes.
You might be able to mark the region, then narrow the view to the region, change the mode, indent, return to text-mode, and return to the full buffer again. I forget the exact shortcuts at the moment, but it should be fairly easy to turn into a function.