Klimi's new dotfiles with stow.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

130 wiersze
4.5 KiB

4 lat temu
  1. ;;; haskell-move-nested.el --- Change the column of text nested below a line -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010 Chris Done
  3. ;; Author: Chris Done <chrisdone@gmail.com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software: you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation, either version 3 of
  8. ;; the License, or (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be
  10. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  11. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. ;; PURPOSE. See the GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public
  14. ;; License along with this program. If not, see
  15. ;; <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This module is intended for Haskell mode users, but is
  18. ;; independent of Haskell mode.
  19. ;; Example usage:
  20. ;; (define-key haskell-mode-map (kbd "C-,") 'haskell-move-nested-left)
  21. ;; (define-key haskell-mode-map (kbd "C-.") 'haskell-move-nested-right)
  22. ;;; Code:
  23. ;;;###autoload
  24. (defun haskell-move-nested (cols)
  25. "Shift the nested off-side-rule block adjacent to point by COLS columns to the right.
  26. In Transient Mark mode, if the mark is active, operate on the contents
  27. of the region instead.
  28. "
  29. (save-excursion
  30. (if (and transient-mark-mode mark-active)
  31. (progn
  32. (indent-rigidly (region-beginning) (region-end) cols)
  33. (setq deactivate-mark nil))
  34. (let ((region (haskell-move-nested-region)))
  35. (when region
  36. (indent-rigidly (car region) (cdr region) cols))))))
  37. ;;;###autoload
  38. (defun haskell-move-nested-right (cols)
  39. "Increase indentation of the following off-side-rule block adjacent to point.
  40. Use a numeric prefix argument to indicate amount of indentation to apply.
  41. In Transient Mark mode, if the mark is active, operate on the contents
  42. of the region instead."
  43. (interactive "p")
  44. (haskell-move-nested cols)
  45. )
  46. ;;;###autoload
  47. (defun haskell-move-nested-left (cols)
  48. "Decrease indentation of the following off-side-rule block adjacent to point.
  49. Use a numeric prefix argument to indicate amount of indentation to apply.
  50. In Transient Mark mode, if the mark is active, operate on the contents
  51. of the region instead."
  52. (interactive "p")
  53. (haskell-move-nested (- cols))
  54. )
  55. (defun haskell-move-nested-region ()
  56. "Infer region off-side-rule block adjacent to point.
  57. Used by `haskell-move-nested'.
  58. "
  59. (save-excursion
  60. (let ((starting-level (current-column)))
  61. (forward-line)
  62. (let ((current-level (haskell-move-nested-indent-level)))
  63. (let ((start-point (line-beginning-position))
  64. (start-end-point (line-end-position))
  65. (end-point nil)
  66. (last-line 0))
  67. (forward-line)
  68. (while (and (not (= (line-beginning-position) last-line))
  69. (or (> (haskell-move-nested-indent-level) starting-level)
  70. (and (> current-level starting-level)
  71. (>= (haskell-move-nested-indent-level) current-level))))
  72. (setq last-line (line-beginning-position))
  73. (setq end-point (line-end-position))
  74. (forward-line))
  75. (cons start-point (or end-point
  76. start-end-point)))))))
  77. (defun haskell-move-nested-indent-level ()
  78. (max
  79. 0
  80. (1- (length
  81. (buffer-substring-no-properties
  82. (line-beginning-position)
  83. (or (save-excursion (goto-char (line-beginning-position))
  84. (search-forward-regexp "[^ ]" (line-end-position) t 1))
  85. (line-beginning-position)))))))
  86. (defun haskell-kill-nested ()
  87. "Kill the nested region after point."
  88. (interactive)
  89. (let ((start (point))
  90. (reg (save-excursion
  91. (search-backward-regexp "^[ ]+" (line-beginning-position) t 1)
  92. (search-forward-regexp "[^ ]" (line-end-position) t 1)
  93. (haskell-move-nested-region))))
  94. (kill-region start (cdr reg))))
  95. (defun haskell-delete-nested ()
  96. "Kill the nested region after point."
  97. (interactive)
  98. (let ((start (point))
  99. (reg (save-excursion
  100. (search-backward-regexp "^[ ]+" (line-beginning-position) t 1)
  101. (search-forward-regexp "[^ ]" (line-end-position) t 1)
  102. (haskell-move-nested-region))))
  103. (delete-region start (cdr reg))))
  104. (provide 'haskell-move-nested)
  105. ;;; haskell-move-nested.el ends here