all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Simple useful function
@ 2010-07-05 21:19 Andrea Crotti
  2010-07-05 21:56 ` Deniz Dogan
  0 siblings, 1 reply; 15+ messages in thread
From: Andrea Crotti @ 2010-07-05 21:19 UTC (permalink / raw)
  To: help-gnu-emacs

I write it here because I hope someone will find it useful...

I wrote this function to open all the files listed in my git repository

--8<---------------cut here---------------start------------->8---
(defun open-git-files ()
  "Visit all the files in the current git project"
  (interactive)
  (dolist
      (file (split-string (shell-command-to-string "git ls-files")))
    (message "Opening %s" file)
    ;; we have to keep the original position
    (find-file file)))
--8<---------------cut here---------------end--------------->8---

The problem is that if I understand well when I find the file I also
change the current directory, and then nothing works anymore...

I also tried to store the start point and concatenate it but it also
doesn't work.

Is there a smarter way to do it? (maybe not using git ls-files at all?)




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-05 21:19 Simple useful function Andrea Crotti
@ 2010-07-05 21:56 ` Deniz Dogan
  2010-07-05 22:58   ` Dan Davison
  0 siblings, 1 reply; 15+ messages in thread
From: Deniz Dogan @ 2010-07-05 21:56 UTC (permalink / raw)
  To: Andrea Crotti; +Cc: help-gnu-emacs

2010/7/5 Andrea Crotti <andrea.crotti.0@gmail.com>:
> I write it here because I hope someone will find it useful...
>
> I wrote this function to open all the files listed in my git repository
>
> --8<---------------cut here---------------start------------->8---
> (defun open-git-files ()
>  "Visit all the files in the current git project"
>  (interactive)
>  (dolist
>      (file (split-string (shell-command-to-string "git ls-files")))
>    (message "Opening %s" file)
>    ;; we have to keep the original position
>    (find-file file)))
> --8<---------------cut here---------------end--------------->8---
>
> The problem is that if I understand well when I find the file I also
> change the current directory, and then nothing works anymore...
>
> I also tried to store the start point and concatenate it but it also
> doesn't work.
>
> Is there a smarter way to do it? (maybe not using git ls-files at all?)
>
>
>

Just wrap the call to `find-file' in `save-excursion' and you're good.

(save-excursion
  (find-file file))

-- 
Deniz Dogan



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-05 21:56 ` Deniz Dogan
@ 2010-07-05 22:58   ` Dan Davison
  2010-07-05 23:34     ` Drew Adams
  0 siblings, 1 reply; 15+ messages in thread
From: Dan Davison @ 2010-07-05 22:58 UTC (permalink / raw)
  To: Deniz Dogan; +Cc: help-gnu-emacs, Andrea Crotti

Deniz Dogan <deniz.a.m.dogan@gmail.com> writes:

> 2010/7/5 Andrea Crotti <andrea.crotti.0@gmail.com>:
>> I write it here because I hope someone will find it useful...
>>
>> I wrote this function to open all the files listed in my git repository

Is there a way to do this sort of stuff with dired (perhaps involving
magit)? E.g. how would one limit dired to showing just the files that
git is tracking?

Dan

>>
>> --8<---------------cut here---------------start------------->8---
>> (defun open-git-files ()
>>  "Visit all the files in the current git project"
>>  (interactive)
>>  (dolist
>>      (file (split-string (shell-command-to-string "git ls-files")))
>>    (message "Opening %s" file)
>>    ;; we have to keep the original position
>>    (find-file file)))
>> --8<---------------cut here---------------end--------------->8---
>>
>> The problem is that if I understand well when I find the file I also
>> change the current directory, and then nothing works anymore...
>>
>> I also tried to store the start point and concatenate it but it also
>> doesn't work.
>>
>> Is there a smarter way to do it? (maybe not using git ls-files at all?)
>>
>>
>>
>
> Just wrap the call to `find-file' in `save-excursion' and you're good.
>
> (save-excursion
>   (find-file file))



^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: Simple useful function
  2010-07-05 22:58   ` Dan Davison
@ 2010-07-05 23:34     ` Drew Adams
  2010-07-06  5:33       ` Thierry Volpiatto
  2010-07-06 11:03       ` Andrea Crotti
  0 siblings, 2 replies; 15+ messages in thread
From: Drew Adams @ 2010-07-05 23:34 UTC (permalink / raw)
  To: 'Dan Davison', 'Deniz Dogan'
  Cc: help-gnu-emacs, 'Andrea Crotti'

> Is there a way to do this sort of stuff with dired (perhaps involving
> magit)? E.g. how would one limit dired to showing just the files that
> git is tracking?

If you have an Emacs function that returns those file names (absolute or
relative), then you can open a Dired buffer for just those files.

You do that by calling `dired' with a cons arg: the list of file names.  If the
files are not all in the same directory, then use absolute names.  For example:

 (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))

`C-h f dired':

,----
| dired is an interactive compiled Lisp function in `dired.el'.
...
| (dired DIRNAME &optional SWITCHES)
...
| If DIRNAME is a cons, its first element is taken as the directory name
| and the rest as an explicit list of files to make directory entries for.
| You can move around in it with the usual commands.
...
`----

You can do more than "move around in it"!  All Dired operations work normally on
the files listed.

The file names can be a mix of local and remote, absolute and relative.

This is an important and little known Dired feature.  IMHO, it is not well
documented.  And that's probably why it had a few bugs wrt remote files until
this year.  Little attention is drawn to this feature, yet it is very useful.
You can use it to organize files into projects etc.  Define specific commands
that give you just the sets of files you want.

Combine this feature with an ability to bookmark Dired buffers and you have a
flexible way to get quickly to and operate on a given set of files, without
defining specialized commands.  With Bookmark+, you can not only bookmark an
arbitrary Dired buffer (saving the current Dired switches, markings, and subdir
showings).  You can also open a Dired buffer on the bookmarks currently marked
in the `*Bookmark List*'.  http://www.emacswiki.org/emacs/BookmarkPlus





^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-05 23:34     ` Drew Adams
@ 2010-07-06  5:33       ` Thierry Volpiatto
  2010-07-06 13:21         ` Drew Adams
  2010-07-06 11:03       ` Andrea Crotti
  1 sibling, 1 reply; 15+ messages in thread
From: Thierry Volpiatto @ 2010-07-06  5:33 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> Is there a way to do this sort of stuff with dired (perhaps involving
>> magit)? E.g. how would one limit dired to showing just the files that
>> git is tracking?
>
> If you have an Emacs function that returns those file names (absolute or
> relative), then you can open a Dired buffer for just those files.
>
> You do that by calling `dired' with a cons arg: the list of file names.  If the
> files are not all in the same directory, then use absolute names.  For example:
>
>  (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))

Isn't it?

,----
| (dired (cons "My GIT Files" '("/my/first/file" "/my/2nd/file"...)))
`----

It seem nice to use that with:

,----
| (dired (cons "my files" (dired-get-marked-files)))
`----

Nice feature anyway.

> `C-h f dired':
>
> ,----
> | dired is an interactive compiled Lisp function in `dired.el'.
> ...
> | (dired DIRNAME &optional SWITCHES)
> ...
> | If DIRNAME is a cons, its first element is taken as the directory name
> | and the rest as an explicit list of files to make directory entries for.
> | You can move around in it with the usual commands.
> ...
> `----
>
> You can do more than "move around in it"!  All Dired operations work normally on
> the files listed.
>
> The file names can be a mix of local and remote, absolute and relative.
>
> This is an important and little known Dired feature.  IMHO, it is not well
> documented.  And that's probably why it had a few bugs wrt remote files until
> this year.  Little attention is drawn to this feature, yet it is very useful.
> You can use it to organize files into projects etc.  Define specific commands
> that give you just the sets of files you want.
>
> Combine this feature with an ability to bookmark Dired buffers and you have a
> flexible way to get quickly to and operate on a given set of files, without
> defining specialized commands.  With Bookmark+, you can not only bookmark an
> arbitrary Dired buffer (saving the current Dired switches, markings, and subdir
> showings).  You can also open a Dired buffer on the bookmarks currently marked
> in the `*Bookmark List*'.  http://www.emacswiki.org/emacs/BookmarkPlus
>
>
>
>

-- 
Thierry Volpiatto
Gpg key: http://pgp.mit.edu/




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
       [not found] <mailman.16.1278364774.11527.help-gnu-emacs@gnu.org>
@ 2010-07-06  7:26 ` Nicolas Neuss
  2010-07-06  7:36   ` Teemu Likonen
  0 siblings, 1 reply; 15+ messages in thread
From: Nicolas Neuss @ 2010-07-06  7:26 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> writes:

> [...]

Hello Andrea,

I find your highlighting of code

--8<---------------cut here---------------start------------->8---
(defun open-git-files ()
  ...)
--8<---------------cut here---------------end--------------->8---

very nice.  Is this feature documented somewhere?  Is there some command
for inserting the "scissor" lines?

Nicolas


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-06  7:26 ` Nicolas Neuss
@ 2010-07-06  7:36   ` Teemu Likonen
  0 siblings, 0 replies; 15+ messages in thread
From: Teemu Likonen @ 2010-07-06  7:36 UTC (permalink / raw)
  To: help-gnu-emacs

* 2010-07-06 09:26 (+0200), Nicolas Neuss wrote:

> Is there some command for inserting the "scissor" lines?

In message-mode the commands are message-mark-inserted-region (C-c M-m)
and message-mark-insert-file (C-c M-f).


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-05 23:34     ` Drew Adams
  2010-07-06  5:33       ` Thierry Volpiatto
@ 2010-07-06 11:03       ` Andrea Crotti
  2010-07-06 11:11         ` Lennart Borgman
  1 sibling, 1 reply; 15+ messages in thread
From: Andrea Crotti @ 2010-07-06 11:03 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:
>
> If you have an Emacs function that returns those file names (absolute or
> relative), then you can open a Dired buffer for just those files.
>
> You do that by calling `dired' with a cons arg: the list of file names.  If the
> files are not all in the same directory, then use absolute names.  For example:
>
>  (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))
>
> `C-h f dired':

Very nice, I simply did something like:

--8<---------------cut here---------------start------------->8---
(defun open-git-files ()
  "Visit all the files in the current git project"
  (interactive)
  (dolist
      (file (ls-git-files))
    (message "Opening %s" file)
    ;; we have to keep the original position
    (save-excursion (find-file file))))

(defun dired-git-files ()
  (interactive)
  (dired (ls-git-files)))

(defun ls-git-files ()
  (if
      (file-exists-p ".git")
      (split-string (shell-command-to-string "git ls-files"))
    (message "not a git repo")))
--8<---------------cut here---------------end--------------->8---

Any improvement is welcome ;)
Does ls-git-files "returns" nil automatically if ".git" is not found?




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-06 11:03       ` Andrea Crotti
@ 2010-07-06 11:11         ` Lennart Borgman
  2010-07-06 11:49           ` Andrea Crotti
  0 siblings, 1 reply; 15+ messages in thread
From: Lennart Borgman @ 2010-07-06 11:11 UTC (permalink / raw)
  To: Andrea Crotti; +Cc: help-gnu-emacs

On Tue, Jul 6, 2010 at 1:03 PM, Andrea Crotti <andrea.crotti.0@gmail.com> wrote:
>
> (defun ls-git-files ()
>  (if
>      (file-exists-p ".git")
>      (split-string (shell-command-to-string "git ls-files"))
>    (message "not a git repo")))
> --8<---------------cut here---------------end--------------->8---
>
> Any improvement is welcome ;)
> Does ls-git-files "returns" nil automatically if ".git" is not found?

No. The last statement run will be (message ...) and the doc string
for message says "Return the message".

Just add nil if you want it to return nil:

(defun ls-git-files ()
 (if
     (file-exists-p ".git")
     (split-string (shell-command-to-string "git ls-files"))
   (message "not a git repo")
   nil))



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-06 11:11         ` Lennart Borgman
@ 2010-07-06 11:49           ` Andrea Crotti
  2010-07-06 12:25             ` Lennart Borgman
  0 siblings, 1 reply; 15+ messages in thread
From: Andrea Crotti @ 2010-07-06 11:49 UTC (permalink / raw)
  To: help-gnu-emacs

Lennart Borgman <lennart.borgman@gmail.com> writes:

>
> No. The last statement run will be (message ...) and the doc string
> for message says "Return the message".
>
> Just add nil if you want it to return nil:
>
> (defun ls-git-files ()
>  (if
>      (file-exists-p ".git")
>      (split-string (shell-command-to-string "git ls-files"))
>    (message "not a git repo")
>    nil))

Ah good thanks, a non related question, why the last message written in
*Messages* is not automatically saved somewhere?

I mean, sometimes it's useful to get it, without going to the *Messages*
buffer every time...




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-06 11:49           ` Andrea Crotti
@ 2010-07-06 12:25             ` Lennart Borgman
  0 siblings, 0 replies; 15+ messages in thread
From: Lennart Borgman @ 2010-07-06 12:25 UTC (permalink / raw)
  To: Andrea Crotti; +Cc: help-gnu-emacs

On Tue, Jul 6, 2010 at 1:49 PM, Andrea Crotti <andrea.crotti.0@gmail.com> wrote:
> Lennart Borgman <lennart.borgman@gmail.com> writes:
>
>>
>> No. The last statement run will be (message ...) and the doc string
>> for message says "Return the message".
>>
>> Just add nil if you want it to return nil:
>>
>> (defun ls-git-files ()
>>  (if
>>      (file-exists-p ".git")
>>      (split-string (shell-command-to-string "git ls-files"))
>>    (message "not a git repo")
>>    nil))
>
> Ah good thanks, a non related question, why the last message written in
> *Messages* is not automatically saved somewhere?
>
> I mean, sometimes it's useful to get it, without going to the *Messages*
> buffer every time...

There is (current-message), see

  (info "(elisp) Displaying Messages")



^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: Simple useful function
  2010-07-06  5:33       ` Thierry Volpiatto
@ 2010-07-06 13:21         ` Drew Adams
  2010-07-15  7:39           ` Thierry Volpiatto
       [not found]           ` <mailman.2.1279179720.17180.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 15+ messages in thread
From: Drew Adams @ 2010-07-06 13:21 UTC (permalink / raw)
  To: 'Thierry Volpiatto', help-gnu-emacs

> >  (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))
> 
> Isn't it?
> (dired (cons "My GIT Files" '("/my/first/file" "/my/2nd/file"...)))

Yes, it is!  Sorry about that.

> It seem nice to use that with:
> (dired (cons "my files" (dired-get-marked-files)))

Yes.

> Nice feature anyway.




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-06 13:21         ` Drew Adams
@ 2010-07-15  7:39           ` Thierry Volpiatto
  2010-08-03 15:11             ` Andrea Crotti
       [not found]           ` <mailman.2.1279179720.17180.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 15+ messages in thread
From: Thierry Volpiatto @ 2010-07-15  7:39 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> >  (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))
>> 
>> Isn't it?
>> (dired (cons "My GIT Files" '("/my/first/file" "/my/2nd/file"...)))
>
> Yes, it is!  Sorry about that.
>
>> It seem nice to use that with:
>> (dired (cons "my files" (dired-get-marked-files)))
>
> Yes.

However, that lead to many failures in all dired commands, i have
submitted a bug, have a look.

>> Nice feature anyway.
>
>
>

-- 
Thierry Volpiatto
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
       [not found]           ` <mailman.2.1279179720.17180.help-gnu-emacs@gnu.org>
@ 2010-07-15  8:10             ` Andreas Politz
  0 siblings, 0 replies; 15+ messages in thread
From: Andreas Politz @ 2010-07-15  8:10 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> "Drew Adams" <drew.adams@oracle.com> writes:
>
>>> >  (dired "My GIT Files" '("/my/first/file" "/my/2nd/file"...))
>>> 
>>> Isn't it?
>>> (dired (cons "My GIT Files" '("/my/first/file" "/my/2nd/file"...)))
>>
>> Yes, it is!  Sorry about that.
>>
>>> It seem nice to use that with:
>>> (dired (cons "my files" (dired-get-marked-files)))
>>
>> Yes.
>
> However, that lead to many failures in all dired commands, i have
> submitted a bug, have a look.

Probably because the car is ment to be a directory and not a randomly
chosen name, no bug.

-ap
>
>>> Nice feature anyway.
>>
>>
>>


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Simple useful function
  2010-07-15  7:39           ` Thierry Volpiatto
@ 2010-08-03 15:11             ` Andrea Crotti
  0 siblings, 0 replies; 15+ messages in thread
From: Andrea Crotti @ 2010-08-03 15:11 UTC (permalink / raw)
  To: help-gnu-emacs


If someone is interested now my "git" related functions are a bit
smarter

--8<---------------cut here---------------start------------->8---
(defun open-git-files ()
  "Visit all the files in the current git project"
  (interactive)
  (dolist
      (file (ls-git-files))
    (message "Opening %s" file)
    ;; we have to keep the original position
    (save-excursion (find-file file))))

(defun dired-git-files (directory)
  "Open a dired buffer containing the local git files"
  (interactive "D")
  (cd directory)
  (let ((files (ls-git-files)))
    (if
        (or 
         (< (length files) 200)
         (yes-or-no-p (format "%d files, are you sure?" (length files))))
        ;; rename the buffer to something with a sense
        (progn
          (dired (ls-git-files))
          (rename-buffer (concat "git-" (before-last (split-string directory "/"))))))))

;; TODO: take the return code instead
(defun ls-git-files ()
  (let
      ((result (shell-command-to-string (concat "git ls-files"))))
    (if
        (string-match "fatal" result)
        nil
      (split-string result))))
--8<---------------cut here---------------end--------------->8---

But this is probably even more cool
--8<---------------cut here---------------start------------->8---
  (defun query-replace-in-git (from to)
    "query replace regexp on the files given"
    (interactive "sFrom: \nsTo: ")
    (dired-git-files (pwd))
    (dired-mark-files-regexp ".[ch]")
    (dired-do-query-replace-regexp from to))
--8<---------------cut here---------------end--------------->8---

I use the above function and marking with git to replace in the whole
project in only one shot.
I should add maybe some other options for example for the files to mark




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2010-08-03 15:11 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-05 21:19 Simple useful function Andrea Crotti
2010-07-05 21:56 ` Deniz Dogan
2010-07-05 22:58   ` Dan Davison
2010-07-05 23:34     ` Drew Adams
2010-07-06  5:33       ` Thierry Volpiatto
2010-07-06 13:21         ` Drew Adams
2010-07-15  7:39           ` Thierry Volpiatto
2010-08-03 15:11             ` Andrea Crotti
     [not found]           ` <mailman.2.1279179720.17180.help-gnu-emacs@gnu.org>
2010-07-15  8:10             ` Andreas Politz
2010-07-06 11:03       ` Andrea Crotti
2010-07-06 11:11         ` Lennart Borgman
2010-07-06 11:49           ` Andrea Crotti
2010-07-06 12:25             ` Lennart Borgman
     [not found] <mailman.16.1278364774.11527.help-gnu-emacs@gnu.org>
2010-07-06  7:26 ` Nicolas Neuss
2010-07-06  7:36   ` Teemu Likonen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.