Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend spacemacs/copy-file command with extra actions #14754

Merged

Conversation

dalanicolai
Copy link
Contributor

This PR implements an alternative for PR #8974.

@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch 2 times, most recently from ca6a3f5 to 2579a08 Compare May 8, 2021 21:50
Copy link
Collaborator

@lebensterben lebensterben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, replace error by user-error. These errors should not start the debugger.

(not (y-or-n-p (format "File %s exists. Overwrite?: " new-file-name))))
(error "Canceled"))
(pcase
(read-answer "New file action: "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general avoid string comparison. This is inefficient.

You can just match the character code.

If you really want, match symbol, e.g. 'only-write.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this info, handy to be aware of. But in this case the code only runs once only when this command is called, so I guess the efficiency is not so important in this case.

So otherwise you would suggest to compare only the first (or other relevant) characters, or alternatively compare by converting to symbol with intern?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I did not know about user-error. Indeed I used a catch-throw before, but I thought error looks nicer (and then accept the "wrong" behavior). Very useful info!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So otherwise you would suggest to compare only the first (or other relevant) characters, or alternatively compare by converting to symbol with intern?

See https://www.gnu.org/software/emacs/manual/html_node/elisp/Multiple-Queries.html

  • wrap this in a let form to set read-answer-short to t.
  • Change the long answer to a single character string. the long answer is not shown but is returned. Since it has length 1, it's fast to compare.
  • Since read-answer-short is t, user only need to type one key to select a choice, instead of being required to type a sentence/phrase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I was aware of this, but then I would probably have wrapped it in read-answer-short 'auto (and indeed make the long answers slightly shorter, but still a little more informative)... somehow I assumed the 'auto was the default behavior...

(when (directory-name-p new-file-name)
(if buffer-file-name
(setq new-file-name (concat new-file-name (buffer-name)))
(error "Copying of non-visiting file buffers requires to specify new file name.")))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"non-visiting file" is not easy to understand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree somewhat, although I think it is hard to misunderstand this (of course it is "non-visiting file buffer").
I would be happy if you could suggest a better term (English is not my native language).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "non-visiting-file buffers"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels like romance language, not english

layers/+spacemacs/spacemacs-defaults/funcs.el Outdated Show resolved Hide resolved
layers/+spacemacs/spacemacs-defaults/funcs.el Outdated Show resolved Hide resolved
@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch 2 times, most recently from fc1dbcb to 76948b8 Compare May 9, 2021 05:34
@lebensterben
Copy link
Collaborator

key-bindings for the multiple choice can be improved.

If this command is invoked via SPC f c, we can put the most possible choice under the key c.
So the user just quickly type SPC f c c to perform the default behavior.

I suggest

  • c Copy and open in new buffer
  • C Copy and open on current buffer
  • d Don't open the new file after copying
  • q Quit

@dalanicolai
Copy link
Contributor Author

dalanicolai commented May 9, 2021

I agree with you @lebensterben that generally, it is nice to put the default bindings on the quickest way to type. However, in this case after typing SPC f c the user first has to enter a new filename and a RET. So it is not very important here that the default action is under c.
Also, I always prefer downcase letters, so I would vote against using C.

On the other hand, I do agree that my suggestions (r, n and o) might not be the best choices. But I would suggest we choose either the most sensible mnemonic or most consistent with other command's keybindings (which should generally most sensible mnemonic also).

So another main candidate for me was:
c for open in current buffer
o for open in other buffer
w for just write (or 's' for just save)

@lebensterben
Copy link
Collaborator

okay. c o w looks a good choice

@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch from 76948b8 to eab5a05 Compare May 9, 2021 11:21
@dalanicolai
Copy link
Contributor Author

Updated! The reason why I chose "replace" over "current-buffer" is that the former "more explicitly" implies that the old-file buffer is getting killed (or well... replaced). But I guess this cow will also produce our milk...

This command prompts for a new filename then prompts for
selecting an action to perform on the new file.

If prefixed with the universal ARG \\[universal-argument] the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind me why we need this prefix argument?

(read-file-name "Save file as: " (if arg buffer-file-name default-directory))

Can we just write

(read-file-name "Save file as: " (or buffer-file-name default-directory))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument is to include the filename in the prompt for new filename like in the original proposal #8974 (comment) (but then inverted). The or wouldn't work here, at least not for how I meant it. On the other hand (buffer-name) would be even better than buffer-file-name.

@lebensterben
Copy link
Collaborator

lebensterben commented May 26, 2021

Use this

(defun spacemacs/save-as (filename &optional visit)
  "Save current buffer or active region as specified file.
When called interactively, it first prompts for FILENAME, and then asks
 whether to VISIT it, and if so, whether to show it in current window or
 another window.

FILENAME  a non-empty string as the name of the saved file.
VISIT     When it's `:current', open FILENAME in current window. When it's
          `:other', open FILENAME in another window. When it's nil, only
          save to FILENAME but does not visit it. (Default to `:current'
          when called from a LISP program.)

When FILENAME already exists, it also asks the user whether to overwrite it."
  (interactive (let* ((filename (expand-file-name (read-file-name "Save buffer as: ")))
                      (choices  '("Current window"
                                  "Other window"
                                  "Don't open"))
                      (actions  '(:current :other nil))
                      (visit    (let ((completion-ignore-case t))
                                  (nth (cl-position
                                        (completing-read "Do you want to open the file? "
                                                         choices nil t)
                                        choices
                                        :test #'equal)
                                       actions))))
                 (list filename visit)))
  (unless (called-interactively-p 'any)
    (cl-assert (and (stringp filename)
                    (not (string-empty-p filename))
                    (not (directory-name-p filename)))
              t "Expect a non-empty filepath, found: %s")
    (setq filename (expand-file-name filename)
          visit (or visit :other))
    (let ((choices '(:current :other nil)))
      (cl-assert (memq visit choices)
                 t "Found %s, expect one of %s")))
  (let ((dir (file-name-directory filename)))
    (unless (file-directory-p dir)
      (make-directory dir t)))
  (if (use-region-p)
      (write-region (region-beginning) (region-end) filename nil nil nil t)
    (write-region nil nil filename nil nil nil t))
  (pcase visit
    (:current (find-file filename))
    (:other   (find-file-other-window filename))))

@dalanicolai
Copy link
Contributor Author

dalanicolai commented May 26, 2021

Haha! That's funny... it is a full rewrite. Anyway, that is fine with me. However, this doesn't yet include an option to include the (current) filename in the prompt. (Which was one of the main "improvements" of the original proposal #8974 (comment). Or you don't like that?)
Also it is not possible to select a "choice" with a single keystroke, but that is a matter of preference of course (I like this completing-read solution too).

Nice addition to include the 'write selected region' option!

@lebensterben
Copy link
Collaborator

@dalanicolai
That's a bug of helm.
If you want, (read-file-name "Save buffer as: " nil nil nil buffer-file-name)

@lebensterben
Copy link
Collaborator

I already found this issue. I just didn't bother to fix it.

@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch from eab5a05 to 303e0ba Compare May 26, 2021 07:39
@lebensterben
Copy link
Collaborator

lebensterben commented May 26, 2021

it's possible to select with two keystroke. the initial and <enter>.

@lebensterben
Copy link
Collaborator

Also note that I've edited the code snippet. It's certainly not final.

Copy link
Collaborator

@lebensterben lebensterben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this. Just fix the docstring.
Also please update CHANGELOG.develop

(call-interactively 'write-file))
(defun spacemacs/save-as (filename &optional visit)
"Save current buffer or active region as specified file.
When called interactively, it first prompts for FILENAME, and then asks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra space in the beginning of each line of docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch 3 times, most recently from 77c224c to e6ce398 Compare August 28, 2021 03:26
@dalanicolai
Copy link
Contributor Author

dalanicolai commented Aug 28, 2021

I got a little confused about 5d213ce. It caused this PR to consist of 2 commits, and I think it was not meant like that. Anyway, I have reverted to a single commit, implemented the requested changes, and rebased the PR/branch on develop.

@lebensterben
Copy link
Collaborator

https://github.com/syl20bnr/spacemacs/pull/14754/files
This is what the changes will be like when it's squashed and merged.

@dalanicolai
Copy link
Contributor Author

You mean it is fine to have multiple commits because you will squash them when merging?

@lebensterben
Copy link
Collaborator

lebensterben commented Aug 28, 2021

yes.
squash and merge is the default actually

This commit implements an alternative for PR syl20bnr#8974
@dalanicolai dalanicolai force-pushed the extend-spacemacs/copy-file-command branch from e6ce398 to 7492e1b Compare September 15, 2021 11:31
@lebensterben lebensterben merged commit cabe650 into syl20bnr:develop Sep 15, 2021
@lebensterben
Copy link
Collaborator

Merged. Thanks for your contributions.

@bcc32
Copy link
Contributor

bcc32 commented Sep 16, 2021

I think this key binding needs to be updated, since the name of the function was changed:

;; file -----------------------------------------------------------------------
(spacemacs|spacebind
 "Files manipulation."
 :global
 (("f" "Files"
   ("A" spacemacs/find-file-and-replace-buffer "Set another file for buffer...")
   ("c" spacemacs/copy-file "Copy file to new file...")

lebensterben added a commit that referenced this pull request Sep 16, 2021
zv added a commit to zv/spacemacs that referenced this pull request Oct 6, 2021
* checkversion/develop: (55 commits)
  [doc] use org-mode  example block for example
  Added COPYRIGHT file
  [ivy] Fix new src block in docs
  [ivy] counsel search commands use spacemacs--ivy-file-actions
  [core] Support packing elisp to *.elc and *.el.gz files
  [version-control] Switch default version control diff tool to git-gutter
  [python] Remove comments and fix new commands
  [bot] "built_in_updates" Wed Sep 29 19:18:45 UTC 2021
  Add pydoc to python layer for better python doc functionality
  [python] Make ipython version detection more robust
  Revise some smaller layers
  [bot] "documentation_updates" Fri Sep 17 19:24:14 UTC 2021
  Fix (activate) company-emoji completion
  [bot] "documentation_updates" Fri Sep 17 18:59:01 UTC 2021
  Add documentation about evilifying 'special-mode buffers' (+fix eww)
  Fix and update PR syl20bnr#14992: update eaf layer
  Update keybinding
  Extend spacemacs/copy-file command with extra actions (syl20bnr#14754)
  Add simple workaround to ebib kill buffer issue (syl20bnr#15048)
  [bot] "documentation_updates" Sat Sep 11 22:58:39 UTC 2021
  ...
2ynn pushed a commit to 2ynn/spacemacs that referenced this pull request Jun 22, 2022
2ynn pushed a commit to 2ynn/spacemacs that referenced this pull request Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants