Skip to content

Commit

Permalink
Introduce ignore region customize variable
Browse files Browse the repository at this point in the history
  • Loading branch information
syohex committed Nov 13, 2023
1 parent b1a862f commit 11b33dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
`org-open-at-point-functions`, allowing other libraries to
handle links specially. [GH-780][]
- Support media handler for images and drag and drop images [GH-804][]
- Introduce customizable variable `markdown-ignore-region` [GH-812][]

* Bug fixes:
- Don't highlight superscript/subscript in math inline/block [GH-802][]
Expand All @@ -19,6 +20,7 @@
[gh-802]: https://github.com/jrblevin/markdown-mode/issues/802
[gh-804]: https://github.com/jrblevin/markdown-mode/issues/804
[gh-805]: https://github.com/jrblevin/markdown-mode/issues/805
[gh-812]: https://github.com/jrblevin/markdown-mode/pull/812

# Markdown Mode 2.6

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ provides an interface to all of the possible customizations:
* `markdown-fontify-whole-heading-line` - font lock for highlighting
the whole line for headings.(default: `nil`)

* `markdown-ignore-region` - ignore region for inserting commands.
(default: `nil`)

Additionally, the faces used for syntax highlighting can be modified to
your liking by issuing <kbd>M-x customize-group RET markdown-faces</kbd>
or by using the "Markdown Faces" link at the bottom of the mode
Expand Down
40 changes: 25 additions & 15 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,13 @@ markdown-header-face-* faces."
:safe 'booleanp
:package-version '(markdown-mode . "2.5"))

(defcustom markdown-ignore-region nil
"Non-nil means temporarily disable the active region."
:group 'markdown
:type 'boolean
:safe 'booleanp
:package-version '(markdown-mode . "2.7"))


;;; Markdown-Specific `rx' Macro ==============================================

Expand Down Expand Up @@ -3635,6 +3642,9 @@ SEQ may be an atom or a sequence."

;;; Element Insertion =========================================================

(defsubst markdown--region-active-p ()
(and (not markdown-ignore-region) (use-region-p)))

(defun markdown-ensure-blank-line-before ()
"If previous line is not already blank, insert a blank line before point."
(unless (bolp) (insert "\n"))
Expand Down Expand Up @@ -3665,7 +3675,7 @@ and S1 and S2 were only inserted."
b end
new-point (+ (point) (length s1))))
;; Active region
((use-region-p)
((markdown--region-active-p)
(setq a (region-beginning)
b (region-end)
new-point (+ (point) (length s1))))
Expand Down Expand Up @@ -3760,7 +3770,7 @@ prefixed with an integer from 1 to the length of

(defun markdown--insert-common (start-delim end-delim regex start-group end-group face
&optional skip-space)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(let* ((bounds (markdown-unwrap-things-in-region
(region-beginning) (region-end)
Expand Down Expand Up @@ -3828,7 +3838,7 @@ fragment. If the point is at a word, make the word an inline
code fragment. Otherwise, simply insert code delimiters and
place the point in between them."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(let ((bounds (markdown-unwrap-things-in-region
(region-beginning) (region-end)
Expand All @@ -3845,7 +3855,7 @@ If there is an active region, use the region. If the point is at
a word, use the word. Otherwise, simply insert <kbd> tags and
place the point in between them."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(let ((bounds (markdown-unwrap-things-in-region
(region-beginning) (region-end)
Expand Down Expand Up @@ -3971,7 +3981,7 @@ When IMAGE is non-nil, insert an image. Otherwise, insert a link.
This is an internal function called by
`markdown-insert-link' and `markdown-insert-image'."
(cl-multiple-value-bind (begin end text uri ref title)
(if (use-region-p)
(if (markdown--region-active-p)
;; Use region as either link text or URL as appropriate.
(let ((region (buffer-substring-no-properties
(region-beginning) (region-end))))
Expand Down Expand Up @@ -4101,7 +4111,7 @@ at a URI, wrap it with angle brackets. If the point is at an
inline URI, remove the angle brackets. Otherwise, simply insert
angle brackets place the point between them."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(let ((bounds (markdown-unwrap-things-in-region
(region-beginning) (region-end)
Expand All @@ -4121,7 +4131,7 @@ If the point is at a word, use the word as the link text. If
there is no active region and the point is not at word, simply
insert link markup."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(markdown-wrap-or-insert "[[" "]]" nil (region-beginning) (region-end))
;; Markup removal, wiki link at at point, or empty markup insertion
Expand Down Expand Up @@ -4159,7 +4169,7 @@ header will be inserted."
(setq level (min (max (or level 1) 1) (if setext 2 6)))
;; Determine header text if not given
(when (null text)
(if (use-region-p)
(if (markdown--region-active-p)
;; Active region
(setq text (delete-and-extract-region (region-beginning) (region-end)))
;; No active region
Expand Down Expand Up @@ -4302,7 +4312,7 @@ Also see `markdown-pre-indentation'."
If Transient Mark mode is on and a region is active, it is used as
the blockquote text."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
(markdown-blockquote-region (region-beginning) (region-end))
(markdown-ensure-blank-line-before)
(insert (markdown-blockquote-indentation (point)) "> ")
Expand Down Expand Up @@ -4361,7 +4371,7 @@ Also see `markdown-blockquote-indentation'."
If Transient Mark mode is on and a region is active, it is marked
as preformatted text."
(interactive)
(if (use-region-p)
(if (markdown--region-active-p)
(markdown-pre-region (region-beginning) (region-end))
(markdown-ensure-blank-line-before)
(insert (markdown-pre-indentation (point)))
Expand Down Expand Up @@ -4544,7 +4554,7 @@ code block in an indirect buffer after insertion."
lang)))
(let ((gfm-open-brace (if markdown-code-block-braces "{" ""))
(gfm-close-brace (if markdown-code-block-braces "}" "")))
(if (use-region-p)
(if (markdown--region-active-p)
(let* ((b (region-beginning)) (e (region-end)) end
(indent (progn (goto-char b) (current-indentation))))
(goto-char e)
Expand Down Expand Up @@ -4630,7 +4640,7 @@ element. More details here https://developer.mozilla.org/en-US/docs/Web/HTML/Ele
(details-close-tag "</details>")
(summary-open-tag "<summary>")
(summary-close-tag " </summary>"))
(if (use-region-p)
(if (markdown--region-active-p)
(let* ((b (region-beginning))
(e (region-end))
(indent (progn (goto-char b) (current-indentation))))
Expand Down Expand Up @@ -5109,7 +5119,7 @@ before the current point, then outdent the line one level.
Otherwise, do normal delete by repeating
`backward-delete-char-untabify' ARG times."
(interactive "*p")
(if (use-region-p)
(if (markdown--region-active-p)
(backward-delete-char-untabify arg)
(let ((cur-pos (current-column))
(start-of-indention (save-excursion
Expand Down Expand Up @@ -5262,7 +5272,7 @@ Return nil if markup was complete and non-nil if markup was completed."
Handle all objects in `markdown-complete-alist', in order.
See `markdown-complete-at-point' and `markdown-complete-region'."
(interactive "*")
(if (use-region-p)
(if (markdown--region-active-p)
(markdown-complete-region (region-beginning) (region-end))
(markdown-complete-at-point)))

Expand Down Expand Up @@ -7422,7 +7432,7 @@ Return the name of the output buffer used."
(command (car-safe commands))
(command-args (cdr-safe commands))
begin-region end-region)
(if (use-region-p)
(if (markdown--region-active-p)
(setq begin-region (region-beginning)
end-region (region-end))
(setq begin-region (point-min)
Expand Down
14 changes: 13 additions & 1 deletion tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,19 @@ Extracts region from BEGIN to END and inserts in OUTPUT-BUFFER."
(should (eq font-lock-multiline t))
(should (eq major-mode 'markdown-mode))))

;;; Element insertion tests:
;;; Utility tests:

(ert-deftest test-markdown-mode/active-region ()
(markdown-test-string "hello world"
(transient-mark-mode)
(push-mark (point) t t)
(goto-char 5)
(should (markdown--region-active-p))

(let ((markdown-ignore-region t))
(should-not (markdown--region-active-p)))))

;;; ELEMENT insertion tests:

(ert-deftest test-markdown-insertion/blank-line-before-1 ()
"Test function `markdown-ensure-blank-line-before' at beginning of line."
Expand Down

0 comments on commit 11b33dc

Please sign in to comment.