Skip to content

Commit

Permalink
Update code for lexical-binding, cl-lib, and advice-add
Browse files Browse the repository at this point in the history
Activate `lexical-binding`.  Use `advice-add` rather than `defadvice`.
Avoid deprectaed `point-at-bol/eol`.  Prefer #' to quote function names.
Fix a few docstrings to silence compiler warnings.

* cc-mode-expansions.el (er/c-define-construct): Fix ' in docstring.

* enh-ruby-mode-expansions.el: Require `expand-region-core`.

* er-basic-expansions.el (er/mark-next-accessor): Remove unused var
`symbol-regexp`.

* expand-region-core.el: Don't require `cl`.
(er--show-expansion-message): Declare it.
(er--first-invocation): Move before first use.
(er--expand-region-1): Remove unused var `set-mark-default-inactive`.
(er/set-temporary-overlay-map): Hoist definition outside of `if`.

* expand-region-custom.el (expand-region-preferred-python-mode): Fix
custom type.

* expand-region.el: Explicitly require Emacs-24.4.

* js-mode-expansions.el: Require `er-basic-expansions`.

* ruby-mode-expansions.el (expand-region-core): Require `cl-lib` rather
than `cl`.  Require `er-basic-expansions`.
(er/ruby-backward-up): Use `cl-loop`.
(er/get-ruby-block): Remove unused vars `beg` and `end`.

* the-org-mode-expansions.el (er-basic-expansions):
Require `er-basic-expansions`.

* yaml-mode-expansions.el: Re-add now that the author signed the paperwork.
  • Loading branch information
monnier committed Oct 15, 2023
1 parent bd23f6d commit 8493232
Show file tree
Hide file tree
Showing 30 changed files with 361 additions and 167 deletions.
20 changes: 10 additions & 10 deletions cc-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; cc-mode-expansions.el --- C-specific expansions for expand-region
;;; cc-mode-expansions.el --- C-specific expansions for expand-region -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2020 Free Software Foundation, Inc
;; Copyright (C) 2012-2023 Free Software Foundation, Inc

;; Author: François Févotte
;; Based on js-mode-expansions by: Magnar Sveen <magnars@gmail.com>
Expand Down Expand Up @@ -119,7 +119,7 @@ This function captures identifiers composed of multiple
doc "\n\n"
"This function tries to mark a region consisting of two parts:\n"
(format " - the first part is marked using `%s'\n" (symbol-name mark-first-part))
(format " - the second part is a block beginning with '%s'\n\n" open-brace)))
(format " - the second part is a block beginning with %S\n\n" open-brace)))
`(progn
(defun ,(intern (concat (symbol-name name) "-1")) ()
,(concat docstring
Expand Down Expand Up @@ -173,13 +173,13 @@ This function captures identifiers composed of multiple
er/c-mark-statement-block-1 er/c-mark-statement-block-2
er/c-mark-vector-access-1 er/c-mark-vector-access-2))))

(er/enable-mode-expansions 'c-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'c++-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'objc-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'java-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'idl-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'pike-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'awk-mode 'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'c-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'c++-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'objc-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'java-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'idl-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'pike-mode #'er/add-cc-mode-expansions)
(er/enable-mode-expansions 'awk-mode #'er/add-cc-mode-expansions)

(provide 'cc-mode-expansions)

Expand Down
8 changes: 4 additions & 4 deletions clojure-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; clojure-mode-expansions.el --- Clojure-specific expansions for expand-region
;;; clojure-mode-expansions.el --- Clojure-specific expansions for expand-region -*- lexical-binding: t; -*-

;; Copyright (C) 2011-2020 Free Software Foundation, Inc
;; Copyright (C) 2011-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand Down Expand Up @@ -98,8 +98,8 @@ If point is inside the parens, they will be marked first anyway."
er/mark-clj-set-literal
er/mark-clj-function-literal))))

(er/enable-mode-expansions 'clojure-mode 'er/add-clojure-mode-expansions)
(er/enable-mode-expansions 'nrepl-mode 'er/add-clojure-mode-expansions)
(er/enable-mode-expansions 'clojure-mode #'er/add-clojure-mode-expansions)
(er/enable-mode-expansions 'nrepl-mode #'er/add-clojure-mode-expansions)

(provide 'clojure-mode-expansions)

Expand Down
6 changes: 3 additions & 3 deletions cperl-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; cperl-mode-expansions.el --- perl-specific expansions for expand-region
;;; cperl-mode-expansions.el --- perl-specific expansions for expand-region -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2020 Free Software Foundation, Inc
;; Copyright (C) 2012-2023 Free Software Foundation, Inc

;; Author: Kang-min Liu <gugod@gugod.org>
;; Keywords: marking region cperl
Expand Down Expand Up @@ -60,7 +60,7 @@
er/mark-cperl-subroutine
))))

(er/enable-mode-expansions 'cperl-mode 'er/add-cperl-mode-expansions)
(er/enable-mode-expansions 'cperl-mode #'er/add-cperl-mode-expansions)

(provide 'cperl-mode-expansions)

Expand Down
6 changes: 3 additions & 3 deletions css-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; css-mode-expansions.el --- CSS-specific expansions for expand-region
;;; css-mode-expansions.el --- CSS-specific expansions for expand-region -*- lexical-binding: t; -*-

;; Copyright (C) 2011-2020 Free Software Foundation, Inc
;; Copyright (C) 2011-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand Down Expand Up @@ -45,7 +45,7 @@
er/try-expand-list
'(er/mark-css-declaration))))

(er/enable-mode-expansions 'css-mode 'er/add-css-mode-expansions)
(er/enable-mode-expansions 'css-mode #'er/add-css-mode-expansions)

(provide 'css-mode-expansions)

Expand Down
8 changes: 5 additions & 3 deletions enh-ruby-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; enh-ruby-mode-expansions.el --- Expansions for enh-ruby-mode
;;; enh-ruby-mode-expansions.el --- Expansions for enh-ruby-mode -*- lexical-binding: t; -*-

;; Copyright (C) 2011-2020 Free Software Foundation, Inc
;; Copyright (C) 2011-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand Down Expand Up @@ -28,6 +28,8 @@

;;; Code:

(require 'expand-region-core)

(defun er/add-enh-ruby-mode-expansions ()
"Adds Ruby-specific expansions for buffers in enh-ruby-mode"
(require 'ruby-mode-expansions)
Expand All @@ -37,6 +39,6 @@
'(er/mark-ruby-instance-variable
er/mark-ruby-block-up))))

(er/enable-mode-expansions 'enh-ruby-mode 'er/add-enh-ruby-mode-expansions)
(er/enable-mode-expansions 'enh-ruby-mode #'er/add-enh-ruby-mode-expansions)

(provide 'enh-ruby-mode-expansions)
10 changes: 5 additions & 5 deletions er-basic-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; er-basic-expansions.el --- Words, symbols, strings, et al
;;; er-basic-expansions.el --- Words, symbols, strings, et al -*- lexical-binding: t; -*-

;; Copyright (C) 2011-2020 Free Software Foundation, Inc
;; Copyright (C) 2011-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand Down Expand Up @@ -69,11 +69,11 @@ period and marks next symbol."
(when (use-region-p)
(when (< (point) (mark))
(exchange-point-and-mark))
(let ((symbol-regexp "\\s_\\|\\sw"))
;; (let ((symbol-regexp "\\s_\\|\\sw"))
(when (looking-at "\\.")
(forward-char 1)
(skip-syntax-forward "_w")
(exchange-point-and-mark)))))
(exchange-point-and-mark)))) ;; )

(defun er/mark-method-call ()
"Mark the current symbol (including dots) and then paren to closing paren."
Expand Down Expand Up @@ -116,7 +116,7 @@ period and marks next symbol."
"The char that is the current quote delimiter"
(nth 3 (syntax-ppss)))

(defalias 'er--point-inside-string-p 'er--current-quotes-char)
(defalias 'er--point-inside-string-p #'er--current-quotes-char)

(defun er--move-point-forward-out-of-string ()
"Move point forward until it exits the current quoted string."
Expand Down
6 changes: 3 additions & 3 deletions erlang-mode-expansions.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; erlang-mode-expansions.el --- Erlang-specific expansions for expand-region
;;; erlang-mode-expansions.el --- Erlang-specific expansions for expand-region -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2020 Free Software Foundation, Inc
;; Copyright (C) 2012-2023 Free Software Foundation, Inc

;; Author: Gleb Peregud
;; Based on python-mode-expansions by: Ivan Andrus <darthandrus@gmail.com>
Expand Down Expand Up @@ -40,7 +40,7 @@
'(erlang-mark-function
erlang-mark-clause))))

(er/enable-mode-expansions 'erlang-mode 'er/add-erlang-mode-expansions)
(er/enable-mode-expansions 'erlang-mode #'er/add-erlang-mode-expansions)

(provide 'erlang-mode-expansions)

Expand Down
52 changes: 28 additions & 24 deletions expand-region-core.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; expand-region-core.el --- Increase selected region by semantic units.
;;; expand-region-core.el --- Increase selected region by semantic units. -*- lexical-binding: t; -*-

;; Copyright (C) 2011-2020 Free Software Foundation, Inc
;; Copyright (C) 2011-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand All @@ -26,7 +26,6 @@

;;; Code:

(eval-when-compile (require 'cl))
(require 'expand-region-custom)
(declare-function er/expand-region "expand-region")

Expand All @@ -39,14 +38,18 @@
(defvar er--space-str " \t\n")
(defvar er--blank-list (append er--space-str nil))

(set-default 'er--show-expansion-message nil)
(defvar er--show-expansion-message nil)

(defvar er/try-expand-list nil
"A list of functions that are tried when expanding.")

(defvar er/save-mode-excursion nil
"A function to save excursion state when expanding.")

(defsubst er--first-invocation ()
"t if this is the first invocation of `er/expand-region' or `er/contract-region'."
(not (memq last-command '(er/expand-region er/contract-region))))

(defun er--prepare-expanding ()
(when (and (er--first-invocation)
(not (use-region-p)))
Expand Down Expand Up @@ -87,11 +90,12 @@ moving point or mark as little as possible."
(try-list er/try-expand-list)
(best-start (point-min))
(best-end (point-max))
(set-mark-default-inactive nil))
;; (set-mark-default-inactive nil)
)

;; add hook to clear history on buffer changes
(unless er/history
(add-hook 'after-change-functions 'er/clear-history t t))
(add-hook 'after-change-functions #'er/clear-history t t))

;; remember the start and end points so we can contract later
;; unless we're already at maximum size
Expand Down Expand Up @@ -215,10 +219,11 @@ before calling `er/expand-region' for the first time."
t)
(or (minibufferp) (message "%s" msg)))))

(if (fboundp 'set-temporary-overlay-map)
(fset 'er/set-temporary-overlay-map 'set-temporary-overlay-map)
;; Backport this function from newer emacs versions
(defun er/set-temporary-overlay-map (map &optional keep-pred)
(defalias 'er/set-temporary-overlay-map
(if (fboundp 'set-temporary-overlay-map) ;Emacs≥24.3
#'set-temporary-overlay-map
;; Backport this function from newer emacs versions
(lambda (map &optional keep-pred)
"Set a new keymap that will only exist for a short period of time.
The new keymap to use must be given in the MAP variable. When to
remove the keymap depends on user input and KEEP-PRED:
Expand Down Expand Up @@ -252,29 +257,28 @@ remove the keymap depends on user input and KEEP-PRED:
(fset clearfunsym clearfun)
(add-hook 'pre-command-hook clearfunsym)

(push alist emulation-mode-map-alists))))
(push alist emulation-mode-map-alists)))))

(defadvice keyboard-quit (before collapse-region activate)
(advice-add 'keyboard-quit :before #'er--collapse-region-before)
(advice-add 'cua-cancel :before #'er--collapse-region-before)
(defun er--collapse-region-before (&rest _)
;; FIXME: Re-use `er--first-invocation'?
(when (memq last-command '(er/expand-region er/contract-region))
(er/contract-region 0)))

(defadvice minibuffer-keyboard-quit (around collapse-region activate)
(advice-add 'minibuffer-keyboard-quit
:around #'er--collapse-region-minibuffer-keyboard-quit)
(defun er--collapse-region-minibuffer-keyboard-quit (orig-fun &rest args)
;; FIXME: Re-use `er--first-invocation'?
(if (memq last-command '(er/expand-region er/contract-region))
(er/contract-region 0)
ad-do-it))
(apply orig-fun args)))

(defadvice cua-cancel (before collapse-region activate)
(when (memq last-command '(er/expand-region er/contract-region))
(er/contract-region 0)))

(defun er/clear-history (&rest args)
(defun er/clear-history (&rest _)
"Clear the history."
(setq er/history '())
(remove-hook 'after-change-functions 'er/clear-history t))

(defsubst er--first-invocation ()
"t if this is the first invocation of er/expand-region or er/contract-region"
(not (memq last-command '(er/expand-region er/contract-region))))
(remove-hook 'after-change-functions #'er/clear-history t))

(defun er--point-is-surrounded-by-white-space ()
(and (or (memq (char-before) er--blank-list)
Expand All @@ -283,7 +287,7 @@ remove the keymap depends on user input and KEEP-PRED:

(defun er/enable-mode-expansions (mode add-fn)
(add-hook (intern (format "%s-hook" mode)) add-fn)
(save-window-excursion
(save-window-excursion ;; FIXME: Why?
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (derived-mode-p mode)
Expand Down
20 changes: 5 additions & 15 deletions expand-region-custom.el
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; expand-region-custom.el --- Increase selected region by semantic units.
;;; expand-region-custom.el --- Increase selected region by semantic units. -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2020 Free Software Foundation, Inc
;; Copyright (C) 2012-2023 Free Software Foundation, Inc

;; Author: Magnar Sveen <magnars@gmail.com>
;; Keywords: marking region
Expand Down Expand Up @@ -32,15 +32,13 @@
;;;###autoload
(defcustom expand-region-preferred-python-mode 'python
"The name of your preferred python mode"
:group 'expand-region
:type '(choice (const :tag "Emacs' python.el" 'python)
(const :tag "fgallina's python.el" 'fgallina-python)
(const :tag "python-mode.el" 'python-mode)))
:type '(choice (const :tag "Emacs' python.el" python)
(const :tag "fgallina's python.el" fgallina-python)
(const :tag "python-mode.el" python-mode)))

;;;###autoload
(defcustom expand-region-guess-python-mode t
"If expand-region should attempt to guess your preferred python mode"
:group 'expand-region
:type '(choice (const :tag "Guess" t)
(const :tag "Do not guess" nil)))

Expand All @@ -56,40 +54,34 @@
"If set to a string of a single character (try \"e\"), then the
contents of the most recent expand or contract command will
always be copied to the register named after that character."
:group 'expand-region
:type 'string)

;;;###autoload
(defcustom expand-region-skip-whitespace t
"If expand-region should skip past whitespace on initial expansion"
:group 'expand-region
:type '(choice (const :tag "Skip whitespace" t)
(const :tag "Do not skip whitespace" nil)))

;;;###autoload
(defcustom expand-region-fast-keys-enabled t
"If expand-region should bind fast keys after initial expand/contract"
:group 'expand-region
:type '(choice (const :tag "Enable fast keys" t)
(const :tag "Disable fast keys" nil)))

;;;###autoload
(defcustom expand-region-contract-fast-key "-"
"Key to use after an initial expand/contract to contract once more."
:group 'expand-region
:type 'string)

;;;###autoload
(defcustom expand-region-reset-fast-key "0"
"Key to use after an initial expand/contract to undo."
:group 'expand-region
:type 'string)

;;;###autoload
(defcustom expand-region-exclude-text-mode-expansions
'(html-mode nxml-mode)
"List of modes which derive from `text-mode' for which text mode expansions are not appropriate."
:group 'expand-region
:type '(repeat (symbol :tag "Major Mode" unknown)))

;;;###autoload
Expand All @@ -100,7 +92,6 @@ If set to t, and the cursor is already at the beginning of the new region,
keep it there; otherwise, put it at the end of the region.
If set to nil, always place the cursor at the beginning of the region."
:group 'expand-region
:type '(choice (const :tag "Smart behaviour" t)
(const :tag "Standard behaviour" nil)))

Expand All @@ -111,7 +102,6 @@ If set to nil, always place the cursor at the beginning of the region."
;;;###autoload
(defcustom expand-region-subword-enabled nil
"Whether expand-region should use subword expansions."
:group 'expand-region
:type '(choice (const :tag "Enable subword expansions" t)
(const :tag "Disable subword expansions" nil)))

Expand Down
Loading

0 comments on commit 8493232

Please sign in to comment.