I'm looking for a way to tell auto-insert-mode to use different templates regarding on the file path (not only on the file extension).
I want all .org files created under a directory */meetings to have a defined template.
The following setting does not work :
(auto-insert-mode)
(setq auto-insert-directory "~/org/template/") ;;; *NOTE* Trailing slash important
(setq auto-insert-query nil) ;;; If you don't want to be prompted before insertion
(define-auto-insert "\/meetings/\.org" ".meetings_template.org")
(define-auto-insert "\.org" ".template.org")
Do you have an idea ?
The names are matched with a regular expression. The one you gave for meetings is not doing what you want. Try this one instead: "/meeting/.*\\.org"
Related
In Emacs, I am putting on the menu an item to load the init.el file, since I am in there almost daily.
menu code is working fine, but the file isn't loading.
So in a buffer, for troubleshooting, I enter:
(load user-init-file)
and use C-x C-e to execute it.
Turns out if fails because it needs double backslashes in the path.
user-init-file resolves to "c:\steve\emacs\init.el"
But should be `
"c:\\steve\\emacs\\init.el"
is there a function already to convert to the double backslashes?
Or how do I do that with a search/replace?
This is similar to other search questions I found except this is for replacing within a string instead of within a buffer.
I think you probably just want (load-file user-init-file). load-file does not use load-path, and it does not try to append .elc or .el.
(If you use MS Windows notation for a file name then you can see what Emacs really thinks the file name is, by calling file-truename on it.)
If you really want to use load, try (load user-init-file nil nil t).
load tries to expand its FILE arg, automatically adding .elc and .el. The 4th argument is NOSUFFIX, which if non-nil prevents that behavior.
C-h f load:
**load-** is a built-in function inC source code`.
(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
Execute a file of Lisp code named FILE.
First try FILE with .elc appended, then try with .el,
then try FILE unmodified (the exact suffixes in the exact order are
determined by load-suffixes). Environment variable references in
FILE are replaced with their values by calling substitute-in-file-name.
This function searches the directories in load-path.
If optional second arg NOERROR is non-nil,
report no error if FILE doesn't exist.
Print messages at start and end of loading unless
optional third arg NOMESSAGE is non-nil (but force-load-messages
overrides that).
If optional fourth arg NOSUFFIX is non-nil, don't try adding
suffixes .elc or .el to the specified name FILE.
If optional fifth arg MUST-SUFFIX is non-nil, insist on
the suffix .elc or .el; don't accept just FILE unless
it ends in one of those suffixes or includes a directory name.
If NOSUFFIX is nil, then if a file could not be found, try looking for
a different representation of the file by adding non-empty suffixes to
its name, before trying another file. Emacs uses this feature to find
compressed versions of files when Auto Compression mode is enabled.
If NOSUFFIX is non-nil, disable this feature.
The suffixes that this function tries out, when NOSUFFIX is nil, are
given by the return value of get-load-suffixes and the values listed
in load-file-rep-suffixes. If MUST-SUFFIX is non-nil, only the
return value of get-load-suffixes is used, i.e. the file name is
required to have a non-empty suffix.
When searching suffixes, this function normally stops at the first
one that exists. If the option load-prefer-newer is non-nil,
however, it tries all suffixes, and uses whichever file is the newest.
Loading a file records its definitions, and its provide and
require calls, in an element of load-history whose
car is the file name loaded. See load-history.
While the file is in the process of being loaded, the variable
load-in-progress is non-nil and the variable load-file-name
is bound to the file's name.
Return t if the file exists and loads successfully.
I have a folder /var/~/. In config .emacs I wanna load some files from this folder.
I try to use (load-file "/var/~/foobar.el"), but emacs alerts File error: Cannot open load file, ~/foobar.el.
Furthermore I couldn't even open the files under this folder with c-x c-f. In minibuffer the path will auto be redirected to my home.
How could I load files in that folder?
You need to rename your directory.
load-file is a simple wrapper around load, which passes the given file name through substitute-in-file-name. From the docstring of substitute-in-file-name (emphasis mine):
Substitute environment variables referred to in FILENAME. `$FOO' where FOO is an environment variable name means to substitute
the value of that variable. The variable name should be terminated
with a character not a letter, digit or underscore; otherwise, enclose
the entire variable name in braces.
If `/~' appears, all of FILENAME through that `/' is discarded. If `//' appears, everything up to and including the first of those `/' is discarded.
In other words, substitute-in-file-name throws away everything before /~, turning /var/~/foo.el into ~/foo.el.
I completely fail to see any reason in this behaviour, but it is what it is, and you cannot (easily) work around it, so renaming is your best way out of this dilemma.
It's a reasonable thing to do, anyway. Using ~ as directory name is bad idea on Unix systems generally, not just for Emacs alone.
lunaryorn explained your problem well, and I agree with his suggestion that not using ~ in file paths is the best solution. However, If you can't rename these paths for whatever reason, I believe you can work around substitute-in-file-name by loading a relative file path as documented here.
Basically, you need to add nil to your load-path variable, then set your default-directory variable to the troublesome path, finally then load the file using a relative name. e.g.:
; adding nil causes load to also search your 'default-directory'
(setq load-path (append '(nil) load-path))
(setq default-directory "/tmp/~/")
(load "foobar.el")
Note that if you suspect the file name might exist (be loaded from) elsewhere in your load-path you would need to ensure the file you want is first in the load-path.
See How programs do loading.
The command ido-find-file offers the option to restrict the completion list using a filter. For example, one may input .cpp, press C-SPC et voila, the completion list is restricted to files with .cpp.
However, sometimes it is useful to restrict the list to directories in the first steps of navigation, e.g. when one is navigating to a file, residing in a subdirectory of unknown name, which is located within a directory with a lot of ordinary files.
Is there some built-in functionality for restricting ido to accept only directories in the current step (current step is either delimited by a) calling an un-restrict function, or b) [also the better solution] by an actual naviagtional command, i.e. descending/ascending in the directory tree level)? Or, if not, can someone write a function to do just that?
This functionality is already present in form of ido-dired.
UPD:
Two-stage find-file is as easy as you could imagine:
(defun find-file-2 ()
(interactive)
(ido-dired)
(ido-find-file))
I have files in some location:
location_a/doc.tex
location_a/doc.cls
...
I want to work on them in another directory via symbolic links:
work_directory/doc.tex -> location_a/doc.tex
work_directory/doc.cls -> location_a/doc.cls
work_directory/doc.pdf
work_directory/doc.log
...
However, when I run emacs doc.tex in the work directory and do some editing, emacs creates a backup file at location_a/doc.tex~. I want the backup file to be stored in the work directory, though. I don't want any new files created in location_a.
How can I make emacs do that?
This is trickier than it seems it should be because backup-buffer insists on chasing the links of the buffer file name before calling any backup file name construction machinery, such as make-backup-file-name-function. The result is that Emacs allows no way to customize this behavior, short of redefining backup-buffer, which is a fairly complicated piece of code.
A compromise solution I came up with is to install an "advice" around backup-buffer that temporarily disables file-chase-links while backup-buffer is being evaluated. This allows the backup file to be in the directory where the symlink resides. However, it also causes Emacs to create the backup by renaming the original symlink, leaving one with work_directory/doc.tex~ being a symlink that points to location_a/doc.tex! Fortunately, this is easy to prevent by setting backup-by-copying to t.
Here is the code. A word of warning: while I have tried it to verify that it works, I cannot guarantee that it will not have an undesirable side effect, like the above interference with the backup mechanism that required backup-by-copying. However, it might also work just fine - just be careful when using it.
(require 'cl) ; for flet
(defadvice backup-buffer (around disable-chase-links)
(flet ((file-chase-links (file) file))
ad-do-it))
(ad-activate 'backup-buffer)
For the fun of it, let me describe a radically different approach, based on directory variables.
In short, you would put in your work-directory/ a file named .dir-locals.el containing:
((nil . ((eval . (set (make-local-variable 'backup-directory-alist)
(list (cons "."
(file-relative-name
(file-name-directory (buffer-file-name))
(file-name-directory (file-truename
(buffer-file-name)))))))))))
What this does is abusing somewhat the backup-directory-alist, and install a local version
of it for all your files in work-directory/. That local version will in turn make sure that any backup file is kept within work-directory.
In order to achieve that, we need 2 things:
have something like '(("." . "path/to/work-directory/")) as the local value
make sure this path is relative to location_a/
The reason for the second point is that as noted elsewhere, the starting point of backup-buffer is indeed the location of the actual file, once symlinks are resolved. And we can't simply put the absolute path without having backup files changing shape (in case of absolute path for the backup directory, the backup filenames encode the complete path, so that there is no collision)
Notes:
you'll need to make sure that specific local variable is recorded in the safe-local-variable-values. Since it's a generic form, it's a one time job though (just hit "!" the first time you're asked about it)
this assumes find-file-visit-truename is set to nil, but I guess you wouldn't ask that question if that was not the case :)
Pros of the approach:
no need for advice (which is always a good thing)
reasonably portable although it assumes your Emacs supports directory variables
you keep the flexibility to put that in place only where you need it
Cons of the approach:
well, obviously you might have to copy that .dir-locals.el in several places
Also note that if you wanted a one-shot approach, you could make it much simpler, such as:
((nil . ((backup-directory-alist (("." . "../path/to/work-directory"))))))
where you actually compute the relative name yourself, once and for all.
I'm doing completing-read with a list of filesystem paths, eg. ("~/parent/child" "/parent/child").
I would like to mask the output of completing-read/ido-completing-read so that it would show only child, but would still return the compelete path.
Is this possible at all?
Best regards, Rat
Answer: no, it is not possible, because completing-read does not keep into account any semantics of the strings.
This is possible, but with multiple calls of completing-list.
The idea is that this function is passed a list of strings, and it helps you chose a string from that list.
Completing-list has no control over what the strings contain. It does not care about the semantics of strings, as paths. It sees only list of strings.
So, in order to make how you want, you call it once with the list of files from the current directory, and if you select a directory, first you change tto that directory, and then you call it once again with the list of files from that directory, etc.
If you want to keep pwd the same as initial, then when it returns , you change again to the original dir.
(cd (concat default-directory
(completing-read "> " (directory-files default-directory ))))
The package uniquify does something similar to what you want. When you have two buffers visiting files with the same filename it will add a string at then end to differentiate the two.
For example if you have files /etc/foo/bar/a and /mnt/a it will call the buffers something like a <bar> and a <mnt>. Then when you use ido to switch buffers you do not have to select from the full filename but you end up in the right buffer.