(and (not (use-region-p)) - (<= orig-pos (point)))))) + ;; Corrent line is properly indented + (= (current-column) + (let ((indent (calculate-lisp-indent))) + (if (listp indent) + (car indent) + indent))) + ;; Point at indentation + (= orig-pos (point)))))) Revising the diff to account for nil indent and to put the most expensive check last: - (and (not (use-region-p)) - (<= orig-pos (point)))))) + (and (= orig-pos (point)) + (not (use-region-p)) + ;; Corrent line is properly indented + (let ((indent (calculate-lisp-indent))) + (and indent + (= (current-column) + (if (listp indent) + (car indent) + indent)))))))) Regarding: + ;; Only pass an explicit numeric prefix, not `C-u' prefix. + (unless (listp parg) + (prefix-numeric-value parg))) What sort of use cases do prefix args as lists normally serve? From what I see, they come in powers of four, which doesn't seem applicable to adjust-parens. The Elisp manual entry doesn't really cover the "why" of this kind of prefix arg. + (cl-callf or parg 1) + ;; Negative prefix arg inverts the behavior + (when (< parg 0) + (setq parg (- parg) + adjust-function + (cl-case adjust-function + (adjust-close-paren-for-indent 'adjust-close-paren-for-dedent) + (adjust-close-paren-for-dedent 'adjust-close-paren-for-indent) + (otherwise (error "Unknown adjust-function: %s" adjust-function))))) + (when (> parg 0) Processing negative prefix arg makes sense. I think it is cleaner to do it one level higher and pass the right function as the adjust-function argument. eg: (adjust-parens-and-indent (if (< parg 0) 'adjust-close-paren-for-dedent 'adjust-close-paren-for-indent) (abs parg)) + ;; Move forward (but not back) to end of indentation (but don't + ;; change the indentation unlike `indent-for-tab-command'. + (when (< (current-column) (current-indentation)) + (back-to-indentation)))) I figured since is unbound in 'emacs -Q', adjust-parens should do nothing in this case. The analogous in Python appears to also do nothing if it does not dedent. It might be easier to decide what to do here with better perspective about how people use . +(define-globalized-minor-mode global-adjust-parens-mode adjust-parens-mode (lambda () - (local-set-key (kbd "TAB") 'lisp-indent-adjust-parens) - (local-set-key (kbd "") 'lisp-dedent-adjust-parens))) + ;; Add or remove hook + (funcall (if global-adjust-parens-mode #'add-hook #'remove-hook) + 'after-change-major-mode-hook + #'adjust-parens-after-change-mm-hook) + ;; Run the hook in existing buffers to enable/disable the mode + (dolist (buf (buffer-list)) + (with-current-buffer buf + (adjust-parens-after-change-mm-hook))))) adjust-parens doesn't seem global in nature, so I'm uncertain about the choice of a global minor mode. I think a local minor mode makes sense though. Then the user would enable it in major modes by eg: (add-hook 'emacs-lisp-mode-hook #'adjust-parens-mode)