Emacs: Use 'use-region-p' and 'set-marker' instead of altering the mark
latex-itemize should be more reliable and will indent the result.master
parent
7e44e34761
commit
3f0afafd5a
|
@ -1,23 +1,21 @@
|
||||||
;; Functions
|
;; Functions
|
||||||
|
|
||||||
;; Notes on mark and region: to get a consistent behaviour whether Transient
|
;; Notes on mark and region: to get a consistent behaviour regardless of Transient
|
||||||
;; mode is on or off, check `mark-active'. It will work as expected if
|
;; mode, check `(use-region-p)'. It will work as expected if
|
||||||
;; transient. If not, it will always be true as soon as the mark has been set
|
;; transient. If not, it will always be true as soon as the mark has been set
|
||||||
;; once; so you need to make sure the mark is set as you want beforehand (e.g.
|
;; once; so you need to make sure the mark is set as you want beforehand (e.g.
|
||||||
;; whole buffer, single line...). This is the behaviour of `sort-lines'.
|
;; whole buffer, single line...). This is the behaviour of `sort-lines'.
|
||||||
;;
|
;;
|
||||||
;; The clean way to get region boundaries and fallback on buffer:
|
;; The clean way to get static region boundaries and fallback on buffer boundaries:
|
||||||
; (let (start end)
|
; (let (start end)
|
||||||
; (if mark-active
|
; (if (use-region-p)
|
||||||
; (setq start (region-beginning) end (region-end))
|
; (setq start (region-beginning) end (region-end))
|
||||||
; (setq start (point-min) end (point-max)))
|
; (setq start (point-min) end (point-max)))
|
||||||
;
|
;
|
||||||
;; If several commands act on region and the region size/pos is susceptible to change:
|
;; If several commands act on region and the region size/pos is susceptible to change:
|
||||||
; (save-mark-and-excursion
|
; (let ((start (set-marker (make-marker) (if (use-region-p) (region-beginning) (point-min))))
|
||||||
; (unless mark-active
|
; (end (set-marker (make-marker) (if (use-region-p) (region-end) (point-end)))))
|
||||||
; (mark-whole-buffer))
|
;
|
||||||
;; Then call (region-beginning) and (region-end)
|
|
||||||
;;
|
|
||||||
;; For commands that only work on regions:
|
;; For commands that only work on regions:
|
||||||
; (defun count-lines-region (start end)
|
; (defun count-lines-region (start end)
|
||||||
; "Print number of lines and characters in the region."
|
; "Print number of lines and characters in the region."
|
||||||
|
@ -56,11 +54,11 @@ there's a region, all lines that region covers will be duplicated."
|
||||||
end
|
end
|
||||||
(origin (point))
|
(origin (point))
|
||||||
(auto-fill-p (symbol-value 'auto-fill-function)))
|
(auto-fill-p (symbol-value 'auto-fill-function)))
|
||||||
(if (and mark-active (> (point) (mark)))
|
(when (and (use-region-p) (> (point) (mark)))
|
||||||
(exchange-point-and-mark))
|
(exchange-point-and-mark))
|
||||||
(setq beg (line-beginning-position))
|
(setq beg (line-beginning-position))
|
||||||
(if mark-active
|
(when (use-region-p)
|
||||||
(exchange-point-and-mark))
|
(exchange-point-and-mark))
|
||||||
(setq end (line-end-position))
|
(setq end (line-end-position))
|
||||||
(let ((region (buffer-substring-no-properties beg end)))
|
(let ((region (buffer-substring-no-properties beg end)))
|
||||||
(auto-fill-mode -1)
|
(auto-fill-mode -1)
|
||||||
|
@ -91,12 +89,8 @@ TO-STRING."
|
||||||
(interactive)
|
(interactive)
|
||||||
(unless regex (setq regex "\\([\"\\\\]\\)"))
|
(unless regex (setq regex "\\([\"\\\\]\\)"))
|
||||||
(unless to-string (setq to-string "\\\\\\1"))
|
(unless to-string (setq to-string "\\\\\\1"))
|
||||||
(save-excursion
|
(while (re-search-forward regex (if (use-region-p) (region-end) (point-max)) t)
|
||||||
(while (re-search-forward
|
(replace-match to-string)))
|
||||||
regex
|
|
||||||
(if mark-active (region-end) (point-max))
|
|
||||||
t)
|
|
||||||
(replace-match to-string))))
|
|
||||||
|
|
||||||
(defun eval-and-replace ()
|
(defun eval-and-replace ()
|
||||||
"Replace the last sexp with its value."
|
"Replace the last sexp with its value."
|
||||||
|
@ -342,10 +336,8 @@ WARNING: this may slow down editing on big files."
|
||||||
Tabify if `indent-tabs-mode' is true, otherwise use spaces.
|
Tabify if `indent-tabs-mode' is true, otherwise use spaces.
|
||||||
Work on buffer or region. Require `tabify-leading'."
|
Work on buffer or region. Require `tabify-leading'."
|
||||||
(interactive)
|
(interactive)
|
||||||
(let (start end)
|
(let ((start (set-marker (make-marker) (if (use-region-p) (region-beginning) (point-min))))
|
||||||
(if mark-active
|
(end (set-marker (make-marker) (if (use-region-p) (region-end) (point-end)))))
|
||||||
(setq start (region-beginning) end (region-end))
|
|
||||||
(setq start (point-min) end (point-max)))
|
|
||||||
(if indent-tabs-mode
|
(if indent-tabs-mode
|
||||||
(tabify-leading)
|
(tabify-leading)
|
||||||
(untabify start end))
|
(untabify start end))
|
||||||
|
@ -396,15 +388,12 @@ If REVERSE it t, move to previes placeholder."
|
||||||
"Remove trailing white space, then duplicate lines, then sort the result.
|
"Remove trailing white space, then duplicate lines, then sort the result.
|
||||||
Do not fold case with \\[universal-argument] or non-nil ARG."
|
Do not fold case with \\[universal-argument] or non-nil ARG."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
;; We use save-excursion here because the region boundaries change during
|
(let ((start (set-marker (make-marker) (if (use-region-p) (region-beginning) (point-min))))
|
||||||
;; execution, so it's more convenient to track them with region functions.
|
(end (set-marker (make-marker) (if (use-region-p) (region-end) (point-end)))))
|
||||||
(save-mark-and-excursion
|
|
||||||
(unless mark-active
|
|
||||||
(mark-whole-buffer))
|
|
||||||
(let ((sort-fold-case (if arg nil t)))
|
(let ((sort-fold-case (if arg nil t)))
|
||||||
(delete-trailing-whitespace (region-beginning) (region-end))
|
(delete-trailing-whitespace start end)
|
||||||
(delete-duplicate-lines (region-beginning) (region-end))
|
(delete-duplicate-lines start end)
|
||||||
(sort-lines nil (region-beginning) (region-end)))))
|
(sort-lines nil start end))))
|
||||||
|
|
||||||
(defun spawn-terminal ()
|
(defun spawn-terminal ()
|
||||||
"Spawn terminal asynchronously.
|
"Spawn terminal asynchronously.
|
||||||
|
@ -439,7 +428,7 @@ Works on whole buffer if region is unactive."
|
||||||
(interactive)
|
(interactive)
|
||||||
(require 'tabify) ; Need this to initialize `tabify-regexp'.
|
(require 'tabify) ; Need this to initialize `tabify-regexp'.
|
||||||
(let ((tabify-regexp-old tabify-regexp) start end)
|
(let ((tabify-regexp-old tabify-regexp) start end)
|
||||||
(if mark-active
|
(if (use-region-p)
|
||||||
(setq start (region-beginning) end (region-end))
|
(setq start (region-beginning) end (region-end))
|
||||||
(setq start (point-min) end (point-max)))
|
(setq start (point-min) end (point-max)))
|
||||||
(unwind-protect
|
(unwind-protect
|
||||||
|
|
|
@ -67,7 +67,7 @@ restored."
|
||||||
(let (status
|
(let (status
|
||||||
start end
|
start end
|
||||||
(formatbuf (get-buffer-create "*C format buffer*")))
|
(formatbuf (get-buffer-create "*C format buffer*")))
|
||||||
(if mark-active
|
(if (use-region-p)
|
||||||
(setq start (region-beginning) end (region-end))
|
(setq start (region-beginning) end (region-end))
|
||||||
(setq start (point-min) end (point-max)))
|
(setq start (point-min) end (point-max)))
|
||||||
(setq status
|
(setq status
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
;; Go
|
;; Go
|
||||||
|
|
||||||
;; TODO: Report missing `use-local-map` upstream.
|
;; TODO: Report missing `use-local-map' upstream.
|
||||||
;; https://github.com/dominikh/go-mode.el/issues/191
|
;; https://github.com/dominikh/go-mode.el/issues/191
|
||||||
(use-local-map go-mode-map)
|
(use-local-map go-mode-map)
|
||||||
|
|
||||||
|
|
|
@ -30,16 +30,14 @@
|
||||||
;; Functions
|
;; Functions
|
||||||
|
|
||||||
(defun latex-itemize ()
|
(defun latex-itemize ()
|
||||||
"Prepend \\item to the beginning of the line if not already
|
"Itemize current line or lines in region.
|
||||||
there, otherwise insert it on next line. On region, append
|
Prepend \\item to the beginning of the lines if not already
|
||||||
\item to every line and surround the region by an `itemize'
|
there, otherwise insert it on next line. If region, surround it
|
||||||
environment. If bound to M-RET, you can then easily apply this
|
by an {itemize} environment."
|
||||||
command on the paragraph at point with M-h M-RET."
|
|
||||||
(interactive)
|
(interactive)
|
||||||
(let (min max case-fold-search)
|
(let (min max)
|
||||||
(if (not (region-active-p))
|
(if (not (use-region-p))
|
||||||
|
(if (string-match "\\item" (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
|
||||||
(if (string-match "\\item" (buffer-substring (line-beginning-position) (line-end-position)))
|
|
||||||
(progn
|
(progn
|
||||||
(goto-char (line-end-position))
|
(goto-char (line-end-position))
|
||||||
(newline)
|
(newline)
|
||||||
|
@ -47,17 +45,19 @@
|
||||||
(goto-char (line-beginning-position))
|
(goto-char (line-beginning-position))
|
||||||
(insert "\\item")
|
(insert "\\item")
|
||||||
(just-one-space))
|
(just-one-space))
|
||||||
|
;; On region:
|
||||||
(replace-regexp "^ *\\([^
|
(let ((end-marker (set-marker (make-marker) (region-end))))
|
||||||
]\\)" "\\\\item \\1" nil (region-beginning) (region-end))
|
(goto-char (region-beginning))
|
||||||
(goto-char (region-end))
|
(goto-char (line-beginning-position))
|
||||||
(goto-char (line-end-position))
|
(insert "\\begin{itemize}")
|
||||||
(newline)
|
(newline-and-indent)
|
||||||
(insert "\\end{itemize}")
|
(while (and (< (line-beginning-position) end-marker) (not (eobp)))
|
||||||
(goto-char (region-beginning))
|
(insert "\\item")
|
||||||
(goto-char (line-beginning-position))
|
(just-one-space)
|
||||||
(insert "\\begin{itemize}")
|
(indent-according-to-mode)
|
||||||
(newline))))
|
(forward-line))
|
||||||
|
(insert "\\end{itemize}")
|
||||||
|
(newline-and-indent)))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; LaTeX setup
|
;; LaTeX setup
|
||||||
|
|
|
@ -55,7 +55,7 @@ called with universal argument, insert result at point. If IN or
|
||||||
OUT are nil, use `itranslate-lang-input' and
|
OUT are nil, use `itranslate-lang-input' and
|
||||||
`itranslate-lang-output' respectively."
|
`itranslate-lang-output' respectively."
|
||||||
(interactive
|
(interactive
|
||||||
(list (if mark-active (mark) (error "Mark not set"))
|
(list (if (use-region-p) (mark) (error "Mark not set"))
|
||||||
(point)
|
(point)
|
||||||
(equal current-prefix-arg '(4))))
|
(equal current-prefix-arg '(4))))
|
||||||
(when (called-interactively-p 'any) (itranslate-init))
|
(when (called-interactively-p 'any) (itranslate-init))
|
||||||
|
@ -78,7 +78,7 @@ region. This calls the `itranslate' function. Output result at
|
||||||
the end after an ' = ' separtor."
|
the end after an ' = ' separtor."
|
||||||
(interactive
|
(interactive
|
||||||
(list (line-number-at-pos (point))
|
(list (line-number-at-pos (point))
|
||||||
(line-number-at-pos (if mark-active (mark) (point))) nil nil))
|
(line-number-at-pos (if (use-region-p) (mark) (point))) nil nil))
|
||||||
(when (called-interactively-p 'any) (itranslate-init))
|
(when (called-interactively-p 'any) (itranslate-init))
|
||||||
(when (> beg end)
|
(when (> beg end)
|
||||||
(setq beg end)
|
(setq beg end)
|
||||||
|
|
Loading…
Reference in New Issue