emacs lisp listing files with glob expansion - lisp

Are there any library or function that performs a bash-like glob expansion for emacs lisp?
For example:
(directory-files-glob "~/Desktop/*")
> ("/home/user/Desktop/file1" "/home/user/Desktop/file2")
If there isn't such a function are there any hint/suggestion on how to implement it?
EDIT:
I've found in the docs also an useful function that does quite exactly this:
file-expand-wildcards:
This function expands the wildcard pattern pattern, returning a list of file names that match it.

Not sure why this was overlooked maybe it was not in emacs in 2010 but in a current emacs at least there is function file-expand-wildcards
(file-expand-wildcards "~/Desktop/*")
which does exactly what you want to do..

Check out the documentation for directory-files:
(directory-files "~/Desktop" nil ".")
Note: The third argument is a regular expression - not globbing.
It is straight forward to turn globbing patterns into regular expressions. eshell comes with a translation package which you can use:
(require 'em-glob)
(defun directory-files-glob (path)
(directory-files (file-name-directory path)
nil
(eshell-glob-regexp (file-name-nondirectory path))))
And, if you want full exposure to eshell's globbing (with directories), there's probably a way to get that. The above assumes that the globbing part is in the non-directory portion of the path.

Package f adds a huge amount of file and filepath manipulation functions under a consistent naming scheme. It has a function f-glob that does exactly that:
(f-glob "~/doc/*.org") ; returns a list of files ending with ".org"
(f-glob "*.org" "~/doc/") ; they all behave the same
(f-glob "*.org" "~/doc")

Related

using wildcards in expand-file-name in elisp?

Is it possible to use wildcards such as * in elisp to expand filenames?
For example, I saw that
(expand-file-name "~/.emacs.d/elpa/babel-repl-20160504.1501")
expands to /home/username/.emacs.d/elpa/babel-repl-20160504.1501.
Is there a function to also expand filenames containing wildcards so that we can do:
(expand-file-name "~/.emacs.d/elpa/babel-repl*")
(taking the first filename if multiple matches exist)?
I tried the above, but expand-file-name does not seem to recognize * (Emacs 24.5 from Ubuntu 16.04)
-- EDIT --
#Drew I tried (byte-recompile-directory (file-expand-wildcards "~/.emacs.d/elpa/babel-repl*/") 0) but got an error Wrong type argument: stringp, nil.
I am not very familiar with elisp and the stringp type (a list of strings, I guess). I tried to use a (car ) on the returned value of file-expand-wildcards in order to get the first matched filename. But Emacs still work start correctly.
Any pointers?
Try file-expand-wildcards:
file-expand-wildcards is a compiled Lisp function in files.el.
(file-expand-wildcards PATTERN &optional FULL)
Expand wildcard pattern PATTERN.
This returns a list of file names which match the pattern.
If PATTERN is written as an absolute file name,
the values are absolute also.
If PATTERN is written as a relative file name, it is interpreted
relative to the current default directory, default-directory.
The file names returned are normally also relative to the current
default directory. However, if FULL is non-nil, they are absolute.
That gives you a list of expansions. Pick the first one or any one you want.

indent-[code-]rigidly called from emacs LISP function

I'm trying to write an emacs LISP function to un-indent the region
(rigidly). I can pass prefix arguments to indent-code-rigidly or
indent-rigidly or indent-region and they all work fine, but I don't
want to always have to pass a negative prefix argument to shift things
left.
My current code is as below but it seems to do nothing:
(defun undent ()
"un-indent rigidly."
(interactive)
(list
(setq fline (line-number-at-pos (region-beginning)))
(setq lline (line-number-at-pos (region-end)))
(setq curIndent (current-indentation))
;;(indent-rigidly fline lline (- curIndent 1))
(indent-region fline lline 2)
;;(message "%d %d" curIndent (- curIndent 1))
)
)
I gather that (current-indentation) won't get me the indentation of the first line
of the region, but of the first line following the region (so a second quesiton is
how to get that!). But even when I just use a constant for the column (as shown,
I don't see this function do any change.
Though if I uncomment the (message) call, it displays reasonable numbers.
GNU Emacs 24.3.1, on Ubuntu. And in case it matters, I use
(setq-default indent-tabs-mode nil) and (cua-mode).
I must be missing something obvious... ?
All of what Tim X said is true, but if you just need something that works, or an example to show you what direction to take your own code, I think you're looking for something like this:
(defun unindent-rigidly (start end arg &optional interactive)
"As `indent-rigidly', but reversed."
(interactive "r\np\np")
(indent-rigidly start end (- arg) interactive))
All this does is call indent-rigidly with an appropriately transformed prefix argument. If you call this with a prefix argument n, it will act as if you had called indent-rigidly with the argument -n. If you omit the prefix argument, it will behave as if you called indent-rigidly with the argument -1 (instead of going into indent-rigidly's interactive mode).
There are a number of problems with your function, including some vary
fundamental elisp requirements. Highly recommend reading the Emacs Lisp
Reference Manual (bundled with emacs). If you are new to programming and lisp,
you may also find An Introduction to Emacs Lisp useful (also bundled with
Emacs).
A few things to read about which will probably help
Read the section on the command loop from the elisp reference. In particular,
look at the node which describes how to define a new command and the use of
'interactive', which you will need if you want to bind your function to a key
or call it with M-x.
Read the section on variables from the lisp reference
and understand variable scope (local v global). Look at using 'let' rather
than 'setq' and what the difference is.
Read the section on 'positions' in the elisp reference. In particular, look at
'save-excursion' and 'save-restriction'. Understanding how to define and use
the region is also important.
It isn't clear if your writing this function just as a learning exercise or
not. However, just in case you are doing it because it is something you need to
do rather than just something to learn elisp, be sure to go through the Emacs
manual and index. What you appear to need is a common and fairly well supported
requirement. It can get a little complicated if programming modes are involved
(as opposed to plain text). However, with emacs, if what you need seems like
something which would be a common requirement, you can be fairly confident it is
already there - you just need to find it (which can be a challenge at first).
A common convention is for functions/commands to be defined which act 'in
reverse' when supplied with a negative or universal argument. Any command which
has this ability can also be called as a function in elisp code with the
argument necessary to get that behaviour, so understanding the inter-play
between commands, functions and calling conventions is important.

How do I find-file recursively in directories

Often, I know the file name but don't remember or find it cumbersome to specify the exactly directory. I'd like to be able to do:
find-file x/y/*/some_file.txt
where * would allow searching in recursive directories instead of just the current one.
As described here:
M-x find-name-dired
After you provide the directory and the pattern the matching files will be displayed in a Dired buffer. You can navigate the file list (C-n, C-p, etc.) and open files as you wish (Ret).
I've long looked for that feature and I'm now satisfied with what I found: I'm using helm-projectile.
Projectile is a project interaction library. Projects are VCS directories or directories containing a .projectile file. It is based on GNU find (but offers a pure emacs lisp implementation too)
https://github.com/bbatsov/projectile
(you can install it with packages.el). You can use it alone: call projectile-find-file (or projectile-mode and C-c p f). It uses ido for the completion by default, but in that case I prefer the interface of helm-projectile.
My colleagues kept telling me to use Sublime Text because of that feature. Fortunately, I found projectile :)
ido-find-file does this: when the filename that you enter doesn't match
anything in the current directory, it uses something like locate to match
what you wrote against all files on your system.
The setup is just one line:
(ido-mode)
After this, C-x C-f will call ido-find-file.
Also, I usually add:
(setq ido-enable-flex-matching t)
because I like it, but with it you will locate more results,
i.e. foo will also match froo.
In case of files in a project directory, I have used Fiplr that uses a fuzzy search library by the same author.
https://github.com/d11wtq/fiplr
This is available from MELPA.
Other solution that I haven't tried is https://github.com/technomancy/find-file-in-project
edit: I found much better with projectile, see my other answer.
There is find-recursive.el : http://www.webweavertech.com/ovidiu/emacs/find-recursive.txt
Download it, put it in your load path and require it in your .emacs: (it isn't available through ELPA or el-get)
(require 'find-recursive)
now when you call M-x find-recursive, it will ask for a file name to search for recursively, a base directory, then you have to select one file among a result list. It isn't integrated into ido unfortunately, but I like it, it is useful sometimes.
Here is a solution that gives you selectivity over the files and subdirectories searched. To use it you need both Dired+ and Icicles.
The command, used only in Dired, is icicle-visit-marked-file-of-content-recursive (or the -other-window version of it). It is bound to M-+ C-F (for other window, use M-+ C-O -- and yes, those are uppercase F and O).
What it does:
It provides, as candidates for completion, all of the files marked in the current Dired directory, pslus all of those in any marked subdirectories that have Dired buffers, and so on recursively.
You can visit any number of such candidates that match your minibuffer input, including all of them. You can visit some that match a minibuffer input pattern, then change the pattern to match and visit others --- all in the same command invocation.
"Matching" your input can mean either or both:
Matching the file name
Matching the file content
That is, your input can be two-part (separated by hitting C-M-j: a file-name part and a file-content part. Narrow the choices by matching file names, and narrow further by matching text in those files. This search is fast -- it succeeds as soon as it finds a single match in the file.
You can omit either part: match only file names (fast) or only file contents (slower, naturally).
You can combine input patterns -- progressive completion. It is much easier to add additional patterns to match, incrementally and interactively, than it is to try to come up with a single regexp that does just what you want.
Each input pattern (each part of a two-part pattern) can be a regexp. Fuzzy matching is also available.
You can alternatively just insert subdirectories in your Dired buffer, instead of having them as separate Dired buffers. Any files and subdirs marked in a Dired buffer are handled the same way.
If you do not want to descend into subdirs that are marked, you can use the non-recursive version of the command, icicle-visit-marked-file-of-content, bound to C-F (C-O for other-window).
If you also use Bookmark+ then you can bookmark a Dired buffer or a set of Dired buffers. The bookmark records which files and subdirectories are marked, which subdirectories are inserted, and which files and subdirectories are omitted. Jumping to such a bookmark restores all of these things. This gives you a way to snapshot a project, which you can then search using M-+ C-F etc.
A lot of options listed, I'm using next command:
find-file-in-current-directory
You can put it in your .emacs like this:
;; Find file in current directory:
(global-set-key (kbd "C-M-,") 'find-file-in-current-directory)
Interface looks very familiar to those who use fsf tools (https://github.com/junegunn/fzf)
If you have helm, you can use this:
(defun my/helm-find-file-recursively ()
"Recursively find files in glob manner, in the specified directory."
(interactive)
(helm-find 'ask-for-dir))
(global-set-key (kbd "C-c o f") 'my/helm-find-file-recursively)
Icicles command icicle-locate (or icicle-locate-file, if you do not have a locate program for your OS) is made for that. Locate a file anywhere under a given directory (including ~HOME or the root directory).
You can match against any parts of the absolute file name (i.e., the path and the file name). You can use several kinds of matching, including regexp, substring, and fuzzy matching.
For a more programmatic approach (using the built-in files library):
(directory-files-recursively "~/assignments/" "[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}.org")
;; ("~/assignments/a/b/2022-05-18.org"
;; "~/assignments/c/d/2022-07-15.org"
;; "~/assignments/e/f/2022-08-08.org")
Here's a thin wrapper around that function which reads a filename from the minibuffer and returns a list of matching directories (starting from the current directory), from which you select an entry which will then be visited:
(defun my/find-directory ()
"Find the directory containing FILE."
(interactive)
(let ((matches (directory-files-recursively default-directory (read-from-minibuffer "Filename: "))))
(if matches
(find-file
(completing-read
"Pick a directory: "
(mapcar 'file-name-directory matches))))
(message "No matching directories.")))

How to print all the defined variables in emacs?

M-x < TAB > prints all the defined functions.
To check a variable is defined or not evaluating the following expression,
(boundp 'variable-name) C-x C-e will print t if the variable-name is defined else nill.
How to print all the defined variables in emacs.
It's unclear exactly what you want to do with a full list of symbols, since the way in which M-x displays function names is somewhat specialized.
Assuming that you want to programmatically obtain a list of all defined symbols, here's how auto-complete.el does it:
(loop for x being the symbols
if (boundp x)
collect (symbol-name x))
Note that you can also enter M-x describe-var RET, and then press TAB to get a sorted completion list of all symbols.
I presume (apropos-variable "." t) would show you all the variables defined at that point in time.
edit: I presumed wrongly, it would seem.
Interestingly, this actually shows me significantly fewer results than the auto-completions from describe-var.
Can anyone shed light on that?
e.g. the differences between these, when winner-mode has been enabled:
C-uM-x apropos-variable RET winner- RET
C-hv winner- TAB
edit 2: Ah... it looks like apropos may ignore any symbol which lacks a documentation string.
If it's possible, I suggest reassigning the accepted answer.
Extrapolating (heavily!) what is being asked for, here is a way to get a pretty-printed alist of all buffer-local variables with their values. This is very convenient for finding out why for instance a mode isn't behaving the way one expects.
To get this listing, do:
M-x pp-eval-expression RET (buffer-local-variables) RET
Relevant portions from this list can be added almost verbatim to a .dir-locals.el file for use with multiple files.

regexp-opt function missing in Emacs?

I've started playing around with elisp to create font-locks (syntax highlighting) in Emacs. Most of the tutorials out there mention using an elisp function "regexp-opt" to optimize regular expressions, but my version of emacs (23.2) doesn't seem to have that function. Furthermore, Googling for the answer doesn't seem to turn up useful results. Any ideas?
As you can find out via C-h f regexp-opt:
regexp-opt is a compiled Lisp
function.
(regexp-opt strings &optional paren)
Return a regexp to match a string in
the list strings. Each string should
be unique in strings and should not
contain any regexps, quoted or not.
If optional paren is non-nil, ensure
that the returned regexp is enclosed
by at least one regexp grouping
construct. The returned regexp is
typically more efficient than the
equivalent regexp:
(let ((open (if paren "\(" ""))
(close (if paren "\)" "")))
(concat open (mapconcat 'regexp-quote
strings "\|") close))
If paren is `words', then the
resulting regexp is additionally
surrounded by \< and >.
Note, that it is a function to be used in Lisp code, not an interactive command which you could run with M-x
regexexp-opt is a elisp function but not an emacs command. That is why you cannot execute it by running: M-x regexp-opt
However, you can execute any elisp function from the elisp shell. Type in M-x eshell. And from this shell you can run regexp-opt
As others have said, regexp-opt is not a command (so you cannot invoke it using M-x). But you can invoke an application of it interactively, using M-:.
For example, M-: (regexp-opt '("foo" "toto" "blat" "total" "mistral")) RET returns the regexp "\\(?:blat\\|foo\\|mistral\\|tot\\(?:al\\|o\\)\\)".