From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.help Subject: Re: DWIM region (was: Re: count regexp hits) Date: Thu, 04 Jan 2018 22:48:42 +0000 Message-ID: References: <86shbmcldx.fsf@zoho.com> <86d12pcvgj.fsf@zoho.com> <86d12pb988.fsf_-_@zoho.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: blaine.gmane.org 1515106063 4567 195.159.176.226 (4 Jan 2018 22:47:43 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 4 Jan 2018 22:47:43 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jan 04 23:47:38 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eXEIJ-00006S-5j for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 23:47:27 +0100 Original-Received: from localhost ([::1]:49668 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eXEKI-0003nc-Dm for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 17:49:30 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eXEJk-0003lR-ED for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:48:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eXEJj-0005cG-5s for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:48:56 -0500 Original-Received: from mail-yb0-x230.google.com ([2607:f8b0:4002:c09::230]:44220) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eXEJi-0005ao-Lx for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:48:55 -0500 Original-Received: by mail-yb0-x230.google.com with SMTP id h189so1228332ybc.11 for ; Thu, 04 Jan 2018 14:48:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=D93kgHAlXCoM0SWlAqniQyHJLGnVbjVbMhLWczMtG2I=; b=g6fbQNhYn5Tyose/q15d9B5+u8IFG8cNKsOzdFw7aIpV5ME88IrRT3T0LM+wCYj8LB wP5ftShYcKEb2Yt7P8Esj3Yfpbj8mbpB2xogSe8R6KL2n3NxONFe2itINDlzenKZkaXS i4Dy9zpLOQoYC4+D8EoCXcmnbC/WCLm9QXT2LwAArCezgT4JXcLOlwP1yWtZmZG/a1mZ BqX9sQsB/kxXuCEVMNnp6hBUZC5jp/oI5E9HxNrm0449ah9IH6EVV3wV2MedX0BT1+Bq LJ2EpW2zzagebhKIsS1tPnFYtANsqyrJZW7KOH8qRh4WnMys6wZSf7jINNfFxvy6705E 7HwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=D93kgHAlXCoM0SWlAqniQyHJLGnVbjVbMhLWczMtG2I=; b=r4fZaVGReKATKHXgNlh6XOkh/2TGipjnGxDQjtt03jKJI50laCXUAD4ZY1pMNRqbBV Z8m/pYndIt+OVTMdFCIAd6YGir01SnrL8zft7uaXDVXdcUg+UU/h2wJ8oKQSDo12pZYk p9LQ4WnjrYp3OnjPs6vA7t8RKRTbZ15YwqmII6vq0+Awij8dEViEmfb7+hxPohF2oFgP E3+OCANJ2Kc6Lke4L6IEEnmz4XkbMDlnKg0vvEx725zzlKtb9Tv02AknlmbYwuJgopIU a7saXCjoZ9i7o8Ok2k7Q4hEy3Pq1RMrjMwBwbFOrcwjJEIlk0yMZQkFlNNt7ZaVh38P/ qy5Q== X-Gm-Message-State: AKGB3mLRu3ytXK006F2dshFjvykoI0IW8cszqnT/EDmhxZQxR3bKLUDT KBkOH92USA2LZqvJCg0smGZkPS2XWInXStdehK4= X-Google-Smtp-Source: ACJfBosk3PVLjjjN4vstGYacn9ufwZIG+9XpRKFGBhA7v1YkXnYvwKqW9Xw9/ew6FUEpNYmU41BJl37yzxX6BId7KHM= X-Received: by 10.37.64.75 with SMTP id n72mr1094082yba.499.1515106133677; Thu, 04 Jan 2018 14:48:53 -0800 (PST) In-Reply-To: <86d12pb988.fsf_-_@zoho.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4002:c09::230 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115581 Archived-At: On Thu, Jan 4, 2018 at 4:35 PM Emanuel Berg wrote: > BTW this method on acting on the region (if > any) I have found very useful and it has > recurred many times in my code: > > Is it the canonical way of doing it as well? > I don't think there's a canonical way.. here's how I dealt with the same problem: ;;; Operate on Region or Whole Buffer (defvar modi/region-or-whole-fns '(indent-region eval-region) "List of functions to act on the whole buffer if no region is selected.") (defun modi/advice-region-or-whole (orig-fun &rest args) "Advice function that applies ORIG-FUN to the whole buffer if no region is selected. http://thread.gmane.org/gmane.emacs.help/109025/focus=109102 " ;; Required to override the "r" argument of `interactive' in functions like ;; `indent-region' so that they can be called without an active region. (interactive (if (use-region-p) (list (region-beginning) (region-end)) (list (point-min) (point-max)))) (prog1 ; Return value of the advising fn needs to be the same as ORIG-FUN (apply orig-fun args) (when (and (called-interactively-p 'interactive) (not (use-region-p))) (message "Executed %s on the whole buffer." (propertize (symbol-name this-command) 'face 'font-lock-function-name-face))))) (dolist (fn modi/region-or-whole-fns) (advice-add fn :around #'modi/advice-region-or-whole)) Now with that setup, I simply need to add functions, where I want the same whole buffer or region behavior, to the modi/region-or-whole-fns variable. Ref: https://github.com/kaushalmodi/.emacs.d/blob/c7e3d5bae08105a7a1853566b44ea65c73c80e69/setup-files/setup-editing.el#L1001-L1028 -- Kaushal Modi