I'd like to use mark-paragraph (also for movement with forward/backward-paragraph) in org-mode buffers in the same way as in other major modes, i.e. to mark a continuous region delimited by empty lines. This should apply also to headings, list items, lines starting with '#' etc. – i.e. I'd like for the purposes of paragraph editing to everything be treated as regular text.
Is this possible?
See the variables paragraph-start and paragraph-separate, and possibly also the use-hard-newlines function which is related (but probably not actually relevant in this case).
(defun use-default-paragraph-delimiters ()
(setq paragraph-start (default-value 'paragraph-start)
paragraph-separate (default-value 'paragraph-separate)))
(add-hook 'org-mode-hook 'use-default-paragraph-delimiters)
Edit: Admittedly, org-mode might depend upon its paragraph definitions for more than just interactive marking and moving, so here is a more targeted approach for customising the paragraph definitions for those commands only when called interactively using their key bindings.
(defmacro with-default-paragraph-definition (&rest body)
"Evaluate body forms using the default definition of a paragraph."
`(let ((paragraph-start (default-value 'paragraph-start))
(paragraph-separate (default-value 'paragraph-separate)))
,#body))
(defalias 'my-org-mark-paragraph 'mark-paragraph)
(defadvice my-org-mark-paragraph
(around my-org-mark-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defalias 'my-org-forward-paragraph 'forward-paragraph)
(defadvice my-org-forward-paragraph
(around my-org-forward-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defalias 'my-org-backward-paragraph 'backward-paragraph)
(defadvice my-org-backward-paragraph
(around my-org-backward-paragraph-advice activate)
(with-default-paragraph-definition ad-do-it))
(defun my-org-paragraph-overrides ()
"Use the default paragraph definitions in org-mode
when marking or moving by paragraph."
(local-set-key [remap mark-paragraph] 'my-org-mark-paragraph)
(local-set-key [remap forward-paragraph] 'my-org-forward-paragraph)
(local-set-key [remap backward-paragraph] 'my-org-backward-paragraph))
(add-hook 'org-mode-hook 'my-org-paragraph-overrides)
You can try customising the paragraph-start variable. I'm not sure
what would be appropriate here, org sets it to something quite elaborate
as you see in the quoted docstring below. Setting it to the default
might work, or you could try use-hard-newlines as mentioned in the quote below.
paragraph-start is a variable defined in `paragraphs.el'.
Its value is
"\f\|[ ]$\|\+ \|[ ]#\|\([ ]\([-+]\|\(\([0-9]+\)[.)]\)\)\|[ ]+\*\)\([ ]+\|$\)\|[ ]*[:|]\|\$\$\|\\\(begin\|end\|[][]\)"
Original value was "\f\|[ ]*$"
Local in buffer coding.org; global value is "\f\|[ ]*$"
This variable is safe as a file local variable if its value
satisfies the predicate `stringp'.
Documentation:
Regexp for beginning of a line that starts OR separates paragraphs.
This regexp should match lines that separate paragraphs
and should also match lines that start a paragraph
(and are part of that paragraph).
This is matched against the text at the left margin, which is not necessarily
the beginning of the line, so it should never use "^" as an anchor. This
ensures that the paragraph functions will work equally well within a region
of text indented by a margin setting.
The variable `paragraph-separate' specifies how to distinguish
lines that start paragraphs from lines that separate them.
If the variable `use-hard-newlines' is non-nil, then only lines following a
hard newline are considered to match.
Related
Is there a way to add a reference / citation format which inserts the reference / citation label directly into the buffer without enclosing it into a macro?
To be clear: When I hit C-c ) AUCTeX prompts for a reference format, when I hit return it'll insert ~\ref{LABEL} into the buffer [after selecting the appropriate reference in the next buffer]. I would like to add a reference format which is bound to \?s (space) that inserts only the LABEL part.
That is to say: I hit C-c ), then <space>, then select the reference and... tadaa there's LABEL in the buffer.
[Edit:] I have tried
(eval-after-load "latex"
'(progn
(add-to-list
'reftex-ref-style-alist
'("Default" t
(("" ?\s))))))
however this encloses the label in curly braces and prepends ~ if there's a word before point.
I have created a solution by adding an around-advice to reftex-reference, however, it's not a pretty solution. I'll put it up as an answer but I'm still hoping for a better solution.
(eval-after-load "latex"
'(progn
(add-to-list
'reftex-ref-style-alist
'("Default" t
(("LABEL ONLY" ?\s))))
(defadvice reftex-format-special (around reftex-format-special-labely-only activate)
"Advice `reftex-format-special' such that if
REFSTYLE is \"LABEL ONLY\" it will insert
only the reference's label."
(if (string= (ad-get-arg 2) "LABEL ONLY")
(setq ad-return-value (format "%s" (ad-get-arg 0)))
ad-do-it))))
I'm new to emacs, and its indenting is driving me up the walls. It's too smart for its own good; it (incorrectly) thinks it knows how I want to format my source, but I don't have time to chase down every setting for every mode for every different language that I write code for; and many of those languages don't have any mode enabled at all.
Here's the behaviour I'd like:
TAB inserts indent
RET inserts a new line then copies the blank characters from the start of the previous line to the first non-blank character, or end of line, whichever comes sooner
DEL (backspace key) in the blank text between line start and first non-blank character / end of line deletes one indent if possible, otherwise single character like normal
No auto-indent on {
No auto-unindent on }
In fact, no smart-ass indenting behaviour anywhere anytime, just copy previous line's indent on RET.
Two variables to be configured per source file format: display tab width, and contents of indent. Preferably these can be configured for random source code formats without having to write a major mode for them, unless writing a major mode is a one-liner in .emacs, consisting of two setqs.
This would get me logical and consistent behaviour across all languages. It would leave the work of formatting the code to me, but that's OK, I've been doing that for 20 years, and I know how to make other macros that make it efficient. More importantly, it saves me from endless fiddling with configuration settings trying to get the automatic behaviour to suit my preferences. And my macros can rely on consistent behaviour so they work correctly in all modes.
Is the above possible? Surely someone else has done this before? Is there some minor mode out there that makes it so?
Here's the code:
(setq tab-width 4)
(defun plain-tab ()
(interactive)
(insert (make-string tab-width ?\ )))
(defun plain-ret ()
(interactive)
(looking-back "^\\( +\\).*")
(newline)
(insert (match-string 1)))
(defun plain-del ()
(interactive)
(backward-delete-char
(if (looking-back (format " \\{%d\\}" tab-width)) tab-width 1)))
(defvar all-the-mode-maps
'(c-mode-map c++-mode-map java-mode-map
js-mode-map emacs-lisp-mode-map
clojure-mode-map))
(require 'cc-mode)
(require 'js)
(require 'clojure-mode)
(eval `(mapc
(lambda(map)
(define-key map [tab] 'plain-tab)
(define-key map [return] 'plain-ret)
(define-key map [backspace] 'plain-del)
(define-key map "{" (lambda()(interactive)(insert "{")))
(define-key map "}" (lambda()(interactive)(insert "}"))))
(list ,#all-the-mode-maps)))
How to add an arbitrary character (hyphen, for example) to forward/backward-word functionality, so Emacs would handle words like "k-vector" as a single word. The same question about double-click on the word.
Syntax tables define which character is considered a word constituent. You can read about it here.
Emacs's notion of "word" is rather fixed and doesn't quite match what you want. You can mess with the syntax-table, but it might break functionality of the major-mode.
AFAICT what you want is to handle what Emacs calls "symbols" rather than "words". E.g. use forward-symbol and backward-symbol commands.
The easiest way to use those other commands might be to enable superword-mode.
Add following to your .emacs file to make hyphen included in words for every major mode:
(add-hook 'after-change-major-mode-hook
(lambda ()
(modify-syntax-entry ?- "w")))
You can use this code it use regular expression it use space or parents as word separator
(defun backward-word-with-dash ()
(interactive)
(search-backward-regexp "[ ()][A-Za-z\-]+ *")
(forward-char))
(defun forward-word-with-dash ()
(interactive)
(search-forward-regexp " *[A-Za-z\-]+[ ()]")
(backward-char))
(global-set-key "\M-b" 'backward-word-with-dash)
(global-set-key "\M-f" 'forward-word-with-dash)
if you want other characters just add it to the regex
I've looked through a number of other questions and el files looking for something i could modify to suit my needs but I'm having trouble so I came to the experts.
Is there anyway to have a key behave differently depending on where in the line the cursor is?
To be more specific I'd like to map the tab key to go to the end of the line if I'm in the middle of the line but work as a tab normally would if my cursor is positioned at the beginning of the line.
So far I have braces and quotes auto-pairing and re-positioning the cursor within them for C++/Java etc. I'd like to use the tab key to end-of-line if for example a function doesn't have any arguments.
Behaving differently depending on where point is in the line is the easy bit (see (if (looking-back "^") ...) in the code). "[Working] as a tab normally would" is the harder bit, as that's contextual.
Here's one approach, but I was thinking afterwards that a more robust method would be to define a minor mode with its own binding for TAB and let that function look up the fallback binding dynamically. I wasn't sure how to do that last bit, but there's a solution right here:
Emacs key binding fallback
(defvar my-major-mode-tab-function-alist nil)
(defmacro make-my-tab-function ()
"Return a major mode-specific function suitable for binding to TAB.
Performs the original TAB behaviour when point is at the beginning of
a line, and moves point to the end of the line otherwise."
;; If we have already defined a custom function for this mode,
;; return that (otherwise that would be our fall-back function).
(or (cdr (assq major-mode my-major-mode-tab-function-alist))
;; Otherwise find the current binding for this mode, and
;; specify it as the fall-back for our custom function.
(let ((original-tab-function (key-binding (kbd "TAB") t)))
`(let ((new-tab-function
(lambda ()
(interactive)
(if (looking-back "^") ;; point is at bol
(,original-tab-function)
(move-end-of-line nil)))))
(add-to-list 'my-major-mode-tab-function-alist
(cons ',major-mode new-tab-function))
new-tab-function))))
(add-hook
'java-mode-hook
(lambda () (local-set-key (kbd "TAB") (make-my-tab-function)))
t) ;; Append, so that we run after the other hooks.
This page of Emacs Wiki lists several packages (smarttab, etc.) which make TAB do different things depending on the context. You can probably modify one of them to do what you want.
I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.
Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.
I want to use two spaces per indent, tabs-to-spaces.
When I press RETURN, the new line indentation must match the previous line.
When I press TAB on the leading whitespace, the line contents must be indented by one indentation unit.
When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.
Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.
Update:
(Would put this in a comment, but it needs formatting)
If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:
local run = function(...)
x
"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.
Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.
Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.
In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)
Put the following code in the file named .emacs in your home directory (~):
(setq-default indent-tabs-mode nil) ; use spaces for indentation
(defvar my-indentation-width 2
"The number of spaces I prefer for line indentation.")
(defun my-enter ()
"Inserts a newline character then indents the new line just
like the previous line"
(interactive)
(newline)
(indent-relative-maybe))
(defun my-indent ()
"When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
(interactive)
(insert (make-string my-indentation-width ? )))
(defun my-indentation-setup ()
"Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
(local-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent))
(defun delete-trailing-whitespace-and-blank-lines ()
"Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
(interactive)
(let ((point (point)))
(delete-trailing-whitespace)
(goto-char (point-max))
(delete-blank-lines)
(goto-char (min point (point-max)))))
;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)
;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)
;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)
This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.
For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.
Fixing TAB and RETURN:
(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)
Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.