emacs: open all .txt files in a specific directory in a specific major mode - emacs

EDIT: It turns out that the second edit to my .emacs file actually works. (See the comments below this entry.)
I tried a couple of addition to the .emacs to make all txt files opened in emacs use orgmode. They did not work. How can I make it happen?
;;SET EMACS AS DEFAULT MAJOR MODE TO FOR ALL FILES WITH AN UNSPECIFIED MODE
(setq default-major-mode 'org-mode)
;;OPEN ALL TXT FILES IN ORGMODE
(add-to-list 'auto-mode-alist '("\\.txt$" . org-mode))
Additionally:
It would be even better to open only txt files in a certain directory orgmode. Any hint as to how that could be done would also be appreciated.

Another way to do this is using directory-local variables. This is nice because you can put a file in any directory where you want this behavior to engage, and it works recursively in any subdirectories.
Create a file called .dir-locals.el in the desired directory.
Here are the contents:
((nil (eval . (if (string-match ".txt$" (buffer-file-name))(org-mode)))))
Read this like so: for any major-mode (nil), evaluate the following form:
(if .... (org-mode))

The regex in auto-mode-alist could be something more complex, like "^/path/to/.*\\.txt$"

You can implement a hook which verifies the file directory and modifies the buffer mode:
(add-hook 'find-file-hooks
(lambda ()
(let ((file (buffer-file-name)))
(when (and file (equal (file-name-directory file) "c:/temp/"))
(org-mode)))))
As an alternative you can add the mode line in the beginning of your text file. In this case emacs will set the specified mode.
; -*- mode: org;-*-
* header 1
** header 2

I glued together some code from Oleg Pavliv's answer here, and from yibe's at elisp - File extension hook in Emacs - Stack Overflow
(defun use-org-mode-for-dot-txt-files-in-owncloud ()
(when (and (string-match owncloud buffer-file-name)
(string-match "\\.txt\\'" buffer-file-name))
(org-mode)))
(add-hook 'find-file-hook 'use-org-mode-for-dot-txt-files-in-owncloud)
This way, though ownCloud Web and phone apps are currently friendly only with .txt files, from my PC I can use Emacs' Org-mode for them.
(If I set all .txt files to use Org-mode, it breaks todotxt-mode.)
(Note that owncloud is a string variable equal to my ownCloud path.)

Related

Running a .el file when opening emacs

I downloaded two .el files
One is to highlight current column where cursor is and another one to highlight some specific words.
I followed next steps in the file:
(add-to-list 'load-path "~/.xemacs/packages/") //path where I saved .el files
(load "column-marker") //name of file without .el extension
To this step it works fine, I find those when I press M-x column-marker and I can use it.
My problem begins when I want to use it everytime i open emacs and I found that I could use something similar to this:
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
Which in my case I am typing:
(add-to-list 'auto-mode-alist '("\\.\\'" . column-marker)) //I want to enable it for all .something files
But... when doing that, there is an error when opening emacs and it starts with default setup.
What am I doing wrong? I have tried many ways and none work.
Thanks in advance
Always post the error message you see. That helps others help you.
C-h f auto-mode-alist tells you that the functions you use in it must implement major modes. column-marker is not a major-mode function. It is not even a function; it is a file.
What you need to do, for each mode where you want some function in file column-marker.el to be invoked, is to put that function on the major-mode hook for that function. For example:
(add-hook 'emacs-lisp-mode (lambda () (interactive) (column-marker-1 80))
And lo and behold, what does the Commentary in file column-marker.el tell you?
;; Installation: Place this file on your load path, and put this in
;; your init file (`.emacs'):
;;
;; (require 'column-marker)
;;
;; Other init file suggestions (examples):
;;
;; ;; Highlight column 80 in foo mode.
;; (add-hook 'foo-mode-hook (lambda () (interactive) (column-marker-1 80)))
Couldn't be clearer. Provided you actually read it.
Seriously, a minimum of investigation is in order, before you post a question to StackOverflow. You should do a Google search, open README files and read them, and so on --- first. And in the case of Emacs questions, IMHO, you should ask Emacs first (e.g. C-h v auto-mode-alist).
According to the rules of S.O. posting, not doing preliminary simple research is even grounds for closing a question. So do not be surprised if this question gets closed or downvoted.

Load and evaluate file from .emacs

I work on many different machines and VMs, each of which might have their own needs regarding options for .emacs. I have a Git repository / subdirectory .common-system with a second Emacs Lisp file .common-system/emacs containing common definitions used for all my systems.
What changes can I make to .emacs which will load and eval my secondary definition file automagically? Bonus points for keeping the secondary file out of my buffer list after startup.
Have you tried:
(load-file ".common-system/emacs")
It works better (as Stefan suggests) if you specify the full path to the file, something like
(load-file "~/.common-system/emacs")
If you stick with you current repository layout, using load-file (as suggested by others) is the way to go.
However, if you name your top-level directory .emacs.d and place the file init.el in it, Emacs will find it and run it automatically. That way, you don't have to hand-edit a .emacs file on each system you use.
You use a (cond), dispatching on variables such as user-login-name, system-type and system-name
(cond
((equal user-login-name
"myname")
;; Stuff
)
((equal system-type 'gnu/linux)
(require 'some-file)
;; some-file, could be any file in load-path, named some-file.el, and ending
;; with (provide 'some-file)
))

How to make all org-files under a folder added in agenda-list automatically?

I am using org-mode to write notes and org-agenda to organize all notes, especially to search some info. by keyword or tag.
C-c a m can search some files by tag inputed, C-c a s by keyword ,those functions from org-agenda are well to utilize, however, I need to add org-file into the agenda-list by hand.
I added some codes into .emacs, such as
(setq org-agenda-files (list "path/folder/*.org"))
or
(setq org-agenda-files (file-expand-wildcards "path/folder/*.org"))
but, both failed to add files under the folder specified into agenda-list automatically, so I can't search keyword or tag among those org-files, unless that I open a org-file and type C-c [ to add it into agenda-list.
How can I make all org-files under a folder automatically added in agenda?
Just naming the directory should be enough. For example this works for me very well:
(setq org-agenda-files '("~/org"))
Also take a look at org-agenda-text-search-extra-files; it lets you
add extra files included only in text searches. A typical value might
be,
(setq org-agenda-text-search-extra-files
'(agenda-archives
"~/org/subdir/textfile1.txt"
"~/org/subdir/textfile1.txt"))
Caveat: If you add a file to the directory after you have started
Emacs, it will not be included.
Edit: (2018) To include all files with a certain extension in the extra files list you can try the following function I wrote sometime back (a more recent version might be available here).
;; recursively find .org files in provided directory
;; modified from an Emacs Lisp Intro example
(defun sa-find-org-file-recursively (&optional directory filext)
"Return .org and .org_archive files recursively from DIRECTORY.
If FILEXT is provided, return files with extension FILEXT instead."
(interactive "DDirectory: ")
(let* (org-file-list
(case-fold-search t) ; filesystems are case sensitive
(file-name-regex "^[^.#].*") ; exclude dot, autosave, and backupfiles
(filext (or filext "org$\\\|org_archive"))
(fileregex (format "%s\\.\\(%s$\\)" file-name-regex filext))
(cur-dir-list (directory-files directory t file-name-regex)))
;; loop over directory listing
(dolist (file-or-dir cur-dir-list org-file-list) ; returns org-file-list
(cond
((file-regular-p file-or-dir) ; regular files
(if (string-match fileregex file-or-dir) ; org files
(add-to-list 'org-file-list file-or-dir)))
((file-directory-p file-or-dir)
(dolist (org-file (sa-find-org-file-recursively file-or-dir filext)
org-file-list) ; add files found to result
(add-to-list 'org-file-list org-file)))))))
You can use it like this:
(setq org-agenda-text-search-extra-files
(append (sa-find-org-file-recursively "~/org/dir1/" "txt")
(sa-find-org-file-recursively "~/org/dir2/" "tex")))
Edit: (2019) As mentioned in the answer by #mingwei-zhang and the comment by #xiaobing, find-lisp-find-files from find-lisp and directory-files-recursively also provides this functionality. However, please note in these cases the file name argument is a (greedy) regex. So something like (directory-files-recursively "~/my-dir" "org") will give you all Org files including backup files (*.org~). To include only *.org files, you may use (directory-files-recursively "~/my-dir" "org$").
There is a simpler way of doing recursive search of org files (courtesy #xiaobing):
(setq org-agenda-files (directory-files-recursively "~/org/" "\\.org$"))
EDIT: You can also filter out certain directory from lookup by adding a array filter. Example, filtering out all org files in xxxx/xxx/daily/ directory:
(setq org-agenda-files
(seq-filter (lambda(x) (not (string-match "/daily/"(file-name-directory x))))
(directory-files-recursively "~/Notes/roam" "\\.org$")
))
For Emacs <25, you can use find-lisp-find-files:
(load-library "find-lisp")
(setq org-agenda-files
(find-lisp-find-files "FOLDERNAME" "\.org$"))

Emacs: Set tab indent for just one file on the fly

I work on an open source project where the creator sets his tab-indents to 2 spaces.
I'd like to just enable it on the fly for the one file I work on and not other files of the same type. There must be something like M-x set-tab-indent. It is a JavaScript file ending in .js.
I know I can use:
(setq-default tab-width int)
inside my .emacs file, but I rather just call an M-x command to set it and forget it during my duration of working on this file. I tried M-x apropos and Google but couldn't find the specific command.
Thanks.
You can make the variable js-indent-level local to the buffer using:
M-x make-variable-buffer-local <RET> js-indent-level <RET>
Then you can set that variable in the buffer using:
M-x set-variable <RET> js-indent-level <RET> 2
The easiest way to do this for a single buffer is to use M-x set-variable.
Type M-x set-variable and press enter
When prompted for the variable to set, set tab-width then press enter
You'll be prompted with the line Set tab-width (buffer-local) to value:.
Put the value you want, then hit enter
The buffer should instantly be updated with the new value.
You could also use file local variables to automate omrib's solution for that one file, by adding this to it:
// Local Variables:
// js-indent-level: 2
// indent-tabs-mode: nil
// End:
Create a file ".dir-locals.el" in the project's directory and fill it like this:
((nil . ((tab-width . 2))))
This will take care of setting tab-width automatically and you don't have to modify the actual file (which is likely version-controlled.)
See the manual for more information about the format. I believe this requires Emacs 23.
As indicated by others, one issue with the File Local Variables approach is that you need to modify the file, and that's not ideal if you need to keep those declarations out of version control.
If you want the variables to apply to all files under a given directory, then Directory Local Variables is obviously the way to go, and you can implement that with either a .dir-locals.el file, or by calling (dir-locals-set-directory-class):
http://www.emacswiki.org/emacs/DirectoryVariables
http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
I prefer the directory class approach myself, and I was thinking that it's a shame that there isn't an analogous approach for file local variables, but I found that the directory class code actually works perfectly with files, and the only issue is that dir-locals-set-directory-class calls file-name-as-directory on its argument, which prevents it from being matched, due to the trailing slash.
The following therefore is a way to configure directory local variables for a single file, without modifying the file itself, or affecting other files under the same parent directory.
(defun my-file-locals-set-directory-class (file class &optional mtime)
"Enable 'directory local' classes for individual files,
by allowing non-directories in `dir-locals-directory-cache'.
Adapted from `dir-locals-set-directory-class'."
(setq file (expand-file-name file))
(unless (assq class dir-locals-class-alist)
(error "No such class `%s'" (symbol-name class)))
(push (list file class mtime) dir-locals-directory-cache))
(dir-locals-set-class-variables
'my-javascript-class
'((nil . ((js-indent-level . 2)
(indent-tabs-mode . nil)))))
(my-file-locals-set-directory-class
"path/to/the/file.js" 'my-javascript-class)
I use a snippet of code in my init.el that tries to auto-detect files that use 2-space indents, and switch Emacs's indentation for that file to 2 spaces when it sees such files:
(add-hook 'js-mode-hook
(lambda ()
(when (string-match-p "^ [A-Za-z]" (buffer-string))
(make-variable-buffer-local 'js-indent-level)
(set-variable 'js-indent-level 2))))

How can I hide the backup files that emacs creates?

I just started using emacs after having used vi for a long time. :)
One thing which is annoying me is that whenever I modify a file, save it and exit emacs, I see a backup file created in the same directory named filename~ (if the file I edited was filename).
Is there any way I can get rid of this? Or hide these files? It is very annoying to see tons of backup files when I do ls of the directory.
You can either move them to their own folder with the following code:
;; Don't clutter up directories with files~
(setq backup-directory-alist `(("." . ,(expand-file-name
(concat dotfiles-dir "backups")))))
;; Don't clutter with #files either
(setq auto-save-file-name-transforms
`((".*" ,(expand-file-name (concat dotfiles-dir "backups")))))
Or you can remove them completely, like so:
(setq make-backup-files nil)
(setq auto-save-default nil)
Personally I would be wary of removing them as they can come in useful. Further discussion is here:
http://www.emacswiki.org/emacs/BackupDirectory
http://www.emacswiki.org/emacs/AutoSave
I would recommend checking out the emacs-starter-kit it sorts out a load of issues that people have when coming to emacs, and is pretty heavily used.
http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-misc.el
Update:
There seems to be much confusion over how to use the functions. I'm going to have a little play around later but here is some more information. Note that auto-save-file-name-transforms:
lets you specify a series of regular expressions and replacements to transform the auto save file name
[emacs-manual]
so it's not just as simple as adding in a folder name. That said it seems from a quick google search the following might just do what you all want:
;;; backup/autosave
(defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
(defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
(setq backup-directory-alist (list (cons ".*" backup-dir)))
(setq auto-save-list-file-prefix autosave-dir)
(setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))
http://www.google.com/codesearch?hl=en&lr=&q=auto-save-file-name-transforms&sbtn=Search
The following lines in ~/.emacs will put all of the auto-save and backup files in /tmp:
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
In your .emacs:
(setq make-backup-files nil)
Edit:
If you're unfamiliar with the .emacs file, it's a file named .emacs that resides in your user $HOME directory. If you don't have one already, you can just create it and emacs will load it on startup.
Here is a link to the same question answered on SuperUser and my response. And a StackOverflow question entitled Emacs: Don’t create #these# files when not saving modified buffer
And for completeness, as stated by others; to stop the backup files being created put this in your .emacs
(setq make-backup-files nil)