mirror of
https://github.com/lliding/ld-emacs.git
synced 2025-10-13 05:23:05 +00:00
168 lines
5.2 KiB
EmacsLisp
168 lines
5.2 KiB
EmacsLisp
;; -*- coding: utf-8; -*-
|
|
;;; Require:
|
|
|
|
;;; Code:
|
|
(defun insert-line-number (beg end &optional start-line)
|
|
"Insert line numbers into buffer."
|
|
(interactive "r")
|
|
(save-excursion
|
|
(let ((max (count-lines beg end))
|
|
(line (or start-line 1))
|
|
(counter 1))
|
|
(goto-char beg)
|
|
(while (<= counter max)
|
|
(insert (format "%0d " line))
|
|
(beginning-of-line 2)
|
|
(cl-incf line)
|
|
(cl-incf counter)))))
|
|
|
|
(defun insert-line-number+ ()
|
|
"Insert line number into buffer."
|
|
(interactive)
|
|
(if mark-active
|
|
(insert-line-number (region-beginning) (region-end) (read-number "Start line: "))
|
|
(insert-line-number (point-min) (point-max))))
|
|
|
|
(defun strip-blank-lines()
|
|
"Strip all blank lines in select area of buffer,
|
|
if not select any area, then strip all blank lines of buffer."
|
|
(interactive)
|
|
(strip-regular-expression-string "^[ \t]*\n")
|
|
(message "Blanks line striped. ^_^"))
|
|
|
|
(defun strip-line-number()
|
|
"Strip all line number in select area of buffer,
|
|
if not select any area, then strip all line number of buffer."
|
|
(interactive)
|
|
(strip-regular-expression-string "^[0-9]+ ")
|
|
(message "Line number striped. ^_^"))
|
|
|
|
(defun strip-regular-expression-string (regular-expression)
|
|
"Strip all string that match REGULAR-EXPRESSION in select area of buffer.
|
|
If not select any area, then strip current buffer"
|
|
(interactive)
|
|
(let ((begin (point-min))
|
|
(end (point-max)))
|
|
(if mark-active
|
|
(setq begin (region-beginning)
|
|
end (region-end)))
|
|
(save-excursion
|
|
(goto-char end)
|
|
(while (and (> (point) begin)
|
|
(re-search-backward regular-expression nil t))
|
|
(replace-match "" t t)))))
|
|
|
|
(defun indent-comment-buffer ()
|
|
"Indent comment of buffer."
|
|
(interactive)
|
|
(indent-comment-region (point-min) (point-max)))
|
|
|
|
(defun indent-comment-region (start end)
|
|
"Indent region."
|
|
(interactive "r")
|
|
(save-excursion
|
|
(setq end (copy-marker end))
|
|
(goto-char start)
|
|
(while (< (point) end)
|
|
(if (comment-search-forward end t)
|
|
(comment-indent)
|
|
(goto-char end)))))
|
|
|
|
(defun capitalize-one-char (arg)
|
|
"Change the letter pointed by the cursor to uppercase."
|
|
(interactive "P")
|
|
(upcase-region (point) (+ (point) (or arg 1)))
|
|
(forward-char (or arg 1)))
|
|
|
|
(defun lowercase-one-char (arg)
|
|
"Change the letter pointed by the cursor to lowercase."
|
|
(interactive "P")
|
|
(downcase-region (point) (+ (point) (or arg 1)))
|
|
(forward-char (or arg 1)))
|
|
|
|
(defun delete-chars-hungry-forward (&optional reverse)
|
|
"Delete chars forward use `hungry' style.
|
|
Optional argument REVERSE default is delete forward, if reverse is non-nil delete backward."
|
|
(delete-region
|
|
(point)
|
|
(progn
|
|
(if reverse
|
|
(skip-chars-backward " \t\n\r")
|
|
(skip-chars-forward " \t\n\r"))
|
|
(point))))
|
|
|
|
(defun delete-chars-hungry-backward ()
|
|
"Delete chars backward use `hungry' style."
|
|
(delete-chars-hungry-forward t))
|
|
|
|
(defun reverse-chars-in-region (start end)
|
|
"Reverse the region character by character without reversing lines."
|
|
(interactive "r")
|
|
(let ((str (buffer-substring start end)))
|
|
(delete-region start end)
|
|
(dolist (line (split-string str "\n"))
|
|
(let ((chars (mapcar (lambda (c)
|
|
(or (matching-paren c)
|
|
c))
|
|
(reverse (append line nil)))))
|
|
(when chars
|
|
(apply 'insert chars))
|
|
(newline)))))
|
|
|
|
(defun underline-line-with (char)
|
|
"Insert some char below at current line."
|
|
(interactive "cType one char: ")
|
|
(save-excursion
|
|
(let ((length (- (point-at-eol) (point-at-bol))))
|
|
(end-of-line)
|
|
(insert "\n")
|
|
(insert (make-string length char)))))
|
|
|
|
(defun prettyfy-string (string &optional after)
|
|
"Strip starting and ending whitespace and pretty `STRING'.
|
|
Replace any chars after AFTER with '...'.
|
|
Argument STRING the string that need pretty."
|
|
(let ((replace-map (list
|
|
(cons "^[ \t]*" "")
|
|
(cons "[ \t]*$" "")
|
|
(cons (concat "^\\(.\\{"
|
|
(or (number-to-string after) "10")
|
|
"\\}\\).*")
|
|
"\\1..."))))
|
|
(dolist (replace replace-map)
|
|
(when (string-match (car replace) string)
|
|
(setq string (replace-match (cdr replace) nil nil string))))
|
|
string))
|
|
|
|
(defun forward-button-with-line-begin ()
|
|
"Move to next button with line begin."
|
|
(interactive)
|
|
(call-interactively 'forward-button)
|
|
(while (not (bolp))
|
|
(call-interactively 'forward-button)))
|
|
|
|
(defun backward-button-with-line-begin ()
|
|
"Move to previous button with line begin."
|
|
(interactive)
|
|
(call-interactively 'backward-button)
|
|
(while (not (bolp))
|
|
(call-interactively 'backward-button)))
|
|
|
|
(defun only-comment-p ()
|
|
"Return t if current line only contains comment. Otherwise return nil."
|
|
(interactive)
|
|
(save-excursion
|
|
(beginning-of-line)
|
|
(if (search-forward comment-start (line-end-position) t)
|
|
(progn
|
|
(backward-char (length comment-start))
|
|
(equal (point)
|
|
(progn
|
|
(back-to-indentation)
|
|
(point))))
|
|
nil)))
|
|
|
|
(provide 'ld-tools)
|
|
|
|
;;; ld-tools.el ends here
|