comparison libgui/src/m-editor/file-editor-tab.cc @ 31004:2b4f7287aa3a stable

fix removing trailing spaces on closing a modified file (bug #62271) * file-editor-tab.cc (do_save_file): remove trailing spaces before the file is opened, make sure edit area is writeable before removing spaces, open the file in read-write mode avoiding truncation of existing parts of the file
author Torsten Lilge <ttl-octave@mailbox.org>
date Mon, 16 May 2022 23:00:27 +0200
parents 796f54d4ddbf
children a0c8c28c38cb 7ea420f2c722
comparison
equal deleted inserted replaced
31002:c05aa021e971 31004:2b4f7287aa3a
2188 // stop watching file 2188 // stop watching file
2189 QStringList trackedFiles = m_file_system_watcher.files (); 2189 QStringList trackedFiles = m_file_system_watcher.files ();
2190 if (trackedFiles.contains (file_to_save)) 2190 if (trackedFiles.contains (file_to_save))
2191 m_file_system_watcher.removePath (file_to_save); 2191 m_file_system_watcher.removePath (file_to_save);
2192 2192
2193 // open the file for writing 2193 // Remove trailing white spaces if desired
2194 if (! file.open (QIODevice::WriteOnly)) 2194 resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
2195 gui_settings *settings = rmgr.get_settings ();
2196
2197 if (settings->value (ed_rm_trailing_spaces).toBool ())
2198 {
2199 // Replace trailing spaces, make sure edit area is writable,
2200 // which is not the case when saving at exit or when closing
2201 // the modified file.
2202 bool ro = m_edit_area->isReadOnly ();
2203 m_edit_area->setReadOnly (false); // allow writing for replace_all
2204 m_edit_area->replace_all ("[ \\t]+$", "", true, false, false);
2205 m_edit_area->setReadOnly (ro); // recover read only state
2206 }
2207
2208 // open the file for writing (use QIODevice::ReadWrite for avoiding
2209 // truncating the previous file contents)
2210 if (! file.open (QIODevice::ReadWrite))
2195 { 2211 {
2196 // Unsuccessful, begin watching file again if it was being 2212 // Unsuccessful, begin watching file again if it was being
2197 // watched previously. 2213 // watched previously.
2198 if (trackedFiles.contains (file_to_save)) 2214 if (trackedFiles.contains (file_to_save))
2199 m_file_system_watcher.addPath (file_to_save); 2215 m_file_system_watcher.addPath (file_to_save);
2217 2233
2218 // set the desired codec (if suitable for contents) 2234 // set the desired codec (if suitable for contents)
2219 QTextCodec *codec = check_valid_codec (); 2235 QTextCodec *codec = check_valid_codec ();
2220 if (! codec) 2236 if (! codec)
2221 return; // No valid codec 2237 return; // No valid codec
2222
2223 // Remove trailing white spaces if desired
2224 resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
2225 gui_settings *settings = rmgr.get_settings ();
2226
2227 if (settings->value (ed_rm_trailing_spaces).toBool ())
2228 m_edit_area->replace_all ("[ \\t]+$", "", true, false, false);
2229 2238
2230 // Save the file 2239 // Save the file
2231 out.setCodec (codec); 2240 out.setCodec (codec);
2232 2241
2233 QApplication::setOverrideCursor (Qt::WaitCursor); 2242 QApplication::setOverrideCursor (Qt::WaitCursor);