Emacs Pretty Symbols in cperl mode: :: must be surrounded my spaces - emacs

I'm using Emacs with PDE and cperl-mode. I really want prettify-symbols-mode to work with it. When just using perl-mode this happens:
Mod::thing->new( {c => 'sea'} );
becomes
Mod∷thing→new( {c ⇒ 'sea'} );
however when using cperl same expression becomes
Mod::thing→new( {c ⇒ 'sea'} );
in cperl-mode the :: only becomes ∷ when it is separated by spaces on both sides.
I have tried adding to the cperl--pretty-symbols-alist and pretty-symbols-alist. and still only the -> and => work.
I thought it might be how cperl defines characters as symbols or words, but the -> and => work just fine without being surrounded by spaces.

I got it. I went to /usr/share/emacs/26.1/lisp/progmodes and deleted a file called cperl-mode.elc. then I went into an archinve here /usr/share/emacs/26.1/lisp/progmodes/cperl-mode.el.gz. I edited line 1498 of cperl-mode.el to be (modify-syntax-entry ?: "." cperl-mode-syntax-table) instead of (modify-syntax-entry ?: "_" cperl-mode-syntax-table). Then I byte-compiled the file and put it back in /usr/share/emacs/26.1/lisp/progmodes.

You can also do this in your init file so you don't have to edit any files that are shipped with Emacs. If you edit files that are part of Emacs, you'll need to update them every time you install or upgrade.
(with-eval-after-load "cperl-mode"
(modify-syntax-entry ?: "." cperl-mode-syntax-table))

Related

How do I make emacs treat '_' as a normal character, not a separation one? [duplicate]

In python mode, when I forward-word. The cursor jumps from H to d (Hello_World). But in another mode(shell-mode or c-mode), the cursor jumps from H to _.
I want the result which i get in Python mode, even in the other mode. What should I do?
PS: I saw a similar question before, I have searched, but I could't find it.
I think you're looking for this:
(modify-syntax-entry ?_ "w")
Underscores will be treated as part of a word. This command will change the syntax table of the mode you're currently in. AFAIK there's no way to change syntax globally. However, you could try modifying the standard syntax table. Most major modes inherit the standard-syntax-table.
(modify-syntax-entry ?_ "w" standard-syntax-table)
If that doesn't work, I guess you have to add mode-hooks for all modes you're using and modify their syntax tables individually.
Things got a little simpler since Emacs 24.4. There's now a M-x superword-mode that has the desired effect.

How to replace '_' by space in emacs

I have a text file which I am only able to look that there is an underscore between some words only using emcas editor but not other editors such as vi. I do not know how to use emacs but I wanted to replace these underscores "_" by space in the emacs editor automated fashion. How can I do that ?
I believe that those underscore aren't really underscore, but non breaking space (U+00A0 unicode char), that Emacs show as underscore with a different color. You probably don't need to replace them, but if this is really needed, just use M-x replace-string and kill and yank one of those non-breaking space in the string to be replaced.
Hit the M-x key-combination (that is, hold meta key - alt on windows - and hit x) type replace-string and hit enter. You can then type [underscore] enter [space] enter.
In Emacs notation:
M-x replace-string RET _ RET " "
Should the previous answer not solve it: Remember that as a coding system error. Check with C-x = if it's char 95.
If not, check variables coding-system-for-read, coding-system-for-write, buffer-file-coding-system
Finally, get emacs core developers at help-gnu-emacs#gnu.org

Emacs mode/function that wraps operators with spaces

I remember that there was a mode for Emacs, or was it an option, can't recall, that prettified the code by placing space after operators like ,, ., wrapped = with spaces, and some other cools stuff that I can't remember.
I reckon that this is pretty much language-specific issue, but I usually put space after commas, and wrap = with spaces. Any ideas about this mode?
You're probably thinking of smart-operator.el.
How about smartchr.el?
This is probably not what you are trying to recall, but I'm sure it works nicely for that purpose.
For example:
(require 'smartchr)
(global-set-key (kbd "=") (smartchr '(" = " " == " "=")))
With this configuration, when you type = several times, = is inserted and it rotates to == to = then back to = .
Here is an introduction by the author (written in Japanese, but with a demo video and example of configuration): http://tech.kayac.com/archive/emacs-tips-smartchr.html

emacs + latex + yasnippet: Why are newlines inserted after a snippet?

Everytime I insert a snippet (with yasnippet) in a .tex document, I obtain a newline after the snippet. This is quite annoying for small snippets that are typically used in text style. How can I avoid that?
I read a bit about the problem (http://code.google.com/p/yasnippet/issues/detail?id=115 or http://yasnippet.googlecode.com/svn/trunk/doc/faq.html) but couldn't find a solution. Reproduce it as follows (I work with Aquamacs 2.3a on Mac OS X 10.6.8 with yasnippet version 0.6.1c):
Define ~/Library/Preferences/Aquamacs Emacs/Preferences.el to be:
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "~/Library/Preferences/Aquamacs Emacs/plugins/yasnippet-0.6.1c/snippets")
define the following snippet (call it "bm.yasnippet" [bm = boldmath]; the star * symbolizes where the cursor ends -- note that there is no newline after the snippet)
# name: \bm{}{}
# key: bm
# --
\bm{$1}*
restart Aquamacs and open a .tex file and type in bm + Tab [this should insert the snippet]
A newline is added after the snippet. This is quite inconvenient since \bm{foo} is typically used in text style, so for example in "The vector \bm{x} is not the null vector". A typical cause of this is that the snippet ends with a newline which is then inserted, too. However, I specifically obtain this behavior even the snippet does not end with a newline.
I can't repro it with plain Emacs. In fact, I had this exact issue, but my problem is I had require-final-newline set to t. So Emacs was adding a newline at the end of my template.
My setup is a little more complicated but the solution for you is probably to set mode-require-final-newline to nil and restart Emacs.
To verify this is the problem, open up the template and check for the final newline.
Thanks to the answers in Temporarly disable adding of newlines in Emacs, I'm using a function to only temporarily disable the adding of final newlines in the current buffer:
(defun disable-final-newline ()
(interactive)
(set (make-local-variable 'require-final-newline) nil))
the reason why u got a new line is that your snippet has space or tab at the end.
Ctrl+e and Ctrl+k to kill them will make it works, nearly 1 hour to figure it out...
I had a similar issue with a few snippets, one of that was \frac{}{} which I use quite often.
The snippet version of frac that I use is not the one bundled with yasnippets.
The issue was that I edited some of the snippets in VIM and when you save the file, VIM automatically appends a newline to it.
To resolve it I had to remove the newline in a different editor e.g. emacs.

GNU Emacs: skeleton-mode, is it still used?

given all the possible solutions to have a template system with GNU Emacs, what do people use today ? I am still using skeleton-mode but as I read it here and there, we must be really few to do so.
What are you using and why ? (maybe I could switch to a more popular tool).
For example, given this snippet:
(define-skeleton mwe:cl-defpackage-skeleton
"Inserts a Common Lisp DEFPACKAGE skeleton."
(skeleton-read "Package: " (if v1
(file-name-sans-extension
(file-name-nondirectory
(buffer-file-name)))))
(if (setq v1 (bobp)) ";;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp;")
& (if buffer-file-coding-system
(concat " Coding:"
(symbol-name
(coding-system-get buffer-file-coding-system
'mime-charset))))
& " -*-"
& \n
& \n "(defpackage #:" str
\n "(:nicknames" ("Nickname: " " #:" str) & ")" | '(kill-whole-line -1)
\n "(:use #:CL" ((slime-read-package-name "USEd package: ") " #:" str) ")"
")" \n
\n
(if v1 "(in-package #:") & str & ")" & \n &
\n
_)
(credits: http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-skeleton)
which (modern) template mode could do the same (and how ;)) ?
Cheers
For the record: even another 7 years later I am still very happy with skeletons. Lots of them available over there in my init files: https://github.com/ska2342/ska-init-files/blob/master/dot.emacs.d/init.el
I use yasnippet.
In my emacs I have this:
(require 'yasnippet-bundle)
In my hook for each mode where I want to use snippets (like my c-mode hook, etc), I have this:
(yas/minor-mode-on)
The "static" snippets I use are available, in the directory structure I use, here:
http://cheeso.members.winisp.net/srcview.aspx?dir=emacs&file=snippets.zip
You need to create the bundle .el file mentioned above, once, when any of the snippets change. do it this way:
(require 'yasnippet)
(yas/compile-bundle
; the starting point
"c:/your/path/yasnippet.el"
; the bundle file to generate
"c:/your/path/yasnippet-bundle.el"
; the snippet dir from which to generate the bundle
"c:/your/path/snippets")
That's it!
Then, when I'm in a C# file and type for<TAB>, I get a template with a for loop. And so on.
I also use yasnippet with dynamic snippet templates. A C# code-completion module I wrote calls yas/expand-snippet with a dynamically constructed string that defines the template to expand.
So, you can type
MyType.Method(<COMPLETE>
...where <COMPLETE> is the code-completion key, and the code-completion module does the lookup on the MyType.Method(, then builds a menu of choices, and pops it up. When the user selects a choice from the menu, the code-completion module builds the template, containing fields for each of the arguments for the selected method. Then it calls yas/expand-snippet and that template is injected into the buffer, just as if it had been a static template. In the dynamically-generated template, each argument to the method gets a "typeover" field, and I just fill it in, tabbing through the fields. Pretty nice.
This "dynamic snippet" idea would work with any code-completion engine. You just need a way to map from a method or function signature, like this:
function(int arg1, string arg2, char arg3)
to a yasnippet template definition string, which looks like this:
function(${1:int arg1}, ${2:string arg2}, ${3:char arg3})
And that's a pretty trivial piece of elisp.
I haven't used skeleton mode much, but I use YASnippet while coding in Ruby and C. Its pretty useful, but I suspect skeleton mode is far more powerful.
The emacs wiki lists Yasnippet as a possible replacement for skeleton. The snippets that come with yasnippet are pretty good, but you should really write your own, as the true power lies there.