• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Emacs Lisp
  • License
    GNU General Publi...
  • Created almost 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Edit comment or string/docstring or code block inside them in separate buffer with your favorite mode

CI MELPA

separedit.el

Edit comment/string/docstring/code block in separate buffer with your favorite mode.

+----------+         Edit           +-----------+         Edit           +-----------+
|          | ---------------------> |   edit    | ---------------------> |   edit    | ...
|          |  point-at-comment?     |   buffer  |  point-at-comment?     |   buffer  |
|  source  |  point-at-string?      |           |  point-at-string?      |           | ...
|  buffer  |  point-at-codeblock?   | (markdown |  point-at-codeblock?   | (markdown | ...
|          |  point-at-...?         |  orgmode  |  point-at-...?         |  orgmode  |
|          | <--------------------- |   ...)    | <--------------------- |   ...)    | ...
+----------+     Commit changes     +-----------+     Commit changes     +-----------+

Installation

Clone this repository, or install from MELPA. Add the following to your .emacs:

(require 'separedit)

;; Key binding for modes you want edit
;; or simply bind ‘global-map’ for all.
(define-key prog-mode-map        (kbd "C-c '") #'separedit)
(define-key minibuffer-local-map (kbd "C-c '") #'separedit)
(define-key help-mode-map        (kbd "C-c '") #'separedit)
(define-key helpful-mode-map     (kbd "C-c '") #'separedit)

;; Default major-mode for edit buffer
;; can also be other mode e.g. ‘org-mode’.
(setq separedit-default-mode 'markdown-mode)

;; Feature options
;; (setq separedit-preserve-string-indentation t)
;; (setq separedit-continue-fill-column t)
;; (setq separedit-write-file-when-execute-save t)
;; (setq separedit-remove-trailing-spaces-in-comment t)

Usage

  • Move the cursor to a comment/string/code block or any supported place.

  • Press C-c '.

    or press C-u C-c ' to starting edit with manually selected major mode.

Can also press C-c ' on an active region.

Following are default keys in edit buffer:

Key Function Summary
C-c C-c separedit-commit Commit changes and close edit buffer
C-x C-s separedit-save Commit changes (even write source file) without closing edit buffer
C-c C-k separedit-abort Discard changes and close edit buffer
C-c ' separedit or follow the settings of markdown/org Open a new edit buffer

Edit comment

separedit use continuity as basis for determining whether it is a comment block or line. Continuous means that there is no barrier (e.g. code or blank line) between the end of previous line and the beginning of next line, for example:

/*
 * this is a
 * comment block
 */

//
// this is also a
// comment block
//

//
// this is another
// comment block
//

code 1 /* all this are comment lines */
code 2 /* all this are comment lines */
code 3 // all this are comment lines
code 4 // all this are comment lines

By setting separedit-default-mode to choose the mode (e.g. markdown-mode or org-mode) for edit buffer. In edit buffer, the comment delimiter will be removed, for example (â–ˆ represents the cursor):

source buffer     ->    edit buffer   ->    edit buffer

/*
 * # Exampleâ–ˆ           # Example
 *
 * ``` C                ``` C
 * foo("bar");          foo("bar");â–ˆ        foo("bar");
 * ```                  ```
 */

// * Exampleâ–ˆ           * Example
//
// #+BEGIN_SRC C        #+BEGIN_SRC C
// foo("bar");          foo("bar");â–ˆ        foo("bar");
// #+END_SRC            #+END_SRC

Edit string

separedit provides convenience for editing escaped strings, if there are nested string or code block, just continue press C-c ' to enter a new edit buffer:

source buffer     ->    edit buffer   ->    edit buffer

"aâ–ˆ\"b\\\"c\\\"\""       a"bâ–ˆ\"c\""           b"c"

Edit code block

separedit also support for editing code block directly in comment or string:

source buffer     ->    edit buffer

",--- elisp
 | (foo \"bar\")â–ˆ       (foo "bar")
 `---"

/*
 * ``` C
 * foo("bar");â–ˆ         foo("bar");
 * ```
 */

If the language identifier of code block is omitted, the edit buffer uses the same mode as the source buffer.

Edit heredoc

The heredoc marker can be used to specify the language:

source buffer       ->      edit buffer (css-mode)

...<<CSS
h1 {                        h1 {
  color: red;â–ˆ                color: red;â–ˆ
}                           }
CSS

Both LANG and __LANG__ are supported, see separedit-heredoc-language-regexp-alist for more detail.

Edit C/C++ macro

#defineâ–ˆFOO(a, b)    \      ->      #defineâ–ˆFOO(a, b)
do {                 \              do {
    auto _a = (a);   \                  auto _a = (a);
    auto _b = (b);   \                  auto _b = (b);
} while (false)                     } while (false)

Edit value form of variable in help/helpful buffer

Describe a variable, move cursor to the local/global value form, press C-c ' to edit it.

Edit minibuffer

Don't get stuck in minibuffer, press C-c ' to open a edit buffer.

Edit in vterm

Make sure the the vterm Directory tracking and Prompt tracking is set correctly.

Then put the cursor after prompt, press C-c ' to start a new edit, or C-p C-c ' to edit previous command.

Customization

Change key bindings in edit buffer

If you don't like the default key bindings in edit buffer, you can change it:

  • separedit-save-key
  • separedit-entry-key
  • separedit-abort-key
  • separedit-commit-key

Add support for a new major mode

  1. Add the start/end delimiter of block style comment to separedit-comment-encloser-alist.
  2. Add the delimiter of each comment line to separedit-comment-delimiter-alist.
  3. Add the string (including docstring) quotes to separedit-string-quotes-alist.
  4. Add definition to separedit-string-indent-offset-alist if there is base indent offset in docstring.
  5. Add a mode name to separedit-not-support-docstring-modes if not support docstring.

Add support for a new code block

  1. Add a set of regexps matching the new code block to separedit-block-regexp-plists.
  2. Add a language name to separedit-code-lang-modes if can't get mode by simply adding suffix -mode.

Preserving indentation of block in string

If separedit-preserve-string-indentation is non-nil, the indentation of string block will be preseved in edit buffer, e.g:

source buffer                         edit buffer
+--------------------+                +--------------------+
| def func():        |                | Usage:             |
|     '''            |                |     func()         |
|     Usage:         |       ->       |                    |
|         func()     |                |                    |
|     '''            |                |                    |
|     pass           |                |                    |
+====================+                +====================+

No only for the docsting, normal string are also supported:

source buffer                         edit buffer
+--------------------+                +--------------------+
| emacs \            |                | (progn             |
|    --batch \       |                |   ...)             |
|    --eval "(progn  |       ->       |                    |
|              ...)" |                |                    |
|                    |                |                    |
+====================+                +====================+

Continue fill-column width in edit buffer

If separedit-continue-fill-column is non-nil, use the remaining fill-width in edit buffer:

source buffer                   edit buffer

    //
    // this is a                this is a
    // comment block            comment block
    //

|<---->|<------------>|         |<------------->|
   |         |                         |
   |         '-- available width for --'
   |                edit buffer
used width

You may also like to enable auto-fill-mode in edit buffer:

(add-hook 'separedit-buffer-creation-hook #'auto-fill-mode)

Some extended usage

Combine multipe adjacent blocks as a single edit block

(defun separedit//region-of-el-commentary ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^;;; Commentary:\n+")
      (let ((begin (point)))
        (when (re-search-forward  "\n;;; .*$" nil t)
          (goto-char (match-beginning 0))
          (list begin (point)))))))

(defun separedit/edit-el-commentary ()
  "Edit whole commentary section as a single block."
  (interactive)
  (let ((separedit-leave-blank-line-in-comment t))
    (separedit-dwim
     (apply #'separedit-mark-region
            `(,@(separedit//region-of-el-commentary)
              markdown-mode)))))

Break long lines in comment

(defun separedit/re-fill ()
  (interactive)
  (let ((separedit-continue-fill-column t))
    (with-current-buffer (separedit-dwim)
      (fill-region (point-min) (point-max))
      (execute-kbd-macro (kbd "C-c C-c")))))

Eval multiple-line sexp in comment

(defun separedit/eval-last-sexp-in-comment ()
  (interactive)
  (let ((separedit-default-mode 'emacs-lisp-mode)
        (separedit-inhibit-edit-window-p t))
    (with-current-buffer (separedit)
      (unwind-protect (call-interactively #'eval-last-sexp)
        (separedit-abort)))))

(define-key emacs-lisp-mode-map (kbd "C-x C-e")
  (lambda ()
    (interactive)
    (call-interactively
     (if (separedit--point-at-comment)
         #'separedit/eval-last-sexp-in-comment
       #'eval-last-sexp))))

More Repositories

1

you-get_install

一键安装 you-get 到 windows
Shell
157
star
2

nix-gccemacs-darwin

Build gccemacs for macOS 10.15 or newer using Nix.
Nix
39
star
3

acm-terminal

Patch for LSP bridge acm on Terminal
Emacs Lisp
36
star
4

elpl

Provides a simple interface to evaluating Emacs Lisp expressions but without contaminating current Emacs.
Emacs Lisp
33
star
5

nerd-fonts.el

Emacs nerd-fonts (https://github.com/ryanoasis/nerd-fonts) utilities.
Emacs Lisp
32
star
6

emacsq-sh

Helper script to run `emacs -Q`
Shell
26
star
7

with-proxy.el

Evaluate expressions with proxy
Emacs Lisp
24
star
8

shim.el

Emacs integration for rbenv/pyenv/nodenv/jenv/goenv
Emacs Lisp
18
star
9

homebrew-ccls

Homebrew formula to install [ccls](https://github.com/MaskRay/ccls)
Ruby
17
star
10

multi-translate.el

Translate word or region at point with multiple translation services.
Emacs Lisp
17
star
11

with-emacs.el

Evaluate expressions in a separate Emacs process
Emacs Lisp
16
star
12

psearch.el

Pcase based search for Emacs Lisp
Emacs Lisp
16
star
13

whitespace4r.el

Emacs minor mode to show whitespace for selected region.
Emacs Lisp
15
star
14

playonline.el

Play code with online playgrounds
Emacs Lisp
13
star
15

nerd-icons.el

Emacs Lisp
12
star
16

jagger

Move/swap things more conveniently in Emacs
Emacs Lisp
12
star
17

vterm-capf

Vterm `completion-at-point` facilities.
Emacs Lisp
10
star
18

dom-select.el

CSS-style selector based on dom.el
Emacs Lisp
8
star
19

numbering.el

Provide features like rectangle-number-lines but more interactive.
Emacs Lisp
6
star
20

nix-gccemacs-sierra

Build gccemacs for macOS 10.12 Sierra using Nix.
Nix
5
star
21

lisp-keyword-indent.el

Rectify the indent of keyword for Emacs Lisp
Emacs Lisp
5
star
22

async-copy-file.el

Copy files asynchronously in Emacs
Emacs Lisp
4
star
23

fuck.el

Convert full-width/half-width punctuations before or around point.
Emacs Lisp
4
star
24

lsp-dart

**DEPRECATED** lsp-mode (https://github.com/emacs-lsp/lsp-mode) has built-in support for dart
Emacs Lisp
3
star
25

irregular-plist.el

Utilities for irregular plist
Emacs Lisp
2
star
26

plutil.el

Apple plist file utilities
Emacs Lisp
2
star
27

ob-playonline

Online src_block execution for org-babel
Emacs Lisp
2
star
28

helm-pinyin

Pinyin support for helm-find-files and helm-mini
Emacs Lisp
2
star
29

shadowsocks_install

Auto Install Shadowsocks Server via Fabric.
Shell
1
star
30

emfd

Emacs-based wrapper around mdfind
Emacs Lisp
1
star
31

homebrew-cquery

Homebrew formula to install [cquery](https://github.com/cquery-project/cquery).
Ruby
1
star
32

ob-with-emacs

Execute elisp code blocks in a separate Emacs process
Emacs Lisp
1
star
33

nodenv.el

**DEPRECATED** Please use https://github.com/twlz0ne/shim.el instead
Emacs Lisp
1
star
34

test-ff-pr

Test fast forward PR.
1
star
35

homebrew-casks4hs

Homebrew casks for High Sierra
Ruby
1
star
36

helm-symbol-hint

Show symbol hint for Helm
Emacs Lisp
1
star
37

unzip.el

Unzip wrapper for Emacs
Emacs Lisp
1
star
38

subjust

A simple command line tool for subtitle adjustment
C
1
star
39

latin2uyghur.el

Uyghur <==> Latin Alphabet Converter
Emacs Lisp
1
star