This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+lang/perl5/funcs.el
Jim Pudar 0f40fe9df2 Improve point handling in perltidy functions
This moves the point and content of the window close to where you were
before you ran perltidy. Of course if perltidy adds or removes a
significant amount of characters / lines as part of tidying, the point
will be moved by that amount. However in practice this I've found this
to be close enough.
2019-06-21 19:04:34 +02:00

58 lines
1.9 KiB
EmacsLisp

;;; funcs.el --- Perl5 Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Eivind Fonn <evfonn@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//perl5-smartparens-enable ()
(define-key cperl-mode-map "{" nil))
(defun spacemacs//perl5-spartparens-disable ()
(define-key cperl-mode-map "{" 'cperl-electric-lbrace))
(defun spacemacs/perltidy-format ()
"Format Perl code with perltidy.
If region is active, operate on it, else operate on line."
(interactive)
(let ((old-point (point))
(pos
(if (use-region-p)
(cons (region-beginning)
(if (char-equal ?\n (char-before (region-end)))
(region-end)
(save-excursion ;; must including terminating newline
(goto-char (region-end))
(1+ (line-end-position)))))
(cons (line-beginning-position)
(1+ (line-end-position))))))
(apply #'call-process-region (car pos) (cdr pos) perl5-perltidy-executable t '(t nil)
"--quiet"
"--standard-error-output"
perl5-perltidy-options)
(goto-char old-point)))
(defun spacemacs/perltidy-format-buffer ()
"Format current buffer with perltidy."
(interactive)
(let ((old-point (point))
(old-window-start (window-start)))
(mark-whole-buffer)
(spacemacs/perltidy-format)
(goto-char old-point)
(set-window-start (selected-window) old-window-start)))
(defun spacemacs/perltidy-format-function ()
"Format current function with perltidy."
(interactive)
(let ((old-point (point))
(old-window-start (window-start)))
(mark-defun)
(spacemacs/perltidy-format)
(goto-char old-point)
(set-window-start (selected-window) old-window-start)))