simple useful functions from Tak Ota
[elbb.git] / code / region-or.el
blob468bae83817fc164713eaddc65fb81b463309e08
1 ;;; region-or.el --- functions dealing with region
3 ;; Keywords: lisp
5 ;; This file is free software; you can redistribute it
6 ;; and/or modify it under the terms of the GNU General
7 ;; Public License as published by the Free Software
8 ;; Foundation; either version 2, or (at your option)
9 ;; any later version.
11 ;; This file is distributed in the hope that it will be
12 ;; useful, but WITHOUT ANY WARRANTY; without even the
13 ;; implied warranty of MERCHANTABILITY or FITNESS FOR A
14 ;; PARTICULAR PURPOSE. See the GNU General Public
15 ;; License for more details.
17 ;; You should have received a copy of the GNU General
18 ;; Public License along with GNU Emacs; see the file
19 ;; COPYING. If not, write to the Free Software
20 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;; Purpose of this file is to collect as to discuss some
26 ;; small and general forms
28 ;; Everyone interested in Emacs Lisp should easily be
29 ;; able to augment or change this git-archive
31 ;; Instead of marking a region, quite often a default
32 ;; value may do the right thing - thus saving keystrokes.
34 ;; Below an example function: Maybe better methods
35 ;; exist? Comments welcome!
37 ;; Author: Andreas Roehler <andreas.roehler@online.de>
39 (defun return-region-or-line (&optional arg beg end)
40 " "
41 (interactive "p")
42 (let* ((beg (cond (beg beg)
43 ((region-active-p)
44 (region-beginning))
45 (t (line-beginning-position))))
46 (end (cond (end (copy-marker end))
47 ((region-active-p)
48 (copy-marker (region-end)))
49 (t (copy-marker (line-end-position)))))
50 (text (buffer-substring-no-properties beg end)))
51 (when arg (message "%s" text))
52 text))
54 ;; or as below
56 ;; for (symbol-end-position-atpt) and
57 ;; (symbol-beginning-position-atpt) you need
58 ;; thingatpt-utils from
59 ;; https://code.launchpad.net/s-x-emacs-werkstatt/
61 ;; or replace this form with another one returning
62 ;; beg- end positions
64 (defun return-region-or-symbol (&optional arg beg end)
65 " "
66 (interactive "p")
67 (let* ((beg (cond (beg beg)
68 ((region-active-p)
69 (region-beginning))
70 (t (symbol-beginning-position-atpt))))
71 (end (cond (end (copy-marker end))
72 ((region-active-p)
73 (copy-marker (region-end)))
74 (t (copy-marker (symbol-end-position-atpt)))))
75 (text (buffer-substring-no-properties beg end)))
76 (when arg (message "%s" text))
77 text))